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 DPDK built with liburing (RTE_HAS_LIBURING). If
tx_io_uring_enabled is set and the io_uring can't be set up, probe
fails. A kick that finds the submission queue full uses sendto(), so
frames queued since the last kick aren't left waiting for the next
burst.

Signed-off-by: Scott Mitchell <[email protected]>
---
 app/test/test_pmd_af_packet.c             | 391 ++++++++++++++++++++++
 doc/guides/nics/af_packet.rst             |  24 ++
 doc/guides/rel_notes/release_27_03.rst    |   1 +
 drivers/net/af_packet/meson.build         |   7 +
 drivers/net/af_packet/rte_eth_af_packet.c | 327 +++++++++++++++++-
 5 files changed, 746 insertions(+), 4 deletions(-)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index b42121d8ee..3a0597a7a6 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>
@@ -34,6 +36,11 @@
 
 #include "test.h"
 
+#ifdef RTE_HAS_LIBURING
+#include <sys/syscall.h>
+#include <linux/io_uring.h>
+#endif
+
 #ifndef RTE_EXEC_ENV_LINUX
 static int
 test_pmd_af_packet(void)
@@ -104,6 +111,12 @@ struct tag_count {
 #define CKSUM_BAD 0xdead
 #define CKSUM_TCI 0x123
 
+/* io_uring tests */
+#define IDLE_WAKE_ROUNDS 40
+#define IDLE_WAKE_GAP_MS 25    /* > 2 jiffies at HZ=100, so an idle SQ thread 
sleeps */
+#define SQPOLL_QUEUES 4
+#define SQPOLL_STABLE_SAMPLES 5        /* 10 ms apart */
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -2404,6 +2417,378 @@ 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;
+}
+
+/* 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 hold at want for
+ * SQPOLL_STABLE_SAMPLES samples. Returns want, or the last other count.
+ */
+static int
+wait_sqpoll_threads(int want)
+{
+       int i, n, stable = 0, other = -1;
+
+       for (i = 0; i < 100; i++) {
+               n = count_sqpoll_threads();
+               if (n == want) {
+                       if (++stable >= SQPOLL_STABLE_SAMPLES)
+                               return want;
+               } else {
+                       stable = 0;
+                       other = n;
+               }
+               rte_delay_ms(10);
+       }
+       return other;
+}
+
+/* Helper: Whether the io_uring SQPOLL tests can run here; prints why not. */
+static bool
+io_uring_sqpoll_testable(void)
+{
+#ifdef RTE_HAS_LIBURING
+       struct io_uring_params p = { .flags = IORING_SETUP_SQPOLL };
+       long fd;
+
+       fd = syscall(__NR_io_uring_setup, 1, &p);
+       if (fd < 0) {
+               printf("SKIPPED: io_uring SQPOLL unavailable: %s\n", 
strerror(errno));
+               return false;
+       }
+       close((int)fd);
+       return true;
+#else
+       printf("SKIPPED: af_packet built without liburing\n");
+       return false;
+#endif
+}
+
+/* Helper: Fail if an io_uring port runs no SQPOLL thread. */
+static int
+check_sqpoll_active(int want)
+{
+       int n = wait_sqpoll_threads(want);
+
+       if (n < 1) {
+               printf("No io_uring SQPOLL thread (count %d)\n", n);
+               return TEST_FAILED;
+       }
+       return TEST_SUCCESS;
+}
+
+/*
+ * Test: TX with io_uring kicks
+ */
+/*
+ * Test: A port that asks for io_uring fails probe if it can't have it, and
+ * leaves no SQPOLL thread behind.
+ */
+static int
+test_af_packet_io_uring_setup_fails(void)
+{
+       const char *name = "net_af_packet_uring_fail";
+       char devargs[256];
+       int n, ret;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+
+#ifdef RTE_HAS_LIBURING
+       if (!io_uring_sqpoll_testable())
+               return TEST_SKIPPED;
+       /* the kernel rejects an SQPOLL CPU at or above nr_cpu_ids */
+       snprintf(devargs, sizeof(devargs),
+                "iface=%s,tx_io_uring_enabled=1,tx_io_uring_q_cpu=1000000", 
TAP_DEV_NAME);
+#else
+       snprintf(devargs, sizeof(devargs), "iface=%s,tx_io_uring_enabled=1", 
TAP_DEV_NAME);
+#endif
+       ret = rte_vdev_init(name, devargs);
+       if (ret == 0)
+               rte_vdev_uninit(name);
+       TEST_ASSERT(ret != 0, "Expected probe failure with %s", devargs);
+
+       n = wait_sqpoll_threads(0);
+       TEST_ASSERT(n == 0, "%d SQPOLL threads left after failed probe", n);
+       return TEST_SUCCESS;
+}
+
+static int
+test_af_packet_io_uring_tx(void)
+{
+       const char *name = "net_af_packet_uring";
+       char devargs[256];
+       unsigned int framecnt;
+       uint16_t pid;
+       int flags, ret;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+
+       if (!io_uring_sqpoll_testable())
+               return TEST_SKIPPED;
+
+       framecnt = layout_devargs(devargs, sizeof(devargs), 
",tx_io_uring_enabled=1");
+       ret = create_test_port(name, devargs, 0, &pid);
+       TEST_ASSERT(ret == 0, "Failed to create port");
+
+       ret = check_sqpoll_active(1);
+       if (ret != TEST_SUCCESS) {
+               destroy_test_port(name, pid);
+               return ret;
+       }
+
+       flags = fcntl(tap_fd, F_GETFL, 0);
+       fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK);
+       tap_drain();
+
+       ret = check_tx_seq(pid, 3 * framecnt, 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 an IDLE_WAKE_GAP_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;
+       }
+
+       if (!io_uring_sqpoll_testable())
+               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");
+
+       ret = check_sqpoll_active(1);
+       if (ret != TEST_SUCCESS) {
+               destroy_test_port(name, pid);
+               return ret;
+       }
+
+       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(IDLE_WAKE_GAP_MS);
+               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;
+       }
+
+       if (!io_uring_sqpoll_testable())
+               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);
+               /* Without io_uring rings there would be nothing to leak */
+               if (i == 0 && check_sqpoll_active(1) != TEST_SUCCESS) {
+                       destroy_test_port(name, pid);
+                       return TEST_FAILED;
+               }
+               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;
+}
+
+/*
+ * 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;
+       }
+       if (!io_uring_sqpoll_testable())
+               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);
+
+               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,
@@ -2434,6 +2819,12 @@ static struct unit_test_suite af_packet_test_suite = {
                TEST_CASE(test_af_packet_tx_mbuf_accounting),
                TEST_CASE(test_af_packet_tx_ring_full),
                TEST_CASE(test_af_packet_tx_cksum),
+               TEST_CASE(test_af_packet_invalid_io_uring_args),
+               TEST_CASE(test_af_packet_io_uring_setup_fails),
+               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 93c307175a..c3cb692703 100644
--- a/doc/guides/nics/af_packet.rst
+++ b/doc/guides/nics/af_packet.rst
@@ -33,6 +33,26 @@ Some of these, in turn, will be used to configure the 
PACKET_MMAP settings.
     of 16B, at least 64B);
 *   ``framecnt`` - PACKET_MMAP frame count (optional, default 512; Note: 
multiple
     of ``blocksz`` / ``framesz``).
+*   ``tx_io_uring_enabled`` - Use an io_uring SQPOLL thread to notify the 
kernel
+    on Tx. Needs DPDK built with liburing 2.4 or later; probe fails if io_uring
+    can't be set up (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; 0 selects the kernel default of 1 second
+    (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. With ``tx_io_uring_wq_num``, values apply to
+    SQPOLL threads instead: value i pins the thread shared by queues whose 
index
+    modulo ``tx_io_uring_wq_num`` is i (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>`_.
@@ -96,3 +116,7 @@ Features and Limitations
    application.
 *  The PMD will add the kernel packet timestamp with nanoseconds resolution and
    UNIX origo, i.e. time since 1-JAN-1970 UTC, if 
``RTE_ETH_RX_OFFLOAD_TIMESTAMP`` is enabled.
+*  With ``tx_io_uring_enabled``, the kernel transmits from the SQPOLL thread, 
and the
+   CPU it runs on selects the interface Tx queue. Pin SQPOLL threads with
+   ``tx_io_uring_q_cpu`` so a queue's packets are not reordered across 
interface
+   Tx queues. Queues sharing an SQPOLL thread also share an interface Tx queue.
diff --git a/doc/guides/rel_notes/release_27_03.rst 
b/doc/guides/rel_notes/release_27_03.rst
index dd9e8a57bb..88cc6a67d8 100644
--- a/doc/guides/rel_notes/release_27_03.rst
+++ b/doc/guides/rel_notes/release_27_03.rst
@@ -58,6 +58,7 @@ New Features
 * **Updated af_packet net driver.**
 
   * Added software TX checksum offload for IPv4, UDP and TCP.
+  * 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..7546bf31ed 100644
--- a/drivers/net/af_packet/meson.build
+++ b/drivers/net/af_packet/meson.build
@@ -5,5 +5,12 @@ if not is_linux
     build = false
     reason = 'only supported on Linux'
 endif
+
+deps += ['ethdev']
+
+if dpdk_conf.has('RTE_HAS_LIBURING')
+    ext_deps += iouring_dep
+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 bf847ab602..8d5aa63f11 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -20,6 +20,11 @@
 #include <bus_vdev_driver.h>
 
 #include <ctype.h>
+#ifdef RTE_HAS_LIBURING
+#include <liburing.h>
+#endif
+struct io_uring;
+
 #include <errno.h>
 #include <limits.h>
 #include <linux/if_ether.h>
@@ -53,9 +58,21 @@
 #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_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_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
 
 /* Rx data starts at tp_mac, past the header aligned as in the kernel's 
tpacket_rcv() */
 #define ETH_AF_PACKET_RX_FRAME_OVERHEAD \
@@ -97,6 +114,13 @@ struct __rte_cache_aligned pkt_tx_queue {
        unsigned int framecount;
        unsigned int framenum;
 
+#ifdef RTE_HAS_LIBURING
+       struct io_uring uring_ring;
+       bool uring_enabled;
+       uint32_t uring_sqe_since_cq_advance;
+       uint8_t uring_sqe_flags;
+#endif
+
        volatile unsigned long tx_pkts;
        volatile unsigned long err_pkts;
        volatile unsigned long tx_bytes;
@@ -115,6 +139,18 @@ struct pmd_internals {
        struct pkt_tx_queue *tx_queue;
        uint8_t vlan_strip;
        uint8_t timestamp_offloading;
+       struct io_uring **shared_uring_rings;
+       uint32_t shared_uring_count;
+};
+
+/* io_uring Tx kick devargs */
+struct af_packet_io_uring_args {
+       bool enabled;
+       uint32_t sq_size;
+       uint32_t sq_thread_idle_ms;
+       uint32_t kq_cores[RTE_MAX_QUEUES_PER_PORT];
+       uint32_t kq_cores_size;
+       uint32_t wq_num;
 };
 
 static const char *valid_arguments[] = {
@@ -126,6 +162,11 @@ 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_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
 };
 
@@ -199,6 +240,150 @@ tpacket_write_status(struct tpacket2_hdr *ppd, uint32_t 
status)
                        rte_memory_order_release);
 }
 
+#ifdef RTE_HAS_LIBURING
+/*
+ * Create a ring. Kernels before 6.0 reject IORING_SETUP_SINGLE_ISSUER with
+ * -EINVAL; it is only an optimization, so retry without it.
+ */
+static int
+af_packet_io_uring_init(struct io_uring *ring, uint32_t entries,
+                       struct io_uring_params *params)
+{
+       struct io_uring_params p = *params;
+       int rc;
+
+       rc = io_uring_queue_init_params(entries, ring, &p);
+       if (rc == -EINVAL && (params->flags & IORING_SETUP_SINGLE_ISSUER)) {
+               params->flags &= ~IORING_SETUP_SINGLE_ISSUER;
+               p = *params;
+               rc = io_uring_queue_init_params(entries, ring, &p);
+       }
+       if (rc == 0)
+               *params = p;
+       return rc;
+}
+
+/* Flags for a ring that creates its own SQPOLL thread, pinned if a CPU is 
configured */
+static void
+af_packet_io_uring_own_thread(struct io_uring_params *params, uint32_t cpu_idx,
+                             const struct af_packet_io_uring_args *args)
+{
+       if (cpu_idx < args->kq_cores_size) {
+               params->flags |= IORING_SETUP_SQ_AFF;
+               params->sq_thread_cpu = args->kq_cores[cpu_idx];
+       }
+       params->flags |= IORING_SETUP_SINGLE_ISSUER;
+}
+#endif /* RTE_HAS_LIBURING */
+
+/* Prepare SQPOLL thread sharing. Fails if io_uring is requested but not built 
in. */
+static int
+af_packet_io_uring_share_init(struct pmd_internals *internals __rte_unused,
+                             unsigned int numa_node __rte_unused,
+                             const struct af_packet_io_uring_args *args)
+{
+       if (!args->enabled)
+               return 0;
+#ifdef RTE_HAS_LIBURING
+       if (args->wq_num == 0)
+               return 0;
+       if (args->kq_cores_size > args->wq_num)
+               PMD_LOG(WARNING, "only the first %u %s values are used",
+                       args->wq_num, ETH_AF_PACKET_TX_IO_URING_Q_CPU);
+
+       /* Rings attach to a group's SQPOLL thread with IORING_SETUP_ATTACH_WQ 
*/
+       internals->shared_uring_rings = 
rte_calloc_socket("af_packet_shared_uring",
+                       args->wq_num, sizeof(*internals->shared_uring_rings), 
0, numa_node);
+       if (internals->shared_uring_rings == NULL)
+               return -ENOMEM;
+       internals->shared_uring_count = args->wq_num;
+       return 0;
+#else
+       PMD_LOG(ERR, "%s=1 needs liburing, which this build lacks",
+               ETH_AF_PACKET_TX_IO_URING_ENABLED_ARG);
+       return -ENOTSUP;
+#endif
+}
+
+/* Create the io_uring that kicks Tx queue q. Fails if it can't be set up. */
+static int
+af_packet_io_uring_tx_queue_init(struct pmd_internals *internals __rte_unused,
+                                unsigned int q __rte_unused,
+                                const struct af_packet_io_uring_args *args)
+{
+       if (!args->enabled)
+               return 0;
+#ifdef RTE_HAS_LIBURING
+       struct pkt_tx_queue *tx_queue = &internals->tx_queue[q];
+       struct io_uring_params params = {
+               .sq_thread_idle = args->sq_thread_idle_ms,
+               .flags = IORING_SETUP_SQPOLL,
+       };
+       /* q_cpu is indexed by SQPOLL thread group when threads are shared */
+       uint32_t ring_idx = internals->shared_uring_rings ? q % 
internals->shared_uring_count : 0;
+       uint32_t cpu_idx = internals->shared_uring_rings ? ring_idx : q;
+       bool attach = internals->shared_uring_rings && ring_idx < q &&
+               internals->shared_uring_rings[ring_idx];
+       int rc;
+
+       if (attach) {
+               /* Share the group's SQPOLL thread; the ring's memory and 
lifetime stay per queue */
+               params.flags |= IORING_SETUP_ATTACH_WQ;
+               params.wq_fd = internals->shared_uring_rings[ring_idx]->ring_fd;
+       } else {
+               af_packet_io_uring_own_thread(&params, cpu_idx, args);
+       }
+
+       rc = af_packet_io_uring_init(&tx_queue->uring_ring, args->sq_size, 
&params);
+       if (rc < 0 && attach) {
+               PMD_LOG(NOTICE, "tx_queue %u: can't share SQPOLL thread: %s", 
q, strerror(-rc));
+               attach = false;
+               params.flags &= ~IORING_SETUP_ATTACH_WQ;
+               params.wq_fd = 0;
+               af_packet_io_uring_own_thread(&params, cpu_idx, args);
+               rc = af_packet_io_uring_init(&tx_queue->uring_ring, 
args->sq_size, &params);
+       }
+       if (rc < 0) {
+               PMD_LOG(ERR, "tx_queue %u: io_uring setup failed: %s", q, 
strerror(-rc));
+               return rc;
+       }
+
+       /* The kick needs IORING_OP_SEND (Linux 5.6); skipping its CQEs (5.17) 
is optional */
+       struct io_uring_probe *probe = 
io_uring_get_probe_ring(&tx_queue->uring_ring);
+       bool send_ok = probe != NULL && io_uring_opcode_supported(probe, 
IORING_OP_SEND);
+
+       if (probe != NULL)
+               io_uring_free_probe(probe);
+       if (!send_ok) {
+               PMD_LOG(ERR, "tx_queue %u: kernel lacks io_uring send", q);
+               io_uring_queue_exit(&tx_queue->uring_ring);
+               return -ENOTSUP;
+       }
+       tx_queue->uring_sqe_flags = IOSQE_FIXED_FILE;
+       if (params.features & IORING_FEAT_CQE_SKIP)
+               tx_queue->uring_sqe_flags |= IOSQE_CQE_SKIP_SUCCESS;
+
+       /* 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(ERR, "tx_queue %u: io_uring file registration failed: 
%s",
+                       q, strerror(-rc));
+               io_uring_queue_exit(&tx_queue->uring_ring);
+               return rc;
+       }
+
+       tx_queue->uring_enabled = true;
+       tx_queue->uring_sqe_since_cq_advance = 0;
+       if (!attach && internals->shared_uring_rings && 
!internals->shared_uring_rings[ring_idx])
+               internals->shared_uring_rings[ring_idx] = &tx_queue->uring_ring;
+       PMD_LOG(DEBUG, "tx_queue %u: io_uring SQPOLL%s, flags 0x%x",
+               q, attach ? " shared" : "", params.flags);
+       return 0;
+#else
+       return -ENOTSUP;
+#endif
+}
+
 static uint16_t
 eth_af_packet_rx(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
 {
@@ -422,6 +607,52 @@ af_packet_tx_cksum(uint8_t *data, const struct rte_mbuf *m)
        return true;
 }
 
+/*
+ * Kick the kernel to process frames in SEND_REQUEST. Returns false if the
+ * kernel rejected a synchronous kick with an error other than ENOBUFS/EAGAIN.
+ * io_uring kick errors aren't observed; the frames are in the ring either way.
+ */
+static inline bool
+eth_af_packet_tx_kick(struct pkt_tx_queue *pkt_q)
+{
+#ifdef RTE_HAS_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 only failures post CQEs; without it 
every kick does.
+                * Either way the CQ stays within its default 2 * sq_size 
entries.
+                */
+               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 (likely(sqe != NULL)) {
+                       /* 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, pkt_q->uring_sqe_flags);
+                       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);
+                       return true;
+               }
+               /*
+                * SQ full: the kernel publishes the SQ head only after issuing 
a whole
+                * batch, so a pending kick may already have scanned the ring. 
Kick
+                * synchronously so frames queued since then aren't left behind.
+                */
+       }
+#endif
+       return sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) != -1 ||
+               errno == ENOBUFS || errno == EAGAIN;
+}
+
 /*
  * Callback to handle sending packets through a real NIC.
  */
@@ -490,14 +721,12 @@ eth_af_packet_tx(void *queue, struct rte_mbuf **bufs, 
uint16_t nb_pkts)
        rte_pktmbuf_free_bulk(bufs, i);
 
        /* kick-off transmits, even if nothing new was queued */
-       if (unlikely(sendto(pkt_q->sockfd, NULL, 0, MSG_DONTWAIT, NULL, 0) == 
-1 &&
-                    errno != ENOBUFS && errno != EAGAIN)) {
+       if (unlikely(!eth_af_packet_tx_kick(pkt_q))) {
                /*
                 * 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;
        }
@@ -708,11 +937,20 @@ eth_dev_close(struct rte_eth_dev *dev)
                internals->rx_queue[q].sockfd = -1;
                internals->tx_queue[q].sockfd = -1;
 
+#ifdef RTE_HAS_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);
        }
+       rte_free(internals->shared_uring_rings);
        rte_free(internals->if_name);
        rte_free(internals->rx_queue);
        rte_free(internals->tx_queue);
@@ -996,6 +1234,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                       unsigned int qdisc_bypass,
                       const char *fanout_mode,
                       unsigned int ignore_outgoing,
+                      const struct af_packet_io_uring_args *uring_args,
                       struct pmd_internals **internals,
                       struct rte_eth_dev **eth_dev,
                       struct rte_kvargs *kvlist)
@@ -1110,6 +1349,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;
 
+       if (af_packet_io_uring_share_init(*internals, numa_node, uring_args) < 
0)
+               goto error;
+
        for (q = 0; q < nb_queues; q++) {
                /* Open an AF_PACKET socket for this queue... */
                qsockfd = socket(AF_PACKET, SOCK_RAW, 0);
@@ -1247,6 +1489,9 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                                goto error;
                        }
                }
+
+               if (af_packet_io_uring_tx_queue_init(*internals, q, uring_args) 
< 0)
+                       goto error;
        }
 
        /* reserve an ethdev entry */
@@ -1280,6 +1525,13 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
        if (qsockfd != -1)
                close(qsockfd);
        for (q = 0; q < nb_queues; q++) {
+#ifdef RTE_HAS_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);
@@ -1291,6 +1543,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                        close((*internals)->rx_queue[q].sockfd);
        }
 free_internals:
+       rte_free((*internals)->shared_uring_rings);
        rte_free((*internals)->rx_queue);
        rte_free((*internals)->tx_queue);
        rte_free((*internals)->if_name);
@@ -1353,6 +1606,12 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
        unsigned int qdisc_bypass = 1;
        const char *fanout_mode = NULL;
        unsigned int ignore_outgoing = 0;
+       struct af_packet_io_uring_args uring_args = {
+               .enabled = DFLT_TX_IO_URING_ENABLED,
+               .sq_size = DFLT_TX_IO_URING_SQ_SIZE,
+               .sq_thread_idle_ms = DFLT_TX_IO_URING_SQ_THREAD_IDLE_MS,
+       };
+       unsigned int uval;
 
        /* do some parameter checking */
        if (*sockfd < 0)
@@ -1424,6 +1683,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;
+                       uring_args.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;
+                       }
+                       uring_args.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;
+                       uring_args.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 (uring_args.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;
+                       }
+                       uring_args.kq_cores[uring_args.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;
+                       uring_args.wq_num = uval;
+                       continue;
+               }
        }
 
        if (framesize > blocksize) {
@@ -1458,12 +1758,26 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
                PMD_LOG(DEBUG, "%s:\tfanout mode %s", name, "default 
PACKET_FANOUT_HASH");
        PMD_LOG(DEBUG, "%s:\tignore outgoing %u", name, ignore_outgoing);
 
+       PMD_LOG(INFO, "%s:\t%s %d", name,
+               ETH_AF_PACKET_TX_IO_URING_ENABLED_ARG, uring_args.enabled ? 1 : 
0);
+       PMD_LOG(INFO, "%s:\t%s %" PRIu32, name,
+               ETH_AF_PACKET_TX_IO_URING_SQ_SIZE_ARG, uring_args.sq_size);
+       PMD_LOG(INFO, "%s:\t%s %" PRIu32, name,
+               ETH_AF_PACKET_TX_IO_URING_SQ_THREAD_IDLE_ARG, 
uring_args.sq_thread_idle_ms);
+       for (uint32_t cpu_i = 0; cpu_i < uring_args.kq_cores_size; cpu_i++) {
+               PMD_LOG(INFO, "%s:\t%s[%" PRIu32 "] %" PRIu32, name,
+                       ETH_AF_PACKET_TX_IO_URING_Q_CPU, cpu_i, 
uring_args.kq_cores[cpu_i]);
+       }
+       PMD_LOG(INFO, "%s:\t%s %" PRIu32, name,
+               ETH_AF_PACKET_TX_IO_URING_WQ_NUM, uring_args.wq_num);
+
        if (rte_pmd_init_internals(dev, *sockfd, qpairs,
                                   blocksize, blockcount,
                                   framesize, framecount,
                                   qdisc_bypass,
                                   fanout_mode,
                                   ignore_outgoing,
+                                  &uring_args,
                                   &internals, &eth_dev,
                                   kvlist) < 0)
                return -1;
@@ -1562,4 +1876,9 @@ 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> "
+       "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