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 #if defined(__linux__)
72 #include <sys/syscall.h>
76 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
77 #include <sys/eventfd.h>
78 #include <sys/ioctl.h>
79 #include "qemu/userfaultfd.h"
81 static bool ufd_version_check(void)
83 struct uffdio_api api_struct
;
86 int ufd
= uffd_open(O_CLOEXEC
);
89 g_test_message("Skipping test: userfaultfd not available");
93 api_struct
.api
= UFFD_API
;
94 api_struct
.features
= 0;
95 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
96 g_test_message("Skipping test: UFFDIO_API failed");
99 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
101 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
102 (__u64
)1 << _UFFDIO_UNREGISTER
;
103 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
104 g_test_message("Skipping test: Missing userfault feature");
112 static bool ufd_version_check(void)
114 g_test_message("Skipping test: Userfault not available (builtdtime)");
121 static char *bootpath
;
123 /* The boot file modifies memory area in [start_address, end_address)
124 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
126 #include "tests/migration/i386/a-b-bootblock.h"
127 #include "tests/migration/aarch64/a-b-kernel.h"
128 #include "tests/migration/s390x/a-b-bios.h"
130 static void bootfile_create(char *dir
)
132 const char *arch
= qtest_get_arch();
133 unsigned char *content
;
136 bootpath
= g_strdup_printf("%s/bootsect", dir
);
137 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
138 /* the assembled x86 boot sector should be exactly one sector large */
139 g_assert(sizeof(x86_bootsect
) == 512);
140 content
= x86_bootsect
;
141 len
= sizeof(x86_bootsect
);
142 } else if (g_str_equal(arch
, "s390x")) {
144 len
= sizeof(s390x_elf
);
145 } else if (strcmp(arch
, "ppc64") == 0) {
147 * sane architectures can be programmed at the boot prompt
150 } else if (strcmp(arch
, "aarch64") == 0) {
151 content
= aarch64_kernel
;
152 len
= sizeof(aarch64_kernel
);
153 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
155 g_assert_not_reached();
158 FILE *bootfile
= fopen(bootpath
, "wb");
160 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
164 static void bootfile_delete(void)
172 * Wait for some output in the serial output file,
173 * we get an 'A' followed by an endless string of 'B's
174 * but on the destination we won't have the A.
176 static void wait_for_serial(const char *side
)
178 g_autofree
char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
179 FILE *serialfile
= fopen(serialpath
, "r");
180 const char *arch
= qtest_get_arch();
181 int started
= (strcmp(side
, "src_serial") == 0 &&
182 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
185 int readvalue
= fgetc(serialfile
);
188 /* SLOF prints its banner before starting test,
189 * to ignore it, mark the start of the test with '_',
190 * ignore all characters until this marker
197 fseek(serialfile
, 0, SEEK_SET
);
214 started
= (strcmp(side
, "src_serial") == 0 &&
215 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
216 fseek(serialfile
, 0, SEEK_SET
);
221 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
222 g_assert_not_reached();
228 * It's tricky to use qemu's migration event capability with qtest,
229 * events suddenly appearing confuse the qmp()/hmp() responses.
232 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
234 QDict
*rsp_return
, *rsp_ram
;
237 rsp_return
= migrate_query_not_failed(who
);
238 if (!qdict_haskey(rsp_return
, "ram")) {
242 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
243 result
= qdict_get_try_int(rsp_ram
, property
, 0);
245 qobject_unref(rsp_return
);
249 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
254 rsp_return
= migrate_query_not_failed(who
);
255 result
= qdict_get_try_int(rsp_return
, property
, 0);
256 qobject_unref(rsp_return
);
260 static uint64_t get_migration_pass(QTestState
*who
)
262 return read_ram_property_int(who
, "dirty-sync-count");
265 static void read_blocktime(QTestState
*who
)
269 rsp_return
= migrate_query_not_failed(who
);
270 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
271 qobject_unref(rsp_return
);
274 static void wait_for_migration_pass(QTestState
*who
)
276 uint64_t initial_pass
= get_migration_pass(who
);
279 /* Wait for the 1st sync */
280 while (!got_src_stop
&& !initial_pass
) {
282 initial_pass
= get_migration_pass(who
);
287 pass
= get_migration_pass(who
);
288 } while (pass
== initial_pass
&& !got_src_stop
);
291 static void check_guests_ram(QTestState
*who
)
293 /* Our ASM test will have been incrementing one byte from each page from
294 * start_address to < end_address in order. This gives us a constraint
295 * that any page's byte should be equal or less than the previous pages
296 * byte (mod 256); and they should all be equal except for one transition
297 * at the point where we meet the incrementer. (We're running this with
298 * the guest stopped).
303 bool hit_edge
= false;
306 qtest_memread(who
, start_address
, &first_byte
, 1);
307 last_byte
= first_byte
;
309 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
310 address
+= TEST_MEM_PAGE_SIZE
)
313 qtest_memread(who
, address
, &b
, 1);
314 if (b
!= last_byte
) {
315 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
316 /* This is OK, the guest stopped at the point of
317 * incrementing the previous page but didn't get
325 fprintf(stderr
, "Memory content inconsistency at %x"
326 " first_byte = %x last_byte = %x current = %x"
328 address
, first_byte
, last_byte
, b
, hit_edge
);
334 fprintf(stderr
, "and in another %d pages", bad
- 10);
339 static void cleanup(const char *filename
)
341 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
346 static char *SocketAddress_to_str(SocketAddress
*addr
)
348 switch (addr
->type
) {
349 case SOCKET_ADDRESS_TYPE_INET
:
350 return g_strdup_printf("tcp:%s:%s",
353 case SOCKET_ADDRESS_TYPE_UNIX
:
354 return g_strdup_printf("unix:%s",
355 addr
->u
.q_unix
.path
);
356 case SOCKET_ADDRESS_TYPE_FD
:
357 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
358 case SOCKET_ADDRESS_TYPE_VSOCK
:
359 return g_strdup_printf("tcp:%s:%s",
363 return g_strdup("unknown address type");
367 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
371 SocketAddressList
*addrs
;
375 rsp
= migrate_query(who
);
376 object
= qdict_get(rsp
, parameter
);
378 iv
= qobject_input_visitor_new(object
);
379 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
382 /* we are only using a single address */
383 result
= SocketAddress_to_str(addrs
->value
);
385 qapi_free_SocketAddressList(addrs
);
390 static long long migrate_get_parameter_int(QTestState
*who
,
391 const char *parameter
)
396 rsp
= qtest_qmp_assert_success_ref(
397 who
, "{ 'execute': 'query-migrate-parameters' }");
398 result
= qdict_get_int(rsp
, parameter
);
403 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
408 result
= migrate_get_parameter_int(who
, parameter
);
409 g_assert_cmpint(result
, ==, value
);
412 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
415 qtest_qmp_assert_success(who
,
416 "{ 'execute': 'migrate-set-parameters',"
417 "'arguments': { %s: %lld } }",
419 migrate_check_parameter_int(who
, parameter
, value
);
422 static char *migrate_get_parameter_str(QTestState
*who
,
423 const char *parameter
)
428 rsp
= qtest_qmp_assert_success_ref(
429 who
, "{ 'execute': 'query-migrate-parameters' }");
430 result
= g_strdup(qdict_get_str(rsp
, parameter
));
435 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
438 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
439 g_assert_cmpstr(result
, ==, value
);
442 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
445 qtest_qmp_assert_success(who
,
446 "{ 'execute': 'migrate-set-parameters',"
447 "'arguments': { %s: %s } }",
449 migrate_check_parameter_str(who
, parameter
, value
);
452 static long long migrate_get_parameter_bool(QTestState
*who
,
453 const char *parameter
)
458 rsp
= qtest_qmp_assert_success_ref(
459 who
, "{ 'execute': 'query-migrate-parameters' }");
460 result
= qdict_get_bool(rsp
, parameter
);
465 static void migrate_check_parameter_bool(QTestState
*who
, const char *parameter
,
470 result
= migrate_get_parameter_bool(who
, parameter
);
471 g_assert_cmpint(result
, ==, value
);
474 static void migrate_set_parameter_bool(QTestState
*who
, const char *parameter
,
477 qtest_qmp_assert_success(who
,
478 "{ 'execute': 'migrate-set-parameters',"
479 "'arguments': { %s: %i } }",
481 migrate_check_parameter_bool(who
, parameter
, value
);
484 static void migrate_ensure_non_converge(QTestState
*who
)
486 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
487 migrate_set_parameter_int(who
, "max-bandwidth", 3 * 1000 * 1000);
488 migrate_set_parameter_int(who
, "downtime-limit", 1);
491 static void migrate_ensure_converge(QTestState
*who
)
493 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
494 migrate_set_parameter_int(who
, "max-bandwidth", 1 * 1000 * 1000 * 1000);
495 migrate_set_parameter_int(who
, "downtime-limit", 30 * 1000);
499 * Our goal is to ensure that we run a single full migration
500 * iteration, and also dirty memory, ensuring that at least
501 * one further iteration is required.
503 * We can't directly synchronize with the start of a migration
504 * so we have to apply some tricks monitoring memory that is
507 * Initially we set the migration bandwidth to an insanely
508 * low value, with tiny max downtime too. This basically
509 * guarantees migration will never complete.
511 * This will result in a test that is unacceptably slow though,
512 * so we can't let the entire migration pass run at this speed.
513 * Our intent is to let it run just long enough that we can
514 * prove data prior to the marker has been transferred *AND*
515 * also prove this transferred data is dirty again.
517 * Before migration starts, we write a 64-bit magic marker
518 * into a fixed location in the src VM RAM.
520 * Then watch dst memory until the marker appears. This is
521 * proof that start_address -> MAGIC_OFFSET_BASE has been
524 * Finally we go back to the source and read a byte just
525 * before the marker until we see it flip in value. This
526 * is proof that start_address -> MAGIC_OFFSET_BASE
527 * is now dirty again.
529 * IOW, we're guaranteed at least a 2nd migration pass
532 * We can now let migration run at full speed to finish
535 static void migrate_prepare_for_dirty_mem(QTestState
*from
)
538 * The guest workflow iterates from start_address to
539 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
542 * IOW, if we write to mem at a point which is NOT
543 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
544 * conflict with the migration workflow.
546 * We put in a marker here, that we'll use to determine
547 * when the data has been transferred to the dst.
549 qtest_writeq(from
, start_address
+ MAGIC_OFFSET
, MAGIC_MARKER
);
552 static void migrate_wait_for_dirty_mem(QTestState
*from
,
555 uint64_t watch_address
= start_address
+ MAGIC_OFFSET_BASE
;
556 uint64_t marker_address
= start_address
+ MAGIC_OFFSET
;
560 * Wait for the MAGIC_MARKER to get transferred, as an
561 * indicator that a migration pass has made some known
562 * amount of progress.
566 } while (qtest_readq(to
, marker_address
) != MAGIC_MARKER
);
569 * Now ensure that already transferred bytes are
570 * dirty again from the guest workload. Note the
571 * guest byte value will wrap around and by chance
572 * match the original watch_byte. This is harmless
573 * as we'll eventually see a different value if we
576 watch_byte
= qtest_readb(from
, watch_address
);
579 } while (qtest_readb(from
, watch_address
) == watch_byte
);
583 static void migrate_pause(QTestState
*who
)
585 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate-pause' }");
588 static void migrate_continue(QTestState
*who
, const char *state
)
590 qtest_qmp_assert_success(who
,
591 "{ 'execute': 'migrate-continue',"
592 " 'arguments': { 'state': %s } }",
596 static void migrate_recover(QTestState
*who
, const char *uri
)
598 qtest_qmp_assert_success(who
,
599 "{ 'execute': 'migrate-recover', "
600 " 'id': 'recover-cmd', "
601 " 'arguments': { 'uri': %s } }",
605 static void migrate_cancel(QTestState
*who
)
607 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate_cancel' }");
610 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
612 qtest_qmp_assert_success(from
, "{ 'execute': 'migrate-start-postcopy' }");
615 qtest_qmp_eventwait(from
, "STOP");
618 qtest_qmp_eventwait(to
, "RESUME");
623 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
624 * unconditionally, because it means the user would like to be verbose.
628 /* only launch the target process */
630 /* Use dirty ring if true; dirty logging otherwise */
632 const char *opts_source
;
633 const char *opts_target
;
637 * A hook that runs after the src and dst QEMUs have been
638 * created, but before the migration is started. This can
639 * be used to set migration parameters and capabilities.
641 * Returns: NULL, or a pointer to opaque state to be
642 * later passed to the TestMigrateFinishHook
644 typedef void * (*TestMigrateStartHook
)(QTestState
*from
,
648 * A hook that runs after the migration has finished,
649 * regardless of whether it succeeded or failed, but
650 * before QEMU has terminated (unless it self-terminated
651 * due to migration error)
653 * @opaque is a pointer to state previously returned
654 * by the TestMigrateStartHook if any, or NULL.
656 typedef void (*TestMigrateFinishHook
)(QTestState
*from
,
661 /* Optional: fine tune start parameters */
664 /* Required: the URI for the dst QEMU to listen on */
665 const char *listen_uri
;
668 * Optional: the URI for the src QEMU to connect to
669 * If NULL, then it will query the dst QEMU for its actual
670 * listening address and use that as the connect address.
671 * This allows for dynamically picking a free TCP port.
673 const char *connect_uri
;
675 /* Optional: callback to run at start to set migration parameters */
676 TestMigrateStartHook start_hook
;
677 /* Optional: callback to run at finish to cleanup */
678 TestMigrateFinishHook finish_hook
;
681 * Optional: normally we expect the migration process to complete.
683 * There can be a variety of reasons and stages in which failure
684 * can happen during tests.
686 * If a failure is expected to happen at time of establishing
687 * the connection, then MIG_TEST_FAIL will indicate that the dst
688 * QEMU is expected to stay running and accept future migration
691 * If a failure is expected to happen while processing the
692 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
693 * that the dst QEMU is expected to quit with non-zero exit status
696 /* This test should succeed, the default */
697 MIG_TEST_SUCCEED
= 0,
698 /* This test should fail, dest qemu should keep alive */
700 /* This test should fail, dest qemu should fail with abnormal status */
701 MIG_TEST_FAIL_DEST_QUIT_ERR
,
702 /* The QMP command for this migration should fail with an error */
707 * Optional: set number of migration passes to wait for, if live==true.
708 * If zero, then merely wait for a few MB of dirty data
710 unsigned int iterations
;
713 * Optional: whether the guest CPUs should be running during a precopy
714 * migration test. We used to always run with live but it took much
715 * longer so we reduced live tests to only the ones that have solid
716 * reason to be tested live-only. For each of the new test cases for
717 * precopy please provide justifications to use live explicitly (please
718 * refer to existing ones with live=true), or use live=off by default.
722 /* Postcopy specific fields */
724 bool postcopy_preempt
;
727 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
728 const char *uri
, MigrateStart
*args
)
730 g_autofree gchar
*arch_source
= NULL
;
731 g_autofree gchar
*arch_target
= NULL
;
732 /* options for source and target */
733 g_autofree gchar
*arch_opts
= NULL
;
734 g_autofree gchar
*cmd_source
= NULL
;
735 g_autofree gchar
*cmd_target
= NULL
;
736 const gchar
*ignore_stderr
;
737 g_autofree
char *shmem_opts
= NULL
;
738 g_autofree
char *shmem_path
= NULL
;
739 const char *kvm_opts
= NULL
;
740 const char *arch
= qtest_get_arch();
741 const char *memory_size
;
743 if (args
->use_shmem
) {
744 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
745 g_test_skip("/dev/shm is not supported");
750 got_src_stop
= false;
751 got_dst_resume
= false;
752 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
753 memory_size
= "150M";
754 arch_opts
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
755 start_address
= X86_TEST_MEM_START
;
756 end_address
= X86_TEST_MEM_END
;
757 } else if (g_str_equal(arch
, "s390x")) {
758 memory_size
= "128M";
759 arch_opts
= g_strdup_printf("-bios %s", bootpath
);
760 start_address
= S390_TEST_MEM_START
;
761 end_address
= S390_TEST_MEM_END
;
762 } else if (strcmp(arch
, "ppc64") == 0) {
763 memory_size
= "256M";
764 start_address
= PPC_TEST_MEM_START
;
765 end_address
= PPC_TEST_MEM_END
;
766 arch_source
= g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
767 "'nvramrc=hex .\" _\" begin %x %x "
768 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
769 "until'", end_address
, start_address
);
770 arch_opts
= g_strdup("-nodefaults -machine vsmt=8");
771 } else if (strcmp(arch
, "aarch64") == 0) {
772 memory_size
= "150M";
773 arch_opts
= g_strdup_printf("-machine virt,gic-version=max -cpu max "
774 "-kernel %s", bootpath
);
775 start_address
= ARM_TEST_MEM_START
;
776 end_address
= ARM_TEST_MEM_END
;
778 g_assert_not_reached();
781 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
783 ignore_stderr
= "2>/dev/null";
786 * On Windows the QEMU executable is created via CreateProcess() and
787 * IO redirection does not work, so don't bother adding IO redirection
788 * to the command line.
796 if (args
->use_shmem
) {
797 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
798 shmem_opts
= g_strdup_printf(
799 "-object memory-backend-file,id=mem0,size=%s"
800 ",mem-path=%s,share=on -numa node,memdev=mem0",
801 memory_size
, shmem_path
);
804 if (args
->use_dirty_ring
) {
805 kvm_opts
= ",dirty-ring-size=4096";
808 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg "
809 "-name source,debug-threads=on "
811 "-serial file:%s/src_serial "
813 kvm_opts
? kvm_opts
: "",
815 arch_opts
? arch_opts
: "",
816 arch_source
? arch_source
: "",
817 shmem_opts
? shmem_opts
: "",
818 args
->opts_source
? args
->opts_source
: "",
820 if (!args
->only_target
) {
821 *from
= qtest_init(cmd_source
);
822 qtest_qmp_set_event_callback(*from
,
823 migrate_watch_for_stop
,
827 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg "
828 "-name target,debug-threads=on "
830 "-serial file:%s/dest_serial "
833 kvm_opts
? kvm_opts
: "",
834 memory_size
, tmpfs
, uri
,
835 arch_opts
? arch_opts
: "",
836 arch_target
? arch_target
: "",
837 shmem_opts
? shmem_opts
: "",
838 args
->opts_target
? args
->opts_target
: "",
840 *to
= qtest_init(cmd_target
);
841 qtest_qmp_set_event_callback(*to
,
842 migrate_watch_for_resume
,
846 * Remove shmem file immediately to avoid memory leak in test failed case.
847 * It's valid because QEMU has already opened this file
849 if (args
->use_shmem
) {
856 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
858 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
863 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
865 /* Destination still running, wait for a byte to change */
867 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
869 } while (dest_byte_a
== dest_byte_b
);
871 qtest_qmp_assert_success(to
, "{ 'execute' : 'stop'}");
873 /* With it stopped, check nothing changes */
874 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
876 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
877 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
879 check_guests_ram(to
);
884 cleanup("migsocket");
885 cleanup("src_serial");
886 cleanup("dest_serial");
890 struct TestMigrateTLSPSKData
{
898 test_migrate_tls_psk_start_common(QTestState
*from
,
902 struct TestMigrateTLSPSKData
*data
=
903 g_new0(struct TestMigrateTLSPSKData
, 1);
905 data
->workdir
= g_strdup_printf("%s/tlscredspsk0", tmpfs
);
906 data
->pskfile
= g_strdup_printf("%s/%s", data
->workdir
,
907 QCRYPTO_TLS_CREDS_PSKFILE
);
908 g_mkdir_with_parents(data
->workdir
, 0700);
909 test_tls_psk_init(data
->pskfile
);
912 data
->workdiralt
= g_strdup_printf("%s/tlscredspskalt0", tmpfs
);
913 data
->pskfilealt
= g_strdup_printf("%s/%s", data
->workdiralt
,
914 QCRYPTO_TLS_CREDS_PSKFILE
);
915 g_mkdir_with_parents(data
->workdiralt
, 0700);
916 test_tls_psk_init_alt(data
->pskfilealt
);
919 qtest_qmp_assert_success(from
,
920 "{ 'execute': 'object-add',"
921 " 'arguments': { 'qom-type': 'tls-creds-psk',"
922 " 'id': 'tlscredspsk0',"
923 " 'endpoint': 'client',"
925 " 'username': 'qemu'} }",
928 qtest_qmp_assert_success(to
,
929 "{ 'execute': 'object-add',"
930 " 'arguments': { 'qom-type': 'tls-creds-psk',"
931 " 'id': 'tlscredspsk0',"
932 " 'endpoint': 'server',"
934 mismatch
? data
->workdiralt
: data
->workdir
);
936 migrate_set_parameter_str(from
, "tls-creds", "tlscredspsk0");
937 migrate_set_parameter_str(to
, "tls-creds", "tlscredspsk0");
943 test_migrate_tls_psk_start_match(QTestState
*from
,
946 return test_migrate_tls_psk_start_common(from
, to
, false);
950 test_migrate_tls_psk_start_mismatch(QTestState
*from
,
953 return test_migrate_tls_psk_start_common(from
, to
, true);
957 test_migrate_tls_psk_finish(QTestState
*from
,
961 struct TestMigrateTLSPSKData
*data
= opaque
;
963 test_tls_psk_cleanup(data
->pskfile
);
964 if (data
->pskfilealt
) {
965 test_tls_psk_cleanup(data
->pskfilealt
);
967 rmdir(data
->workdir
);
968 if (data
->workdiralt
) {
969 rmdir(data
->workdiralt
);
972 g_free(data
->workdiralt
);
973 g_free(data
->pskfilealt
);
974 g_free(data
->workdir
);
975 g_free(data
->pskfile
);
988 } TestMigrateTLSX509Data
;
995 const char *certhostname
;
996 const char *certipaddr
;
997 } TestMigrateTLSX509
;
1000 test_migrate_tls_x509_start_common(QTestState
*from
,
1002 TestMigrateTLSX509
*args
)
1004 TestMigrateTLSX509Data
*data
= g_new0(TestMigrateTLSX509Data
, 1);
1006 data
->workdir
= g_strdup_printf("%s/tlscredsx5090", tmpfs
);
1007 data
->keyfile
= g_strdup_printf("%s/key.pem", data
->workdir
);
1009 data
->cacert
= g_strdup_printf("%s/ca-cert.pem", data
->workdir
);
1010 data
->serverkey
= g_strdup_printf("%s/server-key.pem", data
->workdir
);
1011 data
->servercert
= g_strdup_printf("%s/server-cert.pem", data
->workdir
);
1012 if (args
->clientcert
) {
1013 data
->clientkey
= g_strdup_printf("%s/client-key.pem", data
->workdir
);
1014 data
->clientcert
= g_strdup_printf("%s/client-cert.pem", data
->workdir
);
1017 g_mkdir_with_parents(data
->workdir
, 0700);
1019 test_tls_init(data
->keyfile
);
1021 g_assert(link(data
->keyfile
, data
->serverkey
) == 0);
1023 g_assert(CreateHardLink(data
->serverkey
, data
->keyfile
, NULL
) != 0);
1025 if (args
->clientcert
) {
1027 g_assert(link(data
->keyfile
, data
->clientkey
) == 0);
1029 g_assert(CreateHardLink(data
->clientkey
, data
->keyfile
, NULL
) != 0);
1033 TLS_ROOT_REQ_SIMPLE(cacertreq
, data
->cacert
);
1034 if (args
->clientcert
) {
1035 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq
, cacertreq
,
1036 args
->hostileclient
?
1037 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME
:
1038 QCRYPTO_TLS_TEST_CLIENT_NAME
,
1042 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq
, cacertreq
,
1047 qtest_qmp_assert_success(from
,
1048 "{ 'execute': 'object-add',"
1049 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1050 " 'id': 'tlscredsx509client0',"
1051 " 'endpoint': 'client',"
1053 " 'sanity-check': true,"
1054 " 'verify-peer': true} }",
1056 migrate_set_parameter_str(from
, "tls-creds", "tlscredsx509client0");
1057 if (args
->certhostname
) {
1058 migrate_set_parameter_str(from
, "tls-hostname", args
->certhostname
);
1061 qtest_qmp_assert_success(to
,
1062 "{ 'execute': 'object-add',"
1063 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1064 " 'id': 'tlscredsx509server0',"
1065 " 'endpoint': 'server',"
1067 " 'sanity-check': true,"
1068 " 'verify-peer': %i} }",
1069 data
->workdir
, args
->verifyclient
);
1070 migrate_set_parameter_str(to
, "tls-creds", "tlscredsx509server0");
1072 if (args
->authzclient
) {
1073 qtest_qmp_assert_success(to
,
1074 "{ 'execute': 'object-add',"
1075 " 'arguments': { 'qom-type': 'authz-simple',"
1076 " 'id': 'tlsauthz0',"
1077 " 'identity': %s} }",
1078 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME
);
1079 migrate_set_parameter_str(to
, "tls-authz", "tlsauthz0");
1086 * The normal case: match server's cert hostname against
1087 * whatever host we were telling QEMU to connect to (if any)
1090 test_migrate_tls_x509_start_default_host(QTestState
*from
,
1093 TestMigrateTLSX509 args
= {
1094 .verifyclient
= true,
1096 .certipaddr
= "127.0.0.1"
1098 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1102 * The unusual case: the server's cert is different from
1103 * the address we're telling QEMU to connect to (if any),
1104 * so we must give QEMU an explicit hostname to validate
1107 test_migrate_tls_x509_start_override_host(QTestState
*from
,
1110 TestMigrateTLSX509 args
= {
1111 .verifyclient
= true,
1113 .certhostname
= "qemu.org",
1115 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1119 * The unusual case: the server's cert is different from
1120 * the address we're telling QEMU to connect to, and so we
1121 * expect the client to reject the server
1124 test_migrate_tls_x509_start_mismatch_host(QTestState
*from
,
1127 TestMigrateTLSX509 args
= {
1128 .verifyclient
= true,
1130 .certipaddr
= "10.0.0.1",
1132 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1136 test_migrate_tls_x509_start_friendly_client(QTestState
*from
,
1139 TestMigrateTLSX509 args
= {
1140 .verifyclient
= true,
1142 .authzclient
= true,
1143 .certipaddr
= "127.0.0.1",
1145 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1149 test_migrate_tls_x509_start_hostile_client(QTestState
*from
,
1152 TestMigrateTLSX509 args
= {
1153 .verifyclient
= true,
1155 .hostileclient
= true,
1156 .authzclient
= true,
1157 .certipaddr
= "127.0.0.1",
1159 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1163 * The case with no client certificate presented,
1164 * and no server verification
1167 test_migrate_tls_x509_start_allow_anon_client(QTestState
*from
,
1170 TestMigrateTLSX509 args
= {
1171 .certipaddr
= "127.0.0.1",
1173 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1177 * The case with no client certificate presented,
1178 * and server verification rejecting
1181 test_migrate_tls_x509_start_reject_anon_client(QTestState
*from
,
1184 TestMigrateTLSX509 args
= {
1185 .verifyclient
= true,
1186 .certipaddr
= "127.0.0.1",
1188 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1192 test_migrate_tls_x509_finish(QTestState
*from
,
1196 TestMigrateTLSX509Data
*data
= opaque
;
1198 test_tls_cleanup(data
->keyfile
);
1199 g_free(data
->keyfile
);
1201 unlink(data
->cacert
);
1202 g_free(data
->cacert
);
1203 unlink(data
->servercert
);
1204 g_free(data
->servercert
);
1205 unlink(data
->serverkey
);
1206 g_free(data
->serverkey
);
1208 if (data
->clientcert
) {
1209 unlink(data
->clientcert
);
1210 g_free(data
->clientcert
);
1212 if (data
->clientkey
) {
1213 unlink(data
->clientkey
);
1214 g_free(data
->clientkey
);
1217 rmdir(data
->workdir
);
1218 g_free(data
->workdir
);
1222 #endif /* CONFIG_TASN1 */
1223 #endif /* CONFIG_GNUTLS */
1226 test_migrate_compress_start(QTestState
*from
,
1229 migrate_set_parameter_int(from
, "compress-level", 1);
1230 migrate_set_parameter_int(from
, "compress-threads", 4);
1231 migrate_set_parameter_bool(from
, "compress-wait-thread", true);
1232 migrate_set_parameter_int(to
, "decompress-threads", 4);
1234 migrate_set_capability(from
, "compress", true);
1235 migrate_set_capability(to
, "compress", true);
1241 test_migrate_compress_nowait_start(QTestState
*from
,
1244 migrate_set_parameter_int(from
, "compress-level", 9);
1245 migrate_set_parameter_int(from
, "compress-threads", 1);
1246 migrate_set_parameter_bool(from
, "compress-wait-thread", false);
1247 migrate_set_parameter_int(to
, "decompress-threads", 1);
1249 migrate_set_capability(from
, "compress", true);
1250 migrate_set_capability(to
, "compress", true);
1255 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
1256 QTestState
**to_ptr
,
1257 MigrateCommon
*args
)
1259 QTestState
*from
, *to
;
1261 if (test_migrate_start(&from
, &to
, "defer", &args
->start
)) {
1265 if (args
->start_hook
) {
1266 args
->postcopy_data
= args
->start_hook(from
, to
);
1269 migrate_set_capability(from
, "postcopy-ram", true);
1270 migrate_set_capability(to
, "postcopy-ram", true);
1271 migrate_set_capability(to
, "postcopy-blocktime", true);
1273 if (args
->postcopy_preempt
) {
1274 migrate_set_capability(from
, "postcopy-preempt", true);
1275 migrate_set_capability(to
, "postcopy-preempt", true);
1278 migrate_ensure_non_converge(from
);
1280 migrate_prepare_for_dirty_mem(from
);
1281 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1282 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1284 /* Wait for the first serial output from the source */
1285 wait_for_serial("src_serial");
1287 g_autofree
char *uri
= migrate_get_socket_address(to
, "socket-address");
1288 migrate_qmp(from
, uri
, "{}");
1290 migrate_wait_for_dirty_mem(from
, to
);
1298 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
,
1299 MigrateCommon
*args
)
1301 wait_for_migration_complete(from
);
1303 /* Make sure we get at least one "B" on destination */
1304 wait_for_serial("dest_serial");
1306 if (uffd_feature_thread_id
) {
1310 if (args
->finish_hook
) {
1311 args
->finish_hook(from
, to
, args
->postcopy_data
);
1312 args
->postcopy_data
= NULL
;
1315 test_migrate_end(from
, to
, true);
1318 static void test_postcopy_common(MigrateCommon
*args
)
1320 QTestState
*from
, *to
;
1322 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1325 migrate_postcopy_start(from
, to
);
1326 migrate_postcopy_complete(from
, to
, args
);
1329 static void test_postcopy(void)
1331 MigrateCommon args
= { };
1333 test_postcopy_common(&args
);
1336 static void test_postcopy_compress(void)
1338 MigrateCommon args
= {
1339 .start_hook
= test_migrate_compress_start
1342 test_postcopy_common(&args
);
1345 static void test_postcopy_preempt(void)
1347 MigrateCommon args
= {
1348 .postcopy_preempt
= true,
1351 test_postcopy_common(&args
);
1354 #ifdef CONFIG_GNUTLS
1355 static void test_postcopy_tls_psk(void)
1357 MigrateCommon args
= {
1358 .start_hook
= test_migrate_tls_psk_start_match
,
1359 .finish_hook
= test_migrate_tls_psk_finish
,
1362 test_postcopy_common(&args
);
1365 static void test_postcopy_preempt_tls_psk(void)
1367 MigrateCommon args
= {
1368 .postcopy_preempt
= true,
1369 .start_hook
= test_migrate_tls_psk_start_match
,
1370 .finish_hook
= test_migrate_tls_psk_finish
,
1373 test_postcopy_common(&args
);
1377 static void test_postcopy_recovery_common(MigrateCommon
*args
)
1379 QTestState
*from
, *to
;
1380 g_autofree
char *uri
= NULL
;
1382 /* Always hide errors for postcopy recover tests since they're expected */
1383 args
->start
.hide_stderr
= true;
1385 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1389 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1390 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
1392 /* Now we start the postcopy */
1393 migrate_postcopy_start(from
, to
);
1396 * Wait until postcopy is really started; we can only run the
1397 * migrate-pause command during a postcopy
1399 wait_for_migration_status(from
, "postcopy-active", NULL
);
1402 * Manually stop the postcopy migration. This emulates a network
1403 * failure with the migration socket
1405 migrate_pause(from
);
1408 * Wait for destination side to reach postcopy-paused state. The
1409 * migrate-recover command can only succeed if destination machine
1410 * is in the paused state
1412 wait_for_migration_status(to
, "postcopy-paused",
1413 (const char * []) { "failed", "active",
1414 "completed", NULL
});
1417 * Create a new socket to emulate a new channel that is different
1418 * from the broken migration channel; tell the destination to
1419 * listen to the new port
1421 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
1422 migrate_recover(to
, uri
);
1425 * Try to rebuild the migration channel using the resume flag and
1426 * the newly created channel
1428 wait_for_migration_status(from
, "postcopy-paused",
1429 (const char * []) { "failed", "active",
1430 "completed", NULL
});
1431 migrate_qmp(from
, uri
, "{'resume': true}");
1433 /* Restore the postcopy bandwidth to unlimited */
1434 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
1436 migrate_postcopy_complete(from
, to
, args
);
1439 static void test_postcopy_recovery(void)
1441 MigrateCommon args
= { };
1443 test_postcopy_recovery_common(&args
);
1446 static void test_postcopy_recovery_compress(void)
1448 MigrateCommon args
= {
1449 .start_hook
= test_migrate_compress_start
1452 test_postcopy_recovery_common(&args
);
1455 #ifdef CONFIG_GNUTLS
1456 static void test_postcopy_recovery_tls_psk(void)
1458 MigrateCommon args
= {
1459 .start_hook
= test_migrate_tls_psk_start_match
,
1460 .finish_hook
= test_migrate_tls_psk_finish
,
1463 test_postcopy_recovery_common(&args
);
1467 static void test_postcopy_preempt_recovery(void)
1469 MigrateCommon args
= {
1470 .postcopy_preempt
= true,
1473 test_postcopy_recovery_common(&args
);
1476 #ifdef CONFIG_GNUTLS
1477 /* This contains preempt+recovery+tls test altogether */
1478 static void test_postcopy_preempt_all(void)
1480 MigrateCommon args
= {
1481 .postcopy_preempt
= true,
1482 .start_hook
= test_migrate_tls_psk_start_match
,
1483 .finish_hook
= test_migrate_tls_psk_finish
,
1486 test_postcopy_recovery_common(&args
);
1491 static void test_baddest(void)
1493 MigrateStart args
= {
1496 QTestState
*from
, *to
;
1498 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1501 migrate_qmp(from
, "tcp:127.0.0.1:0", "{}");
1502 wait_for_migration_fail(from
, false);
1503 test_migrate_end(from
, to
, false);
1507 static void test_analyze_script(void)
1509 MigrateStart args
= {
1510 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
1512 QTestState
*from
, *to
;
1513 g_autofree
char *uri
= NULL
;
1514 g_autofree
char *file
= NULL
;
1516 const char *python
= g_getenv("PYTHON");
1519 g_test_skip("PYTHON variable not set");
1524 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1529 * Setting these two capabilities causes the "configuration"
1530 * vmstate to include subsections for them. The script needs to
1531 * parse those subsections properly.
1533 migrate_set_capability(from
, "validate-uuid", true);
1534 migrate_set_capability(from
, "x-ignore-shared", true);
1536 file
= g_strdup_printf("%s/migfile", tmpfs
);
1537 uri
= g_strdup_printf("exec:cat > %s", file
);
1539 migrate_ensure_converge(from
);
1540 migrate_qmp(from
, uri
, "{}");
1541 wait_for_migration_complete(from
);
1546 open("/dev/null", O_WRONLY
);
1547 execl(python
, python
, ANALYZE_SCRIPT
, "-f", file
, NULL
);
1548 g_assert_not_reached();
1551 g_assert(waitpid(pid
, &wstatus
, 0) == pid
);
1552 if (WIFEXITED(wstatus
) && WEXITSTATUS(wstatus
) != 0) {
1553 g_test_message("Failed to analyze the migration stream");
1556 test_migrate_end(from
, to
, false);
1561 static void test_precopy_common(MigrateCommon
*args
)
1563 QTestState
*from
, *to
;
1564 void *data_hook
= NULL
;
1565 g_autofree
char *connect_uri
= NULL
;
1567 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1571 if (args
->start_hook
) {
1572 data_hook
= args
->start_hook(from
, to
);
1575 /* Wait for the first serial output from the source */
1576 if (args
->result
== MIG_TEST_SUCCEED
) {
1577 wait_for_serial("src_serial");
1581 migrate_ensure_non_converge(from
);
1582 migrate_prepare_for_dirty_mem(from
);
1585 * Testing non-live migration, we allow it to run at
1586 * full speed to ensure short test case duration.
1587 * For tests expected to fail, we don't need to
1590 if (args
->result
== MIG_TEST_SUCCEED
) {
1591 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1592 if (!got_src_stop
) {
1593 qtest_qmp_eventwait(from
, "STOP");
1595 migrate_ensure_converge(from
);
1599 if (!args
->connect_uri
) {
1600 connect_uri
= migrate_get_socket_address(to
, "socket-address");
1602 connect_uri
= g_strdup(args
->connect_uri
);
1605 if (args
->result
== MIG_TEST_QMP_ERROR
) {
1606 migrate_qmp_fail(from
, connect_uri
, "{}");
1610 migrate_qmp(from
, connect_uri
, "{}");
1612 if (args
->result
!= MIG_TEST_SUCCEED
) {
1613 bool allow_active
= args
->result
== MIG_TEST_FAIL
;
1614 wait_for_migration_fail(from
, allow_active
);
1616 if (args
->result
== MIG_TEST_FAIL_DEST_QUIT_ERR
) {
1617 qtest_set_expected_status(to
, EXIT_FAILURE
);
1622 * For initial iteration(s) we must do a full pass,
1623 * but for the final iteration, we need only wait
1624 * for some dirty mem before switching to converge
1626 while (args
->iterations
> 1) {
1627 wait_for_migration_pass(from
);
1630 migrate_wait_for_dirty_mem(from
, to
);
1632 migrate_ensure_converge(from
);
1635 * We do this first, as it has a timeout to stop us
1636 * hanging forever if migration didn't converge
1638 wait_for_migration_complete(from
);
1640 if (!got_src_stop
) {
1641 qtest_qmp_eventwait(from
, "STOP");
1644 wait_for_migration_complete(from
);
1646 * Must wait for dst to finish reading all incoming
1647 * data on the socket before issuing 'cont' otherwise
1650 wait_for_migration_complete(to
);
1652 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1655 if (!got_dst_resume
) {
1656 qtest_qmp_eventwait(to
, "RESUME");
1659 wait_for_serial("dest_serial");
1663 if (args
->finish_hook
) {
1664 args
->finish_hook(from
, to
, data_hook
);
1667 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1670 static void test_precopy_unix_plain(void)
1672 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1673 MigrateCommon args
= {
1677 * The simplest use case of precopy, covering smoke tests of
1678 * get-dirty-log dirty tracking.
1683 test_precopy_common(&args
);
1687 static void test_precopy_unix_dirty_ring(void)
1689 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1690 MigrateCommon args
= {
1692 .use_dirty_ring
= true,
1697 * Besides the precopy/unix basic test, cover dirty ring interface
1698 * rather than get-dirty-log.
1703 test_precopy_common(&args
);
1706 #ifdef CONFIG_GNUTLS
1707 static void test_precopy_unix_tls_psk(void)
1709 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1710 MigrateCommon args
= {
1713 .start_hook
= test_migrate_tls_psk_start_match
,
1714 .finish_hook
= test_migrate_tls_psk_finish
,
1717 test_precopy_common(&args
);
1721 static void test_precopy_unix_tls_x509_default_host(void)
1723 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1724 MigrateCommon args
= {
1726 .hide_stderr
= true,
1730 .start_hook
= test_migrate_tls_x509_start_default_host
,
1731 .finish_hook
= test_migrate_tls_x509_finish
,
1732 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1735 test_precopy_common(&args
);
1738 static void test_precopy_unix_tls_x509_override_host(void)
1740 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1741 MigrateCommon args
= {
1744 .start_hook
= test_migrate_tls_x509_start_override_host
,
1745 .finish_hook
= test_migrate_tls_x509_finish
,
1748 test_precopy_common(&args
);
1750 #endif /* CONFIG_TASN1 */
1751 #endif /* CONFIG_GNUTLS */
1754 /* Currently upset on aarch64 TCG */
1755 static void test_ignore_shared(void)
1757 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1758 QTestState
*from
, *to
;
1760 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
1764 migrate_ensure_non_converge(from
);
1765 migrate_prepare_for_dirty_mem(from
);
1767 migrate_set_capability(from
, "x-ignore-shared", true);
1768 migrate_set_capability(to
, "x-ignore-shared", true);
1770 /* Wait for the first serial output from the source */
1771 wait_for_serial("src_serial");
1773 migrate_qmp(from
, uri
, "{}");
1775 migrate_wait_for_dirty_mem(from
, to
);
1777 if (!got_src_stop
) {
1778 qtest_qmp_eventwait(from
, "STOP");
1781 qtest_qmp_eventwait(to
, "RESUME");
1783 wait_for_serial("dest_serial");
1784 wait_for_migration_complete(from
);
1786 /* Check whether shared RAM has been really skipped */
1787 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1789 test_migrate_end(from
, to
, true);
1794 test_migrate_xbzrle_start(QTestState
*from
,
1797 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1799 migrate_set_capability(from
, "xbzrle", true);
1800 migrate_set_capability(to
, "xbzrle", true);
1805 static void test_precopy_unix_xbzrle(void)
1807 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1808 MigrateCommon args
= {
1811 .start_hook
= test_migrate_xbzrle_start
,
1814 * XBZRLE needs pages to be modified when doing the 2nd+ round
1815 * iteration to have real data pushed to the stream.
1820 test_precopy_common(&args
);
1823 static void test_precopy_unix_compress(void)
1825 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1826 MigrateCommon args
= {
1829 .start_hook
= test_migrate_compress_start
,
1831 * Test that no invalid thread state is left over from
1832 * the previous iteration.
1836 * We make sure the compressor can always work well even if guest
1837 * memory is changing. See commit 34ab9e9743 where we used to fix
1838 * a bug when only trigger-able with guest memory changing.
1843 test_precopy_common(&args
);
1846 static void test_precopy_unix_compress_nowait(void)
1848 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1849 MigrateCommon args
= {
1852 .start_hook
= test_migrate_compress_nowait_start
,
1854 * Test that no invalid thread state is left over from
1855 * the previous iteration.
1858 /* Same reason for the wait version of precopy compress test */
1862 test_precopy_common(&args
);
1865 static void test_precopy_tcp_plain(void)
1867 MigrateCommon args
= {
1868 .listen_uri
= "tcp:127.0.0.1:0",
1871 test_precopy_common(&args
);
1874 static void *test_migrate_switchover_ack_start(QTestState
*from
, QTestState
*to
)
1877 migrate_set_capability(from
, "return-path", true);
1878 migrate_set_capability(to
, "return-path", true);
1880 migrate_set_capability(from
, "switchover-ack", true);
1881 migrate_set_capability(to
, "switchover-ack", true);
1886 static void test_precopy_tcp_switchover_ack(void)
1888 MigrateCommon args
= {
1889 .listen_uri
= "tcp:127.0.0.1:0",
1890 .start_hook
= test_migrate_switchover_ack_start
,
1892 * Source VM must be running in order to consider the switchover ACK
1893 * when deciding to do switchover or not.
1898 test_precopy_common(&args
);
1901 #ifdef CONFIG_GNUTLS
1902 static void test_precopy_tcp_tls_psk_match(void)
1904 MigrateCommon args
= {
1905 .listen_uri
= "tcp:127.0.0.1:0",
1906 .start_hook
= test_migrate_tls_psk_start_match
,
1907 .finish_hook
= test_migrate_tls_psk_finish
,
1910 test_precopy_common(&args
);
1913 static void test_precopy_tcp_tls_psk_mismatch(void)
1915 MigrateCommon args
= {
1917 .hide_stderr
= true,
1919 .listen_uri
= "tcp:127.0.0.1:0",
1920 .start_hook
= test_migrate_tls_psk_start_mismatch
,
1921 .finish_hook
= test_migrate_tls_psk_finish
,
1922 .result
= MIG_TEST_FAIL
,
1925 test_precopy_common(&args
);
1929 static void test_precopy_tcp_tls_x509_default_host(void)
1931 MigrateCommon args
= {
1932 .listen_uri
= "tcp:127.0.0.1:0",
1933 .start_hook
= test_migrate_tls_x509_start_default_host
,
1934 .finish_hook
= test_migrate_tls_x509_finish
,
1937 test_precopy_common(&args
);
1940 static void test_precopy_tcp_tls_x509_override_host(void)
1942 MigrateCommon args
= {
1943 .listen_uri
= "tcp:127.0.0.1:0",
1944 .start_hook
= test_migrate_tls_x509_start_override_host
,
1945 .finish_hook
= test_migrate_tls_x509_finish
,
1948 test_precopy_common(&args
);
1951 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1953 MigrateCommon args
= {
1955 .hide_stderr
= true,
1957 .listen_uri
= "tcp:127.0.0.1:0",
1958 .start_hook
= test_migrate_tls_x509_start_mismatch_host
,
1959 .finish_hook
= test_migrate_tls_x509_finish
,
1960 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1963 test_precopy_common(&args
);
1966 static void test_precopy_tcp_tls_x509_friendly_client(void)
1968 MigrateCommon args
= {
1969 .listen_uri
= "tcp:127.0.0.1:0",
1970 .start_hook
= test_migrate_tls_x509_start_friendly_client
,
1971 .finish_hook
= test_migrate_tls_x509_finish
,
1974 test_precopy_common(&args
);
1977 static void test_precopy_tcp_tls_x509_hostile_client(void)
1979 MigrateCommon args
= {
1981 .hide_stderr
= true,
1983 .listen_uri
= "tcp:127.0.0.1:0",
1984 .start_hook
= test_migrate_tls_x509_start_hostile_client
,
1985 .finish_hook
= test_migrate_tls_x509_finish
,
1986 .result
= MIG_TEST_FAIL
,
1989 test_precopy_common(&args
);
1992 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1994 MigrateCommon args
= {
1995 .listen_uri
= "tcp:127.0.0.1:0",
1996 .start_hook
= test_migrate_tls_x509_start_allow_anon_client
,
1997 .finish_hook
= test_migrate_tls_x509_finish
,
2000 test_precopy_common(&args
);
2003 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2005 MigrateCommon args
= {
2007 .hide_stderr
= true,
2009 .listen_uri
= "tcp:127.0.0.1:0",
2010 .start_hook
= test_migrate_tls_x509_start_reject_anon_client
,
2011 .finish_hook
= test_migrate_tls_x509_finish
,
2012 .result
= MIG_TEST_FAIL
,
2015 test_precopy_common(&args
);
2017 #endif /* CONFIG_TASN1 */
2018 #endif /* CONFIG_GNUTLS */
2021 static void *test_migrate_fd_start_hook(QTestState
*from
,
2027 /* Create two connected sockets for migration */
2028 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
2029 g_assert_cmpint(ret
, ==, 0);
2031 /* Send the 1st socket to the target */
2032 qtest_qmp_fds_assert_success(to
, &pair
[0], 1,
2033 "{ 'execute': 'getfd',"
2034 " 'arguments': { 'fdname': 'fd-mig' }}");
2037 /* Start incoming migration from the 1st socket */
2038 migrate_incoming_qmp(to
, "fd:fd-mig", "{}");
2040 /* Send the 2nd socket to the target */
2041 qtest_qmp_fds_assert_success(from
, &pair
[1], 1,
2042 "{ 'execute': 'getfd',"
2043 " 'arguments': { 'fdname': 'fd-mig' }}");
2049 static void test_migrate_fd_finish_hook(QTestState
*from
,
2054 const char *error_desc
;
2056 /* Test closing fds */
2057 /* We assume, that QEMU removes named fd from its list,
2058 * so this should fail */
2059 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
2060 " 'arguments': { 'fdname': 'fd-mig' }}");
2061 g_assert_true(qdict_haskey(rsp
, "error"));
2062 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2063 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2066 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
2067 " 'arguments': { 'fdname': 'fd-mig' }}");
2068 g_assert_true(qdict_haskey(rsp
, "error"));
2069 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2070 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2074 static void test_migrate_fd_proto(void)
2076 MigrateCommon args
= {
2077 .listen_uri
= "defer",
2078 .connect_uri
= "fd:fd-mig",
2079 .start_hook
= test_migrate_fd_start_hook
,
2080 .finish_hook
= test_migrate_fd_finish_hook
2082 test_precopy_common(&args
);
2086 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
2088 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2089 QTestState
*from
, *to
;
2091 if (test_migrate_start(&from
, &to
, uri
, args
)) {
2096 * UUID validation is at the begin of migration. So, the main process of
2097 * migration is not interesting for us here. Thus, set huge downtime for
2098 * very fast migration.
2100 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
2101 migrate_set_capability(from
, "validate-uuid", true);
2103 /* Wait for the first serial output from the source */
2104 wait_for_serial("src_serial");
2106 migrate_qmp(from
, uri
, "{}");
2109 qtest_set_expected_status(to
, EXIT_FAILURE
);
2110 wait_for_migration_fail(from
, true);
2112 wait_for_migration_complete(from
);
2115 test_migrate_end(from
, to
, false);
2118 static void test_validate_uuid(void)
2120 MigrateStart args
= {
2121 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2122 .opts_target
= "-uuid 11111111-1111-1111-1111-111111111111",
2125 do_test_validate_uuid(&args
, false);
2128 static void test_validate_uuid_error(void)
2130 MigrateStart args
= {
2131 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2132 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2133 .hide_stderr
= true,
2136 do_test_validate_uuid(&args
, true);
2139 static void test_validate_uuid_src_not_set(void)
2141 MigrateStart args
= {
2142 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2143 .hide_stderr
= true,
2146 do_test_validate_uuid(&args
, false);
2149 static void test_validate_uuid_dst_not_set(void)
2151 MigrateStart args
= {
2152 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2153 .hide_stderr
= true,
2156 do_test_validate_uuid(&args
, false);
2160 * The way auto_converge works, we need to do too many passes to
2161 * run this test. Auto_converge logic is only run once every
2162 * three iterations, so:
2164 * - 3 iterations without auto_converge enabled
2165 * - 3 iterations with pct = 5
2166 * - 3 iterations with pct = 30
2167 * - 3 iterations with pct = 55
2168 * - 3 iterations with pct = 80
2169 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2171 * To make things even worse, we need to run the initial stage at
2172 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2174 static void test_migrate_auto_converge(void)
2176 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2177 MigrateStart args
= {};
2178 QTestState
*from
, *to
;
2182 * We want the test to be stable and as fast as possible.
2183 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2184 * so we need to decrease a bandwidth.
2186 const int64_t init_pct
= 5, inc_pct
= 25, max_pct
= 95;
2188 if (test_migrate_start(&from
, &to
, uri
, &args
)) {
2192 migrate_set_capability(from
, "auto-converge", true);
2193 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
2194 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
2195 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
2198 * Set the initial parameters so that the migration could not converge
2199 * without throttling.
2201 migrate_ensure_non_converge(from
);
2203 /* To check remaining size after precopy */
2204 migrate_set_capability(from
, "pause-before-switchover", true);
2206 /* Wait for the first serial output from the source */
2207 wait_for_serial("src_serial");
2209 migrate_qmp(from
, uri
, "{}");
2211 /* Wait for throttling begins */
2214 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2215 if (percentage
!= 0) {
2219 g_assert_false(got_src_stop
);
2221 /* The first percentage of throttling should be at least init_pct */
2222 g_assert_cmpint(percentage
, >=, init_pct
);
2223 /* Now, when we tested that throttling works, let it converge */
2224 migrate_ensure_converge(from
);
2227 * Wait for pre-switchover status to check last throttle percentage
2228 * and remaining. These values will be zeroed later
2230 wait_for_migration_status(from
, "pre-switchover", NULL
);
2232 /* The final percentage of throttling shouldn't be greater than max_pct */
2233 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2234 g_assert_cmpint(percentage
, <=, max_pct
);
2235 migrate_continue(from
, "pre-switchover");
2237 qtest_qmp_eventwait(to
, "RESUME");
2239 wait_for_serial("dest_serial");
2240 wait_for_migration_complete(from
);
2242 test_migrate_end(from
, to
, true);
2246 test_migrate_precopy_tcp_multifd_start_common(QTestState
*from
,
2250 migrate_set_parameter_int(from
, "multifd-channels", 16);
2251 migrate_set_parameter_int(to
, "multifd-channels", 16);
2253 migrate_set_parameter_str(from
, "multifd-compression", method
);
2254 migrate_set_parameter_str(to
, "multifd-compression", method
);
2256 migrate_set_capability(from
, "multifd", true);
2257 migrate_set_capability(to
, "multifd", true);
2259 /* Start incoming migration from the 1st socket */
2260 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2266 test_migrate_precopy_tcp_multifd_start(QTestState
*from
,
2269 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2273 test_migrate_precopy_tcp_multifd_zlib_start(QTestState
*from
,
2276 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zlib");
2281 test_migrate_precopy_tcp_multifd_zstd_start(QTestState
*from
,
2284 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zstd");
2286 #endif /* CONFIG_ZSTD */
2288 static void test_multifd_tcp_none(void)
2290 MigrateCommon args
= {
2291 .listen_uri
= "defer",
2292 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2294 * Multifd is more complicated than most of the features, it
2295 * directly takes guest page buffers when sending, make sure
2296 * everything will work alright even if guest page is changing.
2300 test_precopy_common(&args
);
2303 static void test_multifd_tcp_zlib(void)
2305 MigrateCommon args
= {
2306 .listen_uri
= "defer",
2307 .start_hook
= test_migrate_precopy_tcp_multifd_zlib_start
,
2309 test_precopy_common(&args
);
2313 static void test_multifd_tcp_zstd(void)
2315 MigrateCommon args
= {
2316 .listen_uri
= "defer",
2317 .start_hook
= test_migrate_precopy_tcp_multifd_zstd_start
,
2319 test_precopy_common(&args
);
2323 #ifdef CONFIG_GNUTLS
2325 test_migrate_multifd_tcp_tls_psk_start_match(QTestState
*from
,
2328 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2329 return test_migrate_tls_psk_start_match(from
, to
);
2333 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState
*from
,
2336 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2337 return test_migrate_tls_psk_start_mismatch(from
, to
);
2342 test_migrate_multifd_tls_x509_start_default_host(QTestState
*from
,
2345 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2346 return test_migrate_tls_x509_start_default_host(from
, to
);
2350 test_migrate_multifd_tls_x509_start_override_host(QTestState
*from
,
2353 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2354 return test_migrate_tls_x509_start_override_host(from
, to
);
2358 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState
*from
,
2361 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2362 return test_migrate_tls_x509_start_mismatch_host(from
, to
);
2366 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState
*from
,
2369 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2370 return test_migrate_tls_x509_start_allow_anon_client(from
, to
);
2374 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState
*from
,
2377 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2378 return test_migrate_tls_x509_start_reject_anon_client(from
, to
);
2380 #endif /* CONFIG_TASN1 */
2382 static void test_multifd_tcp_tls_psk_match(void)
2384 MigrateCommon args
= {
2385 .listen_uri
= "defer",
2386 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_match
,
2387 .finish_hook
= test_migrate_tls_psk_finish
,
2389 test_precopy_common(&args
);
2392 static void test_multifd_tcp_tls_psk_mismatch(void)
2394 MigrateCommon args
= {
2396 .hide_stderr
= true,
2398 .listen_uri
= "defer",
2399 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_mismatch
,
2400 .finish_hook
= test_migrate_tls_psk_finish
,
2401 .result
= MIG_TEST_FAIL
,
2403 test_precopy_common(&args
);
2407 static void test_multifd_tcp_tls_x509_default_host(void)
2409 MigrateCommon args
= {
2410 .listen_uri
= "defer",
2411 .start_hook
= test_migrate_multifd_tls_x509_start_default_host
,
2412 .finish_hook
= test_migrate_tls_x509_finish
,
2414 test_precopy_common(&args
);
2417 static void test_multifd_tcp_tls_x509_override_host(void)
2419 MigrateCommon args
= {
2420 .listen_uri
= "defer",
2421 .start_hook
= test_migrate_multifd_tls_x509_start_override_host
,
2422 .finish_hook
= test_migrate_tls_x509_finish
,
2424 test_precopy_common(&args
);
2427 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2430 * This has different behaviour to the non-multifd case.
2432 * In non-multifd case when client aborts due to mismatched
2433 * cert host, the server has already started trying to load
2434 * migration state, and so it exits with I/O failure.
2436 * In multifd case when client aborts due to mismatched
2437 * cert host, the server is still waiting for the other
2438 * multifd connections to arrive so hasn't started trying
2439 * to load migration state, and thus just aborts the migration
2442 MigrateCommon args
= {
2444 .hide_stderr
= true,
2446 .listen_uri
= "defer",
2447 .start_hook
= test_migrate_multifd_tls_x509_start_mismatch_host
,
2448 .finish_hook
= test_migrate_tls_x509_finish
,
2449 .result
= MIG_TEST_FAIL
,
2451 test_precopy_common(&args
);
2454 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2456 MigrateCommon args
= {
2457 .listen_uri
= "defer",
2458 .start_hook
= test_migrate_multifd_tls_x509_start_allow_anon_client
,
2459 .finish_hook
= test_migrate_tls_x509_finish
,
2461 test_precopy_common(&args
);
2464 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2466 MigrateCommon args
= {
2468 .hide_stderr
= true,
2470 .listen_uri
= "defer",
2471 .start_hook
= test_migrate_multifd_tls_x509_start_reject_anon_client
,
2472 .finish_hook
= test_migrate_tls_x509_finish
,
2473 .result
= MIG_TEST_FAIL
,
2475 test_precopy_common(&args
);
2477 #endif /* CONFIG_TASN1 */
2478 #endif /* CONFIG_GNUTLS */
2486 * launch another target
2489 * And see that it works
2491 static void test_multifd_tcp_cancel(void)
2493 MigrateStart args
= {
2494 .hide_stderr
= true,
2496 QTestState
*from
, *to
, *to2
;
2497 g_autofree
char *uri
= NULL
;
2499 if (test_migrate_start(&from
, &to
, "defer", &args
)) {
2503 migrate_ensure_non_converge(from
);
2504 migrate_prepare_for_dirty_mem(from
);
2506 migrate_set_parameter_int(from
, "multifd-channels", 16);
2507 migrate_set_parameter_int(to
, "multifd-channels", 16);
2509 migrate_set_capability(from
, "multifd", true);
2510 migrate_set_capability(to
, "multifd", true);
2512 /* Start incoming migration from the 1st socket */
2513 migrate_incoming_qmp(to
, "tcp:127.0.0.1:0", "{}");
2515 /* Wait for the first serial output from the source */
2516 wait_for_serial("src_serial");
2518 uri
= migrate_get_socket_address(to
, "socket-address");
2520 migrate_qmp(from
, uri
, "{}");
2522 migrate_wait_for_dirty_mem(from
, to
);
2524 migrate_cancel(from
);
2526 /* Make sure QEMU process "to" exited */
2527 qtest_set_expected_status(to
, EXIT_FAILURE
);
2528 qtest_wait_qemu(to
);
2530 args
= (MigrateStart
){
2531 .only_target
= true,
2534 if (test_migrate_start(&from
, &to2
, "defer", &args
)) {
2538 migrate_set_parameter_int(to2
, "multifd-channels", 16);
2540 migrate_set_capability(to2
, "multifd", true);
2542 /* Start incoming migration from the 1st socket */
2543 migrate_incoming_qmp(to2
, "tcp:127.0.0.1:0", "{}");
2546 uri
= migrate_get_socket_address(to2
, "socket-address");
2548 wait_for_migration_status(from
, "cancelled", NULL
);
2550 migrate_ensure_non_converge(from
);
2552 migrate_qmp(from
, uri
, "{}");
2554 migrate_wait_for_dirty_mem(from
, to2
);
2556 migrate_ensure_converge(from
);
2558 if (!got_src_stop
) {
2559 qtest_qmp_eventwait(from
, "STOP");
2561 qtest_qmp_eventwait(to2
, "RESUME");
2563 wait_for_serial("dest_serial");
2564 wait_for_migration_complete(from
);
2565 test_migrate_end(from
, to2
, true);
2568 static void calc_dirty_rate(QTestState
*who
, uint64_t calc_time
)
2570 qtest_qmp_assert_success(who
,
2571 "{ 'execute': 'calc-dirty-rate',"
2573 "'calc-time': %" PRIu64
","
2574 "'mode': 'dirty-ring' }}",
2578 static QDict
*query_dirty_rate(QTestState
*who
)
2580 return qtest_qmp_assert_success_ref(who
,
2581 "{ 'execute': 'query-dirty-rate' }");
2584 static void dirtylimit_set_all(QTestState
*who
, uint64_t dirtyrate
)
2586 qtest_qmp_assert_success(who
,
2587 "{ 'execute': 'set-vcpu-dirty-limit',"
2589 "'dirty-rate': %" PRIu64
" } }",
2593 static void cancel_vcpu_dirty_limit(QTestState
*who
)
2595 qtest_qmp_assert_success(who
,
2596 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2599 static QDict
*query_vcpu_dirty_limit(QTestState
*who
)
2603 rsp
= qtest_qmp(who
, "{ 'execute': 'query-vcpu-dirty-limit' }");
2604 g_assert(!qdict_haskey(rsp
, "error"));
2605 g_assert(qdict_haskey(rsp
, "return"));
2610 static bool calc_dirtyrate_ready(QTestState
*who
)
2615 rsp_return
= query_dirty_rate(who
);
2616 g_assert(rsp_return
);
2618 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2621 return g_strcmp0(status
, "measuring");
2624 static void wait_for_calc_dirtyrate_complete(QTestState
*who
,
2627 int max_try_count
= 10000;
2628 usleep(time_s
* 1000000);
2630 while (!calc_dirtyrate_ready(who
) && max_try_count
--) {
2635 * Set the timeout with 10 s(max_try_count * 1000us),
2636 * if dirtyrate measurement not complete, fail test.
2638 g_assert_cmpint(max_try_count
, !=, 0);
2641 static int64_t get_dirty_rate(QTestState
*who
)
2646 const QListEntry
*entry
;
2650 rsp_return
= query_dirty_rate(who
);
2651 g_assert(rsp_return
);
2653 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2655 g_assert_cmpstr(status
, ==, "measured");
2657 rates
= qdict_get_qlist(rsp_return
, "vcpu-dirty-rate");
2658 g_assert(rates
&& !qlist_empty(rates
));
2660 entry
= qlist_first(rates
);
2663 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2666 dirtyrate
= qdict_get_try_int(rate
, "dirty-rate", -1);
2668 qobject_unref(rsp_return
);
2672 static int64_t get_limit_rate(QTestState
*who
)
2676 const QListEntry
*entry
;
2680 rsp_return
= query_vcpu_dirty_limit(who
);
2681 g_assert(rsp_return
);
2683 rates
= qdict_get_qlist(rsp_return
, "return");
2684 g_assert(rates
&& !qlist_empty(rates
));
2686 entry
= qlist_first(rates
);
2689 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2692 dirtyrate
= qdict_get_try_int(rate
, "limit-rate", -1);
2694 qobject_unref(rsp_return
);
2698 static QTestState
*dirtylimit_start_vm(void)
2700 QTestState
*vm
= NULL
;
2702 cmd
= g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2703 "-name dirtylimit-test,debug-threads=on "
2705 "-serial file:%s/vm_serial "
2706 "-drive file=%s,format=raw ",
2709 vm
= qtest_init(cmd
);
2713 static void dirtylimit_stop_vm(QTestState
*vm
)
2716 cleanup("vm_serial");
2719 static void test_vcpu_dirty_limit(void)
2722 int64_t origin_rate
;
2725 int max_try_count
= 20;
2728 /* Start vm for vcpu dirtylimit test */
2729 vm
= dirtylimit_start_vm();
2731 /* Wait for the first serial output from the vm*/
2732 wait_for_serial("vm_serial");
2734 /* Do dirtyrate measurement with calc time equals 1s */
2735 calc_dirty_rate(vm
, 1);
2737 /* Sleep calc time and wait for calc dirtyrate complete */
2738 wait_for_calc_dirtyrate_complete(vm
, 1);
2740 /* Query original dirty page rate */
2741 origin_rate
= get_dirty_rate(vm
);
2743 /* VM booted from bootsect should dirty memory steadily */
2744 assert(origin_rate
!= 0);
2746 /* Setup quota dirty page rate at half of origin */
2747 quota_rate
= origin_rate
/ 2;
2749 /* Set dirtylimit */
2750 dirtylimit_set_all(vm
, quota_rate
);
2753 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2756 g_assert_cmpint(quota_rate
, ==, get_limit_rate(vm
));
2758 /* Sleep a bit to check if it take effect */
2762 * Check if dirtylimit take effect realistically, set the
2763 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2764 * doesn't take effect, fail test.
2766 while (--max_try_count
) {
2767 calc_dirty_rate(vm
, 1);
2768 wait_for_calc_dirtyrate_complete(vm
, 1);
2769 rate
= get_dirty_rate(vm
);
2772 * Assume hitting if current rate is less
2773 * than quota rate (within accepting error)
2775 if (rate
< (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2781 g_assert_cmpint(hit
, ==, 1);
2786 /* Check if dirtylimit cancellation take effect */
2787 cancel_vcpu_dirty_limit(vm
);
2788 while (--max_try_count
) {
2789 calc_dirty_rate(vm
, 1);
2790 wait_for_calc_dirtyrate_complete(vm
, 1);
2791 rate
= get_dirty_rate(vm
);
2794 * Assume dirtylimit be canceled if current rate is
2795 * greater than quota rate (within accepting error)
2797 if (rate
> (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2803 g_assert_cmpint(hit
, ==, 1);
2804 dirtylimit_stop_vm(vm
);
2807 static bool kvm_dirty_ring_supported(void)
2809 #if defined(__linux__) && defined(HOST_X86_64)
2810 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
2816 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
2819 /* We test with 4096 slots */
2830 int main(int argc
, char **argv
)
2832 bool has_kvm
, has_tcg
;
2835 g_autoptr(GError
) err
= NULL
;
2838 g_test_init(&argc
, &argv
, NULL
);
2840 has_kvm
= qtest_has_accel("kvm");
2841 has_tcg
= qtest_has_accel("tcg");
2843 if (!has_tcg
&& !has_kvm
) {
2844 g_test_skip("No KVM or TCG accelerator available");
2848 has_uffd
= ufd_version_check();
2849 arch
= qtest_get_arch();
2852 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2853 * is touchy due to race conditions on dirty bits (especially on PPC for
2856 if (g_str_equal(arch
, "ppc64") &&
2857 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
2858 g_test_message("Skipping test: kvm_hv not available");
2859 return g_test_run();
2863 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2864 * there until the problems are resolved
2866 if (g_str_equal(arch
, "s390x") && !has_kvm
) {
2867 g_test_message("Skipping test: s390x host with KVM is required");
2868 return g_test_run();
2871 tmpfs
= g_dir_make_tmp("migration-test-XXXXXX", &err
);
2873 g_test_message("Can't create temporary directory in %s: %s",
2874 g_get_tmp_dir(), err
->message
);
2877 bootfile_create(tmpfs
);
2879 module_call_init(MODULE_INIT_QOM
);
2882 qtest_add_func("/migration/postcopy/plain", test_postcopy
);
2883 qtest_add_func("/migration/postcopy/recovery/plain",
2884 test_postcopy_recovery
);
2885 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt
);
2886 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2887 test_postcopy_preempt_recovery
);
2888 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2889 qtest_add_func("/migration/postcopy/compress/plain",
2890 test_postcopy_compress
);
2891 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2892 test_postcopy_recovery_compress
);
2896 qtest_add_func("/migration/bad_dest", test_baddest
);
2898 qtest_add_func("/migration/analyze-script", test_analyze_script
);
2900 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain
);
2901 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle
);
2903 * Compression fails from time to time.
2904 * Put test here but don't enable it until everything is fixed.
2906 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2907 qtest_add_func("/migration/precopy/unix/compress/wait",
2908 test_precopy_unix_compress
);
2909 qtest_add_func("/migration/precopy/unix/compress/nowait",
2910 test_precopy_unix_compress_nowait
);
2912 #ifdef CONFIG_GNUTLS
2913 qtest_add_func("/migration/precopy/unix/tls/psk",
2914 test_precopy_unix_tls_psk
);
2918 * NOTE: psk test is enough for postcopy, as other types of TLS
2919 * channels are tested under precopy. Here what we want to test is the
2920 * general postcopy path that has TLS channel enabled.
2922 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk
);
2923 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2924 test_postcopy_recovery_tls_psk
);
2925 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2926 test_postcopy_preempt_tls_psk
);
2927 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2928 test_postcopy_preempt_all
);
2931 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2932 test_precopy_unix_tls_x509_default_host
);
2933 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2934 test_precopy_unix_tls_x509_override_host
);
2935 #endif /* CONFIG_TASN1 */
2936 #endif /* CONFIG_GNUTLS */
2938 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain
);
2940 qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
2941 test_precopy_tcp_switchover_ack
);
2943 #ifdef CONFIG_GNUTLS
2944 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2945 test_precopy_tcp_tls_psk_match
);
2946 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2947 test_precopy_tcp_tls_psk_mismatch
);
2949 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2950 test_precopy_tcp_tls_x509_default_host
);
2951 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2952 test_precopy_tcp_tls_x509_override_host
);
2953 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2954 test_precopy_tcp_tls_x509_mismatch_host
);
2955 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2956 test_precopy_tcp_tls_x509_friendly_client
);
2957 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2958 test_precopy_tcp_tls_x509_hostile_client
);
2959 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2960 test_precopy_tcp_tls_x509_allow_anon_client
);
2961 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2962 test_precopy_tcp_tls_x509_reject_anon_client
);
2963 #endif /* CONFIG_TASN1 */
2964 #endif /* CONFIG_GNUTLS */
2966 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2968 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
2970 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
2971 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
2972 qtest_add_func("/migration/validate_uuid_src_not_set",
2973 test_validate_uuid_src_not_set
);
2974 qtest_add_func("/migration/validate_uuid_dst_not_set",
2975 test_validate_uuid_dst_not_set
);
2977 * See explanation why this test is slow on function definition
2979 if (g_test_slow()) {
2980 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
2982 qtest_add_func("/migration/multifd/tcp/plain/none",
2983 test_multifd_tcp_none
);
2985 * This test is flaky and sometimes fails in CI and otherwise:
2986 * don't run unless user opts in via environment variable.
2988 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2989 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2990 test_multifd_tcp_cancel
);
2992 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2993 test_multifd_tcp_zlib
);
2995 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2996 test_multifd_tcp_zstd
);
2998 #ifdef CONFIG_GNUTLS
2999 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
3000 test_multifd_tcp_tls_psk_match
);
3001 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
3002 test_multifd_tcp_tls_psk_mismatch
);
3004 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
3005 test_multifd_tcp_tls_x509_default_host
);
3006 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
3007 test_multifd_tcp_tls_x509_override_host
);
3008 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
3009 test_multifd_tcp_tls_x509_mismatch_host
);
3010 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
3011 test_multifd_tcp_tls_x509_allow_anon_client
);
3012 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
3013 test_multifd_tcp_tls_x509_reject_anon_client
);
3014 #endif /* CONFIG_TASN1 */
3015 #endif /* CONFIG_GNUTLS */
3017 if (g_str_equal(arch
, "x86_64") && has_kvm
&& kvm_dirty_ring_supported()) {
3018 qtest_add_func("/migration/dirty_ring",
3019 test_precopy_unix_dirty_ring
);
3020 qtest_add_func("/migration/vcpu_dirty_limit",
3021 test_vcpu_dirty_limit
);
3026 g_assert_cmpint(ret
, ==, 0);
3031 g_test_message("unable to rmdir: path (%s): %s",
3032 tmpfs
, strerror(errno
));