virtio-blk: implement per-device migration calls
[qemu/ar7.git] / tests / vhost-user-test.c
blob2af2381a1de7829fd84d3b8bfd4aeff3b8ff5b8c
1 /*
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.
9 */
11 #define QEMU_GLIB_COMPAT_H
12 #include <glib.h>
14 #include "libqtest.h"
15 #include "qemu/option.h"
16 #include "sysemu/char.h"
17 #include "sysemu/sysemu.h"
19 #include <linux/vhost.h>
20 #include <sys/mman.h>
21 #include <sys/vfs.h>
22 #include <qemu/sockets.h>
24 /* GLIB version compatibility flags */
25 #if GLIB_CHECK_VERSION(2, 28, 0)
26 #define HAVE_MONOTONIC_TIME
27 #endif
29 #if GLIB_CHECK_VERSION(2, 32, 0)
30 #define HAVE_MUTEX_INIT
31 #define HAVE_COND_INIT
32 #define HAVE_THREAD_NEW
33 #endif
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 {
53 VHOST_USER_NONE = 0,
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,
68 VHOST_USER_MAX
69 } VhostUserRequest;
71 typedef struct VhostUserMemoryRegion {
72 uint64_t guest_phys_addr;
73 uint64_t memory_size;
74 uint64_t userspace_addr;
75 } VhostUserMemoryRegion;
77 typedef struct VhostUserMemory {
78 uint32_t nregions;
79 uint32_t padding;
80 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
81 } VhostUserMemory;
83 typedef struct VhostUserMsg {
84 VhostUserRequest request;
86 #define VHOST_USER_VERSION_MASK (0x3)
87 #define VHOST_USER_REPLY_MASK (0x1<<2)
88 uint32_t flags;
89 uint32_t size; /* the following payload size */
90 union {
91 uint64_t u64;
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) \
100 + sizeof(m.flags) \
101 + sizeof(m.size))
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();
118 #else
119 GTimeVal time;
120 g_get_current_time(&time);
122 return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
123 #endif
126 static GMutex *_mutex_new(void)
128 GMutex *mutex;
130 #ifdef HAVE_MUTEX_INIT
131 mutex = g_new(GMutex, 1);
132 g_mutex_init(mutex);
133 #else
134 mutex = g_mutex_new();
135 #endif
137 return mutex;
140 static void _mutex_free(GMutex *mutex)
142 #ifdef HAVE_MUTEX_INIT
143 g_mutex_clear(mutex);
144 g_free(mutex);
145 #else
146 g_mutex_free(mutex);
147 #endif
150 static GCond *_cond_new(void)
152 GCond *cond;
154 #ifdef HAVE_COND_INIT
155 cond = g_new(GCond, 1);
156 g_cond_init(cond);
157 #else
158 cond = g_cond_new();
159 #endif
161 return cond;
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);
169 #else
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);
173 #endif
174 return ret;
177 static void _cond_free(GCond *cond)
179 #ifdef HAVE_COND_INIT
180 g_cond_clear(cond);
181 g_free(cond);
182 #else
183 g_cond_free(cond);
184 #endif
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);
193 #else
194 thread = g_thread_create(func, data, TRUE, &error);
195 #endif
196 return thread;
199 static void read_guest_mem(void)
201 uint32_t *guest_mem;
202 gint64 end_time;
203 int i, j;
205 g_mutex_lock(data_mutex);
207 end_time = _get_time() + 5 * G_TIME_SPAN_SECOND;
208 while (!fds_num) {
209 if (!_cond_wait_until(data_cond, data_mutex, end_time)) {
210 /* timeout has passed */
211 g_assert(fds_num);
212 break;
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) {
225 continue;
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)
249 GMainLoop *loop;
250 loop = g_main_loop_new(NULL, FALSE);
251 g_main_loop_run(loop);
252 return NULL;
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;
263 VhostUserMsg msg;
264 uint8_t *p = (uint8_t *) &msg;
265 int fd;
267 if (size != VHOST_USER_HDR_SIZE) {
268 g_test_message("Wrong message size received %d\n", size);
269 return;
272 g_mutex_lock(data_mutex);
273 memcpy(p, buf, VHOST_USER_HDR_SIZE);
275 if (msg.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);
285 msg.u64 = 0;
286 p = (uint8_t *) &msg;
287 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
288 break;
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);
294 msg.state.num = 0;
295 p = (uint8_t *) &msg;
296 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
297 break;
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);
306 break;
308 case VHOST_USER_SET_VRING_KICK:
309 case VHOST_USER_SET_VRING_CALL:
310 /* consume the fd */
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);
318 break;
319 default:
320 break;
322 g_mutex_unlock(data_mutex);
325 static const char *init_hugepagefs(void)
327 const char *path;
328 struct statfs fs;
329 int ret;
331 path = getenv("QTEST_HUGETLBFS_PATH");
332 if (!path) {
333 path = "/hugetlbfs";
336 if (access(path, R_OK | W_OK | X_OK)) {
337 g_test_message("access on path (%s): %s\n", path, strerror(errno));
338 return NULL;
341 do {
342 ret = statfs(path, &fs);
343 } while (ret != 0 && errno == EINTR);
345 if (ret != 0) {
346 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
347 return NULL;
350 if (fs.f_type != HUGETLBFS_MAGIC) {
351 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
352 return NULL;
355 return 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;
364 char *qemu_cmd = 0;
365 char *chr_path = 0;
366 int ret;
368 g_test_init(&argc, &argv, NULL);
370 module_call_init(MODULE_INIT_QOM);
372 hugefs = init_hugepagefs();
373 if (!hugefs) {
374 return 0;
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);
383 g_free(chr_path);
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);
393 g_free(qemu_cmd);
395 qtest_add_func("/vhost-user/read-guest-mem", read_guest_mem);
397 ret = g_test_run();
399 if (s) {
400 qtest_quit(s);
403 /* cleanup */
404 unlink(socket_path);
405 g_free(socket_path);
406 _cond_free(data_cond);
407 _mutex_free(data_mutex);
409 return ret;