From f0722d3927ea4cfffc56e7d9590fbede94eb2d96 Mon Sep 17 00:00:00 2001 From: Francisco Javier Honduvilla Coto Date: Thu, 28 Apr 2022 11:34:00 +0100 Subject: [PATCH] Add strict mode API See https://github.com/libbpf/libbpf/wiki/Libbpf-1.0-migration-guide for context. Related to https://github.com/aquasecurity/libbpfgo/issues/159 --- libbpfgo.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/libbpfgo.go b/libbpfgo.go index e5117390..bc7a9f35 100644 --- a/libbpfgo.go +++ b/libbpfgo.go @@ -328,6 +328,41 @@ func NewModuleFromFile(bpfObjPath string) (*Module, error) { }) } +// LibbpfStrictMode is an enum as defined in https://elixir.bootlin.com/linux/v5.17.5/source/tools/lib/bpf/libbpf_legacy.h#L23 +type LibbpfStrictMode uint32 + +const ( + LibbpfStrictModeAll LibbpfStrictMode = C.LIBBPF_STRICT_ALL + LibbpfStrictModeNone LibbpfStrictMode = C.LIBBPF_STRICT_NONE + LibbpfStrictModeCleanPtrs LibbpfStrictMode = C.LIBBPF_STRICT_CLEAN_PTRS + LibbpfStrictModeDirectErrs LibbpfStrictMode = C.LIBBPF_STRICT_DIRECT_ERRS + LibbpfStrictModeSecName LibbpfStrictMode = C.LIBBPF_STRICT_SEC_NAME + LibbpfStrictModeNoObjectList LibbpfStrictMode = C.LIBBPF_STRICT_NO_OBJECT_LIST + LibbpfStrictModeAutoRlimitMemlock LibbpfStrictMode = C.LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK +) + +func (b LibbpfStrictMode) String() (str string) { + x := map[LibbpfStrictMode]string{ + LibbpfStrictModeAll: "LIBBPF_STRICT_ALL", + LibbpfStrictModeNone: "LIBBPF_STRICT_NONE", + LibbpfStrictModeCleanPtrs: "LIBBPF_STRICT_CLEAN_PTRS", + LibbpfStrictModeDirectErrs: "LIBBPF_STRICT_DIRECT_ERRS", + LibbpfStrictModeSecName: "LIBBPF_STRICT_SEC_NAME", + LibbpfStrictModeNoObjectList: "LIBBPF_STRICT_NO_OBJECT_LIST", + LibbpfStrictModeAutoRlimitMemlock: "LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK", + } + + str, ok := x[b] + if !ok { + str = LibbpfStrictModeNone.String() + } + return str +} + +func SetStrictMode(mode LibbpfStrictMode) { + C.libbpf_set_strict_mode(uint32(mode)) +} + func NewModuleFromFileArgs(args NewModuleArgs) (*Module, error) { C.set_print_fn() if err := bumpMemlockRlimit(); err != nil { @@ -787,7 +822,7 @@ func (b *BPFMap) DeleteKeyBatch(keys unsafe.Pointer, count uint32) error { ElemFlags: C.BPF_ANY, Flags: C.BPF_ANY, } - + errC := C.bpf_map_delete_batch(b.fd, keys, &countC, bpfMapBatchOptsToC(opts)) if errC != 0 { sc := syscall.Errno(-errC)