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/qmp/qdict.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "qapi/qapi-visit-sockets.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qapi/qobject-output-visitor.h"
26 #include "migration-helpers.h"
27 #include "migration/migration-test.h"
29 /* TODO actually test the results and get rid of this */
30 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
32 unsigned start_address
;
34 static bool uffd_feature_thread_id
;
36 #if defined(__linux__)
37 #include <sys/syscall.h>
41 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
42 #include <sys/eventfd.h>
43 #include <sys/ioctl.h>
44 #include <linux/userfaultfd.h>
46 static bool ufd_version_check(void)
48 struct uffdio_api api_struct
;
51 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
54 g_test_message("Skipping test: userfaultfd not available");
58 api_struct
.api
= UFFD_API
;
59 api_struct
.features
= 0;
60 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
61 g_test_message("Skipping test: UFFDIO_API failed");
64 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
66 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
67 (__u64
)1 << _UFFDIO_UNREGISTER
;
68 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
69 g_test_message("Skipping test: Missing userfault feature");
77 static bool ufd_version_check(void)
79 g_test_message("Skipping test: Userfault not available (builtdtime)");
85 static const char *tmpfs
;
87 /* The boot file modifies memory area in [start_address, end_address)
88 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
90 #include "tests/migration/i386/a-b-bootblock.h"
91 #include "tests/migration/aarch64/a-b-kernel.h"
92 #include "tests/migration/s390x/a-b-bios.h"
94 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
96 FILE *bootfile
= fopen(bootpath
, "wb");
98 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
103 * Wait for some output in the serial output file,
104 * we get an 'A' followed by an endless string of 'B's
105 * but on the destination we won't have the A.
107 static void wait_for_serial(const char *side
)
109 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
110 FILE *serialfile
= fopen(serialpath
, "r");
111 const char *arch
= qtest_get_arch();
112 int started
= (strcmp(side
, "src_serial") == 0 &&
113 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
117 int readvalue
= fgetc(serialfile
);
120 /* SLOF prints its banner before starting test,
121 * to ignore it, mark the start of the test with '_',
122 * ignore all characters until this marker
129 fseek(serialfile
, 0, SEEK_SET
);
146 started
= (strcmp(side
, "src_serial") == 0 &&
147 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
148 fseek(serialfile
, 0, SEEK_SET
);
153 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
154 g_assert_not_reached();
160 * It's tricky to use qemu's migration event capability with qtest,
161 * events suddenly appearing confuse the qmp()/hmp() responses.
164 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
166 QDict
*rsp_return
, *rsp_ram
;
169 rsp_return
= migrate_query(who
);
170 if (!qdict_haskey(rsp_return
, "ram")) {
174 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
175 result
= qdict_get_try_int(rsp_ram
, property
, 0);
177 qobject_unref(rsp_return
);
181 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
186 rsp_return
= migrate_query(who
);
187 result
= qdict_get_try_int(rsp_return
, property
, 0);
188 qobject_unref(rsp_return
);
192 static uint64_t get_migration_pass(QTestState
*who
)
194 return read_ram_property_int(who
, "dirty-sync-count");
197 static void read_blocktime(QTestState
*who
)
201 rsp_return
= migrate_query(who
);
202 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
203 qobject_unref(rsp_return
);
206 static void wait_for_migration_pass(QTestState
*who
)
208 uint64_t initial_pass
= get_migration_pass(who
);
211 /* Wait for the 1st sync */
212 while (!got_stop
&& !initial_pass
) {
214 initial_pass
= get_migration_pass(who
);
219 pass
= get_migration_pass(who
);
220 } while (pass
== initial_pass
&& !got_stop
);
223 static void check_guests_ram(QTestState
*who
)
225 /* Our ASM test will have been incrementing one byte from each page from
226 * start_address to < end_address in order. This gives us a constraint
227 * that any page's byte should be equal or less than the previous pages
228 * byte (mod 256); and they should all be equal except for one transition
229 * at the point where we meet the incrementer. (We're running this with
230 * the guest stopped).
235 bool hit_edge
= false;
238 qtest_memread(who
, start_address
, &first_byte
, 1);
239 last_byte
= first_byte
;
241 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
242 address
+= TEST_MEM_PAGE_SIZE
)
245 qtest_memread(who
, address
, &b
, 1);
246 if (b
!= last_byte
) {
247 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
248 /* This is OK, the guest stopped at the point of
249 * incrementing the previous page but didn't get
257 fprintf(stderr
, "Memory content inconsistency at %x"
258 " first_byte = %x last_byte = %x current = %x"
260 address
, first_byte
, last_byte
, b
, hit_edge
);
266 fprintf(stderr
, "and in another %d pages", bad
- 10);
271 static void cleanup(const char *filename
)
273 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
279 static char *SocketAddress_to_str(SocketAddress
*addr
)
281 switch (addr
->type
) {
282 case SOCKET_ADDRESS_TYPE_INET
:
283 return g_strdup_printf("tcp:%s:%s",
286 case SOCKET_ADDRESS_TYPE_UNIX
:
287 return g_strdup_printf("unix:%s",
288 addr
->u
.q_unix
.path
);
289 case SOCKET_ADDRESS_TYPE_FD
:
290 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
291 case SOCKET_ADDRESS_TYPE_VSOCK
:
292 return g_strdup_printf("tcp:%s:%s",
296 return g_strdup("unknown address type");
300 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
304 Error
*local_err
= NULL
;
305 SocketAddressList
*addrs
;
309 rsp
= migrate_query(who
);
310 object
= qdict_get(rsp
, parameter
);
312 iv
= qobject_input_visitor_new(object
);
313 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &local_err
);
316 /* we are only using a single address */
317 result
= SocketAddress_to_str(addrs
->value
);
319 qapi_free_SocketAddressList(addrs
);
324 static long long migrate_get_parameter_int(QTestState
*who
,
325 const char *parameter
)
330 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
331 result
= qdict_get_int(rsp
, parameter
);
336 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
341 result
= migrate_get_parameter_int(who
, parameter
);
342 g_assert_cmpint(result
, ==, value
);
345 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
351 "{ 'execute': 'migrate-set-parameters',"
352 "'arguments': { %s: %lld } }",
354 g_assert(qdict_haskey(rsp
, "return"));
356 migrate_check_parameter_int(who
, parameter
, value
);
359 static char *migrate_get_parameter_str(QTestState
*who
,
360 const char *parameter
)
365 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
366 result
= g_strdup(qdict_get_str(rsp
, parameter
));
371 static void migrate_check_parameter_str(QTestState
*who
, const char *parameter
,
376 result
= migrate_get_parameter_str(who
, parameter
);
377 g_assert_cmpstr(result
, ==, value
);
381 __attribute__((unused
))
382 static void migrate_set_parameter_str(QTestState
*who
, const char *parameter
,
388 "{ 'execute': 'migrate-set-parameters',"
389 "'arguments': { %s: %s } }",
391 g_assert(qdict_haskey(rsp
, "return"));
393 migrate_check_parameter_str(who
, parameter
, value
);
396 static void migrate_pause(QTestState
*who
)
400 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
404 static void migrate_continue(QTestState
*who
, const char *state
)
408 rsp
= wait_command(who
,
409 "{ 'execute': 'migrate-continue',"
410 " 'arguments': { 'state': %s } }",
415 static void migrate_recover(QTestState
*who
, const char *uri
)
419 rsp
= wait_command(who
,
420 "{ 'execute': 'migrate-recover', "
421 " 'id': 'recover-cmd', "
422 " 'arguments': { 'uri': %s } }",
427 static void migrate_cancel(QTestState
*who
)
431 rsp
= wait_command(who
, "{ 'execute': 'migrate_cancel' }");
435 static void migrate_set_capability(QTestState
*who
, const char *capability
,
441 "{ 'execute': 'migrate-set-capabilities',"
443 "'capabilities': [ { "
444 "'capability': %s, 'state': %i } ] } }",
446 g_assert(qdict_haskey(rsp
, "return"));
450 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
454 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
458 qtest_qmp_eventwait(from
, "STOP");
461 qtest_qmp_eventwait(to
, "RESUME");
467 /* only launch the target process */
473 static MigrateStart
*migrate_start_new(void)
475 MigrateStart
*args
= g_new0(MigrateStart
, 1);
477 args
->opts_source
= g_strdup("");
478 args
->opts_target
= g_strdup("");
482 static void migrate_start_destroy(MigrateStart
*args
)
484 g_free(args
->opts_source
);
485 g_free(args
->opts_target
);
489 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
490 const char *uri
, MigrateStart
*args
)
492 gchar
*arch_source
, *arch_target
;
493 gchar
*cmd_source
, *cmd_target
;
494 const gchar
*ignore_stderr
;
495 char *bootpath
= NULL
;
498 const char *arch
= qtest_get_arch();
499 const char *machine_opts
= NULL
;
500 const char *memory_size
;
502 if (args
->use_shmem
) {
503 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
504 g_test_skip("/dev/shm is not supported");
510 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
511 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
512 /* the assembled x86 boot sector should be exactly one sector large */
513 assert(sizeof(x86_bootsect
) == 512);
514 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
515 memory_size
= "150M";
516 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
517 arch_target
= g_strdup(arch_source
);
518 start_address
= X86_TEST_MEM_START
;
519 end_address
= X86_TEST_MEM_END
;
520 } else if (g_str_equal(arch
, "s390x")) {
521 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
522 memory_size
= "128M";
523 arch_source
= g_strdup_printf("-bios %s", bootpath
);
524 arch_target
= g_strdup(arch_source
);
525 start_address
= S390_TEST_MEM_START
;
526 end_address
= S390_TEST_MEM_END
;
527 } else if (strcmp(arch
, "ppc64") == 0) {
528 machine_opts
= "vsmt=8";
529 memory_size
= "256M";
530 start_address
= PPC_TEST_MEM_START
;
531 end_address
= PPC_TEST_MEM_END
;
532 arch_source
= g_strdup_printf("-nodefaults "
533 "-prom-env 'use-nvramrc?=true' -prom-env "
534 "'nvramrc=hex .\" _\" begin %x %x "
535 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
536 "until'", end_address
, start_address
);
537 arch_target
= g_strdup("");
538 } else if (strcmp(arch
, "aarch64") == 0) {
539 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
540 machine_opts
= "virt,gic-version=max";
541 memory_size
= "150M";
542 arch_source
= g_strdup_printf("-cpu max "
545 arch_target
= g_strdup(arch_source
);
546 start_address
= ARM_TEST_MEM_START
;
547 end_address
= ARM_TEST_MEM_END
;
549 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
551 g_assert_not_reached();
556 if (args
->hide_stderr
) {
557 ignore_stderr
= "2>/dev/null";
562 if (args
->use_shmem
) {
563 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
564 shmem_opts
= g_strdup_printf(
565 "-object memory-backend-file,id=mem0,size=%s"
566 ",mem-path=%s,share=on -numa node,memdev=mem0",
567 memory_size
, shmem_path
);
570 shmem_opts
= g_strdup("");
573 cmd_source
= g_strdup_printf("-accel kvm -accel tcg%s%s "
574 "-name source,debug-threads=on "
576 "-serial file:%s/src_serial "
578 machine_opts
? " -machine " : "",
579 machine_opts
? machine_opts
: "",
581 arch_source
, shmem_opts
, args
->opts_source
,
584 if (!args
->only_target
) {
585 *from
= qtest_init(cmd_source
);
589 cmd_target
= g_strdup_printf("-accel kvm -accel tcg%s%s "
590 "-name target,debug-threads=on "
592 "-serial file:%s/dest_serial "
595 machine_opts
? " -machine " : "",
596 machine_opts
? machine_opts
: "",
597 memory_size
, tmpfs
, uri
,
598 arch_target
, shmem_opts
,
599 args
->opts_target
, ignore_stderr
);
601 *to
= qtest_init(cmd_target
);
606 * Remove shmem file immediately to avoid memory leak in test failed case.
607 * It's valid becase QEMU has already opened this file
609 if (args
->use_shmem
) {
614 migrate_start_destroy(args
);
618 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
620 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
625 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
627 /* Destination still running, wait for a byte to change */
629 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
631 } while (dest_byte_a
== dest_byte_b
);
633 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
635 /* With it stopped, check nothing changes */
636 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
638 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
639 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
641 check_guests_ram(to
);
647 cleanup("migsocket");
648 cleanup("src_serial");
649 cleanup("dest_serial");
652 static void deprecated_set_downtime(QTestState
*who
, const double value
)
657 "{ 'execute': 'migrate_set_downtime',"
658 " 'arguments': { 'value': %f } }", value
);
659 g_assert(qdict_haskey(rsp
, "return"));
661 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
664 static void deprecated_set_speed(QTestState
*who
, long long value
)
668 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
669 "'arguments': { 'value': %lld } }", value
);
670 g_assert(qdict_haskey(rsp
, "return"));
672 migrate_check_parameter_int(who
, "max-bandwidth", value
);
675 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
679 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
680 "'arguments': { 'value': %lld } }", value
);
681 g_assert(qdict_haskey(rsp
, "return"));
683 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
686 static void test_deprecated(void)
690 from
= qtest_init("-machine none");
692 deprecated_set_downtime(from
, 0.12345);
693 deprecated_set_speed(from
, 12345);
694 deprecated_set_cache_size(from
, 4096);
699 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
703 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
704 QTestState
*from
, *to
;
706 if (test_migrate_start(&from
, &to
, uri
, args
)) {
710 migrate_set_capability(from
, "postcopy-ram", true);
711 migrate_set_capability(to
, "postcopy-ram", true);
712 migrate_set_capability(to
, "postcopy-blocktime", true);
714 /* We want to pick a speed slow enough that the test completes
715 * quickly, but that it doesn't complete precopy even on a slow
716 * machine, so also set the downtime.
718 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
719 migrate_set_parameter_int(from
, "downtime-limit", 1);
721 /* Wait for the first serial output from the source */
722 wait_for_serial("src_serial");
724 migrate_qmp(from
, uri
, "{}");
727 wait_for_migration_pass(from
);
735 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
737 wait_for_migration_complete(from
);
739 /* Make sure we get at least one "B" on destination */
740 wait_for_serial("dest_serial");
742 if (uffd_feature_thread_id
) {
746 test_migrate_end(from
, to
, true);
749 static void test_postcopy(void)
751 MigrateStart
*args
= migrate_start_new();
752 QTestState
*from
, *to
;
754 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
757 migrate_postcopy_start(from
, to
);
758 migrate_postcopy_complete(from
, to
);
761 static void test_postcopy_recovery(void)
763 MigrateStart
*args
= migrate_start_new();
764 QTestState
*from
, *to
;
767 args
->hide_stderr
= true;
769 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
773 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
774 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
776 /* Now we start the postcopy */
777 migrate_postcopy_start(from
, to
);
780 * Wait until postcopy is really started; we can only run the
781 * migrate-pause command during a postcopy
783 wait_for_migration_status(from
, "postcopy-active", NULL
);
786 * Manually stop the postcopy migration. This emulates a network
787 * failure with the migration socket
792 * Wait for destination side to reach postcopy-paused state. The
793 * migrate-recover command can only succeed if destination machine
794 * is in the paused state
796 wait_for_migration_status(to
, "postcopy-paused",
797 (const char * []) { "failed", "active",
798 "completed", NULL
});
801 * Create a new socket to emulate a new channel that is different
802 * from the broken migration channel; tell the destination to
803 * listen to the new port
805 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
806 migrate_recover(to
, uri
);
809 * Try to rebuild the migration channel using the resume flag and
810 * the newly created channel
812 wait_for_migration_status(from
, "postcopy-paused",
813 (const char * []) { "failed", "active",
814 "completed", NULL
});
815 migrate_qmp(from
, uri
, "{'resume': true}");
818 /* Restore the postcopy bandwidth to unlimited */
819 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
821 migrate_postcopy_complete(from
, to
);
824 static void test_baddest(void)
826 MigrateStart
*args
= migrate_start_new();
827 QTestState
*from
, *to
;
829 args
->hide_stderr
= true;
831 if (test_migrate_start(&from
, &to
, "tcp:0:0", args
)) {
834 migrate_qmp(from
, "tcp:0:0", "{}");
835 wait_for_migration_fail(from
, false);
836 test_migrate_end(from
, to
, false);
839 static void test_precopy_unix(void)
841 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
842 MigrateStart
*args
= migrate_start_new();
843 QTestState
*from
, *to
;
845 if (test_migrate_start(&from
, &to
, uri
, args
)) {
849 /* We want to pick a speed slow enough that the test completes
850 * quickly, but that it doesn't complete precopy even on a slow
851 * machine, so also set the downtime.
853 /* 1 ms should make it not converge*/
854 migrate_set_parameter_int(from
, "downtime-limit", 1);
856 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
858 /* Wait for the first serial output from the source */
859 wait_for_serial("src_serial");
861 migrate_qmp(from
, uri
, "{}");
863 wait_for_migration_pass(from
);
865 /* 300 ms should converge */
866 migrate_set_parameter_int(from
, "downtime-limit", 300);
869 qtest_qmp_eventwait(from
, "STOP");
872 qtest_qmp_eventwait(to
, "RESUME");
874 wait_for_serial("dest_serial");
875 wait_for_migration_complete(from
);
877 test_migrate_end(from
, to
, true);
882 /* Currently upset on aarch64 TCG */
883 static void test_ignore_shared(void)
885 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
886 QTestState
*from
, *to
;
888 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
892 migrate_set_capability(from
, "x-ignore-shared", true);
893 migrate_set_capability(to
, "x-ignore-shared", true);
895 /* Wait for the first serial output from the source */
896 wait_for_serial("src_serial");
898 migrate_qmp(from
, uri
, "{}");
900 wait_for_migration_pass(from
);
903 qtest_qmp_eventwait(from
, "STOP");
906 qtest_qmp_eventwait(to
, "RESUME");
908 wait_for_serial("dest_serial");
909 wait_for_migration_complete(from
);
911 /* Check whether shared RAM has been really skipped */
912 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
914 test_migrate_end(from
, to
, true);
919 static void test_xbzrle(const char *uri
)
921 MigrateStart
*args
= migrate_start_new();
922 QTestState
*from
, *to
;
924 if (test_migrate_start(&from
, &to
, uri
, args
)) {
929 * We want to pick a speed slow enough that the test completes
930 * quickly, but that it doesn't complete precopy even on a slow
931 * machine, so also set the downtime.
933 /* 1 ms should make it not converge*/
934 migrate_set_parameter_int(from
, "downtime-limit", 1);
936 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
938 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
940 migrate_set_capability(from
, "xbzrle", "true");
941 migrate_set_capability(to
, "xbzrle", "true");
942 /* Wait for the first serial output from the source */
943 wait_for_serial("src_serial");
945 migrate_qmp(from
, uri
, "{}");
947 wait_for_migration_pass(from
);
949 /* 300ms should converge */
950 migrate_set_parameter_int(from
, "downtime-limit", 300);
953 qtest_qmp_eventwait(from
, "STOP");
955 qtest_qmp_eventwait(to
, "RESUME");
957 wait_for_serial("dest_serial");
958 wait_for_migration_complete(from
);
960 test_migrate_end(from
, to
, true);
963 static void test_xbzrle_unix(void)
965 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
971 static void test_precopy_tcp(void)
973 MigrateStart
*args
= migrate_start_new();
975 QTestState
*from
, *to
;
977 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
982 * We want to pick a speed slow enough that the test completes
983 * quickly, but that it doesn't complete precopy even on a slow
984 * machine, so also set the downtime.
986 /* 1 ms should make it not converge*/
987 migrate_set_parameter_int(from
, "downtime-limit", 1);
989 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
991 /* Wait for the first serial output from the source */
992 wait_for_serial("src_serial");
994 uri
= migrate_get_socket_address(to
, "socket-address");
996 migrate_qmp(from
, uri
, "{}");
998 wait_for_migration_pass(from
);
1000 /* 300ms should converge */
1001 migrate_set_parameter_int(from
, "downtime-limit", 300);
1004 qtest_qmp_eventwait(from
, "STOP");
1006 qtest_qmp_eventwait(to
, "RESUME");
1008 wait_for_serial("dest_serial");
1009 wait_for_migration_complete(from
);
1011 test_migrate_end(from
, to
, true);
1015 static void test_migrate_fd_proto(void)
1017 MigrateStart
*args
= migrate_start_new();
1018 QTestState
*from
, *to
;
1022 const char *error_desc
;
1024 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1029 * We want to pick a speed slow enough that the test completes
1030 * quickly, but that it doesn't complete precopy even on a slow
1031 * machine, so also set the downtime.
1033 /* 1 ms should make it not converge */
1034 migrate_set_parameter_int(from
, "downtime-limit", 1);
1036 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1038 /* Wait for the first serial output from the source */
1039 wait_for_serial("src_serial");
1041 /* Create two connected sockets for migration */
1042 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1043 g_assert_cmpint(ret
, ==, 0);
1045 /* Send the 1st socket to the target */
1046 rsp
= wait_command_fd(to
, pair
[0],
1047 "{ 'execute': 'getfd',"
1048 " 'arguments': { 'fdname': 'fd-mig' }}");
1052 /* Start incoming migration from the 1st socket */
1053 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1054 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1057 /* Send the 2nd socket to the target */
1058 rsp
= wait_command_fd(from
, pair
[1],
1059 "{ 'execute': 'getfd',"
1060 " 'arguments': { 'fdname': 'fd-mig' }}");
1064 /* Start migration to the 2nd socket*/
1065 migrate_qmp(from
, "fd:fd-mig", "{}");
1067 wait_for_migration_pass(from
);
1069 /* 300ms should converge */
1070 migrate_set_parameter_int(from
, "downtime-limit", 300);
1073 qtest_qmp_eventwait(from
, "STOP");
1075 qtest_qmp_eventwait(to
, "RESUME");
1077 /* Test closing fds */
1078 /* We assume, that QEMU removes named fd from its list,
1079 * so this should fail */
1080 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1081 " 'arguments': { 'fdname': 'fd-mig' }}");
1082 g_assert_true(qdict_haskey(rsp
, "error"));
1083 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1084 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1087 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1088 " 'arguments': { 'fdname': 'fd-mig' }}");
1089 g_assert_true(qdict_haskey(rsp
, "error"));
1090 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1091 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1094 /* Complete migration */
1095 wait_for_serial("dest_serial");
1096 wait_for_migration_complete(from
);
1097 test_migrate_end(from
, to
, true);
1100 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1102 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1103 QTestState
*from
, *to
;
1105 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1110 * UUID validation is at the begin of migration. So, the main process of
1111 * migration is not interesting for us here. Thus, set huge downtime for
1112 * very fast migration.
1114 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1115 migrate_set_capability(from
, "validate-uuid", true);
1117 /* Wait for the first serial output from the source */
1118 wait_for_serial("src_serial");
1120 migrate_qmp(from
, uri
, "{}");
1123 qtest_set_expected_status(to
, 1);
1124 wait_for_migration_fail(from
, true);
1126 wait_for_migration_complete(from
);
1129 test_migrate_end(from
, to
, false);
1133 static void test_validate_uuid(void)
1135 MigrateStart
*args
= migrate_start_new();
1137 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1138 args
->opts_target
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1139 do_test_validate_uuid(args
, false);
1142 static void test_validate_uuid_error(void)
1144 MigrateStart
*args
= migrate_start_new();
1146 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1147 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1148 args
->hide_stderr
= true;
1149 do_test_validate_uuid(args
, true);
1152 static void test_validate_uuid_src_not_set(void)
1154 MigrateStart
*args
= migrate_start_new();
1156 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1157 args
->hide_stderr
= true;
1158 do_test_validate_uuid(args
, false);
1161 static void test_validate_uuid_dst_not_set(void)
1163 MigrateStart
*args
= migrate_start_new();
1165 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1166 args
->hide_stderr
= true;
1167 do_test_validate_uuid(args
, false);
1170 static void test_migrate_auto_converge(void)
1172 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1173 MigrateStart
*args
= migrate_start_new();
1174 QTestState
*from
, *to
;
1175 int64_t remaining
, percentage
;
1178 * We want the test to be stable and as fast as possible.
1179 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1180 * so we need to decrease a bandwidth.
1182 const int64_t init_pct
= 5, inc_pct
= 50, max_pct
= 95;
1183 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
1184 const int64_t downtime_limit
= 250; /* 250ms */
1186 * We migrate through unix-socket (> 500Mb/s).
1187 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1188 * So, we can predict expected_threshold
1190 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
1192 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1196 migrate_set_capability(from
, "auto-converge", true);
1197 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1198 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1199 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1202 * Set the initial parameters so that the migration could not converge
1203 * without throttling.
1205 migrate_set_parameter_int(from
, "downtime-limit", 1);
1206 migrate_set_parameter_int(from
, "max-bandwidth", 100000000); /* ~100Mb/s */
1208 /* To check remaining size after precopy */
1209 migrate_set_capability(from
, "pause-before-switchover", true);
1211 /* Wait for the first serial output from the source */
1212 wait_for_serial("src_serial");
1214 migrate_qmp(from
, uri
, "{}");
1216 /* Wait for throttling begins */
1218 while (percentage
== 0) {
1219 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1221 g_assert_false(got_stop
);
1223 /* The first percentage of throttling should be equal to init_pct */
1224 g_assert_cmpint(percentage
, ==, init_pct
);
1225 /* Now, when we tested that throttling works, let it converge */
1226 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
1227 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
1230 * Wait for pre-switchover status to check last throttle percentage
1231 * and remaining. These values will be zeroed later
1233 wait_for_migration_status(from
, "pre-switchover", NULL
);
1235 /* The final percentage of throttling shouldn't be greater than max_pct */
1236 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1237 g_assert_cmpint(percentage
, <=, max_pct
);
1239 remaining
= read_ram_property_int(from
, "remaining");
1240 g_assert_cmpint(remaining
, <, expected_threshold
);
1242 migrate_continue(from
, "pre-switchover");
1244 qtest_qmp_eventwait(to
, "RESUME");
1246 wait_for_serial("dest_serial");
1247 wait_for_migration_complete(from
);
1251 test_migrate_end(from
, to
, true);
1254 static void test_multifd_tcp(void)
1256 MigrateStart
*args
= migrate_start_new();
1257 QTestState
*from
, *to
;
1261 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1266 * We want to pick a speed slow enough that the test completes
1267 * quickly, but that it doesn't complete precopy even on a slow
1268 * machine, so also set the downtime.
1270 /* 1 ms should make it not converge*/
1271 migrate_set_parameter_int(from
, "downtime-limit", 1);
1273 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1275 migrate_set_parameter_int(from
, "multifd-channels", 16);
1276 migrate_set_parameter_int(to
, "multifd-channels", 16);
1278 migrate_set_capability(from
, "multifd", "true");
1279 migrate_set_capability(to
, "multifd", "true");
1281 /* Start incoming migration from the 1st socket */
1282 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1283 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1286 /* Wait for the first serial output from the source */
1287 wait_for_serial("src_serial");
1289 uri
= migrate_get_socket_address(to
, "socket-address");
1291 migrate_qmp(from
, uri
, "{}");
1293 wait_for_migration_pass(from
);
1295 /* 300ms it should converge */
1296 migrate_set_parameter_int(from
, "downtime-limit", 300);
1299 qtest_qmp_eventwait(from
, "STOP");
1301 qtest_qmp_eventwait(to
, "RESUME");
1303 wait_for_serial("dest_serial");
1304 wait_for_migration_complete(from
);
1305 test_migrate_end(from
, to
, true);
1315 * launch another target
1318 * And see that it works
1321 static void test_multifd_tcp_cancel(void)
1323 MigrateStart
*args
= migrate_start_new();
1324 QTestState
*from
, *to
, *to2
;
1328 args
->hide_stderr
= true;
1330 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1335 * We want to pick a speed slow enough that the test completes
1336 * quickly, but that it doesn't complete precopy even on a slow
1337 * machine, so also set the downtime.
1339 /* 1 ms should make it not converge*/
1340 migrate_set_parameter_int(from
, "downtime-limit", 1);
1342 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
1344 migrate_set_parameter_int(from
, "multifd-channels", 16);
1345 migrate_set_parameter_int(to
, "multifd-channels", 16);
1347 migrate_set_capability(from
, "multifd", "true");
1348 migrate_set_capability(to
, "multifd", "true");
1350 /* Start incoming migration from the 1st socket */
1351 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1352 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1355 /* Wait for the first serial output from the source */
1356 wait_for_serial("src_serial");
1358 uri
= migrate_get_socket_address(to
, "socket-address");
1360 migrate_qmp(from
, uri
, "{}");
1362 wait_for_migration_pass(from
);
1364 migrate_cancel(from
);
1366 args
= migrate_start_new();
1367 args
->only_target
= true;
1369 if (test_migrate_start(&from
, &to2
, "defer", args
)) {
1373 migrate_set_parameter_int(to2
, "multifd-channels", 16);
1375 migrate_set_capability(to2
, "multifd", "true");
1377 /* Start incoming migration from the 1st socket */
1378 rsp
= wait_command(to2
, "{ 'execute': 'migrate-incoming',"
1379 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1382 uri
= migrate_get_socket_address(to2
, "socket-address");
1384 wait_for_migration_status(from
, "cancelled", NULL
);
1386 /* 300ms it should converge */
1387 migrate_set_parameter_int(from
, "downtime-limit", 300);
1389 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1391 migrate_qmp(from
, uri
, "{}");
1393 wait_for_migration_pass(from
);
1396 qtest_qmp_eventwait(from
, "STOP");
1398 qtest_qmp_eventwait(to2
, "RESUME");
1400 wait_for_serial("dest_serial");
1401 wait_for_migration_complete(from
);
1402 test_migrate_end(from
, to2
, true);
1406 int main(int argc
, char **argv
)
1408 char template[] = "/tmp/migration-test-XXXXXX";
1411 g_test_init(&argc
, &argv
, NULL
);
1413 if (!ufd_version_check()) {
1414 return g_test_run();
1418 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1419 * is touchy due to race conditions on dirty bits (especially on PPC for
1422 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1423 (access("/sys/module/kvm_hv", F_OK
) ||
1424 access("/dev/kvm", R_OK
| W_OK
))) {
1425 g_test_message("Skipping test: kvm_hv not available");
1426 return g_test_run();
1430 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1431 * there until the problems are resolved
1433 if (g_str_equal(qtest_get_arch(), "s390x")) {
1434 #if defined(HOST_S390X)
1435 if (access("/dev/kvm", R_OK
| W_OK
)) {
1436 g_test_message("Skipping test: kvm not available");
1437 return g_test_run();
1440 g_test_message("Skipping test: Need s390x host to work properly");
1441 return g_test_run();
1445 tmpfs
= mkdtemp(template);
1447 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1451 module_call_init(MODULE_INIT_QOM
);
1453 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1454 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1455 qtest_add_func("/migration/deprecated", test_deprecated
);
1456 qtest_add_func("/migration/bad_dest", test_baddest
);
1457 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1458 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1459 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1460 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1461 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1462 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
1463 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
1464 qtest_add_func("/migration/validate_uuid_src_not_set",
1465 test_validate_uuid_src_not_set
);
1466 qtest_add_func("/migration/validate_uuid_dst_not_set",
1467 test_validate_uuid_dst_not_set
);
1469 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
1470 qtest_add_func("/migration/multifd/tcp", test_multifd_tcp
);
1471 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel
);
1475 g_assert_cmpint(ret
, ==, 0);
1479 g_test_message("unable to rmdir: path (%s): %s",
1480 tmpfs
, strerror(errno
));