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 "qapi/qmp/qjson.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/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
;
35 static bool uffd_feature_thread_id
;
37 #if defined(__linux__)
38 #include <sys/syscall.h>
42 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
43 #include <sys/eventfd.h>
44 #include <sys/ioctl.h>
45 #include <linux/userfaultfd.h>
47 static bool ufd_version_check(void)
49 struct uffdio_api api_struct
;
52 int ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
55 g_test_message("Skipping test: userfaultfd not available");
59 api_struct
.api
= UFFD_API
;
60 api_struct
.features
= 0;
61 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
62 g_test_message("Skipping test: UFFDIO_API failed");
65 uffd_feature_thread_id
= api_struct
.features
& UFFD_FEATURE_THREAD_ID
;
67 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
68 (__u64
)1 << _UFFDIO_UNREGISTER
;
69 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
70 g_test_message("Skipping test: Missing userfault feature");
78 static bool ufd_version_check(void)
80 g_test_message("Skipping test: Userfault not available (builtdtime)");
86 static const char *tmpfs
;
88 /* The boot file modifies memory area in [start_address, end_address)
89 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
91 #include "tests/migration/i386/a-b-bootblock.h"
92 #include "tests/migration/aarch64/a-b-kernel.h"
93 #include "tests/migration/s390x/a-b-bios.h"
95 static void init_bootfile(const char *bootpath
, void *content
, size_t len
)
97 FILE *bootfile
= fopen(bootpath
, "wb");
99 g_assert_cmpint(fwrite(content
, len
, 1, bootfile
), ==, 1);
104 * Wait for some output in the serial output file,
105 * we get an 'A' followed by an endless string of 'B's
106 * but on the destination we won't have the A.
108 static void wait_for_serial(const char *side
)
110 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
111 FILE *serialfile
= fopen(serialpath
, "r");
112 const char *arch
= qtest_get_arch();
113 int started
= (strcmp(side
, "src_serial") == 0 &&
114 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
118 int readvalue
= fgetc(serialfile
);
121 /* SLOF prints its banner before starting test,
122 * to ignore it, mark the start of the test with '_',
123 * ignore all characters until this marker
130 fseek(serialfile
, 0, SEEK_SET
);
147 started
= (strcmp(side
, "src_serial") == 0 &&
148 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
149 fseek(serialfile
, 0, SEEK_SET
);
154 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
155 g_assert_not_reached();
160 static void stop_cb(void *opaque
, const char *name
, QDict
*data
)
162 if (!strcmp(name
, "STOP")) {
168 * Events can get in the way of responses we are actually waiting for.
171 static QDict
*wait_command_fd(QTestState
*who
, int fd
, const char *command
, ...)
175 va_start(ap
, command
);
176 qtest_qmp_vsend_fds(who
, &fd
, 1, command
, ap
);
179 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
183 * Events can get in the way of responses we are actually waiting for.
186 static QDict
*wait_command(QTestState
*who
, const char *command
, ...)
190 va_start(ap
, command
);
191 qtest_qmp_vsend(who
, command
, ap
);
194 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
198 * Note: caller is responsible to free the returned object via
199 * qobject_unref() after use
201 static QDict
*migrate_query(QTestState
*who
)
203 return wait_command(who
, "{ 'execute': 'query-migrate' }");
207 * Note: caller is responsible to free the returned object via
210 static gchar
*migrate_query_status(QTestState
*who
)
212 QDict
*rsp_return
= migrate_query(who
);
213 gchar
*status
= g_strdup(qdict_get_str(rsp_return
, "status"));
216 qobject_unref(rsp_return
);
222 * It's tricky to use qemu's migration event capability with qtest,
223 * events suddenly appearing confuse the qmp()/hmp() responses.
226 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
228 QDict
*rsp_return
, *rsp_ram
;
231 rsp_return
= migrate_query(who
);
232 if (!qdict_haskey(rsp_return
, "ram")) {
236 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
237 result
= qdict_get_try_int(rsp_ram
, property
, 0);
239 qobject_unref(rsp_return
);
243 static int64_t read_migrate_property_int(QTestState
*who
, const char *property
)
248 rsp_return
= migrate_query(who
);
249 result
= qdict_get_try_int(rsp_return
, property
, 0);
250 qobject_unref(rsp_return
);
254 static uint64_t get_migration_pass(QTestState
*who
)
256 return read_ram_property_int(who
, "dirty-sync-count");
259 static void read_blocktime(QTestState
*who
)
263 rsp_return
= migrate_query(who
);
264 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
265 qobject_unref(rsp_return
);
268 static bool check_migration_status(QTestState
*who
, const char *goal
,
269 const char **ungoals
)
272 char *current_status
;
275 current_status
= migrate_query_status(who
);
276 ready
= strcmp(current_status
, goal
) == 0;
278 g_assert_cmpstr(current_status
, !=, "failed");
280 * If looking for a state other than completed,
281 * completion of migration would cause the test to
284 if (strcmp(goal
, "completed") != 0) {
285 g_assert_cmpstr(current_status
, !=, "completed");
288 for (ungoal
= ungoals
; *ungoal
; ungoal
++) {
289 g_assert_cmpstr(current_status
, !=, *ungoal
);
292 g_free(current_status
);
296 static void wait_for_migration_status(QTestState
*who
,
298 const char **ungoals
)
300 while (!check_migration_status(who
, goal
, ungoals
)) {
305 static void wait_for_migration_complete(QTestState
*who
)
307 wait_for_migration_status(who
, "completed", NULL
);
310 static void wait_for_migration_pass(QTestState
*who
)
312 uint64_t initial_pass
= get_migration_pass(who
);
315 /* Wait for the 1st sync */
316 while (!got_stop
&& !initial_pass
) {
318 initial_pass
= get_migration_pass(who
);
323 pass
= get_migration_pass(who
);
324 } while (pass
== initial_pass
&& !got_stop
);
327 static void check_guests_ram(QTestState
*who
)
329 /* Our ASM test will have been incrementing one byte from each page from
330 * start_address to < end_address in order. This gives us a constraint
331 * that any page's byte should be equal or less than the previous pages
332 * byte (mod 256); and they should all be equal except for one transition
333 * at the point where we meet the incrementer. (We're running this with
334 * the guest stopped).
339 bool hit_edge
= false;
342 qtest_memread(who
, start_address
, &first_byte
, 1);
343 last_byte
= first_byte
;
345 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
346 address
+= TEST_MEM_PAGE_SIZE
)
349 qtest_memread(who
, address
, &b
, 1);
350 if (b
!= last_byte
) {
351 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
352 /* This is OK, the guest stopped at the point of
353 * incrementing the previous page but didn't get
361 fprintf(stderr
, "Memory content inconsistency at %x"
362 " first_byte = %x last_byte = %x current = %x"
364 address
, first_byte
, last_byte
, b
, hit_edge
);
370 fprintf(stderr
, "and in another %d pages", bad
- 10);
375 static void cleanup(const char *filename
)
377 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
383 static char *SocketAddress_to_str(SocketAddress
*addr
)
385 switch (addr
->type
) {
386 case SOCKET_ADDRESS_TYPE_INET
:
387 return g_strdup_printf("tcp:%s:%s",
390 case SOCKET_ADDRESS_TYPE_UNIX
:
391 return g_strdup_printf("unix:%s",
392 addr
->u
.q_unix
.path
);
393 case SOCKET_ADDRESS_TYPE_FD
:
394 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
395 case SOCKET_ADDRESS_TYPE_VSOCK
:
396 return g_strdup_printf("tcp:%s:%s",
400 return g_strdup("unknown address type");
404 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
408 Error
*local_err
= NULL
;
409 SocketAddressList
*addrs
;
413 rsp
= migrate_query(who
);
414 object
= qdict_get(rsp
, parameter
);
416 iv
= qobject_input_visitor_new(object
);
417 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &local_err
);
420 /* we are only using a single address */
421 result
= SocketAddress_to_str(addrs
->value
);
423 qapi_free_SocketAddressList(addrs
);
428 static long long migrate_get_parameter_int(QTestState
*who
,
429 const char *parameter
)
434 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
435 result
= qdict_get_int(rsp
, parameter
);
440 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
445 result
= migrate_get_parameter_int(who
, parameter
);
446 g_assert_cmpint(result
, ==, value
);
449 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
455 "{ 'execute': 'migrate-set-parameters',"
456 "'arguments': { %s: %lld } }",
458 g_assert(qdict_haskey(rsp
, "return"));
460 migrate_check_parameter_int(who
, parameter
, value
);
463 static void migrate_pause(QTestState
*who
)
467 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
471 static void migrate_continue(QTestState
*who
, const char *state
)
475 rsp
= wait_command(who
,
476 "{ 'execute': 'migrate-continue',"
477 " 'arguments': { 'state': %s } }",
482 static void migrate_recover(QTestState
*who
, const char *uri
)
486 rsp
= wait_command(who
,
487 "{ 'execute': 'migrate-recover', "
488 " 'id': 'recover-cmd', "
489 " 'arguments': { 'uri': %s } }",
494 static void migrate_set_capability(QTestState
*who
, const char *capability
,
500 "{ 'execute': 'migrate-set-capabilities',"
502 "'capabilities': [ { "
503 "'capability': %s, 'state': %i } ] } }",
505 g_assert(qdict_haskey(rsp
, "return"));
510 * Send QMP command "migrate".
511 * Arguments are built from @fmt... (formatted like
512 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
515 static void migrate(QTestState
*who
, const char *uri
, const char *fmt
, ...)
521 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
524 g_assert(!qdict_haskey(args
, "uri"));
525 qdict_put_str(args
, "uri", uri
);
527 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate', 'arguments': %p}", args
);
529 g_assert(qdict_haskey(rsp
, "return"));
533 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
537 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
541 qtest_qmp_eventwait(from
, "STOP");
544 qtest_qmp_eventwait(to
, "RESUME");
554 static MigrateStart
*migrate_start_new(void)
556 MigrateStart
*args
= g_new0(MigrateStart
, 1);
558 args
->opts_source
= g_strdup("");
559 args
->opts_target
= g_strdup("");
563 static void migrate_start_destroy(MigrateStart
*args
)
565 g_free(args
->opts_source
);
566 g_free(args
->opts_target
);
570 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
571 const char *uri
, MigrateStart
*args
)
573 gchar
*arch_source
, *arch_target
;
574 gchar
*cmd_source
, *cmd_target
;
575 const gchar
*ignore_stderr
;
576 char *bootpath
= NULL
;
579 const char *arch
= qtest_get_arch();
580 const char *machine_opts
= NULL
;
581 const char *memory_size
;
583 if (args
->use_shmem
) {
584 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
585 g_test_skip("/dev/shm is not supported");
591 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
592 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
593 /* the assembled x86 boot sector should be exactly one sector large */
594 assert(sizeof(x86_bootsect
) == 512);
595 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
596 memory_size
= "150M";
597 arch_source
= g_strdup_printf("-drive file=%s,format=raw", bootpath
);
598 arch_target
= g_strdup(arch_source
);
599 start_address
= X86_TEST_MEM_START
;
600 end_address
= X86_TEST_MEM_END
;
601 } else if (g_str_equal(arch
, "s390x")) {
602 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
603 memory_size
= "128M";
604 arch_source
= g_strdup_printf("-bios %s", bootpath
);
605 arch_target
= g_strdup(arch_source
);
606 start_address
= S390_TEST_MEM_START
;
607 end_address
= S390_TEST_MEM_END
;
608 } else if (strcmp(arch
, "ppc64") == 0) {
609 machine_opts
= "vsmt=8";
610 memory_size
= "256M";
611 arch_source
= g_strdup_printf("-nodefaults "
612 "-prom-env 'use-nvramrc?=true' -prom-env "
613 "'nvramrc=hex .\" _\" begin %x %x "
614 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
615 "until'", end_address
, start_address
);
616 arch_target
= g_strdup("");
617 start_address
= PPC_TEST_MEM_START
;
618 end_address
= PPC_TEST_MEM_END
;
619 } else if (strcmp(arch
, "aarch64") == 0) {
620 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
621 machine_opts
= "virt,gic-version=max";
622 memory_size
= "150M";
623 arch_source
= g_strdup_printf("-cpu max "
626 arch_target
= g_strdup(arch_source
);
627 start_address
= ARM_TEST_MEM_START
;
628 end_address
= ARM_TEST_MEM_END
;
630 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
632 g_assert_not_reached();
637 if (args
->hide_stderr
) {
638 ignore_stderr
= "2>/dev/null";
643 if (args
->use_shmem
) {
644 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
645 shmem_opts
= g_strdup_printf(
646 "-object memory-backend-file,id=mem0,size=%s"
647 ",mem-path=%s,share=on -numa node,memdev=mem0",
648 memory_size
, shmem_path
);
651 shmem_opts
= g_strdup("");
654 cmd_source
= g_strdup_printf("-accel kvm -accel tcg%s%s "
655 "-name source,debug-threads=on "
657 "-serial file:%s/src_serial "
659 machine_opts
? " -machine " : "",
660 machine_opts
? machine_opts
: "",
662 arch_source
, shmem_opts
, args
->opts_source
,
665 *from
= qtest_init(cmd_source
);
668 cmd_target
= g_strdup_printf("-accel kvm -accel tcg%s%s "
669 "-name target,debug-threads=on "
671 "-serial file:%s/dest_serial "
674 machine_opts
? " -machine " : "",
675 machine_opts
? machine_opts
: "",
676 memory_size
, tmpfs
, uri
,
677 arch_target
, shmem_opts
,
678 args
->opts_target
, ignore_stderr
);
680 *to
= qtest_init(cmd_target
);
685 * Remove shmem file immediately to avoid memory leak in test failed case.
686 * It's valid becase QEMU has already opened this file
688 if (args
->use_shmem
) {
693 migrate_start_destroy(args
);
697 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
699 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
704 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
706 /* Destination still running, wait for a byte to change */
708 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
710 } while (dest_byte_a
== dest_byte_b
);
712 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
714 /* With it stopped, check nothing changes */
715 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
717 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
718 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
720 check_guests_ram(to
);
726 cleanup("migsocket");
727 cleanup("src_serial");
728 cleanup("dest_serial");
731 static void deprecated_set_downtime(QTestState
*who
, const double value
)
736 "{ 'execute': 'migrate_set_downtime',"
737 " 'arguments': { 'value': %f } }", value
);
738 g_assert(qdict_haskey(rsp
, "return"));
740 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
743 static void deprecated_set_speed(QTestState
*who
, long long value
)
747 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
748 "'arguments': { 'value': %lld } }", value
);
749 g_assert(qdict_haskey(rsp
, "return"));
751 migrate_check_parameter_int(who
, "max-bandwidth", value
);
754 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
758 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
759 "'arguments': { 'value': %lld } }", value
);
760 g_assert(qdict_haskey(rsp
, "return"));
762 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
765 static void test_deprecated(void)
769 from
= qtest_init("-machine none");
771 deprecated_set_downtime(from
, 0.12345);
772 deprecated_set_speed(from
, 12345);
773 deprecated_set_cache_size(from
, 4096);
778 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
782 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
783 QTestState
*from
, *to
;
785 if (test_migrate_start(&from
, &to
, uri
, args
)) {
789 migrate_set_capability(from
, "postcopy-ram", true);
790 migrate_set_capability(to
, "postcopy-ram", true);
791 migrate_set_capability(to
, "postcopy-blocktime", true);
793 /* We want to pick a speed slow enough that the test completes
794 * quickly, but that it doesn't complete precopy even on a slow
795 * machine, so also set the downtime.
797 migrate_set_parameter_int(from
, "max-bandwidth", 30000000);
798 migrate_set_parameter_int(from
, "downtime-limit", 1);
800 /* Wait for the first serial output from the source */
801 wait_for_serial("src_serial");
803 migrate(from
, uri
, "{}");
806 wait_for_migration_pass(from
);
814 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
816 wait_for_migration_complete(from
);
818 /* Make sure we get at least one "B" on destination */
819 wait_for_serial("dest_serial");
821 if (uffd_feature_thread_id
) {
825 test_migrate_end(from
, to
, true);
828 static void test_postcopy(void)
830 MigrateStart
*args
= migrate_start_new();
831 QTestState
*from
, *to
;
833 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
836 migrate_postcopy_start(from
, to
);
837 migrate_postcopy_complete(from
, to
);
840 static void test_postcopy_recovery(void)
842 MigrateStart
*args
= migrate_start_new();
843 QTestState
*from
, *to
;
846 args
->hide_stderr
= true;
848 if (migrate_postcopy_prepare(&from
, &to
, args
)) {
852 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
853 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
855 /* Now we start the postcopy */
856 migrate_postcopy_start(from
, to
);
859 * Wait until postcopy is really started; we can only run the
860 * migrate-pause command during a postcopy
862 wait_for_migration_status(from
, "postcopy-active", NULL
);
865 * Manually stop the postcopy migration. This emulates a network
866 * failure with the migration socket
871 * Wait for destination side to reach postcopy-paused state. The
872 * migrate-recover command can only succeed if destination machine
873 * is in the paused state
875 wait_for_migration_status(to
, "postcopy-paused",
876 (const char * []) { "failed", "active",
877 "completed", NULL
});
880 * Create a new socket to emulate a new channel that is different
881 * from the broken migration channel; tell the destination to
882 * listen to the new port
884 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
885 migrate_recover(to
, uri
);
888 * Try to rebuild the migration channel using the resume flag and
889 * the newly created channel
891 wait_for_migration_status(from
, "postcopy-paused",
892 (const char * []) { "failed", "active",
893 "completed", NULL
});
894 migrate(from
, uri
, "{'resume': true}");
897 /* Restore the postcopy bandwidth to unlimited */
898 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
900 migrate_postcopy_complete(from
, to
);
903 static void wait_for_migration_fail(QTestState
*from
, bool allow_active
)
910 status
= migrate_query_status(from
);
911 bool result
= !strcmp(status
, "setup") || !strcmp(status
, "failed") ||
912 (allow_active
&& !strcmp(status
, "active"));
914 fprintf(stderr
, "%s: unexpected status status=%s allow_active=%d\n",
915 __func__
, status
, allow_active
);
918 failed
= !strcmp(status
, "failed");
922 /* Is the machine currently running? */
923 rsp_return
= wait_command(from
, "{ 'execute': 'query-status' }");
924 g_assert(qdict_haskey(rsp_return
, "running"));
925 g_assert(qdict_get_bool(rsp_return
, "running"));
926 qobject_unref(rsp_return
);
929 static void test_baddest(void)
931 MigrateStart
*args
= migrate_start_new();
932 QTestState
*from
, *to
;
934 args
->hide_stderr
= true;
936 if (test_migrate_start(&from
, &to
, "tcp:0:0", args
)) {
939 migrate(from
, "tcp:0:0", "{}");
940 wait_for_migration_fail(from
, false);
941 test_migrate_end(from
, to
, false);
944 static void test_precopy_unix(void)
946 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
947 MigrateStart
*args
= migrate_start_new();
948 QTestState
*from
, *to
;
950 if (test_migrate_start(&from
, &to
, uri
, 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 migrate(from
, uri
, "{}");
968 wait_for_migration_pass(from
);
970 /* 300 ms should converge */
971 migrate_set_parameter_int(from
, "downtime-limit", 300);
974 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);
987 /* Currently upset on aarch64 TCG */
988 static void test_ignore_shared(void)
990 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
991 QTestState
*from
, *to
;
993 if (test_migrate_start(&from
, &to
, uri
, false, true, NULL
, NULL
)) {
997 migrate_set_capability(from
, "x-ignore-shared", true);
998 migrate_set_capability(to
, "x-ignore-shared", true);
1000 /* Wait for the first serial output from the source */
1001 wait_for_serial("src_serial");
1003 migrate(from
, uri
, "{}");
1005 wait_for_migration_pass(from
);
1008 qtest_qmp_eventwait(from
, "STOP");
1011 qtest_qmp_eventwait(to
, "RESUME");
1013 wait_for_serial("dest_serial");
1014 wait_for_migration_complete(from
);
1016 /* Check whether shared RAM has been really skipped */
1017 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
1019 test_migrate_end(from
, to
, true);
1024 static void test_xbzrle(const char *uri
)
1026 MigrateStart
*args
= migrate_start_new();
1027 QTestState
*from
, *to
;
1029 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1034 * We want to pick a speed slow enough that the test completes
1035 * quickly, but that it doesn't complete precopy even on a slow
1036 * machine, so also set the downtime.
1038 /* 1 ms should make it not converge*/
1039 migrate_set_parameter_int(from
, "downtime-limit", 1);
1041 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1043 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
1045 migrate_set_capability(from
, "xbzrle", "true");
1046 migrate_set_capability(to
, "xbzrle", "true");
1047 /* Wait for the first serial output from the source */
1048 wait_for_serial("src_serial");
1050 migrate(from
, uri
, "{}");
1052 wait_for_migration_pass(from
);
1054 /* 300ms should converge */
1055 migrate_set_parameter_int(from
, "downtime-limit", 300);
1058 qtest_qmp_eventwait(from
, "STOP");
1060 qtest_qmp_eventwait(to
, "RESUME");
1062 wait_for_serial("dest_serial");
1063 wait_for_migration_complete(from
);
1065 test_migrate_end(from
, to
, true);
1068 static void test_xbzrle_unix(void)
1070 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1076 static void test_precopy_tcp(void)
1078 MigrateStart
*args
= migrate_start_new();
1080 QTestState
*from
, *to
;
1082 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", args
)) {
1087 * We want to pick a speed slow enough that the test completes
1088 * quickly, but that it doesn't complete precopy even on a slow
1089 * machine, so also set the downtime.
1091 /* 1 ms should make it not converge*/
1092 migrate_set_parameter_int(from
, "downtime-limit", 1);
1094 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1096 /* Wait for the first serial output from the source */
1097 wait_for_serial("src_serial");
1099 uri
= migrate_get_socket_address(to
, "socket-address");
1101 migrate(from
, uri
, "{}");
1103 wait_for_migration_pass(from
);
1105 /* 300ms should converge */
1106 migrate_set_parameter_int(from
, "downtime-limit", 300);
1109 qtest_qmp_eventwait(from
, "STOP");
1111 qtest_qmp_eventwait(to
, "RESUME");
1113 wait_for_serial("dest_serial");
1114 wait_for_migration_complete(from
);
1116 test_migrate_end(from
, to
, true);
1120 static void test_migrate_fd_proto(void)
1122 MigrateStart
*args
= migrate_start_new();
1123 QTestState
*from
, *to
;
1127 const char *error_desc
;
1129 if (test_migrate_start(&from
, &to
, "defer", args
)) {
1134 * We want to pick a speed slow enough that the test completes
1135 * quickly, but that it doesn't complete precopy even on a slow
1136 * machine, so also set the downtime.
1138 /* 1 ms should make it not converge */
1139 migrate_set_parameter_int(from
, "downtime-limit", 1);
1141 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1143 /* Wait for the first serial output from the source */
1144 wait_for_serial("src_serial");
1146 /* Create two connected sockets for migration */
1147 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1148 g_assert_cmpint(ret
, ==, 0);
1150 /* Send the 1st socket to the target */
1151 rsp
= wait_command_fd(to
, pair
[0],
1152 "{ 'execute': 'getfd',"
1153 " 'arguments': { 'fdname': 'fd-mig' }}");
1157 /* Start incoming migration from the 1st socket */
1158 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1159 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1162 /* Send the 2nd socket to the target */
1163 rsp
= wait_command_fd(from
, pair
[1],
1164 "{ 'execute': 'getfd',"
1165 " 'arguments': { 'fdname': 'fd-mig' }}");
1169 /* Start migration to the 2nd socket*/
1170 migrate(from
, "fd:fd-mig", "{}");
1172 wait_for_migration_pass(from
);
1174 /* 300ms should converge */
1175 migrate_set_parameter_int(from
, "downtime-limit", 300);
1178 qtest_qmp_eventwait(from
, "STOP");
1180 qtest_qmp_eventwait(to
, "RESUME");
1182 /* Test closing fds */
1183 /* We assume, that QEMU removes named fd from its list,
1184 * so this should fail */
1185 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1186 " 'arguments': { 'fdname': 'fd-mig' }}");
1187 g_assert_true(qdict_haskey(rsp
, "error"));
1188 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1189 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1192 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1193 " 'arguments': { 'fdname': 'fd-mig' }}");
1194 g_assert_true(qdict_haskey(rsp
, "error"));
1195 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1196 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1199 /* Complete migration */
1200 wait_for_serial("dest_serial");
1201 wait_for_migration_complete(from
);
1202 test_migrate_end(from
, to
, true);
1205 static void do_test_validate_uuid(MigrateStart
*args
, bool should_fail
)
1207 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1208 QTestState
*from
, *to
;
1210 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1215 * UUID validation is at the begin of migration. So, the main process of
1216 * migration is not interesting for us here. Thus, set huge downtime for
1217 * very fast migration.
1219 migrate_set_parameter_int(from
, "downtime-limit", 1000000);
1220 migrate_set_capability(from
, "validate-uuid", true);
1222 /* Wait for the first serial output from the source */
1223 wait_for_serial("src_serial");
1225 migrate(from
, uri
, "{}");
1228 qtest_set_expected_status(to
, 1);
1229 wait_for_migration_fail(from
, true);
1231 wait_for_migration_complete(from
);
1234 test_migrate_end(from
, to
, false);
1238 static void test_validate_uuid(void)
1240 MigrateStart
*args
= migrate_start_new();
1242 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1243 args
->opts_target
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1244 do_test_validate_uuid(args
, false);
1247 static void test_validate_uuid_error(void)
1249 MigrateStart
*args
= migrate_start_new();
1251 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1252 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1253 args
->hide_stderr
= true;
1254 do_test_validate_uuid(args
, true);
1257 static void test_validate_uuid_src_not_set(void)
1259 MigrateStart
*args
= migrate_start_new();
1261 args
->opts_target
= g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1262 args
->hide_stderr
= true;
1263 do_test_validate_uuid(args
, false);
1266 static void test_validate_uuid_dst_not_set(void)
1268 MigrateStart
*args
= migrate_start_new();
1270 args
->opts_source
= g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1271 args
->hide_stderr
= true;
1272 do_test_validate_uuid(args
, false);
1275 static void test_migrate_auto_converge(void)
1277 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1278 MigrateStart
*args
= migrate_start_new();
1279 QTestState
*from
, *to
;
1280 int64_t remaining
, percentage
;
1283 * We want the test to be stable and as fast as possible.
1284 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1285 * so we need to decrease a bandwidth.
1287 const int64_t init_pct
= 5, inc_pct
= 50, max_pct
= 95;
1288 const int64_t max_bandwidth
= 400000000; /* ~400Mb/s */
1289 const int64_t downtime_limit
= 250; /* 250ms */
1291 * We migrate through unix-socket (> 500Mb/s).
1292 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1293 * So, we can predict expected_threshold
1295 const int64_t expected_threshold
= max_bandwidth
* downtime_limit
/ 1000;
1297 if (test_migrate_start(&from
, &to
, uri
, args
)) {
1301 migrate_set_capability(from
, "auto-converge", true);
1302 migrate_set_parameter_int(from
, "cpu-throttle-initial", init_pct
);
1303 migrate_set_parameter_int(from
, "cpu-throttle-increment", inc_pct
);
1304 migrate_set_parameter_int(from
, "max-cpu-throttle", max_pct
);
1307 * Set the initial parameters so that the migration could not converge
1308 * without throttling.
1310 migrate_set_parameter_int(from
, "downtime-limit", 1);
1311 migrate_set_parameter_int(from
, "max-bandwidth", 100000000); /* ~100Mb/s */
1313 /* To check remaining size after precopy */
1314 migrate_set_capability(from
, "pause-before-switchover", true);
1316 /* Wait for the first serial output from the source */
1317 wait_for_serial("src_serial");
1319 migrate(from
, uri
, "{}");
1321 /* Wait for throttling begins */
1323 while (percentage
== 0) {
1324 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1326 g_assert_false(got_stop
);
1328 /* The first percentage of throttling should be equal to init_pct */
1329 g_assert_cmpint(percentage
, ==, init_pct
);
1330 /* Now, when we tested that throttling works, let it converge */
1331 migrate_set_parameter_int(from
, "downtime-limit", downtime_limit
);
1332 migrate_set_parameter_int(from
, "max-bandwidth", max_bandwidth
);
1335 * Wait for pre-switchover status to check last throttle percentage
1336 * and remaining. These values will be zeroed later
1338 wait_for_migration_status(from
, "pre-switchover", NULL
);
1340 /* The final percentage of throttling shouldn't be greater than max_pct */
1341 percentage
= read_migrate_property_int(from
, "cpu-throttle-percentage");
1342 g_assert_cmpint(percentage
, <=, max_pct
);
1344 remaining
= read_ram_property_int(from
, "remaining");
1345 g_assert_cmpint(remaining
, <, expected_threshold
);
1347 migrate_continue(from
, "pre-switchover");
1349 qtest_qmp_eventwait(to
, "RESUME");
1351 wait_for_serial("dest_serial");
1352 wait_for_migration_complete(from
);
1356 test_migrate_end(from
, to
, true);
1359 int main(int argc
, char **argv
)
1361 char template[] = "/tmp/migration-test-XXXXXX";
1364 g_test_init(&argc
, &argv
, NULL
);
1366 if (!ufd_version_check()) {
1367 return g_test_run();
1371 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1372 * is touchy due to race conditions on dirty bits (especially on PPC for
1375 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1376 (access("/sys/module/kvm_hv", F_OK
) ||
1377 access("/dev/kvm", R_OK
| W_OK
))) {
1378 g_test_message("Skipping test: kvm_hv not available");
1379 return g_test_run();
1383 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1384 * there until the problems are resolved
1386 if (g_str_equal(qtest_get_arch(), "s390x")) {
1387 #if defined(HOST_S390X)
1388 if (access("/dev/kvm", R_OK
| W_OK
)) {
1389 g_test_message("Skipping test: kvm not available");
1390 return g_test_run();
1393 g_test_message("Skipping test: Need s390x host to work properly");
1394 return g_test_run();
1398 tmpfs
= mkdtemp(template);
1400 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1404 module_call_init(MODULE_INIT_QOM
);
1406 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1407 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1408 qtest_add_func("/migration/deprecated", test_deprecated
);
1409 qtest_add_func("/migration/bad_dest", test_baddest
);
1410 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1411 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1412 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1413 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1414 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1415 qtest_add_func("/migration/validate_uuid", test_validate_uuid
);
1416 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error
);
1417 qtest_add_func("/migration/validate_uuid_src_not_set",
1418 test_validate_uuid_src_not_set
);
1419 qtest_add_func("/migration/validate_uuid_dst_not_set",
1420 test_validate_uuid_dst_not_set
);
1422 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge
);
1426 g_assert_cmpint(ret
, ==, 0);
1430 g_test_message("unable to rmdir: path (%s): %s",
1431 tmpfs
, strerror(errno
));