tests/qtest: migration: Add migrate_incoming_qmp helper
[qemu/armbru.git] / tests / qtest / migration-test.c
blob48778565a3967cf88b012cf99b0f4618cc7d76dd
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;
119 static char *bootpath;
121 /* The boot file modifies memory area in [start_address, end_address)
122 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
124 #include "tests/migration/i386/a-b-bootblock.h"
125 #include "tests/migration/aarch64/a-b-kernel.h"
126 #include "tests/migration/s390x/a-b-bios.h"
128 static void bootfile_create(char *dir)
130 const char *arch = qtest_get_arch();
131 unsigned char *content;
132 size_t len;
134 bootpath = g_strdup_printf("%s/bootsect", dir);
135 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
136 /* the assembled x86 boot sector should be exactly one sector large */
137 g_assert(sizeof(x86_bootsect) == 512);
138 content = x86_bootsect;
139 len = sizeof(x86_bootsect);
140 } else if (g_str_equal(arch, "s390x")) {
141 content = s390x_elf;
142 len = sizeof(s390x_elf);
143 } else if (strcmp(arch, "ppc64") == 0) {
145 * sane architectures can be programmed at the boot prompt
147 return;
148 } else if (strcmp(arch, "aarch64") == 0) {
149 content = aarch64_kernel;
150 len = sizeof(aarch64_kernel);
151 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
152 } else {
153 g_assert_not_reached();
156 FILE *bootfile = fopen(bootpath, "wb");
158 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
159 fclose(bootfile);
162 static void bootfile_delete(void)
164 unlink(bootpath);
165 g_free(bootpath);
166 bootpath = NULL;
170 * Wait for some output in the serial output file,
171 * we get an 'A' followed by an endless string of 'B's
172 * but on the destination we won't have the A.
174 static void wait_for_serial(const char *side)
176 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
177 FILE *serialfile = fopen(serialpath, "r");
178 const char *arch = qtest_get_arch();
179 int started = (strcmp(side, "src_serial") == 0 &&
180 strcmp(arch, "ppc64") == 0) ? 0 : 1;
182 do {
183 int readvalue = fgetc(serialfile);
185 if (!started) {
186 /* SLOF prints its banner before starting test,
187 * to ignore it, mark the start of the test with '_',
188 * ignore all characters until this marker
190 switch (readvalue) {
191 case '_':
192 started = 1;
193 break;
194 case EOF:
195 fseek(serialfile, 0, SEEK_SET);
196 usleep(1000);
197 break;
199 continue;
201 switch (readvalue) {
202 case 'A':
203 /* Fine */
204 break;
206 case 'B':
207 /* It's alive! */
208 fclose(serialfile);
209 return;
211 case EOF:
212 started = (strcmp(side, "src_serial") == 0 &&
213 strcmp(arch, "ppc64") == 0) ? 0 : 1;
214 fseek(serialfile, 0, SEEK_SET);
215 usleep(1000);
216 break;
218 default:
219 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
220 g_assert_not_reached();
222 } while (true);
226 * It's tricky to use qemu's migration event capability with qtest,
227 * events suddenly appearing confuse the qmp()/hmp() responses.
230 static int64_t read_ram_property_int(QTestState *who, const char *property)
232 QDict *rsp_return, *rsp_ram;
233 int64_t result;
235 rsp_return = migrate_query_not_failed(who);
236 if (!qdict_haskey(rsp_return, "ram")) {
237 /* Still in setup */
238 result = 0;
239 } else {
240 rsp_ram = qdict_get_qdict(rsp_return, "ram");
241 result = qdict_get_try_int(rsp_ram, property, 0);
243 qobject_unref(rsp_return);
244 return result;
247 static int64_t read_migrate_property_int(QTestState *who, const char *property)
249 QDict *rsp_return;
250 int64_t result;
252 rsp_return = migrate_query_not_failed(who);
253 result = qdict_get_try_int(rsp_return, property, 0);
254 qobject_unref(rsp_return);
255 return result;
258 static uint64_t get_migration_pass(QTestState *who)
260 return read_ram_property_int(who, "dirty-sync-count");
263 static void read_blocktime(QTestState *who)
265 QDict *rsp_return;
267 rsp_return = migrate_query_not_failed(who);
268 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
269 qobject_unref(rsp_return);
272 static void wait_for_migration_pass(QTestState *who)
274 uint64_t initial_pass = get_migration_pass(who);
275 uint64_t pass;
277 /* Wait for the 1st sync */
278 while (!got_src_stop && !initial_pass) {
279 usleep(1000);
280 initial_pass = get_migration_pass(who);
283 do {
284 usleep(1000);
285 pass = get_migration_pass(who);
286 } while (pass == initial_pass && !got_src_stop);
289 static void check_guests_ram(QTestState *who)
291 /* Our ASM test will have been incrementing one byte from each page from
292 * start_address to < end_address in order. This gives us a constraint
293 * that any page's byte should be equal or less than the previous pages
294 * byte (mod 256); and they should all be equal except for one transition
295 * at the point where we meet the incrementer. (We're running this with
296 * the guest stopped).
298 unsigned address;
299 uint8_t first_byte;
300 uint8_t last_byte;
301 bool hit_edge = false;
302 int bad = 0;
304 qtest_memread(who, start_address, &first_byte, 1);
305 last_byte = first_byte;
307 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
308 address += TEST_MEM_PAGE_SIZE)
310 uint8_t b;
311 qtest_memread(who, address, &b, 1);
312 if (b != last_byte) {
313 if (((b + 1) % 256) == last_byte && !hit_edge) {
314 /* This is OK, the guest stopped at the point of
315 * incrementing the previous page but didn't get
316 * to us yet.
318 hit_edge = true;
319 last_byte = b;
320 } else {
321 bad++;
322 if (bad <= 10) {
323 fprintf(stderr, "Memory content inconsistency at %x"
324 " first_byte = %x last_byte = %x current = %x"
325 " hit_edge = %x\n",
326 address, first_byte, last_byte, b, hit_edge);
331 if (bad >= 10) {
332 fprintf(stderr, "and in another %d pages", bad - 10);
334 g_assert(bad == 0);
337 static void cleanup(const char *filename)
339 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
341 unlink(path);
344 static char *SocketAddress_to_str(SocketAddress *addr)
346 switch (addr->type) {
347 case SOCKET_ADDRESS_TYPE_INET:
348 return g_strdup_printf("tcp:%s:%s",
349 addr->u.inet.host,
350 addr->u.inet.port);
351 case SOCKET_ADDRESS_TYPE_UNIX:
352 return g_strdup_printf("unix:%s",
353 addr->u.q_unix.path);
354 case SOCKET_ADDRESS_TYPE_FD:
355 return g_strdup_printf("fd:%s", addr->u.fd.str);
356 case SOCKET_ADDRESS_TYPE_VSOCK:
357 return g_strdup_printf("tcp:%s:%s",
358 addr->u.vsock.cid,
359 addr->u.vsock.port);
360 default:
361 return g_strdup("unknown address type");
365 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
367 QDict *rsp;
368 char *result;
369 SocketAddressList *addrs;
370 Visitor *iv = NULL;
371 QObject *object;
373 rsp = migrate_query(who);
374 object = qdict_get(rsp, parameter);
376 iv = qobject_input_visitor_new(object);
377 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
378 visit_free(iv);
380 /* we are only using a single address */
381 result = SocketAddress_to_str(addrs->value);
383 qapi_free_SocketAddressList(addrs);
384 qobject_unref(rsp);
385 return result;
388 static long long migrate_get_parameter_int(QTestState *who,
389 const char *parameter)
391 QDict *rsp;
392 long long result;
394 rsp = qtest_qmp_assert_success_ref(
395 who, "{ 'execute': 'query-migrate-parameters' }");
396 result = qdict_get_int(rsp, parameter);
397 qobject_unref(rsp);
398 return result;
401 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
402 long long value)
404 long long result;
406 result = migrate_get_parameter_int(who, parameter);
407 g_assert_cmpint(result, ==, value);
410 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
411 long long value)
413 qtest_qmp_assert_success(who,
414 "{ 'execute': 'migrate-set-parameters',"
415 "'arguments': { %s: %lld } }",
416 parameter, value);
417 migrate_check_parameter_int(who, parameter, value);
420 static char *migrate_get_parameter_str(QTestState *who,
421 const char *parameter)
423 QDict *rsp;
424 char *result;
426 rsp = qtest_qmp_assert_success_ref(
427 who, "{ 'execute': 'query-migrate-parameters' }");
428 result = g_strdup(qdict_get_str(rsp, parameter));
429 qobject_unref(rsp);
430 return result;
433 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
434 const char *value)
436 g_autofree char *result = migrate_get_parameter_str(who, parameter);
437 g_assert_cmpstr(result, ==, value);
440 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
441 const char *value)
443 qtest_qmp_assert_success(who,
444 "{ 'execute': 'migrate-set-parameters',"
445 "'arguments': { %s: %s } }",
446 parameter, value);
447 migrate_check_parameter_str(who, parameter, value);
450 static long long migrate_get_parameter_bool(QTestState *who,
451 const char *parameter)
453 QDict *rsp;
454 int result;
456 rsp = qtest_qmp_assert_success_ref(
457 who, "{ 'execute': 'query-migrate-parameters' }");
458 result = qdict_get_bool(rsp, parameter);
459 qobject_unref(rsp);
460 return !!result;
463 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
464 int value)
466 int result;
468 result = migrate_get_parameter_bool(who, parameter);
469 g_assert_cmpint(result, ==, value);
472 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
473 int value)
475 qtest_qmp_assert_success(who,
476 "{ 'execute': 'migrate-set-parameters',"
477 "'arguments': { %s: %i } }",
478 parameter, value);
479 migrate_check_parameter_bool(who, parameter, value);
482 static void migrate_ensure_non_converge(QTestState *who)
484 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
485 migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
486 migrate_set_parameter_int(who, "downtime-limit", 1);
489 static void migrate_ensure_converge(QTestState *who)
491 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
492 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
493 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
497 * Our goal is to ensure that we run a single full migration
498 * iteration, and also dirty memory, ensuring that at least
499 * one further iteration is required.
501 * We can't directly synchronize with the start of a migration
502 * so we have to apply some tricks monitoring memory that is
503 * transferred.
505 * Initially we set the migration bandwidth to an insanely
506 * low value, with tiny max downtime too. This basically
507 * guarantees migration will never complete.
509 * This will result in a test that is unacceptably slow though,
510 * so we can't let the entire migration pass run at this speed.
511 * Our intent is to let it run just long enough that we can
512 * prove data prior to the marker has been transferred *AND*
513 * also prove this transferred data is dirty again.
515 * Before migration starts, we write a 64-bit magic marker
516 * into a fixed location in the src VM RAM.
518 * Then watch dst memory until the marker appears. This is
519 * proof that start_address -> MAGIC_OFFSET_BASE has been
520 * transferred.
522 * Finally we go back to the source and read a byte just
523 * before the marker until we see it flip in value. This
524 * is proof that start_address -> MAGIC_OFFSET_BASE
525 * is now dirty again.
527 * IOW, we're guaranteed at least a 2nd migration pass
528 * at this point.
530 * We can now let migration run at full speed to finish
531 * the test
533 static void migrate_prepare_for_dirty_mem(QTestState *from)
536 * The guest workflow iterates from start_address to
537 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
538 * bytes.
540 * IOW, if we write to mem at a point which is NOT
541 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
542 * conflict with the migration workflow.
544 * We put in a marker here, that we'll use to determine
545 * when the data has been transferred to the dst.
547 qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
550 static void migrate_wait_for_dirty_mem(QTestState *from,
551 QTestState *to)
553 uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
554 uint64_t marker_address = start_address + MAGIC_OFFSET;
555 uint8_t watch_byte;
558 * Wait for the MAGIC_MARKER to get transferred, as an
559 * indicator that a migration pass has made some known
560 * amount of progress.
562 do {
563 usleep(1000 * 10);
564 } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
567 * Now ensure that already transferred bytes are
568 * dirty again from the guest workload. Note the
569 * guest byte value will wrap around and by chance
570 * match the original watch_byte. This is harmless
571 * as we'll eventually see a different value if we
572 * keep watching
574 watch_byte = qtest_readb(from, watch_address);
575 do {
576 usleep(1000 * 10);
577 } while (qtest_readb(from, watch_address) == watch_byte);
581 static void migrate_pause(QTestState *who)
583 qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
586 static void migrate_continue(QTestState *who, const char *state)
588 qtest_qmp_assert_success(who,
589 "{ 'execute': 'migrate-continue',"
590 " 'arguments': { 'state': %s } }",
591 state);
594 static void migrate_recover(QTestState *who, const char *uri)
596 qtest_qmp_assert_success(who,
597 "{ 'execute': 'migrate-recover', "
598 " 'id': 'recover-cmd', "
599 " 'arguments': { 'uri': %s } }",
600 uri);
603 static void migrate_cancel(QTestState *who)
605 qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
608 static void migrate_postcopy_start(QTestState *from, QTestState *to)
610 qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
612 if (!got_src_stop) {
613 qtest_qmp_eventwait(from, "STOP");
616 qtest_qmp_eventwait(to, "RESUME");
619 typedef struct {
621 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
622 * unconditionally, because it means the user would like to be verbose.
624 bool hide_stderr;
625 bool use_shmem;
626 /* only launch the target process */
627 bool only_target;
628 /* Use dirty ring if true; dirty logging otherwise */
629 bool use_dirty_ring;
630 const char *opts_source;
631 const char *opts_target;
632 } MigrateStart;
635 * A hook that runs after the src and dst QEMUs have been
636 * created, but before the migration is started. This can
637 * be used to set migration parameters and capabilities.
639 * Returns: NULL, or a pointer to opaque state to be
640 * later passed to the TestMigrateFinishHook
642 typedef void * (*TestMigrateStartHook)(QTestState *from,
643 QTestState *to);
646 * A hook that runs after the migration has finished,
647 * regardless of whether it succeeded or failed, but
648 * before QEMU has terminated (unless it self-terminated
649 * due to migration error)
651 * @opaque is a pointer to state previously returned
652 * by the TestMigrateStartHook if any, or NULL.
654 typedef void (*TestMigrateFinishHook)(QTestState *from,
655 QTestState *to,
656 void *opaque);
658 typedef struct {
659 /* Optional: fine tune start parameters */
660 MigrateStart start;
662 /* Required: the URI for the dst QEMU to listen on */
663 const char *listen_uri;
666 * Optional: the URI for the src QEMU to connect to
667 * If NULL, then it will query the dst QEMU for its actual
668 * listening address and use that as the connect address.
669 * This allows for dynamically picking a free TCP port.
671 const char *connect_uri;
673 /* Optional: callback to run at start to set migration parameters */
674 TestMigrateStartHook start_hook;
675 /* Optional: callback to run at finish to cleanup */
676 TestMigrateFinishHook finish_hook;
679 * Optional: normally we expect the migration process to complete.
681 * There can be a variety of reasons and stages in which failure
682 * can happen during tests.
684 * If a failure is expected to happen at time of establishing
685 * the connection, then MIG_TEST_FAIL will indicate that the dst
686 * QEMU is expected to stay running and accept future migration
687 * connections.
689 * If a failure is expected to happen while processing the
690 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
691 * that the dst QEMU is expected to quit with non-zero exit status
693 enum {
694 /* This test should succeed, the default */
695 MIG_TEST_SUCCEED = 0,
696 /* This test should fail, dest qemu should keep alive */
697 MIG_TEST_FAIL,
698 /* This test should fail, dest qemu should fail with abnormal status */
699 MIG_TEST_FAIL_DEST_QUIT_ERR,
700 } result;
703 * Optional: set number of migration passes to wait for, if live==true.
704 * If zero, then merely wait for a few MB of dirty data
706 unsigned int iterations;
709 * Optional: whether the guest CPUs should be running during a precopy
710 * migration test. We used to always run with live but it took much
711 * longer so we reduced live tests to only the ones that have solid
712 * reason to be tested live-only. For each of the new test cases for
713 * precopy please provide justifications to use live explicitly (please
714 * refer to existing ones with live=true), or use live=off by default.
716 bool live;
718 /* Postcopy specific fields */
719 void *postcopy_data;
720 bool postcopy_preempt;
721 } MigrateCommon;
723 static int test_migrate_start(QTestState **from, QTestState **to,
724 const char *uri, MigrateStart *args)
726 g_autofree gchar *arch_source = NULL;
727 g_autofree gchar *arch_target = NULL;
728 /* options for source and target */
729 g_autofree gchar *arch_opts = NULL;
730 g_autofree gchar *cmd_source = NULL;
731 g_autofree gchar *cmd_target = NULL;
732 const gchar *ignore_stderr;
733 g_autofree char *shmem_opts = NULL;
734 g_autofree char *shmem_path = NULL;
735 const char *kvm_opts = NULL;
736 const char *arch = qtest_get_arch();
737 const char *memory_size;
739 if (args->use_shmem) {
740 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
741 g_test_skip("/dev/shm is not supported");
742 return -1;
746 got_src_stop = false;
747 got_dst_resume = false;
748 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
749 memory_size = "150M";
750 arch_opts = g_strdup_printf("-drive file=%s,format=raw", bootpath);
751 start_address = X86_TEST_MEM_START;
752 end_address = X86_TEST_MEM_END;
753 } else if (g_str_equal(arch, "s390x")) {
754 memory_size = "128M";
755 arch_opts = g_strdup_printf("-bios %s", bootpath);
756 start_address = S390_TEST_MEM_START;
757 end_address = S390_TEST_MEM_END;
758 } else if (strcmp(arch, "ppc64") == 0) {
759 memory_size = "256M";
760 start_address = PPC_TEST_MEM_START;
761 end_address = PPC_TEST_MEM_END;
762 arch_source = g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
763 "'nvramrc=hex .\" _\" begin %x %x "
764 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
765 "until'", end_address, start_address);
766 arch_opts = g_strdup("-nodefaults -machine vsmt=8");
767 } else if (strcmp(arch, "aarch64") == 0) {
768 memory_size = "150M";
769 arch_opts = g_strdup_printf("-machine virt,gic-version=max -cpu max "
770 "-kernel %s", bootpath);
771 start_address = ARM_TEST_MEM_START;
772 end_address = ARM_TEST_MEM_END;
773 } else {
774 g_assert_not_reached();
777 if (!getenv("QTEST_LOG") && args->hide_stderr) {
778 #ifndef _WIN32
779 ignore_stderr = "2>/dev/null";
780 #else
782 * On Windows the QEMU executable is created via CreateProcess() and
783 * IO redirection does not work, so don't bother adding IO redirection
784 * to the command line.
786 ignore_stderr = "";
787 #endif
788 } else {
789 ignore_stderr = "";
792 if (args->use_shmem) {
793 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
794 shmem_opts = g_strdup_printf(
795 "-object memory-backend-file,id=mem0,size=%s"
796 ",mem-path=%s,share=on -numa node,memdev=mem0",
797 memory_size, shmem_path);
800 if (args->use_dirty_ring) {
801 kvm_opts = ",dirty-ring-size=4096";
804 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
805 "-name source,debug-threads=on "
806 "-m %s "
807 "-serial file:%s/src_serial "
808 "%s %s %s %s %s",
809 kvm_opts ? kvm_opts : "",
810 memory_size, tmpfs,
811 arch_opts ? arch_opts : "",
812 arch_source ? arch_source : "",
813 shmem_opts ? shmem_opts : "",
814 args->opts_source ? args->opts_source : "",
815 ignore_stderr);
816 if (!args->only_target) {
817 *from = qtest_init(cmd_source);
818 qtest_qmp_set_event_callback(*from,
819 migrate_watch_for_stop,
820 &got_src_stop);
823 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
824 "-name target,debug-threads=on "
825 "-m %s "
826 "-serial file:%s/dest_serial "
827 "-incoming %s "
828 "%s %s %s %s %s",
829 kvm_opts ? kvm_opts : "",
830 memory_size, tmpfs, uri,
831 arch_opts ? arch_opts : "",
832 arch_target ? arch_target : "",
833 shmem_opts ? shmem_opts : "",
834 args->opts_target ? args->opts_target : "",
835 ignore_stderr);
836 *to = qtest_init(cmd_target);
837 qtest_qmp_set_event_callback(*to,
838 migrate_watch_for_resume,
839 &got_dst_resume);
842 * Remove shmem file immediately to avoid memory leak in test failed case.
843 * It's valid because QEMU has already opened this file
845 if (args->use_shmem) {
846 unlink(shmem_path);
849 return 0;
852 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
854 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
856 qtest_quit(from);
858 if (test_dest) {
859 qtest_memread(to, start_address, &dest_byte_a, 1);
861 /* Destination still running, wait for a byte to change */
862 do {
863 qtest_memread(to, start_address, &dest_byte_b, 1);
864 usleep(1000 * 10);
865 } while (dest_byte_a == dest_byte_b);
867 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
869 /* With it stopped, check nothing changes */
870 qtest_memread(to, start_address, &dest_byte_c, 1);
871 usleep(1000 * 200);
872 qtest_memread(to, start_address, &dest_byte_d, 1);
873 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
875 check_guests_ram(to);
878 qtest_quit(to);
880 cleanup("migsocket");
881 cleanup("src_serial");
882 cleanup("dest_serial");
885 #ifdef CONFIG_GNUTLS
886 struct TestMigrateTLSPSKData {
887 char *workdir;
888 char *workdiralt;
889 char *pskfile;
890 char *pskfilealt;
893 static void *
894 test_migrate_tls_psk_start_common(QTestState *from,
895 QTestState *to,
896 bool mismatch)
898 struct TestMigrateTLSPSKData *data =
899 g_new0(struct TestMigrateTLSPSKData, 1);
901 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
902 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
903 QCRYPTO_TLS_CREDS_PSKFILE);
904 g_mkdir_with_parents(data->workdir, 0700);
905 test_tls_psk_init(data->pskfile);
907 if (mismatch) {
908 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
909 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
910 QCRYPTO_TLS_CREDS_PSKFILE);
911 g_mkdir_with_parents(data->workdiralt, 0700);
912 test_tls_psk_init_alt(data->pskfilealt);
915 qtest_qmp_assert_success(from,
916 "{ 'execute': 'object-add',"
917 " 'arguments': { 'qom-type': 'tls-creds-psk',"
918 " 'id': 'tlscredspsk0',"
919 " 'endpoint': 'client',"
920 " 'dir': %s,"
921 " 'username': 'qemu'} }",
922 data->workdir);
924 qtest_qmp_assert_success(to,
925 "{ 'execute': 'object-add',"
926 " 'arguments': { 'qom-type': 'tls-creds-psk',"
927 " 'id': 'tlscredspsk0',"
928 " 'endpoint': 'server',"
929 " 'dir': %s } }",
930 mismatch ? data->workdiralt : data->workdir);
932 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
933 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
935 return data;
938 static void *
939 test_migrate_tls_psk_start_match(QTestState *from,
940 QTestState *to)
942 return test_migrate_tls_psk_start_common(from, to, false);
945 static void *
946 test_migrate_tls_psk_start_mismatch(QTestState *from,
947 QTestState *to)
949 return test_migrate_tls_psk_start_common(from, to, true);
952 static void
953 test_migrate_tls_psk_finish(QTestState *from,
954 QTestState *to,
955 void *opaque)
957 struct TestMigrateTLSPSKData *data = opaque;
959 test_tls_psk_cleanup(data->pskfile);
960 if (data->pskfilealt) {
961 test_tls_psk_cleanup(data->pskfilealt);
963 rmdir(data->workdir);
964 if (data->workdiralt) {
965 rmdir(data->workdiralt);
968 g_free(data->workdiralt);
969 g_free(data->pskfilealt);
970 g_free(data->workdir);
971 g_free(data->pskfile);
972 g_free(data);
975 #ifdef CONFIG_TASN1
976 typedef struct {
977 char *workdir;
978 char *keyfile;
979 char *cacert;
980 char *servercert;
981 char *serverkey;
982 char *clientcert;
983 char *clientkey;
984 } TestMigrateTLSX509Data;
986 typedef struct {
987 bool verifyclient;
988 bool clientcert;
989 bool hostileclient;
990 bool authzclient;
991 const char *certhostname;
992 const char *certipaddr;
993 } TestMigrateTLSX509;
995 static void *
996 test_migrate_tls_x509_start_common(QTestState *from,
997 QTestState *to,
998 TestMigrateTLSX509 *args)
1000 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
1002 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
1003 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
1005 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
1006 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
1007 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
1008 if (args->clientcert) {
1009 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
1010 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
1013 g_mkdir_with_parents(data->workdir, 0700);
1015 test_tls_init(data->keyfile);
1016 #ifndef _WIN32
1017 g_assert(link(data->keyfile, data->serverkey) == 0);
1018 #else
1019 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
1020 #endif
1021 if (args->clientcert) {
1022 #ifndef _WIN32
1023 g_assert(link(data->keyfile, data->clientkey) == 0);
1024 #else
1025 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
1026 #endif
1029 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
1030 if (args->clientcert) {
1031 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
1032 args->hostileclient ?
1033 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
1034 QCRYPTO_TLS_TEST_CLIENT_NAME,
1035 data->clientcert);
1038 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
1039 data->servercert,
1040 args->certhostname,
1041 args->certipaddr);
1043 qtest_qmp_assert_success(from,
1044 "{ 'execute': 'object-add',"
1045 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1046 " 'id': 'tlscredsx509client0',"
1047 " 'endpoint': 'client',"
1048 " 'dir': %s,"
1049 " 'sanity-check': true,"
1050 " 'verify-peer': true} }",
1051 data->workdir);
1052 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
1053 if (args->certhostname) {
1054 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
1057 qtest_qmp_assert_success(to,
1058 "{ 'execute': 'object-add',"
1059 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1060 " 'id': 'tlscredsx509server0',"
1061 " 'endpoint': 'server',"
1062 " 'dir': %s,"
1063 " 'sanity-check': true,"
1064 " 'verify-peer': %i} }",
1065 data->workdir, args->verifyclient);
1066 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
1068 if (args->authzclient) {
1069 qtest_qmp_assert_success(to,
1070 "{ 'execute': 'object-add',"
1071 " 'arguments': { 'qom-type': 'authz-simple',"
1072 " 'id': 'tlsauthz0',"
1073 " 'identity': %s} }",
1074 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
1075 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
1078 return data;
1082 * The normal case: match server's cert hostname against
1083 * whatever host we were telling QEMU to connect to (if any)
1085 static void *
1086 test_migrate_tls_x509_start_default_host(QTestState *from,
1087 QTestState *to)
1089 TestMigrateTLSX509 args = {
1090 .verifyclient = true,
1091 .clientcert = true,
1092 .certipaddr = "127.0.0.1"
1094 return test_migrate_tls_x509_start_common(from, to, &args);
1098 * The unusual case: the server's cert is different from
1099 * the address we're telling QEMU to connect to (if any),
1100 * so we must give QEMU an explicit hostname to validate
1102 static void *
1103 test_migrate_tls_x509_start_override_host(QTestState *from,
1104 QTestState *to)
1106 TestMigrateTLSX509 args = {
1107 .verifyclient = true,
1108 .clientcert = true,
1109 .certhostname = "qemu.org",
1111 return test_migrate_tls_x509_start_common(from, to, &args);
1115 * The unusual case: the server's cert is different from
1116 * the address we're telling QEMU to connect to, and so we
1117 * expect the client to reject the server
1119 static void *
1120 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1121 QTestState *to)
1123 TestMigrateTLSX509 args = {
1124 .verifyclient = true,
1125 .clientcert = true,
1126 .certipaddr = "10.0.0.1",
1128 return test_migrate_tls_x509_start_common(from, to, &args);
1131 static void *
1132 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1133 QTestState *to)
1135 TestMigrateTLSX509 args = {
1136 .verifyclient = true,
1137 .clientcert = true,
1138 .authzclient = true,
1139 .certipaddr = "127.0.0.1",
1141 return test_migrate_tls_x509_start_common(from, to, &args);
1144 static void *
1145 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1146 QTestState *to)
1148 TestMigrateTLSX509 args = {
1149 .verifyclient = true,
1150 .clientcert = true,
1151 .hostileclient = true,
1152 .authzclient = true,
1153 .certipaddr = "127.0.0.1",
1155 return test_migrate_tls_x509_start_common(from, to, &args);
1159 * The case with no client certificate presented,
1160 * and no server verification
1162 static void *
1163 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1164 QTestState *to)
1166 TestMigrateTLSX509 args = {
1167 .certipaddr = "127.0.0.1",
1169 return test_migrate_tls_x509_start_common(from, to, &args);
1173 * The case with no client certificate presented,
1174 * and server verification rejecting
1176 static void *
1177 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1178 QTestState *to)
1180 TestMigrateTLSX509 args = {
1181 .verifyclient = true,
1182 .certipaddr = "127.0.0.1",
1184 return test_migrate_tls_x509_start_common(from, to, &args);
1187 static void
1188 test_migrate_tls_x509_finish(QTestState *from,
1189 QTestState *to,
1190 void *opaque)
1192 TestMigrateTLSX509Data *data = opaque;
1194 test_tls_cleanup(data->keyfile);
1195 g_free(data->keyfile);
1197 unlink(data->cacert);
1198 g_free(data->cacert);
1199 unlink(data->servercert);
1200 g_free(data->servercert);
1201 unlink(data->serverkey);
1202 g_free(data->serverkey);
1204 if (data->clientcert) {
1205 unlink(data->clientcert);
1206 g_free(data->clientcert);
1208 if (data->clientkey) {
1209 unlink(data->clientkey);
1210 g_free(data->clientkey);
1213 rmdir(data->workdir);
1214 g_free(data->workdir);
1216 g_free(data);
1218 #endif /* CONFIG_TASN1 */
1219 #endif /* CONFIG_GNUTLS */
1221 static void *
1222 test_migrate_compress_start(QTestState *from,
1223 QTestState *to)
1225 migrate_set_parameter_int(from, "compress-level", 1);
1226 migrate_set_parameter_int(from, "compress-threads", 4);
1227 migrate_set_parameter_bool(from, "compress-wait-thread", true);
1228 migrate_set_parameter_int(to, "decompress-threads", 4);
1230 migrate_set_capability(from, "compress", true);
1231 migrate_set_capability(to, "compress", true);
1233 return NULL;
1236 static void *
1237 test_migrate_compress_nowait_start(QTestState *from,
1238 QTestState *to)
1240 migrate_set_parameter_int(from, "compress-level", 9);
1241 migrate_set_parameter_int(from, "compress-threads", 1);
1242 migrate_set_parameter_bool(from, "compress-wait-thread", false);
1243 migrate_set_parameter_int(to, "decompress-threads", 1);
1245 migrate_set_capability(from, "compress", true);
1246 migrate_set_capability(to, "compress", true);
1248 return NULL;
1251 static int migrate_postcopy_prepare(QTestState **from_ptr,
1252 QTestState **to_ptr,
1253 MigrateCommon *args)
1255 QTestState *from, *to;
1257 if (test_migrate_start(&from, &to, "defer", &args->start)) {
1258 return -1;
1261 if (args->start_hook) {
1262 args->postcopy_data = args->start_hook(from, to);
1265 migrate_set_capability(from, "postcopy-ram", true);
1266 migrate_set_capability(to, "postcopy-ram", true);
1267 migrate_set_capability(to, "postcopy-blocktime", true);
1269 if (args->postcopy_preempt) {
1270 migrate_set_capability(from, "postcopy-preempt", true);
1271 migrate_set_capability(to, "postcopy-preempt", true);
1274 migrate_ensure_non_converge(from);
1276 migrate_prepare_for_dirty_mem(from);
1277 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1278 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1280 /* Wait for the first serial output from the source */
1281 wait_for_serial("src_serial");
1283 g_autofree char *uri = migrate_get_socket_address(to, "socket-address");
1284 migrate_qmp(from, uri, "{}");
1286 migrate_wait_for_dirty_mem(from, to);
1288 *from_ptr = from;
1289 *to_ptr = to;
1291 return 0;
1294 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1295 MigrateCommon *args)
1297 wait_for_migration_complete(from);
1299 /* Make sure we get at least one "B" on destination */
1300 wait_for_serial("dest_serial");
1302 if (uffd_feature_thread_id) {
1303 read_blocktime(to);
1306 if (args->finish_hook) {
1307 args->finish_hook(from, to, args->postcopy_data);
1308 args->postcopy_data = NULL;
1311 test_migrate_end(from, to, true);
1314 static void test_postcopy_common(MigrateCommon *args)
1316 QTestState *from, *to;
1318 if (migrate_postcopy_prepare(&from, &to, args)) {
1319 return;
1321 migrate_postcopy_start(from, to);
1322 migrate_postcopy_complete(from, to, args);
1325 static void test_postcopy(void)
1327 MigrateCommon args = { };
1329 test_postcopy_common(&args);
1332 static void test_postcopy_compress(void)
1334 MigrateCommon args = {
1335 .start_hook = test_migrate_compress_start
1338 test_postcopy_common(&args);
1341 static void test_postcopy_preempt(void)
1343 MigrateCommon args = {
1344 .postcopy_preempt = true,
1347 test_postcopy_common(&args);
1350 #ifdef CONFIG_GNUTLS
1351 static void test_postcopy_tls_psk(void)
1353 MigrateCommon args = {
1354 .start_hook = test_migrate_tls_psk_start_match,
1355 .finish_hook = test_migrate_tls_psk_finish,
1358 test_postcopy_common(&args);
1361 static void test_postcopy_preempt_tls_psk(void)
1363 MigrateCommon args = {
1364 .postcopy_preempt = true,
1365 .start_hook = test_migrate_tls_psk_start_match,
1366 .finish_hook = test_migrate_tls_psk_finish,
1369 test_postcopy_common(&args);
1371 #endif
1373 static void test_postcopy_recovery_common(MigrateCommon *args)
1375 QTestState *from, *to;
1376 g_autofree char *uri = NULL;
1378 /* Always hide errors for postcopy recover tests since they're expected */
1379 args->start.hide_stderr = true;
1381 if (migrate_postcopy_prepare(&from, &to, args)) {
1382 return;
1385 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1386 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1388 /* Now we start the postcopy */
1389 migrate_postcopy_start(from, to);
1392 * Wait until postcopy is really started; we can only run the
1393 * migrate-pause command during a postcopy
1395 wait_for_migration_status(from, "postcopy-active", NULL);
1398 * Manually stop the postcopy migration. This emulates a network
1399 * failure with the migration socket
1401 migrate_pause(from);
1404 * Wait for destination side to reach postcopy-paused state. The
1405 * migrate-recover command can only succeed if destination machine
1406 * is in the paused state
1408 wait_for_migration_status(to, "postcopy-paused",
1409 (const char * []) { "failed", "active",
1410 "completed", NULL });
1413 * Create a new socket to emulate a new channel that is different
1414 * from the broken migration channel; tell the destination to
1415 * listen to the new port
1417 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1418 migrate_recover(to, uri);
1421 * Try to rebuild the migration channel using the resume flag and
1422 * the newly created channel
1424 wait_for_migration_status(from, "postcopy-paused",
1425 (const char * []) { "failed", "active",
1426 "completed", NULL });
1427 migrate_qmp(from, uri, "{'resume': true}");
1429 /* Restore the postcopy bandwidth to unlimited */
1430 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1432 migrate_postcopy_complete(from, to, args);
1435 static void test_postcopy_recovery(void)
1437 MigrateCommon args = { };
1439 test_postcopy_recovery_common(&args);
1442 static void test_postcopy_recovery_compress(void)
1444 MigrateCommon args = {
1445 .start_hook = test_migrate_compress_start
1448 test_postcopy_recovery_common(&args);
1451 #ifdef CONFIG_GNUTLS
1452 static void test_postcopy_recovery_tls_psk(void)
1454 MigrateCommon args = {
1455 .start_hook = test_migrate_tls_psk_start_match,
1456 .finish_hook = test_migrate_tls_psk_finish,
1459 test_postcopy_recovery_common(&args);
1461 #endif
1463 static void test_postcopy_preempt_recovery(void)
1465 MigrateCommon args = {
1466 .postcopy_preempt = true,
1469 test_postcopy_recovery_common(&args);
1472 #ifdef CONFIG_GNUTLS
1473 /* This contains preempt+recovery+tls test altogether */
1474 static void test_postcopy_preempt_all(void)
1476 MigrateCommon args = {
1477 .postcopy_preempt = true,
1478 .start_hook = test_migrate_tls_psk_start_match,
1479 .finish_hook = test_migrate_tls_psk_finish,
1482 test_postcopy_recovery_common(&args);
1485 #endif
1487 static void test_baddest(void)
1489 MigrateStart args = {
1490 .hide_stderr = true
1492 QTestState *from, *to;
1494 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1495 return;
1497 migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1498 wait_for_migration_fail(from, false);
1499 test_migrate_end(from, to, false);
1502 static void test_precopy_common(MigrateCommon *args)
1504 QTestState *from, *to;
1505 void *data_hook = NULL;
1507 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1508 return;
1511 if (args->start_hook) {
1512 data_hook = args->start_hook(from, to);
1515 /* Wait for the first serial output from the source */
1516 if (args->result == MIG_TEST_SUCCEED) {
1517 wait_for_serial("src_serial");
1520 if (args->live) {
1521 migrate_ensure_non_converge(from);
1522 migrate_prepare_for_dirty_mem(from);
1523 } else {
1525 * Testing non-live migration, we allow it to run at
1526 * full speed to ensure short test case duration.
1527 * For tests expected to fail, we don't need to
1528 * change anything.
1530 if (args->result == MIG_TEST_SUCCEED) {
1531 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1532 if (!got_src_stop) {
1533 qtest_qmp_eventwait(from, "STOP");
1535 migrate_ensure_converge(from);
1539 if (!args->connect_uri) {
1540 g_autofree char *local_connect_uri =
1541 migrate_get_socket_address(to, "socket-address");
1542 migrate_qmp(from, local_connect_uri, "{}");
1543 } else {
1544 migrate_qmp(from, args->connect_uri, "{}");
1548 if (args->result != MIG_TEST_SUCCEED) {
1549 bool allow_active = args->result == MIG_TEST_FAIL;
1550 wait_for_migration_fail(from, allow_active);
1552 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1553 qtest_set_expected_status(to, EXIT_FAILURE);
1555 } else {
1556 if (args->live) {
1558 * For initial iteration(s) we must do a full pass,
1559 * but for the final iteration, we need only wait
1560 * for some dirty mem before switching to converge
1562 while (args->iterations > 1) {
1563 wait_for_migration_pass(from);
1564 args->iterations--;
1566 migrate_wait_for_dirty_mem(from, to);
1568 migrate_ensure_converge(from);
1571 * We do this first, as it has a timeout to stop us
1572 * hanging forever if migration didn't converge
1574 wait_for_migration_complete(from);
1576 if (!got_src_stop) {
1577 qtest_qmp_eventwait(from, "STOP");
1579 } else {
1580 wait_for_migration_complete(from);
1582 * Must wait for dst to finish reading all incoming
1583 * data on the socket before issuing 'cont' otherwise
1584 * it'll be ignored
1586 wait_for_migration_complete(to);
1588 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1591 if (!got_dst_resume) {
1592 qtest_qmp_eventwait(to, "RESUME");
1595 wait_for_serial("dest_serial");
1598 if (args->finish_hook) {
1599 args->finish_hook(from, to, data_hook);
1602 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1605 static void test_precopy_unix_plain(void)
1607 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1608 MigrateCommon args = {
1609 .listen_uri = uri,
1610 .connect_uri = uri,
1612 * The simplest use case of precopy, covering smoke tests of
1613 * get-dirty-log dirty tracking.
1615 .live = true,
1618 test_precopy_common(&args);
1622 static void test_precopy_unix_dirty_ring(void)
1624 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1625 MigrateCommon args = {
1626 .start = {
1627 .use_dirty_ring = true,
1629 .listen_uri = uri,
1630 .connect_uri = uri,
1632 * Besides the precopy/unix basic test, cover dirty ring interface
1633 * rather than get-dirty-log.
1635 .live = true,
1638 test_precopy_common(&args);
1641 #ifdef CONFIG_GNUTLS
1642 static void test_precopy_unix_tls_psk(void)
1644 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1645 MigrateCommon args = {
1646 .connect_uri = uri,
1647 .listen_uri = uri,
1648 .start_hook = test_migrate_tls_psk_start_match,
1649 .finish_hook = test_migrate_tls_psk_finish,
1652 test_precopy_common(&args);
1655 #ifdef CONFIG_TASN1
1656 static void test_precopy_unix_tls_x509_default_host(void)
1658 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1659 MigrateCommon args = {
1660 .start = {
1661 .hide_stderr = true,
1663 .connect_uri = uri,
1664 .listen_uri = uri,
1665 .start_hook = test_migrate_tls_x509_start_default_host,
1666 .finish_hook = test_migrate_tls_x509_finish,
1667 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1670 test_precopy_common(&args);
1673 static void test_precopy_unix_tls_x509_override_host(void)
1675 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1676 MigrateCommon args = {
1677 .connect_uri = uri,
1678 .listen_uri = uri,
1679 .start_hook = test_migrate_tls_x509_start_override_host,
1680 .finish_hook = test_migrate_tls_x509_finish,
1683 test_precopy_common(&args);
1685 #endif /* CONFIG_TASN1 */
1686 #endif /* CONFIG_GNUTLS */
1688 #if 0
1689 /* Currently upset on aarch64 TCG */
1690 static void test_ignore_shared(void)
1692 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1693 QTestState *from, *to;
1695 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1696 return;
1699 migrate_ensure_non_converge(from);
1700 migrate_prepare_for_dirty_mem(from);
1702 migrate_set_capability(from, "x-ignore-shared", true);
1703 migrate_set_capability(to, "x-ignore-shared", true);
1705 /* Wait for the first serial output from the source */
1706 wait_for_serial("src_serial");
1708 migrate_qmp(from, uri, "{}");
1710 migrate_wait_for_dirty_mem(from, to);
1712 if (!got_src_stop) {
1713 qtest_qmp_eventwait(from, "STOP");
1716 qtest_qmp_eventwait(to, "RESUME");
1718 wait_for_serial("dest_serial");
1719 wait_for_migration_complete(from);
1721 /* Check whether shared RAM has been really skipped */
1722 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1724 test_migrate_end(from, to, true);
1726 #endif
1728 static void *
1729 test_migrate_xbzrle_start(QTestState *from,
1730 QTestState *to)
1732 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1734 migrate_set_capability(from, "xbzrle", true);
1735 migrate_set_capability(to, "xbzrle", true);
1737 return NULL;
1740 static void test_precopy_unix_xbzrle(void)
1742 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1743 MigrateCommon args = {
1744 .connect_uri = uri,
1745 .listen_uri = uri,
1746 .start_hook = test_migrate_xbzrle_start,
1747 .iterations = 2,
1749 * XBZRLE needs pages to be modified when doing the 2nd+ round
1750 * iteration to have real data pushed to the stream.
1752 .live = true,
1755 test_precopy_common(&args);
1758 static void test_precopy_unix_compress(void)
1760 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1761 MigrateCommon args = {
1762 .connect_uri = uri,
1763 .listen_uri = uri,
1764 .start_hook = test_migrate_compress_start,
1766 * Test that no invalid thread state is left over from
1767 * the previous iteration.
1769 .iterations = 2,
1771 * We make sure the compressor can always work well even if guest
1772 * memory is changing. See commit 34ab9e9743 where we used to fix
1773 * a bug when only trigger-able with guest memory changing.
1775 .live = true,
1778 test_precopy_common(&args);
1781 static void test_precopy_unix_compress_nowait(void)
1783 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1784 MigrateCommon args = {
1785 .connect_uri = uri,
1786 .listen_uri = uri,
1787 .start_hook = test_migrate_compress_nowait_start,
1789 * Test that no invalid thread state is left over from
1790 * the previous iteration.
1792 .iterations = 2,
1793 /* Same reason for the wait version of precopy compress test */
1794 .live = true,
1797 test_precopy_common(&args);
1800 static void test_precopy_tcp_plain(void)
1802 MigrateCommon args = {
1803 .listen_uri = "tcp:127.0.0.1:0",
1806 test_precopy_common(&args);
1809 static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to)
1812 migrate_set_capability(from, "return-path", true);
1813 migrate_set_capability(to, "return-path", true);
1815 migrate_set_capability(from, "switchover-ack", true);
1816 migrate_set_capability(to, "switchover-ack", true);
1818 return NULL;
1821 static void test_precopy_tcp_switchover_ack(void)
1823 MigrateCommon args = {
1824 .listen_uri = "tcp:127.0.0.1:0",
1825 .start_hook = test_migrate_switchover_ack_start,
1827 * Source VM must be running in order to consider the switchover ACK
1828 * when deciding to do switchover or not.
1830 .live = true,
1833 test_precopy_common(&args);
1836 #ifdef CONFIG_GNUTLS
1837 static void test_precopy_tcp_tls_psk_match(void)
1839 MigrateCommon args = {
1840 .listen_uri = "tcp:127.0.0.1:0",
1841 .start_hook = test_migrate_tls_psk_start_match,
1842 .finish_hook = test_migrate_tls_psk_finish,
1845 test_precopy_common(&args);
1848 static void test_precopy_tcp_tls_psk_mismatch(void)
1850 MigrateCommon args = {
1851 .start = {
1852 .hide_stderr = true,
1854 .listen_uri = "tcp:127.0.0.1:0",
1855 .start_hook = test_migrate_tls_psk_start_mismatch,
1856 .finish_hook = test_migrate_tls_psk_finish,
1857 .result = MIG_TEST_FAIL,
1860 test_precopy_common(&args);
1863 #ifdef CONFIG_TASN1
1864 static void test_precopy_tcp_tls_x509_default_host(void)
1866 MigrateCommon args = {
1867 .listen_uri = "tcp:127.0.0.1:0",
1868 .start_hook = test_migrate_tls_x509_start_default_host,
1869 .finish_hook = test_migrate_tls_x509_finish,
1872 test_precopy_common(&args);
1875 static void test_precopy_tcp_tls_x509_override_host(void)
1877 MigrateCommon args = {
1878 .listen_uri = "tcp:127.0.0.1:0",
1879 .start_hook = test_migrate_tls_x509_start_override_host,
1880 .finish_hook = test_migrate_tls_x509_finish,
1883 test_precopy_common(&args);
1886 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1888 MigrateCommon args = {
1889 .start = {
1890 .hide_stderr = true,
1892 .listen_uri = "tcp:127.0.0.1:0",
1893 .start_hook = test_migrate_tls_x509_start_mismatch_host,
1894 .finish_hook = test_migrate_tls_x509_finish,
1895 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1898 test_precopy_common(&args);
1901 static void test_precopy_tcp_tls_x509_friendly_client(void)
1903 MigrateCommon args = {
1904 .listen_uri = "tcp:127.0.0.1:0",
1905 .start_hook = test_migrate_tls_x509_start_friendly_client,
1906 .finish_hook = test_migrate_tls_x509_finish,
1909 test_precopy_common(&args);
1912 static void test_precopy_tcp_tls_x509_hostile_client(void)
1914 MigrateCommon args = {
1915 .start = {
1916 .hide_stderr = true,
1918 .listen_uri = "tcp:127.0.0.1:0",
1919 .start_hook = test_migrate_tls_x509_start_hostile_client,
1920 .finish_hook = test_migrate_tls_x509_finish,
1921 .result = MIG_TEST_FAIL,
1924 test_precopy_common(&args);
1927 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1929 MigrateCommon args = {
1930 .listen_uri = "tcp:127.0.0.1:0",
1931 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1932 .finish_hook = test_migrate_tls_x509_finish,
1935 test_precopy_common(&args);
1938 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1940 MigrateCommon args = {
1941 .start = {
1942 .hide_stderr = true,
1944 .listen_uri = "tcp:127.0.0.1:0",
1945 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1946 .finish_hook = test_migrate_tls_x509_finish,
1947 .result = MIG_TEST_FAIL,
1950 test_precopy_common(&args);
1952 #endif /* CONFIG_TASN1 */
1953 #endif /* CONFIG_GNUTLS */
1955 #ifndef _WIN32
1956 static void *test_migrate_fd_start_hook(QTestState *from,
1957 QTestState *to)
1959 int ret;
1960 int pair[2];
1962 /* Create two connected sockets for migration */
1963 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1964 g_assert_cmpint(ret, ==, 0);
1966 /* Send the 1st socket to the target */
1967 qtest_qmp_fds_assert_success(to, &pair[0], 1,
1968 "{ 'execute': 'getfd',"
1969 " 'arguments': { 'fdname': 'fd-mig' }}");
1970 close(pair[0]);
1972 /* Start incoming migration from the 1st socket */
1973 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1974 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1976 /* Send the 2nd socket to the target */
1977 qtest_qmp_fds_assert_success(from, &pair[1], 1,
1978 "{ 'execute': 'getfd',"
1979 " 'arguments': { 'fdname': 'fd-mig' }}");
1980 close(pair[1]);
1982 return NULL;
1985 static void test_migrate_fd_finish_hook(QTestState *from,
1986 QTestState *to,
1987 void *opaque)
1989 QDict *rsp;
1990 const char *error_desc;
1992 /* Test closing fds */
1993 /* We assume, that QEMU removes named fd from its list,
1994 * so this should fail */
1995 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1996 " 'arguments': { 'fdname': 'fd-mig' }}");
1997 g_assert_true(qdict_haskey(rsp, "error"));
1998 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1999 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2000 qobject_unref(rsp);
2002 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
2003 " 'arguments': { 'fdname': 'fd-mig' }}");
2004 g_assert_true(qdict_haskey(rsp, "error"));
2005 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2006 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2007 qobject_unref(rsp);
2010 static void test_migrate_fd_proto(void)
2012 MigrateCommon args = {
2013 .listen_uri = "defer",
2014 .connect_uri = "fd:fd-mig",
2015 .start_hook = test_migrate_fd_start_hook,
2016 .finish_hook = test_migrate_fd_finish_hook
2018 test_precopy_common(&args);
2020 #endif /* _WIN32 */
2022 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
2024 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2025 QTestState *from, *to;
2027 if (test_migrate_start(&from, &to, uri, args)) {
2028 return;
2032 * UUID validation is at the begin of migration. So, the main process of
2033 * migration is not interesting for us here. Thus, set huge downtime for
2034 * very fast migration.
2036 migrate_set_parameter_int(from, "downtime-limit", 1000000);
2037 migrate_set_capability(from, "validate-uuid", true);
2039 /* Wait for the first serial output from the source */
2040 wait_for_serial("src_serial");
2042 migrate_qmp(from, uri, "{}");
2044 if (should_fail) {
2045 qtest_set_expected_status(to, EXIT_FAILURE);
2046 wait_for_migration_fail(from, true);
2047 } else {
2048 wait_for_migration_complete(from);
2051 test_migrate_end(from, to, false);
2054 static void test_validate_uuid(void)
2056 MigrateStart args = {
2057 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2058 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
2061 do_test_validate_uuid(&args, false);
2064 static void test_validate_uuid_error(void)
2066 MigrateStart args = {
2067 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2068 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2069 .hide_stderr = true,
2072 do_test_validate_uuid(&args, true);
2075 static void test_validate_uuid_src_not_set(void)
2077 MigrateStart args = {
2078 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2079 .hide_stderr = true,
2082 do_test_validate_uuid(&args, false);
2085 static void test_validate_uuid_dst_not_set(void)
2087 MigrateStart args = {
2088 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2089 .hide_stderr = true,
2092 do_test_validate_uuid(&args, false);
2096 * The way auto_converge works, we need to do too many passes to
2097 * run this test. Auto_converge logic is only run once every
2098 * three iterations, so:
2100 * - 3 iterations without auto_converge enabled
2101 * - 3 iterations with pct = 5
2102 * - 3 iterations with pct = 30
2103 * - 3 iterations with pct = 55
2104 * - 3 iterations with pct = 80
2105 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2107 * To make things even worse, we need to run the initial stage at
2108 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2110 static void test_migrate_auto_converge(void)
2112 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2113 MigrateStart args = {};
2114 QTestState *from, *to;
2115 int64_t percentage;
2118 * We want the test to be stable and as fast as possible.
2119 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2120 * so we need to decrease a bandwidth.
2122 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
2124 if (test_migrate_start(&from, &to, uri, &args)) {
2125 return;
2128 migrate_set_capability(from, "auto-converge", true);
2129 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
2130 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
2131 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
2134 * Set the initial parameters so that the migration could not converge
2135 * without throttling.
2137 migrate_ensure_non_converge(from);
2139 /* To check remaining size after precopy */
2140 migrate_set_capability(from, "pause-before-switchover", true);
2142 /* Wait for the first serial output from the source */
2143 wait_for_serial("src_serial");
2145 migrate_qmp(from, uri, "{}");
2147 /* Wait for throttling begins */
2148 percentage = 0;
2149 do {
2150 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2151 if (percentage != 0) {
2152 break;
2154 usleep(20);
2155 g_assert_false(got_src_stop);
2156 } while (true);
2157 /* The first percentage of throttling should be at least init_pct */
2158 g_assert_cmpint(percentage, >=, init_pct);
2159 /* Now, when we tested that throttling works, let it converge */
2160 migrate_ensure_converge(from);
2163 * Wait for pre-switchover status to check last throttle percentage
2164 * and remaining. These values will be zeroed later
2166 wait_for_migration_status(from, "pre-switchover", NULL);
2168 /* The final percentage of throttling shouldn't be greater than max_pct */
2169 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2170 g_assert_cmpint(percentage, <=, max_pct);
2171 migrate_continue(from, "pre-switchover");
2173 qtest_qmp_eventwait(to, "RESUME");
2175 wait_for_serial("dest_serial");
2176 wait_for_migration_complete(from);
2178 test_migrate_end(from, to, true);
2181 static void *
2182 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2183 QTestState *to,
2184 const char *method)
2186 migrate_set_parameter_int(from, "multifd-channels", 16);
2187 migrate_set_parameter_int(to, "multifd-channels", 16);
2189 migrate_set_parameter_str(from, "multifd-compression", method);
2190 migrate_set_parameter_str(to, "multifd-compression", method);
2192 migrate_set_capability(from, "multifd", true);
2193 migrate_set_capability(to, "multifd", true);
2195 /* Start incoming migration from the 1st socket */
2196 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2197 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2199 return NULL;
2202 static void *
2203 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2204 QTestState *to)
2206 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2209 static void *
2210 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2211 QTestState *to)
2213 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2216 #ifdef CONFIG_ZSTD
2217 static void *
2218 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2219 QTestState *to)
2221 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2223 #endif /* CONFIG_ZSTD */
2225 static void test_multifd_tcp_none(void)
2227 MigrateCommon args = {
2228 .listen_uri = "defer",
2229 .start_hook = test_migrate_precopy_tcp_multifd_start,
2231 * Multifd is more complicated than most of the features, it
2232 * directly takes guest page buffers when sending, make sure
2233 * everything will work alright even if guest page is changing.
2235 .live = true,
2237 test_precopy_common(&args);
2240 static void test_multifd_tcp_zlib(void)
2242 MigrateCommon args = {
2243 .listen_uri = "defer",
2244 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2246 test_precopy_common(&args);
2249 #ifdef CONFIG_ZSTD
2250 static void test_multifd_tcp_zstd(void)
2252 MigrateCommon args = {
2253 .listen_uri = "defer",
2254 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2256 test_precopy_common(&args);
2258 #endif
2260 #ifdef CONFIG_GNUTLS
2261 static void *
2262 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2263 QTestState *to)
2265 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2266 return test_migrate_tls_psk_start_match(from, to);
2269 static void *
2270 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2271 QTestState *to)
2273 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2274 return test_migrate_tls_psk_start_mismatch(from, to);
2277 #ifdef CONFIG_TASN1
2278 static void *
2279 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2280 QTestState *to)
2282 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2283 return test_migrate_tls_x509_start_default_host(from, to);
2286 static void *
2287 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2288 QTestState *to)
2290 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2291 return test_migrate_tls_x509_start_override_host(from, to);
2294 static void *
2295 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2296 QTestState *to)
2298 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2299 return test_migrate_tls_x509_start_mismatch_host(from, to);
2302 static void *
2303 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2304 QTestState *to)
2306 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2307 return test_migrate_tls_x509_start_allow_anon_client(from, to);
2310 static void *
2311 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2312 QTestState *to)
2314 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2315 return test_migrate_tls_x509_start_reject_anon_client(from, to);
2317 #endif /* CONFIG_TASN1 */
2319 static void test_multifd_tcp_tls_psk_match(void)
2321 MigrateCommon args = {
2322 .listen_uri = "defer",
2323 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2324 .finish_hook = test_migrate_tls_psk_finish,
2326 test_precopy_common(&args);
2329 static void test_multifd_tcp_tls_psk_mismatch(void)
2331 MigrateCommon args = {
2332 .start = {
2333 .hide_stderr = true,
2335 .listen_uri = "defer",
2336 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2337 .finish_hook = test_migrate_tls_psk_finish,
2338 .result = MIG_TEST_FAIL,
2340 test_precopy_common(&args);
2343 #ifdef CONFIG_TASN1
2344 static void test_multifd_tcp_tls_x509_default_host(void)
2346 MigrateCommon args = {
2347 .listen_uri = "defer",
2348 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2349 .finish_hook = test_migrate_tls_x509_finish,
2351 test_precopy_common(&args);
2354 static void test_multifd_tcp_tls_x509_override_host(void)
2356 MigrateCommon args = {
2357 .listen_uri = "defer",
2358 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2359 .finish_hook = test_migrate_tls_x509_finish,
2361 test_precopy_common(&args);
2364 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2367 * This has different behaviour to the non-multifd case.
2369 * In non-multifd case when client aborts due to mismatched
2370 * cert host, the server has already started trying to load
2371 * migration state, and so it exits with I/O failure.
2373 * In multifd case when client aborts due to mismatched
2374 * cert host, the server is still waiting for the other
2375 * multifd connections to arrive so hasn't started trying
2376 * to load migration state, and thus just aborts the migration
2377 * without exiting.
2379 MigrateCommon args = {
2380 .start = {
2381 .hide_stderr = true,
2383 .listen_uri = "defer",
2384 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2385 .finish_hook = test_migrate_tls_x509_finish,
2386 .result = MIG_TEST_FAIL,
2388 test_precopy_common(&args);
2391 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2393 MigrateCommon args = {
2394 .listen_uri = "defer",
2395 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2396 .finish_hook = test_migrate_tls_x509_finish,
2398 test_precopy_common(&args);
2401 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2403 MigrateCommon args = {
2404 .start = {
2405 .hide_stderr = true,
2407 .listen_uri = "defer",
2408 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2409 .finish_hook = test_migrate_tls_x509_finish,
2410 .result = MIG_TEST_FAIL,
2412 test_precopy_common(&args);
2414 #endif /* CONFIG_TASN1 */
2415 #endif /* CONFIG_GNUTLS */
2418 * This test does:
2419 * source target
2420 * migrate_incoming
2421 * migrate
2422 * migrate_cancel
2423 * launch another target
2424 * migrate
2426 * And see that it works
2428 static void test_multifd_tcp_cancel(void)
2430 MigrateStart args = {
2431 .hide_stderr = true,
2433 QTestState *from, *to, *to2;
2434 g_autofree char *uri = NULL;
2436 if (test_migrate_start(&from, &to, "defer", &args)) {
2437 return;
2440 migrate_ensure_non_converge(from);
2441 migrate_prepare_for_dirty_mem(from);
2443 migrate_set_parameter_int(from, "multifd-channels", 16);
2444 migrate_set_parameter_int(to, "multifd-channels", 16);
2446 migrate_set_capability(from, "multifd", true);
2447 migrate_set_capability(to, "multifd", true);
2449 /* Start incoming migration from the 1st socket */
2450 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2451 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2453 /* Wait for the first serial output from the source */
2454 wait_for_serial("src_serial");
2456 uri = migrate_get_socket_address(to, "socket-address");
2458 migrate_qmp(from, uri, "{}");
2460 migrate_wait_for_dirty_mem(from, to);
2462 migrate_cancel(from);
2464 /* Make sure QEMU process "to" exited */
2465 qtest_set_expected_status(to, EXIT_FAILURE);
2466 qtest_wait_qemu(to);
2468 args = (MigrateStart){
2469 .only_target = true,
2472 if (test_migrate_start(&from, &to2, "defer", &args)) {
2473 return;
2476 migrate_set_parameter_int(to2, "multifd-channels", 16);
2478 migrate_set_capability(to2, "multifd", true);
2480 /* Start incoming migration from the 1st socket */
2481 qtest_qmp_assert_success(to2, "{ 'execute': 'migrate-incoming',"
2482 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2484 g_free(uri);
2485 uri = migrate_get_socket_address(to2, "socket-address");
2487 wait_for_migration_status(from, "cancelled", NULL);
2489 migrate_ensure_non_converge(from);
2491 migrate_qmp(from, uri, "{}");
2493 migrate_wait_for_dirty_mem(from, to2);
2495 migrate_ensure_converge(from);
2497 if (!got_src_stop) {
2498 qtest_qmp_eventwait(from, "STOP");
2500 qtest_qmp_eventwait(to2, "RESUME");
2502 wait_for_serial("dest_serial");
2503 wait_for_migration_complete(from);
2504 test_migrate_end(from, to2, true);
2507 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2509 qtest_qmp_assert_success(who,
2510 "{ 'execute': 'calc-dirty-rate',"
2511 "'arguments': { "
2512 "'calc-time': %" PRIu64 ","
2513 "'mode': 'dirty-ring' }}",
2514 calc_time);
2517 static QDict *query_dirty_rate(QTestState *who)
2519 return qtest_qmp_assert_success_ref(who,
2520 "{ 'execute': 'query-dirty-rate' }");
2523 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
2525 qtest_qmp_assert_success(who,
2526 "{ 'execute': 'set-vcpu-dirty-limit',"
2527 "'arguments': { "
2528 "'dirty-rate': %" PRIu64 " } }",
2529 dirtyrate);
2532 static void cancel_vcpu_dirty_limit(QTestState *who)
2534 qtest_qmp_assert_success(who,
2535 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2538 static QDict *query_vcpu_dirty_limit(QTestState *who)
2540 QDict *rsp;
2542 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
2543 g_assert(!qdict_haskey(rsp, "error"));
2544 g_assert(qdict_haskey(rsp, "return"));
2546 return rsp;
2549 static bool calc_dirtyrate_ready(QTestState *who)
2551 QDict *rsp_return;
2552 gchar *status;
2554 rsp_return = query_dirty_rate(who);
2555 g_assert(rsp_return);
2557 status = g_strdup(qdict_get_str(rsp_return, "status"));
2558 g_assert(status);
2560 return g_strcmp0(status, "measuring");
2563 static void wait_for_calc_dirtyrate_complete(QTestState *who,
2564 int64_t time_s)
2566 int max_try_count = 10000;
2567 usleep(time_s * 1000000);
2569 while (!calc_dirtyrate_ready(who) && max_try_count--) {
2570 usleep(1000);
2574 * Set the timeout with 10 s(max_try_count * 1000us),
2575 * if dirtyrate measurement not complete, fail test.
2577 g_assert_cmpint(max_try_count, !=, 0);
2580 static int64_t get_dirty_rate(QTestState *who)
2582 QDict *rsp_return;
2583 gchar *status;
2584 QList *rates;
2585 const QListEntry *entry;
2586 QDict *rate;
2587 int64_t dirtyrate;
2589 rsp_return = query_dirty_rate(who);
2590 g_assert(rsp_return);
2592 status = g_strdup(qdict_get_str(rsp_return, "status"));
2593 g_assert(status);
2594 g_assert_cmpstr(status, ==, "measured");
2596 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
2597 g_assert(rates && !qlist_empty(rates));
2599 entry = qlist_first(rates);
2600 g_assert(entry);
2602 rate = qobject_to(QDict, qlist_entry_obj(entry));
2603 g_assert(rate);
2605 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
2607 qobject_unref(rsp_return);
2608 return dirtyrate;
2611 static int64_t get_limit_rate(QTestState *who)
2613 QDict *rsp_return;
2614 QList *rates;
2615 const QListEntry *entry;
2616 QDict *rate;
2617 int64_t dirtyrate;
2619 rsp_return = query_vcpu_dirty_limit(who);
2620 g_assert(rsp_return);
2622 rates = qdict_get_qlist(rsp_return, "return");
2623 g_assert(rates && !qlist_empty(rates));
2625 entry = qlist_first(rates);
2626 g_assert(entry);
2628 rate = qobject_to(QDict, qlist_entry_obj(entry));
2629 g_assert(rate);
2631 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
2633 qobject_unref(rsp_return);
2634 return dirtyrate;
2637 static QTestState *dirtylimit_start_vm(void)
2639 QTestState *vm = NULL;
2640 g_autofree gchar *
2641 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2642 "-name dirtylimit-test,debug-threads=on "
2643 "-m 150M -smp 1 "
2644 "-serial file:%s/vm_serial "
2645 "-drive file=%s,format=raw ",
2646 tmpfs, bootpath);
2648 vm = qtest_init(cmd);
2649 return vm;
2652 static void dirtylimit_stop_vm(QTestState *vm)
2654 qtest_quit(vm);
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);
2816 bootfile_create(tmpfs);
2818 module_call_init(MODULE_INIT_QOM);
2820 if (has_uffd) {
2821 qtest_add_func("/migration/postcopy/plain", test_postcopy);
2822 qtest_add_func("/migration/postcopy/recovery/plain",
2823 test_postcopy_recovery);
2824 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
2825 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2826 test_postcopy_preempt_recovery);
2827 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2828 qtest_add_func("/migration/postcopy/compress/plain",
2829 test_postcopy_compress);
2830 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2831 test_postcopy_recovery_compress);
2835 qtest_add_func("/migration/bad_dest", test_baddest);
2836 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2837 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2839 * Compression fails from time to time.
2840 * Put test here but don't enable it until everything is fixed.
2842 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2843 qtest_add_func("/migration/precopy/unix/compress/wait",
2844 test_precopy_unix_compress);
2845 qtest_add_func("/migration/precopy/unix/compress/nowait",
2846 test_precopy_unix_compress_nowait);
2848 #ifdef CONFIG_GNUTLS
2849 qtest_add_func("/migration/precopy/unix/tls/psk",
2850 test_precopy_unix_tls_psk);
2852 if (has_uffd) {
2854 * NOTE: psk test is enough for postcopy, as other types of TLS
2855 * channels are tested under precopy. Here what we want to test is the
2856 * general postcopy path that has TLS channel enabled.
2858 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
2859 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2860 test_postcopy_recovery_tls_psk);
2861 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2862 test_postcopy_preempt_tls_psk);
2863 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2864 test_postcopy_preempt_all);
2866 #ifdef CONFIG_TASN1
2867 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2868 test_precopy_unix_tls_x509_default_host);
2869 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2870 test_precopy_unix_tls_x509_override_host);
2871 #endif /* CONFIG_TASN1 */
2872 #endif /* CONFIG_GNUTLS */
2874 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2876 qtest_add_func("/migration/precopy/tcp/plain/switchover-ack",
2877 test_precopy_tcp_switchover_ack);
2879 #ifdef CONFIG_GNUTLS
2880 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2881 test_precopy_tcp_tls_psk_match);
2882 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2883 test_precopy_tcp_tls_psk_mismatch);
2884 #ifdef CONFIG_TASN1
2885 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2886 test_precopy_tcp_tls_x509_default_host);
2887 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2888 test_precopy_tcp_tls_x509_override_host);
2889 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2890 test_precopy_tcp_tls_x509_mismatch_host);
2891 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2892 test_precopy_tcp_tls_x509_friendly_client);
2893 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2894 test_precopy_tcp_tls_x509_hostile_client);
2895 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2896 test_precopy_tcp_tls_x509_allow_anon_client);
2897 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2898 test_precopy_tcp_tls_x509_reject_anon_client);
2899 #endif /* CONFIG_TASN1 */
2900 #endif /* CONFIG_GNUTLS */
2902 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2903 #ifndef _WIN32
2904 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2905 #endif
2906 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2907 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2908 qtest_add_func("/migration/validate_uuid_src_not_set",
2909 test_validate_uuid_src_not_set);
2910 qtest_add_func("/migration/validate_uuid_dst_not_set",
2911 test_validate_uuid_dst_not_set);
2913 * See explanation why this test is slow on function definition
2915 if (g_test_slow()) {
2916 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2918 qtest_add_func("/migration/multifd/tcp/plain/none",
2919 test_multifd_tcp_none);
2921 * This test is flaky and sometimes fails in CI and otherwise:
2922 * don't run unless user opts in via environment variable.
2924 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2925 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2926 test_multifd_tcp_cancel);
2928 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2929 test_multifd_tcp_zlib);
2930 #ifdef CONFIG_ZSTD
2931 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2932 test_multifd_tcp_zstd);
2933 #endif
2934 #ifdef CONFIG_GNUTLS
2935 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2936 test_multifd_tcp_tls_psk_match);
2937 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2938 test_multifd_tcp_tls_psk_mismatch);
2939 #ifdef CONFIG_TASN1
2940 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2941 test_multifd_tcp_tls_x509_default_host);
2942 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2943 test_multifd_tcp_tls_x509_override_host);
2944 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2945 test_multifd_tcp_tls_x509_mismatch_host);
2946 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2947 test_multifd_tcp_tls_x509_allow_anon_client);
2948 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2949 test_multifd_tcp_tls_x509_reject_anon_client);
2950 #endif /* CONFIG_TASN1 */
2951 #endif /* CONFIG_GNUTLS */
2953 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
2954 qtest_add_func("/migration/dirty_ring",
2955 test_precopy_unix_dirty_ring);
2956 qtest_add_func("/migration/vcpu_dirty_limit",
2957 test_vcpu_dirty_limit);
2960 ret = g_test_run();
2962 g_assert_cmpint(ret, ==, 0);
2964 bootfile_delete();
2965 ret = rmdir(tmpfs);
2966 if (ret != 0) {
2967 g_test_message("unable to rmdir: path (%s): %s",
2968 tmpfs, strerror(errno));
2970 g_free(tmpfs);
2972 return ret;