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