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"
13 #include "libqtest-single.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qemu/config-file.h"
17 #include "qemu/option.h"
18 #include "qemu/range.h"
19 #include "qemu/sockets.h"
20 #include "chardev/char-fe.h"
21 #include "qemu/memfd.h"
22 #include "qemu/module.h"
23 #include "sysemu/sysemu.h"
24 #include "libqos/libqos.h"
25 #include "libqos/pci-pc.h"
26 #include "libqos/virtio-pci.h"
28 #include "libqos/malloc-pc.h"
29 #include "libqos/qgraph_internal.h"
30 #include "hw/virtio/virtio-net.h"
32 #include "standard-headers/linux/vhost_types.h"
33 #include "standard-headers/linux/virtio_ids.h"
34 #include "standard-headers/linux/virtio_net.h"
35 #include "standard-headers/linux/virtio_gpio.h"
36 #include "standard-headers/linux/virtio_scmi.h"
43 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
44 "mem-path=%s,share=on -numa node,memdev=mem"
45 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
46 " -numa node,memdev=mem"
47 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
48 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce=on"
50 #define HUGETLBFS_MAGIC 0x958458f6
52 /*********** FROM hw/virtio/vhost-user.c *************************************/
54 #define VHOST_MEMORY_MAX_NREGIONS 8
55 #define VHOST_MAX_VIRTQUEUES 0x100
57 #define VHOST_USER_F_PROTOCOL_FEATURES 30
58 #define VIRTIO_F_VERSION_1 32
60 #define VHOST_USER_PROTOCOL_F_MQ 0
61 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
62 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
63 #define VHOST_USER_PROTOCOL_F_CONFIG 9
65 #define VHOST_LOG_PAGE 0x1000
67 typedef enum VhostUserRequest
{
69 VHOST_USER_GET_FEATURES
= 1,
70 VHOST_USER_SET_FEATURES
= 2,
71 VHOST_USER_SET_OWNER
= 3,
72 VHOST_USER_RESET_OWNER
= 4,
73 VHOST_USER_SET_MEM_TABLE
= 5,
74 VHOST_USER_SET_LOG_BASE
= 6,
75 VHOST_USER_SET_LOG_FD
= 7,
76 VHOST_USER_SET_VRING_NUM
= 8,
77 VHOST_USER_SET_VRING_ADDR
= 9,
78 VHOST_USER_SET_VRING_BASE
= 10,
79 VHOST_USER_GET_VRING_BASE
= 11,
80 VHOST_USER_SET_VRING_KICK
= 12,
81 VHOST_USER_SET_VRING_CALL
= 13,
82 VHOST_USER_SET_VRING_ERR
= 14,
83 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
84 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
85 VHOST_USER_GET_QUEUE_NUM
= 17,
86 VHOST_USER_SET_VRING_ENABLE
= 18,
87 VHOST_USER_GET_CONFIG
= 24,
88 VHOST_USER_SET_CONFIG
= 25,
92 typedef struct VhostUserMemoryRegion
{
93 uint64_t guest_phys_addr
;
95 uint64_t userspace_addr
;
97 } VhostUserMemoryRegion
;
99 typedef struct VhostUserMemory
{
102 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
105 typedef struct VhostUserLog
{
107 uint64_t mmap_offset
;
110 typedef struct VhostUserMsg
{
111 VhostUserRequest request
;
113 #define VHOST_USER_VERSION_MASK (0x3)
114 #define VHOST_USER_REPLY_MASK (0x1<<2)
116 uint32_t size
; /* the following payload size */
118 #define VHOST_USER_VRING_IDX_MASK (0xff)
119 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
121 struct vhost_vring_state state
;
122 struct vhost_vring_addr addr
;
123 VhostUserMemory memory
;
126 } QEMU_PACKED VhostUserMsg
;
128 static VhostUserMsg m
__attribute__ ((unused
));
129 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
133 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
135 /* The version of the protocol we support */
136 #define VHOST_USER_VERSION (0x1)
137 /*****************************************************************************/
141 TEST_FLAGS_DISCONNECT
,
152 typedef struct TestServer
{
159 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
160 VhostUserMemory memory
;
161 GMainContext
*context
;
171 struct vhost_user_ops
*vu_ops
;
174 struct vhost_user_ops
{
177 void (*append_opts
)(TestServer
*s
, GString
*cmd_line
,
178 const char *chr_opts
);
180 /* VHOST-USER commands. */
181 uint64_t (*get_features
)(TestServer
*s
);
182 void (*set_features
)(TestServer
*s
, CharBackend
*chr
,
184 void (*get_protocol_features
)(TestServer
*s
,
185 CharBackend
*chr
, VhostUserMsg
*msg
);
188 static const char *init_hugepagefs(void);
189 static TestServer
*test_server_new(const gchar
*name
,
190 struct vhost_user_ops
*ops
);
191 static void test_server_free(TestServer
*server
);
192 static void test_server_listen(TestServer
*server
);
200 static void append_vhost_net_opts(TestServer
*s
, GString
*cmd_line
,
201 const char *chr_opts
)
203 g_string_append_printf(cmd_line
, QEMU_CMD_CHR QEMU_CMD_NETDEV
,
204 s
->chr_name
, s
->socket_path
,
205 chr_opts
, s
->chr_name
);
209 * For GPIO there are no other magic devices we need to add (like
210 * block or netdev) so all we need to worry about is the vhost-user
213 static void append_vhost_gpio_opts(TestServer
*s
, GString
*cmd_line
,
214 const char *chr_opts
)
216 g_string_append_printf(cmd_line
, QEMU_CMD_CHR
,
217 s
->chr_name
, s
->socket_path
,
221 static void append_mem_opts(TestServer
*server
, GString
*cmd_line
,
222 int size
, enum test_memfd memfd
)
224 if (memfd
== TEST_MEMFD_AUTO
) {
225 memfd
= qemu_memfd_check(MFD_ALLOW_SEALING
) ? TEST_MEMFD_YES
229 if (memfd
== TEST_MEMFD_YES
) {
230 g_string_append_printf(cmd_line
, QEMU_CMD_MEMFD
, size
, size
);
232 const char *root
= init_hugepagefs() ? : server
->tmpfs
;
234 g_string_append_printf(cmd_line
, QEMU_CMD_MEM
, size
, size
, root
);
238 static bool wait_for_fds(TestServer
*s
)
244 g_mutex_lock(&s
->data_mutex
);
246 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
247 while (!s
->fds_num
) {
248 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
249 /* timeout has passed */
250 g_assert(s
->fds_num
);
255 /* check for sanity */
256 g_assert_cmpint(s
->fds_num
, >, 0);
257 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
259 g_mutex_unlock(&s
->data_mutex
);
262 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
263 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
264 if (reg
->guest_phys_addr
== 0) {
270 g_test_skip("No memory at address 0x0");
275 static void read_guest_mem_server(QTestState
*qts
, TestServer
*s
)
281 g_mutex_lock(&s
->data_mutex
);
283 /* iterate all regions */
284 for (i
= 0; i
< s
->fds_num
; i
++) {
286 /* We'll check only the region starting at 0x0 */
287 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
291 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
293 size
= s
->memory
.regions
[i
].memory_size
+
294 s
->memory
.regions
[i
].mmap_offset
;
296 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
297 MAP_SHARED
, s
->fds
[i
], 0);
299 g_assert(guest_mem
!= MAP_FAILED
);
300 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
302 for (j
= 0; j
< 1024; j
++) {
303 uint32_t a
= qtest_readb(qts
, s
->memory
.regions
[i
].guest_phys_addr
+ j
);
304 uint32_t b
= guest_mem
[j
];
306 g_assert_cmpint(a
, ==, b
);
309 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
312 g_mutex_unlock(&s
->data_mutex
);
315 static void *thread_function(void *data
)
317 GMainLoop
*loop
= data
;
318 g_main_loop_run(loop
);
322 static int chr_can_read(void *opaque
)
324 return VHOST_USER_HDR_SIZE
;
327 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
329 g_autoptr(GError
) err
= NULL
;
330 TestServer
*s
= opaque
;
331 CharBackend
*chr
= &s
->chr
;
333 uint8_t *p
= (uint8_t *) &msg
;
337 qemu_chr_fe_disconnect(chr
);
338 /* now switch to non-failure */
339 s
->test_fail
= false;
342 if (size
!= VHOST_USER_HDR_SIZE
) {
343 qos_printf("%s: Wrong message size received %d\n", __func__
, size
);
347 g_mutex_lock(&s
->data_mutex
);
348 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
351 p
+= VHOST_USER_HDR_SIZE
;
352 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
353 if (size
!= msg
.size
) {
354 qos_printf("%s: Wrong message size received %d != %d\n",
355 __func__
, size
, msg
.size
);
360 switch (msg
.request
) {
361 case VHOST_USER_GET_FEATURES
:
362 /* Mandatory for tests to define get_features */
363 g_assert(s
->vu_ops
->get_features
);
365 /* send back features to qemu */
366 msg
.flags
|= VHOST_USER_REPLY_MASK
;
367 msg
.size
= sizeof(m
.payload
.u64
);
369 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
371 s
->test_flags
= TEST_FLAGS_END
;
373 msg
.payload
.u64
= s
->vu_ops
->get_features(s
);
376 qemu_chr_fe_write_all(chr
, (uint8_t *) &msg
,
377 VHOST_USER_HDR_SIZE
+ msg
.size
);
380 case VHOST_USER_SET_FEATURES
:
381 if (s
->vu_ops
->set_features
) {
382 s
->vu_ops
->set_features(s
, chr
, &msg
);
386 case VHOST_USER_SET_OWNER
:
388 * We don't need to do anything here, the remote is just
389 * letting us know it is in charge. Just log it.
391 qos_printf("set_owner: start of session\n");
394 case VHOST_USER_GET_PROTOCOL_FEATURES
:
395 if (s
->vu_ops
->get_protocol_features
) {
396 s
->vu_ops
->get_protocol_features(s
, chr
, &msg
);
400 case VHOST_USER_GET_CONFIG
:
402 * Treat GET_CONFIG as a NOP and just reply and let the guest
403 * consider we have updated its memory. Tests currently don't
404 * require working configs.
406 msg
.flags
|= VHOST_USER_REPLY_MASK
;
407 p
= (uint8_t *) &msg
;
408 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
411 case VHOST_USER_SET_PROTOCOL_FEATURES
:
413 * We did set VHOST_USER_F_PROTOCOL_FEATURES so its valid for
414 * the remote end to send this. There is no handshake reply so
415 * just log the details for debugging.
417 qos_printf("set_protocol_features: 0x%"PRIx64
"\n", msg
.payload
.u64
);
421 * A real vhost-user backend would actually set the size and
422 * address of the vrings but we can simply report them.
424 case VHOST_USER_SET_VRING_NUM
:
425 qos_printf("set_vring_num: %d/%d\n",
426 msg
.payload
.state
.index
, msg
.payload
.state
.num
);
428 case VHOST_USER_SET_VRING_ADDR
:
429 qos_printf("set_vring_addr: 0x%"PRIx64
"/0x%"PRIx64
"/0x%"PRIx64
"\n",
430 msg
.payload
.addr
.avail_user_addr
,
431 msg
.payload
.addr
.desc_user_addr
,
432 msg
.payload
.addr
.used_user_addr
);
435 case VHOST_USER_GET_VRING_BASE
:
436 /* send back vring base to qemu */
437 msg
.flags
|= VHOST_USER_REPLY_MASK
;
438 msg
.size
= sizeof(m
.payload
.state
);
439 msg
.payload
.state
.num
= 0;
440 p
= (uint8_t *) &msg
;
441 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
443 assert(msg
.payload
.state
.index
< s
->queues
* 2);
444 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
445 g_cond_broadcast(&s
->data_cond
);
448 case VHOST_USER_SET_MEM_TABLE
:
449 /* received the mem table */
450 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
451 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
452 G_N_ELEMENTS(s
->fds
));
454 /* signal the test that it can continue */
455 g_cond_broadcast(&s
->data_cond
);
458 case VHOST_USER_SET_VRING_KICK
:
459 case VHOST_USER_SET_VRING_CALL
:
461 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
463 * This is a non-blocking eventfd.
464 * The receive function forces it to be blocking,
465 * so revert it back to non-blocking.
467 g_unix_set_fd_nonblocking(fd
, true, &err
);
468 g_assert_no_error(err
);
471 case VHOST_USER_SET_LOG_BASE
:
472 if (s
->log_fd
!= -1) {
476 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
477 msg
.flags
|= VHOST_USER_REPLY_MASK
;
479 p
= (uint8_t *) &msg
;
480 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
482 g_cond_broadcast(&s
->data_cond
);
485 case VHOST_USER_SET_VRING_BASE
:
486 assert(msg
.payload
.state
.index
< s
->queues
* 2);
487 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
488 g_cond_broadcast(&s
->data_cond
);
491 case VHOST_USER_GET_QUEUE_NUM
:
492 msg
.flags
|= VHOST_USER_REPLY_MASK
;
493 msg
.size
= sizeof(m
.payload
.u64
);
494 msg
.payload
.u64
= s
->queues
;
495 p
= (uint8_t *) &msg
;
496 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
499 case VHOST_USER_SET_VRING_ENABLE
:
501 * Another case we ignore as we don't need to respond. With a
502 * fully functioning vhost-user we would enable/disable the
505 qos_printf("set_vring(%d)=%s\n", msg
.payload
.state
.index
,
506 msg
.payload
.state
.num
? "enabled" : "disabled");
510 qos_printf("vhost-user: un-handled message: %d\n", msg
.request
);
515 g_mutex_unlock(&s
->data_mutex
);
518 static const char *init_hugepagefs(void)
521 static const char *hugepagefs
;
522 const char *path
= getenv("QTEST_HUGETLBFS_PATH");
533 if (access(path
, R_OK
| W_OK
| X_OK
)) {
534 qos_printf("access on path (%s): %s", path
, strerror(errno
));
540 ret
= statfs(path
, &fs
);
541 } while (ret
!= 0 && errno
== EINTR
);
544 qos_printf("statfs on path (%s): %s", path
, strerror(errno
));
549 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
550 qos_printf("Warning: path not on HugeTLBFS: %s", path
);
562 static TestServer
*test_server_new(const gchar
*name
,
563 struct vhost_user_ops
*ops
)
565 TestServer
*server
= g_new0(TestServer
, 1);
566 g_autofree
const char *tmpfs
= NULL
;
569 server
->context
= g_main_context_new();
570 server
->loop
= g_main_loop_new(server
->context
, FALSE
);
572 /* run the main loop thread so the chardev may operate */
573 server
->thread
= g_thread_new(NULL
, thread_function
, server
->loop
);
575 tmpfs
= g_dir_make_tmp("vhost-test-XXXXXX", &err
);
577 g_test_message("Can't create temporary directory in %s: %s",
578 g_get_tmp_dir(), err
->message
);
583 server
->tmpfs
= g_strdup(tmpfs
);
584 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
585 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
586 server
->chr_name
= g_strdup_printf("chr-%s", name
);
588 g_mutex_init(&server
->data_mutex
);
589 g_cond_init(&server
->data_cond
);
593 server
->vu_ops
= ops
;
598 static void chr_event(void *opaque
, QEMUChrEvent event
)
600 TestServer
*s
= opaque
;
602 if (s
->test_flags
== TEST_FLAGS_END
&&
603 event
== CHR_EVENT_CLOSED
) {
604 s
->test_flags
= TEST_FLAGS_OK
;
608 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
610 g_autofree gchar
*chr_path
= g_strdup_printf("unix:%s%s",
611 server
->socket_path
, opt
);
614 chr
= qemu_chr_new(server
->chr_name
, chr_path
, server
->context
);
617 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
618 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
619 chr_event
, NULL
, server
, server
->context
, true);
622 static void test_server_listen(TestServer
*server
)
624 test_server_create_chr(server
, ",server=on,wait=off");
627 static void test_server_free(TestServer
*server
)
631 /* finish the helper thread and dispatch pending sources */
632 g_main_loop_quit(server
->loop
);
633 g_thread_join(server
->thread
);
634 while (g_main_context_pending(NULL
)) {
635 g_main_context_iteration(NULL
, TRUE
);
638 unlink(server
->socket_path
);
639 g_free(server
->socket_path
);
641 unlink(server
->mig_path
);
642 g_free(server
->mig_path
);
644 ret
= rmdir(server
->tmpfs
);
646 g_test_message("unable to rmdir: path (%s): %s",
647 server
->tmpfs
, strerror(errno
));
649 g_free(server
->tmpfs
);
651 qemu_chr_fe_deinit(&server
->chr
, true);
653 for (i
= 0; i
< server
->fds_num
; i
++) {
654 close(server
->fds
[i
]);
657 if (server
->log_fd
!= -1) {
658 close(server
->log_fd
);
661 g_free(server
->chr_name
);
663 g_main_loop_unref(server
->loop
);
664 g_main_context_unref(server
->context
);
665 g_cond_clear(&server
->data_cond
);
666 g_mutex_clear(&server
->data_mutex
);
670 static void wait_for_log_fd(TestServer
*s
)
674 g_mutex_lock(&s
->data_mutex
);
675 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
676 while (s
->log_fd
== -1) {
677 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
678 /* timeout has passed */
679 g_assert(s
->log_fd
!= -1);
684 g_mutex_unlock(&s
->data_mutex
);
687 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
693 /* iterate all regions */
694 for (i
= 0; i
< s
->fds_num
; i
++) {
696 /* We'll write only the region statring at 0x0 */
697 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
701 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
703 size
= s
->memory
.regions
[i
].memory_size
+
704 s
->memory
.regions
[i
].mmap_offset
;
706 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
707 MAP_SHARED
, s
->fds
[i
], 0);
709 g_assert(guest_mem
!= MAP_FAILED
);
710 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
712 for (j
= 0; j
< 256; j
++) {
713 guest_mem
[j
] = seed
+ j
;
716 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
721 static guint64
get_log_size(TestServer
*s
)
723 guint64 log_size
= 0;
726 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
727 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
728 guint64 last
= range_get_last(reg
->guest_phys_addr
,
730 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
736 typedef struct TestMigrateSource
{
743 test_migrate_source_check(GSource
*source
)
745 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
746 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
753 GSourceFuncs test_migrate_source_funcs
= {
754 .check
= test_migrate_source_check
,
757 static void vhost_user_test_cleanup(void *s
)
759 TestServer
*server
= s
;
761 qos_invalidate_command_line();
762 test_server_free(server
);
765 static void *vhost_user_test_setup(GString
*cmd_line
, void *arg
)
767 TestServer
*server
= test_server_new("vhost-user-test", arg
);
768 test_server_listen(server
);
770 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_AUTO
);
771 server
->vu_ops
->append_opts(server
, cmd_line
, "");
773 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
778 static void *vhost_user_test_setup_memfd(GString
*cmd_line
, void *arg
)
780 TestServer
*server
= test_server_new("vhost-user-test", arg
);
781 test_server_listen(server
);
783 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_YES
);
784 server
->vu_ops
->append_opts(server
, cmd_line
, "");
786 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
791 static void test_read_guest_mem(void *obj
, void *arg
, QGuestAllocator
*alloc
)
793 TestServer
*server
= arg
;
795 if (!wait_for_fds(server
)) {
799 read_guest_mem_server(global_qtest
, server
);
802 static void test_migrate(void *obj
, void *arg
, QGuestAllocator
*alloc
)
806 GString
*dest_cmdline
;
814 if (!wait_for_fds(s
)) {
818 dest
= test_server_new("dest", s
->vu_ops
);
819 dest_cmdline
= g_string_new(qos_get_current_command_line());
820 uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
822 size
= get_log_size(s
);
823 g_assert_cmpint(size
, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
825 test_server_listen(dest
);
826 g_string_append_printf(dest_cmdline
, " -incoming %s", uri
);
827 append_mem_opts(dest
, dest_cmdline
, 256, TEST_MEMFD_AUTO
);
828 dest
->vu_ops
->append_opts(dest
, dest_cmdline
, "");
829 to
= qtest_init(dest_cmdline
->str
);
831 /* This would be where you call qos_allocate_objects(to, NULL), if you want
832 * to talk to the QVirtioNet object on the destination.
835 source
= g_source_new(&test_migrate_source_funcs
,
836 sizeof(TestMigrateSource
));
837 ((TestMigrateSource
*)source
)->src
= s
;
838 ((TestMigrateSource
*)source
)->dest
= dest
;
839 g_source_attach(source
, s
->context
);
841 /* slow down migration to have time to fiddle with log */
842 /* TODO: qtest could learn to break on some places */
843 rsp
= qmp("{ 'execute': 'migrate-set-parameters',"
844 "'arguments': { 'max-bandwidth': 10 } }");
845 g_assert(qdict_haskey(rsp
, "return"));
848 rsp
= qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri
);
849 g_assert(qdict_haskey(rsp
, "return"));
854 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
855 g_assert(log
!= MAP_FAILED
);
857 /* modify first page */
858 write_guest_mem(s
, 0x42);
862 /* speed things up */
863 rsp
= qmp("{ 'execute': 'migrate-set-parameters',"
864 "'arguments': { 'max-bandwidth': 0 } }");
865 g_assert(qdict_haskey(rsp
, "return"));
868 qmp_eventwait("STOP");
869 qtest_qmp_eventwait(to
, "RESUME");
871 g_assert(wait_for_fds(dest
));
872 read_guest_mem_server(to
, dest
);
874 g_source_destroy(source
);
875 g_source_unref(source
);
878 test_server_free(dest
);
880 g_string_free(dest_cmdline
, true);
883 static void wait_for_rings_started(TestServer
*s
, size_t count
)
887 g_mutex_lock(&s
->data_mutex
);
888 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
889 while (ctpop64(s
->rings
) != count
) {
890 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
891 /* timeout has passed */
892 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
897 g_mutex_unlock(&s
->data_mutex
);
900 static inline void test_server_connect(TestServer
*server
)
902 test_server_create_chr(server
, ",reconnect=1");
906 reconnect_cb(gpointer user_data
)
908 TestServer
*s
= user_data
;
910 qemu_chr_fe_disconnect(&s
->chr
);
916 connect_thread(gpointer data
)
918 TestServer
*s
= data
;
920 /* wait for qemu to start before first try, to avoid extra warnings */
921 g_usleep(G_USEC_PER_SEC
);
922 test_server_connect(s
);
927 static void *vhost_user_test_setup_reconnect(GString
*cmd_line
, void *arg
)
929 TestServer
*s
= test_server_new("reconnect", arg
);
931 g_thread_new("connect", connect_thread
, s
);
932 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
933 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
935 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
940 static void test_reconnect(void *obj
, void *arg
, QGuestAllocator
*alloc
)
945 if (!wait_for_fds(s
)) {
949 wait_for_rings_started(s
, 2);
954 src
= g_idle_source_new();
955 g_source_set_callback(src
, reconnect_cb
, s
, NULL
);
956 g_source_attach(src
, s
->context
);
958 g_assert(wait_for_fds(s
));
959 wait_for_rings_started(s
, 2);
962 static void *vhost_user_test_setup_connect_fail(GString
*cmd_line
, void *arg
)
964 TestServer
*s
= test_server_new("connect-fail", arg
);
968 g_thread_new("connect", connect_thread
, s
);
969 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
970 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
972 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
977 static void *vhost_user_test_setup_flags_mismatch(GString
*cmd_line
, void *arg
)
979 TestServer
*s
= test_server_new("flags-mismatch", arg
);
981 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
983 g_thread_new("connect", connect_thread
, s
);
984 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
985 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
987 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
992 static void test_vhost_user_started(void *obj
, void *arg
, QGuestAllocator
*alloc
)
996 if (!wait_for_fds(s
)) {
999 wait_for_rings_started(s
, 2);
1002 static void *vhost_user_test_setup_multiqueue(GString
*cmd_line
, void *arg
)
1004 TestServer
*s
= vhost_user_test_setup(cmd_line
, arg
);
1007 g_string_append_printf(cmd_line
,
1008 " -set netdev.hs0.queues=%d"
1009 " -global virtio-net-pci.vectors=%d",
1010 s
->queues
, s
->queues
* 2 + 2);
1015 static void test_multiqueue(void *obj
, void *arg
, QGuestAllocator
*alloc
)
1017 TestServer
*s
= arg
;
1019 wait_for_rings_started(s
, s
->queues
* 2);
1023 static uint64_t vu_net_get_features(TestServer
*s
)
1025 uint64_t features
= 0x1ULL
<< VHOST_F_LOG_ALL
|
1026 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
1028 if (s
->queues
> 1) {
1029 features
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
1035 static void vu_net_set_features(TestServer
*s
, CharBackend
*chr
,
1038 g_assert(msg
->payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
));
1039 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
1040 qemu_chr_fe_disconnect(chr
);
1041 s
->test_flags
= TEST_FLAGS_BAD
;
1045 static void vu_net_get_protocol_features(TestServer
*s
, CharBackend
*chr
,
1048 /* send back features to qemu */
1049 msg
->flags
|= VHOST_USER_REPLY_MASK
;
1050 msg
->size
= sizeof(m
.payload
.u64
);
1051 msg
->payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
1052 msg
->payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
;
1053 if (s
->queues
> 1) {
1054 msg
->payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
1056 qemu_chr_fe_write_all(chr
, (uint8_t *)msg
, VHOST_USER_HDR_SIZE
+ msg
->size
);
1059 /* Each VHOST-USER device should have its ops structure defined. */
1060 static struct vhost_user_ops g_vu_net_ops
= {
1061 .type
= VHOST_USER_NET
,
1063 .append_opts
= append_vhost_net_opts
,
1065 .get_features
= vu_net_get_features
,
1066 .set_features
= vu_net_set_features
,
1067 .get_protocol_features
= vu_net_get_protocol_features
,
1070 static void register_vhost_user_test(void)
1072 QOSGraphTestOptions opts
= {
1073 .before
= vhost_user_test_setup
,
1075 .arg
= &g_vu_net_ops
,
1078 qemu_add_opts(&qemu_chardev_opts
);
1080 qos_add_test("vhost-user/read-guest-mem/memfile",
1082 test_read_guest_mem
, &opts
);
1084 if (qemu_memfd_check(MFD_ALLOW_SEALING
)) {
1085 opts
.before
= vhost_user_test_setup_memfd
;
1086 qos_add_test("vhost-user/read-guest-mem/memfd",
1088 test_read_guest_mem
, &opts
);
1091 qos_add_test("vhost-user/migrate",
1093 test_migrate
, &opts
);
1095 opts
.before
= vhost_user_test_setup_reconnect
;
1096 qos_add_test("vhost-user/reconnect", "virtio-net",
1097 test_reconnect
, &opts
);
1099 opts
.before
= vhost_user_test_setup_connect_fail
;
1100 qos_add_test("vhost-user/connect-fail", "virtio-net",
1101 test_vhost_user_started
, &opts
);
1103 opts
.before
= vhost_user_test_setup_flags_mismatch
;
1104 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
1105 test_vhost_user_started
, &opts
);
1107 opts
.before
= vhost_user_test_setup_multiqueue
;
1108 opts
.edge
.extra_device_opts
= "mq=on";
1109 qos_add_test("vhost-user/multiqueue",
1111 test_multiqueue
, &opts
);
1113 libqos_init(register_vhost_user_test
);
1115 static uint64_t vu_gpio_get_features(TestServer
*s
)
1117 return 0x1ULL
<< VIRTIO_F_VERSION_1
|
1118 0x1ULL
<< VIRTIO_GPIO_F_IRQ
|
1119 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
1123 * This stub can't handle all the message types but we should reply
1124 * that we support VHOST_USER_PROTOCOL_F_CONFIG as gpio would use it
1125 * talking to a read vhost-user daemon.
1127 static void vu_gpio_get_protocol_features(TestServer
*s
, CharBackend
*chr
,
1130 /* send back features to qemu */
1131 msg
->flags
|= VHOST_USER_REPLY_MASK
;
1132 msg
->size
= sizeof(m
.payload
.u64
);
1133 msg
->payload
.u64
= 1ULL << VHOST_USER_PROTOCOL_F_CONFIG
;
1135 qemu_chr_fe_write_all(chr
, (uint8_t *)msg
, VHOST_USER_HDR_SIZE
+ msg
->size
);
1138 static struct vhost_user_ops g_vu_gpio_ops
= {
1139 .type
= VHOST_USER_GPIO
,
1141 .append_opts
= append_vhost_gpio_opts
,
1143 .get_features
= vu_gpio_get_features
,
1144 .set_features
= vu_net_set_features
,
1145 .get_protocol_features
= vu_gpio_get_protocol_features
,
1148 static void register_vhost_gpio_test(void)
1150 QOSGraphTestOptions opts
= {
1151 .before
= vhost_user_test_setup
,
1153 .arg
= &g_vu_gpio_ops
,
1156 qemu_add_opts(&qemu_chardev_opts
);
1158 qos_add_test("read-guest-mem/memfile",
1159 "vhost-user-gpio", test_read_guest_mem
, &opts
);
1161 libqos_init(register_vhost_gpio_test
);
1163 static uint64_t vu_scmi_get_features(TestServer
*s
)
1165 return 0x1ULL
<< VIRTIO_F_VERSION_1
|
1166 0x1ULL
<< VIRTIO_SCMI_F_P2A_CHANNELS
|
1167 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
1170 static void vu_scmi_get_protocol_features(TestServer
*s
, CharBackend
*chr
,
1173 msg
->flags
|= VHOST_USER_REPLY_MASK
;
1174 msg
->size
= sizeof(m
.payload
.u64
);
1175 msg
->payload
.u64
= 1ULL << VHOST_USER_PROTOCOL_F_MQ
;
1177 qemu_chr_fe_write_all(chr
, (uint8_t *)msg
, VHOST_USER_HDR_SIZE
+ msg
->size
);
1180 static struct vhost_user_ops g_vu_scmi_ops
= {
1181 .type
= VHOST_USER_SCMI
,
1183 .append_opts
= append_vhost_gpio_opts
,
1185 .get_features
= vu_scmi_get_features
,
1186 .set_features
= vu_net_set_features
,
1187 .get_protocol_features
= vu_scmi_get_protocol_features
,
1190 static void register_vhost_scmi_test(void)
1192 QOSGraphTestOptions opts
= {
1193 .before
= vhost_user_test_setup
,
1195 .arg
= &g_vu_scmi_ops
,
1198 qemu_add_opts(&qemu_chardev_opts
);
1200 qos_add_test("scmi/read-guest-mem/memfile",
1201 "vhost-user-scmi", test_read_guest_mem
, &opts
);
1203 libqos_init(register_vhost_scmi_test
);