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