Search code examples
filesystemsfile-descriptorbpfebpf

eBPF: retrieve `fd` of the pinned bpf program


I know that eBPF program can be pinned to /sys/fs/bpf (default location of small bpffs. For example using bpftool :

$ bpftool prog load ./my_bpf.o /sys/fs/bpf/my_bpf

I was expecting that open("/sys/fs/bpf/my_bpf") would return me the value of file descriptor allocated for my_bpf by the kernel. But open() call just fails, I think it returns -EINVAL.

In this case, what is the purpose of pinning? How do I normally obtain fd of a BPF program, given that there might be lots of them currently loaded. I know that libbpf allows to retrieve fd from id, but what if I don't have an id either?


Solution

  • bpf(BPF_OBJ_GET, …) wrapped in bpf_obj_get() in libbpf provided by kernel returns fd of the pinned object. Path to pinned object is an argument to bpf_obj_get().