The sendto() TX kick makes the kernel process queued frames
synchronously on the calling lcore. Optionally kick through an io_uring
SQPOLL ring instead, so a kernel thread does that work, as virtio
backends do. This improves throughput by 30%+ where virtio isn't an
option, such as containers.

New devargs: tx_io_uring_enabled, tx_io_uring_sq_size,
tx_io_uring_sq_thread_idle, tx_io_uring_q_cpu (SQPOLL CPU affinity)
and tx_io_uring_wq_num (queues share SQPOLL threads).

Requires liburing >= 2.4. Without it, or if SQPOLL setup fails, TX
falls back to sendto().

Signed-off-by: Scott Mitchell <[email protected]>
---
Depends-on: series-40223 ("net/af_packet: correctness, performance, cksum")

RFC v2:
- submit with io_uring_submit(); the open-coded SQ tail store lacked the
  full barrier before the IORING_SQ_NEED_WAKEUP check
- fix tx_io_uring_wq_num: the IORING_SETUP_ATTACH_WQ probe always failed
- create rings directly instead of probing, drop flags the kernel rejects
- drop IORING_SETUP_COOP_TASKRUN, which the kernel rejects with SQPOLL
- parse devargs with parse_uint()
- add tests
---
 app/test/test_pmd_af_packet.c             | 282 +++++++++++++++
 doc/guides/nics/af_packet.rst             |  18 +-
 doc/guides/rel_notes/release_27_03.rst    |   1 +
 drivers/net/af_packet/meson.build         |  14 +
 drivers/net/af_packet/rte_eth_af_packet.c | 399 +++++++++++++++++++++-
 5 files changed, 699 insertions(+), 15 deletions(-)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index 7193d7505f..41433d829e 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -4,7 +4,9 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <dirent.h>
 #include <inttypes.h>
+#include <limits.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <linux/if.h>
@@ -99,6 +101,10 @@ struct tag_count {
 #define CKSUM_DST_PORT 0x5678
 #define CKSUM_PAYLOAD_LEN 32
 
+/* io_uring tests */
+#define IDLE_WAKE_ROUNDS 100
+#define SQPOLL_QUEUES 4
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -2188,6 +2194,277 @@ test_af_packet_tx_cksum(void)
        return ret;
 }
 
+/*
+ * Test: io_uring devargs are validated
+ */
+static int
+test_af_packet_invalid_io_uring_args(void)
+{
+       static const char * const bad[] = {
+               "tx_io_uring_enabled=2",
+               "tx_io_uring_sq_size=0",
+               "tx_io_uring_sq_size=40000",
+               "tx_io_uring_sq_size=abc",
+               "tx_io_uring_sq_thread_idle=-1",
+               "tx_io_uring_q_cpu=-1",
+               "tx_io_uring_wq_num=99999",
+       };
+       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,%s", TAP_DEV_NAME, 
bad[i]);
+               ret = rte_vdev_init("net_af_packet_uring_bad", devargs);
+               if (ret == 0)
+                       rte_vdev_uninit("net_af_packet_uring_bad");
+               TEST_ASSERT(ret != 0, "Expected failure with %s", bad[i]);
+       }
+
+       return TEST_SUCCESS;
+}
+
+/*
+ * Test: TX with io_uring kicks. Passes on the sendto() fallback too, when
+ * liburing or SQPOLL is unavailable.
+ */
+static int
+test_af_packet_io_uring_tx(void)
+{
+       const char *name = "net_af_packet_uring";
+       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,tx_io_uring_enabled=1",
+                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);
+
+       fcntl(tap_fd, F_SETFL, flags);
+       destroy_test_port(name, pid);
+       return ret;
+}
+
+/*
+ * Test: A frame sent after the SQPOLL thread went idle still goes out.
+ * With a 1 ms idle timeout and a 3 ms gap before each frame, every kick has
+ * to wake a sleeping SQ thread; a missed wakeup leaves the frame queued.
+ * This is a smoke test: the race window is small, so a pass is not proof.
+ */
+static int
+test_af_packet_io_uring_idle_wakeup(void)
+{
+       const char *name = "net_af_packet_uring_idle";
+       bool seen[IDLE_WAKE_ROUNDS] = { false };
+       unsigned int round, nb_seen = 0;
+       struct rte_mbuf *m;
+       char devargs[256];
+       uint8_t *data;
+       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,tx_io_uring_enabled=1,tx_io_uring_sq_thread_idle=1",
+                TAP_DEV_NAME);
+       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;
+       for (round = 0; round < IDLE_WAKE_ROUNDS; round++) {
+               rte_delay_ms(3);
+               m = rte_pktmbuf_alloc(mp);
+               if (m == NULL)
+                       goto out;
+               data = (uint8_t *)rte_pktmbuf_append(m, TEST_FRAME_LEN);
+               if (data == NULL) {
+                       rte_pktmbuf_free(m);
+                       goto out;
+               }
+               fill_seq_frame(data, TEST_FRAME_LEN, &DEV_MAC_TAG, round);
+               if (do_tx_burst(pid, 0, &m, 1) != 1)
+                       goto out;
+               if (tap_collect_seq(seen, IDLE_WAKE_ROUNDS, &nb_seen, round + 1,
+                                   TEST_FRAME_LEN, TAP_READ_TIMEOUT_US) != 
(int)(round + 1)) {
+                       printf("Frame %u not sent after idle (seen %u)\n", 
round, nb_seen);
+                       goto out;
+               }
+       }
+       ret = TEST_SUCCESS;
+out:
+       fcntl(tap_fd, F_SETFL, flags);
+       destroy_test_port(name, pid);
+       return ret;
+}
+
+/* Helper: Count open file descriptors of this process, or -1. */
+static int
+count_open_fds(void)
+{
+       struct dirent *de;
+       DIR *dir;
+       int n = 0;
+
+       dir = opendir("/proc/self/fd");
+       if (dir == NULL)
+               return -1;
+       while ((de = readdir(dir)) != NULL)
+               if (de->d_name[0] != '.')
+                       n++;
+       closedir(dir);
+       return n;
+}
+
+/*
+ * Test: Creating and closing io_uring ports, including ones sharing an
+ * SQPOLL thread (tx_io_uring_wq_num), does not leak file descriptors.
+ */
+static int
+test_af_packet_io_uring_create_destroy(void)
+{
+       const char *name = "net_af_packet_uring_cd";
+       char devargs[256];
+       int before, after, i;
+       uint16_t pid;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+
+       snprintf(devargs, sizeof(devargs),
+                "iface=%s,qpairs=2,tx_io_uring_enabled=1,tx_io_uring_wq_num=1",
+                TAP_DEV_NAME);
+
+       before = count_open_fds();
+       for (i = 0; i < 10; i++) {
+               TEST_ASSERT(create_af_packet_port(name, devargs, &pid) == 0,
+                           "Failed to create port (iteration %d)", i);
+               TEST_ASSERT(configure_af_packet_port(pid, 2, 2) == 0,
+                           "Failed to configure port (iteration %d)", i);
+               destroy_test_port(name, pid);
+       }
+       after = count_open_fds();
+
+       TEST_ASSERT(before >= 0 && before == after,
+                   "Open fds %d before, %d after", before, after);
+       return TEST_SUCCESS;
+}
+
+/* Helper: Count this process's io_uring SQPOLL threads (comm 
"iou-sqp-<pid>"), or -1. */
+static int
+count_sqpoll_threads(void)
+{
+       char path[PATH_MAX], comm[32];
+       struct dirent *de;
+       DIR *dir;
+       FILE *f;
+       int n = 0;
+
+       dir = opendir("/proc/self/task");
+       if (dir == NULL)
+               return -1;
+       while ((de = readdir(dir)) != NULL) {
+               if (de->d_name[0] == '.')
+                       continue;
+               snprintf(path, sizeof(path), "/proc/self/task/%s/comm", 
de->d_name);
+               f = fopen(path, "r");
+               if (f == NULL)
+                       continue;
+               if (fgets(comm, sizeof(comm), f) != NULL && strncmp(comm, 
"iou-sqp-", 8) == 0)
+                       n++;
+               fclose(f);
+       }
+       closedir(dir);
+       return n;
+}
+
+/* Helper: Wait up to 1s for the SQPOLL thread count to reach want. Returns 
the count. */
+static int
+wait_sqpoll_threads(int want)
+{
+       int i, n = count_sqpoll_threads();
+
+       for (i = 0; i < 100 && n != want; i++) {
+               rte_delay_ms(10);
+               n = count_sqpoll_threads();
+       }
+       return n;
+}
+
+/*
+ * Test: Queues share SQPOLL threads when tx_io_uring_wq_num is below the
+ * queue count (IORING_SETUP_ATTACH_WQ).
+ */
+static int
+test_af_packet_io_uring_shared_sqpoll(void)
+{
+       static const unsigned int wq_nums[] = { 1, 2 };
+       const char *name = "net_af_packet_uring_wq";
+       char devargs[256];
+       unsigned int i;
+       uint16_t pid;
+       int n;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+       TEST_ASSERT(wait_sqpoll_threads(0) == 0, "SQPOLL threads left from 
earlier tests");
+
+       for (i = 0; i < RTE_DIM(wq_nums); i++) {
+               snprintf(devargs, sizeof(devargs),
+                        
"iface=%s,qpairs=%d,tx_io_uring_enabled=1,tx_io_uring_wq_num=%u",
+                        TAP_DEV_NAME, SQPOLL_QUEUES, wq_nums[i]);
+               TEST_ASSERT(create_af_packet_port(name, devargs, &pid) == 0,
+                           "Failed to create port");
+               if (configure_af_packet_port(pid, SQPOLL_QUEUES, SQPOLL_QUEUES) 
!= 0) {
+                       destroy_test_port(name, pid);
+                       return TEST_FAILED;
+               }
+               /* SQPOLL threads start and exit asynchronously; let the count 
settle */
+               n = wait_sqpoll_threads(wq_nums[i]);
+               destroy_test_port(name, pid);
+
+               if (n == 0) {
+                       printf("SKIPPED: io_uring SQPOLL not available\n");
+                       return TEST_SKIPPED;
+               }
+               printf("%d queues, %s=%u: %d SQPOLL threads\n", SQPOLL_QUEUES,
+                      "tx_io_uring_wq_num", wq_nums[i], n);
+               TEST_ASSERT(n == (int)wq_nums[i], "Expected %u SQPOLL threads, 
got %d",
+                           wq_nums[i], n);
+               TEST_ASSERT(wait_sqpoll_threads(0) == 0, "SQPOLL threads did 
not exit");
+       }
+       return TEST_SUCCESS;
+}
+
 static struct unit_test_suite af_packet_test_suite = {
        .suite_name = "AF_PACKET PMD Unit Test Suite",
        .setup = test_af_packet_setup,
@@ -2218,6 +2495,11 @@ static struct unit_test_suite af_packet_test_suite = {
                TEST_CASE(test_af_packet_invalid_txpollnotrdy),
                TEST_CASE(test_af_packet_txpollnotrdy_nonblocking),
                TEST_CASE(test_af_packet_tx_cksum),
+               TEST_CASE(test_af_packet_invalid_io_uring_args),
+               TEST_CASE(test_af_packet_io_uring_tx),
+               TEST_CASE(test_af_packet_io_uring_idle_wakeup),
+               TEST_CASE(test_af_packet_io_uring_create_destroy),
+               TEST_CASE(test_af_packet_io_uring_shared_sqpoll),
                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 7a6cc4ebe9..6e00db5165 100644
--- a/doc/guides/nics/af_packet.rst
+++ b/doc/guides/nics/af_packet.rst
@@ -35,6 +35,22 @@ Some of these, in turn, will be used to configure the 
PACKET_MMAP settings.
     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).
+*   ``tx_io_uring_enabled`` - Use io_uring to notify the kernel on tx 
(optional,
+    default 0)
+*   ``tx_io_uring_sq_size`` - Number of entries for the io_uring ring 
(optional,
+    default 1024)
+*   ``tx_io_uring_sq_thread_idle`` - Idle time (in milliseconds) before an 
io_uring
+    SQPOLL thread goes to sleep (optional, default 500)
+*   ``tx_io_uring_q_cpu`` - This argument can be repeated to specify CPU core
+    affinity for each queue's io_uring SQPOLL kernel thread (see
+    ``io_uring_params.sq_thread_cpu``). The first value applies to queue 0,
+    the second to queue 1, etc. (optional, default empty). An unpinned SQPOLL
+    thread may run on any CPU, so pin it when lcores run with real-time 
priority,
+    where it could otherwise be starved.
+*   ``tx_io_uring_wq_num`` - Number of SQPOLL kernel threads. On kernels with
+    ``IORING_SETUP_ATTACH_WQ`` support, setting this lower than the queue count
+    enables thread sharing across queues. If 0 or unspecified, no sharing is 
configured
+    and each queue creates its own SQPOLL thread (optional, default 0)
 
 For details regarding ``fanout_mode`` argument, you can consult the
 `PACKET_FANOUT documentation 
<https://www.man7.org/linux/man-pages/man7/packet.7.html>`_.
@@ -88,7 +104,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,txpollnotrdy=0
+    
--vdev=eth_af_packet0,iface=tap0,blocksz=4096,framesz=2048,framecnt=512,qpairs=1,qdisc_bypass=1,fanout_mode=hash,txpollnotrdy=0,tx_io_uring_enabled=1
 
 Features and Limitations
 ------------------------
diff --git a/doc/guides/rel_notes/release_27_03.rst 
b/doc/guides/rel_notes/release_27_03.rst
index c2b25c1387..76bc028afc 100644
--- a/doc/guides/rel_notes/release_27_03.rst
+++ b/doc/guides/rel_notes/release_27_03.rst
@@ -60,6 +60,7 @@ New Features
   * Added ``txpollnotrdy`` devarg to avoid ``poll()`` blocking calls.
   * Added software checksum offload support for ``IPV4_CKSUM``, ``UDP_CKSUM``
     and ``TCP_CKSUM``.
+  * Added optional io_uring SQPOLL notification of the kernel on Tx.
 
 
 Removed Items
diff --git a/drivers/net/af_packet/meson.build 
b/drivers/net/af_packet/meson.build
index f45e4491d4..a672262656 100644
--- a/drivers/net/af_packet/meson.build
+++ b/drivers/net/af_packet/meson.build
@@ -5,5 +5,19 @@ if not is_linux
     build = false
     reason = 'only supported on Linux'
 endif
+
+deps += ['ethdev']
+
+liburing_dep = dependency('liburing', required: false)
+if not liburing_dep.found()
+    message('AF_PACKET PMD: liburing not found, fallback to sendto()')
+elif not liburing_dep.version().version_compare('>=2.4')
+    message('AF_PACKET PMD: liburing version too old, need >= 2.4. fallback to 
sendto()')
+else
+    ext_deps += liburing_dep
+    cflags += '-DRTE_NET_AF_PACKET_HAVE_LIBURING'
+    message('AF_PACKET PMD: liburing @0@ support 
enabled'.format(liburing_dep.version()))
+endif
+
 sources = files('rte_eth_af_packet.c')
 require_iova_in_mbuf = false
diff --git a/drivers/net/af_packet/rte_eth_af_packet.c 
b/drivers/net/af_packet/rte_eth_af_packet.c
index f21be21009..37e29048e9 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -20,6 +20,10 @@
 #include <bus_vdev_driver.h>
 
 #include <ctype.h>
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+#include <liburing.h>
+#endif
+
 #include <errno.h>
 #include <limits.h>
 #include <linux/if_ether.h>
@@ -55,10 +59,22 @@
 #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 ETH_AF_PACKET_TX_IO_URING_ENABLED_ARG  "tx_io_uring_enabled"
+#define ETH_AF_PACKET_TX_IO_URING_SQ_SIZE_ARG  "tx_io_uring_sq_size"
+#define ETH_AF_PACKET_TX_IO_URING_SQ_THREAD_IDLE_ARG   
"tx_io_uring_sq_thread_idle"
+#define ETH_AF_PACKET_TX_IO_URING_Q_CPU        "tx_io_uring_q_cpu"
+#define ETH_AF_PACKET_TX_IO_URING_WQ_NUM       "tx_io_uring_wq_num"
 
 #define DFLT_FRAME_SIZE                (1 << 11)
 #define DFLT_FRAME_COUNT       (1 << 9)
 #define DFLT_TX_POLL_NOT_RDY   1
+#define DFLT_TX_IO_URING_ENABLED       false
+#define DFLT_TX_IO_URING_SQ_SIZE       1024
+#define ETH_AF_PACKET_URING_SQ_SIZE_MAX        32768
+#define DFLT_TX_IO_URING_SQ_THREAD_IDLE_MS     500
+
+/* io_uring registered file descriptor index for the socket fd */
+#define ETH_AF_PACKET_URING_SOCKFD_INDEX       0
 
 #define ETH_AF_PACKET_FRAME_OVERHEAD (TPACKET2_HDRLEN - sizeof(struct 
sockaddr_ll))
 
@@ -95,6 +111,12 @@ struct __rte_cache_aligned pkt_tx_queue {
        unsigned int framecount;
        unsigned int framenum;
 
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+       struct io_uring uring_ring;
+       bool uring_enabled;
+       uint32_t uring_sqe_since_cq_advance;
+#endif
+
        bool txpollnotrdy;
        bool sw_cksum;
 
@@ -117,6 +139,11 @@ struct pmd_internals {
        uint8_t vlan_strip;
        uint8_t timestamp_offloading;
        bool tx_sw_cksum;
+
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+       struct io_uring **shared_uring_rings;
+       uint32_t shared_uring_count;
+#endif
 };
 
 static const char *valid_arguments[] = {
@@ -129,6 +156,11 @@ static const char *valid_arguments[] = {
        ETH_AF_PACKET_FANOUT_MODE_ARG,
        ETH_AF_PACKET_IGNORE_OUTGOING_ARG,
        ETH_AF_PACKET_TX_POLL_NOT_READY_ARG,
+       ETH_AF_PACKET_TX_IO_URING_ENABLED_ARG,
+       ETH_AF_PACKET_TX_IO_URING_SQ_SIZE_ARG,
+       ETH_AF_PACKET_TX_IO_URING_SQ_THREAD_IDLE_ARG,
+       ETH_AF_PACKET_TX_IO_URING_Q_CPU,
+       ETH_AF_PACKET_TX_IO_URING_WQ_NUM,
        NULL
 };
 
@@ -211,6 +243,198 @@ tpacket_write_status(volatile void *tp_status, uint32_t 
status)
        *((volatile unaligned_uint32_t *)tp_status) = status;
 }
 
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+/*
+ * Create a ring. The kernel rejects unsupported setup flags with -EINVAL 
before
+ * creating anything, so optional flags are dropped one at a time and retried.
+ */
+static int
+af_packet_io_uring_init(struct io_uring *ring, uint32_t entries,
+                       struct io_uring_params *params, unsigned int q)
+{
+       static const struct {
+               uint32_t flag;
+               const char *name;
+       } optional[] = {
+               { IORING_SETUP_SINGLE_ISSUER, "IORING_SETUP_SINGLE_ISSUER" },
+               { IORING_SETUP_SQ_AFF, "IORING_SETUP_SQ_AFF (SQPOLL CPU 
pinning)" },
+       };
+       struct io_uring_params p = *params;
+       unsigned int i;
+       int rc;
+
+       rc = io_uring_queue_init_params(entries, ring, &p);
+       for (i = 0; rc == -EINVAL && i < RTE_DIM(optional); i++) {
+               if ((params->flags & optional[i].flag) == 0)
+                       continue;
+               params->flags &= ~optional[i].flag;
+               PMD_LOG(NOTICE, "AF_PACKET PMD io_uring tx_queue=%u: %s 
rejected, retrying without it",
+                       q, optional[i].name);
+               p = *params;
+               rc = io_uring_queue_init_params(entries, ring, &p);
+       }
+       if (rc == 0)
+               *params = p;
+       return rc;
+}
+#endif /* RTE_NET_AF_PACKET_HAVE_LIBURING */
+
+static void
+af_packet_io_uring_share_uring_init(struct pmd_internals *internals,
+                               const unsigned int numa_node,
+                               const unsigned int nb_queues,
+                               const bool tx_io_uring_enabled,
+                               const uint32_t tx_io_uring_kq_cores_size,
+                               const uint32_t tx_io_uring_wq_num)
+{
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+       if (!tx_io_uring_enabled)
+               return;
+
+       if (tx_io_uring_wq_num <= 0)
+               return;
+
+       /* Rings attach to a queue's SQPOLL thread with IORING_SETUP_ATTACH_WQ. 
Support
+        * can't be probed without a ring to attach to, so failures are handled 
per queue.
+        */
+       internals->shared_uring_rings =
+               rte_calloc_socket("af_packet_shared_uring", tx_io_uring_wq_num,
+                       sizeof(*internals->shared_uring_rings), 0, numa_node);
+       if (!internals->shared_uring_rings) {
+               PMD_LOG(ERR, "Failed to allocate shared io_uring rings");
+               return;
+       }
+       internals->shared_uring_count = tx_io_uring_wq_num;
+
+       if (tx_io_uring_kq_cores_size > tx_io_uring_wq_num) {
+               PMD_LOG(WARNING,
+                       "AF_PACKET PMD %s count=%" PRIu32 " > %s=%" PRIu32 ", "
+                       "extra CPU affinities will be ignored",
+                       ETH_AF_PACKET_TX_IO_URING_Q_CPU, 
tx_io_uring_kq_cores_size,
+                       ETH_AF_PACKET_TX_IO_URING_WQ_NUM, tx_io_uring_wq_num);
+       }
+
+       if (nb_queues % tx_io_uring_wq_num != 0) {
+               PMD_LOG(INFO,
+                       "AF_PACKET PMD %s=%u not divisible by %s=%" PRIu32 ", "
+                       "SQPOLL thread work may be imbalanced",
+                       ETH_AF_PACKET_NUM_Q_ARG, nb_queues,
+                       ETH_AF_PACKET_TX_IO_URING_WQ_NUM, tx_io_uring_wq_num);
+       }
+#else
+       if (tx_io_uring_enabled) {
+               PMD_LOG(NOTICE,
+                       "AF_PACKET PMD %s enabled, but feature not enabled at 
compile time.",
+                       ETH_AF_PACKET_TX_IO_URING_ENABLED_ARG);
+       }
+       RTE_SET_USED(internals);
+       RTE_SET_USED(numa_node);
+       RTE_SET_USED(nb_queues);
+       RTE_SET_USED(tx_io_uring_kq_cores_size);
+       RTE_SET_USED(tx_io_uring_wq_num);
+#endif /* RTE_NET_AF_PACKET_HAVE_LIBURING */
+}
+
+static void
+af_packet_io_uring_tx_queue_init(struct pmd_internals *internals,
+                               const unsigned int q,
+                               const bool tx_io_uring_enabled,
+                               const uint32_t tx_io_uring_sq_size,
+                               const uint32_t tx_io_uring_sq_thread_idle_ms,
+                               const uint32_t *tx_io_uring_kq_cores,
+                               const uint32_t tx_io_uring_kq_cores_size)
+{
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+       if (!tx_io_uring_enabled)
+               return;
+
+       int rc;
+       struct pkt_tx_queue *tx_queue = &internals->tx_queue[q];
+       struct io_uring_params uring_params = {
+               .sq_thread_idle = tx_io_uring_sq_thread_idle_ms,
+               .flags = IORING_SETUP_SQPOLL,
+       };
+
+       /* Determine if attaching to existing ring or creating new one */
+       uint32_t ring_idx = internals->shared_uring_rings ? q % 
internals->shared_uring_count : 0;
+       bool attach_mode = internals->shared_uring_rings && ring_idx < q &&
+                       internals->shared_uring_rings[ring_idx];
+
+       if (attach_mode) {
+               /* Share the group's SQPOLL thread; the ring's memory and 
lifetime stay per queue */
+               uring_params.flags |= IORING_SETUP_ATTACH_WQ;
+               uring_params.wq_fd = 
internals->shared_uring_rings[ring_idx]->ring_fd;
+       } else {
+               /* Create new ring: add CPU affinity if configured */
+               if (tx_io_uring_kq_cores_size > 0) {
+                       uint32_t cpu_idx = internals->shared_uring_rings ? 
ring_idx : q;
+                       if (cpu_idx < tx_io_uring_kq_cores_size) {
+                               uring_params.flags |= IORING_SETUP_SQ_AFF;
+                               uring_params.sq_thread_cpu = 
tx_io_uring_kq_cores[cpu_idx];
+                       } else {
+                               PMD_LOG(DEBUG,
+                                       "AF_PACKET PMD io_uring tx_queue=%u > 
%s size=%" PRIu32 " "
+                                       "and will not be CPU pinned", q,
+                                       ETH_AF_PACKET_TX_IO_URING_Q_CPU,
+                                       tx_io_uring_kq_cores_size);
+                       }
+               }
+
+               uring_params.flags |= IORING_SETUP_SINGLE_ISSUER;
+       }
+
+       rc = af_packet_io_uring_init(&tx_queue->uring_ring, tx_io_uring_sq_size,
+                       &uring_params, q);
+       if (rc < 0 && attach_mode) {
+               PMD_LOG(WARNING, "AF_PACKET PMD io_uring IORING_SETUP_ATTACH_WQ 
failed (error %d: %s), "
+                       "tx_queue=%u uses its own SQPOLL thread", -rc, 
strerror(-rc), q);
+               attach_mode = false;
+               uring_params.flags &= ~IORING_SETUP_ATTACH_WQ;
+               uring_params.wq_fd = 0;
+               rc = af_packet_io_uring_init(&tx_queue->uring_ring, 
tx_io_uring_sq_size,
+                               &uring_params, q);
+       }
+       if (rc < 0) {
+               PMD_LOG(WARNING, "AF_PACKET PMD Failed 
io_uring_queue_init_params (error %d: %s), "
+                       "tx_queue=%u sq_size=%" PRIu32 " flags=0x%X 
sq_thread_cpu=%u "
+                       "sq_thread_idle=%u, falling back to synchronous 
sendto()",
+                       -rc, strerror(-rc), q, tx_io_uring_sq_size, 
uring_params.flags,
+                       uring_params.sq_thread_cpu, 
uring_params.sq_thread_idle);
+               return;
+       }
+
+       /* Register the sockfd at index ETH_AF_PACKET_URING_SOCKFD_INDEX */
+       rc = io_uring_register_files(&tx_queue->uring_ring, &tx_queue->sockfd, 
1);
+       if (rc < 0) {
+               PMD_LOG(WARNING, "AF_PACKET PMD Failed io_uring_register_files 
(error %d: %s)",
+                               -rc, strerror(-rc));
+               io_uring_queue_exit(&tx_queue->uring_ring);
+               return;
+       }
+
+       tx_queue->uring_enabled = true;
+       tx_queue->uring_sqe_since_cq_advance = 0;
+
+       if (!attach_mode && internals->shared_uring_rings &&
+           !internals->shared_uring_rings[ring_idx])
+               internals->shared_uring_rings[ring_idx] = &tx_queue->uring_ring;
+
+       PMD_LOG(DEBUG, "AF_PACKET PMD io_uring SQPOLL%s enabled tx_queue=%u 
ring_idx=%u "
+               "tx_io_uring_sq_size=%" PRIu32 " kthread_cpu=%u 
sq_thread_idle=%u flags=0x%X",
+               attach_mode ? "+ATTACH_WQ" : "", q, ring_idx, 
tx_io_uring_sq_size,
+               uring_params.sq_thread_cpu, uring_params.sq_thread_idle, 
uring_params.flags);
+
+#else
+       RTE_SET_USED(internals);
+       RTE_SET_USED(q);
+       RTE_SET_USED(tx_io_uring_enabled);
+       RTE_SET_USED(tx_io_uring_sq_size);
+       RTE_SET_USED(tx_io_uring_sq_thread_idle_ms);
+       RTE_SET_USED(tx_io_uring_kq_cores);
+       RTE_SET_USED(tx_io_uring_kq_cores_size);
+#endif /* RTE_NET_AF_PACKET_HAVE_LIBURING */
+}
+
 static uint16_t
 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 {
@@ -346,6 +570,61 @@ tx_ring_status_available(uint32_t tp_status)
        return tp_status == TP_STATUS_AVAILABLE;
 }
 
+/*
+ * Kick the kernel to transmit packets in the ring.
+ *
+ * The sendto() call with NULL buffer is a notification to the kernel that
+ * frames have been queued (marked TP_STATUS_SEND_REQUEST) in the ring.
+ * The kernel will eventually process all queued frames regardless of whether
+ * this kick succeeds or fails. We don't check for errors because:
+ *
+ * 1. Data is already in the ring - the PMD's work is complete
+ * 2. ENOBUFS/EAGAIN just mean "try again later" - kernel will still process 
ring
+ * 3. Fatal errors (EBADF) indicate socket is broken - subsequent ops will fail
+ * 4. Consistent with io_uring path which doesn't check completion status
+ */
+static inline void
+eth_af_packet_tx_kick(struct pkt_tx_queue *pkt_q)
+{
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+       if (pkt_q->uring_enabled) {
+               /* Batch CQ advancement: only advance every sq_size submissions 
to amortize
+                * memory barrier cost (2 load_acquire + 1 store_release). With
+                * IOSQE_CQE_SKIP_SUCCESS, CQ is mostly empty (successes don't 
generate CQEs).
+                * This prevents rare failure CQEs from accumulating while 
minimizing overhead.
+                */
+               if (++pkt_q->uring_sqe_since_cq_advance >= 
pkt_q->uring_ring.sq.ring_entries) {
+                       io_uring_cq_advance(&pkt_q->uring_ring,
+                                       io_uring_cq_ready(&pkt_q->uring_ring));
+                       pkt_q->uring_sqe_since_cq_advance = 0;
+               }
+
+               struct io_uring_sqe *sqe = io_uring_get_sqe(&pkt_q->uring_ring);
+               /* If no sqe available skip this notification and wait for 
kernel to catch up.
+                * Avoid fall back sendto() so we don't block the application 
thread and it is
+                * highly likely the kernel will process pending events and/or 
subsequent TX
+                * will generate new notification.
+                */
+               if (unlikely(sqe == NULL))
+                       return;
+
+               /* Use registered fd (see io_uring_register_files call during 
setup) */
+               io_uring_prep_send(sqe, ETH_AF_PACKET_URING_SOCKFD_INDEX, NULL, 
0, MSG_DONTWAIT);
+               io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE | 
IOSQE_CQE_SKIP_SUCCESS);
+               io_uring_sqe_set_data(sqe, NULL);
+
+               /* With SQPOLL this only enters the kernel to wake a sleeping 
SQ thread;
+                * its full barrier orders the tail store before the 
NEED_WAKEUP check.
+                */
+               io_uring_submit(&pkt_q->uring_ring);
+       } else {
+               sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0);
+       }
+#else
+       sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0);
+#endif
+}
+
 /*
  * Callback to handle sending packets through a real NIC.
  */
@@ -436,19 +715,8 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
 
        rte_pktmbuf_free_bulk(&bufs[0], i);
 
-       /* kick-off transmits */
-       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
-                * are sent.
-                */
-
-               num_tx = 0;
-               num_tx_bytes = 0;
-       }
+       if (likely(num_tx > 0))
+               eth_af_packet_tx_kick(pkt_q);
 
        pkt_q->framenum = framenum;
        pkt_q->tx_pkts += num_tx;
@@ -659,11 +927,22 @@ eth_dev_close(struct rte_eth_dev *dev)
                internals->rx_queue[q].sockfd = -1;
                internals->tx_queue[q].sockfd = -1;
 
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+               if (internals->tx_queue[q].uring_enabled) {
+                       
io_uring_unregister_files(&internals->tx_queue[q].uring_ring);
+                       io_uring_queue_exit(&internals->tx_queue[q].uring_ring);
+                       internals->tx_queue[q].uring_enabled = false;
+               }
+#endif
+
                munmap(internals->rx_queue[q].map,
                        2 * req->tp_block_size * req->tp_block_nr);
                rte_free(internals->rx_queue[q].rd);
                rte_free(internals->tx_queue[q].rd);
        }
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+       rte_free(internals->shared_uring_rings);
+#endif
        rte_free(internals->if_name);
        rte_free(internals->rx_queue);
        rte_free(internals->tx_queue);
@@ -939,6 +1218,12 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                       const char *fanout_mode,
                       unsigned int ignore_outgoing,
                       bool txpollnotrdy,
+                      bool tx_io_uring_enabled,
+                      uint32_t tx_io_uring_sq_size,
+                      uint32_t tx_io_uring_sq_thread_idle_ms,
+                      uint32_t *tx_io_uring_kq_cores,
+                      uint32_t tx_io_uring_kq_cores_size,
+                      uint32_t tx_io_uring_wq_num,
                       struct pmd_internals **internals,
                       struct rte_eth_dev **eth_dev,
                       struct rte_kvargs *kvlist)
@@ -1053,6 +1338,9 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
        if (ignore_outgoing != 0 && nb_queues > 1)
                fanout_arg |= PACKET_FANOUT_FLAG_IGNORE_OUTGOING << 16;
 
+       af_packet_io_uring_share_uring_init(*internals, numa_node, nb_queues,
+                       tx_io_uring_enabled, tx_io_uring_kq_cores_size, 
tx_io_uring_wq_num);
+
        for (q = 0; q < nb_queues; q++) {
                /* Open an AF_PACKET socket for this queue... */
                qsockfd = socket(AF_PACKET, SOCK_RAW, 0);
@@ -1206,6 +1494,10 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                                goto error;
                        }
                }
+
+               af_packet_io_uring_tx_queue_init(*internals, q, 
tx_io_uring_enabled,
+                       tx_io_uring_sq_size, tx_io_uring_sq_thread_idle_ms, 
tx_io_uring_kq_cores,
+                       tx_io_uring_kq_cores_size);
        }
 
        /* reserve an ethdev entry */
@@ -1239,6 +1531,13 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
        if (qsockfd != -1)
                close(qsockfd);
        for (q = 0; q < nb_queues; q++) {
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+               if ((*internals)->tx_queue[q].uring_enabled) {
+                       
io_uring_unregister_files(&(*internals)->tx_queue[q].uring_ring);
+                       
io_uring_queue_exit(&(*internals)->tx_queue[q].uring_ring);
+                       (*internals)->tx_queue[q].uring_enabled = false;
+               }
+#endif
                if ((*internals)->rx_queue[q].map != MAP_FAILED)
                        munmap((*internals)->rx_queue[q].map,
                               2 * req->tp_block_size * req->tp_block_nr);
@@ -1250,6 +1549,9 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                        close((*internals)->rx_queue[q].sockfd);
        }
 free_internals:
+#ifdef RTE_NET_AF_PACKET_HAVE_LIBURING
+       rte_free((*internals)->shared_uring_rings);
+#endif
        rte_free((*internals)->rx_queue);
        rte_free((*internals)->tx_queue);
        rte_free((*internals)->if_name);
@@ -1313,6 +1615,13 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        const char *fanout_mode = NULL;
        unsigned int ignore_outgoing = 0;
        unsigned int txpollnotrdy = DFLT_TX_POLL_NOT_RDY;
+       bool tx_io_uring_enabled = DFLT_TX_IO_URING_ENABLED;
+       uint32_t tx_io_uring_sq_size = DFLT_TX_IO_URING_SQ_SIZE;
+       uint32_t tx_io_uring_sq_thread_idle_ms = 
DFLT_TX_IO_URING_SQ_THREAD_IDLE_MS;
+       uint32_t tx_io_uring_kq_cores[RTE_MAX_QUEUES_PER_PORT] = {};
+       uint32_t tx_io_uring_kq_cores_size = 0;
+       uint32_t tx_io_uring_wq_num = 0;
+       unsigned int uval;
 
        /* do some parameter checking */
        if (*sockfd < 0)
@@ -1387,6 +1696,47 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                                return -1;
                        continue;
                }
+               if (strstr(pair->key, ETH_AF_PACKET_TX_IO_URING_ENABLED_ARG) != 
NULL) {
+                       if (parse_uint(pair->key, pair->value, &uval, 1) < 0)
+                               return -1;
+                       tx_io_uring_enabled = uval != 0;
+                       continue;
+               }
+               if (strstr(pair->key, ETH_AF_PACKET_TX_IO_URING_SQ_SIZE_ARG) != 
NULL) {
+                       if (parse_uint(pair->key, pair->value, &uval,
+                                      ETH_AF_PACKET_URING_SQ_SIZE_MAX) < 0)
+                               return -1;
+                       if (uval == 0) {
+                               PMD_LOG(ERR, "%s: %s must be > 0", name,
+                                       ETH_AF_PACKET_TX_IO_URING_SQ_SIZE_ARG);
+                               return -1;
+                       }
+                       tx_io_uring_sq_size = uval;
+                       continue;
+               }
+               if (strstr(pair->key, 
ETH_AF_PACKET_TX_IO_URING_SQ_THREAD_IDLE_ARG) != NULL) {
+                       if (parse_uint(pair->key, pair->value, &uval, 
UINT32_MAX) < 0)
+                               return -1;
+                       tx_io_uring_sq_thread_idle_ms = uval;
+                       continue;
+               }
+               if (strstr(pair->key, ETH_AF_PACKET_TX_IO_URING_Q_CPU) != NULL) 
{
+                       if (parse_uint(pair->key, pair->value, &uval, 
UINT32_MAX) < 0)
+                               return -1;
+                       if (tx_io_uring_kq_cores_size >= 
RTE_MAX_QUEUES_PER_PORT) {
+                               PMD_LOG(ERR, "%s: too many %s args, max of %d", 
name,
+                                       ETH_AF_PACKET_TX_IO_URING_Q_CPU, 
RTE_MAX_QUEUES_PER_PORT);
+                               return -1;
+                       }
+                       tx_io_uring_kq_cores[tx_io_uring_kq_cores_size++] = 
uval;
+                       continue;
+               }
+               if (strstr(pair->key, ETH_AF_PACKET_TX_IO_URING_WQ_NUM) != 
NULL) {
+                       if (parse_uint(pair->key, pair->value, &uval, 
RTE_MAX_QUEUES_PER_PORT) < 0)
+                               return -1;
+                       tx_io_uring_wq_num = uval;
+                       continue;
+               }
        }
 
        if (framesize > blocksize) {
@@ -1458,6 +1808,19 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        PMD_LOG(DEBUG, "%s:\tignore outgoing %u", name, ignore_outgoing);
        PMD_LOG(DEBUG, "%s:\ttxpollnotrdy %u", name, txpollnotrdy);
 
+       PMD_LOG(INFO, "%s:\t%s %d", name,
+               ETH_AF_PACKET_TX_IO_URING_ENABLED_ARG, tx_io_uring_enabled ? 1 
: 0);
+       PMD_LOG(INFO, "%s:\t%s %" PRIu32, name,
+               ETH_AF_PACKET_TX_IO_URING_SQ_SIZE_ARG, tx_io_uring_sq_size);
+       PMD_LOG(INFO, "%s:\t%s %" PRIu32, name,
+               ETH_AF_PACKET_TX_IO_URING_SQ_THREAD_IDLE_ARG, 
tx_io_uring_sq_thread_idle_ms);
+       for (uint32_t cpu_i = 0; cpu_i < tx_io_uring_kq_cores_size; cpu_i++) {
+               PMD_LOG(INFO, "%s:\t%s[%" PRIu32 "] %" PRIu32, name,
+                       ETH_AF_PACKET_TX_IO_URING_Q_CPU, cpu_i, 
tx_io_uring_kq_cores[cpu_i]);
+       }
+       PMD_LOG(INFO, "%s:\t%s %" PRIu32, name,
+               ETH_AF_PACKET_TX_IO_URING_WQ_NUM, tx_io_uring_wq_num);
+
        if (rte_pmd_init_internals(dev, *sockfd, qpairs,
                                   blocksize, blockcount,
                                   framesize, framecount,
@@ -1465,6 +1828,9 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                                   fanout_mode,
                                   ignore_outgoing,
                                   txpollnotrdy != 0,
+                                  tx_io_uring_enabled, tx_io_uring_sq_size,
+                                  tx_io_uring_sq_thread_idle_ms, 
tx_io_uring_kq_cores,
+                                  tx_io_uring_kq_cores_size, 
tx_io_uring_wq_num,
                                   &internals, &eth_dev,
                                   kvlist) < 0)
                return -1;
@@ -1564,4 +1930,9 @@ RTE_PMD_REGISTER_PARAM_STRING(net_af_packet,
        "qdisc_bypass=<0|1> "
        "fanout_mode=<hash|lb|cpu|rollover|rnd|qm> "
        "ignore_outgoing=<0|1> "
-       "txpollnotrdy=<0|1>");
+       "txpollnotrdy=<0|1> "
+       "tx_io_uring_enabled=<0|1> "
+       "tx_io_uring_sq_size=<int> "
+       "tx_io_uring_sq_thread_idle=<int> "
+       "tx_io_uring_q_cpu=<int> "
+       "tx_io_uring_wq_num=<int>");
-- 
2.54.0 (Apple Git-157)

Reply via email to