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 "sysemu/char.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 /* 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 HUGETLBFS_MAGIC 0x958458f6
54 /*********** FROM hw/virtio/vhost-user.c *************************************/
56 #define VHOST_MEMORY_MAX_NREGIONS 8
58 #define VHOST_USER_F_PROTOCOL_FEATURES 30
59 #define VHOST_USER_PROTOCOL_F_MQ 0
60 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
62 #define VHOST_LOG_PAGE 0x1000
64 typedef enum VhostUserRequest
{
66 VHOST_USER_GET_FEATURES
= 1,
67 VHOST_USER_SET_FEATURES
= 2,
68 VHOST_USER_SET_OWNER
= 3,
69 VHOST_USER_RESET_OWNER
= 4,
70 VHOST_USER_SET_MEM_TABLE
= 5,
71 VHOST_USER_SET_LOG_BASE
= 6,
72 VHOST_USER_SET_LOG_FD
= 7,
73 VHOST_USER_SET_VRING_NUM
= 8,
74 VHOST_USER_SET_VRING_ADDR
= 9,
75 VHOST_USER_SET_VRING_BASE
= 10,
76 VHOST_USER_GET_VRING_BASE
= 11,
77 VHOST_USER_SET_VRING_KICK
= 12,
78 VHOST_USER_SET_VRING_CALL
= 13,
79 VHOST_USER_SET_VRING_ERR
= 14,
80 VHOST_USER_GET_PROTOCOL_FEATURES
= 15,
81 VHOST_USER_SET_PROTOCOL_FEATURES
= 16,
82 VHOST_USER_GET_QUEUE_NUM
= 17,
83 VHOST_USER_SET_VRING_ENABLE
= 18,
87 typedef struct VhostUserMemoryRegion
{
88 uint64_t guest_phys_addr
;
90 uint64_t userspace_addr
;
92 } VhostUserMemoryRegion
;
94 typedef struct VhostUserMemory
{
97 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
100 typedef struct VhostUserLog
{
102 uint64_t mmap_offset
;
105 typedef struct VhostUserMsg
{
106 VhostUserRequest request
;
108 #define VHOST_USER_VERSION_MASK (0x3)
109 #define VHOST_USER_REPLY_MASK (0x1<<2)
111 uint32_t size
; /* the following payload size */
113 #define VHOST_USER_VRING_IDX_MASK (0xff)
114 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
116 struct vhost_vring_state state
;
117 struct vhost_vring_addr addr
;
118 VhostUserMemory memory
;
121 } QEMU_PACKED VhostUserMsg
;
123 static VhostUserMsg m
__attribute__ ((unused
));
124 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
128 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
130 /* The version of the protocol we support */
131 #define VHOST_USER_VERSION (0x1)
132 /*****************************************************************************/
136 TEST_FLAGS_DISCONNECT
,
141 typedef struct TestServer
{
148 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
149 VhostUserMemory memory
;
150 CompatGMutex data_mutex
;
151 CompatGCond data_cond
;
159 static const char *tmpfs
;
160 static const char *root
;
162 static void init_virtio_dev(TestServer
*s
)
164 QVirtioPCIDevice
*dev
;
167 s
->bus
= qpci_init_pc(NULL
);
168 g_assert_nonnull(s
->bus
);
170 dev
= qvirtio_pci_device_find(s
->bus
, VIRTIO_ID_NET
);
171 g_assert_nonnull(dev
);
173 qvirtio_pci_device_enable(dev
);
174 qvirtio_reset(&dev
->vdev
);
175 qvirtio_set_acknowledge(&dev
->vdev
);
176 qvirtio_set_driver(&dev
->vdev
);
178 features
= qvirtio_get_features(&dev
->vdev
);
179 features
= features
& VIRTIO_NET_F_MAC
;
180 qvirtio_set_features(&dev
->vdev
, features
);
182 qvirtio_set_driver_ok(&dev
->vdev
);
183 qvirtio_pci_device_free(dev
);
186 static void wait_for_fds(TestServer
*s
)
190 g_mutex_lock(&s
->data_mutex
);
192 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
193 while (!s
->fds_num
) {
194 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
195 /* timeout has passed */
196 g_assert(s
->fds_num
);
201 /* check for sanity */
202 g_assert_cmpint(s
->fds_num
, >, 0);
203 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
205 g_mutex_unlock(&s
->data_mutex
);
208 static void read_guest_mem(const void *data
)
210 TestServer
*s
= (void *)data
;
217 g_mutex_lock(&s
->data_mutex
);
219 /* iterate all regions */
220 for (i
= 0; i
< s
->fds_num
; i
++) {
222 /* We'll check only the region statring at 0x0*/
223 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
227 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
229 size
= s
->memory
.regions
[i
].memory_size
+
230 s
->memory
.regions
[i
].mmap_offset
;
232 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
233 MAP_SHARED
, s
->fds
[i
], 0);
235 g_assert(guest_mem
!= MAP_FAILED
);
236 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
238 for (j
= 0; j
< 256; j
++) {
239 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
240 uint32_t b
= guest_mem
[j
];
242 g_assert_cmpint(a
, ==, b
);
245 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
248 g_mutex_unlock(&s
->data_mutex
);
251 static void *thread_function(void *data
)
253 GMainLoop
*loop
= data
;
254 g_main_loop_run(loop
);
258 static int chr_can_read(void *opaque
)
260 return VHOST_USER_HDR_SIZE
;
263 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
265 TestServer
*s
= opaque
;
266 CharBackend
*chr
= &s
->chr
;
268 uint8_t *p
= (uint8_t *) &msg
;
272 qemu_chr_fe_disconnect(chr
);
273 /* now switch to non-failure */
274 s
->test_fail
= false;
277 if (size
!= VHOST_USER_HDR_SIZE
) {
278 g_test_message("Wrong message size received %d\n", size
);
282 g_mutex_lock(&s
->data_mutex
);
283 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
286 p
+= VHOST_USER_HDR_SIZE
;
287 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
288 if (size
!= msg
.size
) {
289 g_test_message("Wrong message size received %d != %d\n",
295 switch (msg
.request
) {
296 case VHOST_USER_GET_FEATURES
:
297 /* send back features to qemu */
298 msg
.flags
|= VHOST_USER_REPLY_MASK
;
299 msg
.size
= sizeof(m
.payload
.u64
);
300 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
301 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
303 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
305 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
307 s
->test_flags
= TEST_FLAGS_END
;
309 p
= (uint8_t *) &msg
;
310 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
313 case VHOST_USER_SET_FEATURES
:
314 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
316 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
317 qemu_chr_fe_disconnect(chr
);
318 s
->test_flags
= TEST_FLAGS_BAD
;
322 case VHOST_USER_GET_PROTOCOL_FEATURES
:
323 /* send back features to qemu */
324 msg
.flags
|= VHOST_USER_REPLY_MASK
;
325 msg
.size
= sizeof(m
.payload
.u64
);
326 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
328 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
330 p
= (uint8_t *) &msg
;
331 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
334 case VHOST_USER_GET_VRING_BASE
:
335 /* send back vring base to qemu */
336 msg
.flags
|= VHOST_USER_REPLY_MASK
;
337 msg
.size
= sizeof(m
.payload
.state
);
338 msg
.payload
.state
.num
= 0;
339 p
= (uint8_t *) &msg
;
340 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
342 assert(msg
.payload
.state
.index
< s
->queues
* 2);
343 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
346 case VHOST_USER_SET_MEM_TABLE
:
347 /* received the mem table */
348 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
349 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
350 G_N_ELEMENTS(s
->fds
));
352 /* signal the test that it can continue */
353 g_cond_signal(&s
->data_cond
);
356 case VHOST_USER_SET_VRING_KICK
:
357 case VHOST_USER_SET_VRING_CALL
:
359 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
361 * This is a non-blocking eventfd.
362 * The receive function forces it to be blocking,
363 * so revert it back to non-blocking.
365 qemu_set_nonblock(fd
);
368 case VHOST_USER_SET_LOG_BASE
:
369 if (s
->log_fd
!= -1) {
373 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
374 msg
.flags
|= VHOST_USER_REPLY_MASK
;
376 p
= (uint8_t *) &msg
;
377 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
379 g_cond_signal(&s
->data_cond
);
382 case VHOST_USER_SET_VRING_BASE
:
383 assert(msg
.payload
.state
.index
< s
->queues
* 2);
384 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
387 case VHOST_USER_GET_QUEUE_NUM
:
388 msg
.flags
|= VHOST_USER_REPLY_MASK
;
389 msg
.size
= sizeof(m
.payload
.u64
);
390 msg
.payload
.u64
= s
->queues
;
391 p
= (uint8_t *) &msg
;
392 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
399 g_mutex_unlock(&s
->data_mutex
);
402 static const char *init_hugepagefs(const char *path
)
407 if (access(path
, R_OK
| W_OK
| X_OK
)) {
408 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
413 ret
= statfs(path
, &fs
);
414 } while (ret
!= 0 && errno
== EINTR
);
417 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
421 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
422 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
429 static TestServer
*test_server_new(const gchar
*name
)
431 TestServer
*server
= g_new0(TestServer
, 1);
433 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
434 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
435 server
->chr_name
= g_strdup_printf("chr-%s", name
);
437 g_mutex_init(&server
->data_mutex
);
438 g_cond_init(&server
->data_cond
);
446 static void chr_event(void *opaque
, int event
)
448 TestServer
*s
= opaque
;
450 if (s
->test_flags
== TEST_FLAGS_END
&&
451 event
== CHR_EVENT_CLOSED
) {
452 s
->test_flags
= TEST_FLAGS_OK
;
456 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
461 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
462 chr
= qemu_chr_new(server
->chr_name
, chr_path
);
465 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
466 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
467 chr_event
, server
, NULL
, true);
470 static void test_server_listen(TestServer
*server
)
472 test_server_create_chr(server
, ",server,nowait");
475 static inline void test_server_connect(TestServer
*server
)
477 test_server_create_chr(server
, ",reconnect=1");
480 #define GET_QEMU_CMD(s) \
481 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
482 (s)->socket_path, "", (s)->chr_name)
484 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
485 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
486 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
488 static gboolean
_test_server_free(TestServer
*server
)
491 Chardev
*chr
= qemu_chr_fe_get_driver(&server
->chr
);
493 qemu_chr_fe_deinit(&server
->chr
);
494 object_unparent(OBJECT(chr
));
496 for (i
= 0; i
< server
->fds_num
; i
++) {
497 close(server
->fds
[i
]);
500 if (server
->log_fd
!= -1) {
501 close(server
->log_fd
);
504 unlink(server
->socket_path
);
505 g_free(server
->socket_path
);
507 unlink(server
->mig_path
);
508 g_free(server
->mig_path
);
510 g_free(server
->chr_name
);
511 qpci_free_pc(server
->bus
);
518 static void test_server_free(TestServer
*server
)
520 g_idle_add((GSourceFunc
)_test_server_free
, server
);
523 static void wait_for_log_fd(TestServer
*s
)
527 g_mutex_lock(&s
->data_mutex
);
528 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
529 while (s
->log_fd
== -1) {
530 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
531 /* timeout has passed */
532 g_assert(s
->log_fd
!= -1);
537 g_mutex_unlock(&s
->data_mutex
);
540 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
548 /* iterate all regions */
549 for (i
= 0; i
< s
->fds_num
; i
++) {
551 /* We'll write only the region statring at 0x0 */
552 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
556 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
558 size
= s
->memory
.regions
[i
].memory_size
+
559 s
->memory
.regions
[i
].mmap_offset
;
561 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
562 MAP_SHARED
, s
->fds
[i
], 0);
564 g_assert(guest_mem
!= MAP_FAILED
);
565 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
567 for (j
= 0; j
< 256; j
++) {
568 guest_mem
[j
] = seed
+ j
;
571 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
576 static guint64
get_log_size(TestServer
*s
)
578 guint64 log_size
= 0;
581 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
582 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
583 guint64 last
= range_get_last(reg
->guest_phys_addr
,
585 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
591 typedef struct TestMigrateSource
{
598 test_migrate_source_check(GSource
*source
)
600 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
601 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
608 #if !GLIB_CHECK_VERSION(2,36,0)
609 /* this callback is unnecessary with glib >2.36, the default
610 * prepare for the source does the same */
612 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
619 GSourceFuncs test_migrate_source_funcs
= {
620 #if !GLIB_CHECK_VERSION(2,36,0)
621 .prepare
= test_migrate_source_prepare
,
623 .check
= test_migrate_source_check
,
626 static void test_migrate(void)
628 TestServer
*s
= test_server_new("src");
629 TestServer
*dest
= test_server_new("dest");
630 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
631 QTestState
*global
= global_qtest
, *from
, *to
;
638 test_server_listen(s
);
639 test_server_listen(dest
);
641 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
642 from
= qtest_start(cmd
);
647 size
= get_log_size(s
);
648 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
650 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
651 to
= qtest_init(cmd
);
654 source
= g_source_new(&test_migrate_source_funcs
,
655 sizeof(TestMigrateSource
));
656 ((TestMigrateSource
*)source
)->src
= s
;
657 ((TestMigrateSource
*)source
)->dest
= dest
;
658 g_source_attach(source
, NULL
);
660 /* slow down migration to have time to fiddle with log */
661 /* TODO: qtest could learn to break on some places */
662 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
663 "'arguments': { 'value': 10 } }");
664 g_assert(qdict_haskey(rsp
, "return"));
667 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
668 "'arguments': { 'uri': '%s' } }",
672 g_assert(qdict_haskey(rsp
, "return"));
677 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
678 g_assert(log
!= MAP_FAILED
);
680 /* modify first page */
681 write_guest_mem(s
, 0x42);
685 /* speed things up */
686 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
687 "'arguments': { 'value': 0 } }");
688 g_assert(qdict_haskey(rsp
, "return"));
691 qmp_eventwait("STOP");
694 qmp_eventwait("RESUME");
696 read_guest_mem(dest
);
698 g_source_destroy(source
);
699 g_source_unref(source
);
702 test_server_free(dest
);
707 global_qtest
= global
;
710 static void wait_for_rings_started(TestServer
*s
, size_t count
)
714 g_mutex_lock(&s
->data_mutex
);
715 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
716 while (ctpop64(s
->rings
) != count
) {
717 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
718 /* timeout has passed */
719 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
724 g_mutex_unlock(&s
->data_mutex
);
727 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
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
);
967 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
968 qtest_add_func("/vhost-user/reconnect/subprocess",
969 test_reconnect_subprocess
);
970 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
971 qtest_add_func("/vhost-user/connect-fail/subprocess",
972 test_connect_fail_subprocess
);
973 qtest_add_func("/vhost-user/connect-fail", test_connect_fail
);
974 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
975 test_flags_mismatch_subprocess
);
976 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch
);
986 test_server_free(server
);
988 /* finish the helper thread and dispatch pending sources */
989 g_main_loop_quit(loop
);
990 g_thread_join(thread
);
991 while (g_main_context_pending(NULL
)) {
992 g_main_context_iteration (NULL
, TRUE
);
994 g_main_loop_unref(loop
);
998 g_test_message("unable to rmdir: path (%s): %s\n",
999 tmpfs
, strerror(errno
));
1001 g_assert_cmpint(ret
, ==, 0);