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