Let's suppose there are two programs (User program and Kernel program).
User program made bpf map by api bpf_create_map_name
() and it returns fd. With this fd, I can access the map by syscalls (e.g., bpf_map_update
(fd, ..)). But I can do this only in the user space programs because the fd is valid only to user program(=user process), then How can I access to this map in the bpf program (located in the kernel space)?
I was heard that I can pin the map in the fs via libbpf's bpf_obj_pin
(fd, file path) and can get this map via libbpf's bpf_obj_get
(file path) but the problem is bpf_obj_get
is only available in the user space because this is system call.
I saw a similar discussion before (Accessing BPF maps from kernel space). but this was not clear to me. To access bpf map via bpf_map_lookup_elem
(fd, ..) in the kernel space, I have to know map's fd in advance. But as I mentioned before, the map's fd is not valid in the kernel.
I am using libbpf not BCC.
You should probably look at libbpf's function
bpf_map__reuse_fd()
, which allows to reuse a file descriptor pointing to an existing map for a BPF program.Here is an example using this function: we first retrieve a pointer to the map to replace in the
struct bpf_object
by callingbpf_object__find_map_by_name()
, and then tell it to reuse the existing fd.