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