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