* Re: [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload
2026-11-27 10:53 ` [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload Jason Xing
@ 2026-11-27 16:40 ` Björn Töpel
2026-11-28 11:47 ` netdev-bot+sashiko
1 sibling, 0 replies; 6+ messages in thread
From: Björn Töpel @ 2026-11-27 16:40 UTC (permalink / raw)
To: Jason Xing, willemb, magnus.karlsson, maciej.fijalkowski, sdf,
davem, edumazet, kuba, pabeni, horms
Cc: netdev, bpf, Jason Xing, stable
Jason Xing <kerneljasonxing@gmail.com> writes:
> An XDP_TXMD_FLAGS_CHECKSUM request is turned into an skb carrying
> CHECKSUM_PARTIAL by xsk_skb_metadata() where csum_start/csum_offset/
> ip_summed are set.
>
> However skb->protocol stays 0, the offload never actually reaches
> the hardware and checksum ends up being computed in software every time
> (please see skb_checksum_help() in xsk path):
> 1. harmonize_features() strips the checksum features on devices.
> 2. ice_features_check() rejects the frame on NETIF_F_HW_CSUM capable ice.
>
> The frame layout comes from user space, and request.csum_start is
> already the offset of the L4 header from the start of the frame, so the
> transport header is known exactly.
>
> Only the L2 length has to be found, by parsing the Ethernet header and,
> when present, the VLAN tag stack. skb_header_pointer() is used for that,
> so it works for frames whose data is not in the linear part of the skb.
>
> skb->protocol is set to the outer ethertype, as the rest of the stack does
> for in-band VLAN tags, so skb_vlan_tagged() and vlan_features_check()
> keep seeing the tags. can_checksum_protocol() and the drivers get the L3
> type through vlan_get_protocol(). The mac header is anchored by
> __dev_direct_xmit() in the previous patch.
>
> A frame whose L2 header cannot be parsed, whose L3 type is not IPv4 or
> IPv6, or whose csum_start does not leave room for an L3 header cannot be
> described to the hardware, so it keeps being checksummed in software
> exactly as before.
>
> The patch is based on Maciej suggestion: 1) do not rely on mac header that
> probe in AF_PACKET uses, 2) use skb_header_pointer and walk vlan tags to
> inspect the accurate location of each field that existing drivers
> possibly use.
>
> [1]: https://lore.kernel.org/all/arKm5gCaERk6MaGA@boxer/
>
> Fixes: 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
> Cc: stable@vger.kernel.org
> Suggested-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
> Signed-off-by: Jason Xing <kerneljasonxing@gmail.com>
> ---
> net/xdp/xsk.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
> 1 file changed, 45 insertions(+), 2 deletions(-)
>
> diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
> index 52d538160e0a..8f566996a6dd 100644
> --- a/net/xdp/xsk.c
> +++ b/net/xdp/xsk.c
> @@ -11,8 +11,12 @@
>
> #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
>
> +#include <linux/if_arp.h>
> +#include <linux/if_vlan.h>
> #include <linux/if_xdp.h>
> #include <linux/init.h>
> +#include <linux/ip.h>
> +#include <linux/ipv6.h>
> #include <linux/sched/mm.h>
> #include <linux/sched/signal.h>
> #include <linux/sched/task.h>
> @@ -1027,9 +1031,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
>
> static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
nit: Name! Now this helper has a second job; parse the Ethernet/VLAN
headers and set up the skb for hardware offload, even though the
application didn't ask to force software.
> {
> - if (unlikely(pool->tx_sw_csum &&
> - skb->ip_summed == CHECKSUM_PARTIAL))
> + struct ethhdr eth_buf, *eth;
> + int nhoff = ETH_HLEN;
> + unsigned int l3_min;
> + __be16 proto;
> +
> + if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
> + return 0;
> +
> + if (unlikely(pool->tx_sw_csum))
> + return skb_checksum_help(skb);
> +
> + if (unlikely(skb->dev->type != ARPHRD_ETHER))
> + return 0;
> +
> + eth = skb_header_pointer(skb, 0, sizeof(*eth), ð_buf);
> + if (unlikely(!eth))
> + return skb_checksum_help(skb);
> +
> + proto = eth->h_proto;
> + if (eth_type_vlan(proto)) {
> + proto = __vlan_get_protocol(skb, proto, &nhoff);
> + if (unlikely(!proto))
> + return skb_checksum_help(skb);
> + }
> +
> + switch (proto) {
> + case htons(ETH_P_IP):
> + l3_min = sizeof(struct iphdr);
> + break;
> + case htons(ETH_P_IPV6):
> + l3_min = sizeof(struct ipv6hdr);
> + break;
> + default:
> return skb_checksum_help(skb);
> + }
> +
> + if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
> + return skb_checksum_help(skb);
> +
Forcing software csum when parsing fails seems wrong. Can we return 0
and leave the skb unchanged instead?
Take Ethernet/MPLS/IPv4/UDP with one MPLS label. UDP starts at byte 38
and its checksum field is at byte 44. The parser stops at MPLS, but with
TX checksum offload enabled, virtio can still pass csum_start=38 and
csum_offset=6 to the backend.
skb_checksum_help() can sum bytes in fragments, but it requires the
checksum start and the result field to be in the linear head. Virtio
sets IFF_TX_SKB_NO_LINEAR, so AF_XDP puts all packet bytes in fragments.
The helper returns -EINVAL and AF_XDP drops the packet.
Returning 0 leaves CHECKSUM_PARTIAL and both offsets intact, so the
normal TX path can handle the request as before. The old
XDP_UMEM_TX_SW_CSUM should case is ok!
Björn
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload
2026-11-27 10:53 ` [PATCH net v2 2/2] xsk: fill in header offsets for TX checksum offload Jason Xing
2026-11-27 16:40 ` Björn Töpel
@ 2026-11-28 11:47 ` netdev-bot+sashiko
1 sibling, 0 replies; 6+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 11:47 UTC (permalink / raw)
To: kerneljasonxing
Cc: willemb, magnus.karlsson, maciej.fijalkowski, sdf, bjorn, davem,
edumazet, kuba, pabeni, horms, netdev, bpf, stable
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 3 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] This patch adds `skb_checksum_help()` calls in
`xsk_skb_sw_csum()` (net/xdp/xsk.c) that run even when…
- [Medium] When `pool->tx_sw_csum` is false, `xsk_skb_sw_csum()` now
checksums on the CPU every Ethernet CHECKSUM_PARTIAL frame it cannot…
- [Medium] `xsk_skb_sw_csum()` now sets skb->protocol, network_header and
transport_header from a frame fully controlled by user space.
- [Medium] `xsk_skb_sw_csum()` now passes AF_XDP copy-mode
CHECKSUM_PARTIAL frames to protocol-aware or unit-constrained hardware…
--
xsk: fill in header offsets for TX checksum offload
This patch makes xsk_skb_sw_csum() parse the Ethernet and VLAN headers of
CHECKSUM_PARTIAL AF_XDP copy-mode frames. It then sets skb->protocol and
the network and transport headers so the checksum request can be offloaded
to hardware. Frames whose L2 header can't be parsed, that aren't IPv4/IPv6,
or whose csum_start leaves no room for an L3 header fall back to
skb_checksum_help().
> diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
> index 18cbd50047d80..3827d8e4b642a 100644
> --- a/net/xdp/xsk.c
> +++ b/net/xdp/xsk.c
[ ... ]
> @@ -1028,9 +1032,48 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
>
> static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
> {
> - if (unlikely(pool->tx_sw_csum &&
> - skb->ip_summed == CHECKSUM_PARTIAL))
> + struct ethhdr eth_buf, *eth;
> + int nhoff = ETH_HLEN;
> + unsigned int l3_min;
> + __be16 proto;
> +
> + if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
> + return 0;
> +
> + if (unlikely(pool->tx_sw_csum))
> + return skb_checksum_help(skb);
> +
> + if (unlikely(skb->dev->type != ARPHRD_ETHER))
> + return 0;
> +
> + eth = skb_header_pointer(skb, 0, sizeof(*eth), ð_buf);
> + if (unlikely(!eth))
> + return skb_checksum_help(skb);
[Severity: High]
Can this fallback, and the other new skb_checksum_help() fallbacks below,
break copy-mode transmit on IFF_TX_SKB_NO_LINEAR devices such as
virtio_net?
On those devices xsk_build_skb() goes through xsk_build_skb_zerocopy(),
which only reserves headroom in the linear area:
net/xdp/xsk.c:xsk_build_skb_zerocopy() {
...
skb = sock_alloc_send_skb(&xs->sk, hr, 1, &err);
...
skb_reserve(skb, hr);
...
}
That leaves skb_headlen() at 0. All of the payload sits in page frags
attached with skb_fill_page_desc(), which doesn't set SKBFL_SHARED_FRAG.
So skb_checksum_help() doesn't linearize, and it hits this check:
net/core/dev.c:skb_checksum_help() {
...
if (unlikely(offset >= skb_headlen(skb))) {
DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n",
...
}
It then returns -EINVAL, so __xsk_generic_xmit() drops the frame and
sendmsg() fails:
err = xsk_skb_sw_csum(skb, xs->pool);
if (unlikely(err)) {
xsk_drop_skb(skb);
goto out;
}
User space can reach every one of these fallbacks, because
xsk_skb_metadata() only checks csum_start + csum_offset + 2 <= desc->len.
Any of these frames will do:
- a frame shorter than ETH_HLEN
- an unparsable VLAN stack
- a non-IPv4/IPv6 ethertype
- a csum_start below nhoff + 20 (IPv4) or nhoff + 40 (IPv6)
Before this patch, these frames returned 0 here when tx_sw_csum was off.
xp_assign_dev() also refuses tx_sw_csum on these devices, precisely so
that skb_checksum_help() is never reached for them:
if (pool->tx_sw_csum && (netdev->priv_flags & IFF_TX_SKB_NO_LINEAR))
return -EOPNOTSUPP;
virtio_net advertises NETIF_F_HW_CSUM, so the device computed the
checksum. With this patch the frame is dropped instead, and the user can
trigger a WARN_ONCE plus skb_dump. On panic_on_warn systems that is a
panic.
The commit message says these frames "keep being checksummed in software
exactly as before". Would returning 0 in the fallback cases work better?
That would leave the decision to validate_xmit_skb() as it was before.
> +
> + proto = eth->h_proto;
> + if (eth_type_vlan(proto)) {
> + proto = __vlan_get_protocol(skb, proto, &nhoff);
> + if (unlikely(!proto))
> + return skb_checksum_help(skb);
> + }
> +
> + switch (proto) {
> + case htons(ETH_P_IP):
> + l3_min = sizeof(struct iphdr);
> + break;
> + case htons(ETH_P_IPV6):
> + l3_min = sizeof(struct ipv6hdr);
> + break;
> + default:
> return skb_checksum_help(skb);
[Severity: Medium]
Leaving the non-linear case aside, does this take checksum offload away
from frames that were offloaded before this patch?
Even with skb->protocol == 0, a NETIF_F_HW_CSUM device still offloaded
the frame:
include/linux/netdevice.h:can_checksum_protocol() {
...
if (features & NETIF_F_HW_CSUM) {
/* Can checksum everything */
return true;
}
...
}
skb_csum_hwoffload_help() also returns 0 for NETIF_F_HW_CSUM. On
virtio_net, veth and other HW_CSUM devices, non-IP or unparsable frames
are now checksummed on the CPU instead.
The policy also looks inconsistent. Non-ARPHRD_ETHER devices return 0
and keep offload, but Ethernet devices with an unknown L3 type are forced
into software.
Documentation/networking/xsk-tx-metadata.rst says XDP_TXMD_FLAGS_CHECKSUM
asks the device to compute the checksum. It describes CPU checksumming as
the XDP_UMEM_TX_SW_CSUM option.
For HW_CSUM devices the commit message doesn't match this behaviour
either:
However skb->protocol stays 0, the offload never actually reaches
the hardware and checksum ends up being computed in software every time
and:
... cannot be described to the hardware, so it keeps being checksummed
in software exactly as before.
Since this is tagged for stable, could the commit message describe the
behaviour change accurately? Alternatively, could these fallbacks return
0 so the core path keeps making the decision?
> + }
> +
> + if (unlikely(skb_checksum_start_offset(skb) < nhoff + l3_min))
> + return skb_checksum_help(skb);
[Severity: Medium]
Is checking only the minimal L3 header size enough before handing the
frame to protocol-aware driver checksum paths?
xsk_skb_metadata() only guarantees csum_start + csum_offset + 2 <=
desc->len. Nothing guarantees that a full L4 header exists at
csum_start.
Before this patch, harmonize_features() or ice_features_check() kept
these frames away from the driver offload path. Now take ice E810, which
has IP_CSUM|IPV6_CSUM but no HW_CSUM. ice_tx_csum() takes the L4 protocol
from the user-controlled ip.v4->protocol, and for TCP it reads:
drivers/net/ethernet/intel/ice/ice_txrx.c:ice_tx_csum() {
...
case IPPROTO_TCP:
/* enable checksum offloads */
cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP;
l4_len = l4.tcp->doff;
...
}
For example, take eth(14) + IPv4 with protocol=TCP, csum_start=34,
csum_offset=6 and desc->len=42:
- skb_csum_hwoffload_help() accepts it, since csum_offset 6 equals
offsetof(struct udphdr, check)
- ice_features_check() accepts MACLEN 14 and IPLEN 20
doff is then read at transport_header + 12, up to 5 bytes past
skb->tail. The read stays inside the skb head allocation, but that memory
is uninitialized and KMSAN would flag it.
The value read is programmed into the descriptor's L4LEN field. Whether
a bogus L4LEN can trigger NIC malicious driver detection or TX hangs
hasn't been verified.
AF_PACKET with vnet_hdr has a similar weakness, but this patch newly
exposes the AF_XDP path to it.
Should this require a full TCP/UDP header past csum_start, consistent
with csum_offset, before setting the transport header and skipping the
software checksum?
> +
> + skb->protocol = eth->h_proto;
> + skb_set_network_header(skb, nhoff);
> + skb_set_transport_header(skb, skb_checksum_start_offset(skb));
[Severity: Medium]
Can the hardware end up writing the checksum somewhere other than the
requested csum_start/csum_offset?
Only the ethertype and csum_start >= nhoff + l3_min are checked here.
Drivers, though, program these offsets in coarser units or take the L4
type from the packet itself. Three cases on ice show this.
First, the legacy path (E810) programs IPLEN in dwords:
l3_len = l4.hdr - ip.hdr;
offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S;
ice_features_check() only rejects odd or oversized lengths:
len = skb_network_header_len(skb);
if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
goto out_rm_features;
An L3 length of 22 therefore becomes 5 dwords, and the L4 checksum starts
2 bytes before csum_start.
Second, the L4 type comes from ip.v4->protocol or the IPv6 nexthdr, not
from csum_offset. skb_csum_hwoffload_help() only requires csum_offset to
be 16 or 6.
With protocol=TCP and csum_offset=6, the device writes the TCP checksum
at L4+16 rather than L4+6. With protocol=SCTP (csum_not_inet is 0), it
writes a CRC32c at L4+8 rather than a 16-bit Internet checksum.
Third, the GCS path (E830, HW_CSUM) uses:
u16 csum_offset = skb->csum_offset / 2;
That rounds an odd csum_offset down, so the result lands one byte early.
Before this patch, skb_checksum_help() computed all of these at exactly
csum_start/csum_offset. With this patch, other bytes of the transmitted
frame get overwritten. So the commit message's "exactly as before" claim
doesn't hold for these frames either.
Would it make sense to add these checks, or else leave the decision to
the core path?
- IPv4 ihl * 4 (or the IPv6 header plus extension headers) equals
csum_start - nhoff
- the L4 protocol is TCP or UDP with a matching csum_offset (16 or 6)
- the offsets are even
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127105347.72997-1-kerneljasonxing%40gmail.com
^ permalink raw reply [flat|nested] 6+ messages in thread