Problem loading eBPF/XDP program into the kernel

73 Views Asked by At

My goal is to run a eBPF (extended Berkeley Packet Filter) / XDP (eXpress Data Path) program, but I can't load it into the kernel.

I tried to run this eBPF/XDP example (from Learning eBPF book) program:

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

int counter = 0;

SEC("xdp")
int hello(struct xdp_md *ctx) {
    bpf_printk("Hello World %d", counter);
    counter++;
    return XDP_PASS;
}

char LICENSE[] SEC("license") = "Dual BSD/GPL";

The code is in hello.bpf.c file. After compilation, I tried to upload the program into the kernel with sudo bpftool prog load hello.bpf.o /sys/fs/bpf/hello but it failed. The output received was:

libbpf: Error loading BTF: Invalid argument(22)d
libbpf: magic: 0xeb9f
version: 1
flags: 0x0
hdr_len: 24
type_off: 0
type_len: 372
str_off: 372
str_len: 289
btf_total_size: 685
[1] PTR (anon) type_id=2
[2] STRUCT xdp_md size=20 vlen=5
        data type_id=3 bits_offset=0
        data_end type_id=3 bits_offset=32
        data_meta type_id=3 bits_offset=64
        ingress_ifindex type_id=3 bits_offset=96
        rx_queue_index type_id=3 bits_offset=128
[3] TYPEDEF __u32 type_id=4
[4] INT unsigned int size=4 bits_offset=0 nr_bits=32 encoding=(none)
[5] FUNC_PROTO (anon) return=6 args=(1 ctx)
[6] INT int size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[7] FUNC hello type_id=5 vlen != 0

libbpf: Error loading .BTF into kernel: -22.
libbpf: bpf: relocation: not yet supported relo for non-static global 'counter' variable found in insns[0].code 0x18
Error: failed to open object file

Any idea about how to solve this?

1

There are 1 best solutions below

0
Dylan Reimerink On

It seems like you are using a very old version of libbpf. This particular error message has been removed since v0.0.6 of libbpf in 2019 https://github.com/libbpf/libbpf/commit/83535cb2bfa0fabbb31dbcbc6ffe759a03757c0f

I recommend you get the latest libbpf version from https://github.com/libbpf/libbpf instead. It should support initialized global variables.