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"
19 #include "libqos/libqos.h"
20 #include "libqos/pci-pc.h"
21 #include "libqos/virtio-pci.h"
23 #include <linux/vhost.h>
24 #include <linux/virtio_ids.h>
25 #include <linux/virtio_net.h>
28 /* GLIB version compatibility flags */
29 #if !GLIB_CHECK_VERSION(2, 26, 0)
30 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
33 #if GLIB_CHECK_VERSION(2, 28, 0)
34 #define HAVE_MONOTONIC_TIME
37 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
38 "mem-path=%s,share=on -numa node,memdev=mem"
39 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
40 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
41 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
43 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
44 QEMU_CMD_NETDEV QEMU_CMD_NET
46 #define HUGETLBFS_MAGIC 0x958458f6
48 /*********** FROM hw/virtio/vhost-user.c *************************************/
50 #define VHOST_MEMORY_MAX_NREGIONS 8
52 #define VHOST_USER_F_PROTOCOL_FEATURES 30
53 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
55 #define VHOST_LOG_PAGE 0x1000
57 typedef enum VhostUserRequest
{
59 VHOST_USER_GET_FEATURES
= 1,
60 VHOST_USER_SET_FEATURES
= 2,
61 VHOST_USER_SET_OWNER
= 3,
62 VHOST_USER_RESET_OWNER
= 4,
63 VHOST_USER_SET_MEM_TABLE
= 5,
64 VHOST_USER_SET_LOG_BASE
= 6,
65 VHOST_USER_SET_LOG_FD
= 7,
66 VHOST_USER_SET_VRING_NUM
= 8,
67 VHOST_USER_SET_VRING_ADDR
= 9,
68 VHOST_USER_SET_VRING_BASE
= 10,
69 VHOST_USER_GET_VRING_BASE
= 11,
70 VHOST_USER_SET_VRING_KICK
= 12,
71 VHOST_USER_SET_VRING_CALL
= 13,
72 VHOST_USER_SET_VRING_ERR
= 14,
73 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
74 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
75 VHOST_USER_SET_VRING_ENABLE
= 18,
79 typedef struct VhostUserMemoryRegion
{
80 uint64_t guest_phys_addr
;
82 uint64_t userspace_addr
;
84 } VhostUserMemoryRegion
;
86 typedef struct VhostUserMemory
{
89 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
92 typedef struct VhostUserLog
{
97 typedef struct VhostUserMsg
{
98 VhostUserRequest request
;
100 #define VHOST_USER_VERSION_MASK (0x3)
101 #define VHOST_USER_REPLY_MASK (0x1<<2)
103 uint32_t size
; /* the following payload size */
105 #define VHOST_USER_VRING_IDX_MASK (0xff)
106 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
108 struct vhost_vring_state state
;
109 struct vhost_vring_addr addr
;
110 VhostUserMemory memory
;
113 } QEMU_PACKED VhostUserMsg
;
115 static VhostUserMsg m
__attribute__ ((unused
));
116 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
120 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
122 /* The version of the protocol we support */
123 #define VHOST_USER_VERSION (0x1)
124 /*****************************************************************************/
126 typedef struct TestServer
{
130 CharDriverState
*chr
;
132 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
133 VhostUserMemory memory
;
134 CompatGMutex data_mutex
;
135 CompatGCond data_cond
;
140 static const char *tmpfs
;
141 static const char *root
;
143 static void init_virtio_dev(TestServer
*s
)
146 QVirtioPCIDevice
*dev
;
149 bus
= qpci_init_pc();
150 g_assert_nonnull(bus
);
152 dev
= qvirtio_pci_device_find(bus
, VIRTIO_ID_NET
);
153 g_assert_nonnull(dev
);
155 qvirtio_pci_device_enable(dev
);
156 qvirtio_reset(&qvirtio_pci
, &dev
->vdev
);
157 qvirtio_set_acknowledge(&qvirtio_pci
, &dev
->vdev
);
158 qvirtio_set_driver(&qvirtio_pci
, &dev
->vdev
);
160 features
= qvirtio_get_features(&qvirtio_pci
, &dev
->vdev
);
161 features
= features
& VIRTIO_NET_F_MAC
;
162 qvirtio_set_features(&qvirtio_pci
, &dev
->vdev
, features
);
164 qvirtio_set_driver_ok(&qvirtio_pci
, &dev
->vdev
);
167 static void wait_for_fds(TestServer
*s
)
171 g_mutex_lock(&s
->data_mutex
);
173 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
174 while (!s
->fds_num
) {
175 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
176 /* timeout has passed */
177 g_assert(s
->fds_num
);
182 /* check for sanity */
183 g_assert_cmpint(s
->fds_num
, >, 0);
184 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
186 g_mutex_unlock(&s
->data_mutex
);
189 static void read_guest_mem(const void *data
)
191 TestServer
*s
= (void *)data
;
198 g_mutex_lock(&s
->data_mutex
);
200 /* iterate all regions */
201 for (i
= 0; i
< s
->fds_num
; i
++) {
203 /* We'll check only the region statring at 0x0*/
204 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
208 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
210 size
= s
->memory
.regions
[i
].memory_size
+
211 s
->memory
.regions
[i
].mmap_offset
;
213 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
214 MAP_SHARED
, s
->fds
[i
], 0);
216 g_assert(guest_mem
!= MAP_FAILED
);
217 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
219 for (j
= 0; j
< 256; j
++) {
220 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
221 uint32_t b
= guest_mem
[j
];
223 g_assert_cmpint(a
, ==, b
);
226 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
229 g_mutex_unlock(&s
->data_mutex
);
232 static void *thread_function(void *data
)
234 GMainLoop
*loop
= data
;
235 g_main_loop_run(loop
);
239 static int chr_can_read(void *opaque
)
241 return VHOST_USER_HDR_SIZE
;
244 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
246 TestServer
*s
= opaque
;
247 CharDriverState
*chr
= s
->chr
;
249 uint8_t *p
= (uint8_t *) &msg
;
252 if (size
!= VHOST_USER_HDR_SIZE
) {
253 g_test_message("Wrong message size received %d\n", size
);
257 g_mutex_lock(&s
->data_mutex
);
258 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
261 p
+= VHOST_USER_HDR_SIZE
;
262 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
263 if (size
!= msg
.size
) {
264 g_test_message("Wrong message size received %d != %d\n",
270 switch (msg
.request
) {
271 case VHOST_USER_GET_FEATURES
:
272 /* send back features to qemu */
273 msg
.flags
|= VHOST_USER_REPLY_MASK
;
274 msg
.size
= sizeof(m
.payload
.u64
);
275 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
276 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
277 p
= (uint8_t *) &msg
;
278 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
281 case VHOST_USER_SET_FEATURES
:
282 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
286 case VHOST_USER_GET_PROTOCOL_FEATURES
:
287 /* send back features to qemu */
288 msg
.flags
|= VHOST_USER_REPLY_MASK
;
289 msg
.size
= sizeof(m
.payload
.u64
);
290 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
291 p
= (uint8_t *) &msg
;
292 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
295 case VHOST_USER_GET_VRING_BASE
:
296 /* send back vring base to qemu */
297 msg
.flags
|= VHOST_USER_REPLY_MASK
;
298 msg
.size
= sizeof(m
.payload
.state
);
299 msg
.payload
.state
.num
= 0;
300 p
= (uint8_t *) &msg
;
301 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
303 assert(msg
.payload
.state
.index
< 2);
304 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
307 case VHOST_USER_SET_MEM_TABLE
:
308 /* received the mem table */
309 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
310 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
, G_N_ELEMENTS(s
->fds
));
312 /* signal the test that it can continue */
313 g_cond_signal(&s
->data_cond
);
316 case VHOST_USER_SET_VRING_KICK
:
317 case VHOST_USER_SET_VRING_CALL
:
319 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
321 * This is a non-blocking eventfd.
322 * The receive function forces it to be blocking,
323 * so revert it back to non-blocking.
325 qemu_set_nonblock(fd
);
328 case VHOST_USER_SET_LOG_BASE
:
329 if (s
->log_fd
!= -1) {
333 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
334 msg
.flags
|= VHOST_USER_REPLY_MASK
;
336 p
= (uint8_t *) &msg
;
337 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
339 g_cond_signal(&s
->data_cond
);
342 case VHOST_USER_SET_VRING_BASE
:
343 assert(msg
.payload
.state
.index
< 2);
344 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
351 g_mutex_unlock(&s
->data_mutex
);
354 static const char *init_hugepagefs(const char *path
)
359 if (access(path
, R_OK
| W_OK
| X_OK
)) {
360 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
365 ret
= statfs(path
, &fs
);
366 } while (ret
!= 0 && errno
== EINTR
);
369 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
373 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
374 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
381 static TestServer
*test_server_new(const gchar
*name
)
383 TestServer
*server
= g_new0(TestServer
, 1);
385 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
386 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
387 server
->chr_name
= g_strdup_printf("chr-%s", name
);
389 g_mutex_init(&server
->data_mutex
);
390 g_cond_init(&server
->data_cond
);
397 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
401 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
402 server
->chr
= qemu_chr_new(server
->chr_name
, chr_path
, NULL
);
405 qemu_chr_add_handlers(server
->chr
, chr_can_read
, chr_read
, NULL
, server
);
408 static void test_server_listen(TestServer
*server
)
410 test_server_create_chr(server
, ",server,nowait");
413 static inline void test_server_connect(TestServer
*server
)
415 test_server_create_chr(server
, ",reconnect=1");
418 #define GET_QEMU_CMD(s) \
419 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
420 (s)->socket_path, "", (s)->chr_name)
422 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
423 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
424 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
426 static gboolean
_test_server_free(TestServer
*server
)
430 qemu_chr_delete(server
->chr
);
432 for (i
= 0; i
< server
->fds_num
; i
++) {
433 close(server
->fds
[i
]);
436 if (server
->log_fd
!= -1) {
437 close(server
->log_fd
);
440 unlink(server
->socket_path
);
441 g_free(server
->socket_path
);
443 unlink(server
->mig_path
);
444 g_free(server
->mig_path
);
446 g_free(server
->chr_name
);
452 static void test_server_free(TestServer
*server
)
454 g_idle_add((GSourceFunc
)_test_server_free
, server
);
457 static void wait_for_log_fd(TestServer
*s
)
461 g_mutex_lock(&s
->data_mutex
);
462 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
463 while (s
->log_fd
== -1) {
464 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
465 /* timeout has passed */
466 g_assert(s
->log_fd
!= -1);
471 g_mutex_unlock(&s
->data_mutex
);
474 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
482 /* iterate all regions */
483 for (i
= 0; i
< s
->fds_num
; i
++) {
485 /* We'll write only the region statring at 0x0 */
486 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
490 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
492 size
= s
->memory
.regions
[i
].memory_size
+
493 s
->memory
.regions
[i
].mmap_offset
;
495 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
496 MAP_SHARED
, s
->fds
[i
], 0);
498 g_assert(guest_mem
!= MAP_FAILED
);
499 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
501 for (j
= 0; j
< 256; j
++) {
502 guest_mem
[j
] = seed
+ j
;
505 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
510 static guint64
get_log_size(TestServer
*s
)
512 guint64 log_size
= 0;
515 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
516 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
517 guint64 last
= range_get_last(reg
->guest_phys_addr
,
519 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
525 typedef struct TestMigrateSource
{
532 test_migrate_source_check(GSource
*source
)
534 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
535 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
542 #if !GLIB_CHECK_VERSION(2,36,0)
543 /* this callback is unnecessary with glib >2.36, the default
544 * prepare for the source does the same */
546 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
553 GSourceFuncs test_migrate_source_funcs
= {
554 #if !GLIB_CHECK_VERSION(2,36,0)
555 .prepare
= test_migrate_source_prepare
,
557 .check
= test_migrate_source_check
,
560 static void test_migrate(void)
562 TestServer
*s
= test_server_new("src");
563 TestServer
*dest
= test_server_new("dest");
564 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
565 QTestState
*global
= global_qtest
, *from
, *to
;
572 test_server_listen(s
);
573 test_server_listen(dest
);
575 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
576 from
= qtest_start(cmd
);
581 size
= get_log_size(s
);
582 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
584 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
585 to
= qtest_init(cmd
);
588 source
= g_source_new(&test_migrate_source_funcs
,
589 sizeof(TestMigrateSource
));
590 ((TestMigrateSource
*)source
)->src
= s
;
591 ((TestMigrateSource
*)source
)->dest
= dest
;
592 g_source_attach(source
, NULL
);
594 /* slow down migration to have time to fiddle with log */
595 /* TODO: qtest could learn to break on some places */
596 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
597 "'arguments': { 'value': 10 } }");
598 g_assert(qdict_haskey(rsp
, "return"));
601 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
602 "'arguments': { 'uri': '%s' } }",
606 g_assert(qdict_haskey(rsp
, "return"));
611 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
612 g_assert(log
!= MAP_FAILED
);
614 /* modify first page */
615 write_guest_mem(s
, 0x42);
619 /* speed things up */
620 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
621 "'arguments': { 'value': 0 } }");
622 g_assert(qdict_haskey(rsp
, "return"));
625 qmp_eventwait("STOP");
628 qmp_eventwait("RESUME");
630 read_guest_mem(dest
);
632 g_source_destroy(source
);
633 g_source_unref(source
);
636 test_server_free(dest
);
641 global_qtest
= global
;
644 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
645 static void wait_for_rings_started(TestServer
*s
, size_t count
)
649 g_mutex_lock(&s
->data_mutex
);
650 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
651 while (ctpop64(s
->rings
) != count
) {
652 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
653 /* timeout has passed */
654 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
659 g_mutex_unlock(&s
->data_mutex
);
663 reconnect_cb(gpointer user_data
)
665 TestServer
*s
= user_data
;
667 qemu_chr_disconnect(s
->chr
);
673 connect_thread(gpointer data
)
675 TestServer
*s
= data
;
677 /* wait for qemu to start before first try, to avoid extra warnings */
678 g_usleep(G_USEC_PER_SEC
);
679 test_server_connect(s
);
684 static void test_reconnect_subprocess(void)
686 TestServer
*s
= test_server_new("reconnect");
689 g_thread_new("connect", connect_thread
, s
);
690 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
696 wait_for_rings_started(s
, 2);
701 g_idle_add(reconnect_cb
, s
);
703 wait_for_rings_started(s
, 2);
710 static void test_reconnect(void)
712 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
714 g_test_trap_subprocess(path
, 0, 0);
715 g_test_trap_assert_passed();
720 int main(int argc
, char **argv
)
722 QTestState
*s
= NULL
;
723 TestServer
*server
= NULL
;
725 char *qemu_cmd
= NULL
;
727 char template[] = "/tmp/vhost-test-XXXXXX";
731 g_test_init(&argc
, &argv
, NULL
);
733 module_call_init(MODULE_INIT_QOM
);
734 qemu_add_opts(&qemu_chardev_opts
);
736 tmpfs
= mkdtemp(template);
738 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
742 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
744 root
= init_hugepagefs(hugefs
);
750 server
= test_server_new("test");
751 test_server_listen(server
);
753 loop
= g_main_loop_new(NULL
, FALSE
);
754 /* run the main loop thread so the chardev may operate */
755 thread
= g_thread_new(NULL
, thread_function
, loop
);
757 qemu_cmd
= GET_QEMU_CMD(server
);
759 s
= qtest_start(qemu_cmd
);
761 init_virtio_dev(server
);
763 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
764 qtest_add_func("/vhost-user/migrate", test_migrate
);
765 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
766 qtest_add_func("/vhost-user/reconnect/subprocess",
767 test_reconnect_subprocess
);
768 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
778 test_server_free(server
);
780 /* finish the helper thread and dispatch pending sources */
781 g_main_loop_quit(loop
);
782 g_thread_join(thread
);
783 while (g_main_context_pending(NULL
)) {
784 g_main_context_iteration (NULL
, TRUE
);
786 g_main_loop_unref(loop
);
790 g_test_message("unable to rmdir: path (%s): %s\n",
791 tmpfs
, strerror(errno
));
793 g_assert_cmpint(ret
, ==, 0);