s390x/tcg: Implement VECTOR COUNT TRAILING ZEROS
[qemu/ar7.git] / tests / vhost-user-test.c
blob3817966010bd89df534680edd5aa5e0145c041c8
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.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 "sysemu/sysemu.h"
23 #include "libqos/libqos.h"
24 #include "libqos/pci-pc.h"
25 #include "libqos/virtio-pci.h"
27 #include "libqos/malloc-pc.h"
28 #include "hw/virtio/virtio-net.h"
30 #include "standard-headers/linux/vhost_types.h"
31 #include "standard-headers/linux/virtio_ids.h"
32 #include "standard-headers/linux/virtio_net.h"
34 #ifdef CONFIG_LINUX
35 #include <sys/vfs.h>
36 #endif
39 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
40 "mem-path=%s,share=on -numa node,memdev=mem"
41 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
42 " -numa node,memdev=mem"
43 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
44 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce"
46 #define HUGETLBFS_MAGIC 0x958458f6
48 /*********** FROM hw/virtio/vhost-user.c *************************************/
50 #define VHOST_MEMORY_MAX_NREGIONS 8
51 #define VHOST_MAX_VIRTQUEUES 0x100
53 #define VHOST_USER_F_PROTOCOL_FEATURES 30
54 #define VHOST_USER_PROTOCOL_F_MQ 0
55 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
56 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
58 #define VHOST_LOG_PAGE 0x1000
60 typedef enum VhostUserRequest {
61 VHOST_USER_NONE = 0,
62 VHOST_USER_GET_FEATURES = 1,
63 VHOST_USER_SET_FEATURES = 2,
64 VHOST_USER_SET_OWNER = 3,
65 VHOST_USER_RESET_OWNER = 4,
66 VHOST_USER_SET_MEM_TABLE = 5,
67 VHOST_USER_SET_LOG_BASE = 6,
68 VHOST_USER_SET_LOG_FD = 7,
69 VHOST_USER_SET_VRING_NUM = 8,
70 VHOST_USER_SET_VRING_ADDR = 9,
71 VHOST_USER_SET_VRING_BASE = 10,
72 VHOST_USER_GET_VRING_BASE = 11,
73 VHOST_USER_SET_VRING_KICK = 12,
74 VHOST_USER_SET_VRING_CALL = 13,
75 VHOST_USER_SET_VRING_ERR = 14,
76 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
77 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
78 VHOST_USER_GET_QUEUE_NUM = 17,
79 VHOST_USER_SET_VRING_ENABLE = 18,
80 VHOST_USER_MAX
81 } VhostUserRequest;
83 typedef struct VhostUserMemoryRegion {
84 uint64_t guest_phys_addr;
85 uint64_t memory_size;
86 uint64_t userspace_addr;
87 uint64_t mmap_offset;
88 } VhostUserMemoryRegion;
90 typedef struct VhostUserMemory {
91 uint32_t nregions;
92 uint32_t padding;
93 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
94 } VhostUserMemory;
96 typedef struct VhostUserLog {
97 uint64_t mmap_size;
98 uint64_t mmap_offset;
99 } VhostUserLog;
101 typedef struct VhostUserMsg {
102 VhostUserRequest request;
104 #define VHOST_USER_VERSION_MASK (0x3)
105 #define VHOST_USER_REPLY_MASK (0x1<<2)
106 uint32_t flags;
107 uint32_t size; /* the following payload size */
108 union {
109 #define VHOST_USER_VRING_IDX_MASK (0xff)
110 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
111 uint64_t u64;
112 struct vhost_vring_state state;
113 struct vhost_vring_addr addr;
114 VhostUserMemory memory;
115 VhostUserLog log;
116 } payload;
117 } QEMU_PACKED VhostUserMsg;
119 static VhostUserMsg m __attribute__ ((unused));
120 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
121 + sizeof(m.flags) \
122 + sizeof(m.size))
124 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
126 /* The version of the protocol we support */
127 #define VHOST_USER_VERSION (0x1)
128 /*****************************************************************************/
130 enum {
131 TEST_FLAGS_OK,
132 TEST_FLAGS_DISCONNECT,
133 TEST_FLAGS_BAD,
134 TEST_FLAGS_END,
137 typedef struct TestServer {
138 gchar *socket_path;
139 gchar *mig_path;
140 gchar *chr_name;
141 gchar *tmpfs;
142 CharBackend chr;
143 int fds_num;
144 int fds[VHOST_MEMORY_MAX_NREGIONS];
145 VhostUserMemory memory;
146 GMainContext *context;
147 GMainLoop *loop;
148 GThread *thread;
149 GMutex data_mutex;
150 GCond data_cond;
151 int log_fd;
152 uint64_t rings;
153 bool test_fail;
154 int test_flags;
155 int queues;
156 } TestServer;
158 static const char *init_hugepagefs(void);
159 static TestServer *test_server_new(const gchar *name);
160 static void test_server_free(TestServer *server);
161 static void test_server_listen(TestServer *server);
163 enum test_memfd {
164 TEST_MEMFD_AUTO,
165 TEST_MEMFD_YES,
166 TEST_MEMFD_NO,
169 static void append_vhost_opts(TestServer *s, GString *cmd_line,
170 const char *chr_opts)
172 g_string_append_printf(cmd_line, QEMU_CMD_CHR QEMU_CMD_NETDEV,
173 s->chr_name, s->socket_path,
174 chr_opts, s->chr_name);
177 static void append_mem_opts(TestServer *server, GString *cmd_line,
178 int size, enum test_memfd memfd)
180 if (memfd == TEST_MEMFD_AUTO) {
181 memfd = qemu_memfd_check(MFD_ALLOW_SEALING) ? TEST_MEMFD_YES
182 : TEST_MEMFD_NO;
185 if (memfd == TEST_MEMFD_YES) {
186 g_string_append_printf(cmd_line, QEMU_CMD_MEMFD, size, size);
187 } else {
188 const char *root = init_hugepagefs() ? : server->tmpfs;
190 g_string_append_printf(cmd_line, QEMU_CMD_MEM, size, size, root);
194 static bool wait_for_fds(TestServer *s)
196 gint64 end_time;
197 bool got_region;
198 int i;
200 g_mutex_lock(&s->data_mutex);
202 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
203 while (!s->fds_num) {
204 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
205 /* timeout has passed */
206 g_assert(s->fds_num);
207 break;
211 /* check for sanity */
212 g_assert_cmpint(s->fds_num, >, 0);
213 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
215 g_mutex_unlock(&s->data_mutex);
217 got_region = false;
218 for (i = 0; i < s->memory.nregions; ++i) {
219 VhostUserMemoryRegion *reg = &s->memory.regions[i];
220 if (reg->guest_phys_addr == 0) {
221 got_region = true;
222 break;
225 if (!got_region) {
226 g_test_skip("No memory at address 0x0");
228 return got_region;
231 static void read_guest_mem_server(QTestState *qts, TestServer *s)
233 uint8_t *guest_mem;
234 int i, j;
235 size_t size;
237 g_mutex_lock(&s->data_mutex);
239 /* iterate all regions */
240 for (i = 0; i < s->fds_num; i++) {
242 /* We'll check only the region statring at 0x0*/
243 if (s->memory.regions[i].guest_phys_addr != 0x0) {
244 continue;
247 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
249 size = s->memory.regions[i].memory_size +
250 s->memory.regions[i].mmap_offset;
252 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
253 MAP_SHARED, s->fds[i], 0);
255 g_assert(guest_mem != MAP_FAILED);
256 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
258 for (j = 0; j < 1024; j++) {
259 uint32_t a = qtest_readb(qts, s->memory.regions[i].guest_phys_addr + j);
260 uint32_t b = guest_mem[j];
262 g_assert_cmpint(a, ==, b);
265 munmap(guest_mem, s->memory.regions[i].memory_size);
268 g_mutex_unlock(&s->data_mutex);
271 static void *thread_function(void *data)
273 GMainLoop *loop = data;
274 g_main_loop_run(loop);
275 return NULL;
278 static int chr_can_read(void *opaque)
280 return VHOST_USER_HDR_SIZE;
283 static void chr_read(void *opaque, const uint8_t *buf, int size)
285 TestServer *s = opaque;
286 CharBackend *chr = &s->chr;
287 VhostUserMsg msg;
288 uint8_t *p = (uint8_t *) &msg;
289 int fd = -1;
291 if (s->test_fail) {
292 qemu_chr_fe_disconnect(chr);
293 /* now switch to non-failure */
294 s->test_fail = false;
297 if (size != VHOST_USER_HDR_SIZE) {
298 g_test_message("Wrong message size received %d", size);
299 return;
302 g_mutex_lock(&s->data_mutex);
303 memcpy(p, buf, VHOST_USER_HDR_SIZE);
305 if (msg.size) {
306 p += VHOST_USER_HDR_SIZE;
307 size = qemu_chr_fe_read_all(chr, p, msg.size);
308 if (size != msg.size) {
309 g_test_message("Wrong message size received %d != %d",
310 size, msg.size);
311 return;
315 switch (msg.request) {
316 case VHOST_USER_GET_FEATURES:
317 /* send back features to qemu */
318 msg.flags |= VHOST_USER_REPLY_MASK;
319 msg.size = sizeof(m.payload.u64);
320 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
321 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
322 if (s->queues > 1) {
323 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
325 if (s->test_flags >= TEST_FLAGS_BAD) {
326 msg.payload.u64 = 0;
327 s->test_flags = TEST_FLAGS_END;
329 p = (uint8_t *) &msg;
330 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
331 break;
333 case VHOST_USER_SET_FEATURES:
334 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
335 !=, 0ULL);
336 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
337 qemu_chr_fe_disconnect(chr);
338 s->test_flags = TEST_FLAGS_BAD;
340 break;
342 case VHOST_USER_GET_PROTOCOL_FEATURES:
343 /* send back features to qemu */
344 msg.flags |= VHOST_USER_REPLY_MASK;
345 msg.size = sizeof(m.payload.u64);
346 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
347 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN;
348 if (s->queues > 1) {
349 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
351 p = (uint8_t *) &msg;
352 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
353 break;
355 case VHOST_USER_GET_VRING_BASE:
356 /* send back vring base to qemu */
357 msg.flags |= VHOST_USER_REPLY_MASK;
358 msg.size = sizeof(m.payload.state);
359 msg.payload.state.num = 0;
360 p = (uint8_t *) &msg;
361 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
363 assert(msg.payload.state.index < s->queues * 2);
364 s->rings &= ~(0x1ULL << msg.payload.state.index);
365 g_cond_broadcast(&s->data_cond);
366 break;
368 case VHOST_USER_SET_MEM_TABLE:
369 /* received the mem table */
370 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
371 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
372 G_N_ELEMENTS(s->fds));
374 /* signal the test that it can continue */
375 g_cond_broadcast(&s->data_cond);
376 break;
378 case VHOST_USER_SET_VRING_KICK:
379 case VHOST_USER_SET_VRING_CALL:
380 /* consume the fd */
381 qemu_chr_fe_get_msgfds(chr, &fd, 1);
383 * This is a non-blocking eventfd.
384 * The receive function forces it to be blocking,
385 * so revert it back to non-blocking.
387 qemu_set_nonblock(fd);
388 break;
390 case VHOST_USER_SET_LOG_BASE:
391 if (s->log_fd != -1) {
392 close(s->log_fd);
393 s->log_fd = -1;
395 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
396 msg.flags |= VHOST_USER_REPLY_MASK;
397 msg.size = 0;
398 p = (uint8_t *) &msg;
399 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
401 g_cond_broadcast(&s->data_cond);
402 break;
404 case VHOST_USER_SET_VRING_BASE:
405 assert(msg.payload.state.index < s->queues * 2);
406 s->rings |= 0x1ULL << msg.payload.state.index;
407 g_cond_broadcast(&s->data_cond);
408 break;
410 case VHOST_USER_GET_QUEUE_NUM:
411 msg.flags |= VHOST_USER_REPLY_MASK;
412 msg.size = sizeof(m.payload.u64);
413 msg.payload.u64 = s->queues;
414 p = (uint8_t *) &msg;
415 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
416 break;
418 default:
419 break;
422 g_mutex_unlock(&s->data_mutex);
425 static const char *init_hugepagefs(void)
427 #ifdef CONFIG_LINUX
428 static const char *hugepagefs;
429 const char *path = getenv("QTEST_HUGETLBFS_PATH");
430 struct statfs fs;
431 int ret;
433 if (hugepagefs) {
434 return hugepagefs;
436 if (!path) {
437 return NULL;
440 if (access(path, R_OK | W_OK | X_OK)) {
441 g_test_message("access on path (%s): %s", path, strerror(errno));
442 g_test_fail();
443 return NULL;
446 do {
447 ret = statfs(path, &fs);
448 } while (ret != 0 && errno == EINTR);
450 if (ret != 0) {
451 g_test_message("statfs on path (%s): %s", path, strerror(errno));
452 g_test_fail();
453 return NULL;
456 if (fs.f_type != HUGETLBFS_MAGIC) {
457 g_test_message("Warning: path not on HugeTLBFS: %s", path);
458 g_test_fail();
459 return NULL;
462 hugepagefs = path;
463 return hugepagefs;
464 #else
465 return NULL;
466 #endif
469 static TestServer *test_server_new(const gchar *name)
471 TestServer *server = g_new0(TestServer, 1);
472 char template[] = "/tmp/vhost-test-XXXXXX";
473 const char *tmpfs;
475 server->context = g_main_context_new();
476 server->loop = g_main_loop_new(server->context, FALSE);
478 /* run the main loop thread so the chardev may operate */
479 server->thread = g_thread_new(NULL, thread_function, server->loop);
481 tmpfs = mkdtemp(template);
482 if (!tmpfs) {
483 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
485 g_assert(tmpfs);
487 server->tmpfs = g_strdup(tmpfs);
488 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
489 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
490 server->chr_name = g_strdup_printf("chr-%s", name);
492 g_mutex_init(&server->data_mutex);
493 g_cond_init(&server->data_cond);
495 server->log_fd = -1;
496 server->queues = 1;
498 return server;
501 static void chr_event(void *opaque, int event)
503 TestServer *s = opaque;
505 if (s->test_flags == TEST_FLAGS_END &&
506 event == CHR_EVENT_CLOSED) {
507 s->test_flags = TEST_FLAGS_OK;
511 static void test_server_create_chr(TestServer *server, const gchar *opt)
513 gchar *chr_path;
514 Chardev *chr;
516 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
517 chr = qemu_chr_new(server->chr_name, chr_path, server->context);
518 g_free(chr_path);
520 g_assert_nonnull(chr);
521 qemu_chr_fe_init(&server->chr, chr, &error_abort);
522 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
523 chr_event, NULL, server, server->context, true);
526 static void test_server_listen(TestServer *server)
528 test_server_create_chr(server, ",server,nowait");
531 static void test_server_free(TestServer *server)
533 int i, ret;
535 /* finish the helper thread and dispatch pending sources */
536 g_main_loop_quit(server->loop);
537 g_thread_join(server->thread);
538 while (g_main_context_pending(NULL)) {
539 g_main_context_iteration(NULL, TRUE);
542 unlink(server->socket_path);
543 g_free(server->socket_path);
545 unlink(server->mig_path);
546 g_free(server->mig_path);
548 ret = rmdir(server->tmpfs);
549 if (ret != 0) {
550 g_test_message("unable to rmdir: path (%s): %s",
551 server->tmpfs, strerror(errno));
553 g_free(server->tmpfs);
555 qemu_chr_fe_deinit(&server->chr, true);
557 for (i = 0; i < server->fds_num; i++) {
558 close(server->fds[i]);
561 if (server->log_fd != -1) {
562 close(server->log_fd);
565 g_free(server->chr_name);
567 g_main_loop_unref(server->loop);
568 g_main_context_unref(server->context);
569 g_cond_clear(&server->data_cond);
570 g_mutex_clear(&server->data_mutex);
571 g_free(server);
574 static void wait_for_log_fd(TestServer *s)
576 gint64 end_time;
578 g_mutex_lock(&s->data_mutex);
579 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
580 while (s->log_fd == -1) {
581 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
582 /* timeout has passed */
583 g_assert(s->log_fd != -1);
584 break;
588 g_mutex_unlock(&s->data_mutex);
591 static void write_guest_mem(TestServer *s, uint32_t seed)
593 uint32_t *guest_mem;
594 int i, j;
595 size_t size;
597 /* iterate all regions */
598 for (i = 0; i < s->fds_num; i++) {
600 /* We'll write only the region statring at 0x0 */
601 if (s->memory.regions[i].guest_phys_addr != 0x0) {
602 continue;
605 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
607 size = s->memory.regions[i].memory_size +
608 s->memory.regions[i].mmap_offset;
610 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
611 MAP_SHARED, s->fds[i], 0);
613 g_assert(guest_mem != MAP_FAILED);
614 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
616 for (j = 0; j < 256; j++) {
617 guest_mem[j] = seed + j;
620 munmap(guest_mem, s->memory.regions[i].memory_size);
621 break;
625 static guint64 get_log_size(TestServer *s)
627 guint64 log_size = 0;
628 int i;
630 for (i = 0; i < s->memory.nregions; ++i) {
631 VhostUserMemoryRegion *reg = &s->memory.regions[i];
632 guint64 last = range_get_last(reg->guest_phys_addr,
633 reg->memory_size);
634 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
637 return log_size;
640 typedef struct TestMigrateSource {
641 GSource source;
642 TestServer *src;
643 TestServer *dest;
644 } TestMigrateSource;
646 static gboolean
647 test_migrate_source_check(GSource *source)
649 TestMigrateSource *t = (TestMigrateSource *)source;
650 gboolean overlap = t->src->rings && t->dest->rings;
652 g_assert(!overlap);
654 return FALSE;
657 GSourceFuncs test_migrate_source_funcs = {
658 .check = test_migrate_source_check,
661 static void vhost_user_test_cleanup(void *s)
663 TestServer *server = s;
665 qos_invalidate_command_line();
666 test_server_free(server);
669 static void *vhost_user_test_setup(GString *cmd_line, void *arg)
671 TestServer *server = test_server_new("vhost-user-test");
672 test_server_listen(server);
674 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_AUTO);
675 append_vhost_opts(server, cmd_line, "");
677 g_test_queue_destroy(vhost_user_test_cleanup, server);
679 return server;
682 static void *vhost_user_test_setup_memfd(GString *cmd_line, void *arg)
684 TestServer *server = test_server_new("vhost-user-test");
685 test_server_listen(server);
687 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_YES);
688 append_vhost_opts(server, cmd_line, "");
690 g_test_queue_destroy(vhost_user_test_cleanup, server);
692 return server;
695 static void test_read_guest_mem(void *obj, void *arg, QGuestAllocator *alloc)
697 TestServer *server = arg;
699 if (!wait_for_fds(server)) {
700 return;
703 read_guest_mem_server(global_qtest, server);
706 static void test_migrate(void *obj, void *arg, QGuestAllocator *alloc)
708 TestServer *s = arg;
709 TestServer *dest = test_server_new("dest");
710 GString *dest_cmdline = g_string_new(qos_get_current_command_line());
711 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
712 QTestState *to;
713 GSource *source;
714 QDict *rsp;
715 guint8 *log;
716 guint64 size;
718 if (!wait_for_fds(s)) {
719 return;
722 size = get_log_size(s);
723 g_assert_cmpint(size, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
725 test_server_listen(dest);
726 g_string_append_printf(dest_cmdline, " -incoming %s", uri);
727 append_mem_opts(dest, dest_cmdline, 256, TEST_MEMFD_AUTO);
728 append_vhost_opts(dest, dest_cmdline, "");
729 to = qtest_init(dest_cmdline->str);
731 /* This would be where you call qos_allocate_objects(to, NULL), if you want
732 * to talk to the QVirtioNet object on the destination.
735 source = g_source_new(&test_migrate_source_funcs,
736 sizeof(TestMigrateSource));
737 ((TestMigrateSource *)source)->src = s;
738 ((TestMigrateSource *)source)->dest = dest;
739 g_source_attach(source, s->context);
741 /* slow down migration to have time to fiddle with log */
742 /* TODO: qtest could learn to break on some places */
743 rsp = qmp("{ 'execute': 'migrate_set_speed',"
744 "'arguments': { 'value': 10 } }");
745 g_assert(qdict_haskey(rsp, "return"));
746 qobject_unref(rsp);
748 rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
749 g_assert(qdict_haskey(rsp, "return"));
750 qobject_unref(rsp);
752 wait_for_log_fd(s);
754 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
755 g_assert(log != MAP_FAILED);
757 /* modify first page */
758 write_guest_mem(s, 0x42);
759 log[0] = 1;
760 munmap(log, size);
762 /* speed things up */
763 rsp = qmp("{ 'execute': 'migrate_set_speed',"
764 "'arguments': { 'value': 0 } }");
765 g_assert(qdict_haskey(rsp, "return"));
766 qobject_unref(rsp);
768 qmp_eventwait("STOP");
769 qtest_qmp_eventwait(to, "RESUME");
771 g_assert(wait_for_fds(dest));
772 read_guest_mem_server(to, dest);
774 g_source_destroy(source);
775 g_source_unref(source);
777 qtest_quit(to);
778 test_server_free(dest);
779 g_free(uri);
782 static void wait_for_rings_started(TestServer *s, size_t count)
784 gint64 end_time;
786 g_mutex_lock(&s->data_mutex);
787 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
788 while (ctpop64(s->rings) != count) {
789 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
790 /* timeout has passed */
791 g_assert_cmpint(ctpop64(s->rings), ==, count);
792 break;
796 g_mutex_unlock(&s->data_mutex);
799 static inline void test_server_connect(TestServer *server)
801 test_server_create_chr(server, ",reconnect=1");
804 static gboolean
805 reconnect_cb(gpointer user_data)
807 TestServer *s = user_data;
809 qemu_chr_fe_disconnect(&s->chr);
811 return FALSE;
814 static gpointer
815 connect_thread(gpointer data)
817 TestServer *s = data;
819 /* wait for qemu to start before first try, to avoid extra warnings */
820 g_usleep(G_USEC_PER_SEC);
821 test_server_connect(s);
823 return NULL;
826 static void *vhost_user_test_setup_reconnect(GString *cmd_line, void *arg)
828 TestServer *s = test_server_new("reconnect");
830 g_thread_new("connect", connect_thread, s);
831 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
832 append_vhost_opts(s, cmd_line, ",server");
834 g_test_queue_destroy(vhost_user_test_cleanup, s);
836 return s;
839 static void test_reconnect(void *obj, void *arg, QGuestAllocator *alloc)
841 TestServer *s = arg;
842 GSource *src;
844 if (!wait_for_fds(s)) {
845 return;
848 wait_for_rings_started(s, 2);
850 /* reconnect */
851 s->fds_num = 0;
852 s->rings = 0;
853 src = g_idle_source_new();
854 g_source_set_callback(src, reconnect_cb, s, NULL);
855 g_source_attach(src, s->context);
856 g_source_unref(src);
857 g_assert(wait_for_fds(s));
858 wait_for_rings_started(s, 2);
861 static void *vhost_user_test_setup_connect_fail(GString *cmd_line, void *arg)
863 TestServer *s = test_server_new("connect-fail");
865 s->test_fail = true;
867 g_thread_new("connect", connect_thread, s);
868 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
869 append_vhost_opts(s, cmd_line, ",server");
871 g_test_queue_destroy(vhost_user_test_cleanup, s);
873 return s;
876 static void *vhost_user_test_setup_flags_mismatch(GString *cmd_line, void *arg)
878 TestServer *s = test_server_new("flags-mismatch");
880 s->test_flags = TEST_FLAGS_DISCONNECT;
882 g_thread_new("connect", connect_thread, s);
883 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
884 append_vhost_opts(s, cmd_line, ",server");
886 g_test_queue_destroy(vhost_user_test_cleanup, s);
888 return s;
891 static void test_vhost_user_started(void *obj, void *arg, QGuestAllocator *alloc)
893 TestServer *s = arg;
895 if (!wait_for_fds(s)) {
896 return;
898 wait_for_rings_started(s, 2);
901 static void *vhost_user_test_setup_multiqueue(GString *cmd_line, void *arg)
903 TestServer *s = vhost_user_test_setup(cmd_line, arg);
905 s->queues = 2;
906 g_string_append_printf(cmd_line,
907 " -set netdev.hs0.queues=%d"
908 " -global virtio-net-pci.vectors=%d",
909 s->queues, s->queues * 2 + 2);
911 return s;
914 static void test_multiqueue(void *obj, void *arg, QGuestAllocator *alloc)
916 TestServer *s = arg;
918 wait_for_rings_started(s, s->queues * 2);
921 static void register_vhost_user_test(void)
923 QOSGraphTestOptions opts = {
924 .before = vhost_user_test_setup,
925 .subprocess = true,
928 qemu_add_opts(&qemu_chardev_opts);
930 qos_add_test("vhost-user/read-guest-mem/memfile",
931 "virtio-net",
932 test_read_guest_mem, &opts);
934 if (qemu_memfd_check(MFD_ALLOW_SEALING)) {
935 opts.before = vhost_user_test_setup_memfd;
936 qos_add_test("vhost-user/read-guest-mem/memfd",
937 "virtio-net",
938 test_read_guest_mem, &opts);
941 qos_add_test("vhost-user/migrate",
942 "virtio-net",
943 test_migrate, &opts);
945 /* keeps failing on build-system since Aug 15 2017 */
946 if (getenv("QTEST_VHOST_USER_FIXME")) {
947 opts.before = vhost_user_test_setup_reconnect;
948 qos_add_test("vhost-user/reconnect", "virtio-net",
949 test_reconnect, &opts);
951 opts.before = vhost_user_test_setup_connect_fail;
952 qos_add_test("vhost-user/connect-fail", "virtio-net",
953 test_vhost_user_started, &opts);
955 opts.before = vhost_user_test_setup_flags_mismatch;
956 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
957 test_vhost_user_started, &opts);
960 opts.before = vhost_user_test_setup_multiqueue;
961 opts.edge.extra_device_opts = "mq=on";
962 qos_add_test("vhost-user/multiqueue",
963 "virtio-net",
964 test_multiqueue, &opts);
966 libqos_init(register_vhost_user_test);