From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99F31606A320 for ; Tue, 1 Dec 2026 22:25:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=eNTDpp/8 Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 69EAF402E3; Tue, 1 Dec 2026 23:25:39 +0100 (CET) Received: from mail-dl1-f43.google.com (mail-dl1-f43.google.com [74.125.82.43]) by mails.dpdk.org (Postfix) with ESMTPS id 5207B4042C for ; Tue, 1 Dec 2026 23:25:37 +0100 (CET) Received: by mail-dl1-f43.google.com with SMTP id a92af1059eb24-1b5bbedcdbeso2190638c88.0 for ; Tue, 01 Dec 2026 14:25:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796163936; x=1796768736; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=GUJBsFsQ56x7aGK4oXK7ViKKy8tlINw0pkXm4WgH3mQ=; b=eNTDpp/8kBq+118dZOXuu+Ot0s++CHAr3ym/y0AqgvQaJaeV8KZICyEqEAvorNGyNm fbfoLRDRWclhaNQHRqDYtPgzBwTW3DiWHEKxZQunTlLSTq/q/oDsECxy34E5PhS435Wv U9oQ/67pbXIryRHJVN242/p3yTc4vVdH4puM0NVAQg+z9h0HWgF2/n4e4fvpHqK3RQM+ pxFsI6X2K7Ol9qkpwvK/G0fMLfOdc2CahLFCEBisziHuHPTFf2PaFiAGtDNg9UDhJVDW R0mQW0ZdQvzaNvVL8ntf4eQgiYc6pUJ4KkiDY8sDKeY7xc0yAPpilbDcTpb8yjfifJCH Q2fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796163936; x=1796768736; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=GUJBsFsQ56x7aGK4oXK7ViKKy8tlINw0pkXm4WgH3mQ=; b=Ypyz7a8QG58pnbv9FmlVkMdnJM4oFk7qbGg71nYiNntp2JLK+DnLVA6yDg8NadL1ba 0QxZ5Odx6p9UDq5n54L9bQ9/pbYoOreh2sZvs+gSFeSnL/SdLFxWbLmtOKd6hxThDDUp cmLdTyUEZzHCKTV3kZDZLH/lTyNc24xX94McHbxfJESqrGuMP6Q0kpqPERJ8GnRmp5in TJGvSz4dktNhX0V/ZgkdgsEHxa5tGd5IvT0y7oiSFpxhbyvTXb9FJ5TY8RdJ40MZFNrO H9Fmt7w0UZ/8R+5HGHg06NJ2R1YkJVIJ3e6IEFL6Ph39I/KDc5qM824i18Njp4OGuYZV LEKQ== X-Gm-Message-State: APuU4Z/eiGZ1Db+t/yG7dUPIky92iCvBgYnSEk03aQDqff5jXBp6AkAi BjDqw5rXyW8XlPQdJyAxvNwfz3TlzSJRWg0zt6U8HzrIL/xJHEV9E7J4HWyMyg== X-Gm-Gg: AZFdTZdEb6Rb/L0c3+mAgD8fB2ukNBGZE4ZfulAiGuq4a3ccd/JcPiN0FbC72cfAiM6 jVnX2k1XkbVBRyAACib/P690ZiIVm9NyU0CdhNVUvqPFIyO3dPTcf6ucw6izq6jfljUuuQoyQUc 7SWy1CVULFPWE1jlKRl4i8uz+8SJY/Kh/2vMULvbwbZQKZhx2FsyQlPX7IAtlSyPBgyikKHM1Cq 2EB1wKHcqk554LRGYYJ4fsbudJ41yvr1DlJNbz+mfhnr2M73rIIv6swyv457zVvQ88320n64t3N x+c7we9HH5EqAtPMvQLc7qP1OxaywG7hqdkKkEVKzXFfGAndvOwaBDARzG9aThCkbLPeoQFLHeP iTxIphPmXSIIoHkIgI3w3zRK8U3zjBID7DtwrJz9xI5xprSENAKh4wJhUe1G042GjDsFnIje7kc Pt98ybftcING5IlloHJlZVZ29lKxqzdgk4+UTT6Ohop3WXzPhqwRZ1rO7MM2fJ4xQuWkkyAK5PG t0blNIrRLxB2EJKZDjDVnvFjv+Y8DMAFF38vHnRJfU2p9JnBvlxmsSqGLVcksGgA4xcMmy6vEKL eTGNPDxcmSSE56snwXDaEhFu+AEPL9N/xdvdOfG8m51P1VPKJEJ5Rosv1Vpi9aGJOgHstiSUGA= = X-Received: by 2002:a05:701b:230b:b0:1b2:4da6:bccd with SMTP id a92af1059eb24-1b5568feaadmr31624253c88.34.1796163935404; Tue, 01 Dec 2026 14:25:35 -0800 (PST) Received: from mr55p01nt-relayp02.apple.com ([17.199.97.123]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38566cd40c5sm138009198eec.10.2026.12.01.14.25.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 01 Dec 2026 14:25:35 -0800 (PST) From: Scott Mitchell To: dev@dpdk.org Cc: Stephen Hemminger , Thomas Monjalon , David Marchand , Scott Mitchell , 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 Message-ID: <20261201222405.88922-2-scott.k.mitch1@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261201222405.88922-1-scott.k.mitch1@gmail.com> References: <20260206011141.2377-1-scott.k.mitch1@gmail.com> <20261201222405.88922-1-scott.k.mitch1@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-20.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- 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 #include #include +#include #include #include #include @@ -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(ð->dst_addr, 0xFF, RTE_ETHER_ADDR_LEN); + rte_ether_addr_copy(src, ð->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(ð->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 #include #include +#include +#include #include #include #include @@ -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)