qapi: Improve reporting of redefinition
[qemu/armbru.git] / tests / migration-test.c
blob221a33d0834b04c231e1d36c2359e0e76a401681
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/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
27 #include "migration/migration-test.h"
29 /* TODO actually test the results and get rid of this */
30 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
32 unsigned start_address;
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"
93 #include "tests/migration/s390x/a-b-bios.h"
95 static void init_bootfile(const char *bootpath, void *content, size_t len)
97 FILE *bootfile = fopen(bootpath, "wb");
99 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
100 fclose(bootfile);
104 * Wait for some output in the serial output file,
105 * we get an 'A' followed by an endless string of 'B's
106 * but on the destination we won't have the A.
108 static void wait_for_serial(const char *side)
110 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
111 FILE *serialfile = fopen(serialpath, "r");
112 const char *arch = qtest_get_arch();
113 int started = (strcmp(side, "src_serial") == 0 &&
114 strcmp(arch, "ppc64") == 0) ? 0 : 1;
116 g_free(serialpath);
117 do {
118 int readvalue = fgetc(serialfile);
120 if (!started) {
121 /* SLOF prints its banner before starting test,
122 * to ignore it, mark the start of the test with '_',
123 * ignore all characters until this marker
125 switch (readvalue) {
126 case '_':
127 started = 1;
128 break;
129 case EOF:
130 fseek(serialfile, 0, SEEK_SET);
131 usleep(1000);
132 break;
134 continue;
136 switch (readvalue) {
137 case 'A':
138 /* Fine */
139 break;
141 case 'B':
142 /* It's alive! */
143 fclose(serialfile);
144 return;
146 case EOF:
147 started = (strcmp(side, "src_serial") == 0 &&
148 strcmp(arch, "ppc64") == 0) ? 0 : 1;
149 fseek(serialfile, 0, SEEK_SET);
150 usleep(1000);
151 break;
153 default:
154 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
155 g_assert_not_reached();
157 } while (true);
160 static void stop_cb(void *opaque, const char *name, QDict *data)
162 if (!strcmp(name, "STOP")) {
163 got_stop = true;
168 * Events can get in the way of responses we are actually waiting for.
170 GCC_FMT_ATTR(3, 4)
171 static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
173 va_list ap;
175 va_start(ap, command);
176 qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
177 va_end(ap);
179 return qtest_qmp_receive_success(who, stop_cb, NULL);
183 * Events can get in the way of responses we are actually waiting for.
185 GCC_FMT_ATTR(2, 3)
186 static QDict *wait_command(QTestState *who, const char *command, ...)
188 va_list ap;
190 va_start(ap, command);
191 qtest_qmp_vsend(who, command, ap);
192 va_end(ap);
194 return qtest_qmp_receive_success(who, stop_cb, NULL);
198 * Note: caller is responsible to free the returned object via
199 * qobject_unref() after use
201 static QDict *migrate_query(QTestState *who)
203 return wait_command(who, "{ 'execute': 'query-migrate' }");
207 * Note: caller is responsible to free the returned object via
208 * g_free() after use
210 static gchar *migrate_query_status(QTestState *who)
212 QDict *rsp_return = migrate_query(who);
213 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
215 g_assert(status);
216 qobject_unref(rsp_return);
218 return status;
222 * It's tricky to use qemu's migration event capability with qtest,
223 * events suddenly appearing confuse the qmp()/hmp() responses.
226 static int64_t read_ram_property_int(QTestState *who, const char *property)
228 QDict *rsp_return, *rsp_ram;
229 int64_t result;
231 rsp_return = migrate_query(who);
232 if (!qdict_haskey(rsp_return, "ram")) {
233 /* Still in setup */
234 result = 0;
235 } else {
236 rsp_ram = qdict_get_qdict(rsp_return, "ram");
237 result = qdict_get_try_int(rsp_ram, property, 0);
239 qobject_unref(rsp_return);
240 return result;
243 static uint64_t get_migration_pass(QTestState *who)
245 return read_ram_property_int(who, "dirty-sync-count");
248 static void read_blocktime(QTestState *who)
250 QDict *rsp_return;
252 rsp_return = migrate_query(who);
253 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
254 qobject_unref(rsp_return);
257 static void wait_for_migration_status(QTestState *who,
258 const char *goal,
259 const char **ungoals)
261 while (true) {
262 bool completed;
263 char *status;
264 const char **ungoal;
266 status = migrate_query_status(who);
267 completed = strcmp(status, goal) == 0;
268 for (ungoal = ungoals; *ungoal; ungoal++) {
269 g_assert_cmpstr(status, !=, *ungoal);
271 g_free(status);
272 if (completed) {
273 return;
275 usleep(1000);
279 static void wait_for_migration_complete(QTestState *who)
281 wait_for_migration_status(who, "completed",
282 (const char * []) { "failed", NULL });
285 static void wait_for_migration_pass(QTestState *who)
287 uint64_t initial_pass = get_migration_pass(who);
288 uint64_t pass;
290 /* Wait for the 1st sync */
291 while (!got_stop && !initial_pass) {
292 usleep(1000);
293 initial_pass = get_migration_pass(who);
296 do {
297 usleep(1000);
298 pass = get_migration_pass(who);
299 } while (pass == initial_pass && !got_stop);
302 static void check_guests_ram(QTestState *who)
304 /* Our ASM test will have been incrementing one byte from each page from
305 * start_address to < end_address in order. This gives us a constraint
306 * that any page's byte should be equal or less than the previous pages
307 * byte (mod 256); and they should all be equal except for one transition
308 * at the point where we meet the incrementer. (We're running this with
309 * the guest stopped).
311 unsigned address;
312 uint8_t first_byte;
313 uint8_t last_byte;
314 bool hit_edge = false;
315 int bad = 0;
317 qtest_memread(who, start_address, &first_byte, 1);
318 last_byte = first_byte;
320 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
321 address += TEST_MEM_PAGE_SIZE)
323 uint8_t b;
324 qtest_memread(who, address, &b, 1);
325 if (b != last_byte) {
326 if (((b + 1) % 256) == last_byte && !hit_edge) {
327 /* This is OK, the guest stopped at the point of
328 * incrementing the previous page but didn't get
329 * to us yet.
331 hit_edge = true;
332 last_byte = b;
333 } else {
334 bad++;
335 if (bad <= 10) {
336 fprintf(stderr, "Memory content inconsistency at %x"
337 " first_byte = %x last_byte = %x current = %x"
338 " hit_edge = %x\n",
339 address, first_byte, last_byte, b, hit_edge);
344 if (bad >= 10) {
345 fprintf(stderr, "and in another %d pages", bad - 10);
347 g_assert(bad == 0);
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_int(QTestState *who,
411 const char *parameter)
413 QDict *rsp;
414 long long result;
416 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
417 result = qdict_get_int(rsp, parameter);
418 qobject_unref(rsp);
419 return result;
422 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
423 long long value)
425 long long result;
427 result = migrate_get_parameter_int(who, parameter);
428 g_assert_cmpint(result, ==, value);
431 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
432 long long value)
434 QDict *rsp;
436 rsp = qtest_qmp(who,
437 "{ 'execute': 'migrate-set-parameters',"
438 "'arguments': { %s: %lld } }",
439 parameter, value);
440 g_assert(qdict_haskey(rsp, "return"));
441 qobject_unref(rsp);
442 migrate_check_parameter_int(who, parameter, value);
445 static void migrate_pause(QTestState *who)
447 QDict *rsp;
449 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
450 qobject_unref(rsp);
453 static void migrate_recover(QTestState *who, const char *uri)
455 QDict *rsp;
457 rsp = wait_command(who,
458 "{ 'execute': 'migrate-recover', "
459 " 'id': 'recover-cmd', "
460 " 'arguments': { 'uri': %s } }",
461 uri);
462 qobject_unref(rsp);
465 static void migrate_set_capability(QTestState *who, const char *capability,
466 bool value)
468 QDict *rsp;
470 rsp = qtest_qmp(who,
471 "{ 'execute': 'migrate-set-capabilities',"
472 "'arguments': { "
473 "'capabilities': [ { "
474 "'capability': %s, 'state': %i } ] } }",
475 capability, value);
476 g_assert(qdict_haskey(rsp, "return"));
477 qobject_unref(rsp);
481 * Send QMP command "migrate".
482 * Arguments are built from @fmt... (formatted like
483 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
485 GCC_FMT_ATTR(3, 4)
486 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
488 va_list ap;
489 QDict *args, *rsp;
491 va_start(ap, fmt);
492 args = qdict_from_vjsonf_nofail(fmt, ap);
493 va_end(ap);
495 g_assert(!qdict_haskey(args, "uri"));
496 qdict_put_str(args, "uri", uri);
498 rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args);
500 g_assert(qdict_haskey(rsp, "return"));
501 qobject_unref(rsp);
504 static void migrate_postcopy_start(QTestState *from, QTestState *to)
506 QDict *rsp;
508 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
509 qobject_unref(rsp);
511 if (!got_stop) {
512 qtest_qmp_eventwait(from, "STOP");
515 qtest_qmp_eventwait(to, "RESUME");
518 static int test_migrate_start(QTestState **from, QTestState **to,
519 const char *uri, bool hide_stderr,
520 bool use_shmem, const char *opts_src,
521 const char *opts_dst)
523 gchar *cmd_src, *cmd_dst;
524 char *bootpath = NULL;
525 char *extra_opts = NULL;
526 char *shmem_path = NULL;
527 const char *arch = qtest_get_arch();
528 const char *accel = "kvm:tcg";
530 opts_src = opts_src ? opts_src : "";
531 opts_dst = opts_dst ? opts_dst : "";
533 if (use_shmem) {
534 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
535 g_test_skip("/dev/shm is not supported");
536 return -1;
538 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
541 got_stop = false;
542 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
543 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
544 /* the assembled x86 boot sector should be exactly one sector large */
545 assert(sizeof(x86_bootsect) == 512);
546 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
547 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
548 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
549 " -name source,debug-threads=on"
550 " -serial file:%s/src_serial"
551 " -drive file=%s,format=raw %s %s",
552 accel, tmpfs, bootpath,
553 extra_opts ? extra_opts : "", opts_src);
554 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
555 " -name target,debug-threads=on"
556 " -serial file:%s/dest_serial"
557 " -drive file=%s,format=raw"
558 " -incoming %s %s %s",
559 accel, tmpfs, bootpath, uri,
560 extra_opts ? extra_opts : "", opts_dst);
561 start_address = X86_TEST_MEM_START;
562 end_address = X86_TEST_MEM_END;
563 } else if (g_str_equal(arch, "s390x")) {
564 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
565 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
566 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
567 " -name source,debug-threads=on"
568 " -serial file:%s/src_serial -bios %s %s %s",
569 accel, tmpfs, bootpath,
570 extra_opts ? extra_opts : "", opts_src);
571 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
572 " -name target,debug-threads=on"
573 " -serial file:%s/dest_serial -bios %s"
574 " -incoming %s %s %s",
575 accel, tmpfs, bootpath, uri,
576 extra_opts ? extra_opts : "", opts_dst);
577 start_address = S390_TEST_MEM_START;
578 end_address = S390_TEST_MEM_END;
579 } else if (strcmp(arch, "ppc64") == 0) {
580 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
581 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
582 " -name source,debug-threads=on"
583 " -serial file:%s/src_serial"
584 " -prom-env 'use-nvramrc?=true' -prom-env "
585 "'nvramrc=hex .\" _\" begin %x %x "
586 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
587 "until' %s %s", accel, tmpfs, end_address,
588 start_address, extra_opts ? extra_opts : "",
589 opts_src);
590 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
591 " -name target,debug-threads=on"
592 " -serial file:%s/dest_serial"
593 " -incoming %s %s %s",
594 accel, tmpfs, uri,
595 extra_opts ? extra_opts : "", opts_dst);
597 start_address = PPC_TEST_MEM_START;
598 end_address = PPC_TEST_MEM_END;
599 } else if (strcmp(arch, "aarch64") == 0) {
600 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
601 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
602 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
603 "-name vmsource,debug-threads=on -cpu max "
604 "-m 150M -serial file:%s/src_serial "
605 "-kernel %s %s %s",
606 accel, tmpfs, bootpath,
607 extra_opts ? extra_opts : "", opts_src);
608 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
609 "-name vmdest,debug-threads=on -cpu max "
610 "-m 150M -serial file:%s/dest_serial "
611 "-kernel %s "
612 "-incoming %s %s %s",
613 accel, tmpfs, bootpath, uri,
614 extra_opts ? extra_opts : "", opts_dst);
616 start_address = ARM_TEST_MEM_START;
617 end_address = ARM_TEST_MEM_END;
619 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
620 } else {
621 g_assert_not_reached();
624 g_free(bootpath);
625 g_free(extra_opts);
627 if (hide_stderr) {
628 gchar *tmp;
629 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
630 g_free(cmd_src);
631 cmd_src = tmp;
633 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
634 g_free(cmd_dst);
635 cmd_dst = tmp;
638 *from = qtest_init(cmd_src);
639 g_free(cmd_src);
641 *to = qtest_init(cmd_dst);
642 g_free(cmd_dst);
645 * Remove shmem file immediately to avoid memory leak in test failed case.
646 * It's valid becase QEMU has already opened this file
648 if (use_shmem) {
649 unlink(shmem_path);
650 g_free(shmem_path);
653 return 0;
656 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
658 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
660 qtest_quit(from);
662 if (test_dest) {
663 qtest_memread(to, start_address, &dest_byte_a, 1);
665 /* Destination still running, wait for a byte to change */
666 do {
667 qtest_memread(to, start_address, &dest_byte_b, 1);
668 usleep(1000 * 10);
669 } while (dest_byte_a == dest_byte_b);
671 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
673 /* With it stopped, check nothing changes */
674 qtest_memread(to, start_address, &dest_byte_c, 1);
675 usleep(1000 * 200);
676 qtest_memread(to, start_address, &dest_byte_d, 1);
677 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
679 check_guests_ram(to);
682 qtest_quit(to);
684 cleanup("bootsect");
685 cleanup("migsocket");
686 cleanup("src_serial");
687 cleanup("dest_serial");
690 static void deprecated_set_downtime(QTestState *who, const double value)
692 QDict *rsp;
694 rsp = qtest_qmp(who,
695 "{ 'execute': 'migrate_set_downtime',"
696 " 'arguments': { 'value': %f } }", value);
697 g_assert(qdict_haskey(rsp, "return"));
698 qobject_unref(rsp);
699 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
702 static void deprecated_set_speed(QTestState *who, long long value)
704 QDict *rsp;
706 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
707 "'arguments': { 'value': %lld } }", value);
708 g_assert(qdict_haskey(rsp, "return"));
709 qobject_unref(rsp);
710 migrate_check_parameter_int(who, "max-bandwidth", value);
713 static void deprecated_set_cache_size(QTestState *who, long long value)
715 QDict *rsp;
717 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
718 "'arguments': { 'value': %lld } }", value);
719 g_assert(qdict_haskey(rsp, "return"));
720 qobject_unref(rsp);
721 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
724 static void test_deprecated(void)
726 QTestState *from;
728 from = qtest_init("-machine none");
730 deprecated_set_downtime(from, 0.12345);
731 deprecated_set_speed(from, 12345);
732 deprecated_set_cache_size(from, 4096);
734 qtest_quit(from);
737 static int migrate_postcopy_prepare(QTestState **from_ptr,
738 QTestState **to_ptr,
739 bool hide_error)
741 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
742 QTestState *from, *to;
744 if (test_migrate_start(&from, &to, uri, hide_error, false, NULL, NULL)) {
745 return -1;
748 migrate_set_capability(from, "postcopy-ram", true);
749 migrate_set_capability(to, "postcopy-ram", true);
750 migrate_set_capability(to, "postcopy-blocktime", true);
752 /* We want to pick a speed slow enough that the test completes
753 * quickly, but that it doesn't complete precopy even on a slow
754 * machine, so also set the downtime.
756 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
757 migrate_set_parameter_int(from, "downtime-limit", 1);
759 /* Wait for the first serial output from the source */
760 wait_for_serial("src_serial");
762 migrate(from, uri, "{}");
763 g_free(uri);
765 wait_for_migration_pass(from);
767 *from_ptr = from;
768 *to_ptr = to;
770 return 0;
773 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
775 wait_for_migration_complete(from);
777 /* Make sure we get at least one "B" on destination */
778 wait_for_serial("dest_serial");
780 if (uffd_feature_thread_id) {
781 read_blocktime(to);
784 test_migrate_end(from, to, true);
787 static void test_postcopy(void)
789 QTestState *from, *to;
791 if (migrate_postcopy_prepare(&from, &to, false)) {
792 return;
794 migrate_postcopy_start(from, to);
795 migrate_postcopy_complete(from, to);
798 static void test_postcopy_recovery(void)
800 QTestState *from, *to;
801 char *uri;
803 if (migrate_postcopy_prepare(&from, &to, true)) {
804 return;
807 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
808 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
810 /* Now we start the postcopy */
811 migrate_postcopy_start(from, to);
814 * Wait until postcopy is really started; we can only run the
815 * migrate-pause command during a postcopy
817 wait_for_migration_status(from, "postcopy-active",
818 (const char * []) { "failed",
819 "completed", NULL });
822 * Manually stop the postcopy migration. This emulates a network
823 * failure with the migration socket
825 migrate_pause(from);
828 * Wait for destination side to reach postcopy-paused state. The
829 * migrate-recover command can only succeed if destination machine
830 * is in the paused state
832 wait_for_migration_status(to, "postcopy-paused",
833 (const char * []) { "failed", "active",
834 "completed", NULL });
837 * Create a new socket to emulate a new channel that is different
838 * from the broken migration channel; tell the destination to
839 * listen to the new port
841 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
842 migrate_recover(to, uri);
845 * Try to rebuild the migration channel using the resume flag and
846 * the newly created channel
848 wait_for_migration_status(from, "postcopy-paused",
849 (const char * []) { "failed", "active",
850 "completed", NULL });
851 migrate(from, uri, "{'resume': true}");
852 g_free(uri);
854 /* Restore the postcopy bandwidth to unlimited */
855 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
857 migrate_postcopy_complete(from, to);
860 static void wait_for_migration_fail(QTestState *from, bool allow_active)
862 QDict *rsp_return;
863 char *status;
864 bool failed;
866 do {
867 status = migrate_query_status(from);
868 g_assert(!strcmp(status, "setup") || !strcmp(status, "failed") ||
869 (allow_active && !strcmp(status, "active")));
870 failed = !strcmp(status, "failed");
871 g_free(status);
872 } while (!failed);
874 /* Is the machine currently running? */
875 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
876 g_assert(qdict_haskey(rsp_return, "running"));
877 g_assert(qdict_get_bool(rsp_return, "running"));
878 qobject_unref(rsp_return);
881 static void test_baddest(void)
883 QTestState *from, *to;
885 if (test_migrate_start(&from, &to, "tcp:0:0", true, false, NULL, NULL)) {
886 return;
888 migrate(from, "tcp:0:0", "{}");
889 wait_for_migration_fail(from, false);
890 test_migrate_end(from, to, false);
893 static void test_precopy_unix(void)
895 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
896 QTestState *from, *to;
898 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
899 return;
902 /* We want to pick a speed slow enough that the test completes
903 * quickly, but that it doesn't complete precopy even on a slow
904 * machine, so also set the downtime.
906 /* 1 ms should make it not converge*/
907 migrate_set_parameter_int(from, "downtime-limit", 1);
908 /* 1GB/s */
909 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
911 /* Wait for the first serial output from the source */
912 wait_for_serial("src_serial");
914 migrate(from, uri, "{}");
916 wait_for_migration_pass(from);
918 /* 300 ms should converge */
919 migrate_set_parameter_int(from, "downtime-limit", 300);
921 if (!got_stop) {
922 qtest_qmp_eventwait(from, "STOP");
925 qtest_qmp_eventwait(to, "RESUME");
927 wait_for_serial("dest_serial");
928 wait_for_migration_complete(from);
930 test_migrate_end(from, to, true);
931 g_free(uri);
934 #if 0
935 /* Currently upset on aarch64 TCG */
936 static void test_ignore_shared(void)
938 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
939 QTestState *from, *to;
941 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
942 return;
945 migrate_set_capability(from, "x-ignore-shared", true);
946 migrate_set_capability(to, "x-ignore-shared", true);
948 /* Wait for the first serial output from the source */
949 wait_for_serial("src_serial");
951 migrate(from, uri, "{}");
953 wait_for_migration_pass(from);
955 if (!got_stop) {
956 qtest_qmp_eventwait(from, "STOP");
959 qtest_qmp_eventwait(to, "RESUME");
961 wait_for_serial("dest_serial");
962 wait_for_migration_complete(from);
964 /* Check whether shared RAM has been really skipped */
965 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
967 test_migrate_end(from, to, true);
968 g_free(uri);
970 #endif
972 static void test_xbzrle(const char *uri)
974 QTestState *from, *to;
976 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
977 return;
981 * We want to pick a speed slow enough that the test completes
982 * quickly, but that it doesn't complete precopy even on a slow
983 * machine, so also set the downtime.
985 /* 1 ms should make it not converge*/
986 migrate_set_parameter_int(from, "downtime-limit", 1);
987 /* 1GB/s */
988 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
990 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
992 migrate_set_capability(from, "xbzrle", "true");
993 migrate_set_capability(to, "xbzrle", "true");
994 /* Wait for the first serial output from the source */
995 wait_for_serial("src_serial");
997 migrate(from, uri, "{}");
999 wait_for_migration_pass(from);
1001 /* 300ms should converge */
1002 migrate_set_parameter_int(from, "downtime-limit", 300);
1004 if (!got_stop) {
1005 qtest_qmp_eventwait(from, "STOP");
1007 qtest_qmp_eventwait(to, "RESUME");
1009 wait_for_serial("dest_serial");
1010 wait_for_migration_complete(from);
1012 test_migrate_end(from, to, true);
1015 static void test_xbzrle_unix(void)
1017 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1019 test_xbzrle(uri);
1020 g_free(uri);
1023 static void test_precopy_tcp(void)
1025 char *uri;
1026 QTestState *from, *to;
1028 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false,
1029 NULL, NULL)) {
1030 return;
1034 * We want to pick a speed slow enough that the test completes
1035 * quickly, but that it doesn't complete precopy even on a slow
1036 * machine, so also set the downtime.
1038 /* 1 ms should make it not converge*/
1039 migrate_set_parameter_int(from, "downtime-limit", 1);
1040 /* 1GB/s */
1041 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1043 /* Wait for the first serial output from the source */
1044 wait_for_serial("src_serial");
1046 uri = migrate_get_socket_address(to, "socket-address");
1048 migrate(from, uri, "{}");
1050 wait_for_migration_pass(from);
1052 /* 300ms should converge */
1053 migrate_set_parameter_int(from, "downtime-limit", 300);
1055 if (!got_stop) {
1056 qtest_qmp_eventwait(from, "STOP");
1058 qtest_qmp_eventwait(to, "RESUME");
1060 wait_for_serial("dest_serial");
1061 wait_for_migration_complete(from);
1063 test_migrate_end(from, to, true);
1064 g_free(uri);
1067 static void test_migrate_fd_proto(void)
1069 QTestState *from, *to;
1070 int ret;
1071 int pair[2];
1072 QDict *rsp;
1073 const char *error_desc;
1075 if (test_migrate_start(&from, &to, "defer", false, false, NULL, NULL)) {
1076 return;
1080 * We want to pick a speed slow enough that the test completes
1081 * quickly, but that it doesn't complete precopy even on a slow
1082 * machine, so also set the downtime.
1084 /* 1 ms should make it not converge */
1085 migrate_set_parameter_int(from, "downtime-limit", 1);
1086 /* 1GB/s */
1087 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1089 /* Wait for the first serial output from the source */
1090 wait_for_serial("src_serial");
1092 /* Create two connected sockets for migration */
1093 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1094 g_assert_cmpint(ret, ==, 0);
1096 /* Send the 1st socket to the target */
1097 rsp = wait_command_fd(to, pair[0],
1098 "{ 'execute': 'getfd',"
1099 " 'arguments': { 'fdname': 'fd-mig' }}");
1100 qobject_unref(rsp);
1101 close(pair[0]);
1103 /* Start incoming migration from the 1st socket */
1104 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1105 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1106 qobject_unref(rsp);
1108 /* Send the 2nd socket to the target */
1109 rsp = wait_command_fd(from, pair[1],
1110 "{ 'execute': 'getfd',"
1111 " 'arguments': { 'fdname': 'fd-mig' }}");
1112 qobject_unref(rsp);
1113 close(pair[1]);
1115 /* Start migration to the 2nd socket*/
1116 migrate(from, "fd:fd-mig", "{}");
1118 wait_for_migration_pass(from);
1120 /* 300ms should converge */
1121 migrate_set_parameter_int(from, "downtime-limit", 300);
1123 if (!got_stop) {
1124 qtest_qmp_eventwait(from, "STOP");
1126 qtest_qmp_eventwait(to, "RESUME");
1128 /* Test closing fds */
1129 /* We assume, that QEMU removes named fd from its list,
1130 * so this should fail */
1131 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1132 " 'arguments': { 'fdname': 'fd-mig' }}");
1133 g_assert_true(qdict_haskey(rsp, "error"));
1134 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1135 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1136 qobject_unref(rsp);
1138 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1139 " 'arguments': { 'fdname': 'fd-mig' }}");
1140 g_assert_true(qdict_haskey(rsp, "error"));
1141 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1142 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1143 qobject_unref(rsp);
1145 /* Complete migration */
1146 wait_for_serial("dest_serial");
1147 wait_for_migration_complete(from);
1148 test_migrate_end(from, to, true);
1151 static void do_test_validate_uuid(const char *uuid_arg_src,
1152 const char *uuid_arg_dst,
1153 bool should_fail, bool hide_stderr)
1155 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1156 QTestState *from, *to;
1158 if (test_migrate_start(&from, &to, uri, hide_stderr, false,
1159 uuid_arg_src, uuid_arg_dst)) {
1160 return;
1164 * UUID validation is at the begin of migration. So, the main process of
1165 * migration is not interesting for us here. Thus, set huge downtime for
1166 * very fast migration.
1168 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1169 migrate_set_capability(from, "validate-uuid", true);
1171 /* Wait for the first serial output from the source */
1172 wait_for_serial("src_serial");
1174 migrate(from, uri, "{}");
1176 if (should_fail) {
1177 qtest_set_expected_status(to, 1);
1178 wait_for_migration_fail(from, true);
1179 } else {
1180 wait_for_migration_complete(from);
1183 test_migrate_end(from, to, false);
1184 g_free(uri);
1187 static void test_validate_uuid(void)
1189 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1190 "-uuid 11111111-1111-1111-1111-111111111111",
1191 false, false);
1194 static void test_validate_uuid_error(void)
1196 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1197 "-uuid 22222222-2222-2222-2222-222222222222",
1198 true, true);
1201 static void test_validate_uuid_src_not_set(void)
1203 do_test_validate_uuid(NULL, "-uuid 11111111-1111-1111-1111-111111111111",
1204 false, true);
1207 static void test_validate_uuid_dst_not_set(void)
1209 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", NULL,
1210 false, true);
1213 int main(int argc, char **argv)
1215 char template[] = "/tmp/migration-test-XXXXXX";
1216 int ret;
1218 g_test_init(&argc, &argv, NULL);
1220 if (!ufd_version_check()) {
1221 return g_test_run();
1225 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1226 * is touchy due to race conditions on dirty bits (especially on PPC for
1227 * some reason)
1229 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1230 access("/sys/module/kvm_hv", F_OK)) {
1231 g_test_message("Skipping test: kvm_hv not available");
1232 return g_test_run();
1236 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1237 * there until the problems are resolved
1239 if (g_str_equal(qtest_get_arch(), "s390x")) {
1240 #if defined(HOST_S390X)
1241 if (access("/dev/kvm", R_OK | W_OK)) {
1242 g_test_message("Skipping test: kvm not available");
1243 return g_test_run();
1245 #else
1246 g_test_message("Skipping test: Need s390x host to work properly");
1247 return g_test_run();
1248 #endif
1251 tmpfs = mkdtemp(template);
1252 if (!tmpfs) {
1253 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1255 g_assert(tmpfs);
1257 module_call_init(MODULE_INIT_QOM);
1259 qtest_add_func("/migration/postcopy/unix", test_postcopy);
1260 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1261 qtest_add_func("/migration/deprecated", test_deprecated);
1262 qtest_add_func("/migration/bad_dest", test_baddest);
1263 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1264 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1265 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1266 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1267 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1268 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1269 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1270 qtest_add_func("/migration/validate_uuid_src_not_set",
1271 test_validate_uuid_src_not_set);
1272 qtest_add_func("/migration/validate_uuid_dst_not_set",
1273 test_validate_uuid_dst_not_set);
1275 ret = g_test_run();
1277 g_assert_cmpint(ret, ==, 0);
1279 ret = rmdir(tmpfs);
1280 if (ret != 0) {
1281 g_test_message("unable to rmdir: path (%s): %s",
1282 tmpfs, strerror(errno));
1285 return ret;