Skip to contentSkip to conversation

net: bcmgenet: add XDP support

PATCH Superseded Nicolai Buchwitz 3 participants 22 messages Apr 28 – May 1

Subsystems (4)

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Nicolai Buchwitz

author maintainer
#0 lore raw

v8 cover net: bcmgenet: add XDP support

Add XDP support to the bcmgenet driver, covering XDP_PASS, XDP_DROP, XDP_TX, XDP_REDIRECT, and ndo_xdp_xmit.

The first patch converts the RX path from the existing kmalloc-based allocation to page_pool, which is a prerequisite for XDP. The remaining patches incrementally add XDP functionality and per-action statistics.

Tested on Raspberry Pi CM4 (BCM2711, bcmgenet, 1Gbps link):
- XDP_PASS: 943 Mbit/s TX, 935 Mbit/s RX (no regression vs baseline)
- XDP_PASS latency: 0.164ms avg, 0% packet loss
- XDP_DROP: all inbound traffic blocked as expected
- XDP_TX: TX counter increments (packet reflection working)
- Link flap with XDP attached: no errors
- Program swap under iperf3 load: no errors
- Upstream XDP selftests (xdp.py): pass_sb, drop_sb, tx_sb passing
- XDP-based EtherCAT master (~37 kHz cycle rate, all packet processing in BPF/XDP), stable over multiple days

Previous versions:
v7: https://lore.kernel.org/netdev/[email protected]/
v6: https://lore.kernel.org/netdev/[email protected]/
v5: https://lore.kernel.org/netdev/[email protected]/
v4: https://lore.kernel.org/netdev/[email protected]/
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/

Changes since v7:
- No code changes; resubmitted after net-next reopened.

Changes since v6:
- Removed GENET_XDP_HEADROOM alias, use XDP_PACKET_HEADROOM directly. (Jakub Kicinski)
- Dropped redundant __GFP_NOWARN from page_pool_alloc_pages(), page_pool adds it automatically. (Jakub Kicinski)
- Removed floating code block in desc_rx, moved variables to outer scope. (Jakub Kicinski)
- Make bcmgenet_run_xdp() return XDP_PASS when no program is set, removing the if (xdp_prog) indentation from desc_rx. (Jakub Kicinski)

Changes since v5:
- Refactored desc_rx: always prepare xdp_buff and use bcmgenet_xdp_build_skb for both XDP and non-XDP paths, treating no-prog as XDP_PASS. (Jakub Kicinski)
- Removed synchronize_net() before bpf_prog_put(), RCU handles the grace period. (Jakub Kicinski)
- Save status->rx_csum before running XDP program to prevent bpf_xdp_adjust_head from corrupting the RSB checksum. (Jakub Kicinski)
- Tightened TSB headroom check to include sizeof(struct xdp_frame). (Jakub Kicinski)
- Fixed reclaim gating: check for pending frames on the XDP TX ring instead of priv->xdp_prog, so in-flight frames are still reclaimed after XDP program detach. (Jakub Kicinski)
- Removed dead len -= ETH_FCS_LEN in patch 1. (Mohsin Bashir)
- Added patch 7: minimal ndo_change_mtu that rejects MTU values incompatible with XDP when a program is attached. (Mohsin Bashir, Florian Fainelli)

Changes since v4:
- Fixed unused variable warning: moved tx_ring declaration from patch 4 to patch 5 where it is first used. (Jakub Kicinski)

Changes since v3:
- Fixed xdp_prepare_buff() called with meta_valid=false, causing bcmgenet_xdp_build_skb() to compute metasize=UINT_MAX and corrupt skb meta_len. Now passes true. (Simon Horman)
- Removed bcmgenet_dump_tx_queue() for ring 16 in bcmgenet_timeout(). Ring 16 has no netdev TX queue, so netdev_get_tx_queue(dev, 16) accessed beyond the allocated _tx array. (Simon Horman)
- Fixed checkpatch alignment warnings in patches 4 and 5.

Changes since v2:
- Fixed page leak on partial bcmgenet_alloc_rx_buffers() failure:
free already-allocated rx_cbs before destroying page pool. (Simon Horman)
- Fixed GENET_Q16_TX_BD_CNT defined as 64 instead of 32. (Simon Horman)
- Moved XDP TX ring to a separate struct member (xdp_tx_ring)

    instead of expanding tx_rings[] to DESC_INDEX+1. (Justin Chen)

- Added synchronize_net() before bpf_prog_put() in XDP prog swap.
- Removed goto drop_page inside switch; inlined page_pool_put calls in each failure path. (Justin Chen)
- Removed unnecessary curly braces around case XDP_TX. (Justin Chen)
- Moved int err hoisting from patch 2 to patch 1. (Justin Chen)
- Kept return type on same line as function name, per driver convention. (Justin Chen)
- XDP TX packets/bytes now counted in TX reclaim for standard network statistics.

Changes since v1:
- Fixed tx_rings[DESC_INDEX] out-of-bounds access. Expanded array to DESC_INDEX+1 and initialized ring 16 with dedicated BDs.
- Use ring 16 (hardware default descriptor ring) for XDP TX, isolating from normal SKB TX queues.
- Piggyback ring 16 TX completion on RX NAPI poll (INTRL2_1 bit collision with RX ring 0).
- Fixed ring 16 TX reclaim: skip INTRL2_1 clear, skip BQL completion, use non-destructive reclaim in RX poll path.
- Prepend zeroed TSB before XDP TX frame data (TBUF_64B_EN requires 64-byte struct status_64 prefix on all TX buffers).
- Tested with upstream XDP selftests (xdp.py): pass_sb, drop_sb, tx_sb all passing. The multi-buffer tests (pass_mb, drop_mb, tx_mb) fail because bcmgenet does not support jumbo frames / MTU changes; I plan to add ndo_change_mtu support in a follow-up series.

Nicolai Buchwitz (7):
net: bcmgenet: convert RX path to page_pool
net: bcmgenet: register xdp_rxq_info for each RX ring
net: bcmgenet: add basic XDP support (PASS/DROP)
net: bcmgenet: add XDP_TX support
net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support
net: bcmgenet: add XDP statistics counters
net: bcmgenet: reject MTU changes incompatible with XDP

 drivers/net/ethernet/broadcom/Kconfig         |   1 +
 .../net/ethernet/broadcom/genet/bcmgenet.c    | 637 +++++++++++++++---
 .../net/ethernet/broadcom/genet/bcmgenet.h    |  19 +
 3 files changed, 559 insertions(+), 98 deletions(-)

--
2.51.0

author maintainer
#3 lore raw

PATCH v8 2/7 net: bcmgenet: register xdp_rxq_info for each RX ring

Register an xdp_rxq_info per RX ring and associate it with the ring's page_pool via MEM_TYPE_PAGE_POOL. This is required infrastructure for XDP program execution: the XDP framework needs to know the memory model backing each RX queue for correct page lifecycle management.

No functional change - XDP programs are not yet attached or executed.

---
 drivers/net/ethernet/broadcom/genet/bcmgenet.c | 18 ++++++++++++++++++
 drivers/net/ethernet/broadcom/genet/bcmgenet.h |  2 ++
 2 files changed, 20 insertions(+)
drivers/net/ethernet/broadcom/genet/bcmgenet.c +18 -0
index d013a3df9048..e71d9713f917 100644
@@ -2780,7 +2780,23 @@ static int bcmgenet_rx_ring_create_pool(struct bcmgenet_priv *priv,
 		return err;
 	}
 
+	err = xdp_rxq_info_reg(&ring->xdp_rxq, priv->dev, ring->index, 0);
+	if (err)
+		goto err_free_pp;
+
+	err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq, MEM_TYPE_PAGE_POOL,
+					 ring->page_pool);
+	if (err)
+		goto err_unreg_rxq;
+
 	return 0;
+
+err_unreg_rxq:
+	xdp_rxq_info_unreg(&ring->xdp_rxq);
+err_free_pp:
+	page_pool_destroy(ring->page_pool);
+	ring->page_pool = NULL;
+	return err;
 }
 
 /* Initialize a RDMA ring */
@@ -2809,6 +2825,7 @@ static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
 	if (ret) {
 		for (i = 0; i < ring->size; i++)
 			bcmgenet_free_rx_cb(ring->cbs + i, ring->page_pool);
+		xdp_rxq_info_unreg(&ring->xdp_rxq);
 		page_pool_destroy(ring->page_pool);
 		ring->page_pool = NULL;
 		return ret;
@@ -3014,6 +3031,7 @@ static void bcmgenet_destroy_rx_page_pools(struct bcmgenet_priv *priv)
 	for (i = 0; i <= priv->hw_params->rx_queues; ++i) {
 		ring = &priv->rx_rings[i];
 		if (ring->page_pool) {
+			xdp_rxq_info_unreg(&ring->xdp_rxq);
 			page_pool_destroy(ring->page_pool);
 			ring->page_pool = NULL;
 		}
drivers/net/ethernet/broadcom/genet/bcmgenet.h +2 -0
index 11a0ec563a89..82a6d29f481d 100644
@@ -16,6 +16,7 @@
 #include <linux/dim.h>
 #include <linux/ethtool.h>
 #include <net/page_pool/helpers.h>
+#include <net/xdp.h>
 
 #include "../unimac.h"
 
@@ -579,6 +580,7 @@ struct bcmgenet_rx_ring {
 	u32		rx_max_coalesced_frames;
 	u32		rx_coalesce_usecs;
 	struct page_pool *page_pool;
+	struct xdp_rxq_info xdp_rxq;
 	struct bcmgenet_priv *priv;
 };
 
Signature · 1 line
2.51.0
author maintainer
#1 lore raw

PATCH v8 1/7 net: bcmgenet: convert RX path to page_pool

In reply to Nicolai Buchwitz

Replace the per-packet __netdev_alloc_skb() + dma_map_single() in the RX path with page_pool, which provides efficient page recycling and DMA mapping management. This is a prerequisite for XDP support (which requires stable page-backed buffers rather than SKB linear data).

Key changes:
- Create a page_pool per RX ring (PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV)
- bcmgenet_rx_refill() allocates pages via page_pool_alloc_pages()
- bcmgenet_desc_rx() builds SKBs from pages via napi_build_skb() with skb_mark_for_recycle() for automatic page_pool return
- Buffer layout reserves XDP_PACKET_HEADROOM (256 bytes) before the HW RSB (64 bytes) + alignment pad (2 bytes) for future XDP headroom

---
 drivers/net/ethernet/broadcom/Kconfig         |   1 +
 .../net/ethernet/broadcom/genet/bcmgenet.c    | 217 +++++++++++-------
 .../net/ethernet/broadcom/genet/bcmgenet.h    |   4 +
 3 files changed, 143 insertions(+), 79 deletions(-)
drivers/net/ethernet/broadcom/Kconfig +1 -0
index 4287edc7ddd6..f0bac0dd1439 100644
@@ -78,6 +78,7 @@ config BCMGENET
 	select BCM7XXX_PHY
 	select MDIO_BCM_UNIMAC
 	select DIMLIB
+	select PAGE_POOL
 	select BROADCOM_PHY if ARCH_BCM2835
 	help
 	  This driver supports the built-in Ethernet MACs found in the
drivers/net/ethernet/broadcom/genet/bcmgenet.c +138 -79

353 lines not on this page · raw message

drivers/net/ethernet/broadcom/genet/bcmgenet.h +4 -0
index 9e4110c7fdf6..11a0ec563a89 100644
@@ -15,6 +15,7 @@
 #include <linux/phy.h>
 #include <linux/dim.h>
 #include <linux/ethtool.h>
+#include <net/page_pool/helpers.h>
 
 #include "../unimac.h"
 
@@ -469,6 +470,8 @@ struct bcmgenet_rx_stats64 {
 
 struct enet_cb {
 	struct sk_buff      *skb;
+	struct page         *rx_page;
+	unsigned int        rx_page_offset;
 	void __iomem *bd_addr;
 	DEFINE_DMA_UNMAP_ADDR(dma_addr);
 	DEFINE_DMA_UNMAP_LEN(dma_len);
@@ -575,6 +578,7 @@ struct bcmgenet_rx_ring {
 	struct bcmgenet_net_dim dim;
 	u32		rx_max_coalesced_frames;
 	u32		rx_coalesce_usecs;
+	struct page_pool *page_pool;
 	struct bcmgenet_priv *priv;
 };
 
Signature · 1 line
2.51.0
author maintainer
#4 lore raw

PATCH v8 3/7 net: bcmgenet: add basic XDP support (PASS/DROP)

In reply to Nicolai Buchwitz

Add XDP program attachment via ndo_bpf and execute XDP programs in the RX path. XDP_PASS builds an SKB from the xdp_buff (handling xdp_adjust_head/tail), XDP_DROP returns the page to page_pool without SKB allocation.

XDP_TX and XDP_REDIRECT are not yet supported and return XDP_ABORTED.

Advertise NETDEV_XDP_ACT_BASIC in xdp_features.

---
 .../net/ethernet/broadcom/genet/bcmgenet.c    | 129 +++++++++++++++---
 .../net/ethernet/broadcom/genet/bcmgenet.h    |   4 +
 2 files changed, 116 insertions(+), 17 deletions(-)
drivers/net/ethernet/broadcom/genet/bcmgenet.c +112 -17
index e71d9713f917..1b60571446e1 100644
@@ -35,6 +35,8 @@
 #include <linux/ip.h>
 #include <linux/ipv6.h>
 #include <linux/phy.h>
+#include <linux/bpf_trace.h>
+#include <linux/filter.h>
 
 #include <linux/unaligned.h>
 
@@ -2275,6 +2277,56 @@ static int bcmgenet_rx_refill(struct bcmgenet_rx_ring *ring,
 	return 0;
 }
 
+static struct sk_buff *bcmgenet_xdp_build_skb(struct bcmgenet_rx_ring *ring,
+					      struct xdp_buff *xdp)
+{
+	unsigned int metasize;
+	struct sk_buff *skb;
+
+	skb = napi_build_skb(xdp->data_hard_start, PAGE_SIZE);
+	if (unlikely(!skb))
+		return NULL;
+
+	skb_mark_for_recycle(skb);
+
+	metasize = xdp->data - xdp->data_meta;
+	skb_reserve(skb, xdp->data - xdp->data_hard_start);
+	__skb_put(skb, xdp->data_end - xdp->data);
+
+	if (metasize)
+		skb_metadata_set(skb, metasize);
+
+	return skb;
+}
+
+static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
+				     struct bpf_prog *prog,
+				     struct xdp_buff *xdp,
+				     struct page *rx_page)
+{
+	unsigned int act;
+
+	if (!prog)
+		return XDP_PASS;
+
+	act = bpf_prog_run_xdp(prog, xdp);
+
+	switch (act) {
+	case XDP_PASS:
+		return XDP_PASS;
+	case XDP_DROP:
+		page_pool_put_full_page(ring->page_pool, rx_page, true);
+		return XDP_DROP;
+	default:
+		bpf_warn_invalid_xdp_action(ring->priv->dev, prog, act);
+		fallthrough;
+	case XDP_ABORTED:
+		trace_xdp_exception(ring->priv->dev, prog, act);
+		page_pool_put_full_page(ring->page_pool, rx_page, true);
+		return XDP_ABORTED;
+	}
+}
+
 /* bcmgenet_desc_rx - descriptor based rx process.
  * this could be called from bottom half, or from NAPI polling method.
  */
@@ -2284,6 +2336,7 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 	struct bcmgenet_rx_stats64 *stats = &ring->stats64;
 	struct bcmgenet_priv *priv = ring->priv;
 	struct net_device *dev = priv->dev;
+	struct bpf_prog *xdp_prog;
 	struct enet_cb *cb;
 	struct sk_buff *skb;
 	u32 dma_length_status;
@@ -2294,6 +2347,8 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 	unsigned int p_index, mask;
 	unsigned int discards;
 
+	xdp_prog = READ_ONCE(priv->xdp_prog);
+
 	/* Clear status before servicing to reduce spurious interrupts */
 	mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
 	bcmgenet_intrl2_1_writel(priv, mask, INTRL2_CPU_CLEAR);
@@ -2325,9 +2380,12 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 	       (rxpktprocessed < budget)) {
 		struct status_64 *status;
 		struct page *rx_page;
+		unsigned int xdp_act;
 		unsigned int rx_off;
-		__be16 rx_csum;
+		struct xdp_buff xdp;
+		__be16 rx_csum = 0;
 		void *hard_start;
+		int pkt_len;
 
 		cb = &priv->rx_cbs[ring->read_ptr];
 
@@ -2404,30 +2462,34 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 			goto next;
 		} /* error packet */
 
-		/* Build SKB from the page - data starts at hard_start,
-		 * frame begins after RSB(64) + pad(2) = 66 bytes.
+		pkt_len = len - GENET_RSB_PAD;
+		if (priv->crc_fwd_en)
+			pkt_len -= ETH_FCS_LEN;
+
+		/* Save rx_csum before XDP runs - an XDP program
+		 * could overwrite the RSB via bpf_xdp_adjust_head.
 		 */
-		skb = napi_build_skb(hard_start, PAGE_SIZE - XDP_PACKET_HEADROOM);
-		if (unlikely(!skb)) {
-			BCMGENET_STATS64_INC(stats, dropped);
-			page_pool_put_full_page(ring->page_pool, rx_page,
-						true);
-			goto next;
-		}
+		if (dev->features & NETIF_F_RXCSUM)
+			rx_csum = (__force __be16)(status->rx_csum & 0xffff);
 
-		skb_mark_for_recycle(skb);
+		xdp_init_buff(&xdp, PAGE_SIZE, &ring->xdp_rxq);
+		xdp_prepare_buff(&xdp, page_address(rx_page),
+				 GENET_RX_HEADROOM, pkt_len, true);
 
-		/* Reserve the RSB + pad, then set the data length */
-		skb_reserve(skb, GENET_RSB_PAD);
-		__skb_put(skb, len - GENET_RSB_PAD);
+		xdp_act = bcmgenet_run_xdp(ring, xdp_prog, &xdp, rx_page);
+		if (xdp_act != XDP_PASS)
+			goto next;
 
-		if (priv->crc_fwd_en) {
-			skb_trim(skb, skb->len - ETH_FCS_LEN);
+		skb = bcmgenet_xdp_build_skb(ring, &xdp);
+		if (unlikely(!skb)) {
+			BCMGENET_STATS64_INC(stats, dropped);
+			page_pool_put_full_page(ring->page_pool,
+						rx_page, true);
+			goto next;
 		}
 
 		/* Set up checksum offload */
 		if (dev->features & NETIF_F_RXCSUM) {
-			rx_csum = (__force __be16)(status->rx_csum & 0xffff);
 			if (rx_csum) {
 				skb->csum = (__force __wsum)ntohs(rx_csum);
 				skb->ip_summed = CHECKSUM_COMPLETE;
@@ -3741,6 +3803,37 @@ static int bcmgenet_change_carrier(struct net_device *dev, bool new_carrier)
 	return 0;
 }
 
+static int bcmgenet_xdp_setup(struct net_device *dev,
+			      struct netdev_bpf *xdp)
+{
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+	struct bpf_prog *old_prog;
+	struct bpf_prog *prog = xdp->prog;
+
+	if (prog && dev->mtu > PAGE_SIZE - GENET_RX_HEADROOM -
+	    SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) {
+		NL_SET_ERR_MSG_MOD(xdp->extack,
+				   "MTU too large for single-page XDP buffer");
+		return -EOPNOTSUPP;
+	}
+
+	old_prog = xchg(&priv->xdp_prog, prog);
+	if (old_prog)
+		bpf_prog_put(old_prog);
+
+	return 0;
+}
+
+static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
+{
+	switch (xdp->command) {
+	case XDP_SETUP_PROG:
+		return bcmgenet_xdp_setup(dev, xdp);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
 static const struct net_device_ops bcmgenet_netdev_ops = {
 	.ndo_open		= bcmgenet_open,
 	.ndo_stop		= bcmgenet_close,
@@ -3752,6 +3845,7 @@ static const struct net_device_ops bcmgenet_netdev_ops = {
 	.ndo_set_features	= bcmgenet_set_features,
 	.ndo_get_stats64	= bcmgenet_get_stats64,
 	.ndo_change_carrier	= bcmgenet_change_carrier,
+	.ndo_bpf		= bcmgenet_xdp,
 };
 
 /* GENET hardware parameters/characteristics */
@@ -4054,6 +4148,7 @@ static int bcmgenet_probe(struct platform_device *pdev)
 			 NETIF_F_RXCSUM;
 	dev->hw_features |= dev->features;
 	dev->vlan_features |= dev->features;
+	dev->xdp_features = NETDEV_XDP_ACT_BASIC;
 
 	netdev_sw_irq_coalesce_default_on(dev);
 
drivers/net/ethernet/broadcom/genet/bcmgenet.h +4 -0
index 82a6d29f481d..1459473ac1b0 100644
@@ -16,6 +16,7 @@
 #include <linux/dim.h>
 #include <linux/ethtool.h>
 #include <net/page_pool/helpers.h>
+#include <linux/bpf.h>
 #include <net/xdp.h>
 
 #include "../unimac.h"
@@ -671,6 +672,9 @@ struct bcmgenet_priv {
 	u8 sopass[SOPASS_MAX];
 
 	struct bcmgenet_mib_counters mib;
+
+	/* XDP */
+	struct bpf_prog *xdp_prog;
 };
 
 static inline bool bcmgenet_has_40bits(struct bcmgenet_priv *priv)
Signature · 1 line
2.51.0
author maintainer
#7 lore raw

PATCH v8 4/7 net: bcmgenet: add XDP_TX support

In reply to Nicolai Buchwitz

Implement XDP_TX using ring 16 (DESC_INDEX), the hardware default descriptor ring, dedicated to XDP TX for isolation from SKB TX queues.

Ring 16 gets 32 BDs carved from ring 0's allocation. TX completion is piggybacked on RX NAPI poll since ring 16's INTRL2_1 bit collides with RX ring 0, similar to how bnxt, ice, and other XDP drivers handle TX completion within the RX poll path.

The GENET MAC has TBUF_64B_EN set globally, requiring every TX buffer to start with a 64-byte struct status_64 (TSB). For local XDP_TX, the TSB is prepended by backing xdp->data into the RSB area (unused after BPF execution) and zeroing it. For foreign frames redirected from other devices, the TSB is written into the xdp_frame headroom.

The page_pool DMA direction is changed from DMA_FROM_DEVICE to DMA_BIDIRECTIONAL to allow TX reuse of the existing DMA mapping.

---
 .../net/ethernet/broadcom/genet/bcmgenet.c    | 224 ++++++++++++++++--
 .../net/ethernet/broadcom/genet/bcmgenet.h    |   3 +
 2 files changed, 205 insertions(+), 22 deletions(-)
drivers/net/ethernet/broadcom/genet/bcmgenet.c +202 -22
index 1b60571446e1..3c3b0c44ea8a 100644
@@ -48,8 +48,10 @@
 
 #define GENET_Q0_RX_BD_CNT	\
 	(TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
+#define GENET_Q16_TX_BD_CNT	32
 #define GENET_Q0_TX_BD_CNT	\
-	(TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
+	(TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q \
+	 - GENET_Q16_TX_BD_CNT)
 
 #define RX_BUF_LENGTH		2048
 #define SKB_ALIGNMENT		32
@@ -1892,6 +1894,14 @@ static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
 		if (cb == GENET_CB(skb)->last_cb)
 			return skb;
 
+	} else if (cb->xdpf) {
+		if (cb->xdp_dma_map)
+			dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
+					 dma_unmap_len(cb, dma_len),
+					 DMA_TO_DEVICE);
+		dma_unmap_addr_set(cb, dma_addr, 0);
+		xdp_return_frame(cb->xdpf);
+		cb->xdpf = NULL;
 	} else if (dma_unmap_addr(cb, dma_addr)) {
 		dma_unmap_page(dev,
 			       dma_unmap_addr(cb, dma_addr),
@@ -1924,10 +1934,16 @@ static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
 	unsigned int pkts_compl = 0;
 	unsigned int txbds_ready;
 	unsigned int c_index;
+	struct enet_cb *tx_cb;
 	struct sk_buff *skb;
 
-	/* Clear status before servicing to reduce spurious interrupts */
-	bcmgenet_intrl2_1_writel(priv, (1 << ring->index), INTRL2_CPU_CLEAR);
+	/* Clear status before servicing to reduce spurious interrupts.
+	 * Ring DESC_INDEX (XDP TX) has no interrupt; skip the clear to
+	 * avoid clobbering RX ring 0's bit at the same position.
+	 */
+	if (ring->index != DESC_INDEX)
+		bcmgenet_intrl2_1_writel(priv, BIT(ring->index),
+					 INTRL2_CPU_CLEAR);
 
 	/* Compute how many buffers are transmitted since last xmit call */
 	c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
@@ -1940,8 +1956,15 @@ static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
 
 	/* Reclaim transmitted buffers */
 	while (txbds_processed < txbds_ready) {
-		skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
-					  &priv->tx_cbs[ring->clean_ptr]);
+		tx_cb = &priv->tx_cbs[ring->clean_ptr];
+		if (tx_cb->xdpf) {
+			pkts_compl++;
+			bytes_compl += tx_cb->xdp_dma_map
+				? tx_cb->xdpf->len
+				: tx_cb->xdpf->len -
+				  sizeof(struct status_64);
+		}
+		skb = bcmgenet_free_tx_cb(&priv->pdev->dev, tx_cb);
 		if (skb) {
 			pkts_compl++;
 			bytes_compl += GENET_CB(skb)->bytes_sent;
@@ -1963,8 +1986,11 @@ static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
 	u64_stats_add(&stats->bytes, bytes_compl);
 	u64_stats_update_end(&stats->syncp);
 
-	netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->index),
-				  pkts_compl, bytes_compl);
+	/* Ring DESC_INDEX (XDP TX) has no netdev TX queue; skip BQL */
+	if (ring->index != DESC_INDEX)
+		netdev_tx_completed_queue(netdev_get_tx_queue(dev,
+							      ring->index),
+					  pkts_compl, bytes_compl);
 
 	return txbds_processed;
 }
@@ -2043,6 +2069,9 @@ static void bcmgenet_tx_reclaim_all(struct net_device *dev)
 	do {
 		bcmgenet_tx_reclaim(dev, &priv->tx_rings[i++], true);
 	} while (i <= priv->hw_params->tx_queues && netif_is_multiqueue(dev));
+
+	/* Also reclaim XDP TX ring */
+	bcmgenet_tx_reclaim(dev, &priv->xdp_tx_ring, true);
 }
 
 /* Reallocate the SKB to put enough headroom in front of it and insert
@@ -2299,11 +2328,96 @@ static struct sk_buff *bcmgenet_xdp_build_skb(struct bcmgenet_rx_ring *ring,
 	return skb;
 }
 
+static bool bcmgenet_xdp_xmit_frame(struct bcmgenet_priv *priv,
+				     struct xdp_frame *xdpf, bool dma_map)
+{
+	struct bcmgenet_tx_ring *ring = &priv->xdp_tx_ring;
+	struct device *kdev = &priv->pdev->dev;
+	struct enet_cb *tx_cb_ptr;
+	dma_addr_t mapping;
+	unsigned int dma_len;
+	u32 len_stat;
+
+	spin_lock(&ring->lock);
+
+	if (ring->free_bds < 1) {
+		spin_unlock(&ring->lock);
+		return false;
+	}
+
+	tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
+
+	if (dma_map) {
+		void *tsb_start;
+
+		/* The GENET MAC has TBUF_64B_EN set globally, so hardware
+		 * expects a 64-byte TSB prefix on every TX buffer.  For
+		 * redirected frames (ndo_xdp_xmit) we prepend a zeroed TSB
+		 * using the frame's headroom.
+		 */
+		if (unlikely(xdpf->headroom < sizeof(struct status_64))) {
+			bcmgenet_put_txcb(priv, ring);
+			spin_unlock(&ring->lock);
+			return false;
+		}
+
+		tsb_start = xdpf->data - sizeof(struct status_64);
+		memset(tsb_start, 0, sizeof(struct status_64));
+
+		dma_len = xdpf->len + sizeof(struct status_64);
+		mapping = dma_map_single(kdev, tsb_start, dma_len,
+					 DMA_TO_DEVICE);
+		if (dma_mapping_error(kdev, mapping)) {
+			tx_cb_ptr->skb = NULL;
+			tx_cb_ptr->xdpf = NULL;
+			bcmgenet_put_txcb(priv, ring);
+			spin_unlock(&ring->lock);
+			return false;
+		}
+	} else {
+		struct page *page = virt_to_page(xdpf->data);
+
+		/* For local XDP_TX the caller already prepended the TSB
+		 * into xdpf->data/len, so dma_len == xdpf->len.
+		 */
+		dma_len = xdpf->len;
+		mapping = page_pool_get_dma_addr(page) +
+			  sizeof(*xdpf) + xdpf->headroom;
+		dma_sync_single_for_device(kdev, mapping, dma_len,
+					   DMA_BIDIRECTIONAL);
+	}
+
+	dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
+	dma_unmap_len_set(tx_cb_ptr, dma_len, dma_len);
+	tx_cb_ptr->skb = NULL;
+	tx_cb_ptr->xdpf = xdpf;
+	tx_cb_ptr->xdp_dma_map = dma_map;
+
+	len_stat = (dma_len << DMA_BUFLENGTH_SHIFT) |
+		   (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
+		   DMA_TX_APPEND_CRC | DMA_SOP | DMA_EOP;
+
+	dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
+
+	ring->free_bds--;
+	ring->prod_index++;
+	ring->prod_index &= DMA_P_INDEX_MASK;
+
+	bcmgenet_tdma_ring_writel(priv, ring->index, ring->prod_index,
+				  TDMA_PROD_INDEX);
+
+	spin_unlock(&ring->lock);
+
+	return true;
+}
+
 static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 				     struct bpf_prog *prog,
 				     struct xdp_buff *xdp,
 				     struct page *rx_page)
 {
+	struct bcmgenet_priv *priv = ring->priv;
+	struct xdp_frame *xdpf;
 	unsigned int act;
 
 	if (!prog)
@@ -2314,14 +2428,42 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 	switch (act) {
 	case XDP_PASS:
 		return XDP_PASS;
+	case XDP_TX:
+		/* Prepend a zeroed TSB (Transmit Status Block).  The GENET
+		 * MAC has TBUF_64B_EN set globally, so hardware expects every
+		 * TX buffer to begin with a 64-byte struct status_64.  Back
+		 * up xdp->data into the RSB area (which is no longer needed
+		 * after the BPF program ran) and zero it.
+		 */
+		if (xdp->data - xdp->data_hard_start <
+		    sizeof(struct status_64) + sizeof(struct xdp_frame)) {
+			page_pool_put_full_page(ring->page_pool, rx_page,
+						true);
+			return XDP_DROP;
+		}
+		xdp->data -= sizeof(struct status_64);
+		xdp->data_meta -= sizeof(struct status_64);
+		memset(xdp->data, 0, sizeof(struct status_64));
+
+		xdpf = xdp_convert_buff_to_frame(xdp);
+		if (unlikely(!xdpf)) {
+			page_pool_put_full_page(ring->page_pool, rx_page,
+						true);
+			return XDP_DROP;
+		}
+		if (unlikely(!bcmgenet_xdp_xmit_frame(priv, xdpf, false))) {
+			xdp_return_frame_rx_napi(xdpf);
+			return XDP_DROP;
+		}
+		return XDP_TX;
 	case XDP_DROP:
 		page_pool_put_full_page(ring->page_pool, rx_page, true);
 		return XDP_DROP;
 	default:
-		bpf_warn_invalid_xdp_action(ring->priv->dev, prog, act);
+		bpf_warn_invalid_xdp_action(priv->dev, prog, act);
 		fallthrough;
 	case XDP_ABORTED:
-		trace_xdp_exception(ring->priv->dev, prog, act);
+		trace_xdp_exception(priv->dev, prog, act);
 		page_pool_put_full_page(ring->page_pool, rx_page, true);
 		return XDP_ABORTED;
 	}
@@ -2539,9 +2681,15 @@ static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
 {
 	struct bcmgenet_rx_ring *ring = container_of(napi,
 			struct bcmgenet_rx_ring, napi);
+	struct bcmgenet_priv *priv = ring->priv;
 	struct dim_sample dim_sample = {};
 	unsigned int work_done;
 
+	/* Reclaim completed XDP TX frames (ring 16 has no interrupt) */
+	if (priv->xdp_tx_ring.free_bds < priv->xdp_tx_ring.size)
+		bcmgenet_tx_reclaim(priv->dev,
+				    &priv->xdp_tx_ring, false);
+
 	work_done = bcmgenet_desc_rx(ring, budget);
 
 	if (work_done < budget && napi_complete_done(napi, work_done))
@@ -2772,10 +2920,11 @@ static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
 
 /* Initialize a Tx ring along with corresponding hardware registers */
 static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
+				  struct bcmgenet_tx_ring *ring,
 				  unsigned int index, unsigned int size,
-				  unsigned int start_ptr, unsigned int end_ptr)
+				  unsigned int start_ptr,
+				  unsigned int end_ptr)
 {
-	struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
 	u32 words_per_bd = WORDS_PER_BD(priv);
 	u32 flow_period_val = 0;
 
@@ -2816,8 +2965,11 @@ static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
 	bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
 				  DMA_END_ADDR);
 
-	/* Initialize Tx NAPI */
-	netif_napi_add_tx(priv->dev, &ring->napi, bcmgenet_tx_poll);
+	/* Initialize Tx NAPI for priority queues only; ring DESC_INDEX
+	 * (XDP TX) has its completions handled inline in RX NAPI.
+	 */
+	if (index != DESC_INDEX)
+		netif_napi_add_tx(priv->dev, &ring->napi, bcmgenet_tx_poll);
 }
 
 static int bcmgenet_rx_ring_create_pool(struct bcmgenet_priv *priv,
@@ -2829,7 +2981,7 @@ static int bcmgenet_rx_ring_create_pool(struct bcmgenet_priv *priv,
 		.pool_size = ring->size,
 		.nid = NUMA_NO_NODE,
 		.dev = &priv->pdev->dev,
-		.dma_dir = DMA_FROM_DEVICE,
+		.dma_dir = DMA_BIDIRECTIONAL,
 		.offset = XDP_PACKET_HEADROOM,
 		.max_len = RX_BUF_LENGTH,
 	};
@@ -2963,6 +3115,7 @@ static int bcmgenet_tdma_disable(struct bcmgenet_priv *priv)
 
 	reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
 	mask = (1 << (priv->hw_params->tx_queues + 1)) - 1;
+	mask |= BIT(DESC_INDEX);
 	mask = (mask << DMA_RING_BUF_EN_SHIFT) | DMA_EN;
 	reg &= ~mask;
 	bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
@@ -3008,14 +3161,18 @@ static int bcmgenet_rdma_disable(struct bcmgenet_priv *priv)
  * with queue 1 being the highest priority queue.
  *
  * Queue 0 is the default Tx queue with
- * GENET_Q0_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
+ * GENET_Q0_TX_BD_CNT = 256 - 4 * 32 - 32 = 96 descriptors.
+ *
+ * Ring 16 (DESC_INDEX) is used for XDP TX with
+ * GENET_Q16_TX_BD_CNT = 32 descriptors.
  *
  * The transmit control block pool is then partitioned as follows:
- * - Tx queue 0 uses tx_cbs[0..127]
- * - Tx queue 1 uses tx_cbs[128..159]
- * - Tx queue 2 uses tx_cbs[160..191]
- * - Tx queue 3 uses tx_cbs[192..223]
- * - Tx queue 4 uses tx_cbs[224..255]
+ * - Tx queue 0 uses tx_cbs[0..95]
+ * - Tx queue 1 uses tx_cbs[96..127]
+ * - Tx queue 2 uses tx_cbs[128..159]
+ * - Tx queue 3 uses tx_cbs[160..191]
+ * - Tx queue 4 uses tx_cbs[192..223]
+ * - Tx queue 16 uses tx_cbs[224..255]
  */
 static void bcmgenet_init_tx_queues(struct net_device *dev)
 {
@@ -3028,7 +3185,8 @@ static void bcmgenet_init_tx_queues(struct net_device *dev)
 
 	/* Initialize Tx priority queues */
 	for (i = 0; i <= priv->hw_params->tx_queues; i++) {
-		bcmgenet_init_tx_ring(priv, i, end - start, start, end);
+		bcmgenet_init_tx_ring(priv, &priv->tx_rings[i],
+				      i, end - start, start, end);
 		start = end;
 		end += priv->hw_params->tx_bds_per_q;
 		dma_priority[DMA_PRIO_REG_INDEX(i)] |=
@@ -3036,13 +3194,19 @@ static void bcmgenet_init_tx_queues(struct net_device *dev)
 			<< DMA_PRIO_REG_SHIFT(i);
 	}
 
+	/* Initialize ring 16 (descriptor ring) for XDP TX */
+	bcmgenet_init_tx_ring(priv, &priv->xdp_tx_ring,
+			      DESC_INDEX, GENET_Q16_TX_BD_CNT,
+			      TOTAL_DESC - GENET_Q16_TX_BD_CNT, TOTAL_DESC);
+
 	/* Set Tx queue priorities */
 	bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
 	bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
 	bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
 
-	/* Configure Tx queues as descriptor rings */
+	/* Configure Tx queues as descriptor rings, including ring 16 */
 	ring_mask = (1 << (priv->hw_params->tx_queues + 1)) - 1;
+	ring_mask |= BIT(DESC_INDEX);
 	bcmgenet_tdma_writel(priv, ring_mask, DMA_RING_CFG);
 
 	/* Enable Tx rings */
@@ -3752,6 +3916,21 @@ static void bcmgenet_get_stats64(struct net_device *dev,
 		stats->tx_dropped += tx_dropped;
 	}
 
+	/* Include XDP TX ring (DESC_INDEX) stats */
+	tx_stats = &priv->xdp_tx_ring.stats64;
+	do {
+		start = u64_stats_fetch_begin(&tx_stats->syncp);
+		tx_bytes = u64_stats_read(&tx_stats->bytes);
+		tx_packets = u64_stats_read(&tx_stats->packets);
+		tx_errors = u64_stats_read(&tx_stats->errors);
+		tx_dropped = u64_stats_read(&tx_stats->dropped);
+	} while (u64_stats_fetch_retry(&tx_stats->syncp, start));
+
+	stats->tx_bytes += tx_bytes;
+	stats->tx_packets += tx_packets;
+	stats->tx_errors += tx_errors;
+	stats->tx_dropped += tx_dropped;
+
 	for (q = 0; q <= priv->hw_params->rx_queues; q++) {
 		rx_stats = &priv->rx_rings[q].stats64;
 		do {
@@ -4255,6 +4434,7 @@ static int bcmgenet_probe(struct platform_device *pdev)
 		u64_stats_init(&priv->rx_rings[i].stats64.syncp);
 	for (i = 0; i <= priv->hw_params->tx_queues; i++)
 		u64_stats_init(&priv->tx_rings[i].stats64.syncp);
+	u64_stats_init(&priv->xdp_tx_ring.stats64.syncp);
 
 	/* libphy will determine the link state */
 	netif_carrier_off(dev);
drivers/net/ethernet/broadcom/genet/bcmgenet.h +3 -0
index 1459473ac1b0..8966d32efe2f 100644
@@ -472,6 +472,8 @@ struct bcmgenet_rx_stats64 {
 
 struct enet_cb {
 	struct sk_buff      *skb;
+	struct xdp_frame    *xdpf;
+	bool                xdp_dma_map;
 	struct page         *rx_page;
 	unsigned int        rx_page_offset;
 	void __iomem *bd_addr;
@@ -611,6 +613,7 @@ struct bcmgenet_priv {
 	unsigned int num_tx_bds;
 
 	struct bcmgenet_tx_ring tx_rings[GENET_MAX_MQ_CNT + 1];
+	struct bcmgenet_tx_ring xdp_tx_ring;
 
 	/* receive variables */
 	void __iomem *rx_bds;
Signature · 1 line
2.51.0
author maintainer
#11 lore raw

PATCH v8 5/7 net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support

In reply to Nicolai Buchwitz

Add XDP_REDIRECT support and implement ndo_xdp_xmit for receiving redirected frames from other devices.

XDP_REDIRECT calls xdp_do_redirect() in the RX path with xdp_do_flush() once per NAPI poll cycle. ndo_xdp_xmit batches frames into ring 16 under a single spinlock acquisition.

Advertise NETDEV_XDP_ACT_REDIRECT and NETDEV_XDP_ACT_NDO_XMIT in xdp_features.

---
 .../net/ethernet/broadcom/genet/bcmgenet.c    | 87 ++++++++++++++++---
 1 file changed, 73 insertions(+), 14 deletions(-)
drivers/net/ethernet/broadcom/genet/bcmgenet.c +73 -14
index 3c3b0c44ea8a..9dd258567824 100644
@@ -2328,22 +2328,22 @@ static struct sk_buff *bcmgenet_xdp_build_skb(struct bcmgenet_rx_ring *ring,
 	return skb;
 }
 
+/* Submit a single XDP frame to the TX ring. Caller must hold ring->lock.
+ * Returns true on success. Does not ring the doorbell - caller must
+ * write TDMA_PROD_INDEX after batching.
+ */
 static bool bcmgenet_xdp_xmit_frame(struct bcmgenet_priv *priv,
+				     struct bcmgenet_tx_ring *ring,
 				     struct xdp_frame *xdpf, bool dma_map)
 {
-	struct bcmgenet_tx_ring *ring = &priv->xdp_tx_ring;
 	struct device *kdev = &priv->pdev->dev;
 	struct enet_cb *tx_cb_ptr;
 	dma_addr_t mapping;
 	unsigned int dma_len;
 	u32 len_stat;
 
-	spin_lock(&ring->lock);
-
-	if (ring->free_bds < 1) {
-		spin_unlock(&ring->lock);
+	if (ring->free_bds < 1)
 		return false;
-	}
 
 	tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
 
@@ -2357,7 +2357,6 @@ static bool bcmgenet_xdp_xmit_frame(struct bcmgenet_priv *priv,
 		 */
 		if (unlikely(xdpf->headroom < sizeof(struct status_64))) {
 			bcmgenet_put_txcb(priv, ring);
-			spin_unlock(&ring->lock);
 			return false;
 		}
 
@@ -2371,7 +2370,6 @@ static bool bcmgenet_xdp_xmit_frame(struct bcmgenet_priv *priv,
 			tx_cb_ptr->skb = NULL;
 			tx_cb_ptr->xdpf = NULL;
 			bcmgenet_put_txcb(priv, ring);
-			spin_unlock(&ring->lock);
 			return false;
 		}
 	} else {
@@ -2403,12 +2401,14 @@ static bool bcmgenet_xdp_xmit_frame(struct bcmgenet_priv *priv,
 	ring->prod_index++;
 	ring->prod_index &= DMA_P_INDEX_MASK;
 
+	return true;
+}
+
+static void bcmgenet_xdp_ring_doorbell(struct bcmgenet_priv *priv,
+				       struct bcmgenet_tx_ring *ring)
+{
 	bcmgenet_tdma_ring_writel(priv, ring->index, ring->prod_index,
 				  TDMA_PROD_INDEX);
-
-	spin_unlock(&ring->lock);
-
-	return true;
 }
 
 static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
@@ -2417,6 +2417,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 				     struct page *rx_page)
 {
 	struct bcmgenet_priv *priv = ring->priv;
+	struct bcmgenet_tx_ring *tx_ring;
 	struct xdp_frame *xdpf;
 	unsigned int act;
 
@@ -2451,11 +2452,25 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 						true);
 			return XDP_DROP;
 		}
-		if (unlikely(!bcmgenet_xdp_xmit_frame(priv, xdpf, false))) {
+
+		tx_ring = &priv->xdp_tx_ring;
+		spin_lock(&tx_ring->lock);
+		if (unlikely(!bcmgenet_xdp_xmit_frame(priv, tx_ring,
+						      xdpf, false))) {
+			spin_unlock(&tx_ring->lock);
 			xdp_return_frame_rx_napi(xdpf);
 			return XDP_DROP;
 		}
+		bcmgenet_xdp_ring_doorbell(priv, tx_ring);
+		spin_unlock(&tx_ring->lock);
 		return XDP_TX;
+	case XDP_REDIRECT:
+		if (unlikely(xdp_do_redirect(priv->dev, xdp, prog))) {
+			page_pool_put_full_page(ring->page_pool, rx_page,
+						true);
+			return XDP_DROP;
+		}
+		return XDP_REDIRECT;
 	case XDP_DROP:
 		page_pool_put_full_page(ring->page_pool, rx_page, true);
 		return XDP_DROP;
@@ -2479,6 +2494,7 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 	struct bcmgenet_priv *priv = ring->priv;
 	struct net_device *dev = priv->dev;
 	struct bpf_prog *xdp_prog;
+	bool xdp_flush = false;
 	struct enet_cb *cb;
 	struct sk_buff *skb;
 	u32 dma_length_status;
@@ -2619,6 +2635,8 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 				 GENET_RX_HEADROOM, pkt_len, true);
 
 		xdp_act = bcmgenet_run_xdp(ring, xdp_prog, &xdp, rx_page);
+		if (xdp_act == XDP_REDIRECT)
+			xdp_flush = true;
 		if (xdp_act != XDP_PASS)
 			goto next;
 
@@ -2670,6 +2688,9 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 		bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
 	}
 
+	if (xdp_flush)
+		xdp_do_flush();
+
 	ring->dim.bytes = bytes_processed;
 	ring->dim.packets = rxpktprocessed;
 
@@ -3996,10 +4017,16 @@ static int bcmgenet_xdp_setup(struct net_device *dev,
 		return -EOPNOTSUPP;
 	}
 
+	if (!prog)
+		xdp_features_clear_redirect_target(dev);
+
 	old_prog = xchg(&priv->xdp_prog, prog);
 	if (old_prog)
 		bpf_prog_put(old_prog);
 
+	if (prog)
+		xdp_features_set_redirect_target(dev, false);
+
 	return 0;
 }
 
@@ -4013,6 +4040,36 @@ static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
 	}
 }
 
+static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames,
+			     struct xdp_frame **frames, u32 flags)
+{
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+	struct bcmgenet_tx_ring *ring = &priv->xdp_tx_ring;
+	int sent = 0;
+	int i;
+
+	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
+		return -EINVAL;
+
+	if (unlikely(!netif_running(dev)))
+		return -ENETDOWN;
+
+	spin_lock(&ring->lock);
+
+	for (i = 0; i < num_frames; i++) {
+		if (!bcmgenet_xdp_xmit_frame(priv, ring, frames[i], true))
+			break;
+		sent++;
+	}
+
+	if (sent)
+		bcmgenet_xdp_ring_doorbell(priv, ring);
+
+	spin_unlock(&ring->lock);
+
+	return sent;
+}
+
 static const struct net_device_ops bcmgenet_netdev_ops = {
 	.ndo_open		= bcmgenet_open,
 	.ndo_stop		= bcmgenet_close,
@@ -4025,6 +4082,7 @@ static const struct net_device_ops bcmgenet_netdev_ops = {
 	.ndo_get_stats64	= bcmgenet_get_stats64,
 	.ndo_change_carrier	= bcmgenet_change_carrier,
 	.ndo_bpf		= bcmgenet_xdp,
+	.ndo_xdp_xmit		= bcmgenet_xdp_xmit,
 };
 
 /* GENET hardware parameters/characteristics */
@@ -4327,7 +4385,8 @@ static int bcmgenet_probe(struct platform_device *pdev)
 			 NETIF_F_RXCSUM;
 	dev->hw_features |= dev->features;
 	dev->vlan_features |= dev->features;
-	dev->xdp_features = NETDEV_XDP_ACT_BASIC;
+	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
+			    NETDEV_XDP_ACT_NDO_XMIT;
 
 	netdev_sw_irq_coalesce_default_on(dev);
 
Signature · 1 line
2.51.0
author maintainer
#15 lore raw

PATCH v8 6/7 net: bcmgenet: add XDP statistics counters

In reply to Nicolai Buchwitz

Expose per-action XDP counters via ethtool -S: xdp_pass, xdp_drop, xdp_tx, xdp_tx_err, xdp_redirect, and xdp_redirect_err.

These use the existing soft MIB infrastructure and are incremented in bcmgenet_run_xdp() alongside the existing driver statistics.

---
 drivers/net/ethernet/broadcom/genet/bcmgenet.c | 15 +++++++++++++++
 drivers/net/ethernet/broadcom/genet/bcmgenet.h |  6 ++++++
 2 files changed, 21 insertions(+)
drivers/net/ethernet/broadcom/genet/bcmgenet.c +15 -0
index 9dd258567824..02ad2f410d6c 100644
@@ -1169,6 +1169,13 @@ static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
 	STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb),
 	STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed",
 			    mib.tx_realloc_tsb_failed),
+	/* XDP counters */
+	STAT_GENET_SOFT_MIB("xdp_pass", mib.xdp_pass),
+	STAT_GENET_SOFT_MIB("xdp_drop", mib.xdp_drop),
+	STAT_GENET_SOFT_MIB("xdp_tx", mib.xdp_tx),
+	STAT_GENET_SOFT_MIB("xdp_tx_err", mib.xdp_tx_err),
+	STAT_GENET_SOFT_MIB("xdp_redirect", mib.xdp_redirect),
+	STAT_GENET_SOFT_MIB("xdp_redirect_err", mib.xdp_redirect_err),
 	/* Per TX queues */
 	STAT_GENET_Q(0),
 	STAT_GENET_Q(1),
@@ -2428,6 +2435,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 
 	switch (act) {
 	case XDP_PASS:
+		priv->mib.xdp_pass++;
 		return XDP_PASS;
 	case XDP_TX:
 		/* Prepend a zeroed TSB (Transmit Status Block).  The GENET
@@ -2440,6 +2448,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 		    sizeof(struct status_64) + sizeof(struct xdp_frame)) {
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
+			priv->mib.xdp_tx_err++;
 			return XDP_DROP;
 		}
 		xdp->data -= sizeof(struct status_64);
@@ -2459,19 +2468,24 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 						      xdpf, false))) {
 			spin_unlock(&tx_ring->lock);
 			xdp_return_frame_rx_napi(xdpf);
+			priv->mib.xdp_tx_err++;
 			return XDP_DROP;
 		}
 		bcmgenet_xdp_ring_doorbell(priv, tx_ring);
 		spin_unlock(&tx_ring->lock);
+		priv->mib.xdp_tx++;
 		return XDP_TX;
 	case XDP_REDIRECT:
 		if (unlikely(xdp_do_redirect(priv->dev, xdp, prog))) {
+			priv->mib.xdp_redirect_err++;
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
 			return XDP_DROP;
 		}
+		priv->mib.xdp_redirect++;
 		return XDP_REDIRECT;
 	case XDP_DROP:
+		priv->mib.xdp_drop++;
 		page_pool_put_full_page(ring->page_pool, rx_page, true);
 		return XDP_DROP;
 	default:
@@ -2479,6 +2493,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 		fallthrough;
 	case XDP_ABORTED:
 		trace_xdp_exception(priv->dev, prog, act);
+		priv->mib.xdp_drop++;
 		page_pool_put_full_page(ring->page_pool, rx_page, true);
 		return XDP_ABORTED;
 	}
drivers/net/ethernet/broadcom/genet/bcmgenet.h +6 -0
index 8966d32efe2f..c4e85c185702 100644
@@ -156,6 +156,12 @@ struct bcmgenet_mib_counters {
 	u32	tx_dma_failed;
 	u32	tx_realloc_tsb;
 	u32	tx_realloc_tsb_failed;
+	u32	xdp_pass;
+	u32	xdp_drop;
+	u32	xdp_tx;
+	u32	xdp_tx_err;
+	u32	xdp_redirect;
+	u32	xdp_redirect_err;
 };
 
 struct bcmgenet_tx_stats64 {
Signature · 1 line
2.51.0
author maintainer
#19 lore raw

PATCH v8 7/7 net: bcmgenet: reject MTU changes incompatible with XDP

In reply to Nicolai Buchwitz

Add a minimal ndo_change_mtu that rejects MTU values too large for single-page XDP buffers when an XDP program is attached. Without this, users could change the MTU at runtime and break the XDP buffer layout.

When no XDP program is attached, any MTU change is accepted, matching the existing behavior without ndo_change_mtu.

---
 drivers/net/ethernet/broadcom/genet/bcmgenet.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/net/ethernet/broadcom/genet/bcmgenet.c +15 -0
index 02ad2f410d6c..4d1ec68ec0c5 100644
@@ -4085,6 +4085,20 @@ static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames,
 	return sent;
 }
 
+static int bcmgenet_change_mtu(struct net_device *dev, int new_mtu)
+{
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+
+	if (priv->xdp_prog && new_mtu > PAGE_SIZE - GENET_RX_HEADROOM -
+	    SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) {
+		netdev_warn(dev, "MTU too large for single-page XDP buffer\n");
+		return -EINVAL;
+	}
+
+	WRITE_ONCE(dev->mtu, new_mtu);
+	return 0;
+}
+
 static const struct net_device_ops bcmgenet_netdev_ops = {
 	.ndo_open		= bcmgenet_open,
 	.ndo_stop		= bcmgenet_close,
@@ -4095,6 +4109,7 @@ static const struct net_device_ops bcmgenet_netdev_ops = {
 	.ndo_eth_ioctl		= phy_do_ioctl_running,
 	.ndo_set_features	= bcmgenet_set_features,
 	.ndo_get_stats64	= bcmgenet_get_stats64,
+	.ndo_change_mtu		= bcmgenet_change_mtu,
 	.ndo_change_carrier	= bcmgenet_change_carrier,
 	.ndo_bpf		= bcmgenet_xdp,
 	.ndo_xdp_xmit		= bcmgenet_xdp_xmit,
Signature · 1 line
2.51.0
#5 lore raw

Re: [PATCH net-next v8 3/7] net: bcmgenet: add basic XDP support (PASS/DROP)

In reply to Nicolai Buchwitz
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Hardware checksum offload (CHECKSUM_COMPLETE) is unconditionally applied after XDP execution…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Hardware checksum offload (CHECKSUM_COMPLETE) is unconditionally applied after XDP execution, causing valid modified packets to be dropped.
- [Medium] The driver advertises NETDEV_XDP_ACT_BASIC without supporting XDP_TX, violating the XDP feature API contract.
--

commit fcb14e148a380096868da445b34bcc00893540bf
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add basic XDP support (PASS/DROP)

This commit adds basic XDP support to the bcmgenet driver, allowing XDP programs to execute in the RX path with support for XDP_PASS and XDP_DROP.

Quoted 3 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0

[ ... ]

Quoted 1 line
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
@@ -2404,30 +2462,34 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,

[ ... ]

Quoted 19 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +15 -0
+		/* Save rx_csum before XDP runs - an XDP program
+		 * could overwrite the RSB via bpf_xdp_adjust_head.
 		 */
+		if (dev->features & NETIF_F_RXCSUM)
+			rx_csum = (__force __be16)(status->rx_csum & 0xffff);
 
+		xdp_init_buff(&xdp, PAGE_SIZE, &ring->xdp_rxq);
+		xdp_prepare_buff(&xdp, page_address(rx_page),
+				 GENET_RX_HEADROOM, pkt_len, true);
 
+		xdp_act = bcmgenet_run_xdp(ring, xdp_prog, &xdp, rx_page);
+		if (xdp_act != XDP_PASS)
+			goto next;
 
+		skb = bcmgenet_xdp_build_skb(ring, &xdp);
+		if (unlikely(!skb)) {
+			BCMGENET_STATS64_INC(stats, dropped);
+			page_pool_put_full_page(ring->page_pool,
+						rx_page, true);

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -1
+			goto next;
 		}
 
 		/* Set up checksum offload */
 		if (dev->features & NETIF_F_RXCSUM) {
-			rx_csum = (__force __be16)(status->rx_csum & 0xffff);
 			if (rx_csum) {
 				skb->csum = (__force __wsum)ntohs(rx_csum);
 				skb->ip_summed = CHECKSUM_COMPLETE;
 			}

If the XDP program modifies the packet payload (e.g., rewriting IPs or ports), the hardware checksum saved before XDP execution will no longer match the new payload.

If we unconditionally apply this old checksum and set CHECKSUM_COMPLETE on the SKB here, will the network stack drop the valid modified packet due to a hardware checksum failure?

Should the checksum be invalidated when an XDP program is present?

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_probe(struct platform_device *pdev) · line 4153

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
@@ -4054,6 +4148,7 @@ static int bcmgenet_probe(struct platform_device *pdev)
 			 NETIF_F_RXCSUM;
 	dev->hw_features |= dev->features;
 	dev->vlan_features |= dev->features;
+	dev->xdp_features = NETDEV_XDP_ACT_BASIC;
 
 	netdev_sw_irq_coalesce_default_on(dev);

The commit message notes that XDP_TX is not yet supported and returns XDP_ABORTED.

The UAPI definition for NETDEV_XDP_ACT_BASIC implies support for XDP_PASS, XDP_DROP, XDP_ABORTED, and XDP_TX. Does advertising this flag without XDP_TX support break feature detection for userspace applications?

I see from the patch series that XDP_TX support is added in a later commit. Would it be safer to defer setting NETDEV_XDP_ACT_BASIC until that patch to avoid breaking userspace assumptions during a bisect?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#8 lore raw

Re: [PATCH net-next v8 4/7] net: bcmgenet: add XDP_TX support

In reply to Nicolai Buchwitz
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Out-of-bounds array access in `bcmgenet_tx_reclaim` during interface teardown - [High] Incorrect…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Out-of-bounds array access in bcmgenet_tx_reclaim during interface teardown
- [High] Incorrect DMA address calculation for local XDP_TX causes information leak and packet corruption
- [High] Prepending status_64 block overwrites XDP metadata and causes memory corruption
- [Medium] XDP TX ring size is smaller than standard NAPI budget
--

commit 9add891fa4e7cf62ec26683116338b5877c21662
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add XDP_TX support

Implement XDP_TX using ring 16 (DESC_INDEX), the hardware default descriptor ring, dedicated to XDP TX for isolation from SKB TX queues.

drivers/net/ethernet/broadcom/genet/bcmgenet.c line 51

Quoted 9 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
index 1b60571446e1..3c3b0c44ea8a 100644
@@ -48,8 +48,10 @@
 
 #define GENET_Q0_RX_BD_CNT	\
 	(TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
+#define GENET_Q16_TX_BD_CNT	32

Is it possible to drop packets here during bursts since this TX ring is smaller than the standard NAPI budget?

The standard RX NAPI budget is 64, and TX completions are reclaimed only once before the RX loop. If the RX loop processes a full budget of 64 packets and they all result in an XDP_TX action, the first 32 will fill the TX ring.

The remaining 32 packets would fail in bcmgenet_xdp_xmit_frame() because ring->free_bds < 1, and be dropped.

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static void bcmgenet_tx_reclaim_all(struct net_device *dev) · line 2075

Quoted 8 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
@@ -2043,6 +2069,9 @@ static void bcmgenet_tx_reclaim_all(struct net_device *dev)
 	do {
 		bcmgenet_tx_reclaim(dev, &priv->tx_rings[i++], true);
 	} while (i <= priv->hw_params->tx_queues && netif_is_multiqueue(dev));
+
+	/* Also reclaim XDP TX ring */
+	bcmgenet_tx_reclaim(dev, &priv->xdp_tx_ring, true);
 }

Can this cause an out-of-bounds array access during interface teardown?

When bcmgenet_tx_reclaim_all() calls bcmgenet_tx_reclaim() with all == true, it unconditionally calls netdev_tx_reset_queue(netdev_get_tx_queue(dev, ring->index)).

The XDP TX ring has an index of 16 (DESC_INDEX), but the network device is allocated with only GENET_MAX_MQ_CNT + 1 TX queues (typically 5). Accessing index 16 of dev->_tx is out-of-bounds, and netdev_tx_reset_queue() will write to this invalid memory.

[ ... ]

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
@@ -2299,11 +2328,96 @@ static struct sk_buff *bcmgenet_xdp_build_skb(struct bcmgenet_rx_ring *ring,
 	return skb;
 }
 
+static bool bcmgenet_xdp_xmit_frame(struct bcmgenet_priv *priv,
+				     struct xdp_frame *xdpf, bool dma_map)
+{

[ ... ]

Quoted 1 line
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
+	if (dma_map) {

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static struct sk_buff *bcmgenet_xdp_build_skb(struct bcmgenet_rx_ring *ring, · line 2385

Quoted 9 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +9 -0
+	} else {
+		struct page *page = virt_to_page(xdpf->data);
+
+		/* For local XDP_TX the caller already prepended the TSB
+		 * into xdpf->data/len, so dma_len == xdpf->len.
+		 */
+		dma_len = xdpf->len;
+		mapping = page_pool_get_dma_addr(page) +
+			  sizeof(*xdpf) + xdpf->headroom;

Does this calculation result in an incorrect DMA address?

The calculation implicitly assumes that xdpf resides exactly at the beginning of the page. However, the driver configures the RX page pool with offset = XDP_PACKET_HEADROOM (typically 256 bytes).

By omitting the distance from the start of the page to xdpf, the calculated DMA address points XDP_PACKET_HEADROOM bytes before the actual packet data, which could lead to transmitting uninitialized memory.

Quoted 3 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
+		dma_sync_single_for_device(kdev, mapping, dma_len,
+					   DMA_BIDIRECTIONAL);
+	}

[ ... ]

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -0
+	case XDP_TX:
+		/* Prepend a zeroed TSB (Transmit Status Block).  The GENET
+		 * MAC has TBUF_64B_EN set globally, so hardware expects every
+		 * TX buffer to begin with a 64-byte struct status_64.  Back
+		 * up xdp->data into the RSB area (which is no longer needed
+		 * after the BPF program ran) and zero it.

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2446

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +10 -0
+		 */
+		if (xdp->data - xdp->data_hard_start <
+		    sizeof(struct status_64) + sizeof(struct xdp_frame)) {
+			page_pool_put_full_page(ring->page_pool, rx_page,
+						true);
+			return XDP_DROP;
+		}
+		xdp->data -= sizeof(struct status_64);
+		xdp->data_meta -= sizeof(struct status_64);
+		memset(xdp->data, 0, sizeof(struct status_64));

Could this overwrite XDP metadata and cause memory corruption?

The space check does not account for metasize (xdp->data - xdp->data_meta). If the BPF program appended metadata, the memset() here completely overwrites it.

Furthermore, if metasize is > 0, the shifted xdp->data_meta pointer could underflow and point exactly to or before xdp->data_hard_start. When xdp_convert_buff_to_frame() is subsequently called, it writes the xdp_frame structure at data_hard_start, which would overlap with the shifted data_meta.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#12 lore raw

Re: [PATCH net-next v8 5/7] net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support

In reply to Nicolai Buchwitz
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] Prepended TSB in ndo_xdp_xmit completely overwrites struct xdp_frame. - [Critical] Headroom…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] Prepended TSB in ndo_xdp_xmit completely overwrites struct xdp_frame.
- [Critical] Headroom validation in XDP_TX ignores metadata size, allowing out-of-bounds writes.
- [High] DMA mapping address in XDP_TX is incorrect when XDP metadata is present.
- [High] Redirected XDP frames will be leaked indefinitely if the target interface is idle.
- [Medium] bcmgenet_xdp_setup() clears NETDEV_XDP_ACT_NDO_XMIT when detaching an XDP program, hiding device capabilities.
- [Medium] Runt XDP frames (less than 60 bytes) will not be padded by the hardware and will be transmitted as undersized packets. --

commit 893d59a522f087d2ccf486f06a06bb9e815cf330
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support

Add XDP_REDIRECT support and implement ndo_xdp_xmit for receiving redirected frames from other devices.

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 3c3b0c44ea8a..9dd258567824 100644

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2459

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +5 -1
@@ -2451,11 +2452,25 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 						true);
 			return XDP_DROP;
 		}
-		if (unlikely(!bcmgenet_xdp_xmit_frame(priv, xdpf, false))) {
+
+		tx_ring = &priv->xdp_tx_ring;
+		spin_lock(&tx_ring->lock);
+		if (unlikely(!bcmgenet_xdp_xmit_frame(priv, tx_ring,
+						      xdpf, false))) {

Does the DMA mapping calculation in bcmgenet_xdp_xmit_frame() for this local XDP_TX path (dma_map == false) account for XDP metadata?

If an XDP program populates metadata, the calculation sizeof(*xdpf) + xdpf->headroom in the else block of bcmgenet_xdp_xmit_frame() will point to the start of the metadata rather than where the TSB was actually prepended at xdpf->data. The hardware expects the TSB at the DMA address, so it might misinterpret the metadata as the status block.

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2466

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
+			spin_unlock(&tx_ring->lock);
 			xdp_return_frame_rx_napi(xdpf);
 			return XDP_DROP;
 		}
+		bcmgenet_xdp_ring_doorbell(priv, tx_ring);
+		spin_unlock(&tx_ring->lock);
 		return XDP_TX;

Does the headroom check earlier in this XDP_TX block adequately validate xdp->data_meta?

Before backing up the pointers:

	if (xdp->data - xdp->data_hard_start <
	    sizeof(struct status_64) + sizeof(struct xdp_frame)) {

This check validates the space before xdp->data, but the code then subtracts 64 bytes from xdp->data_meta:

	xdp->data -= sizeof(struct status_64);
	xdp->data_meta -= sizeof(struct status_64);

If an XDP program populates metadata, xdp->data_meta will be lower than xdp->data. The subtraction might push xdp->data_meta below xdp->data_hard_start, causing out-of-bounds writes when xdp_convert_buff_to_frame() constructs the struct xdp_frame.

[ ... ]

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -0
@@ -3996,10 +4017,16 @@ static int bcmgenet_xdp_setup(struct net_device *dev,
 		return -EOPNOTSUPP;
 	}
 
+	if (!prog)
+		xdp_features_clear_redirect_target(dev);

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp_setup(struct net_device *dev, · line 4031

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +4 -0
+
 	old_prog = xchg(&priv->xdp_prog, prog);
 	if (old_prog)
 		bpf_prog_put(old_prog);
 
+	if (prog)
+		xdp_features_set_redirect_target(dev, false);
+
 	return 0;
 }

Can this unintentionally clear NETDEV_XDP_ACT_NDO_XMIT when detaching an XDP program?

The driver statically implements ndo_xdp_xmit and advertises it in dev->xdp_features during probe, indicating the interface can receive XDP redirected frames from other interfaces regardless of its own XDP program state. Calling xdp_features_clear_redirect_target(dev) when prog is NULL will clear the NETDEV_XDP_ACT_NDO_XMIT capability, hiding it from discovery.

[ ... ]

Quoted 13 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +9 -0
@@ -4013,6 +4040,36 @@ static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
 	}
 }
 
+static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames,
+			     struct xdp_frame **frames, u32 flags)
+{
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+	struct bcmgenet_tx_ring *ring = &priv->xdp_tx_ring;
+	int sent = 0;
+	int i;
+
+	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp) · line 4061

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +10 -0
+		return -EINVAL;
+
+	if (unlikely(!netif_running(dev)))
+		return -ENETDOWN;
+
+	spin_lock(&ring->lock);
+
+	for (i = 0; i < num_frames; i++) {
+		if (!bcmgenet_xdp_xmit_frame(priv, ring, frames[i], true))
+			break;

Does bcmgenet_xdp_xmit_frame() overwrite the struct xdp_frame metadata when handling these redirected frames?

When dma_map == true for redirected frames, the driver prepends a 64-byte hardware TSB by calling memset(xdpf->data - sizeof(struct status_64), 0, 64). Since the struct xdp_frame is located right before xdpf->data (or any prepended metadata), writing backwards into the headroom could completely zero out the struct xdp_frame fields, leading to crashes when returning the frame.

Also, does this cause runt frames to be transmitted as undersized packets?

If the XDP frame is less than 60 bytes, enabling DMA_TX_APPEND_CRC inside bcmgenet_xdp_xmit_frame() without first padding the frame might trigger the GENET hardware quirk where it fails to pad packets to the minimum Ethernet size, causing them to be dropped by receiving switches.

Is there a mechanism to ensure these redirected frames won't be leaked if the target interface is idle?

Without setting DMA_TX_REQ_INTR in len_stat during transmission in bcmgenet_xdp_xmit_frame(), TX completions for the XDP TX ring rely entirely on being polled during the RX NAPI cycle. If the interface is receiving no normal traffic, the NAPI poll might never run, causing the redirected frames to sit in the TX ring indefinitely.

Quoted 2 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -0
+		sent++;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#16 lore raw

Re: [PATCH net-next v8 6/7] net: bcmgenet: add XDP statistics counters

In reply to Nicolai Buchwitz
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] XDP_TX headroom check ignores metadata size, and TSB insertion corrupts metadata state. - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] XDP_TX headroom check ignores metadata size, and TSB insertion corrupts metadata state.
- [Medium] Non-atomic updates to global XDP statistics from concurrent NAPI contexts cause data races and cache-line bouncing.
- [Medium] Missing counter increment when xdp_convert_buff_to_frame() fails in the XDP_TX path.
--

commit 9febde2d94a02157dc480d909cc304c59196d72f
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add XDP statistics counters

This commit exposes per-action XDP counters via ethtool -S. It uses the soft MIB infrastructure and increments the counters in bcmgenet_run_xdp().

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 9dd258567824..02ad2f410d6c 100644

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2438

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
@@ -2428,6 +2435,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 
 	switch (act) {
 	case XDP_PASS:
+		priv->mib.xdp_pass++;

Is it safe to update global priv->mib statistics here?

Since bcmgenet_run_xdp() can execute concurrently in multiple independent NAPI polling contexts (one per RX ring), non-atomic increments to shared global variables can cause data races and lost increments.

Continually modifying a shared cacheline on the fast-path for every packet could also force cacheline bouncing across CPUs and degrade XDP performance. Should these statistics be maintained per-queue or using per-CPU variables instead?

Quoted 8 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
 		return XDP_PASS;
 	case XDP_TX:
 		/* Prepend a zeroed TSB (Transmit Status Block).  The GENET
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
 		 * MAC has TBUF_64B_EN set globally, so hardware expects every
 		 * TX buffer to begin with a 64-byte struct status_64.  Back
 		 * up xdp->data into the RSB area (which is no longer needed
 		 * after the BPF program ran) and zero it.
 		 */

drivers/net/ethernet/broadcom/genet/bcmgenet.c

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
 		if (xdp->data - xdp->data_hard_start <
 		    sizeof(struct status_64) + sizeof(struct xdp_frame)) {
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
+			priv->mib.xdp_tx_err++;
 			return XDP_DROP;
 		}
 		xdp->data -= sizeof(struct status_64);
 		xdp->data_meta -= sizeof(struct status_64);
 		memset(xdp->data, 0, sizeof(struct status_64));

While not introduced by this patch, does this headroom check and TSB insertion handle metadata correctly?

The headroom check doesn't account for metasize, which could cause xdp_convert_buff_to_frame() to fail its own bounds check later.

Additionally, if the XDP program added metadata, it would be located at [old_data - metasize, old_data]. The memset writes zeroes to [new_data, new_data + 64], which is [old_data - 64, old_data], potentially overwriting the original metadata.

Since xdp->data_meta is shifted back by 64 bytes, could it now point to garbage memory and corrupt the metadata state?

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2453

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
 		xdpf = xdp_convert_buff_to_frame(xdp);
 		if (unlikely(!xdpf)) {
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
 			return XDP_DROP;
 		}

Are we missing a counter increment on this failure path?

The preceding headroom check failure explicitly increments xdp_tx_err before dropping, but this path returns directly. Since it doesn't fall through to the XDP_DROP switch case, the dropped packet appears to be unaccounted for in the new statistics.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#20 lore raw

Re: [PATCH net-next v8 7/7] net: bcmgenet: reject MTU changes incompatible with XDP

In reply to Nicolai Buchwitz
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The XDP MTU validation logic contains a mathematical error (ignoring L2 headers) and is…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The XDP MTU validation logic contains a mathematical error (ignoring L2 headers) and is structurally unreachable (dead code) because the device's maximum MTU is fixed at 1500. --

commit b8018c0a123b4927db5ca8c39c8395c751532069
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: reject MTU changes incompatible with XDP

This commit introduces a minimal ndo_change_mtu function to the bcmgenet driver. It checks if an XDP program is attached and rejects MTU values that would exceed the capacity of a single-page XDP buffer.

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 02ad2f410d6c..4d1ec68ec0c5 100644
@@ -4085,6 +4085,20 @@ static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames,

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames, · line 4093

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -0
+static int bcmgenet_change_mtu(struct net_device *dev, int new_mtu)
+{
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+
+	if (priv->xdp_prog && new_mtu > PAGE_SIZE - GENET_RX_HEADROOM -
+	    SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) {

Is this condition actually reachable, and does it correctly account for L2 headers?

The bcmgenet driver does not appear to override dev->max_mtu, which defaults to ETH_DATA_LEN (1500). Because dev_validate_mtu() in the core networking stack checks that new_mtu <= dev->max_mtu before calling ndo_change_mtu, new_mtu will never exceed 1500.

Given that PAGE_SIZE (typically 4096) minus GENET_RX_HEADROOM and the skb_shared_info alignment is roughly 3454 bytes, the check new_mtu > 3454 will always evaluate to false.

Also, new_mtu represents the L3 payload size. Shouldn't the check add the L2 header overhead when comparing against the L2 buffer capacity?

Furthermore, is it possible for an MTU change to corrupt the XDP buffer layout as the commit message claims?

The hardware DMA appears to be statically configured to write a maximum of RX_BUF_LENGTH (2048) bytes per buffer, and bcmgenet_desc_rx() explicitly drops any packet larger than 2048 bytes. It seems structurally impossible to overflow the 4096-byte page and corrupt skb_shared_info.

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +7 -0
+		netdev_warn(dev, "MTU too large for single-page XDP buffer\n");
+		return -EINVAL;
+	}
+
+	WRITE_ONCE(dev->mtu, new_mtu);
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author maintainer
#6 lore raw

Re: [PATCH net-next v8 3/7] net: bcmgenet: add basic XDP support (PASS/DROP)

In reply to [email protected]
Quoted 56 lines · On 29.4.2026 23:17, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Hardware checksum offload (CHECKSUM_COMPLETE) is
unconditionally applied after XDP execution, causing valid modified packets to be dropped.
- [Medium] The driver advertises NETDEV_XDP_ACT_BASIC without
supporting XDP_TX, violating the XDP feature API contract.
--

commit fcb14e148a380096868da445b34bcc00893540bf
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add basic XDP support (PASS/DROP)

This commit adds basic XDP support to the bcmgenet driver, allowing XDP programs to execute in the RX path with support for XDP_PASS and XDP_DROP.

Quoted 3 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0

[ ... ]

Quoted 2 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
@@ -2404,30 +2462,34 @@ static unsigned int bcmgenet_desc_rx(struct 
 bcmgenet_rx_ring *ring,

[ ... ]

Quoted 19 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +15 -0
+		/* Save rx_csum before XDP runs - an XDP program
+		 * could overwrite the RSB via bpf_xdp_adjust_head.
 		 */
+		if (dev->features & NETIF_F_RXCSUM)
+			rx_csum = (__force __be16)(status->rx_csum & 0xffff);
 
+		xdp_init_buff(&xdp, PAGE_SIZE, &ring->xdp_rxq);
+		xdp_prepare_buff(&xdp, page_address(rx_page),
+				 GENET_RX_HEADROOM, pkt_len, true);
 
+		xdp_act = bcmgenet_run_xdp(ring, xdp_prog, &xdp, rx_page);
+		if (xdp_act != XDP_PASS)
+			goto next;
 
+		skb = bcmgenet_xdp_build_skb(ring, &xdp);
+		if (unlikely(!skb)) {
+			BCMGENET_STATS64_INC(stats, dropped);
+			page_pool_put_full_page(ring->page_pool,
+						rx_page, true);

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -1
+			goto next;
 		}
 
 		/* Set up checksum offload */
 		if (dev->features & NETIF_F_RXCSUM) {
-			rx_csum = (__force __be16)(status->rx_csum & 0xffff);
 			if (rx_csum) {
 				skb->csum = (__force __wsum)ntohs(rx_csum);
 				skb->ip_summed = CHECKSUM_COMPLETE;
 			}

If the XDP program modifies the packet payload (e.g., rewriting IPs or ports), the hardware checksum saved before XDP execution will no longer match the new payload.

Quoted 5 lines

If we unconditionally apply this old checksum and set CHECKSUM_COMPLETE on the SKB here, will the network stack drop the valid modified packet due to a hardware checksum failure?

Should the checksum be invalidated when an XDP program is present?

This is the standard pattern across XDP drivers (e.g. ice). With CHECKSUM_COMPLETE, the stack performs software validation by combining the hardware checksum with its own pseudo-header sum. If an XDP program modifies the packet, validation fails and the stack drops the packet, no silent corruption. XDP programs that modify packets and return PASS are extremely rare; the common pattern is modify+TX/REDIRECT.

Quoted 14 lines

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_probe(struct platform_device · line 4154

Quoted 8 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
@@ -4054,6 +4148,7 @@ static int bcmgenet_probe(struct platform_device 
 *pdev)
 			 NETIF_F_RXCSUM;
 	dev->hw_features |= dev->features;
 	dev->vlan_features |= dev->features;
+	dev->xdp_features = NETDEV_XDP_ACT_BASIC;
 
 	netdev_sw_irq_coalesce_default_on(dev);

The commit message notes that XDP_TX is not yet supported and returns XDP_ABORTED.

Quoted 7 lines

The UAPI definition for NETDEV_XDP_ACT_BASIC implies support for XDP_PASS, XDP_DROP, XDP_ABORTED, and XDP_TX. Does advertising this flag without XDP_TX support break feature detection for userspace applications?

I see from the patch series that XDP_TX support is added in a later commit. Would it be safer to defer setting NETDEV_XDP_ACT_BASIC until that patch to avoid breaking userspace assumptions during a bisect?

Per <linux/netdev.h>, NETDEV_XDP_ACT_BASIC = "XDP features set supported by all drivers (XDP_ABORTED, XDP_DROP, XDP_PASS, XDP_TX)". The XDP_TX listed there is the BPF action constant, which is always handled (we return XDP_ABORTED for unsupported actions, which is the documented behavior). The driver-level XDP_TX *capability* is a separate flag (NETDEV_XDP_ACT_XDP_TX), not implied by BASIC.

Thanks
Nicolai

author maintainer
#9 lore raw

Re: [PATCH net-next v8 4/7] net: bcmgenet: add XDP_TX support

In reply to [email protected]
Quoted 24 lines · On 29.4.2026 23:17, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Out-of-bounds array access in bcmgenet_tx_reclaim during interface teardown
- [High] Incorrect DMA address calculation for local XDP_TX causes information leak and packet corruption
- [High] Prepending status_64 block overwrites XDP metadata and
causes memory corruption
- [Medium] XDP TX ring size is smaller than standard NAPI budget
--

commit 9add891fa4e7cf62ec26683116338b5877c21662
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add XDP_TX support

Implement XDP_TX using ring 16 (DESC_INDEX), the hardware default descriptor ring, dedicated to XDP TX for isolation from SKB TX queues.

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 1b60571446e1..3c3b0c44ea8a 100644
@@ -48,8 +48,10 @@
 
Quoted 7 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c line 51

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
 #define GENET_Q0_RX_BD_CNT	\
 	(TOTAL_DESC - priv->hw_params->rx_queues * 
 priv->hw_params->rx_bds_per_q)
+#define GENET_Q16_TX_BD_CNT	32

Is it possible to drop packets here during bursts since this TX ring is smaller than the standard NAPI budget?

Yes, by design. If the ring fills, frames are dropped, which is standard XDP backpressure. Same approach as bnxt, mvneta, and other drivers.

Quoted 22 lines

The standard RX NAPI budget is 64, and TX completions are reclaimed only once before the RX loop. If the RX loop processes a full budget of 64 packets and they all result in an XDP_TX action, the first 32 will fill the TX ring.

The remaining 32 packets would fail in bcmgenet_xdp_xmit_frame() because ring->free_bds < 1, and be dropped.

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static void bcmgenet_tx_reclaim_all(struct · line 2077

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
@@ -2043,6 +2069,9 @@ static void bcmgenet_tx_reclaim_all(struct 
 net_device *dev)
 	do {
 		bcmgenet_tx_reclaim(dev, &priv->tx_rings[i++], true);
 	} while (i <= priv->hw_params->tx_queues && 
 netif_is_multiqueue(dev));
+
+	/* Also reclaim XDP TX ring */
+	bcmgenet_tx_reclaim(dev, &priv->xdp_tx_ring, true);
 }

Can this cause an out-of-bounds array access during interface teardown?

Quoted 2 lines

When bcmgenet_tx_reclaim_all() calls bcmgenet_tx_reclaim() with all == true, it unconditionally calls netdev_tx_reset_queue(netdev_get_tx_queue(dev, ring->index)).

bcmgenet_tx_reclaim() does not call netdev_tx_reset_queue() or netdev_get_tx_queue(). The only netdev_get_tx_queue() in the reclaim path (in __bcmgenet_tx_reclaim, for BQL) is guarded by
"if (ring->index != DESC_INDEX)". The netdev_tx_reset_queue() loop in bcmgenet_fini_dma() iterates indices 0..tx_queues only.

Quoted 30 lines

The XDP TX ring has an index of 16 (DESC_INDEX), but the network device is allocated with only GENET_MAX_MQ_CNT + 1 TX queues (typically 5). Accessing index 16 of dev->_tx is out-of-bounds, and netdev_tx_reset_queue() will write to this invalid memory.

[ ... ]

Quoted 8 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
@@ -2299,11 +2328,96 @@ static struct sk_buff 
 *bcmgenet_xdp_build_skb(struct bcmgenet_rx_ring *ring,
 	return skb;
 }
 
+static bool bcmgenet_xdp_xmit_frame(struct bcmgenet_priv *priv,
+				     struct xdp_frame *xdpf, bool dma_map)
+{

[ ... ]

Quoted 1 line
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
+	if (dma_map) {

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static struct sk_buff *bcmgenet_xdp_build_skb(struct bcmgenet_rx_ring *ring, · line 2385

Quoted 9 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +9 -0
+	} else {
+		struct page *page = virt_to_page(xdpf->data);
+
+		/* For local XDP_TX the caller already prepended the TSB
+		 * into xdpf->data/len, so dma_len == xdpf->len.
+		 */
+		dma_len = xdpf->len;
+		mapping = page_pool_get_dma_addr(page) +
+			  sizeof(*xdpf) + xdpf->headroom;

Does this calculation result in an incorrect DMA address?

Quoted 7 lines

The calculation implicitly assumes that xdpf resides exactly at the beginning of the page. However, the driver configures the RX page pool with offset = XDP_PACKET_HEADROOM (typically 256 bytes).

By omitting the distance from the start of the page to xdpf, the calculated DMA address points XDP_PACKET_HEADROOM bytes before the actual packet data, which could lead to transmitting uninitialized memory.

Correct, xdpf does reside at the start of the page. xdp_convert_buff_to_frame() stores the xdp_frame at xdp->data_hard_start, which equals page_address(page) for our page_pool layout. page_pool_get_dma_addr() returns the DMA address of the page start (not offset by .offset). sizeof(*xdpf) + xdpf->headroom equals xdpf->data - data_hard_start, pointing to the prepended TSB.

Quoted 15 lines
Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
+		dma_sync_single_for_device(kdev, mapping, dma_len,
+					   DMA_BIDIRECTIONAL);
+	}

[ ... ]

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +10 -0
+	case XDP_TX:
+		/* Prepend a zeroed TSB (Transmit Status Block).  The GENET
+		 * MAC has TBUF_64B_EN set globally, so hardware expects every
+		 * TX buffer to begin with a 64-byte struct status_64.  Back
+		 * up xdp->data into the RSB area (which is no longer needed
+		 * after the BPF program ran) and zero it.
+		 */
+		if (xdp->data - xdp->data_hard_start <
+		    sizeof(struct status_64) + sizeof(struct xdp_frame)) {
+			page_pool_put_full_page(ring->page_pool, rx_page,
Quoted 8 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2446

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -0
+						true);
+			return XDP_DROP;
+		}
+		xdp->data -= sizeof(struct status_64);
+		xdp->data_meta -= sizeof(struct status_64);
+		memset(xdp->data, 0, sizeof(struct status_64));

Could this overwrite XDP metadata and cause memory corruption?

xdp_convert_buff_to_frame() validates (headroom - metasize) >= sizeof(struct xdp_frame) and returns NULL otherwise; we drop the frame in that case. The memset writes to [xdp->data - 64, xdp->data] before the data shift, which was previously the RSB area, not the metadata area. Both data and data_meta are shifted equally, preserving the metasize gap.

Quoted 9 lines

The space check does not account for metasize (xdp->data - xdp->data_meta). If the BPF program appended metadata, the memset() here completely overwrites it.

Furthermore, if metasize is > 0, the shifted xdp->data_meta pointer could underflow and point exactly to or before xdp->data_hard_start. When xdp_convert_buff_to_frame() is subsequently called, it writes the xdp_frame structure at data_hard_start, which would overlap with the shifted data_meta.

Thanks
Nicolai

author maintainer
#21 lore raw

Re: [PATCH net-next v8 7/7] net: bcmgenet: reject MTU changes incompatible with XDP

In reply to [email protected]
Quoted 40 lines · On 29.4.2026 23:17, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The XDP MTU validation logic contains a mathematical error
(ignoring L2 headers) and is structurally unreachable (dead code) because the device's maximum MTU is fixed at 1500. --

commit b8018c0a123b4927db5ca8c39c8395c751532069
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: reject MTU changes incompatible with XDP

This commit introduces a minimal ndo_change_mtu function to the bcmgenet driver. It checks if an XDP program is attached and rejects MTU values that would exceed the capacity of a single-page XDP buffer.

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 02ad2f410d6c..4d1ec68ec0c5 100644
@@ -4085,6 +4085,20 @@ static int bcmgenet_xdp_xmit(struct net_device 
 *dev, int num_frames,

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames, · line 4093

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -0
+static int bcmgenet_change_mtu(struct net_device *dev, int new_mtu)
+{
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+
+	if (priv->xdp_prog && new_mtu > PAGE_SIZE - GENET_RX_HEADROOM -
+	    SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) {

Is this condition actually reachable, and does it correctly account for L2 headers?

The bcmgenet driver does not appear to override dev->max_mtu, which defaults to ETH_DATA_LEN (1500). Because dev_validate_mtu() in the core networking stack checks that new_mtu <= dev->max_mtu before calling ndo_change_mtu, new_mtu will never exceed 1500.

Given that PAGE_SIZE (typically 4096) minus GENET_RX_HEADROOM and the skb_shared_info alignment is roughly 3454 bytes, the check new_mtu > 3454 will always evaluate to false.

Quoted 5 lines

Also, new_mtu represents the L3 payload size. Shouldn't the check add the L2 header overhead when comparing against the L2 buffer capacity?

Furthermore, is it possible for an MTU change to corrupt the XDP buffer layout as the commit message claims?

Correct, the check is currently unreachable since dev->max_mtu defaults to 1500. It is a safety guard for the planned follow-up series adding full ndo_change_mtu support. L2 overhead handling will be part of that follow-up.

The commit message wording is slightly misleading; today no MTU change can corrupt the layout, but once jumbo MTU support is added, this guard prevents misconfiguration while an XDP program is attached.

The original discussion with Mohsin and Florian on the rationale for adding this in the XDP series rather than deferring to the MTU follow-up is here:
https://lore.kernel.org/netdev/[email protected]/
https://lore.kernel.org/netdev/[email protected]/

Quoted 6 lines

The hardware DMA appears to be statically configured to write a maximum of RX_BUF_LENGTH (2048) bytes per buffer, and bcmgenet_desc_rx() explicitly drops any packet larger than 2048 bytes. It seems structurally impossible to overflow the 4096-byte page and corrupt skb_shared_info.

Quoted 7 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames, · line 4100

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +7 -0
+		netdev_warn(dev, "MTU too large for single-page XDP buffer\n");
+		return -EINVAL;
+	}
+
+	WRITE_ONCE(dev->mtu, new_mtu);
+	return 0;
+}

Thanks
Nicolai

author maintainer
#17 lore raw

Re: [PATCH net-next v8 6/7] net: bcmgenet: add XDP statistics counters

In reply to [email protected]
Quoted 25 lines · On 29.4.2026 23:17, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] XDP_TX headroom check ignores metadata size, and TSB insertion corrupts metadata state.
- [Medium] Non-atomic updates to global XDP statistics from concurrent
NAPI contexts cause data races and cache-line bouncing.
- [Medium] Missing counter increment when xdp_convert_buff_to_frame() fails in the XDP_TX path.
--

commit 9febde2d94a02157dc480d909cc304c59196d72f
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add XDP statistics counters

This commit exposes per-action XDP counters via ethtool -S. It uses the soft MIB infrastructure and increments the counters in bcmgenet_run_xdp().

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 9dd258567824..02ad2f410d6c 100644

[ ... ]

Quoted 8 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct · line 2439

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
@@ -2428,6 +2435,7 @@ static unsigned int bcmgenet_run_xdp(struct 
 bcmgenet_rx_ring *ring,
 
 	switch (act) {
 	case XDP_PASS:
+		priv->mib.xdp_pass++;

Is it safe to update global priv->mib statistics here?

The XDP counters follow the existing soft MIB convention in this driver (plain u32, unsynchronized). A larger ring refactoring is AFAIK planned that should address all MIB counters consistently.

Quoted 23 lines

Since bcmgenet_run_xdp() can execute concurrently in multiple independent NAPI polling contexts (one per RX ring), non-atomic increments to shared global variables can cause data races and lost increments.

Continually modifying a shared cacheline on the fast-path for every packet could also force cacheline bouncing across CPUs and degrade XDP performance. Should these statistics be maintained per-queue or using per-CPU variables instead?

Quoted 13 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
 		return XDP_PASS;
 	case XDP_TX:
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
 		/* Prepend a zeroed TSB (Transmit Status Block).  The GENET
 		 * MAC has TBUF_64B_EN set globally, so hardware expects every
 		 * TX buffer to begin with a 64-byte struct status_64.  Back
 		 * up xdp->data into the RSB area (which is no longer needed
 		 * after the BPF program ran) and zero it.
 		 */
 		if (xdp->data - xdp->data_hard_start <
 		    sizeof(struct status_64) + sizeof(struct xdp_frame)) {
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
+			priv->mib.xdp_tx_err++;
Quoted 8 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
 			return XDP_DROP;
 		}
 		xdp->data -= sizeof(struct status_64);
 		xdp->data_meta -= sizeof(struct status_64);
 		memset(xdp->data, 0, sizeof(struct status_64));

While not introduced by this patch, does this headroom check and TSB insertion handle metadata correctly?

Already addressed under patches 4 and 5: xdp_convert_buff_to_frame() validates headroom against metasize. The memset writes into the former RSB area, above the metadata; both data and data_meta shift equally so the metadata position is preserved.

Quoted 13 lines

The headroom check doesn't account for metasize, which could cause xdp_convert_buff_to_frame() to fail its own bounds check later.

Additionally, if the XDP program added metadata, it would be located at [old_data - metasize, old_data]. The memset writes zeroes to [new_data, new_data + 64], which is [old_data - 64, old_data], potentially overwriting the original metadata.

Since xdp->data_meta is shifted back by 64 bytes, could it now point to garbage memory and corrupt the metadata state?

Quoted 1 line
Quoted 8 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2453

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
 		xdpf = xdp_convert_buff_to_frame(xdp);
 		if (unlikely(!xdpf)) {
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
 			return XDP_DROP;
 		}

Are we missing a counter increment on this failure path?

Fair point, but xdp_convert_buff_to_frame() failure is an extremely rare path (insufficient headroom for xdp_frame). Can be addressed in a follow-up.

Quoted 5 lines

The preceding headroom check failure explicitly increments xdp_tx_err before dropping, but this path returns directly. Since it doesn't fall through to the XDP_DROP switch case, the dropped packet appears to be unaccounted for in the new statistics.

Thanks,
Nicolai

author maintainer
#13 lore raw

Re: [PATCH net-next v8 5/7] net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support

In reply to [email protected]
Quoted 30 lines · On 29.4.2026 23:17, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] Prepended TSB in ndo_xdp_xmit completely overwrites struct xdp_frame.
- [Critical] Headroom validation in XDP_TX ignores metadata size,
allowing out-of-bounds writes.
- [High] DMA mapping address in XDP_TX is incorrect when XDP metadata
is present.
- [High] Redirected XDP frames will be leaked indefinitely if the
target interface is idle.
- [Medium] bcmgenet_xdp_setup() clears NETDEV_XDP_ACT_NDO_XMIT when detaching an XDP program, hiding device capabilities.
- [Medium] Runt XDP frames (less than 60 bytes) will not be padded by
the hardware and will be transmitted as undersized packets.
--

commit 893d59a522f087d2ccf486f06a06bb9e815cf330
Author: Nicolai Buchwitz <[email protected]>

net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support

Add XDP_REDIRECT support and implement ndo_xdp_xmit for receiving redirected frames from other devices.

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 3c3b0c44ea8a..9dd258567824 100644

[ ... ]

Quoted 14 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct · line 2460

Quoted 11 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +5 -1
@@ -2451,11 +2452,25 @@ static unsigned int bcmgenet_run_xdp(struct 
 bcmgenet_rx_ring *ring,
 						true);
 			return XDP_DROP;
 		}
-		if (unlikely(!bcmgenet_xdp_xmit_frame(priv, xdpf, false))) {
+
+		tx_ring = &priv->xdp_tx_ring;
+		spin_lock(&tx_ring->lock);
+		if (unlikely(!bcmgenet_xdp_xmit_frame(priv, tx_ring,
+						      xdpf, false))) {

Does the DMA mapping calculation in bcmgenet_xdp_xmit_frame() for this local XDP_TX path (dma_map == false) account for XDP metadata?

For local XDP_TX, the caller adjusts xdp->data backwards to include the prepended TSB before xdp_convert_buff_to_frame(). xdpf->data then points to the TSB start, and the mapping calculation correctly resolves to that location.

Quoted 7 lines

If an XDP program populates metadata, the calculation sizeof(*xdpf) + xdpf->headroom in the else block of bcmgenet_xdp_xmit_frame() will point to the start of the metadata rather than where the TSB was actually prepended at xdpf->data. The hardware expects the TSB at the DMA address, so it might misinterpret the metadata as the status block.

Quoted 10 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2466

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
+			spin_unlock(&tx_ring->lock);
 			xdp_return_frame_rx_napi(xdpf);
 			return XDP_DROP;
 		}
+		bcmgenet_xdp_ring_doorbell(priv, tx_ring);
+		spin_unlock(&tx_ring->lock);
 		return XDP_TX;

Does the headroom check earlier in this XDP_TX block adequately validate xdp->data_meta?

xdp_convert_buff_to_frame() validates the remaining headroom against sizeof(struct xdp_frame) and returns NULL on insufficient space, which we handle by dropping the frame.

Quoted 29 lines

Before backing up the pointers:

	if (xdp->data - xdp->data_hard_start <
	    sizeof(struct status_64) + sizeof(struct xdp_frame)) {

This check validates the space before xdp->data, but the code then subtracts 64 bytes from xdp->data_meta:

	xdp->data -= sizeof(struct status_64);
	xdp->data_meta -= sizeof(struct status_64);

If an XDP program populates metadata, xdp->data_meta will be lower than xdp->data. The subtraction might push xdp->data_meta below xdp->data_hard_start, causing out-of-bounds writes when xdp_convert_buff_to_frame() constructs the struct xdp_frame.

[ ... ]

Quoted 12 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
@@ -3996,10 +4017,16 @@ static int bcmgenet_xdp_setup(struct 
 net_device *dev,
 		return -EOPNOTSUPP;
 	}
 
+	if (!prog)
+		xdp_features_clear_redirect_target(dev);
+
 	old_prog = xchg(&priv->xdp_prog, prog);
 	if (old_prog)
 		bpf_prog_put(old_prog);
 
Quoted 8 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp_setup(struct net_device *dev, · line 4031

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
+	if (prog)
+		xdp_features_set_redirect_target(dev, false);
+
 	return 0;
 }

Can this unintentionally clear NETDEV_XDP_ACT_NDO_XMIT when detaching an XDP program?

xdp_features_clear_redirect_target() / xdp_features_set_redirect_target() only manipulate the redirect-target advertisement, not NETDEV_XDP_ACT_NDO_XMIT. The same pattern is used by ice, bnxt and others.

Quoted 28 lines

The driver statically implements ndo_xdp_xmit and advertises it in dev->xdp_features during probe, indicating the interface can receive XDP redirected frames from other interfaces regardless of its own XDP program state. Calling xdp_features_clear_redirect_target(dev) when prog is NULL will clear the NETDEV_XDP_ACT_NDO_XMIT capability, hiding it from discovery.

[ ... ]

Quoted 19 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +14 -0
@@ -4013,6 +4040,36 @@ static int bcmgenet_xdp(struct net_device *dev, 
 struct netdev_bpf *xdp)
 	}
 }
 
+static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames,
+			     struct xdp_frame **frames, u32 flags)
+{
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+	struct bcmgenet_tx_ring *ring = &priv->xdp_tx_ring;
+	int sent = 0;
+	int i;
+
+	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
+		return -EINVAL;
+
+	if (unlikely(!netif_running(dev)))
+		return -ENETDOWN;
+
Quoted 8 lines

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp) · line 4061

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +5 -0
+	spin_lock(&ring->lock);
+
+	for (i = 0; i < num_frames; i++) {
+		if (!bcmgenet_xdp_xmit_frame(priv, ring, frames[i], true))
+			break;

Does bcmgenet_xdp_xmit_frame() overwrite the struct xdp_frame metadata when handling these redirected frames?

No. The xdp_frame ends at data_hard_start + sizeof(xdp_frame). The TSB is written at xdpf->data - 64, which falls inside the headroom (after the xdp_frame). The check xdpf->headroom < sizeof(struct status_64) ensures no overlap with the xdp_frame.

Quoted 8 lines

When dma_map == true for redirected frames, the driver prepends a 64-byte hardware TSB by calling memset(xdpf->data - sizeof(struct status_64), 0, 64). Since the struct xdp_frame is located right before xdpf->data (or any prepended metadata), writing backwards into the headroom could completely zero out the struct xdp_frame fields, leading to crashes when returning the frame.

Also, does this cause runt frames to be transmitted as undersized packets?

Padding short frames is the responsibility of the XDP program / source device, same as in other XDP drivers.

Quoted 8 lines

If the XDP frame is less than 60 bytes, enabling DMA_TX_APPEND_CRC inside bcmgenet_xdp_xmit_frame() without first padding the frame might trigger the GENET hardware quirk where it fails to pad packets to the minimum Ethernet size, causing them to be dropped by receiving switches.

Is there a mechanism to ensure these redirected frames won't be leaked if the target interface is idle?

Known limitation of piggybacked TX reclaim, shared by other drivers. In practice, redirect targets see bidirectional traffic. Frames are dropped (not leaked) when the ring fills

Quoted 9 lines

Without setting DMA_TX_REQ_INTR in len_stat during transmission in bcmgenet_xdp_xmit_frame(), TX completions for the XDP TX ring rely entirely on being polled during the RX NAPI cycle. If the interface is receiving no normal traffic, the NAPI poll might never run, causing the redirected frames to sit in the TX ring indefinitely.

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp(struct net_device *dev, · line 4064

Quoted 2 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -0
+		sent++;
+	}

Thanks,
Nicolai

maintainer
#2 lore raw

Re: [PATCH net-next v8 1/7] net: bcmgenet: convert RX path to page_pool

In reply to Nicolai Buchwitz

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: bcmgenet: convert RX path to page_pool

This patch replaces the per-packet __netdev_alloc_skb() + dma_map_single() in the bcmgenet RX path with page_pool, adding a page_pool per RX ring and building SKBs from pages via napi_build_skb() with skb_mark_for_recycle(). A few questions on the changes below.

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 54f71b1e85fc..d013a3df9048 100644

drivers/net/ethernet/broadcom/genet/bcmgenet.c line 60

Quoted 9 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -0
@@ -52,6 +52,13 @@
 #define RX_BUF_LENGTH		2048
 #define SKB_ALIGNMENT		32
 
+/* Page pool RX buffer layout:
+ * XDP_PACKET_HEADROOM | RSB(64) + pad(2) | frame data | skb_shared_info
+ * The HW writes the 64B RSB + 2B alignment padding before the frame.
+ */
+#define GENET_RSB_PAD		(sizeof(struct status_64) + 2)
+#define GENET_RX_HEADROOM	(XDP_PACKET_HEADROOM + GENET_RSB_PAD)

Is GENET_RX_HEADROOM used anywhere? A grep of the tree after this patch shows no references to it; all call sites use XDP_PACKET_HEADROOM and GENET_RSB_PAD separately. Can this macro be dropped?

Jakub: false positive

Also, XDP_PACKET_HEADROOM is referenced here and in three other places in bcmgenet.c (bcmgenet_rx_refill, bcmgenet_desc_rx, and bcmgenet_rx_ring_create_pool), but the only new include added by the patch is <net/page_pool/helpers.h> in bcmgenet.h. XDP_PACKET_HEADROOM is defined in include/uapi/linux/bpf.h, and the helpers.h/types.h chain does not include it. Would it be worth adding an explicit #include <linux/bpf.h> to bcmgenet.c so this does not rely on transitive inclusion (other drivers using this macro, e.g. bnxt, include it explicitly)?

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -2
@@ -2250,46 +2249,30 @@ static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
-static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
-					  struct enet_cb *cb)
+static int bcmgenet_rx_refill(struct bcmgenet_rx_ring *ring,
+			      struct enet_cb *cb)
 {

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev) · line 2263

Quoted 3 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -3
-	/* Allocate a new Rx skb */
-	skb = __netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT,
-				 GFP_ATOMIC | __GFP_NOWARN);

After this conversion, priv->rx_buf_len no longer has any readers; the old dma_map_single/dma_unmap paths that consumed it are gone, and the new code uses RX_BUF_LENGTH directly in pp_params.max_len and page_pool_dma_sync_for_cpu(). bcmgenet_init() still writes the field. Should priv->rx_buf_len be removed as part of this cleanup?

[ ... ]

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
@@ -2341,25 +2324,28 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 	while ((rxpktprocessed < rxpkttoprocess) &&
 	       (rxpktprocessed < budget)) {
 		struct status_64 *status;
+		struct page *rx_page;
+		unsigned int rx_off;
 		__be16 rx_csum;
+		void *hard_start;
 
 		cb = &priv->rx_cbs[ring->read_ptr];

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring, · line 2344

Quoted 2 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -0
+		page_pool_dma_sync_for_cpu(ring->page_pool, rx_page, 0,
+					   RX_BUF_LENGTH);

page_pool_dma_sync_for_cpu() is called unconditionally with a fixed size of RX_BUF_LENGTH (2048) even when the actual received frame is much smaller. The received length is available in the descriptor status word once it is read. On the non-cache-coherent SoCs this driver targets (for example BCM2711), is there a reason not to sync only the number of bytes that were actually received?

[ ... ]

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
@@ -2375,7 +2361,8 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
 		if (unlikely(len > RX_BUF_LENGTH)) {
 			netif_err(priv, rx_status, dev, "oversized packet\n");
 			BCMGENET_STATS64_INC(stats, length_errors);

[ ... ]

Quoted 1 line
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
@@ -2411,24 +2399,47 @@ static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,

[ ... ]

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +9 -1
-		skb_put(skb, len);
+		/* Build SKB from the page - data starts at hard_start,
+		 * frame begins after RSB(64) + pad(2) = 66 bytes.
+		 */
+		skb = napi_build_skb(hard_start, PAGE_SIZE - XDP_PACKET_HEADROOM);
+		if (unlikely(!skb)) {
+			BCMGENET_STATS64_INC(stats, dropped);
+			page_pool_put_full_page(ring->page_pool, rx_page,
+						true);
+			goto next;

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring, · line 2422

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -3
+		}
 
-		/* remove RSB and hardware 2bytes added for IP alignment */
-		skb_pull(skb, 66);
-		len -= 66;
+		skb_mark_for_recycle(skb);
+
+		/* Reserve the RSB + pad, then set the data length */
+		skb_reserve(skb, GENET_RSB_PAD);
+		__skb_put(skb, len - GENET_RSB_PAD);

Can len here be smaller than GENET_RSB_PAD (66)? len is taken from status->length_status >> DMA_BUFLENGTH_SHIFT and the only bound checked above is the upper bound against RX_BUF_LENGTH:

    if (unlikely(len > RX_BUF_LENGTH)) {
        ...
    }

If HW ever reports a runt length below 66 (for example after a reset or due to an errata path), len - GENET_RSB_PAD underflows as an unsigned value. __skb_put() is the unchecked variant:

    static inline void *__skb_put(struct sk_buff *skb, unsigned int len)
    {
        void *tmp = skb_tail_pointer(skb);
        SKB_LINEAR_ASSERT(skb);
        skb->tail += len;
        skb->len  += len;
        return tmp;
    }

so it will not trip skb_over_panic(); instead skb->tail/skb->len advance past skb->end into the page. Since napi_build_skb() was given frag_size = PAGE_SIZE - XDP_PACKET_HEADROOM, skb->end sits inside the same page_pool-backed page and the SKB would then expose recycled page_pool memory up the stack via napi_gro_receive().

The previous code used skb_put(skb, len) followed by skb_pull(skb, 66), both of which are bounds-checked. Would it make sense to add an explicit guard such as:

    if (unlikely(len < GENET_RSB_PAD)) {
        BCMGENET_STATS64_INC(stats, length_errors);
        page_pool_put_full_page(ring->page_pool, rx_page, true);
        goto next;
    }

before the __skb_put() call?

Jakub: I suppose.. IDK if protecting against bad HW is required, up to you

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring, · line 2426

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -2
 		if (priv->crc_fwd_en) {
-			skb_trim(skb, len - ETH_FCS_LEN);
-			len -= ETH_FCS_LEN;
+			skb_trim(skb, skb->len - ETH_FCS_LEN);
 		}

The same underflow concern applies here: if skb->len is less than ETH_FCS_LEN, skb->len - ETH_FCS_LEN wraps to a very large unsigned value, and skb_trim() does:

    void skb_trim(struct sk_buff *skb, unsigned int len)
    {
        if (skb->len > len)
            __skb_trim(skb, len);
    }

so the trim is silently skipped and the FCS stays attached. Should this path also validate skb->len before subtracting ETH_FCS_LEN?

[ ... ]

Signature · 1 line
pw-bot: cr
maintainer
#10 lore raw

Re: [PATCH net-next v8 4/7] net: bcmgenet: add XDP_TX support

In reply to Nicolai Buchwitz

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: bcmgenet: add XDP_TX support

This patch adds XDP_TX support to bcmgenet by dedicating ring 16 (DESC_INDEX) to XDP TX, carving 32 BDs from ring 0's allocation and piggybacking TX completion on the RX NAPI poll. The page_pool DMA direction is switched from DMA_FROM_DEVICE to DMA_BIDIRECTIONAL.

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 1b60571446e1..3c3b0c44ea8a 100644

[ ... ]

Quoted 22 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +9 -2
@@ -1940,8 +1956,15 @@ static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
 
 	/* Reclaim transmitted buffers */
 	while (txbds_processed < txbds_ready) {
-		skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
-					  &priv->tx_cbs[ring->clean_ptr]);
+		tx_cb = &priv->tx_cbs[ring->clean_ptr];
+		if (tx_cb->xdpf) {
+			pkts_compl++;
+			bytes_compl += tx_cb->xdp_dma_map
+				? tx_cb->xdpf->len
+				: tx_cb->xdpf->len -
+				  sizeof(struct status_64);
+		}
+		skb = bcmgenet_free_tx_cb(&priv->pdev->dev, tx_cb);
 		if (skb) {
 			pkts_compl++;
 			bytes_compl += GENET_CB(skb)->bytes_sent;
@@ -1963,8 +1986,11 @@ static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
 	u64_stats_add(&stats->bytes, bytes_compl);
 	u64_stats_update_end(&stats->syncp);
 

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev, · line 1996

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +5 -2
-	netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->index),
-				  pkts_compl, bytes_compl);
+	/* Ring DESC_INDEX (XDP TX) has no netdev TX queue; skip BQL */
+	if (ring->index != DESC_INDEX)
+		netdev_tx_completed_queue(netdev_get_tx_queue(dev,
+							      ring->index),
+					  pkts_compl, bytes_compl);
 
 	return txbds_processed;
 }

The guard here skips netdev_get_tx_queue() for the XDP TX ring, but the outer wrapper bcmgenet_tx_reclaim() still calls:

    netdev_tx_reset_queue(netdev_get_tx_queue(dev, ring->index));
    ...
    bcmgenet_tdma_ring_writel(priv, ring->index, ring->prod_index, ...);

unconditionally when invoked with all=true.

For the XDP TX ring, ring->index == DESC_INDEX == 16, but bcmgenet_probe() allocates the netdev via alloc_etherdev_mqs(..., GENET_MAX_MQ_CNT + 1, GENET_MAX_MQ_CNT + 1), which is only 5 TX queues. So netdev_get_tx_queue(dev,
16) returns &dev->_tx[16], well past the end of the allocated _tx[] array.

On CONFIG_BQL=y (the default), netdev_tx_reset_queue() then writes into that memory through dql_reset() and clear_bit(__QUEUE_STATE_STACK_XOFF, &q->state).

Can this path corrupt memory past dev->_tx[] whenever bcmgenet_tx_reclaim_all() runs on the XDP TX ring? See the new call added below:

[ ... ]

drivers/net/ethernet/broadcom/genet/bcmgenet.c static void bcmgenet_tx_reclaim_all(struct net_device *dev) · line 2075

Quoted 8 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
@@ -2043,6 +2069,9 @@ static void bcmgenet_tx_reclaim_all(struct net_device *dev)
 	do {
 		bcmgenet_tx_reclaim(dev, &priv->tx_rings[i++], true);
 	} while (i <= priv->hw_params->tx_queues && netif_is_multiqueue(dev));
+
+	/* Also reclaim XDP TX ring */
+	bcmgenet_tx_reclaim(dev, &priv->xdp_tx_ring, true);
 }

bcmgenet_tx_reclaim_all() is reached from bcmgenet_netif_stop() (ifconfig down), the WoL/suspend reconfigure path, and open/reset-failure cleanup, so this is triggerable from userspace on every interface-down.

Should the netdev_tx_reset_queue() in bcmgenet_tx_reclaim()'s all-branch be guarded with the same ring->index != DESC_INDEX check that was added to __bcmgenet_tx_reclaim()?

[ ... ]

Quoted 1 line
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
@@ -3008,14 +3161,18 @@ static int bcmgenet_rdma_disable(struct bcmgenet_priv *priv)

[ ... ]

Quoted 15 lines

static void bcmgenet_init_tx_queues(struct net_device *dev) {

drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -1
@@ -3028,7 +3185,8 @@ static void bcmgenet_init_tx_queues(struct net_device *dev)
 
 	/* Initialize Tx priority queues */
 	for (i = 0; i <= priv->hw_params->tx_queues; i++) {
-		bcmgenet_init_tx_ring(priv, i, end - start, start, end);
+		bcmgenet_init_tx_ring(priv, &priv->tx_rings[i],
+				      i, end - start, start, end);
 		start = end;
 		end += priv->hw_params->tx_bds_per_q;
 		dma_priority[DMA_PRIO_REG_INDEX(i)] |=
@@ -3036,13 +3194,19 @@ static void bcmgenet_init_tx_queues(struct net_device *dev)
 			<< DMA_PRIO_REG_SHIFT(i);
 	}

drivers/net/ethernet/broadcom/genet/bcmgenet.c static void bcmgenet_init_tx_queues(struct net_device *dev) · line 3205

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +5 -0
 
+	/* Initialize ring 16 (descriptor ring) for XDP TX */
+	bcmgenet_init_tx_ring(priv, &priv->xdp_tx_ring,
+			      DESC_INDEX, GENET_Q16_TX_BD_CNT,
+			      TOTAL_DESC - GENET_Q16_TX_BD_CNT, TOTAL_DESC);
+
 	/* Set Tx queue priorities */
 	bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
 	bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
 	bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);

What priority does ring 16 end up with under strict-priority arbitration?

dma_priority[] is declared as:

    u32 dma_priority[3] = {0, 0, 0};

and only rings 0..tx_queues populate it. Ring 16's priority field lives in DMA_PRIORITY_2, bits [20..24] (DMA_PRIO_REG_INDEX(16) == 2, DMA_PRIO_REG_SHIFT(16) == 20), and is left as zero.

In this driver priority 0 is the highest:

    #define GENET_Q1_PRIORITY   0   /* Default highest priority queue */
    #define GENET_Q0_PRIORITY   1

A few lines above, arbitration is set to strict priority:

    bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);

So ring 16 ends up at priority 0, outranking Q0 (priority 1) and equal to the user-configured high-priority queues Q1..Q4. Should ring 16 be given an explicit priority (for example the same as Q0, or lower) so XDP_TX does not preempt normal SKB TX under strict-priority arbitration?

maintainer
#14 lore raw

Re: [PATCH net-next v8 5/7] net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support

In reply to Nicolai Buchwitz

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: bcmgenet: add XDP_REDIRECT and ndo_xdp_xmit support

This patch adds XDP_REDIRECT support in the RX path and implements ndo_xdp_xmit for receiving redirected frames from other devices, batching frames into ring 16 under a single spinlock. It also advertises NETDEV_XDP_ACT_REDIRECT and NETDEV_XDP_ACT_NDO_XMIT in xdp_features.

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 3c3b0c44ea8a..9dd258567824 100644

[ ... ]

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
@@ -3996,10 +4017,16 @@ static int bcmgenet_xdp_setup(struct net_device *dev,
 		return -EOPNOTSUPP;
 	}
 

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp_setup(struct net_device *dev, · line 4029

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -0
+	if (!prog)
+		xdp_features_clear_redirect_target(dev);
+
 	old_prog = xchg(&priv->xdp_prog, prog);
 	if (old_prog)
 		bpf_prog_put(old_prog);
 
+	if (prog)
+		xdp_features_set_redirect_target(dev, false);
+

Does this leave NETDEV_XDP_ACT_NDO_XMIT permanently cleared after a single attach+detach cycle?

bcmgenet_probe() unconditionally sets:

    dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
                        NETDEV_XDP_ACT_NDO_XMIT;

but on any call to bcmgenet_xdp_setup() with prog == NULL this code runs xdp_features_clear_redirect_target(dev), which clears both NETDEV_XDP_ACT_NDO_XMIT and NETDEV_XDP_ACT_NDO_XMIT_SG in xdp_features_clear_redirect_target_locked() (net/core/xdp.c).

bcmgenet_xdp_xmit() does not depend on priv->xdp_prog — it only needs netif_running() and priv->xdp_tx_ring, which is initialized unconditionally in bcmgenet_init_tx_queues(). So after attach+detach, is_valid_dst() in kernel/bpf/devmap.c:__xdp_enqueue() will see NETDEV_XDP_ACT_NDO_XMIT cleared and reject every redirected frame with -EOPNOTSUPP until another program is attached.

The paired xdp_features_set_redirect_target(dev, false) call only fires for prog != NULL, so it does not restore the bit after a no-op detach, and it is redundant with the probe-time advertisement when prog is non-NULL.

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
 	return 0;
 }
 
@@ -4013,6 +4040,36 @@ static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
 	}
 }
 
+static int bcmgenet_xdp_xmit(struct net_device *dev, int num_frames,
+			     struct xdp_frame **frames, u32 flags)
+{

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp) · line 4055

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +10 -0
+	struct bcmgenet_priv *priv = netdev_priv(dev);
+	struct bcmgenet_tx_ring *ring = &priv->xdp_tx_ring;
+	int sent = 0;
+	int i;
+
+	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
+		return -EINVAL;
+
+	if (unlikely(!netif_running(dev)))
+		return -ENETDOWN;

Is there a use-after-free window between this check and a concurrent bcmgenet_close()?

netif_running() is an unlocked test_bit on __LINK_STATE_START. __dev_close_many() clears that bit and then calls ndo_stop, which reaches bcmgenet_close() -> bcmgenet_netif_stop() -> bcmgenet_fini_dma(), where kfree(priv->tx_cbs) frees the backing array that ring->cbs points into.

The synchronize_net() inside dev_deactivate_many() only waits for outstanding qdisc enqueuers, not for a remote device's NAPI loop that is inside xdp_do_flush() -> __dev_flush() -> bq_xmit_all() -> bcmgenet_xdp_xmit() on another CPU.

A caller that passed the netif_running() test just before __LINK_STATE_START was cleared can then execute bcmgenet_get_txcb() and dmadesc_set() against freed tx_cbs memory after bcmgenet_fini_dma() has run.

Would an RCU-protected "xmit enabled" flag cleared on close followed by synchronize_rcu() before freeing the ring be needed here?

Jakub: I think this is worried that sync_net will be skipped if the device has no qdiscs on down(). I don't think we care.

Quoted 6 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +6 -0
+
+	spin_lock(&ring->lock);
+
+	for (i = 0; i < num_frames; i++) {
+		if (!bcmgenet_xdp_xmit_frame(priv, ring, frames[i], true))
+			break;

drivers/net/ethernet/broadcom/genet/bcmgenet.c static int bcmgenet_xdp(struct net_device *dev, struct netdev_bpf *xdp) · line 4071

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +10 -0
+		sent++;
+	}
+
+	if (sent)
+		bcmgenet_xdp_ring_doorbell(priv, ring);
+
+	spin_unlock(&ring->lock);
+
+	return sent;
+}

How are completions reclaimed on priv->xdp_tx_ring (ring 16) when the device has no local RX traffic?

The existing ring 16 has no TX interrupt and is reclaimed only from bcmgenet_rx_poll(); the driver comment states:

    /* ring 16 has no interrupt */

bcmgenet_xdp_xmit() itself has no completion path. When this device is used purely as a redirect target (receiving frames via ndo_xdp_xmit from other NICs) with little or no local RX traffic, bcmgenet_rx_poll() is not scheduled, completed descriptors are never reclaimed, ring->free_bds stays at 0 after the first burst, and bcmgenet_xdp_xmit_frame() returns false for every subsequent frame. bcmgenet_xdp_xmit() then returns 0 (all frames dropped) until unrelated RX traffic happens to arrive.

The XDP_TX path does not hit this because it runs from inside bcmgenet_rx_poll(), which drains the ring on each call.

Would scheduling tx reclaim from ndo_xdp_xmit, or installing a completion interrupt or timer for ring 16, be appropriate here?

Quoted 4 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
+
 static const struct net_device_ops bcmgenet_netdev_ops = {
 	.ndo_open		= bcmgenet_open,
 	.ndo_stop		= bcmgenet_close,

[ ... ]

Quoted 9 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -1
@@ -4327,7 +4385,8 @@ static int bcmgenet_probe(struct platform_device *pdev)
 			 NETIF_F_RXCSUM;
 	dev->hw_features |= dev->features;
 	dev->vlan_features |= dev->features;
-	dev->xdp_features = NETDEV_XDP_ACT_BASIC;
+	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
+			    NETDEV_XDP_ACT_NDO_XMIT;
 
 	netdev_sw_irq_coalesce_default_on(dev);
maintainer
#18 lore raw

Re: [PATCH net-next v8 6/7] net: bcmgenet: add XDP statistics counters

In reply to Nicolai Buchwitz

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
---
net: bcmgenet: add XDP statistics counters

This commit adds six per-action XDP counters (xdp_pass, xdp_drop, xdp_tx, xdp_tx_err, xdp_redirect, xdp_redirect_err) to bcmgenet via the existing soft MIB infrastructure, exposing them through ethtool -S and incrementing them from bcmgenet_run_xdp().

Quoted 5 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +0 -0
index 9dd258567824..02ad2f410d6c 100644

drivers/net/ethernet/broadcom/genet/bcmgenet.c static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = { · line 1178

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +7 -0
@@ -1169,6 +1169,13 @@ static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
 	STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb),
 	STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed",
 			    mib.tx_realloc_tsb_failed),
+	/* XDP counters */
+	STAT_GENET_SOFT_MIB("xdp_pass", mib.xdp_pass),
+	STAT_GENET_SOFT_MIB("xdp_drop", mib.xdp_drop),
+	STAT_GENET_SOFT_MIB("xdp_tx", mib.xdp_tx),
+	STAT_GENET_SOFT_MIB("xdp_tx_err", mib.xdp_tx_err),
+	STAT_GENET_SOFT_MIB("xdp_redirect", mib.xdp_redirect),
+	STAT_GENET_SOFT_MIB("xdp_redirect_err", mib.xdp_redirect_err),

Should these counters be exposed through the standard netdev netlink qstats interface rather than as driver-private ethtool -S strings?

Per-action XDP statistics are a generic, cross-driver category that already has a standard uAPI: the netdev netlink family exposes per-device and per-queue XDP counters (rx-xdp-packets, rx-xdp-bytes, rx-xdp-errors, tx-xdp-xmit-*, etc.) via the qstats ops, retrievable with ynl.

Documentation/networking/statistics.rst reserves ethtool -S for driver-private counters, and the networking subsystem guidance is that new counters should be plumbed through the standard interface rather than added as private strings, even when the driver does not currently implement that interface.

Jakub: no, this is wrong, there are no standard counters for XDP today

Quoted 20 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -0
 	/* Per TX queues */
 	STAT_GENET_Q(0),
 	STAT_GENET_Q(1),
@@ -2428,6 +2435,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 
 	switch (act) {
 	case XDP_PASS:
+		priv->mib.xdp_pass++;
 		return XDP_PASS;
 	case XDP_TX:
 		/* Prepend a zeroed TSB (Transmit Status Block).  The GENET
@@ -2440,6 +2448,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 		    sizeof(struct status_64) + sizeof(struct xdp_frame)) {
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
+			priv->mib.xdp_tx_err++;
 			return XDP_DROP;
 		}
 		xdp->data -= sizeof(struct status_64);

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2477

Quoted 10 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +2 -0
@@ -2459,19 +2468,24 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 						      xdpf, false))) {
 			spin_unlock(&tx_ring->lock);
 			xdp_return_frame_rx_napi(xdpf);
+			priv->mib.xdp_tx_err++;
 			return XDP_DROP;
 		}
 		bcmgenet_xdp_ring_doorbell(priv, tx_ring);
 		spin_unlock(&tx_ring->lock);
+		priv->mib.xdp_tx++;
 		return XDP_TX;

Is the XDP_TX path missing an increment on one of its failure exits? The headroom check and the bcmgenet_xdp_xmit_frame() failure both bump xdp_tx_err, but the xdp_convert_buff_to_frame() failure in between:

	xdpf = xdp_convert_buff_to_frame(xdp);
	if (unlikely(!xdpf)) {
		page_pool_put_full_page(ring->page_pool, rx_page,
					true);
		return XDP_DROP;
	}

returns XDP_DROP without touching any counter.

With this path in place, does the identity

	xdp_pass + xdp_drop + xdp_tx + xdp_tx_err +
	xdp_redirect + xdp_redirect_err == packets_processed

still hold?

Quoted 15 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +3 -0
 	case XDP_REDIRECT:
 		if (unlikely(xdp_do_redirect(priv->dev, xdp, prog))) {
+			priv->mib.xdp_redirect_err++;
 			page_pool_put_full_page(ring->page_pool, rx_page,
 						true);
 			return XDP_DROP;
 		}
+		priv->mib.xdp_redirect++;
 		return XDP_REDIRECT;
 	case XDP_DROP:
+		priv->mib.xdp_drop++;
 		page_pool_put_full_page(ring->page_pool, rx_page, true);
 		return XDP_DROP;
 	default:

drivers/net/ethernet/broadcom/genet/bcmgenet.c static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring, · line 2499

Quoted 7 lines
drivers/net/ethernet/broadcom/genet/bcmgenet.c +1 -0
@@ -2479,6 +2493,7 @@ static unsigned int bcmgenet_run_xdp(struct bcmgenet_rx_ring *ring,
 		fallthrough;
 	case XDP_ABORTED:
 		trace_xdp_exception(priv->dev, prog, act);
+		priv->mib.xdp_drop++;
 		page_pool_put_full_page(ring->page_pool, rx_page, true);
 		return XDP_ABORTED;
 	}

Does folding XDP_ABORTED and the unsupported-action default case into xdp_drop match the commit message's description of "per-action XDP counters"?

The function returns XDP_ABORTED (not XDP_DROP) in those cases, and the existing trace_xdp_exception() call distinguishes aborts and unknown actions from intentional XDP_DROP.

With the current mapping, operators cannot tell an explicit XDP_DROP from a program abort or an invalid action return when looking at xdp_drop. Would a separate xdp_aborted (or xdp_errors) counter for the XDP_ABORTED and default cases be more appropriate?