Xdp fails to load?

Today I wanted to compile a small simple xdp programm that is standard tutorial implementation:

#include <linux/bpf.h>
int main() {
	  return XDP_DROP;
}

I used this Makefile

all: dropper.c
		$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o dropper.o $^ $(LDLIBS) -lrt

and this

all: dropper.c
		 clang -O2 -target bpf -c dropper.c -o dropper.o

The second used my host-buildchain.
The first gives me following error:

root@LivingRoom:~# ip link set dev wlan0 xdp obj dropper.o sec .text
ELF format error, ELF file not for eBPF?
Cannot initialize ELF context!

I found the answer! :smiley: I can just cross-compile

clang -O2 -target bpfeb -c dropper.c -o dropper.o

For cross-compilation, the two targets bpfeb and bpfel were introduced, thanks to that BPF programs can be compiled on a node running in one endianness (e.g. little endian on x86) and run on a node in another endianness format (e.g. big endian on arm). Note that the front end (clang) needs to run in the target endianness as well. https://docs.cilium.io/en/v1.9/bpf/

@PolynomialDivision started really early today with xdp foo. while googeling and trying to figure stuff out, I stumbled across this post. What are you working on ?

root@OpenWrt:/# ./ubpf -S -d eth1
[ 8149.659858] *** NOT YET: opcode db ***
Success: Loaded BPF-object(xdp_prog_kern.o) and used section(xdp_stats1)

  • XDP prog attached on device:eth1(ifindex:3)

Collecting stats from BPF map

  • BPF map (bpf_map_type:2) id:62 name:xdp_stats_map key_size:4 value_size:8 max_entries:5

XDP-action
XDP_PASS 4 pkts ( 16 pps) period:0.250405
XDP_PASS 15 pkts ( 5 pps) period:2.001264
XDP_PASS 24 pkts ( 4 pps) period:2.001228
XDP_PASS 40 pkts ( 8 pps) period:2.001225
XDP_PASS 51 pkts ( 5 pps) period:2.001241
XDP_PASS 60 pkts ( 4 pps) period:2.001206
XDP_PASS 70 pkts ( 5 pps) period:2.001212
XDP_PASS 81 pkts ( 5 pps) period:2.001237
XDP_PASS 99 pkts ( 9 pps) period:2.001230
XDP_PASS 112 pkts ( 6 pps) period:2.001234
XDP_PASS 125 pkts ( 6 pps) period:2.000977
XDP_PASS 136 pkts ( 5 pps) period:2.000732

1 Like

Could u give me your Makefile how you compiled that? I wanted to use xdp-tools, but I had some issues with the linker and include statements. I had no time to work on this again.

I have two cases I want to have a look at:

  • I want to manipulate IPv6 Header
  • I want to grep statistics from monitor sockets

With iproute2 I can easily laod xdp-ebpf scripts into the kernel. But actually, I want to use a loader from userspace.

give 1-2 more weeks and I will put ubpf into a git tree. It will be very close to what you want to do. I wrote the central core of the code today and will move onto the application layer next week. I have just been able to drop amd rewrite packets and also send dhcp requests to a userland map. interface redirect is not working yet.
clang -O2 -g -Wall -target bpf -c -o ubf_kern.o ubpf_kern.c -I /usr/include/x86_64-linux-gnu/ is how I compile

1 Like

Nice. :slight_smile: Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.