cpu: Introduce CPUNegativeOffsetState
[qemu/ar7.git] / tests / migration-test.c
blobe0407576cb10808254c953ca6041d35e7cf17401
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 "qapi/qmp/qjson.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "sysemu/sysemu.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
27 #include "migration/migration-test.h"
29 /* TODO actually test the results and get rid of this */
30 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
32 unsigned start_address;
33 unsigned end_address;
34 bool got_stop;
35 static bool uffd_feature_thread_id;
37 #if defined(__linux__)
38 #include <sys/syscall.h>
39 #include <sys/vfs.h>
40 #endif
42 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
43 #include <sys/eventfd.h>
44 #include <sys/ioctl.h>
45 #include <linux/userfaultfd.h>
47 static bool ufd_version_check(void)
49 struct uffdio_api api_struct;
50 uint64_t ioctl_mask;
52 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
54 if (ufd == -1) {
55 g_test_message("Skipping test: userfaultfd not available");
56 return false;
59 api_struct.api = UFFD_API;
60 api_struct.features = 0;
61 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
62 g_test_message("Skipping test: UFFDIO_API failed");
63 return false;
65 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
67 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
68 (__u64)1 << _UFFDIO_UNREGISTER;
69 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
70 g_test_message("Skipping test: Missing userfault feature");
71 return false;
74 return true;
77 #else
78 static bool ufd_version_check(void)
80 g_test_message("Skipping test: Userfault not available (builtdtime)");
81 return false;
84 #endif
86 static const char *tmpfs;
88 /* The boot file modifies memory area in [start_address, end_address)
89 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
91 #include "tests/migration/i386/a-b-bootblock.h"
92 #include "tests/migration/aarch64/a-b-kernel.h"
94 static void init_bootfile(const char *bootpath, void *content)
96 FILE *bootfile = fopen(bootpath, "wb");
98 g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1);
99 fclose(bootfile);
102 #include "tests/migration/s390x/a-b-bios.h"
104 static void init_bootfile_s390x(const char *bootpath)
106 FILE *bootfile = fopen(bootpath, "wb");
107 size_t len = sizeof(s390x_elf);
109 g_assert_cmpint(fwrite(s390x_elf, len, 1, bootfile), ==, 1);
110 fclose(bootfile);
114 * Wait for some output in the serial output file,
115 * we get an 'A' followed by an endless string of 'B's
116 * but on the destination we won't have the A.
118 static void wait_for_serial(const char *side)
120 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
121 FILE *serialfile = fopen(serialpath, "r");
122 const char *arch = qtest_get_arch();
123 int started = (strcmp(side, "src_serial") == 0 &&
124 strcmp(arch, "ppc64") == 0) ? 0 : 1;
126 g_free(serialpath);
127 do {
128 int readvalue = fgetc(serialfile);
130 if (!started) {
131 /* SLOF prints its banner before starting test,
132 * to ignore it, mark the start of the test with '_',
133 * ignore all characters until this marker
135 switch (readvalue) {
136 case '_':
137 started = 1;
138 break;
139 case EOF:
140 fseek(serialfile, 0, SEEK_SET);
141 usleep(1000);
142 break;
144 continue;
146 switch (readvalue) {
147 case 'A':
148 /* Fine */
149 break;
151 case 'B':
152 /* It's alive! */
153 fclose(serialfile);
154 return;
156 case EOF:
157 started = (strcmp(side, "src_serial") == 0 &&
158 strcmp(arch, "ppc64") == 0) ? 0 : 1;
159 fseek(serialfile, 0, SEEK_SET);
160 usleep(1000);
161 break;
163 default:
164 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
165 g_assert_not_reached();
167 } while (true);
170 static void stop_cb(void *opaque, const char *name, QDict *data)
172 if (!strcmp(name, "STOP")) {
173 got_stop = true;
178 * Events can get in the way of responses we are actually waiting for.
180 GCC_FMT_ATTR(3, 4)
181 static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
183 va_list ap;
185 va_start(ap, command);
186 qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
187 va_end(ap);
189 return qtest_qmp_receive_success(who, stop_cb, NULL);
193 * Events can get in the way of responses we are actually waiting for.
195 GCC_FMT_ATTR(2, 3)
196 static QDict *wait_command(QTestState *who, const char *command, ...)
198 va_list ap;
200 va_start(ap, command);
201 qtest_qmp_vsend(who, command, ap);
202 va_end(ap);
204 return qtest_qmp_receive_success(who, stop_cb, NULL);
208 * Note: caller is responsible to free the returned object via
209 * qobject_unref() after use
211 static QDict *migrate_query(QTestState *who)
213 return wait_command(who, "{ 'execute': 'query-migrate' }");
217 * Note: caller is responsible to free the returned object via
218 * g_free() after use
220 static gchar *migrate_query_status(QTestState *who)
222 QDict *rsp_return = migrate_query(who);
223 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
225 g_assert(status);
226 qobject_unref(rsp_return);
228 return status;
232 * It's tricky to use qemu's migration event capability with qtest,
233 * events suddenly appearing confuse the qmp()/hmp() responses.
236 static int64_t read_ram_property_int(QTestState *who, const char *property)
238 QDict *rsp_return, *rsp_ram;
239 int64_t result;
241 rsp_return = migrate_query(who);
242 if (!qdict_haskey(rsp_return, "ram")) {
243 /* Still in setup */
244 result = 0;
245 } else {
246 rsp_ram = qdict_get_qdict(rsp_return, "ram");
247 result = qdict_get_try_int(rsp_ram, property, 0);
249 qobject_unref(rsp_return);
250 return result;
253 static uint64_t get_migration_pass(QTestState *who)
255 return read_ram_property_int(who, "dirty-sync-count");
258 static void read_blocktime(QTestState *who)
260 QDict *rsp_return;
262 rsp_return = migrate_query(who);
263 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
264 qobject_unref(rsp_return);
267 static void wait_for_migration_status(QTestState *who,
268 const char *goal)
270 while (true) {
271 bool completed;
272 char *status;
274 status = migrate_query_status(who);
275 completed = strcmp(status, goal) == 0;
276 g_assert_cmpstr(status, !=, "failed");
277 g_free(status);
278 if (completed) {
279 return;
281 usleep(1000);
285 static void wait_for_migration_complete(QTestState *who)
287 wait_for_migration_status(who, "completed");
290 static void wait_for_migration_pass(QTestState *who)
292 uint64_t initial_pass = get_migration_pass(who);
293 uint64_t pass;
295 /* Wait for the 1st sync */
296 while (!got_stop && !initial_pass) {
297 usleep(1000);
298 initial_pass = get_migration_pass(who);
301 do {
302 usleep(1000);
303 pass = get_migration_pass(who);
304 } while (pass == initial_pass && !got_stop);
307 static void check_guests_ram(QTestState *who)
309 /* Our ASM test will have been incrementing one byte from each page from
310 * start_address to < end_address in order. This gives us a constraint
311 * that any page's byte should be equal or less than the previous pages
312 * byte (mod 256); and they should all be equal except for one transition
313 * at the point where we meet the incrementer. (We're running this with
314 * the guest stopped).
316 unsigned address;
317 uint8_t first_byte;
318 uint8_t last_byte;
319 bool hit_edge = false;
320 bool bad = false;
322 qtest_memread(who, start_address, &first_byte, 1);
323 last_byte = first_byte;
325 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
326 address += TEST_MEM_PAGE_SIZE)
328 uint8_t b;
329 qtest_memread(who, address, &b, 1);
330 if (b != last_byte) {
331 if (((b + 1) % 256) == last_byte && !hit_edge) {
332 /* This is OK, the guest stopped at the point of
333 * incrementing the previous page but didn't get
334 * to us yet.
336 hit_edge = true;
337 last_byte = b;
338 } else {
339 fprintf(stderr, "Memory content inconsistency at %x"
340 " first_byte = %x last_byte = %x current = %x"
341 " hit_edge = %x\n",
342 address, first_byte, last_byte, b, hit_edge);
343 bad = true;
347 g_assert_false(bad);
350 static void cleanup(const char *filename)
352 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
354 unlink(path);
355 g_free(path);
358 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
360 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
361 ",mem-path=%s,share=on -numa node,memdev=mem0",
362 mem_size, shmem_path);
365 static char *SocketAddress_to_str(SocketAddress *addr)
367 switch (addr->type) {
368 case SOCKET_ADDRESS_TYPE_INET:
369 return g_strdup_printf("tcp:%s:%s",
370 addr->u.inet.host,
371 addr->u.inet.port);
372 case SOCKET_ADDRESS_TYPE_UNIX:
373 return g_strdup_printf("unix:%s",
374 addr->u.q_unix.path);
375 case SOCKET_ADDRESS_TYPE_FD:
376 return g_strdup_printf("fd:%s", addr->u.fd.str);
377 case SOCKET_ADDRESS_TYPE_VSOCK:
378 return g_strdup_printf("tcp:%s:%s",
379 addr->u.vsock.cid,
380 addr->u.vsock.port);
381 default:
382 return g_strdup("unknown address type");
386 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
388 QDict *rsp;
389 char *result;
390 Error *local_err = NULL;
391 SocketAddressList *addrs;
392 Visitor *iv = NULL;
393 QObject *object;
395 rsp = migrate_query(who);
396 object = qdict_get(rsp, parameter);
398 iv = qobject_input_visitor_new(object);
399 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
400 visit_free(iv);
402 /* we are only using a single address */
403 result = SocketAddress_to_str(addrs->value);
405 qapi_free_SocketAddressList(addrs);
406 qobject_unref(rsp);
407 return result;
410 static long long migrate_get_parameter(QTestState *who, const char *parameter)
412 QDict *rsp;
413 long long result;
415 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
416 result = qdict_get_int(rsp, parameter);
417 qobject_unref(rsp);
418 return result;
421 static void migrate_check_parameter(QTestState *who, const char *parameter,
422 long long value)
424 long long result;
426 result = migrate_get_parameter(who, parameter);
427 g_assert_cmpint(result, ==, value);
430 static void migrate_set_parameter(QTestState *who, const char *parameter,
431 long long value)
433 QDict *rsp;
435 rsp = qtest_qmp(who,
436 "{ 'execute': 'migrate-set-parameters',"
437 "'arguments': { %s: %lld } }",
438 parameter, value);
439 g_assert(qdict_haskey(rsp, "return"));
440 qobject_unref(rsp);
441 migrate_check_parameter(who, parameter, value);
444 static void migrate_pause(QTestState *who)
446 QDict *rsp;
448 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
449 qobject_unref(rsp);
452 static void migrate_recover(QTestState *who, const char *uri)
454 QDict *rsp;
456 rsp = wait_command(who,
457 "{ 'execute': 'migrate-recover', "
458 " 'id': 'recover-cmd', "
459 " 'arguments': { 'uri': %s } }",
460 uri);
461 qobject_unref(rsp);
464 static void migrate_set_capability(QTestState *who, const char *capability,
465 bool value)
467 QDict *rsp;
469 rsp = qtest_qmp(who,
470 "{ 'execute': 'migrate-set-capabilities',"
471 "'arguments': { "
472 "'capabilities': [ { "
473 "'capability': %s, 'state': %i } ] } }",
474 capability, value);
475 g_assert(qdict_haskey(rsp, "return"));
476 qobject_unref(rsp);
480 * Send QMP command "migrate".
481 * Arguments are built from @fmt... (formatted like
482 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
484 GCC_FMT_ATTR(3, 4)
485 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
487 va_list ap;
488 QDict *args, *rsp;
490 va_start(ap, fmt);
491 args = qdict_from_vjsonf_nofail(fmt, ap);
492 va_end(ap);
494 g_assert(!qdict_haskey(args, "uri"));
495 qdict_put_str(args, "uri", uri);
497 rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
499 g_assert(qdict_haskey(rsp, "return"));
500 qobject_unref(rsp);
503 static void migrate_postcopy_start(QTestState *from, QTestState *to)
505 QDict *rsp;
507 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
508 qobject_unref(rsp);
510 if (!got_stop) {
511 qtest_qmp_eventwait(from, "STOP");
514 qtest_qmp_eventwait(to, "RESUME");
517 static int test_migrate_start(QTestState **from, QTestState **to,
518 const char *uri, bool hide_stderr,
519 bool use_shmem)
521 gchar *cmd_src, *cmd_dst;
522 char *bootpath = NULL;
523 char *extra_opts = NULL;
524 char *shmem_path = NULL;
525 const char *arch = qtest_get_arch();
526 const char *accel = "kvm:tcg";
528 if (use_shmem) {
529 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
530 g_test_skip("/dev/shm is not supported");
531 return -1;
533 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
536 got_stop = false;
537 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
538 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
539 init_bootfile(bootpath, x86_bootsect);
540 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
541 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
542 " -name source,debug-threads=on"
543 " -serial file:%s/src_serial"
544 " -drive file=%s,format=raw %s",
545 accel, tmpfs, bootpath,
546 extra_opts ? extra_opts : "");
547 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
548 " -name target,debug-threads=on"
549 " -serial file:%s/dest_serial"
550 " -drive file=%s,format=raw"
551 " -incoming %s %s",
552 accel, tmpfs, bootpath, uri,
553 extra_opts ? extra_opts : "");
554 start_address = X86_TEST_MEM_START;
555 end_address = X86_TEST_MEM_END;
556 } else if (g_str_equal(arch, "s390x")) {
557 init_bootfile_s390x(bootpath);
558 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
559 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
560 " -name source,debug-threads=on"
561 " -serial file:%s/src_serial -bios %s %s",
562 accel, tmpfs, bootpath,
563 extra_opts ? extra_opts : "");
564 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
565 " -name target,debug-threads=on"
566 " -serial file:%s/dest_serial -bios %s"
567 " -incoming %s %s",
568 accel, tmpfs, bootpath, uri,
569 extra_opts ? extra_opts : "");
570 start_address = S390_TEST_MEM_START;
571 end_address = S390_TEST_MEM_END;
572 } else if (strcmp(arch, "ppc64") == 0) {
573 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
574 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
575 " -name source,debug-threads=on"
576 " -serial file:%s/src_serial"
577 " -prom-env 'use-nvramrc?=true' -prom-env "
578 "'nvramrc=hex .\" _\" begin %x %x "
579 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
580 "until' %s", accel, tmpfs, end_address,
581 start_address, extra_opts ? extra_opts : "");
582 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
583 " -name target,debug-threads=on"
584 " -serial file:%s/dest_serial"
585 " -incoming %s %s",
586 accel, tmpfs, uri,
587 extra_opts ? extra_opts : "");
589 start_address = PPC_TEST_MEM_START;
590 end_address = PPC_TEST_MEM_END;
591 } else if (strcmp(arch, "aarch64") == 0) {
592 init_bootfile(bootpath, aarch64_kernel);
593 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
594 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
595 "-name vmsource,debug-threads=on -cpu max "
596 "-m 150M -serial file:%s/src_serial "
597 "-kernel %s %s",
598 accel, tmpfs, bootpath,
599 extra_opts ? extra_opts : "");
600 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
601 "-name vmdest,debug-threads=on -cpu max "
602 "-m 150M -serial file:%s/dest_serial "
603 "-kernel %s "
604 "-incoming %s %s",
605 accel, tmpfs, bootpath, uri,
606 extra_opts ? extra_opts : "");
608 start_address = ARM_TEST_MEM_START;
609 end_address = ARM_TEST_MEM_END;
611 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
612 } else {
613 g_assert_not_reached();
616 g_free(bootpath);
617 g_free(extra_opts);
619 if (hide_stderr) {
620 gchar *tmp;
621 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
622 g_free(cmd_src);
623 cmd_src = tmp;
625 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
626 g_free(cmd_dst);
627 cmd_dst = tmp;
630 *from = qtest_start(cmd_src);
631 g_free(cmd_src);
633 *to = qtest_init(cmd_dst);
634 g_free(cmd_dst);
637 * Remove shmem file immediately to avoid memory leak in test failed case.
638 * It's valid becase QEMU has already opened this file
640 if (use_shmem) {
641 unlink(shmem_path);
642 g_free(shmem_path);
645 return 0;
648 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
650 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
652 qtest_quit(from);
654 if (test_dest) {
655 qtest_memread(to, start_address, &dest_byte_a, 1);
657 /* Destination still running, wait for a byte to change */
658 do {
659 qtest_memread(to, start_address, &dest_byte_b, 1);
660 usleep(1000 * 10);
661 } while (dest_byte_a == dest_byte_b);
663 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
665 /* With it stopped, check nothing changes */
666 qtest_memread(to, start_address, &dest_byte_c, 1);
667 usleep(1000 * 200);
668 qtest_memread(to, start_address, &dest_byte_d, 1);
669 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
671 check_guests_ram(to);
674 qtest_quit(to);
676 cleanup("bootsect");
677 cleanup("migsocket");
678 cleanup("src_serial");
679 cleanup("dest_serial");
682 static void deprecated_set_downtime(QTestState *who, const double value)
684 QDict *rsp;
686 rsp = qtest_qmp(who,
687 "{ 'execute': 'migrate_set_downtime',"
688 " 'arguments': { 'value': %f } }", value);
689 g_assert(qdict_haskey(rsp, "return"));
690 qobject_unref(rsp);
691 migrate_check_parameter(who, "downtime-limit", value * 1000);
694 static void deprecated_set_speed(QTestState *who, long long value)
696 QDict *rsp;
698 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
699 "'arguments': { 'value': %lld } }", value);
700 g_assert(qdict_haskey(rsp, "return"));
701 qobject_unref(rsp);
702 migrate_check_parameter(who, "max-bandwidth", value);
705 static void deprecated_set_cache_size(QTestState *who, long long value)
707 QDict *rsp;
709 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
710 "'arguments': { 'value': %lld } }", value);
711 g_assert(qdict_haskey(rsp, "return"));
712 qobject_unref(rsp);
713 migrate_check_parameter(who, "xbzrle-cache-size", value);
716 static void test_deprecated(void)
718 QTestState *from;
720 from = qtest_start("-machine none");
722 deprecated_set_downtime(from, 0.12345);
723 deprecated_set_speed(from, 12345);
724 deprecated_set_cache_size(from, 4096);
726 qtest_quit(from);
729 static int migrate_postcopy_prepare(QTestState **from_ptr,
730 QTestState **to_ptr,
731 bool hide_error)
733 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
734 QTestState *from, *to;
736 if (test_migrate_start(&from, &to, uri, hide_error, false)) {
737 return -1;
740 migrate_set_capability(from, "postcopy-ram", true);
741 migrate_set_capability(to, "postcopy-ram", true);
742 migrate_set_capability(to, "postcopy-blocktime", true);
744 /* We want to pick a speed slow enough that the test completes
745 * quickly, but that it doesn't complete precopy even on a slow
746 * machine, so also set the downtime.
748 migrate_set_parameter(from, "max-bandwidth", 100000000);
749 migrate_set_parameter(from, "downtime-limit", 1);
751 /* Wait for the first serial output from the source */
752 wait_for_serial("src_serial");
754 migrate(from, uri, "{}");
755 g_free(uri);
757 wait_for_migration_pass(from);
759 *from_ptr = from;
760 *to_ptr = to;
762 return 0;
765 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
767 wait_for_migration_complete(from);
769 /* Make sure we get at least one "B" on destination */
770 wait_for_serial("dest_serial");
772 if (uffd_feature_thread_id) {
773 read_blocktime(to);
776 test_migrate_end(from, to, true);
779 static void test_postcopy(void)
781 QTestState *from, *to;
783 if (migrate_postcopy_prepare(&from, &to, false)) {
784 return;
786 migrate_postcopy_start(from, to);
787 migrate_postcopy_complete(from, to);
790 static void test_postcopy_recovery(void)
792 QTestState *from, *to;
793 char *uri;
795 if (migrate_postcopy_prepare(&from, &to, true)) {
796 return;
799 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
800 migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
802 /* Now we start the postcopy */
803 migrate_postcopy_start(from, to);
806 * Wait until postcopy is really started; we can only run the
807 * migrate-pause command during a postcopy
809 wait_for_migration_status(from, "postcopy-active");
812 * Manually stop the postcopy migration. This emulates a network
813 * failure with the migration socket
815 migrate_pause(from);
818 * Wait for destination side to reach postcopy-paused state. The
819 * migrate-recover command can only succeed if destination machine
820 * is in the paused state
822 wait_for_migration_status(to, "postcopy-paused");
825 * Create a new socket to emulate a new channel that is different
826 * from the broken migration channel; tell the destination to
827 * listen to the new port
829 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
830 migrate_recover(to, uri);
833 * Try to rebuild the migration channel using the resume flag and
834 * the newly created channel
836 wait_for_migration_status(from, "postcopy-paused");
837 migrate(from, uri, "{'resume': true}");
838 g_free(uri);
840 /* Restore the postcopy bandwidth to unlimited */
841 migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
843 migrate_postcopy_complete(from, to);
846 static void test_baddest(void)
848 QTestState *from, *to;
849 QDict *rsp_return;
850 char *status;
851 bool failed;
853 if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
854 return;
856 migrate(from, "tcp:0:0", "{}");
857 do {
858 status = migrate_query_status(from);
859 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
860 failed = !strcmp(status, "failed");
861 g_free(status);
862 } while (!failed);
864 /* Is the machine currently running? */
865 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
866 g_assert(qdict_haskey(rsp_return, "running"));
867 g_assert(qdict_get_bool(rsp_return, "running"));
868 qobject_unref(rsp_return);
870 test_migrate_end(from, to, false);
873 static void test_precopy_unix(void)
875 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
876 QTestState *from, *to;
878 if (test_migrate_start(&from, &to, uri, false, false)) {
879 return;
882 /* We want to pick a speed slow enough that the test completes
883 * quickly, but that it doesn't complete precopy even on a slow
884 * machine, so also set the downtime.
886 /* 1 ms should make it not converge*/
887 migrate_set_parameter(from, "downtime-limit", 1);
888 /* 1GB/s */
889 migrate_set_parameter(from, "max-bandwidth", 1000000000);
891 /* Wait for the first serial output from the source */
892 wait_for_serial("src_serial");
894 migrate(from, uri, "{}");
896 wait_for_migration_pass(from);
898 /* 300 ms should converge */
899 migrate_set_parameter(from, "downtime-limit", 300);
901 if (!got_stop) {
902 qtest_qmp_eventwait(from, "STOP");
905 qtest_qmp_eventwait(to, "RESUME");
907 wait_for_serial("dest_serial");
908 wait_for_migration_complete(from);
910 test_migrate_end(from, to, true);
911 g_free(uri);
914 #if 0
915 /* Currently upset on aarch64 TCG */
916 static void test_ignore_shared(void)
918 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
919 QTestState *from, *to;
921 if (test_migrate_start(&from, &to, uri, false, true)) {
922 return;
925 migrate_set_capability(from, "x-ignore-shared", true);
926 migrate_set_capability(to, "x-ignore-shared", true);
928 /* Wait for the first serial output from the source */
929 wait_for_serial("src_serial");
931 migrate(from, uri, "{}");
933 wait_for_migration_pass(from);
935 if (!got_stop) {
936 qtest_qmp_eventwait(from, "STOP");
939 qtest_qmp_eventwait(to, "RESUME");
941 wait_for_serial("dest_serial");
942 wait_for_migration_complete(from);
944 /* Check whether shared RAM has been really skipped */
945 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
947 test_migrate_end(from, to, true);
948 g_free(uri);
950 #endif
952 static void test_xbzrle(const char *uri)
954 QTestState *from, *to;
956 if (test_migrate_start(&from, &to, uri, false, false)) {
957 return;
961 * We want to pick a speed slow enough that the test completes
962 * quickly, but that it doesn't complete precopy even on a slow
963 * machine, so also set the downtime.
965 /* 1 ms should make it not converge*/
966 migrate_set_parameter(from, "downtime-limit", 1);
967 /* 1GB/s */
968 migrate_set_parameter(from, "max-bandwidth", 1000000000);
970 migrate_set_parameter(from, "xbzrle-cache-size", 33554432);
972 migrate_set_capability(from, "xbzrle", "true");
973 migrate_set_capability(to, "xbzrle", "true");
974 /* Wait for the first serial output from the source */
975 wait_for_serial("src_serial");
977 migrate(from, uri, "{}");
979 wait_for_migration_pass(from);
981 /* 300ms should converge */
982 migrate_set_parameter(from, "downtime-limit", 300);
984 if (!got_stop) {
985 qtest_qmp_eventwait(from, "STOP");
987 qtest_qmp_eventwait(to, "RESUME");
989 wait_for_serial("dest_serial");
990 wait_for_migration_complete(from);
992 test_migrate_end(from, to, true);
995 static void test_xbzrle_unix(void)
997 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
999 test_xbzrle(uri);
1000 g_free(uri);
1003 static void test_precopy_tcp(void)
1005 char *uri;
1006 QTestState *from, *to;
1008 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false)) {
1009 return;
1013 * We want to pick a speed slow enough that the test completes
1014 * quickly, but that it doesn't complete precopy even on a slow
1015 * machine, so also set the downtime.
1017 /* 1 ms should make it not converge*/
1018 migrate_set_parameter(from, "downtime-limit", 1);
1019 /* 1GB/s */
1020 migrate_set_parameter(from, "max-bandwidth", 1000000000);
1022 /* Wait for the first serial output from the source */
1023 wait_for_serial("src_serial");
1025 uri = migrate_get_socket_address(to, "socket-address");
1027 migrate(from, uri, "{}");
1029 wait_for_migration_pass(from);
1031 /* 300ms should converge */
1032 migrate_set_parameter(from, "downtime-limit", 300);
1034 if (!got_stop) {
1035 qtest_qmp_eventwait(from, "STOP");
1037 qtest_qmp_eventwait(to, "RESUME");
1039 wait_for_serial("dest_serial");
1040 wait_for_migration_complete(from);
1042 test_migrate_end(from, to, true);
1043 g_free(uri);
1046 static void test_migrate_fd_proto(void)
1048 QTestState *from, *to;
1049 int ret;
1050 int pair[2];
1051 QDict *rsp;
1052 const char *error_desc;
1054 if (test_migrate_start(&from, &to, "defer", false, false)) {
1055 return;
1059 * We want to pick a speed slow enough that the test completes
1060 * quickly, but that it doesn't complete precopy even on a slow
1061 * machine, so also set the downtime.
1063 /* 1 ms should make it not converge */
1064 migrate_set_parameter(from, "downtime-limit", 1);
1065 /* 1GB/s */
1066 migrate_set_parameter(from, "max-bandwidth", 1000000000);
1068 /* Wait for the first serial output from the source */
1069 wait_for_serial("src_serial");
1071 /* Create two connected sockets for migration */
1072 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1073 g_assert_cmpint(ret, ==, 0);
1075 /* Send the 1st socket to the target */
1076 rsp = wait_command_fd(to, pair[0],
1077 "{ 'execute': 'getfd',"
1078 " 'arguments': { 'fdname': 'fd-mig' }}");
1079 qobject_unref(rsp);
1080 close(pair[0]);
1082 /* Start incoming migration from the 1st socket */
1083 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1084 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1085 qobject_unref(rsp);
1087 /* Send the 2nd socket to the target */
1088 rsp = wait_command_fd(from, pair[1],
1089 "{ 'execute': 'getfd',"
1090 " 'arguments': { 'fdname': 'fd-mig' }}");
1091 qobject_unref(rsp);
1092 close(pair[1]);
1094 /* Start migration to the 2nd socket*/
1095 migrate(from, "fd:fd-mig", "{}");
1097 wait_for_migration_pass(from);
1099 /* 300ms should converge */
1100 migrate_set_parameter(from, "downtime-limit", 300);
1102 if (!got_stop) {
1103 qtest_qmp_eventwait(from, "STOP");
1105 qtest_qmp_eventwait(to, "RESUME");
1107 /* Test closing fds */
1108 /* We assume, that QEMU removes named fd from its list,
1109 * so this should fail */
1110 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1111 " 'arguments': { 'fdname': 'fd-mig' }}");
1112 g_assert_true(qdict_haskey(rsp, "error"));
1113 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1114 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1115 qobject_unref(rsp);
1117 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1118 " 'arguments': { 'fdname': 'fd-mig' }}");
1119 g_assert_true(qdict_haskey(rsp, "error"));
1120 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1121 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1122 qobject_unref(rsp);
1124 /* Complete migration */
1125 wait_for_serial("dest_serial");
1126 wait_for_migration_complete(from);
1127 test_migrate_end(from, to, true);
1130 int main(int argc, char **argv)
1132 char template[] = "/tmp/migration-test-XXXXXX";
1133 int ret;
1135 g_test_init(&argc, &argv, NULL);
1137 if (!ufd_version_check()) {
1138 return g_test_run();
1142 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1143 * is touchy due to race conditions on dirty bits (especially on PPC for
1144 * some reason)
1146 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1147 access("/sys/module/kvm_hv", F_OK)) {
1148 g_test_message("Skipping test: kvm_hv not available");
1149 return g_test_run();
1153 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1154 * there until the problems are resolved
1156 if (g_str_equal(qtest_get_arch(), "s390x")) {
1157 #if defined(HOST_S390X)
1158 if (access("/dev/kvm", R_OK | W_OK)) {
1159 g_test_message("Skipping test: kvm not available");
1160 return g_test_run();
1162 #else
1163 g_test_message("Skipping test: Need s390x host to work properly");
1164 return g_test_run();
1165 #endif
1168 tmpfs = mkdtemp(template);
1169 if (!tmpfs) {
1170 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1172 g_assert(tmpfs);
1174 module_call_init(MODULE_INIT_QOM);
1176 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1177 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1178 qtest_add_func("/migration/deprecated", test_deprecated);
1179 qtest_add_func("/migration/bad_dest", test_baddest);
1180 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1181 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1182 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1183 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1184 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1186 ret = g_test_run();
1188 g_assert_cmpint(ret, ==, 0);
1190 ret = rmdir(tmpfs);
1191 if (ret != 0) {
1192 g_test_message("unable to rmdir: path (%s): %s",
1193 tmpfs, strerror(errno));
1196 return ret;