1 /* 2 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates 3 * based on the vhost-user-test.c that is: 4 * Copyright (c) 2014 Virtual Open Systems Sarl. 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 * 9 */ 10 11 #include "qemu/osdep.h" 12 13 #include "chardev/char.h" 14 #include "crypto/tlscredspsk.h" 15 #include "libqtest.h" 16 #include "migration/bootfile.h" 17 #include "migration/framework.h" 18 #include "migration/migration-qmp.h" 19 #include "migration/migration-util.h" 20 #include "ppc-util.h" 21 #include "qapi/error.h" 22 #include "qobject/qjson.h" 23 #include "qobject/qlist.h" 24 #include "qemu/module.h" 25 #include "qemu/option.h" 26 #include "qemu/range.h" 27 #include "qemu/sockets.h" 28 29 30 #define QEMU_VM_FILE_MAGIC 0x5145564d 31 #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC" 32 #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST" 33 #define MULTIFD_TEST_CHANNELS 4 34 35 unsigned start_address; 36 unsigned end_address; 37 static QTestMigrationState src_state; 38 static QTestMigrationState dst_state; 39 static char *tmpfs; 40 41 /* 42 * An initial 3 MB offset is used as that corresponds 43 * to ~1 sec of data transfer with our bandwidth setting. 44 */ 45 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024) 46 /* 47 * A further 1k is added to ensure we're not a multiple 48 * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes 49 * from the migration guest workload. 50 */ 51 #define MAGIC_OFFSET_SHUFFLE 1024 52 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE) 53 #define MAGIC_MARKER 0xFEED12345678CAFEULL 54 55 56 /* 57 * Wait for some output in the serial output file, 58 * we get an 'A' followed by an endless string of 'B's 59 * but on the destination we won't have the A (unless we enabled suspend/resume) 60 */ 61 void wait_for_serial(const char *side) 62 { 63 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side); 64 FILE *serialfile = fopen(serialpath, "r"); 65 66 do { 67 int readvalue = fgetc(serialfile); 68 69 switch (readvalue) { 70 case 'A': 71 /* Fine */ 72 break; 73 74 case 'B': 75 /* It's alive! */ 76 fclose(serialfile); 77 return; 78 79 case EOF: 80 fseek(serialfile, 0, SEEK_SET); 81 usleep(1000); 82 break; 83 84 default: 85 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side); 86 g_assert_not_reached(); 87 } 88 } while (true); 89 } 90 91 void migrate_prepare_for_dirty_mem(QTestState *from) 92 { 93 /* 94 * The guest workflow iterates from start_address to 95 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE 96 * bytes. 97 * 98 * IOW, if we write to mem at a point which is NOT 99 * a multiple of TEST_MEM_PAGE_SIZE, our write won't 100 * conflict with the migration workflow. 101 * 102 * We put in a marker here, that we'll use to determine 103 * when the data has been transferred to the dst. 104 */ 105 qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER); 106 } 107 108 void migrate_wait_for_dirty_mem(QTestState *from, QTestState *to) 109 { 110 uint64_t watch_address = start_address + MAGIC_OFFSET_BASE; 111 uint64_t marker_address = start_address + MAGIC_OFFSET; 112 uint8_t watch_byte; 113 114 /* 115 * Wait for the MAGIC_MARKER to get transferred, as an 116 * indicator that a migration pass has made some known 117 * amount of progress. 118 */ 119 do { 120 usleep(1000 * 10); 121 } while (qtest_readq(to, marker_address) != MAGIC_MARKER); 122 123 124 /* If suspended, src only iterates once, and watch_byte may never change */ 125 if (src_state.suspend_me) { 126 return; 127 } 128 129 /* 130 * Now ensure that already transferred bytes are 131 * dirty again from the guest workload. Note the 132 * guest byte value will wrap around and by chance 133 * match the original watch_byte. This is harmless 134 * as we'll eventually see a different value if we 135 * keep watching 136 */ 137 watch_byte = qtest_readb(from, watch_address); 138 do { 139 usleep(1000 * 10); 140 } while (qtest_readb(from, watch_address) == watch_byte); 141 } 142 143 static void check_guests_ram(QTestState *who) 144 { 145 /* 146 * Our ASM test will have been incrementing one byte from each page from 147 * start_address to < end_address in order. This gives us a constraint 148 * that any page's byte should be equal or less than the previous pages 149 * byte (mod 256); and they should all be equal except for one transition 150 * at the point where we meet the incrementer. (We're running this with 151 * the guest stopped). 152 */ 153 unsigned address; 154 uint8_t first_byte; 155 uint8_t last_byte; 156 bool hit_edge = false; 157 int bad = 0; 158 159 qtest_memread(who, start_address, &first_byte, 1); 160 last_byte = first_byte; 161 162 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address; 163 address += TEST_MEM_PAGE_SIZE) 164 { 165 uint8_t b; 166 qtest_memread(who, address, &b, 1); 167 if (b != last_byte) { 168 if (((b + 1) % 256) == last_byte && !hit_edge) { 169 /* 170 * This is OK, the guest stopped at the point of 171 * incrementing the previous page but didn't get 172 * to us yet. 173 */ 174 hit_edge = true; 175 last_byte = b; 176 } else { 177 bad++; 178 if (bad <= 10) { 179 fprintf(stderr, "Memory content inconsistency at %x" 180 " first_byte = %x last_byte = %x current = %x" 181 " hit_edge = %x\n", 182 address, first_byte, last_byte, b, hit_edge); 183 } 184 } 185 } 186 } 187 if (bad >= 10) { 188 fprintf(stderr, "and in another %d pages", bad - 10); 189 } 190 g_assert(bad == 0); 191 } 192 193 static void cleanup(const char *filename) 194 { 195 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename); 196 197 unlink(path); 198 } 199 200 static QList *migrate_start_get_qmp_capabilities(const MigrateStart *args) 201 { 202 QList *capabilities = NULL; 203 204 if (args->oob) { 205 capabilities = qlist_new(); 206 qlist_append_str(capabilities, "oob"); 207 } 208 return capabilities; 209 } 210 211 static void migrate_start_set_capabilities(QTestState *from, QTestState *to, 212 MigrateStart *args) 213 { 214 /* 215 * MigrationCapability_lookup and MIGRATION_CAPABILITY_ constants 216 * are from qapi-types-migration.h. 217 */ 218 for (uint8_t i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 219 if (!args->caps[i]) { 220 continue; 221 } 222 if (from) { 223 migrate_set_capability(from, 224 MigrationCapability_lookup.array[i], true); 225 } 226 if (to) { 227 migrate_set_capability(to, 228 MigrationCapability_lookup.array[i], true); 229 } 230 } 231 232 /* 233 * Always enable migration events. Libvirt always uses it, let's try 234 * to mimic as closer as that. 235 */ 236 migrate_set_capability(from, "events", true); 237 if (!args->defer_target_connect) { 238 migrate_set_capability(to, "events", true); 239 } 240 241 /* 242 * Default number of channels should be fine for most 243 * tests. Individual tests can override by calling 244 * migrate_set_parameter() directly. 245 */ 246 if (args->caps[MIGRATION_CAPABILITY_MULTIFD]) { 247 migrate_set_parameter_int(from, "multifd-channels", 248 MULTIFD_TEST_CHANNELS); 249 migrate_set_parameter_int(to, "multifd-channels", 250 MULTIFD_TEST_CHANNELS); 251 } 252 253 return; 254 } 255 256 int migrate_start(QTestState **from, QTestState **to, const char *uri, 257 MigrateStart *args) 258 { 259 /* options for source and target */ 260 g_autofree gchar *arch_opts = NULL; 261 g_autofree gchar *cmd_source = NULL; 262 g_autofree gchar *cmd_target = NULL; 263 const gchar *ignore_stderr; 264 g_autofree char *shmem_opts = NULL; 265 g_autofree char *shmem_path = NULL; 266 const char *kvm_opts = NULL; 267 const char *arch = qtest_get_arch(); 268 const char *memory_size; 269 const char *machine_alias, *machine_opts = ""; 270 g_autofree char *machine = NULL; 271 const char *bootpath; 272 g_autoptr(QList) capabilities = migrate_start_get_qmp_capabilities(args); 273 g_autofree char *memory_backend = NULL; 274 const char *events; 275 276 if (args->use_shmem) { 277 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) { 278 g_test_skip("/dev/shm is not supported"); 279 return -1; 280 } 281 } 282 283 dst_state = (QTestMigrationState) { }; 284 src_state = (QTestMigrationState) { }; 285 bootpath = bootfile_create(arch, tmpfs, args->suspend_me); 286 src_state.suspend_me = args->suspend_me; 287 288 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 289 memory_size = "150M"; 290 291 if (g_str_equal(arch, "i386")) { 292 machine_alias = "pc"; 293 } else { 294 machine_alias = "q35"; 295 } 296 arch_opts = g_strdup_printf( 297 "-drive if=none,id=d0,file=%s,format=raw " 298 "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath); 299 start_address = X86_TEST_MEM_START; 300 end_address = X86_TEST_MEM_END; 301 } else if (g_str_equal(arch, "s390x")) { 302 memory_size = "128M"; 303 machine_alias = "s390-ccw-virtio"; 304 arch_opts = g_strdup_printf("-bios %s", bootpath); 305 start_address = S390_TEST_MEM_START; 306 end_address = S390_TEST_MEM_END; 307 } else if (strcmp(arch, "ppc64") == 0) { 308 memory_size = "256M"; 309 start_address = PPC_TEST_MEM_START; 310 end_address = PPC_TEST_MEM_END; 311 machine_alias = "pseries"; 312 machine_opts = "vsmt=8"; 313 arch_opts = g_strdup_printf( 314 "-nodefaults -machine " PSERIES_DEFAULT_CAPABILITIES " " 315 "-bios %s", bootpath); 316 } else if (strcmp(arch, "aarch64") == 0) { 317 memory_size = "150M"; 318 machine_alias = "virt"; 319 machine_opts = "gic-version=3"; 320 arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath); 321 start_address = ARM_TEST_MEM_START; 322 end_address = ARM_TEST_MEM_END; 323 } else { 324 g_assert_not_reached(); 325 } 326 327 if (!getenv("QTEST_LOG") && args->hide_stderr) { 328 #ifndef _WIN32 329 ignore_stderr = "2>/dev/null"; 330 #else 331 /* 332 * On Windows the QEMU executable is created via CreateProcess() and 333 * IO redirection does not work, so don't bother adding IO redirection 334 * to the command line. 335 */ 336 ignore_stderr = ""; 337 #endif 338 } else { 339 ignore_stderr = ""; 340 } 341 342 if (args->use_shmem) { 343 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid()); 344 shmem_opts = g_strdup_printf( 345 "-object memory-backend-file,id=mem0,size=%s" 346 ",mem-path=%s,share=on -numa node,memdev=mem0", 347 memory_size, shmem_path); 348 } 349 350 if (args->memory_backend) { 351 memory_backend = g_strdup_printf(args->memory_backend, memory_size); 352 } else { 353 memory_backend = g_strdup_printf("-m %s ", memory_size); 354 } 355 356 if (args->use_dirty_ring) { 357 kvm_opts = ",dirty-ring-size=4096"; 358 } 359 360 if (!qtest_has_machine(machine_alias)) { 361 g_autofree char *msg = g_strdup_printf("machine %s not supported", machine_alias); 362 g_test_skip(msg); 363 return -1; 364 } 365 366 machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC, 367 QEMU_ENV_DST); 368 369 g_test_message("Using machine type: %s", machine); 370 371 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg " 372 "-machine %s,%s " 373 "-name source,debug-threads=on " 374 "%s " 375 "-serial file:%s/src_serial " 376 "%s %s %s %s", 377 kvm_opts ? kvm_opts : "", 378 machine, machine_opts, 379 memory_backend, tmpfs, 380 arch_opts ? arch_opts : "", 381 shmem_opts ? shmem_opts : "", 382 args->opts_source ? args->opts_source : "", 383 ignore_stderr); 384 if (!args->only_target) { 385 *from = qtest_init_with_env_and_capabilities(QEMU_ENV_SRC, cmd_source, 386 capabilities, true); 387 qtest_qmp_set_event_callback(*from, 388 migrate_watch_for_events, 389 &src_state); 390 } 391 392 /* 393 * If the monitor connection is deferred, enable events on the command line 394 * so none are missed. This is for testing only, do not set migration 395 * options like this in general. 396 */ 397 events = args->defer_target_connect ? "-global migration.x-events=on" : ""; 398 399 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg " 400 "-machine %s,%s " 401 "-name target,debug-threads=on " 402 "%s " 403 "-serial file:%s/dest_serial " 404 "-incoming %s " 405 "%s %s %s %s %s", 406 kvm_opts ? kvm_opts : "", 407 machine, machine_opts, 408 memory_backend, tmpfs, uri, 409 events, 410 arch_opts ? arch_opts : "", 411 shmem_opts ? shmem_opts : "", 412 args->opts_target ? args->opts_target : "", 413 ignore_stderr); 414 *to = qtest_init_with_env_and_capabilities(QEMU_ENV_DST, cmd_target, 415 capabilities, !args->defer_target_connect); 416 qtest_qmp_set_event_callback(*to, 417 migrate_watch_for_events, 418 &dst_state); 419 420 /* 421 * Remove shmem file immediately to avoid memory leak in test failed case. 422 * It's valid because QEMU has already opened this file 423 */ 424 if (args->use_shmem) { 425 unlink(shmem_path); 426 } 427 428 migrate_start_set_capabilities(*from, *to, args); 429 430 return 0; 431 } 432 433 void migrate_end(QTestState *from, QTestState *to, bool test_dest) 434 { 435 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; 436 437 qtest_quit(from); 438 439 if (test_dest) { 440 qtest_memread(to, start_address, &dest_byte_a, 1); 441 442 /* Destination still running, wait for a byte to change */ 443 do { 444 qtest_memread(to, start_address, &dest_byte_b, 1); 445 usleep(1000 * 10); 446 } while (dest_byte_a == dest_byte_b); 447 448 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}"); 449 450 /* With it stopped, check nothing changes */ 451 qtest_memread(to, start_address, &dest_byte_c, 1); 452 usleep(1000 * 200); 453 qtest_memread(to, start_address, &dest_byte_d, 1); 454 g_assert_cmpint(dest_byte_c, ==, dest_byte_d); 455 456 check_guests_ram(to); 457 } 458 459 qtest_quit(to); 460 461 cleanup("migsocket"); 462 cleanup("cpr.sock"); 463 cleanup("src_serial"); 464 cleanup("dest_serial"); 465 cleanup(FILE_TEST_FILENAME); 466 } 467 468 static int migrate_postcopy_prepare(QTestState **from_ptr, 469 QTestState **to_ptr, 470 MigrateCommon *args) 471 { 472 QTestState *from, *to; 473 474 /* set postcopy capabilities */ 475 args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME] = true; 476 args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] = true; 477 478 if (migrate_start(&from, &to, "defer", &args->start)) { 479 return -1; 480 } 481 482 if (args->start_hook) { 483 args->postcopy_data = args->start_hook(from, to); 484 } 485 486 migrate_ensure_non_converge(from); 487 migrate_prepare_for_dirty_mem(from); 488 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming'," 489 " 'arguments': { " 490 " 'channels': [ { 'channel-type': 'main'," 491 " 'addr': { 'transport': 'socket'," 492 " 'type': 'inet'," 493 " 'host': '127.0.0.1'," 494 " 'port': '0' } } ] } }"); 495 496 /* Wait for the first serial output from the source */ 497 wait_for_serial("src_serial"); 498 wait_for_suspend(from, &src_state); 499 500 migrate_qmp(from, to, NULL, NULL, "{}"); 501 502 migrate_wait_for_dirty_mem(from, to); 503 504 *from_ptr = from; 505 *to_ptr = to; 506 507 return 0; 508 } 509 510 static void migrate_postcopy_complete(QTestState *from, QTestState *to, 511 MigrateCommon *args) 512 { 513 MigrationTestEnv *env = migration_get_env(); 514 515 wait_for_migration_complete(from); 516 517 if (args->start.suspend_me) { 518 /* wakeup succeeds only if guest is suspended */ 519 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}"); 520 } 521 522 /* Make sure we get at least one "B" on destination */ 523 wait_for_serial("dest_serial"); 524 525 if (env->uffd_feature_thread_id) { 526 read_blocktime(to); 527 } 528 529 if (args->end_hook) { 530 args->end_hook(from, to, args->postcopy_data); 531 args->postcopy_data = NULL; 532 } 533 534 migrate_end(from, to, true); 535 } 536 537 void test_postcopy_common(MigrateCommon *args) 538 { 539 QTestState *from, *to; 540 541 if (migrate_postcopy_prepare(&from, &to, args)) { 542 return; 543 } 544 migrate_postcopy_start(from, to, &src_state); 545 migrate_postcopy_complete(from, to, args); 546 } 547 548 static void wait_for_postcopy_status(QTestState *one, const char *status) 549 { 550 wait_for_migration_status(one, status, 551 (const char * []) { 552 "failed", "active", 553 "completed", NULL 554 }); 555 } 556 557 static void postcopy_recover_fail(QTestState *from, QTestState *to, 558 PostcopyRecoveryFailStage stage) 559 { 560 #ifndef _WIN32 561 bool fail_early = (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH); 562 int ret, pair1[2], pair2[2]; 563 char c; 564 565 g_assert(stage > POSTCOPY_FAIL_NONE && stage < POSTCOPY_FAIL_MAX); 566 567 /* Create two unrelated socketpairs */ 568 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1); 569 g_assert_cmpint(ret, ==, 0); 570 571 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2); 572 g_assert_cmpint(ret, ==, 0); 573 574 /* 575 * Give the guests unpaired ends of the sockets, so they'll all blocked 576 * at reading. This mimics a wrong channel established. 577 */ 578 qtest_qmp_fds_assert_success(from, &pair1[0], 1, 579 "{ 'execute': 'getfd'," 580 " 'arguments': { 'fdname': 'fd-mig' }}"); 581 qtest_qmp_fds_assert_success(to, &pair2[0], 1, 582 "{ 'execute': 'getfd'," 583 " 'arguments': { 'fdname': 'fd-mig' }}"); 584 585 /* 586 * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to 587 * emulate the 1st byte of a real recovery, but stops from there to 588 * keep dest QEMU in RECOVER. This is needed so that we can kick off 589 * the recover process on dest QEMU (by triggering the G_IO_IN event). 590 * 591 * NOTE: this trick is not needed on src QEMUs, because src doesn't 592 * rely on an pre-existing G_IO_IN event, so it will always trigger the 593 * upcoming recovery anyway even if it can read nothing. 594 */ 595 #define QEMU_VM_COMMAND 0x08 596 c = QEMU_VM_COMMAND; 597 ret = send(pair2[1], &c, 1, 0); 598 g_assert_cmpint(ret, ==, 1); 599 600 if (stage == POSTCOPY_FAIL_CHANNEL_ESTABLISH) { 601 /* 602 * This will make src QEMU to fail at an early stage when trying to 603 * resume later, where it shouldn't reach RECOVER stage at all. 604 */ 605 close(pair1[1]); 606 } 607 608 migrate_recover(to, "fd:fd-mig"); 609 migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}"); 610 611 /* 612 * Source QEMU has an extra RECOVER_SETUP phase, dest doesn't have it. 613 * Make sure it appears along the way. 614 */ 615 migration_event_wait(from, "postcopy-recover-setup"); 616 617 if (fail_early) { 618 /* 619 * When fails at reconnection, src QEMU will automatically goes 620 * back to PAUSED state. Making sure there is an event in this 621 * case: Libvirt relies on this to detect early reconnection 622 * errors. 623 */ 624 migration_event_wait(from, "postcopy-paused"); 625 } else { 626 /* 627 * We want to test "fail later" at RECOVER stage here. Make sure 628 * both QEMU instances will go into RECOVER stage first, then test 629 * kicking them out using migrate-pause. 630 * 631 * Explicitly check the RECOVER event on src, that's what Libvirt 632 * relies on, rather than polling. 633 */ 634 migration_event_wait(from, "postcopy-recover"); 635 wait_for_postcopy_status(from, "postcopy-recover"); 636 637 /* Need an explicit kick on src QEMU in this case */ 638 migrate_pause(from); 639 } 640 641 /* 642 * For all failure cases, we'll reach such states on both sides now. 643 * Check them. 644 */ 645 wait_for_postcopy_status(from, "postcopy-paused"); 646 wait_for_postcopy_status(to, "postcopy-recover"); 647 648 /* 649 * Kick dest QEMU out too. This is normally not needed in reality 650 * because when the channel is shutdown it should also happen on src. 651 * However here we used separate socket pairs so we need to do that 652 * explicitly. 653 */ 654 migrate_pause(to); 655 wait_for_postcopy_status(to, "postcopy-paused"); 656 657 close(pair1[0]); 658 close(pair2[0]); 659 close(pair2[1]); 660 661 if (stage != POSTCOPY_FAIL_CHANNEL_ESTABLISH) { 662 close(pair1[1]); 663 } 664 #endif 665 } 666 667 void test_postcopy_recovery_common(MigrateCommon *args) 668 { 669 QTestState *from, *to; 670 g_autofree char *uri = NULL; 671 672 /* 673 * Always enable OOB QMP capability for recovery tests, migrate-recover is 674 * executed out-of-band 675 */ 676 args->start.oob = true; 677 678 /* Always hide errors for postcopy recover tests since they're expected */ 679 args->start.hide_stderr = true; 680 681 if (migrate_postcopy_prepare(&from, &to, args)) { 682 return; 683 } 684 685 /* Turn postcopy speed down, 4K/s is slow enough on any machines */ 686 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096); 687 688 /* Now we start the postcopy */ 689 migrate_postcopy_start(from, to, &src_state); 690 691 /* 692 * Wait until postcopy is really started; we can only run the 693 * migrate-pause command during a postcopy 694 */ 695 wait_for_migration_status(from, "postcopy-active", NULL); 696 697 /* 698 * Manually stop the postcopy migration. This emulates a network 699 * failure with the migration socket 700 */ 701 migrate_pause(from); 702 703 /* 704 * Wait for destination side to reach postcopy-paused state. The 705 * migrate-recover command can only succeed if destination machine 706 * is in the paused state 707 */ 708 wait_for_postcopy_status(to, "postcopy-paused"); 709 wait_for_postcopy_status(from, "postcopy-paused"); 710 711 if (args->postcopy_recovery_fail_stage) { 712 /* 713 * Test when a wrong socket specified for recover, and then the 714 * ability to kick it out, and continue with a correct socket. 715 */ 716 postcopy_recover_fail(from, to, args->postcopy_recovery_fail_stage); 717 /* continue with a good recovery */ 718 } 719 720 /* 721 * Create a new socket to emulate a new channel that is different 722 * from the broken migration channel; tell the destination to 723 * listen to the new port 724 */ 725 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); 726 migrate_recover(to, uri); 727 728 /* 729 * Try to rebuild the migration channel using the resume flag and 730 * the newly created channel 731 */ 732 migrate_qmp(from, to, uri, NULL, "{'resume': true}"); 733 734 /* Restore the postcopy bandwidth to unlimited */ 735 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0); 736 737 migrate_postcopy_complete(from, to, args); 738 } 739 740 void test_precopy_common(MigrateCommon *args) 741 { 742 QTestState *from, *to; 743 void *data_hook = NULL; 744 QObject *in_channels = NULL; 745 QObject *out_channels = NULL; 746 747 g_assert(!args->cpr_channel || args->connect_channels); 748 749 if (migrate_start(&from, &to, args->listen_uri, &args->start)) { 750 return; 751 } 752 753 if (args->start_hook) { 754 data_hook = args->start_hook(from, to); 755 } 756 757 /* Wait for the first serial output from the source */ 758 if (args->result == MIG_TEST_SUCCEED) { 759 wait_for_serial("src_serial"); 760 wait_for_suspend(from, &src_state); 761 } 762 763 if (args->live) { 764 migrate_ensure_non_converge(from); 765 migrate_prepare_for_dirty_mem(from); 766 } else { 767 /* 768 * Testing non-live migration, we allow it to run at 769 * full speed to ensure short test case duration. 770 * For tests expected to fail, we don't need to 771 * change anything. 772 */ 773 if (args->result == MIG_TEST_SUCCEED) { 774 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}"); 775 wait_for_stop(from, &src_state); 776 migrate_ensure_converge(from); 777 } 778 } 779 780 /* 781 * The cpr channel must be included in outgoing channels, but not in 782 * migrate-incoming channels. 783 */ 784 if (args->connect_channels) { 785 if (args->start.defer_target_connect && 786 !strcmp(args->listen_uri, "defer")) { 787 in_channels = qobject_from_json(args->connect_channels, 788 &error_abort); 789 } 790 out_channels = qobject_from_json(args->connect_channels, &error_abort); 791 792 if (args->cpr_channel) { 793 QList *channels_list = qobject_to(QList, out_channels); 794 QObject *obj = migrate_str_to_channel(args->cpr_channel); 795 796 qlist_append(channels_list, obj); 797 } 798 } 799 800 if (args->result == MIG_TEST_QMP_ERROR) { 801 migrate_qmp_fail(from, args->connect_uri, out_channels, "{}"); 802 goto finish; 803 } 804 805 migrate_qmp(from, to, args->connect_uri, out_channels, "{}"); 806 807 if (args->start.defer_target_connect) { 808 qtest_connect(to); 809 qtest_qmp_handshake(to, NULL); 810 if (!strcmp(args->listen_uri, "defer")) { 811 migrate_incoming_qmp(to, args->connect_uri, in_channels, "{}"); 812 } 813 } 814 815 if (args->result != MIG_TEST_SUCCEED) { 816 bool allow_active = args->result == MIG_TEST_FAIL; 817 wait_for_migration_fail(from, allow_active); 818 819 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) { 820 qtest_set_expected_status(to, EXIT_FAILURE); 821 } 822 } else { 823 if (args->live) { 824 /* 825 * For initial iteration(s) we must do a full pass, 826 * but for the final iteration, we need only wait 827 * for some dirty mem before switching to converge 828 */ 829 while (args->iterations > 1) { 830 wait_for_migration_pass(from, &src_state); 831 args->iterations--; 832 } 833 migrate_wait_for_dirty_mem(from, to); 834 835 migrate_ensure_converge(from); 836 837 /* 838 * We do this first, as it has a timeout to stop us 839 * hanging forever if migration didn't converge 840 */ 841 wait_for_migration_complete(from); 842 843 wait_for_stop(from, &src_state); 844 845 } else { 846 wait_for_migration_complete(from); 847 /* 848 * Must wait for dst to finish reading all incoming 849 * data on the socket before issuing 'cont' otherwise 850 * it'll be ignored 851 */ 852 wait_for_migration_complete(to); 853 854 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}"); 855 } 856 857 wait_for_resume(to, &dst_state); 858 859 if (args->start.suspend_me) { 860 /* wakeup succeeds only if guest is suspended */ 861 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}"); 862 } 863 864 wait_for_serial("dest_serial"); 865 } 866 867 finish: 868 if (args->end_hook) { 869 args->end_hook(from, to, data_hook); 870 } 871 872 migrate_end(from, to, args->result == MIG_TEST_SUCCEED); 873 } 874 875 static void file_dirty_offset_region(void) 876 { 877 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME); 878 size_t size = FILE_TEST_OFFSET; 879 g_autofree char *data = g_new0(char, size); 880 881 memset(data, FILE_TEST_MARKER, size); 882 g_assert(g_file_set_contents(path, data, size, NULL)); 883 } 884 885 static void file_check_offset_region(void) 886 { 887 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME); 888 size_t size = FILE_TEST_OFFSET; 889 g_autofree char *expected = g_new0(char, size); 890 g_autofree char *actual = NULL; 891 uint64_t *stream_start; 892 893 /* 894 * Ensure the skipped offset region's data has not been touched 895 * and the migration stream starts at the right place. 896 */ 897 898 memset(expected, FILE_TEST_MARKER, size); 899 900 g_assert(g_file_get_contents(path, &actual, NULL, NULL)); 901 g_assert(!memcmp(actual, expected, size)); 902 903 stream_start = (uint64_t *)(actual + size); 904 g_assert_cmpint(cpu_to_be64(*stream_start) >> 32, ==, QEMU_VM_FILE_MAGIC); 905 } 906 907 void test_file_common(MigrateCommon *args, bool stop_src) 908 { 909 QTestState *from, *to; 910 void *data_hook = NULL; 911 bool check_offset = false; 912 913 if (migrate_start(&from, &to, args->listen_uri, &args->start)) { 914 return; 915 } 916 917 /* 918 * File migration is never live. We can keep the source VM running 919 * during migration, but the destination will not be running 920 * concurrently. 921 */ 922 g_assert_false(args->live); 923 924 if (g_strrstr(args->connect_uri, "offset=")) { 925 check_offset = true; 926 /* 927 * This comes before the start_hook because it's equivalent to 928 * a management application creating the file and writing to 929 * it so hooks should expect the file to be already present. 930 */ 931 file_dirty_offset_region(); 932 } 933 934 if (args->start_hook) { 935 data_hook = args->start_hook(from, to); 936 } 937 938 migrate_ensure_converge(from); 939 wait_for_serial("src_serial"); 940 941 if (stop_src) { 942 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}"); 943 wait_for_stop(from, &src_state); 944 } 945 946 if (args->result == MIG_TEST_QMP_ERROR) { 947 migrate_qmp_fail(from, args->connect_uri, NULL, "{}"); 948 goto finish; 949 } 950 951 migrate_qmp(from, to, args->connect_uri, NULL, "{}"); 952 wait_for_migration_complete(from); 953 954 /* 955 * We need to wait for the source to finish before starting the 956 * destination. 957 */ 958 migrate_incoming_qmp(to, args->connect_uri, NULL, "{}"); 959 wait_for_migration_complete(to); 960 961 if (stop_src) { 962 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}"); 963 } 964 wait_for_resume(to, &dst_state); 965 966 wait_for_serial("dest_serial"); 967 968 if (check_offset) { 969 file_check_offset_region(); 970 } 971 972 finish: 973 if (args->end_hook) { 974 args->end_hook(from, to, data_hook); 975 } 976 977 migrate_end(from, to, args->result == MIG_TEST_SUCCEED); 978 } 979 980 void *migrate_hook_start_precopy_tcp_multifd_common(QTestState *from, 981 QTestState *to, 982 const char *method) 983 { 984 migrate_set_parameter_str(from, "multifd-compression", method); 985 migrate_set_parameter_str(to, "multifd-compression", method); 986 987 /* Start incoming migration from the 1st socket */ 988 migrate_incoming_qmp(to, "tcp:127.0.0.1:0", NULL, "{}"); 989 990 return NULL; 991 } 992 993 QTestMigrationState *get_src(void) 994 { 995 return &src_state; 996 } 997 998 MigrationTestEnv *migration_get_env(void) 999 { 1000 static MigrationTestEnv *env; 1001 g_autoptr(GError) err = NULL; 1002 1003 if (env) { 1004 return env; 1005 } 1006 1007 env = g_new0(MigrationTestEnv, 1); 1008 env->qemu_src = getenv(QEMU_ENV_SRC); 1009 env->qemu_dst = getenv(QEMU_ENV_DST); 1010 1011 /* 1012 * The default QTEST_QEMU_BINARY must always be provided because 1013 * that is what helpers use to query the accel type and 1014 * architecture. 1015 */ 1016 if (env->qemu_src && env->qemu_dst) { 1017 g_test_message("Only one of %s, %s is allowed", 1018 QEMU_ENV_SRC, QEMU_ENV_DST); 1019 exit(1); 1020 } 1021 1022 env->has_kvm = qtest_has_accel("kvm"); 1023 env->has_tcg = qtest_has_accel("tcg"); 1024 1025 if (!env->has_tcg && !env->has_kvm) { 1026 g_test_skip("No KVM or TCG accelerator available"); 1027 return env; 1028 } 1029 1030 env->has_dirty_ring = kvm_dirty_ring_supported(); 1031 env->has_uffd = ufd_version_check(&env->uffd_feature_thread_id); 1032 env->arch = qtest_get_arch(); 1033 env->is_x86 = !strcmp(env->arch, "i386") || !strcmp(env->arch, "x86_64"); 1034 1035 env->tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err); 1036 if (!env->tmpfs) { 1037 g_test_message("Can't create temporary directory in %s: %s", 1038 g_get_tmp_dir(), err->message); 1039 } 1040 g_assert(env->tmpfs); 1041 1042 tmpfs = env->tmpfs; 1043 1044 return env; 1045 } 1046 1047 int migration_env_clean(MigrationTestEnv *env) 1048 { 1049 char *tmpfs; 1050 int ret = 0; 1051 1052 if (!env) { 1053 return ret; 1054 } 1055 1056 bootfile_delete(); 1057 1058 tmpfs = env->tmpfs; 1059 ret = rmdir(tmpfs); 1060 if (ret != 0) { 1061 g_test_message("unable to rmdir: path (%s): %s", 1062 tmpfs, strerror(errno)); 1063 } 1064 g_free(tmpfs); 1065 1066 return ret; 1067 } 1068