xdp-project / xdp-tutorial

XDP tutorial
2.33k stars 563 forks source link

Avoid loading and attaching xdp_dispatcher #435

Open pvvm opened 5 days ago

pvvm commented 5 days ago

Hello, Is it possible to avoid loading xdp_dispatcher and, instead, directly load the XDP program I want using load_bpf_and_xdp_attach function from this tutorial or xdp_program__attach? For example, even if my system supports xdp_dispatcher, I want it to load the program directly, without a xdp_dispatcher intermediating the process.

pvvm commented 4 days ago

I found a way to do that using bpf_xdp_attach instead of xdp_program__attach. But before that I had to get the bpf_object, load it, get a bpf_program using bpf_object__next_program, and finally attaching with bpf_xdp_attach.

tohojo commented 4 days ago

Pedro Mizuno @.***> writes:

I found a way to do that using bpf_xdp_attach instead of xdp_program__attach. But before that I had to get the bpf_object, load it, get a bpf_program using bpf_object__next_program, and finally attaching with bpf_xdp_attach.

If you're doing this you might as well skip using libxdp at all, and just use libbpf natively :)

I am curious why you need to skip the dispatcher?

pvvm commented 3 days ago

Yeah, that makes sense.

I wanted to use bpf_prog_test_run_opts to run a XDP program. However, when I tried to call it, it returned errno 524 ENOTSUPP, which means that type of eBPF program wasn't supported by the BPF_PROG_RUN syscall. And, from what I understood, when I had a xdp_dispatcher, my loaded XDP program would be classified as "ext" type, and this type isn't supported by that syscall. Then, my idea was to load and attach the XDP program without the dispatcher, so the type would remain as "xdp". And it worked, I was able to run bpf_prog_test_run_opts successfully.

However, after trying to integrate this code that runs bpf_prog_test_run_opts with a code that utilizes AF_XDP, I noticed that to use AF_XDP it seems kind of mandatory to use an xdp_dispatcher when creating a xsk socket: "libxdp: Existing program is not using a dispatcher, can't replace; unload first"

So, I'm stuck with this other problem. Would you know a way to run bpf_prog_test_run_opts on a program attached to the dispatcher?

tohojo commented 3 days ago

Pedro Mizuno @.***> writes:

So, I'm stuck with this other problem. Would you know a way to run bpf_prog_test_run_opts on a program attached to the dispatcher?

Yes! There's xdp_program__test_run() for exactly this purpose :)

You can use it with a program that has not been loaded at all, or you can use it with one that has already been attached to a dispatcher (as long as your kernel is new enough to support multiple attachments of the same program)

pvvm commented 3 days ago

Oh wow! That's really great, I've just tested it and it's working well! Thank you very much!

And the only thing that keeps bugging me is the following message, that is spammed when I run that function: "libbpf: elf: skipping unrecognized data section(7) xdp_metadata" Would you know what this is about?

Also, is it possible to redirect the frame tested with xdp_programtest_run back with AF_XDP (maybe BPF_F_TEST_XDP_LIVE_FRAMES)? For example, I call xdp_programtest_run and send a frame, do something with it in the XDP program, and use bpf_redirect_map to send it back with AF_XDP. If not, can we "inject" a frame into UMEM and send it using the TX path of AF_XDP?

tohojo commented 3 days ago

Pedro Mizuno @.***> writes:

Oh wow! That's really great, I've just tested it and it's working well! Thank you very much!

And the only thing that keeps bugging me is the following message, that is spammed when I run that function: "libbpf: elf: skipping unrecognized data section(7) xdp_metadata" Would you know what this is about?

Yeah, that particular wart we never did manage to get rid of. Basically, libbpf insists on owning all section names, and emits warnings for anything it doesn't know about. You can silence the warning by changing the libbpf logging function (see silence_libbpf_logging() in the xdp-tools source tree for an example of how to do this).

Also, would you know if there is any documentation listing the functions from libbpf and libxdp, what they do, etc? Because sometimes finding a proper one to use can be a bit challenging

The most comprehensive listing of functions is in the respective header files. We do have a man page for libxdp, but it's not quite comprehensive, I'm afraid. I am not aware of any authoritative documentation for libbpf.

pvvm commented 3 days ago

Oh that's good to know, thanks!

Also, is it possible to redirect the frame tested with xdp_programtest_run back with AF_XDP (maybe BPF_F_TEST_XDP_LIVE_FRAMES)? For example, I call xdp_programtest_run and send a frame, do something with it in the XDP program, and use bpf_redirect_map to send it back with AF_XDP.

If not, can we "inject" a frame into UMEM and send it using the TX path of AF_XDP?