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.
11 #include "qemu/osdep.h"
14 #include "qemu/option.h"
15 #include "qemu/range.h"
16 #include "qemu/sockets.h"
17 #include "sysemu/char.h"
18 #include "sysemu/sysemu.h"
20 #include <linux/vhost.h>
23 /* GLIB version compatibility flags */
24 #if !GLIB_CHECK_VERSION(2, 26, 0)
25 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
28 #if GLIB_CHECK_VERSION(2, 28, 0)
29 #define HAVE_MONOTONIC_TIME
32 #define QEMU_CMD_ACCEL " -machine accel=tcg"
33 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
34 "mem-path=%s,share=on -numa node,memdev=mem"
35 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
36 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
37 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0,romfile=./pc-bios/pxe-virtio.rom"
39 #define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \
40 QEMU_CMD_NETDEV QEMU_CMD_NET
42 #define HUGETLBFS_MAGIC 0x958458f6
44 /*********** FROM hw/virtio/vhost-user.c *************************************/
46 #define VHOST_MEMORY_MAX_NREGIONS 8
48 #define VHOST_USER_F_PROTOCOL_FEATURES 30
49 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
51 #define VHOST_LOG_PAGE 0x1000
53 typedef enum VhostUserRequest
{
55 VHOST_USER_GET_FEATURES
= 1,
56 VHOST_USER_SET_FEATURES
= 2,
57 VHOST_USER_SET_OWNER
= 3,
58 VHOST_USER_RESET_OWNER
= 4,
59 VHOST_USER_SET_MEM_TABLE
= 5,
60 VHOST_USER_SET_LOG_BASE
= 6,
61 VHOST_USER_SET_LOG_FD
= 7,
62 VHOST_USER_SET_VRING_NUM
= 8,
63 VHOST_USER_SET_VRING_ADDR
= 9,
64 VHOST_USER_SET_VRING_BASE
= 10,
65 VHOST_USER_GET_VRING_BASE
= 11,
66 VHOST_USER_SET_VRING_KICK
= 12,
67 VHOST_USER_SET_VRING_CALL
= 13,
68 VHOST_USER_SET_VRING_ERR
= 14,
69 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
70 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
71 VHOST_USER_SET_VRING_ENABLE
= 18,
75 typedef struct VhostUserMemoryRegion
{
76 uint64_t guest_phys_addr
;
78 uint64_t userspace_addr
;
80 } VhostUserMemoryRegion
;
82 typedef struct VhostUserMemory
{
85 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
88 typedef struct VhostUserLog
{
93 typedef struct VhostUserMsg
{
94 VhostUserRequest request
;
96 #define VHOST_USER_VERSION_MASK (0x3)
97 #define VHOST_USER_REPLY_MASK (0x1<<2)
99 uint32_t size
; /* the following payload size */
101 #define VHOST_USER_VRING_IDX_MASK (0xff)
102 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
104 struct vhost_vring_state state
;
105 struct vhost_vring_addr addr
;
106 VhostUserMemory memory
;
109 } QEMU_PACKED VhostUserMsg
;
111 static VhostUserMsg m
__attribute__ ((unused
));
112 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
116 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
118 /* The version of the protocol we support */
119 #define VHOST_USER_VERSION (0x1)
120 /*****************************************************************************/
122 typedef struct TestServer
{
126 CharDriverState
*chr
;
128 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
129 VhostUserMemory memory
;
130 CompatGMutex data_mutex
;
131 CompatGCond data_cond
;
136 static const char *tmpfs
;
137 static const char *root
;
139 static void wait_for_fds(TestServer
*s
)
143 g_mutex_lock(&s
->data_mutex
);
145 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
146 while (!s
->fds_num
) {
147 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
148 /* timeout has passed */
149 g_assert(s
->fds_num
);
154 /* check for sanity */
155 g_assert_cmpint(s
->fds_num
, >, 0);
156 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
158 g_mutex_unlock(&s
->data_mutex
);
161 static void read_guest_mem(const void *data
)
163 TestServer
*s
= (void *)data
;
170 g_mutex_lock(&s
->data_mutex
);
172 /* iterate all regions */
173 for (i
= 0; i
< s
->fds_num
; i
++) {
175 /* We'll check only the region statring at 0x0*/
176 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
180 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
182 size
= s
->memory
.regions
[i
].memory_size
+
183 s
->memory
.regions
[i
].mmap_offset
;
185 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
186 MAP_SHARED
, s
->fds
[i
], 0);
188 g_assert(guest_mem
!= MAP_FAILED
);
189 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
191 for (j
= 0; j
< 256; j
++) {
192 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
193 uint32_t b
= guest_mem
[j
];
195 g_assert_cmpint(a
, ==, b
);
198 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
201 g_mutex_unlock(&s
->data_mutex
);
204 static void *thread_function(void *data
)
206 GMainLoop
*loop
= data
;
207 g_main_loop_run(loop
);
211 static int chr_can_read(void *opaque
)
213 return VHOST_USER_HDR_SIZE
;
216 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
218 TestServer
*s
= opaque
;
219 CharDriverState
*chr
= s
->chr
;
221 uint8_t *p
= (uint8_t *) &msg
;
224 if (size
!= VHOST_USER_HDR_SIZE
) {
225 g_test_message("Wrong message size received %d\n", size
);
229 g_mutex_lock(&s
->data_mutex
);
230 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
233 p
+= VHOST_USER_HDR_SIZE
;
234 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
235 if (size
!= msg
.size
) {
236 g_test_message("Wrong message size received %d != %d\n",
242 switch (msg
.request
) {
243 case VHOST_USER_GET_FEATURES
:
244 /* send back features to qemu */
245 msg
.flags
|= VHOST_USER_REPLY_MASK
;
246 msg
.size
= sizeof(m
.payload
.u64
);
247 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
248 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
249 p
= (uint8_t *) &msg
;
250 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
253 case VHOST_USER_SET_FEATURES
:
254 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
258 case VHOST_USER_GET_PROTOCOL_FEATURES
:
259 /* send back features to qemu */
260 msg
.flags
|= VHOST_USER_REPLY_MASK
;
261 msg
.size
= sizeof(m
.payload
.u64
);
262 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
263 p
= (uint8_t *) &msg
;
264 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
267 case VHOST_USER_GET_VRING_BASE
:
268 /* send back vring base to qemu */
269 msg
.flags
|= VHOST_USER_REPLY_MASK
;
270 msg
.size
= sizeof(m
.payload
.state
);
271 msg
.payload
.state
.num
= 0;
272 p
= (uint8_t *) &msg
;
273 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
275 assert(msg
.payload
.state
.index
< 2);
276 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
279 case VHOST_USER_SET_MEM_TABLE
:
280 /* received the mem table */
281 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
282 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
, G_N_ELEMENTS(s
->fds
));
284 /* signal the test that it can continue */
285 g_cond_signal(&s
->data_cond
);
288 case VHOST_USER_SET_VRING_KICK
:
289 case VHOST_USER_SET_VRING_CALL
:
291 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
293 * This is a non-blocking eventfd.
294 * The receive function forces it to be blocking,
295 * so revert it back to non-blocking.
297 qemu_set_nonblock(fd
);
300 case VHOST_USER_SET_LOG_BASE
:
301 if (s
->log_fd
!= -1) {
305 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
306 msg
.flags
|= VHOST_USER_REPLY_MASK
;
308 p
= (uint8_t *) &msg
;
309 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
311 g_cond_signal(&s
->data_cond
);
314 case VHOST_USER_SET_VRING_BASE
:
315 assert(msg
.payload
.state
.index
< 2);
316 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
323 g_mutex_unlock(&s
->data_mutex
);
326 static const char *init_hugepagefs(const char *path
)
331 if (access(path
, R_OK
| W_OK
| X_OK
)) {
332 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
337 ret
= statfs(path
, &fs
);
338 } while (ret
!= 0 && errno
== EINTR
);
341 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
345 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
346 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
353 static TestServer
*test_server_new(const gchar
*name
)
355 TestServer
*server
= g_new0(TestServer
, 1);
357 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
358 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
359 server
->chr_name
= g_strdup_printf("chr-%s", name
);
361 g_mutex_init(&server
->data_mutex
);
362 g_cond_init(&server
->data_cond
);
369 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
373 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
374 server
->chr
= qemu_chr_new(server
->chr_name
, chr_path
, NULL
);
377 qemu_chr_add_handlers(server
->chr
, chr_can_read
, chr_read
, NULL
, server
);
380 static void test_server_listen(TestServer
*server
)
382 test_server_create_chr(server
, ",server,nowait");
385 static inline void test_server_connect(TestServer
*server
)
387 test_server_create_chr(server
, ",reconnect=1");
390 #define GET_QEMU_CMD(s) \
391 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
392 (s)->socket_path, "", (s)->chr_name)
394 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
395 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
396 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
398 static gboolean
_test_server_free(TestServer
*server
)
402 qemu_chr_delete(server
->chr
);
404 for (i
= 0; i
< server
->fds_num
; i
++) {
405 close(server
->fds
[i
]);
408 if (server
->log_fd
!= -1) {
409 close(server
->log_fd
);
412 unlink(server
->socket_path
);
413 g_free(server
->socket_path
);
415 unlink(server
->mig_path
);
416 g_free(server
->mig_path
);
418 g_free(server
->chr_name
);
424 static void test_server_free(TestServer
*server
)
426 g_idle_add((GSourceFunc
)_test_server_free
, server
);
429 static void wait_for_log_fd(TestServer
*s
)
433 g_mutex_lock(&s
->data_mutex
);
434 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
435 while (s
->log_fd
== -1) {
436 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
437 /* timeout has passed */
438 g_assert(s
->log_fd
!= -1);
443 g_mutex_unlock(&s
->data_mutex
);
446 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
454 /* iterate all regions */
455 for (i
= 0; i
< s
->fds_num
; i
++) {
457 /* We'll write only the region statring at 0x0 */
458 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
462 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
464 size
= s
->memory
.regions
[i
].memory_size
+
465 s
->memory
.regions
[i
].mmap_offset
;
467 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
468 MAP_SHARED
, s
->fds
[i
], 0);
470 g_assert(guest_mem
!= MAP_FAILED
);
471 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
473 for (j
= 0; j
< 256; j
++) {
474 guest_mem
[j
] = seed
+ j
;
477 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
482 static guint64
get_log_size(TestServer
*s
)
484 guint64 log_size
= 0;
487 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
488 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
489 guint64 last
= range_get_last(reg
->guest_phys_addr
,
491 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
497 typedef struct TestMigrateSource
{
504 test_migrate_source_check(GSource
*source
)
506 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
507 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
514 #if !GLIB_CHECK_VERSION(2,36,0)
515 /* this callback is unnecessary with glib >2.36, the default
516 * prepare for the source does the same */
518 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
525 GSourceFuncs test_migrate_source_funcs
= {
526 #if !GLIB_CHECK_VERSION(2,36,0)
527 .prepare
= test_migrate_source_prepare
,
529 .check
= test_migrate_source_check
,
532 static void test_migrate(void)
534 TestServer
*s
= test_server_new("src");
535 TestServer
*dest
= test_server_new("dest");
536 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
537 QTestState
*global
= global_qtest
, *from
, *to
;
544 test_server_listen(s
);
545 test_server_listen(dest
);
547 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
548 from
= qtest_start(cmd
);
552 size
= get_log_size(s
);
553 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
555 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
556 to
= qtest_init(cmd
);
559 source
= g_source_new(&test_migrate_source_funcs
,
560 sizeof(TestMigrateSource
));
561 ((TestMigrateSource
*)source
)->src
= s
;
562 ((TestMigrateSource
*)source
)->dest
= dest
;
563 g_source_attach(source
, NULL
);
565 /* slow down migration to have time to fiddle with log */
566 /* TODO: qtest could learn to break on some places */
567 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
568 "'arguments': { 'value': 10 } }");
569 g_assert(qdict_haskey(rsp
, "return"));
572 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
573 "'arguments': { 'uri': '%s' } }",
577 g_assert(qdict_haskey(rsp
, "return"));
582 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
583 g_assert(log
!= MAP_FAILED
);
585 /* modify first page */
586 write_guest_mem(s
, 0x42);
590 /* speed things up */
591 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
592 "'arguments': { 'value': 0 } }");
593 g_assert(qdict_haskey(rsp
, "return"));
596 qmp_eventwait("STOP");
599 qmp_eventwait("RESUME");
601 read_guest_mem(dest
);
603 g_source_destroy(source
);
604 g_source_unref(source
);
607 test_server_free(dest
);
612 global_qtest
= global
;
615 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
616 static void wait_for_rings_started(TestServer
*s
, size_t count
)
620 g_mutex_lock(&s
->data_mutex
);
621 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
622 while (ctpop64(s
->rings
) != count
) {
623 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
624 /* timeout has passed */
625 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
630 g_mutex_unlock(&s
->data_mutex
);
634 reconnect_cb(gpointer user_data
)
636 TestServer
*s
= user_data
;
638 qemu_chr_disconnect(s
->chr
);
644 connect_thread(gpointer data
)
646 TestServer
*s
= data
;
648 /* wait for qemu to start before first try, to avoid extra warnings */
649 g_usleep(G_USEC_PER_SEC
);
650 test_server_connect(s
);
655 static void test_reconnect_subprocess(void)
657 TestServer
*s
= test_server_new("reconnect");
660 g_thread_new("connect", connect_thread
, s
);
661 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
666 wait_for_rings_started(s
, 2);
671 g_idle_add(reconnect_cb
, s
);
673 wait_for_rings_started(s
, 2);
680 static void test_reconnect(void)
682 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
684 g_test_trap_subprocess(path
, 0, 0);
685 g_test_trap_assert_passed();
690 int main(int argc
, char **argv
)
692 QTestState
*s
= NULL
;
693 TestServer
*server
= NULL
;
695 char *qemu_cmd
= NULL
;
697 char template[] = "/tmp/vhost-test-XXXXXX";
701 g_test_init(&argc
, &argv
, NULL
);
703 module_call_init(MODULE_INIT_QOM
);
704 qemu_add_opts(&qemu_chardev_opts
);
706 tmpfs
= mkdtemp(template);
708 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
712 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
714 root
= init_hugepagefs(hugefs
);
720 server
= test_server_new("test");
721 test_server_listen(server
);
723 loop
= g_main_loop_new(NULL
, FALSE
);
724 /* run the main loop thread so the chardev may operate */
725 thread
= g_thread_new(NULL
, thread_function
, loop
);
727 qemu_cmd
= GET_QEMU_CMD(server
);
729 s
= qtest_start(qemu_cmd
);
732 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
733 qtest_add_func("/vhost-user/migrate", test_migrate
);
734 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
735 qtest_add_func("/vhost-user/reconnect/subprocess",
736 test_reconnect_subprocess
);
737 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
747 test_server_free(server
);
749 /* finish the helper thread and dispatch pending sources */
750 g_main_loop_quit(loop
);
751 g_thread_join(thread
);
752 while (g_main_context_pending(NULL
)) {
753 g_main_context_iteration (NULL
, TRUE
);
755 g_main_loop_unref(loop
);
759 g_test_message("unable to rmdir: path (%s): %s\n",
760 tmpfs
, strerror(errno
));
762 g_assert_cmpint(ret
, ==, 0);