ppc/pnv: introduce a new pic_print_info() operation to the chip model
[qemu/ar7.git] / tests / vhost-user-test.c
blob0c965b3b1e1638946e9c8a7d7b510eebb0434c89
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(0) ? TEST_MEMFD_YES : TEST_MEMFD_NO;
184 if (memfd == TEST_MEMFD_YES) {
185 g_string_append_printf(cmd_line, QEMU_CMD_MEMFD, size, size);
186 } else {
187 const char *root = init_hugepagefs() ? : server->tmpfs;
189 g_string_append_printf(cmd_line, QEMU_CMD_MEM, size, size, root);
193 static bool wait_for_fds(TestServer *s)
195 gint64 end_time;
196 bool got_region;
197 int i;
199 g_mutex_lock(&s->data_mutex);
201 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
202 while (!s->fds_num) {
203 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
204 /* timeout has passed */
205 g_assert(s->fds_num);
206 break;
210 /* check for sanity */
211 g_assert_cmpint(s->fds_num, >, 0);
212 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
214 g_mutex_unlock(&s->data_mutex);
216 got_region = false;
217 for (i = 0; i < s->memory.nregions; ++i) {
218 VhostUserMemoryRegion *reg = &s->memory.regions[i];
219 if (reg->guest_phys_addr == 0) {
220 got_region = true;
221 break;
224 if (!got_region) {
225 g_test_skip("No memory at address 0x0");
227 return got_region;
230 static void read_guest_mem_server(QTestState *qts, TestServer *s)
232 uint8_t *guest_mem;
233 int i, j;
234 size_t size;
236 g_mutex_lock(&s->data_mutex);
238 /* iterate all regions */
239 for (i = 0; i < s->fds_num; i++) {
241 /* We'll check only the region statring at 0x0*/
242 if (s->memory.regions[i].guest_phys_addr != 0x0) {
243 continue;
246 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
248 size = s->memory.regions[i].memory_size +
249 s->memory.regions[i].mmap_offset;
251 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
252 MAP_SHARED, s->fds[i], 0);
254 g_assert(guest_mem != MAP_FAILED);
255 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
257 for (j = 0; j < 1024; j++) {
258 uint32_t a = qtest_readb(qts, s->memory.regions[i].guest_phys_addr + j);
259 uint32_t b = guest_mem[j];
261 g_assert_cmpint(a, ==, b);
264 munmap(guest_mem, s->memory.regions[i].memory_size);
267 g_mutex_unlock(&s->data_mutex);
270 static void *thread_function(void *data)
272 GMainLoop *loop = data;
273 g_main_loop_run(loop);
274 return NULL;
277 static int chr_can_read(void *opaque)
279 return VHOST_USER_HDR_SIZE;
282 static void chr_read(void *opaque, const uint8_t *buf, int size)
284 TestServer *s = opaque;
285 CharBackend *chr = &s->chr;
286 VhostUserMsg msg;
287 uint8_t *p = (uint8_t *) &msg;
288 int fd = -1;
290 if (s->test_fail) {
291 qemu_chr_fe_disconnect(chr);
292 /* now switch to non-failure */
293 s->test_fail = false;
296 if (size != VHOST_USER_HDR_SIZE) {
297 g_test_message("Wrong message size received %d", size);
298 return;
301 g_mutex_lock(&s->data_mutex);
302 memcpy(p, buf, VHOST_USER_HDR_SIZE);
304 if (msg.size) {
305 p += VHOST_USER_HDR_SIZE;
306 size = qemu_chr_fe_read_all(chr, p, msg.size);
307 if (size != msg.size) {
308 g_test_message("Wrong message size received %d != %d",
309 size, msg.size);
310 return;
314 switch (msg.request) {
315 case VHOST_USER_GET_FEATURES:
316 /* send back features to qemu */
317 msg.flags |= VHOST_USER_REPLY_MASK;
318 msg.size = sizeof(m.payload.u64);
319 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
320 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
321 if (s->queues > 1) {
322 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
324 if (s->test_flags >= TEST_FLAGS_BAD) {
325 msg.payload.u64 = 0;
326 s->test_flags = TEST_FLAGS_END;
328 p = (uint8_t *) &msg;
329 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
330 break;
332 case VHOST_USER_SET_FEATURES:
333 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
334 !=, 0ULL);
335 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
336 qemu_chr_fe_disconnect(chr);
337 s->test_flags = TEST_FLAGS_BAD;
339 break;
341 case VHOST_USER_GET_PROTOCOL_FEATURES:
342 /* send back features to qemu */
343 msg.flags |= VHOST_USER_REPLY_MASK;
344 msg.size = sizeof(m.payload.u64);
345 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
346 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN;
347 if (s->queues > 1) {
348 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
350 p = (uint8_t *) &msg;
351 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
352 break;
354 case VHOST_USER_GET_VRING_BASE:
355 /* send back vring base to qemu */
356 msg.flags |= VHOST_USER_REPLY_MASK;
357 msg.size = sizeof(m.payload.state);
358 msg.payload.state.num = 0;
359 p = (uint8_t *) &msg;
360 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
362 assert(msg.payload.state.index < s->queues * 2);
363 s->rings &= ~(0x1ULL << msg.payload.state.index);
364 g_cond_broadcast(&s->data_cond);
365 break;
367 case VHOST_USER_SET_MEM_TABLE:
368 /* received the mem table */
369 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
370 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
371 G_N_ELEMENTS(s->fds));
373 /* signal the test that it can continue */
374 g_cond_broadcast(&s->data_cond);
375 break;
377 case VHOST_USER_SET_VRING_KICK:
378 case VHOST_USER_SET_VRING_CALL:
379 /* consume the fd */
380 qemu_chr_fe_get_msgfds(chr, &fd, 1);
382 * This is a non-blocking eventfd.
383 * The receive function forces it to be blocking,
384 * so revert it back to non-blocking.
386 qemu_set_nonblock(fd);
387 break;
389 case VHOST_USER_SET_LOG_BASE:
390 if (s->log_fd != -1) {
391 close(s->log_fd);
392 s->log_fd = -1;
394 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
395 msg.flags |= VHOST_USER_REPLY_MASK;
396 msg.size = 0;
397 p = (uint8_t *) &msg;
398 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
400 g_cond_broadcast(&s->data_cond);
401 break;
403 case VHOST_USER_SET_VRING_BASE:
404 assert(msg.payload.state.index < s->queues * 2);
405 s->rings |= 0x1ULL << msg.payload.state.index;
406 g_cond_broadcast(&s->data_cond);
407 break;
409 case VHOST_USER_GET_QUEUE_NUM:
410 msg.flags |= VHOST_USER_REPLY_MASK;
411 msg.size = sizeof(m.payload.u64);
412 msg.payload.u64 = s->queues;
413 p = (uint8_t *) &msg;
414 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
415 break;
417 default:
418 break;
421 g_mutex_unlock(&s->data_mutex);
424 static const char *init_hugepagefs(void)
426 #ifdef CONFIG_LINUX
427 static const char *hugepagefs;
428 const char *path = getenv("QTEST_HUGETLBFS_PATH");
429 struct statfs fs;
430 int ret;
432 if (hugepagefs) {
433 return hugepagefs;
435 if (!path) {
436 return NULL;
439 if (access(path, R_OK | W_OK | X_OK)) {
440 g_test_message("access on path (%s): %s", path, strerror(errno));
441 g_test_fail();
442 return NULL;
445 do {
446 ret = statfs(path, &fs);
447 } while (ret != 0 && errno == EINTR);
449 if (ret != 0) {
450 g_test_message("statfs on path (%s): %s", path, strerror(errno));
451 g_test_fail();
452 return NULL;
455 if (fs.f_type != HUGETLBFS_MAGIC) {
456 g_test_message("Warning: path not on HugeTLBFS: %s", path);
457 g_test_fail();
458 return NULL;
461 hugepagefs = path;
462 return hugepagefs;
463 #else
464 return NULL;
465 #endif
468 static TestServer *test_server_new(const gchar *name)
470 TestServer *server = g_new0(TestServer, 1);
471 char template[] = "/tmp/vhost-test-XXXXXX";
472 const char *tmpfs;
474 server->context = g_main_context_new();
475 server->loop = g_main_loop_new(server->context, FALSE);
477 /* run the main loop thread so the chardev may operate */
478 server->thread = g_thread_new(NULL, thread_function, server->loop);
480 tmpfs = mkdtemp(template);
481 if (!tmpfs) {
482 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
484 g_assert(tmpfs);
486 server->tmpfs = g_strdup(tmpfs);
487 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
488 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
489 server->chr_name = g_strdup_printf("chr-%s", name);
491 g_mutex_init(&server->data_mutex);
492 g_cond_init(&server->data_cond);
494 server->log_fd = -1;
495 server->queues = 1;
497 return server;
500 static void chr_event(void *opaque, int event)
502 TestServer *s = opaque;
504 if (s->test_flags == TEST_FLAGS_END &&
505 event == CHR_EVENT_CLOSED) {
506 s->test_flags = TEST_FLAGS_OK;
510 static void test_server_create_chr(TestServer *server, const gchar *opt)
512 gchar *chr_path;
513 Chardev *chr;
515 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
516 chr = qemu_chr_new(server->chr_name, chr_path, server->context);
517 g_free(chr_path);
519 g_assert_nonnull(chr);
520 qemu_chr_fe_init(&server->chr, chr, &error_abort);
521 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
522 chr_event, NULL, server, server->context, true);
525 static void test_server_listen(TestServer *server)
527 test_server_create_chr(server, ",server,nowait");
530 static void test_server_free(TestServer *server)
532 int i, ret;
534 /* finish the helper thread and dispatch pending sources */
535 g_main_loop_quit(server->loop);
536 g_thread_join(server->thread);
537 while (g_main_context_pending(NULL)) {
538 g_main_context_iteration(NULL, TRUE);
541 unlink(server->socket_path);
542 g_free(server->socket_path);
544 unlink(server->mig_path);
545 g_free(server->mig_path);
547 ret = rmdir(server->tmpfs);
548 if (ret != 0) {
549 g_test_message("unable to rmdir: path (%s): %s",
550 server->tmpfs, strerror(errno));
552 g_free(server->tmpfs);
554 qemu_chr_fe_deinit(&server->chr, true);
556 for (i = 0; i < server->fds_num; i++) {
557 close(server->fds[i]);
560 if (server->log_fd != -1) {
561 close(server->log_fd);
564 g_free(server->chr_name);
566 g_main_loop_unref(server->loop);
567 g_main_context_unref(server->context);
568 g_cond_clear(&server->data_cond);
569 g_mutex_clear(&server->data_mutex);
570 g_free(server);
573 static void wait_for_log_fd(TestServer *s)
575 gint64 end_time;
577 g_mutex_lock(&s->data_mutex);
578 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
579 while (s->log_fd == -1) {
580 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
581 /* timeout has passed */
582 g_assert(s->log_fd != -1);
583 break;
587 g_mutex_unlock(&s->data_mutex);
590 static void write_guest_mem(TestServer *s, uint32_t seed)
592 uint32_t *guest_mem;
593 int i, j;
594 size_t size;
596 /* iterate all regions */
597 for (i = 0; i < s->fds_num; i++) {
599 /* We'll write only the region statring at 0x0 */
600 if (s->memory.regions[i].guest_phys_addr != 0x0) {
601 continue;
604 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
606 size = s->memory.regions[i].memory_size +
607 s->memory.regions[i].mmap_offset;
609 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
610 MAP_SHARED, s->fds[i], 0);
612 g_assert(guest_mem != MAP_FAILED);
613 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
615 for (j = 0; j < 256; j++) {
616 guest_mem[j] = seed + j;
619 munmap(guest_mem, s->memory.regions[i].memory_size);
620 break;
624 static guint64 get_log_size(TestServer *s)
626 guint64 log_size = 0;
627 int i;
629 for (i = 0; i < s->memory.nregions; ++i) {
630 VhostUserMemoryRegion *reg = &s->memory.regions[i];
631 guint64 last = range_get_last(reg->guest_phys_addr,
632 reg->memory_size);
633 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
636 return log_size;
639 typedef struct TestMigrateSource {
640 GSource source;
641 TestServer *src;
642 TestServer *dest;
643 } TestMigrateSource;
645 static gboolean
646 test_migrate_source_check(GSource *source)
648 TestMigrateSource *t = (TestMigrateSource *)source;
649 gboolean overlap = t->src->rings && t->dest->rings;
651 g_assert(!overlap);
653 return FALSE;
656 GSourceFuncs test_migrate_source_funcs = {
657 .check = test_migrate_source_check,
660 static void vhost_user_test_cleanup(void *s)
662 TestServer *server = s;
664 qos_invalidate_command_line();
665 test_server_free(server);
668 static void *vhost_user_test_setup(GString *cmd_line, void *arg)
670 TestServer *server = test_server_new("vhost-user-test");
671 test_server_listen(server);
673 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_AUTO);
674 append_vhost_opts(server, cmd_line, "");
676 g_test_queue_destroy(vhost_user_test_cleanup, server);
678 return server;
681 static void *vhost_user_test_setup_memfd(GString *cmd_line, void *arg)
683 TestServer *server = test_server_new("vhost-user-test");
684 test_server_listen(server);
686 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_YES);
687 append_vhost_opts(server, cmd_line, "");
689 g_test_queue_destroy(vhost_user_test_cleanup, server);
691 return server;
694 static void test_read_guest_mem(void *obj, void *arg, QGuestAllocator *alloc)
696 TestServer *server = arg;
698 if (!wait_for_fds(server)) {
699 return;
702 read_guest_mem_server(global_qtest, server);
705 static void test_migrate(void *obj, void *arg, QGuestAllocator *alloc)
707 TestServer *s = arg;
708 TestServer *dest = test_server_new("dest");
709 GString *dest_cmdline = g_string_new(qos_get_current_command_line());
710 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
711 QTestState *to;
712 GSource *source;
713 QDict *rsp;
714 guint8 *log;
715 guint64 size;
717 if (!wait_for_fds(s)) {
718 return;
721 size = get_log_size(s);
722 g_assert_cmpint(size, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
724 test_server_listen(dest);
725 g_string_append_printf(dest_cmdline, " -incoming %s", uri);
726 append_mem_opts(dest, dest_cmdline, 256, TEST_MEMFD_AUTO);
727 append_vhost_opts(dest, dest_cmdline, "");
728 to = qtest_init(dest_cmdline->str);
730 /* This would be where you call qos_allocate_objects(to, NULL), if you want
731 * to talk to the QVirtioNet object on the destination.
734 source = g_source_new(&test_migrate_source_funcs,
735 sizeof(TestMigrateSource));
736 ((TestMigrateSource *)source)->src = s;
737 ((TestMigrateSource *)source)->dest = dest;
738 g_source_attach(source, s->context);
740 /* slow down migration to have time to fiddle with log */
741 /* TODO: qtest could learn to break on some places */
742 rsp = qmp("{ 'execute': 'migrate_set_speed',"
743 "'arguments': { 'value': 10 } }");
744 g_assert(qdict_haskey(rsp, "return"));
745 qobject_unref(rsp);
747 rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
748 g_assert(qdict_haskey(rsp, "return"));
749 qobject_unref(rsp);
751 wait_for_log_fd(s);
753 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
754 g_assert(log != MAP_FAILED);
756 /* modify first page */
757 write_guest_mem(s, 0x42);
758 log[0] = 1;
759 munmap(log, size);
761 /* speed things up */
762 rsp = qmp("{ 'execute': 'migrate_set_speed',"
763 "'arguments': { 'value': 0 } }");
764 g_assert(qdict_haskey(rsp, "return"));
765 qobject_unref(rsp);
767 qmp_eventwait("STOP");
768 qtest_qmp_eventwait(to, "RESUME");
770 g_assert(wait_for_fds(dest));
771 read_guest_mem_server(to, dest);
773 g_source_destroy(source);
774 g_source_unref(source);
776 qtest_quit(to);
777 test_server_free(dest);
778 g_free(uri);
781 static void wait_for_rings_started(TestServer *s, size_t count)
783 gint64 end_time;
785 g_mutex_lock(&s->data_mutex);
786 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
787 while (ctpop64(s->rings) != count) {
788 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
789 /* timeout has passed */
790 g_assert_cmpint(ctpop64(s->rings), ==, count);
791 break;
795 g_mutex_unlock(&s->data_mutex);
798 static inline void test_server_connect(TestServer *server)
800 test_server_create_chr(server, ",reconnect=1");
803 static gboolean
804 reconnect_cb(gpointer user_data)
806 TestServer *s = user_data;
808 qemu_chr_fe_disconnect(&s->chr);
810 return FALSE;
813 static gpointer
814 connect_thread(gpointer data)
816 TestServer *s = data;
818 /* wait for qemu to start before first try, to avoid extra warnings */
819 g_usleep(G_USEC_PER_SEC);
820 test_server_connect(s);
822 return NULL;
825 static void *vhost_user_test_setup_reconnect(GString *cmd_line, void *arg)
827 TestServer *s = test_server_new("reconnect");
829 g_thread_new("connect", connect_thread, s);
830 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
831 append_vhost_opts(s, cmd_line, ",server");
833 g_test_queue_destroy(vhost_user_test_cleanup, s);
835 return s;
838 static void test_reconnect(void *obj, void *arg, QGuestAllocator *alloc)
840 TestServer *s = arg;
841 GSource *src;
843 if (!wait_for_fds(s)) {
844 return;
847 wait_for_rings_started(s, 2);
849 /* reconnect */
850 s->fds_num = 0;
851 s->rings = 0;
852 src = g_idle_source_new();
853 g_source_set_callback(src, reconnect_cb, s, NULL);
854 g_source_attach(src, s->context);
855 g_source_unref(src);
856 g_assert(wait_for_fds(s));
857 wait_for_rings_started(s, 2);
860 static void *vhost_user_test_setup_connect_fail(GString *cmd_line, void *arg)
862 TestServer *s = test_server_new("connect-fail");
864 s->test_fail = true;
866 g_thread_new("connect", connect_thread, s);
867 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
868 append_vhost_opts(s, cmd_line, ",server");
870 g_test_queue_destroy(vhost_user_test_cleanup, s);
872 return s;
875 static void *vhost_user_test_setup_flags_mismatch(GString *cmd_line, void *arg)
877 TestServer *s = test_server_new("flags-mismatch");
879 s->test_flags = TEST_FLAGS_DISCONNECT;
881 g_thread_new("connect", connect_thread, s);
882 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
883 append_vhost_opts(s, cmd_line, ",server");
885 g_test_queue_destroy(vhost_user_test_cleanup, s);
887 return s;
890 static void test_vhost_user_started(void *obj, void *arg, QGuestAllocator *alloc)
892 TestServer *s = arg;
894 if (!wait_for_fds(s)) {
895 return;
897 wait_for_rings_started(s, 2);
900 static void *vhost_user_test_setup_multiqueue(GString *cmd_line, void *arg)
902 TestServer *s = vhost_user_test_setup(cmd_line, arg);
904 s->queues = 2;
905 g_string_append_printf(cmd_line,
906 " -set netdev.hs0.queues=%d"
907 " -global virtio-net-pci.vectors=%d",
908 s->queues, s->queues * 2 + 2);
910 return s;
913 static void test_multiqueue(void *obj, void *arg, QGuestAllocator *alloc)
915 TestServer *s = arg;
917 wait_for_rings_started(s, s->queues * 2);
920 static void register_vhost_user_test(void)
922 QOSGraphTestOptions opts = {
923 .before = vhost_user_test_setup,
924 .subprocess = true,
927 qemu_add_opts(&qemu_chardev_opts);
929 qos_add_test("vhost-user/read-guest-mem/memfile",
930 "virtio-net",
931 test_read_guest_mem, &opts);
933 if (qemu_memfd_check(0)) {
934 opts.before = vhost_user_test_setup_memfd;
935 qos_add_test("vhost-user/read-guest-mem/memfd",
936 "virtio-net",
937 test_read_guest_mem, &opts);
940 qos_add_test("vhost-user/migrate",
941 "virtio-net",
942 test_migrate, &opts);
944 /* keeps failing on build-system since Aug 15 2017 */
945 if (getenv("QTEST_VHOST_USER_FIXME")) {
946 opts.before = vhost_user_test_setup_reconnect;
947 qos_add_test("vhost-user/reconnect", "virtio-net",
948 test_reconnect, &opts);
950 opts.before = vhost_user_test_setup_connect_fail;
951 qos_add_test("vhost-user/connect-fail", "virtio-net",
952 test_vhost_user_started, &opts);
954 opts.before = vhost_user_test_setup_flags_mismatch;
955 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
956 test_vhost_user_started, &opts);
959 opts.before = vhost_user_test_setup_multiqueue;
960 opts.edge.extra_device_opts = "mq=on";
961 qos_add_test("vhost-user/multiqueue",
962 "virtio-net",
963 test_multiqueue, &opts);
965 libqos_init(register_vhost_user_test);