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: GetMapsIdsByName #377

Closed
wants to merge 3 commits into from
Closed
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
33 changes: 33 additions & 0 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,39 @@ func (m *Module) InitGlobalVariable(name string, value interface{}) error {
return err
}

// GetMapsIdsByName searches for maps with a given name.
// It returns a slice of unsigned 32-bit integers representing the IDs of matching maps.
func GetMapsIdsByName(name string) []uint32 {
bpfMapsIds := []uint32{}

startId := C.uint(0)
nextId := C.uint(0)

for {
err := C.bpf_map_get_next_id(startId, &nextId)
if err != 0 {
return bpfMapsIds
}

startId = nextId + 1

bpfMapLow, errMap := GetMapByID(uint32(nextId))
if errMap != nil {
return bpfMapsIds
}

if err := syscall.Close(bpfMapLow.FileDescriptor()); err != nil {
return bpfMapsIds
}

if bpfMapLow.Name() != name {
continue
aymericDD marked this conversation as resolved.
Show resolved Hide resolved
}

bpfMapsIds = append(bpfMapsIds, bpfMapLow.info.ID)
}
}

func (m *Module) GetMap(mapName string) (*BPFMap, error) {
cs := C.CString(mapName)
bpfMapC, errno := C.bpf_object__find_map_by_name(m.obj, cs)
Expand Down
1 change: 1 addition & 0 deletions selftest/map-get-maps-by-name/Makefile
7 changes: 7 additions & 0 deletions selftest/map-get-maps-by-name/go.mod
Original file line number Diff line number Diff line change
@@ -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 => ../../
4 changes: 4 additions & 0 deletions selftest/map-get-maps-by-name/go.sum
Original file line number Diff line number Diff line change
@@ -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=
20 changes: 20 additions & 0 deletions selftest/map-get-maps-by-name/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//+build ignore

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>

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";
45 changes: 45 additions & 0 deletions selftest/map-get-maps-by-name/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import "C"

import (
"fmt"
"os"
"unsafe"

bpf "github.com/aquasecurity/libbpfgo"
)

const BPFMapName = "test"

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()

mapsIdS := bpf.GetMapsIdsByName(BPFMapName)
if len(mapsIdS) == 0 {
fmt.Fprintln(os.Stderr, fmt.Errorf("no maps found for the %s map", BPFMapName))
os.Exit(-1)
}

bpfMapId := mapsIdS[0]

bpfMap, err := bpf.GetMapByID(bpfMapId)
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Errorf("the %s map with %d id not found: %w", BPFMapName, bpfMapId, err))
os.Exit(-1)
}

key1 := uint32(0)
value1 := uint32(55)
if err := bpfMap.Update(unsafe.Pointer(&key1), unsafe.Pointer(&value1)); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}
1 change: 1 addition & 0 deletions selftest/map-get-maps-by-name/run.sh
Loading