]> git.proxmox.com Git - mirror_qemu.git/blob - tests/vhost-user-test.c
vhost-user-test: create a main loop per TestServer
[mirror_qemu.git] / tests / vhost-user-test.c
1 /*
2 * QTest testcase for the vhost-user
3 *
4 * Copyright (c) 2014 Virtual Open Systems Sarl.
5 *
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.
8 *
9 */
10
11 #include "qemu/osdep.h"
12
13 #include "libqtest.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qemu/config-file.h"
17 #include "qemu/option.h"
18 #include "qemu/range.h"
19 #include "qemu/sockets.h"
20 #include "chardev/char-fe.h"
21 #include "qemu/memfd.h"
22 #include "sysemu/sysemu.h"
23 #include "libqos/libqos.h"
24 #include "libqos/pci-pc.h"
25 #include "libqos/virtio-pci.h"
26
27 #include "libqos/malloc-pc.h"
28 #include "hw/virtio/virtio-net.h"
29
30 #include "standard-headers/linux/vhost_types.h"
31 #include "standard-headers/linux/virtio_ids.h"
32 #include "standard-headers/linux/virtio_net.h"
33
34 #ifdef CONFIG_LINUX
35 #include <sys/vfs.h>
36 #endif
37
38
39 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
40 "mem-path=%s,share=on -numa node,memdev=mem"
41 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
42 " -numa node,memdev=mem"
43 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
44 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
45 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
46
47 #define HUGETLBFS_MAGIC 0x958458f6
48
49 /*********** FROM hw/virtio/vhost-user.c *************************************/
50
51 #define VHOST_MEMORY_MAX_NREGIONS 8
52 #define VHOST_MAX_VIRTQUEUES 0x100
53
54 #define VHOST_USER_F_PROTOCOL_FEATURES 30
55 #define VHOST_USER_PROTOCOL_F_MQ 0
56 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
57 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
58
59 #define VHOST_LOG_PAGE 0x1000
60
61 typedef enum VhostUserRequest {
62 VHOST_USER_NONE = 0,
63 VHOST_USER_GET_FEATURES = 1,
64 VHOST_USER_SET_FEATURES = 2,
65 VHOST_USER_SET_OWNER = 3,
66 VHOST_USER_RESET_OWNER = 4,
67 VHOST_USER_SET_MEM_TABLE = 5,
68 VHOST_USER_SET_LOG_BASE = 6,
69 VHOST_USER_SET_LOG_FD = 7,
70 VHOST_USER_SET_VRING_NUM = 8,
71 VHOST_USER_SET_VRING_ADDR = 9,
72 VHOST_USER_SET_VRING_BASE = 10,
73 VHOST_USER_GET_VRING_BASE = 11,
74 VHOST_USER_SET_VRING_KICK = 12,
75 VHOST_USER_SET_VRING_CALL = 13,
76 VHOST_USER_SET_VRING_ERR = 14,
77 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
78 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
79 VHOST_USER_GET_QUEUE_NUM = 17,
80 VHOST_USER_SET_VRING_ENABLE = 18,
81 VHOST_USER_MAX
82 } VhostUserRequest;
83
84 typedef struct VhostUserMemoryRegion {
85 uint64_t guest_phys_addr;
86 uint64_t memory_size;
87 uint64_t userspace_addr;
88 uint64_t mmap_offset;
89 } VhostUserMemoryRegion;
90
91 typedef struct VhostUserMemory {
92 uint32_t nregions;
93 uint32_t padding;
94 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
95 } VhostUserMemory;
96
97 typedef struct VhostUserLog {
98 uint64_t mmap_size;
99 uint64_t mmap_offset;
100 } VhostUserLog;
101
102 typedef struct VhostUserMsg {
103 VhostUserRequest request;
104
105 #define VHOST_USER_VERSION_MASK (0x3)
106 #define VHOST_USER_REPLY_MASK (0x1<<2)
107 uint32_t flags;
108 uint32_t size; /* the following payload size */
109 union {
110 #define VHOST_USER_VRING_IDX_MASK (0xff)
111 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
112 uint64_t u64;
113 struct vhost_vring_state state;
114 struct vhost_vring_addr addr;
115 VhostUserMemory memory;
116 VhostUserLog log;
117 } payload;
118 } QEMU_PACKED VhostUserMsg;
119
120 static VhostUserMsg m __attribute__ ((unused));
121 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
122 + sizeof(m.flags) \
123 + sizeof(m.size))
124
125 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
126
127 /* The version of the protocol we support */
128 #define VHOST_USER_VERSION (0x1)
129 /*****************************************************************************/
130
131 enum {
132 TEST_FLAGS_OK,
133 TEST_FLAGS_DISCONNECT,
134 TEST_FLAGS_BAD,
135 TEST_FLAGS_END,
136 };
137
138 typedef struct TestServer {
139 QPCIBus *bus;
140 QVirtioPCIDevice *dev;
141 QVirtQueue *vq[VHOST_MAX_VIRTQUEUES];
142 gchar *socket_path;
143 gchar *mig_path;
144 gchar *chr_name;
145 CharBackend chr;
146 int fds_num;
147 int fds[VHOST_MEMORY_MAX_NREGIONS];
148 VhostUserMemory memory;
149 GMainContext *context;
150 GMainLoop *loop;
151 GThread *thread;
152 GMutex data_mutex;
153 GCond data_cond;
154 int log_fd;
155 uint64_t rings;
156 bool test_fail;
157 int test_flags;
158 int queues;
159 QGuestAllocator *alloc;
160 } TestServer;
161
162 static TestServer *test_server_new(const gchar *name);
163 static void test_server_free(TestServer *server);
164 static void test_server_listen(TestServer *server);
165
166 static const char *tmpfs;
167 static const char *root;
168
169 enum test_memfd {
170 TEST_MEMFD_AUTO,
171 TEST_MEMFD_YES,
172 TEST_MEMFD_NO,
173 };
174
175 static char *get_qemu_cmd(TestServer *s,
176 int mem, enum test_memfd memfd, const char *mem_path,
177 const char *chr_opts, const char *extra)
178 {
179 if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check(0)) {
180 memfd = TEST_MEMFD_YES;
181 }
182
183 if (memfd == TEST_MEMFD_YES) {
184 return g_strdup_printf(QEMU_CMD_MEMFD QEMU_CMD_CHR
185 QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem,
186 s->chr_name, s->socket_path,
187 chr_opts, s->chr_name, extra);
188 } else {
189 return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR
190 QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem,
191 mem_path, s->chr_name, s->socket_path,
192 chr_opts, s->chr_name, extra);
193 }
194 }
195
196 static void init_virtio_dev(QTestState *qts, TestServer *s, uint32_t features_mask)
197 {
198 uint32_t features;
199 int i;
200
201 s->bus = qpci_init_pc(qts, NULL);
202 g_assert_nonnull(s->bus);
203
204 s->dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
205 g_assert_nonnull(s->dev);
206
207 qvirtio_pci_device_enable(s->dev);
208 qvirtio_reset(&s->dev->vdev);
209 qvirtio_set_acknowledge(&s->dev->vdev);
210 qvirtio_set_driver(&s->dev->vdev);
211
212 s->alloc = pc_alloc_init(qts);
213
214 for (i = 0; i < s->queues * 2; i++) {
215 s->vq[i] = qvirtqueue_setup(&s->dev->vdev, s->alloc, i);
216 }
217
218 features = qvirtio_get_features(&s->dev->vdev);
219 features = features & features_mask;
220 qvirtio_set_features(&s->dev->vdev, features);
221
222 qvirtio_set_driver_ok(&s->dev->vdev);
223 }
224
225 static void uninit_virtio_dev(TestServer *s)
226 {
227 int i;
228
229 for (i = 0; i < s->queues * 2; i++) {
230 qvirtqueue_cleanup(s->dev->vdev.bus, s->vq[i], s->alloc);
231 }
232 pc_alloc_uninit(s->alloc);
233
234 qvirtio_pci_device_free(s->dev);
235 }
236
237 static bool wait_for_fds(TestServer *s)
238 {
239 gint64 end_time;
240 bool got_region;
241 int i;
242
243 g_mutex_lock(&s->data_mutex);
244
245 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
246 while (!s->fds_num) {
247 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
248 /* timeout has passed */
249 g_assert(s->fds_num);
250 break;
251 }
252 }
253
254 /* check for sanity */
255 g_assert_cmpint(s->fds_num, >, 0);
256 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
257
258 g_mutex_unlock(&s->data_mutex);
259
260 got_region = false;
261 for (i = 0; i < s->memory.nregions; ++i) {
262 VhostUserMemoryRegion *reg = &s->memory.regions[i];
263 if (reg->guest_phys_addr == 0) {
264 got_region = true;
265 break;
266 }
267 }
268 if (!got_region) {
269 g_test_skip("No memory at address 0x0");
270 }
271 return got_region;
272 }
273
274 static void read_guest_mem_server(QTestState *qts, TestServer *s)
275 {
276 uint8_t *guest_mem;
277 int i, j;
278 size_t size;
279
280 g_mutex_lock(&s->data_mutex);
281
282 /* iterate all regions */
283 for (i = 0; i < s->fds_num; i++) {
284
285 /* We'll check only the region statring at 0x0*/
286 if (s->memory.regions[i].guest_phys_addr != 0x0) {
287 continue;
288 }
289
290 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
291
292 size = s->memory.regions[i].memory_size +
293 s->memory.regions[i].mmap_offset;
294
295 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
296 MAP_SHARED, s->fds[i], 0);
297
298 g_assert(guest_mem != MAP_FAILED);
299 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
300
301 for (j = 0; j < 1024; j++) {
302 uint32_t a = qtest_readb(qts, s->memory.regions[i].guest_phys_addr + j);
303 uint32_t b = guest_mem[j];
304
305 g_assert_cmpint(a, ==, b);
306 }
307
308 munmap(guest_mem, s->memory.regions[i].memory_size);
309 }
310
311 g_mutex_unlock(&s->data_mutex);
312 }
313
314 static void *thread_function(void *data)
315 {
316 GMainLoop *loop = data;
317 g_main_loop_run(loop);
318 return NULL;
319 }
320
321 static int chr_can_read(void *opaque)
322 {
323 return VHOST_USER_HDR_SIZE;
324 }
325
326 static void chr_read(void *opaque, const uint8_t *buf, int size)
327 {
328 TestServer *s = opaque;
329 CharBackend *chr = &s->chr;
330 VhostUserMsg msg;
331 uint8_t *p = (uint8_t *) &msg;
332 int fd = -1;
333
334 if (s->test_fail) {
335 qemu_chr_fe_disconnect(chr);
336 /* now switch to non-failure */
337 s->test_fail = false;
338 }
339
340 if (size != VHOST_USER_HDR_SIZE) {
341 g_test_message("Wrong message size received %d\n", size);
342 return;
343 }
344
345 g_mutex_lock(&s->data_mutex);
346 memcpy(p, buf, VHOST_USER_HDR_SIZE);
347
348 if (msg.size) {
349 p += VHOST_USER_HDR_SIZE;
350 size = qemu_chr_fe_read_all(chr, p, msg.size);
351 if (size != msg.size) {
352 g_test_message("Wrong message size received %d != %d\n",
353 size, msg.size);
354 return;
355 }
356 }
357
358 switch (msg.request) {
359 case VHOST_USER_GET_FEATURES:
360 /* send back features to qemu */
361 msg.flags |= VHOST_USER_REPLY_MASK;
362 msg.size = sizeof(m.payload.u64);
363 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
364 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
365 if (s->queues > 1) {
366 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
367 }
368 if (s->test_flags >= TEST_FLAGS_BAD) {
369 msg.payload.u64 = 0;
370 s->test_flags = TEST_FLAGS_END;
371 }
372 p = (uint8_t *) &msg;
373 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
374 break;
375
376 case VHOST_USER_SET_FEATURES:
377 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
378 !=, 0ULL);
379 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
380 qemu_chr_fe_disconnect(chr);
381 s->test_flags = TEST_FLAGS_BAD;
382 }
383 break;
384
385 case VHOST_USER_GET_PROTOCOL_FEATURES:
386 /* send back features to qemu */
387 msg.flags |= VHOST_USER_REPLY_MASK;
388 msg.size = sizeof(m.payload.u64);
389 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
390 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN;
391 if (s->queues > 1) {
392 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
393 }
394 p = (uint8_t *) &msg;
395 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
396 break;
397
398 case VHOST_USER_GET_VRING_BASE:
399 /* send back vring base to qemu */
400 msg.flags |= VHOST_USER_REPLY_MASK;
401 msg.size = sizeof(m.payload.state);
402 msg.payload.state.num = 0;
403 p = (uint8_t *) &msg;
404 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
405
406 assert(msg.payload.state.index < s->queues * 2);
407 s->rings &= ~(0x1ULL << msg.payload.state.index);
408 g_cond_broadcast(&s->data_cond);
409 break;
410
411 case VHOST_USER_SET_MEM_TABLE:
412 /* received the mem table */
413 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
414 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
415 G_N_ELEMENTS(s->fds));
416
417 /* signal the test that it can continue */
418 g_cond_broadcast(&s->data_cond);
419 break;
420
421 case VHOST_USER_SET_VRING_KICK:
422 case VHOST_USER_SET_VRING_CALL:
423 /* consume the fd */
424 qemu_chr_fe_get_msgfds(chr, &fd, 1);
425 /*
426 * This is a non-blocking eventfd.
427 * The receive function forces it to be blocking,
428 * so revert it back to non-blocking.
429 */
430 qemu_set_nonblock(fd);
431 break;
432
433 case VHOST_USER_SET_LOG_BASE:
434 if (s->log_fd != -1) {
435 close(s->log_fd);
436 s->log_fd = -1;
437 }
438 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
439 msg.flags |= VHOST_USER_REPLY_MASK;
440 msg.size = 0;
441 p = (uint8_t *) &msg;
442 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
443
444 g_cond_broadcast(&s->data_cond);
445 break;
446
447 case VHOST_USER_SET_VRING_BASE:
448 assert(msg.payload.state.index < s->queues * 2);
449 s->rings |= 0x1ULL << msg.payload.state.index;
450 g_cond_broadcast(&s->data_cond);
451 break;
452
453 case VHOST_USER_GET_QUEUE_NUM:
454 msg.flags |= VHOST_USER_REPLY_MASK;
455 msg.size = sizeof(m.payload.u64);
456 msg.payload.u64 = s->queues;
457 p = (uint8_t *) &msg;
458 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
459 break;
460
461 default:
462 break;
463 }
464
465 g_mutex_unlock(&s->data_mutex);
466 }
467
468 #ifdef CONFIG_LINUX
469 static const char *init_hugepagefs(const char *path)
470 {
471 struct statfs fs;
472 int ret;
473
474 if (access(path, R_OK | W_OK | X_OK)) {
475 g_test_message("access on path (%s): %s\n", path, strerror(errno));
476 return NULL;
477 }
478
479 do {
480 ret = statfs(path, &fs);
481 } while (ret != 0 && errno == EINTR);
482
483 if (ret != 0) {
484 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
485 return NULL;
486 }
487
488 if (fs.f_type != HUGETLBFS_MAGIC) {
489 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
490 return NULL;
491 }
492
493 return path;
494 }
495 #endif
496
497 static TestServer *test_server_new(const gchar *name)
498 {
499 TestServer *server = g_new0(TestServer, 1);
500
501 server->context = g_main_context_new();
502 server->loop = g_main_loop_new(server->context, FALSE);
503
504 /* run the main loop thread so the chardev may operate */
505 server->thread = g_thread_new(NULL, thread_function, server->loop);
506
507 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
508 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
509 server->chr_name = g_strdup_printf("chr-%s", name);
510
511 g_mutex_init(&server->data_mutex);
512 g_cond_init(&server->data_cond);
513
514 server->log_fd = -1;
515 server->queues = 1;
516
517 return server;
518 }
519
520 static void chr_event(void *opaque, int event)
521 {
522 TestServer *s = opaque;
523
524 if (s->test_flags == TEST_FLAGS_END &&
525 event == CHR_EVENT_CLOSED) {
526 s->test_flags = TEST_FLAGS_OK;
527 }
528 }
529
530 static void test_server_create_chr(TestServer *server, const gchar *opt)
531 {
532 gchar *chr_path;
533 Chardev *chr;
534
535 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
536 chr = qemu_chr_new(server->chr_name, chr_path, server->context);
537 g_free(chr_path);
538
539 g_assert_nonnull(chr);
540 qemu_chr_fe_init(&server->chr, chr, &error_abort);
541 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
542 chr_event, NULL, server, server->context, true);
543 }
544
545 static void test_server_listen(TestServer *server)
546 {
547 test_server_create_chr(server, ",server,nowait");
548 }
549
550 static void test_server_free(TestServer *server)
551 {
552 int i;
553
554 /* finish the helper thread and dispatch pending sources */
555 g_main_loop_quit(server->loop);
556 g_thread_join(server->thread);
557 while (g_main_context_pending(NULL)) {
558 g_main_context_iteration(NULL, TRUE);
559 }
560
561 qemu_chr_fe_deinit(&server->chr, true);
562
563 for (i = 0; i < server->fds_num; i++) {
564 close(server->fds[i]);
565 }
566
567 if (server->log_fd != -1) {
568 close(server->log_fd);
569 }
570
571 unlink(server->socket_path);
572 g_free(server->socket_path);
573
574 unlink(server->mig_path);
575 g_free(server->mig_path);
576
577 g_free(server->chr_name);
578 g_assert(server->bus);
579 qpci_free_pc(server->bus);
580
581 g_main_loop_unref(server->loop);
582 g_main_context_unref(server->context);
583 g_free(server);
584 }
585
586 static void wait_for_log_fd(TestServer *s)
587 {
588 gint64 end_time;
589
590 g_mutex_lock(&s->data_mutex);
591 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
592 while (s->log_fd == -1) {
593 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
594 /* timeout has passed */
595 g_assert(s->log_fd != -1);
596 break;
597 }
598 }
599
600 g_mutex_unlock(&s->data_mutex);
601 }
602
603 static void write_guest_mem(TestServer *s, uint32_t seed)
604 {
605 uint32_t *guest_mem;
606 int i, j;
607 size_t size;
608
609 /* iterate all regions */
610 for (i = 0; i < s->fds_num; i++) {
611
612 /* We'll write only the region statring at 0x0 */
613 if (s->memory.regions[i].guest_phys_addr != 0x0) {
614 continue;
615 }
616
617 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
618
619 size = s->memory.regions[i].memory_size +
620 s->memory.regions[i].mmap_offset;
621
622 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
623 MAP_SHARED, s->fds[i], 0);
624
625 g_assert(guest_mem != MAP_FAILED);
626 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
627
628 for (j = 0; j < 256; j++) {
629 guest_mem[j] = seed + j;
630 }
631
632 munmap(guest_mem, s->memory.regions[i].memory_size);
633 break;
634 }
635 }
636
637 static guint64 get_log_size(TestServer *s)
638 {
639 guint64 log_size = 0;
640 int i;
641
642 for (i = 0; i < s->memory.nregions; ++i) {
643 VhostUserMemoryRegion *reg = &s->memory.regions[i];
644 guint64 last = range_get_last(reg->guest_phys_addr,
645 reg->memory_size);
646 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
647 }
648
649 return log_size;
650 }
651
652 typedef struct TestMigrateSource {
653 GSource source;
654 TestServer *src;
655 TestServer *dest;
656 } TestMigrateSource;
657
658 static gboolean
659 test_migrate_source_check(GSource *source)
660 {
661 TestMigrateSource *t = (TestMigrateSource *)source;
662 gboolean overlap = t->src->rings && t->dest->rings;
663
664 g_assert(!overlap);
665
666 return FALSE;
667 }
668
669 GSourceFuncs test_migrate_source_funcs = {
670 .check = test_migrate_source_check,
671 };
672
673 static void test_read_guest_mem(const void *arg)
674 {
675 enum test_memfd memfd = GPOINTER_TO_INT(arg);
676 TestServer *server = NULL;
677 char *qemu_cmd = NULL;
678 QTestState *s = NULL;
679
680 server = test_server_new(memfd == TEST_MEMFD_YES ?
681 "read-guest-memfd" : "read-guest-mem");
682 test_server_listen(server);
683
684 qemu_cmd = get_qemu_cmd(server, 512, memfd, root, "", "");
685
686 s = qtest_start(qemu_cmd);
687 g_free(qemu_cmd);
688
689 init_virtio_dev(global_qtest, server, 1u << VIRTIO_NET_F_MAC);
690
691 if (!wait_for_fds(server)) {
692 goto exit;
693 }
694
695 read_guest_mem_server(global_qtest, server);
696
697 exit:
698 uninit_virtio_dev(server);
699
700 qtest_quit(s);
701 test_server_free(server);
702 }
703
704 static void test_migrate(void)
705 {
706 TestServer *s = test_server_new("src");
707 TestServer *dest = test_server_new("dest");
708 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
709 QTestState *from, *to;
710 GSource *source;
711 gchar *cmd, *tmp;
712 QDict *rsp;
713 guint8 *log;
714 guint64 size;
715
716 test_server_listen(s);
717 test_server_listen(dest);
718
719 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, "", "");
720 from = qtest_start(cmd);
721 g_free(cmd);
722
723 init_virtio_dev(from, s, 1u << VIRTIO_NET_F_MAC);
724 if (!wait_for_fds(s)) {
725 goto exit;
726 }
727
728 size = get_log_size(s);
729 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
730
731 tmp = g_strdup_printf(" -incoming %s", uri);
732 cmd = get_qemu_cmd(dest, 2, TEST_MEMFD_AUTO, root, "", tmp);
733 g_free(tmp);
734 to = qtest_init(cmd);
735 g_free(cmd);
736 init_virtio_dev(to, dest, 1u << VIRTIO_NET_F_MAC);
737
738 source = g_source_new(&test_migrate_source_funcs,
739 sizeof(TestMigrateSource));
740 ((TestMigrateSource *)source)->src = s;
741 ((TestMigrateSource *)source)->dest = dest;
742 g_source_attach(source, s->context);
743
744 /* slow down migration to have time to fiddle with log */
745 /* TODO: qtest could learn to break on some places */
746 rsp = qmp("{ 'execute': 'migrate_set_speed',"
747 "'arguments': { 'value': 10 } }");
748 g_assert(qdict_haskey(rsp, "return"));
749 qobject_unref(rsp);
750
751 rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
752 g_assert(qdict_haskey(rsp, "return"));
753 qobject_unref(rsp);
754
755 wait_for_log_fd(s);
756
757 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
758 g_assert(log != MAP_FAILED);
759
760 /* modify first page */
761 write_guest_mem(s, 0x42);
762 log[0] = 1;
763 munmap(log, size);
764
765 /* speed things up */
766 rsp = qmp("{ 'execute': 'migrate_set_speed',"
767 "'arguments': { 'value': 0 } }");
768 g_assert(qdict_haskey(rsp, "return"));
769 qobject_unref(rsp);
770
771 qmp_eventwait("STOP");
772 qtest_qmp_eventwait(to, "RESUME");
773
774 g_assert(wait_for_fds(dest));
775 read_guest_mem_server(to, dest);
776
777 uninit_virtio_dev(dest);
778 qtest_quit(to);
779
780 g_source_destroy(source);
781 g_source_unref(source);
782
783 exit:
784 uninit_virtio_dev(s);
785
786 test_server_free(dest);
787 qtest_quit(from);
788 test_server_free(s);
789 g_free(uri);
790 }
791
792 static void wait_for_rings_started(TestServer *s, size_t count)
793 {
794 gint64 end_time;
795
796 g_mutex_lock(&s->data_mutex);
797 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
798 while (ctpop64(s->rings) != count) {
799 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
800 /* timeout has passed */
801 g_assert_cmpint(ctpop64(s->rings), ==, count);
802 break;
803 }
804 }
805
806 g_mutex_unlock(&s->data_mutex);
807 }
808
809 static inline void test_server_connect(TestServer *server)
810 {
811 test_server_create_chr(server, ",reconnect=1");
812 }
813
814 static gboolean
815 reconnect_cb(gpointer user_data)
816 {
817 TestServer *s = user_data;
818
819 qemu_chr_fe_disconnect(&s->chr);
820
821 return FALSE;
822 }
823
824 static gpointer
825 connect_thread(gpointer data)
826 {
827 TestServer *s = data;
828
829 /* wait for qemu to start before first try, to avoid extra warnings */
830 g_usleep(G_USEC_PER_SEC);
831 test_server_connect(s);
832
833 return NULL;
834 }
835
836 static void test_reconnect_subprocess(void)
837 {
838 TestServer *s = test_server_new("reconnect");
839 GSource *src;
840 char *cmd;
841
842 g_thread_new("connect", connect_thread, s);
843 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
844 qtest_start(cmd);
845 g_free(cmd);
846
847 init_virtio_dev(global_qtest, s, 1u << VIRTIO_NET_F_MAC);
848 if (!wait_for_fds(s)) {
849 goto exit;
850 }
851
852 wait_for_rings_started(s, 2);
853
854 /* reconnect */
855 s->fds_num = 0;
856 s->rings = 0;
857 src = g_idle_source_new();
858 g_source_set_callback(src, reconnect_cb, s, NULL);
859 g_source_attach(src, s->context);
860 g_source_unref(src);
861 g_assert(wait_for_fds(s));
862 wait_for_rings_started(s, 2);
863
864 exit:
865 uninit_virtio_dev(s);
866
867 qtest_end();
868 test_server_free(s);
869 return;
870 }
871
872 static void test_reconnect(void)
873 {
874 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
875 qtest_get_arch());
876 g_test_trap_subprocess(path, 0, 0);
877 g_test_trap_assert_passed();
878 g_free(path);
879 }
880
881 static void test_connect_fail_subprocess(void)
882 {
883 TestServer *s = test_server_new("connect-fail");
884 char *cmd;
885
886 s->test_fail = true;
887 g_thread_new("connect", connect_thread, s);
888 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
889 qtest_start(cmd);
890 g_free(cmd);
891
892 init_virtio_dev(global_qtest, s, 1u << VIRTIO_NET_F_MAC);
893 if (!wait_for_fds(s)) {
894 goto exit;
895 }
896 wait_for_rings_started(s, 2);
897
898 exit:
899 uninit_virtio_dev(s);
900
901 qtest_end();
902 test_server_free(s);
903 }
904
905 static void test_connect_fail(void)
906 {
907 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
908 qtest_get_arch());
909 g_test_trap_subprocess(path, 0, 0);
910 g_test_trap_assert_passed();
911 g_free(path);
912 }
913
914 static void test_flags_mismatch_subprocess(void)
915 {
916 TestServer *s = test_server_new("flags-mismatch");
917 char *cmd;
918
919 s->test_flags = TEST_FLAGS_DISCONNECT;
920 g_thread_new("connect", connect_thread, s);
921 cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
922 qtest_start(cmd);
923 g_free(cmd);
924
925 init_virtio_dev(global_qtest, s, 1u << VIRTIO_NET_F_MAC);
926 if (!wait_for_fds(s)) {
927 goto exit;
928 }
929 wait_for_rings_started(s, 2);
930
931 exit:
932 uninit_virtio_dev(s);
933
934 qtest_end();
935 test_server_free(s);
936 }
937
938 static void test_flags_mismatch(void)
939 {
940 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
941 qtest_get_arch());
942 g_test_trap_subprocess(path, 0, 0);
943 g_test_trap_assert_passed();
944 g_free(path);
945 }
946
947
948 static void test_multiqueue(void)
949 {
950 TestServer *s = test_server_new("mq");
951 char *cmd;
952 uint32_t features_mask = ~(QVIRTIO_F_BAD_FEATURE |
953 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
954 (1u << VIRTIO_RING_F_EVENT_IDX));
955 s->queues = 2;
956 test_server_listen(s);
957
958 if (qemu_memfd_check(0)) {
959 cmd = g_strdup_printf(
960 QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
961 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
962 512, 512, s->chr_name,
963 s->socket_path, "", s->chr_name,
964 s->queues, s->queues * 2 + 2);
965 } else {
966 cmd = g_strdup_printf(
967 QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
968 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
969 512, 512, root, s->chr_name,
970 s->socket_path, "", s->chr_name,
971 s->queues, s->queues * 2 + 2);
972 }
973 qtest_start(cmd);
974 g_free(cmd);
975
976 init_virtio_dev(global_qtest, s, features_mask);
977
978 wait_for_rings_started(s, s->queues * 2);
979
980 uninit_virtio_dev(s);
981
982 qtest_end();
983
984 test_server_free(s);
985 }
986
987 int main(int argc, char **argv)
988 {
989 const char *hugefs;
990 int ret;
991 char template[] = "/tmp/vhost-test-XXXXXX";
992
993 g_test_init(&argc, &argv, NULL);
994
995 module_call_init(MODULE_INIT_QOM);
996 qemu_add_opts(&qemu_chardev_opts);
997
998 tmpfs = mkdtemp(template);
999 if (!tmpfs) {
1000 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
1001 }
1002 g_assert(tmpfs);
1003
1004 root = tmpfs;
1005 #ifdef CONFIG_LINUX
1006 hugefs = getenv("QTEST_HUGETLBFS_PATH");
1007 if (hugefs) {
1008 root = init_hugepagefs(hugefs);
1009 g_assert(root);
1010 }
1011 #endif
1012
1013 if (qemu_memfd_check(0)) {
1014 qtest_add_data_func("/vhost-user/read-guest-mem/memfd",
1015 GINT_TO_POINTER(TEST_MEMFD_YES),
1016 test_read_guest_mem);
1017 }
1018 qtest_add_data_func("/vhost-user/read-guest-mem/memfile",
1019 GINT_TO_POINTER(TEST_MEMFD_NO), test_read_guest_mem);
1020 qtest_add_func("/vhost-user/migrate", test_migrate);
1021 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
1022
1023 /* keeps failing on build-system since Aug 15 2017 */
1024 if (getenv("QTEST_VHOST_USER_FIXME")) {
1025 qtest_add_func("/vhost-user/reconnect/subprocess",
1026 test_reconnect_subprocess);
1027 qtest_add_func("/vhost-user/reconnect", test_reconnect);
1028 qtest_add_func("/vhost-user/connect-fail/subprocess",
1029 test_connect_fail_subprocess);
1030 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
1031 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
1032 test_flags_mismatch_subprocess);
1033 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
1034 }
1035
1036 ret = g_test_run();
1037
1038 /* cleanup */
1039
1040 ret = rmdir(tmpfs);
1041 if (ret != 0) {
1042 g_test_message("unable to rmdir: path (%s): %s\n",
1043 tmpfs, strerror(errno));
1044 }
1045 g_assert_cmpint(ret, ==, 0);
1046
1047 return ret;
1048 }