Add a txpollnotrdy devarg (default 1). When 0, TX returns instead of
blocking in poll() when the ring is full, for applications that must
not block and apply backpressure themselves.

Signed-off-by: Scott Mitchell <[email protected]>
---
 app/test/test_pmd_af_packet.c             | 108 ++++++++++++++++++++++
 doc/guides/nics/af_packet.rst             |   6 +-
 doc/guides/rel_notes/release_27_03.rst    |   4 +
 drivers/net/af_packet/rte_eth_af_packet.c |  34 +++++--
 4 files changed, 144 insertions(+), 8 deletions(-)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index 93e10f4b77..59a5353cba 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -89,6 +89,9 @@ struct tag_count {
 #define LAYOUT_NB_FRAMES (3 * LAYOUT_FRAMECNT)
 #define LAYOUT_FRAME_LEN 1400
 
+/* TX ring size for the txpollnotrdy test */
+#define TXPOLL_FRAMECNT 16
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -1917,6 +1920,109 @@ test_af_packet_tx_mbuf_accounting(void)
        return ret;
 }
 
+/*
+ * Test: txpollnotrdy only accepts 0 or 1
+ */
+static int
+test_af_packet_invalid_txpollnotrdy(void)
+{
+       static const char * const bad[] = { "2", "-1", "abc", "" };
+       char devargs[256];
+       unsigned int i;
+       int ret;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+
+       for (i = 0; i < RTE_DIM(bad); i++) {
+               snprintf(devargs, sizeof(devargs), "iface=%s,txpollnotrdy=%s",
+                        TAP_DEV_NAME, bad[i]);
+               ret = rte_vdev_init("net_af_packet_txpoll_bad", devargs);
+               if (ret == 0)
+                       rte_vdev_uninit("net_af_packet_txpoll_bad");
+               TEST_ASSERT(ret != 0, "Expected failure with txpollnotrdy=%s", 
bad[i]);
+       }
+
+       return TEST_SUCCESS;
+}
+
+/* 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: With txpollnotrdy=0, TX returns instead of blocking when the ring
+ * is full. With the link down the kernel leaves frames in SEND_REQUEST, so
+ * the ring fills after framecnt frames.
+ */
+static int
+test_af_packet_txpollnotrdy_nonblocking(void)
+{
+       const char *name = "net_af_packet_txpoll";
+       struct rte_mbuf *bufs[2 * TXPOLL_FRAMECNT];
+       char devargs[256];
+       uint64_t start, elapsed_ms;
+       uint16_t pid, nb_tx, nb_tx2;
+       int 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=2048,framecnt=%d,txpollnotrdy=0",
+                TAP_DEV_NAME, TXPOLL_FRAMECNT);
+       ret = create_test_port(name, devargs, 0, &pid);
+       TEST_ASSERT(ret == 0, "Failed to create port");
+
+       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, RTE_DIM(bufs)) == 0)
+               goto out;
+       nb_tx = do_tx_burst(pid, 0, bufs, RTE_DIM(bufs));
+       if (alloc_tx_mbufs(bufs, RTE_DIM(bufs)) == 0)
+               goto out;
+       nb_tx2 = do_tx_burst(pid, 0, bufs, RTE_DIM(bufs));
+       elapsed_ms = (rte_get_timer_cycles() - start) * MS_PER_S / 
rte_get_timer_hz();
+
+       printf("Link down: TX %u then %u of %zu in %" PRIu64 " ms\n",
+              nb_tx, nb_tx2, RTE_DIM(bufs), elapsed_ms);
+       if (nb_tx > TXPOLL_FRAMECNT || nb_tx2 != 0 || elapsed_ms > 1000)
+               goto out;
+       ret = TEST_SUCCESS;
+out:
+       set_tap_link(true);
+       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,
@@ -1944,6 +2050,8 @@ 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_invalid_txpollnotrdy),
+               TEST_CASE(test_af_packet_txpollnotrdy_nonblocking),
                TEST_CASE(test_af_packet_qdisc_bypass),
                TEST_CASE(test_af_packet_multi_queue),
                TEST_CASE(test_af_packet_fanout_modes),
diff --git a/doc/guides/nics/af_packet.rst b/doc/guides/nics/af_packet.rst
index d7fb4162fd..7a6cc4ebe9 100644
--- a/doc/guides/nics/af_packet.rst
+++ b/doc/guides/nics/af_packet.rst
@@ -31,6 +31,10 @@ Some of these, in turn, will be used to configure the 
PACKET_MMAP settings.
 *   ``framesz`` - PACKET_MMAP frame size (optional, default 2048B; Note: 
multiple
     of 16B);
 *   ``framecnt`` - PACKET_MMAP frame count (optional, default 512).
+*   ``txpollnotrdy`` - Control behavior if tx is attempted but there is no
+    space available to write to the kernel. If 1, call poll() and block until
+    space is available to tx. If 0, don't call poll() and return from tx 
(optional,
+    default 1).
 
 For details regarding ``fanout_mode`` argument, you can consult the
 `PACKET_FANOUT documentation 
<https://www.man7.org/linux/man-pages/man7/packet.7.html>`_.
@@ -84,7 +88,7 @@ framecnt=512):
 
 .. code-block:: console
 
-    
--vdev=eth_af_packet0,iface=tap0,blocksz=4096,framesz=2048,framecnt=512,qpairs=1,qdisc_bypass=1,fanout_mode=hash
+    
--vdev=eth_af_packet0,iface=tap0,blocksz=4096,framesz=2048,framecnt=512,qpairs=1,qdisc_bypass=1,fanout_mode=hash,txpollnotrdy=0
 
 Features and Limitations
 ------------------------
diff --git a/doc/guides/rel_notes/release_27_03.rst 
b/doc/guides/rel_notes/release_27_03.rst
index a91813faca..01940f9ac6 100644
--- a/doc/guides/rel_notes/release_27_03.rst
+++ b/doc/guides/rel_notes/release_27_03.rst
@@ -55,6 +55,10 @@ New Features
      Also, make sure to start the actual text at the margin.
      =======================================================
 
+* **Updated af_packet net driver.**
+
+  * Added ``txpollnotrdy`` devarg to avoid ``poll()`` blocking calls.
+
 
 Removed Items
 -------------
diff --git a/drivers/net/af_packet/rte_eth_af_packet.c 
b/drivers/net/af_packet/rte_eth_af_packet.c
index 72d0dbdd60..5ed0bd73ba 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -52,9 +52,11 @@
 #define ETH_AF_PACKET_QDISC_BYPASS_ARG "qdisc_bypass"
 #define ETH_AF_PACKET_FANOUT_MODE_ARG  "fanout_mode"
 #define ETH_AF_PACKET_IGNORE_OUTGOING_ARG      "ignore_outgoing"
+#define ETH_AF_PACKET_TX_POLL_NOT_READY_ARG    "txpollnotrdy"
 
 #define DFLT_FRAME_SIZE                (1 << 11)
 #define DFLT_FRAME_COUNT       (1 << 9)
+#define DFLT_TX_POLL_NOT_RDY   1
 
 #define ETH_AF_PACKET_FRAME_OVERHEAD (TPACKET2_HDRLEN - sizeof(struct 
sockaddr_ll))
 
@@ -91,6 +93,9 @@ struct __rte_cache_aligned pkt_tx_queue {
        unsigned int framecount;
        unsigned int framenum;
 
+       bool txpollnotrdy;
+       bool sw_cksum;
+
        volatile unsigned long tx_pkts;
        volatile unsigned long err_pkts;
        volatile unsigned long tx_bytes;
@@ -120,6 +125,7 @@ static const char *valid_arguments[] = {
        ETH_AF_PACKET_QDISC_BYPASS_ARG,
        ETH_AF_PACKET_FANOUT_MODE_ARG,
        ETH_AF_PACKET_IGNORE_OUTGOING_ARG,
+       ETH_AF_PACKET_TX_POLL_NOT_READY_ARG,
        NULL
 };
 
@@ -353,10 +359,12 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
        uint32_t num_tx_bytes = 0;
        uint16_t i;
 
-       memset(&pfd, 0, sizeof(pfd));
-       pfd.fd = pkt_q->sockfd;
-       pfd.events = POLLOUT;
-       pfd.revents = 0;
+       if (pkt_q->txpollnotrdy) {
+               memset(&pfd, 0, sizeof(pfd));
+               pfd.fd = pkt_q->sockfd;
+               pfd.events = POLLOUT;
+               pfd.revents = 0;
+       }
 
        framecount = pkt_q->framecount;
        framenum = pkt_q->framenum;
@@ -385,8 +393,9 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
                 * This results in poll() returning POLLOUT.
                 */
                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))))) {
+                       (!pkt_q->txpollnotrdy || 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;
                }
@@ -909,6 +918,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                       unsigned int qdisc_bypass,
                       const char *fanout_mode,
                       unsigned int ignore_outgoing,
+                      bool txpollnotrdy,
                       struct pmd_internals **internals,
                       struct rte_eth_dev **eth_dev,
                       struct rte_kvargs *kvlist)
@@ -1155,6 +1165,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                        tx_queue->rd[i].iov_len = req->tp_frame_size;
                }
                tx_queue->sockfd = qsockfd;
+               tx_queue->txpollnotrdy = txpollnotrdy;
 
                rc = bind(qsockfd, (const struct sockaddr*)&sockaddr, 
sizeof(sockaddr));
                if (rc == -1) {
@@ -1281,6 +1292,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        unsigned int qdisc_bypass = 1;
        const char *fanout_mode = NULL;
        unsigned int ignore_outgoing = 0;
+       unsigned int txpollnotrdy = DFLT_TX_POLL_NOT_RDY;
 
        /* do some parameter checking */
        if (*sockfd < 0)
@@ -1350,6 +1362,11 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                                return -1;
                        continue;
                }
+               if (strstr(pair->key, ETH_AF_PACKET_TX_POLL_NOT_READY_ARG) != 
NULL) {
+                       if (parse_uint(pair->key, pair->value, &txpollnotrdy, 
1) < 0)
+                               return -1;
+                       continue;
+               }
        }
 
        if (framesize > blocksize) {
@@ -1419,6 +1436,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        else
                PMD_LOG(DEBUG, "%s:\tfanout mode %s", name, "default 
PACKET_FANOUT_HASH");
        PMD_LOG(DEBUG, "%s:\tignore outgoing %u", name, ignore_outgoing);
+       PMD_LOG(DEBUG, "%s:\ttxpollnotrdy %u", name, txpollnotrdy);
 
        if (rte_pmd_init_internals(dev, *sockfd, qpairs,
                                   blocksize, blockcount,
@@ -1426,6 +1444,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                                   qdisc_bypass,
                                   fanout_mode,
                                   ignore_outgoing,
+                                  txpollnotrdy != 0,
                                   &internals, &eth_dev,
                                   kvlist) < 0)
                return -1;
@@ -1524,4 +1543,5 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,
        "framecnt=<int> "
        "qdisc_bypass=<0|1> "
        "fanout_mode=<hash|lb|cpu|rollover|rnd|qm> "
-       "ignore_outgoing=<0|1>");
+       "ignore_outgoing=<0|1> "
+       "txpollnotrdy=<0|1>");
-- 
2.54.0 (Apple Git-157)

Reply via email to