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
;
747 if (args
->use_shmem
) {
748 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
749 g_test_skip("/dev/shm is not supported");
754 got_src_stop
= false;
755 got_dst_resume
= false;
756 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
757 memory_size
= "150M";
758 arch_opts
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
759 start_address
= X86_TEST_MEM_START
;
760 end_address
= X86_TEST_MEM_END
;
761 } else if (g_str_equal(arch
, "s390x")) {
762 memory_size
= "128M";
763 arch_opts
= g_strdup_printf("-bios %s", bootpath
);
764 start_address
= S390_TEST_MEM_START
;
765 end_address
= S390_TEST_MEM_END
;
766 } else if (strcmp(arch
, "ppc64") == 0) {
767 memory_size
= "256M";
768 start_address
= PPC_TEST_MEM_START
;
769 end_address
= PPC_TEST_MEM_END
;
770 arch_source
= g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
771 "'nvramrc=hex .\" _\" begin %x %x "
772 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
773 "until'", end_address
, start_address
);
774 arch_opts
= g_strdup("-nodefaults -machine vsmt=8");
775 } else if (strcmp(arch
, "aarch64") == 0) {
776 memory_size
= "150M";
777 arch_opts
= g_strdup_printf("-machine virt,gic-version=max -cpu max "
778 "-kernel %s", bootpath
);
779 start_address
= ARM_TEST_MEM_START
;
780 end_address
= ARM_TEST_MEM_END
;
782 g_assert_not_reached();
785 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
787 ignore_stderr
= "2>/dev/null";
790 * On Windows the QEMU executable is created via CreateProcess() and
791 * IO redirection does not work, so don't bother adding IO redirection
792 * to the command line.
800 if (args
->use_shmem
) {
801 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
802 shmem_opts
= g_strdup_printf(
803 "-object memory-backend-file,id=mem0,size=%s"
804 ",mem-path=%s,share=on -numa node,memdev=mem0",
805 memory_size
, shmem_path
);
808 if (args
->use_dirty_ring
) {
809 kvm_opts
= ",dirty-ring-size=4096";
812 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg "
813 "-name source,debug-threads=on "
815 "-serial file:%s/src_serial "
817 kvm_opts
? kvm_opts
: "",
819 arch_opts
? arch_opts
: "",
820 arch_source
? arch_source
: "",
821 shmem_opts
? shmem_opts
: "",
822 args
->opts_source
? args
->opts_source
: "",
824 if (!args
->only_target
) {
825 *from
= qtest_init(cmd_source
);
826 qtest_qmp_set_event_callback(*from
,
827 migrate_watch_for_stop
,
831 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg "
832 "-name target,debug-threads=on "
834 "-serial file:%s/dest_serial "
837 kvm_opts
? kvm_opts
: "",
838 memory_size
, tmpfs
, uri
,
839 arch_opts
? arch_opts
: "",
840 arch_target
? arch_target
: "",
841 shmem_opts
? shmem_opts
: "",
842 args
->opts_target
? args
->opts_target
: "",
844 *to
= qtest_init(cmd_target
);
845 qtest_qmp_set_event_callback(*to
,
846 migrate_watch_for_resume
,
850 * Remove shmem file immediately to avoid memory leak in test failed case.
851 * It's valid because QEMU has already opened this file
853 if (args
->use_shmem
) {
860 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
862 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
867 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
869 /* Destination still running, wait for a byte to change */
871 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
873 } while (dest_byte_a
== dest_byte_b
);
875 qtest_qmp_assert_success(to
, "{ 'execute' : 'stop'}");
877 /* With it stopped, check nothing changes */
878 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
880 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
881 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
883 check_guests_ram(to
);
888 cleanup("migsocket");
889 cleanup("src_serial");
890 cleanup("dest_serial");
891 cleanup(FILE_TEST_FILENAME
);
895 struct TestMigrateTLSPSKData
{
903 test_migrate_tls_psk_start_common(QTestState
*from
,
907 struct TestMigrateTLSPSKData
*data
=
908 g_new0(struct TestMigrateTLSPSKData
, 1);
910 data
->workdir
= g_strdup_printf("%s/tlscredspsk0", tmpfs
);
911 data
->pskfile
= g_strdup_printf("%s/%s", data
->workdir
,
912 QCRYPTO_TLS_CREDS_PSKFILE
);
913 g_mkdir_with_parents(data
->workdir
, 0700);
914 test_tls_psk_init(data
->pskfile
);
917 data
->workdiralt
= g_strdup_printf("%s/tlscredspskalt0", tmpfs
);
918 data
->pskfilealt
= g_strdup_printf("%s/%s", data
->workdiralt
,
919 QCRYPTO_TLS_CREDS_PSKFILE
);
920 g_mkdir_with_parents(data
->workdiralt
, 0700);
921 test_tls_psk_init_alt(data
->pskfilealt
);
924 qtest_qmp_assert_success(from
,
925 "{ 'execute': 'object-add',"
926 " 'arguments': { 'qom-type': 'tls-creds-psk',"
927 " 'id': 'tlscredspsk0',"
928 " 'endpoint': 'client',"
930 " 'username': 'qemu'} }",
933 qtest_qmp_assert_success(to
,
934 "{ 'execute': 'object-add',"
935 " 'arguments': { 'qom-type': 'tls-creds-psk',"
936 " 'id': 'tlscredspsk0',"
937 " 'endpoint': 'server',"
939 mismatch
? data
->workdiralt
: data
->workdir
);
941 migrate_set_parameter_str(from
, "tls-creds", "tlscredspsk0");
942 migrate_set_parameter_str(to
, "tls-creds", "tlscredspsk0");
948 test_migrate_tls_psk_start_match(QTestState
*from
,
951 return test_migrate_tls_psk_start_common(from
, to
, false);
955 test_migrate_tls_psk_start_mismatch(QTestState
*from
,
958 return test_migrate_tls_psk_start_common(from
, to
, true);
962 test_migrate_tls_psk_finish(QTestState
*from
,
966 struct TestMigrateTLSPSKData
*data
= opaque
;
968 test_tls_psk_cleanup(data
->pskfile
);
969 if (data
->pskfilealt
) {
970 test_tls_psk_cleanup(data
->pskfilealt
);
972 rmdir(data
->workdir
);
973 if (data
->workdiralt
) {
974 rmdir(data
->workdiralt
);
977 g_free(data
->workdiralt
);
978 g_free(data
->pskfilealt
);
979 g_free(data
->workdir
);
980 g_free(data
->pskfile
);
993 } TestMigrateTLSX509Data
;
1000 const char *certhostname
;
1001 const char *certipaddr
;
1002 } TestMigrateTLSX509
;
1005 test_migrate_tls_x509_start_common(QTestState
*from
,
1007 TestMigrateTLSX509
*args
)
1009 TestMigrateTLSX509Data
*data
= g_new0(TestMigrateTLSX509Data
, 1);
1011 data
->workdir
= g_strdup_printf("%s/tlscredsx5090", tmpfs
);
1012 data
->keyfile
= g_strdup_printf("%s/key.pem", data
->workdir
);
1014 data
->cacert
= g_strdup_printf("%s/ca-cert.pem", data
->workdir
);
1015 data
->serverkey
= g_strdup_printf("%s/server-key.pem", data
->workdir
);
1016 data
->servercert
= g_strdup_printf("%s/server-cert.pem", data
->workdir
);
1017 if (args
->clientcert
) {
1018 data
->clientkey
= g_strdup_printf("%s/client-key.pem", data
->workdir
);
1019 data
->clientcert
= g_strdup_printf("%s/client-cert.pem", data
->workdir
);
1022 g_mkdir_with_parents(data
->workdir
, 0700);
1024 test_tls_init(data
->keyfile
);
1026 g_assert(link(data
->keyfile
, data
->serverkey
) == 0);
1028 g_assert(CreateHardLink(data
->serverkey
, data
->keyfile
, NULL
) != 0);
1030 if (args
->clientcert
) {
1032 g_assert(link(data
->keyfile
, data
->clientkey
) == 0);
1034 g_assert(CreateHardLink(data
->clientkey
, data
->keyfile
, NULL
) != 0);
1038 TLS_ROOT_REQ_SIMPLE(cacertreq
, data
->cacert
);
1039 if (args
->clientcert
) {
1040 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq
, cacertreq
,
1041 args
->hostileclient
?
1042 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME
:
1043 QCRYPTO_TLS_TEST_CLIENT_NAME
,
1047 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq
, cacertreq
,
1052 qtest_qmp_assert_success(from
,
1053 "{ 'execute': 'object-add',"
1054 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1055 " 'id': 'tlscredsx509client0',"
1056 " 'endpoint': 'client',"
1058 " 'sanity-check': true,"
1059 " 'verify-peer': true} }",
1061 migrate_set_parameter_str(from
, "tls-creds", "tlscredsx509client0");
1062 if (args
->certhostname
) {
1063 migrate_set_parameter_str(from
, "tls-hostname", args
->certhostname
);
1066 qtest_qmp_assert_success(to
,
1067 "{ 'execute': 'object-add',"
1068 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1069 " 'id': 'tlscredsx509server0',"
1070 " 'endpoint': 'server',"
1072 " 'sanity-check': true,"
1073 " 'verify-peer': %i} }",
1074 data
->workdir
, args
->verifyclient
);
1075 migrate_set_parameter_str(to
, "tls-creds", "tlscredsx509server0");
1077 if (args
->authzclient
) {
1078 qtest_qmp_assert_success(to
,
1079 "{ 'execute': 'object-add',"
1080 " 'arguments': { 'qom-type': 'authz-simple',"
1081 " 'id': 'tlsauthz0',"
1082 " 'identity': %s} }",
1083 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME
);
1084 migrate_set_parameter_str(to
, "tls-authz", "tlsauthz0");
1091 * The normal case: match server's cert hostname against
1092 * whatever host we were telling QEMU to connect to (if any)
1095 test_migrate_tls_x509_start_default_host(QTestState
*from
,
1098 TestMigrateTLSX509 args
= {
1099 .verifyclient
= true,
1101 .certipaddr
= "127.0.0.1"
1103 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1107 * The unusual case: the server's cert is different from
1108 * the address we're telling QEMU to connect to (if any),
1109 * so we must give QEMU an explicit hostname to validate
1112 test_migrate_tls_x509_start_override_host(QTestState
*from
,
1115 TestMigrateTLSX509 args
= {
1116 .verifyclient
= true,
1118 .certhostname
= "qemu.org",
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, and so we
1126 * expect the client to reject the server
1129 test_migrate_tls_x509_start_mismatch_host(QTestState
*from
,
1132 TestMigrateTLSX509 args
= {
1133 .verifyclient
= true,
1135 .certipaddr
= "10.0.0.1",
1137 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1141 test_migrate_tls_x509_start_friendly_client(QTestState
*from
,
1144 TestMigrateTLSX509 args
= {
1145 .verifyclient
= true,
1147 .authzclient
= true,
1148 .certipaddr
= "127.0.0.1",
1150 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1154 test_migrate_tls_x509_start_hostile_client(QTestState
*from
,
1157 TestMigrateTLSX509 args
= {
1158 .verifyclient
= true,
1160 .hostileclient
= true,
1161 .authzclient
= true,
1162 .certipaddr
= "127.0.0.1",
1164 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1168 * The case with no client certificate presented,
1169 * and no server verification
1172 test_migrate_tls_x509_start_allow_anon_client(QTestState
*from
,
1175 TestMigrateTLSX509 args
= {
1176 .certipaddr
= "127.0.0.1",
1178 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1182 * The case with no client certificate presented,
1183 * and server verification rejecting
1186 test_migrate_tls_x509_start_reject_anon_client(QTestState
*from
,
1189 TestMigrateTLSX509 args
= {
1190 .verifyclient
= true,
1191 .certipaddr
= "127.0.0.1",
1193 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1197 test_migrate_tls_x509_finish(QTestState
*from
,
1201 TestMigrateTLSX509Data
*data
= opaque
;
1203 test_tls_cleanup(data
->keyfile
);
1204 g_free(data
->keyfile
);
1206 unlink(data
->cacert
);
1207 g_free(data
->cacert
);
1208 unlink(data
->servercert
);
1209 g_free(data
->servercert
);
1210 unlink(data
->serverkey
);
1211 g_free(data
->serverkey
);
1213 if (data
->clientcert
) {
1214 unlink(data
->clientcert
);
1215 g_free(data
->clientcert
);
1217 if (data
->clientkey
) {
1218 unlink(data
->clientkey
);
1219 g_free(data
->clientkey
);
1222 rmdir(data
->workdir
);
1223 g_free(data
->workdir
);
1227 #endif /* CONFIG_TASN1 */
1228 #endif /* CONFIG_GNUTLS */
1231 test_migrate_compress_start(QTestState
*from
,
1234 migrate_set_parameter_int(from
, "compress-level", 1);
1235 migrate_set_parameter_int(from
, "compress-threads", 4);
1236 migrate_set_parameter_bool(from
, "compress-wait-thread", true);
1237 migrate_set_parameter_int(to
, "decompress-threads", 4);
1239 migrate_set_capability(from
, "compress", true);
1240 migrate_set_capability(to
, "compress", true);
1246 test_migrate_compress_nowait_start(QTestState
*from
,
1249 migrate_set_parameter_int(from
, "compress-level", 9);
1250 migrate_set_parameter_int(from
, "compress-threads", 1);
1251 migrate_set_parameter_bool(from
, "compress-wait-thread", false);
1252 migrate_set_parameter_int(to
, "decompress-threads", 1);
1254 migrate_set_capability(from
, "compress", true);
1255 migrate_set_capability(to
, "compress", true);
1260 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
1261 QTestState
**to_ptr
,
1262 MigrateCommon
*args
)
1264 QTestState
*from
, *to
;
1266 if (test_migrate_start(&from
, &to
, "defer", &args
->start
)) {
1270 if (args
->start_hook
) {
1271 args
->postcopy_data
= args
->start_hook(from
, to
);
1274 migrate_set_capability(from
, "postcopy-ram", true);
1275 migrate_set_capability(to
, "postcopy-ram", true);
1276 migrate_set_capability(to
, "postcopy-blocktime", true);
1278 if (args
->postcopy_preempt
) {
1279 migrate_set_capability(from
, "postcopy-preempt", true);
1280 migrate_set_capability(to
, "postcopy-preempt", true);
1283 migrate_ensure_non_converge(from
);
1285 migrate_prepare_for_dirty_mem(from
);
1286 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1287 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1289 /* Wait for the first serial output from the source */
1290 wait_for_serial("src_serial");
1292 g_autofree
char *uri
= migrate_get_socket_address(to
, "socket-address");
1293 migrate_qmp(from
, uri
, "{}");
1295 migrate_wait_for_dirty_mem(from
, to
);
1303 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
,
1304 MigrateCommon
*args
)
1306 wait_for_migration_complete(from
);
1308 /* Make sure we get at least one "B" on destination */
1309 wait_for_serial("dest_serial");
1311 if (uffd_feature_thread_id
) {
1315 if (args
->finish_hook
) {
1316 args
->finish_hook(from
, to
, args
->postcopy_data
);
1317 args
->postcopy_data
= NULL
;
1320 test_migrate_end(from
, to
, true);
1323 static void test_postcopy_common(MigrateCommon
*args
)
1325 QTestState
*from
, *to
;
1327 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1330 migrate_postcopy_start(from
, to
);
1331 migrate_postcopy_complete(from
, to
, args
);
1334 static void test_postcopy(void)
1336 MigrateCommon args
= { };
1338 test_postcopy_common(&args
);
1341 static void test_postcopy_compress(void)
1343 MigrateCommon args
= {
1344 .start_hook
= test_migrate_compress_start
1347 test_postcopy_common(&args
);
1350 static void test_postcopy_preempt(void)
1352 MigrateCommon args
= {
1353 .postcopy_preempt
= true,
1356 test_postcopy_common(&args
);
1359 #ifdef CONFIG_GNUTLS
1360 static void test_postcopy_tls_psk(void)
1362 MigrateCommon args
= {
1363 .start_hook
= test_migrate_tls_psk_start_match
,
1364 .finish_hook
= test_migrate_tls_psk_finish
,
1367 test_postcopy_common(&args
);
1370 static void test_postcopy_preempt_tls_psk(void)
1372 MigrateCommon args
= {
1373 .postcopy_preempt
= true,
1374 .start_hook
= test_migrate_tls_psk_start_match
,
1375 .finish_hook
= test_migrate_tls_psk_finish
,
1378 test_postcopy_common(&args
);
1382 static void test_postcopy_recovery_common(MigrateCommon
*args
)
1384 QTestState
*from
, *to
;
1385 g_autofree
char *uri
= NULL
;
1387 /* Always hide errors for postcopy recover tests since they're expected */
1388 args
->start
.hide_stderr
= true;
1390 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1394 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1395 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
1397 /* Now we start the postcopy */
1398 migrate_postcopy_start(from
, to
);
1401 * Wait until postcopy is really started; we can only run the
1402 * migrate-pause command during a postcopy
1404 wait_for_migration_status(from
, "postcopy-active", NULL
);
1407 * Manually stop the postcopy migration. This emulates a network
1408 * failure with the migration socket
1410 migrate_pause(from
);
1413 * Wait for destination side to reach postcopy-paused state. The
1414 * migrate-recover command can only succeed if destination machine
1415 * is in the paused state
1417 wait_for_migration_status(to
, "postcopy-paused",
1418 (const char * []) { "failed", "active",
1419 "completed", NULL
});
1422 * Create a new socket to emulate a new channel that is different
1423 * from the broken migration channel; tell the destination to
1424 * listen to the new port
1426 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
1427 migrate_recover(to
, uri
);
1430 * Try to rebuild the migration channel using the resume flag and
1431 * the newly created channel
1433 wait_for_migration_status(from
, "postcopy-paused",
1434 (const char * []) { "failed", "active",
1435 "completed", NULL
});
1436 migrate_qmp(from
, uri
, "{'resume': true}");
1438 /* Restore the postcopy bandwidth to unlimited */
1439 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
1441 migrate_postcopy_complete(from
, to
, args
);
1444 static void test_postcopy_recovery(void)
1446 MigrateCommon args
= { };
1448 test_postcopy_recovery_common(&args
);
1451 static void test_postcopy_recovery_compress(void)
1453 MigrateCommon args
= {
1454 .start_hook
= test_migrate_compress_start
1457 test_postcopy_recovery_common(&args
);
1460 #ifdef CONFIG_GNUTLS
1461 static void test_postcopy_recovery_tls_psk(void)
1463 MigrateCommon args
= {
1464 .start_hook
= test_migrate_tls_psk_start_match
,
1465 .finish_hook
= test_migrate_tls_psk_finish
,
1468 test_postcopy_recovery_common(&args
);
1472 static void test_postcopy_preempt_recovery(void)
1474 MigrateCommon args
= {
1475 .postcopy_preempt
= true,
1478 test_postcopy_recovery_common(&args
);
1481 #ifdef CONFIG_GNUTLS
1482 /* This contains preempt+recovery+tls test altogether */
1483 static void test_postcopy_preempt_all(void)
1485 MigrateCommon args
= {
1486 .postcopy_preempt
= true,
1487 .start_hook
= test_migrate_tls_psk_start_match
,
1488 .finish_hook
= test_migrate_tls_psk_finish
,
1491 test_postcopy_recovery_common(&args
);
1496 static void test_baddest(void)
1498 MigrateStart args
= {
1501 QTestState
*from
, *to
;
1503 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1506 migrate_qmp(from
, "tcp:127.0.0.1:0", "{}");
1507 wait_for_migration_fail(from
, false);
1508 test_migrate_end(from
, to
, false);
1512 static void test_analyze_script(void)
1514 MigrateStart args
= {
1515 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
1517 QTestState
*from
, *to
;
1518 g_autofree
char *uri
= NULL
;
1519 g_autofree
char *file
= NULL
;
1521 const char *python
= g_getenv("PYTHON");
1524 g_test_skip("PYTHON variable not set");
1529 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1534 * Setting these two capabilities causes the "configuration"
1535 * vmstate to include subsections for them. The script needs to
1536 * parse those subsections properly.
1538 migrate_set_capability(from
, "validate-uuid", true);
1539 migrate_set_capability(from
, "x-ignore-shared", true);
1541 file
= g_strdup_printf("%s/migfile", tmpfs
);
1542 uri
= g_strdup_printf("exec:cat > %s", file
);
1544 migrate_ensure_converge(from
);
1545 migrate_qmp(from
, uri
, "{}");
1546 wait_for_migration_complete(from
);
1551 open("/dev/null", O_WRONLY
);
1552 execl(python
, python
, ANALYZE_SCRIPT
, "-f", file
, NULL
);
1553 g_assert_not_reached();
1556 g_assert(waitpid(pid
, &wstatus
, 0) == pid
);
1557 if (WIFEXITED(wstatus
) && WEXITSTATUS(wstatus
) != 0) {
1558 g_test_message("Failed to analyze the migration stream");
1561 test_migrate_end(from
, to
, false);
1566 static void test_precopy_common(MigrateCommon
*args
)
1568 QTestState
*from
, *to
;
1569 void *data_hook
= NULL
;
1570 g_autofree
char *connect_uri
= NULL
;
1572 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1576 if (args
->start_hook
) {
1577 data_hook
= args
->start_hook(from
, to
);
1580 /* Wait for the first serial output from the source */
1581 if (args
->result
== MIG_TEST_SUCCEED
) {
1582 wait_for_serial("src_serial");
1586 migrate_ensure_non_converge(from
);
1587 migrate_prepare_for_dirty_mem(from
);
1590 * Testing non-live migration, we allow it to run at
1591 * full speed to ensure short test case duration.
1592 * For tests expected to fail, we don't need to
1595 if (args
->result
== MIG_TEST_SUCCEED
) {
1596 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1597 if (!got_src_stop
) {
1598 qtest_qmp_eventwait(from
, "STOP");
1600 migrate_ensure_converge(from
);
1604 if (!args
->connect_uri
) {
1605 connect_uri
= migrate_get_socket_address(to
, "socket-address");
1607 connect_uri
= g_strdup(args
->connect_uri
);
1610 if (args
->result
== MIG_TEST_QMP_ERROR
) {
1611 migrate_qmp_fail(from
, connect_uri
, "{}");
1615 migrate_qmp(from
, connect_uri
, "{}");
1617 if (args
->result
!= MIG_TEST_SUCCEED
) {
1618 bool allow_active
= args
->result
== MIG_TEST_FAIL
;
1619 wait_for_migration_fail(from
, allow_active
);
1621 if (args
->result
== MIG_TEST_FAIL_DEST_QUIT_ERR
) {
1622 qtest_set_expected_status(to
, EXIT_FAILURE
);
1627 * For initial iteration(s) we must do a full pass,
1628 * but for the final iteration, we need only wait
1629 * for some dirty mem before switching to converge
1631 while (args
->iterations
> 1) {
1632 wait_for_migration_pass(from
);
1635 migrate_wait_for_dirty_mem(from
, to
);
1637 migrate_ensure_converge(from
);
1640 * We do this first, as it has a timeout to stop us
1641 * hanging forever if migration didn't converge
1643 wait_for_migration_complete(from
);
1645 if (!got_src_stop
) {
1646 qtest_qmp_eventwait(from
, "STOP");
1649 wait_for_migration_complete(from
);
1651 * Must wait for dst to finish reading all incoming
1652 * data on the socket before issuing 'cont' otherwise
1655 wait_for_migration_complete(to
);
1657 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1660 if (!got_dst_resume
) {
1661 qtest_qmp_eventwait(to
, "RESUME");
1664 wait_for_serial("dest_serial");
1668 if (args
->finish_hook
) {
1669 args
->finish_hook(from
, to
, data_hook
);
1672 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1675 static void test_file_common(MigrateCommon
*args
, bool stop_src
)
1677 QTestState
*from
, *to
;
1678 void *data_hook
= NULL
;
1679 g_autofree
char *connect_uri
= g_strdup(args
->connect_uri
);
1681 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1686 * File migration is never live. We can keep the source VM running
1687 * during migration, but the destination will not be running
1690 g_assert_false(args
->live
);
1692 if (args
->start_hook
) {
1693 data_hook
= args
->start_hook(from
, to
);
1696 migrate_ensure_converge(from
);
1697 wait_for_serial("src_serial");
1700 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1701 if (!got_src_stop
) {
1702 qtest_qmp_eventwait(from
, "STOP");
1706 if (args
->result
== MIG_TEST_QMP_ERROR
) {
1707 migrate_qmp_fail(from
, connect_uri
, "{}");
1711 migrate_qmp(from
, connect_uri
, "{}");
1712 wait_for_migration_complete(from
);
1715 * We need to wait for the source to finish before starting the
1718 migrate_incoming_qmp(to
, connect_uri
, "{}");
1719 wait_for_migration_complete(to
);
1722 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1725 if (!got_dst_resume
) {
1726 qtest_qmp_eventwait(to
, "RESUME");
1729 wait_for_serial("dest_serial");
1732 if (args
->finish_hook
) {
1733 args
->finish_hook(from
, to
, data_hook
);
1736 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1739 static void test_precopy_unix_plain(void)
1741 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1742 MigrateCommon args
= {
1746 * The simplest use case of precopy, covering smoke tests of
1747 * get-dirty-log dirty tracking.
1752 test_precopy_common(&args
);
1756 static void test_precopy_unix_dirty_ring(void)
1758 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1759 MigrateCommon args
= {
1761 .use_dirty_ring
= true,
1766 * Besides the precopy/unix basic test, cover dirty ring interface
1767 * rather than get-dirty-log.
1772 test_precopy_common(&args
);
1775 #ifdef CONFIG_GNUTLS
1776 static void test_precopy_unix_tls_psk(void)
1778 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1779 MigrateCommon args
= {
1782 .start_hook
= test_migrate_tls_psk_start_match
,
1783 .finish_hook
= test_migrate_tls_psk_finish
,
1786 test_precopy_common(&args
);
1790 static void test_precopy_unix_tls_x509_default_host(void)
1792 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1793 MigrateCommon args
= {
1795 .hide_stderr
= true,
1799 .start_hook
= test_migrate_tls_x509_start_default_host
,
1800 .finish_hook
= test_migrate_tls_x509_finish
,
1801 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1804 test_precopy_common(&args
);
1807 static void test_precopy_unix_tls_x509_override_host(void)
1809 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1810 MigrateCommon args
= {
1813 .start_hook
= test_migrate_tls_x509_start_override_host
,
1814 .finish_hook
= test_migrate_tls_x509_finish
,
1817 test_precopy_common(&args
);
1819 #endif /* CONFIG_TASN1 */
1820 #endif /* CONFIG_GNUTLS */
1823 /* Currently upset on aarch64 TCG */
1824 static void test_ignore_shared(void)
1826 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1827 QTestState
*from
, *to
;
1829 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
1833 migrate_ensure_non_converge(from
);
1834 migrate_prepare_for_dirty_mem(from
);
1836 migrate_set_capability(from
, "x-ignore-shared", true);
1837 migrate_set_capability(to
, "x-ignore-shared", true);
1839 /* Wait for the first serial output from the source */
1840 wait_for_serial("src_serial");
1842 migrate_qmp(from
, uri
, "{}");
1844 migrate_wait_for_dirty_mem(from
, to
);
1846 if (!got_src_stop
) {
1847 qtest_qmp_eventwait(from
, "STOP");
1850 qtest_qmp_eventwait(to
, "RESUME");
1852 wait_for_serial("dest_serial");
1853 wait_for_migration_complete(from
);
1855 /* Check whether shared RAM has been really skipped */
1856 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1858 test_migrate_end(from
, to
, true);
1863 test_migrate_xbzrle_start(QTestState
*from
,
1866 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1868 migrate_set_capability(from
, "xbzrle", true);
1869 migrate_set_capability(to
, "xbzrle", true);
1874 static void test_precopy_unix_xbzrle(void)
1876 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1877 MigrateCommon args
= {
1880 .start_hook
= test_migrate_xbzrle_start
,
1883 * XBZRLE needs pages to be modified when doing the 2nd+ round
1884 * iteration to have real data pushed to the stream.
1889 test_precopy_common(&args
);
1892 static void test_precopy_unix_compress(void)
1894 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1895 MigrateCommon args
= {
1898 .start_hook
= test_migrate_compress_start
,
1900 * Test that no invalid thread state is left over from
1901 * the previous iteration.
1905 * We make sure the compressor can always work well even if guest
1906 * memory is changing. See commit 34ab9e9743 where we used to fix
1907 * a bug when only trigger-able with guest memory changing.
1912 test_precopy_common(&args
);
1915 static void test_precopy_unix_compress_nowait(void)
1917 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1918 MigrateCommon args
= {
1921 .start_hook
= test_migrate_compress_nowait_start
,
1923 * Test that no invalid thread state is left over from
1924 * the previous iteration.
1927 /* Same reason for the wait version of precopy compress test */
1931 test_precopy_common(&args
);
1934 static void test_precopy_file(void)
1936 g_autofree
char *uri
= g_strdup_printf("file:%s/%s", tmpfs
,
1937 FILE_TEST_FILENAME
);
1938 MigrateCommon args
= {
1940 .listen_uri
= "defer",
1943 test_file_common(&args
, true);
1946 static void file_offset_finish_hook(QTestState
*from
, QTestState
*to
,
1949 #if defined(__linux__)
1950 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, FILE_TEST_FILENAME
);
1951 size_t size
= FILE_TEST_OFFSET
+ sizeof(QEMU_VM_FILE_MAGIC
);
1952 uintptr_t *addr
, *p
;
1955 fd
= open(path
, O_RDONLY
);
1957 addr
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0);
1958 g_assert(addr
!= MAP_FAILED
);
1961 * Ensure the skipped offset contains zeros and the migration
1962 * stream starts at the right place.
1965 while (p
< addr
+ FILE_TEST_OFFSET
/ sizeof(uintptr_t)) {
1969 g_assert_cmpint(cpu_to_be64(*p
) >> 32, ==, QEMU_VM_FILE_MAGIC
);
1976 static void test_precopy_file_offset(void)
1978 g_autofree
char *uri
= g_strdup_printf("file:%s/%s,offset=%d", tmpfs
,
1981 MigrateCommon args
= {
1983 .listen_uri
= "defer",
1984 .finish_hook
= file_offset_finish_hook
,
1987 test_file_common(&args
, false);
1990 static void test_precopy_file_offset_bad(void)
1992 /* using a value not supported by qemu_strtosz() */
1993 g_autofree
char *uri
= g_strdup_printf("file:%s/%s,offset=0x20M",
1994 tmpfs
, FILE_TEST_FILENAME
);
1995 MigrateCommon args
= {
1997 .listen_uri
= "defer",
1998 .result
= MIG_TEST_QMP_ERROR
,
2001 test_file_common(&args
, false);
2004 static void test_precopy_tcp_plain(void)
2006 MigrateCommon args
= {
2007 .listen_uri
= "tcp:127.0.0.1:0",
2010 test_precopy_common(&args
);
2013 static void *test_migrate_switchover_ack_start(QTestState
*from
, QTestState
*to
)
2016 migrate_set_capability(from
, "return-path", true);
2017 migrate_set_capability(to
, "return-path", true);
2019 migrate_set_capability(from
, "switchover-ack", true);
2020 migrate_set_capability(to
, "switchover-ack", true);
2025 static void test_precopy_tcp_switchover_ack(void)
2027 MigrateCommon args
= {
2028 .listen_uri
= "tcp:127.0.0.1:0",
2029 .start_hook
= test_migrate_switchover_ack_start
,
2031 * Source VM must be running in order to consider the switchover ACK
2032 * when deciding to do switchover or not.
2037 test_precopy_common(&args
);
2040 #ifdef CONFIG_GNUTLS
2041 static void test_precopy_tcp_tls_psk_match(void)
2043 MigrateCommon args
= {
2044 .listen_uri
= "tcp:127.0.0.1:0",
2045 .start_hook
= test_migrate_tls_psk_start_match
,
2046 .finish_hook
= test_migrate_tls_psk_finish
,
2049 test_precopy_common(&args
);
2052 static void test_precopy_tcp_tls_psk_mismatch(void)
2054 MigrateCommon args
= {
2056 .hide_stderr
= true,
2058 .listen_uri
= "tcp:127.0.0.1:0",
2059 .start_hook
= test_migrate_tls_psk_start_mismatch
,
2060 .finish_hook
= test_migrate_tls_psk_finish
,
2061 .result
= MIG_TEST_FAIL
,
2064 test_precopy_common(&args
);
2068 static void test_precopy_tcp_tls_x509_default_host(void)
2070 MigrateCommon args
= {
2071 .listen_uri
= "tcp:127.0.0.1:0",
2072 .start_hook
= test_migrate_tls_x509_start_default_host
,
2073 .finish_hook
= test_migrate_tls_x509_finish
,
2076 test_precopy_common(&args
);
2079 static void test_precopy_tcp_tls_x509_override_host(void)
2081 MigrateCommon args
= {
2082 .listen_uri
= "tcp:127.0.0.1:0",
2083 .start_hook
= test_migrate_tls_x509_start_override_host
,
2084 .finish_hook
= test_migrate_tls_x509_finish
,
2087 test_precopy_common(&args
);
2090 static void test_precopy_tcp_tls_x509_mismatch_host(void)
2092 MigrateCommon args
= {
2094 .hide_stderr
= true,
2096 .listen_uri
= "tcp:127.0.0.1:0",
2097 .start_hook
= test_migrate_tls_x509_start_mismatch_host
,
2098 .finish_hook
= test_migrate_tls_x509_finish
,
2099 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
2102 test_precopy_common(&args
);
2105 static void test_precopy_tcp_tls_x509_friendly_client(void)
2107 MigrateCommon args
= {
2108 .listen_uri
= "tcp:127.0.0.1:0",
2109 .start_hook
= test_migrate_tls_x509_start_friendly_client
,
2110 .finish_hook
= test_migrate_tls_x509_finish
,
2113 test_precopy_common(&args
);
2116 static void test_precopy_tcp_tls_x509_hostile_client(void)
2118 MigrateCommon args
= {
2120 .hide_stderr
= true,
2122 .listen_uri
= "tcp:127.0.0.1:0",
2123 .start_hook
= test_migrate_tls_x509_start_hostile_client
,
2124 .finish_hook
= test_migrate_tls_x509_finish
,
2125 .result
= MIG_TEST_FAIL
,
2128 test_precopy_common(&args
);
2131 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2133 MigrateCommon args
= {
2134 .listen_uri
= "tcp:127.0.0.1:0",
2135 .start_hook
= test_migrate_tls_x509_start_allow_anon_client
,
2136 .finish_hook
= test_migrate_tls_x509_finish
,
2139 test_precopy_common(&args
);
2142 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2144 MigrateCommon args
= {
2146 .hide_stderr
= true,
2148 .listen_uri
= "tcp:127.0.0.1:0",
2149 .start_hook
= test_migrate_tls_x509_start_reject_anon_client
,
2150 .finish_hook
= test_migrate_tls_x509_finish
,
2151 .result
= MIG_TEST_FAIL
,
2154 test_precopy_common(&args
);
2156 #endif /* CONFIG_TASN1 */
2157 #endif /* CONFIG_GNUTLS */
2160 static void *test_migrate_fd_start_hook(QTestState
*from
,
2166 /* Create two connected sockets for migration */
2167 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
2168 g_assert_cmpint(ret
, ==, 0);
2170 /* Send the 1st socket to the target */
2171 qtest_qmp_fds_assert_success(to
, &pair
[0], 1,
2172 "{ 'execute': 'getfd',"
2173 " 'arguments': { 'fdname': 'fd-mig' }}");
2176 /* Start incoming migration from the 1st socket */
2177 migrate_incoming_qmp(to
, "fd:fd-mig", "{}");
2179 /* Send the 2nd socket to the target */
2180 qtest_qmp_fds_assert_success(from
, &pair
[1], 1,
2181 "{ 'execute': 'getfd',"
2182 " 'arguments': { 'fdname': 'fd-mig' }}");
2188 static void test_migrate_fd_finish_hook(QTestState
*from
,
2193 const char *error_desc
;
2195 /* Test closing fds */
2196 /* We assume, that QEMU removes named fd from its list,
2197 * so this should fail */
2198 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
2199 " 'arguments': { 'fdname': 'fd-mig' }}");
2200 g_assert_true(qdict_haskey(rsp
, "error"));
2201 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2202 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2205 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
2206 " 'arguments': { 'fdname': 'fd-mig' }}");
2207 g_assert_true(qdict_haskey(rsp
, "error"));
2208 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2209 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2213 static void test_migrate_fd_proto(void)
2215 MigrateCommon args
= {
2216 .listen_uri
= "defer",
2217 .connect_uri
= "fd:fd-mig",
2218 .start_hook
= test_migrate_fd_start_hook
,
2219 .finish_hook
= test_migrate_fd_finish_hook
2221 test_precopy_common(&args
);
2225 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
2227 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2228 QTestState
*from
, *to
;
2230 if (test_migrate_start(&from
, &to
, uri
, args
)) {
2235 * UUID validation is at the begin of migration. So, the main process of
2236 * migration is not interesting for us here. Thus, set huge downtime for
2237 * very fast migration.
2239 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
2240 migrate_set_capability(from
, "validate-uuid", true);
2242 /* Wait for the first serial output from the source */
2243 wait_for_serial("src_serial");
2245 migrate_qmp(from
, uri
, "{}");
2248 qtest_set_expected_status(to
, EXIT_FAILURE
);
2249 wait_for_migration_fail(from
, true);
2251 wait_for_migration_complete(from
);
2254 test_migrate_end(from
, to
, false);
2257 static void test_validate_uuid(void)
2259 MigrateStart args
= {
2260 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2261 .opts_target
= "-uuid 11111111-1111-1111-1111-111111111111",
2264 do_test_validate_uuid(&args
, false);
2267 static void test_validate_uuid_error(void)
2269 MigrateStart args
= {
2270 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2271 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2272 .hide_stderr
= true,
2275 do_test_validate_uuid(&args
, true);
2278 static void test_validate_uuid_src_not_set(void)
2280 MigrateStart args
= {
2281 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2282 .hide_stderr
= true,
2285 do_test_validate_uuid(&args
, false);
2288 static void test_validate_uuid_dst_not_set(void)
2290 MigrateStart args
= {
2291 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2292 .hide_stderr
= true,
2295 do_test_validate_uuid(&args
, false);
2299 * The way auto_converge works, we need to do too many passes to
2300 * run this test. Auto_converge logic is only run once every
2301 * three iterations, so:
2303 * - 3 iterations without auto_converge enabled
2304 * - 3 iterations with pct = 5
2305 * - 3 iterations with pct = 30
2306 * - 3 iterations with pct = 55
2307 * - 3 iterations with pct = 80
2308 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2310 * To make things even worse, we need to run the initial stage at
2311 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2313 static void test_migrate_auto_converge(void)
2315 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2316 MigrateStart args
= {};
2317 QTestState
*from
, *to
;
2321 * We want the test to be stable and as fast as possible.
2322 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2323 * so we need to decrease a bandwidth.
2325 const int64_t init_pct
= 5, inc_pct
= 25, max_pct
= 95;
2327 if (test_migrate_start(&from
, &to
, uri
, &args
)) {
2331 migrate_set_capability(from
, "auto-converge", true);
2332 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
2333 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
2334 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
2337 * Set the initial parameters so that the migration could not converge
2338 * without throttling.
2340 migrate_ensure_non_converge(from
);
2342 /* To check remaining size after precopy */
2343 migrate_set_capability(from
, "pause-before-switchover", true);
2345 /* Wait for the first serial output from the source */
2346 wait_for_serial("src_serial");
2348 migrate_qmp(from
, uri
, "{}");
2350 /* Wait for throttling begins */
2353 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2354 if (percentage
!= 0) {
2358 g_assert_false(got_src_stop
);
2360 /* The first percentage of throttling should be at least init_pct */
2361 g_assert_cmpint(percentage
, >=, init_pct
);
2362 /* Now, when we tested that throttling works, let it converge */
2363 migrate_ensure_converge(from
);
2366 * Wait for pre-switchover status to check last throttle percentage
2367 * and remaining. These values will be zeroed later
2369 wait_for_migration_status(from
, "pre-switchover", NULL
);
2371 /* The final percentage of throttling shouldn't be greater than max_pct */
2372 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2373 g_assert_cmpint(percentage
, <=, max_pct
);
2374 migrate_continue(from
, "pre-switchover");
2376 qtest_qmp_eventwait(to
, "RESUME");
2378 wait_for_serial("dest_serial");
2379 wait_for_migration_complete(from
);
2381 test_migrate_end(from
, to
, true);
2385 test_migrate_precopy_tcp_multifd_start_common(QTestState
*from
,
2389 migrate_set_parameter_int(from
, "multifd-channels", 16);
2390 migrate_set_parameter_int(to
, "multifd-channels", 16);
2392 migrate_set_parameter_str(from
, "multifd-compression", method
);
2393 migrate_set_parameter_str(to
, "multifd-compression", method
);
2395 migrate_set_capability(from
, "multifd", true);
2396 migrate_set_capability(to
, "multifd", true);
2398 /* Start incoming migration from the 1st socket */
2399 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2405 test_migrate_precopy_tcp_multifd_start(QTestState
*from
,
2408 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2412 test_migrate_precopy_tcp_multifd_zlib_start(QTestState
*from
,
2415 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zlib");
2420 test_migrate_precopy_tcp_multifd_zstd_start(QTestState
*from
,
2423 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zstd");
2425 #endif /* CONFIG_ZSTD */
2427 static void test_multifd_tcp_none(void)
2429 MigrateCommon args
= {
2430 .listen_uri
= "defer",
2431 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2433 * Multifd is more complicated than most of the features, it
2434 * directly takes guest page buffers when sending, make sure
2435 * everything will work alright even if guest page is changing.
2439 test_precopy_common(&args
);
2442 static void test_multifd_tcp_zlib(void)
2444 MigrateCommon args
= {
2445 .listen_uri
= "defer",
2446 .start_hook
= test_migrate_precopy_tcp_multifd_zlib_start
,
2448 test_precopy_common(&args
);
2452 static void test_multifd_tcp_zstd(void)
2454 MigrateCommon args
= {
2455 .listen_uri
= "defer",
2456 .start_hook
= test_migrate_precopy_tcp_multifd_zstd_start
,
2458 test_precopy_common(&args
);
2462 #ifdef CONFIG_GNUTLS
2464 test_migrate_multifd_tcp_tls_psk_start_match(QTestState
*from
,
2467 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2468 return test_migrate_tls_psk_start_match(from
, to
);
2472 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState
*from
,
2475 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2476 return test_migrate_tls_psk_start_mismatch(from
, to
);
2481 test_migrate_multifd_tls_x509_start_default_host(QTestState
*from
,
2484 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2485 return test_migrate_tls_x509_start_default_host(from
, to
);
2489 test_migrate_multifd_tls_x509_start_override_host(QTestState
*from
,
2492 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2493 return test_migrate_tls_x509_start_override_host(from
, to
);
2497 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState
*from
,
2500 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2501 return test_migrate_tls_x509_start_mismatch_host(from
, to
);
2505 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState
*from
,
2508 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2509 return test_migrate_tls_x509_start_allow_anon_client(from
, to
);
2513 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState
*from
,
2516 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2517 return test_migrate_tls_x509_start_reject_anon_client(from
, to
);
2519 #endif /* CONFIG_TASN1 */
2521 static void test_multifd_tcp_tls_psk_match(void)
2523 MigrateCommon args
= {
2524 .listen_uri
= "defer",
2525 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_match
,
2526 .finish_hook
= test_migrate_tls_psk_finish
,
2528 test_precopy_common(&args
);
2531 static void test_multifd_tcp_tls_psk_mismatch(void)
2533 MigrateCommon args
= {
2535 .hide_stderr
= true,
2537 .listen_uri
= "defer",
2538 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_mismatch
,
2539 .finish_hook
= test_migrate_tls_psk_finish
,
2540 .result
= MIG_TEST_FAIL
,
2542 test_precopy_common(&args
);
2546 static void test_multifd_tcp_tls_x509_default_host(void)
2548 MigrateCommon args
= {
2549 .listen_uri
= "defer",
2550 .start_hook
= test_migrate_multifd_tls_x509_start_default_host
,
2551 .finish_hook
= test_migrate_tls_x509_finish
,
2553 test_precopy_common(&args
);
2556 static void test_multifd_tcp_tls_x509_override_host(void)
2558 MigrateCommon args
= {
2559 .listen_uri
= "defer",
2560 .start_hook
= test_migrate_multifd_tls_x509_start_override_host
,
2561 .finish_hook
= test_migrate_tls_x509_finish
,
2563 test_precopy_common(&args
);
2566 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2569 * This has different behaviour to the non-multifd case.
2571 * In non-multifd case when client aborts due to mismatched
2572 * cert host, the server has already started trying to load
2573 * migration state, and so it exits with I/O failure.
2575 * In multifd case when client aborts due to mismatched
2576 * cert host, the server is still waiting for the other
2577 * multifd connections to arrive so hasn't started trying
2578 * to load migration state, and thus just aborts the migration
2581 MigrateCommon args
= {
2583 .hide_stderr
= true,
2585 .listen_uri
= "defer",
2586 .start_hook
= test_migrate_multifd_tls_x509_start_mismatch_host
,
2587 .finish_hook
= test_migrate_tls_x509_finish
,
2588 .result
= MIG_TEST_FAIL
,
2590 test_precopy_common(&args
);
2593 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2595 MigrateCommon args
= {
2596 .listen_uri
= "defer",
2597 .start_hook
= test_migrate_multifd_tls_x509_start_allow_anon_client
,
2598 .finish_hook
= test_migrate_tls_x509_finish
,
2600 test_precopy_common(&args
);
2603 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2605 MigrateCommon args
= {
2607 .hide_stderr
= true,
2609 .listen_uri
= "defer",
2610 .start_hook
= test_migrate_multifd_tls_x509_start_reject_anon_client
,
2611 .finish_hook
= test_migrate_tls_x509_finish
,
2612 .result
= MIG_TEST_FAIL
,
2614 test_precopy_common(&args
);
2616 #endif /* CONFIG_TASN1 */
2617 #endif /* CONFIG_GNUTLS */
2625 * launch another target
2628 * And see that it works
2630 static void test_multifd_tcp_cancel(void)
2632 MigrateStart args
= {
2633 .hide_stderr
= true,
2635 QTestState
*from
, *to
, *to2
;
2636 g_autofree
char *uri
= NULL
;
2638 if (test_migrate_start(&from
, &to
, "defer", &args
)) {
2642 migrate_ensure_non_converge(from
);
2643 migrate_prepare_for_dirty_mem(from
);
2645 migrate_set_parameter_int(from
, "multifd-channels", 16);
2646 migrate_set_parameter_int(to
, "multifd-channels", 16);
2648 migrate_set_capability(from
, "multifd", true);
2649 migrate_set_capability(to
, "multifd", true);
2651 /* Start incoming migration from the 1st socket */
2652 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2654 /* Wait for the first serial output from the source */
2655 wait_for_serial("src_serial");
2657 uri
= migrate_get_socket_address(to
, "socket-address");
2659 migrate_qmp(from
, uri
, "{}");
2661 migrate_wait_for_dirty_mem(from
, to
);
2663 migrate_cancel(from
);
2665 /* Make sure QEMU process "to" exited */
2666 qtest_set_expected_status(to
, EXIT_FAILURE
);
2667 qtest_wait_qemu(to
);
2669 args
= (MigrateStart
){
2670 .only_target
= true,
2673 if (test_migrate_start(&from
, &to2
, "defer", &args
)) {
2677 migrate_set_parameter_int(to2
, "multifd-channels", 16);
2679 migrate_set_capability(to2
, "multifd", true);
2681 /* Start incoming migration from the 1st socket */
2682 migrate_incoming_qmp(to2
, "tcp:127.0.0.1:0", "{}");
2685 uri
= migrate_get_socket_address(to2
, "socket-address");
2687 wait_for_migration_status(from
, "cancelled", NULL
);
2689 migrate_ensure_non_converge(from
);
2691 migrate_qmp(from
, uri
, "{}");
2693 migrate_wait_for_dirty_mem(from
, to2
);
2695 migrate_ensure_converge(from
);
2697 if (!got_src_stop
) {
2698 qtest_qmp_eventwait(from
, "STOP");
2700 qtest_qmp_eventwait(to2
, "RESUME");
2702 wait_for_serial("dest_serial");
2703 wait_for_migration_complete(from
);
2704 test_migrate_end(from
, to2
, true);
2707 static void calc_dirty_rate(QTestState
*who
, uint64_t calc_time
)
2709 qtest_qmp_assert_success(who
,
2710 "{ 'execute': 'calc-dirty-rate',"
2712 "'calc-time': %" PRIu64
","
2713 "'mode': 'dirty-ring' }}",
2717 static QDict
*query_dirty_rate(QTestState
*who
)
2719 return qtest_qmp_assert_success_ref(who
,
2720 "{ 'execute': 'query-dirty-rate' }");
2723 static void dirtylimit_set_all(QTestState
*who
, uint64_t dirtyrate
)
2725 qtest_qmp_assert_success(who
,
2726 "{ 'execute': 'set-vcpu-dirty-limit',"
2728 "'dirty-rate': %" PRIu64
" } }",
2732 static void cancel_vcpu_dirty_limit(QTestState
*who
)
2734 qtest_qmp_assert_success(who
,
2735 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2738 static QDict
*query_vcpu_dirty_limit(QTestState
*who
)
2742 rsp
= qtest_qmp(who
, "{ 'execute': 'query-vcpu-dirty-limit' }");
2743 g_assert(!qdict_haskey(rsp
, "error"));
2744 g_assert(qdict_haskey(rsp
, "return"));
2749 static bool calc_dirtyrate_ready(QTestState
*who
)
2754 rsp_return
= query_dirty_rate(who
);
2755 g_assert(rsp_return
);
2757 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2760 return g_strcmp0(status
, "measuring");
2763 static void wait_for_calc_dirtyrate_complete(QTestState
*who
,
2766 int max_try_count
= 10000;
2767 usleep(time_s
* 1000000);
2769 while (!calc_dirtyrate_ready(who
) && max_try_count
--) {
2774 * Set the timeout with 10 s(max_try_count * 1000us),
2775 * if dirtyrate measurement not complete, fail test.
2777 g_assert_cmpint(max_try_count
, !=, 0);
2780 static int64_t get_dirty_rate(QTestState
*who
)
2785 const QListEntry
*entry
;
2789 rsp_return
= query_dirty_rate(who
);
2790 g_assert(rsp_return
);
2792 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2794 g_assert_cmpstr(status
, ==, "measured");
2796 rates
= qdict_get_qlist(rsp_return
, "vcpu-dirty-rate");
2797 g_assert(rates
&& !qlist_empty(rates
));
2799 entry
= qlist_first(rates
);
2802 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2805 dirtyrate
= qdict_get_try_int(rate
, "dirty-rate", -1);
2807 qobject_unref(rsp_return
);
2811 static int64_t get_limit_rate(QTestState
*who
)
2815 const QListEntry
*entry
;
2819 rsp_return
= query_vcpu_dirty_limit(who
);
2820 g_assert(rsp_return
);
2822 rates
= qdict_get_qlist(rsp_return
, "return");
2823 g_assert(rates
&& !qlist_empty(rates
));
2825 entry
= qlist_first(rates
);
2828 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2831 dirtyrate
= qdict_get_try_int(rate
, "limit-rate", -1);
2833 qobject_unref(rsp_return
);
2837 static QTestState
*dirtylimit_start_vm(void)
2839 QTestState
*vm
= NULL
;
2841 cmd
= g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2842 "-name dirtylimit-test,debug-threads=on "
2844 "-serial file:%s/vm_serial "
2845 "-drive file=%s,format=raw ",
2848 vm
= qtest_init(cmd
);
2852 static void dirtylimit_stop_vm(QTestState
*vm
)
2855 cleanup("vm_serial");
2858 static void test_vcpu_dirty_limit(void)
2861 int64_t origin_rate
;
2864 int max_try_count
= 20;
2867 /* Start vm for vcpu dirtylimit test */
2868 vm
= dirtylimit_start_vm();
2870 /* Wait for the first serial output from the vm*/
2871 wait_for_serial("vm_serial");
2873 /* Do dirtyrate measurement with calc time equals 1s */
2874 calc_dirty_rate(vm
, 1);
2876 /* Sleep calc time and wait for calc dirtyrate complete */
2877 wait_for_calc_dirtyrate_complete(vm
, 1);
2879 /* Query original dirty page rate */
2880 origin_rate
= get_dirty_rate(vm
);
2882 /* VM booted from bootsect should dirty memory steadily */
2883 assert(origin_rate
!= 0);
2885 /* Setup quota dirty page rate at half of origin */
2886 quota_rate
= origin_rate
/ 2;
2888 /* Set dirtylimit */
2889 dirtylimit_set_all(vm
, quota_rate
);
2892 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2895 g_assert_cmpint(quota_rate
, ==, get_limit_rate(vm
));
2897 /* Sleep a bit to check if it take effect */
2901 * Check if dirtylimit take effect realistically, set the
2902 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2903 * doesn't take effect, fail test.
2905 while (--max_try_count
) {
2906 calc_dirty_rate(vm
, 1);
2907 wait_for_calc_dirtyrate_complete(vm
, 1);
2908 rate
= get_dirty_rate(vm
);
2911 * Assume hitting if current rate is less
2912 * than quota rate (within accepting error)
2914 if (rate
< (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2920 g_assert_cmpint(hit
, ==, 1);
2925 /* Check if dirtylimit cancellation take effect */
2926 cancel_vcpu_dirty_limit(vm
);
2927 while (--max_try_count
) {
2928 calc_dirty_rate(vm
, 1);
2929 wait_for_calc_dirtyrate_complete(vm
, 1);
2930 rate
= get_dirty_rate(vm
);
2933 * Assume dirtylimit be canceled if current rate is
2934 * greater than quota rate (within accepting error)
2936 if (rate
> (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2942 g_assert_cmpint(hit
, ==, 1);
2943 dirtylimit_stop_vm(vm
);
2946 static bool kvm_dirty_ring_supported(void)
2948 #if defined(__linux__) && defined(HOST_X86_64)
2949 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
2955 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
2958 /* We test with 4096 slots */
2969 int main(int argc
, char **argv
)
2971 bool has_kvm
, has_tcg
;
2974 g_autoptr(GError
) err
= NULL
;
2977 g_test_init(&argc
, &argv
, NULL
);
2979 has_kvm
= qtest_has_accel("kvm");
2980 has_tcg
= qtest_has_accel("tcg");
2982 if (!has_tcg
&& !has_kvm
) {
2983 g_test_skip("No KVM or TCG accelerator available");
2987 has_uffd
= ufd_version_check();
2988 arch
= qtest_get_arch();
2991 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2992 * is touchy due to race conditions on dirty bits (especially on PPC for
2995 if (g_str_equal(arch
, "ppc64") &&
2996 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
2997 g_test_message("Skipping test: kvm_hv not available");
2998 return g_test_run();
3002 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
3003 * there until the problems are resolved
3005 if (g_str_equal(arch
, "s390x") && !has_kvm
) {
3006 g_test_message("Skipping test: s390x host with KVM is required");
3007 return g_test_run();
3010 tmpfs
= g_dir_make_tmp("migration-test-XXXXXX", &err
);
3012 g_test_message("Can't create temporary directory in %s: %s",
3013 g_get_tmp_dir(), err
->message
);
3016 bootfile_create(tmpfs
);
3018 module_call_init(MODULE_INIT_QOM
);
3021 qtest_add_func("/migration/postcopy/plain", test_postcopy
);
3022 qtest_add_func("/migration/postcopy/recovery/plain",
3023 test_postcopy_recovery
);
3024 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt
);
3025 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
3026 test_postcopy_preempt_recovery
);
3027 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3028 qtest_add_func("/migration/postcopy/compress/plain",
3029 test_postcopy_compress
);
3030 qtest_add_func("/migration/postcopy/recovery/compress/plain",
3031 test_postcopy_recovery_compress
);
3035 qtest_add_func("/migration/bad_dest", test_baddest
);
3037 if (!g_str_equal(arch
, "s390x")) {
3038 qtest_add_func("/migration/analyze-script", test_analyze_script
);
3041 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain
);
3042 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle
);
3044 * Compression fails from time to time.
3045 * Put test here but don't enable it until everything is fixed.
3047 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3048 qtest_add_func("/migration/precopy/unix/compress/wait",
3049 test_precopy_unix_compress
);
3050 qtest_add_func("/migration/precopy/unix/compress/nowait",
3051 test_precopy_unix_compress_nowait
);
3054 qtest_add_func("/migration/precopy/file",
3056 qtest_add_func("/migration/precopy/file/offset",
3057 test_precopy_file_offset
);
3058 qtest_add_func("/migration/precopy/file/offset/bad",
3059 test_precopy_file_offset_bad
);
3061 #ifdef CONFIG_GNUTLS
3062 qtest_add_func("/migration/precopy/unix/tls/psk",
3063 test_precopy_unix_tls_psk
);
3067 * NOTE: psk test is enough for postcopy, as other types of TLS
3068 * channels are tested under precopy. Here what we want to test is the
3069 * general postcopy path that has TLS channel enabled.
3071 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk
);
3072 qtest_add_func("/migration/postcopy/recovery/tls/psk",
3073 test_postcopy_recovery_tls_psk
);
3074 qtest_add_func("/migration/postcopy/preempt/tls/psk",
3075 test_postcopy_preempt_tls_psk
);
3076 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
3077 test_postcopy_preempt_all
);
3080 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
3081 test_precopy_unix_tls_x509_default_host
);
3082 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
3083 test_precopy_unix_tls_x509_override_host
);
3084 #endif /* CONFIG_TASN1 */
3085 #endif /* CONFIG_GNUTLS */
3087 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain
);
3089 qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
3090 test_precopy_tcp_switchover_ack
);
3092 #ifdef CONFIG_GNUTLS
3093 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
3094 test_precopy_tcp_tls_psk_match
);
3095 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
3096 test_precopy_tcp_tls_psk_mismatch
);
3098 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
3099 test_precopy_tcp_tls_x509_default_host
);
3100 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
3101 test_precopy_tcp_tls_x509_override_host
);
3102 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
3103 test_precopy_tcp_tls_x509_mismatch_host
);
3104 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
3105 test_precopy_tcp_tls_x509_friendly_client
);
3106 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
3107 test_precopy_tcp_tls_x509_hostile_client
);
3108 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
3109 test_precopy_tcp_tls_x509_allow_anon_client
);
3110 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
3111 test_precopy_tcp_tls_x509_reject_anon_client
);
3112 #endif /* CONFIG_TASN1 */
3113 #endif /* CONFIG_GNUTLS */
3115 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
3117 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
3119 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
3120 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
3121 qtest_add_func("/migration/validate_uuid_src_not_set",
3122 test_validate_uuid_src_not_set
);
3123 qtest_add_func("/migration/validate_uuid_dst_not_set",
3124 test_validate_uuid_dst_not_set
);
3126 * See explanation why this test is slow on function definition
3128 if (g_test_slow()) {
3129 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
3131 qtest_add_func("/migration/multifd/tcp/plain/none",
3132 test_multifd_tcp_none
);
3134 * This test is flaky and sometimes fails in CI and otherwise:
3135 * don't run unless user opts in via environment variable.
3137 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3138 qtest_add_func("/migration/multifd/tcp/plain/cancel",
3139 test_multifd_tcp_cancel
);
3141 qtest_add_func("/migration/multifd/tcp/plain/zlib",
3142 test_multifd_tcp_zlib
);
3144 qtest_add_func("/migration/multifd/tcp/plain/zstd",
3145 test_multifd_tcp_zstd
);
3147 #ifdef CONFIG_GNUTLS
3148 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
3149 test_multifd_tcp_tls_psk_match
);
3150 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
3151 test_multifd_tcp_tls_psk_mismatch
);
3153 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
3154 test_multifd_tcp_tls_x509_default_host
);
3155 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
3156 test_multifd_tcp_tls_x509_override_host
);
3157 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
3158 test_multifd_tcp_tls_x509_mismatch_host
);
3159 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
3160 test_multifd_tcp_tls_x509_allow_anon_client
);
3161 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
3162 test_multifd_tcp_tls_x509_reject_anon_client
);
3163 #endif /* CONFIG_TASN1 */
3164 #endif /* CONFIG_GNUTLS */
3166 if (g_str_equal(arch
, "x86_64") && has_kvm
&& kvm_dirty_ring_supported()) {
3167 qtest_add_func("/migration/dirty_ring",
3168 test_precopy_unix_dirty_ring
);
3169 qtest_add_func("/migration/vcpu_dirty_limit",
3170 test_vcpu_dirty_limit
);
3175 g_assert_cmpint(ret
, ==, 0);
3180 g_test_message("unable to rmdir: path (%s): %s",
3181 tmpfs
, strerror(errno
));