tests/iothread: Always connect iothread GSource to a GMainContext
[qemu/ar7.git] / tests / migration-test.c
blob53afec439522df145bc35d7d060997d9fe9b4eab
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 "libqtest.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "qapi/qapi-visit-sockets.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qapi/qobject-output-visitor.h"
26 #include "migration-helpers.h"
27 #include "migration/migration-test.h"
29 /* TODO actually test the results and get rid of this */
30 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
32 unsigned start_address;
33 unsigned end_address;
34 static bool uffd_feature_thread_id;
36 #if defined(__linux__)
37 #include <sys/syscall.h>
38 #include <sys/vfs.h>
39 #endif
41 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
42 #include <sys/eventfd.h>
43 #include <sys/ioctl.h>
44 #include <linux/userfaultfd.h>
46 static bool ufd_version_check(void)
48 struct uffdio_api api_struct;
49 uint64_t ioctl_mask;
51 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
53 if (ufd == -1) {
54 g_test_message("Skipping test: userfaultfd not available");
55 return false;
58 api_struct.api = UFFD_API;
59 api_struct.features = 0;
60 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
61 g_test_message("Skipping test: UFFDIO_API failed");
62 return false;
64 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
66 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
67 (__u64)1 << _UFFDIO_UNREGISTER;
68 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
69 g_test_message("Skipping test: Missing userfault feature");
70 return false;
73 return true;
76 #else
77 static bool ufd_version_check(void)
79 g_test_message("Skipping test: Userfault not available (builtdtime)");
80 return false;
83 #endif
85 static const char *tmpfs;
87 /* The boot file modifies memory area in [start_address, end_address)
88 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
90 #include "tests/migration/i386/a-b-bootblock.h"
91 #include "tests/migration/aarch64/a-b-kernel.h"
92 #include "tests/migration/s390x/a-b-bios.h"
94 static void init_bootfile(const char *bootpath, void *content, size_t len)
96 FILE *bootfile = fopen(bootpath, "wb");
98 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
99 fclose(bootfile);
103 * Wait for some output in the serial output file,
104 * we get an 'A' followed by an endless string of 'B's
105 * but on the destination we won't have the A.
107 static void wait_for_serial(const char *side)
109 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
110 FILE *serialfile = fopen(serialpath, "r");
111 const char *arch = qtest_get_arch();
112 int started = (strcmp(side, "src_serial") == 0 &&
113 strcmp(arch, "ppc64") == 0) ? 0 : 1;
115 g_free(serialpath);
116 do {
117 int readvalue = fgetc(serialfile);
119 if (!started) {
120 /* SLOF prints its banner before starting test,
121 * to ignore it, mark the start of the test with '_',
122 * ignore all characters until this marker
124 switch (readvalue) {
125 case '_':
126 started = 1;
127 break;
128 case EOF:
129 fseek(serialfile, 0, SEEK_SET);
130 usleep(1000);
131 break;
133 continue;
135 switch (readvalue) {
136 case 'A':
137 /* Fine */
138 break;
140 case 'B':
141 /* It's alive! */
142 fclose(serialfile);
143 return;
145 case EOF:
146 started = (strcmp(side, "src_serial") == 0 &&
147 strcmp(arch, "ppc64") == 0) ? 0 : 1;
148 fseek(serialfile, 0, SEEK_SET);
149 usleep(1000);
150 break;
152 default:
153 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
154 g_assert_not_reached();
156 } while (true);
160 * It's tricky to use qemu's migration event capability with qtest,
161 * events suddenly appearing confuse the qmp()/hmp() responses.
164 static int64_t read_ram_property_int(QTestState *who, const char *property)
166 QDict *rsp_return, *rsp_ram;
167 int64_t result;
169 rsp_return = migrate_query(who);
170 if (!qdict_haskey(rsp_return, "ram")) {
171 /* Still in setup */
172 result = 0;
173 } else {
174 rsp_ram = qdict_get_qdict(rsp_return, "ram");
175 result = qdict_get_try_int(rsp_ram, property, 0);
177 qobject_unref(rsp_return);
178 return result;
181 static int64_t read_migrate_property_int(QTestState *who, const char *property)
183 QDict *rsp_return;
184 int64_t result;
186 rsp_return = migrate_query(who);
187 result = qdict_get_try_int(rsp_return, property, 0);
188 qobject_unref(rsp_return);
189 return result;
192 static uint64_t get_migration_pass(QTestState *who)
194 return read_ram_property_int(who, "dirty-sync-count");
197 static void read_blocktime(QTestState *who)
199 QDict *rsp_return;
201 rsp_return = migrate_query(who);
202 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
203 qobject_unref(rsp_return);
206 static void wait_for_migration_pass(QTestState *who)
208 uint64_t initial_pass = get_migration_pass(who);
209 uint64_t pass;
211 /* Wait for the 1st sync */
212 while (!got_stop && !initial_pass) {
213 usleep(1000);
214 initial_pass = get_migration_pass(who);
217 do {
218 usleep(1000);
219 pass = get_migration_pass(who);
220 } while (pass == initial_pass && !got_stop);
223 static void check_guests_ram(QTestState *who)
225 /* Our ASM test will have been incrementing one byte from each page from
226 * start_address to < end_address in order. This gives us a constraint
227 * that any page's byte should be equal or less than the previous pages
228 * byte (mod 256); and they should all be equal except for one transition
229 * at the point where we meet the incrementer. (We're running this with
230 * the guest stopped).
232 unsigned address;
233 uint8_t first_byte;
234 uint8_t last_byte;
235 bool hit_edge = false;
236 int bad = 0;
238 qtest_memread(who, start_address, &first_byte, 1);
239 last_byte = first_byte;
241 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
242 address += TEST_MEM_PAGE_SIZE)
244 uint8_t b;
245 qtest_memread(who, address, &b, 1);
246 if (b != last_byte) {
247 if (((b + 1) % 256) == last_byte && !hit_edge) {
248 /* This is OK, the guest stopped at the point of
249 * incrementing the previous page but didn't get
250 * to us yet.
252 hit_edge = true;
253 last_byte = b;
254 } else {
255 bad++;
256 if (bad <= 10) {
257 fprintf(stderr, "Memory content inconsistency at %x"
258 " first_byte = %x last_byte = %x current = %x"
259 " hit_edge = %x\n",
260 address, first_byte, last_byte, b, hit_edge);
265 if (bad >= 10) {
266 fprintf(stderr, "and in another %d pages", bad - 10);
268 g_assert(bad == 0);
271 static void cleanup(const char *filename)
273 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
275 unlink(path);
276 g_free(path);
279 static char *SocketAddress_to_str(SocketAddress *addr)
281 switch (addr->type) {
282 case SOCKET_ADDRESS_TYPE_INET:
283 return g_strdup_printf("tcp:%s:%s",
284 addr->u.inet.host,
285 addr->u.inet.port);
286 case SOCKET_ADDRESS_TYPE_UNIX:
287 return g_strdup_printf("unix:%s",
288 addr->u.q_unix.path);
289 case SOCKET_ADDRESS_TYPE_FD:
290 return g_strdup_printf("fd:%s", addr->u.fd.str);
291 case SOCKET_ADDRESS_TYPE_VSOCK:
292 return g_strdup_printf("tcp:%s:%s",
293 addr->u.vsock.cid,
294 addr->u.vsock.port);
295 default:
296 return g_strdup("unknown address type");
300 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
302 QDict *rsp;
303 char *result;
304 Error *local_err = NULL;
305 SocketAddressList *addrs;
306 Visitor *iv = NULL;
307 QObject *object;
309 rsp = migrate_query(who);
310 object = qdict_get(rsp, parameter);
312 iv = qobject_input_visitor_new(object);
313 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
314 visit_free(iv);
316 /* we are only using a single address */
317 result = SocketAddress_to_str(addrs->value);
319 qapi_free_SocketAddressList(addrs);
320 qobject_unref(rsp);
321 return result;
324 static long long migrate_get_parameter_int(QTestState *who,
325 const char *parameter)
327 QDict *rsp;
328 long long result;
330 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
331 result = qdict_get_int(rsp, parameter);
332 qobject_unref(rsp);
333 return result;
336 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
337 long long value)
339 long long result;
341 result = migrate_get_parameter_int(who, parameter);
342 g_assert_cmpint(result, ==, value);
345 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
346 long long value)
348 QDict *rsp;
350 rsp = qtest_qmp(who,
351 "{ 'execute': 'migrate-set-parameters',"
352 "'arguments': { %s: %lld } }",
353 parameter, value);
354 g_assert(qdict_haskey(rsp, "return"));
355 qobject_unref(rsp);
356 migrate_check_parameter_int(who, parameter, value);
359 static void migrate_pause(QTestState *who)
361 QDict *rsp;
363 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
364 qobject_unref(rsp);
367 static void migrate_continue(QTestState *who, const char *state)
369 QDict *rsp;
371 rsp = wait_command(who,
372 "{ 'execute': 'migrate-continue',"
373 " 'arguments': { 'state': %s } }",
374 state);
375 qobject_unref(rsp);
378 static void migrate_recover(QTestState *who, const char *uri)
380 QDict *rsp;
382 rsp = wait_command(who,
383 "{ 'execute': 'migrate-recover', "
384 " 'id': 'recover-cmd', "
385 " 'arguments': { 'uri': %s } }",
386 uri);
387 qobject_unref(rsp);
390 static void migrate_set_capability(QTestState *who, const char *capability,
391 bool value)
393 QDict *rsp;
395 rsp = qtest_qmp(who,
396 "{ 'execute': 'migrate-set-capabilities',"
397 "'arguments': { "
398 "'capabilities': [ { "
399 "'capability': %s, 'state': %i } ] } }",
400 capability, value);
401 g_assert(qdict_haskey(rsp, "return"));
402 qobject_unref(rsp);
405 static void migrate_postcopy_start(QTestState *from, QTestState *to)
407 QDict *rsp;
409 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
410 qobject_unref(rsp);
412 if (!got_stop) {
413 qtest_qmp_eventwait(from, "STOP");
416 qtest_qmp_eventwait(to, "RESUME");
419 typedef struct {
420 bool hide_stderr;
421 bool use_shmem;
422 char *opts_source;
423 char *opts_target;
424 } MigrateStart;
426 static MigrateStart *migrate_start_new(void)
428 MigrateStart *args = g_new0(MigrateStart, 1);
430 args->opts_source = g_strdup("");
431 args->opts_target = g_strdup("");
432 return args;
435 static void migrate_start_destroy(MigrateStart *args)
437 g_free(args->opts_source);
438 g_free(args->opts_target);
439 g_free(args);
442 static int test_migrate_start(QTestState **from, QTestState **to,
443 const char *uri, MigrateStart *args)
445 gchar *arch_source, *arch_target;
446 gchar *cmd_source, *cmd_target;
447 const gchar *ignore_stderr;
448 char *bootpath = NULL;
449 char *shmem_opts;
450 char *shmem_path;
451 const char *arch = qtest_get_arch();
452 const char *machine_opts = NULL;
453 const char *memory_size;
455 if (args->use_shmem) {
456 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
457 g_test_skip("/dev/shm is not supported");
458 return -1;
462 got_stop = false;
463 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
464 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
465 /* the assembled x86 boot sector should be exactly one sector large */
466 assert(sizeof(x86_bootsect) == 512);
467 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
468 memory_size = "150M";
469 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
470 arch_target = g_strdup(arch_source);
471 start_address = X86_TEST_MEM_START;
472 end_address = X86_TEST_MEM_END;
473 } else if (g_str_equal(arch, "s390x")) {
474 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
475 memory_size = "128M";
476 arch_source = g_strdup_printf("-bios %s", bootpath);
477 arch_target = g_strdup(arch_source);
478 start_address = S390_TEST_MEM_START;
479 end_address = S390_TEST_MEM_END;
480 } else if (strcmp(arch, "ppc64") == 0) {
481 machine_opts = "vsmt=8";
482 memory_size = "256M";
483 arch_source = g_strdup_printf("-nodefaults "
484 "-prom-env 'use-nvramrc?=true' -prom-env "
485 "'nvramrc=hex .\" _\" begin %x %x "
486 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
487 "until'", end_address, start_address);
488 arch_target = g_strdup("");
489 start_address = PPC_TEST_MEM_START;
490 end_address = PPC_TEST_MEM_END;
491 } else if (strcmp(arch, "aarch64") == 0) {
492 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
493 machine_opts = "virt,gic-version=max";
494 memory_size = "150M";
495 arch_source = g_strdup_printf("-cpu max "
496 "-kernel %s",
497 bootpath);
498 arch_target = g_strdup(arch_source);
499 start_address = ARM_TEST_MEM_START;
500 end_address = ARM_TEST_MEM_END;
502 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
503 } else {
504 g_assert_not_reached();
507 g_free(bootpath);
509 if (args->hide_stderr) {
510 ignore_stderr = "2>/dev/null";
511 } else {
512 ignore_stderr = "";
515 if (args->use_shmem) {
516 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
517 shmem_opts = g_strdup_printf(
518 "-object memory-backend-file,id=mem0,size=%s"
519 ",mem-path=%s,share=on -numa node,memdev=mem0",
520 memory_size, shmem_path);
521 } else {
522 shmem_path = NULL;
523 shmem_opts = g_strdup("");
526 cmd_source = g_strdup_printf("-accel kvm -accel tcg%s%s "
527 "-name source,debug-threads=on "
528 "-m %s "
529 "-serial file:%s/src_serial "
530 "%s %s %s %s",
531 machine_opts ? " -machine " : "",
532 machine_opts ? machine_opts : "",
533 memory_size, tmpfs,
534 arch_source, shmem_opts, args->opts_source,
535 ignore_stderr);
536 g_free(arch_source);
537 *from = qtest_init(cmd_source);
538 g_free(cmd_source);
540 cmd_target = g_strdup_printf("-accel kvm -accel tcg%s%s "
541 "-name target,debug-threads=on "
542 "-m %s "
543 "-serial file:%s/dest_serial "
544 "-incoming %s "
545 "%s %s %s %s",
546 machine_opts ? " -machine " : "",
547 machine_opts ? machine_opts : "",
548 memory_size, tmpfs, uri,
549 arch_target, shmem_opts,
550 args->opts_target, ignore_stderr);
551 g_free(arch_target);
552 *to = qtest_init(cmd_target);
553 g_free(cmd_target);
555 g_free(shmem_opts);
557 * Remove shmem file immediately to avoid memory leak in test failed case.
558 * It's valid becase QEMU has already opened this file
560 if (args->use_shmem) {
561 unlink(shmem_path);
562 g_free(shmem_path);
565 migrate_start_destroy(args);
566 return 0;
569 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
571 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
573 qtest_quit(from);
575 if (test_dest) {
576 qtest_memread(to, start_address, &dest_byte_a, 1);
578 /* Destination still running, wait for a byte to change */
579 do {
580 qtest_memread(to, start_address, &dest_byte_b, 1);
581 usleep(1000 * 10);
582 } while (dest_byte_a == dest_byte_b);
584 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
586 /* With it stopped, check nothing changes */
587 qtest_memread(to, start_address, &dest_byte_c, 1);
588 usleep(1000 * 200);
589 qtest_memread(to, start_address, &dest_byte_d, 1);
590 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
592 check_guests_ram(to);
595 qtest_quit(to);
597 cleanup("bootsect");
598 cleanup("migsocket");
599 cleanup("src_serial");
600 cleanup("dest_serial");
603 static void deprecated_set_downtime(QTestState *who, const double value)
605 QDict *rsp;
607 rsp = qtest_qmp(who,
608 "{ 'execute': 'migrate_set_downtime',"
609 " 'arguments': { 'value': %f } }", value);
610 g_assert(qdict_haskey(rsp, "return"));
611 qobject_unref(rsp);
612 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
615 static void deprecated_set_speed(QTestState *who, long long value)
617 QDict *rsp;
619 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
620 "'arguments': { 'value': %lld } }", value);
621 g_assert(qdict_haskey(rsp, "return"));
622 qobject_unref(rsp);
623 migrate_check_parameter_int(who, "max-bandwidth", value);
626 static void deprecated_set_cache_size(QTestState *who, long long value)
628 QDict *rsp;
630 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
631 "'arguments': { 'value': %lld } }", value);
632 g_assert(qdict_haskey(rsp, "return"));
633 qobject_unref(rsp);
634 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
637 static void test_deprecated(void)
639 QTestState *from;
641 from = qtest_init("-machine none");
643 deprecated_set_downtime(from, 0.12345);
644 deprecated_set_speed(from, 12345);
645 deprecated_set_cache_size(from, 4096);
647 qtest_quit(from);
650 static int migrate_postcopy_prepare(QTestState **from_ptr,
651 QTestState **to_ptr,
652 MigrateStart *args)
654 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, "{}");
676 g_free(uri);
678 wait_for_migration_pass(from);
680 *from_ptr = from;
681 *to_ptr = to;
683 return 0;
686 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
688 wait_for_migration_complete(from);
690 /* Make sure we get at least one "B" on destination */
691 wait_for_serial("dest_serial");
693 if (uffd_feature_thread_id) {
694 read_blocktime(to);
697 test_migrate_end(from, to, true);
700 static void test_postcopy(void)
702 MigrateStart *args = migrate_start_new();
703 QTestState *from, *to;
705 if (migrate_postcopy_prepare(&from, &to, args)) {
706 return;
708 migrate_postcopy_start(from, to);
709 migrate_postcopy_complete(from, to);
712 static void test_postcopy_recovery(void)
714 MigrateStart *args = migrate_start_new();
715 QTestState *from, *to;
716 char *uri;
718 args->hide_stderr = true;
720 if (migrate_postcopy_prepare(&from, &to, args)) {
721 return;
724 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
725 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
727 /* Now we start the postcopy */
728 migrate_postcopy_start(from, to);
731 * Wait until postcopy is really started; we can only run the
732 * migrate-pause command during a postcopy
734 wait_for_migration_status(from, "postcopy-active", NULL);
737 * Manually stop the postcopy migration. This emulates a network
738 * failure with the migration socket
740 migrate_pause(from);
743 * Wait for destination side to reach postcopy-paused state. The
744 * migrate-recover command can only succeed if destination machine
745 * is in the paused state
747 wait_for_migration_status(to, "postcopy-paused",
748 (const char * []) { "failed", "active",
749 "completed", NULL });
752 * Create a new socket to emulate a new channel that is different
753 * from the broken migration channel; tell the destination to
754 * listen to the new port
756 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
757 migrate_recover(to, uri);
760 * Try to rebuild the migration channel using the resume flag and
761 * the newly created channel
763 wait_for_migration_status(from, "postcopy-paused",
764 (const char * []) { "failed", "active",
765 "completed", NULL });
766 migrate_qmp(from, uri, "{'resume': true}");
767 g_free(uri);
769 /* Restore the postcopy bandwidth to unlimited */
770 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
772 migrate_postcopy_complete(from, to);
775 static void test_baddest(void)
777 MigrateStart *args = migrate_start_new();
778 QTestState *from, *to;
780 args->hide_stderr = true;
782 if (test_migrate_start(&from, &to, "tcp:0:0", args)) {
783 return;
785 migrate_qmp(from, "tcp:0:0", "{}");
786 wait_for_migration_fail(from, false);
787 test_migrate_end(from, to, false);
790 static void test_precopy_unix(void)
792 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
793 MigrateStart *args = migrate_start_new();
794 QTestState *from, *to;
796 if (test_migrate_start(&from, &to, uri, args)) {
797 return;
800 /* We want to pick a speed slow enough that the test completes
801 * quickly, but that it doesn't complete precopy even on a slow
802 * machine, so also set the downtime.
804 /* 1 ms should make it not converge*/
805 migrate_set_parameter_int(from, "downtime-limit", 1);
806 /* 1GB/s */
807 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
809 /* Wait for the first serial output from the source */
810 wait_for_serial("src_serial");
812 migrate_qmp(from, uri, "{}");
814 wait_for_migration_pass(from);
816 /* 300 ms should converge */
817 migrate_set_parameter_int(from, "downtime-limit", 300);
819 if (!got_stop) {
820 qtest_qmp_eventwait(from, "STOP");
823 qtest_qmp_eventwait(to, "RESUME");
825 wait_for_serial("dest_serial");
826 wait_for_migration_complete(from);
828 test_migrate_end(from, to, true);
829 g_free(uri);
832 #if 0
833 /* Currently upset on aarch64 TCG */
834 static void test_ignore_shared(void)
836 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
837 QTestState *from, *to;
839 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
840 return;
843 migrate_set_capability(from, "x-ignore-shared", true);
844 migrate_set_capability(to, "x-ignore-shared", true);
846 /* Wait for the first serial output from the source */
847 wait_for_serial("src_serial");
849 migrate_qmp(from, uri, "{}");
851 wait_for_migration_pass(from);
853 if (!got_stop) {
854 qtest_qmp_eventwait(from, "STOP");
857 qtest_qmp_eventwait(to, "RESUME");
859 wait_for_serial("dest_serial");
860 wait_for_migration_complete(from);
862 /* Check whether shared RAM has been really skipped */
863 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
865 test_migrate_end(from, to, true);
866 g_free(uri);
868 #endif
870 static void test_xbzrle(const char *uri)
872 MigrateStart *args = migrate_start_new();
873 QTestState *from, *to;
875 if (test_migrate_start(&from, &to, uri, args)) {
876 return;
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);
886 /* 1GB/s */
887 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
889 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
891 migrate_set_capability(from, "xbzrle", "true");
892 migrate_set_capability(to, "xbzrle", "true");
893 /* Wait for the first serial output from the source */
894 wait_for_serial("src_serial");
896 migrate_qmp(from, uri, "{}");
898 wait_for_migration_pass(from);
900 /* 300ms should converge */
901 migrate_set_parameter_int(from, "downtime-limit", 300);
903 if (!got_stop) {
904 qtest_qmp_eventwait(from, "STOP");
906 qtest_qmp_eventwait(to, "RESUME");
908 wait_for_serial("dest_serial");
909 wait_for_migration_complete(from);
911 test_migrate_end(from, to, true);
914 static void test_xbzrle_unix(void)
916 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
918 test_xbzrle(uri);
919 g_free(uri);
922 static void test_precopy_tcp(void)
924 MigrateStart *args = migrate_start_new();
925 char *uri;
926 QTestState *from, *to;
928 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", args)) {
929 return;
933 * We want to pick a speed slow enough that the test completes
934 * quickly, but that it doesn't complete precopy even on a slow
935 * machine, so also set the downtime.
937 /* 1 ms should make it not converge*/
938 migrate_set_parameter_int(from, "downtime-limit", 1);
939 /* 1GB/s */
940 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
942 /* Wait for the first serial output from the source */
943 wait_for_serial("src_serial");
945 uri = migrate_get_socket_address(to, "socket-address");
947 migrate_qmp(from, uri, "{}");
949 wait_for_migration_pass(from);
951 /* 300ms should converge */
952 migrate_set_parameter_int(from, "downtime-limit", 300);
954 if (!got_stop) {
955 qtest_qmp_eventwait(from, "STOP");
957 qtest_qmp_eventwait(to, "RESUME");
959 wait_for_serial("dest_serial");
960 wait_for_migration_complete(from);
962 test_migrate_end(from, to, true);
963 g_free(uri);
966 static void test_migrate_fd_proto(void)
968 MigrateStart *args = migrate_start_new();
969 QTestState *from, *to;
970 int ret;
971 int pair[2];
972 QDict *rsp;
973 const char *error_desc;
975 if (test_migrate_start(&from, &to, "defer", args)) {
976 return;
980 * We want to pick a speed slow enough that the test completes
981 * quickly, but that it doesn't complete precopy even on a slow
982 * machine, so also set the downtime.
984 /* 1 ms should make it not converge */
985 migrate_set_parameter_int(from, "downtime-limit", 1);
986 /* 1GB/s */
987 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
989 /* Wait for the first serial output from the source */
990 wait_for_serial("src_serial");
992 /* Create two connected sockets for migration */
993 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
994 g_assert_cmpint(ret, ==, 0);
996 /* Send the 1st socket to the target */
997 rsp = wait_command_fd(to, pair[0],
998 "{ 'execute': 'getfd',"
999 " 'arguments': { 'fdname': 'fd-mig' }}");
1000 qobject_unref(rsp);
1001 close(pair[0]);
1003 /* Start incoming migration from the 1st socket */
1004 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1005 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1006 qobject_unref(rsp);
1008 /* Send the 2nd socket to the target */
1009 rsp = wait_command_fd(from, pair[1],
1010 "{ 'execute': 'getfd',"
1011 " 'arguments': { 'fdname': 'fd-mig' }}");
1012 qobject_unref(rsp);
1013 close(pair[1]);
1015 /* Start migration to the 2nd socket*/
1016 migrate_qmp(from, "fd:fd-mig", "{}");
1018 wait_for_migration_pass(from);
1020 /* 300ms should converge */
1021 migrate_set_parameter_int(from, "downtime-limit", 300);
1023 if (!got_stop) {
1024 qtest_qmp_eventwait(from, "STOP");
1026 qtest_qmp_eventwait(to, "RESUME");
1028 /* Test closing fds */
1029 /* We assume, that QEMU removes named fd from its list,
1030 * so this should fail */
1031 rsp = qtest_qmp(from, "{ '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 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1039 " 'arguments': { 'fdname': 'fd-mig' }}");
1040 g_assert_true(qdict_haskey(rsp, "error"));
1041 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1042 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1043 qobject_unref(rsp);
1045 /* Complete migration */
1046 wait_for_serial("dest_serial");
1047 wait_for_migration_complete(from);
1048 test_migrate_end(from, to, true);
1051 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1053 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1054 QTestState *from, *to;
1056 if (test_migrate_start(&from, &to, uri, args)) {
1057 return;
1061 * UUID validation is at the begin of migration. So, the main process of
1062 * migration is not interesting for us here. Thus, set huge downtime for
1063 * very fast migration.
1065 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1066 migrate_set_capability(from, "validate-uuid", true);
1068 /* Wait for the first serial output from the source */
1069 wait_for_serial("src_serial");
1071 migrate_qmp(from, uri, "{}");
1073 if (should_fail) {
1074 qtest_set_expected_status(to, 1);
1075 wait_for_migration_fail(from, true);
1076 } else {
1077 wait_for_migration_complete(from);
1080 test_migrate_end(from, to, false);
1081 g_free(uri);
1084 static void test_validate_uuid(void)
1086 MigrateStart *args = migrate_start_new();
1088 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1089 args->opts_target = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1090 do_test_validate_uuid(args, false);
1093 static void test_validate_uuid_error(void)
1095 MigrateStart *args = migrate_start_new();
1097 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1098 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1099 args->hide_stderr = true;
1100 do_test_validate_uuid(args, true);
1103 static void test_validate_uuid_src_not_set(void)
1105 MigrateStart *args = migrate_start_new();
1107 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1108 args->hide_stderr = true;
1109 do_test_validate_uuid(args, false);
1112 static void test_validate_uuid_dst_not_set(void)
1114 MigrateStart *args = migrate_start_new();
1116 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1117 args->hide_stderr = true;
1118 do_test_validate_uuid(args, false);
1121 static void test_migrate_auto_converge(void)
1123 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1124 MigrateStart *args = migrate_start_new();
1125 QTestState *from, *to;
1126 int64_t remaining, percentage;
1129 * We want the test to be stable and as fast as possible.
1130 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1131 * so we need to decrease a bandwidth.
1133 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1134 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1135 const int64_t downtime_limit = 250; /* 250ms */
1137 * We migrate through unix-socket (> 500Mb/s).
1138 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1139 * So, we can predict expected_threshold
1141 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1143 if (test_migrate_start(&from, &to, uri, args)) {
1144 return;
1147 migrate_set_capability(from, "auto-converge", true);
1148 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1149 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1150 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1153 * Set the initial parameters so that the migration could not converge
1154 * without throttling.
1156 migrate_set_parameter_int(from, "downtime-limit", 1);
1157 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1159 /* To check remaining size after precopy */
1160 migrate_set_capability(from, "pause-before-switchover", true);
1162 /* Wait for the first serial output from the source */
1163 wait_for_serial("src_serial");
1165 migrate_qmp(from, uri, "{}");
1167 /* Wait for throttling begins */
1168 percentage = 0;
1169 while (percentage == 0) {
1170 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1171 usleep(100);
1172 g_assert_false(got_stop);
1174 /* The first percentage of throttling should be equal to init_pct */
1175 g_assert_cmpint(percentage, ==, init_pct);
1176 /* Now, when we tested that throttling works, let it converge */
1177 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1178 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1181 * Wait for pre-switchover status to check last throttle percentage
1182 * and remaining. These values will be zeroed later
1184 wait_for_migration_status(from, "pre-switchover", NULL);
1186 /* The final percentage of throttling shouldn't be greater than max_pct */
1187 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1188 g_assert_cmpint(percentage, <=, max_pct);
1190 remaining = read_ram_property_int(from, "remaining");
1191 g_assert_cmpint(remaining, <, expected_threshold);
1193 migrate_continue(from, "pre-switchover");
1195 qtest_qmp_eventwait(to, "RESUME");
1197 wait_for_serial("dest_serial");
1198 wait_for_migration_complete(from);
1200 g_free(uri);
1202 test_migrate_end(from, to, true);
1205 int main(int argc, char **argv)
1207 char template[] = "/tmp/migration-test-XXXXXX";
1208 int ret;
1210 g_test_init(&argc, &argv, NULL);
1212 if (!ufd_version_check()) {
1213 return g_test_run();
1217 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1218 * is touchy due to race conditions on dirty bits (especially on PPC for
1219 * some reason)
1221 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1222 (access("/sys/module/kvm_hv", F_OK) ||
1223 access("/dev/kvm", R_OK | W_OK))) {
1224 g_test_message("Skipping test: kvm_hv not available");
1225 return g_test_run();
1229 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1230 * there until the problems are resolved
1232 if (g_str_equal(qtest_get_arch(), "s390x")) {
1233 #if defined(HOST_S390X)
1234 if (access("/dev/kvm", R_OK | W_OK)) {
1235 g_test_message("Skipping test: kvm not available");
1236 return g_test_run();
1238 #else
1239 g_test_message("Skipping test: Need s390x host to work properly");
1240 return g_test_run();
1241 #endif
1244 tmpfs = mkdtemp(template);
1245 if (!tmpfs) {
1246 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1248 g_assert(tmpfs);
1250 module_call_init(MODULE_INIT_QOM);
1252 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1253 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1254 qtest_add_func("/migration/deprecated", test_deprecated);
1255 qtest_add_func("/migration/bad_dest", test_baddest);
1256 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1257 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1258 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1259 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1260 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1261 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1262 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1263 qtest_add_func("/migration/validate_uuid_src_not_set",
1264 test_validate_uuid_src_not_set);
1265 qtest_add_func("/migration/validate_uuid_dst_not_set",
1266 test_validate_uuid_dst_not_set);
1268 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1270 ret = g_test_run();
1272 g_assert_cmpint(ret, ==, 0);
1274 ret = rmdir(tmpfs);
1275 if (ret != 0) {
1276 g_test_message("unable to rmdir: path (%s): %s",
1277 tmpfs, strerror(errno));
1280 return ret;