qjson: Spell out some silent assumptions
[qemu/ar7.git] / tests / vhost-user-test.c
blob022223b2a73735ac188bbdbf880e72d335751d26
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 "
39 #define QEMU_CMD_ROM " -option-rom ../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 QEMU_CMD_ROM
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 {
56 VHOST_USER_NONE = 0,
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,
74 VHOST_USER_MAX
75 } VhostUserRequest;
77 typedef struct VhostUserMemoryRegion {
78 uint64_t guest_phys_addr;
79 uint64_t memory_size;
80 uint64_t userspace_addr;
81 uint64_t mmap_offset;
82 } VhostUserMemoryRegion;
84 typedef struct VhostUserMemory {
85 uint32_t nregions;
86 uint32_t padding;
87 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
88 } VhostUserMemory;
90 typedef struct VhostUserLog {
91 uint64_t mmap_size;
92 uint64_t mmap_offset;
93 } VhostUserLog;
95 typedef struct VhostUserMsg {
96 VhostUserRequest request;
98 #define VHOST_USER_VERSION_MASK (0x3)
99 #define VHOST_USER_REPLY_MASK (0x1<<2)
100 uint32_t flags;
101 uint32_t size; /* the following payload size */
102 union {
103 #define VHOST_USER_VRING_IDX_MASK (0xff)
104 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
105 uint64_t u64;
106 struct vhost_vring_state state;
107 struct vhost_vring_addr addr;
108 VhostUserMemory memory;
109 VhostUserLog log;
110 } payload;
111 } QEMU_PACKED VhostUserMsg;
113 static VhostUserMsg m __attribute__ ((unused));
114 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
115 + sizeof(m.flags) \
116 + sizeof(m.size))
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 {
125 gchar *socket_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 } TestServer;
136 #if !GLIB_CHECK_VERSION(2, 32, 0)
137 static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex,
138 gint64 end_time)
140 gboolean ret = FALSE;
141 end_time -= g_get_monotonic_time();
142 GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
143 end_time % G_TIME_SPAN_SECOND };
144 ret = g_cond_timed_wait(cond, mutex, &time);
145 return ret;
147 #endif
149 static const char *tmpfs;
150 static const char *root;
152 static void wait_for_fds(TestServer *s)
154 gint64 end_time;
156 g_mutex_lock(&s->data_mutex);
158 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
159 while (!s->fds_num) {
160 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
161 /* timeout has passed */
162 g_assert(s->fds_num);
163 break;
167 /* check for sanity */
168 g_assert_cmpint(s->fds_num, >, 0);
169 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
171 g_mutex_unlock(&s->data_mutex);
174 static void read_guest_mem(TestServer *s)
176 uint32_t *guest_mem;
177 int i, j;
178 size_t size;
180 wait_for_fds(s);
182 g_mutex_lock(&s->data_mutex);
184 /* iterate all regions */
185 for (i = 0; i < s->fds_num; i++) {
187 /* We'll check only the region statring at 0x0*/
188 if (s->memory.regions[i].guest_phys_addr != 0x0) {
189 continue;
192 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
194 size = s->memory.regions[i].memory_size +
195 s->memory.regions[i].mmap_offset;
197 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
198 MAP_SHARED, s->fds[i], 0);
200 g_assert(guest_mem != MAP_FAILED);
201 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
203 for (j = 0; j < 256; j++) {
204 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
205 uint32_t b = guest_mem[j];
207 g_assert_cmpint(a, ==, b);
210 munmap(guest_mem, s->memory.regions[i].memory_size);
213 g_mutex_unlock(&s->data_mutex);
216 static void *thread_function(void *data)
218 GMainLoop *loop;
219 loop = g_main_loop_new(NULL, FALSE);
220 g_main_loop_run(loop);
221 return NULL;
224 static int chr_can_read(void *opaque)
226 return VHOST_USER_HDR_SIZE;
229 static void chr_read(void *opaque, const uint8_t *buf, int size)
231 TestServer *s = opaque;
232 CharDriverState *chr = s->chr;
233 VhostUserMsg msg;
234 uint8_t *p = (uint8_t *) &msg;
235 int fd;
237 if (size != VHOST_USER_HDR_SIZE) {
238 g_test_message("Wrong message size received %d\n", size);
239 return;
242 g_mutex_lock(&s->data_mutex);
243 memcpy(p, buf, VHOST_USER_HDR_SIZE);
245 if (msg.size) {
246 p += VHOST_USER_HDR_SIZE;
247 g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), ==, msg.size);
250 switch (msg.request) {
251 case VHOST_USER_GET_FEATURES:
252 /* send back features to qemu */
253 msg.flags |= VHOST_USER_REPLY_MASK;
254 msg.size = sizeof(m.payload.u64);
255 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
256 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
257 p = (uint8_t *) &msg;
258 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
259 break;
261 case VHOST_USER_SET_FEATURES:
262 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
263 !=, 0ULL);
264 break;
266 case VHOST_USER_GET_PROTOCOL_FEATURES:
267 /* send back features to qemu */
268 msg.flags |= VHOST_USER_REPLY_MASK;
269 msg.size = sizeof(m.payload.u64);
270 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
271 p = (uint8_t *) &msg;
272 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
273 break;
275 case VHOST_USER_GET_VRING_BASE:
276 /* send back vring base to qemu */
277 msg.flags |= VHOST_USER_REPLY_MASK;
278 msg.size = sizeof(m.payload.state);
279 msg.payload.state.num = 0;
280 p = (uint8_t *) &msg;
281 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
282 break;
284 case VHOST_USER_SET_MEM_TABLE:
285 /* received the mem table */
286 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
287 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(s->fds));
289 /* signal the test that it can continue */
290 g_cond_signal(&s->data_cond);
291 break;
293 case VHOST_USER_SET_VRING_KICK:
294 case VHOST_USER_SET_VRING_CALL:
295 /* consume the fd */
296 qemu_chr_fe_get_msgfds(chr, &fd, 1);
298 * This is a non-blocking eventfd.
299 * The receive function forces it to be blocking,
300 * so revert it back to non-blocking.
302 qemu_set_nonblock(fd);
303 break;
305 case VHOST_USER_SET_LOG_BASE:
306 if (s->log_fd != -1) {
307 close(s->log_fd);
308 s->log_fd = -1;
310 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
311 msg.flags |= VHOST_USER_REPLY_MASK;
312 msg.size = 0;
313 p = (uint8_t *) &msg;
314 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
316 g_cond_signal(&s->data_cond);
317 break;
319 case VHOST_USER_SET_VRING_ENABLE:
320 if (!msg.payload.state.num) {
321 s->fds_num = 0;
323 break;
325 default:
326 break;
329 g_mutex_unlock(&s->data_mutex);
332 static const char *init_hugepagefs(const char *path)
334 struct statfs fs;
335 int ret;
337 if (access(path, R_OK | W_OK | X_OK)) {
338 g_test_message("access on path (%s): %s\n", path, strerror(errno));
339 return NULL;
342 do {
343 ret = statfs(path, &fs);
344 } while (ret != 0 && errno == EINTR);
346 if (ret != 0) {
347 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
348 return NULL;
351 if (fs.f_type != HUGETLBFS_MAGIC) {
352 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
353 return NULL;
356 return path;
359 static TestServer *test_server_new(const gchar *name)
361 TestServer *server = g_new0(TestServer, 1);
362 gchar *chr_path;
364 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
366 chr_path = g_strdup_printf("unix:%s,server,nowait", server->socket_path);
367 server->chr_name = g_strdup_printf("chr-%s", name);
368 server->chr = qemu_chr_new(server->chr_name, chr_path, NULL);
369 g_free(chr_path);
371 qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, server);
373 g_mutex_init(&server->data_mutex);
374 g_cond_init(&server->data_cond);
376 server->log_fd = -1;
378 return server;
381 #define GET_QEMU_CMD(s) \
382 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
383 (s)->socket_path, (s)->chr_name)
385 #define GET_QEMU_CMDE(s, mem, extra, ...) \
386 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
387 (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
389 static void test_server_free(TestServer *server)
391 int i;
393 qemu_chr_delete(server->chr);
395 for (i = 0; i < server->fds_num; i++) {
396 close(server->fds[i]);
399 if (server->log_fd != -1) {
400 close(server->log_fd);
403 unlink(server->socket_path);
404 g_free(server->socket_path);
407 g_free(server->chr_name);
408 g_free(server);
411 static void wait_for_log_fd(TestServer *s)
413 gint64 end_time;
415 g_mutex_lock(&s->data_mutex);
416 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
417 while (s->log_fd == -1) {
418 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
419 /* timeout has passed */
420 g_assert(s->log_fd != -1);
421 break;
425 g_mutex_unlock(&s->data_mutex);
428 static void write_guest_mem(TestServer *s, uint32 seed)
430 uint32_t *guest_mem;
431 int i, j;
432 size_t size;
434 wait_for_fds(s);
436 /* iterate all regions */
437 for (i = 0; i < s->fds_num; i++) {
439 /* We'll write only the region statring at 0x0 */
440 if (s->memory.regions[i].guest_phys_addr != 0x0) {
441 continue;
444 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
446 size = s->memory.regions[i].memory_size +
447 s->memory.regions[i].mmap_offset;
449 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
450 MAP_SHARED, s->fds[i], 0);
452 g_assert(guest_mem != MAP_FAILED);
453 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
455 for (j = 0; j < 256; j++) {
456 guest_mem[j] = seed + j;
459 munmap(guest_mem, s->memory.regions[i].memory_size);
460 break;
464 static guint64 get_log_size(TestServer *s)
466 guint64 log_size = 0;
467 int i;
469 for (i = 0; i < s->memory.nregions; ++i) {
470 VhostUserMemoryRegion *reg = &s->memory.regions[i];
471 guint64 last = range_get_last(reg->guest_phys_addr,
472 reg->memory_size);
473 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
476 return log_size;
479 typedef struct TestMigrateSource {
480 GSource source;
481 TestServer *src;
482 TestServer *dest;
483 } TestMigrateSource;
485 static gboolean
486 test_migrate_source_check(GSource *source)
488 TestMigrateSource *t = (TestMigrateSource *)source;
489 gboolean overlap = t->src->fds_num > 0 && t->dest->fds_num > 0;
491 g_assert(!overlap);
493 return FALSE;
496 GSourceFuncs test_migrate_source_funcs = {
497 NULL,
498 test_migrate_source_check,
499 NULL,
500 NULL
503 static void test_migrate(void)
505 TestServer *s = test_server_new("src");
506 TestServer *dest = test_server_new("dest");
507 const char *uri = "tcp:127.0.0.1:1234";
508 QTestState *global = global_qtest, *from, *to;
509 GSource *source;
510 gchar *cmd;
511 QDict *rsp;
512 guint8 *log;
513 guint64 size;
515 cmd = GET_QEMU_CMDE(s, 2, "");
516 from = qtest_start(cmd);
517 g_free(cmd);
519 wait_for_fds(s);
520 size = get_log_size(s);
521 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
523 cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri);
524 to = qtest_init(cmd);
525 g_free(cmd);
527 source = g_source_new(&test_migrate_source_funcs,
528 sizeof(TestMigrateSource));
529 ((TestMigrateSource *)source)->src = s;
530 ((TestMigrateSource *)source)->dest = dest;
531 g_source_attach(source, NULL);
533 /* slow down migration to have time to fiddle with log */
534 /* TODO: qtest could learn to break on some places */
535 rsp = qmp("{ 'execute': 'migrate_set_speed',"
536 "'arguments': { 'value': 10 } }");
537 g_assert(qdict_haskey(rsp, "return"));
538 QDECREF(rsp);
540 cmd = g_strdup_printf("{ 'execute': 'migrate',"
541 "'arguments': { 'uri': '%s' } }",
542 uri);
543 rsp = qmp(cmd);
544 g_free(cmd);
545 g_assert(qdict_haskey(rsp, "return"));
546 QDECREF(rsp);
548 wait_for_log_fd(s);
550 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
551 g_assert(log != MAP_FAILED);
553 /* modify first page */
554 write_guest_mem(s, 0x42);
555 log[0] = 1;
556 munmap(log, size);
558 /* speed things up */
559 rsp = qmp("{ 'execute': 'migrate_set_speed',"
560 "'arguments': { 'value': 0 } }");
561 g_assert(qdict_haskey(rsp, "return"));
562 QDECREF(rsp);
564 qmp_eventwait("STOP");
566 global_qtest = to;
567 qmp_eventwait("RESUME");
569 read_guest_mem(dest);
571 g_source_destroy(source);
572 g_source_unref(source);
574 qtest_quit(to);
575 test_server_free(dest);
576 qtest_quit(from);
577 test_server_free(s);
579 global_qtest = global;
582 int main(int argc, char **argv)
584 QTestState *s = NULL;
585 TestServer *server = NULL;
586 const char *hugefs;
587 char *qemu_cmd = NULL;
588 int ret;
589 char template[] = "/tmp/vhost-test-XXXXXX";
591 g_test_init(&argc, &argv, NULL);
593 module_call_init(MODULE_INIT_QOM);
594 qemu_add_opts(&qemu_chardev_opts);
596 tmpfs = mkdtemp(template);
597 if (!tmpfs) {
598 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
600 g_assert(tmpfs);
602 hugefs = getenv("QTEST_HUGETLBFS_PATH");
603 if (hugefs) {
604 root = init_hugepagefs(hugefs);
605 g_assert(root);
606 } else {
607 root = tmpfs;
610 server = test_server_new("test");
612 /* run the main loop thread so the chardev may operate */
613 g_thread_new(NULL, thread_function, NULL);
615 qemu_cmd = GET_QEMU_CMD(server);
617 s = qtest_start(qemu_cmd);
618 g_free(qemu_cmd);
620 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
621 qtest_add_func("/vhost-user/migrate", test_migrate);
623 ret = g_test_run();
625 if (s) {
626 qtest_quit(s);
629 /* cleanup */
630 test_server_free(server);
632 ret = rmdir(tmpfs);
633 if (ret != 0) {
634 g_test_message("unable to rmdir: path (%s): %s\n",
635 tmpfs, strerror(errno));
637 g_assert_cmpint(ret, ==, 0);
639 return ret;