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 "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 "sysemu/sysemu.h"
23 #include "libqos/libqos.h"
24 #include "libqos/pci-pc.h"
25 #include "libqos/virtio-pci.h"
27 #include "libqos/malloc-pc.h"
28 #include "hw/virtio/virtio-net.h"
30 #include "standard-headers/linux/vhost_types.h"
31 #include "standard-headers/linux/virtio_ids.h"
32 #include "standard-headers/linux/virtio_net.h"
39 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
40 "mem-path=%s,share=on -numa node,memdev=mem"
41 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
42 " -numa node,memdev=mem"
43 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
44 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce"
46 #define HUGETLBFS_MAGIC 0x958458f6
48 /*********** FROM hw/virtio/vhost-user.c *************************************/
50 #define VHOST_MEMORY_MAX_NREGIONS 8
51 #define VHOST_MAX_VIRTQUEUES 0x100
53 #define VHOST_USER_F_PROTOCOL_FEATURES 30
54 #define VHOST_USER_PROTOCOL_F_MQ 0
55 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
56 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
58 #define VHOST_LOG_PAGE 0x1000
60 typedef enum VhostUserRequest
{
62 VHOST_USER_GET_FEATURES
= 1,
63 VHOST_USER_SET_FEATURES
= 2,
64 VHOST_USER_SET_OWNER
= 3,
65 VHOST_USER_RESET_OWNER
= 4,
66 VHOST_USER_SET_MEM_TABLE
= 5,
67 VHOST_USER_SET_LOG_BASE
= 6,
68 VHOST_USER_SET_LOG_FD
= 7,
69 VHOST_USER_SET_VRING_NUM
= 8,
70 VHOST_USER_SET_VRING_ADDR
= 9,
71 VHOST_USER_SET_VRING_BASE
= 10,
72 VHOST_USER_GET_VRING_BASE
= 11,
73 VHOST_USER_SET_VRING_KICK
= 12,
74 VHOST_USER_SET_VRING_CALL
= 13,
75 VHOST_USER_SET_VRING_ERR
= 14,
76 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
77 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
78 VHOST_USER_GET_QUEUE_NUM
= 17,
79 VHOST_USER_SET_VRING_ENABLE
= 18,
83 typedef struct VhostUserMemoryRegion
{
84 uint64_t guest_phys_addr
;
86 uint64_t userspace_addr
;
88 } VhostUserMemoryRegion
;
90 typedef struct VhostUserMemory
{
93 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
96 typedef struct VhostUserLog
{
101 typedef struct VhostUserMsg
{
102 VhostUserRequest request
;
104 #define VHOST_USER_VERSION_MASK (0x3)
105 #define VHOST_USER_REPLY_MASK (0x1<<2)
107 uint32_t size
; /* the following payload size */
109 #define VHOST_USER_VRING_IDX_MASK (0xff)
110 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
112 struct vhost_vring_state state
;
113 struct vhost_vring_addr addr
;
114 VhostUserMemory memory
;
117 } QEMU_PACKED VhostUserMsg
;
119 static VhostUserMsg m
__attribute__ ((unused
));
120 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
124 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
126 /* The version of the protocol we support */
127 #define VHOST_USER_VERSION (0x1)
128 /*****************************************************************************/
132 TEST_FLAGS_DISCONNECT
,
137 typedef struct TestServer
{
144 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
145 VhostUserMemory memory
;
146 GMainContext
*context
;
158 static const char *init_hugepagefs(void);
159 static TestServer
*test_server_new(const gchar
*name
);
160 static void test_server_free(TestServer
*server
);
161 static void test_server_listen(TestServer
*server
);
169 static void append_vhost_opts(TestServer
*s
, GString
*cmd_line
,
170 const char *chr_opts
)
172 g_string_append_printf(cmd_line
, QEMU_CMD_CHR QEMU_CMD_NETDEV
,
173 s
->chr_name
, s
->socket_path
,
174 chr_opts
, s
->chr_name
);
177 static void append_mem_opts(TestServer
*server
, GString
*cmd_line
,
178 int size
, enum test_memfd memfd
)
180 if (memfd
== TEST_MEMFD_AUTO
) {
181 memfd
= qemu_memfd_check(0) ? TEST_MEMFD_YES
: TEST_MEMFD_NO
;
184 if (memfd
== TEST_MEMFD_YES
) {
185 g_string_append_printf(cmd_line
, QEMU_CMD_MEMFD
, size
, size
);
187 const char *root
= init_hugepagefs() ? : server
->tmpfs
;
189 g_string_append_printf(cmd_line
, QEMU_CMD_MEM
, size
, size
, root
);
193 static bool wait_for_fds(TestServer
*s
)
199 g_mutex_lock(&s
->data_mutex
);
201 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
202 while (!s
->fds_num
) {
203 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
204 /* timeout has passed */
205 g_assert(s
->fds_num
);
210 /* check for sanity */
211 g_assert_cmpint(s
->fds_num
, >, 0);
212 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
214 g_mutex_unlock(&s
->data_mutex
);
217 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
218 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
219 if (reg
->guest_phys_addr
== 0) {
225 g_test_skip("No memory at address 0x0");
230 static void read_guest_mem_server(QTestState
*qts
, TestServer
*s
)
236 g_mutex_lock(&s
->data_mutex
);
238 /* iterate all regions */
239 for (i
= 0; i
< s
->fds_num
; i
++) {
241 /* We'll check only the region statring at 0x0*/
242 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
246 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
248 size
= s
->memory
.regions
[i
].memory_size
+
249 s
->memory
.regions
[i
].mmap_offset
;
251 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
252 MAP_SHARED
, s
->fds
[i
], 0);
254 g_assert(guest_mem
!= MAP_FAILED
);
255 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
257 for (j
= 0; j
< 1024; j
++) {
258 uint32_t a
= qtest_readb(qts
, s
->memory
.regions
[i
].guest_phys_addr
+ j
);
259 uint32_t b
= guest_mem
[j
];
261 g_assert_cmpint(a
, ==, b
);
264 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
267 g_mutex_unlock(&s
->data_mutex
);
270 static void *thread_function(void *data
)
272 GMainLoop
*loop
= data
;
273 g_main_loop_run(loop
);
277 static int chr_can_read(void *opaque
)
279 return VHOST_USER_HDR_SIZE
;
282 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
284 TestServer
*s
= opaque
;
285 CharBackend
*chr
= &s
->chr
;
287 uint8_t *p
= (uint8_t *) &msg
;
291 qemu_chr_fe_disconnect(chr
);
292 /* now switch to non-failure */
293 s
->test_fail
= false;
296 if (size
!= VHOST_USER_HDR_SIZE
) {
297 g_test_message("Wrong message size received %d", size
);
301 g_mutex_lock(&s
->data_mutex
);
302 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
305 p
+= VHOST_USER_HDR_SIZE
;
306 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
307 if (size
!= msg
.size
) {
308 g_test_message("Wrong message size received %d != %d",
314 switch (msg
.request
) {
315 case VHOST_USER_GET_FEATURES
:
316 /* send back features to qemu */
317 msg
.flags
|= VHOST_USER_REPLY_MASK
;
318 msg
.size
= sizeof(m
.payload
.u64
);
319 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
320 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
322 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
324 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
326 s
->test_flags
= TEST_FLAGS_END
;
328 p
= (uint8_t *) &msg
;
329 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
332 case VHOST_USER_SET_FEATURES
:
333 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
335 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
336 qemu_chr_fe_disconnect(chr
);
337 s
->test_flags
= TEST_FLAGS_BAD
;
341 case VHOST_USER_GET_PROTOCOL_FEATURES
:
342 /* send back features to qemu */
343 msg
.flags
|= VHOST_USER_REPLY_MASK
;
344 msg
.size
= sizeof(m
.payload
.u64
);
345 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
346 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
;
348 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
350 p
= (uint8_t *) &msg
;
351 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
354 case VHOST_USER_GET_VRING_BASE
:
355 /* send back vring base to qemu */
356 msg
.flags
|= VHOST_USER_REPLY_MASK
;
357 msg
.size
= sizeof(m
.payload
.state
);
358 msg
.payload
.state
.num
= 0;
359 p
= (uint8_t *) &msg
;
360 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
362 assert(msg
.payload
.state
.index
< s
->queues
* 2);
363 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
364 g_cond_broadcast(&s
->data_cond
);
367 case VHOST_USER_SET_MEM_TABLE
:
368 /* received the mem table */
369 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
370 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
371 G_N_ELEMENTS(s
->fds
));
373 /* signal the test that it can continue */
374 g_cond_broadcast(&s
->data_cond
);
377 case VHOST_USER_SET_VRING_KICK
:
378 case VHOST_USER_SET_VRING_CALL
:
380 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
382 * This is a non-blocking eventfd.
383 * The receive function forces it to be blocking,
384 * so revert it back to non-blocking.
386 qemu_set_nonblock(fd
);
389 case VHOST_USER_SET_LOG_BASE
:
390 if (s
->log_fd
!= -1) {
394 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
395 msg
.flags
|= VHOST_USER_REPLY_MASK
;
397 p
= (uint8_t *) &msg
;
398 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
400 g_cond_broadcast(&s
->data_cond
);
403 case VHOST_USER_SET_VRING_BASE
:
404 assert(msg
.payload
.state
.index
< s
->queues
* 2);
405 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
406 g_cond_broadcast(&s
->data_cond
);
409 case VHOST_USER_GET_QUEUE_NUM
:
410 msg
.flags
|= VHOST_USER_REPLY_MASK
;
411 msg
.size
= sizeof(m
.payload
.u64
);
412 msg
.payload
.u64
= s
->queues
;
413 p
= (uint8_t *) &msg
;
414 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
421 g_mutex_unlock(&s
->data_mutex
);
424 static const char *init_hugepagefs(void)
427 static const char *hugepagefs
;
428 const char *path
= getenv("QTEST_HUGETLBFS_PATH");
439 if (access(path
, R_OK
| W_OK
| X_OK
)) {
440 g_test_message("access on path (%s): %s", path
, strerror(errno
));
446 ret
= statfs(path
, &fs
);
447 } while (ret
!= 0 && errno
== EINTR
);
450 g_test_message("statfs on path (%s): %s", path
, strerror(errno
));
455 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
456 g_test_message("Warning: path not on HugeTLBFS: %s", path
);
468 static TestServer
*test_server_new(const gchar
*name
)
470 TestServer
*server
= g_new0(TestServer
, 1);
471 char template[] = "/tmp/vhost-test-XXXXXX";
474 server
->context
= g_main_context_new();
475 server
->loop
= g_main_loop_new(server
->context
, FALSE
);
477 /* run the main loop thread so the chardev may operate */
478 server
->thread
= g_thread_new(NULL
, thread_function
, server
->loop
);
480 tmpfs
= mkdtemp(template);
482 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
486 server
->tmpfs
= g_strdup(tmpfs
);
487 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
488 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
489 server
->chr_name
= g_strdup_printf("chr-%s", name
);
491 g_mutex_init(&server
->data_mutex
);
492 g_cond_init(&server
->data_cond
);
500 static void chr_event(void *opaque
, int event
)
502 TestServer
*s
= opaque
;
504 if (s
->test_flags
== TEST_FLAGS_END
&&
505 event
== CHR_EVENT_CLOSED
) {
506 s
->test_flags
= TEST_FLAGS_OK
;
510 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
515 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
516 chr
= qemu_chr_new(server
->chr_name
, chr_path
, server
->context
);
519 g_assert_nonnull(chr
);
520 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
521 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
522 chr_event
, NULL
, server
, server
->context
, true);
525 static void test_server_listen(TestServer
*server
)
527 test_server_create_chr(server
, ",server,nowait");
530 static void test_server_free(TestServer
*server
)
534 /* finish the helper thread and dispatch pending sources */
535 g_main_loop_quit(server
->loop
);
536 g_thread_join(server
->thread
);
537 while (g_main_context_pending(NULL
)) {
538 g_main_context_iteration(NULL
, TRUE
);
541 unlink(server
->socket_path
);
542 g_free(server
->socket_path
);
544 unlink(server
->mig_path
);
545 g_free(server
->mig_path
);
547 ret
= rmdir(server
->tmpfs
);
549 g_test_message("unable to rmdir: path (%s): %s",
550 server
->tmpfs
, strerror(errno
));
552 g_free(server
->tmpfs
);
554 qemu_chr_fe_deinit(&server
->chr
, true);
556 for (i
= 0; i
< server
->fds_num
; i
++) {
557 close(server
->fds
[i
]);
560 if (server
->log_fd
!= -1) {
561 close(server
->log_fd
);
564 g_free(server
->chr_name
);
566 g_main_loop_unref(server
->loop
);
567 g_main_context_unref(server
->context
);
568 g_cond_clear(&server
->data_cond
);
569 g_mutex_clear(&server
->data_mutex
);
573 static void wait_for_log_fd(TestServer
*s
)
577 g_mutex_lock(&s
->data_mutex
);
578 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
579 while (s
->log_fd
== -1) {
580 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
581 /* timeout has passed */
582 g_assert(s
->log_fd
!= -1);
587 g_mutex_unlock(&s
->data_mutex
);
590 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
596 /* iterate all regions */
597 for (i
= 0; i
< s
->fds_num
; i
++) {
599 /* We'll write only the region statring at 0x0 */
600 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
604 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
606 size
= s
->memory
.regions
[i
].memory_size
+
607 s
->memory
.regions
[i
].mmap_offset
;
609 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
610 MAP_SHARED
, s
->fds
[i
], 0);
612 g_assert(guest_mem
!= MAP_FAILED
);
613 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
615 for (j
= 0; j
< 256; j
++) {
616 guest_mem
[j
] = seed
+ j
;
619 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
624 static guint64
get_log_size(TestServer
*s
)
626 guint64 log_size
= 0;
629 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
630 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
631 guint64 last
= range_get_last(reg
->guest_phys_addr
,
633 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
639 typedef struct TestMigrateSource
{
646 test_migrate_source_check(GSource
*source
)
648 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
649 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
656 GSourceFuncs test_migrate_source_funcs
= {
657 .check
= test_migrate_source_check
,
660 static void vhost_user_test_cleanup(void *s
)
662 TestServer
*server
= s
;
664 qos_invalidate_command_line();
665 test_server_free(server
);
668 static void *vhost_user_test_setup(GString
*cmd_line
, void *arg
)
670 TestServer
*server
= test_server_new("vhost-user-test");
671 test_server_listen(server
);
673 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_AUTO
);
674 append_vhost_opts(server
, cmd_line
, "");
676 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
681 static void *vhost_user_test_setup_memfd(GString
*cmd_line
, void *arg
)
683 TestServer
*server
= test_server_new("vhost-user-test");
684 test_server_listen(server
);
686 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_YES
);
687 append_vhost_opts(server
, cmd_line
, "");
689 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
694 static void test_read_guest_mem(void *obj
, void *arg
, QGuestAllocator
*alloc
)
696 TestServer
*server
= arg
;
698 if (!wait_for_fds(server
)) {
702 read_guest_mem_server(global_qtest
, server
);
705 static void test_migrate(void *obj
, void *arg
, QGuestAllocator
*alloc
)
708 TestServer
*dest
= test_server_new("dest");
709 GString
*dest_cmdline
= g_string_new(qos_get_current_command_line());
710 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
717 if (!wait_for_fds(s
)) {
721 size
= get_log_size(s
);
722 g_assert_cmpint(size
, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
724 test_server_listen(dest
);
725 g_string_append_printf(dest_cmdline
, " -incoming %s", uri
);
726 append_mem_opts(dest
, dest_cmdline
, 256, TEST_MEMFD_AUTO
);
727 append_vhost_opts(dest
, dest_cmdline
, "");
728 to
= qtest_init(dest_cmdline
->str
);
730 /* This would be where you call qos_allocate_objects(to, NULL), if you want
731 * to talk to the QVirtioNet object on the destination.
734 source
= g_source_new(&test_migrate_source_funcs
,
735 sizeof(TestMigrateSource
));
736 ((TestMigrateSource
*)source
)->src
= s
;
737 ((TestMigrateSource
*)source
)->dest
= dest
;
738 g_source_attach(source
, s
->context
);
740 /* slow down migration to have time to fiddle with log */
741 /* TODO: qtest could learn to break on some places */
742 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
743 "'arguments': { 'value': 10 } }");
744 g_assert(qdict_haskey(rsp
, "return"));
747 rsp
= qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri
);
748 g_assert(qdict_haskey(rsp
, "return"));
753 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
754 g_assert(log
!= MAP_FAILED
);
756 /* modify first page */
757 write_guest_mem(s
, 0x42);
761 /* speed things up */
762 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
763 "'arguments': { 'value': 0 } }");
764 g_assert(qdict_haskey(rsp
, "return"));
767 qmp_eventwait("STOP");
768 qtest_qmp_eventwait(to
, "RESUME");
770 g_assert(wait_for_fds(dest
));
771 read_guest_mem_server(to
, dest
);
773 g_source_destroy(source
);
774 g_source_unref(source
);
777 test_server_free(dest
);
781 static void wait_for_rings_started(TestServer
*s
, size_t count
)
785 g_mutex_lock(&s
->data_mutex
);
786 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
787 while (ctpop64(s
->rings
) != count
) {
788 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
789 /* timeout has passed */
790 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
795 g_mutex_unlock(&s
->data_mutex
);
798 static inline void test_server_connect(TestServer
*server
)
800 test_server_create_chr(server
, ",reconnect=1");
804 reconnect_cb(gpointer user_data
)
806 TestServer
*s
= user_data
;
808 qemu_chr_fe_disconnect(&s
->chr
);
814 connect_thread(gpointer data
)
816 TestServer
*s
= data
;
818 /* wait for qemu to start before first try, to avoid extra warnings */
819 g_usleep(G_USEC_PER_SEC
);
820 test_server_connect(s
);
825 static void *vhost_user_test_setup_reconnect(GString
*cmd_line
, void *arg
)
827 TestServer
*s
= test_server_new("reconnect");
829 g_thread_new("connect", connect_thread
, s
);
830 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
831 append_vhost_opts(s
, cmd_line
, ",server");
833 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
838 static void test_reconnect(void *obj
, void *arg
, QGuestAllocator
*alloc
)
843 if (!wait_for_fds(s
)) {
847 wait_for_rings_started(s
, 2);
852 src
= g_idle_source_new();
853 g_source_set_callback(src
, reconnect_cb
, s
, NULL
);
854 g_source_attach(src
, s
->context
);
856 g_assert(wait_for_fds(s
));
857 wait_for_rings_started(s
, 2);
860 static void *vhost_user_test_setup_connect_fail(GString
*cmd_line
, void *arg
)
862 TestServer
*s
= test_server_new("connect-fail");
866 g_thread_new("connect", connect_thread
, s
);
867 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
868 append_vhost_opts(s
, cmd_line
, ",server");
870 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
875 static void *vhost_user_test_setup_flags_mismatch(GString
*cmd_line
, void *arg
)
877 TestServer
*s
= test_server_new("flags-mismatch");
879 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
881 g_thread_new("connect", connect_thread
, s
);
882 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
883 append_vhost_opts(s
, cmd_line
, ",server");
885 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
890 static void test_vhost_user_started(void *obj
, void *arg
, QGuestAllocator
*alloc
)
894 if (!wait_for_fds(s
)) {
897 wait_for_rings_started(s
, 2);
900 static void *vhost_user_test_setup_multiqueue(GString
*cmd_line
, void *arg
)
902 TestServer
*s
= vhost_user_test_setup(cmd_line
, arg
);
905 g_string_append_printf(cmd_line
,
906 " -set netdev.hs0.queues=%d"
907 " -global virtio-net-pci.vectors=%d",
908 s
->queues
, s
->queues
* 2 + 2);
913 static void test_multiqueue(void *obj
, void *arg
, QGuestAllocator
*alloc
)
917 wait_for_rings_started(s
, s
->queues
* 2);
920 static void register_vhost_user_test(void)
922 QOSGraphTestOptions opts
= {
923 .before
= vhost_user_test_setup
,
927 qemu_add_opts(&qemu_chardev_opts
);
929 qos_add_test("vhost-user/read-guest-mem/memfile",
931 test_read_guest_mem
, &opts
);
933 if (qemu_memfd_check(0)) {
934 opts
.before
= vhost_user_test_setup_memfd
;
935 qos_add_test("vhost-user/read-guest-mem/memfd",
937 test_read_guest_mem
, &opts
);
940 qos_add_test("vhost-user/migrate",
942 test_migrate
, &opts
);
944 /* keeps failing on build-system since Aug 15 2017 */
945 if (getenv("QTEST_VHOST_USER_FIXME")) {
946 opts
.before
= vhost_user_test_setup_reconnect
;
947 qos_add_test("vhost-user/reconnect", "virtio-net",
948 test_reconnect
, &opts
);
950 opts
.before
= vhost_user_test_setup_connect_fail
;
951 qos_add_test("vhost-user/connect-fail", "virtio-net",
952 test_vhost_user_started
, &opts
);
954 opts
.before
= vhost_user_test_setup_flags_mismatch
;
955 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
956 test_vhost_user_started
, &opts
);
959 opts
.before
= vhost_user_test_setup_multiqueue
;
960 opts
.edge
.extra_device_opts
= "mq=on";
961 qos_add_test("vhost-user/multiqueue",
963 test_multiqueue
, &opts
);
965 libqos_init(register_vhost_user_test
);