block/mirror: Wait for in-flight op conflicts
[qemu/ar7.git] / tests / vhost-user-test.c
blobbbc80912865a44b30fab34bdf2c4626c38462f38
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 <linux/vhost.h>
31 #include <linux/virtio_ids.h>
32 #include <linux/virtio_net.h>
33 #include <sys/vfs.h>
35 /* GLIB version compatibility flags */
36 #if !GLIB_CHECK_VERSION(2, 26, 0)
37 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
38 #endif
40 #if GLIB_CHECK_VERSION(2, 28, 0)
41 #define HAVE_MONOTONIC_TIME
42 #endif
44 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
45 "mem-path=%s,share=on -numa node,memdev=mem"
46 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
47 " -numa node,memdev=mem"
48 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
49 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
50 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
52 #define HUGETLBFS_MAGIC 0x958458f6
54 /*********** FROM hw/virtio/vhost-user.c *************************************/
56 #define VHOST_MEMORY_MAX_NREGIONS 8
57 #define VHOST_MAX_VIRTQUEUES 0x100
59 #define VHOST_USER_F_PROTOCOL_FEATURES 30
60 #define VHOST_USER_PROTOCOL_F_MQ 0
61 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
63 #define VHOST_LOG_PAGE 0x1000
65 typedef enum VhostUserRequest {
66 VHOST_USER_NONE = 0,
67 VHOST_USER_GET_FEATURES = 1,
68 VHOST_USER_SET_FEATURES = 2,
69 VHOST_USER_SET_OWNER = 3,
70 VHOST_USER_RESET_OWNER = 4,
71 VHOST_USER_SET_MEM_TABLE = 5,
72 VHOST_USER_SET_LOG_BASE = 6,
73 VHOST_USER_SET_LOG_FD = 7,
74 VHOST_USER_SET_VRING_NUM = 8,
75 VHOST_USER_SET_VRING_ADDR = 9,
76 VHOST_USER_SET_VRING_BASE = 10,
77 VHOST_USER_GET_VRING_BASE = 11,
78 VHOST_USER_SET_VRING_KICK = 12,
79 VHOST_USER_SET_VRING_CALL = 13,
80 VHOST_USER_SET_VRING_ERR = 14,
81 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
82 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
83 VHOST_USER_GET_QUEUE_NUM = 17,
84 VHOST_USER_SET_VRING_ENABLE = 18,
85 VHOST_USER_MAX
86 } VhostUserRequest;
88 typedef struct VhostUserMemoryRegion {
89 uint64_t guest_phys_addr;
90 uint64_t memory_size;
91 uint64_t userspace_addr;
92 uint64_t mmap_offset;
93 } VhostUserMemoryRegion;
95 typedef struct VhostUserMemory {
96 uint32_t nregions;
97 uint32_t padding;
98 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
99 } VhostUserMemory;
101 typedef struct VhostUserLog {
102 uint64_t mmap_size;
103 uint64_t mmap_offset;
104 } VhostUserLog;
106 typedef struct VhostUserMsg {
107 VhostUserRequest request;
109 #define VHOST_USER_VERSION_MASK (0x3)
110 #define VHOST_USER_REPLY_MASK (0x1<<2)
111 uint32_t flags;
112 uint32_t size; /* the following payload size */
113 union {
114 #define VHOST_USER_VRING_IDX_MASK (0xff)
115 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
116 uint64_t u64;
117 struct vhost_vring_state state;
118 struct vhost_vring_addr addr;
119 VhostUserMemory memory;
120 VhostUserLog log;
121 } payload;
122 } QEMU_PACKED VhostUserMsg;
124 static VhostUserMsg m __attribute__ ((unused));
125 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
126 + sizeof(m.flags) \
127 + sizeof(m.size))
129 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
131 /* The version of the protocol we support */
132 #define VHOST_USER_VERSION (0x1)
133 /*****************************************************************************/
135 enum {
136 TEST_FLAGS_OK,
137 TEST_FLAGS_DISCONNECT,
138 TEST_FLAGS_BAD,
139 TEST_FLAGS_END,
142 typedef struct TestServer {
143 QPCIBus *bus;
144 QVirtioPCIDevice *dev;
145 QVirtQueue *vq[VHOST_MAX_VIRTQUEUES];
146 gchar *socket_path;
147 gchar *mig_path;
148 gchar *chr_name;
149 CharBackend chr;
150 int fds_num;
151 int fds[VHOST_MEMORY_MAX_NREGIONS];
152 VhostUserMemory memory;
153 CompatGMutex data_mutex;
154 CompatGCond data_cond;
155 int log_fd;
156 uint64_t rings;
157 bool test_fail;
158 int test_flags;
159 int queues;
160 QGuestAllocator *alloc;
161 } TestServer;
163 static TestServer *test_server_new(const gchar *name);
164 static void test_server_free(TestServer *server);
165 static void test_server_listen(TestServer *server);
167 static const char *tmpfs;
168 static const char *root;
170 enum test_memfd {
171 TEST_MEMFD_AUTO,
172 TEST_MEMFD_YES,
173 TEST_MEMFD_NO,
176 static char *get_qemu_cmd(TestServer *s,
177 int mem, enum test_memfd memfd, const char *mem_path,
178 const char *chr_opts, const char *extra)
180 if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check()) {
181 memfd = TEST_MEMFD_YES;
184 if (memfd == TEST_MEMFD_YES) {
185 return g_strdup_printf(QEMU_CMD_MEMFD QEMU_CMD_CHR
186 QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem,
187 s->chr_name, s->socket_path,
188 chr_opts, s->chr_name, extra);
189 } else {
190 return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR
191 QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem,
192 mem_path, s->chr_name, s->socket_path,
193 chr_opts, s->chr_name, extra);
197 static void init_virtio_dev(TestServer *s, uint32_t features_mask)
199 uint32_t features;
200 int i;
202 s->bus = qpci_init_pc(global_qtest, NULL);
203 g_assert_nonnull(s->bus);
205 s->dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
206 g_assert_nonnull(s->dev);
208 qvirtio_pci_device_enable(s->dev);
209 qvirtio_reset(&s->dev->vdev);
210 qvirtio_set_acknowledge(&s->dev->vdev);
211 qvirtio_set_driver(&s->dev->vdev);
213 s->alloc = pc_alloc_init(global_qtest);
215 for (i = 0; i < s->queues * 2; i++) {
216 s->vq[i] = qvirtqueue_setup(&s->dev->vdev, s->alloc, i);
219 features = qvirtio_get_features(&s->dev->vdev);
220 features = features & features_mask;
221 qvirtio_set_features(&s->dev->vdev, features);
223 qvirtio_set_driver_ok(&s->dev->vdev);
226 static void uninit_virtio_dev(TestServer *s)
228 int i;
230 for (i = 0; i < s->queues * 2; i++) {
231 qvirtqueue_cleanup(s->dev->vdev.bus, s->vq[i], s->alloc);
233 pc_alloc_uninit(s->alloc);
235 qvirtio_pci_device_free(s->dev);
238 static void wait_for_fds(TestServer *s)
240 gint64 end_time;
242 g_mutex_lock(&s->data_mutex);
244 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
245 while (!s->fds_num) {
246 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
247 /* timeout has passed */
248 g_assert(s->fds_num);
249 break;
253 /* check for sanity */
254 g_assert_cmpint(s->fds_num, >, 0);
255 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
257 g_mutex_unlock(&s->data_mutex);
260 static void read_guest_mem_server(TestServer *s)
262 uint32_t *guest_mem;
263 int i, j;
264 size_t size;
266 wait_for_fds(s);
268 g_mutex_lock(&s->data_mutex);
270 /* iterate all regions */
271 for (i = 0; i < s->fds_num; i++) {
273 /* We'll check only the region statring at 0x0*/
274 if (s->memory.regions[i].guest_phys_addr != 0x0) {
275 continue;
278 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
280 size = s->memory.regions[i].memory_size +
281 s->memory.regions[i].mmap_offset;
283 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
284 MAP_SHARED, s->fds[i], 0);
286 g_assert(guest_mem != MAP_FAILED);
287 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
289 for (j = 0; j < 256; j++) {
290 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
291 uint32_t b = guest_mem[j];
293 g_assert_cmpint(a, ==, b);
296 munmap(guest_mem, s->memory.regions[i].memory_size);
299 g_mutex_unlock(&s->data_mutex);
302 static void *thread_function(void *data)
304 GMainLoop *loop = data;
305 g_main_loop_run(loop);
306 return NULL;
309 static int chr_can_read(void *opaque)
311 return VHOST_USER_HDR_SIZE;
314 static void chr_read(void *opaque, const uint8_t *buf, int size)
316 TestServer *s = opaque;
317 CharBackend *chr = &s->chr;
318 VhostUserMsg msg;
319 uint8_t *p = (uint8_t *) &msg;
320 int fd;
322 if (s->test_fail) {
323 qemu_chr_fe_disconnect(chr);
324 /* now switch to non-failure */
325 s->test_fail = false;
328 if (size != VHOST_USER_HDR_SIZE) {
329 g_test_message("Wrong message size received %d\n", size);
330 return;
333 g_mutex_lock(&s->data_mutex);
334 memcpy(p, buf, VHOST_USER_HDR_SIZE);
336 if (msg.size) {
337 p += VHOST_USER_HDR_SIZE;
338 size = qemu_chr_fe_read_all(chr, p, msg.size);
339 if (size != msg.size) {
340 g_test_message("Wrong message size received %d != %d\n",
341 size, msg.size);
342 return;
346 switch (msg.request) {
347 case VHOST_USER_GET_FEATURES:
348 /* send back features to qemu */
349 msg.flags |= VHOST_USER_REPLY_MASK;
350 msg.size = sizeof(m.payload.u64);
351 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
352 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
353 if (s->queues > 1) {
354 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
356 if (s->test_flags >= TEST_FLAGS_BAD) {
357 msg.payload.u64 = 0;
358 s->test_flags = TEST_FLAGS_END;
360 p = (uint8_t *) &msg;
361 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
362 break;
364 case VHOST_USER_SET_FEATURES:
365 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
366 !=, 0ULL);
367 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
368 qemu_chr_fe_disconnect(chr);
369 s->test_flags = TEST_FLAGS_BAD;
371 break;
373 case VHOST_USER_GET_PROTOCOL_FEATURES:
374 /* send back features to qemu */
375 msg.flags |= VHOST_USER_REPLY_MASK;
376 msg.size = sizeof(m.payload.u64);
377 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
378 if (s->queues > 1) {
379 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
381 p = (uint8_t *) &msg;
382 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
383 break;
385 case VHOST_USER_GET_VRING_BASE:
386 /* send back vring base to qemu */
387 msg.flags |= VHOST_USER_REPLY_MASK;
388 msg.size = sizeof(m.payload.state);
389 msg.payload.state.num = 0;
390 p = (uint8_t *) &msg;
391 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
393 assert(msg.payload.state.index < s->queues * 2);
394 s->rings &= ~(0x1ULL << msg.payload.state.index);
395 break;
397 case VHOST_USER_SET_MEM_TABLE:
398 /* received the mem table */
399 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
400 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
401 G_N_ELEMENTS(s->fds));
403 /* signal the test that it can continue */
404 g_cond_signal(&s->data_cond);
405 break;
407 case VHOST_USER_SET_VRING_KICK:
408 case VHOST_USER_SET_VRING_CALL:
409 /* consume the fd */
410 qemu_chr_fe_get_msgfds(chr, &fd, 1);
412 * This is a non-blocking eventfd.
413 * The receive function forces it to be blocking,
414 * so revert it back to non-blocking.
416 qemu_set_nonblock(fd);
417 break;
419 case VHOST_USER_SET_LOG_BASE:
420 if (s->log_fd != -1) {
421 close(s->log_fd);
422 s->log_fd = -1;
424 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
425 msg.flags |= VHOST_USER_REPLY_MASK;
426 msg.size = 0;
427 p = (uint8_t *) &msg;
428 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
430 g_cond_signal(&s->data_cond);
431 break;
433 case VHOST_USER_SET_VRING_BASE:
434 assert(msg.payload.state.index < s->queues * 2);
435 s->rings |= 0x1ULL << msg.payload.state.index;
436 break;
438 case VHOST_USER_GET_QUEUE_NUM:
439 msg.flags |= VHOST_USER_REPLY_MASK;
440 msg.size = sizeof(m.payload.u64);
441 msg.payload.u64 = s->queues;
442 p = (uint8_t *) &msg;
443 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
444 break;
446 default:
447 break;
450 g_mutex_unlock(&s->data_mutex);
453 static const char *init_hugepagefs(const char *path)
455 struct statfs fs;
456 int ret;
458 if (access(path, R_OK | W_OK | X_OK)) {
459 g_test_message("access on path (%s): %s\n", path, strerror(errno));
460 return NULL;
463 do {
464 ret = statfs(path, &fs);
465 } while (ret != 0 && errno == EINTR);
467 if (ret != 0) {
468 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
469 return NULL;
472 if (fs.f_type != HUGETLBFS_MAGIC) {
473 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
474 return NULL;
477 return path;
480 static TestServer *test_server_new(const gchar *name)
482 TestServer *server = g_new0(TestServer, 1);
484 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
485 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
486 server->chr_name = g_strdup_printf("chr-%s", name);
488 g_mutex_init(&server->data_mutex);
489 g_cond_init(&server->data_cond);
491 server->log_fd = -1;
492 server->queues = 1;
494 return server;
497 static void chr_event(void *opaque, int event)
499 TestServer *s = opaque;
501 if (s->test_flags == TEST_FLAGS_END &&
502 event == CHR_EVENT_CLOSED) {
503 s->test_flags = TEST_FLAGS_OK;
507 static void test_server_create_chr(TestServer *server, const gchar *opt)
509 gchar *chr_path;
510 Chardev *chr;
512 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
513 chr = qemu_chr_new(server->chr_name, chr_path);
514 g_free(chr_path);
516 g_assert_nonnull(chr);
517 qemu_chr_fe_init(&server->chr, chr, &error_abort);
518 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
519 chr_event, NULL, server, NULL, true);
522 static void test_server_listen(TestServer *server)
524 test_server_create_chr(server, ",server,nowait");
527 static gboolean _test_server_free(TestServer *server)
529 int i;
531 qemu_chr_fe_deinit(&server->chr, true);
533 for (i = 0; i < server->fds_num; i++) {
534 close(server->fds[i]);
537 if (server->log_fd != -1) {
538 close(server->log_fd);
541 unlink(server->socket_path);
542 g_free(server->socket_path);
544 unlink(server->mig_path);
545 g_free(server->mig_path);
547 g_free(server->chr_name);
548 qpci_free_pc(server->bus);
550 g_free(server);
552 return FALSE;
555 static void test_server_free(TestServer *server)
557 g_idle_add((GSourceFunc)_test_server_free, server);
560 static void wait_for_log_fd(TestServer *s)
562 gint64 end_time;
564 g_mutex_lock(&s->data_mutex);
565 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
566 while (s->log_fd == -1) {
567 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
568 /* timeout has passed */
569 g_assert(s->log_fd != -1);
570 break;
574 g_mutex_unlock(&s->data_mutex);
577 static void write_guest_mem(TestServer *s, uint32_t seed)
579 uint32_t *guest_mem;
580 int i, j;
581 size_t size;
583 wait_for_fds(s);
585 /* iterate all regions */
586 for (i = 0; i < s->fds_num; i++) {
588 /* We'll write only the region statring at 0x0 */
589 if (s->memory.regions[i].guest_phys_addr != 0x0) {
590 continue;
593 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
595 size = s->memory.regions[i].memory_size +
596 s->memory.regions[i].mmap_offset;
598 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
599 MAP_SHARED, s->fds[i], 0);
601 g_assert(guest_mem != MAP_FAILED);
602 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
604 for (j = 0; j < 256; j++) {
605 guest_mem[j] = seed + j;
608 munmap(guest_mem, s->memory.regions[i].memory_size);
609 break;
613 static guint64 get_log_size(TestServer *s)
615 guint64 log_size = 0;
616 int i;
618 for (i = 0; i < s->memory.nregions; ++i) {
619 VhostUserMemoryRegion *reg = &s->memory.regions[i];
620 guint64 last = range_get_last(reg->guest_phys_addr,
621 reg->memory_size);
622 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
625 return log_size;
628 typedef struct TestMigrateSource {
629 GSource source;
630 TestServer *src;
631 TestServer *dest;
632 } TestMigrateSource;
634 static gboolean
635 test_migrate_source_check(GSource *source)
637 TestMigrateSource *t = (TestMigrateSource *)source;
638 gboolean overlap = t->src->rings && t->dest->rings;
640 g_assert(!overlap);
642 return FALSE;
645 #if !GLIB_CHECK_VERSION(2,36,0)
646 /* this callback is unnecessary with glib >2.36, the default
647 * prepare for the source does the same */
648 static gboolean
649 test_migrate_source_prepare(GSource *source, gint *timeout)
651 *timeout = -1;
652 return FALSE;
654 #endif
656 GSourceFuncs test_migrate_source_funcs = {
657 #if !GLIB_CHECK_VERSION(2,36,0)
658 .prepare = test_migrate_source_prepare,
659 #endif
660 .check = test_migrate_source_check,
663 static void test_read_guest_mem(const void *arg)
665 enum test_memfd memfd = GPOINTER_TO_INT(arg);
666 TestServer *server = NULL;
667 char *qemu_cmd = NULL;
668 QTestState *s = NULL;
670 server = test_server_new(memfd == TEST_MEMFD_YES ?
671 "read-guest-memfd" : "read-guest-mem");
672 test_server_listen(server);
674 qemu_cmd = get_qemu_cmd(server, 512, memfd, root, "", "");
676 s = qtest_start(qemu_cmd);
677 g_free(qemu_cmd);
679 init_virtio_dev(server, 1u << VIRTIO_NET_F_MAC);
681 read_guest_mem_server(server);
683 uninit_virtio_dev(server);
685 qtest_quit(s);
686 test_server_free(server);
689 static void test_migrate(void)
691 TestServer *s = test_server_new("src");
692 TestServer *dest = test_server_new("dest");
693 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
694 QTestState *global = global_qtest, *from, *to;
695 GSource *source;
696 gchar *cmd, *tmp;
697 QDict *rsp;
698 guint8 *log;
699 guint64 size;
701 test_server_listen(s);
702 test_server_listen(dest);
704 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, "", "");
705 from = qtest_start(cmd);
706 g_free(cmd);
708 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
709 wait_for_fds(s);
710 size = get_log_size(s);
711 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
713 tmp = g_strdup_printf(" -incoming %s", uri);
714 cmd = get_qemu_cmd(dest, 2, TEST_MEMFD_AUTO, root, "", tmp);
715 g_free(tmp);
716 to = qtest_init(cmd);
717 g_free(cmd);
719 source = g_source_new(&test_migrate_source_funcs,
720 sizeof(TestMigrateSource));
721 ((TestMigrateSource *)source)->src = s;
722 ((TestMigrateSource *)source)->dest = dest;
723 g_source_attach(source, NULL);
725 /* slow down migration to have time to fiddle with log */
726 /* TODO: qtest could learn to break on some places */
727 rsp = qmp("{ 'execute': 'migrate_set_speed',"
728 "'arguments': { 'value': 10 } }");
729 g_assert(qdict_haskey(rsp, "return"));
730 qobject_unref(rsp);
732 cmd = g_strdup_printf("{ 'execute': 'migrate',"
733 "'arguments': { 'uri': '%s' } }",
734 uri);
735 rsp = qmp(cmd);
736 g_free(cmd);
737 g_assert(qdict_haskey(rsp, "return"));
738 qobject_unref(rsp);
740 wait_for_log_fd(s);
742 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
743 g_assert(log != MAP_FAILED);
745 /* modify first page */
746 write_guest_mem(s, 0x42);
747 log[0] = 1;
748 munmap(log, size);
750 /* speed things up */
751 rsp = qmp("{ 'execute': 'migrate_set_speed',"
752 "'arguments': { 'value': 0 } }");
753 g_assert(qdict_haskey(rsp, "return"));
754 qobject_unref(rsp);
756 qmp_eventwait("STOP");
758 global_qtest = to;
759 qmp_eventwait("RESUME");
761 read_guest_mem_server(dest);
763 uninit_virtio_dev(s);
765 g_source_destroy(source);
766 g_source_unref(source);
768 qtest_quit(to);
769 test_server_free(dest);
770 qtest_quit(from);
771 test_server_free(s);
772 g_free(uri);
774 global_qtest = global;
777 static void wait_for_rings_started(TestServer *s, size_t count)
779 gint64 end_time;
781 g_mutex_lock(&s->data_mutex);
782 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
783 while (ctpop64(s->rings) != count) {
784 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
785 /* timeout has passed */
786 g_assert_cmpint(ctpop64(s->rings), ==, count);
787 break;
791 g_mutex_unlock(&s->data_mutex);
794 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
795 static inline void test_server_connect(TestServer *server)
797 test_server_create_chr(server, ",reconnect=1");
800 static gboolean
801 reconnect_cb(gpointer user_data)
803 TestServer *s = user_data;
805 qemu_chr_fe_disconnect(&s->chr);
807 return FALSE;
810 static gpointer
811 connect_thread(gpointer data)
813 TestServer *s = data;
815 /* wait for qemu to start before first try, to avoid extra warnings */
816 g_usleep(G_USEC_PER_SEC);
817 test_server_connect(s);
819 return NULL;
822 static void test_reconnect_subprocess(void)
824 TestServer *s = test_server_new("reconnect");
825 char *cmd;
827 g_thread_new("connect", connect_thread, s);
828 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
829 qtest_start(cmd);
830 g_free(cmd);
832 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
833 wait_for_fds(s);
834 wait_for_rings_started(s, 2);
836 /* reconnect */
837 s->fds_num = 0;
838 s->rings = 0;
839 g_idle_add(reconnect_cb, s);
840 wait_for_fds(s);
841 wait_for_rings_started(s, 2);
843 uninit_virtio_dev(s);
845 qtest_end();
846 test_server_free(s);
847 return;
850 static void test_reconnect(void)
852 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
853 qtest_get_arch());
854 g_test_trap_subprocess(path, 0, 0);
855 g_test_trap_assert_passed();
856 g_free(path);
859 static void test_connect_fail_subprocess(void)
861 TestServer *s = test_server_new("connect-fail");
862 char *cmd;
864 s->test_fail = true;
865 g_thread_new("connect", connect_thread, s);
866 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
867 qtest_start(cmd);
868 g_free(cmd);
870 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
871 wait_for_fds(s);
872 wait_for_rings_started(s, 2);
874 uninit_virtio_dev(s);
876 qtest_end();
877 test_server_free(s);
880 static void test_connect_fail(void)
882 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
883 qtest_get_arch());
884 g_test_trap_subprocess(path, 0, 0);
885 g_test_trap_assert_passed();
886 g_free(path);
889 static void test_flags_mismatch_subprocess(void)
891 TestServer *s = test_server_new("flags-mismatch");
892 char *cmd;
894 s->test_flags = TEST_FLAGS_DISCONNECT;
895 g_thread_new("connect", connect_thread, s);
896 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
897 qtest_start(cmd);
898 g_free(cmd);
900 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
901 wait_for_fds(s);
902 wait_for_rings_started(s, 2);
904 uninit_virtio_dev(s);
906 qtest_end();
907 test_server_free(s);
910 static void test_flags_mismatch(void)
912 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
913 qtest_get_arch());
914 g_test_trap_subprocess(path, 0, 0);
915 g_test_trap_assert_passed();
916 g_free(path);
919 #endif
921 static void test_multiqueue(void)
923 TestServer *s = test_server_new("mq");
924 char *cmd;
925 uint32_t features_mask = ~(QVIRTIO_F_BAD_FEATURE |
926 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
927 (1u << VIRTIO_RING_F_EVENT_IDX));
928 s->queues = 2;
929 test_server_listen(s);
931 if (qemu_memfd_check()) {
932 cmd = g_strdup_printf(
933 QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
934 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
935 512, 512, s->chr_name,
936 s->socket_path, "", s->chr_name,
937 s->queues, s->queues * 2 + 2);
938 } else {
939 cmd = g_strdup_printf(
940 QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
941 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
942 512, 512, root, s->chr_name,
943 s->socket_path, "", s->chr_name,
944 s->queues, s->queues * 2 + 2);
946 qtest_start(cmd);
947 g_free(cmd);
949 init_virtio_dev(s, features_mask);
951 wait_for_rings_started(s, s->queues * 2);
953 uninit_virtio_dev(s);
955 qtest_end();
957 test_server_free(s);
960 int main(int argc, char **argv)
962 const char *hugefs;
963 int ret;
964 char template[] = "/tmp/vhost-test-XXXXXX";
965 GMainLoop *loop;
966 GThread *thread;
968 g_test_init(&argc, &argv, NULL);
970 module_call_init(MODULE_INIT_QOM);
971 qemu_add_opts(&qemu_chardev_opts);
973 tmpfs = mkdtemp(template);
974 if (!tmpfs) {
975 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
977 g_assert(tmpfs);
979 hugefs = getenv("QTEST_HUGETLBFS_PATH");
980 if (hugefs) {
981 root = init_hugepagefs(hugefs);
982 g_assert(root);
983 } else {
984 root = tmpfs;
987 loop = g_main_loop_new(NULL, FALSE);
988 /* run the main loop thread so the chardev may operate */
989 thread = g_thread_new(NULL, thread_function, loop);
991 if (qemu_memfd_check()) {
992 qtest_add_data_func("/vhost-user/read-guest-mem/memfd",
993 GINT_TO_POINTER(TEST_MEMFD_YES),
994 test_read_guest_mem);
996 qtest_add_data_func("/vhost-user/read-guest-mem/memfile",
997 GINT_TO_POINTER(TEST_MEMFD_NO), test_read_guest_mem);
998 qtest_add_func("/vhost-user/migrate", test_migrate);
999 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
1001 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
1002 /* keeps failing on build-system since Aug 15 2017 */
1003 if (getenv("QTEST_VHOST_USER_FIXME")) {
1004 qtest_add_func("/vhost-user/reconnect/subprocess",
1005 test_reconnect_subprocess);
1006 qtest_add_func("/vhost-user/reconnect", test_reconnect);
1007 qtest_add_func("/vhost-user/connect-fail/subprocess",
1008 test_connect_fail_subprocess);
1009 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
1010 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
1011 test_flags_mismatch_subprocess);
1012 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
1014 #endif
1016 ret = g_test_run();
1018 /* cleanup */
1020 /* finish the helper thread and dispatch pending sources */
1021 g_main_loop_quit(loop);
1022 g_thread_join(thread);
1023 while (g_main_context_pending(NULL)) {
1024 g_main_context_iteration (NULL, TRUE);
1026 g_main_loop_unref(loop);
1028 ret = rmdir(tmpfs);
1029 if (ret != 0) {
1030 g_test_message("unable to rmdir: path (%s): %s\n",
1031 tmpfs, strerror(errno));
1033 g_assert_cmpint(ret, ==, 0);
1035 return ret;