Test io_uring Tx through the driver: frames sent with io_uring kicks
arrive on the TAP, also after the SQPOLL thread went idle and with
queues sharing SQPOLL threads; probe fails for io_uring devargs that
can't be honored and when io_uring can't be set up; and creating and
destroying ports leaks no file descriptors. The tests skip when io_uring
SQPOLL is unavailable.

Signed-off-by: Scott Mitchell <[email protected]>
---
Depends-on: series-40254 ("net/af_packet fixes and Tx features")

 app/test/test_pmd_af_packet.c | 357 ++++++++++++++++++++++++++++++++++
 1 file changed, 357 insertions(+)

diff --git a/app/test/test_pmd_af_packet.c b/app/test/test_pmd_af_packet.c
index b42121d8ee..6bac72bedd 100644
--- a/app/test/test_pmd_af_packet.c
+++ b/app/test/test_pmd_af_packet.c
@@ -6,6 +6,7 @@
 #include <fcntl.h>
 #include <inttypes.h>
 #include <sys/ioctl.h>
+#include <sys/resource.h>
 #include <sys/socket.h>
 #include <linux/if.h>
 #include <linux/if_packet.h>
@@ -34,6 +35,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 +110,13 @@ 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 FD_LEAK_MARGIN 32      /* free fds left under RLIMIT_NOFILE */
+#define FD_LEAK_ROUNDS (2 * FD_LEAK_MARGIN)
+
 static struct rte_mempool *mp;
 static uint16_t port_id = RTE_MAX_ETHPORTS;
 static int tap_fd = -1;
@@ -2404,6 +2417,344 @@ test_af_packet_tx_cksum(void)
        return ret;
 }
 
+/*
+ * Test: io_uring devargs are validated, and ones the port can't honor fail 
probe
+ */
+static int
+test_af_packet_invalid_io_uring_args(void)
+{
+       static const char * const bad[] = {
+               "tx_io_uring_enabled=2",
+               "tx_io_uring_enabled=1,tx_io_uring_sq_size=0",
+               "tx_io_uring_enabled=1,tx_io_uring_sq_size=40000",
+               "tx_io_uring_enabled=1,tx_io_uring_sq_size=abc",
+               "tx_io_uring_enabled=1,tx_io_uring_sq_thread_idle=-1",
+               "tx_io_uring_enabled=1,tx_io_uring_q_cpu=-1",
+               "tx_io_uring_enabled=1,tx_io_uring_threads=99999",
+               /* other io_uring devargs without tx_io_uring_enabled=1 */
+               "tx_io_uring_sq_size=64",
+               "tx_io_uring_enabled=0,tx_io_uring_threads=1",
+               /* more SQPOLL threads than queues */
+               "tx_io_uring_enabled=1,tx_io_uring_threads=2",
+               /* more CPUs than SQPOLL threads */
+               "tx_io_uring_enabled=1,tx_io_uring_q_cpu=0,tx_io_uring_q_cpu=0",
+               "qpairs=2,tx_io_uring_enabled=1,tx_io_uring_threads=1,"
+               "tx_io_uring_q_cpu=0,tx_io_uring_q_cpu=0",
+       };
+       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: Whether this kernel lets the process create an io_uring SQPOLL 
ring. */
+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: Send SEQ_TX_BURST frames on each of nb_queues Tx queues of pid and
+ * check that all of them arrive on the (non-blocking) TAP fd.
+ */
+static int
+check_tx_seq_queues(uint16_t pid, uint16_t nb_queues, size_t frame_len)
+{
+       const unsigned int n = nb_queues * SEQ_TX_BURST;
+       struct rte_mbuf *bufs[SEQ_TX_BURST];
+       unsigned int i, nb_seen = 0;
+       uint16_t q;
+       bool *seen;
+       int ret = TEST_FAILED;
+
+       seen = calloc(n, sizeof(*seen));
+       if (seen == NULL)
+               return TEST_FAILED;
+
+       for (q = 0; q < nb_queues; q++) {
+               if (rte_pktmbuf_alloc_bulk(mp, bufs, SEQ_TX_BURST) != 0)
+                       goto out;
+               for (i = 0; i < SEQ_TX_BURST; i++) {
+                       uint8_t *data = (uint8_t *)rte_pktmbuf_append(bufs[i], 
frame_len);
+
+                       if (data == NULL) {
+                               rte_pktmbuf_free_bulk(bufs, SEQ_TX_BURST);
+                               goto out;
+                       }
+                       fill_seq_frame(data, frame_len, &DEV_MAC_TAG, q * 
SEQ_TX_BURST + i);
+               }
+               if (do_tx_burst(pid, q, bufs, SEQ_TX_BURST) != SEQ_TX_BURST) {
+                       printf("TX burst on queue %u was short\n", q);
+                       goto out;
+               }
+       }
+       if (tap_collect_seq(seen, n, &nb_seen, n, frame_len, 
TAP_READ_TIMEOUT_US) != (int)n) {
+               printf("TX: %u of %u frames arrived intact\n", nb_seen, n);
+               goto out;
+       }
+       ret = TEST_SUCCESS;
+out:
+       free(seen);
+       return ret;
+}
+
+/*
+ * Test: A port that asks for io_uring fails probe if it can't have it
+ */
+static int
+test_af_packet_io_uring_setup_fails(void)
+{
+       const char *name = "net_af_packet_uring_fail";
+       char devargs[256];
+       int 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);
+       return TEST_SUCCESS;
+}
+
+/*
+ * Test: TX with io_uring kicks
+ */
+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");
+
+       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");
+
+       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;
+}
+
+/*
+ * Test: Creating and destroying an io_uring port with a shared SQPOLL thread
+ * doesn't leak file descriptors. RLIMIT_NOFILE leaves FD_LEAK_MARGIN fds free,
+ * so leaking even one per round makes probe fail before the last round.
+ */
+static int
+test_af_packet_io_uring_create_destroy(void)
+{
+       const char *name = "net_af_packet_uring_cd";
+       struct rlimit saved, lim;
+       char devargs[256];
+       int fd, i, rc, ret = TEST_SUCCESS;
+       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_threads=1",
+                TAP_DEV_NAME);
+
+       /* the next fd is the lowest free one; allow FD_LEAK_MARGIN above it */
+       fd = open("/dev/null", O_RDONLY);
+       TEST_ASSERT(fd >= 0, "open /dev/null failed");
+       close(fd);
+       TEST_ASSERT(getrlimit(RLIMIT_NOFILE, &saved) == 0, "getrlimit failed");
+       lim = saved;
+       lim.rlim_cur = RTE_MIN((rlim_t)fd + FD_LEAK_MARGIN, saved.rlim_cur);
+       TEST_ASSERT(setrlimit(RLIMIT_NOFILE, &lim) == 0, "setrlimit failed");
+
+       for (i = 0; i < FD_LEAK_ROUNDS; i++) {
+               if (create_af_packet_port(name, devargs, &pid) != 0) {
+                       printf("Port creation failed in round %d: fd leak?\n", 
i);
+                       ret = TEST_FAILED;
+                       break;
+               }
+               rc = configure_af_packet_port(pid, 2, 2);
+               destroy_test_port(name, pid);
+               if (rc != 0) {
+                       printf("Port configuration failed in round %d\n", i);
+                       ret = TEST_FAILED;
+                       break;
+               }
+       }
+
+       setrlimit(RLIMIT_NOFILE, &saved);
+       return ret;
+}
+
+/*
+ * Test: With queues sharing SQPOLL threads (tx_io_uring_threads below qpairs),
+ * frames sent on every queue arrive.
+ */
+static int
+test_af_packet_io_uring_shared_sqpoll(void)
+{
+       static const unsigned int threads[] = { 1, 2 };
+       const char *name = "net_af_packet_uring_shared";
+       char devargs[256];
+       unsigned int i;
+       uint16_t pid;
+       int flags, ret = TEST_SUCCESS;
+
+       if (!tap_created) {
+               printf("SKIPPED: TAP interface not available (need root)\n");
+               return TEST_SKIPPED;
+       }
+       if (!io_uring_sqpoll_testable())
+               return TEST_SKIPPED;
+
+       flags = fcntl(tap_fd, F_GETFL, 0);
+       fcntl(tap_fd, F_SETFL, flags | O_NONBLOCK);
+
+       for (i = 0; i < RTE_DIM(threads) && ret == TEST_SUCCESS; i++) {
+               snprintf(devargs, sizeof(devargs),
+                        
"iface=%s,qpairs=%d,tx_io_uring_enabled=1,tx_io_uring_threads=%u",
+                        TAP_DEV_NAME, SQPOLL_QUEUES, threads[i]);
+               ret = create_af_packet_port(name, devargs, &pid);
+               if (ret != 0) {
+                       printf("Failed to create port with %s\n", devargs);
+                       ret = TEST_FAILED;
+                       break;
+               }
+               if (configure_af_packet_port(pid, SQPOLL_QUEUES, SQPOLL_QUEUES) 
!= 0 ||
+                   rte_eth_dev_start(pid) != 0) {
+                       ret = TEST_FAILED;
+               } else {
+                       tap_drain();
+                       ret = check_tx_seq_queues(pid, SQPOLL_QUEUES, 
LAYOUT_FRAME_LEN);
+               }
+               destroy_test_port(name, pid);
+       }
+
+       fcntl(tap_fd, F_SETFL, flags);
+       return ret;
+}
+
 static struct unit_test_suite af_packet_test_suite = {
        .suite_name = "AF_PACKET PMD Unit Test Suite",
        .setup = test_af_packet_setup,
@@ -2434,6 +2785,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),
-- 
2.54.0 (Apple Git-157)

Reply via email to