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 uint64_t get_migration_pass(QTestState
*who
)
245 return read_ram_property_int(who
, "dirty-sync-count");
248 static void read_blocktime(QTestState
*who
)
252 rsp_return
= migrate_query(who
);
253 g_assert(qdict_haskey(rsp_return
, "postcopy-blocktime"));
254 qobject_unref(rsp_return
);
257 static void wait_for_migration_status(QTestState
*who
,
264 status
= migrate_query_status(who
);
265 completed
= strcmp(status
, goal
) == 0;
266 g_assert_cmpstr(status
, !=, "failed");
275 static void wait_for_migration_complete(QTestState
*who
)
277 wait_for_migration_status(who
, "completed");
280 static void wait_for_migration_pass(QTestState
*who
)
282 uint64_t initial_pass
= get_migration_pass(who
);
285 /* Wait for the 1st sync */
286 while (!got_stop
&& !initial_pass
) {
288 initial_pass
= get_migration_pass(who
);
293 pass
= get_migration_pass(who
);
294 } while (pass
== initial_pass
&& !got_stop
);
297 static void check_guests_ram(QTestState
*who
)
299 /* Our ASM test will have been incrementing one byte from each page from
300 * start_address to < end_address in order. This gives us a constraint
301 * that any page's byte should be equal or less than the previous pages
302 * byte (mod 256); and they should all be equal except for one transition
303 * at the point where we meet the incrementer. (We're running this with
304 * the guest stopped).
309 bool hit_edge
= false;
312 qtest_memread(who
, start_address
, &first_byte
, 1);
313 last_byte
= first_byte
;
315 for (address
= start_address
+ TEST_MEM_PAGE_SIZE
; address
< end_address
;
316 address
+= TEST_MEM_PAGE_SIZE
)
319 qtest_memread(who
, address
, &b
, 1);
320 if (b
!= last_byte
) {
321 if (((b
+ 1) % 256) == last_byte
&& !hit_edge
) {
322 /* This is OK, the guest stopped at the point of
323 * incrementing the previous page but didn't get
331 fprintf(stderr
, "Memory content inconsistency at %x"
332 " first_byte = %x last_byte = %x current = %x"
334 address
, first_byte
, last_byte
, b
, hit_edge
);
340 fprintf(stderr
, "and in another %d pages", bad
- 10);
345 static void cleanup(const char *filename
)
347 char *path
= g_strdup_printf("%s/%s", tmpfs
, filename
);
353 static char *get_shmem_opts(const char *mem_size
, const char *shmem_path
)
355 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
356 ",mem-path=%s,share=on -numa node,memdev=mem0",
357 mem_size
, shmem_path
);
360 static char *SocketAddress_to_str(SocketAddress
*addr
)
362 switch (addr
->type
) {
363 case SOCKET_ADDRESS_TYPE_INET
:
364 return g_strdup_printf("tcp:%s:%s",
367 case SOCKET_ADDRESS_TYPE_UNIX
:
368 return g_strdup_printf("unix:%s",
369 addr
->u
.q_unix
.path
);
370 case SOCKET_ADDRESS_TYPE_FD
:
371 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
372 case SOCKET_ADDRESS_TYPE_VSOCK
:
373 return g_strdup_printf("tcp:%s:%s",
377 return g_strdup("unknown address type");
381 static char *migrate_get_socket_address(QTestState
*who
, const char *parameter
)
385 Error
*local_err
= NULL
;
386 SocketAddressList
*addrs
;
390 rsp
= migrate_query(who
);
391 object
= qdict_get(rsp
, parameter
);
393 iv
= qobject_input_visitor_new(object
);
394 visit_type_SocketAddressList(iv
, NULL
, &addrs
, &local_err
);
397 /* we are only using a single address */
398 result
= SocketAddress_to_str(addrs
->value
);
400 qapi_free_SocketAddressList(addrs
);
405 static long long migrate_get_parameter_int(QTestState
*who
,
406 const char *parameter
)
411 rsp
= wait_command(who
, "{ 'execute': 'query-migrate-parameters' }");
412 result
= qdict_get_int(rsp
, parameter
);
417 static void migrate_check_parameter_int(QTestState
*who
, const char *parameter
,
422 result
= migrate_get_parameter_int(who
, parameter
);
423 g_assert_cmpint(result
, ==, value
);
426 static void migrate_set_parameter_int(QTestState
*who
, const char *parameter
,
432 "{ 'execute': 'migrate-set-parameters',"
433 "'arguments': { %s: %lld } }",
435 g_assert(qdict_haskey(rsp
, "return"));
437 migrate_check_parameter_int(who
, parameter
, value
);
440 static void migrate_pause(QTestState
*who
)
444 rsp
= wait_command(who
, "{ 'execute': 'migrate-pause' }");
448 static void migrate_recover(QTestState
*who
, const char *uri
)
452 rsp
= wait_command(who
,
453 "{ 'execute': 'migrate-recover', "
454 " 'id': 'recover-cmd', "
455 " 'arguments': { 'uri': %s } }",
460 static void migrate_set_capability(QTestState
*who
, const char *capability
,
466 "{ 'execute': 'migrate-set-capabilities',"
468 "'capabilities': [ { "
469 "'capability': %s, 'state': %i } ] } }",
471 g_assert(qdict_haskey(rsp
, "return"));
476 * Send QMP command "migrate".
477 * Arguments are built from @fmt... (formatted like
478 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
481 static void migrate(QTestState
*who
, const char *uri
, const char *fmt
, ...)
487 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
490 g_assert(!qdict_haskey(args
, "uri"));
491 qdict_put_str(args
, "uri", uri
);
493 rsp
= qmp("{ 'execute': 'migrate', 'arguments': %p}", args
);
495 g_assert(qdict_haskey(rsp
, "return"));
499 static void migrate_postcopy_start(QTestState
*from
, QTestState
*to
)
503 rsp
= wait_command(from
, "{ 'execute': 'migrate-start-postcopy' }");
507 qtest_qmp_eventwait(from
, "STOP");
510 qtest_qmp_eventwait(to
, "RESUME");
513 static int test_migrate_start(QTestState
**from
, QTestState
**to
,
514 const char *uri
, bool hide_stderr
,
517 gchar
*cmd_src
, *cmd_dst
;
518 char *bootpath
= NULL
;
519 char *extra_opts
= NULL
;
520 char *shmem_path
= NULL
;
521 const char *arch
= qtest_get_arch();
522 const char *accel
= "kvm:tcg";
525 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR
)) {
526 g_test_skip("/dev/shm is not supported");
529 shmem_path
= g_strdup_printf("/dev/shm/qemu-%d", getpid());
533 bootpath
= g_strdup_printf("%s/bootsect", tmpfs
);
534 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
535 /* the assembled x86 boot sector should be exactly one sector large */
536 assert(sizeof(x86_bootsect
) == 512);
537 init_bootfile(bootpath
, x86_bootsect
, sizeof(x86_bootsect
));
538 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
539 cmd_src
= g_strdup_printf("-machine accel=%s -m 150M"
540 " -name source,debug-threads=on"
541 " -serial file:%s/src_serial"
542 " -drive file=%s,format=raw %s",
543 accel
, tmpfs
, bootpath
,
544 extra_opts
? extra_opts
: "");
545 cmd_dst
= g_strdup_printf("-machine accel=%s -m 150M"
546 " -name target,debug-threads=on"
547 " -serial file:%s/dest_serial"
548 " -drive file=%s,format=raw"
550 accel
, tmpfs
, bootpath
, uri
,
551 extra_opts
? extra_opts
: "");
552 start_address
= X86_TEST_MEM_START
;
553 end_address
= X86_TEST_MEM_END
;
554 } else if (g_str_equal(arch
, "s390x")) {
555 init_bootfile(bootpath
, s390x_elf
, sizeof(s390x_elf
));
556 extra_opts
= use_shmem
? get_shmem_opts("128M", shmem_path
) : NULL
;
557 cmd_src
= g_strdup_printf("-machine accel=%s -m 128M"
558 " -name source,debug-threads=on"
559 " -serial file:%s/src_serial -bios %s %s",
560 accel
, tmpfs
, bootpath
,
561 extra_opts
? extra_opts
: "");
562 cmd_dst
= g_strdup_printf("-machine accel=%s -m 128M"
563 " -name target,debug-threads=on"
564 " -serial file:%s/dest_serial -bios %s"
566 accel
, tmpfs
, bootpath
, uri
,
567 extra_opts
? extra_opts
: "");
568 start_address
= S390_TEST_MEM_START
;
569 end_address
= S390_TEST_MEM_END
;
570 } else if (strcmp(arch
, "ppc64") == 0) {
571 extra_opts
= use_shmem
? get_shmem_opts("256M", shmem_path
) : NULL
;
572 cmd_src
= g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
573 " -name source,debug-threads=on"
574 " -serial file:%s/src_serial"
575 " -prom-env 'use-nvramrc?=true' -prom-env "
576 "'nvramrc=hex .\" _\" begin %x %x "
577 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
578 "until' %s", accel
, tmpfs
, end_address
,
579 start_address
, extra_opts
? extra_opts
: "");
580 cmd_dst
= g_strdup_printf("-machine accel=%s -m 256M"
581 " -name target,debug-threads=on"
582 " -serial file:%s/dest_serial"
585 extra_opts
? extra_opts
: "");
587 start_address
= PPC_TEST_MEM_START
;
588 end_address
= PPC_TEST_MEM_END
;
589 } else if (strcmp(arch
, "aarch64") == 0) {
590 init_bootfile(bootpath
, aarch64_kernel
, sizeof(aarch64_kernel
));
591 extra_opts
= use_shmem
? get_shmem_opts("150M", shmem_path
) : NULL
;
592 cmd_src
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
593 "-name vmsource,debug-threads=on -cpu max "
594 "-m 150M -serial file:%s/src_serial "
596 accel
, tmpfs
, bootpath
,
597 extra_opts
? extra_opts
: "");
598 cmd_dst
= g_strdup_printf("-machine virt,accel=%s,gic-version=max "
599 "-name vmdest,debug-threads=on -cpu max "
600 "-m 150M -serial file:%s/dest_serial "
603 accel
, tmpfs
, bootpath
, uri
,
604 extra_opts
? extra_opts
: "");
606 start_address
= ARM_TEST_MEM_START
;
607 end_address
= ARM_TEST_MEM_END
;
609 g_assert(sizeof(aarch64_kernel
) <= ARM_TEST_MAX_KERNEL_SIZE
);
611 g_assert_not_reached();
619 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_src
);
623 tmp
= g_strdup_printf("%s 2>/dev/null", cmd_dst
);
628 *from
= qtest_start(cmd_src
);
631 *to
= qtest_init(cmd_dst
);
635 * Remove shmem file immediately to avoid memory leak in test failed case.
636 * It's valid becase QEMU has already opened this file
646 static void test_migrate_end(QTestState
*from
, QTestState
*to
, bool test_dest
)
648 unsigned char dest_byte_a
, dest_byte_b
, dest_byte_c
, dest_byte_d
;
653 qtest_memread(to
, start_address
, &dest_byte_a
, 1);
655 /* Destination still running, wait for a byte to change */
657 qtest_memread(to
, start_address
, &dest_byte_b
, 1);
659 } while (dest_byte_a
== dest_byte_b
);
661 qtest_qmp_discard_response(to
, "{ 'execute' : 'stop'}");
663 /* With it stopped, check nothing changes */
664 qtest_memread(to
, start_address
, &dest_byte_c
, 1);
666 qtest_memread(to
, start_address
, &dest_byte_d
, 1);
667 g_assert_cmpint(dest_byte_c
, ==, dest_byte_d
);
669 check_guests_ram(to
);
675 cleanup("migsocket");
676 cleanup("src_serial");
677 cleanup("dest_serial");
680 static void deprecated_set_downtime(QTestState
*who
, const double value
)
685 "{ 'execute': 'migrate_set_downtime',"
686 " 'arguments': { 'value': %f } }", value
);
687 g_assert(qdict_haskey(rsp
, "return"));
689 migrate_check_parameter_int(who
, "downtime-limit", value
* 1000);
692 static void deprecated_set_speed(QTestState
*who
, long long value
)
696 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate_set_speed',"
697 "'arguments': { 'value': %lld } }", value
);
698 g_assert(qdict_haskey(rsp
, "return"));
700 migrate_check_parameter_int(who
, "max-bandwidth", value
);
703 static void deprecated_set_cache_size(QTestState
*who
, long long value
)
707 rsp
= qtest_qmp(who
, "{ 'execute': 'migrate-set-cache-size',"
708 "'arguments': { 'value': %lld } }", value
);
709 g_assert(qdict_haskey(rsp
, "return"));
711 migrate_check_parameter_int(who
, "xbzrle-cache-size", value
);
714 static void test_deprecated(void)
718 from
= qtest_start("-machine none");
720 deprecated_set_downtime(from
, 0.12345);
721 deprecated_set_speed(from
, 12345);
722 deprecated_set_cache_size(from
, 4096);
727 static int migrate_postcopy_prepare(QTestState
**from_ptr
,
731 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
732 QTestState
*from
, *to
;
734 if (test_migrate_start(&from
, &to
, uri
, hide_error
, false)) {
738 migrate_set_capability(from
, "postcopy-ram", true);
739 migrate_set_capability(to
, "postcopy-ram", true);
740 migrate_set_capability(to
, "postcopy-blocktime", true);
742 /* We want to pick a speed slow enough that the test completes
743 * quickly, but that it doesn't complete precopy even on a slow
744 * machine, so also set the downtime.
746 migrate_set_parameter_int(from
, "max-bandwidth", 100000000);
747 migrate_set_parameter_int(from
, "downtime-limit", 1);
749 /* Wait for the first serial output from the source */
750 wait_for_serial("src_serial");
752 migrate(from
, uri
, "{}");
755 wait_for_migration_pass(from
);
763 static void migrate_postcopy_complete(QTestState
*from
, QTestState
*to
)
765 wait_for_migration_complete(from
);
767 /* Make sure we get at least one "B" on destination */
768 wait_for_serial("dest_serial");
770 if (uffd_feature_thread_id
) {
774 test_migrate_end(from
, to
, true);
777 static void test_postcopy(void)
779 QTestState
*from
, *to
;
781 if (migrate_postcopy_prepare(&from
, &to
, false)) {
784 migrate_postcopy_start(from
, to
);
785 migrate_postcopy_complete(from
, to
);
788 static void test_postcopy_recovery(void)
790 QTestState
*from
, *to
;
793 if (migrate_postcopy_prepare(&from
, &to
, true)) {
797 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
798 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 4096);
800 /* Now we start the postcopy */
801 migrate_postcopy_start(from
, to
);
804 * Wait until postcopy is really started; we can only run the
805 * migrate-pause command during a postcopy
807 wait_for_migration_status(from
, "postcopy-active");
810 * Manually stop the postcopy migration. This emulates a network
811 * failure with the migration socket
816 * Wait for destination side to reach postcopy-paused state. The
817 * migrate-recover command can only succeed if destination machine
818 * is in the paused state
820 wait_for_migration_status(to
, "postcopy-paused");
823 * Create a new socket to emulate a new channel that is different
824 * from the broken migration channel; tell the destination to
825 * listen to the new port
827 uri
= g_strdup_printf("unix:%s/migsocket-recover", tmpfs
);
828 migrate_recover(to
, uri
);
831 * Try to rebuild the migration channel using the resume flag and
832 * the newly created channel
834 wait_for_migration_status(from
, "postcopy-paused");
835 migrate(from
, uri
, "{'resume': true}");
838 /* Restore the postcopy bandwidth to unlimited */
839 migrate_set_parameter_int(from
, "max-postcopy-bandwidth", 0);
841 migrate_postcopy_complete(from
, to
);
844 static void test_baddest(void)
846 QTestState
*from
, *to
;
851 if (test_migrate_start(&from
, &to
, "tcp:0:0", true, false)) {
854 migrate(from
, "tcp:0:0", "{}");
856 status
= migrate_query_status(from
);
857 g_assert(!strcmp(status
, "setup") || !(strcmp(status
, "failed")));
858 failed
= !strcmp(status
, "failed");
862 /* Is the machine currently running? */
863 rsp_return
= wait_command(from
, "{ 'execute': 'query-status' }");
864 g_assert(qdict_haskey(rsp_return
, "running"));
865 g_assert(qdict_get_bool(rsp_return
, "running"));
866 qobject_unref(rsp_return
);
868 test_migrate_end(from
, to
, false);
871 static void test_precopy_unix(void)
873 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
874 QTestState
*from
, *to
;
876 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
880 /* We want to pick a speed slow enough that the test completes
881 * quickly, but that it doesn't complete precopy even on a slow
882 * machine, so also set the downtime.
884 /* 1 ms should make it not converge*/
885 migrate_set_parameter_int(from
, "downtime-limit", 1);
887 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
889 /* Wait for the first serial output from the source */
890 wait_for_serial("src_serial");
892 migrate(from
, uri
, "{}");
894 wait_for_migration_pass(from
);
896 /* 300 ms should converge */
897 migrate_set_parameter_int(from
, "downtime-limit", 300);
900 qtest_qmp_eventwait(from
, "STOP");
903 qtest_qmp_eventwait(to
, "RESUME");
905 wait_for_serial("dest_serial");
906 wait_for_migration_complete(from
);
908 test_migrate_end(from
, to
, true);
913 /* Currently upset on aarch64 TCG */
914 static void test_ignore_shared(void)
916 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
917 QTestState
*from
, *to
;
919 if (test_migrate_start(&from
, &to
, uri
, false, true)) {
923 migrate_set_capability(from
, "x-ignore-shared", true);
924 migrate_set_capability(to
, "x-ignore-shared", true);
926 /* Wait for the first serial output from the source */
927 wait_for_serial("src_serial");
929 migrate(from
, uri
, "{}");
931 wait_for_migration_pass(from
);
934 qtest_qmp_eventwait(from
, "STOP");
937 qtest_qmp_eventwait(to
, "RESUME");
939 wait_for_serial("dest_serial");
940 wait_for_migration_complete(from
);
942 /* Check whether shared RAM has been really skipped */
943 g_assert_cmpint(read_ram_property_int(from
, "transferred"), <, 1024 * 1024);
945 test_migrate_end(from
, to
, true);
950 static void test_xbzrle(const char *uri
)
952 QTestState
*from
, *to
;
954 if (test_migrate_start(&from
, &to
, uri
, false, false)) {
959 * We want to pick a speed slow enough that the test completes
960 * quickly, but that it doesn't complete precopy even on a slow
961 * machine, so also set the downtime.
963 /* 1 ms should make it not converge*/
964 migrate_set_parameter_int(from
, "downtime-limit", 1);
966 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
968 migrate_set_parameter_int(from
, "xbzrle-cache-size", 33554432);
970 migrate_set_capability(from
, "xbzrle", "true");
971 migrate_set_capability(to
, "xbzrle", "true");
972 /* Wait for the first serial output from the source */
973 wait_for_serial("src_serial");
975 migrate(from
, uri
, "{}");
977 wait_for_migration_pass(from
);
979 /* 300ms should converge */
980 migrate_set_parameter_int(from
, "downtime-limit", 300);
983 qtest_qmp_eventwait(from
, "STOP");
985 qtest_qmp_eventwait(to
, "RESUME");
987 wait_for_serial("dest_serial");
988 wait_for_migration_complete(from
);
990 test_migrate_end(from
, to
, true);
993 static void test_xbzrle_unix(void)
995 char *uri
= g_strdup_printf("unix:%s/migsocket", tmpfs
);
1001 static void test_precopy_tcp(void)
1004 QTestState
*from
, *to
;
1006 if (test_migrate_start(&from
, &to
, "tcp:127.0.0.1:0", false, false)) {
1011 * We want to pick a speed slow enough that the test completes
1012 * quickly, but that it doesn't complete precopy even on a slow
1013 * machine, so also set the downtime.
1015 /* 1 ms should make it not converge*/
1016 migrate_set_parameter_int(from
, "downtime-limit", 1);
1018 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1020 /* Wait for the first serial output from the source */
1021 wait_for_serial("src_serial");
1023 uri
= migrate_get_socket_address(to
, "socket-address");
1025 migrate(from
, uri
, "{}");
1027 wait_for_migration_pass(from
);
1029 /* 300ms should converge */
1030 migrate_set_parameter_int(from
, "downtime-limit", 300);
1033 qtest_qmp_eventwait(from
, "STOP");
1035 qtest_qmp_eventwait(to
, "RESUME");
1037 wait_for_serial("dest_serial");
1038 wait_for_migration_complete(from
);
1040 test_migrate_end(from
, to
, true);
1044 static void test_migrate_fd_proto(void)
1046 QTestState
*from
, *to
;
1050 const char *error_desc
;
1052 if (test_migrate_start(&from
, &to
, "defer", false, false)) {
1057 * We want to pick a speed slow enough that the test completes
1058 * quickly, but that it doesn't complete precopy even on a slow
1059 * machine, so also set the downtime.
1061 /* 1 ms should make it not converge */
1062 migrate_set_parameter_int(from
, "downtime-limit", 1);
1064 migrate_set_parameter_int(from
, "max-bandwidth", 1000000000);
1066 /* Wait for the first serial output from the source */
1067 wait_for_serial("src_serial");
1069 /* Create two connected sockets for migration */
1070 ret
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
1071 g_assert_cmpint(ret
, ==, 0);
1073 /* Send the 1st socket to the target */
1074 rsp
= wait_command_fd(to
, pair
[0],
1075 "{ 'execute': 'getfd',"
1076 " 'arguments': { 'fdname': 'fd-mig' }}");
1080 /* Start incoming migration from the 1st socket */
1081 rsp
= wait_command(to
, "{ 'execute': 'migrate-incoming',"
1082 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1085 /* Send the 2nd socket to the target */
1086 rsp
= wait_command_fd(from
, pair
[1],
1087 "{ 'execute': 'getfd',"
1088 " 'arguments': { 'fdname': 'fd-mig' }}");
1092 /* Start migration to the 2nd socket*/
1093 migrate(from
, "fd:fd-mig", "{}");
1095 wait_for_migration_pass(from
);
1097 /* 300ms should converge */
1098 migrate_set_parameter_int(from
, "downtime-limit", 300);
1101 qtest_qmp_eventwait(from
, "STOP");
1103 qtest_qmp_eventwait(to
, "RESUME");
1105 /* Test closing fds */
1106 /* We assume, that QEMU removes named fd from its list,
1107 * so this should fail */
1108 rsp
= qtest_qmp(from
, "{ 'execute': 'closefd',"
1109 " 'arguments': { 'fdname': 'fd-mig' }}");
1110 g_assert_true(qdict_haskey(rsp
, "error"));
1111 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1112 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1115 rsp
= qtest_qmp(to
, "{ 'execute': 'closefd',"
1116 " 'arguments': { 'fdname': 'fd-mig' }}");
1117 g_assert_true(qdict_haskey(rsp
, "error"));
1118 error_desc
= qdict_get_str(qdict_get_qdict(rsp
, "error"), "desc");
1119 g_assert_cmpstr(error_desc
, ==, "File descriptor named 'fd-mig' not found");
1122 /* Complete migration */
1123 wait_for_serial("dest_serial");
1124 wait_for_migration_complete(from
);
1125 test_migrate_end(from
, to
, true);
1128 int main(int argc
, char **argv
)
1130 char template[] = "/tmp/migration-test-XXXXXX";
1133 g_test_init(&argc
, &argv
, NULL
);
1135 if (!ufd_version_check()) {
1136 return g_test_run();
1140 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1141 * is touchy due to race conditions on dirty bits (especially on PPC for
1144 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1145 access("/sys/module/kvm_hv", F_OK
)) {
1146 g_test_message("Skipping test: kvm_hv not available");
1147 return g_test_run();
1151 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1152 * there until the problems are resolved
1154 if (g_str_equal(qtest_get_arch(), "s390x")) {
1155 #if defined(HOST_S390X)
1156 if (access("/dev/kvm", R_OK
| W_OK
)) {
1157 g_test_message("Skipping test: kvm not available");
1158 return g_test_run();
1161 g_test_message("Skipping test: Need s390x host to work properly");
1162 return g_test_run();
1166 tmpfs
= mkdtemp(template);
1168 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno
));
1172 module_call_init(MODULE_INIT_QOM
);
1174 qtest_add_func("/migration/postcopy/unix", test_postcopy
);
1175 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery
);
1176 qtest_add_func("/migration/deprecated", test_deprecated
);
1177 qtest_add_func("/migration/bad_dest", test_baddest
);
1178 qtest_add_func("/migration/precopy/unix", test_precopy_unix
);
1179 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp
);
1180 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1181 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix
);
1182 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto
);
1186 g_assert_cmpint(ret
, ==, 0);
1190 g_test_message("unable to rmdir: path (%s): %s",
1191 tmpfs
, strerror(errno
));