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,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
{
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,
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
{
127 CharDriverState
*chr
;
129 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
130 VhostUserMemory memory
;
137 #if !GLIB_CHECK_VERSION(2, 32, 0)
138 static gboolean
g_cond_wait_until(CompatGCond cond
, CompatGMutex mutex
,
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
);
150 static const char *tmpfs
;
151 static const char *root
;
153 static void wait_for_fds(TestServer
*s
)
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
);
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
;
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) {
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
);
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
;
235 uint8_t *p
= (uint8_t *) &msg
;
238 if (size
!= VHOST_USER_HDR_SIZE
) {
239 g_test_message("Wrong message size received %d\n", size
);
243 g_mutex_lock(&s
->data_mutex
);
244 memcpy(p
, buf
, VHOST_USER_HDR_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
);
262 case VHOST_USER_SET_FEATURES
:
263 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
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
);
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
);
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
);
297 case VHOST_USER_SET_VRING_KICK
:
298 case VHOST_USER_SET_VRING_CALL
:
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
);
309 case VHOST_USER_SET_LOG_BASE
:
310 if (s
->log_fd
!= -1) {
314 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
315 msg
.flags
|= VHOST_USER_REPLY_MASK
;
317 p
= (uint8_t *) &msg
;
318 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
320 g_cond_signal(&s
->data_cond
);
323 case VHOST_USER_SET_VRING_BASE
:
324 assert(msg
.payload
.state
.index
< 2);
325 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
332 g_mutex_unlock(&s
->data_mutex
);
335 static const char *init_hugepagefs(const char *path
)
340 if (access(path
, R_OK
| W_OK
| X_OK
)) {
341 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
346 ret
= statfs(path
, &fs
);
347 } while (ret
!= 0 && errno
== EINTR
);
350 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
354 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
355 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
362 static TestServer
*test_server_new(const gchar
*name
)
364 TestServer
*server
= g_new0(TestServer
, 1);
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
);
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
);
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
)
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
);
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
)
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);
438 g_mutex_unlock(&s
->data_mutex
);
441 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
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) {
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
);
477 static guint64
get_log_size(TestServer
*s
)
479 guint64 log_size
= 0;
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
,
486 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
492 typedef struct TestMigrateSource
{
499 test_migrate_source_check(GSource
*source
)
501 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
502 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
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 */
513 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
520 GSourceFuncs test_migrate_source_funcs
= {
521 #if !GLIB_CHECK_VERSION(2,36,0)
522 .prepare
= test_migrate_source_prepare
,
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
;
539 cmd
= GET_QEMU_CMDE(s
, 2, "");
540 from
= qtest_start(cmd
);
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
);
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"));
564 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
565 "'arguments': { 'uri': '%s' } }",
569 g_assert(qdict_haskey(rsp
, "return"));
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);
582 /* speed things up */
583 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
584 "'arguments': { 'value': 0 } }");
585 g_assert(qdict_haskey(rsp
, "return"));
588 qmp_eventwait("STOP");
591 qmp_eventwait("RESUME");
593 read_guest_mem(dest
);
595 g_source_destroy(source
);
596 g_source_unref(source
);
599 test_server_free(dest
);
604 global_qtest
= global
;
607 int main(int argc
, char **argv
)
609 QTestState
*s
= NULL
;
610 TestServer
*server
= NULL
;
612 char *qemu_cmd
= NULL
;
614 char template[] = "/tmp/vhost-test-XXXXXX";
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);
625 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
629 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
631 root
= init_hugepagefs(hugefs
);
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
);
648 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
649 qtest_add_func("/vhost-user/migrate", test_migrate
);
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
);
670 g_test_message("unable to rmdir: path (%s): %s\n",
671 tmpfs
, strerror(errno
));
673 g_assert_cmpint(ret
, ==, 0);