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

feat: wrapper creation for libbpf_probe_bpf_helper #446

Merged
merged 2 commits into from
Jul 2, 2024
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
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