iotests: Fix _img_info for backslashes
[qemu/ar7.git] / tests / vhost-user-test.c
blobec6ac9dc9edda9cf3d34c6f1f7a163035674c7a9
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 "qemu/config-file.h"
16 #include "qemu/option.h"
17 #include "qemu/range.h"
18 #include "qemu/sockets.h"
19 #include "chardev/char-fe.h"
20 #include "sysemu/sysemu.h"
21 #include "libqos/libqos.h"
22 #include "libqos/pci-pc.h"
23 #include "libqos/virtio-pci.h"
25 #include "libqos/malloc-pc.h"
26 #include "hw/virtio/virtio-net.h"
28 #include <linux/vhost.h>
29 #include <linux/virtio_ids.h>
30 #include <linux/virtio_net.h>
31 #include <sys/vfs.h>
33 #define VHOST_USER_NET_TESTS_WORKING 0 /* broken as of 2.10.0 */
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_CHR " -chardev socket,id=%s,path=%s%s"
47 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
48 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
50 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
51 QEMU_CMD_NETDEV QEMU_CMD_NET
53 #define HUGETLBFS_MAGIC 0x958458f6
55 /*********** FROM hw/virtio/vhost-user.c *************************************/
57 #define VHOST_MEMORY_MAX_NREGIONS 8
58 #define VHOST_MAX_VIRTQUEUES 0x100
60 #define VHOST_USER_F_PROTOCOL_FEATURES 30
61 #define VHOST_USER_PROTOCOL_F_MQ 0
62 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
64 #define VHOST_LOG_PAGE 0x1000
66 typedef enum VhostUserRequest {
67 VHOST_USER_NONE = 0,
68 VHOST_USER_GET_FEATURES = 1,
69 VHOST_USER_SET_FEATURES = 2,
70 VHOST_USER_SET_OWNER = 3,
71 VHOST_USER_RESET_OWNER = 4,
72 VHOST_USER_SET_MEM_TABLE = 5,
73 VHOST_USER_SET_LOG_BASE = 6,
74 VHOST_USER_SET_LOG_FD = 7,
75 VHOST_USER_SET_VRING_NUM = 8,
76 VHOST_USER_SET_VRING_ADDR = 9,
77 VHOST_USER_SET_VRING_BASE = 10,
78 VHOST_USER_GET_VRING_BASE = 11,
79 VHOST_USER_SET_VRING_KICK = 12,
80 VHOST_USER_SET_VRING_CALL = 13,
81 VHOST_USER_SET_VRING_ERR = 14,
82 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
83 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
84 VHOST_USER_GET_QUEUE_NUM = 17,
85 VHOST_USER_SET_VRING_ENABLE = 18,
86 VHOST_USER_MAX
87 } VhostUserRequest;
89 typedef struct VhostUserMemoryRegion {
90 uint64_t guest_phys_addr;
91 uint64_t memory_size;
92 uint64_t userspace_addr;
93 uint64_t mmap_offset;
94 } VhostUserMemoryRegion;
96 typedef struct VhostUserMemory {
97 uint32_t nregions;
98 uint32_t padding;
99 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
100 } VhostUserMemory;
102 typedef struct VhostUserLog {
103 uint64_t mmap_size;
104 uint64_t mmap_offset;
105 } VhostUserLog;
107 typedef struct VhostUserMsg {
108 VhostUserRequest request;
110 #define VHOST_USER_VERSION_MASK (0x3)
111 #define VHOST_USER_REPLY_MASK (0x1<<2)
112 uint32_t flags;
113 uint32_t size; /* the following payload size */
114 union {
115 #define VHOST_USER_VRING_IDX_MASK (0xff)
116 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
117 uint64_t u64;
118 struct vhost_vring_state state;
119 struct vhost_vring_addr addr;
120 VhostUserMemory memory;
121 VhostUserLog log;
122 } payload;
123 } QEMU_PACKED VhostUserMsg;
125 static VhostUserMsg m __attribute__ ((unused));
126 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
127 + sizeof(m.flags) \
128 + sizeof(m.size))
130 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
132 /* The version of the protocol we support */
133 #define VHOST_USER_VERSION (0x1)
134 /*****************************************************************************/
136 enum {
137 TEST_FLAGS_OK,
138 TEST_FLAGS_DISCONNECT,
139 TEST_FLAGS_BAD,
140 TEST_FLAGS_END,
143 typedef struct TestServer {
144 QPCIBus *bus;
145 QVirtioPCIDevice *dev;
146 QVirtQueue *vq[VHOST_MAX_VIRTQUEUES];
147 gchar *socket_path;
148 gchar *mig_path;
149 gchar *chr_name;
150 CharBackend chr;
151 int fds_num;
152 int fds[VHOST_MEMORY_MAX_NREGIONS];
153 VhostUserMemory memory;
154 CompatGMutex data_mutex;
155 CompatGCond 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 const char *tmpfs;
165 static const char *root;
167 static void init_virtio_dev(TestServer *s, uint32_t features_mask)
169 uint32_t features;
170 int i;
172 s->bus = qpci_init_pc(NULL);
173 g_assert_nonnull(s->bus);
175 s->dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
176 g_assert_nonnull(s->dev);
178 qvirtio_pci_device_enable(s->dev);
179 qvirtio_reset(&s->dev->vdev);
180 qvirtio_set_acknowledge(&s->dev->vdev);
181 qvirtio_set_driver(&s->dev->vdev);
183 s->alloc = pc_alloc_init();
185 for (i = 0; i < s->queues * 2; i++) {
186 s->vq[i] = qvirtqueue_setup(&s->dev->vdev, s->alloc, i);
189 features = qvirtio_get_features(&s->dev->vdev);
190 features = features & features_mask;
191 qvirtio_set_features(&s->dev->vdev, features);
193 qvirtio_set_driver_ok(&s->dev->vdev);
196 static void uninit_virtio_dev(TestServer *s)
198 int i;
200 for (i = 0; i < s->queues * 2; i++) {
201 qvirtqueue_cleanup(s->dev->vdev.bus, s->vq[i], s->alloc);
203 pc_alloc_uninit(s->alloc);
205 qvirtio_pci_device_free(s->dev);
208 static void wait_for_fds(TestServer *s)
210 gint64 end_time;
212 g_mutex_lock(&s->data_mutex);
214 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
215 while (!s->fds_num) {
216 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
217 /* timeout has passed */
218 g_assert(s->fds_num);
219 break;
223 /* check for sanity */
224 g_assert_cmpint(s->fds_num, >, 0);
225 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
227 g_mutex_unlock(&s->data_mutex);
230 static void read_guest_mem(const void *data)
232 TestServer *s = (void *)data;
233 uint32_t *guest_mem;
234 int i, j;
235 size_t size;
237 wait_for_fds(s);
239 g_mutex_lock(&s->data_mutex);
241 /* iterate all regions */
242 for (i = 0; i < s->fds_num; i++) {
244 /* We'll check only the region statring at 0x0*/
245 if (s->memory.regions[i].guest_phys_addr != 0x0) {
246 continue;
249 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
251 size = s->memory.regions[i].memory_size +
252 s->memory.regions[i].mmap_offset;
254 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
255 MAP_SHARED, s->fds[i], 0);
257 g_assert(guest_mem != MAP_FAILED);
258 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
260 for (j = 0; j < 256; j++) {
261 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
262 uint32_t b = guest_mem[j];
264 g_assert_cmpint(a, ==, b);
267 munmap(guest_mem, s->memory.regions[i].memory_size);
270 g_mutex_unlock(&s->data_mutex);
273 static void *thread_function(void *data)
275 GMainLoop *loop = data;
276 g_main_loop_run(loop);
277 return NULL;
280 static int chr_can_read(void *opaque)
282 return VHOST_USER_HDR_SIZE;
285 static void chr_read(void *opaque, const uint8_t *buf, int size)
287 TestServer *s = opaque;
288 CharBackend *chr = &s->chr;
289 VhostUserMsg msg;
290 uint8_t *p = (uint8_t *) &msg;
291 int fd;
293 if (s->test_fail) {
294 qemu_chr_fe_disconnect(chr);
295 /* now switch to non-failure */
296 s->test_fail = false;
299 if (size != VHOST_USER_HDR_SIZE) {
300 g_test_message("Wrong message size received %d\n", size);
301 return;
304 g_mutex_lock(&s->data_mutex);
305 memcpy(p, buf, VHOST_USER_HDR_SIZE);
307 if (msg.size) {
308 p += VHOST_USER_HDR_SIZE;
309 size = qemu_chr_fe_read_all(chr, p, msg.size);
310 if (size != msg.size) {
311 g_test_message("Wrong message size received %d != %d\n",
312 size, msg.size);
313 return;
317 switch (msg.request) {
318 case VHOST_USER_GET_FEATURES:
319 /* send back features to qemu */
320 msg.flags |= VHOST_USER_REPLY_MASK;
321 msg.size = sizeof(m.payload.u64);
322 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
323 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
324 if (s->queues > 1) {
325 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
327 if (s->test_flags >= TEST_FLAGS_BAD) {
328 msg.payload.u64 = 0;
329 s->test_flags = TEST_FLAGS_END;
331 p = (uint8_t *) &msg;
332 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
333 break;
335 case VHOST_USER_SET_FEATURES:
336 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
337 !=, 0ULL);
338 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
339 qemu_chr_fe_disconnect(chr);
340 s->test_flags = TEST_FLAGS_BAD;
342 break;
344 case VHOST_USER_GET_PROTOCOL_FEATURES:
345 /* send back features to qemu */
346 msg.flags |= VHOST_USER_REPLY_MASK;
347 msg.size = sizeof(m.payload.u64);
348 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
349 if (s->queues > 1) {
350 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
352 p = (uint8_t *) &msg;
353 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
354 break;
356 case VHOST_USER_GET_VRING_BASE:
357 /* send back vring base to qemu */
358 msg.flags |= VHOST_USER_REPLY_MASK;
359 msg.size = sizeof(m.payload.state);
360 msg.payload.state.num = 0;
361 p = (uint8_t *) &msg;
362 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
364 assert(msg.payload.state.index < s->queues * 2);
365 s->rings &= ~(0x1ULL << msg.payload.state.index);
366 break;
368 case VHOST_USER_SET_MEM_TABLE:
369 /* received the mem table */
370 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
371 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
372 G_N_ELEMENTS(s->fds));
374 /* signal the test that it can continue */
375 g_cond_signal(&s->data_cond);
376 break;
378 case VHOST_USER_SET_VRING_KICK:
379 case VHOST_USER_SET_VRING_CALL:
380 /* consume the fd */
381 qemu_chr_fe_get_msgfds(chr, &fd, 1);
383 * This is a non-blocking eventfd.
384 * The receive function forces it to be blocking,
385 * so revert it back to non-blocking.
387 qemu_set_nonblock(fd);
388 break;
390 case VHOST_USER_SET_LOG_BASE:
391 if (s->log_fd != -1) {
392 close(s->log_fd);
393 s->log_fd = -1;
395 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
396 msg.flags |= VHOST_USER_REPLY_MASK;
397 msg.size = 0;
398 p = (uint8_t *) &msg;
399 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
401 g_cond_signal(&s->data_cond);
402 break;
404 case VHOST_USER_SET_VRING_BASE:
405 assert(msg.payload.state.index < s->queues * 2);
406 s->rings |= 0x1ULL << msg.payload.state.index;
407 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(const char *path)
426 struct statfs fs;
427 int ret;
429 if (access(path, R_OK | W_OK | X_OK)) {
430 g_test_message("access on path (%s): %s\n", path, strerror(errno));
431 return NULL;
434 do {
435 ret = statfs(path, &fs);
436 } while (ret != 0 && errno == EINTR);
438 if (ret != 0) {
439 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
440 return NULL;
443 if (fs.f_type != HUGETLBFS_MAGIC) {
444 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
445 return NULL;
448 return path;
451 static TestServer *test_server_new(const gchar *name)
453 TestServer *server = g_new0(TestServer, 1);
455 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
456 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
457 server->chr_name = g_strdup_printf("chr-%s", name);
459 g_mutex_init(&server->data_mutex);
460 g_cond_init(&server->data_cond);
462 server->log_fd = -1;
463 server->queues = 1;
465 return server;
468 static void chr_event(void *opaque, int event)
470 TestServer *s = opaque;
472 if (s->test_flags == TEST_FLAGS_END &&
473 event == CHR_EVENT_CLOSED) {
474 s->test_flags = TEST_FLAGS_OK;
478 static void test_server_create_chr(TestServer *server, const gchar *opt)
480 gchar *chr_path;
481 Chardev *chr;
483 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
484 chr = qemu_chr_new(server->chr_name, chr_path);
485 g_free(chr_path);
487 qemu_chr_fe_init(&server->chr, chr, &error_abort);
488 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
489 chr_event, NULL, server, NULL, true);
492 static void test_server_listen(TestServer *server)
494 test_server_create_chr(server, ",server,nowait");
497 #define GET_QEMU_CMD(s) \
498 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
499 (s)->socket_path, "", (s)->chr_name)
501 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
502 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
503 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
505 static gboolean _test_server_free(TestServer *server)
507 int i;
509 qemu_chr_fe_deinit(&server->chr, true);
511 for (i = 0; i < server->fds_num; i++) {
512 close(server->fds[i]);
515 if (server->log_fd != -1) {
516 close(server->log_fd);
519 unlink(server->socket_path);
520 g_free(server->socket_path);
522 unlink(server->mig_path);
523 g_free(server->mig_path);
525 g_free(server->chr_name);
526 qpci_free_pc(server->bus);
528 g_free(server);
530 return FALSE;
533 static void test_server_free(TestServer *server)
535 g_idle_add((GSourceFunc)_test_server_free, server);
538 static void wait_for_log_fd(TestServer *s)
540 gint64 end_time;
542 g_mutex_lock(&s->data_mutex);
543 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
544 while (s->log_fd == -1) {
545 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
546 /* timeout has passed */
547 g_assert(s->log_fd != -1);
548 break;
552 g_mutex_unlock(&s->data_mutex);
555 static void write_guest_mem(TestServer *s, uint32_t seed)
557 uint32_t *guest_mem;
558 int i, j;
559 size_t size;
561 wait_for_fds(s);
563 /* iterate all regions */
564 for (i = 0; i < s->fds_num; i++) {
566 /* We'll write only the region statring at 0x0 */
567 if (s->memory.regions[i].guest_phys_addr != 0x0) {
568 continue;
571 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
573 size = s->memory.regions[i].memory_size +
574 s->memory.regions[i].mmap_offset;
576 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
577 MAP_SHARED, s->fds[i], 0);
579 g_assert(guest_mem != MAP_FAILED);
580 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
582 for (j = 0; j < 256; j++) {
583 guest_mem[j] = seed + j;
586 munmap(guest_mem, s->memory.regions[i].memory_size);
587 break;
591 static guint64 get_log_size(TestServer *s)
593 guint64 log_size = 0;
594 int i;
596 for (i = 0; i < s->memory.nregions; ++i) {
597 VhostUserMemoryRegion *reg = &s->memory.regions[i];
598 guint64 last = range_get_last(reg->guest_phys_addr,
599 reg->memory_size);
600 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
603 return log_size;
606 typedef struct TestMigrateSource {
607 GSource source;
608 TestServer *src;
609 TestServer *dest;
610 } TestMigrateSource;
612 static gboolean
613 test_migrate_source_check(GSource *source)
615 TestMigrateSource *t = (TestMigrateSource *)source;
616 gboolean overlap = t->src->rings && t->dest->rings;
618 g_assert(!overlap);
620 return FALSE;
623 #if !GLIB_CHECK_VERSION(2,36,0)
624 /* this callback is unnecessary with glib >2.36, the default
625 * prepare for the source does the same */
626 static gboolean
627 test_migrate_source_prepare(GSource *source, gint *timeout)
629 *timeout = -1;
630 return FALSE;
632 #endif
634 GSourceFuncs test_migrate_source_funcs = {
635 #if !GLIB_CHECK_VERSION(2,36,0)
636 .prepare = test_migrate_source_prepare,
637 #endif
638 .check = test_migrate_source_check,
641 static void test_read_guest_mem(void)
643 TestServer *server = NULL;
644 char *qemu_cmd = NULL;
645 QTestState *s = NULL;
647 server = test_server_new("test");
648 test_server_listen(server);
650 qemu_cmd = GET_QEMU_CMD(server);
652 s = qtest_start(qemu_cmd);
653 g_free(qemu_cmd);
655 init_virtio_dev(server, 1u << VIRTIO_NET_F_MAC);
657 read_guest_mem(server);
659 uninit_virtio_dev(server);
661 qtest_quit(s);
662 test_server_free(server);
665 static void test_migrate(void)
667 TestServer *s = test_server_new("src");
668 TestServer *dest = test_server_new("dest");
669 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
670 QTestState *global = global_qtest, *from, *to;
671 GSource *source;
672 gchar *cmd;
673 QDict *rsp;
674 guint8 *log;
675 guint64 size;
677 test_server_listen(s);
678 test_server_listen(dest);
680 cmd = GET_QEMU_CMDE(s, 2, "", "");
681 from = qtest_start(cmd);
682 g_free(cmd);
684 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
685 wait_for_fds(s);
686 size = get_log_size(s);
687 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
689 cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
690 to = qtest_init(cmd);
691 g_free(cmd);
693 source = g_source_new(&test_migrate_source_funcs,
694 sizeof(TestMigrateSource));
695 ((TestMigrateSource *)source)->src = s;
696 ((TestMigrateSource *)source)->dest = dest;
697 g_source_attach(source, NULL);
699 /* slow down migration to have time to fiddle with log */
700 /* TODO: qtest could learn to break on some places */
701 rsp = qmp("{ 'execute': 'migrate_set_speed',"
702 "'arguments': { 'value': 10 } }");
703 g_assert(qdict_haskey(rsp, "return"));
704 QDECREF(rsp);
706 cmd = g_strdup_printf("{ 'execute': 'migrate',"
707 "'arguments': { 'uri': '%s' } }",
708 uri);
709 rsp = qmp(cmd);
710 g_free(cmd);
711 g_assert(qdict_haskey(rsp, "return"));
712 QDECREF(rsp);
714 wait_for_log_fd(s);
716 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
717 g_assert(log != MAP_FAILED);
719 /* modify first page */
720 write_guest_mem(s, 0x42);
721 log[0] = 1;
722 munmap(log, size);
724 /* speed things up */
725 rsp = qmp("{ 'execute': 'migrate_set_speed',"
726 "'arguments': { 'value': 0 } }");
727 g_assert(qdict_haskey(rsp, "return"));
728 QDECREF(rsp);
730 qmp_eventwait("STOP");
732 global_qtest = to;
733 qmp_eventwait("RESUME");
735 read_guest_mem(dest);
737 uninit_virtio_dev(s);
739 g_source_destroy(source);
740 g_source_unref(source);
742 qtest_quit(to);
743 test_server_free(dest);
744 qtest_quit(from);
745 test_server_free(s);
746 g_free(uri);
748 global_qtest = global;
751 static void wait_for_rings_started(TestServer *s, size_t count)
753 gint64 end_time;
755 g_mutex_lock(&s->data_mutex);
756 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
757 while (ctpop64(s->rings) != count) {
758 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
759 /* timeout has passed */
760 g_assert_cmpint(ctpop64(s->rings), ==, count);
761 break;
765 g_mutex_unlock(&s->data_mutex);
768 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
769 static inline void test_server_connect(TestServer *server)
771 test_server_create_chr(server, ",reconnect=1");
774 static gboolean
775 reconnect_cb(gpointer user_data)
777 TestServer *s = user_data;
779 qemu_chr_fe_disconnect(&s->chr);
781 return FALSE;
784 static gpointer
785 connect_thread(gpointer data)
787 TestServer *s = data;
789 /* wait for qemu to start before first try, to avoid extra warnings */
790 g_usleep(G_USEC_PER_SEC);
791 test_server_connect(s);
793 return NULL;
796 static void test_reconnect_subprocess(void)
798 TestServer *s = test_server_new("reconnect");
799 char *cmd;
801 g_thread_new("connect", connect_thread, s);
802 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
803 qtest_start(cmd);
804 g_free(cmd);
806 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
807 wait_for_fds(s);
808 wait_for_rings_started(s, 2);
810 /* reconnect */
811 s->fds_num = 0;
812 s->rings = 0;
813 g_idle_add(reconnect_cb, s);
814 wait_for_fds(s);
815 wait_for_rings_started(s, 2);
817 uninit_virtio_dev(s);
819 qtest_end();
820 test_server_free(s);
821 return;
824 static void test_reconnect(void)
826 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
827 qtest_get_arch());
828 g_test_trap_subprocess(path, 0, 0);
829 g_test_trap_assert_passed();
830 g_free(path);
833 static void test_connect_fail_subprocess(void)
835 TestServer *s = test_server_new("connect-fail");
836 char *cmd;
838 s->test_fail = true;
839 g_thread_new("connect", connect_thread, s);
840 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
841 qtest_start(cmd);
842 g_free(cmd);
844 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
845 wait_for_fds(s);
846 wait_for_rings_started(s, 2);
848 uninit_virtio_dev(s);
850 qtest_end();
851 test_server_free(s);
854 static void test_connect_fail(void)
856 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
857 qtest_get_arch());
858 g_test_trap_subprocess(path, 0, 0);
859 g_test_trap_assert_passed();
860 g_free(path);
863 static void test_flags_mismatch_subprocess(void)
865 TestServer *s = test_server_new("flags-mismatch");
866 char *cmd;
868 s->test_flags = TEST_FLAGS_DISCONNECT;
869 g_thread_new("connect", connect_thread, s);
870 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
871 qtest_start(cmd);
872 g_free(cmd);
874 init_virtio_dev(s, 1u << VIRTIO_NET_F_MAC);
875 wait_for_fds(s);
876 wait_for_rings_started(s, 2);
878 uninit_virtio_dev(s);
880 qtest_end();
881 test_server_free(s);
884 static void test_flags_mismatch(void)
886 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
887 qtest_get_arch());
888 g_test_trap_subprocess(path, 0, 0);
889 g_test_trap_assert_passed();
890 g_free(path);
893 #endif
895 static void test_multiqueue(void)
897 TestServer *s = test_server_new("mq");
898 char *cmd;
899 uint32_t features_mask = ~(QVIRTIO_F_BAD_FEATURE |
900 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
901 (1u << VIRTIO_RING_F_EVENT_IDX));
902 s->queues = 2;
903 test_server_listen(s);
905 cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
906 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
907 512, 512, root, s->chr_name,
908 s->socket_path, "", s->chr_name,
909 s->queues, s->queues * 2 + 2);
910 qtest_start(cmd);
911 g_free(cmd);
913 init_virtio_dev(s, features_mask);
915 wait_for_rings_started(s, s->queues * 2);
917 uninit_virtio_dev(s);
919 qtest_end();
921 test_server_free(s);
924 int main(int argc, char **argv)
926 const char *hugefs;
927 int ret;
928 char template[] = "/tmp/vhost-test-XXXXXX";
929 GMainLoop *loop;
930 GThread *thread;
932 g_test_init(&argc, &argv, NULL);
934 module_call_init(MODULE_INIT_QOM);
935 qemu_add_opts(&qemu_chardev_opts);
937 tmpfs = mkdtemp(template);
938 if (!tmpfs) {
939 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
941 g_assert(tmpfs);
943 hugefs = getenv("QTEST_HUGETLBFS_PATH");
944 if (hugefs) {
945 root = init_hugepagefs(hugefs);
946 g_assert(root);
947 } else {
948 root = tmpfs;
951 loop = g_main_loop_new(NULL, FALSE);
952 /* run the main loop thread so the chardev may operate */
953 thread = g_thread_new(NULL, thread_function, loop);
955 qtest_add_func("/vhost-user/read-guest-mem", test_read_guest_mem);
956 qtest_add_func("/vhost-user/migrate", test_migrate);
957 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
959 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
960 qtest_add_func("/vhost-user/reconnect/subprocess",
961 test_reconnect_subprocess);
962 qtest_add_func("/vhost-user/reconnect", test_reconnect);
963 qtest_add_func("/vhost-user/connect-fail/subprocess",
964 test_connect_fail_subprocess);
965 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
966 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
967 test_flags_mismatch_subprocess);
968 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
969 #endif
971 ret = g_test_run();
973 /* cleanup */
975 /* finish the helper thread and dispatch pending sources */
976 g_main_loop_quit(loop);
977 g_thread_join(thread);
978 while (g_main_context_pending(NULL)) {
979 g_main_context_iteration (NULL, TRUE);
981 g_main_loop_unref(loop);
983 ret = rmdir(tmpfs);
984 if (ret != 0) {
985 g_test_message("unable to rmdir: path (%s): %s\n",
986 tmpfs, strerror(errno));
988 g_assert_cmpint(ret, ==, 0);
990 return ret;