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/error.h" 17 #include "qapi/qmp/qdict.h" 18 #include "qemu/module.h" 19 #include "qemu/option.h" 20 #include "qemu/range.h" 21 #include "qemu/sockets.h" 22 #include "chardev/char.h" 23 #include "qapi/qapi-visit-sockets.h" 24 #include "qapi/qobject-input-visitor.h" 25 #include "qapi/qobject-output-visitor.h" 26 #include "crypto/tlscredspsk.h" 27 #include "qapi/qmp/qlist.h" 28 29 #include "migration-helpers.h" 30 #include "tests/migration/migration-test.h" 31 #ifdef CONFIG_GNUTLS 32 # include "tests/unit/crypto-tls-psk-helpers.h" 33 # ifdef CONFIG_TASN1 34 # include "tests/unit/crypto-tls-x509-helpers.h" 35 # endif /* CONFIG_TASN1 */ 36 #endif /* CONFIG_GNUTLS */ 37 38 /* For dirty ring test; so far only x86_64 is supported */ 39 #if defined(__linux__) && defined(HOST_X86_64) 40 #include "linux/kvm.h" 41 #endif 42 43 /* TODO actually test the results and get rid of this */ 44 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__)) 45 46 unsigned start_address; 47 unsigned end_address; 48 static bool uffd_feature_thread_id; 49 50 /* 51 * Dirtylimit stop working if dirty page rate error 52 * value less than DIRTYLIMIT_TOLERANCE_RANGE 53 */ 54 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */ 55 56 #if defined(__linux__) 57 #include <sys/syscall.h> 58 #include <sys/vfs.h> 59 #endif 60 61 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD) 62 #include <sys/eventfd.h> 63 #include <sys/ioctl.h> 64 #include <linux/userfaultfd.h> 65 66 static bool ufd_version_check(void) 67 { 68 struct uffdio_api api_struct; 69 uint64_t ioctl_mask; 70 71 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC); 72 73 if (ufd == -1) { 74 g_test_message("Skipping test: userfaultfd not available"); 75 return false; 76 } 77 78 api_struct.api = UFFD_API; 79 api_struct.features = 0; 80 if (ioctl(ufd, UFFDIO_API, &api_struct)) { 81 g_test_message("Skipping test: UFFDIO_API failed"); 82 return false; 83 } 84 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID; 85 86 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER | 87 (__u64)1 << _UFFDIO_UNREGISTER; 88 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) { 89 g_test_message("Skipping test: Missing userfault feature"); 90 return false; 91 } 92 93 return true; 94 } 95 96 #else 97 static bool ufd_version_check(void) 98 { 99 g_test_message("Skipping test: Userfault not available (builtdtime)"); 100 return false; 101 } 102 103 #endif 104 105 static const char *tmpfs; 106 107 /* The boot file modifies memory area in [start_address, end_address) 108 * repeatedly. It outputs a 'B' at a fixed rate while it's still running. 109 */ 110 #include "tests/migration/i386/a-b-bootblock.h" 111 #include "tests/migration/aarch64/a-b-kernel.h" 112 #include "tests/migration/s390x/a-b-bios.h" 113 114 static void init_bootfile(const char *bootpath, void *content, size_t len) 115 { 116 FILE *bootfile = fopen(bootpath, "wb"); 117 118 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1); 119 fclose(bootfile); 120 } 121 122 /* 123 * Wait for some output in the serial output file, 124 * we get an 'A' followed by an endless string of 'B's 125 * but on the destination we won't have the A. 126 */ 127 static void wait_for_serial(const char *side) 128 { 129 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side); 130 FILE *serialfile = fopen(serialpath, "r"); 131 const char *arch = qtest_get_arch(); 132 int started = (strcmp(side, "src_serial") == 0 && 133 strcmp(arch, "ppc64") == 0) ? 0 : 1; 134 135 do { 136 int readvalue = fgetc(serialfile); 137 138 if (!started) { 139 /* SLOF prints its banner before starting test, 140 * to ignore it, mark the start of the test with '_', 141 * ignore all characters until this marker 142 */ 143 switch (readvalue) { 144 case '_': 145 started = 1; 146 break; 147 case EOF: 148 fseek(serialfile, 0, SEEK_SET); 149 usleep(1000); 150 break; 151 } 152 continue; 153 } 154 switch (readvalue) { 155 case 'A': 156 /* Fine */ 157 break; 158 159 case 'B': 160 /* It's alive! */ 161 fclose(serialfile); 162 return; 163 164 case EOF: 165 started = (strcmp(side, "src_serial") == 0 && 166 strcmp(arch, "ppc64") == 0) ? 0 : 1; 167 fseek(serialfile, 0, SEEK_SET); 168 usleep(1000); 169 break; 170 171 default: 172 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side); 173 g_assert_not_reached(); 174 } 175 } while (true); 176 } 177 178 /* 179 * It's tricky to use qemu's migration event capability with qtest, 180 * events suddenly appearing confuse the qmp()/hmp() responses. 181 */ 182 183 static int64_t read_ram_property_int(QTestState *who, const char *property) 184 { 185 QDict *rsp_return, *rsp_ram; 186 int64_t result; 187 188 rsp_return = migrate_query_not_failed(who); 189 if (!qdict_haskey(rsp_return, "ram")) { 190 /* Still in setup */ 191 result = 0; 192 } else { 193 rsp_ram = qdict_get_qdict(rsp_return, "ram"); 194 result = qdict_get_try_int(rsp_ram, property, 0); 195 } 196 qobject_unref(rsp_return); 197 return result; 198 } 199 200 static int64_t read_migrate_property_int(QTestState *who, const char *property) 201 { 202 QDict *rsp_return; 203 int64_t result; 204 205 rsp_return = migrate_query_not_failed(who); 206 result = qdict_get_try_int(rsp_return, property, 0); 207 qobject_unref(rsp_return); 208 return result; 209 } 210 211 static uint64_t get_migration_pass(QTestState *who) 212 { 213 return read_ram_property_int(who, "dirty-sync-count"); 214 } 215 216 static void read_blocktime(QTestState *who) 217 { 218 QDict *rsp_return; 219 220 rsp_return = migrate_query_not_failed(who); 221 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime")); 222 qobject_unref(rsp_return); 223 } 224 225 static void wait_for_migration_pass(QTestState *who) 226 { 227 uint64_t initial_pass = get_migration_pass(who); 228 uint64_t pass; 229 230 /* Wait for the 1st sync */ 231 while (!got_stop && !initial_pass) { 232 usleep(1000); 233 initial_pass = get_migration_pass(who); 234 } 235 236 do { 237 usleep(1000); 238 pass = get_migration_pass(who); 239 } while (pass == initial_pass && !got_stop); 240 } 241 242 static void check_guests_ram(QTestState *who) 243 { 244 /* Our ASM test will have been incrementing one byte from each page from 245 * start_address to < end_address in order. This gives us a constraint 246 * that any page's byte should be equal or less than the previous pages 247 * byte (mod 256); and they should all be equal except for one transition 248 * at the point where we meet the incrementer. (We're running this with 249 * the guest stopped). 250 */ 251 unsigned address; 252 uint8_t first_byte; 253 uint8_t last_byte; 254 bool hit_edge = false; 255 int bad = 0; 256 257 qtest_memread(who, start_address, &first_byte, 1); 258 last_byte = first_byte; 259 260 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address; 261 address += TEST_MEM_PAGE_SIZE) 262 { 263 uint8_t b; 264 qtest_memread(who, address, &b, 1); 265 if (b != last_byte) { 266 if (((b + 1) % 256) == last_byte && !hit_edge) { 267 /* This is OK, the guest stopped at the point of 268 * incrementing the previous page but didn't get 269 * to us yet. 270 */ 271 hit_edge = true; 272 last_byte = b; 273 } else { 274 bad++; 275 if (bad <= 10) { 276 fprintf(stderr, "Memory content inconsistency at %x" 277 " first_byte = %x last_byte = %x current = %x" 278 " hit_edge = %x\n", 279 address, first_byte, last_byte, b, hit_edge); 280 } 281 } 282 } 283 } 284 if (bad >= 10) { 285 fprintf(stderr, "and in another %d pages", bad - 10); 286 } 287 g_assert(bad == 0); 288 } 289 290 static void cleanup(const char *filename) 291 { 292 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename); 293 294 unlink(path); 295 } 296 297 static char *SocketAddress_to_str(SocketAddress *addr) 298 { 299 switch (addr->type) { 300 case SOCKET_ADDRESS_TYPE_INET: 301 return g_strdup_printf("tcp:%s:%s", 302 addr->u.inet.host, 303 addr->u.inet.port); 304 case SOCKET_ADDRESS_TYPE_UNIX: 305 return g_strdup_printf("unix:%s", 306 addr->u.q_unix.path); 307 case SOCKET_ADDRESS_TYPE_FD: 308 return g_strdup_printf("fd:%s", addr->u.fd.str); 309 case SOCKET_ADDRESS_TYPE_VSOCK: 310 return g_strdup_printf("tcp:%s:%s", 311 addr->u.vsock.cid, 312 addr->u.vsock.port); 313 default: 314 return g_strdup("unknown address type"); 315 } 316 } 317 318 static char *migrate_get_socket_address(QTestState *who, const char *parameter) 319 { 320 QDict *rsp; 321 char *result; 322 SocketAddressList *addrs; 323 Visitor *iv = NULL; 324 QObject *object; 325 326 rsp = migrate_query(who); 327 object = qdict_get(rsp, parameter); 328 329 iv = qobject_input_visitor_new(object); 330 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort); 331 visit_free(iv); 332 333 /* we are only using a single address */ 334 result = SocketAddress_to_str(addrs->value); 335 336 qapi_free_SocketAddressList(addrs); 337 qobject_unref(rsp); 338 return result; 339 } 340 341 static long long migrate_get_parameter_int(QTestState *who, 342 const char *parameter) 343 { 344 QDict *rsp; 345 long long result; 346 347 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }"); 348 result = qdict_get_int(rsp, parameter); 349 qobject_unref(rsp); 350 return result; 351 } 352 353 static void migrate_check_parameter_int(QTestState *who, const char *parameter, 354 long long value) 355 { 356 long long result; 357 358 result = migrate_get_parameter_int(who, parameter); 359 g_assert_cmpint(result, ==, value); 360 } 361 362 static void migrate_set_parameter_int(QTestState *who, const char *parameter, 363 long long value) 364 { 365 QDict *rsp; 366 367 rsp = qtest_qmp(who, 368 "{ 'execute': 'migrate-set-parameters'," 369 "'arguments': { %s: %lld } }", 370 parameter, value); 371 g_assert(qdict_haskey(rsp, "return")); 372 qobject_unref(rsp); 373 migrate_check_parameter_int(who, parameter, value); 374 } 375 376 static char *migrate_get_parameter_str(QTestState *who, 377 const char *parameter) 378 { 379 QDict *rsp; 380 char *result; 381 382 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }"); 383 result = g_strdup(qdict_get_str(rsp, parameter)); 384 qobject_unref(rsp); 385 return result; 386 } 387 388 static void migrate_check_parameter_str(QTestState *who, const char *parameter, 389 const char *value) 390 { 391 g_autofree char *result = migrate_get_parameter_str(who, parameter); 392 g_assert_cmpstr(result, ==, value); 393 } 394 395 static void migrate_set_parameter_str(QTestState *who, const char *parameter, 396 const char *value) 397 { 398 QDict *rsp; 399 400 rsp = qtest_qmp(who, 401 "{ 'execute': 'migrate-set-parameters'," 402 "'arguments': { %s: %s } }", 403 parameter, value); 404 g_assert(qdict_haskey(rsp, "return")); 405 qobject_unref(rsp); 406 migrate_check_parameter_str(who, parameter, value); 407 } 408 409 static void migrate_ensure_non_converge(QTestState *who) 410 { 411 /* Can't converge with 1ms downtime + 30 mbs bandwidth limit */ 412 migrate_set_parameter_int(who, "max-bandwidth", 30 * 1000 * 1000); 413 migrate_set_parameter_int(who, "downtime-limit", 1); 414 } 415 416 static void migrate_ensure_converge(QTestState *who) 417 { 418 /* Should converge with 30s downtime + 1 gbs bandwidth limit */ 419 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000); 420 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000); 421 } 422 423 static void migrate_pause(QTestState *who) 424 { 425 QDict *rsp; 426 427 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }"); 428 qobject_unref(rsp); 429 } 430 431 static void migrate_continue(QTestState *who, const char *state) 432 { 433 QDict *rsp; 434 435 rsp = wait_command(who, 436 "{ 'execute': 'migrate-continue'," 437 " 'arguments': { 'state': %s } }", 438 state); 439 qobject_unref(rsp); 440 } 441 442 static void migrate_recover(QTestState *who, const char *uri) 443 { 444 QDict *rsp; 445 446 rsp = wait_command(who, 447 "{ 'execute': 'migrate-recover', " 448 " 'id': 'recover-cmd', " 449 " 'arguments': { 'uri': %s } }", 450 uri); 451 qobject_unref(rsp); 452 } 453 454 static void migrate_cancel(QTestState *who) 455 { 456 QDict *rsp; 457 458 rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }"); 459 qobject_unref(rsp); 460 } 461 462 static void migrate_set_capability(QTestState *who, const char *capability, 463 bool value) 464 { 465 QDict *rsp; 466 467 rsp = qtest_qmp(who, 468 "{ 'execute': 'migrate-set-capabilities'," 469 "'arguments': { " 470 "'capabilities': [ { " 471 "'capability': %s, 'state': %i } ] } }", 472 capability, value); 473 g_assert(qdict_haskey(rsp, "return")); 474 qobject_unref(rsp); 475 } 476 477 static void migrate_postcopy_start(QTestState *from, QTestState *to) 478 { 479 QDict *rsp; 480 481 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }"); 482 qobject_unref(rsp); 483 484 if (!got_stop) { 485 qtest_qmp_eventwait(from, "STOP"); 486 } 487 488 qtest_qmp_eventwait(to, "RESUME"); 489 } 490 491 typedef struct { 492 /* 493 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors 494 * unconditionally, because it means the user would like to be verbose. 495 */ 496 bool hide_stderr; 497 bool use_shmem; 498 /* only launch the target process */ 499 bool only_target; 500 /* Use dirty ring if true; dirty logging otherwise */ 501 bool use_dirty_ring; 502 const char *opts_source; 503 const char *opts_target; 504 } MigrateStart; 505 506 /* 507 * A hook that runs after the src and dst QEMUs have been 508 * created, but before the migration is started. This can 509 * be used to set migration parameters and capabilities. 510 * 511 * Returns: NULL, or a pointer to opaque state to be 512 * later passed to the TestMigrateFinishHook 513 */ 514 typedef void * (*TestMigrateStartHook)(QTestState *from, 515 QTestState *to); 516 517 /* 518 * A hook that runs after the migration has finished, 519 * regardless of whether it succeeded or failed, but 520 * before QEMU has terminated (unless it self-terminated 521 * due to migration error) 522 * 523 * @opaque is a pointer to state previously returned 524 * by the TestMigrateStartHook if any, or NULL. 525 */ 526 typedef void (*TestMigrateFinishHook)(QTestState *from, 527 QTestState *to, 528 void *opaque); 529 530 typedef struct { 531 /* Optional: fine tune start parameters */ 532 MigrateStart start; 533 534 /* Required: the URI for the dst QEMU to listen on */ 535 const char *listen_uri; 536 537 /* 538 * Optional: the URI for the src QEMU to connect to 539 * If NULL, then it will query the dst QEMU for its actual 540 * listening address and use that as the connect address. 541 * This allows for dynamically picking a free TCP port. 542 */ 543 const char *connect_uri; 544 545 /* Optional: callback to run at start to set migration parameters */ 546 TestMigrateStartHook start_hook; 547 /* Optional: callback to run at finish to cleanup */ 548 TestMigrateFinishHook finish_hook; 549 550 /* 551 * Optional: normally we expect the migration process to complete. 552 * 553 * There can be a variety of reasons and stages in which failure 554 * can happen during tests. 555 * 556 * If a failure is expected to happen at time of establishing 557 * the connection, then MIG_TEST_FAIL will indicate that the dst 558 * QEMU is expected to stay running and accept future migration 559 * connections. 560 * 561 * If a failure is expected to happen while processing the 562 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate 563 * that the dst QEMU is expected to quit with non-zero exit status 564 */ 565 enum { 566 /* This test should succeed, the default */ 567 MIG_TEST_SUCCEED = 0, 568 /* This test should fail, dest qemu should keep alive */ 569 MIG_TEST_FAIL, 570 /* This test should fail, dest qemu should fail with abnormal status */ 571 MIG_TEST_FAIL_DEST_QUIT_ERR, 572 } result; 573 574 /* Optional: set number of migration passes to wait for */ 575 unsigned int iterations; 576 577 /* Postcopy specific fields */ 578 void *postcopy_data; 579 bool postcopy_preempt; 580 } MigrateCommon; 581 582 static int test_migrate_start(QTestState **from, QTestState **to, 583 const char *uri, MigrateStart *args) 584 { 585 g_autofree gchar *arch_source = NULL; 586 g_autofree gchar *arch_target = NULL; 587 g_autofree gchar *cmd_source = NULL; 588 g_autofree gchar *cmd_target = NULL; 589 const gchar *ignore_stderr; 590 g_autofree char *bootpath = NULL; 591 g_autofree char *shmem_opts = NULL; 592 g_autofree char *shmem_path = NULL; 593 const char *arch = qtest_get_arch(); 594 const char *machine_opts = NULL; 595 const char *memory_size; 596 597 if (args->use_shmem) { 598 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) { 599 g_test_skip("/dev/shm is not supported"); 600 return -1; 601 } 602 } 603 604 got_stop = false; 605 bootpath = g_strdup_printf("%s/bootsect", tmpfs); 606 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 607 /* the assembled x86 boot sector should be exactly one sector large */ 608 assert(sizeof(x86_bootsect) == 512); 609 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect)); 610 memory_size = "150M"; 611 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath); 612 arch_target = g_strdup(arch_source); 613 start_address = X86_TEST_MEM_START; 614 end_address = X86_TEST_MEM_END; 615 } else if (g_str_equal(arch, "s390x")) { 616 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf)); 617 memory_size = "128M"; 618 arch_source = g_strdup_printf("-bios %s", bootpath); 619 arch_target = g_strdup(arch_source); 620 start_address = S390_TEST_MEM_START; 621 end_address = S390_TEST_MEM_END; 622 } else if (strcmp(arch, "ppc64") == 0) { 623 machine_opts = "vsmt=8"; 624 memory_size = "256M"; 625 start_address = PPC_TEST_MEM_START; 626 end_address = PPC_TEST_MEM_END; 627 arch_source = g_strdup_printf("-nodefaults " 628 "-prom-env 'use-nvramrc?=true' -prom-env " 629 "'nvramrc=hex .\" _\" begin %x %x " 630 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 " 631 "until'", end_address, start_address); 632 arch_target = g_strdup(""); 633 } else if (strcmp(arch, "aarch64") == 0) { 634 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel)); 635 machine_opts = "virt,gic-version=max"; 636 memory_size = "150M"; 637 arch_source = g_strdup_printf("-cpu max " 638 "-kernel %s", 639 bootpath); 640 arch_target = g_strdup(arch_source); 641 start_address = ARM_TEST_MEM_START; 642 end_address = ARM_TEST_MEM_END; 643 644 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE); 645 } else { 646 g_assert_not_reached(); 647 } 648 649 if (!getenv("QTEST_LOG") && args->hide_stderr) { 650 ignore_stderr = "2>/dev/null"; 651 } else { 652 ignore_stderr = ""; 653 } 654 655 if (args->use_shmem) { 656 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid()); 657 shmem_opts = g_strdup_printf( 658 "-object memory-backend-file,id=mem0,size=%s" 659 ",mem-path=%s,share=on -numa node,memdev=mem0", 660 memory_size, shmem_path); 661 } else { 662 shmem_path = NULL; 663 shmem_opts = g_strdup(""); 664 } 665 666 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s " 667 "-name source,debug-threads=on " 668 "-m %s " 669 "-serial file:%s/src_serial " 670 "%s %s %s %s", 671 args->use_dirty_ring ? 672 ",dirty-ring-size=4096" : "", 673 machine_opts ? " -machine " : "", 674 machine_opts ? machine_opts : "", 675 memory_size, tmpfs, 676 arch_source, shmem_opts, 677 args->opts_source ? args->opts_source : "", 678 ignore_stderr); 679 if (!args->only_target) { 680 *from = qtest_init(cmd_source); 681 } 682 683 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s " 684 "-name target,debug-threads=on " 685 "-m %s " 686 "-serial file:%s/dest_serial " 687 "-incoming %s " 688 "%s %s %s %s", 689 args->use_dirty_ring ? 690 ",dirty-ring-size=4096" : "", 691 machine_opts ? " -machine " : "", 692 machine_opts ? machine_opts : "", 693 memory_size, tmpfs, uri, 694 arch_target, shmem_opts, 695 args->opts_target ? args->opts_target : "", 696 ignore_stderr); 697 *to = qtest_init(cmd_target); 698 699 /* 700 * Remove shmem file immediately to avoid memory leak in test failed case. 701 * It's valid becase QEMU has already opened this file 702 */ 703 if (args->use_shmem) { 704 unlink(shmem_path); 705 } 706 707 return 0; 708 } 709 710 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) 711 { 712 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; 713 714 qtest_quit(from); 715 716 if (test_dest) { 717 qtest_memread(to, start_address, &dest_byte_a, 1); 718 719 /* Destination still running, wait for a byte to change */ 720 do { 721 qtest_memread(to, start_address, &dest_byte_b, 1); 722 usleep(1000 * 10); 723 } while (dest_byte_a == dest_byte_b); 724 725 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}"); 726 727 /* With it stopped, check nothing changes */ 728 qtest_memread(to, start_address, &dest_byte_c, 1); 729 usleep(1000 * 200); 730 qtest_memread(to, start_address, &dest_byte_d, 1); 731 g_assert_cmpint(dest_byte_c, ==, dest_byte_d); 732 733 check_guests_ram(to); 734 } 735 736 qtest_quit(to); 737 738 cleanup("bootsect"); 739 cleanup("migsocket"); 740 cleanup("src_serial"); 741 cleanup("dest_serial"); 742 } 743 744 #ifdef CONFIG_GNUTLS 745 struct TestMigrateTLSPSKData { 746 char *workdir; 747 char *workdiralt; 748 char *pskfile; 749 char *pskfilealt; 750 }; 751 752 static void * 753 test_migrate_tls_psk_start_common(QTestState *from, 754 QTestState *to, 755 bool mismatch) 756 { 757 struct TestMigrateTLSPSKData *data = 758 g_new0(struct TestMigrateTLSPSKData, 1); 759 QDict *rsp; 760 761 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs); 762 data->pskfile = g_strdup_printf("%s/%s", data->workdir, 763 QCRYPTO_TLS_CREDS_PSKFILE); 764 g_mkdir_with_parents(data->workdir, 0700); 765 test_tls_psk_init(data->pskfile); 766 767 if (mismatch) { 768 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs); 769 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt, 770 QCRYPTO_TLS_CREDS_PSKFILE); 771 g_mkdir_with_parents(data->workdiralt, 0700); 772 test_tls_psk_init_alt(data->pskfilealt); 773 } 774 775 rsp = wait_command(from, 776 "{ 'execute': 'object-add'," 777 " 'arguments': { 'qom-type': 'tls-creds-psk'," 778 " 'id': 'tlscredspsk0'," 779 " 'endpoint': 'client'," 780 " 'dir': %s," 781 " 'username': 'qemu'} }", 782 data->workdir); 783 qobject_unref(rsp); 784 785 rsp = wait_command(to, 786 "{ 'execute': 'object-add'," 787 " 'arguments': { 'qom-type': 'tls-creds-psk'," 788 " 'id': 'tlscredspsk0'," 789 " 'endpoint': 'server'," 790 " 'dir': %s } }", 791 mismatch ? data->workdiralt : data->workdir); 792 qobject_unref(rsp); 793 794 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0"); 795 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0"); 796 797 return data; 798 } 799 800 static void * 801 test_migrate_tls_psk_start_match(QTestState *from, 802 QTestState *to) 803 { 804 return test_migrate_tls_psk_start_common(from, to, false); 805 } 806 807 static void * 808 test_migrate_tls_psk_start_mismatch(QTestState *from, 809 QTestState *to) 810 { 811 return test_migrate_tls_psk_start_common(from, to, true); 812 } 813 814 static void 815 test_migrate_tls_psk_finish(QTestState *from, 816 QTestState *to, 817 void *opaque) 818 { 819 struct TestMigrateTLSPSKData *data = opaque; 820 821 test_tls_psk_cleanup(data->pskfile); 822 if (data->pskfilealt) { 823 test_tls_psk_cleanup(data->pskfilealt); 824 } 825 rmdir(data->workdir); 826 if (data->workdiralt) { 827 rmdir(data->workdiralt); 828 } 829 830 g_free(data->workdiralt); 831 g_free(data->pskfilealt); 832 g_free(data->workdir); 833 g_free(data->pskfile); 834 g_free(data); 835 } 836 837 #ifdef CONFIG_TASN1 838 typedef struct { 839 char *workdir; 840 char *keyfile; 841 char *cacert; 842 char *servercert; 843 char *serverkey; 844 char *clientcert; 845 char *clientkey; 846 } TestMigrateTLSX509Data; 847 848 typedef struct { 849 bool verifyclient; 850 bool clientcert; 851 bool hostileclient; 852 bool authzclient; 853 const char *certhostname; 854 const char *certipaddr; 855 } TestMigrateTLSX509; 856 857 static void * 858 test_migrate_tls_x509_start_common(QTestState *from, 859 QTestState *to, 860 TestMigrateTLSX509 *args) 861 { 862 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1); 863 QDict *rsp; 864 865 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs); 866 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir); 867 868 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir); 869 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir); 870 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir); 871 if (args->clientcert) { 872 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir); 873 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir); 874 } 875 876 g_mkdir_with_parents(data->workdir, 0700); 877 878 test_tls_init(data->keyfile); 879 #ifndef _WIN32 880 g_assert(link(data->keyfile, data->serverkey) == 0); 881 #else 882 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0); 883 #endif 884 if (args->clientcert) { 885 #ifndef _WIN32 886 g_assert(link(data->keyfile, data->clientkey) == 0); 887 #else 888 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0); 889 #endif 890 } 891 892 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert); 893 if (args->clientcert) { 894 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq, 895 args->hostileclient ? 896 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME : 897 QCRYPTO_TLS_TEST_CLIENT_NAME, 898 data->clientcert); 899 } 900 901 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq, 902 data->servercert, 903 args->certhostname, 904 args->certipaddr); 905 906 rsp = wait_command(from, 907 "{ 'execute': 'object-add'," 908 " 'arguments': { 'qom-type': 'tls-creds-x509'," 909 " 'id': 'tlscredsx509client0'," 910 " 'endpoint': 'client'," 911 " 'dir': %s," 912 " 'sanity-check': true," 913 " 'verify-peer': true} }", 914 data->workdir); 915 qobject_unref(rsp); 916 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0"); 917 if (args->certhostname) { 918 migrate_set_parameter_str(from, "tls-hostname", args->certhostname); 919 } 920 921 rsp = wait_command(to, 922 "{ 'execute': 'object-add'," 923 " 'arguments': { 'qom-type': 'tls-creds-x509'," 924 " 'id': 'tlscredsx509server0'," 925 " 'endpoint': 'server'," 926 " 'dir': %s," 927 " 'sanity-check': true," 928 " 'verify-peer': %i} }", 929 data->workdir, args->verifyclient); 930 qobject_unref(rsp); 931 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0"); 932 933 if (args->authzclient) { 934 rsp = wait_command(to, 935 "{ 'execute': 'object-add'," 936 " 'arguments': { 'qom-type': 'authz-simple'," 937 " 'id': 'tlsauthz0'," 938 " 'identity': %s} }", 939 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME); 940 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0"); 941 } 942 943 return data; 944 } 945 946 /* 947 * The normal case: match server's cert hostname against 948 * whatever host we were telling QEMU to connect to (if any) 949 */ 950 static void * 951 test_migrate_tls_x509_start_default_host(QTestState *from, 952 QTestState *to) 953 { 954 TestMigrateTLSX509 args = { 955 .verifyclient = true, 956 .clientcert = true, 957 .certipaddr = "127.0.0.1" 958 }; 959 return test_migrate_tls_x509_start_common(from, to, &args); 960 } 961 962 /* 963 * The unusual case: the server's cert is different from 964 * the address we're telling QEMU to connect to (if any), 965 * so we must give QEMU an explicit hostname to validate 966 */ 967 static void * 968 test_migrate_tls_x509_start_override_host(QTestState *from, 969 QTestState *to) 970 { 971 TestMigrateTLSX509 args = { 972 .verifyclient = true, 973 .clientcert = true, 974 .certhostname = "qemu.org", 975 }; 976 return test_migrate_tls_x509_start_common(from, to, &args); 977 } 978 979 /* 980 * The unusual case: the server's cert is different from 981 * the address we're telling QEMU to connect to, and so we 982 * expect the client to reject the server 983 */ 984 static void * 985 test_migrate_tls_x509_start_mismatch_host(QTestState *from, 986 QTestState *to) 987 { 988 TestMigrateTLSX509 args = { 989 .verifyclient = true, 990 .clientcert = true, 991 .certipaddr = "10.0.0.1", 992 }; 993 return test_migrate_tls_x509_start_common(from, to, &args); 994 } 995 996 static void * 997 test_migrate_tls_x509_start_friendly_client(QTestState *from, 998 QTestState *to) 999 { 1000 TestMigrateTLSX509 args = { 1001 .verifyclient = true, 1002 .clientcert = true, 1003 .authzclient = true, 1004 .certipaddr = "127.0.0.1", 1005 }; 1006 return test_migrate_tls_x509_start_common(from, to, &args); 1007 } 1008 1009 static void * 1010 test_migrate_tls_x509_start_hostile_client(QTestState *from, 1011 QTestState *to) 1012 { 1013 TestMigrateTLSX509 args = { 1014 .verifyclient = true, 1015 .clientcert = true, 1016 .hostileclient = true, 1017 .authzclient = true, 1018 .certipaddr = "127.0.0.1", 1019 }; 1020 return test_migrate_tls_x509_start_common(from, to, &args); 1021 } 1022 1023 /* 1024 * The case with no client certificate presented, 1025 * and no server verification 1026 */ 1027 static void * 1028 test_migrate_tls_x509_start_allow_anon_client(QTestState *from, 1029 QTestState *to) 1030 { 1031 TestMigrateTLSX509 args = { 1032 .certipaddr = "127.0.0.1", 1033 }; 1034 return test_migrate_tls_x509_start_common(from, to, &args); 1035 } 1036 1037 /* 1038 * The case with no client certificate presented, 1039 * and server verification rejecting 1040 */ 1041 static void * 1042 test_migrate_tls_x509_start_reject_anon_client(QTestState *from, 1043 QTestState *to) 1044 { 1045 TestMigrateTLSX509 args = { 1046 .verifyclient = true, 1047 .certipaddr = "127.0.0.1", 1048 }; 1049 return test_migrate_tls_x509_start_common(from, to, &args); 1050 } 1051 1052 static void 1053 test_migrate_tls_x509_finish(QTestState *from, 1054 QTestState *to, 1055 void *opaque) 1056 { 1057 TestMigrateTLSX509Data *data = opaque; 1058 1059 test_tls_cleanup(data->keyfile); 1060 unlink(data->cacert); 1061 unlink(data->servercert); 1062 unlink(data->serverkey); 1063 unlink(data->clientcert); 1064 unlink(data->clientkey); 1065 rmdir(data->workdir); 1066 1067 g_free(data->workdir); 1068 g_free(data->keyfile); 1069 g_free(data); 1070 } 1071 #endif /* CONFIG_TASN1 */ 1072 #endif /* CONFIG_GNUTLS */ 1073 1074 static int migrate_postcopy_prepare(QTestState **from_ptr, 1075 QTestState **to_ptr, 1076 MigrateCommon *args) 1077 { 1078 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1079 QTestState *from, *to; 1080 1081 if (test_migrate_start(&from, &to, uri, &args->start)) { 1082 return -1; 1083 } 1084 1085 if (args->start_hook) { 1086 args->postcopy_data = args->start_hook(from, to); 1087 } 1088 1089 migrate_set_capability(from, "postcopy-ram", true); 1090 migrate_set_capability(to, "postcopy-ram", true); 1091 migrate_set_capability(to, "postcopy-blocktime", true); 1092 1093 if (args->postcopy_preempt) { 1094 migrate_set_capability(from, "postcopy-preempt", true); 1095 migrate_set_capability(to, "postcopy-preempt", true); 1096 } 1097 1098 migrate_ensure_non_converge(from); 1099 1100 /* Wait for the first serial output from the source */ 1101 wait_for_serial("src_serial"); 1102 1103 migrate_qmp(from, uri, "{}"); 1104 1105 wait_for_migration_pass(from); 1106 1107 *from_ptr = from; 1108 *to_ptr = to; 1109 1110 return 0; 1111 } 1112 1113 static void migrate_postcopy_complete(QTestState *from, QTestState *to, 1114 MigrateCommon *args) 1115 { 1116 wait_for_migration_complete(from); 1117 1118 /* Make sure we get at least one "B" on destination */ 1119 wait_for_serial("dest_serial"); 1120 1121 if (uffd_feature_thread_id) { 1122 read_blocktime(to); 1123 } 1124 1125 if (args->finish_hook) { 1126 args->finish_hook(from, to, args->postcopy_data); 1127 args->postcopy_data = NULL; 1128 } 1129 1130 test_migrate_end(from, to, true); 1131 } 1132 1133 static void test_postcopy_common(MigrateCommon *args) 1134 { 1135 QTestState *from, *to; 1136 1137 if (migrate_postcopy_prepare(&from, &to, args)) { 1138 return; 1139 } 1140 migrate_postcopy_start(from, to); 1141 migrate_postcopy_complete(from, to, args); 1142 } 1143 1144 static void test_postcopy(void) 1145 { 1146 MigrateCommon args = { }; 1147 1148 test_postcopy_common(&args); 1149 } 1150 1151 static void test_postcopy_preempt(void) 1152 { 1153 MigrateCommon args = { 1154 .postcopy_preempt = true, 1155 }; 1156 1157 test_postcopy_common(&args); 1158 } 1159 1160 #ifdef CONFIG_GNUTLS 1161 static void test_postcopy_tls_psk(void) 1162 { 1163 MigrateCommon args = { 1164 .start_hook = test_migrate_tls_psk_start_match, 1165 .finish_hook = test_migrate_tls_psk_finish, 1166 }; 1167 1168 test_postcopy_common(&args); 1169 } 1170 1171 static void test_postcopy_preempt_tls_psk(void) 1172 { 1173 MigrateCommon args = { 1174 .postcopy_preempt = true, 1175 .start_hook = test_migrate_tls_psk_start_match, 1176 .finish_hook = test_migrate_tls_psk_finish, 1177 }; 1178 1179 test_postcopy_common(&args); 1180 } 1181 #endif 1182 1183 static void test_postcopy_recovery_common(MigrateCommon *args) 1184 { 1185 QTestState *from, *to; 1186 g_autofree char *uri = NULL; 1187 1188 /* Always hide errors for postcopy recover tests since they're expected */ 1189 args->start.hide_stderr = true; 1190 1191 if (migrate_postcopy_prepare(&from, &to, args)) { 1192 return; 1193 } 1194 1195 /* Turn postcopy speed down, 4K/s is slow enough on any machines */ 1196 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096); 1197 1198 /* Now we start the postcopy */ 1199 migrate_postcopy_start(from, to); 1200 1201 /* 1202 * Wait until postcopy is really started; we can only run the 1203 * migrate-pause command during a postcopy 1204 */ 1205 wait_for_migration_status(from, "postcopy-active", NULL); 1206 1207 /* 1208 * Manually stop the postcopy migration. This emulates a network 1209 * failure with the migration socket 1210 */ 1211 migrate_pause(from); 1212 1213 /* 1214 * Wait for destination side to reach postcopy-paused state. The 1215 * migrate-recover command can only succeed if destination machine 1216 * is in the paused state 1217 */ 1218 wait_for_migration_status(to, "postcopy-paused", 1219 (const char * []) { "failed", "active", 1220 "completed", NULL }); 1221 1222 /* 1223 * Create a new socket to emulate a new channel that is different 1224 * from the broken migration channel; tell the destination to 1225 * listen to the new port 1226 */ 1227 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); 1228 migrate_recover(to, uri); 1229 1230 /* 1231 * Try to rebuild the migration channel using the resume flag and 1232 * the newly created channel 1233 */ 1234 wait_for_migration_status(from, "postcopy-paused", 1235 (const char * []) { "failed", "active", 1236 "completed", NULL }); 1237 migrate_qmp(from, uri, "{'resume': true}"); 1238 1239 /* Restore the postcopy bandwidth to unlimited */ 1240 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0); 1241 1242 migrate_postcopy_complete(from, to, args); 1243 } 1244 1245 static void test_postcopy_recovery(void) 1246 { 1247 MigrateCommon args = { }; 1248 1249 test_postcopy_recovery_common(&args); 1250 } 1251 1252 #ifdef CONFIG_GNUTLS 1253 static void test_postcopy_recovery_tls_psk(void) 1254 { 1255 MigrateCommon args = { 1256 .start_hook = test_migrate_tls_psk_start_match, 1257 .finish_hook = test_migrate_tls_psk_finish, 1258 }; 1259 1260 test_postcopy_recovery_common(&args); 1261 } 1262 #endif 1263 1264 static void test_postcopy_preempt_recovery(void) 1265 { 1266 MigrateCommon args = { 1267 .postcopy_preempt = true, 1268 }; 1269 1270 test_postcopy_recovery_common(&args); 1271 } 1272 1273 #ifdef CONFIG_GNUTLS 1274 /* This contains preempt+recovery+tls test altogether */ 1275 static void test_postcopy_preempt_all(void) 1276 { 1277 MigrateCommon args = { 1278 .postcopy_preempt = true, 1279 .start_hook = test_migrate_tls_psk_start_match, 1280 .finish_hook = test_migrate_tls_psk_finish, 1281 }; 1282 1283 test_postcopy_recovery_common(&args); 1284 } 1285 #endif 1286 1287 static void test_baddest(void) 1288 { 1289 MigrateStart args = { 1290 .hide_stderr = true 1291 }; 1292 QTestState *from, *to; 1293 1294 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) { 1295 return; 1296 } 1297 migrate_qmp(from, "tcp:127.0.0.1:0", "{}"); 1298 wait_for_migration_fail(from, false); 1299 test_migrate_end(from, to, false); 1300 } 1301 1302 static void test_precopy_common(MigrateCommon *args) 1303 { 1304 QTestState *from, *to; 1305 void *data_hook = NULL; 1306 1307 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) { 1308 return; 1309 } 1310 1311 migrate_ensure_non_converge(from); 1312 1313 if (args->start_hook) { 1314 data_hook = args->start_hook(from, to); 1315 } 1316 1317 /* Wait for the first serial output from the source */ 1318 if (args->result == MIG_TEST_SUCCEED) { 1319 wait_for_serial("src_serial"); 1320 } 1321 1322 if (!args->connect_uri) { 1323 g_autofree char *local_connect_uri = 1324 migrate_get_socket_address(to, "socket-address"); 1325 migrate_qmp(from, local_connect_uri, "{}"); 1326 } else { 1327 migrate_qmp(from, args->connect_uri, "{}"); 1328 } 1329 1330 1331 if (args->result != MIG_TEST_SUCCEED) { 1332 bool allow_active = args->result == MIG_TEST_FAIL; 1333 wait_for_migration_fail(from, allow_active); 1334 1335 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) { 1336 qtest_set_expected_status(to, 1); 1337 } 1338 } else { 1339 if (args->iterations) { 1340 while (args->iterations--) { 1341 wait_for_migration_pass(from); 1342 } 1343 } else { 1344 wait_for_migration_pass(from); 1345 } 1346 1347 migrate_ensure_converge(from); 1348 1349 /* We do this first, as it has a timeout to stop us 1350 * hanging forever if migration didn't converge */ 1351 wait_for_migration_complete(from); 1352 1353 if (!got_stop) { 1354 qtest_qmp_eventwait(from, "STOP"); 1355 } 1356 1357 qtest_qmp_eventwait(to, "RESUME"); 1358 1359 wait_for_serial("dest_serial"); 1360 } 1361 1362 if (args->finish_hook) { 1363 args->finish_hook(from, to, data_hook); 1364 } 1365 1366 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED); 1367 } 1368 1369 static void test_precopy_unix_plain(void) 1370 { 1371 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1372 MigrateCommon args = { 1373 .listen_uri = uri, 1374 .connect_uri = uri, 1375 }; 1376 1377 test_precopy_common(&args); 1378 } 1379 1380 1381 static void test_precopy_unix_dirty_ring(void) 1382 { 1383 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1384 MigrateCommon args = { 1385 .start = { 1386 .use_dirty_ring = true, 1387 }, 1388 .listen_uri = uri, 1389 .connect_uri = uri, 1390 }; 1391 1392 test_precopy_common(&args); 1393 } 1394 1395 #ifdef CONFIG_GNUTLS 1396 static void test_precopy_unix_tls_psk(void) 1397 { 1398 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1399 MigrateCommon args = { 1400 .connect_uri = uri, 1401 .listen_uri = uri, 1402 .start_hook = test_migrate_tls_psk_start_match, 1403 .finish_hook = test_migrate_tls_psk_finish, 1404 }; 1405 1406 test_precopy_common(&args); 1407 } 1408 1409 #ifdef CONFIG_TASN1 1410 static void test_precopy_unix_tls_x509_default_host(void) 1411 { 1412 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1413 MigrateCommon args = { 1414 .start = { 1415 .hide_stderr = true, 1416 }, 1417 .connect_uri = uri, 1418 .listen_uri = uri, 1419 .start_hook = test_migrate_tls_x509_start_default_host, 1420 .finish_hook = test_migrate_tls_x509_finish, 1421 .result = MIG_TEST_FAIL_DEST_QUIT_ERR, 1422 }; 1423 1424 test_precopy_common(&args); 1425 } 1426 1427 static void test_precopy_unix_tls_x509_override_host(void) 1428 { 1429 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1430 MigrateCommon args = { 1431 .connect_uri = uri, 1432 .listen_uri = uri, 1433 .start_hook = test_migrate_tls_x509_start_override_host, 1434 .finish_hook = test_migrate_tls_x509_finish, 1435 }; 1436 1437 test_precopy_common(&args); 1438 } 1439 #endif /* CONFIG_TASN1 */ 1440 #endif /* CONFIG_GNUTLS */ 1441 1442 #if 0 1443 /* Currently upset on aarch64 TCG */ 1444 static void test_ignore_shared(void) 1445 { 1446 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1447 QTestState *from, *to; 1448 1449 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) { 1450 return; 1451 } 1452 1453 migrate_set_capability(from, "x-ignore-shared", true); 1454 migrate_set_capability(to, "x-ignore-shared", true); 1455 1456 /* Wait for the first serial output from the source */ 1457 wait_for_serial("src_serial"); 1458 1459 migrate_qmp(from, uri, "{}"); 1460 1461 wait_for_migration_pass(from); 1462 1463 if (!got_stop) { 1464 qtest_qmp_eventwait(from, "STOP"); 1465 } 1466 1467 qtest_qmp_eventwait(to, "RESUME"); 1468 1469 wait_for_serial("dest_serial"); 1470 wait_for_migration_complete(from); 1471 1472 /* Check whether shared RAM has been really skipped */ 1473 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024); 1474 1475 test_migrate_end(from, to, true); 1476 } 1477 #endif 1478 1479 static void * 1480 test_migrate_xbzrle_start(QTestState *from, 1481 QTestState *to) 1482 { 1483 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432); 1484 1485 migrate_set_capability(from, "xbzrle", true); 1486 migrate_set_capability(to, "xbzrle", true); 1487 1488 return NULL; 1489 } 1490 1491 static void test_precopy_unix_xbzrle(void) 1492 { 1493 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1494 MigrateCommon args = { 1495 .connect_uri = uri, 1496 .listen_uri = uri, 1497 1498 .start_hook = test_migrate_xbzrle_start, 1499 1500 .iterations = 2, 1501 }; 1502 1503 test_precopy_common(&args); 1504 } 1505 1506 static void test_precopy_tcp_plain(void) 1507 { 1508 MigrateCommon args = { 1509 .listen_uri = "tcp:127.0.0.1:0", 1510 }; 1511 1512 test_precopy_common(&args); 1513 } 1514 1515 #ifdef CONFIG_GNUTLS 1516 static void test_precopy_tcp_tls_psk_match(void) 1517 { 1518 MigrateCommon args = { 1519 .listen_uri = "tcp:127.0.0.1:0", 1520 .start_hook = test_migrate_tls_psk_start_match, 1521 .finish_hook = test_migrate_tls_psk_finish, 1522 }; 1523 1524 test_precopy_common(&args); 1525 } 1526 1527 static void test_precopy_tcp_tls_psk_mismatch(void) 1528 { 1529 MigrateCommon args = { 1530 .start = { 1531 .hide_stderr = true, 1532 }, 1533 .listen_uri = "tcp:127.0.0.1:0", 1534 .start_hook = test_migrate_tls_psk_start_mismatch, 1535 .finish_hook = test_migrate_tls_psk_finish, 1536 .result = MIG_TEST_FAIL, 1537 }; 1538 1539 test_precopy_common(&args); 1540 } 1541 1542 #ifdef CONFIG_TASN1 1543 static void test_precopy_tcp_tls_x509_default_host(void) 1544 { 1545 MigrateCommon args = { 1546 .listen_uri = "tcp:127.0.0.1:0", 1547 .start_hook = test_migrate_tls_x509_start_default_host, 1548 .finish_hook = test_migrate_tls_x509_finish, 1549 }; 1550 1551 test_precopy_common(&args); 1552 } 1553 1554 static void test_precopy_tcp_tls_x509_override_host(void) 1555 { 1556 MigrateCommon args = { 1557 .listen_uri = "tcp:127.0.0.1:0", 1558 .start_hook = test_migrate_tls_x509_start_override_host, 1559 .finish_hook = test_migrate_tls_x509_finish, 1560 }; 1561 1562 test_precopy_common(&args); 1563 } 1564 1565 static void test_precopy_tcp_tls_x509_mismatch_host(void) 1566 { 1567 MigrateCommon args = { 1568 .start = { 1569 .hide_stderr = true, 1570 }, 1571 .listen_uri = "tcp:127.0.0.1:0", 1572 .start_hook = test_migrate_tls_x509_start_mismatch_host, 1573 .finish_hook = test_migrate_tls_x509_finish, 1574 .result = MIG_TEST_FAIL_DEST_QUIT_ERR, 1575 }; 1576 1577 test_precopy_common(&args); 1578 } 1579 1580 static void test_precopy_tcp_tls_x509_friendly_client(void) 1581 { 1582 MigrateCommon args = { 1583 .listen_uri = "tcp:127.0.0.1:0", 1584 .start_hook = test_migrate_tls_x509_start_friendly_client, 1585 .finish_hook = test_migrate_tls_x509_finish, 1586 }; 1587 1588 test_precopy_common(&args); 1589 } 1590 1591 static void test_precopy_tcp_tls_x509_hostile_client(void) 1592 { 1593 MigrateCommon args = { 1594 .start = { 1595 .hide_stderr = true, 1596 }, 1597 .listen_uri = "tcp:127.0.0.1:0", 1598 .start_hook = test_migrate_tls_x509_start_hostile_client, 1599 .finish_hook = test_migrate_tls_x509_finish, 1600 .result = MIG_TEST_FAIL, 1601 }; 1602 1603 test_precopy_common(&args); 1604 } 1605 1606 static void test_precopy_tcp_tls_x509_allow_anon_client(void) 1607 { 1608 MigrateCommon args = { 1609 .listen_uri = "tcp:127.0.0.1:0", 1610 .start_hook = test_migrate_tls_x509_start_allow_anon_client, 1611 .finish_hook = test_migrate_tls_x509_finish, 1612 }; 1613 1614 test_precopy_common(&args); 1615 } 1616 1617 static void test_precopy_tcp_tls_x509_reject_anon_client(void) 1618 { 1619 MigrateCommon args = { 1620 .start = { 1621 .hide_stderr = true, 1622 }, 1623 .listen_uri = "tcp:127.0.0.1:0", 1624 .start_hook = test_migrate_tls_x509_start_reject_anon_client, 1625 .finish_hook = test_migrate_tls_x509_finish, 1626 .result = MIG_TEST_FAIL, 1627 }; 1628 1629 test_precopy_common(&args); 1630 } 1631 #endif /* CONFIG_TASN1 */ 1632 #endif /* CONFIG_GNUTLS */ 1633 1634 #ifndef _WIN32 1635 static void *test_migrate_fd_start_hook(QTestState *from, 1636 QTestState *to) 1637 { 1638 QDict *rsp; 1639 int ret; 1640 int pair[2]; 1641 1642 /* Create two connected sockets for migration */ 1643 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair); 1644 g_assert_cmpint(ret, ==, 0); 1645 1646 /* Send the 1st socket to the target */ 1647 rsp = wait_command_fd(to, pair[0], 1648 "{ 'execute': 'getfd'," 1649 " 'arguments': { 'fdname': 'fd-mig' }}"); 1650 qobject_unref(rsp); 1651 close(pair[0]); 1652 1653 /* Start incoming migration from the 1st socket */ 1654 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 1655 " 'arguments': { 'uri': 'fd:fd-mig' }}"); 1656 qobject_unref(rsp); 1657 1658 /* Send the 2nd socket to the target */ 1659 rsp = wait_command_fd(from, pair[1], 1660 "{ 'execute': 'getfd'," 1661 " 'arguments': { 'fdname': 'fd-mig' }}"); 1662 qobject_unref(rsp); 1663 close(pair[1]); 1664 1665 return NULL; 1666 } 1667 1668 static void test_migrate_fd_finish_hook(QTestState *from, 1669 QTestState *to, 1670 void *opaque) 1671 { 1672 QDict *rsp; 1673 const char *error_desc; 1674 1675 /* Test closing fds */ 1676 /* We assume, that QEMU removes named fd from its list, 1677 * so this should fail */ 1678 rsp = qtest_qmp(from, "{ 'execute': 'closefd'," 1679 " 'arguments': { 'fdname': 'fd-mig' }}"); 1680 g_assert_true(qdict_haskey(rsp, "error")); 1681 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 1682 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 1683 qobject_unref(rsp); 1684 1685 rsp = qtest_qmp(to, "{ 'execute': 'closefd'," 1686 " 'arguments': { 'fdname': 'fd-mig' }}"); 1687 g_assert_true(qdict_haskey(rsp, "error")); 1688 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 1689 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 1690 qobject_unref(rsp); 1691 } 1692 1693 static void test_migrate_fd_proto(void) 1694 { 1695 MigrateCommon args = { 1696 .listen_uri = "defer", 1697 .connect_uri = "fd:fd-mig", 1698 .start_hook = test_migrate_fd_start_hook, 1699 .finish_hook = test_migrate_fd_finish_hook 1700 }; 1701 test_precopy_common(&args); 1702 } 1703 #endif /* _WIN32 */ 1704 1705 static void do_test_validate_uuid(MigrateStart *args, bool should_fail) 1706 { 1707 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1708 QTestState *from, *to; 1709 1710 if (test_migrate_start(&from, &to, uri, args)) { 1711 return; 1712 } 1713 1714 /* 1715 * UUID validation is at the begin of migration. So, the main process of 1716 * migration is not interesting for us here. Thus, set huge downtime for 1717 * very fast migration. 1718 */ 1719 migrate_set_parameter_int(from, "downtime-limit", 1000000); 1720 migrate_set_capability(from, "validate-uuid", true); 1721 1722 /* Wait for the first serial output from the source */ 1723 wait_for_serial("src_serial"); 1724 1725 migrate_qmp(from, uri, "{}"); 1726 1727 if (should_fail) { 1728 qtest_set_expected_status(to, 1); 1729 wait_for_migration_fail(from, true); 1730 } else { 1731 wait_for_migration_complete(from); 1732 } 1733 1734 test_migrate_end(from, to, false); 1735 } 1736 1737 static void test_validate_uuid(void) 1738 { 1739 MigrateStart args = { 1740 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1741 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111", 1742 }; 1743 1744 do_test_validate_uuid(&args, false); 1745 } 1746 1747 static void test_validate_uuid_error(void) 1748 { 1749 MigrateStart args = { 1750 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1751 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 1752 .hide_stderr = true, 1753 }; 1754 1755 do_test_validate_uuid(&args, true); 1756 } 1757 1758 static void test_validate_uuid_src_not_set(void) 1759 { 1760 MigrateStart args = { 1761 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 1762 .hide_stderr = true, 1763 }; 1764 1765 do_test_validate_uuid(&args, false); 1766 } 1767 1768 static void test_validate_uuid_dst_not_set(void) 1769 { 1770 MigrateStart args = { 1771 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1772 .hide_stderr = true, 1773 }; 1774 1775 do_test_validate_uuid(&args, false); 1776 } 1777 1778 static void test_migrate_auto_converge(void) 1779 { 1780 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1781 MigrateStart args = {}; 1782 QTestState *from, *to; 1783 int64_t percentage; 1784 1785 /* 1786 * We want the test to be stable and as fast as possible. 1787 * E.g., with 1Gb/s bandwith migration may pass without throttling, 1788 * so we need to decrease a bandwidth. 1789 */ 1790 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95; 1791 1792 if (test_migrate_start(&from, &to, uri, &args)) { 1793 return; 1794 } 1795 1796 migrate_set_capability(from, "auto-converge", true); 1797 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct); 1798 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct); 1799 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct); 1800 1801 /* 1802 * Set the initial parameters so that the migration could not converge 1803 * without throttling. 1804 */ 1805 migrate_ensure_non_converge(from); 1806 1807 /* To check remaining size after precopy */ 1808 migrate_set_capability(from, "pause-before-switchover", true); 1809 1810 /* Wait for the first serial output from the source */ 1811 wait_for_serial("src_serial"); 1812 1813 migrate_qmp(from, uri, "{}"); 1814 1815 /* Wait for throttling begins */ 1816 percentage = 0; 1817 while (percentage == 0) { 1818 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 1819 usleep(100); 1820 g_assert_false(got_stop); 1821 } 1822 /* The first percentage of throttling should be equal to init_pct */ 1823 g_assert_cmpint(percentage, ==, init_pct); 1824 /* Now, when we tested that throttling works, let it converge */ 1825 migrate_ensure_converge(from); 1826 1827 /* 1828 * Wait for pre-switchover status to check last throttle percentage 1829 * and remaining. These values will be zeroed later 1830 */ 1831 wait_for_migration_status(from, "pre-switchover", NULL); 1832 1833 /* The final percentage of throttling shouldn't be greater than max_pct */ 1834 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 1835 g_assert_cmpint(percentage, <=, max_pct); 1836 migrate_continue(from, "pre-switchover"); 1837 1838 qtest_qmp_eventwait(to, "RESUME"); 1839 1840 wait_for_serial("dest_serial"); 1841 wait_for_migration_complete(from); 1842 1843 test_migrate_end(from, to, true); 1844 } 1845 1846 static void * 1847 test_migrate_precopy_tcp_multifd_start_common(QTestState *from, 1848 QTestState *to, 1849 const char *method) 1850 { 1851 QDict *rsp; 1852 1853 migrate_set_parameter_int(from, "multifd-channels", 16); 1854 migrate_set_parameter_int(to, "multifd-channels", 16); 1855 1856 migrate_set_parameter_str(from, "multifd-compression", method); 1857 migrate_set_parameter_str(to, "multifd-compression", method); 1858 1859 migrate_set_capability(from, "multifd", true); 1860 migrate_set_capability(to, "multifd", true); 1861 1862 /* Start incoming migration from the 1st socket */ 1863 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 1864 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 1865 qobject_unref(rsp); 1866 1867 return NULL; 1868 } 1869 1870 static void * 1871 test_migrate_precopy_tcp_multifd_start(QTestState *from, 1872 QTestState *to) 1873 { 1874 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1875 } 1876 1877 static void * 1878 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from, 1879 QTestState *to) 1880 { 1881 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib"); 1882 } 1883 1884 #ifdef CONFIG_ZSTD 1885 static void * 1886 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from, 1887 QTestState *to) 1888 { 1889 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd"); 1890 } 1891 #endif /* CONFIG_ZSTD */ 1892 1893 static void test_multifd_tcp_none(void) 1894 { 1895 MigrateCommon args = { 1896 .listen_uri = "defer", 1897 .start_hook = test_migrate_precopy_tcp_multifd_start, 1898 }; 1899 test_precopy_common(&args); 1900 } 1901 1902 static void test_multifd_tcp_zlib(void) 1903 { 1904 MigrateCommon args = { 1905 .listen_uri = "defer", 1906 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start, 1907 }; 1908 test_precopy_common(&args); 1909 } 1910 1911 #ifdef CONFIG_ZSTD 1912 static void test_multifd_tcp_zstd(void) 1913 { 1914 MigrateCommon args = { 1915 .listen_uri = "defer", 1916 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start, 1917 }; 1918 test_precopy_common(&args); 1919 } 1920 #endif 1921 1922 #ifdef CONFIG_GNUTLS 1923 static void * 1924 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from, 1925 QTestState *to) 1926 { 1927 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1928 return test_migrate_tls_psk_start_match(from, to); 1929 } 1930 1931 static void * 1932 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from, 1933 QTestState *to) 1934 { 1935 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1936 return test_migrate_tls_psk_start_mismatch(from, to); 1937 } 1938 1939 #ifdef CONFIG_TASN1 1940 static void * 1941 test_migrate_multifd_tls_x509_start_default_host(QTestState *from, 1942 QTestState *to) 1943 { 1944 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1945 return test_migrate_tls_x509_start_default_host(from, to); 1946 } 1947 1948 static void * 1949 test_migrate_multifd_tls_x509_start_override_host(QTestState *from, 1950 QTestState *to) 1951 { 1952 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1953 return test_migrate_tls_x509_start_override_host(from, to); 1954 } 1955 1956 static void * 1957 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from, 1958 QTestState *to) 1959 { 1960 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1961 return test_migrate_tls_x509_start_mismatch_host(from, to); 1962 } 1963 1964 static void * 1965 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from, 1966 QTestState *to) 1967 { 1968 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1969 return test_migrate_tls_x509_start_allow_anon_client(from, to); 1970 } 1971 1972 static void * 1973 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from, 1974 QTestState *to) 1975 { 1976 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1977 return test_migrate_tls_x509_start_reject_anon_client(from, to); 1978 } 1979 #endif /* CONFIG_TASN1 */ 1980 1981 static void test_multifd_tcp_tls_psk_match(void) 1982 { 1983 MigrateCommon args = { 1984 .listen_uri = "defer", 1985 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match, 1986 .finish_hook = test_migrate_tls_psk_finish, 1987 }; 1988 test_precopy_common(&args); 1989 } 1990 1991 static void test_multifd_tcp_tls_psk_mismatch(void) 1992 { 1993 MigrateCommon args = { 1994 .start = { 1995 .hide_stderr = true, 1996 }, 1997 .listen_uri = "defer", 1998 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch, 1999 .finish_hook = test_migrate_tls_psk_finish, 2000 .result = MIG_TEST_FAIL, 2001 }; 2002 test_precopy_common(&args); 2003 } 2004 2005 #ifdef CONFIG_TASN1 2006 static void test_multifd_tcp_tls_x509_default_host(void) 2007 { 2008 MigrateCommon args = { 2009 .listen_uri = "defer", 2010 .start_hook = test_migrate_multifd_tls_x509_start_default_host, 2011 .finish_hook = test_migrate_tls_x509_finish, 2012 }; 2013 test_precopy_common(&args); 2014 } 2015 2016 static void test_multifd_tcp_tls_x509_override_host(void) 2017 { 2018 MigrateCommon args = { 2019 .listen_uri = "defer", 2020 .start_hook = test_migrate_multifd_tls_x509_start_override_host, 2021 .finish_hook = test_migrate_tls_x509_finish, 2022 }; 2023 test_precopy_common(&args); 2024 } 2025 2026 static void test_multifd_tcp_tls_x509_mismatch_host(void) 2027 { 2028 /* 2029 * This has different behaviour to the non-multifd case. 2030 * 2031 * In non-multifd case when client aborts due to mismatched 2032 * cert host, the server has already started trying to load 2033 * migration state, and so it exits with I/O failure. 2034 * 2035 * In multifd case when client aborts due to mismatched 2036 * cert host, the server is still waiting for the other 2037 * multifd connections to arrive so hasn't started trying 2038 * to load migration state, and thus just aborts the migration 2039 * without exiting. 2040 */ 2041 MigrateCommon args = { 2042 .start = { 2043 .hide_stderr = true, 2044 }, 2045 .listen_uri = "defer", 2046 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host, 2047 .finish_hook = test_migrate_tls_x509_finish, 2048 .result = MIG_TEST_FAIL, 2049 }; 2050 test_precopy_common(&args); 2051 } 2052 2053 static void test_multifd_tcp_tls_x509_allow_anon_client(void) 2054 { 2055 MigrateCommon args = { 2056 .listen_uri = "defer", 2057 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client, 2058 .finish_hook = test_migrate_tls_x509_finish, 2059 }; 2060 test_precopy_common(&args); 2061 } 2062 2063 static void test_multifd_tcp_tls_x509_reject_anon_client(void) 2064 { 2065 MigrateCommon args = { 2066 .start = { 2067 .hide_stderr = true, 2068 }, 2069 .listen_uri = "defer", 2070 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client, 2071 .finish_hook = test_migrate_tls_x509_finish, 2072 .result = MIG_TEST_FAIL, 2073 }; 2074 test_precopy_common(&args); 2075 } 2076 #endif /* CONFIG_TASN1 */ 2077 #endif /* CONFIG_GNUTLS */ 2078 2079 /* 2080 * This test does: 2081 * source target 2082 * migrate_incoming 2083 * migrate 2084 * migrate_cancel 2085 * launch another target 2086 * migrate 2087 * 2088 * And see that it works 2089 */ 2090 static void test_multifd_tcp_cancel(void) 2091 { 2092 MigrateStart args = { 2093 .hide_stderr = true, 2094 }; 2095 QTestState *from, *to, *to2; 2096 QDict *rsp; 2097 g_autofree char *uri = NULL; 2098 2099 if (test_migrate_start(&from, &to, "defer", &args)) { 2100 return; 2101 } 2102 2103 migrate_ensure_non_converge(from); 2104 2105 migrate_set_parameter_int(from, "multifd-channels", 16); 2106 migrate_set_parameter_int(to, "multifd-channels", 16); 2107 2108 migrate_set_capability(from, "multifd", true); 2109 migrate_set_capability(to, "multifd", true); 2110 2111 /* Start incoming migration from the 1st socket */ 2112 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 2113 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 2114 qobject_unref(rsp); 2115 2116 /* Wait for the first serial output from the source */ 2117 wait_for_serial("src_serial"); 2118 2119 uri = migrate_get_socket_address(to, "socket-address"); 2120 2121 migrate_qmp(from, uri, "{}"); 2122 2123 wait_for_migration_pass(from); 2124 2125 migrate_cancel(from); 2126 2127 args = (MigrateStart){ 2128 .only_target = true, 2129 }; 2130 2131 if (test_migrate_start(&from, &to2, "defer", &args)) { 2132 return; 2133 } 2134 2135 migrate_set_parameter_int(to2, "multifd-channels", 16); 2136 2137 migrate_set_capability(to2, "multifd", true); 2138 2139 /* Start incoming migration from the 1st socket */ 2140 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming'," 2141 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 2142 qobject_unref(rsp); 2143 2144 g_free(uri); 2145 uri = migrate_get_socket_address(to2, "socket-address"); 2146 2147 wait_for_migration_status(from, "cancelled", NULL); 2148 2149 migrate_ensure_converge(from); 2150 2151 migrate_qmp(from, uri, "{}"); 2152 2153 wait_for_migration_pass(from); 2154 2155 if (!got_stop) { 2156 qtest_qmp_eventwait(from, "STOP"); 2157 } 2158 qtest_qmp_eventwait(to2, "RESUME"); 2159 2160 wait_for_serial("dest_serial"); 2161 wait_for_migration_complete(from); 2162 test_migrate_end(from, to2, true); 2163 } 2164 2165 static void calc_dirty_rate(QTestState *who, uint64_t calc_time) 2166 { 2167 qobject_unref(qmp_command(who, 2168 "{ 'execute': 'calc-dirty-rate'," 2169 "'arguments': { " 2170 "'calc-time': %ld," 2171 "'mode': 'dirty-ring' }}", 2172 calc_time)); 2173 } 2174 2175 static QDict *query_dirty_rate(QTestState *who) 2176 { 2177 return qmp_command(who, "{ 'execute': 'query-dirty-rate' }"); 2178 } 2179 2180 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate) 2181 { 2182 qobject_unref(qmp_command(who, 2183 "{ 'execute': 'set-vcpu-dirty-limit'," 2184 "'arguments': { " 2185 "'dirty-rate': %ld } }", 2186 dirtyrate)); 2187 } 2188 2189 static void cancel_vcpu_dirty_limit(QTestState *who) 2190 { 2191 qobject_unref(qmp_command(who, 2192 "{ 'execute': 'cancel-vcpu-dirty-limit' }")); 2193 } 2194 2195 static QDict *query_vcpu_dirty_limit(QTestState *who) 2196 { 2197 QDict *rsp; 2198 2199 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }"); 2200 g_assert(!qdict_haskey(rsp, "error")); 2201 g_assert(qdict_haskey(rsp, "return")); 2202 2203 return rsp; 2204 } 2205 2206 static bool calc_dirtyrate_ready(QTestState *who) 2207 { 2208 QDict *rsp_return; 2209 gchar *status; 2210 2211 rsp_return = query_dirty_rate(who); 2212 g_assert(rsp_return); 2213 2214 status = g_strdup(qdict_get_str(rsp_return, "status")); 2215 g_assert(status); 2216 2217 return g_strcmp0(status, "measuring"); 2218 } 2219 2220 static void wait_for_calc_dirtyrate_complete(QTestState *who, 2221 int64_t time_s) 2222 { 2223 int max_try_count = 10000; 2224 usleep(time_s * 1000000); 2225 2226 while (!calc_dirtyrate_ready(who) && max_try_count--) { 2227 usleep(1000); 2228 } 2229 2230 /* 2231 * Set the timeout with 10 s(max_try_count * 1000us), 2232 * if dirtyrate measurement not complete, fail test. 2233 */ 2234 g_assert_cmpint(max_try_count, !=, 0); 2235 } 2236 2237 static int64_t get_dirty_rate(QTestState *who) 2238 { 2239 QDict *rsp_return; 2240 gchar *status; 2241 QList *rates; 2242 const QListEntry *entry; 2243 QDict *rate; 2244 int64_t dirtyrate; 2245 2246 rsp_return = query_dirty_rate(who); 2247 g_assert(rsp_return); 2248 2249 status = g_strdup(qdict_get_str(rsp_return, "status")); 2250 g_assert(status); 2251 g_assert_cmpstr(status, ==, "measured"); 2252 2253 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate"); 2254 g_assert(rates && !qlist_empty(rates)); 2255 2256 entry = qlist_first(rates); 2257 g_assert(entry); 2258 2259 rate = qobject_to(QDict, qlist_entry_obj(entry)); 2260 g_assert(rate); 2261 2262 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1); 2263 2264 qobject_unref(rsp_return); 2265 return dirtyrate; 2266 } 2267 2268 static int64_t get_limit_rate(QTestState *who) 2269 { 2270 QDict *rsp_return; 2271 QList *rates; 2272 const QListEntry *entry; 2273 QDict *rate; 2274 int64_t dirtyrate; 2275 2276 rsp_return = query_vcpu_dirty_limit(who); 2277 g_assert(rsp_return); 2278 2279 rates = qdict_get_qlist(rsp_return, "return"); 2280 g_assert(rates && !qlist_empty(rates)); 2281 2282 entry = qlist_first(rates); 2283 g_assert(entry); 2284 2285 rate = qobject_to(QDict, qlist_entry_obj(entry)); 2286 g_assert(rate); 2287 2288 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1); 2289 2290 qobject_unref(rsp_return); 2291 return dirtyrate; 2292 } 2293 2294 static QTestState *dirtylimit_start_vm(void) 2295 { 2296 QTestState *vm = NULL; 2297 g_autofree gchar *cmd = NULL; 2298 const char *arch = qtest_get_arch(); 2299 g_autofree char *bootpath = NULL; 2300 2301 assert((strcmp(arch, "x86_64") == 0)); 2302 bootpath = g_strdup_printf("%s/bootsect", tmpfs); 2303 assert(sizeof(x86_bootsect) == 512); 2304 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect)); 2305 2306 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 " 2307 "-name dirtylimit-test,debug-threads=on " 2308 "-m 150M -smp 1 " 2309 "-serial file:%s/vm_serial " 2310 "-drive file=%s,format=raw ", 2311 tmpfs, bootpath); 2312 2313 vm = qtest_init(cmd); 2314 return vm; 2315 } 2316 2317 static void dirtylimit_stop_vm(QTestState *vm) 2318 { 2319 qtest_quit(vm); 2320 cleanup("bootsect"); 2321 cleanup("vm_serial"); 2322 } 2323 2324 static void test_vcpu_dirty_limit(void) 2325 { 2326 QTestState *vm; 2327 int64_t origin_rate; 2328 int64_t quota_rate; 2329 int64_t rate ; 2330 int max_try_count = 20; 2331 int hit = 0; 2332 2333 /* Start vm for vcpu dirtylimit test */ 2334 vm = dirtylimit_start_vm(); 2335 2336 /* Wait for the first serial output from the vm*/ 2337 wait_for_serial("vm_serial"); 2338 2339 /* Do dirtyrate measurement with calc time equals 1s */ 2340 calc_dirty_rate(vm, 1); 2341 2342 /* Sleep calc time and wait for calc dirtyrate complete */ 2343 wait_for_calc_dirtyrate_complete(vm, 1); 2344 2345 /* Query original dirty page rate */ 2346 origin_rate = get_dirty_rate(vm); 2347 2348 /* VM booted from bootsect should dirty memory steadily */ 2349 assert(origin_rate != 0); 2350 2351 /* Setup quota dirty page rate at half of origin */ 2352 quota_rate = origin_rate / 2; 2353 2354 /* Set dirtylimit */ 2355 dirtylimit_set_all(vm, quota_rate); 2356 2357 /* 2358 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit 2359 * works literally 2360 */ 2361 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm)); 2362 2363 /* Sleep a bit to check if it take effect */ 2364 usleep(2000000); 2365 2366 /* 2367 * Check if dirtylimit take effect realistically, set the 2368 * timeout with 20 s(max_try_count * 1s), if dirtylimit 2369 * doesn't take effect, fail test. 2370 */ 2371 while (--max_try_count) { 2372 calc_dirty_rate(vm, 1); 2373 wait_for_calc_dirtyrate_complete(vm, 1); 2374 rate = get_dirty_rate(vm); 2375 2376 /* 2377 * Assume hitting if current rate is less 2378 * than quota rate (within accepting error) 2379 */ 2380 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 2381 hit = 1; 2382 break; 2383 } 2384 } 2385 2386 g_assert_cmpint(hit, ==, 1); 2387 2388 hit = 0; 2389 max_try_count = 20; 2390 2391 /* Check if dirtylimit cancellation take effect */ 2392 cancel_vcpu_dirty_limit(vm); 2393 while (--max_try_count) { 2394 calc_dirty_rate(vm, 1); 2395 wait_for_calc_dirtyrate_complete(vm, 1); 2396 rate = get_dirty_rate(vm); 2397 2398 /* 2399 * Assume dirtylimit be canceled if current rate is 2400 * greater than quota rate (within accepting error) 2401 */ 2402 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 2403 hit = 1; 2404 break; 2405 } 2406 } 2407 2408 g_assert_cmpint(hit, ==, 1); 2409 dirtylimit_stop_vm(vm); 2410 } 2411 2412 static bool kvm_dirty_ring_supported(void) 2413 { 2414 #if defined(__linux__) && defined(HOST_X86_64) 2415 int ret, kvm_fd = open("/dev/kvm", O_RDONLY); 2416 2417 if (kvm_fd < 0) { 2418 return false; 2419 } 2420 2421 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING); 2422 close(kvm_fd); 2423 2424 /* We test with 4096 slots */ 2425 if (ret < 4096) { 2426 return false; 2427 } 2428 2429 return true; 2430 #else 2431 return false; 2432 #endif 2433 } 2434 2435 int main(int argc, char **argv) 2436 { 2437 char template[] = "/tmp/migration-test-XXXXXX"; 2438 const bool has_kvm = qtest_has_accel("kvm"); 2439 const bool has_uffd = ufd_version_check(); 2440 const char *arch = qtest_get_arch(); 2441 int ret; 2442 2443 g_test_init(&argc, &argv, NULL); 2444 2445 /* 2446 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG 2447 * is touchy due to race conditions on dirty bits (especially on PPC for 2448 * some reason) 2449 */ 2450 if (g_str_equal(arch, "ppc64") && 2451 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) { 2452 g_test_message("Skipping test: kvm_hv not available"); 2453 return g_test_run(); 2454 } 2455 2456 /* 2457 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it 2458 * there until the problems are resolved 2459 */ 2460 if (g_str_equal(arch, "s390x") && !has_kvm) { 2461 g_test_message("Skipping test: s390x host with KVM is required"); 2462 return g_test_run(); 2463 } 2464 2465 tmpfs = g_mkdtemp(template); 2466 if (!tmpfs) { 2467 g_test_message("g_mkdtemp on path (%s): %s", template, strerror(errno)); 2468 } 2469 g_assert(tmpfs); 2470 2471 module_call_init(MODULE_INIT_QOM); 2472 2473 if (has_uffd) { 2474 qtest_add_func("/migration/postcopy/plain", test_postcopy); 2475 qtest_add_func("/migration/postcopy/recovery/plain", 2476 test_postcopy_recovery); 2477 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt); 2478 qtest_add_func("/migration/postcopy/preempt/recovery/plain", 2479 test_postcopy_preempt_recovery); 2480 } 2481 2482 qtest_add_func("/migration/bad_dest", test_baddest); 2483 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain); 2484 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle); 2485 #ifdef CONFIG_GNUTLS 2486 qtest_add_func("/migration/precopy/unix/tls/psk", 2487 test_precopy_unix_tls_psk); 2488 2489 if (has_uffd) { 2490 /* 2491 * NOTE: psk test is enough for postcopy, as other types of TLS 2492 * channels are tested under precopy. Here what we want to test is the 2493 * general postcopy path that has TLS channel enabled. 2494 */ 2495 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk); 2496 qtest_add_func("/migration/postcopy/recovery/tls/psk", 2497 test_postcopy_recovery_tls_psk); 2498 qtest_add_func("/migration/postcopy/preempt/tls/psk", 2499 test_postcopy_preempt_tls_psk); 2500 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk", 2501 test_postcopy_preempt_all); 2502 } 2503 #ifdef CONFIG_TASN1 2504 qtest_add_func("/migration/precopy/unix/tls/x509/default-host", 2505 test_precopy_unix_tls_x509_default_host); 2506 qtest_add_func("/migration/precopy/unix/tls/x509/override-host", 2507 test_precopy_unix_tls_x509_override_host); 2508 #endif /* CONFIG_TASN1 */ 2509 #endif /* CONFIG_GNUTLS */ 2510 2511 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain); 2512 #ifdef CONFIG_GNUTLS 2513 qtest_add_func("/migration/precopy/tcp/tls/psk/match", 2514 test_precopy_tcp_tls_psk_match); 2515 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch", 2516 test_precopy_tcp_tls_psk_mismatch); 2517 #ifdef CONFIG_TASN1 2518 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host", 2519 test_precopy_tcp_tls_x509_default_host); 2520 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host", 2521 test_precopy_tcp_tls_x509_override_host); 2522 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host", 2523 test_precopy_tcp_tls_x509_mismatch_host); 2524 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client", 2525 test_precopy_tcp_tls_x509_friendly_client); 2526 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client", 2527 test_precopy_tcp_tls_x509_hostile_client); 2528 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client", 2529 test_precopy_tcp_tls_x509_allow_anon_client); 2530 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client", 2531 test_precopy_tcp_tls_x509_reject_anon_client); 2532 #endif /* CONFIG_TASN1 */ 2533 #endif /* CONFIG_GNUTLS */ 2534 2535 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ 2536 #ifndef _WIN32 2537 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto); 2538 #endif 2539 qtest_add_func("/migration/validate_uuid", test_validate_uuid); 2540 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error); 2541 qtest_add_func("/migration/validate_uuid_src_not_set", 2542 test_validate_uuid_src_not_set); 2543 qtest_add_func("/migration/validate_uuid_dst_not_set", 2544 test_validate_uuid_dst_not_set); 2545 2546 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge); 2547 qtest_add_func("/migration/multifd/tcp/plain/none", 2548 test_multifd_tcp_none); 2549 qtest_add_func("/migration/multifd/tcp/plain/cancel", 2550 test_multifd_tcp_cancel); 2551 qtest_add_func("/migration/multifd/tcp/plain/zlib", 2552 test_multifd_tcp_zlib); 2553 #ifdef CONFIG_ZSTD 2554 qtest_add_func("/migration/multifd/tcp/plain/zstd", 2555 test_multifd_tcp_zstd); 2556 #endif 2557 #ifdef CONFIG_GNUTLS 2558 qtest_add_func("/migration/multifd/tcp/tls/psk/match", 2559 test_multifd_tcp_tls_psk_match); 2560 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch", 2561 test_multifd_tcp_tls_psk_mismatch); 2562 #ifdef CONFIG_TASN1 2563 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host", 2564 test_multifd_tcp_tls_x509_default_host); 2565 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host", 2566 test_multifd_tcp_tls_x509_override_host); 2567 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host", 2568 test_multifd_tcp_tls_x509_mismatch_host); 2569 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client", 2570 test_multifd_tcp_tls_x509_allow_anon_client); 2571 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client", 2572 test_multifd_tcp_tls_x509_reject_anon_client); 2573 #endif /* CONFIG_TASN1 */ 2574 #endif /* CONFIG_GNUTLS */ 2575 2576 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) { 2577 qtest_add_func("/migration/dirty_ring", 2578 test_precopy_unix_dirty_ring); 2579 qtest_add_func("/migration/vcpu_dirty_limit", 2580 test_vcpu_dirty_limit); 2581 } 2582 2583 ret = g_test_run(); 2584 2585 g_assert_cmpint(ret, ==, 0); 2586 2587 ret = rmdir(tmpfs); 2588 if (ret != 0) { 2589 g_test_message("unable to rmdir: path (%s): %s", 2590 tmpfs, strerror(errno)); 2591 } 2592 2593 return ret; 2594 } 2595