Skip to contentSkip to conversation

netkit: Support for io_uring zero-copy and AF_XDP

PATCH Applied Daniel Borkmann 5 participants 31 messages Apr 2 – May 20

Subsystems (12)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

v11 cover netkit: Support for io_uring zero-copy and AF_XDP

Containers use virtual netdevs to route traffic from a physical netdev in the host namespace. They do not have access to the physical netdev in the host and thus can't use memory providers or AF_XDP that require reconfiguring/restarting queues in the physical netdev.

This patchset adds the concept of queue leasing to virtual netdevs that allow containers to use memory providers and AF_XDP at native speed. Leased queues are bound to a real queue in a physical netdev and act as a proxy.

Memory providers and AF_XDP operations take an ifindex and queue id, so containers would pass in an ifindex for a virtual netdev and a queue id of a leased queue, which then gets proxied to the underlying real queue.

We have implemented support for this concept in netkit and tested the latter against Nvidia ConnectX-6 (mlx5) as well as Broadcom BCM957504 (bnxt_en) 100G NICs. For more details see the individual patches.

v10->v11:
- Fix missing mp_ops->uninstall upon unlease path (Gemini)
- Fix dma device retrieval on tx queue when rx queue is leased (Gemini)
- Rework ethtool channel checks to check rx/tx individually (Gemini)
- The remainder of the Gemini findings were non-issues
- Add more extensive net selftests around queue leasing corner cases
- Rebase and retested everything with mlx5 + bnxt_en
v9->v10:
- Fix netkit selftest ruff checks (Jakub)
- Pass in net arg in netdev_put_lock (Gemini)
- Make the ethtool channel busy checks direction-aware (Gemini)
- Fix list corruption in netkit_check_lease_unregister (Gemini)
- Fix netkit_set_headroom in single device mode (Gemini)
- Fix xsk_clear_pool_at_qid bounds to use num_{rx,tx}_queues (Gemini)
- Fix netkit xsk teardown state checks (Gemini)
- The remainder of the Gemini findings were non-issues
- Rebase and retested everything with mlx5 + bnxt_en
v8->v9:
- Use ynl --family netdev in commit desc (Jakub)
- Update docs and comments about locking (Jakub)
- Propagate extack to the driver in ndo_queue_create (Jakub)
- Move function to net/core/dev.h core where possible (Jakub)
- Add comment about lease in netdev_rx_queue struct (Jakub)
- Add min: 0 check to netns id in policy (Jakub)
- Drop ifindex == ifindex_lease test in netdev_nl_queue_create_doit (Jakub)
- Detailed extack errors in netkit's ndo_queue_create (Jakub)
- Refactor lease dump into own function in netdev_nl_queue_fill_one (Jakub)
- Dump mp and xsk info for both queues in netdev_nl_queue_fill_one (Jakub)
- Replace the net_ prefix with netif_ for net_mp_{open,close}_rxq (Jakub)
- Remove ifq_idx naming cleanup from net_mp_{open,close}_rxq (Jakub)
- Rework the mp entry points to have explicit code that deals with the lease (Jakub)
- Fix locking in netdev_queue_get_dma_dev (Jakub)
- Remove unneeded carrier flap in netkit_queue_create (Jakub)
- Drop base net selftests that were already merged
- Add more nekit queue leasing coverage of base functionality (Jakub)
- Remove any mp leftovers upon unlease found via test cases
- Rebase and retested everything with mlx5 + bnxt_en
v7->v8:
- Rework and refactor net_mp_{open,close}_rxq patch (Jakub)
- Moved queue lease netlink command from env into test (Jakub)
- Support netns_id also in queue create call and not only dump
- Rebase and retested everything with mlx5 + bnxt_en
v6->v7:
- Add xsk_dev_queue_valid real_num_rx_queues check given bound xs->queue_id could be from a TX queue (Claude)
- Fix up exception path in queue leasing selftest (Claude)
- Rebase and retested everything with mlx5 + bnxt_en
v5->v6:
- Fix nest_queue test in netdev_nl_queue_fill_one (Jakub/Claude)
- Fix netdev notifier locking leak (Jakub/Claude)
- Drop NETREG_UNREGISTERING WARN_ON_ONCE to avoid confusion (Stan)
- Remove slipped-in .gitignore cruft in net selftest (Stan)
- Fix Pylint warnings in net selftest (Jakub)
- Rebase and retested everything with mlx5 + bnxt_en
v4->v5:
- Rework of the core API into queue-create op (Jakub)
- Rename from queue peering to queue leasing (Jakub)
- Add net selftests for queue leasing (Stan, Jakub)
- Move netkit_queue_get_dma_dev into core (Jakub)
- Dropped netkit_get_channels (Jakub)
- Moved ndo_queue_create back to return index or error (Jakub)
- Inline __netdev_rx_queue_{peer,unpeer} helpers (Jakub)
- Adding helpers in patches where they are used (Jakub)
- Undo inline for netdev_put_lock (Jakub)
- Factoring out checks whether device can lease (Jakub)
- Fix up return codes in netdev_nl_bind_queue_doit (Jakub)
- Reject when AF_XDP or mp already bound (Jakub)
- Switch some error cases to NL_SET_BAD_ATTR() (Jakub)
- Rebase and retested everything with mlx5 + bnxt_en
v3->v4:
- ndo_queue_create store dst queue via arg (Nikolay)
- Small nits like a spelling issue + rev xmas (Nikolay)
- admin-perm flag in bind-queue spec (Jakub)
- Fix potential ABBA deadlock situation in bind (Jakub, Paolo, Stan)
- Add a peer dev_tracker to not reuse the sysfs one (Jakub)
- New patch (12/14) to handle the underlying device going away (Jakub)
- Improve commit message on queue-get (Jakub)
- Do not expose phys dev info from container on queue-get (Jakub)
- Add netif_put_rx_queue_peer_locked to simplify code (Stan)
- Rework xsk handling to simplify the code and drop a few patches
- Rebase and retested everything with mlx5 + bnxt_en
v2->v3:
- Use netdev_ops_assert_locked instead of netdev_assert_locked (syzbot)
- Add missing netdev_lockdep_set_classes in netkit
v1->v2:
- Removed bind sample ynl code (Stan)
- Reworked netdev locking to have consistent order (Stan, Kuba)
- Return 'not supported' in API patch (Stan)
- Improved ynl documentation (Kuba)
- Added 'max: s32-max' in ynl spec for ifindex (Kuba)
- Added also queue type in ynl to have user specify rx to make it obvious (Kuba)
- Use of netdev_hold (Kuba)
- Avoid static inlines from another header (Kuba)
- Squashed some commits (Kuba, Stan)
- Removed ndo_{peer,unpeer}_queues callback and simplified
code (Kuba)
- Improved commit messages (Toke, Kuba, Stan, zf)
- Got rid of locking genl_sk_priv_get (Stan)
- Removed af_xdp cleanup churn (Maciej)
- Added netdev locking asserts (Stan)
- Reject ethtool ioctl path queue resizing (Kuba)
- Added kdoc for ndo_queue_create (Stan)
- Uninvert logic in netkit single dev mode (Jordan)
- Added binding support for multiple queues

Daniel Borkmann (10):
net: Add queue-create operation
net: Implement netdev_nl_queue_create_doit
net: Add lease info to queue-get response
net, ethtool: Disallow leased real rxqs to be resized
net: Slightly simplify net_mp_{open,close}_rxq
xsk: Extend xsk_rcv_check validation
xsk: Proxy pool management for leased queues
netkit: Add single device mode for netkit
netkit: Add netkit notifier to check for unregistering devices
netkit: Add xsk support for af_xdp applications

David Wei (4):
net: Proxy netif_mp_{open,close}_rxq for leased queues
net: Proxy netdev_queue_get_dma_dev for leased queues
netkit: Implement rtnl_link_ops->alloc and ndo_queue_create selftests/net: Add queue leasing tests with netkit

 Documentation/netlink/specs/netdev.yaml       |   46 +
 Documentation/netlink/specs/rt-link.yaml      |   11 +
 Documentation/networking/netdevices.rst       |    6 +
 drivers/net/netkit.c                          |  412 ++++-
 include/linux/netdevice.h                     |   11 +-
 include/net/netdev_queues.h                   |   23 +-
 include/net/netdev_rx_queue.h                 |   29 +-
 include/net/page_pool/memory_provider.h       |    8 +-
 include/uapi/linux/if_link.h                  |    6 +
 include/uapi/linux/netdev.h                   |   11 +
 io_uring/zcrx.c                               |   12 +-
 net/core/dev.c                                |   18 +-
 net/core/dev.h                                |   12 +
 net/core/devmem.c                             |    6 +-
 net/core/netdev-genl-gen.c                    |   20 +
 net/core/netdev-genl-gen.h                    |    2 +
 net/core/netdev-genl.c                        |  238 ++-
 net/core/netdev_queues.c                      |  103 +-
 net/core/netdev_rx_queue.c                    |  205 ++-
 net/ethtool/channels.c                        |   28 +-
 net/ethtool/ioctl.c                           |   21 +-
 net/xdp/xsk.c                                 |   76 +-
 tools/include/uapi/linux/netdev.h             |   11 +
 .../testing/selftests/drivers/net/hw/Makefile |    1 +
 .../drivers/net/hw/lib/py/__init__.py         |    4 +-
 .../selftests/drivers/net/hw/nk_qlease.py     | 1407 +++++++++++++++++
 26 files changed, 2558 insertions(+), 169 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/hw/nk_qlease.py
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v11 1/14 net: Add queue-create operation

Add a ynl netdev family operation called queue-create that creates a new queue on a netdevice:

name: queue-create
attribute-set: queue
flags: [admin-perm]
do:
request:
attributes:
- ifindex
- type
- lease
reply: &queue-create-op
attributes:
- id

This is a generic operation such that it can be extended for various use cases in future. Right now it is mandatory to specify ifindex, the queue type which is enforced to rx and a lease. The newly created queue id is returned to the caller.

A queue from a virtual device can have a lease which refers to another queue from a physical device. This is useful for memory providers and AF_XDP operations which take an ifindex and queue id to allow applications to bind against virtual devices in containers. The lease couples both queues together and allows to proxy the operations from a virtual device in a container to the physical device.

In future, the nested lease attribute can be lifted and made optional for other use-cases such as dynamic queue creation for physical netdevs. The lack of lease and the specification of the physical device as an ifindex will imply that we need a real queue to be allocated. Similarly, the queue type enforcement to rx can then be lifted as well to support tx.

An early implementation had only driver-specific integration [0], but in order for other virtual devices to reuse, it makes sense to have this as a generic API in core net.

For leasing queues, the virtual netdev must have real_num_rx_queues less than num_rx_queues at the time of calling queue-create. The queue-type must be rx as only rx queues are supported for leasing for now. We also enforce that the queue-create ifindex must point to a virtual device, and that the nested lease attribute's ifindex must point to a physical device. The nested lease attribute set contains a netns-id attribute which is optional and can specify a netns-id relative to the caller's netns. It requires cap_net_admin and if the netns-id attribute is not specified, the lease ifindex will be retrieved from the current netns. Also, it is modeled as an s32 type similarly as done elsewhere in the stack.

---
 Documentation/netlink/specs/netdev.yaml | 46 +++++++++++++++++++++++++
 include/uapi/linux/netdev.h             | 11 ++++++
 net/core/netdev-genl-gen.c              | 20 +++++++++++
 net/core/netdev-genl-gen.h              |  2 ++
 net/core/netdev-genl.c                  |  5 +++
 tools/include/uapi/linux/netdev.h       | 11 ++++++
 6 files changed, 95 insertions(+)
Documentation/netlink/specs/netdev.yaml +46 -0
index 596c306ce52b..b93beb247a11 100644
@@ -339,6 +339,15 @@ attribute-sets:
         doc: XSK information for this queue, if any.
         type: nest
         nested-attributes: xsk-info
+      -
+        name: lease
+        doc: |
+          A queue from a virtual device can have a lease which refers to
+          another queue from a physical device. This is useful for memory
+          providers and AF_XDP operations which take an ifindex and queue id
+          to allow applications to bind against virtual devices in containers.
+        type: nest
+        nested-attributes: lease
   -
     name: qstats
     doc: |
@@ -537,6 +546,26 @@ attribute-sets:
         name: id
       -
         name: type
+  -
+    name: lease
+    attributes:
+      -
+        name: ifindex
+        doc: The netdev ifindex to lease the queue from.
+        type: u32
+        checks:
+          min: 1
+      -
+        name: queue
+        doc: The netdev queue to lease from.
+        type: nest
+        nested-attributes: queue-id
+      -
+        name: netns-id
+        doc: The network namespace id of the netdev.
+        type: s32
+        checks:
+          min: 0
   -
     name: dmabuf
     attributes:
@@ -686,6 +715,7 @@ operations:
             - dmabuf
             - io-uring
             - xsk
+            - lease
       dump:
         request:
           attributes:
@@ -797,6 +827,22 @@ operations:
         reply:
           attributes:
             - id
+    -
+      name: queue-create
+      doc: |
+        Create a new queue for the given netdevice. Whether this operation
+        is supported depends on the device and the driver.
+      attribute-set: queue
+      flags: [admin-perm]
+      do:
+        request:
+          attributes:
+            - ifindex
+            - type
+            - lease
+        reply: &queue-create-op
+          attributes:
+            - id
 
 kernel-family:
   headers: ["net/netdev_netlink.h"]
include/uapi/linux/netdev.h +11 -0
index e0b579a1df4f..7df1056a35fd 100644
@@ -160,6 +160,7 @@ enum {
 	NETDEV_A_QUEUE_DMABUF,
 	NETDEV_A_QUEUE_IO_URING,
 	NETDEV_A_QUEUE_XSK,
+	NETDEV_A_QUEUE_LEASE,
 
 	__NETDEV_A_QUEUE_MAX,
 	NETDEV_A_QUEUE_MAX = (__NETDEV_A_QUEUE_MAX - 1)
@@ -202,6 +203,15 @@ enum {
 	NETDEV_A_QSTATS_MAX = (__NETDEV_A_QSTATS_MAX - 1)
 };
 
+enum {
+	NETDEV_A_LEASE_IFINDEX = 1,
+	NETDEV_A_LEASE_QUEUE,
+	NETDEV_A_LEASE_NETNS_ID,
+
+	__NETDEV_A_LEASE_MAX,
+	NETDEV_A_LEASE_MAX = (__NETDEV_A_LEASE_MAX - 1)
+};
+
 enum {
 	NETDEV_A_DMABUF_IFINDEX = 1,
 	NETDEV_A_DMABUF_QUEUES,
@@ -228,6 +238,7 @@ enum {
 	NETDEV_CMD_BIND_RX,
 	NETDEV_CMD_NAPI_SET,
 	NETDEV_CMD_BIND_TX,
+	NETDEV_CMD_QUEUE_CREATE,
 
 	__NETDEV_CMD_MAX,
 	NETDEV_CMD_MAX = (__NETDEV_CMD_MAX - 1)
net/core/netdev-genl-gen.c +20 -0
index ba673e81716f..81aecb5d3bc5 100644
@@ -28,6 +28,12 @@ static const struct netlink_range_validation netdev_a_napi_defer_hard_irqs_range
 };
 
 /* Common nested types */
+const struct nla_policy netdev_lease_nl_policy[NETDEV_A_LEASE_NETNS_ID + 1] = {
+	[NETDEV_A_LEASE_IFINDEX] = NLA_POLICY_MIN(NLA_U32, 1),
+	[NETDEV_A_LEASE_QUEUE] = NLA_POLICY_NESTED(netdev_queue_id_nl_policy),
+	[NETDEV_A_LEASE_NETNS_ID] = NLA_POLICY_MIN(NLA_S32, 0),
+};
+
 const struct nla_policy netdev_page_pool_info_nl_policy[NETDEV_A_PAGE_POOL_IFINDEX + 1] = {
 	[NETDEV_A_PAGE_POOL_ID] = NLA_POLICY_FULL_RANGE(NLA_UINT, &netdev_a_page_pool_id_range),
 	[NETDEV_A_PAGE_POOL_IFINDEX] = NLA_POLICY_FULL_RANGE(NLA_U32, &netdev_a_page_pool_ifindex_range),
@@ -107,6 +113,13 @@ static const struct nla_policy netdev_bind_tx_nl_policy[NETDEV_A_DMABUF_FD + 1]
 	[NETDEV_A_DMABUF_FD] = { .type = NLA_U32, },
 };
 
+/* NETDEV_CMD_QUEUE_CREATE - do */
+static const struct nla_policy netdev_queue_create_nl_policy[NETDEV_A_QUEUE_LEASE + 1] = {
+	[NETDEV_A_QUEUE_IFINDEX] = NLA_POLICY_MIN(NLA_U32, 1),
+	[NETDEV_A_QUEUE_TYPE] = NLA_POLICY_MAX(NLA_U32, 1),
+	[NETDEV_A_QUEUE_LEASE] = NLA_POLICY_NESTED(netdev_lease_nl_policy),
+};
+
 /* Ops table for netdev */
 static const struct genl_split_ops netdev_nl_ops[] = {
 	{
@@ -205,6 +218,13 @@ static const struct genl_split_ops netdev_nl_ops[] = {
 		.maxattr	= NETDEV_A_DMABUF_FD,
 		.flags		= GENL_CMD_CAP_DO,
 	},
+	{
+		.cmd		= NETDEV_CMD_QUEUE_CREATE,
+		.doit		= netdev_nl_queue_create_doit,
+		.policy		= netdev_queue_create_nl_policy,
+		.maxattr	= NETDEV_A_QUEUE_LEASE,
+		.flags		= GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
+	},
 };
 
 static const struct genl_multicast_group netdev_nl_mcgrps[] = {
net/core/netdev-genl-gen.h +2 -0
index cffc08517a41..d71b435d72c1 100644
@@ -14,6 +14,7 @@
 #include <net/netdev_netlink.h>
 
 /* Common nested types */
+extern const struct nla_policy netdev_lease_nl_policy[NETDEV_A_LEASE_NETNS_ID + 1];
 extern const struct nla_policy netdev_page_pool_info_nl_policy[NETDEV_A_PAGE_POOL_IFINDEX + 1];
 extern const struct nla_policy netdev_queue_id_nl_policy[NETDEV_A_QUEUE_TYPE + 1];
 
@@ -36,6 +37,7 @@ int netdev_nl_qstats_get_dumpit(struct sk_buff *skb,
 int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info);
 int netdev_nl_napi_set_doit(struct sk_buff *skb, struct genl_info *info);
 int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info);
+int netdev_nl_queue_create_doit(struct sk_buff *skb, struct genl_info *info);
 
 enum {
 	NETDEV_NLGRP_MGMT,
net/core/netdev-genl.c +5 -0
index 470fabbeacd9..aae75431858d 100644
@@ -1120,6 +1120,11 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
 	return err;
 }
 
+int netdev_nl_queue_create_doit(struct sk_buff *skb, struct genl_info *info)
+{
+	return -EOPNOTSUPP;
+}
+
 void netdev_nl_sock_priv_init(struct netdev_nl_sock *priv)
 {
 	INIT_LIST_HEAD(&priv->bindings);
tools/include/uapi/linux/netdev.h +11 -0
index e0b579a1df4f..7df1056a35fd 100644
@@ -160,6 +160,7 @@ enum {
 	NETDEV_A_QUEUE_DMABUF,
 	NETDEV_A_QUEUE_IO_URING,
 	NETDEV_A_QUEUE_XSK,
+	NETDEV_A_QUEUE_LEASE,
 
 	__NETDEV_A_QUEUE_MAX,
 	NETDEV_A_QUEUE_MAX = (__NETDEV_A_QUEUE_MAX - 1)
@@ -202,6 +203,15 @@ enum {
 	NETDEV_A_QSTATS_MAX = (__NETDEV_A_QSTATS_MAX - 1)
 };
 
+enum {
+	NETDEV_A_LEASE_IFINDEX = 1,
+	NETDEV_A_LEASE_QUEUE,
+	NETDEV_A_LEASE_NETNS_ID,
+
+	__NETDEV_A_LEASE_MAX,
+	NETDEV_A_LEASE_MAX = (__NETDEV_A_LEASE_MAX - 1)
+};
+
 enum {
 	NETDEV_A_DMABUF_IFINDEX = 1,
 	NETDEV_A_DMABUF_QUEUES,
@@ -228,6 +238,7 @@ enum {
 	NETDEV_CMD_BIND_RX,
 	NETDEV_CMD_NAPI_SET,
 	NETDEV_CMD_BIND_TX,
+	NETDEV_CMD_QUEUE_CREATE,
 
 	__NETDEV_CMD_MAX,
 	NETDEV_CMD_MAX = (__NETDEV_CMD_MAX - 1)
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v11 2/14 net: Implement netdev_nl_queue_create_doit

In reply to Daniel Borkmann

Implement netdev_nl_queue_create_doit which creates a new rx queue in a virtual netdev and then leases it to a rx queue in a physical netdev.

Example with ynl client:

  # ynl --family netdev --output-json --do queue-create \
        --json '{"ifindex": 8, "type": "rx", "lease": {"ifindex": 4, "queue": {"type": "rx", "id": 15}}}'
  {'id': 1}

Note that the netdevice locking order is always from the virtual to the physical device.

---
 Documentation/networking/netdevices.rst |   6 +
 include/linux/netdevice.h               |   9 +-
 include/net/netdev_queues.h             |  19 ++-
 include/net/netdev_rx_queue.h           |  15 ++-
 net/core/dev.c                          |   8 ++
 net/core/dev.h                          |   5 +
 net/core/netdev-genl.c                  | 164 +++++++++++++++++++++++-
 net/core/netdev_queues.c                |  62 +++++++++
 net/core/netdev_rx_queue.c              |  46 ++++++-
 9 files changed, 323 insertions(+), 11 deletions(-)
Documentation/networking/netdevices.rst +6 -0
index 35704d115312..83e28b96884f 100644
@@ -329,6 +329,12 @@ by setting ``request_ops_lock`` to true. Code comments and docs refer
 to drivers which have ops called under the instance lock as "ops locked".
 See also the documentation of the ``lock`` member of struct net_device.
 
+There is also a case of taking two per-netdev locks in sequence when netdev
+queues are leased, that is, the netdev-scope lock is taken for both the
+virtual and the physical device. To prevent deadlocks, the virtual device's
+lock must always be acquired before the physical device's (see
+``netdev_nl_queue_create_doit``).
+
 In the future, there will be an option for individual
 drivers to opt out of using ``rtnl_lock`` and instead perform their control
 operations directly under the netdev instance lock.
include/linux/netdevice.h +8 -1
index e15367373f7c..e8aa9cc4075d 100644
@@ -2561,7 +2561,14 @@ struct net_device {
 	 * Also protects some fields in:
 	 *	struct napi_struct, struct netdev_queue, struct netdev_rx_queue
 	 *
-	 * Ordering: take after rtnl_lock.
+	 * Ordering:
+	 *
+	 * - take after rtnl_lock
+	 *
+	 * - for the case of netdev queue leasing, the netdev-scope lock is
+	 *   taken for both the virtual and the physical device; to prevent
+	 *   deadlocks, the virtual device's lock must always be acquired
+	 *   before the physical device's (see netdev_nl_queue_create_doit)
 	 */
 	struct mutex		lock;
 
include/net/netdev_queues.h +16 -3
index 95ed28212f4e..748b70552ed1 100644
@@ -150,6 +150,11 @@ enum {
  *			When NIC-wide config is changed the callback will
  *			be invoked for all queues.
  *
+ * @ndo_queue_create:	Create a new RX queue on a virtual device that will
+ *			be paired with a physical device's queue via leasing.
+ *			Return the new queue id on success, negative error
+ *			on failure.
+ *
  * @supported_params:	Bitmask of supported parameters, see QCFG_*.
  *
  * Note that @ndo_queue_mem_alloc and @ndo_queue_mem_free may be called while
@@ -178,6 +183,8 @@ struct netdev_queue_mgmt_ops {
 				     struct netlink_ext_ack *extack);
 	struct device *	(*ndo_queue_get_dma_dev)(struct net_device *dev,
 						 int idx);
+	int	(*ndo_queue_create)(struct net_device *dev,
+				    struct netlink_ext_ack *extack);
 
 	unsigned int supported_params;
 };
@@ -185,7 +192,7 @@ struct netdev_queue_mgmt_ops {
 void netdev_queue_config(struct net_device *dev, int rxq,
 			 struct netdev_queue_config *qcfg);
 
-bool netif_rxq_has_unreadable_mp(struct net_device *dev, int idx);
+bool netif_rxq_has_unreadable_mp(struct net_device *dev, unsigned int rxq_idx);
 
 /**
  * DOC: Lockless queue stopping / waking helpers.
@@ -374,5 +381,11 @@ static inline unsigned int netif_xmit_timeout_ms(struct netdev_queue *txq)
 	})
 
 struct device *netdev_queue_get_dma_dev(struct net_device *dev, int idx);
-
-#endif
+bool netdev_can_create_queue(const struct net_device *dev,
+			     struct netlink_ext_ack *extack);
+bool netdev_can_lease_queue(const struct net_device *dev,
+			    struct netlink_ext_ack *extack);
+bool netdev_queue_busy(struct net_device *dev, unsigned int idx,
+		       enum netdev_queue_type type,
+		       struct netlink_ext_ack *extack);
+#endif /* _LINUX_NET_QUEUES_H */
include/net/netdev_rx_queue.h +13 -2
index 08f81329fc11..1d41c253f0a3 100644
@@ -31,6 +31,14 @@ struct netdev_rx_queue {
 	struct napi_struct		*napi;
 	struct netdev_queue_config	qcfg;
 	struct pp_memory_provider_params mp_params;
+
+	/* If a queue is leased, then the lease pointer is always
+	 * valid. From the physical device it points to the virtual
+	 * queue, and from the virtual device it points to the
+	 * physical queue.
+	 */
+	struct netdev_rx_queue		*lease;
+	netdevice_tracker		lease_tracker;
 } ____cacheline_aligned_in_smp;
 
 /*
@@ -60,5 +68,8 @@ get_netdev_rx_queue_index(struct netdev_rx_queue *queue)
 }
 
 int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq);
-
-#endif
+void netdev_rx_queue_lease(struct netdev_rx_queue *rxq_dst,
+			   struct netdev_rx_queue *rxq_src);
+void netdev_rx_queue_unlease(struct netdev_rx_queue *rxq_dst,
+			     struct netdev_rx_queue *rxq_src);
+#endif /* _LINUX_NETDEV_RX_QUEUE_H */
net/core/dev.c +8 -0
index 4519f0e59beb..f0a83a09477e 100644
@@ -1121,6 +1121,14 @@ netdev_get_by_index_lock_ops_compat(struct net *net, int ifindex)
 	return __netdev_put_lock_ops_compat(dev, net);
 }
 
+struct net_device *
+netdev_put_lock(struct net_device *dev, struct net *net,
+		netdevice_tracker *tracker)
+{
+	netdev_tracker_free(dev, tracker);
+	return __netdev_put_lock(dev, net);
+}
+
 struct net_device *
 netdev_xa_find_lock(struct net *net, struct net_device *dev,
 		    unsigned long *index)
net/core/dev.h +5 -0
index 781619e76b3e..6516ce2b5517 100644
@@ -31,6 +31,8 @@ netdev_napi_by_id_lock(struct net *net, unsigned int napi_id);
 struct net_device *dev_get_by_napi_id(unsigned int napi_id);
 
 struct net_device *__netdev_put_lock(struct net_device *dev, struct net *net);
+struct net_device *netdev_put_lock(struct net_device *dev, struct net *net,
+				   netdevice_tracker *tracker);
 struct net_device *
 netdev_xa_find_lock(struct net *net, struct net_device *dev,
 		    unsigned long *index);
@@ -96,6 +98,9 @@ int netdev_queue_config_validate(struct net_device *dev, int rxq_idx,
 				 struct netdev_queue_config *qcfg,
 				 struct netlink_ext_ack *extack);
 
+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);
+
 /* netdev management, shared between various uAPI entry points */
 struct netdev_name_node {
 	struct hlist_node hlist;
net/core/netdev-genl.c +163 -1

170 lines not on this page · raw message

net/core/netdev_queues.c +62 -0

71 lines not on this page · raw message

net/core/netdev_rx_queue.c +42 -4

57 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v11 3/14 net: Add lease info to queue-get response

In reply to Daniel Borkmann

Populate nested lease info to the queue-get response that returns the ifindex, queue id with type and optionally netns id if the device resides in a different netns.

Example with ynl client when using AF_XDP via queue leasing:

  # ip a
  [...]
  4: enp10s0f0np0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 xdp/id:24 qdisc mq state UP group default qlen 1000
    link/ether e8:eb:d3:a3:43:f6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.2/24 scope global enp10s0f0np0
       valid_lft forever preferred_lft forever
    inet6 fe80::eaeb:d3ff:fea3:43f6/64 scope link proto kernel_ll
       valid_lft forever preferred_lft forever
  [...]
  # ethtool -i enp10s0f0np0
  driver: mlx5_core
  [...]
  # ynl --family netdev --output-json --do queue-get \
        --json '{"ifindex": 4, "id": 15, "type": "rx"}'
  {'id': 15,
   'ifindex': 4,
   'lease': {'ifindex': 8, 'netns-id': 0, 'queue': {'id': 1, 'type': 'rx'}},
   'napi-id': 8227,
   'type': 'rx',
   'xsk': {}}
  # ip netns list
  foo (id: 0)
  # ip netns exec foo ip a
  [...]
  8: nk@NONE: <BROADCAST,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
      link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
      inet6 fe80::200:ff:fe00:0/64 scope link proto kernel_ll
         valid_lft forever preferred_lft forever
  [...]
  # ip netns exec foo ethtool -i nk
  driver: netkit
  [...]
  # ip netns exec foo ls /sys/class/net/nk/queues/
  rx-0  rx-1  tx-0
  # ip netns exec foo ynl --family netdev --output-json --do queue-get \
        --json '{"ifindex": 8, "id": 1, "type": "rx"}'
  {"id": 1, "type": "rx", "ifindex": 8, "xsk": {}}

Note that the caller of netdev_nl_queue_fill_one() holds the netdevice lock. For the queue-get we do not lock both devices. When queues get {un,}leased, both devices are locked, thus if __netif_get_rx_queue_lease() returns a lease pointer, it points to a valid device. The netns-id is fetched via peernet2id_alloc() similarly as done in OVS.

---
 include/net/netdev_rx_queue.h | 14 ++++++++
 net/core/netdev-genl.c        | 66 ++++++++++++++++++++++++++++++++---
 net/core/netdev_rx_queue.c    | 54 ++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+), 4 deletions(-)
include/net/netdev_rx_queue.h +14 -0
index 1d41c253f0a3..7e98c679ea84 100644
@@ -67,6 +67,20 @@ get_netdev_rx_queue_index(struct netdev_rx_queue *queue)
 	return index;
 }
 
+enum netif_lease_dir {
+	NETIF_VIRT_TO_PHYS,
+	NETIF_PHYS_TO_VIRT,
+};
+
+struct netdev_rx_queue *
+__netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq,
+			   enum netif_lease_dir dir);
+
+struct netdev_rx_queue *
+netif_get_rx_queue_lease_locked(struct net_device **dev, unsigned int *rxq);
+void netif_put_rx_queue_lease_locked(struct net_device *orig_dev,
+				     struct net_device *dev);
+
 int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq);
 void netdev_rx_queue_lease(struct netdev_rx_queue *rxq_dst,
 			   struct netdev_rx_queue *rxq_src);
net/core/netdev-genl.c +62 -4

97 lines not on this page · raw message

net/core/netdev_rx_queue.c +54 -0

60 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 29 lines · On Fri, 3 Apr 2026 01:10:20 +0200 Daniel Borkmann wrote:
net/core/netdev_rx_queue.c +29 -0
+struct netdev_rx_queue *
+__netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq_idx,
+			   enum netif_lease_dir dir)
+{
+	struct net_device *orig_dev = *dev;
+	struct netdev_rx_queue *rxq = __netif_get_rx_queue(orig_dev, *rxq_idx);
+
+	if (rxq->lease) {
+		if (!netif_lease_dir_ok(orig_dev, dir))
+			return NULL;
+		rxq = rxq->lease;
+		*rxq_idx = get_netdev_rx_queue_index(rxq);
+		*dev = rxq->dev;
+	}
+	return rxq;
+}
+
+struct netdev_rx_queue *
+netif_get_rx_queue_lease_locked(struct net_device **dev, unsigned int *rxq_idx)
+{
+	struct net_device *orig_dev = *dev;
+	struct netdev_rx_queue *rxq;
+
+	/* Locking order is always from the virtual to the physical device
+	 * see netdev_nl_queue_create_doit().
+	 */
+	netdev_ops_assert_locked(orig_dev);
+	rxq = __netif_get_rx_queue_lease(dev, rxq_idx, NETIF_VIRT_TO_PHYS);
+	if (rxq && orig_dev != *dev)

net/core/netdev_rx_queue.c bool netif_rxq_is_leased(struct net_device *dev, unsigned int rxq_idx) · line 96

Quoted 10 lines
net/core/netdev_rx_queue.c +10 -0
+		netdev_lock(*dev);
+	return rxq;
+}
+
+void netif_put_rx_queue_lease_locked(struct net_device *orig_dev,
+				     struct net_device *dev)
+{
+	if (orig_dev != dev)
+		netdev_unlock(dev);
+}

Pretty sure I already complained about these ugly helpers. I'll try to find the time tomorrow to come up with something better.

author maintainer
#5 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 35 lines · Jakub Kicinski
Quoted 34 lines · On Fri, 3 Apr 2026 01:10:20 +0200 Daniel Borkmann wrote:
net/core/netdev_rx_queue.c +34 -0
+struct netdev_rx_queue *
+__netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq_idx,
+			   enum netif_lease_dir dir)
+{
+	struct net_device *orig_dev = *dev;
+	struct netdev_rx_queue *rxq = __netif_get_rx_queue(orig_dev, *rxq_idx);
+
+	if (rxq->lease) {
+		if (!netif_lease_dir_ok(orig_dev, dir))
+			return NULL;
+		rxq = rxq->lease;
+		*rxq_idx = get_netdev_rx_queue_index(rxq);
+		*dev = rxq->dev;
+	}
+	return rxq;
+}
+
+struct netdev_rx_queue *
+netif_get_rx_queue_lease_locked(struct net_device **dev, unsigned int *rxq_idx)
+{
+	struct net_device *orig_dev = *dev;
+	struct netdev_rx_queue *rxq;
+
+	/* Locking order is always from the virtual to the physical device
+	 * see netdev_nl_queue_create_doit().
+	 */
+	netdev_ops_assert_locked(orig_dev);
+	rxq = __netif_get_rx_queue_lease(dev, rxq_idx, NETIF_VIRT_TO_PHYS);
+	if (rxq && orig_dev != *dev)
+		netdev_lock(*dev);
+	return rxq;
+}
+
+void netif_put_rx_queue_lease_locked(struct net_device *orig_dev,
Quoted 8 lines

net/core/netdev_rx_queue.c bool netif_rxq_is_leased(struct net_device *dev, unsigned int rxq_idx) · line 96

Quoted 5 lines
net/core/netdev_rx_queue.c +5 -0
+				     struct net_device *dev)
+{
+	if (orig_dev != dev)
+		netdev_unlock(dev);
+}

Pretty sure I already complained about these ugly helpers. I'll try to find the time tomorrow to come up with something better.

Ok, sounds good. Happy to adapt if you find something better and then I'll work this into the series, and also integrate the things mentioned in my cover letter reply (netkit nl dump + additional tests).

Thanks,
Daniel

maintainer
#6 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 10 lines · On Wed, 8 Apr 2026 11:09:34 +0200 Daniel Borkmann wrote:
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+void netif_put_rx_queue_lease_locked(struct net_device *orig_dev,
+				     struct net_device *dev)
+{
+	if (orig_dev != dev)
+		netdev_unlock(dev);
+}  

Pretty sure I already complained about these ugly helpers. I'll try to find the time tomorrow to come up with something better.

Quoted 3 lines

Ok, sounds good. Happy to adapt if you find something better and then I'll work this into the series, and also integrate the things mentioned in my cover letter reply (netkit nl dump + additional tests).

Hi! How would you feel about something like the following on top?

--->8----------

net: remove the netif_get_rx_queue_lease_locked() helpers

The netif_get_rx_queue_lease_locked() API hides the locking and the descend onto the leased queue. Making the code harder to follow (at least to me). Remove the API and open code the descend a bit. Most of the code now looks like:

if (!leased)

     return __helper(x);

hw_rxq = ..
netdev_lock(hw_rxq->dev);
ret = __helper(x);
netdev_unlock(hw_rxq->dev);

return ret;

Of course if we have more code paths that need the wrapping we may need to revisit. For now, IMHO, having to know what netif_get_rx_queue_lease_locked() does is not worth the 20LoC it saves.

---
 include/net/netdev_rx_queue.h |  5 ---
 net/core/dev.h                |  1 +
 net/core/netdev-genl.c        | 59 +++++++++++++++++----------
 net/core/netdev_queues.c      | 14 ++++---
 net/core/netdev_rx_queue.c    | 48 +++++++---------------
 net/xdp/xsk.c                 | 77 ++++++++++++++++++++++-------------
 6 files changed, 111 insertions(+), 93 deletions(-)
include/net/netdev_rx_queue.h +0 -5
index 7e98c679ea84..9415a94d333d 100644
@@ -76,11 +76,6 @@ struct netdev_rx_queue *
 __netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq,
 			   enum netif_lease_dir dir);
 
-struct netdev_rx_queue *
-netif_get_rx_queue_lease_locked(struct net_device **dev, unsigned int *rxq);
-void netif_put_rx_queue_lease_locked(struct net_device *orig_dev,
-				     struct net_device *dev);
-
 int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq);
 void netdev_rx_queue_lease(struct netdev_rx_queue *rxq_dst,
 			   struct netdev_rx_queue *rxq_src);
net/core/dev.h +1 -0
index 95edb2d4eff8..376bac4a82da 100644
@@ -101,6 +101,7 @@ int netdev_queue_config_validate(struct net_device *dev, int rxq_idx,
 
 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);
 
 void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
 			      const struct pp_memory_provider_params *p);
net/core/netdev-genl.c +38 -21

87 lines not on this page · raw message

net/core/netdev_queues.c +9 -5
index 265161e12a9c..5597af86591b 100644
@@ -37,18 +37,22 @@ struct device *netdev_queue_get_dma_dev(struct net_device *dev,
 					unsigned int idx,
 					enum netdev_queue_type type)
 {
-	struct net_device *orig_dev = dev;
+	struct netdev_rx_queue *hw_rxq;
 	struct device *dma_dev;
 
 	/* Only RX side supports queue leasing today. */
 	if (type != NETDEV_QUEUE_TYPE_RX || !netif_rxq_is_leased(dev, idx))
 		return __netdev_queue_get_dma_dev(dev, idx);
-
-	if (!netif_get_rx_queue_lease_locked(&dev, &idx))
+	if (!netif_is_queue_leasee(dev))
 		return NULL;
 
-	dma_dev = __netdev_queue_get_dma_dev(dev, idx);
-	netif_put_rx_queue_lease_locked(orig_dev, dev);
+	hw_rxq = __netif_get_rx_queue(dev, idx)->lease;
+
+	netdev_lock(hw_rxq->dev);
+	idx = get_netdev_rx_queue_index(hw_rxq);
+	dma_dev = __netdev_queue_get_dma_dev(hw_rxq->dev, idx);
+	netdev_unlock(hw_rxq->dev);
+
 	return dma_dev;
 }
 
net/core/netdev_rx_queue.c +14 -34

93 lines not on this page · raw message

net/xdp/xsk.c +49 -28

104 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#7 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 34 lines · Jakub Kicinski
Quoted 10 lines · On Wed, 8 Apr 2026 11:09:34 +0200 Daniel Borkmann wrote:
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+void netif_put_rx_queue_lease_locked(struct net_device *orig_dev,
+				     struct net_device *dev)
+{
+	if (orig_dev != dev)
+		netdev_unlock(dev);
+}

Pretty sure I already complained about these ugly helpers. I'll try to find the time tomorrow to come up with something better.

Quoted 3 lines

Ok, sounds good. Happy to adapt if you find something better and then I'll work this into the series, and also integrate the things mentioned in my cover letter reply (netkit nl dump + additional tests).

Hi! How would you feel about something like the following on top?

--->8----------

net: remove the netif_get_rx_queue_lease_locked() helpers

The netif_get_rx_queue_lease_locked() API hides the locking and the descend onto the leased queue. Making the code harder to follow (at least to me). Remove the API and open code the descend a bit. Most of the code now looks like:

  if (!leased)
      return __helper(x);

  hw_rxq = ..
  netdev_lock(hw_rxq->dev);
  ret = __helper(x);
  netdev_unlock(hw_rxq->dev);
Quoted 8 lines

return ret;

Of course if we have more code paths that need the wrapping we may need to revisit. For now, IMHO, having to know what netif_get_rx_queue_lease_locked() does is not worth the 20LoC it saves.

Thanks for looking into it! That looks good to me. I've also retested that it still works.

Maybe small nits could be below to move the netif_is_queue_leasee into the netdev_rx_queue.h header since its used outside of core and it might be worth to also have the lock assertion in netdev_queue_get_dma_dev.

Do you want me to add your patch on top for a v12 of the series?

Thanks,
Daniel

 include/net/netdev_rx_queue.h | 2 ++
 net/core/dev.h                | 1 -
 net/core/netdev_queues.c      | 2 ++
 net/xdp/xsk.c                 | 2 --
 4 files changed, 4 insertions(+), 3 deletions(-)
include/net/netdev_rx_queue.h +2 -0
index 9415a94d333d..e9f7f098609b 100644
@@ -76,6 +76,8 @@ struct netdev_rx_queue *
 __netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq,
 			   enum netif_lease_dir dir);
 
+bool netif_is_queue_leasee(const struct net_device *dev);
+
 int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq);
 void netdev_rx_queue_lease(struct netdev_rx_queue *rxq_dst,
 			   struct netdev_rx_queue *rxq_src);
net/core/dev.h +0 -1
index 376bac4a82da..95edb2d4eff8 100644
@@ -101,7 +101,6 @@ int netdev_queue_config_validate(struct net_device *dev, int rxq_idx,
 
 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);
 
 void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
 			      const struct pp_memory_provider_params *p);
net/core/netdev_queues.c +2 -0
index 5597af86591b..73fb28087a93 100644
@@ -40,6 +40,8 @@ struct device *netdev_queue_get_dma_dev(struct net_device *dev,
 	struct netdev_rx_queue *hw_rxq;
 	struct device *dma_dev;
 
+	netdev_ops_assert_locked(dev);
+
 	/* Only RX side supports queue leasing today. */
 	if (type != NETDEV_QUEUE_TYPE_RX || !netif_rxq_is_leased(dev, idx))
 		return __netdev_queue_get_dma_dev(dev, idx);
net/xdp/xsk.c +0 -2
index 616cd7b42502..7da620aa11d6 100644
@@ -31,8 +31,6 @@
 #include <net/netdev_rx_queue.h>
 #include <net/xdp.h>
 
-#include "../core/dev.h"
-
 #include "xsk_queue.h"
 #include "xdp_umem.h"
 #include "xsk.h"
Signature · 1 line
2.43.0
author maintainer
#8 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 79 lines · Daniel Borkmann
Quoted 34 lines · Jakub Kicinski
Quoted 10 lines · On Wed, 8 Apr 2026 11:09:34 +0200 Daniel Borkmann wrote:
Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+void netif_put_rx_queue_lease_locked(struct net_device *orig_dev,
+                     struct net_device *dev)
+{
+    if (orig_dev != dev)
+        netdev_unlock(dev);
+}

Pretty sure I already complained about these ugly helpers. I'll try to find the time tomorrow to come up with something better.

Quoted 3 lines

Ok, sounds good. Happy to adapt if you find something better and then I'll work this into the series, and also integrate the things mentioned in my cover letter reply (netkit nl dump + additional tests).

Hi! How would you feel about something like the following on top?

--->8----------

net: remove the netif_get_rx_queue_lease_locked() helpers

The netif_get_rx_queue_lease_locked() API hides the locking and the descend onto the leased queue. Making the code harder to follow (at least to me). Remove the API and open code the descend a bit. Most of the code now looks like:

  if (!leased)
      return __helper(x);

  hw_rxq = ..
  netdev_lock(hw_rxq->dev);
  ret = __helper(x);
  netdev_unlock(hw_rxq->dev);

Quoted 8 lines

  return ret;

Of course if we have more code paths that need the wrapping we may need to revisit. For now, IMHO, having to know what netif_get_rx_queue_lease_locked() does is not worth the 20LoC it saves.

Thanks for looking into it! That looks good to me. I've also retested that it still works.

Maybe small nits could be below to move the netif_is_queue_leasee into the netdev_rx_queue.h header since its used outside of core and it might be worth to also have the lock assertion in netdev_queue_get_dma_dev.

Do you want me to add your patch on top for a v12 of the series?

Thanks,
Daniel

  include/net/netdev_rx_queue.h | 2 ++
  net/core/dev.h                | 1 -
  net/core/netdev_queues.c      | 2 ++
  net/xdp/xsk.c                 | 2 --
  4 files changed, 4 insertions(+), 3 deletions(-)
include/net/netdev_rx_queue.h +2 -0
index 9415a94d333d..e9f7f098609b 100644
@@ -76,6 +76,8 @@ struct netdev_rx_queue *
  __netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq,
                 enum netif_lease_dir dir);
 
+bool netif_is_queue_leasee(const struct net_device *dev);
+
  int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq);
  void netdev_rx_queue_lease(struct netdev_rx_queue *rxq_dst,
                 struct netdev_rx_queue *rxq_src);
net/core/dev.h +0 -0
index 376bac4a82da..95edb2d4eff8 100644

net/core/dev.h int netdev_queue_config_validate(struct net_device *dev, int rxq_idx, · line 104

Quoted 4 lines
net/core/dev.h +0 -1
@@ -101,7 +101,6 @@ int netdev_queue_config_validate(struct net_device *dev, int rxq_idx,
 
  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);

(Ok, if so then netif_rxq_is_leased would have to move too..
maybe its fine as-is then.)

Thanks,
Daniel

maintainer
#9 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 7 lines · On Thu, 9 Apr 2026 15:52:42 +0200 Daniel Borkmann wrote:
Quoted 7 lines
Quoted 1 line

Thanks for looking into it! That looks good to me. I've also retested that it still works.

Maybe small nits could be below to move the netif_is_queue_leasee into the netdev_rx_queue.h header since its used outside of core and it might be worth to also have the lock assertion in netdev_queue_get_dma_dev.

I suspected it may irk you :) No strong preference on the placement. We do include the ../core/dev.h in a couple of places but agreed that it is slightly ugly.

Quoted 1 line
Quoted 1 line

Do you want me to add your patch on top for a v12 of the series?

Yes, please. Let's get it into 7.1.

I think the test has to be reworked but of the available options seems like merging it as is and following up quickly is the best. I've only set up the container testing in our CI yesterday anyway so there may be more things that need changing in the test as we gain experience :S

author maintainer
#10 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Thu, 9 Apr 2026 15:52:42 +0200 Daniel Borkmann wrote:
Quoted 4 lines
Quoted 1 line

Thanks for looking into it! That looks good to me. I've also retested that it still works.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

Maybe small nits could be below to move the netif_is_queue_leasee into the netdev_rx_queue.h header since its used outside of core and it might be worth to also have the lock assertion in netdev_queue_get_dma_dev.

I suspected it may irk you :) No strong preference on the placement. We do include the ../core/dev.h in a couple of places but agreed that it is slightly ugly.

How you prefer, I think either is fine if we also do this from a couple of other places too.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Do you want me to add your patch on top for a v12 of the series?

Yes, please. Let's get it into 7.1.

I think the test has to be reworked but of the available options seems like merging it as is and following up quickly is the best. I've only set up the container testing in our CI yesterday anyway so there may be more things that need changing in the test as we gain experience :S

No objections obviously if you want to land as-is with your refactor on top. Then we'll follow-up with the selftest feedback + netkit nl dump.

Thanks,
Daniel

maintainer
#11 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 7 lines · On Thu, 9 Apr 2026 17:32:31 +0200 Daniel Borkmann wrote:
Quoted 4 lines

I think the test has to be reworked but of the available options seems like merging it as is and following up quickly is the best. I've only set up the container testing in our CI yesterday anyway so there may be more things that need changing in the test as we gain experience :S

No objections obviously if you want to land as-is with your refactor on top.

Done, please double check my work, there were some conflicts with net.

author maintainer
#12 lore raw

Re: [PATCH net-next v11 03/14] net: Add lease info to queue-get response

Quoted 10 lines · Jakub Kicinski
Quoted 7 lines · On Thu, 9 Apr 2026 17:32:31 +0200 Daniel Borkmann wrote:
Quoted 4 lines

I think the test has to be reworked but of the available options seems like merging it as is and following up quickly is the best. I've only set up the container testing in our CI yesterday anyway so there may be more things that need changing in the test as we gain experience :S

No objections obviously if you want to land as-is with your refactor on top.

Done, please double check my work, there were some conflicts with net.

Looks good to me, thanks a lot for everything!

author maintainer
#13 lore raw

PATCH v11 4/14 net, ethtool: Disallow leased real rxqs to be resized

In reply to Daniel Borkmann

Similar to AF_XDP, do not allow queues in a physical netdev to be resized by ethtool -L when they are leased. Cover channel resize paths (both netlink and ioctl) to reject resizing when the queues would be affected.

Given we need to have different checks for RX vs TX, detangle the code into a two-loop version rather than the range of new_combined + min(new_rx, new_tx) to old_combined + max(old_rx, old_tx).

---
 net/ethtool/channels.c | 28 +++++++++++++++++-----------
 net/ethtool/ioctl.c    | 21 ++++++++++++---------
 2 files changed, 29 insertions(+), 20 deletions(-)
net/ethtool/channels.c +17 -11
index 45232cf1c144..64ef8cff2005 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
-#include <net/xdp_sock_drv.h>
+#include <net/netdev_queues.h>
 
 #include "common.h"
 #include "netlink.h"
@@ -109,7 +109,7 @@ ethnl_set_channels_validate(struct ethnl_req_info *req_info,
 static int
 ethnl_set_channels(struct ethnl_req_info *req_info, struct genl_info *info)
 {
-	unsigned int from_channel, old_total, i;
+	unsigned int old_combined, old_rx, old_tx, i;
 	bool mod = false, mod_combined = false;
 	struct net_device *dev = req_info->dev;
 	struct ethtool_channels channels = {};
@@ -118,8 +118,9 @@ ethnl_set_channels(struct ethnl_req_info *req_info, struct genl_info *info)
 	int ret;
 
 	dev->ethtool_ops->get_channels(dev, &channels);
-	old_total = channels.combined_count +
-		    max(channels.rx_count, channels.tx_count);
+	old_combined = channels.combined_count;
+	old_rx = channels.rx_count;
+	old_tx = channels.tx_count;
 
 	ethnl_update_u32(&channels.rx_count, tb[ETHTOOL_A_CHANNELS_RX_COUNT],
 			 &mod);
@@ -169,14 +170,19 @@ ethnl_set_channels(struct ethnl_req_info *req_info, struct genl_info *info)
 	if (ret)
 		return ret;
 
-	/* Disabling channels, query zero-copy AF_XDP sockets */
-	from_channel = channels.combined_count +
-		       min(channels.rx_count, channels.tx_count);
-	for (i = from_channel; i < old_total; i++)
-		if (xsk_get_pool_from_qid(dev, i)) {
-			GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing zerocopy AF_XDP sockets");
+	/* ensure channels are not busy at the moment */
+	for (i = channels.combined_count + channels.rx_count;
+	     i < old_combined + old_rx; i++) {
+		if (netdev_queue_busy(dev, i, NETDEV_QUEUE_TYPE_RX,
+				      info->extack))
 			return -EINVAL;
-		}
+	}
+	for (i = channels.combined_count + channels.tx_count;
+	     i < old_combined + old_tx; i++) {
+		if (netdev_queue_busy(dev, i, NETDEV_QUEUE_TYPE_TX,
+				      info->extack))
+			return -EINVAL;
+	}
 
 	ret = dev->ethtool_ops->set_channels(dev, &channels);
 	return ret < 0 ? ret : 1;
net/ethtool/ioctl.c +12 -9
index 3c713a91ad0d..bd97f9b9bf18 100644
@@ -27,12 +27,12 @@
 #include <linux/net.h>
 #include <linux/pm_runtime.h>
 #include <linux/utsname.h>
+#include <linux/ethtool_netlink.h>
 #include <net/devlink.h>
 #include <net/ipv6.h>
-#include <net/xdp_sock_drv.h>
 #include <net/flow_offload.h>
 #include <net/netdev_lock.h>
-#include <linux/ethtool_netlink.h>
+#include <net/netdev_queues.h>
 
 #include "common.h"
 
@@ -2250,7 +2250,6 @@ static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
 						   void __user *useraddr)
 {
 	struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
-	u16 from_channel, to_channel;
 	unsigned int i;
 	int ret;
 
@@ -2284,13 +2283,17 @@ static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
 	if (ret)
 		return ret;
 
-	/* Disabling channels, query zero-copy AF_XDP sockets */
-	from_channel = channels.combined_count +
-		min(channels.rx_count, channels.tx_count);
-	to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
-	for (i = from_channel; i < to_channel; i++)
-		if (xsk_get_pool_from_qid(dev, i))
+	/* Disabling channels, query busy queues (AF_XDP, queue leasing) */
+	for (i = channels.combined_count + channels.rx_count;
+	     i < curr.combined_count + curr.rx_count; i++) {
+		if (netdev_queue_busy(dev, i, NETDEV_QUEUE_TYPE_RX, NULL))
 			return -EINVAL;
+	}
+	for (i = channels.combined_count + channels.tx_count;
+	     i < curr.combined_count + curr.tx_count; i++) {
+		if (netdev_queue_busy(dev, i, NETDEV_QUEUE_TYPE_TX, NULL))
+			return -EINVAL;
+	}
 
 	ret = dev->ethtool_ops->set_channels(dev, &channels);
 	if (!ret)
Signature · 1 line
2.43.0
author maintainer
#14 lore raw

PATCH v11 5/14 net: Slightly simplify net_mp_{open,close}_rxq

In reply to Daniel Borkmann

net_mp_open_rxq is currently not used in the tree as all callers are using __net_mp_open_rxq directly, and net_mp_close_rxq is only used once while all other locations use __net_mp_close_rxq.

Consolidate into a single API, netif_mp_{open,close}_rxq, using the netif_ prefix to indicate that the caller is responsible for locking.

---
 include/net/page_pool/memory_provider.h |  8 ++------
 io_uring/zcrx.c                         |  9 ++++++---
 net/core/devmem.c                       |  6 +++---
 net/core/netdev_rx_queue.c              | 23 ++---------------------
 4 files changed, 13 insertions(+), 33 deletions(-)
include/net/page_pool/memory_provider.h +2 -6
index ada4f968960a..255ce4cfd975 100644
@@ -23,14 +23,10 @@ bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr);
 void net_mp_niov_set_page_pool(struct page_pool *pool, struct net_iov *niov);
 void net_mp_niov_clear_page_pool(struct net_iov *niov);
 
-int net_mp_open_rxq(struct net_device *dev, unsigned ifq_idx,
-		    struct pp_memory_provider_params *p);
-int __net_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
+int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		      const struct pp_memory_provider_params *p,
 		      struct netlink_ext_ack *extack);
-void net_mp_close_rxq(struct net_device *dev, unsigned ifq_idx,
-		      struct pp_memory_provider_params *old_p);
-void __net_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
+void netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
 			const struct pp_memory_provider_params *old_p);
 
 /**
io_uring/zcrx.c +6 -3
index 62d693287457..d3ec63c83d0c 100644
@@ -552,8 +552,11 @@ static void io_close_queue(struct io_zcrx_ifq *ifq)
 	}
 
 	if (netdev) {
-		if (ifq->if_rxq != -1)
-			net_mp_close_rxq(netdev, ifq->if_rxq, &p);
+		if (ifq->if_rxq != -1) {
+			netdev_lock(netdev);
+			netif_mp_close_rxq(netdev, ifq->if_rxq, &p);
+			netdev_unlock(netdev);
+		}
 		netdev_put(netdev, &netdev_tracker);
 	}
 	ifq->if_rxq = -1;
@@ -841,7 +844,7 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx,
 		mp_param.rx_page_size = 1U << ifq->niov_shift;
 	mp_param.mp_ops = &io_uring_pp_zc_ops;
 	mp_param.mp_priv = ifq;
-	ret = __net_mp_open_rxq(ifq->netdev, reg.if_rxq, &mp_param, NULL);
+	ret = netif_mp_open_rxq(ifq->netdev, reg.if_rxq, &mp_param, NULL);
 	if (ret)
 		goto netdev_put_unlock;
 	netdev_unlock(ifq->netdev);
net/core/devmem.c +3 -3
index 69d79aee07ef..cde4c89bc146 100644
@@ -145,7 +145,7 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
 
 		rxq_idx = get_netdev_rx_queue_index(rxq);
 
-		__net_mp_close_rxq(binding->dev, rxq_idx, &mp_params);
+		netif_mp_close_rxq(binding->dev, rxq_idx, &mp_params);
 	}
 
 	percpu_ref_kill(&binding->ref);
@@ -163,7 +163,7 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
 	u32 xa_idx;
 	int err;
 
-	err = __net_mp_open_rxq(dev, rxq_idx, &mp_params, extack);
+	err = netif_mp_open_rxq(dev, rxq_idx, &mp_params, extack);
 	if (err)
 		return err;
 
@@ -176,7 +176,7 @@ int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
 	return 0;
 
 err_close_rxq:
-	__net_mp_close_rxq(dev, rxq_idx, &mp_params);
+	netif_mp_close_rxq(dev, rxq_idx, &mp_params);
 	return err;
 }
 
net/core/netdev_rx_queue.c +2 -21
index a4d8cad6db74..06ac3bd5507f 100644
@@ -200,7 +200,7 @@ int netdev_rx_queue_restart(struct net_device *dev, unsigned int rxq_idx)
 }
 EXPORT_SYMBOL_NS_GPL(netdev_rx_queue_restart, "NETDEV_INTERNAL");
 
-int __net_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
+int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		      const struct pp_memory_provider_params *p,
 		      struct netlink_ext_ack *extack)
 {
@@ -264,18 +264,7 @@ int __net_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 	return ret;
 }
 
-int net_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
-		    struct pp_memory_provider_params *p)
-{
-	int ret;
-
-	netdev_lock(dev);
-	ret = __net_mp_open_rxq(dev, rxq_idx, p, NULL);
-	netdev_unlock(dev);
-	return ret;
-}
-
-void __net_mp_close_rxq(struct net_device *dev, unsigned int ifq_idx,
+void netif_mp_close_rxq(struct net_device *dev, unsigned int ifq_idx,
 			const struct pp_memory_provider_params *old_p)
 {
 	struct netdev_queue_config qcfg[2];
@@ -305,11 +294,3 @@ void __net_mp_close_rxq(struct net_device *dev, unsigned int ifq_idx,
 	err = netdev_rx_queue_reconfig(dev, ifq_idx, &qcfg[0], &qcfg[1]);
 	WARN_ON(err && err != -ENETDOWN);
 }
-
-void net_mp_close_rxq(struct net_device *dev, unsigned ifq_idx,
-		      struct pp_memory_provider_params *old_p)
-{
-	netdev_lock(dev);
-	__net_mp_close_rxq(dev, ifq_idx, old_p);
-	netdev_unlock(dev);
-}
Signature · 1 line
2.43.0
author maintainer
#15 lore raw

PATCH v11 6/14 net: Proxy netif_mp_{open,close}_rxq for leased queues

In reply to Daniel Borkmann

From: David Wei <[email protected]>

When a process in a container wants to setup a memory provider, it will use the virtual netdev and a leased rxq, and call netif_mp_{open,close}_rxq to try and restart the queue. At this point, proxy the queue restart on the real rxq in the physical netdev.

For memory providers (io_uring zero-copy rx and devmem), it causes the real rxq in the physical netdev to be filled from a memory provider that has DMA mapped memory from a process within a container.

---
 net/core/dev.c             |   4 +-
 net/core/dev.h             |   7 +++
 net/core/netdev_rx_queue.c | 104 +++++++++++++++++++++++++++++++------
 3 files changed, 95 insertions(+), 20 deletions(-)
net/core/dev.c +1 -3
index f0a83a09477e..f8cc8667dff4 100644
@@ -12339,10 +12339,8 @@ static void dev_memory_provider_uninstall(struct net_device *dev)
 
 	for (i = 0; i < dev->real_num_rx_queues; i++) {
 		struct netdev_rx_queue *rxq = &dev->_rx[i];
-		struct pp_memory_provider_params *p = &rxq->mp_params;
 
-		if (p->mp_ops && p->mp_ops->uninstall)
-			p->mp_ops->uninstall(rxq->mp_params.mp_priv, rxq);
+		__netif_mp_uninstall_rxq(rxq, &rxq->mp_params);
 	}
 }
 
net/core/dev.h +7 -0
index 6516ce2b5517..95edb2d4eff8 100644
@@ -12,6 +12,7 @@ struct net;
 struct netlink_ext_ack;
 struct netdev_queue_config;
 struct cpumask;
+struct pp_memory_provider_params;
 
 /* Random bits of netdevice that don't need to be exposed */
 #define FLOW_LIMIT_HISTORY	(1 << 7)  /* must be ^2 and !overflow buckets */
@@ -101,6 +102,12 @@ int netdev_queue_config_validate(struct net_device *dev, int rxq_idx,
 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);
 
+void __netif_mp_uninstall_rxq(struct netdev_rx_queue *rxq,
+			      const struct pp_memory_provider_params *p);
+
+void netif_rxq_cleanup_unlease(struct netdev_rx_queue *phys_rxq,
+			       struct netdev_rx_queue *virt_rxq);
+
 /* netdev management, shared between various uAPI entry points */
 struct netdev_name_node {
 	struct hlist_node hlist;
net/core/netdev_rx_queue.c +87 -17

136 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#16 lore raw

PATCH v11 7/14 net: Proxy netdev_queue_get_dma_dev for leased queues

In reply to Daniel Borkmann

From: David Wei <[email protected]>

Extend netdev_queue_get_dma_dev to return the physical device of the real rxq for DMA in case the queue was leased. This allows memory providers like io_uring zero-copy or devmem to bind to the physically leased rxq via virtual devices such as netkit.

---
 include/net/netdev_queues.h |  4 +++-
 io_uring/zcrx.c             |  3 ++-
 net/core/netdev-genl.c      |  5 +++--
 net/core/netdev_queues.c    | 41 +++++++++++++++++++++++++++++--------
 4 files changed, 40 insertions(+), 13 deletions(-)
include/net/netdev_queues.h +3 -1
index 748b70552ed1..70c9fe9e83cc 100644
@@ -380,7 +380,9 @@ static inline unsigned int netif_xmit_timeout_ms(struct netdev_queue *txq)
 					 get_desc, start_thrs);		\
 	})
 
-struct device *netdev_queue_get_dma_dev(struct net_device *dev, int idx);
+struct device *netdev_queue_get_dma_dev(struct net_device *dev,
+					unsigned int idx,
+					enum netdev_queue_type type);
 bool netdev_can_create_queue(const struct net_device *dev,
 			     struct netlink_ext_ack *extack);
 bool netdev_can_lease_queue(const struct net_device *dev,
io_uring/zcrx.c +2 -1
index d3ec63c83d0c..f4a7809ba0c2 100644
@@ -829,7 +829,8 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx,
 	}
 	netdev_hold(ifq->netdev, &ifq->netdev_tracker, GFP_KERNEL);
 
-	ifq->dev = netdev_queue_get_dma_dev(ifq->netdev, reg.if_rxq);
+	ifq->dev = netdev_queue_get_dma_dev(ifq->netdev, reg.if_rxq,
+					    NETDEV_QUEUE_TYPE_RX);
 	if (!ifq->dev) {
 		ret = -EOPNOTSUPP;
 		goto netdev_put_unlock;
net/core/netdev-genl.c +3 -2
index 515832854251..056460d01940 100644
@@ -976,7 +976,8 @@ netdev_nl_get_dma_dev(struct net_device *netdev, unsigned long *rxq_bitmap,
 	for_each_set_bit(rxq_idx, rxq_bitmap, netdev->real_num_rx_queues) {
 		struct device *rxq_dma_dev;
 
-		rxq_dma_dev = netdev_queue_get_dma_dev(netdev, rxq_idx);
+		rxq_dma_dev = netdev_queue_get_dma_dev(netdev, rxq_idx,
+						       NETDEV_QUEUE_TYPE_RX);
 		if (dma_dev && rxq_dma_dev != dma_dev) {
 			NL_SET_ERR_MSG_FMT(extack, "DMA device mismatch between queue %u and %u (multi-PF device?)",
 					   rxq_idx, prev_rxq_idx);
@@ -1153,7 +1154,7 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
 		goto err_unlock_netdev;
 	}
 
-	dma_dev = netdev_queue_get_dma_dev(netdev, 0);
+	dma_dev = netdev_queue_get_dma_dev(netdev, 0, NETDEV_QUEUE_TYPE_TX);
 	binding = net_devmem_bind_dmabuf(netdev, dma_dev, DMA_TO_DEVICE,
 					 dmabuf_fd, priv, info->extack);
 	if (IS_ERR(binding)) {
net/core/netdev_queues.c +32 -9

59 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#17 lore raw

PATCH v11 8/14 xsk: Extend xsk_rcv_check validation

In reply to Daniel Borkmann

xsk_rcv_check tests for inbound packets to see whether they match the bound AF_XDP socket. Refactor the test into a small helper xsk_dev_queue_valid and move the validation against xs->dev and xs->queue_id there.

The fast-path case stays in place and allows for quick return in xsk_dev_queue_valid. If it fails, the validation is extended to check whether the AF_XDP socket is bound against a leased queue, and if so, the test is redone.

---
 net/xdp/xsk.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
net/xdp/xsk.c +26 -3
index 6149f6a79897..d638d7dbd7ed 100644
@@ -330,14 +330,37 @@ static bool xsk_is_bound(struct xdp_sock *xs)
 	return false;
 }
 
+static bool xsk_dev_queue_valid(const struct xdp_sock *xs,
+				const struct xdp_rxq_info *info)
+{
+	struct net_device *dev = xs->dev;
+	u32 queue_index = xs->queue_id;
+	struct netdev_rx_queue *rxq;
+
+	if (info->dev == dev &&
+	    info->queue_index == queue_index)
+		return true;
+
+	if (queue_index < dev->real_num_rx_queues) {
+		rxq = READ_ONCE(__netif_get_rx_queue(dev, queue_index)->lease);
+		if (!rxq)
+			return false;
+
+		dev = rxq->dev;
+		queue_index = get_netdev_rx_queue_index(rxq);
+
+		return info->dev == dev &&
+		       info->queue_index == queue_index;
+	}
+	return false;
+}
+
 static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 {
 	if (!xsk_is_bound(xs))
 		return -ENXIO;
-
-	if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
+	if (!xsk_dev_queue_valid(xs, xdp->rxq))
 		return -EINVAL;
-
 	if (len > xsk_pool_get_rx_frame_size(xs->pool) && !xs->sg) {
 		xs->rx_dropped++;
 		return -ENOSPC;
Signature · 1 line
2.43.0
author maintainer
#18 lore raw

PATCH v11 9/14 xsk: Proxy pool management for leased queues

In reply to Daniel Borkmann

Similarly to the netif_mp_{open,close}_rxq handling for leased queues, proxy the xsk_{reg,clear}_pool_at_qid via netif_get_rx_queue_lease_locked such that in case a virtual netdev picked a leased rxq, the request gets through to the real rxq in the physical netdev. The proxying is only relevant for queue_id < dev->real_num_rx_queues since right now it's only supported for rxqs.

---
 net/xdp/xsk.c | 47 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 12 deletions(-)
net/xdp/xsk.c +35 -12
index d638d7dbd7ed..fe1c7899455e 100644
@@ -23,6 +23,8 @@
 #include <linux/netdevice.h>
 #include <linux/rculist.h>
 #include <linux/vmalloc.h>
+
+#include <net/netdev_queues.h>
 #include <net/xdp_sock_drv.h>
 #include <net/busy_poll.h>
 #include <net/netdev_lock.h>
@@ -117,10 +119,18 @@ EXPORT_SYMBOL(xsk_get_pool_from_qid);
 
 void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
 {
-	if (queue_id < dev->num_rx_queues)
-		dev->_rx[queue_id].pool = NULL;
-	if (queue_id < dev->num_tx_queues)
-		dev->_tx[queue_id].pool = NULL;
+	struct net_device *orig_dev = dev;
+	unsigned int id = queue_id;
+
+	if (id < dev->real_num_rx_queues)
+		WARN_ON_ONCE(!netif_get_rx_queue_lease_locked(&dev, &id));
+
+	if (id < dev->num_rx_queues)
+		dev->_rx[id].pool = NULL;
+	if (id < dev->num_tx_queues)
+		dev->_tx[id].pool = NULL;
+
+	netif_put_rx_queue_lease_locked(orig_dev, dev);
 }
 
 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
@@ -130,17 +140,30 @@ void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
 int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
 			u16 queue_id)
 {
-	if (queue_id >= max_t(unsigned int,
-			      dev->real_num_rx_queues,
-			      dev->real_num_tx_queues))
+	struct net_device *orig_dev = dev;
+	unsigned int id = queue_id;
+	int ret = 0;
+
+	if (id >= max(dev->real_num_rx_queues,
+		      dev->real_num_tx_queues))
 		return -EINVAL;
 
-	if (queue_id < dev->real_num_rx_queues)
-		dev->_rx[queue_id].pool = pool;
-	if (queue_id < dev->real_num_tx_queues)
-		dev->_tx[queue_id].pool = pool;
+	if (id < dev->real_num_rx_queues) {
+		if (!netif_get_rx_queue_lease_locked(&dev, &id))
+			return -EBUSY;
+		if (xsk_get_pool_from_qid(dev, id)) {
+			ret = -EBUSY;
+			goto out;
+		}
+	}
 
-	return 0;
+	if (id < dev->real_num_rx_queues)
+		dev->_rx[id].pool = pool;
+	if (id < dev->real_num_tx_queues)
+		dev->_tx[id].pool = pool;
+out:
+	netif_put_rx_queue_lease_locked(orig_dev, dev);
+	return ret;
 }
 
 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff_xsk *xskb, u32 len,
Signature · 1 line
2.43.0
author maintainer
#19 lore raw

PATCH v11 10/14 netkit: Add single device mode for netkit

In reply to Daniel Borkmann

Add a single device mode for netkit instead of netkit pairs. The primary target for the paired devices is to connect network namespaces, of course, and support has been implemented in projects like Cilium [0]. For the rxq leasing the plan is to support two main scenarios related to single device mode:

* For the use-case of io_uring zero-copy, the control plane can either set up a netkit pair where the peer device can perform rxq leasing which is then tied to the lifetime of the peer device, or the control plane can use a regular netkit pair to connect the hostns to a Pod/container and dynamically add/remove rxq leasing through a single device without having to interrupt the device pair. In the case of io_uring, the memory pool is used as skb non-linear pages, and thus the skb will go its way through the regular stack into netkit. Things like the netkit policy when no BPF is attached or skb scrubbing etc apply as-is in case the paired devices are used, or if the backend memory is tied to the single device and traffic goes through a paired device.

* For the use-case of AF_XDP, the control plane needs to use netkit in the single device mode. The single device mode currently enforces only a pass policy when no BPF is attached, and does not yet support BPF link attachments for AF_XDP. skbs sent to that device get dropped at the moment. Given AF_XDP operates at a lower layer of the stack tying this to the netkit pair did not make sense. In future, the plan is to allow BPF at the XDP layer which can: i) process traffic coming from the AF_XDP application (e.g. QEMU with AF_XDP backend) to filter egress traffic or to push selected egress traffic up to the single netkit device to the local stack (e.g. DHCP requests), and ii) vice-versa skbs sent to the single netkit into the AF_XDP application (e.g. DHCP replies). Also, the control-plane can dynamically manage rxq leasing for the single netkit device without having to interrupt (e.g. down/up cycle) the main netkit pair for the Pod which has traffic going in and out.

---
 Documentation/netlink/specs/rt-link.yaml |  11 ++
 drivers/net/netkit.c                     | 131 ++++++++++++++---------
 include/uapi/linux/if_link.h             |   6 ++
 3 files changed, 99 insertions(+), 49 deletions(-)
Documentation/netlink/specs/rt-link.yaml +11 -0
index df4b56beb818..fcb5aaf0926f 100644
@@ -825,6 +825,13 @@ definitions:
     entries:
       - name: none
       - name: default
+  -
+    name: netkit-pairing
+    type: enum
+    enum-name: netkit-pairing
+    entries:
+      - name: pair
+      - name: single
   -
     name: ovpn-mode
     enum-name: ovpn-mode
@@ -2299,6 +2306,10 @@ attribute-sets:
       -
         name: tailroom
         type: u16
+      -
+        name: pairing
+        type: u32
+        enum: netkit-pairing
   -
     name: linkinfo-ovpn-attrs
     name-prefix: ifla-ovpn-
drivers/net/netkit.c +82 -49

237 lines not on this page · raw message

include/uapi/linux/if_link.h +6 -0
index 83a96c56b8ca..280bb1780512 100644
@@ -1296,6 +1296,11 @@ enum netkit_mode {
 	NETKIT_L3,
 };
 
+enum netkit_pairing {
+	NETKIT_DEVICE_PAIR,
+	NETKIT_DEVICE_SINGLE,
+};
+
 /* NETKIT_SCRUB_NONE leaves clearing skb->{mark,priority} up to
  * the BPF program if attached. This also means the latter can
  * consume the two fields if they were populated earlier.
@@ -1320,6 +1325,7 @@ enum {
 	IFLA_NETKIT_PEER_SCRUB,
 	IFLA_NETKIT_HEADROOM,
 	IFLA_NETKIT_TAILROOM,
+	IFLA_NETKIT_PAIRING,
 	__IFLA_NETKIT_MAX,
 };
 #define IFLA_NETKIT_MAX	(__IFLA_NETKIT_MAX - 1)
Signature · 1 line
2.43.0
author maintainer
#20 lore raw

PATCH v11 11/14 netkit: Implement rtnl_link_ops->alloc and ndo_queue_create

In reply to Daniel Borkmann

From: David Wei <[email protected]>

Implement rtnl_link_ops->alloc that allows the number of rx queues to be set when netkit is created. By default, netkit has only a single rxq (and single txq). The number of queues is deliberately not allowed to be changed via ethtool -L and is fixed for the lifetime of a netkit instance.

For netkit device creation, numrxqueues with larger than one rxq can be specified. These rxqs are leasable to real rxqs in physical netdevs:

ip link add type netkit peer numrxqueues 64 # for device pair
ip link add numrxqueues 64 type netkit single # for single device

The limit of numrxqueues for netkit is currently set to 1024, which allows leasing multiple real rxqs from physical netdevs.

The implementation of ndo_queue_create() adds a new rxq during the queue lease operation. We allow to create queues either in single device mode or for the case of dual device mode for the netkit peer device which gets placed into the target network namespace. For dual device mode the lease against the primary device does not make sense for the targeted use cases, and therefore gets rejected.

We also need to add a lockdep class for netkit, such that lockdep does not trip over us, similarly done as in commit 0bef512012b1 ("net: add netdev_lockdep_set_classes() to virtual drivers").

This is also the last missing bit to netkit for supporting io_uring with zero-copy mode [0]. Up until this point it was not possible to consume the latter out of containers or Kubernetes Pods where applications are in their own network namespace.

io_uring example with eth0 being a physical device with 16 queues where netkit is bound to the last queue, iou-zcrx.c is binary from selftests; ethtool configuration (tcp-data-split, hds_thresh, RSS, flow steering) is done on the physical device by the control plane; here, flow steering to that queue is based on the service VIP:port of the server utilizing io_uring:

  # ethtool -X eth0 start 0 equal 15
  # ethtool -X eth0 start 15 equal 1 context new
  # ethtool --config-ntuple eth0 flow-type tcp4 dst-ip 1.2.3.4 dst-port 5000 action 15
  # ip netns add foo
  # ip link add type netkit peer numrxqueues 2
  # ynl --family netdev --output-json --do queue-create \
        --json "{"ifindex": $(ifindex nk0), "type": "rx", \
                 "lease": { "ifindex": $(ifindex eth0), \
                            "queue": { "type": "rx", "id": 15 } } }"
  {'id': 1}
  # ip link set nk0 netns foo
  # ip link set nk1 up
  # ip netns exec foo ip link set lo up
  # ip netns exec foo ip link set nk0 up
  # ip netns exec foo ip addr add 1.2.3.4/32 dev nk0
  [ ... setup routing etc to get external traffic into the netns ... ]
  # ip netns exec foo ./iou-zcrx -s -p 5000 -i nk0 -q 1

Remote io_uring client:

  # ./iou-zcrx -c -h 1.2.3.4 -p 5000 -l 12840 -z 65536

We have tested the above against a Broadcom BCM957504 (bnxt_en) 100G NIC, supporting TCP header/data split.

Similarly, this also works for devmem which we tested using ncdevmem:

  # ip netns exec foo ./ncdevmem -s 1.2.3.4 -l -p 5000 -f nk0 -t 1 -q 1

And on the remote client:

  # ./ncdevmem -s 1.2.3.4 -p 5000 -f eth0

For Cilium, the plan is to open up support for the various memory providers for regular Kubernetes Pods when Cilium is configured with netkit datapath mode.

---
 drivers/net/netkit.c | 126 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 114 insertions(+), 12 deletions(-)
drivers/net/netkit.c +114 -12
index 96c098a6db0d..b22bd0b6508a 100644
@@ -9,11 +9,20 @@
 #include <linux/bpf_mprog.h>
 #include <linux/indirect_call_wrapper.h>
 
+#include <net/netdev_lock.h>
+#include <net/netdev_queues.h>
+#include <net/netdev_rx_queue.h>
 #include <net/netkit.h>
 #include <net/dst.h>
 #include <net/tcx.h>
 
-#define DRV_NAME "netkit"
+#define NETKIT_DRV_NAME	"netkit"
+
+#define NETKIT_NUM_RX_QUEUES_MAX  1024
+#define NETKIT_NUM_TX_QUEUES_MAX  1
+
+#define NETKIT_NUM_RX_QUEUES_REAL 1
+#define NETKIT_NUM_TX_QUEUES_REAL 1
 
 struct netkit {
 	__cacheline_group_begin(netkit_fastpath);
@@ -37,6 +46,8 @@ struct netkit_link {
 	struct net_device *dev;
 };
 
+static struct rtnl_link_ops netkit_link_ops;
+
 static __always_inline int
 netkit_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
 	   enum netkit_action ret)
@@ -225,9 +236,16 @@ static void netkit_get_stats(struct net_device *dev,
 	stats->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
 }
 
+static int netkit_init(struct net_device *dev)
+{
+	netdev_lockdep_set_classes(dev);
+	return 0;
+}
+
 static void netkit_uninit(struct net_device *dev);
 
 static const struct net_device_ops netkit_netdev_ops = {
+	.ndo_init		= netkit_init,
 	.ndo_open		= netkit_open,
 	.ndo_stop		= netkit_close,
 	.ndo_start_xmit		= netkit_xmit,
@@ -244,13 +262,96 @@ static const struct net_device_ops netkit_netdev_ops = {
 static void netkit_get_drvinfo(struct net_device *dev,
 			       struct ethtool_drvinfo *info)
 {
-	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
+	strscpy(info->driver, NETKIT_DRV_NAME, sizeof(info->driver));
 }
 
 static const struct ethtool_ops netkit_ethtool_ops = {
 	.get_drvinfo		= netkit_get_drvinfo,
 };
 
+static int netkit_queue_create(struct net_device *dev,
+			       struct netlink_ext_ack *extack)
+{
+	struct netkit *nk = netkit_priv(dev);
+	u32 rxq_count_old, rxq_count_new;
+	int err;
+
+	rxq_count_old = dev->real_num_rx_queues;
+	rxq_count_new = rxq_count_old + 1;
+
+	/* In paired mode, only the non-primary (peer) device can
+	 * create leased queues since the primary is the management
+	 * side. In single device mode, leasing is always allowed.
+	 */
+	if (nk->pair == NETKIT_DEVICE_PAIR && nk->primary) {
+		NL_SET_ERR_MSG(extack,
+			       "netkit can only lease against the peer device");
+		return -EOPNOTSUPP;
+	}
+
+	err = netif_set_real_num_rx_queues(dev, rxq_count_new);
+	if (err) {
+		if (rxq_count_new > dev->num_rx_queues)
+			NL_SET_ERR_MSG(extack,
+				       "netkit maximum queue limit reached");
+		else
+			NL_SET_ERR_MSG_FMT(extack,
+					   "netkit cannot create more queues err=%d", err);
+		return err;
+	}
+
+	return rxq_count_old;
+}
+
+static const struct netdev_queue_mgmt_ops netkit_queue_mgmt_ops = {
+	.ndo_queue_create	= netkit_queue_create,
+};
+
+static struct net_device *netkit_alloc(struct nlattr *tb[],
+				       const char *ifname,
+				       unsigned char name_assign_type,
+				       unsigned int num_tx_queues,
+				       unsigned int num_rx_queues)
+{
+	const struct rtnl_link_ops *ops = &netkit_link_ops;
+	struct net_device *dev;
+
+	if (num_tx_queues > NETKIT_NUM_TX_QUEUES_MAX ||
+	    num_rx_queues > NETKIT_NUM_RX_QUEUES_MAX)
+		return ERR_PTR(-EOPNOTSUPP);
+
+	dev = alloc_netdev_mqs(ops->priv_size, ifname,
+			       name_assign_type, ops->setup,
+			       num_tx_queues, num_rx_queues);
+	if (dev) {
+		dev->real_num_tx_queues = NETKIT_NUM_TX_QUEUES_REAL;
+		dev->real_num_rx_queues = NETKIT_NUM_RX_QUEUES_REAL;
+	}
+	return dev;
+}
+
+static void netkit_queue_unlease(struct net_device *dev)
+{
+	struct netdev_rx_queue *rxq, *rxq_lease;
+	struct net_device *dev_lease;
+	int i;
+
+	if (dev->real_num_rx_queues == 1)
+		return;
+
+	netdev_lock(dev);
+	for (i = 1; i < dev->real_num_rx_queues; i++) {
+		rxq = __netif_get_rx_queue(dev, i);
+		rxq_lease = rxq->lease;
+		dev_lease = rxq_lease->dev;
+
+		netdev_lock(dev_lease);
+		netdev_rx_queue_unlease(rxq, rxq_lease);
+		netdev_unlock(dev_lease);
+	}
+	netdev_unlock(dev);
+}
+
 static void netkit_setup(struct net_device *dev)
 {
 	static const netdev_features_t netkit_features_hw_vlan =
@@ -281,8 +382,9 @@ static void netkit_setup(struct net_device *dev)
 	dev->priv_flags |= IFF_DISABLE_NETPOLL;
 	dev->lltx = true;
 
-	dev->ethtool_ops = &netkit_ethtool_ops;
-	dev->netdev_ops  = &netkit_netdev_ops;
+	dev->netdev_ops     = &netkit_netdev_ops;
+	dev->ethtool_ops    = &netkit_ethtool_ops;
+	dev->queue_mgmt_ops = &netkit_queue_mgmt_ops;
 
 	dev->features |= netkit_features;
 	dev->hw_features = netkit_features;
@@ -331,8 +433,6 @@ static int netkit_validate(struct nlattr *tb[], struct nlattr *data[],
 	return 0;
 }
 
-static struct rtnl_link_ops netkit_link_ops;
-
 static int netkit_new_link(struct net_device *dev,
 			   struct rtnl_newlink_params *params,
 			   struct netlink_ext_ack *extack)
@@ -870,6 +970,7 @@ static void netkit_release_all(struct net_device *dev)
 static void netkit_uninit(struct net_device *dev)
 {
 	netkit_release_all(dev);
+	netkit_queue_unlease(dev);
 }
 
 static void netkit_del_link(struct net_device *dev, struct list_head *head)
@@ -1010,8 +1111,9 @@ static const struct nla_policy netkit_policy[IFLA_NETKIT_MAX + 1] = {
 };
 
 static struct rtnl_link_ops netkit_link_ops = {
-	.kind		= DRV_NAME,
+	.kind		= NETKIT_DRV_NAME,
 	.priv_size	= sizeof(struct netkit),
+	.alloc		= netkit_alloc,
 	.setup		= netkit_setup,
 	.newlink	= netkit_new_link,
 	.dellink	= netkit_del_link,
@@ -1025,7 +1127,7 @@ static struct rtnl_link_ops netkit_link_ops = {
 	.maxtype	= IFLA_NETKIT_MAX,
 };
 
-static __init int netkit_init(void)
+static __init int netkit_mod_init(void)
 {
 	BUILD_BUG_ON((int)NETKIT_NEXT != (int)TCX_NEXT ||
 		     (int)NETKIT_PASS != (int)TCX_PASS ||
@@ -1035,16 +1137,16 @@ static __init int netkit_init(void)
 	return rtnl_link_register(&netkit_link_ops);
 }
 
-static __exit void netkit_exit(void)
+static __exit void netkit_mod_exit(void)
 {
 	rtnl_link_unregister(&netkit_link_ops);
 }
 
-module_init(netkit_init);
-module_exit(netkit_exit);
+module_init(netkit_mod_init);
+module_exit(netkit_mod_exit);
 
 MODULE_DESCRIPTION("BPF-programmable network device");
 MODULE_AUTHOR("Daniel Borkmann <[email protected]>");
 MODULE_AUTHOR("Nikolay Aleksandrov <[email protected]>");
 MODULE_LICENSE("GPL");
-MODULE_ALIAS_RTNL_LINK(DRV_NAME);
+MODULE_ALIAS_RTNL_LINK(NETKIT_DRV_NAME);
Signature · 1 line
2.43.0
author maintainer
#21 lore raw

PATCH v11 12/14 netkit: Add netkit notifier to check for unregistering devices

In reply to Daniel Borkmann

Add a netdevice notifier in netkit to watch for NETDEV_UNREGISTER events. If the target device is indeed NETREG_UNREGISTERING and previously leased a queue to a netkit device, then collect the related netkit devices and batch-unregister_netdevice_many() them.

If this were not done, then the netkit device would hold a reference on the physical device preventing it from going away. However, in case of both io_uring zero-copy as well as AF_XDP this situation is handled gracefully and the allocated resources are torn down.

In the case where mentioned infra is used through netkit, the applications have a reference on netkit, and netkit in turn holds a reference on the physical device. In order to have netkit release the reference on the physical device, we need such watcher to then unregister the netkit ones.

This is generally quite similar to the dependency handling in case of tunnels (e.g. vxlan bound to a underlying netdev) where the tunnel device gets removed along with the physical device.

  # ip a
  [...]
  4: enp10s0f0np0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN group default qlen 1000
      link/ether e8:eb:d3:a3:43:f6 brd ff:ff:ff:ff:ff:ff
      inet 10.0.0.2/24 scope global enp10s0f0np0
         valid_lft forever preferred_lft forever
  [...]
  8: nk@NONE: <BROADCAST,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
      link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
  [...]
  # rmmod mlx5_ib
  # rmmod mlx5_core
  [...]
  [  309.261822] mlx5_core 0000:0a:00.0 mlx5_0: Port: 1 Link DOWN
  [  344.235236] mlx5_core 0000:0a:00.1: E-Switch: Unload vfs: mode(LEGACY), nvfs(0), necvfs(0), active vports(0)
  [  344.246948] mlx5_core 0000:0a:00.1: E-Switch: Disable: mode(LEGACY), nvfs(0), necvfs(0), active vports(0)
  [  344.463754] mlx5_core 0000:0a:00.1: E-Switch: Disable: mode(LEGACY), nvfs(0), necvfs(0), active vports(0)
  [  344.770155] mlx5_core 0000:0a:00.1: E-Switch: cleanup
  [...]
  # ip a
  [...]
  [ both enp10s0f0np0 and nk gone ]
  [...]
---
 drivers/net/netkit.c      | 69 +++++++++++++++++++++++++++++++++++++--
 include/linux/netdevice.h |  2 ++
 net/core/dev.c            |  6 ++++
 3 files changed, 75 insertions(+), 2 deletions(-)
drivers/net/netkit.c +67 -2
index b22bd0b6508a..1ec21aef348f 100644
@@ -983,7 +983,15 @@ static void netkit_del_link(struct net_device *dev, struct list_head *head)
 	if (peer) {
 		nk = netkit_priv(peer);
 		RCU_INIT_POINTER(nk->peer, NULL);
-		unregister_netdevice_queue(peer, head);
+		/* Guard against the peer already being in an unregister
+		 * list (e.g. same-namespace teardown where the peer is
+		 * in the caller's dev_kill_list). list_move_tail() on an
+		 * already-queued device would otherwise corrupt that
+		 * list's iteration. This situation can occur via netkit
+		 * notifier, hence guard against this scenario.
+		 */
+		if (!unregister_netdevice_queued(peer))
+			unregister_netdevice_queue(peer, head);
 	}
 }
 
@@ -1051,6 +1059,50 @@ static int netkit_change_link(struct net_device *dev, struct nlattr *tb[],
 	return 0;
 }
 
+static void netkit_check_lease_unregister(struct net_device *dev)
+{
+	LIST_HEAD(list_kill);
+	u32 q_idx;
+
+	if (READ_ONCE(dev->reg_state) != NETREG_UNREGISTERING ||
+	    !dev->dev.parent)
+		return;
+
+	netdev_lock_ops(dev);
+	for (q_idx = 0; q_idx < dev->real_num_rx_queues; q_idx++) {
+		struct net_device *tmp = dev;
+		struct netdev_rx_queue *rxq;
+		u32 tmp_q_idx = q_idx;
+
+		rxq = __netif_get_rx_queue_lease(&tmp, &tmp_q_idx,
+						 NETIF_PHYS_TO_VIRT);
+		if (rxq && tmp != dev &&
+		    tmp->netdev_ops == &netkit_netdev_ops) {
+			/* A single phys device can have multiple queues leased
+			 * to one netkit device. We can only queue that netkit
+			 * device once to the list_kill. Queues of that phys
+			 * device can be leased with different individual netkit
+			 * devices, hence we batch via list_kill.
+			 */
+			if (unregister_netdevice_queued(tmp))
+				continue;
+			netkit_del_link(tmp, &list_kill);
+		}
+	}
+	netdev_unlock_ops(dev);
+	unregister_netdevice_many(&list_kill);
+}
+
+static int netkit_notifier(struct notifier_block *this,
+			   unsigned long event, void *ptr)
+{
+	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+
+	if (event == NETDEV_UNREGISTER)
+		netkit_check_lease_unregister(dev);
+	return NOTIFY_DONE;
+}
+
 static size_t netkit_get_size(const struct net_device *dev)
 {
 	return nla_total_size(sizeof(u32)) + /* IFLA_NETKIT_POLICY */
@@ -1127,18 +1179,31 @@ static struct rtnl_link_ops netkit_link_ops = {
 	.maxtype	= IFLA_NETKIT_MAX,
 };
 
+static struct notifier_block netkit_netdev_notifier = {
+	.notifier_call	= netkit_notifier,
+};
+
 static __init int netkit_mod_init(void)
 {
+	int ret;
+
 	BUILD_BUG_ON((int)NETKIT_NEXT != (int)TCX_NEXT ||
 		     (int)NETKIT_PASS != (int)TCX_PASS ||
 		     (int)NETKIT_DROP != (int)TCX_DROP ||
 		     (int)NETKIT_REDIRECT != (int)TCX_REDIRECT);
 
-	return rtnl_link_register(&netkit_link_ops);
+	ret = rtnl_link_register(&netkit_link_ops);
+	if (ret)
+		return ret;
+	ret = register_netdevice_notifier(&netkit_netdev_notifier);
+	if (ret)
+		rtnl_link_unregister(&netkit_link_ops);
+	return ret;
 }
 
 static __exit void netkit_mod_exit(void)
 {
+	unregister_netdevice_notifier(&netkit_netdev_notifier);
 	rtnl_link_unregister(&netkit_link_ops);
 }
 
include/linux/netdevice.h +2 -0
index e8aa9cc4075d..47417b2d48a4 100644
@@ -3420,6 +3420,8 @@ static inline int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
 int register_netdevice(struct net_device *dev);
 void unregister_netdevice_queue(struct net_device *dev, struct list_head *head);
 void unregister_netdevice_many(struct list_head *head);
+bool unregister_netdevice_queued(const struct net_device *dev);
+
 static inline void unregister_netdevice(struct net_device *dev)
 {
 	unregister_netdevice_queue(dev, NULL);
net/core/dev.c +6 -0
index f8cc8667dff4..f66b0d27ba65 100644
@@ -12373,6 +12373,12 @@ static void netif_close_many_and_unlock_cond(struct list_head *close_head)
 #endif
 }
 
+bool unregister_netdevice_queued(const struct net_device *dev)
+{
+	ASSERT_RTNL();
+	return !list_empty(&dev->unreg_list);
+}
+
 void unregister_netdevice_many_notify(struct list_head *head,
 				      u32 portid, const struct nlmsghdr *nlh)
 {
Signature · 1 line
2.43.0
author maintainer
#22 lore raw

PATCH v11 13/14 netkit: Add xsk support for af_xdp applications

In reply to Daniel Borkmann

Enable support for AF_XDP applications to operate on a netkit device. The goal is that AF_XDP applications can natively consume AF_XDP from network namespaces. The use-case from Cilium side is to support Kubernetes KubeVirt VMs through QEMU's AF_XDP backend. KubeVirt is a virtual machine management add-on for Kubernetes which aims to provide a common ground for virtualization. KubeVirt spawns the VMs inside Kubernetes Pods which reside in their own network namespace just like regular Pods.

Raw QEMU AF_XDP backend example with eth0 being a physical device with 16 queues where netkit is bound to the last queue (for multi-queue RSS context can be used if supported by the driver):

  # ethtool -X eth0 start 0 equal 15
  # ethtool -X eth0 start 15 equal 1 context new
  # ethtool --config-ntuple eth0 flow-type ether \
            src 00:00:00:00:00:00 \
            src-mask ff:ff:ff:ff:ff:ff \
            dst $mac dst-mask 00:00:00:00:00:00 \
            proto 0 proto-mask 0xffff action 15
  [ ... setup BPF/XDP prog on eth0 to steer into shared xsk map ... ]
  # ip netns add foo
  # ip link add numrxqueues 2 nk type netkit single
  # ynl --family netdev --output-json --do queue-create \
        --json "{"ifindex": $(ifindex nk), "type": "rx", \
                 "lease": { "ifindex": $(ifindex eth0), \
                            "queue": { "type": "rx", "id": 15 } } }"
  {'id': 1}
  # ip link set nk netns foo
  # ip netns exec foo ip link set lo up
  # ip netns exec foo ip link set nk up
  # ip netns exec foo qemu-system-x86_64 \
          -kernel $kernel \
          -drive file=${image_name},index=0,media=disk,format=raw \
          -append "root=/dev/sda rw console=ttyS0" \
          -cpu host \
          -m $memory \
          -enable-kvm \
          -device virtio-net-pci,netdev=net0,mac=$mac \
          -netdev af-xdp,ifname=nk,id=net0,mode=native,queues=1,start-queue=1,inhibit=on,map-path=$dir/xsks_map \
          -nographic

We have tested the above against a dual-port Nvidia ConnectX-6 (mlx5) 100G NIC with successful network connectivity out of QEMU. An earlier iteration of this work was presented at LSF/MM/BPF [0] and more recently at LPC [1].

For getting to a first starting point to connect all things with KubeVirt, bind mounting the xsk map from Cilium into the VM launcher Pod which acts as a regular Kubernetes Pod while not perfect, is not a big problem given its out of reach from the application sitting inside the VM (and some of the control plane aspects are baked in the launcher Pod already), so the isolation barrier is still the VM. Eventually the goal is to have a XDP/XSK redirect extension where there is no need to have the xsk map, and the BPF program can just derive the target xsk through the queue where traffic was received on.

The exposure through netkit is because Cilium should not act as a proxy handing out xsk sockets. Existing applications expect a netdev from kernel side and should not need to rewrite just to implement against a CNI's protocol. Also, all the memory should not be accounted against Cilium but rather the application Pod itself which is consuming AF_XDP. Further, on up/downgrades we expect the data plane to being completely decoupled from the control plane; if Cilium would own the sockets that would be disruptive. Another use-case which opens up and is regularly asked from users would be to have DPDK applications on top of AF_XDP in regular Kubernetes Pods.

---
 drivers/net/netkit.c | 86 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
drivers/net/netkit.c +86 -0
index 1ec21aef348f..5619209329d5 100644
@@ -12,6 +12,7 @@
 #include <net/netdev_lock.h>
 #include <net/netdev_queues.h>
 #include <net/netdev_rx_queue.h>
+#include <net/xdp_sock_drv.h>
 #include <net/netkit.h>
 #include <net/dst.h>
 #include <net/tcx.h>
@@ -236,6 +237,86 @@ static void netkit_get_stats(struct net_device *dev,
 	stats->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
 }
 
+static bool netkit_xsk_supported_at_phys(const struct net_device *dev)
+{
+	if (!dev->netdev_ops->ndo_bpf ||
+	    !dev->netdev_ops->ndo_xdp_xmit ||
+	    !dev->netdev_ops->ndo_xsk_wakeup)
+		return false;
+	return true;
+}
+
+static int netkit_xsk(struct net_device *dev, struct netdev_bpf *xdp)
+{
+	struct netkit *nk = netkit_priv(dev);
+	struct netdev_bpf xdp_lower;
+	struct netdev_rx_queue *rxq;
+	struct net_device *phys;
+	bool create = false;
+	int ret = -EBUSY;
+
+	switch (xdp->command) {
+	case XDP_SETUP_XSK_POOL:
+		if (nk->pair == NETKIT_DEVICE_PAIR)
+			return -EOPNOTSUPP;
+		if (xdp->xsk.queue_id >= dev->real_num_rx_queues)
+			return -EINVAL;
+
+		rxq = __netif_get_rx_queue(dev, xdp->xsk.queue_id);
+		if (!rxq->lease)
+			return -EOPNOTSUPP;
+
+		phys = rxq->lease->dev;
+		if (!netkit_xsk_supported_at_phys(phys))
+			return -EOPNOTSUPP;
+
+		create = xdp->xsk.pool;
+		memcpy(&xdp_lower, xdp, sizeof(xdp_lower));
+		xdp_lower.xsk.queue_id = get_netdev_rx_queue_index(rxq->lease);
+		break;
+	case XDP_SETUP_PROG:
+		return -EOPNOTSUPP;
+	default:
+		return -EINVAL;
+	}
+
+	netdev_lock(phys);
+	if (create &&
+	    (phys->xdp_features & NETDEV_XDP_ACT_XSK) != NETDEV_XDP_ACT_XSK) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
+	if (!create || !dev_get_min_mp_channel_count(phys))
+		ret = phys->netdev_ops->ndo_bpf(phys, &xdp_lower);
+out:
+	netdev_unlock(phys);
+	return ret;
+}
+
+static int netkit_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
+{
+	struct netdev_rx_queue *rxq, *rxq_lease;
+	struct net_device *phys;
+
+	if (queue_id >= dev->real_num_rx_queues)
+		return -EINVAL;
+
+	rxq = __netif_get_rx_queue(dev, queue_id);
+	rxq_lease = READ_ONCE(rxq->lease);
+	if (unlikely(!rxq_lease))
+		return -EOPNOTSUPP;
+
+	/* netkit_xsk already validated full xsk support, hence it's
+	 * fine to call into ndo_xsk_wakeup right away given this
+	 * was a prerequisite to get here in the first place. The
+	 * phys xsk support cannot change without tearing down the
+	 * device (which clears the lease first).
+	 */
+	phys = rxq_lease->dev;
+	return phys->netdev_ops->ndo_xsk_wakeup(phys,
+			get_netdev_rx_queue_index(rxq_lease), flags);
+}
+
 static int netkit_init(struct net_device *dev)
 {
 	netdev_lockdep_set_classes(dev);
@@ -256,6 +337,8 @@ static const struct net_device_ops netkit_netdev_ops = {
 	.ndo_get_peer_dev	= netkit_peer_dev,
 	.ndo_get_stats64	= netkit_get_stats,
 	.ndo_uninit		= netkit_uninit,
+	.ndo_bpf		= netkit_xsk,
+	.ndo_xsk_wakeup		= netkit_xsk_wakeup,
 	.ndo_features_check	= passthru_features_check,
 };
 
@@ -569,6 +652,9 @@ static int netkit_new_link(struct net_device *dev,
 	nk->headroom = headroom;
 	bpf_mprog_bundle_init(&nk->bundle);
 
+	if (pair == NETKIT_DEVICE_SINGLE)
+		xdp_set_features_flag(dev, NETDEV_XDP_ACT_XSK);
+
 	err = register_netdevice(dev);
 	if (err < 0)
 		goto err_configure_peer;
Signature · 1 line
2.43.0
#23 lore raw

Re: [PATCH net-next v11 13/14] netkit: Add xsk support for af_xdp applications

Quoted 47 lines · Daniel Borkmann

Enable support for AF_XDP applications to operate on a netkit device. The goal is that AF_XDP applications can natively consume AF_XDP from network namespaces. The use-case from Cilium side is to support Kubernetes KubeVirt VMs through QEMU's AF_XDP backend. KubeVirt is a virtual machine management add-on for Kubernetes which aims to provide a common ground for virtualization. KubeVirt spawns the VMs inside Kubernetes Pods which reside in their own network namespace just like regular Pods.

Raw QEMU AF_XDP backend example with eth0 being a physical device with 16 queues where netkit is bound to the last queue (for multi-queue RSS context can be used if supported by the driver):

   # ethtool -X eth0 start 0 equal 15
   # ethtool -X eth0 start 15 equal 1 context new
   # ethtool --config-ntuple eth0 flow-type ether \
             src 00:00:00:00:00:00 \
             src-mask ff:ff:ff:ff:ff:ff \
             dst $mac dst-mask 00:00:00:00:00:00 \
             proto 0 proto-mask 0xffff action 15
   [ ... setup BPF/XDP prog on eth0 to steer into shared xsk map ... ]
   # ip netns add foo
   # ip link add numrxqueues 2 nk type netkit single
   # ynl --family netdev --output-json --do queue-create \
         --json "{"ifindex": $(ifindex nk), "type": "rx", \
                  "lease": { "ifindex": $(ifindex eth0), \
                             "queue": { "type": "rx", "id": 15 } } }"
   {'id': 1}
   # ip link set nk netns foo
   # ip netns exec foo ip link set lo up
   # ip netns exec foo ip link set nk up
   # ip netns exec foo qemu-system-x86_64 \
           -kernel $kernel \
           -drive file=${image_name},index=0,media=disk,format=raw \
           -append "root=/dev/sda rw console=ttyS0" \
           -cpu host \
           -m $memory \
           -enable-kvm \
           -device virtio-net-pci,netdev=net0,mac=$mac \
           -netdev af-xdp,ifname=nk,id=net0,mode=native,queues=1,start-queue=1,inhibit=on,map-path=$dir/xsks_map \
           -nographic

We have tested the above against a dual-port Nvidia ConnectX-6 (mlx5) 100G NIC with successful network connectivity out of QEMU. An earlier iteration of this work was presented at LSF/MM/BPF [0] and more recently at LPC [1].

Quoted 10 lines

For getting to a first starting point to connect all things with KubeVirt, bind mounting the xsk map from Cilium into the VM launcher Pod which acts as a regular Kubernetes Pod while not perfect, is not a big problem given its out of reach from the application sitting inside the VM (and some of the control plane aspects are baked in the launcher Pod already), so the isolation barrier is still the VM. Eventually the goal is to have a XDP/XSK redirect extension where there is no need to have the xsk map, and the BPF program can just derive the target xsk through the queue where traffic was received on.

Sorry, this may be a bit late regarding this commit.

If I remember correctly, eBPF is already supported in netkit. Since eBPF can cover everything XDP does, I’m wondering what the motivation was for adding explicit XDP support to the netkit driver.

Thanks,
Zhu Yanjun

Quoted 140 lines

The exposure through netkit is because Cilium should not act as a proxy handing out xsk sockets. Existing applications expect a netdev from kernel side and should not need to rewrite just to implement against a CNI's protocol. Also, all the memory should not be accounted against Cilium but rather the application Pod itself which is consuming AF_XDP. Further, on up/downgrades we expect the data plane to being completely decoupled from the control plane; if Cilium would own the sockets that would be disruptive. Another use-case which opens up and is regularly asked from users would be to have DPDK applications on top of AF_XDP in regular Kubernetes Pods.

---
  drivers/net/netkit.c | 86 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 86 insertions(+)
drivers/net/netkit.c +86 -0
index 1ec21aef348f..5619209329d5 100644
@@ -12,6 +12,7 @@
  #include <net/netdev_lock.h>
  #include <net/netdev_queues.h>
  #include <net/netdev_rx_queue.h>
+#include <net/xdp_sock_drv.h>
  #include <net/netkit.h>
  #include <net/dst.h>
  #include <net/tcx.h>
@@ -236,6 +237,86 @@ static void netkit_get_stats(struct net_device *dev,
  	stats->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
  }
 
+static bool netkit_xsk_supported_at_phys(const struct net_device *dev)
+{
+	if (!dev->netdev_ops->ndo_bpf ||
+	    !dev->netdev_ops->ndo_xdp_xmit ||
+	    !dev->netdev_ops->ndo_xsk_wakeup)
+		return false;
+	return true;
+}
+
+static int netkit_xsk(struct net_device *dev, struct netdev_bpf *xdp)
+{
+	struct netkit *nk = netkit_priv(dev);
+	struct netdev_bpf xdp_lower;
+	struct netdev_rx_queue *rxq;
+	struct net_device *phys;
+	bool create = false;
+	int ret = -EBUSY;
+
+	switch (xdp->command) {
+	case XDP_SETUP_XSK_POOL:
+		if (nk->pair == NETKIT_DEVICE_PAIR)
+			return -EOPNOTSUPP;
+		if (xdp->xsk.queue_id >= dev->real_num_rx_queues)
+			return -EINVAL;
+
+		rxq = __netif_get_rx_queue(dev, xdp->xsk.queue_id);
+		if (!rxq->lease)
+			return -EOPNOTSUPP;
+
+		phys = rxq->lease->dev;
+		if (!netkit_xsk_supported_at_phys(phys))
+			return -EOPNOTSUPP;
+
+		create = xdp->xsk.pool;
+		memcpy(&xdp_lower, xdp, sizeof(xdp_lower));
+		xdp_lower.xsk.queue_id = get_netdev_rx_queue_index(rxq->lease);
+		break;
+	case XDP_SETUP_PROG:
+		return -EOPNOTSUPP;
+	default:
+		return -EINVAL;
+	}
+
+	netdev_lock(phys);
+	if (create &&
+	    (phys->xdp_features & NETDEV_XDP_ACT_XSK) != NETDEV_XDP_ACT_XSK) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
+	if (!create || !dev_get_min_mp_channel_count(phys))
+		ret = phys->netdev_ops->ndo_bpf(phys, &xdp_lower);
+out:
+	netdev_unlock(phys);
+	return ret;
+}
+
+static int netkit_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
+{
+	struct netdev_rx_queue *rxq, *rxq_lease;
+	struct net_device *phys;
+
+	if (queue_id >= dev->real_num_rx_queues)
+		return -EINVAL;
+
+	rxq = __netif_get_rx_queue(dev, queue_id);
+	rxq_lease = READ_ONCE(rxq->lease);
+	if (unlikely(!rxq_lease))
+		return -EOPNOTSUPP;
+
+	/* netkit_xsk already validated full xsk support, hence it's
+	 * fine to call into ndo_xsk_wakeup right away given this
+	 * was a prerequisite to get here in the first place. The
+	 * phys xsk support cannot change without tearing down the
+	 * device (which clears the lease first).
+	 */
+	phys = rxq_lease->dev;
+	return phys->netdev_ops->ndo_xsk_wakeup(phys,
+			get_netdev_rx_queue_index(rxq_lease), flags);
+}
+
  static int netkit_init(struct net_device *dev)
  {
  	netdev_lockdep_set_classes(dev);
@@ -256,6 +337,8 @@ static const struct net_device_ops netkit_netdev_ops = {
  	.ndo_get_peer_dev	= netkit_peer_dev,
  	.ndo_get_stats64	= netkit_get_stats,
  	.ndo_uninit		= netkit_uninit,
+	.ndo_bpf		= netkit_xsk,
+	.ndo_xsk_wakeup		= netkit_xsk_wakeup,
  	.ndo_features_check	= passthru_features_check,
  };
 
@@ -569,6 +652,9 @@ static int netkit_new_link(struct net_device *dev,
  	nk->headroom = headroom;
  	bpf_mprog_bundle_init(&nk->bundle);
 
+	if (pair == NETKIT_DEVICE_SINGLE)
+		xdp_set_features_flag(dev, NETDEV_XDP_ACT_XSK);
+
  	err = register_netdevice(dev);
  	if (err < 0)
  		goto err_configure_peer;
author maintainer
#24 lore raw

Re: [PATCH net-next v11 13/14] netkit: Add xsk support for af_xdp applications

Quoted 59 lines · Zhu Yanjun
Quoted 47 lines · Daniel Borkmann

Enable support for AF_XDP applications to operate on a netkit device. The goal is that AF_XDP applications can natively consume AF_XDP from network namespaces. The use-case from Cilium side is to support Kubernetes KubeVirt VMs through QEMU's AF_XDP backend. KubeVirt is a virtual machine management add-on for Kubernetes which aims to provide a common ground for virtualization. KubeVirt spawns the VMs inside Kubernetes Pods which reside in their own network namespace just like regular Pods.

Raw QEMU AF_XDP backend example with eth0 being a physical device with 16 queues where netkit is bound to the last queue (for multi-queue RSS context can be used if supported by the driver):

   # ethtool -X eth0 start 0 equal 15
   # ethtool -X eth0 start 15 equal 1 context new
   # ethtool --config-ntuple eth0 flow-type ether \
             src 00:00:00:00:00:00 \
             src-mask ff:ff:ff:ff:ff:ff \
             dst $mac dst-mask 00:00:00:00:00:00 \
             proto 0 proto-mask 0xffff action 15
   [ ... setup BPF/XDP prog on eth0 to steer into shared xsk map ... ]
   # ip netns add foo
   # ip link add numrxqueues 2 nk type netkit single
   # ynl --family netdev --output-json --do queue-create \
         --json "{"ifindex": $(ifindex nk), "type": "rx", \
                  "lease": { "ifindex": $(ifindex eth0), \
                             "queue": { "type": "rx", "id": 15 } } }"
   {'id': 1}
   # ip link set nk netns foo
   # ip netns exec foo ip link set lo up
   # ip netns exec foo ip link set nk up
   # ip netns exec foo qemu-system-x86_64 \
           -kernel $kernel \
           -drive file=${image_name},index=0,media=disk,format=raw \
           -append "root=/dev/sda rw console=ttyS0" \
           -cpu host \
           -m $memory \
           -enable-kvm \
           -device virtio-net-pci,netdev=net0,mac=$mac \
           -netdev af-xdp,ifname=nk,id=net0,mode=native,queues=1,start-queue=1,inhibit=on,map-path=$dir/xsks_map \
           -nographic

We have tested the above against a dual-port Nvidia ConnectX-6 (mlx5) 100G NIC with successful network connectivity out of QEMU. An earlier iteration of this work was presented at LSF/MM/BPF [0] and more recently at LPC [1].

Quoted 10 lines

For getting to a first starting point to connect all things with KubeVirt, bind mounting the xsk map from Cilium into the VM launcher Pod which acts as a regular Kubernetes Pod while not perfect, is not a big problem given its out of reach from the application sitting inside the VM (and some of the control plane aspects are baked in the launcher Pod already), so the isolation barrier is still the VM. Eventually the goal is to have a XDP/XSK redirect extension where there is no need to have the xsk map, and the BPF program can just derive the target xsk through the queue where traffic was received on.

Quoted 3 lines

Sorry, this may be a bit late regarding this commit.

If I remember correctly, eBPF is already supported in netkit. Since eBPF can cover everything XDP does, I’m wondering what the motivation was for adding explicit XDP support to the netkit driver.

Pls see the commit message which explains the details; tl;dr the need was to support zero-copy af_xdp based application in containers to bind against netkit, so that we can have native speed there. (see qemu example which uses af_xdp backend)

author maintainer
#25 lore raw

PATCH v11 14/14 selftests/net: Add queue leasing tests with netkit

In reply to Daniel Borkmann

From: David Wei <[email protected]>

Add extensive selftests for netkit queue leasing, using io_uring zero copy test binary inside of a netns with netkit. This checks that memory providers can be bound against virtual queues in a netkit within a netns that are leasing from a physical netdev in the default netns. Also add various test cases around corner cases for the queue creation itself as well as queue info dumping and teardown in case of netkit in device pair and single mode.

---
 .../testing/selftests/drivers/net/hw/Makefile |    1 +
 .../drivers/net/hw/lib/py/__init__.py         |    4 +-
 .../selftests/drivers/net/hw/nk_qlease.py     | 1407 +++++++++++++++++
 3 files changed, 1410 insertions(+), 2 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/hw/nk_qlease.py
tools/testing/selftests/drivers/net/hw/Makefile +1 -0
index deeca3f8d080..28d245e11bc4 100644
@@ -35,6 +35,7 @@ TEST_PROGS = \
 	loopback.sh \
 	nic_timestamp.py \
 	nk_netns.py \
+	nk_qlease.py \
 	pp_alloc_fail.py \
 	rss_api.py \
 	rss_ctx.py \
tools/testing/selftests/drivers/net/hw/lib/py/__init__.py +2 -2
index df4da5078c48..84a4dab6c649 100644
@@ -20,7 +20,7 @@ try:
     # Import one by one to avoid pylint false positives
     from net.lib.py import NetNS, NetNSEnter, NetdevSimDev
     from net.lib.py import EthtoolFamily, NetdevFamily, NetshaperFamily, \
-        NlError, RtnlFamily, DevlinkFamily, PSPFamily
+        NlError, RtnlFamily, DevlinkFamily, PSPFamily, Netlink
     from net.lib.py import CmdExitFailure
     from net.lib.py import bkg, cmd, bpftool, bpftrace, defer, ethtool, \
         fd_read_timeout, ip, rand_port, rand_ports, wait_port_listen, \
@@ -36,7 +36,7 @@ try:
 
     __all__ = ["NetNS", "NetNSEnter", "NetdevSimDev",
                "EthtoolFamily", "NetdevFamily", "NetshaperFamily",
-               "NlError", "RtnlFamily", "DevlinkFamily", "PSPFamily",
+               "NlError", "RtnlFamily", "DevlinkFamily", "PSPFamily", "Netlink",
                "CmdExitFailure",
                "bkg", "cmd", "bpftool", "bpftrace", "defer", "ethtool",
                "fd_read_timeout", "ip", "rand_port", "rand_ports",
tools/testing/selftests/drivers/net/hw/nk_qlease.py new file +1407 -0

1,407 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#26 lore raw

Re: [PATCH net-next v11 14/14] selftests/net: Add queue leasing tests with netkit

Quoted 6 lines · On Fri, 3 Apr 2026 01:10:31 +0200 Daniel Borkmann wrote:
tools/testing/selftests/drivers/net/hw/nk_qlease.py new file +6 -0
+    ksft_run(
+        [
+            test_remove_phys,
+            test_double_lease,
+            test_virtual_lessor,
+            test_phys_lessee,

tools/testing/selftests/drivers/net/hw/nk_qlease.py line 1403

Quoted 10 lines
tools/testing/selftests/drivers/net/hw/nk_qlease.py new file +9 -0
+            test_different_lessors,
+            test_queue_out_of_range,
+            test_resize_leased,
 
+        # test_destroy must be last because it destroys the netkit devices
+        ksft_run(
+            [test_iou_zcrx, test_attrs, test_attach_xdp_with_mp, test_destroy],
+            args=(cfg,),
+        )
+    ksft_exit()

ksft_run() can't be called multiple times.

The first run looks like it's purely testing netdevsim. So that should move to selftests/net. The rest which tests HW should stay here. Please also move all the setup inside the test cases.

#27 lore raw

Re: [PATCH net-next v11 14/14] selftests/net: Add queue leasing tests with netkit

Quoted 18 lines · Jakub Kicinski
Quoted 6 lines · On Fri, 3 Apr 2026 01:10:31 +0200 Daniel Borkmann wrote:
tools/testing/selftests/drivers/net/hw/nk_qlease.py new file +6 -0
+    ksft_run(
+        [
+            test_remove_phys,
+            test_double_lease,
+            test_virtual_lessor,
+            test_phys_lessee,

tools/testing/selftests/drivers/net/hw/nk_qlease.py line 1403

Quoted 10 lines
tools/testing/selftests/drivers/net/hw/nk_qlease.py new file +9 -0
+            test_different_lessors,
+            test_queue_out_of_range,
+            test_resize_leased,
 
+        # test_destroy must be last because it destroys the netkit devices
+        ksft_run(
+            [test_iou_zcrx, test_attrs, test_attach_xdp_with_mp, test_destroy],
+            args=(cfg,),
+        )
+    ksft_exit()
Quoted 5 lines

ksft_run() can't be called multiple times.

The first run looks like it's purely testing netdevsim. So that should move to selftests/net. The rest which tests HW should stay here. Please also move all the setup inside the test cases.

Sorry, didn't know about multiple ksft_run(). I'll prep the follow up separately so we can land it soon after 7.1.

maintainer
#28 lore raw

Re: [PATCH net-next v11 14/14] selftests/net: Add queue leasing tests with netkit

Quoted 8 lines · On Thu, 9 Apr 2026 08:26:30 -0700 David Wei wrote:
Quoted 5 lines

ksft_run() can't be called multiple times.

The first run looks like it's purely testing netdevsim. So that should move to selftests/net. The rest which tests HW should stay here. Please also move all the setup inside the test cases.

Sorry, didn't know about multiple ksft_run(). I'll prep the follow up separately so we can land it soon after 7.1.

I have a patch queued to make multiple ksft_run()s hard-fail. So let's not wait until after the merge window if that's what you mean.

author maintainer
#29 lore raw
In reply to Daniel Borkmann

Hi Jakub,

Quoted 19 lines · Daniel Borkmann

Containers use virtual netdevs to route traffic from a physical netdev in the host namespace. They do not have access to the physical netdev in the host and thus can't use memory providers or AF_XDP that require reconfiguring/restarting queues in the physical netdev.

This patchset adds the concept of queue leasing to virtual netdevs that allow containers to use memory providers and AF_XDP at native speed. Leased queues are bound to a real queue in a physical netdev and act as a proxy.

Memory providers and AF_XDP operations take an ifindex and queue id, so containers would pass in an ifindex for a virtual netdev and a queue id of a leased queue, which then gets proxied to the underlying real queue.

We have implemented support for this concept in netkit and tested the latter against Nvidia ConnectX-6 (mlx5) as well as Broadcom BCM957504 (bnxt_en) 100G NICs. For more details see the individual patches.

Quoted 7 lines

v10->v11:
- Fix missing mp_ops->uninstall upon unlease path (Gemini)
- Fix dma device retrieval on tx queue when rx queue is leased (Gemini)
- Rework ethtool channel checks to check rx/tx individually (Gemini)
- The remainder of the Gemini findings were non-issues
- Add more extensive net selftests around queue leasing corner cases
- Rebase and retested everything with mlx5 + bnxt_en

I've look into the latest v11 findings from Gemini in the meantime, and thought I'd share an update/comments related to them:

https://sashiko.dev/#/patchset/20260402231031.447597-1-daniel%40iogearbox.net

From all the claims related to the series, the one in the netkit device dump looks reasonable to improve, iow, to not dump IFLA_NETKIT_SCRUB if in single mode since we also reject the presence of the attribute during device creation, change would look like:

- if (nla_put_u32(skb, IFLA_NETKIT_SCRUB, nk->scrub))
- return -EMSGSIZE;
+ if (nk->pair != NETKIT_DEVICE_SINGLE &&
+ nla_put_u32(skb, IFLA_NETKIT_SCRUB, nk->scrub))
+ return -EMSGSIZE;

Anyway, wrt the individual patch comments from Gemini:

Patch 2:
- The capable(CAP_NET_ADMIN) is intentional
Patch 6:
- Solved in your patch which is a good thing to do

    https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

-> Iiuc, its unreachable today due to other guards, but an explicit check
would definitely cement that
- The second double free or use-after-free on unregistered devices claim is not true, there is an inconsistency in the io_uring vs devmem wrt clearing mp_ops to NULL but the code handles both cases correctly
Patch 7:
- Independent of the series; oob queue index in io_uring/zcrx claims that there is no bounds-check against real_num_rx_queues before being passed to netdev_queue_get_dma_dev and from there into ndo_queue_get_dma_dev callback
-> mlx5e_queue_get_dma_dev as the only implementation does bounds check
internally, it might make sense to have a guard in the core code if more drivers implement the ndo_queue_get_dma_dev
- Order-dependent DMA dev validation in netdev_nl_get_dma_dev claim cannot override memory provider state, mixed cases get rejected
Patch 9:
- Intentional, netkit proxies all xsk operations to the phys device
Patch 10:
- Above diff to not dump IFLA_NETKIT_SCRUB makes sense to me
- The changelink operations claim further below is intentional and for now rejected with -EOPNOTSUPP given nothing new can be configured here
Patch 11:
- netkit notifier takes care of this and is part of this series

Some cases in patch 6 and 7 that don't require a specific driver are worth additional tracking in the netkit leasing selftests to asset behavior though. Happy to fold that
+ the IFLA_NETKIT_SCRUB diff above into a v12 or as a follow-up, let me know.

Thanks a lot,
Daniel

#30 lore raw
In reply to Daniel Borkmann
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Fri, 3 Apr 2026 01:10:17 +0200 you wrote:

Containers use virtual netdevs to route traffic from a physical netdev in the host namespace. They do not have access to the physical netdev in the host and thus can't use memory providers or AF_XDP that require reconfiguring/restarting queues in the physical netdev.

This patchset adds the concept of queue leasing to virtual netdevs that allow containers to use memory providers and AF_XDP at native speed. Leased queues are bound to a real queue in a physical netdev and act as a proxy.

[...]

Here is the summary with links:
- [net-next,v11,01/14] net: Add queue-create operation

    https://git.kernel.org/netdev/net-next/c/7789c6bb76ac
  - [net-next,v11,02/14] net: Implement netdev_nl_queue_create_doit
    https://git.kernel.org/netdev/net-next/c/d04686d9bc86
  - [net-next,v11,03/14] net: Add lease info to queue-get response
    https://git.kernel.org/netdev/net-next/c/21d58b35e500
  - [net-next,v11,04/14] net, ethtool: Disallow leased real rxqs to be resized
    https://git.kernel.org/netdev/net-next/c/22fdf28f7c03
  - [net-next,v11,05/14] net: Slightly simplify net_mp_{open,close}_rxq
    https://git.kernel.org/netdev/net-next/c/1e91c98bc9a8
  - [net-next,v11,06/14] net: Proxy netif_mp_{open,close}_rxq for leased queues
    https://git.kernel.org/netdev/net-next/c/5602ad61ebee
  - [net-next,v11,07/14] net: Proxy netdev_queue_get_dma_dev for leased queues
    https://git.kernel.org/netdev/net-next/c/222b5566a02d
  - [net-next,v11,08/14] xsk: Extend xsk_rcv_check validation
    https://git.kernel.org/netdev/net-next/c/9368397fb92a
  - [net-next,v11,09/14] xsk: Proxy pool management for leased queues
    https://git.kernel.org/netdev/net-next/c/910f636db958
  - [net-next,v11,10/14] netkit: Add single device mode for netkit
    https://git.kernel.org/netdev/net-next/c/481038960538
  - [net-next,v11,11/14] netkit: Implement rtnl_link_ops->alloc and ndo_queue_create
    https://git.kernel.org/netdev/net-next/c/b789acc0695c
  - [net-next,v11,12/14] netkit: Add netkit notifier to check for unregistering devices
    https://git.kernel.org/netdev/net-next/c/25444470570b
  - [net-next,v11,13/14] netkit: Add xsk support for af_xdp applications
    https://git.kernel.org/netdev/net-next/c/a14fd6474883
  - [net-next,v11,14/14] selftests/net: Add queue leasing tests with netkit
    https://git.kernel.org/netdev/net-next/c/65d657d80684

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html