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 "qemu/option.h"
15 #include "qemu/range.h"
16 #include "qemu/sockets.h"
17 #include "sysemu/char.h"
18 #include "sysemu/sysemu.h"
19 #include "libqos/libqos.h"
20 #include "libqos/pci-pc.h"
21 #include "libqos/virtio-pci.h"
23 #include "libqos/pci-pc.h"
24 #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 /* GLIB version compatibility flags */
34 #if !GLIB_CHECK_VERSION(2, 26, 0)
35 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
38 #if GLIB_CHECK_VERSION(2, 28, 0)
39 #define HAVE_MONOTONIC_TIME
42 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
43 "mem-path=%s,share=on -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=net0,chardev=%s,vhostforce"
46 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
48 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
49 QEMU_CMD_NETDEV QEMU_CMD_NET
51 #define HUGETLBFS_MAGIC 0x958458f6
53 /*********** FROM hw/virtio/vhost-user.c *************************************/
55 #define VHOST_MEMORY_MAX_NREGIONS 8
57 #define VHOST_USER_F_PROTOCOL_FEATURES 30
58 #define VHOST_USER_PROTOCOL_F_MQ 0
59 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
61 #define VHOST_LOG_PAGE 0x1000
63 typedef enum VhostUserRequest
{
65 VHOST_USER_GET_FEATURES
= 1,
66 VHOST_USER_SET_FEATURES
= 2,
67 VHOST_USER_SET_OWNER
= 3,
68 VHOST_USER_RESET_OWNER
= 4,
69 VHOST_USER_SET_MEM_TABLE
= 5,
70 VHOST_USER_SET_LOG_BASE
= 6,
71 VHOST_USER_SET_LOG_FD
= 7,
72 VHOST_USER_SET_VRING_NUM
= 8,
73 VHOST_USER_SET_VRING_ADDR
= 9,
74 VHOST_USER_SET_VRING_BASE
= 10,
75 VHOST_USER_GET_VRING_BASE
= 11,
76 VHOST_USER_SET_VRING_KICK
= 12,
77 VHOST_USER_SET_VRING_CALL
= 13,
78 VHOST_USER_SET_VRING_ERR
= 14,
79 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
80 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
81 VHOST_USER_GET_QUEUE_NUM
= 17,
82 VHOST_USER_SET_VRING_ENABLE
= 18,
86 typedef struct VhostUserMemoryRegion
{
87 uint64_t guest_phys_addr
;
89 uint64_t userspace_addr
;
91 } VhostUserMemoryRegion
;
93 typedef struct VhostUserMemory
{
96 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
99 typedef struct VhostUserLog
{
101 uint64_t mmap_offset
;
104 typedef struct VhostUserMsg
{
105 VhostUserRequest request
;
107 #define VHOST_USER_VERSION_MASK (0x3)
108 #define VHOST_USER_REPLY_MASK (0x1<<2)
110 uint32_t size
; /* the following payload size */
112 #define VHOST_USER_VRING_IDX_MASK (0xff)
113 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
115 struct vhost_vring_state state
;
116 struct vhost_vring_addr addr
;
117 VhostUserMemory memory
;
120 } QEMU_PACKED VhostUserMsg
;
122 static VhostUserMsg m
__attribute__ ((unused
));
123 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
127 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
129 /* The version of the protocol we support */
130 #define VHOST_USER_VERSION (0x1)
131 /*****************************************************************************/
135 TEST_FLAGS_DISCONNECT
,
140 typedef struct TestServer
{
144 CharDriverState
*chr
;
146 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
147 VhostUserMemory memory
;
148 CompatGMutex data_mutex
;
149 CompatGCond data_cond
;
157 static const char *tmpfs
;
158 static const char *root
;
160 static void init_virtio_dev(TestServer
*s
)
163 QVirtioPCIDevice
*dev
;
166 bus
= qpci_init_pc(NULL
);
167 g_assert_nonnull(bus
);
169 dev
= qvirtio_pci_device_find(bus
, VIRTIO_ID_NET
);
170 g_assert_nonnull(dev
);
172 qvirtio_pci_device_enable(dev
);
173 qvirtio_reset(&qvirtio_pci
, &dev
->vdev
);
174 qvirtio_set_acknowledge(&qvirtio_pci
, &dev
->vdev
);
175 qvirtio_set_driver(&qvirtio_pci
, &dev
->vdev
);
177 features
= qvirtio_get_features(&qvirtio_pci
, &dev
->vdev
);
178 features
= features
& VIRTIO_NET_F_MAC
;
179 qvirtio_set_features(&qvirtio_pci
, &dev
->vdev
, features
);
181 qvirtio_set_driver_ok(&qvirtio_pci
, &dev
->vdev
);
184 static void wait_for_fds(TestServer
*s
)
188 g_mutex_lock(&s
->data_mutex
);
190 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
191 while (!s
->fds_num
) {
192 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
193 /* timeout has passed */
194 g_assert(s
->fds_num
);
199 /* check for sanity */
200 g_assert_cmpint(s
->fds_num
, >, 0);
201 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
203 g_mutex_unlock(&s
->data_mutex
);
206 static void read_guest_mem(const void *data
)
208 TestServer
*s
= (void *)data
;
215 g_mutex_lock(&s
->data_mutex
);
217 /* iterate all regions */
218 for (i
= 0; i
< s
->fds_num
; i
++) {
220 /* We'll check only the region statring at 0x0*/
221 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
225 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
227 size
= s
->memory
.regions
[i
].memory_size
+
228 s
->memory
.regions
[i
].mmap_offset
;
230 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
231 MAP_SHARED
, s
->fds
[i
], 0);
233 g_assert(guest_mem
!= MAP_FAILED
);
234 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
236 for (j
= 0; j
< 256; j
++) {
237 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
238 uint32_t b
= guest_mem
[j
];
240 g_assert_cmpint(a
, ==, b
);
243 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
246 g_mutex_unlock(&s
->data_mutex
);
249 static void *thread_function(void *data
)
251 GMainLoop
*loop
= data
;
252 g_main_loop_run(loop
);
256 static int chr_can_read(void *opaque
)
258 return VHOST_USER_HDR_SIZE
;
261 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
263 TestServer
*s
= opaque
;
264 CharDriverState
*chr
= s
->chr
;
266 uint8_t *p
= (uint8_t *) &msg
;
270 qemu_chr_disconnect(chr
);
271 /* now switch to non-failure */
272 s
->test_fail
= false;
275 if (size
!= VHOST_USER_HDR_SIZE
) {
276 g_test_message("Wrong message size received %d\n", size
);
280 g_mutex_lock(&s
->data_mutex
);
281 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
284 p
+= VHOST_USER_HDR_SIZE
;
285 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
286 if (size
!= msg
.size
) {
287 g_test_message("Wrong message size received %d != %d\n",
293 switch (msg
.request
) {
294 case VHOST_USER_GET_FEATURES
:
295 /* send back features to qemu */
296 msg
.flags
|= VHOST_USER_REPLY_MASK
;
297 msg
.size
= sizeof(m
.payload
.u64
);
298 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
299 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
301 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
303 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
305 s
->test_flags
= TEST_FLAGS_END
;
307 p
= (uint8_t *) &msg
;
308 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
311 case VHOST_USER_SET_FEATURES
:
312 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
314 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
315 qemu_chr_disconnect(chr
);
316 s
->test_flags
= TEST_FLAGS_BAD
;
320 case VHOST_USER_GET_PROTOCOL_FEATURES
:
321 /* send back features to qemu */
322 msg
.flags
|= VHOST_USER_REPLY_MASK
;
323 msg
.size
= sizeof(m
.payload
.u64
);
324 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
326 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
328 p
= (uint8_t *) &msg
;
329 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
332 case VHOST_USER_GET_VRING_BASE
:
333 /* send back vring base to qemu */
334 msg
.flags
|= VHOST_USER_REPLY_MASK
;
335 msg
.size
= sizeof(m
.payload
.state
);
336 msg
.payload
.state
.num
= 0;
337 p
= (uint8_t *) &msg
;
338 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
340 assert(msg
.payload
.state
.index
< s
->queues
* 2);
341 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
344 case VHOST_USER_SET_MEM_TABLE
:
345 /* received the mem table */
346 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
347 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
, G_N_ELEMENTS(s
->fds
));
349 /* signal the test that it can continue */
350 g_cond_signal(&s
->data_cond
);
353 case VHOST_USER_SET_VRING_KICK
:
354 case VHOST_USER_SET_VRING_CALL
:
356 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
358 * This is a non-blocking eventfd.
359 * The receive function forces it to be blocking,
360 * so revert it back to non-blocking.
362 qemu_set_nonblock(fd
);
365 case VHOST_USER_SET_LOG_BASE
:
366 if (s
->log_fd
!= -1) {
370 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
371 msg
.flags
|= VHOST_USER_REPLY_MASK
;
373 p
= (uint8_t *) &msg
;
374 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
376 g_cond_signal(&s
->data_cond
);
379 case VHOST_USER_SET_VRING_BASE
:
380 assert(msg
.payload
.state
.index
< s
->queues
* 2);
381 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
384 case VHOST_USER_GET_QUEUE_NUM
:
385 msg
.flags
|= VHOST_USER_REPLY_MASK
;
386 msg
.size
= sizeof(m
.payload
.u64
);
387 msg
.payload
.u64
= s
->queues
;
388 p
= (uint8_t *) &msg
;
389 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
396 g_mutex_unlock(&s
->data_mutex
);
399 static const char *init_hugepagefs(const char *path
)
404 if (access(path
, R_OK
| W_OK
| X_OK
)) {
405 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
410 ret
= statfs(path
, &fs
);
411 } while (ret
!= 0 && errno
== EINTR
);
414 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
418 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
419 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
426 static TestServer
*test_server_new(const gchar
*name
)
428 TestServer
*server
= g_new0(TestServer
, 1);
430 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
431 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
432 server
->chr_name
= g_strdup_printf("chr-%s", name
);
434 g_mutex_init(&server
->data_mutex
);
435 g_cond_init(&server
->data_cond
);
443 static void chr_event(void *opaque
, int event
)
445 TestServer
*s
= opaque
;
447 if (s
->test_flags
== TEST_FLAGS_END
&&
448 event
== CHR_EVENT_CLOSED
) {
449 s
->test_flags
= TEST_FLAGS_OK
;
453 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
457 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
458 server
->chr
= qemu_chr_new(server
->chr_name
, chr_path
, NULL
);
461 qemu_chr_add_handlers(server
->chr
, chr_can_read
, chr_read
,
465 static void test_server_listen(TestServer
*server
)
467 test_server_create_chr(server
, ",server,nowait");
470 static inline void test_server_connect(TestServer
*server
)
472 test_server_create_chr(server
, ",reconnect=1");
475 #define GET_QEMU_CMD(s) \
476 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
477 (s)->socket_path, "", (s)->chr_name)
479 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
480 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
481 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
483 static gboolean
_test_server_free(TestServer
*server
)
487 qemu_chr_delete(server
->chr
);
489 for (i
= 0; i
< server
->fds_num
; i
++) {
490 close(server
->fds
[i
]);
493 if (server
->log_fd
!= -1) {
494 close(server
->log_fd
);
497 unlink(server
->socket_path
);
498 g_free(server
->socket_path
);
500 unlink(server
->mig_path
);
501 g_free(server
->mig_path
);
503 g_free(server
->chr_name
);
509 static void test_server_free(TestServer
*server
)
511 g_idle_add((GSourceFunc
)_test_server_free
, server
);
514 static void wait_for_log_fd(TestServer
*s
)
518 g_mutex_lock(&s
->data_mutex
);
519 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
520 while (s
->log_fd
== -1) {
521 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
522 /* timeout has passed */
523 g_assert(s
->log_fd
!= -1);
528 g_mutex_unlock(&s
->data_mutex
);
531 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
539 /* iterate all regions */
540 for (i
= 0; i
< s
->fds_num
; i
++) {
542 /* We'll write only the region statring at 0x0 */
543 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
547 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
549 size
= s
->memory
.regions
[i
].memory_size
+
550 s
->memory
.regions
[i
].mmap_offset
;
552 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
553 MAP_SHARED
, s
->fds
[i
], 0);
555 g_assert(guest_mem
!= MAP_FAILED
);
556 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
558 for (j
= 0; j
< 256; j
++) {
559 guest_mem
[j
] = seed
+ j
;
562 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
567 static guint64
get_log_size(TestServer
*s
)
569 guint64 log_size
= 0;
572 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
573 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
574 guint64 last
= range_get_last(reg
->guest_phys_addr
,
576 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
582 typedef struct TestMigrateSource
{
589 test_migrate_source_check(GSource
*source
)
591 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
592 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
599 #if !GLIB_CHECK_VERSION(2,36,0)
600 /* this callback is unnecessary with glib >2.36, the default
601 * prepare for the source does the same */
603 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
610 GSourceFuncs test_migrate_source_funcs
= {
611 #if !GLIB_CHECK_VERSION(2,36,0)
612 .prepare
= test_migrate_source_prepare
,
614 .check
= test_migrate_source_check
,
617 static void test_migrate(void)
619 TestServer
*s
= test_server_new("src");
620 TestServer
*dest
= test_server_new("dest");
621 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
622 QTestState
*global
= global_qtest
, *from
, *to
;
629 test_server_listen(s
);
630 test_server_listen(dest
);
632 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
633 from
= qtest_start(cmd
);
638 size
= get_log_size(s
);
639 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
641 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
642 to
= qtest_init(cmd
);
645 source
= g_source_new(&test_migrate_source_funcs
,
646 sizeof(TestMigrateSource
));
647 ((TestMigrateSource
*)source
)->src
= s
;
648 ((TestMigrateSource
*)source
)->dest
= dest
;
649 g_source_attach(source
, NULL
);
651 /* slow down migration to have time to fiddle with log */
652 /* TODO: qtest could learn to break on some places */
653 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
654 "'arguments': { 'value': 10 } }");
655 g_assert(qdict_haskey(rsp
, "return"));
658 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
659 "'arguments': { 'uri': '%s' } }",
663 g_assert(qdict_haskey(rsp
, "return"));
668 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
669 g_assert(log
!= MAP_FAILED
);
671 /* modify first page */
672 write_guest_mem(s
, 0x42);
676 /* speed things up */
677 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
678 "'arguments': { 'value': 0 } }");
679 g_assert(qdict_haskey(rsp
, "return"));
682 qmp_eventwait("STOP");
685 qmp_eventwait("RESUME");
687 read_guest_mem(dest
);
689 g_source_destroy(source
);
690 g_source_unref(source
);
693 test_server_free(dest
);
698 global_qtest
= global
;
701 static void wait_for_rings_started(TestServer
*s
, size_t count
)
705 g_mutex_lock(&s
->data_mutex
);
706 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
707 while (ctpop64(s
->rings
) != count
) {
708 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
709 /* timeout has passed */
710 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
715 g_mutex_unlock(&s
->data_mutex
);
718 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
720 reconnect_cb(gpointer user_data
)
722 TestServer
*s
= user_data
;
724 qemu_chr_disconnect(s
->chr
);
730 connect_thread(gpointer data
)
732 TestServer
*s
= data
;
734 /* wait for qemu to start before first try, to avoid extra warnings */
735 g_usleep(G_USEC_PER_SEC
);
736 test_server_connect(s
);
741 static void test_reconnect_subprocess(void)
743 TestServer
*s
= test_server_new("reconnect");
746 g_thread_new("connect", connect_thread
, s
);
747 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
753 wait_for_rings_started(s
, 2);
758 g_idle_add(reconnect_cb
, s
);
760 wait_for_rings_started(s
, 2);
767 static void test_reconnect(void)
769 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
771 g_test_trap_subprocess(path
, 0, 0);
772 g_test_trap_assert_passed();
776 static void test_connect_fail_subprocess(void)
778 TestServer
*s
= test_server_new("connect-fail");
782 g_thread_new("connect", connect_thread
, s
);
783 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
789 wait_for_rings_started(s
, 2);
795 static void test_connect_fail(void)
797 gchar
*path
= g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
799 g_test_trap_subprocess(path
, 0, 0);
800 g_test_trap_assert_passed();
804 static void test_flags_mismatch_subprocess(void)
806 TestServer
*s
= test_server_new("flags-mismatch");
809 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
810 g_thread_new("connect", connect_thread
, s
);
811 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
817 wait_for_rings_started(s
, 2);
823 static void test_flags_mismatch(void)
825 gchar
*path
= g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
827 g_test_trap_subprocess(path
, 0, 0);
828 g_test_trap_assert_passed();
834 static QVirtioPCIDevice
*virtio_net_pci_init(QPCIBus
*bus
, int slot
)
836 QVirtioPCIDevice
*dev
;
838 dev
= qvirtio_pci_device_find(bus
, VIRTIO_ID_NET
);
839 g_assert(dev
!= NULL
);
840 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_NET
);
842 qvirtio_pci_device_enable(dev
);
843 qvirtio_reset(&qvirtio_pci
, &dev
->vdev
);
844 qvirtio_set_acknowledge(&qvirtio_pci
, &dev
->vdev
);
845 qvirtio_set_driver(&qvirtio_pci
, &dev
->vdev
);
850 static void driver_init(const QVirtioBus
*bus
, QVirtioDevice
*dev
)
854 features
= qvirtio_get_features(bus
, dev
);
855 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
856 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
857 (1u << VIRTIO_RING_F_EVENT_IDX
));
858 qvirtio_set_features(bus
, dev
, features
);
860 qvirtio_set_driver_ok(bus
, dev
);
863 #define PCI_SLOT 0x04
865 static void test_multiqueue(void)
867 const int queues
= 2;
868 TestServer
*s
= test_server_new("mq");
869 QVirtioPCIDevice
*dev
;
871 QVirtQueuePCI
*vq
[queues
* 2];
872 QGuestAllocator
*alloc
;
877 test_server_listen(s
);
879 cmd
= g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
880 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
881 512, 512, root
, s
->chr_name
,
882 s
->socket_path
, "", s
->chr_name
,
883 queues
, queues
* 2 + 2);
887 bus
= qpci_init_pc(NULL
);
888 dev
= virtio_net_pci_init(bus
, PCI_SLOT
);
890 alloc
= pc_alloc_init();
891 for (i
= 0; i
< queues
* 2; i
++) {
892 vq
[i
] = (QVirtQueuePCI
*)qvirtqueue_setup(&qvirtio_pci
, &dev
->vdev
,
896 driver_init(&qvirtio_pci
, &dev
->vdev
);
897 wait_for_rings_started(s
, queues
* 2);
900 for (i
= 0; i
< queues
* 2; i
++) {
901 qvirtqueue_cleanup(&qvirtio_pci
, &vq
[i
]->vq
, alloc
);
903 pc_alloc_uninit(alloc
);
904 qvirtio_pci_device_disable(dev
);
913 int main(int argc
, char **argv
)
915 QTestState
*s
= NULL
;
916 TestServer
*server
= NULL
;
918 char *qemu_cmd
= NULL
;
920 char template[] = "/tmp/vhost-test-XXXXXX";
924 g_test_init(&argc
, &argv
, NULL
);
926 module_call_init(MODULE_INIT_QOM
);
927 qemu_add_opts(&qemu_chardev_opts
);
929 tmpfs
= mkdtemp(template);
931 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
935 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
937 root
= init_hugepagefs(hugefs
);
943 server
= test_server_new("test");
944 test_server_listen(server
);
946 loop
= g_main_loop_new(NULL
, FALSE
);
947 /* run the main loop thread so the chardev may operate */
948 thread
= g_thread_new(NULL
, thread_function
, loop
);
950 qemu_cmd
= GET_QEMU_CMD(server
);
952 s
= qtest_start(qemu_cmd
);
954 init_virtio_dev(server
);
956 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
957 qtest_add_func("/vhost-user/migrate", test_migrate
);
958 qtest_add_func("/vhost-user/multiqueue", test_multiqueue
);
959 #ifdef 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
);
978 test_server_free(server
);
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);