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 "sysemu/sysemu.h"
22 #include "libqos/libqos.h"
23 #include "libqos/pci-pc.h"
24 #include "libqos/virtio-pci.h"
26 #include "libqos/malloc-pc.h"
27 #include "hw/virtio/virtio-net.h"
29 #include <linux/vhost.h>
30 #include <linux/virtio_ids.h>
31 #include <linux/virtio_net.h>
34 /* GLIB version compatibility flags */
35 #if !GLIB_CHECK_VERSION(2, 26, 0)
36 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
39 #if GLIB_CHECK_VERSION(2, 28, 0)
40 #define HAVE_MONOTONIC_TIME
43 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
44 "mem-path=%s,share=on -numa node,memdev=mem"
45 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
46 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
47 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
49 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
50 QEMU_CMD_NETDEV QEMU_CMD_NET
52 #define GET_QEMU_CMD(s) \
53 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
54 (s)->socket_path, "", (s)->chr_name)
56 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
57 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
58 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
60 #define HUGETLBFS_MAGIC 0x958458f6
62 /*********** FROM hw/virtio/vhost-user.c *************************************/
64 #define VHOST_MEMORY_MAX_NREGIONS 8
65 #define VHOST_MAX_VIRTQUEUES 0x100
67 #define VHOST_USER_F_PROTOCOL_FEATURES 30
68 #define VHOST_USER_PROTOCOL_F_MQ 0
69 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
71 #define VHOST_LOG_PAGE 0x1000
73 typedef enum VhostUserRequest
{
75 VHOST_USER_GET_FEATURES
= 1,
76 VHOST_USER_SET_FEATURES
= 2,
77 VHOST_USER_SET_OWNER
= 3,
78 VHOST_USER_RESET_OWNER
= 4,
79 VHOST_USER_SET_MEM_TABLE
= 5,
80 VHOST_USER_SET_LOG_BASE
= 6,
81 VHOST_USER_SET_LOG_FD
= 7,
82 VHOST_USER_SET_VRING_NUM
= 8,
83 VHOST_USER_SET_VRING_ADDR
= 9,
84 VHOST_USER_SET_VRING_BASE
= 10,
85 VHOST_USER_GET_VRING_BASE
= 11,
86 VHOST_USER_SET_VRING_KICK
= 12,
87 VHOST_USER_SET_VRING_CALL
= 13,
88 VHOST_USER_SET_VRING_ERR
= 14,
89 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
90 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
91 VHOST_USER_GET_QUEUE_NUM
= 17,
92 VHOST_USER_SET_VRING_ENABLE
= 18,
96 typedef struct VhostUserMemoryRegion
{
97 uint64_t guest_phys_addr
;
99 uint64_t userspace_addr
;
100 uint64_t mmap_offset
;
101 } VhostUserMemoryRegion
;
103 typedef struct VhostUserMemory
{
106 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
109 typedef struct VhostUserLog
{
111 uint64_t mmap_offset
;
114 typedef struct VhostUserMsg
{
115 VhostUserRequest request
;
117 #define VHOST_USER_VERSION_MASK (0x3)
118 #define VHOST_USER_REPLY_MASK (0x1<<2)
120 uint32_t size
; /* the following payload size */
122 #define VHOST_USER_VRING_IDX_MASK (0xff)
123 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
125 struct vhost_vring_state state
;
126 struct vhost_vring_addr addr
;
127 VhostUserMemory memory
;
130 } QEMU_PACKED VhostUserMsg
;
132 static VhostUserMsg m
__attribute__ ((unused
));
133 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
137 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
139 /* The version of the protocol we support */
140 #define VHOST_USER_VERSION (0x1)
141 /*****************************************************************************/
145 TEST_FLAGS_DISCONNECT
,
150 typedef struct TestServer
{
152 QVirtioPCIDevice
*dev
;
153 QVirtQueue
*vq
[VHOST_MAX_VIRTQUEUES
];
159 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
160 VhostUserMemory memory
;
161 CompatGMutex data_mutex
;
162 CompatGCond data_cond
;
168 QGuestAllocator
*alloc
;
171 static TestServer
*test_server_new(const gchar
*name
);
172 static void test_server_free(TestServer
*server
);
173 static void test_server_listen(TestServer
*server
);
175 static const char *tmpfs
;
176 static const char *root
;
178 static void init_virtio_dev(TestServer
*s
, uint32_t features_mask
)
183 s
->bus
= qpci_init_pc(global_qtest
, NULL
);
184 g_assert_nonnull(s
->bus
);
186 s
->dev
= qvirtio_pci_device_find(s
->bus
, VIRTIO_ID_NET
);
187 g_assert_nonnull(s
->dev
);
189 qvirtio_pci_device_enable(s
->dev
);
190 qvirtio_reset(&s
->dev
->vdev
);
191 qvirtio_set_acknowledge(&s
->dev
->vdev
);
192 qvirtio_set_driver(&s
->dev
->vdev
);
194 s
->alloc
= pc_alloc_init(global_qtest
);
196 for (i
= 0; i
< s
->queues
* 2; i
++) {
197 s
->vq
[i
] = qvirtqueue_setup(&s
->dev
->vdev
, s
->alloc
, i
);
200 features
= qvirtio_get_features(&s
->dev
->vdev
);
201 features
= features
& features_mask
;
202 qvirtio_set_features(&s
->dev
->vdev
, features
);
204 qvirtio_set_driver_ok(&s
->dev
->vdev
);
207 static void uninit_virtio_dev(TestServer
*s
)
211 for (i
= 0; i
< s
->queues
* 2; i
++) {
212 qvirtqueue_cleanup(s
->dev
->vdev
.bus
, s
->vq
[i
], s
->alloc
);
214 pc_alloc_uninit(s
->alloc
);
216 qvirtio_pci_device_free(s
->dev
);
219 static void wait_for_fds(TestServer
*s
)
223 g_mutex_lock(&s
->data_mutex
);
225 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
226 while (!s
->fds_num
) {
227 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
228 /* timeout has passed */
229 g_assert(s
->fds_num
);
234 /* check for sanity */
235 g_assert_cmpint(s
->fds_num
, >, 0);
236 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
238 g_mutex_unlock(&s
->data_mutex
);
241 static void read_guest_mem_server(TestServer
*s
)
249 g_mutex_lock(&s
->data_mutex
);
251 /* iterate all regions */
252 for (i
= 0; i
< s
->fds_num
; i
++) {
254 /* We'll check only the region statring at 0x0*/
255 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
259 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
261 size
= s
->memory
.regions
[i
].memory_size
+
262 s
->memory
.regions
[i
].mmap_offset
;
264 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
265 MAP_SHARED
, s
->fds
[i
], 0);
267 g_assert(guest_mem
!= MAP_FAILED
);
268 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
270 for (j
= 0; j
< 256; j
++) {
271 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
272 uint32_t b
= guest_mem
[j
];
274 g_assert_cmpint(a
, ==, b
);
277 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
280 g_mutex_unlock(&s
->data_mutex
);
283 static void *thread_function(void *data
)
285 GMainLoop
*loop
= data
;
286 g_main_loop_run(loop
);
290 static int chr_can_read(void *opaque
)
292 return VHOST_USER_HDR_SIZE
;
295 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
297 TestServer
*s
= opaque
;
298 CharBackend
*chr
= &s
->chr
;
300 uint8_t *p
= (uint8_t *) &msg
;
304 qemu_chr_fe_disconnect(chr
);
305 /* now switch to non-failure */
306 s
->test_fail
= false;
309 if (size
!= VHOST_USER_HDR_SIZE
) {
310 g_test_message("Wrong message size received %d\n", size
);
314 g_mutex_lock(&s
->data_mutex
);
315 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
318 p
+= VHOST_USER_HDR_SIZE
;
319 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
320 if (size
!= msg
.size
) {
321 g_test_message("Wrong message size received %d != %d\n",
327 switch (msg
.request
) {
328 case VHOST_USER_GET_FEATURES
:
329 /* send back features to qemu */
330 msg
.flags
|= VHOST_USER_REPLY_MASK
;
331 msg
.size
= sizeof(m
.payload
.u64
);
332 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
333 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
335 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
337 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
339 s
->test_flags
= TEST_FLAGS_END
;
341 p
= (uint8_t *) &msg
;
342 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
345 case VHOST_USER_SET_FEATURES
:
346 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
348 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
349 qemu_chr_fe_disconnect(chr
);
350 s
->test_flags
= TEST_FLAGS_BAD
;
354 case VHOST_USER_GET_PROTOCOL_FEATURES
:
355 /* send back features to qemu */
356 msg
.flags
|= VHOST_USER_REPLY_MASK
;
357 msg
.size
= sizeof(m
.payload
.u64
);
358 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
360 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
362 p
= (uint8_t *) &msg
;
363 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
366 case VHOST_USER_GET_VRING_BASE
:
367 /* send back vring base to qemu */
368 msg
.flags
|= VHOST_USER_REPLY_MASK
;
369 msg
.size
= sizeof(m
.payload
.state
);
370 msg
.payload
.state
.num
= 0;
371 p
= (uint8_t *) &msg
;
372 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
374 assert(msg
.payload
.state
.index
< s
->queues
* 2);
375 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
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_signal(&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_signal(&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
;
419 case VHOST_USER_GET_QUEUE_NUM
:
420 msg
.flags
|= VHOST_USER_REPLY_MASK
;
421 msg
.size
= sizeof(m
.payload
.u64
);
422 msg
.payload
.u64
= s
->queues
;
423 p
= (uint8_t *) &msg
;
424 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
431 g_mutex_unlock(&s
->data_mutex
);
434 static const char *init_hugepagefs(const char *path
)
439 if (access(path
, R_OK
| W_OK
| X_OK
)) {
440 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
445 ret
= statfs(path
, &fs
);
446 } while (ret
!= 0 && errno
== EINTR
);
449 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
453 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
454 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
461 static TestServer
*test_server_new(const gchar
*name
)
463 TestServer
*server
= g_new0(TestServer
, 1);
465 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
466 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
467 server
->chr_name
= g_strdup_printf("chr-%s", name
);
469 g_mutex_init(&server
->data_mutex
);
470 g_cond_init(&server
->data_cond
);
478 static void chr_event(void *opaque
, int event
)
480 TestServer
*s
= opaque
;
482 if (s
->test_flags
== TEST_FLAGS_END
&&
483 event
== CHR_EVENT_CLOSED
) {
484 s
->test_flags
= TEST_FLAGS_OK
;
488 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
493 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
494 chr
= qemu_chr_new(server
->chr_name
, chr_path
);
497 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
498 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
499 chr_event
, NULL
, server
, NULL
, true);
502 static void test_server_listen(TestServer
*server
)
504 test_server_create_chr(server
, ",server,nowait");
507 static gboolean
_test_server_free(TestServer
*server
)
511 qemu_chr_fe_deinit(&server
->chr
, true);
513 for (i
= 0; i
< server
->fds_num
; i
++) {
514 close(server
->fds
[i
]);
517 if (server
->log_fd
!= -1) {
518 close(server
->log_fd
);
521 unlink(server
->socket_path
);
522 g_free(server
->socket_path
);
524 unlink(server
->mig_path
);
525 g_free(server
->mig_path
);
527 g_free(server
->chr_name
);
528 qpci_free_pc(server
->bus
);
535 static void test_server_free(TestServer
*server
)
537 g_idle_add((GSourceFunc
)_test_server_free
, server
);
540 static void wait_for_log_fd(TestServer
*s
)
544 g_mutex_lock(&s
->data_mutex
);
545 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
546 while (s
->log_fd
== -1) {
547 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
548 /* timeout has passed */
549 g_assert(s
->log_fd
!= -1);
554 g_mutex_unlock(&s
->data_mutex
);
557 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
565 /* iterate all regions */
566 for (i
= 0; i
< s
->fds_num
; i
++) {
568 /* We'll write only the region statring at 0x0 */
569 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
573 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
575 size
= s
->memory
.regions
[i
].memory_size
+
576 s
->memory
.regions
[i
].mmap_offset
;
578 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
579 MAP_SHARED
, s
->fds
[i
], 0);
581 g_assert(guest_mem
!= MAP_FAILED
);
582 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
584 for (j
= 0; j
< 256; j
++) {
585 guest_mem
[j
] = seed
+ j
;
588 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
593 static guint64
get_log_size(TestServer
*s
)
595 guint64 log_size
= 0;
598 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
599 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
600 guint64 last
= range_get_last(reg
->guest_phys_addr
,
602 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
608 typedef struct TestMigrateSource
{
615 test_migrate_source_check(GSource
*source
)
617 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
618 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
625 #if !GLIB_CHECK_VERSION(2,36,0)
626 /* this callback is unnecessary with glib >2.36, the default
627 * prepare for the source does the same */
629 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
636 GSourceFuncs test_migrate_source_funcs
= {
637 #if !GLIB_CHECK_VERSION(2,36,0)
638 .prepare
= test_migrate_source_prepare
,
640 .check
= test_migrate_source_check
,
643 static void test_read_guest_mem(void)
645 TestServer
*server
= NULL
;
646 char *qemu_cmd
= NULL
;
647 QTestState
*s
= NULL
;
649 server
= test_server_new("test");
650 test_server_listen(server
);
652 qemu_cmd
= GET_QEMU_CMD(server
);
654 s
= qtest_start(qemu_cmd
);
657 init_virtio_dev(server
, 1u << VIRTIO_NET_F_MAC
);
659 read_guest_mem_server(server
);
661 uninit_virtio_dev(server
);
664 test_server_free(server
);
667 static void test_migrate(void)
669 TestServer
*s
= test_server_new("src");
670 TestServer
*dest
= test_server_new("dest");
671 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
672 QTestState
*global
= global_qtest
, *from
, *to
;
679 test_server_listen(s
);
680 test_server_listen(dest
);
682 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
683 from
= qtest_start(cmd
);
686 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
688 size
= get_log_size(s
);
689 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
691 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
692 to
= qtest_init(cmd
);
695 source
= g_source_new(&test_migrate_source_funcs
,
696 sizeof(TestMigrateSource
));
697 ((TestMigrateSource
*)source
)->src
= s
;
698 ((TestMigrateSource
*)source
)->dest
= dest
;
699 g_source_attach(source
, NULL
);
701 /* slow down migration to have time to fiddle with log */
702 /* TODO: qtest could learn to break on some places */
703 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
704 "'arguments': { 'value': 10 } }");
705 g_assert(qdict_haskey(rsp
, "return"));
708 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
709 "'arguments': { 'uri': '%s' } }",
713 g_assert(qdict_haskey(rsp
, "return"));
718 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
719 g_assert(log
!= MAP_FAILED
);
721 /* modify first page */
722 write_guest_mem(s
, 0x42);
726 /* speed things up */
727 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
728 "'arguments': { 'value': 0 } }");
729 g_assert(qdict_haskey(rsp
, "return"));
732 qmp_eventwait("STOP");
735 qmp_eventwait("RESUME");
737 read_guest_mem_server(dest
);
739 uninit_virtio_dev(s
);
741 g_source_destroy(source
);
742 g_source_unref(source
);
745 test_server_free(dest
);
750 global_qtest
= global
;
753 static void wait_for_rings_started(TestServer
*s
, size_t count
)
757 g_mutex_lock(&s
->data_mutex
);
758 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
759 while (ctpop64(s
->rings
) != count
) {
760 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
761 /* timeout has passed */
762 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
767 g_mutex_unlock(&s
->data_mutex
);
770 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
771 static inline void test_server_connect(TestServer
*server
)
773 test_server_create_chr(server
, ",reconnect=1");
777 reconnect_cb(gpointer user_data
)
779 TestServer
*s
= user_data
;
781 qemu_chr_fe_disconnect(&s
->chr
);
787 connect_thread(gpointer data
)
789 TestServer
*s
= data
;
791 /* wait for qemu to start before first try, to avoid extra warnings */
792 g_usleep(G_USEC_PER_SEC
);
793 test_server_connect(s
);
798 static void test_reconnect_subprocess(void)
800 TestServer
*s
= test_server_new("reconnect");
803 g_thread_new("connect", connect_thread
, s
);
804 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
808 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
810 wait_for_rings_started(s
, 2);
815 g_idle_add(reconnect_cb
, s
);
817 wait_for_rings_started(s
, 2);
819 uninit_virtio_dev(s
);
826 static void test_reconnect(void)
828 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
830 g_test_trap_subprocess(path
, 0, 0);
831 g_test_trap_assert_passed();
835 static void test_connect_fail_subprocess(void)
837 TestServer
*s
= test_server_new("connect-fail");
841 g_thread_new("connect", connect_thread
, s
);
842 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
846 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
848 wait_for_rings_started(s
, 2);
850 uninit_virtio_dev(s
);
856 static void test_connect_fail(void)
858 gchar
*path
= g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
860 g_test_trap_subprocess(path
, 0, 0);
861 g_test_trap_assert_passed();
865 static void test_flags_mismatch_subprocess(void)
867 TestServer
*s
= test_server_new("flags-mismatch");
870 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
871 g_thread_new("connect", connect_thread
, s
);
872 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
876 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
878 wait_for_rings_started(s
, 2);
880 uninit_virtio_dev(s
);
886 static void test_flags_mismatch(void)
888 gchar
*path
= g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
890 g_test_trap_subprocess(path
, 0, 0);
891 g_test_trap_assert_passed();
897 static void test_multiqueue(void)
899 TestServer
*s
= test_server_new("mq");
901 uint32_t features_mask
= ~(QVIRTIO_F_BAD_FEATURE
|
902 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
903 (1u << VIRTIO_RING_F_EVENT_IDX
));
905 test_server_listen(s
);
907 cmd
= g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
908 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
909 512, 512, root
, s
->chr_name
,
910 s
->socket_path
, "", s
->chr_name
,
911 s
->queues
, s
->queues
* 2 + 2);
915 init_virtio_dev(s
, features_mask
);
917 wait_for_rings_started(s
, s
->queues
* 2);
919 uninit_virtio_dev(s
);
926 int main(int argc
, char **argv
)
930 char template[] = "/tmp/vhost-test-XXXXXX";
934 g_test_init(&argc
, &argv
, NULL
);
936 module_call_init(MODULE_INIT_QOM
);
937 qemu_add_opts(&qemu_chardev_opts
);
939 tmpfs
= mkdtemp(template);
941 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
945 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
947 root
= init_hugepagefs(hugefs
);
953 loop
= g_main_loop_new(NULL
, FALSE
);
954 /* run the main loop thread so the chardev may operate */
955 thread
= g_thread_new(NULL
, thread_function
, loop
);
957 qtest_add_func("/vhost-user/read-guest-mem", test_read_guest_mem
);
958 qtest_add_func("/vhost-user/migrate", test_migrate
);
959 qtest_add_func("/vhost-user/multiqueue", test_multiqueue
);
961 #if defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
962 /* keeps failing on build-system since Aug 15 2017 */
963 if (getenv("QTEST_VHOST_USER_FIXME")) {
964 qtest_add_func("/vhost-user/reconnect/subprocess",
965 test_reconnect_subprocess
);
966 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
967 qtest_add_func("/vhost-user/connect-fail/subprocess",
968 test_connect_fail_subprocess
);
969 qtest_add_func("/vhost-user/connect-fail", test_connect_fail
);
970 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
971 test_flags_mismatch_subprocess
);
972 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch
);
980 /* finish the helper thread and dispatch pending sources */
981 g_main_loop_quit(loop
);
982 g_thread_join(thread
);
983 while (g_main_context_pending(NULL
)) {
984 g_main_context_iteration (NULL
, TRUE
);
986 g_main_loop_unref(loop
);
990 g_test_message("unable to rmdir: path (%s): %s\n",
991 tmpfs
, strerror(errno
));
993 g_assert_cmpint(ret
, ==, 0);