问题
When I use xdp with eBPF, I figured I could use ip link to set mode.
For example,
ip link set dev eno1 xdpoffload obj xdp.o sec .text
I wanted to know how that xdpoffload or generic or native mode is implemented in the code.
So I was looking at other codes and I found something like :
attach_xdp(device, fn, flags)
I assume flags is the place where the flag for setting mode would go in?
It would be great if someone could tell me if it's true and if it is, which numbers I could use to choose the option.
Thank you very much in advance.
回答1:
ip link gets the XDP mode and sets the flags indeed. You can see that in ip/iplink_xdp.c:
if (!force)
xdp.flags |= XDP_FLAGS_UPDATE_IF_NOEXIST;
if (generic)
xdp.flags |= XDP_FLAGS_SKB_MODE;
if (drv)
xdp.flags |= XDP_FLAGS_DRV_MODE;
if (offload)
xdp.flags |= XDP_FLAGS_HW_MODE;
There are not so many values available, and they are in a header that comes from Linux UAPI, if_link.h:
#define XDP_FLAGS_UPDATE_IF_NOEXIST (1U << 0)
#define XDP_FLAGS_SKB_MODE (1U << 1)
#define XDP_FLAGS_DRV_MODE (1U << 2)
#define XDP_FLAGS_HW_MODE (1U << 3)
#define XDP_FLAGS_MODES (XDP_FLAGS_SKB_MODE | \
XDP_FLAGS_DRV_MODE | \
XDP_FLAGS_HW_MODE)
#define XDP_FLAGS_MASK (XDP_FLAGS_UPDATE_IF_NOEXIST | \
XDP_FLAGS_MODES)
So basically, the three modes: generic/SKB (xdpgeneric), native/driver (xdp), and hardware offload (xdpoffload). Which would be confirmed by ip-link(8) manual page:
xdp object | pinned | offset (or unset) a XDP ("eXpress Data Path") BPF program to run on every packet at driver level.
ip linkoutput will indicate axdpflag for the networking device. If the driver does not have native XDP support, the kernel will fall back to a slower, driver-independent "generic" XDP variant. Theip linkoutput will in that case indicatexdpgenericinstead ofxdponly. If the driver does have native XDP support, but the program is loaded underxdpgeneric object | pinnedthen the kernel will use the generic XDP variant instead of the native one.xdpdrvhas the opposite effect of requestsing that the automatic fallback to the generic XDP variant be disabled and in case driver is not XDP-capable error should be returned.xdpdrvalso disables hardware offloads.xdpoffloadin ip link output indicates that the program has been offloaded to hardware and can also be used to request the "offload" mode, much likexdpgenericit forces program to be installed specifically in HW/FW of the apater.
Once the command line arguments have been parsed, the xdp object is sent to the kernel and attach to the selected XDP hook via a netlink message. Then in the kernel, the program is processed according to the flags passed from user space.
(You can use a cross referencer or git grep, git log -S etc. to track the flags, for example, in the source repositories.)
来源:https://stackoverflow.com/questions/57171394/with-attach-xdp-does-flags-control-the-mode