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 "qemu/config-file.h"
16 #include "qemu/option.h"
17 #include "qemu/range.h"
18 #include "qemu/sockets.h"
19 #include "chardev/char-fe.h"
20 #include "sysemu/sysemu.h"
21 #include "libqos/libqos.h"
22 #include "libqos/pci-pc.h"
23 #include "libqos/virtio-pci.h"
25 #include "libqos/malloc-pc.h"
26 #include "hw/virtio/virtio-net.h"
28 #include <linux/vhost.h>
29 #include <linux/virtio_ids.h>
30 #include <linux/virtio_net.h>
33 #define VHOST_USER_NET_TESTS_WORKING 0 /* broken as of 2.10.0 */
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_CHR " -chardev socket,id=%s,path=%s%s"
47 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
48 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
50 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
51 QEMU_CMD_NETDEV QEMU_CMD_NET
53 #define HUGETLBFS_MAGIC 0x958458f6
55 /*********** FROM hw/virtio/vhost-user.c *************************************/
57 #define VHOST_MEMORY_MAX_NREGIONS 8
58 #define VHOST_MAX_VIRTQUEUES 0x100
60 #define VHOST_USER_F_PROTOCOL_FEATURES 30
61 #define VHOST_USER_PROTOCOL_F_MQ 0
62 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
64 #define VHOST_LOG_PAGE 0x1000
66 typedef enum VhostUserRequest
{
68 VHOST_USER_GET_FEATURES
= 1,
69 VHOST_USER_SET_FEATURES
= 2,
70 VHOST_USER_SET_OWNER
= 3,
71 VHOST_USER_RESET_OWNER
= 4,
72 VHOST_USER_SET_MEM_TABLE
= 5,
73 VHOST_USER_SET_LOG_BASE
= 6,
74 VHOST_USER_SET_LOG_FD
= 7,
75 VHOST_USER_SET_VRING_NUM
= 8,
76 VHOST_USER_SET_VRING_ADDR
= 9,
77 VHOST_USER_SET_VRING_BASE
= 10,
78 VHOST_USER_GET_VRING_BASE
= 11,
79 VHOST_USER_SET_VRING_KICK
= 12,
80 VHOST_USER_SET_VRING_CALL
= 13,
81 VHOST_USER_SET_VRING_ERR
= 14,
82 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
83 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
84 VHOST_USER_GET_QUEUE_NUM
= 17,
85 VHOST_USER_SET_VRING_ENABLE
= 18,
89 typedef struct VhostUserMemoryRegion
{
90 uint64_t guest_phys_addr
;
92 uint64_t userspace_addr
;
94 } VhostUserMemoryRegion
;
96 typedef struct VhostUserMemory
{
99 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
102 typedef struct VhostUserLog
{
104 uint64_t mmap_offset
;
107 typedef struct VhostUserMsg
{
108 VhostUserRequest request
;
110 #define VHOST_USER_VERSION_MASK (0x3)
111 #define VHOST_USER_REPLY_MASK (0x1<<2)
113 uint32_t size
; /* the following payload size */
115 #define VHOST_USER_VRING_IDX_MASK (0xff)
116 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
118 struct vhost_vring_state state
;
119 struct vhost_vring_addr addr
;
120 VhostUserMemory memory
;
123 } QEMU_PACKED VhostUserMsg
;
125 static VhostUserMsg m
__attribute__ ((unused
));
126 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
130 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
132 /* The version of the protocol we support */
133 #define VHOST_USER_VERSION (0x1)
134 /*****************************************************************************/
138 TEST_FLAGS_DISCONNECT
,
143 typedef struct TestServer
{
145 QVirtioPCIDevice
*dev
;
146 QVirtQueue
*vq
[VHOST_MAX_VIRTQUEUES
];
152 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
153 VhostUserMemory memory
;
154 CompatGMutex data_mutex
;
155 CompatGCond data_cond
;
161 QGuestAllocator
*alloc
;
164 static const char *tmpfs
;
165 static const char *root
;
167 static void init_virtio_dev(TestServer
*s
, uint32_t features_mask
)
172 s
->bus
= qpci_init_pc(NULL
);
173 g_assert_nonnull(s
->bus
);
175 s
->dev
= qvirtio_pci_device_find(s
->bus
, VIRTIO_ID_NET
);
176 g_assert_nonnull(s
->dev
);
178 qvirtio_pci_device_enable(s
->dev
);
179 qvirtio_reset(&s
->dev
->vdev
);
180 qvirtio_set_acknowledge(&s
->dev
->vdev
);
181 qvirtio_set_driver(&s
->dev
->vdev
);
183 s
->alloc
= pc_alloc_init();
185 for (i
= 0; i
< s
->queues
* 2; i
++) {
186 s
->vq
[i
] = qvirtqueue_setup(&s
->dev
->vdev
, s
->alloc
, i
);
189 features
= qvirtio_get_features(&s
->dev
->vdev
);
190 features
= features
& features_mask
;
191 qvirtio_set_features(&s
->dev
->vdev
, features
);
193 qvirtio_set_driver_ok(&s
->dev
->vdev
);
196 static void uninit_virtio_dev(TestServer
*s
)
200 for (i
= 0; i
< s
->queues
* 2; i
++) {
201 qvirtqueue_cleanup(s
->dev
->vdev
.bus
, s
->vq
[i
], s
->alloc
);
203 pc_alloc_uninit(s
->alloc
);
205 qvirtio_pci_device_free(s
->dev
);
208 static void wait_for_fds(TestServer
*s
)
212 g_mutex_lock(&s
->data_mutex
);
214 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
215 while (!s
->fds_num
) {
216 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
217 /* timeout has passed */
218 g_assert(s
->fds_num
);
223 /* check for sanity */
224 g_assert_cmpint(s
->fds_num
, >, 0);
225 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
227 g_mutex_unlock(&s
->data_mutex
);
230 static void read_guest_mem(const void *data
)
232 TestServer
*s
= (void *)data
;
239 g_mutex_lock(&s
->data_mutex
);
241 /* iterate all regions */
242 for (i
= 0; i
< s
->fds_num
; i
++) {
244 /* We'll check only the region statring at 0x0*/
245 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
249 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
251 size
= s
->memory
.regions
[i
].memory_size
+
252 s
->memory
.regions
[i
].mmap_offset
;
254 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
255 MAP_SHARED
, s
->fds
[i
], 0);
257 g_assert(guest_mem
!= MAP_FAILED
);
258 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
260 for (j
= 0; j
< 256; j
++) {
261 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
262 uint32_t b
= guest_mem
[j
];
264 g_assert_cmpint(a
, ==, b
);
267 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
270 g_mutex_unlock(&s
->data_mutex
);
273 static void *thread_function(void *data
)
275 GMainLoop
*loop
= data
;
276 g_main_loop_run(loop
);
280 static int chr_can_read(void *opaque
)
282 return VHOST_USER_HDR_SIZE
;
285 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
287 TestServer
*s
= opaque
;
288 CharBackend
*chr
= &s
->chr
;
290 uint8_t *p
= (uint8_t *) &msg
;
294 qemu_chr_fe_disconnect(chr
);
295 /* now switch to non-failure */
296 s
->test_fail
= false;
299 if (size
!= VHOST_USER_HDR_SIZE
) {
300 g_test_message("Wrong message size received %d\n", size
);
304 g_mutex_lock(&s
->data_mutex
);
305 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
308 p
+= VHOST_USER_HDR_SIZE
;
309 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
310 if (size
!= msg
.size
) {
311 g_test_message("Wrong message size received %d != %d\n",
317 switch (msg
.request
) {
318 case VHOST_USER_GET_FEATURES
:
319 /* send back features to qemu */
320 msg
.flags
|= VHOST_USER_REPLY_MASK
;
321 msg
.size
= sizeof(m
.payload
.u64
);
322 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
323 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
325 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
327 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
329 s
->test_flags
= TEST_FLAGS_END
;
331 p
= (uint8_t *) &msg
;
332 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
335 case VHOST_USER_SET_FEATURES
:
336 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
338 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
339 qemu_chr_fe_disconnect(chr
);
340 s
->test_flags
= TEST_FLAGS_BAD
;
344 case VHOST_USER_GET_PROTOCOL_FEATURES
:
345 /* send back features to qemu */
346 msg
.flags
|= VHOST_USER_REPLY_MASK
;
347 msg
.size
= sizeof(m
.payload
.u64
);
348 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
350 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
352 p
= (uint8_t *) &msg
;
353 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
356 case VHOST_USER_GET_VRING_BASE
:
357 /* send back vring base to qemu */
358 msg
.flags
|= VHOST_USER_REPLY_MASK
;
359 msg
.size
= sizeof(m
.payload
.state
);
360 msg
.payload
.state
.num
= 0;
361 p
= (uint8_t *) &msg
;
362 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
364 assert(msg
.payload
.state
.index
< s
->queues
* 2);
365 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
368 case VHOST_USER_SET_MEM_TABLE
:
369 /* received the mem table */
370 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
371 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
372 G_N_ELEMENTS(s
->fds
));
374 /* signal the test that it can continue */
375 g_cond_signal(&s
->data_cond
);
378 case VHOST_USER_SET_VRING_KICK
:
379 case VHOST_USER_SET_VRING_CALL
:
381 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
383 * This is a non-blocking eventfd.
384 * The receive function forces it to be blocking,
385 * so revert it back to non-blocking.
387 qemu_set_nonblock(fd
);
390 case VHOST_USER_SET_LOG_BASE
:
391 if (s
->log_fd
!= -1) {
395 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
396 msg
.flags
|= VHOST_USER_REPLY_MASK
;
398 p
= (uint8_t *) &msg
;
399 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
401 g_cond_signal(&s
->data_cond
);
404 case VHOST_USER_SET_VRING_BASE
:
405 assert(msg
.payload
.state
.index
< s
->queues
* 2);
406 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
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(const char *path
)
429 if (access(path
, R_OK
| W_OK
| X_OK
)) {
430 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
435 ret
= statfs(path
, &fs
);
436 } while (ret
!= 0 && errno
== EINTR
);
439 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
443 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
444 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
451 static TestServer
*test_server_new(const gchar
*name
)
453 TestServer
*server
= g_new0(TestServer
, 1);
455 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
456 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
457 server
->chr_name
= g_strdup_printf("chr-%s", name
);
459 g_mutex_init(&server
->data_mutex
);
460 g_cond_init(&server
->data_cond
);
468 static void chr_event(void *opaque
, int event
)
470 TestServer
*s
= opaque
;
472 if (s
->test_flags
== TEST_FLAGS_END
&&
473 event
== CHR_EVENT_CLOSED
) {
474 s
->test_flags
= TEST_FLAGS_OK
;
478 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
483 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
484 chr
= qemu_chr_new(server
->chr_name
, chr_path
);
487 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
488 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
489 chr_event
, NULL
, server
, NULL
, true);
492 static void test_server_listen(TestServer
*server
)
494 test_server_create_chr(server
, ",server,nowait");
497 #define GET_QEMU_CMD(s) \
498 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
499 (s)->socket_path, "", (s)->chr_name)
501 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
502 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
503 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
505 static gboolean
_test_server_free(TestServer
*server
)
509 qemu_chr_fe_deinit(&server
->chr
, true);
511 for (i
= 0; i
< server
->fds_num
; i
++) {
512 close(server
->fds
[i
]);
515 if (server
->log_fd
!= -1) {
516 close(server
->log_fd
);
519 unlink(server
->socket_path
);
520 g_free(server
->socket_path
);
522 unlink(server
->mig_path
);
523 g_free(server
->mig_path
);
525 g_free(server
->chr_name
);
526 qpci_free_pc(server
->bus
);
533 static void test_server_free(TestServer
*server
)
535 g_idle_add((GSourceFunc
)_test_server_free
, server
);
538 static void wait_for_log_fd(TestServer
*s
)
542 g_mutex_lock(&s
->data_mutex
);
543 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
544 while (s
->log_fd
== -1) {
545 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
546 /* timeout has passed */
547 g_assert(s
->log_fd
!= -1);
552 g_mutex_unlock(&s
->data_mutex
);
555 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
563 /* iterate all regions */
564 for (i
= 0; i
< s
->fds_num
; i
++) {
566 /* We'll write only the region statring at 0x0 */
567 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
571 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
573 size
= s
->memory
.regions
[i
].memory_size
+
574 s
->memory
.regions
[i
].mmap_offset
;
576 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
577 MAP_SHARED
, s
->fds
[i
], 0);
579 g_assert(guest_mem
!= MAP_FAILED
);
580 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
582 for (j
= 0; j
< 256; j
++) {
583 guest_mem
[j
] = seed
+ j
;
586 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
591 static guint64
get_log_size(TestServer
*s
)
593 guint64 log_size
= 0;
596 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
597 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
598 guint64 last
= range_get_last(reg
->guest_phys_addr
,
600 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
606 typedef struct TestMigrateSource
{
613 test_migrate_source_check(GSource
*source
)
615 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
616 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
623 #if !GLIB_CHECK_VERSION(2,36,0)
624 /* this callback is unnecessary with glib >2.36, the default
625 * prepare for the source does the same */
627 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
634 GSourceFuncs test_migrate_source_funcs
= {
635 #if !GLIB_CHECK_VERSION(2,36,0)
636 .prepare
= test_migrate_source_prepare
,
638 .check
= test_migrate_source_check
,
641 static void test_read_guest_mem(void)
643 TestServer
*server
= NULL
;
644 char *qemu_cmd
= NULL
;
645 QTestState
*s
= NULL
;
647 server
= test_server_new("test");
648 test_server_listen(server
);
650 qemu_cmd
= GET_QEMU_CMD(server
);
652 s
= qtest_start(qemu_cmd
);
655 init_virtio_dev(server
, 1u << VIRTIO_NET_F_MAC
);
657 read_guest_mem(server
);
659 uninit_virtio_dev(server
);
662 test_server_free(server
);
665 static void test_migrate(void)
667 TestServer
*s
= test_server_new("src");
668 TestServer
*dest
= test_server_new("dest");
669 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
670 QTestState
*global
= global_qtest
, *from
, *to
;
677 test_server_listen(s
);
678 test_server_listen(dest
);
680 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
681 from
= qtest_start(cmd
);
684 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
686 size
= get_log_size(s
);
687 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
689 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
690 to
= qtest_init(cmd
);
693 source
= g_source_new(&test_migrate_source_funcs
,
694 sizeof(TestMigrateSource
));
695 ((TestMigrateSource
*)source
)->src
= s
;
696 ((TestMigrateSource
*)source
)->dest
= dest
;
697 g_source_attach(source
, NULL
);
699 /* slow down migration to have time to fiddle with log */
700 /* TODO: qtest could learn to break on some places */
701 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
702 "'arguments': { 'value': 10 } }");
703 g_assert(qdict_haskey(rsp
, "return"));
706 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
707 "'arguments': { 'uri': '%s' } }",
711 g_assert(qdict_haskey(rsp
, "return"));
716 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
717 g_assert(log
!= MAP_FAILED
);
719 /* modify first page */
720 write_guest_mem(s
, 0x42);
724 /* speed things up */
725 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
726 "'arguments': { 'value': 0 } }");
727 g_assert(qdict_haskey(rsp
, "return"));
730 qmp_eventwait("STOP");
733 qmp_eventwait("RESUME");
735 read_guest_mem(dest
);
737 uninit_virtio_dev(s
);
739 g_source_destroy(source
);
740 g_source_unref(source
);
743 test_server_free(dest
);
748 global_qtest
= global
;
751 static void wait_for_rings_started(TestServer
*s
, size_t count
)
755 g_mutex_lock(&s
->data_mutex
);
756 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
757 while (ctpop64(s
->rings
) != count
) {
758 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
759 /* timeout has passed */
760 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
765 g_mutex_unlock(&s
->data_mutex
);
768 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
769 static inline void test_server_connect(TestServer
*server
)
771 test_server_create_chr(server
, ",reconnect=1");
775 reconnect_cb(gpointer user_data
)
777 TestServer
*s
= user_data
;
779 qemu_chr_fe_disconnect(&s
->chr
);
785 connect_thread(gpointer data
)
787 TestServer
*s
= data
;
789 /* wait for qemu to start before first try, to avoid extra warnings */
790 g_usleep(G_USEC_PER_SEC
);
791 test_server_connect(s
);
796 static void test_reconnect_subprocess(void)
798 TestServer
*s
= test_server_new("reconnect");
801 g_thread_new("connect", connect_thread
, s
);
802 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
806 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
808 wait_for_rings_started(s
, 2);
813 g_idle_add(reconnect_cb
, s
);
815 wait_for_rings_started(s
, 2);
817 uninit_virtio_dev(s
);
824 static void test_reconnect(void)
826 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
828 g_test_trap_subprocess(path
, 0, 0);
829 g_test_trap_assert_passed();
833 static void test_connect_fail_subprocess(void)
835 TestServer
*s
= test_server_new("connect-fail");
839 g_thread_new("connect", connect_thread
, s
);
840 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
844 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
846 wait_for_rings_started(s
, 2);
848 uninit_virtio_dev(s
);
854 static void test_connect_fail(void)
856 gchar
*path
= g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
858 g_test_trap_subprocess(path
, 0, 0);
859 g_test_trap_assert_passed();
863 static void test_flags_mismatch_subprocess(void)
865 TestServer
*s
= test_server_new("flags-mismatch");
868 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
869 g_thread_new("connect", connect_thread
, s
);
870 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
874 init_virtio_dev(s
, 1u << VIRTIO_NET_F_MAC
);
876 wait_for_rings_started(s
, 2);
878 uninit_virtio_dev(s
);
884 static void test_flags_mismatch(void)
886 gchar
*path
= g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
888 g_test_trap_subprocess(path
, 0, 0);
889 g_test_trap_assert_passed();
895 static void test_multiqueue(void)
897 TestServer
*s
= test_server_new("mq");
899 uint32_t features_mask
= ~(QVIRTIO_F_BAD_FEATURE
|
900 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
901 (1u << VIRTIO_RING_F_EVENT_IDX
));
903 test_server_listen(s
);
905 cmd
= g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
906 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
907 512, 512, root
, s
->chr_name
,
908 s
->socket_path
, "", s
->chr_name
,
909 s
->queues
, s
->queues
* 2 + 2);
913 init_virtio_dev(s
, features_mask
);
915 wait_for_rings_started(s
, s
->queues
* 2);
917 uninit_virtio_dev(s
);
924 int main(int argc
, char **argv
)
928 char template[] = "/tmp/vhost-test-XXXXXX";
932 g_test_init(&argc
, &argv
, NULL
);
934 module_call_init(MODULE_INIT_QOM
);
935 qemu_add_opts(&qemu_chardev_opts
);
937 tmpfs
= mkdtemp(template);
939 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
943 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
945 root
= init_hugepagefs(hugefs
);
951 loop
= g_main_loop_new(NULL
, FALSE
);
952 /* run the main loop thread so the chardev may operate */
953 thread
= g_thread_new(NULL
, thread_function
, loop
);
955 qtest_add_func("/vhost-user/read-guest-mem", test_read_guest_mem
);
956 qtest_add_func("/vhost-user/migrate", test_migrate
);
957 qtest_add_func("/vhost-user/multiqueue", test_multiqueue
);
959 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
960 qtest_add_func("/vhost-user/reconnect/subprocess",
961 test_reconnect_subprocess
);
962 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
963 qtest_add_func("/vhost-user/connect-fail/subprocess",
964 test_connect_fail_subprocess
);
965 qtest_add_func("/vhost-user/connect-fail", test_connect_fail
);
966 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
967 test_flags_mismatch_subprocess
);
968 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch
);
975 /* finish the helper thread and dispatch pending sources */
976 g_main_loop_quit(loop
);
977 g_thread_join(thread
);
978 while (g_main_context_pending(NULL
)) {
979 g_main_context_iteration (NULL
, TRUE
);
981 g_main_loop_unref(loop
);
985 g_test_message("unable to rmdir: path (%s): %s\n",
986 tmpfs
, strerror(errno
));
988 g_assert_cmpint(ret
, ==, 0);