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 "hw/virtio/virtio-net.h"
31 #include "standard-headers/linux/vhost_types.h"
32 #include "standard-headers/linux/virtio_ids.h"
33 #include "standard-headers/linux/virtio_net.h"
40 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
41 "mem-path=%s,share=on -numa node,memdev=mem"
42 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
43 " -numa node,memdev=mem"
44 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
45 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce=on"
47 #define HUGETLBFS_MAGIC 0x958458f6
49 /*********** FROM hw/virtio/vhost-user.c *************************************/
51 #define VHOST_MEMORY_MAX_NREGIONS 8
52 #define VHOST_MAX_VIRTQUEUES 0x100
54 #define VHOST_USER_F_PROTOCOL_FEATURES 30
55 #define VHOST_USER_PROTOCOL_F_MQ 0
56 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
57 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
59 #define VHOST_LOG_PAGE 0x1000
61 typedef enum VhostUserRequest
{
63 VHOST_USER_GET_FEATURES
= 1,
64 VHOST_USER_SET_FEATURES
= 2,
65 VHOST_USER_SET_OWNER
= 3,
66 VHOST_USER_RESET_OWNER
= 4,
67 VHOST_USER_SET_MEM_TABLE
= 5,
68 VHOST_USER_SET_LOG_BASE
= 6,
69 VHOST_USER_SET_LOG_FD
= 7,
70 VHOST_USER_SET_VRING_NUM
= 8,
71 VHOST_USER_SET_VRING_ADDR
= 9,
72 VHOST_USER_SET_VRING_BASE
= 10,
73 VHOST_USER_GET_VRING_BASE
= 11,
74 VHOST_USER_SET_VRING_KICK
= 12,
75 VHOST_USER_SET_VRING_CALL
= 13,
76 VHOST_USER_SET_VRING_ERR
= 14,
77 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
78 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
79 VHOST_USER_GET_QUEUE_NUM
= 17,
80 VHOST_USER_SET_VRING_ENABLE
= 18,
84 typedef struct VhostUserMemoryRegion
{
85 uint64_t guest_phys_addr
;
87 uint64_t userspace_addr
;
89 } VhostUserMemoryRegion
;
91 typedef struct VhostUserMemory
{
94 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
97 typedef struct VhostUserLog
{
102 typedef struct VhostUserMsg
{
103 VhostUserRequest request
;
105 #define VHOST_USER_VERSION_MASK (0x3)
106 #define VHOST_USER_REPLY_MASK (0x1<<2)
108 uint32_t size
; /* the following payload size */
110 #define VHOST_USER_VRING_IDX_MASK (0xff)
111 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
113 struct vhost_vring_state state
;
114 struct vhost_vring_addr addr
;
115 VhostUserMemory memory
;
118 } QEMU_PACKED VhostUserMsg
;
120 static VhostUserMsg m
__attribute__ ((unused
));
121 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
125 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
127 /* The version of the protocol we support */
128 #define VHOST_USER_VERSION (0x1)
129 /*****************************************************************************/
133 TEST_FLAGS_DISCONNECT
,
142 typedef struct TestServer
{
149 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
150 VhostUserMemory memory
;
151 GMainContext
*context
;
161 struct vhost_user_ops
*vu_ops
;
164 struct vhost_user_ops
{
167 void (*append_opts
)(TestServer
*s
, GString
*cmd_line
,
168 const char *chr_opts
);
170 /* VHOST-USER commands. */
171 void (*set_features
)(TestServer
*s
, CharBackend
*chr
,
173 void (*get_protocol_features
)(TestServer
*s
,
174 CharBackend
*chr
, VhostUserMsg
*msg
);
177 static const char *init_hugepagefs(void);
178 static TestServer
*test_server_new(const gchar
*name
,
179 struct vhost_user_ops
*ops
);
180 static void test_server_free(TestServer
*server
);
181 static void test_server_listen(TestServer
*server
);
189 static void append_vhost_net_opts(TestServer
*s
, GString
*cmd_line
,
190 const char *chr_opts
)
192 g_string_append_printf(cmd_line
, QEMU_CMD_CHR QEMU_CMD_NETDEV
,
193 s
->chr_name
, s
->socket_path
,
194 chr_opts
, s
->chr_name
);
197 static void append_mem_opts(TestServer
*server
, GString
*cmd_line
,
198 int size
, enum test_memfd memfd
)
200 if (memfd
== TEST_MEMFD_AUTO
) {
201 memfd
= qemu_memfd_check(MFD_ALLOW_SEALING
) ? TEST_MEMFD_YES
205 if (memfd
== TEST_MEMFD_YES
) {
206 g_string_append_printf(cmd_line
, QEMU_CMD_MEMFD
, size
, size
);
208 const char *root
= init_hugepagefs() ? : server
->tmpfs
;
210 g_string_append_printf(cmd_line
, QEMU_CMD_MEM
, size
, size
, root
);
214 static bool wait_for_fds(TestServer
*s
)
220 g_mutex_lock(&s
->data_mutex
);
222 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
223 while (!s
->fds_num
) {
224 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
225 /* timeout has passed */
226 g_assert(s
->fds_num
);
231 /* check for sanity */
232 g_assert_cmpint(s
->fds_num
, >, 0);
233 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
235 g_mutex_unlock(&s
->data_mutex
);
238 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
239 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
240 if (reg
->guest_phys_addr
== 0) {
246 g_test_skip("No memory at address 0x0");
251 static void read_guest_mem_server(QTestState
*qts
, TestServer
*s
)
257 g_mutex_lock(&s
->data_mutex
);
259 /* iterate all regions */
260 for (i
= 0; i
< s
->fds_num
; i
++) {
262 /* We'll check only the region statring at 0x0*/
263 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
267 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
269 size
= s
->memory
.regions
[i
].memory_size
+
270 s
->memory
.regions
[i
].mmap_offset
;
272 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
273 MAP_SHARED
, s
->fds
[i
], 0);
275 g_assert(guest_mem
!= MAP_FAILED
);
276 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
278 for (j
= 0; j
< 1024; j
++) {
279 uint32_t a
= qtest_readb(qts
, s
->memory
.regions
[i
].guest_phys_addr
+ j
);
280 uint32_t b
= guest_mem
[j
];
282 g_assert_cmpint(a
, ==, b
);
285 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
288 g_mutex_unlock(&s
->data_mutex
);
291 static void *thread_function(void *data
)
293 GMainLoop
*loop
= data
;
294 g_main_loop_run(loop
);
298 static int chr_can_read(void *opaque
)
300 return VHOST_USER_HDR_SIZE
;
303 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
305 g_autoptr(GError
) err
= NULL
;
306 TestServer
*s
= opaque
;
307 CharBackend
*chr
= &s
->chr
;
309 uint8_t *p
= (uint8_t *) &msg
;
313 qemu_chr_fe_disconnect(chr
);
314 /* now switch to non-failure */
315 s
->test_fail
= false;
318 if (size
!= VHOST_USER_HDR_SIZE
) {
319 g_test_message("Wrong message size received %d", size
);
323 g_mutex_lock(&s
->data_mutex
);
324 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
327 p
+= VHOST_USER_HDR_SIZE
;
328 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
329 if (size
!= msg
.size
) {
330 g_test_message("Wrong message size received %d != %d",
336 switch (msg
.request
) {
337 case VHOST_USER_GET_FEATURES
:
338 /* send back features to qemu */
339 msg
.flags
|= VHOST_USER_REPLY_MASK
;
340 msg
.size
= sizeof(m
.payload
.u64
);
341 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
342 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
344 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
346 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
348 s
->test_flags
= TEST_FLAGS_END
;
350 p
= (uint8_t *) &msg
;
351 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
354 case VHOST_USER_SET_FEATURES
:
355 if (s
->vu_ops
->set_features
) {
356 s
->vu_ops
->set_features(s
, chr
, &msg
);
360 case VHOST_USER_GET_PROTOCOL_FEATURES
:
361 if (s
->vu_ops
->get_protocol_features
) {
362 s
->vu_ops
->get_protocol_features(s
, chr
, &msg
);
366 case VHOST_USER_GET_VRING_BASE
:
367 /* send back vring base to qemu */
368 msg
.flags
|= VHOST_USER_REPLY_MASK
;
369 msg
.size
= sizeof(m
.payload
.state
);
370 msg
.payload
.state
.num
= 0;
371 p
= (uint8_t *) &msg
;
372 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
374 assert(msg
.payload
.state
.index
< s
->queues
* 2);
375 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
376 g_cond_broadcast(&s
->data_cond
);
379 case VHOST_USER_SET_MEM_TABLE
:
380 /* received the mem table */
381 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
382 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
383 G_N_ELEMENTS(s
->fds
));
385 /* signal the test that it can continue */
386 g_cond_broadcast(&s
->data_cond
);
389 case VHOST_USER_SET_VRING_KICK
:
390 case VHOST_USER_SET_VRING_CALL
:
392 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
394 * This is a non-blocking eventfd.
395 * The receive function forces it to be blocking,
396 * so revert it back to non-blocking.
398 g_unix_set_fd_nonblocking(fd
, true, &err
);
399 g_assert_no_error(err
);
402 case VHOST_USER_SET_LOG_BASE
:
403 if (s
->log_fd
!= -1) {
407 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
408 msg
.flags
|= VHOST_USER_REPLY_MASK
;
410 p
= (uint8_t *) &msg
;
411 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
413 g_cond_broadcast(&s
->data_cond
);
416 case VHOST_USER_SET_VRING_BASE
:
417 assert(msg
.payload
.state
.index
< s
->queues
* 2);
418 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
419 g_cond_broadcast(&s
->data_cond
);
422 case VHOST_USER_GET_QUEUE_NUM
:
423 msg
.flags
|= VHOST_USER_REPLY_MASK
;
424 msg
.size
= sizeof(m
.payload
.u64
);
425 msg
.payload
.u64
= s
->queues
;
426 p
= (uint8_t *) &msg
;
427 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
434 g_mutex_unlock(&s
->data_mutex
);
437 static const char *init_hugepagefs(void)
440 static const char *hugepagefs
;
441 const char *path
= getenv("QTEST_HUGETLBFS_PATH");
452 if (access(path
, R_OK
| W_OK
| X_OK
)) {
453 g_test_message("access on path (%s): %s", path
, strerror(errno
));
459 ret
= statfs(path
, &fs
);
460 } while (ret
!= 0 && errno
== EINTR
);
463 g_test_message("statfs on path (%s): %s", path
, strerror(errno
));
468 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
469 g_test_message("Warning: path not on HugeTLBFS: %s", path
);
481 static TestServer
*test_server_new(const gchar
*name
,
482 struct vhost_user_ops
*ops
)
484 TestServer
*server
= g_new0(TestServer
, 1);
485 char template[] = "/tmp/vhost-test-XXXXXX";
488 server
->context
= g_main_context_new();
489 server
->loop
= g_main_loop_new(server
->context
, FALSE
);
491 /* run the main loop thread so the chardev may operate */
492 server
->thread
= g_thread_new(NULL
, thread_function
, server
->loop
);
494 tmpfs
= mkdtemp(template);
496 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
500 server
->tmpfs
= g_strdup(tmpfs
);
501 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
502 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
503 server
->chr_name
= g_strdup_printf("chr-%s", name
);
505 g_mutex_init(&server
->data_mutex
);
506 g_cond_init(&server
->data_cond
);
510 server
->vu_ops
= ops
;
515 static void chr_event(void *opaque
, QEMUChrEvent event
)
517 TestServer
*s
= opaque
;
519 if (s
->test_flags
== TEST_FLAGS_END
&&
520 event
== CHR_EVENT_CLOSED
) {
521 s
->test_flags
= TEST_FLAGS_OK
;
525 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
527 g_autofree gchar
*chr_path
= g_strdup_printf("unix:%s%s",
528 server
->socket_path
, opt
);
531 chr
= qemu_chr_new(server
->chr_name
, chr_path
, server
->context
);
534 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
535 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
536 chr_event
, NULL
, server
, server
->context
, true);
539 static void test_server_listen(TestServer
*server
)
541 test_server_create_chr(server
, ",server=on,wait=off");
544 static void test_server_free(TestServer
*server
)
548 /* finish the helper thread and dispatch pending sources */
549 g_main_loop_quit(server
->loop
);
550 g_thread_join(server
->thread
);
551 while (g_main_context_pending(NULL
)) {
552 g_main_context_iteration(NULL
, TRUE
);
555 unlink(server
->socket_path
);
556 g_free(server
->socket_path
);
558 unlink(server
->mig_path
);
559 g_free(server
->mig_path
);
561 ret
= rmdir(server
->tmpfs
);
563 g_test_message("unable to rmdir: path (%s): %s",
564 server
->tmpfs
, strerror(errno
));
566 g_free(server
->tmpfs
);
568 qemu_chr_fe_deinit(&server
->chr
, true);
570 for (i
= 0; i
< server
->fds_num
; i
++) {
571 close(server
->fds
[i
]);
574 if (server
->log_fd
!= -1) {
575 close(server
->log_fd
);
578 g_free(server
->chr_name
);
580 g_main_loop_unref(server
->loop
);
581 g_main_context_unref(server
->context
);
582 g_cond_clear(&server
->data_cond
);
583 g_mutex_clear(&server
->data_mutex
);
587 static void wait_for_log_fd(TestServer
*s
)
591 g_mutex_lock(&s
->data_mutex
);
592 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
593 while (s
->log_fd
== -1) {
594 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
595 /* timeout has passed */
596 g_assert(s
->log_fd
!= -1);
601 g_mutex_unlock(&s
->data_mutex
);
604 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
610 /* iterate all regions */
611 for (i
= 0; i
< s
->fds_num
; i
++) {
613 /* We'll write only the region statring at 0x0 */
614 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
618 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
620 size
= s
->memory
.regions
[i
].memory_size
+
621 s
->memory
.regions
[i
].mmap_offset
;
623 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
624 MAP_SHARED
, s
->fds
[i
], 0);
626 g_assert(guest_mem
!= MAP_FAILED
);
627 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
629 for (j
= 0; j
< 256; j
++) {
630 guest_mem
[j
] = seed
+ j
;
633 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
638 static guint64
get_log_size(TestServer
*s
)
640 guint64 log_size
= 0;
643 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
644 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
645 guint64 last
= range_get_last(reg
->guest_phys_addr
,
647 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
653 typedef struct TestMigrateSource
{
660 test_migrate_source_check(GSource
*source
)
662 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
663 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
670 GSourceFuncs test_migrate_source_funcs
= {
671 .check
= test_migrate_source_check
,
674 static void vhost_user_test_cleanup(void *s
)
676 TestServer
*server
= s
;
678 qos_invalidate_command_line();
679 test_server_free(server
);
682 static void *vhost_user_test_setup(GString
*cmd_line
, void *arg
)
684 TestServer
*server
= test_server_new("vhost-user-test", arg
);
685 test_server_listen(server
);
687 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_AUTO
);
688 server
->vu_ops
->append_opts(server
, cmd_line
, "");
690 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
695 static void *vhost_user_test_setup_memfd(GString
*cmd_line
, void *arg
)
697 TestServer
*server
= test_server_new("vhost-user-test", arg
);
698 test_server_listen(server
);
700 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_YES
);
701 server
->vu_ops
->append_opts(server
, cmd_line
, "");
703 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
708 static void test_read_guest_mem(void *obj
, void *arg
, QGuestAllocator
*alloc
)
710 TestServer
*server
= arg
;
712 if (!wait_for_fds(server
)) {
716 read_guest_mem_server(global_qtest
, server
);
719 static void test_migrate(void *obj
, void *arg
, QGuestAllocator
*alloc
)
723 GString
*dest_cmdline
;
731 if (!wait_for_fds(s
)) {
735 dest
= test_server_new("dest", s
->vu_ops
);
736 dest_cmdline
= g_string_new(qos_get_current_command_line());
737 uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
739 size
= get_log_size(s
);
740 g_assert_cmpint(size
, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
742 test_server_listen(dest
);
743 g_string_append_printf(dest_cmdline
, " -incoming %s", uri
);
744 append_mem_opts(dest
, dest_cmdline
, 256, TEST_MEMFD_AUTO
);
745 dest
->vu_ops
->append_opts(dest
, dest_cmdline
, "");
746 to
= qtest_init(dest_cmdline
->str
);
748 /* This would be where you call qos_allocate_objects(to, NULL), if you want
749 * to talk to the QVirtioNet object on the destination.
752 source
= g_source_new(&test_migrate_source_funcs
,
753 sizeof(TestMigrateSource
));
754 ((TestMigrateSource
*)source
)->src
= s
;
755 ((TestMigrateSource
*)source
)->dest
= dest
;
756 g_source_attach(source
, s
->context
);
758 /* slow down migration to have time to fiddle with log */
759 /* TODO: qtest could learn to break on some places */
760 rsp
= qmp("{ 'execute': 'migrate-set-parameters',"
761 "'arguments': { 'max-bandwidth': 10 } }");
762 g_assert(qdict_haskey(rsp
, "return"));
765 rsp
= qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri
);
766 g_assert(qdict_haskey(rsp
, "return"));
771 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
772 g_assert(log
!= MAP_FAILED
);
774 /* modify first page */
775 write_guest_mem(s
, 0x42);
779 /* speed things up */
780 rsp
= qmp("{ 'execute': 'migrate-set-parameters',"
781 "'arguments': { 'max-bandwidth': 0 } }");
782 g_assert(qdict_haskey(rsp
, "return"));
785 qmp_eventwait("STOP");
786 qtest_qmp_eventwait(to
, "RESUME");
788 g_assert(wait_for_fds(dest
));
789 read_guest_mem_server(to
, dest
);
791 g_source_destroy(source
);
792 g_source_unref(source
);
795 test_server_free(dest
);
797 g_string_free(dest_cmdline
, true);
800 static void wait_for_rings_started(TestServer
*s
, size_t count
)
804 g_mutex_lock(&s
->data_mutex
);
805 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
806 while (ctpop64(s
->rings
) != count
) {
807 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
808 /* timeout has passed */
809 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
814 g_mutex_unlock(&s
->data_mutex
);
817 static inline void test_server_connect(TestServer
*server
)
819 test_server_create_chr(server
, ",reconnect=1");
823 reconnect_cb(gpointer user_data
)
825 TestServer
*s
= user_data
;
827 qemu_chr_fe_disconnect(&s
->chr
);
833 connect_thread(gpointer data
)
835 TestServer
*s
= data
;
837 /* wait for qemu to start before first try, to avoid extra warnings */
838 g_usleep(G_USEC_PER_SEC
);
839 test_server_connect(s
);
844 static void *vhost_user_test_setup_reconnect(GString
*cmd_line
, void *arg
)
846 TestServer
*s
= test_server_new("reconnect", arg
);
848 g_thread_new("connect", connect_thread
, s
);
849 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
850 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
852 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
857 static void test_reconnect(void *obj
, void *arg
, QGuestAllocator
*alloc
)
862 if (!wait_for_fds(s
)) {
866 wait_for_rings_started(s
, 2);
871 src
= g_idle_source_new();
872 g_source_set_callback(src
, reconnect_cb
, s
, NULL
);
873 g_source_attach(src
, s
->context
);
875 g_assert(wait_for_fds(s
));
876 wait_for_rings_started(s
, 2);
879 static void *vhost_user_test_setup_connect_fail(GString
*cmd_line
, void *arg
)
881 TestServer
*s
= test_server_new("connect-fail", arg
);
885 g_thread_new("connect", connect_thread
, s
);
886 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
887 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
889 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
894 static void *vhost_user_test_setup_flags_mismatch(GString
*cmd_line
, void *arg
)
896 TestServer
*s
= test_server_new("flags-mismatch", arg
);
898 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
900 g_thread_new("connect", connect_thread
, s
);
901 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
902 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
904 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
909 static void test_vhost_user_started(void *obj
, void *arg
, QGuestAllocator
*alloc
)
913 if (!wait_for_fds(s
)) {
916 wait_for_rings_started(s
, 2);
919 static void *vhost_user_test_setup_multiqueue(GString
*cmd_line
, void *arg
)
921 TestServer
*s
= vhost_user_test_setup(cmd_line
, arg
);
924 g_string_append_printf(cmd_line
,
925 " -set netdev.hs0.queues=%d"
926 " -global virtio-net-pci.vectors=%d",
927 s
->queues
, s
->queues
* 2 + 2);
932 static void test_multiqueue(void *obj
, void *arg
, QGuestAllocator
*alloc
)
936 wait_for_rings_started(s
, s
->queues
* 2);
939 static void vu_net_set_features(TestServer
*s
, CharBackend
*chr
,
942 g_assert_cmpint(msg
->payload
.u64
&
943 (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
), !=, 0ULL);
944 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
945 qemu_chr_fe_disconnect(chr
);
946 s
->test_flags
= TEST_FLAGS_BAD
;
950 static void vu_net_get_protocol_features(TestServer
*s
, CharBackend
*chr
,
953 /* send back features to qemu */
954 msg
->flags
|= VHOST_USER_REPLY_MASK
;
955 msg
->size
= sizeof(m
.payload
.u64
);
956 msg
->payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
957 msg
->payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
;
959 msg
->payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
961 qemu_chr_fe_write_all(chr
, (uint8_t *)msg
, VHOST_USER_HDR_SIZE
+ msg
->size
);
964 /* Each VHOST-USER device should have its ops structure defined. */
965 static struct vhost_user_ops g_vu_net_ops
= {
966 .type
= VHOST_USER_NET
,
968 .append_opts
= append_vhost_net_opts
,
970 .set_features
= vu_net_set_features
,
971 .get_protocol_features
= vu_net_get_protocol_features
,
974 static void register_vhost_user_test(void)
976 QOSGraphTestOptions opts
= {
977 .before
= vhost_user_test_setup
,
979 .arg
= &g_vu_net_ops
,
982 qemu_add_opts(&qemu_chardev_opts
);
984 qos_add_test("vhost-user/read-guest-mem/memfile",
986 test_read_guest_mem
, &opts
);
988 if (qemu_memfd_check(MFD_ALLOW_SEALING
)) {
989 opts
.before
= vhost_user_test_setup_memfd
;
990 qos_add_test("vhost-user/read-guest-mem/memfd",
992 test_read_guest_mem
, &opts
);
995 qos_add_test("vhost-user/migrate",
997 test_migrate
, &opts
);
999 opts
.before
= vhost_user_test_setup_reconnect
;
1000 qos_add_test("vhost-user/reconnect", "virtio-net",
1001 test_reconnect
, &opts
);
1003 opts
.before
= vhost_user_test_setup_connect_fail
;
1004 qos_add_test("vhost-user/connect-fail", "virtio-net",
1005 test_vhost_user_started
, &opts
);
1007 opts
.before
= vhost_user_test_setup_flags_mismatch
;
1008 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
1009 test_vhost_user_started
, &opts
);
1011 opts
.before
= vhost_user_test_setup_multiqueue
;
1012 opts
.edge
.extra_device_opts
= "mq=on";
1013 qos_add_test("vhost-user/multiqueue",
1015 test_multiqueue
, &opts
);
1017 libqos_init(register_vhost_user_test
);