]> Git Repo - qemu.git/blob - tests/vhost-user-test.c
tests: Remove unnecessary glib.h includes
[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 "qemu/option.h"
15 #include "qemu/range.h"
16 #include "sysemu/char.h"
17 #include "sysemu/sysemu.h"
18
19 #include <linux/vhost.h>
20 #include <sys/mman.h>
21 #include <sys/vfs.h>
22 #include <qemu/sockets.h>
23
24 /* GLIB version compatibility flags */
25 #if !GLIB_CHECK_VERSION(2, 26, 0)
26 #define G_TIME_SPAN_SECOND              (G_GINT64_CONSTANT(1000000))
27 #endif
28
29 #if GLIB_CHECK_VERSION(2, 28, 0)
30 #define HAVE_MONOTONIC_TIME
31 #endif
32
33 #define QEMU_CMD_ACCEL  " -machine accel=tcg"
34 #define QEMU_CMD_MEM    " -m %d -object memory-backend-file,id=mem,size=%dM,"\
35                         "mem-path=%s,share=on -numa node,memdev=mem"
36 #define QEMU_CMD_CHR    " -chardev socket,id=%s,path=%s"
37 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
38 #define QEMU_CMD_NET    " -device virtio-net-pci,netdev=net0,romfile=./pc-bios/pxe-virtio.rom"
39
40 #define QEMU_CMD        QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \
41                         QEMU_CMD_NETDEV QEMU_CMD_NET
42
43 #define HUGETLBFS_MAGIC       0x958458f6
44
45 /*********** FROM hw/virtio/vhost-user.c *************************************/
46
47 #define VHOST_MEMORY_MAX_NREGIONS    8
48
49 #define VHOST_USER_F_PROTOCOL_FEATURES 30
50 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
51
52 #define VHOST_LOG_PAGE 0x1000
53
54 typedef enum VhostUserRequest {
55     VHOST_USER_NONE = 0,
56     VHOST_USER_GET_FEATURES = 1,
57     VHOST_USER_SET_FEATURES = 2,
58     VHOST_USER_SET_OWNER = 3,
59     VHOST_USER_RESET_OWNER = 4,
60     VHOST_USER_SET_MEM_TABLE = 5,
61     VHOST_USER_SET_LOG_BASE = 6,
62     VHOST_USER_SET_LOG_FD = 7,
63     VHOST_USER_SET_VRING_NUM = 8,
64     VHOST_USER_SET_VRING_ADDR = 9,
65     VHOST_USER_SET_VRING_BASE = 10,
66     VHOST_USER_GET_VRING_BASE = 11,
67     VHOST_USER_SET_VRING_KICK = 12,
68     VHOST_USER_SET_VRING_CALL = 13,
69     VHOST_USER_SET_VRING_ERR = 14,
70     VHOST_USER_GET_PROTOCOL_FEATURES = 15,
71     VHOST_USER_SET_PROTOCOL_FEATURES = 16,
72     VHOST_USER_SET_VRING_ENABLE = 18,
73     VHOST_USER_MAX
74 } VhostUserRequest;
75
76 typedef struct VhostUserMemoryRegion {
77     uint64_t guest_phys_addr;
78     uint64_t memory_size;
79     uint64_t userspace_addr;
80     uint64_t mmap_offset;
81 } VhostUserMemoryRegion;
82
83 typedef struct VhostUserMemory {
84     uint32_t nregions;
85     uint32_t padding;
86     VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
87 } VhostUserMemory;
88
89 typedef struct VhostUserLog {
90     uint64_t mmap_size;
91     uint64_t mmap_offset;
92 } VhostUserLog;
93
94 typedef struct VhostUserMsg {
95     VhostUserRequest request;
96
97 #define VHOST_USER_VERSION_MASK     (0x3)
98 #define VHOST_USER_REPLY_MASK       (0x1<<2)
99     uint32_t flags;
100     uint32_t size; /* the following payload size */
101     union {
102 #define VHOST_USER_VRING_IDX_MASK   (0xff)
103 #define VHOST_USER_VRING_NOFD_MASK  (0x1<<8)
104         uint64_t u64;
105         struct vhost_vring_state state;
106         struct vhost_vring_addr addr;
107         VhostUserMemory memory;
108         VhostUserLog log;
109     } payload;
110 } QEMU_PACKED VhostUserMsg;
111
112 static VhostUserMsg m __attribute__ ((unused));
113 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
114                             + sizeof(m.flags) \
115                             + sizeof(m.size))
116
117 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
118
119 /* The version of the protocol we support */
120 #define VHOST_USER_VERSION    (0x1)
121 /*****************************************************************************/
122
123 typedef struct TestServer {
124     gchar *socket_path;
125     gchar *mig_path;
126     gchar *chr_name;
127     CharDriverState *chr;
128     int fds_num;
129     int fds[VHOST_MEMORY_MAX_NREGIONS];
130     VhostUserMemory memory;
131     GMutex data_mutex;
132     GCond data_cond;
133     int log_fd;
134     uint64_t rings;
135 } TestServer;
136
137 #if !GLIB_CHECK_VERSION(2, 32, 0)
138 static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex,
139                                   gint64 end_time)
140 {
141     gboolean ret = FALSE;
142     end_time -= g_get_monotonic_time();
143     GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
144                       end_time % G_TIME_SPAN_SECOND };
145     ret = g_cond_timed_wait(cond, mutex, &time);
146     return ret;
147 }
148 #endif
149
150 static const char *tmpfs;
151 static const char *root;
152
153 static void wait_for_fds(TestServer *s)
154 {
155     gint64 end_time;
156
157     g_mutex_lock(&s->data_mutex);
158
159     end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
160     while (!s->fds_num) {
161         if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
162             /* timeout has passed */
163             g_assert(s->fds_num);
164             break;
165         }
166     }
167
168     /* check for sanity */
169     g_assert_cmpint(s->fds_num, >, 0);
170     g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
171
172     g_mutex_unlock(&s->data_mutex);
173 }
174
175 static void read_guest_mem(const void *data)
176 {
177     TestServer *s = (void *)data;
178     uint32_t *guest_mem;
179     int i, j;
180     size_t size;
181
182     wait_for_fds(s);
183
184     g_mutex_lock(&s->data_mutex);
185
186     /* iterate all regions */
187     for (i = 0; i < s->fds_num; i++) {
188
189         /* We'll check only the region statring at 0x0*/
190         if (s->memory.regions[i].guest_phys_addr != 0x0) {
191             continue;
192         }
193
194         g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
195
196         size = s->memory.regions[i].memory_size +
197             s->memory.regions[i].mmap_offset;
198
199         guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
200                          MAP_SHARED, s->fds[i], 0);
201
202         g_assert(guest_mem != MAP_FAILED);
203         guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
204
205         for (j = 0; j < 256; j++) {
206             uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
207             uint32_t b = guest_mem[j];
208
209             g_assert_cmpint(a, ==, b);
210         }
211
212         munmap(guest_mem, s->memory.regions[i].memory_size);
213     }
214
215     g_mutex_unlock(&s->data_mutex);
216 }
217
218 static void *thread_function(void *data)
219 {
220     GMainLoop *loop = data;
221     g_main_loop_run(loop);
222     return NULL;
223 }
224
225 static int chr_can_read(void *opaque)
226 {
227     return VHOST_USER_HDR_SIZE;
228 }
229
230 static void chr_read(void *opaque, const uint8_t *buf, int size)
231 {
232     TestServer *s = opaque;
233     CharDriverState *chr = s->chr;
234     VhostUserMsg msg;
235     uint8_t *p = (uint8_t *) &msg;
236     int fd;
237
238     if (size != VHOST_USER_HDR_SIZE) {
239         g_test_message("Wrong message size received %d\n", size);
240         return;
241     }
242
243     g_mutex_lock(&s->data_mutex);
244     memcpy(p, buf, VHOST_USER_HDR_SIZE);
245
246     if (msg.size) {
247         p += VHOST_USER_HDR_SIZE;
248         g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), ==, msg.size);
249     }
250
251     switch (msg.request) {
252     case VHOST_USER_GET_FEATURES:
253         /* send back features to qemu */
254         msg.flags |= VHOST_USER_REPLY_MASK;
255         msg.size = sizeof(m.payload.u64);
256         msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
257             0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
258         p = (uint8_t *) &msg;
259         qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
260         break;
261
262     case VHOST_USER_SET_FEATURES:
263         g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
264                         !=, 0ULL);
265         break;
266
267     case VHOST_USER_GET_PROTOCOL_FEATURES:
268         /* send back features to qemu */
269         msg.flags |= VHOST_USER_REPLY_MASK;
270         msg.size = sizeof(m.payload.u64);
271         msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
272         p = (uint8_t *) &msg;
273         qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
274         break;
275
276     case VHOST_USER_GET_VRING_BASE:
277         /* send back vring base to qemu */
278         msg.flags |= VHOST_USER_REPLY_MASK;
279         msg.size = sizeof(m.payload.state);
280         msg.payload.state.num = 0;
281         p = (uint8_t *) &msg;
282         qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
283
284         assert(msg.payload.state.index < 2);
285         s->rings &= ~(0x1ULL << msg.payload.state.index);
286         break;
287
288     case VHOST_USER_SET_MEM_TABLE:
289         /* received the mem table */
290         memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
291         s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(s->fds));
292
293         /* signal the test that it can continue */
294         g_cond_signal(&s->data_cond);
295         break;
296
297     case VHOST_USER_SET_VRING_KICK:
298     case VHOST_USER_SET_VRING_CALL:
299         /* consume the fd */
300         qemu_chr_fe_get_msgfds(chr, &fd, 1);
301         /*
302          * This is a non-blocking eventfd.
303          * The receive function forces it to be blocking,
304          * so revert it back to non-blocking.
305          */
306         qemu_set_nonblock(fd);
307         break;
308
309     case VHOST_USER_SET_LOG_BASE:
310         if (s->log_fd != -1) {
311             close(s->log_fd);
312             s->log_fd = -1;
313         }
314         qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
315         msg.flags |= VHOST_USER_REPLY_MASK;
316         msg.size = 0;
317         p = (uint8_t *) &msg;
318         qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
319
320         g_cond_signal(&s->data_cond);
321         break;
322
323     case VHOST_USER_SET_VRING_BASE:
324         assert(msg.payload.state.index < 2);
325         s->rings |= 0x1ULL << msg.payload.state.index;
326         break;
327
328     default:
329         break;
330     }
331
332     g_mutex_unlock(&s->data_mutex);
333 }
334
335 static const char *init_hugepagefs(const char *path)
336 {
337     struct statfs fs;
338     int ret;
339
340     if (access(path, R_OK | W_OK | X_OK)) {
341         g_test_message("access on path (%s): %s\n", path, strerror(errno));
342         return NULL;
343     }
344
345     do {
346         ret = statfs(path, &fs);
347     } while (ret != 0 && errno == EINTR);
348
349     if (ret != 0) {
350         g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
351         return NULL;
352     }
353
354     if (fs.f_type != HUGETLBFS_MAGIC) {
355         g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
356         return NULL;
357     }
358
359     return path;
360 }
361
362 static TestServer *test_server_new(const gchar *name)
363 {
364     TestServer *server = g_new0(TestServer, 1);
365     gchar *chr_path;
366
367     server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
368     server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
369
370     chr_path = g_strdup_printf("unix:%s,server,nowait", server->socket_path);
371     server->chr_name = g_strdup_printf("chr-%s", name);
372     server->chr = qemu_chr_new(server->chr_name, chr_path, NULL);
373     g_free(chr_path);
374
375     qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, server);
376
377     g_mutex_init(&server->data_mutex);
378     g_cond_init(&server->data_cond);
379
380     server->log_fd = -1;
381
382     return server;
383 }
384
385 #define GET_QEMU_CMD(s)                                                        \
386     g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name,                 \
387                     (s)->socket_path, (s)->chr_name)
388
389 #define GET_QEMU_CMDE(s, mem, extra, ...)                                      \
390     g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name,       \
391                     (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
392
393 static gboolean _test_server_free(TestServer *server)
394 {
395     int i;
396
397     qemu_chr_delete(server->chr);
398
399     for (i = 0; i < server->fds_num; i++) {
400         close(server->fds[i]);
401     }
402
403     if (server->log_fd != -1) {
404         close(server->log_fd);
405     }
406
407     unlink(server->socket_path);
408     g_free(server->socket_path);
409
410     unlink(server->mig_path);
411     g_free(server->mig_path);
412
413     g_free(server->chr_name);
414     g_free(server);
415
416     return FALSE;
417 }
418
419 static void test_server_free(TestServer *server)
420 {
421     g_idle_add((GSourceFunc)_test_server_free, server);
422 }
423
424 static void wait_for_log_fd(TestServer *s)
425 {
426     gint64 end_time;
427
428     g_mutex_lock(&s->data_mutex);
429     end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
430     while (s->log_fd == -1) {
431         if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
432             /* timeout has passed */
433             g_assert(s->log_fd != -1);
434             break;
435         }
436     }
437
438     g_mutex_unlock(&s->data_mutex);
439 }
440
441 static void write_guest_mem(TestServer *s, uint32_t seed)
442 {
443     uint32_t *guest_mem;
444     int i, j;
445     size_t size;
446
447     wait_for_fds(s);
448
449     /* iterate all regions */
450     for (i = 0; i < s->fds_num; i++) {
451
452         /* We'll write only the region statring at 0x0 */
453         if (s->memory.regions[i].guest_phys_addr != 0x0) {
454             continue;
455         }
456
457         g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
458
459         size = s->memory.regions[i].memory_size +
460             s->memory.regions[i].mmap_offset;
461
462         guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
463                          MAP_SHARED, s->fds[i], 0);
464
465         g_assert(guest_mem != MAP_FAILED);
466         guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
467
468         for (j = 0; j < 256; j++) {
469             guest_mem[j] = seed + j;
470         }
471
472         munmap(guest_mem, s->memory.regions[i].memory_size);
473         break;
474     }
475 }
476
477 static guint64 get_log_size(TestServer *s)
478 {
479     guint64 log_size = 0;
480     int i;
481
482     for (i = 0; i < s->memory.nregions; ++i) {
483         VhostUserMemoryRegion *reg = &s->memory.regions[i];
484         guint64 last = range_get_last(reg->guest_phys_addr,
485                                        reg->memory_size);
486         log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
487     }
488
489     return log_size;
490 }
491
492 typedef struct TestMigrateSource {
493     GSource source;
494     TestServer *src;
495     TestServer *dest;
496 } TestMigrateSource;
497
498 static gboolean
499 test_migrate_source_check(GSource *source)
500 {
501     TestMigrateSource *t = (TestMigrateSource *)source;
502     gboolean overlap = t->src->rings && t->dest->rings;
503
504     g_assert(!overlap);
505
506     return FALSE;
507 }
508
509 #if !GLIB_CHECK_VERSION(2,36,0)
510 /* this callback is unnecessary with glib >2.36, the default
511  * prepare for the source does the same */
512 static gboolean
513 test_migrate_source_prepare(GSource *source, gint *timeout)
514 {
515     *timeout = -1;
516     return FALSE;
517 }
518 #endif
519
520 GSourceFuncs test_migrate_source_funcs = {
521 #if !GLIB_CHECK_VERSION(2,36,0)
522     .prepare = test_migrate_source_prepare,
523 #endif
524     .check = test_migrate_source_check,
525 };
526
527 static void test_migrate(void)
528 {
529     TestServer *s = test_server_new("src");
530     TestServer *dest = test_server_new("dest");
531     char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
532     QTestState *global = global_qtest, *from, *to;
533     GSource *source;
534     gchar *cmd;
535     QDict *rsp;
536     guint8 *log;
537     guint64 size;
538
539     cmd = GET_QEMU_CMDE(s, 2, "");
540     from = qtest_start(cmd);
541     g_free(cmd);
542
543     wait_for_fds(s);
544     size = get_log_size(s);
545     g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
546
547     cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri);
548     to = qtest_init(cmd);
549     g_free(cmd);
550
551     source = g_source_new(&test_migrate_source_funcs,
552                           sizeof(TestMigrateSource));
553     ((TestMigrateSource *)source)->src = s;
554     ((TestMigrateSource *)source)->dest = dest;
555     g_source_attach(source, NULL);
556
557     /* slow down migration to have time to fiddle with log */
558     /* TODO: qtest could learn to break on some places */
559     rsp = qmp("{ 'execute': 'migrate_set_speed',"
560               "'arguments': { 'value': 10 } }");
561     g_assert(qdict_haskey(rsp, "return"));
562     QDECREF(rsp);
563
564     cmd = g_strdup_printf("{ 'execute': 'migrate',"
565                           "'arguments': { 'uri': '%s' } }",
566                           uri);
567     rsp = qmp(cmd);
568     g_free(cmd);
569     g_assert(qdict_haskey(rsp, "return"));
570     QDECREF(rsp);
571
572     wait_for_log_fd(s);
573
574     log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
575     g_assert(log != MAP_FAILED);
576
577     /* modify first page */
578     write_guest_mem(s, 0x42);
579     log[0] = 1;
580     munmap(log, size);
581
582     /* speed things up */
583     rsp = qmp("{ 'execute': 'migrate_set_speed',"
584               "'arguments': { 'value': 0 } }");
585     g_assert(qdict_haskey(rsp, "return"));
586     QDECREF(rsp);
587
588     qmp_eventwait("STOP");
589
590     global_qtest = to;
591     qmp_eventwait("RESUME");
592
593     read_guest_mem(dest);
594
595     g_source_destroy(source);
596     g_source_unref(source);
597
598     qtest_quit(to);
599     test_server_free(dest);
600     qtest_quit(from);
601     test_server_free(s);
602     g_free(uri);
603
604     global_qtest = global;
605 }
606
607 int main(int argc, char **argv)
608 {
609     QTestState *s = NULL;
610     TestServer *server = NULL;
611     const char *hugefs;
612     char *qemu_cmd = NULL;
613     int ret;
614     char template[] = "/tmp/vhost-test-XXXXXX";
615     GMainLoop *loop;
616     GThread *thread;
617
618     g_test_init(&argc, &argv, NULL);
619
620     module_call_init(MODULE_INIT_QOM);
621     qemu_add_opts(&qemu_chardev_opts);
622
623     tmpfs = mkdtemp(template);
624     if (!tmpfs) {
625         g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
626     }
627     g_assert(tmpfs);
628
629     hugefs = getenv("QTEST_HUGETLBFS_PATH");
630     if (hugefs) {
631         root = init_hugepagefs(hugefs);
632         g_assert(root);
633     } else {
634         root = tmpfs;
635     }
636
637     server = test_server_new("test");
638
639     loop = g_main_loop_new(NULL, FALSE);
640     /* run the main loop thread so the chardev may operate */
641     thread = g_thread_new(NULL, thread_function, loop);
642
643     qemu_cmd = GET_QEMU_CMD(server);
644
645     s = qtest_start(qemu_cmd);
646     g_free(qemu_cmd);
647
648     qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
649     qtest_add_func("/vhost-user/migrate", test_migrate);
650
651     ret = g_test_run();
652
653     if (s) {
654         qtest_quit(s);
655     }
656
657     /* cleanup */
658     test_server_free(server);
659
660     /* finish the helper thread and dispatch pending sources */
661     g_main_loop_quit(loop);
662     g_thread_join(thread);
663     while (g_main_context_pending(NULL)) {
664         g_main_context_iteration (NULL, TRUE);
665     }
666     g_main_loop_unref(loop);
667
668     ret = rmdir(tmpfs);
669     if (ret != 0) {
670         g_test_message("unable to rmdir: path (%s): %s\n",
671                        tmpfs, strerror(errno));
672     }
673     g_assert_cmpint(ret, ==, 0);
674
675     return ret;
676 }
This page took 0.063106 seconds and 4 git commands to generate.