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,
73 VHOST_USER_SET_VRING_ENABLE
= 18,
77 typedef struct VhostUserMemoryRegion
{
78 uint64_t guest_phys_addr
;
80 uint64_t userspace_addr
;
82 } VhostUserMemoryRegion
;
84 typedef struct VhostUserMemory
{
87 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
90 typedef struct VhostUserLog
{
95 typedef struct VhostUserMsg
{
96 VhostUserRequest request
;
98 #define VHOST_USER_VERSION_MASK (0x3)
99 #define VHOST_USER_REPLY_MASK (0x1<<2)
101 uint32_t size
; /* the following payload size */
103 #define VHOST_USER_VRING_IDX_MASK (0xff)
104 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
106 struct vhost_vring_state state
;
107 struct vhost_vring_addr addr
;
108 VhostUserMemory memory
;
111 } QEMU_PACKED VhostUserMsg
;
113 static VhostUserMsg m
__attribute__ ((unused
));
114 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
118 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
120 /* The version of the protocol we support */
121 #define VHOST_USER_VERSION (0x1)
122 /*****************************************************************************/
124 typedef struct TestServer
{
127 CharDriverState
*chr
;
129 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
130 VhostUserMemory memory
;
136 #if !GLIB_CHECK_VERSION(2, 32, 0)
137 static gboolean
g_cond_wait_until(CompatGCond cond
, CompatGMutex mutex
,
140 gboolean ret
= FALSE
;
141 end_time
-= g_get_monotonic_time();
142 GTimeVal time
= { end_time
/ G_TIME_SPAN_SECOND
,
143 end_time
% G_TIME_SPAN_SECOND
};
144 ret
= g_cond_timed_wait(cond
, mutex
, &time
);
149 static const char *tmpfs
;
150 static const char *root
;
152 static void wait_for_fds(TestServer
*s
)
156 g_mutex_lock(&s
->data_mutex
);
158 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
159 while (!s
->fds_num
) {
160 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
161 /* timeout has passed */
162 g_assert(s
->fds_num
);
167 /* check for sanity */
168 g_assert_cmpint(s
->fds_num
, >, 0);
169 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
171 g_mutex_unlock(&s
->data_mutex
);
174 static void read_guest_mem(TestServer
*s
)
182 g_mutex_lock(&s
->data_mutex
);
184 /* iterate all regions */
185 for (i
= 0; i
< s
->fds_num
; i
++) {
187 /* We'll check only the region statring at 0x0*/
188 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
192 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
194 size
= s
->memory
.regions
[i
].memory_size
+
195 s
->memory
.regions
[i
].mmap_offset
;
197 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
198 MAP_SHARED
, s
->fds
[i
], 0);
200 g_assert(guest_mem
!= MAP_FAILED
);
201 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
203 for (j
= 0; j
< 256; j
++) {
204 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
205 uint32_t b
= guest_mem
[j
];
207 g_assert_cmpint(a
, ==, b
);
210 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
213 g_mutex_unlock(&s
->data_mutex
);
216 static void *thread_function(void *data
)
219 loop
= g_main_loop_new(NULL
, FALSE
);
220 g_main_loop_run(loop
);
224 static int chr_can_read(void *opaque
)
226 return VHOST_USER_HDR_SIZE
;
229 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
231 TestServer
*s
= opaque
;
232 CharDriverState
*chr
= s
->chr
;
234 uint8_t *p
= (uint8_t *) &msg
;
237 if (size
!= VHOST_USER_HDR_SIZE
) {
238 g_test_message("Wrong message size received %d\n", size
);
242 g_mutex_lock(&s
->data_mutex
);
243 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
246 p
+= VHOST_USER_HDR_SIZE
;
247 g_assert_cmpint(qemu_chr_fe_read_all(chr
, p
, msg
.size
), ==, msg
.size
);
250 switch (msg
.request
) {
251 case VHOST_USER_GET_FEATURES
:
252 /* send back features to qemu */
253 msg
.flags
|= VHOST_USER_REPLY_MASK
;
254 msg
.size
= sizeof(m
.payload
.u64
);
255 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
256 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
257 p
= (uint8_t *) &msg
;
258 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
261 case VHOST_USER_SET_FEATURES
:
262 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
266 case VHOST_USER_GET_PROTOCOL_FEATURES
:
267 /* send back features to qemu */
268 msg
.flags
|= VHOST_USER_REPLY_MASK
;
269 msg
.size
= sizeof(m
.payload
.u64
);
270 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
271 p
= (uint8_t *) &msg
;
272 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
275 case VHOST_USER_GET_VRING_BASE
:
276 /* send back vring base to qemu */
277 msg
.flags
|= VHOST_USER_REPLY_MASK
;
278 msg
.size
= sizeof(m
.payload
.state
);
279 msg
.payload
.state
.num
= 0;
280 p
= (uint8_t *) &msg
;
281 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
284 case VHOST_USER_SET_MEM_TABLE
:
285 /* received the mem table */
286 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
287 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
, G_N_ELEMENTS(s
->fds
));
289 /* signal the test that it can continue */
290 g_cond_signal(&s
->data_cond
);
293 case VHOST_USER_SET_VRING_KICK
:
294 case VHOST_USER_SET_VRING_CALL
:
296 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
298 * This is a non-blocking eventfd.
299 * The receive function forces it to be blocking,
300 * so revert it back to non-blocking.
302 qemu_set_nonblock(fd
);
305 case VHOST_USER_SET_LOG_BASE
:
306 if (s
->log_fd
!= -1) {
310 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
311 msg
.flags
|= VHOST_USER_REPLY_MASK
;
313 p
= (uint8_t *) &msg
;
314 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
316 g_cond_signal(&s
->data_cond
);
319 case VHOST_USER_SET_VRING_ENABLE
:
320 if (!msg
.payload
.state
.num
) {
329 g_mutex_unlock(&s
->data_mutex
);
332 static const char *init_hugepagefs(const char *path
)
337 if (access(path
, R_OK
| W_OK
| X_OK
)) {
338 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
343 ret
= statfs(path
, &fs
);
344 } while (ret
!= 0 && errno
== EINTR
);
347 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
351 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
352 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
359 static TestServer
*test_server_new(const gchar
*name
)
361 TestServer
*server
= g_new0(TestServer
, 1);
364 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
366 chr_path
= g_strdup_printf("unix:%s,server,nowait", server
->socket_path
);
367 server
->chr_name
= g_strdup_printf("chr-%s", name
);
368 server
->chr
= qemu_chr_new(server
->chr_name
, chr_path
, NULL
);
371 qemu_chr_add_handlers(server
->chr
, chr_can_read
, chr_read
, NULL
, server
);
373 g_mutex_init(&server
->data_mutex
);
374 g_cond_init(&server
->data_cond
);
381 #define GET_QEMU_CMD(s) \
382 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
383 (s)->socket_path, (s)->chr_name)
385 #define GET_QEMU_CMDE(s, mem, extra, ...) \
386 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
387 (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
389 static void test_server_free(TestServer
*server
)
393 qemu_chr_delete(server
->chr
);
395 for (i
= 0; i
< server
->fds_num
; i
++) {
396 close(server
->fds
[i
]);
399 if (server
->log_fd
!= -1) {
400 close(server
->log_fd
);
403 unlink(server
->socket_path
);
404 g_free(server
->socket_path
);
407 g_free(server
->chr_name
);
411 static void wait_for_log_fd(TestServer
*s
)
415 g_mutex_lock(&s
->data_mutex
);
416 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
417 while (s
->log_fd
== -1) {
418 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
419 /* timeout has passed */
420 g_assert(s
->log_fd
!= -1);
425 g_mutex_unlock(&s
->data_mutex
);
428 static void write_guest_mem(TestServer
*s
, uint32 seed
)
436 /* iterate all regions */
437 for (i
= 0; i
< s
->fds_num
; i
++) {
439 /* We'll write only the region statring at 0x0 */
440 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
444 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
446 size
= s
->memory
.regions
[i
].memory_size
+
447 s
->memory
.regions
[i
].mmap_offset
;
449 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
450 MAP_SHARED
, s
->fds
[i
], 0);
452 g_assert(guest_mem
!= MAP_FAILED
);
453 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
455 for (j
= 0; j
< 256; j
++) {
456 guest_mem
[j
] = seed
+ j
;
459 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
464 static guint64
get_log_size(TestServer
*s
)
466 guint64 log_size
= 0;
469 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
470 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
471 guint64 last
= range_get_last(reg
->guest_phys_addr
,
473 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
479 typedef struct TestMigrateSource
{
486 test_migrate_source_check(GSource
*source
)
488 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
489 gboolean overlap
= t
->src
->fds_num
> 0 && t
->dest
->fds_num
> 0;
496 GSourceFuncs test_migrate_source_funcs
= {
498 test_migrate_source_check
,
503 static void test_migrate(void)
505 TestServer
*s
= test_server_new("src");
506 TestServer
*dest
= test_server_new("dest");
507 const char *uri
= "tcp:127.0.0.1:1234";
508 QTestState
*global
= global_qtest
, *from
, *to
;
515 cmd
= GET_QEMU_CMDE(s
, 2, "");
516 from
= qtest_start(cmd
);
520 size
= get_log_size(s
);
521 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
523 cmd
= GET_QEMU_CMDE(dest
, 2, " -incoming %s", uri
);
524 to
= qtest_init(cmd
);
527 source
= g_source_new(&test_migrate_source_funcs
,
528 sizeof(TestMigrateSource
));
529 ((TestMigrateSource
*)source
)->src
= s
;
530 ((TestMigrateSource
*)source
)->dest
= dest
;
531 g_source_attach(source
, NULL
);
533 /* slow down migration to have time to fiddle with log */
534 /* TODO: qtest could learn to break on some places */
535 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
536 "'arguments': { 'value': 10 } }");
537 g_assert(qdict_haskey(rsp
, "return"));
540 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
541 "'arguments': { 'uri': '%s' } }",
545 g_assert(qdict_haskey(rsp
, "return"));
550 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
551 g_assert(log
!= MAP_FAILED
);
553 /* modify first page */
554 write_guest_mem(s
, 0x42);
558 /* speed things up */
559 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
560 "'arguments': { 'value': 0 } }");
561 g_assert(qdict_haskey(rsp
, "return"));
564 qmp_eventwait("STOP");
567 qmp_eventwait("RESUME");
569 read_guest_mem(dest
);
571 g_source_destroy(source
);
572 g_source_unref(source
);
575 test_server_free(dest
);
579 global_qtest
= global
;
582 int main(int argc
, char **argv
)
584 QTestState
*s
= NULL
;
585 TestServer
*server
= NULL
;
587 char *qemu_cmd
= NULL
;
589 char template[] = "/tmp/vhost-test-XXXXXX";
591 g_test_init(&argc
, &argv
, NULL
);
593 module_call_init(MODULE_INIT_QOM
);
594 qemu_add_opts(&qemu_chardev_opts
);
596 tmpfs
= mkdtemp(template);
598 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
602 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
604 root
= init_hugepagefs(hugefs
);
610 server
= test_server_new("test");
612 /* run the main loop thread so the chardev may operate */
613 g_thread_new(NULL
, thread_function
, NULL
);
615 qemu_cmd
= GET_QEMU_CMD(server
);
617 s
= qtest_start(qemu_cmd
);
620 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
621 qtest_add_func("/vhost-user/migrate", test_migrate
);
630 test_server_free(server
);
634 g_test_message("unable to rmdir: path (%s): %s\n",
635 tmpfs
, strerror(errno
));
637 g_assert_cmpint(ret
, ==, 0);