Merge remote-tracking branch 'remotes/dgilbert/tags/pull-hmp-20180320' into staging
[qemu/ar7.git] / tests / vhost-user-test.c
blob22e9202b8dff46beca20e46a2d33ab6166257370
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 "sysemu/sysemu.h"
22 #include "libqos/libqos.h"
23 #include "libqos/pci-pc.h"
24 #include "libqos/virtio-pci.h"
26 #include "libqos/malloc-pc.h"
27 #include "hw/virtio/virtio-net.h"
29 #include <linux/vhost.h>
30 #include <linux/virtio_ids.h>
31 #include <linux/virtio_net.h>
32 #include <sys/vfs.h>
34 /* GLIB version compatibility flags */
35 #if !GLIB_CHECK_VERSION(2, 26, 0)
36 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
37 #endif
39 #if GLIB_CHECK_VERSION(2, 28, 0)
40 #define HAVE_MONOTONIC_TIME
41 #endif
43 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
44 "mem-path=%s,share=on -numa node,memdev=mem"
45 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
46 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
47 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
49 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
50 QEMU_CMD_NETDEV QEMU_CMD_NET
52 #define GET_QEMU_CMD(s) \
53 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
54 (s)->socket_path, "", (s)->chr_name)
56 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
57 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
58 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
60 #define HUGETLBFS_MAGIC 0x958458f6
62 /*********** FROM hw/virtio/vhost-user.c *************************************/
64 #define VHOST_MEMORY_MAX_NREGIONS 8
65 #define VHOST_MAX_VIRTQUEUES 0x100
67 #define VHOST_USER_F_PROTOCOL_FEATURES 30
68 #define VHOST_USER_PROTOCOL_F_MQ 0
69 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
71 #define VHOST_LOG_PAGE 0x1000
73 typedef enum VhostUserRequest {
74 VHOST_USER_NONE = 0,
75 VHOST_USER_GET_FEATURES = 1,
76 VHOST_USER_SET_FEATURES = 2,
77 VHOST_USER_SET_OWNER = 3,
78 VHOST_USER_RESET_OWNER = 4,
79 VHOST_USER_SET_MEM_TABLE = 5,
80 VHOST_USER_SET_LOG_BASE = 6,
81 VHOST_USER_SET_LOG_FD = 7,
82 VHOST_USER_SET_VRING_NUM = 8,
83 VHOST_USER_SET_VRING_ADDR = 9,
84 VHOST_USER_SET_VRING_BASE = 10,
85 VHOST_USER_GET_VRING_BASE = 11,
86 VHOST_USER_SET_VRING_KICK = 12,
87 VHOST_USER_SET_VRING_CALL = 13,
88 VHOST_USER_SET_VRING_ERR = 14,
89 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
90 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
91 VHOST_USER_GET_QUEUE_NUM = 17,
92 VHOST_USER_SET_VRING_ENABLE = 18,
93 VHOST_USER_MAX
94 } VhostUserRequest;
96 typedef struct VhostUserMemoryRegion {
97 uint64_t guest_phys_addr;
98 uint64_t memory_size;
99 uint64_t userspace_addr;
100 uint64_t mmap_offset;
101 } VhostUserMemoryRegion;
103 typedef struct VhostUserMemory {
104 uint32_t nregions;
105 uint32_t padding;
106 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
107 } VhostUserMemory;
109 typedef struct VhostUserLog {
110 uint64_t mmap_size;
111 uint64_t mmap_offset;
112 } VhostUserLog;
114 typedef struct VhostUserMsg {
115 VhostUserRequest request;
117 #define VHOST_USER_VERSION_MASK (0x3)
118 #define VHOST_USER_REPLY_MASK (0x1<<2)
119 uint32_t flags;
120 uint32_t size; /* the following payload size */
121 union {
122 #define VHOST_USER_VRING_IDX_MASK (0xff)
123 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
124 uint64_t u64;
125 struct vhost_vring_state state;
126 struct vhost_vring_addr addr;
127 VhostUserMemory memory;
128 VhostUserLog log;
129 } payload;
130 } QEMU_PACKED VhostUserMsg;
132 static VhostUserMsg m __attribute__ ((unused));
133 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
134 + sizeof(m.flags) \
135 + sizeof(m.size))
137 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
139 /* The version of the protocol we support */
140 #define VHOST_USER_VERSION (0x1)
141 /*****************************************************************************/
143 enum {
144 TEST_FLAGS_OK,
145 TEST_FLAGS_DISCONNECT,
146 TEST_FLAGS_BAD,
147 TEST_FLAGS_END,
150 typedef struct TestServer {
151 QPCIBus *bus;
152 QVirtioPCIDevice *dev;
153 QVirtQueue *vq[VHOST_MAX_VIRTQUEUES];
154 gchar *socket_path;
155 gchar *mig_path;
156 gchar *chr_name;
157 CharBackend chr;
158 int fds_num;
159 int fds[VHOST_MEMORY_MAX_NREGIONS];
160 VhostUserMemory memory;
161 CompatGMutex data_mutex;
162 CompatGCond data_cond;
163 int log_fd;
164 uint64_t rings;
165 bool test_fail;
166 int test_flags;
167 int queues;
168 QGuestAllocator *alloc;
169 } TestServer;
171 static TestServer *test_server_new(const gchar *name);
172 static void test_server_free(TestServer *server);
173 static void test_server_listen(TestServer *server);
175 static const char *tmpfs;
176 static const char *root;
178 static void init_virtio_dev(TestServer *s, uint32_t features_mask)
180 uint32_t features;
181 int i;
183 s->bus = qpci_init_pc(global_qtest, NULL);
184 g_assert_nonnull(s->bus);
186 s->dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
187 g_assert_nonnull(s->dev);
189 qvirtio_pci_device_enable(s->dev);
190 qvirtio_reset(&s->dev->vdev);
191 qvirtio_set_acknowledge(&s->dev->vdev);
192 qvirtio_set_driver(&s->dev->vdev);
194 s->alloc = pc_alloc_init(global_qtest);
196 for (i = 0; i < s->queues * 2; i++) {
197 s->vq[i] = qvirtqueue_setup(&s->dev->vdev, s->alloc, i);
200 features = qvirtio_get_features(&s->dev->vdev);
201 features = features & features_mask;
202 qvirtio_set_features(&s->dev->vdev, features);
204 qvirtio_set_driver_ok(&s->dev->vdev);
207 static void uninit_virtio_dev(TestServer *s)
209 int i;
211 for (i = 0; i < s->queues * 2; i++) {
212 qvirtqueue_cleanup(s->dev->vdev.bus, s->vq[i], s->alloc);
214 pc_alloc_uninit(s->alloc);
216 qvirtio_pci_device_free(s->dev);
219 static void wait_for_fds(TestServer *s)
221 gint64 end_time;
223 g_mutex_lock(&s->data_mutex);
225 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
226 while (!s->fds_num) {
227 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
228 /* timeout has passed */
229 g_assert(s->fds_num);
230 break;
234 /* check for sanity */
235 g_assert_cmpint(s->fds_num, >, 0);
236 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
238 g_mutex_unlock(&s->data_mutex);
241 static void read_guest_mem_server(TestServer *s)
243 uint32_t *guest_mem;
244 int i, j;
245 size_t size;
247 wait_for_fds(s);
249 g_mutex_lock(&s->data_mutex);
251 /* iterate all regions */
252 for (i = 0; i < s->fds_num; i++) {
254 /* We'll check only the region statring at 0x0*/
255 if (s->memory.regions[i].guest_phys_addr != 0x0) {
256 continue;
259 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
261 size = s->memory.regions[i].memory_size +
262 s->memory.regions[i].mmap_offset;
264 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
265 MAP_SHARED, s->fds[i], 0);
267 g_assert(guest_mem != MAP_FAILED);
268 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
270 for (j = 0; j < 256; j++) {
271 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
272 uint32_t b = guest_mem[j];
274 g_assert_cmpint(a, ==, b);
277 munmap(guest_mem, s->memory.regions[i].memory_size);
280 g_mutex_unlock(&s->data_mutex);
283 static void *thread_function(void *data)
285 GMainLoop *loop = data;
286 g_main_loop_run(loop);
287 return NULL;
290 static int chr_can_read(void *opaque)
292 return VHOST_USER_HDR_SIZE;
295 static void chr_read(void *opaque, const uint8_t *buf, int size)
297 TestServer *s = opaque;
298 CharBackend *chr = &s->chr;
299 VhostUserMsg msg;
300 uint8_t *p = (uint8_t *) &msg;
301 int fd;
303 if (s->test_fail) {
304 qemu_chr_fe_disconnect(chr);
305 /* now switch to non-failure */
306 s->test_fail = false;
309 if (size != VHOST_USER_HDR_SIZE) {
310 g_test_message("Wrong message size received %d\n", size);
311 return;
314 g_mutex_lock(&s->data_mutex);
315 memcpy(p, buf, VHOST_USER_HDR_SIZE);
317 if (msg.size) {
318 p += VHOST_USER_HDR_SIZE;
319 size = qemu_chr_fe_read_all(chr, p, msg.size);
320 if (size != msg.size) {
321 g_test_message("Wrong message size received %d != %d\n",
322 size, msg.size);
323 return;
327 switch (msg.request) {
328 case VHOST_USER_GET_FEATURES:
329 /* send back features to qemu */
330 msg.flags |= VHOST_USER_REPLY_MASK;
331 msg.size = sizeof(m.payload.u64);
332 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
333 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
334 if (s->queues > 1) {
335 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
337 if (s->test_flags >= TEST_FLAGS_BAD) {
338 msg.payload.u64 = 0;
339 s->test_flags = TEST_FLAGS_END;
341 p = (uint8_t *) &msg;
342 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
343 break;
345 case VHOST_USER_SET_FEATURES:
346 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
347 !=, 0ULL);
348 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
349 qemu_chr_fe_disconnect(chr);
350 s->test_flags = TEST_FLAGS_BAD;
352 break;
354 case VHOST_USER_GET_PROTOCOL_FEATURES:
355 /* send back features to qemu */
356 msg.flags |= VHOST_USER_REPLY_MASK;
357 msg.size = sizeof(m.payload.u64);
358 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
359 if (s->queues > 1) {
360 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
362 p = (uint8_t *) &msg;
363 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
364 break;
366 case VHOST_USER_GET_VRING_BASE:
367 /* send back vring base to qemu */
368 msg.flags |= VHOST_USER_REPLY_MASK;
369 msg.size = sizeof(m.payload.state);
370 msg.payload.state.num = 0;
371 p = (uint8_t *) &msg;
372 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
374 assert(msg.payload.state.index < s->queues * 2);
375 s->rings &= ~(0x1ULL << msg.payload.state.index);
376 break;
378 case VHOST_USER_SET_MEM_TABLE:
379 /* received the mem table */
380 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
381 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
382 G_N_ELEMENTS(s->fds));
384 /* signal the test that it can continue */
385 g_cond_signal(&s->data_cond);
386 break;
388 case VHOST_USER_SET_VRING_KICK:
389 case VHOST_USER_SET_VRING_CALL:
390 /* consume the fd */
391 qemu_chr_fe_get_msgfds(chr, &fd, 1);
393 * This is a non-blocking eventfd.
394 * The receive function forces it to be blocking,
395 * so revert it back to non-blocking.
397 qemu_set_nonblock(fd);
398 break;
400 case VHOST_USER_SET_LOG_BASE:
401 if (s->log_fd != -1) {
402 close(s->log_fd);
403 s->log_fd = -1;
405 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
406 msg.flags |= VHOST_USER_REPLY_MASK;
407 msg.size = 0;
408 p = (uint8_t *) &msg;
409 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
411 g_cond_signal(&s->data_cond);
412 break;
414 case VHOST_USER_SET_VRING_BASE:
415 assert(msg.payload.state.index < s->queues * 2);
416 s->rings |= 0x1ULL << msg.payload.state.index;
417 break;
419 case VHOST_USER_GET_QUEUE_NUM:
420 msg.flags |= VHOST_USER_REPLY_MASK;
421 msg.size = sizeof(m.payload.u64);
422 msg.payload.u64 = s->queues;
423 p = (uint8_t *) &msg;
424 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
425 break;
427 default:
428 break;
431 g_mutex_unlock(&s->data_mutex);
434 static const char *init_hugepagefs(const char *path)
436 struct statfs fs;
437 int ret;
439 if (access(path, R_OK | W_OK | X_OK)) {
440 g_test_message("access on path (%s): %s\n", path, strerror(errno));
441 return NULL;
444 do {
445 ret = statfs(path, &fs);
446 } while (ret != 0 && errno == EINTR);
448 if (ret != 0) {
449 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
450 return NULL;
453 if (fs.f_type != HUGETLBFS_MAGIC) {
454 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
455 return NULL;
458 return path;
461 static TestServer *test_server_new(const gchar *name)
463 TestServer *server = g_new0(TestServer, 1);
465 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
466 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
467 server->chr_name = g_strdup_printf("chr-%s", name);
469 g_mutex_init(&server->data_mutex);
470 g_cond_init(&server->data_cond);
472 server->log_fd = -1;
473 server->queues = 1;
475 return server;
478 static void chr_event(void *opaque, int event)
480 TestServer *s = opaque;
482 if (s->test_flags == TEST_FLAGS_END &&
483 event == CHR_EVENT_CLOSED) {
484 s->test_flags = TEST_FLAGS_OK;
488 static void test_server_create_chr(TestServer *server, const gchar *opt)
490 gchar *chr_path;
491 Chardev *chr;
493 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
494 chr = qemu_chr_new(server->chr_name, chr_path);
495 g_free(chr_path);
497 qemu_chr_fe_init(&server->chr, chr, &error_abort);
498 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
499 chr_event, NULL, server, NULL, true);
502 static void test_server_listen(TestServer *server)
504 test_server_create_chr(server, ",server,nowait");
507 static gboolean _test_server_free(TestServer *server)
509 int i;
511 qemu_chr_fe_deinit(&server->chr, true);
513 for (i = 0; i < server->fds_num; i++) {
514 close(server->fds[i]);
517 if (server->log_fd != -1) {
518 close(server->log_fd);
521 unlink(server->socket_path);
522 g_free(server->socket_path);
524 unlink(server->mig_path);
525 g_free(server->mig_path);
527 g_free(server->chr_name);
528 qpci_free_pc(server->bus);
530 g_free(server);
532 return FALSE;
535 static void test_server_free(TestServer *server)
537 g_idle_add((GSourceFunc)_test_server_free, server);
540 static void wait_for_log_fd(TestServer *s)
542 gint64 end_time;
544 g_mutex_lock(&s->data_mutex);
545 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
546 while (s->log_fd == -1) {
547 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
548 /* timeout has passed */
549 g_assert(s->log_fd != -1);
550 break;
554 g_mutex_unlock(&s->data_mutex);
557 static void write_guest_mem(TestServer *s, uint32_t seed)
559 uint32_t *guest_mem;
560 int i, j;
561 size_t size;
563 wait_for_fds(s);
565 /* iterate all regions */
566 for (i = 0; i < s->fds_num; i++) {
568 /* We'll write only the region statring at 0x0 */
569 if (s->memory.regions[i].guest_phys_addr != 0x0) {
570 continue;
573 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
575 size = s->memory.regions[i].memory_size +
576 s->memory.regions[i].mmap_offset;
578 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
579 MAP_SHARED, s->fds[i], 0);
581 g_assert(guest_mem != MAP_FAILED);
582 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
584 for (j = 0; j < 256; j++) {
585 guest_mem[j] = seed + j;
588 munmap(guest_mem, s->memory.regions[i].memory_size);
589 break;
593 static guint64 get_log_size(TestServer *s)
595 guint64 log_size = 0;
596 int i;
598 for (i = 0; i < s->memory.nregions; ++i) {
599 VhostUserMemoryRegion *reg = &s->memory.regions[i];
600 guint64 last = range_get_last(reg->guest_phys_addr,
601 reg->memory_size);
602 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
605 return log_size;
608 typedef struct TestMigrateSource {
609 GSource source;
610 TestServer *src;
611 TestServer *dest;
612 } TestMigrateSource;
614 static gboolean
615 test_migrate_source_check(GSource *source)
617 TestMigrateSource *t = (TestMigrateSource *)source;
618 gboolean overlap = t->src->rings && t->dest->rings;
620 g_assert(!overlap);
622 return FALSE;
625 #if !GLIB_CHECK_VERSION(2,36,0)
626 /* this callback is unnecessary with glib >2.36, the default
627 * prepare for the source does the same */
628 static gboolean
629 test_migrate_source_prepare(GSource *source, gint *timeout)
631 *timeout = -1;
632 return FALSE;
634 #endif
636 GSourceFuncs test_migrate_source_funcs = {
637 #if !GLIB_CHECK_VERSION(2,36,0)
638 .prepare = test_migrate_source_prepare,
639 #endif
640 .check = test_migrate_source_check,
643 static void test_read_guest_mem(void)
645 TestServer *server = NULL;
646 char *qemu_cmd = NULL;
647 QTestState *s = NULL;
649 server = test_server_new("test");
650 test_server_listen(server);
652 qemu_cmd = GET_QEMU_CMD(server);
654 s = qtest_start(qemu_cmd);
655 g_free(qemu_cmd);
657 init_virtio_dev(server, 1u << VIRTIO_NET_F_MAC);
659 read_guest_mem_server(server);
661 uninit_virtio_dev(server);
663 qtest_quit(s);
664 test_server_free(server);
667 static void test_migrate(void)
669 TestServer *s = test_server_new("src");
670 TestServer *dest = test_server_new("dest");
671 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
672 QTestState *global = global_qtest, *from, *to;
673 GSource *source;
674 gchar *cmd;
675 QDict *rsp;
676 guint8 *log;
677 guint64 size;
679 test_server_listen(s);
680 test_server_listen(dest);
682 cmd = GET_QEMU_CMDE(s, 2, "", "");
683 from = qtest_start(cmd);
684 g_free(cmd);
686 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
687 wait_for_fds(s);
688 size = get_log_size(s);
689 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
691 cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
692 to = qtest_init(cmd);
693 g_free(cmd);
695 source = g_source_new(&test_migrate_source_funcs,
696 sizeof(TestMigrateSource));
697 ((TestMigrateSource *)source)->src = s;
698 ((TestMigrateSource *)source)->dest = dest;
699 g_source_attach(source, NULL);
701 /* slow down migration to have time to fiddle with log */
702 /* TODO: qtest could learn to break on some places */
703 rsp = qmp("{ 'execute': 'migrate_set_speed',"
704 "'arguments': { 'value': 10 } }");
705 g_assert(qdict_haskey(rsp, "return"));
706 QDECREF(rsp);
708 cmd = g_strdup_printf("{ 'execute': 'migrate',"
709 "'arguments': { 'uri': '%s' } }",
710 uri);
711 rsp = qmp(cmd);
712 g_free(cmd);
713 g_assert(qdict_haskey(rsp, "return"));
714 QDECREF(rsp);
716 wait_for_log_fd(s);
718 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
719 g_assert(log != MAP_FAILED);
721 /* modify first page */
722 write_guest_mem(s, 0x42);
723 log[0] = 1;
724 munmap(log, size);
726 /* speed things up */
727 rsp = qmp("{ 'execute': 'migrate_set_speed',"
728 "'arguments': { 'value': 0 } }");
729 g_assert(qdict_haskey(rsp, "return"));
730 QDECREF(rsp);
732 qmp_eventwait("STOP");
734 global_qtest = to;
735 qmp_eventwait("RESUME");
737 read_guest_mem_server(dest);
739 uninit_virtio_dev(s);
741 g_source_destroy(source);
742 g_source_unref(source);
744 qtest_quit(to);
745 test_server_free(dest);
746 qtest_quit(from);
747 test_server_free(s);
748 g_free(uri);
750 global_qtest = global;
753 static void wait_for_rings_started(TestServer *s, size_t count)
755 gint64 end_time;
757 g_mutex_lock(&s->data_mutex);
758 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
759 while (ctpop64(s->rings) != count) {
760 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
761 /* timeout has passed */
762 g_assert_cmpint(ctpop64(s->rings), ==, count);
763 break;
767 g_mutex_unlock(&s->data_mutex);
770 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
771 static inline void test_server_connect(TestServer *server)
773 test_server_create_chr(server, ",reconnect=1");
776 static gboolean
777 reconnect_cb(gpointer user_data)
779 TestServer *s = user_data;
781 qemu_chr_fe_disconnect(&s->chr);
783 return FALSE;
786 static gpointer
787 connect_thread(gpointer data)
789 TestServer *s = data;
791 /* wait for qemu to start before first try, to avoid extra warnings */
792 g_usleep(G_USEC_PER_SEC);
793 test_server_connect(s);
795 return NULL;
798 static void test_reconnect_subprocess(void)
800 TestServer *s = test_server_new("reconnect");
801 char *cmd;
803 g_thread_new("connect", connect_thread, s);
804 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
805 qtest_start(cmd);
806 g_free(cmd);
808 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
809 wait_for_fds(s);
810 wait_for_rings_started(s, 2);
812 /* reconnect */
813 s->fds_num = 0;
814 s->rings = 0;
815 g_idle_add(reconnect_cb, s);
816 wait_for_fds(s);
817 wait_for_rings_started(s, 2);
819 uninit_virtio_dev(s);
821 qtest_end();
822 test_server_free(s);
823 return;
826 static void test_reconnect(void)
828 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
829 qtest_get_arch());
830 g_test_trap_subprocess(path, 0, 0);
831 g_test_trap_assert_passed();
832 g_free(path);
835 static void test_connect_fail_subprocess(void)
837 TestServer *s = test_server_new("connect-fail");
838 char *cmd;
840 s->test_fail = true;
841 g_thread_new("connect", connect_thread, s);
842 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
843 qtest_start(cmd);
844 g_free(cmd);
846 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
847 wait_for_fds(s);
848 wait_for_rings_started(s, 2);
850 uninit_virtio_dev(s);
852 qtest_end();
853 test_server_free(s);
856 static void test_connect_fail(void)
858 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
859 qtest_get_arch());
860 g_test_trap_subprocess(path, 0, 0);
861 g_test_trap_assert_passed();
862 g_free(path);
865 static void test_flags_mismatch_subprocess(void)
867 TestServer *s = test_server_new("flags-mismatch");
868 char *cmd;
870 s->test_flags = TEST_FLAGS_DISCONNECT;
871 g_thread_new("connect", connect_thread, s);
872 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
873 qtest_start(cmd);
874 g_free(cmd);
876 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
877 wait_for_fds(s);
878 wait_for_rings_started(s, 2);
880 uninit_virtio_dev(s);
882 qtest_end();
883 test_server_free(s);
886 static void test_flags_mismatch(void)
888 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
889 qtest_get_arch());
890 g_test_trap_subprocess(path, 0, 0);
891 g_test_trap_assert_passed();
892 g_free(path);
895 #endif
897 static void test_multiqueue(void)
899 TestServer *s = test_server_new("mq");
900 char *cmd;
901 uint32_t features_mask = ~(QVIRTIO_F_BAD_FEATURE |
902 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
903 (1u << VIRTIO_RING_F_EVENT_IDX));
904 s->queues = 2;
905 test_server_listen(s);
907 cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
908 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
909 512, 512, root, s->chr_name,
910 s->socket_path, "", s->chr_name,
911 s->queues, s->queues * 2 + 2);
912 qtest_start(cmd);
913 g_free(cmd);
915 init_virtio_dev(s, features_mask);
917 wait_for_rings_started(s, s->queues * 2);
919 uninit_virtio_dev(s);
921 qtest_end();
923 test_server_free(s);
926 int main(int argc, char **argv)
928 const char *hugefs;
929 int ret;
930 char template[] = "/tmp/vhost-test-XXXXXX";
931 GMainLoop *loop;
932 GThread *thread;
934 g_test_init(&argc, &argv, NULL);
936 module_call_init(MODULE_INIT_QOM);
937 qemu_add_opts(&qemu_chardev_opts);
939 tmpfs = mkdtemp(template);
940 if (!tmpfs) {
941 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
943 g_assert(tmpfs);
945 hugefs = getenv("QTEST_HUGETLBFS_PATH");
946 if (hugefs) {
947 root = init_hugepagefs(hugefs);
948 g_assert(root);
949 } else {
950 root = tmpfs;
953 loop = g_main_loop_new(NULL, FALSE);
954 /* run the main loop thread so the chardev may operate */
955 thread = g_thread_new(NULL, thread_function, loop);
957 qtest_add_func("/vhost-user/read-guest-mem", test_read_guest_mem);
958 qtest_add_func("/vhost-user/migrate", test_migrate);
959 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
961 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
962 /* keeps failing on build-system since Aug 15 2017 */
963 if (getenv("QTEST_VHOST_USER_FIXME")) {
964 qtest_add_func("/vhost-user/reconnect/subprocess",
965 test_reconnect_subprocess);
966 qtest_add_func("/vhost-user/reconnect", test_reconnect);
967 qtest_add_func("/vhost-user/connect-fail/subprocess",
968 test_connect_fail_subprocess);
969 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
970 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
971 test_flags_mismatch_subprocess);
972 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
974 #endif
976 ret = g_test_run();
978 /* cleanup */
980 /* finish the helper thread and dispatch pending sources */
981 g_main_loop_quit(loop);
982 g_thread_join(thread);
983 while (g_main_context_pending(NULL)) {
984 g_main_context_iteration (NULL, TRUE);
986 g_main_loop_unref(loop);
988 ret = rmdir(tmpfs);
989 if (ret != 0) {
990 g_test_message("unable to rmdir: path (%s): %s\n",
991 tmpfs, strerror(errno));
993 g_assert_cmpint(ret, ==, 0);
995 return ret;