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

Simplify GetMapsIDsByName #384

Merged
merged 8 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 3 additions & 6 deletions map-low.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ func GetMapByID(id uint32) (*BPFMapLow, error) {
func GetMapsIDsByName(name string) ([]uint32, error) {
bpfMapsIds := []uint32{}

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

for {
retC := C.bpf_map_get_next_id(startId, &nextId)
retC := C.bpf_map_get_next_id(id, &id)
geyslan marked this conversation as resolved.
Show resolved Hide resolved
errno := syscall.Errno(-retC)
if retC < 0 {
if errno == syscall.ENOENT {
Expand All @@ -119,9 +118,7 @@ func GetMapsIDsByName(name string) ([]uint32, error) {
return bpfMapsIds, fmt.Errorf("failed to get next map id: %w", errno)
}

startId = nextId + 1

bpfMapLow, err := GetMapByID(uint32(nextId))
bpfMapLow, err := GetMapByID(uint32(id))
if err != nil {
return bpfMapsIds, err
}
Expand Down
11 changes: 11 additions & 0 deletions selftest/map-getmapsbyname/main.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,15 @@ struct {
__uint(value_size, sizeof(u32));
} test_name SEC(".maps");

struct test_struct {
char value[10];
};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, int);
__type(value, struct test_struct);
} test_hash_name SEC(".maps");

char LICENSE[] SEC("license") = "Dual BSD/GPL";
6 changes: 6 additions & 0 deletions selftest/map-getmapsbyname/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const (
BPFMapNameToNotFind = "not_found"
// The following properties are used to identify the map
BPFHashMapNameToFind = "test_hash_name"
BPFMapNameToFind = "test_name"
BPFMapTypeToFind = bpf.MapTypeArray
BPFMapMaxEntriesToFind = 1
Expand All @@ -34,6 +35,11 @@ func main() {
log.Fatalf("the %s map should not be found, but it was found with ids: %v", BPFMapNameToNotFind, notFoundMapsIDs)
}

bpfHashMapsIDs, err := bpf.GetMapsIDsByName(BPFHashMapNameToFind)
if len(bpfHashMapsIDs) == 0 {
log.Fatalf("the %s map should be found", BPFHashMapNameToFind)
}

mapsIDs, err := bpf.GetMapsIDsByName(BPFMapNameToFind)
if err != nil {
log.Fatal(err)
Expand Down