]>
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 | ||
681c28a3 | 11 | #include "qemu/osdep.h" |
bd95939f NN |
12 | #include <glib.h> |
13 | ||
a77e6b14 NN |
14 | #include "libqtest.h" |
15 | #include "qemu/option.h" | |
b1819747 | 16 | #include "qemu/range.h" |
a77e6b14 NN |
17 | #include "sysemu/char.h" |
18 | #include "sysemu/sysemu.h" | |
19 | ||
a77e6b14 NN |
20 | #include <linux/vhost.h> |
21 | #include <sys/mman.h> | |
22 | #include <sys/vfs.h> | |
23 | #include <qemu/sockets.h> | |
24 | ||
30de46db GA |
25 | /* GLIB version compatibility flags */ |
26 | #if !GLIB_CHECK_VERSION(2, 26, 0) | |
27 | #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000)) | |
28 | #endif | |
29 | ||
30 | #if GLIB_CHECK_VERSION(2, 28, 0) | |
31 | #define HAVE_MONOTONIC_TIME | |
32 | #endif | |
33 | ||
a77e6b14 | 34 | #define QEMU_CMD_ACCEL " -machine accel=tcg" |
704b2168 | 35 | #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\ |
a77e6b14 | 36 | "mem-path=%s,share=on -numa node,memdev=mem" |
704b2168 MAL |
37 | #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s" |
38 | #define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce" | |
b5c6eaf1 | 39 | #define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0,romfile=./pc-bios/pxe-virtio.rom" |
a77e6b14 NN |
40 | |
41 | #define QEMU_CMD QEMU_CMD_ACCEL QEMU_CMD_MEM QEMU_CMD_CHR \ | |
b5c6eaf1 | 42 | QEMU_CMD_NETDEV QEMU_CMD_NET |
a77e6b14 NN |
43 | |
44 | #define HUGETLBFS_MAGIC 0x958458f6 | |
45 | ||
46 | /*********** FROM hw/virtio/vhost-user.c *************************************/ | |
47 | ||
48 | #define VHOST_MEMORY_MAX_NREGIONS 8 | |
49 | ||
8a9b6b37 | 50 | #define VHOST_USER_F_PROTOCOL_FEATURES 30 |
b1819747 MAL |
51 | #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1 |
52 | ||
53 | #define VHOST_LOG_PAGE 0x1000 | |
8a9b6b37 | 54 | |
a77e6b14 NN |
55 | typedef enum VhostUserRequest { |
56 | VHOST_USER_NONE = 0, | |
57 | VHOST_USER_GET_FEATURES = 1, | |
58 | VHOST_USER_SET_FEATURES = 2, | |
59 | VHOST_USER_SET_OWNER = 3, | |
60915dc4 | 60 | VHOST_USER_RESET_OWNER = 4, |
a77e6b14 NN |
61 | VHOST_USER_SET_MEM_TABLE = 5, |
62 | VHOST_USER_SET_LOG_BASE = 6, | |
63 | VHOST_USER_SET_LOG_FD = 7, | |
64 | VHOST_USER_SET_VRING_NUM = 8, | |
65 | VHOST_USER_SET_VRING_ADDR = 9, | |
66 | VHOST_USER_SET_VRING_BASE = 10, | |
67 | VHOST_USER_GET_VRING_BASE = 11, | |
68 | VHOST_USER_SET_VRING_KICK = 12, | |
69 | VHOST_USER_SET_VRING_CALL = 13, | |
70 | VHOST_USER_SET_VRING_ERR = 14, | |
8a9b6b37 MT |
71 | VHOST_USER_GET_PROTOCOL_FEATURES = 15, |
72 | VHOST_USER_SET_PROTOCOL_FEATURES = 16, | |
87656d50 | 73 | VHOST_USER_SET_VRING_ENABLE = 18, |
a77e6b14 NN |
74 | VHOST_USER_MAX |
75 | } VhostUserRequest; | |
76 | ||
77 | typedef struct VhostUserMemoryRegion { | |
78 | uint64_t guest_phys_addr; | |
79 | uint64_t memory_size; | |
80 | uint64_t userspace_addr; | |
d6970e3b | 81 | uint64_t mmap_offset; |
a77e6b14 NN |
82 | } VhostUserMemoryRegion; |
83 | ||
84 | typedef struct VhostUserMemory { | |
85 | uint32_t nregions; | |
86 | uint32_t padding; | |
87 | VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS]; | |
88 | } VhostUserMemory; | |
89 | ||
2b8819c6 VK |
90 | typedef struct VhostUserLog { |
91 | uint64_t mmap_size; | |
92 | uint64_t mmap_offset; | |
93 | } VhostUserLog; | |
94 | ||
a77e6b14 NN |
95 | typedef struct VhostUserMsg { |
96 | VhostUserRequest request; | |
97 | ||
98 | #define VHOST_USER_VERSION_MASK (0x3) | |
99 | #define VHOST_USER_REPLY_MASK (0x1<<2) | |
100 | uint32_t flags; | |
101 | uint32_t size; /* the following payload size */ | |
102 | union { | |
2b8819c6 VK |
103 | #define VHOST_USER_VRING_IDX_MASK (0xff) |
104 | #define VHOST_USER_VRING_NOFD_MASK (0x1<<8) | |
a77e6b14 NN |
105 | uint64_t u64; |
106 | struct vhost_vring_state state; | |
107 | struct vhost_vring_addr addr; | |
108 | VhostUserMemory memory; | |
2b8819c6 | 109 | VhostUserLog log; |
12ebf690 | 110 | } payload; |
a77e6b14 NN |
111 | } QEMU_PACKED VhostUserMsg; |
112 | ||
113 | static VhostUserMsg m __attribute__ ((unused)); | |
114 | #define VHOST_USER_HDR_SIZE (sizeof(m.request) \ | |
115 | + sizeof(m.flags) \ | |
116 | + sizeof(m.size)) | |
117 | ||
118 | #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE) | |
119 | ||
120 | /* The version of the protocol we support */ | |
121 | #define VHOST_USER_VERSION (0x1) | |
122 | /*****************************************************************************/ | |
123 | ||
ae31fb54 MAL |
124 | typedef struct TestServer { |
125 | gchar *socket_path; | |
a899b1ea | 126 | gchar *mig_path; |
ae31fb54 MAL |
127 | gchar *chr_name; |
128 | CharDriverState *chr; | |
129 | int fds_num; | |
130 | int fds[VHOST_MEMORY_MAX_NREGIONS]; | |
131 | VhostUserMemory memory; | |
132 | GMutex data_mutex; | |
133 | GCond data_cond; | |
b1819747 | 134 | int log_fd; |
d08e42a1 | 135 | uint64_t rings; |
ae31fb54 | 136 | } TestServer; |
bd95939f | 137 | |
ca06d9cc PB |
138 | #if !GLIB_CHECK_VERSION(2, 32, 0) |
139 | static gboolean g_cond_wait_until(CompatGCond cond, CompatGMutex mutex, | |
140 | gint64 end_time) | |
bd95939f NN |
141 | { |
142 | gboolean ret = FALSE; | |
ca06d9cc | 143 | end_time -= g_get_monotonic_time(); |
bd95939f NN |
144 | GTimeVal time = { end_time / G_TIME_SPAN_SECOND, |
145 | end_time % G_TIME_SPAN_SECOND }; | |
146 | ret = g_cond_timed_wait(cond, mutex, &time); | |
bd95939f NN |
147 | return ret; |
148 | } | |
bd95939f | 149 | #endif |
a77e6b14 | 150 | |
704b2168 MAL |
151 | static const char *tmpfs; |
152 | static const char *root; | |
153 | ||
ae31fb54 | 154 | static void wait_for_fds(TestServer *s) |
a77e6b14 | 155 | { |
a77e6b14 | 156 | gint64 end_time; |
a77e6b14 | 157 | |
ae31fb54 | 158 | g_mutex_lock(&s->data_mutex); |
a77e6b14 | 159 | |
ca06d9cc | 160 | end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; |
ae31fb54 MAL |
161 | while (!s->fds_num) { |
162 | if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { | |
a77e6b14 | 163 | /* timeout has passed */ |
ae31fb54 | 164 | g_assert(s->fds_num); |
a77e6b14 NN |
165 | break; |
166 | } | |
167 | } | |
168 | ||
169 | /* check for sanity */ | |
ae31fb54 MAL |
170 | g_assert_cmpint(s->fds_num, >, 0); |
171 | g_assert_cmpint(s->fds_num, ==, s->memory.nregions); | |
a77e6b14 | 172 | |
ae31fb54 | 173 | g_mutex_unlock(&s->data_mutex); |
cf72b57f MAL |
174 | } |
175 | ||
041088c7 | 176 | static void read_guest_mem(const void *data) |
cf72b57f | 177 | { |
041088c7 | 178 | TestServer *s = (void *)data; |
cf72b57f MAL |
179 | uint32_t *guest_mem; |
180 | int i, j; | |
181 | size_t size; | |
182 | ||
ae31fb54 | 183 | wait_for_fds(s); |
cf72b57f | 184 | |
ae31fb54 | 185 | g_mutex_lock(&s->data_mutex); |
cf72b57f | 186 | |
a77e6b14 | 187 | /* iterate all regions */ |
ae31fb54 | 188 | for (i = 0; i < s->fds_num; i++) { |
a77e6b14 NN |
189 | |
190 | /* We'll check only the region statring at 0x0*/ | |
ae31fb54 | 191 | if (s->memory.regions[i].guest_phys_addr != 0x0) { |
a77e6b14 NN |
192 | continue; |
193 | } | |
194 | ||
ae31fb54 | 195 | g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); |
a77e6b14 | 196 | |
ae31fb54 MAL |
197 | size = s->memory.regions[i].memory_size + |
198 | s->memory.regions[i].mmap_offset; | |
d6970e3b NN |
199 | |
200 | guest_mem = mmap(0, size, PROT_READ | PROT_WRITE, | |
ae31fb54 | 201 | MAP_SHARED, s->fds[i], 0); |
d6970e3b NN |
202 | |
203 | g_assert(guest_mem != MAP_FAILED); | |
ae31fb54 | 204 | guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem)); |
a77e6b14 NN |
205 | |
206 | for (j = 0; j < 256; j++) { | |
ae31fb54 | 207 | uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4); |
a77e6b14 NN |
208 | uint32_t b = guest_mem[j]; |
209 | ||
210 | g_assert_cmpint(a, ==, b); | |
211 | } | |
212 | ||
ae31fb54 | 213 | munmap(guest_mem, s->memory.regions[i].memory_size); |
a77e6b14 NN |
214 | } |
215 | ||
ae31fb54 | 216 | g_mutex_unlock(&s->data_mutex); |
a77e6b14 NN |
217 | } |
218 | ||
219 | static void *thread_function(void *data) | |
220 | { | |
9732baf6 | 221 | GMainLoop *loop = data; |
a77e6b14 NN |
222 | g_main_loop_run(loop); |
223 | return NULL; | |
224 | } | |
225 | ||
226 | static int chr_can_read(void *opaque) | |
227 | { | |
228 | return VHOST_USER_HDR_SIZE; | |
229 | } | |
230 | ||
231 | static void chr_read(void *opaque, const uint8_t *buf, int size) | |
232 | { | |
ae31fb54 MAL |
233 | TestServer *s = opaque; |
234 | CharDriverState *chr = s->chr; | |
a77e6b14 NN |
235 | VhostUserMsg msg; |
236 | uint8_t *p = (uint8_t *) &msg; | |
237 | int fd; | |
238 | ||
239 | if (size != VHOST_USER_HDR_SIZE) { | |
240 | g_test_message("Wrong message size received %d\n", size); | |
241 | return; | |
242 | } | |
243 | ||
ae31fb54 | 244 | g_mutex_lock(&s->data_mutex); |
a77e6b14 NN |
245 | memcpy(p, buf, VHOST_USER_HDR_SIZE); |
246 | ||
247 | if (msg.size) { | |
248 | p += VHOST_USER_HDR_SIZE; | |
ae31fb54 | 249 | g_assert_cmpint(qemu_chr_fe_read_all(chr, p, msg.size), ==, msg.size); |
a77e6b14 NN |
250 | } |
251 | ||
252 | switch (msg.request) { | |
253 | case VHOST_USER_GET_FEATURES: | |
8a9b6b37 MT |
254 | /* send back features to qemu */ |
255 | msg.flags |= VHOST_USER_REPLY_MASK; | |
12ebf690 MT |
256 | msg.size = sizeof(m.payload.u64); |
257 | msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL | | |
b1819747 | 258 | 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES; |
8a9b6b37 MT |
259 | p = (uint8_t *) &msg; |
260 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); | |
261 | break; | |
262 | ||
263 | case VHOST_USER_SET_FEATURES: | |
12ebf690 | 264 | g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES), |
8a9b6b37 MT |
265 | !=, 0ULL); |
266 | break; | |
267 | ||
268 | case VHOST_USER_GET_PROTOCOL_FEATURES: | |
a77e6b14 NN |
269 | /* send back features to qemu */ |
270 | msg.flags |= VHOST_USER_REPLY_MASK; | |
12ebf690 MT |
271 | msg.size = sizeof(m.payload.u64); |
272 | msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD; | |
a77e6b14 NN |
273 | p = (uint8_t *) &msg; |
274 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); | |
275 | break; | |
276 | ||
277 | case VHOST_USER_GET_VRING_BASE: | |
278 | /* send back vring base to qemu */ | |
279 | msg.flags |= VHOST_USER_REPLY_MASK; | |
12ebf690 MT |
280 | msg.size = sizeof(m.payload.state); |
281 | msg.payload.state.num = 0; | |
a77e6b14 NN |
282 | p = (uint8_t *) &msg; |
283 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); | |
d08e42a1 MT |
284 | |
285 | assert(msg.payload.state.index < 2); | |
286 | s->rings &= ~(0x1ULL << msg.payload.state.index); | |
a77e6b14 NN |
287 | break; |
288 | ||
289 | case VHOST_USER_SET_MEM_TABLE: | |
290 | /* received the mem table */ | |
12ebf690 | 291 | memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory)); |
ae31fb54 | 292 | s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(s->fds)); |
a77e6b14 NN |
293 | |
294 | /* signal the test that it can continue */ | |
ae31fb54 | 295 | g_cond_signal(&s->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; | |
b1819747 MAL |
309 | |
310 | case VHOST_USER_SET_LOG_BASE: | |
311 | if (s->log_fd != -1) { | |
312 | close(s->log_fd); | |
313 | s->log_fd = -1; | |
314 | } | |
315 | qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1); | |
316 | msg.flags |= VHOST_USER_REPLY_MASK; | |
317 | msg.size = 0; | |
318 | p = (uint8_t *) &msg; | |
319 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE); | |
320 | ||
321 | g_cond_signal(&s->data_cond); | |
322 | break; | |
323 | ||
d08e42a1 MT |
324 | case VHOST_USER_SET_VRING_BASE: |
325 | assert(msg.payload.state.index < 2); | |
326 | s->rings |= 0x1ULL << msg.payload.state.index; | |
1d9edff7 MAL |
327 | break; |
328 | ||
a77e6b14 NN |
329 | default: |
330 | break; | |
331 | } | |
ae31fb54 MAL |
332 | |
333 | g_mutex_unlock(&s->data_mutex); | |
a77e6b14 NN |
334 | } |
335 | ||
1b7e1e3b | 336 | static const char *init_hugepagefs(const char *path) |
a77e6b14 | 337 | { |
a77e6b14 NN |
338 | struct statfs fs; |
339 | int ret; | |
340 | ||
a77e6b14 NN |
341 | if (access(path, R_OK | W_OK | X_OK)) { |
342 | g_test_message("access on path (%s): %s\n", path, strerror(errno)); | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | do { | |
347 | ret = statfs(path, &fs); | |
348 | } while (ret != 0 && errno == EINTR); | |
349 | ||
350 | if (ret != 0) { | |
351 | g_test_message("statfs on path (%s): %s\n", path, strerror(errno)); | |
352 | return NULL; | |
353 | } | |
354 | ||
355 | if (fs.f_type != HUGETLBFS_MAGIC) { | |
356 | g_test_message("Warning: path not on HugeTLBFS: %s\n", path); | |
357 | return NULL; | |
358 | } | |
359 | ||
360 | return path; | |
361 | } | |
362 | ||
704b2168 | 363 | static TestServer *test_server_new(const gchar *name) |
ae31fb54 MAL |
364 | { |
365 | TestServer *server = g_new0(TestServer, 1); | |
366 | gchar *chr_path; | |
367 | ||
368 | server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name); | |
a899b1ea | 369 | server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name); |
ae31fb54 MAL |
370 | |
371 | chr_path = g_strdup_printf("unix:%s,server,nowait", server->socket_path); | |
372 | server->chr_name = g_strdup_printf("chr-%s", name); | |
373 | server->chr = qemu_chr_new(server->chr_name, chr_path, NULL); | |
374 | g_free(chr_path); | |
375 | ||
376 | qemu_chr_add_handlers(server->chr, chr_can_read, chr_read, NULL, server); | |
377 | ||
378 | g_mutex_init(&server->data_mutex); | |
379 | g_cond_init(&server->data_cond); | |
380 | ||
b1819747 MAL |
381 | server->log_fd = -1; |
382 | ||
ae31fb54 MAL |
383 | return server; |
384 | } | |
385 | ||
704b2168 MAL |
386 | #define GET_QEMU_CMD(s) \ |
387 | g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ | |
388 | (s)->socket_path, (s)->chr_name) | |
ae31fb54 | 389 | |
704b2168 MAL |
390 | #define GET_QEMU_CMDE(s, mem, extra, ...) \ |
391 | g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ | |
392 | (s)->socket_path, (s)->chr_name, ##__VA_ARGS__) | |
ae31fb54 | 393 | |
9732baf6 | 394 | static gboolean _test_server_free(TestServer *server) |
ae31fb54 MAL |
395 | { |
396 | int i; | |
397 | ||
398 | qemu_chr_delete(server->chr); | |
399 | ||
400 | for (i = 0; i < server->fds_num; i++) { | |
401 | close(server->fds[i]); | |
402 | } | |
403 | ||
b1819747 MAL |
404 | if (server->log_fd != -1) { |
405 | close(server->log_fd); | |
406 | } | |
407 | ||
ae31fb54 MAL |
408 | unlink(server->socket_path); |
409 | g_free(server->socket_path); | |
410 | ||
a899b1ea MAL |
411 | unlink(server->mig_path); |
412 | g_free(server->mig_path); | |
413 | ||
b1819747 | 414 | g_free(server->chr_name); |
ae31fb54 | 415 | g_free(server); |
9732baf6 MAL |
416 | |
417 | return FALSE; | |
418 | } | |
419 | ||
420 | static void test_server_free(TestServer *server) | |
421 | { | |
422 | g_idle_add((GSourceFunc)_test_server_free, server); | |
ae31fb54 MAL |
423 | } |
424 | ||
b1819747 MAL |
425 | static void wait_for_log_fd(TestServer *s) |
426 | { | |
427 | gint64 end_time; | |
428 | ||
429 | g_mutex_lock(&s->data_mutex); | |
430 | end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; | |
431 | while (s->log_fd == -1) { | |
432 | if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { | |
433 | /* timeout has passed */ | |
434 | g_assert(s->log_fd != -1); | |
435 | break; | |
436 | } | |
437 | } | |
438 | ||
439 | g_mutex_unlock(&s->data_mutex); | |
440 | } | |
441 | ||
3a87d009 | 442 | static void write_guest_mem(TestServer *s, uint32_t seed) |
b1819747 MAL |
443 | { |
444 | uint32_t *guest_mem; | |
445 | int i, j; | |
446 | size_t size; | |
447 | ||
448 | wait_for_fds(s); | |
449 | ||
450 | /* iterate all regions */ | |
451 | for (i = 0; i < s->fds_num; i++) { | |
452 | ||
453 | /* We'll write only the region statring at 0x0 */ | |
454 | if (s->memory.regions[i].guest_phys_addr != 0x0) { | |
455 | continue; | |
456 | } | |
457 | ||
458 | g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); | |
459 | ||
460 | size = s->memory.regions[i].memory_size + | |
461 | s->memory.regions[i].mmap_offset; | |
462 | ||
463 | guest_mem = mmap(0, size, PROT_READ | PROT_WRITE, | |
464 | MAP_SHARED, s->fds[i], 0); | |
465 | ||
466 | g_assert(guest_mem != MAP_FAILED); | |
467 | guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem)); | |
468 | ||
469 | for (j = 0; j < 256; j++) { | |
470 | guest_mem[j] = seed + j; | |
471 | } | |
472 | ||
473 | munmap(guest_mem, s->memory.regions[i].memory_size); | |
474 | break; | |
475 | } | |
476 | } | |
477 | ||
478 | static guint64 get_log_size(TestServer *s) | |
479 | { | |
480 | guint64 log_size = 0; | |
481 | int i; | |
482 | ||
483 | for (i = 0; i < s->memory.nregions; ++i) { | |
484 | VhostUserMemoryRegion *reg = &s->memory.regions[i]; | |
485 | guint64 last = range_get_last(reg->guest_phys_addr, | |
486 | reg->memory_size); | |
487 | log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1); | |
488 | } | |
489 | ||
490 | return log_size; | |
491 | } | |
492 | ||
1d9edff7 MAL |
493 | typedef struct TestMigrateSource { |
494 | GSource source; | |
495 | TestServer *src; | |
496 | TestServer *dest; | |
497 | } TestMigrateSource; | |
498 | ||
499 | static gboolean | |
500 | test_migrate_source_check(GSource *source) | |
501 | { | |
502 | TestMigrateSource *t = (TestMigrateSource *)source; | |
d08e42a1 | 503 | gboolean overlap = t->src->rings && t->dest->rings; |
1d9edff7 MAL |
504 | |
505 | g_assert(!overlap); | |
506 | ||
507 | return FALSE; | |
508 | } | |
509 | ||
45ce5126 MAL |
510 | #if !GLIB_CHECK_VERSION(2,36,0) |
511 | /* this callback is unnecessary with glib >2.36, the default | |
512 | * prepare for the source does the same */ | |
513 | static gboolean | |
514 | test_migrate_source_prepare(GSource *source, gint *timeout) | |
515 | { | |
516 | *timeout = -1; | |
517 | return FALSE; | |
518 | } | |
519 | #endif | |
520 | ||
1d9edff7 | 521 | GSourceFuncs test_migrate_source_funcs = { |
45ce5126 MAL |
522 | #if !GLIB_CHECK_VERSION(2,36,0) |
523 | .prepare = test_migrate_source_prepare, | |
524 | #endif | |
525 | .check = test_migrate_source_check, | |
1d9edff7 MAL |
526 | }; |
527 | ||
b1819747 MAL |
528 | static void test_migrate(void) |
529 | { | |
530 | TestServer *s = test_server_new("src"); | |
531 | TestServer *dest = test_server_new("dest"); | |
a899b1ea | 532 | char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path); |
b1819747 | 533 | QTestState *global = global_qtest, *from, *to; |
1d9edff7 | 534 | GSource *source; |
b1819747 MAL |
535 | gchar *cmd; |
536 | QDict *rsp; | |
537 | guint8 *log; | |
538 | guint64 size; | |
539 | ||
540 | cmd = GET_QEMU_CMDE(s, 2, ""); | |
541 | from = qtest_start(cmd); | |
542 | g_free(cmd); | |
543 | ||
544 | wait_for_fds(s); | |
545 | size = get_log_size(s); | |
546 | g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8)); | |
547 | ||
548 | cmd = GET_QEMU_CMDE(dest, 2, " -incoming %s", uri); | |
549 | to = qtest_init(cmd); | |
550 | g_free(cmd); | |
551 | ||
1d9edff7 MAL |
552 | source = g_source_new(&test_migrate_source_funcs, |
553 | sizeof(TestMigrateSource)); | |
554 | ((TestMigrateSource *)source)->src = s; | |
555 | ((TestMigrateSource *)source)->dest = dest; | |
556 | g_source_attach(source, NULL); | |
557 | ||
b1819747 MAL |
558 | /* slow down migration to have time to fiddle with log */ |
559 | /* TODO: qtest could learn to break on some places */ | |
560 | rsp = qmp("{ 'execute': 'migrate_set_speed'," | |
561 | "'arguments': { 'value': 10 } }"); | |
562 | g_assert(qdict_haskey(rsp, "return")); | |
563 | QDECREF(rsp); | |
564 | ||
565 | cmd = g_strdup_printf("{ 'execute': 'migrate'," | |
566 | "'arguments': { 'uri': '%s' } }", | |
567 | uri); | |
568 | rsp = qmp(cmd); | |
569 | g_free(cmd); | |
570 | g_assert(qdict_haskey(rsp, "return")); | |
571 | QDECREF(rsp); | |
572 | ||
573 | wait_for_log_fd(s); | |
574 | ||
575 | log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0); | |
576 | g_assert(log != MAP_FAILED); | |
577 | ||
578 | /* modify first page */ | |
579 | write_guest_mem(s, 0x42); | |
580 | log[0] = 1; | |
581 | munmap(log, size); | |
582 | ||
583 | /* speed things up */ | |
584 | rsp = qmp("{ 'execute': 'migrate_set_speed'," | |
585 | "'arguments': { 'value': 0 } }"); | |
586 | g_assert(qdict_haskey(rsp, "return")); | |
587 | QDECREF(rsp); | |
588 | ||
589 | qmp_eventwait("STOP"); | |
590 | ||
591 | global_qtest = to; | |
592 | qmp_eventwait("RESUME"); | |
593 | ||
594 | read_guest_mem(dest); | |
595 | ||
1d9edff7 MAL |
596 | g_source_destroy(source); |
597 | g_source_unref(source); | |
598 | ||
b1819747 MAL |
599 | qtest_quit(to); |
600 | test_server_free(dest); | |
601 | qtest_quit(from); | |
602 | test_server_free(s); | |
a899b1ea | 603 | g_free(uri); |
b1819747 MAL |
604 | |
605 | global_qtest = global; | |
606 | } | |
607 | ||
a77e6b14 NN |
608 | int main(int argc, char **argv) |
609 | { | |
610 | QTestState *s = NULL; | |
ae31fb54 | 611 | TestServer *server = NULL; |
1b7e1e3b | 612 | const char *hugefs; |
ae31fb54 | 613 | char *qemu_cmd = NULL; |
a77e6b14 | 614 | int ret; |
1b7e1e3b | 615 | char template[] = "/tmp/vhost-test-XXXXXX"; |
9732baf6 MAL |
616 | GMainLoop *loop; |
617 | GThread *thread; | |
a77e6b14 NN |
618 | |
619 | g_test_init(&argc, &argv, NULL); | |
620 | ||
621 | module_call_init(MODULE_INIT_QOM); | |
ae31fb54 | 622 | qemu_add_opts(&qemu_chardev_opts); |
a77e6b14 | 623 | |
1b7e1e3b MT |
624 | tmpfs = mkdtemp(template); |
625 | if (!tmpfs) { | |
ae31fb54 | 626 | g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno)); |
1b7e1e3b MT |
627 | } |
628 | g_assert(tmpfs); | |
629 | ||
630 | hugefs = getenv("QTEST_HUGETLBFS_PATH"); | |
631 | if (hugefs) { | |
632 | root = init_hugepagefs(hugefs); | |
633 | g_assert(root); | |
634 | } else { | |
635 | root = tmpfs; | |
a77e6b14 NN |
636 | } |
637 | ||
704b2168 | 638 | server = test_server_new("test"); |
a77e6b14 | 639 | |
9732baf6 | 640 | loop = g_main_loop_new(NULL, FALSE); |
a77e6b14 | 641 | /* run the main loop thread so the chardev may operate */ |
9732baf6 | 642 | thread = g_thread_new(NULL, thread_function, loop); |
a77e6b14 | 643 | |
704b2168 | 644 | qemu_cmd = GET_QEMU_CMD(server); |
ae31fb54 | 645 | |
a77e6b14 NN |
646 | s = qtest_start(qemu_cmd); |
647 | g_free(qemu_cmd); | |
648 | ||
ae31fb54 | 649 | qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem); |
b1819747 | 650 | qtest_add_func("/vhost-user/migrate", test_migrate); |
a77e6b14 NN |
651 | |
652 | ret = g_test_run(); | |
653 | ||
654 | if (s) { | |
655 | qtest_quit(s); | |
656 | } | |
657 | ||
658 | /* cleanup */ | |
ae31fb54 | 659 | test_server_free(server); |
a77e6b14 | 660 | |
9732baf6 MAL |
661 | /* finish the helper thread and dispatch pending sources */ |
662 | g_main_loop_quit(loop); | |
663 | g_thread_join(thread); | |
664 | while (g_main_context_pending(NULL)) { | |
665 | g_main_context_iteration (NULL, TRUE); | |
666 | } | |
667 | g_main_loop_unref(loop); | |
668 | ||
1b7e1e3b MT |
669 | ret = rmdir(tmpfs); |
670 | if (ret != 0) { | |
671 | g_test_message("unable to rmdir: path (%s): %s\n", | |
672 | tmpfs, strerror(errno)); | |
673 | } | |
674 | g_assert_cmpint(ret, ==, 0); | |
675 | ||
a77e6b14 NN |
676 | return ret; |
677 | } |