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