DPDK AF_XDP
AF_XDP 是 kernel v4.18+ 新加入的一个协议族(如AF_INET), 主要使用 XDP 实现(下图是 XDP 的基本原理图). 核心原理是在 kernel NAPI poll 位置(网卡驱动内部实现, 为内核最早RX数据包位置)运行 BPF 程序, 通过不断调用 poll 方法, 最终将数据包送到正确的XDP程序处理.
XDP 支持 3 种工作模式:
- Native XDP -运行在网卡驱动实现的 poll 函数中, 需网卡驱动支持;
- Offloaded XDP - 可将 XDP 程序 offload 到网卡硬件中, 需要网卡硬件支持(Netronome);
- Generic XDP - 网卡驱动不支持XDP, XDP 作为默认网络路径加载到内核并运行在 receive_skb 函数中, 这种模式达不到Native & Offload XDP 相同性能, 自 kernel 4.12 以后就可以加载运行, 适合用于测试XDP;
注: kernel 版本越高, 支持 XDP 的网卡型号越多, 建议尽量使用新版kernel.
DPDK AF_XDP PMD
DPDK 自 19.05 版本已添加AF_XDP pmd.
DPDK AF_XDP PMD 环境要求:
组件&特性 | 最低要求 |
---|---|
mini kernel version | > v4.18 |
libbpf | kernel > v5.1-rc4 |
need_wakeup | kerner > v5.3-rc1 |
PMD zero copy | kernel > v5.4-rc1 |
shared_umem | kernel > v5.10 & libbpf > 0.2.0 |
32-bit | kernel > 5.4 |
busy polling | kernel > 5.11 |
注: AF_XDP PMD 不支持 RX/TX Offload & Link Status Interrupt.
pmd Options
iface
- 关联网卡名称(必选项);
start_queue
- 关联网卡起始队列id(可选项-默认0);
queue_count
- 关联网卡队列数量(可选项-默认1);
shared_umem
- 是否启用UMEM
(可选项-默认0);
xdp_prog
- 自定义xdp程序(可选项-默认无-none);
busy_budget
- busy polling budget(可选项-默认64);
MTU
Shared UMEM
--vdev net_af_xdp0,iface=enp5s0f0,shared_umem=1
Preferred Busy Polling
SO_PREFER_BUSY_POLL
是 kernel v5.11 的新加入特性. 据说可提升单核高负载流量下的处理性能. 当启用SO_PREFER_BUSY_POLL
后, 调度到软中断 NAPI 上下文执行napi_poll检查到设置此标识, 则立即退出. 避免 busy-polling
进程超出调度时间片后与NAPI调度频繁切换, 与SO_BUSY_POLL
相比减少了 NAPI 上下文调度, 提高了收包性能.
AF_XDP pmd 默认会自动开启此特性, 可通过 busy_budget=0
关闭.
--vdev net_af_xdp0,iface=enp5s0f0,busy_budget=0
busy_budget
为一个kernel 尝试在 netdev NAPI
上下文与busy polling
进程中处理数据包的数量, 默认值为64
, 可尝试增大此值.
AF_XDP pmd 默认会自动开启此特性, 可通过 busy_budget=0
关闭.
--vdev net_af_xdp0,iface=enp5s0f0,busy_budget=0
busy_budget
为一个kernel 尝试在 netdev NAPI
上下文中处理数据包的数量, 默认值为64
, 可尝试增大此值.
强烈推荐设置以下参数获取最佳性能:
echo 2 | sudo tee /sys/class/net/enp5s0f0/napi_defer_hard_irqs
echo 200000 | sudo tee /sys/class/net/enp5s0f0/gro_flush_timeout
libbpf
https://github.com/libbpf/libbpf
git clone https://github.com/libbpf/libbpf
cd libbpf/src
make PREFIX=/usr install
注:
也可直接使用kernel源代码下 <kernel src tree>/tools/lib/bpf
libbpf 版本.
make install_lib
make install_headers
dpdk 20.11
dnf install git rpm-build dnf-utils ninja-build meson bpftool libbpf-devel
$ export PKG_CONFIG_PATH=/mnt/ramfs/extra/rdma-core-devel-50mlnx1-1.50218.x86_64/usr/lib64/pkgconfig
$ CC=clang meson \
-Dmax_lcores=64 \
-Dmachine=sandybridge \
-Ddisable_drivers=vpda/mlx5,net/mlx5,regex/mlx5,common/mlx5,net/dpaa,net/dpaa2,net/bnx2x,net/ark,net/atlantic,net/avp,net/axgbe,net/bnxt,net/cxgbe,net/ena,net/enetc,net/enic,net/hinic,net/hns3,net/igc,net/liquidio,net/memif,net/mlx4,net/netvsc,net/nfp,net/octeontx,net/pfe,net/qede,net/thunderx,net/txgbe \
-Dexamples=l3fwd,l2fwd \
-Dwerror=false \
build
$ cd build
$ ninja
testpmd 示例
NIC 型号: Mellanox CX5/CX6-Dx、INTEL 82599ES
$ lspci |grep Eth
02:00.0 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
02:00.3 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
05:00.0 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
05:00.1 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
06:00.0 Ethernet controller: Mellanox Technologies MT27800 Family [ConnectX-5]
06:00.1 Ethernet controller: Mellanox Technologies MT27800 Family [ConnectX-5]
单队列:
$ ethtool -L enp5s0f0 combined 1
$ dpdk-20.11/build/app/dpdk-testpmd --iova-mode=va --log-level="pmd.net.af_xdp":8 --log-level="lib.eal":8 --log-level=pmd:8 \
-c 0xffff -m 4096 -n 2 -r 2 --no-pci --vdev net_af_xdp0,iface=enp5s0f0 \
-- -i -a --rxq=1 --txq=1 --rxd=2048 --txd=2048 --forward-mode=icmpecho
多队列:
$ ethtool -L enp5s0f0 combined 4
$ dpdk-20.11/build/app/dpdk-testpmd --log-level="pmd.net.af_xdp":8 --log-level="lib.eal":8 --log-level=pmd:8 -c 0xffff -m 4096 -n 2 -r 2 --no-pci \
--vdev net_af_xdp,iface=enp5s0f0,start_queue=0,queue_count=4 \
-- -i -a --rxq=4 --txq=4 --rxd=2048 --txd=2048 --nb-cores=4 \
--forward-mode=icmpecho
# CX5
$ ethtool -L enp5s0f0 combined 4
$ ethtool -X enp5s0f0 weight 0 1 1 1
$ ethtool -N enp5s0f0 flow-type ip4 l4proto 1 action 0
# IXGBE 只支持部分 flow-type
$ ethtool -L enp5s0f0 combined 4
$ ethtool -K enp5s0f0 ntuple on
$ ethtool -N enp5s0f0 flow-type tcp4 src-ip 10.23.4.6 dst-ip 10.23.4.18 action 0
$ ethtool -N enp5s0f0 flow-type tcp4 src-ip 192.168.10.1 dst-ip 192.168.10.2 src-port 2000 dst-port 2001 action 2 loc 33
# DROP udp
$ ethtool -N enp5s0f0 flow-type udp4 src-ip 10.4.82.2 action -1
$ dpdk-20.11/build/app/dpdk-testpmd --iova-mode=va --log-level="pmd.net.af_xdp":8 --log-level="lib.eal":8 --log-level=pmd:8 \
-c 0xffff -m 4096 -n 2 -r 2 --no-pci --vdev net_af_xdp0,iface=enp5s0f0 \
-- -i -a --rxq=1 --txq=1 --rxd=2048 --txd=2048 --forward-mode=icmpecho
xdpsock
$ NETDEV=enp5s0f0
$ ethtool -N $NETDEV rx-flow-hash udp4 fn
$ ethtool -N $NETDEV flow-type udp4 src-port 4242 dst-port 4242 \
action 4
bpftool map -p
samples/bpf/xdpsock -i $NETDEV -q 4 -r -N
Other
kernel AF_XDP overview
IOVisor’s XDP page
“[What Is] XDP/AF_XDP and its potential”
Cilium’s BPF and XDP reference guide - stable
Cilium’s BPF and XDP reference guide - latest
lwn.net - Introducing AF_XDP support
lwn.net - Accelerating networking with AF_XDP
Dive into BPF: a list of reading material
DPDK PMD for AF_XDP Tests
Design: Having XDP programs per RX-queue
The Cloudflare Blog - Kernel bypass
The Cloudflare Blog - Single RX queue kernel bypass in Netmap for high packet rate networking
Suricata eBPF and XDP
Scalable TCP Session Monitoring with Symmetric Receive-side Scaling
libbpf
xdp-project
xdp-tutorial
bpf-examples