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=1796401714; b=YAY0dqvBkCQuVN9zLNNYFLLaV2xpVAshXJhUsVcZSBQm8/upAoGDzjMBApn3j2mqQFdE kRs9+3nBowODj9HFTfQHBydWrl5794V3uIrApfTmUYWMLz4q8JD1RYjt3I2usuksyaRaF 0W82dfxKXEIhXA0GdDc1DfQphe6V63/01dlcnNqxX9deVZhPLieYlm+s3ErUbZw86/3LX 0Y0RBTz9SCJ+sL+ih1/m+SHhOHdUqNhHPdixn8IN935P4d+vPzut4BsKmWLTex+RCJemc ClfeB4iXz99ZwSBJrwwJiLryXnvX84asCWGTUTWyF3LlpsXzI1nFTYwFSTm9U88/wdw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401714; 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=KT4rcg7MnPuFjUMa1FJ+4FbgSMvRrAYuZgcMDZHKFxo=; b=YJZXuwI2n22j+EyB6J3i0eyiaZDubdfCrE8GUBV3lN9w8UCTQhRkgji3aoj92gbjToHi 14rTujvo7LyoF7clgKQ6VpwS+qvAyboxRmGJtAAplYeFKPt1jHImZzqtcvLGUun54X/yD vW4BPfcnL7B0GePFzT3Ue9Fv3RSFvCJrPrcKzPhHiaHqxW0zBHVBHYE0i0KJQdIAKfILS 3FVjy1/uaN9yLS5ypZIsWykXqpxQ3hrDOXqFOtRjFVgj+V0OgYu/0WsRzU/bH/hyE/GZf iEVioKkbKOOHFZzRjB6M7EST5w5vArx9lPhz5+PH/GMrlOJhIvSmFQW473EbzKZKoJA== 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=XMTGFZDE; 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 3B029606A322; Fri, 4 Dec 2026 16:28:33 +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=XMTGFZDE Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5934A1F00933; Fri, 4 Dec 2026 16:28:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401713; bh=KT4rcg7MnPuFjUMa1FJ+4FbgSMvRrAYuZgcMDZHKFxo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XMTGFZDEgkWqdXZnhbDFbMGZrvV+hElY167cSqGKpE4oDXfzd2c6tWSJXs75Py0ew zehBHTZB8qSuPUul418S2mMqANv2EW2HS325w9Fi2mnULFD/UjvtMTTI002qUFWcvL NeucqLUEiSkgn5djV3lwhtFSsH46PZOzzFUKplN0eqcZK4R2QFu7hnV4KcrERPo+Ae xwH81LTF4IRwoG3o1C+NkU+Zmfvy2nJ84unpD1oAwSb0iL4zsIlC4dqxSJ7/bdq7kq 0hvOB8F98mvwumLC4iUVCuYkvWfZ20KzOwOiS2fT9S+8UC0FU3SNPWDR8HZDbS0pyw ip5AcWiMe65kQ== 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 06/15] xsk: Deliver packets and return chunks explicitly Date: Fri, 4 Dec 2026 17:27:11 +0100 Message-ID: <20261204162723.755897-7-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: linux-rdma@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 Once a chunk holds several buffers, an RX descriptor can no longer give its chunk back. Deliver redirected XDP-only buffers to the queue's receiving socket, the first bound socket with an RX ring. Each buffer becomes one RX descriptor where the device put it, chained with XDP_PKT_CONTD, with the program's metadata in front of the head. An empty head becomes a zero-length descriptor only if it carries metadata. The socket drops and counts packets that do not fit its UMEM, XDP_USE_SG setting or RX ring. Redirects, traced by the xdp_redirect_map tracepoints, fail with -EOPNOTSUPP outside an XSKMAP and with -EINVAL to another socket. Once the kernel is done with a chunk whose buffer reached the socket, the new XDP_RX_CHUNK_RETURNED marks the descriptor of the buffer that freed it, or an empty descriptor if the device let go later. The RX ring keeps a slot for each chunk owed, so returns never wait, and packets drop that much earlier under overload. Returned chunks count against no page pool, so they pin none after a restart. The provider takes no lock, as the socket drops its references in the queue's NAPI. So the NAPI, or an operation that runs only while that NAPI cannot use the rings, makes every last put of a chunk and every RX ring write. Signed-off-by: Björn Töpel --- Notes (v2changes): Changes in v2: - Rename from "xsk: Deliver to the socket in the device's buffer layout". - Rename XDP_RX_CHUNK_FREE to XDP_RX_CHUNK_RETURNED (Magnus). - Deliver an empty head with metadata as a zero-length first descriptor with XDP_PKT_CONTD. v1 dropped such packets (Sashiko). - Say in the uAPI comment of XDP_PKT_CONTD that single-buffer packets never set it, as the options of their descriptor may carry XDP_RX_CHUNK_RETURNED. - Trace redirects and their errors in the xdp_redirect_map tracepoints, at no cost while they are off (Sashiko). - Stop counting a chunk that goes back to userspace against its page pool, once per poll, so that chunks that userspace keeps hold no old page pool alive after a restart (Sashiko). - A hand-off writes the return descriptor itself, and the RX ring keeps room for the chunks that the device still holds, replacing the list that the next poll posted (Sashiko). include/net/xdp_sock.h | 8 + include/uapi/linux/if_xdp.h | 21 +- net/core/filter.c | 38 ++++ net/xdp/xsk.c | 28 ++- net/xdp/xsk_mp.c | 365 +++++++++++++++++++++++++++++- net/xdp/xsk_mp.h | 5 + tools/include/uapi/linux/if_xdp.h | 21 +- 7 files changed, 463 insertions(+), 23 deletions(-) diff --git a/include/net/xdp_sock.h b/include/net/xdp_sock.h index 6e70b320b399..f56cacd5c343 100644 --- a/include/net/xdp_sock.h +++ b/include/net/xdp_sock.h @@ -126,6 +126,8 @@ struct xsk_tx_metadata_ops { int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp); int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp); void __xsk_map_flush(struct list_head *flush_list); +int xsk_mp_redirect(struct xdp_buff *xdp, enum bpf_map_type map_type, + void *fwd); INDIRECT_CALLABLE_DECLARE(void xsk_destruct_skb(struct sk_buff *)); /** @@ -182,6 +184,12 @@ static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) return -ENOTSUPP; } +static inline int xsk_mp_redirect(struct xdp_buff *xdp, + enum bpf_map_type map_type, void *fwd) +{ + return -EOPNOTSUPP; +} + static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp) { return -EOPNOTSUPP; diff --git a/include/uapi/linux/if_xdp.h b/include/uapi/linux/if_xdp.h index 60f01759a1d4..a319e8d83a97 100644 --- a/include/uapi/linux/if_xdp.h +++ b/include/uapi/linux/if_xdp.h @@ -47,9 +47,12 @@ /* With explicit return, the kernel and the driver decide where packets * land in a chunk, how many share it and the headroom: userspace only - * supplies chunks on FILL. An RX descriptor does not give its chunk - * back, and a packet may span chunks. Needs aligned page-sized chunks - * and no headroom. + * supplies chunks on FILL, and a packet may span chunks. Receiving a + * buffer does not give its chunk back. A chunk is userspace's again + * once an RX descriptor carries XDP_RX_CHUNK_RETURNED for it, or, + * without a descriptor, when the receiving socket closes or the UMEM + * stops backing the queue. Needs aligned page-sized chunks and no + * headroom. */ #define XDP_UMEM_EXPLICIT_RETURN (1 << 4) @@ -182,12 +185,20 @@ struct xdp_desc { /* Flag indicating that the packet continues with the buffer pointed out by the * next frame in the ring. The end of the packet is signalled by setting this - * bit to zero. For single buffer packets, every descriptor has 'options' set - * to 0 and this maintains backward compatibility. + * bit to zero. Single buffer packets never set it, and this maintains + * backward compatibility. */ #define XDP_PKT_CONTD (1 << 0) /* TX packet carries valid metadata. */ #define XDP_TX_METADATA (1 << 1) +/* Flag on an RX descriptor of an XDP_UMEM_EXPLICIT_RETURN UMEM: the + * kernel is done with the UMEM chunk of the descriptor, and puts nothing + * more in it. The chunk is userspace's again. An empty descriptor at the + * chunk carries only this flag, when the device let go of the chunk after + * its last buffer. + */ +#define XDP_RX_CHUNK_RETURNED (1 << 2) + #endif /* _UAPI_LINUX_IF_XDP_H */ diff --git a/net/core/filter.c b/net/core/filter.c index 90e4bf78c689..71a4f93c2b20 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4770,12 +4770,50 @@ __xdp_do_redirect_frame(struct bpf_redirect_info *ri, struct net_device *dev, return err; } +static noinline int xdp_redirect_xdp_only_traced(const struct net_device *dev, + struct xdp_buff *xdp, + const struct bpf_prog *prog, + struct bpf_redirect_info *ri) +{ + enum bpf_map_type map_type = ri->map_type; + void *fwd = ri->tgt_value; + u32 index = ri->tgt_index; + u32 map_id = ri->map_id; + int err; + + ri->map_id = 0; + ri->map_type = BPF_MAP_TYPE_UNSPEC; + err = xsk_mp_redirect(xdp, map_type, fwd); + if (err) + _trace_xdp_redirect_map_err(dev, prog, fwd, map_type, map_id, + index, err); + else + _trace_xdp_redirect_map(dev, prog, fwd, map_type, map_id, + index); + return err; +} + int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp, const struct bpf_prog *xdp_prog) { struct bpf_redirect_info *ri = bpf_net_ctx_get_ri(); enum bpf_map_type map_type = ri->map_type; + if (static_branch_unlikely(&xdp_only_mp_key) && + xdp_buff_is_xdp_only(xdp)) { + void *fwd; + + if (trace_xdp_redirect_enabled() || + trace_xdp_redirect_err_enabled()) + return xdp_redirect_xdp_only_traced(dev, xdp, xdp_prog, + ri); + + fwd = ri->tgt_value; + ri->map_id = 0; + ri->map_type = BPF_MAP_TYPE_UNSPEC; + return xsk_mp_redirect(xdp, map_type, fwd); + } + if (map_type == BPF_MAP_TYPE_XSKMAP) return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog); diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index 48a835e53518..482bb2cd94bf 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -35,6 +35,7 @@ #include "../core/dev.h" #include "xsk_queue.h" +#include "xsk_mp.h" #include "xdp_umem.h" #include "xsk.h" @@ -437,6 +438,12 @@ static bool xsk_dev_queue_valid(const struct xdp_sock *xs, return false; } +/* The pool receives with explicit return, or did */ +static bool xsk_explicit_return(const struct xdp_sock *xs) +{ + return xs->pool->umem->flags & XDP_UMEM_EXPLICIT_RETURN; +} + static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) { if (!xsk_is_bound(xs)) @@ -454,8 +461,14 @@ static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) static void xsk_flush(struct xdp_sock *xs) { + struct xsk_mp *mp = xs->pool->mp; + + if (mp) + xsk_mp_flush(mp); xskq_prod_submit(xs->rx); - __xskq_cons_release(xs->pool->fq); + /* The provider releases FILL as it takes it, see xsk_mp_take_fill() */ + if (!mp) + __xskq_cons_release(xs->pool->fq); sock_def_readable(&xs->sk); } @@ -465,6 +478,11 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) int err; err = xsk_rcv_check(xs, xdp, len); + /* A socket with explicit return takes buffers of its provider only */ + if (!err && xsk_explicit_return(xs)) { + xs->rx_dropped++; + err = -EOPNOTSUPP; + } if (!err) { spin_lock_bh(&xs->pool->rx_lock); err = __xsk_rcv(xs, xdp, len); @@ -489,6 +507,14 @@ static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) return xsk_rcv_zc(xs, xdp, len); } + /* XDP-only buffers take xsk_mp_redirect(); others cannot go to a + * socket with explicit return + */ + if (xsk_explicit_return(xs)) { + xs->rx_dropped++; + return -EOPNOTSUPP; + } + err = __xsk_rcv(xs, xdp, len); if (!err) xdp_return_buff(xdp); diff --git a/net/xdp/xsk_mp.c b/net/xdp/xsk_mp.c index 0e33fd4d9fd5..f7db0d6f788a 100644 --- a/net/xdp/xsk_mp.c +++ b/net/xdp/xsk_mp.c @@ -5,23 +5,48 @@ * ring, and only XDP_RX_CHUNK_RETURNED gives them back. Each chunk * becomes a net_iov of a page pool memory provider, and the device splits it * as it splits pages: a chunk may hold several RX buffers, and a packet may - * span chunks. + * span chunks. Each buffer of a packet that XDP redirects to the socket + * becomes one RX descriptor, at the address the device chose. + * + * The socket drops the page pool reference of each buffer it takes. A chunk + * goes back to userspace once nothing in the kernel uses it, if a buffer of + * it reached the socket: the descriptor of the buffer that freed it carries + * XDP_RX_CHUNK_RETURNED, or, when the device lets go of the chunk later, an + * empty descriptor at the chunk does: + * + * FILL 0x7000 + * RX {0x7100, A} + * RX {0x7900, B, XDP_RX_CHUNK_RETURNED} + * or + * RX {0x7000, 0, XDP_RX_CHUNK_RETURNED} + * + * Such a chunk no longer counts against its page pool, so that chunks that + * userspace keeps hold no page pool alive, and the page pool that takes it + * from FILL counts it again. Chunks whose buffers all stayed in the kernel, as + * for dropped packets, stay in the kernel. * * One context at a time owns the provider state of a queue: the queue's * NAPI, in softirq, busy polling or its thread, or a driver operation that * runs only while that NAPI cannot use the rings, as a queue restart or an * error recovery does. A driver that lets the provider in agrees, see * PP_FLAG_ALLOW_XDP_ONLY_NETMEM. It fills and drains its rings only while - * the queue's NAPI cannot use them, so the owner reads FILL and changes the - * chunk states without a lock. The exception is a page pool that goes - * away: it releases the chunks it never handed out from any context, onto - * a lock-free list. + * the queue's NAPI cannot use them, and the buffers leave it only to the + * queue's socket, which drops its references in that NAPI. So the owner + * makes every last put of a chunk, and reads FILL, writes the RX ring and + * changes the chunk states and counts below without a lock. The exception + * is a page pool that goes away: it releases the chunks it never handed + * out from any context, onto a lock-free list. + * + * The RX ring keeps room for one descriptor per chunk that the kernel owes + * back, so a return never waits for room: a packet that would leave less is + * dropped. * * The kernel maps the chunks through the UMEM mapping, so XDP programs read - * and write the packets in place. The kernel stores none of its own data in - * the chunks: the shared info of a packet lives in the buffer pool, see - * %XDP_FLAGS_SINFO_EXT, and the core turns every verdict that would build an - * skb or a frame in the buffers into a drop. + * and write the packets in place, and their metadata lands in front of the + * first buffer. The kernel stores none of its own data in the chunks: the + * shared info of a packet lives in the buffer pool, see %XDP_FLAGS_SINFO_EXT, + * and the core turns every verdict that would build an skb or a frame in the + * buffers into a drop. */ #include @@ -39,9 +64,11 @@ /* Chunks a refill tops up to with released ones */ #define XSK_MP_RESERVE 32 -/* &net_iov.mp_state: whether userspace or the kernel has the chunk */ +/* &net_iov.mp_state */ enum { + /* Userspace has the chunk */ XSK_MP_USER, + /* The kernel has it */ XSK_MP_LENT, }; @@ -52,10 +79,22 @@ struct xsk_mp { /* NULL once no queue uses the provider */ struct netdev_rx_queue *rxq; refcount_t refs; - /* NULL once no queue refills from the provider. Its consumer is the - * owner of the queue, see the top of this file. + /* NULL once no queue refills from the provider. This and the fields + * up to @owed belong to the owner of the queue, see the top of this + * file. */ struct xsk_queue *fq; + /* Page pool of the chunks that went back to userspace and still count + * against it, and their number, see xsk_mp_returned() + */ + struct page_pool *ret_pp; + u32 ret_nr; + /* Chunks marked mp_hand_off, whose returns the RX ring keeps room + * for, see xsk_mp_rcv() + */ + u32 owed; + /* Set once: see xsk_mp_add_sock() */ + struct xdp_sock *xs; /* One node per chunk: chunks that page pools released, from any * context */ @@ -182,6 +221,104 @@ static u32 xsk_mp_take_released(struct xsk_mp *mp, struct page_pool *pp, return n; } +/* Counts the returned chunks off their page pool, before the RX ring shows + * them and before the owner lets go of the queue, so the batch is empty + * outside a NAPI poll. The page pool, and with it the provider, may go + * once its chunks no longer count against it, so touch neither after that. + */ +static void xsk_mp_flush_returned(struct xsk_mp *mp) +{ + struct page_pool *pp = mp->ret_pp; + u32 nr = mp->ret_nr; + + if (!nr) + return; + mp->ret_pp = NULL; + mp->ret_nr = 0; + net_mp_release_page_pool(pp, nr); +} + +/* A chunk that goes back to userspace no longer counts against its page + * pool, so that chunks that userspace keeps hold no old page pool of a + * restarted queue alive. It keeps the page pool info, see + * xsk_mp_take_fill(). Returns count off their page pool in batches, see + * xsk_mp_flush_returned(). + */ +static void xsk_mp_returned(struct xsk_mp *mp, struct net_iov *niov) +{ + struct page_pool *pp = netmem_get_pp(net_iov_to_netmem(niov)); + + if (pp != mp->ret_pp) { + xsk_mp_flush_returned(mp); + mp->ret_pp = pp; + } + mp->ret_nr++; +} + +/* Gives @niov back on an empty RX descriptor. The device let go of it after + * the socket dropped its references, so this comes after the descriptors + * of its buffers, and the room that the receive path keeps for it is there, + * unless userspace moved the consumer back, which costs it only its own + * descriptors. + */ +static void xsk_mp_return(struct xsk_mp *mp, struct xdp_sock *xs, + struct net_iov *niov) +{ + xsk_mp_returned(mp, niov); + mp->owed--; + /* Pairs with xsk_mp_take_fill() */ + smp_store_release(&niov->mp_state, XSK_MP_USER); + __xskq_prod_reserve_desc(xs->rx, (u64)net_iov_idx(niov) << PAGE_SHIFT, + 0, XDP_RX_CHUNK_RETURNED); +} + +/* Shows what the owner put on the RX ring outside the flush of a poll */ +static void xsk_mp_publish(struct xsk_mp *mp, struct xdp_sock *xs) +{ + xsk_mp_flush_returned(mp); + xskq_prod_submit(xs->rx); + xs->sk.sk_data_ready(&xs->sk); +} + +/* Drops the reference of the buffer of @niov that the socket took at the + * last RX descriptor. Only the owner of the queue changes the count of a + * chunk that delivers to the socket, so this needs no atomic. + */ +static void xsk_mp_drop_buf(struct xsk_mp *mp, struct xdp_sock *xs, + struct net_iov *niov) +{ + struct xdp_rxtx_ring *ring; + atomic_long_t *count; + long left; + + count = netmem_get_pp_ref_count_ref(net_iov_to_netmem(niov)); + left = atomic_long_read(count) - 1; + if (left > 0) { + /* The device still uses it: its last put hands it off, onto + * a descriptor that the RX ring keeps room for + */ + atomic_long_set(count, left); + if (!niov->mp_hand_off) { + niov->mp_hand_off = 1; + mp->owed++; + } + return; + } + + /* Free: back to userspace with this descriptor, with the count of a + * free netmem + */ + atomic_long_set(count, 1); + mp->owed -= niov->mp_hand_off; + niov->mp_hand_off = 0; + xsk_mp_returned(mp, niov); + /* Pairs with xsk_mp_take_fill() */ + smp_store_release(&niov->mp_state, XSK_MP_USER); + ring = (struct xdp_rxtx_ring *)xs->rx->ring; + ring->desc[(xs->rx->cached_prod - 1) & xs->rx->ring_mask].options |= + XDP_RX_CHUNK_RETURNED; +} + static netmem_ref xsk_mp_alloc_netmems(struct page_pool *pp, gfp_t gfp) { struct xsk_mp *mp = pp->mp_priv; @@ -238,6 +375,23 @@ static bool xsk_mp_release_netmem(struct page_pool *pp, netmem_ref netmem) return false; } +/* The last put of a chunk with a buffer in userspace, by the owner of the + * queue: the chunk goes back to userspace + */ +static bool xsk_mp_hand_off(struct page_pool *pp, netmem_ref netmem) +{ + struct net_iov *niov = netmem_to_net_iov(netmem); + struct xsk_mp *mp = pp->mp_priv; + struct xdp_sock *xs; + + niov->mp_hand_off = 0; + /* A buffer of the chunk reached the socket, which stays */ + xs = READ_ONCE(mp->xs); + xsk_mp_return(mp, xs, niov); + xsk_mp_publish(mp, xs); + return true; +} + static int xsk_mp_nl_fill(void *mp_priv, struct sk_buff *rsp, struct netdev_rx_queue *rxq) { @@ -272,6 +426,7 @@ static void xsk_mp_uninstall(void *mp_priv, struct netdev_rx_queue *rxq) static const struct memory_provider_ops xsk_mp_ops = { .alloc_netmems = xsk_mp_alloc_netmems, .release_netmem = xsk_mp_release_netmem, + .hand_off = xsk_mp_hand_off, .init = xsk_mp_init, .destroy = xsk_mp_destroy, .nl_fill = xsk_mp_nl_fill, @@ -424,6 +579,27 @@ int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev, return 0; } +/* The first socket with an RX ring receives; others sharing the pool, + * such as TX-only sockets, do not. + * + * Context: RTNL and the instance lock of the device + */ +void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs) +{ + if (xs->rx && !mp->xs) { + /* The NAPI acquires the socket once it is set up */ + smp_store_release(&mp->xs, xs); + } +} + +/* The receiving socket, if any. + * Context: RTNL and the instance lock of the device + */ +struct xdp_sock *xsk_mp_sock(struct xsk_mp *mp) +{ + return mp->xs; +} + /* Context: RTNL and the instance lock of the device */ void xsk_mp_unbind(struct xsk_buff_pool *pool) { @@ -441,3 +617,168 @@ void xsk_mp_unbind(struct xsk_buff_pool *pool) netif_close(pool->netdev); xsk_mp_detach(pool); } + +/* The net_iov of @netmem if it is one of ours; tells by its address */ +static struct net_iov *xsk_mp_niov(struct xsk_mp *mp, netmem_ref netmem) +{ + struct net_iov *niov; + + if (!netmem_is_net_iov(netmem)) + return NULL; + niov = netmem_to_net_iov(netmem); + if ((unsigned long)(niov - mp->area.niovs) >= mp->area.num_niovs) + return NULL; + return niov; +} + +/* Puts the buffers of a packet on the RX ring, one descriptor each, with + * the metadata of the program in front of the first. Returns 0 if the socket + * took them. + */ +static int xsk_mp_rcv(struct xsk_mp *mp, struct xdp_sock *xs, + struct xdp_buff *xdp) +{ + u32 i, n, k, len, hlen, nr_frags = 0, headroom; + struct skb_shared_info *sinfo; + struct net_iov *niov, *hniov; + const skb_frag_t *frag; + netmem_ref netmem; + u32 metalen = 0; + bool head_desc; + u64 addr, off; + + sinfo = xdp_get_shared_info_from_buff(xdp); + if (xdp_buff_has_frags(xdp)) + nr_frags = sinfo->nr_frags; + + /* The head sits in the UMEM mapping. It is empty when the device + * puts every byte in fragments, as mlx5 does for a nonlinear RQ. + */ + off = (u8 *)xdp->data - (u8 *)mp->area.xdp_vaddr; + if (off >= (u64)mp->area.num_niovs << PAGE_SHIFT) + goto drop; + hniov = &mp->area.niovs[off >> PAGE_SHIFT]; + hlen = xdp->data_end - xdp->data; + if ((off & ~PAGE_MASK) + hlen > PAGE_SIZE) + goto drop; + headroom = min_t(u64, off & ~PAGE_MASK, + xdp->data - xdp->data_hard_start); + + if (!hlen && !nr_frags) + goto drop; + + /* The program put metadata in front of the head, which must be the + * first buffer, within its chunk + */ + if (!xdp_data_meta_unsupported(xdp)) + metalen = xdp->data - xdp->data_meta; + if (metalen > headroom) + goto drop; + + /* An empty head reaches the socket only with metadata in front of + * it, as a descriptor of length zero that XDP_PKT_CONTD chains to + * the data. Without metadata it holds nothing the socket wants. + */ + head_desc = hlen || metalen; + n = nr_frags + head_desc; + if (n > 1 && !xs->sg) + goto drop; + + for (i = 0; i < nr_frags; i++) + if (!xsk_mp_niov(mp, skb_frag_netmem(&sinfo->frags[i]))) + goto drop; + + /* Room for the packet, and for a return of every chunk that the + * kernel owes after it, so returns never wait: those it owes, and one + * per buffer, as the device may keep the chunk of each + */ + k = 2 * n + mp->owed; + if (xskq_prod_nb_free(xs->rx, k) < k) { + xs->rx_queue_full++; + return -ENOBUFS; + } + + /* Puts nothing, so no return lands between the descriptors */ + for (i = 0; i < n; i++) { + if (head_desc && !i) { + niov = hniov; + addr = off; + len = hlen; + } else { + frag = &sinfo->frags[i - head_desc]; + niov = netmem_to_net_iov(skb_frag_netmem(frag)); + addr = ((u64)(niov - mp->area.niovs) << PAGE_SHIFT) + + skb_frag_off(frag); + len = skb_frag_size(frag); + } + __xskq_prod_reserve_desc(xs->rx, addr, len, + i + 1 < n ? XDP_PKT_CONTD : 0); + xsk_mp_drop_buf(mp, xs, niov); + } + + if (!head_desc) { + netmem = net_iov_to_netmem(hniov); + page_pool_put_netmem(netmem_get_pp(netmem), netmem, -1, false); + } + return 0; + +drop: + xs->rx_dropped++; + return -EINVAL; +} + +/** + * xsk_mp_flush - give back chunks at the end of a NAPI poll + * @mp: provider of a socket that received in this poll + * + * Context: the queue NAPI, from xdp_do_flush(), before the RX ring is + * submitted. + */ +void xsk_mp_flush(struct xsk_mp *mp) +{ + xsk_mp_flush_returned(mp); +} + +/** + * xsk_mp_redirect - deliver an XDP-only buffer to a socket of its queue + * @xdp: XDP-only buffer, see xdp_buff_is_xdp_only() + * @map_type: map of the redirect + * @fwd: target of the redirect + * + * The target must be the receiving socket of the queue in an XSKMAP. + * Other maps cannot take XDP-only buffers. + * + * Context: the queue NAPI, from xdp_do_redirect(). + * Return: 0 if the socket took the buffer. + */ +int xsk_mp_redirect(struct xdp_buff *xdp, enum bpf_map_type map_type, + void *fwd) +{ + struct net_iov_area *area = xdp->rxq->xdp_only_area; + struct xdp_sock *xs; + struct xsk_mp *mp; + int err; + + /* Set for every queue with an AF_XDP memory provider, see + * PP_FLAG_ALLOW_XDP_ONLY_NETMEM + */ + if (unlikely(!area)) + return -EINVAL; + mp = container_of(area, struct xsk_mp, area); + + if (map_type != BPF_MAP_TYPE_XSKMAP) + return -EOPNOTSUPP; + + /* Pairs with xsk_mp_add_sock() */ + xs = smp_load_acquire(&mp->xs); + if (unlikely(!xs || xs != fwd)) + return -EINVAL; + + err = xsk_mp_rcv(mp, xs, xdp); + if (err) + return err; + + if (!xs->flush_node.prev) + list_add(&xs->flush_node, bpf_net_ctx_get_xskmap_flush_list()); + return 0; +} diff --git a/net/xdp/xsk_mp.h b/net/xdp/xsk_mp.h index e6c965bd3b2c..46d73fdb40a1 100644 --- a/net/xdp/xsk_mp.h +++ b/net/xdp/xsk_mp.h @@ -5,10 +5,15 @@ #define XSK_MP_H_ struct net_device; +struct xdp_sock; struct xsk_buff_pool; +struct xsk_mp; int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev, u16 queue_id, u16 flags); void xsk_mp_unbind(struct xsk_buff_pool *pool); +void xsk_mp_flush(struct xsk_mp *mp); +void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs); +struct xdp_sock *xsk_mp_sock(struct xsk_mp *mp); #endif /* XSK_MP_H_ */ diff --git a/tools/include/uapi/linux/if_xdp.h b/tools/include/uapi/linux/if_xdp.h index 60f01759a1d4..a319e8d83a97 100644 --- a/tools/include/uapi/linux/if_xdp.h +++ b/tools/include/uapi/linux/if_xdp.h @@ -47,9 +47,12 @@ /* With explicit return, the kernel and the driver decide where packets * land in a chunk, how many share it and the headroom: userspace only - * supplies chunks on FILL. An RX descriptor does not give its chunk - * back, and a packet may span chunks. Needs aligned page-sized chunks - * and no headroom. + * supplies chunks on FILL, and a packet may span chunks. Receiving a + * buffer does not give its chunk back. A chunk is userspace's again + * once an RX descriptor carries XDP_RX_CHUNK_RETURNED for it, or, + * without a descriptor, when the receiving socket closes or the UMEM + * stops backing the queue. Needs aligned page-sized chunks and no + * headroom. */ #define XDP_UMEM_EXPLICIT_RETURN (1 << 4) @@ -182,12 +185,20 @@ struct xdp_desc { /* Flag indicating that the packet continues with the buffer pointed out by the * next frame in the ring. The end of the packet is signalled by setting this - * bit to zero. For single buffer packets, every descriptor has 'options' set - * to 0 and this maintains backward compatibility. + * bit to zero. Single buffer packets never set it, and this maintains + * backward compatibility. */ #define XDP_PKT_CONTD (1 << 0) /* TX packet carries valid metadata. */ #define XDP_TX_METADATA (1 << 1) +/* Flag on an RX descriptor of an XDP_UMEM_EXPLICIT_RETURN UMEM: the + * kernel is done with the UMEM chunk of the descriptor, and puts nothing + * more in it. The chunk is userspace's again. An empty descriptor at the + * chunk carries only this flag, when the device let go of the chunk after + * its last buffer. + */ +#define XDP_RX_CHUNK_RETURNED (1 << 2) + #endif /* _UAPI_LINUX_IF_XDP_H */ -- 2.56.1