2

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?

pchaigno
  • 11,313
  • 2
  • 29
  • 54
Mark
  • 6,052
  • 8
  • 61
  • 129
  • 1
    As you already guessed, `bpf(BPF_OBJ_GET, …)` (there is probably a wrapper in libbpf indeed) is the answer. You have other possibilities now that IDs exist, such as `bpf(BPF_PROG_GET_FD_BY_ID, …)` that needs the ID of the program (as shown with `bpftool prog show`)—and that's if you have an ID of course. – Qeole Feb 14 '18 at 17:17

1 Answers1

2

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().

Mark
  • 6,052
  • 8
  • 61
  • 129