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
,
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 TestServer
*s
= opaque
;
306 CharBackend
*chr
= &s
->chr
;
308 uint8_t *p
= (uint8_t *) &msg
;
312 qemu_chr_fe_disconnect(chr
);
313 /* now switch to non-failure */
314 s
->test_fail
= false;
317 if (size
!= VHOST_USER_HDR_SIZE
) {
318 g_test_message("Wrong message size received %d", size
);
322 g_mutex_lock(&s
->data_mutex
);
323 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
326 p
+= VHOST_USER_HDR_SIZE
;
327 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
328 if (size
!= msg
.size
) {
329 g_test_message("Wrong message size received %d != %d",
335 switch (msg
.request
) {
336 case VHOST_USER_GET_FEATURES
:
337 /* send back features to qemu */
338 msg
.flags
|= VHOST_USER_REPLY_MASK
;
339 msg
.size
= sizeof(m
.payload
.u64
);
340 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
341 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
343 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
345 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
347 s
->test_flags
= TEST_FLAGS_END
;
349 p
= (uint8_t *) &msg
;
350 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
353 case VHOST_USER_SET_FEATURES
:
354 if (s
->vu_ops
->set_features
) {
355 s
->vu_ops
->set_features(s
, chr
, &msg
);
359 case VHOST_USER_GET_PROTOCOL_FEATURES
:
360 if (s
->vu_ops
->get_protocol_features
) {
361 s
->vu_ops
->get_protocol_features(s
, chr
, &msg
);
365 case VHOST_USER_GET_VRING_BASE
:
366 /* send back vring base to qemu */
367 msg
.flags
|= VHOST_USER_REPLY_MASK
;
368 msg
.size
= sizeof(m
.payload
.state
);
369 msg
.payload
.state
.num
= 0;
370 p
= (uint8_t *) &msg
;
371 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
373 assert(msg
.payload
.state
.index
< s
->queues
* 2);
374 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
375 g_cond_broadcast(&s
->data_cond
);
378 case VHOST_USER_SET_MEM_TABLE
:
379 /* received the mem table */
380 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
381 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
382 G_N_ELEMENTS(s
->fds
));
384 /* signal the test that it can continue */
385 g_cond_broadcast(&s
->data_cond
);
388 case VHOST_USER_SET_VRING_KICK
:
389 case VHOST_USER_SET_VRING_CALL
:
391 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
393 * This is a non-blocking eventfd.
394 * The receive function forces it to be blocking,
395 * so revert it back to non-blocking.
397 qemu_set_nonblock(fd
);
400 case VHOST_USER_SET_LOG_BASE
:
401 if (s
->log_fd
!= -1) {
405 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
406 msg
.flags
|= VHOST_USER_REPLY_MASK
;
408 p
= (uint8_t *) &msg
;
409 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
411 g_cond_broadcast(&s
->data_cond
);
414 case VHOST_USER_SET_VRING_BASE
:
415 assert(msg
.payload
.state
.index
< s
->queues
* 2);
416 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
417 g_cond_broadcast(&s
->data_cond
);
420 case VHOST_USER_GET_QUEUE_NUM
:
421 msg
.flags
|= VHOST_USER_REPLY_MASK
;
422 msg
.size
= sizeof(m
.payload
.u64
);
423 msg
.payload
.u64
= s
->queues
;
424 p
= (uint8_t *) &msg
;
425 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
432 g_mutex_unlock(&s
->data_mutex
);
435 static const char *init_hugepagefs(void)
438 static const char *hugepagefs
;
439 const char *path
= getenv("QTEST_HUGETLBFS_PATH");
450 if (access(path
, R_OK
| W_OK
| X_OK
)) {
451 g_test_message("access on path (%s): %s", path
, strerror(errno
));
457 ret
= statfs(path
, &fs
);
458 } while (ret
!= 0 && errno
== EINTR
);
461 g_test_message("statfs on path (%s): %s", path
, strerror(errno
));
466 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
467 g_test_message("Warning: path not on HugeTLBFS: %s", path
);
479 static TestServer
*test_server_new(const gchar
*name
,
480 struct vhost_user_ops
*ops
)
482 TestServer
*server
= g_new0(TestServer
, 1);
483 char template[] = "/tmp/vhost-test-XXXXXX";
486 server
->context
= g_main_context_new();
487 server
->loop
= g_main_loop_new(server
->context
, FALSE
);
489 /* run the main loop thread so the chardev may operate */
490 server
->thread
= g_thread_new(NULL
, thread_function
, server
->loop
);
492 tmpfs
= mkdtemp(template);
494 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
498 server
->tmpfs
= g_strdup(tmpfs
);
499 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
500 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
501 server
->chr_name
= g_strdup_printf("chr-%s", name
);
503 g_mutex_init(&server
->data_mutex
);
504 g_cond_init(&server
->data_cond
);
508 server
->vu_ops
= ops
;
513 static void chr_event(void *opaque
, QEMUChrEvent event
)
515 TestServer
*s
= opaque
;
517 if (s
->test_flags
== TEST_FLAGS_END
&&
518 event
== CHR_EVENT_CLOSED
) {
519 s
->test_flags
= TEST_FLAGS_OK
;
523 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
528 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
529 chr
= qemu_chr_new(server
->chr_name
, chr_path
, server
->context
);
532 g_assert_nonnull(chr
);
533 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
534 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
535 chr_event
, NULL
, server
, server
->context
, true);
538 static void test_server_listen(TestServer
*server
)
540 test_server_create_chr(server
, ",server=on,wait=off");
543 static void test_server_free(TestServer
*server
)
547 /* finish the helper thread and dispatch pending sources */
548 g_main_loop_quit(server
->loop
);
549 g_thread_join(server
->thread
);
550 while (g_main_context_pending(NULL
)) {
551 g_main_context_iteration(NULL
, TRUE
);
554 unlink(server
->socket_path
);
555 g_free(server
->socket_path
);
557 unlink(server
->mig_path
);
558 g_free(server
->mig_path
);
560 ret
= rmdir(server
->tmpfs
);
562 g_test_message("unable to rmdir: path (%s): %s",
563 server
->tmpfs
, strerror(errno
));
565 g_free(server
->tmpfs
);
567 qemu_chr_fe_deinit(&server
->chr
, true);
569 for (i
= 0; i
< server
->fds_num
; i
++) {
570 close(server
->fds
[i
]);
573 if (server
->log_fd
!= -1) {
574 close(server
->log_fd
);
577 g_free(server
->chr_name
);
579 g_main_loop_unref(server
->loop
);
580 g_main_context_unref(server
->context
);
581 g_cond_clear(&server
->data_cond
);
582 g_mutex_clear(&server
->data_mutex
);
586 static void wait_for_log_fd(TestServer
*s
)
590 g_mutex_lock(&s
->data_mutex
);
591 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
592 while (s
->log_fd
== -1) {
593 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
594 /* timeout has passed */
595 g_assert(s
->log_fd
!= -1);
600 g_mutex_unlock(&s
->data_mutex
);
603 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
609 /* iterate all regions */
610 for (i
= 0; i
< s
->fds_num
; i
++) {
612 /* We'll write only the region statring at 0x0 */
613 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
617 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
619 size
= s
->memory
.regions
[i
].memory_size
+
620 s
->memory
.regions
[i
].mmap_offset
;
622 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
623 MAP_SHARED
, s
->fds
[i
], 0);
625 g_assert(guest_mem
!= MAP_FAILED
);
626 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
628 for (j
= 0; j
< 256; j
++) {
629 guest_mem
[j
] = seed
+ j
;
632 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
637 static guint64
get_log_size(TestServer
*s
)
639 guint64 log_size
= 0;
642 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
643 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
644 guint64 last
= range_get_last(reg
->guest_phys_addr
,
646 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
652 typedef struct TestMigrateSource
{
659 test_migrate_source_check(GSource
*source
)
661 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
662 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
669 GSourceFuncs test_migrate_source_funcs
= {
670 .check
= test_migrate_source_check
,
673 static void vhost_user_test_cleanup(void *s
)
675 TestServer
*server
= s
;
677 qos_invalidate_command_line();
678 test_server_free(server
);
681 static void *vhost_user_test_setup(GString
*cmd_line
, void *arg
)
683 TestServer
*server
= test_server_new("vhost-user-test", arg
);
684 test_server_listen(server
);
686 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_AUTO
);
687 server
->vu_ops
->append_opts(server
, cmd_line
, "");
689 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
694 static void *vhost_user_test_setup_memfd(GString
*cmd_line
, void *arg
)
696 TestServer
*server
= test_server_new("vhost-user-test", arg
);
697 test_server_listen(server
);
699 append_mem_opts(server
, cmd_line
, 256, TEST_MEMFD_YES
);
700 server
->vu_ops
->append_opts(server
, cmd_line
, "");
702 g_test_queue_destroy(vhost_user_test_cleanup
, server
);
707 static void test_read_guest_mem(void *obj
, void *arg
, QGuestAllocator
*alloc
)
709 TestServer
*server
= arg
;
711 if (!wait_for_fds(server
)) {
715 read_guest_mem_server(global_qtest
, server
);
718 static void test_migrate(void *obj
, void *arg
, QGuestAllocator
*alloc
)
722 GString
*dest_cmdline
;
730 if (!wait_for_fds(s
)) {
734 dest
= test_server_new("dest", s
->vu_ops
);
735 dest_cmdline
= g_string_new(qos_get_current_command_line());
736 uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
738 size
= get_log_size(s
);
739 g_assert_cmpint(size
, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
741 test_server_listen(dest
);
742 g_string_append_printf(dest_cmdline
, " -incoming %s", uri
);
743 append_mem_opts(dest
, dest_cmdline
, 256, TEST_MEMFD_AUTO
);
744 dest
->vu_ops
->append_opts(dest
, dest_cmdline
, "");
745 to
= qtest_init(dest_cmdline
->str
);
747 /* This would be where you call qos_allocate_objects(to, NULL), if you want
748 * to talk to the QVirtioNet object on the destination.
751 source
= g_source_new(&test_migrate_source_funcs
,
752 sizeof(TestMigrateSource
));
753 ((TestMigrateSource
*)source
)->src
= s
;
754 ((TestMigrateSource
*)source
)->dest
= dest
;
755 g_source_attach(source
, s
->context
);
757 /* slow down migration to have time to fiddle with log */
758 /* TODO: qtest could learn to break on some places */
759 rsp
= qmp("{ 'execute': 'migrate-set-parameters',"
760 "'arguments': { 'max-bandwidth': 10 } }");
761 g_assert(qdict_haskey(rsp
, "return"));
764 rsp
= qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri
);
765 g_assert(qdict_haskey(rsp
, "return"));
770 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
771 g_assert(log
!= MAP_FAILED
);
773 /* modify first page */
774 write_guest_mem(s
, 0x42);
778 /* speed things up */
779 rsp
= qmp("{ 'execute': 'migrate-set-parameters',"
780 "'arguments': { 'max-bandwidth': 0 } }");
781 g_assert(qdict_haskey(rsp
, "return"));
784 qmp_eventwait("STOP");
785 qtest_qmp_eventwait(to
, "RESUME");
787 g_assert(wait_for_fds(dest
));
788 read_guest_mem_server(to
, dest
);
790 g_source_destroy(source
);
791 g_source_unref(source
);
794 test_server_free(dest
);
796 g_string_free(dest_cmdline
, true);
799 static void wait_for_rings_started(TestServer
*s
, size_t count
)
803 g_mutex_lock(&s
->data_mutex
);
804 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
805 while (ctpop64(s
->rings
) != count
) {
806 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
807 /* timeout has passed */
808 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
813 g_mutex_unlock(&s
->data_mutex
);
816 static inline void test_server_connect(TestServer
*server
)
818 test_server_create_chr(server
, ",reconnect=1");
822 reconnect_cb(gpointer user_data
)
824 TestServer
*s
= user_data
;
826 qemu_chr_fe_disconnect(&s
->chr
);
832 connect_thread(gpointer data
)
834 TestServer
*s
= data
;
836 /* wait for qemu to start before first try, to avoid extra warnings */
837 g_usleep(G_USEC_PER_SEC
);
838 test_server_connect(s
);
843 static void *vhost_user_test_setup_reconnect(GString
*cmd_line
, void *arg
)
845 TestServer
*s
= test_server_new("reconnect", arg
);
847 g_thread_new("connect", connect_thread
, s
);
848 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
849 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
851 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
856 static void test_reconnect(void *obj
, void *arg
, QGuestAllocator
*alloc
)
861 if (!wait_for_fds(s
)) {
865 wait_for_rings_started(s
, 2);
870 src
= g_idle_source_new();
871 g_source_set_callback(src
, reconnect_cb
, s
, NULL
);
872 g_source_attach(src
, s
->context
);
874 g_assert(wait_for_fds(s
));
875 wait_for_rings_started(s
, 2);
878 static void *vhost_user_test_setup_connect_fail(GString
*cmd_line
, void *arg
)
880 TestServer
*s
= test_server_new("connect-fail", arg
);
884 g_thread_new("connect", connect_thread
, s
);
885 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
886 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
888 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
893 static void *vhost_user_test_setup_flags_mismatch(GString
*cmd_line
, void *arg
)
895 TestServer
*s
= test_server_new("flags-mismatch", arg
);
897 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
899 g_thread_new("connect", connect_thread
, s
);
900 append_mem_opts(s
, cmd_line
, 256, TEST_MEMFD_AUTO
);
901 s
->vu_ops
->append_opts(s
, cmd_line
, ",server=on");
903 g_test_queue_destroy(vhost_user_test_cleanup
, s
);
908 static void test_vhost_user_started(void *obj
, void *arg
, QGuestAllocator
*alloc
)
912 if (!wait_for_fds(s
)) {
915 wait_for_rings_started(s
, 2);
918 static void *vhost_user_test_setup_multiqueue(GString
*cmd_line
, void *arg
)
920 TestServer
*s
= vhost_user_test_setup(cmd_line
, arg
);
923 g_string_append_printf(cmd_line
,
924 " -set netdev.hs0.queues=%d"
925 " -global virtio-net-pci.vectors=%d",
926 s
->queues
, s
->queues
* 2 + 2);
931 static void test_multiqueue(void *obj
, void *arg
, QGuestAllocator
*alloc
)
935 wait_for_rings_started(s
, s
->queues
* 2);
938 static void vu_net_set_features(TestServer
*s
, CharBackend
*chr
,
941 g_assert_cmpint(msg
->payload
.u64
&
942 (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
), !=, 0ULL);
943 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
944 qemu_chr_fe_disconnect(chr
);
945 s
->test_flags
= TEST_FLAGS_BAD
;
949 static void vu_net_get_protocol_features(TestServer
*s
, CharBackend
*chr
,
952 /* send back features to qemu */
953 msg
->flags
|= VHOST_USER_REPLY_MASK
;
954 msg
->size
= sizeof(m
.payload
.u64
);
955 msg
->payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
956 msg
->payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN
;
958 msg
->payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
960 qemu_chr_fe_write_all(chr
, (uint8_t *)msg
, VHOST_USER_HDR_SIZE
+ msg
->size
);
963 /* Each VHOST-USER device should have its ops structure defined. */
964 static struct vhost_user_ops g_vu_net_ops
= {
965 .type
= VHOST_USER_NET
,
967 .append_opts
= append_vhost_net_opts
,
969 .set_features
= vu_net_set_features
,
970 .get_protocol_features
= vu_net_get_protocol_features
,
973 static void register_vhost_user_test(void)
975 QOSGraphTestOptions opts
= {
976 .before
= vhost_user_test_setup
,
978 .arg
= &g_vu_net_ops
,
981 qemu_add_opts(&qemu_chardev_opts
);
983 qos_add_test("vhost-user/read-guest-mem/memfile",
985 test_read_guest_mem
, &opts
);
987 if (qemu_memfd_check(MFD_ALLOW_SEALING
)) {
988 opts
.before
= vhost_user_test_setup_memfd
;
989 qos_add_test("vhost-user/read-guest-mem/memfd",
991 test_read_guest_mem
, &opts
);
994 qos_add_test("vhost-user/migrate",
996 test_migrate
, &opts
);
998 /* keeps failing on build-system since Aug 15 2017 */
999 if (getenv("QTEST_VHOST_USER_FIXME")) {
1000 opts
.before
= vhost_user_test_setup_reconnect
;
1001 qos_add_test("vhost-user/reconnect", "virtio-net",
1002 test_reconnect
, &opts
);
1004 opts
.before
= vhost_user_test_setup_connect_fail
;
1005 qos_add_test("vhost-user/connect-fail", "virtio-net",
1006 test_vhost_user_started
, &opts
);
1008 opts
.before
= vhost_user_test_setup_flags_mismatch
;
1009 qos_add_test("vhost-user/flags-mismatch", "virtio-net",
1010 test_vhost_user_started
, &opts
);
1013 opts
.before
= vhost_user_test_setup_multiqueue
;
1014 opts
.edge
.extra_device_opts
= "mq=on";
1015 qos_add_test("vhost-user/multiqueue",
1017 test_multiqueue
, &opts
);
1019 libqos_init(register_vhost_user_test
);