I am now trying to insert some bpf code into kernel based on cilium. Through this tutorial I step by step follow the instructions to compile using its provided Makefile and then using bpftool to try to load the object file into kernel, unfortunately it always shows the errors like the picture, I am very sure that I have installed all the related dependencies and also upgrade my kernel to the newest one, the link is tutorial https://github.com/cilium/cilium/tree/main/bpf/custom maybe someone has idea why my load fails
This is the error I get:
$ sudo bpftool prog load bpf/custom/bytecount.o /sys/fs/bpf/tc/globals/bytecounter /sys/fs/bpf/tc/globals/bytecounter_maps
libbpf: Error loading BTF: Invalid argument(22)
libbpf: magic: 0xeb9f
version: 1
flags: 0x0
hdrlen: 24
type_off: 0
type_len: 1164
stroff: 1164
str_len: 1234
btf_total_size: 2422
[1] STRICT (anon) size=32 vlen=4
type typeid=2 bitsoffset=0
key typeid=6 bitsoffset=64
value typeid=9 bitsoffset=126
max_entries type_id=12 bits_offset=192
...
[25] INT unsigned char size=1 bits_offset=0 nr_bits=8 encoding=(none)
[26] FUNC_PROTO (anon) return=3 args=(15 ctx)
[27] FUNC custom_hook type_id=26 vlen != 0
libbpf: Error loading .BTF into kernel: -22. Error: failed to open object file
[27] FUNC custom_hook type_id=26 vlen != 0
You seem to be hitting this error condition: https://github.com/torvalds/linux/blob/85d33df357b634649ddbe0a20fd2d0fc5732c3cb/kernel/bpf/btf.c#L2655
The BTF (type info) standard was updated to start using vlen
to indicate the linkage type of functions. The kernel started supporting this after v5.6.
Clang/LLVM has been updated as well (at some point) to emit BTF this way where static=0, global=1, external=2
The example in cilium/bpf/custom has a global function, so the combination of a new compiler, older kernel and this code is causing the issue. Adding the static
keyword should fix it:
__section(STRINGIFY(BPF_CUSTOM_PROG_NAME))
static int custom_hook(const struct __ctx_buff *ctx)