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 mkdir(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 mkdir(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 mkdir(data->workdir, 0700); 877 878 test_tls_init(data->keyfile); 879 g_assert(link(data->keyfile, data->serverkey) == 0); 880 if (args->clientcert) { 881 g_assert(link(data->keyfile, data->clientkey) == 0); 882 } 883 884 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert); 885 if (args->clientcert) { 886 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq, 887 args->hostileclient ? 888 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME : 889 QCRYPTO_TLS_TEST_CLIENT_NAME, 890 data->clientcert); 891 } 892 893 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq, 894 data->servercert, 895 args->certhostname, 896 args->certipaddr); 897 898 rsp = wait_command(from, 899 "{ 'execute': 'object-add'," 900 " 'arguments': { 'qom-type': 'tls-creds-x509'," 901 " 'id': 'tlscredsx509client0'," 902 " 'endpoint': 'client'," 903 " 'dir': %s," 904 " 'sanity-check': true," 905 " 'verify-peer': true} }", 906 data->workdir); 907 qobject_unref(rsp); 908 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0"); 909 if (args->certhostname) { 910 migrate_set_parameter_str(from, "tls-hostname", args->certhostname); 911 } 912 913 rsp = wait_command(to, 914 "{ 'execute': 'object-add'," 915 " 'arguments': { 'qom-type': 'tls-creds-x509'," 916 " 'id': 'tlscredsx509server0'," 917 " 'endpoint': 'server'," 918 " 'dir': %s," 919 " 'sanity-check': true," 920 " 'verify-peer': %i} }", 921 data->workdir, args->verifyclient); 922 qobject_unref(rsp); 923 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0"); 924 925 if (args->authzclient) { 926 rsp = wait_command(to, 927 "{ 'execute': 'object-add'," 928 " 'arguments': { 'qom-type': 'authz-simple'," 929 " 'id': 'tlsauthz0'," 930 " 'identity': %s} }", 931 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME); 932 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0"); 933 } 934 935 return data; 936 } 937 938 /* 939 * The normal case: match server's cert hostname against 940 * whatever host we were telling QEMU to connect to (if any) 941 */ 942 static void * 943 test_migrate_tls_x509_start_default_host(QTestState *from, 944 QTestState *to) 945 { 946 TestMigrateTLSX509 args = { 947 .verifyclient = true, 948 .clientcert = true, 949 .certipaddr = "127.0.0.1" 950 }; 951 return test_migrate_tls_x509_start_common(from, to, &args); 952 } 953 954 /* 955 * The unusual case: the server's cert is different from 956 * the address we're telling QEMU to connect to (if any), 957 * so we must give QEMU an explicit hostname to validate 958 */ 959 static void * 960 test_migrate_tls_x509_start_override_host(QTestState *from, 961 QTestState *to) 962 { 963 TestMigrateTLSX509 args = { 964 .verifyclient = true, 965 .clientcert = true, 966 .certhostname = "qemu.org", 967 }; 968 return test_migrate_tls_x509_start_common(from, to, &args); 969 } 970 971 /* 972 * The unusual case: the server's cert is different from 973 * the address we're telling QEMU to connect to, and so we 974 * expect the client to reject the server 975 */ 976 static void * 977 test_migrate_tls_x509_start_mismatch_host(QTestState *from, 978 QTestState *to) 979 { 980 TestMigrateTLSX509 args = { 981 .verifyclient = true, 982 .clientcert = true, 983 .certipaddr = "10.0.0.1", 984 }; 985 return test_migrate_tls_x509_start_common(from, to, &args); 986 } 987 988 static void * 989 test_migrate_tls_x509_start_friendly_client(QTestState *from, 990 QTestState *to) 991 { 992 TestMigrateTLSX509 args = { 993 .verifyclient = true, 994 .clientcert = true, 995 .authzclient = true, 996 .certipaddr = "127.0.0.1", 997 }; 998 return test_migrate_tls_x509_start_common(from, to, &args); 999 } 1000 1001 static void * 1002 test_migrate_tls_x509_start_hostile_client(QTestState *from, 1003 QTestState *to) 1004 { 1005 TestMigrateTLSX509 args = { 1006 .verifyclient = true, 1007 .clientcert = true, 1008 .hostileclient = true, 1009 .authzclient = true, 1010 .certipaddr = "127.0.0.1", 1011 }; 1012 return test_migrate_tls_x509_start_common(from, to, &args); 1013 } 1014 1015 /* 1016 * The case with no client certificate presented, 1017 * and no server verification 1018 */ 1019 static void * 1020 test_migrate_tls_x509_start_allow_anon_client(QTestState *from, 1021 QTestState *to) 1022 { 1023 TestMigrateTLSX509 args = { 1024 .certipaddr = "127.0.0.1", 1025 }; 1026 return test_migrate_tls_x509_start_common(from, to, &args); 1027 } 1028 1029 /* 1030 * The case with no client certificate presented, 1031 * and server verification rejecting 1032 */ 1033 static void * 1034 test_migrate_tls_x509_start_reject_anon_client(QTestState *from, 1035 QTestState *to) 1036 { 1037 TestMigrateTLSX509 args = { 1038 .verifyclient = true, 1039 .certipaddr = "127.0.0.1", 1040 }; 1041 return test_migrate_tls_x509_start_common(from, to, &args); 1042 } 1043 1044 static void 1045 test_migrate_tls_x509_finish(QTestState *from, 1046 QTestState *to, 1047 void *opaque) 1048 { 1049 TestMigrateTLSX509Data *data = opaque; 1050 1051 test_tls_cleanup(data->keyfile); 1052 unlink(data->cacert); 1053 unlink(data->servercert); 1054 unlink(data->serverkey); 1055 unlink(data->clientcert); 1056 unlink(data->clientkey); 1057 rmdir(data->workdir); 1058 1059 g_free(data->workdir); 1060 g_free(data->keyfile); 1061 g_free(data); 1062 } 1063 #endif /* CONFIG_TASN1 */ 1064 #endif /* CONFIG_GNUTLS */ 1065 1066 static int migrate_postcopy_prepare(QTestState **from_ptr, 1067 QTestState **to_ptr, 1068 MigrateCommon *args) 1069 { 1070 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1071 QTestState *from, *to; 1072 1073 if (test_migrate_start(&from, &to, uri, &args->start)) { 1074 return -1; 1075 } 1076 1077 if (args->start_hook) { 1078 args->postcopy_data = args->start_hook(from, to); 1079 } 1080 1081 migrate_set_capability(from, "postcopy-ram", true); 1082 migrate_set_capability(to, "postcopy-ram", true); 1083 migrate_set_capability(to, "postcopy-blocktime", true); 1084 1085 if (args->postcopy_preempt) { 1086 migrate_set_capability(from, "postcopy-preempt", true); 1087 migrate_set_capability(to, "postcopy-preempt", true); 1088 } 1089 1090 migrate_ensure_non_converge(from); 1091 1092 /* Wait for the first serial output from the source */ 1093 wait_for_serial("src_serial"); 1094 1095 migrate_qmp(from, uri, "{}"); 1096 1097 wait_for_migration_pass(from); 1098 1099 *from_ptr = from; 1100 *to_ptr = to; 1101 1102 return 0; 1103 } 1104 1105 static void migrate_postcopy_complete(QTestState *from, QTestState *to, 1106 MigrateCommon *args) 1107 { 1108 wait_for_migration_complete(from); 1109 1110 /* Make sure we get at least one "B" on destination */ 1111 wait_for_serial("dest_serial"); 1112 1113 if (uffd_feature_thread_id) { 1114 read_blocktime(to); 1115 } 1116 1117 if (args->finish_hook) { 1118 args->finish_hook(from, to, args->postcopy_data); 1119 args->postcopy_data = NULL; 1120 } 1121 1122 test_migrate_end(from, to, true); 1123 } 1124 1125 static void test_postcopy_common(MigrateCommon *args) 1126 { 1127 QTestState *from, *to; 1128 1129 if (migrate_postcopy_prepare(&from, &to, args)) { 1130 return; 1131 } 1132 migrate_postcopy_start(from, to); 1133 migrate_postcopy_complete(from, to, args); 1134 } 1135 1136 static void test_postcopy(void) 1137 { 1138 MigrateCommon args = { }; 1139 1140 test_postcopy_common(&args); 1141 } 1142 1143 static void test_postcopy_preempt(void) 1144 { 1145 MigrateCommon args = { 1146 .postcopy_preempt = true, 1147 }; 1148 1149 test_postcopy_common(&args); 1150 } 1151 1152 #ifdef CONFIG_GNUTLS 1153 static void test_postcopy_tls_psk(void) 1154 { 1155 MigrateCommon args = { 1156 .start_hook = test_migrate_tls_psk_start_match, 1157 .finish_hook = test_migrate_tls_psk_finish, 1158 }; 1159 1160 test_postcopy_common(&args); 1161 } 1162 1163 static void test_postcopy_preempt_tls_psk(void) 1164 { 1165 MigrateCommon args = { 1166 .postcopy_preempt = true, 1167 .start_hook = test_migrate_tls_psk_start_match, 1168 .finish_hook = test_migrate_tls_psk_finish, 1169 }; 1170 1171 test_postcopy_common(&args); 1172 } 1173 #endif 1174 1175 static void test_postcopy_recovery_common(MigrateCommon *args) 1176 { 1177 QTestState *from, *to; 1178 g_autofree char *uri = NULL; 1179 1180 /* Always hide errors for postcopy recover tests since they're expected */ 1181 args->start.hide_stderr = true; 1182 1183 if (migrate_postcopy_prepare(&from, &to, args)) { 1184 return; 1185 } 1186 1187 /* Turn postcopy speed down, 4K/s is slow enough on any machines */ 1188 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096); 1189 1190 /* Now we start the postcopy */ 1191 migrate_postcopy_start(from, to); 1192 1193 /* 1194 * Wait until postcopy is really started; we can only run the 1195 * migrate-pause command during a postcopy 1196 */ 1197 wait_for_migration_status(from, "postcopy-active", NULL); 1198 1199 /* 1200 * Manually stop the postcopy migration. This emulates a network 1201 * failure with the migration socket 1202 */ 1203 migrate_pause(from); 1204 1205 /* 1206 * Wait for destination side to reach postcopy-paused state. The 1207 * migrate-recover command can only succeed if destination machine 1208 * is in the paused state 1209 */ 1210 wait_for_migration_status(to, "postcopy-paused", 1211 (const char * []) { "failed", "active", 1212 "completed", NULL }); 1213 1214 /* 1215 * Create a new socket to emulate a new channel that is different 1216 * from the broken migration channel; tell the destination to 1217 * listen to the new port 1218 */ 1219 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs); 1220 migrate_recover(to, uri); 1221 1222 /* 1223 * Try to rebuild the migration channel using the resume flag and 1224 * the newly created channel 1225 */ 1226 wait_for_migration_status(from, "postcopy-paused", 1227 (const char * []) { "failed", "active", 1228 "completed", NULL }); 1229 migrate_qmp(from, uri, "{'resume': true}"); 1230 1231 /* Restore the postcopy bandwidth to unlimited */ 1232 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0); 1233 1234 migrate_postcopy_complete(from, to, args); 1235 } 1236 1237 static void test_postcopy_recovery(void) 1238 { 1239 MigrateCommon args = { }; 1240 1241 test_postcopy_recovery_common(&args); 1242 } 1243 1244 #ifdef CONFIG_GNUTLS 1245 static void test_postcopy_recovery_tls_psk(void) 1246 { 1247 MigrateCommon args = { 1248 .start_hook = test_migrate_tls_psk_start_match, 1249 .finish_hook = test_migrate_tls_psk_finish, 1250 }; 1251 1252 test_postcopy_recovery_common(&args); 1253 } 1254 #endif 1255 1256 static void test_postcopy_preempt_recovery(void) 1257 { 1258 MigrateCommon args = { 1259 .postcopy_preempt = true, 1260 }; 1261 1262 test_postcopy_recovery_common(&args); 1263 } 1264 1265 #ifdef CONFIG_GNUTLS 1266 /* This contains preempt+recovery+tls test altogether */ 1267 static void test_postcopy_preempt_all(void) 1268 { 1269 MigrateCommon args = { 1270 .postcopy_preempt = true, 1271 .start_hook = test_migrate_tls_psk_start_match, 1272 .finish_hook = test_migrate_tls_psk_finish, 1273 }; 1274 1275 test_postcopy_recovery_common(&args); 1276 } 1277 #endif 1278 1279 static void test_baddest(void) 1280 { 1281 MigrateStart args = { 1282 .hide_stderr = true 1283 }; 1284 QTestState *from, *to; 1285 1286 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) { 1287 return; 1288 } 1289 migrate_qmp(from, "tcp:127.0.0.1:0", "{}"); 1290 wait_for_migration_fail(from, false); 1291 test_migrate_end(from, to, false); 1292 } 1293 1294 static void test_precopy_common(MigrateCommon *args) 1295 { 1296 QTestState *from, *to; 1297 void *data_hook = NULL; 1298 1299 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) { 1300 return; 1301 } 1302 1303 migrate_ensure_non_converge(from); 1304 1305 if (args->start_hook) { 1306 data_hook = args->start_hook(from, to); 1307 } 1308 1309 /* Wait for the first serial output from the source */ 1310 wait_for_serial("src_serial"); 1311 1312 if (!args->connect_uri) { 1313 g_autofree char *local_connect_uri = 1314 migrate_get_socket_address(to, "socket-address"); 1315 migrate_qmp(from, local_connect_uri, "{}"); 1316 } else { 1317 migrate_qmp(from, args->connect_uri, "{}"); 1318 } 1319 1320 1321 if (args->result != MIG_TEST_SUCCEED) { 1322 bool allow_active = args->result == MIG_TEST_FAIL; 1323 wait_for_migration_fail(from, allow_active); 1324 1325 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) { 1326 qtest_set_expected_status(to, 1); 1327 } 1328 } else { 1329 if (args->iterations) { 1330 while (args->iterations--) { 1331 wait_for_migration_pass(from); 1332 } 1333 } else { 1334 wait_for_migration_pass(from); 1335 } 1336 1337 migrate_ensure_converge(from); 1338 1339 /* We do this first, as it has a timeout to stop us 1340 * hanging forever if migration didn't converge */ 1341 wait_for_migration_complete(from); 1342 1343 if (!got_stop) { 1344 qtest_qmp_eventwait(from, "STOP"); 1345 } 1346 1347 qtest_qmp_eventwait(to, "RESUME"); 1348 1349 wait_for_serial("dest_serial"); 1350 } 1351 1352 if (args->finish_hook) { 1353 args->finish_hook(from, to, data_hook); 1354 } 1355 1356 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED); 1357 } 1358 1359 static void test_precopy_unix_plain(void) 1360 { 1361 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1362 MigrateCommon args = { 1363 .listen_uri = uri, 1364 .connect_uri = uri, 1365 }; 1366 1367 test_precopy_common(&args); 1368 } 1369 1370 1371 static void test_precopy_unix_dirty_ring(void) 1372 { 1373 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1374 MigrateCommon args = { 1375 .start = { 1376 .use_dirty_ring = true, 1377 }, 1378 .listen_uri = uri, 1379 .connect_uri = uri, 1380 }; 1381 1382 test_precopy_common(&args); 1383 } 1384 1385 #ifdef CONFIG_GNUTLS 1386 static void test_precopy_unix_tls_psk(void) 1387 { 1388 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1389 MigrateCommon args = { 1390 .connect_uri = uri, 1391 .listen_uri = uri, 1392 .start_hook = test_migrate_tls_psk_start_match, 1393 .finish_hook = test_migrate_tls_psk_finish, 1394 }; 1395 1396 test_precopy_common(&args); 1397 } 1398 1399 #ifdef CONFIG_TASN1 1400 static void test_precopy_unix_tls_x509_default_host(void) 1401 { 1402 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1403 MigrateCommon args = { 1404 .start = { 1405 .hide_stderr = true, 1406 }, 1407 .connect_uri = uri, 1408 .listen_uri = uri, 1409 .start_hook = test_migrate_tls_x509_start_default_host, 1410 .finish_hook = test_migrate_tls_x509_finish, 1411 .result = MIG_TEST_FAIL_DEST_QUIT_ERR, 1412 }; 1413 1414 test_precopy_common(&args); 1415 } 1416 1417 static void test_precopy_unix_tls_x509_override_host(void) 1418 { 1419 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1420 MigrateCommon args = { 1421 .connect_uri = uri, 1422 .listen_uri = uri, 1423 .start_hook = test_migrate_tls_x509_start_override_host, 1424 .finish_hook = test_migrate_tls_x509_finish, 1425 }; 1426 1427 test_precopy_common(&args); 1428 } 1429 #endif /* CONFIG_TASN1 */ 1430 #endif /* CONFIG_GNUTLS */ 1431 1432 #if 0 1433 /* Currently upset on aarch64 TCG */ 1434 static void test_ignore_shared(void) 1435 { 1436 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1437 QTestState *from, *to; 1438 1439 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) { 1440 return; 1441 } 1442 1443 migrate_set_capability(from, "x-ignore-shared", true); 1444 migrate_set_capability(to, "x-ignore-shared", true); 1445 1446 /* Wait for the first serial output from the source */ 1447 wait_for_serial("src_serial"); 1448 1449 migrate_qmp(from, uri, "{}"); 1450 1451 wait_for_migration_pass(from); 1452 1453 if (!got_stop) { 1454 qtest_qmp_eventwait(from, "STOP"); 1455 } 1456 1457 qtest_qmp_eventwait(to, "RESUME"); 1458 1459 wait_for_serial("dest_serial"); 1460 wait_for_migration_complete(from); 1461 1462 /* Check whether shared RAM has been really skipped */ 1463 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024); 1464 1465 test_migrate_end(from, to, true); 1466 } 1467 #endif 1468 1469 static void * 1470 test_migrate_xbzrle_start(QTestState *from, 1471 QTestState *to) 1472 { 1473 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432); 1474 1475 migrate_set_capability(from, "xbzrle", true); 1476 migrate_set_capability(to, "xbzrle", true); 1477 1478 return NULL; 1479 } 1480 1481 static void test_precopy_unix_xbzrle(void) 1482 { 1483 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1484 MigrateCommon args = { 1485 .connect_uri = uri, 1486 .listen_uri = uri, 1487 1488 .start_hook = test_migrate_xbzrle_start, 1489 1490 .iterations = 2, 1491 }; 1492 1493 test_precopy_common(&args); 1494 } 1495 1496 static void test_precopy_tcp_plain(void) 1497 { 1498 MigrateCommon args = { 1499 .listen_uri = "tcp:127.0.0.1:0", 1500 }; 1501 1502 test_precopy_common(&args); 1503 } 1504 1505 #ifdef CONFIG_GNUTLS 1506 static void test_precopy_tcp_tls_psk_match(void) 1507 { 1508 MigrateCommon args = { 1509 .listen_uri = "tcp:127.0.0.1:0", 1510 .start_hook = test_migrate_tls_psk_start_match, 1511 .finish_hook = test_migrate_tls_psk_finish, 1512 }; 1513 1514 test_precopy_common(&args); 1515 } 1516 1517 static void test_precopy_tcp_tls_psk_mismatch(void) 1518 { 1519 MigrateCommon args = { 1520 .start = { 1521 .hide_stderr = true, 1522 }, 1523 .listen_uri = "tcp:127.0.0.1:0", 1524 .start_hook = test_migrate_tls_psk_start_mismatch, 1525 .finish_hook = test_migrate_tls_psk_finish, 1526 .result = MIG_TEST_FAIL, 1527 }; 1528 1529 test_precopy_common(&args); 1530 } 1531 1532 #ifdef CONFIG_TASN1 1533 static void test_precopy_tcp_tls_x509_default_host(void) 1534 { 1535 MigrateCommon args = { 1536 .listen_uri = "tcp:127.0.0.1:0", 1537 .start_hook = test_migrate_tls_x509_start_default_host, 1538 .finish_hook = test_migrate_tls_x509_finish, 1539 }; 1540 1541 test_precopy_common(&args); 1542 } 1543 1544 static void test_precopy_tcp_tls_x509_override_host(void) 1545 { 1546 MigrateCommon args = { 1547 .listen_uri = "tcp:127.0.0.1:0", 1548 .start_hook = test_migrate_tls_x509_start_override_host, 1549 .finish_hook = test_migrate_tls_x509_finish, 1550 }; 1551 1552 test_precopy_common(&args); 1553 } 1554 1555 static void test_precopy_tcp_tls_x509_mismatch_host(void) 1556 { 1557 MigrateCommon args = { 1558 .start = { 1559 .hide_stderr = true, 1560 }, 1561 .listen_uri = "tcp:127.0.0.1:0", 1562 .start_hook = test_migrate_tls_x509_start_mismatch_host, 1563 .finish_hook = test_migrate_tls_x509_finish, 1564 .result = MIG_TEST_FAIL_DEST_QUIT_ERR, 1565 }; 1566 1567 test_precopy_common(&args); 1568 } 1569 1570 static void test_precopy_tcp_tls_x509_friendly_client(void) 1571 { 1572 MigrateCommon args = { 1573 .listen_uri = "tcp:127.0.0.1:0", 1574 .start_hook = test_migrate_tls_x509_start_friendly_client, 1575 .finish_hook = test_migrate_tls_x509_finish, 1576 }; 1577 1578 test_precopy_common(&args); 1579 } 1580 1581 static void test_precopy_tcp_tls_x509_hostile_client(void) 1582 { 1583 MigrateCommon args = { 1584 .start = { 1585 .hide_stderr = true, 1586 }, 1587 .listen_uri = "tcp:127.0.0.1:0", 1588 .start_hook = test_migrate_tls_x509_start_hostile_client, 1589 .finish_hook = test_migrate_tls_x509_finish, 1590 .result = MIG_TEST_FAIL, 1591 }; 1592 1593 test_precopy_common(&args); 1594 } 1595 1596 static void test_precopy_tcp_tls_x509_allow_anon_client(void) 1597 { 1598 MigrateCommon args = { 1599 .listen_uri = "tcp:127.0.0.1:0", 1600 .start_hook = test_migrate_tls_x509_start_allow_anon_client, 1601 .finish_hook = test_migrate_tls_x509_finish, 1602 }; 1603 1604 test_precopy_common(&args); 1605 } 1606 1607 static void test_precopy_tcp_tls_x509_reject_anon_client(void) 1608 { 1609 MigrateCommon args = { 1610 .start = { 1611 .hide_stderr = true, 1612 }, 1613 .listen_uri = "tcp:127.0.0.1:0", 1614 .start_hook = test_migrate_tls_x509_start_reject_anon_client, 1615 .finish_hook = test_migrate_tls_x509_finish, 1616 .result = MIG_TEST_FAIL, 1617 }; 1618 1619 test_precopy_common(&args); 1620 } 1621 #endif /* CONFIG_TASN1 */ 1622 #endif /* CONFIG_GNUTLS */ 1623 1624 static void *test_migrate_fd_start_hook(QTestState *from, 1625 QTestState *to) 1626 { 1627 QDict *rsp; 1628 int ret; 1629 int pair[2]; 1630 1631 /* Create two connected sockets for migration */ 1632 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair); 1633 g_assert_cmpint(ret, ==, 0); 1634 1635 /* Send the 1st socket to the target */ 1636 rsp = wait_command_fd(to, pair[0], 1637 "{ 'execute': 'getfd'," 1638 " 'arguments': { 'fdname': 'fd-mig' }}"); 1639 qobject_unref(rsp); 1640 close(pair[0]); 1641 1642 /* Start incoming migration from the 1st socket */ 1643 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 1644 " 'arguments': { 'uri': 'fd:fd-mig' }}"); 1645 qobject_unref(rsp); 1646 1647 /* Send the 2nd socket to the target */ 1648 rsp = wait_command_fd(from, pair[1], 1649 "{ 'execute': 'getfd'," 1650 " 'arguments': { 'fdname': 'fd-mig' }}"); 1651 qobject_unref(rsp); 1652 close(pair[1]); 1653 1654 return NULL; 1655 } 1656 1657 static void test_migrate_fd_finish_hook(QTestState *from, 1658 QTestState *to, 1659 void *opaque) 1660 { 1661 QDict *rsp; 1662 const char *error_desc; 1663 1664 /* Test closing fds */ 1665 /* We assume, that QEMU removes named fd from its list, 1666 * so this should fail */ 1667 rsp = qtest_qmp(from, "{ 'execute': 'closefd'," 1668 " 'arguments': { 'fdname': 'fd-mig' }}"); 1669 g_assert_true(qdict_haskey(rsp, "error")); 1670 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 1671 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 1672 qobject_unref(rsp); 1673 1674 rsp = qtest_qmp(to, "{ 'execute': 'closefd'," 1675 " 'arguments': { 'fdname': 'fd-mig' }}"); 1676 g_assert_true(qdict_haskey(rsp, "error")); 1677 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc"); 1678 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found"); 1679 qobject_unref(rsp); 1680 } 1681 1682 static void test_migrate_fd_proto(void) 1683 { 1684 MigrateCommon args = { 1685 .listen_uri = "defer", 1686 .connect_uri = "fd:fd-mig", 1687 .start_hook = test_migrate_fd_start_hook, 1688 .finish_hook = test_migrate_fd_finish_hook 1689 }; 1690 test_precopy_common(&args); 1691 } 1692 1693 static void do_test_validate_uuid(MigrateStart *args, bool should_fail) 1694 { 1695 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1696 QTestState *from, *to; 1697 1698 if (test_migrate_start(&from, &to, uri, args)) { 1699 return; 1700 } 1701 1702 /* 1703 * UUID validation is at the begin of migration. So, the main process of 1704 * migration is not interesting for us here. Thus, set huge downtime for 1705 * very fast migration. 1706 */ 1707 migrate_set_parameter_int(from, "downtime-limit", 1000000); 1708 migrate_set_capability(from, "validate-uuid", true); 1709 1710 /* Wait for the first serial output from the source */ 1711 wait_for_serial("src_serial"); 1712 1713 migrate_qmp(from, uri, "{}"); 1714 1715 if (should_fail) { 1716 qtest_set_expected_status(to, 1); 1717 wait_for_migration_fail(from, true); 1718 } else { 1719 wait_for_migration_complete(from); 1720 } 1721 1722 test_migrate_end(from, to, false); 1723 } 1724 1725 static void test_validate_uuid(void) 1726 { 1727 MigrateStart args = { 1728 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1729 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111", 1730 }; 1731 1732 do_test_validate_uuid(&args, false); 1733 } 1734 1735 static void test_validate_uuid_error(void) 1736 { 1737 MigrateStart args = { 1738 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1739 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 1740 .hide_stderr = true, 1741 }; 1742 1743 do_test_validate_uuid(&args, true); 1744 } 1745 1746 static void test_validate_uuid_src_not_set(void) 1747 { 1748 MigrateStart args = { 1749 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222", 1750 .hide_stderr = true, 1751 }; 1752 1753 do_test_validate_uuid(&args, false); 1754 } 1755 1756 static void test_validate_uuid_dst_not_set(void) 1757 { 1758 MigrateStart args = { 1759 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111", 1760 .hide_stderr = true, 1761 }; 1762 1763 do_test_validate_uuid(&args, false); 1764 } 1765 1766 static void test_migrate_auto_converge(void) 1767 { 1768 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs); 1769 MigrateStart args = {}; 1770 QTestState *from, *to; 1771 int64_t remaining, percentage; 1772 1773 /* 1774 * We want the test to be stable and as fast as possible. 1775 * E.g., with 1Gb/s bandwith migration may pass without throttling, 1776 * so we need to decrease a bandwidth. 1777 */ 1778 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95; 1779 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */ 1780 const int64_t downtime_limit = 250; /* 250ms */ 1781 /* 1782 * We migrate through unix-socket (> 500Mb/s). 1783 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s). 1784 * So, we can predict expected_threshold 1785 */ 1786 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000; 1787 1788 if (test_migrate_start(&from, &to, uri, &args)) { 1789 return; 1790 } 1791 1792 migrate_set_capability(from, "auto-converge", true); 1793 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct); 1794 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct); 1795 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct); 1796 1797 /* 1798 * Set the initial parameters so that the migration could not converge 1799 * without throttling. 1800 */ 1801 migrate_ensure_non_converge(from); 1802 1803 /* To check remaining size after precopy */ 1804 migrate_set_capability(from, "pause-before-switchover", true); 1805 1806 /* Wait for the first serial output from the source */ 1807 wait_for_serial("src_serial"); 1808 1809 migrate_qmp(from, uri, "{}"); 1810 1811 /* Wait for throttling begins */ 1812 percentage = 0; 1813 while (percentage == 0) { 1814 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 1815 usleep(100); 1816 g_assert_false(got_stop); 1817 } 1818 /* The first percentage of throttling should be equal to init_pct */ 1819 g_assert_cmpint(percentage, ==, init_pct); 1820 /* Now, when we tested that throttling works, let it converge */ 1821 migrate_set_parameter_int(from, "downtime-limit", downtime_limit); 1822 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth); 1823 1824 /* 1825 * Wait for pre-switchover status to check last throttle percentage 1826 * and remaining. These values will be zeroed later 1827 */ 1828 wait_for_migration_status(from, "pre-switchover", NULL); 1829 1830 /* The final percentage of throttling shouldn't be greater than max_pct */ 1831 percentage = read_migrate_property_int(from, "cpu-throttle-percentage"); 1832 g_assert_cmpint(percentage, <=, max_pct); 1833 1834 remaining = read_ram_property_int(from, "remaining"); 1835 g_assert_cmpint(remaining, <, 1836 (expected_threshold + expected_threshold / 100)); 1837 1838 migrate_continue(from, "pre-switchover"); 1839 1840 qtest_qmp_eventwait(to, "RESUME"); 1841 1842 wait_for_serial("dest_serial"); 1843 wait_for_migration_complete(from); 1844 1845 1846 test_migrate_end(from, to, true); 1847 } 1848 1849 static void * 1850 test_migrate_precopy_tcp_multifd_start_common(QTestState *from, 1851 QTestState *to, 1852 const char *method) 1853 { 1854 QDict *rsp; 1855 1856 migrate_set_parameter_int(from, "multifd-channels", 16); 1857 migrate_set_parameter_int(to, "multifd-channels", 16); 1858 1859 migrate_set_parameter_str(from, "multifd-compression", method); 1860 migrate_set_parameter_str(to, "multifd-compression", method); 1861 1862 migrate_set_capability(from, "multifd", true); 1863 migrate_set_capability(to, "multifd", true); 1864 1865 /* Start incoming migration from the 1st socket */ 1866 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 1867 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 1868 qobject_unref(rsp); 1869 1870 return NULL; 1871 } 1872 1873 static void * 1874 test_migrate_precopy_tcp_multifd_start(QTestState *from, 1875 QTestState *to) 1876 { 1877 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1878 } 1879 1880 static void * 1881 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from, 1882 QTestState *to) 1883 { 1884 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib"); 1885 } 1886 1887 #ifdef CONFIG_ZSTD 1888 static void * 1889 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from, 1890 QTestState *to) 1891 { 1892 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd"); 1893 } 1894 #endif /* CONFIG_ZSTD */ 1895 1896 static void test_multifd_tcp_none(void) 1897 { 1898 MigrateCommon args = { 1899 .listen_uri = "defer", 1900 .start_hook = test_migrate_precopy_tcp_multifd_start, 1901 }; 1902 test_precopy_common(&args); 1903 } 1904 1905 static void test_multifd_tcp_zlib(void) 1906 { 1907 MigrateCommon args = { 1908 .listen_uri = "defer", 1909 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start, 1910 }; 1911 test_precopy_common(&args); 1912 } 1913 1914 #ifdef CONFIG_ZSTD 1915 static void test_multifd_tcp_zstd(void) 1916 { 1917 MigrateCommon args = { 1918 .listen_uri = "defer", 1919 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start, 1920 }; 1921 test_precopy_common(&args); 1922 } 1923 #endif 1924 1925 #ifdef CONFIG_GNUTLS 1926 static void * 1927 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from, 1928 QTestState *to) 1929 { 1930 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1931 return test_migrate_tls_psk_start_match(from, to); 1932 } 1933 1934 static void * 1935 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from, 1936 QTestState *to) 1937 { 1938 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1939 return test_migrate_tls_psk_start_mismatch(from, to); 1940 } 1941 1942 #ifdef CONFIG_TASN1 1943 static void * 1944 test_migrate_multifd_tls_x509_start_default_host(QTestState *from, 1945 QTestState *to) 1946 { 1947 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1948 return test_migrate_tls_x509_start_default_host(from, to); 1949 } 1950 1951 static void * 1952 test_migrate_multifd_tls_x509_start_override_host(QTestState *from, 1953 QTestState *to) 1954 { 1955 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1956 return test_migrate_tls_x509_start_override_host(from, to); 1957 } 1958 1959 static void * 1960 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from, 1961 QTestState *to) 1962 { 1963 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1964 return test_migrate_tls_x509_start_mismatch_host(from, to); 1965 } 1966 1967 static void * 1968 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from, 1969 QTestState *to) 1970 { 1971 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1972 return test_migrate_tls_x509_start_allow_anon_client(from, to); 1973 } 1974 1975 static void * 1976 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from, 1977 QTestState *to) 1978 { 1979 test_migrate_precopy_tcp_multifd_start_common(from, to, "none"); 1980 return test_migrate_tls_x509_start_reject_anon_client(from, to); 1981 } 1982 #endif /* CONFIG_TASN1 */ 1983 1984 static void test_multifd_tcp_tls_psk_match(void) 1985 { 1986 MigrateCommon args = { 1987 .listen_uri = "defer", 1988 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match, 1989 .finish_hook = test_migrate_tls_psk_finish, 1990 }; 1991 test_precopy_common(&args); 1992 } 1993 1994 static void test_multifd_tcp_tls_psk_mismatch(void) 1995 { 1996 MigrateCommon args = { 1997 .start = { 1998 .hide_stderr = true, 1999 }, 2000 .listen_uri = "defer", 2001 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch, 2002 .finish_hook = test_migrate_tls_psk_finish, 2003 .result = MIG_TEST_FAIL, 2004 }; 2005 test_precopy_common(&args); 2006 } 2007 2008 #ifdef CONFIG_TASN1 2009 static void test_multifd_tcp_tls_x509_default_host(void) 2010 { 2011 MigrateCommon args = { 2012 .listen_uri = "defer", 2013 .start_hook = test_migrate_multifd_tls_x509_start_default_host, 2014 .finish_hook = test_migrate_tls_x509_finish, 2015 }; 2016 test_precopy_common(&args); 2017 } 2018 2019 static void test_multifd_tcp_tls_x509_override_host(void) 2020 { 2021 MigrateCommon args = { 2022 .listen_uri = "defer", 2023 .start_hook = test_migrate_multifd_tls_x509_start_override_host, 2024 .finish_hook = test_migrate_tls_x509_finish, 2025 }; 2026 test_precopy_common(&args); 2027 } 2028 2029 static void test_multifd_tcp_tls_x509_mismatch_host(void) 2030 { 2031 /* 2032 * This has different behaviour to the non-multifd case. 2033 * 2034 * In non-multifd case when client aborts due to mismatched 2035 * cert host, the server has already started trying to load 2036 * migration state, and so it exits with I/O failure. 2037 * 2038 * In multifd case when client aborts due to mismatched 2039 * cert host, the server is still waiting for the other 2040 * multifd connections to arrive so hasn't started trying 2041 * to load migration state, and thus just aborts the migration 2042 * without exiting. 2043 */ 2044 MigrateCommon args = { 2045 .start = { 2046 .hide_stderr = true, 2047 }, 2048 .listen_uri = "defer", 2049 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host, 2050 .finish_hook = test_migrate_tls_x509_finish, 2051 .result = MIG_TEST_FAIL, 2052 }; 2053 test_precopy_common(&args); 2054 } 2055 2056 static void test_multifd_tcp_tls_x509_allow_anon_client(void) 2057 { 2058 MigrateCommon args = { 2059 .listen_uri = "defer", 2060 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client, 2061 .finish_hook = test_migrate_tls_x509_finish, 2062 }; 2063 test_precopy_common(&args); 2064 } 2065 2066 static void test_multifd_tcp_tls_x509_reject_anon_client(void) 2067 { 2068 MigrateCommon args = { 2069 .start = { 2070 .hide_stderr = true, 2071 }, 2072 .listen_uri = "defer", 2073 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client, 2074 .finish_hook = test_migrate_tls_x509_finish, 2075 .result = MIG_TEST_FAIL, 2076 }; 2077 test_precopy_common(&args); 2078 } 2079 #endif /* CONFIG_TASN1 */ 2080 #endif /* CONFIG_GNUTLS */ 2081 2082 /* 2083 * This test does: 2084 * source target 2085 * migrate_incoming 2086 * migrate 2087 * migrate_cancel 2088 * launch another target 2089 * migrate 2090 * 2091 * And see that it works 2092 */ 2093 static void test_multifd_tcp_cancel(void) 2094 { 2095 MigrateStart args = { 2096 .hide_stderr = true, 2097 }; 2098 QTestState *from, *to, *to2; 2099 QDict *rsp; 2100 g_autofree char *uri = NULL; 2101 2102 if (test_migrate_start(&from, &to, "defer", &args)) { 2103 return; 2104 } 2105 2106 migrate_ensure_non_converge(from); 2107 2108 migrate_set_parameter_int(from, "multifd-channels", 16); 2109 migrate_set_parameter_int(to, "multifd-channels", 16); 2110 2111 migrate_set_capability(from, "multifd", true); 2112 migrate_set_capability(to, "multifd", true); 2113 2114 /* Start incoming migration from the 1st socket */ 2115 rsp = wait_command(to, "{ 'execute': 'migrate-incoming'," 2116 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 2117 qobject_unref(rsp); 2118 2119 /* Wait for the first serial output from the source */ 2120 wait_for_serial("src_serial"); 2121 2122 uri = migrate_get_socket_address(to, "socket-address"); 2123 2124 migrate_qmp(from, uri, "{}"); 2125 2126 wait_for_migration_pass(from); 2127 2128 migrate_cancel(from); 2129 2130 args = (MigrateStart){ 2131 .only_target = true, 2132 }; 2133 2134 if (test_migrate_start(&from, &to2, "defer", &args)) { 2135 return; 2136 } 2137 2138 migrate_set_parameter_int(to2, "multifd-channels", 16); 2139 2140 migrate_set_capability(to2, "multifd", true); 2141 2142 /* Start incoming migration from the 1st socket */ 2143 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming'," 2144 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}"); 2145 qobject_unref(rsp); 2146 2147 g_free(uri); 2148 uri = migrate_get_socket_address(to2, "socket-address"); 2149 2150 wait_for_migration_status(from, "cancelled", NULL); 2151 2152 migrate_ensure_converge(from); 2153 2154 migrate_qmp(from, uri, "{}"); 2155 2156 wait_for_migration_pass(from); 2157 2158 if (!got_stop) { 2159 qtest_qmp_eventwait(from, "STOP"); 2160 } 2161 qtest_qmp_eventwait(to2, "RESUME"); 2162 2163 wait_for_serial("dest_serial"); 2164 wait_for_migration_complete(from); 2165 test_migrate_end(from, to2, true); 2166 } 2167 2168 static void calc_dirty_rate(QTestState *who, uint64_t calc_time) 2169 { 2170 qobject_unref(qmp_command(who, 2171 "{ 'execute': 'calc-dirty-rate'," 2172 "'arguments': { " 2173 "'calc-time': %ld," 2174 "'mode': 'dirty-ring' }}", 2175 calc_time)); 2176 } 2177 2178 static QDict *query_dirty_rate(QTestState *who) 2179 { 2180 return qmp_command(who, "{ 'execute': 'query-dirty-rate' }"); 2181 } 2182 2183 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate) 2184 { 2185 qobject_unref(qmp_command(who, 2186 "{ 'execute': 'set-vcpu-dirty-limit'," 2187 "'arguments': { " 2188 "'dirty-rate': %ld } }", 2189 dirtyrate)); 2190 } 2191 2192 static void cancel_vcpu_dirty_limit(QTestState *who) 2193 { 2194 qobject_unref(qmp_command(who, 2195 "{ 'execute': 'cancel-vcpu-dirty-limit' }")); 2196 } 2197 2198 static QDict *query_vcpu_dirty_limit(QTestState *who) 2199 { 2200 QDict *rsp; 2201 2202 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }"); 2203 g_assert(!qdict_haskey(rsp, "error")); 2204 g_assert(qdict_haskey(rsp, "return")); 2205 2206 return rsp; 2207 } 2208 2209 static bool calc_dirtyrate_ready(QTestState *who) 2210 { 2211 QDict *rsp_return; 2212 gchar *status; 2213 2214 rsp_return = query_dirty_rate(who); 2215 g_assert(rsp_return); 2216 2217 status = g_strdup(qdict_get_str(rsp_return, "status")); 2218 g_assert(status); 2219 2220 return g_strcmp0(status, "measuring"); 2221 } 2222 2223 static void wait_for_calc_dirtyrate_complete(QTestState *who, 2224 int64_t time_s) 2225 { 2226 int max_try_count = 10000; 2227 usleep(time_s * 1000000); 2228 2229 while (!calc_dirtyrate_ready(who) && max_try_count--) { 2230 usleep(1000); 2231 } 2232 2233 /* 2234 * Set the timeout with 10 s(max_try_count * 1000us), 2235 * if dirtyrate measurement not complete, fail test. 2236 */ 2237 g_assert_cmpint(max_try_count, !=, 0); 2238 } 2239 2240 static int64_t get_dirty_rate(QTestState *who) 2241 { 2242 QDict *rsp_return; 2243 gchar *status; 2244 QList *rates; 2245 const QListEntry *entry; 2246 QDict *rate; 2247 int64_t dirtyrate; 2248 2249 rsp_return = query_dirty_rate(who); 2250 g_assert(rsp_return); 2251 2252 status = g_strdup(qdict_get_str(rsp_return, "status")); 2253 g_assert(status); 2254 g_assert_cmpstr(status, ==, "measured"); 2255 2256 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate"); 2257 g_assert(rates && !qlist_empty(rates)); 2258 2259 entry = qlist_first(rates); 2260 g_assert(entry); 2261 2262 rate = qobject_to(QDict, qlist_entry_obj(entry)); 2263 g_assert(rate); 2264 2265 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1); 2266 2267 qobject_unref(rsp_return); 2268 return dirtyrate; 2269 } 2270 2271 static int64_t get_limit_rate(QTestState *who) 2272 { 2273 QDict *rsp_return; 2274 QList *rates; 2275 const QListEntry *entry; 2276 QDict *rate; 2277 int64_t dirtyrate; 2278 2279 rsp_return = query_vcpu_dirty_limit(who); 2280 g_assert(rsp_return); 2281 2282 rates = qdict_get_qlist(rsp_return, "return"); 2283 g_assert(rates && !qlist_empty(rates)); 2284 2285 entry = qlist_first(rates); 2286 g_assert(entry); 2287 2288 rate = qobject_to(QDict, qlist_entry_obj(entry)); 2289 g_assert(rate); 2290 2291 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1); 2292 2293 qobject_unref(rsp_return); 2294 return dirtyrate; 2295 } 2296 2297 static QTestState *dirtylimit_start_vm(void) 2298 { 2299 QTestState *vm = NULL; 2300 g_autofree gchar *cmd = NULL; 2301 const char *arch = qtest_get_arch(); 2302 g_autofree char *bootpath = NULL; 2303 2304 assert((strcmp(arch, "x86_64") == 0)); 2305 bootpath = g_strdup_printf("%s/bootsect", tmpfs); 2306 assert(sizeof(x86_bootsect) == 512); 2307 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect)); 2308 2309 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 " 2310 "-name dirtylimit-test,debug-threads=on " 2311 "-m 150M -smp 1 " 2312 "-serial file:%s/vm_serial " 2313 "-drive file=%s,format=raw ", 2314 tmpfs, bootpath); 2315 2316 vm = qtest_init(cmd); 2317 return vm; 2318 } 2319 2320 static void dirtylimit_stop_vm(QTestState *vm) 2321 { 2322 qtest_quit(vm); 2323 cleanup("bootsect"); 2324 cleanup("vm_serial"); 2325 } 2326 2327 static void test_vcpu_dirty_limit(void) 2328 { 2329 QTestState *vm; 2330 int64_t origin_rate; 2331 int64_t quota_rate; 2332 int64_t rate ; 2333 int max_try_count = 20; 2334 int hit = 0; 2335 2336 /* Start vm for vcpu dirtylimit test */ 2337 vm = dirtylimit_start_vm(); 2338 2339 /* Wait for the first serial output from the vm*/ 2340 wait_for_serial("vm_serial"); 2341 2342 /* Do dirtyrate measurement with calc time equals 1s */ 2343 calc_dirty_rate(vm, 1); 2344 2345 /* Sleep calc time and wait for calc dirtyrate complete */ 2346 wait_for_calc_dirtyrate_complete(vm, 1); 2347 2348 /* Query original dirty page rate */ 2349 origin_rate = get_dirty_rate(vm); 2350 2351 /* VM booted from bootsect should dirty memory steadily */ 2352 assert(origin_rate != 0); 2353 2354 /* Setup quota dirty page rate at half of origin */ 2355 quota_rate = origin_rate / 2; 2356 2357 /* Set dirtylimit */ 2358 dirtylimit_set_all(vm, quota_rate); 2359 2360 /* 2361 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit 2362 * works literally 2363 */ 2364 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm)); 2365 2366 /* Sleep a bit to check if it take effect */ 2367 usleep(2000000); 2368 2369 /* 2370 * Check if dirtylimit take effect realistically, set the 2371 * timeout with 20 s(max_try_count * 1s), if dirtylimit 2372 * doesn't take effect, fail test. 2373 */ 2374 while (--max_try_count) { 2375 calc_dirty_rate(vm, 1); 2376 wait_for_calc_dirtyrate_complete(vm, 1); 2377 rate = get_dirty_rate(vm); 2378 2379 /* 2380 * Assume hitting if current rate is less 2381 * than quota rate (within accepting error) 2382 */ 2383 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 2384 hit = 1; 2385 break; 2386 } 2387 } 2388 2389 g_assert_cmpint(hit, ==, 1); 2390 2391 hit = 0; 2392 max_try_count = 20; 2393 2394 /* Check if dirtylimit cancellation take effect */ 2395 cancel_vcpu_dirty_limit(vm); 2396 while (--max_try_count) { 2397 calc_dirty_rate(vm, 1); 2398 wait_for_calc_dirtyrate_complete(vm, 1); 2399 rate = get_dirty_rate(vm); 2400 2401 /* 2402 * Assume dirtylimit be canceled if current rate is 2403 * greater than quota rate (within accepting error) 2404 */ 2405 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) { 2406 hit = 1; 2407 break; 2408 } 2409 } 2410 2411 g_assert_cmpint(hit, ==, 1); 2412 dirtylimit_stop_vm(vm); 2413 } 2414 2415 static bool kvm_dirty_ring_supported(void) 2416 { 2417 #if defined(__linux__) && defined(HOST_X86_64) 2418 int ret, kvm_fd = open("/dev/kvm", O_RDONLY); 2419 2420 if (kvm_fd < 0) { 2421 return false; 2422 } 2423 2424 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING); 2425 close(kvm_fd); 2426 2427 /* We test with 4096 slots */ 2428 if (ret < 4096) { 2429 return false; 2430 } 2431 2432 return true; 2433 #else 2434 return false; 2435 #endif 2436 } 2437 2438 int main(int argc, char **argv) 2439 { 2440 char template[] = "/tmp/migration-test-XXXXXX"; 2441 const bool has_kvm = qtest_has_accel("kvm"); 2442 int ret; 2443 2444 g_test_init(&argc, &argv, NULL); 2445 2446 if (!ufd_version_check()) { 2447 return g_test_run(); 2448 } 2449 2450 /* 2451 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG 2452 * is touchy due to race conditions on dirty bits (especially on PPC for 2453 * some reason) 2454 */ 2455 if (g_str_equal(qtest_get_arch(), "ppc64") && 2456 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) { 2457 g_test_message("Skipping test: kvm_hv not available"); 2458 return g_test_run(); 2459 } 2460 2461 /* 2462 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it 2463 * there until the problems are resolved 2464 */ 2465 if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm) { 2466 g_test_message("Skipping test: s390x host with KVM is required"); 2467 return g_test_run(); 2468 } 2469 2470 tmpfs = mkdtemp(template); 2471 if (!tmpfs) { 2472 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno)); 2473 } 2474 g_assert(tmpfs); 2475 2476 module_call_init(MODULE_INIT_QOM); 2477 2478 qtest_add_func("/migration/postcopy/unix", test_postcopy); 2479 qtest_add_func("/migration/postcopy/plain", test_postcopy); 2480 qtest_add_func("/migration/postcopy/recovery/plain", 2481 test_postcopy_recovery); 2482 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt); 2483 qtest_add_func("/migration/postcopy/preempt/recovery/plain", 2484 test_postcopy_preempt_recovery); 2485 2486 qtest_add_func("/migration/bad_dest", test_baddest); 2487 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain); 2488 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle); 2489 #ifdef CONFIG_GNUTLS 2490 qtest_add_func("/migration/precopy/unix/tls/psk", 2491 test_precopy_unix_tls_psk); 2492 /* 2493 * NOTE: psk test is enough for postcopy, as other types of TLS 2494 * channels are tested under precopy. Here what we want to test is the 2495 * general postcopy path that has TLS channel enabled. 2496 */ 2497 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk); 2498 qtest_add_func("/migration/postcopy/recovery/tls/psk", 2499 test_postcopy_recovery_tls_psk); 2500 qtest_add_func("/migration/postcopy/preempt/tls/psk", 2501 test_postcopy_preempt_tls_psk); 2502 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk", 2503 test_postcopy_preempt_all); 2504 #ifdef CONFIG_TASN1 2505 qtest_add_func("/migration/precopy/unix/tls/x509/default-host", 2506 test_precopy_unix_tls_x509_default_host); 2507 qtest_add_func("/migration/precopy/unix/tls/x509/override-host", 2508 test_precopy_unix_tls_x509_override_host); 2509 #endif /* CONFIG_TASN1 */ 2510 #endif /* CONFIG_GNUTLS */ 2511 2512 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain); 2513 #ifdef CONFIG_GNUTLS 2514 qtest_add_func("/migration/precopy/tcp/tls/psk/match", 2515 test_precopy_tcp_tls_psk_match); 2516 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch", 2517 test_precopy_tcp_tls_psk_mismatch); 2518 #ifdef CONFIG_TASN1 2519 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host", 2520 test_precopy_tcp_tls_x509_default_host); 2521 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host", 2522 test_precopy_tcp_tls_x509_override_host); 2523 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host", 2524 test_precopy_tcp_tls_x509_mismatch_host); 2525 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client", 2526 test_precopy_tcp_tls_x509_friendly_client); 2527 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client", 2528 test_precopy_tcp_tls_x509_hostile_client); 2529 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client", 2530 test_precopy_tcp_tls_x509_allow_anon_client); 2531 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client", 2532 test_precopy_tcp_tls_x509_reject_anon_client); 2533 #endif /* CONFIG_TASN1 */ 2534 #endif /* CONFIG_GNUTLS */ 2535 2536 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ 2537 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto); 2538 qtest_add_func("/migration/validate_uuid", test_validate_uuid); 2539 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error); 2540 qtest_add_func("/migration/validate_uuid_src_not_set", 2541 test_validate_uuid_src_not_set); 2542 qtest_add_func("/migration/validate_uuid_dst_not_set", 2543 test_validate_uuid_dst_not_set); 2544 2545 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge); 2546 qtest_add_func("/migration/multifd/tcp/plain/none", 2547 test_multifd_tcp_none); 2548 qtest_add_func("/migration/multifd/tcp/plain/cancel", 2549 test_multifd_tcp_cancel); 2550 qtest_add_func("/migration/multifd/tcp/plain/zlib", 2551 test_multifd_tcp_zlib); 2552 #ifdef CONFIG_ZSTD 2553 qtest_add_func("/migration/multifd/tcp/plain/zstd", 2554 test_multifd_tcp_zstd); 2555 #endif 2556 #ifdef CONFIG_GNUTLS 2557 qtest_add_func("/migration/multifd/tcp/tls/psk/match", 2558 test_multifd_tcp_tls_psk_match); 2559 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch", 2560 test_multifd_tcp_tls_psk_mismatch); 2561 #ifdef CONFIG_TASN1 2562 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host", 2563 test_multifd_tcp_tls_x509_default_host); 2564 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host", 2565 test_multifd_tcp_tls_x509_override_host); 2566 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host", 2567 test_multifd_tcp_tls_x509_mismatch_host); 2568 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client", 2569 test_multifd_tcp_tls_x509_allow_anon_client); 2570 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client", 2571 test_multifd_tcp_tls_x509_reject_anon_client); 2572 #endif /* CONFIG_TASN1 */ 2573 #endif /* CONFIG_GNUTLS */ 2574 2575 if (kvm_dirty_ring_supported()) { 2576 qtest_add_func("/migration/dirty_ring", 2577 test_precopy_unix_dirty_ring); 2578 qtest_add_func("/migration/vcpu_dirty_limit", 2579 test_vcpu_dirty_limit); 2580 } 2581 2582 ret = g_test_run(); 2583 2584 g_assert_cmpint(ret, ==, 0); 2585 2586 ret = rmdir(tmpfs); 2587 if (ret != 0) { 2588 g_test_message("unable to rmdir: path (%s): %s", 2589 tmpfs, strerror(errno)); 2590 } 2591 2592 return ret; 2593 } 2594