From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796401795; b=EnhtM11Wc33v8bF1/grsrVEbykzeGqOnGuep4uRszpfQau89JuiaREIRnFxtlHsrrkrJ +5N0UYh5d50z72n44zzm2rlVExG2VzQ6MU8pmTRdP9js0pGKg0e+G+5xwMccvMHVgv8ru ToE0DDZKbXGM2T1PXL8btCHmF9vKgeJXygj1lO22RHUUZygrHuMTdhJeA4DedRvLP4n/O DdgnZo9gyjozbTMaVL808cIvn/L9qSQfeF2mrBr20nWBVmEBcd+yos5XedpnI80IR8zfV 6soy9eVkW1zD2Ea1nDWZBbSq0f1ZuyTXuF7trnQNQEHiMFj7vTJPLOOP55RVPh0usXA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401795; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=sz7togAI6BPpRpbhsAd7nn22VT73XXd3alaDy3dTkz8=; b=NEPodKa8vRqbyFE3vCMqNsLk0U04bLs2OgtogPSiTY4wclwQRIOvlomv4pcIGU1A3biF aEhpx07KLa+OYFCKKYTypGxlxLhEn5QS9dFv6CKA/zShHyDviwjGaMHXH+qAv7bCQjPN2 iFWxGKAZgKmRfbP6xgC871sxc/HPUC8AP3CrkiriFMcEnJB5CQyrR8HW3Ci0sCheWsYIg yXdmCF15sbaNAZLVEw4gCY5C4Q0MA5SVfZVVDO2VbfiSocgtZzmU/KwQMPPhkr6VZVFEM sUUV9GoqcqZ3GExxStMgzWv+vg9KIyY7SqnB/HGocL4CF5H2jdoXqzTPamhwpqilTlQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZGMLMNpB; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F301F606A685; Fri, 4 Dec 2026 16:29:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZGMLMNpB Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B5C01F0092C; Fri, 4 Dec 2026 16:29:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401794; bh=sz7togAI6BPpRpbhsAd7nn22VT73XXd3alaDy3dTkz8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ZGMLMNpBeC7fcO37Gay+GeFH8dFtiCyGoVGt1XxxW3wFQ72vA8WXlq9MAK9I+iN8r kQlg/B+9U1ZCxEY8quDxwh0G6ZiNR5XLqWxIB6tkKvmy5iZjU6Wft6MWRRRPVWW597 wpvPMRDN9DjgRidMQfxS4Iv/uLf0CNi1DKZg5jKwgo0eP9zaFOkqBlUxff29fyzv65 fGborPT42pkyTrI4CmuugQdabmRYaj1okQo4NgE/ehGleX8wep0oRIp1f+XJpPnO5p XFnYe+vh4tP/00wkT1ciI0Es4WkZWcss7wNco/DySnmwObdCadAO/2NrRW7Wwtni1t WlFfR4Rs1uPgg== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Nikolay Aleksandrov , Andrew Lunn , Ilias Apalodimas , Magnus Karlsson , Maciej Fijalkowski , Donald Hunter , Alexander Duyck , Saeed Mahameed , Tariq Toukan , Mark Bloch , Leon Romanovsky , Shuah Khan , Jonathan Corbet , netdev@vger.kernel.org, bpf@vger.kernel.org Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Simon Horman , Stanislav Fomichev , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , David Wei , Tao Cui , linux-kernel@vger.kernel.org, kernel-team@meta.com, "Mike Marciniszyn (Meta)" , linux-rdma@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan , Randy Dunlap , linux-doc@vger.kernel.org Subject: [PATCH net-next v2 15/15] Documentation: xsk: Describe explicit chunk return Date: Fri, 4 Dec 2026 17:27:20 +0100 Message-ID: <20261204162723.755897-16-bjorn@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org> References: <20261204162723.755897-1-bjorn@kernel.org> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit XDP_UMEM_EXPLICIT_RETURN changes who owns a chunk and when, what the program may do, and how chunks come back. None of it is documented. Describe the setup and its errors, the program, receive and chunk return, need_wakeup, DMA syncs, TX of received buffers, and the driver rule. Signed-off-by: Björn Töpel --- Notes (v2changes): Changes in v2: - Rename from "Documentation: networking: Describe AF_XDP RX in the device's layout", and the section from "Device Buffer Layout" to "Explicit Chunk Return". Say that the application cannot influence where packets land, how many share a chunk, or the headroom. - Describe the bind requirements and their errors: CAP_NET_ADMIN in the initial user namespace and XDP_ZEROCOPY (Sashiko), the other EOPNOTSUPP cases, the UMEM size, and binds with XDP_SHARED_UMEM. - Describe empty heads on RX, the headroom, and when need_wakeup is set and cleared, with a table that tells descriptors apart. - Describe what TX may do with received buffers, and when a chunk is the application's again. - Say that the UMEM stops backing the queue once the last socket of the queue is gone, and that a tool that detaches the program right after closing its sockets should retry on EBUSY. - Say that only netlink can replace the program while the UMEM backs the queue. - Give the page pool cache size per range of page sizes, EBUSY for a queue that the device leases to another one, and ENODATA for page pools that do not take the provider (Sashiko). - Say that the RX ring keeps room for the chunks the kernel owes, so returns never wait and need no wakeup, and that the chunks a queue did not return are the application's once it goes or its receiving socket closes (Sashiko). - Say that the kernel does not check a chunk on the FILL rings of two queues, which can only corrupt the application's own data (Sashiko). - Say that a socket whose fd is closed while its rings stay mapped is still bound. - Add a "Drivers" paragraph on the context rule that a driver agrees to with PP_FLAG_ALLOW_XDP_ONLY_NETMEM, and on running the NAPI once after a queue start. Documentation/networking/af_xdp.rst | 244 +++++++++++++++++++++++++++- 1 file changed, 243 insertions(+), 1 deletion(-) diff --git a/Documentation/networking/af_xdp.rst b/Documentation/networking/af_xdp.rst index 538e71adb759..073eab1da103 100644 --- a/Documentation/networking/af_xdp.rst +++ b/Documentation/networking/af_xdp.rst @@ -429,7 +429,9 @@ There is also an option to set the headroom of each single buffer in the UMEM. If you set this to N bytes, it means that the packet will start N bytes into the buffer leaving the first N bytes for the application to use. The final option is the flags field, but it will -be dealt with in separate sections for each UMEM flag. +be dealt with in separate sections for each UMEM flag. With the +XDP_UMEM_EXPLICIT_RETURN flag, chunks are pages that the device fills in +its own layout, see the "Explicit Chunk Return" section. SO_BINDTODEVICE setsockopt -------------------------- @@ -761,6 +763,246 @@ processing. Note that the Tx batch API guarantees that it will provide a batch of Tx descriptors that ends with full packet at the end. This to facilitate extending a zero-copy driver with multi-buffer support. +Explicit Chunk Return +===================== + +In the classic zero-copy mode, the driver fills one UMEM frame per +buffer, at the UMEM headroom, and each RX descriptor gives its frame +back to the application. The driver needs a receive path of its own +for AF_XDP. + +With the XDP_UMEM_EXPLICIT_RETURN UMEM flag, an RX descriptor does not +give its chunk back. The device places buffers in UMEM chunks as it +places them in the pages of its page pool: several buffers may share a +chunk, a packet may span chunks, and the device picks the offsets. A +chunk comes back only when an RX descriptor carries +XDP_RX_CHUNK_RETURNED, and the application must not reuse it before. +The XDP program reads and writes packets in place, in the UMEM, but +the kernel stores none of its own structures in the chunks. + +The application cannot influence the layout. The kernel and the driver +decide where packets land, how many share a chunk, and the headroom in +front of a packet. The application only puts chunks on the FILL ring +and takes back the chunks that XDP_RX_CHUNK_RETURNED returns. + +The UMEM backs the page pools of the bound queue through a memory +provider, so the driver needs no AF_XDP code. A driver supports the +mode if it runs native XDP with XDP_REDIRECT, takes its receive +buffers from a page pool of netmem that allows such providers, and +can restart a queue. + +Setting up +---------- + +* Register the UMEM with XDP_UMEM_EXPLICIT_RETURN, aligned chunks of + PAGE_SIZE, and no headroom. The UMEM must hold more chunks than the + size of each page pool of the queue plus its cache, or the bind + fails with ENOSPC. The driver sizes its page pools from the ring + size and the MTU. The cache holds PP_ALLOC_CACHE_SIZE chunks: 128 + with pages smaller than 16K, 32 with pages from 16K to below 64K, + and 8 with pages of 64K or larger. + +* Attach a native XDP program to the device itself through netlink, not + through a BPF link or an upper device such as a bond, before the bind. + The bind fails with ENOENT without one. While the UMEM backs the + queue, the program cannot be detached (EBUSY), and only netlink can + replace it, not a BPF link, so the queue always runs a program until + the UMEM no longer backs the queue. That happens shortly after the + last socket of the queue is gone, its fd closed and its rings + unmapped: a tool that detaches the program right after closing its + sockets should retry on EBUSY. + +* Put chunks on the FILL ring before the bind. The bind starts the + queue with the chunks it finds there, and the queue drops packets + while it has none. A queue that finds the FILL ring empty sets the + need_wakeup flag of the FILL ring, and takes chunks again at the + wakeup that follows, see below. + +* Bind with XDP_USE_NEED_WAKEUP: a queue that used up the FILL ring + takes chunks again only after a wakeup. A bind without it fails with + EINVAL. Bind with XDP_USE_SG to receive packets that the device + splits over more than one buffer. + +* The bind needs CAP_NET_ADMIN in the initial user namespace, as + io_uring zero-copy receive does, because the queue's program then + drops every packet that it does not redirect to the socket, and + cannot be detached. Without it, the bind fails with EPERM, also for + a process that has CAP_NET_ADMIN only in a user namespace of its + own. + +* XDP_COPY and XDP_ZEROCOPY fail with EOPNOTSUPP. Both ask for a + classic mode, a copy or the driver's own zero-copy path, and this + mode is neither. The bind also fails with EOPNOTSUPP on a device + without native XDP_REDIRECT or queue management, with XDP_USE_SG on + a device without multi-buffer RX (NETDEV_XDP_ACT_RX_SG), on a queue + that the device leases from another device, on a queue without a DMA + device, and on a queue that has no RX queue, such as a TX-only + queue. A queue that the device leases to another device fails with + EBUSY. A queue whose page pools do not take the provider, as on mlx5 + without a striding RQ, fails with ENODATA. + A queue id beyond both the RX and the TX queues fails with EINVAL, as + in the classic modes. + +* These requirements apply to the bind that sets up the queue. A + socket that binds with XDP_SHARED_UMEM to the same queue takes the + setup of the first, and passes no flags of its own. + +* One socket per queue receives: the first one with an RX ring, also + when it binds with XDP_SHARED_UMEM. Another socket with an RX ring + gets EBUSY, and so does any socket with an RX ring once the receiving + socket is gone. Sockets that share the UMEM with XDP_SHARED_UMEM may + transmit. A queue without a receiving socket drops every packet that + its program redirects. The UMEM stops backing the queue shortly after + the last socket of the queue closes, when the kernel tears down its + buffer pool from a workqueue, as with classic zero copy. + +The XDP program +--------------- + +The program works on the packet in place, in the UMEM: direct packet +access, bpf_xdp_load_bytes(), bpf_xdp_store_bytes(), dynptrs, +bpf_xdp_adjust_head(), bpf_xdp_adjust_tail(), bpf_xdp_adjust_meta() +and the RX metadata kfuncs work as with pages, on every buffer of the +packet. What the program writes is in the UMEM for the application to +see. The kernel keeps its own data, such as the fragment list of a +packet that spans buffers, in kernel memory, so the application cannot +change what the kernel relies on. The application may still change +the packet at any time, also while the program runs: two reads of the +same bytes may differ. + +Buffers of the bound queue may only go to its receiving socket. Every +verdict but XDP_REDIRECT drops the packet, XDP_PASS included, and +redirects to anything but the receiving socket fail. + +Redirect through an XSKMAP entry that holds the receiving socket. + +Receiving +--------- + +A FILL entry hands a whole chunk to the kernel, by its address, as +in the classic modes. Each buffer of a packet becomes one RX +descriptor that points at the data, anywhere in a chunk, and +XDP_PKT_CONTD chains the buffers of a packet as described in the +multi-buffer section. In front of the first buffer +the device leaves the headroom that native XDP has on it, so a program +may grow a header as it does without a socket. Metadata that the program +wrote is in front of the first buffer, where the program put it. A +packet whose metadata does not fit in front of its first buffer, in +the same chunk, is dropped. + +The kernel gives a chunk back with XDP_RX_CHUNK_RETURNED in the options +of an RX descriptor: + +* On a descriptor of a packet, the flag means that the kernel is done + with the chunk of that descriptor. The application owns the whole + chunk again. An empty head carries the flag like any other + descriptor of the packet. + +* A descriptor with length zero and without XDP_PKT_CONTD, at the + address of a chunk, only gives that chunk back. The kernel posts one + when the device lets go of the chunk after its last buffer reached the + socket, after the descriptors of its buffers. Such a descriptor is + never part of a packet, and it always carries XDP_RX_CHUNK_RETURNED. + +A first descriptor of length zero with XDP_PKT_CONTD is an empty head: +it carries no bytes, and its address anchors the metadata in front of +it. The data of the packet sits in the descriptors that follow. The +kernel posts one when the device put every byte of a packet in +fragments, as some devices do for frames larger than a page, and the +program wrote metadata. Such a head reaches the socket only on a +socket bound with XDP_USE_SG, as the packet then takes more than one +descriptor. + +XDP_PKT_CONTD tells the two apart, never XDP_RX_CHUNK_RETURNED. The +table assumes that the device puts no fragment of length zero in a +packet, which no driver of this mode does: + +========= ============== ====================== ========================== +Length XDP_PKT_CONTD XDP_RX_CHUNK_RETURNED The descriptor +========= ============== ====================== ========================== +non-zero either either carries data of a packet; + with the flag, its chunk + came back +zero set either is an empty head; with the + flag, its chunk came back +zero clear always set only gives a chunk back +========= ============== ====================== ========================== + +XDP_RX_CHUNK_RETURNED belongs to RX, and a TX descriptor that carries +it is invalid. + +A chunk may go on the FILL ring again once it came back and the +application is done with all its buffers. The kernel never gives back +single buffers, so a packet that the application holds keeps its +chunk. Chunks whose buffers all stayed in the kernel, as for dropped +packets, stay with the kernel. Once the UMEM stops backing the queue, +every chunk that the queue had not returned is the application's +again, without a descriptor. So is every chunk that the kernel still +owes when the receiving socket goes. A socket goes once its fd is +closed and its rings are unmapped, as usual for AF_XDP: a socket whose +fd is closed while its rings stay mapped is still bound. + +The kernel keeps room in the RX ring for one descriptor per chunk that +it still owes back, so a return never waits for room. A packet that +would leave less room is dropped and counted in rx_ring_full, so under +overload packets drop a little earlier than the ring is full. A queue +restart returns the chunks of the old queue as it stops it. + +Within a queue, a FILL entry of a chunk that the kernel still holds is +invalid, and the kernel skips it. Across queues that share a UMEM, the +kernel does not check: as with classic zero copy, a chunk on the FILL +rings of two queues at once may be written by both devices. That +corrupts only the application's data, as the kernel keeps nothing in +the chunks. + +The kernel sets the need_wakeup flag of the FILL ring at bind and when +the queue finds no chunks to take, and clears it once the queue took +chunks, from the FILL ring or from those it kept, as in the classic +zero-copy mode. Check the flag, not the FILL ring: recvmsg() or poll() +then schedules the queue's NAPI. Returns need no wakeup, as the kernel +posts each one as the device lets go of its chunk. With preferred busy +polling, the application's poll runs the queue's NAPI. + +Devices that need DMA syncs work as they do with page pools: the +kernel syncs a chunk for the device when it takes the chunk from the +FILL ring, and the driver syncs each buffer for the CPU before the +program runs. + +Drivers +------- + +The provider takes no lock. A driver that lets it back a page pool, +with PP_FLAG_ALLOW_XDP_ONLY_NETMEM, keeps one context at a time on the +buffers of the queue: its NAPI, or an operation that runs only while +that NAPI cannot use the rings, as when the driver restarts the queue, +changes its configuration or recovers from an error. It fills and +drains the rings only then, and runs the NAPI once after the queue +starts, so that a queue that starts with FILL empty asks for a wakeup. +The buffers leave the driver only to the AF_XDP socket of the queue, +which drops its references in that NAPI. + +Transmitting +------------ + +Sockets in this mode transmit in copy mode only. A buffer that the +application received may go on the TX ring, within these limits: + +* TX takes no descriptor of length zero. A forwarder skips the empty + head of a received packet and starts the packet at the next + descriptor. + +* With XDP_UMEM_TX_METADATA_LEN, every TX descriptor needs that much + room in front of it, in its chunk. Forwarded buffers cannot carry TX + metadata: a buffer that RX delivered may start anywhere in a chunk, + and in a chunk that it shares, the room in front of it may hold the + packet before it, which the metadata would overwrite. + +* A TX completion returns the address of the descriptor, not its + chunk. A chunk is the application's again only once an RX + descriptor returned it with XDP_RX_CHUNK_RETURNED and every TX + descriptor that points into it is on the COMPLETION ring. Only then + may it go on the FILL ring. + Sample application ================== There is a xdpsock benchmarking/test application that can be found at -- 2.56.1