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_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,
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 VhostUserLog
{
94 typedef struct VhostUserMsg
{
95 VhostUserRequest request
;
97 #define VHOST_USER_VERSION_MASK (0x3)
98 #define VHOST_USER_REPLY_MASK (0x1<<2)
100 uint32_t size
; /* the following payload size */
102 #define VHOST_USER_VRING_IDX_MASK (0xff)
103 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
105 struct vhost_vring_state state
;
106 struct vhost_vring_addr addr
;
107 VhostUserMemory memory
;
110 } QEMU_PACKED VhostUserMsg
;
112 static VhostUserMsg m
__attribute__ ((unused
));
113 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
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
{
126 CharDriverState
*chr
;
128 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
129 VhostUserMemory memory
;
135 #if !GLIB_CHECK_VERSION(2, 32, 0)
136 static gboolean
g_cond_wait_until(CompatGCond cond
, CompatGMutex mutex
,
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
);
148 static const char *tmpfs
;
149 static const char *root
;
151 static void wait_for_fds(TestServer
*s
)
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
);
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
)
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) {
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
)
218 loop
= g_main_loop_new(NULL
, FALSE
);
219 g_main_loop_run(loop
);
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
;
233 uint8_t *p
= (uint8_t *) &msg
;
236 if (size
!= VHOST_USER_HDR_SIZE
) {
237 g_test_message("Wrong message size received %d\n", size
);
241 g_mutex_lock(&s
->data_mutex
);
242 memcpy(p
, buf
, VHOST_USER_HDR_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
);
260 case VHOST_USER_SET_FEATURES
:
261 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
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
);
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
);
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
);
292 case VHOST_USER_SET_VRING_KICK
:
293 case VHOST_USER_SET_VRING_CALL
:
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
);
304 case VHOST_USER_SET_LOG_BASE
:
305 if (s
->log_fd
!= -1) {
309 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
310 msg
.flags
|= VHOST_USER_REPLY_MASK
;
312 p
= (uint8_t *) &msg
;
313 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
315 g_cond_signal(&s
->data_cond
);
318 case VHOST_USER_RESET_OWNER
:
326 g_mutex_unlock(&s
->data_mutex
);
329 static const char *init_hugepagefs(const char *path
)
334 if (access(path
, R_OK
| W_OK
| X_OK
)) {
335 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
340 ret
= statfs(path
, &fs
);
341 } while (ret
!= 0 && errno
== EINTR
);
344 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
348 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
349 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
356 static TestServer
*test_server_new(const gchar
*name
)
358 TestServer
*server
= g_new0(TestServer
, 1);
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
);
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
);
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
)
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
);
408 static void wait_for_log_fd(TestServer
*s
)
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);
422 g_mutex_unlock(&s
->data_mutex
);
425 static void write_guest_mem(TestServer
*s
, uint32 seed
)
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) {
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
);
461 static guint64
get_log_size(TestServer
*s
)
463 guint64 log_size
= 0;
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
,
470 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
476 typedef struct TestMigrateSource
{
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;
493 GSourceFuncs test_migrate_source_funcs
= {
495 test_migrate_source_check
,
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
;
512 cmd
= GET_QEMU_CMDE(s
, 2, "");
513 from
= qtest_start(cmd
);
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
);
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"));
537 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
538 "'arguments': { 'uri': '%s' } }",
542 g_assert(qdict_haskey(rsp
, "return"));
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);
555 /* speed things up */
556 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
557 "'arguments': { 'value': 0 } }");
558 g_assert(qdict_haskey(rsp
, "return"));
561 qmp_eventwait("STOP");
564 qmp_eventwait("RESUME");
566 read_guest_mem(dest
);
568 g_source_destroy(source
);
569 g_source_unref(source
);
572 test_server_free(dest
);
576 global_qtest
= global
;
579 int main(int argc
, char **argv
)
581 QTestState
*s
= NULL
;
582 TestServer
*server
= NULL
;
584 char *qemu_cmd
= NULL
;
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);
595 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
599 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
601 root
= init_hugepagefs(hugefs
);
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
);
617 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
618 qtest_add_func("/vhost-user/migrate", test_migrate
);
627 test_server_free(server
);
631 g_test_message("unable to rmdir: path (%s): %s\n",
632 tmpfs
, strerror(errno
));
634 g_assert_cmpint(ret
, ==, 0);