Skip to contentSkip to conversation

XDP with io_uring zero-copy RX and devmem

PATCH In review Björn Töpel 3 participants 14 messages Nov 17 – Nov 20

Subsystems (12)
lore
Share & tools
lore
#0 lore raw

cover XDP with io_uring zero-copy RX and devmem

Hi!

I had a long chat on page-pool providers with Magnus and Maciej earlier this week, and the topic of "XDP on unreadable providers" came up. Magnus suggested building on the xdp.frags mode, to support this.

Here goes...

Devmem and io_uring zero-copy RX place packet payload in memory that the kernel cannot read through the normal packet APIs. Header split keeps the headers in ordinary kernel memory. Today, the core rejects XDP and these memory providers on the same device, in either attach order.

A program that checks headers should not need to copy the payload or turn off zero-copy receive. Keep the payload in fragments and run a native XDP program with frags support. Frags support means that the program accepts a packet whose data_end does not cover the whole packet. It does not give the program permission to read provider data.

Kuba added XDP_FLAGS_FRAGS_UNREADABLE, which already records this kind of payload. xdp_buff_add_frag() sets it for net_iov fragments, and skb construction copies it to skb->unreadable. Drivers use this path even without an XDP program. The series uses the same flag to stop helpers from reading or writing provider fragments. Header access still works.

XDP_PASS keeps the payload for the normal receive path. XDP_DROP frees it without reading it. Frame redirects and AF_XDP copies reject a buffer while the flag is set. The driver must also reject XDP_TX:
provider payload is mapped for receive, not transmit. Tail trimming can remove the payload without reading it; after the last fragment is removed, the remaining header buffer has no unreadable fragments.

Only drivers that declare xdp_unreadable support accept the combination. fbnic is the real driver in this series. Its XDP_TX path drops packets while unreadable fragments remain. netdevsim tests the attachment rules, but it does not test packet access to provider memory.

Generic XDP remains rejected. Its receive path first copies or linearizes nonlinear skbs, which fails when it reaches provider data. Even if that step were skipped, skb-to-XDP conversion does not carry the unreadable flag, and generic transmit and redirect use skb paths outside the native driver's XDP_TX guard. Supporting generic XDP needs separate handling; admitting it here would not be enough.

Björn

Björn Töpel (7):
bpf: Keep XDP helpers within readable data
xdp: Reject frames with unreadable fragments
net: Admit native XDP on provider queues
eth: fbnic: Run XDP on provider queues
netdevsim: Run native XDP next to devmem
selftests: net: Test XDP with devmem bindings
selftests: net: Test XDP on io_uring RX

 Documentation/networking/netmem.rst           |   8 +
 MAINTAINERS                                   |   2 +
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  |   4 +-
 drivers/net/netdevsim/netdev.c                |   2 +
 include/linux/filter.h                        |   9 +-
 include/net/netdev_queues.h                   |   6 +
 include/net/xdp.h                             |  20 +-
 kernel/bpf/helpers.c                          |   4 +-
 net/core/dev.c                                |  28 +-
 net/core/dev.h                                |   1 +
 net/core/filter.c                             |  37 ++-
 net/core/netdev_rx_queue.c                    |   2 +-
 net/xdp/xsk.c                                 |   5 +
 .../selftests/drivers/net/hw/.gitignore       |   1 +
 .../testing/selftests/drivers/net/hw/Makefile |  10 +-
 tools/testing/selftests/drivers/net/hw/config |   5 +
 .../selftests/drivers/net/hw/iou-zcrx.c       | 242 +++++++++++++++++-
 .../selftests/drivers/net/hw/iou-zcrx.py      | 108 +++++++-
 .../selftests/drivers/net/hw/iou_zcrx.bpf.c   | 111 ++++++++
 .../selftests/drivers/net/hw/iou_zcrx.h       |  34 +++
 tools/testing/selftests/net/nl_netdev.py      |  54 +++-
 21 files changed, 646 insertions(+), 47 deletions(-)
 create mode 100644 tools/testing/selftests/drivers/net/hw/iou_zcrx.bpf.c
 create mode 100644 tools/testing/selftests/drivers/net/hw/iou_zcrx.h

base-commit: f23dcd8055612eb37c666c67fe0cfe38b3c65021

Signature · 1 line
2.56.1
#1 lore raw

PATCH 1/7 bpf: Keep XDP helpers within readable data

Devmem and io_uring zero-copy RX put payload in fragments that the kernel cannot read. XDP helpers must not treat these fragments as normal pages.

Reject operations that read or write these fragments. Access to the linear part still works. Tail trimming does not read the payload; clear the unreadable flag after it removes every fragment.

Drivers currently use these buffers to build skbs without running XDP. These checks are needed before native XDP can run on provider queues.

---
 include/linux/filter.h |  9 +++++----
 include/net/xdp.h      | 12 ++++++++++++
 kernel/bpf/helpers.c   |  4 +++-
 net/core/filter.c      | 37 +++++++++++++++++++++++++------------
 4 files changed, 45 insertions(+), 17 deletions(-)
include/linux/filter.h +5 -4
index 4f0cd67caccb..5c2f57ec3940 100644
@@ -1994,8 +1994,8 @@ int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
-void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
-		      void *buf, unsigned long len, bool flush);
+int bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+		     void *buf, unsigned long len, bool flush);
 int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
 			       const void *from, u32 len, u64 flags);
 void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset);
@@ -2029,9 +2029,10 @@ static inline void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
 	return NULL;
 }
 
-static inline void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off, void *buf,
-				    unsigned long len, bool flush)
+static inline int bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+				   void *buf, unsigned long len, bool flush)
 {
+	return -EOPNOTSUPP;
 }
 
 static inline int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
include/net/xdp.h +12 -0
index 07231adfb5f8..16693cf9653e 100644
@@ -136,6 +136,18 @@ static __always_inline void xdp_buff_set_frag_unreadable(struct xdp_buff *xdp)
 	xdp->flags |= XDP_FLAGS_FRAGS_UNREADABLE;
 }
 
+static __always_inline void
+xdp_buff_clear_frag_unreadable(struct xdp_buff *xdp)
+{
+	xdp->flags &= ~XDP_FLAGS_FRAGS_UNREADABLE;
+}
+
+static __always_inline bool
+xdp_buff_is_frag_unreadable(const struct xdp_buff *xdp)
+{
+	return !!(xdp->flags & XDP_FLAGS_FRAGS_UNREADABLE);
+}
+
 static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp)
 {
 	return xdp->flags;
kernel/bpf/helpers.c +3 -1
index 6788489a2608..8c9e4640bcfe 100644
@@ -3140,7 +3140,9 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr *p, u64 offset,
 
 		if (!buffer__nullable)
 			return NULL;
-		bpf_xdp_copy_buf(ptr->data, ptr->offset + offset, buffer__nullable, len, false);
+		if (bpf_xdp_copy_buf(ptr->data, ptr->offset + offset,
+				     buffer__nullable, len, false))
+			return NULL;
 		return buffer__nullable;
 	}
 	case BPF_DYNPTR_TYPE_SKB_META:
net/core/filter.c +25 -12
index f42ecfb5841f..9a55f61fbcdf 100644
@@ -4269,8 +4269,8 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
 	.arg2_type	= ARG_ANYTHING,
 };
 
-void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
-		      void *buf, unsigned long len, bool flush)
+int bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
+		     void *buf, unsigned long len, bool flush)
 {
 	unsigned long ptr_len, ptr_off = 0;
 	skb_frag_t *next_frag, *end_frag;
@@ -4282,9 +4282,12 @@ void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
 		src = flush ? buf : xdp->data + off;
 		dst = flush ? xdp->data + off : buf;
 		memcpy(dst, src, len);
-		return;
+		return 0;
 	}
 
+	if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+		return -EFAULT;
+
 	sinfo = xdp_get_shared_info_from_buff(xdp);
 	end_frag = &sinfo->frags[sinfo->nr_frags];
 	next_frag = &sinfo->frags[0];
@@ -4314,6 +4317,8 @@ void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
 		ptr_len = skb_frag_size(next_frag);
 		next_frag++;
 	}
+
+	return 0;
 }
 
 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
@@ -4332,6 +4337,9 @@ void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
 	if (likely(offset < size)) /* linear area */
 		goto out;
 
+	if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+		return ERR_PTR(-EFAULT);
+
 	sinfo = xdp_get_shared_info_from_buff(xdp);
 	offset -= size;
 	for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */
@@ -4358,10 +4366,9 @@ BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset,
 		return PTR_ERR(ptr);
 
 	if (!ptr)
-		bpf_xdp_copy_buf(xdp, offset, buf, len, false);
-	else
-		memmove(buf, ptr, len);
+		return bpf_xdp_copy_buf(xdp, offset, buf, len, false);
 
+	memmove(buf, ptr, len);
 	return 0;
 }
 
@@ -4390,11 +4397,9 @@ BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
 		return PTR_ERR(ptr);
 
 	if (!ptr)
-		bpf_xdp_copy_buf(xdp, offset, buf, len, true);
-	else
-		/* through a dynptr the source may be an overlapping slice */
-		memmove(ptr, buf, len);
+		return bpf_xdp_copy_buf(xdp, offset, buf, len, true);
 
+	memmove(ptr, buf, len);
 	return 0;
 }
 
@@ -4422,6 +4427,8 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
 
 	if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
 		return -EOPNOTSUPP;
+	if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+		return -EFAULT;
 
 	tailroom = rxq->frag_size - skb_frag_size(frag) -
 		   skb_frag_off(frag) % rxq->frag_size;
@@ -4503,6 +4510,7 @@ static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
 	if (unlikely(!sinfo->nr_frags)) {
 		xdp_buff_clear_frags_flag(xdp);
 		xdp_buff_clear_frag_pfmemalloc(xdp);
+		xdp_buff_clear_frag_unreadable(xdp);
 		xdp->data_end -= offset;
 	}
 
@@ -5355,8 +5363,7 @@ static unsigned long bpf_xdp_copy(void *dst, const void *ctx,
 {
 	struct xdp_buff *xdp = (struct xdp_buff *)ctx;
 
-	bpf_xdp_copy_buf(xdp, off, dst, len, false);
-	return 0;
+	return bpf_xdp_copy_buf(xdp, off, dst, len, false) ? len : 0;
 }
 
 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
@@ -5369,6 +5376,9 @@ BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
 
 	if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
 		return -EFAULT;
+	if (unlikely(xdp_size > xdp->data_end - xdp->data &&
+		     xdp_buff_is_frag_unreadable(xdp)))
+		return -EFAULT;
 
 	return bpf_event_output(map, flags, meta, meta_size, xdp,
 				xdp_size, bpf_xdp_copy);
@@ -12797,6 +12807,7 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
  * Return:
  * * %0         - success
  * * %-EINVAL   - invalid len
+ * * %-EFAULT   - the kernel cannot read the fragments
  */
 __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)
 {
@@ -12812,6 +12823,8 @@ __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)
 
 	if (unlikely(len > xdp_get_buff_len(xdp)))
 		return -EINVAL;
+	if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+		return -EFAULT;
 
 	start = xdp_data_meta_unsupported(xdp) ? xdp->data : xdp->data_meta;
 
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH net-next 1/7] bpf: Keep XDP helpers within readable data

Quoted 5 lines · Björn Töpel

Devmem and io_uring zero-copy RX put payload in fragments that the kernel cannot read. XDP helpers must not treat these fragments as normal pages.

Reject operations that read or write these fragments. Access to the

[..]

Quoted 2 lines

linear part still works. Tail trimming does not read the payload; clear the unreadable flag after it removes every fragment.

I guess this is technically true, but why allow it at all? Easier/safer to reject tail trimming as well?

#3 lore raw

PATCH 2/7 xdp: Reject frames with unreadable fragments

In reply to Björn Töpel

An xdp_frame can be sent to another device, processed on another CPU, or copied into an AF_XDP socket. These paths do not support provider fragments that the kernel cannot read or remap for transmit.

Reject conversion of such buffers to xdp_frame and reject AF_XDP copies. A failed redirect leaves buffer ownership with the driver, which drops and frees the packet.

---
 include/net/xdp.h | 3 +++
 net/xdp/xsk.c     | 5 +++++
 2 files changed, 8 insertions(+)
include/net/xdp.h +3 -0
index 16693cf9653e..20d173fe163c 100644
@@ -432,6 +432,9 @@ int xdp_update_frame_from_buff(const struct xdp_buff *xdp,
 		return -ENOSPC;
 	}
 
+	if (unlikely(xdp_buff_is_frag_unreadable(xdp)))
+		return -EFAULT;
+
 	xdp_frame->data = xdp->data;
 	xdp_frame->len  = xdp->data_end - xdp->data;
 	xdp_frame->headroom = headroom - sizeof(*xdp_frame);
net/xdp/xsk.c +5 -0
index f22f9a2cdbbe..25b95ec19334 100644
@@ -489,6 +489,11 @@ static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 		return xsk_rcv_zc(xs, xdp, len);
 	}
 
+	if (unlikely(xdp_buff_is_frag_unreadable(xdp))) {
+		xs->rx_dropped++;
+		return -EFAULT;
+	}
+
 	err = __xsk_rcv(xs, xdp, len);
 	if (!err)
 		xdp_return_buff(xdp);
Signature · 1 line
2.56.1
#4 lore raw

PATCH 3/7 net: Admit native XDP on provider queues

In reply to Björn Töpel

XDP and memory providers are currently rejected on the same device. Header-split RX keeps headers in normal memory, so a program can filter packets without reading the provider payload.

Keep this restriction unless the driver declares xdp_unreadable support. Admit native XDP in either attach order on these drivers. Existing header-split checks require frags-aware programs.

The driver must mark provider fragments and reject XDP_TX while those fragments remain. The core rejects helper access and frame redirects.

Keep generic and hardware-offloaded XDP rejected. Generic XDP builds an xdp_buff from an skb without carrying the unreadable flag. Its transmit and redirect paths also bypass the native driver guard.

---
 Documentation/networking/netmem.rst |  8 ++++++++
 include/net/netdev_queues.h         |  6 ++++++
 include/net/xdp.h                   |  5 ++---
 net/core/dev.c                      | 28 ++++++++++++++++++++++++++--
 net/core/dev.h                      |  1 +
 net/core/netdev_rx_queue.c          |  2 +-
 6 files changed, 44 insertions(+), 6 deletions(-)
Documentation/networking/netmem.rst +8 -0
index 6871dcdd700c..1ac94fc9d667 100644
@@ -131,6 +131,14 @@ Driver RX Requirements
    circulation times, such as when userspace holds a reference in zerocopy
    scenarios.
 
+8. XDP programs run next to unreadable memory providers only if the driver
+   sets netdev_queue_mgmt_ops.xdp_unreadable, and only in native mode. The
+   programs see the headers in the linear part, and helpers that reach
+   unreadable fragments fail. Attach payload fragments with
+   xdp_buff_add_frag(), which marks unreadable ones, and drop XDP_TX of a
+   buffer for which xdp_buff_is_frag_unreadable() is true: the payload is
+   mapped for receive only. Redirects of such a buffer fail in the core.
+
 Driver TX Requirements
 ======================
 
include/net/netdev_queues.h +6 -0
index c5335e935b7d..4f6702b284b4 100644
@@ -199,6 +199,11 @@ enum {
  *
  * @supported_params:	Bitmask of supported parameters, see QCFG_*.
  *
+ * @xdp_unreadable:	Native XDP programs may run on queues with unreadable
+ *			memory providers. The driver builds XDP buffers with
+ *			xdp_buff_add_frag(), and drops XDP_TX of a buffer for
+ *			which xdp_buff_is_frag_unreadable() is true.
+ *
  * Note that @ndo_queue_mem_alloc and @ndo_queue_mem_free may be called while
  * the interface is closed. @ndo_queue_start and @ndo_queue_stop will only
  * be called for an interface which is open.
@@ -229,6 +234,7 @@ struct netdev_queue_mgmt_ops {
 				    struct netlink_ext_ack *extack);
 
 	unsigned int supported_params;
+	bool xdp_unreadable;
 };
 
 void netdev_queue_config(struct net_device *dev, int rxq,
include/net/xdp.h +2 -3
index 20d173fe163c..167f733a0bb0 100644
@@ -76,9 +76,8 @@ enum xdp_buff_flags {
 	XDP_FLAGS_FRAGS_PF_MEMALLOC	= BIT(1), /* xdp paged memory is under
 						   * pressure
 						   */
-	/* frags have unreadable mem, this can't be true for real XDP packets,
-	 * but drivers may use XDP helpers to construct Rx pkt state even when
-	 * XDP program is not attached.
+	/* frags have unreadable mem: XDP programs reach only the linear part,
+	 * and the buffer cannot become an xdp_frame
 	 */
 	XDP_FLAGS_FRAGS_UNREADABLE	= BIT(2),
 };
net/core/dev.c +26 -2
index a230ecce9327..f0c3d56d5b83 100644
@@ -10575,6 +10575,27 @@ u8 dev_xdp_sb_prog_count(struct net_device *dev)
 	return count;
 }
 
+/* Only native programs run next to unreadable memory providers. Drivers must
+ * mark unreadable fragments and reject XDP_TX while they remain attached.
+ * Tcp-data-split, which these providers need, also needs frags support.
+ */
+static bool netif_xdp_mp_mode_ok(const struct net_device *dev,
+				 enum bpf_xdp_mode mode)
+{
+	return mode == XDP_MODE_DRV && dev->queue_mgmt_ops->xdp_unreadable;
+}
+
+/* Can the XDP programs of @dev run next to a new memory provider? */
+bool netif_xdp_mp_ok(struct net_device *dev)
+{
+	int i;
+
+	for (i = 0; i < __MAX_XDP_MODE; i++)
+		if (dev_xdp_prog(dev, i) && !netif_xdp_mp_mode_ok(dev, i))
+			return false;
+	return true;
+}
+
 int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 {
 	if (!dev->netdev_ops->ndo_bpf)
@@ -10588,7 +10609,9 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (dev_get_min_mp_channel_count(dev) &&
+	    (bpf->command != XDP_SETUP_PROG ||
+	     !netif_xdp_mp_mode_ok(dev, XDP_MODE_DRV))) {
 		NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");
 		return -EBUSY;
 	}
@@ -10664,7 +10687,8 @@ static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (dev_get_min_mp_channel_count(dev) &&
+	    !netif_xdp_mp_mode_ok(dev, mode)) {
 		NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider");
 		return -EBUSY;
 	}
net/core/dev.h +1 -0
index 4e9e8cd15697..a7036302f120 100644
@@ -118,6 +118,7 @@ int netdev_queue_config_revalidate(struct net_device *dev,
 bool netif_rxq_has_mp(struct net_device *dev, unsigned int rxq_idx);
 bool netif_rxq_is_leased(struct net_device *dev, unsigned int rxq_idx);
 bool netif_is_queue_leasee(const struct net_device *dev);
+bool netif_xdp_mp_ok(struct net_device *dev);
 
 void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
 			      const struct pp_memory_provider_params *p);
net/core/netdev_rx_queue.c +1 -1
index a7261fedd5e7..dab5107e31f8 100644
@@ -204,7 +204,7 @@ static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		NL_SET_ERR_MSG(extack, "hds-thresh is not zero");
 		return -EINVAL;
 	}
-	if (dev_xdp_prog_count(dev)) {
+	if (!netif_xdp_mp_ok(dev)) {
 		NL_SET_ERR_MSG(extack, "unable to custom memory provider to device with XDP program attached");
 		return -EEXIST;
 	}
Signature · 1 line
2.56.1
#5 lore raw

Re: [PATCH net-next 3/7] net: Admit native XDP on provider queues

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: missing propagated XDP check in netif_xdp_mp_ok() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: missing propagated XDP check in netif_xdp_mp_ok() --

commit d33c78f94dc0fb7af8ecef5adb0fc4ff90275bda
Author: Björn Töpel <[email protected]>

net: Admit native XDP on provider queues

This commit allows native XDP programs to run alongside unreadable memory providers on drivers that set netdev_queue_mgmt_ops.xdp_unreadable.

Quoted 33 lines
net/core/dev.c +21 -0
index a230ecce9327d..f0c3d56d5b832 100644
@@ -10575,6 +10575,27 @@ u8 dev_xdp_sb_prog_count(struct net_device *dev)
 	return count;
 }
 
+/* Only native programs run next to unreadable memory providers. Drivers must
+ * mark unreadable fragments and reject XDP_TX while they remain attached.
+ * Tcp-data-split, which these providers need, also needs frags support.
+ */
+static bool netif_xdp_mp_mode_ok(const struct net_device *dev,
+				 enum bpf_xdp_mode mode)
+{
+	return mode == XDP_MODE_DRV && dev->queue_mgmt_ops->xdp_unreadable;
+}
+
+/* Can the XDP programs of @dev run next to a new memory provider? */
+bool netif_xdp_mp_ok(struct net_device *dev)
+{
+	int i;
+
+	for (i = 0; i < __MAX_XDP_MODE; i++)
+		if (dev_xdp_prog(dev, i) && !netif_xdp_mp_mode_ok(dev, i))
+			return false;
+	return true;
+}
+
 int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 {
 	if (!dev->netdev_ops->ndo_bpf)

net/core/dev.c int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf) · line 10617

Quoted 10 lines
net/core/dev.c +3 -1
@@ -10588,7 +10609,9 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (dev_get_min_mp_channel_count(dev) &&
+	    (bpf->command != XDP_SETUP_PROG ||
+	     !netif_xdp_mp_mode_ok(dev, XDP_MODE_DRV))) {
 		NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");
 		return -EBUSY;
 	}

[Severity: Medium]
Does netif_xdp_mp_ok() miss native XDP programs propagated from an upper device such as bonding or hv_netvsc?

When an upper device propagates XDP via netif_xdp_propagate(), the program is stored in the upper device's xdp_state[] while the lower device's dev->xdp_state[] remains empty. If native XDP is attached to the upper device first and a memory provider is then opened on the lower device via __netif_mp_open_rxq(), netif_xdp_mp_ok() sees no program on the lower device and returns true without checking dev->queue_mgmt_ops->xdp_unreadable.

On a lower driver with xdp_unreadable set to false, this lets native XDP run on queues with unreadable net_iov fragments (which can cause a NULL pointer dereference when XDP_TX or XDP_DROP paths call skb_frag_page()). In addition, while the memory provider remains bound, netif_xdp_propagate() rejects even bpf->prog == NULL with -EBUSY, preventing the upper device from detaching the propagated XDP program and leaking the bpf_prog reference.

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

Re: [PATCH net-next 3/7] net: Admit native XDP on provider queues

In reply to Björn Töpel
Quoted 61 lines · Björn Töpel

XDP and memory providers are currently rejected on the same device. Header-split RX keeps headers in normal memory, so a program can filter packets without reading the provider payload.

Keep this restriction unless the driver declares xdp_unreadable support. Admit native XDP in either attach order on these drivers. Existing header-split checks require frags-aware programs.

The driver must mark provider fragments and reject XDP_TX while those fragments remain. The core rejects helper access and frame redirects.

Keep generic and hardware-offloaded XDP rejected. Generic XDP builds an xdp_buff from an skb without carrying the unreadable flag. Its transmit and redirect paths also bypass the native driver guard.

---
 Documentation/networking/netmem.rst |  8 ++++++++
 include/net/netdev_queues.h         |  6 ++++++
 include/net/xdp.h                   |  5 ++---
 net/core/dev.c                      | 28 ++++++++++++++++++++++++++--
 net/core/dev.h                      |  1 +
 net/core/netdev_rx_queue.c          |  2 +-
 6 files changed, 44 insertions(+), 6 deletions(-)
Documentation/networking/netmem.rst +8 -0
index 6871dcdd700c..1ac94fc9d667 100644
@@ -131,6 +131,14 @@ Driver RX Requirements
    circulation times, such as when userspace holds a reference in zerocopy
    scenarios.
 
+8. XDP programs run next to unreadable memory providers only if the driver
+   sets netdev_queue_mgmt_ops.xdp_unreadable, and only in native mode. The
+   programs see the headers in the linear part, and helpers that reach
+   unreadable fragments fail. Attach payload fragments with
+   xdp_buff_add_frag(), which marks unreadable ones, and drop XDP_TX of a
+   buffer for which xdp_buff_is_frag_unreadable() is true: the payload is
+   mapped for receive only. Redirects of such a buffer fail in the core.
+
 Driver TX Requirements
 ======================
 
include/net/netdev_queues.h +5 -0
index c5335e935b7d..4f6702b284b4 100644
@@ -199,6 +199,11 @@ enum {
  *
  * @supported_params:	Bitmask of supported parameters, see QCFG_*.
  *
+ * @xdp_unreadable:	Native XDP programs may run on queues with unreadable
+ *			memory providers. The driver builds XDP buffers with
+ *			xdp_buff_add_frag(), and drops XDP_TX of a buffer for
+ *			which xdp_buff_is_frag_unreadable() is true.
+ *
  * Note that @ndo_queue_mem_alloc and @ndo_queue_mem_free may be called while
  * the interface is closed. @ndo_queue_start and @ndo_queue_stop will only
  * be called for an interface which is open.

include/net/netdev_queues.h struct netdev_queue_mgmt_ops { · line 237

Quoted 4 lines
include/net/netdev_queues.h +1 -0
@@ -229,6 +234,7 @@ struct netdev_queue_mgmt_ops {
 				    struct netlink_ext_ack *extack);
 
 	unsigned int supported_params;
+	bool xdp_unreadable;

Any reason this is not a new QCFG_ in supported_params ?

#7 lore raw

PATCH 4/7 eth: fbnic: Run XDP on provider queues

In reply to Björn Töpel

fbnic keeps headers in normal pages and attaches provider payload as XDP fragments. The payload is mapped only for receive and must not be sent with XDP_TX.

Drop XDP_TX while unreadable fragments remain and declare native XDP support on provider queues. Normal page-backed packets keep their existing behavior.

---
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +3 -1
index 07d4b2e92362..52f461bdc62a 100644
@@ -1130,7 +1130,8 @@ static long fbnic_pkt_tx(struct fbnic_napi_vector *nv,
 		frag = &shinfo->frags[0];
 	}
 
-	if (fbnic_desc_unused(ring) < nsegs) {
+	if (unlikely(xdp_buff_is_frag_unreadable(&pkt->buff)) ||
+	    fbnic_desc_unused(ring) < nsegs) {
 		u64_stats_update_begin(&ring->stats.syncp);
 		ring->stats.dropped++;
 		u64_stats_update_end(&ring->stats.syncp);
@@ -3124,4 +3125,5 @@ const struct netdev_queue_mgmt_ops fbnic_queue_mgmt_ops = {
 	.ndo_default_qcfg	= fbnic_default_qcfg,
 	.ndo_validate_qcfg	= fbnic_validate_qcfg,
 	.supported_params	= QCFG_RX_PAGE_SIZE,
+	.xdp_unreadable		= true,
 };
Signature · 1 line
2.56.1
#8 lore raw

PATCH 5/7 netdevsim: Run native XDP next to devmem

In reply to Björn Töpel

The attachment rules need a test driver that accepts native XDP and devmem bindings together. netdevsim runs XDP on ordinary skbs sent by its peer, not on provider payload.

Declare native XDP support alongside devmem bindings so selftests can check both attach orders. This tests configuration, not access to provider data.

---
 drivers/net/netdevsim/netdev.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/netdevsim/netdev.c +2 -0
index 0372a9bd63bb..3d1657d24f60 100644
@@ -880,6 +880,8 @@ static const struct netdev_queue_mgmt_ops nsim_queue_mgmt_ops = {
 	.ndo_queue_mem_free	= nsim_queue_mem_free,
 	.ndo_queue_start	= nsim_queue_start,
 	.ndo_queue_stop		= nsim_queue_stop,
+	/* XDP runs on the skbs of the peer, never on provider memory */
+	.xdp_unreadable		= true,
 };
 
 static ssize_t
Signature · 1 line
2.56.1
#9 lore raw

PATCH 6/7 selftests: net: Test XDP with devmem bindings

In reply to Björn Töpel

The XDP and devmem attachment rules must work in either order. A native program with frags support should coexist with a devmem RX binding; generic and single-buffer programs must remain rejected.

Test these rules on netdevsim with header split enabled. The test does not send packets with provider fragments.

---
 tools/testing/selftests/net/nl_netdev.py | 54 +++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 2 deletions(-)
tools/testing/selftests/net/nl_netdev.py +52 -2
index dd0321c0b463..15326fb66362 100755
@@ -13,7 +13,8 @@ import struct
 from os import system
 from lib.py import ksft_run, ksft_exit, KsftFailEx, KsftSkipEx
 from lib.py import ksft_eq, ksft_ge, ksft_ne, ksft_raises, ksft_busy_wait
-from lib.py import EthtoolFamily, NetdevFamily, NetdevSimDev, NlError, defer, ip
+from lib.py import CmdExitFailure, EthtoolFamily, NetdevFamily, NetdevSimDev
+from lib.py import NlError, defer, ip
 
 
 def empty_check(nf) -> None:
@@ -483,6 +484,54 @@ def devmem_bind_tx_unregister_check(_nf) -> None:
     nf_priv.close()
 
 
+def devmem_xdp_check(_nf) -> None:
+    """Native XDP with frags runs next to a devmem binding, in either order."""
+    prog = os.path.join(os.path.dirname(os.path.abspath(__file__)),
+                        "lib", "xdp_dummy.bpf.o")
+    if not os.path.exists(prog):
+        raise KsftSkipEx(f"{prog} is not built")
+
+    dmabuf_fd = _create_udmabuf()
+    defer(os.close, dmabuf_fd)
+    nf_priv = NetdevFamily()
+    defer(nf_priv.close)
+    nsimdev = NetdevSimDev(queue_count=2)
+    defer(nsimdev.remove)
+    nsim = nsimdev.nsims[0]
+    dev = nsim.ifname
+
+    ip(f"link set dev {dev} up")
+    with EthtoolFamily() as ef:
+        ef.rings_set({"header": {"dev-index": nsim.ifindex},
+                      "tcp-data-split": "enabled"})
+
+    # A generic program keeps the provider away
+    ip(f"link set dev {dev} xdpgeneric obj {prog} sec xdp.frags")
+    with ksft_raises(NlError) as cm:
+        nf_priv.bind_rx({"ifindex": nsim.ifindex, "fd": dmabuf_fd,
+                         "queues": [{"id": 1, "type": "rx"}]})
+    ksft_eq(cm.exception.nl_msg.error, -errno.EEXIST)
+    ip(f"link set dev {dev} xdpgeneric off")
+
+    # A native program first, then the provider
+    ip(f"link set dev {dev} xdpdrv obj {prog} sec xdp.frags")
+    _devmem_bind(nf_priv.bind_rx, {
+        "ifindex": nsim.ifindex,
+        "fd": dmabuf_fd,
+        "queues": [{"id": 1, "type": "rx"}],
+    })
+    ip(f"link set dev {dev} xdpdrv off")
+
+    # The provider first: native programs with frags only. Header split
+    # already refuses single-buffer programs.
+    ip(f"link set dev {dev} xdpdrv obj {prog} sec xdp.frags")
+    ip(f"link set dev {dev} xdpdrv off")
+    with ksft_raises(CmdExitFailure):
+        ip(f"link set dev {dev} xdpdrv obj {prog} sec xdp")
+    with ksft_raises(CmdExitFailure):
+        ip(f"link set dev {dev} xdpgeneric obj {prog} sec xdp.frags")
+
+
 def main() -> None:
     """ Ksft boiler plate main """
     nf = NetdevFamily()
@@ -498,7 +547,8 @@ def main() -> None:
               page_pool_ifindex_leak_check,
               page_pool_stats_ifindex_check,
               devmem_bind_rx_unregister_check,
-              devmem_bind_tx_unregister_check
+              devmem_bind_tx_unregister_check,
+              devmem_xdp_check
               ],
              args=(nf, ))
     ksft_exit()
Signature · 1 line
2.56.1
#10 lore raw

Re: [PATCH net-next 6/7] selftests: net: Test XDP with devmem bindings

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests: net: devmem_xdp_check() fails instead of skipping without devmem --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests: net: devmem_xdp_check() fails instead of skipping without devmem
--

commit af2bb584e6a40d5bea937deb32aab6eee5f5687a
Author: Björn Töpel <[email protected]>

selftests: net: Test XDP with devmem bindings

This commit adds a selftest in nl_netdev.py verifying the attachment rules for native and generic XDP programs alongside devmem RX bindings on netdevsim.

Quoted 4 lines
tools/testing/selftests/net/nl_netdev.py +0 -0
index dd0321c0b463a..15326fb663624 100755

[ ... ]

Quoted 5 lines
tools/testing/selftests/net/nl_netdev.py +1 -0
@@ -483,6 +484,54 @@ def devmem_bind_tx_unregister_check(_nf) -> None:
     nf_priv.close()
 
 
+def devmem_xdp_check(_nf) -> None:

[ ... ]

Quoted 5 lines
tools/testing/selftests/net/nl_netdev.py +5 -0
+    # A generic program keeps the provider away
+    ip(f"link set dev {dev} xdpgeneric obj {prog} sec xdp.frags")
+    with ksft_raises(NlError) as cm:
+        nf_priv.bind_rx({"ifindex": nsim.ifindex, "fd": dmabuf_fd,
+                         "queues": [{"id": 1, "type": "rx"}]})

tools/testing/selftests/net/nl_netdev.py def devmem_bind_tx_unregister_check(_nf) -> None: · line 522

Quoted 10 lines
tools/testing/selftests/net/nl_netdev.py +10 -0
+    ksft_eq(cm.exception.nl_msg.error, -errno.EEXIST)
+    ip(f"link set dev {dev} xdpgeneric off")
+
+    # A native program first, then the provider
+    ip(f"link set dev {dev} xdpdrv obj {prog} sec xdp.frags")
+    _devmem_bind(nf_priv.bind_rx, {
+        "ifindex": nsim.ifindex,
+        "fd": dmabuf_fd,
+        "queues": [{"id": 1, "type": "rx"}],
+    })

[Severity: Low]
Does devmem_xdp_check() fail instead of skipping when CONFIG_NET_DEVMEM is disabled?

In netdev_nl_bind_rx_doit(), net_devmem_bind_dmabuf() runs before net_devmem_bind_dmabuf_to_queue() and returns -EOPNOTSUPP when CONFIG_NET_DEVMEM is disabled. Because the first nf_priv.bind_rx() call does not go through _devmem_bind(), ksft_eq() compares -EOPNOTSUPP against -EEXIST and sets KSFT_RESULT = False before the later _devmem_bind() call raises KsftSkipEx, causing ksft_run() to report a failure.

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

PATCH 7/7 selftests: net: Test XDP on io_uring RX

In reply to Björn Töpel

Native XDP must not access io_uring payload through normal packet helpers. Compare ordinary page-backed RX with an io_uring RX queue. Check header, fragment and boundary access, and verify the received bytes through recv() and RECV_ZC.

Probe support by binding io_uring first and attaching a frags-aware XDP_PASS program. Skip unsupported combinations, but fail later attach, verifier and data-path checks. Require non-linear packets on the selected queue. Use XDP links so the test cannot replace another program, and restore header split, RSS and the flow rule.

An AI review found that ignoring io_uring setup errors could turn them into capability skips. A process-local seccomp filter returning EPERM reproduced this. The new XDP test now treats setup errors as failures.

---
 MAINTAINERS                                   |   2 +
 .../selftests/drivers/net/hw/.gitignore       |   1 +
 .../testing/selftests/drivers/net/hw/Makefile |  10 +-
 tools/testing/selftests/drivers/net/hw/config |   5 +
 .../selftests/drivers/net/hw/iou-zcrx.c       | 242 +++++++++++++++++-
 .../selftests/drivers/net/hw/iou-zcrx.py      | 108 +++++++-
 .../selftests/drivers/net/hw/iou_zcrx.bpf.c   | 111 ++++++++
 .../selftests/drivers/net/hw/iou_zcrx.h       |  34 +++
 8 files changed, 492 insertions(+), 21 deletions(-)
 create mode 100644 tools/testing/selftests/drivers/net/hw/iou_zcrx.bpf.c
 create mode 100644 tools/testing/selftests/drivers/net/hw/iou_zcrx.h
MAINTAINERS +2 -0
index 32e218c0fe68..ea383dd81668 100644
@@ -14076,6 +14076,8 @@ T:	git https://github.com/isilence/linux.git zcrx/for-next
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git
 S:	Maintained
 F:	io_uring/zcrx.*
+F:	tools/testing/selftests/drivers/net/hw/iou-zcrx.*
+F:	tools/testing/selftests/drivers/net/hw/iou_zcrx.*
 
 IO_URING BPF-OPS
 M:	Pavel Begunkov <[email protected]>
tools/testing/selftests/drivers/net/hw/.gitignore +1 -0
index 911a9bfeaf41..085aa8b17afc 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 iou-zcrx
+iou_zcrx.skel.h
 ncdevmem
 tls_hw_offload
 toeplitz
tools/testing/selftests/drivers/net/hw/Makefile +8 -2
index 429cb545a4f0..d7a1fea08ea7 100644
@@ -10,7 +10,8 @@ HAS_IOURING_ZCRX := $(shell \
 	echo -e '#include <liburing.h>\n' \
 	     'void *func = (void *)io_uring_register_ifq;\n' \
 	     'int main() {return 0;}' | \
-	$(CC) -luring -x c - -o /dev/null 2>&1 && echo y)
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(USERCFLAGS) $(LDFLAGS) \
+	$(USERLDFLAGS) -x c - -o /dev/null -luring 2>&1 && echo y)
 
 ifeq ($(HAS_IOURING_ZCRX),y)
 COND_GEN_FILES += iou-zcrx
@@ -98,5 +99,10 @@ include ../../../net/ynl.mk
 include ../../../net/bpf.mk
 
 ifeq ($(HAS_IOURING_ZCRX),y)
-$(OUTPUT)/iou-zcrx: LDLIBS += -luring -lpthread
+$(OUTPUT)/iou-zcrx: CFLAGS += -I$(OUTPUT) -I$(SCRATCH_DIR)/include
+$(OUTPUT)/iou-zcrx: LDLIBS += -luring -lpthread -lelf -lz
+$(OUTPUT)/iou-zcrx: LOCAL_HDRS += iou_zcrx.h $(OUTPUT)/iou_zcrx.skel.h
+$(OUTPUT)/iou-zcrx: iou_zcrx.h $(OUTPUT)/iou_zcrx.skel.h $(BPFOBJ)
 endif
+
+$(OUTPUT)/iou_zcrx.bpf.o: iou_zcrx.h $(selfdir)/bpf/bpf_kfuncs.h
tools/testing/selftests/drivers/net/hw/config +5 -0
index 081155bf7847..ef77543b567f 100644
@@ -1,4 +1,6 @@
+CONFIG_BPF_JIT=y
 CONFIG_BPF_SYSCALL=y
+CONFIG_DEBUG_INFO_BTF=y
 CONFIG_FAIL_FUNCTION=y
 CONFIG_FAULT_INJECTION=y
 CONFIG_FAULT_INJECTION_DEBUG_FS=y
@@ -9,6 +11,7 @@ CONFIG_INET6_ESP_OFFLOAD=y
 CONFIG_INET_ESP=y
 CONFIG_INET_ESP_OFFLOAD=y
 CONFIG_IO_URING=y
+CONFIG_IO_URING_ZCRX=y
 CONFIG_IPV6=y
 CONFIG_IPV6_GRE=y
 CONFIG_IPV6_SIT=y
@@ -20,7 +23,9 @@ CONFIG_NET_IPGRE=y
 CONFIG_NET_IPGRE_DEMUX=y
 CONFIG_NET_IPIP=y
 CONFIG_NETKIT=y
+CONFIG_NET_RX_BUSY_POLL=y
 CONFIG_NET_SCH_INGRESS=y
+CONFIG_PAGE_POOL=y
 CONFIG_SYNC_FILE=y
 CONFIG_TLS=y
 CONFIG_TLS_DEVICE=y
tools/testing/selftests/drivers/net/hw/iou-zcrx.c +229 -13

370 lines not on this page · raw message

tools/testing/selftests/drivers/net/hw/iou-zcrx.py +102 -6

139 lines not on this page · raw message

tools/testing/selftests/drivers/net/hw/iou_zcrx.bpf.c new file +111 -0

111 lines not on this page · raw message

tools/testing/selftests/drivers/net/hw/iou_zcrx.h new file +34 -0
new file mode 100644
index 000000000000..c9749bc9a0a6
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef IOU_ZCRX_XDP_H
+#define IOU_ZCRX_XDP_H
+
+#include <linux/types.h>
+
+enum xdp_zcrx_location {
+	XDP_ZCRX_HEADER,
+	XDP_ZCRX_FRAG,
+	XDP_ZCRX_BOUNDARY,
+	XDP_ZCRX_LOCATIONS,
+};
+
+enum xdp_zcrx_op {
+	XDP_ZCRX_LOAD,
+	XDP_ZCRX_STORE,
+	XDP_ZCRX_DYN_READ,
+	XDP_ZCRX_DYN_WRITE,
+	XDP_ZCRX_SLICE,
+	XDP_ZCRX_SLICE_RDWR,
+	XDP_ZCRX_OPS,
+};
+
+struct xdp_zcrx_stats {
+	__u64 packets;
+	__u64 tested;
+	__u64 wrong_queue;
+	__u64 bad_headers;
+	__u64 linear_bytes;
+	__u64 frag_bytes;
+	__u64 errors[XDP_ZCRX_LOCATIONS][XDP_ZCRX_OPS];
+};
+
+#endif
Signature · 1 line
2.56.1
#12 lore raw

Re: [PATCH net-next 7/7] selftests: net: Test XDP on io_uring RX

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] selftests: drv-net: broken HDS check and restore in xdp_setup() - [Medium] selftests: drv-net…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] selftests: drv-net: broken HDS check and restore in xdp_setup()
- [Medium] selftests: drv-net: xdp_setup() leaves RSS table user-configured
--

commit b83bbac3d08069e4bdafe35dfbe5badfc8c67e47
Author: Björn Töpel <[email protected]>

selftests: net: Test XDP on io_uring RX

This commit adds a selftest that compares page-backed and io_uring-backed RX queues under a multi-buffer native XDP program, verifying packet memory access restrictions and payload reception via recv() and RECV_ZC.

Quoted 4 lines
tools/testing/selftests/drivers/net/hw/iou-zcrx.py +0 -0
index 5047fdb560057..e6b765c523ed3 100755

[ ... ]

Quoted 33 lines
tools/testing/selftests/drivers/net/hw/iou-zcrx.py +29 -0
@@ -243,6 +253,91 @@ def test_zcrx_large_chunks(cfg) -> None:
         cmd(tx_cmd, host=cfg.remote)
 
 
+def xdp_setup(cfg):
+    """Reserve one RX queue without changing unrelated RSS contexts."""
+    cfg.require_ipver('6')
+    dev = cfg.netnl.dev_get({'ifindex': cfg.ifindex})
+    if not {'basic', 'rx-sg'} <= set(dev.get('xdp-features', [])):
+        raise KsftSkipEx('Device lacks native multi-buffer XDP')
+    if 'xdp' in ip(f"-d link show dev {cfg.ifname}", json=True)[0]:
+        raise KsftSkipEx('An XDP program is already attached')
+
+    queues = cfg.netnl.queue_get({'ifindex': cfg.ifindex}, dump=True)
+    if any(key in q for q in queues
+           for key in ('io-uring', 'dmabuf', 'xsk', 'lease')):
+        raise KsftSkipEx('Device has an active queue provider or lease')
+
+    header = {'dev-index': cfg.ifindex}
+    try:
+        channels = cfg.ethnl.channels_get({'header': header})
+        rings = cfg.ethnl.rings_get({'header': header})
+        indir = cfg.ethnl.rss_get({'header': header})['indir']
+    except NlError as error:
+        if error.error != errno.EOPNOTSUPP:
+            raise
+        raise KsftSkipEx('Device lacks channels, rings or RSS support') from error
+    count = channels.get('combined-count', 0)
+    if count < 2:
+        raise KsftSkipEx('Test requires at least two combined channels')
+    if rings.get('tcp-data-split', 'unknown') == 'unknown':
+        raise KsftSkipEx('Device lacks TCP header/data split')
+

tools/testing/selftests/drivers/net/hw/iou-zcrx.py def test_zcrx_large_chunks(cfg) -> None: · line 294

Quoted 10 lines
tools/testing/selftests/drivers/net/hw/iou-zcrx.py +10 -0
+    _require_ntuple(cfg)
+    cfg.target = count - 1
+    old_rings = {'header': header,
+                 'tcp-data-split': rings['tcp-data-split']}
+    new_rings = {'header': header, 'tcp-data-split': 'enabled'}
+    if 'hds-thresh' in rings:
+        old_rings['hds-thresh'] = rings['hds-thresh']
+        new_rings['hds-thresh'] = 0
+    cfg.ethnl.rings_set(new_rings)
+    defer(cfg.ethnl.rings_set, old_rings)

[Severity: Medium]
Does saving and restoring rings['tcp-data-split'] here leave header/data split misconfigured or fail during cleanup?

On drivers such as fbnic and netdevsim, get_ringparam() reports tcp-data-split as 'enabled' even when dev->cfg->hds_config is ETHTOOL_TCP_DATA_SPLIT_UNKNOWN. Restoring rings['tcp-data-split'] instead of resetting 'tcp-data-split' to 'unknown' (as single() and rss() do earlier in this file) leaves dev->cfg->hds_config set to ETHTOOL_TCP_DATA_SPLIT_ENABLED after the test, causing dev_xdp_install() to reject subsequent single-buffer XDP programs with -EBUSY.

In addition, bnxt_get_ringparam() can report 'disabled' while bnxt_set_ringparam() rejects ETHTOOL_TCP_DATA_SPLIT_DISABLED with -EINVAL on cleanup, and mlx5e_ethtool_get_ringparam() leaves tcp_data_split at 'unknown' until explicitly enabled, causing the pre-check above to skip on mlx5.

tools/testing/selftests/drivers/net/hw/iou-zcrx.py def test_zcrx_large_chunks(cfg) -> None: · line 298

Quoted 4 lines
tools/testing/selftests/drivers/net/hw/iou-zcrx.py +4 -0
+
+    cfg.ethnl.rss_set({'header': header,
+                      'indir': [q if q != cfg.target else 0 for q in indir]})
+    defer(cfg.ethnl.rss_set, {'header': header, 'indir': indir})

[Severity: Medium]
Does restoring the saved indir list leave the RSS indirection table marked as user-configured?

In ethnl_rss_set(), passing a non-empty indir array sets dev->ethtool->rss_indir_user_size to a non-zero value rather than resetting it to 0. After test_zcrx_xdp() finishes (or skips in probe_xdp()), netif_is_rxfh_configured() stays true with queue count - 1 referenced in the table, so later tests or ethtool calls that reduce the channel count fail in ethtool_check_max_channel() with -EINVAL. Passing 'indir': None resets the indirection table to its default state.

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

Re: [PATCH net-next 7/7] selftests: net: Test XDP on io_uring RX

In reply to Björn Töpel
Quoted 301 lines · Björn Töpel

Native XDP must not access io_uring payload through normal packet helpers. Compare ordinary page-backed RX with an io_uring RX queue. Check header, fragment and boundary access, and verify the received bytes through recv() and RECV_ZC.

Probe support by binding io_uring first and attaching a frags-aware XDP_PASS program. Skip unsupported combinations, but fail later attach, verifier and data-path checks. Require non-linear packets on the selected queue. Use XDP links so the test cannot replace another program, and restore header split, RSS and the flow rule.

An AI review found that ignoring io_uring setup errors could turn them into capability skips. A process-local seccomp filter returning EPERM reproduced this. The new XDP test now treats setup errors as failures.

---
 MAINTAINERS                                   |   2 +
 .../selftests/drivers/net/hw/.gitignore       |   1 +
 .../testing/selftests/drivers/net/hw/Makefile |  10 +-
 tools/testing/selftests/drivers/net/hw/config |   5 +
 .../selftests/drivers/net/hw/iou-zcrx.c       | 242 +++++++++++++++++-
 .../selftests/drivers/net/hw/iou-zcrx.py      | 108 +++++++-
 .../selftests/drivers/net/hw/iou_zcrx.bpf.c   | 111 ++++++++
 .../selftests/drivers/net/hw/iou_zcrx.h       |  34 +++
 8 files changed, 492 insertions(+), 21 deletions(-)
 create mode 100644 tools/testing/selftests/drivers/net/hw/iou_zcrx.bpf.c
 create mode 100644 tools/testing/selftests/drivers/net/hw/iou_zcrx.h
MAINTAINERS +2 -0
index 32e218c0fe68..ea383dd81668 100644
@@ -14076,6 +14076,8 @@ T:	git https://github.com/isilence/linux.git zcrx/for-next
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git
 S:	Maintained
 F:	io_uring/zcrx.*
+F:	tools/testing/selftests/drivers/net/hw/iou-zcrx.*
+F:	tools/testing/selftests/drivers/net/hw/iou_zcrx.*
 
 IO_URING BPF-OPS
 M:	Pavel Begunkov <[email protected]>
tools/testing/selftests/drivers/net/hw/.gitignore +1 -0
index 911a9bfeaf41..085aa8b17afc 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 iou-zcrx
+iou_zcrx.skel.h
 ncdevmem
 tls_hw_offload
 toeplitz
tools/testing/selftests/drivers/net/hw/Makefile +8 -2
index 429cb545a4f0..d7a1fea08ea7 100644
@@ -10,7 +10,8 @@ HAS_IOURING_ZCRX := $(shell \
 	echo -e '#include <liburing.h>\n' \
 	     'void *func = (void *)io_uring_register_ifq;\n' \
 	     'int main() {return 0;}' | \
-	$(CC) -luring -x c - -o /dev/null 2>&1 && echo y)
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(USERCFLAGS) $(LDFLAGS) \
+	$(USERLDFLAGS) -x c - -o /dev/null -luring 2>&1 && echo y)
 
 ifeq ($(HAS_IOURING_ZCRX),y)
 COND_GEN_FILES += iou-zcrx
@@ -98,5 +99,10 @@ include ../../../net/ynl.mk
 include ../../../net/bpf.mk
 
 ifeq ($(HAS_IOURING_ZCRX),y)
-$(OUTPUT)/iou-zcrx: LDLIBS += -luring -lpthread
+$(OUTPUT)/iou-zcrx: CFLAGS += -I$(OUTPUT) -I$(SCRATCH_DIR)/include
+$(OUTPUT)/iou-zcrx: LDLIBS += -luring -lpthread -lelf -lz
+$(OUTPUT)/iou-zcrx: LOCAL_HDRS += iou_zcrx.h $(OUTPUT)/iou_zcrx.skel.h
+$(OUTPUT)/iou-zcrx: iou_zcrx.h $(OUTPUT)/iou_zcrx.skel.h $(BPFOBJ)
 endif
+
+$(OUTPUT)/iou_zcrx.bpf.o: iou_zcrx.h $(selfdir)/bpf/bpf_kfuncs.h
tools/testing/selftests/drivers/net/hw/config +5 -0
index 081155bf7847..ef77543b567f 100644
@@ -1,4 +1,6 @@
+CONFIG_BPF_JIT=y
 CONFIG_BPF_SYSCALL=y
+CONFIG_DEBUG_INFO_BTF=y
 CONFIG_FAIL_FUNCTION=y
 CONFIG_FAULT_INJECTION=y
 CONFIG_FAULT_INJECTION_DEBUG_FS=y
@@ -9,6 +11,7 @@ CONFIG_INET6_ESP_OFFLOAD=y
 CONFIG_INET_ESP=y
 CONFIG_INET_ESP_OFFLOAD=y
 CONFIG_IO_URING=y
+CONFIG_IO_URING_ZCRX=y
 CONFIG_IPV6=y
 CONFIG_IPV6_GRE=y
 CONFIG_IPV6_SIT=y
@@ -20,7 +23,9 @@ CONFIG_NET_IPGRE=y
 CONFIG_NET_IPGRE_DEMUX=y
 CONFIG_NET_IPIP=y
 CONFIG_NETKIT=y
+CONFIG_NET_RX_BUSY_POLL=y
 CONFIG_NET_SCH_INGRESS=y
+CONFIG_PAGE_POOL=y
 CONFIG_SYNC_FILE=y
 CONFIG_TLS=y
 CONFIG_TLS_DEVICE=y
tools/testing/selftests/drivers/net/hw/iou-zcrx.c +146 -0
index 91b025a8771e..f7e909fd6840 100644
@@ -16,6 +16,7 @@
 #include <linux/mman.h>
 #include <linux/errqueue.h>
 #include <linux/if_packet.h>
+#include <linux/if_link.h>
 #include <linux/ipv6.h>
 #include <linux/socket.h>
 #include <linux/sockios.h>
@@ -37,9 +38,13 @@
 #include <sys/un.h>
 #include <sys/wait.h>
 
+#include <bpf/bpf.h>
 #include <liburing.h>
 #include <ynl.h>
+#include "kselftest.h"
 #include "netdev-user.h"
+#include "iou_zcrx.h"
+#include "iou_zcrx.skel.h"
 
 #define SKIP_CODE	42
 
@@ -89,7 +94,11 @@ static struct sockaddr_in6 cfg_addr;
 static unsigned int cfg_rx_buf_len;
 static bool cfg_dry_run;
 static int cfg_num_threads = 1;
+static bool cfg_xdp;
+static bool cfg_pages;
 
+static struct iou_zcrx *xdp_skel;
+static int xdp_link = -1;
 static char *payload;
 
 #define MAX_CONNS_PER_THREAD	64
@@ -154,6 +163,139 @@ static inline size_t get_refill_ring_size(unsigned int rq_entries)
 	return ALIGN_UP(size, page_size);
 }
 
+static int probe_xdp(void)
+{
+	const struct bpf_insn insns[] = {
+		{
+			.code = BPF_ALU64 | BPF_MOV | BPF_K,
+			.dst_reg = BPF_REG_0,
+			.imm = XDP_PASS,
+		},
+		{ .code = BPF_JMP | BPF_EXIT },
+	};
+	LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
+		    .prog_flags = BPF_F_XDP_HAS_FRAGS);
+	LIBBPF_OPTS(bpf_link_create_opts, link_opts,
+		    .flags = XDP_FLAGS_DRV_MODE);
+	unsigned int ifindex;
+	int prog_fd, link_fd;
+
+	ifindex = if_nametoindex(cfg_ifname);
+	if (!ifindex) {
+		error(0, errno, "if_nametoindex()");
+		return EXIT_FAILURE;
+	}
+	prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, "zcrx_probe", "GPL",
+				insns, ARRAY_SIZE(insns), &load_opts);
+	if (prog_fd < 0) {
+		error(0, -prog_fd, "load XDP probe");
+		return EXIT_FAILURE;
+	}
+
+	/* The io_uring queue is already registered; never replace a program. */
+	link_fd = bpf_link_create(prog_fd, ifindex, BPF_XDP, &link_opts);
+	close(prog_fd);
+	if (link_fd >= 0) {
+		close(link_fd);
+		return 0;
+	}
+	if (link_fd == -EBUSY || link_fd == -EOPNOTSUPP) {
+		printf("Native XDP with ZCRX is not supported: %s\n",
+		       strerror(-link_fd));
+		return SKIP_CODE;
+	}
+	error(0, -link_fd, "attach XDP probe");
+	return EXIT_FAILURE;
+}
+
+static void setup_xdp(void)
+{
+	LIBBPF_OPTS(bpf_link_create_opts, opts,
+		    .flags = XDP_FLAGS_DRV_MODE);
+	unsigned int ifindex;
+	int prog_fd;
+
+	ifindex = if_nametoindex(cfg_ifname);
+	if (!ifindex)
+		error(1, errno, "if_nametoindex()");
+	xdp_skel = iou_zcrx__open();
+	if (!xdp_skel)
+		error(1, errno, "iou_zcrx__open()");
+	xdp_skel->bss->port = cfg_port;
+	xdp_skel->bss->queue_id = cfg_queue_id;
+	xdp_skel->bss->unreadable = !cfg_pages;
+	if (iou_zcrx__load(xdp_skel))
+		error(1, errno, "iou_zcrx__load()");
+
+	/* A link cannot replace another program and closes with the process. */
+	prog_fd = bpf_program__fd(xdp_skel->progs.iou_zcrx);
+	xdp_link = bpf_link_create(prog_fd, ifindex, BPF_XDP, &opts);
+	if (xdp_link < 0)
+		error(1, errno, "attach native XDP");
+}
+
+static void check_xdp(void)
+{
+	static const char * const locations[] = {
+		"header", "fragment", "boundary",
+	};
+	static const char * const ops[] = {
+		"load", "store", "dynptr read", "dynptr write",
+		"slice", "slice rdwr",
+	};
+	struct xdp_zcrx_stats sum = {}, *all, *s;
+	int ncpus, cpu, loc, op;
+	__u32 key = 0;
+	bool failed;
+
+	close(xdp_link);
+	xdp_link = -1;
+	ncpus = libbpf_num_possible_cpus();
+	if (ncpus < 0)
+		error(1, 0, "libbpf_num_possible_cpus(): %d", ncpus);
+	all = calloc(ncpus, sizeof(*all));
+	if (!all)
+		error(1, errno, "calloc XDP statistics");
+	if (bpf_map_lookup_elem(bpf_map__fd(xdp_skel->maps.stats), &key, all))
+		error(1, errno, "read XDP statistics");
+
+	for (cpu = 0; cpu < ncpus; cpu++) {
+		s = &all[cpu];
+		sum.packets += s->packets;
+		sum.tested += s->tested;
+		sum.wrong_queue += s->wrong_queue;
+		sum.bad_headers += s->bad_headers;
+		sum.linear_bytes += s->linear_bytes;
+		sum.frag_bytes += s->frag_bytes;
+		for (loc = 0; loc < XDP_ZCRX_LOCATIONS; loc++) {
+			for (op = 0; op < XDP_ZCRX_OPS; op++)
+				sum.errors[loc][op] += s->errors[loc][op];
+		}
+	}
+	free(all);
+
+	printf("XDP %s: queue=%d packets=%llu tested=%llu\n",
+	       cfg_pages ? "pages" : "io_uring", cfg_queue_id,
+	       sum.packets, sum.tested);
+	printf("XDP geometry: linear=%llu frags=%llu\n",
+	       sum.linear_bytes, sum.frag_bytes);
+	printf("XDP errors: wrong_queue=%llu bad_headers=%llu\n",
+	       sum.wrong_queue, sum.bad_headers);
+	failed = !sum.tested || sum.wrong_queue || sum.bad_headers;
+	for (loc = 0; loc < XDP_ZCRX_LOCATIONS; loc++) {
+		for (op = 0; op < XDP_ZCRX_OPS; op++) {
+			if (!sum.errors[loc][op])
+				continue;
+			fprintf(stderr, "%s %s: %llu failures\n",
+				locations[loc], ops[op], sum.errors[loc][op]);
+			failed = true;
+		}
+	}
+	iou_zcrx__destroy(xdp_skel);
+	if (failed)
+		error(1, 0, "XDP access checks failed");
+}
+
 static void setup_zcrx(struct thread_ctx *ctx)
 {
 	unsigned int rq_entries = AREA_SIZE / page_size;
@@ -217,6 +359,10 @@ static void setup_zcrx(struct thread_ctx *ctx)
 	};
 
 	ret = io_uring_register_ifq(&ctx->ring, (void *)&reg);
+	if (cfg_xdp && cfg_dry_run && ret == -EOPNOTSUPP) {
+		puts("Interface-bound ZCRX is not supported");
+		exit(SKIP_CODE);
+	}
 	if (cfg_rx_buf_len && (ret == -EINVAL || ret == -EOPNOTSUPP ||
 			       ret == -ERANGE)) {
 		printf("Large chunks are not supported %i\n", ret);
@@ -258,6 +404,34 @@ static void add_recvzc_oneshot(struct thread_ctx *ctx, int conn_idx, size_t len)
 	sqe->user_data = conn_idx;
 }
 

[..]

tools/testing/selftests/drivers/net/hw/iou-zcrx.c static void add_recvzc_oneshot(struct thread_ctx *ctx, int conn_idx, size_t len) · line 418

Quoted 12 lines
tools/testing/selftests/drivers/net/hw/iou-zcrx.c +12 -0
+static void verify_data(struct thread_ctx *ctx, int conn_idx,
+			const char *data, size_t len)
+{
+	size_t offset = ctx->received[conn_idx];
+	size_t i;
+
+	for (i = 0; i < len; i++) {
+		if (data[i] != payload[offset + i])
+			error(1, 0, "payload mismatch at %zu", i);
+	}
+	ctx->received[conn_idx] += len;
+}

Move some of the refactoring/fixes out into a separate patch?

But in general feels like a wrong test to extend.. Why not call ncdevmem somewhere in python wrapper (and attach xdp prog that you have in this patch)? To decouple xdp testing from the entity that generates/receives unreadable frags.