2 * QTest testcase for the vhost-user
4 * Copyright (c) 2014 Virtual Open Systems Sarl.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
15 #include "qemu/option.h"
16 #include "qemu/range.h"
17 #include "sysemu/char.h"
18 #include "sysemu/sysemu.h"
20 #include <linux/vhost.h>
23 #include <qemu/sockets.h>
25 /* GLIB version compatibility flags */
26 #if !GLIB_CHECK_VERSION(2, 26, 0)
27 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
30 #if GLIB_CHECK_VERSION(2, 28, 0)
31 #define HAVE_MONOTONIC_TIME
34 #define QEMU_CMD_ACCEL " -machine accel=tcg"
35 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
36 "mem-path=%s,share=on -numa node,memdev=mem"
37 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s"
38 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
39 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0,romfile=./pc-bios/pxe-virtio.rom"
41 #define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \
42 QEMU_CMD_NETDEV QEMU_CMD_NET
44 #define HUGETLBFS_MAGIC 0x958458f6
46 /*********** FROM hw/virtio/vhost-user.c *************************************/
48 #define VHOST_MEMORY_MAX_NREGIONS 8
50 #define VHOST_USER_F_PROTOCOL_FEATURES 30
51 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
53 #define VHOST_LOG_PAGE 0x1000
55 typedef enum VhostUserRequest {
57 VHOST_USER_GET_FEATURES = 1,
58 VHOST_USER_SET_FEATURES = 2,
59 VHOST_USER_SET_OWNER = 3,
60 VHOST_USER_RESET_OWNER = 4,
61 VHOST_USER_SET_MEM_TABLE = 5,
62 VHOST_USER_SET_LOG_BASE = 6,
63 VHOST_USER_SET_LOG_FD = 7,
64 VHOST_USER_SET_VRING_NUM = 8,
65 VHOST_USER_SET_VRING_ADDR = 9,
66 VHOST_USER_SET_VRING_BASE = 10,
67 VHOST_USER_GET_VRING_BASE = 11,
68 VHOST_USER_SET_VRING_KICK = 12,
69 VHOST_USER_SET_VRING_CALL = 13,
70 VHOST_USER_SET_VRING_ERR = 14,
71 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
72 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
73 VHOST_USER_SET_VRING_ENABLE = 18,
77 typedef struct VhostUserMemoryRegion {
78 uint64_t guest_phys_addr;
80 uint64_t userspace_addr;
82 } VhostUserMemoryRegion;
84 typedef struct VhostUserMemory {
87 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
90 typedef struct VhostUserLog {
95 typedef struct VhostUserMsg {
96 VhostUserRequest request;
98 #define VHOST_USER_VERSION_MASK (0x3)
99 #define VHOST_USER_REPLY_MASK (0x1<<2)
101 uint32_t size; /* the following payload size */
103 #define VHOST_USER_VRING_IDX_MASK (0xff)
104 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
106 struct vhost_vring_state state;
107 struct vhost_vring_addr addr;
108 VhostUserMemory memory;
111 } QEMU_PACKED VhostUserMsg;
113 static VhostUserMsg m __attribute__ ((unused));
114 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
118 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
120 /* The version of the protocol we support */
121 #define VHOST_USER_VERSION (0x1)
122 /*****************************************************************************/
124 typedef struct TestServer {
128 CharDriverState *chr;
130 int fds[VHOST_MEMORY_MAX_NREGIONS];
131 VhostUserMemory memory;
138 #if !GLIB_CHECK_VERSION(2, 32, 0)
139 static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex,
142 gboolean ret = FALSE;
143 end_time -= g_get_monotonic_time();
144 GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
145 end_time % G_TIME_SPAN_SECOND };
146 ret = g_cond_timed_wait(cond, mutex, &time);
151 static const char *tmpfs;
152 static const char *root;
154 static void wait_for_fds(TestServer *s)
158 g_mutex_lock(&s->data_mutex);
160 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
161 while (!s->fds_num) {
162 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
163 /* timeout has passed */
164 g_assert(s->fds_num);
169 /* check for sanity */
170 g_assert_cmpint(s->fds_num, >, 0);
171 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
173 g_mutex_unlock(&s->data_mutex);
176 static void read_guest_mem(const void *data)
178 TestServer *s = (void *)data;
185 g_mutex_lock(&s->data_mutex);
187 /* iterate all regions */
188 for (i = 0; i < s->fds_num; i++) {
190 /* We'll check only the region statring at 0x0*/
191 if (s->memory.regions[i].guest_phys_addr != 0x0) {
195 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
197 size = s->memory.regions[i].memory_size +
198 s->memory.regions[i].mmap_offset;
200 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
201 MAP_SHARED, s->fds[i], 0);
203 g_assert(guest_mem != MAP_FAILED);
204 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
206 for (j = 0; j < 256; j++) {
207 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
208 uint32_t b = guest_mem[j];
210 g_assert_cmpint(a, ==, b);
213 munmap(guest_mem, s->memory.regions[i].memory_size);
216 g_mutex_unlock(&s->data_mutex);
219 static void *thread_function(void *data)
221 GMainLoop *loop = data;
222 g_main_loop_run(loop);
226 static int chr_can_read(void *opaque)
228 return VHOST_USER_HDR_SIZE;
231 static void chr_read(void *opaque, const uint8_t *buf, int size)
233 TestServer *s = opaque;
234 CharDriverState *chr = s->chr;
236 uint8_t *p = (uint8_t *) &msg;
239 if (size != VHOST_USER_HDR_SIZE) {
240 g_test_message("Wrong message size received %d\n", size);
244 g_mutex_lock(&s->data_mutex);
245 memcpy(p, buf, VHOST_USER_HDR_SIZE);
248 p += VHOST_USER_HDR_SIZE;
249 g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), ==, msg.size);
252 switch (msg.request) {
253 case VHOST_USER_GET_FEATURES:
254 /* send back features to qemu */
255 msg.flags |= VHOST_USER_REPLY_MASK;
256 msg.size = sizeof(m.payload.u64);
257 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
258 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
259 p = (uint8_t *) &msg;
260 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
263 case VHOST_USER_SET_FEATURES:
264 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
268 case VHOST_USER_GET_PROTOCOL_FEATURES:
269 /* send back features to qemu */
270 msg.flags |= VHOST_USER_REPLY_MASK;
271 msg.size = sizeof(m.payload.u64);
272 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
273 p = (uint8_t *) &msg;
274 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
277 case VHOST_USER_GET_VRING_BASE:
278 /* send back vring base to qemu */
279 msg.flags |= VHOST_USER_REPLY_MASK;
280 msg.size = sizeof(m.payload.state);
281 msg.payload.state.num = 0;
282 p = (uint8_t *) &msg;
283 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
285 assert(msg.payload.state.index < 2);
286 s->rings &= ~(0x1ULL << msg.payload.state.index);
289 case VHOST_USER_SET_MEM_TABLE:
290 /* received the mem table */
291 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
292 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(s->fds));
294 /* signal the test that it can continue */
295 g_cond_signal(&s->data_cond);
298 case VHOST_USER_SET_VRING_KICK:
299 case VHOST_USER_SET_VRING_CALL:
301 qemu_chr_fe_get_msgfds(chr, &fd, 1);
303 * This is a non-blocking eventfd.
304 * The receive function forces it to be blocking,
305 * so revert it back to non-blocking.
307 qemu_set_nonblock(fd);
310 case VHOST_USER_SET_LOG_BASE:
311 if (s->log_fd != -1) {
315 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
316 msg.flags |= VHOST_USER_REPLY_MASK;
318 p = (uint8_t *) &msg;
319 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
321 g_cond_signal(&s->data_cond);
324 case VHOST_USER_SET_VRING_BASE:
325 assert(msg.payload.state.index < 2);
326 s->rings |= 0x1ULL << msg.payload.state.index;
333 g_mutex_unlock(&s->data_mutex);
336 static const char *init_hugepagefs(const char *path)
341 if (access(path, R_OK | W_OK | X_OK)) {
342 g_test_message("access on path (%s): %s\n", path, strerror(errno));
347 ret = statfs(path, &fs);
348 } while (ret != 0 && errno == EINTR);
351 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
355 if (fs.f_type != HUGETLBFS_MAGIC) {
356 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
363 static TestServer *test_server_new(const gchar *name)
365 TestServer *server = g_new0(TestServer, 1);
368 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
369 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
371 chr_path = g_strdup_printf("unix:%s,server,nowait", server->socket_path);
372 server->chr_name = g_strdup_printf("chr-%s", name);
373 server->chr = qemu_chr_new(server->chr_name, chr_path, NULL);
376 qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, server);
378 g_mutex_init(&server->data_mutex);
379 g_cond_init(&server->data_cond);
386 #define GET_QEMU_CMD(s) \
387 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
388 (s)->socket_path, (s)->chr_name)
390 #define GET_QEMU_CMDE(s, mem, extra, ...) \
391 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
392 (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
394 static gboolean _test_server_free(TestServer *server)
398 qemu_chr_delete(server->chr);
400 for (i = 0; i < server->fds_num; i++) {
401 close(server->fds[i]);
404 if (server->log_fd != -1) {
405 close(server->log_fd);
408 unlink(server->socket_path);
409 g_free(server->socket_path);
411 unlink(server->mig_path);
412 g_free(server->mig_path);
414 g_free(server->chr_name);
420 static void test_server_free(TestServer *server)
422 g_idle_add((GSourceFunc)_test_server_free, server);
425 static void wait_for_log_fd(TestServer *s)
429 g_mutex_lock(&s->data_mutex);
430 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
431 while (s->log_fd == -1) {
432 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
433 /* timeout has passed */
434 g_assert(s->log_fd != -1);
439 g_mutex_unlock(&s->data_mutex);
442 static void write_guest_mem(TestServer *s, uint32_t seed)
450 /* iterate all regions */
451 for (i = 0; i < s->fds_num; i++) {
453 /* We'll write only the region statring at 0x0 */
454 if (s->memory.regions[i].guest_phys_addr != 0x0) {
458 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
460 size = s->memory.regions[i].memory_size +
461 s->memory.regions[i].mmap_offset;
463 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
464 MAP_SHARED, s->fds[i], 0);
466 g_assert(guest_mem != MAP_FAILED);
467 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
469 for (j = 0; j < 256; j++) {
470 guest_mem[j] = seed + j;
473 munmap(guest_mem, s->memory.regions[i].memory_size);
478 static guint64 get_log_size(TestServer *s)
480 guint64 log_size = 0;
483 for (i = 0; i < s->memory.nregions; ++i) {
484 VhostUserMemoryRegion *reg = &s->memory.regions[i];
485 guint64 last = range_get_last(reg->guest_phys_addr,
487 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
493 typedef struct TestMigrateSource {
500 test_migrate_source_check(GSource *source)
502 TestMigrateSource *t = (TestMigrateSource *)source;
503 gboolean overlap = t->src->rings && t->dest->rings;
510 #if !GLIB_CHECK_VERSION(2,36,0)
511 /* this callback is unnecessary with glib >2.36, the default
512 * prepare for the source does the same */
514 test_migrate_source_prepare(GSource *source, gint *timeout)
521 GSourceFuncs test_migrate_source_funcs = {
522 #if !GLIB_CHECK_VERSION(2,36,0)
523 .prepare = test_migrate_source_prepare,
525 .check = test_migrate_source_check,
528 static void test_migrate(void)
530 TestServer *s = test_server_new("src");
531 TestServer *dest = test_server_new("dest");
532 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
533 QTestState *global = global_qtest, *from, *to;
540 cmd = GET_QEMU_CMDE(s, 2, "");
541 from = qtest_start(cmd);
545 size = get_log_size(s);
546 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
548 cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri);
549 to = qtest_init(cmd);
552 source = g_source_new(&test_migrate_source_funcs,
553 sizeof(TestMigrateSource));
554 ((TestMigrateSource *)source)->src = s;
555 ((TestMigrateSource *)source)->dest = dest;
556 g_source_attach(source, NULL);
558 /* slow down migration to have time to fiddle with log */
559 /* TODO: qtest could learn to break on some places */
560 rsp = qmp("{ 'execute': 'migrate_set_speed',"
561 "'arguments': { 'value': 10 } }");
562 g_assert(qdict_haskey(rsp, "return"));
565 cmd = g_strdup_printf("{ 'execute': 'migrate',"
566 "'arguments': { 'uri': '%s' } }",
570 g_assert(qdict_haskey(rsp, "return"));
575 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
576 g_assert(log != MAP_FAILED);
578 /* modify first page */
579 write_guest_mem(s, 0x42);
583 /* speed things up */
584 rsp = qmp("{ 'execute': 'migrate_set_speed',"
585 "'arguments': { 'value': 0 } }");
586 g_assert(qdict_haskey(rsp, "return"));
589 qmp_eventwait("STOP");
592 qmp_eventwait("RESUME");
594 read_guest_mem(dest);
596 g_source_destroy(source);
597 g_source_unref(source);
600 test_server_free(dest);
605 global_qtest = global;
608 int main(int argc, char **argv)
610 QTestState *s = NULL;
611 TestServer *server = NULL;
613 char *qemu_cmd = NULL;
615 char template[] = "/tmp/vhost-test-XXXXXX";
619 g_test_init(&argc, &argv, NULL);
621 module_call_init(MODULE_INIT_QOM);
622 qemu_add_opts(&qemu_chardev_opts);
624 tmpfs = mkdtemp(template);
626 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
630 hugefs = getenv("QTEST_HUGETLBFS_PATH");
632 root = init_hugepagefs(hugefs);
638 server = test_server_new("test");
640 loop = g_main_loop_new(NULL, FALSE);
641 /* run the main loop thread so the chardev may operate */
642 thread = g_thread_new(NULL, thread_function, loop);
644 qemu_cmd = GET_QEMU_CMD(server);
646 s = qtest_start(qemu_cmd);
649 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
650 qtest_add_func("/vhost-user/migrate", test_migrate);
659 test_server_free(server);
661 /* finish the helper thread and dispatch pending sources */
662 g_main_loop_quit(loop);
663 g_thread_join(thread);
664 while (g_main_context_pending(NULL)) {
665 g_main_context_iteration (NULL, TRUE);
667 g_main_loop_unref(loop);
671 g_test_message("unable to rmdir: path (%s): %s\n",
672 tmpfs, strerror(errno));
674 g_assert_cmpint(ret, ==, 0);