Search code examples
ebpfbpflibbpf

libbpf failed to open system Kconfig when using 'bpftool struct_ops' command


I am trying to use the bpf_cubic example in the linux kernel as a sample to load onto kernel using bpftool struct_ops command. The code is compiled successfully, but when I run bpftool struct_ops register bpf_cubic.o, it results in the following error:

libbpf: failed to open system Kconfig
libbpf: failed to load object 'bpf_cubic.o'

Any ideas on what the issue might be?

  • Edit:

Following is the output of strace -f -ebpf bpftool struct_ops register bpf_cubic.o:

bpf(BPF_PROG_LOAD, {prog_type=BPF_PROG_TYPE_SOCKET_FILTER, insn_cnt=2, insns=0x7ffd21f7caf0, license="GPL", log_level=0, log_size=0, log_buf=NULL, kern_version=KERNEL_VERSION(0, 0, 0), prog_flags=0, prog_name="", prog_ifindex=0, expected_attach_type=BPF_CGROUP_INET_INGRESS, prog_btf_fd=0, func_info_rec_size=0, func_info=NULL, func_info_cnt=0, line_info_rec_size=0, line_info=NULL, line_info_cnt=0, attach_btf_id=0, attach_prog_fd=0, fd_array=NULL}, 144) = 3
bpf(BPF_PROG_LOAD, {prog_type=BPF_PROG_TYPE_SOCKET_FILTER, insn_cnt=2, insns=0x7ffd21f7c8e0, license="GPL", log_level=0, log_size=0, log_buf=NULL, kern_version=KERNEL_VERSION(0, 0, 0), prog_flags=0, prog_name="", prog_ifindex=0, expected_attach_type=BPF_CGROUP_INET_INGRESS, prog_btf_fd=0, func_info_rec_size=0, func_info=NULL, func_info_cnt=0, line_info_rec_size=0, line_info=NULL, line_info_cnt=0, attach_btf_id=0, attach_prog_fd=0}, 116) = 3
bpf(BPF_PROG_LOAD, {prog_type=BPF_PROG_TYPE_SOCKET_FILTER, insn_cnt=2, insns=0x7ffd21f7caf0, license="GPL", log_level=0, log_size=0, log_buf=NULL, kern_version=KERNEL_VERSION(0, 0, 0), prog_flags=0, prog_name="", prog_ifindex=0, expected_attach_type=BPF_CGROUP_INET_INGRESS, prog_btf_fd=0, func_info_rec_size=0, func_info=NULL, func_info_cnt=0, line_info_rec_size=0, line_info=NULL, line_info_cnt=0, attach_btf_id=0, attach_prog_fd=0, fd_array=NULL}, 128) = 3
libbpf: failed to open system Kconfig
libbpf: failed to load object 'bpf_cubic.o'
+++ exited with 255 +++

Output of uname -r:

6.1.45-8-esx

Output of ls -l /boot/config*:

-rw-r--r-- 1 root root 125476 Sep 20 07:09 /boot/config-6.1.45-8.ph5-esx

However, I should point out that after I used menuconfig to enable BTF support and other related feature to work with bpftool, I changed the image used by grub located at /boot/grub2/grub.cfg to point to the vmlinuz file created after compiling the new kernel. What I want to say is that the config file shown in the output of ls -l /boot/config* is the old config file which does not have the mentioned features enabled.

Below is the output of ls /boot/:

config-6.1.45-8.ph5-esx      linux-6.1.45-8.ph5-esx.cfg   System.old
efi                          photon.cfg                   vmlinuz
grub                         systemd.cfg                  vmlinuz-6.1.45-8.ph5-esx
grub2                        System.map                   vmlinuz.old
initrd.img-6.1.45-8.ph5-esx  System.map-6.1.45-8.ph5-esx

Solution

  • TL;DR. The config file for the running kernel should be in /boot/config-$(uname -r) for bpftool to succeed.


    Explanations

    To load this specific BPF program, bpftool needs access to the kernel config file. That's because the BPF program refers to one kernel config.

    The libbpf library (used by bpftool to load BPF programs) expects the kernel configuration to be in either /boot/config-$(uname -r) or /proc/config.gz.

    An easy way to fix it would be to copy the config file for the running kernel to /boot/config-$(uname -r). Another solution would be to edit bpf_cubic.c to hardcode the value of CONFIG_HZ.