diff --git a/libbpfgo.go b/libbpfgo.go index 01cdfb96..37402b06 100644 --- a/libbpfgo.go +++ b/libbpfgo.go @@ -393,6 +393,33 @@ func (m *Module) InitGlobalVariable(name string, value interface{}) error { return err } +func GetMapsByName(name string) []*BPFMapLow { + bpfMaps := []*BPFMapLow{} + + startId := C.uint(0) + nextId := C.uint(0) + + for { + err := C.bpf_map_get_next_id(startId, &nextId) + if err != 0 { + return bpfMaps + } + + startId = nextId + 1 + + bpfMapLow, errMap := GetMapByID(uint32(nextId)) + if errMap != nil { + return bpfMaps + } + + if bpfMapLow.Name() != name { + continue + } + + bpfMaps = append(bpfMaps, bpfMapLow) + } +} + func (m *Module) GetMap(mapName string) (*BPFMap, error) { cs := C.CString(mapName) bpfMapC, errno := C.bpf_object__find_map_by_name(m.obj, cs) diff --git a/selftest/map-get-maps-by-name/Makefile b/selftest/map-get-maps-by-name/Makefile new file mode 120000 index 00000000..d981720c --- /dev/null +++ b/selftest/map-get-maps-by-name/Makefile @@ -0,0 +1 @@ +../common/Makefile \ No newline at end of file diff --git a/selftest/map-get-maps-by-name/go.mod b/selftest/map-get-maps-by-name/go.mod new file mode 100644 index 00000000..1fd0933d --- /dev/null +++ b/selftest/map-get-maps-by-name/go.mod @@ -0,0 +1,7 @@ +module github.com/aquasecurity/libbpfgo/selftest/map-update + +go 1.18 + +require github.com/aquasecurity/libbpfgo v0.4.7-libbpf-1.2.0-b2e29a1 + +replace github.com/aquasecurity/libbpfgo => ../../ diff --git a/selftest/map-get-maps-by-name/go.sum b/selftest/map-get-maps-by-name/go.sum new file mode 100644 index 00000000..c60af667 --- /dev/null +++ b/selftest/map-get-maps-by-name/go.sum @@ -0,0 +1,4 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/selftest/map-get-maps-by-name/main.bpf.c b/selftest/map-get-maps-by-name/main.bpf.c new file mode 100644 index 00000000..b095e26a --- /dev/null +++ b/selftest/map-get-maps-by-name/main.bpf.c @@ -0,0 +1,20 @@ +//+build ignore + +#include + +#include + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u32)); +} test SEC(".maps"); + +SEC("kprobe/sys_execve") +int kprobe__sys_execve(struct pt_regs *ctx) +{ + return 0; +} + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; \ No newline at end of file diff --git a/selftest/map-get-maps-by-name/main.go b/selftest/map-get-maps-by-name/main.go new file mode 100644 index 00000000..afd90bae --- /dev/null +++ b/selftest/map-get-maps-by-name/main.go @@ -0,0 +1,36 @@ +package main + +import "C" + +import ( + "fmt" + "os" + "unsafe" + + bpf "github.com/aquasecurity/libbpfgo" +) + +func main() { + bpfModule, err := bpf.NewModuleFromFile("main.bpf.o") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + defer bpfModule.Close() + + bpfModule.BPFLoadObject() + + testMaps := bpf.GetMapsByName("test") + if len(testMaps) == 0 { + fmt.Fprintln(os.Stderr, fmt.Errorf("no maps found")) + os.Exit(-1) + } + + testMap := testMaps[0] + key1 := uint32(0) + value1 := uint32(55) + if err := testMap.Update(unsafe.Pointer(&key1), unsafe.Pointer(&value1)); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } +} diff --git a/selftest/map-get-maps-by-name/run.sh b/selftest/map-get-maps-by-name/run.sh new file mode 120000 index 00000000..aee911b2 --- /dev/null +++ b/selftest/map-get-maps-by-name/run.sh @@ -0,0 +1 @@ +../common/run.sh \ No newline at end of file