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/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
26 #include "crypto/tlscredspsk.h"
27 #include "qapi/qmp/qlist.h"
29 #include "migration-helpers.h"
30 #include "tests/migration/migration-test.h"
32 # include "tests/unit/crypto-tls-psk-helpers.h"
34 # include "tests/unit/crypto-tls-x509-helpers.h"
35 # endif /* CONFIG_TASN1 */
36 #endif /* CONFIG_GNUTLS */
38 /* For dirty ring test; so far only x86_64 is supported */
39 #if defined(__linux__) && defined(HOST_X86_64)
40 #include "linux/kvm.h"
43 unsigned start_address
;
45 static bool uffd_feature_thread_id
;
46 static bool got_src_stop
;
47 static bool got_dst_resume
;
50 * An initial 3 MB offset is used as that corresponds
51 * to ~1 sec of data transfer with our bandwidth setting.
53 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
55 * A further 1k is added to ensure we're not a multiple
56 * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
57 * from the migration guest workload.
59 #define MAGIC_OFFSET_SHUFFLE 1024
60 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
61 #define MAGIC_MARKER 0xFEED12345678CAFEULL
64 * Dirtylimit stop working if dirty page rate error
65 * value less than DIRTYLIMIT_TOLERANCE_RANGE
67 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
69 #define ANALYZE_SCRIPT "scripts/analyze-migration.py"
71 #define QEMU_VM_FILE_MAGIC 0x5145564d
72 #define FILE_TEST_FILENAME "migfile"
73 #define FILE_TEST_OFFSET 0x1000
75 #if defined(__linux__)
76 #include <sys/syscall.h>
80 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
81 #include <sys/eventfd.h>
82 #include <sys/ioctl.h>
83 #include "qemu/userfaultfd.h"
85 static bool ufd_version_check(void)
87 struct uffdio_api api_struct
;
90 int ufd
= uffd_open(O_CLOEXEC
);
93 g_test_message("Skipping test: userfaultfd not available");
97 api_struct
.api
= UFFD_API
;
98 api_struct
.features
= 0;
99 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
100 g_test_message("Skipping test: UFFDIO_API failed");
103 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
105 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
106 (__u64
)1 << _UFFDIO_UNREGISTER
;
107 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
108 g_test_message("Skipping test: Missing userfault feature");
116 static bool ufd_version_check(void)
118 g_test_message("Skipping test: Userfault not available (builtdtime)");
125 static char *bootpath
;
127 /* The boot file modifies memory area in [start_address, end_address)
128 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
130 #include "tests/migration/i386/a-b-bootblock.h"
131 #include "tests/migration/aarch64/a-b-kernel.h"
132 #include "tests/migration/s390x/a-b-bios.h"
134 static void bootfile_create(char *dir
)
136 const char *arch
= qtest_get_arch();
137 unsigned char *content
;
140 bootpath
= g_strdup_printf("%s/bootsect", dir
);
141 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
142 /* the assembled x86 boot sector should be exactly one sector large */
143 g_assert(sizeof(x86_bootsect
) == 512);
144 content
= x86_bootsect
;
145 len
= sizeof(x86_bootsect
);
146 } else if (g_str_equal(arch
, "s390x")) {
148 len
= sizeof(s390x_elf
);
149 } else if (strcmp(arch
, "ppc64") == 0) {
151 * sane architectures can be programmed at the boot prompt
154 } else if (strcmp(arch
, "aarch64") == 0) {
155 content
= aarch64_kernel
;
156 len
= sizeof(aarch64_kernel
);
157 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
159 g_assert_not_reached();
162 FILE *bootfile
= fopen(bootpath
, "wb");
164 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
168 static void bootfile_delete(void)
176 * Wait for some output in the serial output file,
177 * we get an 'A' followed by an endless string of 'B's
178 * but on the destination we won't have the A.
180 static void wait_for_serial(const char *side
)
182 g_autofree
char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
183 FILE *serialfile
= fopen(serialpath
, "r");
184 const char *arch
= qtest_get_arch();
185 int started
= (strcmp(side
, "src_serial") == 0 &&
186 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
189 int readvalue
= fgetc(serialfile
);
192 /* SLOF prints its banner before starting test,
193 * to ignore it, mark the start of the test with '_',
194 * ignore all characters until this marker
201 fseek(serialfile
, 0, SEEK_SET
);
218 started
= (strcmp(side
, "src_serial") == 0 &&
219 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
220 fseek(serialfile
, 0, SEEK_SET
);
225 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
226 g_assert_not_reached();
232 * It's tricky to use qemu's migration event capability with qtest,
233 * events suddenly appearing confuse the qmp()/hmp() responses.
236 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
238 QDict
*rsp_return
, *rsp_ram
;
241 rsp_return
= migrate_query_not_failed(who
);
242 if (!qdict_haskey(rsp_return
, "ram")) {
246 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
247 result
= qdict_get_try_int(rsp_ram
, property
, 0);
249 qobject_unref(rsp_return
);
253 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
258 rsp_return
= migrate_query_not_failed(who
);
259 result
= qdict_get_try_int(rsp_return
, property
, 0);
260 qobject_unref(rsp_return
);
264 static uint64_t get_migration_pass(QTestState
*who
)
266 return read_ram_property_int(who
, "dirty-sync-count");
269 static void read_blocktime(QTestState
*who
)
273 rsp_return
= migrate_query_not_failed(who
);
274 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
275 qobject_unref(rsp_return
);
278 static void wait_for_migration_pass(QTestState
*who
)
280 uint64_t initial_pass
= get_migration_pass(who
);
283 /* Wait for the 1st sync */
284 while (!got_src_stop
&& !initial_pass
) {
286 initial_pass
= get_migration_pass(who
);
291 pass
= get_migration_pass(who
);
292 } while (pass
== initial_pass
&& !got_src_stop
);
295 static void check_guests_ram(QTestState
*who
)
297 /* Our ASM test will have been incrementing one byte from each page from
298 * start_address to < end_address in order. This gives us a constraint
299 * that any page's byte should be equal or less than the previous pages
300 * byte (mod 256); and they should all be equal except for one transition
301 * at the point where we meet the incrementer. (We're running this with
302 * the guest stopped).
307 bool hit_edge
= false;
310 qtest_memread(who
, start_address
, &first_byte
, 1);
311 last_byte
= first_byte
;
313 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
314 address
+= TEST_MEM_PAGE_SIZE
)
317 qtest_memread(who
, address
, &b
, 1);
318 if (b
!= last_byte
) {
319 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
320 /* This is OK, the guest stopped at the point of
321 * incrementing the previous page but didn't get
329 fprintf(stderr
, "Memory content inconsistency at %x"
330 " first_byte = %x last_byte = %x current = %x"
332 address
, first_byte
, last_byte
, b
, hit_edge
);
338 fprintf(stderr
, "and in another %d pages", bad
- 10);
343 static void cleanup(const char *filename
)
345 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
350 static char *SocketAddress_to_str(SocketAddress
*addr
)
352 switch (addr
->type
) {
353 case SOCKET_ADDRESS_TYPE_INET
:
354 return g_strdup_printf("tcp:%s:%s",
357 case SOCKET_ADDRESS_TYPE_UNIX
:
358 return g_strdup_printf("unix:%s",
359 addr
->u
.q_unix
.path
);
360 case SOCKET_ADDRESS_TYPE_FD
:
361 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
362 case SOCKET_ADDRESS_TYPE_VSOCK
:
363 return g_strdup_printf("tcp:%s:%s",
367 return g_strdup("unknown address type");
371 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
375 SocketAddressList
*addrs
;
379 rsp
= migrate_query(who
);
380 object
= qdict_get(rsp
, parameter
);
382 iv
= qobject_input_visitor_new(object
);
383 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
386 /* we are only using a single address */
387 result
= SocketAddress_to_str(addrs
->value
);
389 qapi_free_SocketAddressList(addrs
);
394 static long long migrate_get_parameter_int(QTestState
*who
,
395 const char *parameter
)
400 rsp
= qtest_qmp_assert_success_ref(
401 who
, "{ 'execute': 'query-migrate-parameters' }");
402 result
= qdict_get_int(rsp
, parameter
);
407 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
412 result
= migrate_get_parameter_int(who
, parameter
);
413 g_assert_cmpint(result
, ==, value
);
416 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
419 qtest_qmp_assert_success(who
,
420 "{ 'execute': 'migrate-set-parameters',"
421 "'arguments': { %s: %lld } }",
423 migrate_check_parameter_int(who
, parameter
, value
);
426 static char *migrate_get_parameter_str(QTestState
*who
,
427 const char *parameter
)
432 rsp
= qtest_qmp_assert_success_ref(
433 who
, "{ 'execute': 'query-migrate-parameters' }");
434 result
= g_strdup(qdict_get_str(rsp
, parameter
));
439 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
442 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
443 g_assert_cmpstr(result
, ==, value
);
446 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
449 qtest_qmp_assert_success(who
,
450 "{ 'execute': 'migrate-set-parameters',"
451 "'arguments': { %s: %s } }",
453 migrate_check_parameter_str(who
, parameter
, value
);
456 static long long migrate_get_parameter_bool(QTestState
*who
,
457 const char *parameter
)
462 rsp
= qtest_qmp_assert_success_ref(
463 who
, "{ 'execute': 'query-migrate-parameters' }");
464 result
= qdict_get_bool(rsp
, parameter
);
469 static void migrate_check_parameter_bool(QTestState
*who
, const char *parameter
,
474 result
= migrate_get_parameter_bool(who
, parameter
);
475 g_assert_cmpint(result
, ==, value
);
478 static void migrate_set_parameter_bool(QTestState
*who
, const char *parameter
,
481 qtest_qmp_assert_success(who
,
482 "{ 'execute': 'migrate-set-parameters',"
483 "'arguments': { %s: %i } }",
485 migrate_check_parameter_bool(who
, parameter
, value
);
488 static void migrate_ensure_non_converge(QTestState
*who
)
490 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
491 migrate_set_parameter_int(who
, "max-bandwidth", 3 * 1000 * 1000);
492 migrate_set_parameter_int(who
, "downtime-limit", 1);
495 static void migrate_ensure_converge(QTestState
*who
)
497 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
498 migrate_set_parameter_int(who
, "max-bandwidth", 1 * 1000 * 1000 * 1000);
499 migrate_set_parameter_int(who
, "downtime-limit", 30 * 1000);
503 * Our goal is to ensure that we run a single full migration
504 * iteration, and also dirty memory, ensuring that at least
505 * one further iteration is required.
507 * We can't directly synchronize with the start of a migration
508 * so we have to apply some tricks monitoring memory that is
511 * Initially we set the migration bandwidth to an insanely
512 * low value, with tiny max downtime too. This basically
513 * guarantees migration will never complete.
515 * This will result in a test that is unacceptably slow though,
516 * so we can't let the entire migration pass run at this speed.
517 * Our intent is to let it run just long enough that we can
518 * prove data prior to the marker has been transferred *AND*
519 * also prove this transferred data is dirty again.
521 * Before migration starts, we write a 64-bit magic marker
522 * into a fixed location in the src VM RAM.
524 * Then watch dst memory until the marker appears. This is
525 * proof that start_address -> MAGIC_OFFSET_BASE has been
528 * Finally we go back to the source and read a byte just
529 * before the marker until we see it flip in value. This
530 * is proof that start_address -> MAGIC_OFFSET_BASE
531 * is now dirty again.
533 * IOW, we're guaranteed at least a 2nd migration pass
536 * We can now let migration run at full speed to finish
539 static void migrate_prepare_for_dirty_mem(QTestState
*from
)
542 * The guest workflow iterates from start_address to
543 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
546 * IOW, if we write to mem at a point which is NOT
547 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
548 * conflict with the migration workflow.
550 * We put in a marker here, that we'll use to determine
551 * when the data has been transferred to the dst.
553 qtest_writeq(from
, start_address
+ MAGIC_OFFSET
, MAGIC_MARKER
);
556 static void migrate_wait_for_dirty_mem(QTestState
*from
,
559 uint64_t watch_address
= start_address
+ MAGIC_OFFSET_BASE
;
560 uint64_t marker_address
= start_address
+ MAGIC_OFFSET
;
564 * Wait for the MAGIC_MARKER to get transferred, as an
565 * indicator that a migration pass has made some known
566 * amount of progress.
570 } while (qtest_readq(to
, marker_address
) != MAGIC_MARKER
);
573 * Now ensure that already transferred bytes are
574 * dirty again from the guest workload. Note the
575 * guest byte value will wrap around and by chance
576 * match the original watch_byte. This is harmless
577 * as we'll eventually see a different value if we
580 watch_byte
= qtest_readb(from
, watch_address
);
583 } while (qtest_readb(from
, watch_address
) == watch_byte
);
587 static void migrate_pause(QTestState
*who
)
589 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate-pause' }");
592 static void migrate_continue(QTestState
*who
, const char *state
)
594 qtest_qmp_assert_success(who
,
595 "{ 'execute': 'migrate-continue',"
596 " 'arguments': { 'state': %s } }",
600 static void migrate_recover(QTestState
*who
, const char *uri
)
602 qtest_qmp_assert_success(who
,
603 "{ 'execute': 'migrate-recover', "
604 " 'id': 'recover-cmd', "
605 " 'arguments': { 'uri': %s } }",
609 static void migrate_cancel(QTestState
*who
)
611 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate_cancel' }");
614 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
616 qtest_qmp_assert_success(from
, "{ 'execute': 'migrate-start-postcopy' }");
619 qtest_qmp_eventwait(from
, "STOP");
622 qtest_qmp_eventwait(to
, "RESUME");
627 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
628 * unconditionally, because it means the user would like to be verbose.
632 /* only launch the target process */
634 /* Use dirty ring if true; dirty logging otherwise */
636 const char *opts_source
;
637 const char *opts_target
;
641 * A hook that runs after the src and dst QEMUs have been
642 * created, but before the migration is started. This can
643 * be used to set migration parameters and capabilities.
645 * Returns: NULL, or a pointer to opaque state to be
646 * later passed to the TestMigrateFinishHook
648 typedef void * (*TestMigrateStartHook
)(QTestState
*from
,
652 * A hook that runs after the migration has finished,
653 * regardless of whether it succeeded or failed, but
654 * before QEMU has terminated (unless it self-terminated
655 * due to migration error)
657 * @opaque is a pointer to state previously returned
658 * by the TestMigrateStartHook if any, or NULL.
660 typedef void (*TestMigrateFinishHook
)(QTestState
*from
,
665 /* Optional: fine tune start parameters */
668 /* Required: the URI for the dst QEMU to listen on */
669 const char *listen_uri
;
672 * Optional: the URI for the src QEMU to connect to
673 * If NULL, then it will query the dst QEMU for its actual
674 * listening address and use that as the connect address.
675 * This allows for dynamically picking a free TCP port.
677 const char *connect_uri
;
679 /* Optional: callback to run at start to set migration parameters */
680 TestMigrateStartHook start_hook
;
681 /* Optional: callback to run at finish to cleanup */
682 TestMigrateFinishHook finish_hook
;
685 * Optional: normally we expect the migration process to complete.
687 * There can be a variety of reasons and stages in which failure
688 * can happen during tests.
690 * If a failure is expected to happen at time of establishing
691 * the connection, then MIG_TEST_FAIL will indicate that the dst
692 * QEMU is expected to stay running and accept future migration
695 * If a failure is expected to happen while processing the
696 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
697 * that the dst QEMU is expected to quit with non-zero exit status
700 /* This test should succeed, the default */
701 MIG_TEST_SUCCEED
= 0,
702 /* This test should fail, dest qemu should keep alive */
704 /* This test should fail, dest qemu should fail with abnormal status */
705 MIG_TEST_FAIL_DEST_QUIT_ERR
,
706 /* The QMP command for this migration should fail with an error */
711 * Optional: set number of migration passes to wait for, if live==true.
712 * If zero, then merely wait for a few MB of dirty data
714 unsigned int iterations
;
717 * Optional: whether the guest CPUs should be running during a precopy
718 * migration test. We used to always run with live but it took much
719 * longer so we reduced live tests to only the ones that have solid
720 * reason to be tested live-only. For each of the new test cases for
721 * precopy please provide justifications to use live explicitly (please
722 * refer to existing ones with live=true), or use live=off by default.
726 /* Postcopy specific fields */
728 bool postcopy_preempt
;
731 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
732 const char *uri
, MigrateStart
*args
)
734 g_autofree gchar
*arch_source
= NULL
;
735 g_autofree gchar
*arch_target
= NULL
;
736 /* options for source and target */
737 g_autofree gchar
*arch_opts
= NULL
;
738 g_autofree gchar
*cmd_source
= NULL
;
739 g_autofree gchar
*cmd_target
= NULL
;
740 const gchar
*ignore_stderr
;
741 g_autofree
char *shmem_opts
= NULL
;
742 g_autofree
char *shmem_path
= NULL
;
743 const char *kvm_opts
= NULL
;
744 const char *arch
= qtest_get_arch();
745 const char *memory_size
;
746 const char *machine_alias
, *machine_opts
= "";
748 if (args
->use_shmem
) {
749 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
750 g_test_skip("/dev/shm is not supported");
755 got_src_stop
= false;
756 got_dst_resume
= false;
757 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
758 memory_size
= "150M";
760 if (g_str_equal(arch
, "i386")) {
761 machine_alias
= "pc";
763 machine_alias
= "q35";
765 arch_opts
= g_strdup_printf(
766 "-drive if=none,id=d0,file=%s,format=raw "
767 "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath
);
768 start_address
= X86_TEST_MEM_START
;
769 end_address
= X86_TEST_MEM_END
;
770 } else if (g_str_equal(arch
, "s390x")) {
771 memory_size
= "128M";
772 machine_alias
= "s390-ccw-virtio";
773 arch_opts
= g_strdup_printf("-bios %s", bootpath
);
774 start_address
= S390_TEST_MEM_START
;
775 end_address
= S390_TEST_MEM_END
;
776 } else if (strcmp(arch
, "ppc64") == 0) {
777 memory_size
= "256M";
778 start_address
= PPC_TEST_MEM_START
;
779 end_address
= PPC_TEST_MEM_END
;
780 arch_source
= g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
781 "'nvramrc=hex .\" _\" begin %x %x "
782 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
783 "until'", end_address
, start_address
);
784 machine_alias
= "pseries";
785 machine_opts
= "vsmt=8";
786 arch_opts
= g_strdup("-nodefaults");
787 } else if (strcmp(arch
, "aarch64") == 0) {
788 memory_size
= "150M";
789 machine_alias
= "virt";
790 machine_opts
= "gic-version=max";
791 arch_opts
= g_strdup_printf("-cpu max -kernel %s", bootpath
);
792 start_address
= ARM_TEST_MEM_START
;
793 end_address
= ARM_TEST_MEM_END
;
795 g_assert_not_reached();
798 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
800 ignore_stderr
= "2>/dev/null";
803 * On Windows the QEMU executable is created via CreateProcess() and
804 * IO redirection does not work, so don't bother adding IO redirection
805 * to the command line.
813 if (args
->use_shmem
) {
814 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
815 shmem_opts
= g_strdup_printf(
816 "-object memory-backend-file,id=mem0,size=%s"
817 ",mem-path=%s,share=on -numa node,memdev=mem0",
818 memory_size
, shmem_path
);
821 if (args
->use_dirty_ring
) {
822 kvm_opts
= ",dirty-ring-size=4096";
825 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg "
827 "-name source,debug-threads=on "
829 "-serial file:%s/src_serial "
831 kvm_opts
? kvm_opts
: "",
832 machine_alias
, machine_opts
,
834 arch_opts
? arch_opts
: "",
835 arch_source
? arch_source
: "",
836 shmem_opts
? shmem_opts
: "",
837 args
->opts_source
? args
->opts_source
: "",
839 if (!args
->only_target
) {
840 *from
= qtest_init(cmd_source
);
841 qtest_qmp_set_event_callback(*from
,
842 migrate_watch_for_stop
,
846 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg "
848 "-name target,debug-threads=on "
850 "-serial file:%s/dest_serial "
853 kvm_opts
? kvm_opts
: "",
854 machine_alias
, machine_opts
,
855 memory_size
, tmpfs
, uri
,
856 arch_opts
? arch_opts
: "",
857 arch_target
? arch_target
: "",
858 shmem_opts
? shmem_opts
: "",
859 args
->opts_target
? args
->opts_target
: "",
861 *to
= qtest_init(cmd_target
);
862 qtest_qmp_set_event_callback(*to
,
863 migrate_watch_for_resume
,
867 * Remove shmem file immediately to avoid memory leak in test failed case.
868 * It's valid because QEMU has already opened this file
870 if (args
->use_shmem
) {
877 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
879 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
884 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
886 /* Destination still running, wait for a byte to change */
888 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
890 } while (dest_byte_a
== dest_byte_b
);
892 qtest_qmp_assert_success(to
, "{ 'execute' : 'stop'}");
894 /* With it stopped, check nothing changes */
895 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
897 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
898 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
900 check_guests_ram(to
);
905 cleanup("migsocket");
906 cleanup("src_serial");
907 cleanup("dest_serial");
908 cleanup(FILE_TEST_FILENAME
);
912 struct TestMigrateTLSPSKData
{
920 test_migrate_tls_psk_start_common(QTestState
*from
,
924 struct TestMigrateTLSPSKData
*data
=
925 g_new0(struct TestMigrateTLSPSKData
, 1);
927 data
->workdir
= g_strdup_printf("%s/tlscredspsk0", tmpfs
);
928 data
->pskfile
= g_strdup_printf("%s/%s", data
->workdir
,
929 QCRYPTO_TLS_CREDS_PSKFILE
);
930 g_mkdir_with_parents(data
->workdir
, 0700);
931 test_tls_psk_init(data
->pskfile
);
934 data
->workdiralt
= g_strdup_printf("%s/tlscredspskalt0", tmpfs
);
935 data
->pskfilealt
= g_strdup_printf("%s/%s", data
->workdiralt
,
936 QCRYPTO_TLS_CREDS_PSKFILE
);
937 g_mkdir_with_parents(data
->workdiralt
, 0700);
938 test_tls_psk_init_alt(data
->pskfilealt
);
941 qtest_qmp_assert_success(from
,
942 "{ 'execute': 'object-add',"
943 " 'arguments': { 'qom-type': 'tls-creds-psk',"
944 " 'id': 'tlscredspsk0',"
945 " 'endpoint': 'client',"
947 " 'username': 'qemu'} }",
950 qtest_qmp_assert_success(to
,
951 "{ 'execute': 'object-add',"
952 " 'arguments': { 'qom-type': 'tls-creds-psk',"
953 " 'id': 'tlscredspsk0',"
954 " 'endpoint': 'server',"
956 mismatch
? data
->workdiralt
: data
->workdir
);
958 migrate_set_parameter_str(from
, "tls-creds", "tlscredspsk0");
959 migrate_set_parameter_str(to
, "tls-creds", "tlscredspsk0");
965 test_migrate_tls_psk_start_match(QTestState
*from
,
968 return test_migrate_tls_psk_start_common(from
, to
, false);
972 test_migrate_tls_psk_start_mismatch(QTestState
*from
,
975 return test_migrate_tls_psk_start_common(from
, to
, true);
979 test_migrate_tls_psk_finish(QTestState
*from
,
983 struct TestMigrateTLSPSKData
*data
= opaque
;
985 test_tls_psk_cleanup(data
->pskfile
);
986 if (data
->pskfilealt
) {
987 test_tls_psk_cleanup(data
->pskfilealt
);
989 rmdir(data
->workdir
);
990 if (data
->workdiralt
) {
991 rmdir(data
->workdiralt
);
994 g_free(data
->workdiralt
);
995 g_free(data
->pskfilealt
);
996 g_free(data
->workdir
);
997 g_free(data
->pskfile
);
1010 } TestMigrateTLSX509Data
;
1017 const char *certhostname
;
1018 const char *certipaddr
;
1019 } TestMigrateTLSX509
;
1022 test_migrate_tls_x509_start_common(QTestState
*from
,
1024 TestMigrateTLSX509
*args
)
1026 TestMigrateTLSX509Data
*data
= g_new0(TestMigrateTLSX509Data
, 1);
1028 data
->workdir
= g_strdup_printf("%s/tlscredsx5090", tmpfs
);
1029 data
->keyfile
= g_strdup_printf("%s/key.pem", data
->workdir
);
1031 data
->cacert
= g_strdup_printf("%s/ca-cert.pem", data
->workdir
);
1032 data
->serverkey
= g_strdup_printf("%s/server-key.pem", data
->workdir
);
1033 data
->servercert
= g_strdup_printf("%s/server-cert.pem", data
->workdir
);
1034 if (args
->clientcert
) {
1035 data
->clientkey
= g_strdup_printf("%s/client-key.pem", data
->workdir
);
1036 data
->clientcert
= g_strdup_printf("%s/client-cert.pem", data
->workdir
);
1039 g_mkdir_with_parents(data
->workdir
, 0700);
1041 test_tls_init(data
->keyfile
);
1043 g_assert(link(data
->keyfile
, data
->serverkey
) == 0);
1045 g_assert(CreateHardLink(data
->serverkey
, data
->keyfile
, NULL
) != 0);
1047 if (args
->clientcert
) {
1049 g_assert(link(data
->keyfile
, data
->clientkey
) == 0);
1051 g_assert(CreateHardLink(data
->clientkey
, data
->keyfile
, NULL
) != 0);
1055 TLS_ROOT_REQ_SIMPLE(cacertreq
, data
->cacert
);
1056 if (args
->clientcert
) {
1057 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq
, cacertreq
,
1058 args
->hostileclient
?
1059 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME
:
1060 QCRYPTO_TLS_TEST_CLIENT_NAME
,
1064 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq
, cacertreq
,
1069 qtest_qmp_assert_success(from
,
1070 "{ 'execute': 'object-add',"
1071 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1072 " 'id': 'tlscredsx509client0',"
1073 " 'endpoint': 'client',"
1075 " 'sanity-check': true,"
1076 " 'verify-peer': true} }",
1078 migrate_set_parameter_str(from
, "tls-creds", "tlscredsx509client0");
1079 if (args
->certhostname
) {
1080 migrate_set_parameter_str(from
, "tls-hostname", args
->certhostname
);
1083 qtest_qmp_assert_success(to
,
1084 "{ 'execute': 'object-add',"
1085 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1086 " 'id': 'tlscredsx509server0',"
1087 " 'endpoint': 'server',"
1089 " 'sanity-check': true,"
1090 " 'verify-peer': %i} }",
1091 data
->workdir
, args
->verifyclient
);
1092 migrate_set_parameter_str(to
, "tls-creds", "tlscredsx509server0");
1094 if (args
->authzclient
) {
1095 qtest_qmp_assert_success(to
,
1096 "{ 'execute': 'object-add',"
1097 " 'arguments': { 'qom-type': 'authz-simple',"
1098 " 'id': 'tlsauthz0',"
1099 " 'identity': %s} }",
1100 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME
);
1101 migrate_set_parameter_str(to
, "tls-authz", "tlsauthz0");
1108 * The normal case: match server's cert hostname against
1109 * whatever host we were telling QEMU to connect to (if any)
1112 test_migrate_tls_x509_start_default_host(QTestState
*from
,
1115 TestMigrateTLSX509 args
= {
1116 .verifyclient
= true,
1118 .certipaddr
= "127.0.0.1"
1120 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1124 * The unusual case: the server's cert is different from
1125 * the address we're telling QEMU to connect to (if any),
1126 * so we must give QEMU an explicit hostname to validate
1129 test_migrate_tls_x509_start_override_host(QTestState
*from
,
1132 TestMigrateTLSX509 args
= {
1133 .verifyclient
= true,
1135 .certhostname
= "qemu.org",
1137 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1141 * The unusual case: the server's cert is different from
1142 * the address we're telling QEMU to connect to, and so we
1143 * expect the client to reject the server
1146 test_migrate_tls_x509_start_mismatch_host(QTestState
*from
,
1149 TestMigrateTLSX509 args
= {
1150 .verifyclient
= true,
1152 .certipaddr
= "10.0.0.1",
1154 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1158 test_migrate_tls_x509_start_friendly_client(QTestState
*from
,
1161 TestMigrateTLSX509 args
= {
1162 .verifyclient
= true,
1164 .authzclient
= true,
1165 .certipaddr
= "127.0.0.1",
1167 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1171 test_migrate_tls_x509_start_hostile_client(QTestState
*from
,
1174 TestMigrateTLSX509 args
= {
1175 .verifyclient
= true,
1177 .hostileclient
= true,
1178 .authzclient
= true,
1179 .certipaddr
= "127.0.0.1",
1181 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1185 * The case with no client certificate presented,
1186 * and no server verification
1189 test_migrate_tls_x509_start_allow_anon_client(QTestState
*from
,
1192 TestMigrateTLSX509 args
= {
1193 .certipaddr
= "127.0.0.1",
1195 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1199 * The case with no client certificate presented,
1200 * and server verification rejecting
1203 test_migrate_tls_x509_start_reject_anon_client(QTestState
*from
,
1206 TestMigrateTLSX509 args
= {
1207 .verifyclient
= true,
1208 .certipaddr
= "127.0.0.1",
1210 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1214 test_migrate_tls_x509_finish(QTestState
*from
,
1218 TestMigrateTLSX509Data
*data
= opaque
;
1220 test_tls_cleanup(data
->keyfile
);
1221 g_free(data
->keyfile
);
1223 unlink(data
->cacert
);
1224 g_free(data
->cacert
);
1225 unlink(data
->servercert
);
1226 g_free(data
->servercert
);
1227 unlink(data
->serverkey
);
1228 g_free(data
->serverkey
);
1230 if (data
->clientcert
) {
1231 unlink(data
->clientcert
);
1232 g_free(data
->clientcert
);
1234 if (data
->clientkey
) {
1235 unlink(data
->clientkey
);
1236 g_free(data
->clientkey
);
1239 rmdir(data
->workdir
);
1240 g_free(data
->workdir
);
1244 #endif /* CONFIG_TASN1 */
1245 #endif /* CONFIG_GNUTLS */
1248 test_migrate_compress_start(QTestState
*from
,
1251 migrate_set_parameter_int(from
, "compress-level", 1);
1252 migrate_set_parameter_int(from
, "compress-threads", 4);
1253 migrate_set_parameter_bool(from
, "compress-wait-thread", true);
1254 migrate_set_parameter_int(to
, "decompress-threads", 4);
1256 migrate_set_capability(from
, "compress", true);
1257 migrate_set_capability(to
, "compress", true);
1263 test_migrate_compress_nowait_start(QTestState
*from
,
1266 migrate_set_parameter_int(from
, "compress-level", 9);
1267 migrate_set_parameter_int(from
, "compress-threads", 1);
1268 migrate_set_parameter_bool(from
, "compress-wait-thread", false);
1269 migrate_set_parameter_int(to
, "decompress-threads", 1);
1271 migrate_set_capability(from
, "compress", true);
1272 migrate_set_capability(to
, "compress", true);
1277 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
1278 QTestState
**to_ptr
,
1279 MigrateCommon
*args
)
1281 QTestState
*from
, *to
;
1283 if (test_migrate_start(&from
, &to
, "defer", &args
->start
)) {
1287 if (args
->start_hook
) {
1288 args
->postcopy_data
= args
->start_hook(from
, to
);
1291 migrate_set_capability(from
, "postcopy-ram", true);
1292 migrate_set_capability(to
, "postcopy-ram", true);
1293 migrate_set_capability(to
, "postcopy-blocktime", true);
1295 if (args
->postcopy_preempt
) {
1296 migrate_set_capability(from
, "postcopy-preempt", true);
1297 migrate_set_capability(to
, "postcopy-preempt", true);
1300 migrate_ensure_non_converge(from
);
1302 migrate_prepare_for_dirty_mem(from
);
1303 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1304 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1306 /* Wait for the first serial output from the source */
1307 wait_for_serial("src_serial");
1309 g_autofree
char *uri
= migrate_get_socket_address(to
, "socket-address");
1310 migrate_qmp(from
, uri
, "{}");
1312 migrate_wait_for_dirty_mem(from
, to
);
1320 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
,
1321 MigrateCommon
*args
)
1323 wait_for_migration_complete(from
);
1325 /* Make sure we get at least one "B" on destination */
1326 wait_for_serial("dest_serial");
1328 if (uffd_feature_thread_id
) {
1332 if (args
->finish_hook
) {
1333 args
->finish_hook(from
, to
, args
->postcopy_data
);
1334 args
->postcopy_data
= NULL
;
1337 test_migrate_end(from
, to
, true);
1340 static void test_postcopy_common(MigrateCommon
*args
)
1342 QTestState
*from
, *to
;
1344 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1347 migrate_postcopy_start(from
, to
);
1348 migrate_postcopy_complete(from
, to
, args
);
1351 static void test_postcopy(void)
1353 MigrateCommon args
= { };
1355 test_postcopy_common(&args
);
1358 static void test_postcopy_compress(void)
1360 MigrateCommon args
= {
1361 .start_hook
= test_migrate_compress_start
1364 test_postcopy_common(&args
);
1367 static void test_postcopy_preempt(void)
1369 MigrateCommon args
= {
1370 .postcopy_preempt
= true,
1373 test_postcopy_common(&args
);
1376 #ifdef CONFIG_GNUTLS
1377 static void test_postcopy_tls_psk(void)
1379 MigrateCommon args
= {
1380 .start_hook
= test_migrate_tls_psk_start_match
,
1381 .finish_hook
= test_migrate_tls_psk_finish
,
1384 test_postcopy_common(&args
);
1387 static void test_postcopy_preempt_tls_psk(void)
1389 MigrateCommon args
= {
1390 .postcopy_preempt
= true,
1391 .start_hook
= test_migrate_tls_psk_start_match
,
1392 .finish_hook
= test_migrate_tls_psk_finish
,
1395 test_postcopy_common(&args
);
1399 static void test_postcopy_recovery_common(MigrateCommon
*args
)
1401 QTestState
*from
, *to
;
1402 g_autofree
char *uri
= NULL
;
1404 /* Always hide errors for postcopy recover tests since they're expected */
1405 args
->start
.hide_stderr
= true;
1407 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1411 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1412 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
1414 /* Now we start the postcopy */
1415 migrate_postcopy_start(from
, to
);
1418 * Wait until postcopy is really started; we can only run the
1419 * migrate-pause command during a postcopy
1421 wait_for_migration_status(from
, "postcopy-active", NULL
);
1424 * Manually stop the postcopy migration. This emulates a network
1425 * failure with the migration socket
1427 migrate_pause(from
);
1430 * Wait for destination side to reach postcopy-paused state. The
1431 * migrate-recover command can only succeed if destination machine
1432 * is in the paused state
1434 wait_for_migration_status(to
, "postcopy-paused",
1435 (const char * []) { "failed", "active",
1436 "completed", NULL
});
1439 * Create a new socket to emulate a new channel that is different
1440 * from the broken migration channel; tell the destination to
1441 * listen to the new port
1443 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
1444 migrate_recover(to
, uri
);
1447 * Try to rebuild the migration channel using the resume flag and
1448 * the newly created channel
1450 wait_for_migration_status(from
, "postcopy-paused",
1451 (const char * []) { "failed", "active",
1452 "completed", NULL
});
1453 migrate_qmp(from
, uri
, "{'resume': true}");
1455 /* Restore the postcopy bandwidth to unlimited */
1456 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
1458 migrate_postcopy_complete(from
, to
, args
);
1461 static void test_postcopy_recovery(void)
1463 MigrateCommon args
= { };
1465 test_postcopy_recovery_common(&args
);
1468 static void test_postcopy_recovery_compress(void)
1470 MigrateCommon args
= {
1471 .start_hook
= test_migrate_compress_start
1474 test_postcopy_recovery_common(&args
);
1477 #ifdef CONFIG_GNUTLS
1478 static void test_postcopy_recovery_tls_psk(void)
1480 MigrateCommon args
= {
1481 .start_hook
= test_migrate_tls_psk_start_match
,
1482 .finish_hook
= test_migrate_tls_psk_finish
,
1485 test_postcopy_recovery_common(&args
);
1489 static void test_postcopy_preempt_recovery(void)
1491 MigrateCommon args
= {
1492 .postcopy_preempt
= true,
1495 test_postcopy_recovery_common(&args
);
1498 #ifdef CONFIG_GNUTLS
1499 /* This contains preempt+recovery+tls test altogether */
1500 static void test_postcopy_preempt_all(void)
1502 MigrateCommon args
= {
1503 .postcopy_preempt
= true,
1504 .start_hook
= test_migrate_tls_psk_start_match
,
1505 .finish_hook
= test_migrate_tls_psk_finish
,
1508 test_postcopy_recovery_common(&args
);
1513 static void test_baddest(void)
1515 MigrateStart args
= {
1518 QTestState
*from
, *to
;
1520 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1523 migrate_qmp(from
, "tcp:127.0.0.1:0", "{}");
1524 wait_for_migration_fail(from
, false);
1525 test_migrate_end(from
, to
, false);
1529 static void test_analyze_script(void)
1531 MigrateStart args
= {
1532 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
1534 QTestState
*from
, *to
;
1535 g_autofree
char *uri
= NULL
;
1536 g_autofree
char *file
= NULL
;
1538 const char *python
= g_getenv("PYTHON");
1541 g_test_skip("PYTHON variable not set");
1546 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1551 * Setting these two capabilities causes the "configuration"
1552 * vmstate to include subsections for them. The script needs to
1553 * parse those subsections properly.
1555 migrate_set_capability(from
, "validate-uuid", true);
1556 migrate_set_capability(from
, "x-ignore-shared", true);
1558 file
= g_strdup_printf("%s/migfile", tmpfs
);
1559 uri
= g_strdup_printf("exec:cat > %s", file
);
1561 migrate_ensure_converge(from
);
1562 migrate_qmp(from
, uri
, "{}");
1563 wait_for_migration_complete(from
);
1568 open("/dev/null", O_WRONLY
);
1569 execl(python
, python
, ANALYZE_SCRIPT
, "-f", file
, NULL
);
1570 g_assert_not_reached();
1573 g_assert(waitpid(pid
, &wstatus
, 0) == pid
);
1574 if (WIFEXITED(wstatus
) && WEXITSTATUS(wstatus
) != 0) {
1575 g_test_message("Failed to analyze the migration stream");
1578 test_migrate_end(from
, to
, false);
1583 static void test_precopy_common(MigrateCommon
*args
)
1585 QTestState
*from
, *to
;
1586 void *data_hook
= NULL
;
1587 g_autofree
char *connect_uri
= NULL
;
1589 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1593 if (args
->start_hook
) {
1594 data_hook
= args
->start_hook(from
, to
);
1597 /* Wait for the first serial output from the source */
1598 if (args
->result
== MIG_TEST_SUCCEED
) {
1599 wait_for_serial("src_serial");
1603 migrate_ensure_non_converge(from
);
1604 migrate_prepare_for_dirty_mem(from
);
1607 * Testing non-live migration, we allow it to run at
1608 * full speed to ensure short test case duration.
1609 * For tests expected to fail, we don't need to
1612 if (args
->result
== MIG_TEST_SUCCEED
) {
1613 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1614 if (!got_src_stop
) {
1615 qtest_qmp_eventwait(from
, "STOP");
1617 migrate_ensure_converge(from
);
1621 if (!args
->connect_uri
) {
1622 connect_uri
= migrate_get_socket_address(to
, "socket-address");
1624 connect_uri
= g_strdup(args
->connect_uri
);
1627 if (args
->result
== MIG_TEST_QMP_ERROR
) {
1628 migrate_qmp_fail(from
, connect_uri
, "{}");
1632 migrate_qmp(from
, connect_uri
, "{}");
1634 if (args
->result
!= MIG_TEST_SUCCEED
) {
1635 bool allow_active
= args
->result
== MIG_TEST_FAIL
;
1636 wait_for_migration_fail(from
, allow_active
);
1638 if (args
->result
== MIG_TEST_FAIL_DEST_QUIT_ERR
) {
1639 qtest_set_expected_status(to
, EXIT_FAILURE
);
1644 * For initial iteration(s) we must do a full pass,
1645 * but for the final iteration, we need only wait
1646 * for some dirty mem before switching to converge
1648 while (args
->iterations
> 1) {
1649 wait_for_migration_pass(from
);
1652 migrate_wait_for_dirty_mem(from
, to
);
1654 migrate_ensure_converge(from
);
1657 * We do this first, as it has a timeout to stop us
1658 * hanging forever if migration didn't converge
1660 wait_for_migration_complete(from
);
1662 if (!got_src_stop
) {
1663 qtest_qmp_eventwait(from
, "STOP");
1666 wait_for_migration_complete(from
);
1668 * Must wait for dst to finish reading all incoming
1669 * data on the socket before issuing 'cont' otherwise
1672 wait_for_migration_complete(to
);
1674 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1677 if (!got_dst_resume
) {
1678 qtest_qmp_eventwait(to
, "RESUME");
1681 wait_for_serial("dest_serial");
1685 if (args
->finish_hook
) {
1686 args
->finish_hook(from
, to
, data_hook
);
1689 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1692 static void test_file_common(MigrateCommon
*args
, bool stop_src
)
1694 QTestState
*from
, *to
;
1695 void *data_hook
= NULL
;
1696 g_autofree
char *connect_uri
= g_strdup(args
->connect_uri
);
1698 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1703 * File migration is never live. We can keep the source VM running
1704 * during migration, but the destination will not be running
1707 g_assert_false(args
->live
);
1709 if (args
->start_hook
) {
1710 data_hook
= args
->start_hook(from
, to
);
1713 migrate_ensure_converge(from
);
1714 wait_for_serial("src_serial");
1717 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1718 if (!got_src_stop
) {
1719 qtest_qmp_eventwait(from
, "STOP");
1723 if (args
->result
== MIG_TEST_QMP_ERROR
) {
1724 migrate_qmp_fail(from
, connect_uri
, "{}");
1728 migrate_qmp(from
, connect_uri
, "{}");
1729 wait_for_migration_complete(from
);
1732 * We need to wait for the source to finish before starting the
1735 migrate_incoming_qmp(to
, connect_uri
, "{}");
1736 wait_for_migration_complete(to
);
1739 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1742 if (!got_dst_resume
) {
1743 qtest_qmp_eventwait(to
, "RESUME");
1746 wait_for_serial("dest_serial");
1749 if (args
->finish_hook
) {
1750 args
->finish_hook(from
, to
, data_hook
);
1753 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1756 static void test_precopy_unix_plain(void)
1758 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1759 MigrateCommon args
= {
1763 * The simplest use case of precopy, covering smoke tests of
1764 * get-dirty-log dirty tracking.
1769 test_precopy_common(&args
);
1773 static void test_precopy_unix_dirty_ring(void)
1775 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1776 MigrateCommon args
= {
1778 .use_dirty_ring
= true,
1783 * Besides the precopy/unix basic test, cover dirty ring interface
1784 * rather than get-dirty-log.
1789 test_precopy_common(&args
);
1792 #ifdef CONFIG_GNUTLS
1793 static void test_precopy_unix_tls_psk(void)
1795 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1796 MigrateCommon args
= {
1799 .start_hook
= test_migrate_tls_psk_start_match
,
1800 .finish_hook
= test_migrate_tls_psk_finish
,
1803 test_precopy_common(&args
);
1807 static void test_precopy_unix_tls_x509_default_host(void)
1809 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1810 MigrateCommon args
= {
1812 .hide_stderr
= true,
1816 .start_hook
= test_migrate_tls_x509_start_default_host
,
1817 .finish_hook
= test_migrate_tls_x509_finish
,
1818 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1821 test_precopy_common(&args
);
1824 static void test_precopy_unix_tls_x509_override_host(void)
1826 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1827 MigrateCommon args
= {
1830 .start_hook
= test_migrate_tls_x509_start_override_host
,
1831 .finish_hook
= test_migrate_tls_x509_finish
,
1834 test_precopy_common(&args
);
1836 #endif /* CONFIG_TASN1 */
1837 #endif /* CONFIG_GNUTLS */
1840 /* Currently upset on aarch64 TCG */
1841 static void test_ignore_shared(void)
1843 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1844 QTestState
*from
, *to
;
1846 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
1850 migrate_ensure_non_converge(from
);
1851 migrate_prepare_for_dirty_mem(from
);
1853 migrate_set_capability(from
, "x-ignore-shared", true);
1854 migrate_set_capability(to
, "x-ignore-shared", true);
1856 /* Wait for the first serial output from the source */
1857 wait_for_serial("src_serial");
1859 migrate_qmp(from
, uri
, "{}");
1861 migrate_wait_for_dirty_mem(from
, to
);
1863 if (!got_src_stop
) {
1864 qtest_qmp_eventwait(from
, "STOP");
1867 qtest_qmp_eventwait(to
, "RESUME");
1869 wait_for_serial("dest_serial");
1870 wait_for_migration_complete(from
);
1872 /* Check whether shared RAM has been really skipped */
1873 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1875 test_migrate_end(from
, to
, true);
1880 test_migrate_xbzrle_start(QTestState
*from
,
1883 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1885 migrate_set_capability(from
, "xbzrle", true);
1886 migrate_set_capability(to
, "xbzrle", true);
1891 static void test_precopy_unix_xbzrle(void)
1893 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1894 MigrateCommon args
= {
1897 .start_hook
= test_migrate_xbzrle_start
,
1900 * XBZRLE needs pages to be modified when doing the 2nd+ round
1901 * iteration to have real data pushed to the stream.
1906 test_precopy_common(&args
);
1909 static void test_precopy_unix_compress(void)
1911 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1912 MigrateCommon args
= {
1915 .start_hook
= test_migrate_compress_start
,
1917 * Test that no invalid thread state is left over from
1918 * the previous iteration.
1922 * We make sure the compressor can always work well even if guest
1923 * memory is changing. See commit 34ab9e9743 where we used to fix
1924 * a bug when only trigger-able with guest memory changing.
1929 test_precopy_common(&args
);
1932 static void test_precopy_unix_compress_nowait(void)
1934 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1935 MigrateCommon args
= {
1938 .start_hook
= test_migrate_compress_nowait_start
,
1940 * Test that no invalid thread state is left over from
1941 * the previous iteration.
1944 /* Same reason for the wait version of precopy compress test */
1948 test_precopy_common(&args
);
1951 static void test_precopy_file(void)
1953 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
1954 FILE_TEST_FILENAME
);
1955 MigrateCommon args
= {
1957 .listen_uri
= "defer",
1960 test_file_common(&args
, true);
1963 static void file_offset_finish_hook(QTestState
*from
, QTestState
*to
,
1966 #if defined(__linux__)
1967 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, FILE_TEST_FILENAME
);
1968 size_t size
= FILE_TEST_OFFSET
+ sizeof(QEMU_VM_FILE_MAGIC
);
1969 uintptr_t *addr
, *p
;
1972 fd
= open(path
, O_RDONLY
);
1974 addr
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0);
1975 g_assert(addr
!= MAP_FAILED
);
1978 * Ensure the skipped offset contains zeros and the migration
1979 * stream starts at the right place.
1982 while (p
< addr
+ FILE_TEST_OFFSET
/ sizeof(uintptr_t)) {
1986 g_assert_cmpint(cpu_to_be64(*p
) >> 32, ==, QEMU_VM_FILE_MAGIC
);
1993 static void test_precopy_file_offset(void)
1995 g_autofree
char *uri
= g_strdup_printf("file:%s/%s,offset=%d", tmpfs
,
1998 MigrateCommon args
= {
2000 .listen_uri
= "defer",
2001 .finish_hook
= file_offset_finish_hook
,
2004 test_file_common(&args
, false);
2007 static void test_precopy_file_offset_bad(void)
2009 /* using a value not supported by qemu_strtosz() */
2010 g_autofree
char *uri
= g_strdup_printf("file:%s/%s,offset=0x20M",
2011 tmpfs
, FILE_TEST_FILENAME
);
2012 MigrateCommon args
= {
2014 .listen_uri
= "defer",
2015 .result
= MIG_TEST_QMP_ERROR
,
2018 test_file_common(&args
, false);
2021 static void test_precopy_tcp_plain(void)
2023 MigrateCommon args
= {
2024 .listen_uri
= "tcp:127.0.0.1:0",
2027 test_precopy_common(&args
);
2030 static void *test_migrate_switchover_ack_start(QTestState
*from
, QTestState
*to
)
2033 migrate_set_capability(from
, "return-path", true);
2034 migrate_set_capability(to
, "return-path", true);
2036 migrate_set_capability(from
, "switchover-ack", true);
2037 migrate_set_capability(to
, "switchover-ack", true);
2042 static void test_precopy_tcp_switchover_ack(void)
2044 MigrateCommon args
= {
2045 .listen_uri
= "tcp:127.0.0.1:0",
2046 .start_hook
= test_migrate_switchover_ack_start
,
2048 * Source VM must be running in order to consider the switchover ACK
2049 * when deciding to do switchover or not.
2054 test_precopy_common(&args
);
2057 #ifdef CONFIG_GNUTLS
2058 static void test_precopy_tcp_tls_psk_match(void)
2060 MigrateCommon args
= {
2061 .listen_uri
= "tcp:127.0.0.1:0",
2062 .start_hook
= test_migrate_tls_psk_start_match
,
2063 .finish_hook
= test_migrate_tls_psk_finish
,
2066 test_precopy_common(&args
);
2069 static void test_precopy_tcp_tls_psk_mismatch(void)
2071 MigrateCommon args
= {
2073 .hide_stderr
= true,
2075 .listen_uri
= "tcp:127.0.0.1:0",
2076 .start_hook
= test_migrate_tls_psk_start_mismatch
,
2077 .finish_hook
= test_migrate_tls_psk_finish
,
2078 .result
= MIG_TEST_FAIL
,
2081 test_precopy_common(&args
);
2085 static void test_precopy_tcp_tls_x509_default_host(void)
2087 MigrateCommon args
= {
2088 .listen_uri
= "tcp:127.0.0.1:0",
2089 .start_hook
= test_migrate_tls_x509_start_default_host
,
2090 .finish_hook
= test_migrate_tls_x509_finish
,
2093 test_precopy_common(&args
);
2096 static void test_precopy_tcp_tls_x509_override_host(void)
2098 MigrateCommon args
= {
2099 .listen_uri
= "tcp:127.0.0.1:0",
2100 .start_hook
= test_migrate_tls_x509_start_override_host
,
2101 .finish_hook
= test_migrate_tls_x509_finish
,
2104 test_precopy_common(&args
);
2107 static void test_precopy_tcp_tls_x509_mismatch_host(void)
2109 MigrateCommon args
= {
2111 .hide_stderr
= true,
2113 .listen_uri
= "tcp:127.0.0.1:0",
2114 .start_hook
= test_migrate_tls_x509_start_mismatch_host
,
2115 .finish_hook
= test_migrate_tls_x509_finish
,
2116 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
2119 test_precopy_common(&args
);
2122 static void test_precopy_tcp_tls_x509_friendly_client(void)
2124 MigrateCommon args
= {
2125 .listen_uri
= "tcp:127.0.0.1:0",
2126 .start_hook
= test_migrate_tls_x509_start_friendly_client
,
2127 .finish_hook
= test_migrate_tls_x509_finish
,
2130 test_precopy_common(&args
);
2133 static void test_precopy_tcp_tls_x509_hostile_client(void)
2135 MigrateCommon args
= {
2137 .hide_stderr
= true,
2139 .listen_uri
= "tcp:127.0.0.1:0",
2140 .start_hook
= test_migrate_tls_x509_start_hostile_client
,
2141 .finish_hook
= test_migrate_tls_x509_finish
,
2142 .result
= MIG_TEST_FAIL
,
2145 test_precopy_common(&args
);
2148 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2150 MigrateCommon args
= {
2151 .listen_uri
= "tcp:127.0.0.1:0",
2152 .start_hook
= test_migrate_tls_x509_start_allow_anon_client
,
2153 .finish_hook
= test_migrate_tls_x509_finish
,
2156 test_precopy_common(&args
);
2159 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2161 MigrateCommon args
= {
2163 .hide_stderr
= true,
2165 .listen_uri
= "tcp:127.0.0.1:0",
2166 .start_hook
= test_migrate_tls_x509_start_reject_anon_client
,
2167 .finish_hook
= test_migrate_tls_x509_finish
,
2168 .result
= MIG_TEST_FAIL
,
2171 test_precopy_common(&args
);
2173 #endif /* CONFIG_TASN1 */
2174 #endif /* CONFIG_GNUTLS */
2177 static void *test_migrate_fd_start_hook(QTestState
*from
,
2183 /* Create two connected sockets for migration */
2184 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
2185 g_assert_cmpint(ret
, ==, 0);
2187 /* Send the 1st socket to the target */
2188 qtest_qmp_fds_assert_success(to
, &pair
[0], 1,
2189 "{ 'execute': 'getfd',"
2190 " 'arguments': { 'fdname': 'fd-mig' }}");
2193 /* Start incoming migration from the 1st socket */
2194 migrate_incoming_qmp(to
, "fd:fd-mig", "{}");
2196 /* Send the 2nd socket to the target */
2197 qtest_qmp_fds_assert_success(from
, &pair
[1], 1,
2198 "{ 'execute': 'getfd',"
2199 " 'arguments': { 'fdname': 'fd-mig' }}");
2205 static void test_migrate_fd_finish_hook(QTestState
*from
,
2210 const char *error_desc
;
2212 /* Test closing fds */
2213 /* We assume, that QEMU removes named fd from its list,
2214 * so this should fail */
2215 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
2216 " 'arguments': { 'fdname': 'fd-mig' }}");
2217 g_assert_true(qdict_haskey(rsp
, "error"));
2218 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2219 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2222 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
2223 " 'arguments': { 'fdname': 'fd-mig' }}");
2224 g_assert_true(qdict_haskey(rsp
, "error"));
2225 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2226 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2230 static void test_migrate_fd_proto(void)
2232 MigrateCommon args
= {
2233 .listen_uri
= "defer",
2234 .connect_uri
= "fd:fd-mig",
2235 .start_hook
= test_migrate_fd_start_hook
,
2236 .finish_hook
= test_migrate_fd_finish_hook
2238 test_precopy_common(&args
);
2242 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
2244 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2245 QTestState
*from
, *to
;
2247 if (test_migrate_start(&from
, &to
, uri
, args
)) {
2252 * UUID validation is at the begin of migration. So, the main process of
2253 * migration is not interesting for us here. Thus, set huge downtime for
2254 * very fast migration.
2256 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
2257 migrate_set_capability(from
, "validate-uuid", true);
2259 /* Wait for the first serial output from the source */
2260 wait_for_serial("src_serial");
2262 migrate_qmp(from
, uri
, "{}");
2265 qtest_set_expected_status(to
, EXIT_FAILURE
);
2266 wait_for_migration_fail(from
, true);
2268 wait_for_migration_complete(from
);
2271 test_migrate_end(from
, to
, false);
2274 static void test_validate_uuid(void)
2276 MigrateStart args
= {
2277 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2278 .opts_target
= "-uuid 11111111-1111-1111-1111-111111111111",
2281 do_test_validate_uuid(&args
, false);
2284 static void test_validate_uuid_error(void)
2286 MigrateStart args
= {
2287 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2288 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2289 .hide_stderr
= true,
2292 do_test_validate_uuid(&args
, true);
2295 static void test_validate_uuid_src_not_set(void)
2297 MigrateStart args
= {
2298 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2299 .hide_stderr
= true,
2302 do_test_validate_uuid(&args
, false);
2305 static void test_validate_uuid_dst_not_set(void)
2307 MigrateStart args
= {
2308 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2309 .hide_stderr
= true,
2312 do_test_validate_uuid(&args
, false);
2316 * The way auto_converge works, we need to do too many passes to
2317 * run this test. Auto_converge logic is only run once every
2318 * three iterations, so:
2320 * - 3 iterations without auto_converge enabled
2321 * - 3 iterations with pct = 5
2322 * - 3 iterations with pct = 30
2323 * - 3 iterations with pct = 55
2324 * - 3 iterations with pct = 80
2325 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2327 * To make things even worse, we need to run the initial stage at
2328 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2330 static void test_migrate_auto_converge(void)
2332 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2333 MigrateStart args
= {};
2334 QTestState
*from
, *to
;
2338 * We want the test to be stable and as fast as possible.
2339 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2340 * so we need to decrease a bandwidth.
2342 const int64_t init_pct
= 5, inc_pct
= 25, max_pct
= 95;
2344 if (test_migrate_start(&from
, &to
, uri
, &args
)) {
2348 migrate_set_capability(from
, "auto-converge", true);
2349 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
2350 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
2351 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
2354 * Set the initial parameters so that the migration could not converge
2355 * without throttling.
2357 migrate_ensure_non_converge(from
);
2359 /* To check remaining size after precopy */
2360 migrate_set_capability(from
, "pause-before-switchover", true);
2362 /* Wait for the first serial output from the source */
2363 wait_for_serial("src_serial");
2365 migrate_qmp(from
, uri
, "{}");
2367 /* Wait for throttling begins */
2370 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2371 if (percentage
!= 0) {
2375 g_assert_false(got_src_stop
);
2377 /* The first percentage of throttling should be at least init_pct */
2378 g_assert_cmpint(percentage
, >=, init_pct
);
2379 /* Now, when we tested that throttling works, let it converge */
2380 migrate_ensure_converge(from
);
2383 * Wait for pre-switchover status to check last throttle percentage
2384 * and remaining. These values will be zeroed later
2386 wait_for_migration_status(from
, "pre-switchover", NULL
);
2388 /* The final percentage of throttling shouldn't be greater than max_pct */
2389 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2390 g_assert_cmpint(percentage
, <=, max_pct
);
2391 migrate_continue(from
, "pre-switchover");
2393 qtest_qmp_eventwait(to
, "RESUME");
2395 wait_for_serial("dest_serial");
2396 wait_for_migration_complete(from
);
2398 test_migrate_end(from
, to
, true);
2402 test_migrate_precopy_tcp_multifd_start_common(QTestState
*from
,
2406 migrate_set_parameter_int(from
, "multifd-channels", 16);
2407 migrate_set_parameter_int(to
, "multifd-channels", 16);
2409 migrate_set_parameter_str(from
, "multifd-compression", method
);
2410 migrate_set_parameter_str(to
, "multifd-compression", method
);
2412 migrate_set_capability(from
, "multifd", true);
2413 migrate_set_capability(to
, "multifd", true);
2415 /* Start incoming migration from the 1st socket */
2416 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2422 test_migrate_precopy_tcp_multifd_start(QTestState
*from
,
2425 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2429 test_migrate_precopy_tcp_multifd_zlib_start(QTestState
*from
,
2432 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zlib");
2437 test_migrate_precopy_tcp_multifd_zstd_start(QTestState
*from
,
2440 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zstd");
2442 #endif /* CONFIG_ZSTD */
2444 static void test_multifd_tcp_none(void)
2446 MigrateCommon args
= {
2447 .listen_uri
= "defer",
2448 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2450 * Multifd is more complicated than most of the features, it
2451 * directly takes guest page buffers when sending, make sure
2452 * everything will work alright even if guest page is changing.
2456 test_precopy_common(&args
);
2459 static void test_multifd_tcp_zlib(void)
2461 MigrateCommon args
= {
2462 .listen_uri
= "defer",
2463 .start_hook
= test_migrate_precopy_tcp_multifd_zlib_start
,
2465 test_precopy_common(&args
);
2469 static void test_multifd_tcp_zstd(void)
2471 MigrateCommon args
= {
2472 .listen_uri
= "defer",
2473 .start_hook
= test_migrate_precopy_tcp_multifd_zstd_start
,
2475 test_precopy_common(&args
);
2479 #ifdef CONFIG_GNUTLS
2481 test_migrate_multifd_tcp_tls_psk_start_match(QTestState
*from
,
2484 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2485 return test_migrate_tls_psk_start_match(from
, to
);
2489 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState
*from
,
2492 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2493 return test_migrate_tls_psk_start_mismatch(from
, to
);
2498 test_migrate_multifd_tls_x509_start_default_host(QTestState
*from
,
2501 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2502 return test_migrate_tls_x509_start_default_host(from
, to
);
2506 test_migrate_multifd_tls_x509_start_override_host(QTestState
*from
,
2509 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2510 return test_migrate_tls_x509_start_override_host(from
, to
);
2514 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState
*from
,
2517 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2518 return test_migrate_tls_x509_start_mismatch_host(from
, to
);
2522 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState
*from
,
2525 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2526 return test_migrate_tls_x509_start_allow_anon_client(from
, to
);
2530 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState
*from
,
2533 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2534 return test_migrate_tls_x509_start_reject_anon_client(from
, to
);
2536 #endif /* CONFIG_TASN1 */
2538 static void test_multifd_tcp_tls_psk_match(void)
2540 MigrateCommon args
= {
2541 .listen_uri
= "defer",
2542 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_match
,
2543 .finish_hook
= test_migrate_tls_psk_finish
,
2545 test_precopy_common(&args
);
2548 static void test_multifd_tcp_tls_psk_mismatch(void)
2550 MigrateCommon args
= {
2552 .hide_stderr
= true,
2554 .listen_uri
= "defer",
2555 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_mismatch
,
2556 .finish_hook
= test_migrate_tls_psk_finish
,
2557 .result
= MIG_TEST_FAIL
,
2559 test_precopy_common(&args
);
2563 static void test_multifd_tcp_tls_x509_default_host(void)
2565 MigrateCommon args
= {
2566 .listen_uri
= "defer",
2567 .start_hook
= test_migrate_multifd_tls_x509_start_default_host
,
2568 .finish_hook
= test_migrate_tls_x509_finish
,
2570 test_precopy_common(&args
);
2573 static void test_multifd_tcp_tls_x509_override_host(void)
2575 MigrateCommon args
= {
2576 .listen_uri
= "defer",
2577 .start_hook
= test_migrate_multifd_tls_x509_start_override_host
,
2578 .finish_hook
= test_migrate_tls_x509_finish
,
2580 test_precopy_common(&args
);
2583 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2586 * This has different behaviour to the non-multifd case.
2588 * In non-multifd case when client aborts due to mismatched
2589 * cert host, the server has already started trying to load
2590 * migration state, and so it exits with I/O failure.
2592 * In multifd case when client aborts due to mismatched
2593 * cert host, the server is still waiting for the other
2594 * multifd connections to arrive so hasn't started trying
2595 * to load migration state, and thus just aborts the migration
2598 MigrateCommon args
= {
2600 .hide_stderr
= true,
2602 .listen_uri
= "defer",
2603 .start_hook
= test_migrate_multifd_tls_x509_start_mismatch_host
,
2604 .finish_hook
= test_migrate_tls_x509_finish
,
2605 .result
= MIG_TEST_FAIL
,
2607 test_precopy_common(&args
);
2610 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2612 MigrateCommon args
= {
2613 .listen_uri
= "defer",
2614 .start_hook
= test_migrate_multifd_tls_x509_start_allow_anon_client
,
2615 .finish_hook
= test_migrate_tls_x509_finish
,
2617 test_precopy_common(&args
);
2620 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2622 MigrateCommon args
= {
2624 .hide_stderr
= true,
2626 .listen_uri
= "defer",
2627 .start_hook
= test_migrate_multifd_tls_x509_start_reject_anon_client
,
2628 .finish_hook
= test_migrate_tls_x509_finish
,
2629 .result
= MIG_TEST_FAIL
,
2631 test_precopy_common(&args
);
2633 #endif /* CONFIG_TASN1 */
2634 #endif /* CONFIG_GNUTLS */
2642 * launch another target
2645 * And see that it works
2647 static void test_multifd_tcp_cancel(void)
2649 MigrateStart args
= {
2650 .hide_stderr
= true,
2652 QTestState
*from
, *to
, *to2
;
2653 g_autofree
char *uri
= NULL
;
2655 if (test_migrate_start(&from
, &to
, "defer", &args
)) {
2659 migrate_ensure_non_converge(from
);
2660 migrate_prepare_for_dirty_mem(from
);
2662 migrate_set_parameter_int(from
, "multifd-channels", 16);
2663 migrate_set_parameter_int(to
, "multifd-channels", 16);
2665 migrate_set_capability(from
, "multifd", true);
2666 migrate_set_capability(to
, "multifd", true);
2668 /* Start incoming migration from the 1st socket */
2669 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2671 /* Wait for the first serial output from the source */
2672 wait_for_serial("src_serial");
2674 uri
= migrate_get_socket_address(to
, "socket-address");
2676 migrate_qmp(from
, uri
, "{}");
2678 migrate_wait_for_dirty_mem(from
, to
);
2680 migrate_cancel(from
);
2682 /* Make sure QEMU process "to" exited */
2683 qtest_set_expected_status(to
, EXIT_FAILURE
);
2684 qtest_wait_qemu(to
);
2686 args
= (MigrateStart
){
2687 .only_target
= true,
2690 if (test_migrate_start(&from
, &to2
, "defer", &args
)) {
2694 migrate_set_parameter_int(to2
, "multifd-channels", 16);
2696 migrate_set_capability(to2
, "multifd", true);
2698 /* Start incoming migration from the 1st socket */
2699 migrate_incoming_qmp(to2
, "tcp:127.0.0.1:0", "{}");
2702 uri
= migrate_get_socket_address(to2
, "socket-address");
2704 wait_for_migration_status(from
, "cancelled", NULL
);
2706 migrate_ensure_non_converge(from
);
2708 migrate_qmp(from
, uri
, "{}");
2710 migrate_wait_for_dirty_mem(from
, to2
);
2712 migrate_ensure_converge(from
);
2714 if (!got_src_stop
) {
2715 qtest_qmp_eventwait(from
, "STOP");
2717 qtest_qmp_eventwait(to2
, "RESUME");
2719 wait_for_serial("dest_serial");
2720 wait_for_migration_complete(from
);
2721 test_migrate_end(from
, to2
, true);
2724 static void calc_dirty_rate(QTestState
*who
, uint64_t calc_time
)
2726 qtest_qmp_assert_success(who
,
2727 "{ 'execute': 'calc-dirty-rate',"
2729 "'calc-time': %" PRIu64
","
2730 "'mode': 'dirty-ring' }}",
2734 static QDict
*query_dirty_rate(QTestState
*who
)
2736 return qtest_qmp_assert_success_ref(who
,
2737 "{ 'execute': 'query-dirty-rate' }");
2740 static void dirtylimit_set_all(QTestState
*who
, uint64_t dirtyrate
)
2742 qtest_qmp_assert_success(who
,
2743 "{ 'execute': 'set-vcpu-dirty-limit',"
2745 "'dirty-rate': %" PRIu64
" } }",
2749 static void cancel_vcpu_dirty_limit(QTestState
*who
)
2751 qtest_qmp_assert_success(who
,
2752 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2755 static QDict
*query_vcpu_dirty_limit(QTestState
*who
)
2759 rsp
= qtest_qmp(who
, "{ 'execute': 'query-vcpu-dirty-limit' }");
2760 g_assert(!qdict_haskey(rsp
, "error"));
2761 g_assert(qdict_haskey(rsp
, "return"));
2766 static bool calc_dirtyrate_ready(QTestState
*who
)
2771 rsp_return
= query_dirty_rate(who
);
2772 g_assert(rsp_return
);
2774 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2777 return g_strcmp0(status
, "measuring");
2780 static void wait_for_calc_dirtyrate_complete(QTestState
*who
,
2783 int max_try_count
= 10000;
2784 usleep(time_s
* 1000000);
2786 while (!calc_dirtyrate_ready(who
) && max_try_count
--) {
2791 * Set the timeout with 10 s(max_try_count * 1000us),
2792 * if dirtyrate measurement not complete, fail test.
2794 g_assert_cmpint(max_try_count
, !=, 0);
2797 static int64_t get_dirty_rate(QTestState
*who
)
2802 const QListEntry
*entry
;
2806 rsp_return
= query_dirty_rate(who
);
2807 g_assert(rsp_return
);
2809 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2811 g_assert_cmpstr(status
, ==, "measured");
2813 rates
= qdict_get_qlist(rsp_return
, "vcpu-dirty-rate");
2814 g_assert(rates
&& !qlist_empty(rates
));
2816 entry
= qlist_first(rates
);
2819 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2822 dirtyrate
= qdict_get_try_int(rate
, "dirty-rate", -1);
2824 qobject_unref(rsp_return
);
2828 static int64_t get_limit_rate(QTestState
*who
)
2832 const QListEntry
*entry
;
2836 rsp_return
= query_vcpu_dirty_limit(who
);
2837 g_assert(rsp_return
);
2839 rates
= qdict_get_qlist(rsp_return
, "return");
2840 g_assert(rates
&& !qlist_empty(rates
));
2842 entry
= qlist_first(rates
);
2845 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2848 dirtyrate
= qdict_get_try_int(rate
, "limit-rate", -1);
2850 qobject_unref(rsp_return
);
2854 static QTestState
*dirtylimit_start_vm(void)
2856 QTestState
*vm
= NULL
;
2858 cmd
= g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2859 "-name dirtylimit-test,debug-threads=on "
2861 "-serial file:%s/vm_serial "
2862 "-drive file=%s,format=raw ",
2865 vm
= qtest_init(cmd
);
2869 static void dirtylimit_stop_vm(QTestState
*vm
)
2872 cleanup("vm_serial");
2875 static void test_vcpu_dirty_limit(void)
2878 int64_t origin_rate
;
2881 int max_try_count
= 20;
2884 /* Start vm for vcpu dirtylimit test */
2885 vm
= dirtylimit_start_vm();
2887 /* Wait for the first serial output from the vm*/
2888 wait_for_serial("vm_serial");
2890 /* Do dirtyrate measurement with calc time equals 1s */
2891 calc_dirty_rate(vm
, 1);
2893 /* Sleep calc time and wait for calc dirtyrate complete */
2894 wait_for_calc_dirtyrate_complete(vm
, 1);
2896 /* Query original dirty page rate */
2897 origin_rate
= get_dirty_rate(vm
);
2899 /* VM booted from bootsect should dirty memory steadily */
2900 assert(origin_rate
!= 0);
2902 /* Setup quota dirty page rate at half of origin */
2903 quota_rate
= origin_rate
/ 2;
2905 /* Set dirtylimit */
2906 dirtylimit_set_all(vm
, quota_rate
);
2909 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2912 g_assert_cmpint(quota_rate
, ==, get_limit_rate(vm
));
2914 /* Sleep a bit to check if it take effect */
2918 * Check if dirtylimit take effect realistically, set the
2919 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2920 * doesn't take effect, fail test.
2922 while (--max_try_count
) {
2923 calc_dirty_rate(vm
, 1);
2924 wait_for_calc_dirtyrate_complete(vm
, 1);
2925 rate
= get_dirty_rate(vm
);
2928 * Assume hitting if current rate is less
2929 * than quota rate (within accepting error)
2931 if (rate
< (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2937 g_assert_cmpint(hit
, ==, 1);
2942 /* Check if dirtylimit cancellation take effect */
2943 cancel_vcpu_dirty_limit(vm
);
2944 while (--max_try_count
) {
2945 calc_dirty_rate(vm
, 1);
2946 wait_for_calc_dirtyrate_complete(vm
, 1);
2947 rate
= get_dirty_rate(vm
);
2950 * Assume dirtylimit be canceled if current rate is
2951 * greater than quota rate (within accepting error)
2953 if (rate
> (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2959 g_assert_cmpint(hit
, ==, 1);
2960 dirtylimit_stop_vm(vm
);
2963 static bool kvm_dirty_ring_supported(void)
2965 #if defined(__linux__) && defined(HOST_X86_64)
2966 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
2972 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
2975 /* We test with 4096 slots */
2986 int main(int argc
, char **argv
)
2988 bool has_kvm
, has_tcg
;
2991 g_autoptr(GError
) err
= NULL
;
2994 g_test_init(&argc
, &argv
, NULL
);
2996 has_kvm
= qtest_has_accel("kvm");
2997 has_tcg
= qtest_has_accel("tcg");
2999 if (!has_tcg
&& !has_kvm
) {
3000 g_test_skip("No KVM or TCG accelerator available");
3004 has_uffd
= ufd_version_check();
3005 arch
= qtest_get_arch();
3008 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
3009 * is touchy due to race conditions on dirty bits (especially on PPC for
3012 if (g_str_equal(arch
, "ppc64") &&
3013 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
3014 g_test_message("Skipping test: kvm_hv not available");
3015 return g_test_run();
3019 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
3020 * there until the problems are resolved
3022 if (g_str_equal(arch
, "s390x") && !has_kvm
) {
3023 g_test_message("Skipping test: s390x host with KVM is required");
3024 return g_test_run();
3027 tmpfs
= g_dir_make_tmp("migration-test-XXXXXX", &err
);
3029 g_test_message("Can't create temporary directory in %s: %s",
3030 g_get_tmp_dir(), err
->message
);
3033 bootfile_create(tmpfs
);
3035 module_call_init(MODULE_INIT_QOM
);
3038 qtest_add_func("/migration/postcopy/plain", test_postcopy
);
3039 qtest_add_func("/migration/postcopy/recovery/plain",
3040 test_postcopy_recovery
);
3041 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt
);
3042 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
3043 test_postcopy_preempt_recovery
);
3044 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3045 qtest_add_func("/migration/postcopy/compress/plain",
3046 test_postcopy_compress
);
3047 qtest_add_func("/migration/postcopy/recovery/compress/plain",
3048 test_postcopy_recovery_compress
);
3052 qtest_add_func("/migration/bad_dest", test_baddest
);
3054 if (!g_str_equal(arch
, "s390x")) {
3055 qtest_add_func("/migration/analyze-script", test_analyze_script
);
3058 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain
);
3059 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle
);
3061 * Compression fails from time to time.
3062 * Put test here but don't enable it until everything is fixed.
3064 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3065 qtest_add_func("/migration/precopy/unix/compress/wait",
3066 test_precopy_unix_compress
);
3067 qtest_add_func("/migration/precopy/unix/compress/nowait",
3068 test_precopy_unix_compress_nowait
);
3071 qtest_add_func("/migration/precopy/file",
3073 qtest_add_func("/migration/precopy/file/offset",
3074 test_precopy_file_offset
);
3075 qtest_add_func("/migration/precopy/file/offset/bad",
3076 test_precopy_file_offset_bad
);
3078 #ifdef CONFIG_GNUTLS
3079 qtest_add_func("/migration/precopy/unix/tls/psk",
3080 test_precopy_unix_tls_psk
);
3084 * NOTE: psk test is enough for postcopy, as other types of TLS
3085 * channels are tested under precopy. Here what we want to test is the
3086 * general postcopy path that has TLS channel enabled.
3088 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk
);
3089 qtest_add_func("/migration/postcopy/recovery/tls/psk",
3090 test_postcopy_recovery_tls_psk
);
3091 qtest_add_func("/migration/postcopy/preempt/tls/psk",
3092 test_postcopy_preempt_tls_psk
);
3093 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
3094 test_postcopy_preempt_all
);
3097 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
3098 test_precopy_unix_tls_x509_default_host
);
3099 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
3100 test_precopy_unix_tls_x509_override_host
);
3101 #endif /* CONFIG_TASN1 */
3102 #endif /* CONFIG_GNUTLS */
3104 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain
);
3106 qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
3107 test_precopy_tcp_switchover_ack
);
3109 #ifdef CONFIG_GNUTLS
3110 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
3111 test_precopy_tcp_tls_psk_match
);
3112 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
3113 test_precopy_tcp_tls_psk_mismatch
);
3115 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
3116 test_precopy_tcp_tls_x509_default_host
);
3117 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
3118 test_precopy_tcp_tls_x509_override_host
);
3119 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
3120 test_precopy_tcp_tls_x509_mismatch_host
);
3121 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
3122 test_precopy_tcp_tls_x509_friendly_client
);
3123 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
3124 test_precopy_tcp_tls_x509_hostile_client
);
3125 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
3126 test_precopy_tcp_tls_x509_allow_anon_client
);
3127 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
3128 test_precopy_tcp_tls_x509_reject_anon_client
);
3129 #endif /* CONFIG_TASN1 */
3130 #endif /* CONFIG_GNUTLS */
3132 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
3134 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
3136 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
3137 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
3138 qtest_add_func("/migration/validate_uuid_src_not_set",
3139 test_validate_uuid_src_not_set
);
3140 qtest_add_func("/migration/validate_uuid_dst_not_set",
3141 test_validate_uuid_dst_not_set
);
3143 * See explanation why this test is slow on function definition
3145 if (g_test_slow()) {
3146 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
3148 qtest_add_func("/migration/multifd/tcp/plain/none",
3149 test_multifd_tcp_none
);
3151 * This test is flaky and sometimes fails in CI and otherwise:
3152 * don't run unless user opts in via environment variable.
3154 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3155 qtest_add_func("/migration/multifd/tcp/plain/cancel",
3156 test_multifd_tcp_cancel
);
3158 qtest_add_func("/migration/multifd/tcp/plain/zlib",
3159 test_multifd_tcp_zlib
);
3161 qtest_add_func("/migration/multifd/tcp/plain/zstd",
3162 test_multifd_tcp_zstd
);
3164 #ifdef CONFIG_GNUTLS
3165 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
3166 test_multifd_tcp_tls_psk_match
);
3167 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
3168 test_multifd_tcp_tls_psk_mismatch
);
3170 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
3171 test_multifd_tcp_tls_x509_default_host
);
3172 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
3173 test_multifd_tcp_tls_x509_override_host
);
3174 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
3175 test_multifd_tcp_tls_x509_mismatch_host
);
3176 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
3177 test_multifd_tcp_tls_x509_allow_anon_client
);
3178 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
3179 test_multifd_tcp_tls_x509_reject_anon_client
);
3180 #endif /* CONFIG_TASN1 */
3181 #endif /* CONFIG_GNUTLS */
3183 if (g_str_equal(arch
, "x86_64") && has_kvm
&& kvm_dirty_ring_supported()) {
3184 qtest_add_func("/migration/dirty_ring",
3185 test_precopy_unix_dirty_ring
);
3186 qtest_add_func("/migration/vcpu_dirty_limit",
3187 test_vcpu_dirty_limit
);
3192 g_assert_cmpint(ret
, ==, 0);
3197 g_test_message("unable to rmdir: path (%s): %s",
3198 tmpfs
, strerror(errno
));