Home > Back-end >  eBPF - Will bpf_object__pin_maps pin internal maps ( for BPF_MAP_TYPE_HASH_OF_MAPS)?
eBPF - Will bpf_object__pin_maps pin internal maps ( for BPF_MAP_TYPE_HASH_OF_MAPS)?

Time:09-16

I'm confused about whether bpf_object__pin_maps(bpf_obj, pin_dir) will pin the inner maps of map type BPF_MAP_TYPE_HASH_OF_MAPS. Since this function takes bpf_obj as the argument and we would have not created the inner maps till this point. If it does not pin inner maps then how can we do that?

CodePudding user response:

If your inner maps are defined in the same object file as your program and outer map, I would expect libbpf to create and pin them all at the same time.

If, on the contrary, they are not created yet (because you create them later in your workflow, for example with bpftool or from another object file), then you still have the possibility to pin them later.

With libbpf, you could do that by retrieving a handle to the map, for example from a struct bpf_object with bpf_object__find_map_by_name() or a similar function:

LIBBPF_API struct bpf_map *
bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name);

And then you can pin it easily with bpf_map__pin():

LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);

With bpftool, if you have a map loaded but not pinned yet (referenced either by a file descriptor in a user space application or by an eBPF program), you could use the following command to pin it:

# bpftool map pin <map> <pinned path>

Note that creating a map with bpftool automatically pins it, and requires you to provide a path:

# bpftool map create <pinned path> type <type> key <key size> value <value size> \
         entries <max entries> name <name>
  • Related