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 #define QEMU_GLIB_COMPAT_H
15 #include "qemu/option.h"
16 #include "sysemu/char.h"
17 #include "sysemu/sysemu.h"
19 #include <linux/vhost.h>
22 #include <qemu/sockets.h>
24 /* GLIB version compatibility flags */
25 #if GLIB_CHECK_VERSION(2, 28, 0)
26 #define HAVE_MONOTONIC_TIME
29 #if GLIB_CHECK_VERSION(2, 32, 0)
30 #define HAVE_MUTEX_INIT
31 #define HAVE_COND_INIT
32 #define HAVE_THREAD_NEW
35 #define QEMU_CMD_ACCEL " -machine accel=tcg"
36 #define QEMU_CMD_MEM " -m 512 -object memory-backend-file,id=mem,size=512M,"\
37 "mem-path=%s,share=on -numa node,memdev=mem"
38 #define QEMU_CMD_CHR " -chardev socket,id=chr0,path=%s"
39 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=chr0,vhostforce"
40 #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0 "
41 #define QEMU_CMD_ROM " -option-rom ../pc-bios/pxe-virtio.rom"
43 #define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \
44 QEMU_CMD_NETDEV QEMU_CMD_NET QEMU_CMD_ROM
46 #define HUGETLBFS_MAGIC 0x958458f6
48 /*********** FROM hw/virtio/vhost-user.c *************************************/
50 #define VHOST_MEMORY_MAX_NREGIONS 8
52 typedef enum VhostUserRequest
{
54 VHOST_USER_GET_FEATURES
= 1,
55 VHOST_USER_SET_FEATURES
= 2,
56 VHOST_USER_SET_OWNER
= 3,
57 VHOST_USER_RESET_OWNER
= 4,
58 VHOST_USER_SET_MEM_TABLE
= 5,
59 VHOST_USER_SET_LOG_BASE
= 6,
60 VHOST_USER_SET_LOG_FD
= 7,
61 VHOST_USER_SET_VRING_NUM
= 8,
62 VHOST_USER_SET_VRING_ADDR
= 9,
63 VHOST_USER_SET_VRING_BASE
= 10,
64 VHOST_USER_GET_VRING_BASE
= 11,
65 VHOST_USER_SET_VRING_KICK
= 12,
66 VHOST_USER_SET_VRING_CALL
= 13,
67 VHOST_USER_SET_VRING_ERR
= 14,
71 typedef struct VhostUserMemoryRegion
{
72 uint64_t guest_phys_addr
;
74 uint64_t userspace_addr
;
75 } VhostUserMemoryRegion
;
77 typedef struct VhostUserMemory
{
80 VhostUserMemoryRegion regions
[VHOST_MEMORY_MAX_NREGIONS
];
83 typedef struct VhostUserMsg
{
84 VhostUserRequest request
;
86 #define VHOST_USER_VERSION_MASK (0x3)
87 #define VHOST_USER_REPLY_MASK (0x1<<2)
89 uint32_t size
; /* the following payload size */
92 struct vhost_vring_state state
;
93 struct vhost_vring_addr addr
;
94 VhostUserMemory memory
;
96 } QEMU_PACKED VhostUserMsg
;
98 static VhostUserMsg m
__attribute__ ((unused
));
99 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
103 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
105 /* The version of the protocol we support */
106 #define VHOST_USER_VERSION (0x1)
107 /*****************************************************************************/
109 int fds_num
= 0, fds
[VHOST_MEMORY_MAX_NREGIONS
];
110 static VhostUserMemory memory
;
111 static GMutex
*data_mutex
;
112 static GCond
*data_cond
;
114 static gint64
_get_time(void)
116 #ifdef HAVE_MONOTONIC_TIME
117 return g_get_monotonic_time();
120 g_get_current_time(&time
);
122 return time
.tv_sec
* G_TIME_SPAN_SECOND
+ time
.tv_usec
;
126 static GMutex
*_mutex_new(void)
130 #ifdef HAVE_MUTEX_INIT
131 mutex
= g_new(GMutex
, 1);
134 mutex
= g_mutex_new();
140 static void _mutex_free(GMutex
*mutex
)
142 #ifdef HAVE_MUTEX_INIT
143 g_mutex_clear(mutex
);
150 static GCond
*_cond_new(void)
154 #ifdef HAVE_COND_INIT
155 cond
= g_new(GCond
, 1);
164 static gboolean
_cond_wait_until(GCond
*cond
, GMutex
*mutex
, gint64 end_time
)
166 gboolean ret
= FALSE
;
167 #ifdef HAVE_COND_INIT
168 ret
= g_cond_wait_until(cond
, mutex
, end_time
);
170 GTimeVal time
= { end_time
/ G_TIME_SPAN_SECOND
,
171 end_time
% G_TIME_SPAN_SECOND
};
172 ret
= g_cond_timed_wait(cond
, mutex
, &time
);
177 static void _cond_free(GCond
*cond
)
179 #ifdef HAVE_COND_INIT
187 static GThread
*_thread_new(const gchar
*name
, GThreadFunc func
, gpointer data
)
189 GThread
*thread
= NULL
;
190 GError
*error
= NULL
;
191 #ifdef HAVE_THREAD_NEW
192 thread
= g_thread_try_new(name
, func
, data
, &error
);
194 thread
= g_thread_create(func
, data
, TRUE
, &error
);
199 static void read_guest_mem(void)
205 g_mutex_lock(data_mutex
);
207 end_time
= _get_time() + 5 * G_TIME_SPAN_SECOND
;
209 if (!_cond_wait_until(data_cond
, data_mutex
, end_time
)) {
210 /* timeout has passed */
216 /* check for sanity */
217 g_assert_cmpint(fds_num
, >, 0);
218 g_assert_cmpint(fds_num
, ==, memory
.nregions
);
220 /* iterate all regions */
221 for (i
= 0; i
< fds_num
; i
++) {
223 /* We'll check only the region statring at 0x0*/
224 if (memory
.regions
[i
].guest_phys_addr
!= 0x0) {
228 g_assert_cmpint(memory
.regions
[i
].memory_size
, >, 1024);
230 guest_mem
= mmap(0, memory
.regions
[i
].memory_size
,
231 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fds
[i
], 0);
233 for (j
= 0; j
< 256; j
++) {
234 uint32_t a
= readl(memory
.regions
[i
].guest_phys_addr
+ j
*4);
235 uint32_t b
= guest_mem
[j
];
237 g_assert_cmpint(a
, ==, b
);
240 munmap(guest_mem
, memory
.regions
[i
].memory_size
);
243 g_assert_cmpint(1, ==, 1);
244 g_mutex_unlock(data_mutex
);
247 static void *thread_function(void *data
)
250 loop
= g_main_loop_new(NULL
, FALSE
);
251 g_main_loop_run(loop
);
255 static int chr_can_read(void *opaque
)
257 return VHOST_USER_HDR_SIZE
;
260 static void chr_read(void *opaque
, const uint8_t *buf
, int size
)
262 CharDriverState
*chr
= opaque
;
264 uint8_t *p
= (uint8_t *) &msg
;
267 if (size
!= VHOST_USER_HDR_SIZE
) {
268 g_test_message("Wrong message size received %d\n", size
);
272 g_mutex_lock(data_mutex
);
273 memcpy(p
, buf
, VHOST_USER_HDR_SIZE
);
276 p
+= VHOST_USER_HDR_SIZE
;
277 qemu_chr_fe_read_all(chr
, p
, msg
.size
);
280 switch (msg
.request
) {
281 case VHOST_USER_GET_FEATURES
:
282 /* send back features to qemu */
283 msg
.flags
|= VHOST_USER_REPLY_MASK
;
284 msg
.size
= sizeof(m
.u64
);
286 p
= (uint8_t *) &msg
;
287 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
290 case VHOST_USER_GET_VRING_BASE
:
291 /* send back vring base to qemu */
292 msg
.flags
|= VHOST_USER_REPLY_MASK
;
293 msg
.size
= sizeof(m
.state
);
295 p
= (uint8_t *) &msg
;
296 qemu_chr_fe_write_all(chr
, p
, VHOST_USER_HDR_SIZE
+ msg
.size
);
299 case VHOST_USER_SET_MEM_TABLE
:
300 /* received the mem table */
301 memcpy(&memory
, &msg
.memory
, sizeof(msg
.memory
));
302 fds_num
= qemu_chr_fe_get_msgfds(chr
, fds
, sizeof(fds
) / sizeof(int));
304 /* signal the test that it can continue */
305 g_cond_signal(data_cond
);
308 case VHOST_USER_SET_VRING_KICK
:
309 case VHOST_USER_SET_VRING_CALL
:
311 qemu_chr_fe_get_msgfds(chr
, &fd
, 1);
313 * This is a non-blocking eventfd.
314 * The receive function forces it to be blocking,
315 * so revert it back to non-blocking.
317 qemu_set_nonblock(fd
);
322 g_mutex_unlock(data_mutex
);
325 static const char *init_hugepagefs(void)
331 path
= getenv("QTEST_HUGETLBFS_PATH");
336 if (access(path
, R_OK
| W_OK
| X_OK
)) {
337 g_test_message("access on path (%s): %s\n", path
, strerror(errno
));
342 ret
= statfs(path
, &fs
);
343 } while (ret
!= 0 && errno
== EINTR
);
346 g_test_message("statfs on path (%s): %s\n", path
, strerror(errno
));
350 if (fs
.f_type
!= HUGETLBFS_MAGIC
) {
351 g_test_message("Warning: path not on HugeTLBFS: %s\n", path
);
358 int main(int argc
, char **argv
)
360 QTestState
*s
= NULL
;
361 CharDriverState
*chr
= NULL
;
362 const char *hugefs
= 0;
363 char *socket_path
= 0;
368 g_test_init(&argc
, &argv
, NULL
);
370 module_call_init(MODULE_INIT_QOM
);
372 hugefs
= init_hugepagefs();
377 socket_path
= g_strdup_printf("/tmp/vhost-%d.sock", getpid());
379 /* create char dev and add read handlers */
380 qemu_add_opts(&qemu_chardev_opts
);
381 chr_path
= g_strdup_printf("unix:%s,server,nowait", socket_path
);
382 chr
= qemu_chr_new("chr0", chr_path
, NULL
);
384 qemu_chr_add_handlers(chr
, chr_can_read
, chr_read
, NULL
, chr
);
386 /* run the main loop thread so the chardev may operate */
387 data_mutex
= _mutex_new();
388 data_cond
= _cond_new();
389 _thread_new(NULL
, thread_function
, NULL
);
391 qemu_cmd
= g_strdup_printf(QEMU_CMD
, hugefs
, socket_path
);
392 s
= qtest_start(qemu_cmd
);
395 qtest_add_func("/vhost-user/read-guest-mem", read_guest_mem
);
406 _cond_free(data_cond
);
407 _mutex_free(data_mutex
);