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