Search code examples
bpfebpfbcc-bpf

XDP offloaded mode flags set is not working with bcc


I'm trying to run this tutorial XDP code that is provided in the bcc.

The code I use is this script: bcc/examples/networking/xdp/xdp_drop_count.py.

and to my understanding, XDP flag works as follows (from that question):

#define XDP_FLAGS_SKB_MODE (1U << 1)
#define XDP_FLAGS_DRV_MODE (1U << 2)
#define XDP_FLAGS_HW_MODE (1U << 3)

So, doesn't this mean that if I change the flags bit to

flags |= 1 << 3

I should be able to run this code in hardware accelerated mode (offloaded)?

I have a NIC card that supports XDP HW accelerated mode and it works fine when I just attach a simple program with only one line of code:

return XDP_PASS;

and attach it in offloaded mode by using ip link set dev interface xdpoffload etc.

So I have confirmed my NIC is capable of loading an offloaded XDP program but when I try the above, it gives me an error:

bpf: Attaching prog to enp4s0np1: Invalid argumentTraceback (most recent call last) :
File "xdp_drop_count.py", line 132, in <module>
b. attach_xdp(device, fn, flags)
File "usr/lib/python2.7/dist-packages/bcc/__init__.py", line 723, in attach_xdp % (dev, errstr))
Exception : Failed to attach BPF to device enp4s0np1: No such file or directory

Also, when I set the flags to :

flags |= 1 << 2

I am not sure if this is actually running the XDP program in driver mode.

Am I missing something?

Thank you in advance.


Solution

  • If you build bcc from sources

    Since commit d147588, bcc has hardware offload support. To offload programs using bcc, you will need three things:

    • The XDP_FLAGS_HW_MODE bit (1U << 3) should be set in the flags passed to attach_xdp().
    • The name of the interface to which you want to offload the program should be given to BPF() with the device= parameter. It will allow bcc to offload the maps to the appropriate device. It is unnecessary if you don't have maps.
    • The interface's name should also be given to load_func, again with parameter device=, such that bcc tells the kernel where to offload the program.

    Note that, with the latest bcc sources, the xdp_drop_count.py script has been updated to do all this for you when you pass the -H option:

    sudo ./xdp_drop_count.py -H $ETHNAME
    

    For older versions of bcc

    Older versions of bcc do not support hardware offload. You can use bpftool or ip (>v4.16) instead, e.g.:

    sudo ip link set dev $ETHNAME xdpoffload obj prog.o sec .text
    sudo bpftool prog load prog.o /sys/fs/bpf/prog type xdp dev $ETHNAME