Search code examples
cebpflibbpf

Libbpf: Find map pinned at a different location in Kernel Space


I have created a HASH_MAP that has been pinned at /sys/fs/bpf/my_prog/request_map from the userspace.

I have been trying to access this map in the kernel space as follows:

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __type(key, struct key);
    __type(value, struct val);
    __uint(pinning, LIBBPF_PIN_BY_NAME);
} request_map SEC(".maps");

However, LIBBPF_PIN_BY_NAME only checks for the map in /sys/fs/bpf/ and not the modified map location. How would I override this default folder?


Solution

  • LIBBPF_PIN_BY_NAME pins the map at pin_root_path which is provided by the options passed while opening the object file.

    That is, I had to modify my userspace attachment code to open the file as follows:

    struct bpf_object_open_opts opts;
    opts->object_name = my_obj_name;
    opts->pin_root_path = /sys/fs/bpf/my_prog
    obj = bpf_object__open_file(filename, &opts);
    
    ...<attachment logic here>...