Skip to content

Commit

Permalink
feat: wrapper creation for libbpf_probe_bpf_helper (#446)
Browse files Browse the repository at this point in the history
* feat: wrapper creation for libbpf_probe_bpf_helper

* test: test cases for the new wrapper
  • Loading branch information
rscampos authored Jul 2, 2024
1 parent 560e67b commit 73f0dea
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
59 changes: 59 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package libbpfgo

import (
"testing"

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

func TestFuncSupportbyType(t *testing.T) {
tt := []struct {
progType BPFProgType
funcId helpers.BPFFunc
supported bool
}{
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncMapLookupElem,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncLoop,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncKtimeGetNs,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncSysBpf,
supported: false,
},
{
progType: BPFProgTypeLsm,
funcId: helpers.BPFFuncGetCurrentCgroupId,
supported: false,
},
{
progType: BPFProgTypeSkLookup,
funcId: helpers.BPFFuncGetCurrentCgroupId,
supported: true,
},
{
progType: BPFProgTypeKprobe,
funcId: helpers.BPFFuncSockMapUpdate,
supported: false,
},
}

for _, tc := range tt {
support, _ := BPFHelperIsSupported(tc.progType, tc.funcId)
// This may fail if the bpf helper support for a specific program changes in future.
if support != tc.supported {
t.Errorf("expected %v, got %v", tc.supported, support)
}
}
}
11 changes: 11 additions & 0 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import "C"
import (
"fmt"
"syscall"

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

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

func BPFHelperIsSupported(progType BPFProgType, funcId helpers.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)
}

return supportedC == 1, nil
}

//
// Misc
//
Expand Down

0 comments on commit 73f0dea

Please sign in to comment.