DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Isaac Boukris <iboukris@gmail.com>
To: Maxime Coquelin <maxime.coquelin@redhat.com>,
	Chenbo Xia <chenbox@nvidia.com>
Cc: dev@dpdk.org, David Marchand <david.marchand@redhat.com>,
	Aman Singh <aman.deep.singh@intel.com>,
	Luca Vizzarro <luca.vizzarro@arm.com>,
	Patrick Robb <patrickrobb1997@gmail.com>,
	Isaac Boukris <iboukris@gmail.com>
Subject: [PATCH v2 2/5] test/virtio_user: add unit test of Rx interrupts
Date: Tue, 17 Nov 2026 23:59:58 +0200	[thread overview]
Message-ID: <20261117220001.965310-3-iboukris@gmail.com> (raw)
In-Reply-To: <20261117220001.965310-1-iboukris@gmail.com>

Add a test suite for the virtio PMD on a virtio-user port backed by
vhost-net and a TAP interface, modeled on the test suite of the TAP
PMD. It needs no VM and no PCI device. It is skipped where the port
cannot be created (no root, no /dev/vhost-net).

The first case configures the port with Rx interrupts and an Rx
offload. It enables the interrupt of the Rx queue, sends a frame from
the kernel and sleeps on the interrupt. The frame must end the sleep.
Then it does the same after a second configure.

The port has two queue pairs and the test configures one Rx queue, so
the port has more interrupt eventfds than Rx queues. These eventfds
are the backend's call fds, and the driver must not release them. If
it does when it initializes the device again, the test fails.

  DPDK_TEST=virtio_user_pmd_autotest ./build/app/dpdk-test --no-huge

Signed-off-by: Isaac Boukris <iboukris@gmail.com>
---
 MAINTAINERS                     |   1 +
 app/test/meson.build            |   1 +
 app/test/test_pmd_virtio_user.c | 296 ++++++++++++++++++++++++++++++++
 3 files changed, 298 insertions(+)
 create mode 100644 app/test/test_pmd_virtio_user.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 8fcb4ef5fa..36dd253f36 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1106,6 +1106,7 @@ T: git://dpdk.org/next/dpdk-next-virtio
 F: drivers/net/virtio/
 F: doc/guides/nics/virtio.rst
 F: doc/guides/nics/features/virtio*.ini
+F: app/test/test_pmd_virtio_user.c
 
 Wind River AVP
 M: Steven Webster <steven.webster@windriver.com>
diff --git a/app/test/meson.build b/app/test/meson.build
index 2f1cd817cd..e480353885 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -155,6 +155,7 @@ source_file_deps = {
     'test_pmd_ring.c': ['net_ring', 'ethdev', 'bus_vdev'],
     'test_pmd_ring_perf.c': ['ethdev', 'net_ring', 'bus_vdev'],
     'test_pmd_tap.c': ['ethdev', 'net_tap', 'bus_vdev'],
+    'test_pmd_virtio_user.c': ['ethdev', 'net_virtio', 'bus_vdev'],
     'test_pmu.c': ['pmu'],
     'test_power.c': ['power', 'power_acpi', 'power_kvm_vm', 'power_intel_pstate',
         'power_amd_pstate', 'power_cppc'],
diff --git a/app/test/test_pmd_virtio_user.c b/app/test/test_pmd_virtio_user.c
new file mode 100644
index 0000000000..d62ce000fb
--- /dev/null
+++ b/app/test/test_pmd_virtio_user.c
@@ -0,0 +1,296 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2026 Isaac Boukris
+ */
+
+/*
+ * Tests of the virtio PMD on a virtio-user port backed by vhost-net and a TAP
+ * interface. They need no VM and no PCI device. The kernel's end is a packet
+ * socket on the TAP interface.
+ */
+
+#include "test.h"
+
+#include <stdio.h>
+
+#ifndef RTE_EXEC_ENV_LINUX
+
+static int
+test_pmd_virtio_user(void)
+{
+	printf("virtio-user with vhost-net is only available on Linux, skipping test\n");
+	return TEST_SKIPPED;
+}
+
+#else /* RTE_EXEC_ENV_LINUX */
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/if_packet.h>
+#include <linux/virtio_net.h>
+
+#include <rte_bus_vdev.h>
+#include <rte_cycles.h>
+#include <rte_epoll.h>
+#include <rte_ethdev.h>
+#include <rte_ether.h>
+#include <rte_mbuf.h>
+
+#define VDEV_NAME "net_virtio_user_test"
+#define TAP_NAME "dtap_vu_test"
+#define RING_SIZE 256
+#define NB_MBUF 1024
+#define BURST 32
+#define WAIT_MS 1000
+/* The test frames' EtherType (IEEE 802 local experimental) */
+#define TEST_ETHER_TYPE 0x88b5
+
+static struct rte_mempool *mp;
+static uint16_t port_id = RTE_MAX_ETHPORTS;
+static int ksock = -1; /* the kernel's end, each frame follows a virtio_net_hdr */
+static bool started;
+
+static int
+port_stop(void)
+{
+	int ret;
+
+	if (!started)
+		return TEST_SUCCESS;
+	started = false;
+	ret = rte_eth_dev_stop(port_id);
+	TEST_ASSERT_SUCCESS(ret, "stop failed: %s", rte_strerror(-ret));
+
+	return TEST_SUCCESS;
+}
+
+/* Configure the port and start it. A failed case may have left it started. */
+static int
+port_start(const struct rte_eth_conf *conf)
+{
+	int ret;
+
+	TEST_ASSERT_SUCCESS(port_stop(), "port stop failed");
+	ret = rte_eth_dev_configure(port_id, 1, 1, conf);
+	TEST_ASSERT_SUCCESS(ret, "configure failed: %s", rte_strerror(-ret));
+	ret = rte_eth_rx_queue_setup(port_id, 0, RING_SIZE, SOCKET_ID_ANY, NULL, mp);
+	TEST_ASSERT_SUCCESS(ret, "Rx queue setup failed: %s", rte_strerror(-ret));
+	ret = rte_eth_tx_queue_setup(port_id, 0, RING_SIZE, SOCKET_ID_ANY, NULL);
+	TEST_ASSERT_SUCCESS(ret, "Tx queue setup failed: %s", rte_strerror(-ret));
+	ret = rte_eth_dev_start(port_id);
+	TEST_ASSERT_SUCCESS(ret, "start failed: %s", rte_strerror(-ret));
+	started = true;
+
+	return TEST_SUCCESS;
+}
+
+/* A frame from the kernel's end to the port, recognized by its EtherType. */
+static int
+kernel_send(void)
+{
+	struct {
+		struct virtio_net_hdr vnet; /* no offload */
+		struct rte_ether_hdr eth;
+		uint8_t payload[RTE_ETHER_MIN_LEN];
+	} frame;
+	ssize_t n;
+
+	memset(&frame, 0, sizeof(frame));
+	memset(&frame.eth.dst_addr, 0xff, RTE_ETHER_ADDR_LEN);
+	frame.eth.src_addr.addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
+	frame.eth.ether_type = rte_cpu_to_be_16(TEST_ETHER_TYPE);
+	n = send(ksock, &frame, sizeof(frame), 0);
+	TEST_ASSERT(n == (ssize_t)sizeof(frame), "send on %s failed: %s", TAP_NAME,
+		    strerror(errno));
+
+	return TEST_SUCCESS;
+}
+
+/* Receive what the queue holds. Return true if it holds the frame of kernel_send(). */
+static bool
+port_receive(void)
+{
+	struct rte_mbuf *pkts[BURST];
+	bool found = false;
+	uint16_t i, n;
+
+	while ((n = rte_eth_rx_burst(port_id, 0, pkts, BURST)) != 0) {
+		for (i = 0; i < n; i++) {
+			const struct rte_ether_hdr *eth =
+				rte_pktmbuf_mtod(pkts[i], const struct rte_ether_hdr *);
+
+			if (eth->ether_type == rte_cpu_to_be_16(TEST_ETHER_TYPE))
+				found = true;
+		}
+		rte_pktmbuf_free_bulk(pkts, n);
+	}
+
+	return found;
+}
+
+/* Sleep on the Rx queue's interrupt. A frame from the kernel must end the sleep. */
+static int
+rx_intr_wakes(void)
+{
+	uint64_t deadline = rte_get_timer_cycles() + rte_get_timer_hz() * WAIT_MS / 1000;
+	struct rte_epoll_event event;
+	bool found;
+	int ret;
+
+	ret = rte_eth_dev_rx_intr_ctl_q(port_id, 0, RTE_EPOLL_PER_THREAD, RTE_INTR_EVENT_ADD,
+					NULL);
+	TEST_ASSERT_SUCCESS(ret, "no interrupt for the Rx queue: %s", rte_strerror(-ret));
+
+	do {
+		/*
+		 * Enable the interrupt before the kernel sends. A frame that
+		 * arrives from now on raises it, also before the sleep below.
+		 */
+		ret = rte_eth_dev_rx_intr_enable(port_id, 0);
+		TEST_ASSERT_SUCCESS(ret, "Rx interrupt enable failed: %s", rte_strerror(-ret));
+		/* Drop the frames that the kernel sent earlier, and their event. */
+		port_receive();
+		rte_epoll_wait(RTE_EPOLL_PER_THREAD, &event, 1, 0);
+
+		TEST_ASSERT_SUCCESS(kernel_send(), "kernel send failed");
+		ret = rte_epoll_wait(RTE_EPOLL_PER_THREAD, &event, 1, WAIT_MS);
+		TEST_ASSERT(ret == 1, "no Rx interrupt within %d ms", WAIT_MS);
+		ret = rte_eth_dev_rx_intr_disable(port_id, 0);
+		TEST_ASSERT_SUCCESS(ret, "Rx interrupt disable failed: %s", rte_strerror(-ret));
+		/* If another frame of the kernel raised the interrupt, send again. */
+		found = port_receive();
+	} while (!found && rte_get_timer_cycles() < deadline);
+	TEST_ASSERT(found, "frame from the kernel not received");
+
+	ret = rte_eth_dev_rx_intr_ctl_q(port_id, 0, RTE_EPOLL_PER_THREAD, RTE_INTR_EVENT_DEL,
+					NULL);
+	TEST_ASSERT_SUCCESS(ret, "Rx interrupt removal failed: %s", rte_strerror(-ret));
+
+	return TEST_SUCCESS;
+}
+
+/*
+ * Rx interrupts with an offload, which makes configure initialize the device
+ * twice. Then a second configure. The interrupt eventfds belong to the
+ * backend, so the driver must not release them when it initializes the
+ * device again.
+ */
+static int
+test_virtio_user_rx_intr(void)
+{
+	struct rte_eth_conf conf = {
+		.intr_conf.rxq = 1,
+		.rxmode.offloads = RTE_ETH_RX_OFFLOAD_UDP_CKSUM | RTE_ETH_RX_OFFLOAD_TCP_CKSUM,
+	};
+	struct rte_eth_dev_info info;
+	int round, ret;
+
+	ret = rte_eth_dev_info_get(port_id, &info);
+	TEST_ASSERT_SUCCESS(ret, "dev_info failed: %s", rte_strerror(-ret));
+	conf.rxmode.offloads &= info.rx_offload_capa;
+	TEST_ASSERT(conf.rxmode.offloads != 0, "no Rx checksum offload to request");
+
+	for (round = 0; round < 2; round++) {
+		TEST_ASSERT_SUCCESS(port_start(&conf), "round %d: port start failed", round);
+		TEST_ASSERT_SUCCESS(rx_intr_wakes(), "round %d: Rx interrupt failed", round);
+		TEST_ASSERT_SUCCESS(port_stop(), "round %d: port stop failed", round);
+	}
+
+	return TEST_SUCCESS;
+}
+
+static void
+test_virtio_user_cleanup(void)
+{
+	if (ksock >= 0)
+		close(ksock);
+	ksock = -1;
+	if (port_id != RTE_MAX_ETHPORTS) {
+		port_stop();
+		rte_eth_dev_close(port_id);
+		rte_vdev_uninit(VDEV_NAME);
+	}
+	port_id = RTE_MAX_ETHPORTS;
+	rte_mempool_free(mp);
+	mp = NULL;
+}
+
+/* Open the kernel's end. Set the TAP interface up and bind a packet socket to it. */
+static int
+kernel_open(void)
+{
+	struct sockaddr_ll addr = {
+		.sll_family = AF_PACKET,
+		.sll_protocol = htons(ETH_P_ALL),
+	};
+	struct ifreq ifr;
+	int one = 1;
+
+	ksock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+	if (ksock < 0)
+		return -1;
+	memset(&ifr, 0, sizeof(ifr));
+	strlcpy(ifr.ifr_name, TAP_NAME, sizeof(ifr.ifr_name));
+	if (ioctl(ksock, SIOCGIFFLAGS, &ifr) < 0)
+		return -1;
+	ifr.ifr_flags |= IFF_UP;
+	if (ioctl(ksock, SIOCSIFFLAGS, &ifr) < 0)
+		return -1;
+	addr.sll_ifindex = if_nametoindex(TAP_NAME);
+	if (addr.sll_ifindex == 0 ||
+	    bind(ksock, (const struct sockaddr *)&addr, sizeof(addr)) < 0 ||
+	    setsockopt(ksock, SOL_PACKET, PACKET_VNET_HDR, &one, sizeof(one)) < 0)
+		return -1;
+
+	return 0;
+}
+
+static int
+test_virtio_user_setup(void)
+{
+	mp = rte_pktmbuf_pool_create("virtio_user_test", NB_MBUF, 32, 0,
+				     RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
+	TEST_ASSERT_NOT_NULL(mp, "mempool creation failed: %s", rte_strerror(rte_errno));
+
+	/*
+	 * The port has two queue pairs and the tests configure one Rx queue,
+	 * so the port has more interrupt eventfds than Rx queues.
+	 * vhost-net creates the TAP interface, which takes root or CAP_NET_ADMIN.
+	 */
+	if (rte_vdev_init(VDEV_NAME, "path=/dev/vhost-net,iface=" TAP_NAME ",queues=2") < 0 ||
+	    rte_eth_dev_get_port_by_name(VDEV_NAME, &port_id) != 0) {
+		printf("No virtio-user port on /dev/vhost-net, skipping test\n");
+		test_virtio_user_cleanup();
+		return TEST_SKIPPED;
+	}
+	if (kernel_open() < 0) {
+		printf("No packet socket on %s (%s), skipping test\n", TAP_NAME, strerror(errno));
+		test_virtio_user_cleanup();
+		return TEST_SKIPPED;
+	}
+
+	return TEST_SUCCESS;
+}
+
+static struct unit_test_suite test_pmd_virtio_user_suite = {
+	.setup = test_virtio_user_setup,
+	.teardown = test_virtio_user_cleanup,
+	.suite_name = "virtio-user PMD Unit Test Suite",
+	.unit_test_cases = {
+		TEST_CASE(test_virtio_user_rx_intr),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_pmd_virtio_user(void)
+{
+	return unit_test_suite_runner(&test_pmd_virtio_user_suite);
+}
+
+#endif /* RTE_EXEC_ENV_LINUX */
+
+REGISTER_FAST_TEST(virtio_user_pmd_autotest, NOHUGE_OK, ASAN_OK, test_pmd_virtio_user);
-- 
2.56.1


  parent reply	other threads:[~2026-11-17 22:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 12:43 [PATCH 0/1] net/virtio: fix Rx interrupts with MSI-X Isaac Boukris
2026-11-17 12:43 ` [PATCH 1/1] net/virtio: fix Rx interrupt setup " Isaac Boukris
2026-11-17 14:29   ` David Marchand
2026-11-17 19:51     ` Isaac Boukris
2026-11-17 21:59 ` [PATCH v2 0/5] net/virtio: fix Rx interrupts " Isaac Boukris
2026-11-17 21:59   ` [PATCH v2 1/5] net/virtio: fix Rx interrupt setup " Isaac Boukris
2026-11-17 21:59   ` Isaac Boukris [this message]
2026-11-17 21:59   ` [PATCH v2 3/5] app/testpmd: add Rx queue interrupts option Isaac Boukris
2026-11-17 22:00   ` [PATCH v2 4/5] dts: support virtio PCI ports Isaac Boukris
2026-11-17 22:00   ` [PATCH v2 5/5] dts: add Rx interrupt test suite Isaac Boukris
2026-11-18  0:19 ` [PATCH v3 0/7] net/virtio: fix Rx interrupts with MSI-X Isaac Boukris
2026-11-18  0:19   ` [PATCH v3 1/7] net/virtio: fix Rx interrupt setup " Isaac Boukris
2026-11-18  0:19   ` [PATCH v3 2/7] net/virtio-user: fix packet loss with unused queue pairs Isaac Boukris
2026-11-18  0:19   ` [PATCH v3 3/7] test/virtio_user: add unit test of " Isaac Boukris
2026-11-18  0:19   ` [PATCH v3 4/7] test/virtio_user: add unit test of Rx interrupts Isaac Boukris
2026-11-18  0:19   ` [PATCH v3 5/7] app/testpmd: add Rx queue interrupts option Isaac Boukris
2026-11-18  0:19   ` [PATCH v3 6/7] dts: support virtio PCI ports Isaac Boukris
2026-11-18  0:19   ` [PATCH v3 7/7] dts: add Rx interrupt test suite Isaac Boukris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117220001.965310-3-iboukris@gmail.com \
    --to=iboukris@gmail.com \
    --cc=aman.deep.singh@intel.com \
    --cc=chenbox@nvidia.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=luca.vizzarro@arm.com \
    --cc=maxime.coquelin@redhat.com \
    --cc=patrickrobb1997@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox