qos-test: virtio-blk test node
[qemu/ar7.git] / tests / vhost-user-test.c
blobb1414e2b4459724957b10565e94eeb27a7822516
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=net0,chardev=%s,vhostforce"
45 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
47 #define HUGETLBFS_MAGIC 0x958458f6
49 /*********** FROM hw/virtio/vhost-user.c *************************************/
51 #define VHOST_MEMORY_MAX_NREGIONS 8
52 #define VHOST_MAX_VIRTQUEUES 0x100
54 #define VHOST_USER_F_PROTOCOL_FEATURES 30
55 #define VHOST_USER_PROTOCOL_F_MQ 0
56 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
57 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
59 #define VHOST_LOG_PAGE 0x1000
61 typedef enum VhostUserRequest {
62 VHOST_USER_NONE = 0,
63 VHOST_USER_GET_FEATURES = 1,
64 VHOST_USER_SET_FEATURES = 2,
65 VHOST_USER_SET_OWNER = 3,
66 VHOST_USER_RESET_OWNER = 4,
67 VHOST_USER_SET_MEM_TABLE = 5,
68 VHOST_USER_SET_LOG_BASE = 6,
69 VHOST_USER_SET_LOG_FD = 7,
70 VHOST_USER_SET_VRING_NUM = 8,
71 VHOST_USER_SET_VRING_ADDR = 9,
72 VHOST_USER_SET_VRING_BASE = 10,
73 VHOST_USER_GET_VRING_BASE = 11,
74 VHOST_USER_SET_VRING_KICK = 12,
75 VHOST_USER_SET_VRING_CALL = 13,
76 VHOST_USER_SET_VRING_ERR = 14,
77 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
78 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
79 VHOST_USER_GET_QUEUE_NUM = 17,
80 VHOST_USER_SET_VRING_ENABLE = 18,
81 VHOST_USER_MAX
82 } VhostUserRequest;
84 typedef struct VhostUserMemoryRegion {
85 uint64_t guest_phys_addr;
86 uint64_t memory_size;
87 uint64_t userspace_addr;
88 uint64_t mmap_offset;
89 } VhostUserMemoryRegion;
91 typedef struct VhostUserMemory {
92 uint32_t nregions;
93 uint32_t padding;
94 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
95 } VhostUserMemory;
97 typedef struct VhostUserLog {
98 uint64_t mmap_size;
99 uint64_t mmap_offset;
100 } VhostUserLog;
102 typedef struct VhostUserMsg {
103 VhostUserRequest request;
105 #define VHOST_USER_VERSION_MASK (0x3)
106 #define VHOST_USER_REPLY_MASK (0x1<<2)
107 uint32_t flags;
108 uint32_t size; /* the following payload size */
109 union {
110 #define VHOST_USER_VRING_IDX_MASK (0xff)
111 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
112 uint64_t u64;
113 struct vhost_vring_state state;
114 struct vhost_vring_addr addr;
115 VhostUserMemory memory;
116 VhostUserLog log;
117 } payload;
118 } QEMU_PACKED VhostUserMsg;
120 static VhostUserMsg m __attribute__ ((unused));
121 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
122 + sizeof(m.flags) \
123 + sizeof(m.size))
125 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
127 /* The version of the protocol we support */
128 #define VHOST_USER_VERSION (0x1)
129 /*****************************************************************************/
131 enum {
132 TEST_FLAGS_OK,
133 TEST_FLAGS_DISCONNECT,
134 TEST_FLAGS_BAD,
135 TEST_FLAGS_END,
138 typedef struct TestServer {
139 QPCIBus *bus;
140 QVirtioPCIDevice *dev;
141 QVirtQueue *vq[VHOST_MAX_VIRTQUEUES];
142 gchar *socket_path;
143 gchar *mig_path;
144 gchar *chr_name;
145 const gchar *mem_path;
146 gchar *tmpfs;
147 CharBackend chr;
148 int fds_num;
149 int fds[VHOST_MEMORY_MAX_NREGIONS];
150 VhostUserMemory memory;
151 GMainContext *context;
152 GMainLoop *loop;
153 GThread *thread;
154 GMutex data_mutex;
155 GCond data_cond;
156 int log_fd;
157 uint64_t rings;
158 bool test_fail;
159 int test_flags;
160 int queues;
161 QGuestAllocator alloc;
162 } TestServer;
164 static TestServer *test_server_new(const gchar *name);
165 static void test_server_free(TestServer *server);
166 static void test_server_listen(TestServer *server);
168 enum test_memfd {
169 TEST_MEMFD_AUTO,
170 TEST_MEMFD_YES,
171 TEST_MEMFD_NO,
174 static char *get_qemu_cmd(TestServer *s,
175 int mem, enum test_memfd memfd,
176 const char *chr_opts, const char *extra)
178 if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check(0)) {
179 memfd = TEST_MEMFD_YES;
182 if (memfd == TEST_MEMFD_YES) {
183 return g_strdup_printf(QEMU_CMD_MEMFD QEMU_CMD_CHR
184 QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem,
185 s->chr_name, s->socket_path,
186 chr_opts, s->chr_name, extra);
187 } else {
188 return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR
189 QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem,
190 s->mem_path, s->chr_name, s->socket_path,
191 chr_opts, s->chr_name, extra);
195 static void init_virtio_dev(QTestState *qts, TestServer *s, uint32_t features_mask)
197 uint32_t features;
198 int i;
200 s->bus = qpci_new_pc(qts, NULL);
201 g_assert_nonnull(s->bus);
203 s->dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
204 g_assert_nonnull(s->dev);
206 qvirtio_pci_device_enable(s->dev);
207 qvirtio_start_device(&s->dev->vdev);
209 pc_alloc_init(&s->alloc, qts, 0);
211 for (i = 0; i < s->queues * 2; i++) {
212 s->vq[i] = qvirtqueue_setup(&s->dev->vdev, &s->alloc, i);
215 features = qvirtio_get_features(&s->dev->vdev);
216 features = features & features_mask;
217 qvirtio_set_features(&s->dev->vdev, features);
219 qvirtio_set_driver_ok(&s->dev->vdev);
222 static void uninit_virtio_dev(TestServer *s)
224 int i;
226 for (i = 0; i < s->queues * 2; i++) {
227 qvirtqueue_cleanup(s->dev->vdev.bus, s->vq[i], &s->alloc);
229 alloc_destroy(&s->alloc);
231 qvirtio_pci_device_free(s->dev);
234 static bool wait_for_fds(TestServer *s)
236 gint64 end_time;
237 bool got_region;
238 int i;
240 g_mutex_lock(&s->data_mutex);
242 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
243 while (!s->fds_num) {
244 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
245 /* timeout has passed */
246 g_assert(s->fds_num);
247 break;
251 /* check for sanity */
252 g_assert_cmpint(s->fds_num, >, 0);
253 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
255 g_mutex_unlock(&s->data_mutex);
257 got_region = false;
258 for (i = 0; i < s->memory.nregions; ++i) {
259 VhostUserMemoryRegion *reg = &s->memory.regions[i];
260 if (reg->guest_phys_addr == 0) {
261 got_region = true;
262 break;
265 if (!got_region) {
266 g_test_skip("No memory at address 0x0");
268 return got_region;
271 static void read_guest_mem_server(QTestState *qts, TestServer *s)
273 uint8_t *guest_mem;
274 int i, j;
275 size_t size;
277 g_mutex_lock(&s->data_mutex);
279 /* iterate all regions */
280 for (i = 0; i < s->fds_num; i++) {
282 /* We'll check only the region statring at 0x0*/
283 if (s->memory.regions[i].guest_phys_addr != 0x0) {
284 continue;
287 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
289 size = s->memory.regions[i].memory_size +
290 s->memory.regions[i].mmap_offset;
292 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
293 MAP_SHARED, s->fds[i], 0);
295 g_assert(guest_mem != MAP_FAILED);
296 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
298 for (j = 0; j < 1024; j++) {
299 uint32_t a = qtest_readb(qts, s->memory.regions[i].guest_phys_addr + j);
300 uint32_t b = guest_mem[j];
302 g_assert_cmpint(a, ==, b);
305 munmap(guest_mem, s->memory.regions[i].memory_size);
308 g_mutex_unlock(&s->data_mutex);
311 static void *thread_function(void *data)
313 GMainLoop *loop = data;
314 g_main_loop_run(loop);
315 return NULL;
318 static int chr_can_read(void *opaque)
320 return VHOST_USER_HDR_SIZE;
323 static void chr_read(void *opaque, const uint8_t *buf, int size)
325 TestServer *s = opaque;
326 CharBackend *chr = &s->chr;
327 VhostUserMsg msg;
328 uint8_t *p = (uint8_t *) &msg;
329 int fd = -1;
331 if (s->test_fail) {
332 qemu_chr_fe_disconnect(chr);
333 /* now switch to non-failure */
334 s->test_fail = false;
337 if (size != VHOST_USER_HDR_SIZE) {
338 g_test_message("Wrong message size received %d\n", size);
339 return;
342 g_mutex_lock(&s->data_mutex);
343 memcpy(p, buf, VHOST_USER_HDR_SIZE);
345 if (msg.size) {
346 p += VHOST_USER_HDR_SIZE;
347 size = qemu_chr_fe_read_all(chr, p, msg.size);
348 if (size != msg.size) {
349 g_test_message("Wrong message size received %d != %d\n",
350 size, msg.size);
351 return;
355 switch (msg.request) {
356 case VHOST_USER_GET_FEATURES:
357 /* send back features to qemu */
358 msg.flags |= VHOST_USER_REPLY_MASK;
359 msg.size = sizeof(m.payload.u64);
360 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
361 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
362 if (s->queues > 1) {
363 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
365 if (s->test_flags >= TEST_FLAGS_BAD) {
366 msg.payload.u64 = 0;
367 s->test_flags = TEST_FLAGS_END;
369 p = (uint8_t *) &msg;
370 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
371 break;
373 case VHOST_USER_SET_FEATURES:
374 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
375 !=, 0ULL);
376 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
377 qemu_chr_fe_disconnect(chr);
378 s->test_flags = TEST_FLAGS_BAD;
380 break;
382 case VHOST_USER_GET_PROTOCOL_FEATURES:
383 /* send back features to qemu */
384 msg.flags |= VHOST_USER_REPLY_MASK;
385 msg.size = sizeof(m.payload.u64);
386 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
387 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN;
388 if (s->queues > 1) {
389 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
391 p = (uint8_t *) &msg;
392 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
393 break;
395 case VHOST_USER_GET_VRING_BASE:
396 /* send back vring base to qemu */
397 msg.flags |= VHOST_USER_REPLY_MASK;
398 msg.size = sizeof(m.payload.state);
399 msg.payload.state.num = 0;
400 p = (uint8_t *) &msg;
401 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
403 assert(msg.payload.state.index < s->queues * 2);
404 s->rings &= ~(0x1ULL << msg.payload.state.index);
405 g_cond_broadcast(&s->data_cond);
406 break;
408 case VHOST_USER_SET_MEM_TABLE:
409 /* received the mem table */
410 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
411 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
412 G_N_ELEMENTS(s->fds));
414 /* signal the test that it can continue */
415 g_cond_broadcast(&s->data_cond);
416 break;
418 case VHOST_USER_SET_VRING_KICK:
419 case VHOST_USER_SET_VRING_CALL:
420 /* consume the fd */
421 qemu_chr_fe_get_msgfds(chr, &fd, 1);
423 * This is a non-blocking eventfd.
424 * The receive function forces it to be blocking,
425 * so revert it back to non-blocking.
427 qemu_set_nonblock(fd);
428 break;
430 case VHOST_USER_SET_LOG_BASE:
431 if (s->log_fd != -1) {
432 close(s->log_fd);
433 s->log_fd = -1;
435 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
436 msg.flags |= VHOST_USER_REPLY_MASK;
437 msg.size = 0;
438 p = (uint8_t *) &msg;
439 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
441 g_cond_broadcast(&s->data_cond);
442 break;
444 case VHOST_USER_SET_VRING_BASE:
445 assert(msg.payload.state.index < s->queues * 2);
446 s->rings |= 0x1ULL << msg.payload.state.index;
447 g_cond_broadcast(&s->data_cond);
448 break;
450 case VHOST_USER_GET_QUEUE_NUM:
451 msg.flags |= VHOST_USER_REPLY_MASK;
452 msg.size = sizeof(m.payload.u64);
453 msg.payload.u64 = s->queues;
454 p = (uint8_t *) &msg;
455 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
456 break;
458 default:
459 break;
462 g_mutex_unlock(&s->data_mutex);
465 static const char *init_hugepagefs(void)
467 #ifdef CONFIG_LINUX
468 const char *path = getenv("QTEST_HUGETLBFS_PATH");
469 struct statfs fs;
470 int ret;
472 if (!path) {
473 return NULL;
476 if (access(path, R_OK | W_OK | X_OK)) {
477 g_test_message("access on path (%s): %s\n", path, strerror(errno));
478 abort();
479 return NULL;
482 do {
483 ret = statfs(path, &fs);
484 } while (ret != 0 && errno == EINTR);
486 if (ret != 0) {
487 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
488 abort();
489 return NULL;
492 if (fs.f_type != HUGETLBFS_MAGIC) {
493 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
494 abort();
495 return NULL;
498 return path;
499 #else
500 return NULL;
501 #endif
504 static TestServer *test_server_new(const gchar *name)
506 TestServer *server = g_new0(TestServer, 1);
507 char template[] = "/tmp/vhost-test-XXXXXX";
508 const char *tmpfs;
510 server->context = g_main_context_new();
511 server->loop = g_main_loop_new(server->context, FALSE);
513 /* run the main loop thread so the chardev may operate */
514 server->thread = g_thread_new(NULL, thread_function, server->loop);
516 tmpfs = mkdtemp(template);
517 if (!tmpfs) {
518 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
520 g_assert(tmpfs);
522 server->tmpfs = g_strdup(tmpfs);
523 server->mem_path = init_hugepagefs() ? : server->tmpfs;
524 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
525 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
526 server->chr_name = g_strdup_printf("chr-%s", name);
528 g_mutex_init(&server->data_mutex);
529 g_cond_init(&server->data_cond);
531 server->log_fd = -1;
532 server->queues = 1;
534 return server;
537 static void chr_event(void *opaque, int event)
539 TestServer *s = opaque;
541 if (s->test_flags == TEST_FLAGS_END &&
542 event == CHR_EVENT_CLOSED) {
543 s->test_flags = TEST_FLAGS_OK;
547 static void test_server_create_chr(TestServer *server, const gchar *opt)
549 gchar *chr_path;
550 Chardev *chr;
552 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
553 chr = qemu_chr_new(server->chr_name, chr_path, server->context);
554 g_free(chr_path);
556 g_assert_nonnull(chr);
557 qemu_chr_fe_init(&server->chr, chr, &error_abort);
558 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
559 chr_event, NULL, server, server->context, true);
562 static void test_server_listen(TestServer *server)
564 test_server_create_chr(server, ",server,nowait");
567 static void test_server_free(TestServer *server)
569 int i, ret;
571 /* finish the helper thread and dispatch pending sources */
572 g_main_loop_quit(server->loop);
573 g_thread_join(server->thread);
574 while (g_main_context_pending(NULL)) {
575 g_main_context_iteration(NULL, TRUE);
578 unlink(server->socket_path);
579 g_free(server->socket_path);
581 unlink(server->mig_path);
582 g_free(server->mig_path);
584 ret = rmdir(server->tmpfs);
585 if (ret != 0) {
586 g_test_message("unable to rmdir: path (%s): %s",
587 server->tmpfs, strerror(errno));
590 qemu_chr_fe_deinit(&server->chr, true);
592 for (i = 0; i < server->fds_num; i++) {
593 close(server->fds[i]);
596 if (server->log_fd != -1) {
597 close(server->log_fd);
600 g_free(server->chr_name);
601 g_assert(server->bus);
602 qpci_free_pc(server->bus);
604 g_main_loop_unref(server->loop);
605 g_main_context_unref(server->context);
606 g_free(server);
609 static void wait_for_log_fd(TestServer *s)
611 gint64 end_time;
613 g_mutex_lock(&s->data_mutex);
614 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
615 while (s->log_fd == -1) {
616 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
617 /* timeout has passed */
618 g_assert(s->log_fd != -1);
619 break;
623 g_mutex_unlock(&s->data_mutex);
626 static void write_guest_mem(TestServer *s, uint32_t seed)
628 uint32_t *guest_mem;
629 int i, j;
630 size_t size;
632 /* iterate all regions */
633 for (i = 0; i < s->fds_num; i++) {
635 /* We'll write only the region statring at 0x0 */
636 if (s->memory.regions[i].guest_phys_addr != 0x0) {
637 continue;
640 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
642 size = s->memory.regions[i].memory_size +
643 s->memory.regions[i].mmap_offset;
645 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
646 MAP_SHARED, s->fds[i], 0);
648 g_assert(guest_mem != MAP_FAILED);
649 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
651 for (j = 0; j < 256; j++) {
652 guest_mem[j] = seed + j;
655 munmap(guest_mem, s->memory.regions[i].memory_size);
656 break;
660 static guint64 get_log_size(TestServer *s)
662 guint64 log_size = 0;
663 int i;
665 for (i = 0; i < s->memory.nregions; ++i) {
666 VhostUserMemoryRegion *reg = &s->memory.regions[i];
667 guint64 last = range_get_last(reg->guest_phys_addr,
668 reg->memory_size);
669 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
672 return log_size;
675 typedef struct TestMigrateSource {
676 GSource source;
677 TestServer *src;
678 TestServer *dest;
679 } TestMigrateSource;
681 static gboolean
682 test_migrate_source_check(GSource *source)
684 TestMigrateSource *t = (TestMigrateSource *)source;
685 gboolean overlap = t->src->rings && t->dest->rings;
687 g_assert(!overlap);
689 return FALSE;
692 GSourceFuncs test_migrate_source_funcs = {
693 .check = test_migrate_source_check,
696 static void test_read_guest_mem(const void *arg)
698 enum test_memfd memfd = GPOINTER_TO_INT(arg);
699 TestServer *server = NULL;
700 char *qemu_cmd = NULL;
701 QTestState *s = NULL;
703 server = test_server_new(memfd == TEST_MEMFD_YES ?
704 "read-guest-memfd" : "read-guest-mem");
705 test_server_listen(server);
707 qemu_cmd = get_qemu_cmd(server, 512, memfd, "", "");
709 s = qtest_start(qemu_cmd);
710 g_free(qemu_cmd);
712 init_virtio_dev(global_qtest, server, 1u << VIRTIO_NET_F_MAC);
714 if (!wait_for_fds(server)) {
715 goto exit;
718 read_guest_mem_server(global_qtest, server);
720 exit:
721 uninit_virtio_dev(server);
723 qtest_quit(s);
724 test_server_free(server);
727 static void test_migrate(void)
729 TestServer *s = test_server_new("src");
730 TestServer *dest = test_server_new("dest");
731 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
732 QTestState *from, *to;
733 GSource *source;
734 gchar *cmd, *tmp;
735 QDict *rsp;
736 guint8 *log;
737 guint64 size;
739 test_server_listen(s);
740 test_server_listen(dest);
742 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, "", "");
743 from = qtest_start(cmd);
744 g_free(cmd);
746 init_virtio_dev(from, s, 1u << VIRTIO_NET_F_MAC);
747 if (!wait_for_fds(s)) {
748 goto exit;
751 size = get_log_size(s);
752 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
754 tmp = g_strdup_printf(" -incoming %s", uri);
755 cmd = get_qemu_cmd(dest, 2, TEST_MEMFD_AUTO, "", tmp);
756 g_free(tmp);
757 to = qtest_init(cmd);
758 g_free(cmd);
759 init_virtio_dev(to, dest, 1u << VIRTIO_NET_F_MAC);
761 source = g_source_new(&test_migrate_source_funcs,
762 sizeof(TestMigrateSource));
763 ((TestMigrateSource *)source)->src = s;
764 ((TestMigrateSource *)source)->dest = dest;
765 g_source_attach(source, s->context);
767 /* slow down migration to have time to fiddle with log */
768 /* TODO: qtest could learn to break on some places */
769 rsp = qmp("{ 'execute': 'migrate_set_speed',"
770 "'arguments': { 'value': 10 } }");
771 g_assert(qdict_haskey(rsp, "return"));
772 qobject_unref(rsp);
774 rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
775 g_assert(qdict_haskey(rsp, "return"));
776 qobject_unref(rsp);
778 wait_for_log_fd(s);
780 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
781 g_assert(log != MAP_FAILED);
783 /* modify first page */
784 write_guest_mem(s, 0x42);
785 log[0] = 1;
786 munmap(log, size);
788 /* speed things up */
789 rsp = qmp("{ 'execute': 'migrate_set_speed',"
790 "'arguments': { 'value': 0 } }");
791 g_assert(qdict_haskey(rsp, "return"));
792 qobject_unref(rsp);
794 qmp_eventwait("STOP");
795 qtest_qmp_eventwait(to, "RESUME");
797 g_assert(wait_for_fds(dest));
798 read_guest_mem_server(to, dest);
800 uninit_virtio_dev(dest);
801 qtest_quit(to);
803 g_source_destroy(source);
804 g_source_unref(source);
806 exit:
807 uninit_virtio_dev(s);
809 test_server_free(dest);
810 qtest_quit(from);
811 test_server_free(s);
812 g_free(uri);
815 static void wait_for_rings_started(TestServer *s, size_t count)
817 gint64 end_time;
819 g_mutex_lock(&s->data_mutex);
820 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
821 while (ctpop64(s->rings) != count) {
822 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
823 /* timeout has passed */
824 g_assert_cmpint(ctpop64(s->rings), ==, count);
825 break;
829 g_mutex_unlock(&s->data_mutex);
832 static inline void test_server_connect(TestServer *server)
834 test_server_create_chr(server, ",reconnect=1");
837 static gboolean
838 reconnect_cb(gpointer user_data)
840 TestServer *s = user_data;
842 qemu_chr_fe_disconnect(&s->chr);
844 return FALSE;
847 static gpointer
848 connect_thread(gpointer data)
850 TestServer *s = data;
852 /* wait for qemu to start before first try, to avoid extra warnings */
853 g_usleep(G_USEC_PER_SEC);
854 test_server_connect(s);
856 return NULL;
859 static void test_reconnect_subprocess(void)
861 TestServer *s = test_server_new("reconnect");
862 GSource *src;
863 char *cmd;
865 g_thread_new("connect", connect_thread, s);
866 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, ",server", "");
867 qtest_start(cmd);
868 g_free(cmd);
870 init_virtio_dev(global_qtest, s, 1u << VIRTIO_NET_F_MAC);
871 if (!wait_for_fds(s)) {
872 goto exit;
875 wait_for_rings_started(s, 2);
877 /* reconnect */
878 s->fds_num = 0;
879 s->rings = 0;
880 src = g_idle_source_new();
881 g_source_set_callback(src, reconnect_cb, s, NULL);
882 g_source_attach(src, s->context);
883 g_source_unref(src);
884 g_assert(wait_for_fds(s));
885 wait_for_rings_started(s, 2);
887 exit:
888 uninit_virtio_dev(s);
890 qtest_end();
891 test_server_free(s);
892 return;
895 static void test_reconnect(void)
897 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
898 qtest_get_arch());
899 g_test_trap_subprocess(path, 0, 0);
900 g_test_trap_assert_passed();
901 g_free(path);
904 static void test_connect_fail_subprocess(void)
906 TestServer *s = test_server_new("connect-fail");
907 char *cmd;
909 s->test_fail = true;
910 g_thread_new("connect", connect_thread, s);
911 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, ",server", "");
912 qtest_start(cmd);
913 g_free(cmd);
915 init_virtio_dev(global_qtest, s, 1u << VIRTIO_NET_F_MAC);
916 if (!wait_for_fds(s)) {
917 goto exit;
919 wait_for_rings_started(s, 2);
921 exit:
922 uninit_virtio_dev(s);
924 qtest_end();
925 test_server_free(s);
928 static void test_connect_fail(void)
930 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
931 qtest_get_arch());
932 g_test_trap_subprocess(path, 0, 0);
933 g_test_trap_assert_passed();
934 g_free(path);
937 static void test_flags_mismatch_subprocess(void)
939 TestServer *s = test_server_new("flags-mismatch");
940 char *cmd;
942 s->test_flags = TEST_FLAGS_DISCONNECT;
943 g_thread_new("connect", connect_thread, s);
944 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, ",server", "");
945 qtest_start(cmd);
946 g_free(cmd);
948 init_virtio_dev(global_qtest, s, 1u << VIRTIO_NET_F_MAC);
949 if (!wait_for_fds(s)) {
950 goto exit;
952 wait_for_rings_started(s, 2);
954 exit:
955 uninit_virtio_dev(s);
957 qtest_end();
958 test_server_free(s);
961 static void test_flags_mismatch(void)
963 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
964 qtest_get_arch());
965 g_test_trap_subprocess(path, 0, 0);
966 g_test_trap_assert_passed();
967 g_free(path);
971 static void test_multiqueue(void)
973 TestServer *s = test_server_new("mq");
974 char *cmd;
975 uint32_t features_mask = ~(QVIRTIO_F_BAD_FEATURE |
976 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
977 (1u << VIRTIO_RING_F_EVENT_IDX));
978 s->queues = 2;
979 test_server_listen(s);
981 if (qemu_memfd_check(0)) {
982 cmd = g_strdup_printf(
983 QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
984 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
985 512, 512, s->chr_name,
986 s->socket_path, "", s->chr_name,
987 s->queues, s->queues * 2 + 2);
988 } else {
989 cmd = g_strdup_printf(
990 QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
991 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
992 512, 512, s->mem_path, s->chr_name,
993 s->socket_path, "", s->chr_name,
994 s->queues, s->queues * 2 + 2);
996 qtest_start(cmd);
997 g_free(cmd);
999 init_virtio_dev(global_qtest, s, features_mask);
1001 wait_for_rings_started(s, s->queues * 2);
1003 uninit_virtio_dev(s);
1005 qtest_end();
1007 test_server_free(s);
1010 int main(int argc, char **argv)
1012 g_test_init(&argc, &argv, NULL);
1014 module_call_init(MODULE_INIT_QOM);
1015 qemu_add_opts(&qemu_chardev_opts);
1017 if (qemu_memfd_check(0)) {
1018 qtest_add_data_func("/vhost-user/read-guest-mem/memfd",
1019 GINT_TO_POINTER(TEST_MEMFD_YES),
1020 test_read_guest_mem);
1022 qtest_add_data_func("/vhost-user/read-guest-mem/memfile",
1023 GINT_TO_POINTER(TEST_MEMFD_NO), test_read_guest_mem);
1024 qtest_add_func("/vhost-user/migrate", test_migrate);
1025 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
1027 /* keeps failing on build-system since Aug 15 2017 */
1028 if (getenv("QTEST_VHOST_USER_FIXME")) {
1029 qtest_add_func("/vhost-user/reconnect/subprocess",
1030 test_reconnect_subprocess);
1031 qtest_add_func("/vhost-user/reconnect", test_reconnect);
1032 qtest_add_func("/vhost-user/connect-fail/subprocess",
1033 test_connect_fail_subprocess);
1034 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
1035 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
1036 test_flags_mismatch_subprocess);
1037 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
1040 return g_test_run();