]>
Commit | Line | Data |
---|---|---|
ea0c6d62 | 1 | /* |
2656bfd9 | 2 | * QTest testcase for migration |
ea0c6d62 | 3 | * |
17ca7746 | 4 | * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates |
ea0c6d62 DDAG |
5 | * based on the vhost-user-test.c that is: |
6 | * Copyright (c) 2014 Virtual Open Systems Sarl. | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
9 | * See the COPYING file in the top-level directory. | |
10 | * | |
11 | */ | |
12 | ||
13 | #include "qemu/osdep.h" | |
ea0c6d62 DDAG |
14 | |
15 | #include "libqtest.h" | |
452fcdbc | 16 | #include "qapi/qmp/qdict.h" |
b5bbd3f3 | 17 | #include "qapi/qmp/qjson.h" |
0b8fa32f | 18 | #include "qemu/module.h" |
ea0c6d62 DDAG |
19 | #include "qemu/option.h" |
20 | #include "qemu/range.h" | |
a9c94277 | 21 | #include "qemu/sockets.h" |
8228e353 | 22 | #include "chardev/char.h" |
609d3844 JQ |
23 | #include "qapi/qapi-visit-sockets.h" |
24 | #include "qapi/qobject-input-visitor.h" | |
25 | #include "qapi/qobject-output-visitor.h" | |
ea0c6d62 | 26 | |
e51e711b WH |
27 | #include "migration/migration-test.h" |
28 | ||
055a1efc MA |
29 | /* TODO actually test the results and get rid of this */ |
30 | #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__)) | |
31 | ||
e51e711b WH |
32 | unsigned start_address; |
33 | unsigned end_address; | |
ea0c6d62 | 34 | bool got_stop; |
346f3dab | 35 | static bool uffd_feature_thread_id; |
ea0c6d62 DDAG |
36 | |
37 | #if defined(__linux__) | |
ea0c6d62 DDAG |
38 | #include <sys/syscall.h> |
39 | #include <sys/vfs.h> | |
40 | #endif | |
41 | ||
42 | #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD) | |
43 | #include <sys/eventfd.h> | |
44 | #include <sys/ioctl.h> | |
45 | #include <linux/userfaultfd.h> | |
46 | ||
47 | static bool ufd_version_check(void) | |
48 | { | |
49 | struct uffdio_api api_struct; | |
50 | uint64_t ioctl_mask; | |
51 | ||
e1ae9fb6 | 52 | int ufd = syscall(__NR_userfaultfd, O_CLOEXEC); |
ea0c6d62 DDAG |
53 | |
54 | if (ufd == -1) { | |
55 | g_test_message("Skipping test: userfaultfd not available"); | |
56 | return false; | |
57 | } | |
58 | ||
59 | api_struct.api = UFFD_API; | |
60 | api_struct.features = 0; | |
61 | if (ioctl(ufd, UFFDIO_API, &api_struct)) { | |
62 | g_test_message("Skipping test: UFFDIO_API failed"); | |
63 | return false; | |
64 | } | |
346f3dab | 65 | uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID; |
ea0c6d62 DDAG |
66 | |
67 | ioctl_mask = (__u64)1 << _UFFDIO_REGISTER | | |
68 | (__u64)1 << _UFFDIO_UNREGISTER; | |
69 | if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) { | |
70 | g_test_message("Skipping test: Missing userfault feature"); | |
71 | return false; | |
72 | } | |
73 | ||
74 | return true; | |
75 | } | |
76 | ||
77 | #else | |
78 | static bool ufd_version_check(void) | |
79 | { | |
80 | g_test_message("Skipping test: Userfault not available (builtdtime)"); | |
81 | return false; | |
82 | } | |
83 | ||
84 | #endif | |
85 | ||
86 | static const char *tmpfs; | |
87 | ||
e51e711b WH |
88 | /* The boot file modifies memory area in [start_address, end_address) |
89 | * repeatedly. It outputs a 'B' at a fixed rate while it's still running. | |
ea0c6d62 | 90 | */ |
d54927ef | 91 | #include "tests/migration/i386/a-b-bootblock.h" |
c02b3781 | 92 | #include "tests/migration/aarch64/a-b-kernel.h" |
5571dc82 TH |
93 | #include "tests/migration/s390x/a-b-bios.h" |
94 | ||
2785f196 | 95 | static void init_bootfile(const char *bootpath, void *content, size_t len) |
5571dc82 TH |
96 | { |
97 | FILE *bootfile = fopen(bootpath, "wb"); | |
5571dc82 | 98 | |
2785f196 | 99 | g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1); |
5571dc82 TH |
100 | fclose(bootfile); |
101 | } | |
102 | ||
ea0c6d62 DDAG |
103 | /* |
104 | * Wait for some output in the serial output file, | |
105 | * we get an 'A' followed by an endless string of 'B's | |
106 | * but on the destination we won't have the A. | |
107 | */ | |
108 | static void wait_for_serial(const char *side) | |
109 | { | |
110 | char *serialpath = g_strdup_printf("%s/%s", tmpfs, side); | |
111 | FILE *serialfile = fopen(serialpath, "r"); | |
aaf89c8a | 112 | const char *arch = qtest_get_arch(); |
113 | int started = (strcmp(side, "src_serial") == 0 && | |
114 | strcmp(arch, "ppc64") == 0) ? 0 : 1; | |
ea0c6d62 | 115 | |
e2dd21e5 | 116 | g_free(serialpath); |
ea0c6d62 DDAG |
117 | do { |
118 | int readvalue = fgetc(serialfile); | |
119 | ||
aaf89c8a | 120 | if (!started) { |
121 | /* SLOF prints its banner before starting test, | |
122 | * to ignore it, mark the start of the test with '_', | |
123 | * ignore all characters until this marker | |
124 | */ | |
125 | switch (readvalue) { | |
126 | case '_': | |
127 | started = 1; | |
128 | break; | |
129 | case EOF: | |
130 | fseek(serialfile, 0, SEEK_SET); | |
131 | usleep(1000); | |
132 | break; | |
133 | } | |
134 | continue; | |
135 | } | |
ea0c6d62 DDAG |
136 | switch (readvalue) { |
137 | case 'A': | |
138 | /* Fine */ | |
139 | break; | |
140 | ||
141 | case 'B': | |
142 | /* It's alive! */ | |
143 | fclose(serialfile); | |
ea0c6d62 DDAG |
144 | return; |
145 | ||
146 | case EOF: | |
aaf89c8a | 147 | started = (strcmp(side, "src_serial") == 0 && |
148 | strcmp(arch, "ppc64") == 0) ? 0 : 1; | |
ea0c6d62 DDAG |
149 | fseek(serialfile, 0, SEEK_SET); |
150 | usleep(1000); | |
151 | break; | |
152 | ||
153 | default: | |
154 | fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side); | |
155 | g_assert_not_reached(); | |
156 | } | |
157 | } while (true); | |
158 | } | |
159 | ||
3cd46d42 MA |
160 | static void stop_cb(void *opaque, const char *name, QDict *data) |
161 | { | |
162 | if (!strcmp(name, "STOP")) { | |
163 | got_stop = true; | |
164 | } | |
165 | } | |
166 | ||
24d5588c YK |
167 | /* |
168 | * Events can get in the way of responses we are actually waiting for. | |
169 | */ | |
170 | GCC_FMT_ATTR(3, 4) | |
171 | static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...) | |
172 | { | |
173 | va_list ap; | |
174 | ||
175 | va_start(ap, command); | |
176 | qtest_qmp_vsend_fds(who, &fd, 1, command, ap); | |
177 | va_end(ap); | |
178 | ||
179 | return qtest_qmp_receive_success(who, stop_cb, NULL); | |
180 | } | |
181 | ||
ea0c6d62 DDAG |
182 | /* |
183 | * Events can get in the way of responses we are actually waiting for. | |
184 | */ | |
b7281c69 | 185 | GCC_FMT_ATTR(2, 3) |
4399596b | 186 | static QDict *wait_command(QTestState *who, const char *command, ...) |
ea0c6d62 | 187 | { |
4399596b MA |
188 | va_list ap; |
189 | ||
190 | va_start(ap, command); | |
191 | qtest_qmp_vsend(who, command, ap); | |
192 | va_end(ap); | |
193 | ||
3cd46d42 | 194 | return qtest_qmp_receive_success(who, stop_cb, NULL); |
ea0c6d62 DDAG |
195 | } |
196 | ||
2f7074c6 PX |
197 | /* |
198 | * Note: caller is responsible to free the returned object via | |
199 | * qobject_unref() after use | |
200 | */ | |
201 | static QDict *migrate_query(QTestState *who) | |
202 | { | |
e1454165 | 203 | return wait_command(who, "{ 'execute': 'query-migrate' }"); |
2f7074c6 PX |
204 | } |
205 | ||
206 | /* | |
207 | * Note: caller is responsible to free the returned object via | |
208 | * g_free() after use | |
209 | */ | |
210 | static gchar *migrate_query_status(QTestState *who) | |
211 | { | |
212 | QDict *rsp_return = migrate_query(who); | |
213 | gchar *status = g_strdup(qdict_get_str(rsp_return, "status")); | |
214 | ||
215 | g_assert(status); | |
216 | qobject_unref(rsp_return); | |
217 | ||
218 | return status; | |
219 | } | |
ea0c6d62 DDAG |
220 | |
221 | /* | |
222 | * It's tricky to use qemu's migration event capability with qtest, | |
223 | * events suddenly appearing confuse the qmp()/hmp() responses. | |
ea0c6d62 DDAG |
224 | */ |
225 | ||
660a9b68 | 226 | static int64_t read_ram_property_int(QTestState *who, const char *property) |
ea0c6d62 | 227 | { |
2f7074c6 | 228 | QDict *rsp_return, *rsp_ram; |
660a9b68 | 229 | int64_t result; |
ea0c6d62 | 230 | |
2f7074c6 | 231 | rsp_return = migrate_query(who); |
ea0c6d62 DDAG |
232 | if (!qdict_haskey(rsp_return, "ram")) { |
233 | /* Still in setup */ | |
234 | result = 0; | |
235 | } else { | |
236 | rsp_ram = qdict_get_qdict(rsp_return, "ram"); | |
660a9b68 | 237 | result = qdict_get_try_int(rsp_ram, property, 0); |
ea0c6d62 | 238 | } |
2f7074c6 | 239 | qobject_unref(rsp_return); |
ea0c6d62 DDAG |
240 | return result; |
241 | } | |
242 | ||
8c51642b YK |
243 | static int64_t read_migrate_property_int(QTestState *who, const char *property) |
244 | { | |
245 | QDict *rsp_return; | |
246 | int64_t result; | |
247 | ||
248 | rsp_return = migrate_query(who); | |
249 | result = qdict_get_try_int(rsp_return, property, 0); | |
250 | qobject_unref(rsp_return); | |
251 | return result; | |
252 | } | |
253 | ||
660a9b68 YK |
254 | static uint64_t get_migration_pass(QTestState *who) |
255 | { | |
256 | return read_ram_property_int(who, "dirty-sync-count"); | |
257 | } | |
258 | ||
346f3dab AP |
259 | static void read_blocktime(QTestState *who) |
260 | { | |
2f7074c6 | 261 | QDict *rsp_return; |
346f3dab | 262 | |
2f7074c6 | 263 | rsp_return = migrate_query(who); |
346f3dab | 264 | g_assert(qdict_haskey(rsp_return, "postcopy-blocktime")); |
2f7074c6 | 265 | qobject_unref(rsp_return); |
346f3dab AP |
266 | } |
267 | ||
8c51642b YK |
268 | static bool check_migration_status(QTestState *who, const char *goal, |
269 | const char **ungoals) | |
270 | { | |
271 | bool ready; | |
272 | char *current_status; | |
273 | const char **ungoal; | |
274 | ||
275 | current_status = migrate_query_status(who); | |
276 | ready = strcmp(current_status, goal) == 0; | |
277 | if (!ungoals) { | |
278 | g_assert_cmpstr(current_status, !=, "failed"); | |
279 | /* | |
280 | * If looking for a state other than completed, | |
281 | * completion of migration would cause the test to | |
282 | * hang. | |
283 | */ | |
284 | if (strcmp(goal, "completed") != 0) { | |
285 | g_assert_cmpstr(current_status, !=, "completed"); | |
286 | } | |
287 | } else { | |
288 | for (ungoal = ungoals; *ungoal; ungoal++) { | |
289 | g_assert_cmpstr(current_status, !=, *ungoal); | |
290 | } | |
291 | } | |
292 | g_free(current_status); | |
293 | return ready; | |
294 | } | |
295 | ||
2f6d3138 | 296 | static void wait_for_migration_status(QTestState *who, |
e15310ea DDAG |
297 | const char *goal, |
298 | const char **ungoals) | |
ea0c6d62 | 299 | { |
8c51642b | 300 | while (!check_migration_status(who, goal, ungoals)) { |
6a7724e9 JQ |
301 | usleep(1000); |
302 | } | |
ea0c6d62 DDAG |
303 | } |
304 | ||
2f6d3138 PX |
305 | static void wait_for_migration_complete(QTestState *who) |
306 | { | |
8c51642b | 307 | wait_for_migration_status(who, "completed", NULL); |
2f6d3138 PX |
308 | } |
309 | ||
863e27a8 | 310 | static void wait_for_migration_pass(QTestState *who) |
ea0c6d62 | 311 | { |
863e27a8 | 312 | uint64_t initial_pass = get_migration_pass(who); |
ea0c6d62 DDAG |
313 | uint64_t pass; |
314 | ||
315 | /* Wait for the 1st sync */ | |
6a7724e9 JQ |
316 | while (!got_stop && !initial_pass) { |
317 | usleep(1000); | |
863e27a8 | 318 | initial_pass = get_migration_pass(who); |
6a7724e9 | 319 | } |
ea0c6d62 DDAG |
320 | |
321 | do { | |
6a7724e9 | 322 | usleep(1000); |
863e27a8 | 323 | pass = get_migration_pass(who); |
ea0c6d62 DDAG |
324 | } while (pass == initial_pass && !got_stop); |
325 | } | |
326 | ||
7195a871 | 327 | static void check_guests_ram(QTestState *who) |
ea0c6d62 DDAG |
328 | { |
329 | /* Our ASM test will have been incrementing one byte from each page from | |
e51e711b WH |
330 | * start_address to < end_address in order. This gives us a constraint |
331 | * that any page's byte should be equal or less than the previous pages | |
332 | * byte (mod 256); and they should all be equal except for one transition | |
333 | * at the point where we meet the incrementer. (We're running this with | |
334 | * the guest stopped). | |
ea0c6d62 DDAG |
335 | */ |
336 | unsigned address; | |
337 | uint8_t first_byte; | |
338 | uint8_t last_byte; | |
339 | bool hit_edge = false; | |
601b5575 | 340 | int bad = 0; |
ea0c6d62 | 341 | |
7195a871 | 342 | qtest_memread(who, start_address, &first_byte, 1); |
ea0c6d62 DDAG |
343 | last_byte = first_byte; |
344 | ||
e51e711b WH |
345 | for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address; |
346 | address += TEST_MEM_PAGE_SIZE) | |
ea0c6d62 DDAG |
347 | { |
348 | uint8_t b; | |
7195a871 | 349 | qtest_memread(who, address, &b, 1); |
ea0c6d62 DDAG |
350 | if (b != last_byte) { |
351 | if (((b + 1) % 256) == last_byte && !hit_edge) { | |
352 | /* This is OK, the guest stopped at the point of | |
353 | * incrementing the previous page but didn't get | |
354 | * to us yet. | |
355 | */ | |
356 | hit_edge = true; | |
829db8b4 | 357 | last_byte = b; |
ea0c6d62 | 358 | } else { |
601b5575 AB |
359 | bad++; |
360 | if (bad <= 10) { | |
361 | fprintf(stderr, "Memory content inconsistency at %x" | |
362 | " first_byte = %x last_byte = %x current = %x" | |
363 | " hit_edge = %x\n", | |
364 | address, first_byte, last_byte, b, hit_edge); | |
365 | } | |
ea0c6d62 DDAG |
366 | } |
367 | } | |
ea0c6d62 | 368 | } |
601b5575 AB |
369 | if (bad >= 10) { |
370 | fprintf(stderr, "and in another %d pages", bad - 10); | |
371 | } | |
372 | g_assert(bad == 0); | |
ea0c6d62 DDAG |
373 | } |
374 | ||
375 | static void cleanup(const char *filename) | |
376 | { | |
377 | char *path = g_strdup_printf("%s/%s", tmpfs, filename); | |
378 | ||
379 | unlink(path); | |
e2dd21e5 | 380 | g_free(path); |
ea0c6d62 DDAG |
381 | } |
382 | ||
660a9b68 YK |
383 | static char *get_shmem_opts(const char *mem_size, const char *shmem_path) |
384 | { | |
385 | return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s" | |
386 | ",mem-path=%s,share=on -numa node,memdev=mem0", | |
387 | mem_size, shmem_path); | |
388 | } | |
389 | ||
609d3844 JQ |
390 | static char *SocketAddress_to_str(SocketAddress *addr) |
391 | { | |
392 | switch (addr->type) { | |
393 | case SOCKET_ADDRESS_TYPE_INET: | |
394 | return g_strdup_printf("tcp:%s:%s", | |
395 | addr->u.inet.host, | |
396 | addr->u.inet.port); | |
397 | case SOCKET_ADDRESS_TYPE_UNIX: | |
398 | return g_strdup_printf("unix:%s", | |
399 | addr->u.q_unix.path); | |
400 | case SOCKET_ADDRESS_TYPE_FD: | |
401 | return g_strdup_printf("fd:%s", addr->u.fd.str); | |
402 | case SOCKET_ADDRESS_TYPE_VSOCK: | |
403 | return g_strdup_printf("tcp:%s:%s", | |
404 | addr->u.vsock.cid, | |
405 | addr->u.vsock.port); | |
406 | default: | |
407 | return g_strdup("unknown address type"); | |
408 | } | |
409 | } | |
410 | ||
411 | static char *migrate_get_socket_address(QTestState *who, const char *parameter) | |
412 | { | |
413 | QDict *rsp; | |
414 | char *result; | |
415 | Error *local_err = NULL; | |
416 | SocketAddressList *addrs; | |
417 | Visitor *iv = NULL; | |
418 | QObject *object; | |
419 | ||
420 | rsp = migrate_query(who); | |
421 | object = qdict_get(rsp, parameter); | |
422 | ||
423 | iv = qobject_input_visitor_new(object); | |
424 | visit_type_SocketAddressList(iv, NULL, &addrs, &local_err); | |
1e25879e | 425 | visit_free(iv); |
609d3844 JQ |
426 | |
427 | /* we are only using a single address */ | |
1e25879e | 428 | result = SocketAddress_to_str(addrs->value); |
609d3844 JQ |
429 | |
430 | qapi_free_SocketAddressList(addrs); | |
431 | qobject_unref(rsp); | |
432 | return result; | |
433 | } | |
434 | ||
8f7798f1 JQ |
435 | static long long migrate_get_parameter_int(QTestState *who, |
436 | const char *parameter) | |
609d3844 JQ |
437 | { |
438 | QDict *rsp; | |
439 | long long result; | |
440 | ||
441 | rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }"); | |
442 | result = qdict_get_int(rsp, parameter); | |
443 | qobject_unref(rsp); | |
444 | return result; | |
445 | } | |
446 | ||
8f7798f1 JQ |
447 | static void migrate_check_parameter_int(QTestState *who, const char *parameter, |
448 | long long value) | |
56b4a42a | 449 | { |
609d3844 | 450 | long long result; |
56b4a42a | 451 | |
8f7798f1 | 452 | result = migrate_get_parameter_int(who, parameter); |
609d3844 | 453 | g_assert_cmpint(result, ==, value); |
56b4a42a JQ |
454 | } |
455 | ||
8f7798f1 JQ |
456 | static void migrate_set_parameter_int(QTestState *who, const char *parameter, |
457 | long long value) | |
d62fbe60 JQ |
458 | { |
459 | QDict *rsp; | |
d62fbe60 | 460 | |
c44a56d8 MA |
461 | rsp = qtest_qmp(who, |
462 | "{ 'execute': 'migrate-set-parameters'," | |
463 | "'arguments': { %s: %lld } }", | |
464 | parameter, value); | |
d62fbe60 | 465 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 466 | qobject_unref(rsp); |
8f7798f1 | 467 | migrate_check_parameter_int(who, parameter, value); |
d62fbe60 JQ |
468 | } |
469 | ||
d5f49640 PX |
470 | static void migrate_pause(QTestState *who) |
471 | { | |
472 | QDict *rsp; | |
473 | ||
474 | rsp = wait_command(who, "{ 'execute': 'migrate-pause' }"); | |
d5f49640 PX |
475 | qobject_unref(rsp); |
476 | } | |
477 | ||
8c51642b YK |
478 | static void migrate_continue(QTestState *who, const char *state) |
479 | { | |
480 | QDict *rsp; | |
481 | ||
482 | rsp = wait_command(who, | |
483 | "{ 'execute': 'migrate-continue'," | |
484 | " 'arguments': { 'state': %s } }", | |
485 | state); | |
486 | qobject_unref(rsp); | |
487 | } | |
488 | ||
d5f49640 PX |
489 | static void migrate_recover(QTestState *who, const char *uri) |
490 | { | |
491 | QDict *rsp; | |
d5f49640 | 492 | |
b7281c69 MA |
493 | rsp = wait_command(who, |
494 | "{ 'execute': 'migrate-recover', " | |
495 | " 'id': 'recover-cmd', " | |
496 | " 'arguments': { 'uri': %s } }", | |
497 | uri); | |
d5f49640 PX |
498 | qobject_unref(rsp); |
499 | } | |
500 | ||
d62fbe60 | 501 | static void migrate_set_capability(QTestState *who, const char *capability, |
c44a56d8 | 502 | bool value) |
d62fbe60 JQ |
503 | { |
504 | QDict *rsp; | |
c44a56d8 MA |
505 | |
506 | rsp = qtest_qmp(who, | |
507 | "{ 'execute': 'migrate-set-capabilities'," | |
508 | "'arguments': { " | |
509 | "'capabilities': [ { " | |
510 | "'capability': %s, 'state': %i } ] } }", | |
511 | capability, value); | |
d62fbe60 | 512 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 513 | qobject_unref(rsp); |
d62fbe60 JQ |
514 | } |
515 | ||
b5bbd3f3 MA |
516 | /* |
517 | * Send QMP command "migrate". | |
518 | * Arguments are built from @fmt... (formatted like | |
519 | * qobject_from_jsonf_nofail()) with "uri": @uri spliced in. | |
520 | */ | |
521 | GCC_FMT_ATTR(3, 4) | |
522 | static void migrate(QTestState *who, const char *uri, const char *fmt, ...) | |
d62fbe60 | 523 | { |
b5bbd3f3 MA |
524 | va_list ap; |
525 | QDict *args, *rsp; | |
d62fbe60 | 526 | |
b5bbd3f3 MA |
527 | va_start(ap, fmt); |
528 | args = qdict_from_vjsonf_nofail(fmt, ap); | |
529 | va_end(ap); | |
530 | ||
531 | g_assert(!qdict_haskey(args, "uri")); | |
532 | qdict_put_str(args, "uri", uri); | |
533 | ||
a2726593 | 534 | rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args); |
24d5588c | 535 | |
d62fbe60 | 536 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 537 | qobject_unref(rsp); |
d62fbe60 JQ |
538 | } |
539 | ||
d131662a | 540 | static void migrate_postcopy_start(QTestState *from, QTestState *to) |
eb665d7d JQ |
541 | { |
542 | QDict *rsp; | |
543 | ||
d131662a | 544 | rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }"); |
cb3e7f08 | 545 | qobject_unref(rsp); |
d131662a PX |
546 | |
547 | if (!got_stop) { | |
548 | qtest_qmp_eventwait(from, "STOP"); | |
549 | } | |
550 | ||
551 | qtest_qmp_eventwait(to, "RESUME"); | |
eb665d7d JQ |
552 | } |
553 | ||
5fd4a9c9 | 554 | static int test_migrate_start(QTestState **from, QTestState **to, |
660a9b68 | 555 | const char *uri, bool hide_stderr, |
3af31a34 YK |
556 | bool use_shmem, const char *opts_src, |
557 | const char *opts_dst) | |
ea0c6d62 | 558 | { |
d62fbe60 | 559 | gchar *cmd_src, *cmd_dst; |
8443415f | 560 | gchar *cmd_source, *cmd_target; |
1b023718 | 561 | const gchar *ignore_stderr; |
660a9b68 YK |
562 | char *bootpath = NULL; |
563 | char *extra_opts = NULL; | |
564 | char *shmem_path = NULL; | |
aaf89c8a | 565 | const char *arch = qtest_get_arch(); |
e022c277 JQ |
566 | const char *machine_type; |
567 | const char *machine_args; | |
7b6d44cb | 568 | const char *memory_size; |
ea0c6d62 | 569 | |
3af31a34 YK |
570 | opts_src = opts_src ? opts_src : ""; |
571 | opts_dst = opts_dst ? opts_dst : ""; | |
572 | ||
660a9b68 YK |
573 | if (use_shmem) { |
574 | if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) { | |
575 | g_test_skip("/dev/shm is not supported"); | |
576 | return -1; | |
577 | } | |
578 | shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid()); | |
579 | } | |
ea0c6d62 | 580 | |
660a9b68 YK |
581 | got_stop = false; |
582 | bootpath = g_strdup_printf("%s/bootsect", tmpfs); | |
aaf89c8a | 583 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
2785f196 PM |
584 | /* the assembled x86 boot sector should be exactly one sector large */ |
585 | assert(sizeof(x86_bootsect) == 512); | |
586 | init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect)); | |
e022c277 JQ |
587 | machine_type = ""; |
588 | machine_args = ""; | |
7b6d44cb JQ |
589 | memory_size = "150M"; |
590 | extra_opts = use_shmem ? get_shmem_opts(memory_size, shmem_path) : NULL; | |
591 | cmd_src = g_strdup_printf(" -name source,debug-threads=on" | |
aaf89c8a | 592 | " -serial file:%s/src_serial" |
8443415f | 593 | " -drive file=%s,format=raw %s", |
e022c277 | 594 | tmpfs, bootpath, |
8443415f | 595 | extra_opts ? extra_opts : ""); |
7b6d44cb | 596 | cmd_dst = g_strdup_printf(" -name target,debug-threads=on" |
aaf89c8a | 597 | " -serial file:%s/dest_serial" |
598 | " -drive file=%s,format=raw" | |
8443415f | 599 | " -incoming %s %s", |
e022c277 | 600 | tmpfs, bootpath, uri, |
8443415f | 601 | extra_opts ? extra_opts : ""); |
e51e711b WH |
602 | start_address = X86_TEST_MEM_START; |
603 | end_address = X86_TEST_MEM_END; | |
5571dc82 | 604 | } else if (g_str_equal(arch, "s390x")) { |
2785f196 | 605 | init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf)); |
e022c277 JQ |
606 | machine_type = ""; |
607 | machine_args = ""; | |
7b6d44cb JQ |
608 | memory_size = "128M"; |
609 | extra_opts = use_shmem ? get_shmem_opts(memory_size, shmem_path) : NULL; | |
610 | cmd_src = g_strdup_printf(" -name source,debug-threads=on" | |
8443415f | 611 | " -serial file:%s/src_serial -bios %s %s", |
e022c277 | 612 | tmpfs, bootpath, |
8443415f | 613 | extra_opts ? extra_opts : ""); |
7b6d44cb | 614 | cmd_dst = g_strdup_printf(" -name target,debug-threads=on" |
5571dc82 | 615 | " -serial file:%s/dest_serial -bios %s" |
8443415f | 616 | " -incoming %s %s", |
e022c277 | 617 | tmpfs, bootpath, uri, |
8443415f | 618 | extra_opts ? extra_opts : ""); |
5571dc82 TH |
619 | start_address = S390_TEST_MEM_START; |
620 | end_address = S390_TEST_MEM_END; | |
aaf89c8a | 621 | } else if (strcmp(arch, "ppc64") == 0) { |
e022c277 JQ |
622 | machine_type = ""; |
623 | machine_args = ",vsmt=8"; | |
7b6d44cb JQ |
624 | memory_size = "256M"; |
625 | extra_opts = use_shmem ? get_shmem_opts(memory_size, shmem_path) : NULL; | |
626 | cmd_src = g_strdup_printf("-nodefaults" | |
31a6bb74 | 627 | " -name source,debug-threads=on" |
aaf89c8a | 628 | " -serial file:%s/src_serial" |
fc71e3e5 TH |
629 | " -prom-env 'use-nvramrc?=true' -prom-env " |
630 | "'nvramrc=hex .\" _\" begin %x %x " | |
cdf33815 | 631 | "do i c@ 1 + i c! 1000 +loop .\" B\" 0 " |
e022c277 | 632 | "until' %s", tmpfs, end_address, |
8443415f | 633 | start_address, extra_opts ? extra_opts : ""); |
7b6d44cb | 634 | cmd_dst = g_strdup_printf(" -name target,debug-threads=on" |
aaf89c8a | 635 | " -serial file:%s/dest_serial" |
8443415f | 636 | " -incoming %s %s", |
e022c277 | 637 | tmpfs, uri, |
8443415f | 638 | extra_opts ? extra_opts : ""); |
e51e711b WH |
639 | |
640 | start_address = PPC_TEST_MEM_START; | |
641 | end_address = PPC_TEST_MEM_END; | |
c02b3781 | 642 | } else if (strcmp(arch, "aarch64") == 0) { |
2785f196 | 643 | init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel)); |
e022c277 JQ |
644 | machine_type = "virt,"; |
645 | machine_args = "gic-version=max"; | |
7b6d44cb JQ |
646 | memory_size = "150M"; |
647 | extra_opts = use_shmem ? get_shmem_opts(memory_size, shmem_path) : NULL; | |
e022c277 | 648 | cmd_src = g_strdup_printf("-name vmsource,debug-threads=on -cpu max " |
7b6d44cb | 649 | "-serial file:%s/src_serial " |
8443415f | 650 | "-kernel %s %s", |
e022c277 | 651 | tmpfs, bootpath, |
8443415f | 652 | extra_opts ? extra_opts : ""); |
e022c277 | 653 | cmd_dst = g_strdup_printf("-name vmdest,debug-threads=on -cpu max " |
7b6d44cb | 654 | "-serial file:%s/dest_serial " |
c02b3781 | 655 | "-kernel %s " |
8443415f | 656 | "-incoming %s %s", |
e022c277 | 657 | tmpfs, bootpath, uri, |
8443415f | 658 | extra_opts ? extra_opts : ""); |
c02b3781 WH |
659 | |
660 | start_address = ARM_TEST_MEM_START; | |
661 | end_address = ARM_TEST_MEM_END; | |
662 | ||
663 | g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE); | |
aaf89c8a | 664 | } else { |
665 | g_assert_not_reached(); | |
666 | } | |
667 | ||
e2dd21e5 | 668 | g_free(bootpath); |
660a9b68 | 669 | g_free(extra_opts); |
e2dd21e5 | 670 | |
f96d6651 | 671 | if (hide_stderr) { |
1b023718 JQ |
672 | ignore_stderr = "2>/dev/null"; |
673 | } else { | |
674 | ignore_stderr = ""; | |
f96d6651 DDAG |
675 | } |
676 | ||
7b6d44cb JQ |
677 | cmd_source = g_strdup_printf("-machine %saccel=kvm:tcg%s " |
678 | "-m %s " | |
679 | "%s %s %s", | |
e022c277 | 680 | machine_type, machine_args, |
7b6d44cb | 681 | memory_size, |
1b023718 | 682 | cmd_src, opts_src, ignore_stderr); |
aaf89c8a | 683 | g_free(cmd_src); |
8443415f JQ |
684 | *from = qtest_init(cmd_source); |
685 | g_free(cmd_source); | |
aaf89c8a | 686 | |
7b6d44cb JQ |
687 | cmd_target = g_strdup_printf("-machine %saccel=kvm:tcg%s " |
688 | "-m %s " | |
689 | "%s %s %s", | |
e022c277 | 690 | machine_type, machine_args, |
7b6d44cb | 691 | memory_size, |
1b023718 | 692 | cmd_dst, opts_dst, ignore_stderr); |
aaf89c8a | 693 | g_free(cmd_dst); |
8443415f JQ |
694 | *to = qtest_init(cmd_target); |
695 | g_free(cmd_target); | |
660a9b68 YK |
696 | |
697 | /* | |
698 | * Remove shmem file immediately to avoid memory leak in test failed case. | |
699 | * It's valid becase QEMU has already opened this file | |
700 | */ | |
701 | if (use_shmem) { | |
702 | unlink(shmem_path); | |
703 | g_free(shmem_path); | |
704 | } | |
705 | ||
5fd4a9c9 | 706 | return 0; |
7195a871 JQ |
707 | } |
708 | ||
2c9bb297 | 709 | static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) |
7195a871 JQ |
710 | { |
711 | unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; | |
712 | ||
713 | qtest_quit(from); | |
714 | ||
2c9bb297 DDAG |
715 | if (test_dest) { |
716 | qtest_memread(to, start_address, &dest_byte_a, 1); | |
7195a871 | 717 | |
2c9bb297 DDAG |
718 | /* Destination still running, wait for a byte to change */ |
719 | do { | |
720 | qtest_memread(to, start_address, &dest_byte_b, 1); | |
721 | usleep(1000 * 10); | |
722 | } while (dest_byte_a == dest_byte_b); | |
723 | ||
724 | qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}"); | |
7195a871 | 725 | |
2c9bb297 DDAG |
726 | /* With it stopped, check nothing changes */ |
727 | qtest_memread(to, start_address, &dest_byte_c, 1); | |
728 | usleep(1000 * 200); | |
729 | qtest_memread(to, start_address, &dest_byte_d, 1); | |
730 | g_assert_cmpint(dest_byte_c, ==, dest_byte_d); | |
7195a871 | 731 | |
2c9bb297 DDAG |
732 | check_guests_ram(to); |
733 | } | |
7195a871 JQ |
734 | |
735 | qtest_quit(to); | |
736 | ||
737 | cleanup("bootsect"); | |
738 | cleanup("migsocket"); | |
739 | cleanup("src_serial"); | |
740 | cleanup("dest_serial"); | |
741 | } | |
742 | ||
4c27486d JQ |
743 | static void deprecated_set_downtime(QTestState *who, const double value) |
744 | { | |
745 | QDict *rsp; | |
4c27486d | 746 | |
015715f5 MA |
747 | rsp = qtest_qmp(who, |
748 | "{ 'execute': 'migrate_set_downtime'," | |
749 | " 'arguments': { 'value': %f } }", value); | |
4c27486d | 750 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 751 | qobject_unref(rsp); |
8f7798f1 | 752 | migrate_check_parameter_int(who, "downtime-limit", value * 1000); |
4c27486d JQ |
753 | } |
754 | ||
c44a56d8 | 755 | static void deprecated_set_speed(QTestState *who, long long value) |
4c27486d JQ |
756 | { |
757 | QDict *rsp; | |
4c27486d | 758 | |
c44a56d8 MA |
759 | rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed'," |
760 | "'arguments': { 'value': %lld } }", value); | |
4c27486d | 761 | g_assert(qdict_haskey(rsp, "return")); |
cb3e7f08 | 762 | qobject_unref(rsp); |
8f7798f1 | 763 | migrate_check_parameter_int(who, "max-bandwidth", value); |
4c27486d JQ |
764 | } |
765 | ||
cdf84229 JQ |
766 | static void deprecated_set_cache_size(QTestState *who, long long value) |
767 | { | |
768 | QDict *rsp; | |
769 | ||
770 | rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size'," | |
771 | "'arguments': { 'value': %lld } }", value); | |
772 | g_assert(qdict_haskey(rsp, "return")); | |
773 | qobject_unref(rsp); | |
8f7798f1 | 774 | migrate_check_parameter_int(who, "xbzrle-cache-size", value); |
cdf84229 JQ |
775 | } |
776 | ||
4c27486d JQ |
777 | static void test_deprecated(void) |
778 | { | |
779 | QTestState *from; | |
780 | ||
a2726593 | 781 | from = qtest_init("-machine none"); |
4c27486d JQ |
782 | |
783 | deprecated_set_downtime(from, 0.12345); | |
c44a56d8 | 784 | deprecated_set_speed(from, 12345); |
cdf84229 | 785 | deprecated_set_cache_size(from, 4096); |
4c27486d JQ |
786 | |
787 | qtest_quit(from); | |
788 | } | |
789 | ||
d131662a | 790 | static int migrate_postcopy_prepare(QTestState **from_ptr, |
3e81f73c PX |
791 | QTestState **to_ptr, |
792 | bool hide_error) | |
7195a871 JQ |
793 | { |
794 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
863e27a8 | 795 | QTestState *from, *to; |
7195a871 | 796 | |
3af31a34 | 797 | if (test_migrate_start(&from, &to, uri, hide_error, false, NULL, NULL)) { |
d131662a | 798 | return -1; |
5fd4a9c9 | 799 | } |
ea0c6d62 | 800 | |
c44a56d8 MA |
801 | migrate_set_capability(from, "postcopy-ram", true); |
802 | migrate_set_capability(to, "postcopy-ram", true); | |
803 | migrate_set_capability(to, "postcopy-blocktime", true); | |
ea0c6d62 DDAG |
804 | |
805 | /* We want to pick a speed slow enough that the test completes | |
806 | * quickly, but that it doesn't complete precopy even on a slow | |
807 | * machine, so also set the downtime. | |
808 | */ | |
513aa2c6 | 809 | migrate_set_parameter_int(from, "max-bandwidth", 30000000); |
8f7798f1 | 810 | migrate_set_parameter_int(from, "downtime-limit", 1); |
ea0c6d62 DDAG |
811 | |
812 | /* Wait for the first serial output from the source */ | |
813 | wait_for_serial("src_serial"); | |
814 | ||
b5bbd3f3 | 815 | migrate(from, uri, "{}"); |
d131662a | 816 | g_free(uri); |
ea0c6d62 | 817 | |
863e27a8 | 818 | wait_for_migration_pass(from); |
ea0c6d62 | 819 | |
d131662a PX |
820 | *from_ptr = from; |
821 | *to_ptr = to; | |
ea0c6d62 | 822 | |
d131662a PX |
823 | return 0; |
824 | } | |
ea0c6d62 | 825 | |
d131662a PX |
826 | static void migrate_postcopy_complete(QTestState *from, QTestState *to) |
827 | { | |
828 | wait_for_migration_complete(from); | |
ea0c6d62 | 829 | |
d131662a | 830 | /* Make sure we get at least one "B" on destination */ |
ea0c6d62 | 831 | wait_for_serial("dest_serial"); |
ea0c6d62 | 832 | |
346f3dab AP |
833 | if (uffd_feature_thread_id) { |
834 | read_blocktime(to); | |
835 | } | |
ea0c6d62 | 836 | |
2c9bb297 DDAG |
837 | test_migrate_end(from, to, true); |
838 | } | |
839 | ||
d131662a PX |
840 | static void test_postcopy(void) |
841 | { | |
842 | QTestState *from, *to; | |
843 | ||
3e81f73c | 844 | if (migrate_postcopy_prepare(&from, &to, false)) { |
d131662a PX |
845 | return; |
846 | } | |
847 | migrate_postcopy_start(from, to); | |
848 | migrate_postcopy_complete(from, to); | |
849 | } | |
850 | ||
d5f49640 PX |
851 | static void test_postcopy_recovery(void) |
852 | { | |
853 | QTestState *from, *to; | |
854 | char *uri; | |
855 | ||
3e81f73c | 856 | if (migrate_postcopy_prepare(&from, &to, true)) { |
d5f49640 PX |
857 | return; |
858 | } | |
859 | ||
860 | /* Turn postcopy speed down, 4K/s is slow enough on any machines */ | |
8f7798f1 | 861 | migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096); |
d5f49640 PX |
862 | |
863 | /* Now we start the postcopy */ | |
864 | migrate_postcopy_start(from, to); | |
865 | ||
866 | /* | |
867 | * Wait until postcopy is really started; we can only run the | |
868 | * migrate-pause command during a postcopy | |
869 | */ | |
8c51642b | 870 | wait_for_migration_status(from, "postcopy-active", NULL); |
d5f49640 PX |
871 | |
872 | /* | |
873 | * Manually stop the postcopy migration. This emulates a network | |
874 | * failure with the migration socket | |
875 | */ | |
876 | migrate_pause(from); | |
877 | ||
878 | /* | |
879 | * Wait for destination side to reach postcopy-paused state. The | |
880 | * migrate-recover command can only succeed if destination machine | |
881 | * is in the paused state | |
882 | */ | |
e15310ea DDAG |
883 | wait_for_migration_status(to, "postcopy-paused", |
884 | (const char * []) { "failed", "active", | |
885 | "completed", NULL }); | |
d5f49640 PX |
886 | |
887 | /* | |
888 | * Create a new socket to emulate a new channel that is different | |
889 | * from the broken migration channel; tell the destination to | |
890 | * listen to the new port | |
891 | */ | |
892 | uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); | |
893 | migrate_recover(to, uri); | |
894 | ||
895 | /* | |
896 | * Try to rebuild the migration channel using the resume flag and | |
897 | * the newly created channel | |
898 | */ | |
e15310ea DDAG |
899 | wait_for_migration_status(from, "postcopy-paused", |
900 | (const char * []) { "failed", "active", | |
901 | "completed", NULL }); | |
b5bbd3f3 | 902 | migrate(from, uri, "{'resume': true}"); |
d5f49640 PX |
903 | g_free(uri); |
904 | ||
905 | /* Restore the postcopy bandwidth to unlimited */ | |
8f7798f1 | 906 | migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0); |
d5f49640 PX |
907 | |
908 | migrate_postcopy_complete(from, to); | |
909 | } | |
910 | ||
3af31a34 | 911 | static void wait_for_migration_fail(QTestState *from, bool allow_active) |
2c9bb297 | 912 | { |
e1454165 | 913 | QDict *rsp_return; |
2f7074c6 | 914 | char *status; |
2c9bb297 DDAG |
915 | bool failed; |
916 | ||
2c9bb297 | 917 | do { |
2f7074c6 | 918 | status = migrate_query_status(from); |
84b2c7e5 DDAG |
919 | bool result = !strcmp(status, "setup") || !strcmp(status, "failed") || |
920 | (allow_active && !strcmp(status, "active")); | |
921 | if (!result) { | |
922 | fprintf(stderr, "%s: unexpected status status=%s allow_active=%d\n", | |
923 | __func__, status, allow_active); | |
924 | } | |
925 | g_assert(result); | |
2c9bb297 | 926 | failed = !strcmp(status, "failed"); |
2f7074c6 | 927 | g_free(status); |
2c9bb297 DDAG |
928 | } while (!failed); |
929 | ||
930 | /* Is the machine currently running? */ | |
e1454165 | 931 | rsp_return = wait_command(from, "{ 'execute': 'query-status' }"); |
2c9bb297 DDAG |
932 | g_assert(qdict_haskey(rsp_return, "running")); |
933 | g_assert(qdict_get_bool(rsp_return, "running")); | |
e1454165 | 934 | qobject_unref(rsp_return); |
3af31a34 YK |
935 | } |
936 | ||
937 | static void test_baddest(void) | |
938 | { | |
939 | QTestState *from, *to; | |
2c9bb297 | 940 | |
3af31a34 YK |
941 | if (test_migrate_start(&from, &to, "tcp:0:0", true, false, NULL, NULL)) { |
942 | return; | |
943 | } | |
944 | migrate(from, "tcp:0:0", "{}"); | |
945 | wait_for_migration_fail(from, false); | |
2c9bb297 | 946 | test_migrate_end(from, to, false); |
ea0c6d62 DDAG |
947 | } |
948 | ||
2884100c JQ |
949 | static void test_precopy_unix(void) |
950 | { | |
951 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
952 | QTestState *from, *to; | |
953 | ||
3af31a34 | 954 | if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) { |
5fd4a9c9 DDAG |
955 | return; |
956 | } | |
2884100c JQ |
957 | |
958 | /* We want to pick a speed slow enough that the test completes | |
959 | * quickly, but that it doesn't complete precopy even on a slow | |
960 | * machine, so also set the downtime. | |
961 | */ | |
962 | /* 1 ms should make it not converge*/ | |
8f7798f1 | 963 | migrate_set_parameter_int(from, "downtime-limit", 1); |
2884100c | 964 | /* 1GB/s */ |
8f7798f1 | 965 | migrate_set_parameter_int(from, "max-bandwidth", 1000000000); |
2884100c JQ |
966 | |
967 | /* Wait for the first serial output from the source */ | |
968 | wait_for_serial("src_serial"); | |
969 | ||
b5bbd3f3 | 970 | migrate(from, uri, "{}"); |
2884100c JQ |
971 | |
972 | wait_for_migration_pass(from); | |
973 | ||
974 | /* 300 ms should converge */ | |
8f7798f1 | 975 | migrate_set_parameter_int(from, "downtime-limit", 300); |
2884100c JQ |
976 | |
977 | if (!got_stop) { | |
978 | qtest_qmp_eventwait(from, "STOP"); | |
979 | } | |
980 | ||
981 | qtest_qmp_eventwait(to, "RESUME"); | |
982 | ||
983 | wait_for_serial("dest_serial"); | |
984 | wait_for_migration_complete(from); | |
985 | ||
986 | test_migrate_end(from, to, true); | |
987 | g_free(uri); | |
988 | } | |
989 | ||
660a9b68 YK |
990 | #if 0 |
991 | /* Currently upset on aarch64 TCG */ | |
992 | static void test_ignore_shared(void) | |
993 | { | |
994 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
995 | QTestState *from, *to; | |
996 | ||
3af31a34 | 997 | if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) { |
660a9b68 YK |
998 | return; |
999 | } | |
1000 | ||
1001 | migrate_set_capability(from, "x-ignore-shared", true); | |
1002 | migrate_set_capability(to, "x-ignore-shared", true); | |
1003 | ||
1004 | /* Wait for the first serial output from the source */ | |
1005 | wait_for_serial("src_serial"); | |
1006 | ||
1007 | migrate(from, uri, "{}"); | |
1008 | ||
1009 | wait_for_migration_pass(from); | |
1010 | ||
1011 | if (!got_stop) { | |
1012 | qtest_qmp_eventwait(from, "STOP"); | |
1013 | } | |
1014 | ||
1015 | qtest_qmp_eventwait(to, "RESUME"); | |
1016 | ||
1017 | wait_for_serial("dest_serial"); | |
1018 | wait_for_migration_complete(from); | |
1019 | ||
1020 | /* Check whether shared RAM has been really skipped */ | |
1021 | g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024); | |
1022 | ||
1023 | test_migrate_end(from, to, true); | |
1024 | g_free(uri); | |
1025 | } | |
1026 | #endif | |
1027 | ||
cdf84229 JQ |
1028 | static void test_xbzrle(const char *uri) |
1029 | { | |
1030 | QTestState *from, *to; | |
1031 | ||
3af31a34 | 1032 | if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) { |
cdf84229 JQ |
1033 | return; |
1034 | } | |
1035 | ||
1036 | /* | |
1037 | * We want to pick a speed slow enough that the test completes | |
1038 | * quickly, but that it doesn't complete precopy even on a slow | |
1039 | * machine, so also set the downtime. | |
1040 | */ | |
1041 | /* 1 ms should make it not converge*/ | |
8f7798f1 | 1042 | migrate_set_parameter_int(from, "downtime-limit", 1); |
cdf84229 | 1043 | /* 1GB/s */ |
8f7798f1 | 1044 | migrate_set_parameter_int(from, "max-bandwidth", 1000000000); |
cdf84229 | 1045 | |
8f7798f1 | 1046 | migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432); |
cdf84229 JQ |
1047 | |
1048 | migrate_set_capability(from, "xbzrle", "true"); | |
1049 | migrate_set_capability(to, "xbzrle", "true"); | |
1050 | /* Wait for the first serial output from the source */ | |
1051 | wait_for_serial("src_serial"); | |
1052 | ||
1053 | migrate(from, uri, "{}"); | |
1054 | ||
1055 | wait_for_migration_pass(from); | |
1056 | ||
1057 | /* 300ms should converge */ | |
8f7798f1 | 1058 | migrate_set_parameter_int(from, "downtime-limit", 300); |
cdf84229 JQ |
1059 | |
1060 | if (!got_stop) { | |
1061 | qtest_qmp_eventwait(from, "STOP"); | |
1062 | } | |
1063 | qtest_qmp_eventwait(to, "RESUME"); | |
1064 | ||
1065 | wait_for_serial("dest_serial"); | |
1066 | wait_for_migration_complete(from); | |
1067 | ||
1068 | test_migrate_end(from, to, true); | |
1069 | } | |
1070 | ||
1071 | static void test_xbzrle_unix(void) | |
1072 | { | |
1073 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
1074 | ||
1075 | test_xbzrle(uri); | |
1076 | g_free(uri); | |
1077 | } | |
1078 | ||
609d3844 JQ |
1079 | static void test_precopy_tcp(void) |
1080 | { | |
1081 | char *uri; | |
1082 | QTestState *from, *to; | |
1083 | ||
3af31a34 YK |
1084 | if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false, |
1085 | NULL, NULL)) { | |
609d3844 JQ |
1086 | return; |
1087 | } | |
1088 | ||
1089 | /* | |
1090 | * We want to pick a speed slow enough that the test completes | |
1091 | * quickly, but that it doesn't complete precopy even on a slow | |
1092 | * machine, so also set the downtime. | |
1093 | */ | |
1094 | /* 1 ms should make it not converge*/ | |
8f7798f1 | 1095 | migrate_set_parameter_int(from, "downtime-limit", 1); |
609d3844 | 1096 | /* 1GB/s */ |
8f7798f1 | 1097 | migrate_set_parameter_int(from, "max-bandwidth", 1000000000); |
609d3844 JQ |
1098 | |
1099 | /* Wait for the first serial output from the source */ | |
1100 | wait_for_serial("src_serial"); | |
1101 | ||
1102 | uri = migrate_get_socket_address(to, "socket-address"); | |
1103 | ||
1104 | migrate(from, uri, "{}"); | |
1105 | ||
1106 | wait_for_migration_pass(from); | |
1107 | ||
1108 | /* 300ms should converge */ | |
8f7798f1 | 1109 | migrate_set_parameter_int(from, "downtime-limit", 300); |
609d3844 JQ |
1110 | |
1111 | if (!got_stop) { | |
1112 | qtest_qmp_eventwait(from, "STOP"); | |
1113 | } | |
1114 | qtest_qmp_eventwait(to, "RESUME"); | |
1115 | ||
1116 | wait_for_serial("dest_serial"); | |
1117 | wait_for_migration_complete(from); | |
1118 | ||
1119 | test_migrate_end(from, to, true); | |
1120 | g_free(uri); | |
1121 | } | |
1122 | ||
24d5588c YK |
1123 | static void test_migrate_fd_proto(void) |
1124 | { | |
1125 | QTestState *from, *to; | |
1126 | int ret; | |
1127 | int pair[2]; | |
1128 | QDict *rsp; | |
1129 | const char *error_desc; | |
1130 | ||
3af31a34 | 1131 | if (test_migrate_start(&from, &to, "defer", false, false, NULL, NULL)) { |
24d5588c YK |
1132 | return; |
1133 | } | |
1134 | ||
1135 | /* | |
1136 | * We want to pick a speed slow enough that the test completes | |
1137 | * quickly, but that it doesn't complete precopy even on a slow | |
1138 | * machine, so also set the downtime. | |
1139 | */ | |
1140 | /* 1 ms should make it not converge */ | |
8f7798f1 | 1141 | migrate_set_parameter_int(from, "downtime-limit", 1); |
24d5588c | 1142 | /* 1GB/s */ |
8f7798f1 | 1143 | migrate_set_parameter_int(from, "max-bandwidth", 1000000000); |
24d5588c YK |
1144 | |
1145 | /* Wait for the first serial output from the source */ | |
1146 | wait_for_serial("src_serial"); | |
1147 | ||
1148 | /* Create two connected sockets for migration */ | |
1149 | ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair); | |
1150 | g_assert_cmpint(ret, ==, 0); | |
1151 | ||
1152 | /* Send the 1st socket to the target */ | |
1153 | rsp = wait_command_fd(to, pair[0], | |
1154 | "{ 'execute': 'getfd'," | |
1155 | " 'arguments': { 'fdname': 'fd-mig' }}"); | |
1156 | qobject_unref(rsp); | |
1157 | close(pair[0]); | |
1158 | ||
1159 | /* Start incoming migration from the 1st socket */ | |
1160 | rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," | |
1161 | " 'arguments': { 'uri': 'fd:fd-mig' }}"); | |
1162 | qobject_unref(rsp); | |
1163 | ||
1164 | /* Send the 2nd socket to the target */ | |
1165 | rsp = wait_command_fd(from, pair[1], | |
1166 | "{ 'execute': 'getfd'," | |
1167 | " 'arguments': { 'fdname': 'fd-mig' }}"); | |
1168 | qobject_unref(rsp); | |
1169 | close(pair[1]); | |
1170 | ||
1171 | /* Start migration to the 2nd socket*/ | |
1172 | migrate(from, "fd:fd-mig", "{}"); | |
1173 | ||
1174 | wait_for_migration_pass(from); | |
1175 | ||
1176 | /* 300ms should converge */ | |
8f7798f1 | 1177 | migrate_set_parameter_int(from, "downtime-limit", 300); |
24d5588c YK |
1178 | |
1179 | if (!got_stop) { | |
1180 | qtest_qmp_eventwait(from, "STOP"); | |
1181 | } | |
1182 | qtest_qmp_eventwait(to, "RESUME"); | |
1183 | ||
1184 | /* Test closing fds */ | |
1185 | /* We assume, that QEMU removes named fd from its list, | |
1186 | * so this should fail */ | |
1187 | rsp = qtest_qmp(from, "{ 'execute': 'closefd'," | |
1188 | " 'arguments': { 'fdname': 'fd-mig' }}"); | |
1189 | g_assert_true(qdict_haskey(rsp, "error")); | |
1190 | error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); | |
1191 | g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); | |
1192 | qobject_unref(rsp); | |
1193 | ||
1194 | rsp = qtest_qmp(to, "{ 'execute': 'closefd'," | |
1195 | " 'arguments': { 'fdname': 'fd-mig' }}"); | |
1196 | g_assert_true(qdict_haskey(rsp, "error")); | |
1197 | error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); | |
1198 | g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); | |
1199 | qobject_unref(rsp); | |
1200 | ||
1201 | /* Complete migration */ | |
1202 | wait_for_serial("dest_serial"); | |
1203 | wait_for_migration_complete(from); | |
1204 | test_migrate_end(from, to, true); | |
1205 | } | |
1206 | ||
3af31a34 YK |
1207 | static void do_test_validate_uuid(const char *uuid_arg_src, |
1208 | const char *uuid_arg_dst, | |
1209 | bool should_fail, bool hide_stderr) | |
1210 | { | |
1211 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
1212 | QTestState *from, *to; | |
1213 | ||
1214 | if (test_migrate_start(&from, &to, uri, hide_stderr, false, | |
1215 | uuid_arg_src, uuid_arg_dst)) { | |
1216 | return; | |
1217 | } | |
1218 | ||
1219 | /* | |
1220 | * UUID validation is at the begin of migration. So, the main process of | |
1221 | * migration is not interesting for us here. Thus, set huge downtime for | |
1222 | * very fast migration. | |
1223 | */ | |
1224 | migrate_set_parameter_int(from, "downtime-limit", 1000000); | |
1225 | migrate_set_capability(from, "validate-uuid", true); | |
1226 | ||
1227 | /* Wait for the first serial output from the source */ | |
1228 | wait_for_serial("src_serial"); | |
1229 | ||
1230 | migrate(from, uri, "{}"); | |
1231 | ||
1232 | if (should_fail) { | |
1233 | qtest_set_expected_status(to, 1); | |
1234 | wait_for_migration_fail(from, true); | |
1235 | } else { | |
1236 | wait_for_migration_complete(from); | |
1237 | } | |
1238 | ||
1239 | test_migrate_end(from, to, false); | |
1240 | g_free(uri); | |
1241 | } | |
1242 | ||
1243 | static void test_validate_uuid(void) | |
1244 | { | |
1245 | do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", | |
1246 | "-uuid 11111111-1111-1111-1111-111111111111", | |
1247 | false, false); | |
1248 | } | |
1249 | ||
1250 | static void test_validate_uuid_error(void) | |
1251 | { | |
1252 | do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", | |
1253 | "-uuid 22222222-2222-2222-2222-222222222222", | |
1254 | true, true); | |
1255 | } | |
1256 | ||
1257 | static void test_validate_uuid_src_not_set(void) | |
1258 | { | |
1259 | do_test_validate_uuid(NULL, "-uuid 11111111-1111-1111-1111-111111111111", | |
1260 | false, true); | |
1261 | } | |
1262 | ||
1263 | static void test_validate_uuid_dst_not_set(void) | |
1264 | { | |
1265 | do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", NULL, | |
1266 | false, true); | |
1267 | } | |
1268 | ||
8c51642b YK |
1269 | static void test_migrate_auto_converge(void) |
1270 | { | |
1271 | char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); | |
1272 | QTestState *from, *to; | |
1273 | int64_t remaining, percentage; | |
1274 | ||
1275 | /* | |
1276 | * We want the test to be stable and as fast as possible. | |
1277 | * E.g., with 1Gb/s bandwith migration may pass without throttling, | |
1278 | * so we need to decrease a bandwidth. | |
1279 | */ | |
1280 | const int64_t init_pct = 5, inc_pct = 50, max_pct = 95; | |
1281 | const int64_t max_bandwidth = 400000000; /* ~400Mb/s */ | |
1282 | const int64_t downtime_limit = 250; /* 250ms */ | |
1283 | /* | |
1284 | * We migrate through unix-socket (> 500Mb/s). | |
1285 | * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s). | |
1286 | * So, we can predict expected_threshold | |
1287 | */ | |
1288 | const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000; | |
1289 | ||
1290 | if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) { | |
1291 | return; | |
1292 | } | |
1293 | ||
1294 | migrate_set_capability(from, "auto-converge", true); | |
1295 | migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct); | |
1296 | migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct); | |
1297 | migrate_set_parameter_int(from, "max-cpu-throttle", max_pct); | |
1298 | ||
1299 | /* | |
1300 | * Set the initial parameters so that the migration could not converge | |
1301 | * without throttling. | |
1302 | */ | |
1303 | migrate_set_parameter_int(from, "downtime-limit", 1); | |
1304 | migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */ | |
1305 | ||
1306 | /* To check remaining size after precopy */ | |
1307 | migrate_set_capability(from, "pause-before-switchover", true); | |
1308 | ||
1309 | /* Wait for the first serial output from the source */ | |
1310 | wait_for_serial("src_serial"); | |
1311 | ||
1312 | migrate(from, uri, "{}"); | |
1313 | ||
1314 | /* Wait for throttling begins */ | |
1315 | percentage = 0; | |
1316 | while (percentage == 0) { | |
1317 | percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); | |
1318 | usleep(100); | |
1319 | g_assert_false(got_stop); | |
1320 | } | |
1321 | /* The first percentage of throttling should be equal to init_pct */ | |
1322 | g_assert_cmpint(percentage, ==, init_pct); | |
1323 | /* Now, when we tested that throttling works, let it converge */ | |
1324 | migrate_set_parameter_int(from, "downtime-limit", downtime_limit); | |
1325 | migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth); | |
1326 | ||
1327 | /* | |
1328 | * Wait for pre-switchover status to check last throttle percentage | |
1329 | * and remaining. These values will be zeroed later | |
1330 | */ | |
1331 | wait_for_migration_status(from, "pre-switchover", NULL); | |
1332 | ||
1333 | /* The final percentage of throttling shouldn't be greater than max_pct */ | |
1334 | percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); | |
1335 | g_assert_cmpint(percentage, <=, max_pct); | |
1336 | ||
1337 | remaining = read_ram_property_int(from, "remaining"); | |
1338 | g_assert_cmpint(remaining, <, expected_threshold); | |
1339 | ||
1340 | migrate_continue(from, "pre-switchover"); | |
1341 | ||
1342 | qtest_qmp_eventwait(to, "RESUME"); | |
1343 | ||
1344 | wait_for_serial("dest_serial"); | |
1345 | wait_for_migration_complete(from); | |
1346 | ||
1347 | g_free(uri); | |
1348 | ||
1349 | test_migrate_end(from, to, true); | |
1350 | } | |
1351 | ||
ea0c6d62 DDAG |
1352 | int main(int argc, char **argv) |
1353 | { | |
2656bfd9 | 1354 | char template[] = "/tmp/migration-test-XXXXXX"; |
ea0c6d62 DDAG |
1355 | int ret; |
1356 | ||
1357 | g_test_init(&argc, &argv, NULL); | |
1358 | ||
1359 | if (!ufd_version_check()) { | |
4848cb3d | 1360 | return g_test_run(); |
ea0c6d62 DDAG |
1361 | } |
1362 | ||
880b169a TH |
1363 | /* |
1364 | * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG | |
1365 | * is touchy due to race conditions on dirty bits (especially on PPC for | |
1366 | * some reason) | |
1367 | */ | |
1368 | if (g_str_equal(qtest_get_arch(), "ppc64") && | |
1ee5e144 LV |
1369 | (access("/sys/module/kvm_hv", F_OK) || |
1370 | access("/dev/kvm", R_OK | W_OK))) { | |
880b169a | 1371 | g_test_message("Skipping test: kvm_hv not available"); |
4848cb3d | 1372 | return g_test_run(); |
880b169a TH |
1373 | } |
1374 | ||
d254b392 TH |
1375 | /* |
1376 | * Similar to ppc64, s390x seems to be touchy with TCG, so disable it | |
1377 | * there until the problems are resolved | |
1378 | */ | |
1379 | if (g_str_equal(qtest_get_arch(), "s390x")) { | |
1380 | #if defined(HOST_S390X) | |
1381 | if (access("/dev/kvm", R_OK | W_OK)) { | |
1382 | g_test_message("Skipping test: kvm not available"); | |
4848cb3d | 1383 | return g_test_run(); |
d254b392 TH |
1384 | } |
1385 | #else | |
1386 | g_test_message("Skipping test: Need s390x host to work properly"); | |
4848cb3d | 1387 | return g_test_run(); |
d254b392 TH |
1388 | #endif |
1389 | } | |
1390 | ||
ea0c6d62 DDAG |
1391 | tmpfs = mkdtemp(template); |
1392 | if (!tmpfs) { | |
13ee9e30 | 1393 | g_test_message("mkdtemp on path (%s): %s", template, strerror(errno)); |
ea0c6d62 DDAG |
1394 | } |
1395 | g_assert(tmpfs); | |
1396 | ||
1397 | module_call_init(MODULE_INIT_QOM); | |
1398 | ||
2884100c | 1399 | qtest_add_func("/migration/postcopy/unix", test_postcopy); |
d5f49640 | 1400 | qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery); |
4c27486d | 1401 | qtest_add_func("/migration/deprecated", test_deprecated); |
2c9bb297 | 1402 | qtest_add_func("/migration/bad_dest", test_baddest); |
2884100c | 1403 | qtest_add_func("/migration/precopy/unix", test_precopy_unix); |
609d3844 | 1404 | qtest_add_func("/migration/precopy/tcp", test_precopy_tcp); |
660a9b68 | 1405 | /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ |
cdf84229 | 1406 | qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix); |
24d5588c | 1407 | qtest_add_func("/migration/fd_proto", test_migrate_fd_proto); |
3af31a34 YK |
1408 | qtest_add_func("/migration/validate_uuid", test_validate_uuid); |
1409 | qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error); | |
1410 | qtest_add_func("/migration/validate_uuid_src_not_set", | |
1411 | test_validate_uuid_src_not_set); | |
1412 | qtest_add_func("/migration/validate_uuid_dst_not_set", | |
1413 | test_validate_uuid_dst_not_set); | |
ea0c6d62 | 1414 | |
8c51642b YK |
1415 | qtest_add_func("/migration/auto_converge", test_migrate_auto_converge); |
1416 | ||
ea0c6d62 DDAG |
1417 | ret = g_test_run(); |
1418 | ||
1419 | g_assert_cmpint(ret, ==, 0); | |
1420 | ||
1421 | ret = rmdir(tmpfs); | |
1422 | if (ret != 0) { | |
13ee9e30 | 1423 | g_test_message("unable to rmdir: path (%s): %s", |
ea0c6d62 DDAG |
1424 | tmpfs, strerror(errno)); |
1425 | } | |
1426 | ||
1427 | return ret; | |
1428 | } |