Skip to contentSkip to conversation

vmxnet3: Add XDP support.

RFC PATCH Superseded William Tu 1 participant 1 message May 7, 2023

Subsystems (3)

v21 of 22 Superseded Latest is v22 →

lore
Share & tools
lore

William Tu

author
#0 lore raw

RFC PATCH v21 1/1 vmxnet3: Add XDP support.

The patch adds native-mode XDP support: XDP DROP, PASS, TX, and REDIRECT.

Background:
The vmxnet3 rx consists of three rings: ring0, ring1, and dataring. For r0 and r1, buffers at r0 are allocated using alloc_skb APIs and dma mapped to the ring's descriptor. If LRO is enabled and packet size larger than 3K, VMXNET3_MAX_SKB_BUF_SIZE, then r1 is used to mapped the rest of the buffer larger than VMXNET3_MAX_SKB_BUF_SIZE. Each buffer in r1 is allocated using alloc_page. So for LRO packets, the payload will be in one buffer from r0 and multiple from r1, for non-LRO packets, only one descriptor in r0 is used for packet size less than 3k.

When receiving a packet, the first descriptor will have the sop (start of packet) bit set, and the last descriptor will have the eop (end of packet) bit set. Non-LRO packets will have only one descriptor with both sop and eop set.

Other than r0 and r1, vmxnet3 dataring is specifically designed for handling packets with small size, usually 128 bytes, defined in VMXNET3_DEF_RXDATA_DESC_SIZE, by simply copying the packet from the backend driver in ESXi to the ring's memory region at front-end vmxnet3 driver, in order to avoid memory mapping/unmapping overhead. In summary, packet size:

    A. < 128B: use dataring
    B. 128B - 3K: use ring0 (VMXNET3_RX_BUF_SKB)
    C. > 3K: use ring0 and ring1 (VMXNET3_RX_BUF_SKB + VMXNET3_RX_BUF_PAGE)

As a result, the patch adds XDP support for packets using dataring and r0 (case A and B), not the large packet size when LRO is enabled.

XDP Implementation:
When user loads and XDP prog, vmxnet3 driver checks configurations, such as mtu, lro, and re-allocate the rx buffer size for reserving the extra headroom, XDP_PACKET_HEADROOM, for XDP frame. The XDP prog will then be associated with every rx queue of the device. Note that when using dataring for small packet size, vmxnet3 (front-end driver) doesn't control the buffer allocation, as a result we allocate a new page and copy packet from the dataring to XDP frame.

The receive side of XDP is implemented for case A and B, by invoking the bpf program at vmxnet3_rq_rx_complete and handle its returned action. The vmxnet3_process_xdp(), vmxnet3_process_xdp_small() function handles the ring0 and dataring case separately, and decides the next journey of the packet afterward.

For TX, vmxnet3 has split header design. Outgoing packets are parsed first and protocol headers (L2/L3/L4) are copied to the backend. The rest of the payload are dma mapped. Since XDP_TX does not parse the packet protocol, the entire XDP frame is dma mapped for transmission and transmitted in a batch. Later on, the frame is freed and recycled back to the memory pool.

Performance:
Tested using two VMs inside one ESXi vSphere 7.0 machine, using single core on each vmxnet3 device, sender using DPDK testpmd tx-mode attached to vmxnet3 device, sending 64B or 512B UDP packet.

VM1 txgen:

$ dpdk-testpmd -l 0-3 -n 1 -- -i --nb-cores=3 \
--forward-mode=txonly --eth-peer=0,<mac addr of vm2>
option: add "--txonly-multi-flow"
option: use --txpkts=512 or 64 byte

VM2 running XDP:

$ ./samples/bpf/xdp_rxq_info -d ens160 -a <options> --skb-mode
$ ./samples/bpf/xdp_rxq_info -d ens160 -a <options>
options: XDP_DROP, XDP_PASS, XDP_TX

To test REDIRECT to cpu 0, use

$ ./samples/bpf/xdp_redirect_cpu -d ens160 -c 0 -e drop

Single core performance comparison with skb-mode.
64B: skb-mode -> native-mode
XDP_DROP: 1.6Mpps -> 2.4Mpps
XDP_PASS: 338Kpps -> 367Kpps
XDP_TX: 1.1Mpps -> 2.3Mpps
REDIRECT-drop: 1.3Mpps -> 2.3Mpps

512B: skb-mode -> native-mode
XDP_DROP: 863Kpps -> 1.3Mpps
XDP_PASS: 275Kpps -> 376Kpps
XDP_TX: 554Kpps -> 1.2Mpps
REDIRECT-drop: 659Kpps -> 1.2Mpps

Demo: https://youtu.be/4lm1CSCi78Q

Future work:
- XDP frag support
- use napi_consume_skb() instead of dev_kfree_skb_any at unmap
- stats using u64_stats_t
- using bitfield macro BIT()
- optimization for DMA synchronization using actual frame length, instead of always max_len

---
v20 -> v21:
- remove set but not used 'nq', found by Simon
- fix duplicate include <linux/skbuff.h> found by kernel test robot
- run through some vmware internal testing done by Sankar
https://github.com/williamtu/net-next/compare/v20..v21

v19 -> v20:
feedbacks from Alexander Lobakin
- remove rcu_read_lock, fix error path in XDP_REDIRECT
- code refactoring
https://github.com/williamtu/net-next/compare/v19..v20

v18 -> v19:
feedbacks from
Horatiu Vultur <[email protected]> and
Dan Carpenter <[email protected]>
- use DMA_TO_DEVICE in vmxnet3_xdp_xmit_frame
- remove one-time used local variable
- fix smatch warnings:
vmxnet3_process_xdp_small() warn: variable dereferenced before check 'skb_xdp_pass' (see line 361)
https://github.com/williamtu/net-next/compare/v18..v19

v17 -> v18:
- rebase df28e8690e71 ("Merge branch 'net-better-const'") feedbacks from Alexander Lobakin
- use NL_SET_ERR_MSG_FMT_MOD for printing MTU in error msg
- I decided to skip the optimization for DMA synchronization, to keep this version simpler, leave it for future work.
https://github.com/williamtu/net-next/compare/v17..v18

v16 -> v17:
feedbacks from Alexander Lobakin
- code refectoring and cleanup and remove excessive braces around condition
- page pool creation: create 1-dir instead of bi-direction. Bidir sync is slower than 1-dir on certain architectures without DMA coherence engines, as you need to not only drop the page from the cache, but also do a writeback.
- add XDP feature advertisement
- rebase to net-next 01bb11ad828b32074976

v15 -> v16:
feedbacks from Maciej Fijalkowski
- fix unneeded code churn
- destory pp resource at vmxnet3_rq_init error path
- handle when LRO is set again when XDP is enabled in vmxnet3_fix_features
- add a helper, vmxnet3_xdp_get_tq
- remove using rcu_read_lock,unlock around XDP invocation
https://lore.kernel.org/bpf/[email protected]/
- fix a mistake in recycle page when !xdp_prog
- pass the xdp_prog to vmxnet3_run_xdp to avoid another deref

feedbacks from Alexander Lobakin
- use virt_to_page instead of virt_to_head_page (due to always 0-order)
- shrink indent level (two places)
ps. on the second thought I decide not to do it as it is easier for vmware internal review of xdp specific code all inside the 'if' statement block.
https://github.com/williamtu/net-next/compare/v15..v16

v14 -> v15:
feedbacks from Alexander Lobakin
- add rcu_read_lock unlock around xdp_frame_bulk_init
- define correct VMXNET3_XDP_MAX_MTU
- add comment explaining tx deferred and threshold in backend driver
- minor code refactoring and style fixes
- fix one bug in process_xdp when !xdp_prog

v13 -> v14:
feedbacks from Alexander Lobakin
- fix several new lines, unrelated changes, unlikely, RCT style, coding style, etc.
- add NET_IP_ALIGN and create VMXNET3_XDP_HEADROOM, instead of using XDP_PACKET_HEADROOM
- remove %pp_page and use %page in rx_buf_int
- fix the %VMXNET3_XDP_MAX_MTU, mtu doesn't include eth, vlan, and fcs
- use NL_SET instead of netdev_err when detecting LRP
- remove two global function, vmxnet3{_xdp_headroom, xdp_enabled} make the vmxnet3_xdp_enabled static inline function, and remove vmxnet3_xdp_headroom.
- rename the VMXNET3_XDP_MAX_MTU to VMXNET3_XDP_MAX_FRSIZE

- add Reviewed-by: Alexander Duyck <[email protected]>
- add yifeng sun for testing on ESXi and XDP compare v13..v14
https://github.com/williamtu/net-next/compare/v13..v14

v12 -> v13:
- feedbacks from Guolin:
Instead of return -ENOTSUPP, disable the LRO in
netdev->features, and print err msg

v11 -> v12:
work on feedbacks from Alexander Duyck
- fix issues and refactor the vmxnet3_unmap_tx_buf and unmap_pkt

v10 -> v11:
work on feedbacks from Alexander Duyck
internal feedback from Guolin and Ronak
- fix the issue of xdp_return_frame_bulk, move to up level of vmxnet3_unmap_tx_buf and some refactoring
- refactor and simplify vmxnet3_tq_cleanup
- disable XDP when LRO is enabled, suggested by Ronak

v9 -> v10:
- Mark as RFC as we're waiting for internal review Feedback from Alexander Duyck
- fix dma mapping leak of ndo_xdp_xmit case
- remove unused MAP_INVALID and adjist bitfield

v8 -> v9:
new
- add rxdataring support (need extra copy but still fast)
- update performance number (much better than v8!)
https://youtu.be/4lm1CSCi78Q

- work on feedbacks from Alexander Duyck and Alexander Lobakin Alexander Lobakin
- use xdp_return_frame_bulk and see some performance improvement
- use xdp_do_flush not xdp_do_flush_map
- fix several alignment issues, formatting issues, minor code restructure, remove 2 dead functions, unrelated add/del of new lines, add braces when logical ops nearby, endianness conversion
- remove several oneliner goto label
- anonymous union of skb and xdpf
- remove xdp_enabled and use xdp prog directly to check
- use bitsfields macro --> I decide to do it later as there are many unrelated places needed to change.

Alexander Duyck
- use bitfield for tbi map type
- anonymous union of skb and xdpf
- remove use of modulus ops, cpu % tq_number

others
- fix issue reported from kernel test robot using sparse

v7 -> v8:
- work on feedbacks from Gerhard Engleder and Alexander
- change memory model to use page pool API, rewrite some of the XDP processing code
- attach bpf xdp prog to adapter, not per rx queue
- I reference some of the XDP implementation from drivers/net/ethernet/mediatek and drivers/net/ethernet/stmicro/stmmac/
- drop support for rxdataring for this version
- redo performance evaluation and demo here
https://youtu.be/T7_0yrCXCe0
- check using /sys/kernel/debug/kmemleak

v6 -> v7:
work on feedbacks from Alexander Duyck on XDP_TX and XDP_REDIRECT
- fix memleak of xdp frame when doing ndo_xdp_xmit (vmxnet3_xdp_xmit)
- at vmxnet3_xdp_xmit_frame, fix return value, -NOSPC and ENOMEM, and free skb when dma map fails
- add xdp_buff_clean_frags_flag since we don't support frag
- update experiements of XDP_TX and XDP_REDIRECT
- for XDP_REDIRECT, I assume calling xdp_do_redirect will take the buffer and free it, so I need to allocate a new buffer to refill the rx ring. However, I hit some OOM when testing using ./samples/bpf/xdp_redirect_cpu -d ens160 -c 0 -e <drop or pass>
- I couldn't find the reason so mark this patch as RFC

v5 -> v6:
work on feedbacks from Alexander Duyck
- remove unused skb parameter at vmxnet3_xdp_xmit
- add trace point for XDP_ABORTED
- avoid TX packet buffer being overwritten by allocatin new skb and memcpy (TX performance drop from 2.3 to 1.8Mpps)
- update some of the performance number and a demo video
https://youtu.be/I3nx5wQDTXw
- pass VMware internal regression test using non-ENS: vmxnet3v2, vmxnet3v3, vmxnet3v4, so remove RFC tag.

v4 -> v5:
- move XDP code to separate file: vmxnet3_xdp.{c, h}, suggested by Guolin
- expose vmxnet3_rq_create_all and vmxnet3_adjust_rx_ring_size
- more test using samples/bpf/{xdp1, xdp2, xdp_adjust_tail}
- add debug print
- rebase on commit 65e6af6cebe

v3 -> v4:
- code refactoring and improved commit message
- make dataring and non-dataring case clear
- in XDP_PASS, handle xdp.data and xdp.data_end change after bpf program executed
- now still working on internal testing
- v4 applied on net-next commit 65e6af6cebef

v2 -> v3:
- code refactoring: move the XDP processing to the front of the vmxnet3_rq_rx_complete, and minimize the places of changes to existing code.
- Performance improvement over BUF_SKB (512B) due to skipping skb allocation when DROP and TX.

v1 -> v2:
- Avoid skb allocation for small packet size (when dataring is used)
- Use rcu_read_lock unlock instead of READ_ONCE
- Peroformance improvement over v1
- Merge xdp drop, tx, pass, and redirect into 1 patch

---
 drivers/net/Kconfig                   |   1 +
 drivers/net/vmxnet3/Makefile          |   2 +-
 drivers/net/vmxnet3/vmxnet3_drv.c     | 236 +++++++++++++--
 drivers/net/vmxnet3/vmxnet3_ethtool.c |  23 ++
 drivers/net/vmxnet3/vmxnet3_int.h     |  42 ++-
 drivers/net/vmxnet3/vmxnet3_xdp.c     | 419 ++++++++++++++++++++++++++
 drivers/net/vmxnet3/vmxnet3_xdp.h     |  49 +++
 7 files changed, 730 insertions(+), 42 deletions(-)
 create mode 100644 drivers/net/vmxnet3/vmxnet3_xdp.c
 create mode 100644 drivers/net/vmxnet3/vmxnet3_xdp.h
drivers/net/Kconfig +1 -0
index c34bd432da27..2deba5f8d497 100644
@@ -570,6 +570,7 @@ config VMXNET3
 	tristate "VMware VMXNET3 ethernet driver"
 	depends on PCI && INET
 	depends on PAGE_SIZE_LESS_THAN_64KB
+	select PAGE_POOL
 	help
 	  This driver supports VMware's vmxnet3 virtual ethernet NIC.
 	  To compile this driver as a module, choose M here: the
drivers/net/vmxnet3/Makefile +1 -1
index a666a88ac1ff..f82870c10205 100644
@@ -32,4 +32,4 @@
 
 obj-$(CONFIG_VMXNET3) += vmxnet3.o
 
-vmxnet3-objs := vmxnet3_drv.o vmxnet3_ethtool.o
+vmxnet3-objs := vmxnet3_drv.o vmxnet3_ethtool.o vmxnet3_xdp.o
drivers/net/vmxnet3/vmxnet3_drv.c +205 -31

430 lines not on this page · raw message

drivers/net/vmxnet3/vmxnet3_ethtool.c +23 -0
index 18cf7c723201..98c22d7d87a2 100644
@@ -28,6 +28,7 @@
 #include "vmxnet3_int.h"
 #include <net/vxlan.h>
 #include <net/geneve.h>
+#include "vmxnet3_xdp.h"
 
 #define VXLAN_UDP_PORT 8472
 
@@ -76,6 +77,10 @@ vmxnet3_tq_driver_stats[] = {
 					 copy_skb_header) },
 	{ "  giant hdr",	offsetof(struct vmxnet3_tq_driver_stats,
 					 oversized_hdr) },
+	{ "  xdp xmit",		offsetof(struct vmxnet3_tq_driver_stats,
+					 xdp_xmit) },
+	{ "  xdp xmit err",	offsetof(struct vmxnet3_tq_driver_stats,
+					 xdp_xmit_err) },
 };
 
 /* per rq stats maintained by the device */
@@ -106,6 +111,16 @@ vmxnet3_rq_driver_stats[] = {
 					 drop_fcs) },
 	{ "  rx buf alloc fail", offsetof(struct vmxnet3_rq_driver_stats,
 					  rx_buf_alloc_failure) },
+	{ "     xdp packets", offsetof(struct vmxnet3_rq_driver_stats,
+				       xdp_packets) },
+	{ "     xdp tx", offsetof(struct vmxnet3_rq_driver_stats,
+				  xdp_tx) },
+	{ "     xdp redirects", offsetof(struct vmxnet3_rq_driver_stats,
+					 xdp_redirects) },
+	{ "     xdp drops", offsetof(struct vmxnet3_rq_driver_stats,
+				     xdp_drops) },
+	{ "     xdp aborted", offsetof(struct vmxnet3_rq_driver_stats,
+				       xdp_aborted) },
 };
 
 /* global stats maintained by the driver */
@@ -249,10 +264,18 @@ vmxnet3_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
 netdev_features_t vmxnet3_fix_features(struct net_device *netdev,
 				       netdev_features_t features)
 {
+	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
+
 	/* If Rx checksum is disabled, then LRO should also be disabled */
 	if (!(features & NETIF_F_RXCSUM))
 		features &= ~NETIF_F_LRO;
 
+	/* If XDP is enabled, then LRO should not be enabled */
+	if (vmxnet3_xdp_enabled(adapter) && (features & NETIF_F_LRO)) {
+		netdev_err(netdev, "LRO is not supported with XDP");
+		features &= ~NETIF_F_LRO;
+	}
+
 	return features;
 }
 
drivers/net/vmxnet3/vmxnet3_int.h +32 -10

100 lines not on this page · raw message

drivers/net/vmxnet3/vmxnet3_xdp.c new file +419 -0

419 lines not on this page · raw message

drivers/net/vmxnet3/vmxnet3_xdp.h new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
2.37.1 (Apple Git-137.1)