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 static void *test_migrate_fd_start_hook(QTestState *from, 1635 QTestState *to) 1636 { 1637 QDict *rsp; 1638 int ret; 1639 int pair[2]; 1640 1641 /* Create two connected sockets for migration */ 1642 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair); 1643 g_assert_cmpint(ret, ==, 0); 1644 1645 /* Send the 1st socket to the target */ 1646 rsp = wait_command_fd(to, pair[0], 1647 "{ 'execute': 'getfd'," 1648 " 'arguments': { 'fdname': 'fd-mig' }}"); 1649 qobject_unref(rsp); 1650 close(pair[0]); 1651 1652 /* Start incoming migration from the 1st socket */ 1653 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 1654 " 'arguments': { 'uri': 'fd:fd-mig' }}"); 1655 qobject_unref(rsp); 1656 1657 /* Send the 2nd socket to the target */ 1658 rsp = wait_command_fd(from, pair[1], 1659 "{ 'execute': 'getfd'," 1660 " 'arguments': { 'fdname': 'fd-mig' }}"); 1661 qobject_unref(rsp); 1662 close(pair[1]); 1663 1664 return NULL; 1665 } 1666 1667 static void test_migrate_fd_finish_hook(QTestState *from, 1668 QTestState *to, 1669 void *opaque) 1670 { 1671 QDict *rsp; 1672 const char *error_desc; 1673 1674 /* Test closing fds */ 1675 /* We assume, that QEMU removes named fd from its list, 1676 * so this should fail */ 1677 rsp = qtest_qmp(from, "{ 'execute': 'closefd'," 1678 " 'arguments': { 'fdname': 'fd-mig' }}"); 1679 g_assert_true(qdict_haskey(rsp, "error")); 1680 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 1681 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 1682 qobject_unref(rsp); 1683 1684 rsp = qtest_qmp(to, "{ 'execute': 'closefd'," 1685 " 'arguments': { 'fdname': 'fd-mig' }}"); 1686 g_assert_true(qdict_haskey(rsp, "error")); 1687 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 1688 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 1689 qobject_unref(rsp); 1690 } 1691 1692 static void test_migrate_fd_proto(void) 1693 { 1694 MigrateCommon args = { 1695 .listen_uri = "defer", 1696 .connect_uri = "fd:fd-mig", 1697 .start_hook = test_migrate_fd_start_hook, 1698 .finish_hook = test_migrate_fd_finish_hook 1699 }; 1700 test_precopy_common(&args); 1701 } 1702 1703 static void do_test_validate_uuid(MigrateStart *args, bool should_fail) 1704 { 1705 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1706 QTestState *from, *to; 1707 1708 if (test_migrate_start(&from, &to, uri, args)) { 1709 return; 1710 } 1711 1712 /* 1713 * UUID validation is at the begin of migration. So, the main process of 1714 * migration is not interesting for us here. Thus, set huge downtime for 1715 * very fast migration. 1716 */ 1717 migrate_set_parameter_int(from, "downtime-limit", 1000000); 1718 migrate_set_capability(from, "validate-uuid", true); 1719 1720 /* Wait for the first serial output from the source */ 1721 wait_for_serial("src_serial"); 1722 1723 migrate_qmp(from, uri, "{}"); 1724 1725 if (should_fail) { 1726 qtest_set_expected_status(to, 1); 1727 wait_for_migration_fail(from, true); 1728 } else { 1729 wait_for_migration_complete(from); 1730 } 1731 1732 test_migrate_end(from, to, false); 1733 } 1734 1735 static void test_validate_uuid(void) 1736 { 1737 MigrateStart args = { 1738 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1739 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111", 1740 }; 1741 1742 do_test_validate_uuid(&args, false); 1743 } 1744 1745 static void test_validate_uuid_error(void) 1746 { 1747 MigrateStart args = { 1748 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1749 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 1750 .hide_stderr = true, 1751 }; 1752 1753 do_test_validate_uuid(&args, true); 1754 } 1755 1756 static void test_validate_uuid_src_not_set(void) 1757 { 1758 MigrateStart args = { 1759 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 1760 .hide_stderr = true, 1761 }; 1762 1763 do_test_validate_uuid(&args, false); 1764 } 1765 1766 static void test_validate_uuid_dst_not_set(void) 1767 { 1768 MigrateStart args = { 1769 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1770 .hide_stderr = true, 1771 }; 1772 1773 do_test_validate_uuid(&args, false); 1774 } 1775 1776 static void test_migrate_auto_converge(void) 1777 { 1778 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1779 MigrateStart args = {}; 1780 QTestState *from, *to; 1781 int64_t percentage; 1782 1783 /* 1784 * We want the test to be stable and as fast as possible. 1785 * E.g., with 1Gb/s bandwith migration may pass without throttling, 1786 * so we need to decrease a bandwidth. 1787 */ 1788 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95; 1789 1790 if (test_migrate_start(&from, &to, uri, &args)) { 1791 return; 1792 } 1793 1794 migrate_set_capability(from, "auto-converge", true); 1795 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct); 1796 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct); 1797 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct); 1798 1799 /* 1800 * Set the initial parameters so that the migration could not converge 1801 * without throttling. 1802 */ 1803 migrate_ensure_non_converge(from); 1804 1805 /* To check remaining size after precopy */ 1806 migrate_set_capability(from, "pause-before-switchover", true); 1807 1808 /* Wait for the first serial output from the source */ 1809 wait_for_serial("src_serial"); 1810 1811 migrate_qmp(from, uri, "{}"); 1812 1813 /* Wait for throttling begins */ 1814 percentage = 0; 1815 while (percentage == 0) { 1816 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 1817 usleep(100); 1818 g_assert_false(got_stop); 1819 } 1820 /* The first percentage of throttling should be equal to init_pct */ 1821 g_assert_cmpint(percentage, ==, init_pct); 1822 /* Now, when we tested that throttling works, let it converge */ 1823 migrate_ensure_converge(from); 1824 1825 /* 1826 * Wait for pre-switchover status to check last throttle percentage 1827 * and remaining. These values will be zeroed later 1828 */ 1829 wait_for_migration_status(from, "pre-switchover", NULL); 1830 1831 /* The final percentage of throttling shouldn't be greater than max_pct */ 1832 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 1833 g_assert_cmpint(percentage, <=, max_pct); 1834 migrate_continue(from, "pre-switchover"); 1835 1836 qtest_qmp_eventwait(to, "RESUME"); 1837 1838 wait_for_serial("dest_serial"); 1839 wait_for_migration_complete(from); 1840 1841 test_migrate_end(from, to, true); 1842 } 1843 1844 static void * 1845 test_migrate_precopy_tcp_multifd_start_common(QTestState *from, 1846 QTestState *to, 1847 const char *method) 1848 { 1849 QDict *rsp; 1850 1851 migrate_set_parameter_int(from, "multifd-channels", 16); 1852 migrate_set_parameter_int(to, "multifd-channels", 16); 1853 1854 migrate_set_parameter_str(from, "multifd-compression", method); 1855 migrate_set_parameter_str(to, "multifd-compression", method); 1856 1857 migrate_set_capability(from, "multifd", true); 1858 migrate_set_capability(to, "multifd", true); 1859 1860 /* Start incoming migration from the 1st socket */ 1861 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 1862 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 1863 qobject_unref(rsp); 1864 1865 return NULL; 1866 } 1867 1868 static void * 1869 test_migrate_precopy_tcp_multifd_start(QTestState *from, 1870 QTestState *to) 1871 { 1872 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1873 } 1874 1875 static void * 1876 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from, 1877 QTestState *to) 1878 { 1879 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib"); 1880 } 1881 1882 #ifdef CONFIG_ZSTD 1883 static void * 1884 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from, 1885 QTestState *to) 1886 { 1887 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd"); 1888 } 1889 #endif /* CONFIG_ZSTD */ 1890 1891 static void test_multifd_tcp_none(void) 1892 { 1893 MigrateCommon args = { 1894 .listen_uri = "defer", 1895 .start_hook = test_migrate_precopy_tcp_multifd_start, 1896 }; 1897 test_precopy_common(&args); 1898 } 1899 1900 static void test_multifd_tcp_zlib(void) 1901 { 1902 MigrateCommon args = { 1903 .listen_uri = "defer", 1904 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start, 1905 }; 1906 test_precopy_common(&args); 1907 } 1908 1909 #ifdef CONFIG_ZSTD 1910 static void test_multifd_tcp_zstd(void) 1911 { 1912 MigrateCommon args = { 1913 .listen_uri = "defer", 1914 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start, 1915 }; 1916 test_precopy_common(&args); 1917 } 1918 #endif 1919 1920 #ifdef CONFIG_GNUTLS 1921 static void * 1922 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from, 1923 QTestState *to) 1924 { 1925 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1926 return test_migrate_tls_psk_start_match(from, to); 1927 } 1928 1929 static void * 1930 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from, 1931 QTestState *to) 1932 { 1933 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1934 return test_migrate_tls_psk_start_mismatch(from, to); 1935 } 1936 1937 #ifdef CONFIG_TASN1 1938 static void * 1939 test_migrate_multifd_tls_x509_start_default_host(QTestState *from, 1940 QTestState *to) 1941 { 1942 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1943 return test_migrate_tls_x509_start_default_host(from, to); 1944 } 1945 1946 static void * 1947 test_migrate_multifd_tls_x509_start_override_host(QTestState *from, 1948 QTestState *to) 1949 { 1950 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1951 return test_migrate_tls_x509_start_override_host(from, to); 1952 } 1953 1954 static void * 1955 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from, 1956 QTestState *to) 1957 { 1958 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1959 return test_migrate_tls_x509_start_mismatch_host(from, to); 1960 } 1961 1962 static void * 1963 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from, 1964 QTestState *to) 1965 { 1966 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1967 return test_migrate_tls_x509_start_allow_anon_client(from, to); 1968 } 1969 1970 static void * 1971 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from, 1972 QTestState *to) 1973 { 1974 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1975 return test_migrate_tls_x509_start_reject_anon_client(from, to); 1976 } 1977 #endif /* CONFIG_TASN1 */ 1978 1979 static void test_multifd_tcp_tls_psk_match(void) 1980 { 1981 MigrateCommon args = { 1982 .listen_uri = "defer", 1983 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match, 1984 .finish_hook = test_migrate_tls_psk_finish, 1985 }; 1986 test_precopy_common(&args); 1987 } 1988 1989 static void test_multifd_tcp_tls_psk_mismatch(void) 1990 { 1991 MigrateCommon args = { 1992 .start = { 1993 .hide_stderr = true, 1994 }, 1995 .listen_uri = "defer", 1996 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch, 1997 .finish_hook = test_migrate_tls_psk_finish, 1998 .result = MIG_TEST_FAIL, 1999 }; 2000 test_precopy_common(&args); 2001 } 2002 2003 #ifdef CONFIG_TASN1 2004 static void test_multifd_tcp_tls_x509_default_host(void) 2005 { 2006 MigrateCommon args = { 2007 .listen_uri = "defer", 2008 .start_hook = test_migrate_multifd_tls_x509_start_default_host, 2009 .finish_hook = test_migrate_tls_x509_finish, 2010 }; 2011 test_precopy_common(&args); 2012 } 2013 2014 static void test_multifd_tcp_tls_x509_override_host(void) 2015 { 2016 MigrateCommon args = { 2017 .listen_uri = "defer", 2018 .start_hook = test_migrate_multifd_tls_x509_start_override_host, 2019 .finish_hook = test_migrate_tls_x509_finish, 2020 }; 2021 test_precopy_common(&args); 2022 } 2023 2024 static void test_multifd_tcp_tls_x509_mismatch_host(void) 2025 { 2026 /* 2027 * This has different behaviour to the non-multifd case. 2028 * 2029 * In non-multifd case when client aborts due to mismatched 2030 * cert host, the server has already started trying to load 2031 * migration state, and so it exits with I/O failure. 2032 * 2033 * In multifd case when client aborts due to mismatched 2034 * cert host, the server is still waiting for the other 2035 * multifd connections to arrive so hasn't started trying 2036 * to load migration state, and thus just aborts the migration 2037 * without exiting. 2038 */ 2039 MigrateCommon args = { 2040 .start = { 2041 .hide_stderr = true, 2042 }, 2043 .listen_uri = "defer", 2044 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host, 2045 .finish_hook = test_migrate_tls_x509_finish, 2046 .result = MIG_TEST_FAIL, 2047 }; 2048 test_precopy_common(&args); 2049 } 2050 2051 static void test_multifd_tcp_tls_x509_allow_anon_client(void) 2052 { 2053 MigrateCommon args = { 2054 .listen_uri = "defer", 2055 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client, 2056 .finish_hook = test_migrate_tls_x509_finish, 2057 }; 2058 test_precopy_common(&args); 2059 } 2060 2061 static void test_multifd_tcp_tls_x509_reject_anon_client(void) 2062 { 2063 MigrateCommon args = { 2064 .start = { 2065 .hide_stderr = true, 2066 }, 2067 .listen_uri = "defer", 2068 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client, 2069 .finish_hook = test_migrate_tls_x509_finish, 2070 .result = MIG_TEST_FAIL, 2071 }; 2072 test_precopy_common(&args); 2073 } 2074 #endif /* CONFIG_TASN1 */ 2075 #endif /* CONFIG_GNUTLS */ 2076 2077 /* 2078 * This test does: 2079 * source target 2080 * migrate_incoming 2081 * migrate 2082 * migrate_cancel 2083 * launch another target 2084 * migrate 2085 * 2086 * And see that it works 2087 */ 2088 static void test_multifd_tcp_cancel(void) 2089 { 2090 MigrateStart args = { 2091 .hide_stderr = true, 2092 }; 2093 QTestState *from, *to, *to2; 2094 QDict *rsp; 2095 g_autofree char *uri = NULL; 2096 2097 if (test_migrate_start(&from, &to, "defer", &args)) { 2098 return; 2099 } 2100 2101 migrate_ensure_non_converge(from); 2102 2103 migrate_set_parameter_int(from, "multifd-channels", 16); 2104 migrate_set_parameter_int(to, "multifd-channels", 16); 2105 2106 migrate_set_capability(from, "multifd", true); 2107 migrate_set_capability(to, "multifd", true); 2108 2109 /* Start incoming migration from the 1st socket */ 2110 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 2111 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 2112 qobject_unref(rsp); 2113 2114 /* Wait for the first serial output from the source */ 2115 wait_for_serial("src_serial"); 2116 2117 uri = migrate_get_socket_address(to, "socket-address"); 2118 2119 migrate_qmp(from, uri, "{}"); 2120 2121 wait_for_migration_pass(from); 2122 2123 migrate_cancel(from); 2124 2125 args = (MigrateStart){ 2126 .only_target = true, 2127 }; 2128 2129 if (test_migrate_start(&from, &to2, "defer", &args)) { 2130 return; 2131 } 2132 2133 migrate_set_parameter_int(to2, "multifd-channels", 16); 2134 2135 migrate_set_capability(to2, "multifd", true); 2136 2137 /* Start incoming migration from the 1st socket */ 2138 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming'," 2139 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 2140 qobject_unref(rsp); 2141 2142 g_free(uri); 2143 uri = migrate_get_socket_address(to2, "socket-address"); 2144 2145 wait_for_migration_status(from, "cancelled", NULL); 2146 2147 migrate_ensure_converge(from); 2148 2149 migrate_qmp(from, uri, "{}"); 2150 2151 wait_for_migration_pass(from); 2152 2153 if (!got_stop) { 2154 qtest_qmp_eventwait(from, "STOP"); 2155 } 2156 qtest_qmp_eventwait(to2, "RESUME"); 2157 2158 wait_for_serial("dest_serial"); 2159 wait_for_migration_complete(from); 2160 test_migrate_end(from, to2, true); 2161 } 2162 2163 static void calc_dirty_rate(QTestState *who, uint64_t calc_time) 2164 { 2165 qobject_unref(qmp_command(who, 2166 "{ 'execute': 'calc-dirty-rate'," 2167 "'arguments': { " 2168 "'calc-time': %ld," 2169 "'mode': 'dirty-ring' }}", 2170 calc_time)); 2171 } 2172 2173 static QDict *query_dirty_rate(QTestState *who) 2174 { 2175 return qmp_command(who, "{ 'execute': 'query-dirty-rate' }"); 2176 } 2177 2178 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate) 2179 { 2180 qobject_unref(qmp_command(who, 2181 "{ 'execute': 'set-vcpu-dirty-limit'," 2182 "'arguments': { " 2183 "'dirty-rate': %ld } }", 2184 dirtyrate)); 2185 } 2186 2187 static void cancel_vcpu_dirty_limit(QTestState *who) 2188 { 2189 qobject_unref(qmp_command(who, 2190 "{ 'execute': 'cancel-vcpu-dirty-limit' }")); 2191 } 2192 2193 static QDict *query_vcpu_dirty_limit(QTestState *who) 2194 { 2195 QDict *rsp; 2196 2197 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }"); 2198 g_assert(!qdict_haskey(rsp, "error")); 2199 g_assert(qdict_haskey(rsp, "return")); 2200 2201 return rsp; 2202 } 2203 2204 static bool calc_dirtyrate_ready(QTestState *who) 2205 { 2206 QDict *rsp_return; 2207 gchar *status; 2208 2209 rsp_return = query_dirty_rate(who); 2210 g_assert(rsp_return); 2211 2212 status = g_strdup(qdict_get_str(rsp_return, "status")); 2213 g_assert(status); 2214 2215 return g_strcmp0(status, "measuring"); 2216 } 2217 2218 static void wait_for_calc_dirtyrate_complete(QTestState *who, 2219 int64_t time_s) 2220 { 2221 int max_try_count = 10000; 2222 usleep(time_s * 1000000); 2223 2224 while (!calc_dirtyrate_ready(who) && max_try_count--) { 2225 usleep(1000); 2226 } 2227 2228 /* 2229 * Set the timeout with 10 s(max_try_count * 1000us), 2230 * if dirtyrate measurement not complete, fail test. 2231 */ 2232 g_assert_cmpint(max_try_count, !=, 0); 2233 } 2234 2235 static int64_t get_dirty_rate(QTestState *who) 2236 { 2237 QDict *rsp_return; 2238 gchar *status; 2239 QList *rates; 2240 const QListEntry *entry; 2241 QDict *rate; 2242 int64_t dirtyrate; 2243 2244 rsp_return = query_dirty_rate(who); 2245 g_assert(rsp_return); 2246 2247 status = g_strdup(qdict_get_str(rsp_return, "status")); 2248 g_assert(status); 2249 g_assert_cmpstr(status, ==, "measured"); 2250 2251 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate"); 2252 g_assert(rates && !qlist_empty(rates)); 2253 2254 entry = qlist_first(rates); 2255 g_assert(entry); 2256 2257 rate = qobject_to(QDict, qlist_entry_obj(entry)); 2258 g_assert(rate); 2259 2260 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1); 2261 2262 qobject_unref(rsp_return); 2263 return dirtyrate; 2264 } 2265 2266 static int64_t get_limit_rate(QTestState *who) 2267 { 2268 QDict *rsp_return; 2269 QList *rates; 2270 const QListEntry *entry; 2271 QDict *rate; 2272 int64_t dirtyrate; 2273 2274 rsp_return = query_vcpu_dirty_limit(who); 2275 g_assert(rsp_return); 2276 2277 rates = qdict_get_qlist(rsp_return, "return"); 2278 g_assert(rates && !qlist_empty(rates)); 2279 2280 entry = qlist_first(rates); 2281 g_assert(entry); 2282 2283 rate = qobject_to(QDict, qlist_entry_obj(entry)); 2284 g_assert(rate); 2285 2286 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1); 2287 2288 qobject_unref(rsp_return); 2289 return dirtyrate; 2290 } 2291 2292 static QTestState *dirtylimit_start_vm(void) 2293 { 2294 QTestState *vm = NULL; 2295 g_autofree gchar *cmd = NULL; 2296 const char *arch = qtest_get_arch(); 2297 g_autofree char *bootpath = NULL; 2298 2299 assert((strcmp(arch, "x86_64") == 0)); 2300 bootpath = g_strdup_printf("%s/bootsect", tmpfs); 2301 assert(sizeof(x86_bootsect) == 512); 2302 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect)); 2303 2304 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 " 2305 "-name dirtylimit-test,debug-threads=on " 2306 "-m 150M -smp 1 " 2307 "-serial file:%s/vm_serial " 2308 "-drive file=%s,format=raw ", 2309 tmpfs, bootpath); 2310 2311 vm = qtest_init(cmd); 2312 return vm; 2313 } 2314 2315 static void dirtylimit_stop_vm(QTestState *vm) 2316 { 2317 qtest_quit(vm); 2318 cleanup("bootsect"); 2319 cleanup("vm_serial"); 2320 } 2321 2322 static void test_vcpu_dirty_limit(void) 2323 { 2324 QTestState *vm; 2325 int64_t origin_rate; 2326 int64_t quota_rate; 2327 int64_t rate ; 2328 int max_try_count = 20; 2329 int hit = 0; 2330 2331 /* Start vm for vcpu dirtylimit test */ 2332 vm = dirtylimit_start_vm(); 2333 2334 /* Wait for the first serial output from the vm*/ 2335 wait_for_serial("vm_serial"); 2336 2337 /* Do dirtyrate measurement with calc time equals 1s */ 2338 calc_dirty_rate(vm, 1); 2339 2340 /* Sleep calc time and wait for calc dirtyrate complete */ 2341 wait_for_calc_dirtyrate_complete(vm, 1); 2342 2343 /* Query original dirty page rate */ 2344 origin_rate = get_dirty_rate(vm); 2345 2346 /* VM booted from bootsect should dirty memory steadily */ 2347 assert(origin_rate != 0); 2348 2349 /* Setup quota dirty page rate at half of origin */ 2350 quota_rate = origin_rate / 2; 2351 2352 /* Set dirtylimit */ 2353 dirtylimit_set_all(vm, quota_rate); 2354 2355 /* 2356 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit 2357 * works literally 2358 */ 2359 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm)); 2360 2361 /* Sleep a bit to check if it take effect */ 2362 usleep(2000000); 2363 2364 /* 2365 * Check if dirtylimit take effect realistically, set the 2366 * timeout with 20 s(max_try_count * 1s), if dirtylimit 2367 * doesn't take effect, fail test. 2368 */ 2369 while (--max_try_count) { 2370 calc_dirty_rate(vm, 1); 2371 wait_for_calc_dirtyrate_complete(vm, 1); 2372 rate = get_dirty_rate(vm); 2373 2374 /* 2375 * Assume hitting if current rate is less 2376 * than quota rate (within accepting error) 2377 */ 2378 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 2379 hit = 1; 2380 break; 2381 } 2382 } 2383 2384 g_assert_cmpint(hit, ==, 1); 2385 2386 hit = 0; 2387 max_try_count = 20; 2388 2389 /* Check if dirtylimit cancellation take effect */ 2390 cancel_vcpu_dirty_limit(vm); 2391 while (--max_try_count) { 2392 calc_dirty_rate(vm, 1); 2393 wait_for_calc_dirtyrate_complete(vm, 1); 2394 rate = get_dirty_rate(vm); 2395 2396 /* 2397 * Assume dirtylimit be canceled if current rate is 2398 * greater than quota rate (within accepting error) 2399 */ 2400 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 2401 hit = 1; 2402 break; 2403 } 2404 } 2405 2406 g_assert_cmpint(hit, ==, 1); 2407 dirtylimit_stop_vm(vm); 2408 } 2409 2410 static bool kvm_dirty_ring_supported(void) 2411 { 2412 #if defined(__linux__) && defined(HOST_X86_64) 2413 int ret, kvm_fd = open("/dev/kvm", O_RDONLY); 2414 2415 if (kvm_fd < 0) { 2416 return false; 2417 } 2418 2419 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING); 2420 close(kvm_fd); 2421 2422 /* We test with 4096 slots */ 2423 if (ret < 4096) { 2424 return false; 2425 } 2426 2427 return true; 2428 #else 2429 return false; 2430 #endif 2431 } 2432 2433 int main(int argc, char **argv) 2434 { 2435 char template[] = "/tmp/migration-test-XXXXXX"; 2436 const bool has_kvm = qtest_has_accel("kvm"); 2437 const bool has_uffd = ufd_version_check(); 2438 const char *arch = qtest_get_arch(); 2439 int ret; 2440 2441 g_test_init(&argc, &argv, NULL); 2442 2443 /* 2444 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG 2445 * is touchy due to race conditions on dirty bits (especially on PPC for 2446 * some reason) 2447 */ 2448 if (g_str_equal(arch, "ppc64") && 2449 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) { 2450 g_test_message("Skipping test: kvm_hv not available"); 2451 return g_test_run(); 2452 } 2453 2454 /* 2455 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it 2456 * there until the problems are resolved 2457 */ 2458 if (g_str_equal(arch, "s390x") && !has_kvm) { 2459 g_test_message("Skipping test: s390x host with KVM is required"); 2460 return g_test_run(); 2461 } 2462 2463 tmpfs = g_mkdtemp(template); 2464 if (!tmpfs) { 2465 g_test_message("g_mkdtemp on path (%s): %s", template, strerror(errno)); 2466 } 2467 g_assert(tmpfs); 2468 2469 module_call_init(MODULE_INIT_QOM); 2470 2471 if (has_uffd) { 2472 qtest_add_func("/migration/postcopy/plain", test_postcopy); 2473 qtest_add_func("/migration/postcopy/recovery/plain", 2474 test_postcopy_recovery); 2475 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt); 2476 qtest_add_func("/migration/postcopy/preempt/recovery/plain", 2477 test_postcopy_preempt_recovery); 2478 } 2479 2480 qtest_add_func("/migration/bad_dest", test_baddest); 2481 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain); 2482 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle); 2483 #ifdef CONFIG_GNUTLS 2484 qtest_add_func("/migration/precopy/unix/tls/psk", 2485 test_precopy_unix_tls_psk); 2486 2487 if (has_uffd) { 2488 /* 2489 * NOTE: psk test is enough for postcopy, as other types of TLS 2490 * channels are tested under precopy. Here what we want to test is the 2491 * general postcopy path that has TLS channel enabled. 2492 */ 2493 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk); 2494 qtest_add_func("/migration/postcopy/recovery/tls/psk", 2495 test_postcopy_recovery_tls_psk); 2496 qtest_add_func("/migration/postcopy/preempt/tls/psk", 2497 test_postcopy_preempt_tls_psk); 2498 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk", 2499 test_postcopy_preempt_all); 2500 } 2501 #ifdef CONFIG_TASN1 2502 qtest_add_func("/migration/precopy/unix/tls/x509/default-host", 2503 test_precopy_unix_tls_x509_default_host); 2504 qtest_add_func("/migration/precopy/unix/tls/x509/override-host", 2505 test_precopy_unix_tls_x509_override_host); 2506 #endif /* CONFIG_TASN1 */ 2507 #endif /* CONFIG_GNUTLS */ 2508 2509 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain); 2510 #ifdef CONFIG_GNUTLS 2511 qtest_add_func("/migration/precopy/tcp/tls/psk/match", 2512 test_precopy_tcp_tls_psk_match); 2513 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch", 2514 test_precopy_tcp_tls_psk_mismatch); 2515 #ifdef CONFIG_TASN1 2516 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host", 2517 test_precopy_tcp_tls_x509_default_host); 2518 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host", 2519 test_precopy_tcp_tls_x509_override_host); 2520 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host", 2521 test_precopy_tcp_tls_x509_mismatch_host); 2522 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client", 2523 test_precopy_tcp_tls_x509_friendly_client); 2524 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client", 2525 test_precopy_tcp_tls_x509_hostile_client); 2526 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client", 2527 test_precopy_tcp_tls_x509_allow_anon_client); 2528 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client", 2529 test_precopy_tcp_tls_x509_reject_anon_client); 2530 #endif /* CONFIG_TASN1 */ 2531 #endif /* CONFIG_GNUTLS */ 2532 2533 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ 2534 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto); 2535 qtest_add_func("/migration/validate_uuid", test_validate_uuid); 2536 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error); 2537 qtest_add_func("/migration/validate_uuid_src_not_set", 2538 test_validate_uuid_src_not_set); 2539 qtest_add_func("/migration/validate_uuid_dst_not_set", 2540 test_validate_uuid_dst_not_set); 2541 2542 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge); 2543 qtest_add_func("/migration/multifd/tcp/plain/none", 2544 test_multifd_tcp_none); 2545 qtest_add_func("/migration/multifd/tcp/plain/cancel", 2546 test_multifd_tcp_cancel); 2547 qtest_add_func("/migration/multifd/tcp/plain/zlib", 2548 test_multifd_tcp_zlib); 2549 #ifdef CONFIG_ZSTD 2550 qtest_add_func("/migration/multifd/tcp/plain/zstd", 2551 test_multifd_tcp_zstd); 2552 #endif 2553 #ifdef CONFIG_GNUTLS 2554 qtest_add_func("/migration/multifd/tcp/tls/psk/match", 2555 test_multifd_tcp_tls_psk_match); 2556 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch", 2557 test_multifd_tcp_tls_psk_mismatch); 2558 #ifdef CONFIG_TASN1 2559 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host", 2560 test_multifd_tcp_tls_x509_default_host); 2561 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host", 2562 test_multifd_tcp_tls_x509_override_host); 2563 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host", 2564 test_multifd_tcp_tls_x509_mismatch_host); 2565 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client", 2566 test_multifd_tcp_tls_x509_allow_anon_client); 2567 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client", 2568 test_multifd_tcp_tls_x509_reject_anon_client); 2569 #endif /* CONFIG_TASN1 */ 2570 #endif /* CONFIG_GNUTLS */ 2571 2572 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) { 2573 qtest_add_func("/migration/dirty_ring", 2574 test_precopy_unix_dirty_ring); 2575 qtest_add_func("/migration/vcpu_dirty_limit", 2576 test_vcpu_dirty_limit); 2577 } 2578 2579 ret = g_test_run(); 2580 2581 g_assert_cmpint(ret, ==, 0); 2582 2583 ret = rmdir(tmpfs); 2584 if (ret != 0) { 2585 g_test_message("unable to rmdir: path (%s): %s", 2586 tmpfs, strerror(errno)); 2587 } 2588 2589 return ret; 2590 } 2591