Skip to contentSkip to conversation

Add vhost-vdpa and Shadow Virtqueue tests

RFC PATCH In review Eugenio Pérez 2 participants 10 messages Mar 5 – Mar 25 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover Add vhost-vdpa and Shadow Virtqueue tests

Based on vhost-user tests, the qos register itself as a VDUSE device and receives the events from QEMU.

The test infrastructure creates a thread that acts as a VDUSE device, while the regular test thread is managing QEMU.

This enables automated testing of vhost-vdpa code paths that previously required manual testing.

Changes from vhost-user test:
* Automatic cleanup of many things.
* Handle the vduse fd and timeout.
* VDPA device cannot be removed before deleting QEMU, killing QEMU in vhost_vdpa_test_cleanup.
* Read in enable callbacks, and the actual test_read_guest_mem is just waiting.
* Add vhost_vdpa_thread to abstract fd monitoring
* Use QemuMutex and QemuConf for scoped cleanup

RFC: I'm not sure if this is the right place to add the tests in meson. Also, a few things are just with asserts() or g_spawn(), instead of more elegant code. I can clean them if this result useful.

Also, I don't know how to include the libvduse.a library as meson complains it's out of the tests/ directory, so I'm including the .c directly. Ugly but it works for now.

The test verifies SVQ correctly handles batched pushes where multiple elements are filled before flushing the used ring.

With this test, gcov reported coverage is:
Total Hit

Lines:     83.9 %    347  291
Functions: 90.3 %     31   28
Branches:  59.2 %    157   93

Apart from impossible banches like scoped cleanups, the missing blocks are:
* Full queue situations, or use a full queue at once with IN_ORDER.
* Event idx.
* All SVQ CVQ handling.
* Hard to reproduce casuistics like a linear buffer in GPA that is split into more than one buffer in HVA, and then SVQ is saturated.
* Buggy input (no descriptors, used descriptors that are not available, moving indexes more than vq size).
* Unbinding device call notifier from QEMU vhost system.
* Malicious inputs.

This builds on top of
https://mail.gnu.org/archive/html/qemu-devel/2026-03/msg01366.html

Eugenio Pérez (8):
tests: vhost-vdpa: add initial VDUSE-based vhost-vdpa tests
tests: vhost-vdpa: parameterize VDUSE operations
tests: vhost-vdpa: add TX packet transmission test
tests: vhost-vdpa: test SVQ cleanup of pending buffers
tests: vhost-vdpa: add descriptor chain tests
tests: vhost-vdpa: test out-of-order descriptor completion
tests: vhost-vdpa: introduce TestParameters struct
tests: vhost-vdpa: add VIRTIO_F_IN_ORDER feature tests

 tests/qtest/meson.build       |   3 +
 tests/qtest/vhost-vdpa-test.c | 805 ++++++++++++++++++++++++++++++++++
 2 files changed, 808 insertions(+)
 create mode 100644 tests/qtest/vhost-vdpa-test.c
Signature · 1 line
2.53.0
#1 lore raw

RFC PATCH 1/8 tests: vhost-vdpa: add initial VDUSE-based vhost-vdpa tests

Based on vhost-user tests, the qos register itself as a VDUSE device and receives the events from QEMU.

The test infrastructure creates a thread that acts as a VDUSE device, while the regular test thread is managing QEMU.

This basic test just verify that the guest memory ring addresses are accessible, similar to the already existing test in vhost-user.

This enables automated testing of vhost-vdpa code paths that previously required manual testing with real hardware.

Changes from vhost-user test:
* Automatic cleanup of many things.
* Handle the vduse fd and timeout.
* VDPA device cannot be removed before deleting QEMU, killing QEMU in vhost_vdpa_test_cleanup.
* Read in enable callbacks, and the actual test_read_guest_mem is just waiting.
* Add vhost_vdpa_thread to abstract fd monitoring
* Use QemuMutex and QemuConf for scoped cleanup

RFC: I'm not sure if this is the right place to add the tests in meson. Also, a few things are just with asserts() or g_spawn(), instead of more elegant code.

Also, I don't know how to include the libvduse.a library as meson complains it's out of the tests/ directory, so I'm including the .c directly. Ugly but it works.

---
 tests/qtest/meson.build       |   3 +
 tests/qtest/vhost-vdpa-test.c | 426 ++++++++++++++++++++++++++++++++++
 2 files changed, 429 insertions(+)
 create mode 100644 tests/qtest/vhost-vdpa-test.c
tests/qtest/meson.build +3 -0
index ba9f59d2f8f7..0fdc8fb4a764 100644
@@ -346,6 +346,9 @@ endif
 if have_tools and have_vhost_user_blk_server
   qos_test_ss.add(files('vhost-user-blk-test.c'))
 endif
+if have_libvduse and have_vhost_vdpa
+  qos_test_ss.add(files('vhost-vdpa-test.c'))
+endif
 
 tpmemu_files = ['tpm-emu.c', 'tpm-util.c', 'tpm-tests.c']
 
tests/qtest/vhost-vdpa-test.c new file +426 -0

426 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [RFC PATCH 1/8] tests: vhost-vdpa: add initial VDUSE-based vhost-vdpa tests

Quoted 22 lines · Eugenio Pérez

Based on vhost-user tests, the qos register itself as a VDUSE device and receives the events from QEMU.

The test infrastructure creates a thread that acts as a VDUSE device, while the regular test thread is managing QEMU.

This basic test just verify that the guest memory ring addresses are accessible, similar to the already existing test in vhost-user.

This enables automated testing of vhost-vdpa code paths that previously required manual testing with real hardware.

Changes from vhost-user test:
* Automatic cleanup of many things.
* Handle the vduse fd and timeout.
* VDPA device cannot be removed before deleting QEMU, killing QEMU in vhost_vdpa_test_cleanup.
* Read in enable callbacks, and the actual test_read_guest_mem is just waiting.
* Add vhost_vdpa_thread to abstract fd monitoring
* Use QemuMutex and QemuConf for scoped cleanup

Quoted 8 lines

RFC: I'm not sure if this is the right place to add the tests in meson. Also, a few things are just with asserts() or g_spawn(), instead of more elegant code.

Also, I don't know how to include the libvduse.a library as meson complains it's out of the tests/ directory, so I'm including the .c directly. Ugly but it works.

The proper way should be the following. Unfortunately, you'll have to rewrite some parts of the test as it currently accesses symbols internal to the library.

-- >8 --
From ca4418e57a2de83b1fea49f589f2c27ba424039b Mon Sep 17 00:00:00 2001
From: Fabiano Rosas <[email protected]>
Date: Wed, 25 Mar 2026 17:54:41 -0300
Subject: [PATCH] tmp

---
 tests/qtest/meson.build | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/qtest/meson.build +3 -1
index 2b002ea1ce..d6cf0e25ad 100644
@@ -348,6 +348,7 @@ if have_tools and have_vhost_user_blk_server
 endif
 if have_libvduse and have_vhost_vdpa
   qos_test_ss.add(files('vhost-vdpa-test.c'))
+  qos_test_ss.add(libvduse)
 endif
 
 tpmemu_files = ['tpm-emu.c', 'tpm-util.c', 'tpm-tests.c']
@@ -395,7 +396,8 @@ qtests = {
   'pxe-test': files('boot-sector.c'),
   'pnv-xive2-test': files('pnv-xive2-common.c', 'pnv-xive2-flush-sync.c',
                           'pnv-xive2-nvpg_bar.c'),
-  'qos-test': [chardev, io, qos_test_ss.apply({}).sources()],
+  'qos-test': [chardev, io, qos_test_ss.apply({}).sources(),
+               qos_test_ss.apply({}).dependencies()],
   'tpm-crb-swtpm-test': [io, tpmemu_files],
   'tpm-crb-test': [io, tpmemu_files],
   'tpm-tis-swtpm-test': [io, tpmemu_files, 'tpm-tis-util.c'],
Signature · 1 line
2.51.0
#3 lore raw

RFC PATCH 2/8 tests: vhost-vdpa: parameterize VDUSE operations

In reply to Eugenio Pérez

Make the VduseOps callbacks a parameter to test_server_new() instead of hardcoding vduse_read_guest_mem_ops.

This allows different test cases to provide custom queue enable/disable handlers for testing various vhost-vdpa scenarios.

---
 tests/qtest/vhost-vdpa-test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/qtest/vhost-vdpa-test.c +3 -3
index 1fc5acacfed3..1c7d8540bd19 100644
@@ -277,7 +277,7 @@ static bool test_setup_reconnect_log(VduseDev *vdev, const char *tmpfs)
     return ok;
 }
 
-static TestServer *test_server_new(const gchar *name)
+static TestServer *test_server_new(const gchar *name, const VduseOps *ops)
 {
     TestServer *server = g_new0(TestServer, 1);
     g_autoptr(GError) err = NULL;
@@ -302,7 +302,7 @@ static TestServer *test_server_new(const gchar *name)
                                     2, /* num_queues */
                                     sizeof(config),
                                     config,
-                                    &vduse_read_guest_mem_ops,
+                                    ops,
                                     server);
 
     if (!server->vdev) {
@@ -387,7 +387,7 @@ static void vhost_vdpa_test_cleanup(void *s)
 
 static void *vhost_vdpa_test_setup_memfile(GString *cmd_line, void *arg)
 {
-    TestServer *server = test_server_new("vdpa-memfile");
+    TestServer *server = test_server_new("vdpa-memfile", &vduse_read_guest_mem_ops);
 
     if (!server->ready) {
         g_test_skip("Failed to create VDUSE device");
Signature · 1 line
2.53.0
#4 lore raw

RFC PATCH 3/8 tests: vhost-vdpa: add TX packet transmission test

In reply to Eugenio Pérez

Add test infrastructure for sending packets through TX virtqueue.

---
 tests/qtest/vhost-vdpa-test.c | 141 +++++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 9 deletions(-)
tests/qtest/vhost-vdpa-test.c +132 -9
index 1c7d8540bd19..7b1c34aa415e 100644
@@ -17,6 +17,7 @@
 #include "libqtest-single.h"
 #include "qapi/error.h"
 #include "libqos/qgraph.h"
+#include "libqos/virtio-net.h"
 #include "hw/virtio/virtio-net.h"
 
 #include "standard-headers/linux/virtio_ids.h"
@@ -25,6 +26,7 @@
 #include "subprojects/libvduse/linux-headers/linux/vduse.h"
 #include "subprojects/libvduse/libvduse.h"
 
+#include <endian.h>
 #include <poll.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
@@ -35,7 +37,7 @@
 
 #define QEMU_CMD_MEM    " -m %d -object memory-backend-file,id=mem,size=%dM," \
                         "mem-path=%s,share=on -numa node,memdev=mem"
-#define QEMU_CMD_VDPA   " -netdev type=vhost-vdpa,vhostdev=%s,id=hs0"
+#define QEMU_CMD_VDPA   " -netdev type=vhost-vdpa,x-svq=on,vhostdev=%s,id=hs0"
 #define VDUSE_RECONNECT_LOG "vduse_reconnect.log"
 
 typedef struct VdpaThread {
@@ -80,6 +82,61 @@ static void vhost_vdpa_thread_add_source_fd(VdpaThread *t, int fd,
     g_source_unref(src);
 }
 
+/**
+ * Send a descriptor or a chain of descriptors to the device, and optionally
+ * and / or update the avail ring and avail_idx of the driver ring.
+ *
+ * @alloc: the guest allocator to allocate memory for the descriptors
+ * @net: the virtio net device
+ * @t: the vdpa thread to push the expected chain length if kick is true
+ *
+ * Returns the kick_id you can use to kick the device in a later call to this
+ * function.
+ */
+static uint32_t vhost_vdpa_add_tx_pkt_descs(QGuestAllocator *alloc,
+                                            QVirtioNet *net, VdpaThread *t)
+{
+    QTestState *qts = global_qtest;
+    uint32_t req_addr;
+
+    /* TODO: Actually free this. RFC, is actually needed? */
+    req_addr = guest_alloc(alloc, 64);
+    g_assert_cmpint(req_addr, >, 0);
+
+    return qvirtqueue_add(qts, net->queues[1], req_addr, 64, /* write */ false,
+                          /* next */ false);
+}
+
+static void vhost_vdpa_kick_tx_desc(VdpaThread *t, QVirtioNet *net,
+                                    uint32_t kick_id)
+{
+    QTestState *qts = global_qtest;
+
+    qvirtqueue_kick(qts, net->vdev, net->queues[1], kick_id);
+}
+
+static void vhost_vdpa_get_tx_pkt(QGuestAllocator *alloc, QVirtioNet *net,
+                                  uint32_t desc_idx, VdpaThread *t)
+{
+    g_autofree struct VduseVirtqElement *elem = NULL;
+    int64_t timeout = 5 * G_TIME_SPAN_SECOND;
+    QTestState *qts = global_qtest;
+    vring_desc_t desc;
+    int64_t end_time_us;
+    uint32_t len;
+
+    end_time_us = g_get_monotonic_time() + timeout;
+    qvirtio_wait_used_elem(qts, net->vdev, net->queues[1], desc_idx, &len,
+                           timeout);
+    g_assert_cmpint(g_get_monotonic_time(), <, end_time_us);
+    g_assert_cmpint(len, ==, 0);
+
+    qtest_memread(qts, net->queues[1]->desc + sizeof(desc)*desc_idx, &desc,
+                  sizeof(desc));
+    /* We know we're version 1 so always little endian */
+    guest_free(alloc, le64toh(desc.addr));
+}
+
 typedef struct TestServer {
     gchar *vduse_name;
     gchar *vdpa_dev_path;
@@ -163,6 +220,54 @@ static const VduseOps vduse_read_guest_mem_ops = {
     .disable_queue = vduse_read_guest_mem_disable_queue,
 };
 
+static gboolean vhost_vdpa_rxtx_handle_tx(int fd, GIOCondition condition,
+                                          void *data)
+{
+    VduseVirtq *vq = data;
+
+    eventfd_read(fd, (eventfd_t[]){0});
+    do {
+        g_autofree VduseVirtqElement *elem = NULL;
+
+        elem = vduse_queue_pop(vq, sizeof(*elem));
+        if (!elem) {
+            break;
+        }
+
+        g_test_message("Got element with %d buffers", elem->out_num);
+        g_assert_cmpint(elem->in_num, ==, 0);
+
+        vduse_queue_push(vq, elem, 0);
+        vduse_queue_notify(vq);
+    } while (true);
+
+    return G_SOURCE_CONTINUE;
+}
+
+static void vduse_rxtx_enable_queue(VduseDev *dev, VduseVirtq *vq)
+{
+    TestServer *s = vduse_dev_get_priv(dev);
+
+    g_test_message("Enabling queue %d", vq->index);
+
+    if (vq->index == 1) {
+        /* This is the tx queue, add a source to handle it */
+        vhost_vdpa_thread_add_source_fd(&s->vdpa_thread,
+                                        vduse_queue_get_fd(vq),
+                                        vhost_vdpa_rxtx_handle_tx, vq);
+    }
+}
+
+static void vduse_rxtx_disable_queue(VduseDev *dev, VduseVirtq *vq)
+{
+    /* Queue disabled */
+}
+
+static const VduseOps vduse_rxtx_ops = {
+    .enable_queue = vduse_rxtx_enable_queue,
+    .disable_queue = vduse_rxtx_disable_queue,
+};
+
 static gboolean vduse_dev_handler_source_fd(int fd, GIOCondition condition,
                                             void *data)
 {
@@ -292,7 +397,8 @@ static TestServer *test_server_new(const gchar *name, const VduseOps *ops)
     qemu_mutex_init(&server->data_mutex);
     qemu_cond_init(&server->data_cond);
 
-    features = vduse_get_virtio_features() |
+    /* Disabling NOTIFY_ON_EMPTY as SVQ does not support it */
+    features = (vduse_get_virtio_features() & ~(1ULL << VIRTIO_F_NOTIFY_ON_EMPTY)) |
                (1ULL << VIRTIO_NET_F_MAC);
 
     server->vdev = vduse_dev_create(server->vduse_name,
@@ -376,6 +482,13 @@ static void wait_for_vqs(TestServer *s)
     }
 }
 
+static void test_wait(void *obj, void *arg, QGuestAllocator *alloc)
+{
+    TestServer *server = arg;
+
+    wait_for_vqs(server);
+}
+
 static void vhost_vdpa_test_cleanup(void *s)
 {
     TestServer *server = s;
@@ -385,9 +498,9 @@ static void vhost_vdpa_test_cleanup(void *s)
     test_server_free(server);
 }
 
-static void *vhost_vdpa_test_setup_memfile(GString *cmd_line, void *arg)
+static void *vhost_vdpa_test_setup(GString *cmd_line, void *arg)
 {
-    TestServer *server = test_server_new("vdpa-memfile", &vduse_read_guest_mem_ops);
+    TestServer *server = test_server_new("vdpa-memfile", arg);
 
     if (!server->ready) {
         g_test_skip("Failed to create VDUSE device");
@@ -404,23 +517,33 @@ static void *vhost_vdpa_test_setup_memfile(GString *cmd_line, void *arg)
     return server;
 }
 
-static void test_read_guest_mem(void *obj, void *arg, QGuestAllocator *alloc)
+static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
 {
     TestServer *server = arg;
+    QVirtioNet *net = obj;
+    uint32_t free_head;
 
-    wait_for_vqs(server);
+    free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread);
+    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head);
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
 }
 
 static void register_vhost_vdpa_test(void)
 {
+    /* TODO: void * discards const qualifier */
     QOSGraphTestOptions opts = {
-        .before = vhost_vdpa_test_setup_memfile,
+        .before = vhost_vdpa_test_setup,
         .subprocess = true,
-        .arg = NULL,
+        .arg = (void *)&vduse_read_guest_mem_ops,
     };
 
     qos_add_test("vhost-vdpa/read-guest-mem/memfile",
                  "virtio-net",
-                 test_read_guest_mem, &opts);
+                 test_wait, &opts);
+
+    opts.arg = (void *)&vduse_rxtx_ops;
+    qos_add_test("vhost-vdpa/rxtx",
+                 "virtio-net",
+                 vhost_vdpa_tx_test, &opts);
 }
 libqos_init(register_vhost_vdpa_test);
Signature · 1 line
2.53.0
#5 lore raw

RFC PATCH 4/8 tests: vhost-vdpa: test SVQ cleanup of pending buffers

In reply to Eugenio Pérez

Add RX buffers to the receive queue before terminating QEMU to verify that shadow virtqueue properly cleans up unused descriptors during shutdown.

This tests the SVQ teardown path when descriptors remain in the available ring but were never used by the device.

---
 tests/qtest/vhost-vdpa-test.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tests/qtest/vhost-vdpa-test.c +23 -0
index 7b1c34aa415e..8dde7d95b167 100644
@@ -40,10 +40,15 @@
 #define QEMU_CMD_VDPA   " -netdev type=vhost-vdpa,x-svq=on,vhostdev=%s,id=hs0"
 #define VDUSE_RECONNECT_LOG "vduse_reconnect.log"
 
+static int NUM_RX_BUFS = 2;
+
 typedef struct VdpaThread {
     GThread *thread;
     GMainLoop *loop;
     GMainContext *context;
+
+    /* Guest memory that must be free at the end of the test */
+    uint64_t qemu_mem_to_free;
 } VdpaThread;
 
 static void *vhost_vdpa_thread_function(void *data)
@@ -82,6 +87,21 @@ static void vhost_vdpa_thread_add_source_fd(VdpaThread *t, int fd,
     g_source_unref(src);
 }
 
+static void vhost_vdpa_add_rx_pkts(QGuestAllocator *alloc, QVirtioNet *net,
+                                   VdpaThread *t)
+{
+    QTestState *qts = global_qtest;
+
+    t->qemu_mem_to_free = guest_alloc(alloc, 64);
+
+    for (int i = 0; i < NUM_RX_BUFS; i++) {
+        uint32_t head = qvirtqueue_add(qts, net->queues[0],
+                                       t->qemu_mem_to_free, 64,
+                                       /* write */ false, /* next */ false);
+        qvirtqueue_kick(qts, net->vdev, net->queues[0], head);
+    }
+}
+
 /**
  * Send a descriptor or a chain of descriptors to the device, and optionally
  * and / or update the avail ring and avail_idx of the driver ring.
@@ -523,6 +543,9 @@ static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
     QVirtioNet *net = obj;
     uint32_t free_head;
 
+    /* Add some rx packets so SVQ must clean them at the end of QEMU run */
+    vhost_vdpa_add_rx_pkts(alloc, net, &server->vdpa_thread);
+
     free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread);
     vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head);
     vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
Signature · 1 line
2.53.0
#6 lore raw

RFC PATCH 5/8 tests: vhost-vdpa: add descriptor chain tests

In reply to Eugenio Pérez

Extend TX tests to support multi-descriptor chains:
- Add chain_len parameter to control chain length
- Use GAsyncQueue to validate expected vs actual chain lengths
- Test single descriptors, 2-descriptor chains, and out-of-order chains

Chain descriptors all point to the same guest memory buffer to simplify allocation tracking while still testing chain traversal logic.

---
 tests/qtest/vhost-vdpa-test.c | 53 ++++++++++++++++++++++++++++++-----
 1 file changed, 46 insertions(+), 7 deletions(-)
tests/qtest/vhost-vdpa-test.c +46 -7
index 8dde7d95b167..433e5d71ca7a 100644
@@ -47,6 +47,9 @@ typedef struct VdpaThread {
     GMainLoop *loop;
     GMainContext *context;
 
+    /* Expected elements queue to compare properties */
+    GAsyncQueue *elem_queue;
+
     /* Guest memory that must be free at the end of the test */
     uint64_t qemu_mem_to_free;
 } VdpaThread;
@@ -60,6 +63,7 @@ static void *vhost_vdpa_thread_function(void *data)
 
 static void vhost_vdpa_thread_init(VdpaThread *t)
 {
+    t->elem_queue = g_async_queue_new();
     t->context = g_main_context_new();
     t->loop = g_main_loop_new(t->context, FALSE);
     t->thread = g_thread_new("vdpa-thread", vhost_vdpa_thread_function, t->loop);
@@ -76,6 +80,7 @@ static void vhost_vdpa_thread_cleanup(VdpaThread *t)
 
     g_main_loop_unref(t->loop);
     g_main_context_unref(t->context);
+    g_async_queue_unref(t->elem_queue);
 }
 
 static void vhost_vdpa_thread_add_source_fd(VdpaThread *t, int fd,
@@ -109,29 +114,48 @@ static void vhost_vdpa_add_rx_pkts(QGuestAllocator *alloc, QVirtioNet *net,
  * @alloc: the guest allocator to allocate memory for the descriptors
  * @net: the virtio net device
  * @t: the vdpa thread to push the expected chain length if kick is true
+ * @chain_len: the number of descriptors in the chain to add
  *
  * Returns the kick_id you can use to kick the device in a later call to this
  * function.
  */
 static uint32_t vhost_vdpa_add_tx_pkt_descs(QGuestAllocator *alloc,
-                                            QVirtioNet *net, VdpaThread *t)
+                                            QVirtioNet *net, VdpaThread *t,
+                                            uint32_t chain_len)
 {
     QTestState *qts = global_qtest;
-    uint32_t req_addr;
+    uint32_t req_addr, kick_id = UINT32_MAX;
 
+    assert(chain_len > 0);
     /* TODO: Actually free this. RFC, is actually needed? */
     req_addr = guest_alloc(alloc, 64);
     g_assert_cmpint(req_addr, >, 0);
 
-    return qvirtqueue_add(qts, net->queues[1], req_addr, 64, /* write */ false,
-                          /* next */ false);
+    /*
+    * We set up the descriptors in a way that each of them points to the same
+    * buffer. This simplifies guest's memory management while still exercising
+    * chain traversal in SVQ.
+    */
+    for (uint32_t i = 0; i < chain_len; i++) {
+        uint32_t head;
+        bool next = i != chain_len - 1;
+
+        head = qvirtqueue_add(qts, net->queues[1], req_addr, 64,
+                              /* write */ false, next);
+        if (i == 0) {
+            kick_id = head;
+        }
+    }
+
+    return kick_id;
 }
 
 static void vhost_vdpa_kick_tx_desc(VdpaThread *t, QVirtioNet *net,
-                                    uint32_t kick_id)
+                                    uint32_t kick_id, uint32_t chain_len)
 {
     QTestState *qts = global_qtest;
 
+    g_async_queue_push(t->elem_queue, (void *)(intptr_t)chain_len);
     qvirtqueue_kick(qts, net->vdev, net->queues[1], kick_id);
 }
 
@@ -244,9 +268,12 @@ static gboolean vhost_vdpa_rxtx_handle_tx(int fd, GIOCondition condition,
                                           void *data)
 {
     VduseVirtq *vq = data;
+    VduseDev *dev = vduse_queue_get_dev(vq);
+    TestServer *s = vduse_dev_get_priv(dev);
 
     eventfd_read(fd, (eventfd_t[]){0});
     do {
+        intptr_t expected_elems;
         g_autofree VduseVirtqElement *elem = NULL;
 
         elem = vduse_queue_pop(vq, sizeof(*elem));
@@ -254,7 +281,10 @@ static gboolean vhost_vdpa_rxtx_handle_tx(int fd, GIOCondition condition,
             break;
         }
 
+        expected_elems = (intptr_t)g_async_queue_try_pop(s->vdpa_thread.elem_queue);
+        g_assert_cmpint(expected_elems, >, 0);
         g_test_message("Got element with %d buffers", elem->out_num);
+        g_assert_cmpint(elem->out_num, ==, expected_elems);
         g_assert_cmpint(elem->in_num, ==, 0);
 
         vduse_queue_push(vq, elem, 0);
@@ -546,9 +576,18 @@ static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
     /* Add some rx packets so SVQ must clean them at the end of QEMU run */
     vhost_vdpa_add_rx_pkts(alloc, net, &server->vdpa_thread);
 
-    free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread);
-    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head);
+    /* Simple packet */
+    free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
+                                            1);
+    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 1);
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
+
+    /* Simple chain */
+    free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
+                                            2);
+    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 2);
     vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
+
 }
 
 static void register_vhost_vdpa_test(void)
Signature · 1 line
2.53.0
#7 lore raw

RFC PATCH 6/8 tests: vhost-vdpa: test out-of-order descriptor completion

In reply to Eugenio Pérez

Add tests that submit descriptors in one order but complete them in a different order:
- Submit desc1 then desc2, complete desc2 then desc1
- Same pattern with 2-descriptor chains

This verifies SVQ correctly handles devices that don't preserve submission order in completions.

---
 tests/qtest/vhost-vdpa-test.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
tests/qtest/vhost-vdpa-test.c +19 -1
index 433e5d71ca7a..5efe688673cb 100644
@@ -571,7 +571,7 @@ static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
 {
     TestServer *server = arg;
     QVirtioNet *net = obj;
-    uint32_t free_head;
+    uint32_t free_head, free_head2;
 
     /* Add some rx packets so SVQ must clean them at the end of QEMU run */
     vhost_vdpa_add_rx_pkts(alloc, net, &server->vdpa_thread);
@@ -588,6 +588,24 @@ static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
     vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 2);
     vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
 
+    /* Out of order descriptors */
+    free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
+                                            1);
+    free_head2 = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
+                                             1);
+    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head2, 1);
+    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 1);
+
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head2, &server->vdpa_thread);
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
+
+    /* Out of order chains */
+    free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
+                                            2);
+    free_head2 = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
+                                             2);
+    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head2, 2);
+    vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 2);
 }
 
 static void register_vhost_vdpa_test(void)
Signature · 1 line
2.53.0
#8 lore raw

RFC PATCH 7/8 tests: vhost-vdpa: introduce TestParameters struct

In reply to Eugenio Pérez

Encapsulate test configuration (VduseOps, features) in TestParameters struct to prepare for parameterized tests with different device feature flags.

---
 tests/qtest/vhost-vdpa-test.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tests/qtest/vhost-vdpa-test.c +17 -4
index 5efe688673cb..0192b00748d5 100644
@@ -42,6 +42,10 @@
 
 static int NUM_RX_BUFS = 2;
 
+typedef struct TestParameters {
+    const VduseOps *ops;
+} TestParameters;
+
 typedef struct VdpaThread {
     GThread *thread;
     GMainLoop *loop;
@@ -264,6 +268,10 @@ static const VduseOps vduse_read_guest_mem_ops = {
     .disable_queue = vduse_read_guest_mem_disable_queue,
 };
 
+static const TestParameters mem_read_params = {
+    .ops = &vduse_read_guest_mem_ops,
+};
+
 static gboolean vhost_vdpa_rxtx_handle_tx(int fd, GIOCondition condition,
                                           void *data)
 {
@@ -318,6 +326,10 @@ static const VduseOps vduse_rxtx_ops = {
     .disable_queue = vduse_rxtx_disable_queue,
 };
 
+static const TestParameters rxtx_params = {
+    .ops = &vduse_rxtx_ops,
+};
+
 static gboolean vduse_dev_handler_source_fd(int fd, GIOCondition condition,
                                             void *data)
 {
@@ -432,7 +444,8 @@ static bool test_setup_reconnect_log(VduseDev *vdev, const char *tmpfs)
     return ok;
 }
 
-static TestServer *test_server_new(const gchar *name, const VduseOps *ops)
+static TestServer *test_server_new(const gchar *name,
+                                   const TestParameters *params)
 {
     TestServer *server = g_new0(TestServer, 1);
     g_autoptr(GError) err = NULL;
@@ -458,7 +471,7 @@ static TestServer *test_server_new(const gchar *name, const VduseOps *ops)
                                     2, /* num_queues */
                                     sizeof(config),
                                     config,
-                                    ops,
+                                    params->ops,
                                     server);
 
     if (!server->vdev) {
@@ -614,14 +627,14 @@ static void register_vhost_vdpa_test(void)
     QOSGraphTestOptions opts = {
         .before = vhost_vdpa_test_setup,
         .subprocess = true,
-        .arg = (void *)&vduse_read_guest_mem_ops,
+        .arg = (void *)&mem_read_params,
     };
 
     qos_add_test("vhost-vdpa/read-guest-mem/memfile",
                  "virtio-net",
                  test_wait, &opts);
 
-    opts.arg = (void *)&vduse_rxtx_ops;
+    opts.arg = (void *)&rxtx_params;
     qos_add_test("vhost-vdpa/rxtx",
                  "virtio-net",
                  vhost_vdpa_tx_test, &opts);
Signature · 1 line
2.53.0
#9 lore raw

RFC PATCH 8/8 tests: vhost-vdpa: add VIRTIO_F_IN_ORDER feature tests

In reply to Eugenio Pérez

The test verifies SVQ correctly handles batched pushes where multiple elements are filled before flushing the used ring.

With this test, gcov reported coverage is:
Total Hit

Lines:     83.9 %    347  291
Functions: 90.3 %     31   28
Branches:  59.2 %    157   93

Apart from impossible banches like scoped cleanups, the missing blocks are:
* Event idx.
* All SVQ CVQ handling.
* Hard to reproduce casuistics like a linear buffer in GPA that is split into more than one buffer in HVA, and then SVQ is saturated.
* Buggy input (no descriptors, used descriptors that are not available, moving indexes more than vq size).
* Unbinding device call notifier from QEMU vhost system.

RFC: I think the tx_packets_inorder struct is also better than the async queue for previous tests. But I'd like to know other people opinions too.

---
 tests/qtest/vhost-vdpa-test.c | 183 ++++++++++++++++++++++++++++++++--
 1 file changed, 173 insertions(+), 10 deletions(-)
tests/qtest/vhost-vdpa-test.c +173 -10
index 0192b00748d5..99a4df4433e3 100644
@@ -44,6 +44,7 @@ static int NUM_RX_BUFS = 2;
 
 typedef struct TestParameters {
     const VduseOps *ops;
+    uint64_t features;
 } TestParameters;
 
 typedef struct VdpaThread {
@@ -56,6 +57,9 @@ typedef struct VdpaThread {
 
     /* Guest memory that must be free at the end of the test */
     uint64_t qemu_mem_to_free;
+
+    /* Expected avail_idx in in_order */
+    uint16_t expected_avail_idx;
 } VdpaThread;
 
 static void *vhost_vdpa_thread_function(void *data)
@@ -163,8 +167,19 @@ static void vhost_vdpa_kick_tx_desc(VdpaThread *t, QVirtioNet *net,
     qvirtqueue_kick(qts, net->vdev, net->queues[1], kick_id);
 }
 
+/**
+ * Get the next used element of the tx queue and check properties of it. If
+ * wait is true, wait until an element is available, otherwise just check if
+ * it's already available.
+ *
+ * Note that waiting clean the ISR, so if you call this function with
+ * wait=true, another thread must call the VQ so this call returns. If you
+ * expect to fetch many descs you need to get subsequent elements with
+ * wait=false.
+ */
 static void vhost_vdpa_get_tx_pkt(QGuestAllocator *alloc, QVirtioNet *net,
-                                  uint32_t desc_idx, VdpaThread *t)
+                                  uint32_t desc_idx, VdpaThread *t,
+                                  bool wait)
 {
     g_autofree struct VduseVirtqElement *elem = NULL;
     int64_t timeout = 5 * G_TIME_SPAN_SECOND;
@@ -174,8 +189,17 @@ static void vhost_vdpa_get_tx_pkt(QGuestAllocator *alloc, QVirtioNet *net,
     uint32_t len;
 
     end_time_us = g_get_monotonic_time() + timeout;
-    qvirtio_wait_used_elem(qts, net->vdev, net->queues[1], desc_idx, &len,
-                           timeout);
+    if (wait) {
+        qvirtio_wait_used_elem(qts, net->vdev, net->queues[1], desc_idx, &len,
+                               timeout);
+    } else {
+        uint32_t got_desc_idx;
+
+        if (!qvirtqueue_get_buf(qts, net->queues[1], &got_desc_idx, &len)) {
+            g_test_fail();
+        }
+        g_assert_cmpint(got_desc_idx, ==, desc_idx);
+    }
     g_assert_cmpint(g_get_monotonic_time(), <, end_time_us);
     g_assert_cmpint(len, ==, 0);
 
@@ -190,6 +214,7 @@ typedef struct TestServer {
     gchar *vdpa_dev_path;
     gchar *tmpfs;
     int vq_read_num;
+    size_t test_cursor;
     VduseDev *vdev;
     VdpaThread vdpa_thread;
     QemuMutex data_mutex;
@@ -330,6 +355,93 @@ static const TestParameters rxtx_params = {
     .ops = &vduse_rxtx_ops,
 };
 
+static const struct  {
+    uint16_t out_num;
+    bool push;
+} tx_packets_inorder[] = {
+    /* One buffer first */
+    { .out_num = 1, .push = true },
+
+    /* Then two buffers chained */
+    { .out_num = 2, .push = true },
+
+    /* Then one buffer, but batch it with the next one */
+    { .out_num = 1, .push = false },
+    { .out_num = 1, .push = true },
+
+    /* Finally, two buffers chained again, but batch them together */
+    { .out_num = 2, .push = false },
+    { .out_num = 2, .push = true },
+};
+
+static gboolean vhost_vdpa_rxtx_inorder_handle_tx(int fd,
+                                                  GIOCondition condition,
+                                                  void *data)
+{
+    VduseVirtq *vq = data;
+    VduseDev *dev = vduse_queue_get_dev(vq);
+    TestServer *s = vduse_dev_get_priv(dev);
+
+    eventfd_read(fd, (eventfd_t[]){0});
+    do {
+        g_autofree VduseVirtqElement *elem = NULL;
+        size_t batch_size = 1;
+        elem = vduse_queue_pop(vq, sizeof(*elem));
+        if (!elem) {
+            break;
+        }
+
+        g_assert_cmpint(s->test_cursor, <, G_N_ELEMENTS(tx_packets_inorder));
+        g_assert_cmpint(elem->out_num, ==,
+                        tx_packets_inorder[s->test_cursor].out_num);
+        g_assert_cmpint(elem->in_num, ==, 0);
+
+        if (tx_packets_inorder[s->test_cursor].push) {
+            for (ssize_t i = s->test_cursor - 1;
+                 i >= 0 && !tx_packets_inorder[i].push; i--) {
+                batch_size++;
+            }
+            vduse_queue_fill(vq, elem, /* len */ 0, /* offset */ 0);
+            vduse_queue_flush(vq, batch_size);
+            vduse_queue_notify(vq);
+        }
+        s->test_cursor++;
+    } while (true);
+
+    return G_SOURCE_CONTINUE;
+}
+
+static void vduse_rxtx_inorder_enable_queue(VduseDev *dev, VduseVirtq *vq)
+{
+    TestServer *s = vduse_dev_get_priv(dev);
+
+    g_test_message("Enabling queue %d", vq->index);
+
+    if (vq->index == 1) {
+        g_assert(dev->features & (1ULL << VIRTIO_F_IN_ORDER));
+
+        /* This is the tx queue, add a source to handle it */
+        vhost_vdpa_thread_add_source_fd(&s->vdpa_thread,
+                                        vduse_queue_get_fd(vq),
+                                        vhost_vdpa_rxtx_inorder_handle_tx, vq);
+    }
+}
+
+static void vduse_rxtx_inorder_disable_queue(VduseDev *dev, VduseVirtq *vq)
+{
+    /* Queue disabled */
+}
+
+static const VduseOps vduse_rxtx_inorder_ops = {
+    .enable_queue = vduse_rxtx_inorder_enable_queue,
+    .disable_queue = vduse_rxtx_inorder_disable_queue,
+};
+
+static const TestParameters rxtx_inorder_params = {
+    .ops = &vduse_rxtx_inorder_ops,
+    .features = (1ULL << VIRTIO_F_IN_ORDER),
+};
+
 static gboolean vduse_dev_handler_source_fd(int fd, GIOCondition condition,
                                             void *data)
 {
@@ -462,7 +574,7 @@ static TestServer *test_server_new(const gchar *name,
 
     /* Disabling NOTIFY_ON_EMPTY as SVQ does not support it */
     features = (vduse_get_virtio_features() & ~(1ULL << VIRTIO_F_NOTIFY_ON_EMPTY)) |
-               (1ULL << VIRTIO_NET_F_MAC);
+               (1ULL << VIRTIO_NET_F_MAC) | params->features;
 
     server->vdev = vduse_dev_create(server->vduse_name,
                                     VIRTIO_ID_NET,
@@ -561,7 +673,7 @@ static void vhost_vdpa_test_cleanup(void *s)
     test_server_free(server);
 }
 
-static void *vhost_vdpa_test_setup(GString *cmd_line, void *arg)
+static void *vhost_vdpa_test_setup0(GString *cmd_line, void *arg)
 {
     TestServer *server = test_server_new("vdpa-memfile", arg);
 
@@ -580,6 +692,17 @@ static void *vhost_vdpa_test_setup(GString *cmd_line, void *arg)
     return server;
 }
 
+static void *vhost_vdpa_test_setup_inorder(GString *cmd_line, void *arg)
+{
+    const char *device = g_strstr_len(cmd_line->str, -1,
+                                      "-device virtio-net-pci");
+    g_assert_nonnull(device);
+    device += strlen("-device virtio-net-pci");
+    g_string_insert(cmd_line, device - cmd_line->str, ",in_order=on");
+
+    return vhost_vdpa_test_setup0(cmd_line, arg);
+}
+
 static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
 {
     TestServer *server = arg;
@@ -593,13 +716,13 @@ static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
     free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
                                             1);
     vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 1);
-    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread, true);
 
     /* Simple chain */
     free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
                                             2);
     vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 2);
-    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread, true);
 
     /* Out of order descriptors */
     free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
@@ -609,8 +732,8 @@ static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
     vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head2, 1);
     vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 1);
 
-    vhost_vdpa_get_tx_pkt(alloc, net, free_head2, &server->vdpa_thread);
-    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread);
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head2, &server->vdpa_thread, true);
+    vhost_vdpa_get_tx_pkt(alloc, net, free_head, &server->vdpa_thread, true);
 
     /* Out of order chains */
     free_head = vhost_vdpa_add_tx_pkt_descs(alloc, net, &server->vdpa_thread,
@@ -621,11 +744,45 @@ static void vhost_vdpa_tx_test(void *obj, void *arg, QGuestAllocator *alloc)
     vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head, 2);
 }
 
+static void vhost_vdpa_tx_inorder_test(void *obj, void *arg, QGuestAllocator *alloc)
+{
+    uint32_t free_head[G_N_ELEMENTS(tx_packets_inorder)];
+    TestServer *server = arg;
+    QVirtioNet *net = obj;
+    size_t free_head_idx = 0;
+
+    for (size_t i = 0; i < G_N_ELEMENTS(tx_packets_inorder); i++) {
+        uint32_t chain_len = tx_packets_inorder[i].out_num;
+        bool wait = true;
+
+        free_head[i] = vhost_vdpa_add_tx_pkt_descs(alloc, net,
+                                                   &server->vdpa_thread,
+                                                   chain_len);
+        vhost_vdpa_kick_tx_desc(&server->vdpa_thread, net, free_head[i], chain_len);
+        if (!tx_packets_inorder[i].push) {
+            continue;
+        }
+
+        /*
+         * TODO: SVQ uses the emulated VirtIO device underneath so it does not
+         * support to batch many used elements in order.  This is fragile and
+         * could change in the future, and the right solution is to add proper
+         * support in vhost_vdpa_get_tx_pkt -> qvirtio_wait_used_elem.  Let's
+         * just code the current behavior here for now.
+         */
+        for (; free_head_idx <= i; free_head_idx++) {
+            vhost_vdpa_get_tx_pkt(alloc, net, free_head[free_head_idx],
+                                  &server->vdpa_thread, wait);
+            wait = false;
+        }
+    }
+}
+
 static void register_vhost_vdpa_test(void)
 {
     /* TODO: void * discards const qualifier */
     QOSGraphTestOptions opts = {
-        .before = vhost_vdpa_test_setup,
+        .before = vhost_vdpa_test_setup0,
         .subprocess = true,
         .arg = (void *)&mem_read_params,
     };
@@ -638,5 +795,11 @@ static void register_vhost_vdpa_test(void)
     qos_add_test("vhost-vdpa/rxtx",
                  "virtio-net",
                  vhost_vdpa_tx_test, &opts);
+
+    opts.before = vhost_vdpa_test_setup_inorder;
+    opts.arg = (void *)&rxtx_inorder_params;
+    qos_add_test("vhost-vdpa/rxtx-inorder",
+                 "virtio-net",
+                 vhost_vdpa_tx_inorder_test, &opts);
 }
 libqos_init(register_vhost_vdpa_test);
Signature · 1 line
2.53.0