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(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();
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 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
517 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
518 chr_event
, NULL
, server
, NULL
, true);
521 static void test_server_listen(TestServer
*server
)
523 test_server_create_chr(server
, ",server,nowait");
526 static gboolean
_test_server_free(TestServer
*server
)
530 qemu_chr_fe_deinit(&server
->chr
, true);
532 for (i
= 0; i
< server
->fds_num
; i
++) {
533 close(server
->fds
[i
]);
536 if (server
->log_fd
!= -1) {
537 close(server
->log_fd
);
540 unlink(server
->socket_path
);
541 g_free(server
->socket_path
);
543 unlink(server
->mig_path
);
544 g_free(server
->mig_path
);
546 g_free(server
->chr_name
);
547 qpci_free_pc(server
->bus
);
554 static void test_server_free(TestServer
*server
)
556 g_idle_add((GSourceFunc
)_test_server_free
, server
);
559 static void wait_for_log_fd(TestServer
*s
)
563 g_mutex_lock(&s
->data_mutex
);
564 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
565 while (s
->log_fd
== -1) {
566 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
567 /* timeout has passed */
568 g_assert(s
->log_fd
!= -1);
573 g_mutex_unlock(&s
->data_mutex
);
576 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
584 /* iterate all regions */
585 for (i
= 0; i
< s
->fds_num
; i
++) {
587 /* We'll write only the region statring at 0x0 */
588 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
592 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
594 size
= s
->memory
.regions
[i
].memory_size
+
595 s
->memory
.regions
[i
].mmap_offset
;
597 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
598 MAP_SHARED
, s
->fds
[i
], 0);
600 g_assert(guest_mem
!= MAP_FAILED
);
601 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
603 for (j
= 0; j
< 256; j
++) {
604 guest_mem
[j
] = seed
+ j
;
607 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
612 static guint64
get_log_size(TestServer
*s
)
614 guint64 log_size
= 0;
617 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
618 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
619 guint64 last
= range_get_last(reg
->guest_phys_addr
,
621 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
627 typedef struct TestMigrateSource
{
634 test_migrate_source_check(GSource
*source
)
636 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
637 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
644 #if !GLIB_CHECK_VERSION(2,36,0)
645 /* this callback is unnecessary with glib >2.36, the default
646 * prepare for the source does the same */
648 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
655 GSourceFuncs test_migrate_source_funcs
= {
656 #if !GLIB_CHECK_VERSION(2,36,0)
657 .prepare
= test_migrate_source_prepare
,
659 .check
= test_migrate_source_check
,
662 static void test_read_guest_mem(const void *arg
)
664 enum test_memfd memfd
= GPOINTER_TO_INT(arg
);
665 TestServer
*server
= NULL
;
666 char *qemu_cmd
= NULL
;
667 QTestState
*s
= NULL
;
669 server
= test_server_new("test");
670 test_server_listen(server
);
672 qemu_cmd
= get_qemu_cmd(server
, 512, memfd
, root
, "", "");
674 s
= qtest_start(qemu_cmd
);
677 init_virtio_dev(server
, 1u << VIRTIO_NET_F_MAC
);
679 read_guest_mem_server(server
);
681 uninit_virtio_dev(server
);
684 test_server_free(server
);
687 static void test_migrate(void)
689 TestServer
*s
= test_server_new("src");
690 TestServer
*dest
= test_server_new("dest");
691 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
692 QTestState
*global
= global_qtest
, *from
, *to
;
699 test_server_listen(s
);
700 test_server_listen(dest
);
702 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, "", "");
703 from
= qtest_start(cmd
);
706 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
708 size
= get_log_size(s
);
709 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
711 tmp
= g_strdup_printf(" -incoming %s", uri
);
712 cmd
= get_qemu_cmd(dest
, 2, TEST_MEMFD_AUTO
, root
, "", tmp
);
714 to
= qtest_init(cmd
);
717 source
= g_source_new(&test_migrate_source_funcs
,
718 sizeof(TestMigrateSource
));
719 ((TestMigrateSource
*)source
)->src
= s
;
720 ((TestMigrateSource
*)source
)->dest
= dest
;
721 g_source_attach(source
, NULL
);
723 /* slow down migration to have time to fiddle with log */
724 /* TODO: qtest could learn to break on some places */
725 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
726 "'arguments': { 'value': 10 } }");
727 g_assert(qdict_haskey(rsp
, "return"));
730 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
731 "'arguments': { 'uri': '%s' } }",
735 g_assert(qdict_haskey(rsp
, "return"));
740 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
741 g_assert(log
!= MAP_FAILED
);
743 /* modify first page */
744 write_guest_mem(s
, 0x42);
748 /* speed things up */
749 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
750 "'arguments': { 'value': 0 } }");
751 g_assert(qdict_haskey(rsp
, "return"));
754 qmp_eventwait("STOP");
757 qmp_eventwait("RESUME");
759 read_guest_mem_server(dest
);
761 uninit_virtio_dev(s
);
763 g_source_destroy(source
);
764 g_source_unref(source
);
767 test_server_free(dest
);
772 global_qtest
= global
;
775 static void wait_for_rings_started(TestServer
*s
, size_t count
)
779 g_mutex_lock(&s
->data_mutex
);
780 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
781 while (ctpop64(s
->rings
) != count
) {
782 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
783 /* timeout has passed */
784 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
789 g_mutex_unlock(&s
->data_mutex
);
792 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
793 static inline void test_server_connect(TestServer
*server
)
795 test_server_create_chr(server
, ",reconnect=1");
799 reconnect_cb(gpointer user_data
)
801 TestServer
*s
= user_data
;
803 qemu_chr_fe_disconnect(&s
->chr
);
809 connect_thread(gpointer data
)
811 TestServer
*s
= data
;
813 /* wait for qemu to start before first try, to avoid extra warnings */
814 g_usleep(G_USEC_PER_SEC
);
815 test_server_connect(s
);
820 static void test_reconnect_subprocess(void)
822 TestServer
*s
= test_server_new("reconnect");
825 g_thread_new("connect", connect_thread
, s
);
826 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, ",server", "");
830 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
832 wait_for_rings_started(s
, 2);
837 g_idle_add(reconnect_cb
, s
);
839 wait_for_rings_started(s
, 2);
841 uninit_virtio_dev(s
);
848 static void test_reconnect(void)
850 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
852 g_test_trap_subprocess(path
, 0, 0);
853 g_test_trap_assert_passed();
857 static void test_connect_fail_subprocess(void)
859 TestServer
*s
= test_server_new("connect-fail");
863 g_thread_new("connect", connect_thread
, s
);
864 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, ",server", "");
868 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
870 wait_for_rings_started(s
, 2);
872 uninit_virtio_dev(s
);
878 static void test_connect_fail(void)
880 gchar
*path
= g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
882 g_test_trap_subprocess(path
, 0, 0);
883 g_test_trap_assert_passed();
887 static void test_flags_mismatch_subprocess(void)
889 TestServer
*s
= test_server_new("flags-mismatch");
892 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
893 g_thread_new("connect", connect_thread
, s
);
894 cmd
= get_qemu_cmd(s
, 2, TEST_MEMFD_AUTO
, root
, ",server", "");
898 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
900 wait_for_rings_started(s
, 2);
902 uninit_virtio_dev(s
);
908 static void test_flags_mismatch(void)
910 gchar
*path
= g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
912 g_test_trap_subprocess(path
, 0, 0);
913 g_test_trap_assert_passed();
919 static void test_multiqueue(void)
921 TestServer
*s
= test_server_new("mq");
923 uint32_t features_mask
= ~(QVIRTIO_F_BAD_FEATURE
|
924 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
925 (1u << VIRTIO_RING_F_EVENT_IDX
));
927 test_server_listen(s
);
929 if (qemu_memfd_check()) {
930 cmd
= g_strdup_printf(
931 QEMU_CMD_MEMFD QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
932 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
933 512, 512, s
->chr_name
,
934 s
->socket_path
, "", s
->chr_name
,
935 s
->queues
, s
->queues
* 2 + 2);
937 cmd
= g_strdup_printf(
938 QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
939 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
940 512, 512, root
, s
->chr_name
,
941 s
->socket_path
, "", s
->chr_name
,
942 s
->queues
, s
->queues
* 2 + 2);
947 init_virtio_dev(s
, features_mask
);
949 wait_for_rings_started(s
, s
->queues
* 2);
951 uninit_virtio_dev(s
);
958 int main(int argc
, char **argv
)
962 char template[] = "/tmp/vhost-test-XXXXXX";
966 g_test_init(&argc
, &argv
, NULL
);
968 module_call_init(MODULE_INIT_QOM
);
969 qemu_add_opts(&qemu_chardev_opts
);
971 tmpfs
= mkdtemp(template);
973 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
977 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
979 root
= init_hugepagefs(hugefs
);
985 loop
= g_main_loop_new(NULL
, FALSE
);
986 /* run the main loop thread so the chardev may operate */
987 thread
= g_thread_new(NULL
, thread_function
, loop
);
989 if (qemu_memfd_check()) {
990 qtest_add_data_func("/vhost-user/read-guest-mem/memfd",
991 GINT_TO_POINTER(TEST_MEMFD_YES
),
992 test_read_guest_mem
);
994 qtest_add_data_func("/vhost-user/read-guest-mem/memfile",
995 GINT_TO_POINTER(TEST_MEMFD_NO
), test_read_guest_mem
);
996 qtest_add_func("/vhost-user/migrate", test_migrate
);
997 qtest_add_func("/vhost-user/multiqueue", test_multiqueue
);
999 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
1000 /* keeps failing on build-system since Aug 15 2017 */
1001 if (getenv("QTEST_VHOST_USER_FIXME")) {
1002 qtest_add_func("/vhost-user/reconnect/subprocess",
1003 test_reconnect_subprocess
);
1004 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
1005 qtest_add_func("/vhost-user/connect-fail/subprocess",
1006 test_connect_fail_subprocess
);
1007 qtest_add_func("/vhost-user/connect-fail", test_connect_fail
);
1008 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
1009 test_flags_mismatch_subprocess
);
1010 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch
);
1018 /* finish the helper thread and dispatch pending sources */
1019 g_main_loop_quit(loop
);
1020 g_thread_join(thread
);
1021 while (g_main_context_pending(NULL
)) {
1022 g_main_context_iteration (NULL
, TRUE
);
1024 g_main_loop_unref(loop
);
1028 g_test_message("unable to rmdir: path (%s): %s\n",
1029 tmpfs
, strerror(errno
));
1031 g_assert_cmpint(ret
, ==, 0);