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 /* TODO actually test the results and get rid of this */
31 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
33 unsigned start_address
;
35 static bool uffd_feature_thread_id
;
37 /* A downtime where the test really should converge */
38 #define CONVERGE_DOWNTIME 1000
40 #if defined(__linux__)
41 #include <sys/syscall.h>
45 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
46 #include <sys/eventfd.h>
47 #include <sys/ioctl.h>
48 #include <linux/userfaultfd.h>
50 static bool ufd_version_check(void)
52 struct uffdio_api api_struct
;
55 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
58 g_test_message("Skipping test: userfaultfd not available");
62 api_struct
.api
= UFFD_API
;
63 api_struct
.features
= 0;
64 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
65 g_test_message("Skipping test: UFFDIO_API failed");
68 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
70 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
71 (__u64
)1 << _UFFDIO_UNREGISTER
;
72 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
73 g_test_message("Skipping test: Missing userfault feature");
81 static bool ufd_version_check(void)
83 g_test_message("Skipping test: Userfault not available (builtdtime)");
89 static const char *tmpfs
;
91 /* The boot file modifies memory area in [start_address, end_address)
92 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
94 #include "tests/migration/i386/a-b-bootblock.h"
95 #include "tests/migration/aarch64/a-b-kernel.h"
96 #include "tests/migration/s390x/a-b-bios.h"
98 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
100 FILE *bootfile
= fopen(bootpath
, "wb");
102 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
107 * Wait for some output in the serial output file,
108 * we get an 'A' followed by an endless string of 'B's
109 * but on the destination we won't have the A.
111 static void wait_for_serial(const char *side
)
113 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
114 FILE *serialfile
= fopen(serialpath
, "r");
115 const char *arch
= qtest_get_arch();
116 int started
= (strcmp(side
, "src_serial") == 0 &&
117 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
121 int readvalue
= fgetc(serialfile
);
124 /* SLOF prints its banner before starting test,
125 * to ignore it, mark the start of the test with '_',
126 * ignore all characters until this marker
133 fseek(serialfile
, 0, SEEK_SET
);
150 started
= (strcmp(side
, "src_serial") == 0 &&
151 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
152 fseek(serialfile
, 0, SEEK_SET
);
157 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
158 g_assert_not_reached();
164 * It's tricky to use qemu's migration event capability with qtest,
165 * events suddenly appearing confuse the qmp()/hmp() responses.
168 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
170 QDict
*rsp_return
, *rsp_ram
;
173 rsp_return
= migrate_query(who
);
174 if (!qdict_haskey(rsp_return
, "ram")) {
178 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
179 result
= qdict_get_try_int(rsp_ram
, property
, 0);
181 qobject_unref(rsp_return
);
185 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
190 rsp_return
= migrate_query(who
);
191 result
= qdict_get_try_int(rsp_return
, property
, 0);
192 qobject_unref(rsp_return
);
196 static uint64_t get_migration_pass(QTestState
*who
)
198 return read_ram_property_int(who
, "dirty-sync-count");
201 static void read_blocktime(QTestState
*who
)
205 rsp_return
= migrate_query(who
);
206 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
207 qobject_unref(rsp_return
);
210 static void wait_for_migration_pass(QTestState
*who
)
212 uint64_t initial_pass
= get_migration_pass(who
);
215 /* Wait for the 1st sync */
216 while (!got_stop
&& !initial_pass
) {
218 initial_pass
= get_migration_pass(who
);
223 pass
= get_migration_pass(who
);
224 } while (pass
== initial_pass
&& !got_stop
);
227 static void check_guests_ram(QTestState
*who
)
229 /* Our ASM test will have been incrementing one byte from each page from
230 * start_address to < end_address in order. This gives us a constraint
231 * that any page's byte should be equal or less than the previous pages
232 * byte (mod 256); and they should all be equal except for one transition
233 * at the point where we meet the incrementer. (We're running this with
234 * the guest stopped).
239 bool hit_edge
= false;
242 qtest_memread(who
, start_address
, &first_byte
, 1);
243 last_byte
= first_byte
;
245 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
246 address
+= TEST_MEM_PAGE_SIZE
)
249 qtest_memread(who
, address
, &b
, 1);
250 if (b
!= last_byte
) {
251 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
252 /* This is OK, the guest stopped at the point of
253 * incrementing the previous page but didn't get
261 fprintf(stderr
, "Memory content inconsistency at %x"
262 " first_byte = %x last_byte = %x current = %x"
264 address
, first_byte
, last_byte
, b
, hit_edge
);
270 fprintf(stderr
, "and in another %d pages", bad
- 10);
275 static void cleanup(const char *filename
)
277 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
283 static char *SocketAddress_to_str(SocketAddress
*addr
)
285 switch (addr
->type
) {
286 case SOCKET_ADDRESS_TYPE_INET
:
287 return g_strdup_printf("tcp:%s:%s",
290 case SOCKET_ADDRESS_TYPE_UNIX
:
291 return g_strdup_printf("unix:%s",
292 addr
->u
.q_unix
.path
);
293 case SOCKET_ADDRESS_TYPE_FD
:
294 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
295 case SOCKET_ADDRESS_TYPE_VSOCK
:
296 return g_strdup_printf("tcp:%s:%s",
300 return g_strdup("unknown address type");
304 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
308 SocketAddressList
*addrs
;
312 rsp
= migrate_query(who
);
313 object
= qdict_get(rsp
, parameter
);
315 iv
= qobject_input_visitor_new(object
);
316 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &error_abort
);
319 /* we are only using a single address */
320 result
= SocketAddress_to_str(addrs
->value
);
322 qapi_free_SocketAddressList(addrs
);
327 static long long migrate_get_parameter_int(QTestState
*who
,
328 const char *parameter
)
333 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
334 result
= qdict_get_int(rsp
, parameter
);
339 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
344 result
= migrate_get_parameter_int(who
, parameter
);
345 g_assert_cmpint(result
, ==, value
);
348 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
354 "{ 'execute': 'migrate-set-parameters',"
355 "'arguments': { %s: %lld } }",
357 g_assert(qdict_haskey(rsp
, "return"));
359 migrate_check_parameter_int(who
, parameter
, value
);
362 static char *migrate_get_parameter_str(QTestState
*who
,
363 const char *parameter
)
368 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
369 result
= g_strdup(qdict_get_str(rsp
, parameter
));
374 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
379 result
= migrate_get_parameter_str(who
, parameter
);
380 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 */
479 static MigrateStart
*migrate_start_new(void)
481 MigrateStart
*args
= g_new0(MigrateStart
, 1);
483 args
->opts_source
= g_strdup("");
484 args
->opts_target
= g_strdup("");
488 static void migrate_start_destroy(MigrateStart
*args
)
490 g_free(args
->opts_source
);
491 g_free(args
->opts_target
);
495 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
496 const char *uri
, MigrateStart
*args
)
498 gchar
*arch_source
, *arch_target
;
499 gchar
*cmd_source
, *cmd_target
;
500 const gchar
*ignore_stderr
;
501 char *bootpath
= NULL
;
504 const char *arch
= qtest_get_arch();
505 const char *machine_opts
= NULL
;
506 const char *memory_size
;
509 if (args
->use_shmem
) {
510 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
511 g_test_skip("/dev/shm is not supported");
518 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
519 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
520 /* the assembled x86 boot sector should be exactly one sector large */
521 assert(sizeof(x86_bootsect
) == 512);
522 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
523 memory_size
= "150M";
524 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
525 arch_target
= g_strdup(arch_source
);
526 start_address
= X86_TEST_MEM_START
;
527 end_address
= X86_TEST_MEM_END
;
528 } else if (g_str_equal(arch
, "s390x")) {
529 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
530 memory_size
= "128M";
531 arch_source
= g_strdup_printf("-bios %s", bootpath
);
532 arch_target
= g_strdup(arch_source
);
533 start_address
= S390_TEST_MEM_START
;
534 end_address
= S390_TEST_MEM_END
;
535 } else if (strcmp(arch
, "ppc64") == 0) {
536 machine_opts
= "vsmt=8";
537 memory_size
= "256M";
538 start_address
= PPC_TEST_MEM_START
;
539 end_address
= PPC_TEST_MEM_END
;
540 arch_source
= g_strdup_printf("-nodefaults "
541 "-prom-env 'use-nvramrc?=true' -prom-env "
542 "'nvramrc=hex .\" _\" begin %x %x "
543 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
544 "until'", end_address
, start_address
);
545 arch_target
= g_strdup("");
546 } else if (strcmp(arch
, "aarch64") == 0) {
547 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
548 machine_opts
= "virt,gic-version=max";
549 memory_size
= "150M";
550 arch_source
= g_strdup_printf("-cpu max "
553 arch_target
= g_strdup(arch_source
);
554 start_address
= ARM_TEST_MEM_START
;
555 end_address
= ARM_TEST_MEM_END
;
557 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
559 g_assert_not_reached();
564 if (!getenv("QTEST_LOG") && args
->hide_stderr
) {
565 ignore_stderr
= "2>/dev/null";
570 if (args
->use_shmem
) {
571 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
572 shmem_opts
= g_strdup_printf(
573 "-object memory-backend-file,id=mem0,size=%s"
574 ",mem-path=%s,share=on -numa node,memdev=mem0",
575 memory_size
, shmem_path
);
578 shmem_opts
= g_strdup("");
581 cmd_source
= g_strdup_printf("-accel kvm -accel tcg%s%s "
582 "-name source,debug-threads=on "
584 "-serial file:%s/src_serial "
586 machine_opts
? " -machine " : "",
587 machine_opts
? machine_opts
: "",
589 arch_source
, shmem_opts
, args
->opts_source
,
592 if (!args
->only_target
) {
593 *from
= qtest_init(cmd_source
);
597 cmd_target
= g_strdup_printf("-accel kvm -accel tcg%s%s "
598 "-name target,debug-threads=on "
600 "-serial file:%s/dest_serial "
603 machine_opts
? " -machine " : "",
604 machine_opts
? machine_opts
: "",
605 memory_size
, tmpfs
, uri
,
606 arch_target
, shmem_opts
,
607 args
->opts_target
, ignore_stderr
);
609 *to
= qtest_init(cmd_target
);
614 * Remove shmem file immediately to avoid memory leak in test failed case.
615 * It's valid becase QEMU has already opened this file
617 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 void deprecated_set_downtime(QTestState
*who
, const double value
)
666 "{ 'execute': 'migrate_set_downtime',"
667 " 'arguments': { 'value': %f } }", value
);
668 g_assert(qdict_haskey(rsp
, "return"));
670 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
673 static void deprecated_set_speed(QTestState
*who
, long long value
)
677 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
678 "'arguments': { 'value': %lld } }", value
);
679 g_assert(qdict_haskey(rsp
, "return"));
681 migrate_check_parameter_int(who
, "max-bandwidth", value
);
684 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
688 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
689 "'arguments': { 'value': %lld } }", value
);
690 g_assert(qdict_haskey(rsp
, "return"));
692 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
695 static void test_deprecated(void)
699 from
= qtest_init("-machine none");
701 deprecated_set_downtime(from
, 0.12345);
702 deprecated_set_speed(from
, 12345);
703 deprecated_set_cache_size(from
, 4096);
708 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
712 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
713 QTestState
*from
, *to
;
715 if (test_migrate_start(&from
, &to
, uri
, args
)) {
719 migrate_set_capability(from
, "postcopy-ram", true);
720 migrate_set_capability(to
, "postcopy-ram", true);
721 migrate_set_capability(to
, "postcopy-blocktime", true);
723 /* We want to pick a speed slow enough that the test completes
724 * quickly, but that it doesn't complete precopy even on a slow
725 * machine, so also set the downtime.
727 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
728 migrate_set_parameter_int(from
, "downtime-limit", 1);
730 /* Wait for the first serial output from the source */
731 wait_for_serial("src_serial");
733 migrate_qmp(from
, uri
, "{}");
736 wait_for_migration_pass(from
);
744 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
746 wait_for_migration_complete(from
);
748 /* Make sure we get at least one "B" on destination */
749 wait_for_serial("dest_serial");
751 if (uffd_feature_thread_id
) {
755 test_migrate_end(from
, to
, true);
758 static void test_postcopy(void)
760 MigrateStart
*args
= migrate_start_new();
761 QTestState
*from
, *to
;
763 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
766 migrate_postcopy_start(from
, to
);
767 migrate_postcopy_complete(from
, to
);
770 static void test_postcopy_recovery(void)
772 MigrateStart
*args
= migrate_start_new();
773 QTestState
*from
, *to
;
776 args
->hide_stderr
= true;
778 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
782 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
783 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
785 /* Now we start the postcopy */
786 migrate_postcopy_start(from
, to
);
789 * Wait until postcopy is really started; we can only run the
790 * migrate-pause command during a postcopy
792 wait_for_migration_status(from
, "postcopy-active", NULL
);
795 * Manually stop the postcopy migration. This emulates a network
796 * failure with the migration socket
801 * Wait for destination side to reach postcopy-paused state. The
802 * migrate-recover command can only succeed if destination machine
803 * is in the paused state
805 wait_for_migration_status(to
, "postcopy-paused",
806 (const char * []) { "failed", "active",
807 "completed", NULL
});
810 * Create a new socket to emulate a new channel that is different
811 * from the broken migration channel; tell the destination to
812 * listen to the new port
814 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
815 migrate_recover(to
, uri
);
818 * Try to rebuild the migration channel using the resume flag and
819 * the newly created channel
821 wait_for_migration_status(from
, "postcopy-paused",
822 (const char * []) { "failed", "active",
823 "completed", NULL
});
824 migrate_qmp(from
, uri
, "{'resume': true}");
827 /* Restore the postcopy bandwidth to unlimited */
828 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
830 migrate_postcopy_complete(from
, to
);
833 static void test_baddest(void)
835 MigrateStart
*args
= migrate_start_new();
836 QTestState
*from
, *to
;
838 args
->hide_stderr
= true;
840 if (test_migrate_start(&from
, &to
, "tcp:0:0", args
)) {
843 migrate_qmp(from
, "tcp:0:0", "{}");
844 wait_for_migration_fail(from
, false);
845 test_migrate_end(from
, to
, false);
848 static void test_precopy_unix(void)
850 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
851 MigrateStart
*args
= migrate_start_new();
852 QTestState
*from
, *to
;
854 if (test_migrate_start(&from
, &to
, uri
, args
)) {
858 /* We want to pick a speed slow enough that the test completes
859 * quickly, but that it doesn't complete precopy even on a slow
860 * machine, so also set the downtime.
862 /* 1 ms should make it not converge*/
863 migrate_set_parameter_int(from
, "downtime-limit", 1);
865 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
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
);
874 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
877 qtest_qmp_eventwait(from
, "STOP");
880 qtest_qmp_eventwait(to
, "RESUME");
882 wait_for_serial("dest_serial");
883 wait_for_migration_complete(from
);
885 test_migrate_end(from
, to
, true);
890 /* Currently upset on aarch64 TCG */
891 static void test_ignore_shared(void)
893 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
894 QTestState
*from
, *to
;
896 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
900 migrate_set_capability(from
, "x-ignore-shared", true);
901 migrate_set_capability(to
, "x-ignore-shared", true);
903 /* Wait for the first serial output from the source */
904 wait_for_serial("src_serial");
906 migrate_qmp(from
, uri
, "{}");
908 wait_for_migration_pass(from
);
911 qtest_qmp_eventwait(from
, "STOP");
914 qtest_qmp_eventwait(to
, "RESUME");
916 wait_for_serial("dest_serial");
917 wait_for_migration_complete(from
);
919 /* Check whether shared RAM has been really skipped */
920 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
922 test_migrate_end(from
, to
, true);
927 static void test_xbzrle(const char *uri
)
929 MigrateStart
*args
= migrate_start_new();
930 QTestState
*from
, *to
;
932 if (test_migrate_start(&from
, &to
, uri
, args
)) {
937 * We want to pick a speed slow enough that the test completes
938 * quickly, but that it doesn't complete precopy even on a slow
939 * machine, so also set the downtime.
941 /* 1 ms should make it not converge*/
942 migrate_set_parameter_int(from
, "downtime-limit", 1);
944 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
946 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
948 migrate_set_capability(from
, "xbzrle", "true");
949 migrate_set_capability(to
, "xbzrle", "true");
950 /* Wait for the first serial output from the source */
951 wait_for_serial("src_serial");
953 migrate_qmp(from
, uri
, "{}");
955 wait_for_migration_pass(from
);
956 /* Make sure we have 2 passes, so the xbzrle cache gets a workout */
957 wait_for_migration_pass(from
);
959 /* 1000ms should converge */
960 migrate_set_parameter_int(from
, "downtime-limit", 1000);
963 qtest_qmp_eventwait(from
, "STOP");
965 qtest_qmp_eventwait(to
, "RESUME");
967 wait_for_serial("dest_serial");
968 wait_for_migration_complete(from
);
970 test_migrate_end(from
, to
, true);
973 static void test_xbzrle_unix(void)
975 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
981 static void test_precopy_tcp(void)
983 MigrateStart
*args
= migrate_start_new();
985 QTestState
*from
, *to
;
987 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
992 * We want to pick a speed slow enough that the test completes
993 * quickly, but that it doesn't complete precopy even on a slow
994 * machine, so also set the downtime.
996 /* 1 ms should make it not converge*/
997 migrate_set_parameter_int(from
, "downtime-limit", 1);
999 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1001 /* Wait for the first serial output from the source */
1002 wait_for_serial("src_serial");
1004 uri
= migrate_get_socket_address(to
, "socket-address");
1006 migrate_qmp(from
, uri
, "{}");
1008 wait_for_migration_pass(from
);
1010 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
1013 qtest_qmp_eventwait(from
, "STOP");
1015 qtest_qmp_eventwait(to
, "RESUME");
1017 wait_for_serial("dest_serial");
1018 wait_for_migration_complete(from
);
1020 test_migrate_end(from
, to
, true);
1024 static void test_migrate_fd_proto(void)
1026 MigrateStart
*args
= migrate_start_new();
1027 QTestState
*from
, *to
;
1031 const char *error_desc
;
1033 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1038 * We want to pick a speed slow enough that the test completes
1039 * quickly, but that it doesn't complete precopy even on a slow
1040 * machine, so also set the downtime.
1042 /* 1 ms should make it not converge */
1043 migrate_set_parameter_int(from
, "downtime-limit", 1);
1045 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1047 /* Wait for the first serial output from the source */
1048 wait_for_serial("src_serial");
1050 /* Create two connected sockets for migration */
1051 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1052 g_assert_cmpint(ret
, ==, 0);
1054 /* Send the 1st socket to the target */
1055 rsp
= wait_command_fd(to
, pair
[0],
1056 "{ 'execute': 'getfd',"
1057 " 'arguments': { 'fdname': 'fd-mig' }}");
1061 /* Start incoming migration from the 1st socket */
1062 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1063 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1066 /* Send the 2nd socket to the target */
1067 rsp
= wait_command_fd(from
, pair
[1],
1068 "{ 'execute': 'getfd',"
1069 " 'arguments': { 'fdname': 'fd-mig' }}");
1073 /* Start migration to the 2nd socket*/
1074 migrate_qmp(from
, "fd:fd-mig", "{}");
1076 wait_for_migration_pass(from
);
1078 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
1081 qtest_qmp_eventwait(from
, "STOP");
1083 qtest_qmp_eventwait(to
, "RESUME");
1085 /* Test closing fds */
1086 /* We assume, that QEMU removes named fd from its list,
1087 * so this should fail */
1088 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1089 " 'arguments': { 'fdname': 'fd-mig' }}");
1090 g_assert_true(qdict_haskey(rsp
, "error"));
1091 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1092 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1095 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1096 " 'arguments': { 'fdname': 'fd-mig' }}");
1097 g_assert_true(qdict_haskey(rsp
, "error"));
1098 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1099 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1102 /* Complete migration */
1103 wait_for_serial("dest_serial");
1104 wait_for_migration_complete(from
);
1105 test_migrate_end(from
, to
, true);
1108 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1110 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1111 QTestState
*from
, *to
;
1113 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1118 * UUID validation is at the begin of migration. So, the main process of
1119 * migration is not interesting for us here. Thus, set huge downtime for
1120 * very fast migration.
1122 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1123 migrate_set_capability(from
, "validate-uuid", true);
1125 /* Wait for the first serial output from the source */
1126 wait_for_serial("src_serial");
1128 migrate_qmp(from
, uri
, "{}");
1131 qtest_set_expected_status(to
, 1);
1132 wait_for_migration_fail(from
, true);
1134 wait_for_migration_complete(from
);
1137 test_migrate_end(from
, to
, false);
1141 static void test_validate_uuid(void)
1143 MigrateStart
*args
= migrate_start_new();
1145 g_free(args
->opts_source
);
1146 g_free(args
->opts_target
);
1147 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1148 args
->opts_target
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1149 do_test_validate_uuid(args
, false);
1152 static void test_validate_uuid_error(void)
1154 MigrateStart
*args
= migrate_start_new();
1156 g_free(args
->opts_source
);
1157 g_free(args
->opts_target
);
1158 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1159 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1160 args
->hide_stderr
= true;
1161 do_test_validate_uuid(args
, true);
1164 static void test_validate_uuid_src_not_set(void)
1166 MigrateStart
*args
= migrate_start_new();
1168 g_free(args
->opts_target
);
1169 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1170 args
->hide_stderr
= true;
1171 do_test_validate_uuid(args
, false);
1174 static void test_validate_uuid_dst_not_set(void)
1176 MigrateStart
*args
= migrate_start_new();
1178 g_free(args
->opts_source
);
1179 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1180 args
->hide_stderr
= true;
1181 do_test_validate_uuid(args
, false);
1184 static void test_migrate_auto_converge(void)
1186 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1187 MigrateStart
*args
= migrate_start_new();
1188 QTestState
*from
, *to
;
1189 int64_t remaining
, percentage
;
1192 * We want the test to be stable and as fast as possible.
1193 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1194 * so we need to decrease a bandwidth.
1196 const int64_t init_pct
= 5, inc_pct
= 50, max_pct
= 95;
1197 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
1198 const int64_t downtime_limit
= 250; /* 250ms */
1200 * We migrate through unix-socket (> 500Mb/s).
1201 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1202 * So, we can predict expected_threshold
1204 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
1206 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1210 migrate_set_capability(from
, "auto-converge", true);
1211 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1212 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1213 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1216 * Set the initial parameters so that the migration could not converge
1217 * without throttling.
1219 migrate_set_parameter_int(from
, "downtime-limit", 1);
1220 migrate_set_parameter_int(from
, "max-bandwidth", 100000000); /* ~100Mb/s */
1222 /* To check remaining size after precopy */
1223 migrate_set_capability(from
, "pause-before-switchover", true);
1225 /* Wait for the first serial output from the source */
1226 wait_for_serial("src_serial");
1228 migrate_qmp(from
, uri
, "{}");
1230 /* Wait for throttling begins */
1232 while (percentage
== 0) {
1233 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1235 g_assert_false(got_stop
);
1237 /* The first percentage of throttling should be equal to init_pct */
1238 g_assert_cmpint(percentage
, ==, init_pct
);
1239 /* Now, when we tested that throttling works, let it converge */
1240 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
1241 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
1244 * Wait for pre-switchover status to check last throttle percentage
1245 * and remaining. These values will be zeroed later
1247 wait_for_migration_status(from
, "pre-switchover", NULL
);
1249 /* The final percentage of throttling shouldn't be greater than max_pct */
1250 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1251 g_assert_cmpint(percentage
, <=, max_pct
);
1253 remaining
= read_ram_property_int(from
, "remaining");
1254 g_assert_cmpint(remaining
, <,
1255 (expected_threshold
+ expected_threshold
/ 100));
1257 migrate_continue(from
, "pre-switchover");
1259 qtest_qmp_eventwait(to
, "RESUME");
1261 wait_for_serial("dest_serial");
1262 wait_for_migration_complete(from
);
1266 test_migrate_end(from
, to
, true);
1269 static void test_multifd_tcp(const char *method
)
1271 MigrateStart
*args
= migrate_start_new();
1272 QTestState
*from
, *to
;
1276 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1281 * We want to pick a speed slow enough that the test completes
1282 * quickly, but that it doesn't complete precopy even on a slow
1283 * machine, so also set the downtime.
1285 /* 1 ms should make it not converge*/
1286 migrate_set_parameter_int(from
, "downtime-limit", 1);
1288 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1290 migrate_set_parameter_int(from
, "multifd-channels", 16);
1291 migrate_set_parameter_int(to
, "multifd-channels", 16);
1293 migrate_set_parameter_str(from
, "multifd-compression", method
);
1294 migrate_set_parameter_str(to
, "multifd-compression", method
);
1296 migrate_set_capability(from
, "multifd", "true");
1297 migrate_set_capability(to
, "multifd", "true");
1299 /* Start incoming migration from the 1st socket */
1300 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1301 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1304 /* Wait for the first serial output from the source */
1305 wait_for_serial("src_serial");
1307 uri
= migrate_get_socket_address(to
, "socket-address");
1309 migrate_qmp(from
, uri
, "{}");
1311 wait_for_migration_pass(from
);
1313 migrate_set_parameter_int(from
, "downtime-limit", CONVERGE_DOWNTIME
);
1316 qtest_qmp_eventwait(from
, "STOP");
1318 qtest_qmp_eventwait(to
, "RESUME");
1320 wait_for_serial("dest_serial");
1321 wait_for_migration_complete(from
);
1322 test_migrate_end(from
, to
, true);
1326 static void test_multifd_tcp_none(void)
1328 test_multifd_tcp("none");
1331 static void test_multifd_tcp_zlib(void)
1333 test_multifd_tcp("zlib");
1337 static void test_multifd_tcp_zstd(void)
1339 test_multifd_tcp("zstd");
1349 * launch another target
1352 * And see that it works
1354 static void test_multifd_tcp_cancel(void)
1356 MigrateStart
*args
= migrate_start_new();
1357 QTestState
*from
, *to
, *to2
;
1361 args
->hide_stderr
= true;
1363 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1368 * We want to pick a speed slow enough that the test completes
1369 * quickly, but that it doesn't complete precopy even on a slow
1370 * machine, so also set the downtime.
1372 /* 1 ms should make it not converge*/
1373 migrate_set_parameter_int(from
, "downtime-limit", 1);
1375 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
1377 migrate_set_parameter_int(from
, "multifd-channels", 16);
1378 migrate_set_parameter_int(to
, "multifd-channels", 16);
1380 migrate_set_capability(from
, "multifd", "true");
1381 migrate_set_capability(to
, "multifd", "true");
1383 /* Start incoming migration from the 1st socket */
1384 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1385 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1388 /* Wait for the first serial output from the source */
1389 wait_for_serial("src_serial");
1391 uri
= migrate_get_socket_address(to
, "socket-address");
1393 migrate_qmp(from
, uri
, "{}");
1395 wait_for_migration_pass(from
);
1397 migrate_cancel(from
);
1399 args
= migrate_start_new();
1400 args
->only_target
= true;
1402 if (test_migrate_start(&from
, &to2
, "defer", args
)) {
1406 migrate_set_parameter_int(to2
, "multifd-channels", 16);
1408 migrate_set_capability(to2
, "multifd", "true");
1410 /* Start incoming migration from the 1st socket */
1411 rsp
= wait_command(to2
, "{ 'execute': 'migrate-incoming',"
1412 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1416 uri
= migrate_get_socket_address(to2
, "socket-address");
1418 wait_for_migration_status(from
, "cancelled", NULL
);
1420 /* 300ms it should converge */
1421 migrate_set_parameter_int(from
, "downtime-limit", 300);
1423 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1425 migrate_qmp(from
, uri
, "{}");
1427 wait_for_migration_pass(from
);
1430 qtest_qmp_eventwait(from
, "STOP");
1432 qtest_qmp_eventwait(to2
, "RESUME");
1434 wait_for_serial("dest_serial");
1435 wait_for_migration_complete(from
);
1436 test_migrate_end(from
, to2
, true);
1440 int main(int argc
, char **argv
)
1442 char template[] = "/tmp/migration-test-XXXXXX";
1445 g_test_init(&argc
, &argv
, NULL
);
1447 if (!ufd_version_check()) {
1448 return g_test_run();
1452 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1453 * is touchy due to race conditions on dirty bits (especially on PPC for
1456 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1457 (access("/sys/module/kvm_hv", F_OK
) ||
1458 access("/dev/kvm", R_OK
| W_OK
))) {
1459 g_test_message("Skipping test: kvm_hv not available");
1460 return g_test_run();
1464 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1465 * there until the problems are resolved
1467 if (g_str_equal(qtest_get_arch(), "s390x")) {
1468 #if defined(HOST_S390X)
1469 if (access("/dev/kvm", R_OK
| W_OK
)) {
1470 g_test_message("Skipping test: kvm not available");
1471 return g_test_run();
1474 g_test_message("Skipping test: Need s390x host to work properly");
1475 return g_test_run();
1479 tmpfs
= mkdtemp(template);
1481 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1485 module_call_init(MODULE_INIT_QOM
);
1487 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1488 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1489 qtest_add_func("/migration/deprecated", test_deprecated
);
1490 qtest_add_func("/migration/bad_dest", test_baddest
);
1491 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1492 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1493 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1494 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1495 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1496 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
1497 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
1498 qtest_add_func("/migration/validate_uuid_src_not_set",
1499 test_validate_uuid_src_not_set
);
1500 qtest_add_func("/migration/validate_uuid_dst_not_set",
1501 test_validate_uuid_dst_not_set
);
1503 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
1504 qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none
);
1505 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel
);
1506 qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib
);
1508 qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd
);
1513 g_assert_cmpint(ret
, ==, 0);
1517 g_test_message("unable to rmdir: path (%s): %s",
1518 tmpfs
, strerror(errno
));