tests/qtest: massively speed up migration-test
[qemu/ar7.git] / tests / qtest / migration-test.c
blobefa8c729db7157ec8db56bf7f4a96cc5db69d5c0
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/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
26 #include "crypto/tlscredspsk.h"
27 #include "qapi/qmp/qlist.h"
29 #include "migration-helpers.h"
30 #include "tests/migration/migration-test.h"
31 #ifdef CONFIG_GNUTLS
32 # include "tests/unit/crypto-tls-psk-helpers.h"
33 # ifdef CONFIG_TASN1
34 # include "tests/unit/crypto-tls-x509-helpers.h"
35 # endif /* CONFIG_TASN1 */
36 #endif /* CONFIG_GNUTLS */
38 /* For dirty ring test; so far only x86_64 is supported */
39 #if defined(__linux__) && defined(HOST_X86_64)
40 #include "linux/kvm.h"
41 #endif
43 unsigned start_address;
44 unsigned end_address;
45 static bool uffd_feature_thread_id;
46 static bool got_src_stop;
47 static bool got_dst_resume;
50 * An initial 3 MB offset is used as that corresponds
51 * to ~1 sec of data transfer with our bandwidth setting.
53 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
55 * A further 1k is added to ensure we're not a multiple
56 * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
57 * from the migration guest workload.
59 #define MAGIC_OFFSET_SHUFFLE 1024
60 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
61 #define MAGIC_MARKER 0xFEED12345678CAFEULL
64 * Dirtylimit stop working if dirty page rate error
65 * value less than DIRTYLIMIT_TOLERANCE_RANGE
67 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
69 #if defined(__linux__)
70 #include <sys/syscall.h>
71 #include <sys/vfs.h>
72 #endif
74 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
75 #include <sys/eventfd.h>
76 #include <sys/ioctl.h>
77 #include "qemu/userfaultfd.h"
79 static bool ufd_version_check(void)
81 struct uffdio_api api_struct;
82 uint64_t ioctl_mask;
84 int ufd = uffd_open(O_CLOEXEC);
86 if (ufd == -1) {
87 g_test_message("Skipping test: userfaultfd not available");
88 return false;
91 api_struct.api = UFFD_API;
92 api_struct.features = 0;
93 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
94 g_test_message("Skipping test: UFFDIO_API failed");
95 return false;
97 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
99 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
100 (__u64)1 << _UFFDIO_UNREGISTER;
101 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
102 g_test_message("Skipping test: Missing userfault feature");
103 return false;
106 return true;
109 #else
110 static bool ufd_version_check(void)
112 g_test_message("Skipping test: Userfault not available (builtdtime)");
113 return false;
116 #endif
118 static char *tmpfs;
120 /* The boot file modifies memory area in [start_address, end_address)
121 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
123 #include "tests/migration/i386/a-b-bootblock.h"
124 #include "tests/migration/aarch64/a-b-kernel.h"
125 #include "tests/migration/s390x/a-b-bios.h"
127 static void init_bootfile(const char *bootpath, void *content, size_t len)
129 FILE *bootfile = fopen(bootpath, "wb");
131 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
132 fclose(bootfile);
136 * Wait for some output in the serial output file,
137 * we get an 'A' followed by an endless string of 'B's
138 * but on the destination we won't have the A.
140 static void wait_for_serial(const char *side)
142 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
143 FILE *serialfile = fopen(serialpath, "r");
144 const char *arch = qtest_get_arch();
145 int started = (strcmp(side, "src_serial") == 0 &&
146 strcmp(arch, "ppc64") == 0) ? 0 : 1;
148 do {
149 int readvalue = fgetc(serialfile);
151 if (!started) {
152 /* SLOF prints its banner before starting test,
153 * to ignore it, mark the start of the test with '_',
154 * ignore all characters until this marker
156 switch (readvalue) {
157 case '_':
158 started = 1;
159 break;
160 case EOF:
161 fseek(serialfile, 0, SEEK_SET);
162 usleep(1000);
163 break;
165 continue;
167 switch (readvalue) {
168 case 'A':
169 /* Fine */
170 break;
172 case 'B':
173 /* It's alive! */
174 fclose(serialfile);
175 return;
177 case EOF:
178 started = (strcmp(side, "src_serial") == 0 &&
179 strcmp(arch, "ppc64") == 0) ? 0 : 1;
180 fseek(serialfile, 0, SEEK_SET);
181 usleep(1000);
182 break;
184 default:
185 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
186 g_assert_not_reached();
188 } while (true);
192 * It's tricky to use qemu's migration event capability with qtest,
193 * events suddenly appearing confuse the qmp()/hmp() responses.
196 static int64_t read_ram_property_int(QTestState *who, const char *property)
198 QDict *rsp_return, *rsp_ram;
199 int64_t result;
201 rsp_return = migrate_query_not_failed(who);
202 if (!qdict_haskey(rsp_return, "ram")) {
203 /* Still in setup */
204 result = 0;
205 } else {
206 rsp_ram = qdict_get_qdict(rsp_return, "ram");
207 result = qdict_get_try_int(rsp_ram, property, 0);
209 qobject_unref(rsp_return);
210 return result;
213 static int64_t read_migrate_property_int(QTestState *who, const char *property)
215 QDict *rsp_return;
216 int64_t result;
218 rsp_return = migrate_query_not_failed(who);
219 result = qdict_get_try_int(rsp_return, property, 0);
220 qobject_unref(rsp_return);
221 return result;
224 static uint64_t get_migration_pass(QTestState *who)
226 return read_ram_property_int(who, "dirty-sync-count");
229 static void read_blocktime(QTestState *who)
231 QDict *rsp_return;
233 rsp_return = migrate_query_not_failed(who);
234 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
235 qobject_unref(rsp_return);
238 static void wait_for_migration_pass(QTestState *who)
240 uint64_t initial_pass = get_migration_pass(who);
241 uint64_t pass;
243 /* Wait for the 1st sync */
244 while (!got_src_stop && !initial_pass) {
245 usleep(1000);
246 initial_pass = get_migration_pass(who);
249 do {
250 usleep(1000);
251 pass = get_migration_pass(who);
252 } while (pass == initial_pass && !got_src_stop);
255 static void check_guests_ram(QTestState *who)
257 /* Our ASM test will have been incrementing one byte from each page from
258 * start_address to < end_address in order. This gives us a constraint
259 * that any page's byte should be equal or less than the previous pages
260 * byte (mod 256); and they should all be equal except for one transition
261 * at the point where we meet the incrementer. (We're running this with
262 * the guest stopped).
264 unsigned address;
265 uint8_t first_byte;
266 uint8_t last_byte;
267 bool hit_edge = false;
268 int bad = 0;
270 qtest_memread(who, start_address, &first_byte, 1);
271 last_byte = first_byte;
273 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
274 address += TEST_MEM_PAGE_SIZE)
276 uint8_t b;
277 qtest_memread(who, address, &b, 1);
278 if (b != last_byte) {
279 if (((b + 1) % 256) == last_byte && !hit_edge) {
280 /* This is OK, the guest stopped at the point of
281 * incrementing the previous page but didn't get
282 * to us yet.
284 hit_edge = true;
285 last_byte = b;
286 } else {
287 bad++;
288 if (bad <= 10) {
289 fprintf(stderr, "Memory content inconsistency at %x"
290 " first_byte = %x last_byte = %x current = %x"
291 " hit_edge = %x\n",
292 address, first_byte, last_byte, b, hit_edge);
297 if (bad >= 10) {
298 fprintf(stderr, "and in another %d pages", bad - 10);
300 g_assert(bad == 0);
303 static void cleanup(const char *filename)
305 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
307 unlink(path);
310 static char *SocketAddress_to_str(SocketAddress *addr)
312 switch (addr->type) {
313 case SOCKET_ADDRESS_TYPE_INET:
314 return g_strdup_printf("tcp:%s:%s",
315 addr->u.inet.host,
316 addr->u.inet.port);
317 case SOCKET_ADDRESS_TYPE_UNIX:
318 return g_strdup_printf("unix:%s",
319 addr->u.q_unix.path);
320 case SOCKET_ADDRESS_TYPE_FD:
321 return g_strdup_printf("fd:%s", addr->u.fd.str);
322 case SOCKET_ADDRESS_TYPE_VSOCK:
323 return g_strdup_printf("tcp:%s:%s",
324 addr->u.vsock.cid,
325 addr->u.vsock.port);
326 default:
327 return g_strdup("unknown address type");
331 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
333 QDict *rsp;
334 char *result;
335 SocketAddressList *addrs;
336 Visitor *iv = NULL;
337 QObject *object;
339 rsp = migrate_query(who);
340 object = qdict_get(rsp, parameter);
342 iv = qobject_input_visitor_new(object);
343 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
344 visit_free(iv);
346 /* we are only using a single address */
347 result = SocketAddress_to_str(addrs->value);
349 qapi_free_SocketAddressList(addrs);
350 qobject_unref(rsp);
351 return result;
354 static long long migrate_get_parameter_int(QTestState *who,
355 const char *parameter)
357 QDict *rsp;
358 long long result;
360 rsp = qtest_qmp_assert_success_ref(
361 who, "{ 'execute': 'query-migrate-parameters' }");
362 result = qdict_get_int(rsp, parameter);
363 qobject_unref(rsp);
364 return result;
367 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
368 long long value)
370 long long result;
372 result = migrate_get_parameter_int(who, parameter);
373 g_assert_cmpint(result, ==, value);
376 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
377 long long value)
379 qtest_qmp_assert_success(who,
380 "{ 'execute': 'migrate-set-parameters',"
381 "'arguments': { %s: %lld } }",
382 parameter, value);
383 migrate_check_parameter_int(who, parameter, value);
386 static char *migrate_get_parameter_str(QTestState *who,
387 const char *parameter)
389 QDict *rsp;
390 char *result;
392 rsp = qtest_qmp_assert_success_ref(
393 who, "{ 'execute': 'query-migrate-parameters' }");
394 result = g_strdup(qdict_get_str(rsp, parameter));
395 qobject_unref(rsp);
396 return result;
399 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
400 const char *value)
402 g_autofree char *result = migrate_get_parameter_str(who, parameter);
403 g_assert_cmpstr(result, ==, value);
406 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
407 const char *value)
409 qtest_qmp_assert_success(who,
410 "{ 'execute': 'migrate-set-parameters',"
411 "'arguments': { %s: %s } }",
412 parameter, value);
413 migrate_check_parameter_str(who, parameter, value);
416 static long long migrate_get_parameter_bool(QTestState *who,
417 const char *parameter)
419 QDict *rsp;
420 int result;
422 rsp = qtest_qmp_assert_success_ref(
423 who, "{ 'execute': 'query-migrate-parameters' }");
424 result = qdict_get_bool(rsp, parameter);
425 qobject_unref(rsp);
426 return !!result;
429 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
430 int value)
432 int result;
434 result = migrate_get_parameter_bool(who, parameter);
435 g_assert_cmpint(result, ==, value);
438 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
439 int value)
441 qtest_qmp_assert_success(who,
442 "{ 'execute': 'migrate-set-parameters',"
443 "'arguments': { %s: %i } }",
444 parameter, value);
445 migrate_check_parameter_bool(who, parameter, value);
448 static void migrate_ensure_non_converge(QTestState *who)
450 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
451 migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
452 migrate_set_parameter_int(who, "downtime-limit", 1);
455 static void migrate_ensure_converge(QTestState *who)
457 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
458 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
459 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
463 * Our goal is to ensure that we run a single full migration
464 * iteration, and also dirty memory, ensuring that at least
465 * one further iteration is required.
467 * We can't directly synchronize with the start of a migration
468 * so we have to apply some tricks monitoring memory that is
469 * transferred.
471 * Initially we set the migration bandwidth to an insanely
472 * low value, with tiny max downtime too. This basically
473 * guarantees migration will never complete.
475 * This will result in a test that is unacceptably slow though,
476 * so we can't let the entire migration pass run at this speed.
477 * Our intent is to let it run just long enough that we can
478 * prove data prior to the marker has been transferred *AND*
479 * also prove this transferred data is dirty again.
481 * Before migration starts, we write a 64-bit magic marker
482 * into a fixed location in the src VM RAM.
484 * Then watch dst memory until the marker appears. This is
485 * proof that start_address -> MAGIC_OFFSET_BASE has been
486 * transferred.
488 * Finally we go back to the source and read a byte just
489 * before the marker untill we see it flip in value. This
490 * is proof that start_address -> MAGIC_OFFSET_BASE
491 * is now dirty again.
493 * IOW, we're guaranteed at least a 2nd migration pass
494 * at this point.
496 * We can now let migration run at full speed to finish
497 * the test
499 static void migrate_prepare_for_dirty_mem(QTestState *from)
502 * The guest workflow iterates from start_address to
503 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
504 * bytes.
506 * IOW, if we write to mem at a point which is NOT
507 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
508 * conflict with the migration workflow.
510 * We put in a marker here, that we'll use to determine
511 * when the data has been transferred to the dst.
513 qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
516 static void migrate_wait_for_dirty_mem(QTestState *from,
517 QTestState *to)
519 uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
520 uint64_t marker_address = start_address + MAGIC_OFFSET;
521 uint8_t watch_byte;
524 * Wait for the MAGIC_MARKER to get transferred, as an
525 * indicator that a migration pass has made some known
526 * amount of progress.
528 do {
529 usleep(1000 * 10);
530 } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
533 * Now ensure that already transferred bytes are
534 * dirty again from the guest workload. Note the
535 * guest byte value will wrap around and by chance
536 * match the original watch_byte. This is harmless
537 * as we'll eventually see a different value if we
538 * keep watching
540 watch_byte = qtest_readb(from, watch_address);
541 do {
542 usleep(1000 * 10);
543 } while (qtest_readb(from, watch_address) == watch_byte);
547 static void migrate_pause(QTestState *who)
549 qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
552 static void migrate_continue(QTestState *who, const char *state)
554 qtest_qmp_assert_success(who,
555 "{ 'execute': 'migrate-continue',"
556 " 'arguments': { 'state': %s } }",
557 state);
560 static void migrate_recover(QTestState *who, const char *uri)
562 qtest_qmp_assert_success(who,
563 "{ 'execute': 'migrate-recover', "
564 " 'id': 'recover-cmd', "
565 " 'arguments': { 'uri': %s } }",
566 uri);
569 static void migrate_cancel(QTestState *who)
571 qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
574 static void migrate_set_capability(QTestState *who, const char *capability,
575 bool value)
577 qtest_qmp_assert_success(who,
578 "{ 'execute': 'migrate-set-capabilities',"
579 "'arguments': { "
580 "'capabilities': [ { "
581 "'capability': %s, 'state': %i } ] } }",
582 capability, value);
585 static void migrate_postcopy_start(QTestState *from, QTestState *to)
587 qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
589 if (!got_src_stop) {
590 qtest_qmp_eventwait(from, "STOP");
593 qtest_qmp_eventwait(to, "RESUME");
596 typedef struct {
598 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
599 * unconditionally, because it means the user would like to be verbose.
601 bool hide_stderr;
602 bool use_shmem;
603 /* only launch the target process */
604 bool only_target;
605 /* Use dirty ring if true; dirty logging otherwise */
606 bool use_dirty_ring;
607 const char *opts_source;
608 const char *opts_target;
609 } MigrateStart;
612 * A hook that runs after the src and dst QEMUs have been
613 * created, but before the migration is started. This can
614 * be used to set migration parameters and capabilities.
616 * Returns: NULL, or a pointer to opaque state to be
617 * later passed to the TestMigrateFinishHook
619 typedef void * (*TestMigrateStartHook)(QTestState *from,
620 QTestState *to);
623 * A hook that runs after the migration has finished,
624 * regardless of whether it succeeded or failed, but
625 * before QEMU has terminated (unless it self-terminated
626 * due to migration error)
628 * @opaque is a pointer to state previously returned
629 * by the TestMigrateStartHook if any, or NULL.
631 typedef void (*TestMigrateFinishHook)(QTestState *from,
632 QTestState *to,
633 void *opaque);
635 typedef struct {
636 /* Optional: fine tune start parameters */
637 MigrateStart start;
639 /* Required: the URI for the dst QEMU to listen on */
640 const char *listen_uri;
643 * Optional: the URI for the src QEMU to connect to
644 * If NULL, then it will query the dst QEMU for its actual
645 * listening address and use that as the connect address.
646 * This allows for dynamically picking a free TCP port.
648 const char *connect_uri;
650 /* Optional: callback to run at start to set migration parameters */
651 TestMigrateStartHook start_hook;
652 /* Optional: callback to run at finish to cleanup */
653 TestMigrateFinishHook finish_hook;
656 * Optional: normally we expect the migration process to complete.
658 * There can be a variety of reasons and stages in which failure
659 * can happen during tests.
661 * If a failure is expected to happen at time of establishing
662 * the connection, then MIG_TEST_FAIL will indicate that the dst
663 * QEMU is expected to stay running and accept future migration
664 * connections.
666 * If a failure is expected to happen while processing the
667 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
668 * that the dst QEMU is expected to quit with non-zero exit status
670 enum {
671 /* This test should succeed, the default */
672 MIG_TEST_SUCCEED = 0,
673 /* This test should fail, dest qemu should keep alive */
674 MIG_TEST_FAIL,
675 /* This test should fail, dest qemu should fail with abnormal status */
676 MIG_TEST_FAIL_DEST_QUIT_ERR,
677 } result;
680 * Optional: set number of migration passes to wait for, if live==true.
681 * If zero, then merely wait for a few MB of dirty data
683 unsigned int iterations;
686 * Optional: whether the guest CPUs should be running during a precopy
687 * migration test. We used to always run with live but it took much
688 * longer so we reduced live tests to only the ones that have solid
689 * reason to be tested live-only. For each of the new test cases for
690 * precopy please provide justifications to use live explicitly (please
691 * refer to existing ones with live=true), or use live=off by default.
693 bool live;
695 /* Postcopy specific fields */
696 void *postcopy_data;
697 bool postcopy_preempt;
698 } MigrateCommon;
700 static int test_migrate_start(QTestState **from, QTestState **to,
701 const char *uri, MigrateStart *args)
703 g_autofree gchar *arch_source = NULL;
704 g_autofree gchar *arch_target = NULL;
705 g_autofree gchar *cmd_source = NULL;
706 g_autofree gchar *cmd_target = NULL;
707 const gchar *ignore_stderr;
708 g_autofree char *bootpath = NULL;
709 g_autofree char *shmem_opts = NULL;
710 g_autofree char *shmem_path = NULL;
711 const char *arch = qtest_get_arch();
712 const char *machine_opts = NULL;
713 const char *memory_size;
715 if (args->use_shmem) {
716 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
717 g_test_skip("/dev/shm is not supported");
718 return -1;
722 got_src_stop = false;
723 got_dst_resume = false;
724 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
725 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
726 /* the assembled x86 boot sector should be exactly one sector large */
727 assert(sizeof(x86_bootsect) == 512);
728 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
729 memory_size = "150M";
730 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
731 arch_target = g_strdup(arch_source);
732 start_address = X86_TEST_MEM_START;
733 end_address = X86_TEST_MEM_END;
734 } else if (g_str_equal(arch, "s390x")) {
735 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
736 memory_size = "128M";
737 arch_source = g_strdup_printf("-bios %s", bootpath);
738 arch_target = g_strdup(arch_source);
739 start_address = S390_TEST_MEM_START;
740 end_address = S390_TEST_MEM_END;
741 } else if (strcmp(arch, "ppc64") == 0) {
742 machine_opts = "vsmt=8";
743 memory_size = "256M";
744 start_address = PPC_TEST_MEM_START;
745 end_address = PPC_TEST_MEM_END;
746 arch_source = g_strdup_printf("-nodefaults "
747 "-prom-env 'use-nvramrc?=true' -prom-env "
748 "'nvramrc=hex .\" _\" begin %x %x "
749 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
750 "until'", end_address, start_address);
751 arch_target = g_strdup("");
752 } else if (strcmp(arch, "aarch64") == 0) {
753 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
754 machine_opts = "virt,gic-version=max";
755 memory_size = "150M";
756 arch_source = g_strdup_printf("-cpu max "
757 "-kernel %s",
758 bootpath);
759 arch_target = g_strdup(arch_source);
760 start_address = ARM_TEST_MEM_START;
761 end_address = ARM_TEST_MEM_END;
763 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
764 } else {
765 g_assert_not_reached();
768 if (!getenv("QTEST_LOG") && args->hide_stderr) {
769 #ifndef _WIN32
770 ignore_stderr = "2>/dev/null";
771 #else
773 * On Windows the QEMU executable is created via CreateProcess() and
774 * IO redirection does not work, so don't bother adding IO redirection
775 * to the command line.
777 ignore_stderr = "";
778 #endif
779 } else {
780 ignore_stderr = "";
783 if (args->use_shmem) {
784 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
785 shmem_opts = g_strdup_printf(
786 "-object memory-backend-file,id=mem0,size=%s"
787 ",mem-path=%s,share=on -numa node,memdev=mem0",
788 memory_size, shmem_path);
789 } else {
790 shmem_path = NULL;
791 shmem_opts = g_strdup("");
794 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
795 "-name source,debug-threads=on "
796 "-m %s "
797 "-serial file:%s/src_serial "
798 "%s %s %s %s",
799 args->use_dirty_ring ?
800 ",dirty-ring-size=4096" : "",
801 machine_opts ? " -machine " : "",
802 machine_opts ? machine_opts : "",
803 memory_size, tmpfs,
804 arch_source, shmem_opts,
805 args->opts_source ? args->opts_source : "",
806 ignore_stderr);
807 if (!args->only_target) {
808 *from = qtest_init(cmd_source);
809 qtest_qmp_set_event_callback(*from,
810 migrate_watch_for_stop,
811 &got_src_stop);
814 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
815 "-name target,debug-threads=on "
816 "-m %s "
817 "-serial file:%s/dest_serial "
818 "-incoming %s "
819 "%s %s %s %s",
820 args->use_dirty_ring ?
821 ",dirty-ring-size=4096" : "",
822 machine_opts ? " -machine " : "",
823 machine_opts ? machine_opts : "",
824 memory_size, tmpfs, uri,
825 arch_target, shmem_opts,
826 args->opts_target ? args->opts_target : "",
827 ignore_stderr);
828 *to = qtest_init(cmd_target);
829 qtest_qmp_set_event_callback(*to,
830 migrate_watch_for_resume,
831 &got_dst_resume);
834 * Remove shmem file immediately to avoid memory leak in test failed case.
835 * It's valid becase QEMU has already opened this file
837 if (args->use_shmem) {
838 unlink(shmem_path);
841 return 0;
844 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
846 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
848 qtest_quit(from);
850 if (test_dest) {
851 qtest_memread(to, start_address, &dest_byte_a, 1);
853 /* Destination still running, wait for a byte to change */
854 do {
855 qtest_memread(to, start_address, &dest_byte_b, 1);
856 usleep(1000 * 10);
857 } while (dest_byte_a == dest_byte_b);
859 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
861 /* With it stopped, check nothing changes */
862 qtest_memread(to, start_address, &dest_byte_c, 1);
863 usleep(1000 * 200);
864 qtest_memread(to, start_address, &dest_byte_d, 1);
865 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
867 check_guests_ram(to);
870 qtest_quit(to);
872 cleanup("bootsect");
873 cleanup("migsocket");
874 cleanup("src_serial");
875 cleanup("dest_serial");
878 #ifdef CONFIG_GNUTLS
879 struct TestMigrateTLSPSKData {
880 char *workdir;
881 char *workdiralt;
882 char *pskfile;
883 char *pskfilealt;
886 static void *
887 test_migrate_tls_psk_start_common(QTestState *from,
888 QTestState *to,
889 bool mismatch)
891 struct TestMigrateTLSPSKData *data =
892 g_new0(struct TestMigrateTLSPSKData, 1);
894 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
895 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
896 QCRYPTO_TLS_CREDS_PSKFILE);
897 g_mkdir_with_parents(data->workdir, 0700);
898 test_tls_psk_init(data->pskfile);
900 if (mismatch) {
901 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
902 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
903 QCRYPTO_TLS_CREDS_PSKFILE);
904 g_mkdir_with_parents(data->workdiralt, 0700);
905 test_tls_psk_init_alt(data->pskfilealt);
908 qtest_qmp_assert_success(from,
909 "{ 'execute': 'object-add',"
910 " 'arguments': { 'qom-type': 'tls-creds-psk',"
911 " 'id': 'tlscredspsk0',"
912 " 'endpoint': 'client',"
913 " 'dir': %s,"
914 " 'username': 'qemu'} }",
915 data->workdir);
917 qtest_qmp_assert_success(to,
918 "{ 'execute': 'object-add',"
919 " 'arguments': { 'qom-type': 'tls-creds-psk',"
920 " 'id': 'tlscredspsk0',"
921 " 'endpoint': 'server',"
922 " 'dir': %s } }",
923 mismatch ? data->workdiralt : data->workdir);
925 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
926 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
928 return data;
931 static void *
932 test_migrate_tls_psk_start_match(QTestState *from,
933 QTestState *to)
935 return test_migrate_tls_psk_start_common(from, to, false);
938 static void *
939 test_migrate_tls_psk_start_mismatch(QTestState *from,
940 QTestState *to)
942 return test_migrate_tls_psk_start_common(from, to, true);
945 static void
946 test_migrate_tls_psk_finish(QTestState *from,
947 QTestState *to,
948 void *opaque)
950 struct TestMigrateTLSPSKData *data = opaque;
952 test_tls_psk_cleanup(data->pskfile);
953 if (data->pskfilealt) {
954 test_tls_psk_cleanup(data->pskfilealt);
956 rmdir(data->workdir);
957 if (data->workdiralt) {
958 rmdir(data->workdiralt);
961 g_free(data->workdiralt);
962 g_free(data->pskfilealt);
963 g_free(data->workdir);
964 g_free(data->pskfile);
965 g_free(data);
968 #ifdef CONFIG_TASN1
969 typedef struct {
970 char *workdir;
971 char *keyfile;
972 char *cacert;
973 char *servercert;
974 char *serverkey;
975 char *clientcert;
976 char *clientkey;
977 } TestMigrateTLSX509Data;
979 typedef struct {
980 bool verifyclient;
981 bool clientcert;
982 bool hostileclient;
983 bool authzclient;
984 const char *certhostname;
985 const char *certipaddr;
986 } TestMigrateTLSX509;
988 static void *
989 test_migrate_tls_x509_start_common(QTestState *from,
990 QTestState *to,
991 TestMigrateTLSX509 *args)
993 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
995 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
996 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
998 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
999 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
1000 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
1001 if (args->clientcert) {
1002 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
1003 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
1006 g_mkdir_with_parents(data->workdir, 0700);
1008 test_tls_init(data->keyfile);
1009 #ifndef _WIN32
1010 g_assert(link(data->keyfile, data->serverkey) == 0);
1011 #else
1012 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
1013 #endif
1014 if (args->clientcert) {
1015 #ifndef _WIN32
1016 g_assert(link(data->keyfile, data->clientkey) == 0);
1017 #else
1018 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
1019 #endif
1022 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
1023 if (args->clientcert) {
1024 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
1025 args->hostileclient ?
1026 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
1027 QCRYPTO_TLS_TEST_CLIENT_NAME,
1028 data->clientcert);
1031 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
1032 data->servercert,
1033 args->certhostname,
1034 args->certipaddr);
1036 qtest_qmp_assert_success(from,
1037 "{ 'execute': 'object-add',"
1038 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1039 " 'id': 'tlscredsx509client0',"
1040 " 'endpoint': 'client',"
1041 " 'dir': %s,"
1042 " 'sanity-check': true,"
1043 " 'verify-peer': true} }",
1044 data->workdir);
1045 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
1046 if (args->certhostname) {
1047 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
1050 qtest_qmp_assert_success(to,
1051 "{ 'execute': 'object-add',"
1052 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1053 " 'id': 'tlscredsx509server0',"
1054 " 'endpoint': 'server',"
1055 " 'dir': %s,"
1056 " 'sanity-check': true,"
1057 " 'verify-peer': %i} }",
1058 data->workdir, args->verifyclient);
1059 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
1061 if (args->authzclient) {
1062 qtest_qmp_assert_success(to,
1063 "{ 'execute': 'object-add',"
1064 " 'arguments': { 'qom-type': 'authz-simple',"
1065 " 'id': 'tlsauthz0',"
1066 " 'identity': %s} }",
1067 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
1068 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
1071 return data;
1075 * The normal case: match server's cert hostname against
1076 * whatever host we were telling QEMU to connect to (if any)
1078 static void *
1079 test_migrate_tls_x509_start_default_host(QTestState *from,
1080 QTestState *to)
1082 TestMigrateTLSX509 args = {
1083 .verifyclient = true,
1084 .clientcert = true,
1085 .certipaddr = "127.0.0.1"
1087 return test_migrate_tls_x509_start_common(from, to, &args);
1091 * The unusual case: the server's cert is different from
1092 * the address we're telling QEMU to connect to (if any),
1093 * so we must give QEMU an explicit hostname to validate
1095 static void *
1096 test_migrate_tls_x509_start_override_host(QTestState *from,
1097 QTestState *to)
1099 TestMigrateTLSX509 args = {
1100 .verifyclient = true,
1101 .clientcert = true,
1102 .certhostname = "qemu.org",
1104 return test_migrate_tls_x509_start_common(from, to, &args);
1108 * The unusual case: the server's cert is different from
1109 * the address we're telling QEMU to connect to, and so we
1110 * expect the client to reject the server
1112 static void *
1113 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1114 QTestState *to)
1116 TestMigrateTLSX509 args = {
1117 .verifyclient = true,
1118 .clientcert = true,
1119 .certipaddr = "10.0.0.1",
1121 return test_migrate_tls_x509_start_common(from, to, &args);
1124 static void *
1125 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1126 QTestState *to)
1128 TestMigrateTLSX509 args = {
1129 .verifyclient = true,
1130 .clientcert = true,
1131 .authzclient = true,
1132 .certipaddr = "127.0.0.1",
1134 return test_migrate_tls_x509_start_common(from, to, &args);
1137 static void *
1138 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1139 QTestState *to)
1141 TestMigrateTLSX509 args = {
1142 .verifyclient = true,
1143 .clientcert = true,
1144 .hostileclient = true,
1145 .authzclient = true,
1146 .certipaddr = "127.0.0.1",
1148 return test_migrate_tls_x509_start_common(from, to, &args);
1152 * The case with no client certificate presented,
1153 * and no server verification
1155 static void *
1156 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1157 QTestState *to)
1159 TestMigrateTLSX509 args = {
1160 .certipaddr = "127.0.0.1",
1162 return test_migrate_tls_x509_start_common(from, to, &args);
1166 * The case with no client certificate presented,
1167 * and server verification rejecting
1169 static void *
1170 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1171 QTestState *to)
1173 TestMigrateTLSX509 args = {
1174 .verifyclient = true,
1175 .certipaddr = "127.0.0.1",
1177 return test_migrate_tls_x509_start_common(from, to, &args);
1180 static void
1181 test_migrate_tls_x509_finish(QTestState *from,
1182 QTestState *to,
1183 void *opaque)
1185 TestMigrateTLSX509Data *data = opaque;
1187 test_tls_cleanup(data->keyfile);
1188 g_free(data->keyfile);
1190 unlink(data->cacert);
1191 g_free(data->cacert);
1192 unlink(data->servercert);
1193 g_free(data->servercert);
1194 unlink(data->serverkey);
1195 g_free(data->serverkey);
1197 if (data->clientcert) {
1198 unlink(data->clientcert);
1199 g_free(data->clientcert);
1201 if (data->clientkey) {
1202 unlink(data->clientkey);
1203 g_free(data->clientkey);
1206 rmdir(data->workdir);
1207 g_free(data->workdir);
1209 g_free(data);
1211 #endif /* CONFIG_TASN1 */
1212 #endif /* CONFIG_GNUTLS */
1214 static void *
1215 test_migrate_compress_start(QTestState *from,
1216 QTestState *to)
1218 migrate_set_parameter_int(from, "compress-level", 1);
1219 migrate_set_parameter_int(from, "compress-threads", 4);
1220 migrate_set_parameter_bool(from, "compress-wait-thread", true);
1221 migrate_set_parameter_int(to, "decompress-threads", 4);
1223 migrate_set_capability(from, "compress", true);
1224 migrate_set_capability(to, "compress", true);
1226 return NULL;
1229 static void *
1230 test_migrate_compress_nowait_start(QTestState *from,
1231 QTestState *to)
1233 migrate_set_parameter_int(from, "compress-level", 9);
1234 migrate_set_parameter_int(from, "compress-threads", 1);
1235 migrate_set_parameter_bool(from, "compress-wait-thread", false);
1236 migrate_set_parameter_int(to, "decompress-threads", 1);
1238 migrate_set_capability(from, "compress", true);
1239 migrate_set_capability(to, "compress", true);
1241 return NULL;
1244 static int migrate_postcopy_prepare(QTestState **from_ptr,
1245 QTestState **to_ptr,
1246 MigrateCommon *args)
1248 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1249 QTestState *from, *to;
1251 if (test_migrate_start(&from, &to, uri, &args->start)) {
1252 return -1;
1255 if (args->start_hook) {
1256 args->postcopy_data = args->start_hook(from, to);
1259 migrate_set_capability(from, "postcopy-ram", true);
1260 migrate_set_capability(to, "postcopy-ram", true);
1261 migrate_set_capability(to, "postcopy-blocktime", true);
1263 if (args->postcopy_preempt) {
1264 migrate_set_capability(from, "postcopy-preempt", true);
1265 migrate_set_capability(to, "postcopy-preempt", true);
1268 migrate_ensure_non_converge(from);
1270 migrate_prepare_for_dirty_mem(from);
1272 /* Wait for the first serial output from the source */
1273 wait_for_serial("src_serial");
1275 migrate_qmp(from, uri, "{}");
1277 migrate_wait_for_dirty_mem(from, to);
1279 *from_ptr = from;
1280 *to_ptr = to;
1282 return 0;
1285 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1286 MigrateCommon *args)
1288 wait_for_migration_complete(from);
1290 /* Make sure we get at least one "B" on destination */
1291 wait_for_serial("dest_serial");
1293 if (uffd_feature_thread_id) {
1294 read_blocktime(to);
1297 if (args->finish_hook) {
1298 args->finish_hook(from, to, args->postcopy_data);
1299 args->postcopy_data = NULL;
1302 test_migrate_end(from, to, true);
1305 static void test_postcopy_common(MigrateCommon *args)
1307 QTestState *from, *to;
1309 if (migrate_postcopy_prepare(&from, &to, args)) {
1310 return;
1312 migrate_postcopy_start(from, to);
1313 migrate_postcopy_complete(from, to, args);
1316 static void test_postcopy(void)
1318 MigrateCommon args = { };
1320 test_postcopy_common(&args);
1323 static void test_postcopy_compress(void)
1325 MigrateCommon args = {
1326 .start_hook = test_migrate_compress_start
1329 test_postcopy_common(&args);
1332 static void test_postcopy_preempt(void)
1334 MigrateCommon args = {
1335 .postcopy_preempt = true,
1338 test_postcopy_common(&args);
1341 #ifdef CONFIG_GNUTLS
1342 static void test_postcopy_tls_psk(void)
1344 MigrateCommon args = {
1345 .start_hook = test_migrate_tls_psk_start_match,
1346 .finish_hook = test_migrate_tls_psk_finish,
1349 test_postcopy_common(&args);
1352 static void test_postcopy_preempt_tls_psk(void)
1354 MigrateCommon args = {
1355 .postcopy_preempt = true,
1356 .start_hook = test_migrate_tls_psk_start_match,
1357 .finish_hook = test_migrate_tls_psk_finish,
1360 test_postcopy_common(&args);
1362 #endif
1364 static void test_postcopy_recovery_common(MigrateCommon *args)
1366 QTestState *from, *to;
1367 g_autofree char *uri = NULL;
1369 /* Always hide errors for postcopy recover tests since they're expected */
1370 args->start.hide_stderr = true;
1372 if (migrate_postcopy_prepare(&from, &to, args)) {
1373 return;
1376 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1377 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1379 /* Now we start the postcopy */
1380 migrate_postcopy_start(from, to);
1383 * Wait until postcopy is really started; we can only run the
1384 * migrate-pause command during a postcopy
1386 wait_for_migration_status(from, "postcopy-active", NULL);
1389 * Manually stop the postcopy migration. This emulates a network
1390 * failure with the migration socket
1392 migrate_pause(from);
1395 * Wait for destination side to reach postcopy-paused state. The
1396 * migrate-recover command can only succeed if destination machine
1397 * is in the paused state
1399 wait_for_migration_status(to, "postcopy-paused",
1400 (const char * []) { "failed", "active",
1401 "completed", NULL });
1404 * Create a new socket to emulate a new channel that is different
1405 * from the broken migration channel; tell the destination to
1406 * listen to the new port
1408 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1409 migrate_recover(to, uri);
1412 * Try to rebuild the migration channel using the resume flag and
1413 * the newly created channel
1415 wait_for_migration_status(from, "postcopy-paused",
1416 (const char * []) { "failed", "active",
1417 "completed", NULL });
1418 migrate_qmp(from, uri, "{'resume': true}");
1420 /* Restore the postcopy bandwidth to unlimited */
1421 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1423 migrate_postcopy_complete(from, to, args);
1426 static void test_postcopy_recovery(void)
1428 MigrateCommon args = { };
1430 test_postcopy_recovery_common(&args);
1433 static void test_postcopy_recovery_compress(void)
1435 MigrateCommon args = {
1436 .start_hook = test_migrate_compress_start
1439 test_postcopy_recovery_common(&args);
1442 #ifdef CONFIG_GNUTLS
1443 static void test_postcopy_recovery_tls_psk(void)
1445 MigrateCommon args = {
1446 .start_hook = test_migrate_tls_psk_start_match,
1447 .finish_hook = test_migrate_tls_psk_finish,
1450 test_postcopy_recovery_common(&args);
1452 #endif
1454 static void test_postcopy_preempt_recovery(void)
1456 MigrateCommon args = {
1457 .postcopy_preempt = true,
1460 test_postcopy_recovery_common(&args);
1463 #ifdef CONFIG_GNUTLS
1464 /* This contains preempt+recovery+tls test altogether */
1465 static void test_postcopy_preempt_all(void)
1467 MigrateCommon args = {
1468 .postcopy_preempt = true,
1469 .start_hook = test_migrate_tls_psk_start_match,
1470 .finish_hook = test_migrate_tls_psk_finish,
1473 test_postcopy_recovery_common(&args);
1476 #endif
1478 static void test_baddest(void)
1480 MigrateStart args = {
1481 .hide_stderr = true
1483 QTestState *from, *to;
1485 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1486 return;
1488 migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1489 wait_for_migration_fail(from, false);
1490 test_migrate_end(from, to, false);
1493 static void test_precopy_common(MigrateCommon *args)
1495 QTestState *from, *to;
1496 void *data_hook = NULL;
1498 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1499 return;
1502 if (args->start_hook) {
1503 data_hook = args->start_hook(from, to);
1506 /* Wait for the first serial output from the source */
1507 if (args->result == MIG_TEST_SUCCEED) {
1508 wait_for_serial("src_serial");
1511 if (args->live) {
1512 migrate_ensure_non_converge(from);
1513 migrate_prepare_for_dirty_mem(from);
1514 } else {
1516 * Testing non-live migration, we allow it to run at
1517 * full speed to ensure short test case duration.
1518 * For tests expected to fail, we don't need to
1519 * change anything.
1521 if (args->result == MIG_TEST_SUCCEED) {
1522 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1523 if (!got_src_stop) {
1524 qtest_qmp_eventwait(from, "STOP");
1526 migrate_ensure_converge(from);
1530 if (!args->connect_uri) {
1531 g_autofree char *local_connect_uri =
1532 migrate_get_socket_address(to, "socket-address");
1533 migrate_qmp(from, local_connect_uri, "{}");
1534 } else {
1535 migrate_qmp(from, args->connect_uri, "{}");
1539 if (args->result != MIG_TEST_SUCCEED) {
1540 bool allow_active = args->result == MIG_TEST_FAIL;
1541 wait_for_migration_fail(from, allow_active);
1543 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1544 qtest_set_expected_status(to, EXIT_FAILURE);
1546 } else {
1547 if (args->live) {
1549 * For initial iteration(s) we must do a full pass,
1550 * but for the final iteration, we need only wait
1551 * for some dirty mem before switching to converge
1553 while (args->iterations > 1) {
1554 wait_for_migration_pass(from);
1555 args->iterations--;
1557 migrate_wait_for_dirty_mem(from, to);
1559 migrate_ensure_converge(from);
1562 * We do this first, as it has a timeout to stop us
1563 * hanging forever if migration didn't converge
1565 wait_for_migration_complete(from);
1567 if (!got_src_stop) {
1568 qtest_qmp_eventwait(from, "STOP");
1570 } else {
1571 wait_for_migration_complete(from);
1573 * Must wait for dst to finish reading all incoming
1574 * data on the socket before issuing 'cont' otherwise
1575 * it'll be ignored
1577 wait_for_migration_complete(to);
1579 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1582 if (!got_dst_resume) {
1583 qtest_qmp_eventwait(to, "RESUME");
1586 wait_for_serial("dest_serial");
1589 if (args->finish_hook) {
1590 args->finish_hook(from, to, data_hook);
1593 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1596 static void test_precopy_unix_plain(void)
1598 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1599 MigrateCommon args = {
1600 .listen_uri = uri,
1601 .connect_uri = uri,
1603 * The simplest use case of precopy, covering smoke tests of
1604 * get-dirty-log dirty tracking.
1606 .live = true,
1609 test_precopy_common(&args);
1613 static void test_precopy_unix_dirty_ring(void)
1615 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1616 MigrateCommon args = {
1617 .start = {
1618 .use_dirty_ring = true,
1620 .listen_uri = uri,
1621 .connect_uri = uri,
1623 * Besides the precopy/unix basic test, cover dirty ring interface
1624 * rather than get-dirty-log.
1626 .live = true,
1629 test_precopy_common(&args);
1632 #ifdef CONFIG_GNUTLS
1633 static void test_precopy_unix_tls_psk(void)
1635 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1636 MigrateCommon args = {
1637 .connect_uri = uri,
1638 .listen_uri = uri,
1639 .start_hook = test_migrate_tls_psk_start_match,
1640 .finish_hook = test_migrate_tls_psk_finish,
1643 test_precopy_common(&args);
1646 #ifdef CONFIG_TASN1
1647 static void test_precopy_unix_tls_x509_default_host(void)
1649 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1650 MigrateCommon args = {
1651 .start = {
1652 .hide_stderr = true,
1654 .connect_uri = uri,
1655 .listen_uri = uri,
1656 .start_hook = test_migrate_tls_x509_start_default_host,
1657 .finish_hook = test_migrate_tls_x509_finish,
1658 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1661 test_precopy_common(&args);
1664 static void test_precopy_unix_tls_x509_override_host(void)
1666 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1667 MigrateCommon args = {
1668 .connect_uri = uri,
1669 .listen_uri = uri,
1670 .start_hook = test_migrate_tls_x509_start_override_host,
1671 .finish_hook = test_migrate_tls_x509_finish,
1674 test_precopy_common(&args);
1676 #endif /* CONFIG_TASN1 */
1677 #endif /* CONFIG_GNUTLS */
1679 #if 0
1680 /* Currently upset on aarch64 TCG */
1681 static void test_ignore_shared(void)
1683 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1684 QTestState *from, *to;
1686 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1687 return;
1690 migrate_ensure_non_converge(from);
1691 migrate_prepare_for_dirty_mem(from);
1693 migrate_set_capability(from, "x-ignore-shared", true);
1694 migrate_set_capability(to, "x-ignore-shared", true);
1696 /* Wait for the first serial output from the source */
1697 wait_for_serial("src_serial");
1699 migrate_qmp(from, uri, "{}");
1701 migrate_wait_for_dirty_mem(from, to);
1703 if (!got_src_stop) {
1704 qtest_qmp_eventwait(from, "STOP");
1707 qtest_qmp_eventwait(to, "RESUME");
1709 wait_for_serial("dest_serial");
1710 wait_for_migration_complete(from);
1712 /* Check whether shared RAM has been really skipped */
1713 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1715 test_migrate_end(from, to, true);
1717 #endif
1719 static void *
1720 test_migrate_xbzrle_start(QTestState *from,
1721 QTestState *to)
1723 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1725 migrate_set_capability(from, "xbzrle", true);
1726 migrate_set_capability(to, "xbzrle", true);
1728 return NULL;
1731 static void test_precopy_unix_xbzrle(void)
1733 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1734 MigrateCommon args = {
1735 .connect_uri = uri,
1736 .listen_uri = uri,
1737 .start_hook = test_migrate_xbzrle_start,
1738 .iterations = 2,
1740 * XBZRLE needs pages to be modified when doing the 2nd+ round
1741 * iteration to have real data pushed to the stream.
1743 .live = true,
1746 test_precopy_common(&args);
1749 static void test_precopy_unix_compress(void)
1751 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1752 MigrateCommon args = {
1753 .connect_uri = uri,
1754 .listen_uri = uri,
1755 .start_hook = test_migrate_compress_start,
1757 * Test that no invalid thread state is left over from
1758 * the previous iteration.
1760 .iterations = 2,
1762 * We make sure the compressor can always work well even if guest
1763 * memory is changing. See commit 34ab9e9743 where we used to fix
1764 * a bug when only trigger-able with guest memory changing.
1766 .live = true,
1769 test_precopy_common(&args);
1772 static void test_precopy_unix_compress_nowait(void)
1774 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1775 MigrateCommon args = {
1776 .connect_uri = uri,
1777 .listen_uri = uri,
1778 .start_hook = test_migrate_compress_nowait_start,
1780 * Test that no invalid thread state is left over from
1781 * the previous iteration.
1783 .iterations = 2,
1784 /* Same reason for the wait version of precopy compress test */
1785 .live = true,
1788 test_precopy_common(&args);
1791 static void test_precopy_tcp_plain(void)
1793 MigrateCommon args = {
1794 .listen_uri = "tcp:127.0.0.1:0",
1797 test_precopy_common(&args);
1800 static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to)
1803 migrate_set_capability(from, "return-path", true);
1804 migrate_set_capability(to, "return-path", true);
1806 migrate_set_capability(from, "switchover-ack", true);
1807 migrate_set_capability(to, "switchover-ack", true);
1809 return NULL;
1812 static void test_precopy_tcp_switchover_ack(void)
1814 MigrateCommon args = {
1815 .listen_uri = "tcp:127.0.0.1:0",
1816 .start_hook = test_migrate_switchover_ack_start,
1818 * Source VM must be running in order to consider the switchover ACK
1819 * when deciding to do switchover or not.
1821 .live = true,
1824 test_precopy_common(&args);
1827 #ifdef CONFIG_GNUTLS
1828 static void test_precopy_tcp_tls_psk_match(void)
1830 MigrateCommon args = {
1831 .listen_uri = "tcp:127.0.0.1:0",
1832 .start_hook = test_migrate_tls_psk_start_match,
1833 .finish_hook = test_migrate_tls_psk_finish,
1836 test_precopy_common(&args);
1839 static void test_precopy_tcp_tls_psk_mismatch(void)
1841 MigrateCommon args = {
1842 .start = {
1843 .hide_stderr = true,
1845 .listen_uri = "tcp:127.0.0.1:0",
1846 .start_hook = test_migrate_tls_psk_start_mismatch,
1847 .finish_hook = test_migrate_tls_psk_finish,
1848 .result = MIG_TEST_FAIL,
1851 test_precopy_common(&args);
1854 #ifdef CONFIG_TASN1
1855 static void test_precopy_tcp_tls_x509_default_host(void)
1857 MigrateCommon args = {
1858 .listen_uri = "tcp:127.0.0.1:0",
1859 .start_hook = test_migrate_tls_x509_start_default_host,
1860 .finish_hook = test_migrate_tls_x509_finish,
1863 test_precopy_common(&args);
1866 static void test_precopy_tcp_tls_x509_override_host(void)
1868 MigrateCommon args = {
1869 .listen_uri = "tcp:127.0.0.1:0",
1870 .start_hook = test_migrate_tls_x509_start_override_host,
1871 .finish_hook = test_migrate_tls_x509_finish,
1874 test_precopy_common(&args);
1877 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1879 MigrateCommon args = {
1880 .start = {
1881 .hide_stderr = true,
1883 .listen_uri = "tcp:127.0.0.1:0",
1884 .start_hook = test_migrate_tls_x509_start_mismatch_host,
1885 .finish_hook = test_migrate_tls_x509_finish,
1886 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1889 test_precopy_common(&args);
1892 static void test_precopy_tcp_tls_x509_friendly_client(void)
1894 MigrateCommon args = {
1895 .listen_uri = "tcp:127.0.0.1:0",
1896 .start_hook = test_migrate_tls_x509_start_friendly_client,
1897 .finish_hook = test_migrate_tls_x509_finish,
1900 test_precopy_common(&args);
1903 static void test_precopy_tcp_tls_x509_hostile_client(void)
1905 MigrateCommon args = {
1906 .start = {
1907 .hide_stderr = true,
1909 .listen_uri = "tcp:127.0.0.1:0",
1910 .start_hook = test_migrate_tls_x509_start_hostile_client,
1911 .finish_hook = test_migrate_tls_x509_finish,
1912 .result = MIG_TEST_FAIL,
1915 test_precopy_common(&args);
1918 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1920 MigrateCommon args = {
1921 .listen_uri = "tcp:127.0.0.1:0",
1922 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1923 .finish_hook = test_migrate_tls_x509_finish,
1926 test_precopy_common(&args);
1929 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1931 MigrateCommon args = {
1932 .start = {
1933 .hide_stderr = true,
1935 .listen_uri = "tcp:127.0.0.1:0",
1936 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1937 .finish_hook = test_migrate_tls_x509_finish,
1938 .result = MIG_TEST_FAIL,
1941 test_precopy_common(&args);
1943 #endif /* CONFIG_TASN1 */
1944 #endif /* CONFIG_GNUTLS */
1946 #ifndef _WIN32
1947 static void *test_migrate_fd_start_hook(QTestState *from,
1948 QTestState *to)
1950 int ret;
1951 int pair[2];
1953 /* Create two connected sockets for migration */
1954 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1955 g_assert_cmpint(ret, ==, 0);
1957 /* Send the 1st socket to the target */
1958 qtest_qmp_fds_assert_success(to, &pair[0], 1,
1959 "{ 'execute': 'getfd',"
1960 " 'arguments': { 'fdname': 'fd-mig' }}");
1961 close(pair[0]);
1963 /* Start incoming migration from the 1st socket */
1964 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1965 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1967 /* Send the 2nd socket to the target */
1968 qtest_qmp_fds_assert_success(from, &pair[1], 1,
1969 "{ 'execute': 'getfd',"
1970 " 'arguments': { 'fdname': 'fd-mig' }}");
1971 close(pair[1]);
1973 return NULL;
1976 static void test_migrate_fd_finish_hook(QTestState *from,
1977 QTestState *to,
1978 void *opaque)
1980 QDict *rsp;
1981 const char *error_desc;
1983 /* Test closing fds */
1984 /* We assume, that QEMU removes named fd from its list,
1985 * so this should fail */
1986 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1987 " 'arguments': { 'fdname': 'fd-mig' }}");
1988 g_assert_true(qdict_haskey(rsp, "error"));
1989 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1990 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1991 qobject_unref(rsp);
1993 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1994 " 'arguments': { 'fdname': 'fd-mig' }}");
1995 g_assert_true(qdict_haskey(rsp, "error"));
1996 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1997 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1998 qobject_unref(rsp);
2001 static void test_migrate_fd_proto(void)
2003 MigrateCommon args = {
2004 .listen_uri = "defer",
2005 .connect_uri = "fd:fd-mig",
2006 .start_hook = test_migrate_fd_start_hook,
2007 .finish_hook = test_migrate_fd_finish_hook
2009 test_precopy_common(&args);
2011 #endif /* _WIN32 */
2013 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
2015 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2016 QTestState *from, *to;
2018 if (test_migrate_start(&from, &to, uri, args)) {
2019 return;
2023 * UUID validation is at the begin of migration. So, the main process of
2024 * migration is not interesting for us here. Thus, set huge downtime for
2025 * very fast migration.
2027 migrate_set_parameter_int(from, "downtime-limit", 1000000);
2028 migrate_set_capability(from, "validate-uuid", true);
2030 /* Wait for the first serial output from the source */
2031 wait_for_serial("src_serial");
2033 migrate_qmp(from, uri, "{}");
2035 if (should_fail) {
2036 qtest_set_expected_status(to, EXIT_FAILURE);
2037 wait_for_migration_fail(from, true);
2038 } else {
2039 wait_for_migration_complete(from);
2042 test_migrate_end(from, to, false);
2045 static void test_validate_uuid(void)
2047 MigrateStart args = {
2048 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2049 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
2052 do_test_validate_uuid(&args, false);
2055 static void test_validate_uuid_error(void)
2057 MigrateStart args = {
2058 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2059 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2060 .hide_stderr = true,
2063 do_test_validate_uuid(&args, true);
2066 static void test_validate_uuid_src_not_set(void)
2068 MigrateStart args = {
2069 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2070 .hide_stderr = true,
2073 do_test_validate_uuid(&args, false);
2076 static void test_validate_uuid_dst_not_set(void)
2078 MigrateStart args = {
2079 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2080 .hide_stderr = true,
2083 do_test_validate_uuid(&args, false);
2087 * The way auto_converge works, we need to do too many passes to
2088 * run this test. Auto_converge logic is only run once every
2089 * three iterations, so:
2091 * - 3 iterations without auto_converge enabled
2092 * - 3 iterations with pct = 5
2093 * - 3 iterations with pct = 30
2094 * - 3 iterations with pct = 55
2095 * - 3 iterations with pct = 80
2096 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2098 * To make things even worse, we need to run the initial stage at
2099 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2101 static void test_migrate_auto_converge(void)
2103 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2104 MigrateStart args = {};
2105 QTestState *from, *to;
2106 int64_t percentage;
2109 * We want the test to be stable and as fast as possible.
2110 * E.g., with 1Gb/s bandwith migration may pass without throttling,
2111 * so we need to decrease a bandwidth.
2113 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
2115 if (test_migrate_start(&from, &to, uri, &args)) {
2116 return;
2119 migrate_set_capability(from, "auto-converge", true);
2120 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
2121 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
2122 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
2125 * Set the initial parameters so that the migration could not converge
2126 * without throttling.
2128 migrate_ensure_non_converge(from);
2130 /* To check remaining size after precopy */
2131 migrate_set_capability(from, "pause-before-switchover", true);
2133 /* Wait for the first serial output from the source */
2134 wait_for_serial("src_serial");
2136 migrate_qmp(from, uri, "{}");
2138 /* Wait for throttling begins */
2139 percentage = 0;
2140 do {
2141 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2142 if (percentage != 0) {
2143 break;
2145 usleep(20);
2146 g_assert_false(got_src_stop);
2147 } while (true);
2148 /* The first percentage of throttling should be at least init_pct */
2149 g_assert_cmpint(percentage, >=, init_pct);
2150 /* Now, when we tested that throttling works, let it converge */
2151 migrate_ensure_converge(from);
2154 * Wait for pre-switchover status to check last throttle percentage
2155 * and remaining. These values will be zeroed later
2157 wait_for_migration_status(from, "pre-switchover", NULL);
2159 /* The final percentage of throttling shouldn't be greater than max_pct */
2160 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2161 g_assert_cmpint(percentage, <=, max_pct);
2162 migrate_continue(from, "pre-switchover");
2164 qtest_qmp_eventwait(to, "RESUME");
2166 wait_for_serial("dest_serial");
2167 wait_for_migration_complete(from);
2169 test_migrate_end(from, to, true);
2172 static void *
2173 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2174 QTestState *to,
2175 const char *method)
2177 migrate_set_parameter_int(from, "multifd-channels", 16);
2178 migrate_set_parameter_int(to, "multifd-channels", 16);
2180 migrate_set_parameter_str(from, "multifd-compression", method);
2181 migrate_set_parameter_str(to, "multifd-compression", method);
2183 migrate_set_capability(from, "multifd", true);
2184 migrate_set_capability(to, "multifd", true);
2186 /* Start incoming migration from the 1st socket */
2187 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2188 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2190 return NULL;
2193 static void *
2194 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2195 QTestState *to)
2197 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2200 static void *
2201 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2202 QTestState *to)
2204 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2207 #ifdef CONFIG_ZSTD
2208 static void *
2209 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2210 QTestState *to)
2212 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2214 #endif /* CONFIG_ZSTD */
2216 static void test_multifd_tcp_none(void)
2218 MigrateCommon args = {
2219 .listen_uri = "defer",
2220 .start_hook = test_migrate_precopy_tcp_multifd_start,
2222 * Multifd is more complicated than most of the features, it
2223 * directly takes guest page buffers when sending, make sure
2224 * everything will work alright even if guest page is changing.
2226 .live = true,
2228 test_precopy_common(&args);
2231 static void test_multifd_tcp_zlib(void)
2233 MigrateCommon args = {
2234 .listen_uri = "defer",
2235 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2237 test_precopy_common(&args);
2240 #ifdef CONFIG_ZSTD
2241 static void test_multifd_tcp_zstd(void)
2243 MigrateCommon args = {
2244 .listen_uri = "defer",
2245 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2247 test_precopy_common(&args);
2249 #endif
2251 #ifdef CONFIG_GNUTLS
2252 static void *
2253 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2254 QTestState *to)
2256 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2257 return test_migrate_tls_psk_start_match(from, to);
2260 static void *
2261 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2262 QTestState *to)
2264 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2265 return test_migrate_tls_psk_start_mismatch(from, to);
2268 #ifdef CONFIG_TASN1
2269 static void *
2270 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2271 QTestState *to)
2273 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2274 return test_migrate_tls_x509_start_default_host(from, to);
2277 static void *
2278 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2279 QTestState *to)
2281 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2282 return test_migrate_tls_x509_start_override_host(from, to);
2285 static void *
2286 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2287 QTestState *to)
2289 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2290 return test_migrate_tls_x509_start_mismatch_host(from, to);
2293 static void *
2294 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2295 QTestState *to)
2297 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2298 return test_migrate_tls_x509_start_allow_anon_client(from, to);
2301 static void *
2302 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2303 QTestState *to)
2305 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2306 return test_migrate_tls_x509_start_reject_anon_client(from, to);
2308 #endif /* CONFIG_TASN1 */
2310 static void test_multifd_tcp_tls_psk_match(void)
2312 MigrateCommon args = {
2313 .listen_uri = "defer",
2314 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2315 .finish_hook = test_migrate_tls_psk_finish,
2317 test_precopy_common(&args);
2320 static void test_multifd_tcp_tls_psk_mismatch(void)
2322 MigrateCommon args = {
2323 .start = {
2324 .hide_stderr = true,
2326 .listen_uri = "defer",
2327 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2328 .finish_hook = test_migrate_tls_psk_finish,
2329 .result = MIG_TEST_FAIL,
2331 test_precopy_common(&args);
2334 #ifdef CONFIG_TASN1
2335 static void test_multifd_tcp_tls_x509_default_host(void)
2337 MigrateCommon args = {
2338 .listen_uri = "defer",
2339 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2340 .finish_hook = test_migrate_tls_x509_finish,
2342 test_precopy_common(&args);
2345 static void test_multifd_tcp_tls_x509_override_host(void)
2347 MigrateCommon args = {
2348 .listen_uri = "defer",
2349 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2350 .finish_hook = test_migrate_tls_x509_finish,
2352 test_precopy_common(&args);
2355 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2358 * This has different behaviour to the non-multifd case.
2360 * In non-multifd case when client aborts due to mismatched
2361 * cert host, the server has already started trying to load
2362 * migration state, and so it exits with I/O failure.
2364 * In multifd case when client aborts due to mismatched
2365 * cert host, the server is still waiting for the other
2366 * multifd connections to arrive so hasn't started trying
2367 * to load migration state, and thus just aborts the migration
2368 * without exiting.
2370 MigrateCommon args = {
2371 .start = {
2372 .hide_stderr = true,
2374 .listen_uri = "defer",
2375 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2376 .finish_hook = test_migrate_tls_x509_finish,
2377 .result = MIG_TEST_FAIL,
2379 test_precopy_common(&args);
2382 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2384 MigrateCommon args = {
2385 .listen_uri = "defer",
2386 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2387 .finish_hook = test_migrate_tls_x509_finish,
2389 test_precopy_common(&args);
2392 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2394 MigrateCommon args = {
2395 .start = {
2396 .hide_stderr = true,
2398 .listen_uri = "defer",
2399 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2400 .finish_hook = test_migrate_tls_x509_finish,
2401 .result = MIG_TEST_FAIL,
2403 test_precopy_common(&args);
2405 #endif /* CONFIG_TASN1 */
2406 #endif /* CONFIG_GNUTLS */
2409 * This test does:
2410 * source target
2411 * migrate_incoming
2412 * migrate
2413 * migrate_cancel
2414 * launch another target
2415 * migrate
2417 * And see that it works
2419 static void test_multifd_tcp_cancel(void)
2421 MigrateStart args = {
2422 .hide_stderr = true,
2424 QTestState *from, *to, *to2;
2425 g_autofree char *uri = NULL;
2427 if (test_migrate_start(&from, &to, "defer", &args)) {
2428 return;
2431 migrate_ensure_non_converge(from);
2432 migrate_prepare_for_dirty_mem(from);
2434 migrate_set_parameter_int(from, "multifd-channels", 16);
2435 migrate_set_parameter_int(to, "multifd-channels", 16);
2437 migrate_set_capability(from, "multifd", true);
2438 migrate_set_capability(to, "multifd", true);
2440 /* Start incoming migration from the 1st socket */
2441 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2442 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2444 /* Wait for the first serial output from the source */
2445 wait_for_serial("src_serial");
2447 uri = migrate_get_socket_address(to, "socket-address");
2449 migrate_qmp(from, uri, "{}");
2451 migrate_wait_for_dirty_mem(from, to);
2453 migrate_cancel(from);
2455 /* Make sure QEMU process "to" exited */
2456 qtest_set_expected_status(to, EXIT_FAILURE);
2457 qtest_wait_qemu(to);
2459 args = (MigrateStart){
2460 .only_target = true,
2463 if (test_migrate_start(&from, &to2, "defer", &args)) {
2464 return;
2467 migrate_set_parameter_int(to2, "multifd-channels", 16);
2469 migrate_set_capability(to2, "multifd", true);
2471 /* Start incoming migration from the 1st socket */
2472 qtest_qmp_assert_success(to2, "{ 'execute': 'migrate-incoming',"
2473 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2475 g_free(uri);
2476 uri = migrate_get_socket_address(to2, "socket-address");
2478 wait_for_migration_status(from, "cancelled", NULL);
2480 migrate_ensure_non_converge(from);
2482 migrate_qmp(from, uri, "{}");
2484 migrate_wait_for_dirty_mem(from, to);
2486 migrate_ensure_converge(from);
2488 if (!got_src_stop) {
2489 qtest_qmp_eventwait(from, "STOP");
2491 qtest_qmp_eventwait(to2, "RESUME");
2493 wait_for_serial("dest_serial");
2494 wait_for_migration_complete(from);
2495 test_migrate_end(from, to2, true);
2498 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2500 qtest_qmp_assert_success(who,
2501 "{ 'execute': 'calc-dirty-rate',"
2502 "'arguments': { "
2503 "'calc-time': %" PRIu64 ","
2504 "'mode': 'dirty-ring' }}",
2505 calc_time);
2508 static QDict *query_dirty_rate(QTestState *who)
2510 return qtest_qmp_assert_success_ref(who,
2511 "{ 'execute': 'query-dirty-rate' }");
2514 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
2516 qtest_qmp_assert_success(who,
2517 "{ 'execute': 'set-vcpu-dirty-limit',"
2518 "'arguments': { "
2519 "'dirty-rate': %" PRIu64 " } }",
2520 dirtyrate);
2523 static void cancel_vcpu_dirty_limit(QTestState *who)
2525 qtest_qmp_assert_success(who,
2526 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2529 static QDict *query_vcpu_dirty_limit(QTestState *who)
2531 QDict *rsp;
2533 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
2534 g_assert(!qdict_haskey(rsp, "error"));
2535 g_assert(qdict_haskey(rsp, "return"));
2537 return rsp;
2540 static bool calc_dirtyrate_ready(QTestState *who)
2542 QDict *rsp_return;
2543 gchar *status;
2545 rsp_return = query_dirty_rate(who);
2546 g_assert(rsp_return);
2548 status = g_strdup(qdict_get_str(rsp_return, "status"));
2549 g_assert(status);
2551 return g_strcmp0(status, "measuring");
2554 static void wait_for_calc_dirtyrate_complete(QTestState *who,
2555 int64_t time_s)
2557 int max_try_count = 10000;
2558 usleep(time_s * 1000000);
2560 while (!calc_dirtyrate_ready(who) && max_try_count--) {
2561 usleep(1000);
2565 * Set the timeout with 10 s(max_try_count * 1000us),
2566 * if dirtyrate measurement not complete, fail test.
2568 g_assert_cmpint(max_try_count, !=, 0);
2571 static int64_t get_dirty_rate(QTestState *who)
2573 QDict *rsp_return;
2574 gchar *status;
2575 QList *rates;
2576 const QListEntry *entry;
2577 QDict *rate;
2578 int64_t dirtyrate;
2580 rsp_return = query_dirty_rate(who);
2581 g_assert(rsp_return);
2583 status = g_strdup(qdict_get_str(rsp_return, "status"));
2584 g_assert(status);
2585 g_assert_cmpstr(status, ==, "measured");
2587 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
2588 g_assert(rates && !qlist_empty(rates));
2590 entry = qlist_first(rates);
2591 g_assert(entry);
2593 rate = qobject_to(QDict, qlist_entry_obj(entry));
2594 g_assert(rate);
2596 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
2598 qobject_unref(rsp_return);
2599 return dirtyrate;
2602 static int64_t get_limit_rate(QTestState *who)
2604 QDict *rsp_return;
2605 QList *rates;
2606 const QListEntry *entry;
2607 QDict *rate;
2608 int64_t dirtyrate;
2610 rsp_return = query_vcpu_dirty_limit(who);
2611 g_assert(rsp_return);
2613 rates = qdict_get_qlist(rsp_return, "return");
2614 g_assert(rates && !qlist_empty(rates));
2616 entry = qlist_first(rates);
2617 g_assert(entry);
2619 rate = qobject_to(QDict, qlist_entry_obj(entry));
2620 g_assert(rate);
2622 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
2624 qobject_unref(rsp_return);
2625 return dirtyrate;
2628 static QTestState *dirtylimit_start_vm(void)
2630 QTestState *vm = NULL;
2631 g_autofree gchar *cmd = NULL;
2632 const char *arch = qtest_get_arch();
2633 g_autofree char *bootpath = NULL;
2635 assert((strcmp(arch, "x86_64") == 0));
2636 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
2637 assert(sizeof(x86_bootsect) == 512);
2638 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
2640 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2641 "-name dirtylimit-test,debug-threads=on "
2642 "-m 150M -smp 1 "
2643 "-serial file:%s/vm_serial "
2644 "-drive file=%s,format=raw ",
2645 tmpfs, bootpath);
2647 vm = qtest_init(cmd);
2648 return vm;
2651 static void dirtylimit_stop_vm(QTestState *vm)
2653 qtest_quit(vm);
2654 cleanup("bootsect");
2655 cleanup("vm_serial");
2658 static void test_vcpu_dirty_limit(void)
2660 QTestState *vm;
2661 int64_t origin_rate;
2662 int64_t quota_rate;
2663 int64_t rate ;
2664 int max_try_count = 20;
2665 int hit = 0;
2667 /* Start vm for vcpu dirtylimit test */
2668 vm = dirtylimit_start_vm();
2670 /* Wait for the first serial output from the vm*/
2671 wait_for_serial("vm_serial");
2673 /* Do dirtyrate measurement with calc time equals 1s */
2674 calc_dirty_rate(vm, 1);
2676 /* Sleep calc time and wait for calc dirtyrate complete */
2677 wait_for_calc_dirtyrate_complete(vm, 1);
2679 /* Query original dirty page rate */
2680 origin_rate = get_dirty_rate(vm);
2682 /* VM booted from bootsect should dirty memory steadily */
2683 assert(origin_rate != 0);
2685 /* Setup quota dirty page rate at half of origin */
2686 quota_rate = origin_rate / 2;
2688 /* Set dirtylimit */
2689 dirtylimit_set_all(vm, quota_rate);
2692 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2693 * works literally
2695 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
2697 /* Sleep a bit to check if it take effect */
2698 usleep(2000000);
2701 * Check if dirtylimit take effect realistically, set the
2702 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2703 * doesn't take effect, fail test.
2705 while (--max_try_count) {
2706 calc_dirty_rate(vm, 1);
2707 wait_for_calc_dirtyrate_complete(vm, 1);
2708 rate = get_dirty_rate(vm);
2711 * Assume hitting if current rate is less
2712 * than quota rate (within accepting error)
2714 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2715 hit = 1;
2716 break;
2720 g_assert_cmpint(hit, ==, 1);
2722 hit = 0;
2723 max_try_count = 20;
2725 /* Check if dirtylimit cancellation take effect */
2726 cancel_vcpu_dirty_limit(vm);
2727 while (--max_try_count) {
2728 calc_dirty_rate(vm, 1);
2729 wait_for_calc_dirtyrate_complete(vm, 1);
2730 rate = get_dirty_rate(vm);
2733 * Assume dirtylimit be canceled if current rate is
2734 * greater than quota rate (within accepting error)
2736 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2737 hit = 1;
2738 break;
2742 g_assert_cmpint(hit, ==, 1);
2743 dirtylimit_stop_vm(vm);
2746 static bool kvm_dirty_ring_supported(void)
2748 #if defined(__linux__) && defined(HOST_X86_64)
2749 int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2751 if (kvm_fd < 0) {
2752 return false;
2755 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2756 close(kvm_fd);
2758 /* We test with 4096 slots */
2759 if (ret < 4096) {
2760 return false;
2763 return true;
2764 #else
2765 return false;
2766 #endif
2769 int main(int argc, char **argv)
2771 bool has_kvm, has_tcg;
2772 bool has_uffd;
2773 const char *arch;
2774 g_autoptr(GError) err = NULL;
2775 int ret;
2777 g_test_init(&argc, &argv, NULL);
2779 has_kvm = qtest_has_accel("kvm");
2780 has_tcg = qtest_has_accel("tcg");
2782 if (!has_tcg && !has_kvm) {
2783 g_test_skip("No KVM or TCG accelerator available");
2784 return 0;
2787 has_uffd = ufd_version_check();
2788 arch = qtest_get_arch();
2791 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2792 * is touchy due to race conditions on dirty bits (especially on PPC for
2793 * some reason)
2795 if (g_str_equal(arch, "ppc64") &&
2796 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2797 g_test_message("Skipping test: kvm_hv not available");
2798 return g_test_run();
2802 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2803 * there until the problems are resolved
2805 if (g_str_equal(arch, "s390x") && !has_kvm) {
2806 g_test_message("Skipping test: s390x host with KVM is required");
2807 return g_test_run();
2810 tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
2811 if (!tmpfs) {
2812 g_test_message("Can't create temporary directory in %s: %s",
2813 g_get_tmp_dir(), err->message);
2815 g_assert(tmpfs);
2817 module_call_init(MODULE_INIT_QOM);
2819 if (has_uffd) {
2820 qtest_add_func("/migration/postcopy/plain", test_postcopy);
2821 qtest_add_func("/migration/postcopy/recovery/plain",
2822 test_postcopy_recovery);
2823 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
2824 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2825 test_postcopy_preempt_recovery);
2826 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2827 qtest_add_func("/migration/postcopy/compress/plain",
2828 test_postcopy_compress);
2829 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2830 test_postcopy_recovery_compress);
2834 qtest_add_func("/migration/bad_dest", test_baddest);
2835 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2836 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2838 * Compression fails from time to time.
2839 * Put test here but don't enable it until everything is fixed.
2841 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2842 qtest_add_func("/migration/precopy/unix/compress/wait",
2843 test_precopy_unix_compress);
2844 qtest_add_func("/migration/precopy/unix/compress/nowait",
2845 test_precopy_unix_compress_nowait);
2847 #ifdef CONFIG_GNUTLS
2848 qtest_add_func("/migration/precopy/unix/tls/psk",
2849 test_precopy_unix_tls_psk);
2851 if (has_uffd) {
2853 * NOTE: psk test is enough for postcopy, as other types of TLS
2854 * channels are tested under precopy. Here what we want to test is the
2855 * general postcopy path that has TLS channel enabled.
2857 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
2858 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2859 test_postcopy_recovery_tls_psk);
2860 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2861 test_postcopy_preempt_tls_psk);
2862 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2863 test_postcopy_preempt_all);
2865 #ifdef CONFIG_TASN1
2866 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2867 test_precopy_unix_tls_x509_default_host);
2868 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2869 test_precopy_unix_tls_x509_override_host);
2870 #endif /* CONFIG_TASN1 */
2871 #endif /* CONFIG_GNUTLS */
2873 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2875 qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
2876 test_precopy_tcp_switchover_ack);
2878 #ifdef CONFIG_GNUTLS
2879 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2880 test_precopy_tcp_tls_psk_match);
2881 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2882 test_precopy_tcp_tls_psk_mismatch);
2883 #ifdef CONFIG_TASN1
2884 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2885 test_precopy_tcp_tls_x509_default_host);
2886 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2887 test_precopy_tcp_tls_x509_override_host);
2888 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2889 test_precopy_tcp_tls_x509_mismatch_host);
2890 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2891 test_precopy_tcp_tls_x509_friendly_client);
2892 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2893 test_precopy_tcp_tls_x509_hostile_client);
2894 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2895 test_precopy_tcp_tls_x509_allow_anon_client);
2896 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2897 test_precopy_tcp_tls_x509_reject_anon_client);
2898 #endif /* CONFIG_TASN1 */
2899 #endif /* CONFIG_GNUTLS */
2901 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2902 #ifndef _WIN32
2903 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2904 #endif
2905 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2906 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2907 qtest_add_func("/migration/validate_uuid_src_not_set",
2908 test_validate_uuid_src_not_set);
2909 qtest_add_func("/migration/validate_uuid_dst_not_set",
2910 test_validate_uuid_dst_not_set);
2912 * See explanation why this test is slow on function definition
2914 if (g_test_slow()) {
2915 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2917 qtest_add_func("/migration/multifd/tcp/plain/none",
2918 test_multifd_tcp_none);
2920 * This test is flaky and sometimes fails in CI and otherwise:
2921 * don't run unless user opts in via environment variable.
2923 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2924 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2925 test_multifd_tcp_cancel);
2927 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2928 test_multifd_tcp_zlib);
2929 #ifdef CONFIG_ZSTD
2930 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2931 test_multifd_tcp_zstd);
2932 #endif
2933 #ifdef CONFIG_GNUTLS
2934 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2935 test_multifd_tcp_tls_psk_match);
2936 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2937 test_multifd_tcp_tls_psk_mismatch);
2938 #ifdef CONFIG_TASN1
2939 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2940 test_multifd_tcp_tls_x509_default_host);
2941 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2942 test_multifd_tcp_tls_x509_override_host);
2943 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2944 test_multifd_tcp_tls_x509_mismatch_host);
2945 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2946 test_multifd_tcp_tls_x509_allow_anon_client);
2947 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2948 test_multifd_tcp_tls_x509_reject_anon_client);
2949 #endif /* CONFIG_TASN1 */
2950 #endif /* CONFIG_GNUTLS */
2952 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
2953 qtest_add_func("/migration/dirty_ring",
2954 test_precopy_unix_dirty_ring);
2955 qtest_add_func("/migration/vcpu_dirty_limit",
2956 test_vcpu_dirty_limit);
2959 ret = g_test_run();
2961 g_assert_cmpint(ret, ==, 0);
2963 ret = rmdir(tmpfs);
2964 if (ret != 0) {
2965 g_test_message("unable to rmdir: path (%s): %s",
2966 tmpfs, strerror(errno));
2968 g_free(tmpfs);
2970 return ret;