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)");
120 /* The boot file modifies memory area in [start_address, end_address)
121 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
123 #include "tests/migration/i386/a-b-bootblock.h"
124 #include "tests/migration/aarch64/a-b-kernel.h"
125 #include "tests/migration/s390x/a-b-bios.h"
127 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
129 FILE *bootfile
= fopen(bootpath
, "wb");
131 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
136 * Wait for some output in the serial output file,
137 * we get an 'A' followed by an endless string of 'B's
138 * but on the destination we won't have the A.
140 static void wait_for_serial(const char *side
)
142 g_autofree
char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
143 FILE *serialfile
= fopen(serialpath
, "r");
144 const char *arch
= qtest_get_arch();
145 int started
= (strcmp(side
, "src_serial") == 0 &&
146 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
149 int readvalue
= fgetc(serialfile
);
152 /* SLOF prints its banner before starting test,
153 * to ignore it, mark the start of the test with '_',
154 * ignore all characters until this marker
161 fseek(serialfile
, 0, SEEK_SET
);
178 started
= (strcmp(side
, "src_serial") == 0 &&
179 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
180 fseek(serialfile
, 0, SEEK_SET
);
185 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
186 g_assert_not_reached();
192 * It's tricky to use qemu's migration event capability with qtest,
193 * events suddenly appearing confuse the qmp()/hmp() responses.
196 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
198 QDict
*rsp_return
, *rsp_ram
;
201 rsp_return
= migrate_query_not_failed(who
);
202 if (!qdict_haskey(rsp_return
, "ram")) {
206 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
207 result
= qdict_get_try_int(rsp_ram
, property
, 0);
209 qobject_unref(rsp_return
);
213 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
218 rsp_return
= migrate_query_not_failed(who
);
219 result
= qdict_get_try_int(rsp_return
, property
, 0);
220 qobject_unref(rsp_return
);
224 static uint64_t get_migration_pass(QTestState
*who
)
226 return read_ram_property_int(who
, "dirty-sync-count");
229 static void read_blocktime(QTestState
*who
)
233 rsp_return
= migrate_query_not_failed(who
);
234 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
235 qobject_unref(rsp_return
);
238 static void wait_for_migration_pass(QTestState
*who
)
240 uint64_t initial_pass
= get_migration_pass(who
);
243 /* Wait for the 1st sync */
244 while (!got_src_stop
&& !initial_pass
) {
246 initial_pass
= get_migration_pass(who
);
251 pass
= get_migration_pass(who
);
252 } while (pass
== initial_pass
&& !got_src_stop
);
255 static void check_guests_ram(QTestState
*who
)
257 /* Our ASM test will have been incrementing one byte from each page from
258 * start_address to < end_address in order. This gives us a constraint
259 * that any page's byte should be equal or less than the previous pages
260 * byte (mod 256); and they should all be equal except for one transition
261 * at the point where we meet the incrementer. (We're running this with
262 * the guest stopped).
267 bool hit_edge
= false;
270 qtest_memread(who
, start_address
, &first_byte
, 1);
271 last_byte
= first_byte
;
273 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
274 address
+= TEST_MEM_PAGE_SIZE
)
277 qtest_memread(who
, address
, &b
, 1);
278 if (b
!= last_byte
) {
279 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
280 /* This is OK, the guest stopped at the point of
281 * incrementing the previous page but didn't get
289 fprintf(stderr
, "Memory content inconsistency at %x"
290 " first_byte = %x last_byte = %x current = %x"
292 address
, first_byte
, last_byte
, b
, hit_edge
);
298 fprintf(stderr
, "and in another %d pages", bad
- 10);
303 static void cleanup(const char *filename
)
305 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
310 static char *SocketAddress_to_str(SocketAddress
*addr
)
312 switch (addr
->type
) {
313 case SOCKET_ADDRESS_TYPE_INET
:
314 return g_strdup_printf("tcp:%s:%s",
317 case SOCKET_ADDRESS_TYPE_UNIX
:
318 return g_strdup_printf("unix:%s",
319 addr
->u
.q_unix
.path
);
320 case SOCKET_ADDRESS_TYPE_FD
:
321 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
322 case SOCKET_ADDRESS_TYPE_VSOCK
:
323 return g_strdup_printf("tcp:%s:%s",
327 return g_strdup("unknown address type");
331 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
335 SocketAddressList
*addrs
;
339 rsp
= migrate_query(who
);
340 object
= qdict_get(rsp
, parameter
);
342 iv
= qobject_input_visitor_new(object
);
343 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
346 /* we are only using a single address */
347 result
= SocketAddress_to_str(addrs
->value
);
349 qapi_free_SocketAddressList(addrs
);
354 static long long migrate_get_parameter_int(QTestState
*who
,
355 const char *parameter
)
360 rsp
= qtest_qmp_assert_success_ref(
361 who
, "{ 'execute': 'query-migrate-parameters' }");
362 result
= qdict_get_int(rsp
, parameter
);
367 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
372 result
= migrate_get_parameter_int(who
, parameter
);
373 g_assert_cmpint(result
, ==, value
);
376 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
379 qtest_qmp_assert_success(who
,
380 "{ 'execute': 'migrate-set-parameters',"
381 "'arguments': { %s: %lld } }",
383 migrate_check_parameter_int(who
, parameter
, value
);
386 static char *migrate_get_parameter_str(QTestState
*who
,
387 const char *parameter
)
392 rsp
= qtest_qmp_assert_success_ref(
393 who
, "{ 'execute': 'query-migrate-parameters' }");
394 result
= g_strdup(qdict_get_str(rsp
, parameter
));
399 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
402 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
403 g_assert_cmpstr(result
, ==, value
);
406 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
409 qtest_qmp_assert_success(who
,
410 "{ 'execute': 'migrate-set-parameters',"
411 "'arguments': { %s: %s } }",
413 migrate_check_parameter_str(who
, parameter
, value
);
416 static long long migrate_get_parameter_bool(QTestState
*who
,
417 const char *parameter
)
422 rsp
= qtest_qmp_assert_success_ref(
423 who
, "{ 'execute': 'query-migrate-parameters' }");
424 result
= qdict_get_bool(rsp
, parameter
);
429 static void migrate_check_parameter_bool(QTestState
*who
, const char *parameter
,
434 result
= migrate_get_parameter_bool(who
, parameter
);
435 g_assert_cmpint(result
, ==, value
);
438 static void migrate_set_parameter_bool(QTestState
*who
, const char *parameter
,
441 qtest_qmp_assert_success(who
,
442 "{ 'execute': 'migrate-set-parameters',"
443 "'arguments': { %s: %i } }",
445 migrate_check_parameter_bool(who
, parameter
, value
);
448 static void migrate_ensure_non_converge(QTestState
*who
)
450 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
451 migrate_set_parameter_int(who
, "max-bandwidth", 3 * 1000 * 1000);
452 migrate_set_parameter_int(who
, "downtime-limit", 1);
455 static void migrate_ensure_converge(QTestState
*who
)
457 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
458 migrate_set_parameter_int(who
, "max-bandwidth", 1 * 1000 * 1000 * 1000);
459 migrate_set_parameter_int(who
, "downtime-limit", 30 * 1000);
463 * Our goal is to ensure that we run a single full migration
464 * iteration, and also dirty memory, ensuring that at least
465 * one further iteration is required.
467 * We can't directly synchronize with the start of a migration
468 * so we have to apply some tricks monitoring memory that is
471 * Initially we set the migration bandwidth to an insanely
472 * low value, with tiny max downtime too. This basically
473 * guarantees migration will never complete.
475 * This will result in a test that is unacceptably slow though,
476 * so we can't let the entire migration pass run at this speed.
477 * Our intent is to let it run just long enough that we can
478 * prove data prior to the marker has been transferred *AND*
479 * also prove this transferred data is dirty again.
481 * Before migration starts, we write a 64-bit magic marker
482 * into a fixed location in the src VM RAM.
484 * Then watch dst memory until the marker appears. This is
485 * proof that start_address -> MAGIC_OFFSET_BASE has been
488 * Finally we go back to the source and read a byte just
489 * before the marker until we see it flip in value. This
490 * is proof that start_address -> MAGIC_OFFSET_BASE
491 * is now dirty again.
493 * IOW, we're guaranteed at least a 2nd migration pass
496 * We can now let migration run at full speed to finish
499 static void migrate_prepare_for_dirty_mem(QTestState
*from
)
502 * The guest workflow iterates from start_address to
503 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
506 * IOW, if we write to mem at a point which is NOT
507 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
508 * conflict with the migration workflow.
510 * We put in a marker here, that we'll use to determine
511 * when the data has been transferred to the dst.
513 qtest_writeq(from
, start_address
+ MAGIC_OFFSET
, MAGIC_MARKER
);
516 static void migrate_wait_for_dirty_mem(QTestState
*from
,
519 uint64_t watch_address
= start_address
+ MAGIC_OFFSET_BASE
;
520 uint64_t marker_address
= start_address
+ MAGIC_OFFSET
;
524 * Wait for the MAGIC_MARKER to get transferred, as an
525 * indicator that a migration pass has made some known
526 * amount of progress.
530 } while (qtest_readq(to
, marker_address
) != MAGIC_MARKER
);
533 * Now ensure that already transferred bytes are
534 * dirty again from the guest workload. Note the
535 * guest byte value will wrap around and by chance
536 * match the original watch_byte. This is harmless
537 * as we'll eventually see a different value if we
540 watch_byte
= qtest_readb(from
, watch_address
);
543 } while (qtest_readb(from
, watch_address
) == watch_byte
);
547 static void migrate_pause(QTestState
*who
)
549 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate-pause' }");
552 static void migrate_continue(QTestState
*who
, const char *state
)
554 qtest_qmp_assert_success(who
,
555 "{ 'execute': 'migrate-continue',"
556 " 'arguments': { 'state': %s } }",
560 static void migrate_recover(QTestState
*who
, const char *uri
)
562 qtest_qmp_assert_success(who
,
563 "{ 'execute': 'migrate-recover', "
564 " 'id': 'recover-cmd', "
565 " 'arguments': { 'uri': %s } }",
569 static void migrate_cancel(QTestState
*who
)
571 qtest_qmp_assert_success(who
, "{ 'execute': 'migrate_cancel' }");
574 static void migrate_set_capability(QTestState
*who
, const char *capability
,
577 qtest_qmp_assert_success(who
,
578 "{ 'execute': 'migrate-set-capabilities',"
580 "'capabilities': [ { "
581 "'capability': %s, 'state': %i } ] } }",
585 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
587 qtest_qmp_assert_success(from
, "{ 'execute': 'migrate-start-postcopy' }");
590 qtest_qmp_eventwait(from
, "STOP");
593 qtest_qmp_eventwait(to
, "RESUME");
598 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
599 * unconditionally, because it means the user would like to be verbose.
603 /* only launch the target process */
605 /* Use dirty ring if true; dirty logging otherwise */
607 const char *opts_source
;
608 const char *opts_target
;
612 * A hook that runs after the src and dst QEMUs have been
613 * created, but before the migration is started. This can
614 * be used to set migration parameters and capabilities.
616 * Returns: NULL, or a pointer to opaque state to be
617 * later passed to the TestMigrateFinishHook
619 typedef void * (*TestMigrateStartHook
)(QTestState
*from
,
623 * A hook that runs after the migration has finished,
624 * regardless of whether it succeeded or failed, but
625 * before QEMU has terminated (unless it self-terminated
626 * due to migration error)
628 * @opaque is a pointer to state previously returned
629 * by the TestMigrateStartHook if any, or NULL.
631 typedef void (*TestMigrateFinishHook
)(QTestState
*from
,
636 /* Optional: fine tune start parameters */
639 /* Required: the URI for the dst QEMU to listen on */
640 const char *listen_uri
;
643 * Optional: the URI for the src QEMU to connect to
644 * If NULL, then it will query the dst QEMU for its actual
645 * listening address and use that as the connect address.
646 * This allows for dynamically picking a free TCP port.
648 const char *connect_uri
;
650 /* Optional: callback to run at start to set migration parameters */
651 TestMigrateStartHook start_hook
;
652 /* Optional: callback to run at finish to cleanup */
653 TestMigrateFinishHook finish_hook
;
656 * Optional: normally we expect the migration process to complete.
658 * There can be a variety of reasons and stages in which failure
659 * can happen during tests.
661 * If a failure is expected to happen at time of establishing
662 * the connection, then MIG_TEST_FAIL will indicate that the dst
663 * QEMU is expected to stay running and accept future migration
666 * If a failure is expected to happen while processing the
667 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
668 * that the dst QEMU is expected to quit with non-zero exit status
671 /* This test should succeed, the default */
672 MIG_TEST_SUCCEED
= 0,
673 /* This test should fail, dest qemu should keep alive */
675 /* This test should fail, dest qemu should fail with abnormal status */
676 MIG_TEST_FAIL_DEST_QUIT_ERR
,
680 * Optional: set number of migration passes to wait for, if live==true.
681 * If zero, then merely wait for a few MB of dirty data
683 unsigned int iterations
;
686 * Optional: whether the guest CPUs should be running during a precopy
687 * migration test. We used to always run with live but it took much
688 * longer so we reduced live tests to only the ones that have solid
689 * reason to be tested live-only. For each of the new test cases for
690 * precopy please provide justifications to use live explicitly (please
691 * refer to existing ones with live=true), or use live=off by default.
695 /* Postcopy specific fields */
697 bool postcopy_preempt
;
700 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
701 const char *uri
, MigrateStart
*args
)
703 g_autofree gchar
*arch_source
= NULL
;
704 g_autofree gchar
*arch_target
= NULL
;
705 /* options for source and target */
706 g_autofree gchar
*arch_opts
= NULL
;
707 g_autofree gchar
*cmd_source
= NULL
;
708 g_autofree gchar
*cmd_target
= NULL
;
709 const gchar
*ignore_stderr
;
710 g_autofree
char *bootpath
= NULL
;
711 g_autofree
char *shmem_opts
= NULL
;
712 g_autofree
char *shmem_path
= NULL
;
713 const char *arch
= qtest_get_arch();
714 const char *memory_size
;
716 if (args
->use_shmem
) {
717 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
718 g_test_skip("/dev/shm is not supported");
723 got_src_stop
= false;
724 got_dst_resume
= false;
725 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
726 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
727 /* the assembled x86 boot sector should be exactly one sector large */
728 assert(sizeof(x86_bootsect
) == 512);
729 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
730 memory_size
= "150M";
731 arch_opts
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
732 start_address
= X86_TEST_MEM_START
;
733 end_address
= X86_TEST_MEM_END
;
734 } else if (g_str_equal(arch
, "s390x")) {
735 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
736 memory_size
= "128M";
737 arch_opts
= g_strdup_printf("-bios %s", bootpath
);
738 start_address
= S390_TEST_MEM_START
;
739 end_address
= S390_TEST_MEM_END
;
740 } else if (strcmp(arch
, "ppc64") == 0) {
741 memory_size
= "256M";
742 start_address
= PPC_TEST_MEM_START
;
743 end_address
= PPC_TEST_MEM_END
;
744 arch_source
= g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
745 "'nvramrc=hex .\" _\" begin %x %x "
746 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
747 "until'", end_address
, start_address
);
748 arch_opts
= g_strdup("-nodefaults -machine vsmt=8");
749 } else if (strcmp(arch
, "aarch64") == 0) {
750 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
751 memory_size
= "150M";
752 arch_opts
= g_strdup_printf("-machine virt,gic-version=max -cpu max "
753 "-kernel %s", bootpath
);
754 start_address
= ARM_TEST_MEM_START
;
755 end_address
= ARM_TEST_MEM_END
;
757 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
759 g_assert_not_reached();
762 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
764 ignore_stderr
= "2>/dev/null";
767 * On Windows the QEMU executable is created via CreateProcess() and
768 * IO redirection does not work, so don't bother adding IO redirection
769 * to the command line.
777 if (args
->use_shmem
) {
778 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
779 shmem_opts
= g_strdup_printf(
780 "-object memory-backend-file,id=mem0,size=%s"
781 ",mem-path=%s,share=on -numa node,memdev=mem0",
782 memory_size
, shmem_path
);
785 shmem_opts
= g_strdup("");
788 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg "
789 "-name source,debug-threads=on "
791 "-serial file:%s/src_serial "
793 args
->use_dirty_ring
?
794 ",dirty-ring-size=4096" : "",
796 arch_opts
? arch_opts
: "",
797 arch_source
? arch_source
: "",
799 args
->opts_source
? args
->opts_source
: "",
801 if (!args
->only_target
) {
802 *from
= qtest_init(cmd_source
);
803 qtest_qmp_set_event_callback(*from
,
804 migrate_watch_for_stop
,
808 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg "
809 "-name target,debug-threads=on "
811 "-serial file:%s/dest_serial "
814 args
->use_dirty_ring
?
815 ",dirty-ring-size=4096" : "",
816 memory_size
, tmpfs
, uri
,
817 arch_opts
? arch_opts
: "",
818 arch_target
? arch_target
: "",
820 args
->opts_target
? args
->opts_target
: "",
822 *to
= qtest_init(cmd_target
);
823 qtest_qmp_set_event_callback(*to
,
824 migrate_watch_for_resume
,
828 * Remove shmem file immediately to avoid memory leak in test failed case.
829 * It's valid because QEMU has already opened this file
831 if (args
->use_shmem
) {
838 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
840 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
845 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
847 /* Destination still running, wait for a byte to change */
849 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
851 } while (dest_byte_a
== dest_byte_b
);
853 qtest_qmp_assert_success(to
, "{ 'execute' : 'stop'}");
855 /* With it stopped, check nothing changes */
856 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
858 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
859 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
861 check_guests_ram(to
);
867 cleanup("migsocket");
868 cleanup("src_serial");
869 cleanup("dest_serial");
873 struct TestMigrateTLSPSKData
{
881 test_migrate_tls_psk_start_common(QTestState
*from
,
885 struct TestMigrateTLSPSKData
*data
=
886 g_new0(struct TestMigrateTLSPSKData
, 1);
888 data
->workdir
= g_strdup_printf("%s/tlscredspsk0", tmpfs
);
889 data
->pskfile
= g_strdup_printf("%s/%s", data
->workdir
,
890 QCRYPTO_TLS_CREDS_PSKFILE
);
891 g_mkdir_with_parents(data
->workdir
, 0700);
892 test_tls_psk_init(data
->pskfile
);
895 data
->workdiralt
= g_strdup_printf("%s/tlscredspskalt0", tmpfs
);
896 data
->pskfilealt
= g_strdup_printf("%s/%s", data
->workdiralt
,
897 QCRYPTO_TLS_CREDS_PSKFILE
);
898 g_mkdir_with_parents(data
->workdiralt
, 0700);
899 test_tls_psk_init_alt(data
->pskfilealt
);
902 qtest_qmp_assert_success(from
,
903 "{ 'execute': 'object-add',"
904 " 'arguments': { 'qom-type': 'tls-creds-psk',"
905 " 'id': 'tlscredspsk0',"
906 " 'endpoint': 'client',"
908 " 'username': 'qemu'} }",
911 qtest_qmp_assert_success(to
,
912 "{ 'execute': 'object-add',"
913 " 'arguments': { 'qom-type': 'tls-creds-psk',"
914 " 'id': 'tlscredspsk0',"
915 " 'endpoint': 'server',"
917 mismatch
? data
->workdiralt
: data
->workdir
);
919 migrate_set_parameter_str(from
, "tls-creds", "tlscredspsk0");
920 migrate_set_parameter_str(to
, "tls-creds", "tlscredspsk0");
926 test_migrate_tls_psk_start_match(QTestState
*from
,
929 return test_migrate_tls_psk_start_common(from
, to
, false);
933 test_migrate_tls_psk_start_mismatch(QTestState
*from
,
936 return test_migrate_tls_psk_start_common(from
, to
, true);
940 test_migrate_tls_psk_finish(QTestState
*from
,
944 struct TestMigrateTLSPSKData
*data
= opaque
;
946 test_tls_psk_cleanup(data
->pskfile
);
947 if (data
->pskfilealt
) {
948 test_tls_psk_cleanup(data
->pskfilealt
);
950 rmdir(data
->workdir
);
951 if (data
->workdiralt
) {
952 rmdir(data
->workdiralt
);
955 g_free(data
->workdiralt
);
956 g_free(data
->pskfilealt
);
957 g_free(data
->workdir
);
958 g_free(data
->pskfile
);
971 } TestMigrateTLSX509Data
;
978 const char *certhostname
;
979 const char *certipaddr
;
980 } TestMigrateTLSX509
;
983 test_migrate_tls_x509_start_common(QTestState
*from
,
985 TestMigrateTLSX509
*args
)
987 TestMigrateTLSX509Data
*data
= g_new0(TestMigrateTLSX509Data
, 1);
989 data
->workdir
= g_strdup_printf("%s/tlscredsx5090", tmpfs
);
990 data
->keyfile
= g_strdup_printf("%s/key.pem", data
->workdir
);
992 data
->cacert
= g_strdup_printf("%s/ca-cert.pem", data
->workdir
);
993 data
->serverkey
= g_strdup_printf("%s/server-key.pem", data
->workdir
);
994 data
->servercert
= g_strdup_printf("%s/server-cert.pem", data
->workdir
);
995 if (args
->clientcert
) {
996 data
->clientkey
= g_strdup_printf("%s/client-key.pem", data
->workdir
);
997 data
->clientcert
= g_strdup_printf("%s/client-cert.pem", data
->workdir
);
1000 g_mkdir_with_parents(data
->workdir
, 0700);
1002 test_tls_init(data
->keyfile
);
1004 g_assert(link(data
->keyfile
, data
->serverkey
) == 0);
1006 g_assert(CreateHardLink(data
->serverkey
, data
->keyfile
, NULL
) != 0);
1008 if (args
->clientcert
) {
1010 g_assert(link(data
->keyfile
, data
->clientkey
) == 0);
1012 g_assert(CreateHardLink(data
->clientkey
, data
->keyfile
, NULL
) != 0);
1016 TLS_ROOT_REQ_SIMPLE(cacertreq
, data
->cacert
);
1017 if (args
->clientcert
) {
1018 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq
, cacertreq
,
1019 args
->hostileclient
?
1020 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME
:
1021 QCRYPTO_TLS_TEST_CLIENT_NAME
,
1025 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq
, cacertreq
,
1030 qtest_qmp_assert_success(from
,
1031 "{ 'execute': 'object-add',"
1032 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1033 " 'id': 'tlscredsx509client0',"
1034 " 'endpoint': 'client',"
1036 " 'sanity-check': true,"
1037 " 'verify-peer': true} }",
1039 migrate_set_parameter_str(from
, "tls-creds", "tlscredsx509client0");
1040 if (args
->certhostname
) {
1041 migrate_set_parameter_str(from
, "tls-hostname", args
->certhostname
);
1044 qtest_qmp_assert_success(to
,
1045 "{ 'execute': 'object-add',"
1046 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1047 " 'id': 'tlscredsx509server0',"
1048 " 'endpoint': 'server',"
1050 " 'sanity-check': true,"
1051 " 'verify-peer': %i} }",
1052 data
->workdir
, args
->verifyclient
);
1053 migrate_set_parameter_str(to
, "tls-creds", "tlscredsx509server0");
1055 if (args
->authzclient
) {
1056 qtest_qmp_assert_success(to
,
1057 "{ 'execute': 'object-add',"
1058 " 'arguments': { 'qom-type': 'authz-simple',"
1059 " 'id': 'tlsauthz0',"
1060 " 'identity': %s} }",
1061 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME
);
1062 migrate_set_parameter_str(to
, "tls-authz", "tlsauthz0");
1069 * The normal case: match server's cert hostname against
1070 * whatever host we were telling QEMU to connect to (if any)
1073 test_migrate_tls_x509_start_default_host(QTestState
*from
,
1076 TestMigrateTLSX509 args
= {
1077 .verifyclient
= true,
1079 .certipaddr
= "127.0.0.1"
1081 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1085 * The unusual case: the server's cert is different from
1086 * the address we're telling QEMU to connect to (if any),
1087 * so we must give QEMU an explicit hostname to validate
1090 test_migrate_tls_x509_start_override_host(QTestState
*from
,
1093 TestMigrateTLSX509 args
= {
1094 .verifyclient
= true,
1096 .certhostname
= "qemu.org",
1098 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1102 * The unusual case: the server's cert is different from
1103 * the address we're telling QEMU to connect to, and so we
1104 * expect the client to reject the server
1107 test_migrate_tls_x509_start_mismatch_host(QTestState
*from
,
1110 TestMigrateTLSX509 args
= {
1111 .verifyclient
= true,
1113 .certipaddr
= "10.0.0.1",
1115 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1119 test_migrate_tls_x509_start_friendly_client(QTestState
*from
,
1122 TestMigrateTLSX509 args
= {
1123 .verifyclient
= true,
1125 .authzclient
= true,
1126 .certipaddr
= "127.0.0.1",
1128 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1132 test_migrate_tls_x509_start_hostile_client(QTestState
*from
,
1135 TestMigrateTLSX509 args
= {
1136 .verifyclient
= true,
1138 .hostileclient
= true,
1139 .authzclient
= true,
1140 .certipaddr
= "127.0.0.1",
1142 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1146 * The case with no client certificate presented,
1147 * and no server verification
1150 test_migrate_tls_x509_start_allow_anon_client(QTestState
*from
,
1153 TestMigrateTLSX509 args
= {
1154 .certipaddr
= "127.0.0.1",
1156 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1160 * The case with no client certificate presented,
1161 * and server verification rejecting
1164 test_migrate_tls_x509_start_reject_anon_client(QTestState
*from
,
1167 TestMigrateTLSX509 args
= {
1168 .verifyclient
= true,
1169 .certipaddr
= "127.0.0.1",
1171 return test_migrate_tls_x509_start_common(from
, to
, &args
);
1175 test_migrate_tls_x509_finish(QTestState
*from
,
1179 TestMigrateTLSX509Data
*data
= opaque
;
1181 test_tls_cleanup(data
->keyfile
);
1182 g_free(data
->keyfile
);
1184 unlink(data
->cacert
);
1185 g_free(data
->cacert
);
1186 unlink(data
->servercert
);
1187 g_free(data
->servercert
);
1188 unlink(data
->serverkey
);
1189 g_free(data
->serverkey
);
1191 if (data
->clientcert
) {
1192 unlink(data
->clientcert
);
1193 g_free(data
->clientcert
);
1195 if (data
->clientkey
) {
1196 unlink(data
->clientkey
);
1197 g_free(data
->clientkey
);
1200 rmdir(data
->workdir
);
1201 g_free(data
->workdir
);
1205 #endif /* CONFIG_TASN1 */
1206 #endif /* CONFIG_GNUTLS */
1209 test_migrate_compress_start(QTestState
*from
,
1212 migrate_set_parameter_int(from
, "compress-level", 1);
1213 migrate_set_parameter_int(from
, "compress-threads", 4);
1214 migrate_set_parameter_bool(from
, "compress-wait-thread", true);
1215 migrate_set_parameter_int(to
, "decompress-threads", 4);
1217 migrate_set_capability(from
, "compress", true);
1218 migrate_set_capability(to
, "compress", true);
1224 test_migrate_compress_nowait_start(QTestState
*from
,
1227 migrate_set_parameter_int(from
, "compress-level", 9);
1228 migrate_set_parameter_int(from
, "compress-threads", 1);
1229 migrate_set_parameter_bool(from
, "compress-wait-thread", false);
1230 migrate_set_parameter_int(to
, "decompress-threads", 1);
1232 migrate_set_capability(from
, "compress", true);
1233 migrate_set_capability(to
, "compress", true);
1238 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
1239 QTestState
**to_ptr
,
1240 MigrateCommon
*args
)
1242 QTestState
*from
, *to
;
1244 if (test_migrate_start(&from
, &to
, "defer", &args
->start
)) {
1248 if (args
->start_hook
) {
1249 args
->postcopy_data
= args
->start_hook(from
, to
);
1252 migrate_set_capability(from
, "postcopy-ram", true);
1253 migrate_set_capability(to
, "postcopy-ram", true);
1254 migrate_set_capability(to
, "postcopy-blocktime", true);
1256 if (args
->postcopy_preempt
) {
1257 migrate_set_capability(from
, "postcopy-preempt", true);
1258 migrate_set_capability(to
, "postcopy-preempt", true);
1261 migrate_ensure_non_converge(from
);
1263 migrate_prepare_for_dirty_mem(from
);
1264 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1265 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1267 /* Wait for the first serial output from the source */
1268 wait_for_serial("src_serial");
1270 g_autofree
char *uri
= migrate_get_socket_address(to
, "socket-address");
1271 migrate_qmp(from
, uri
, "{}");
1273 migrate_wait_for_dirty_mem(from
, to
);
1281 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
,
1282 MigrateCommon
*args
)
1284 wait_for_migration_complete(from
);
1286 /* Make sure we get at least one "B" on destination */
1287 wait_for_serial("dest_serial");
1289 if (uffd_feature_thread_id
) {
1293 if (args
->finish_hook
) {
1294 args
->finish_hook(from
, to
, args
->postcopy_data
);
1295 args
->postcopy_data
= NULL
;
1298 test_migrate_end(from
, to
, true);
1301 static void test_postcopy_common(MigrateCommon
*args
)
1303 QTestState
*from
, *to
;
1305 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1308 migrate_postcopy_start(from
, to
);
1309 migrate_postcopy_complete(from
, to
, args
);
1312 static void test_postcopy(void)
1314 MigrateCommon args
= { };
1316 test_postcopy_common(&args
);
1319 static void test_postcopy_compress(void)
1321 MigrateCommon args
= {
1322 .start_hook
= test_migrate_compress_start
1325 test_postcopy_common(&args
);
1328 static void test_postcopy_preempt(void)
1330 MigrateCommon args
= {
1331 .postcopy_preempt
= true,
1334 test_postcopy_common(&args
);
1337 #ifdef CONFIG_GNUTLS
1338 static void test_postcopy_tls_psk(void)
1340 MigrateCommon args
= {
1341 .start_hook
= test_migrate_tls_psk_start_match
,
1342 .finish_hook
= test_migrate_tls_psk_finish
,
1345 test_postcopy_common(&args
);
1348 static void test_postcopy_preempt_tls_psk(void)
1350 MigrateCommon args
= {
1351 .postcopy_preempt
= true,
1352 .start_hook
= test_migrate_tls_psk_start_match
,
1353 .finish_hook
= test_migrate_tls_psk_finish
,
1356 test_postcopy_common(&args
);
1360 static void test_postcopy_recovery_common(MigrateCommon
*args
)
1362 QTestState
*from
, *to
;
1363 g_autofree
char *uri
= NULL
;
1365 /* Always hide errors for postcopy recover tests since they're expected */
1366 args
->start
.hide_stderr
= true;
1368 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
1372 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1373 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
1375 /* Now we start the postcopy */
1376 migrate_postcopy_start(from
, to
);
1379 * Wait until postcopy is really started; we can only run the
1380 * migrate-pause command during a postcopy
1382 wait_for_migration_status(from
, "postcopy-active", NULL
);
1385 * Manually stop the postcopy migration. This emulates a network
1386 * failure with the migration socket
1388 migrate_pause(from
);
1391 * Wait for destination side to reach postcopy-paused state. The
1392 * migrate-recover command can only succeed if destination machine
1393 * is in the paused state
1395 wait_for_migration_status(to
, "postcopy-paused",
1396 (const char * []) { "failed", "active",
1397 "completed", NULL
});
1400 * Create a new socket to emulate a new channel that is different
1401 * from the broken migration channel; tell the destination to
1402 * listen to the new port
1404 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
1405 migrate_recover(to
, uri
);
1408 * Try to rebuild the migration channel using the resume flag and
1409 * the newly created channel
1411 wait_for_migration_status(from
, "postcopy-paused",
1412 (const char * []) { "failed", "active",
1413 "completed", NULL
});
1414 migrate_qmp(from
, uri
, "{'resume': true}");
1416 /* Restore the postcopy bandwidth to unlimited */
1417 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
1419 migrate_postcopy_complete(from
, to
, args
);
1422 static void test_postcopy_recovery(void)
1424 MigrateCommon args
= { };
1426 test_postcopy_recovery_common(&args
);
1429 static void test_postcopy_recovery_compress(void)
1431 MigrateCommon args
= {
1432 .start_hook
= test_migrate_compress_start
1435 test_postcopy_recovery_common(&args
);
1438 #ifdef CONFIG_GNUTLS
1439 static void test_postcopy_recovery_tls_psk(void)
1441 MigrateCommon args
= {
1442 .start_hook
= test_migrate_tls_psk_start_match
,
1443 .finish_hook
= test_migrate_tls_psk_finish
,
1446 test_postcopy_recovery_common(&args
);
1450 static void test_postcopy_preempt_recovery(void)
1452 MigrateCommon args
= {
1453 .postcopy_preempt
= true,
1456 test_postcopy_recovery_common(&args
);
1459 #ifdef CONFIG_GNUTLS
1460 /* This contains preempt+recovery+tls test altogether */
1461 static void test_postcopy_preempt_all(void)
1463 MigrateCommon args
= {
1464 .postcopy_preempt
= true,
1465 .start_hook
= test_migrate_tls_psk_start_match
,
1466 .finish_hook
= test_migrate_tls_psk_finish
,
1469 test_postcopy_recovery_common(&args
);
1474 static void test_baddest(void)
1476 MigrateStart args
= {
1479 QTestState
*from
, *to
;
1481 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", &args
)) {
1484 migrate_qmp(from
, "tcp:127.0.0.1:0", "{}");
1485 wait_for_migration_fail(from
, false);
1486 test_migrate_end(from
, to
, false);
1489 static void test_precopy_common(MigrateCommon
*args
)
1491 QTestState
*from
, *to
;
1492 void *data_hook
= NULL
;
1494 if (test_migrate_start(&from
, &to
, args
->listen_uri
, &args
->start
)) {
1498 if (args
->start_hook
) {
1499 data_hook
= args
->start_hook(from
, to
);
1502 /* Wait for the first serial output from the source */
1503 if (args
->result
== MIG_TEST_SUCCEED
) {
1504 wait_for_serial("src_serial");
1508 migrate_ensure_non_converge(from
);
1509 migrate_prepare_for_dirty_mem(from
);
1512 * Testing non-live migration, we allow it to run at
1513 * full speed to ensure short test case duration.
1514 * For tests expected to fail, we don't need to
1517 if (args
->result
== MIG_TEST_SUCCEED
) {
1518 qtest_qmp_assert_success(from
, "{ 'execute' : 'stop'}");
1519 if (!got_src_stop
) {
1520 qtest_qmp_eventwait(from
, "STOP");
1522 migrate_ensure_converge(from
);
1526 if (!args
->connect_uri
) {
1527 g_autofree
char *local_connect_uri
=
1528 migrate_get_socket_address(to
, "socket-address");
1529 migrate_qmp(from
, local_connect_uri
, "{}");
1531 migrate_qmp(from
, args
->connect_uri
, "{}");
1535 if (args
->result
!= MIG_TEST_SUCCEED
) {
1536 bool allow_active
= args
->result
== MIG_TEST_FAIL
;
1537 wait_for_migration_fail(from
, allow_active
);
1539 if (args
->result
== MIG_TEST_FAIL_DEST_QUIT_ERR
) {
1540 qtest_set_expected_status(to
, EXIT_FAILURE
);
1545 * For initial iteration(s) we must do a full pass,
1546 * but for the final iteration, we need only wait
1547 * for some dirty mem before switching to converge
1549 while (args
->iterations
> 1) {
1550 wait_for_migration_pass(from
);
1553 migrate_wait_for_dirty_mem(from
, to
);
1555 migrate_ensure_converge(from
);
1558 * We do this first, as it has a timeout to stop us
1559 * hanging forever if migration didn't converge
1561 wait_for_migration_complete(from
);
1563 if (!got_src_stop
) {
1564 qtest_qmp_eventwait(from
, "STOP");
1567 wait_for_migration_complete(from
);
1569 * Must wait for dst to finish reading all incoming
1570 * data on the socket before issuing 'cont' otherwise
1573 wait_for_migration_complete(to
);
1575 qtest_qmp_assert_success(to
, "{ 'execute' : 'cont'}");
1578 if (!got_dst_resume
) {
1579 qtest_qmp_eventwait(to
, "RESUME");
1582 wait_for_serial("dest_serial");
1585 if (args
->finish_hook
) {
1586 args
->finish_hook(from
, to
, data_hook
);
1589 test_migrate_end(from
, to
, args
->result
== MIG_TEST_SUCCEED
);
1592 static void test_precopy_unix_plain(void)
1594 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1595 MigrateCommon args
= {
1599 * The simplest use case of precopy, covering smoke tests of
1600 * get-dirty-log dirty tracking.
1605 test_precopy_common(&args
);
1609 static void test_precopy_unix_dirty_ring(void)
1611 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1612 MigrateCommon args
= {
1614 .use_dirty_ring
= true,
1619 * Besides the precopy/unix basic test, cover dirty ring interface
1620 * rather than get-dirty-log.
1625 test_precopy_common(&args
);
1628 #ifdef CONFIG_GNUTLS
1629 static void test_precopy_unix_tls_psk(void)
1631 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1632 MigrateCommon args
= {
1635 .start_hook
= test_migrate_tls_psk_start_match
,
1636 .finish_hook
= test_migrate_tls_psk_finish
,
1639 test_precopy_common(&args
);
1643 static void test_precopy_unix_tls_x509_default_host(void)
1645 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1646 MigrateCommon args
= {
1648 .hide_stderr
= true,
1652 .start_hook
= test_migrate_tls_x509_start_default_host
,
1653 .finish_hook
= test_migrate_tls_x509_finish
,
1654 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1657 test_precopy_common(&args
);
1660 static void test_precopy_unix_tls_x509_override_host(void)
1662 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1663 MigrateCommon args
= {
1666 .start_hook
= test_migrate_tls_x509_start_override_host
,
1667 .finish_hook
= test_migrate_tls_x509_finish
,
1670 test_precopy_common(&args
);
1672 #endif /* CONFIG_TASN1 */
1673 #endif /* CONFIG_GNUTLS */
1676 /* Currently upset on aarch64 TCG */
1677 static void test_ignore_shared(void)
1679 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1680 QTestState
*from
, *to
;
1682 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
1686 migrate_ensure_non_converge(from
);
1687 migrate_prepare_for_dirty_mem(from
);
1689 migrate_set_capability(from
, "x-ignore-shared", true);
1690 migrate_set_capability(to
, "x-ignore-shared", true);
1692 /* Wait for the first serial output from the source */
1693 wait_for_serial("src_serial");
1695 migrate_qmp(from
, uri
, "{}");
1697 migrate_wait_for_dirty_mem(from
, to
);
1699 if (!got_src_stop
) {
1700 qtest_qmp_eventwait(from
, "STOP");
1703 qtest_qmp_eventwait(to
, "RESUME");
1705 wait_for_serial("dest_serial");
1706 wait_for_migration_complete(from
);
1708 /* Check whether shared RAM has been really skipped */
1709 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1711 test_migrate_end(from
, to
, true);
1716 test_migrate_xbzrle_start(QTestState
*from
,
1719 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1721 migrate_set_capability(from
, "xbzrle", true);
1722 migrate_set_capability(to
, "xbzrle", true);
1727 static void test_precopy_unix_xbzrle(void)
1729 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1730 MigrateCommon args
= {
1733 .start_hook
= test_migrate_xbzrle_start
,
1736 * XBZRLE needs pages to be modified when doing the 2nd+ round
1737 * iteration to have real data pushed to the stream.
1742 test_precopy_common(&args
);
1745 static void test_precopy_unix_compress(void)
1747 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1748 MigrateCommon args
= {
1751 .start_hook
= test_migrate_compress_start
,
1753 * Test that no invalid thread state is left over from
1754 * the previous iteration.
1758 * We make sure the compressor can always work well even if guest
1759 * memory is changing. See commit 34ab9e9743 where we used to fix
1760 * a bug when only trigger-able with guest memory changing.
1765 test_precopy_common(&args
);
1768 static void test_precopy_unix_compress_nowait(void)
1770 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1771 MigrateCommon args
= {
1774 .start_hook
= test_migrate_compress_nowait_start
,
1776 * Test that no invalid thread state is left over from
1777 * the previous iteration.
1780 /* Same reason for the wait version of precopy compress test */
1784 test_precopy_common(&args
);
1787 static void test_precopy_tcp_plain(void)
1789 MigrateCommon args
= {
1790 .listen_uri
= "tcp:127.0.0.1:0",
1793 test_precopy_common(&args
);
1796 static void *test_migrate_switchover_ack_start(QTestState
*from
, QTestState
*to
)
1799 migrate_set_capability(from
, "return-path", true);
1800 migrate_set_capability(to
, "return-path", true);
1802 migrate_set_capability(from
, "switchover-ack", true);
1803 migrate_set_capability(to
, "switchover-ack", true);
1808 static void test_precopy_tcp_switchover_ack(void)
1810 MigrateCommon args
= {
1811 .listen_uri
= "tcp:127.0.0.1:0",
1812 .start_hook
= test_migrate_switchover_ack_start
,
1814 * Source VM must be running in order to consider the switchover ACK
1815 * when deciding to do switchover or not.
1820 test_precopy_common(&args
);
1823 #ifdef CONFIG_GNUTLS
1824 static void test_precopy_tcp_tls_psk_match(void)
1826 MigrateCommon args
= {
1827 .listen_uri
= "tcp:127.0.0.1:0",
1828 .start_hook
= test_migrate_tls_psk_start_match
,
1829 .finish_hook
= test_migrate_tls_psk_finish
,
1832 test_precopy_common(&args
);
1835 static void test_precopy_tcp_tls_psk_mismatch(void)
1837 MigrateCommon args
= {
1839 .hide_stderr
= true,
1841 .listen_uri
= "tcp:127.0.0.1:0",
1842 .start_hook
= test_migrate_tls_psk_start_mismatch
,
1843 .finish_hook
= test_migrate_tls_psk_finish
,
1844 .result
= MIG_TEST_FAIL
,
1847 test_precopy_common(&args
);
1851 static void test_precopy_tcp_tls_x509_default_host(void)
1853 MigrateCommon args
= {
1854 .listen_uri
= "tcp:127.0.0.1:0",
1855 .start_hook
= test_migrate_tls_x509_start_default_host
,
1856 .finish_hook
= test_migrate_tls_x509_finish
,
1859 test_precopy_common(&args
);
1862 static void test_precopy_tcp_tls_x509_override_host(void)
1864 MigrateCommon args
= {
1865 .listen_uri
= "tcp:127.0.0.1:0",
1866 .start_hook
= test_migrate_tls_x509_start_override_host
,
1867 .finish_hook
= test_migrate_tls_x509_finish
,
1870 test_precopy_common(&args
);
1873 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1875 MigrateCommon args
= {
1877 .hide_stderr
= true,
1879 .listen_uri
= "tcp:127.0.0.1:0",
1880 .start_hook
= test_migrate_tls_x509_start_mismatch_host
,
1881 .finish_hook
= test_migrate_tls_x509_finish
,
1882 .result
= MIG_TEST_FAIL_DEST_QUIT_ERR
,
1885 test_precopy_common(&args
);
1888 static void test_precopy_tcp_tls_x509_friendly_client(void)
1890 MigrateCommon args
= {
1891 .listen_uri
= "tcp:127.0.0.1:0",
1892 .start_hook
= test_migrate_tls_x509_start_friendly_client
,
1893 .finish_hook
= test_migrate_tls_x509_finish
,
1896 test_precopy_common(&args
);
1899 static void test_precopy_tcp_tls_x509_hostile_client(void)
1901 MigrateCommon args
= {
1903 .hide_stderr
= true,
1905 .listen_uri
= "tcp:127.0.0.1:0",
1906 .start_hook
= test_migrate_tls_x509_start_hostile_client
,
1907 .finish_hook
= test_migrate_tls_x509_finish
,
1908 .result
= MIG_TEST_FAIL
,
1911 test_precopy_common(&args
);
1914 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1916 MigrateCommon args
= {
1917 .listen_uri
= "tcp:127.0.0.1:0",
1918 .start_hook
= test_migrate_tls_x509_start_allow_anon_client
,
1919 .finish_hook
= test_migrate_tls_x509_finish
,
1922 test_precopy_common(&args
);
1925 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1927 MigrateCommon args
= {
1929 .hide_stderr
= true,
1931 .listen_uri
= "tcp:127.0.0.1:0",
1932 .start_hook
= test_migrate_tls_x509_start_reject_anon_client
,
1933 .finish_hook
= test_migrate_tls_x509_finish
,
1934 .result
= MIG_TEST_FAIL
,
1937 test_precopy_common(&args
);
1939 #endif /* CONFIG_TASN1 */
1940 #endif /* CONFIG_GNUTLS */
1943 static void *test_migrate_fd_start_hook(QTestState
*from
,
1949 /* Create two connected sockets for migration */
1950 ret
= qemu_socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1951 g_assert_cmpint(ret
, ==, 0);
1953 /* Send the 1st socket to the target */
1954 qtest_qmp_fds_assert_success(to
, &pair
[0], 1,
1955 "{ 'execute': 'getfd',"
1956 " 'arguments': { 'fdname': 'fd-mig' }}");
1959 /* Start incoming migration from the 1st socket */
1960 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
1961 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1963 /* Send the 2nd socket to the target */
1964 qtest_qmp_fds_assert_success(from
, &pair
[1], 1,
1965 "{ 'execute': 'getfd',"
1966 " 'arguments': { 'fdname': 'fd-mig' }}");
1972 static void test_migrate_fd_finish_hook(QTestState
*from
,
1977 const char *error_desc
;
1979 /* Test closing fds */
1980 /* We assume, that QEMU removes named fd from its list,
1981 * so this should fail */
1982 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1983 " 'arguments': { 'fdname': 'fd-mig' }}");
1984 g_assert_true(qdict_haskey(rsp
, "error"));
1985 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1986 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1989 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1990 " 'arguments': { 'fdname': 'fd-mig' }}");
1991 g_assert_true(qdict_haskey(rsp
, "error"));
1992 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1993 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1997 static void test_migrate_fd_proto(void)
1999 MigrateCommon args
= {
2000 .listen_uri
= "defer",
2001 .connect_uri
= "fd:fd-mig",
2002 .start_hook
= test_migrate_fd_start_hook
,
2003 .finish_hook
= test_migrate_fd_finish_hook
2005 test_precopy_common(&args
);
2009 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
2011 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2012 QTestState
*from
, *to
;
2014 if (test_migrate_start(&from
, &to
, uri
, args
)) {
2019 * UUID validation is at the begin of migration. So, the main process of
2020 * migration is not interesting for us here. Thus, set huge downtime for
2021 * very fast migration.
2023 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
2024 migrate_set_capability(from
, "validate-uuid", true);
2026 /* Wait for the first serial output from the source */
2027 wait_for_serial("src_serial");
2029 migrate_qmp(from
, uri
, "{}");
2032 qtest_set_expected_status(to
, EXIT_FAILURE
);
2033 wait_for_migration_fail(from
, true);
2035 wait_for_migration_complete(from
);
2038 test_migrate_end(from
, to
, false);
2041 static void test_validate_uuid(void)
2043 MigrateStart args
= {
2044 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2045 .opts_target
= "-uuid 11111111-1111-1111-1111-111111111111",
2048 do_test_validate_uuid(&args
, false);
2051 static void test_validate_uuid_error(void)
2053 MigrateStart args
= {
2054 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2055 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2056 .hide_stderr
= true,
2059 do_test_validate_uuid(&args
, true);
2062 static void test_validate_uuid_src_not_set(void)
2064 MigrateStart args
= {
2065 .opts_target
= "-uuid 22222222-2222-2222-2222-222222222222",
2066 .hide_stderr
= true,
2069 do_test_validate_uuid(&args
, false);
2072 static void test_validate_uuid_dst_not_set(void)
2074 MigrateStart args
= {
2075 .opts_source
= "-uuid 11111111-1111-1111-1111-111111111111",
2076 .hide_stderr
= true,
2079 do_test_validate_uuid(&args
, false);
2083 * The way auto_converge works, we need to do too many passes to
2084 * run this test. Auto_converge logic is only run once every
2085 * three iterations, so:
2087 * - 3 iterations without auto_converge enabled
2088 * - 3 iterations with pct = 5
2089 * - 3 iterations with pct = 30
2090 * - 3 iterations with pct = 55
2091 * - 3 iterations with pct = 80
2092 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2094 * To make things even worse, we need to run the initial stage at
2095 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2097 static void test_migrate_auto_converge(void)
2099 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
2100 MigrateStart args
= {};
2101 QTestState
*from
, *to
;
2105 * We want the test to be stable and as fast as possible.
2106 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2107 * so we need to decrease a bandwidth.
2109 const int64_t init_pct
= 5, inc_pct
= 25, max_pct
= 95;
2111 if (test_migrate_start(&from
, &to
, uri
, &args
)) {
2115 migrate_set_capability(from
, "auto-converge", true);
2116 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
2117 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
2118 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
2121 * Set the initial parameters so that the migration could not converge
2122 * without throttling.
2124 migrate_ensure_non_converge(from
);
2126 /* To check remaining size after precopy */
2127 migrate_set_capability(from
, "pause-before-switchover", true);
2129 /* Wait for the first serial output from the source */
2130 wait_for_serial("src_serial");
2132 migrate_qmp(from
, uri
, "{}");
2134 /* Wait for throttling begins */
2137 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2138 if (percentage
!= 0) {
2142 g_assert_false(got_src_stop
);
2144 /* The first percentage of throttling should be at least init_pct */
2145 g_assert_cmpint(percentage
, >=, init_pct
);
2146 /* Now, when we tested that throttling works, let it converge */
2147 migrate_ensure_converge(from
);
2150 * Wait for pre-switchover status to check last throttle percentage
2151 * and remaining. These values will be zeroed later
2153 wait_for_migration_status(from
, "pre-switchover", NULL
);
2155 /* The final percentage of throttling shouldn't be greater than max_pct */
2156 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
2157 g_assert_cmpint(percentage
, <=, max_pct
);
2158 migrate_continue(from
, "pre-switchover");
2160 qtest_qmp_eventwait(to
, "RESUME");
2162 wait_for_serial("dest_serial");
2163 wait_for_migration_complete(from
);
2165 test_migrate_end(from
, to
, true);
2169 test_migrate_precopy_tcp_multifd_start_common(QTestState
*from
,
2173 migrate_set_parameter_int(from
, "multifd-channels", 16);
2174 migrate_set_parameter_int(to
, "multifd-channels", 16);
2176 migrate_set_parameter_str(from
, "multifd-compression", method
);
2177 migrate_set_parameter_str(to
, "multifd-compression", method
);
2179 migrate_set_capability(from
, "multifd", true);
2180 migrate_set_capability(to
, "multifd", true);
2182 /* Start incoming migration from the 1st socket */
2183 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
2184 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2190 test_migrate_precopy_tcp_multifd_start(QTestState
*from
,
2193 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2197 test_migrate_precopy_tcp_multifd_zlib_start(QTestState
*from
,
2200 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zlib");
2205 test_migrate_precopy_tcp_multifd_zstd_start(QTestState
*from
,
2208 return test_migrate_precopy_tcp_multifd_start_common(from
, to
, "zstd");
2210 #endif /* CONFIG_ZSTD */
2212 static void test_multifd_tcp_none(void)
2214 MigrateCommon args
= {
2215 .listen_uri
= "defer",
2216 .start_hook
= test_migrate_precopy_tcp_multifd_start
,
2218 * Multifd is more complicated than most of the features, it
2219 * directly takes guest page buffers when sending, make sure
2220 * everything will work alright even if guest page is changing.
2224 test_precopy_common(&args
);
2227 static void test_multifd_tcp_zlib(void)
2229 MigrateCommon args
= {
2230 .listen_uri
= "defer",
2231 .start_hook
= test_migrate_precopy_tcp_multifd_zlib_start
,
2233 test_precopy_common(&args
);
2237 static void test_multifd_tcp_zstd(void)
2239 MigrateCommon args
= {
2240 .listen_uri
= "defer",
2241 .start_hook
= test_migrate_precopy_tcp_multifd_zstd_start
,
2243 test_precopy_common(&args
);
2247 #ifdef CONFIG_GNUTLS
2249 test_migrate_multifd_tcp_tls_psk_start_match(QTestState
*from
,
2252 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2253 return test_migrate_tls_psk_start_match(from
, to
);
2257 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState
*from
,
2260 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2261 return test_migrate_tls_psk_start_mismatch(from
, to
);
2266 test_migrate_multifd_tls_x509_start_default_host(QTestState
*from
,
2269 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2270 return test_migrate_tls_x509_start_default_host(from
, to
);
2274 test_migrate_multifd_tls_x509_start_override_host(QTestState
*from
,
2277 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2278 return test_migrate_tls_x509_start_override_host(from
, to
);
2282 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState
*from
,
2285 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2286 return test_migrate_tls_x509_start_mismatch_host(from
, to
);
2290 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState
*from
,
2293 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2294 return test_migrate_tls_x509_start_allow_anon_client(from
, to
);
2298 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState
*from
,
2301 test_migrate_precopy_tcp_multifd_start_common(from
, to
, "none");
2302 return test_migrate_tls_x509_start_reject_anon_client(from
, to
);
2304 #endif /* CONFIG_TASN1 */
2306 static void test_multifd_tcp_tls_psk_match(void)
2308 MigrateCommon args
= {
2309 .listen_uri
= "defer",
2310 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_match
,
2311 .finish_hook
= test_migrate_tls_psk_finish
,
2313 test_precopy_common(&args
);
2316 static void test_multifd_tcp_tls_psk_mismatch(void)
2318 MigrateCommon args
= {
2320 .hide_stderr
= true,
2322 .listen_uri
= "defer",
2323 .start_hook
= test_migrate_multifd_tcp_tls_psk_start_mismatch
,
2324 .finish_hook
= test_migrate_tls_psk_finish
,
2325 .result
= MIG_TEST_FAIL
,
2327 test_precopy_common(&args
);
2331 static void test_multifd_tcp_tls_x509_default_host(void)
2333 MigrateCommon args
= {
2334 .listen_uri
= "defer",
2335 .start_hook
= test_migrate_multifd_tls_x509_start_default_host
,
2336 .finish_hook
= test_migrate_tls_x509_finish
,
2338 test_precopy_common(&args
);
2341 static void test_multifd_tcp_tls_x509_override_host(void)
2343 MigrateCommon args
= {
2344 .listen_uri
= "defer",
2345 .start_hook
= test_migrate_multifd_tls_x509_start_override_host
,
2346 .finish_hook
= test_migrate_tls_x509_finish
,
2348 test_precopy_common(&args
);
2351 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2354 * This has different behaviour to the non-multifd case.
2356 * In non-multifd case when client aborts due to mismatched
2357 * cert host, the server has already started trying to load
2358 * migration state, and so it exits with I/O failure.
2360 * In multifd case when client aborts due to mismatched
2361 * cert host, the server is still waiting for the other
2362 * multifd connections to arrive so hasn't started trying
2363 * to load migration state, and thus just aborts the migration
2366 MigrateCommon args
= {
2368 .hide_stderr
= true,
2370 .listen_uri
= "defer",
2371 .start_hook
= test_migrate_multifd_tls_x509_start_mismatch_host
,
2372 .finish_hook
= test_migrate_tls_x509_finish
,
2373 .result
= MIG_TEST_FAIL
,
2375 test_precopy_common(&args
);
2378 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2380 MigrateCommon args
= {
2381 .listen_uri
= "defer",
2382 .start_hook
= test_migrate_multifd_tls_x509_start_allow_anon_client
,
2383 .finish_hook
= test_migrate_tls_x509_finish
,
2385 test_precopy_common(&args
);
2388 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2390 MigrateCommon args
= {
2392 .hide_stderr
= true,
2394 .listen_uri
= "defer",
2395 .start_hook
= test_migrate_multifd_tls_x509_start_reject_anon_client
,
2396 .finish_hook
= test_migrate_tls_x509_finish
,
2397 .result
= MIG_TEST_FAIL
,
2399 test_precopy_common(&args
);
2401 #endif /* CONFIG_TASN1 */
2402 #endif /* CONFIG_GNUTLS */
2410 * launch another target
2413 * And see that it works
2415 static void test_multifd_tcp_cancel(void)
2417 MigrateStart args
= {
2418 .hide_stderr
= true,
2420 QTestState
*from
, *to
, *to2
;
2421 g_autofree
char *uri
= NULL
;
2423 if (test_migrate_start(&from
, &to
, "defer", &args
)) {
2427 migrate_ensure_non_converge(from
);
2428 migrate_prepare_for_dirty_mem(from
);
2430 migrate_set_parameter_int(from
, "multifd-channels", 16);
2431 migrate_set_parameter_int(to
, "multifd-channels", 16);
2433 migrate_set_capability(from
, "multifd", true);
2434 migrate_set_capability(to
, "multifd", true);
2436 /* Start incoming migration from the 1st socket */
2437 qtest_qmp_assert_success(to
, "{ 'execute': 'migrate-incoming',"
2438 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2440 /* Wait for the first serial output from the source */
2441 wait_for_serial("src_serial");
2443 uri
= migrate_get_socket_address(to
, "socket-address");
2445 migrate_qmp(from
, uri
, "{}");
2447 migrate_wait_for_dirty_mem(from
, to
);
2449 migrate_cancel(from
);
2451 /* Make sure QEMU process "to" exited */
2452 qtest_set_expected_status(to
, EXIT_FAILURE
);
2453 qtest_wait_qemu(to
);
2455 args
= (MigrateStart
){
2456 .only_target
= true,
2459 if (test_migrate_start(&from
, &to2
, "defer", &args
)) {
2463 migrate_set_parameter_int(to2
, "multifd-channels", 16);
2465 migrate_set_capability(to2
, "multifd", true);
2467 /* Start incoming migration from the 1st socket */
2468 qtest_qmp_assert_success(to2
, "{ 'execute': 'migrate-incoming',"
2469 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2472 uri
= migrate_get_socket_address(to2
, "socket-address");
2474 wait_for_migration_status(from
, "cancelled", NULL
);
2476 migrate_ensure_non_converge(from
);
2478 migrate_qmp(from
, uri
, "{}");
2480 migrate_wait_for_dirty_mem(from
, to2
);
2482 migrate_ensure_converge(from
);
2484 if (!got_src_stop
) {
2485 qtest_qmp_eventwait(from
, "STOP");
2487 qtest_qmp_eventwait(to2
, "RESUME");
2489 wait_for_serial("dest_serial");
2490 wait_for_migration_complete(from
);
2491 test_migrate_end(from
, to2
, true);
2494 static void calc_dirty_rate(QTestState
*who
, uint64_t calc_time
)
2496 qtest_qmp_assert_success(who
,
2497 "{ 'execute': 'calc-dirty-rate',"
2499 "'calc-time': %" PRIu64
","
2500 "'mode': 'dirty-ring' }}",
2504 static QDict
*query_dirty_rate(QTestState
*who
)
2506 return qtest_qmp_assert_success_ref(who
,
2507 "{ 'execute': 'query-dirty-rate' }");
2510 static void dirtylimit_set_all(QTestState
*who
, uint64_t dirtyrate
)
2512 qtest_qmp_assert_success(who
,
2513 "{ 'execute': 'set-vcpu-dirty-limit',"
2515 "'dirty-rate': %" PRIu64
" } }",
2519 static void cancel_vcpu_dirty_limit(QTestState
*who
)
2521 qtest_qmp_assert_success(who
,
2522 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2525 static QDict
*query_vcpu_dirty_limit(QTestState
*who
)
2529 rsp
= qtest_qmp(who
, "{ 'execute': 'query-vcpu-dirty-limit' }");
2530 g_assert(!qdict_haskey(rsp
, "error"));
2531 g_assert(qdict_haskey(rsp
, "return"));
2536 static bool calc_dirtyrate_ready(QTestState
*who
)
2541 rsp_return
= query_dirty_rate(who
);
2542 g_assert(rsp_return
);
2544 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2547 return g_strcmp0(status
, "measuring");
2550 static void wait_for_calc_dirtyrate_complete(QTestState
*who
,
2553 int max_try_count
= 10000;
2554 usleep(time_s
* 1000000);
2556 while (!calc_dirtyrate_ready(who
) && max_try_count
--) {
2561 * Set the timeout with 10 s(max_try_count * 1000us),
2562 * if dirtyrate measurement not complete, fail test.
2564 g_assert_cmpint(max_try_count
, !=, 0);
2567 static int64_t get_dirty_rate(QTestState
*who
)
2572 const QListEntry
*entry
;
2576 rsp_return
= query_dirty_rate(who
);
2577 g_assert(rsp_return
);
2579 status
= g_strdup(qdict_get_str(rsp_return
, "status"));
2581 g_assert_cmpstr(status
, ==, "measured");
2583 rates
= qdict_get_qlist(rsp_return
, "vcpu-dirty-rate");
2584 g_assert(rates
&& !qlist_empty(rates
));
2586 entry
= qlist_first(rates
);
2589 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2592 dirtyrate
= qdict_get_try_int(rate
, "dirty-rate", -1);
2594 qobject_unref(rsp_return
);
2598 static int64_t get_limit_rate(QTestState
*who
)
2602 const QListEntry
*entry
;
2606 rsp_return
= query_vcpu_dirty_limit(who
);
2607 g_assert(rsp_return
);
2609 rates
= qdict_get_qlist(rsp_return
, "return");
2610 g_assert(rates
&& !qlist_empty(rates
));
2612 entry
= qlist_first(rates
);
2615 rate
= qobject_to(QDict
, qlist_entry_obj(entry
));
2618 dirtyrate
= qdict_get_try_int(rate
, "limit-rate", -1);
2620 qobject_unref(rsp_return
);
2624 static QTestState
*dirtylimit_start_vm(void)
2626 QTestState
*vm
= NULL
;
2627 g_autofree gchar
*cmd
= NULL
;
2628 const char *arch
= qtest_get_arch();
2629 g_autofree
char *bootpath
= NULL
;
2631 assert((strcmp(arch
, "x86_64") == 0));
2632 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
2633 assert(sizeof(x86_bootsect
) == 512);
2634 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
2636 cmd
= g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2637 "-name dirtylimit-test,debug-threads=on "
2639 "-serial file:%s/vm_serial "
2640 "-drive file=%s,format=raw ",
2643 vm
= qtest_init(cmd
);
2647 static void dirtylimit_stop_vm(QTestState
*vm
)
2650 cleanup("bootsect");
2651 cleanup("vm_serial");
2654 static void test_vcpu_dirty_limit(void)
2657 int64_t origin_rate
;
2660 int max_try_count
= 20;
2663 /* Start vm for vcpu dirtylimit test */
2664 vm
= dirtylimit_start_vm();
2666 /* Wait for the first serial output from the vm*/
2667 wait_for_serial("vm_serial");
2669 /* Do dirtyrate measurement with calc time equals 1s */
2670 calc_dirty_rate(vm
, 1);
2672 /* Sleep calc time and wait for calc dirtyrate complete */
2673 wait_for_calc_dirtyrate_complete(vm
, 1);
2675 /* Query original dirty page rate */
2676 origin_rate
= get_dirty_rate(vm
);
2678 /* VM booted from bootsect should dirty memory steadily */
2679 assert(origin_rate
!= 0);
2681 /* Setup quota dirty page rate at half of origin */
2682 quota_rate
= origin_rate
/ 2;
2684 /* Set dirtylimit */
2685 dirtylimit_set_all(vm
, quota_rate
);
2688 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2691 g_assert_cmpint(quota_rate
, ==, get_limit_rate(vm
));
2693 /* Sleep a bit to check if it take effect */
2697 * Check if dirtylimit take effect realistically, set the
2698 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2699 * doesn't take effect, fail test.
2701 while (--max_try_count
) {
2702 calc_dirty_rate(vm
, 1);
2703 wait_for_calc_dirtyrate_complete(vm
, 1);
2704 rate
= get_dirty_rate(vm
);
2707 * Assume hitting if current rate is less
2708 * than quota rate (within accepting error)
2710 if (rate
< (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2716 g_assert_cmpint(hit
, ==, 1);
2721 /* Check if dirtylimit cancellation take effect */
2722 cancel_vcpu_dirty_limit(vm
);
2723 while (--max_try_count
) {
2724 calc_dirty_rate(vm
, 1);
2725 wait_for_calc_dirtyrate_complete(vm
, 1);
2726 rate
= get_dirty_rate(vm
);
2729 * Assume dirtylimit be canceled if current rate is
2730 * greater than quota rate (within accepting error)
2732 if (rate
> (quota_rate
+ DIRTYLIMIT_TOLERANCE_RANGE
)) {
2738 g_assert_cmpint(hit
, ==, 1);
2739 dirtylimit_stop_vm(vm
);
2742 static bool kvm_dirty_ring_supported(void)
2744 #if defined(__linux__) && defined(HOST_X86_64)
2745 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
2751 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
2754 /* We test with 4096 slots */
2765 int main(int argc
, char **argv
)
2767 bool has_kvm
, has_tcg
;
2770 g_autoptr(GError
) err
= NULL
;
2773 g_test_init(&argc
, &argv
, NULL
);
2775 has_kvm
= qtest_has_accel("kvm");
2776 has_tcg
= qtest_has_accel("tcg");
2778 if (!has_tcg
&& !has_kvm
) {
2779 g_test_skip("No KVM or TCG accelerator available");
2783 has_uffd
= ufd_version_check();
2784 arch
= qtest_get_arch();
2787 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2788 * is touchy due to race conditions on dirty bits (especially on PPC for
2791 if (g_str_equal(arch
, "ppc64") &&
2792 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
2793 g_test_message("Skipping test: kvm_hv not available");
2794 return g_test_run();
2798 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2799 * there until the problems are resolved
2801 if (g_str_equal(arch
, "s390x") && !has_kvm
) {
2802 g_test_message("Skipping test: s390x host with KVM is required");
2803 return g_test_run();
2806 tmpfs
= g_dir_make_tmp("migration-test-XXXXXX", &err
);
2808 g_test_message("Can't create temporary directory in %s: %s",
2809 g_get_tmp_dir(), err
->message
);
2813 module_call_init(MODULE_INIT_QOM
);
2816 qtest_add_func("/migration/postcopy/plain", test_postcopy
);
2817 qtest_add_func("/migration/postcopy/recovery/plain",
2818 test_postcopy_recovery
);
2819 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt
);
2820 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2821 test_postcopy_preempt_recovery
);
2822 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2823 qtest_add_func("/migration/postcopy/compress/plain",
2824 test_postcopy_compress
);
2825 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2826 test_postcopy_recovery_compress
);
2830 qtest_add_func("/migration/bad_dest", test_baddest
);
2831 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain
);
2832 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle
);
2834 * Compression fails from time to time.
2835 * Put test here but don't enable it until everything is fixed.
2837 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2838 qtest_add_func("/migration/precopy/unix/compress/wait",
2839 test_precopy_unix_compress
);
2840 qtest_add_func("/migration/precopy/unix/compress/nowait",
2841 test_precopy_unix_compress_nowait
);
2843 #ifdef CONFIG_GNUTLS
2844 qtest_add_func("/migration/precopy/unix/tls/psk",
2845 test_precopy_unix_tls_psk
);
2849 * NOTE: psk test is enough for postcopy, as other types of TLS
2850 * channels are tested under precopy. Here what we want to test is the
2851 * general postcopy path that has TLS channel enabled.
2853 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk
);
2854 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2855 test_postcopy_recovery_tls_psk
);
2856 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2857 test_postcopy_preempt_tls_psk
);
2858 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2859 test_postcopy_preempt_all
);
2862 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2863 test_precopy_unix_tls_x509_default_host
);
2864 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2865 test_precopy_unix_tls_x509_override_host
);
2866 #endif /* CONFIG_TASN1 */
2867 #endif /* CONFIG_GNUTLS */
2869 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain
);
2871 qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
2872 test_precopy_tcp_switchover_ack
);
2874 #ifdef CONFIG_GNUTLS
2875 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2876 test_precopy_tcp_tls_psk_match
);
2877 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2878 test_precopy_tcp_tls_psk_mismatch
);
2880 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2881 test_precopy_tcp_tls_x509_default_host
);
2882 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2883 test_precopy_tcp_tls_x509_override_host
);
2884 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2885 test_precopy_tcp_tls_x509_mismatch_host
);
2886 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2887 test_precopy_tcp_tls_x509_friendly_client
);
2888 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2889 test_precopy_tcp_tls_x509_hostile_client
);
2890 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2891 test_precopy_tcp_tls_x509_allow_anon_client
);
2892 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2893 test_precopy_tcp_tls_x509_reject_anon_client
);
2894 #endif /* CONFIG_TASN1 */
2895 #endif /* CONFIG_GNUTLS */
2897 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2899 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
2901 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
2902 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
2903 qtest_add_func("/migration/validate_uuid_src_not_set",
2904 test_validate_uuid_src_not_set
);
2905 qtest_add_func("/migration/validate_uuid_dst_not_set",
2906 test_validate_uuid_dst_not_set
);
2908 * See explanation why this test is slow on function definition
2910 if (g_test_slow()) {
2911 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
2913 qtest_add_func("/migration/multifd/tcp/plain/none",
2914 test_multifd_tcp_none
);
2916 * This test is flaky and sometimes fails in CI and otherwise:
2917 * don't run unless user opts in via environment variable.
2919 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2920 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2921 test_multifd_tcp_cancel
);
2923 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2924 test_multifd_tcp_zlib
);
2926 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2927 test_multifd_tcp_zstd
);
2929 #ifdef CONFIG_GNUTLS
2930 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2931 test_multifd_tcp_tls_psk_match
);
2932 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2933 test_multifd_tcp_tls_psk_mismatch
);
2935 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2936 test_multifd_tcp_tls_x509_default_host
);
2937 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2938 test_multifd_tcp_tls_x509_override_host
);
2939 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2940 test_multifd_tcp_tls_x509_mismatch_host
);
2941 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2942 test_multifd_tcp_tls_x509_allow_anon_client
);
2943 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2944 test_multifd_tcp_tls_x509_reject_anon_client
);
2945 #endif /* CONFIG_TASN1 */
2946 #endif /* CONFIG_GNUTLS */
2948 if (g_str_equal(arch
, "x86_64") && has_kvm
&& kvm_dirty_ring_supported()) {
2949 qtest_add_func("/migration/dirty_ring",
2950 test_precopy_unix_dirty_ring
);
2951 qtest_add_func("/migration/vcpu_dirty_limit",
2952 test_vcpu_dirty_limit
);
2957 g_assert_cmpint(ret
, ==, 0);
2961 g_test_message("unable to rmdir: path (%s): %s",
2962 tmpfs
, strerror(errno
));