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.
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>
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))
29 #if GLIB_CHECK_VERSION(2, 28, 0)
30 #define HAVE_MONOTONIC_TIME
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 {
57 VHOST_USER_GET_FEATURES = 1,
58 VHOST_USER_SET_FEATURES = 2,
59 VHOST_USER_SET_OWNER = 3,
60 VHOST_USER_RESET_DEVICE = 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,
76 typedef struct VhostUserMemoryRegion {
77 uint64_t guest_phys_addr;
79 uint64_t userspace_addr;
81 } VhostUserMemoryRegion;
83 typedef struct VhostUserMemory {
86 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
89 typedef struct VhostUserMsg {
90 VhostUserRequest request;
92 #define VHOST_USER_VERSION_MASK (0x3)
93 #define VHOST_USER_REPLY_MASK (0x1<<2)
95 uint32_t size; /* the following payload size */
98 struct vhost_vring_state state;
99 struct vhost_vring_addr addr;
100 VhostUserMemory memory;
102 } QEMU_PACKED VhostUserMsg;
104 static VhostUserMsg m __attribute__ ((unused));
105 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
109 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
111 /* The version of the protocol we support */
112 #define VHOST_USER_VERSION (0x1)
113 /*****************************************************************************/
115 typedef struct TestServer {
118 CharDriverState *chr;
120 int fds[VHOST_MEMORY_MAX_NREGIONS];
121 VhostUserMemory memory;
127 #if !GLIB_CHECK_VERSION(2, 32, 0)
128 static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex,
131 gboolean ret = FALSE;
132 end_time -= g_get_monotonic_time();
133 GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
134 end_time % G_TIME_SPAN_SECOND };
135 ret = g_cond_timed_wait(cond, mutex, &time);
140 static const char *tmpfs;
141 static const char *root;
143 static void wait_for_fds(TestServer *s)
147 g_mutex_lock(&s->data_mutex);
149 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
150 while (!s->fds_num) {
151 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
152 /* timeout has passed */
153 g_assert(s->fds_num);
158 /* check for sanity */
159 g_assert_cmpint(s->fds_num, >, 0);
160 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
162 g_mutex_unlock(&s->data_mutex);
165 static void read_guest_mem(TestServer *s)
173 g_mutex_lock(&s->data_mutex);
175 /* iterate all regions */
176 for (i = 0; i < s->fds_num; i++) {
178 /* We'll check only the region statring at 0x0*/
179 if (s->memory.regions[i].guest_phys_addr != 0x0) {
183 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
185 size = s->memory.regions[i].memory_size +
186 s->memory.regions[i].mmap_offset;
188 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
189 MAP_SHARED, s->fds[i], 0);
191 g_assert(guest_mem != MAP_FAILED);
192 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
194 for (j = 0; j < 256; j++) {
195 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
196 uint32_t b = guest_mem[j];
198 g_assert_cmpint(a, ==, b);
201 munmap(guest_mem, s->memory.regions[i].memory_size);
204 g_mutex_unlock(&s->data_mutex);
207 static void *thread_function(void *data)
210 loop = g_main_loop_new(NULL, FALSE);
211 g_main_loop_run(loop);
215 static int chr_can_read(void *opaque)
217 return VHOST_USER_HDR_SIZE;
220 static void chr_read(void *opaque, const uint8_t *buf, int size)
222 TestServer *s = opaque;
223 CharDriverState *chr = s->chr;
225 uint8_t *p = (uint8_t *) &msg;
228 if (size != VHOST_USER_HDR_SIZE) {
229 g_test_message("Wrong message size received %d\n", size);
233 g_mutex_lock(&s->data_mutex);
234 memcpy(p, buf, VHOST_USER_HDR_SIZE);
237 p += VHOST_USER_HDR_SIZE;
238 g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), ==, msg.size);
241 switch (msg.request) {
242 case VHOST_USER_GET_FEATURES:
243 /* send back features to qemu */
244 msg.flags |= VHOST_USER_REPLY_MASK;
245 msg.size = sizeof(m.u64);
246 msg.u64 = 0x1ULL << VHOST_F_LOG_ALL |
247 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
248 p = (uint8_t *) &msg;
249 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
252 case VHOST_USER_SET_FEATURES:
253 g_assert_cmpint(msg.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
257 case VHOST_USER_GET_PROTOCOL_FEATURES:
258 /* send back features to qemu */
259 msg.flags |= VHOST_USER_REPLY_MASK;
260 msg.size = sizeof(m.u64);
261 msg.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
262 p = (uint8_t *) &msg;
263 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
266 case VHOST_USER_GET_VRING_BASE:
267 /* send back vring base to qemu */
268 msg.flags |= VHOST_USER_REPLY_MASK;
269 msg.size = sizeof(m.state);
271 p = (uint8_t *) &msg;
272 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
275 case VHOST_USER_SET_MEM_TABLE:
276 /* received the mem table */
277 memcpy(&s->memory, &msg.memory, sizeof(msg.memory));
278 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(s->fds));
280 /* signal the test that it can continue */
281 g_cond_signal(&s->data_cond);
284 case VHOST_USER_SET_VRING_KICK:
285 case VHOST_USER_SET_VRING_CALL:
287 qemu_chr_fe_get_msgfds(chr, &fd, 1);
289 * This is a non-blocking eventfd.
290 * The receive function forces it to be blocking,
291 * so revert it back to non-blocking.
293 qemu_set_nonblock(fd);
296 case VHOST_USER_SET_LOG_BASE:
297 if (s->log_fd != -1) {
301 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
302 msg.flags |= VHOST_USER_REPLY_MASK;
304 p = (uint8_t *) &msg;
305 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
307 g_cond_signal(&s->data_cond);
310 case VHOST_USER_RESET_DEVICE:
318 g_mutex_unlock(&s->data_mutex);
321 static const char *init_hugepagefs(const char *path)
326 if (access(path, R_OK | W_OK | X_OK)) {
327 g_test_message("access on path (%s): %s\n", path, strerror(errno));
332 ret = statfs(path, &fs);
333 } while (ret != 0 && errno == EINTR);
336 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
340 if (fs.f_type != HUGETLBFS_MAGIC) {
341 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
348 static TestServer *test_server_new(const gchar *name)
350 TestServer *server = g_new0(TestServer, 1);
353 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
355 chr_path = g_strdup_printf("unix:%s,server,nowait", server->socket_path);
356 server->chr_name = g_strdup_printf("chr-%s", name);
357 server->chr = qemu_chr_new(server->chr_name, chr_path, NULL);
360 qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, server);
362 g_mutex_init(&server->data_mutex);
363 g_cond_init(&server->data_cond);
370 #define GET_QEMU_CMD(s) \
371 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
372 (s)->socket_path, (s)->chr_name)
374 #define GET_QEMU_CMDE(s, mem, extra, ...) \
375 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
376 (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
378 static void test_server_free(TestServer *server)
382 qemu_chr_delete(server->chr);
384 for (i = 0; i < server->fds_num; i++) {
385 close(server->fds[i]);
388 if (server->log_fd != -1) {
389 close(server->log_fd);
392 unlink(server->socket_path);
393 g_free(server->socket_path);
396 g_free(server->chr_name);
400 static void wait_for_log_fd(TestServer *s)
404 g_mutex_lock(&s->data_mutex);
405 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
406 while (s->log_fd == -1) {
407 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
408 /* timeout has passed */
409 g_assert(s->log_fd != -1);
414 g_mutex_unlock(&s->data_mutex);
417 static void write_guest_mem(TestServer *s, uint32 seed)
425 /* iterate all regions */
426 for (i = 0; i < s->fds_num; i++) {
428 /* We'll write only the region statring at 0x0 */
429 if (s->memory.regions[i].guest_phys_addr != 0x0) {
433 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
435 size = s->memory.regions[i].memory_size +
436 s->memory.regions[i].mmap_offset;
438 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
439 MAP_SHARED, s->fds[i], 0);
441 g_assert(guest_mem != MAP_FAILED);
442 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
444 for (j = 0; j < 256; j++) {
445 guest_mem[j] = seed + j;
448 munmap(guest_mem, s->memory.regions[i].memory_size);
453 static guint64 get_log_size(TestServer *s)
455 guint64 log_size = 0;
458 for (i = 0; i < s->memory.nregions; ++i) {
459 VhostUserMemoryRegion *reg = &s->memory.regions[i];
460 guint64 last = range_get_last(reg->guest_phys_addr,
462 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
468 typedef struct TestMigrateSource {
475 test_migrate_source_check(GSource *source)
477 TestMigrateSource *t = (TestMigrateSource *)source;
478 gboolean overlap = t->src->fds_num > 0 && t->dest->fds_num > 0;
485 GSourceFuncs test_migrate_source_funcs = {
487 test_migrate_source_check,
492 static void test_migrate(void)
494 TestServer *s = test_server_new("src");
495 TestServer *dest = test_server_new("dest");
496 const char *uri = "tcp:127.0.0.1:1234";
497 QTestState *global = global_qtest, *from, *to;
504 cmd = GET_QEMU_CMDE(s, 2, "");
505 from = qtest_start(cmd);
509 size = get_log_size(s);
510 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
512 cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri);
513 to = qtest_init(cmd);
516 source = g_source_new(&test_migrate_source_funcs,
517 sizeof(TestMigrateSource));
518 ((TestMigrateSource *)source)->src = s;
519 ((TestMigrateSource *)source)->dest = dest;
520 g_source_attach(source, NULL);
522 /* slow down migration to have time to fiddle with log */
523 /* TODO: qtest could learn to break on some places */
524 rsp = qmp("{ 'execute': 'migrate_set_speed',"
525 "'arguments': { 'value': 10 } }");
526 g_assert(qdict_haskey(rsp, "return"));
529 cmd = g_strdup_printf("{ 'execute': 'migrate',"
530 "'arguments': { 'uri': '%s' } }",
534 g_assert(qdict_haskey(rsp, "return"));
539 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
540 g_assert(log != MAP_FAILED);
542 /* modify first page */
543 write_guest_mem(s, 0x42);
547 /* speed things up */
548 rsp = qmp("{ 'execute': 'migrate_set_speed',"
549 "'arguments': { 'value': 0 } }");
550 g_assert(qdict_haskey(rsp, "return"));
553 qmp_eventwait("STOP");
556 qmp_eventwait("RESUME");
558 read_guest_mem(dest);
560 g_source_destroy(source);
561 g_source_unref(source);
564 test_server_free(dest);
568 global_qtest = global;
571 int main(int argc, char **argv)
573 QTestState *s = NULL;
574 TestServer *server = NULL;
576 char *qemu_cmd = NULL;
578 char template[] = "/tmp/vhost-test-XXXXXX";
580 g_test_init(&argc, &argv, NULL);
582 module_call_init(MODULE_INIT_QOM);
583 qemu_add_opts(&qemu_chardev_opts);
585 tmpfs = mkdtemp(template);
587 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
591 hugefs = getenv("QTEST_HUGETLBFS_PATH");
593 root = init_hugepagefs(hugefs);
599 server = test_server_new("test");
601 /* run the main loop thread so the chardev may operate */
602 g_thread_new(NULL, thread_function, NULL);
604 qemu_cmd = GET_QEMU_CMD(server);
606 s = qtest_start(qemu_cmd);
609 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
610 qtest_add_func("/vhost-user/migrate", test_migrate);
619 test_server_free(server);
623 g_test_message("unable to rmdir: path (%s): %s\n",
624 tmpfs, strerror(errno));
626 g_assert_cmpint(ret, ==, 0);