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 * Dirtylimit stop working if dirty page rate error
51 * value less than DIRTYLIMIT_TOLERANCE_RANGE
53 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
55 #if defined(__linux__)
56 #include <sys/syscall.h>
60 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
61 #include <sys/eventfd.h>
62 #include <sys/ioctl.h>
63 #include "qemu/userfaultfd.h"
65 static bool ufd_version_check(void)
67 struct uffdio_api api_struct
;
70 int ufd
= uffd_open(O_CLOEXEC
);
73 g_test_message("Skipping test: userfaultfd not available");
77 api_struct
.api
= UFFD_API
;
78 api_struct
.features
= 0;
79 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
80 g_test_message("Skipping test: UFFDIO_API failed");
83 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
85 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
86 (__u64
)1 << _UFFDIO_UNREGISTER
;
87 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
88 g_test_message("Skipping test: Missing userfault feature");
96 static bool ufd_version_check(void)
98 g_test_message("Skipping test: Userfault not available (builtdtime)");
106 /* The boot file modifies memory area in [start_address, end_address)
107 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
109 #include "tests/migration/i386/a-b-bootblock.h"
110 #include "tests/migration/aarch64/a-b-kernel.h"
111 #include "tests/migration/s390x/a-b-bios.h"
113 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
115 FILE *bootfile
= fopen(bootpath
, "wb");
117 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
122 * Wait for some output in the serial output file,
123 * we get an 'A' followed by an endless string of 'B's
124 * but on the destination we won't have the A.
126 static void wait_for_serial(const char *side
)
128 g_autofree
char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
129 FILE *serialfile
= fopen(serialpath
, "r");
130 const char *arch
= qtest_get_arch();
131 int started
= (strcmp(side
, "src_serial") == 0 &&
132 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
135 int readvalue
= fgetc(serialfile
);
138 /* SLOF prints its banner before starting test,
139 * to ignore it, mark the start of the test with '_',
140 * ignore all characters until this marker
147 fseek(serialfile
, 0, SEEK_SET
);
164 started
= (strcmp(side
, "src_serial") == 0 &&
165 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
166 fseek(serialfile
, 0, SEEK_SET
);
171 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
172 g_assert_not_reached();
178 * It's tricky to use qemu's migration event capability with qtest,
179 * events suddenly appearing confuse the qmp()/hmp() responses.
182 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
184 QDict
*rsp_return
, *rsp_ram
;
187 rsp_return
= migrate_query_not_failed(who
);
188 if (!qdict_haskey(rsp_return
, "ram")) {
192 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
193 result
= qdict_get_try_int(rsp_ram
, property
, 0);
195 qobject_unref(rsp_return
);
199 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
204 rsp_return
= migrate_query_not_failed(who
);
205 result
= qdict_get_try_int(rsp_return
, property
, 0);
206 qobject_unref(rsp_return
);
210 static uint64_t get_migration_pass(QTestState
*who
)
212 return read_ram_property_int(who
, "dirty-sync-count");
215 static void read_blocktime(QTestState
*who
)
219 rsp_return
= migrate_query_not_failed(who
);
220 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
221 qobject_unref(rsp_return
);
224 static void wait_for_migration_pass(QTestState
*who
)
226 uint64_t initial_pass
= get_migration_pass(who
);
229 /* Wait for the 1st sync */
230 while (!got_src_stop
&& !initial_pass
) {
232 initial_pass
= get_migration_pass(who
);
237 pass
= get_migration_pass(who
);
238 } while (pass
== initial_pass
&& !got_src_stop
);
241 static void check_guests_ram(QTestState
*who
)
243 /* Our ASM test will have been incrementing one byte from each page from
244 * start_address to < end_address in order. This gives us a constraint
245 * that any page's byte should be equal or less than the previous pages
246 * byte (mod 256); and they should all be equal except for one transition
247 * at the point where we meet the incrementer. (We're running this with
248 * the guest stopped).
253 bool hit_edge
= false;
256 qtest_memread(who
, start_address
, &first_byte
, 1);
257 last_byte
= first_byte
;
259 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
260 address
+= TEST_MEM_PAGE_SIZE
)
263 qtest_memread(who
, address
, &b
, 1);
264 if (b
!= last_byte
) {
265 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
266 /* This is OK, the guest stopped at the point of
267 * incrementing the previous page but didn't get
275 fprintf(stderr
, "Memory content inconsistency at %x"
276 " first_byte = %x last_byte = %x current = %x"
278 address
, first_byte
, last_byte
, b
, hit_edge
);
284 fprintf(stderr
, "and in another %d pages", bad
- 10);
289 static void cleanup(const char *filename
)
291 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
296 static char *SocketAddress_to_str(SocketAddress
*addr
)
298 switch (addr
->type
) {
299 case SOCKET_ADDRESS_TYPE_INET
:
300 return g_strdup_printf("tcp:%s:%s",
303 case SOCKET_ADDRESS_TYPE_UNIX
:
304 return g_strdup_printf("unix:%s",
305 addr
->u
.q_unix
.path
);
306 case SOCKET_ADDRESS_TYPE_FD
:
307 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
308 case SOCKET_ADDRESS_TYPE_VSOCK
:
309 return g_strdup_printf("tcp:%s:%s",
313 return g_strdup("unknown address type");
317 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
321 SocketAddressList
*addrs
;
325 rsp
= migrate_query(who
);
326 object
= qdict_get(rsp
, parameter
);
328 iv
= qobject_input_visitor_new(object
);
329 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
332 /* we are only using a single address */
333 result
= SocketAddress_to_str(addrs
->value
);
335 qapi_free_SocketAddressList(addrs
);
340 static long long migrate_get_parameter_int(QTestState
*who
,
341 const char *parameter
)
346 rsp
= qtest_qmp_assert_success_ref(
347 who
, "{ 'execute': 'query-migrate-parameters' }");
348 result
= qdict_get_int(rsp
, parameter
);
353 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
358 result
= migrate_get_parameter_int(who
, parameter
);
359 g_assert_cmpint(result
, ==, value
);
362 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
365 qtest_qmp_assert_success(who
,
366 "{ 'execute': 'migrate-set-parameters',"
367 "'arguments': { %s: %lld } }",
369 migrate_check_parameter_int(who
, parameter
, value
);
372 static char *migrate_get_parameter_str(QTestState
*who
,
373 const char *parameter
)
378 rsp
= qtest_qmp_assert_success_ref(
379 who
, "{ 'execute': 'query-migrate-parameters' }");
380 result
= g_strdup(qdict_get_str(rsp
, parameter
));
385 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
388 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
389 g_assert_cmpstr(result
, ==, value
);
392 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
395 qtest_qmp_assert_success(who
,
396 "{ 'execute': 'migrate-set-parameters',"
397 "'arguments': { %s: %s } }",
399 migrate_check_parameter_str(who
, parameter
, value
);
402 static long long migrate_get_parameter_bool(QTestState
*who
,
403 const char *parameter
)
408 rsp
= qtest_qmp_assert_success_ref(
409 who
, "{ 'execute': 'query-migrate-parameters' }");
410 result
= qdict_get_bool(rsp
, parameter
);
415 static void migrate_check_parameter_bool(QTestState
*who
, const char *parameter
,
420 result
= migrate_get_parameter_bool(who
, parameter
);
421 g_assert_cmpint(result
, ==, value
);
424 static void migrate_set_parameter_bool(QTestState
*who
, const char *parameter
,
427 qtest_qmp_assert_success(who
,
428 "{ 'execute': 'migrate-set-parameters',"
429 "'arguments': { %s: %i } }",
431 migrate_check_parameter_bool(who
, parameter
, value
);
434 static void migrate_ensure_non_converge(QTestState
*who
)
436 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
437 migrate_set_parameter_int(who
, "max-bandwidth", 3 * 1000 * 1000);
438 migrate_set_parameter_int(who
, "downtime-limit", 1);
441 static void migrate_ensure_converge(QTestState
*who
)
443 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
444 migrate_set_parameter_int(who
, "max-bandwidth", 1 * 1000 * 1000 * 1000);
445 migrate_set_parameter_int(who
, "downtime-limit", 30 * 1000);
448 static void migrate_pause(QTestState
*who
)
450 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate-pause' }");
453 static void migrate_continue(QTestState
*who
, const char *state
)
455 qtest_qmp_assert_success(who
,
456 "{ 'execute': 'migrate-continue',"
457 " 'arguments': { 'state': %s } }",
461 static void migrate_recover(QTestState
*who
, const char *uri
)
463 qtest_qmp_assert_success(who
,
464 "{ 'execute': 'migrate-recover', "
465 " 'id': 'recover-cmd', "
466 " 'arguments': { 'uri': %s } }",
470 static void migrate_cancel(QTestState
*who
)
472 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate_cancel' }");
475 static void migrate_set_capability(QTestState
*who
, const char *capability
,
478 qtest_qmp_assert_success(who
,
479 "{ 'execute': 'migrate-set-capabilities',"
481 "'capabilities': [ { "
482 "'capability': %s, 'state': %i } ] } }",
486 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
488 qtest_qmp_assert_success(from
, "{ 'execute': 'migrate-start-postcopy' }");
491 qtest_qmp_eventwait(from
, "STOP");
494 qtest_qmp_eventwait(to
, "RESUME");
499 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
500 * unconditionally, because it means the user would like to be verbose.
504 /* only launch the target process */
506 /* Use dirty ring if true; dirty logging otherwise */
508 const char *opts_source
;
509 const char *opts_target
;
513 * A hook that runs after the src and dst QEMUs have been
514 * created, but before the migration is started. This can
515 * be used to set migration parameters and capabilities.
517 * Returns: NULL, or a pointer to opaque state to be
518 * later passed to the TestMigrateFinishHook
520 typedef void * (*TestMigrateStartHook
)(QTestState
*from
,
524 * A hook that runs after the migration has finished,
525 * regardless of whether it succeeded or failed, but
526 * before QEMU has terminated (unless it self-terminated
527 * due to migration error)
529 * @opaque is a pointer to state previously returned
530 * by the TestMigrateStartHook if any, or NULL.
532 typedef void (*TestMigrateFinishHook
)(QTestState
*from
,
537 /* Optional: fine tune start parameters */
540 /* Required: the URI for the dst QEMU to listen on */
541 const char *listen_uri
;
544 * Optional: the URI for the src QEMU to connect to
545 * If NULL, then it will query the dst QEMU for its actual
546 * listening address and use that as the connect address.
547 * This allows for dynamically picking a free TCP port.
549 const char *connect_uri
;
551 /* Optional: callback to run at start to set migration parameters */
552 TestMigrateStartHook start_hook
;
553 /* Optional: callback to run at finish to cleanup */
554 TestMigrateFinishHook finish_hook
;
557 * Optional: normally we expect the migration process to complete.
559 * There can be a variety of reasons and stages in which failure
560 * can happen during tests.
562 * If a failure is expected to happen at time of establishing
563 * the connection, then MIG_TEST_FAIL will indicate that the dst
564 * QEMU is expected to stay running and accept future migration
567 * If a failure is expected to happen while processing the
568 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
569 * that the dst QEMU is expected to quit with non-zero exit status
572 /* This test should succeed, the default */
573 MIG_TEST_SUCCEED
= 0,
574 /* This test should fail, dest qemu should keep alive */
576 /* This test should fail, dest qemu should fail with abnormal status */
577 MIG_TEST_FAIL_DEST_QUIT_ERR
,
580 /* Optional: set number of migration passes to wait for, if live==true */
581 unsigned int iterations
;
584 * Optional: whether the guest CPUs should be running during a precopy
585 * migration test. We used to always run with live but it took much
586 * longer so we reduced live tests to only the ones that have solid
587 * reason to be tested live-only. For each of the new test cases for
588 * precopy please provide justifications to use live explicitly (please
589 * refer to existing ones with live=true), or use live=off by default.
593 /* Postcopy specific fields */
595 bool postcopy_preempt
;
598 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
599 const char *uri
, MigrateStart
*args
)
601 g_autofree gchar
*arch_source
= NULL
;
602 g_autofree gchar
*arch_target
= NULL
;
603 g_autofree gchar
*cmd_source
= NULL
;
604 g_autofree gchar
*cmd_target
= NULL
;
605 const gchar
*ignore_stderr
;
606 g_autofree
char *bootpath
= NULL
;
607 g_autofree
char *shmem_opts
= NULL
;
608 g_autofree
char *shmem_path
= NULL
;
609 const char *arch
= qtest_get_arch();
610 const char *machine_opts
= NULL
;
611 const char *memory_size
;
613 if (args
->use_shmem
) {
614 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
615 g_test_skip("/dev/shm is not supported");
620 got_src_stop
= false;
621 got_dst_resume
= false;
622 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
623 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
624 /* the assembled x86 boot sector should be exactly one sector large */
625 assert(sizeof(x86_bootsect
) == 512);
626 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
627 memory_size
= "150M";
628 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
629 arch_target
= g_strdup(arch_source
);
630 start_address
= X86_TEST_MEM_START
;
631 end_address
= X86_TEST_MEM_END
;
632 } else if (g_str_equal(arch
, "s390x")) {
633 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
634 memory_size
= "128M";
635 arch_source
= g_strdup_printf("-bios %s", bootpath
);
636 arch_target
= g_strdup(arch_source
);
637 start_address
= S390_TEST_MEM_START
;
638 end_address
= S390_TEST_MEM_END
;
639 } else if (strcmp(arch
, "ppc64") == 0) {
640 machine_opts
= "vsmt=8";
641 memory_size
= "256M";
642 start_address
= PPC_TEST_MEM_START
;
643 end_address
= PPC_TEST_MEM_END
;
644 arch_source
= g_strdup_printf("-nodefaults "
645 "-prom-env 'use-nvramrc?=true' -prom-env "
646 "'nvramrc=hex .\" _\" begin %x %x "
647 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
648 "until'", end_address
, start_address
);
649 arch_target
= g_strdup("");
650 } else if (strcmp(arch
, "aarch64") == 0) {
651 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
652 machine_opts
= "virt,gic-version=max";
653 memory_size
= "150M";
654 arch_source
= g_strdup_printf("-cpu max "
657 arch_target
= g_strdup(arch_source
);
658 start_address
= ARM_TEST_MEM_START
;
659 end_address
= ARM_TEST_MEM_END
;
661 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
663 g_assert_not_reached();
666 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
668 ignore_stderr
= "2>/dev/null";
671 * On Windows the QEMU executable is created via CreateProcess() and
672 * IO redirection does not work, so don't bother adding IO redirection
673 * to the command line.
681 if (args
->use_shmem
) {
682 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
683 shmem_opts
= g_strdup_printf(
684 "-object memory-backend-file,id=mem0,size=%s"
685 ",mem-path=%s,share=on -numa node,memdev=mem0",
686 memory_size
, shmem_path
);
689 shmem_opts
= g_strdup("");
692 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg%s%s "
693 "-name source,debug-threads=on "
695 "-serial file:%s/src_serial "
697 args
->use_dirty_ring
?
698 ",dirty-ring-size=4096" : "",
699 machine_opts
? " -machine " : "",
700 machine_opts
? machine_opts
: "",
702 arch_source
, shmem_opts
,
703 args
->opts_source
? args
->opts_source
: "",
705 if (!args
->only_target
) {
706 *from
= qtest_init(cmd_source
);
707 qtest_qmp_set_event_callback(*from
,
708 migrate_watch_for_stop
,
712 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg%s%s "
713 "-name target,debug-threads=on "
715 "-serial file:%s/dest_serial "
718 args
->use_dirty_ring
?
719 ",dirty-ring-size=4096" : "",
720 machine_opts
? " -machine " : "",
721 machine_opts
? machine_opts
: "",
722 memory_size
, tmpfs
, uri
,
723 arch_target
, shmem_opts
,
724 args
->opts_target
? args
->opts_target
: "",
726 *to
= qtest_init(cmd_target
);
727 qtest_qmp_set_event_callback(*to
,
728 migrate_watch_for_resume
,
732 * Remove shmem file immediately to avoid memory leak in test failed case.
733 * It's valid becase QEMU has already opened this file
735 if (args
->use_shmem
) {
742 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
744 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
749 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
751 /* Destination still running, wait for a byte to change */
753 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
755 } while (dest_byte_a
== dest_byte_b
);
757 qtest_qmp_assert_success(to
, "{ 'execute' : 'stop'}");
759 /* With it stopped, check nothing changes */
760 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
762 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
763 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
765 check_guests_ram(to
);
771 cleanup("migsocket");
772 cleanup("src_serial");
773 cleanup("dest_serial");
777 struct TestMigrateTLSPSKData
{
785 test_migrate_tls_psk_start_common(QTestState
*from
,
789 struct TestMigrateTLSPSKData
*data
=
790 g_new0(struct TestMigrateTLSPSKData
, 1);
792 data
->workdir
= g_strdup_printf("%s/tlscredspsk0", tmpfs
);
793 data
->pskfile
= g_strdup_printf("%s/%s", data
->workdir
,
794 QCRYPTO_TLS_CREDS_PSKFILE
);
795 g_mkdir_with_parents(data
->workdir
, 0700);
796 test_tls_psk_init(data
->pskfile
);
799 data
->workdiralt
= g_strdup_printf("%s/tlscredspskalt0", tmpfs
);
800 data
->pskfilealt
= g_strdup_printf("%s/%s", data
->workdiralt
,
801 QCRYPTO_TLS_CREDS_PSKFILE
);
802 g_mkdir_with_parents(data
->workdiralt
, 0700);
803 test_tls_psk_init_alt(data
->pskfilealt
);
806 qtest_qmp_assert_success(from
,
807 "{ 'execute': 'object-add',"
808 " 'arguments': { 'qom-type': 'tls-creds-psk',"
809 " 'id': 'tlscredspsk0',"
810 " 'endpoint': 'client',"
812 " 'username': 'qemu'} }",
815 qtest_qmp_assert_success(to
,
816 "{ 'execute': 'object-add',"
817 " 'arguments': { 'qom-type': 'tls-creds-psk',"
818 " 'id': 'tlscredspsk0',"
819 " 'endpoint': 'server',"
821 mismatch
? data
->workdiralt
: data
->workdir
);
823 migrate_set_parameter_str(from
, "tls-creds", "tlscredspsk0");
824 migrate_set_parameter_str(to
, "tls-creds", "tlscredspsk0");
830 test_migrate_tls_psk_start_match(QTestState
*from
,
833 return test_migrate_tls_psk_start_common(from
, to
, false);
837 test_migrate_tls_psk_start_mismatch(QTestState
*from
,
840 return test_migrate_tls_psk_start_common(from
, to
, true);
844 test_migrate_tls_psk_finish(QTestState
*from
,
848 struct TestMigrateTLSPSKData
*data
= opaque
;
850 test_tls_psk_cleanup(data
->pskfile
);
851 if (data
->pskfilealt
) {
852 test_tls_psk_cleanup(data
->pskfilealt
);
854 rmdir(data
->workdir
);
855 if (data
->workdiralt
) {
856 rmdir(data
->workdiralt
);
859 g_free(data
->workdiralt
);
860 g_free(data
->pskfilealt
);
861 g_free(data
->workdir
);
862 g_free(data
->pskfile
);
875 } TestMigrateTLSX509Data
;
882 const char *certhostname
;
883 const char *certipaddr
;
884 } TestMigrateTLSX509
;
887 test_migrate_tls_x509_start_common(QTestState
*from
,
889 TestMigrateTLSX509
*args
)
891 TestMigrateTLSX509Data
*data
= g_new0(TestMigrateTLSX509Data
, 1);
893 data
->workdir
= g_strdup_printf("%s/tlscredsx5090", tmpfs
);
894 data
->keyfile
= g_strdup_printf("%s/key.pem", data
->workdir
);
896 data
->cacert
= g_strdup_printf("%s/ca-cert.pem", data
->workdir
);
897 data
->serverkey
= g_strdup_printf("%s/server-key.pem", data
->workdir
);
898 data
->servercert
= g_strdup_printf("%s/server-cert.pem", data
->workdir
);
899 if (args
->clientcert
) {
900 data
->clientkey
= g_strdup_printf("%s/client-key.pem", data
->workdir
);
901 data
->clientcert
= g_strdup_printf("%s/client-cert.pem", data
->workdir
);
904 g_mkdir_with_parents(data
->workdir
, 0700);
906 test_tls_init(data
->keyfile
);
908 g_assert(link(data
->keyfile
, data
->serverkey
) == 0);
910 g_assert(CreateHardLink(data
->serverkey
, data
->keyfile
, NULL
) != 0);
912 if (args
->clientcert
) {
914 g_assert(link(data
->keyfile
, data
->clientkey
) == 0);
916 g_assert(CreateHardLink(data
->clientkey
, data
->keyfile
, NULL
) != 0);
920 TLS_ROOT_REQ_SIMPLE(cacertreq
, data
->cacert
);
921 if (args
->clientcert
) {
922 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq
, cacertreq
,
923 args
->hostileclient
?
924 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME
:
925 QCRYPTO_TLS_TEST_CLIENT_NAME
,
929 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq
, cacertreq
,
934 qtest_qmp_assert_success(from
,
935 "{ 'execute': 'object-add',"
936 " 'arguments': { 'qom-type': 'tls-creds-x509',"
937 " 'id': 'tlscredsx509client0',"
938 " 'endpoint': 'client',"
940 " 'sanity-check': true,"
941 " 'verify-peer': true} }",
943 migrate_set_parameter_str(from
, "tls-creds", "tlscredsx509client0");
944 if (args
->certhostname
) {
945 migrate_set_parameter_str(from
, "tls-hostname", args
->certhostname
);
948 qtest_qmp_assert_success(to
,
949 "{ 'execute': 'object-add',"
950 " 'arguments': { 'qom-type': 'tls-creds-x509',"
951 " 'id': 'tlscredsx509server0',"
952 " 'endpoint': 'server',"
954 " 'sanity-check': true,"
955 " 'verify-peer': %i} }",
956 data
->workdir
, args
->verifyclient
);
957 migrate_set_parameter_str(to
, "tls-creds", "tlscredsx509server0");
959 if (args
->authzclient
) {
960 qtest_qmp_assert_success(to
,
961 "{ 'execute': 'object-add',"
962 " 'arguments': { 'qom-type': 'authz-simple',"
963 " 'id': 'tlsauthz0',"
964 " 'identity': %s} }",
965 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME
);
966 migrate_set_parameter_str(to
, "tls-authz", "tlsauthz0");
973 * The normal case: match server's cert hostname against
974 * whatever host we were telling QEMU to connect to (if any)
977 test_migrate_tls_x509_start_default_host(QTestState
*from
,
980 TestMigrateTLSX509 args
= {
981 .verifyclient
= true,
983 .certipaddr
= "127.0.0.1"
985 return test_migrate_tls_x509_start_common(from
, to
, &args
);
989 * The unusual case: the server's cert is different from
990 * the address we're telling QEMU to connect to (if any),
991 * so we must give QEMU an explicit hostname to validate
994 test_migrate_tls_x509_start_override_host(QTestState
*from
,
997 TestMigrateTLSX509 args
= {
998 .verifyclient
= true,
1000 .certhostname
= "qemu.org",
1002 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1006 * The unusual case: the server's cert is different from
1007 * the address we're telling QEMU to connect to, and so we
1008 * expect the client to reject the server
1011 test_migrate_tls_x509_start_mismatch_host(QTestState
*from
,
1014 TestMigrateTLSX509 args
= {
1015 .verifyclient
= true,
1017 .certipaddr
= "10.0.0.1",
1019 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1023 test_migrate_tls_x509_start_friendly_client(QTestState
*from
,
1026 TestMigrateTLSX509 args
= {
1027 .verifyclient
= true,
1029 .authzclient
= true,
1030 .certipaddr
= "127.0.0.1",
1032 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1036 test_migrate_tls_x509_start_hostile_client(QTestState
*from
,
1039 TestMigrateTLSX509 args
= {
1040 .verifyclient
= true,
1042 .hostileclient
= true,
1043 .authzclient
= true,
1044 .certipaddr
= "127.0.0.1",
1046 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1050 * The case with no client certificate presented,
1051 * and no server verification
1054 test_migrate_tls_x509_start_allow_anon_client(QTestState
*from
,
1057 TestMigrateTLSX509 args
= {
1058 .certipaddr
= "127.0.0.1",
1060 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1064 * The case with no client certificate presented,
1065 * and server verification rejecting
1068 test_migrate_tls_x509_start_reject_anon_client(QTestState
*from
,
1071 TestMigrateTLSX509 args
= {
1072 .verifyclient
= true,
1073 .certipaddr
= "127.0.0.1",
1075 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1079 test_migrate_tls_x509_finish(QTestState
*from
,
1083 TestMigrateTLSX509Data
*data
= opaque
;
1085 test_tls_cleanup(data
->keyfile
);
1086 g_free(data
->keyfile
);
1088 unlink(data
->cacert
);
1089 g_free(data
->cacert
);
1090 unlink(data
->servercert
);
1091 g_free(data
->servercert
);
1092 unlink(data
->serverkey
);
1093 g_free(data
->serverkey
);
1095 if (data
->clientcert
) {
1096 unlink(data
->clientcert
);
1097 g_free(data
->clientcert
);
1099 if (data
->clientkey
) {
1100 unlink(data
->clientkey
);
1101 g_free(data
->clientkey
);
1104 rmdir(data
->workdir
);
1105 g_free(data
->workdir
);
1109 #endif /* CONFIG_TASN1 */
1110 #endif /* CONFIG_GNUTLS */
1113 test_migrate_compress_start(QTestState
*from
,
1116 migrate_set_parameter_int(from
, "compress-level", 1);
1117 migrate_set_parameter_int(from
, "compress-threads", 4);
1118 migrate_set_parameter_bool(from
, "compress-wait-thread", true);
1119 migrate_set_parameter_int(to
, "decompress-threads", 4);
1121 migrate_set_capability(from
, "compress", true);
1122 migrate_set_capability(to
, "compress", true);
1128 test_migrate_compress_nowait_start(QTestState
*from
,
1131 migrate_set_parameter_int(from
, "compress-level", 9);
1132 migrate_set_parameter_int(from
, "compress-threads", 1);
1133 migrate_set_parameter_bool(from
, "compress-wait-thread", false);
1134 migrate_set_parameter_int(to
, "decompress-threads", 1);
1136 migrate_set_capability(from
, "compress", true);
1137 migrate_set_capability(to
, "compress", true);
1142 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
1143 QTestState
**to_ptr
,
1144 MigrateCommon
*args
)
1146 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1147 QTestState
*from
, *to
;
1149 if (test_migrate_start(&from
, &to
, uri
, &args
->start
)) {
1153 if (args
->start_hook
) {
1154 args
->postcopy_data
= args
->start_hook(from
, to
);
1157 migrate_set_capability(from
, "postcopy-ram", true);
1158 migrate_set_capability(to
, "postcopy-ram", true);
1159 migrate_set_capability(to
, "postcopy-blocktime", true);
1161 if (args
->postcopy_preempt
) {
1162 migrate_set_capability(from
, "postcopy-preempt", true);
1163 migrate_set_capability(to
, "postcopy-preempt", true);
1166 migrate_ensure_non_converge(from
);
1168 /* Wait for the first serial output from the source */
1169 wait_for_serial("src_serial");
1171 migrate_qmp(from
, uri
, "{}");
1173 wait_for_migration_pass(from
);
1181 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
,
1182 MigrateCommon
*args
)
1184 wait_for_migration_complete(from
);
1186 /* Make sure we get at least one "B" on destination */
1187 wait_for_serial("dest_serial");
1189 if (uffd_feature_thread_id
) {
1193 if (args
->finish_hook
) {
1194 args
->finish_hook(from
, to
, args
->postcopy_data
);
1195 args
->postcopy_data
= NULL
;
1198 test_migrate_end(from
, to
, true);
1201 static void test_postcopy_common(MigrateCommon
*args
)
1203 QTestState
*from
, *to
;
1205 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1208 migrate_postcopy_start(from
, to
);
1209 migrate_postcopy_complete(from
, to
, args
);
1212 static void test_postcopy(void)
1214 MigrateCommon args
= { };
1216 test_postcopy_common(&args
);
1219 static void test_postcopy_compress(void)
1221 MigrateCommon args
= {
1222 .start_hook
= test_migrate_compress_start
1225 test_postcopy_common(&args
);
1228 static void test_postcopy_preempt(void)
1230 MigrateCommon args
= {
1231 .postcopy_preempt
= true,
1234 test_postcopy_common(&args
);
1237 #ifdef CONFIG_GNUTLS
1238 static void test_postcopy_tls_psk(void)
1240 MigrateCommon args
= {
1241 .start_hook
= test_migrate_tls_psk_start_match
,
1242 .finish_hook
= test_migrate_tls_psk_finish
,
1245 test_postcopy_common(&args
);
1248 static void test_postcopy_preempt_tls_psk(void)
1250 MigrateCommon args
= {
1251 .postcopy_preempt
= true,
1252 .start_hook
= test_migrate_tls_psk_start_match
,
1253 .finish_hook
= test_migrate_tls_psk_finish
,
1256 test_postcopy_common(&args
);
1260 static void test_postcopy_recovery_common(MigrateCommon
*args
)
1262 QTestState
*from
, *to
;
1263 g_autofree
char *uri
= NULL
;
1265 /* Always hide errors for postcopy recover tests since they're expected */
1266 args
->start
.hide_stderr
= true;
1268 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1272 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1273 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
1275 /* Now we start the postcopy */
1276 migrate_postcopy_start(from
, to
);
1279 * Wait until postcopy is really started; we can only run the
1280 * migrate-pause command during a postcopy
1282 wait_for_migration_status(from
, "postcopy-active", NULL
);
1285 * Manually stop the postcopy migration. This emulates a network
1286 * failure with the migration socket
1288 migrate_pause(from
);
1291 * Wait for destination side to reach postcopy-paused state. The
1292 * migrate-recover command can only succeed if destination machine
1293 * is in the paused state
1295 wait_for_migration_status(to
, "postcopy-paused",
1296 (const char * []) { "failed", "active",
1297 "completed", NULL
});
1300 * Create a new socket to emulate a new channel that is different
1301 * from the broken migration channel; tell the destination to
1302 * listen to the new port
1304 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
1305 migrate_recover(to
, uri
);
1308 * Try to rebuild the migration channel using the resume flag and
1309 * the newly created channel
1311 wait_for_migration_status(from
, "postcopy-paused",
1312 (const char * []) { "failed", "active",
1313 "completed", NULL
});
1314 migrate_qmp(from
, uri
, "{'resume': true}");
1316 /* Restore the postcopy bandwidth to unlimited */
1317 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
1319 migrate_postcopy_complete(from
, to
, args
);
1322 static void test_postcopy_recovery(void)
1324 MigrateCommon args
= { };
1326 test_postcopy_recovery_common(&args
);
1329 static void test_postcopy_recovery_compress(void)
1331 MigrateCommon args
= {
1332 .start_hook
= test_migrate_compress_start
1335 test_postcopy_recovery_common(&args
);
1338 #ifdef CONFIG_GNUTLS
1339 static void test_postcopy_recovery_tls_psk(void)
1341 MigrateCommon args
= {
1342 .start_hook
= test_migrate_tls_psk_start_match
,
1343 .finish_hook
= test_migrate_tls_psk_finish
,
1346 test_postcopy_recovery_common(&args
);
1350 static void test_postcopy_preempt_recovery(void)
1352 MigrateCommon args
= {
1353 .postcopy_preempt
= true,
1356 test_postcopy_recovery_common(&args
);
1359 #ifdef CONFIG_GNUTLS
1360 /* This contains preempt+recovery+tls test altogether */
1361 static void test_postcopy_preempt_all(void)
1363 MigrateCommon args
= {
1364 .postcopy_preempt
= true,
1365 .start_hook
= test_migrate_tls_psk_start_match
,
1366 .finish_hook
= test_migrate_tls_psk_finish
,
1369 test_postcopy_recovery_common(&args
);
1374 static void test_baddest(void)
1376 MigrateStart args
= {
1379 QTestState
*from
, *to
;
1381 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1384 migrate_qmp(from
, "tcp:127.0.0.1:0", "{}");
1385 wait_for_migration_fail(from
, false);
1386 test_migrate_end(from
, to
, false);
1389 static void test_precopy_common(MigrateCommon
*args
)
1391 QTestState
*from
, *to
;
1392 void *data_hook
= NULL
;
1394 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1398 if (args
->start_hook
) {
1399 data_hook
= args
->start_hook(from
, to
);
1402 /* Wait for the first serial output from the source */
1403 if (args
->result
== MIG_TEST_SUCCEED
) {
1404 wait_for_serial("src_serial");
1409 * Testing live migration, we want to ensure that some
1410 * memory is re-dirtied after being transferred, so that
1411 * we exercise logic for dirty page handling. We achieve
1412 * this with a ridiculosly low bandwidth that guarantees
1415 migrate_ensure_non_converge(from
);
1418 * Testing non-live migration, we allow it to run at
1419 * full speed to ensure short test case duration.
1420 * For tests expected to fail, we don't need to
1423 if (args
->result
== MIG_TEST_SUCCEED
) {
1424 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1425 if (!got_src_stop
) {
1426 qtest_qmp_eventwait(from
, "STOP");
1428 migrate_ensure_converge(from
);
1432 if (!args
->connect_uri
) {
1433 g_autofree
char *local_connect_uri
=
1434 migrate_get_socket_address(to
, "socket-address");
1435 migrate_qmp(from
, local_connect_uri
, "{}");
1437 migrate_qmp(from
, args
->connect_uri
, "{}");
1441 if (args
->result
!= MIG_TEST_SUCCEED
) {
1442 bool allow_active
= args
->result
== MIG_TEST_FAIL
;
1443 wait_for_migration_fail(from
, allow_active
);
1445 if (args
->result
== MIG_TEST_FAIL_DEST_QUIT_ERR
) {
1446 qtest_set_expected_status(to
, EXIT_FAILURE
);
1450 if (args
->iterations
) {
1451 while (args
->iterations
--) {
1452 wait_for_migration_pass(from
);
1455 wait_for_migration_pass(from
);
1458 migrate_ensure_converge(from
);
1461 * We do this first, as it has a timeout to stop us
1462 * hanging forever if migration didn't converge
1464 wait_for_migration_complete(from
);
1466 if (!got_src_stop
) {
1467 qtest_qmp_eventwait(from
, "STOP");
1470 wait_for_migration_complete(from
);
1472 * Must wait for dst to finish reading all incoming
1473 * data on the socket before issuing 'cont' otherwise
1476 wait_for_migration_complete(to
);
1478 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1481 if (!got_dst_resume
) {
1482 qtest_qmp_eventwait(to
, "RESUME");
1485 wait_for_serial("dest_serial");
1488 if (args
->finish_hook
) {
1489 args
->finish_hook(from
, to
, data_hook
);
1492 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1495 static void test_precopy_unix_plain(void)
1497 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1498 MigrateCommon args
= {
1502 * The simplest use case of precopy, covering smoke tests of
1503 * get-dirty-log dirty tracking.
1508 test_precopy_common(&args
);
1512 static void test_precopy_unix_dirty_ring(void)
1514 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1515 MigrateCommon args
= {
1517 .use_dirty_ring
= true,
1522 * Besides the precopy/unix basic test, cover dirty ring interface
1523 * rather than get-dirty-log.
1528 test_precopy_common(&args
);
1531 #ifdef CONFIG_GNUTLS
1532 static void test_precopy_unix_tls_psk(void)
1534 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1535 MigrateCommon args
= {
1538 .start_hook
= test_migrate_tls_psk_start_match
,
1539 .finish_hook
= test_migrate_tls_psk_finish
,
1542 test_precopy_common(&args
);
1546 static void test_precopy_unix_tls_x509_default_host(void)
1548 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1549 MigrateCommon args
= {
1551 .hide_stderr
= true,
1555 .start_hook
= test_migrate_tls_x509_start_default_host
,
1556 .finish_hook
= test_migrate_tls_x509_finish
,
1557 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1560 test_precopy_common(&args
);
1563 static void test_precopy_unix_tls_x509_override_host(void)
1565 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1566 MigrateCommon args
= {
1569 .start_hook
= test_migrate_tls_x509_start_override_host
,
1570 .finish_hook
= test_migrate_tls_x509_finish
,
1573 test_precopy_common(&args
);
1575 #endif /* CONFIG_TASN1 */
1576 #endif /* CONFIG_GNUTLS */
1579 /* Currently upset on aarch64 TCG */
1580 static void test_ignore_shared(void)
1582 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1583 QTestState
*from
, *to
;
1585 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
1589 migrate_set_capability(from
, "x-ignore-shared", true);
1590 migrate_set_capability(to
, "x-ignore-shared", true);
1592 /* Wait for the first serial output from the source */
1593 wait_for_serial("src_serial");
1595 migrate_qmp(from
, uri
, "{}");
1597 wait_for_migration_pass(from
);
1599 if (!got_src_stop
) {
1600 qtest_qmp_eventwait(from
, "STOP");
1603 qtest_qmp_eventwait(to
, "RESUME");
1605 wait_for_serial("dest_serial");
1606 wait_for_migration_complete(from
);
1608 /* Check whether shared RAM has been really skipped */
1609 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1611 test_migrate_end(from
, to
, true);
1616 test_migrate_xbzrle_start(QTestState
*from
,
1619 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1621 migrate_set_capability(from
, "xbzrle", true);
1622 migrate_set_capability(to
, "xbzrle", true);
1627 static void test_precopy_unix_xbzrle(void)
1629 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1630 MigrateCommon args
= {
1633 .start_hook
= test_migrate_xbzrle_start
,
1636 * XBZRLE needs pages to be modified when doing the 2nd+ round
1637 * iteration to have real data pushed to the stream.
1642 test_precopy_common(&args
);
1645 static void test_precopy_unix_compress(void)
1647 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1648 MigrateCommon args
= {
1651 .start_hook
= test_migrate_compress_start
,
1653 * Test that no invalid thread state is left over from
1654 * the previous iteration.
1658 * We make sure the compressor can always work well even if guest
1659 * memory is changing. See commit 34ab9e9743 where we used to fix
1660 * a bug when only trigger-able with guest memory changing.
1665 test_precopy_common(&args
);
1668 static void test_precopy_unix_compress_nowait(void)
1670 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1671 MigrateCommon args
= {
1674 .start_hook
= test_migrate_compress_nowait_start
,
1676 * Test that no invalid thread state is left over from
1677 * the previous iteration.
1680 /* Same reason for the wait version of precopy compress test */
1684 test_precopy_common(&args
);
1687 static void test_precopy_tcp_plain(void)
1689 MigrateCommon args
= {
1690 .listen_uri
= "tcp:127.0.0.1:0",
1693 test_precopy_common(&args
);
1696 #ifdef CONFIG_GNUTLS
1697 static void test_precopy_tcp_tls_psk_match(void)
1699 MigrateCommon args
= {
1700 .listen_uri
= "tcp:127.0.0.1:0",
1701 .start_hook
= test_migrate_tls_psk_start_match
,
1702 .finish_hook
= test_migrate_tls_psk_finish
,
1705 test_precopy_common(&args
);
1708 static void test_precopy_tcp_tls_psk_mismatch(void)
1710 MigrateCommon args
= {
1712 .hide_stderr
= true,
1714 .listen_uri
= "tcp:127.0.0.1:0",
1715 .start_hook
= test_migrate_tls_psk_start_mismatch
,
1716 .finish_hook
= test_migrate_tls_psk_finish
,
1717 .result
= MIG_TEST_FAIL
,
1720 test_precopy_common(&args
);
1724 static void test_precopy_tcp_tls_x509_default_host(void)
1726 MigrateCommon args
= {
1727 .listen_uri
= "tcp:127.0.0.1:0",
1728 .start_hook
= test_migrate_tls_x509_start_default_host
,
1729 .finish_hook
= test_migrate_tls_x509_finish
,
1732 test_precopy_common(&args
);
1735 static void test_precopy_tcp_tls_x509_override_host(void)
1737 MigrateCommon args
= {
1738 .listen_uri
= "tcp:127.0.0.1:0",
1739 .start_hook
= test_migrate_tls_x509_start_override_host
,
1740 .finish_hook
= test_migrate_tls_x509_finish
,
1743 test_precopy_common(&args
);
1746 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1748 MigrateCommon args
= {
1750 .hide_stderr
= true,
1752 .listen_uri
= "tcp:127.0.0.1:0",
1753 .start_hook
= test_migrate_tls_x509_start_mismatch_host
,
1754 .finish_hook
= test_migrate_tls_x509_finish
,
1755 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1758 test_precopy_common(&args
);
1761 static void test_precopy_tcp_tls_x509_friendly_client(void)
1763 MigrateCommon args
= {
1764 .listen_uri
= "tcp:127.0.0.1:0",
1765 .start_hook
= test_migrate_tls_x509_start_friendly_client
,
1766 .finish_hook
= test_migrate_tls_x509_finish
,
1769 test_precopy_common(&args
);
1772 static void test_precopy_tcp_tls_x509_hostile_client(void)
1774 MigrateCommon args
= {
1776 .hide_stderr
= true,
1778 .listen_uri
= "tcp:127.0.0.1:0",
1779 .start_hook
= test_migrate_tls_x509_start_hostile_client
,
1780 .finish_hook
= test_migrate_tls_x509_finish
,
1781 .result
= MIG_TEST_FAIL
,
1784 test_precopy_common(&args
);
1787 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1789 MigrateCommon args
= {
1790 .listen_uri
= "tcp:127.0.0.1:0",
1791 .start_hook
= test_migrate_tls_x509_start_allow_anon_client
,
1792 .finish_hook
= test_migrate_tls_x509_finish
,
1795 test_precopy_common(&args
);
1798 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1800 MigrateCommon args
= {
1802 .hide_stderr
= true,
1804 .listen_uri
= "tcp:127.0.0.1:0",
1805 .start_hook
= test_migrate_tls_x509_start_reject_anon_client
,
1806 .finish_hook
= test_migrate_tls_x509_finish
,
1807 .result
= MIG_TEST_FAIL
,
1810 test_precopy_common(&args
);
1812 #endif /* CONFIG_TASN1 */
1813 #endif /* CONFIG_GNUTLS */
1816 static void *test_migrate_fd_start_hook(QTestState
*from
,
1822 /* Create two connected sockets for migration */
1823 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1824 g_assert_cmpint(ret
, ==, 0);
1826 /* Send the 1st socket to the target */
1827 qtest_qmp_fds_assert_success(to
, &pair
[0], 1,
1828 "{ 'execute': 'getfd',"
1829 " 'arguments': { 'fdname': 'fd-mig' }}");
1832 /* Start incoming migration from the 1st socket */
1833 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1834 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1836 /* Send the 2nd socket to the target */
1837 qtest_qmp_fds_assert_success(from
, &pair
[1], 1,
1838 "{ 'execute': 'getfd',"
1839 " 'arguments': { 'fdname': 'fd-mig' }}");
1845 static void test_migrate_fd_finish_hook(QTestState
*from
,
1850 const char *error_desc
;
1852 /* Test closing fds */
1853 /* We assume, that QEMU removes named fd from its list,
1854 * so this should fail */
1855 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1856 " 'arguments': { 'fdname': 'fd-mig' }}");
1857 g_assert_true(qdict_haskey(rsp
, "error"));
1858 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1859 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1862 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1863 " 'arguments': { 'fdname': 'fd-mig' }}");
1864 g_assert_true(qdict_haskey(rsp
, "error"));
1865 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1866 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1870 static void test_migrate_fd_proto(void)
1872 MigrateCommon args
= {
1873 .listen_uri
= "defer",
1874 .connect_uri
= "fd:fd-mig",
1875 .start_hook
= test_migrate_fd_start_hook
,
1876 .finish_hook
= test_migrate_fd_finish_hook
1878 test_precopy_common(&args
);
1882 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1884 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1885 QTestState
*from
, *to
;
1887 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1892 * UUID validation is at the begin of migration. So, the main process of
1893 * migration is not interesting for us here. Thus, set huge downtime for
1894 * very fast migration.
1896 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1897 migrate_set_capability(from
, "validate-uuid", true);
1899 /* Wait for the first serial output from the source */
1900 wait_for_serial("src_serial");
1902 migrate_qmp(from
, uri
, "{}");
1905 qtest_set_expected_status(to
, EXIT_FAILURE
);
1906 wait_for_migration_fail(from
, true);
1908 wait_for_migration_complete(from
);
1911 test_migrate_end(from
, to
, false);
1914 static void test_validate_uuid(void)
1916 MigrateStart args
= {
1917 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
1918 .opts_target
= "-uuid 11111111-1111-1111-1111-111111111111",
1921 do_test_validate_uuid(&args
, false);
1924 static void test_validate_uuid_error(void)
1926 MigrateStart args
= {
1927 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
1928 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
1929 .hide_stderr
= true,
1932 do_test_validate_uuid(&args
, true);
1935 static void test_validate_uuid_src_not_set(void)
1937 MigrateStart args
= {
1938 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
1939 .hide_stderr
= true,
1942 do_test_validate_uuid(&args
, false);
1945 static void test_validate_uuid_dst_not_set(void)
1947 MigrateStart args
= {
1948 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
1949 .hide_stderr
= true,
1952 do_test_validate_uuid(&args
, false);
1956 * The way auto_converge works, we need to do too many passes to
1957 * run this test. Auto_converge logic is only run once every
1958 * three iterations, so:
1960 * - 3 iterations without auto_converge enabled
1961 * - 3 iterations with pct = 5
1962 * - 3 iterations with pct = 30
1963 * - 3 iterations with pct = 55
1964 * - 3 iterations with pct = 80
1965 * - 3 iterations with pct = 95 (max(95, 80 + 25))
1967 * To make things even worse, we need to run the initial stage at
1968 * 3MB/s so we enter autoconverge even when host is (over)loaded.
1970 static void test_migrate_auto_converge(void)
1972 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1973 MigrateStart args
= {};
1974 QTestState
*from
, *to
;
1978 * We want the test to be stable and as fast as possible.
1979 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1980 * so we need to decrease a bandwidth.
1982 const int64_t init_pct
= 5, inc_pct
= 25, max_pct
= 95;
1984 if (test_migrate_start(&from
, &to
, uri
, &args
)) {
1988 migrate_set_capability(from
, "auto-converge", true);
1989 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1990 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1991 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1994 * Set the initial parameters so that the migration could not converge
1995 * without throttling.
1997 migrate_ensure_non_converge(from
);
1999 /* To check remaining size after precopy */
2000 migrate_set_capability(from
, "pause-before-switchover", true);
2002 /* Wait for the first serial output from the source */
2003 wait_for_serial("src_serial");
2005 migrate_qmp(from
, uri
, "{}");
2007 /* Wait for throttling begins */
2010 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2011 if (percentage
!= 0) {
2015 g_assert_false(got_src_stop
);
2017 /* The first percentage of throttling should be at least init_pct */
2018 g_assert_cmpint(percentage
, >=, init_pct
);
2019 /* Now, when we tested that throttling works, let it converge */
2020 migrate_ensure_converge(from
);
2023 * Wait for pre-switchover status to check last throttle percentage
2024 * and remaining. These values will be zeroed later
2026 wait_for_migration_status(from
, "pre-switchover", NULL
);
2028 /* The final percentage of throttling shouldn't be greater than max_pct */
2029 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2030 g_assert_cmpint(percentage
, <=, max_pct
);
2031 migrate_continue(from
, "pre-switchover");
2033 qtest_qmp_eventwait(to
, "RESUME");
2035 wait_for_serial("dest_serial");
2036 wait_for_migration_complete(from
);
2038 test_migrate_end(from
, to
, true);
2042 test_migrate_precopy_tcp_multifd_start_common(QTestState
*from
,
2046 migrate_set_parameter_int(from
, "multifd-channels", 16);
2047 migrate_set_parameter_int(to
, "multifd-channels", 16);
2049 migrate_set_parameter_str(from
, "multifd-compression", method
);
2050 migrate_set_parameter_str(to
, "multifd-compression", method
);
2052 migrate_set_capability(from
, "multifd", true);
2053 migrate_set_capability(to
, "multifd", true);
2055 /* Start incoming migration from the 1st socket */
2056 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
2057 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2063 test_migrate_precopy_tcp_multifd_start(QTestState
*from
,
2066 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2070 test_migrate_precopy_tcp_multifd_zlib_start(QTestState
*from
,
2073 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zlib");
2078 test_migrate_precopy_tcp_multifd_zstd_start(QTestState
*from
,
2081 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zstd");
2083 #endif /* CONFIG_ZSTD */
2085 static void test_multifd_tcp_none(void)
2087 MigrateCommon args
= {
2088 .listen_uri
= "defer",
2089 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2091 * Multifd is more complicated than most of the features, it
2092 * directly takes guest page buffers when sending, make sure
2093 * everything will work alright even if guest page is changing.
2097 test_precopy_common(&args
);
2100 static void test_multifd_tcp_zlib(void)
2102 MigrateCommon args
= {
2103 .listen_uri
= "defer",
2104 .start_hook
= test_migrate_precopy_tcp_multifd_zlib_start
,
2106 test_precopy_common(&args
);
2110 static void test_multifd_tcp_zstd(void)
2112 MigrateCommon args
= {
2113 .listen_uri
= "defer",
2114 .start_hook
= test_migrate_precopy_tcp_multifd_zstd_start
,
2116 test_precopy_common(&args
);
2120 #ifdef CONFIG_GNUTLS
2122 test_migrate_multifd_tcp_tls_psk_start_match(QTestState
*from
,
2125 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2126 return test_migrate_tls_psk_start_match(from
, to
);
2130 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState
*from
,
2133 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2134 return test_migrate_tls_psk_start_mismatch(from
, to
);
2139 test_migrate_multifd_tls_x509_start_default_host(QTestState
*from
,
2142 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2143 return test_migrate_tls_x509_start_default_host(from
, to
);
2147 test_migrate_multifd_tls_x509_start_override_host(QTestState
*from
,
2150 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2151 return test_migrate_tls_x509_start_override_host(from
, to
);
2155 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState
*from
,
2158 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2159 return test_migrate_tls_x509_start_mismatch_host(from
, to
);
2163 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState
*from
,
2166 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2167 return test_migrate_tls_x509_start_allow_anon_client(from
, to
);
2171 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState
*from
,
2174 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2175 return test_migrate_tls_x509_start_reject_anon_client(from
, to
);
2177 #endif /* CONFIG_TASN1 */
2179 static void test_multifd_tcp_tls_psk_match(void)
2181 MigrateCommon args
= {
2182 .listen_uri
= "defer",
2183 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_match
,
2184 .finish_hook
= test_migrate_tls_psk_finish
,
2186 test_precopy_common(&args
);
2189 static void test_multifd_tcp_tls_psk_mismatch(void)
2191 MigrateCommon args
= {
2193 .hide_stderr
= true,
2195 .listen_uri
= "defer",
2196 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_mismatch
,
2197 .finish_hook
= test_migrate_tls_psk_finish
,
2198 .result
= MIG_TEST_FAIL
,
2200 test_precopy_common(&args
);
2204 static void test_multifd_tcp_tls_x509_default_host(void)
2206 MigrateCommon args
= {
2207 .listen_uri
= "defer",
2208 .start_hook
= test_migrate_multifd_tls_x509_start_default_host
,
2209 .finish_hook
= test_migrate_tls_x509_finish
,
2211 test_precopy_common(&args
);
2214 static void test_multifd_tcp_tls_x509_override_host(void)
2216 MigrateCommon args
= {
2217 .listen_uri
= "defer",
2218 .start_hook
= test_migrate_multifd_tls_x509_start_override_host
,
2219 .finish_hook
= test_migrate_tls_x509_finish
,
2221 test_precopy_common(&args
);
2224 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2227 * This has different behaviour to the non-multifd case.
2229 * In non-multifd case when client aborts due to mismatched
2230 * cert host, the server has already started trying to load
2231 * migration state, and so it exits with I/O failure.
2233 * In multifd case when client aborts due to mismatched
2234 * cert host, the server is still waiting for the other
2235 * multifd connections to arrive so hasn't started trying
2236 * to load migration state, and thus just aborts the migration
2239 MigrateCommon args
= {
2241 .hide_stderr
= true,
2243 .listen_uri
= "defer",
2244 .start_hook
= test_migrate_multifd_tls_x509_start_mismatch_host
,
2245 .finish_hook
= test_migrate_tls_x509_finish
,
2246 .result
= MIG_TEST_FAIL
,
2248 test_precopy_common(&args
);
2251 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2253 MigrateCommon args
= {
2254 .listen_uri
= "defer",
2255 .start_hook
= test_migrate_multifd_tls_x509_start_allow_anon_client
,
2256 .finish_hook
= test_migrate_tls_x509_finish
,
2258 test_precopy_common(&args
);
2261 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2263 MigrateCommon args
= {
2265 .hide_stderr
= true,
2267 .listen_uri
= "defer",
2268 .start_hook
= test_migrate_multifd_tls_x509_start_reject_anon_client
,
2269 .finish_hook
= test_migrate_tls_x509_finish
,
2270 .result
= MIG_TEST_FAIL
,
2272 test_precopy_common(&args
);
2274 #endif /* CONFIG_TASN1 */
2275 #endif /* CONFIG_GNUTLS */
2283 * launch another target
2286 * And see that it works
2288 static void test_multifd_tcp_cancel(void)
2290 MigrateStart args
= {
2291 .hide_stderr
= true,
2293 QTestState
*from
, *to
, *to2
;
2294 g_autofree
char *uri
= NULL
;
2296 if (test_migrate_start(&from
, &to
, "defer", &args
)) {
2300 migrate_ensure_non_converge(from
);
2302 migrate_set_parameter_int(from
, "multifd-channels", 16);
2303 migrate_set_parameter_int(to
, "multifd-channels", 16);
2305 migrate_set_capability(from
, "multifd", true);
2306 migrate_set_capability(to
, "multifd", true);
2308 /* Start incoming migration from the 1st socket */
2309 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
2310 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2312 /* Wait for the first serial output from the source */
2313 wait_for_serial("src_serial");
2315 uri
= migrate_get_socket_address(to
, "socket-address");
2317 migrate_qmp(from
, uri
, "{}");
2319 wait_for_migration_pass(from
);
2321 migrate_cancel(from
);
2323 /* Make sure QEMU process "to" exited */
2324 qtest_set_expected_status(to
, EXIT_FAILURE
);
2325 qtest_wait_qemu(to
);
2327 args
= (MigrateStart
){
2328 .only_target
= true,
2331 if (test_migrate_start(&from
, &to2
, "defer", &args
)) {
2335 migrate_set_parameter_int(to2
, "multifd-channels", 16);
2337 migrate_set_capability(to2
, "multifd", true);
2339 /* Start incoming migration from the 1st socket */
2340 qtest_qmp_assert_success(to2
, "{ 'execute': 'migrate-incoming',"
2341 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2344 uri
= migrate_get_socket_address(to2
, "socket-address");
2346 wait_for_migration_status(from
, "cancelled", NULL
);
2348 migrate_ensure_converge(from
);
2350 migrate_qmp(from
, uri
, "{}");
2352 wait_for_migration_pass(from
);
2354 if (!got_src_stop
) {
2355 qtest_qmp_eventwait(from
, "STOP");
2357 qtest_qmp_eventwait(to2
, "RESUME");
2359 wait_for_serial("dest_serial");
2360 wait_for_migration_complete(from
);
2361 test_migrate_end(from
, to2
, true);
2364 static void calc_dirty_rate(QTestState
*who
, uint64_t calc_time
)
2366 qtest_qmp_assert_success(who
,
2367 "{ 'execute': 'calc-dirty-rate',"
2369 "'calc-time': %" PRIu64
","
2370 "'mode': 'dirty-ring' }}",
2374 static QDict
*query_dirty_rate(QTestState
*who
)
2376 return qtest_qmp_assert_success_ref(who
,
2377 "{ 'execute': 'query-dirty-rate' }");
2380 static void dirtylimit_set_all(QTestState
*who
, uint64_t dirtyrate
)
2382 qtest_qmp_assert_success(who
,
2383 "{ 'execute': 'set-vcpu-dirty-limit',"
2385 "'dirty-rate': %" PRIu64
" } }",
2389 static void cancel_vcpu_dirty_limit(QTestState
*who
)
2391 qtest_qmp_assert_success(who
,
2392 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2395 static QDict
*query_vcpu_dirty_limit(QTestState
*who
)
2399 rsp
= qtest_qmp(who
, "{ 'execute': 'query-vcpu-dirty-limit' }");
2400 g_assert(!qdict_haskey(rsp
, "error"));
2401 g_assert(qdict_haskey(rsp
, "return"));
2406 static bool calc_dirtyrate_ready(QTestState
*who
)
2411 rsp_return
= query_dirty_rate(who
);
2412 g_assert(rsp_return
);
2414 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2417 return g_strcmp0(status
, "measuring");
2420 static void wait_for_calc_dirtyrate_complete(QTestState
*who
,
2423 int max_try_count
= 10000;
2424 usleep(time_s
* 1000000);
2426 while (!calc_dirtyrate_ready(who
) && max_try_count
--) {
2431 * Set the timeout with 10 s(max_try_count * 1000us),
2432 * if dirtyrate measurement not complete, fail test.
2434 g_assert_cmpint(max_try_count
, !=, 0);
2437 static int64_t get_dirty_rate(QTestState
*who
)
2442 const QListEntry
*entry
;
2446 rsp_return
= query_dirty_rate(who
);
2447 g_assert(rsp_return
);
2449 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2451 g_assert_cmpstr(status
, ==, "measured");
2453 rates
= qdict_get_qlist(rsp_return
, "vcpu-dirty-rate");
2454 g_assert(rates
&& !qlist_empty(rates
));
2456 entry
= qlist_first(rates
);
2459 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2462 dirtyrate
= qdict_get_try_int(rate
, "dirty-rate", -1);
2464 qobject_unref(rsp_return
);
2468 static int64_t get_limit_rate(QTestState
*who
)
2472 const QListEntry
*entry
;
2476 rsp_return
= query_vcpu_dirty_limit(who
);
2477 g_assert(rsp_return
);
2479 rates
= qdict_get_qlist(rsp_return
, "return");
2480 g_assert(rates
&& !qlist_empty(rates
));
2482 entry
= qlist_first(rates
);
2485 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2488 dirtyrate
= qdict_get_try_int(rate
, "limit-rate", -1);
2490 qobject_unref(rsp_return
);
2494 static QTestState
*dirtylimit_start_vm(void)
2496 QTestState
*vm
= NULL
;
2497 g_autofree gchar
*cmd
= NULL
;
2498 const char *arch
= qtest_get_arch();
2499 g_autofree
char *bootpath
= NULL
;
2501 assert((strcmp(arch
, "x86_64") == 0));
2502 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
2503 assert(sizeof(x86_bootsect
) == 512);
2504 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
2506 cmd
= g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2507 "-name dirtylimit-test,debug-threads=on "
2509 "-serial file:%s/vm_serial "
2510 "-drive file=%s,format=raw ",
2513 vm
= qtest_init(cmd
);
2517 static void dirtylimit_stop_vm(QTestState
*vm
)
2520 cleanup("bootsect");
2521 cleanup("vm_serial");
2524 static void test_vcpu_dirty_limit(void)
2527 int64_t origin_rate
;
2530 int max_try_count
= 20;
2533 /* Start vm for vcpu dirtylimit test */
2534 vm
= dirtylimit_start_vm();
2536 /* Wait for the first serial output from the vm*/
2537 wait_for_serial("vm_serial");
2539 /* Do dirtyrate measurement with calc time equals 1s */
2540 calc_dirty_rate(vm
, 1);
2542 /* Sleep calc time and wait for calc dirtyrate complete */
2543 wait_for_calc_dirtyrate_complete(vm
, 1);
2545 /* Query original dirty page rate */
2546 origin_rate
= get_dirty_rate(vm
);
2548 /* VM booted from bootsect should dirty memory steadily */
2549 assert(origin_rate
!= 0);
2551 /* Setup quota dirty page rate at half of origin */
2552 quota_rate
= origin_rate
/ 2;
2554 /* Set dirtylimit */
2555 dirtylimit_set_all(vm
, quota_rate
);
2558 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2561 g_assert_cmpint(quota_rate
, ==, get_limit_rate(vm
));
2563 /* Sleep a bit to check if it take effect */
2567 * Check if dirtylimit take effect realistically, set the
2568 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2569 * doesn't take effect, fail test.
2571 while (--max_try_count
) {
2572 calc_dirty_rate(vm
, 1);
2573 wait_for_calc_dirtyrate_complete(vm
, 1);
2574 rate
= get_dirty_rate(vm
);
2577 * Assume hitting if current rate is less
2578 * than quota rate (within accepting error)
2580 if (rate
< (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2586 g_assert_cmpint(hit
, ==, 1);
2591 /* Check if dirtylimit cancellation take effect */
2592 cancel_vcpu_dirty_limit(vm
);
2593 while (--max_try_count
) {
2594 calc_dirty_rate(vm
, 1);
2595 wait_for_calc_dirtyrate_complete(vm
, 1);
2596 rate
= get_dirty_rate(vm
);
2599 * Assume dirtylimit be canceled if current rate is
2600 * greater than quota rate (within accepting error)
2602 if (rate
> (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2608 g_assert_cmpint(hit
, ==, 1);
2609 dirtylimit_stop_vm(vm
);
2612 static bool kvm_dirty_ring_supported(void)
2614 #if defined(__linux__) && defined(HOST_X86_64)
2615 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
2621 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
2624 /* We test with 4096 slots */
2635 int main(int argc
, char **argv
)
2637 bool has_kvm
, has_tcg
;
2640 g_autoptr(GError
) err
= NULL
;
2643 g_test_init(&argc
, &argv
, NULL
);
2645 has_kvm
= qtest_has_accel("kvm");
2646 has_tcg
= qtest_has_accel("tcg");
2648 if (!has_tcg
&& !has_kvm
) {
2649 g_test_skip("No KVM or TCG accelerator available");
2653 has_uffd
= ufd_version_check();
2654 arch
= qtest_get_arch();
2657 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2658 * is touchy due to race conditions on dirty bits (especially on PPC for
2661 if (g_str_equal(arch
, "ppc64") &&
2662 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
2663 g_test_message("Skipping test: kvm_hv not available");
2664 return g_test_run();
2668 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2669 * there until the problems are resolved
2671 if (g_str_equal(arch
, "s390x") && !has_kvm
) {
2672 g_test_message("Skipping test: s390x host with KVM is required");
2673 return g_test_run();
2676 tmpfs
= g_dir_make_tmp("migration-test-XXXXXX", &err
);
2678 g_test_message("Can't create temporary directory in %s: %s",
2679 g_get_tmp_dir(), err
->message
);
2683 module_call_init(MODULE_INIT_QOM
);
2686 qtest_add_func("/migration/postcopy/plain", test_postcopy
);
2687 qtest_add_func("/migration/postcopy/recovery/plain",
2688 test_postcopy_recovery
);
2689 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt
);
2690 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2691 test_postcopy_preempt_recovery
);
2692 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2693 qtest_add_func("/migration/postcopy/compress/plain",
2694 test_postcopy_compress
);
2695 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2696 test_postcopy_recovery_compress
);
2700 qtest_add_func("/migration/bad_dest", test_baddest
);
2701 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain
);
2702 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle
);
2704 * Compression fails from time to time.
2705 * Put test here but don't enable it until everything is fixed.
2707 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2708 qtest_add_func("/migration/precopy/unix/compress/wait",
2709 test_precopy_unix_compress
);
2710 qtest_add_func("/migration/precopy/unix/compress/nowait",
2711 test_precopy_unix_compress_nowait
);
2713 #ifdef CONFIG_GNUTLS
2714 qtest_add_func("/migration/precopy/unix/tls/psk",
2715 test_precopy_unix_tls_psk
);
2719 * NOTE: psk test is enough for postcopy, as other types of TLS
2720 * channels are tested under precopy. Here what we want to test is the
2721 * general postcopy path that has TLS channel enabled.
2723 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk
);
2724 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2725 test_postcopy_recovery_tls_psk
);
2726 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2727 test_postcopy_preempt_tls_psk
);
2728 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2729 test_postcopy_preempt_all
);
2732 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2733 test_precopy_unix_tls_x509_default_host
);
2734 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2735 test_precopy_unix_tls_x509_override_host
);
2736 #endif /* CONFIG_TASN1 */
2737 #endif /* CONFIG_GNUTLS */
2739 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain
);
2740 #ifdef CONFIG_GNUTLS
2741 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2742 test_precopy_tcp_tls_psk_match
);
2743 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2744 test_precopy_tcp_tls_psk_mismatch
);
2746 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2747 test_precopy_tcp_tls_x509_default_host
);
2748 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2749 test_precopy_tcp_tls_x509_override_host
);
2750 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2751 test_precopy_tcp_tls_x509_mismatch_host
);
2752 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2753 test_precopy_tcp_tls_x509_friendly_client
);
2754 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2755 test_precopy_tcp_tls_x509_hostile_client
);
2756 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2757 test_precopy_tcp_tls_x509_allow_anon_client
);
2758 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2759 test_precopy_tcp_tls_x509_reject_anon_client
);
2760 #endif /* CONFIG_TASN1 */
2761 #endif /* CONFIG_GNUTLS */
2763 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2765 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
2767 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
2768 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
2769 qtest_add_func("/migration/validate_uuid_src_not_set",
2770 test_validate_uuid_src_not_set
);
2771 qtest_add_func("/migration/validate_uuid_dst_not_set",
2772 test_validate_uuid_dst_not_set
);
2774 * See explanation why this test is slow on function definition
2776 if (g_test_slow()) {
2777 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
2779 qtest_add_func("/migration/multifd/tcp/plain/none",
2780 test_multifd_tcp_none
);
2782 * This test is flaky and sometimes fails in CI and otherwise:
2783 * don't run unless user opts in via environment variable.
2785 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2786 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2787 test_multifd_tcp_cancel
);
2789 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2790 test_multifd_tcp_zlib
);
2792 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2793 test_multifd_tcp_zstd
);
2795 #ifdef CONFIG_GNUTLS
2796 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2797 test_multifd_tcp_tls_psk_match
);
2798 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2799 test_multifd_tcp_tls_psk_mismatch
);
2801 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2802 test_multifd_tcp_tls_x509_default_host
);
2803 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2804 test_multifd_tcp_tls_x509_override_host
);
2805 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2806 test_multifd_tcp_tls_x509_mismatch_host
);
2807 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2808 test_multifd_tcp_tls_x509_allow_anon_client
);
2809 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2810 test_multifd_tcp_tls_x509_reject_anon_client
);
2811 #endif /* CONFIG_TASN1 */
2812 #endif /* CONFIG_GNUTLS */
2814 if (g_str_equal(arch
, "x86_64") && has_kvm
&& kvm_dirty_ring_supported()) {
2815 qtest_add_func("/migration/dirty_ring",
2816 test_precopy_unix_dirty_ring
);
2817 qtest_add_func("/migration/vcpu_dirty_limit",
2818 test_vcpu_dirty_limit
);
2823 g_assert_cmpint(ret
, ==, 0);
2827 g_test_message("unable to rmdir: path (%s): %s",
2828 tmpfs
, strerror(errno
));