iovisor / gobpf

Go bindings for creating BPF programs.
Apache License 2.0
2.14k stars 313 forks source link

BPF map definition is different from kernel #198

Open tahsinrahman opened 5 years ago

tahsinrahman commented 5 years ago

Map definition of the elf package is different from kernel definition of bpf map.

So currently it isn't possible to load elf files that uses kernel definition of maps. It there any specific reason for not using the map structure defined in kernel?

Also, the elf package requires that maps section should start with maps/ prefix, but all the sample codes from samples/bpf directory in the kernel use SEC("maps"). this also requires us to change map section name, recompile and load using this library.

tamalsaha commented 5 years ago

/cc

alban commented 5 years ago

Different projects grow organically with different map definition over time. Even outside of gobpf, there are different definitions.

Now a few projects rely on the convention in gobpf. But maybe gobpf could learn to parse the different ELF conventions from different projects (kernel style, perf, iproute2/tc...)?

[kernel samples](https://github.com/torvalds/linux/blob/master/samples/bpf/bpf_load.h#L10-L18): ``` struct bpf_load_map_def { unsigned int type; unsigned int key_size; unsigned int value_size; unsigned int max_entries; unsigned int map_flags; unsigned int inner_map_idx; unsigned int numa_node; }; ``` [perf](https://github.com/torvalds/linux/blob/master/tools/lib/bpf/libbpf.h#L285-L291): ``` struct bpf_map_def { unsigned int type; unsigned int key_size; unsigned int value_size; unsigned int max_entries; unsigned int map_flags; }; ``` [iproute2 / tc](https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/include/bpf_elf.h#n32): ``` /* ELF map definition */ struct bpf_elf_map { __u32 type; __u32 size_key; __u32 size_value; __u32 max_elem; __u32 flags; __u32 id; __u32 pinning; __u32 inner_id; __u32 inner_idx; }; ```