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"
15 #include "libqos/libqtest.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"
27 #include "migration-helpers.h"
28 #include "tests/migration/migration-test.h"
30 /* For dirty ring test; so far only x86_64 is supported */
31 #if defined(__linux__) && defined(HOST_X86_64)
32 #include "linux/kvm.h"
35 /* TODO actually test the results and get rid of this */
36 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
38 unsigned start_address
;
40 static bool uffd_feature_thread_id
;
42 /* A downtime where the test really should converge */
43 #define CONVERGE_DOWNTIME 1000
45 #if defined(__linux__)
46 #include <sys/syscall.h>
50 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
51 #include <sys/eventfd.h>
52 #include <sys/ioctl.h>
53 #include <linux/userfaultfd.h>
55 static bool ufd_version_check(void)
57 struct uffdio_api api_struct
;
60 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
63 g_test_message("Skipping test: userfaultfd not available");
67 api_struct
.api
= UFFD_API
;
68 api_struct
.features
= 0;
69 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
70 g_test_message("Skipping test: UFFDIO_API failed");
73 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
75 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
76 (__u64
)1 << _UFFDIO_UNREGISTER
;
77 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
78 g_test_message("Skipping test: Missing userfault feature");
86 static bool ufd_version_check(void)
88 g_test_message("Skipping test: Userfault not available (builtdtime)");
94 static const char *tmpfs
;
96 /* The boot file modifies memory area in [start_address, end_address)
97 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
99 #include "tests/migration/i386/a-b-bootblock.h"
100 #include "tests/migration/aarch64/a-b-kernel.h"
101 #include "tests/migration/s390x/a-b-bios.h"
103 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
105 FILE *bootfile
= fopen(bootpath
, "wb");
107 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
112 * Wait for some output in the serial output file,
113 * we get an 'A' followed by an endless string of 'B's
114 * but on the destination we won't have the A.
116 static void wait_for_serial(const char *side
)
118 g_autofree
char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
119 FILE *serialfile
= fopen(serialpath
, "r");
120 const char *arch
= qtest_get_arch();
121 int started
= (strcmp(side
, "src_serial") == 0 &&
122 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
125 int readvalue
= fgetc(serialfile
);
128 /* SLOF prints its banner before starting test,
129 * to ignore it, mark the start of the test with '_',
130 * ignore all characters until this marker
137 fseek(serialfile
, 0, SEEK_SET
);
154 started
= (strcmp(side
, "src_serial") == 0 &&
155 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
156 fseek(serialfile
, 0, SEEK_SET
);
161 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
162 g_assert_not_reached();
168 * It's tricky to use qemu's migration event capability with qtest,
169 * events suddenly appearing confuse the qmp()/hmp() responses.
172 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
174 QDict
*rsp_return
, *rsp_ram
;
177 rsp_return
= migrate_query(who
);
178 if (!qdict_haskey(rsp_return
, "ram")) {
182 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
183 result
= qdict_get_try_int(rsp_ram
, property
, 0);
185 qobject_unref(rsp_return
);
189 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
194 rsp_return
= migrate_query(who
);
195 result
= qdict_get_try_int(rsp_return
, property
, 0);
196 qobject_unref(rsp_return
);
200 static uint64_t get_migration_pass(QTestState
*who
)
202 return read_ram_property_int(who
, "dirty-sync-count");
205 static void read_blocktime(QTestState
*who
)
209 rsp_return
= migrate_query(who
);
210 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
211 qobject_unref(rsp_return
);
214 static void wait_for_migration_pass(QTestState
*who
)
216 uint64_t initial_pass
= get_migration_pass(who
);
219 /* Wait for the 1st sync */
220 while (!got_stop
&& !initial_pass
) {
222 initial_pass
= get_migration_pass(who
);
227 pass
= get_migration_pass(who
);
228 } while (pass
== initial_pass
&& !got_stop
);
231 static void check_guests_ram(QTestState
*who
)
233 /* Our ASM test will have been incrementing one byte from each page from
234 * start_address to < end_address in order. This gives us a constraint
235 * that any page's byte should be equal or less than the previous pages
236 * byte (mod 256); and they should all be equal except for one transition
237 * at the point where we meet the incrementer. (We're running this with
238 * the guest stopped).
243 bool hit_edge
= false;
246 qtest_memread(who
, start_address
, &first_byte
, 1);
247 last_byte
= first_byte
;
249 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
250 address
+= TEST_MEM_PAGE_SIZE
)
253 qtest_memread(who
, address
, &b
, 1);
254 if (b
!= last_byte
) {
255 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
256 /* This is OK, the guest stopped at the point of
257 * incrementing the previous page but didn't get
265 fprintf(stderr
, "Memory content inconsistency at %x"
266 " first_byte = %x last_byte = %x current = %x"
268 address
, first_byte
, last_byte
, b
, hit_edge
);
274 fprintf(stderr
, "and in another %d pages", bad
- 10);
279 static void cleanup(const char *filename
)
281 g_autofree
char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
286 static char *SocketAddress_to_str(SocketAddress
*addr
)
288 switch (addr
->type
) {
289 case SOCKET_ADDRESS_TYPE_INET
:
290 return g_strdup_printf("tcp:%s:%s",
293 case SOCKET_ADDRESS_TYPE_UNIX
:
294 return g_strdup_printf("unix:%s",
295 addr
->u
.q_unix
.path
);
296 case SOCKET_ADDRESS_TYPE_FD
:
297 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
298 case SOCKET_ADDRESS_TYPE_VSOCK
:
299 return g_strdup_printf("tcp:%s:%s",
303 return g_strdup("unknown address type");
307 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
311 SocketAddressList
*addrs
;
315 rsp
= migrate_query(who
);
316 object
= qdict_get(rsp
, parameter
);
318 iv
= qobject_input_visitor_new(object
);
319 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
322 /* we are only using a single address */
323 result
= SocketAddress_to_str(addrs
->value
);
325 qapi_free_SocketAddressList(addrs
);
330 static long long migrate_get_parameter_int(QTestState
*who
,
331 const char *parameter
)
336 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
337 result
= qdict_get_int(rsp
, parameter
);
342 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
347 result
= migrate_get_parameter_int(who
, parameter
);
348 g_assert_cmpint(result
, ==, value
);
351 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
357 "{ 'execute': 'migrate-set-parameters',"
358 "'arguments': { %s: %lld } }",
360 g_assert(qdict_haskey(rsp
, "return"));
362 migrate_check_parameter_int(who
, parameter
, value
);
365 static char *migrate_get_parameter_str(QTestState
*who
,
366 const char *parameter
)
371 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
372 result
= g_strdup(qdict_get_str(rsp
, parameter
));
377 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
380 g_autofree
char *result
= migrate_get_parameter_str(who
, parameter
);
381 g_assert_cmpstr(result
, ==, value
);
384 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
390 "{ 'execute': 'migrate-set-parameters',"
391 "'arguments': { %s: %s } }",
393 g_assert(qdict_haskey(rsp
, "return"));
395 migrate_check_parameter_str(who
, parameter
, value
);
398 static void migrate_pause(QTestState
*who
)
402 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
406 static void migrate_continue(QTestState
*who
, const char *state
)
410 rsp
= wait_command(who
,
411 "{ 'execute': 'migrate-continue',"
412 " 'arguments': { 'state': %s } }",
417 static void migrate_recover(QTestState
*who
, const char *uri
)
421 rsp
= wait_command(who
,
422 "{ 'execute': 'migrate-recover', "
423 " 'id': 'recover-cmd', "
424 " 'arguments': { 'uri': %s } }",
429 static void migrate_cancel(QTestState
*who
)
433 rsp
= wait_command(who
, "{ 'execute': 'migrate_cancel' }");
437 static void migrate_set_capability(QTestState
*who
, const char *capability
,
443 "{ 'execute': 'migrate-set-capabilities',"
445 "'capabilities': [ { "
446 "'capability': %s, 'state': %i } ] } }",
448 g_assert(qdict_haskey(rsp
, "return"));
452 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
456 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
460 qtest_qmp_eventwait(from
, "STOP");
463 qtest_qmp_eventwait(to
, "RESUME");
468 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
469 * unconditionally, because it means the user would like to be verbose.
473 /* only launch the target process */
475 /* Use dirty ring if true; dirty logging otherwise */
481 static MigrateStart
*migrate_start_new(void)
483 MigrateStart
*args
= g_new0(MigrateStart
, 1);
485 args
->opts_source
= g_strdup("");
486 args
->opts_target
= g_strdup("");
490 static void migrate_start_destroy(MigrateStart
*args
)
492 g_free(args
->opts_source
);
493 g_free(args
->opts_target
);
497 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
498 const char *uri
, MigrateStart
*args
)
500 g_autofree gchar
*arch_source
= NULL
;
501 g_autofree gchar
*arch_target
= NULL
;
502 g_autofree gchar
*cmd_source
= NULL
;
503 g_autofree gchar
*cmd_target
= NULL
;
504 const gchar
*ignore_stderr
;
505 g_autofree
char *bootpath
= NULL
;
506 g_autofree
char *shmem_opts
= NULL
;
507 g_autofree
char *shmem_path
= NULL
;
508 const char *arch
= qtest_get_arch();
509 const char *machine_opts
= NULL
;
510 const char *memory_size
;
513 if (args
->use_shmem
) {
514 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
515 g_test_skip("/dev/shm is not supported");
522 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
523 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
524 /* the assembled x86 boot sector should be exactly one sector large */
525 assert(sizeof(x86_bootsect
) == 512);
526 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
527 memory_size
= "150M";
528 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
529 arch_target
= g_strdup(arch_source
);
530 start_address
= X86_TEST_MEM_START
;
531 end_address
= X86_TEST_MEM_END
;
532 } else if (g_str_equal(arch
, "s390x")) {
533 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
534 memory_size
= "128M";
535 arch_source
= g_strdup_printf("-bios %s", bootpath
);
536 arch_target
= g_strdup(arch_source
);
537 start_address
= S390_TEST_MEM_START
;
538 end_address
= S390_TEST_MEM_END
;
539 } else if (strcmp(arch
, "ppc64") == 0) {
540 machine_opts
= "vsmt=8";
541 memory_size
= "256M";
542 start_address
= PPC_TEST_MEM_START
;
543 end_address
= PPC_TEST_MEM_END
;
544 arch_source
= g_strdup_printf("-nodefaults "
545 "-prom-env 'use-nvramrc?=true' -prom-env "
546 "'nvramrc=hex .\" _\" begin %x %x "
547 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
548 "until'", end_address
, start_address
);
549 arch_target
= g_strdup("");
550 } else if (strcmp(arch
, "aarch64") == 0) {
551 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
552 machine_opts
= "virt,gic-version=max";
553 memory_size
= "150M";
554 arch_source
= g_strdup_printf("-cpu max "
557 arch_target
= g_strdup(arch_source
);
558 start_address
= ARM_TEST_MEM_START
;
559 end_address
= ARM_TEST_MEM_END
;
561 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
563 g_assert_not_reached();
566 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
567 ignore_stderr
= "2>/dev/null";
572 if (args
->use_shmem
) {
573 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
574 shmem_opts
= g_strdup_printf(
575 "-object memory-backend-file,id=mem0,size=%s"
576 ",mem-path=%s,share=on -numa node,memdev=mem0",
577 memory_size
, shmem_path
);
580 shmem_opts
= g_strdup("");
583 cmd_source
= g_strdup_printf("-accel kvm%s -accel tcg%s%s "
584 "-name source,debug-threads=on "
586 "-serial file:%s/src_serial "
588 args
->use_dirty_ring
?
589 ",dirty-ring-size=4096" : "",
590 machine_opts
? " -machine " : "",
591 machine_opts
? machine_opts
: "",
593 arch_source
, shmem_opts
, args
->opts_source
,
595 if (!args
->only_target
) {
596 *from
= qtest_init(cmd_source
);
599 cmd_target
= g_strdup_printf("-accel kvm%s -accel tcg%s%s "
600 "-name target,debug-threads=on "
602 "-serial file:%s/dest_serial "
605 args
->use_dirty_ring
?
606 ",dirty-ring-size=4096" : "",
607 machine_opts
? " -machine " : "",
608 machine_opts
? machine_opts
: "",
609 memory_size
, tmpfs
, uri
,
610 arch_target
, shmem_opts
,
611 args
->opts_target
, ignore_stderr
);
612 *to
= qtest_init(cmd_target
);
615 * Remove shmem file immediately to avoid memory leak in test failed case.
616 * It's valid becase QEMU has already opened this file
618 if (args
->use_shmem
) {
623 migrate_start_destroy(args
);
627 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
629 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
634 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
636 /* Destination still running, wait for a byte to change */
638 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
640 } while (dest_byte_a
== dest_byte_b
);
642 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
644 /* With it stopped, check nothing changes */
645 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
647 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
648 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
650 check_guests_ram(to
);
656 cleanup("migsocket");
657 cleanup("src_serial");
658 cleanup("dest_serial");
661 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
665 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
666 QTestState
*from
, *to
;
668 if (test_migrate_start(&from
, &to
, uri
, args
)) {
672 migrate_set_capability(from
, "postcopy-ram", true);
673 migrate_set_capability(to
, "postcopy-ram", true);
674 migrate_set_capability(to
, "postcopy-blocktime", true);
676 /* We want to pick a speed slow enough that the test completes
677 * quickly, but that it doesn't complete precopy even on a slow
678 * machine, so also set the downtime.
680 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
681 migrate_set_parameter_int(from
, "downtime-limit", 1);
683 /* Wait for the first serial output from the source */
684 wait_for_serial("src_serial");
686 migrate_qmp(from
, uri
, "{}");
688 wait_for_migration_pass(from
);
696 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
698 wait_for_migration_complete(from
);
700 /* Make sure we get at least one "B" on destination */
701 wait_for_serial("dest_serial");
703 if (uffd_feature_thread_id
) {
707 test_migrate_end(from
, to
, true);
710 static void test_postcopy(void)
712 MigrateStart
*args
= migrate_start_new();
713 QTestState
*from
, *to
;
715 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
718 migrate_postcopy_start(from
, to
);
719 migrate_postcopy_complete(from
, to
);
722 static void test_postcopy_recovery(void)
724 MigrateStart
*args
= migrate_start_new();
725 QTestState
*from
, *to
;
726 g_autofree
char *uri
= NULL
;
728 args
->hide_stderr
= true;
730 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
734 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
735 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
737 /* Now we start the postcopy */
738 migrate_postcopy_start(from
, to
);
741 * Wait until postcopy is really started; we can only run the
742 * migrate-pause command during a postcopy
744 wait_for_migration_status(from
, "postcopy-active", NULL
);
747 * Manually stop the postcopy migration. This emulates a network
748 * failure with the migration socket
753 * Wait for destination side to reach postcopy-paused state. The
754 * migrate-recover command can only succeed if destination machine
755 * is in the paused state
757 wait_for_migration_status(to
, "postcopy-paused",
758 (const char * []) { "failed", "active",
759 "completed", NULL
});
762 * Create a new socket to emulate a new channel that is different
763 * from the broken migration channel; tell the destination to
764 * listen to the new port
766 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
767 migrate_recover(to
, uri
);
770 * Try to rebuild the migration channel using the resume flag and
771 * the newly created channel
773 wait_for_migration_status(from
, "postcopy-paused",
774 (const char * []) { "failed", "active",
775 "completed", NULL
});
776 migrate_qmp(from
, uri
, "{'resume': true}");
778 /* Restore the postcopy bandwidth to unlimited */
779 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
781 migrate_postcopy_complete(from
, to
);
784 static void test_baddest(void)
786 MigrateStart
*args
= migrate_start_new();
787 QTestState
*from
, *to
;
789 args
->hide_stderr
= true;
791 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
794 migrate_qmp(from
, "tcp:127.0.0.1:0", "{}");
795 wait_for_migration_fail(from
, false);
796 test_migrate_end(from
, to
, false);
799 static void test_precopy_unix_common(bool dirty_ring
)
801 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
802 MigrateStart
*args
= migrate_start_new();
803 QTestState
*from
, *to
;
805 args
->use_dirty_ring
= dirty_ring
;
807 if (test_migrate_start(&from
, &to
, uri
, args
)) {
811 /* We want to pick a speed slow enough that the test completes
812 * quickly, but that it doesn't complete precopy even on a slow
813 * machine, so also set the downtime.
815 /* 1 ms should make it not converge*/
816 migrate_set_parameter_int(from
, "downtime-limit", 1);
818 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
820 /* Wait for the first serial output from the source */
821 wait_for_serial("src_serial");
823 migrate_qmp(from
, uri
, "{}");
825 wait_for_migration_pass(from
);
827 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
830 qtest_qmp_eventwait(from
, "STOP");
833 qtest_qmp_eventwait(to
, "RESUME");
835 wait_for_serial("dest_serial");
836 wait_for_migration_complete(from
);
838 test_migrate_end(from
, to
, true);
841 static void test_precopy_unix(void)
843 /* Using default dirty logging */
844 test_precopy_unix_common(false);
847 static void test_precopy_unix_dirty_ring(void)
849 /* Using dirty ring tracking */
850 test_precopy_unix_common(true);
854 /* Currently upset on aarch64 TCG */
855 static void test_ignore_shared(void)
857 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
858 QTestState
*from
, *to
;
860 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
864 migrate_set_capability(from
, "x-ignore-shared", true);
865 migrate_set_capability(to
, "x-ignore-shared", true);
867 /* Wait for the first serial output from the source */
868 wait_for_serial("src_serial");
870 migrate_qmp(from
, uri
, "{}");
872 wait_for_migration_pass(from
);
875 qtest_qmp_eventwait(from
, "STOP");
878 qtest_qmp_eventwait(to
, "RESUME");
880 wait_for_serial("dest_serial");
881 wait_for_migration_complete(from
);
883 /* Check whether shared RAM has been really skipped */
884 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
886 test_migrate_end(from
, to
, true);
890 static void test_xbzrle(const char *uri
)
892 MigrateStart
*args
= migrate_start_new();
893 QTestState
*from
, *to
;
895 if (test_migrate_start(&from
, &to
, uri
, args
)) {
900 * We want to pick a speed slow enough that the test completes
901 * quickly, but that it doesn't complete precopy even on a slow
902 * machine, so also set the downtime.
904 /* 1 ms should make it not converge*/
905 migrate_set_parameter_int(from
, "downtime-limit", 1);
907 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
909 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
911 migrate_set_capability(from
, "xbzrle", true);
912 migrate_set_capability(to
, "xbzrle", true);
913 /* Wait for the first serial output from the source */
914 wait_for_serial("src_serial");
916 migrate_qmp(from
, uri
, "{}");
918 wait_for_migration_pass(from
);
919 /* Make sure we have 2 passes, so the xbzrle cache gets a workout */
920 wait_for_migration_pass(from
);
922 /* 1000ms should converge */
923 migrate_set_parameter_int(from
, "downtime-limit", 1000);
926 qtest_qmp_eventwait(from
, "STOP");
928 qtest_qmp_eventwait(to
, "RESUME");
930 wait_for_serial("dest_serial");
931 wait_for_migration_complete(from
);
933 test_migrate_end(from
, to
, true);
936 static void test_xbzrle_unix(void)
938 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
943 static void test_precopy_tcp(void)
945 MigrateStart
*args
= migrate_start_new();
946 g_autofree
char *uri
= NULL
;
947 QTestState
*from
, *to
;
949 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
954 * We want to pick a speed slow enough that the test completes
955 * quickly, but that it doesn't complete precopy even on a slow
956 * machine, so also set the downtime.
958 /* 1 ms should make it not converge*/
959 migrate_set_parameter_int(from
, "downtime-limit", 1);
961 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
963 /* Wait for the first serial output from the source */
964 wait_for_serial("src_serial");
966 uri
= migrate_get_socket_address(to
, "socket-address");
968 migrate_qmp(from
, uri
, "{}");
970 wait_for_migration_pass(from
);
972 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
975 qtest_qmp_eventwait(from
, "STOP");
977 qtest_qmp_eventwait(to
, "RESUME");
979 wait_for_serial("dest_serial");
980 wait_for_migration_complete(from
);
982 test_migrate_end(from
, to
, true);
985 static void test_migrate_fd_proto(void)
987 MigrateStart
*args
= migrate_start_new();
988 QTestState
*from
, *to
;
992 const char *error_desc
;
994 if (test_migrate_start(&from
, &to
, "defer", args
)) {
999 * We want to pick a speed slow enough that the test completes
1000 * quickly, but that it doesn't complete precopy even on a slow
1001 * machine, so also set the downtime.
1003 /* 1 ms should make it not converge */
1004 migrate_set_parameter_int(from
, "downtime-limit", 1);
1006 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1008 /* Wait for the first serial output from the source */
1009 wait_for_serial("src_serial");
1011 /* Create two connected sockets for migration */
1012 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1013 g_assert_cmpint(ret
, ==, 0);
1015 /* Send the 1st socket to the target */
1016 rsp
= wait_command_fd(to
, pair
[0],
1017 "{ 'execute': 'getfd',"
1018 " 'arguments': { 'fdname': 'fd-mig' }}");
1022 /* Start incoming migration from the 1st socket */
1023 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1024 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1027 /* Send the 2nd socket to the target */
1028 rsp
= wait_command_fd(from
, pair
[1],
1029 "{ 'execute': 'getfd',"
1030 " 'arguments': { 'fdname': 'fd-mig' }}");
1034 /* Start migration to the 2nd socket*/
1035 migrate_qmp(from
, "fd:fd-mig", "{}");
1037 wait_for_migration_pass(from
);
1039 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
1042 qtest_qmp_eventwait(from
, "STOP");
1044 qtest_qmp_eventwait(to
, "RESUME");
1046 /* Test closing fds */
1047 /* We assume, that QEMU removes named fd from its list,
1048 * so this should fail */
1049 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1050 " 'arguments': { 'fdname': 'fd-mig' }}");
1051 g_assert_true(qdict_haskey(rsp
, "error"));
1052 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1053 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1056 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1057 " 'arguments': { 'fdname': 'fd-mig' }}");
1058 g_assert_true(qdict_haskey(rsp
, "error"));
1059 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1060 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1063 /* Complete migration */
1064 wait_for_serial("dest_serial");
1065 wait_for_migration_complete(from
);
1066 test_migrate_end(from
, to
, true);
1069 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1071 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1072 QTestState
*from
, *to
;
1074 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1079 * UUID validation is at the begin of migration. So, the main process of
1080 * migration is not interesting for us here. Thus, set huge downtime for
1081 * very fast migration.
1083 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1084 migrate_set_capability(from
, "validate-uuid", true);
1086 /* Wait for the first serial output from the source */
1087 wait_for_serial("src_serial");
1089 migrate_qmp(from
, uri
, "{}");
1092 qtest_set_expected_status(to
, 1);
1093 wait_for_migration_fail(from
, true);
1095 wait_for_migration_complete(from
);
1098 test_migrate_end(from
, to
, false);
1101 static void test_validate_uuid(void)
1103 MigrateStart
*args
= migrate_start_new();
1105 g_free(args
->opts_source
);
1106 g_free(args
->opts_target
);
1107 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1108 args
->opts_target
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1109 do_test_validate_uuid(args
, false);
1112 static void test_validate_uuid_error(void)
1114 MigrateStart
*args
= migrate_start_new();
1116 g_free(args
->opts_source
);
1117 g_free(args
->opts_target
);
1118 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1119 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1120 args
->hide_stderr
= true;
1121 do_test_validate_uuid(args
, true);
1124 static void test_validate_uuid_src_not_set(void)
1126 MigrateStart
*args
= migrate_start_new();
1128 g_free(args
->opts_target
);
1129 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1130 args
->hide_stderr
= true;
1131 do_test_validate_uuid(args
, false);
1134 static void test_validate_uuid_dst_not_set(void)
1136 MigrateStart
*args
= migrate_start_new();
1138 g_free(args
->opts_source
);
1139 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1140 args
->hide_stderr
= true;
1141 do_test_validate_uuid(args
, false);
1144 static void test_migrate_auto_converge(void)
1146 g_autofree
char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1147 MigrateStart
*args
= migrate_start_new();
1148 QTestState
*from
, *to
;
1149 int64_t remaining
, percentage
;
1152 * We want the test to be stable and as fast as possible.
1153 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1154 * so we need to decrease a bandwidth.
1156 const int64_t init_pct
= 5, inc_pct
= 50, max_pct
= 95;
1157 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
1158 const int64_t downtime_limit
= 250; /* 250ms */
1160 * We migrate through unix-socket (> 500Mb/s).
1161 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1162 * So, we can predict expected_threshold
1164 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
1166 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1170 migrate_set_capability(from
, "auto-converge", true);
1171 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1172 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1173 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1176 * Set the initial parameters so that the migration could not converge
1177 * without throttling.
1179 migrate_set_parameter_int(from
, "downtime-limit", 1);
1180 migrate_set_parameter_int(from
, "max-bandwidth", 100000000); /* ~100Mb/s */
1182 /* To check remaining size after precopy */
1183 migrate_set_capability(from
, "pause-before-switchover", true);
1185 /* Wait for the first serial output from the source */
1186 wait_for_serial("src_serial");
1188 migrate_qmp(from
, uri
, "{}");
1190 /* Wait for throttling begins */
1192 while (percentage
== 0) {
1193 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1195 g_assert_false(got_stop
);
1197 /* The first percentage of throttling should be equal to init_pct */
1198 g_assert_cmpint(percentage
, ==, init_pct
);
1199 /* Now, when we tested that throttling works, let it converge */
1200 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
1201 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
1204 * Wait for pre-switchover status to check last throttle percentage
1205 * and remaining. These values will be zeroed later
1207 wait_for_migration_status(from
, "pre-switchover", NULL
);
1209 /* The final percentage of throttling shouldn't be greater than max_pct */
1210 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1211 g_assert_cmpint(percentage
, <=, max_pct
);
1213 remaining
= read_ram_property_int(from
, "remaining");
1214 g_assert_cmpint(remaining
, <,
1215 (expected_threshold
+ expected_threshold
/ 100));
1217 migrate_continue(from
, "pre-switchover");
1219 qtest_qmp_eventwait(to
, "RESUME");
1221 wait_for_serial("dest_serial");
1222 wait_for_migration_complete(from
);
1225 test_migrate_end(from
, to
, true);
1228 static void test_multifd_tcp(const char *method
)
1230 MigrateStart
*args
= migrate_start_new();
1231 QTestState
*from
, *to
;
1233 g_autofree
char *uri
= NULL
;
1235 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1240 * We want to pick a speed slow enough that the test completes
1241 * quickly, but that it doesn't complete precopy even on a slow
1242 * machine, so also set the downtime.
1244 /* 1 ms should make it not converge*/
1245 migrate_set_parameter_int(from
, "downtime-limit", 1);
1247 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1249 migrate_set_parameter_int(from
, "multifd-channels", 16);
1250 migrate_set_parameter_int(to
, "multifd-channels", 16);
1252 migrate_set_parameter_str(from
, "multifd-compression", method
);
1253 migrate_set_parameter_str(to
, "multifd-compression", method
);
1255 migrate_set_capability(from
, "multifd", true);
1256 migrate_set_capability(to
, "multifd", true);
1258 /* Start incoming migration from the 1st socket */
1259 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1260 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1263 /* Wait for the first serial output from the source */
1264 wait_for_serial("src_serial");
1266 uri
= migrate_get_socket_address(to
, "socket-address");
1268 migrate_qmp(from
, uri
, "{}");
1270 wait_for_migration_pass(from
);
1272 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
1275 qtest_qmp_eventwait(from
, "STOP");
1277 qtest_qmp_eventwait(to
, "RESUME");
1279 wait_for_serial("dest_serial");
1280 wait_for_migration_complete(from
);
1281 test_migrate_end(from
, to
, true);
1284 static void test_multifd_tcp_none(void)
1286 test_multifd_tcp("none");
1289 static void test_multifd_tcp_zlib(void)
1291 test_multifd_tcp("zlib");
1295 static void test_multifd_tcp_zstd(void)
1297 test_multifd_tcp("zstd");
1307 * launch another target
1310 * And see that it works
1312 static void test_multifd_tcp_cancel(void)
1314 MigrateStart
*args
= migrate_start_new();
1315 QTestState
*from
, *to
, *to2
;
1317 g_autofree
char *uri
= NULL
;
1319 args
->hide_stderr
= true;
1321 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1326 * We want to pick a speed slow enough that the test completes
1327 * quickly, but that it doesn't complete precopy even on a slow
1328 * machine, so also set the downtime.
1330 /* 1 ms should make it not converge*/
1331 migrate_set_parameter_int(from
, "downtime-limit", 1);
1333 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
1335 migrate_set_parameter_int(from
, "multifd-channels", 16);
1336 migrate_set_parameter_int(to
, "multifd-channels", 16);
1338 migrate_set_capability(from
, "multifd", true);
1339 migrate_set_capability(to
, "multifd", true);
1341 /* Start incoming migration from the 1st socket */
1342 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1343 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1346 /* Wait for the first serial output from the source */
1347 wait_for_serial("src_serial");
1349 uri
= migrate_get_socket_address(to
, "socket-address");
1351 migrate_qmp(from
, uri
, "{}");
1353 wait_for_migration_pass(from
);
1355 migrate_cancel(from
);
1357 args
= migrate_start_new();
1358 args
->only_target
= true;
1360 if (test_migrate_start(&from
, &to2
, "defer", args
)) {
1364 migrate_set_parameter_int(to2
, "multifd-channels", 16);
1366 migrate_set_capability(to2
, "multifd", true);
1368 /* Start incoming migration from the 1st socket */
1369 rsp
= wait_command(to2
, "{ 'execute': 'migrate-incoming',"
1370 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1374 uri
= migrate_get_socket_address(to2
, "socket-address");
1376 wait_for_migration_status(from
, "cancelled", NULL
);
1378 /* 300ms it should converge */
1379 migrate_set_parameter_int(from
, "downtime-limit", 300);
1381 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1383 migrate_qmp(from
, uri
, "{}");
1385 wait_for_migration_pass(from
);
1388 qtest_qmp_eventwait(from
, "STOP");
1390 qtest_qmp_eventwait(to2
, "RESUME");
1392 wait_for_serial("dest_serial");
1393 wait_for_migration_complete(from
);
1394 test_migrate_end(from
, to2
, true);
1397 static bool kvm_dirty_ring_supported(void)
1399 #if defined(__linux__) && defined(HOST_X86_64)
1400 int ret
, kvm_fd
= open("/dev/kvm", O_RDONLY
);
1406 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, KVM_CAP_DIRTY_LOG_RING
);
1409 /* We test with 4096 slots */
1420 int main(int argc
, char **argv
)
1422 char template[] = "/tmp/migration-test-XXXXXX";
1423 const bool has_kvm
= qtest_has_accel("kvm");
1426 g_test_init(&argc
, &argv
, NULL
);
1428 if (!ufd_version_check()) {
1429 return g_test_run();
1433 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1434 * is touchy due to race conditions on dirty bits (especially on PPC for
1437 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1438 (!has_kvm
|| access("/sys/module/kvm_hv", F_OK
))) {
1439 g_test_message("Skipping test: kvm_hv not available");
1440 return g_test_run();
1444 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1445 * there until the problems are resolved
1447 if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm
) {
1448 g_test_message("Skipping test: s390x host with KVM is required");
1449 return g_test_run();
1452 tmpfs
= mkdtemp(template);
1454 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1458 module_call_init(MODULE_INIT_QOM
);
1460 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1461 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1462 qtest_add_func("/migration/bad_dest", test_baddest
);
1463 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1464 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1465 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1466 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1467 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1468 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
1469 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
1470 qtest_add_func("/migration/validate_uuid_src_not_set",
1471 test_validate_uuid_src_not_set
);
1472 qtest_add_func("/migration/validate_uuid_dst_not_set",
1473 test_validate_uuid_dst_not_set
);
1475 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
1476 qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none
);
1477 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel
);
1478 qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib
);
1480 qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd
);
1483 if (kvm_dirty_ring_supported()) {
1484 qtest_add_func("/migration/dirty_ring",
1485 test_precopy_unix_dirty_ring
);
1490 g_assert_cmpint(ret
, ==, 0);
1494 g_test_message("unable to rmdir: path (%s): %s",
1495 tmpfs
, strerror(errno
));