qapi: Consolidate visitor small integer callbacks
[qemu/ar7.git] / tests / vhost-user-test.c
blobe30b7f4abc0806895832eacc1d906d7f18a0db6a
1 /*
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.
9 */
11 #include <glib.h>
13 #include "libqtest.h"
14 #include "qemu/option.h"
15 #include "qemu/range.h"
16 #include "sysemu/char.h"
17 #include "sysemu/sysemu.h"
19 #include <linux/vhost.h>
20 #include <sys/mman.h>
21 #include <sys/vfs.h>
22 #include <qemu/sockets.h>
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
29 #if GLIB_CHECK_VERSION(2, 28, 0)
30 #define HAVE_MONOTONIC_TIME
31 #endif
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"
40 #define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \
41 QEMU_CMD_NETDEV QEMU_CMD_NET
43 #define HUGETLBFS_MAGIC 0x958458f6
45 /*********** FROM hw/virtio/vhost-user.c *************************************/
47 #define VHOST_MEMORY_MAX_NREGIONS 8
49 #define VHOST_USER_F_PROTOCOL_FEATURES 30
50 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
52 #define VHOST_LOG_PAGE 0x1000
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;
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;
83 typedef struct VhostUserMemory {
84 uint32_t nregions;
85 uint32_t padding;
86 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
87 } VhostUserMemory;
89 typedef struct VhostUserLog {
90 uint64_t mmap_size;
91 uint64_t mmap_offset;
92 } VhostUserLog;
94 typedef struct VhostUserMsg {
95 VhostUserRequest request;
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;
112 static VhostUserMsg m __attribute__ ((unused));
113 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
114 + sizeof(m.flags) \
115 + sizeof(m.size))
117 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
119 /* The version of the protocol we support */
120 #define VHOST_USER_VERSION (0x1)
121 /*****************************************************************************/
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;
137 #if !GLIB_CHECK_VERSION(2, 32, 0)
138 static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex,
139 gint64 end_time)
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;
148 #endif
150 static const char *tmpfs;
151 static const char *root;
153 static void wait_for_fds(TestServer *s)
155 gint64 end_time;
157 g_mutex_lock(&s->data_mutex);
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;
168 /* check for sanity */
169 g_assert_cmpint(s->fds_num, >, 0);
170 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
172 g_mutex_unlock(&s->data_mutex);
175 static void read_guest_mem(const void *data)
177 TestServer *s = (void *)data;
178 uint32_t *guest_mem;
179 int i, j;
180 size_t size;
182 wait_for_fds(s);
184 g_mutex_lock(&s->data_mutex);
186 /* iterate all regions */
187 for (i = 0; i < s->fds_num; i++) {
189 /* We'll check only the region statring at 0x0*/
190 if (s->memory.regions[i].guest_phys_addr != 0x0) {
191 continue;
194 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
196 size = s->memory.regions[i].memory_size +
197 s->memory.regions[i].mmap_offset;
199 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
200 MAP_SHARED, s->fds[i], 0);
202 g_assert(guest_mem != MAP_FAILED);
203 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
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];
209 g_assert_cmpint(a, ==, b);
212 munmap(guest_mem, s->memory.regions[i].memory_size);
215 g_mutex_unlock(&s->data_mutex);
218 static void *thread_function(void *data)
220 GMainLoop *loop = data;
221 g_main_loop_run(loop);
222 return NULL;
225 static int chr_can_read(void *opaque)
227 return VHOST_USER_HDR_SIZE;
230 static void chr_read(void *opaque, const uint8_t *buf, int size)
232 TestServer *s = opaque;
233 CharDriverState *chr = s->chr;
234 VhostUserMsg msg;
235 uint8_t *p = (uint8_t *) &msg;
236 int fd;
238 if (size != VHOST_USER_HDR_SIZE) {
239 g_test_message("Wrong message size received %d\n", size);
240 return;
243 g_mutex_lock(&s->data_mutex);
244 memcpy(p, buf, VHOST_USER_HDR_SIZE);
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);
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;
262 case VHOST_USER_SET_FEATURES:
263 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
264 !=, 0ULL);
265 break;
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;
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);
284 assert(msg.payload.state.index < 2);
285 s->rings &= ~(0x1ULL << msg.payload.state.index);
286 break;
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));
293 /* signal the test that it can continue */
294 g_cond_signal(&s->data_cond);
295 break;
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);
302 * This is a non-blocking eventfd.
303 * The receive function forces it to be blocking,
304 * so revert it back to non-blocking.
306 qemu_set_nonblock(fd);
307 break;
309 case VHOST_USER_SET_LOG_BASE:
310 if (s->log_fd != -1) {
311 close(s->log_fd);
312 s->log_fd = -1;
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);
320 g_cond_signal(&s->data_cond);
321 break;
323 case VHOST_USER_SET_VRING_BASE:
324 assert(msg.payload.state.index < 2);
325 s->rings |= 0x1ULL << msg.payload.state.index;
326 break;
328 default:
329 break;
332 g_mutex_unlock(&s->data_mutex);
335 static const char *init_hugepagefs(const char *path)
337 struct statfs fs;
338 int ret;
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;
345 do {
346 ret = statfs(path, &fs);
347 } while (ret != 0 && errno == EINTR);
349 if (ret != 0) {
350 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
351 return NULL;
354 if (fs.f_type != HUGETLBFS_MAGIC) {
355 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
356 return NULL;
359 return path;
362 static TestServer *test_server_new(const gchar *name)
364 TestServer *server = g_new0(TestServer, 1);
365 gchar *chr_path;
367 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
368 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
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);
375 qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, server);
377 g_mutex_init(&server->data_mutex);
378 g_cond_init(&server->data_cond);
380 server->log_fd = -1;
382 return server;
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)
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__)
393 static gboolean _test_server_free(TestServer *server)
395 int i;
397 qemu_chr_delete(server->chr);
399 for (i = 0; i < server->fds_num; i++) {
400 close(server->fds[i]);
403 if (server->log_fd != -1) {
404 close(server->log_fd);
407 unlink(server->socket_path);
408 g_free(server->socket_path);
410 unlink(server->mig_path);
411 g_free(server->mig_path);
413 g_free(server->chr_name);
414 g_free(server);
416 return FALSE;
419 static void test_server_free(TestServer *server)
421 g_idle_add((GSourceFunc)_test_server_free, server);
424 static void wait_for_log_fd(TestServer *s)
426 gint64 end_time;
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;
438 g_mutex_unlock(&s->data_mutex);
441 static void write_guest_mem(TestServer *s, uint32_t seed)
443 uint32_t *guest_mem;
444 int i, j;
445 size_t size;
447 wait_for_fds(s);
449 /* iterate all regions */
450 for (i = 0; i < s->fds_num; i++) {
452 /* We'll write only the region statring at 0x0 */
453 if (s->memory.regions[i].guest_phys_addr != 0x0) {
454 continue;
457 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
459 size = s->memory.regions[i].memory_size +
460 s->memory.regions[i].mmap_offset;
462 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
463 MAP_SHARED, s->fds[i], 0);
465 g_assert(guest_mem != MAP_FAILED);
466 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
468 for (j = 0; j < 256; j++) {
469 guest_mem[j] = seed + j;
472 munmap(guest_mem, s->memory.regions[i].memory_size);
473 break;
477 static guint64 get_log_size(TestServer *s)
479 guint64 log_size = 0;
480 int i;
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);
489 return log_size;
492 typedef struct TestMigrateSource {
493 GSource source;
494 TestServer *src;
495 TestServer *dest;
496 } TestMigrateSource;
498 static gboolean
499 test_migrate_source_check(GSource *source)
501 TestMigrateSource *t = (TestMigrateSource *)source;
502 gboolean overlap = t->src->rings && t->dest->rings;
504 g_assert(!overlap);
506 return FALSE;
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)
515 *timeout = -1;
516 return FALSE;
518 #endif
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,
527 static void test_migrate(void)
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;
539 cmd = GET_QEMU_CMDE(s, 2, "");
540 from = qtest_start(cmd);
541 g_free(cmd);
543 wait_for_fds(s);
544 size = get_log_size(s);
545 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
547 cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri);
548 to = qtest_init(cmd);
549 g_free(cmd);
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);
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);
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);
572 wait_for_log_fd(s);
574 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
575 g_assert(log != MAP_FAILED);
577 /* modify first page */
578 write_guest_mem(s, 0x42);
579 log[0] = 1;
580 munmap(log, size);
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);
588 qmp_eventwait("STOP");
590 global_qtest = to;
591 qmp_eventwait("RESUME");
593 read_guest_mem(dest);
595 g_source_destroy(source);
596 g_source_unref(source);
598 qtest_quit(to);
599 test_server_free(dest);
600 qtest_quit(from);
601 test_server_free(s);
602 g_free(uri);
604 global_qtest = global;
607 int main(int argc, char **argv)
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;
618 g_test_init(&argc, &argv, NULL);
620 module_call_init(MODULE_INIT_QOM);
621 qemu_add_opts(&qemu_chardev_opts);
623 tmpfs = mkdtemp(template);
624 if (!tmpfs) {
625 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
627 g_assert(tmpfs);
629 hugefs = getenv("QTEST_HUGETLBFS_PATH");
630 if (hugefs) {
631 root = init_hugepagefs(hugefs);
632 g_assert(root);
633 } else {
634 root = tmpfs;
637 server = test_server_new("test");
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);
643 qemu_cmd = GET_QEMU_CMD(server);
645 s = qtest_start(qemu_cmd);
646 g_free(qemu_cmd);
648 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
649 qtest_add_func("/vhost-user/migrate", test_migrate);
651 ret = g_test_run();
653 if (s) {
654 qtest_quit(s);
657 /* cleanup */
658 test_server_free(server);
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);
666 g_main_loop_unref(loop);
668 ret = rmdir(tmpfs);
669 if (ret != 0) {
670 g_test_message("unable to rmdir: path (%s): %s\n",
671 tmpfs, strerror(errno));
673 g_assert_cmpint(ret, ==, 0);
675 return ret;