From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
To: Magnus Karlsson <magnus.karlsson@gmail.com>
Cc: Magnus Karlsson <magnus.karlsson@intel.com>,
<netdev@vger.kernel.org>, <sdf@fomichev.me>,
<davem@davemloft.net>, <edumazet@kernel.org>, <kuba@kernel.org>,
<pabeni@redhat.com>, <horms@kernel.org>, <bjorn@kernel.org>,
<kerneljasonxing@gmail.com>, <intel-wired-lan@lists.osuosl.org>,
<anthony.l.nguyen@intel.com>, <przemyslaw.kitszel@intel.com>,
<jacob.e.keller@intel.com>
Subject: Re: [PATCH net-next 1/5] xsk: add USO support for skb path
Date: Fri, 20 Nov 2026 12:43:08 +0100 [thread overview]
Message-ID: <awAyTCpCDrEAsHEw@boxer> (raw)
In-Reply-To: <CAJ8uoz1yeh1JGHBiE-jfKzzO3e4uKZu=y2qy79KVsSKga0KkgQ@mail.gmail.com>
On Fri, Nov 20, 2026 at 09:55:54AM +0100, Magnus Karlsson wrote:
> On Thu, 19 Nov 2026 at 19:47, Maciej Fijalkowski
> <maciej.fijalkowski@intel.com> wrote:
> >
> > On Thu, Nov 19, 2026 at 05:53:36PM +0100, Magnus Karlsson wrote:
> > > Add UDP Segmentation Offload (USO) to the AF_XDP Tx metadata interface
> > > and implement it for copy mode. An application submits one Ethernet
> > > frame holding a single IP and UDP header followed by the aggregate UDP
> > > payload, and the device emits a train of MTU-sized UDP datagrams.
> > >
> > > The uAPI adds the XDP_TXMD_FLAGS_USO flag and splits the reserved u32
> > > in the request part of struct xsk_tx_metadata into two u16 fields:
> > > gso_size, the UDP payload bytes per output datagram, and
> > > network_offset, the IP header offset. USO requires
> > > XDP_TXMD_FLAGS_CHECKSUM, with csum_start pointing at the UDP header,
> > > csum_offset at its checksum field and the checksum field seeded with
> > > the partial pseudoheader checksum of the aggregate packet. It cannot
> > > be combined with XDP_TXMD_FLAGS_TIMESTAMP or
> > > XDP_UMEM_TX_SW_CSUM. Launch time is supported and applies to the start
> > > of the segmented packet.
> > >
> > > The request is turned into a GSO skb of type SKB_GSO_UDP_L4. Before
> > > transmission, the headers are validated. The link layer is not parsed:
> > > the IP header starts at network_offset and its version selects
> > > unfragmented IPv4 or IPv6 without extension headers, followed by UDP.
> > > The IP and UDP lengths must describe the aggregate packet, every output
> > > datagram must fit the MTU, and the number of segments is limited to
> > > UDP_MAX_SEGMENTS. As skb drivers find L3 through the EtherType, the skb
> > > path also requires the EtherType, after any VLAN tags, to lead to the
> > > same IP header. A request that fits in one datagram is sent as an
> > > ordinary checksum offload request.
> > >
> > > network_offset is part of the uAPI because csum_start only locates L4,
> > > while segmentation also needs the start of L3: the device rewrites the
> > > IP header of every datagram and needs the L2 and L3 lengths, and on
> > > the skb path it becomes the network header. With it, the kernel does
> > > not need to understand the link layer, and the fields can be reused
> > > later. Any future tunnel segmentation extension to the uAPI could then
> > > treat network_offset as the outer L3 offset and csum_start as the
> > > inner L4 offset, adding only the outer L4 and inner L3 offsets.
> > >
> > > AF_XDP never segments these packets itself. A request that produces more
> > > than one datagram is rejected unless the bound device advertises
> > > NETIF_F_GSO_UDP_L4, and the GSO skb is passed to its driver unsegmented.
> > > As with UDP_SEGMENT sockets, how the segmentation is carried out is up
> > > to the driver. Stacked devices such as VLAN or bonding forward the
> > > packet to a lower device that may segment it in software.
> > >
> > > A new pool field, tx_uso_supported, records whether USO requests are
> > > accepted on a pool. It is set only for Ethernet devices (ARPHRD_ETHER)
> > > and is cleared when XDP_UMEM_TX_SW_CSUM is used.
> > >
> > > A USO request that fails validation is dropped: its buffers are
> > > completed and counted in tx_invalid_descs. Errors in other Tx metadata
> > > keep failing sendmsg() and leave the descriptor on the Tx ring, so the
> > > existing behaviour for non-USO requests is unchanged. A USO request
> > > that cannot be processed for lack of memory is also left on the Tx
> > > ring, and sendmsg() fails with -EAGAIN, unless part of it was already
> > > released to user space, either by an earlier call or while peeking
> > > further descriptors. That part may already have been reused, so the
> > > request is then dropped.
> > >
> > > The header validator works on a raw header copy instead of an skb, so
> > > that the zero-copy path in a later patch can reuse it. It lives in
> > > the new files net/xdp/xsk_tx_uso.c and include/net/xsk_tx_uso.h.
> > >
> > > Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
> >
> > Hmm - this looks good to me!
> > Reviewed-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
> >
> > I only had minor comments/questions.
>
> Thanks for taking a look at this Maciej. Things that I have not
> commented on, I will fix. But please read below for some answers to
> your questions.
>
(...)
> > > +/* On -EOVERFLOW, the packet was dropped and its descriptors were consumed and
> > > + * completed as invalid. Other errors leave the descriptor on the Tx ring for
> > > + * the application to retry.
> > > + */
> > > static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
> > > struct xdp_desc *desc)
> > > {
> > > @@ -1196,13 +1282,17 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
> > > if (skb && !xs->skb)
> > > kfree_skb(skb);
> > >
> > > + /* Too many descriptors and invalid USO requests drop the packet. Only
> > > + * the latter can fail on the first descriptor, which is completed
> > > + * here. Other errors are returned so that the application can retry.
> > > + */
> > > if (err == -EOVERFLOW) {
> > > if (xs->skb) {
> > > /* Drop the packet */
> > > xsk_inc_num_desc(xs->skb);
> > > xsk_drop_skb(xs->skb);
> > > } else {
> > > - xsk_cq_cancel_locked(xs->pool, 1);
> > > + xsk_cq_submit_addr_single_locked(xs->pool, desc);
> > > xs->tx->invalid_descs++;
> > > }
> > > xskq_cons_release(xs->tx);
> > > @@ -1279,6 +1369,28 @@ static int __xsk_generic_xmit(struct sock *sk)
> > > continue;
> > > }
> > >
> > > + if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
> > > + err = xsk_skb_uso(skb);
> > > + /* Descriptors already released to user space may be
> > > + * reused, so the packet can only be put back if none
> > > + * of them has been released. Otherwise it is dropped.
> >
> > This is the only part i don't follow.
>
> Before we tell user-space to retry a multi-buffer packet by rewinding
> the ring, we need to make sure that none of those slots making up the
> packet have already been released to user-space. That would break the
> ring as one of those released slots might already have been filled in
> with new data by user-space.
and release can happen during desc peeking via xskq_cons_get_entries() ?
Let me speak in Sashiko words: "this was not introduced by this patch,
but... :D"
I wonder if it's correct to refresh consumer and therefore expose descs to
user space while we are in the middle of packet gathering.
Yet again, this asks for tx path rework where we first grab all descs and
only then proceed to skb build.
>
> > > + */
> > > + if (err == -ENOMEM &&
> > > + xskq_cons_nb_unreleased(xs->tx) >=
> > > + xsk_get_num_desc(skb)) {
> > > + /* Tell user-space to retry the send */
> > > + xskq_cons_cancel_n(xs->tx, xsk_get_num_desc(skb));
> > > + xsk_consume_skb(skb);
> > > + err = -EAGAIN;
> > > + goto out;
> > > + }
> > > + if (err) {
> > > + xsk_drop_skb(skb);
> > > + err = 0;
> > > + continue;
> > > + }
> > > + }
> > > +
> > > err = xsk_skb_sw_csum(skb, xs->pool);
> > > if (unlikely(err)) {
> > > xsk_drop_skb(skb);
> > > diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c
> > > index 5ec092f7cb6f..e62f3c2b3362 100644
> > > --- a/net/xdp/xsk_buff_pool.c
> > > +++ b/net/xdp/xsk_buff_pool.c
> > > @@ -1,5 +1,6 @@
> > > // SPDX-License-Identifier: GPL-2.0
> > >
> > > +#include <linux/if_arp.h>
> > > #include <linux/netdevice.h>
> > > #include <net/netdev_lock.h>
> > > #include <net/xsk_buff_pool.h>
> > > @@ -192,6 +193,8 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
> > > err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
> > > if (err)
> > > return err;
> > > + pool->tx_uso_supported = !pool->tx_sw_csum &&
> > > + netdev->type == ARPHRD_ETHER;
> > >
> > > if (mbuf)
> > > pool->umem->flags |= XDP_UMEM_SG_FLAG;
> > > @@ -260,6 +263,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
> > > goto err_unreg_xsk;
> > > }
> > > pool->umem->zc = true;
> > > + pool->tx_uso_supported = false;
> >
> > I assume this will be lifted on later patch
>
> Correct.
>
> > > pool->xdp_zc_max_segs = netdev->xdp_zc_max_segs;
> > > return 0;
> > >
> > > diff --git a/net/xdp/xsk_queue.h b/net/xdp/xsk_queue.h
> > > index 1bc42c8902f4..182cd1cccb74 100644
> > > --- a/net/xdp/xsk_queue.h
> > > +++ b/net/xdp/xsk_queue.h
> > > @@ -390,6 +390,12 @@ static inline void xskq_cons_release(struct xsk_queue *q)
> > > q->cached_cons++;
> > > }
> > >
> > > +/* Entries consumed locally but not yet released to the producer. */
> > > +static inline u32 xskq_cons_nb_unreleased(struct xsk_queue *q)
> > > +{
> > > + return q->cached_cons - q->ring->consumer;
> >
> > READ_ONCE(q->ring->consumer) ?
> >
> > > +}
> > > +
> > > static inline void xskq_cons_cancel_n(struct xsk_queue *q, u32 cnt)
> > > {
> > > q->cached_cons -= cnt;
> > > diff --git a/net/xdp/xsk_tx_uso.c b/net/xdp/xsk_tx_uso.c
> > > new file mode 100644
> > > index 000000000000..a7313ade1092
> > > --- /dev/null
> > > +++ b/net/xdp/xsk_tx_uso.c
> > > @@ -0,0 +1,77 @@
> > > +// SPDX-License-Identifier: GPL-2.0
> > > +#include <net/ip.h>
> > > +#include <net/ipv6.h>
> > > +#include <net/udp.h>
> > > +#include <net/xsk_tx_uso.h>
> > > +
> > > +/**
> > > + * xsk_tx_uso_validate - validate a kernel-owned plain UDP header
> > > + * @header: kernel-owned buffer containing at least uso->header_len bytes
> > > + * @uso: packet length and metadata offsets; receives protocol and segment count
> > > + * @mtu: maximum output IP packet length
> > > + *
> > > + * The link-layer header is not parsed. The IP header starts at network_offset
> > > + * and its version selects IPv4 or IPv6. The caller must provide a nonzero
> > > + * gso_size other than GSO_BY_FRAGS and header_len equal to transport_offset
> > > + * plus the UDP header size.
> > > + *
> > > + * Return: zero on success or a negative error code.
> > > + */
> > > +int xsk_tx_uso_validate(const void *header, struct xsk_tx_uso_info *uso,
> > > + u32 mtu)
> > > +{
> > > + const struct xsk_tx_metadata *meta = &uso->metadata;
> > > + u32 nhoff = meta->request.network_offset;
> > > + u32 thoff = meta->request.csum_start;
> > > + u32 udp_len, payload_len, network_len;
> > > + const struct udphdr *uh;
> > > + __be16 protocol;
> > > +
> > > + if (nhoff < ETH_HLEN || thoff <= nhoff || uso->header_len < thoff ||
> > > + uso->len < uso->header_len)
> > > + return -EINVAL;
> > > +
> > > + network_len = uso->len - nhoff;
> > > + switch (((const struct iphdr *)(header + nhoff))->version) {
> > > + case 4: {
> > > + const struct iphdr *iph = header + nhoff;
> > > +
> > > + if (thoff - nhoff < sizeof(*iph) ||
> > > + iph->ihl < 5 || iph->ihl * 4 != thoff - nhoff ||
> > > + ip_is_fragment(iph) ||
> > > + ntohs(iph->tot_len) != network_len)
> >
> > question: i keep on seeing this validation of iphdr - don't we have
> > somewhere a generic routine for that?
>
> I tried finding one but failed. The receive path validators are all
> based on skbs and I do not want to create a fake skb in this path for
> the sake of zero-copy mode. I did look at
> skb_flow_dissect_flow_keys_basic() that can take a raw buffer as
> input. The problem with that one is that it does not validate the
> packet header thoroughly so it would not remove much code as I would
> have to do that anyway. It would only remove two lines and add ten
> more to be exact, so +8 lines and a significant increase in run-time
> as I parse it twice. Another problem is that it can run a BPF program,
> and this program could state that everything is fine with the header,
> even though it is not. I need something predictable that cannot be
> modified by user-space.
>
> But if we could find something reusable, that would be preferred of
> course. I just could not.
I was adding something similar to BPF prog on xskxceiver side hence this
open question, I think let's leave it as is.
>
> > > + return -EINVAL;
> > > + protocol = htons(ETH_P_IP);
> > > + uso->l4_protocol = iph->protocol;
> > > + break;
> > > + }
> > > + case 6: {
> > > + const struct ipv6hdr *ip6h = header + nhoff;
> > > +
> > > + if (thoff - nhoff != sizeof(*ip6h) ||
> > > + ntohs(ip6h->payload_len) != network_len - sizeof(*ip6h))
> > > + return -EINVAL;
> > > + protocol = htons(ETH_P_IPV6);
> > > + uso->l4_protocol = ip6h->nexthdr;
> > > + break;
> > > + }
> > > + default:
> > > + return -EPROTONOSUPPORT;
> > > + }
> > > + uso->protocol = protocol;
> > > + udp_len = uso->len - thoff;
> > > + uh = header + thoff;
> > > + if (uso->l4_protocol != IPPROTO_UDP || udp_len > U16_MAX ||
> > > + ntohs(uh->len) != udp_len)
> > > + return -EINVAL;
> >
> > nit: i think you should start with this validation?
> >
> > > + payload_len = udp_len - sizeof(*uh);
> > > + if (payload_len > meta->request.gso_size * UDP_MAX_SEGMENTS ||
> > > + uso->header_len - nhoff +
> > > + min_t(u32, payload_len, meta->request.gso_size) > mtu)
> > > + return -EINVAL;
> > > +
> > > + uso->gso_segs = max_t(u32, 1, DIV_ROUND_UP(payload_len,
> > > + meta->request.gso_size));
> > > + return 0;
> > > +}
next prev parent reply other threads:[~2026-11-20 11:43 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 16:53 [PATCH net-next 0/5] xsk: ice: USO support for AF_XDP Magnus Karlsson
2026-11-19 16:53 ` [PATCH net-next 1/5] xsk: add USO support for skb path Magnus Karlsson
2026-11-19 18:47 ` Maciej Fijalkowski
2026-11-20 8:55 ` Magnus Karlsson
2026-11-20 11:43 ` Maciej Fijalkowski [this message]
2026-11-19 16:53 ` [PATCH net-next 2/5] ice: add Tx descriptor builders and header length check Magnus Karlsson
2026-11-20 11:59 ` Maciej Fijalkowski
2026-11-19 16:53 ` [PATCH net-next 3/5] xsk: ice: add USO zero-copy support Magnus Karlsson
2026-11-19 16:53 ` [PATCH net-next 4/5] xsk: add KUnit tests for AF_XDP USO header helpers Magnus Karlsson
2026-11-19 16:53 ` [PATCH net-next 5/5] selftests: net: add AF_XDP USO test Magnus Karlsson
2026-11-19 19:05 ` Maciej Fijalkowski
2026-11-20 8:35 ` Magnus Karlsson
-- strict thread matches above, loose matches on Subject: below --
2026-11-19 14:10 [PATCH net-next 0/5] xsk: ice: USO support for AF_XDP Magnus Karlsson
2026-11-19 14:10 ` [PATCH net-next 1/5] xsk: add USO support for skb path Magnus Karlsson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awAyTCpCDrEAsHEw@boxer \
--to=maciej.fijalkowski@intel.com \
--cc=anthony.l.nguyen@intel.com \
--cc=bjorn@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=intel-wired-lan@lists.osuosl.org \
--cc=jacob.e.keller@intel.com \
--cc=kerneljasonxing@gmail.com \
--cc=kuba@kernel.org \
--cc=magnus.karlsson@gmail.com \
--cc=magnus.karlsson@intel.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=sdf@fomichev.me \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox