All of lore.kernel.org
 help / color / mirror / Atom feed
From: Scott Mitchell <scott.k.mitch1@gmail.com>
To: dev@dpdk.org
Cc: stephen@networkplumber.org, thomas@monjalon.net,
	david.marchand@redhat.com, mb@smartsharesystems.com,
	Scott Mitchell <scott.k.mitch1@gmail.com>
Subject: [PATCH v8 8/8] net/af_packet: add Tx checksum offload
Date: Wed,  2 Dec 2026 09:18:04 -0800	[thread overview]
Message-ID: <20261202171804.50287-9-scott.k.mitch1@gmail.com> (raw)
In-Reply-To: <20261202171804.50287-1-scott.k.mitch1@gmail.com>

Add software IPv4, UDP and TCP Tx checksum offload. Every packet is
already copied into its ring frame, so compute the checksums in the
frame after the copy. The checksum code does not modify the mbuf, so
shared, indirect and multi-segment mbufs need no special handling.

IPv6 extension headers are supported. Packets with tunnel offload flags
are dropped, since their header lengths describe the inner packet.

Signed-off-by: Scott Mitchell <scott.k.mitch1@gmail.com>
---
 app/test/test_pmd_af_packet.c             | 330 ++++++++++++++++++++++
 doc/guides/nics/features/af_packet.ini    |   2 +
 doc/guides/rel_notes/release_27_03.rst    |   4 +
 drivers/net/af_packet/rte_eth_af_packet.c | 106 ++++++-
 4 files changed, 439 insertions(+), 3 deletions(-)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index e2c1eb1161..b42121d8ee 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -24,10 +24,13 @@
 #include <rte_cycles.h>
 #include <rte_ethdev.h>
 #include <rte_ether.h>
+#include <rte_ip.h>
 #include <rte_lcore.h>
 #include <rte_mbuf.h>
 #include <rte_mempool.h>
 #include <rte_stdatomic.h>
+#include <rte_tcp.h>
+#include <rte_udp.h>
 
 #include "test.h"
 
@@ -95,6 +98,12 @@ struct tag_count {
 #define TX_FULL_MIN_FRAMECNT 16u
 #define TX_FULL_MAX_FRAMECNT 64u
 
+/* Frames built by build_cksum_frame() */
+#define CKSUM_DST_PORT 0x5678
+#define CKSUM_PAYLOAD_LEN 32
+#define CKSUM_BAD 0xdead
+#define CKSUM_TCI 0x123
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -2075,6 +2084,326 @@ test_af_packet_tx_ring_full(void)
 	return ret;
 }
 
+/*
+ * Helper: Build an IPv4 or IPv6 UDP/TCP frame that asks for software
+ * checksums, with garbage in the checksum fields and id as L4 source port.
+ */
+static struct rte_mbuf *
+build_cksum_frame(uint8_t proto, bool ipv6, uint16_t id)
+{
+	const size_t l3_len = ipv6 ? sizeof(struct rte_ipv6_hdr) :
+			      sizeof(struct rte_ipv4_hdr);
+	const size_t l4_len = proto == IPPROTO_UDP ? sizeof(struct rte_udp_hdr) :
+			      sizeof(struct rte_tcp_hdr);
+	const size_t len = sizeof(struct rte_ether_hdr) + l3_len + l4_len +
+			   CKSUM_PAYLOAD_LEN;
+	struct rte_ether_hdr *eth;
+	struct rte_mbuf *m;
+	uint8_t *l3, *l4;
+
+	m = rte_pktmbuf_alloc(mp);
+	if (m == NULL)
+		return NULL;
+	eth = (struct rte_ether_hdr *)rte_pktmbuf_append(m, len);
+	if (eth == NULL) {
+		rte_pktmbuf_free(m);
+		return NULL;
+	}
+	memset(eth, 0xa5, len);
+	memset(&eth->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
+	rte_ether_addr_copy(&DEV_MAC_TAG, &eth->src_addr);
+	l3 = (uint8_t *)(eth + 1);
+	l4 = l3 + l3_len;
+
+	if (ipv6) {
+		struct rte_ipv6_hdr *ip6 = (struct rte_ipv6_hdr *)l3;
+
+		eth->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
+		memset(ip6, 0, sizeof(*ip6));
+		ip6->vtc_flow = rte_cpu_to_be_32(6u << 28);
+		ip6->payload_len = rte_cpu_to_be_16(l4_len + CKSUM_PAYLOAD_LEN);
+		ip6->proto = proto;
+		ip6->hop_limits = 64;
+		ip6->src_addr = (struct rte_ipv6_addr)RTE_IPV6(0xfd00, 0, 0, 0, 0, 0, 0, 1);
+		ip6->dst_addr = (struct rte_ipv6_addr)RTE_IPV6(0xfd00, 0, 0, 0, 0, 0, 0, 2);
+		m->ol_flags = RTE_MBUF_F_TX_IPV6;
+	} else {
+		struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)l3;
+
+		eth->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
+		memset(ip, 0, sizeof(*ip));
+		ip->version_ihl = RTE_IPV4_VHL_DEF;
+		ip->total_length = rte_cpu_to_be_16(l3_len + l4_len + CKSUM_PAYLOAD_LEN);
+		ip->time_to_live = 64;
+		ip->next_proto_id = proto;
+		ip->hdr_checksum = CKSUM_BAD;
+		ip->src_addr = rte_cpu_to_be_32(RTE_IPV4(10, 0, 0, 1));
+		ip->dst_addr = rte_cpu_to_be_32(RTE_IPV4(10, 0, 0, 2));
+		m->ol_flags = RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IP_CKSUM;
+	}
+
+	if (proto == IPPROTO_UDP) {
+		struct rte_udp_hdr *udp = (struct rte_udp_hdr *)l4;
+
+		udp->src_port = rte_cpu_to_be_16(id);
+		udp->dst_port = rte_cpu_to_be_16(CKSUM_DST_PORT);
+		udp->dgram_len = rte_cpu_to_be_16(l4_len + CKSUM_PAYLOAD_LEN);
+		udp->dgram_cksum = CKSUM_BAD;
+		m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
+	} else {
+		struct rte_tcp_hdr *tcp = (struct rte_tcp_hdr *)l4;
+
+		memset(tcp, 0, sizeof(*tcp));
+		tcp->src_port = rte_cpu_to_be_16(id);
+		tcp->dst_port = rte_cpu_to_be_16(CKSUM_DST_PORT);
+		tcp->data_off = (sizeof(*tcp) / 4) << 4;
+		tcp->tcp_flags = RTE_TCP_ACK_FLAG;
+		tcp->cksum = CKSUM_BAD;
+		m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
+	}
+	m->l2_len = sizeof(*eth);
+	m->l3_len = l3_len;
+	return m;
+}
+
+/* Helper: Move the bytes of single-segment m from offset at into a second segment. */
+static int
+split_mbuf(struct rte_mbuf *m, uint16_t at)
+{
+	uint16_t n = rte_pktmbuf_data_len(m) - at;
+	struct rte_mbuf *tail;
+	void *dst;
+
+	tail = rte_pktmbuf_alloc(mp);
+	if (tail == NULL)
+		return -1;
+	dst = rte_pktmbuf_append(tail, n);
+	if (dst == NULL) {
+		rte_pktmbuf_free(tail);
+		return -1;
+	}
+	memcpy(dst, rte_pktmbuf_mtod_offset(m, void *, at), n);
+	rte_pktmbuf_trim(m, n);
+	if (rte_pktmbuf_chain(m, tail) != 0) {
+		rte_pktmbuf_free(tail);
+		return -1;
+	}
+	return 0;
+}
+
+/* Helper: Insert an 8-byte Hop-by-Hop header after the IPv6 header of single-segment m. */
+static int
+add_ipv6_hopopts(struct rte_mbuf *m)
+{
+	const uint16_t l4_len = rte_pktmbuf_data_len(m) - m->l2_len - m->l3_len;
+	struct rte_ipv6_hdr *ip6;
+	uint8_t *ext;
+
+	if (rte_pktmbuf_append(m, 8) == NULL)
+		return -1;
+	ip6 = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, m->l2_len);
+	ext = (uint8_t *)(ip6 + 1);
+	memmove(ext + 8, ext, l4_len);
+	/* next header, length 0 (8 bytes), then a 4-byte PadN option */
+	ext[0] = ip6->proto;
+	ext[1] = 0;
+	ext[2] = 1;
+	ext[3] = 4;
+	memset(ext + 4, 0, 4);
+	ip6->proto = IPPROTO_HOPOPTS;
+	ip6->payload_len = rte_cpu_to_be_16(rte_be_to_cpu_16(ip6->payload_len) + 8);
+	m->l3_len += 8;
+	return 0;
+}
+
+/*
+ * Helper: Check a frame from build_cksum_frame(), which may carry a VLAN tag.
+ * Returns its id, -1 if it is some other frame, -2 if a checksum is wrong.
+ */
+static int
+check_cksum_frame(const uint8_t *frame, size_t len)
+{
+	const struct rte_ether_hdr *eth = (const struct rte_ether_hdr *)frame;
+	size_t off = sizeof(*eth);
+	uint16_t type = eth->ether_type;
+	const struct rte_udp_hdr *l4;
+	const void *l3;
+	int bad;
+
+	if (len < off || !rte_is_same_ether_addr(&eth->src_addr, &DEV_MAC_TAG))
+		return -1;
+	if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
+		const struct rte_vlan_hdr *vh = (const struct rte_vlan_hdr *)(frame + off);
+
+		off += sizeof(*vh);
+		if (len < off || rte_be_to_cpu_16(vh->vlan_tci) != CKSUM_TCI)
+			return -1;
+		type = vh->eth_proto;
+	}
+	l3 = frame + off;
+	if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
+		off += sizeof(struct rte_ipv4_hdr);
+	} else if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
+		off += sizeof(struct rte_ipv6_hdr);
+		if (len < off + 2)
+			return -1;
+		/* Hop-by-Hop header from add_ipv6_hopopts() */
+		if (((const struct rte_ipv6_hdr *)l3)->proto == IPPROTO_HOPOPTS)
+			off += 8 * (frame[off + 1] + 1);
+	} else {
+		return -1;
+	}
+	l4 = (const struct rte_udp_hdr *)(frame + off);
+	if (len < off + sizeof(*l4) || l4->dst_port != rte_cpu_to_be_16(CKSUM_DST_PORT))
+		return -1;
+
+	if (type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
+		bad = rte_ipv4_cksum(l3) != 0 || rte_ipv4_udptcp_cksum_verify(l3, l4) != 0;
+	} else {
+		/* verify with the L4 length and protocol, past any extension header */
+		struct rte_ipv6_hdr ph = *(const struct rte_ipv6_hdr *)l3;
+		const size_t ext_len = (const uint8_t *)l4 - (const uint8_t *)l3 - sizeof(ph);
+
+		if (ext_len != 0)
+			ph.proto = ((const uint8_t *)l3)[sizeof(ph)];
+		ph.payload_len = rte_cpu_to_be_16(rte_be_to_cpu_16(ph.payload_len) - ext_len);
+		bad = rte_ipv6_udptcp_cksum_verify(&ph, l4) != 0;
+	}
+	if (bad) {
+		printf("Bad checksum in frame %u\n", rte_be_to_cpu_16(l4->src_port));
+		return -2;
+	}
+	return rte_be_to_cpu_16(l4->src_port);
+}
+
+/*
+ * Test: Software TX checksum offload. Checksums are filled in for IPv4 and
+ * IPv6, UDP and TCP, with an IPv6 extension header, with a VLAN inserted,
+ * with headers split across segments and for a clone whose data must not
+ * change. Frames whose IP length doesn't fit the packet, or with tunnel
+ * offload flags, are dropped.
+ */
+static int
+test_af_packet_tx_cksum(void)
+{
+	const uint64_t offloads = RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
+		RTE_ETH_TX_OFFLOAD_UDP_CKSUM | RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
+		RTE_ETH_TX_OFFLOAD_VLAN_INSERT | RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
+	enum {
+		V4_UDP, V4_TCP, V6_UDP, V6_TCP, V6_EXT, V4_VLAN, V4_SPLIT, V6_CLONE,
+		V4_BAD, V4_TUNNEL, NB_CASES /* frames from V4_BAD on are dropped */
+	};
+	const char *name = "net_af_packet_cksum";
+	struct rte_eth_dev_info dev_info;
+	struct rte_eth_stats stats;
+	struct rte_mbuf *m[NB_CASES] = { NULL };
+	struct rte_mbuf *orig = NULL;
+	const struct rte_tcp_hdr *orig_tcp;
+	uint8_t buf[RTE_MBUF_DEFAULT_BUF_SIZE];
+	unsigned int seen = 0, i;
+	const unsigned int want = (1u << V4_BAD) - 1;
+	uint64_t elapsed;
+	uint16_t pid;
+	ssize_t len;
+	int flags, ret, id;
+
+	if (!tap_created) {
+		printf("SKIPPED: TAP interface not available (need root)\n");
+		return TEST_SKIPPED;
+	}
+
+	ret = create_test_port(name, "iface=" TAP_DEV_NAME, offloads, &pid);
+	TEST_ASSERT(ret == 0, "Failed to create port");
+
+	flags = fcntl(tap_fd, F_GETFL, 0);
+	fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK);
+	tap_drain();
+
+	ret = TEST_FAILED;
+	if (rte_eth_dev_info_get(pid, &dev_info) != 0 ||
+	    (dev_info.tx_offload_capa & offloads) != offloads) {
+		printf("Checksum offloads not advertised\n");
+		goto out;
+	}
+
+	m[V4_UDP] = build_cksum_frame(IPPROTO_UDP, false, V4_UDP);
+	m[V4_TCP] = build_cksum_frame(IPPROTO_TCP, false, V4_TCP);
+	m[V6_UDP] = build_cksum_frame(IPPROTO_UDP, true, V6_UDP);
+	m[V6_TCP] = build_cksum_frame(IPPROTO_TCP, true, V6_TCP);
+	m[V6_EXT] = build_cksum_frame(IPPROTO_UDP, true, V6_EXT);
+	m[V4_VLAN] = build_cksum_frame(IPPROTO_TCP, false, V4_VLAN);
+	m[V4_SPLIT] = build_cksum_frame(IPPROTO_UDP, false, V4_SPLIT);
+	orig = build_cksum_frame(IPPROTO_TCP, true, V6_CLONE);
+	m[V4_BAD] = build_cksum_frame(IPPROTO_UDP, false, V4_BAD);
+	m[V4_TUNNEL] = build_cksum_frame(IPPROTO_UDP, false, V4_TUNNEL);
+	if (orig != NULL)
+		m[V6_CLONE] = rte_pktmbuf_clone(orig, mp);
+	for (i = 0; i < NB_CASES; i++)
+		if (m[i] == NULL)
+			break;
+	if (i < NB_CASES) {
+		rte_pktmbuf_free_bulk(m, NB_CASES);
+		goto out;
+	}
+
+	m[V4_VLAN]->ol_flags |= RTE_MBUF_F_TX_VLAN;
+	m[V4_VLAN]->vlan_tci = CKSUM_TCI;
+	m[V4_TUNNEL]->ol_flags |= RTE_MBUF_F_TX_TUNNEL_VXLAN;
+	/* V4_SPLIT is split inside the IPv4 header */
+	if (add_ipv6_hopopts(m[V6_EXT]) != 0 ||
+	    split_mbuf(m[V4_SPLIT], m[V4_SPLIT]->l2_len + 10) != 0) {
+		rte_pktmbuf_free_bulk(m, NB_CASES);
+		goto out;
+	}
+	rte_pktmbuf_mtod_offset(m[V4_BAD], struct rte_ipv4_hdr *,
+				m[V4_BAD]->l2_len)->total_length = rte_cpu_to_be_16(1500);
+
+	rte_eth_stats_reset(pid);
+	if (do_tx_burst(pid, 0, m, NB_CASES) != NB_CASES) {
+		printf("TX burst was short\n");
+		goto out;
+	}
+
+	orig_tcp = rte_pktmbuf_mtod_offset(orig, const struct rte_tcp_hdr *,
+					   orig->l2_len + orig->l3_len);
+	if (orig_tcp->cksum != CKSUM_BAD) {
+		printf("TX modified the data of a cloned mbuf\n");
+		goto out;
+	}
+
+	for (elapsed = 0; seen != want && elapsed < TAP_READ_TIMEOUT_US;) {
+		len = read(tap_fd, buf, sizeof(buf));
+		if (len <= 0) {
+			rte_delay_us_block(STATS_POLL_INTERVAL_US);
+			elapsed += STATS_POLL_INTERVAL_US;
+			continue;
+		}
+		id = check_cksum_frame(buf, len);
+		if (id == -2 || id >= V4_BAD) {
+			if (id >= V4_BAD)
+				printf("Frame %d should have been dropped\n", id);
+			goto out;
+		}
+		if (id >= 0 && id < NB_CASES)
+			seen |= 1u << id;
+	}
+	if (seen != want) {
+		printf("Missing frames: seen 0x%x, want 0x%x\n", seen, want);
+		goto out;
+	}
+	if (rte_eth_stats_get(pid, &stats) != 0 || stats.oerrors != NB_CASES - V4_BAD) {
+		printf("oerrors=%" PRIu64 ", expected %d\n", stats.oerrors, NB_CASES - V4_BAD);
+		goto out;
+	}
+	ret = TEST_SUCCESS;
+out:
+	rte_pktmbuf_free(orig);
+	tap_drain();
+	fcntl(tap_fd, F_SETFL, flags);
+	destroy_test_port(name, pid);
+	return ret;
+}
+
 static struct unit_test_suite af_packet_test_suite = {
 	.suite_name = "AF_PACKET PMD Unit Test Suite",
 	.setup = test_af_packet_setup,
@@ -2104,6 +2433,7 @@ static struct unit_test_suite af_packet_test_suite = {
 		TEST_CASE(test_af_packet_tx_vlan_oversize),
 		TEST_CASE(test_af_packet_tx_mbuf_accounting),
 		TEST_CASE(test_af_packet_tx_ring_full),
+		TEST_CASE(test_af_packet_tx_cksum),
 		TEST_CASE(test_af_packet_qdisc_bypass),
 		TEST_CASE(test_af_packet_multi_queue),
 		TEST_CASE(test_af_packet_fanout_modes),
diff --git a/doc/guides/nics/features/af_packet.ini b/doc/guides/nics/features/af_packet.ini
index b1cbc54aca..275a120f0f 100644
--- a/doc/guides/nics/features/af_packet.ini
+++ b/doc/guides/nics/features/af_packet.ini
@@ -6,4 +6,6 @@
 [Features]
 Promiscuous mode     = Y
 MTU update           = Y
+L3 checksum offload  = Y
+L4 checksum offload  = Y
 Stats per queue      = Y
diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst
index a91813faca..dd9e8a57bb 100644
--- a/doc/guides/rel_notes/release_27_03.rst
+++ b/doc/guides/rel_notes/release_27_03.rst
@@ -55,6 +55,10 @@ New Features
      Also, make sure to start the actual text at the margin.
      =======================================================
 
+* **Updated af_packet net driver.**
+
+  * Added software TX checksum offload for IPv4, UDP and TCP.
+
 
 Removed Items
 -------------
diff --git a/drivers/net/af_packet/rte_eth_af_packet.c b/drivers/net/af_packet/rte_eth_af_packet.c
index 1535a93dca..bf847ab602 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -9,6 +9,9 @@
 #include <rte_common.h>
 #include <rte_string_fns.h>
 #include <rte_mbuf.h>
+#include <rte_ip.h>
+#include <rte_tcp.h>
+#include <rte_udp.h>
 #include <rte_stdatomic.h>
 #include <ethdev_driver.h>
 #include <ethdev_vdev.h>
@@ -331,6 +334,94 @@ tx_ring_status_available(uint32_t tp_status)
 	return tp_status == TP_STATUS_AVAILABLE;
 }
 
+#define AF_PACKET_TX_CKSUM_FLAGS (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_L4_MASK)
+
+/*
+ * Fill in the checksums m asks for in its copy at data; the mbuf is not
+ * written. Returns false if m's offload fields don't match the packet.
+ */
+static inline bool
+af_packet_tx_cksum(uint8_t *data, const struct rte_mbuf *m)
+{
+	const uint64_t ol_flags = m->ol_flags;
+	const uint32_t l4_off = m->l2_len + m->l3_len;
+	void *l3 = data + m->l2_len;
+	uint8_t *l4 = data + l4_off;
+	unaligned_uint16_t *l4_cksum;
+	uint32_t l4_hdr_len, l4_end;
+	uint8_t l4_proto;
+
+	/* l2_len/l3_len describe inner headers for tunnels, which aren't supported */
+	if (unlikely(ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK))
+		return false;
+
+	switch (ol_flags & RTE_MBUF_F_TX_L4_MASK) {
+	case RTE_MBUF_F_TX_UDP_CKSUM:
+		l4_cksum = (unaligned_uint16_t *)(l4 + offsetof(struct rte_udp_hdr, dgram_cksum));
+		l4_hdr_len = sizeof(struct rte_udp_hdr);
+		l4_proto = IPPROTO_UDP;
+		break;
+	case RTE_MBUF_F_TX_TCP_CKSUM:
+		l4_cksum = (unaligned_uint16_t *)(l4 + offsetof(struct rte_tcp_hdr, cksum));
+		l4_hdr_len = sizeof(struct rte_tcp_hdr);
+		l4_proto = IPPROTO_TCP;
+		break;
+	case RTE_MBUF_F_TX_L4_NO_CKSUM:
+		l4_cksum = NULL;
+		l4_hdr_len = 0;
+		l4_proto = 0;
+		break;
+	default:
+		return false;
+	}
+
+	/* lib/net takes the L4 length from the IP header; keep it in the packet */
+	if (ol_flags & RTE_MBUF_F_TX_IPV4) {
+		struct rte_ipv4_hdr *ip = l3;
+
+		if (unlikely(m->l3_len < sizeof(*ip) || l4_off + l4_hdr_len > m->pkt_len ||
+			     rte_ipv4_hdr_len(ip) != m->l3_len))
+			return false;
+		l4_end = m->l2_len + rte_be_to_cpu_16(ip->total_length);
+		if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
+			ip->hdr_checksum = 0;
+			ip->hdr_checksum = rte_ipv4_cksum(ip);
+		}
+	} else if ((ol_flags & (RTE_MBUF_F_TX_IPV6 | RTE_MBUF_F_TX_IP_CKSUM)) ==
+		   RTE_MBUF_F_TX_IPV6) {
+		const struct rte_ipv6_hdr *ip6 = l3;
+
+		/* l3_len includes any extension headers */
+		if (unlikely(m->l3_len < sizeof(*ip6) || l4_off + l4_hdr_len > m->pkt_len))
+			return false;
+		l4_end = m->l2_len + sizeof(*ip6) + rte_be_to_cpu_16(ip6->payload_len);
+	} else {
+		return false;
+	}
+
+	if (l4_cksum != NULL) {
+		if (unlikely(l4_end < l4_off + l4_hdr_len || l4_end > m->pkt_len))
+			return false;
+		*l4_cksum = 0;
+		if (ol_flags & RTE_MBUF_F_TX_IPV4) {
+			*l4_cksum = rte_ipv4_udptcp_cksum(l3, l4);
+		} else {
+			const struct rte_ipv6_hdr *ip6 = l3;
+			struct rte_ipv6_hdr ph;
+
+			/* after extension headers: pseudo-header of the L4 length and protocol */
+			if (unlikely(m->l3_len != sizeof(ph))) {
+				ph = *ip6;
+				ph.payload_len = rte_cpu_to_be_16(l4_end - l4_off);
+				ph.proto = l4_proto;
+				ip6 = &ph;
+			}
+			*l4_cksum = rte_ipv6_udptcp_cksum(ip6, l4);
+		}
+	}
+	return true;
+}
+
 /*
  * Callback to handle sending packets through a real NIC.
  */
@@ -339,7 +430,7 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 {
 	struct tpacket2_hdr *ppd;
 	struct rte_mbuf *mbuf;
-	uint8_t *pbuf;
+	uint8_t *data, *pbuf;
 	unsigned int framecount, framenum;
 	struct pkt_tx_queue *pkt_q = queue;
 	uint16_t num_tx = 0;
@@ -366,7 +457,8 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 			break;
 
 		/* copy the tx frame data */
-		pbuf = (uint8_t *)ppd + ETH_AF_PACKET_TX_FRAME_OVERHEAD;
+		data = (uint8_t *)ppd + ETH_AF_PACKET_TX_FRAME_OVERHEAD;
+		pbuf = data;
 
 		struct rte_mbuf *tmp_mbuf = mbuf;
 		while (tmp_mbuf) {
@@ -376,6 +468,11 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 			tmp_mbuf = tmp_mbuf->next;
 		}
 
+		/* drop if checksums can't be done; the frame is reused */
+		if ((mbuf->ol_flags & AF_PACKET_TX_CKSUM_FLAGS) != 0 &&
+		    unlikely(!af_packet_tx_cksum(data, mbuf)))
+			continue;
+
 		ppd->tp_len = mbuf->pkt_len;
 		ppd->tp_snaplen = mbuf->pkt_len;
 
@@ -480,7 +577,10 @@ eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 	dev_info->max_tx_queues = (uint16_t)internals->nb_queues;
 	dev_info->min_rx_bufsize = 0;
 	dev_info->tx_offload_capa = RTE_ETH_TX_OFFLOAD_MULTI_SEGS |
-		RTE_ETH_TX_OFFLOAD_VLAN_INSERT;
+		RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
+		RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
+		RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
+		RTE_ETH_TX_OFFLOAD_TCP_CKSUM;
 	dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP |
 		RTE_ETH_RX_OFFLOAD_TIMESTAMP |
 		RTE_ETH_RX_OFFLOAD_SCATTER;
-- 
2.54.0 (Apple Git-157)


      parent reply	other threads:[~2026-12-02 17:20 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-27 18:13 [PATCH v1 0/3] net/af_packet: correctness fixes and improvements scott.k.mitch1
2026-01-27 18:13 ` [PATCH v1 1/3] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-01-27 18:39   ` Stephen Hemminger
2026-01-28  1:35     ` Scott Mitchell
2026-01-27 18:13 ` [PATCH v1 2/3] net/af_packet: RX/TX rte_memcpy, bulk free, prefetch scott.k.mitch1
2026-01-27 18:54   ` Stephen Hemminger
2026-01-28  1:23     ` Scott Mitchell
2026-01-28  9:49       ` Morten Brørup
2026-01-28 15:37         ` Scott Mitchell
2026-01-28 16:57           ` Stephen Hemminger
2026-01-27 18:13 ` [PATCH v1 3/3] net/af_packet: software checksum and tx poll control scott.k.mitch1
2026-01-27 18:57   ` Stephen Hemminger
2026-01-28  7:05     ` Scott Mitchell
2026-01-28 17:36       ` Stephen Hemminger
2026-01-28 18:59         ` Scott Mitchell
2026-01-27 20:45   ` [REVIEW] " Stephen Hemminger
2026-01-28  9:36 ` [PATCH v2 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-01-28  9:36   ` [PATCH v2 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-01-28 16:59     ` Stephen Hemminger
2026-01-28 18:00       ` Scott Mitchell
2026-01-28 18:28         ` Stephen Hemminger
2026-01-28  9:36   ` [PATCH v2 2/4] net/af_packet: RX/TX unlikely, bulk free, prefetch scott.k.mitch1
2026-01-28  9:36   ` [PATCH v2 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-01-28  9:36   ` [PATCH v2 4/4] net/af_packet: software checksum scott.k.mitch1
2026-01-28 18:27     ` Stephen Hemminger
2026-01-28 19:08       ` Scott Mitchell
2026-01-28 19:10   ` [PATCH v3 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-01-28 19:10     ` [PATCH v3 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-01-28 19:10     ` [PATCH v3 2/4] net/af_packet: RX/TX unlikely, bulk free, prefetch scott.k.mitch1
2026-01-29  1:07       ` Stephen Hemminger
2026-02-02  5:29         ` Scott Mitchell
2026-01-28 19:10     ` [PATCH v3 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-01-28 19:10     ` [PATCH v3 4/4] net/af_packet: software checksum scott.k.mitch1
2026-01-28 21:57       ` [REVIEW] " Stephen Hemminger
2026-02-02  7:55         ` Scott Mitchell
2026-02-02 16:58           ` Stephen Hemminger
2026-02-02  8:14     ` [PATCH v4 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 2/4] net/af_packet: RX/TX bulk free, unlikely hint scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-02-02  8:14       ` [PATCH v4 4/4] net/af_packet: add software checksum offload support scott.k.mitch1
2026-02-02 17:00         ` Stephen Hemminger
2026-02-02 18:47         ` Stephen Hemminger
2026-02-03  6:41           ` Scott Mitchell
2026-02-02 18:53       ` [PATCH v4 0/4] af_packet correctness, performance, cksum Stephen Hemminger
2026-02-03  7:07       ` [PATCH v5 " scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 2/4] net/af_packet: RX/TX bulk free, unlikely hint scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-02-03  7:07         ` [PATCH v5 4/4] net/af_packet: add software checksum offload support scott.k.mitch1
2026-02-03  8:20           ` Scott Mitchell
2026-02-03 14:12             ` Stephen Hemminger
2026-02-04  2:59               ` Scott Mitchell
2026-02-03 14:13           ` Stephen Hemminger
2026-02-04  1:39             ` Scott Mitchell
2026-02-05 21:27               ` Stephen Hemminger
2026-02-06  1:11         ` [PATCH v6 0/4] af_packet correctness, performance, cksum scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 1/4] net/af_packet: fix thread safety and frame calculations scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 2/4] net/af_packet: RX/TX bulk free, unlikely hint scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 3/4] net/af_packet: tx poll control scott.k.mitch1
2026-02-06  1:11           ` [PATCH v6 4/4] net/af_packet: add software checksum offload support scott.k.mitch1
2026-02-06  1:49           ` [PATCH v6 0/4] af_packet correctness, performance, cksum Stephen Hemminger
2026-02-06  4:45             ` Scott Mitchell
2026-02-06 14:36             ` Morten Brørup
2026-02-06 16:11               ` Stephen Hemminger
2026-12-01 22:24           ` [PATCH v7 0/4] net/af_packet: " Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 1/4] net/af_packet: fix thread safety and frame calculations Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 2/4] net/af_packet: RX/TX bulk free, unlikely hint Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 3/4] net/af_packet: tx poll control Scott Mitchell
2026-12-01 22:24             ` [PATCH v7 4/4] net/af_packet: add software checksum offload support Scott Mitchell
2026-12-01 23:22               ` Scott Mitchell
2026-12-01 22:32             ` [PATCH v7 0/4] net/af_packet: correctness, performance, cksum Scott Mitchell
2026-12-02  7:09             ` Stephen Hemminger
2026-12-02  8:01             ` Morten Brørup
2026-12-02 16:11               ` Scott Mitchell
2026-12-02 17:17             ` [PATCH v8 0/8] net/af_packet fixes and Tx features Scott Mitchell
2026-12-02 17:17               ` [PATCH v8 1/8] net/af_packet: fix frame status memory ordering Scott Mitchell
2026-12-02 17:17               ` [PATCH v8 2/8] net/af_packet: fix frame address with unused block tail Scott Mitchell
2026-12-02 17:17               ` [PATCH v8 3/8] net/af_packet: fix maximum Rx packet length Scott Mitchell
2026-12-02 17:18               ` [PATCH v8 4/8] test/af_packet: add frame layout tests Scott Mitchell
2026-12-02 17:18               ` [PATCH v8 5/8] net/af_packet: reject invalid ring parameters at probe Scott Mitchell
2026-12-02 17:18               ` [PATCH v8 6/8] net/af_packet: clean up Tx and bulk free mbufs Scott Mitchell
2026-12-02 17:18               ` [PATCH v8 7/8] net/af_packet: do not block in Tx when the ring is full Scott Mitchell
2026-12-02 17:18               ` Scott Mitchell [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=20261202171804.50287-9-scott.k.mitch1@gmail.com \
    --to=scott.k.mitch1@gmail.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=mb@smartsharesystems.com \
    --cc=stephen@networkplumber.org \
    --cc=thomas@monjalon.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.