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/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "sysemu/sysemu.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"
94 static void init_bootfile(const char *bootpath
, void *content
)
96 FILE *bootfile
= fopen(bootpath
, "wb");
98 g_assert_cmpint(fwrite(content
, 512, 1, bootfile
), ==, 1);
102 #include "tests/migration/s390x/a-b-bios.h"
104 static void init_bootfile_s390x(const char *bootpath
)
106 FILE *bootfile
= fopen(bootpath
, "wb");
107 size_t len
= sizeof(s390x_elf
);
109 g_assert_cmpint(fwrite(s390x_elf
, len
, 1, bootfile
), ==, 1);
114 * Wait for some output in the serial output file,
115 * we get an 'A' followed by an endless string of 'B's
116 * but on the destination we won't have the A.
118 static void wait_for_serial(const char *side
)
120 char *serialpath
= g_strdup_printf("%s/%s", tmpfs
, side
);
121 FILE *serialfile
= fopen(serialpath
, "r");
122 const char *arch
= qtest_get_arch();
123 int started
= (strcmp(side
, "src_serial") == 0 &&
124 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
128 int readvalue
= fgetc(serialfile
);
131 /* SLOF prints its banner before starting test,
132 * to ignore it, mark the start of the test with '_',
133 * ignore all characters until this marker
140 fseek(serialfile
, 0, SEEK_SET
);
157 started
= (strcmp(side
, "src_serial") == 0 &&
158 strcmp(arch
, "ppc64") == 0) ? 0 : 1;
159 fseek(serialfile
, 0, SEEK_SET
);
164 fprintf(stderr
, "Unexpected %d on %s serial\n", readvalue
, side
);
165 g_assert_not_reached();
170 static void stop_cb(void *opaque
, const char *name
, QDict
*data
)
172 if (!strcmp(name
, "STOP")) {
178 * Events can get in the way of responses we are actually waiting for.
181 static QDict
*wait_command_fd(QTestState
*who
, int fd
, const char *command
, ...)
185 va_start(ap
, command
);
186 qtest_qmp_vsend_fds(who
, &fd
, 1, command
, ap
);
189 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
193 * Events can get in the way of responses we are actually waiting for.
196 static QDict
*wait_command(QTestState
*who
, const char *command
, ...)
200 va_start(ap
, command
);
201 qtest_qmp_vsend(who
, command
, ap
);
204 return qtest_qmp_receive_success(who
, stop_cb
, NULL
);
208 * Note: caller is responsible to free the returned object via
209 * qobject_unref() after use
211 static QDict
*migrate_query(QTestState
*who
)
213 return wait_command(who
, "{ 'execute': 'query-migrate' }");
217 * Note: caller is responsible to free the returned object via
220 static gchar
*migrate_query_status(QTestState
*who
)
222 QDict
*rsp_return
= migrate_query(who
);
223 gchar
*status
= g_strdup(qdict_get_str(rsp_return
, "status"));
226 qobject_unref(rsp_return
);
232 * It's tricky to use qemu's migration event capability with qtest,
233 * events suddenly appearing confuse the qmp()/hmp() responses.
236 static int64_t read_ram_property_int(QTestState
*who
, const char *property
)
238 QDict
*rsp_return
, *rsp_ram
;
241 rsp_return
= migrate_query(who
);
242 if (!qdict_haskey(rsp_return
, "ram")) {
246 rsp_ram
= qdict_get_qdict(rsp_return
, "ram");
247 result
= qdict_get_try_int(rsp_ram
, property
, 0);
249 qobject_unref(rsp_return
);
253 static uint64_t get_migration_pass(QTestState
*who
)
255 return read_ram_property_int(who
, "dirty-sync-count");
258 static void read_blocktime(QTestState
*who
)
262 rsp_return
= migrate_query(who
);
263 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
264 qobject_unref(rsp_return
);
267 static void wait_for_migration_status(QTestState
*who
,
274 status
= migrate_query_status(who
);
275 completed
= strcmp(status
, goal
) == 0;
276 g_assert_cmpstr(status
, !=, "failed");
285 static void wait_for_migration_complete(QTestState
*who
)
287 wait_for_migration_status(who
, "completed");
290 static void wait_for_migration_pass(QTestState
*who
)
292 uint64_t initial_pass
= get_migration_pass(who
);
295 /* Wait for the 1st sync */
296 while (!got_stop
&& !initial_pass
) {
298 initial_pass
= get_migration_pass(who
);
303 pass
= get_migration_pass(who
);
304 } while (pass
== initial_pass
&& !got_stop
);
307 static void check_guests_ram(QTestState
*who
)
309 /* Our ASM test will have been incrementing one byte from each page from
310 * start_address to < end_address in order. This gives us a constraint
311 * that any page's byte should be equal or less than the previous pages
312 * byte (mod 256); and they should all be equal except for one transition
313 * at the point where we meet the incrementer. (We're running this with
314 * the guest stopped).
319 bool hit_edge
= false;
322 qtest_memread(who
, start_address
, &first_byte
, 1);
323 last_byte
= first_byte
;
325 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
326 address
+= TEST_MEM_PAGE_SIZE
)
329 qtest_memread(who
, address
, &b
, 1);
330 if (b
!= last_byte
) {
331 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
332 /* This is OK, the guest stopped at the point of
333 * incrementing the previous page but didn't get
339 fprintf(stderr
, "Memory content inconsistency at %x"
340 " first_byte = %x last_byte = %x current = %x"
342 address
, first_byte
, last_byte
, b
, hit_edge
);
350 static void cleanup(const char *filename
)
352 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
358 static char *get_shmem_opts(const char *mem_size
, const char *shmem_path
)
360 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
361 ",mem-path=%s,share=on -numa node,memdev=mem0",
362 mem_size
, shmem_path
);
365 static char *SocketAddress_to_str(SocketAddress
*addr
)
367 switch (addr
->type
) {
368 case SOCKET_ADDRESS_TYPE_INET
:
369 return g_strdup_printf("tcp:%s:%s",
372 case SOCKET_ADDRESS_TYPE_UNIX
:
373 return g_strdup_printf("unix:%s",
374 addr
->u
.q_unix
.path
);
375 case SOCKET_ADDRESS_TYPE_FD
:
376 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
377 case SOCKET_ADDRESS_TYPE_VSOCK
:
378 return g_strdup_printf("tcp:%s:%s",
382 return g_strdup("unknown address type");
386 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
390 Error
*local_err
= NULL
;
391 SocketAddressList
*addrs
;
395 rsp
= migrate_query(who
);
396 object
= qdict_get(rsp
, parameter
);
398 iv
= qobject_input_visitor_new(object
);
399 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &local_err
);
402 /* we are only using a single address */
403 result
= SocketAddress_to_str(addrs
->value
);
405 qapi_free_SocketAddressList(addrs
);
410 static long long migrate_get_parameter(QTestState
*who
, const char *parameter
)
415 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
416 result
= qdict_get_int(rsp
, parameter
);
421 static void migrate_check_parameter(QTestState
*who
, const char *parameter
,
426 result
= migrate_get_parameter(who
, parameter
);
427 g_assert_cmpint(result
, ==, value
);
430 static void migrate_set_parameter(QTestState
*who
, const char *parameter
,
436 "{ 'execute': 'migrate-set-parameters',"
437 "'arguments': { %s: %lld } }",
439 g_assert(qdict_haskey(rsp
, "return"));
441 migrate_check_parameter(who
, parameter
, value
);
444 static void migrate_pause(QTestState
*who
)
448 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
452 static void migrate_recover(QTestState
*who
, const char *uri
)
456 rsp
= wait_command(who
,
457 "{ 'execute': 'migrate-recover', "
458 " 'id': 'recover-cmd', "
459 " 'arguments': { 'uri': %s } }",
464 static void migrate_set_capability(QTestState
*who
, const char *capability
,
470 "{ 'execute': 'migrate-set-capabilities',"
472 "'capabilities': [ { "
473 "'capability': %s, 'state': %i } ] } }",
475 g_assert(qdict_haskey(rsp
, "return"));
480 * Send QMP command "migrate".
481 * Arguments are built from @fmt... (formatted like
482 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
485 static void migrate(QTestState
*who
, const char *uri
, const char *fmt
, ...)
491 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
494 g_assert(!qdict_haskey(args
, "uri"));
495 qdict_put_str(args
, "uri", uri
);
497 rsp
= qmp("{ 'execute': 'migrate', 'arguments': %p}", args
);
499 g_assert(qdict_haskey(rsp
, "return"));
503 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
507 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
511 qtest_qmp_eventwait(from
, "STOP");
514 qtest_qmp_eventwait(to
, "RESUME");
517 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
518 const char *uri
, bool hide_stderr
,
521 gchar
*cmd_src
, *cmd_dst
;
522 char *bootpath
= NULL
;
523 char *extra_opts
= NULL
;
524 char *shmem_path
= NULL
;
525 const char *arch
= qtest_get_arch();
526 const char *accel
= "kvm:tcg";
529 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
530 g_test_skip("/dev/shm is not supported");
533 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
537 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
538 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
539 init_bootfile(bootpath
, x86_bootsect
);
540 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
541 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
542 " -name source,debug-threads=on"
543 " -serial file:%s/src_serial"
544 " -drive file=%s,format=raw %s",
545 accel
, tmpfs
, bootpath
,
546 extra_opts
? extra_opts
: "");
547 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
548 " -name target,debug-threads=on"
549 " -serial file:%s/dest_serial"
550 " -drive file=%s,format=raw"
552 accel
, tmpfs
, bootpath
, uri
,
553 extra_opts
? extra_opts
: "");
554 start_address
= X86_TEST_MEM_START
;
555 end_address
= X86_TEST_MEM_END
;
556 } else if (g_str_equal(arch
, "s390x")) {
557 init_bootfile_s390x(bootpath
);
558 extra_opts
= use_shmem
? get_shmem_opts("128M", shmem_path
) : NULL
;
559 cmd_src
= g_strdup_printf("-machine accel=%s -m 128M"
560 " -name source,debug-threads=on"
561 " -serial file:%s/src_serial -bios %s %s",
562 accel
, tmpfs
, bootpath
,
563 extra_opts
? extra_opts
: "");
564 cmd_dst
= g_strdup_printf("-machine accel=%s -m 128M"
565 " -name target,debug-threads=on"
566 " -serial file:%s/dest_serial -bios %s"
568 accel
, tmpfs
, bootpath
, uri
,
569 extra_opts
? extra_opts
: "");
570 start_address
= S390_TEST_MEM_START
;
571 end_address
= S390_TEST_MEM_END
;
572 } else if (strcmp(arch
, "ppc64") == 0) {
573 extra_opts
= use_shmem
? get_shmem_opts("256M", shmem_path
) : NULL
;
574 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
575 " -name source,debug-threads=on"
576 " -serial file:%s/src_serial"
577 " -prom-env 'use-nvramrc?=true' -prom-env "
578 "'nvramrc=hex .\" _\" begin %x %x "
579 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
580 "until' %s", accel
, tmpfs
, end_address
,
581 start_address
, extra_opts
? extra_opts
: "");
582 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
583 " -name target,debug-threads=on"
584 " -serial file:%s/dest_serial"
587 extra_opts
? extra_opts
: "");
589 start_address
= PPC_TEST_MEM_START
;
590 end_address
= PPC_TEST_MEM_END
;
591 } else if (strcmp(arch
, "aarch64") == 0) {
592 init_bootfile(bootpath
, aarch64_kernel
);
593 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
594 cmd_src
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
595 "-name vmsource,debug-threads=on -cpu max "
596 "-m 150M -serial file:%s/src_serial "
598 accel
, tmpfs
, bootpath
,
599 extra_opts
? extra_opts
: "");
600 cmd_dst
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
601 "-name vmdest,debug-threads=on -cpu max "
602 "-m 150M -serial file:%s/dest_serial "
605 accel
, tmpfs
, bootpath
, uri
,
606 extra_opts
? extra_opts
: "");
608 start_address
= ARM_TEST_MEM_START
;
609 end_address
= ARM_TEST_MEM_END
;
611 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
613 g_assert_not_reached();
621 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
625 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
630 *from
= qtest_start(cmd_src
);
633 *to
= qtest_init(cmd_dst
);
637 * Remove shmem file immediately to avoid memory leak in test failed case.
638 * It's valid becase QEMU has already opened this file
648 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
650 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
655 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
657 /* Destination still running, wait for a byte to change */
659 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
661 } while (dest_byte_a
== dest_byte_b
);
663 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
665 /* With it stopped, check nothing changes */
666 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
668 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
669 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
671 check_guests_ram(to
);
677 cleanup("migsocket");
678 cleanup("src_serial");
679 cleanup("dest_serial");
682 static void deprecated_set_downtime(QTestState
*who
, const double value
)
687 "{ 'execute': 'migrate_set_downtime',"
688 " 'arguments': { 'value': %f } }", value
);
689 g_assert(qdict_haskey(rsp
, "return"));
691 migrate_check_parameter(who
, "downtime-limit", value
* 1000);
694 static void deprecated_set_speed(QTestState
*who
, long long value
)
698 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
699 "'arguments': { 'value': %lld } }", value
);
700 g_assert(qdict_haskey(rsp
, "return"));
702 migrate_check_parameter(who
, "max-bandwidth", value
);
705 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
709 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
710 "'arguments': { 'value': %lld } }", value
);
711 g_assert(qdict_haskey(rsp
, "return"));
713 migrate_check_parameter(who
, "xbzrle-cache-size", value
);
716 static void test_deprecated(void)
720 from
= qtest_start("-machine none");
722 deprecated_set_downtime(from
, 0.12345);
723 deprecated_set_speed(from
, 12345);
724 deprecated_set_cache_size(from
, 4096);
729 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
733 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
734 QTestState
*from
, *to
;
736 if (test_migrate_start(&from
, &to
, uri
, hide_error
, false)) {
740 migrate_set_capability(from
, "postcopy-ram", true);
741 migrate_set_capability(to
, "postcopy-ram", true);
742 migrate_set_capability(to
, "postcopy-blocktime", true);
744 /* We want to pick a speed slow enough that the test completes
745 * quickly, but that it doesn't complete precopy even on a slow
746 * machine, so also set the downtime.
748 migrate_set_parameter(from
, "max-bandwidth", 100000000);
749 migrate_set_parameter(from
, "downtime-limit", 1);
751 /* Wait for the first serial output from the source */
752 wait_for_serial("src_serial");
754 migrate(from
, uri
, "{}");
757 wait_for_migration_pass(from
);
765 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
767 wait_for_migration_complete(from
);
769 /* Make sure we get at least one "B" on destination */
770 wait_for_serial("dest_serial");
772 if (uffd_feature_thread_id
) {
776 test_migrate_end(from
, to
, true);
779 static void test_postcopy(void)
781 QTestState
*from
, *to
;
783 if (migrate_postcopy_prepare(&from
, &to
, false)) {
786 migrate_postcopy_start(from
, to
);
787 migrate_postcopy_complete(from
, to
);
790 static void test_postcopy_recovery(void)
792 QTestState
*from
, *to
;
795 if (migrate_postcopy_prepare(&from
, &to
, true)) {
799 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
800 migrate_set_parameter(from
, "max-postcopy-bandwidth", 4096);
802 /* Now we start the postcopy */
803 migrate_postcopy_start(from
, to
);
806 * Wait until postcopy is really started; we can only run the
807 * migrate-pause command during a postcopy
809 wait_for_migration_status(from
, "postcopy-active");
812 * Manually stop the postcopy migration. This emulates a network
813 * failure with the migration socket
818 * Wait for destination side to reach postcopy-paused state. The
819 * migrate-recover command can only succeed if destination machine
820 * is in the paused state
822 wait_for_migration_status(to
, "postcopy-paused");
825 * Create a new socket to emulate a new channel that is different
826 * from the broken migration channel; tell the destination to
827 * listen to the new port
829 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
830 migrate_recover(to
, uri
);
833 * Try to rebuild the migration channel using the resume flag and
834 * the newly created channel
836 wait_for_migration_status(from
, "postcopy-paused");
837 migrate(from
, uri
, "{'resume': true}");
840 /* Restore the postcopy bandwidth to unlimited */
841 migrate_set_parameter(from
, "max-postcopy-bandwidth", 0);
843 migrate_postcopy_complete(from
, to
);
846 static void test_baddest(void)
848 QTestState
*from
, *to
;
853 if (test_migrate_start(&from
, &to
, "tcp:0:0", true, false)) {
856 migrate(from
, "tcp:0:0", "{}");
858 status
= migrate_query_status(from
);
859 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
860 failed
= !strcmp(status
, "failed");
864 /* Is the machine currently running? */
865 rsp_return
= wait_command(from
, "{ 'execute': 'query-status' }");
866 g_assert(qdict_haskey(rsp_return
, "running"));
867 g_assert(qdict_get_bool(rsp_return
, "running"));
868 qobject_unref(rsp_return
);
870 test_migrate_end(from
, to
, false);
873 static void test_precopy_unix(void)
875 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
876 QTestState
*from
, *to
;
878 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
882 /* We want to pick a speed slow enough that the test completes
883 * quickly, but that it doesn't complete precopy even on a slow
884 * machine, so also set the downtime.
886 /* 1 ms should make it not converge*/
887 migrate_set_parameter(from
, "downtime-limit", 1);
889 migrate_set_parameter(from
, "max-bandwidth", 1000000000);
891 /* Wait for the first serial output from the source */
892 wait_for_serial("src_serial");
894 migrate(from
, uri
, "{}");
896 wait_for_migration_pass(from
);
898 /* 300 ms should converge */
899 migrate_set_parameter(from
, "downtime-limit", 300);
902 qtest_qmp_eventwait(from
, "STOP");
905 qtest_qmp_eventwait(to
, "RESUME");
907 wait_for_serial("dest_serial");
908 wait_for_migration_complete(from
);
910 test_migrate_end(from
, to
, true);
915 /* Currently upset on aarch64 TCG */
916 static void test_ignore_shared(void)
918 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
919 QTestState
*from
, *to
;
921 if (test_migrate_start(&from
, &to
, uri
, false, true)) {
925 migrate_set_capability(from
, "x-ignore-shared", true);
926 migrate_set_capability(to
, "x-ignore-shared", true);
928 /* Wait for the first serial output from the source */
929 wait_for_serial("src_serial");
931 migrate(from
, uri
, "{}");
933 wait_for_migration_pass(from
);
936 qtest_qmp_eventwait(from
, "STOP");
939 qtest_qmp_eventwait(to
, "RESUME");
941 wait_for_serial("dest_serial");
942 wait_for_migration_complete(from
);
944 /* Check whether shared RAM has been really skipped */
945 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
947 test_migrate_end(from
, to
, true);
952 static void test_xbzrle(const char *uri
)
954 QTestState
*from
, *to
;
956 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
961 * We want to pick a speed slow enough that the test completes
962 * quickly, but that it doesn't complete precopy even on a slow
963 * machine, so also set the downtime.
965 /* 1 ms should make it not converge*/
966 migrate_set_parameter(from
, "downtime-limit", 1);
968 migrate_set_parameter(from
, "max-bandwidth", 1000000000);
970 migrate_set_parameter(from
, "xbzrle-cache-size", 33554432);
972 migrate_set_capability(from
, "xbzrle", "true");
973 migrate_set_capability(to
, "xbzrle", "true");
974 /* Wait for the first serial output from the source */
975 wait_for_serial("src_serial");
977 migrate(from
, uri
, "{}");
979 wait_for_migration_pass(from
);
981 /* 300ms should converge */
982 migrate_set_parameter(from
, "downtime-limit", 300);
985 qtest_qmp_eventwait(from
, "STOP");
987 qtest_qmp_eventwait(to
, "RESUME");
989 wait_for_serial("dest_serial");
990 wait_for_migration_complete(from
);
992 test_migrate_end(from
, to
, true);
995 static void test_xbzrle_unix(void)
997 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1003 static void test_precopy_tcp(void)
1006 QTestState
*from
, *to
;
1008 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", false, false)) {
1013 * We want to pick a speed slow enough that the test completes
1014 * quickly, but that it doesn't complete precopy even on a slow
1015 * machine, so also set the downtime.
1017 /* 1 ms should make it not converge*/
1018 migrate_set_parameter(from
, "downtime-limit", 1);
1020 migrate_set_parameter(from
, "max-bandwidth", 1000000000);
1022 /* Wait for the first serial output from the source */
1023 wait_for_serial("src_serial");
1025 uri
= migrate_get_socket_address(to
, "socket-address");
1027 migrate(from
, uri
, "{}");
1029 wait_for_migration_pass(from
);
1031 /* 300ms should converge */
1032 migrate_set_parameter(from
, "downtime-limit", 300);
1035 qtest_qmp_eventwait(from
, "STOP");
1037 qtest_qmp_eventwait(to
, "RESUME");
1039 wait_for_serial("dest_serial");
1040 wait_for_migration_complete(from
);
1042 test_migrate_end(from
, to
, true);
1046 static void test_migrate_fd_proto(void)
1048 QTestState
*from
, *to
;
1052 const char *error_desc
;
1054 if (test_migrate_start(&from
, &to
, "defer", false, false)) {
1059 * We want to pick a speed slow enough that the test completes
1060 * quickly, but that it doesn't complete precopy even on a slow
1061 * machine, so also set the downtime.
1063 /* 1 ms should make it not converge */
1064 migrate_set_parameter(from
, "downtime-limit", 1);
1066 migrate_set_parameter(from
, "max-bandwidth", 1000000000);
1068 /* Wait for the first serial output from the source */
1069 wait_for_serial("src_serial");
1071 /* Create two connected sockets for migration */
1072 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1073 g_assert_cmpint(ret
, ==, 0);
1075 /* Send the 1st socket to the target */
1076 rsp
= wait_command_fd(to
, pair
[0],
1077 "{ 'execute': 'getfd',"
1078 " 'arguments': { 'fdname': 'fd-mig' }}");
1082 /* Start incoming migration from the 1st socket */
1083 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1084 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1087 /* Send the 2nd socket to the target */
1088 rsp
= wait_command_fd(from
, pair
[1],
1089 "{ 'execute': 'getfd',"
1090 " 'arguments': { 'fdname': 'fd-mig' }}");
1094 /* Start migration to the 2nd socket*/
1095 migrate(from
, "fd:fd-mig", "{}");
1097 wait_for_migration_pass(from
);
1099 /* 300ms should converge */
1100 migrate_set_parameter(from
, "downtime-limit", 300);
1103 qtest_qmp_eventwait(from
, "STOP");
1105 qtest_qmp_eventwait(to
, "RESUME");
1107 /* Test closing fds */
1108 /* We assume, that QEMU removes named fd from its list,
1109 * so this should fail */
1110 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1111 " 'arguments': { 'fdname': 'fd-mig' }}");
1112 g_assert_true(qdict_haskey(rsp
, "error"));
1113 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1114 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1117 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1118 " 'arguments': { 'fdname': 'fd-mig' }}");
1119 g_assert_true(qdict_haskey(rsp
, "error"));
1120 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1121 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1124 /* Complete migration */
1125 wait_for_serial("dest_serial");
1126 wait_for_migration_complete(from
);
1127 test_migrate_end(from
, to
, true);
1130 int main(int argc
, char **argv
)
1132 char template[] = "/tmp/migration-test-XXXXXX";
1135 g_test_init(&argc
, &argv
, NULL
);
1137 if (!ufd_version_check()) {
1138 return g_test_run();
1142 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1143 * is touchy due to race conditions on dirty bits (especially on PPC for
1146 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1147 access("/sys/module/kvm_hv", F_OK
)) {
1148 g_test_message("Skipping test: kvm_hv not available");
1149 return g_test_run();
1153 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1154 * there until the problems are resolved
1156 if (g_str_equal(qtest_get_arch(), "s390x")) {
1157 #if defined(HOST_S390X)
1158 if (access("/dev/kvm", R_OK
| W_OK
)) {
1159 g_test_message("Skipping test: kvm not available");
1160 return g_test_run();
1163 g_test_message("Skipping test: Need s390x host to work properly");
1164 return g_test_run();
1168 tmpfs
= mkdtemp(template);
1170 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1174 module_call_init(MODULE_INIT_QOM
);
1176 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1177 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1178 qtest_add_func("/migration/deprecated", test_deprecated
);
1179 qtest_add_func("/migration/bad_dest", test_baddest
);
1180 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1181 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1182 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1183 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1184 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1188 g_assert_cmpint(ret
, ==, 0);
1192 g_test_message("unable to rmdir: path (%s): %s",
1193 tmpfs
, strerror(errno
));