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"
24 #include "qapi/error.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 #define VHOST_USER_NET_TESTS_WORKING 0 /* broken as of 2.10.0 */
36 /* GLIB version compatibility flags */
37 #if !GLIB_CHECK_VERSION(2, 26, 0)
38 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
41 #if GLIB_CHECK_VERSION(2, 28, 0)
42 #define HAVE_MONOTONIC_TIME
45 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
46 "mem-path=%s,share=on -numa node,memdev=mem"
47 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
48 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
49 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
51 #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
52 QEMU_CMD_NETDEV QEMU_CMD_NET
54 #define HUGETLBFS_MAGIC 0x958458f6
56 /*********** FROM hw/virtio/vhost-user.c *************************************/
58 #define VHOST_MEMORY_MAX_NREGIONS 8
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
{
150 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
151 VhostUserMemory memory
;
152 CompatGMutex data_mutex
;
153 CompatGCond data_cond
;
161 static const char *tmpfs
;
162 static const char *root
;
164 static void init_virtio_dev(TestServer
*s
)
166 QVirtioPCIDevice
*dev
;
169 s
->bus
= qpci_init_pc(NULL
);
170 g_assert_nonnull(s
->bus
);
172 dev
= qvirtio_pci_device_find(s
->bus
, VIRTIO_ID_NET
);
173 g_assert_nonnull(dev
);
175 qvirtio_pci_device_enable(dev
);
176 qvirtio_reset(&dev
->vdev
);
177 qvirtio_set_acknowledge(&dev
->vdev
);
178 qvirtio_set_driver(&dev
->vdev
);
180 features
= qvirtio_get_features(&dev
->vdev
);
181 features
= features
& VIRTIO_NET_F_MAC
;
182 qvirtio_set_features(&dev
->vdev
, features
);
184 qvirtio_set_driver_ok(&dev
->vdev
);
185 qvirtio_pci_device_free(dev
);
188 static void wait_for_fds(TestServer
*s
)
192 g_mutex_lock(&s
->data_mutex
);
194 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
195 while (!s
->fds_num
) {
196 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
197 /* timeout has passed */
198 g_assert(s
->fds_num
);
203 /* check for sanity */
204 g_assert_cmpint(s
->fds_num
, >, 0);
205 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
207 g_mutex_unlock(&s
->data_mutex
);
210 static void read_guest_mem(const void *data
)
212 TestServer
*s
= (void *)data
;
219 g_mutex_lock(&s
->data_mutex
);
221 /* iterate all regions */
222 for (i
= 0; i
< s
->fds_num
; i
++) {
224 /* We'll check only the region statring at 0x0*/
225 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
229 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
231 size
= s
->memory
.regions
[i
].memory_size
+
232 s
->memory
.regions
[i
].mmap_offset
;
234 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
235 MAP_SHARED
, s
->fds
[i
], 0);
237 g_assert(guest_mem
!= MAP_FAILED
);
238 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
240 for (j
= 0; j
< 256; j
++) {
241 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
242 uint32_t b
= guest_mem
[j
];
244 g_assert_cmpint(a
, ==, b
);
247 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
250 g_mutex_unlock(&s
->data_mutex
);
253 static void *thread_function(void *data
)
255 GMainLoop
*loop
= data
;
256 g_main_loop_run(loop
);
260 static int chr_can_read(void *opaque
)
262 return VHOST_USER_HDR_SIZE
;
265 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
267 TestServer
*s
= opaque
;
268 CharBackend
*chr
= &s
->chr
;
270 uint8_t *p
= (uint8_t *) &msg
;
274 qemu_chr_fe_disconnect(chr
);
275 /* now switch to non-failure */
276 s
->test_fail
= false;
279 if (size
!= VHOST_USER_HDR_SIZE
) {
280 g_test_message("Wrong message size received %d\n", size
);
284 g_mutex_lock(&s
->data_mutex
);
285 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
288 p
+= VHOST_USER_HDR_SIZE
;
289 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
290 if (size
!= msg
.size
) {
291 g_test_message("Wrong message size received %d != %d\n",
297 switch (msg
.request
) {
298 case VHOST_USER_GET_FEATURES
:
299 /* send back features to qemu */
300 msg
.flags
|= VHOST_USER_REPLY_MASK
;
301 msg
.size
= sizeof(m
.payload
.u64
);
302 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
303 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
305 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
307 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
309 s
->test_flags
= TEST_FLAGS_END
;
311 p
= (uint8_t *) &msg
;
312 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
315 case VHOST_USER_SET_FEATURES
:
316 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
318 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
319 qemu_chr_fe_disconnect(chr
);
320 s
->test_flags
= TEST_FLAGS_BAD
;
324 case VHOST_USER_GET_PROTOCOL_FEATURES
:
325 /* send back features to qemu */
326 msg
.flags
|= VHOST_USER_REPLY_MASK
;
327 msg
.size
= sizeof(m
.payload
.u64
);
328 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
330 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
332 p
= (uint8_t *) &msg
;
333 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
336 case VHOST_USER_GET_VRING_BASE
:
337 /* send back vring base to qemu */
338 msg
.flags
|= VHOST_USER_REPLY_MASK
;
339 msg
.size
= sizeof(m
.payload
.state
);
340 msg
.payload
.state
.num
= 0;
341 p
= (uint8_t *) &msg
;
342 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
344 assert(msg
.payload
.state
.index
< s
->queues
* 2);
345 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
348 case VHOST_USER_SET_MEM_TABLE
:
349 /* received the mem table */
350 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
351 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
352 G_N_ELEMENTS(s
->fds
));
354 /* signal the test that it can continue */
355 g_cond_signal(&s
->data_cond
);
358 case VHOST_USER_SET_VRING_KICK
:
359 case VHOST_USER_SET_VRING_CALL
:
361 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
363 * This is a non-blocking eventfd.
364 * The receive function forces it to be blocking,
365 * so revert it back to non-blocking.
367 qemu_set_nonblock(fd
);
370 case VHOST_USER_SET_LOG_BASE
:
371 if (s
->log_fd
!= -1) {
375 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
376 msg
.flags
|= VHOST_USER_REPLY_MASK
;
378 p
= (uint8_t *) &msg
;
379 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
381 g_cond_signal(&s
->data_cond
);
384 case VHOST_USER_SET_VRING_BASE
:
385 assert(msg
.payload
.state
.index
< s
->queues
* 2);
386 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
389 case VHOST_USER_GET_QUEUE_NUM
:
390 msg
.flags
|= VHOST_USER_REPLY_MASK
;
391 msg
.size
= sizeof(m
.payload
.u64
);
392 msg
.payload
.u64
= s
->queues
;
393 p
= (uint8_t *) &msg
;
394 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
401 g_mutex_unlock(&s
->data_mutex
);
404 static const char *init_hugepagefs(const char *path
)
409 if (access(path
, R_OK
| W_OK
| X_OK
)) {
410 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
415 ret
= statfs(path
, &fs
);
416 } while (ret
!= 0 && errno
== EINTR
);
419 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
423 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
424 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
431 static TestServer
*test_server_new(const gchar
*name
)
433 TestServer
*server
= g_new0(TestServer
, 1);
435 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
436 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
437 server
->chr_name
= g_strdup_printf("chr-%s", name
);
439 g_mutex_init(&server
->data_mutex
);
440 g_cond_init(&server
->data_cond
);
448 static void chr_event(void *opaque
, int event
)
450 TestServer
*s
= opaque
;
452 if (s
->test_flags
== TEST_FLAGS_END
&&
453 event
== CHR_EVENT_CLOSED
) {
454 s
->test_flags
= TEST_FLAGS_OK
;
458 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
463 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
464 chr
= qemu_chr_new(server
->chr_name
, chr_path
);
467 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
468 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
469 chr_event
, NULL
, server
, NULL
, true);
472 static void test_server_listen(TestServer
*server
)
474 test_server_create_chr(server
, ",server,nowait");
477 #define GET_QEMU_CMD(s) \
478 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
479 (s)->socket_path, "", (s)->chr_name)
481 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
482 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
483 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
485 static gboolean
_test_server_free(TestServer
*server
)
489 qemu_chr_fe_deinit(&server
->chr
, true);
491 for (i
= 0; i
< server
->fds_num
; i
++) {
492 close(server
->fds
[i
]);
495 if (server
->log_fd
!= -1) {
496 close(server
->log_fd
);
499 unlink(server
->socket_path
);
500 g_free(server
->socket_path
);
502 unlink(server
->mig_path
);
503 g_free(server
->mig_path
);
505 g_free(server
->chr_name
);
506 qpci_free_pc(server
->bus
);
513 static void test_server_free(TestServer
*server
)
515 g_idle_add((GSourceFunc
)_test_server_free
, server
);
518 static void wait_for_log_fd(TestServer
*s
)
522 g_mutex_lock(&s
->data_mutex
);
523 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
524 while (s
->log_fd
== -1) {
525 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
526 /* timeout has passed */
527 g_assert(s
->log_fd
!= -1);
532 g_mutex_unlock(&s
->data_mutex
);
535 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
543 /* iterate all regions */
544 for (i
= 0; i
< s
->fds_num
; i
++) {
546 /* We'll write only the region statring at 0x0 */
547 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
551 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
553 size
= s
->memory
.regions
[i
].memory_size
+
554 s
->memory
.regions
[i
].mmap_offset
;
556 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
557 MAP_SHARED
, s
->fds
[i
], 0);
559 g_assert(guest_mem
!= MAP_FAILED
);
560 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
562 for (j
= 0; j
< 256; j
++) {
563 guest_mem
[j
] = seed
+ j
;
566 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
571 static guint64
get_log_size(TestServer
*s
)
573 guint64 log_size
= 0;
576 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
577 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
578 guint64 last
= range_get_last(reg
->guest_phys_addr
,
580 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
586 typedef struct TestMigrateSource
{
593 test_migrate_source_check(GSource
*source
)
595 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
596 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
603 #if !GLIB_CHECK_VERSION(2,36,0)
604 /* this callback is unnecessary with glib >2.36, the default
605 * prepare for the source does the same */
607 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
614 GSourceFuncs test_migrate_source_funcs
= {
615 #if !GLIB_CHECK_VERSION(2,36,0)
616 .prepare
= test_migrate_source_prepare
,
618 .check
= test_migrate_source_check
,
621 static void test_migrate(void)
623 TestServer
*s
= test_server_new("src");
624 TestServer
*dest
= test_server_new("dest");
625 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
626 QTestState
*global
= global_qtest
, *from
, *to
;
633 test_server_listen(s
);
634 test_server_listen(dest
);
636 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
637 from
= qtest_start(cmd
);
642 size
= get_log_size(s
);
643 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
645 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
646 to
= qtest_init(cmd
);
649 source
= g_source_new(&test_migrate_source_funcs
,
650 sizeof(TestMigrateSource
));
651 ((TestMigrateSource
*)source
)->src
= s
;
652 ((TestMigrateSource
*)source
)->dest
= dest
;
653 g_source_attach(source
, NULL
);
655 /* slow down migration to have time to fiddle with log */
656 /* TODO: qtest could learn to break on some places */
657 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
658 "'arguments': { 'value': 10 } }");
659 g_assert(qdict_haskey(rsp
, "return"));
662 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
663 "'arguments': { 'uri': '%s' } }",
667 g_assert(qdict_haskey(rsp
, "return"));
672 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
673 g_assert(log
!= MAP_FAILED
);
675 /* modify first page */
676 write_guest_mem(s
, 0x42);
680 /* speed things up */
681 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
682 "'arguments': { 'value': 0 } }");
683 g_assert(qdict_haskey(rsp
, "return"));
686 qmp_eventwait("STOP");
689 qmp_eventwait("RESUME");
691 read_guest_mem(dest
);
693 g_source_destroy(source
);
694 g_source_unref(source
);
697 test_server_free(dest
);
702 global_qtest
= global
;
705 static void wait_for_rings_started(TestServer
*s
, size_t count
)
709 g_mutex_lock(&s
->data_mutex
);
710 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
711 while (ctpop64(s
->rings
) != count
) {
712 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
713 /* timeout has passed */
714 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
719 g_mutex_unlock(&s
->data_mutex
);
722 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
723 static inline void test_server_connect(TestServer
*server
)
725 test_server_create_chr(server
, ",reconnect=1");
729 reconnect_cb(gpointer user_data
)
731 TestServer
*s
= user_data
;
733 qemu_chr_fe_disconnect(&s
->chr
);
739 connect_thread(gpointer data
)
741 TestServer
*s
= data
;
743 /* wait for qemu to start before first try, to avoid extra warnings */
744 g_usleep(G_USEC_PER_SEC
);
745 test_server_connect(s
);
750 static void test_reconnect_subprocess(void)
752 TestServer
*s
= test_server_new("reconnect");
755 g_thread_new("connect", connect_thread
, s
);
756 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
762 wait_for_rings_started(s
, 2);
767 g_idle_add(reconnect_cb
, s
);
769 wait_for_rings_started(s
, 2);
776 static void test_reconnect(void)
778 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
780 g_test_trap_subprocess(path
, 0, 0);
781 g_test_trap_assert_passed();
785 static void test_connect_fail_subprocess(void)
787 TestServer
*s
= test_server_new("connect-fail");
791 g_thread_new("connect", connect_thread
, s
);
792 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
798 wait_for_rings_started(s
, 2);
804 static void test_connect_fail(void)
806 gchar
*path
= g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
808 g_test_trap_subprocess(path
, 0, 0);
809 g_test_trap_assert_passed();
813 static void test_flags_mismatch_subprocess(void)
815 TestServer
*s
= test_server_new("flags-mismatch");
818 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
819 g_thread_new("connect", connect_thread
, s
);
820 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
826 wait_for_rings_started(s
, 2);
832 static void test_flags_mismatch(void)
834 gchar
*path
= g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
836 g_test_trap_subprocess(path
, 0, 0);
837 g_test_trap_assert_passed();
843 static QVirtioPCIDevice
*virtio_net_pci_init(QPCIBus
*bus
, int slot
)
845 QVirtioPCIDevice
*dev
;
847 dev
= qvirtio_pci_device_find(bus
, VIRTIO_ID_NET
);
848 g_assert(dev
!= NULL
);
849 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_NET
);
851 qvirtio_pci_device_enable(dev
);
852 qvirtio_reset(&dev
->vdev
);
853 qvirtio_set_acknowledge(&dev
->vdev
);
854 qvirtio_set_driver(&dev
->vdev
);
859 static void driver_init(QVirtioDevice
*dev
)
863 features
= qvirtio_get_features(dev
);
864 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
865 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
866 (1u << VIRTIO_RING_F_EVENT_IDX
));
867 qvirtio_set_features(dev
, features
);
869 qvirtio_set_driver_ok(dev
);
872 #define PCI_SLOT 0x04
874 static void test_multiqueue(void)
876 const int queues
= 2;
877 TestServer
*s
= test_server_new("mq");
878 QVirtioPCIDevice
*dev
;
880 QVirtQueuePCI
*vq
[queues
* 2];
881 QGuestAllocator
*alloc
;
886 test_server_listen(s
);
888 cmd
= g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
889 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
890 512, 512, root
, s
->chr_name
,
891 s
->socket_path
, "", s
->chr_name
,
892 queues
, queues
* 2 + 2);
896 bus
= qpci_init_pc(NULL
);
897 dev
= virtio_net_pci_init(bus
, PCI_SLOT
);
899 alloc
= pc_alloc_init();
900 for (i
= 0; i
< queues
* 2; i
++) {
901 vq
[i
] = (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, alloc
, i
);
904 driver_init(&dev
->vdev
);
905 wait_for_rings_started(s
, queues
* 2);
908 for (i
= 0; i
< queues
* 2; i
++) {
909 qvirtqueue_cleanup(dev
->vdev
.bus
, &vq
[i
]->vq
, alloc
);
911 pc_alloc_uninit(alloc
);
912 qvirtio_pci_device_disable(dev
);
921 int main(int argc
, char **argv
)
923 QTestState
*s
= NULL
;
924 TestServer
*server
= NULL
;
926 char *qemu_cmd
= NULL
;
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 server
= test_server_new("test");
952 test_server_listen(server
);
954 loop
= g_main_loop_new(NULL
, FALSE
);
955 /* run the main loop thread so the chardev may operate */
956 thread
= g_thread_new(NULL
, thread_function
, loop
);
958 qemu_cmd
= GET_QEMU_CMD(server
);
960 s
= qtest_start(qemu_cmd
);
962 init_virtio_dev(server
);
964 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
965 qtest_add_func("/vhost-user/migrate", test_migrate
);
966 qtest_add_func("/vhost-user/multiqueue", test_multiqueue
);
968 #if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
969 qtest_add_func("/vhost-user/reconnect/subprocess",
970 test_reconnect_subprocess
);
971 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
972 qtest_add_func("/vhost-user/connect-fail/subprocess",
973 test_connect_fail_subprocess
);
974 qtest_add_func("/vhost-user/connect-fail", test_connect_fail
);
975 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
976 test_flags_mismatch_subprocess
);
977 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch
);
987 test_server_free(server
);
989 /* finish the helper thread and dispatch pending sources */
990 g_main_loop_quit(loop
);
991 g_thread_join(thread
);
992 while (g_main_context_pending(NULL
)) {
993 g_main_context_iteration (NULL
, TRUE
);
995 g_main_loop_unref(loop
);
999 g_test_message("unable to rmdir: path (%s): %s\n",
1000 tmpfs
, strerror(errno
));
1002 g_assert_cmpint(ret
, ==, 0);