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