1 /* 2 * QTest testcase for migration 3 * 4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates 5 * based on the vhost-user-test.c that is: 6 * Copyright (c) 2014 Virtual Open Systems Sarl. 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or later. 9 * See the COPYING file in the top-level directory. 10 * 11 */ 12 13 #include "qemu/osdep.h" 14 15 #include "libqtest.h" 16 #include "qapi/qmp/qdict.h" 17 #include "qemu/module.h" 18 #include "qemu/option.h" 19 #include "qemu/range.h" 20 #include "qemu/sockets.h" 21 #include "chardev/char.h" 22 #include "crypto/tlscredspsk.h" 23 #include "qapi/qmp/qlist.h" 24 #include "ppc-util.h" 25 26 #include "migration-helpers.h" 27 #include "tests/migration/migration-test.h" 28 #ifdef CONFIG_GNUTLS 29 # include "tests/unit/crypto-tls-psk-helpers.h" 30 # ifdef CONFIG_TASN1 31 # include "tests/unit/crypto-tls-x509-helpers.h" 32 # endif /* CONFIG_TASN1 */ 33 #endif /* CONFIG_GNUTLS */ 34 35 /* For dirty ring test; so far only x86_64 is supported */ 36 #if defined(__linux__) && defined(HOST_X86_64) 37 #include "linux/kvm.h" 38 #endif 39 40 unsigned start_address; 41 unsigned end_address; 42 static bool uffd_feature_thread_id; 43 static QTestMigrationState src_state; 44 static QTestMigrationState dst_state; 45 46 /* 47 * An initial 3 MB offset is used as that corresponds 48 * to ~1 sec of data transfer with our bandwidth setting. 49 */ 50 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024) 51 /* 52 * A further 1k is added to ensure we're not a multiple 53 * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes 54 * from the migration guest workload. 55 */ 56 #define MAGIC_OFFSET_SHUFFLE 1024 57 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE) 58 #define MAGIC_MARKER 0xFEED12345678CAFEULL 59 60 /* 61 * Dirtylimit stop working if dirty page rate error 62 * value less than DIRTYLIMIT_TOLERANCE_RANGE 63 */ 64 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */ 65 66 #define ANALYZE_SCRIPT "scripts/analyze-migration.py" 67 #define VMSTATE_CHECKER_SCRIPT "scripts/vmstate-static-checker.py" 68 69 #define QEMU_VM_FILE_MAGIC 0x5145564d 70 #define FILE_TEST_FILENAME "migfile" 71 #define FILE_TEST_OFFSET 0x1000 72 #define FILE_TEST_MARKER 'X' 73 #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC" 74 #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST" 75 76 #if defined(__linux__) 77 #include <sys/syscall.h> 78 #include <sys/vfs.h> 79 #endif 80 81 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD) 82 #include <sys/eventfd.h> 83 #include <sys/ioctl.h> 84 #include "qemu/userfaultfd.h" 85 86 static bool ufd_version_check(void) 87 { 88 struct uffdio_api api_struct; 89 uint64_t ioctl_mask; 90 91 int ufd = uffd_open(O_CLOEXEC); 92 93 if (ufd == -1) { 94 g_test_message("Skipping test: userfaultfd not available"); 95 return false; 96 } 97 98 api_struct.api = UFFD_API; 99 api_struct.features = 0; 100 if (ioctl(ufd, UFFDIO_API, &api_struct)) { 101 g_test_message("Skipping test: UFFDIO_API failed"); 102 return false; 103 } 104 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID; 105 106 ioctl_mask = 1ULL << _UFFDIO_REGISTER | 107 1ULL << _UFFDIO_UNREGISTER; 108 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) { 109 g_test_message("Skipping test: Missing userfault feature"); 110 return false; 111 } 112 113 return true; 114 } 115 116 #else 117 static bool ufd_version_check(void) 118 { 119 g_test_message("Skipping test: Userfault not available (builtdtime)"); 120 return false; 121 } 122 123 #endif 124 125 static char *tmpfs; 126 static char *bootpath; 127 128 /* The boot file modifies memory area in [start_address, end_address) 129 * repeatedly. It outputs a 'B' at a fixed rate while it's still running. 130 */ 131 #include "tests/migration/i386/a-b-bootblock.h" 132 #include "tests/migration/aarch64/a-b-kernel.h" 133 #include "tests/migration/ppc64/a-b-kernel.h" 134 #include "tests/migration/s390x/a-b-bios.h" 135 136 static void bootfile_create(char *dir, bool suspend_me) 137 { 138 const char *arch = qtest_get_arch(); 139 unsigned char *content; 140 size_t len; 141 142 bootpath = g_strdup_printf("%s/bootsect", dir); 143 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 144 /* the assembled x86 boot sector should be exactly one sector large */ 145 g_assert(sizeof(x86_bootsect) == 512); 146 x86_bootsect[SYM_suspend_me - SYM_start] = suspend_me; 147 content = x86_bootsect; 148 len = sizeof(x86_bootsect); 149 } else if (g_str_equal(arch, "s390x")) { 150 content = s390x_elf; 151 len = sizeof(s390x_elf); 152 } else if (strcmp(arch, "ppc64") == 0) { 153 content = ppc64_kernel; 154 len = sizeof(ppc64_kernel); 155 } else if (strcmp(arch, "aarch64") == 0) { 156 content = aarch64_kernel; 157 len = sizeof(aarch64_kernel); 158 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE); 159 } else { 160 g_assert_not_reached(); 161 } 162 163 FILE *bootfile = fopen(bootpath, "wb"); 164 165 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1); 166 fclose(bootfile); 167 } 168 169 static void bootfile_delete(void) 170 { 171 unlink(bootpath); 172 g_free(bootpath); 173 bootpath = NULL; 174 } 175 176 /* 177 * Wait for some output in the serial output file, 178 * we get an 'A' followed by an endless string of 'B's 179 * but on the destination we won't have the A (unless we enabled suspend/resume) 180 */ 181 static void wait_for_serial(const char *side) 182 { 183 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side); 184 FILE *serialfile = fopen(serialpath, "r"); 185 186 do { 187 int readvalue = fgetc(serialfile); 188 189 switch (readvalue) { 190 case 'A': 191 /* Fine */ 192 break; 193 194 case 'B': 195 /* It's alive! */ 196 fclose(serialfile); 197 return; 198 199 case EOF: 200 fseek(serialfile, 0, SEEK_SET); 201 usleep(1000); 202 break; 203 204 default: 205 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side); 206 g_assert_not_reached(); 207 } 208 } while (true); 209 } 210 211 static void wait_for_stop(QTestState *who, QTestMigrationState *state) 212 { 213 if (!state->stop_seen) { 214 qtest_qmp_eventwait(who, "STOP"); 215 } 216 } 217 218 static void wait_for_resume(QTestState *who, QTestMigrationState *state) 219 { 220 if (!state->resume_seen) { 221 qtest_qmp_eventwait(who, "RESUME"); 222 } 223 } 224 225 static void wait_for_suspend(QTestState *who, QTestMigrationState *state) 226 { 227 if (state->suspend_me && !state->suspend_seen) { 228 qtest_qmp_eventwait(who, "SUSPEND"); 229 } 230 } 231 232 /* 233 * It's tricky to use qemu's migration event capability with qtest, 234 * events suddenly appearing confuse the qmp()/hmp() responses. 235 */ 236 237 static int64_t read_ram_property_int(QTestState *who, const char *property) 238 { 239 QDict *rsp_return, *rsp_ram; 240 int64_t result; 241 242 rsp_return = migrate_query_not_failed(who); 243 if (!qdict_haskey(rsp_return, "ram")) { 244 /* Still in setup */ 245 result = 0; 246 } else { 247 rsp_ram = qdict_get_qdict(rsp_return, "ram"); 248 result = qdict_get_try_int(rsp_ram, property, 0); 249 } 250 qobject_unref(rsp_return); 251 return result; 252 } 253 254 static int64_t read_migrate_property_int(QTestState *who, const char *property) 255 { 256 QDict *rsp_return; 257 int64_t result; 258 259 rsp_return = migrate_query_not_failed(who); 260 result = qdict_get_try_int(rsp_return, property, 0); 261 qobject_unref(rsp_return); 262 return result; 263 } 264 265 static uint64_t get_migration_pass(QTestState *who) 266 { 267 return read_ram_property_int(who, "dirty-sync-count"); 268 } 269 270 static void read_blocktime(QTestState *who) 271 { 272 QDict *rsp_return; 273 274 rsp_return = migrate_query_not_failed(who); 275 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime")); 276 qobject_unref(rsp_return); 277 } 278 279 /* 280 * Wait for two changes in the migration pass count, but bail if we stop. 281 */ 282 static void wait_for_migration_pass(QTestState *who) 283 { 284 uint64_t pass, prev_pass = 0, changes = 0; 285 286 while (changes < 2 && !src_state.stop_seen && !src_state.suspend_seen) { 287 usleep(1000); 288 pass = get_migration_pass(who); 289 changes += (pass != prev_pass); 290 prev_pass = pass; 291 } 292 } 293 294 static void check_guests_ram(QTestState *who) 295 { 296 /* Our ASM test will have been incrementing one byte from each page from 297 * start_address to < end_address in order. This gives us a constraint 298 * that any page's byte should be equal or less than the previous pages 299 * byte (mod 256); and they should all be equal except for one transition 300 * at the point where we meet the incrementer. (We're running this with 301 * the guest stopped). 302 */ 303 unsigned address; 304 uint8_t first_byte; 305 uint8_t last_byte; 306 bool hit_edge = false; 307 int bad = 0; 308 309 qtest_memread(who, start_address, &first_byte, 1); 310 last_byte = first_byte; 311 312 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address; 313 address += TEST_MEM_PAGE_SIZE) 314 { 315 uint8_t b; 316 qtest_memread(who, address, &b, 1); 317 if (b != last_byte) { 318 if (((b + 1) % 256) == last_byte && !hit_edge) { 319 /* This is OK, the guest stopped at the point of 320 * incrementing the previous page but didn't get 321 * to us yet. 322 */ 323 hit_edge = true; 324 last_byte = b; 325 } else { 326 bad++; 327 if (bad <= 10) { 328 fprintf(stderr, "Memory content inconsistency at %x" 329 " first_byte = %x last_byte = %x current = %x" 330 " hit_edge = %x\n", 331 address, first_byte, last_byte, b, hit_edge); 332 } 333 } 334 } 335 } 336 if (bad >= 10) { 337 fprintf(stderr, "and in another %d pages", bad - 10); 338 } 339 g_assert(bad == 0); 340 } 341 342 static void cleanup(const char *filename) 343 { 344 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename); 345 346 unlink(path); 347 } 348 349 static long long migrate_get_parameter_int(QTestState *who, 350 const char *parameter) 351 { 352 QDict *rsp; 353 long long result; 354 355 rsp = qtest_qmp_assert_success_ref( 356 who, "{ 'execute': 'query-migrate-parameters' }"); 357 result = qdict_get_int(rsp, parameter); 358 qobject_unref(rsp); 359 return result; 360 } 361 362 static void migrate_check_parameter_int(QTestState *who, const char *parameter, 363 long long value) 364 { 365 long long result; 366 367 result = migrate_get_parameter_int(who, parameter); 368 g_assert_cmpint(result, ==, value); 369 } 370 371 static void migrate_set_parameter_int(QTestState *who, const char *parameter, 372 long long value) 373 { 374 qtest_qmp_assert_success(who, 375 "{ 'execute': 'migrate-set-parameters'," 376 "'arguments': { %s: %lld } }", 377 parameter, value); 378 migrate_check_parameter_int(who, parameter, value); 379 } 380 381 static char *migrate_get_parameter_str(QTestState *who, 382 const char *parameter) 383 { 384 QDict *rsp; 385 char *result; 386 387 rsp = qtest_qmp_assert_success_ref( 388 who, "{ 'execute': 'query-migrate-parameters' }"); 389 result = g_strdup(qdict_get_str(rsp, parameter)); 390 qobject_unref(rsp); 391 return result; 392 } 393 394 static void migrate_check_parameter_str(QTestState *who, const char *parameter, 395 const char *value) 396 { 397 g_autofree char *result = migrate_get_parameter_str(who, parameter); 398 g_assert_cmpstr(result, ==, value); 399 } 400 401 static void migrate_set_parameter_str(QTestState *who, const char *parameter, 402 const char *value) 403 { 404 qtest_qmp_assert_success(who, 405 "{ 'execute': 'migrate-set-parameters'," 406 "'arguments': { %s: %s } }", 407 parameter, value); 408 migrate_check_parameter_str(who, parameter, value); 409 } 410 411 static void migrate_ensure_non_converge(QTestState *who) 412 { 413 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */ 414 migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000); 415 migrate_set_parameter_int(who, "downtime-limit", 1); 416 } 417 418 static void migrate_ensure_converge(QTestState *who) 419 { 420 /* Should converge with 30s downtime + 1 gbs bandwidth limit */ 421 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000); 422 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000); 423 } 424 425 /* 426 * Our goal is to ensure that we run a single full migration 427 * iteration, and also dirty memory, ensuring that at least 428 * one further iteration is required. 429 * 430 * We can't directly synchronize with the start of a migration 431 * so we have to apply some tricks monitoring memory that is 432 * transferred. 433 * 434 * Initially we set the migration bandwidth to an insanely 435 * low value, with tiny max downtime too. This basically 436 * guarantees migration will never complete. 437 * 438 * This will result in a test that is unacceptably slow though, 439 * so we can't let the entire migration pass run at this speed. 440 * Our intent is to let it run just long enough that we can 441 * prove data prior to the marker has been transferred *AND* 442 * also prove this transferred data is dirty again. 443 * 444 * Before migration starts, we write a 64-bit magic marker 445 * into a fixed location in the src VM RAM. 446 * 447 * Then watch dst memory until the marker appears. This is 448 * proof that start_address -> MAGIC_OFFSET_BASE has been 449 * transferred. 450 * 451 * Finally we go back to the source and read a byte just 452 * before the marker until we see it flip in value. This 453 * is proof that start_address -> MAGIC_OFFSET_BASE 454 * is now dirty again. 455 * 456 * IOW, we're guaranteed at least a 2nd migration pass 457 * at this point. 458 * 459 * We can now let migration run at full speed to finish 460 * the test 461 */ 462 static void migrate_prepare_for_dirty_mem(QTestState *from) 463 { 464 /* 465 * The guest workflow iterates from start_address to 466 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE 467 * bytes. 468 * 469 * IOW, if we write to mem at a point which is NOT 470 * a multiple of TEST_MEM_PAGE_SIZE, our write won't 471 * conflict with the migration workflow. 472 * 473 * We put in a marker here, that we'll use to determine 474 * when the data has been transferred to the dst. 475 */ 476 qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER); 477 } 478 479 static void migrate_wait_for_dirty_mem(QTestState *from, 480 QTestState *to) 481 { 482 uint64_t watch_address = start_address + MAGIC_OFFSET_BASE; 483 uint64_t marker_address = start_address + MAGIC_OFFSET; 484 uint8_t watch_byte; 485 486 /* 487 * Wait for the MAGIC_MARKER to get transferred, as an 488 * indicator that a migration pass has made some known 489 * amount of progress. 490 */ 491 do { 492 usleep(1000 * 10); 493 } while (qtest_readq(to, marker_address) != MAGIC_MARKER); 494 495 496 /* If suspended, src only iterates once, and watch_byte may never change */ 497 if (src_state.suspend_me) { 498 return; 499 } 500 501 /* 502 * Now ensure that already transferred bytes are 503 * dirty again from the guest workload. Note the 504 * guest byte value will wrap around and by chance 505 * match the original watch_byte. This is harmless 506 * as we'll eventually see a different value if we 507 * keep watching 508 */ 509 watch_byte = qtest_readb(from, watch_address); 510 do { 511 usleep(1000 * 10); 512 } while (qtest_readb(from, watch_address) == watch_byte); 513 } 514 515 516 static void migrate_pause(QTestState *who) 517 { 518 qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }"); 519 } 520 521 static void migrate_continue(QTestState *who, const char *state) 522 { 523 qtest_qmp_assert_success(who, 524 "{ 'execute': 'migrate-continue'," 525 " 'arguments': { 'state': %s } }", 526 state); 527 } 528 529 static void migrate_recover(QTestState *who, const char *uri) 530 { 531 qtest_qmp_assert_success(who, 532 "{ 'execute': 'migrate-recover', " 533 " 'id': 'recover-cmd', " 534 " 'arguments': { 'uri': %s } }", 535 uri); 536 } 537 538 static void migrate_cancel(QTestState *who) 539 { 540 qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }"); 541 } 542 543 static void migrate_postcopy_start(QTestState *from, QTestState *to) 544 { 545 qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }"); 546 547 wait_for_stop(from, &src_state); 548 qtest_qmp_eventwait(to, "RESUME"); 549 } 550 551 typedef struct { 552 /* 553 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors 554 * unconditionally, because it means the user would like to be verbose. 555 */ 556 bool hide_stderr; 557 bool use_shmem; 558 /* only launch the target process */ 559 bool only_target; 560 /* Use dirty ring if true; dirty logging otherwise */ 561 bool use_dirty_ring; 562 const char *opts_source; 563 const char *opts_target; 564 /* suspend the src before migrating to dest. */ 565 bool suspend_me; 566 } MigrateStart; 567 568 /* 569 * A hook that runs after the src and dst QEMUs have been 570 * created, but before the migration is started. This can 571 * be used to set migration parameters and capabilities. 572 * 573 * Returns: NULL, or a pointer to opaque state to be 574 * later passed to the TestMigrateFinishHook 575 */ 576 typedef void * (*TestMigrateStartHook)(QTestState *from, 577 QTestState *to); 578 579 /* 580 * A hook that runs after the migration has finished, 581 * regardless of whether it succeeded or failed, but 582 * before QEMU has terminated (unless it self-terminated 583 * due to migration error) 584 * 585 * @opaque is a pointer to state previously returned 586 * by the TestMigrateStartHook if any, or NULL. 587 */ 588 typedef void (*TestMigrateFinishHook)(QTestState *from, 589 QTestState *to, 590 void *opaque); 591 592 typedef struct { 593 /* Optional: fine tune start parameters */ 594 MigrateStart start; 595 596 /* Required: the URI for the dst QEMU to listen on */ 597 const char *listen_uri; 598 599 /* 600 * Optional: the URI for the src QEMU to connect to 601 * If NULL, then it will query the dst QEMU for its actual 602 * listening address and use that as the connect address. 603 * This allows for dynamically picking a free TCP port. 604 */ 605 const char *connect_uri; 606 607 /* 608 * Optional: JSON-formatted list of src QEMU URIs. If a port is 609 * defined as '0' in any QDict key a value of '0' will be 610 * automatically converted to the correct destination port. 611 */ 612 const char *connect_channels; 613 614 /* Optional: callback to run at start to set migration parameters */ 615 TestMigrateStartHook start_hook; 616 /* Optional: callback to run at finish to cleanup */ 617 TestMigrateFinishHook finish_hook; 618 619 /* 620 * Optional: normally we expect the migration process to complete. 621 * 622 * There can be a variety of reasons and stages in which failure 623 * can happen during tests. 624 * 625 * If a failure is expected to happen at time of establishing 626 * the connection, then MIG_TEST_FAIL will indicate that the dst 627 * QEMU is expected to stay running and accept future migration 628 * connections. 629 * 630 * If a failure is expected to happen while processing the 631 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate 632 * that the dst QEMU is expected to quit with non-zero exit status 633 */ 634 enum { 635 /* This test should succeed, the default */ 636 MIG_TEST_SUCCEED = 0, 637 /* This test should fail, dest qemu should keep alive */ 638 MIG_TEST_FAIL, 639 /* This test should fail, dest qemu should fail with abnormal status */ 640 MIG_TEST_FAIL_DEST_QUIT_ERR, 641 /* The QMP command for this migration should fail with an error */ 642 MIG_TEST_QMP_ERROR, 643 } result; 644 645 /* 646 * Optional: set number of migration passes to wait for, if live==true. 647 * If zero, then merely wait for a few MB of dirty data 648 */ 649 unsigned int iterations; 650 651 /* 652 * Optional: whether the guest CPUs should be running during a precopy 653 * migration test. We used to always run with live but it took much 654 * longer so we reduced live tests to only the ones that have solid 655 * reason to be tested live-only. For each of the new test cases for 656 * precopy please provide justifications to use live explicitly (please 657 * refer to existing ones with live=true), or use live=off by default. 658 */ 659 bool live; 660 661 /* Postcopy specific fields */ 662 void *postcopy_data; 663 bool postcopy_preempt; 664 bool postcopy_recovery_test_fail; 665 } MigrateCommon; 666 667 static int test_migrate_start(QTestState **from, QTestState **to, 668 const char *uri, MigrateStart *args) 669 { 670 g_autofree gchar *arch_source = NULL; 671 g_autofree gchar *arch_target = NULL; 672 /* options for source and target */ 673 g_autofree gchar *arch_opts = NULL; 674 g_autofree gchar *cmd_source = NULL; 675 g_autofree gchar *cmd_target = NULL; 676 const gchar *ignore_stderr; 677 g_autofree char *shmem_opts = NULL; 678 g_autofree char *shmem_path = NULL; 679 const char *kvm_opts = NULL; 680 const char *arch = qtest_get_arch(); 681 const char *memory_size; 682 const char *machine_alias, *machine_opts = ""; 683 g_autofree char *machine = NULL; 684 685 if (args->use_shmem) { 686 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) { 687 g_test_skip("/dev/shm is not supported"); 688 return -1; 689 } 690 } 691 692 dst_state = (QTestMigrationState) { }; 693 src_state = (QTestMigrationState) { }; 694 bootfile_create(tmpfs, args->suspend_me); 695 src_state.suspend_me = args->suspend_me; 696 697 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 698 memory_size = "150M"; 699 700 if (g_str_equal(arch, "i386")) { 701 machine_alias = "pc"; 702 } else { 703 machine_alias = "q35"; 704 } 705 arch_opts = g_strdup_printf( 706 "-drive if=none,id=d0,file=%s,format=raw " 707 "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath); 708 start_address = X86_TEST_MEM_START; 709 end_address = X86_TEST_MEM_END; 710 } else if (g_str_equal(arch, "s390x")) { 711 memory_size = "128M"; 712 machine_alias = "s390-ccw-virtio"; 713 arch_opts = g_strdup_printf("-bios %s", bootpath); 714 start_address = S390_TEST_MEM_START; 715 end_address = S390_TEST_MEM_END; 716 } else if (strcmp(arch, "ppc64") == 0) { 717 memory_size = "256M"; 718 start_address = PPC_TEST_MEM_START; 719 end_address = PPC_TEST_MEM_END; 720 machine_alias = "pseries"; 721 machine_opts = "vsmt=8"; 722 arch_opts = g_strdup_printf( 723 "-nodefaults -machine " PSERIES_DEFAULT_CAPABILITIES " " 724 "-bios %s", bootpath); 725 } else if (strcmp(arch, "aarch64") == 0) { 726 memory_size = "150M"; 727 machine_alias = "virt"; 728 machine_opts = "gic-version=3"; 729 arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath); 730 start_address = ARM_TEST_MEM_START; 731 end_address = ARM_TEST_MEM_END; 732 } else { 733 g_assert_not_reached(); 734 } 735 736 if (!getenv("QTEST_LOG") && args->hide_stderr) { 737 #ifndef _WIN32 738 ignore_stderr = "2>/dev/null"; 739 #else 740 /* 741 * On Windows the QEMU executable is created via CreateProcess() and 742 * IO redirection does not work, so don't bother adding IO redirection 743 * to the command line. 744 */ 745 ignore_stderr = ""; 746 #endif 747 } else { 748 ignore_stderr = ""; 749 } 750 751 if (args->use_shmem) { 752 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid()); 753 shmem_opts = g_strdup_printf( 754 "-object memory-backend-file,id=mem0,size=%s" 755 ",mem-path=%s,share=on -numa node,memdev=mem0", 756 memory_size, shmem_path); 757 } 758 759 if (args->use_dirty_ring) { 760 kvm_opts = ",dirty-ring-size=4096"; 761 } 762 763 if (!qtest_has_machine(machine_alias)) { 764 g_autofree char *msg = g_strdup_printf("machine %s not supported", machine_alias); 765 g_test_skip(msg); 766 return -1; 767 } 768 769 machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC, 770 QEMU_ENV_DST); 771 772 g_test_message("Using machine type: %s", machine); 773 774 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg " 775 "-machine %s,%s " 776 "-name source,debug-threads=on " 777 "-m %s " 778 "-serial file:%s/src_serial " 779 "%s %s %s %s %s", 780 kvm_opts ? kvm_opts : "", 781 machine, machine_opts, 782 memory_size, tmpfs, 783 arch_opts ? arch_opts : "", 784 arch_source ? arch_source : "", 785 shmem_opts ? shmem_opts : "", 786 args->opts_source ? args->opts_source : "", 787 ignore_stderr); 788 if (!args->only_target) { 789 *from = qtest_init_with_env(QEMU_ENV_SRC, cmd_source); 790 qtest_qmp_set_event_callback(*from, 791 migrate_watch_for_events, 792 &src_state); 793 } 794 795 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg " 796 "-machine %s,%s " 797 "-name target,debug-threads=on " 798 "-m %s " 799 "-serial file:%s/dest_serial " 800 "-incoming %s " 801 "%s %s %s %s %s", 802 kvm_opts ? kvm_opts : "", 803 machine, machine_opts, 804 memory_size, tmpfs, uri, 805 arch_opts ? arch_opts : "", 806 arch_target ? arch_target : "", 807 shmem_opts ? shmem_opts : "", 808 args->opts_target ? args->opts_target : "", 809 ignore_stderr); 810 *to = qtest_init_with_env(QEMU_ENV_DST, cmd_target); 811 qtest_qmp_set_event_callback(*to, 812 migrate_watch_for_events, 813 &dst_state); 814 815 /* 816 * Remove shmem file immediately to avoid memory leak in test failed case. 817 * It's valid because QEMU has already opened this file 818 */ 819 if (args->use_shmem) { 820 unlink(shmem_path); 821 } 822 823 return 0; 824 } 825 826 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) 827 { 828 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; 829 830 qtest_quit(from); 831 832 if (test_dest) { 833 qtest_memread(to, start_address, &dest_byte_a, 1); 834 835 /* Destination still running, wait for a byte to change */ 836 do { 837 qtest_memread(to, start_address, &dest_byte_b, 1); 838 usleep(1000 * 10); 839 } while (dest_byte_a == dest_byte_b); 840 841 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}"); 842 843 /* With it stopped, check nothing changes */ 844 qtest_memread(to, start_address, &dest_byte_c, 1); 845 usleep(1000 * 200); 846 qtest_memread(to, start_address, &dest_byte_d, 1); 847 g_assert_cmpint(dest_byte_c, ==, dest_byte_d); 848 849 check_guests_ram(to); 850 } 851 852 qtest_quit(to); 853 854 cleanup("migsocket"); 855 cleanup("src_serial"); 856 cleanup("dest_serial"); 857 cleanup(FILE_TEST_FILENAME); 858 } 859 860 #ifdef CONFIG_GNUTLS 861 struct TestMigrateTLSPSKData { 862 char *workdir; 863 char *workdiralt; 864 char *pskfile; 865 char *pskfilealt; 866 }; 867 868 static void * 869 test_migrate_tls_psk_start_common(QTestState *from, 870 QTestState *to, 871 bool mismatch) 872 { 873 struct TestMigrateTLSPSKData *data = 874 g_new0(struct TestMigrateTLSPSKData, 1); 875 876 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs); 877 data->pskfile = g_strdup_printf("%s/%s", data->workdir, 878 QCRYPTO_TLS_CREDS_PSKFILE); 879 g_mkdir_with_parents(data->workdir, 0700); 880 test_tls_psk_init(data->pskfile); 881 882 if (mismatch) { 883 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs); 884 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt, 885 QCRYPTO_TLS_CREDS_PSKFILE); 886 g_mkdir_with_parents(data->workdiralt, 0700); 887 test_tls_psk_init_alt(data->pskfilealt); 888 } 889 890 qtest_qmp_assert_success(from, 891 "{ 'execute': 'object-add'," 892 " 'arguments': { 'qom-type': 'tls-creds-psk'," 893 " 'id': 'tlscredspsk0'," 894 " 'endpoint': 'client'," 895 " 'dir': %s," 896 " 'username': 'qemu'} }", 897 data->workdir); 898 899 qtest_qmp_assert_success(to, 900 "{ 'execute': 'object-add'," 901 " 'arguments': { 'qom-type': 'tls-creds-psk'," 902 " 'id': 'tlscredspsk0'," 903 " 'endpoint': 'server'," 904 " 'dir': %s } }", 905 mismatch ? data->workdiralt : data->workdir); 906 907 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0"); 908 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0"); 909 910 return data; 911 } 912 913 static void * 914 test_migrate_tls_psk_start_match(QTestState *from, 915 QTestState *to) 916 { 917 return test_migrate_tls_psk_start_common(from, to, false); 918 } 919 920 static void * 921 test_migrate_tls_psk_start_mismatch(QTestState *from, 922 QTestState *to) 923 { 924 return test_migrate_tls_psk_start_common(from, to, true); 925 } 926 927 static void 928 test_migrate_tls_psk_finish(QTestState *from, 929 QTestState *to, 930 void *opaque) 931 { 932 struct TestMigrateTLSPSKData *data = opaque; 933 934 test_tls_psk_cleanup(data->pskfile); 935 if (data->pskfilealt) { 936 test_tls_psk_cleanup(data->pskfilealt); 937 } 938 rmdir(data->workdir); 939 if (data->workdiralt) { 940 rmdir(data->workdiralt); 941 } 942 943 g_free(data->workdiralt); 944 g_free(data->pskfilealt); 945 g_free(data->workdir); 946 g_free(data->pskfile); 947 g_free(data); 948 } 949 950 #ifdef CONFIG_TASN1 951 typedef struct { 952 char *workdir; 953 char *keyfile; 954 char *cacert; 955 char *servercert; 956 char *serverkey; 957 char *clientcert; 958 char *clientkey; 959 } TestMigrateTLSX509Data; 960 961 typedef struct { 962 bool verifyclient; 963 bool clientcert; 964 bool hostileclient; 965 bool authzclient; 966 const char *certhostname; 967 const char *certipaddr; 968 } TestMigrateTLSX509; 969 970 static void * 971 test_migrate_tls_x509_start_common(QTestState *from, 972 QTestState *to, 973 TestMigrateTLSX509 *args) 974 { 975 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1); 976 977 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs); 978 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir); 979 980 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir); 981 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir); 982 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir); 983 if (args->clientcert) { 984 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir); 985 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir); 986 } 987 988 g_mkdir_with_parents(data->workdir, 0700); 989 990 test_tls_init(data->keyfile); 991 #ifndef _WIN32 992 g_assert(link(data->keyfile, data->serverkey) == 0); 993 #else 994 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0); 995 #endif 996 if (args->clientcert) { 997 #ifndef _WIN32 998 g_assert(link(data->keyfile, data->clientkey) == 0); 999 #else 1000 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0); 1001 #endif 1002 } 1003 1004 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert); 1005 if (args->clientcert) { 1006 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq, 1007 args->hostileclient ? 1008 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME : 1009 QCRYPTO_TLS_TEST_CLIENT_NAME, 1010 data->clientcert); 1011 } 1012 1013 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq, 1014 data->servercert, 1015 args->certhostname, 1016 args->certipaddr); 1017 1018 qtest_qmp_assert_success(from, 1019 "{ 'execute': 'object-add'," 1020 " 'arguments': { 'qom-type': 'tls-creds-x509'," 1021 " 'id': 'tlscredsx509client0'," 1022 " 'endpoint': 'client'," 1023 " 'dir': %s," 1024 " 'sanity-check': true," 1025 " 'verify-peer': true} }", 1026 data->workdir); 1027 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0"); 1028 if (args->certhostname) { 1029 migrate_set_parameter_str(from, "tls-hostname", args->certhostname); 1030 } 1031 1032 qtest_qmp_assert_success(to, 1033 "{ 'execute': 'object-add'," 1034 " 'arguments': { 'qom-type': 'tls-creds-x509'," 1035 " 'id': 'tlscredsx509server0'," 1036 " 'endpoint': 'server'," 1037 " 'dir': %s," 1038 " 'sanity-check': true," 1039 " 'verify-peer': %i} }", 1040 data->workdir, args->verifyclient); 1041 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0"); 1042 1043 if (args->authzclient) { 1044 qtest_qmp_assert_success(to, 1045 "{ 'execute': 'object-add'," 1046 " 'arguments': { 'qom-type': 'authz-simple'," 1047 " 'id': 'tlsauthz0'," 1048 " 'identity': %s} }", 1049 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME); 1050 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0"); 1051 } 1052 1053 return data; 1054 } 1055 1056 /* 1057 * The normal case: match server's cert hostname against 1058 * whatever host we were telling QEMU to connect to (if any) 1059 */ 1060 static void * 1061 test_migrate_tls_x509_start_default_host(QTestState *from, 1062 QTestState *to) 1063 { 1064 TestMigrateTLSX509 args = { 1065 .verifyclient = true, 1066 .clientcert = true, 1067 .certipaddr = "127.0.0.1" 1068 }; 1069 return test_migrate_tls_x509_start_common(from, to, &args); 1070 } 1071 1072 /* 1073 * The unusual case: the server's cert is different from 1074 * the address we're telling QEMU to connect to (if any), 1075 * so we must give QEMU an explicit hostname to validate 1076 */ 1077 static void * 1078 test_migrate_tls_x509_start_override_host(QTestState *from, 1079 QTestState *to) 1080 { 1081 TestMigrateTLSX509 args = { 1082 .verifyclient = true, 1083 .clientcert = true, 1084 .certhostname = "qemu.org", 1085 }; 1086 return test_migrate_tls_x509_start_common(from, to, &args); 1087 } 1088 1089 /* 1090 * The unusual case: the server's cert is different from 1091 * the address we're telling QEMU to connect to, and so we 1092 * expect the client to reject the server 1093 */ 1094 static void * 1095 test_migrate_tls_x509_start_mismatch_host(QTestState *from, 1096 QTestState *to) 1097 { 1098 TestMigrateTLSX509 args = { 1099 .verifyclient = true, 1100 .clientcert = true, 1101 .certipaddr = "10.0.0.1", 1102 }; 1103 return test_migrate_tls_x509_start_common(from, to, &args); 1104 } 1105 1106 static void * 1107 test_migrate_tls_x509_start_friendly_client(QTestState *from, 1108 QTestState *to) 1109 { 1110 TestMigrateTLSX509 args = { 1111 .verifyclient = true, 1112 .clientcert = true, 1113 .authzclient = true, 1114 .certipaddr = "127.0.0.1", 1115 }; 1116 return test_migrate_tls_x509_start_common(from, to, &args); 1117 } 1118 1119 static void * 1120 test_migrate_tls_x509_start_hostile_client(QTestState *from, 1121 QTestState *to) 1122 { 1123 TestMigrateTLSX509 args = { 1124 .verifyclient = true, 1125 .clientcert = true, 1126 .hostileclient = true, 1127 .authzclient = true, 1128 .certipaddr = "127.0.0.1", 1129 }; 1130 return test_migrate_tls_x509_start_common(from, to, &args); 1131 } 1132 1133 /* 1134 * The case with no client certificate presented, 1135 * and no server verification 1136 */ 1137 static void * 1138 test_migrate_tls_x509_start_allow_anon_client(QTestState *from, 1139 QTestState *to) 1140 { 1141 TestMigrateTLSX509 args = { 1142 .certipaddr = "127.0.0.1", 1143 }; 1144 return test_migrate_tls_x509_start_common(from, to, &args); 1145 } 1146 1147 /* 1148 * The case with no client certificate presented, 1149 * and server verification rejecting 1150 */ 1151 static void * 1152 test_migrate_tls_x509_start_reject_anon_client(QTestState *from, 1153 QTestState *to) 1154 { 1155 TestMigrateTLSX509 args = { 1156 .verifyclient = true, 1157 .certipaddr = "127.0.0.1", 1158 }; 1159 return test_migrate_tls_x509_start_common(from, to, &args); 1160 } 1161 1162 static void 1163 test_migrate_tls_x509_finish(QTestState *from, 1164 QTestState *to, 1165 void *opaque) 1166 { 1167 TestMigrateTLSX509Data *data = opaque; 1168 1169 test_tls_cleanup(data->keyfile); 1170 g_free(data->keyfile); 1171 1172 unlink(data->cacert); 1173 g_free(data->cacert); 1174 unlink(data->servercert); 1175 g_free(data->servercert); 1176 unlink(data->serverkey); 1177 g_free(data->serverkey); 1178 1179 if (data->clientcert) { 1180 unlink(data->clientcert); 1181 g_free(data->clientcert); 1182 } 1183 if (data->clientkey) { 1184 unlink(data->clientkey); 1185 g_free(data->clientkey); 1186 } 1187 1188 rmdir(data->workdir); 1189 g_free(data->workdir); 1190 1191 g_free(data); 1192 } 1193 #endif /* CONFIG_TASN1 */ 1194 #endif /* CONFIG_GNUTLS */ 1195 1196 static int migrate_postcopy_prepare(QTestState **from_ptr, 1197 QTestState **to_ptr, 1198 MigrateCommon *args) 1199 { 1200 QTestState *from, *to; 1201 1202 if (test_migrate_start(&from, &to, "defer", &args->start)) { 1203 return -1; 1204 } 1205 1206 if (args->start_hook) { 1207 args->postcopy_data = args->start_hook(from, to); 1208 } 1209 1210 migrate_set_capability(from, "postcopy-ram", true); 1211 migrate_set_capability(to, "postcopy-ram", true); 1212 migrate_set_capability(to, "postcopy-blocktime", true); 1213 1214 if (args->postcopy_preempt) { 1215 migrate_set_capability(from, "postcopy-preempt", true); 1216 migrate_set_capability(to, "postcopy-preempt", true); 1217 } 1218 1219 migrate_ensure_non_converge(from); 1220 1221 migrate_prepare_for_dirty_mem(from); 1222 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming'," 1223 " 'arguments': { " 1224 " 'channels': [ { 'channel-type': 'main'," 1225 " 'addr': { 'transport': 'socket'," 1226 " 'type': 'inet'," 1227 " 'host': '127.0.0.1'," 1228 " 'port': '0' } } ] } }"); 1229 1230 /* Wait for the first serial output from the source */ 1231 wait_for_serial("src_serial"); 1232 wait_for_suspend(from, &src_state); 1233 1234 migrate_qmp(from, to, NULL, NULL, "{}"); 1235 1236 migrate_wait_for_dirty_mem(from, to); 1237 1238 *from_ptr = from; 1239 *to_ptr = to; 1240 1241 return 0; 1242 } 1243 1244 static void migrate_postcopy_complete(QTestState *from, QTestState *to, 1245 MigrateCommon *args) 1246 { 1247 wait_for_migration_complete(from); 1248 1249 if (args->start.suspend_me) { 1250 /* wakeup succeeds only if guest is suspended */ 1251 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}"); 1252 } 1253 1254 /* Make sure we get at least one "B" on destination */ 1255 wait_for_serial("dest_serial"); 1256 1257 if (uffd_feature_thread_id) { 1258 read_blocktime(to); 1259 } 1260 1261 if (args->finish_hook) { 1262 args->finish_hook(from, to, args->postcopy_data); 1263 args->postcopy_data = NULL; 1264 } 1265 1266 test_migrate_end(from, to, true); 1267 } 1268 1269 static void test_postcopy_common(MigrateCommon *args) 1270 { 1271 QTestState *from, *to; 1272 1273 if (migrate_postcopy_prepare(&from, &to, args)) { 1274 return; 1275 } 1276 migrate_postcopy_start(from, to); 1277 migrate_postcopy_complete(from, to, args); 1278 } 1279 1280 static void test_postcopy(void) 1281 { 1282 MigrateCommon args = { }; 1283 1284 test_postcopy_common(&args); 1285 } 1286 1287 static void test_postcopy_suspend(void) 1288 { 1289 MigrateCommon args = { 1290 .start.suspend_me = true, 1291 }; 1292 1293 test_postcopy_common(&args); 1294 } 1295 1296 static void test_postcopy_preempt(void) 1297 { 1298 MigrateCommon args = { 1299 .postcopy_preempt = true, 1300 }; 1301 1302 test_postcopy_common(&args); 1303 } 1304 1305 #ifdef CONFIG_GNUTLS 1306 static void test_postcopy_tls_psk(void) 1307 { 1308 MigrateCommon args = { 1309 .start_hook = test_migrate_tls_psk_start_match, 1310 .finish_hook = test_migrate_tls_psk_finish, 1311 }; 1312 1313 test_postcopy_common(&args); 1314 } 1315 1316 static void test_postcopy_preempt_tls_psk(void) 1317 { 1318 MigrateCommon args = { 1319 .postcopy_preempt = true, 1320 .start_hook = test_migrate_tls_psk_start_match, 1321 .finish_hook = test_migrate_tls_psk_finish, 1322 }; 1323 1324 test_postcopy_common(&args); 1325 } 1326 #endif 1327 1328 static void wait_for_postcopy_status(QTestState *one, const char *status) 1329 { 1330 wait_for_migration_status(one, status, 1331 (const char * []) { "failed", "active", 1332 "completed", NULL }); 1333 } 1334 1335 #ifndef _WIN32 1336 static void postcopy_recover_fail(QTestState *from, QTestState *to) 1337 { 1338 int ret, pair1[2], pair2[2]; 1339 char c; 1340 1341 /* Create two unrelated socketpairs */ 1342 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1); 1343 g_assert_cmpint(ret, ==, 0); 1344 1345 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2); 1346 g_assert_cmpint(ret, ==, 0); 1347 1348 /* 1349 * Give the guests unpaired ends of the sockets, so they'll all blocked 1350 * at reading. This mimics a wrong channel established. 1351 */ 1352 qtest_qmp_fds_assert_success(from, &pair1[0], 1, 1353 "{ 'execute': 'getfd'," 1354 " 'arguments': { 'fdname': 'fd-mig' }}"); 1355 qtest_qmp_fds_assert_success(to, &pair2[0], 1, 1356 "{ 'execute': 'getfd'," 1357 " 'arguments': { 'fdname': 'fd-mig' }}"); 1358 1359 /* 1360 * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to 1361 * emulate the 1st byte of a real recovery, but stops from there to 1362 * keep dest QEMU in RECOVER. This is needed so that we can kick off 1363 * the recover process on dest QEMU (by triggering the G_IO_IN event). 1364 * 1365 * NOTE: this trick is not needed on src QEMUs, because src doesn't 1366 * rely on an pre-existing G_IO_IN event, so it will always trigger the 1367 * upcoming recovery anyway even if it can read nothing. 1368 */ 1369 #define QEMU_VM_COMMAND 0x08 1370 c = QEMU_VM_COMMAND; 1371 ret = send(pair2[1], &c, 1, 0); 1372 g_assert_cmpint(ret, ==, 1); 1373 1374 migrate_recover(to, "fd:fd-mig"); 1375 migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}"); 1376 1377 /* 1378 * Make sure both QEMU instances will go into RECOVER stage, then test 1379 * kicking them out using migrate-pause. 1380 */ 1381 wait_for_postcopy_status(from, "postcopy-recover"); 1382 wait_for_postcopy_status(to, "postcopy-recover"); 1383 1384 /* 1385 * This would be issued by the admin upon noticing the hang, we should 1386 * make sure we're able to kick this out. 1387 */ 1388 migrate_pause(from); 1389 wait_for_postcopy_status(from, "postcopy-paused"); 1390 1391 /* Do the same test on dest */ 1392 migrate_pause(to); 1393 wait_for_postcopy_status(to, "postcopy-paused"); 1394 1395 close(pair1[0]); 1396 close(pair1[1]); 1397 close(pair2[0]); 1398 close(pair2[1]); 1399 } 1400 #endif /* _WIN32 */ 1401 1402 static void test_postcopy_recovery_common(MigrateCommon *args) 1403 { 1404 QTestState *from, *to; 1405 g_autofree char *uri = NULL; 1406 1407 /* Always hide errors for postcopy recover tests since they're expected */ 1408 args->start.hide_stderr = true; 1409 1410 if (migrate_postcopy_prepare(&from, &to, args)) { 1411 return; 1412 } 1413 1414 /* Turn postcopy speed down, 4K/s is slow enough on any machines */ 1415 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096); 1416 1417 /* Now we start the postcopy */ 1418 migrate_postcopy_start(from, to); 1419 1420 /* 1421 * Wait until postcopy is really started; we can only run the 1422 * migrate-pause command during a postcopy 1423 */ 1424 wait_for_migration_status(from, "postcopy-active", NULL); 1425 1426 /* 1427 * Manually stop the postcopy migration. This emulates a network 1428 * failure with the migration socket 1429 */ 1430 migrate_pause(from); 1431 1432 /* 1433 * Wait for destination side to reach postcopy-paused state. The 1434 * migrate-recover command can only succeed if destination machine 1435 * is in the paused state 1436 */ 1437 wait_for_postcopy_status(to, "postcopy-paused"); 1438 wait_for_postcopy_status(from, "postcopy-paused"); 1439 1440 #ifndef _WIN32 1441 if (args->postcopy_recovery_test_fail) { 1442 /* 1443 * Test when a wrong socket specified for recover, and then the 1444 * ability to kick it out, and continue with a correct socket. 1445 */ 1446 postcopy_recover_fail(from, to); 1447 /* continue with a good recovery */ 1448 } 1449 #endif /* _WIN32 */ 1450 1451 /* 1452 * Create a new socket to emulate a new channel that is different 1453 * from the broken migration channel; tell the destination to 1454 * listen to the new port 1455 */ 1456 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); 1457 migrate_recover(to, uri); 1458 1459 /* 1460 * Try to rebuild the migration channel using the resume flag and 1461 * the newly created channel 1462 */ 1463 migrate_qmp(from, to, uri, NULL, "{'resume': true}"); 1464 1465 /* Restore the postcopy bandwidth to unlimited */ 1466 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0); 1467 1468 migrate_postcopy_complete(from, to, args); 1469 } 1470 1471 static void test_postcopy_recovery(void) 1472 { 1473 MigrateCommon args = { }; 1474 1475 test_postcopy_recovery_common(&args); 1476 } 1477 1478 #ifndef _WIN32 1479 static void test_postcopy_recovery_double_fail(void) 1480 { 1481 MigrateCommon args = { 1482 .postcopy_recovery_test_fail = true, 1483 }; 1484 1485 test_postcopy_recovery_common(&args); 1486 } 1487 #endif /* _WIN32 */ 1488 1489 #ifdef CONFIG_GNUTLS 1490 static void test_postcopy_recovery_tls_psk(void) 1491 { 1492 MigrateCommon args = { 1493 .start_hook = test_migrate_tls_psk_start_match, 1494 .finish_hook = test_migrate_tls_psk_finish, 1495 }; 1496 1497 test_postcopy_recovery_common(&args); 1498 } 1499 #endif 1500 1501 static void test_postcopy_preempt_recovery(void) 1502 { 1503 MigrateCommon args = { 1504 .postcopy_preempt = true, 1505 }; 1506 1507 test_postcopy_recovery_common(&args); 1508 } 1509 1510 #ifdef CONFIG_GNUTLS 1511 /* This contains preempt+recovery+tls test altogether */ 1512 static void test_postcopy_preempt_all(void) 1513 { 1514 MigrateCommon args = { 1515 .postcopy_preempt = true, 1516 .start_hook = test_migrate_tls_psk_start_match, 1517 .finish_hook = test_migrate_tls_psk_finish, 1518 }; 1519 1520 test_postcopy_recovery_common(&args); 1521 } 1522 1523 #endif 1524 1525 static void test_baddest(void) 1526 { 1527 MigrateStart args = { 1528 .hide_stderr = true 1529 }; 1530 QTestState *from, *to; 1531 1532 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) { 1533 return; 1534 } 1535 migrate_qmp(from, to, "tcp:127.0.0.1:0", NULL, "{}"); 1536 wait_for_migration_fail(from, false); 1537 test_migrate_end(from, to, false); 1538 } 1539 1540 #ifndef _WIN32 1541 static void test_analyze_script(void) 1542 { 1543 MigrateStart args = { 1544 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1545 }; 1546 QTestState *from, *to; 1547 g_autofree char *uri = NULL; 1548 g_autofree char *file = NULL; 1549 int pid, wstatus; 1550 const char *python = g_getenv("PYTHON"); 1551 1552 if (!python) { 1553 g_test_skip("PYTHON variable not set"); 1554 return; 1555 } 1556 1557 /* dummy url */ 1558 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) { 1559 return; 1560 } 1561 1562 /* 1563 * Setting these two capabilities causes the "configuration" 1564 * vmstate to include subsections for them. The script needs to 1565 * parse those subsections properly. 1566 */ 1567 migrate_set_capability(from, "validate-uuid", true); 1568 migrate_set_capability(from, "x-ignore-shared", true); 1569 1570 file = g_strdup_printf("%s/migfile", tmpfs); 1571 uri = g_strdup_printf("exec:cat > %s", file); 1572 1573 migrate_ensure_converge(from); 1574 migrate_qmp(from, to, uri, NULL, "{}"); 1575 wait_for_migration_complete(from); 1576 1577 pid = fork(); 1578 if (!pid) { 1579 close(1); 1580 open("/dev/null", O_WRONLY); 1581 execl(python, python, ANALYZE_SCRIPT, "-f", file, NULL); 1582 g_assert_not_reached(); 1583 } 1584 1585 g_assert(waitpid(pid, &wstatus, 0) == pid); 1586 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) { 1587 g_test_message("Failed to analyze the migration stream"); 1588 g_test_fail(); 1589 } 1590 test_migrate_end(from, to, false); 1591 cleanup("migfile"); 1592 } 1593 1594 static void test_vmstate_checker_script(void) 1595 { 1596 g_autofree gchar *cmd_src = NULL; 1597 g_autofree gchar *cmd_dst = NULL; 1598 g_autofree gchar *vmstate_src = NULL; 1599 g_autofree gchar *vmstate_dst = NULL; 1600 const char *machine_alias, *machine_opts = ""; 1601 g_autofree char *machine = NULL; 1602 const char *arch = qtest_get_arch(); 1603 int pid, wstatus; 1604 const char *python = g_getenv("PYTHON"); 1605 1606 if (!getenv(QEMU_ENV_SRC) && !getenv(QEMU_ENV_DST)) { 1607 g_test_skip("Test needs two different QEMU versions"); 1608 return; 1609 } 1610 1611 if (!python) { 1612 g_test_skip("PYTHON variable not set"); 1613 return; 1614 } 1615 1616 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 1617 if (g_str_equal(arch, "i386")) { 1618 machine_alias = "pc"; 1619 } else { 1620 machine_alias = "q35"; 1621 } 1622 } else if (g_str_equal(arch, "s390x")) { 1623 machine_alias = "s390-ccw-virtio"; 1624 } else if (strcmp(arch, "ppc64") == 0) { 1625 machine_alias = "pseries"; 1626 } else if (strcmp(arch, "aarch64") == 0) { 1627 machine_alias = "virt"; 1628 } else { 1629 g_assert_not_reached(); 1630 } 1631 1632 if (!qtest_has_machine(machine_alias)) { 1633 g_autofree char *msg = g_strdup_printf("machine %s not supported", machine_alias); 1634 g_test_skip(msg); 1635 return; 1636 } 1637 1638 machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC, 1639 QEMU_ENV_DST); 1640 1641 vmstate_src = g_strdup_printf("%s/vmstate-src", tmpfs); 1642 vmstate_dst = g_strdup_printf("%s/vmstate-dst", tmpfs); 1643 1644 cmd_dst = g_strdup_printf("-machine %s,%s -dump-vmstate %s", 1645 machine, machine_opts, vmstate_dst); 1646 cmd_src = g_strdup_printf("-machine %s,%s -dump-vmstate %s", 1647 machine, machine_opts, vmstate_src); 1648 1649 qtest_init_with_env_no_handshake(QEMU_ENV_SRC, cmd_src); 1650 qtest_init_with_env_no_handshake(QEMU_ENV_DST, cmd_dst); 1651 1652 pid = fork(); 1653 if (!pid) { 1654 close(1); 1655 open("/dev/null", O_WRONLY); 1656 execl(python, python, VMSTATE_CHECKER_SCRIPT, 1657 "-s", vmstate_src, 1658 "-d", vmstate_dst, 1659 NULL); 1660 g_assert_not_reached(); 1661 } 1662 1663 g_assert(waitpid(pid, &wstatus, 0) == pid); 1664 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) { 1665 g_test_message("Failed to run vmstate-static-checker.py"); 1666 g_test_fail(); 1667 } 1668 1669 cleanup("vmstate-src"); 1670 cleanup("vmstate-dst"); 1671 } 1672 #endif 1673 1674 static void test_precopy_common(MigrateCommon *args) 1675 { 1676 QTestState *from, *to; 1677 void *data_hook = NULL; 1678 1679 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) { 1680 return; 1681 } 1682 1683 if (args->start_hook) { 1684 data_hook = args->start_hook(from, to); 1685 } 1686 1687 /* Wait for the first serial output from the source */ 1688 if (args->result == MIG_TEST_SUCCEED) { 1689 wait_for_serial("src_serial"); 1690 wait_for_suspend(from, &src_state); 1691 } 1692 1693 if (args->live) { 1694 migrate_ensure_non_converge(from); 1695 migrate_prepare_for_dirty_mem(from); 1696 } else { 1697 /* 1698 * Testing non-live migration, we allow it to run at 1699 * full speed to ensure short test case duration. 1700 * For tests expected to fail, we don't need to 1701 * change anything. 1702 */ 1703 if (args->result == MIG_TEST_SUCCEED) { 1704 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}"); 1705 wait_for_stop(from, &src_state); 1706 migrate_ensure_converge(from); 1707 } 1708 } 1709 1710 if (args->result == MIG_TEST_QMP_ERROR) { 1711 migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}"); 1712 goto finish; 1713 } 1714 1715 migrate_qmp(from, to, args->connect_uri, args->connect_channels, "{}"); 1716 1717 if (args->result != MIG_TEST_SUCCEED) { 1718 bool allow_active = args->result == MIG_TEST_FAIL; 1719 wait_for_migration_fail(from, allow_active); 1720 1721 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) { 1722 qtest_set_expected_status(to, EXIT_FAILURE); 1723 } 1724 } else { 1725 if (args->live) { 1726 /* 1727 * For initial iteration(s) we must do a full pass, 1728 * but for the final iteration, we need only wait 1729 * for some dirty mem before switching to converge 1730 */ 1731 while (args->iterations > 1) { 1732 wait_for_migration_pass(from); 1733 args->iterations--; 1734 } 1735 migrate_wait_for_dirty_mem(from, to); 1736 1737 migrate_ensure_converge(from); 1738 1739 /* 1740 * We do this first, as it has a timeout to stop us 1741 * hanging forever if migration didn't converge 1742 */ 1743 wait_for_migration_complete(from); 1744 1745 wait_for_stop(from, &src_state); 1746 1747 } else { 1748 wait_for_migration_complete(from); 1749 /* 1750 * Must wait for dst to finish reading all incoming 1751 * data on the socket before issuing 'cont' otherwise 1752 * it'll be ignored 1753 */ 1754 wait_for_migration_complete(to); 1755 1756 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}"); 1757 } 1758 1759 wait_for_resume(to, &dst_state); 1760 1761 if (args->start.suspend_me) { 1762 /* wakeup succeeds only if guest is suspended */ 1763 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}"); 1764 } 1765 1766 wait_for_serial("dest_serial"); 1767 } 1768 1769 finish: 1770 if (args->finish_hook) { 1771 args->finish_hook(from, to, data_hook); 1772 } 1773 1774 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED); 1775 } 1776 1777 static void file_dirty_offset_region(void) 1778 { 1779 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME); 1780 size_t size = FILE_TEST_OFFSET; 1781 g_autofree char *data = g_new0(char, size); 1782 1783 memset(data, FILE_TEST_MARKER, size); 1784 g_assert(g_file_set_contents(path, data, size, NULL)); 1785 } 1786 1787 static void file_check_offset_region(void) 1788 { 1789 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME); 1790 size_t size = FILE_TEST_OFFSET; 1791 g_autofree char *expected = g_new0(char, size); 1792 g_autofree char *actual = NULL; 1793 uint64_t *stream_start; 1794 1795 /* 1796 * Ensure the skipped offset region's data has not been touched 1797 * and the migration stream starts at the right place. 1798 */ 1799 1800 memset(expected, FILE_TEST_MARKER, size); 1801 1802 g_assert(g_file_get_contents(path, &actual, NULL, NULL)); 1803 g_assert(!memcmp(actual, expected, size)); 1804 1805 stream_start = (uint64_t *)(actual + size); 1806 g_assert_cmpint(cpu_to_be64(*stream_start) >> 32, ==, QEMU_VM_FILE_MAGIC); 1807 } 1808 1809 static void test_file_common(MigrateCommon *args, bool stop_src) 1810 { 1811 QTestState *from, *to; 1812 void *data_hook = NULL; 1813 bool check_offset = false; 1814 1815 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) { 1816 return; 1817 } 1818 1819 /* 1820 * File migration is never live. We can keep the source VM running 1821 * during migration, but the destination will not be running 1822 * concurrently. 1823 */ 1824 g_assert_false(args->live); 1825 1826 if (g_strrstr(args->connect_uri, "offset=")) { 1827 check_offset = true; 1828 /* 1829 * This comes before the start_hook because it's equivalent to 1830 * a management application creating the file and writing to 1831 * it so hooks should expect the file to be already present. 1832 */ 1833 file_dirty_offset_region(); 1834 } 1835 1836 if (args->start_hook) { 1837 data_hook = args->start_hook(from, to); 1838 } 1839 1840 migrate_ensure_converge(from); 1841 wait_for_serial("src_serial"); 1842 1843 if (stop_src) { 1844 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}"); 1845 wait_for_stop(from, &src_state); 1846 } 1847 1848 if (args->result == MIG_TEST_QMP_ERROR) { 1849 migrate_qmp_fail(from, args->connect_uri, NULL, "{}"); 1850 goto finish; 1851 } 1852 1853 migrate_qmp(from, to, args->connect_uri, NULL, "{}"); 1854 wait_for_migration_complete(from); 1855 1856 /* 1857 * We need to wait for the source to finish before starting the 1858 * destination. 1859 */ 1860 migrate_incoming_qmp(to, args->connect_uri, "{}"); 1861 wait_for_migration_complete(to); 1862 1863 if (stop_src) { 1864 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}"); 1865 } 1866 wait_for_resume(to, &dst_state); 1867 1868 wait_for_serial("dest_serial"); 1869 1870 if (check_offset) { 1871 file_check_offset_region(); 1872 } 1873 1874 finish: 1875 if (args->finish_hook) { 1876 args->finish_hook(from, to, data_hook); 1877 } 1878 1879 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED); 1880 } 1881 1882 static void test_precopy_unix_plain(void) 1883 { 1884 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1885 MigrateCommon args = { 1886 .listen_uri = uri, 1887 .connect_uri = uri, 1888 /* 1889 * The simplest use case of precopy, covering smoke tests of 1890 * get-dirty-log dirty tracking. 1891 */ 1892 .live = true, 1893 }; 1894 1895 test_precopy_common(&args); 1896 } 1897 1898 static void test_precopy_unix_suspend_live(void) 1899 { 1900 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1901 MigrateCommon args = { 1902 .listen_uri = uri, 1903 .connect_uri = uri, 1904 /* 1905 * despite being live, the test is fast because the src 1906 * suspends immediately. 1907 */ 1908 .live = true, 1909 .start.suspend_me = true, 1910 }; 1911 1912 test_precopy_common(&args); 1913 } 1914 1915 static void test_precopy_unix_suspend_notlive(void) 1916 { 1917 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1918 MigrateCommon args = { 1919 .listen_uri = uri, 1920 .connect_uri = uri, 1921 .start.suspend_me = true, 1922 }; 1923 1924 test_precopy_common(&args); 1925 } 1926 1927 static void test_precopy_unix_dirty_ring(void) 1928 { 1929 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1930 MigrateCommon args = { 1931 .start = { 1932 .use_dirty_ring = true, 1933 }, 1934 .listen_uri = uri, 1935 .connect_uri = uri, 1936 /* 1937 * Besides the precopy/unix basic test, cover dirty ring interface 1938 * rather than get-dirty-log. 1939 */ 1940 .live = true, 1941 }; 1942 1943 test_precopy_common(&args); 1944 } 1945 1946 #ifdef CONFIG_GNUTLS 1947 static void test_precopy_unix_tls_psk(void) 1948 { 1949 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1950 MigrateCommon args = { 1951 .connect_uri = uri, 1952 .listen_uri = uri, 1953 .start_hook = test_migrate_tls_psk_start_match, 1954 .finish_hook = test_migrate_tls_psk_finish, 1955 }; 1956 1957 test_precopy_common(&args); 1958 } 1959 1960 #ifdef CONFIG_TASN1 1961 static void test_precopy_unix_tls_x509_default_host(void) 1962 { 1963 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1964 MigrateCommon args = { 1965 .start = { 1966 .hide_stderr = true, 1967 }, 1968 .connect_uri = uri, 1969 .listen_uri = uri, 1970 .start_hook = test_migrate_tls_x509_start_default_host, 1971 .finish_hook = test_migrate_tls_x509_finish, 1972 .result = MIG_TEST_FAIL_DEST_QUIT_ERR, 1973 }; 1974 1975 test_precopy_common(&args); 1976 } 1977 1978 static void test_precopy_unix_tls_x509_override_host(void) 1979 { 1980 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1981 MigrateCommon args = { 1982 .connect_uri = uri, 1983 .listen_uri = uri, 1984 .start_hook = test_migrate_tls_x509_start_override_host, 1985 .finish_hook = test_migrate_tls_x509_finish, 1986 }; 1987 1988 test_precopy_common(&args); 1989 } 1990 #endif /* CONFIG_TASN1 */ 1991 #endif /* CONFIG_GNUTLS */ 1992 1993 #if 0 1994 /* Currently upset on aarch64 TCG */ 1995 static void test_ignore_shared(void) 1996 { 1997 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1998 QTestState *from, *to; 1999 2000 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) { 2001 return; 2002 } 2003 2004 migrate_ensure_non_converge(from); 2005 migrate_prepare_for_dirty_mem(from); 2006 2007 migrate_set_capability(from, "x-ignore-shared", true); 2008 migrate_set_capability(to, "x-ignore-shared", true); 2009 2010 /* Wait for the first serial output from the source */ 2011 wait_for_serial("src_serial"); 2012 2013 migrate_qmp(from, to, uri, NULL, "{}"); 2014 2015 migrate_wait_for_dirty_mem(from, to); 2016 2017 wait_for_stop(from, &src_state); 2018 2019 qtest_qmp_eventwait(to, "RESUME"); 2020 2021 wait_for_serial("dest_serial"); 2022 wait_for_migration_complete(from); 2023 2024 /* Check whether shared RAM has been really skipped */ 2025 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024); 2026 2027 test_migrate_end(from, to, true); 2028 } 2029 #endif 2030 2031 static void * 2032 test_migrate_xbzrle_start(QTestState *from, 2033 QTestState *to) 2034 { 2035 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432); 2036 2037 migrate_set_capability(from, "xbzrle", true); 2038 migrate_set_capability(to, "xbzrle", true); 2039 2040 return NULL; 2041 } 2042 2043 static void test_precopy_unix_xbzrle(void) 2044 { 2045 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 2046 MigrateCommon args = { 2047 .connect_uri = uri, 2048 .listen_uri = uri, 2049 .start_hook = test_migrate_xbzrle_start, 2050 .iterations = 2, 2051 /* 2052 * XBZRLE needs pages to be modified when doing the 2nd+ round 2053 * iteration to have real data pushed to the stream. 2054 */ 2055 .live = true, 2056 }; 2057 2058 test_precopy_common(&args); 2059 } 2060 2061 static void test_precopy_file(void) 2062 { 2063 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs, 2064 FILE_TEST_FILENAME); 2065 MigrateCommon args = { 2066 .connect_uri = uri, 2067 .listen_uri = "defer", 2068 }; 2069 2070 test_file_common(&args, true); 2071 } 2072 2073 #ifndef _WIN32 2074 static void fdset_add_fds(QTestState *qts, const char *file, int flags, 2075 int num_fds) 2076 { 2077 for (int i = 0; i < num_fds; i++) { 2078 int fd; 2079 2080 fd = open(file, flags, 0660); 2081 assert(fd != -1); 2082 2083 qtest_qmp_fds_assert_success(qts, &fd, 1, "{'execute': 'add-fd', " 2084 "'arguments': {'fdset-id': 1}}"); 2085 close(fd); 2086 } 2087 } 2088 2089 static void *file_offset_fdset_start_hook(QTestState *from, QTestState *to) 2090 { 2091 g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME); 2092 2093 fdset_add_fds(from, file, O_WRONLY, 1); 2094 fdset_add_fds(to, file, O_RDONLY, 1); 2095 2096 return NULL; 2097 } 2098 2099 static void test_precopy_file_offset_fdset(void) 2100 { 2101 g_autofree char *uri = g_strdup_printf("file:/dev/fdset/1,offset=%d", 2102 FILE_TEST_OFFSET); 2103 MigrateCommon args = { 2104 .connect_uri = uri, 2105 .listen_uri = "defer", 2106 .start_hook = file_offset_fdset_start_hook, 2107 }; 2108 2109 test_file_common(&args, false); 2110 } 2111 #endif 2112 2113 static void test_precopy_file_offset(void) 2114 { 2115 g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=%d", tmpfs, 2116 FILE_TEST_FILENAME, 2117 FILE_TEST_OFFSET); 2118 MigrateCommon args = { 2119 .connect_uri = uri, 2120 .listen_uri = "defer", 2121 }; 2122 2123 test_file_common(&args, false); 2124 } 2125 2126 static void test_precopy_file_offset_bad(void) 2127 { 2128 /* using a value not supported by qemu_strtosz() */ 2129 g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=0x20M", 2130 tmpfs, FILE_TEST_FILENAME); 2131 MigrateCommon args = { 2132 .connect_uri = uri, 2133 .listen_uri = "defer", 2134 .result = MIG_TEST_QMP_ERROR, 2135 }; 2136 2137 test_file_common(&args, false); 2138 } 2139 2140 static void *test_mode_reboot_start(QTestState *from, QTestState *to) 2141 { 2142 migrate_set_parameter_str(from, "mode", "cpr-reboot"); 2143 migrate_set_parameter_str(to, "mode", "cpr-reboot"); 2144 2145 migrate_set_capability(from, "x-ignore-shared", true); 2146 migrate_set_capability(to, "x-ignore-shared", true); 2147 2148 return NULL; 2149 } 2150 2151 static void *migrate_mapped_ram_start(QTestState *from, QTestState *to) 2152 { 2153 migrate_set_capability(from, "mapped-ram", true); 2154 migrate_set_capability(to, "mapped-ram", true); 2155 2156 return NULL; 2157 } 2158 2159 static void test_mode_reboot(void) 2160 { 2161 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs, 2162 FILE_TEST_FILENAME); 2163 MigrateCommon args = { 2164 .start.use_shmem = true, 2165 .connect_uri = uri, 2166 .listen_uri = "defer", 2167 .start_hook = test_mode_reboot_start 2168 }; 2169 2170 test_file_common(&args, true); 2171 } 2172 2173 static void test_precopy_file_mapped_ram_live(void) 2174 { 2175 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs, 2176 FILE_TEST_FILENAME); 2177 MigrateCommon args = { 2178 .connect_uri = uri, 2179 .listen_uri = "defer", 2180 .start_hook = migrate_mapped_ram_start, 2181 }; 2182 2183 test_file_common(&args, false); 2184 } 2185 2186 static void test_precopy_file_mapped_ram(void) 2187 { 2188 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs, 2189 FILE_TEST_FILENAME); 2190 MigrateCommon args = { 2191 .connect_uri = uri, 2192 .listen_uri = "defer", 2193 .start_hook = migrate_mapped_ram_start, 2194 }; 2195 2196 test_file_common(&args, true); 2197 } 2198 2199 static void *migrate_multifd_mapped_ram_start(QTestState *from, QTestState *to) 2200 { 2201 migrate_mapped_ram_start(from, to); 2202 2203 migrate_set_parameter_int(from, "multifd-channels", 4); 2204 migrate_set_parameter_int(to, "multifd-channels", 4); 2205 2206 migrate_set_capability(from, "multifd", true); 2207 migrate_set_capability(to, "multifd", true); 2208 2209 return NULL; 2210 } 2211 2212 static void test_multifd_file_mapped_ram_live(void) 2213 { 2214 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs, 2215 FILE_TEST_FILENAME); 2216 MigrateCommon args = { 2217 .connect_uri = uri, 2218 .listen_uri = "defer", 2219 .start_hook = migrate_multifd_mapped_ram_start, 2220 }; 2221 2222 test_file_common(&args, false); 2223 } 2224 2225 static void test_multifd_file_mapped_ram(void) 2226 { 2227 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs, 2228 FILE_TEST_FILENAME); 2229 MigrateCommon args = { 2230 .connect_uri = uri, 2231 .listen_uri = "defer", 2232 .start_hook = migrate_multifd_mapped_ram_start, 2233 }; 2234 2235 test_file_common(&args, true); 2236 } 2237 2238 2239 static void test_precopy_tcp_plain(void) 2240 { 2241 MigrateCommon args = { 2242 .listen_uri = "tcp:127.0.0.1:0", 2243 }; 2244 2245 test_precopy_common(&args); 2246 } 2247 2248 static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to) 2249 { 2250 2251 migrate_set_capability(from, "return-path", true); 2252 migrate_set_capability(to, "return-path", true); 2253 2254 migrate_set_capability(from, "switchover-ack", true); 2255 migrate_set_capability(to, "switchover-ack", true); 2256 2257 return NULL; 2258 } 2259 2260 static void test_precopy_tcp_switchover_ack(void) 2261 { 2262 MigrateCommon args = { 2263 .listen_uri = "tcp:127.0.0.1:0", 2264 .start_hook = test_migrate_switchover_ack_start, 2265 /* 2266 * Source VM must be running in order to consider the switchover ACK 2267 * when deciding to do switchover or not. 2268 */ 2269 .live = true, 2270 }; 2271 2272 test_precopy_common(&args); 2273 } 2274 2275 #ifdef CONFIG_GNUTLS 2276 static void test_precopy_tcp_tls_psk_match(void) 2277 { 2278 MigrateCommon args = { 2279 .listen_uri = "tcp:127.0.0.1:0", 2280 .start_hook = test_migrate_tls_psk_start_match, 2281 .finish_hook = test_migrate_tls_psk_finish, 2282 }; 2283 2284 test_precopy_common(&args); 2285 } 2286 2287 static void test_precopy_tcp_tls_psk_mismatch(void) 2288 { 2289 MigrateCommon args = { 2290 .start = { 2291 .hide_stderr = true, 2292 }, 2293 .listen_uri = "tcp:127.0.0.1:0", 2294 .start_hook = test_migrate_tls_psk_start_mismatch, 2295 .finish_hook = test_migrate_tls_psk_finish, 2296 .result = MIG_TEST_FAIL, 2297 }; 2298 2299 test_precopy_common(&args); 2300 } 2301 2302 #ifdef CONFIG_TASN1 2303 static void test_precopy_tcp_tls_x509_default_host(void) 2304 { 2305 MigrateCommon args = { 2306 .listen_uri = "tcp:127.0.0.1:0", 2307 .start_hook = test_migrate_tls_x509_start_default_host, 2308 .finish_hook = test_migrate_tls_x509_finish, 2309 }; 2310 2311 test_precopy_common(&args); 2312 } 2313 2314 static void test_precopy_tcp_tls_x509_override_host(void) 2315 { 2316 MigrateCommon args = { 2317 .listen_uri = "tcp:127.0.0.1:0", 2318 .start_hook = test_migrate_tls_x509_start_override_host, 2319 .finish_hook = test_migrate_tls_x509_finish, 2320 }; 2321 2322 test_precopy_common(&args); 2323 } 2324 2325 static void test_precopy_tcp_tls_x509_mismatch_host(void) 2326 { 2327 MigrateCommon args = { 2328 .start = { 2329 .hide_stderr = true, 2330 }, 2331 .listen_uri = "tcp:127.0.0.1:0", 2332 .start_hook = test_migrate_tls_x509_start_mismatch_host, 2333 .finish_hook = test_migrate_tls_x509_finish, 2334 .result = MIG_TEST_FAIL_DEST_QUIT_ERR, 2335 }; 2336 2337 test_precopy_common(&args); 2338 } 2339 2340 static void test_precopy_tcp_tls_x509_friendly_client(void) 2341 { 2342 MigrateCommon args = { 2343 .listen_uri = "tcp:127.0.0.1:0", 2344 .start_hook = test_migrate_tls_x509_start_friendly_client, 2345 .finish_hook = test_migrate_tls_x509_finish, 2346 }; 2347 2348 test_precopy_common(&args); 2349 } 2350 2351 static void test_precopy_tcp_tls_x509_hostile_client(void) 2352 { 2353 MigrateCommon args = { 2354 .start = { 2355 .hide_stderr = true, 2356 }, 2357 .listen_uri = "tcp:127.0.0.1:0", 2358 .start_hook = test_migrate_tls_x509_start_hostile_client, 2359 .finish_hook = test_migrate_tls_x509_finish, 2360 .result = MIG_TEST_FAIL, 2361 }; 2362 2363 test_precopy_common(&args); 2364 } 2365 2366 static void test_precopy_tcp_tls_x509_allow_anon_client(void) 2367 { 2368 MigrateCommon args = { 2369 .listen_uri = "tcp:127.0.0.1:0", 2370 .start_hook = test_migrate_tls_x509_start_allow_anon_client, 2371 .finish_hook = test_migrate_tls_x509_finish, 2372 }; 2373 2374 test_precopy_common(&args); 2375 } 2376 2377 static void test_precopy_tcp_tls_x509_reject_anon_client(void) 2378 { 2379 MigrateCommon args = { 2380 .start = { 2381 .hide_stderr = true, 2382 }, 2383 .listen_uri = "tcp:127.0.0.1:0", 2384 .start_hook = test_migrate_tls_x509_start_reject_anon_client, 2385 .finish_hook = test_migrate_tls_x509_finish, 2386 .result = MIG_TEST_FAIL, 2387 }; 2388 2389 test_precopy_common(&args); 2390 } 2391 #endif /* CONFIG_TASN1 */ 2392 #endif /* CONFIG_GNUTLS */ 2393 2394 #ifndef _WIN32 2395 static void *test_migrate_fd_start_hook(QTestState *from, 2396 QTestState *to) 2397 { 2398 int ret; 2399 int pair[2]; 2400 2401 /* Create two connected sockets for migration */ 2402 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair); 2403 g_assert_cmpint(ret, ==, 0); 2404 2405 /* Send the 1st socket to the target */ 2406 qtest_qmp_fds_assert_success(to, &pair[0], 1, 2407 "{ 'execute': 'getfd'," 2408 " 'arguments': { 'fdname': 'fd-mig' }}"); 2409 close(pair[0]); 2410 2411 /* Start incoming migration from the 1st socket */ 2412 migrate_incoming_qmp(to, "fd:fd-mig", "{}"); 2413 2414 /* Send the 2nd socket to the target */ 2415 qtest_qmp_fds_assert_success(from, &pair[1], 1, 2416 "{ 'execute': 'getfd'," 2417 " 'arguments': { 'fdname': 'fd-mig' }}"); 2418 close(pair[1]); 2419 2420 return NULL; 2421 } 2422 2423 static void test_migrate_fd_finish_hook(QTestState *from, 2424 QTestState *to, 2425 void *opaque) 2426 { 2427 QDict *rsp; 2428 const char *error_desc; 2429 2430 /* Test closing fds */ 2431 /* We assume, that QEMU removes named fd from its list, 2432 * so this should fail */ 2433 rsp = qtest_qmp(from, "{ 'execute': 'closefd'," 2434 " 'arguments': { 'fdname': 'fd-mig' }}"); 2435 g_assert_true(qdict_haskey(rsp, "error")); 2436 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 2437 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 2438 qobject_unref(rsp); 2439 2440 rsp = qtest_qmp(to, "{ 'execute': 'closefd'," 2441 " 'arguments': { 'fdname': 'fd-mig' }}"); 2442 g_assert_true(qdict_haskey(rsp, "error")); 2443 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 2444 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 2445 qobject_unref(rsp); 2446 } 2447 2448 static void test_migrate_precopy_fd_socket(void) 2449 { 2450 MigrateCommon args = { 2451 .listen_uri = "defer", 2452 .connect_uri = "fd:fd-mig", 2453 .start_hook = test_migrate_fd_start_hook, 2454 .finish_hook = test_migrate_fd_finish_hook 2455 }; 2456 test_precopy_common(&args); 2457 } 2458 2459 static void *migrate_precopy_fd_file_start(QTestState *from, QTestState *to) 2460 { 2461 g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME); 2462 int src_flags = O_CREAT | O_RDWR; 2463 int dst_flags = O_CREAT | O_RDWR; 2464 int fds[2]; 2465 2466 fds[0] = open(file, src_flags, 0660); 2467 assert(fds[0] != -1); 2468 2469 fds[1] = open(file, dst_flags, 0660); 2470 assert(fds[1] != -1); 2471 2472 2473 qtest_qmp_fds_assert_success(to, &fds[0], 1, 2474 "{ 'execute': 'getfd'," 2475 " 'arguments': { 'fdname': 'fd-mig' }}"); 2476 2477 qtest_qmp_fds_assert_success(from, &fds[1], 1, 2478 "{ 'execute': 'getfd'," 2479 " 'arguments': { 'fdname': 'fd-mig' }}"); 2480 2481 close(fds[0]); 2482 close(fds[1]); 2483 2484 return NULL; 2485 } 2486 2487 static void test_migrate_precopy_fd_file(void) 2488 { 2489 MigrateCommon args = { 2490 .listen_uri = "defer", 2491 .connect_uri = "fd:fd-mig", 2492 .start_hook = migrate_precopy_fd_file_start, 2493 .finish_hook = test_migrate_fd_finish_hook 2494 }; 2495 test_file_common(&args, true); 2496 } 2497 #endif /* _WIN32 */ 2498 2499 static void do_test_validate_uuid(MigrateStart *args, bool should_fail) 2500 { 2501 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 2502 QTestState *from, *to; 2503 2504 if (test_migrate_start(&from, &to, uri, args)) { 2505 return; 2506 } 2507 2508 /* 2509 * UUID validation is at the begin of migration. So, the main process of 2510 * migration is not interesting for us here. Thus, set huge downtime for 2511 * very fast migration. 2512 */ 2513 migrate_set_parameter_int(from, "downtime-limit", 1000000); 2514 migrate_set_capability(from, "validate-uuid", true); 2515 2516 /* Wait for the first serial output from the source */ 2517 wait_for_serial("src_serial"); 2518 2519 migrate_qmp(from, to, uri, NULL, "{}"); 2520 2521 if (should_fail) { 2522 qtest_set_expected_status(to, EXIT_FAILURE); 2523 wait_for_migration_fail(from, true); 2524 } else { 2525 wait_for_migration_complete(from); 2526 } 2527 2528 test_migrate_end(from, to, false); 2529 } 2530 2531 static void test_validate_uuid(void) 2532 { 2533 MigrateStart args = { 2534 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 2535 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111", 2536 }; 2537 2538 do_test_validate_uuid(&args, false); 2539 } 2540 2541 static void test_validate_uuid_error(void) 2542 { 2543 MigrateStart args = { 2544 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 2545 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 2546 .hide_stderr = true, 2547 }; 2548 2549 do_test_validate_uuid(&args, true); 2550 } 2551 2552 static void test_validate_uuid_src_not_set(void) 2553 { 2554 MigrateStart args = { 2555 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 2556 .hide_stderr = true, 2557 }; 2558 2559 do_test_validate_uuid(&args, false); 2560 } 2561 2562 static void test_validate_uuid_dst_not_set(void) 2563 { 2564 MigrateStart args = { 2565 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 2566 .hide_stderr = true, 2567 }; 2568 2569 do_test_validate_uuid(&args, false); 2570 } 2571 2572 static void do_test_validate_uri_channel(MigrateCommon *args) 2573 { 2574 QTestState *from, *to; 2575 2576 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) { 2577 return; 2578 } 2579 2580 /* Wait for the first serial output from the source */ 2581 wait_for_serial("src_serial"); 2582 2583 /* 2584 * 'uri' and 'channels' validation is checked even before the migration 2585 * starts. 2586 */ 2587 migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}"); 2588 test_migrate_end(from, to, false); 2589 } 2590 2591 static void test_validate_uri_channels_both_set(void) 2592 { 2593 MigrateCommon args = { 2594 .start = { 2595 .hide_stderr = true, 2596 }, 2597 .listen_uri = "defer", 2598 .connect_uri = "tcp:127.0.0.1:0", 2599 .connect_channels = "[ { 'channel-type': 'main'," 2600 " 'addr': { 'transport': 'socket'," 2601 " 'type': 'inet'," 2602 " 'host': '127.0.0.1'," 2603 " 'port': '0' } } ]", 2604 }; 2605 2606 do_test_validate_uri_channel(&args); 2607 } 2608 2609 static void test_validate_uri_channels_none_set(void) 2610 { 2611 MigrateCommon args = { 2612 .start = { 2613 .hide_stderr = true, 2614 }, 2615 .listen_uri = "defer", 2616 }; 2617 2618 do_test_validate_uri_channel(&args); 2619 } 2620 2621 /* 2622 * The way auto_converge works, we need to do too many passes to 2623 * run this test. Auto_converge logic is only run once every 2624 * three iterations, so: 2625 * 2626 * - 3 iterations without auto_converge enabled 2627 * - 3 iterations with pct = 5 2628 * - 3 iterations with pct = 30 2629 * - 3 iterations with pct = 55 2630 * - 3 iterations with pct = 80 2631 * - 3 iterations with pct = 95 (max(95, 80 + 25)) 2632 * 2633 * To make things even worse, we need to run the initial stage at 2634 * 3MB/s so we enter autoconverge even when host is (over)loaded. 2635 */ 2636 static void test_migrate_auto_converge(void) 2637 { 2638 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 2639 MigrateStart args = {}; 2640 QTestState *from, *to; 2641 int64_t percentage; 2642 2643 /* 2644 * We want the test to be stable and as fast as possible. 2645 * E.g., with 1Gb/s bandwidth migration may pass without throttling, 2646 * so we need to decrease a bandwidth. 2647 */ 2648 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95; 2649 2650 if (test_migrate_start(&from, &to, uri, &args)) { 2651 return; 2652 } 2653 2654 migrate_set_capability(from, "auto-converge", true); 2655 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct); 2656 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct); 2657 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct); 2658 2659 /* 2660 * Set the initial parameters so that the migration could not converge 2661 * without throttling. 2662 */ 2663 migrate_ensure_non_converge(from); 2664 2665 /* To check remaining size after precopy */ 2666 migrate_set_capability(from, "pause-before-switchover", true); 2667 2668 /* Wait for the first serial output from the source */ 2669 wait_for_serial("src_serial"); 2670 2671 migrate_qmp(from, to, uri, NULL, "{}"); 2672 2673 /* Wait for throttling begins */ 2674 percentage = 0; 2675 do { 2676 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 2677 if (percentage != 0) { 2678 break; 2679 } 2680 usleep(20); 2681 g_assert_false(src_state.stop_seen); 2682 } while (true); 2683 /* The first percentage of throttling should be at least init_pct */ 2684 g_assert_cmpint(percentage, >=, init_pct); 2685 /* Now, when we tested that throttling works, let it converge */ 2686 migrate_ensure_converge(from); 2687 2688 /* 2689 * Wait for pre-switchover status to check last throttle percentage 2690 * and remaining. These values will be zeroed later 2691 */ 2692 wait_for_migration_status(from, "pre-switchover", NULL); 2693 2694 /* The final percentage of throttling shouldn't be greater than max_pct */ 2695 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 2696 g_assert_cmpint(percentage, <=, max_pct); 2697 migrate_continue(from, "pre-switchover"); 2698 2699 qtest_qmp_eventwait(to, "RESUME"); 2700 2701 wait_for_serial("dest_serial"); 2702 wait_for_migration_complete(from); 2703 2704 test_migrate_end(from, to, true); 2705 } 2706 2707 static void * 2708 test_migrate_precopy_tcp_multifd_start_common(QTestState *from, 2709 QTestState *to, 2710 const char *method) 2711 { 2712 migrate_set_parameter_int(from, "multifd-channels", 16); 2713 migrate_set_parameter_int(to, "multifd-channels", 16); 2714 2715 migrate_set_parameter_str(from, "multifd-compression", method); 2716 migrate_set_parameter_str(to, "multifd-compression", method); 2717 2718 migrate_set_capability(from, "multifd", true); 2719 migrate_set_capability(to, "multifd", true); 2720 2721 /* Start incoming migration from the 1st socket */ 2722 migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}"); 2723 2724 return NULL; 2725 } 2726 2727 static void * 2728 test_migrate_precopy_tcp_multifd_start(QTestState *from, 2729 QTestState *to) 2730 { 2731 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2732 } 2733 2734 static void * 2735 test_migrate_precopy_tcp_multifd_start_zero_page_legacy(QTestState *from, 2736 QTestState *to) 2737 { 2738 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2739 migrate_set_parameter_str(from, "zero-page-detection", "legacy"); 2740 return NULL; 2741 } 2742 2743 static void * 2744 test_migration_precopy_tcp_multifd_start_no_zero_page(QTestState *from, 2745 QTestState *to) 2746 { 2747 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2748 migrate_set_parameter_str(from, "zero-page-detection", "none"); 2749 return NULL; 2750 } 2751 2752 static void * 2753 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from, 2754 QTestState *to) 2755 { 2756 /* 2757 * Overloading this test to also check that set_parameter does not error. 2758 * This is also done in the tests for the other compression methods. 2759 */ 2760 migrate_set_parameter_int(from, "multifd-zlib-level", 2); 2761 migrate_set_parameter_int(to, "multifd-zlib-level", 2); 2762 2763 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib"); 2764 } 2765 2766 #ifdef CONFIG_ZSTD 2767 static void * 2768 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from, 2769 QTestState *to) 2770 { 2771 migrate_set_parameter_int(from, "multifd-zstd-level", 2); 2772 migrate_set_parameter_int(to, "multifd-zstd-level", 2); 2773 2774 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd"); 2775 } 2776 #endif /* CONFIG_ZSTD */ 2777 2778 #ifdef CONFIG_QPL 2779 static void * 2780 test_migrate_precopy_tcp_multifd_qpl_start(QTestState *from, 2781 QTestState *to) 2782 { 2783 return test_migrate_precopy_tcp_multifd_start_common(from, to, "qpl"); 2784 } 2785 #endif /* CONFIG_QPL */ 2786 #ifdef CONFIG_UADK 2787 static void * 2788 test_migrate_precopy_tcp_multifd_uadk_start(QTestState *from, 2789 QTestState *to) 2790 { 2791 return test_migrate_precopy_tcp_multifd_start_common(from, to, "uadk"); 2792 } 2793 #endif /* CONFIG_UADK */ 2794 2795 static void test_multifd_tcp_uri_none(void) 2796 { 2797 MigrateCommon args = { 2798 .listen_uri = "defer", 2799 .start_hook = test_migrate_precopy_tcp_multifd_start, 2800 /* 2801 * Multifd is more complicated than most of the features, it 2802 * directly takes guest page buffers when sending, make sure 2803 * everything will work alright even if guest page is changing. 2804 */ 2805 .live = true, 2806 }; 2807 test_precopy_common(&args); 2808 } 2809 2810 static void test_multifd_tcp_zero_page_legacy(void) 2811 { 2812 MigrateCommon args = { 2813 .listen_uri = "defer", 2814 .start_hook = test_migrate_precopy_tcp_multifd_start_zero_page_legacy, 2815 /* 2816 * Multifd is more complicated than most of the features, it 2817 * directly takes guest page buffers when sending, make sure 2818 * everything will work alright even if guest page is changing. 2819 */ 2820 .live = true, 2821 }; 2822 test_precopy_common(&args); 2823 } 2824 2825 static void test_multifd_tcp_no_zero_page(void) 2826 { 2827 MigrateCommon args = { 2828 .listen_uri = "defer", 2829 .start_hook = test_migration_precopy_tcp_multifd_start_no_zero_page, 2830 /* 2831 * Multifd is more complicated than most of the features, it 2832 * directly takes guest page buffers when sending, make sure 2833 * everything will work alright even if guest page is changing. 2834 */ 2835 .live = true, 2836 }; 2837 test_precopy_common(&args); 2838 } 2839 2840 static void test_multifd_tcp_channels_none(void) 2841 { 2842 MigrateCommon args = { 2843 .listen_uri = "defer", 2844 .start_hook = test_migrate_precopy_tcp_multifd_start, 2845 .live = true, 2846 .connect_channels = "[ { 'channel-type': 'main'," 2847 " 'addr': { 'transport': 'socket'," 2848 " 'type': 'inet'," 2849 " 'host': '127.0.0.1'," 2850 " 'port': '0' } } ]", 2851 }; 2852 test_precopy_common(&args); 2853 } 2854 2855 static void test_multifd_tcp_zlib(void) 2856 { 2857 MigrateCommon args = { 2858 .listen_uri = "defer", 2859 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start, 2860 }; 2861 test_precopy_common(&args); 2862 } 2863 2864 #ifdef CONFIG_ZSTD 2865 static void test_multifd_tcp_zstd(void) 2866 { 2867 MigrateCommon args = { 2868 .listen_uri = "defer", 2869 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start, 2870 }; 2871 test_precopy_common(&args); 2872 } 2873 #endif 2874 2875 #ifdef CONFIG_QPL 2876 static void test_multifd_tcp_qpl(void) 2877 { 2878 MigrateCommon args = { 2879 .listen_uri = "defer", 2880 .start_hook = test_migrate_precopy_tcp_multifd_qpl_start, 2881 }; 2882 test_precopy_common(&args); 2883 } 2884 #endif 2885 2886 #ifdef CONFIG_UADK 2887 static void test_multifd_tcp_uadk(void) 2888 { 2889 MigrateCommon args = { 2890 .listen_uri = "defer", 2891 .start_hook = test_migrate_precopy_tcp_multifd_uadk_start, 2892 }; 2893 test_precopy_common(&args); 2894 } 2895 #endif 2896 2897 #ifdef CONFIG_GNUTLS 2898 static void * 2899 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from, 2900 QTestState *to) 2901 { 2902 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2903 return test_migrate_tls_psk_start_match(from, to); 2904 } 2905 2906 static void * 2907 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from, 2908 QTestState *to) 2909 { 2910 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2911 return test_migrate_tls_psk_start_mismatch(from, to); 2912 } 2913 2914 #ifdef CONFIG_TASN1 2915 static void * 2916 test_migrate_multifd_tls_x509_start_default_host(QTestState *from, 2917 QTestState *to) 2918 { 2919 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2920 return test_migrate_tls_x509_start_default_host(from, to); 2921 } 2922 2923 static void * 2924 test_migrate_multifd_tls_x509_start_override_host(QTestState *from, 2925 QTestState *to) 2926 { 2927 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2928 return test_migrate_tls_x509_start_override_host(from, to); 2929 } 2930 2931 static void * 2932 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from, 2933 QTestState *to) 2934 { 2935 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2936 return test_migrate_tls_x509_start_mismatch_host(from, to); 2937 } 2938 2939 static void * 2940 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from, 2941 QTestState *to) 2942 { 2943 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2944 return test_migrate_tls_x509_start_allow_anon_client(from, to); 2945 } 2946 2947 static void * 2948 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from, 2949 QTestState *to) 2950 { 2951 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 2952 return test_migrate_tls_x509_start_reject_anon_client(from, to); 2953 } 2954 #endif /* CONFIG_TASN1 */ 2955 2956 static void test_multifd_tcp_tls_psk_match(void) 2957 { 2958 MigrateCommon args = { 2959 .listen_uri = "defer", 2960 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match, 2961 .finish_hook = test_migrate_tls_psk_finish, 2962 }; 2963 test_precopy_common(&args); 2964 } 2965 2966 static void test_multifd_tcp_tls_psk_mismatch(void) 2967 { 2968 MigrateCommon args = { 2969 .start = { 2970 .hide_stderr = true, 2971 }, 2972 .listen_uri = "defer", 2973 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch, 2974 .finish_hook = test_migrate_tls_psk_finish, 2975 .result = MIG_TEST_FAIL, 2976 }; 2977 test_precopy_common(&args); 2978 } 2979 2980 #ifdef CONFIG_TASN1 2981 static void test_multifd_tcp_tls_x509_default_host(void) 2982 { 2983 MigrateCommon args = { 2984 .listen_uri = "defer", 2985 .start_hook = test_migrate_multifd_tls_x509_start_default_host, 2986 .finish_hook = test_migrate_tls_x509_finish, 2987 }; 2988 test_precopy_common(&args); 2989 } 2990 2991 static void test_multifd_tcp_tls_x509_override_host(void) 2992 { 2993 MigrateCommon args = { 2994 .listen_uri = "defer", 2995 .start_hook = test_migrate_multifd_tls_x509_start_override_host, 2996 .finish_hook = test_migrate_tls_x509_finish, 2997 }; 2998 test_precopy_common(&args); 2999 } 3000 3001 static void test_multifd_tcp_tls_x509_mismatch_host(void) 3002 { 3003 /* 3004 * This has different behaviour to the non-multifd case. 3005 * 3006 * In non-multifd case when client aborts due to mismatched 3007 * cert host, the server has already started trying to load 3008 * migration state, and so it exits with I/O failure. 3009 * 3010 * In multifd case when client aborts due to mismatched 3011 * cert host, the server is still waiting for the other 3012 * multifd connections to arrive so hasn't started trying 3013 * to load migration state, and thus just aborts the migration 3014 * without exiting. 3015 */ 3016 MigrateCommon args = { 3017 .start = { 3018 .hide_stderr = true, 3019 }, 3020 .listen_uri = "defer", 3021 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host, 3022 .finish_hook = test_migrate_tls_x509_finish, 3023 .result = MIG_TEST_FAIL, 3024 }; 3025 test_precopy_common(&args); 3026 } 3027 3028 static void test_multifd_tcp_tls_x509_allow_anon_client(void) 3029 { 3030 MigrateCommon args = { 3031 .listen_uri = "defer", 3032 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client, 3033 .finish_hook = test_migrate_tls_x509_finish, 3034 }; 3035 test_precopy_common(&args); 3036 } 3037 3038 static void test_multifd_tcp_tls_x509_reject_anon_client(void) 3039 { 3040 MigrateCommon args = { 3041 .start = { 3042 .hide_stderr = true, 3043 }, 3044 .listen_uri = "defer", 3045 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client, 3046 .finish_hook = test_migrate_tls_x509_finish, 3047 .result = MIG_TEST_FAIL, 3048 }; 3049 test_precopy_common(&args); 3050 } 3051 #endif /* CONFIG_TASN1 */ 3052 #endif /* CONFIG_GNUTLS */ 3053 3054 /* 3055 * This test does: 3056 * source target 3057 * migrate_incoming 3058 * migrate 3059 * migrate_cancel 3060 * launch another target 3061 * migrate 3062 * 3063 * And see that it works 3064 */ 3065 static void test_multifd_tcp_cancel(void) 3066 { 3067 MigrateStart args = { 3068 .hide_stderr = true, 3069 }; 3070 QTestState *from, *to, *to2; 3071 3072 if (test_migrate_start(&from, &to, "defer", &args)) { 3073 return; 3074 } 3075 3076 migrate_ensure_non_converge(from); 3077 migrate_prepare_for_dirty_mem(from); 3078 3079 migrate_set_parameter_int(from, "multifd-channels", 16); 3080 migrate_set_parameter_int(to, "multifd-channels", 16); 3081 3082 migrate_set_capability(from, "multifd", true); 3083 migrate_set_capability(to, "multifd", true); 3084 3085 /* Start incoming migration from the 1st socket */ 3086 migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}"); 3087 3088 /* Wait for the first serial output from the source */ 3089 wait_for_serial("src_serial"); 3090 3091 migrate_qmp(from, to, NULL, NULL, "{}"); 3092 3093 migrate_wait_for_dirty_mem(from, to); 3094 3095 migrate_cancel(from); 3096 3097 /* Make sure QEMU process "to" exited */ 3098 qtest_set_expected_status(to, EXIT_FAILURE); 3099 qtest_wait_qemu(to); 3100 3101 args = (MigrateStart){ 3102 .only_target = true, 3103 }; 3104 3105 if (test_migrate_start(&from, &to2, "defer", &args)) { 3106 return; 3107 } 3108 3109 migrate_set_parameter_int(to2, "multifd-channels", 16); 3110 3111 migrate_set_capability(to2, "multifd", true); 3112 3113 /* Start incoming migration from the 1st socket */ 3114 migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", "{}"); 3115 3116 wait_for_migration_status(from, "cancelled", NULL); 3117 3118 migrate_ensure_non_converge(from); 3119 3120 migrate_qmp(from, to2, NULL, NULL, "{}"); 3121 3122 migrate_wait_for_dirty_mem(from, to2); 3123 3124 migrate_ensure_converge(from); 3125 3126 wait_for_stop(from, &src_state); 3127 qtest_qmp_eventwait(to2, "RESUME"); 3128 3129 wait_for_serial("dest_serial"); 3130 wait_for_migration_complete(from); 3131 test_migrate_end(from, to2, true); 3132 } 3133 3134 static void calc_dirty_rate(QTestState *who, uint64_t calc_time) 3135 { 3136 qtest_qmp_assert_success(who, 3137 "{ 'execute': 'calc-dirty-rate'," 3138 "'arguments': { " 3139 "'calc-time': %" PRIu64 "," 3140 "'mode': 'dirty-ring' }}", 3141 calc_time); 3142 } 3143 3144 static QDict *query_dirty_rate(QTestState *who) 3145 { 3146 return qtest_qmp_assert_success_ref(who, 3147 "{ 'execute': 'query-dirty-rate' }"); 3148 } 3149 3150 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate) 3151 { 3152 qtest_qmp_assert_success(who, 3153 "{ 'execute': 'set-vcpu-dirty-limit'," 3154 "'arguments': { " 3155 "'dirty-rate': %" PRIu64 " } }", 3156 dirtyrate); 3157 } 3158 3159 static void cancel_vcpu_dirty_limit(QTestState *who) 3160 { 3161 qtest_qmp_assert_success(who, 3162 "{ 'execute': 'cancel-vcpu-dirty-limit' }"); 3163 } 3164 3165 static QDict *query_vcpu_dirty_limit(QTestState *who) 3166 { 3167 QDict *rsp; 3168 3169 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }"); 3170 g_assert(!qdict_haskey(rsp, "error")); 3171 g_assert(qdict_haskey(rsp, "return")); 3172 3173 return rsp; 3174 } 3175 3176 static bool calc_dirtyrate_ready(QTestState *who) 3177 { 3178 QDict *rsp_return; 3179 gchar *status; 3180 3181 rsp_return = query_dirty_rate(who); 3182 g_assert(rsp_return); 3183 3184 status = g_strdup(qdict_get_str(rsp_return, "status")); 3185 g_assert(status); 3186 3187 return g_strcmp0(status, "measuring"); 3188 } 3189 3190 static void wait_for_calc_dirtyrate_complete(QTestState *who, 3191 int64_t time_s) 3192 { 3193 int max_try_count = 10000; 3194 usleep(time_s * 1000000); 3195 3196 while (!calc_dirtyrate_ready(who) && max_try_count--) { 3197 usleep(1000); 3198 } 3199 3200 /* 3201 * Set the timeout with 10 s(max_try_count * 1000us), 3202 * if dirtyrate measurement not complete, fail test. 3203 */ 3204 g_assert_cmpint(max_try_count, !=, 0); 3205 } 3206 3207 static int64_t get_dirty_rate(QTestState *who) 3208 { 3209 QDict *rsp_return; 3210 gchar *status; 3211 QList *rates; 3212 const QListEntry *entry; 3213 QDict *rate; 3214 int64_t dirtyrate; 3215 3216 rsp_return = query_dirty_rate(who); 3217 g_assert(rsp_return); 3218 3219 status = g_strdup(qdict_get_str(rsp_return, "status")); 3220 g_assert(status); 3221 g_assert_cmpstr(status, ==, "measured"); 3222 3223 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate"); 3224 g_assert(rates && !qlist_empty(rates)); 3225 3226 entry = qlist_first(rates); 3227 g_assert(entry); 3228 3229 rate = qobject_to(QDict, qlist_entry_obj(entry)); 3230 g_assert(rate); 3231 3232 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1); 3233 3234 qobject_unref(rsp_return); 3235 return dirtyrate; 3236 } 3237 3238 static int64_t get_limit_rate(QTestState *who) 3239 { 3240 QDict *rsp_return; 3241 QList *rates; 3242 const QListEntry *entry; 3243 QDict *rate; 3244 int64_t dirtyrate; 3245 3246 rsp_return = query_vcpu_dirty_limit(who); 3247 g_assert(rsp_return); 3248 3249 rates = qdict_get_qlist(rsp_return, "return"); 3250 g_assert(rates && !qlist_empty(rates)); 3251 3252 entry = qlist_first(rates); 3253 g_assert(entry); 3254 3255 rate = qobject_to(QDict, qlist_entry_obj(entry)); 3256 g_assert(rate); 3257 3258 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1); 3259 3260 qobject_unref(rsp_return); 3261 return dirtyrate; 3262 } 3263 3264 static QTestState *dirtylimit_start_vm(void) 3265 { 3266 QTestState *vm = NULL; 3267 g_autofree gchar *cmd = NULL; 3268 3269 bootfile_create(tmpfs, false); 3270 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 " 3271 "-name dirtylimit-test,debug-threads=on " 3272 "-m 150M -smp 1 " 3273 "-serial file:%s/vm_serial " 3274 "-drive file=%s,format=raw ", 3275 tmpfs, bootpath); 3276 3277 vm = qtest_init(cmd); 3278 return vm; 3279 } 3280 3281 static void dirtylimit_stop_vm(QTestState *vm) 3282 { 3283 qtest_quit(vm); 3284 cleanup("vm_serial"); 3285 } 3286 3287 static void test_vcpu_dirty_limit(void) 3288 { 3289 QTestState *vm; 3290 int64_t origin_rate; 3291 int64_t quota_rate; 3292 int64_t rate ; 3293 int max_try_count = 20; 3294 int hit = 0; 3295 3296 /* Start vm for vcpu dirtylimit test */ 3297 vm = dirtylimit_start_vm(); 3298 3299 /* Wait for the first serial output from the vm*/ 3300 wait_for_serial("vm_serial"); 3301 3302 /* Do dirtyrate measurement with calc time equals 1s */ 3303 calc_dirty_rate(vm, 1); 3304 3305 /* Sleep calc time and wait for calc dirtyrate complete */ 3306 wait_for_calc_dirtyrate_complete(vm, 1); 3307 3308 /* Query original dirty page rate */ 3309 origin_rate = get_dirty_rate(vm); 3310 3311 /* VM booted from bootsect should dirty memory steadily */ 3312 assert(origin_rate != 0); 3313 3314 /* Setup quota dirty page rate at half of origin */ 3315 quota_rate = origin_rate / 2; 3316 3317 /* Set dirtylimit */ 3318 dirtylimit_set_all(vm, quota_rate); 3319 3320 /* 3321 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit 3322 * works literally 3323 */ 3324 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm)); 3325 3326 /* Sleep a bit to check if it take effect */ 3327 usleep(2000000); 3328 3329 /* 3330 * Check if dirtylimit take effect realistically, set the 3331 * timeout with 20 s(max_try_count * 1s), if dirtylimit 3332 * doesn't take effect, fail test. 3333 */ 3334 while (--max_try_count) { 3335 calc_dirty_rate(vm, 1); 3336 wait_for_calc_dirtyrate_complete(vm, 1); 3337 rate = get_dirty_rate(vm); 3338 3339 /* 3340 * Assume hitting if current rate is less 3341 * than quota rate (within accepting error) 3342 */ 3343 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 3344 hit = 1; 3345 break; 3346 } 3347 } 3348 3349 g_assert_cmpint(hit, ==, 1); 3350 3351 hit = 0; 3352 max_try_count = 20; 3353 3354 /* Check if dirtylimit cancellation take effect */ 3355 cancel_vcpu_dirty_limit(vm); 3356 while (--max_try_count) { 3357 calc_dirty_rate(vm, 1); 3358 wait_for_calc_dirtyrate_complete(vm, 1); 3359 rate = get_dirty_rate(vm); 3360 3361 /* 3362 * Assume dirtylimit be canceled if current rate is 3363 * greater than quota rate (within accepting error) 3364 */ 3365 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 3366 hit = 1; 3367 break; 3368 } 3369 } 3370 3371 g_assert_cmpint(hit, ==, 1); 3372 dirtylimit_stop_vm(vm); 3373 } 3374 3375 static void migrate_dirty_limit_wait_showup(QTestState *from, 3376 const int64_t period, 3377 const int64_t value) 3378 { 3379 /* Enable dirty limit capability */ 3380 migrate_set_capability(from, "dirty-limit", true); 3381 3382 /* Set dirty limit parameters */ 3383 migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period); 3384 migrate_set_parameter_int(from, "vcpu-dirty-limit", value); 3385 3386 /* Make sure migrate can't converge */ 3387 migrate_ensure_non_converge(from); 3388 3389 /* To check limit rate after precopy */ 3390 migrate_set_capability(from, "pause-before-switchover", true); 3391 3392 /* Wait for the serial output from the source */ 3393 wait_for_serial("src_serial"); 3394 } 3395 3396 /* 3397 * This test does: 3398 * source destination 3399 * start vm 3400 * start incoming vm 3401 * migrate 3402 * wait dirty limit to begin 3403 * cancel migrate 3404 * cancellation check 3405 * restart incoming vm 3406 * migrate 3407 * wait dirty limit to begin 3408 * wait pre-switchover event 3409 * convergence condition check 3410 * 3411 * And see if dirty limit migration works correctly. 3412 * This test case involves many passes, so it runs in slow mode only. 3413 */ 3414 static void test_migrate_dirty_limit(void) 3415 { 3416 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 3417 QTestState *from, *to; 3418 int64_t remaining; 3419 uint64_t throttle_us_per_full; 3420 /* 3421 * We want the test to be stable and as fast as possible. 3422 * E.g., with 1Gb/s bandwidth migration may pass without dirty limit, 3423 * so we need to decrease a bandwidth. 3424 */ 3425 const int64_t dirtylimit_period = 1000, dirtylimit_value = 50; 3426 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */ 3427 const int64_t downtime_limit = 250; /* 250ms */ 3428 /* 3429 * We migrate through unix-socket (> 500Mb/s). 3430 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s). 3431 * So, we can predict expected_threshold 3432 */ 3433 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000; 3434 int max_try_count = 10; 3435 MigrateCommon args = { 3436 .start = { 3437 .hide_stderr = true, 3438 .use_dirty_ring = true, 3439 }, 3440 .listen_uri = uri, 3441 .connect_uri = uri, 3442 }; 3443 3444 /* Start src, dst vm */ 3445 if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) { 3446 return; 3447 } 3448 3449 /* Prepare for dirty limit migration and wait src vm show up */ 3450 migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value); 3451 3452 /* Start migrate */ 3453 migrate_qmp(from, to, args.connect_uri, NULL, "{}"); 3454 3455 /* Wait for dirty limit throttle begin */ 3456 throttle_us_per_full = 0; 3457 while (throttle_us_per_full == 0) { 3458 throttle_us_per_full = 3459 read_migrate_property_int(from, "dirty-limit-throttle-time-per-round"); 3460 usleep(100); 3461 g_assert_false(src_state.stop_seen); 3462 } 3463 3464 /* Now cancel migrate and wait for dirty limit throttle switch off */ 3465 migrate_cancel(from); 3466 wait_for_migration_status(from, "cancelled", NULL); 3467 3468 /* Check if dirty limit throttle switched off, set timeout 1ms */ 3469 do { 3470 throttle_us_per_full = 3471 read_migrate_property_int(from, "dirty-limit-throttle-time-per-round"); 3472 usleep(100); 3473 g_assert_false(src_state.stop_seen); 3474 } while (throttle_us_per_full != 0 && --max_try_count); 3475 3476 /* Assert dirty limit is not in service */ 3477 g_assert_cmpint(throttle_us_per_full, ==, 0); 3478 3479 args = (MigrateCommon) { 3480 .start = { 3481 .only_target = true, 3482 .use_dirty_ring = true, 3483 }, 3484 .listen_uri = uri, 3485 .connect_uri = uri, 3486 }; 3487 3488 /* Restart dst vm, src vm already show up so we needn't wait anymore */ 3489 if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) { 3490 return; 3491 } 3492 3493 /* Start migrate */ 3494 migrate_qmp(from, to, args.connect_uri, NULL, "{}"); 3495 3496 /* Wait for dirty limit throttle begin */ 3497 throttle_us_per_full = 0; 3498 while (throttle_us_per_full == 0) { 3499 throttle_us_per_full = 3500 read_migrate_property_int(from, "dirty-limit-throttle-time-per-round"); 3501 usleep(100); 3502 g_assert_false(src_state.stop_seen); 3503 } 3504 3505 /* 3506 * The dirty limit rate should equals the return value of 3507 * query-vcpu-dirty-limit if dirty limit cap set 3508 */ 3509 g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from)); 3510 3511 /* Now, we have tested if dirty limit works, let it converge */ 3512 migrate_set_parameter_int(from, "downtime-limit", downtime_limit); 3513 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth); 3514 3515 /* 3516 * Wait for pre-switchover status to check if migration 3517 * satisfy the convergence condition 3518 */ 3519 wait_for_migration_status(from, "pre-switchover", NULL); 3520 3521 remaining = read_ram_property_int(from, "remaining"); 3522 g_assert_cmpint(remaining, <, 3523 (expected_threshold + expected_threshold / 100)); 3524 3525 migrate_continue(from, "pre-switchover"); 3526 3527 qtest_qmp_eventwait(to, "RESUME"); 3528 3529 wait_for_serial("dest_serial"); 3530 wait_for_migration_complete(from); 3531 3532 test_migrate_end(from, to, true); 3533 } 3534 3535 static bool kvm_dirty_ring_supported(void) 3536 { 3537 #if defined(__linux__) && defined(HOST_X86_64) 3538 int ret, kvm_fd = open("/dev/kvm", O_RDONLY); 3539 3540 if (kvm_fd < 0) { 3541 return false; 3542 } 3543 3544 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING); 3545 close(kvm_fd); 3546 3547 /* We test with 4096 slots */ 3548 if (ret < 4096) { 3549 return false; 3550 } 3551 3552 return true; 3553 #else 3554 return false; 3555 #endif 3556 } 3557 3558 int main(int argc, char **argv) 3559 { 3560 bool has_kvm, has_tcg; 3561 bool has_uffd, is_x86; 3562 const char *arch; 3563 g_autoptr(GError) err = NULL; 3564 const char *qemu_src = getenv(QEMU_ENV_SRC); 3565 const char *qemu_dst = getenv(QEMU_ENV_DST); 3566 int ret; 3567 3568 g_test_init(&argc, &argv, NULL); 3569 3570 /* 3571 * The default QTEST_QEMU_BINARY must always be provided because 3572 * that is what helpers use to query the accel type and 3573 * architecture. 3574 */ 3575 if (qemu_src && qemu_dst) { 3576 g_test_message("Only one of %s, %s is allowed", 3577 QEMU_ENV_SRC, QEMU_ENV_DST); 3578 exit(1); 3579 } 3580 3581 has_kvm = qtest_has_accel("kvm"); 3582 has_tcg = qtest_has_accel("tcg"); 3583 3584 if (!has_tcg && !has_kvm) { 3585 g_test_skip("No KVM or TCG accelerator available"); 3586 return 0; 3587 } 3588 3589 has_uffd = ufd_version_check(); 3590 arch = qtest_get_arch(); 3591 is_x86 = !strcmp(arch, "i386") || !strcmp(arch, "x86_64"); 3592 3593 tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err); 3594 if (!tmpfs) { 3595 g_test_message("Can't create temporary directory in %s: %s", 3596 g_get_tmp_dir(), err->message); 3597 } 3598 g_assert(tmpfs); 3599 3600 module_call_init(MODULE_INIT_QOM); 3601 3602 migration_test_add("/migration/bad_dest", test_baddest); 3603 #ifndef _WIN32 3604 migration_test_add("/migration/analyze-script", test_analyze_script); 3605 migration_test_add("/migration/vmstate-checker-script", 3606 test_vmstate_checker_script); 3607 #endif 3608 3609 /* 3610 * On s390x with TCG, migration is observed to hang due to the 'pending' 3611 * state of the flic interrupt controller not being migrated or 3612 * reconstructed post-migration. Disable it until the problem is resolved. 3613 */ 3614 if (g_str_equal(arch, "s390x") && !has_kvm) { 3615 g_test_message("Skipping tests: s390x host with KVM is required"); 3616 goto test_add_done; 3617 } 3618 3619 if (is_x86) { 3620 migration_test_add("/migration/precopy/unix/suspend/live", 3621 test_precopy_unix_suspend_live); 3622 migration_test_add("/migration/precopy/unix/suspend/notlive", 3623 test_precopy_unix_suspend_notlive); 3624 } 3625 3626 if (has_uffd) { 3627 migration_test_add("/migration/postcopy/plain", test_postcopy); 3628 migration_test_add("/migration/postcopy/recovery/plain", 3629 test_postcopy_recovery); 3630 migration_test_add("/migration/postcopy/preempt/plain", 3631 test_postcopy_preempt); 3632 migration_test_add("/migration/postcopy/preempt/recovery/plain", 3633 test_postcopy_preempt_recovery); 3634 #ifndef _WIN32 3635 migration_test_add("/migration/postcopy/recovery/double-failures", 3636 test_postcopy_recovery_double_fail); 3637 #endif /* _WIN32 */ 3638 if (is_x86) { 3639 migration_test_add("/migration/postcopy/suspend", 3640 test_postcopy_suspend); 3641 } 3642 } 3643 3644 migration_test_add("/migration/precopy/unix/plain", 3645 test_precopy_unix_plain); 3646 migration_test_add("/migration/precopy/unix/xbzrle", 3647 test_precopy_unix_xbzrle); 3648 migration_test_add("/migration/precopy/file", 3649 test_precopy_file); 3650 migration_test_add("/migration/precopy/file/offset", 3651 test_precopy_file_offset); 3652 #ifndef _WIN32 3653 migration_test_add("/migration/precopy/file/offset/fdset", 3654 test_precopy_file_offset_fdset); 3655 #endif 3656 migration_test_add("/migration/precopy/file/offset/bad", 3657 test_precopy_file_offset_bad); 3658 3659 /* 3660 * Our CI system has problems with shared memory. 3661 * Don't run this test until we find a workaround. 3662 */ 3663 if (getenv("QEMU_TEST_FLAKY_TESTS")) { 3664 migration_test_add("/migration/mode/reboot", test_mode_reboot); 3665 } 3666 3667 migration_test_add("/migration/precopy/file/mapped-ram", 3668 test_precopy_file_mapped_ram); 3669 migration_test_add("/migration/precopy/file/mapped-ram/live", 3670 test_precopy_file_mapped_ram_live); 3671 3672 migration_test_add("/migration/multifd/file/mapped-ram", 3673 test_multifd_file_mapped_ram); 3674 migration_test_add("/migration/multifd/file/mapped-ram/live", 3675 test_multifd_file_mapped_ram_live); 3676 3677 #ifdef CONFIG_GNUTLS 3678 migration_test_add("/migration/precopy/unix/tls/psk", 3679 test_precopy_unix_tls_psk); 3680 3681 if (has_uffd) { 3682 /* 3683 * NOTE: psk test is enough for postcopy, as other types of TLS 3684 * channels are tested under precopy. Here what we want to test is the 3685 * general postcopy path that has TLS channel enabled. 3686 */ 3687 migration_test_add("/migration/postcopy/tls/psk", 3688 test_postcopy_tls_psk); 3689 migration_test_add("/migration/postcopy/recovery/tls/psk", 3690 test_postcopy_recovery_tls_psk); 3691 migration_test_add("/migration/postcopy/preempt/tls/psk", 3692 test_postcopy_preempt_tls_psk); 3693 migration_test_add("/migration/postcopy/preempt/recovery/tls/psk", 3694 test_postcopy_preempt_all); 3695 } 3696 #ifdef CONFIG_TASN1 3697 migration_test_add("/migration/precopy/unix/tls/x509/default-host", 3698 test_precopy_unix_tls_x509_default_host); 3699 migration_test_add("/migration/precopy/unix/tls/x509/override-host", 3700 test_precopy_unix_tls_x509_override_host); 3701 #endif /* CONFIG_TASN1 */ 3702 #endif /* CONFIG_GNUTLS */ 3703 3704 migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain); 3705 3706 migration_test_add("/migration/precopy/tcp/plain/switchover-ack", 3707 test_precopy_tcp_switchover_ack); 3708 3709 #ifdef CONFIG_GNUTLS 3710 migration_test_add("/migration/precopy/tcp/tls/psk/match", 3711 test_precopy_tcp_tls_psk_match); 3712 migration_test_add("/migration/precopy/tcp/tls/psk/mismatch", 3713 test_precopy_tcp_tls_psk_mismatch); 3714 #ifdef CONFIG_TASN1 3715 migration_test_add("/migration/precopy/tcp/tls/x509/default-host", 3716 test_precopy_tcp_tls_x509_default_host); 3717 migration_test_add("/migration/precopy/tcp/tls/x509/override-host", 3718 test_precopy_tcp_tls_x509_override_host); 3719 migration_test_add("/migration/precopy/tcp/tls/x509/mismatch-host", 3720 test_precopy_tcp_tls_x509_mismatch_host); 3721 migration_test_add("/migration/precopy/tcp/tls/x509/friendly-client", 3722 test_precopy_tcp_tls_x509_friendly_client); 3723 migration_test_add("/migration/precopy/tcp/tls/x509/hostile-client", 3724 test_precopy_tcp_tls_x509_hostile_client); 3725 migration_test_add("/migration/precopy/tcp/tls/x509/allow-anon-client", 3726 test_precopy_tcp_tls_x509_allow_anon_client); 3727 migration_test_add("/migration/precopy/tcp/tls/x509/reject-anon-client", 3728 test_precopy_tcp_tls_x509_reject_anon_client); 3729 #endif /* CONFIG_TASN1 */ 3730 #endif /* CONFIG_GNUTLS */ 3731 3732 /* migration_test_add("/migration/ignore_shared", test_ignore_shared); */ 3733 #ifndef _WIN32 3734 migration_test_add("/migration/precopy/fd/tcp", 3735 test_migrate_precopy_fd_socket); 3736 migration_test_add("/migration/precopy/fd/file", 3737 test_migrate_precopy_fd_file); 3738 #endif 3739 migration_test_add("/migration/validate_uuid", test_validate_uuid); 3740 migration_test_add("/migration/validate_uuid_error", 3741 test_validate_uuid_error); 3742 migration_test_add("/migration/validate_uuid_src_not_set", 3743 test_validate_uuid_src_not_set); 3744 migration_test_add("/migration/validate_uuid_dst_not_set", 3745 test_validate_uuid_dst_not_set); 3746 migration_test_add("/migration/validate_uri/channels/both_set", 3747 test_validate_uri_channels_both_set); 3748 migration_test_add("/migration/validate_uri/channels/none_set", 3749 test_validate_uri_channels_none_set); 3750 /* 3751 * See explanation why this test is slow on function definition 3752 */ 3753 if (g_test_slow()) { 3754 migration_test_add("/migration/auto_converge", 3755 test_migrate_auto_converge); 3756 if (g_str_equal(arch, "x86_64") && 3757 has_kvm && kvm_dirty_ring_supported()) { 3758 migration_test_add("/migration/dirty_limit", 3759 test_migrate_dirty_limit); 3760 } 3761 } 3762 migration_test_add("/migration/multifd/tcp/uri/plain/none", 3763 test_multifd_tcp_uri_none); 3764 migration_test_add("/migration/multifd/tcp/channels/plain/none", 3765 test_multifd_tcp_channels_none); 3766 migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy", 3767 test_multifd_tcp_zero_page_legacy); 3768 migration_test_add("/migration/multifd/tcp/plain/zero-page/none", 3769 test_multifd_tcp_no_zero_page); 3770 migration_test_add("/migration/multifd/tcp/plain/cancel", 3771 test_multifd_tcp_cancel); 3772 migration_test_add("/migration/multifd/tcp/plain/zlib", 3773 test_multifd_tcp_zlib); 3774 #ifdef CONFIG_ZSTD 3775 migration_test_add("/migration/multifd/tcp/plain/zstd", 3776 test_multifd_tcp_zstd); 3777 #endif 3778 #ifdef CONFIG_QPL 3779 migration_test_add("/migration/multifd/tcp/plain/qpl", 3780 test_multifd_tcp_qpl); 3781 #endif 3782 #ifdef CONFIG_UADK 3783 migration_test_add("/migration/multifd/tcp/plain/uadk", 3784 test_multifd_tcp_uadk); 3785 #endif 3786 #ifdef CONFIG_GNUTLS 3787 migration_test_add("/migration/multifd/tcp/tls/psk/match", 3788 test_multifd_tcp_tls_psk_match); 3789 migration_test_add("/migration/multifd/tcp/tls/psk/mismatch", 3790 test_multifd_tcp_tls_psk_mismatch); 3791 #ifdef CONFIG_TASN1 3792 migration_test_add("/migration/multifd/tcp/tls/x509/default-host", 3793 test_multifd_tcp_tls_x509_default_host); 3794 migration_test_add("/migration/multifd/tcp/tls/x509/override-host", 3795 test_multifd_tcp_tls_x509_override_host); 3796 migration_test_add("/migration/multifd/tcp/tls/x509/mismatch-host", 3797 test_multifd_tcp_tls_x509_mismatch_host); 3798 migration_test_add("/migration/multifd/tcp/tls/x509/allow-anon-client", 3799 test_multifd_tcp_tls_x509_allow_anon_client); 3800 migration_test_add("/migration/multifd/tcp/tls/x509/reject-anon-client", 3801 test_multifd_tcp_tls_x509_reject_anon_client); 3802 #endif /* CONFIG_TASN1 */ 3803 #endif /* CONFIG_GNUTLS */ 3804 3805 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) { 3806 migration_test_add("/migration/dirty_ring", 3807 test_precopy_unix_dirty_ring); 3808 migration_test_add("/migration/vcpu_dirty_limit", 3809 test_vcpu_dirty_limit); 3810 } 3811 3812 test_add_done: 3813 3814 ret = g_test_run(); 3815 3816 g_assert_cmpint(ret, ==, 0); 3817 3818 bootfile_delete(); 3819 ret = rmdir(tmpfs); 3820 if (ret != 0) { 3821 g_test_message("unable to rmdir: path (%s): %s", 3822 tmpfs, strerror(errno)); 3823 } 3824 g_free(tmpfs); 3825 3826 return ret; 3827 } 3828