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 #if defined(__linux__)
70 #include <sys/syscall.h>
74 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
75 #include <sys/eventfd.h>
76 #include <sys/ioctl.h>
77 #include "qemu/userfaultfd.h"
79 static bool ufd_version_check(void)
81 struct uffdio_api api_struct
;
84 int ufd
= uffd_open(O_CLOEXEC
);
87 g_test_message("Skipping test: userfaultfd not available");
91 api_struct
.api
= UFFD_API
;
92 api_struct
.features
= 0;
93 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
94 g_test_message("Skipping test: UFFDIO_API failed");
97 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
99 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
100 (__u64
)1 << _UFFDIO_UNREGISTER
;
101 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
102 g_test_message("Skipping test: Missing userfault feature");
110 static bool ufd_version_check(void)
112 g_test_message("Skipping test: Userfault not available (builtdtime)");
119 static char *bootpath
;
121 /* The boot file modifies memory area in [start_address, end_address)
122 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
124 #include "tests/migration/i386/a-b-bootblock.h"
125 #include "tests/migration/aarch64/a-b-kernel.h"
126 #include "tests/migration/s390x/a-b-bios.h"
128 static void bootfile_create(char *dir
)
130 const char *arch
= qtest_get_arch();
131 unsigned char *content
;
134 bootpath
= g_strdup_printf("%s/bootsect", dir
);
135 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
136 /* the assembled x86 boot sector should be exactly one sector large */
137 g_assert(sizeof(x86_bootsect
) == 512);
138 content
= x86_bootsect
;
139 len
= sizeof(x86_bootsect
);
140 } else if (g_str_equal(arch
, "s390x")) {
142 len
= sizeof(s390x_elf
);
143 } else if (strcmp(arch
, "ppc64") == 0) {
145 * sane architectures can be programmed at the boot prompt
148 } else if (strcmp(arch
, "aarch64") == 0) {
149 content
= aarch64_kernel
;
150 len
= sizeof(aarch64_kernel
);
151 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
153 g_assert_not_reached();
156 FILE *bootfile
= fopen(bootpath
, "wb");
158 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
162 static void bootfile_delete(void)
170 * Wait for some output in the serial output file,
171 * we get an 'A' followed by an endless string of 'B's
172 * but on the destination we won't have the A.
174 static void wait_for_serial(const char *side
)
176 g_autofree
char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
177 FILE *serialfile
= fopen(serialpath
, "r");
178 const char *arch
= qtest_get_arch();
179 int started
= (strcmp(side
, "src_serial") == 0 &&
180 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
183 int readvalue
= fgetc(serialfile
);
186 /* SLOF prints its banner before starting test,
187 * to ignore it, mark the start of the test with '_',
188 * ignore all characters until this marker
195 fseek(serialfile
, 0, SEEK_SET
);
212 started
= (strcmp(side
, "src_serial") == 0 &&
213 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
214 fseek(serialfile
, 0, SEEK_SET
);
219 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
220 g_assert_not_reached();
226 * It's tricky to use qemu's migration event capability with qtest,
227 * events suddenly appearing confuse the qmp()/hmp() responses.
230 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
232 QDict
*rsp_return
, *rsp_ram
;
235 rsp_return
= migrate_query_not_failed(who
);
236 if (!qdict_haskey(rsp_return
, "ram")) {
240 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
241 result
= qdict_get_try_int(rsp_ram
, property
, 0);
243 qobject_unref(rsp_return
);
247 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
252 rsp_return
= migrate_query_not_failed(who
);
253 result
= qdict_get_try_int(rsp_return
, property
, 0);
254 qobject_unref(rsp_return
);
258 static uint64_t get_migration_pass(QTestState
*who
)
260 return read_ram_property_int(who
, "dirty-sync-count");
263 static void read_blocktime(QTestState
*who
)
267 rsp_return
= migrate_query_not_failed(who
);
268 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
269 qobject_unref(rsp_return
);
272 static void wait_for_migration_pass(QTestState
*who
)
274 uint64_t initial_pass
= get_migration_pass(who
);
277 /* Wait for the 1st sync */
278 while (!got_src_stop
&& !initial_pass
) {
280 initial_pass
= get_migration_pass(who
);
285 pass
= get_migration_pass(who
);
286 } while (pass
== initial_pass
&& !got_src_stop
);
289 static void check_guests_ram(QTestState
*who
)
291 /* Our ASM test will have been incrementing one byte from each page from
292 * start_address to < end_address in order. This gives us a constraint
293 * that any page's byte should be equal or less than the previous pages
294 * byte (mod 256); and they should all be equal except for one transition
295 * at the point where we meet the incrementer. (We're running this with
296 * the guest stopped).
301 bool hit_edge
= false;
304 qtest_memread(who
, start_address
, &first_byte
, 1);
305 last_byte
= first_byte
;
307 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
308 address
+= TEST_MEM_PAGE_SIZE
)
311 qtest_memread(who
, address
, &b
, 1);
312 if (b
!= last_byte
) {
313 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
314 /* This is OK, the guest stopped at the point of
315 * incrementing the previous page but didn't get
323 fprintf(stderr
, "Memory content inconsistency at %x"
324 " first_byte = %x last_byte = %x current = %x"
326 address
, first_byte
, last_byte
, b
, hit_edge
);
332 fprintf(stderr
, "and in another %d pages", bad
- 10);
337 static void cleanup(const char *filename
)
339 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
344 static char *SocketAddress_to_str(SocketAddress
*addr
)
346 switch (addr
->type
) {
347 case SOCKET_ADDRESS_TYPE_INET
:
348 return g_strdup_printf("tcp:%s:%s",
351 case SOCKET_ADDRESS_TYPE_UNIX
:
352 return g_strdup_printf("unix:%s",
353 addr
->u
.q_unix
.path
);
354 case SOCKET_ADDRESS_TYPE_FD
:
355 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
356 case SOCKET_ADDRESS_TYPE_VSOCK
:
357 return g_strdup_printf("tcp:%s:%s",
361 return g_strdup("unknown address type");
365 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
369 SocketAddressList
*addrs
;
373 rsp
= migrate_query(who
);
374 object
= qdict_get(rsp
, parameter
);
376 iv
= qobject_input_visitor_new(object
);
377 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
380 /* we are only using a single address */
381 result
= SocketAddress_to_str(addrs
->value
);
383 qapi_free_SocketAddressList(addrs
);
388 static long long migrate_get_parameter_int(QTestState
*who
,
389 const char *parameter
)
394 rsp
= qtest_qmp_assert_success_ref(
395 who
, "{ 'execute': 'query-migrate-parameters' }");
396 result
= qdict_get_int(rsp
, parameter
);
401 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
406 result
= migrate_get_parameter_int(who
, parameter
);
407 g_assert_cmpint(result
, ==, value
);
410 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
413 qtest_qmp_assert_success(who
,
414 "{ 'execute': 'migrate-set-parameters',"
415 "'arguments': { %s: %lld } }",
417 migrate_check_parameter_int(who
, parameter
, value
);
420 static char *migrate_get_parameter_str(QTestState
*who
,
421 const char *parameter
)
426 rsp
= qtest_qmp_assert_success_ref(
427 who
, "{ 'execute': 'query-migrate-parameters' }");
428 result
= g_strdup(qdict_get_str(rsp
, parameter
));
433 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
436 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
437 g_assert_cmpstr(result
, ==, value
);
440 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
443 qtest_qmp_assert_success(who
,
444 "{ 'execute': 'migrate-set-parameters',"
445 "'arguments': { %s: %s } }",
447 migrate_check_parameter_str(who
, parameter
, value
);
450 static long long migrate_get_parameter_bool(QTestState
*who
,
451 const char *parameter
)
456 rsp
= qtest_qmp_assert_success_ref(
457 who
, "{ 'execute': 'query-migrate-parameters' }");
458 result
= qdict_get_bool(rsp
, parameter
);
463 static void migrate_check_parameter_bool(QTestState
*who
, const char *parameter
,
468 result
= migrate_get_parameter_bool(who
, parameter
);
469 g_assert_cmpint(result
, ==, value
);
472 static void migrate_set_parameter_bool(QTestState
*who
, const char *parameter
,
475 qtest_qmp_assert_success(who
,
476 "{ 'execute': 'migrate-set-parameters',"
477 "'arguments': { %s: %i } }",
479 migrate_check_parameter_bool(who
, parameter
, value
);
482 static void migrate_ensure_non_converge(QTestState
*who
)
484 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
485 migrate_set_parameter_int(who
, "max-bandwidth", 3 * 1000 * 1000);
486 migrate_set_parameter_int(who
, "downtime-limit", 1);
489 static void migrate_ensure_converge(QTestState
*who
)
491 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
492 migrate_set_parameter_int(who
, "max-bandwidth", 1 * 1000 * 1000 * 1000);
493 migrate_set_parameter_int(who
, "downtime-limit", 30 * 1000);
497 * Our goal is to ensure that we run a single full migration
498 * iteration, and also dirty memory, ensuring that at least
499 * one further iteration is required.
501 * We can't directly synchronize with the start of a migration
502 * so we have to apply some tricks monitoring memory that is
505 * Initially we set the migration bandwidth to an insanely
506 * low value, with tiny max downtime too. This basically
507 * guarantees migration will never complete.
509 * This will result in a test that is unacceptably slow though,
510 * so we can't let the entire migration pass run at this speed.
511 * Our intent is to let it run just long enough that we can
512 * prove data prior to the marker has been transferred *AND*
513 * also prove this transferred data is dirty again.
515 * Before migration starts, we write a 64-bit magic marker
516 * into a fixed location in the src VM RAM.
518 * Then watch dst memory until the marker appears. This is
519 * proof that start_address -> MAGIC_OFFSET_BASE has been
522 * Finally we go back to the source and read a byte just
523 * before the marker until we see it flip in value. This
524 * is proof that start_address -> MAGIC_OFFSET_BASE
525 * is now dirty again.
527 * IOW, we're guaranteed at least a 2nd migration pass
530 * We can now let migration run at full speed to finish
533 static void migrate_prepare_for_dirty_mem(QTestState
*from
)
536 * The guest workflow iterates from start_address to
537 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
540 * IOW, if we write to mem at a point which is NOT
541 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
542 * conflict with the migration workflow.
544 * We put in a marker here, that we'll use to determine
545 * when the data has been transferred to the dst.
547 qtest_writeq(from
, start_address
+ MAGIC_OFFSET
, MAGIC_MARKER
);
550 static void migrate_wait_for_dirty_mem(QTestState
*from
,
553 uint64_t watch_address
= start_address
+ MAGIC_OFFSET_BASE
;
554 uint64_t marker_address
= start_address
+ MAGIC_OFFSET
;
558 * Wait for the MAGIC_MARKER to get transferred, as an
559 * indicator that a migration pass has made some known
560 * amount of progress.
564 } while (qtest_readq(to
, marker_address
) != MAGIC_MARKER
);
567 * Now ensure that already transferred bytes are
568 * dirty again from the guest workload. Note the
569 * guest byte value will wrap around and by chance
570 * match the original watch_byte. This is harmless
571 * as we'll eventually see a different value if we
574 watch_byte
= qtest_readb(from
, watch_address
);
577 } while (qtest_readb(from
, watch_address
) == watch_byte
);
581 static void migrate_pause(QTestState
*who
)
583 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate-pause' }");
586 static void migrate_continue(QTestState
*who
, const char *state
)
588 qtest_qmp_assert_success(who
,
589 "{ 'execute': 'migrate-continue',"
590 " 'arguments': { 'state': %s } }",
594 static void migrate_recover(QTestState
*who
, const char *uri
)
596 qtest_qmp_assert_success(who
,
597 "{ 'execute': 'migrate-recover', "
598 " 'id': 'recover-cmd', "
599 " 'arguments': { 'uri': %s } }",
603 static void migrate_cancel(QTestState
*who
)
605 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate_cancel' }");
608 static void migrate_set_capability(QTestState
*who
, const char *capability
,
611 qtest_qmp_assert_success(who
,
612 "{ 'execute': 'migrate-set-capabilities',"
614 "'capabilities': [ { "
615 "'capability': %s, 'state': %i } ] } }",
619 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
621 qtest_qmp_assert_success(from
, "{ 'execute': 'migrate-start-postcopy' }");
624 qtest_qmp_eventwait(from
, "STOP");
627 qtest_qmp_eventwait(to
, "RESUME");
632 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
633 * unconditionally, because it means the user would like to be verbose.
637 /* only launch the target process */
639 /* Use dirty ring if true; dirty logging otherwise */
641 const char *opts_source
;
642 const char *opts_target
;
646 * A hook that runs after the src and dst QEMUs have been
647 * created, but before the migration is started. This can
648 * be used to set migration parameters and capabilities.
650 * Returns: NULL, or a pointer to opaque state to be
651 * later passed to the TestMigrateFinishHook
653 typedef void * (*TestMigrateStartHook
)(QTestState
*from
,
657 * A hook that runs after the migration has finished,
658 * regardless of whether it succeeded or failed, but
659 * before QEMU has terminated (unless it self-terminated
660 * due to migration error)
662 * @opaque is a pointer to state previously returned
663 * by the TestMigrateStartHook if any, or NULL.
665 typedef void (*TestMigrateFinishHook
)(QTestState
*from
,
670 /* Optional: fine tune start parameters */
673 /* Required: the URI for the dst QEMU to listen on */
674 const char *listen_uri
;
677 * Optional: the URI for the src QEMU to connect to
678 * If NULL, then it will query the dst QEMU for its actual
679 * listening address and use that as the connect address.
680 * This allows for dynamically picking a free TCP port.
682 const char *connect_uri
;
684 /* Optional: callback to run at start to set migration parameters */
685 TestMigrateStartHook start_hook
;
686 /* Optional: callback to run at finish to cleanup */
687 TestMigrateFinishHook finish_hook
;
690 * Optional: normally we expect the migration process to complete.
692 * There can be a variety of reasons and stages in which failure
693 * can happen during tests.
695 * If a failure is expected to happen at time of establishing
696 * the connection, then MIG_TEST_FAIL will indicate that the dst
697 * QEMU is expected to stay running and accept future migration
700 * If a failure is expected to happen while processing the
701 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
702 * that the dst QEMU is expected to quit with non-zero exit status
705 /* This test should succeed, the default */
706 MIG_TEST_SUCCEED
= 0,
707 /* This test should fail, dest qemu should keep alive */
709 /* This test should fail, dest qemu should fail with abnormal status */
710 MIG_TEST_FAIL_DEST_QUIT_ERR
,
714 * Optional: set number of migration passes to wait for, if live==true.
715 * If zero, then merely wait for a few MB of dirty data
717 unsigned int iterations
;
720 * Optional: whether the guest CPUs should be running during a precopy
721 * migration test. We used to always run with live but it took much
722 * longer so we reduced live tests to only the ones that have solid
723 * reason to be tested live-only. For each of the new test cases for
724 * precopy please provide justifications to use live explicitly (please
725 * refer to existing ones with live=true), or use live=off by default.
729 /* Postcopy specific fields */
731 bool postcopy_preempt
;
734 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
735 const char *uri
, MigrateStart
*args
)
737 g_autofree gchar
*arch_source
= NULL
;
738 g_autofree gchar
*arch_target
= NULL
;
739 /* options for source and target */
740 g_autofree gchar
*arch_opts
= NULL
;
741 g_autofree gchar
*cmd_source
= NULL
;
742 g_autofree gchar
*cmd_target
= NULL
;
743 const gchar
*ignore_stderr
;
744 g_autofree
char *shmem_opts
= NULL
;
745 g_autofree
char *shmem_path
= NULL
;
746 const char *kvm_opts
= NULL
;
747 const char *arch
= qtest_get_arch();
748 const char *memory_size
;
750 if (args
->use_shmem
) {
751 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
752 g_test_skip("/dev/shm is not supported");
757 got_src_stop
= false;
758 got_dst_resume
= false;
759 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
760 memory_size
= "150M";
761 arch_opts
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
762 start_address
= X86_TEST_MEM_START
;
763 end_address
= X86_TEST_MEM_END
;
764 } else if (g_str_equal(arch
, "s390x")) {
765 memory_size
= "128M";
766 arch_opts
= g_strdup_printf("-bios %s", bootpath
);
767 start_address
= S390_TEST_MEM_START
;
768 end_address
= S390_TEST_MEM_END
;
769 } else if (strcmp(arch
, "ppc64") == 0) {
770 memory_size
= "256M";
771 start_address
= PPC_TEST_MEM_START
;
772 end_address
= PPC_TEST_MEM_END
;
773 arch_source
= g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
774 "'nvramrc=hex .\" _\" begin %x %x "
775 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
776 "until'", end_address
, start_address
);
777 arch_opts
= g_strdup("-nodefaults -machine vsmt=8");
778 } else if (strcmp(arch
, "aarch64") == 0) {
779 memory_size
= "150M";
780 arch_opts
= g_strdup_printf("-machine virt,gic-version=max -cpu max "
781 "-kernel %s", bootpath
);
782 start_address
= ARM_TEST_MEM_START
;
783 end_address
= ARM_TEST_MEM_END
;
785 g_assert_not_reached();
788 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
790 ignore_stderr
= "2>/dev/null";
793 * On Windows the QEMU executable is created via CreateProcess() and
794 * IO redirection does not work, so don't bother adding IO redirection
795 * to the command line.
803 if (args
->use_shmem
) {
804 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
805 shmem_opts
= g_strdup_printf(
806 "-object memory-backend-file,id=mem0,size=%s"
807 ",mem-path=%s,share=on -numa node,memdev=mem0",
808 memory_size
, shmem_path
);
811 if (args
->use_dirty_ring
) {
812 kvm_opts
= ",dirty-ring-size=4096";
815 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg "
816 "-name source,debug-threads=on "
818 "-serial file:%s/src_serial "
820 kvm_opts
? kvm_opts
: "",
822 arch_opts
? arch_opts
: "",
823 arch_source
? arch_source
: "",
824 shmem_opts
? shmem_opts
: "",
825 args
->opts_source
? args
->opts_source
: "",
827 if (!args
->only_target
) {
828 *from
= qtest_init(cmd_source
);
829 qtest_qmp_set_event_callback(*from
,
830 migrate_watch_for_stop
,
834 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg "
835 "-name target,debug-threads=on "
837 "-serial file:%s/dest_serial "
840 kvm_opts
? kvm_opts
: "",
841 memory_size
, tmpfs
, uri
,
842 arch_opts
? arch_opts
: "",
843 arch_target
? arch_target
: "",
844 shmem_opts
? shmem_opts
: "",
845 args
->opts_target
? args
->opts_target
: "",
847 *to
= qtest_init(cmd_target
);
848 qtest_qmp_set_event_callback(*to
,
849 migrate_watch_for_resume
,
853 * Remove shmem file immediately to avoid memory leak in test failed case.
854 * It's valid because QEMU has already opened this file
856 if (args
->use_shmem
) {
863 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
865 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
870 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
872 /* Destination still running, wait for a byte to change */
874 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
876 } while (dest_byte_a
== dest_byte_b
);
878 qtest_qmp_assert_success(to
, "{ 'execute' : 'stop'}");
880 /* With it stopped, check nothing changes */
881 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
883 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
884 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
886 check_guests_ram(to
);
891 cleanup("migsocket");
892 cleanup("src_serial");
893 cleanup("dest_serial");
897 struct TestMigrateTLSPSKData
{
905 test_migrate_tls_psk_start_common(QTestState
*from
,
909 struct TestMigrateTLSPSKData
*data
=
910 g_new0(struct TestMigrateTLSPSKData
, 1);
912 data
->workdir
= g_strdup_printf("%s/tlscredspsk0", tmpfs
);
913 data
->pskfile
= g_strdup_printf("%s/%s", data
->workdir
,
914 QCRYPTO_TLS_CREDS_PSKFILE
);
915 g_mkdir_with_parents(data
->workdir
, 0700);
916 test_tls_psk_init(data
->pskfile
);
919 data
->workdiralt
= g_strdup_printf("%s/tlscredspskalt0", tmpfs
);
920 data
->pskfilealt
= g_strdup_printf("%s/%s", data
->workdiralt
,
921 QCRYPTO_TLS_CREDS_PSKFILE
);
922 g_mkdir_with_parents(data
->workdiralt
, 0700);
923 test_tls_psk_init_alt(data
->pskfilealt
);
926 qtest_qmp_assert_success(from
,
927 "{ 'execute': 'object-add',"
928 " 'arguments': { 'qom-type': 'tls-creds-psk',"
929 " 'id': 'tlscredspsk0',"
930 " 'endpoint': 'client',"
932 " 'username': 'qemu'} }",
935 qtest_qmp_assert_success(to
,
936 "{ 'execute': 'object-add',"
937 " 'arguments': { 'qom-type': 'tls-creds-psk',"
938 " 'id': 'tlscredspsk0',"
939 " 'endpoint': 'server',"
941 mismatch
? data
->workdiralt
: data
->workdir
);
943 migrate_set_parameter_str(from
, "tls-creds", "tlscredspsk0");
944 migrate_set_parameter_str(to
, "tls-creds", "tlscredspsk0");
950 test_migrate_tls_psk_start_match(QTestState
*from
,
953 return test_migrate_tls_psk_start_common(from
, to
, false);
957 test_migrate_tls_psk_start_mismatch(QTestState
*from
,
960 return test_migrate_tls_psk_start_common(from
, to
, true);
964 test_migrate_tls_psk_finish(QTestState
*from
,
968 struct TestMigrateTLSPSKData
*data
= opaque
;
970 test_tls_psk_cleanup(data
->pskfile
);
971 if (data
->pskfilealt
) {
972 test_tls_psk_cleanup(data
->pskfilealt
);
974 rmdir(data
->workdir
);
975 if (data
->workdiralt
) {
976 rmdir(data
->workdiralt
);
979 g_free(data
->workdiralt
);
980 g_free(data
->pskfilealt
);
981 g_free(data
->workdir
);
982 g_free(data
->pskfile
);
995 } TestMigrateTLSX509Data
;
1002 const char *certhostname
;
1003 const char *certipaddr
;
1004 } TestMigrateTLSX509
;
1007 test_migrate_tls_x509_start_common(QTestState
*from
,
1009 TestMigrateTLSX509
*args
)
1011 TestMigrateTLSX509Data
*data
= g_new0(TestMigrateTLSX509Data
, 1);
1013 data
->workdir
= g_strdup_printf("%s/tlscredsx5090", tmpfs
);
1014 data
->keyfile
= g_strdup_printf("%s/key.pem", data
->workdir
);
1016 data
->cacert
= g_strdup_printf("%s/ca-cert.pem", data
->workdir
);
1017 data
->serverkey
= g_strdup_printf("%s/server-key.pem", data
->workdir
);
1018 data
->servercert
= g_strdup_printf("%s/server-cert.pem", data
->workdir
);
1019 if (args
->clientcert
) {
1020 data
->clientkey
= g_strdup_printf("%s/client-key.pem", data
->workdir
);
1021 data
->clientcert
= g_strdup_printf("%s/client-cert.pem", data
->workdir
);
1024 g_mkdir_with_parents(data
->workdir
, 0700);
1026 test_tls_init(data
->keyfile
);
1028 g_assert(link(data
->keyfile
, data
->serverkey
) == 0);
1030 g_assert(CreateHardLink(data
->serverkey
, data
->keyfile
, NULL
) != 0);
1032 if (args
->clientcert
) {
1034 g_assert(link(data
->keyfile
, data
->clientkey
) == 0);
1036 g_assert(CreateHardLink(data
->clientkey
, data
->keyfile
, NULL
) != 0);
1040 TLS_ROOT_REQ_SIMPLE(cacertreq
, data
->cacert
);
1041 if (args
->clientcert
) {
1042 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq
, cacertreq
,
1043 args
->hostileclient
?
1044 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME
:
1045 QCRYPTO_TLS_TEST_CLIENT_NAME
,
1049 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq
, cacertreq
,
1054 qtest_qmp_assert_success(from
,
1055 "{ 'execute': 'object-add',"
1056 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1057 " 'id': 'tlscredsx509client0',"
1058 " 'endpoint': 'client',"
1060 " 'sanity-check': true,"
1061 " 'verify-peer': true} }",
1063 migrate_set_parameter_str(from
, "tls-creds", "tlscredsx509client0");
1064 if (args
->certhostname
) {
1065 migrate_set_parameter_str(from
, "tls-hostname", args
->certhostname
);
1068 qtest_qmp_assert_success(to
,
1069 "{ 'execute': 'object-add',"
1070 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1071 " 'id': 'tlscredsx509server0',"
1072 " 'endpoint': 'server',"
1074 " 'sanity-check': true,"
1075 " 'verify-peer': %i} }",
1076 data
->workdir
, args
->verifyclient
);
1077 migrate_set_parameter_str(to
, "tls-creds", "tlscredsx509server0");
1079 if (args
->authzclient
) {
1080 qtest_qmp_assert_success(to
,
1081 "{ 'execute': 'object-add',"
1082 " 'arguments': { 'qom-type': 'authz-simple',"
1083 " 'id': 'tlsauthz0',"
1084 " 'identity': %s} }",
1085 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME
);
1086 migrate_set_parameter_str(to
, "tls-authz", "tlsauthz0");
1093 * The normal case: match server's cert hostname against
1094 * whatever host we were telling QEMU to connect to (if any)
1097 test_migrate_tls_x509_start_default_host(QTestState
*from
,
1100 TestMigrateTLSX509 args
= {
1101 .verifyclient
= true,
1103 .certipaddr
= "127.0.0.1"
1105 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1109 * The unusual case: the server's cert is different from
1110 * the address we're telling QEMU to connect to (if any),
1111 * so we must give QEMU an explicit hostname to validate
1114 test_migrate_tls_x509_start_override_host(QTestState
*from
,
1117 TestMigrateTLSX509 args
= {
1118 .verifyclient
= true,
1120 .certhostname
= "qemu.org",
1122 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1126 * The unusual case: the server's cert is different from
1127 * the address we're telling QEMU to connect to, and so we
1128 * expect the client to reject the server
1131 test_migrate_tls_x509_start_mismatch_host(QTestState
*from
,
1134 TestMigrateTLSX509 args
= {
1135 .verifyclient
= true,
1137 .certipaddr
= "10.0.0.1",
1139 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1143 test_migrate_tls_x509_start_friendly_client(QTestState
*from
,
1146 TestMigrateTLSX509 args
= {
1147 .verifyclient
= true,
1149 .authzclient
= true,
1150 .certipaddr
= "127.0.0.1",
1152 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1156 test_migrate_tls_x509_start_hostile_client(QTestState
*from
,
1159 TestMigrateTLSX509 args
= {
1160 .verifyclient
= true,
1162 .hostileclient
= true,
1163 .authzclient
= true,
1164 .certipaddr
= "127.0.0.1",
1166 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1170 * The case with no client certificate presented,
1171 * and no server verification
1174 test_migrate_tls_x509_start_allow_anon_client(QTestState
*from
,
1177 TestMigrateTLSX509 args
= {
1178 .certipaddr
= "127.0.0.1",
1180 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1184 * The case with no client certificate presented,
1185 * and server verification rejecting
1188 test_migrate_tls_x509_start_reject_anon_client(QTestState
*from
,
1191 TestMigrateTLSX509 args
= {
1192 .verifyclient
= true,
1193 .certipaddr
= "127.0.0.1",
1195 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1199 test_migrate_tls_x509_finish(QTestState
*from
,
1203 TestMigrateTLSX509Data
*data
= opaque
;
1205 test_tls_cleanup(data
->keyfile
);
1206 g_free(data
->keyfile
);
1208 unlink(data
->cacert
);
1209 g_free(data
->cacert
);
1210 unlink(data
->servercert
);
1211 g_free(data
->servercert
);
1212 unlink(data
->serverkey
);
1213 g_free(data
->serverkey
);
1215 if (data
->clientcert
) {
1216 unlink(data
->clientcert
);
1217 g_free(data
->clientcert
);
1219 if (data
->clientkey
) {
1220 unlink(data
->clientkey
);
1221 g_free(data
->clientkey
);
1224 rmdir(data
->workdir
);
1225 g_free(data
->workdir
);
1229 #endif /* CONFIG_TASN1 */
1230 #endif /* CONFIG_GNUTLS */
1233 test_migrate_compress_start(QTestState
*from
,
1236 migrate_set_parameter_int(from
, "compress-level", 1);
1237 migrate_set_parameter_int(from
, "compress-threads", 4);
1238 migrate_set_parameter_bool(from
, "compress-wait-thread", true);
1239 migrate_set_parameter_int(to
, "decompress-threads", 4);
1241 migrate_set_capability(from
, "compress", true);
1242 migrate_set_capability(to
, "compress", true);
1248 test_migrate_compress_nowait_start(QTestState
*from
,
1251 migrate_set_parameter_int(from
, "compress-level", 9);
1252 migrate_set_parameter_int(from
, "compress-threads", 1);
1253 migrate_set_parameter_bool(from
, "compress-wait-thread", false);
1254 migrate_set_parameter_int(to
, "decompress-threads", 1);
1256 migrate_set_capability(from
, "compress", true);
1257 migrate_set_capability(to
, "compress", true);
1262 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
1263 QTestState
**to_ptr
,
1264 MigrateCommon
*args
)
1266 QTestState
*from
, *to
;
1268 if (test_migrate_start(&from
, &to
, "defer", &args
->start
)) {
1272 if (args
->start_hook
) {
1273 args
->postcopy_data
= args
->start_hook(from
, to
);
1276 migrate_set_capability(from
, "postcopy-ram", true);
1277 migrate_set_capability(to
, "postcopy-ram", true);
1278 migrate_set_capability(to
, "postcopy-blocktime", true);
1280 if (args
->postcopy_preempt
) {
1281 migrate_set_capability(from
, "postcopy-preempt", true);
1282 migrate_set_capability(to
, "postcopy-preempt", true);
1285 migrate_ensure_non_converge(from
);
1287 migrate_prepare_for_dirty_mem(from
);
1288 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1289 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1291 /* Wait for the first serial output from the source */
1292 wait_for_serial("src_serial");
1294 g_autofree
char *uri
= migrate_get_socket_address(to
, "socket-address");
1295 migrate_qmp(from
, uri
, "{}");
1297 migrate_wait_for_dirty_mem(from
, to
);
1305 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
,
1306 MigrateCommon
*args
)
1308 wait_for_migration_complete(from
);
1310 /* Make sure we get at least one "B" on destination */
1311 wait_for_serial("dest_serial");
1313 if (uffd_feature_thread_id
) {
1317 if (args
->finish_hook
) {
1318 args
->finish_hook(from
, to
, args
->postcopy_data
);
1319 args
->postcopy_data
= NULL
;
1322 test_migrate_end(from
, to
, true);
1325 static void test_postcopy_common(MigrateCommon
*args
)
1327 QTestState
*from
, *to
;
1329 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1332 migrate_postcopy_start(from
, to
);
1333 migrate_postcopy_complete(from
, to
, args
);
1336 static void test_postcopy(void)
1338 MigrateCommon args
= { };
1340 test_postcopy_common(&args
);
1343 static void test_postcopy_compress(void)
1345 MigrateCommon args
= {
1346 .start_hook
= test_migrate_compress_start
1349 test_postcopy_common(&args
);
1352 static void test_postcopy_preempt(void)
1354 MigrateCommon args
= {
1355 .postcopy_preempt
= true,
1358 test_postcopy_common(&args
);
1361 #ifdef CONFIG_GNUTLS
1362 static void test_postcopy_tls_psk(void)
1364 MigrateCommon args
= {
1365 .start_hook
= test_migrate_tls_psk_start_match
,
1366 .finish_hook
= test_migrate_tls_psk_finish
,
1369 test_postcopy_common(&args
);
1372 static void test_postcopy_preempt_tls_psk(void)
1374 MigrateCommon args
= {
1375 .postcopy_preempt
= true,
1376 .start_hook
= test_migrate_tls_psk_start_match
,
1377 .finish_hook
= test_migrate_tls_psk_finish
,
1380 test_postcopy_common(&args
);
1384 static void test_postcopy_recovery_common(MigrateCommon
*args
)
1386 QTestState
*from
, *to
;
1387 g_autofree
char *uri
= NULL
;
1389 /* Always hide errors for postcopy recover tests since they're expected */
1390 args
->start
.hide_stderr
= true;
1392 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1396 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1397 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
1399 /* Now we start the postcopy */
1400 migrate_postcopy_start(from
, to
);
1403 * Wait until postcopy is really started; we can only run the
1404 * migrate-pause command during a postcopy
1406 wait_for_migration_status(from
, "postcopy-active", NULL
);
1409 * Manually stop the postcopy migration. This emulates a network
1410 * failure with the migration socket
1412 migrate_pause(from
);
1415 * Wait for destination side to reach postcopy-paused state. The
1416 * migrate-recover command can only succeed if destination machine
1417 * is in the paused state
1419 wait_for_migration_status(to
, "postcopy-paused",
1420 (const char * []) { "failed", "active",
1421 "completed", NULL
});
1424 * Create a new socket to emulate a new channel that is different
1425 * from the broken migration channel; tell the destination to
1426 * listen to the new port
1428 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
1429 migrate_recover(to
, uri
);
1432 * Try to rebuild the migration channel using the resume flag and
1433 * the newly created channel
1435 wait_for_migration_status(from
, "postcopy-paused",
1436 (const char * []) { "failed", "active",
1437 "completed", NULL
});
1438 migrate_qmp(from
, uri
, "{'resume': true}");
1440 /* Restore the postcopy bandwidth to unlimited */
1441 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
1443 migrate_postcopy_complete(from
, to
, args
);
1446 static void test_postcopy_recovery(void)
1448 MigrateCommon args
= { };
1450 test_postcopy_recovery_common(&args
);
1453 static void test_postcopy_recovery_compress(void)
1455 MigrateCommon args
= {
1456 .start_hook
= test_migrate_compress_start
1459 test_postcopy_recovery_common(&args
);
1462 #ifdef CONFIG_GNUTLS
1463 static void test_postcopy_recovery_tls_psk(void)
1465 MigrateCommon args
= {
1466 .start_hook
= test_migrate_tls_psk_start_match
,
1467 .finish_hook
= test_migrate_tls_psk_finish
,
1470 test_postcopy_recovery_common(&args
);
1474 static void test_postcopy_preempt_recovery(void)
1476 MigrateCommon args
= {
1477 .postcopy_preempt
= true,
1480 test_postcopy_recovery_common(&args
);
1483 #ifdef CONFIG_GNUTLS
1484 /* This contains preempt+recovery+tls test altogether */
1485 static void test_postcopy_preempt_all(void)
1487 MigrateCommon args
= {
1488 .postcopy_preempt
= true,
1489 .start_hook
= test_migrate_tls_psk_start_match
,
1490 .finish_hook
= test_migrate_tls_psk_finish
,
1493 test_postcopy_recovery_common(&args
);
1498 static void test_baddest(void)
1500 MigrateStart args
= {
1503 QTestState
*from
, *to
;
1505 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1508 migrate_qmp(from
, "tcp:127.0.0.1:0", "{}");
1509 wait_for_migration_fail(from
, false);
1510 test_migrate_end(from
, to
, false);
1513 static void test_precopy_common(MigrateCommon
*args
)
1515 QTestState
*from
, *to
;
1516 void *data_hook
= NULL
;
1518 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1522 if (args
->start_hook
) {
1523 data_hook
= args
->start_hook(from
, to
);
1526 /* Wait for the first serial output from the source */
1527 if (args
->result
== MIG_TEST_SUCCEED
) {
1528 wait_for_serial("src_serial");
1532 migrate_ensure_non_converge(from
);
1533 migrate_prepare_for_dirty_mem(from
);
1536 * Testing non-live migration, we allow it to run at
1537 * full speed to ensure short test case duration.
1538 * For tests expected to fail, we don't need to
1541 if (args
->result
== MIG_TEST_SUCCEED
) {
1542 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1543 if (!got_src_stop
) {
1544 qtest_qmp_eventwait(from
, "STOP");
1546 migrate_ensure_converge(from
);
1550 if (!args
->connect_uri
) {
1551 g_autofree
char *local_connect_uri
=
1552 migrate_get_socket_address(to
, "socket-address");
1553 migrate_qmp(from
, local_connect_uri
, "{}");
1555 migrate_qmp(from
, args
->connect_uri
, "{}");
1559 if (args
->result
!= MIG_TEST_SUCCEED
) {
1560 bool allow_active
= args
->result
== MIG_TEST_FAIL
;
1561 wait_for_migration_fail(from
, allow_active
);
1563 if (args
->result
== MIG_TEST_FAIL_DEST_QUIT_ERR
) {
1564 qtest_set_expected_status(to
, EXIT_FAILURE
);
1569 * For initial iteration(s) we must do a full pass,
1570 * but for the final iteration, we need only wait
1571 * for some dirty mem before switching to converge
1573 while (args
->iterations
> 1) {
1574 wait_for_migration_pass(from
);
1577 migrate_wait_for_dirty_mem(from
, to
);
1579 migrate_ensure_converge(from
);
1582 * We do this first, as it has a timeout to stop us
1583 * hanging forever if migration didn't converge
1585 wait_for_migration_complete(from
);
1587 if (!got_src_stop
) {
1588 qtest_qmp_eventwait(from
, "STOP");
1591 wait_for_migration_complete(from
);
1593 * Must wait for dst to finish reading all incoming
1594 * data on the socket before issuing 'cont' otherwise
1597 wait_for_migration_complete(to
);
1599 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1602 if (!got_dst_resume
) {
1603 qtest_qmp_eventwait(to
, "RESUME");
1606 wait_for_serial("dest_serial");
1609 if (args
->finish_hook
) {
1610 args
->finish_hook(from
, to
, data_hook
);
1613 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1616 static void test_precopy_unix_plain(void)
1618 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1619 MigrateCommon args
= {
1623 * The simplest use case of precopy, covering smoke tests of
1624 * get-dirty-log dirty tracking.
1629 test_precopy_common(&args
);
1633 static void test_precopy_unix_dirty_ring(void)
1635 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1636 MigrateCommon args
= {
1638 .use_dirty_ring
= true,
1643 * Besides the precopy/unix basic test, cover dirty ring interface
1644 * rather than get-dirty-log.
1649 test_precopy_common(&args
);
1652 #ifdef CONFIG_GNUTLS
1653 static void test_precopy_unix_tls_psk(void)
1655 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1656 MigrateCommon args
= {
1659 .start_hook
= test_migrate_tls_psk_start_match
,
1660 .finish_hook
= test_migrate_tls_psk_finish
,
1663 test_precopy_common(&args
);
1667 static void test_precopy_unix_tls_x509_default_host(void)
1669 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1670 MigrateCommon args
= {
1672 .hide_stderr
= true,
1676 .start_hook
= test_migrate_tls_x509_start_default_host
,
1677 .finish_hook
= test_migrate_tls_x509_finish
,
1678 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1681 test_precopy_common(&args
);
1684 static void test_precopy_unix_tls_x509_override_host(void)
1686 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1687 MigrateCommon args
= {
1690 .start_hook
= test_migrate_tls_x509_start_override_host
,
1691 .finish_hook
= test_migrate_tls_x509_finish
,
1694 test_precopy_common(&args
);
1696 #endif /* CONFIG_TASN1 */
1697 #endif /* CONFIG_GNUTLS */
1700 /* Currently upset on aarch64 TCG */
1701 static void test_ignore_shared(void)
1703 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1704 QTestState
*from
, *to
;
1706 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
1710 migrate_ensure_non_converge(from
);
1711 migrate_prepare_for_dirty_mem(from
);
1713 migrate_set_capability(from
, "x-ignore-shared", true);
1714 migrate_set_capability(to
, "x-ignore-shared", true);
1716 /* Wait for the first serial output from the source */
1717 wait_for_serial("src_serial");
1719 migrate_qmp(from
, uri
, "{}");
1721 migrate_wait_for_dirty_mem(from
, to
);
1723 if (!got_src_stop
) {
1724 qtest_qmp_eventwait(from
, "STOP");
1727 qtest_qmp_eventwait(to
, "RESUME");
1729 wait_for_serial("dest_serial");
1730 wait_for_migration_complete(from
);
1732 /* Check whether shared RAM has been really skipped */
1733 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1735 test_migrate_end(from
, to
, true);
1740 test_migrate_xbzrle_start(QTestState
*from
,
1743 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1745 migrate_set_capability(from
, "xbzrle", true);
1746 migrate_set_capability(to
, "xbzrle", true);
1751 static void test_precopy_unix_xbzrle(void)
1753 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1754 MigrateCommon args
= {
1757 .start_hook
= test_migrate_xbzrle_start
,
1760 * XBZRLE needs pages to be modified when doing the 2nd+ round
1761 * iteration to have real data pushed to the stream.
1766 test_precopy_common(&args
);
1769 static void test_precopy_unix_compress(void)
1771 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1772 MigrateCommon args
= {
1775 .start_hook
= test_migrate_compress_start
,
1777 * Test that no invalid thread state is left over from
1778 * the previous iteration.
1782 * We make sure the compressor can always work well even if guest
1783 * memory is changing. See commit 34ab9e9743 where we used to fix
1784 * a bug when only trigger-able with guest memory changing.
1789 test_precopy_common(&args
);
1792 static void test_precopy_unix_compress_nowait(void)
1794 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1795 MigrateCommon args
= {
1798 .start_hook
= test_migrate_compress_nowait_start
,
1800 * Test that no invalid thread state is left over from
1801 * the previous iteration.
1804 /* Same reason for the wait version of precopy compress test */
1808 test_precopy_common(&args
);
1811 static void test_precopy_tcp_plain(void)
1813 MigrateCommon args
= {
1814 .listen_uri
= "tcp:127.0.0.1:0",
1817 test_precopy_common(&args
);
1820 static void *test_migrate_switchover_ack_start(QTestState
*from
, QTestState
*to
)
1823 migrate_set_capability(from
, "return-path", true);
1824 migrate_set_capability(to
, "return-path", true);
1826 migrate_set_capability(from
, "switchover-ack", true);
1827 migrate_set_capability(to
, "switchover-ack", true);
1832 static void test_precopy_tcp_switchover_ack(void)
1834 MigrateCommon args
= {
1835 .listen_uri
= "tcp:127.0.0.1:0",
1836 .start_hook
= test_migrate_switchover_ack_start
,
1838 * Source VM must be running in order to consider the switchover ACK
1839 * when deciding to do switchover or not.
1844 test_precopy_common(&args
);
1847 #ifdef CONFIG_GNUTLS
1848 static void test_precopy_tcp_tls_psk_match(void)
1850 MigrateCommon args
= {
1851 .listen_uri
= "tcp:127.0.0.1:0",
1852 .start_hook
= test_migrate_tls_psk_start_match
,
1853 .finish_hook
= test_migrate_tls_psk_finish
,
1856 test_precopy_common(&args
);
1859 static void test_precopy_tcp_tls_psk_mismatch(void)
1861 MigrateCommon args
= {
1863 .hide_stderr
= true,
1865 .listen_uri
= "tcp:127.0.0.1:0",
1866 .start_hook
= test_migrate_tls_psk_start_mismatch
,
1867 .finish_hook
= test_migrate_tls_psk_finish
,
1868 .result
= MIG_TEST_FAIL
,
1871 test_precopy_common(&args
);
1875 static void test_precopy_tcp_tls_x509_default_host(void)
1877 MigrateCommon args
= {
1878 .listen_uri
= "tcp:127.0.0.1:0",
1879 .start_hook
= test_migrate_tls_x509_start_default_host
,
1880 .finish_hook
= test_migrate_tls_x509_finish
,
1883 test_precopy_common(&args
);
1886 static void test_precopy_tcp_tls_x509_override_host(void)
1888 MigrateCommon args
= {
1889 .listen_uri
= "tcp:127.0.0.1:0",
1890 .start_hook
= test_migrate_tls_x509_start_override_host
,
1891 .finish_hook
= test_migrate_tls_x509_finish
,
1894 test_precopy_common(&args
);
1897 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1899 MigrateCommon args
= {
1901 .hide_stderr
= true,
1903 .listen_uri
= "tcp:127.0.0.1:0",
1904 .start_hook
= test_migrate_tls_x509_start_mismatch_host
,
1905 .finish_hook
= test_migrate_tls_x509_finish
,
1906 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1909 test_precopy_common(&args
);
1912 static void test_precopy_tcp_tls_x509_friendly_client(void)
1914 MigrateCommon args
= {
1915 .listen_uri
= "tcp:127.0.0.1:0",
1916 .start_hook
= test_migrate_tls_x509_start_friendly_client
,
1917 .finish_hook
= test_migrate_tls_x509_finish
,
1920 test_precopy_common(&args
);
1923 static void test_precopy_tcp_tls_x509_hostile_client(void)
1925 MigrateCommon args
= {
1927 .hide_stderr
= true,
1929 .listen_uri
= "tcp:127.0.0.1:0",
1930 .start_hook
= test_migrate_tls_x509_start_hostile_client
,
1931 .finish_hook
= test_migrate_tls_x509_finish
,
1932 .result
= MIG_TEST_FAIL
,
1935 test_precopy_common(&args
);
1938 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1940 MigrateCommon args
= {
1941 .listen_uri
= "tcp:127.0.0.1:0",
1942 .start_hook
= test_migrate_tls_x509_start_allow_anon_client
,
1943 .finish_hook
= test_migrate_tls_x509_finish
,
1946 test_precopy_common(&args
);
1949 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1951 MigrateCommon args
= {
1953 .hide_stderr
= true,
1955 .listen_uri
= "tcp:127.0.0.1:0",
1956 .start_hook
= test_migrate_tls_x509_start_reject_anon_client
,
1957 .finish_hook
= test_migrate_tls_x509_finish
,
1958 .result
= MIG_TEST_FAIL
,
1961 test_precopy_common(&args
);
1963 #endif /* CONFIG_TASN1 */
1964 #endif /* CONFIG_GNUTLS */
1967 static void *test_migrate_fd_start_hook(QTestState
*from
,
1973 /* Create two connected sockets for migration */
1974 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1975 g_assert_cmpint(ret
, ==, 0);
1977 /* Send the 1st socket to the target */
1978 qtest_qmp_fds_assert_success(to
, &pair
[0], 1,
1979 "{ 'execute': 'getfd',"
1980 " 'arguments': { 'fdname': 'fd-mig' }}");
1983 /* Start incoming migration from the 1st socket */
1984 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1985 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1987 /* Send the 2nd socket to the target */
1988 qtest_qmp_fds_assert_success(from
, &pair
[1], 1,
1989 "{ 'execute': 'getfd',"
1990 " 'arguments': { 'fdname': 'fd-mig' }}");
1996 static void test_migrate_fd_finish_hook(QTestState
*from
,
2001 const char *error_desc
;
2003 /* Test closing fds */
2004 /* We assume, that QEMU removes named fd from its list,
2005 * so this should fail */
2006 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
2007 " 'arguments': { 'fdname': 'fd-mig' }}");
2008 g_assert_true(qdict_haskey(rsp
, "error"));
2009 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2010 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2013 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
2014 " 'arguments': { 'fdname': 'fd-mig' }}");
2015 g_assert_true(qdict_haskey(rsp
, "error"));
2016 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
2017 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
2021 static void test_migrate_fd_proto(void)
2023 MigrateCommon args
= {
2024 .listen_uri
= "defer",
2025 .connect_uri
= "fd:fd-mig",
2026 .start_hook
= test_migrate_fd_start_hook
,
2027 .finish_hook
= test_migrate_fd_finish_hook
2029 test_precopy_common(&args
);
2033 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
2035 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2036 QTestState
*from
, *to
;
2038 if (test_migrate_start(&from
, &to
, uri
, args
)) {
2043 * UUID validation is at the begin of migration. So, the main process of
2044 * migration is not interesting for us here. Thus, set huge downtime for
2045 * very fast migration.
2047 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
2048 migrate_set_capability(from
, "validate-uuid", true);
2050 /* Wait for the first serial output from the source */
2051 wait_for_serial("src_serial");
2053 migrate_qmp(from
, uri
, "{}");
2056 qtest_set_expected_status(to
, EXIT_FAILURE
);
2057 wait_for_migration_fail(from
, true);
2059 wait_for_migration_complete(from
);
2062 test_migrate_end(from
, to
, false);
2065 static void test_validate_uuid(void)
2067 MigrateStart args
= {
2068 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2069 .opts_target
= "-uuid 11111111-1111-1111-1111-111111111111",
2072 do_test_validate_uuid(&args
, false);
2075 static void test_validate_uuid_error(void)
2077 MigrateStart args
= {
2078 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2079 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2080 .hide_stderr
= true,
2083 do_test_validate_uuid(&args
, true);
2086 static void test_validate_uuid_src_not_set(void)
2088 MigrateStart args
= {
2089 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2090 .hide_stderr
= true,
2093 do_test_validate_uuid(&args
, false);
2096 static void test_validate_uuid_dst_not_set(void)
2098 MigrateStart args
= {
2099 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2100 .hide_stderr
= true,
2103 do_test_validate_uuid(&args
, false);
2107 * The way auto_converge works, we need to do too many passes to
2108 * run this test. Auto_converge logic is only run once every
2109 * three iterations, so:
2111 * - 3 iterations without auto_converge enabled
2112 * - 3 iterations with pct = 5
2113 * - 3 iterations with pct = 30
2114 * - 3 iterations with pct = 55
2115 * - 3 iterations with pct = 80
2116 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2118 * To make things even worse, we need to run the initial stage at
2119 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2121 static void test_migrate_auto_converge(void)
2123 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2124 MigrateStart args
= {};
2125 QTestState
*from
, *to
;
2129 * We want the test to be stable and as fast as possible.
2130 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2131 * so we need to decrease a bandwidth.
2133 const int64_t init_pct
= 5, inc_pct
= 25, max_pct
= 95;
2135 if (test_migrate_start(&from
, &to
, uri
, &args
)) {
2139 migrate_set_capability(from
, "auto-converge", true);
2140 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
2141 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
2142 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
2145 * Set the initial parameters so that the migration could not converge
2146 * without throttling.
2148 migrate_ensure_non_converge(from
);
2150 /* To check remaining size after precopy */
2151 migrate_set_capability(from
, "pause-before-switchover", true);
2153 /* Wait for the first serial output from the source */
2154 wait_for_serial("src_serial");
2156 migrate_qmp(from
, uri
, "{}");
2158 /* Wait for throttling begins */
2161 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2162 if (percentage
!= 0) {
2166 g_assert_false(got_src_stop
);
2168 /* The first percentage of throttling should be at least init_pct */
2169 g_assert_cmpint(percentage
, >=, init_pct
);
2170 /* Now, when we tested that throttling works, let it converge */
2171 migrate_ensure_converge(from
);
2174 * Wait for pre-switchover status to check last throttle percentage
2175 * and remaining. These values will be zeroed later
2177 wait_for_migration_status(from
, "pre-switchover", NULL
);
2179 /* The final percentage of throttling shouldn't be greater than max_pct */
2180 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2181 g_assert_cmpint(percentage
, <=, max_pct
);
2182 migrate_continue(from
, "pre-switchover");
2184 qtest_qmp_eventwait(to
, "RESUME");
2186 wait_for_serial("dest_serial");
2187 wait_for_migration_complete(from
);
2189 test_migrate_end(from
, to
, true);
2193 test_migrate_precopy_tcp_multifd_start_common(QTestState
*from
,
2197 migrate_set_parameter_int(from
, "multifd-channels", 16);
2198 migrate_set_parameter_int(to
, "multifd-channels", 16);
2200 migrate_set_parameter_str(from
, "multifd-compression", method
);
2201 migrate_set_parameter_str(to
, "multifd-compression", method
);
2203 migrate_set_capability(from
, "multifd", true);
2204 migrate_set_capability(to
, "multifd", true);
2206 /* Start incoming migration from the 1st socket */
2207 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
2208 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2214 test_migrate_precopy_tcp_multifd_start(QTestState
*from
,
2217 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2221 test_migrate_precopy_tcp_multifd_zlib_start(QTestState
*from
,
2224 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zlib");
2229 test_migrate_precopy_tcp_multifd_zstd_start(QTestState
*from
,
2232 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zstd");
2234 #endif /* CONFIG_ZSTD */
2236 static void test_multifd_tcp_none(void)
2238 MigrateCommon args
= {
2239 .listen_uri
= "defer",
2240 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2242 * Multifd is more complicated than most of the features, it
2243 * directly takes guest page buffers when sending, make sure
2244 * everything will work alright even if guest page is changing.
2248 test_precopy_common(&args
);
2251 static void test_multifd_tcp_zlib(void)
2253 MigrateCommon args
= {
2254 .listen_uri
= "defer",
2255 .start_hook
= test_migrate_precopy_tcp_multifd_zlib_start
,
2257 test_precopy_common(&args
);
2261 static void test_multifd_tcp_zstd(void)
2263 MigrateCommon args
= {
2264 .listen_uri
= "defer",
2265 .start_hook
= test_migrate_precopy_tcp_multifd_zstd_start
,
2267 test_precopy_common(&args
);
2271 #ifdef CONFIG_GNUTLS
2273 test_migrate_multifd_tcp_tls_psk_start_match(QTestState
*from
,
2276 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2277 return test_migrate_tls_psk_start_match(from
, to
);
2281 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState
*from
,
2284 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2285 return test_migrate_tls_psk_start_mismatch(from
, to
);
2290 test_migrate_multifd_tls_x509_start_default_host(QTestState
*from
,
2293 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2294 return test_migrate_tls_x509_start_default_host(from
, to
);
2298 test_migrate_multifd_tls_x509_start_override_host(QTestState
*from
,
2301 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2302 return test_migrate_tls_x509_start_override_host(from
, to
);
2306 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState
*from
,
2309 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2310 return test_migrate_tls_x509_start_mismatch_host(from
, to
);
2314 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState
*from
,
2317 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2318 return test_migrate_tls_x509_start_allow_anon_client(from
, to
);
2322 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState
*from
,
2325 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2326 return test_migrate_tls_x509_start_reject_anon_client(from
, to
);
2328 #endif /* CONFIG_TASN1 */
2330 static void test_multifd_tcp_tls_psk_match(void)
2332 MigrateCommon args
= {
2333 .listen_uri
= "defer",
2334 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_match
,
2335 .finish_hook
= test_migrate_tls_psk_finish
,
2337 test_precopy_common(&args
);
2340 static void test_multifd_tcp_tls_psk_mismatch(void)
2342 MigrateCommon args
= {
2344 .hide_stderr
= true,
2346 .listen_uri
= "defer",
2347 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_mismatch
,
2348 .finish_hook
= test_migrate_tls_psk_finish
,
2349 .result
= MIG_TEST_FAIL
,
2351 test_precopy_common(&args
);
2355 static void test_multifd_tcp_tls_x509_default_host(void)
2357 MigrateCommon args
= {
2358 .listen_uri
= "defer",
2359 .start_hook
= test_migrate_multifd_tls_x509_start_default_host
,
2360 .finish_hook
= test_migrate_tls_x509_finish
,
2362 test_precopy_common(&args
);
2365 static void test_multifd_tcp_tls_x509_override_host(void)
2367 MigrateCommon args
= {
2368 .listen_uri
= "defer",
2369 .start_hook
= test_migrate_multifd_tls_x509_start_override_host
,
2370 .finish_hook
= test_migrate_tls_x509_finish
,
2372 test_precopy_common(&args
);
2375 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2378 * This has different behaviour to the non-multifd case.
2380 * In non-multifd case when client aborts due to mismatched
2381 * cert host, the server has already started trying to load
2382 * migration state, and so it exits with I/O failure.
2384 * In multifd case when client aborts due to mismatched
2385 * cert host, the server is still waiting for the other
2386 * multifd connections to arrive so hasn't started trying
2387 * to load migration state, and thus just aborts the migration
2390 MigrateCommon args
= {
2392 .hide_stderr
= true,
2394 .listen_uri
= "defer",
2395 .start_hook
= test_migrate_multifd_tls_x509_start_mismatch_host
,
2396 .finish_hook
= test_migrate_tls_x509_finish
,
2397 .result
= MIG_TEST_FAIL
,
2399 test_precopy_common(&args
);
2402 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2404 MigrateCommon args
= {
2405 .listen_uri
= "defer",
2406 .start_hook
= test_migrate_multifd_tls_x509_start_allow_anon_client
,
2407 .finish_hook
= test_migrate_tls_x509_finish
,
2409 test_precopy_common(&args
);
2412 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2414 MigrateCommon args
= {
2416 .hide_stderr
= true,
2418 .listen_uri
= "defer",
2419 .start_hook
= test_migrate_multifd_tls_x509_start_reject_anon_client
,
2420 .finish_hook
= test_migrate_tls_x509_finish
,
2421 .result
= MIG_TEST_FAIL
,
2423 test_precopy_common(&args
);
2425 #endif /* CONFIG_TASN1 */
2426 #endif /* CONFIG_GNUTLS */
2434 * launch another target
2437 * And see that it works
2439 static void test_multifd_tcp_cancel(void)
2441 MigrateStart args
= {
2442 .hide_stderr
= true,
2444 QTestState
*from
, *to
, *to2
;
2445 g_autofree
char *uri
= NULL
;
2447 if (test_migrate_start(&from
, &to
, "defer", &args
)) {
2451 migrate_ensure_non_converge(from
);
2452 migrate_prepare_for_dirty_mem(from
);
2454 migrate_set_parameter_int(from
, "multifd-channels", 16);
2455 migrate_set_parameter_int(to
, "multifd-channels", 16);
2457 migrate_set_capability(from
, "multifd", true);
2458 migrate_set_capability(to
, "multifd", true);
2460 /* Start incoming migration from the 1st socket */
2461 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
2462 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2464 /* Wait for the first serial output from the source */
2465 wait_for_serial("src_serial");
2467 uri
= migrate_get_socket_address(to
, "socket-address");
2469 migrate_qmp(from
, uri
, "{}");
2471 migrate_wait_for_dirty_mem(from
, to
);
2473 migrate_cancel(from
);
2475 /* Make sure QEMU process "to" exited */
2476 qtest_set_expected_status(to
, EXIT_FAILURE
);
2477 qtest_wait_qemu(to
);
2479 args
= (MigrateStart
){
2480 .only_target
= true,
2483 if (test_migrate_start(&from
, &to2
, "defer", &args
)) {
2487 migrate_set_parameter_int(to2
, "multifd-channels", 16);
2489 migrate_set_capability(to2
, "multifd", true);
2491 /* Start incoming migration from the 1st socket */
2492 qtest_qmp_assert_success(to2
, "{ 'execute': 'migrate-incoming',"
2493 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2496 uri
= migrate_get_socket_address(to2
, "socket-address");
2498 wait_for_migration_status(from
, "cancelled", NULL
);
2500 migrate_ensure_non_converge(from
);
2502 migrate_qmp(from
, uri
, "{}");
2504 migrate_wait_for_dirty_mem(from
, to2
);
2506 migrate_ensure_converge(from
);
2508 if (!got_src_stop
) {
2509 qtest_qmp_eventwait(from
, "STOP");
2511 qtest_qmp_eventwait(to2
, "RESUME");
2513 wait_for_serial("dest_serial");
2514 wait_for_migration_complete(from
);
2515 test_migrate_end(from
, to2
, true);
2518 static void calc_dirty_rate(QTestState
*who
, uint64_t calc_time
)
2520 qtest_qmp_assert_success(who
,
2521 "{ 'execute': 'calc-dirty-rate',"
2523 "'calc-time': %" PRIu64
","
2524 "'mode': 'dirty-ring' }}",
2528 static QDict
*query_dirty_rate(QTestState
*who
)
2530 return qtest_qmp_assert_success_ref(who
,
2531 "{ 'execute': 'query-dirty-rate' }");
2534 static void dirtylimit_set_all(QTestState
*who
, uint64_t dirtyrate
)
2536 qtest_qmp_assert_success(who
,
2537 "{ 'execute': 'set-vcpu-dirty-limit',"
2539 "'dirty-rate': %" PRIu64
" } }",
2543 static void cancel_vcpu_dirty_limit(QTestState
*who
)
2545 qtest_qmp_assert_success(who
,
2546 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2549 static QDict
*query_vcpu_dirty_limit(QTestState
*who
)
2553 rsp
= qtest_qmp(who
, "{ 'execute': 'query-vcpu-dirty-limit' }");
2554 g_assert(!qdict_haskey(rsp
, "error"));
2555 g_assert(qdict_haskey(rsp
, "return"));
2560 static bool calc_dirtyrate_ready(QTestState
*who
)
2565 rsp_return
= query_dirty_rate(who
);
2566 g_assert(rsp_return
);
2568 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2571 return g_strcmp0(status
, "measuring");
2574 static void wait_for_calc_dirtyrate_complete(QTestState
*who
,
2577 int max_try_count
= 10000;
2578 usleep(time_s
* 1000000);
2580 while (!calc_dirtyrate_ready(who
) && max_try_count
--) {
2585 * Set the timeout with 10 s(max_try_count * 1000us),
2586 * if dirtyrate measurement not complete, fail test.
2588 g_assert_cmpint(max_try_count
, !=, 0);
2591 static int64_t get_dirty_rate(QTestState
*who
)
2596 const QListEntry
*entry
;
2600 rsp_return
= query_dirty_rate(who
);
2601 g_assert(rsp_return
);
2603 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2605 g_assert_cmpstr(status
, ==, "measured");
2607 rates
= qdict_get_qlist(rsp_return
, "vcpu-dirty-rate");
2608 g_assert(rates
&& !qlist_empty(rates
));
2610 entry
= qlist_first(rates
);
2613 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2616 dirtyrate
= qdict_get_try_int(rate
, "dirty-rate", -1);
2618 qobject_unref(rsp_return
);
2622 static int64_t get_limit_rate(QTestState
*who
)
2626 const QListEntry
*entry
;
2630 rsp_return
= query_vcpu_dirty_limit(who
);
2631 g_assert(rsp_return
);
2633 rates
= qdict_get_qlist(rsp_return
, "return");
2634 g_assert(rates
&& !qlist_empty(rates
));
2636 entry
= qlist_first(rates
);
2639 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2642 dirtyrate
= qdict_get_try_int(rate
, "limit-rate", -1);
2644 qobject_unref(rsp_return
);
2648 static QTestState
*dirtylimit_start_vm(void)
2650 QTestState
*vm
= NULL
;
2652 cmd
= g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2653 "-name dirtylimit-test,debug-threads=on "
2655 "-serial file:%s/vm_serial "
2656 "-drive file=%s,format=raw ",
2659 vm
= qtest_init(cmd
);
2663 static void dirtylimit_stop_vm(QTestState
*vm
)
2666 cleanup("vm_serial");
2669 static void test_vcpu_dirty_limit(void)
2672 int64_t origin_rate
;
2675 int max_try_count
= 20;
2678 /* Start vm for vcpu dirtylimit test */
2679 vm
= dirtylimit_start_vm();
2681 /* Wait for the first serial output from the vm*/
2682 wait_for_serial("vm_serial");
2684 /* Do dirtyrate measurement with calc time equals 1s */
2685 calc_dirty_rate(vm
, 1);
2687 /* Sleep calc time and wait for calc dirtyrate complete */
2688 wait_for_calc_dirtyrate_complete(vm
, 1);
2690 /* Query original dirty page rate */
2691 origin_rate
= get_dirty_rate(vm
);
2693 /* VM booted from bootsect should dirty memory steadily */
2694 assert(origin_rate
!= 0);
2696 /* Setup quota dirty page rate at half of origin */
2697 quota_rate
= origin_rate
/ 2;
2699 /* Set dirtylimit */
2700 dirtylimit_set_all(vm
, quota_rate
);
2703 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2706 g_assert_cmpint(quota_rate
, ==, get_limit_rate(vm
));
2708 /* Sleep a bit to check if it take effect */
2712 * Check if dirtylimit take effect realistically, set the
2713 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2714 * doesn't take effect, fail test.
2716 while (--max_try_count
) {
2717 calc_dirty_rate(vm
, 1);
2718 wait_for_calc_dirtyrate_complete(vm
, 1);
2719 rate
= get_dirty_rate(vm
);
2722 * Assume hitting if current rate is less
2723 * than quota rate (within accepting error)
2725 if (rate
< (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2731 g_assert_cmpint(hit
, ==, 1);
2736 /* Check if dirtylimit cancellation take effect */
2737 cancel_vcpu_dirty_limit(vm
);
2738 while (--max_try_count
) {
2739 calc_dirty_rate(vm
, 1);
2740 wait_for_calc_dirtyrate_complete(vm
, 1);
2741 rate
= get_dirty_rate(vm
);
2744 * Assume dirtylimit be canceled if current rate is
2745 * greater than quota rate (within accepting error)
2747 if (rate
> (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2753 g_assert_cmpint(hit
, ==, 1);
2754 dirtylimit_stop_vm(vm
);
2757 static bool kvm_dirty_ring_supported(void)
2759 #if defined(__linux__) && defined(HOST_X86_64)
2760 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
2766 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
2769 /* We test with 4096 slots */
2780 int main(int argc
, char **argv
)
2782 bool has_kvm
, has_tcg
;
2785 g_autoptr(GError
) err
= NULL
;
2788 g_test_init(&argc
, &argv
, NULL
);
2790 has_kvm
= qtest_has_accel("kvm");
2791 has_tcg
= qtest_has_accel("tcg");
2793 if (!has_tcg
&& !has_kvm
) {
2794 g_test_skip("No KVM or TCG accelerator available");
2798 has_uffd
= ufd_version_check();
2799 arch
= qtest_get_arch();
2802 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2803 * is touchy due to race conditions on dirty bits (especially on PPC for
2806 if (g_str_equal(arch
, "ppc64") &&
2807 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
2808 g_test_message("Skipping test: kvm_hv not available");
2809 return g_test_run();
2813 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2814 * there until the problems are resolved
2816 if (g_str_equal(arch
, "s390x") && !has_kvm
) {
2817 g_test_message("Skipping test: s390x host with KVM is required");
2818 return g_test_run();
2821 tmpfs
= g_dir_make_tmp("migration-test-XXXXXX", &err
);
2823 g_test_message("Can't create temporary directory in %s: %s",
2824 g_get_tmp_dir(), err
->message
);
2827 bootfile_create(tmpfs
);
2829 module_call_init(MODULE_INIT_QOM
);
2832 qtest_add_func("/migration/postcopy/plain", test_postcopy
);
2833 qtest_add_func("/migration/postcopy/recovery/plain",
2834 test_postcopy_recovery
);
2835 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt
);
2836 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2837 test_postcopy_preempt_recovery
);
2838 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2839 qtest_add_func("/migration/postcopy/compress/plain",
2840 test_postcopy_compress
);
2841 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2842 test_postcopy_recovery_compress
);
2846 qtest_add_func("/migration/bad_dest", test_baddest
);
2847 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain
);
2848 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle
);
2850 * Compression fails from time to time.
2851 * Put test here but don't enable it until everything is fixed.
2853 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2854 qtest_add_func("/migration/precopy/unix/compress/wait",
2855 test_precopy_unix_compress
);
2856 qtest_add_func("/migration/precopy/unix/compress/nowait",
2857 test_precopy_unix_compress_nowait
);
2859 #ifdef CONFIG_GNUTLS
2860 qtest_add_func("/migration/precopy/unix/tls/psk",
2861 test_precopy_unix_tls_psk
);
2865 * NOTE: psk test is enough for postcopy, as other types of TLS
2866 * channels are tested under precopy. Here what we want to test is the
2867 * general postcopy path that has TLS channel enabled.
2869 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk
);
2870 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2871 test_postcopy_recovery_tls_psk
);
2872 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2873 test_postcopy_preempt_tls_psk
);
2874 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2875 test_postcopy_preempt_all
);
2878 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2879 test_precopy_unix_tls_x509_default_host
);
2880 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2881 test_precopy_unix_tls_x509_override_host
);
2882 #endif /* CONFIG_TASN1 */
2883 #endif /* CONFIG_GNUTLS */
2885 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain
);
2887 qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
2888 test_precopy_tcp_switchover_ack
);
2890 #ifdef CONFIG_GNUTLS
2891 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2892 test_precopy_tcp_tls_psk_match
);
2893 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2894 test_precopy_tcp_tls_psk_mismatch
);
2896 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2897 test_precopy_tcp_tls_x509_default_host
);
2898 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2899 test_precopy_tcp_tls_x509_override_host
);
2900 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2901 test_precopy_tcp_tls_x509_mismatch_host
);
2902 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2903 test_precopy_tcp_tls_x509_friendly_client
);
2904 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2905 test_precopy_tcp_tls_x509_hostile_client
);
2906 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2907 test_precopy_tcp_tls_x509_allow_anon_client
);
2908 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2909 test_precopy_tcp_tls_x509_reject_anon_client
);
2910 #endif /* CONFIG_TASN1 */
2911 #endif /* CONFIG_GNUTLS */
2913 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2915 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
2917 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
2918 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
2919 qtest_add_func("/migration/validate_uuid_src_not_set",
2920 test_validate_uuid_src_not_set
);
2921 qtest_add_func("/migration/validate_uuid_dst_not_set",
2922 test_validate_uuid_dst_not_set
);
2924 * See explanation why this test is slow on function definition
2926 if (g_test_slow()) {
2927 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
2929 qtest_add_func("/migration/multifd/tcp/plain/none",
2930 test_multifd_tcp_none
);
2932 * This test is flaky and sometimes fails in CI and otherwise:
2933 * don't run unless user opts in via environment variable.
2935 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2936 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2937 test_multifd_tcp_cancel
);
2939 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2940 test_multifd_tcp_zlib
);
2942 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2943 test_multifd_tcp_zstd
);
2945 #ifdef CONFIG_GNUTLS
2946 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2947 test_multifd_tcp_tls_psk_match
);
2948 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2949 test_multifd_tcp_tls_psk_mismatch
);
2951 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2952 test_multifd_tcp_tls_x509_default_host
);
2953 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2954 test_multifd_tcp_tls_x509_override_host
);
2955 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2956 test_multifd_tcp_tls_x509_mismatch_host
);
2957 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2958 test_multifd_tcp_tls_x509_allow_anon_client
);
2959 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2960 test_multifd_tcp_tls_x509_reject_anon_client
);
2961 #endif /* CONFIG_TASN1 */
2962 #endif /* CONFIG_GNUTLS */
2964 if (g_str_equal(arch
, "x86_64") && has_kvm
&& kvm_dirty_ring_supported()) {
2965 qtest_add_func("/migration/dirty_ring",
2966 test_precopy_unix_dirty_ring
);
2967 qtest_add_func("/migration/vcpu_dirty_limit",
2968 test_vcpu_dirty_limit
);
2973 g_assert_cmpint(ret
, ==, 0);
2978 g_test_message("unable to rmdir: path (%s): %s",
2979 tmpfs
, strerror(errno
));