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