Skip to contentSkip to conversation

vmxnet3: Add XDP support.

RFC PATCH Superseded William Tu 3 participants 5 messages Jan 27, 2023 – Feb 4, 2023

Subsystems (3)

v15 of 22 Superseded Latest is v22 →

lore
Share & tools
lore

William Tu

author
#0 lore raw

RFC PATCH v15 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

---
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
https://github.com/williamtu/net-next/compare/v14..v15

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

---
 drivers/net/Kconfig                   |   1 +
 drivers/net/vmxnet3/Makefile          |   2 +-
 drivers/net/vmxnet3/vmxnet3_drv.c     | 224 ++++++++++++--
 drivers/net/vmxnet3/vmxnet3_ethtool.c |  14 +
 drivers/net/vmxnet3/vmxnet3_int.h     |  43 ++-
 drivers/net/vmxnet3/vmxnet3_xdp.c     | 419 ++++++++++++++++++++++++++
 drivers/net/vmxnet3/vmxnet3_xdp.h     |  49 +++
 7 files changed, 712 insertions(+), 40 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 9e63b8c43f3e..a4419d661bdd 100644
@@ -571,6 +571,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 +195 -29

404 lines not on this page · raw message

drivers/net/vmxnet3/vmxnet3_ethtool.c +14 -0
index 18cf7c723201..6f542236b26e 100644
@@ -76,6 +76,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 +110,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 */
drivers/net/vmxnet3/vmxnet3_int.h +33 -10

101 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)
Reviewed-by on v15 1/1
#1 lore raw

From: William Tu <[email protected]>
Date: Fri, 27 Jan 2023 08:30:27 -0800

Quoted 11 lines

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.

[...]

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +2 -0
@@ -1404,6 +1496,8 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
 	struct Vmxnet3_RxDesc rxCmdDesc;
 	struct Vmxnet3_RxCompDesc rxComp;
 #endif
+	bool need_flush = false;
+
 	vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
 			  &rxComp);
 	while (rcd->gen == rq->comp_ring.gen) {

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1541

Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_drv.c +1 -0
@@ -1444,6 +1538,31 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
 			goto rcd_done;
 		}
 
+		if (rcd->sop && rcd->eop && vmxnet3_xdp_enabled(adapter)) {

Hmm, it's a relatively big block of code for one if. I mean, could we do it like that?

		if (!rcd->sop || !vmxnet3_xdp_enabled(adapter))
			goto skip_xdp;

		if (VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
			...

This way you would save 1 indent level and make code a little bit more readable.
But it might be a matter of personal tastes :) Just noticed you have this skip_xdp label anyway, why not reuse it here.

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +10 -0
+			struct sk_buff *skb_xdp_pass;
+			int act;
+
+			if (VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
+				ctx->skb = NULL;
+				goto skip_xdp; /* Handle it later. */
+			}
+
+			if (rbi->buf_type != VMXNET3_RX_BUF_XDP)
+				goto rcd_done;

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1561

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +10 -0
+
+			act = vmxnet3_process_xdp(adapter, rq, rcd, rbi, rxd,
+						  &skb_xdp_pass);
+			if (act == XDP_PASS) {
+				ctx->skb = skb_xdp_pass;
+				goto sop_done;
+			}
+			ctx->skb = NULL;
+			if (act == XDP_REDIRECT)
+				need_flush = true;
			need_flush |= act == XDP_REDIRECT;

But this looks a big ugly to be honest, I just wrote it to show off a bit :D Your if is perfectly fine, not even sure the line I wrote above produces more optimized object code.

Quoted 18 lines
drivers/net/vmxnet3/vmxnet3_drv.c +6 -1
+			goto rcd_done;
+		}
+skip_xdp:
+
 		if (rcd->sop) { /* first buf of the pkt */
 			bool rxDataRingUsed;
 			u16 len;
@@ -1452,7 +1571,8 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
 			       (rcd->rqID != rq->qid &&
 				rcd->rqID != rq->dataRingQid));
 
-			BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
+			BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB &&
+			       rbi->buf_type != VMXNET3_RX_BUF_XDP);
 			BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
 
 			if (unlikely(rcd->len == 0)) {

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1594

Quoted 5 lines
drivers/net/vmxnet3/vmxnet3_drv.c +2 -0
@@ -1470,6 +1590,26 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
 			rxDataRingUsed =
 				VMXNET3_RX_DATA_RING(adapter, rcd->rqID);
 			len = rxDataRingUsed ? rcd->len : rbi->len;
+
+			if (rxDataRingUsed && vmxnet3_xdp_enabled(adapter)) {

Maybe save 1 level here as well:

			if (!rxDataRingUsed || !vmxnet3_xdp_enabled(ad))
				goto alloc_skb;

			sz = rcd->rxdIdx * ...
Quoted 8 lines
drivers/net/vmxnet3/vmxnet3_drv.c +8 -0
+				struct sk_buff *skb_xdp_pass;
+				size_t sz;
+				int act;
+
+				sz = rcd->rxdIdx * rq->data_ring.desc_size;
+				act = vmxnet3_process_xdp_small(adapter, rq,
+								&rq->data_ring.base[sz],
+								rcd->len,

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1612

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +10 -0
+								&skb_xdp_pass);
+				if (act == XDP_PASS) {
+					ctx->skb = skb_xdp_pass;
+					goto sop_done;
+				}
+				if (act == XDP_REDIRECT)
+					need_flush = true;
+
+				goto rcd_done;
+			}

alloc_skb:

Quoted 3 lines
drivers/net/vmxnet3/vmxnet3_drv.c +0 -0
 			new_skb = netdev_alloc_skb_ip_align(adapter->netdev,
 							    len);
 			if (new_skb == NULL) {

[...]

drivers/net/vmxnet3/vmxnet3_int.h struct vmxnet3_tq_driver_stats { · line 226

Quoted 7 lines
drivers/net/vmxnet3/vmxnet3_int.h +3 -0
@@ -217,6 +221,9 @@ struct vmxnet3_tq_driver_stats {
 	u64 linearized;         /* # of pkts linearized */
 	u64 copy_skb_header;    /* # of times we have to copy skb header */
 	u64 oversized_hdr;
+
+	u64 xdp_xmit;
+	u64 xdp_xmit_err;

Sorry for missing this earlier... You use u64 here for stats and previously we were using it for the stats, but then u64_stat_t was introduced to exclude partial updates and tearing. I know there are stats already in u64 above, but maybe right now is the best time to use u64_stat_t for the new fields? :)

Quoted 20 lines
drivers/net/vmxnet3/vmxnet3_int.h +3 -2
 };
 
 struct vmxnet3_tx_ctx {
@@ -253,12 +260,13 @@ struct vmxnet3_tx_queue {
 						    * stopped */
 	int				qid;
 	u16				txdata_desc_size;
-} __attribute__((__aligned__(SMP_CACHE_BYTES)));
+} ____cacheline_aligned;
 
 enum vmxnet3_rx_buf_type {
 	VMXNET3_RX_BUF_NONE = 0,
 	VMXNET3_RX_BUF_SKB = 1,
-	VMXNET3_RX_BUF_PAGE = 2
+	VMXNET3_RX_BUF_PAGE = 2,
+	VMXNET3_RX_BUF_XDP = 3,
 };
 
 #define VMXNET3_RXD_COMP_PENDING        0

drivers/net/vmxnet3/vmxnet3_int.h struct vmxnet3_rq_driver_stats { · line 301

Quoted 9 lines
drivers/net/vmxnet3/vmxnet3_int.h +6 -0
@@ -285,6 +293,12 @@ struct vmxnet3_rq_driver_stats {
 	u64 drop_err;
 	u64 drop_fcs;
 	u64 rx_buf_alloc_failure;
+
+	u64 xdp_packets;	/* Total packets processed by XDP. */
+	u64 xdp_tx;
+	u64 xdp_redirects;
+	u64 xdp_drops;
+	u64 xdp_aborted;

(same)

Quoted 3 lines
drivers/net/vmxnet3/vmxnet3_int.h +0 -0
 };
 
 struct vmxnet3_rx_data_ring {

[...]

Quoted 27 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +27 -0
+static int
+vmxnet3_xdp_xmit_frame(struct vmxnet3_adapter *adapter,
+		       struct xdp_frame *xdpf,
+		       struct vmxnet3_tx_queue *tq, bool dma_map)
+{
+	struct vmxnet3_tx_buf_info *tbi = NULL;
+	union Vmxnet3_GenericDesc *gdesc;
+	struct vmxnet3_tx_ctx ctx;
+	int tx_num_deferred;
+	struct page *page;
+	u32 buf_size;
+	int ret = 0;
+	u32 dw2;
+
+	dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
+	dw2 |= xdpf->len;
+	ctx.sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
+	gdesc = ctx.sop_txd;
+
+	buf_size = xdpf->len;
+	tbi = tq->buf_info + tq->tx_ring.next2fill;
+
+	if (vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) == 0) {
+		tq->stats.tx_ring_full++;
+		return -ENOSPC;
+	}
+

drivers/net/vmxnet3/vmxnet3_xdp.c line 127

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +10 -0
+	tbi->map_type = VMXNET3_MAP_XDP;
+	if (dma_map) { /* ndo_xdp_xmit */
+		tbi->dma_addr = dma_map_single(&adapter->pdev->dev,
+					       xdpf->data, buf_size,
+					       DMA_TO_DEVICE);
+		if (dma_mapping_error(&adapter->pdev->dev, tbi->dma_addr))
+			return -EFAULT;
+		tbi->map_type |= VMXNET3_MAP_SINGLE;
+	} else { /* XDP buffer from page pool */
+		page = virt_to_head_page(xdpf->data);

Nit: your page pools are always order-0, thus you could shortcut those virt_to_head_page() throughout the drivers to just virt_to_page() and save at least 1 unlikely() condition check this way (it will always be false for the pages coming from your page pools). I remember we had huge CPU load point on this compound_head() check inside virt_to_head_page()...

drivers/net/vmxnet3/vmxnet3_xdp.c line 133

Quoted 6 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +6 -0
+		tbi->dma_addr = page_pool_get_dma_addr(page) +
+				XDP_PACKET_HEADROOM;
+		dma_sync_single_for_device(&adapter->pdev->dev,
+					   tbi->dma_addr, buf_size,
+					   DMA_BIDIRECTIONAL);
+	}

[...]

Those are some minors/propositions, up to you whether to address the rest was addressed perfectly by you, thanks!

Olek

author
#2 lore raw

Hi Alexander,
Thanks again for your review!

Quoted 42 lines · Alexander Lobakin

From: William Tu <[email protected]>
Date: Fri, 27 Jan 2023 08:30:27 -0800

Quoted 11 lines

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.

[...]

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +2 -0
@@ -1404,6 +1496,8 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
      struct Vmxnet3_RxDesc rxCmdDesc;
      struct Vmxnet3_RxCompDesc rxComp;
 #endif
+     bool need_flush = false;
+
      vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
                        &rxComp);
      while (rcd->gen == rq->comp_ring.gen) {

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1541

Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_drv.c +1 -0
@@ -1444,6 +1538,31 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
                      goto rcd_done;
              }
 
+             if (rcd->sop && rcd->eop && vmxnet3_xdp_enabled(adapter)) {

Hmm, it's a relatively big block of code for one if. I mean, could we do it like that?

                if (!rcd->sop || !vmxnet3_xdp_enabled(adapter))
                        goto skip_xdp;

                if (VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
                        ...
Quoted 4 lines

This way you would save 1 indent level and make code a little bit more readable.
But it might be a matter of personal tastes :) Just noticed you have this skip_xdp label anyway, why not reuse it here.

Good idea, this function has high indent level and this is a good idea. I will do it.

Quoted 24 lines
Quoted 11 lines
drivers/net/vmxnet3/vmxnet3_drv.c +10 -0
+                     struct sk_buff *skb_xdp_pass;
+                     int act;
+
+                     if (VMXNET3_RX_DATA_RING(adapter, rcd->rqID)) {
+                             ctx->skb = NULL;
+                             goto skip_xdp; /* Handle it later. */
+                     }
+
+                     if (rbi->buf_type != VMXNET3_RX_BUF_XDP)
+                             goto rcd_done;

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1561

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +10 -0
+
+                     act = vmxnet3_process_xdp(adapter, rq, rcd, rbi, rxd,
+                                               &skb_xdp_pass);
+                     if (act == XDP_PASS) {
+                             ctx->skb = skb_xdp_pass;
+                             goto sop_done;
+                     }
+                     ctx->skb = NULL;
+                     if (act == XDP_REDIRECT)
+                             need_flush = true;
                        need_flush |= act == XDP_REDIRECT;
Quoted 3 lines

But this looks a big ugly to be honest, I just wrote it to show off a bit :D Your if is perfectly fine, not even sure the line I wrote above produces more optimized object code.

ha, ok. Good to know there is another option.

Quoted 17 lines
Quoted 17 lines
drivers/net/vmxnet3/vmxnet3_drv.c +6 -1
+                     goto rcd_done;
+             }
+skip_xdp:
+
              if (rcd->sop) { /* first buf of the pkt */
                      bool rxDataRingUsed;
                      u16 len;
@@ -1452,7 +1571,8 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
                             (rcd->rqID != rq->qid &&
                              rcd->rqID != rq->dataRingQid));
 
-                     BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
+                     BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB &&
+                            rbi->buf_type != VMXNET3_RX_BUF_XDP);
                      BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
 
Quoted 9 lines

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1594

Quoted 7 lines
drivers/net/vmxnet3/vmxnet3_drv.c +2 -0
                      if (unlikely(rcd->len == 0)) {
@@ -1470,6 +1590,26 @@ vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
                      rxDataRingUsed =
                              VMXNET3_RX_DATA_RING(adapter, rcd->rqID);
                      len = rxDataRingUsed ? rcd->len : rbi->len;
+
+                     if (rxDataRingUsed && vmxnet3_xdp_enabled(adapter)) {

Maybe save 1 level here as well:

cool, will do it.

Quoted 33 lines
                        if (!rxDataRingUsed || !vmxnet3_xdp_enabled(ad))
                                goto alloc_skb;

                        sz = rcd->rxdIdx * ...
Quoted 8 lines
drivers/net/vmxnet3/vmxnet3_drv.c +8 -0
+                             struct sk_buff *skb_xdp_pass;
+                             size_t sz;
+                             int act;
+
+                             sz = rcd->rxdIdx * rq->data_ring.desc_size;
+                             act = vmxnet3_process_xdp_small(adapter, rq,
+                                                             &rq->data_ring.base[sz],
+                                                             rcd->len,

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq, · line 1612

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +10 -0
+                                                             &skb_xdp_pass);
+                             if (act == XDP_PASS) {
+                                     ctx->skb = skb_xdp_pass;
+                                     goto sop_done;
+                             }
+                             if (act == XDP_REDIRECT)
+                                     need_flush = true;
+
+                             goto rcd_done;
+                     }

alloc_skb:

Quoted 3 lines
drivers/net/vmxnet3/vmxnet3_drv.c +0 -0
                      new_skb = netdev_alloc_skb_ip_align(adapter->netdev,
                                                          len);
                      if (new_skb == NULL) {

[...]

Quoted 13 lines

drivers/net/vmxnet3/vmxnet3_int.h struct vmxnet3_tq_driver_stats { · line 226

Quoted 7 lines
drivers/net/vmxnet3/vmxnet3_int.h +3 -0
@@ -217,6 +221,9 @@ struct vmxnet3_tq_driver_stats {
      u64 linearized;         /* # of pkts linearized */
      u64 copy_skb_header;    /* # of times we have to copy skb header */
      u64 oversized_hdr;
+
+     u64 xdp_xmit;
+     u64 xdp_xmit_err;

Sorry for missing this earlier... You use u64 here for stats and previously we were using it for the stats, but then u64_stat_t was introduced to exclude partial updates and tearing. I know there are stats already in u64 above, but maybe right now is the best time to use u64_stat_t for the new fields? :)

thanks for the suggestion, I read through the u64_stats_t. if I replace with u64_stats_t, then there are several places to replace using u64_stats_init, update, ....
I will keep this as future work or separate patch.

Quoted 19 lines
Quoted 19 lines
drivers/net/vmxnet3/vmxnet3_int.h +3 -2
 };
 
 struct vmxnet3_tx_ctx {
@@ -253,12 +260,13 @@ struct vmxnet3_tx_queue {
                                                  * stopped */
      int                             qid;
      u16                             txdata_desc_size;
-} __attribute__((__aligned__(SMP_CACHE_BYTES)));
+} ____cacheline_aligned;
 
 enum vmxnet3_rx_buf_type {
      VMXNET3_RX_BUF_NONE = 0,
      VMXNET3_RX_BUF_SKB = 1,
-     VMXNET3_RX_BUF_PAGE = 2
+     VMXNET3_RX_BUF_PAGE = 2,
+     VMXNET3_RX_BUF_XDP = 3,
 };
 
Quoted 13 lines

drivers/net/vmxnet3/vmxnet3_int.h struct vmxnet3_rq_driver_stats { · line 301

Quoted 11 lines
drivers/net/vmxnet3/vmxnet3_int.h +6 -0
 #define VMXNET3_RXD_COMP_PENDING        0
@@ -285,6 +293,12 @@ struct vmxnet3_rq_driver_stats {
      u64 drop_err;
      u64 drop_fcs;
      u64 rx_buf_alloc_failure;
+
+     u64 xdp_packets;        /* Total packets processed by XDP. */
+     u64 xdp_tx;
+     u64 xdp_redirects;
+     u64 xdp_drops;
+     u64 xdp_aborted;

(same)

thanks! Will be future work or separate patch.

Quoted 42 lines
Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_int.h +0 -0
 };
 
 struct vmxnet3_rx_data_ring {

[...]

Quoted 35 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +35 -0
+static int
+vmxnet3_xdp_xmit_frame(struct vmxnet3_adapter *adapter,
+                    struct xdp_frame *xdpf,
+                    struct vmxnet3_tx_queue *tq, bool dma_map)
+{
+     struct vmxnet3_tx_buf_info *tbi = NULL;
+     union Vmxnet3_GenericDesc *gdesc;
+     struct vmxnet3_tx_ctx ctx;
+     int tx_num_deferred;
+     struct page *page;
+     u32 buf_size;
+     int ret = 0;
+     u32 dw2;
+
+     dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
+     dw2 |= xdpf->len;
+     ctx.sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
+     gdesc = ctx.sop_txd;
+
+     buf_size = xdpf->len;
+     tbi = tq->buf_info + tq->tx_ring.next2fill;
+
+     if (vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) == 0) {
+             tq->stats.tx_ring_full++;
+             return -ENOSPC;
+     }
+
+     tbi->map_type = VMXNET3_MAP_XDP;
+     if (dma_map) { /* ndo_xdp_xmit */
+             tbi->dma_addr = dma_map_single(&adapter->pdev->dev,
+                                            xdpf->data, buf_size,
+                                            DMA_TO_DEVICE);
+             if (dma_mapping_error(&adapter->pdev->dev, tbi->dma_addr))
+                     return -EFAULT;
+             tbi->map_type |= VMXNET3_MAP_SINGLE;
Quoted 8 lines

drivers/net/vmxnet3/vmxnet3_xdp.c line 127

Quoted 2 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +2 -0
+     } else { /* XDP buffer from page pool */
+             page = virt_to_head_page(xdpf->data);

Nit: your page pools are always order-0, thus you could shortcut those virt_to_head_page() throughout the drivers to just virt_to_page() and save at least 1 unlikely() condition check this way (it will always be false for the pages coming from your page pools). I remember we had huge CPU load point on this compound_head() check inside virt_to_head_page()...

good point! this avoids the _compound_head call
Will do it in next version

Quoted 8 lines

drivers/net/vmxnet3/vmxnet3_xdp.c line 133

Quoted 7 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +6 -0
+             tbi->dma_addr = page_pool_get_dma_addr(page) +
+                             XDP_PACKET_HEADROOM;
+             dma_sync_single_for_device(&adapter->pdev->dev,
+                                        tbi->dma_addr, buf_size,
+                                        DMA_BIDIRECTIONAL);
+     }
Quoted 6 lines

[...]

Those are some minors/propositions, up to you whether to address the rest was addressed perfectly by you, thanks!

Thanks again for your time to review the patch. Working on next version now. William

#3 lore raw
In reply to William Tu
Quoted 91 lines · William Tu

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

Quoted 9 lines

---
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
https://github.com/williamtu/net-next/compare/v14..v15

good stuff with this way of showing the diff to reviewers (y)

below some nits/questions from me. i was not following previous reviews/revisions, sorry.

Quoted 84 lines

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

---
 drivers/net/Kconfig                   |   1 +
 drivers/net/vmxnet3/Makefile          |   2 +-
 drivers/net/vmxnet3/vmxnet3_drv.c     | 224 ++++++++++++--
 drivers/net/vmxnet3/vmxnet3_ethtool.c |  14 +
 drivers/net/vmxnet3/vmxnet3_int.h     |  43 ++-
 drivers/net/vmxnet3/vmxnet3_xdp.c     | 419 ++++++++++++++++++++++++++
 drivers/net/vmxnet3/vmxnet3_xdp.h     |  49 +++
 7 files changed, 712 insertions(+), 40 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 9e63b8c43f3e..a4419d661bdd 100644
@@ -571,6 +571,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 +1 -0
index d3e7b27eb933..eb3b9688299b 100644
@@ -28,6 +28,7 @@
 #include <net/ip6_checksum.h>
 
 #include "vmxnet3_int.h"
+#include "vmxnet3_xdp.h"
 
 char vmxnet3_driver_name[] = "vmxnet3";
 #define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"

drivers/net/vmxnet3/vmxnet3_drv.c static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size) · line 327

Quoted 6 lines
drivers/net/vmxnet3/vmxnet3_drv.c +1 -2
@@ -323,17 +324,18 @@ static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
 
 
 static void
-vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
-		     struct pci_dev *pdev)
+vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi, struct pci_dev *pdev)

nit: unneeded code churn

Quoted 19 lines
drivers/net/vmxnet3/vmxnet3_drv.c +5 -3
 {
-	if (tbi->map_type == VMXNET3_MAP_SINGLE)
+	u32 map_type = tbi->map_type;
+
+	if (map_type & VMXNET3_MAP_SINGLE)
 		dma_unmap_single(&pdev->dev, tbi->dma_addr, tbi->len,
 				 DMA_TO_DEVICE);
-	else if (tbi->map_type == VMXNET3_MAP_PAGE)
+	else if (map_type & VMXNET3_MAP_PAGE)
 		dma_unmap_page(&pdev->dev, tbi->dma_addr, tbi->len,
 			       DMA_TO_DEVICE);
 	else
-		BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
+		BUG_ON((map_type & ~VMXNET3_MAP_XDP));
 
 	tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
 }
@@ -341,19 +343,20 @@ vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
 

(...)

Quoted 22 lines
drivers/net/vmxnet3/vmxnet3_drv.c +5 -3
@@ -1858,14 +2014,16 @@ static int
 vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
 		struct vmxnet3_adapter  *adapter)
 {
-	int i;
+	int i, err;
 
 	/* initialize buf_info */
 	for (i = 0; i < rq->rx_ring[0].size; i++) {
 
-		/* 1st buf for a pkt is skbuff */
+		/* 1st buf for a pkt is skbuff or xdp page */
 		if (i % adapter->rx_buf_per_pkt == 0) {
-			rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
+			rq->buf_info[0][i].buf_type = vmxnet3_xdp_enabled(adapter) ?
+						      VMXNET3_RX_BUF_XDP :
+						      VMXNET3_RX_BUF_SKB;
 			rq->buf_info[0][i].len = adapter->skb_buf_size;
 		} else { /* subsequent bufs for a pkt is frag */
 			rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
@@ -1886,6 +2044,12 @@ vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
 		rq->rx_ring[i].gen = VMXNET3_INIT_GEN;

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_init(struct vmxnet3_rx_queue *rq, · line 2054

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_drv.c +6 -0
 		rq->rx_ring[i].isOutOfOrder = 0;
 	}
+
+	err = vmxnet3_create_pp(adapter, rq,
+				rq->rx_ring[0].size + rq->rx_ring[1].size);
+	if (err)
+		return err;
+
 	if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
 				    adapter) == 0) {

Probably for completeness, if above fails then you should destroy pp resources that got successfully allocd in vmxnet3_create_pp

Quoted 14 lines
drivers/net/vmxnet3/vmxnet3_drv.c +1 -1
 		/* at least has 1 rx buffer for the 1st ring */
@@ -1989,7 +2153,7 @@ vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
 }
 
 
-static int
+int
 vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
 {
 	int i, err = 0;
@@ -3026,7 +3190,7 @@ vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
 }
 
 

(...)

Quoted 33 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +27 -0
new file mode 100644
index 000000000000..c698fb6213b2
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Linux driver for VMware's vmxnet3 ethernet NIC.
+ * Copyright (C) 2008-2023, VMware, Inc. All Rights Reserved.
+ * Maintained by: [email protected]
+ *
+ */
+
+#include "vmxnet3_int.h"
+#include "vmxnet3_xdp.h"
+
+static void
+vmxnet3_xdp_exchange_program(struct vmxnet3_adapter *adapter,
+			     struct bpf_prog *prog)
+{
+	rcu_assign_pointer(adapter->xdp_bpf_prog, prog);
+}
+
+static int
+vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf,
+		struct netlink_ext_ack *extack)
+{
+	struct vmxnet3_adapter *adapter = netdev_priv(netdev);
+	struct bpf_prog *new_bpf_prog = bpf->prog;
+	struct bpf_prog *old_bpf_prog;
+	bool need_update;
+	bool running;

drivers/net/vmxnet3/vmxnet3_xdp.c line 37

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +10 -0
+	int err;
+
+	if (new_bpf_prog && netdev->mtu > VMXNET3_XDP_MAX_MTU) {
+		NL_SET_ERR_MSG_MOD(extack, "MTU too large for XDP");
+		return -EOPNOTSUPP;
+	}
+
+	if (adapter->netdev->features & NETIF_F_LRO) {
+		NL_SET_ERR_MSG_MOD(extack, "LRO is not supported with XDP");
+		adapter->netdev->features &= ~NETIF_F_LRO;

don't you need to reflect clearing this flag in some way? Also what if someone tries to set it when xdp prog is loaded?

Quoted 39 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +39 -0
+	}
+
+	old_bpf_prog = rcu_dereference(adapter->xdp_bpf_prog);
+	if (!new_bpf_prog && !old_bpf_prog)
+		return 0;
+
+	running = netif_running(netdev);
+	need_update = !!old_bpf_prog != !!new_bpf_prog;
+
+	if (running && need_update)
+		vmxnet3_quiesce_dev(adapter);
+
+	vmxnet3_xdp_exchange_program(adapter, new_bpf_prog);
+	if (old_bpf_prog)
+		bpf_prog_put(old_bpf_prog);
+
+	if (!running || !need_update)
+		return 0;
+
+	vmxnet3_reset_dev(adapter);
+	vmxnet3_rq_destroy_all(adapter);
+	vmxnet3_adjust_rx_ring_size(adapter);
+	err = vmxnet3_rq_create_all(adapter);
+	if (err) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "failed to re-create rx queues for XDP.");
+		return -EOPNOTSUPP;
+	}
+	err = vmxnet3_activate_dev(adapter);
+	if (err) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "failed to activate device for XDP.");
+		return -EOPNOTSUPP;
+	}
+	clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
+
+	return 0;
+}
+

(...)

Quoted 43 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +43 -0
+static int
+vmxnet3_xdp_xmit_back(struct vmxnet3_adapter *adapter,
+		      struct xdp_frame *xdpf)
+{
+	struct vmxnet3_tx_queue *tq;
+	struct netdev_queue *nq;
+	int tq_number;
+	int err, cpu;
+
+	tq_number = adapter->num_tx_queues;
+	cpu = smp_processor_id();
+	if (likely(cpu < tq_number))
+		tq = &adapter->tx_queue[cpu];
+	else
+		tq = &adapter->tx_queue[reciprocal_scale(cpu, tq_number)];
+	if (tq->stopped)
+		return -ENETDOWN;
+
+	nq = netdev_get_tx_queue(adapter->netdev, tq->qid);
+
+	__netif_tx_lock(nq, cpu);
+	err = vmxnet3_xdp_xmit_frame(adapter, xdpf, tq, false);
+	__netif_tx_unlock(nq);
+
+	return err;
+}
+
+/* ndo_xdp_xmit */
+int
+vmxnet3_xdp_xmit(struct net_device *dev,
+		 int n, struct xdp_frame **frames, u32 flags)
+{
+	struct vmxnet3_adapter *adapter = netdev_priv(dev);
+	struct vmxnet3_tx_queue *tq;
+	struct netdev_queue *nq;
+	int i, err, cpu;
+	int tq_number;
+
+	if (unlikely(test_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state)))
+		return -ENETDOWN;
+	if (unlikely(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state)))
+		return -EINVAL;
+

drivers/net/vmxnet3/vmxnet3_xdp.c line 230

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +10 -0
+	tq_number = adapter->num_tx_queues;
+	cpu = smp_processor_id();
+	if (likely(cpu < tq_number))
+		tq = &adapter->tx_queue[cpu];
+	else
+		tq = &adapter->tx_queue[reciprocal_scale(cpu, tq_number)];
+	if (tq->stopped)
+		return -ENETDOWN;
+
+	nq = netdev_get_tx_queue(adapter->netdev, tq->qid);

getting the txq logic is repeated in vmxnet3_xdp_xmit_back(), maybe a helper?

Quoted 102 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +102 -0
+
+	for (i = 0; i < n; i++) {
+		err = vmxnet3_xdp_xmit_frame(adapter, frames[i], tq, true);
+		if (err) {
+			tq->stats.xdp_xmit_err++;
+			break;
+		}
+	}
+	tq->stats.xdp_xmit += i;
+
+	return i;
+}
+
+static int
+vmxnet3_run_xdp(struct vmxnet3_rx_queue *rq, struct xdp_buff *xdp)
+{
+	struct xdp_frame *xdpf;
+	struct bpf_prog *prog;
+	struct page *page;
+	int err;
+	u32 act;
+
+	prog = rcu_dereference(rq->adapter->xdp_bpf_prog);
+	act = bpf_prog_run_xdp(prog, xdp);
+	rq->stats.xdp_packets++;
+	page = virt_to_head_page(xdp->data_hard_start);
+
+	switch (act) {
+	case XDP_PASS:
+		return act;
+	case XDP_REDIRECT:
+		err = xdp_do_redirect(rq->adapter->netdev, xdp, prog);
+		if (!err)
+			rq->stats.xdp_redirects++;
+		else
+			rq->stats.xdp_drops++;
+		return act;
+	case XDP_TX:
+		xdpf = xdp_convert_buff_to_frame(xdp);
+		if (unlikely(!xdpf ||
+			     vmxnet3_xdp_xmit_back(rq->adapter, xdpf))) {
+			rq->stats.xdp_drops++;
+			page_pool_recycle_direct(rq->page_pool, page);
+		} else {
+			rq->stats.xdp_tx++;
+		}
+		return act;
+	default:
+		bpf_warn_invalid_xdp_action(rq->adapter->netdev, prog, act);
+		fallthrough;
+	case XDP_ABORTED:
+		trace_xdp_exception(rq->adapter->netdev, prog, act);
+		rq->stats.xdp_aborted++;
+		break;
+	case XDP_DROP:
+		rq->stats.xdp_drops++;
+		break;
+	}
+
+	page_pool_recycle_direct(rq->page_pool, page);
+
+	return act;
+}
+
+static struct sk_buff *
+vmxnet3_build_skb(struct vmxnet3_rx_queue *rq, struct page *page,
+		  const struct xdp_buff *xdp)
+{
+	struct sk_buff *skb;
+
+	skb = build_skb(page_address(page), PAGE_SIZE);
+	if (unlikely(!skb)) {
+		page_pool_recycle_direct(rq->page_pool, page);
+		rq->stats.rx_buf_alloc_failure++;
+		return NULL;
+	}
+
+	/* bpf prog might change len and data position. */
+	skb_reserve(skb, xdp->data - xdp->data_hard_start);
+	skb_put(skb, xdp->data_end - xdp->data);
+	skb_mark_for_recycle(skb);
+
+	return skb;
+}
+
+/* Handle packets from DataRing. */
+int
+vmxnet3_process_xdp_small(struct vmxnet3_adapter *adapter,
+			  struct vmxnet3_rx_queue *rq,
+			  void *data, int len,
+			  struct sk_buff **skb_xdp_pass)
+{
+	struct bpf_prog *xdp_prog;
+	struct xdp_buff xdp;
+	struct page *page;
+	int act;
+
+	page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
+	if (unlikely(!page)) {
+		rq->stats.rx_buf_alloc_failure++;
+		return XDP_DROP;
+	}

drivers/net/vmxnet3/vmxnet3_xdp.c line 342

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +10 -0
+
+	xdp_init_buff(&xdp, PAGE_SIZE, &rq->xdp_rxq);
+	xdp_prepare_buff(&xdp, page_address(page), rq->page_pool->p.offset,
+			 len, false);
+	xdp_buff_clear_frags_flag(&xdp);
+
+	/* Must copy the data because it's at dataring. */
+	memcpy(xdp.data, data, len);
+
+	rcu_read_lock();

https://lore.kernel.org/bpf/[email protected]/

drivers/net/vmxnet3/vmxnet3_xdp.c line 346

Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +4 -0
+	xdp_prog = rcu_dereference(rq->adapter->xdp_bpf_prog);
+	if (!xdp_prog) {
+		rcu_read_unlock();
+		page_pool_recycle_direct(rq->page_pool, page);

recycling the page that is going to be used for build_skb()? am i missing something?

drivers/net/vmxnet3/vmxnet3_xdp.c line 350

Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +4 -0
+		act = XDP_PASS;
+		goto out_skb;
+	}
+	act = vmxnet3_run_xdp(rq, &xdp);

pass the xdp_prog, no reason to deref this again inside vmxnet3_run_xdp()

Quoted 48 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +48 -0
+	rcu_read_unlock();
+
+	if (act == XDP_PASS) {
+out_skb:
+		*skb_xdp_pass = vmxnet3_build_skb(rq, page, &xdp);
+		if (!skb_xdp_pass)
+			return XDP_DROP;
+	}
+
+	/* No need to refill. */
+	return act;
+}
+
+int
+vmxnet3_process_xdp(struct vmxnet3_adapter *adapter,
+		    struct vmxnet3_rx_queue *rq,
+		    struct Vmxnet3_RxCompDesc *rcd,
+		    struct vmxnet3_rx_buf_info *rbi,
+		    struct Vmxnet3_RxDesc *rxd,
+		    struct sk_buff **skb_xdp_pass)
+{
+	struct bpf_prog *xdp_prog;
+	dma_addr_t new_dma_addr;
+	struct xdp_buff xdp;
+	struct page *page;
+	void *new_data;
+	int act;
+
+	page = rbi->page;
+	dma_sync_single_for_cpu(&adapter->pdev->dev,
+				page_pool_get_dma_addr(page) +
+				rq->page_pool->p.offset, rcd->len,
+				page_pool_get_dma_dir(rq->page_pool));
+
+	xdp_init_buff(&xdp, rbi->len, &rq->xdp_rxq);
+	xdp_prepare_buff(&xdp, page_address(page), rq->page_pool->p.offset,
+			 rcd->len, false);
+	xdp_buff_clear_frags_flag(&xdp);
+
+	rcu_read_lock();
+	xdp_prog = rcu_dereference(rq->adapter->xdp_bpf_prog);
+	if (!xdp_prog) {
+		rcu_read_unlock();
+		act = XDP_PASS;
+		goto out_skb;
+	}
+	act = vmxnet3_run_xdp(rq, &xdp);
+	rcu_read_unlock();

drivers/net/vmxnet3/vmxnet3_xdp.c line 408

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +10 -0
+
+	if (act == XDP_PASS) {
+out_skb:
+		*skb_xdp_pass = vmxnet3_build_skb(rq, page, &xdp);
+		if (!skb_xdp_pass)
+			act = XDP_DROP;
+	}
+
+	new_data = vmxnet3_pp_get_buff(rq->page_pool, &new_dma_addr,
+				       GFP_ATOMIC);

are you refilling per each processed buf?

Quoted 69 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +11 -0
+	if (!new_data) {
+		rq->stats.rx_buf_alloc_failure++;
+		return XDP_DROP;
+	}
+	rbi->page = virt_to_head_page(new_data);
+	rbi->dma_addr = new_dma_addr;
+	rxd->addr = cpu_to_le64(rbi->dma_addr);
+	rxd->len = rbi->len;
+
+	return act;
+}
drivers/net/vmxnet3/vmxnet3_xdp.h new file +49 -0
new file mode 100644
index 000000000000..b847af941f5a
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Linux driver for VMware's vmxnet3 ethernet NIC.
+ * Copyright (C) 2008-2023, VMware, Inc. All Rights Reserved.
+ * Maintained by: [email protected]
+ *
+ */
+
+#ifndef _VMXNET3_XDP_H
+#define _VMXNET3_XDP_H
+
+#include <linux/filter.h>
+#include <linux/bpf_trace.h>
+#include <linux/netlink.h>
+#include <net/page_pool.h>
+#include <net/xdp.h>
+
+#include "vmxnet3_int.h"
+
+#define VMXNET3_XDP_HEADROOM	(XDP_PACKET_HEADROOM + NET_IP_ALIGN)
+#define VMXNET3_XDP_RX_TAILROOM	SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
+#define VMXNET3_XDP_RX_OFFSET	VMXNET3_XDP_HEADROOM
+#define VMXNET3_XDP_MAX_FRSIZE	(PAGE_SIZE - VMXNET3_XDP_HEADROOM - \
+				 VMXNET3_XDP_RX_TAILROOM)
+#define VMXNET3_XDP_MAX_MTU	(VMXNET3_XDP_MAX_FRSIZE - ETH_HLEN - \
+				 2 * VLAN_HLEN - ETH_FCS_LEN)
+
+int vmxnet3_xdp(struct net_device *netdev, struct netdev_bpf *bpf);
+int vmxnet3_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
+		     u32 flags);
+int vmxnet3_process_xdp(struct vmxnet3_adapter *adapter,
+			struct vmxnet3_rx_queue *rq,
+			struct Vmxnet3_RxCompDesc *rcd,
+			struct vmxnet3_rx_buf_info *rbi,
+			struct Vmxnet3_RxDesc *rxd,
+			struct sk_buff **skb_xdp_pass);
+int vmxnet3_process_xdp_small(struct vmxnet3_adapter *adapter,
+			      struct vmxnet3_rx_queue *rq,
+			      void *data, int len,
+			      struct sk_buff **skb_xdp_pass);
+void *vmxnet3_pp_get_buff(struct page_pool *pp, dma_addr_t *dma_addr,
+			  gfp_t gfp_mask);
+
+static inline bool vmxnet3_xdp_enabled(struct vmxnet3_adapter *adapter)
+{
+	return !!rcu_access_pointer(adapter->xdp_bpf_prog);
+}
+
+#endif
Signature · 1 line
2.37.1 (Apple Git-137.1)
author
#4 lore raw

Hi Maciej,

Thanks for reviewing the patch!

Quoted 103 lines · Maciej Fijalkowski
Quoted 91 lines · William Tu

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

Quoted 9 lines

---
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
https://github.com/williamtu/net-next/compare/v14..v15

Quoted 1 line

good stuff with this way of showing the diff to reviewers (y)

Thanks! It's also for me to double check whether I address all the reviewer's comments.

Quoted 90 lines

below some nits/questions from me. i was not following previous reviews/revisions, sorry.

Quoted 86 lines

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

---
 drivers/net/Kconfig                   |   1 +
 drivers/net/vmxnet3/Makefile          |   2 +-
 drivers/net/vmxnet3/vmxnet3_drv.c     | 224 ++++++++++++--
 drivers/net/vmxnet3/vmxnet3_ethtool.c |  14 +
 drivers/net/vmxnet3/vmxnet3_int.h     |  43 ++-
 drivers/net/vmxnet3/vmxnet3_xdp.c     | 419 ++++++++++++++++++++++++++
 drivers/net/vmxnet3/vmxnet3_xdp.h     |  49 +++
 7 files changed, 712 insertions(+), 40 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 9e63b8c43f3e..a4419d661bdd 100644
@@ -571,6 +571,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 +1 -0
index d3e7b27eb933..eb3b9688299b 100644
@@ -28,6 +28,7 @@
 #include <net/ip6_checksum.h>
 
 #include "vmxnet3_int.h"
+#include "vmxnet3_xdp.h"
 
 char vmxnet3_driver_name[] = "vmxnet3";
 #define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"
@@ -323,17 +324,18 @@ static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
 
 
Quoted 6 lines

drivers/net/vmxnet3/vmxnet3_drv.c static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size) · line 327

Quoted 4 lines
drivers/net/vmxnet3/vmxnet3_drv.c +1 -2
 static void
-vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
-                  struct pci_dev *pdev)
+vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi, struct pci_dev *pdev)

nit: unneeded code churn

right, this is not necessary changed.

Quoted 50 lines
Quoted 20 lines
drivers/net/vmxnet3/vmxnet3_drv.c +5 -3
 {
-     if (tbi->map_type == VMXNET3_MAP_SINGLE)
+     u32 map_type = tbi->map_type;
+
+     if (map_type & VMXNET3_MAP_SINGLE)
              dma_unmap_single(&pdev->dev, tbi->dma_addr, tbi->len,
                               DMA_TO_DEVICE);
-     else if (tbi->map_type == VMXNET3_MAP_PAGE)
+     else if (map_type & VMXNET3_MAP_PAGE)
              dma_unmap_page(&pdev->dev, tbi->dma_addr, tbi->len,
                             DMA_TO_DEVICE);
      else
-             BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
+             BUG_ON((map_type & ~VMXNET3_MAP_XDP));
 
      tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
 }
@@ -341,19 +343,20 @@ vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
 

(...)

Quoted 27 lines
drivers/net/vmxnet3/vmxnet3_drv.c +8 -3
@@ -1858,14 +2014,16 @@ static int
 vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
              struct vmxnet3_adapter  *adapter)
 {
-     int i;
+     int i, err;
 
      /* initialize buf_info */
      for (i = 0; i < rq->rx_ring[0].size; i++) {
 
-             /* 1st buf for a pkt is skbuff */
+             /* 1st buf for a pkt is skbuff or xdp page */
              if (i % adapter->rx_buf_per_pkt == 0) {
-                     rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
+                     rq->buf_info[0][i].buf_type = vmxnet3_xdp_enabled(adapter) ?
+                                                   VMXNET3_RX_BUF_XDP :
+                                                   VMXNET3_RX_BUF_SKB;
                      rq->buf_info[0][i].len = adapter->skb_buf_size;
              } else { /* subsequent bufs for a pkt is frag */
                      rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
@@ -1886,6 +2044,12 @@ vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
              rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
              rq->rx_ring[i].isOutOfOrder = 0;
      }
+
+     err = vmxnet3_create_pp(adapter, rq,
+                             rq->rx_ring[0].size + rq->rx_ring[1].size);
Quoted 8 lines

drivers/net/vmxnet3/vmxnet3_drv.c vmxnet3_rq_init(struct vmxnet3_rx_queue *rq, · line 2054

Quoted 5 lines
drivers/net/vmxnet3/vmxnet3_drv.c +3 -0
+     if (err)
+             return err;
+
      if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
                                  adapter) == 0) {

Probably for completeness, if above fails then you should destroy pp resources that got successfully allocd in vmxnet3_create_pp

Good catch, will fix it.

Quoted 55 lines
Quoted 15 lines
drivers/net/vmxnet3/vmxnet3_drv.c +1 -1
              /* at least has 1 rx buffer for the 1st ring */
@@ -1989,7 +2153,7 @@ vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
 }
 
 
-static int
+int
 vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
 {
      int i, err = 0;
@@ -3026,7 +3190,7 @@ vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
 }
 
 

(...)

Quoted 37 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +31 -0
new file mode 100644
index 000000000000..c698fb6213b2
@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Linux driver for VMware's vmxnet3 ethernet NIC.
+ * Copyright (C) 2008-2023, VMware, Inc. All Rights Reserved.
+ * Maintained by: [email protected]
+ *
+ */
+
+#include "vmxnet3_int.h"
+#include "vmxnet3_xdp.h"
+
+static void
+vmxnet3_xdp_exchange_program(struct vmxnet3_adapter *adapter,
+                          struct bpf_prog *prog)
+{
+     rcu_assign_pointer(adapter->xdp_bpf_prog, prog);
+}
+
+static int
+vmxnet3_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf,
+             struct netlink_ext_ack *extack)
+{
+     struct vmxnet3_adapter *adapter = netdev_priv(netdev);
+     struct bpf_prog *new_bpf_prog = bpf->prog;
+     struct bpf_prog *old_bpf_prog;
+     bool need_update;
+     bool running;
+     int err;
+
+     if (new_bpf_prog && netdev->mtu > VMXNET3_XDP_MAX_MTU) {
+             NL_SET_ERR_MSG_MOD(extack, "MTU too large for XDP");
Quoted 8 lines

drivers/net/vmxnet3/vmxnet3_xdp.c line 37

Quoted 6 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +6 -0
+             return -EOPNOTSUPP;
+     }
+
+     if (adapter->netdev->features & NETIF_F_LRO) {
+             NL_SET_ERR_MSG_MOD(extack, "LRO is not supported with XDP");
+             adapter->netdev->features &= ~NETIF_F_LRO;

don't you need to reflect clearing this flag in some way?

right, probably I should print s.t like
"LRO is not supported with XDP, disable it"
and it takes effect later on when we call vmxnet3_activate_dev()

Quoted 1 line

Also what if someone tries to set it when xdp prog is loaded?

I checked the vmxnet3_set_features, and looks like enabling/disabling LRO won't trigger NIC reset.
In this case, we will have xdp program remains loaded, but LRO becomes enabled. Then our XDP prog won't be trigger due to the check of both sop and eop.

I will add a warning in vmxnet3_fix_features, checking that if if (vmxnet3_xdp_enabled(adapter) && (features & NETIF_F_LRO))

        netdev_err(netdev, "Can not enable LRO when XDP program is loaded");

Or another way is to unload the XDP prog, and allow users to enable LRO.

Quoted 206 lines
Quoted 40 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +39 -0
+     }
+
+     old_bpf_prog = rcu_dereference(adapter->xdp_bpf_prog);
+     if (!new_bpf_prog && !old_bpf_prog)
+             return 0;
+
+     running = netif_running(netdev);
+     need_update = !!old_bpf_prog != !!new_bpf_prog;
+
+     if (running && need_update)
+             vmxnet3_quiesce_dev(adapter);
+
+     vmxnet3_xdp_exchange_program(adapter, new_bpf_prog);
+     if (old_bpf_prog)
+             bpf_prog_put(old_bpf_prog);
+
+     if (!running || !need_update)
+             return 0;
+
+     vmxnet3_reset_dev(adapter);
+     vmxnet3_rq_destroy_all(adapter);
+     vmxnet3_adjust_rx_ring_size(adapter);
+     err = vmxnet3_rq_create_all(adapter);
+     if (err) {
+             NL_SET_ERR_MSG_MOD(extack,
+                                "failed to re-create rx queues for XDP.");
+             return -EOPNOTSUPP;
+     }
+     err = vmxnet3_activate_dev(adapter);
+     if (err) {
+             NL_SET_ERR_MSG_MOD(extack,
+                                "failed to activate device for XDP.");
+             return -EOPNOTSUPP;
+     }
+     clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
+
+     return 0;
+}
+

(...)

Quoted 43 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +43 -0
+static int
+vmxnet3_xdp_xmit_back(struct vmxnet3_adapter *adapter,
+                   struct xdp_frame *xdpf)
+{
+     struct vmxnet3_tx_queue *tq;
+     struct netdev_queue *nq;
+     int tq_number;
+     int err, cpu;
+
+     tq_number = adapter->num_tx_queues;
+     cpu = smp_processor_id();
+     if (likely(cpu < tq_number))
+             tq = &adapter->tx_queue[cpu];
+     else
+             tq = &adapter->tx_queue[reciprocal_scale(cpu, tq_number)];
+     if (tq->stopped)
+             return -ENETDOWN;
+
+     nq = netdev_get_tx_queue(adapter->netdev, tq->qid);
+
+     __netif_tx_lock(nq, cpu);
+     err = vmxnet3_xdp_xmit_frame(adapter, xdpf, tq, false);
+     __netif_tx_unlock(nq);
+
+     return err;
+}
+
+/* ndo_xdp_xmit */
+int
+vmxnet3_xdp_xmit(struct net_device *dev,
+              int n, struct xdp_frame **frames, u32 flags)
+{
+     struct vmxnet3_adapter *adapter = netdev_priv(dev);
+     struct vmxnet3_tx_queue *tq;
+     struct netdev_queue *nq;
+     int i, err, cpu;
+     int tq_number;
+
+     if (unlikely(test_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state)))
+             return -ENETDOWN;
+     if (unlikely(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state)))
+             return -EINVAL;
+

drivers/net/vmxnet3/vmxnet3_xdp.c line 230

Quoted 10 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +10 -0
+     tq_number = adapter->num_tx_queues;
+     cpu = smp_processor_id();
+     if (likely(cpu < tq_number))
+             tq = &adapter->tx_queue[cpu];
+     else
+             tq = &adapter->tx_queue[reciprocal_scale(cpu, tq_number)];
+     if (tq->stopped)
+             return -ENETDOWN;
+
+     nq = netdev_get_tx_queue(adapter->netdev, tq->qid);

getting the txq logic is repeated in vmxnet3_xdp_xmit_back(), maybe a helper?

Quoted 106 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +106 -0
+
+     for (i = 0; i < n; i++) {
+             err = vmxnet3_xdp_xmit_frame(adapter, frames[i], tq, true);
+             if (err) {
+                     tq->stats.xdp_xmit_err++;
+                     break;
+             }
+     }
+     tq->stats.xdp_xmit += i;
+
+     return i;
+}
+
+static int
+vmxnet3_run_xdp(struct vmxnet3_rx_queue *rq, struct xdp_buff *xdp)
+{
+     struct xdp_frame *xdpf;
+     struct bpf_prog *prog;
+     struct page *page;
+     int err;
+     u32 act;
+
+     prog = rcu_dereference(rq->adapter->xdp_bpf_prog);
+     act = bpf_prog_run_xdp(prog, xdp);
+     rq->stats.xdp_packets++;
+     page = virt_to_head_page(xdp->data_hard_start);
+
+     switch (act) {
+     case XDP_PASS:
+             return act;
+     case XDP_REDIRECT:
+             err = xdp_do_redirect(rq->adapter->netdev, xdp, prog);
+             if (!err)
+                     rq->stats.xdp_redirects++;
+             else
+                     rq->stats.xdp_drops++;
+             return act;
+     case XDP_TX:
+             xdpf = xdp_convert_buff_to_frame(xdp);
+             if (unlikely(!xdpf ||
+                          vmxnet3_xdp_xmit_back(rq->adapter, xdpf))) {
+                     rq->stats.xdp_drops++;
+                     page_pool_recycle_direct(rq->page_pool, page);
+             } else {
+                     rq->stats.xdp_tx++;
+             }
+             return act;
+     default:
+             bpf_warn_invalid_xdp_action(rq->adapter->netdev, prog, act);
+             fallthrough;
+     case XDP_ABORTED:
+             trace_xdp_exception(rq->adapter->netdev, prog, act);
+             rq->stats.xdp_aborted++;
+             break;
+     case XDP_DROP:
+             rq->stats.xdp_drops++;
+             break;
+     }
+
+     page_pool_recycle_direct(rq->page_pool, page);
+
+     return act;
+}
+
+static struct sk_buff *
+vmxnet3_build_skb(struct vmxnet3_rx_queue *rq, struct page *page,
+               const struct xdp_buff *xdp)
+{
+     struct sk_buff *skb;
+
+     skb = build_skb(page_address(page), PAGE_SIZE);
+     if (unlikely(!skb)) {
+             page_pool_recycle_direct(rq->page_pool, page);
+             rq->stats.rx_buf_alloc_failure++;
+             return NULL;
+     }
+
+     /* bpf prog might change len and data position. */
+     skb_reserve(skb, xdp->data - xdp->data_hard_start);
+     skb_put(skb, xdp->data_end - xdp->data);
+     skb_mark_for_recycle(skb);
+
+     return skb;
+}
+
+/* Handle packets from DataRing. */
+int
+vmxnet3_process_xdp_small(struct vmxnet3_adapter *adapter,
+                       struct vmxnet3_rx_queue *rq,
+                       void *data, int len,
+                       struct sk_buff **skb_xdp_pass)
+{
+     struct bpf_prog *xdp_prog;
+     struct xdp_buff xdp;
+     struct page *page;
+     int act;
+
+     page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
+     if (unlikely(!page)) {
+             rq->stats.rx_buf_alloc_failure++;
+             return XDP_DROP;
+     }
+
+     xdp_init_buff(&xdp, PAGE_SIZE, &rq->xdp_rxq);
+     xdp_prepare_buff(&xdp, page_address(page), rq->page_pool->p.offset,
+                      len, false);
Quoted 8 lines

drivers/net/vmxnet3/vmxnet3_xdp.c line 342

Quoted 6 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +6 -0
+     xdp_buff_clear_frags_flag(&xdp);
+
+     /* Must copy the data because it's at dataring. */
+     memcpy(xdp.data, data, len);
+
+     rcu_read_lock();

https://lore.kernel.org/bpf/[email protected]/

Got it, I read through the discussion.
Look like the rcu_read_lock is not needed anymore here.

Quoted 8 lines

drivers/net/vmxnet3/vmxnet3_xdp.c line 346

Quoted 5 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +4 -0
+     xdp_prog = rcu_dereference(rq->adapter->xdp_bpf_prog);
+     if (!xdp_prog) {
+             rcu_read_unlock();
+             page_pool_recycle_direct(rq->page_pool, page);

recycling the page that is going to be used for build_skb()? am i missing something?

you're right... that's another mistake. Will fix it.

Quoted 7 lines

drivers/net/vmxnet3/vmxnet3_xdp.c line 350

Quoted 5 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +4 -0
+             act = XDP_PASS;
+             goto out_skb;
+     }
+     act = vmxnet3_run_xdp(rq, &xdp);

pass the xdp_prog, no reason to deref this again inside vmxnet3_run_xdp()

OK! will do it

Quoted 53 lines
Quoted 53 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +52 -0
+     rcu_read_unlock();
+
+     if (act == XDP_PASS) {
+out_skb:
+             *skb_xdp_pass = vmxnet3_build_skb(rq, page, &xdp);
+             if (!skb_xdp_pass)
+                     return XDP_DROP;
+     }
+
+     /* No need to refill. */
+     return act;
+}
+
+int
+vmxnet3_process_xdp(struct vmxnet3_adapter *adapter,
+                 struct vmxnet3_rx_queue *rq,
+                 struct Vmxnet3_RxCompDesc *rcd,
+                 struct vmxnet3_rx_buf_info *rbi,
+                 struct Vmxnet3_RxDesc *rxd,
+                 struct sk_buff **skb_xdp_pass)
+{
+     struct bpf_prog *xdp_prog;
+     dma_addr_t new_dma_addr;
+     struct xdp_buff xdp;
+     struct page *page;
+     void *new_data;
+     int act;
+
+     page = rbi->page;
+     dma_sync_single_for_cpu(&adapter->pdev->dev,
+                             page_pool_get_dma_addr(page) +
+                             rq->page_pool->p.offset, rcd->len,
+                             page_pool_get_dma_dir(rq->page_pool));
+
+     xdp_init_buff(&xdp, rbi->len, &rq->xdp_rxq);
+     xdp_prepare_buff(&xdp, page_address(page), rq->page_pool->p.offset,
+                      rcd->len, false);
+     xdp_buff_clear_frags_flag(&xdp);
+
+     rcu_read_lock();
+     xdp_prog = rcu_dereference(rq->adapter->xdp_bpf_prog);
+     if (!xdp_prog) {
+             rcu_read_unlock();
+             act = XDP_PASS;
+             goto out_skb;
+     }
+     act = vmxnet3_run_xdp(rq, &xdp);
+     rcu_read_unlock();
+
+     if (act == XDP_PASS) {
+out_skb:
+             *skb_xdp_pass = vmxnet3_build_skb(rq, page, &xdp);
Quoted 8 lines

drivers/net/vmxnet3/vmxnet3_xdp.c line 408

Quoted 6 lines
drivers/net/vmxnet3/vmxnet3_xdp.c new file +6 -0
+             if (!skb_xdp_pass)
+                     act = XDP_DROP;
+     }
+
+     new_data = vmxnet3_pp_get_buff(rq->page_pool, &new_dma_addr,
+                                    GFP_ATOMIC);

are you refilling per each processed buf?

yes, I process one packet, and refill immediately with another one. Maybe there is a better way to do batch refill?

Thanks again for taking another check of this patch and found more issues! I will work on next version
William