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"
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
,
138 typedef struct TestServer
{
145 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
146 VhostUserMemory memory
;
147 GMainContext
*context
;
159 static const char *init_hugepagefs(void);
160 static TestServer
*test_server_new(const gchar
*name
);
161 static void test_server_free(TestServer
*server
);
162 static void test_server_listen(TestServer
*server
);
170 static void append_vhost_opts(TestServer
*s
, GString
*cmd_line
,
171 const char *chr_opts
)
173 g_string_append_printf(cmd_line
, QEMU_CMD_CHR QEMU_CMD_NETDEV
,
174 s
->chr_name
, s
->socket_path
,
175 chr_opts
, s
->chr_name
);
178 static void append_mem_opts(TestServer
*server
, GString
*cmd_line
,
179 int size
, enum test_memfd memfd
)
181 if (memfd
== TEST_MEMFD_AUTO
) {
182 memfd
= qemu_memfd_check(MFD_ALLOW_SEALING
) ? TEST_MEMFD_YES
186 if (memfd
== TEST_MEMFD_YES
) {
187 g_string_append_printf(cmd_line
, QEMU_CMD_MEMFD
, size
, size
);
189 const char *root
= init_hugepagefs() ? : server
->tmpfs
;
191 g_string_append_printf(cmd_line
, QEMU_CMD_MEM
, size
, size
, root
);
195 static bool wait_for_fds(TestServer
*s
)
201 g_mutex_lock(&s
->data_mutex
);
203 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
204 while (!s
->fds_num
) {
205 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
206 /* timeout has passed */
207 g_assert(s
->fds_num
);
212 /* check for sanity */
213 g_assert_cmpint(s
->fds_num
, >, 0);
214 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
216 g_mutex_unlock(&s
->data_mutex
);
219 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
220 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
221 if (reg
->guest_phys_addr
== 0) {
227 g_test_skip("No memory at address 0x0");
232 static void read_guest_mem_server(QTestState
*qts
, TestServer
*s
)
238 g_mutex_lock(&s
->data_mutex
);
240 /* iterate all regions */
241 for (i
= 0; i
< s
->fds_num
; i
++) {
243 /* We'll check only the region statring at 0x0*/
244 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
248 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
250 size
= s
->memory
.regions
[i
].memory_size
+
251 s
->memory
.regions
[i
].mmap_offset
;
253 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
254 MAP_SHARED
, s
->fds
[i
], 0);
256 g_assert(guest_mem
!= MAP_FAILED
);
257 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
259 for (j
= 0; j
< 1024; j
++) {
260 uint32_t a
= qtest_readb(qts
, s
->memory
.regions
[i
].guest_phys_addr
+ j
);
261 uint32_t b
= guest_mem
[j
];
263 g_assert_cmpint(a
, ==, b
);
266 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
269 g_mutex_unlock(&s
->data_mutex
);
272 static void *thread_function(void *data
)
274 GMainLoop
*loop
= data
;
275 g_main_loop_run(loop
);
279 static int chr_can_read(void *opaque
)
281 return VHOST_USER_HDR_SIZE
;
284 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
286 TestServer
*s
= opaque
;
287 CharBackend
*chr
= &s
->chr
;
289 uint8_t *p
= (uint8_t *) &msg
;
293 qemu_chr_fe_disconnect(chr
);
294 /* now switch to non-failure */
295 s
->test_fail
= false;
298 if (size
!= VHOST_USER_HDR_SIZE
) {
299 g_test_message("Wrong message size received %d", size
);
303 g_mutex_lock(&s
->data_mutex
);
304 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
307 p
+= VHOST_USER_HDR_SIZE
;
308 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
309 if (size
!= msg
.size
) {
310 g_test_message("Wrong message size received %d != %d",
316 switch (msg
.request
) {
317 case VHOST_USER_GET_FEATURES
:
318 /* send back features to qemu */
319 msg
.flags
|= VHOST_USER_REPLY_MASK
;
320 msg
.size
= sizeof(m
.payload
.u64
);
321 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
322 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
324 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
326 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
328 s
->test_flags
= TEST_FLAGS_END
;
330 p
= (uint8_t *) &msg
;
331 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
334 case VHOST_USER_SET_FEATURES
:
335 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
337 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
338 qemu_chr_fe_disconnect(chr
);
339 s
->test_flags
= TEST_FLAGS_BAD
;
343 case VHOST_USER_GET_PROTOCOL_FEATURES
:
344 /* send back features to qemu */
345 msg
.flags
|= VHOST_USER_REPLY_MASK
;
346 msg
.size
= sizeof(m
.payload
.u64
);
347 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
348 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
;
350 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
352 p
= (uint8_t *) &msg
;
353 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
356 case VHOST_USER_GET_VRING_BASE
:
357 /* send back vring base to qemu */
358 msg
.flags
|= VHOST_USER_REPLY_MASK
;
359 msg
.size
= sizeof(m
.payload
.state
);
360 msg
.payload
.state
.num
= 0;
361 p
= (uint8_t *) &msg
;
362 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
364 assert(msg
.payload
.state
.index
< s
->queues
* 2);
365 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
366 g_cond_broadcast(&s
->data_cond
);
369 case VHOST_USER_SET_MEM_TABLE
:
370 /* received the mem table */
371 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
372 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
373 G_N_ELEMENTS(s
->fds
));
375 /* signal the test that it can continue */
376 g_cond_broadcast(&s
->data_cond
);
379 case VHOST_USER_SET_VRING_KICK
:
380 case VHOST_USER_SET_VRING_CALL
:
382 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
384 * This is a non-blocking eventfd.
385 * The receive function forces it to be blocking,
386 * so revert it back to non-blocking.
388 qemu_set_nonblock(fd
);
391 case VHOST_USER_SET_LOG_BASE
:
392 if (s
->log_fd
!= -1) {
396 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
397 msg
.flags
|= VHOST_USER_REPLY_MASK
;
399 p
= (uint8_t *) &msg
;
400 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
402 g_cond_broadcast(&s
->data_cond
);
405 case VHOST_USER_SET_VRING_BASE
:
406 assert(msg
.payload
.state
.index
< s
->queues
* 2);
407 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
408 g_cond_broadcast(&s
->data_cond
);
411 case VHOST_USER_GET_QUEUE_NUM
:
412 msg
.flags
|= VHOST_USER_REPLY_MASK
;
413 msg
.size
= sizeof(m
.payload
.u64
);
414 msg
.payload
.u64
= s
->queues
;
415 p
= (uint8_t *) &msg
;
416 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
423 g_mutex_unlock(&s
->data_mutex
);
426 static const char *init_hugepagefs(void)
429 static const char *hugepagefs
;
430 const char *path
= getenv("QTEST_HUGETLBFS_PATH");
441 if (access(path
, R_OK
| W_OK
| X_OK
)) {
442 g_test_message("access on path (%s): %s", path
, strerror(errno
));
448 ret
= statfs(path
, &fs
);
449 } while (ret
!= 0 && errno
== EINTR
);
452 g_test_message("statfs on path (%s): %s", path
, strerror(errno
));
457 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
458 g_test_message("Warning: path not on HugeTLBFS: %s", path
);
470 static TestServer
*test_server_new(const gchar
*name
)
472 TestServer
*server
= g_new0(TestServer
, 1);
473 char template[] = "/tmp/vhost-test-XXXXXX";
476 server
->context
= g_main_context_new();
477 server
->loop
= g_main_loop_new(server
->context
, FALSE
);
479 /* run the main loop thread so the chardev may operate */
480 server
->thread
= g_thread_new(NULL
, thread_function
, server
->loop
);
482 tmpfs
= mkdtemp(template);
484 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
488 server
->tmpfs
= g_strdup(tmpfs
);
489 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
490 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
491 server
->chr_name
= g_strdup_printf("chr-%s", name
);
493 g_mutex_init(&server
->data_mutex
);
494 g_cond_init(&server
->data_cond
);
502 static void chr_event(void *opaque
, int event
)
504 TestServer
*s
= opaque
;
506 if (s
->test_flags
== TEST_FLAGS_END
&&
507 event
== CHR_EVENT_CLOSED
) {
508 s
->test_flags
= TEST_FLAGS_OK
;
512 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
517 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
518 chr
= qemu_chr_new(server
->chr_name
, chr_path
, server
->context
);
521 g_assert_nonnull(chr
);
522 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
523 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
524 chr_event
, NULL
, server
, server
->context
, true);
527 static void test_server_listen(TestServer
*server
)
529 test_server_create_chr(server
, ",server,nowait");
532 static void test_server_free(TestServer
*server
)
536 /* finish the helper thread and dispatch pending sources */
537 g_main_loop_quit(server
->loop
);
538 g_thread_join(server
->thread
);
539 while (g_main_context_pending(NULL
)) {
540 g_main_context_iteration(NULL
, TRUE
);
543 unlink(server
->socket_path
);
544 g_free(server
->socket_path
);
546 unlink(server
->mig_path
);
547 g_free(server
->mig_path
);
549 ret
= rmdir(server
->tmpfs
);
551 g_test_message("unable to rmdir: path (%s): %s",
552 server
->tmpfs
, strerror(errno
));
554 g_free(server
->tmpfs
);
556 qemu_chr_fe_deinit(&server
->chr
, true);
558 for (i
= 0; i
< server
->fds_num
; i
++) {
559 close(server
->fds
[i
]);
562 if (server
->log_fd
!= -1) {
563 close(server
->log_fd
);
566 g_free(server
->chr_name
);
568 g_main_loop_unref(server
->loop
);
569 g_main_context_unref(server
->context
);
570 g_cond_clear(&server
->data_cond
);
571 g_mutex_clear(&server
->data_mutex
);
575 static void wait_for_log_fd(TestServer
*s
)
579 g_mutex_lock(&s
->data_mutex
);
580 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
581 while (s
->log_fd
== -1) {
582 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
583 /* timeout has passed */
584 g_assert(s
->log_fd
!= -1);
589 g_mutex_unlock(&s
->data_mutex
);
592 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
598 /* iterate all regions */
599 for (i
= 0; i
< s
->fds_num
; i
++) {
601 /* We'll write only the region statring at 0x0 */
602 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
606 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
608 size
= s
->memory
.regions
[i
].memory_size
+
609 s
->memory
.regions
[i
].mmap_offset
;
611 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
612 MAP_SHARED
, s
->fds
[i
], 0);
614 g_assert(guest_mem
!= MAP_FAILED
);
615 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
617 for (j
= 0; j
< 256; j
++) {
618 guest_mem
[j
] = seed
+ j
;
621 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
626 static guint64
get_log_size(TestServer
*s
)
628 guint64 log_size
= 0;
631 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
632 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
633 guint64 last
= range_get_last(reg
->guest_phys_addr
,
635 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
641 typedef struct TestMigrateSource
{
648 test_migrate_source_check(GSource
*source
)
650 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
651 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
658 GSourceFuncs test_migrate_source_funcs
= {
659 .check
= test_migrate_source_check
,
662 static void vhost_user_test_cleanup(void *s
)
664 TestServer
*server
= s
;
666 qos_invalidate_command_line();
667 test_server_free(server
);
670 static void *vhost_user_test_setup(GString
*cmd_line
, void *arg
)
672 TestServer
*server
= test_server_new("vhost-user-test");
673 test_server_listen(server
);
675 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_AUTO
);
676 append_vhost_opts(server
, cmd_line
, "");
678 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
683 static void *vhost_user_test_setup_memfd(GString
*cmd_line
, void *arg
)
685 TestServer
*server
= test_server_new("vhost-user-test");
686 test_server_listen(server
);
688 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_YES
);
689 append_vhost_opts(server
, cmd_line
, "");
691 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
696 static void test_read_guest_mem(void *obj
, void *arg
, QGuestAllocator
*alloc
)
698 TestServer
*server
= arg
;
700 if (!wait_for_fds(server
)) {
704 read_guest_mem_server(global_qtest
, server
);
707 static void test_migrate(void *obj
, void *arg
, QGuestAllocator
*alloc
)
710 TestServer
*dest
= test_server_new("dest");
711 GString
*dest_cmdline
= g_string_new(qos_get_current_command_line());
712 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
719 if (!wait_for_fds(s
)) {
723 size
= get_log_size(s
);
724 g_assert_cmpint(size
, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
726 test_server_listen(dest
);
727 g_string_append_printf(dest_cmdline
, " -incoming %s", uri
);
728 append_mem_opts(dest
, dest_cmdline
, 256, TEST_MEMFD_AUTO
);
729 append_vhost_opts(dest
, dest_cmdline
, "");
730 to
= qtest_init(dest_cmdline
->str
);
732 /* This would be where you call qos_allocate_objects(to, NULL), if you want
733 * to talk to the QVirtioNet object on the destination.
736 source
= g_source_new(&test_migrate_source_funcs
,
737 sizeof(TestMigrateSource
));
738 ((TestMigrateSource
*)source
)->src
= s
;
739 ((TestMigrateSource
*)source
)->dest
= dest
;
740 g_source_attach(source
, s
->context
);
742 /* slow down migration to have time to fiddle with log */
743 /* TODO: qtest could learn to break on some places */
744 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
745 "'arguments': { 'value': 10 } }");
746 g_assert(qdict_haskey(rsp
, "return"));
749 rsp
= qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri
);
750 g_assert(qdict_haskey(rsp
, "return"));
755 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
756 g_assert(log
!= MAP_FAILED
);
758 /* modify first page */
759 write_guest_mem(s
, 0x42);
763 /* speed things up */
764 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
765 "'arguments': { 'value': 0 } }");
766 g_assert(qdict_haskey(rsp
, "return"));
769 qmp_eventwait("STOP");
770 qtest_qmp_eventwait(to
, "RESUME");
772 g_assert(wait_for_fds(dest
));
773 read_guest_mem_server(to
, dest
);
775 g_source_destroy(source
);
776 g_source_unref(source
);
779 test_server_free(dest
);
783 static void wait_for_rings_started(TestServer
*s
, size_t count
)
787 g_mutex_lock(&s
->data_mutex
);
788 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
789 while (ctpop64(s
->rings
) != count
) {
790 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
791 /* timeout has passed */
792 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
797 g_mutex_unlock(&s
->data_mutex
);
800 static inline void test_server_connect(TestServer
*server
)
802 test_server_create_chr(server
, ",reconnect=1");
806 reconnect_cb(gpointer user_data
)
808 TestServer
*s
= user_data
;
810 qemu_chr_fe_disconnect(&s
->chr
);
816 connect_thread(gpointer data
)
818 TestServer
*s
= data
;
820 /* wait for qemu to start before first try, to avoid extra warnings */
821 g_usleep(G_USEC_PER_SEC
);
822 test_server_connect(s
);
827 static void *vhost_user_test_setup_reconnect(GString
*cmd_line
, void *arg
)
829 TestServer
*s
= test_server_new("reconnect");
831 g_thread_new("connect", connect_thread
, s
);
832 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
833 append_vhost_opts(s
, cmd_line
, ",server");
835 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
840 static void test_reconnect(void *obj
, void *arg
, QGuestAllocator
*alloc
)
845 if (!wait_for_fds(s
)) {
849 wait_for_rings_started(s
, 2);
854 src
= g_idle_source_new();
855 g_source_set_callback(src
, reconnect_cb
, s
, NULL
);
856 g_source_attach(src
, s
->context
);
858 g_assert(wait_for_fds(s
));
859 wait_for_rings_started(s
, 2);
862 static void *vhost_user_test_setup_connect_fail(GString
*cmd_line
, void *arg
)
864 TestServer
*s
= test_server_new("connect-fail");
868 g_thread_new("connect", connect_thread
, s
);
869 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
870 append_vhost_opts(s
, cmd_line
, ",server");
872 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
877 static void *vhost_user_test_setup_flags_mismatch(GString
*cmd_line
, void *arg
)
879 TestServer
*s
= test_server_new("flags-mismatch");
881 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
883 g_thread_new("connect", connect_thread
, s
);
884 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
885 append_vhost_opts(s
, cmd_line
, ",server");
887 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
892 static void test_vhost_user_started(void *obj
, void *arg
, QGuestAllocator
*alloc
)
896 if (!wait_for_fds(s
)) {
899 wait_for_rings_started(s
, 2);
902 static void *vhost_user_test_setup_multiqueue(GString
*cmd_line
, void *arg
)
904 TestServer
*s
= vhost_user_test_setup(cmd_line
, arg
);
907 g_string_append_printf(cmd_line
,
908 " -set netdev.hs0.queues=%d"
909 " -global virtio-net-pci.vectors=%d",
910 s
->queues
, s
->queues
* 2 + 2);
915 static void test_multiqueue(void *obj
, void *arg
, QGuestAllocator
*alloc
)
919 wait_for_rings_started(s
, s
->queues
* 2);
922 static void register_vhost_user_test(void)
924 QOSGraphTestOptions opts
= {
925 .before
= vhost_user_test_setup
,
929 qemu_add_opts(&qemu_chardev_opts
);
931 qos_add_test("vhost-user/read-guest-mem/memfile",
933 test_read_guest_mem
, &opts
);
935 if (qemu_memfd_check(MFD_ALLOW_SEALING
)) {
936 opts
.before
= vhost_user_test_setup_memfd
;
937 qos_add_test("vhost-user/read-guest-mem/memfd",
939 test_read_guest_mem
, &opts
);
942 qos_add_test("vhost-user/migrate",
944 test_migrate
, &opts
);
946 /* keeps failing on build-system since Aug 15 2017 */
947 if (getenv("QTEST_VHOST_USER_FIXME")) {
948 opts
.before
= vhost_user_test_setup_reconnect
;
949 qos_add_test("vhost-user/reconnect", "virtio-net",
950 test_reconnect
, &opts
);
952 opts
.before
= vhost_user_test_setup_connect_fail
;
953 qos_add_test("vhost-user/connect-fail", "virtio-net",
954 test_vhost_user_started
, &opts
);
956 opts
.before
= vhost_user_test_setup_flags_mismatch
;
957 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
958 test_vhost_user_started
, &opts
);
961 opts
.before
= vhost_user_test_setup_multiqueue
;
962 opts
.edge
.extra_device_opts
= "mq=on";
963 qos_add_test("vhost-user/multiqueue",
965 test_multiqueue
, &opts
);
967 libqos_init(register_vhost_user_test
);