atapi: Prioritize unknown cmd error over BCL error
[qemu/kevin.git] / tests / vhost-user-test.c
blob01cfc7e25da799f786da4b775ed50e4528bc403f
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_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 *chr_name;
126 CharDriverState *chr;
127 int fds_num;
128 int fds[VHOST_MEMORY_MAX_NREGIONS];
129 VhostUserMemory memory;
130 GMutex data_mutex;
131 GCond data_cond;
132 int log_fd;
133 } TestServer;
135 #if !GLIB_CHECK_VERSION(2, 32, 0)
136 static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex,
137 gint64 end_time)
139 gboolean ret = FALSE;
140 end_time -= g_get_monotonic_time();
141 GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
142 end_time % G_TIME_SPAN_SECOND };
143 ret = g_cond_timed_wait(cond, mutex, &time);
144 return ret;
146 #endif
148 static const char *tmpfs;
149 static const char *root;
151 static void wait_for_fds(TestServer *s)
153 gint64 end_time;
155 g_mutex_lock(&s->data_mutex);
157 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
158 while (!s->fds_num) {
159 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
160 /* timeout has passed */
161 g_assert(s->fds_num);
162 break;
166 /* check for sanity */
167 g_assert_cmpint(s->fds_num, >, 0);
168 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
170 g_mutex_unlock(&s->data_mutex);
173 static void read_guest_mem(TestServer *s)
175 uint32_t *guest_mem;
176 int i, j;
177 size_t size;
179 wait_for_fds(s);
181 g_mutex_lock(&s->data_mutex);
183 /* iterate all regions */
184 for (i = 0; i < s->fds_num; i++) {
186 /* We'll check only the region statring at 0x0*/
187 if (s->memory.regions[i].guest_phys_addr != 0x0) {
188 continue;
191 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
193 size = s->memory.regions[i].memory_size +
194 s->memory.regions[i].mmap_offset;
196 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
197 MAP_SHARED, s->fds[i], 0);
199 g_assert(guest_mem != MAP_FAILED);
200 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
202 for (j = 0; j < 256; j++) {
203 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
204 uint32_t b = guest_mem[j];
206 g_assert_cmpint(a, ==, b);
209 munmap(guest_mem, s->memory.regions[i].memory_size);
212 g_mutex_unlock(&s->data_mutex);
215 static void *thread_function(void *data)
217 GMainLoop *loop;
218 loop = g_main_loop_new(NULL, FALSE);
219 g_main_loop_run(loop);
220 return NULL;
223 static int chr_can_read(void *opaque)
225 return VHOST_USER_HDR_SIZE;
228 static void chr_read(void *opaque, const uint8_t *buf, int size)
230 TestServer *s = opaque;
231 CharDriverState *chr = s->chr;
232 VhostUserMsg msg;
233 uint8_t *p = (uint8_t *) &msg;
234 int fd;
236 if (size != VHOST_USER_HDR_SIZE) {
237 g_test_message("Wrong message size received %d\n", size);
238 return;
241 g_mutex_lock(&s->data_mutex);
242 memcpy(p, buf, VHOST_USER_HDR_SIZE);
244 if (msg.size) {
245 p += VHOST_USER_HDR_SIZE;
246 g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), ==, msg.size);
249 switch (msg.request) {
250 case VHOST_USER_GET_FEATURES:
251 /* send back features to qemu */
252 msg.flags |= VHOST_USER_REPLY_MASK;
253 msg.size = sizeof(m.payload.u64);
254 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
255 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
256 p = (uint8_t *) &msg;
257 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
258 break;
260 case VHOST_USER_SET_FEATURES:
261 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
262 !=, 0ULL);
263 break;
265 case VHOST_USER_GET_PROTOCOL_FEATURES:
266 /* send back features to qemu */
267 msg.flags |= VHOST_USER_REPLY_MASK;
268 msg.size = sizeof(m.payload.u64);
269 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
270 p = (uint8_t *) &msg;
271 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
272 break;
274 case VHOST_USER_GET_VRING_BASE:
275 /* send back vring base to qemu */
276 msg.flags |= VHOST_USER_REPLY_MASK;
277 msg.size = sizeof(m.payload.state);
278 msg.payload.state.num = 0;
279 p = (uint8_t *) &msg;
280 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
281 break;
283 case VHOST_USER_SET_MEM_TABLE:
284 /* received the mem table */
285 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
286 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(s->fds));
288 /* signal the test that it can continue */
289 g_cond_signal(&s->data_cond);
290 break;
292 case VHOST_USER_SET_VRING_KICK:
293 case VHOST_USER_SET_VRING_CALL:
294 /* consume the fd */
295 qemu_chr_fe_get_msgfds(chr, &fd, 1);
297 * This is a non-blocking eventfd.
298 * The receive function forces it to be blocking,
299 * so revert it back to non-blocking.
301 qemu_set_nonblock(fd);
302 break;
304 case VHOST_USER_SET_LOG_BASE:
305 if (s->log_fd != -1) {
306 close(s->log_fd);
307 s->log_fd = -1;
309 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
310 msg.flags |= VHOST_USER_REPLY_MASK;
311 msg.size = 0;
312 p = (uint8_t *) &msg;
313 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
315 g_cond_signal(&s->data_cond);
316 break;
318 case VHOST_USER_RESET_OWNER:
319 s->fds_num = 0;
320 break;
322 default:
323 break;
326 g_mutex_unlock(&s->data_mutex);
329 static const char *init_hugepagefs(const char *path)
331 struct statfs fs;
332 int ret;
334 if (access(path, R_OK | W_OK | X_OK)) {
335 g_test_message("access on path (%s): %s\n", path, strerror(errno));
336 return NULL;
339 do {
340 ret = statfs(path, &fs);
341 } while (ret != 0 && errno == EINTR);
343 if (ret != 0) {
344 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
345 return NULL;
348 if (fs.f_type != HUGETLBFS_MAGIC) {
349 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
350 return NULL;
353 return path;
356 static TestServer *test_server_new(const gchar *name)
358 TestServer *server = g_new0(TestServer, 1);
359 gchar *chr_path;
361 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
363 chr_path = g_strdup_printf("unix:%s,server,nowait", server->socket_path);
364 server->chr_name = g_strdup_printf("chr-%s", name);
365 server->chr = qemu_chr_new(server->chr_name, chr_path, NULL);
366 g_free(chr_path);
368 qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, server);
370 g_mutex_init(&server->data_mutex);
371 g_cond_init(&server->data_cond);
373 server->log_fd = -1;
375 return server;
378 #define GET_QEMU_CMD(s) \
379 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
380 (s)->socket_path, (s)->chr_name)
382 #define GET_QEMU_CMDE(s, mem, extra, ...) \
383 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
384 (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
386 static void test_server_free(TestServer *server)
388 int i;
390 qemu_chr_delete(server->chr);
392 for (i = 0; i < server->fds_num; i++) {
393 close(server->fds[i]);
396 if (server->log_fd != -1) {
397 close(server->log_fd);
400 unlink(server->socket_path);
401 g_free(server->socket_path);
404 g_free(server->chr_name);
405 g_free(server);
408 static void wait_for_log_fd(TestServer *s)
410 gint64 end_time;
412 g_mutex_lock(&s->data_mutex);
413 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
414 while (s->log_fd == -1) {
415 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
416 /* timeout has passed */
417 g_assert(s->log_fd != -1);
418 break;
422 g_mutex_unlock(&s->data_mutex);
425 static void write_guest_mem(TestServer *s, uint32 seed)
427 uint32_t *guest_mem;
428 int i, j;
429 size_t size;
431 wait_for_fds(s);
433 /* iterate all regions */
434 for (i = 0; i < s->fds_num; i++) {
436 /* We'll write only the region statring at 0x0 */
437 if (s->memory.regions[i].guest_phys_addr != 0x0) {
438 continue;
441 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
443 size = s->memory.regions[i].memory_size +
444 s->memory.regions[i].mmap_offset;
446 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
447 MAP_SHARED, s->fds[i], 0);
449 g_assert(guest_mem != MAP_FAILED);
450 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
452 for (j = 0; j < 256; j++) {
453 guest_mem[j] = seed + j;
456 munmap(guest_mem, s->memory.regions[i].memory_size);
457 break;
461 static guint64 get_log_size(TestServer *s)
463 guint64 log_size = 0;
464 int i;
466 for (i = 0; i < s->memory.nregions; ++i) {
467 VhostUserMemoryRegion *reg = &s->memory.regions[i];
468 guint64 last = range_get_last(reg->guest_phys_addr,
469 reg->memory_size);
470 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
473 return log_size;
476 typedef struct TestMigrateSource {
477 GSource source;
478 TestServer *src;
479 TestServer *dest;
480 } TestMigrateSource;
482 static gboolean
483 test_migrate_source_check(GSource *source)
485 TestMigrateSource *t = (TestMigrateSource *)source;
486 gboolean overlap = t->src->fds_num > 0 && t->dest->fds_num > 0;
488 g_assert(!overlap);
490 return FALSE;
493 GSourceFuncs test_migrate_source_funcs = {
494 NULL,
495 test_migrate_source_check,
496 NULL,
497 NULL
500 static void test_migrate(void)
502 TestServer *s = test_server_new("src");
503 TestServer *dest = test_server_new("dest");
504 const char *uri = "tcp:127.0.0.1:1234";
505 QTestState *global = global_qtest, *from, *to;
506 GSource *source;
507 gchar *cmd;
508 QDict *rsp;
509 guint8 *log;
510 guint64 size;
512 cmd = GET_QEMU_CMDE(s, 2, "");
513 from = qtest_start(cmd);
514 g_free(cmd);
516 wait_for_fds(s);
517 size = get_log_size(s);
518 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
520 cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri);
521 to = qtest_init(cmd);
522 g_free(cmd);
524 source = g_source_new(&test_migrate_source_funcs,
525 sizeof(TestMigrateSource));
526 ((TestMigrateSource *)source)->src = s;
527 ((TestMigrateSource *)source)->dest = dest;
528 g_source_attach(source, NULL);
530 /* slow down migration to have time to fiddle with log */
531 /* TODO: qtest could learn to break on some places */
532 rsp = qmp("{ 'execute': 'migrate_set_speed',"
533 "'arguments': { 'value': 10 } }");
534 g_assert(qdict_haskey(rsp, "return"));
535 QDECREF(rsp);
537 cmd = g_strdup_printf("{ 'execute': 'migrate',"
538 "'arguments': { 'uri': '%s' } }",
539 uri);
540 rsp = qmp(cmd);
541 g_free(cmd);
542 g_assert(qdict_haskey(rsp, "return"));
543 QDECREF(rsp);
545 wait_for_log_fd(s);
547 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
548 g_assert(log != MAP_FAILED);
550 /* modify first page */
551 write_guest_mem(s, 0x42);
552 log[0] = 1;
553 munmap(log, size);
555 /* speed things up */
556 rsp = qmp("{ 'execute': 'migrate_set_speed',"
557 "'arguments': { 'value': 0 } }");
558 g_assert(qdict_haskey(rsp, "return"));
559 QDECREF(rsp);
561 qmp_eventwait("STOP");
563 global_qtest = to;
564 qmp_eventwait("RESUME");
566 read_guest_mem(dest);
568 g_source_destroy(source);
569 g_source_unref(source);
571 qtest_quit(to);
572 test_server_free(dest);
573 qtest_quit(from);
574 test_server_free(s);
576 global_qtest = global;
579 int main(int argc, char **argv)
581 QTestState *s = NULL;
582 TestServer *server = NULL;
583 const char *hugefs;
584 char *qemu_cmd = NULL;
585 int ret;
586 char template[] = "/tmp/vhost-test-XXXXXX";
588 g_test_init(&argc, &argv, NULL);
590 module_call_init(MODULE_INIT_QOM);
591 qemu_add_opts(&qemu_chardev_opts);
593 tmpfs = mkdtemp(template);
594 if (!tmpfs) {
595 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
597 g_assert(tmpfs);
599 hugefs = getenv("QTEST_HUGETLBFS_PATH");
600 if (hugefs) {
601 root = init_hugepagefs(hugefs);
602 g_assert(root);
603 } else {
604 root = tmpfs;
607 server = test_server_new("test");
609 /* run the main loop thread so the chardev may operate */
610 g_thread_new(NULL, thread_function, NULL);
612 qemu_cmd = GET_QEMU_CMD(server);
614 s = qtest_start(qemu_cmd);
615 g_free(qemu_cmd);
617 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
618 qtest_add_func("/vhost-user/migrate", test_migrate);
620 ret = g_test_run();
622 if (s) {
623 qtest_quit(s);
626 /* cleanup */
627 test_server_free(server);
629 ret = rmdir(tmpfs);
630 if (ret != 0) {
631 g_test_message("unable to rmdir: path (%s): %s\n",
632 tmpfs, strerror(errno));
634 g_assert_cmpint(ret, ==, 0);
636 return ret;