When the TX ring was full, tx_burst called poll() with no timeout and
could block the lcore indefinitely. Return the number of packets queued
instead, as other ethdev drivers do, and let the caller retry the rest.

The kick on every non-empty burst keeps the kernel processing frames
left in SEND_REQUEST, so a full ring drains on later bursts once the
kernel can send again.

Signed-off-by: Scott Mitchell <[email protected]>
---
 app/test/test_pmd_af_packet.c             | 108 ++++++++++++++++++++++
 drivers/net/af_packet/rte_eth_af_packet.c |  32 +------
 2 files changed, 109 insertions(+), 31 deletions(-)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index 3afee26b90..defbca3313 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -91,6 +91,10 @@ struct tag_count {
 /* Largest packet a LAYOUT_FRAMESZ frame holds on Tx */
 #define LAYOUT_TX_MAX_LEN (LAYOUT_FRAMESZ - (TPACKET2_HDRLEN - sizeof(struct 
sockaddr_ll)))
 
+/* Tx ring size for the ring full test: at least this, and at least one page */
+#define TX_FULL_MIN_FRAMECNT 16u
+#define TX_FULL_MAX_FRAMECNT 64u
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -1968,6 +1972,109 @@ test_af_packet_tx_mbuf_accounting(void)
        return ret;
 }
 
+/* Helper: Set or clear IFF_UP on the TAP interface. */
+static int
+set_tap_link(bool up)
+{
+       struct ifreq ifr;
+       int sock, ret = -1;
+
+       sock = socket(AF_INET, SOCK_DGRAM, 0);
+       if (sock < 0)
+               return -1;
+       memset(&ifr, 0, sizeof(ifr));
+       snprintf(ifr.ifr_name, IFNAMSIZ, "%s", TAP_DEV_NAME);
+       if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
+               if (up)
+                       ifr.ifr_flags |= IFF_UP;
+               else
+                       ifr.ifr_flags &= ~IFF_UP;
+               ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
+       }
+       close(sock);
+       return ret;
+}
+
+/*
+ * Test: TX never blocks. With the link down the kernel leaves frames in
+ * SEND_REQUEST, so the ring fills and TX returns short. Once the link is
+ * back, bursts into the full ring must still kick the kernel to drain it.
+ */
+static int
+test_af_packet_tx_ring_full(void)
+{
+       const char *name = "net_af_packet_txfull";
+       const unsigned int pagesize = getpagesize();
+       const unsigned int framecnt = RTE_MAX(TX_FULL_MIN_FRAMECNT, pagesize / 
2048);
+       const unsigned int n = 2 * framecnt;
+       struct rte_mbuf *bufs[2 * TX_FULL_MAX_FRAMECNT];
+       char devargs[256];
+       uint64_t start, elapsed_ms, elapsed;
+       uint16_t pid, nb_tx, nb_tx2, nb_tx3 = 0;
+       int flags, ret;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+       if (framecnt > TX_FULL_MAX_FRAMECNT) {
+               printf("SKIPPED: page size %u too large\n", pagesize);
+               return TEST_SKIPPED;
+       }
+
+       snprintf(devargs, sizeof(devargs), 
"iface=%s,blocksz=%u,framesz=2048,framecnt=%u",
+                TAP_DEV_NAME, pagesize, 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 (set_tap_link(false) != 0) {
+               printf("Could not take %s down\n", TAP_DEV_NAME);
+               goto out;
+       }
+
+       start = rte_get_timer_cycles();
+       if (alloc_tx_mbufs(bufs, n) == 0)
+               goto out;
+       nb_tx = do_tx_burst(pid, 0, bufs, n);
+       if (alloc_tx_mbufs(bufs, n) == 0)
+               goto out;
+       nb_tx2 = do_tx_burst(pid, 0, bufs, n);
+       elapsed_ms = (rte_get_timer_cycles() - start) * MS_PER_S / 
rte_get_timer_hz();
+
+       printf("Link down: TX %u then %u of %u in %" PRIu64 " ms\n",
+              nb_tx, nb_tx2, n, elapsed_ms);
+       if (nb_tx > framecnt || nb_tx2 != 0 || elapsed_ms > 1000)
+               goto out;
+
+       if (set_tap_link(true) != 0) {
+               printf("Could not bring %s up\n", TAP_DEV_NAME);
+               goto out;
+       }
+       for (elapsed = 0; nb_tx3 == 0 && elapsed < TAP_READ_TIMEOUT_US;
+            elapsed += STATS_POLL_INTERVAL_US) {
+               if (alloc_tx_mbufs(bufs, 1) == 0)
+                       goto out;
+               nb_tx3 = do_tx_burst(pid, 0, bufs, 1);
+               tap_drain();
+               if (nb_tx3 == 0)
+                       rte_delay_us_block(STATS_POLL_INTERVAL_US);
+       }
+       printf("Link up: TX %u after the ring was full\n", nb_tx3);
+       if (nb_tx3 == 0)
+               goto out;
+       ret = TEST_SUCCESS;
+out:
+       set_tap_link(true);
+       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,
@@ -1996,6 +2103,7 @@ static struct unit_test_suite af_packet_test_suite = {
                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_tx_ring_full),
                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 a583f24785..89864751a0 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -31,7 +31,6 @@
 #include <string.h>
 #include <sys/mman.h>
 #include <unistd.h>
-#include <poll.h>
 
 /* Added in Linux 4.20. Older kernels reject it with ENOPROTOOPT. */
 #ifndef PACKET_IGNORE_OUTGOING
@@ -342,7 +341,6 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
        struct rte_mbuf *mbuf;
        uint8_t *pbuf;
        unsigned int framecount, framenum;
-       struct pollfd pfd;
        struct pkt_tx_queue *pkt_q = queue;
        uint16_t num_tx = 0;
        unsigned long num_tx_bytes = 0;
@@ -351,11 +349,6 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
        if (unlikely(nb_pkts == 0))
                return 0;
 
-       memset(&pfd, 0, sizeof(pfd));
-       pfd.fd = pkt_q->sockfd;
-       pfd.events = POLLOUT;
-       pfd.revents = 0;
-
        framecount = pkt_q->framecount;
        framenum = pkt_q->framenum;
        ppd = (struct tpacket2_hdr *) pkt_q->rd[framenum].iov_base;
@@ -368,30 +361,7 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
                             mbuf->pkt_len > pkt_q->frame_data_size))
                        continue;
 
-               /* point at the next incoming frame */
-               if 
(unlikely(!tx_ring_status_available(tpacket_read_status(ppd)))) {
-                       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
-                *
-                * This happens, because packet_poll() calls datagram_poll()
-                * which checks the space left in the socket buffer and,
-                * in the case of packet_mmap, the default socket buffer length
-                * doesn't match the requested size for the tx_ring.
-                * As such, there is almost always space left in socket buffer,
-                * which doesn't seem to be correlated to the requested size
-                * for the tx_ring in packet_mmap.
-                *
-                * This results in poll() returning POLLOUT.
-                */
+               /* ring full: return short, the caller retries the rest */
                if 
(unlikely(!tx_ring_status_available(tpacket_read_status(ppd))))
                        break;
 
-- 
2.54.0 (Apple Git-157)

Reply via email to