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 <linux/vhost.h>
31 #include <linux/virtio_ids.h>
32 #include <linux/virtio_net.h>
35 /* GLIB version compatibility flags */
36 #if !GLIB_CHECK_VERSION(2, 26, 0)
37 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
40 #if GLIB_CHECK_VERSION(2, 28, 0)
41 #define HAVE_MONOTONIC_TIME
44 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
45 "mem-path=%s,share=on -numa node,memdev=mem"
46 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
47 " -numa node,memdev=mem"
48 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
49 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
50 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
52 #define HUGETLBFS_MAGIC 0x958458f6
54 /*********** FROM hw/virtio/vhost-user.c *************************************/
56 #define VHOST_MEMORY_MAX_NREGIONS 8
57 #define VHOST_MAX_VIRTQUEUES 0x100
59 #define VHOST_USER_F_PROTOCOL_FEATURES 30
60 #define VHOST_USER_PROTOCOL_F_MQ 0
61 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
63 #define VHOST_LOG_PAGE 0x1000
65 typedef enum VhostUserRequest
{
67 VHOST_USER_GET_FEATURES
= 1,
68 VHOST_USER_SET_FEATURES
= 2,
69 VHOST_USER_SET_OWNER
= 3,
70 VHOST_USER_RESET_OWNER
= 4,
71 VHOST_USER_SET_MEM_TABLE
= 5,
72 VHOST_USER_SET_LOG_BASE
= 6,
73 VHOST_USER_SET_LOG_FD
= 7,
74 VHOST_USER_SET_VRING_NUM
= 8,
75 VHOST_USER_SET_VRING_ADDR
= 9,
76 VHOST_USER_SET_VRING_BASE
= 10,
77 VHOST_USER_GET_VRING_BASE
= 11,
78 VHOST_USER_SET_VRING_KICK
= 12,
79 VHOST_USER_SET_VRING_CALL
= 13,
80 VHOST_USER_SET_VRING_ERR
= 14,
81 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
82 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
83 VHOST_USER_GET_QUEUE_NUM
= 17,
84 VHOST_USER_SET_VRING_ENABLE
= 18,
88 typedef struct VhostUserMemoryRegion
{
89 uint64_t guest_phys_addr
;
91 uint64_t userspace_addr
;
93 } VhostUserMemoryRegion
;
95 typedef struct VhostUserMemory
{
98 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
101 typedef struct VhostUserLog
{
103 uint64_t mmap_offset
;
106 typedef struct VhostUserMsg
{
107 VhostUserRequest request
;
109 #define VHOST_USER_VERSION_MASK (0x3)
110 #define VHOST_USER_REPLY_MASK (0x1<<2)
112 uint32_t size
; /* the following payload size */
114 #define VHOST_USER_VRING_IDX_MASK (0xff)
115 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
117 struct vhost_vring_state state
;
118 struct vhost_vring_addr addr
;
119 VhostUserMemory memory
;
122 } QEMU_PACKED VhostUserMsg
;
124 static VhostUserMsg m
__attribute__ ((unused
));
125 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
129 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
131 /* The version of the protocol we support */
132 #define VHOST_USER_VERSION (0x1)
133 /*****************************************************************************/
137 TEST_FLAGS_DISCONNECT
,
142 typedef struct TestServer
{
144 QVirtioPCIDevice
*dev
;
145 QVirtQueue
*vq
[VHOST_MAX_VIRTQUEUES
];
151 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
152 VhostUserMemory memory
;
153 CompatGMutex data_mutex
;
154 CompatGCond data_cond
;
160 QGuestAllocator
*alloc
;
163 static TestServer
*test_server_new(const gchar
*name
);
164 static void test_server_free(TestServer
*server
);
165 static void test_server_listen(TestServer
*server
);
167 static const char *tmpfs
;
168 static const char *root
;
176 static char *get_qemu_cmd(TestServer
*s
,
177 int mem
, enum test_memfd memfd
, const char *mem_path
,
178 const char *chr_opts
, const char *extra
)
180 if (memfd
== TEST_MEMFD_AUTO
&& qemu_memfd_check()) {
181 memfd
= TEST_MEMFD_YES
;
184 if (memfd
== TEST_MEMFD_YES
) {
185 return g_strdup_printf(QEMU_CMD_MEMFD QEMU_CMD_CHR
186 QEMU_CMD_NETDEV QEMU_CMD_NET
"%s", mem
, mem
,
187 s
->chr_name
, s
->socket_path
,
188 chr_opts
, s
->chr_name
, extra
);
190 return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR
191 QEMU_CMD_NETDEV QEMU_CMD_NET
"%s", mem
, mem
,
192 mem_path
, s
->chr_name
, s
->socket_path
,
193 chr_opts
, s
->chr_name
, extra
);
197 static void init_virtio_dev(TestServer
*s
, uint32_t features_mask
)
202 s
->bus
= qpci_init_pc(global_qtest
, NULL
);
203 g_assert_nonnull(s
->bus
);
205 s
->dev
= qvirtio_pci_device_find(s
->bus
, VIRTIO_ID_NET
);
206 g_assert_nonnull(s
->dev
);
208 qvirtio_pci_device_enable(s
->dev
);
209 qvirtio_reset(&s
->dev
->vdev
);
210 qvirtio_set_acknowledge(&s
->dev
->vdev
);
211 qvirtio_set_driver(&s
->dev
->vdev
);
213 s
->alloc
= pc_alloc_init(global_qtest
);
215 for (i
= 0; i
< s
->queues
* 2; i
++) {
216 s
->vq
[i
] = qvirtqueue_setup(&s
->dev
->vdev
, s
->alloc
, i
);
219 features
= qvirtio_get_features(&s
->dev
->vdev
);
220 features
= features
& features_mask
;
221 qvirtio_set_features(&s
->dev
->vdev
, features
);
223 qvirtio_set_driver_ok(&s
->dev
->vdev
);
226 static void uninit_virtio_dev(TestServer
*s
)
230 for (i
= 0; i
< s
->queues
* 2; i
++) {
231 qvirtqueue_cleanup(s
->dev
->vdev
.bus
, s
->vq
[i
], s
->alloc
);
233 pc_alloc_uninit(s
->alloc
);
235 qvirtio_pci_device_free(s
->dev
);
238 static void wait_for_fds(TestServer
*s
)
242 g_mutex_lock(&s
->data_mutex
);
244 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
245 while (!s
->fds_num
) {
246 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
247 /* timeout has passed */
248 g_assert(s
->fds_num
);
253 /* check for sanity */
254 g_assert_cmpint(s
->fds_num
, >, 0);
255 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
257 g_mutex_unlock(&s
->data_mutex
);
260 static void read_guest_mem_server(TestServer
*s
)
268 g_mutex_lock(&s
->data_mutex
);
270 /* iterate all regions */
271 for (i
= 0; i
< s
->fds_num
; i
++) {
273 /* We'll check only the region statring at 0x0*/
274 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
278 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
280 size
= s
->memory
.regions
[i
].memory_size
+
281 s
->memory
.regions
[i
].mmap_offset
;
283 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
284 MAP_SHARED
, s
->fds
[i
], 0);
286 g_assert(guest_mem
!= MAP_FAILED
);
287 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
289 for (j
= 0; j
< 256; j
++) {
290 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
291 uint32_t b
= guest_mem
[j
];
293 g_assert_cmpint(a
, ==, b
);
296 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
299 g_mutex_unlock(&s
->data_mutex
);
302 static void *thread_function(void *data
)
304 GMainLoop
*loop
= data
;
305 g_main_loop_run(loop
);
309 static int chr_can_read(void *opaque
)
311 return VHOST_USER_HDR_SIZE
;
314 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
316 TestServer
*s
= opaque
;
317 CharBackend
*chr
= &s
->chr
;
319 uint8_t *p
= (uint8_t *) &msg
;
323 qemu_chr_fe_disconnect(chr
);
324 /* now switch to non-failure */
325 s
->test_fail
= false;
328 if (size
!= VHOST_USER_HDR_SIZE
) {
329 g_test_message("Wrong message size received %d\n", size
);
333 g_mutex_lock(&s
->data_mutex
);
334 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
337 p
+= VHOST_USER_HDR_SIZE
;
338 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
339 if (size
!= msg
.size
) {
340 g_test_message("Wrong message size received %d != %d\n",
346 switch (msg
.request
) {
347 case VHOST_USER_GET_FEATURES
:
348 /* send back features to qemu */
349 msg
.flags
|= VHOST_USER_REPLY_MASK
;
350 msg
.size
= sizeof(m
.payload
.u64
);
351 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
352 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
354 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
356 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
358 s
->test_flags
= TEST_FLAGS_END
;
360 p
= (uint8_t *) &msg
;
361 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
364 case VHOST_USER_SET_FEATURES
:
365 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
367 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
368 qemu_chr_fe_disconnect(chr
);
369 s
->test_flags
= TEST_FLAGS_BAD
;
373 case VHOST_USER_GET_PROTOCOL_FEATURES
:
374 /* send back features to qemu */
375 msg
.flags
|= VHOST_USER_REPLY_MASK
;
376 msg
.size
= sizeof(m
.payload
.u64
);
377 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
379 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
381 p
= (uint8_t *) &msg
;
382 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
385 case VHOST_USER_GET_VRING_BASE
:
386 /* send back vring base to qemu */
387 msg
.flags
|= VHOST_USER_REPLY_MASK
;
388 msg
.size
= sizeof(m
.payload
.state
);
389 msg
.payload
.state
.num
= 0;
390 p
= (uint8_t *) &msg
;
391 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
393 assert(msg
.payload
.state
.index
< s
->queues
* 2);
394 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
397 case VHOST_USER_SET_MEM_TABLE
:
398 /* received the mem table */
399 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
400 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
401 G_N_ELEMENTS(s
->fds
));
403 /* signal the test that it can continue */
404 g_cond_signal(&s
->data_cond
);
407 case VHOST_USER_SET_VRING_KICK
:
408 case VHOST_USER_SET_VRING_CALL
:
410 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
412 * This is a non-blocking eventfd.
413 * The receive function forces it to be blocking,
414 * so revert it back to non-blocking.
416 qemu_set_nonblock(fd
);
419 case VHOST_USER_SET_LOG_BASE
:
420 if (s
->log_fd
!= -1) {
424 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
425 msg
.flags
|= VHOST_USER_REPLY_MASK
;
427 p
= (uint8_t *) &msg
;
428 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
430 g_cond_signal(&s
->data_cond
);
433 case VHOST_USER_SET_VRING_BASE
:
434 assert(msg
.payload
.state
.index
< s
->queues
* 2);
435 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
438 case VHOST_USER_GET_QUEUE_NUM
:
439 msg
.flags
|= VHOST_USER_REPLY_MASK
;
440 msg
.size
= sizeof(m
.payload
.u64
);
441 msg
.payload
.u64
= s
->queues
;
442 p
= (uint8_t *) &msg
;
443 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
450 g_mutex_unlock(&s
->data_mutex
);
453 static const char *init_hugepagefs(const char *path
)
458 if (access(path
, R_OK
| W_OK
| X_OK
)) {
459 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
464 ret
= statfs(path
, &fs
);
465 } while (ret
!= 0 && errno
== EINTR
);
468 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
472 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
473 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
480 static TestServer
*test_server_new(const gchar
*name
)
482 TestServer
*server
= g_new0(TestServer
, 1);
484 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
485 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
486 server
->chr_name
= g_strdup_printf("chr-%s", name
);
488 g_mutex_init(&server
->data_mutex
);
489 g_cond_init(&server
->data_cond
);
497 static void chr_event(void *opaque
, int event
)
499 TestServer
*s
= opaque
;
501 if (s
->test_flags
== TEST_FLAGS_END
&&
502 event
== CHR_EVENT_CLOSED
) {
503 s
->test_flags
= TEST_FLAGS_OK
;
507 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
512 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
513 chr
= qemu_chr_new(server
->chr_name
, chr_path
);
516 g_assert_nonnull(chr
);
517 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
518 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
519 chr_event
, NULL
, server
, NULL
, true);
522 static void test_server_listen(TestServer
*server
)
524 test_server_create_chr(server
, ",server,nowait");
527 static gboolean
_test_server_free(TestServer
*server
)
531 qemu_chr_fe_deinit(&server
->chr
, true);
533 for (i
= 0; i
< server
->fds_num
; i
++) {
534 close(server
->fds
[i
]);
537 if (server
->log_fd
!= -1) {
538 close(server
->log_fd
);
541 unlink(server
->socket_path
);
542 g_free(server
->socket_path
);
544 unlink(server
->mig_path
);
545 g_free(server
->mig_path
);
547 g_free(server
->chr_name
);
548 qpci_free_pc(server
->bus
);
555 static void test_server_free(TestServer
*server
)
557 g_idle_add((GSourceFunc
)_test_server_free
, server
);
560 static void wait_for_log_fd(TestServer
*s
)
564 g_mutex_lock(&s
->data_mutex
);
565 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
566 while (s
->log_fd
== -1) {
567 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
568 /* timeout has passed */
569 g_assert(s
->log_fd
!= -1);
574 g_mutex_unlock(&s
->data_mutex
);
577 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
585 /* iterate all regions */
586 for (i
= 0; i
< s
->fds_num
; i
++) {
588 /* We'll write only the region statring at 0x0 */
589 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
593 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
595 size
= s
->memory
.regions
[i
].memory_size
+
596 s
->memory
.regions
[i
].mmap_offset
;
598 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
599 MAP_SHARED
, s
->fds
[i
], 0);
601 g_assert(guest_mem
!= MAP_FAILED
);
602 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
604 for (j
= 0; j
< 256; j
++) {
605 guest_mem
[j
] = seed
+ j
;
608 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
613 static guint64
get_log_size(TestServer
*s
)
615 guint64 log_size
= 0;
618 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
619 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
620 guint64 last
= range_get_last(reg
->guest_phys_addr
,
622 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
628 typedef struct TestMigrateSource
{
635 test_migrate_source_check(GSource
*source
)
637 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
638 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
645 #if !GLIB_CHECK_VERSION(2,36,0)
646 /* this callback is unnecessary with glib >2.36, the default
647 * prepare for the source does the same */
649 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
656 GSourceFuncs test_migrate_source_funcs
= {
657 #if !GLIB_CHECK_VERSION(2,36,0)
658 .prepare
= test_migrate_source_prepare
,
660 .check
= test_migrate_source_check
,
663 static void test_read_guest_mem(const void *arg
)
665 enum test_memfd memfd
= GPOINTER_TO_INT(arg
);
666 TestServer
*server
= NULL
;
667 char *qemu_cmd
= NULL
;
668 QTestState
*s
= NULL
;
670 server
= test_server_new(memfd
== TEST_MEMFD_YES
?
671 "read-guest-memfd" : "read-guest-mem");
672 test_server_listen(server
);
674 qemu_cmd
= get_qemu_cmd(server
, 512, memfd
, root
, "", "");
676 s
= qtest_start(qemu_cmd
);
679 init_virtio_dev(server
, 1u << VIRTIO_NET_F_MAC
);
681 read_guest_mem_server(server
);
683 uninit_virtio_dev(server
);
686 test_server_free(server
);
689 static void test_migrate(void)
691 TestServer
*s
= test_server_new("src");
692 TestServer
*dest
= test_server_new("dest");
693 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
694 QTestState
*global
= global_qtest
, *from
, *to
;
701 test_server_listen(s
);
702 test_server_listen(dest
);
704 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, "", "");
705 from
= qtest_start(cmd
);
708 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
710 size
= get_log_size(s
);
711 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
713 tmp
= g_strdup_printf(" -incoming %s", uri
);
714 cmd
= get_qemu_cmd(dest
, 2, TEST_MEMFD_AUTO
, root
, "", tmp
);
716 to
= qtest_init(cmd
);
719 source
= g_source_new(&test_migrate_source_funcs
,
720 sizeof(TestMigrateSource
));
721 ((TestMigrateSource
*)source
)->src
= s
;
722 ((TestMigrateSource
*)source
)->dest
= dest
;
723 g_source_attach(source
, NULL
);
725 /* slow down migration to have time to fiddle with log */
726 /* TODO: qtest could learn to break on some places */
727 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
728 "'arguments': { 'value': 10 } }");
729 g_assert(qdict_haskey(rsp
, "return"));
732 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
733 "'arguments': { 'uri': '%s' } }",
737 g_assert(qdict_haskey(rsp
, "return"));
742 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
743 g_assert(log
!= MAP_FAILED
);
745 /* modify first page */
746 write_guest_mem(s
, 0x42);
750 /* speed things up */
751 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
752 "'arguments': { 'value': 0 } }");
753 g_assert(qdict_haskey(rsp
, "return"));
756 qmp_eventwait("STOP");
759 qmp_eventwait("RESUME");
761 read_guest_mem_server(dest
);
763 uninit_virtio_dev(s
);
765 g_source_destroy(source
);
766 g_source_unref(source
);
769 test_server_free(dest
);
774 global_qtest
= global
;
777 static void wait_for_rings_started(TestServer
*s
, size_t count
)
781 g_mutex_lock(&s
->data_mutex
);
782 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
783 while (ctpop64(s
->rings
) != count
) {
784 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
785 /* timeout has passed */
786 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
791 g_mutex_unlock(&s
->data_mutex
);
794 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
795 static inline void test_server_connect(TestServer
*server
)
797 test_server_create_chr(server
, ",reconnect=1");
801 reconnect_cb(gpointer user_data
)
803 TestServer
*s
= user_data
;
805 qemu_chr_fe_disconnect(&s
->chr
);
811 connect_thread(gpointer data
)
813 TestServer
*s
= data
;
815 /* wait for qemu to start before first try, to avoid extra warnings */
816 g_usleep(G_USEC_PER_SEC
);
817 test_server_connect(s
);
822 static void test_reconnect_subprocess(void)
824 TestServer
*s
= test_server_new("reconnect");
827 g_thread_new("connect", connect_thread
, s
);
828 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, ",server", "");
832 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
834 wait_for_rings_started(s
, 2);
839 g_idle_add(reconnect_cb
, s
);
841 wait_for_rings_started(s
, 2);
843 uninit_virtio_dev(s
);
850 static void test_reconnect(void)
852 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
854 g_test_trap_subprocess(path
, 0, 0);
855 g_test_trap_assert_passed();
859 static void test_connect_fail_subprocess(void)
861 TestServer
*s
= test_server_new("connect-fail");
865 g_thread_new("connect", connect_thread
, s
);
866 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, ",server", "");
870 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
872 wait_for_rings_started(s
, 2);
874 uninit_virtio_dev(s
);
880 static void test_connect_fail(void)
882 gchar
*path
= g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
884 g_test_trap_subprocess(path
, 0, 0);
885 g_test_trap_assert_passed();
889 static void test_flags_mismatch_subprocess(void)
891 TestServer
*s
= test_server_new("flags-mismatch");
894 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
895 g_thread_new("connect", connect_thread
, s
);
896 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, ",server", "");
900 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
902 wait_for_rings_started(s
, 2);
904 uninit_virtio_dev(s
);
910 static void test_flags_mismatch(void)
912 gchar
*path
= g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
914 g_test_trap_subprocess(path
, 0, 0);
915 g_test_trap_assert_passed();
921 static void test_multiqueue(void)
923 TestServer
*s
= test_server_new("mq");
925 uint32_t features_mask
= ~(QVIRTIO_F_BAD_FEATURE
|
926 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
927 (1u << VIRTIO_RING_F_EVENT_IDX
));
929 test_server_listen(s
);
931 if (qemu_memfd_check()) {
932 cmd
= g_strdup_printf(
933 QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
934 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
935 512, 512, s
->chr_name
,
936 s
->socket_path
, "", s
->chr_name
,
937 s
->queues
, s
->queues
* 2 + 2);
939 cmd
= g_strdup_printf(
940 QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
941 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
942 512, 512, root
, s
->chr_name
,
943 s
->socket_path
, "", s
->chr_name
,
944 s
->queues
, s
->queues
* 2 + 2);
949 init_virtio_dev(s
, features_mask
);
951 wait_for_rings_started(s
, s
->queues
* 2);
953 uninit_virtio_dev(s
);
960 int main(int argc
, char **argv
)
964 char template[] = "/tmp/vhost-test-XXXXXX";
968 g_test_init(&argc
, &argv
, NULL
);
970 module_call_init(MODULE_INIT_QOM
);
971 qemu_add_opts(&qemu_chardev_opts
);
973 tmpfs
= mkdtemp(template);
975 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
979 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
981 root
= init_hugepagefs(hugefs
);
987 loop
= g_main_loop_new(NULL
, FALSE
);
988 /* run the main loop thread so the chardev may operate */
989 thread
= g_thread_new(NULL
, thread_function
, loop
);
991 if (qemu_memfd_check()) {
992 qtest_add_data_func("/vhost-user/read-guest-mem/memfd",
993 GINT_TO_POINTER(TEST_MEMFD_YES
),
994 test_read_guest_mem
);
996 qtest_add_data_func("/vhost-user/read-guest-mem/memfile",
997 GINT_TO_POINTER(TEST_MEMFD_NO
), test_read_guest_mem
);
998 qtest_add_func("/vhost-user/migrate", test_migrate
);
999 qtest_add_func("/vhost-user/multiqueue", test_multiqueue
);
1001 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
1002 /* keeps failing on build-system since Aug 15 2017 */
1003 if (getenv("QTEST_VHOST_USER_FIXME")) {
1004 qtest_add_func("/vhost-user/reconnect/subprocess",
1005 test_reconnect_subprocess
);
1006 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
1007 qtest_add_func("/vhost-user/connect-fail/subprocess",
1008 test_connect_fail_subprocess
);
1009 qtest_add_func("/vhost-user/connect-fail", test_connect_fail
);
1010 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
1011 test_flags_mismatch_subprocess
);
1012 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch
);
1020 /* finish the helper thread and dispatch pending sources */
1021 g_main_loop_quit(loop
);
1022 g_thread_join(thread
);
1023 while (g_main_context_pending(NULL
)) {
1024 g_main_context_iteration (NULL
, TRUE
);
1026 g_main_loop_unref(loop
);
1030 g_test_message("unable to rmdir: path (%s): %s\n",
1031 tmpfs
, strerror(errno
));
1033 g_assert_cmpint(ret
, ==, 0);