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>
Subject: [PATCH v7 2/4] net/af_packet: RX/TX bulk free, unlikely hint
Date: Tue,  1 Dec 2026 14:24:03 -0800	[thread overview]
Message-ID: <20261201222405.88922-3-scott.k.mitch1@gmail.com> (raw)
In-Reply-To: <20261201222405.88922-1-scott.k.mitch1@gmail.com>

Free TX mbufs with rte_pktmbuf_free_bulk(), mark error paths unlikely,
and drop the redundant nb_pkts == 0 checks.

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

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index 3c96ee0045..93e10f4b77 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -1748,6 +1748,175 @@ test_af_packet_frame_max_size(void)
 	return ret;
 }
 
+/*
+ * Test: VLAN insertion happens before the size check. A frame that only
+ * exceeds the slot after its tag is inserted is dropped; one that fits is
+ * sent with the tag.
+ */
+static int
+test_af_packet_tx_vlan_oversize(void)
+{
+	const char *name = "net_af_packet_vlan";
+	struct rte_eth_dev_info dev_info;
+	struct rte_eth_stats stats;
+	struct rte_mbuf *m[2];
+	uint8_t buf[RTE_MBUF_DEFAULT_BUF_SIZE];
+	const uint16_t tci = 0x123;
+	const size_t fit_len = LAYOUT_FRAME_LEN;
+	char devargs[256];
+	uint64_t elapsed;
+	uint32_t max_len;
+	uint16_t pid;
+	ssize_t len;
+	int flags, ret;
+	unsigned int i;
+
+	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, RTE_ETH_TX_OFFLOAD_VLAN_INSERT, &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 ||
+	    rte_pktmbuf_alloc_bulk(mp, m, 2) != 0)
+		goto out;
+	max_len = dev_info.max_rx_pktlen;
+
+	/* m[0] fits only without the 4-byte tag; m[1] fits with it */
+	for (i = 0; i < 2; i++) {
+		size_t flen = i == 0 ? max_len - 2 : fit_len;
+		uint8_t *data = (uint8_t *)rte_pktmbuf_append(m[i], flen);
+
+		if (data == NULL) {
+			rte_pktmbuf_free_bulk(m, 2);
+			goto out;
+		}
+		fill_seq_frame(data, flen, &DEV_MAC_TAG, i);
+		m[i]->ol_flags |= RTE_MBUF_F_TX_VLAN;
+		m[i]->vlan_tci = tci;
+	}
+
+	rte_eth_stats_reset(pid);
+	if (do_tx_burst(pid, 0, m, 2) != 2) {
+		printf("TX burst was short\n");
+		goto out;
+	}
+	if (rte_eth_stats_get(pid, &stats) != 0 || stats.opackets != 1 ||
+	    stats.oerrors != 1) {
+		printf("opackets=%" PRIu64 " oerrors=%" PRIu64 ", expected 1/1\n",
+		       stats.opackets, stats.oerrors);
+		goto out;
+	}
+
+	/* The fitting frame arrives tagged: strip the tag and check the rest */
+	for (elapsed = 0; elapsed < TAP_READ_TIMEOUT_US;
+	     elapsed += STATS_POLL_INTERVAL_US) {
+		struct rte_vlan_hdr *vh;
+
+		len = read(tap_fd, buf, sizeof(buf));
+		if (len <= 0) {
+			rte_delay_us_block(STATS_POLL_INTERVAL_US);
+			continue;
+		}
+		if (len < (ssize_t)(sizeof(struct rte_ether_hdr) + sizeof(*vh)) ||
+		    ((struct rte_ether_hdr *)buf)->ether_type !=
+				rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
+			continue;
+
+		vh = (struct rte_vlan_hdr *)(buf + sizeof(struct rte_ether_hdr));
+		if (rte_be_to_cpu_16(vh->vlan_tci) != tci) {
+			printf("VLAN TCI 0x%x, expected 0x%x\n",
+			       rte_be_to_cpu_16(vh->vlan_tci), tci);
+			goto out;
+		}
+		memmove(buf + 2 * RTE_ETHER_ADDR_LEN,
+			buf + 2 * RTE_ETHER_ADDR_LEN + sizeof(*vh),
+			len - 2 * RTE_ETHER_ADDR_LEN - sizeof(*vh));
+		if (check_seq_frame(buf, len - sizeof(*vh), &DEV_MAC_TAG, fit_len) != 1) {
+			printf("Tagged frame is corrupt\n");
+			goto out;
+		}
+		ret = TEST_SUCCESS;
+		break;
+	}
+	if (ret != TEST_SUCCESS)
+		printf("Tagged frame did not arrive\n");
+out:
+	fcntl(tap_fd, F_SETFL, flags);
+	destroy_test_port(name, pid);
+	return ret;
+}
+
+/*
+ * Test: Every mbuf passed to TX is freed exactly once, whether it is sent
+ * or dropped.
+ */
+static int
+test_af_packet_tx_mbuf_accounting(void)
+{
+	const char *name = "net_af_packet_mbufs";
+	struct rte_eth_dev_info dev_info;
+	struct rte_mbuf *bufs[BURST_SIZE];
+	unsigned int in_use, i, n = 0;
+	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);
+
+	ret = TEST_FAILED;
+	if (rte_eth_dev_info_get(pid, &dev_info) != 0)
+		goto out;
+
+	in_use = rte_mempool_in_use_count(mp);
+	if (rte_pktmbuf_alloc_bulk(mp, bufs, 16) != 0)
+		goto out;
+	/* 12 frames that fit and 4 that are one byte too long */
+	for (i = 0; i < 16; i++) {
+		size_t flen = i < 12 ? TEST_FRAME_LEN : dev_info.max_rx_pktlen + 1;
+		uint8_t *data = (uint8_t *)rte_pktmbuf_append(bufs[i], flen);
+
+		if (data == NULL) {
+			rte_pktmbuf_free_bulk(bufs, 16);
+			goto out;
+		}
+		fill_seq_frame(data, flen, &DEV_MAC_TAG, i);
+	}
+	n = do_tx_burst(pid, 0, bufs, 16);
+	tap_drain();
+
+	if (n != 16 || rte_mempool_in_use_count(mp) != in_use) {
+		printf("TX consumed %u of 16, mbufs in use %u -> %u\n",
+		       n, in_use, rte_mempool_in_use_count(mp));
+		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,
@@ -1773,6 +1942,8 @@ static struct unit_test_suite af_packet_test_suite = {
 		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_tx_vlan_oversize),
+		TEST_CASE(test_af_packet_tx_mbuf_accounting),
 		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 22788eaf1c..72d0dbdd60 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -217,9 +217,6 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	unsigned int framecount, framenum;
 	uint32_t pkt_len;
 
-	if (unlikely(nb_pkts == 0))
-		return 0;
-
 	/*
 	 * Reads the given number of packets from the AF_PACKET socket one by
 	 * one and copies the packet data into a newly allocated mbuf.
@@ -356,9 +353,6 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	uint32_t num_tx_bytes = 0;
 	uint16_t i;
 
-	if (unlikely(nb_pkts == 0))
-		return 0;
-
 	memset(&pfd, 0, sizeof(pfd));
 	pfd.fd = pkt_q->sockfd;
 	pfd.events = POLLOUT;
@@ -366,24 +360,16 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 
 	framecount = pkt_q->framecount;
 	framenum = pkt_q->framenum;
-	ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
 	for (i = 0; i < nb_pkts; i++) {
-		mbuf = *bufs++;
-
-		/* insert vlan info if necessary */
-		if (mbuf->ol_flags & RTE_MBUF_F_TX_VLAN) {
-			if (rte_vlan_insert(&mbuf)) {
-				rte_pktmbuf_free(mbuf);
-				continue;
-			}
-		}
+		mbuf = bufs[i];
 
-		/* drop oversized packets */
-		if (mbuf->pkt_len > pkt_q->frame_data_size) {
-			rte_pktmbuf_free(mbuf);
+		/* Insert VLAN if necessary, then drop oversized packets */
+		if (unlikely(((mbuf->ol_flags & RTE_MBUF_F_TX_VLAN) != 0 &&
+			      rte_vlan_insert(&mbuf) != 0) ||
+			     mbuf->pkt_len > pkt_q->frame_data_size))
 			continue;
-		}
 
+		ppd = (struct tpacket2_hdr *)pkt_q->rd[framenum].iov_base;
 		/*
 		 * poll() will almost always return POLLOUT, even if there
 		 * are no extra buffers available
@@ -407,6 +393,9 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 
 		pbuf = (uint8_t *)ppd + ETH_AF_PACKET_FRAME_OVERHEAD;
 
+		ppd->tp_len = mbuf->pkt_len;
+		ppd->tp_snaplen = mbuf->pkt_len;
+
 		struct rte_mbuf *tmp_mbuf = mbuf;
 		do {
 			uint16_t data_len = rte_pktmbuf_data_len(tmp_mbuf);
@@ -415,23 +404,20 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 			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 */
 		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;
-
 		num_tx++;
 		num_tx_bytes += mbuf->pkt_len;
-		rte_pktmbuf_free(mbuf);
 	}
 
+	rte_pktmbuf_free_bulk(&bufs[0], i);
+
 	/* kick-off transmits */
-	if (sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1 &&
-			errno != ENOBUFS && errno != EAGAIN) {
+	if (unlikely(num_tx > 0 &&
+		     sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1 &&
+		     errno != ENOBUFS && errno != EAGAIN)) {
 		/*
 		 * In case of a ENOBUFS/EAGAIN error all of the enqueued
 		 * packets will be considered successful even though only some
-- 
2.54.0 (Apple Git-157)


  parent 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             ` [PATCH v7 1/4] net/af_packet: fix thread safety and frame calculations Scott Mitchell
2026-12-01 22:24             ` Scott Mitchell [this message]
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-3-scott.k.mitch1@gmail.com \
    --to=scott.k.mitch1@gmail.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@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