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
{
147 int fds
[VHOST_MEMORY_MAX_NREGIONS
];
148 VhostUserMemory memory
;
149 CompatGMutex data_mutex
;
150 CompatGCond data_cond
;
158 static const char *tmpfs
;
159 static const char *root
;
161 static void init_virtio_dev(TestServer
*s
)
164 QVirtioPCIDevice
*dev
;
167 bus
= qpci_init_pc(NULL
);
168 g_assert_nonnull(bus
);
170 dev
= qvirtio_pci_device_find(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
);
185 static void wait_for_fds(TestServer
*s
)
189 g_mutex_lock(&s
->data_mutex
);
191 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
192 while (!s
->fds_num
) {
193 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
194 /* timeout has passed */
195 g_assert(s
->fds_num
);
200 /* check for sanity */
201 g_assert_cmpint(s
->fds_num
, >, 0);
202 g_assert_cmpint(s
->fds_num
, ==, s
->memory
.nregions
);
204 g_mutex_unlock(&s
->data_mutex
);
207 static void read_guest_mem(const void *data
)
209 TestServer
*s
= (void *)data
;
216 g_mutex_lock(&s
->data_mutex
);
218 /* iterate all regions */
219 for (i
= 0; i
< s
->fds_num
; i
++) {
221 /* We'll check only the region statring at 0x0*/
222 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
226 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
228 size
= s
->memory
.regions
[i
].memory_size
+
229 s
->memory
.regions
[i
].mmap_offset
;
231 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
232 MAP_SHARED
, s
->fds
[i
], 0);
234 g_assert(guest_mem
!= MAP_FAILED
);
235 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
237 for (j
= 0; j
< 256; j
++) {
238 uint32_t a
= readl(s
->memory
.regions
[i
].guest_phys_addr
+ j
*4);
239 uint32_t b
= guest_mem
[j
];
241 g_assert_cmpint(a
, ==, b
);
244 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
247 g_mutex_unlock(&s
->data_mutex
);
250 static void *thread_function(void *data
)
252 GMainLoop
*loop
= data
;
253 g_main_loop_run(loop
);
257 static int chr_can_read(void *opaque
)
259 return VHOST_USER_HDR_SIZE
;
262 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
264 TestServer
*s
= opaque
;
265 CharBackend
*chr
= &s
->chr
;
267 uint8_t *p
= (uint8_t *) &msg
;
271 qemu_chr_fe_disconnect(chr
);
272 /* now switch to non-failure */
273 s
->test_fail
= false;
276 if (size
!= VHOST_USER_HDR_SIZE
) {
277 g_test_message("Wrong message size received %d\n", size
);
281 g_mutex_lock(&s
->data_mutex
);
282 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
285 p
+= VHOST_USER_HDR_SIZE
;
286 size
= qemu_chr_fe_read_all(chr
, p
, msg
.size
);
287 if (size
!= msg
.size
) {
288 g_test_message("Wrong message size received %d != %d\n",
294 switch (msg
.request
) {
295 case VHOST_USER_GET_FEATURES
:
296 /* send back features to qemu */
297 msg
.flags
|= VHOST_USER_REPLY_MASK
;
298 msg
.size
= sizeof(m
.payload
.u64
);
299 msg
.payload
.u64
= 0x1ULL
<< VHOST_F_LOG_ALL
|
300 0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
;
302 msg
.payload
.u64
|= 0x1ULL
<< VIRTIO_NET_F_MQ
;
304 if (s
->test_flags
>= TEST_FLAGS_BAD
) {
306 s
->test_flags
= TEST_FLAGS_END
;
308 p
= (uint8_t *) &msg
;
309 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
312 case VHOST_USER_SET_FEATURES
:
313 g_assert_cmpint(msg
.payload
.u64
& (0x1ULL
<< VHOST_USER_F_PROTOCOL_FEATURES
),
315 if (s
->test_flags
== TEST_FLAGS_DISCONNECT
) {
316 qemu_chr_fe_disconnect(chr
);
317 s
->test_flags
= TEST_FLAGS_BAD
;
321 case VHOST_USER_GET_PROTOCOL_FEATURES
:
322 /* send back features to qemu */
323 msg
.flags
|= VHOST_USER_REPLY_MASK
;
324 msg
.size
= sizeof(m
.payload
.u64
);
325 msg
.payload
.u64
= 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD
;
327 msg
.payload
.u64
|= 1 << VHOST_USER_PROTOCOL_F_MQ
;
329 p
= (uint8_t *) &msg
;
330 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
333 case VHOST_USER_GET_VRING_BASE
:
334 /* send back vring base to qemu */
335 msg
.flags
|= VHOST_USER_REPLY_MASK
;
336 msg
.size
= sizeof(m
.payload
.state
);
337 msg
.payload
.state
.num
= 0;
338 p
= (uint8_t *) &msg
;
339 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
341 assert(msg
.payload
.state
.index
< s
->queues
* 2);
342 s
->rings
&= ~(0x1ULL
<< msg
.payload
.state
.index
);
345 case VHOST_USER_SET_MEM_TABLE
:
346 /* received the mem table */
347 memcpy(&s
->memory
, &msg
.payload
.memory
, sizeof(msg
.payload
.memory
));
348 s
->fds_num
= qemu_chr_fe_get_msgfds(chr
, s
->fds
,
349 G_N_ELEMENTS(s
->fds
));
351 /* signal the test that it can continue */
352 g_cond_signal(&s
->data_cond
);
355 case VHOST_USER_SET_VRING_KICK
:
356 case VHOST_USER_SET_VRING_CALL
:
358 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
360 * This is a non-blocking eventfd.
361 * The receive function forces it to be blocking,
362 * so revert it back to non-blocking.
364 qemu_set_nonblock(fd
);
367 case VHOST_USER_SET_LOG_BASE
:
368 if (s
->log_fd
!= -1) {
372 qemu_chr_fe_get_msgfds(chr
, &s
->log_fd
, 1);
373 msg
.flags
|= VHOST_USER_REPLY_MASK
;
375 p
= (uint8_t *) &msg
;
376 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
);
378 g_cond_signal(&s
->data_cond
);
381 case VHOST_USER_SET_VRING_BASE
:
382 assert(msg
.payload
.state
.index
< s
->queues
* 2);
383 s
->rings
|= 0x1ULL
<< msg
.payload
.state
.index
;
386 case VHOST_USER_GET_QUEUE_NUM
:
387 msg
.flags
|= VHOST_USER_REPLY_MASK
;
388 msg
.size
= sizeof(m
.payload
.u64
);
389 msg
.payload
.u64
= s
->queues
;
390 p
= (uint8_t *) &msg
;
391 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
398 g_mutex_unlock(&s
->data_mutex
);
401 static const char *init_hugepagefs(const char *path
)
406 if (access(path
, R_OK
| W_OK
| X_OK
)) {
407 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
412 ret
= statfs(path
, &fs
);
413 } while (ret
!= 0 && errno
== EINTR
);
416 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
420 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
421 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
428 static TestServer
*test_server_new(const gchar
*name
)
430 TestServer
*server
= g_new0(TestServer
, 1);
432 server
->socket_path
= g_strdup_printf("%s/%s.sock", tmpfs
, name
);
433 server
->mig_path
= g_strdup_printf("%s/%s.mig", tmpfs
, name
);
434 server
->chr_name
= g_strdup_printf("chr-%s", name
);
436 g_mutex_init(&server
->data_mutex
);
437 g_cond_init(&server
->data_cond
);
445 static void chr_event(void *opaque
, int event
)
447 TestServer
*s
= opaque
;
449 if (s
->test_flags
== TEST_FLAGS_END
&&
450 event
== CHR_EVENT_CLOSED
) {
451 s
->test_flags
= TEST_FLAGS_OK
;
455 static void test_server_create_chr(TestServer
*server
, const gchar
*opt
)
460 chr_path
= g_strdup_printf("unix:%s%s", server
->socket_path
, opt
);
461 chr
= qemu_chr_new(server
->chr_name
, chr_path
);
464 qemu_chr_fe_init(&server
->chr
, chr
, &error_abort
);
465 qemu_chr_fe_set_handlers(&server
->chr
, chr_can_read
, chr_read
,
466 chr_event
, server
, NULL
, true);
469 static void test_server_listen(TestServer
*server
)
471 test_server_create_chr(server
, ",server,nowait");
474 static inline void test_server_connect(TestServer
*server
)
476 test_server_create_chr(server
, ",reconnect=1");
479 #define GET_QEMU_CMD(s) \
480 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
481 (s)->socket_path, "", (s)->chr_name)
483 #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
484 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
485 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
487 static gboolean
_test_server_free(TestServer
*server
)
490 Chardev
*chr
= qemu_chr_fe_get_driver(&server
->chr
);
492 qemu_chr_fe_deinit(&server
->chr
);
493 qemu_chr_delete(chr
);
495 for (i
= 0; i
< server
->fds_num
; i
++) {
496 close(server
->fds
[i
]);
499 if (server
->log_fd
!= -1) {
500 close(server
->log_fd
);
503 unlink(server
->socket_path
);
504 g_free(server
->socket_path
);
506 unlink(server
->mig_path
);
507 g_free(server
->mig_path
);
509 g_free(server
->chr_name
);
515 static void test_server_free(TestServer
*server
)
517 g_idle_add((GSourceFunc
)_test_server_free
, server
);
520 static void wait_for_log_fd(TestServer
*s
)
524 g_mutex_lock(&s
->data_mutex
);
525 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
526 while (s
->log_fd
== -1) {
527 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
528 /* timeout has passed */
529 g_assert(s
->log_fd
!= -1);
534 g_mutex_unlock(&s
->data_mutex
);
537 static void write_guest_mem(TestServer
*s
, uint32_t seed
)
545 /* iterate all regions */
546 for (i
= 0; i
< s
->fds_num
; i
++) {
548 /* We'll write only the region statring at 0x0 */
549 if (s
->memory
.regions
[i
].guest_phys_addr
!= 0x0) {
553 g_assert_cmpint(s
->memory
.regions
[i
].memory_size
, >, 1024);
555 size
= s
->memory
.regions
[i
].memory_size
+
556 s
->memory
.regions
[i
].mmap_offset
;
558 guest_mem
= mmap(0, size
, PROT_READ
| PROT_WRITE
,
559 MAP_SHARED
, s
->fds
[i
], 0);
561 g_assert(guest_mem
!= MAP_FAILED
);
562 guest_mem
+= (s
->memory
.regions
[i
].mmap_offset
/ sizeof(*guest_mem
));
564 for (j
= 0; j
< 256; j
++) {
565 guest_mem
[j
] = seed
+ j
;
568 munmap(guest_mem
, s
->memory
.regions
[i
].memory_size
);
573 static guint64
get_log_size(TestServer
*s
)
575 guint64 log_size
= 0;
578 for (i
= 0; i
< s
->memory
.nregions
; ++i
) {
579 VhostUserMemoryRegion
*reg
= &s
->memory
.regions
[i
];
580 guint64 last
= range_get_last(reg
->guest_phys_addr
,
582 log_size
= MAX(log_size
, last
/ (8 * VHOST_LOG_PAGE
) + 1);
588 typedef struct TestMigrateSource
{
595 test_migrate_source_check(GSource
*source
)
597 TestMigrateSource
*t
= (TestMigrateSource
*)source
;
598 gboolean overlap
= t
->src
->rings
&& t
->dest
->rings
;
605 #if !GLIB_CHECK_VERSION(2,36,0)
606 /* this callback is unnecessary with glib >2.36, the default
607 * prepare for the source does the same */
609 test_migrate_source_prepare(GSource
*source
, gint
*timeout
)
616 GSourceFuncs test_migrate_source_funcs
= {
617 #if !GLIB_CHECK_VERSION(2,36,0)
618 .prepare
= test_migrate_source_prepare
,
620 .check
= test_migrate_source_check
,
623 static void test_migrate(void)
625 TestServer
*s
= test_server_new("src");
626 TestServer
*dest
= test_server_new("dest");
627 char *uri
= g_strdup_printf("%s%s", "unix:", dest
->mig_path
);
628 QTestState
*global
= global_qtest
, *from
, *to
;
635 test_server_listen(s
);
636 test_server_listen(dest
);
638 cmd
= GET_QEMU_CMDE(s
, 2, "", "");
639 from
= qtest_start(cmd
);
644 size
= get_log_size(s
);
645 g_assert_cmpint(size
, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE
* 8));
647 cmd
= GET_QEMU_CMDE(dest
, 2, "", " -incoming %s", uri
);
648 to
= qtest_init(cmd
);
651 source
= g_source_new(&test_migrate_source_funcs
,
652 sizeof(TestMigrateSource
));
653 ((TestMigrateSource
*)source
)->src
= s
;
654 ((TestMigrateSource
*)source
)->dest
= dest
;
655 g_source_attach(source
, NULL
);
657 /* slow down migration to have time to fiddle with log */
658 /* TODO: qtest could learn to break on some places */
659 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
660 "'arguments': { 'value': 10 } }");
661 g_assert(qdict_haskey(rsp
, "return"));
664 cmd
= g_strdup_printf("{ 'execute': 'migrate',"
665 "'arguments': { 'uri': '%s' } }",
669 g_assert(qdict_haskey(rsp
, "return"));
674 log
= mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, s
->log_fd
, 0);
675 g_assert(log
!= MAP_FAILED
);
677 /* modify first page */
678 write_guest_mem(s
, 0x42);
682 /* speed things up */
683 rsp
= qmp("{ 'execute': 'migrate_set_speed',"
684 "'arguments': { 'value': 0 } }");
685 g_assert(qdict_haskey(rsp
, "return"));
688 qmp_eventwait("STOP");
691 qmp_eventwait("RESUME");
693 read_guest_mem(dest
);
695 g_source_destroy(source
);
696 g_source_unref(source
);
699 test_server_free(dest
);
704 global_qtest
= global
;
707 static void wait_for_rings_started(TestServer
*s
, size_t count
)
711 g_mutex_lock(&s
->data_mutex
);
712 end_time
= g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND
;
713 while (ctpop64(s
->rings
) != count
) {
714 if (!g_cond_wait_until(&s
->data_cond
, &s
->data_mutex
, end_time
)) {
715 /* timeout has passed */
716 g_assert_cmpint(ctpop64(s
->rings
), ==, count
);
721 g_mutex_unlock(&s
->data_mutex
);
724 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
726 reconnect_cb(gpointer user_data
)
728 TestServer
*s
= user_data
;
730 qemu_chr_fe_disconnect(&s
->chr
);
736 connect_thread(gpointer data
)
738 TestServer
*s
= data
;
740 /* wait for qemu to start before first try, to avoid extra warnings */
741 g_usleep(G_USEC_PER_SEC
);
742 test_server_connect(s
);
747 static void test_reconnect_subprocess(void)
749 TestServer
*s
= test_server_new("reconnect");
752 g_thread_new("connect", connect_thread
, s
);
753 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
759 wait_for_rings_started(s
, 2);
764 g_idle_add(reconnect_cb
, s
);
766 wait_for_rings_started(s
, 2);
773 static void test_reconnect(void)
775 gchar
*path
= g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
777 g_test_trap_subprocess(path
, 0, 0);
778 g_test_trap_assert_passed();
782 static void test_connect_fail_subprocess(void)
784 TestServer
*s
= test_server_new("connect-fail");
788 g_thread_new("connect", connect_thread
, s
);
789 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
795 wait_for_rings_started(s
, 2);
801 static void test_connect_fail(void)
803 gchar
*path
= g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
805 g_test_trap_subprocess(path
, 0, 0);
806 g_test_trap_assert_passed();
810 static void test_flags_mismatch_subprocess(void)
812 TestServer
*s
= test_server_new("flags-mismatch");
815 s
->test_flags
= TEST_FLAGS_DISCONNECT
;
816 g_thread_new("connect", connect_thread
, s
);
817 cmd
= GET_QEMU_CMDE(s
, 2, ",server", "");
823 wait_for_rings_started(s
, 2);
829 static void test_flags_mismatch(void)
831 gchar
*path
= g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
833 g_test_trap_subprocess(path
, 0, 0);
834 g_test_trap_assert_passed();
840 static QVirtioPCIDevice
*virtio_net_pci_init(QPCIBus
*bus
, int slot
)
842 QVirtioPCIDevice
*dev
;
844 dev
= qvirtio_pci_device_find(bus
, VIRTIO_ID_NET
);
845 g_assert(dev
!= NULL
);
846 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_NET
);
848 qvirtio_pci_device_enable(dev
);
849 qvirtio_reset(&dev
->vdev
);
850 qvirtio_set_acknowledge(&dev
->vdev
);
851 qvirtio_set_driver(&dev
->vdev
);
856 static void driver_init(QVirtioDevice
*dev
)
860 features
= qvirtio_get_features(dev
);
861 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
862 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
863 (1u << VIRTIO_RING_F_EVENT_IDX
));
864 qvirtio_set_features(dev
, features
);
866 qvirtio_set_driver_ok(dev
);
869 #define PCI_SLOT 0x04
871 static void test_multiqueue(void)
873 const int queues
= 2;
874 TestServer
*s
= test_server_new("mq");
875 QVirtioPCIDevice
*dev
;
877 QVirtQueuePCI
*vq
[queues
* 2];
878 QGuestAllocator
*alloc
;
883 test_server_listen(s
);
885 cmd
= g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV
",queues=%d "
886 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
887 512, 512, root
, s
->chr_name
,
888 s
->socket_path
, "", s
->chr_name
,
889 queues
, queues
* 2 + 2);
893 bus
= qpci_init_pc(NULL
);
894 dev
= virtio_net_pci_init(bus
, PCI_SLOT
);
896 alloc
= pc_alloc_init();
897 for (i
= 0; i
< queues
* 2; i
++) {
898 vq
[i
] = (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, alloc
, i
);
901 driver_init(&dev
->vdev
);
902 wait_for_rings_started(s
, queues
* 2);
905 for (i
= 0; i
< queues
* 2; i
++) {
906 qvirtqueue_cleanup(dev
->vdev
.bus
, &vq
[i
]->vq
, alloc
);
908 pc_alloc_uninit(alloc
);
909 qvirtio_pci_device_disable(dev
);
918 int main(int argc
, char **argv
)
920 QTestState
*s
= NULL
;
921 TestServer
*server
= NULL
;
923 char *qemu_cmd
= NULL
;
925 char template[] = "/tmp/vhost-test-XXXXXX";
929 g_test_init(&argc
, &argv
, NULL
);
931 module_call_init(MODULE_INIT_QOM
);
932 qemu_add_opts(&qemu_chardev_opts
);
934 tmpfs
= mkdtemp(template);
936 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno
));
940 hugefs
= getenv("QTEST_HUGETLBFS_PATH");
942 root
= init_hugepagefs(hugefs
);
948 server
= test_server_new("test");
949 test_server_listen(server
);
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 qemu_cmd
= GET_QEMU_CMD(server
);
957 s
= qtest_start(qemu_cmd
);
959 init_virtio_dev(server
);
961 qtest_add_data_func("/vhost-user/read-guest-mem", server
, read_guest_mem
);
962 qtest_add_func("/vhost-user/migrate", test_migrate
);
963 qtest_add_func("/vhost-user/multiqueue", test_multiqueue
);
964 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
965 qtest_add_func("/vhost-user/reconnect/subprocess",
966 test_reconnect_subprocess
);
967 qtest_add_func("/vhost-user/reconnect", test_reconnect
);
968 qtest_add_func("/vhost-user/connect-fail/subprocess",
969 test_connect_fail_subprocess
);
970 qtest_add_func("/vhost-user/connect-fail", test_connect_fail
);
971 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
972 test_flags_mismatch_subprocess
);
973 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch
);
983 test_server_free(server
);
985 /* finish the helper thread and dispatch pending sources */
986 g_main_loop_quit(loop
);
987 g_thread_join(thread
);
988 while (g_main_context_pending(NULL
)) {
989 g_main_context_iteration (NULL
, TRUE
);
991 g_main_loop_unref(loop
);
995 g_test_message("unable to rmdir: path (%s): %s\n",
996 tmpfs
, strerror(errno
));
998 g_assert_cmpint(ret
, ==, 0);