xdp-project / bpf-examples

Making eBPF programming easier via build env and examples
437 stars 85 forks source link

Help AF_XDP-example run failed ,libbpf: prog 'xdp_pass': BPF program load failed: Invalid argument #88

Closed xjj210130 closed 1 year ago

xjj210130 commented 1 year ago

hi all: My env is centos 7. core is 5.15.46 (build by myself) gcc 11.2.1 clang 14.0.6 llvm 14.0.6 bpftool is v5.15.46

I do as following: 1: git clone https://github.com/xdp-project/bpf-examples.git 2: cd bpf-examples/lib and build libbpf libxdp 3: bpf-examples/AF_XDP-example and modify the code as following: xdpsock.c only do this int main(){ load_xdp_program(); return 0; }

4: make 5: run ./xdpsock -i eno2 -q 5 -r -M The wrong is appear: libbpf: elf: skipping unrecognized data section(19) .eh_frame libbpf: elf: skipping relo section(20) .rel.eh_frame for section(19) .eh_frame libbpf: elf: skipping unrecognized data section(7) xdp_metadata libbpf: prog 'xdp_pass': BPF program load failed: Invalid argument libbpf: prog 'xdp_pass': failed to load: -22 libbpf: failed to load object '/usr/local/lib/bpf/xdp-dispatcher.o' libbpf: elf: skipping unrecognized data section(7) xdp_metadata libbpf: prog 'xdp_dispatcher': BPF program load failed: Invalid argument libbpf: prog 'xdp_dispatcher': -- BEGIN PROG LOAD LOG -- Func#11 is safe for any args that match its prototype btf_vmlinux is malformed R1 type=ctx expected=fp ; int xdp_dispatcher(struct xdp_md ctx) 0: (bf) r6 = r1 1: (b7) r0 = 2 ; __u8 num_progs_enabled = conf.num_progs_enabled; 2: (18) r8 = 0xffff9af086515000 4: (71) r7 = (u8 *)(r8 +2) R0_w=invP2 R1=ctx(id=0,off=0,imm=0) R6_w=ctx(id=0,off=0,imm=0) R8_w=map_value(id=0,off=0,ks=4,vs=124,imm=0) R10=fp0 ; if (num_progs_enabled < 1) 5: (15) if r7 == 0x0 goto pc+141 ; ret = prog0(ctx); 6: (bf) r1 = r6 7: (85) call pc+140 btf_vmlinux is malformed R1 type=ctx expected=fp Caller passes invalid args into func#1 processed 84 insns (limit 100000000) max_states_per_insn 0 total_states 9 peak_states 9 mark_read 1 -- END PROG LOAD LOG -- libbpf: prog 'xdp_dispatcher': failed to load: -22 libbpf: failed to load object '/usr/local/lib/bpf/xdp-dispatcher.o' libxdp: Failed to load dispatcher: Invalid argument libxdp: Falling back to loading single prog without dispatcher libbpf: map 'xsks_map': failed to create: Invalid argument(-22) libbpf: failed to load object 'xdpsock_kern.o' ERROR: attaching program failed: Invalid argument

/usr/local/lib/bpf/xdp-dispatcher.o is exist. i copy from libxdp/src to /usr/local/lib/bpf/lib.(use the origin is also wrong) How can i solve this problem. The function xdp_program__attach return wrong code -22

Meanwhile , if i don't use the function load_xdp_program() , This is other wrong xsk_configure_umem:981: errno: 97/"Address family not supported by protocol"

socket(0x2c / AF_??? /, SOCK_RAW, 0) = -1 EAFNOSUPPORT (协议不支持的地址族) Is it libxdp only specific network card are supported?

Thanks.

tohojo commented 1 year ago

@magnus-karlsson PTAL

magnus-karlsson commented 1 year ago

Does it work without your modifications?

The line "xsk_configure_umem:981: errno: 97/"Address family not supported by protocol"" will happen if you do not have AF_XDP support compiled into your kernel. Do you have that?

/Magnus

xjj210130 commented 1 year ago

@magnus-karlsson Thanks. i rebuild the os kernel . The wrong message is following, you know how to solve it? Thanks again libbpf: elf: skipping unrecognized data section(19) .eh_frame libbpf: elf: skipping relo section(20) .rel.eh_frame for section(19) .eh_frame libbpf: elf: skipping unrecognized data section(7) xdp_metadata libbpf: prog 'xdp_pass': BPF program load failed: Invalid argument libbpf: prog 'xdp_pass': failed to load: -22 libbpf: failed to load object '/usr/local/lib/bpf/xdp-dispatcher.o' libbpf: elf: skipping unrecognized data section(7) xdp_metadata libbpf: prog 'xdp_dispatcher': BPF program load failed: Invalid argument libbpf: prog 'xdp_dispatcher': -- BEGIN PROG LOAD LOG -- Func#11 is safe for any args that match its prototype

magnus-karlsson commented 1 year ago

So one problem solved and one still remaining. The file xdp-dispatcher.o is an XDP program used by libxdp that is the starting point for all other programs. It will always be loaded. Can you please try another example that is only XDP, i.e. something without AF_XDP in its name? Does that program work?

xjj210130 commented 1 year ago

Yes, the problem "Address family not supported by protocol" is solved.
I want to use AF_XDP scoket in my project, because we find use xdp_socket is simple. In fact ,we use xdp in our project, it works fine, but it's too complex。

magnus-karlsson commented 1 year ago

Could you please try one of the XDP examples? The reason for that is that both the AF_XDP examples and the XDP examples uses that program in libxdp. It would give us more information on what is wrong.

xjj210130 commented 1 year ago

@magnus-karlsson I do as following: 1: git clone https://github.com/xdp-project/xdp-tools.git 2: cd xdp-tools 3: ./configure & make 4: cd xdp-dump 5: run cmd: ./xdpdump -i eno3 -x 6: the following message appears

listening on eno3, link-type EN10MB (Ethernet), capture size 262144 bytes 1687862100.736281: packet size 60 bytes, captured 60 bytes on if_name "eno3" 0x0000: ff ff ff ff ff ff 3c ec ef 92 97 c0 08 06 00 01 ......<......... 0x0010: 08 00 06 04 00 01 3c ec ef 92 97 c0 09 a8 01 06 ......<......... 0x0020: 00 00 00 00 00 00 09 a8 01 03 00 00 00 00 00 00 ................ 0x0030: 00 00 00 00 00 00 00 00 00 00 00 00 ............ 1687862100.736300: packet size 42 bytes, captured 42 bytes on if_name "eno3" 0x0000: 3c ec ef 92 97 c0 3c ec ef 90 66 12 08 06 00 01 <.....<...f..... 0x0010: 08 00 06 04 00 02 3c ec ef 90 66 12 09 a8 01 03 ......<...f..... 0x0020: 3c ec ef 92 97 c0 09 a8 01 06 <......... 1687862100.736429: packet size 98 bytes, captured 98 bytes on if_name "eno3" 0x0000: 3c ec ef 90 66 12 3c ec ef 92 97 c0 08 00 45 00 <...f.<.......E. 0x0010: 00 54 6e b4 40 00 40 01 b6 9c 09 a8 01 06 09 a8 .Tn.@.@......... 0x0020: 01 03 08 00 bf d8 a8 f4 00 01 54 bb 9a 64 00 00 ..........T..d.. 0x0030: 00 00 d6 3e 0b 00 00 00 00 00 10 11 12 13 14 15 ...>............ 0x0040: 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 .......... !"#$% 0x0050: 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 &'()*+,-./012345 I think xdp work fine without xdp_socket. Thank you very much.

magnus-karlsson commented 1 year ago

It is the XDP program in libxdp that fails to load in the AF_XDP-example. The same XDP program (xdp-dispatcher.o) is being loaded in the xdp-tools case as it should use libxdp too I believe. Why it fails, I have unfortunately no idea. What mode are you loading the program in: SKB or DRV?

simosund commented 1 year ago

Hmm, this could perhaps be related to #86? I.e. the error message about xdp-dispatcher failing to load might be harmless (caused by libxdp probing for features). If I recall correctly xdp-tools silence these as they're generally just confusing/misleading, which might explain why those messages don't show up when loading xdp-dump, although @tohojo obviously know more about that.

libbpf: prog 'xdp_pass': BPF program load failed: Invalid argument libbpf: prog 'xdp_pass': failed to load: -22 libbpf: failed to load object '/usr/local/lib/bpf/xdp-dispatcher.o' libbpf: elf: skipping unrecognized data section(7) xdp_metadata libbpf: prog 'xdp_dispatcher': BPF program load failed: Invalid argument libbpf: prog 'xdp_dispatcher': -- BEGIN PROG LOAD LOG -- Func#11 is safe for any args that match its prototype

So before starting to chase down the libxdp rabbit hole, @xjj210130, is there any additional output after that (as the final line just looks like the start of a verifier log, and Func#11 apparently passed)?

If it is actually related to libxdp, this repository uses a slightly older version of libxdp (344b241) than the current main at xdp-tools (b8ee97c). So can perhaps be a good idea to either try checking out 344b241 in xdp-tools before trying xdp-dump, or update the submodule here in bpf-examples (cd lib/xdp-tools & git switch master & git pull).

xjj210130 commented 1 year ago

hmm, in fact, i add some code in xdpsock_kern.c lie this:

SEC("xdp_sock") int xdp_sock_prog(struct xdp_md ctx) { rr = (rr + 1) & (MAX_SOCKS - 1); bpf_printk("hell--------------test-----\n"); bpf_trace_printk("hell----------------11-%d--",12); void data = (void )(long)ctx->data; void data_end = (void *)(long)ctx->data_end; if (NULL == data || NULL == data_end) { bpf_printk(" data is null, packet is wrong \n"); return XDP_DROP;

}

struct ethhdr *eth = data;

if (eth + 1 > (struct ethhdr *)data_end)

{ bpf_printk(" packet is wrong \n"); return XDP_DROP; }

struct iphdr *iph = data + sizeof(struct ethhdr);
if (iph + 1 > (struct iphdr *)data_end)

{ bpf_printk(" iphdr is wrong \n"); return XDP_DROP; }

 __u32 sip = iph->saddr;
bpf_printk("rec ip is %ld",sip);

return bpf_redirect_map(&xsks_map, rr, XDP_DROP); }

char __license[] SEC("license") = "GPL";

however, there is nothing in folder /sys/fs/bpf .

There is other wrong message: libbpf: prog 'xdp_dispatcher': failed to load: -22 libbpf: failed to load object '/usr/local/lib/bpf/xdp-dispatcher.o' libxdp: Failed to load dispatcher: Invalid argument libxdp: Falling back to loading single prog without dispatcher -=======load_xdp_program fisnish, 5, 1 ===socket para=====1189335040,16777216 =====opt_num_xsk, 4, rx 1,tx 0 cfg.xdp_flags 2 is 4, 4 cfg.xdp_flags 3 is 4 =====opt_num_xsk, 4, rx 1,tx 0 cfg.xdp_flags 2 is 4, 4 cfg.xdp_flags 3 is 4 =====opt_num_xsk, 4, rx 1,tx 0 cfg.xdp_flags 2 is 4, 4 cfg.xdp_flags 3 is 4 =====opt_num_xsk, 4, rx 1,tx 0 cfg.xdp_flags 2 is 4, 4 cfg.xdp_flags 3 is 4 set socket finish enter into xsks map

sock0@eno3:5 rxdrop xdp-drv pps pkts 1.00 rx 0 0 tx 0 0

sock1@eno3:5 rxdrop xdp-drv

So i think the xdp doesn't work on network.

Meanwhile, i test the project AF_XDP-forwarding, there is the same message:

Buffer pool created successfully. libbpf: elf: skipping unrecognized data section(8) .xdp_run_config libbpf: elf: skipping unrecognized data section(9) xdp_metadata libbpf: elf: skipping unrecognized data section(7) xdp_metadata libbpf: prog 'xdp_pass': BPF program load failed: Invalid argument libbpf: prog 'xdp_pass': failed to load: -22 libbpf: failed to load object 'xdp-dispatcher.o' libbpf: elf: skipping unrecognized data section(7) xdp_metadata libbpf: prog 'xdp_dispatcher': BPF program load failed: Invalid argument libbpf: prog 'xdp_dispatcher': -- BEGIN PROG LOAD LOG -- Func#11 is safe for any args that match its prototype btf_vmlinux is malformed R1 type=ctx expected=fp ; int xdp_dispatcher(struct xdp_md ctx) 0: (bf) r6 = r1 1: (b7) r0 = 2 ; __u8 num_progs_enabled = conf.num_progs_enabled; 2: (18) r8 = 0xffffa44f4671b000 4: (71) r7 = (u8 *)(r8 +2) R0_w=invP2 R1=ctx(id=0,off=0,imm=0) R6_w=ctx(id=0,off=0,imm=0) R8_w=map_value(id=0,off=0,ks=4,vs=124,imm=0) R10=fp0 ; if (num_progs_enabled < 1) 5: (15) if r7 == 0x0 goto pc+141 ; ret = prog0(ctx); 6: (bf) r1 = r6 7: (85) call pc+140 btf_vmlinux is malformed R1 type=ctx expected=fp Caller passes invalid args into func#1 processed 84 insns (limit 100000000) max_states_per_insn 0 total_states 9 peak_states 9 mark_read 1 -- END PROG LOAD LOG -- libbpf: prog 'xdp_dispatcher': failed to load: -22 libbpf: failed to load object 'xdp-dispatcher.o' libxdp: Failed to load dispatcher: Invalid argument libxdp: Falling back to loading single prog without dispatcher Port 0: interface = eno3, queue = 0 All ports created successfully. Thread 0 (CPU core 1): (eno3, 0) -> (eno3, 0),

No XDP program loaded on eno3

so i think the message "prog 'xdp_dispatcher': failed to load: -22" maybe useful.

xjj210130 commented 1 year ago

Thank you very much for your help @tohojo @magnus-karlsson @simosund