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