Home > Blockchain >  how to get map fd and max extries if I have bpf_map object by using bpf_object__find_map_by_name(bpf
how to get map fd and max extries if I have bpf_map object by using bpf_object__find_map_by_name(bpf

Time:08-16

So struct bpf_map member definition of fd and max_entries (which I need) are in source file (libbpf.c) and libbpf.h contains just declaration of ebpf map like struct bpf_map map; So I can't de-reference a pointer(which I have struct bpf_map of) to get fd and max extries which stored in struct within struct.

First So is there any way I can verify what returned from these two function are valids. is including libbpf.h enough for including libbpf_get_error function

 struct bpf_object *obj=bpf_object__open(filename);
 if (libbpf_get_error(obj)) {
    printf("ERROR: opening BPF object file failed\n");
   }

 struct bpf_map *map=bpf_object__find_map_by_name(obj,"hash_map");    
 if (libbpf_get_error(map)) {
    printf("ERROR: finding map\n");
 }

And also moving on from here. How exactly can I get members of struct bpf_map and struct bpf_map_def if I have struct bpf_map object

Can anyone please tell me this?

I am interested in fd and max_entries; If its not supported in ebpf libbpf then is there any value of syscall number in function

   long syscall(long number, ...);

and parameters lists that give me this info.

Thanks

CodePudding user response:

No need to dereference the pointers yourself, the structs are opaque on purpose. Just use the relevant functions from libbpf:

LIBBPF_API int bpf_map__fd(const struct bpf_map *map);
LIBBPF_API __u32 bpf_map__max_entries(const struct bpf_map *map);

If its not supported in ebpf libbpf then is there any value of syscall number in function syscall(...)

There is, but this would only work to retrieve the information from the kernel, after the map has been created (which is admittedly what you want since you're looking for the fd anyway). The BPF_MAP_GET_FD_BY_ID subcommand for the bpf() syscall (syscall(__NR_bpf, cmd, attr, size), see man bpf), would give you the relevant file descriptor, provided you know the map id, and the BPF_OBJ_GET_INFO_BY_FD subcommand, with the relevant arguments would allow you to find the maximum number of entries. But you would pretty much end up reimplementing libbpf.

  • Related