Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linter and unit tests in Darwin #124

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions pkg/ebpf/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package ebpfcommon

import (
"io"
"syscall"
"time"

"github.com/cilium/ebpf"
"github.com/grafana/ebpf-autoinstrument/pkg/goexec"
"golang.org/x/sys/unix"
)

//go:generate $BPF2GO -cc $BPF_CLANG -cflags $BPF_CFLAGS -target bpf -type http_request_trace bpf ../../../bpf/http_trace.c -- -I../../../bpf/headers
Expand Down Expand Up @@ -69,7 +67,3 @@ type Filter struct {
io.Closer
Fd int
}

func (f *Filter) Close() error {
return syscall.SetsockoptInt(f.Fd, unix.SOL_SOCKET, unix.SO_DETACH_BPF, 0)
}
11 changes: 11 additions & 0 deletions pkg/ebpf/common/common_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ebpfcommon

import (
"syscall"

"golang.org/x/sys/unix"
)

func (f *Filter) Close() error {
return syscall.SetsockoptInt(f.Fd, unix.SOL_SOCKET, unix.SO_DETACH_BPF, 0)
}
33 changes: 0 additions & 33 deletions pkg/ebpf/instrumenter.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package ebpf

import (
"encoding/binary"
"fmt"
"io"
"syscall"
"unsafe"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
ebpfcommon "github.com/grafana/ebpf-autoinstrument/pkg/ebpf/common"
"github.com/grafana/ebpf-autoinstrument/pkg/goexec"
"golang.org/x/exp/slog"
"golang.org/x/sys/unix"
)

type instrumenter struct {
Expand Down Expand Up @@ -104,34 +99,6 @@ func (i *instrumenter) kprobe(funcName string, programs ebpfcommon.FunctionProgr
return nil
}

func attachSocketFilter(filter *ebpf.Program) (int, error) {
fd, err := unix.Socket(unix.AF_PACKET, unix.SOCK_RAW, int(htons(unix.ETH_P_ALL)))
if err == nil {
ssoErr := syscall.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_ATTACH_BPF, filter.FD())
if ssoErr != nil {
return -1, ssoErr
}
return int(fd), nil
}

return -1, err
}

func isLittleEndian() bool {
var a uint16 = 1

return *(*byte)(unsafe.Pointer(&a)) == 1
}

func htons(a uint16) uint16 {
if isLittleEndian() {
var arr [2]byte
binary.LittleEndian.PutUint16(arr[:], a)
return binary.BigEndian.Uint16(arr[:])
}
return a
}

func (i *instrumenter) sockfilters(p Tracer) error {
for _, filter := range p.SocketFilters() {
fd, err := attachSocketFilter(filter)
Expand Down
11 changes: 11 additions & 0 deletions pkg/ebpf/instrumenter_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ebpf

import (
"github.com/cilium/ebpf"
)

// placeholder to avoid Darwin linter and unit tests to fail

func attachSocketFilter(_ *ebpf.Program) (int, error) {
return 0, nil
}
38 changes: 38 additions & 0 deletions pkg/ebpf/instrumenter_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ebpf

import (
"encoding/binary"
"syscall"
"unsafe"

"github.com/cilium/ebpf"
"golang.org/x/sys/unix"
)

func attachSocketFilter(filter *ebpf.Program) (int, error) {
fd, err := unix.Socket(unix.AF_PACKET, unix.SOCK_RAW, int(htons(unix.ETH_P_ALL)))
if err == nil {
ssoErr := syscall.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_ATTACH_BPF, filter.FD())
if ssoErr != nil {
return -1, ssoErr
}
return fd, nil
}

return -1, err
}

func isLittleEndian() bool {
var a uint16 = 1

return *(*byte)(unsafe.Pointer(&a)) == 1
}

func htons(a uint16) uint16 {
if isLittleEndian() {
var arr [2]byte
binary.LittleEndian.PutUint16(arr[:], a)
return binary.BigEndian.Uint16(arr[:])
}
return a
}