[PATCH v9 1/8] net/af_packet: fix frame status memory ordering

Scott Mitchell scott.k.mitch1 at gmail.com
Thu Dec 3 17:42:04 CET 2026


tp_status hands each ring frame between the kernel and the driver, but
it was read and written with plain accesses. On weakly ordered CPUs, RX
could read frame data older than the status it just saw, and TX could
publish SEND_REQUEST before the frame contents were visible to the
kernel.

Load tp_status with acquire and store it with release, as libpcap does
for the same ring.

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

Signed-off-by: Scott Mitchell <scott.k.mitch1 at gmail.com>
---
 drivers/net/af_packet/rte_eth_af_packet.c | 32 ++++++++++++++++++-----
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/drivers/net/af_packet/rte_eth_af_packet.c b/drivers/net/af_packet/rte_eth_af_packet.c
index fbaea57615..9b997478c6 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -9,6 +9,7 @@
 #include <rte_common.h>
 #include <rte_string_fns.h>
 #include <rte_mbuf.h>
+#include <rte_stdatomic.h>
 #include <ethdev_driver.h>
 #include <ethdev_vdev.h>
 #include <rte_malloc.h>
@@ -174,6 +175,21 @@ eth_af_packet_rx_scatter(struct rte_mempool *mb_pool, struct rte_mbuf *mbuf,
 	return 0;
 }
 
+/* tp_status hands a frame between the kernel and us; order frame accesses */
+static inline uint32_t
+tpacket_read_status(struct tpacket2_hdr *ppd)
+{
+	return rte_atomic_load_explicit((uint32_t __rte_atomic *)&ppd->tp_status,
+			rte_memory_order_acquire);
+}
+
+static inline void
+tpacket_write_status(struct tpacket2_hdr *ppd, uint32_t status)
+{
+	rte_atomic_store_explicit((uint32_t __rte_atomic *)&ppd->tp_status, status,
+			rte_memory_order_release);
+}
+
 static uint16_t
 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 {
@@ -187,6 +203,7 @@ eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 	unsigned long num_rx_bytes = 0;
 	unsigned int framecount, framenum;
 	uint32_t pkt_len;
+	uint32_t tp_status;
 
 	if (unlikely(nb_pkts == 0))
 		return 0;
@@ -200,7 +217,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);
+		if ((tp_status & TP_STATUS_USER) == 0)
 			break;
 
 		/* skip outgoing frames if ignore_outgoing is set */
@@ -243,7 +261,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 +270,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 +298,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_KERNEL);
 		if (++framenum >= framecount)
 			framenum = 0;
 	}
@@ -355,7 +373,7 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 		}
 
 		/* point at the next incoming frame */
-		if (!tx_ring_status_available(ppd->tp_status)) {
+		if (!tx_ring_status_available(tpacket_read_status(ppd))) {
 			if (poll(&pfd, 1, -1) < 0)
 				break;
 
@@ -378,7 +396,7 @@ 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 (!tx_ring_status_available(tpacket_read_status(ppd)))
 			break;
 
 		/* copy the tx frame data */
@@ -397,7 +415,7 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 		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_SEND_REQUEST);
 		if (++framenum >= framecount)
 			framenum = 0;
 		ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
-- 
2.54.0 (Apple Git-157)



More information about the stable mailing list