target/arm/kvm: Implement virtual time adjustment
[qemu/ar7.git] / tests / qtest / migration-test.c
blobcf27ebbc9d3b1a63ab0b0a621f78feebfca7926d
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 char *migrate_get_parameter_str(QTestState *who,
360 const char *parameter)
362 QDict *rsp;
363 char *result;
365 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
366 result = g_strdup(qdict_get_str(rsp, parameter));
367 qobject_unref(rsp);
368 return result;
371 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
372 const char *value)
374 char *result;
376 result = migrate_get_parameter_str(who, parameter);
377 g_assert_cmpstr(result, ==, value);
378 g_free(result);
381 __attribute__((unused))
382 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
383 const char *value)
385 QDict *rsp;
387 rsp = qtest_qmp(who,
388 "{ 'execute': 'migrate-set-parameters',"
389 "'arguments': { %s: %s } }",
390 parameter, value);
391 g_assert(qdict_haskey(rsp, "return"));
392 qobject_unref(rsp);
393 migrate_check_parameter_str(who, parameter, value);
396 static void migrate_pause(QTestState *who)
398 QDict *rsp;
400 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
401 qobject_unref(rsp);
404 static void migrate_continue(QTestState *who, const char *state)
406 QDict *rsp;
408 rsp = wait_command(who,
409 "{ 'execute': 'migrate-continue',"
410 " 'arguments': { 'state': %s } }",
411 state);
412 qobject_unref(rsp);
415 static void migrate_recover(QTestState *who, const char *uri)
417 QDict *rsp;
419 rsp = wait_command(who,
420 "{ 'execute': 'migrate-recover', "
421 " 'id': 'recover-cmd', "
422 " 'arguments': { 'uri': %s } }",
423 uri);
424 qobject_unref(rsp);
427 static void migrate_cancel(QTestState *who)
429 QDict *rsp;
431 rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
432 qobject_unref(rsp);
435 static void migrate_set_capability(QTestState *who, const char *capability,
436 bool value)
438 QDict *rsp;
440 rsp = qtest_qmp(who,
441 "{ 'execute': 'migrate-set-capabilities',"
442 "'arguments': { "
443 "'capabilities': [ { "
444 "'capability': %s, 'state': %i } ] } }",
445 capability, value);
446 g_assert(qdict_haskey(rsp, "return"));
447 qobject_unref(rsp);
450 static void migrate_postcopy_start(QTestState *from, QTestState *to)
452 QDict *rsp;
454 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
455 qobject_unref(rsp);
457 if (!got_stop) {
458 qtest_qmp_eventwait(from, "STOP");
461 qtest_qmp_eventwait(to, "RESUME");
464 typedef struct {
465 bool hide_stderr;
466 bool use_shmem;
467 /* only launch the target process */
468 bool only_target;
469 char *opts_source;
470 char *opts_target;
471 } MigrateStart;
473 static MigrateStart *migrate_start_new(void)
475 MigrateStart *args = g_new0(MigrateStart, 1);
477 args->opts_source = g_strdup("");
478 args->opts_target = g_strdup("");
479 return args;
482 static void migrate_start_destroy(MigrateStart *args)
484 g_free(args->opts_source);
485 g_free(args->opts_target);
486 g_free(args);
489 static int test_migrate_start(QTestState **from, QTestState **to,
490 const char *uri, MigrateStart *args)
492 gchar *arch_source, *arch_target;
493 gchar *cmd_source, *cmd_target;
494 const gchar *ignore_stderr;
495 char *bootpath = NULL;
496 char *shmem_opts;
497 char *shmem_path;
498 const char *arch = qtest_get_arch();
499 const char *machine_opts = NULL;
500 const char *memory_size;
502 if (args->use_shmem) {
503 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
504 g_test_skip("/dev/shm is not supported");
505 return -1;
509 got_stop = false;
510 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
511 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
512 /* the assembled x86 boot sector should be exactly one sector large */
513 assert(sizeof(x86_bootsect) == 512);
514 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
515 memory_size = "150M";
516 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
517 arch_target = g_strdup(arch_source);
518 start_address = X86_TEST_MEM_START;
519 end_address = X86_TEST_MEM_END;
520 } else if (g_str_equal(arch, "s390x")) {
521 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
522 memory_size = "128M";
523 arch_source = g_strdup_printf("-bios %s", bootpath);
524 arch_target = g_strdup(arch_source);
525 start_address = S390_TEST_MEM_START;
526 end_address = S390_TEST_MEM_END;
527 } else if (strcmp(arch, "ppc64") == 0) {
528 machine_opts = "vsmt=8";
529 memory_size = "256M";
530 start_address = PPC_TEST_MEM_START;
531 end_address = PPC_TEST_MEM_END;
532 arch_source = g_strdup_printf("-nodefaults "
533 "-prom-env 'use-nvramrc?=true' -prom-env "
534 "'nvramrc=hex .\" _\" begin %x %x "
535 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
536 "until'", end_address, start_address);
537 arch_target = g_strdup("");
538 } else if (strcmp(arch, "aarch64") == 0) {
539 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
540 machine_opts = "virt,gic-version=max";
541 memory_size = "150M";
542 arch_source = g_strdup_printf("-cpu max "
543 "-kernel %s",
544 bootpath);
545 arch_target = g_strdup(arch_source);
546 start_address = ARM_TEST_MEM_START;
547 end_address = ARM_TEST_MEM_END;
549 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
550 } else {
551 g_assert_not_reached();
554 g_free(bootpath);
556 if (args->hide_stderr) {
557 ignore_stderr = "2>/dev/null";
558 } else {
559 ignore_stderr = "";
562 if (args->use_shmem) {
563 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
564 shmem_opts = g_strdup_printf(
565 "-object memory-backend-file,id=mem0,size=%s"
566 ",mem-path=%s,share=on -numa node,memdev=mem0",
567 memory_size, shmem_path);
568 } else {
569 shmem_path = NULL;
570 shmem_opts = g_strdup("");
573 cmd_source = g_strdup_printf("-accel kvm -accel tcg%s%s "
574 "-name source,debug-threads=on "
575 "-m %s "
576 "-serial file:%s/src_serial "
577 "%s %s %s %s",
578 machine_opts ? " -machine " : "",
579 machine_opts ? machine_opts : "",
580 memory_size, tmpfs,
581 arch_source, shmem_opts, args->opts_source,
582 ignore_stderr);
583 g_free(arch_source);
584 if (!args->only_target) {
585 *from = qtest_init(cmd_source);
587 g_free(cmd_source);
589 cmd_target = g_strdup_printf("-accel kvm -accel tcg%s%s "
590 "-name target,debug-threads=on "
591 "-m %s "
592 "-serial file:%s/dest_serial "
593 "-incoming %s "
594 "%s %s %s %s",
595 machine_opts ? " -machine " : "",
596 machine_opts ? machine_opts : "",
597 memory_size, tmpfs, uri,
598 arch_target, shmem_opts,
599 args->opts_target, ignore_stderr);
600 g_free(arch_target);
601 *to = qtest_init(cmd_target);
602 g_free(cmd_target);
604 g_free(shmem_opts);
606 * Remove shmem file immediately to avoid memory leak in test failed case.
607 * It's valid becase QEMU has already opened this file
609 if (args->use_shmem) {
610 unlink(shmem_path);
611 g_free(shmem_path);
614 migrate_start_destroy(args);
615 return 0;
618 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
620 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
622 qtest_quit(from);
624 if (test_dest) {
625 qtest_memread(to, start_address, &dest_byte_a, 1);
627 /* Destination still running, wait for a byte to change */
628 do {
629 qtest_memread(to, start_address, &dest_byte_b, 1);
630 usleep(1000 * 10);
631 } while (dest_byte_a == dest_byte_b);
633 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
635 /* With it stopped, check nothing changes */
636 qtest_memread(to, start_address, &dest_byte_c, 1);
637 usleep(1000 * 200);
638 qtest_memread(to, start_address, &dest_byte_d, 1);
639 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
641 check_guests_ram(to);
644 qtest_quit(to);
646 cleanup("bootsect");
647 cleanup("migsocket");
648 cleanup("src_serial");
649 cleanup("dest_serial");
652 static void deprecated_set_downtime(QTestState *who, const double value)
654 QDict *rsp;
656 rsp = qtest_qmp(who,
657 "{ 'execute': 'migrate_set_downtime',"
658 " 'arguments': { 'value': %f } }", value);
659 g_assert(qdict_haskey(rsp, "return"));
660 qobject_unref(rsp);
661 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
664 static void deprecated_set_speed(QTestState *who, long long value)
666 QDict *rsp;
668 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
669 "'arguments': { 'value': %lld } }", value);
670 g_assert(qdict_haskey(rsp, "return"));
671 qobject_unref(rsp);
672 migrate_check_parameter_int(who, "max-bandwidth", value);
675 static void deprecated_set_cache_size(QTestState *who, long long value)
677 QDict *rsp;
679 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
680 "'arguments': { 'value': %lld } }", value);
681 g_assert(qdict_haskey(rsp, "return"));
682 qobject_unref(rsp);
683 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
686 static void test_deprecated(void)
688 QTestState *from;
690 from = qtest_init("-machine none");
692 deprecated_set_downtime(from, 0.12345);
693 deprecated_set_speed(from, 12345);
694 deprecated_set_cache_size(from, 4096);
696 qtest_quit(from);
699 static int migrate_postcopy_prepare(QTestState **from_ptr,
700 QTestState **to_ptr,
701 MigrateStart *args)
703 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
704 QTestState *from, *to;
706 if (test_migrate_start(&from, &to, uri, args)) {
707 return -1;
710 migrate_set_capability(from, "postcopy-ram", true);
711 migrate_set_capability(to, "postcopy-ram", true);
712 migrate_set_capability(to, "postcopy-blocktime", true);
714 /* We want to pick a speed slow enough that the test completes
715 * quickly, but that it doesn't complete precopy even on a slow
716 * machine, so also set the downtime.
718 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
719 migrate_set_parameter_int(from, "downtime-limit", 1);
721 /* Wait for the first serial output from the source */
722 wait_for_serial("src_serial");
724 migrate_qmp(from, uri, "{}");
725 g_free(uri);
727 wait_for_migration_pass(from);
729 *from_ptr = from;
730 *to_ptr = to;
732 return 0;
735 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
737 wait_for_migration_complete(from);
739 /* Make sure we get at least one "B" on destination */
740 wait_for_serial("dest_serial");
742 if (uffd_feature_thread_id) {
743 read_blocktime(to);
746 test_migrate_end(from, to, true);
749 static void test_postcopy(void)
751 MigrateStart *args = migrate_start_new();
752 QTestState *from, *to;
754 if (migrate_postcopy_prepare(&from, &to, args)) {
755 return;
757 migrate_postcopy_start(from, to);
758 migrate_postcopy_complete(from, to);
761 static void test_postcopy_recovery(void)
763 MigrateStart *args = migrate_start_new();
764 QTestState *from, *to;
765 char *uri;
767 args->hide_stderr = true;
769 if (migrate_postcopy_prepare(&from, &to, args)) {
770 return;
773 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
774 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
776 /* Now we start the postcopy */
777 migrate_postcopy_start(from, to);
780 * Wait until postcopy is really started; we can only run the
781 * migrate-pause command during a postcopy
783 wait_for_migration_status(from, "postcopy-active", NULL);
786 * Manually stop the postcopy migration. This emulates a network
787 * failure with the migration socket
789 migrate_pause(from);
792 * Wait for destination side to reach postcopy-paused state. The
793 * migrate-recover command can only succeed if destination machine
794 * is in the paused state
796 wait_for_migration_status(to, "postcopy-paused",
797 (const char * []) { "failed", "active",
798 "completed", NULL });
801 * Create a new socket to emulate a new channel that is different
802 * from the broken migration channel; tell the destination to
803 * listen to the new port
805 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
806 migrate_recover(to, uri);
809 * Try to rebuild the migration channel using the resume flag and
810 * the newly created channel
812 wait_for_migration_status(from, "postcopy-paused",
813 (const char * []) { "failed", "active",
814 "completed", NULL });
815 migrate_qmp(from, uri, "{'resume': true}");
816 g_free(uri);
818 /* Restore the postcopy bandwidth to unlimited */
819 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
821 migrate_postcopy_complete(from, to);
824 static void test_baddest(void)
826 MigrateStart *args = migrate_start_new();
827 QTestState *from, *to;
829 args->hide_stderr = true;
831 if (test_migrate_start(&from, &to, "tcp:0:0", args)) {
832 return;
834 migrate_qmp(from, "tcp:0:0", "{}");
835 wait_for_migration_fail(from, false);
836 test_migrate_end(from, to, false);
839 static void test_precopy_unix(void)
841 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
842 MigrateStart *args = migrate_start_new();
843 QTestState *from, *to;
845 if (test_migrate_start(&from, &to, uri, args)) {
846 return;
849 /* We want to pick a speed slow enough that the test completes
850 * quickly, but that it doesn't complete precopy even on a slow
851 * machine, so also set the downtime.
853 /* 1 ms should make it not converge*/
854 migrate_set_parameter_int(from, "downtime-limit", 1);
855 /* 1GB/s */
856 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
858 /* Wait for the first serial output from the source */
859 wait_for_serial("src_serial");
861 migrate_qmp(from, uri, "{}");
863 wait_for_migration_pass(from);
865 /* 300 ms should converge */
866 migrate_set_parameter_int(from, "downtime-limit", 300);
868 if (!got_stop) {
869 qtest_qmp_eventwait(from, "STOP");
872 qtest_qmp_eventwait(to, "RESUME");
874 wait_for_serial("dest_serial");
875 wait_for_migration_complete(from);
877 test_migrate_end(from, to, true);
878 g_free(uri);
881 #if 0
882 /* Currently upset on aarch64 TCG */
883 static void test_ignore_shared(void)
885 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
886 QTestState *from, *to;
888 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
889 return;
892 migrate_set_capability(from, "x-ignore-shared", true);
893 migrate_set_capability(to, "x-ignore-shared", true);
895 /* Wait for the first serial output from the source */
896 wait_for_serial("src_serial");
898 migrate_qmp(from, uri, "{}");
900 wait_for_migration_pass(from);
902 if (!got_stop) {
903 qtest_qmp_eventwait(from, "STOP");
906 qtest_qmp_eventwait(to, "RESUME");
908 wait_for_serial("dest_serial");
909 wait_for_migration_complete(from);
911 /* Check whether shared RAM has been really skipped */
912 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
914 test_migrate_end(from, to, true);
915 g_free(uri);
917 #endif
919 static void test_xbzrle(const char *uri)
921 MigrateStart *args = migrate_start_new();
922 QTestState *from, *to;
924 if (test_migrate_start(&from, &to, uri, 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 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
940 migrate_set_capability(from, "xbzrle", "true");
941 migrate_set_capability(to, "xbzrle", "true");
942 /* Wait for the first serial output from the source */
943 wait_for_serial("src_serial");
945 migrate_qmp(from, uri, "{}");
947 wait_for_migration_pass(from);
949 /* 300ms should converge */
950 migrate_set_parameter_int(from, "downtime-limit", 300);
952 if (!got_stop) {
953 qtest_qmp_eventwait(from, "STOP");
955 qtest_qmp_eventwait(to, "RESUME");
957 wait_for_serial("dest_serial");
958 wait_for_migration_complete(from);
960 test_migrate_end(from, to, true);
963 static void test_xbzrle_unix(void)
965 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
967 test_xbzrle(uri);
968 g_free(uri);
971 static void test_precopy_tcp(void)
973 MigrateStart *args = migrate_start_new();
974 char *uri;
975 QTestState *from, *to;
977 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", args)) {
978 return;
982 * We want to pick a speed slow enough that the test completes
983 * quickly, but that it doesn't complete precopy even on a slow
984 * machine, so also set the downtime.
986 /* 1 ms should make it not converge*/
987 migrate_set_parameter_int(from, "downtime-limit", 1);
988 /* 1GB/s */
989 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
991 /* Wait for the first serial output from the source */
992 wait_for_serial("src_serial");
994 uri = migrate_get_socket_address(to, "socket-address");
996 migrate_qmp(from, uri, "{}");
998 wait_for_migration_pass(from);
1000 /* 300ms should converge */
1001 migrate_set_parameter_int(from, "downtime-limit", 300);
1003 if (!got_stop) {
1004 qtest_qmp_eventwait(from, "STOP");
1006 qtest_qmp_eventwait(to, "RESUME");
1008 wait_for_serial("dest_serial");
1009 wait_for_migration_complete(from);
1011 test_migrate_end(from, to, true);
1012 g_free(uri);
1015 static void test_migrate_fd_proto(void)
1017 MigrateStart *args = migrate_start_new();
1018 QTestState *from, *to;
1019 int ret;
1020 int pair[2];
1021 QDict *rsp;
1022 const char *error_desc;
1024 if (test_migrate_start(&from, &to, "defer", args)) {
1025 return;
1029 * We want to pick a speed slow enough that the test completes
1030 * quickly, but that it doesn't complete precopy even on a slow
1031 * machine, so also set the downtime.
1033 /* 1 ms should make it not converge */
1034 migrate_set_parameter_int(from, "downtime-limit", 1);
1035 /* 1GB/s */
1036 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1038 /* Wait for the first serial output from the source */
1039 wait_for_serial("src_serial");
1041 /* Create two connected sockets for migration */
1042 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1043 g_assert_cmpint(ret, ==, 0);
1045 /* Send the 1st socket to the target */
1046 rsp = wait_command_fd(to, pair[0],
1047 "{ 'execute': 'getfd',"
1048 " 'arguments': { 'fdname': 'fd-mig' }}");
1049 qobject_unref(rsp);
1050 close(pair[0]);
1052 /* Start incoming migration from the 1st socket */
1053 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1054 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1055 qobject_unref(rsp);
1057 /* Send the 2nd socket to the target */
1058 rsp = wait_command_fd(from, pair[1],
1059 "{ 'execute': 'getfd',"
1060 " 'arguments': { 'fdname': 'fd-mig' }}");
1061 qobject_unref(rsp);
1062 close(pair[1]);
1064 /* Start migration to the 2nd socket*/
1065 migrate_qmp(from, "fd:fd-mig", "{}");
1067 wait_for_migration_pass(from);
1069 /* 300ms should converge */
1070 migrate_set_parameter_int(from, "downtime-limit", 300);
1072 if (!got_stop) {
1073 qtest_qmp_eventwait(from, "STOP");
1075 qtest_qmp_eventwait(to, "RESUME");
1077 /* Test closing fds */
1078 /* We assume, that QEMU removes named fd from its list,
1079 * so this should fail */
1080 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1081 " 'arguments': { 'fdname': 'fd-mig' }}");
1082 g_assert_true(qdict_haskey(rsp, "error"));
1083 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1084 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1085 qobject_unref(rsp);
1087 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1088 " 'arguments': { 'fdname': 'fd-mig' }}");
1089 g_assert_true(qdict_haskey(rsp, "error"));
1090 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1091 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1092 qobject_unref(rsp);
1094 /* Complete migration */
1095 wait_for_serial("dest_serial");
1096 wait_for_migration_complete(from);
1097 test_migrate_end(from, to, true);
1100 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1102 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1103 QTestState *from, *to;
1105 if (test_migrate_start(&from, &to, uri, args)) {
1106 return;
1110 * UUID validation is at the begin of migration. So, the main process of
1111 * migration is not interesting for us here. Thus, set huge downtime for
1112 * very fast migration.
1114 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1115 migrate_set_capability(from, "validate-uuid", true);
1117 /* Wait for the first serial output from the source */
1118 wait_for_serial("src_serial");
1120 migrate_qmp(from, uri, "{}");
1122 if (should_fail) {
1123 qtest_set_expected_status(to, 1);
1124 wait_for_migration_fail(from, true);
1125 } else {
1126 wait_for_migration_complete(from);
1129 test_migrate_end(from, to, false);
1130 g_free(uri);
1133 static void test_validate_uuid(void)
1135 MigrateStart *args = migrate_start_new();
1137 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1138 args->opts_target = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1139 do_test_validate_uuid(args, false);
1142 static void test_validate_uuid_error(void)
1144 MigrateStart *args = migrate_start_new();
1146 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1147 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1148 args->hide_stderr = true;
1149 do_test_validate_uuid(args, true);
1152 static void test_validate_uuid_src_not_set(void)
1154 MigrateStart *args = migrate_start_new();
1156 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1157 args->hide_stderr = true;
1158 do_test_validate_uuid(args, false);
1161 static void test_validate_uuid_dst_not_set(void)
1163 MigrateStart *args = migrate_start_new();
1165 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1166 args->hide_stderr = true;
1167 do_test_validate_uuid(args, false);
1170 static void test_migrate_auto_converge(void)
1172 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1173 MigrateStart *args = migrate_start_new();
1174 QTestState *from, *to;
1175 int64_t remaining, percentage;
1178 * We want the test to be stable and as fast as possible.
1179 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1180 * so we need to decrease a bandwidth.
1182 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1183 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1184 const int64_t downtime_limit = 250; /* 250ms */
1186 * We migrate through unix-socket (> 500Mb/s).
1187 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1188 * So, we can predict expected_threshold
1190 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1192 if (test_migrate_start(&from, &to, uri, args)) {
1193 return;
1196 migrate_set_capability(from, "auto-converge", true);
1197 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1198 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1199 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1202 * Set the initial parameters so that the migration could not converge
1203 * without throttling.
1205 migrate_set_parameter_int(from, "downtime-limit", 1);
1206 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1208 /* To check remaining size after precopy */
1209 migrate_set_capability(from, "pause-before-switchover", true);
1211 /* Wait for the first serial output from the source */
1212 wait_for_serial("src_serial");
1214 migrate_qmp(from, uri, "{}");
1216 /* Wait for throttling begins */
1217 percentage = 0;
1218 while (percentage == 0) {
1219 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1220 usleep(100);
1221 g_assert_false(got_stop);
1223 /* The first percentage of throttling should be equal to init_pct */
1224 g_assert_cmpint(percentage, ==, init_pct);
1225 /* Now, when we tested that throttling works, let it converge */
1226 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1227 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1230 * Wait for pre-switchover status to check last throttle percentage
1231 * and remaining. These values will be zeroed later
1233 wait_for_migration_status(from, "pre-switchover", NULL);
1235 /* The final percentage of throttling shouldn't be greater than max_pct */
1236 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1237 g_assert_cmpint(percentage, <=, max_pct);
1239 remaining = read_ram_property_int(from, "remaining");
1240 g_assert_cmpint(remaining, <, expected_threshold);
1242 migrate_continue(from, "pre-switchover");
1244 qtest_qmp_eventwait(to, "RESUME");
1246 wait_for_serial("dest_serial");
1247 wait_for_migration_complete(from);
1249 g_free(uri);
1251 test_migrate_end(from, to, true);
1254 static void test_multifd_tcp(void)
1256 MigrateStart *args = migrate_start_new();
1257 QTestState *from, *to;
1258 QDict *rsp;
1259 char *uri;
1261 if (test_migrate_start(&from, &to, "defer", args)) {
1262 return;
1266 * We want to pick a speed slow enough that the test completes
1267 * quickly, but that it doesn't complete precopy even on a slow
1268 * machine, so also set the downtime.
1270 /* 1 ms should make it not converge*/
1271 migrate_set_parameter_int(from, "downtime-limit", 1);
1272 /* 1GB/s */
1273 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1275 migrate_set_parameter_int(from, "multifd-channels", 16);
1276 migrate_set_parameter_int(to, "multifd-channels", 16);
1278 migrate_set_capability(from, "multifd", "true");
1279 migrate_set_capability(to, "multifd", "true");
1281 /* Start incoming migration from the 1st socket */
1282 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1283 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1284 qobject_unref(rsp);
1286 /* Wait for the first serial output from the source */
1287 wait_for_serial("src_serial");
1289 uri = migrate_get_socket_address(to, "socket-address");
1291 migrate_qmp(from, uri, "{}");
1293 wait_for_migration_pass(from);
1295 /* 300ms it should converge */
1296 migrate_set_parameter_int(from, "downtime-limit", 300);
1298 if (!got_stop) {
1299 qtest_qmp_eventwait(from, "STOP");
1301 qtest_qmp_eventwait(to, "RESUME");
1303 wait_for_serial("dest_serial");
1304 wait_for_migration_complete(from);
1305 test_migrate_end(from, to, true);
1306 g_free(uri);
1310 * This test does:
1311 * source target
1312 * migrate_incoming
1313 * migrate
1314 * migrate_cancel
1315 * launch another target
1316 * migrate
1318 * And see that it works
1321 static void test_multifd_tcp_cancel(void)
1323 MigrateStart *args = migrate_start_new();
1324 QTestState *from, *to, *to2;
1325 QDict *rsp;
1326 char *uri;
1328 args->hide_stderr = true;
1330 if (test_migrate_start(&from, &to, "defer", args)) {
1331 return;
1335 * We want to pick a speed slow enough that the test completes
1336 * quickly, but that it doesn't complete precopy even on a slow
1337 * machine, so also set the downtime.
1339 /* 1 ms should make it not converge*/
1340 migrate_set_parameter_int(from, "downtime-limit", 1);
1341 /* 300MB/s */
1342 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
1344 migrate_set_parameter_int(from, "multifd-channels", 16);
1345 migrate_set_parameter_int(to, "multifd-channels", 16);
1347 migrate_set_capability(from, "multifd", "true");
1348 migrate_set_capability(to, "multifd", "true");
1350 /* Start incoming migration from the 1st socket */
1351 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1352 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1353 qobject_unref(rsp);
1355 /* Wait for the first serial output from the source */
1356 wait_for_serial("src_serial");
1358 uri = migrate_get_socket_address(to, "socket-address");
1360 migrate_qmp(from, uri, "{}");
1362 wait_for_migration_pass(from);
1364 migrate_cancel(from);
1366 args = migrate_start_new();
1367 args->only_target = true;
1369 if (test_migrate_start(&from, &to2, "defer", args)) {
1370 return;
1373 migrate_set_parameter_int(to2, "multifd-channels", 16);
1375 migrate_set_capability(to2, "multifd", "true");
1377 /* Start incoming migration from the 1st socket */
1378 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
1379 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1380 qobject_unref(rsp);
1382 uri = migrate_get_socket_address(to2, "socket-address");
1384 wait_for_migration_status(from, "cancelled", NULL);
1386 /* 300ms it should converge */
1387 migrate_set_parameter_int(from, "downtime-limit", 300);
1388 /* 1GB/s */
1389 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1391 migrate_qmp(from, uri, "{}");
1393 wait_for_migration_pass(from);
1395 if (!got_stop) {
1396 qtest_qmp_eventwait(from, "STOP");
1398 qtest_qmp_eventwait(to2, "RESUME");
1400 wait_for_serial("dest_serial");
1401 wait_for_migration_complete(from);
1402 test_migrate_end(from, to2, true);
1403 g_free(uri);
1406 int main(int argc, char **argv)
1408 char template[] = "/tmp/migration-test-XXXXXX";
1409 int ret;
1411 g_test_init(&argc, &argv, NULL);
1413 if (!ufd_version_check()) {
1414 return g_test_run();
1418 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1419 * is touchy due to race conditions on dirty bits (especially on PPC for
1420 * some reason)
1422 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1423 (access("/sys/module/kvm_hv", F_OK) ||
1424 access("/dev/kvm", R_OK | W_OK))) {
1425 g_test_message("Skipping test: kvm_hv not available");
1426 return g_test_run();
1430 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1431 * there until the problems are resolved
1433 if (g_str_equal(qtest_get_arch(), "s390x")) {
1434 #if defined(HOST_S390X)
1435 if (access("/dev/kvm", R_OK | W_OK)) {
1436 g_test_message("Skipping test: kvm not available");
1437 return g_test_run();
1439 #else
1440 g_test_message("Skipping test: Need s390x host to work properly");
1441 return g_test_run();
1442 #endif
1445 tmpfs = mkdtemp(template);
1446 if (!tmpfs) {
1447 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1449 g_assert(tmpfs);
1451 module_call_init(MODULE_INIT_QOM);
1453 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1454 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1455 qtest_add_func("/migration/deprecated", test_deprecated);
1456 qtest_add_func("/migration/bad_dest", test_baddest);
1457 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1458 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1459 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1460 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1461 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1462 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1463 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1464 qtest_add_func("/migration/validate_uuid_src_not_set",
1465 test_validate_uuid_src_not_set);
1466 qtest_add_func("/migration/validate_uuid_dst_not_set",
1467 test_validate_uuid_dst_not_set);
1469 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1470 qtest_add_func("/migration/multifd/tcp", test_multifd_tcp);
1471 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel);
1473 ret = g_test_run();
1475 g_assert_cmpint(ret, ==, 0);
1477 ret = rmdir(tmpfs);
1478 if (ret != 0) {
1479 g_test_message("unable to rmdir: path (%s): %s",
1480 tmpfs, strerror(errno));
1483 return ret;