Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Jason Xing <kerneljasonxing@gmail.com>
To: willemb@google.com, magnus.karlsson@intel.com,
	maciej.fijalkowski@intel.com, sdf@fomichev.me, bjorn@kernel.org,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org
Cc: netdev@vger.kernel.org, bpf@vger.kernel.org,
	Jason Xing <kerneljasonxing@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH net v4 2/2] xsk: fill in header offsets for TX checksum offload
Date: Tue,  1 Dec 2026 15:57:31 +0800	[thread overview]
Message-ID: <20261201075731.36746-3-kerneljasonxing@gmail.com> (raw)
In-Reply-To: <20261201075731.36746-1-kerneljasonxing@gmail.com>

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:
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. It is
checked against the L3 header length found in the frame, and the L4
protocol has to be TCP or UDP with a matching csum_offset, 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 the
previous commit ("net: reset the mac header in __dev_direct_xmit()").

A frame whose L2 header cannot be parsed, whose L3 type is not IPv4 or
IPv6, whose csum_start does not match the L3 header length, or whose L4
protocol and csum_offset disagree is left untouched, so
validate_xmit_skb() keeps deciding between hardware offload and software
checksum 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 # depends on "net: reset the mac header in __dev_direct_xmit()"
Suggested-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Signed-off-by: Jason Xing <kerneljasonxing@gmail.com>
---
 net/xdp/xsk.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 75 insertions(+), 4 deletions(-)

diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c
index 52d538160e0a..608b76cc0dd5 100644
--- a/net/xdp/xsk.c
+++ b/net/xdp/xsk.c
@@ -11,8 +11,14 @@
 
 #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/tcp.h>
+#include <linux/udp.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/task.h>
@@ -1025,12 +1031,77 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 	return 0;
 }
 
-static int xsk_skb_sw_csum(struct sk_buff *skb, struct xsk_buff_pool *pool)
+static int xsk_skb_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 csum_start, l4_proto;
+	int nhoff = ETH_HLEN;
+	__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), &eth_buf);
+	if (unlikely(!eth))
+		return 0;
+
+	proto = eth->h_proto;
+	if (eth_type_vlan(proto)) {
+		proto = __vlan_get_protocol(skb, proto, &nhoff);
+		if (unlikely(!proto))
+			return 0;
+	}
+
+	csum_start = skb_checksum_start_offset(skb);
+
+	switch (proto) {
+	case htons(ETH_P_IP): {
+		struct iphdr ip_buf, *iph;
+
+		iph = skb_header_pointer(skb, nhoff, sizeof(*iph), &ip_buf);
+		if (!iph || iph->version != 4 || iph->ihl < 5 ||
+		    csum_start != nhoff + iph->ihl * 4)
+			return 0;
+		l4_proto = iph->protocol;
+		break;
+	}
+	case htons(ETH_P_IPV6): {
+		struct ipv6hdr ip6_buf, *ip6h;
+
+		ip6h = skb_header_pointer(skb, nhoff, sizeof(*ip6h), &ip6_buf);
+		if (!ip6h || ip6h->version != 6 ||
+		    csum_start != nhoff + (int)sizeof(*ip6h))
+			return 0;
+		l4_proto = ip6h->nexthdr;
+		break;
+	}
+	default:
+		return 0;
+	}
+
+	switch (l4_proto) {
+	case IPPROTO_TCP:
+		if (skb->csum_offset != offsetof(struct tcphdr, check))
+			return 0;
+		break;
+	case IPPROTO_UDP:
+		if (skb->csum_offset != offsetof(struct udphdr, check))
+			return 0;
+		break;
+	default:
+		return 0;
+	}
+
+	skb->protocol = eth->h_proto;
+	skb_set_network_header(skb, nhoff);
+	skb_set_transport_header(skb, csum_start);
+
 	return 0;
 }
 
@@ -1281,7 +1352,7 @@ static int __xsk_generic_xmit(struct sock *sk)
 			continue;
 		}
 
-		err = xsk_skb_sw_csum(skb, xs->pool);
+		err = xsk_skb_csum(skb, xs->pool);
 		if (unlikely(err)) {
 			xsk_drop_skb(skb);
 			goto out;
-- 
2.43.7


      parent reply	other threads:[~2026-12-01  7:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261201075731.36746-1-kerneljasonxing@gmail.com>
2026-12-01  7:57 ` [PATCH net v4 1/2] net: reset the mac header in __dev_direct_xmit() Jason Xing
2026-12-01  7:57 ` Jason Xing [this message]

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=20261201075731.36746-3-kerneljasonxing@gmail.com \
    --to=kerneljasonxing@gmail.com \
    --cc=bjorn@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=maciej.fijalkowski@intel.com \
    --cc=magnus.karlsson@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=stable@vger.kernel.org \
    --cc=willemb@google.com \
    /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