DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Scott Mitchell <scott.k.mitch1@gmail.com>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Thomas Monjalon <thomas@monjalon.net>,
	David Marchand <david.marchand@redhat.com>,
	Scott Mitchell <scott.k.mitch1@gmail.com>,
	linville@tuxdriver.com, stable@dpdk.org
Subject: [PATCH v7 1/4] net/af_packet: fix thread safety and frame calculations
Date: Tue,  1 Dec 2026 14:24:02 -0800	[thread overview]
Message-ID: <20261201222405.88922-2-scott.k.mitch1@gmail.com> (raw)
In-Reply-To: <20261201222405.88922-1-scott.k.mitch1@gmail.com>

tp_status was accessed without barriers. On weakly ordered CPUs, RX can
read frame data before seeing the status and TX can publish the status
before the frame is written. Read tp_status then an acquire fence, and
use a release fence before writing it, pairing with the kernel.

Frame addresses assumed a contiguous ring, but the kernel lays frames
out per block (packet_lookup_frame()). When frames don't divide the
block size evenly, frames were read and written at the wrong offsets.

Fixes: 364e08f2bbc0 ("af_packet: add PMD for AF_PACKET-based virtual devices")
Cc: linville@tuxdriver.com
Cc: stable@dpdk.org

Signed-off-by: Scott Mitchell <scott.k.mitch1@gmail.com>
---
 app/test/test_pmd_af_packet.c             | 368 ++++++++++++++++++++++
 drivers/net/af_packet/rte_eth_af_packet.c | 147 ++++++---
 2 files changed, 479 insertions(+), 36 deletions(-)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index 81ab19031e..3c96ee0045 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -13,6 +13,7 @@
 #include <pthread.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
@@ -77,6 +78,17 @@ struct tag_count {
 #define AF_PACKET_DEV_NAME "net_af_packet_test"
 #define TAP_DEV_NAME "dpdkafptest"
 
+/* Frames built by fill_seq_frame(), with an IEEE local experimental EtherType */
+#define TEST_ETHER_TYPE 0x88B5
+#define TAP_READ_TIMEOUT_US 200000    /* 200ms to read frames from the TAP */
+#define SEQ_TX_BURST 8
+
+/* Frame layout tests: 2 frames per 4096-byte block, with 1024 bytes unused */
+#define LAYOUT_FRAMESZ 1536
+#define LAYOUT_FRAMECNT 32
+#define LAYOUT_NB_FRAMES (3 * LAYOUT_FRAMECNT)
+#define LAYOUT_FRAME_LEN 1400
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -382,6 +394,254 @@ wait_for_stats_update(uint16_t pid, uint64_t expected_opackets,
 	return -1;
 }
 
+/*
+ * Helper: Create, configure and start an extra af_packet port on the TAP.
+ */
+static void
+destroy_test_port(const char *name, uint16_t pid)
+{
+	rte_eth_dev_stop(pid);
+	rte_eth_dev_close(pid);
+	rte_vdev_uninit(name);
+}
+
+static int
+create_test_port(const char *name, const char *args, uint64_t tx_offloads,
+		 uint16_t *pid)
+{
+	struct rte_eth_conf conf = { .txmode.offloads = tx_offloads };
+	int ret;
+
+	ret = create_af_packet_port(name, args, pid);
+	if (ret != 0)
+		return ret;
+
+	ret = rte_eth_dev_configure(*pid, 1, 1, &conf);
+	if (ret == 0)
+		ret = rte_eth_rx_queue_setup(*pid, 0, RING_SIZE,
+					     rte_eth_dev_socket_id(*pid), NULL, mp);
+	if (ret == 0)
+		ret = rte_eth_tx_queue_setup(*pid, 0, RING_SIZE,
+					     rte_eth_dev_socket_id(*pid), NULL);
+	if (ret == 0)
+		ret = rte_eth_dev_start(*pid);
+	if (ret != 0) {
+		printf("Failed to set up port '%s': %d\n", name, ret);
+		destroy_test_port(name, *pid);
+	}
+	return ret;
+}
+
+/*
+ * Helper: Build a TEST_ETHER_TYPE frame of len bytes carrying seq and a
+ * payload pattern derived from it.
+ */
+static void
+fill_seq_frame(uint8_t *frame, size_t len, const struct rte_ether_addr *src,
+	       uint32_t seq)
+{
+	struct rte_ether_hdr *eth = (struct rte_ether_hdr *)frame;
+	size_t i;
+
+	memset(&eth->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN);
+	rte_ether_addr_copy(src, &eth->src_addr);
+	eth->ether_type = rte_cpu_to_be_16(TEST_ETHER_TYPE);
+	memcpy(frame + sizeof(*eth), &seq, sizeof(seq));
+	for (i = sizeof(*eth) + sizeof(seq); i < len; i++)
+		frame[i] = (uint8_t)(seq * 31 + i);
+}
+
+/*
+ * Helper: Check a frame built by fill_seq_frame().
+ * Returns its seq, -1 if it is some other frame, -2 if it is corrupt.
+ */
+static int64_t
+check_seq_frame(const uint8_t *frame, size_t len,
+		const struct rte_ether_addr *src, size_t expect_len)
+{
+	const struct rte_ether_hdr *eth = (const struct rte_ether_hdr *)frame;
+	uint32_t seq;
+	size_t i;
+
+	if (len < sizeof(*eth) + sizeof(seq) ||
+	    eth->ether_type != rte_cpu_to_be_16(TEST_ETHER_TYPE) ||
+	    !rte_is_same_ether_addr(&eth->src_addr, src))
+		return -1;
+
+	memcpy(&seq, frame + sizeof(*eth), sizeof(seq));
+	if (len != expect_len)
+		return -2;
+	for (i = sizeof(*eth) + sizeof(seq); i < len; i++)
+		if (frame[i] != (uint8_t)(seq * 31 + i))
+			return -2;
+	return seq;
+}
+
+/*
+ * Helper: Record a checked frame in seen[]. Returns false if it was corrupt.
+ */
+static bool
+record_seq(int64_t seq, bool *seen, unsigned int n, unsigned int *nb_seen)
+{
+	if (seq == -1)
+		return true;
+	if (seq < 0 || seq >= n) {
+		printf("Corrupt frame (seq %" PRId64 ")\n", seq);
+		return false;
+	}
+	if (!seen[seq]) {
+		seen[seq] = true;
+		(*nb_seen)++;
+	}
+	return true;
+}
+
+/* Helper: Discard frames queued on the (non-blocking) TAP fd. */
+static void
+tap_drain(void)
+{
+	uint8_t buf[RTE_MBUF_DEFAULT_BUF_SIZE];
+
+	while (read(tap_fd, buf, sizeof(buf)) > 0)
+		;
+}
+
+/*
+ * Helper: Read frames from the (non-blocking) TAP fd until want frames
+ * built by fill_seq_frame() have been seen, or timeout.
+ * Returns the number seen, -1 on a corrupt frame.
+ */
+static int
+tap_collect_seq(bool *seen, unsigned int n, unsigned int *nb_seen,
+		unsigned int want, size_t frame_len, uint64_t timeout_us)
+{
+	uint8_t buf[RTE_MBUF_DEFAULT_BUF_SIZE];
+	uint64_t elapsed = 0;
+	ssize_t len;
+
+	while (*nb_seen < want && elapsed < timeout_us) {
+		len = read(tap_fd, buf, sizeof(buf));
+		if (len > 0) {
+			if (!record_seq(check_seq_frame(buf, len, &DEV_MAC_TAG,
+							frame_len),
+					seen, n, nb_seen))
+				return -1;
+			continue;
+		}
+		rte_delay_us_block(STATS_POLL_INTERVAL_US);
+		elapsed += STATS_POLL_INTERVAL_US;
+	}
+	return *nb_seen;
+}
+
+/*
+ * Helper: Transmit n frames of frame_len bytes in small bursts and verify
+ * each one arrives intact on the TAP fd.
+ */
+static int
+check_tx_seq(uint16_t pid, unsigned int n, size_t frame_len)
+{
+	struct rte_mbuf *bufs[SEQ_TX_BURST];
+	unsigned int seq, i, burst, nb_seen = 0;
+	bool *seen;
+	int ret = TEST_FAILED;
+
+	seen = calloc(n, sizeof(*seen));
+	if (seen == NULL)
+		return TEST_FAILED;
+
+	for (seq = 0; seq < n; seq += burst) {
+		burst = RTE_MIN(n - seq, (unsigned int)SEQ_TX_BURST);
+		if (rte_pktmbuf_alloc_bulk(mp, bufs, burst) != 0) {
+			printf("Could not allocate mbufs\n");
+			goto out;
+		}
+		for (i = 0; i < burst; i++) {
+			uint8_t *data = (uint8_t *)rte_pktmbuf_append(bufs[i], frame_len);
+
+			if (data == NULL) {
+				rte_pktmbuf_free_bulk(bufs, burst);
+				goto out;
+			}
+			fill_seq_frame(data, frame_len, &DEV_MAC_TAG, seq + i);
+		}
+
+		if (do_tx_burst(pid, 0, bufs, burst) != burst) {
+			printf("TX burst at seq %u was short\n", seq);
+			goto out;
+		}
+		if (tap_collect_seq(seen, n, &nb_seen, seq + burst, frame_len,
+				    TAP_READ_TIMEOUT_US) != (int)(seq + burst)) {
+			printf("TX: %u of %u frames arrived intact\n", nb_seen, seq + burst);
+			goto out;
+		}
+	}
+	ret = TEST_SUCCESS;
+out:
+	free(seen);
+	return ret;
+}
+
+/*
+ * Helper: Write n frames of frame_len bytes to the TAP fd and verify each one
+ * is received intact on pid.
+ */
+static int
+check_rx_seq(uint16_t pid, unsigned int n, size_t frame_len)
+{
+	struct rte_mbuf *bufs[BURST_SIZE];
+	uint8_t frame[RTE_MBUF_DEFAULT_BUF_SIZE];
+	unsigned int seq, nb_seen = 0;
+	uint64_t elapsed;
+	uint16_t i, nb_rx;
+	bool *seen, ok = true;
+	int ret = TEST_FAILED;
+
+	if (frame_len > sizeof(frame))
+		return TEST_FAILED;
+	seen = calloc(n, sizeof(*seen));
+	if (seen == NULL)
+		return TEST_FAILED;
+
+	for (seq = 0; seq < n; seq++) {
+		fill_seq_frame(frame, frame_len, &FD_MAC_TAG, seq);
+		if (write(tap_fd, frame, frame_len) != (ssize_t)frame_len) {
+			printf("TAP write failed: %s\n", strerror(errno));
+			goto out;
+		}
+		if ((seq + 1) % SEQ_TX_BURST != 0 && seq + 1 != n)
+			continue;
+
+		for (elapsed = 0; ok && nb_seen < seq + 1 &&
+		     elapsed < TAP_READ_TIMEOUT_US;
+		     elapsed += STATS_POLL_INTERVAL_US) {
+			nb_rx = rte_eth_rx_burst(pid, 0, bufs, BURST_SIZE);
+			for (i = 0; i < nb_rx; i++) {
+				const uint8_t *data;
+
+				data = rte_pktmbuf_read(bufs[i], 0,
+					rte_pktmbuf_pkt_len(bufs[i]), frame);
+				ok = ok && data != NULL &&
+					record_seq(check_seq_frame(data,
+						rte_pktmbuf_pkt_len(bufs[i]),
+						&FD_MAC_TAG, frame_len),
+						seen, n, &nb_seen);
+			}
+			rte_pktmbuf_free_bulk(bufs, nb_rx);
+			if (nb_rx == 0)
+				rte_delay_us_block(STATS_POLL_INTERVAL_US);
+		}
+		if (!ok || nb_seen < seq + 1) {
+			printf("RX: %u of %u frames arrived intact\n", nb_seen, seq + 1);
+			goto out;
+		}
+	}
+	ret = TEST_SUCCESS;
+out:
+	free(seen);
+	return ret;
+}
+
 static int
 test_af_packet_setup(void)
 {
@@ -1382,6 +1642,112 @@ test_af_packet_ignore_outgoing_multi_queue(void)
 	return test_af_packet_ignore_outgoing(2, false);
 }
 
+/*
+ * Test: Frame addresses follow the kernel's block layout
+ *
+ * framesz=1536 in 4096-byte blocks gives 2 frames per block with 1024 bytes
+ * unused, so sequential addressing (i * framesz) diverges from the kernel's
+ * layout from the third frame on. Each frame must cross the ring intact.
+ */
+static int
+test_af_packet_frame_layout(void)
+{
+	const char *name = "net_af_packet_layout";
+	char devargs[256];
+	uint16_t pid;
+	int flags, ret;
+
+	if (!tap_created) {
+		printf("SKIPPED: TAP interface not available (need root)\n");
+		return TEST_SKIPPED;
+	}
+
+	snprintf(devargs, sizeof(devargs), "iface=%s,blocksz=4096,framesz=%d,framecnt=%d",
+		 TAP_DEV_NAME, LAYOUT_FRAMESZ, LAYOUT_FRAMECNT);
+	ret = create_test_port(name, devargs, 0, &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 = check_tx_seq(pid, LAYOUT_NB_FRAMES, LAYOUT_FRAME_LEN);
+	if (ret == TEST_SUCCESS)
+		ret = check_rx_seq(pid, LAYOUT_NB_FRAMES, LAYOUT_FRAME_LEN);
+
+	fcntl(tap_fd, F_SETFL, flags);
+	destroy_test_port(name, pid);
+	return ret;
+}
+
+/*
+ * Test: The largest frame that fits a ring slot is sent intact; one byte
+ * more is dropped and counted as an error.
+ */
+static int
+test_af_packet_frame_max_size(void)
+{
+	const char *name = "net_af_packet_maxsize";
+	struct rte_eth_dev_info dev_info;
+	struct rte_eth_stats stats;
+	struct rte_mbuf *m;
+	char devargs[256];
+	uint8_t *data;
+	uint32_t max_len;
+	uint16_t pid;
+	int flags, ret;
+
+	if (!tap_created) {
+		printf("SKIPPED: TAP interface not available (need root)\n");
+		return TEST_SKIPPED;
+	}
+
+	/* Keep the slot below the TAP MTU so the kernel accepts the largest frame */
+	snprintf(devargs, sizeof(devargs), "iface=%s,blocksz=4096,framesz=%d,framecnt=%d",
+		 TAP_DEV_NAME, LAYOUT_FRAMESZ, LAYOUT_FRAMECNT);
+	ret = create_test_port(name, devargs, 0, &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)
+		goto out;
+	max_len = dev_info.max_rx_pktlen;
+	printf("framesz=%d max frame length=%u\n", LAYOUT_FRAMESZ, max_len);
+
+	if (check_tx_seq(pid, 1, max_len) != TEST_SUCCESS)
+		goto out;
+
+	rte_eth_stats_reset(pid);
+	m = rte_pktmbuf_alloc(mp);
+	if (m == NULL)
+		goto out;
+	data = (uint8_t *)rte_pktmbuf_append(m, max_len + 1);
+	if (data == NULL) {
+		rte_pktmbuf_free(m);
+		goto out;
+	}
+	fill_seq_frame(data, max_len + 1, &DEV_MAC_TAG, 1);
+	if (do_tx_burst(pid, 0, &m, 1) != 1) {
+		printf("Oversized frame was not consumed\n");
+		goto out;
+	}
+	if (rte_eth_stats_get(pid, &stats) != 0 || stats.oerrors != 1 ||
+	    stats.opackets != 0) {
+		printf("Oversized frame: opackets=%" PRIu64 " oerrors=%" PRIu64 "\n",
+		       stats.opackets, stats.oerrors);
+		goto out;
+	}
+	ret = TEST_SUCCESS;
+out:
+	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,
@@ -1405,6 +1771,8 @@ static struct unit_test_suite af_packet_test_suite = {
 		TEST_CASE(test_af_packet_invalid_bad_iface),
 		TEST_CASE(test_af_packet_invalid_qpairs),
 		TEST_CASE(test_af_packet_frame_config),
+		TEST_CASE(test_af_packet_frame_layout),
+		TEST_CASE(test_af_packet_frame_max_size),
 		TEST_CASE(test_af_packet_qdisc_bypass),
 		TEST_CASE(test_af_packet_multi_queue),
 		TEST_CASE(test_af_packet_fanout_modes),
diff --git a/drivers/net/af_packet/rte_eth_af_packet.c b/drivers/net/af_packet/rte_eth_af_packet.c
index fbaea57615..22788eaf1c 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -9,6 +9,8 @@
 #include <rte_common.h>
 #include <rte_string_fns.h>
 #include <rte_mbuf.h>
+#include <rte_atomic.h>
+#include <rte_bitops.h>
 #include <ethdev_driver.h>
 #include <ethdev_vdev.h>
 #include <rte_malloc.h>
@@ -54,6 +56,8 @@
 #define DFLT_FRAME_SIZE		(1 << 11)
 #define DFLT_FRAME_COUNT	(1 << 9)
 
+#define ETH_AF_PACKET_FRAME_OVERHEAD (TPACKET2_HDRLEN - sizeof(struct sockaddr_ll))
+
 static uint64_t timestamp_dynflag;
 static int timestamp_dynfield_offset = -1;
 
@@ -174,6 +178,30 @@ eth_af_packet_rx_scatter(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
 	return 0;
 }
 
+/**
+ * Read tp_status from the packet mmap ring with acquire ordering, so frame
+ * reads that follow cannot see data older than the status.
+ */
+static inline uint32_t
+tpacket_read_status(const volatile void *tp_status)
+{
+	uint32_t status = *((const volatile unaligned_uint32_t *)tp_status);
+
+	rte_atomic_thread_fence(rte_memory_order_acquire);
+	return status;
+}
+
+/**
+ * Write tp_status to the packet mmap ring with release ordering, so frame
+ * accesses before it complete before the kernel can see the new status.
+ */
+static inline void
+tpacket_write_status(volatile void *tp_status, uint32_t status)
+{
+	rte_atomic_thread_fence(rte_memory_order_release);
+	*((volatile unaligned_uint32_t *)tp_status) = status;
+}
+
 static uint16_t
 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 {
@@ -184,7 +212,8 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	uint8_t *pbuf;
 	struct pkt_rx_queue *pkt_q = queue;
 	uint16_t num_rx = 0;
-	unsigned long num_rx_bytes = 0;
+	uint32_t num_rx_bytes = 0;
+	uint32_t tp_status;
 	unsigned int framecount, framenum;
 	uint32_t pkt_len;
 
@@ -200,7 +229,8 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	for (i = 0; i < nb_pkts; i++) {
 		/* point at the next incoming frame */
 		ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
-		if ((ppd->tp_status & TP_STATUS_USER) == 0)
+		tp_status = tpacket_read_status(&ppd->tp_status);
+		if ((tp_status & TP_STATUS_USER) == 0)
 			break;
 
 		/* skip outgoing frames if ignore_outgoing is set */
@@ -243,7 +273,7 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 		rte_pktmbuf_pkt_len(mbuf) = pkt_len;
 
 		/* check for vlan info */
-		if (ppd->tp_status & TP_STATUS_VLAN_VALID) {
+		if (tp_status & TP_STATUS_VLAN_VALID) {
 			uint16_t tpid;
 
 			mbuf->vlan_tci = ppd->tp_vlan_tci;
@@ -252,7 +282,7 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 			if (pkt_q->vlan_strip) {
 				mbuf->ol_flags |= RTE_MBUF_F_RX_VLAN_STRIPPED;
 			} else {
-				tpid = (ppd->tp_status & TP_STATUS_VLAN_TPID_VALID) ?
+				tpid = (tp_status & TP_STATUS_VLAN_TPID_VALID) ?
 					ppd->tp_vlan_tpid : RTE_ETHER_TYPE_VLAN;
 				if (rte_vlan_insert_tpid(&mbuf, tpid) != 0) {
 					PMD_LOG(ERR, "Failed to reinsert VLAN tag");
@@ -280,7 +310,7 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 
 release_frame:
 		/* release incoming frame and advance ring buffer */
-		ppd->tp_status = TP_STATUS_KERNEL;
+		tpacket_write_status(&ppd->tp_status, TP_STATUS_KERNEL);
 		if (++framenum >= framecount)
 			framenum = 0;
 	}
@@ -323,8 +353,8 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	struct pollfd pfd;
 	struct pkt_tx_queue *pkt_q = queue;
 	uint16_t num_tx = 0;
-	unsigned long num_tx_bytes = 0;
-	int i;
+	uint32_t num_tx_bytes = 0;
+	uint16_t i;
 
 	if (unlikely(nb_pkts == 0))
 		return 0;
@@ -354,16 +384,6 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 			continue;
 		}
 
-		/* point at the next incoming frame */
-		if (!tx_ring_status_available(ppd->tp_status)) {
-			if (poll(&pfd, 1, -1) < 0)
-				break;
-
-			/* poll() can return POLLERR if the interface is down */
-			if (pfd.revents & POLLERR)
-				break;
-		}
-
 		/*
 		 * poll() will almost always return POLLOUT, even if there
 		 * are no extra buffers available
@@ -378,26 +398,28 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 		 *
 		 * This results in poll() returning POLLOUT.
 		 */
-		if (!tx_ring_status_available(ppd->tp_status))
+		if (unlikely(!tx_ring_status_available(tpacket_read_status(&ppd->tp_status)) &&
+			(poll(&pfd, 1, -1) < 0 || (pfd.revents & POLLERR) != 0 ||
+			 !tx_ring_status_available(tpacket_read_status(&ppd->tp_status))))) {
+			/* Ring is full, stop here. Don't process bufs[i]. */
 			break;
+		}
 
-		/* copy the tx frame data */
-		pbuf = (uint8_t *) ppd + TPACKET2_HDRLEN -
-			sizeof(struct sockaddr_ll);
+		pbuf = (uint8_t *)ppd + ETH_AF_PACKET_FRAME_OVERHEAD;
 
 		struct rte_mbuf *tmp_mbuf = mbuf;
-		while (tmp_mbuf) {
+		do {
 			uint16_t data_len = rte_pktmbuf_data_len(tmp_mbuf);
 			memcpy(pbuf, rte_pktmbuf_mtod(tmp_mbuf, void*), data_len);
 			pbuf += data_len;
 			tmp_mbuf = tmp_mbuf->next;
-		}
+		} while (tmp_mbuf);
 
 		ppd->tp_len = mbuf->pkt_len;
 		ppd->tp_snaplen = mbuf->pkt_len;
 
 		/* release incoming frame and advance ring buffer */
-		ppd->tp_status = TP_STATUS_SEND_REQUEST;
+		tpacket_write_status(&ppd->tp_status, TP_STATUS_SEND_REQUEST);
 		if (++framenum >= framecount)
 			framenum = 0;
 		ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
@@ -674,8 +696,7 @@ eth_rx_queue_setup(struct rte_eth_dev *dev,
 
 	buf_size = rte_pktmbuf_data_room_size(pkt_q->mb_pool) -
 		RTE_PKTMBUF_HEADROOM;
-	data_size = internals->req.tp_frame_size;
-	data_size -= TPACKET2_HDRLEN - sizeof(struct sockaddr_ll);
+	data_size = internals->req.tp_frame_size - ETH_AF_PACKET_FRAME_OVERHEAD;
 
 	scatter_enabled = !!(dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_SCATTER);
 
@@ -717,7 +738,7 @@ eth_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
 	int ret;
 	int s;
 	unsigned int data_size = internals->req.tp_frame_size -
-				 TPACKET2_HDRLEN + sizeof(struct sockaddr_ll);
+				 ETH_AF_PACKET_FRAME_OVERHEAD;
 
 	/* ethdev layer validates mtu <= max_mtu before calling this */
 	RTE_ASSERT(mtu <= data_size);
@@ -1113,25 +1134,38 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
 		rx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
 		if (rx_queue->rd == NULL)
 			goto error;
+		/* Frame addresses must match kernel's packet_lookup_frame():
+		 * block_idx = position / frames_per_block
+		 * frame_offset = position % frames_per_block
+		 * address = block_start + (frame_offset * frame_size)
+		 */
+		const uint32_t frames_per_block = req->tp_block_size / req->tp_frame_size;
 		for (i = 0; i < req->tp_frame_nr; ++i) {
-			rx_queue->rd[i].iov_base = rx_queue->map + (i * framesize);
+			const uint32_t block_idx = i / frames_per_block;
+			const uint32_t frame_in_block = i % frames_per_block;
+			rx_queue->rd[i].iov_base = rx_queue->map +
+				(block_idx * req->tp_block_size) +
+				(frame_in_block * req->tp_frame_size);
 			rx_queue->rd[i].iov_len = req->tp_frame_size;
 		}
 		rx_queue->sockfd = qsockfd;
 
 		tx_queue = &((*internals)->tx_queue[q]);
 		tx_queue->framecount = req->tp_frame_nr;
-		tx_queue->frame_data_size = req->tp_frame_size;
-		tx_queue->frame_data_size -= TPACKET2_HDRLEN -
-			sizeof(struct sockaddr_ll);
+		tx_queue->frame_data_size = req->tp_frame_size - ETH_AF_PACKET_FRAME_OVERHEAD;
 
 		tx_queue->map = rx_queue->map + req->tp_block_size * req->tp_block_nr;
 
 		tx_queue->rd = rte_zmalloc_socket(name, rdsize, 0, numa_node);
 		if (tx_queue->rd == NULL)
 			goto error;
+		/* See comment above rx_queue->rd initialization. */
 		for (i = 0; i < req->tp_frame_nr; ++i) {
-			tx_queue->rd[i].iov_base = tx_queue->map + (i * framesize);
+			const uint32_t block_idx = i / frames_per_block;
+			const uint32_t frame_in_block = i % frames_per_block;
+			tx_queue->rd[i].iov_base = tx_queue->map +
+				(block_idx * req->tp_block_size) +
+				(frame_in_block * req->tp_frame_size);
 			tx_queue->rd[i].iov_len = req->tp_frame_size;
 		}
 		tx_queue->sockfd = qsockfd;
@@ -1253,7 +1287,8 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
 	struct rte_kvargs_pair *pair = NULL;
 	unsigned k_idx;
 	unsigned int blockcount;
-	unsigned int blocksize;
+	const int pagesize = getpagesize();
+	unsigned int blocksize = pagesize;
 	unsigned int framesize = DFLT_FRAME_SIZE;
 	unsigned int framecount = DFLT_FRAME_COUNT;
 	unsigned int qpairs = 1;
@@ -1265,8 +1300,6 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
 	if (*sockfd < 0)
 		return -1;
 
-	blocksize = getpagesize();
-
 	/*
 	 * Walk arguments for configurable settings
 	 */
@@ -1340,13 +1373,55 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
 		return -1;
 	}
 
-	blockcount = framecount / (blocksize / framesize);
+	const unsigned int frames_per_block = blocksize / framesize;
+	blockcount = framecount / frames_per_block;
 	if (!blockcount) {
 		PMD_LOG(ERR,
 			"%s: invalid AF_PACKET MMAP parameters", name);
 		return -1;
 	}
 
+	/*
+	 * https://www.kernel.org/doc/Documentation/networking/packet_mmap.txt
+	 * Check constraints that may be enforced by the kernel and cause failure
+	 * to initialize the rings but explicit error messages aren't provided.
+	 * See packet_set_ring in linux kernel for enforcement:
+	 * https://github.com/torvalds/linux/blob/master/net/packet/af_packet.c
+	 */
+	if (blocksize % pagesize != 0) {
+		/* tp_block_size must be a multiple of PAGE_SIZE */
+		PMD_LOG(WARNING, "%s: %s=%u must be a multiple of PAGE_SIZE=%d",
+			name, ETH_AF_PACKET_BLOCKSIZE_ARG, blocksize, pagesize);
+	}
+	if (framesize % TPACKET_ALIGNMENT != 0) {
+		/* tp_frame_size must be a multiple of TPACKET_ALIGNMENT */
+		PMD_LOG(WARNING, "%s: %s=%u must be a multiple of TPACKET_ALIGNMENT=%d",
+			name, ETH_AF_PACKET_FRAMESIZE_ARG, framesize, TPACKET_ALIGNMENT);
+	}
+	if (frames_per_block == 0 || frames_per_block > UINT_MAX / blockcount ||
+	    framecount != frames_per_block * blockcount) {
+		/* tp_frame_nr must be exactly frames_per_block*tp_block_nr */
+		PMD_LOG(WARNING, "%s: %s=%u must be exactly "
+			"frames_per_block(%s/%s = %u/%u = %u) * blockcount(%u)",
+			name, ETH_AF_PACKET_FRAMECOUNT_ARG, framecount,
+			ETH_AF_PACKET_BLOCKSIZE_ARG, ETH_AF_PACKET_FRAMESIZE_ARG,
+			blocksize, framesize, frames_per_block, blockcount);
+	}
+
+	/* Below conditions may not cause errors but provide hints to improve */
+	if (blocksize % framesize != 0) {
+		PMD_LOG(WARNING, "%s: %s=%u not evenly divisible by %s=%u, "
+			"may waste memory", name,
+			ETH_AF_PACKET_BLOCKSIZE_ARG, blocksize,
+			ETH_AF_PACKET_FRAMESIZE_ARG, framesize);
+	}
+	if (!rte_is_power_of_2(blocksize)) {
+		/* tp_block_size should be a power of two or there will be waste */
+		PMD_LOG(WARNING, "%s: %s=%u should be a power of two "
+			"or there will be a waste of memory",
+			name, ETH_AF_PACKET_BLOCKSIZE_ARG, blocksize);
+	}
+
 	PMD_LOG(DEBUG, "%s: AF_PACKET MMAP parameters:", name);
 	PMD_LOG(DEBUG, "%s:\tblock size %d", name, blocksize);
 	PMD_LOG(DEBUG, "%s:\tblock count %d", name, blockcount);
-- 
2.54.0 (Apple Git-157)


  reply	other threads:[~2026-12-01 22:25 UTC|newest]

Thread overview: 73+ 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             ` Scott Mitchell [this message]
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

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=20261201222405.88922-2-scott.k.mitch1@gmail.com \
    --to=scott.k.mitch1@gmail.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=linville@tuxdriver.com \
    --cc=stable@dpdk.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox