linux-user/s390x: Fix trace in restore_regs
[qemu/ar7.git] / tests / qtest / migration-test.c
blob2b028df687512b7ddbf3064649703e65524001c4
1 /*
2 * QTest testcase for migration
4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5 * based on the vhost-user-test.c that is:
6 * Copyright (c) 2014 Virtual Open Systems Sarl.
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "libqos/libqtest.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
27 #include "migration-helpers.h"
28 #include "tests/migration/migration-test.h"
30 /* TODO actually test the results and get rid of this */
31 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
33 unsigned start_address;
34 unsigned end_address;
35 static bool uffd_feature_thread_id;
37 /* A downtime where the test really should converge */
38 #define CONVERGE_DOWNTIME 1000
40 #if defined(__linux__)
41 #include <sys/syscall.h>
42 #include <sys/vfs.h>
43 #endif
45 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
46 #include <sys/eventfd.h>
47 #include <sys/ioctl.h>
48 #include <linux/userfaultfd.h>
50 static bool ufd_version_check(void)
52 struct uffdio_api api_struct;
53 uint64_t ioctl_mask;
55 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
57 if (ufd == -1) {
58 g_test_message("Skipping test: userfaultfd not available");
59 return false;
62 api_struct.api = UFFD_API;
63 api_struct.features = 0;
64 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
65 g_test_message("Skipping test: UFFDIO_API failed");
66 return false;
68 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
70 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
71 (__u64)1 << _UFFDIO_UNREGISTER;
72 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
73 g_test_message("Skipping test: Missing userfault feature");
74 return false;
77 return true;
80 #else
81 static bool ufd_version_check(void)
83 g_test_message("Skipping test: Userfault not available (builtdtime)");
84 return false;
87 #endif
89 static const char *tmpfs;
91 /* The boot file modifies memory area in [start_address, end_address)
92 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
94 #include "tests/migration/i386/a-b-bootblock.h"
95 #include "tests/migration/aarch64/a-b-kernel.h"
96 #include "tests/migration/s390x/a-b-bios.h"
98 static void init_bootfile(const char *bootpath, void *content, size_t len)
100 FILE *bootfile = fopen(bootpath, "wb");
102 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
103 fclose(bootfile);
107 * Wait for some output in the serial output file,
108 * we get an 'A' followed by an endless string of 'B's
109 * but on the destination we won't have the A.
111 static void wait_for_serial(const char *side)
113 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
114 FILE *serialfile = fopen(serialpath, "r");
115 const char *arch = qtest_get_arch();
116 int started = (strcmp(side, "src_serial") == 0 &&
117 strcmp(arch, "ppc64") == 0) ? 0 : 1;
119 do {
120 int readvalue = fgetc(serialfile);
122 if (!started) {
123 /* SLOF prints its banner before starting test,
124 * to ignore it, mark the start of the test with '_',
125 * ignore all characters until this marker
127 switch (readvalue) {
128 case '_':
129 started = 1;
130 break;
131 case EOF:
132 fseek(serialfile, 0, SEEK_SET);
133 usleep(1000);
134 break;
136 continue;
138 switch (readvalue) {
139 case 'A':
140 /* Fine */
141 break;
143 case 'B':
144 /* It's alive! */
145 fclose(serialfile);
146 return;
148 case EOF:
149 started = (strcmp(side, "src_serial") == 0 &&
150 strcmp(arch, "ppc64") == 0) ? 0 : 1;
151 fseek(serialfile, 0, SEEK_SET);
152 usleep(1000);
153 break;
155 default:
156 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
157 g_assert_not_reached();
159 } while (true);
163 * It's tricky to use qemu's migration event capability with qtest,
164 * events suddenly appearing confuse the qmp()/hmp() responses.
167 static int64_t read_ram_property_int(QTestState *who, const char *property)
169 QDict *rsp_return, *rsp_ram;
170 int64_t result;
172 rsp_return = migrate_query(who);
173 if (!qdict_haskey(rsp_return, "ram")) {
174 /* Still in setup */
175 result = 0;
176 } else {
177 rsp_ram = qdict_get_qdict(rsp_return, "ram");
178 result = qdict_get_try_int(rsp_ram, property, 0);
180 qobject_unref(rsp_return);
181 return result;
184 static int64_t read_migrate_property_int(QTestState *who, const char *property)
186 QDict *rsp_return;
187 int64_t result;
189 rsp_return = migrate_query(who);
190 result = qdict_get_try_int(rsp_return, property, 0);
191 qobject_unref(rsp_return);
192 return result;
195 static uint64_t get_migration_pass(QTestState *who)
197 return read_ram_property_int(who, "dirty-sync-count");
200 static void read_blocktime(QTestState *who)
202 QDict *rsp_return;
204 rsp_return = migrate_query(who);
205 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
206 qobject_unref(rsp_return);
209 static void wait_for_migration_pass(QTestState *who)
211 uint64_t initial_pass = get_migration_pass(who);
212 uint64_t pass;
214 /* Wait for the 1st sync */
215 while (!got_stop && !initial_pass) {
216 usleep(1000);
217 initial_pass = get_migration_pass(who);
220 do {
221 usleep(1000);
222 pass = get_migration_pass(who);
223 } while (pass == initial_pass && !got_stop);
226 static void check_guests_ram(QTestState *who)
228 /* Our ASM test will have been incrementing one byte from each page from
229 * start_address to < end_address in order. This gives us a constraint
230 * that any page's byte should be equal or less than the previous pages
231 * byte (mod 256); and they should all be equal except for one transition
232 * at the point where we meet the incrementer. (We're running this with
233 * the guest stopped).
235 unsigned address;
236 uint8_t first_byte;
237 uint8_t last_byte;
238 bool hit_edge = false;
239 int bad = 0;
241 qtest_memread(who, start_address, &first_byte, 1);
242 last_byte = first_byte;
244 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
245 address += TEST_MEM_PAGE_SIZE)
247 uint8_t b;
248 qtest_memread(who, address, &b, 1);
249 if (b != last_byte) {
250 if (((b + 1) % 256) == last_byte && !hit_edge) {
251 /* This is OK, the guest stopped at the point of
252 * incrementing the previous page but didn't get
253 * to us yet.
255 hit_edge = true;
256 last_byte = b;
257 } else {
258 bad++;
259 if (bad <= 10) {
260 fprintf(stderr, "Memory content inconsistency at %x"
261 " first_byte = %x last_byte = %x current = %x"
262 " hit_edge = %x\n",
263 address, first_byte, last_byte, b, hit_edge);
268 if (bad >= 10) {
269 fprintf(stderr, "and in another %d pages", bad - 10);
271 g_assert(bad == 0);
274 static void cleanup(const char *filename)
276 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
278 unlink(path);
281 static char *SocketAddress_to_str(SocketAddress *addr)
283 switch (addr->type) {
284 case SOCKET_ADDRESS_TYPE_INET:
285 return g_strdup_printf("tcp:%s:%s",
286 addr->u.inet.host,
287 addr->u.inet.port);
288 case SOCKET_ADDRESS_TYPE_UNIX:
289 return g_strdup_printf("unix:%s",
290 addr->u.q_unix.path);
291 case SOCKET_ADDRESS_TYPE_FD:
292 return g_strdup_printf("fd:%s", addr->u.fd.str);
293 case SOCKET_ADDRESS_TYPE_VSOCK:
294 return g_strdup_printf("tcp:%s:%s",
295 addr->u.vsock.cid,
296 addr->u.vsock.port);
297 default:
298 return g_strdup("unknown address type");
302 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
304 QDict *rsp;
305 char *result;
306 SocketAddressList *addrs;
307 Visitor *iv = NULL;
308 QObject *object;
310 rsp = migrate_query(who);
311 object = qdict_get(rsp, parameter);
313 iv = qobject_input_visitor_new(object);
314 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
315 visit_free(iv);
317 /* we are only using a single address */
318 result = SocketAddress_to_str(addrs->value);
320 qapi_free_SocketAddressList(addrs);
321 qobject_unref(rsp);
322 return result;
325 static long long migrate_get_parameter_int(QTestState *who,
326 const char *parameter)
328 QDict *rsp;
329 long long result;
331 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
332 result = qdict_get_int(rsp, parameter);
333 qobject_unref(rsp);
334 return result;
337 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
338 long long value)
340 long long result;
342 result = migrate_get_parameter_int(who, parameter);
343 g_assert_cmpint(result, ==, value);
346 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
347 long long value)
349 QDict *rsp;
351 rsp = qtest_qmp(who,
352 "{ 'execute': 'migrate-set-parameters',"
353 "'arguments': { %s: %lld } }",
354 parameter, value);
355 g_assert(qdict_haskey(rsp, "return"));
356 qobject_unref(rsp);
357 migrate_check_parameter_int(who, parameter, value);
360 static char *migrate_get_parameter_str(QTestState *who,
361 const char *parameter)
363 QDict *rsp;
364 char *result;
366 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
367 result = g_strdup(qdict_get_str(rsp, parameter));
368 qobject_unref(rsp);
369 return result;
372 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
373 const char *value)
375 g_autofree char *result = migrate_get_parameter_str(who, parameter);
376 g_assert_cmpstr(result, ==, value);
379 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
380 const char *value)
382 QDict *rsp;
384 rsp = qtest_qmp(who,
385 "{ 'execute': 'migrate-set-parameters',"
386 "'arguments': { %s: %s } }",
387 parameter, value);
388 g_assert(qdict_haskey(rsp, "return"));
389 qobject_unref(rsp);
390 migrate_check_parameter_str(who, parameter, value);
393 static void migrate_pause(QTestState *who)
395 QDict *rsp;
397 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
398 qobject_unref(rsp);
401 static void migrate_continue(QTestState *who, const char *state)
403 QDict *rsp;
405 rsp = wait_command(who,
406 "{ 'execute': 'migrate-continue',"
407 " 'arguments': { 'state': %s } }",
408 state);
409 qobject_unref(rsp);
412 static void migrate_recover(QTestState *who, const char *uri)
414 QDict *rsp;
416 rsp = wait_command(who,
417 "{ 'execute': 'migrate-recover', "
418 " 'id': 'recover-cmd', "
419 " 'arguments': { 'uri': %s } }",
420 uri);
421 qobject_unref(rsp);
424 static void migrate_cancel(QTestState *who)
426 QDict *rsp;
428 rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
429 qobject_unref(rsp);
432 static void migrate_set_capability(QTestState *who, const char *capability,
433 bool value)
435 QDict *rsp;
437 rsp = qtest_qmp(who,
438 "{ 'execute': 'migrate-set-capabilities',"
439 "'arguments': { "
440 "'capabilities': [ { "
441 "'capability': %s, 'state': %i } ] } }",
442 capability, value);
443 g_assert(qdict_haskey(rsp, "return"));
444 qobject_unref(rsp);
447 static void migrate_postcopy_start(QTestState *from, QTestState *to)
449 QDict *rsp;
451 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
452 qobject_unref(rsp);
454 if (!got_stop) {
455 qtest_qmp_eventwait(from, "STOP");
458 qtest_qmp_eventwait(to, "RESUME");
461 typedef struct {
463 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
464 * unconditionally, because it means the user would like to be verbose.
466 bool hide_stderr;
467 bool use_shmem;
468 /* only launch the target process */
469 bool only_target;
470 char *opts_source;
471 char *opts_target;
472 } MigrateStart;
474 static MigrateStart *migrate_start_new(void)
476 MigrateStart *args = g_new0(MigrateStart, 1);
478 args->opts_source = g_strdup("");
479 args->opts_target = g_strdup("");
480 return args;
483 static void migrate_start_destroy(MigrateStart *args)
485 g_free(args->opts_source);
486 g_free(args->opts_target);
487 g_free(args);
490 static int test_migrate_start(QTestState **from, QTestState **to,
491 const char *uri, MigrateStart *args)
493 g_autofree gchar *arch_source = NULL;
494 g_autofree gchar *arch_target = NULL;
495 g_autofree gchar *cmd_source = NULL;
496 g_autofree gchar *cmd_target = NULL;
497 const gchar *ignore_stderr;
498 g_autofree char *bootpath = NULL;
499 g_autofree char *shmem_opts = NULL;
500 g_autofree char *shmem_path = NULL;
501 const char *arch = qtest_get_arch();
502 const char *machine_opts = NULL;
503 const char *memory_size;
504 int ret = 0;
506 if (args->use_shmem) {
507 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
508 g_test_skip("/dev/shm is not supported");
509 ret = -1;
510 goto out;
514 got_stop = false;
515 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
516 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
517 /* the assembled x86 boot sector should be exactly one sector large */
518 assert(sizeof(x86_bootsect) == 512);
519 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
520 memory_size = "150M";
521 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
522 arch_target = g_strdup(arch_source);
523 start_address = X86_TEST_MEM_START;
524 end_address = X86_TEST_MEM_END;
525 } else if (g_str_equal(arch, "s390x")) {
526 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
527 memory_size = "128M";
528 arch_source = g_strdup_printf("-bios %s", bootpath);
529 arch_target = g_strdup(arch_source);
530 start_address = S390_TEST_MEM_START;
531 end_address = S390_TEST_MEM_END;
532 } else if (strcmp(arch, "ppc64") == 0) {
533 machine_opts = "vsmt=8";
534 memory_size = "256M";
535 start_address = PPC_TEST_MEM_START;
536 end_address = PPC_TEST_MEM_END;
537 arch_source = g_strdup_printf("-nodefaults "
538 "-prom-env 'use-nvramrc?=true' -prom-env "
539 "'nvramrc=hex .\" _\" begin %x %x "
540 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
541 "until'", end_address, start_address);
542 arch_target = g_strdup("");
543 } else if (strcmp(arch, "aarch64") == 0) {
544 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
545 machine_opts = "virt,gic-version=max";
546 memory_size = "150M";
547 arch_source = g_strdup_printf("-cpu max "
548 "-kernel %s",
549 bootpath);
550 arch_target = g_strdup(arch_source);
551 start_address = ARM_TEST_MEM_START;
552 end_address = ARM_TEST_MEM_END;
554 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
555 } else {
556 g_assert_not_reached();
559 if (!getenv("QTEST_LOG") && args->hide_stderr) {
560 ignore_stderr = "2>/dev/null";
561 } else {
562 ignore_stderr = "";
565 if (args->use_shmem) {
566 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
567 shmem_opts = g_strdup_printf(
568 "-object memory-backend-file,id=mem0,size=%s"
569 ",mem-path=%s,share=on -numa node,memdev=mem0",
570 memory_size, shmem_path);
571 } else {
572 shmem_path = NULL;
573 shmem_opts = g_strdup("");
576 cmd_source = g_strdup_printf("-accel kvm -accel tcg%s%s "
577 "-name source,debug-threads=on "
578 "-m %s "
579 "-serial file:%s/src_serial "
580 "%s %s %s %s",
581 machine_opts ? " -machine " : "",
582 machine_opts ? machine_opts : "",
583 memory_size, tmpfs,
584 arch_source, shmem_opts, args->opts_source,
585 ignore_stderr);
586 if (!args->only_target) {
587 *from = qtest_init(cmd_source);
590 cmd_target = g_strdup_printf("-accel kvm -accel tcg%s%s "
591 "-name target,debug-threads=on "
592 "-m %s "
593 "-serial file:%s/dest_serial "
594 "-incoming %s "
595 "%s %s %s %s",
596 machine_opts ? " -machine " : "",
597 machine_opts ? machine_opts : "",
598 memory_size, tmpfs, uri,
599 arch_target, shmem_opts,
600 args->opts_target, ignore_stderr);
601 *to = qtest_init(cmd_target);
604 * Remove shmem file immediately to avoid memory leak in test failed case.
605 * It's valid becase QEMU has already opened this file
607 if (args->use_shmem) {
608 unlink(shmem_path);
611 out:
612 migrate_start_destroy(args);
613 return ret;
616 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
618 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
620 qtest_quit(from);
622 if (test_dest) {
623 qtest_memread(to, start_address, &dest_byte_a, 1);
625 /* Destination still running, wait for a byte to change */
626 do {
627 qtest_memread(to, start_address, &dest_byte_b, 1);
628 usleep(1000 * 10);
629 } while (dest_byte_a == dest_byte_b);
631 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
633 /* With it stopped, check nothing changes */
634 qtest_memread(to, start_address, &dest_byte_c, 1);
635 usleep(1000 * 200);
636 qtest_memread(to, start_address, &dest_byte_d, 1);
637 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
639 check_guests_ram(to);
642 qtest_quit(to);
644 cleanup("bootsect");
645 cleanup("migsocket");
646 cleanup("src_serial");
647 cleanup("dest_serial");
650 static int migrate_postcopy_prepare(QTestState **from_ptr,
651 QTestState **to_ptr,
652 MigrateStart *args)
654 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
655 QTestState *from, *to;
657 if (test_migrate_start(&from, &to, uri, args)) {
658 return -1;
661 migrate_set_capability(from, "postcopy-ram", true);
662 migrate_set_capability(to, "postcopy-ram", true);
663 migrate_set_capability(to, "postcopy-blocktime", true);
665 /* We want to pick a speed slow enough that the test completes
666 * quickly, but that it doesn't complete precopy even on a slow
667 * machine, so also set the downtime.
669 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
670 migrate_set_parameter_int(from, "downtime-limit", 1);
672 /* Wait for the first serial output from the source */
673 wait_for_serial("src_serial");
675 migrate_qmp(from, uri, "{}");
677 wait_for_migration_pass(from);
679 *from_ptr = from;
680 *to_ptr = to;
682 return 0;
685 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
687 wait_for_migration_complete(from);
689 /* Make sure we get at least one "B" on destination */
690 wait_for_serial("dest_serial");
692 if (uffd_feature_thread_id) {
693 read_blocktime(to);
696 test_migrate_end(from, to, true);
699 static void test_postcopy(void)
701 MigrateStart *args = migrate_start_new();
702 QTestState *from, *to;
704 if (migrate_postcopy_prepare(&from, &to, args)) {
705 return;
707 migrate_postcopy_start(from, to);
708 migrate_postcopy_complete(from, to);
711 static void test_postcopy_recovery(void)
713 MigrateStart *args = migrate_start_new();
714 QTestState *from, *to;
715 g_autofree char *uri = NULL;
717 args->hide_stderr = true;
719 if (migrate_postcopy_prepare(&from, &to, args)) {
720 return;
723 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
724 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
726 /* Now we start the postcopy */
727 migrate_postcopy_start(from, to);
730 * Wait until postcopy is really started; we can only run the
731 * migrate-pause command during a postcopy
733 wait_for_migration_status(from, "postcopy-active", NULL);
736 * Manually stop the postcopy migration. This emulates a network
737 * failure with the migration socket
739 migrate_pause(from);
742 * Wait for destination side to reach postcopy-paused state. The
743 * migrate-recover command can only succeed if destination machine
744 * is in the paused state
746 wait_for_migration_status(to, "postcopy-paused",
747 (const char * []) { "failed", "active",
748 "completed", NULL });
751 * Create a new socket to emulate a new channel that is different
752 * from the broken migration channel; tell the destination to
753 * listen to the new port
755 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
756 migrate_recover(to, uri);
759 * Try to rebuild the migration channel using the resume flag and
760 * the newly created channel
762 wait_for_migration_status(from, "postcopy-paused",
763 (const char * []) { "failed", "active",
764 "completed", NULL });
765 migrate_qmp(from, uri, "{'resume': true}");
767 /* Restore the postcopy bandwidth to unlimited */
768 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
770 migrate_postcopy_complete(from, to);
773 static void test_baddest(void)
775 MigrateStart *args = migrate_start_new();
776 QTestState *from, *to;
778 args->hide_stderr = true;
780 if (test_migrate_start(&from, &to, "tcp:0:0", args)) {
781 return;
783 migrate_qmp(from, "tcp:0:0", "{}");
784 wait_for_migration_fail(from, false);
785 test_migrate_end(from, to, false);
788 static void test_precopy_unix(void)
790 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
791 MigrateStart *args = migrate_start_new();
792 QTestState *from, *to;
794 if (test_migrate_start(&from, &to, uri, args)) {
795 return;
798 /* We want to pick a speed slow enough that the test completes
799 * quickly, but that it doesn't complete precopy even on a slow
800 * machine, so also set the downtime.
802 /* 1 ms should make it not converge*/
803 migrate_set_parameter_int(from, "downtime-limit", 1);
804 /* 1GB/s */
805 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
807 /* Wait for the first serial output from the source */
808 wait_for_serial("src_serial");
810 migrate_qmp(from, uri, "{}");
812 wait_for_migration_pass(from);
814 migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
816 if (!got_stop) {
817 qtest_qmp_eventwait(from, "STOP");
820 qtest_qmp_eventwait(to, "RESUME");
822 wait_for_serial("dest_serial");
823 wait_for_migration_complete(from);
825 test_migrate_end(from, to, true);
828 #if 0
829 /* Currently upset on aarch64 TCG */
830 static void test_ignore_shared(void)
832 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
833 QTestState *from, *to;
835 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
836 return;
839 migrate_set_capability(from, "x-ignore-shared", true);
840 migrate_set_capability(to, "x-ignore-shared", true);
842 /* Wait for the first serial output from the source */
843 wait_for_serial("src_serial");
845 migrate_qmp(from, uri, "{}");
847 wait_for_migration_pass(from);
849 if (!got_stop) {
850 qtest_qmp_eventwait(from, "STOP");
853 qtest_qmp_eventwait(to, "RESUME");
855 wait_for_serial("dest_serial");
856 wait_for_migration_complete(from);
858 /* Check whether shared RAM has been really skipped */
859 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
861 test_migrate_end(from, to, true);
863 #endif
865 static void test_xbzrle(const char *uri)
867 MigrateStart *args = migrate_start_new();
868 QTestState *from, *to;
870 if (test_migrate_start(&from, &to, uri, args)) {
871 return;
875 * We want to pick a speed slow enough that the test completes
876 * quickly, but that it doesn't complete precopy even on a slow
877 * machine, so also set the downtime.
879 /* 1 ms should make it not converge*/
880 migrate_set_parameter_int(from, "downtime-limit", 1);
881 /* 1GB/s */
882 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
884 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
886 migrate_set_capability(from, "xbzrle", true);
887 migrate_set_capability(to, "xbzrle", true);
888 /* Wait for the first serial output from the source */
889 wait_for_serial("src_serial");
891 migrate_qmp(from, uri, "{}");
893 wait_for_migration_pass(from);
894 /* Make sure we have 2 passes, so the xbzrle cache gets a workout */
895 wait_for_migration_pass(from);
897 /* 1000ms should converge */
898 migrate_set_parameter_int(from, "downtime-limit", 1000);
900 if (!got_stop) {
901 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);
911 static void test_xbzrle_unix(void)
913 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
915 test_xbzrle(uri);
918 static void test_precopy_tcp(void)
920 MigrateStart *args = migrate_start_new();
921 g_autofree char *uri = NULL;
922 QTestState *from, *to;
924 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", args)) {
925 return;
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);
935 /* 1GB/s */
936 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
938 /* Wait for the first serial output from the source */
939 wait_for_serial("src_serial");
941 uri = migrate_get_socket_address(to, "socket-address");
943 migrate_qmp(from, uri, "{}");
945 wait_for_migration_pass(from);
947 migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
949 if (!got_stop) {
950 qtest_qmp_eventwait(from, "STOP");
952 qtest_qmp_eventwait(to, "RESUME");
954 wait_for_serial("dest_serial");
955 wait_for_migration_complete(from);
957 test_migrate_end(from, to, true);
960 static void test_migrate_fd_proto(void)
962 MigrateStart *args = migrate_start_new();
963 QTestState *from, *to;
964 int ret;
965 int pair[2];
966 QDict *rsp;
967 const char *error_desc;
969 if (test_migrate_start(&from, &to, "defer", args)) {
970 return;
974 * We want to pick a speed slow enough that the test completes
975 * quickly, but that it doesn't complete precopy even on a slow
976 * machine, so also set the downtime.
978 /* 1 ms should make it not converge */
979 migrate_set_parameter_int(from, "downtime-limit", 1);
980 /* 1GB/s */
981 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
983 /* Wait for the first serial output from the source */
984 wait_for_serial("src_serial");
986 /* Create two connected sockets for migration */
987 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
988 g_assert_cmpint(ret, ==, 0);
990 /* Send the 1st socket to the target */
991 rsp = wait_command_fd(to, pair[0],
992 "{ 'execute': 'getfd',"
993 " 'arguments': { 'fdname': 'fd-mig' }}");
994 qobject_unref(rsp);
995 close(pair[0]);
997 /* Start incoming migration from the 1st socket */
998 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
999 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1000 qobject_unref(rsp);
1002 /* Send the 2nd socket to the target */
1003 rsp = wait_command_fd(from, pair[1],
1004 "{ 'execute': 'getfd',"
1005 " 'arguments': { 'fdname': 'fd-mig' }}");
1006 qobject_unref(rsp);
1007 close(pair[1]);
1009 /* Start migration to the 2nd socket*/
1010 migrate_qmp(from, "fd:fd-mig", "{}");
1012 wait_for_migration_pass(from);
1014 migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1016 if (!got_stop) {
1017 qtest_qmp_eventwait(from, "STOP");
1019 qtest_qmp_eventwait(to, "RESUME");
1021 /* Test closing fds */
1022 /* We assume, that QEMU removes named fd from its list,
1023 * so this should fail */
1024 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1025 " 'arguments': { 'fdname': 'fd-mig' }}");
1026 g_assert_true(qdict_haskey(rsp, "error"));
1027 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1028 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1029 qobject_unref(rsp);
1031 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1032 " 'arguments': { 'fdname': 'fd-mig' }}");
1033 g_assert_true(qdict_haskey(rsp, "error"));
1034 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1035 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1036 qobject_unref(rsp);
1038 /* Complete migration */
1039 wait_for_serial("dest_serial");
1040 wait_for_migration_complete(from);
1041 test_migrate_end(from, to, true);
1044 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1046 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1047 QTestState *from, *to;
1049 if (test_migrate_start(&from, &to, uri, args)) {
1050 return;
1054 * UUID validation is at the begin of migration. So, the main process of
1055 * migration is not interesting for us here. Thus, set huge downtime for
1056 * very fast migration.
1058 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1059 migrate_set_capability(from, "validate-uuid", true);
1061 /* Wait for the first serial output from the source */
1062 wait_for_serial("src_serial");
1064 migrate_qmp(from, uri, "{}");
1066 if (should_fail) {
1067 qtest_set_expected_status(to, 1);
1068 wait_for_migration_fail(from, true);
1069 } else {
1070 wait_for_migration_complete(from);
1073 test_migrate_end(from, to, false);
1076 static void test_validate_uuid(void)
1078 MigrateStart *args = migrate_start_new();
1080 g_free(args->opts_source);
1081 g_free(args->opts_target);
1082 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1083 args->opts_target = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1084 do_test_validate_uuid(args, false);
1087 static void test_validate_uuid_error(void)
1089 MigrateStart *args = migrate_start_new();
1091 g_free(args->opts_source);
1092 g_free(args->opts_target);
1093 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1094 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1095 args->hide_stderr = true;
1096 do_test_validate_uuid(args, true);
1099 static void test_validate_uuid_src_not_set(void)
1101 MigrateStart *args = migrate_start_new();
1103 g_free(args->opts_target);
1104 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1105 args->hide_stderr = true;
1106 do_test_validate_uuid(args, false);
1109 static void test_validate_uuid_dst_not_set(void)
1111 MigrateStart *args = migrate_start_new();
1113 g_free(args->opts_source);
1114 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1115 args->hide_stderr = true;
1116 do_test_validate_uuid(args, false);
1119 static void test_migrate_auto_converge(void)
1121 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1122 MigrateStart *args = migrate_start_new();
1123 QTestState *from, *to;
1124 int64_t remaining, percentage;
1127 * We want the test to be stable and as fast as possible.
1128 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1129 * so we need to decrease a bandwidth.
1131 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1132 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1133 const int64_t downtime_limit = 250; /* 250ms */
1135 * We migrate through unix-socket (> 500Mb/s).
1136 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1137 * So, we can predict expected_threshold
1139 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1141 if (test_migrate_start(&from, &to, uri, args)) {
1142 return;
1145 migrate_set_capability(from, "auto-converge", true);
1146 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1147 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1148 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1151 * Set the initial parameters so that the migration could not converge
1152 * without throttling.
1154 migrate_set_parameter_int(from, "downtime-limit", 1);
1155 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1157 /* To check remaining size after precopy */
1158 migrate_set_capability(from, "pause-before-switchover", true);
1160 /* Wait for the first serial output from the source */
1161 wait_for_serial("src_serial");
1163 migrate_qmp(from, uri, "{}");
1165 /* Wait for throttling begins */
1166 percentage = 0;
1167 while (percentage == 0) {
1168 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1169 usleep(100);
1170 g_assert_false(got_stop);
1172 /* The first percentage of throttling should be equal to init_pct */
1173 g_assert_cmpint(percentage, ==, init_pct);
1174 /* Now, when we tested that throttling works, let it converge */
1175 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1176 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1179 * Wait for pre-switchover status to check last throttle percentage
1180 * and remaining. These values will be zeroed later
1182 wait_for_migration_status(from, "pre-switchover", NULL);
1184 /* The final percentage of throttling shouldn't be greater than max_pct */
1185 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1186 g_assert_cmpint(percentage, <=, max_pct);
1188 remaining = read_ram_property_int(from, "remaining");
1189 g_assert_cmpint(remaining, <,
1190 (expected_threshold + expected_threshold / 100));
1192 migrate_continue(from, "pre-switchover");
1194 qtest_qmp_eventwait(to, "RESUME");
1196 wait_for_serial("dest_serial");
1197 wait_for_migration_complete(from);
1200 test_migrate_end(from, to, true);
1203 static void test_multifd_tcp(const char *method)
1205 MigrateStart *args = migrate_start_new();
1206 QTestState *from, *to;
1207 QDict *rsp;
1208 g_autofree char *uri = NULL;
1210 if (test_migrate_start(&from, &to, "defer", args)) {
1211 return;
1215 * We want to pick a speed slow enough that the test completes
1216 * quickly, but that it doesn't complete precopy even on a slow
1217 * machine, so also set the downtime.
1219 /* 1 ms should make it not converge*/
1220 migrate_set_parameter_int(from, "downtime-limit", 1);
1221 /* 1GB/s */
1222 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1224 migrate_set_parameter_int(from, "multifd-channels", 16);
1225 migrate_set_parameter_int(to, "multifd-channels", 16);
1227 migrate_set_parameter_str(from, "multifd-compression", method);
1228 migrate_set_parameter_str(to, "multifd-compression", method);
1230 migrate_set_capability(from, "multifd", true);
1231 migrate_set_capability(to, "multifd", true);
1233 /* Start incoming migration from the 1st socket */
1234 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1235 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1236 qobject_unref(rsp);
1238 /* Wait for the first serial output from the source */
1239 wait_for_serial("src_serial");
1241 uri = migrate_get_socket_address(to, "socket-address");
1243 migrate_qmp(from, uri, "{}");
1245 wait_for_migration_pass(from);
1247 migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1249 if (!got_stop) {
1250 qtest_qmp_eventwait(from, "STOP");
1252 qtest_qmp_eventwait(to, "RESUME");
1254 wait_for_serial("dest_serial");
1255 wait_for_migration_complete(from);
1256 test_migrate_end(from, to, true);
1259 static void test_multifd_tcp_none(void)
1261 test_multifd_tcp("none");
1264 static void test_multifd_tcp_zlib(void)
1266 test_multifd_tcp("zlib");
1269 #ifdef CONFIG_ZSTD
1270 static void test_multifd_tcp_zstd(void)
1272 test_multifd_tcp("zstd");
1274 #endif
1277 * This test does:
1278 * source target
1279 * migrate_incoming
1280 * migrate
1281 * migrate_cancel
1282 * launch another target
1283 * migrate
1285 * And see that it works
1287 static void test_multifd_tcp_cancel(void)
1289 MigrateStart *args = migrate_start_new();
1290 QTestState *from, *to, *to2;
1291 QDict *rsp;
1292 g_autofree char *uri = NULL;
1294 args->hide_stderr = true;
1296 if (test_migrate_start(&from, &to, "defer", args)) {
1297 return;
1301 * We want to pick a speed slow enough that the test completes
1302 * quickly, but that it doesn't complete precopy even on a slow
1303 * machine, so also set the downtime.
1305 /* 1 ms should make it not converge*/
1306 migrate_set_parameter_int(from, "downtime-limit", 1);
1307 /* 300MB/s */
1308 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
1310 migrate_set_parameter_int(from, "multifd-channels", 16);
1311 migrate_set_parameter_int(to, "multifd-channels", 16);
1313 migrate_set_capability(from, "multifd", true);
1314 migrate_set_capability(to, "multifd", true);
1316 /* Start incoming migration from the 1st socket */
1317 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1318 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1319 qobject_unref(rsp);
1321 /* Wait for the first serial output from the source */
1322 wait_for_serial("src_serial");
1324 uri = migrate_get_socket_address(to, "socket-address");
1326 migrate_qmp(from, uri, "{}");
1328 wait_for_migration_pass(from);
1330 migrate_cancel(from);
1332 args = migrate_start_new();
1333 args->only_target = true;
1335 if (test_migrate_start(&from, &to2, "defer", args)) {
1336 return;
1339 migrate_set_parameter_int(to2, "multifd-channels", 16);
1341 migrate_set_capability(to2, "multifd", true);
1343 /* Start incoming migration from the 1st socket */
1344 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
1345 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1346 qobject_unref(rsp);
1348 g_free(uri);
1349 uri = migrate_get_socket_address(to2, "socket-address");
1351 wait_for_migration_status(from, "cancelled", NULL);
1353 /* 300ms it should converge */
1354 migrate_set_parameter_int(from, "downtime-limit", 300);
1355 /* 1GB/s */
1356 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1358 migrate_qmp(from, uri, "{}");
1360 wait_for_migration_pass(from);
1362 if (!got_stop) {
1363 qtest_qmp_eventwait(from, "STOP");
1365 qtest_qmp_eventwait(to2, "RESUME");
1367 wait_for_serial("dest_serial");
1368 wait_for_migration_complete(from);
1369 test_migrate_end(from, to2, true);
1372 int main(int argc, char **argv)
1374 char template[] = "/tmp/migration-test-XXXXXX";
1375 int ret;
1377 g_test_init(&argc, &argv, NULL);
1379 if (!ufd_version_check()) {
1380 return g_test_run();
1384 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1385 * is touchy due to race conditions on dirty bits (especially on PPC for
1386 * some reason)
1388 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1389 (access("/sys/module/kvm_hv", F_OK) ||
1390 access("/dev/kvm", R_OK | W_OK))) {
1391 g_test_message("Skipping test: kvm_hv not available");
1392 return g_test_run();
1396 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1397 * there until the problems are resolved
1399 if (g_str_equal(qtest_get_arch(), "s390x")) {
1400 #if defined(HOST_S390X)
1401 if (access("/dev/kvm", R_OK | W_OK)) {
1402 g_test_message("Skipping test: kvm not available");
1403 return g_test_run();
1405 #else
1406 g_test_message("Skipping test: Need s390x host to work properly");
1407 return g_test_run();
1408 #endif
1411 tmpfs = mkdtemp(template);
1412 if (!tmpfs) {
1413 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1415 g_assert(tmpfs);
1417 module_call_init(MODULE_INIT_QOM);
1419 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1420 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1421 qtest_add_func("/migration/bad_dest", test_baddest);
1422 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1423 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1424 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1425 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1426 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1427 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1428 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1429 qtest_add_func("/migration/validate_uuid_src_not_set",
1430 test_validate_uuid_src_not_set);
1431 qtest_add_func("/migration/validate_uuid_dst_not_set",
1432 test_validate_uuid_dst_not_set);
1434 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1435 qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
1436 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel);
1437 qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
1438 #ifdef CONFIG_ZSTD
1439 qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd);
1440 #endif
1442 ret = g_test_run();
1444 g_assert_cmpint(ret, ==, 0);
1446 ret = rmdir(tmpfs);
1447 if (ret != 0) {
1448 g_test_message("unable to rmdir: path (%s): %s",
1449 tmpfs, strerror(errno));
1452 return ret;