char: cadence: correct reset value for baud rate registers
[qemu/ar7.git] / tests / vhost-user-test.c
bloba7f06291cb11b63fdf9eafb3639be382218bdc5a
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/option.h"
16 #include "qemu/range.h"
17 #include "qemu/sockets.h"
18 #include "sysemu/char.h"
19 #include "sysemu/sysemu.h"
20 #include "libqos/libqos.h"
21 #include "libqos/pci-pc.h"
22 #include "libqos/virtio-pci.h"
23 #include "qapi/error.h"
25 #include "libqos/pci-pc.h"
26 #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_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
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 gchar *socket_path;
144 gchar *mig_path;
145 gchar *chr_name;
146 CharBackend chr;
147 int fds_num;
148 int fds[VHOST_MEMORY_MAX_NREGIONS];
149 VhostUserMemory memory;
150 CompatGMutex data_mutex;
151 CompatGCond data_cond;
152 int log_fd;
153 uint64_t rings;
154 bool test_fail;
155 int test_flags;
156 int queues;
157 } TestServer;
159 static const char *tmpfs;
160 static const char *root;
162 static void init_virtio_dev(TestServer *s)
164 QPCIBus *bus;
165 QVirtioPCIDevice *dev;
166 uint32_t features;
168 bus = qpci_init_pc(NULL);
169 g_assert_nonnull(bus);
171 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
172 g_assert_nonnull(dev);
174 qvirtio_pci_device_enable(dev);
175 qvirtio_reset(&qvirtio_pci, &dev->vdev);
176 qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
177 qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
179 features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
180 features = features & VIRTIO_NET_F_MAC;
181 qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);
183 qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
186 static void wait_for_fds(TestServer *s)
188 gint64 end_time;
190 g_mutex_lock(&s->data_mutex);
192 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
193 while (!s->fds_num) {
194 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
195 /* timeout has passed */
196 g_assert(s->fds_num);
197 break;
201 /* check for sanity */
202 g_assert_cmpint(s->fds_num, >, 0);
203 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
205 g_mutex_unlock(&s->data_mutex);
208 static void read_guest_mem(const void *data)
210 TestServer *s = (void *)data;
211 uint32_t *guest_mem;
212 int i, j;
213 size_t size;
215 wait_for_fds(s);
217 g_mutex_lock(&s->data_mutex);
219 /* iterate all regions */
220 for (i = 0; i < s->fds_num; i++) {
222 /* We'll check only the region statring at 0x0*/
223 if (s->memory.regions[i].guest_phys_addr != 0x0) {
224 continue;
227 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
229 size = s->memory.regions[i].memory_size +
230 s->memory.regions[i].mmap_offset;
232 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
233 MAP_SHARED, s->fds[i], 0);
235 g_assert(guest_mem != MAP_FAILED);
236 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
238 for (j = 0; j < 256; j++) {
239 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
240 uint32_t b = guest_mem[j];
242 g_assert_cmpint(a, ==, b);
245 munmap(guest_mem, s->memory.regions[i].memory_size);
248 g_mutex_unlock(&s->data_mutex);
251 static void *thread_function(void *data)
253 GMainLoop *loop = data;
254 g_main_loop_run(loop);
255 return NULL;
258 static int chr_can_read(void *opaque)
260 return VHOST_USER_HDR_SIZE;
263 static void chr_read(void *opaque, const uint8_t *buf, int size)
265 TestServer *s = opaque;
266 CharBackend *chr = &s->chr;
267 VhostUserMsg msg;
268 uint8_t *p = (uint8_t *) &msg;
269 int fd;
271 if (s->test_fail) {
272 qemu_chr_fe_disconnect(chr);
273 /* now switch to non-failure */
274 s->test_fail = false;
277 if (size != VHOST_USER_HDR_SIZE) {
278 g_test_message("Wrong message size received %d\n", size);
279 return;
282 g_mutex_lock(&s->data_mutex);
283 memcpy(p, buf, VHOST_USER_HDR_SIZE);
285 if (msg.size) {
286 p += VHOST_USER_HDR_SIZE;
287 size = qemu_chr_fe_read_all(chr, p, msg.size);
288 if (size != msg.size) {
289 g_test_message("Wrong message size received %d != %d\n",
290 size, msg.size);
291 return;
295 switch (msg.request) {
296 case VHOST_USER_GET_FEATURES:
297 /* send back features to qemu */
298 msg.flags |= VHOST_USER_REPLY_MASK;
299 msg.size = sizeof(m.payload.u64);
300 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
301 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
302 if (s->queues > 1) {
303 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
305 if (s->test_flags >= TEST_FLAGS_BAD) {
306 msg.payload.u64 = 0;
307 s->test_flags = TEST_FLAGS_END;
309 p = (uint8_t *) &msg;
310 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
311 break;
313 case VHOST_USER_SET_FEATURES:
314 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
315 !=, 0ULL);
316 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
317 qemu_chr_fe_disconnect(chr);
318 s->test_flags = TEST_FLAGS_BAD;
320 break;
322 case VHOST_USER_GET_PROTOCOL_FEATURES:
323 /* send back features to qemu */
324 msg.flags |= VHOST_USER_REPLY_MASK;
325 msg.size = sizeof(m.payload.u64);
326 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
327 if (s->queues > 1) {
328 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
330 p = (uint8_t *) &msg;
331 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
332 break;
334 case VHOST_USER_GET_VRING_BASE:
335 /* send back vring base to qemu */
336 msg.flags |= VHOST_USER_REPLY_MASK;
337 msg.size = sizeof(m.payload.state);
338 msg.payload.state.num = 0;
339 p = (uint8_t *) &msg;
340 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
342 assert(msg.payload.state.index < s->queues * 2);
343 s->rings &= ~(0x1ULL << msg.payload.state.index);
344 break;
346 case VHOST_USER_SET_MEM_TABLE:
347 /* received the mem table */
348 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
349 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
350 G_N_ELEMENTS(s->fds));
352 /* signal the test that it can continue */
353 g_cond_signal(&s->data_cond);
354 break;
356 case VHOST_USER_SET_VRING_KICK:
357 case VHOST_USER_SET_VRING_CALL:
358 /* consume the fd */
359 qemu_chr_fe_get_msgfds(chr, &fd, 1);
361 * This is a non-blocking eventfd.
362 * The receive function forces it to be blocking,
363 * so revert it back to non-blocking.
365 qemu_set_nonblock(fd);
366 break;
368 case VHOST_USER_SET_LOG_BASE:
369 if (s->log_fd != -1) {
370 close(s->log_fd);
371 s->log_fd = -1;
373 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
374 msg.flags |= VHOST_USER_REPLY_MASK;
375 msg.size = 0;
376 p = (uint8_t *) &msg;
377 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
379 g_cond_signal(&s->data_cond);
380 break;
382 case VHOST_USER_SET_VRING_BASE:
383 assert(msg.payload.state.index < s->queues * 2);
384 s->rings |= 0x1ULL << msg.payload.state.index;
385 break;
387 case VHOST_USER_GET_QUEUE_NUM:
388 msg.flags |= VHOST_USER_REPLY_MASK;
389 msg.size = sizeof(m.payload.u64);
390 msg.payload.u64 = s->queues;
391 p = (uint8_t *) &msg;
392 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
393 break;
395 default:
396 break;
399 g_mutex_unlock(&s->data_mutex);
402 static const char *init_hugepagefs(const char *path)
404 struct statfs fs;
405 int ret;
407 if (access(path, R_OK | W_OK | X_OK)) {
408 g_test_message("access on path (%s): %s\n", path, strerror(errno));
409 return NULL;
412 do {
413 ret = statfs(path, &fs);
414 } while (ret != 0 && errno == EINTR);
416 if (ret != 0) {
417 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
418 return NULL;
421 if (fs.f_type != HUGETLBFS_MAGIC) {
422 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
423 return NULL;
426 return path;
429 static TestServer *test_server_new(const gchar *name)
431 TestServer *server = g_new0(TestServer, 1);
433 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
434 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
435 server->chr_name = g_strdup_printf("chr-%s", name);
437 g_mutex_init(&server->data_mutex);
438 g_cond_init(&server->data_cond);
440 server->log_fd = -1;
441 server->queues = 1;
443 return server;
446 static void chr_event(void *opaque, int event)
448 TestServer *s = opaque;
450 if (s->test_flags == TEST_FLAGS_END &&
451 event == CHR_EVENT_CLOSED) {
452 s->test_flags = TEST_FLAGS_OK;
456 static void test_server_create_chr(TestServer *server, const gchar *opt)
458 gchar *chr_path;
459 CharDriverState *chr;
461 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
462 chr = qemu_chr_new(server->chr_name, chr_path);
463 g_free(chr_path);
465 qemu_chr_fe_init(&server->chr, chr, &error_abort);
466 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
467 chr_event, server, NULL, true);
470 static void test_server_listen(TestServer *server)
472 test_server_create_chr(server, ",server,nowait");
475 static inline void test_server_connect(TestServer *server)
477 test_server_create_chr(server, ",reconnect=1");
480 #define GET_QEMU_CMD(s) \
481 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
482 (s)->socket_path, "", (s)->chr_name)
484 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
485 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
486 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
488 static gboolean _test_server_free(TestServer *server)
490 int i;
491 CharDriverState *chr = qemu_chr_fe_get_driver(&server->chr);
493 qemu_chr_fe_deinit(&server->chr);
494 qemu_chr_delete(chr);
496 for (i = 0; i < server->fds_num; i++) {
497 close(server->fds[i]);
500 if (server->log_fd != -1) {
501 close(server->log_fd);
504 unlink(server->socket_path);
505 g_free(server->socket_path);
507 unlink(server->mig_path);
508 g_free(server->mig_path);
510 g_free(server->chr_name);
511 g_free(server);
513 return FALSE;
516 static void test_server_free(TestServer *server)
518 g_idle_add((GSourceFunc)_test_server_free, server);
521 static void wait_for_log_fd(TestServer *s)
523 gint64 end_time;
525 g_mutex_lock(&s->data_mutex);
526 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
527 while (s->log_fd == -1) {
528 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
529 /* timeout has passed */
530 g_assert(s->log_fd != -1);
531 break;
535 g_mutex_unlock(&s->data_mutex);
538 static void write_guest_mem(TestServer *s, uint32_t seed)
540 uint32_t *guest_mem;
541 int i, j;
542 size_t size;
544 wait_for_fds(s);
546 /* iterate all regions */
547 for (i = 0; i < s->fds_num; i++) {
549 /* We'll write only the region statring at 0x0 */
550 if (s->memory.regions[i].guest_phys_addr != 0x0) {
551 continue;
554 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
556 size = s->memory.regions[i].memory_size +
557 s->memory.regions[i].mmap_offset;
559 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
560 MAP_SHARED, s->fds[i], 0);
562 g_assert(guest_mem != MAP_FAILED);
563 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
565 for (j = 0; j < 256; j++) {
566 guest_mem[j] = seed + j;
569 munmap(guest_mem, s->memory.regions[i].memory_size);
570 break;
574 static guint64 get_log_size(TestServer *s)
576 guint64 log_size = 0;
577 int i;
579 for (i = 0; i < s->memory.nregions; ++i) {
580 VhostUserMemoryRegion *reg = &s->memory.regions[i];
581 guint64 last = range_get_last(reg->guest_phys_addr,
582 reg->memory_size);
583 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
586 return log_size;
589 typedef struct TestMigrateSource {
590 GSource source;
591 TestServer *src;
592 TestServer *dest;
593 } TestMigrateSource;
595 static gboolean
596 test_migrate_source_check(GSource *source)
598 TestMigrateSource *t = (TestMigrateSource *)source;
599 gboolean overlap = t->src->rings && t->dest->rings;
601 g_assert(!overlap);
603 return FALSE;
606 #if !GLIB_CHECK_VERSION(2,36,0)
607 /* this callback is unnecessary with glib >2.36, the default
608 * prepare for the source does the same */
609 static gboolean
610 test_migrate_source_prepare(GSource *source, gint *timeout)
612 *timeout = -1;
613 return FALSE;
615 #endif
617 GSourceFuncs test_migrate_source_funcs = {
618 #if !GLIB_CHECK_VERSION(2,36,0)
619 .prepare = test_migrate_source_prepare,
620 #endif
621 .check = test_migrate_source_check,
624 static void test_migrate(void)
626 TestServer *s = test_server_new("src");
627 TestServer *dest = test_server_new("dest");
628 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
629 QTestState *global = global_qtest, *from, *to;
630 GSource *source;
631 gchar *cmd;
632 QDict *rsp;
633 guint8 *log;
634 guint64 size;
636 test_server_listen(s);
637 test_server_listen(dest);
639 cmd = GET_QEMU_CMDE(s, 2, "", "");
640 from = qtest_start(cmd);
641 g_free(cmd);
643 init_virtio_dev(s);
644 wait_for_fds(s);
645 size = get_log_size(s);
646 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
648 cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
649 to = qtest_init(cmd);
650 g_free(cmd);
652 source = g_source_new(&test_migrate_source_funcs,
653 sizeof(TestMigrateSource));
654 ((TestMigrateSource *)source)->src = s;
655 ((TestMigrateSource *)source)->dest = dest;
656 g_source_attach(source, NULL);
658 /* slow down migration to have time to fiddle with log */
659 /* TODO: qtest could learn to break on some places */
660 rsp = qmp("{ 'execute': 'migrate_set_speed',"
661 "'arguments': { 'value': 10 } }");
662 g_assert(qdict_haskey(rsp, "return"));
663 QDECREF(rsp);
665 cmd = g_strdup_printf("{ 'execute': 'migrate',"
666 "'arguments': { 'uri': '%s' } }",
667 uri);
668 rsp = qmp(cmd);
669 g_free(cmd);
670 g_assert(qdict_haskey(rsp, "return"));
671 QDECREF(rsp);
673 wait_for_log_fd(s);
675 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
676 g_assert(log != MAP_FAILED);
678 /* modify first page */
679 write_guest_mem(s, 0x42);
680 log[0] = 1;
681 munmap(log, size);
683 /* speed things up */
684 rsp = qmp("{ 'execute': 'migrate_set_speed',"
685 "'arguments': { 'value': 0 } }");
686 g_assert(qdict_haskey(rsp, "return"));
687 QDECREF(rsp);
689 qmp_eventwait("STOP");
691 global_qtest = to;
692 qmp_eventwait("RESUME");
694 read_guest_mem(dest);
696 g_source_destroy(source);
697 g_source_unref(source);
699 qtest_quit(to);
700 test_server_free(dest);
701 qtest_quit(from);
702 test_server_free(s);
703 g_free(uri);
705 global_qtest = global;
708 static void wait_for_rings_started(TestServer *s, size_t count)
710 gint64 end_time;
712 g_mutex_lock(&s->data_mutex);
713 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
714 while (ctpop64(s->rings) != count) {
715 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
716 /* timeout has passed */
717 g_assert_cmpint(ctpop64(s->rings), ==, count);
718 break;
722 g_mutex_unlock(&s->data_mutex);
725 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
726 static gboolean
727 reconnect_cb(gpointer user_data)
729 TestServer *s = user_data;
731 qemu_chr_fe_disconnect(&s->chr);
733 return FALSE;
736 static gpointer
737 connect_thread(gpointer data)
739 TestServer *s = data;
741 /* wait for qemu to start before first try, to avoid extra warnings */
742 g_usleep(G_USEC_PER_SEC);
743 test_server_connect(s);
745 return NULL;
748 static void test_reconnect_subprocess(void)
750 TestServer *s = test_server_new("reconnect");
751 char *cmd;
753 g_thread_new("connect", connect_thread, s);
754 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
755 qtest_start(cmd);
756 g_free(cmd);
758 init_virtio_dev(s);
759 wait_for_fds(s);
760 wait_for_rings_started(s, 2);
762 /* reconnect */
763 s->fds_num = 0;
764 s->rings = 0;
765 g_idle_add(reconnect_cb, s);
766 wait_for_fds(s);
767 wait_for_rings_started(s, 2);
769 qtest_end();
770 test_server_free(s);
771 return;
774 static void test_reconnect(void)
776 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
777 qtest_get_arch());
778 g_test_trap_subprocess(path, 0, 0);
779 g_test_trap_assert_passed();
780 g_free(path);
783 static void test_connect_fail_subprocess(void)
785 TestServer *s = test_server_new("connect-fail");
786 char *cmd;
788 s->test_fail = true;
789 g_thread_new("connect", connect_thread, s);
790 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
791 qtest_start(cmd);
792 g_free(cmd);
794 init_virtio_dev(s);
795 wait_for_fds(s);
796 wait_for_rings_started(s, 2);
798 qtest_end();
799 test_server_free(s);
802 static void test_connect_fail(void)
804 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
805 qtest_get_arch());
806 g_test_trap_subprocess(path, 0, 0);
807 g_test_trap_assert_passed();
808 g_free(path);
811 static void test_flags_mismatch_subprocess(void)
813 TestServer *s = test_server_new("flags-mismatch");
814 char *cmd;
816 s->test_flags = TEST_FLAGS_DISCONNECT;
817 g_thread_new("connect", connect_thread, s);
818 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
819 qtest_start(cmd);
820 g_free(cmd);
822 init_virtio_dev(s);
823 wait_for_fds(s);
824 wait_for_rings_started(s, 2);
826 qtest_end();
827 test_server_free(s);
830 static void test_flags_mismatch(void)
832 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
833 qtest_get_arch());
834 g_test_trap_subprocess(path, 0, 0);
835 g_test_trap_assert_passed();
836 g_free(path);
839 #endif
841 static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
843 QVirtioPCIDevice *dev;
845 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
846 g_assert(dev != NULL);
847 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
849 qvirtio_pci_device_enable(dev);
850 qvirtio_reset(&qvirtio_pci, &dev->vdev);
851 qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
852 qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
854 return dev;
857 static void driver_init(const QVirtioBus *bus, QVirtioDevice *dev)
859 uint32_t features;
861 features = qvirtio_get_features(bus, dev);
862 features = features & ~(QVIRTIO_F_BAD_FEATURE |
863 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
864 (1u << VIRTIO_RING_F_EVENT_IDX));
865 qvirtio_set_features(bus, dev, features);
867 qvirtio_set_driver_ok(bus, dev);
870 #define PCI_SLOT 0x04
872 static void test_multiqueue(void)
874 const int queues = 2;
875 TestServer *s = test_server_new("mq");
876 QVirtioPCIDevice *dev;
877 QPCIBus *bus;
878 QVirtQueuePCI *vq[queues * 2];
879 QGuestAllocator *alloc;
880 char *cmd;
881 int i;
883 s->queues = queues;
884 test_server_listen(s);
886 cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
887 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
888 512, 512, root, s->chr_name,
889 s->socket_path, "", s->chr_name,
890 queues, queues * 2 + 2);
891 qtest_start(cmd);
892 g_free(cmd);
894 bus = qpci_init_pc(NULL);
895 dev = virtio_net_pci_init(bus, PCI_SLOT);
897 alloc = pc_alloc_init();
898 for (i = 0; i < queues * 2; i++) {
899 vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
900 alloc, i);
903 driver_init(&qvirtio_pci, &dev->vdev);
904 wait_for_rings_started(s, queues * 2);
906 /* End test */
907 for (i = 0; i < queues * 2; i++) {
908 qvirtqueue_cleanup(&qvirtio_pci, &vq[i]->vq, alloc);
910 pc_alloc_uninit(alloc);
911 qvirtio_pci_device_disable(dev);
912 g_free(dev->pdev);
913 g_free(dev);
914 qpci_free_pc(bus);
915 qtest_end();
917 test_server_free(s);
920 int main(int argc, char **argv)
922 QTestState *s = NULL;
923 TestServer *server = NULL;
924 const char *hugefs;
925 char *qemu_cmd = NULL;
926 int ret;
927 char template[] = "/tmp/vhost-test-XXXXXX";
928 GMainLoop *loop;
929 GThread *thread;
931 g_test_init(&argc, &argv, NULL);
933 module_call_init(MODULE_INIT_QOM);
934 qemu_add_opts(&qemu_chardev_opts);
936 tmpfs = mkdtemp(template);
937 if (!tmpfs) {
938 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
940 g_assert(tmpfs);
942 hugefs = getenv("QTEST_HUGETLBFS_PATH");
943 if (hugefs) {
944 root = init_hugepagefs(hugefs);
945 g_assert(root);
946 } else {
947 root = tmpfs;
950 server = test_server_new("test");
951 test_server_listen(server);
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 qemu_cmd = GET_QEMU_CMD(server);
959 s = qtest_start(qemu_cmd);
960 g_free(qemu_cmd);
961 init_virtio_dev(server);
963 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
964 qtest_add_func("/vhost-user/migrate", test_migrate);
965 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
966 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
967 qtest_add_func("/vhost-user/reconnect/subprocess",
968 test_reconnect_subprocess);
969 qtest_add_func("/vhost-user/reconnect", test_reconnect);
970 qtest_add_func("/vhost-user/connect-fail/subprocess",
971 test_connect_fail_subprocess);
972 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
973 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
974 test_flags_mismatch_subprocess);
975 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
976 #endif
978 ret = g_test_run();
980 if (s) {
981 qtest_quit(s);
984 /* cleanup */
985 test_server_free(server);
987 /* finish the helper thread and dispatch pending sources */
988 g_main_loop_quit(loop);
989 g_thread_join(thread);
990 while (g_main_context_pending(NULL)) {
991 g_main_context_iteration (NULL, TRUE);
993 g_main_loop_unref(loop);
995 ret = rmdir(tmpfs);
996 if (ret != 0) {
997 g_test_message("unable to rmdir: path (%s): %s\n",
998 tmpfs, strerror(errno));
1000 g_assert_cmpint(ret, ==, 0);
1002 return ret;