]> Git Repo - qemu.git/blame - tests/vhost-user-test.c
hw/virtio/vring/event_idx: fix the vring_avail_event error
[qemu.git] / tests / vhost-user-test.c
CommitLineData
a77e6b14
NN
1/*
2 * QTest testcase for the vhost-user
3 *
4 * Copyright (c) 2014 Virtual Open Systems Sarl.
5 *
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.
8 *
9 */
10
bd95939f
NN
11#define QEMU_GLIB_COMPAT_H
12#include <glib.h>
13
a77e6b14
NN
14#include "libqtest.h"
15#include "qemu/option.h"
16#include "sysemu/char.h"
17#include "sysemu/sysemu.h"
18
a77e6b14
NN
19#include <linux/vhost.h>
20#include <sys/mman.h>
21#include <sys/vfs.h>
22#include <qemu/sockets.h>
23
bd95939f
NN
24#if GLIB_CHECK_VERSION(2, 32, 0)
25#define HAVE_MUTEX_INIT
26#define HAVE_COND_INIT
27#define HAVE_THREAD_NEW
28#endif
29
a77e6b14
NN
30#define QEMU_CMD_ACCEL " -machine accel=tcg"
31#define QEMU_CMD_MEM " -m 512 -object memory-backend-file,id=mem,size=512M,"\
32 "mem-path=%s,share=on -numa node,memdev=mem"
33#define QEMU_CMD_CHR " -chardev socket,id=chr0,path=%s"
34#define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=chr0,vhostforce"
35#define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0 "
36#define QEMU_CMD_ROM " -option-rom ../pc-bios/pxe-virtio.rom"
37
38#define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \
39 QEMU_CMD_NETDEV QEMU_CMD_NET QEMU_CMD_ROM
40
41#define HUGETLBFS_MAGIC 0x958458f6
42
43/*********** FROM hw/virtio/vhost-user.c *************************************/
44
45#define VHOST_MEMORY_MAX_NREGIONS 8
46
47typedef enum VhostUserRequest {
48 VHOST_USER_NONE = 0,
49 VHOST_USER_GET_FEATURES = 1,
50 VHOST_USER_SET_FEATURES = 2,
51 VHOST_USER_SET_OWNER = 3,
52 VHOST_USER_RESET_OWNER = 4,
53 VHOST_USER_SET_MEM_TABLE = 5,
54 VHOST_USER_SET_LOG_BASE = 6,
55 VHOST_USER_SET_LOG_FD = 7,
56 VHOST_USER_SET_VRING_NUM = 8,
57 VHOST_USER_SET_VRING_ADDR = 9,
58 VHOST_USER_SET_VRING_BASE = 10,
59 VHOST_USER_GET_VRING_BASE = 11,
60 VHOST_USER_SET_VRING_KICK = 12,
61 VHOST_USER_SET_VRING_CALL = 13,
62 VHOST_USER_SET_VRING_ERR = 14,
63 VHOST_USER_MAX
64} VhostUserRequest;
65
66typedef struct VhostUserMemoryRegion {
67 uint64_t guest_phys_addr;
68 uint64_t memory_size;
69 uint64_t userspace_addr;
d6970e3b 70 uint64_t mmap_offset;
a77e6b14
NN
71} VhostUserMemoryRegion;
72
73typedef struct VhostUserMemory {
74 uint32_t nregions;
75 uint32_t padding;
76 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
77} VhostUserMemory;
78
79typedef struct VhostUserMsg {
80 VhostUserRequest request;
81
82#define VHOST_USER_VERSION_MASK (0x3)
83#define VHOST_USER_REPLY_MASK (0x1<<2)
84 uint32_t flags;
85 uint32_t size; /* the following payload size */
86 union {
87 uint64_t u64;
88 struct vhost_vring_state state;
89 struct vhost_vring_addr addr;
90 VhostUserMemory memory;
91 };
92} QEMU_PACKED VhostUserMsg;
93
94static VhostUserMsg m __attribute__ ((unused));
95#define VHOST_USER_HDR_SIZE (sizeof(m.request) \
96 + sizeof(m.flags) \
97 + sizeof(m.size))
98
99#define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
100
101/* The version of the protocol we support */
102#define VHOST_USER_VERSION (0x1)
103/*****************************************************************************/
104
105int fds_num = 0, fds[VHOST_MEMORY_MAX_NREGIONS];
106static VhostUserMemory memory;
bd95939f
NN
107static GMutex *data_mutex;
108static GCond *data_cond;
109
bd95939f
NN
110static GMutex *_mutex_new(void)
111{
112 GMutex *mutex;
113
114#ifdef HAVE_MUTEX_INIT
115 mutex = g_new(GMutex, 1);
116 g_mutex_init(mutex);
117#else
118 mutex = g_mutex_new();
119#endif
120
121 return mutex;
122}
123
124static void _mutex_free(GMutex *mutex)
125{
126#ifdef HAVE_MUTEX_INIT
127 g_mutex_clear(mutex);
128 g_free(mutex);
129#else
130 g_mutex_free(mutex);
131#endif
132}
133
134static GCond *_cond_new(void)
135{
136 GCond *cond;
137
138#ifdef HAVE_COND_INIT
139 cond = g_new(GCond, 1);
140 g_cond_init(cond);
141#else
142 cond = g_cond_new();
143#endif
144
145 return cond;
146}
147
148static gboolean _cond_wait_until(GCond *cond, GMutex *mutex, gint64 end_time)
149{
150 gboolean ret = FALSE;
151#ifdef HAVE_COND_INIT
152 ret = g_cond_wait_until(cond, mutex, end_time);
153#else
154 GTimeVal time = { end_time / G_TIME_SPAN_SECOND,
155 end_time % G_TIME_SPAN_SECOND };
156 ret = g_cond_timed_wait(cond, mutex, &time);
157#endif
158 return ret;
159}
160
161static void _cond_free(GCond *cond)
162{
163#ifdef HAVE_COND_INIT
164 g_cond_clear(cond);
165 g_free(cond);
166#else
167 g_cond_free(cond);
168#endif
169}
170
171static GThread *_thread_new(const gchar *name, GThreadFunc func, gpointer data)
172{
173 GThread *thread = NULL;
174 GError *error = NULL;
175#ifdef HAVE_THREAD_NEW
176 thread = g_thread_try_new(name, func, data, &error);
177#else
178 thread = g_thread_create(func, data, TRUE, &error);
179#endif
180 return thread;
181}
a77e6b14
NN
182
183static void read_guest_mem(void)
184{
185 uint32_t *guest_mem;
186 gint64 end_time;
187 int i, j;
d6970e3b 188 size_t size;
a77e6b14 189
bd95939f 190 g_mutex_lock(data_mutex);
a77e6b14 191
89b516d8 192 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
a77e6b14 193 while (!fds_num) {
bd95939f 194 if (!_cond_wait_until(data_cond, data_mutex, end_time)) {
a77e6b14
NN
195 /* timeout has passed */
196 g_assert(fds_num);
197 break;
198 }
199 }
200
201 /* check for sanity */
202 g_assert_cmpint(fds_num, >, 0);
203 g_assert_cmpint(fds_num, ==, memory.nregions);
204
205 /* iterate all regions */
206 for (i = 0; i < fds_num; i++) {
207
208 /* We'll check only the region statring at 0x0*/
209 if (memory.regions[i].guest_phys_addr != 0x0) {
210 continue;
211 }
212
213 g_assert_cmpint(memory.regions[i].memory_size, >, 1024);
214
d6970e3b
NN
215 size = memory.regions[i].memory_size + memory.regions[i].mmap_offset;
216
217 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
218 MAP_SHARED, fds[i], 0);
219
220 g_assert(guest_mem != MAP_FAILED);
221 guest_mem += (memory.regions[i].mmap_offset / sizeof(*guest_mem));
a77e6b14
NN
222
223 for (j = 0; j < 256; j++) {
224 uint32_t a = readl(memory.regions[i].guest_phys_addr + j*4);
225 uint32_t b = guest_mem[j];
226
227 g_assert_cmpint(a, ==, b);
228 }
229
230 munmap(guest_mem, memory.regions[i].memory_size);
231 }
232
233 g_assert_cmpint(1, ==, 1);
bd95939f 234 g_mutex_unlock(data_mutex);
a77e6b14
NN
235}
236
237static void *thread_function(void *data)
238{
239 GMainLoop *loop;
240 loop = g_main_loop_new(NULL, FALSE);
241 g_main_loop_run(loop);
242 return NULL;
243}
244
245static int chr_can_read(void *opaque)
246{
247 return VHOST_USER_HDR_SIZE;
248}
249
250static void chr_read(void *opaque, const uint8_t *buf, int size)
251{
252 CharDriverState *chr = opaque;
253 VhostUserMsg msg;
254 uint8_t *p = (uint8_t *) &msg;
255 int fd;
256
257 if (size != VHOST_USER_HDR_SIZE) {
258 g_test_message("Wrong message size received %d\n", size);
259 return;
260 }
261
f61badf3 262 g_mutex_lock(data_mutex);
a77e6b14
NN
263 memcpy(p, buf, VHOST_USER_HDR_SIZE);
264
265 if (msg.size) {
266 p += VHOST_USER_HDR_SIZE;
267 qemu_chr_fe_read_all(chr, p, msg.size);
268 }
269
270 switch (msg.request) {
271 case VHOST_USER_GET_FEATURES:
272 /* send back features to qemu */
273 msg.flags |= VHOST_USER_REPLY_MASK;
274 msg.size = sizeof(m.u64);
275 msg.u64 = 0;
276 p = (uint8_t *) &msg;
277 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
278 break;
279
280 case VHOST_USER_GET_VRING_BASE:
281 /* send back vring base to qemu */
282 msg.flags |= VHOST_USER_REPLY_MASK;
283 msg.size = sizeof(m.state);
284 msg.state.num = 0;
285 p = (uint8_t *) &msg;
286 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
287 break;
288
289 case VHOST_USER_SET_MEM_TABLE:
290 /* received the mem table */
291 memcpy(&memory, &msg.memory, sizeof(msg.memory));
292 fds_num = qemu_chr_fe_get_msgfds(chr, fds, sizeof(fds) / sizeof(int));
293
294 /* signal the test that it can continue */
bd95939f 295 g_cond_signal(data_cond);
a77e6b14
NN
296 break;
297
298 case VHOST_USER_SET_VRING_KICK:
299 case VHOST_USER_SET_VRING_CALL:
300 /* consume the fd */
301 qemu_chr_fe_get_msgfds(chr, &fd, 1);
302 /*
303 * This is a non-blocking eventfd.
304 * The receive function forces it to be blocking,
305 * so revert it back to non-blocking.
306 */
307 qemu_set_nonblock(fd);
308 break;
309 default:
310 break;
311 }
f61badf3 312 g_mutex_unlock(data_mutex);
a77e6b14
NN
313}
314
315static const char *init_hugepagefs(void)
316{
317 const char *path;
318 struct statfs fs;
319 int ret;
320
321 path = getenv("QTEST_HUGETLBFS_PATH");
322 if (!path) {
323 path = "/hugetlbfs";
324 }
325
326 if (access(path, R_OK | W_OK | X_OK)) {
327 g_test_message("access on path (%s): %s\n", path, strerror(errno));
328 return NULL;
329 }
330
331 do {
332 ret = statfs(path, &fs);
333 } while (ret != 0 && errno == EINTR);
334
335 if (ret != 0) {
336 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
337 return NULL;
338 }
339
340 if (fs.f_type != HUGETLBFS_MAGIC) {
341 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
342 return NULL;
343 }
344
345 return path;
346}
347
348int main(int argc, char **argv)
349{
350 QTestState *s = NULL;
351 CharDriverState *chr = NULL;
352 const char *hugefs = 0;
353 char *socket_path = 0;
354 char *qemu_cmd = 0;
355 char *chr_path = 0;
356 int ret;
357
358 g_test_init(&argc, &argv, NULL);
359
360 module_call_init(MODULE_INIT_QOM);
361
362 hugefs = init_hugepagefs();
363 if (!hugefs) {
364 return 0;
365 }
366
367 socket_path = g_strdup_printf("/tmp/vhost-%d.sock", getpid());
368
369 /* create char dev and add read handlers */
370 qemu_add_opts(&qemu_chardev_opts);
371 chr_path = g_strdup_printf("unix:%s,server,nowait", socket_path);
372 chr = qemu_chr_new("chr0", chr_path, NULL);
373 g_free(chr_path);
374 qemu_chr_add_handlers(chr, chr_can_read, chr_read, NULL, chr);
375
376 /* run the main loop thread so the chardev may operate */
bd95939f
NN
377 data_mutex = _mutex_new();
378 data_cond = _cond_new();
bd95939f 379 _thread_new(NULL, thread_function, NULL);
a77e6b14
NN
380
381 qemu_cmd = g_strdup_printf(QEMU_CMD, hugefs, socket_path);
382 s = qtest_start(qemu_cmd);
383 g_free(qemu_cmd);
384
385 qtest_add_func("/vhost-user/read-guest-mem", read_guest_mem);
386
387 ret = g_test_run();
388
389 if (s) {
390 qtest_quit(s);
391 }
392
393 /* cleanup */
394 unlink(socket_path);
395 g_free(socket_path);
bd95939f
NN
396 _cond_free(data_cond);
397 _mutex_free(data_mutex);
a77e6b14
NN
398
399 return ret;
400}
This page took 0.09153 seconds and 4 git commands to generate.