2 * QTest testcase for migration
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.
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.
13 #include "qemu/osdep.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "crypto/tlscredspsk.h"
23 #include "qapi/qmp/qlist.h"
25 #include "migration-helpers.h"
26 #include "tests/migration/migration-test.h"
28 # include "tests/unit/crypto-tls-psk-helpers.h"
30 # include "tests/unit/crypto-tls-x509-helpers.h"
31 # endif /* CONFIG_TASN1 */
32 #endif /* CONFIG_GNUTLS */
34 /* For dirty ring test; so far only x86_64 is supported */
35 #if defined(__linux__) && defined(HOST_X86_64)
36 #include "linux/kvm.h"
39 unsigned start_address
;
41 static bool uffd_feature_thread_id
;
42 static QTestMigrationState src_state
;
43 static QTestMigrationState dst_state
;
46 * An initial 3 MB offset is used as that corresponds
47 * to ~1 sec of data transfer with our bandwidth setting.
49 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
51 * A further 1k is added to ensure we're not a multiple
52 * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
53 * from the migration guest workload.
55 #define MAGIC_OFFSET_SHUFFLE 1024
56 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
57 #define MAGIC_MARKER 0xFEED12345678CAFEULL
60 * Dirtylimit stop working if dirty page rate error
61 * value less than DIRTYLIMIT_TOLERANCE_RANGE
63 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
65 #define ANALYZE_SCRIPT "scripts/analyze-migration.py"
67 #define QEMU_VM_FILE_MAGIC 0x5145564d
68 #define FILE_TEST_FILENAME "migfile"
69 #define FILE_TEST_OFFSET 0x1000
70 #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
71 #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST"
73 #if defined(__linux__)
74 #include <sys/syscall.h>
78 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
79 #include <sys/eventfd.h>
80 #include <sys/ioctl.h>
81 #include "qemu/userfaultfd.h"
83 static bool ufd_version_check(void)
85 struct uffdio_api api_struct
;
88 int ufd
= uffd_open(O_CLOEXEC
);
91 g_test_message("Skipping test: userfaultfd not available");
95 api_struct
.api
= UFFD_API
;
96 api_struct
.features
= 0;
97 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
98 g_test_message("Skipping test: UFFDIO_API failed");
101 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
103 ioctl_mask
= 1ULL << _UFFDIO_REGISTER
|
104 1ULL << _UFFDIO_UNREGISTER
;
105 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
106 g_test_message("Skipping test: Missing userfault feature");
114 static bool ufd_version_check(void)
116 g_test_message("Skipping test: Userfault not available (builtdtime)");
123 static char *bootpath
;
125 /* The boot file modifies memory area in [start_address, end_address)
126 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
128 #include "tests/migration/i386/a-b-bootblock.h"
129 #include "tests/migration/aarch64/a-b-kernel.h"
130 #include "tests/migration/s390x/a-b-bios.h"
132 static void bootfile_create(char *dir
, bool suspend_me
)
134 const char *arch
= qtest_get_arch();
135 unsigned char *content
;
138 bootpath
= g_strdup_printf("%s/bootsect", dir
);
139 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
140 /* the assembled x86 boot sector should be exactly one sector large */
141 g_assert(sizeof(x86_bootsect
) == 512);
142 x86_bootsect
[SYM_suspend_me
- SYM_start
] = suspend_me
;
143 content
= x86_bootsect
;
144 len
= sizeof(x86_bootsect
);
145 } else if (g_str_equal(arch
, "s390x")) {
147 len
= sizeof(s390x_elf
);
148 } else if (strcmp(arch
, "ppc64") == 0) {
150 * sane architectures can be programmed at the boot prompt
153 } else if (strcmp(arch
, "aarch64") == 0) {
154 content
= aarch64_kernel
;
155 len
= sizeof(aarch64_kernel
);
156 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
158 g_assert_not_reached();
161 FILE *bootfile
= fopen(bootpath
, "wb");
163 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
167 static void bootfile_delete(void)
175 * Wait for some output in the serial output file,
176 * we get an 'A' followed by an endless string of 'B's
177 * but on the destination we won't have the A (unless we enabled suspend/resume)
179 static void wait_for_serial(const char *side
)
181 g_autofree
char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
182 FILE *serialfile
= fopen(serialpath
, "r");
183 const char *arch
= qtest_get_arch();
184 int started
= (strcmp(side
, "src_serial") == 0 &&
185 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
188 int readvalue
= fgetc(serialfile
);
191 /* SLOF prints its banner before starting test,
192 * to ignore it, mark the start of the test with '_',
193 * ignore all characters until this marker
200 fseek(serialfile
, 0, SEEK_SET
);
217 started
= (strcmp(side
, "src_serial") == 0 &&
218 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
219 fseek(serialfile
, 0, SEEK_SET
);
224 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
225 g_assert_not_reached();
230 static void wait_for_stop(QTestState
*who
, QTestMigrationState
*state
)
232 if (!state
->stop_seen
) {
233 qtest_qmp_eventwait(who
, "STOP");
237 static void wait_for_resume(QTestState
*who
, QTestMigrationState
*state
)
239 if (!state
->resume_seen
) {
240 qtest_qmp_eventwait(who
, "RESUME");
244 static void wait_for_suspend(QTestState
*who
, QTestMigrationState
*state
)
246 if (state
->suspend_me
&& !state
->suspend_seen
) {
247 qtest_qmp_eventwait(who
, "SUSPEND");
252 * It's tricky to use qemu's migration event capability with qtest,
253 * events suddenly appearing confuse the qmp()/hmp() responses.
256 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
258 QDict
*rsp_return
, *rsp_ram
;
261 rsp_return
= migrate_query_not_failed(who
);
262 if (!qdict_haskey(rsp_return
, "ram")) {
266 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
267 result
= qdict_get_try_int(rsp_ram
, property
, 0);
269 qobject_unref(rsp_return
);
273 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
278 rsp_return
= migrate_query_not_failed(who
);
279 result
= qdict_get_try_int(rsp_return
, property
, 0);
280 qobject_unref(rsp_return
);
284 static uint64_t get_migration_pass(QTestState
*who
)
286 return read_ram_property_int(who
, "dirty-sync-count");
289 static void read_blocktime(QTestState
*who
)
293 rsp_return
= migrate_query_not_failed(who
);
294 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
295 qobject_unref(rsp_return
);
299 * Wait for two changes in the migration pass count, but bail if we stop.
301 static void wait_for_migration_pass(QTestState
*who
)
303 uint64_t pass
, prev_pass
= 0, changes
= 0;
305 while (changes
< 2 && !src_state
.stop_seen
&& !src_state
.suspend_seen
) {
307 pass
= get_migration_pass(who
);
308 changes
+= (pass
!= prev_pass
);
313 static void check_guests_ram(QTestState
*who
)
315 /* Our ASM test will have been incrementing one byte from each page from
316 * start_address to < end_address in order. This gives us a constraint
317 * that any page's byte should be equal or less than the previous pages
318 * byte (mod 256); and they should all be equal except for one transition
319 * at the point where we meet the incrementer. (We're running this with
320 * the guest stopped).
325 bool hit_edge
= false;
328 qtest_memread(who
, start_address
, &first_byte
, 1);
329 last_byte
= first_byte
;
331 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
332 address
+= TEST_MEM_PAGE_SIZE
)
335 qtest_memread(who
, address
, &b
, 1);
336 if (b
!= last_byte
) {
337 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
338 /* This is OK, the guest stopped at the point of
339 * incrementing the previous page but didn't get
347 fprintf(stderr
, "Memory content inconsistency at %x"
348 " first_byte = %x last_byte = %x current = %x"
350 address
, first_byte
, last_byte
, b
, hit_edge
);
356 fprintf(stderr
, "and in another %d pages", bad
- 10);
361 static void cleanup(const char *filename
)
363 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
368 static long long migrate_get_parameter_int(QTestState
*who
,
369 const char *parameter
)
374 rsp
= qtest_qmp_assert_success_ref(
375 who
, "{ 'execute': 'query-migrate-parameters' }");
376 result
= qdict_get_int(rsp
, parameter
);
381 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
386 result
= migrate_get_parameter_int(who
, parameter
);
387 g_assert_cmpint(result
, ==, value
);
390 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
393 qtest_qmp_assert_success(who
,
394 "{ 'execute': 'migrate-set-parameters',"
395 "'arguments': { %s: %lld } }",
397 migrate_check_parameter_int(who
, parameter
, value
);
400 static char *migrate_get_parameter_str(QTestState
*who
,
401 const char *parameter
)
406 rsp
= qtest_qmp_assert_success_ref(
407 who
, "{ 'execute': 'query-migrate-parameters' }");
408 result
= g_strdup(qdict_get_str(rsp
, parameter
));
413 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
416 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
417 g_assert_cmpstr(result
, ==, value
);
420 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
423 qtest_qmp_assert_success(who
,
424 "{ 'execute': 'migrate-set-parameters',"
425 "'arguments': { %s: %s } }",
427 migrate_check_parameter_str(who
, parameter
, value
);
430 static void migrate_ensure_non_converge(QTestState
*who
)
432 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
433 migrate_set_parameter_int(who
, "max-bandwidth", 3 * 1000 * 1000);
434 migrate_set_parameter_int(who
, "downtime-limit", 1);
437 static void migrate_ensure_converge(QTestState
*who
)
439 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
440 migrate_set_parameter_int(who
, "max-bandwidth", 1 * 1000 * 1000 * 1000);
441 migrate_set_parameter_int(who
, "downtime-limit", 30 * 1000);
445 * Our goal is to ensure that we run a single full migration
446 * iteration, and also dirty memory, ensuring that at least
447 * one further iteration is required.
449 * We can't directly synchronize with the start of a migration
450 * so we have to apply some tricks monitoring memory that is
453 * Initially we set the migration bandwidth to an insanely
454 * low value, with tiny max downtime too. This basically
455 * guarantees migration will never complete.
457 * This will result in a test that is unacceptably slow though,
458 * so we can't let the entire migration pass run at this speed.
459 * Our intent is to let it run just long enough that we can
460 * prove data prior to the marker has been transferred *AND*
461 * also prove this transferred data is dirty again.
463 * Before migration starts, we write a 64-bit magic marker
464 * into a fixed location in the src VM RAM.
466 * Then watch dst memory until the marker appears. This is
467 * proof that start_address -> MAGIC_OFFSET_BASE has been
470 * Finally we go back to the source and read a byte just
471 * before the marker until we see it flip in value. This
472 * is proof that start_address -> MAGIC_OFFSET_BASE
473 * is now dirty again.
475 * IOW, we're guaranteed at least a 2nd migration pass
478 * We can now let migration run at full speed to finish
481 static void migrate_prepare_for_dirty_mem(QTestState
*from
)
484 * The guest workflow iterates from start_address to
485 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
488 * IOW, if we write to mem at a point which is NOT
489 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
490 * conflict with the migration workflow.
492 * We put in a marker here, that we'll use to determine
493 * when the data has been transferred to the dst.
495 qtest_writeq(from
, start_address
+ MAGIC_OFFSET
, MAGIC_MARKER
);
498 static void migrate_wait_for_dirty_mem(QTestState
*from
,
501 uint64_t watch_address
= start_address
+ MAGIC_OFFSET_BASE
;
502 uint64_t marker_address
= start_address
+ MAGIC_OFFSET
;
506 * Wait for the MAGIC_MARKER to get transferred, as an
507 * indicator that a migration pass has made some known
508 * amount of progress.
512 } while (qtest_readq(to
, marker_address
) != MAGIC_MARKER
);
515 /* If suspended, src only iterates once, and watch_byte may never change */
516 if (src_state
.suspend_me
) {
521 * Now ensure that already transferred bytes are
522 * dirty again from the guest workload. Note the
523 * guest byte value will wrap around and by chance
524 * match the original watch_byte. This is harmless
525 * as we'll eventually see a different value if we
528 watch_byte
= qtest_readb(from
, watch_address
);
531 } while (qtest_readb(from
, watch_address
) == watch_byte
);
535 static void migrate_pause(QTestState
*who
)
537 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate-pause' }");
540 static void migrate_continue(QTestState
*who
, const char *state
)
542 qtest_qmp_assert_success(who
,
543 "{ 'execute': 'migrate-continue',"
544 " 'arguments': { 'state': %s } }",
548 static void migrate_recover(QTestState
*who
, const char *uri
)
550 qtest_qmp_assert_success(who
,
551 "{ 'execute': 'migrate-recover', "
552 " 'id': 'recover-cmd', "
553 " 'arguments': { 'uri': %s } }",
557 static void migrate_cancel(QTestState
*who
)
559 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate_cancel' }");
562 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
564 qtest_qmp_assert_success(from
, "{ 'execute': 'migrate-start-postcopy' }");
566 wait_for_stop(from
, &src_state
);
567 qtest_qmp_eventwait(to
, "RESUME");
572 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
573 * unconditionally, because it means the user would like to be verbose.
577 /* only launch the target process */
579 /* Use dirty ring if true; dirty logging otherwise */
581 const char *opts_source
;
582 const char *opts_target
;
583 /* suspend the src before migrating to dest. */
588 * A hook that runs after the src and dst QEMUs have been
589 * created, but before the migration is started. This can
590 * be used to set migration parameters and capabilities.
592 * Returns: NULL, or a pointer to opaque state to be
593 * later passed to the TestMigrateFinishHook
595 typedef void * (*TestMigrateStartHook
)(QTestState
*from
,
599 * A hook that runs after the migration has finished,
600 * regardless of whether it succeeded or failed, but
601 * before QEMU has terminated (unless it self-terminated
602 * due to migration error)
604 * @opaque is a pointer to state previously returned
605 * by the TestMigrateStartHook if any, or NULL.
607 typedef void (*TestMigrateFinishHook
)(QTestState
*from
,
612 /* Optional: fine tune start parameters */
615 /* Required: the URI for the dst QEMU to listen on */
616 const char *listen_uri
;
619 * Optional: the URI for the src QEMU to connect to
620 * If NULL, then it will query the dst QEMU for its actual
621 * listening address and use that as the connect address.
622 * This allows for dynamically picking a free TCP port.
624 const char *connect_uri
;
627 * Optional: JSON-formatted list of src QEMU URIs. If a port is
628 * defined as '0' in any QDict key a value of '0' will be
629 * automatically converted to the correct destination port.
631 const char *connect_channels
;
633 /* Optional: callback to run at start to set migration parameters */
634 TestMigrateStartHook start_hook
;
635 /* Optional: callback to run at finish to cleanup */
636 TestMigrateFinishHook finish_hook
;
639 * Optional: normally we expect the migration process to complete.
641 * There can be a variety of reasons and stages in which failure
642 * can happen during tests.
644 * If a failure is expected to happen at time of establishing
645 * the connection, then MIG_TEST_FAIL will indicate that the dst
646 * QEMU is expected to stay running and accept future migration
649 * If a failure is expected to happen while processing the
650 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
651 * that the dst QEMU is expected to quit with non-zero exit status
654 /* This test should succeed, the default */
655 MIG_TEST_SUCCEED
= 0,
656 /* This test should fail, dest qemu should keep alive */
658 /* This test should fail, dest qemu should fail with abnormal status */
659 MIG_TEST_FAIL_DEST_QUIT_ERR
,
660 /* The QMP command for this migration should fail with an error */
665 * Optional: set number of migration passes to wait for, if live==true.
666 * If zero, then merely wait for a few MB of dirty data
668 unsigned int iterations
;
671 * Optional: whether the guest CPUs should be running during a precopy
672 * migration test. We used to always run with live but it took much
673 * longer so we reduced live tests to only the ones that have solid
674 * reason to be tested live-only. For each of the new test cases for
675 * precopy please provide justifications to use live explicitly (please
676 * refer to existing ones with live=true), or use live=off by default.
680 /* Postcopy specific fields */
682 bool postcopy_preempt
;
683 bool postcopy_recovery_test_fail
;
686 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
687 const char *uri
, MigrateStart
*args
)
689 g_autofree gchar
*arch_source
= NULL
;
690 g_autofree gchar
*arch_target
= NULL
;
691 /* options for source and target */
692 g_autofree gchar
*arch_opts
= NULL
;
693 g_autofree gchar
*cmd_source
= NULL
;
694 g_autofree gchar
*cmd_target
= NULL
;
695 const gchar
*ignore_stderr
;
696 g_autofree
char *shmem_opts
= NULL
;
697 g_autofree
char *shmem_path
= NULL
;
698 const char *kvm_opts
= NULL
;
699 const char *arch
= qtest_get_arch();
700 const char *memory_size
;
701 const char *machine_alias
, *machine_opts
= "";
702 g_autofree
char *machine
= NULL
;
704 if (args
->use_shmem
) {
705 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
706 g_test_skip("/dev/shm is not supported");
711 dst_state
= (QTestMigrationState
) { };
712 src_state
= (QTestMigrationState
) { };
713 bootfile_create(tmpfs
, args
->suspend_me
);
714 src_state
.suspend_me
= args
->suspend_me
;
716 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
717 memory_size
= "150M";
719 if (g_str_equal(arch
, "i386")) {
720 machine_alias
= "pc";
722 machine_alias
= "q35";
724 arch_opts
= g_strdup_printf(
725 "-drive if=none,id=d0,file=%s,format=raw "
726 "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath
);
727 start_address
= X86_TEST_MEM_START
;
728 end_address
= X86_TEST_MEM_END
;
729 } else if (g_str_equal(arch
, "s390x")) {
730 memory_size
= "128M";
731 machine_alias
= "s390-ccw-virtio";
732 arch_opts
= g_strdup_printf("-bios %s", bootpath
);
733 start_address
= S390_TEST_MEM_START
;
734 end_address
= S390_TEST_MEM_END
;
735 } else if (strcmp(arch
, "ppc64") == 0) {
736 memory_size
= "256M";
737 start_address
= PPC_TEST_MEM_START
;
738 end_address
= PPC_TEST_MEM_END
;
739 arch_source
= g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
740 "'nvramrc=hex .\" _\" begin %x %x "
741 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
742 "until'", end_address
, start_address
);
743 machine_alias
= "pseries";
744 machine_opts
= "vsmt=8";
745 arch_opts
= g_strdup("-nodefaults");
746 } else if (strcmp(arch
, "aarch64") == 0) {
747 memory_size
= "150M";
748 machine_alias
= "virt";
749 machine_opts
= "gic-version=3";
750 arch_opts
= g_strdup_printf("-cpu max -kernel %s", bootpath
);
751 start_address
= ARM_TEST_MEM_START
;
752 end_address
= ARM_TEST_MEM_END
;
754 g_assert_not_reached();
757 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
759 ignore_stderr
= "2>/dev/null";
762 * On Windows the QEMU executable is created via CreateProcess() and
763 * IO redirection does not work, so don't bother adding IO redirection
764 * to the command line.
772 if (args
->use_shmem
) {
773 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
774 shmem_opts
= g_strdup_printf(
775 "-object memory-backend-file,id=mem0,size=%s"
776 ",mem-path=%s,share=on -numa node,memdev=mem0",
777 memory_size
, shmem_path
);
780 if (args
->use_dirty_ring
) {
781 kvm_opts
= ",dirty-ring-size=4096";
784 if (!qtest_has_machine(machine_alias
)) {
785 g_autofree
char *msg
= g_strdup_printf("machine %s not supported", machine_alias
);
790 machine
= resolve_machine_version(machine_alias
, QEMU_ENV_SRC
,
793 g_test_message("Using machine type: %s", machine
);
795 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg "
797 "-name source,debug-threads=on "
799 "-serial file:%s/src_serial "
801 kvm_opts
? kvm_opts
: "",
802 machine
, machine_opts
,
804 arch_opts
? arch_opts
: "",
805 arch_source
? arch_source
: "",
806 shmem_opts
? shmem_opts
: "",
807 args
->opts_source
? args
->opts_source
: "",
809 if (!args
->only_target
) {
810 *from
= qtest_init_with_env(QEMU_ENV_SRC
, cmd_source
);
811 qtest_qmp_set_event_callback(*from
,
812 migrate_watch_for_events
,
816 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg "
818 "-name target,debug-threads=on "
820 "-serial file:%s/dest_serial "
823 kvm_opts
? kvm_opts
: "",
824 machine
, machine_opts
,
825 memory_size
, tmpfs
, uri
,
826 arch_opts
? arch_opts
: "",
827 arch_target
? arch_target
: "",
828 shmem_opts
? shmem_opts
: "",
829 args
->opts_target
? args
->opts_target
: "",
831 *to
= qtest_init_with_env(QEMU_ENV_DST
, cmd_target
);
832 qtest_qmp_set_event_callback(*to
,
833 migrate_watch_for_events
,
837 * Remove shmem file immediately to avoid memory leak in test failed case.
838 * It's valid because QEMU has already opened this file
840 if (args
->use_shmem
) {
847 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
849 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
854 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
856 /* Destination still running, wait for a byte to change */
858 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
860 } while (dest_byte_a
== dest_byte_b
);
862 qtest_qmp_assert_success(to
, "{ 'execute' : 'stop'}");
864 /* With it stopped, check nothing changes */
865 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
867 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
868 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
870 check_guests_ram(to
);
875 cleanup("migsocket");
876 cleanup("src_serial");
877 cleanup("dest_serial");
878 cleanup(FILE_TEST_FILENAME
);
882 struct TestMigrateTLSPSKData
{
890 test_migrate_tls_psk_start_common(QTestState
*from
,
894 struct TestMigrateTLSPSKData
*data
=
895 g_new0(struct TestMigrateTLSPSKData
, 1);
897 data
->workdir
= g_strdup_printf("%s/tlscredspsk0", tmpfs
);
898 data
->pskfile
= g_strdup_printf("%s/%s", data
->workdir
,
899 QCRYPTO_TLS_CREDS_PSKFILE
);
900 g_mkdir_with_parents(data
->workdir
, 0700);
901 test_tls_psk_init(data
->pskfile
);
904 data
->workdiralt
= g_strdup_printf("%s/tlscredspskalt0", tmpfs
);
905 data
->pskfilealt
= g_strdup_printf("%s/%s", data
->workdiralt
,
906 QCRYPTO_TLS_CREDS_PSKFILE
);
907 g_mkdir_with_parents(data
->workdiralt
, 0700);
908 test_tls_psk_init_alt(data
->pskfilealt
);
911 qtest_qmp_assert_success(from
,
912 "{ 'execute': 'object-add',"
913 " 'arguments': { 'qom-type': 'tls-creds-psk',"
914 " 'id': 'tlscredspsk0',"
915 " 'endpoint': 'client',"
917 " 'username': 'qemu'} }",
920 qtest_qmp_assert_success(to
,
921 "{ 'execute': 'object-add',"
922 " 'arguments': { 'qom-type': 'tls-creds-psk',"
923 " 'id': 'tlscredspsk0',"
924 " 'endpoint': 'server',"
926 mismatch
? data
->workdiralt
: data
->workdir
);
928 migrate_set_parameter_str(from
, "tls-creds", "tlscredspsk0");
929 migrate_set_parameter_str(to
, "tls-creds", "tlscredspsk0");
935 test_migrate_tls_psk_start_match(QTestState
*from
,
938 return test_migrate_tls_psk_start_common(from
, to
, false);
942 test_migrate_tls_psk_start_mismatch(QTestState
*from
,
945 return test_migrate_tls_psk_start_common(from
, to
, true);
949 test_migrate_tls_psk_finish(QTestState
*from
,
953 struct TestMigrateTLSPSKData
*data
= opaque
;
955 test_tls_psk_cleanup(data
->pskfile
);
956 if (data
->pskfilealt
) {
957 test_tls_psk_cleanup(data
->pskfilealt
);
959 rmdir(data
->workdir
);
960 if (data
->workdiralt
) {
961 rmdir(data
->workdiralt
);
964 g_free(data
->workdiralt
);
965 g_free(data
->pskfilealt
);
966 g_free(data
->workdir
);
967 g_free(data
->pskfile
);
980 } TestMigrateTLSX509Data
;
987 const char *certhostname
;
988 const char *certipaddr
;
989 } TestMigrateTLSX509
;
992 test_migrate_tls_x509_start_common(QTestState
*from
,
994 TestMigrateTLSX509
*args
)
996 TestMigrateTLSX509Data
*data
= g_new0(TestMigrateTLSX509Data
, 1);
998 data
->workdir
= g_strdup_printf("%s/tlscredsx5090", tmpfs
);
999 data
->keyfile
= g_strdup_printf("%s/key.pem", data
->workdir
);
1001 data
->cacert
= g_strdup_printf("%s/ca-cert.pem", data
->workdir
);
1002 data
->serverkey
= g_strdup_printf("%s/server-key.pem", data
->workdir
);
1003 data
->servercert
= g_strdup_printf("%s/server-cert.pem", data
->workdir
);
1004 if (args
->clientcert
) {
1005 data
->clientkey
= g_strdup_printf("%s/client-key.pem", data
->workdir
);
1006 data
->clientcert
= g_strdup_printf("%s/client-cert.pem", data
->workdir
);
1009 g_mkdir_with_parents(data
->workdir
, 0700);
1011 test_tls_init(data
->keyfile
);
1013 g_assert(link(data
->keyfile
, data
->serverkey
) == 0);
1015 g_assert(CreateHardLink(data
->serverkey
, data
->keyfile
, NULL
) != 0);
1017 if (args
->clientcert
) {
1019 g_assert(link(data
->keyfile
, data
->clientkey
) == 0);
1021 g_assert(CreateHardLink(data
->clientkey
, data
->keyfile
, NULL
) != 0);
1025 TLS_ROOT_REQ_SIMPLE(cacertreq
, data
->cacert
);
1026 if (args
->clientcert
) {
1027 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq
, cacertreq
,
1028 args
->hostileclient
?
1029 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME
:
1030 QCRYPTO_TLS_TEST_CLIENT_NAME
,
1034 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq
, cacertreq
,
1039 qtest_qmp_assert_success(from
,
1040 "{ 'execute': 'object-add',"
1041 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1042 " 'id': 'tlscredsx509client0',"
1043 " 'endpoint': 'client',"
1045 " 'sanity-check': true,"
1046 " 'verify-peer': true} }",
1048 migrate_set_parameter_str(from
, "tls-creds", "tlscredsx509client0");
1049 if (args
->certhostname
) {
1050 migrate_set_parameter_str(from
, "tls-hostname", args
->certhostname
);
1053 qtest_qmp_assert_success(to
,
1054 "{ 'execute': 'object-add',"
1055 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1056 " 'id': 'tlscredsx509server0',"
1057 " 'endpoint': 'server',"
1059 " 'sanity-check': true,"
1060 " 'verify-peer': %i} }",
1061 data
->workdir
, args
->verifyclient
);
1062 migrate_set_parameter_str(to
, "tls-creds", "tlscredsx509server0");
1064 if (args
->authzclient
) {
1065 qtest_qmp_assert_success(to
,
1066 "{ 'execute': 'object-add',"
1067 " 'arguments': { 'qom-type': 'authz-simple',"
1068 " 'id': 'tlsauthz0',"
1069 " 'identity': %s} }",
1070 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME
);
1071 migrate_set_parameter_str(to
, "tls-authz", "tlsauthz0");
1078 * The normal case: match server's cert hostname against
1079 * whatever host we were telling QEMU to connect to (if any)
1082 test_migrate_tls_x509_start_default_host(QTestState
*from
,
1085 TestMigrateTLSX509 args
= {
1086 .verifyclient
= true,
1088 .certipaddr
= "127.0.0.1"
1090 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1094 * The unusual case: the server's cert is different from
1095 * the address we're telling QEMU to connect to (if any),
1096 * so we must give QEMU an explicit hostname to validate
1099 test_migrate_tls_x509_start_override_host(QTestState
*from
,
1102 TestMigrateTLSX509 args
= {
1103 .verifyclient
= true,
1105 .certhostname
= "qemu.org",
1107 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1111 * The unusual case: the server's cert is different from
1112 * the address we're telling QEMU to connect to, and so we
1113 * expect the client to reject the server
1116 test_migrate_tls_x509_start_mismatch_host(QTestState
*from
,
1119 TestMigrateTLSX509 args
= {
1120 .verifyclient
= true,
1122 .certipaddr
= "10.0.0.1",
1124 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1128 test_migrate_tls_x509_start_friendly_client(QTestState
*from
,
1131 TestMigrateTLSX509 args
= {
1132 .verifyclient
= true,
1134 .authzclient
= true,
1135 .certipaddr
= "127.0.0.1",
1137 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1141 test_migrate_tls_x509_start_hostile_client(QTestState
*from
,
1144 TestMigrateTLSX509 args
= {
1145 .verifyclient
= true,
1147 .hostileclient
= true,
1148 .authzclient
= true,
1149 .certipaddr
= "127.0.0.1",
1151 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1155 * The case with no client certificate presented,
1156 * and no server verification
1159 test_migrate_tls_x509_start_allow_anon_client(QTestState
*from
,
1162 TestMigrateTLSX509 args
= {
1163 .certipaddr
= "127.0.0.1",
1165 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1169 * The case with no client certificate presented,
1170 * and server verification rejecting
1173 test_migrate_tls_x509_start_reject_anon_client(QTestState
*from
,
1176 TestMigrateTLSX509 args
= {
1177 .verifyclient
= true,
1178 .certipaddr
= "127.0.0.1",
1180 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1184 test_migrate_tls_x509_finish(QTestState
*from
,
1188 TestMigrateTLSX509Data
*data
= opaque
;
1190 test_tls_cleanup(data
->keyfile
);
1191 g_free(data
->keyfile
);
1193 unlink(data
->cacert
);
1194 g_free(data
->cacert
);
1195 unlink(data
->servercert
);
1196 g_free(data
->servercert
);
1197 unlink(data
->serverkey
);
1198 g_free(data
->serverkey
);
1200 if (data
->clientcert
) {
1201 unlink(data
->clientcert
);
1202 g_free(data
->clientcert
);
1204 if (data
->clientkey
) {
1205 unlink(data
->clientkey
);
1206 g_free(data
->clientkey
);
1209 rmdir(data
->workdir
);
1210 g_free(data
->workdir
);
1214 #endif /* CONFIG_TASN1 */
1215 #endif /* CONFIG_GNUTLS */
1217 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
1218 QTestState
**to_ptr
,
1219 MigrateCommon
*args
)
1221 QTestState
*from
, *to
;
1223 if (test_migrate_start(&from
, &to
, "defer", &args
->start
)) {
1227 if (args
->start_hook
) {
1228 args
->postcopy_data
= args
->start_hook(from
, to
);
1231 migrate_set_capability(from
, "postcopy-ram", true);
1232 migrate_set_capability(to
, "postcopy-ram", true);
1233 migrate_set_capability(to
, "postcopy-blocktime", true);
1235 if (args
->postcopy_preempt
) {
1236 migrate_set_capability(from
, "postcopy-preempt", true);
1237 migrate_set_capability(to
, "postcopy-preempt", true);
1240 migrate_ensure_non_converge(from
);
1242 migrate_prepare_for_dirty_mem(from
);
1243 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1245 " 'channels': [ { 'channel-type': 'main',"
1246 " 'addr': { 'transport': 'socket',"
1248 " 'host': '127.0.0.1',"
1249 " 'port': '0' } } ] } }");
1251 /* Wait for the first serial output from the source */
1252 wait_for_serial("src_serial");
1253 wait_for_suspend(from
, &src_state
);
1255 migrate_qmp(from
, to
, NULL
, NULL
, "{}");
1257 migrate_wait_for_dirty_mem(from
, to
);
1265 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
,
1266 MigrateCommon
*args
)
1268 wait_for_migration_complete(from
);
1270 if (args
->start
.suspend_me
) {
1271 /* wakeup succeeds only if guest is suspended */
1272 qtest_qmp_assert_success(to
, "{'execute': 'system_wakeup'}");
1275 /* Make sure we get at least one "B" on destination */
1276 wait_for_serial("dest_serial");
1278 if (uffd_feature_thread_id
) {
1282 if (args
->finish_hook
) {
1283 args
->finish_hook(from
, to
, args
->postcopy_data
);
1284 args
->postcopy_data
= NULL
;
1287 test_migrate_end(from
, to
, true);
1290 static void test_postcopy_common(MigrateCommon
*args
)
1292 QTestState
*from
, *to
;
1294 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1297 migrate_postcopy_start(from
, to
);
1298 migrate_postcopy_complete(from
, to
, args
);
1301 static void test_postcopy(void)
1303 MigrateCommon args
= { };
1305 test_postcopy_common(&args
);
1308 static void test_postcopy_suspend(void)
1310 MigrateCommon args
= {
1311 .start
.suspend_me
= true,
1314 test_postcopy_common(&args
);
1317 static void test_postcopy_preempt(void)
1319 MigrateCommon args
= {
1320 .postcopy_preempt
= true,
1323 test_postcopy_common(&args
);
1326 #ifdef CONFIG_GNUTLS
1327 static void test_postcopy_tls_psk(void)
1329 MigrateCommon args
= {
1330 .start_hook
= test_migrate_tls_psk_start_match
,
1331 .finish_hook
= test_migrate_tls_psk_finish
,
1334 test_postcopy_common(&args
);
1337 static void test_postcopy_preempt_tls_psk(void)
1339 MigrateCommon args
= {
1340 .postcopy_preempt
= true,
1341 .start_hook
= test_migrate_tls_psk_start_match
,
1342 .finish_hook
= test_migrate_tls_psk_finish
,
1345 test_postcopy_common(&args
);
1349 static void wait_for_postcopy_status(QTestState
*one
, const char *status
)
1351 wait_for_migration_status(one
, status
,
1352 (const char * []) { "failed", "active",
1353 "completed", NULL
});
1357 static void postcopy_recover_fail(QTestState
*from
, QTestState
*to
)
1359 int ret
, pair1
[2], pair2
[2];
1362 /* Create two unrelated socketpairs */
1363 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair1
);
1364 g_assert_cmpint(ret
, ==, 0);
1366 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair2
);
1367 g_assert_cmpint(ret
, ==, 0);
1370 * Give the guests unpaired ends of the sockets, so they'll all blocked
1371 * at reading. This mimics a wrong channel established.
1373 qtest_qmp_fds_assert_success(from
, &pair1
[0], 1,
1374 "{ 'execute': 'getfd',"
1375 " 'arguments': { 'fdname': 'fd-mig' }}");
1376 qtest_qmp_fds_assert_success(to
, &pair2
[0], 1,
1377 "{ 'execute': 'getfd',"
1378 " 'arguments': { 'fdname': 'fd-mig' }}");
1381 * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
1382 * emulate the 1st byte of a real recovery, but stops from there to
1383 * keep dest QEMU in RECOVER. This is needed so that we can kick off
1384 * the recover process on dest QEMU (by triggering the G_IO_IN event).
1386 * NOTE: this trick is not needed on src QEMUs, because src doesn't
1387 * rely on an pre-existing G_IO_IN event, so it will always trigger the
1388 * upcoming recovery anyway even if it can read nothing.
1390 #define QEMU_VM_COMMAND 0x08
1391 c
= QEMU_VM_COMMAND
;
1392 ret
= send(pair2
[1], &c
, 1, 0);
1393 g_assert_cmpint(ret
, ==, 1);
1395 migrate_recover(to
, "fd:fd-mig");
1396 migrate_qmp(from
, to
, "fd:fd-mig", NULL
, "{'resume': true}");
1399 * Make sure both QEMU instances will go into RECOVER stage, then test
1400 * kicking them out using migrate-pause.
1402 wait_for_postcopy_status(from
, "postcopy-recover");
1403 wait_for_postcopy_status(to
, "postcopy-recover");
1406 * This would be issued by the admin upon noticing the hang, we should
1407 * make sure we're able to kick this out.
1409 migrate_pause(from
);
1410 wait_for_postcopy_status(from
, "postcopy-paused");
1412 /* Do the same test on dest */
1414 wait_for_postcopy_status(to
, "postcopy-paused");
1423 static void test_postcopy_recovery_common(MigrateCommon
*args
)
1425 QTestState
*from
, *to
;
1426 g_autofree
char *uri
= NULL
;
1428 /* Always hide errors for postcopy recover tests since they're expected */
1429 args
->start
.hide_stderr
= true;
1431 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1435 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1436 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
1438 /* Now we start the postcopy */
1439 migrate_postcopy_start(from
, to
);
1442 * Wait until postcopy is really started; we can only run the
1443 * migrate-pause command during a postcopy
1445 wait_for_migration_status(from
, "postcopy-active", NULL
);
1448 * Manually stop the postcopy migration. This emulates a network
1449 * failure with the migration socket
1451 migrate_pause(from
);
1454 * Wait for destination side to reach postcopy-paused state. The
1455 * migrate-recover command can only succeed if destination machine
1456 * is in the paused state
1458 wait_for_postcopy_status(to
, "postcopy-paused");
1459 wait_for_postcopy_status(from
, "postcopy-paused");
1462 if (args
->postcopy_recovery_test_fail
) {
1464 * Test when a wrong socket specified for recover, and then the
1465 * ability to kick it out, and continue with a correct socket.
1467 postcopy_recover_fail(from
, to
);
1468 /* continue with a good recovery */
1473 * Create a new socket to emulate a new channel that is different
1474 * from the broken migration channel; tell the destination to
1475 * listen to the new port
1477 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
1478 migrate_recover(to
, uri
);
1481 * Try to rebuild the migration channel using the resume flag and
1482 * the newly created channel
1484 migrate_qmp(from
, to
, uri
, NULL
, "{'resume': true}");
1486 /* Restore the postcopy bandwidth to unlimited */
1487 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
1489 migrate_postcopy_complete(from
, to
, args
);
1492 static void test_postcopy_recovery(void)
1494 MigrateCommon args
= { };
1496 test_postcopy_recovery_common(&args
);
1500 static void test_postcopy_recovery_double_fail(void)
1502 MigrateCommon args
= {
1503 .postcopy_recovery_test_fail
= true,
1506 test_postcopy_recovery_common(&args
);
1510 #ifdef CONFIG_GNUTLS
1511 static void test_postcopy_recovery_tls_psk(void)
1513 MigrateCommon args
= {
1514 .start_hook
= test_migrate_tls_psk_start_match
,
1515 .finish_hook
= test_migrate_tls_psk_finish
,
1518 test_postcopy_recovery_common(&args
);
1522 static void test_postcopy_preempt_recovery(void)
1524 MigrateCommon args
= {
1525 .postcopy_preempt
= true,
1528 test_postcopy_recovery_common(&args
);
1531 #ifdef CONFIG_GNUTLS
1532 /* This contains preempt+recovery+tls test altogether */
1533 static void test_postcopy_preempt_all(void)
1535 MigrateCommon args
= {
1536 .postcopy_preempt
= true,
1537 .start_hook
= test_migrate_tls_psk_start_match
,
1538 .finish_hook
= test_migrate_tls_psk_finish
,
1541 test_postcopy_recovery_common(&args
);
1546 static void test_baddest(void)
1548 MigrateStart args
= {
1551 QTestState
*from
, *to
;
1553 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1556 migrate_qmp(from
, to
, "tcp:127.0.0.1:0", NULL
, "{}");
1557 wait_for_migration_fail(from
, false);
1558 test_migrate_end(from
, to
, false);
1562 static void test_analyze_script(void)
1564 MigrateStart args
= {
1565 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
1567 QTestState
*from
, *to
;
1568 g_autofree
char *uri
= NULL
;
1569 g_autofree
char *file
= NULL
;
1571 const char *python
= g_getenv("PYTHON");
1574 g_test_skip("PYTHON variable not set");
1579 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1584 * Setting these two capabilities causes the "configuration"
1585 * vmstate to include subsections for them. The script needs to
1586 * parse those subsections properly.
1588 migrate_set_capability(from
, "validate-uuid", true);
1589 migrate_set_capability(from
, "x-ignore-shared", true);
1591 file
= g_strdup_printf("%s/migfile", tmpfs
);
1592 uri
= g_strdup_printf("exec:cat > %s", file
);
1594 migrate_ensure_converge(from
);
1595 migrate_qmp(from
, to
, uri
, NULL
, "{}");
1596 wait_for_migration_complete(from
);
1601 open("/dev/null", O_WRONLY
);
1602 execl(python
, python
, ANALYZE_SCRIPT
, "-f", file
, NULL
);
1603 g_assert_not_reached();
1606 g_assert(waitpid(pid
, &wstatus
, 0) == pid
);
1607 if (WIFEXITED(wstatus
) && WEXITSTATUS(wstatus
) != 0) {
1608 g_test_message("Failed to analyze the migration stream");
1611 test_migrate_end(from
, to
, false);
1616 static void test_precopy_common(MigrateCommon
*args
)
1618 QTestState
*from
, *to
;
1619 void *data_hook
= NULL
;
1621 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1625 if (args
->start_hook
) {
1626 data_hook
= args
->start_hook(from
, to
);
1629 /* Wait for the first serial output from the source */
1630 if (args
->result
== MIG_TEST_SUCCEED
) {
1631 wait_for_serial("src_serial");
1632 wait_for_suspend(from
, &src_state
);
1636 migrate_ensure_non_converge(from
);
1637 migrate_prepare_for_dirty_mem(from
);
1640 * Testing non-live migration, we allow it to run at
1641 * full speed to ensure short test case duration.
1642 * For tests expected to fail, we don't need to
1645 if (args
->result
== MIG_TEST_SUCCEED
) {
1646 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1647 wait_for_stop(from
, &src_state
);
1648 migrate_ensure_converge(from
);
1652 if (args
->result
== MIG_TEST_QMP_ERROR
) {
1653 migrate_qmp_fail(from
, args
->connect_uri
, args
->connect_channels
, "{}");
1657 migrate_qmp(from
, to
, args
->connect_uri
, args
->connect_channels
, "{}");
1659 if (args
->result
!= MIG_TEST_SUCCEED
) {
1660 bool allow_active
= args
->result
== MIG_TEST_FAIL
;
1661 wait_for_migration_fail(from
, allow_active
);
1663 if (args
->result
== MIG_TEST_FAIL_DEST_QUIT_ERR
) {
1664 qtest_set_expected_status(to
, EXIT_FAILURE
);
1669 * For initial iteration(s) we must do a full pass,
1670 * but for the final iteration, we need only wait
1671 * for some dirty mem before switching to converge
1673 while (args
->iterations
> 1) {
1674 wait_for_migration_pass(from
);
1677 migrate_wait_for_dirty_mem(from
, to
);
1679 migrate_ensure_converge(from
);
1682 * We do this first, as it has a timeout to stop us
1683 * hanging forever if migration didn't converge
1685 wait_for_migration_complete(from
);
1687 wait_for_stop(from
, &src_state
);
1690 wait_for_migration_complete(from
);
1692 * Must wait for dst to finish reading all incoming
1693 * data on the socket before issuing 'cont' otherwise
1696 wait_for_migration_complete(to
);
1698 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1701 wait_for_resume(to
, &dst_state
);
1703 if (args
->start
.suspend_me
) {
1704 /* wakeup succeeds only if guest is suspended */
1705 qtest_qmp_assert_success(to
, "{'execute': 'system_wakeup'}");
1708 wait_for_serial("dest_serial");
1712 if (args
->finish_hook
) {
1713 args
->finish_hook(from
, to
, data_hook
);
1716 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1719 static void test_file_common(MigrateCommon
*args
, bool stop_src
)
1721 QTestState
*from
, *to
;
1722 void *data_hook
= NULL
;
1724 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1729 * File migration is never live. We can keep the source VM running
1730 * during migration, but the destination will not be running
1733 g_assert_false(args
->live
);
1735 if (args
->start_hook
) {
1736 data_hook
= args
->start_hook(from
, to
);
1739 migrate_ensure_converge(from
);
1740 wait_for_serial("src_serial");
1743 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1744 wait_for_stop(from
, &src_state
);
1747 if (args
->result
== MIG_TEST_QMP_ERROR
) {
1748 migrate_qmp_fail(from
, args
->connect_uri
, NULL
, "{}");
1752 migrate_qmp(from
, to
, args
->connect_uri
, NULL
, "{}");
1753 wait_for_migration_complete(from
);
1756 * We need to wait for the source to finish before starting the
1759 migrate_incoming_qmp(to
, args
->connect_uri
, "{}");
1760 wait_for_migration_complete(to
);
1763 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1765 wait_for_resume(to
, &dst_state
);
1767 wait_for_serial("dest_serial");
1770 if (args
->finish_hook
) {
1771 args
->finish_hook(from
, to
, data_hook
);
1774 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1777 static void test_precopy_unix_plain(void)
1779 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1780 MigrateCommon args
= {
1784 * The simplest use case of precopy, covering smoke tests of
1785 * get-dirty-log dirty tracking.
1790 test_precopy_common(&args
);
1793 static void test_precopy_unix_suspend_live(void)
1795 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1796 MigrateCommon args
= {
1800 * despite being live, the test is fast because the src
1801 * suspends immediately.
1804 .start
.suspend_me
= true,
1807 test_precopy_common(&args
);
1810 static void test_precopy_unix_suspend_notlive(void)
1812 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1813 MigrateCommon args
= {
1816 .start
.suspend_me
= true,
1819 test_precopy_common(&args
);
1822 static void test_precopy_unix_dirty_ring(void)
1824 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1825 MigrateCommon args
= {
1827 .use_dirty_ring
= true,
1832 * Besides the precopy/unix basic test, cover dirty ring interface
1833 * rather than get-dirty-log.
1838 test_precopy_common(&args
);
1841 #ifdef CONFIG_GNUTLS
1842 static void test_precopy_unix_tls_psk(void)
1844 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1845 MigrateCommon args
= {
1848 .start_hook
= test_migrate_tls_psk_start_match
,
1849 .finish_hook
= test_migrate_tls_psk_finish
,
1852 test_precopy_common(&args
);
1856 static void test_precopy_unix_tls_x509_default_host(void)
1858 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1859 MigrateCommon args
= {
1861 .hide_stderr
= true,
1865 .start_hook
= test_migrate_tls_x509_start_default_host
,
1866 .finish_hook
= test_migrate_tls_x509_finish
,
1867 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1870 test_precopy_common(&args
);
1873 static void test_precopy_unix_tls_x509_override_host(void)
1875 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1876 MigrateCommon args
= {
1879 .start_hook
= test_migrate_tls_x509_start_override_host
,
1880 .finish_hook
= test_migrate_tls_x509_finish
,
1883 test_precopy_common(&args
);
1885 #endif /* CONFIG_TASN1 */
1886 #endif /* CONFIG_GNUTLS */
1889 /* Currently upset on aarch64 TCG */
1890 static void test_ignore_shared(void)
1892 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1893 QTestState
*from
, *to
;
1895 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
1899 migrate_ensure_non_converge(from
);
1900 migrate_prepare_for_dirty_mem(from
);
1902 migrate_set_capability(from
, "x-ignore-shared", true);
1903 migrate_set_capability(to
, "x-ignore-shared", true);
1905 /* Wait for the first serial output from the source */
1906 wait_for_serial("src_serial");
1908 migrate_qmp(from
, to
, uri
, NULL
, "{}");
1910 migrate_wait_for_dirty_mem(from
, to
);
1912 wait_for_stop(from
, &src_state
);
1914 qtest_qmp_eventwait(to
, "RESUME");
1916 wait_for_serial("dest_serial");
1917 wait_for_migration_complete(from
);
1919 /* Check whether shared RAM has been really skipped */
1920 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1922 test_migrate_end(from
, to
, true);
1927 test_migrate_xbzrle_start(QTestState
*from
,
1930 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1932 migrate_set_capability(from
, "xbzrle", true);
1933 migrate_set_capability(to
, "xbzrle", true);
1938 static void test_precopy_unix_xbzrle(void)
1940 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1941 MigrateCommon args
= {
1944 .start_hook
= test_migrate_xbzrle_start
,
1947 * XBZRLE needs pages to be modified when doing the 2nd+ round
1948 * iteration to have real data pushed to the stream.
1953 test_precopy_common(&args
);
1956 static void test_precopy_file(void)
1958 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
1959 FILE_TEST_FILENAME
);
1960 MigrateCommon args
= {
1962 .listen_uri
= "defer",
1965 test_file_common(&args
, true);
1968 static void file_offset_finish_hook(QTestState
*from
, QTestState
*to
,
1971 #if defined(__linux__)
1972 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, FILE_TEST_FILENAME
);
1973 size_t size
= FILE_TEST_OFFSET
+ sizeof(QEMU_VM_FILE_MAGIC
);
1974 uintptr_t *addr
, *p
;
1977 fd
= open(path
, O_RDONLY
);
1979 addr
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0);
1980 g_assert(addr
!= MAP_FAILED
);
1983 * Ensure the skipped offset contains zeros and the migration
1984 * stream starts at the right place.
1987 while (p
< addr
+ FILE_TEST_OFFSET
/ sizeof(uintptr_t)) {
1991 g_assert_cmpint(cpu_to_be64(*p
) >> 32, ==, QEMU_VM_FILE_MAGIC
);
1998 static void test_precopy_file_offset(void)
2000 g_autofree
char *uri
= g_strdup_printf("file:%s/%s,offset=%d", tmpfs
,
2003 MigrateCommon args
= {
2005 .listen_uri
= "defer",
2006 .finish_hook
= file_offset_finish_hook
,
2009 test_file_common(&args
, false);
2012 static void test_precopy_file_offset_bad(void)
2014 /* using a value not supported by qemu_strtosz() */
2015 g_autofree
char *uri
= g_strdup_printf("file:%s/%s,offset=0x20M",
2016 tmpfs
, FILE_TEST_FILENAME
);
2017 MigrateCommon args
= {
2019 .listen_uri
= "defer",
2020 .result
= MIG_TEST_QMP_ERROR
,
2023 test_file_common(&args
, false);
2026 static void *test_mode_reboot_start(QTestState
*from
, QTestState
*to
)
2028 migrate_set_parameter_str(from
, "mode", "cpr-reboot");
2029 migrate_set_parameter_str(to
, "mode", "cpr-reboot");
2031 migrate_set_capability(from
, "x-ignore-shared", true);
2032 migrate_set_capability(to
, "x-ignore-shared", true);
2037 static void *migrate_mapped_ram_start(QTestState
*from
, QTestState
*to
)
2039 migrate_set_capability(from
, "mapped-ram", true);
2040 migrate_set_capability(to
, "mapped-ram", true);
2045 static void test_mode_reboot(void)
2047 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
2048 FILE_TEST_FILENAME
);
2049 MigrateCommon args
= {
2050 .start
.use_shmem
= true,
2052 .listen_uri
= "defer",
2053 .start_hook
= test_mode_reboot_start
2056 test_file_common(&args
, true);
2059 static void test_precopy_file_mapped_ram_live(void)
2061 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
2062 FILE_TEST_FILENAME
);
2063 MigrateCommon args
= {
2065 .listen_uri
= "defer",
2066 .start_hook
= migrate_mapped_ram_start
,
2069 test_file_common(&args
, false);
2072 static void test_precopy_file_mapped_ram(void)
2074 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
2075 FILE_TEST_FILENAME
);
2076 MigrateCommon args
= {
2078 .listen_uri
= "defer",
2079 .start_hook
= migrate_mapped_ram_start
,
2082 test_file_common(&args
, true);
2085 static void *migrate_multifd_mapped_ram_start(QTestState
*from
, QTestState
*to
)
2087 migrate_mapped_ram_start(from
, to
);
2089 migrate_set_parameter_int(from
, "multifd-channels", 4);
2090 migrate_set_parameter_int(to
, "multifd-channels", 4);
2092 migrate_set_capability(from
, "multifd", true);
2093 migrate_set_capability(to
, "multifd", true);
2098 static void test_multifd_file_mapped_ram_live(void)
2100 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
2101 FILE_TEST_FILENAME
);
2102 MigrateCommon args
= {
2104 .listen_uri
= "defer",
2105 .start_hook
= migrate_multifd_mapped_ram_start
,
2108 test_file_common(&args
, false);
2111 static void test_multifd_file_mapped_ram(void)
2113 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
2114 FILE_TEST_FILENAME
);
2115 MigrateCommon args
= {
2117 .listen_uri
= "defer",
2118 .start_hook
= migrate_multifd_mapped_ram_start
,
2121 test_file_common(&args
, true);
2125 static void test_precopy_tcp_plain(void)
2127 MigrateCommon args
= {
2128 .listen_uri
= "tcp:127.0.0.1:0",
2131 test_precopy_common(&args
);
2134 static void *test_migrate_switchover_ack_start(QTestState
*from
, QTestState
*to
)
2137 migrate_set_capability(from
, "return-path", true);
2138 migrate_set_capability(to
, "return-path", true);
2140 migrate_set_capability(from
, "switchover-ack", true);
2141 migrate_set_capability(to
, "switchover-ack", true);
2146 static void test_precopy_tcp_switchover_ack(void)
2148 MigrateCommon args
= {
2149 .listen_uri
= "tcp:127.0.0.1:0",
2150 .start_hook
= test_migrate_switchover_ack_start
,
2152 * Source VM must be running in order to consider the switchover ACK
2153 * when deciding to do switchover or not.
2158 test_precopy_common(&args
);
2161 #ifdef CONFIG_GNUTLS
2162 static void test_precopy_tcp_tls_psk_match(void)
2164 MigrateCommon args
= {
2165 .listen_uri
= "tcp:127.0.0.1:0",
2166 .start_hook
= test_migrate_tls_psk_start_match
,
2167 .finish_hook
= test_migrate_tls_psk_finish
,
2170 test_precopy_common(&args
);
2173 static void test_precopy_tcp_tls_psk_mismatch(void)
2175 MigrateCommon args
= {
2177 .hide_stderr
= true,
2179 .listen_uri
= "tcp:127.0.0.1:0",
2180 .start_hook
= test_migrate_tls_psk_start_mismatch
,
2181 .finish_hook
= test_migrate_tls_psk_finish
,
2182 .result
= MIG_TEST_FAIL
,
2185 test_precopy_common(&args
);
2189 static void test_precopy_tcp_tls_x509_default_host(void)
2191 MigrateCommon args
= {
2192 .listen_uri
= "tcp:127.0.0.1:0",
2193 .start_hook
= test_migrate_tls_x509_start_default_host
,
2194 .finish_hook
= test_migrate_tls_x509_finish
,
2197 test_precopy_common(&args
);
2200 static void test_precopy_tcp_tls_x509_override_host(void)
2202 MigrateCommon args
= {
2203 .listen_uri
= "tcp:127.0.0.1:0",
2204 .start_hook
= test_migrate_tls_x509_start_override_host
,
2205 .finish_hook
= test_migrate_tls_x509_finish
,
2208 test_precopy_common(&args
);
2211 static void test_precopy_tcp_tls_x509_mismatch_host(void)
2213 MigrateCommon args
= {
2215 .hide_stderr
= true,
2217 .listen_uri
= "tcp:127.0.0.1:0",
2218 .start_hook
= test_migrate_tls_x509_start_mismatch_host
,
2219 .finish_hook
= test_migrate_tls_x509_finish
,
2220 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
2223 test_precopy_common(&args
);
2226 static void test_precopy_tcp_tls_x509_friendly_client(void)
2228 MigrateCommon args
= {
2229 .listen_uri
= "tcp:127.0.0.1:0",
2230 .start_hook
= test_migrate_tls_x509_start_friendly_client
,
2231 .finish_hook
= test_migrate_tls_x509_finish
,
2234 test_precopy_common(&args
);
2237 static void test_precopy_tcp_tls_x509_hostile_client(void)
2239 MigrateCommon args
= {
2241 .hide_stderr
= true,
2243 .listen_uri
= "tcp:127.0.0.1:0",
2244 .start_hook
= test_migrate_tls_x509_start_hostile_client
,
2245 .finish_hook
= test_migrate_tls_x509_finish
,
2246 .result
= MIG_TEST_FAIL
,
2249 test_precopy_common(&args
);
2252 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2254 MigrateCommon args
= {
2255 .listen_uri
= "tcp:127.0.0.1:0",
2256 .start_hook
= test_migrate_tls_x509_start_allow_anon_client
,
2257 .finish_hook
= test_migrate_tls_x509_finish
,
2260 test_precopy_common(&args
);
2263 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2265 MigrateCommon args
= {
2267 .hide_stderr
= true,
2269 .listen_uri
= "tcp:127.0.0.1:0",
2270 .start_hook
= test_migrate_tls_x509_start_reject_anon_client
,
2271 .finish_hook
= test_migrate_tls_x509_finish
,
2272 .result
= MIG_TEST_FAIL
,
2275 test_precopy_common(&args
);
2277 #endif /* CONFIG_TASN1 */
2278 #endif /* CONFIG_GNUTLS */
2281 static void *test_migrate_fd_start_hook(QTestState
*from
,
2287 /* Create two connected sockets for migration */
2288 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
2289 g_assert_cmpint(ret
, ==, 0);
2291 /* Send the 1st socket to the target */
2292 qtest_qmp_fds_assert_success(to
, &pair
[0], 1,
2293 "{ 'execute': 'getfd',"
2294 " 'arguments': { 'fdname': 'fd-mig' }}");
2297 /* Start incoming migration from the 1st socket */
2298 migrate_incoming_qmp(to
, "fd:fd-mig", "{}");
2300 /* Send the 2nd socket to the target */
2301 qtest_qmp_fds_assert_success(from
, &pair
[1], 1,
2302 "{ 'execute': 'getfd',"
2303 " 'arguments': { 'fdname': 'fd-mig' }}");
2309 static void test_migrate_fd_finish_hook(QTestState
*from
,
2314 const char *error_desc
;
2316 /* Test closing fds */
2317 /* We assume, that QEMU removes named fd from its list,
2318 * so this should fail */
2319 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
2320 " 'arguments': { 'fdname': 'fd-mig' }}");
2321 g_assert_true(qdict_haskey(rsp
, "error"));
2322 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2323 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2326 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
2327 " 'arguments': { 'fdname': 'fd-mig' }}");
2328 g_assert_true(qdict_haskey(rsp
, "error"));
2329 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2330 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2334 static void test_migrate_precopy_fd_socket(void)
2336 MigrateCommon args
= {
2337 .listen_uri
= "defer",
2338 .connect_uri
= "fd:fd-mig",
2339 .start_hook
= test_migrate_fd_start_hook
,
2340 .finish_hook
= test_migrate_fd_finish_hook
2342 test_precopy_common(&args
);
2345 static void *migrate_precopy_fd_file_start(QTestState
*from
, QTestState
*to
)
2347 g_autofree
char *file
= g_strdup_printf("%s/%s", tmpfs
, FILE_TEST_FILENAME
);
2348 int src_flags
= O_CREAT
| O_RDWR
;
2349 int dst_flags
= O_CREAT
| O_RDWR
;
2352 fds
[0] = open(file
, src_flags
, 0660);
2353 assert(fds
[0] != -1);
2355 fds
[1] = open(file
, dst_flags
, 0660);
2356 assert(fds
[1] != -1);
2359 qtest_qmp_fds_assert_success(to
, &fds
[0], 1,
2360 "{ 'execute': 'getfd',"
2361 " 'arguments': { 'fdname': 'fd-mig' }}");
2363 qtest_qmp_fds_assert_success(from
, &fds
[1], 1,
2364 "{ 'execute': 'getfd',"
2365 " 'arguments': { 'fdname': 'fd-mig' }}");
2373 static void test_migrate_precopy_fd_file(void)
2375 MigrateCommon args
= {
2376 .listen_uri
= "defer",
2377 .connect_uri
= "fd:fd-mig",
2378 .start_hook
= migrate_precopy_fd_file_start
,
2379 .finish_hook
= test_migrate_fd_finish_hook
2381 test_file_common(&args
, true);
2385 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
2387 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2388 QTestState
*from
, *to
;
2390 if (test_migrate_start(&from
, &to
, uri
, args
)) {
2395 * UUID validation is at the begin of migration. So, the main process of
2396 * migration is not interesting for us here. Thus, set huge downtime for
2397 * very fast migration.
2399 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
2400 migrate_set_capability(from
, "validate-uuid", true);
2402 /* Wait for the first serial output from the source */
2403 wait_for_serial("src_serial");
2405 migrate_qmp(from
, to
, uri
, NULL
, "{}");
2408 qtest_set_expected_status(to
, EXIT_FAILURE
);
2409 wait_for_migration_fail(from
, true);
2411 wait_for_migration_complete(from
);
2414 test_migrate_end(from
, to
, false);
2417 static void test_validate_uuid(void)
2419 MigrateStart args
= {
2420 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2421 .opts_target
= "-uuid 11111111-1111-1111-1111-111111111111",
2424 do_test_validate_uuid(&args
, false);
2427 static void test_validate_uuid_error(void)
2429 MigrateStart args
= {
2430 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2431 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2432 .hide_stderr
= true,
2435 do_test_validate_uuid(&args
, true);
2438 static void test_validate_uuid_src_not_set(void)
2440 MigrateStart args
= {
2441 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2442 .hide_stderr
= true,
2445 do_test_validate_uuid(&args
, false);
2448 static void test_validate_uuid_dst_not_set(void)
2450 MigrateStart args
= {
2451 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2452 .hide_stderr
= true,
2455 do_test_validate_uuid(&args
, false);
2458 static void do_test_validate_uri_channel(MigrateCommon
*args
)
2460 QTestState
*from
, *to
;
2462 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
2466 /* Wait for the first serial output from the source */
2467 wait_for_serial("src_serial");
2470 * 'uri' and 'channels' validation is checked even before the migration
2473 migrate_qmp_fail(from
, args
->connect_uri
, args
->connect_channels
, "{}");
2474 test_migrate_end(from
, to
, false);
2477 static void test_validate_uri_channels_both_set(void)
2479 MigrateCommon args
= {
2481 .hide_stderr
= true,
2483 .listen_uri
= "defer",
2484 .connect_uri
= "tcp:127.0.0.1:0",
2485 .connect_channels
= "[ { 'channel-type': 'main',"
2486 " 'addr': { 'transport': 'socket',"
2488 " 'host': '127.0.0.1',"
2489 " 'port': '0' } } ]",
2492 do_test_validate_uri_channel(&args
);
2495 static void test_validate_uri_channels_none_set(void)
2497 MigrateCommon args
= {
2499 .hide_stderr
= true,
2501 .listen_uri
= "defer",
2504 do_test_validate_uri_channel(&args
);
2508 * The way auto_converge works, we need to do too many passes to
2509 * run this test. Auto_converge logic is only run once every
2510 * three iterations, so:
2512 * - 3 iterations without auto_converge enabled
2513 * - 3 iterations with pct = 5
2514 * - 3 iterations with pct = 30
2515 * - 3 iterations with pct = 55
2516 * - 3 iterations with pct = 80
2517 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2519 * To make things even worse, we need to run the initial stage at
2520 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2522 static void test_migrate_auto_converge(void)
2524 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2525 MigrateStart args
= {};
2526 QTestState
*from
, *to
;
2530 * We want the test to be stable and as fast as possible.
2531 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2532 * so we need to decrease a bandwidth.
2534 const int64_t init_pct
= 5, inc_pct
= 25, max_pct
= 95;
2536 if (test_migrate_start(&from
, &to
, uri
, &args
)) {
2540 migrate_set_capability(from
, "auto-converge", true);
2541 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
2542 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
2543 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
2546 * Set the initial parameters so that the migration could not converge
2547 * without throttling.
2549 migrate_ensure_non_converge(from
);
2551 /* To check remaining size after precopy */
2552 migrate_set_capability(from
, "pause-before-switchover", true);
2554 /* Wait for the first serial output from the source */
2555 wait_for_serial("src_serial");
2557 migrate_qmp(from
, to
, uri
, NULL
, "{}");
2559 /* Wait for throttling begins */
2562 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2563 if (percentage
!= 0) {
2567 g_assert_false(src_state
.stop_seen
);
2569 /* The first percentage of throttling should be at least init_pct */
2570 g_assert_cmpint(percentage
, >=, init_pct
);
2571 /* Now, when we tested that throttling works, let it converge */
2572 migrate_ensure_converge(from
);
2575 * Wait for pre-switchover status to check last throttle percentage
2576 * and remaining. These values will be zeroed later
2578 wait_for_migration_status(from
, "pre-switchover", NULL
);
2580 /* The final percentage of throttling shouldn't be greater than max_pct */
2581 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2582 g_assert_cmpint(percentage
, <=, max_pct
);
2583 migrate_continue(from
, "pre-switchover");
2585 qtest_qmp_eventwait(to
, "RESUME");
2587 wait_for_serial("dest_serial");
2588 wait_for_migration_complete(from
);
2590 test_migrate_end(from
, to
, true);
2594 test_migrate_precopy_tcp_multifd_start_common(QTestState
*from
,
2598 migrate_set_parameter_int(from
, "multifd-channels", 16);
2599 migrate_set_parameter_int(to
, "multifd-channels", 16);
2601 migrate_set_parameter_str(from
, "multifd-compression", method
);
2602 migrate_set_parameter_str(to
, "multifd-compression", method
);
2604 migrate_set_capability(from
, "multifd", true);
2605 migrate_set_capability(to
, "multifd", true);
2607 /* Start incoming migration from the 1st socket */
2608 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2614 test_migrate_precopy_tcp_multifd_start(QTestState
*from
,
2617 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2621 test_migrate_precopy_tcp_multifd_start_zero_page_legacy(QTestState
*from
,
2624 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2625 migrate_set_parameter_str(from
, "zero-page-detection", "legacy");
2630 test_migration_precopy_tcp_multifd_start_no_zero_page(QTestState
*from
,
2633 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2634 migrate_set_parameter_str(from
, "zero-page-detection", "none");
2639 test_migrate_precopy_tcp_multifd_zlib_start(QTestState
*from
,
2643 * Overloading this test to also check that set_parameter does not error.
2644 * This is also done in the tests for the other compression methods.
2646 migrate_set_parameter_int(from
, "multifd-zlib-level", 2);
2647 migrate_set_parameter_int(to
, "multifd-zlib-level", 2);
2649 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zlib");
2654 test_migrate_precopy_tcp_multifd_zstd_start(QTestState
*from
,
2657 migrate_set_parameter_int(from
, "multifd-zstd-level", 2);
2658 migrate_set_parameter_int(to
, "multifd-zstd-level", 2);
2660 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zstd");
2662 #endif /* CONFIG_ZSTD */
2664 static void test_multifd_tcp_uri_none(void)
2666 MigrateCommon args
= {
2667 .listen_uri
= "defer",
2668 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2670 * Multifd is more complicated than most of the features, it
2671 * directly takes guest page buffers when sending, make sure
2672 * everything will work alright even if guest page is changing.
2676 test_precopy_common(&args
);
2679 static void test_multifd_tcp_zero_page_legacy(void)
2681 MigrateCommon args
= {
2682 .listen_uri
= "defer",
2683 .start_hook
= test_migrate_precopy_tcp_multifd_start_zero_page_legacy
,
2685 * Multifd is more complicated than most of the features, it
2686 * directly takes guest page buffers when sending, make sure
2687 * everything will work alright even if guest page is changing.
2691 test_precopy_common(&args
);
2694 static void test_multifd_tcp_no_zero_page(void)
2696 MigrateCommon args
= {
2697 .listen_uri
= "defer",
2698 .start_hook
= test_migration_precopy_tcp_multifd_start_no_zero_page
,
2700 * Multifd is more complicated than most of the features, it
2701 * directly takes guest page buffers when sending, make sure
2702 * everything will work alright even if guest page is changing.
2706 test_precopy_common(&args
);
2709 static void test_multifd_tcp_channels_none(void)
2711 MigrateCommon args
= {
2712 .listen_uri
= "defer",
2713 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2715 .connect_channels
= "[ { 'channel-type': 'main',"
2716 " 'addr': { 'transport': 'socket',"
2718 " 'host': '127.0.0.1',"
2719 " 'port': '0' } } ]",
2721 test_precopy_common(&args
);
2724 static void test_multifd_tcp_zlib(void)
2726 MigrateCommon args
= {
2727 .listen_uri
= "defer",
2728 .start_hook
= test_migrate_precopy_tcp_multifd_zlib_start
,
2730 test_precopy_common(&args
);
2734 static void test_multifd_tcp_zstd(void)
2736 MigrateCommon args
= {
2737 .listen_uri
= "defer",
2738 .start_hook
= test_migrate_precopy_tcp_multifd_zstd_start
,
2740 test_precopy_common(&args
);
2744 #ifdef CONFIG_GNUTLS
2746 test_migrate_multifd_tcp_tls_psk_start_match(QTestState
*from
,
2749 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2750 return test_migrate_tls_psk_start_match(from
, to
);
2754 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState
*from
,
2757 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2758 return test_migrate_tls_psk_start_mismatch(from
, to
);
2763 test_migrate_multifd_tls_x509_start_default_host(QTestState
*from
,
2766 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2767 return test_migrate_tls_x509_start_default_host(from
, to
);
2771 test_migrate_multifd_tls_x509_start_override_host(QTestState
*from
,
2774 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2775 return test_migrate_tls_x509_start_override_host(from
, to
);
2779 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState
*from
,
2782 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2783 return test_migrate_tls_x509_start_mismatch_host(from
, to
);
2787 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState
*from
,
2790 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2791 return test_migrate_tls_x509_start_allow_anon_client(from
, to
);
2795 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState
*from
,
2798 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2799 return test_migrate_tls_x509_start_reject_anon_client(from
, to
);
2801 #endif /* CONFIG_TASN1 */
2803 static void test_multifd_tcp_tls_psk_match(void)
2805 MigrateCommon args
= {
2806 .listen_uri
= "defer",
2807 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_match
,
2808 .finish_hook
= test_migrate_tls_psk_finish
,
2810 test_precopy_common(&args
);
2813 static void test_multifd_tcp_tls_psk_mismatch(void)
2815 MigrateCommon args
= {
2817 .hide_stderr
= true,
2819 .listen_uri
= "defer",
2820 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_mismatch
,
2821 .finish_hook
= test_migrate_tls_psk_finish
,
2822 .result
= MIG_TEST_FAIL
,
2824 test_precopy_common(&args
);
2828 static void test_multifd_tcp_tls_x509_default_host(void)
2830 MigrateCommon args
= {
2831 .listen_uri
= "defer",
2832 .start_hook
= test_migrate_multifd_tls_x509_start_default_host
,
2833 .finish_hook
= test_migrate_tls_x509_finish
,
2835 test_precopy_common(&args
);
2838 static void test_multifd_tcp_tls_x509_override_host(void)
2840 MigrateCommon args
= {
2841 .listen_uri
= "defer",
2842 .start_hook
= test_migrate_multifd_tls_x509_start_override_host
,
2843 .finish_hook
= test_migrate_tls_x509_finish
,
2845 test_precopy_common(&args
);
2848 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2851 * This has different behaviour to the non-multifd case.
2853 * In non-multifd case when client aborts due to mismatched
2854 * cert host, the server has already started trying to load
2855 * migration state, and so it exits with I/O failure.
2857 * In multifd case when client aborts due to mismatched
2858 * cert host, the server is still waiting for the other
2859 * multifd connections to arrive so hasn't started trying
2860 * to load migration state, and thus just aborts the migration
2863 MigrateCommon args
= {
2865 .hide_stderr
= true,
2867 .listen_uri
= "defer",
2868 .start_hook
= test_migrate_multifd_tls_x509_start_mismatch_host
,
2869 .finish_hook
= test_migrate_tls_x509_finish
,
2870 .result
= MIG_TEST_FAIL
,
2872 test_precopy_common(&args
);
2875 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2877 MigrateCommon args
= {
2878 .listen_uri
= "defer",
2879 .start_hook
= test_migrate_multifd_tls_x509_start_allow_anon_client
,
2880 .finish_hook
= test_migrate_tls_x509_finish
,
2882 test_precopy_common(&args
);
2885 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2887 MigrateCommon args
= {
2889 .hide_stderr
= true,
2891 .listen_uri
= "defer",
2892 .start_hook
= test_migrate_multifd_tls_x509_start_reject_anon_client
,
2893 .finish_hook
= test_migrate_tls_x509_finish
,
2894 .result
= MIG_TEST_FAIL
,
2896 test_precopy_common(&args
);
2898 #endif /* CONFIG_TASN1 */
2899 #endif /* CONFIG_GNUTLS */
2907 * launch another target
2910 * And see that it works
2912 static void test_multifd_tcp_cancel(void)
2914 MigrateStart args
= {
2915 .hide_stderr
= true,
2917 QTestState
*from
, *to
, *to2
;
2919 if (test_migrate_start(&from
, &to
, "defer", &args
)) {
2923 migrate_ensure_non_converge(from
);
2924 migrate_prepare_for_dirty_mem(from
);
2926 migrate_set_parameter_int(from
, "multifd-channels", 16);
2927 migrate_set_parameter_int(to
, "multifd-channels", 16);
2929 migrate_set_capability(from
, "multifd", true);
2930 migrate_set_capability(to
, "multifd", true);
2932 /* Start incoming migration from the 1st socket */
2933 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2935 /* Wait for the first serial output from the source */
2936 wait_for_serial("src_serial");
2938 migrate_qmp(from
, to
, NULL
, NULL
, "{}");
2940 migrate_wait_for_dirty_mem(from
, to
);
2942 migrate_cancel(from
);
2944 /* Make sure QEMU process "to" exited */
2945 qtest_set_expected_status(to
, EXIT_FAILURE
);
2946 qtest_wait_qemu(to
);
2948 args
= (MigrateStart
){
2949 .only_target
= true,
2952 if (test_migrate_start(&from
, &to2
, "defer", &args
)) {
2956 migrate_set_parameter_int(to2
, "multifd-channels", 16);
2958 migrate_set_capability(to2
, "multifd", true);
2960 /* Start incoming migration from the 1st socket */
2961 migrate_incoming_qmp(to2
, "tcp:127.0.0.1:0", "{}");
2963 wait_for_migration_status(from
, "cancelled", NULL
);
2965 migrate_ensure_non_converge(from
);
2967 migrate_qmp(from
, to2
, NULL
, NULL
, "{}");
2969 migrate_wait_for_dirty_mem(from
, to2
);
2971 migrate_ensure_converge(from
);
2973 wait_for_stop(from
, &src_state
);
2974 qtest_qmp_eventwait(to2
, "RESUME");
2976 wait_for_serial("dest_serial");
2977 wait_for_migration_complete(from
);
2978 test_migrate_end(from
, to2
, true);
2981 static void calc_dirty_rate(QTestState
*who
, uint64_t calc_time
)
2983 qtest_qmp_assert_success(who
,
2984 "{ 'execute': 'calc-dirty-rate',"
2986 "'calc-time': %" PRIu64
","
2987 "'mode': 'dirty-ring' }}",
2991 static QDict
*query_dirty_rate(QTestState
*who
)
2993 return qtest_qmp_assert_success_ref(who
,
2994 "{ 'execute': 'query-dirty-rate' }");
2997 static void dirtylimit_set_all(QTestState
*who
, uint64_t dirtyrate
)
2999 qtest_qmp_assert_success(who
,
3000 "{ 'execute': 'set-vcpu-dirty-limit',"
3002 "'dirty-rate': %" PRIu64
" } }",
3006 static void cancel_vcpu_dirty_limit(QTestState
*who
)
3008 qtest_qmp_assert_success(who
,
3009 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
3012 static QDict
*query_vcpu_dirty_limit(QTestState
*who
)
3016 rsp
= qtest_qmp(who
, "{ 'execute': 'query-vcpu-dirty-limit' }");
3017 g_assert(!qdict_haskey(rsp
, "error"));
3018 g_assert(qdict_haskey(rsp
, "return"));
3023 static bool calc_dirtyrate_ready(QTestState
*who
)
3028 rsp_return
= query_dirty_rate(who
);
3029 g_assert(rsp_return
);
3031 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
3034 return g_strcmp0(status
, "measuring");
3037 static void wait_for_calc_dirtyrate_complete(QTestState
*who
,
3040 int max_try_count
= 10000;
3041 usleep(time_s
* 1000000);
3043 while (!calc_dirtyrate_ready(who
) && max_try_count
--) {
3048 * Set the timeout with 10 s(max_try_count * 1000us),
3049 * if dirtyrate measurement not complete, fail test.
3051 g_assert_cmpint(max_try_count
, !=, 0);
3054 static int64_t get_dirty_rate(QTestState
*who
)
3059 const QListEntry
*entry
;
3063 rsp_return
= query_dirty_rate(who
);
3064 g_assert(rsp_return
);
3066 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
3068 g_assert_cmpstr(status
, ==, "measured");
3070 rates
= qdict_get_qlist(rsp_return
, "vcpu-dirty-rate");
3071 g_assert(rates
&& !qlist_empty(rates
));
3073 entry
= qlist_first(rates
);
3076 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
3079 dirtyrate
= qdict_get_try_int(rate
, "dirty-rate", -1);
3081 qobject_unref(rsp_return
);
3085 static int64_t get_limit_rate(QTestState
*who
)
3089 const QListEntry
*entry
;
3093 rsp_return
= query_vcpu_dirty_limit(who
);
3094 g_assert(rsp_return
);
3096 rates
= qdict_get_qlist(rsp_return
, "return");
3097 g_assert(rates
&& !qlist_empty(rates
));
3099 entry
= qlist_first(rates
);
3102 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
3105 dirtyrate
= qdict_get_try_int(rate
, "limit-rate", -1);
3107 qobject_unref(rsp_return
);
3111 static QTestState
*dirtylimit_start_vm(void)
3113 QTestState
*vm
= NULL
;
3114 g_autofree gchar
*cmd
= NULL
;
3116 bootfile_create(tmpfs
, false);
3117 cmd
= g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
3118 "-name dirtylimit-test,debug-threads=on "
3120 "-serial file:%s/vm_serial "
3121 "-drive file=%s,format=raw ",
3124 vm
= qtest_init(cmd
);
3128 static void dirtylimit_stop_vm(QTestState
*vm
)
3131 cleanup("vm_serial");
3134 static void test_vcpu_dirty_limit(void)
3137 int64_t origin_rate
;
3140 int max_try_count
= 20;
3143 /* Start vm for vcpu dirtylimit test */
3144 vm
= dirtylimit_start_vm();
3146 /* Wait for the first serial output from the vm*/
3147 wait_for_serial("vm_serial");
3149 /* Do dirtyrate measurement with calc time equals 1s */
3150 calc_dirty_rate(vm
, 1);
3152 /* Sleep calc time and wait for calc dirtyrate complete */
3153 wait_for_calc_dirtyrate_complete(vm
, 1);
3155 /* Query original dirty page rate */
3156 origin_rate
= get_dirty_rate(vm
);
3158 /* VM booted from bootsect should dirty memory steadily */
3159 assert(origin_rate
!= 0);
3161 /* Setup quota dirty page rate at half of origin */
3162 quota_rate
= origin_rate
/ 2;
3164 /* Set dirtylimit */
3165 dirtylimit_set_all(vm
, quota_rate
);
3168 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
3171 g_assert_cmpint(quota_rate
, ==, get_limit_rate(vm
));
3173 /* Sleep a bit to check if it take effect */
3177 * Check if dirtylimit take effect realistically, set the
3178 * timeout with 20 s(max_try_count * 1s), if dirtylimit
3179 * doesn't take effect, fail test.
3181 while (--max_try_count
) {
3182 calc_dirty_rate(vm
, 1);
3183 wait_for_calc_dirtyrate_complete(vm
, 1);
3184 rate
= get_dirty_rate(vm
);
3187 * Assume hitting if current rate is less
3188 * than quota rate (within accepting error)
3190 if (rate
< (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
3196 g_assert_cmpint(hit
, ==, 1);
3201 /* Check if dirtylimit cancellation take effect */
3202 cancel_vcpu_dirty_limit(vm
);
3203 while (--max_try_count
) {
3204 calc_dirty_rate(vm
, 1);
3205 wait_for_calc_dirtyrate_complete(vm
, 1);
3206 rate
= get_dirty_rate(vm
);
3209 * Assume dirtylimit be canceled if current rate is
3210 * greater than quota rate (within accepting error)
3212 if (rate
> (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
3218 g_assert_cmpint(hit
, ==, 1);
3219 dirtylimit_stop_vm(vm
);
3222 static void migrate_dirty_limit_wait_showup(QTestState
*from
,
3223 const int64_t period
,
3224 const int64_t value
)
3226 /* Enable dirty limit capability */
3227 migrate_set_capability(from
, "dirty-limit", true);
3229 /* Set dirty limit parameters */
3230 migrate_set_parameter_int(from
, "x-vcpu-dirty-limit-period", period
);
3231 migrate_set_parameter_int(from
, "vcpu-dirty-limit", value
);
3233 /* Make sure migrate can't converge */
3234 migrate_ensure_non_converge(from
);
3236 /* To check limit rate after precopy */
3237 migrate_set_capability(from
, "pause-before-switchover", true);
3239 /* Wait for the serial output from the source */
3240 wait_for_serial("src_serial");
3245 * source destination
3249 * wait dirty limit to begin
3251 * cancellation check
3252 * restart incoming vm
3254 * wait dirty limit to begin
3255 * wait pre-switchover event
3256 * convergence condition check
3258 * And see if dirty limit migration works correctly.
3259 * This test case involves many passes, so it runs in slow mode only.
3261 static void test_migrate_dirty_limit(void)
3263 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
3264 QTestState
*from
, *to
;
3266 uint64_t throttle_us_per_full
;
3268 * We want the test to be stable and as fast as possible.
3269 * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
3270 * so we need to decrease a bandwidth.
3272 const int64_t dirtylimit_period
= 1000, dirtylimit_value
= 50;
3273 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
3274 const int64_t downtime_limit
= 250; /* 250ms */
3276 * We migrate through unix-socket (> 500Mb/s).
3277 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
3278 * So, we can predict expected_threshold
3280 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
3281 int max_try_count
= 10;
3282 MigrateCommon args
= {
3284 .hide_stderr
= true,
3285 .use_dirty_ring
= true,
3291 /* Start src, dst vm */
3292 if (test_migrate_start(&from
, &to
, args
.listen_uri
, &args
.start
)) {
3296 /* Prepare for dirty limit migration and wait src vm show up */
3297 migrate_dirty_limit_wait_showup(from
, dirtylimit_period
, dirtylimit_value
);
3300 migrate_qmp(from
, to
, args
.connect_uri
, NULL
, "{}");
3302 /* Wait for dirty limit throttle begin */
3303 throttle_us_per_full
= 0;
3304 while (throttle_us_per_full
== 0) {
3305 throttle_us_per_full
=
3306 read_migrate_property_int(from
, "dirty-limit-throttle-time-per-round");
3308 g_assert_false(src_state
.stop_seen
);
3311 /* Now cancel migrate and wait for dirty limit throttle switch off */
3312 migrate_cancel(from
);
3313 wait_for_migration_status(from
, "cancelled", NULL
);
3315 /* Check if dirty limit throttle switched off, set timeout 1ms */
3317 throttle_us_per_full
=
3318 read_migrate_property_int(from
, "dirty-limit-throttle-time-per-round");
3320 g_assert_false(src_state
.stop_seen
);
3321 } while (throttle_us_per_full
!= 0 && --max_try_count
);
3323 /* Assert dirty limit is not in service */
3324 g_assert_cmpint(throttle_us_per_full
, ==, 0);
3326 args
= (MigrateCommon
) {
3328 .only_target
= true,
3329 .use_dirty_ring
= true,
3335 /* Restart dst vm, src vm already show up so we needn't wait anymore */
3336 if (test_migrate_start(&from
, &to
, args
.listen_uri
, &args
.start
)) {
3341 migrate_qmp(from
, to
, args
.connect_uri
, NULL
, "{}");
3343 /* Wait for dirty limit throttle begin */
3344 throttle_us_per_full
= 0;
3345 while (throttle_us_per_full
== 0) {
3346 throttle_us_per_full
=
3347 read_migrate_property_int(from
, "dirty-limit-throttle-time-per-round");
3349 g_assert_false(src_state
.stop_seen
);
3353 * The dirty limit rate should equals the return value of
3354 * query-vcpu-dirty-limit if dirty limit cap set
3356 g_assert_cmpint(dirtylimit_value
, ==, get_limit_rate(from
));
3358 /* Now, we have tested if dirty limit works, let it converge */
3359 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
3360 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
3363 * Wait for pre-switchover status to check if migration
3364 * satisfy the convergence condition
3366 wait_for_migration_status(from
, "pre-switchover", NULL
);
3368 remaining
= read_ram_property_int(from
, "remaining");
3369 g_assert_cmpint(remaining
, <,
3370 (expected_threshold
+ expected_threshold
/ 100));
3372 migrate_continue(from
, "pre-switchover");
3374 qtest_qmp_eventwait(to
, "RESUME");
3376 wait_for_serial("dest_serial");
3377 wait_for_migration_complete(from
);
3379 test_migrate_end(from
, to
, true);
3382 static bool kvm_dirty_ring_supported(void)
3384 #if defined(__linux__) && defined(HOST_X86_64)
3385 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
3391 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
3394 /* We test with 4096 slots */
3405 int main(int argc
, char **argv
)
3407 bool has_kvm
, has_tcg
;
3408 bool has_uffd
, is_x86
;
3410 g_autoptr(GError
) err
= NULL
;
3411 const char *qemu_src
= getenv(QEMU_ENV_SRC
);
3412 const char *qemu_dst
= getenv(QEMU_ENV_DST
);
3415 g_test_init(&argc
, &argv
, NULL
);
3418 * The default QTEST_QEMU_BINARY must always be provided because
3419 * that is what helpers use to query the accel type and
3422 if (qemu_src
&& qemu_dst
) {
3423 g_test_message("Only one of %s, %s is allowed",
3424 QEMU_ENV_SRC
, QEMU_ENV_DST
);
3428 has_kvm
= qtest_has_accel("kvm");
3429 has_tcg
= qtest_has_accel("tcg");
3431 if (!has_tcg
&& !has_kvm
) {
3432 g_test_skip("No KVM or TCG accelerator available");
3436 has_uffd
= ufd_version_check();
3437 arch
= qtest_get_arch();
3438 is_x86
= !strcmp(arch
, "i386") || !strcmp(arch
, "x86_64");
3441 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
3442 * is touchy due to race conditions on dirty bits (especially on PPC for
3445 if (g_str_equal(arch
, "ppc64") &&
3446 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
3447 g_test_message("Skipping test: kvm_hv not available");
3448 return g_test_run();
3452 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
3453 * there until the problems are resolved
3455 if (g_str_equal(arch
, "s390x") && !has_kvm
) {
3456 g_test_message("Skipping test: s390x host with KVM is required");
3457 return g_test_run();
3460 tmpfs
= g_dir_make_tmp("migration-test-XXXXXX", &err
);
3462 g_test_message("Can't create temporary directory in %s: %s",
3463 g_get_tmp_dir(), err
->message
);
3467 module_call_init(MODULE_INIT_QOM
);
3470 migration_test_add("/migration/precopy/unix/suspend/live",
3471 test_precopy_unix_suspend_live
);
3472 migration_test_add("/migration/precopy/unix/suspend/notlive",
3473 test_precopy_unix_suspend_notlive
);
3477 migration_test_add("/migration/postcopy/plain", test_postcopy
);
3478 migration_test_add("/migration/postcopy/recovery/plain",
3479 test_postcopy_recovery
);
3480 migration_test_add("/migration/postcopy/preempt/plain",
3481 test_postcopy_preempt
);
3482 migration_test_add("/migration/postcopy/preempt/recovery/plain",
3483 test_postcopy_preempt_recovery
);
3485 migration_test_add("/migration/postcopy/recovery/double-failures",
3486 test_postcopy_recovery_double_fail
);
3489 migration_test_add("/migration/postcopy/suspend",
3490 test_postcopy_suspend
);
3494 migration_test_add("/migration/bad_dest", test_baddest
);
3496 if (!g_str_equal(arch
, "s390x")) {
3497 migration_test_add("/migration/analyze-script", test_analyze_script
);
3500 migration_test_add("/migration/precopy/unix/plain",
3501 test_precopy_unix_plain
);
3502 migration_test_add("/migration/precopy/unix/xbzrle",
3503 test_precopy_unix_xbzrle
);
3504 migration_test_add("/migration/precopy/file",
3506 migration_test_add("/migration/precopy/file/offset",
3507 test_precopy_file_offset
);
3508 migration_test_add("/migration/precopy/file/offset/bad",
3509 test_precopy_file_offset_bad
);
3512 * Our CI system has problems with shared memory.
3513 * Don't run this test until we find a workaround.
3515 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3516 migration_test_add("/migration/mode/reboot", test_mode_reboot
);
3519 migration_test_add("/migration/precopy/file/mapped-ram",
3520 test_precopy_file_mapped_ram
);
3521 migration_test_add("/migration/precopy/file/mapped-ram/live",
3522 test_precopy_file_mapped_ram_live
);
3524 migration_test_add("/migration/multifd/file/mapped-ram",
3525 test_multifd_file_mapped_ram
);
3526 migration_test_add("/migration/multifd/file/mapped-ram/live",
3527 test_multifd_file_mapped_ram_live
);
3529 #ifdef CONFIG_GNUTLS
3530 migration_test_add("/migration/precopy/unix/tls/psk",
3531 test_precopy_unix_tls_psk
);
3535 * NOTE: psk test is enough for postcopy, as other types of TLS
3536 * channels are tested under precopy. Here what we want to test is the
3537 * general postcopy path that has TLS channel enabled.
3539 migration_test_add("/migration/postcopy/tls/psk",
3540 test_postcopy_tls_psk
);
3541 migration_test_add("/migration/postcopy/recovery/tls/psk",
3542 test_postcopy_recovery_tls_psk
);
3543 migration_test_add("/migration/postcopy/preempt/tls/psk",
3544 test_postcopy_preempt_tls_psk
);
3545 migration_test_add("/migration/postcopy/preempt/recovery/tls/psk",
3546 test_postcopy_preempt_all
);
3549 migration_test_add("/migration/precopy/unix/tls/x509/default-host",
3550 test_precopy_unix_tls_x509_default_host
);
3551 migration_test_add("/migration/precopy/unix/tls/x509/override-host",
3552 test_precopy_unix_tls_x509_override_host
);
3553 #endif /* CONFIG_TASN1 */
3554 #endif /* CONFIG_GNUTLS */
3556 migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain
);
3558 migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
3559 test_precopy_tcp_switchover_ack
);
3561 #ifdef CONFIG_GNUTLS
3562 migration_test_add("/migration/precopy/tcp/tls/psk/match",
3563 test_precopy_tcp_tls_psk_match
);
3564 migration_test_add("/migration/precopy/tcp/tls/psk/mismatch",
3565 test_precopy_tcp_tls_psk_mismatch
);
3567 migration_test_add("/migration/precopy/tcp/tls/x509/default-host",
3568 test_precopy_tcp_tls_x509_default_host
);
3569 migration_test_add("/migration/precopy/tcp/tls/x509/override-host",
3570 test_precopy_tcp_tls_x509_override_host
);
3571 migration_test_add("/migration/precopy/tcp/tls/x509/mismatch-host",
3572 test_precopy_tcp_tls_x509_mismatch_host
);
3573 migration_test_add("/migration/precopy/tcp/tls/x509/friendly-client",
3574 test_precopy_tcp_tls_x509_friendly_client
);
3575 migration_test_add("/migration/precopy/tcp/tls/x509/hostile-client",
3576 test_precopy_tcp_tls_x509_hostile_client
);
3577 migration_test_add("/migration/precopy/tcp/tls/x509/allow-anon-client",
3578 test_precopy_tcp_tls_x509_allow_anon_client
);
3579 migration_test_add("/migration/precopy/tcp/tls/x509/reject-anon-client",
3580 test_precopy_tcp_tls_x509_reject_anon_client
);
3581 #endif /* CONFIG_TASN1 */
3582 #endif /* CONFIG_GNUTLS */
3584 /* migration_test_add("/migration/ignore_shared", test_ignore_shared); */
3586 migration_test_add("/migration/precopy/fd/tcp",
3587 test_migrate_precopy_fd_socket
);
3588 migration_test_add("/migration/precopy/fd/file",
3589 test_migrate_precopy_fd_file
);
3591 migration_test_add("/migration/validate_uuid", test_validate_uuid
);
3592 migration_test_add("/migration/validate_uuid_error",
3593 test_validate_uuid_error
);
3594 migration_test_add("/migration/validate_uuid_src_not_set",
3595 test_validate_uuid_src_not_set
);
3596 migration_test_add("/migration/validate_uuid_dst_not_set",
3597 test_validate_uuid_dst_not_set
);
3598 migration_test_add("/migration/validate_uri/channels/both_set",
3599 test_validate_uri_channels_both_set
);
3600 migration_test_add("/migration/validate_uri/channels/none_set",
3601 test_validate_uri_channels_none_set
);
3603 * See explanation why this test is slow on function definition
3605 if (g_test_slow()) {
3606 migration_test_add("/migration/auto_converge",
3607 test_migrate_auto_converge
);
3608 if (g_str_equal(arch
, "x86_64") &&
3609 has_kvm
&& kvm_dirty_ring_supported()) {
3610 migration_test_add("/migration/dirty_limit",
3611 test_migrate_dirty_limit
);
3614 migration_test_add("/migration/multifd/tcp/uri/plain/none",
3615 test_multifd_tcp_uri_none
);
3616 migration_test_add("/migration/multifd/tcp/channels/plain/none",
3617 test_multifd_tcp_channels_none
);
3618 migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy",
3619 test_multifd_tcp_zero_page_legacy
);
3620 migration_test_add("/migration/multifd/tcp/plain/zero-page/none",
3621 test_multifd_tcp_no_zero_page
);
3622 migration_test_add("/migration/multifd/tcp/plain/cancel",
3623 test_multifd_tcp_cancel
);
3624 migration_test_add("/migration/multifd/tcp/plain/zlib",
3625 test_multifd_tcp_zlib
);
3627 migration_test_add("/migration/multifd/tcp/plain/zstd",
3628 test_multifd_tcp_zstd
);
3630 #ifdef CONFIG_GNUTLS
3631 migration_test_add("/migration/multifd/tcp/tls/psk/match",
3632 test_multifd_tcp_tls_psk_match
);
3633 migration_test_add("/migration/multifd/tcp/tls/psk/mismatch",
3634 test_multifd_tcp_tls_psk_mismatch
);
3636 migration_test_add("/migration/multifd/tcp/tls/x509/default-host",
3637 test_multifd_tcp_tls_x509_default_host
);
3638 migration_test_add("/migration/multifd/tcp/tls/x509/override-host",
3639 test_multifd_tcp_tls_x509_override_host
);
3640 migration_test_add("/migration/multifd/tcp/tls/x509/mismatch-host",
3641 test_multifd_tcp_tls_x509_mismatch_host
);
3642 migration_test_add("/migration/multifd/tcp/tls/x509/allow-anon-client",
3643 test_multifd_tcp_tls_x509_allow_anon_client
);
3644 migration_test_add("/migration/multifd/tcp/tls/x509/reject-anon-client",
3645 test_multifd_tcp_tls_x509_reject_anon_client
);
3646 #endif /* CONFIG_TASN1 */
3647 #endif /* CONFIG_GNUTLS */
3649 if (g_str_equal(arch
, "x86_64") && has_kvm
&& kvm_dirty_ring_supported()) {
3650 migration_test_add("/migration/dirty_ring",
3651 test_precopy_unix_dirty_ring
);
3652 migration_test_add("/migration/vcpu_dirty_limit",
3653 test_vcpu_dirty_limit
);
3658 g_assert_cmpint(ret
, ==, 0);
3663 g_test_message("unable to rmdir: path (%s): %s",
3664 tmpfs
, strerror(errno
));