Skip to content

Commit

Permalink
chore(feat): create type BPFFunc in libbpfgo
Browse files Browse the repository at this point in the history
  • Loading branch information
rscampos committed Jul 3, 2024
1 parent 73f0dea commit a923c7c
Show file tree
Hide file tree
Showing 3 changed files with 671 additions and 13 deletions.
18 changes: 8 additions & 10 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,47 @@ package libbpfgo

import (
"testing"

"github.com/aquasecurity/libbpfgo/helpers"
)

func TestFuncSupportbyType(t *testing.T) {
tt := []struct {
progType BPFProgType
funcId helpers.BPFFunc
funcId BPFFunc
supported bool
}{
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncMapLookupElem,
funcId: BPFFuncMapLookupElem,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncLoop,
funcId: BPFFuncLoop,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncKtimeGetNs,
funcId: BPFFuncKtimeGetNs,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncSysBpf,
funcId: BPFFuncSysBpf,
supported: false,
},
{
progType: BPFProgTypeLsm,
funcId: helpers.BPFFuncGetCurrentCgroupId,
funcId: BPFFuncGetCurrentCgroupId,
supported: false,
},
{
progType: BPFProgTypeSkLookup,
funcId: helpers.BPFFuncGetCurrentCgroupId,
funcId: BPFFuncGetCurrentCgroupId,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncSockMapUpdate,
funcId: BPFFuncSockMapUpdate,
supported: false,
},
}
Expand Down
7 changes: 4 additions & 3 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import "C"
import (
"fmt"
"syscall"

"github.com/aquasecurity/libbpfgo/helpers"
)

//
Expand Down Expand Up @@ -98,7 +96,10 @@ func BPFMapTypeIsSupported(mapType MapType) (bool, error) {
return supportedC == 1, nil
}

func BPFHelperIsSupported(progType BPFProgType, funcId helpers.BPFFunc) (bool, error) {
// Probe may succeed even if program load fails, for unprivileged users check that we did not fail because of
// insufficient permissions.
// ref: https://github.com/libbpf/bpftool/blob/77a72987353fcae8ce330fd87d4c7afb7677a169/src/feature.c#L683-L700
func BPFHelperIsSupported(progType BPFProgType, funcId BPFFunc) (bool, error) {
supportedC := C.libbpf_probe_bpf_helper(C.enum_bpf_prog_type(int(progType)), C.enum_bpf_func_id(int(funcId)), nil)
if supportedC < 0 {
return false, syscall.Errno(-supportedC)
Expand Down
Loading

0 comments on commit a923c7c

Please sign in to comment.