ui: convert common input code to keycodemapdb
[qemu/ar7.git] / tests / vhost-user-test.c
blob4b980184784d3f67686b36660b1cf453b9737dae
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"
24 #include "qapi/error.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 #define VHOST_USER_NET_TESTS_WORKING 0 /* broken as of 2.10.0 */
36 /* GLIB version compatibility flags */
37 #if !GLIB_CHECK_VERSION(2, 26, 0)
38 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
39 #endif
41 #if GLIB_CHECK_VERSION(2, 28, 0)
42 #define HAVE_MONOTONIC_TIME
43 #endif
45 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
46 "mem-path=%s,share=on -numa node,memdev=mem"
47 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
48 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
49 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
51 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
52 QEMU_CMD_NETDEV QEMU_CMD_NET
54 #define HUGETLBFS_MAGIC 0x958458f6
56 /*********** FROM hw/virtio/vhost-user.c *************************************/
58 #define VHOST_MEMORY_MAX_NREGIONS 8
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 gchar *socket_path;
146 gchar *mig_path;
147 gchar *chr_name;
148 CharBackend chr;
149 int fds_num;
150 int fds[VHOST_MEMORY_MAX_NREGIONS];
151 VhostUserMemory memory;
152 CompatGMutex data_mutex;
153 CompatGCond data_cond;
154 int log_fd;
155 uint64_t rings;
156 bool test_fail;
157 int test_flags;
158 int queues;
159 } TestServer;
161 static const char *tmpfs;
162 static const char *root;
164 static void init_virtio_dev(TestServer *s)
166 QVirtioPCIDevice *dev;
167 uint32_t features;
169 s->bus = qpci_init_pc(NULL);
170 g_assert_nonnull(s->bus);
172 dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
173 g_assert_nonnull(dev);
175 qvirtio_pci_device_enable(dev);
176 qvirtio_reset(&dev->vdev);
177 qvirtio_set_acknowledge(&dev->vdev);
178 qvirtio_set_driver(&dev->vdev);
180 features = qvirtio_get_features(&dev->vdev);
181 features = features & VIRTIO_NET_F_MAC;
182 qvirtio_set_features(&dev->vdev, features);
184 qvirtio_set_driver_ok(&dev->vdev);
185 qvirtio_pci_device_free(dev);
188 static void wait_for_fds(TestServer *s)
190 gint64 end_time;
192 g_mutex_lock(&s->data_mutex);
194 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
195 while (!s->fds_num) {
196 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
197 /* timeout has passed */
198 g_assert(s->fds_num);
199 break;
203 /* check for sanity */
204 g_assert_cmpint(s->fds_num, >, 0);
205 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
207 g_mutex_unlock(&s->data_mutex);
210 static void read_guest_mem(const void *data)
212 TestServer *s = (void *)data;
213 uint32_t *guest_mem;
214 int i, j;
215 size_t size;
217 wait_for_fds(s);
219 g_mutex_lock(&s->data_mutex);
221 /* iterate all regions */
222 for (i = 0; i < s->fds_num; i++) {
224 /* We'll check only the region statring at 0x0*/
225 if (s->memory.regions[i].guest_phys_addr != 0x0) {
226 continue;
229 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
231 size = s->memory.regions[i].memory_size +
232 s->memory.regions[i].mmap_offset;
234 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
235 MAP_SHARED, s->fds[i], 0);
237 g_assert(guest_mem != MAP_FAILED);
238 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
240 for (j = 0; j < 256; j++) {
241 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
242 uint32_t b = guest_mem[j];
244 g_assert_cmpint(a, ==, b);
247 munmap(guest_mem, s->memory.regions[i].memory_size);
250 g_mutex_unlock(&s->data_mutex);
253 static void *thread_function(void *data)
255 GMainLoop *loop = data;
256 g_main_loop_run(loop);
257 return NULL;
260 static int chr_can_read(void *opaque)
262 return VHOST_USER_HDR_SIZE;
265 static void chr_read(void *opaque, const uint8_t *buf, int size)
267 TestServer *s = opaque;
268 CharBackend *chr = &s->chr;
269 VhostUserMsg msg;
270 uint8_t *p = (uint8_t *) &msg;
271 int fd;
273 if (s->test_fail) {
274 qemu_chr_fe_disconnect(chr);
275 /* now switch to non-failure */
276 s->test_fail = false;
279 if (size != VHOST_USER_HDR_SIZE) {
280 g_test_message("Wrong message size received %d\n", size);
281 return;
284 g_mutex_lock(&s->data_mutex);
285 memcpy(p, buf, VHOST_USER_HDR_SIZE);
287 if (msg.size) {
288 p += VHOST_USER_HDR_SIZE;
289 size = qemu_chr_fe_read_all(chr, p, msg.size);
290 if (size != msg.size) {
291 g_test_message("Wrong message size received %d != %d\n",
292 size, msg.size);
293 return;
297 switch (msg.request) {
298 case VHOST_USER_GET_FEATURES:
299 /* send back features to qemu */
300 msg.flags |= VHOST_USER_REPLY_MASK;
301 msg.size = sizeof(m.payload.u64);
302 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
303 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
304 if (s->queues > 1) {
305 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
307 if (s->test_flags >= TEST_FLAGS_BAD) {
308 msg.payload.u64 = 0;
309 s->test_flags = TEST_FLAGS_END;
311 p = (uint8_t *) &msg;
312 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
313 break;
315 case VHOST_USER_SET_FEATURES:
316 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
317 !=, 0ULL);
318 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
319 qemu_chr_fe_disconnect(chr);
320 s->test_flags = TEST_FLAGS_BAD;
322 break;
324 case VHOST_USER_GET_PROTOCOL_FEATURES:
325 /* send back features to qemu */
326 msg.flags |= VHOST_USER_REPLY_MASK;
327 msg.size = sizeof(m.payload.u64);
328 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
329 if (s->queues > 1) {
330 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
332 p = (uint8_t *) &msg;
333 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
334 break;
336 case VHOST_USER_GET_VRING_BASE:
337 /* send back vring base to qemu */
338 msg.flags |= VHOST_USER_REPLY_MASK;
339 msg.size = sizeof(m.payload.state);
340 msg.payload.state.num = 0;
341 p = (uint8_t *) &msg;
342 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
344 assert(msg.payload.state.index < s->queues * 2);
345 s->rings &= ~(0x1ULL << msg.payload.state.index);
346 break;
348 case VHOST_USER_SET_MEM_TABLE:
349 /* received the mem table */
350 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
351 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
352 G_N_ELEMENTS(s->fds));
354 /* signal the test that it can continue */
355 g_cond_signal(&s->data_cond);
356 break;
358 case VHOST_USER_SET_VRING_KICK:
359 case VHOST_USER_SET_VRING_CALL:
360 /* consume the fd */
361 qemu_chr_fe_get_msgfds(chr, &fd, 1);
363 * This is a non-blocking eventfd.
364 * The receive function forces it to be blocking,
365 * so revert it back to non-blocking.
367 qemu_set_nonblock(fd);
368 break;
370 case VHOST_USER_SET_LOG_BASE:
371 if (s->log_fd != -1) {
372 close(s->log_fd);
373 s->log_fd = -1;
375 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
376 msg.flags |= VHOST_USER_REPLY_MASK;
377 msg.size = 0;
378 p = (uint8_t *) &msg;
379 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
381 g_cond_signal(&s->data_cond);
382 break;
384 case VHOST_USER_SET_VRING_BASE:
385 assert(msg.payload.state.index < s->queues * 2);
386 s->rings |= 0x1ULL << msg.payload.state.index;
387 break;
389 case VHOST_USER_GET_QUEUE_NUM:
390 msg.flags |= VHOST_USER_REPLY_MASK;
391 msg.size = sizeof(m.payload.u64);
392 msg.payload.u64 = s->queues;
393 p = (uint8_t *) &msg;
394 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
395 break;
397 default:
398 break;
401 g_mutex_unlock(&s->data_mutex);
404 static const char *init_hugepagefs(const char *path)
406 struct statfs fs;
407 int ret;
409 if (access(path, R_OK | W_OK | X_OK)) {
410 g_test_message("access on path (%s): %s\n", path, strerror(errno));
411 return NULL;
414 do {
415 ret = statfs(path, &fs);
416 } while (ret != 0 && errno == EINTR);
418 if (ret != 0) {
419 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
420 return NULL;
423 if (fs.f_type != HUGETLBFS_MAGIC) {
424 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
425 return NULL;
428 return path;
431 static TestServer *test_server_new(const gchar *name)
433 TestServer *server = g_new0(TestServer, 1);
435 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
436 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
437 server->chr_name = g_strdup_printf("chr-%s", name);
439 g_mutex_init(&server->data_mutex);
440 g_cond_init(&server->data_cond);
442 server->log_fd = -1;
443 server->queues = 1;
445 return server;
448 static void chr_event(void *opaque, int event)
450 TestServer *s = opaque;
452 if (s->test_flags == TEST_FLAGS_END &&
453 event == CHR_EVENT_CLOSED) {
454 s->test_flags = TEST_FLAGS_OK;
458 static void test_server_create_chr(TestServer *server, const gchar *opt)
460 gchar *chr_path;
461 Chardev *chr;
463 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
464 chr = qemu_chr_new(server->chr_name, chr_path);
465 g_free(chr_path);
467 qemu_chr_fe_init(&server->chr, chr, &error_abort);
468 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
469 chr_event, NULL, server, NULL, true);
472 static void test_server_listen(TestServer *server)
474 test_server_create_chr(server, ",server,nowait");
477 #define GET_QEMU_CMD(s) \
478 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
479 (s)->socket_path, "", (s)->chr_name)
481 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
482 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
483 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
485 static gboolean _test_server_free(TestServer *server)
487 int i;
489 qemu_chr_fe_deinit(&server->chr, true);
491 for (i = 0; i < server->fds_num; i++) {
492 close(server->fds[i]);
495 if (server->log_fd != -1) {
496 close(server->log_fd);
499 unlink(server->socket_path);
500 g_free(server->socket_path);
502 unlink(server->mig_path);
503 g_free(server->mig_path);
505 g_free(server->chr_name);
506 qpci_free_pc(server->bus);
508 g_free(server);
510 return FALSE;
513 static void test_server_free(TestServer *server)
515 g_idle_add((GSourceFunc)_test_server_free, server);
518 static void wait_for_log_fd(TestServer *s)
520 gint64 end_time;
522 g_mutex_lock(&s->data_mutex);
523 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
524 while (s->log_fd == -1) {
525 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
526 /* timeout has passed */
527 g_assert(s->log_fd != -1);
528 break;
532 g_mutex_unlock(&s->data_mutex);
535 static void write_guest_mem(TestServer *s, uint32_t seed)
537 uint32_t *guest_mem;
538 int i, j;
539 size_t size;
541 wait_for_fds(s);
543 /* iterate all regions */
544 for (i = 0; i < s->fds_num; i++) {
546 /* We'll write only the region statring at 0x0 */
547 if (s->memory.regions[i].guest_phys_addr != 0x0) {
548 continue;
551 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
553 size = s->memory.regions[i].memory_size +
554 s->memory.regions[i].mmap_offset;
556 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
557 MAP_SHARED, s->fds[i], 0);
559 g_assert(guest_mem != MAP_FAILED);
560 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
562 for (j = 0; j < 256; j++) {
563 guest_mem[j] = seed + j;
566 munmap(guest_mem, s->memory.regions[i].memory_size);
567 break;
571 static guint64 get_log_size(TestServer *s)
573 guint64 log_size = 0;
574 int i;
576 for (i = 0; i < s->memory.nregions; ++i) {
577 VhostUserMemoryRegion *reg = &s->memory.regions[i];
578 guint64 last = range_get_last(reg->guest_phys_addr,
579 reg->memory_size);
580 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
583 return log_size;
586 typedef struct TestMigrateSource {
587 GSource source;
588 TestServer *src;
589 TestServer *dest;
590 } TestMigrateSource;
592 static gboolean
593 test_migrate_source_check(GSource *source)
595 TestMigrateSource *t = (TestMigrateSource *)source;
596 gboolean overlap = t->src->rings && t->dest->rings;
598 g_assert(!overlap);
600 return FALSE;
603 #if !GLIB_CHECK_VERSION(2,36,0)
604 /* this callback is unnecessary with glib >2.36, the default
605 * prepare for the source does the same */
606 static gboolean
607 test_migrate_source_prepare(GSource *source, gint *timeout)
609 *timeout = -1;
610 return FALSE;
612 #endif
614 GSourceFuncs test_migrate_source_funcs = {
615 #if !GLIB_CHECK_VERSION(2,36,0)
616 .prepare = test_migrate_source_prepare,
617 #endif
618 .check = test_migrate_source_check,
621 static void test_migrate(void)
623 TestServer *s = test_server_new("src");
624 TestServer *dest = test_server_new("dest");
625 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
626 QTestState *global = global_qtest, *from, *to;
627 GSource *source;
628 gchar *cmd;
629 QDict *rsp;
630 guint8 *log;
631 guint64 size;
633 test_server_listen(s);
634 test_server_listen(dest);
636 cmd = GET_QEMU_CMDE(s, 2, "", "");
637 from = qtest_start(cmd);
638 g_free(cmd);
640 init_virtio_dev(s);
641 wait_for_fds(s);
642 size = get_log_size(s);
643 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
645 cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
646 to = qtest_init(cmd);
647 g_free(cmd);
649 source = g_source_new(&test_migrate_source_funcs,
650 sizeof(TestMigrateSource));
651 ((TestMigrateSource *)source)->src = s;
652 ((TestMigrateSource *)source)->dest = dest;
653 g_source_attach(source, NULL);
655 /* slow down migration to have time to fiddle with log */
656 /* TODO: qtest could learn to break on some places */
657 rsp = qmp("{ 'execute': 'migrate_set_speed',"
658 "'arguments': { 'value': 10 } }");
659 g_assert(qdict_haskey(rsp, "return"));
660 QDECREF(rsp);
662 cmd = g_strdup_printf("{ 'execute': 'migrate',"
663 "'arguments': { 'uri': '%s' } }",
664 uri);
665 rsp = qmp(cmd);
666 g_free(cmd);
667 g_assert(qdict_haskey(rsp, "return"));
668 QDECREF(rsp);
670 wait_for_log_fd(s);
672 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
673 g_assert(log != MAP_FAILED);
675 /* modify first page */
676 write_guest_mem(s, 0x42);
677 log[0] = 1;
678 munmap(log, size);
680 /* speed things up */
681 rsp = qmp("{ 'execute': 'migrate_set_speed',"
682 "'arguments': { 'value': 0 } }");
683 g_assert(qdict_haskey(rsp, "return"));
684 QDECREF(rsp);
686 qmp_eventwait("STOP");
688 global_qtest = to;
689 qmp_eventwait("RESUME");
691 read_guest_mem(dest);
693 g_source_destroy(source);
694 g_source_unref(source);
696 qtest_quit(to);
697 test_server_free(dest);
698 qtest_quit(from);
699 test_server_free(s);
700 g_free(uri);
702 global_qtest = global;
705 static void wait_for_rings_started(TestServer *s, size_t count)
707 gint64 end_time;
709 g_mutex_lock(&s->data_mutex);
710 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
711 while (ctpop64(s->rings) != count) {
712 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
713 /* timeout has passed */
714 g_assert_cmpint(ctpop64(s->rings), ==, count);
715 break;
719 g_mutex_unlock(&s->data_mutex);
722 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
723 static inline void test_server_connect(TestServer *server)
725 test_server_create_chr(server, ",reconnect=1");
728 static gboolean
729 reconnect_cb(gpointer user_data)
731 TestServer *s = user_data;
733 qemu_chr_fe_disconnect(&s->chr);
735 return FALSE;
738 static gpointer
739 connect_thread(gpointer data)
741 TestServer *s = data;
743 /* wait for qemu to start before first try, to avoid extra warnings */
744 g_usleep(G_USEC_PER_SEC);
745 test_server_connect(s);
747 return NULL;
750 static void test_reconnect_subprocess(void)
752 TestServer *s = test_server_new("reconnect");
753 char *cmd;
755 g_thread_new("connect", connect_thread, s);
756 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
757 qtest_start(cmd);
758 g_free(cmd);
760 init_virtio_dev(s);
761 wait_for_fds(s);
762 wait_for_rings_started(s, 2);
764 /* reconnect */
765 s->fds_num = 0;
766 s->rings = 0;
767 g_idle_add(reconnect_cb, s);
768 wait_for_fds(s);
769 wait_for_rings_started(s, 2);
771 qtest_end();
772 test_server_free(s);
773 return;
776 static void test_reconnect(void)
778 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
779 qtest_get_arch());
780 g_test_trap_subprocess(path, 0, 0);
781 g_test_trap_assert_passed();
782 g_free(path);
785 static void test_connect_fail_subprocess(void)
787 TestServer *s = test_server_new("connect-fail");
788 char *cmd;
790 s->test_fail = true;
791 g_thread_new("connect", connect_thread, s);
792 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
793 qtest_start(cmd);
794 g_free(cmd);
796 init_virtio_dev(s);
797 wait_for_fds(s);
798 wait_for_rings_started(s, 2);
800 qtest_end();
801 test_server_free(s);
804 static void test_connect_fail(void)
806 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
807 qtest_get_arch());
808 g_test_trap_subprocess(path, 0, 0);
809 g_test_trap_assert_passed();
810 g_free(path);
813 static void test_flags_mismatch_subprocess(void)
815 TestServer *s = test_server_new("flags-mismatch");
816 char *cmd;
818 s->test_flags = TEST_FLAGS_DISCONNECT;
819 g_thread_new("connect", connect_thread, s);
820 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
821 qtest_start(cmd);
822 g_free(cmd);
824 init_virtio_dev(s);
825 wait_for_fds(s);
826 wait_for_rings_started(s, 2);
828 qtest_end();
829 test_server_free(s);
832 static void test_flags_mismatch(void)
834 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
835 qtest_get_arch());
836 g_test_trap_subprocess(path, 0, 0);
837 g_test_trap_assert_passed();
838 g_free(path);
841 #endif
843 static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
845 QVirtioPCIDevice *dev;
847 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
848 g_assert(dev != NULL);
849 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
851 qvirtio_pci_device_enable(dev);
852 qvirtio_reset(&dev->vdev);
853 qvirtio_set_acknowledge(&dev->vdev);
854 qvirtio_set_driver(&dev->vdev);
856 return dev;
859 static void driver_init(QVirtioDevice *dev)
861 uint32_t features;
863 features = qvirtio_get_features(dev);
864 features = features & ~(QVIRTIO_F_BAD_FEATURE |
865 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
866 (1u << VIRTIO_RING_F_EVENT_IDX));
867 qvirtio_set_features(dev, features);
869 qvirtio_set_driver_ok(dev);
872 #define PCI_SLOT 0x04
874 static void test_multiqueue(void)
876 const int queues = 2;
877 TestServer *s = test_server_new("mq");
878 QVirtioPCIDevice *dev;
879 QPCIBus *bus;
880 QVirtQueuePCI *vq[queues * 2];
881 QGuestAllocator *alloc;
882 char *cmd;
883 int i;
885 s->queues = queues;
886 test_server_listen(s);
888 cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
889 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
890 512, 512, root, s->chr_name,
891 s->socket_path, "", s->chr_name,
892 queues, queues * 2 + 2);
893 qtest_start(cmd);
894 g_free(cmd);
896 bus = qpci_init_pc(NULL);
897 dev = virtio_net_pci_init(bus, PCI_SLOT);
899 alloc = pc_alloc_init();
900 for (i = 0; i < queues * 2; i++) {
901 vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, alloc, i);
904 driver_init(&dev->vdev);
905 wait_for_rings_started(s, queues * 2);
907 /* End test */
908 for (i = 0; i < queues * 2; i++) {
909 qvirtqueue_cleanup(dev->vdev.bus, &vq[i]->vq, alloc);
911 pc_alloc_uninit(alloc);
912 qvirtio_pci_device_disable(dev);
913 g_free(dev->pdev);
914 g_free(dev);
915 qpci_free_pc(bus);
916 qtest_end();
918 test_server_free(s);
921 int main(int argc, char **argv)
923 QTestState *s = NULL;
924 TestServer *server = NULL;
925 const char *hugefs;
926 char *qemu_cmd = NULL;
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 server = test_server_new("test");
952 test_server_listen(server);
954 loop = g_main_loop_new(NULL, FALSE);
955 /* run the main loop thread so the chardev may operate */
956 thread = g_thread_new(NULL, thread_function, loop);
958 qemu_cmd = GET_QEMU_CMD(server);
960 s = qtest_start(qemu_cmd);
961 g_free(qemu_cmd);
962 init_virtio_dev(server);
964 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
965 qtest_add_func("/vhost-user/migrate", test_migrate);
966 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
968 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
969 qtest_add_func("/vhost-user/reconnect/subprocess",
970 test_reconnect_subprocess);
971 qtest_add_func("/vhost-user/reconnect", test_reconnect);
972 qtest_add_func("/vhost-user/connect-fail/subprocess",
973 test_connect_fail_subprocess);
974 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
975 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
976 test_flags_mismatch_subprocess);
977 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
978 #endif
980 ret = g_test_run();
982 if (s) {
983 qtest_quit(s);
986 /* cleanup */
987 test_server_free(server);
989 /* finish the helper thread and dispatch pending sources */
990 g_main_loop_quit(loop);
991 g_thread_join(thread);
992 while (g_main_context_pending(NULL)) {
993 g_main_context_iteration (NULL, TRUE);
995 g_main_loop_unref(loop);
997 ret = rmdir(tmpfs);
998 if (ret != 0) {
999 g_test_message("unable to rmdir: path (%s): %s\n",
1000 tmpfs, strerror(errno));
1002 g_assert_cmpint(ret, ==, 0);
1004 return ret;