Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / tests / qtest / vhost-user-test.c
blobd4e437265f6630d376540e19051d22f2bc568151
1 /*
2 * QTest testcase for the vhost-user
4 * Copyright (c) 2014 Virtual Open Systems Sarl.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
13 #include "libqtest-single.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qemu/config-file.h"
17 #include "qemu/option.h"
18 #include "qemu/range.h"
19 #include "qemu/sockets.h"
20 #include "chardev/char-fe.h"
21 #include "qemu/memfd.h"
22 #include "qemu/module.h"
23 #include "sysemu/sysemu.h"
24 #include "libqos/libqos.h"
25 #include "libqos/pci-pc.h"
26 #include "libqos/virtio-pci.h"
28 #include "libqos/malloc-pc.h"
29 #include "libqos/qgraph_internal.h"
30 #include "hw/virtio/virtio-net.h"
32 #include "standard-headers/linux/vhost_types.h"
33 #include "standard-headers/linux/virtio_ids.h"
34 #include "standard-headers/linux/virtio_net.h"
35 #include "standard-headers/linux/virtio_gpio.h"
36 #include "standard-headers/linux/virtio_scmi.h"
38 #ifdef CONFIG_LINUX
39 #include <sys/vfs.h>
40 #endif
43 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
44 "mem-path=%s,share=on -numa node,memdev=mem"
45 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
46 " -numa node,memdev=mem"
47 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
48 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce=on"
50 #define HUGETLBFS_MAGIC 0x958458f6
52 /*********** FROM hw/virtio/vhost-user.c *************************************/
54 #define VHOST_MEMORY_MAX_NREGIONS 8
55 #define VHOST_MAX_VIRTQUEUES 0x100
57 #define VHOST_USER_F_PROTOCOL_FEATURES 30
58 #define VIRTIO_F_VERSION_1 32
60 #define VHOST_USER_PROTOCOL_F_MQ 0
61 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
62 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
63 #define VHOST_USER_PROTOCOL_F_CONFIG 9
65 #define VHOST_LOG_PAGE 0x1000
67 typedef enum VhostUserRequest {
68 VHOST_USER_NONE = 0,
69 VHOST_USER_GET_FEATURES = 1,
70 VHOST_USER_SET_FEATURES = 2,
71 VHOST_USER_SET_OWNER = 3,
72 VHOST_USER_RESET_OWNER = 4,
73 VHOST_USER_SET_MEM_TABLE = 5,
74 VHOST_USER_SET_LOG_BASE = 6,
75 VHOST_USER_SET_LOG_FD = 7,
76 VHOST_USER_SET_VRING_NUM = 8,
77 VHOST_USER_SET_VRING_ADDR = 9,
78 VHOST_USER_SET_VRING_BASE = 10,
79 VHOST_USER_GET_VRING_BASE = 11,
80 VHOST_USER_SET_VRING_KICK = 12,
81 VHOST_USER_SET_VRING_CALL = 13,
82 VHOST_USER_SET_VRING_ERR = 14,
83 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
84 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
85 VHOST_USER_GET_QUEUE_NUM = 17,
86 VHOST_USER_SET_VRING_ENABLE = 18,
87 VHOST_USER_GET_CONFIG = 24,
88 VHOST_USER_SET_CONFIG = 25,
89 VHOST_USER_MAX
90 } VhostUserRequest;
92 typedef struct VhostUserMemoryRegion {
93 uint64_t guest_phys_addr;
94 uint64_t memory_size;
95 uint64_t userspace_addr;
96 uint64_t mmap_offset;
97 } VhostUserMemoryRegion;
99 typedef struct VhostUserMemory {
100 uint32_t nregions;
101 uint32_t padding;
102 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
103 } VhostUserMemory;
105 typedef struct VhostUserLog {
106 uint64_t mmap_size;
107 uint64_t mmap_offset;
108 } VhostUserLog;
110 typedef struct VhostUserMsg {
111 VhostUserRequest request;
113 #define VHOST_USER_VERSION_MASK (0x3)
114 #define VHOST_USER_REPLY_MASK (0x1<<2)
115 uint32_t flags;
116 uint32_t size; /* the following payload size */
117 union {
118 #define VHOST_USER_VRING_IDX_MASK (0xff)
119 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
120 uint64_t u64;
121 struct vhost_vring_state state;
122 struct vhost_vring_addr addr;
123 VhostUserMemory memory;
124 VhostUserLog log;
125 } payload;
126 } QEMU_PACKED VhostUserMsg;
128 static VhostUserMsg m __attribute__ ((unused));
129 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
130 + sizeof(m.flags) \
131 + sizeof(m.size))
133 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
135 /* The version of the protocol we support */
136 #define VHOST_USER_VERSION (0x1)
137 /*****************************************************************************/
139 enum {
140 TEST_FLAGS_OK,
141 TEST_FLAGS_DISCONNECT,
142 TEST_FLAGS_BAD,
143 TEST_FLAGS_END,
146 enum {
147 VHOST_USER_NET,
148 VHOST_USER_GPIO,
149 VHOST_USER_SCMI,
152 typedef struct TestServer {
153 gchar *socket_path;
154 gchar *mig_path;
155 gchar *chr_name;
156 gchar *tmpfs;
157 CharBackend chr;
158 int fds_num;
159 int fds[VHOST_MEMORY_MAX_NREGIONS];
160 VhostUserMemory memory;
161 GMainContext *context;
162 GMainLoop *loop;
163 GThread *thread;
164 GMutex data_mutex;
165 GCond data_cond;
166 int log_fd;
167 uint64_t rings;
168 bool test_fail;
169 int test_flags;
170 int queues;
171 struct vhost_user_ops *vu_ops;
172 } TestServer;
174 struct vhost_user_ops {
175 /* Device types. */
176 int type;
177 void (*append_opts)(TestServer *s, GString *cmd_line,
178 const char *chr_opts);
180 /* VHOST-USER commands. */
181 uint64_t (*get_features)(TestServer *s);
182 void (*set_features)(TestServer *s, CharBackend *chr,
183 VhostUserMsg *msg);
184 void (*get_protocol_features)(TestServer *s,
185 CharBackend *chr, VhostUserMsg *msg);
188 static const char *init_hugepagefs(void);
189 static TestServer *test_server_new(const gchar *name,
190 struct vhost_user_ops *ops);
191 static void test_server_free(TestServer *server);
192 static void test_server_listen(TestServer *server);
194 enum test_memfd {
195 TEST_MEMFD_AUTO,
196 TEST_MEMFD_YES,
197 TEST_MEMFD_NO,
200 static void append_vhost_net_opts(TestServer *s, GString *cmd_line,
201 const char *chr_opts)
203 g_string_append_printf(cmd_line, QEMU_CMD_CHR QEMU_CMD_NETDEV,
204 s->chr_name, s->socket_path,
205 chr_opts, s->chr_name);
209 * For GPIO there are no other magic devices we need to add (like
210 * block or netdev) so all we need to worry about is the vhost-user
211 * chardev socket.
213 static void append_vhost_gpio_opts(TestServer *s, GString *cmd_line,
214 const char *chr_opts)
216 g_string_append_printf(cmd_line, QEMU_CMD_CHR,
217 s->chr_name, s->socket_path,
218 chr_opts);
221 static void append_mem_opts(TestServer *server, GString *cmd_line,
222 int size, enum test_memfd memfd)
224 if (memfd == TEST_MEMFD_AUTO) {
225 memfd = qemu_memfd_check(MFD_ALLOW_SEALING) ? TEST_MEMFD_YES
226 : TEST_MEMFD_NO;
229 if (memfd == TEST_MEMFD_YES) {
230 g_string_append_printf(cmd_line, QEMU_CMD_MEMFD, size, size);
231 } else {
232 const char *root = init_hugepagefs() ? : server->tmpfs;
234 g_string_append_printf(cmd_line, QEMU_CMD_MEM, size, size, root);
238 static bool wait_for_fds(TestServer *s)
240 gint64 end_time;
241 bool got_region;
242 int i;
244 g_mutex_lock(&s->data_mutex);
246 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
247 while (!s->fds_num) {
248 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
249 /* timeout has passed */
250 g_assert(s->fds_num);
251 break;
255 /* check for sanity */
256 g_assert_cmpint(s->fds_num, >, 0);
257 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
259 g_mutex_unlock(&s->data_mutex);
261 got_region = false;
262 for (i = 0; i < s->memory.nregions; ++i) {
263 VhostUserMemoryRegion *reg = &s->memory.regions[i];
264 if (reg->guest_phys_addr == 0) {
265 got_region = true;
266 break;
269 if (!got_region) {
270 g_test_skip("No memory at address 0x0");
272 return got_region;
275 static void read_guest_mem_server(QTestState *qts, TestServer *s)
277 uint8_t *guest_mem;
278 int i, j;
279 size_t size;
281 g_mutex_lock(&s->data_mutex);
283 /* iterate all regions */
284 for (i = 0; i < s->fds_num; i++) {
286 /* We'll check only the region starting at 0x0 */
287 if (s->memory.regions[i].guest_phys_addr != 0x0) {
288 continue;
291 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
293 size = s->memory.regions[i].memory_size +
294 s->memory.regions[i].mmap_offset;
296 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
297 MAP_SHARED, s->fds[i], 0);
299 g_assert(guest_mem != MAP_FAILED);
300 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
302 for (j = 0; j < 1024; j++) {
303 uint32_t a = qtest_readb(qts, s->memory.regions[i].guest_phys_addr + j);
304 uint32_t b = guest_mem[j];
306 g_assert_cmpint(a, ==, b);
309 munmap(guest_mem, s->memory.regions[i].memory_size);
312 g_mutex_unlock(&s->data_mutex);
315 static void *thread_function(void *data)
317 GMainLoop *loop = data;
318 g_main_loop_run(loop);
319 return NULL;
322 static int chr_can_read(void *opaque)
324 return VHOST_USER_HDR_SIZE;
327 static void chr_read(void *opaque, const uint8_t *buf, int size)
329 g_autoptr(GError) err = NULL;
330 TestServer *s = opaque;
331 CharBackend *chr = &s->chr;
332 VhostUserMsg msg;
333 uint8_t *p = (uint8_t *) &msg;
334 int fd = -1;
336 if (s->test_fail) {
337 qemu_chr_fe_disconnect(chr);
338 /* now switch to non-failure */
339 s->test_fail = false;
342 if (size != VHOST_USER_HDR_SIZE) {
343 qos_printf("%s: Wrong message size received %d\n", __func__, size);
344 return;
347 g_mutex_lock(&s->data_mutex);
348 memcpy(p, buf, VHOST_USER_HDR_SIZE);
350 if (msg.size) {
351 p += VHOST_USER_HDR_SIZE;
352 size = qemu_chr_fe_read_all(chr, p, msg.size);
353 if (size != msg.size) {
354 qos_printf("%s: Wrong message size received %d != %d\n",
355 __func__, size, msg.size);
356 goto out;
360 switch (msg.request) {
361 case VHOST_USER_GET_FEATURES:
362 /* Mandatory for tests to define get_features */
363 g_assert(s->vu_ops->get_features);
365 /* send back features to qemu */
366 msg.flags |= VHOST_USER_REPLY_MASK;
367 msg.size = sizeof(m.payload.u64);
369 if (s->test_flags >= TEST_FLAGS_BAD) {
370 msg.payload.u64 = 0;
371 s->test_flags = TEST_FLAGS_END;
372 } else {
373 msg.payload.u64 = s->vu_ops->get_features(s);
376 qemu_chr_fe_write_all(chr, (uint8_t *) &msg,
377 VHOST_USER_HDR_SIZE + msg.size);
378 break;
380 case VHOST_USER_SET_FEATURES:
381 if (s->vu_ops->set_features) {
382 s->vu_ops->set_features(s, chr, &msg);
384 break;
386 case VHOST_USER_SET_OWNER:
388 * We don't need to do anything here, the remote is just
389 * letting us know it is in charge. Just log it.
391 qos_printf("set_owner: start of session\n");
392 break;
394 case VHOST_USER_GET_PROTOCOL_FEATURES:
395 if (s->vu_ops->get_protocol_features) {
396 s->vu_ops->get_protocol_features(s, chr, &msg);
398 break;
400 case VHOST_USER_GET_CONFIG:
402 * Treat GET_CONFIG as a NOP and just reply and let the guest
403 * consider we have updated its memory. Tests currently don't
404 * require working configs.
406 msg.flags |= VHOST_USER_REPLY_MASK;
407 p = (uint8_t *) &msg;
408 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
409 break;
411 case VHOST_USER_SET_PROTOCOL_FEATURES:
413 * We did set VHOST_USER_F_PROTOCOL_FEATURES so its valid for
414 * the remote end to send this. There is no handshake reply so
415 * just log the details for debugging.
417 qos_printf("set_protocol_features: 0x%"PRIx64 "\n", msg.payload.u64);
418 break;
421 * A real vhost-user backend would actually set the size and
422 * address of the vrings but we can simply report them.
424 case VHOST_USER_SET_VRING_NUM:
425 qos_printf("set_vring_num: %d/%d\n",
426 msg.payload.state.index, msg.payload.state.num);
427 break;
428 case VHOST_USER_SET_VRING_ADDR:
429 qos_printf("set_vring_addr: 0x%"PRIx64"/0x%"PRIx64"/0x%"PRIx64"\n",
430 msg.payload.addr.avail_user_addr,
431 msg.payload.addr.desc_user_addr,
432 msg.payload.addr.used_user_addr);
433 break;
435 case VHOST_USER_GET_VRING_BASE:
436 /* send back vring base to qemu */
437 msg.flags |= VHOST_USER_REPLY_MASK;
438 msg.size = sizeof(m.payload.state);
439 msg.payload.state.num = 0;
440 p = (uint8_t *) &msg;
441 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
443 assert(msg.payload.state.index < s->queues * 2);
444 s->rings &= ~(0x1ULL << msg.payload.state.index);
445 g_cond_broadcast(&s->data_cond);
446 break;
448 case VHOST_USER_SET_MEM_TABLE:
449 /* received the mem table */
450 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
451 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
452 G_N_ELEMENTS(s->fds));
454 /* signal the test that it can continue */
455 g_cond_broadcast(&s->data_cond);
456 break;
458 case VHOST_USER_SET_VRING_KICK:
459 case VHOST_USER_SET_VRING_CALL:
460 /* consume the fd */
461 qemu_chr_fe_get_msgfds(chr, &fd, 1);
463 * This is a non-blocking eventfd.
464 * The receive function forces it to be blocking,
465 * so revert it back to non-blocking.
467 g_unix_set_fd_nonblocking(fd, true, &err);
468 g_assert_no_error(err);
469 break;
471 case VHOST_USER_SET_LOG_BASE:
472 if (s->log_fd != -1) {
473 close(s->log_fd);
474 s->log_fd = -1;
476 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
477 msg.flags |= VHOST_USER_REPLY_MASK;
478 msg.size = 0;
479 p = (uint8_t *) &msg;
480 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
482 g_cond_broadcast(&s->data_cond);
483 break;
485 case VHOST_USER_SET_VRING_BASE:
486 assert(msg.payload.state.index < s->queues * 2);
487 s->rings |= 0x1ULL << msg.payload.state.index;
488 g_cond_broadcast(&s->data_cond);
489 break;
491 case VHOST_USER_GET_QUEUE_NUM:
492 msg.flags |= VHOST_USER_REPLY_MASK;
493 msg.size = sizeof(m.payload.u64);
494 msg.payload.u64 = s->queues;
495 p = (uint8_t *) &msg;
496 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
497 break;
499 case VHOST_USER_SET_VRING_ENABLE:
501 * Another case we ignore as we don't need to respond. With a
502 * fully functioning vhost-user we would enable/disable the
503 * vring monitoring.
505 qos_printf("set_vring(%d)=%s\n", msg.payload.state.index,
506 msg.payload.state.num ? "enabled" : "disabled");
507 break;
509 default:
510 qos_printf("vhost-user: un-handled message: %d\n", msg.request);
511 break;
514 out:
515 g_mutex_unlock(&s->data_mutex);
518 static const char *init_hugepagefs(void)
520 #ifdef CONFIG_LINUX
521 static const char *hugepagefs;
522 const char *path = getenv("QTEST_HUGETLBFS_PATH");
523 struct statfs fs;
524 int ret;
526 if (hugepagefs) {
527 return hugepagefs;
529 if (!path) {
530 return NULL;
533 if (access(path, R_OK | W_OK | X_OK)) {
534 qos_printf("access on path (%s): %s", path, strerror(errno));
535 g_test_fail();
536 return NULL;
539 do {
540 ret = statfs(path, &fs);
541 } while (ret != 0 && errno == EINTR);
543 if (ret != 0) {
544 qos_printf("statfs on path (%s): %s", path, strerror(errno));
545 g_test_fail();
546 return NULL;
549 if (fs.f_type != HUGETLBFS_MAGIC) {
550 qos_printf("Warning: path not on HugeTLBFS: %s", path);
551 g_test_fail();
552 return NULL;
555 hugepagefs = path;
556 return hugepagefs;
557 #else
558 return NULL;
559 #endif
562 static TestServer *test_server_new(const gchar *name,
563 struct vhost_user_ops *ops)
565 TestServer *server = g_new0(TestServer, 1);
566 g_autofree const char *tmpfs = NULL;
567 GError *err = NULL;
569 server->context = g_main_context_new();
570 server->loop = g_main_loop_new(server->context, FALSE);
572 /* run the main loop thread so the chardev may operate */
573 server->thread = g_thread_new(NULL, thread_function, server->loop);
575 tmpfs = g_dir_make_tmp("vhost-test-XXXXXX", &err);
576 if (!tmpfs) {
577 g_test_message("Can't create temporary directory in %s: %s",
578 g_get_tmp_dir(), err->message);
579 g_error_free(err);
581 g_assert(tmpfs);
583 server->tmpfs = g_strdup(tmpfs);
584 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
585 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
586 server->chr_name = g_strdup_printf("chr-%s", name);
588 g_mutex_init(&server->data_mutex);
589 g_cond_init(&server->data_cond);
591 server->log_fd = -1;
592 server->queues = 1;
593 server->vu_ops = ops;
595 return server;
598 static void chr_event(void *opaque, QEMUChrEvent event)
600 TestServer *s = opaque;
602 if (s->test_flags == TEST_FLAGS_END &&
603 event == CHR_EVENT_CLOSED) {
604 s->test_flags = TEST_FLAGS_OK;
608 static void test_server_create_chr(TestServer *server, const gchar *opt)
610 g_autofree gchar *chr_path = g_strdup_printf("unix:%s%s",
611 server->socket_path, opt);
612 Chardev *chr;
614 chr = qemu_chr_new(server->chr_name, chr_path, server->context);
615 g_assert(chr);
617 qemu_chr_fe_init(&server->chr, chr, &error_abort);
618 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
619 chr_event, NULL, server, server->context, true);
622 static void test_server_listen(TestServer *server)
624 test_server_create_chr(server, ",server=on,wait=off");
627 static void test_server_free(TestServer *server)
629 int i, ret;
631 /* finish the helper thread and dispatch pending sources */
632 g_main_loop_quit(server->loop);
633 g_thread_join(server->thread);
634 while (g_main_context_pending(NULL)) {
635 g_main_context_iteration(NULL, TRUE);
638 unlink(server->socket_path);
639 g_free(server->socket_path);
641 unlink(server->mig_path);
642 g_free(server->mig_path);
644 ret = rmdir(server->tmpfs);
645 if (ret != 0) {
646 g_test_message("unable to rmdir: path (%s): %s",
647 server->tmpfs, strerror(errno));
649 g_free(server->tmpfs);
651 qemu_chr_fe_deinit(&server->chr, true);
653 for (i = 0; i < server->fds_num; i++) {
654 close(server->fds[i]);
657 if (server->log_fd != -1) {
658 close(server->log_fd);
661 g_free(server->chr_name);
663 g_main_loop_unref(server->loop);
664 g_main_context_unref(server->context);
665 g_cond_clear(&server->data_cond);
666 g_mutex_clear(&server->data_mutex);
667 g_free(server);
670 static void wait_for_log_fd(TestServer *s)
672 gint64 end_time;
674 g_mutex_lock(&s->data_mutex);
675 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
676 while (s->log_fd == -1) {
677 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
678 /* timeout has passed */
679 g_assert(s->log_fd != -1);
680 break;
684 g_mutex_unlock(&s->data_mutex);
687 static void write_guest_mem(TestServer *s, uint32_t seed)
689 uint32_t *guest_mem;
690 int i, j;
691 size_t size;
693 /* iterate all regions */
694 for (i = 0; i < s->fds_num; i++) {
696 /* We'll write only the region statring at 0x0 */
697 if (s->memory.regions[i].guest_phys_addr != 0x0) {
698 continue;
701 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
703 size = s->memory.regions[i].memory_size +
704 s->memory.regions[i].mmap_offset;
706 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
707 MAP_SHARED, s->fds[i], 0);
709 g_assert(guest_mem != MAP_FAILED);
710 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
712 for (j = 0; j < 256; j++) {
713 guest_mem[j] = seed + j;
716 munmap(guest_mem, s->memory.regions[i].memory_size);
717 break;
721 static guint64 get_log_size(TestServer *s)
723 guint64 log_size = 0;
724 int i;
726 for (i = 0; i < s->memory.nregions; ++i) {
727 VhostUserMemoryRegion *reg = &s->memory.regions[i];
728 guint64 last = range_get_last(reg->guest_phys_addr,
729 reg->memory_size);
730 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
733 return log_size;
736 typedef struct TestMigrateSource {
737 GSource source;
738 TestServer *src;
739 TestServer *dest;
740 } TestMigrateSource;
742 static gboolean
743 test_migrate_source_check(GSource *source)
745 TestMigrateSource *t = (TestMigrateSource *)source;
746 gboolean overlap = t->src->rings && t->dest->rings;
748 g_assert(!overlap);
750 return FALSE;
753 GSourceFuncs test_migrate_source_funcs = {
754 .check = test_migrate_source_check,
757 static void vhost_user_test_cleanup(void *s)
759 TestServer *server = s;
761 qos_invalidate_command_line();
762 test_server_free(server);
765 static void *vhost_user_test_setup(GString *cmd_line, void *arg)
767 TestServer *server = test_server_new("vhost-user-test", arg);
768 test_server_listen(server);
770 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_AUTO);
771 server->vu_ops->append_opts(server, cmd_line, "");
773 g_test_queue_destroy(vhost_user_test_cleanup, server);
775 return server;
778 static void *vhost_user_test_setup_memfd(GString *cmd_line, void *arg)
780 TestServer *server = test_server_new("vhost-user-test", arg);
781 test_server_listen(server);
783 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_YES);
784 server->vu_ops->append_opts(server, cmd_line, "");
786 g_test_queue_destroy(vhost_user_test_cleanup, server);
788 return server;
791 static void test_read_guest_mem(void *obj, void *arg, QGuestAllocator *alloc)
793 TestServer *server = arg;
795 if (!wait_for_fds(server)) {
796 return;
799 read_guest_mem_server(global_qtest, server);
802 static void test_migrate(void *obj, void *arg, QGuestAllocator *alloc)
804 TestServer *s = arg;
805 TestServer *dest;
806 GString *dest_cmdline;
807 char *uri;
808 QTestState *to;
809 GSource *source;
810 QDict *rsp;
811 guint8 *log;
812 guint64 size;
814 if (!wait_for_fds(s)) {
815 return;
818 dest = test_server_new("dest", s->vu_ops);
819 dest_cmdline = g_string_new(qos_get_current_command_line());
820 uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
822 size = get_log_size(s);
823 g_assert_cmpint(size, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
825 test_server_listen(dest);
826 g_string_append_printf(dest_cmdline, " -incoming %s", uri);
827 append_mem_opts(dest, dest_cmdline, 256, TEST_MEMFD_AUTO);
828 dest->vu_ops->append_opts(dest, dest_cmdline, "");
829 to = qtest_init(dest_cmdline->str);
831 /* This would be where you call qos_allocate_objects(to, NULL), if you want
832 * to talk to the QVirtioNet object on the destination.
835 source = g_source_new(&test_migrate_source_funcs,
836 sizeof(TestMigrateSource));
837 ((TestMigrateSource *)source)->src = s;
838 ((TestMigrateSource *)source)->dest = dest;
839 g_source_attach(source, s->context);
841 /* slow down migration to have time to fiddle with log */
842 /* TODO: qtest could learn to break on some places */
843 rsp = qmp("{ 'execute': 'migrate-set-parameters',"
844 "'arguments': { 'max-bandwidth': 10 } }");
845 g_assert(qdict_haskey(rsp, "return"));
846 qobject_unref(rsp);
848 rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
849 g_assert(qdict_haskey(rsp, "return"));
850 qobject_unref(rsp);
852 wait_for_log_fd(s);
854 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
855 g_assert(log != MAP_FAILED);
857 /* modify first page */
858 write_guest_mem(s, 0x42);
859 log[0] = 1;
860 munmap(log, size);
862 /* speed things up */
863 rsp = qmp("{ 'execute': 'migrate-set-parameters',"
864 "'arguments': { 'max-bandwidth': 0 } }");
865 g_assert(qdict_haskey(rsp, "return"));
866 qobject_unref(rsp);
868 qmp_eventwait("STOP");
869 qtest_qmp_eventwait(to, "RESUME");
871 g_assert(wait_for_fds(dest));
872 read_guest_mem_server(to, dest);
874 g_source_destroy(source);
875 g_source_unref(source);
877 qtest_quit(to);
878 test_server_free(dest);
879 g_free(uri);
880 g_string_free(dest_cmdline, true);
883 static void wait_for_rings_started(TestServer *s, size_t count)
885 gint64 end_time;
887 g_mutex_lock(&s->data_mutex);
888 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
889 while (ctpop64(s->rings) != count) {
890 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
891 /* timeout has passed */
892 g_assert_cmpint(ctpop64(s->rings), ==, count);
893 break;
897 g_mutex_unlock(&s->data_mutex);
900 static inline void test_server_connect(TestServer *server)
902 test_server_create_chr(server, ",reconnect=1");
905 static gboolean
906 reconnect_cb(gpointer user_data)
908 TestServer *s = user_data;
910 qemu_chr_fe_disconnect(&s->chr);
912 return FALSE;
915 static gpointer
916 connect_thread(gpointer data)
918 TestServer *s = data;
920 /* wait for qemu to start before first try, to avoid extra warnings */
921 g_usleep(G_USEC_PER_SEC);
922 test_server_connect(s);
924 return NULL;
927 static void *vhost_user_test_setup_reconnect(GString *cmd_line, void *arg)
929 TestServer *s = test_server_new("reconnect", arg);
931 g_thread_new("connect", connect_thread, s);
932 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
933 s->vu_ops->append_opts(s, cmd_line, ",server=on");
935 g_test_queue_destroy(vhost_user_test_cleanup, s);
937 return s;
940 static void test_reconnect(void *obj, void *arg, QGuestAllocator *alloc)
942 TestServer *s = arg;
943 GSource *src;
945 if (!wait_for_fds(s)) {
946 return;
949 wait_for_rings_started(s, 2);
951 /* reconnect */
952 s->fds_num = 0;
953 s->rings = 0;
954 src = g_idle_source_new();
955 g_source_set_callback(src, reconnect_cb, s, NULL);
956 g_source_attach(src, s->context);
957 g_source_unref(src);
958 g_assert(wait_for_fds(s));
959 wait_for_rings_started(s, 2);
962 static void *vhost_user_test_setup_connect_fail(GString *cmd_line, void *arg)
964 TestServer *s = test_server_new("connect-fail", arg);
966 s->test_fail = true;
968 g_thread_new("connect", connect_thread, s);
969 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
970 s->vu_ops->append_opts(s, cmd_line, ",server=on");
972 g_test_queue_destroy(vhost_user_test_cleanup, s);
974 return s;
977 static void *vhost_user_test_setup_flags_mismatch(GString *cmd_line, void *arg)
979 TestServer *s = test_server_new("flags-mismatch", arg);
981 s->test_flags = TEST_FLAGS_DISCONNECT;
983 g_thread_new("connect", connect_thread, s);
984 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
985 s->vu_ops->append_opts(s, cmd_line, ",server=on");
987 g_test_queue_destroy(vhost_user_test_cleanup, s);
989 return s;
992 static void test_vhost_user_started(void *obj, void *arg, QGuestAllocator *alloc)
994 TestServer *s = arg;
996 if (!wait_for_fds(s)) {
997 return;
999 wait_for_rings_started(s, 2);
1002 static void *vhost_user_test_setup_multiqueue(GString *cmd_line, void *arg)
1004 TestServer *s = vhost_user_test_setup(cmd_line, arg);
1006 s->queues = 2;
1007 g_string_append_printf(cmd_line,
1008 " -set netdev.hs0.queues=%d"
1009 " -global virtio-net-pci.vectors=%d",
1010 s->queues, s->queues * 2 + 2);
1012 return s;
1015 static void test_multiqueue(void *obj, void *arg, QGuestAllocator *alloc)
1017 TestServer *s = arg;
1019 wait_for_rings_started(s, s->queues * 2);
1023 static uint64_t vu_net_get_features(TestServer *s)
1025 uint64_t features = 0x1ULL << VHOST_F_LOG_ALL |
1026 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
1028 if (s->queues > 1) {
1029 features |= 0x1ULL << VIRTIO_NET_F_MQ;
1032 return features;
1035 static void vu_net_set_features(TestServer *s, CharBackend *chr,
1036 VhostUserMsg *msg)
1038 g_assert(msg->payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES));
1039 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
1040 qemu_chr_fe_disconnect(chr);
1041 s->test_flags = TEST_FLAGS_BAD;
1045 static void vu_net_get_protocol_features(TestServer *s, CharBackend *chr,
1046 VhostUserMsg *msg)
1048 /* send back features to qemu */
1049 msg->flags |= VHOST_USER_REPLY_MASK;
1050 msg->size = sizeof(m.payload.u64);
1051 msg->payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
1052 msg->payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN;
1053 if (s->queues > 1) {
1054 msg->payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
1056 qemu_chr_fe_write_all(chr, (uint8_t *)msg, VHOST_USER_HDR_SIZE + msg->size);
1059 /* Each VHOST-USER device should have its ops structure defined. */
1060 static struct vhost_user_ops g_vu_net_ops = {
1061 .type = VHOST_USER_NET,
1063 .append_opts = append_vhost_net_opts,
1065 .get_features = vu_net_get_features,
1066 .set_features = vu_net_set_features,
1067 .get_protocol_features = vu_net_get_protocol_features,
1070 static void register_vhost_user_test(void)
1072 QOSGraphTestOptions opts = {
1073 .before = vhost_user_test_setup,
1074 .subprocess = true,
1075 .arg = &g_vu_net_ops,
1078 qemu_add_opts(&qemu_chardev_opts);
1080 qos_add_test("vhost-user/read-guest-mem/memfile",
1081 "virtio-net",
1082 test_read_guest_mem, &opts);
1084 if (qemu_memfd_check(MFD_ALLOW_SEALING)) {
1085 opts.before = vhost_user_test_setup_memfd;
1086 qos_add_test("vhost-user/read-guest-mem/memfd",
1087 "virtio-net",
1088 test_read_guest_mem, &opts);
1091 qos_add_test("vhost-user/migrate",
1092 "virtio-net",
1093 test_migrate, &opts);
1095 opts.before = vhost_user_test_setup_reconnect;
1096 qos_add_test("vhost-user/reconnect", "virtio-net",
1097 test_reconnect, &opts);
1099 opts.before = vhost_user_test_setup_connect_fail;
1100 qos_add_test("vhost-user/connect-fail", "virtio-net",
1101 test_vhost_user_started, &opts);
1103 opts.before = vhost_user_test_setup_flags_mismatch;
1104 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
1105 test_vhost_user_started, &opts);
1107 opts.before = vhost_user_test_setup_multiqueue;
1108 opts.edge.extra_device_opts = "mq=on";
1109 qos_add_test("vhost-user/multiqueue",
1110 "virtio-net",
1111 test_multiqueue, &opts);
1113 libqos_init(register_vhost_user_test);
1115 static uint64_t vu_gpio_get_features(TestServer *s)
1117 return 0x1ULL << VIRTIO_F_VERSION_1 |
1118 0x1ULL << VIRTIO_GPIO_F_IRQ |
1119 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
1123 * This stub can't handle all the message types but we should reply
1124 * that we support VHOST_USER_PROTOCOL_F_CONFIG as gpio would use it
1125 * talking to a read vhost-user daemon.
1127 static void vu_gpio_get_protocol_features(TestServer *s, CharBackend *chr,
1128 VhostUserMsg *msg)
1130 /* send back features to qemu */
1131 msg->flags |= VHOST_USER_REPLY_MASK;
1132 msg->size = sizeof(m.payload.u64);
1133 msg->payload.u64 = 1ULL << VHOST_USER_PROTOCOL_F_CONFIG;
1135 qemu_chr_fe_write_all(chr, (uint8_t *)msg, VHOST_USER_HDR_SIZE + msg->size);
1138 static struct vhost_user_ops g_vu_gpio_ops = {
1139 .type = VHOST_USER_GPIO,
1141 .append_opts = append_vhost_gpio_opts,
1143 .get_features = vu_gpio_get_features,
1144 .set_features = vu_net_set_features,
1145 .get_protocol_features = vu_gpio_get_protocol_features,
1148 static void register_vhost_gpio_test(void)
1150 QOSGraphTestOptions opts = {
1151 .before = vhost_user_test_setup,
1152 .subprocess = true,
1153 .arg = &g_vu_gpio_ops,
1156 qemu_add_opts(&qemu_chardev_opts);
1158 qos_add_test("read-guest-mem/memfile",
1159 "vhost-user-gpio", test_read_guest_mem, &opts);
1161 libqos_init(register_vhost_gpio_test);
1163 static uint64_t vu_scmi_get_features(TestServer *s)
1165 return 0x1ULL << VIRTIO_F_VERSION_1 |
1166 0x1ULL << VIRTIO_SCMI_F_P2A_CHANNELS |
1167 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
1170 static void vu_scmi_get_protocol_features(TestServer *s, CharBackend *chr,
1171 VhostUserMsg *msg)
1173 msg->flags |= VHOST_USER_REPLY_MASK;
1174 msg->size = sizeof(m.payload.u64);
1175 msg->payload.u64 = 1ULL << VHOST_USER_PROTOCOL_F_MQ;
1177 qemu_chr_fe_write_all(chr, (uint8_t *)msg, VHOST_USER_HDR_SIZE + msg->size);
1180 static struct vhost_user_ops g_vu_scmi_ops = {
1181 .type = VHOST_USER_SCMI,
1183 .append_opts = append_vhost_gpio_opts,
1185 .get_features = vu_scmi_get_features,
1186 .set_features = vu_net_set_features,
1187 .get_protocol_features = vu_scmi_get_protocol_features,
1190 static void register_vhost_scmi_test(void)
1192 QOSGraphTestOptions opts = {
1193 .before = vhost_user_test_setup,
1194 .subprocess = true,
1195 .arg = &g_vu_scmi_ops,
1198 qemu_add_opts(&qemu_chardev_opts);
1200 qos_add_test("scmi/read-guest-mem/memfile",
1201 "vhost-user-scmi", test_read_guest_mem, &opts);
1203 libqos_init(register_vhost_scmi_test);