tests/qtest: make more migration pre-copy scenarios run non-live
[qemu/armbru.git] / tests / qtest / migration-test.c
blob0b9d045152f54e266d78ab3eb982246fe1741621
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 * Dirtylimit stop working if dirty page rate error
51 * value less than DIRTYLIMIT_TOLERANCE_RANGE
53 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
55 #if defined(__linux__)
56 #include <sys/syscall.h>
57 #include <sys/vfs.h>
58 #endif
60 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
61 #include <sys/eventfd.h>
62 #include <sys/ioctl.h>
63 #include "qemu/userfaultfd.h"
65 static bool ufd_version_check(void)
67 struct uffdio_api api_struct;
68 uint64_t ioctl_mask;
70 int ufd = uffd_open(O_CLOEXEC);
72 if (ufd == -1) {
73 g_test_message("Skipping test: userfaultfd not available");
74 return false;
77 api_struct.api = UFFD_API;
78 api_struct.features = 0;
79 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
80 g_test_message("Skipping test: UFFDIO_API failed");
81 return false;
83 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
85 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
86 (__u64)1 << _UFFDIO_UNREGISTER;
87 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
88 g_test_message("Skipping test: Missing userfault feature");
89 return false;
92 return true;
95 #else
96 static bool ufd_version_check(void)
98 g_test_message("Skipping test: Userfault not available (builtdtime)");
99 return false;
102 #endif
104 static char *tmpfs;
106 /* The boot file modifies memory area in [start_address, end_address)
107 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
109 #include "tests/migration/i386/a-b-bootblock.h"
110 #include "tests/migration/aarch64/a-b-kernel.h"
111 #include "tests/migration/s390x/a-b-bios.h"
113 static void init_bootfile(const char *bootpath, void *content, size_t len)
115 FILE *bootfile = fopen(bootpath, "wb");
117 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
118 fclose(bootfile);
122 * Wait for some output in the serial output file,
123 * we get an 'A' followed by an endless string of 'B's
124 * but on the destination we won't have the A.
126 static void wait_for_serial(const char *side)
128 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
129 FILE *serialfile = fopen(serialpath, "r");
130 const char *arch = qtest_get_arch();
131 int started = (strcmp(side, "src_serial") == 0 &&
132 strcmp(arch, "ppc64") == 0) ? 0 : 1;
134 do {
135 int readvalue = fgetc(serialfile);
137 if (!started) {
138 /* SLOF prints its banner before starting test,
139 * to ignore it, mark the start of the test with '_',
140 * ignore all characters until this marker
142 switch (readvalue) {
143 case '_':
144 started = 1;
145 break;
146 case EOF:
147 fseek(serialfile, 0, SEEK_SET);
148 usleep(1000);
149 break;
151 continue;
153 switch (readvalue) {
154 case 'A':
155 /* Fine */
156 break;
158 case 'B':
159 /* It's alive! */
160 fclose(serialfile);
161 return;
163 case EOF:
164 started = (strcmp(side, "src_serial") == 0 &&
165 strcmp(arch, "ppc64") == 0) ? 0 : 1;
166 fseek(serialfile, 0, SEEK_SET);
167 usleep(1000);
168 break;
170 default:
171 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
172 g_assert_not_reached();
174 } while (true);
178 * It's tricky to use qemu's migration event capability with qtest,
179 * events suddenly appearing confuse the qmp()/hmp() responses.
182 static int64_t read_ram_property_int(QTestState *who, const char *property)
184 QDict *rsp_return, *rsp_ram;
185 int64_t result;
187 rsp_return = migrate_query_not_failed(who);
188 if (!qdict_haskey(rsp_return, "ram")) {
189 /* Still in setup */
190 result = 0;
191 } else {
192 rsp_ram = qdict_get_qdict(rsp_return, "ram");
193 result = qdict_get_try_int(rsp_ram, property, 0);
195 qobject_unref(rsp_return);
196 return result;
199 static int64_t read_migrate_property_int(QTestState *who, const char *property)
201 QDict *rsp_return;
202 int64_t result;
204 rsp_return = migrate_query_not_failed(who);
205 result = qdict_get_try_int(rsp_return, property, 0);
206 qobject_unref(rsp_return);
207 return result;
210 static uint64_t get_migration_pass(QTestState *who)
212 return read_ram_property_int(who, "dirty-sync-count");
215 static void read_blocktime(QTestState *who)
217 QDict *rsp_return;
219 rsp_return = migrate_query_not_failed(who);
220 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
221 qobject_unref(rsp_return);
224 static void wait_for_migration_pass(QTestState *who)
226 uint64_t initial_pass = get_migration_pass(who);
227 uint64_t pass;
229 /* Wait for the 1st sync */
230 while (!got_src_stop && !initial_pass) {
231 usleep(1000);
232 initial_pass = get_migration_pass(who);
235 do {
236 usleep(1000);
237 pass = get_migration_pass(who);
238 } while (pass == initial_pass && !got_src_stop);
241 static void check_guests_ram(QTestState *who)
243 /* Our ASM test will have been incrementing one byte from each page from
244 * start_address to < end_address in order. This gives us a constraint
245 * that any page's byte should be equal or less than the previous pages
246 * byte (mod 256); and they should all be equal except for one transition
247 * at the point where we meet the incrementer. (We're running this with
248 * the guest stopped).
250 unsigned address;
251 uint8_t first_byte;
252 uint8_t last_byte;
253 bool hit_edge = false;
254 int bad = 0;
256 qtest_memread(who, start_address, &first_byte, 1);
257 last_byte = first_byte;
259 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
260 address += TEST_MEM_PAGE_SIZE)
262 uint8_t b;
263 qtest_memread(who, address, &b, 1);
264 if (b != last_byte) {
265 if (((b + 1) % 256) == last_byte && !hit_edge) {
266 /* This is OK, the guest stopped at the point of
267 * incrementing the previous page but didn't get
268 * to us yet.
270 hit_edge = true;
271 last_byte = b;
272 } else {
273 bad++;
274 if (bad <= 10) {
275 fprintf(stderr, "Memory content inconsistency at %x"
276 " first_byte = %x last_byte = %x current = %x"
277 " hit_edge = %x\n",
278 address, first_byte, last_byte, b, hit_edge);
283 if (bad >= 10) {
284 fprintf(stderr, "and in another %d pages", bad - 10);
286 g_assert(bad == 0);
289 static void cleanup(const char *filename)
291 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
293 unlink(path);
296 static char *SocketAddress_to_str(SocketAddress *addr)
298 switch (addr->type) {
299 case SOCKET_ADDRESS_TYPE_INET:
300 return g_strdup_printf("tcp:%s:%s",
301 addr->u.inet.host,
302 addr->u.inet.port);
303 case SOCKET_ADDRESS_TYPE_UNIX:
304 return g_strdup_printf("unix:%s",
305 addr->u.q_unix.path);
306 case SOCKET_ADDRESS_TYPE_FD:
307 return g_strdup_printf("fd:%s", addr->u.fd.str);
308 case SOCKET_ADDRESS_TYPE_VSOCK:
309 return g_strdup_printf("tcp:%s:%s",
310 addr->u.vsock.cid,
311 addr->u.vsock.port);
312 default:
313 return g_strdup("unknown address type");
317 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
319 QDict *rsp;
320 char *result;
321 SocketAddressList *addrs;
322 Visitor *iv = NULL;
323 QObject *object;
325 rsp = migrate_query(who);
326 object = qdict_get(rsp, parameter);
328 iv = qobject_input_visitor_new(object);
329 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
330 visit_free(iv);
332 /* we are only using a single address */
333 result = SocketAddress_to_str(addrs->value);
335 qapi_free_SocketAddressList(addrs);
336 qobject_unref(rsp);
337 return result;
340 static long long migrate_get_parameter_int(QTestState *who,
341 const char *parameter)
343 QDict *rsp;
344 long long result;
346 rsp = qtest_qmp_assert_success_ref(
347 who, "{ 'execute': 'query-migrate-parameters' }");
348 result = qdict_get_int(rsp, parameter);
349 qobject_unref(rsp);
350 return result;
353 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
354 long long value)
356 long long result;
358 result = migrate_get_parameter_int(who, parameter);
359 g_assert_cmpint(result, ==, value);
362 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
363 long long value)
365 qtest_qmp_assert_success(who,
366 "{ 'execute': 'migrate-set-parameters',"
367 "'arguments': { %s: %lld } }",
368 parameter, value);
369 migrate_check_parameter_int(who, parameter, value);
372 static char *migrate_get_parameter_str(QTestState *who,
373 const char *parameter)
375 QDict *rsp;
376 char *result;
378 rsp = qtest_qmp_assert_success_ref(
379 who, "{ 'execute': 'query-migrate-parameters' }");
380 result = g_strdup(qdict_get_str(rsp, parameter));
381 qobject_unref(rsp);
382 return result;
385 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
386 const char *value)
388 g_autofree char *result = migrate_get_parameter_str(who, parameter);
389 g_assert_cmpstr(result, ==, value);
392 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
393 const char *value)
395 qtest_qmp_assert_success(who,
396 "{ 'execute': 'migrate-set-parameters',"
397 "'arguments': { %s: %s } }",
398 parameter, value);
399 migrate_check_parameter_str(who, parameter, value);
402 static long long migrate_get_parameter_bool(QTestState *who,
403 const char *parameter)
405 QDict *rsp;
406 int result;
408 rsp = qtest_qmp_assert_success_ref(
409 who, "{ 'execute': 'query-migrate-parameters' }");
410 result = qdict_get_bool(rsp, parameter);
411 qobject_unref(rsp);
412 return !!result;
415 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
416 int value)
418 int result;
420 result = migrate_get_parameter_bool(who, parameter);
421 g_assert_cmpint(result, ==, value);
424 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
425 int value)
427 qtest_qmp_assert_success(who,
428 "{ 'execute': 'migrate-set-parameters',"
429 "'arguments': { %s: %i } }",
430 parameter, value);
431 migrate_check_parameter_bool(who, parameter, value);
434 static void migrate_ensure_non_converge(QTestState *who)
436 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
437 migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
438 migrate_set_parameter_int(who, "downtime-limit", 1);
441 static void migrate_ensure_converge(QTestState *who)
443 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
444 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
445 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
448 static void migrate_pause(QTestState *who)
450 qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
453 static void migrate_continue(QTestState *who, const char *state)
455 qtest_qmp_assert_success(who,
456 "{ 'execute': 'migrate-continue',"
457 " 'arguments': { 'state': %s } }",
458 state);
461 static void migrate_recover(QTestState *who, const char *uri)
463 qtest_qmp_assert_success(who,
464 "{ 'execute': 'migrate-recover', "
465 " 'id': 'recover-cmd', "
466 " 'arguments': { 'uri': %s } }",
467 uri);
470 static void migrate_cancel(QTestState *who)
472 qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
475 static void migrate_set_capability(QTestState *who, const char *capability,
476 bool value)
478 qtest_qmp_assert_success(who,
479 "{ 'execute': 'migrate-set-capabilities',"
480 "'arguments': { "
481 "'capabilities': [ { "
482 "'capability': %s, 'state': %i } ] } }",
483 capability, value);
486 static void migrate_postcopy_start(QTestState *from, QTestState *to)
488 qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
490 if (!got_src_stop) {
491 qtest_qmp_eventwait(from, "STOP");
494 qtest_qmp_eventwait(to, "RESUME");
497 typedef struct {
499 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
500 * unconditionally, because it means the user would like to be verbose.
502 bool hide_stderr;
503 bool use_shmem;
504 /* only launch the target process */
505 bool only_target;
506 /* Use dirty ring if true; dirty logging otherwise */
507 bool use_dirty_ring;
508 const char *opts_source;
509 const char *opts_target;
510 } MigrateStart;
513 * A hook that runs after the src and dst QEMUs have been
514 * created, but before the migration is started. This can
515 * be used to set migration parameters and capabilities.
517 * Returns: NULL, or a pointer to opaque state to be
518 * later passed to the TestMigrateFinishHook
520 typedef void * (*TestMigrateStartHook)(QTestState *from,
521 QTestState *to);
524 * A hook that runs after the migration has finished,
525 * regardless of whether it succeeded or failed, but
526 * before QEMU has terminated (unless it self-terminated
527 * due to migration error)
529 * @opaque is a pointer to state previously returned
530 * by the TestMigrateStartHook if any, or NULL.
532 typedef void (*TestMigrateFinishHook)(QTestState *from,
533 QTestState *to,
534 void *opaque);
536 typedef struct {
537 /* Optional: fine tune start parameters */
538 MigrateStart start;
540 /* Required: the URI for the dst QEMU to listen on */
541 const char *listen_uri;
544 * Optional: the URI for the src QEMU to connect to
545 * If NULL, then it will query the dst QEMU for its actual
546 * listening address and use that as the connect address.
547 * This allows for dynamically picking a free TCP port.
549 const char *connect_uri;
551 /* Optional: callback to run at start to set migration parameters */
552 TestMigrateStartHook start_hook;
553 /* Optional: callback to run at finish to cleanup */
554 TestMigrateFinishHook finish_hook;
557 * Optional: normally we expect the migration process to complete.
559 * There can be a variety of reasons and stages in which failure
560 * can happen during tests.
562 * If a failure is expected to happen at time of establishing
563 * the connection, then MIG_TEST_FAIL will indicate that the dst
564 * QEMU is expected to stay running and accept future migration
565 * connections.
567 * If a failure is expected to happen while processing the
568 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
569 * that the dst QEMU is expected to quit with non-zero exit status
571 enum {
572 /* This test should succeed, the default */
573 MIG_TEST_SUCCEED = 0,
574 /* This test should fail, dest qemu should keep alive */
575 MIG_TEST_FAIL,
576 /* This test should fail, dest qemu should fail with abnormal status */
577 MIG_TEST_FAIL_DEST_QUIT_ERR,
578 } result;
580 /* Optional: set number of migration passes to wait for, if live==true */
581 unsigned int iterations;
583 /* Optional: whether the guest CPUs should be running during migration */
584 bool live;
586 /* Postcopy specific fields */
587 void *postcopy_data;
588 bool postcopy_preempt;
589 } MigrateCommon;
591 static int test_migrate_start(QTestState **from, QTestState **to,
592 const char *uri, MigrateStart *args)
594 g_autofree gchar *arch_source = NULL;
595 g_autofree gchar *arch_target = NULL;
596 g_autofree gchar *cmd_source = NULL;
597 g_autofree gchar *cmd_target = NULL;
598 const gchar *ignore_stderr;
599 g_autofree char *bootpath = NULL;
600 g_autofree char *shmem_opts = NULL;
601 g_autofree char *shmem_path = NULL;
602 const char *arch = qtest_get_arch();
603 const char *machine_opts = NULL;
604 const char *memory_size;
606 if (args->use_shmem) {
607 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
608 g_test_skip("/dev/shm is not supported");
609 return -1;
613 got_src_stop = false;
614 got_dst_resume = false;
615 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
616 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
617 /* the assembled x86 boot sector should be exactly one sector large */
618 assert(sizeof(x86_bootsect) == 512);
619 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
620 memory_size = "150M";
621 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
622 arch_target = g_strdup(arch_source);
623 start_address = X86_TEST_MEM_START;
624 end_address = X86_TEST_MEM_END;
625 } else if (g_str_equal(arch, "s390x")) {
626 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
627 memory_size = "128M";
628 arch_source = g_strdup_printf("-bios %s", bootpath);
629 arch_target = g_strdup(arch_source);
630 start_address = S390_TEST_MEM_START;
631 end_address = S390_TEST_MEM_END;
632 } else if (strcmp(arch, "ppc64") == 0) {
633 machine_opts = "vsmt=8";
634 memory_size = "256M";
635 start_address = PPC_TEST_MEM_START;
636 end_address = PPC_TEST_MEM_END;
637 arch_source = g_strdup_printf("-nodefaults "
638 "-prom-env 'use-nvramrc?=true' -prom-env "
639 "'nvramrc=hex .\" _\" begin %x %x "
640 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
641 "until'", end_address, start_address);
642 arch_target = g_strdup("");
643 } else if (strcmp(arch, "aarch64") == 0) {
644 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
645 machine_opts = "virt,gic-version=max";
646 memory_size = "150M";
647 arch_source = g_strdup_printf("-cpu max "
648 "-kernel %s",
649 bootpath);
650 arch_target = g_strdup(arch_source);
651 start_address = ARM_TEST_MEM_START;
652 end_address = ARM_TEST_MEM_END;
654 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
655 } else {
656 g_assert_not_reached();
659 if (!getenv("QTEST_LOG") && args->hide_stderr) {
660 #ifndef _WIN32
661 ignore_stderr = "2>/dev/null";
662 #else
664 * On Windows the QEMU executable is created via CreateProcess() and
665 * IO redirection does not work, so don't bother adding IO redirection
666 * to the command line.
668 ignore_stderr = "";
669 #endif
670 } else {
671 ignore_stderr = "";
674 if (args->use_shmem) {
675 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
676 shmem_opts = g_strdup_printf(
677 "-object memory-backend-file,id=mem0,size=%s"
678 ",mem-path=%s,share=on -numa node,memdev=mem0",
679 memory_size, shmem_path);
680 } else {
681 shmem_path = NULL;
682 shmem_opts = g_strdup("");
685 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
686 "-name source,debug-threads=on "
687 "-m %s "
688 "-serial file:%s/src_serial "
689 "%s %s %s %s",
690 args->use_dirty_ring ?
691 ",dirty-ring-size=4096" : "",
692 machine_opts ? " -machine " : "",
693 machine_opts ? machine_opts : "",
694 memory_size, tmpfs,
695 arch_source, shmem_opts,
696 args->opts_source ? args->opts_source : "",
697 ignore_stderr);
698 if (!args->only_target) {
699 *from = qtest_init(cmd_source);
700 qtest_qmp_set_event_callback(*from,
701 migrate_watch_for_stop,
702 &got_src_stop);
705 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
706 "-name target,debug-threads=on "
707 "-m %s "
708 "-serial file:%s/dest_serial "
709 "-incoming %s "
710 "%s %s %s %s",
711 args->use_dirty_ring ?
712 ",dirty-ring-size=4096" : "",
713 machine_opts ? " -machine " : "",
714 machine_opts ? machine_opts : "",
715 memory_size, tmpfs, uri,
716 arch_target, shmem_opts,
717 args->opts_target ? args->opts_target : "",
718 ignore_stderr);
719 *to = qtest_init(cmd_target);
720 qtest_qmp_set_event_callback(*to,
721 migrate_watch_for_resume,
722 &got_dst_resume);
725 * Remove shmem file immediately to avoid memory leak in test failed case.
726 * It's valid becase QEMU has already opened this file
728 if (args->use_shmem) {
729 unlink(shmem_path);
732 return 0;
735 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
737 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
739 qtest_quit(from);
741 if (test_dest) {
742 qtest_memread(to, start_address, &dest_byte_a, 1);
744 /* Destination still running, wait for a byte to change */
745 do {
746 qtest_memread(to, start_address, &dest_byte_b, 1);
747 usleep(1000 * 10);
748 } while (dest_byte_a == dest_byte_b);
750 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
752 /* With it stopped, check nothing changes */
753 qtest_memread(to, start_address, &dest_byte_c, 1);
754 usleep(1000 * 200);
755 qtest_memread(to, start_address, &dest_byte_d, 1);
756 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
758 check_guests_ram(to);
761 qtest_quit(to);
763 cleanup("bootsect");
764 cleanup("migsocket");
765 cleanup("src_serial");
766 cleanup("dest_serial");
769 #ifdef CONFIG_GNUTLS
770 struct TestMigrateTLSPSKData {
771 char *workdir;
772 char *workdiralt;
773 char *pskfile;
774 char *pskfilealt;
777 static void *
778 test_migrate_tls_psk_start_common(QTestState *from,
779 QTestState *to,
780 bool mismatch)
782 struct TestMigrateTLSPSKData *data =
783 g_new0(struct TestMigrateTLSPSKData, 1);
785 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
786 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
787 QCRYPTO_TLS_CREDS_PSKFILE);
788 g_mkdir_with_parents(data->workdir, 0700);
789 test_tls_psk_init(data->pskfile);
791 if (mismatch) {
792 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
793 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
794 QCRYPTO_TLS_CREDS_PSKFILE);
795 g_mkdir_with_parents(data->workdiralt, 0700);
796 test_tls_psk_init_alt(data->pskfilealt);
799 qtest_qmp_assert_success(from,
800 "{ 'execute': 'object-add',"
801 " 'arguments': { 'qom-type': 'tls-creds-psk',"
802 " 'id': 'tlscredspsk0',"
803 " 'endpoint': 'client',"
804 " 'dir': %s,"
805 " 'username': 'qemu'} }",
806 data->workdir);
808 qtest_qmp_assert_success(to,
809 "{ 'execute': 'object-add',"
810 " 'arguments': { 'qom-type': 'tls-creds-psk',"
811 " 'id': 'tlscredspsk0',"
812 " 'endpoint': 'server',"
813 " 'dir': %s } }",
814 mismatch ? data->workdiralt : data->workdir);
816 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
817 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
819 return data;
822 static void *
823 test_migrate_tls_psk_start_match(QTestState *from,
824 QTestState *to)
826 return test_migrate_tls_psk_start_common(from, to, false);
829 static void *
830 test_migrate_tls_psk_start_mismatch(QTestState *from,
831 QTestState *to)
833 return test_migrate_tls_psk_start_common(from, to, true);
836 static void
837 test_migrate_tls_psk_finish(QTestState *from,
838 QTestState *to,
839 void *opaque)
841 struct TestMigrateTLSPSKData *data = opaque;
843 test_tls_psk_cleanup(data->pskfile);
844 if (data->pskfilealt) {
845 test_tls_psk_cleanup(data->pskfilealt);
847 rmdir(data->workdir);
848 if (data->workdiralt) {
849 rmdir(data->workdiralt);
852 g_free(data->workdiralt);
853 g_free(data->pskfilealt);
854 g_free(data->workdir);
855 g_free(data->pskfile);
856 g_free(data);
859 #ifdef CONFIG_TASN1
860 typedef struct {
861 char *workdir;
862 char *keyfile;
863 char *cacert;
864 char *servercert;
865 char *serverkey;
866 char *clientcert;
867 char *clientkey;
868 } TestMigrateTLSX509Data;
870 typedef struct {
871 bool verifyclient;
872 bool clientcert;
873 bool hostileclient;
874 bool authzclient;
875 const char *certhostname;
876 const char *certipaddr;
877 } TestMigrateTLSX509;
879 static void *
880 test_migrate_tls_x509_start_common(QTestState *from,
881 QTestState *to,
882 TestMigrateTLSX509 *args)
884 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
886 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
887 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
889 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
890 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
891 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
892 if (args->clientcert) {
893 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
894 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
897 g_mkdir_with_parents(data->workdir, 0700);
899 test_tls_init(data->keyfile);
900 #ifndef _WIN32
901 g_assert(link(data->keyfile, data->serverkey) == 0);
902 #else
903 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
904 #endif
905 if (args->clientcert) {
906 #ifndef _WIN32
907 g_assert(link(data->keyfile, data->clientkey) == 0);
908 #else
909 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
910 #endif
913 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
914 if (args->clientcert) {
915 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
916 args->hostileclient ?
917 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
918 QCRYPTO_TLS_TEST_CLIENT_NAME,
919 data->clientcert);
922 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
923 data->servercert,
924 args->certhostname,
925 args->certipaddr);
927 qtest_qmp_assert_success(from,
928 "{ 'execute': 'object-add',"
929 " 'arguments': { 'qom-type': 'tls-creds-x509',"
930 " 'id': 'tlscredsx509client0',"
931 " 'endpoint': 'client',"
932 " 'dir': %s,"
933 " 'sanity-check': true,"
934 " 'verify-peer': true} }",
935 data->workdir);
936 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
937 if (args->certhostname) {
938 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
941 qtest_qmp_assert_success(to,
942 "{ 'execute': 'object-add',"
943 " 'arguments': { 'qom-type': 'tls-creds-x509',"
944 " 'id': 'tlscredsx509server0',"
945 " 'endpoint': 'server',"
946 " 'dir': %s,"
947 " 'sanity-check': true,"
948 " 'verify-peer': %i} }",
949 data->workdir, args->verifyclient);
950 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
952 if (args->authzclient) {
953 qtest_qmp_assert_success(to,
954 "{ 'execute': 'object-add',"
955 " 'arguments': { 'qom-type': 'authz-simple',"
956 " 'id': 'tlsauthz0',"
957 " 'identity': %s} }",
958 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
959 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
962 return data;
966 * The normal case: match server's cert hostname against
967 * whatever host we were telling QEMU to connect to (if any)
969 static void *
970 test_migrate_tls_x509_start_default_host(QTestState *from,
971 QTestState *to)
973 TestMigrateTLSX509 args = {
974 .verifyclient = true,
975 .clientcert = true,
976 .certipaddr = "127.0.0.1"
978 return test_migrate_tls_x509_start_common(from, to, &args);
982 * The unusual case: the server's cert is different from
983 * the address we're telling QEMU to connect to (if any),
984 * so we must give QEMU an explicit hostname to validate
986 static void *
987 test_migrate_tls_x509_start_override_host(QTestState *from,
988 QTestState *to)
990 TestMigrateTLSX509 args = {
991 .verifyclient = true,
992 .clientcert = true,
993 .certhostname = "qemu.org",
995 return test_migrate_tls_x509_start_common(from, to, &args);
999 * The unusual case: the server's cert is different from
1000 * the address we're telling QEMU to connect to, and so we
1001 * expect the client to reject the server
1003 static void *
1004 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1005 QTestState *to)
1007 TestMigrateTLSX509 args = {
1008 .verifyclient = true,
1009 .clientcert = true,
1010 .certipaddr = "10.0.0.1",
1012 return test_migrate_tls_x509_start_common(from, to, &args);
1015 static void *
1016 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1017 QTestState *to)
1019 TestMigrateTLSX509 args = {
1020 .verifyclient = true,
1021 .clientcert = true,
1022 .authzclient = true,
1023 .certipaddr = "127.0.0.1",
1025 return test_migrate_tls_x509_start_common(from, to, &args);
1028 static void *
1029 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1030 QTestState *to)
1032 TestMigrateTLSX509 args = {
1033 .verifyclient = true,
1034 .clientcert = true,
1035 .hostileclient = true,
1036 .authzclient = true,
1037 .certipaddr = "127.0.0.1",
1039 return test_migrate_tls_x509_start_common(from, to, &args);
1043 * The case with no client certificate presented,
1044 * and no server verification
1046 static void *
1047 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1048 QTestState *to)
1050 TestMigrateTLSX509 args = {
1051 .certipaddr = "127.0.0.1",
1053 return test_migrate_tls_x509_start_common(from, to, &args);
1057 * The case with no client certificate presented,
1058 * and server verification rejecting
1060 static void *
1061 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1062 QTestState *to)
1064 TestMigrateTLSX509 args = {
1065 .verifyclient = true,
1066 .certipaddr = "127.0.0.1",
1068 return test_migrate_tls_x509_start_common(from, to, &args);
1071 static void
1072 test_migrate_tls_x509_finish(QTestState *from,
1073 QTestState *to,
1074 void *opaque)
1076 TestMigrateTLSX509Data *data = opaque;
1078 test_tls_cleanup(data->keyfile);
1079 g_free(data->keyfile);
1081 unlink(data->cacert);
1082 g_free(data->cacert);
1083 unlink(data->servercert);
1084 g_free(data->servercert);
1085 unlink(data->serverkey);
1086 g_free(data->serverkey);
1088 if (data->clientcert) {
1089 unlink(data->clientcert);
1090 g_free(data->clientcert);
1092 if (data->clientkey) {
1093 unlink(data->clientkey);
1094 g_free(data->clientkey);
1097 rmdir(data->workdir);
1098 g_free(data->workdir);
1100 g_free(data);
1102 #endif /* CONFIG_TASN1 */
1103 #endif /* CONFIG_GNUTLS */
1105 static void *
1106 test_migrate_compress_start(QTestState *from,
1107 QTestState *to)
1109 migrate_set_parameter_int(from, "compress-level", 1);
1110 migrate_set_parameter_int(from, "compress-threads", 4);
1111 migrate_set_parameter_bool(from, "compress-wait-thread", true);
1112 migrate_set_parameter_int(to, "decompress-threads", 4);
1114 migrate_set_capability(from, "compress", true);
1115 migrate_set_capability(to, "compress", true);
1117 return NULL;
1120 static void *
1121 test_migrate_compress_nowait_start(QTestState *from,
1122 QTestState *to)
1124 migrate_set_parameter_int(from, "compress-level", 9);
1125 migrate_set_parameter_int(from, "compress-threads", 1);
1126 migrate_set_parameter_bool(from, "compress-wait-thread", false);
1127 migrate_set_parameter_int(to, "decompress-threads", 1);
1129 migrate_set_capability(from, "compress", true);
1130 migrate_set_capability(to, "compress", true);
1132 return NULL;
1135 static int migrate_postcopy_prepare(QTestState **from_ptr,
1136 QTestState **to_ptr,
1137 MigrateCommon *args)
1139 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1140 QTestState *from, *to;
1142 if (test_migrate_start(&from, &to, uri, &args->start)) {
1143 return -1;
1146 if (args->start_hook) {
1147 args->postcopy_data = args->start_hook(from, to);
1150 migrate_set_capability(from, "postcopy-ram", true);
1151 migrate_set_capability(to, "postcopy-ram", true);
1152 migrate_set_capability(to, "postcopy-blocktime", true);
1154 if (args->postcopy_preempt) {
1155 migrate_set_capability(from, "postcopy-preempt", true);
1156 migrate_set_capability(to, "postcopy-preempt", true);
1159 migrate_ensure_non_converge(from);
1161 /* Wait for the first serial output from the source */
1162 wait_for_serial("src_serial");
1164 migrate_qmp(from, uri, "{}");
1166 wait_for_migration_pass(from);
1168 *from_ptr = from;
1169 *to_ptr = to;
1171 return 0;
1174 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1175 MigrateCommon *args)
1177 wait_for_migration_complete(from);
1179 /* Make sure we get at least one "B" on destination */
1180 wait_for_serial("dest_serial");
1182 if (uffd_feature_thread_id) {
1183 read_blocktime(to);
1186 if (args->finish_hook) {
1187 args->finish_hook(from, to, args->postcopy_data);
1188 args->postcopy_data = NULL;
1191 test_migrate_end(from, to, true);
1194 static void test_postcopy_common(MigrateCommon *args)
1196 QTestState *from, *to;
1198 if (migrate_postcopy_prepare(&from, &to, args)) {
1199 return;
1201 migrate_postcopy_start(from, to);
1202 migrate_postcopy_complete(from, to, args);
1205 static void test_postcopy(void)
1207 MigrateCommon args = { };
1209 test_postcopy_common(&args);
1212 static void test_postcopy_compress(void)
1214 MigrateCommon args = {
1215 .start_hook = test_migrate_compress_start
1218 test_postcopy_common(&args);
1221 static void test_postcopy_preempt(void)
1223 MigrateCommon args = {
1224 .postcopy_preempt = true,
1227 test_postcopy_common(&args);
1230 #ifdef CONFIG_GNUTLS
1231 static void test_postcopy_tls_psk(void)
1233 MigrateCommon args = {
1234 .start_hook = test_migrate_tls_psk_start_match,
1235 .finish_hook = test_migrate_tls_psk_finish,
1238 test_postcopy_common(&args);
1241 static void test_postcopy_preempt_tls_psk(void)
1243 MigrateCommon args = {
1244 .postcopy_preempt = true,
1245 .start_hook = test_migrate_tls_psk_start_match,
1246 .finish_hook = test_migrate_tls_psk_finish,
1249 test_postcopy_common(&args);
1251 #endif
1253 static void test_postcopy_recovery_common(MigrateCommon *args)
1255 QTestState *from, *to;
1256 g_autofree char *uri = NULL;
1258 /* Always hide errors for postcopy recover tests since they're expected */
1259 args->start.hide_stderr = true;
1261 if (migrate_postcopy_prepare(&from, &to, args)) {
1262 return;
1265 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1266 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1268 /* Now we start the postcopy */
1269 migrate_postcopy_start(from, to);
1272 * Wait until postcopy is really started; we can only run the
1273 * migrate-pause command during a postcopy
1275 wait_for_migration_status(from, "postcopy-active", NULL);
1278 * Manually stop the postcopy migration. This emulates a network
1279 * failure with the migration socket
1281 migrate_pause(from);
1284 * Wait for destination side to reach postcopy-paused state. The
1285 * migrate-recover command can only succeed if destination machine
1286 * is in the paused state
1288 wait_for_migration_status(to, "postcopy-paused",
1289 (const char * []) { "failed", "active",
1290 "completed", NULL });
1293 * Create a new socket to emulate a new channel that is different
1294 * from the broken migration channel; tell the destination to
1295 * listen to the new port
1297 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1298 migrate_recover(to, uri);
1301 * Try to rebuild the migration channel using the resume flag and
1302 * the newly created channel
1304 wait_for_migration_status(from, "postcopy-paused",
1305 (const char * []) { "failed", "active",
1306 "completed", NULL });
1307 migrate_qmp(from, uri, "{'resume': true}");
1309 /* Restore the postcopy bandwidth to unlimited */
1310 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1312 migrate_postcopy_complete(from, to, args);
1315 static void test_postcopy_recovery(void)
1317 MigrateCommon args = { };
1319 test_postcopy_recovery_common(&args);
1322 static void test_postcopy_recovery_compress(void)
1324 MigrateCommon args = {
1325 .start_hook = test_migrate_compress_start
1328 test_postcopy_recovery_common(&args);
1331 #ifdef CONFIG_GNUTLS
1332 static void test_postcopy_recovery_tls_psk(void)
1334 MigrateCommon args = {
1335 .start_hook = test_migrate_tls_psk_start_match,
1336 .finish_hook = test_migrate_tls_psk_finish,
1339 test_postcopy_recovery_common(&args);
1341 #endif
1343 static void test_postcopy_preempt_recovery(void)
1345 MigrateCommon args = {
1346 .postcopy_preempt = true,
1349 test_postcopy_recovery_common(&args);
1352 #ifdef CONFIG_GNUTLS
1353 /* This contains preempt+recovery+tls test altogether */
1354 static void test_postcopy_preempt_all(void)
1356 MigrateCommon args = {
1357 .postcopy_preempt = true,
1358 .start_hook = test_migrate_tls_psk_start_match,
1359 .finish_hook = test_migrate_tls_psk_finish,
1362 test_postcopy_recovery_common(&args);
1365 #endif
1367 static void test_baddest(void)
1369 MigrateStart args = {
1370 .hide_stderr = true
1372 QTestState *from, *to;
1374 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1375 return;
1377 migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1378 wait_for_migration_fail(from, false);
1379 test_migrate_end(from, to, false);
1382 static void test_precopy_common(MigrateCommon *args)
1384 QTestState *from, *to;
1385 void *data_hook = NULL;
1387 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1388 return;
1391 if (args->start_hook) {
1392 data_hook = args->start_hook(from, to);
1395 /* Wait for the first serial output from the source */
1396 if (args->result == MIG_TEST_SUCCEED) {
1397 wait_for_serial("src_serial");
1400 if (args->live) {
1402 * Testing live migration, we want to ensure that some
1403 * memory is re-dirtied after being transferred, so that
1404 * we exercise logic for dirty page handling. We achieve
1405 * this with a ridiculosly low bandwidth that guarantees
1406 * non-convergance.
1408 migrate_ensure_non_converge(from);
1409 } else {
1411 * Testing non-live migration, we allow it to run at
1412 * full speed to ensure short test case duration.
1413 * For tests expected to fail, we don't need to
1414 * change anything.
1416 if (args->result == MIG_TEST_SUCCEED) {
1417 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1418 if (!got_src_stop) {
1419 qtest_qmp_eventwait(from, "STOP");
1421 migrate_ensure_converge(from);
1425 if (!args->connect_uri) {
1426 g_autofree char *local_connect_uri =
1427 migrate_get_socket_address(to, "socket-address");
1428 migrate_qmp(from, local_connect_uri, "{}");
1429 } else {
1430 migrate_qmp(from, args->connect_uri, "{}");
1434 if (args->result != MIG_TEST_SUCCEED) {
1435 bool allow_active = args->result == MIG_TEST_FAIL;
1436 wait_for_migration_fail(from, allow_active);
1438 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1439 qtest_set_expected_status(to, EXIT_FAILURE);
1441 } else {
1442 if (args->live) {
1443 if (args->iterations) {
1444 while (args->iterations--) {
1445 wait_for_migration_pass(from);
1447 } else {
1448 wait_for_migration_pass(from);
1451 migrate_ensure_converge(from);
1454 * We do this first, as it has a timeout to stop us
1455 * hanging forever if migration didn't converge
1457 wait_for_migration_complete(from);
1459 if (!got_src_stop) {
1460 qtest_qmp_eventwait(from, "STOP");
1462 } else {
1463 wait_for_migration_complete(from);
1465 * Must wait for dst to finish reading all incoming
1466 * data on the socket before issuing 'cont' otherwise
1467 * it'll be ignored
1469 wait_for_migration_complete(to);
1471 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1474 if (!got_dst_resume) {
1475 qtest_qmp_eventwait(to, "RESUME");
1478 wait_for_serial("dest_serial");
1481 if (args->finish_hook) {
1482 args->finish_hook(from, to, data_hook);
1485 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1488 static void test_precopy_unix_plain(void)
1490 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1491 MigrateCommon args = {
1492 .listen_uri = uri,
1493 .connect_uri = uri,
1495 .live = true,
1498 test_precopy_common(&args);
1502 static void test_precopy_unix_dirty_ring(void)
1504 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1505 MigrateCommon args = {
1506 .start = {
1507 .use_dirty_ring = true,
1509 .listen_uri = uri,
1510 .connect_uri = uri,
1512 .live = true,
1515 test_precopy_common(&args);
1518 #ifdef CONFIG_GNUTLS
1519 static void test_precopy_unix_tls_psk(void)
1521 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1522 MigrateCommon args = {
1523 .connect_uri = uri,
1524 .listen_uri = uri,
1525 .start_hook = test_migrate_tls_psk_start_match,
1526 .finish_hook = test_migrate_tls_psk_finish,
1529 test_precopy_common(&args);
1532 #ifdef CONFIG_TASN1
1533 static void test_precopy_unix_tls_x509_default_host(void)
1535 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1536 MigrateCommon args = {
1537 .start = {
1538 .hide_stderr = true,
1540 .connect_uri = uri,
1541 .listen_uri = uri,
1542 .start_hook = test_migrate_tls_x509_start_default_host,
1543 .finish_hook = test_migrate_tls_x509_finish,
1544 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1547 test_precopy_common(&args);
1550 static void test_precopy_unix_tls_x509_override_host(void)
1552 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1553 MigrateCommon args = {
1554 .connect_uri = uri,
1555 .listen_uri = uri,
1556 .start_hook = test_migrate_tls_x509_start_override_host,
1557 .finish_hook = test_migrate_tls_x509_finish,
1560 test_precopy_common(&args);
1562 #endif /* CONFIG_TASN1 */
1563 #endif /* CONFIG_GNUTLS */
1565 #if 0
1566 /* Currently upset on aarch64 TCG */
1567 static void test_ignore_shared(void)
1569 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1570 QTestState *from, *to;
1572 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1573 return;
1576 migrate_set_capability(from, "x-ignore-shared", true);
1577 migrate_set_capability(to, "x-ignore-shared", true);
1579 /* Wait for the first serial output from the source */
1580 wait_for_serial("src_serial");
1582 migrate_qmp(from, uri, "{}");
1584 wait_for_migration_pass(from);
1586 if (!got_src_stop) {
1587 qtest_qmp_eventwait(from, "STOP");
1590 qtest_qmp_eventwait(to, "RESUME");
1592 wait_for_serial("dest_serial");
1593 wait_for_migration_complete(from);
1595 /* Check whether shared RAM has been really skipped */
1596 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1598 test_migrate_end(from, to, true);
1600 #endif
1602 static void *
1603 test_migrate_xbzrle_start(QTestState *from,
1604 QTestState *to)
1606 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1608 migrate_set_capability(from, "xbzrle", true);
1609 migrate_set_capability(to, "xbzrle", true);
1611 return NULL;
1614 static void test_precopy_unix_xbzrle(void)
1616 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1617 MigrateCommon args = {
1618 .connect_uri = uri,
1619 .listen_uri = uri,
1621 .start_hook = test_migrate_xbzrle_start,
1623 .iterations = 2,
1624 .live = true,
1627 test_precopy_common(&args);
1630 static void test_precopy_unix_compress(void)
1632 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1633 MigrateCommon args = {
1634 .connect_uri = uri,
1635 .listen_uri = uri,
1636 .start_hook = test_migrate_compress_start,
1638 * Test that no invalid thread state is left over from
1639 * the previous iteration.
1641 .iterations = 2,
1642 .live = true,
1645 test_precopy_common(&args);
1648 static void test_precopy_unix_compress_nowait(void)
1650 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1651 MigrateCommon args = {
1652 .connect_uri = uri,
1653 .listen_uri = uri,
1654 .start_hook = test_migrate_compress_nowait_start,
1656 * Test that no invalid thread state is left over from
1657 * the previous iteration.
1659 .iterations = 2,
1660 .live = true,
1663 test_precopy_common(&args);
1666 static void test_precopy_tcp_plain(void)
1668 MigrateCommon args = {
1669 .listen_uri = "tcp:127.0.0.1:0",
1672 test_precopy_common(&args);
1675 #ifdef CONFIG_GNUTLS
1676 static void test_precopy_tcp_tls_psk_match(void)
1678 MigrateCommon args = {
1679 .listen_uri = "tcp:127.0.0.1:0",
1680 .start_hook = test_migrate_tls_psk_start_match,
1681 .finish_hook = test_migrate_tls_psk_finish,
1684 test_precopy_common(&args);
1687 static void test_precopy_tcp_tls_psk_mismatch(void)
1689 MigrateCommon args = {
1690 .start = {
1691 .hide_stderr = true,
1693 .listen_uri = "tcp:127.0.0.1:0",
1694 .start_hook = test_migrate_tls_psk_start_mismatch,
1695 .finish_hook = test_migrate_tls_psk_finish,
1696 .result = MIG_TEST_FAIL,
1699 test_precopy_common(&args);
1702 #ifdef CONFIG_TASN1
1703 static void test_precopy_tcp_tls_x509_default_host(void)
1705 MigrateCommon args = {
1706 .listen_uri = "tcp:127.0.0.1:0",
1707 .start_hook = test_migrate_tls_x509_start_default_host,
1708 .finish_hook = test_migrate_tls_x509_finish,
1711 test_precopy_common(&args);
1714 static void test_precopy_tcp_tls_x509_override_host(void)
1716 MigrateCommon args = {
1717 .listen_uri = "tcp:127.0.0.1:0",
1718 .start_hook = test_migrate_tls_x509_start_override_host,
1719 .finish_hook = test_migrate_tls_x509_finish,
1722 test_precopy_common(&args);
1725 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1727 MigrateCommon args = {
1728 .start = {
1729 .hide_stderr = true,
1731 .listen_uri = "tcp:127.0.0.1:0",
1732 .start_hook = test_migrate_tls_x509_start_mismatch_host,
1733 .finish_hook = test_migrate_tls_x509_finish,
1734 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1737 test_precopy_common(&args);
1740 static void test_precopy_tcp_tls_x509_friendly_client(void)
1742 MigrateCommon args = {
1743 .listen_uri = "tcp:127.0.0.1:0",
1744 .start_hook = test_migrate_tls_x509_start_friendly_client,
1745 .finish_hook = test_migrate_tls_x509_finish,
1748 test_precopy_common(&args);
1751 static void test_precopy_tcp_tls_x509_hostile_client(void)
1753 MigrateCommon args = {
1754 .start = {
1755 .hide_stderr = true,
1757 .listen_uri = "tcp:127.0.0.1:0",
1758 .start_hook = test_migrate_tls_x509_start_hostile_client,
1759 .finish_hook = test_migrate_tls_x509_finish,
1760 .result = MIG_TEST_FAIL,
1763 test_precopy_common(&args);
1766 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1768 MigrateCommon args = {
1769 .listen_uri = "tcp:127.0.0.1:0",
1770 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1771 .finish_hook = test_migrate_tls_x509_finish,
1774 test_precopy_common(&args);
1777 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1779 MigrateCommon args = {
1780 .start = {
1781 .hide_stderr = true,
1783 .listen_uri = "tcp:127.0.0.1:0",
1784 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1785 .finish_hook = test_migrate_tls_x509_finish,
1786 .result = MIG_TEST_FAIL,
1789 test_precopy_common(&args);
1791 #endif /* CONFIG_TASN1 */
1792 #endif /* CONFIG_GNUTLS */
1794 #ifndef _WIN32
1795 static void *test_migrate_fd_start_hook(QTestState *from,
1796 QTestState *to)
1798 int ret;
1799 int pair[2];
1801 /* Create two connected sockets for migration */
1802 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1803 g_assert_cmpint(ret, ==, 0);
1805 /* Send the 1st socket to the target */
1806 qtest_qmp_fds_assert_success(to, &pair[0], 1,
1807 "{ 'execute': 'getfd',"
1808 " 'arguments': { 'fdname': 'fd-mig' }}");
1809 close(pair[0]);
1811 /* Start incoming migration from the 1st socket */
1812 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1813 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1815 /* Send the 2nd socket to the target */
1816 qtest_qmp_fds_assert_success(from, &pair[1], 1,
1817 "{ 'execute': 'getfd',"
1818 " 'arguments': { 'fdname': 'fd-mig' }}");
1819 close(pair[1]);
1821 return NULL;
1824 static void test_migrate_fd_finish_hook(QTestState *from,
1825 QTestState *to,
1826 void *opaque)
1828 QDict *rsp;
1829 const char *error_desc;
1831 /* Test closing fds */
1832 /* We assume, that QEMU removes named fd from its list,
1833 * so this should fail */
1834 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1835 " 'arguments': { 'fdname': 'fd-mig' }}");
1836 g_assert_true(qdict_haskey(rsp, "error"));
1837 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1838 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1839 qobject_unref(rsp);
1841 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1842 " 'arguments': { 'fdname': 'fd-mig' }}");
1843 g_assert_true(qdict_haskey(rsp, "error"));
1844 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1845 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1846 qobject_unref(rsp);
1849 static void test_migrate_fd_proto(void)
1851 MigrateCommon args = {
1852 .listen_uri = "defer",
1853 .connect_uri = "fd:fd-mig",
1854 .start_hook = test_migrate_fd_start_hook,
1855 .finish_hook = test_migrate_fd_finish_hook
1857 test_precopy_common(&args);
1859 #endif /* _WIN32 */
1861 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1863 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1864 QTestState *from, *to;
1866 if (test_migrate_start(&from, &to, uri, args)) {
1867 return;
1871 * UUID validation is at the begin of migration. So, the main process of
1872 * migration is not interesting for us here. Thus, set huge downtime for
1873 * very fast migration.
1875 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1876 migrate_set_capability(from, "validate-uuid", true);
1878 /* Wait for the first serial output from the source */
1879 wait_for_serial("src_serial");
1881 migrate_qmp(from, uri, "{}");
1883 if (should_fail) {
1884 qtest_set_expected_status(to, EXIT_FAILURE);
1885 wait_for_migration_fail(from, true);
1886 } else {
1887 wait_for_migration_complete(from);
1890 test_migrate_end(from, to, false);
1893 static void test_validate_uuid(void)
1895 MigrateStart args = {
1896 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1897 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
1900 do_test_validate_uuid(&args, false);
1903 static void test_validate_uuid_error(void)
1905 MigrateStart args = {
1906 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1907 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1908 .hide_stderr = true,
1911 do_test_validate_uuid(&args, true);
1914 static void test_validate_uuid_src_not_set(void)
1916 MigrateStart args = {
1917 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1918 .hide_stderr = true,
1921 do_test_validate_uuid(&args, false);
1924 static void test_validate_uuid_dst_not_set(void)
1926 MigrateStart args = {
1927 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1928 .hide_stderr = true,
1931 do_test_validate_uuid(&args, false);
1935 * The way auto_converge works, we need to do too many passes to
1936 * run this test. Auto_converge logic is only run once every
1937 * three iterations, so:
1939 * - 3 iterations without auto_converge enabled
1940 * - 3 iterations with pct = 5
1941 * - 3 iterations with pct = 30
1942 * - 3 iterations with pct = 55
1943 * - 3 iterations with pct = 80
1944 * - 3 iterations with pct = 95 (max(95, 80 + 25))
1946 * To make things even worse, we need to run the initial stage at
1947 * 3MB/s so we enter autoconverge even when host is (over)loaded.
1949 static void test_migrate_auto_converge(void)
1951 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1952 MigrateStart args = {};
1953 QTestState *from, *to;
1954 int64_t percentage;
1957 * We want the test to be stable and as fast as possible.
1958 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1959 * so we need to decrease a bandwidth.
1961 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
1963 if (test_migrate_start(&from, &to, uri, &args)) {
1964 return;
1967 migrate_set_capability(from, "auto-converge", true);
1968 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1969 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1970 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1973 * Set the initial parameters so that the migration could not converge
1974 * without throttling.
1976 migrate_ensure_non_converge(from);
1978 /* To check remaining size after precopy */
1979 migrate_set_capability(from, "pause-before-switchover", true);
1981 /* Wait for the first serial output from the source */
1982 wait_for_serial("src_serial");
1984 migrate_qmp(from, uri, "{}");
1986 /* Wait for throttling begins */
1987 percentage = 0;
1988 do {
1989 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1990 if (percentage != 0) {
1991 break;
1993 usleep(20);
1994 g_assert_false(got_src_stop);
1995 } while (true);
1996 /* The first percentage of throttling should be at least init_pct */
1997 g_assert_cmpint(percentage, >=, init_pct);
1998 /* Now, when we tested that throttling works, let it converge */
1999 migrate_ensure_converge(from);
2002 * Wait for pre-switchover status to check last throttle percentage
2003 * and remaining. These values will be zeroed later
2005 wait_for_migration_status(from, "pre-switchover", NULL);
2007 /* The final percentage of throttling shouldn't be greater than max_pct */
2008 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2009 g_assert_cmpint(percentage, <=, max_pct);
2010 migrate_continue(from, "pre-switchover");
2012 qtest_qmp_eventwait(to, "RESUME");
2014 wait_for_serial("dest_serial");
2015 wait_for_migration_complete(from);
2017 test_migrate_end(from, to, true);
2020 static void *
2021 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2022 QTestState *to,
2023 const char *method)
2025 migrate_set_parameter_int(from, "multifd-channels", 16);
2026 migrate_set_parameter_int(to, "multifd-channels", 16);
2028 migrate_set_parameter_str(from, "multifd-compression", method);
2029 migrate_set_parameter_str(to, "multifd-compression", method);
2031 migrate_set_capability(from, "multifd", true);
2032 migrate_set_capability(to, "multifd", true);
2034 /* Start incoming migration from the 1st socket */
2035 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2036 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2038 return NULL;
2041 static void *
2042 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2043 QTestState *to)
2045 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2048 static void *
2049 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2050 QTestState *to)
2052 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2055 #ifdef CONFIG_ZSTD
2056 static void *
2057 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2058 QTestState *to)
2060 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2062 #endif /* CONFIG_ZSTD */
2064 static void test_multifd_tcp_none(void)
2066 MigrateCommon args = {
2067 .listen_uri = "defer",
2068 .start_hook = test_migrate_precopy_tcp_multifd_start,
2070 .live = true,
2072 test_precopy_common(&args);
2075 static void test_multifd_tcp_zlib(void)
2077 MigrateCommon args = {
2078 .listen_uri = "defer",
2079 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2081 test_precopy_common(&args);
2084 #ifdef CONFIG_ZSTD
2085 static void test_multifd_tcp_zstd(void)
2087 MigrateCommon args = {
2088 .listen_uri = "defer",
2089 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2091 test_precopy_common(&args);
2093 #endif
2095 #ifdef CONFIG_GNUTLS
2096 static void *
2097 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2098 QTestState *to)
2100 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2101 return test_migrate_tls_psk_start_match(from, to);
2104 static void *
2105 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2106 QTestState *to)
2108 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2109 return test_migrate_tls_psk_start_mismatch(from, to);
2112 #ifdef CONFIG_TASN1
2113 static void *
2114 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2115 QTestState *to)
2117 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2118 return test_migrate_tls_x509_start_default_host(from, to);
2121 static void *
2122 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2123 QTestState *to)
2125 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2126 return test_migrate_tls_x509_start_override_host(from, to);
2129 static void *
2130 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2131 QTestState *to)
2133 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2134 return test_migrate_tls_x509_start_mismatch_host(from, to);
2137 static void *
2138 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2139 QTestState *to)
2141 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2142 return test_migrate_tls_x509_start_allow_anon_client(from, to);
2145 static void *
2146 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2147 QTestState *to)
2149 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2150 return test_migrate_tls_x509_start_reject_anon_client(from, to);
2152 #endif /* CONFIG_TASN1 */
2154 static void test_multifd_tcp_tls_psk_match(void)
2156 MigrateCommon args = {
2157 .listen_uri = "defer",
2158 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2159 .finish_hook = test_migrate_tls_psk_finish,
2161 test_precopy_common(&args);
2164 static void test_multifd_tcp_tls_psk_mismatch(void)
2166 MigrateCommon args = {
2167 .start = {
2168 .hide_stderr = true,
2170 .listen_uri = "defer",
2171 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2172 .finish_hook = test_migrate_tls_psk_finish,
2173 .result = MIG_TEST_FAIL,
2175 test_precopy_common(&args);
2178 #ifdef CONFIG_TASN1
2179 static void test_multifd_tcp_tls_x509_default_host(void)
2181 MigrateCommon args = {
2182 .listen_uri = "defer",
2183 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2184 .finish_hook = test_migrate_tls_x509_finish,
2186 test_precopy_common(&args);
2189 static void test_multifd_tcp_tls_x509_override_host(void)
2191 MigrateCommon args = {
2192 .listen_uri = "defer",
2193 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2194 .finish_hook = test_migrate_tls_x509_finish,
2196 test_precopy_common(&args);
2199 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2202 * This has different behaviour to the non-multifd case.
2204 * In non-multifd case when client aborts due to mismatched
2205 * cert host, the server has already started trying to load
2206 * migration state, and so it exits with I/O failure.
2208 * In multifd case when client aborts due to mismatched
2209 * cert host, the server is still waiting for the other
2210 * multifd connections to arrive so hasn't started trying
2211 * to load migration state, and thus just aborts the migration
2212 * without exiting.
2214 MigrateCommon args = {
2215 .start = {
2216 .hide_stderr = true,
2218 .listen_uri = "defer",
2219 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2220 .finish_hook = test_migrate_tls_x509_finish,
2221 .result = MIG_TEST_FAIL,
2223 test_precopy_common(&args);
2226 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2228 MigrateCommon args = {
2229 .listen_uri = "defer",
2230 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2231 .finish_hook = test_migrate_tls_x509_finish,
2233 test_precopy_common(&args);
2236 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2238 MigrateCommon args = {
2239 .start = {
2240 .hide_stderr = true,
2242 .listen_uri = "defer",
2243 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2244 .finish_hook = test_migrate_tls_x509_finish,
2245 .result = MIG_TEST_FAIL,
2247 test_precopy_common(&args);
2249 #endif /* CONFIG_TASN1 */
2250 #endif /* CONFIG_GNUTLS */
2253 * This test does:
2254 * source target
2255 * migrate_incoming
2256 * migrate
2257 * migrate_cancel
2258 * launch another target
2259 * migrate
2261 * And see that it works
2263 static void test_multifd_tcp_cancel(void)
2265 MigrateStart args = {
2266 .hide_stderr = true,
2268 QTestState *from, *to, *to2;
2269 g_autofree char *uri = NULL;
2271 if (test_migrate_start(&from, &to, "defer", &args)) {
2272 return;
2275 migrate_ensure_non_converge(from);
2277 migrate_set_parameter_int(from, "multifd-channels", 16);
2278 migrate_set_parameter_int(to, "multifd-channels", 16);
2280 migrate_set_capability(from, "multifd", true);
2281 migrate_set_capability(to, "multifd", true);
2283 /* Start incoming migration from the 1st socket */
2284 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2285 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2287 /* Wait for the first serial output from the source */
2288 wait_for_serial("src_serial");
2290 uri = migrate_get_socket_address(to, "socket-address");
2292 migrate_qmp(from, uri, "{}");
2294 wait_for_migration_pass(from);
2296 migrate_cancel(from);
2298 /* Make sure QEMU process "to" exited */
2299 qtest_set_expected_status(to, EXIT_FAILURE);
2300 qtest_wait_qemu(to);
2302 args = (MigrateStart){
2303 .only_target = true,
2306 if (test_migrate_start(&from, &to2, "defer", &args)) {
2307 return;
2310 migrate_set_parameter_int(to2, "multifd-channels", 16);
2312 migrate_set_capability(to2, "multifd", true);
2314 /* Start incoming migration from the 1st socket */
2315 qtest_qmp_assert_success(to2, "{ 'execute': 'migrate-incoming',"
2316 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2318 g_free(uri);
2319 uri = migrate_get_socket_address(to2, "socket-address");
2321 wait_for_migration_status(from, "cancelled", NULL);
2323 migrate_ensure_converge(from);
2325 migrate_qmp(from, uri, "{}");
2327 wait_for_migration_pass(from);
2329 if (!got_src_stop) {
2330 qtest_qmp_eventwait(from, "STOP");
2332 qtest_qmp_eventwait(to2, "RESUME");
2334 wait_for_serial("dest_serial");
2335 wait_for_migration_complete(from);
2336 test_migrate_end(from, to2, true);
2339 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2341 qtest_qmp_assert_success(who,
2342 "{ 'execute': 'calc-dirty-rate',"
2343 "'arguments': { "
2344 "'calc-time': %" PRIu64 ","
2345 "'mode': 'dirty-ring' }}",
2346 calc_time);
2349 static QDict *query_dirty_rate(QTestState *who)
2351 return qtest_qmp_assert_success_ref(who,
2352 "{ 'execute': 'query-dirty-rate' }");
2355 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
2357 qtest_qmp_assert_success(who,
2358 "{ 'execute': 'set-vcpu-dirty-limit',"
2359 "'arguments': { "
2360 "'dirty-rate': %" PRIu64 " } }",
2361 dirtyrate);
2364 static void cancel_vcpu_dirty_limit(QTestState *who)
2366 qtest_qmp_assert_success(who,
2367 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2370 static QDict *query_vcpu_dirty_limit(QTestState *who)
2372 QDict *rsp;
2374 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
2375 g_assert(!qdict_haskey(rsp, "error"));
2376 g_assert(qdict_haskey(rsp, "return"));
2378 return rsp;
2381 static bool calc_dirtyrate_ready(QTestState *who)
2383 QDict *rsp_return;
2384 gchar *status;
2386 rsp_return = query_dirty_rate(who);
2387 g_assert(rsp_return);
2389 status = g_strdup(qdict_get_str(rsp_return, "status"));
2390 g_assert(status);
2392 return g_strcmp0(status, "measuring");
2395 static void wait_for_calc_dirtyrate_complete(QTestState *who,
2396 int64_t time_s)
2398 int max_try_count = 10000;
2399 usleep(time_s * 1000000);
2401 while (!calc_dirtyrate_ready(who) && max_try_count--) {
2402 usleep(1000);
2406 * Set the timeout with 10 s(max_try_count * 1000us),
2407 * if dirtyrate measurement not complete, fail test.
2409 g_assert_cmpint(max_try_count, !=, 0);
2412 static int64_t get_dirty_rate(QTestState *who)
2414 QDict *rsp_return;
2415 gchar *status;
2416 QList *rates;
2417 const QListEntry *entry;
2418 QDict *rate;
2419 int64_t dirtyrate;
2421 rsp_return = query_dirty_rate(who);
2422 g_assert(rsp_return);
2424 status = g_strdup(qdict_get_str(rsp_return, "status"));
2425 g_assert(status);
2426 g_assert_cmpstr(status, ==, "measured");
2428 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
2429 g_assert(rates && !qlist_empty(rates));
2431 entry = qlist_first(rates);
2432 g_assert(entry);
2434 rate = qobject_to(QDict, qlist_entry_obj(entry));
2435 g_assert(rate);
2437 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
2439 qobject_unref(rsp_return);
2440 return dirtyrate;
2443 static int64_t get_limit_rate(QTestState *who)
2445 QDict *rsp_return;
2446 QList *rates;
2447 const QListEntry *entry;
2448 QDict *rate;
2449 int64_t dirtyrate;
2451 rsp_return = query_vcpu_dirty_limit(who);
2452 g_assert(rsp_return);
2454 rates = qdict_get_qlist(rsp_return, "return");
2455 g_assert(rates && !qlist_empty(rates));
2457 entry = qlist_first(rates);
2458 g_assert(entry);
2460 rate = qobject_to(QDict, qlist_entry_obj(entry));
2461 g_assert(rate);
2463 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
2465 qobject_unref(rsp_return);
2466 return dirtyrate;
2469 static QTestState *dirtylimit_start_vm(void)
2471 QTestState *vm = NULL;
2472 g_autofree gchar *cmd = NULL;
2473 const char *arch = qtest_get_arch();
2474 g_autofree char *bootpath = NULL;
2476 assert((strcmp(arch, "x86_64") == 0));
2477 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
2478 assert(sizeof(x86_bootsect) == 512);
2479 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
2481 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2482 "-name dirtylimit-test,debug-threads=on "
2483 "-m 150M -smp 1 "
2484 "-serial file:%s/vm_serial "
2485 "-drive file=%s,format=raw ",
2486 tmpfs, bootpath);
2488 vm = qtest_init(cmd);
2489 return vm;
2492 static void dirtylimit_stop_vm(QTestState *vm)
2494 qtest_quit(vm);
2495 cleanup("bootsect");
2496 cleanup("vm_serial");
2499 static void test_vcpu_dirty_limit(void)
2501 QTestState *vm;
2502 int64_t origin_rate;
2503 int64_t quota_rate;
2504 int64_t rate ;
2505 int max_try_count = 20;
2506 int hit = 0;
2508 /* Start vm for vcpu dirtylimit test */
2509 vm = dirtylimit_start_vm();
2511 /* Wait for the first serial output from the vm*/
2512 wait_for_serial("vm_serial");
2514 /* Do dirtyrate measurement with calc time equals 1s */
2515 calc_dirty_rate(vm, 1);
2517 /* Sleep calc time and wait for calc dirtyrate complete */
2518 wait_for_calc_dirtyrate_complete(vm, 1);
2520 /* Query original dirty page rate */
2521 origin_rate = get_dirty_rate(vm);
2523 /* VM booted from bootsect should dirty memory steadily */
2524 assert(origin_rate != 0);
2526 /* Setup quota dirty page rate at half of origin */
2527 quota_rate = origin_rate / 2;
2529 /* Set dirtylimit */
2530 dirtylimit_set_all(vm, quota_rate);
2533 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2534 * works literally
2536 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
2538 /* Sleep a bit to check if it take effect */
2539 usleep(2000000);
2542 * Check if dirtylimit take effect realistically, set the
2543 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2544 * doesn't take effect, fail test.
2546 while (--max_try_count) {
2547 calc_dirty_rate(vm, 1);
2548 wait_for_calc_dirtyrate_complete(vm, 1);
2549 rate = get_dirty_rate(vm);
2552 * Assume hitting if current rate is less
2553 * than quota rate (within accepting error)
2555 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2556 hit = 1;
2557 break;
2561 g_assert_cmpint(hit, ==, 1);
2563 hit = 0;
2564 max_try_count = 20;
2566 /* Check if dirtylimit cancellation take effect */
2567 cancel_vcpu_dirty_limit(vm);
2568 while (--max_try_count) {
2569 calc_dirty_rate(vm, 1);
2570 wait_for_calc_dirtyrate_complete(vm, 1);
2571 rate = get_dirty_rate(vm);
2574 * Assume dirtylimit be canceled if current rate is
2575 * greater than quota rate (within accepting error)
2577 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2578 hit = 1;
2579 break;
2583 g_assert_cmpint(hit, ==, 1);
2584 dirtylimit_stop_vm(vm);
2587 static bool kvm_dirty_ring_supported(void)
2589 #if defined(__linux__) && defined(HOST_X86_64)
2590 int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2592 if (kvm_fd < 0) {
2593 return false;
2596 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2597 close(kvm_fd);
2599 /* We test with 4096 slots */
2600 if (ret < 4096) {
2601 return false;
2604 return true;
2605 #else
2606 return false;
2607 #endif
2610 int main(int argc, char **argv)
2612 bool has_kvm, has_tcg;
2613 bool has_uffd;
2614 const char *arch;
2615 g_autoptr(GError) err = NULL;
2616 int ret;
2618 g_test_init(&argc, &argv, NULL);
2620 has_kvm = qtest_has_accel("kvm");
2621 has_tcg = qtest_has_accel("tcg");
2623 if (!has_tcg && !has_kvm) {
2624 g_test_skip("No KVM or TCG accelerator available");
2625 return 0;
2628 has_uffd = ufd_version_check();
2629 arch = qtest_get_arch();
2632 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2633 * is touchy due to race conditions on dirty bits (especially on PPC for
2634 * some reason)
2636 if (g_str_equal(arch, "ppc64") &&
2637 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2638 g_test_message("Skipping test: kvm_hv not available");
2639 return g_test_run();
2643 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2644 * there until the problems are resolved
2646 if (g_str_equal(arch, "s390x") && !has_kvm) {
2647 g_test_message("Skipping test: s390x host with KVM is required");
2648 return g_test_run();
2651 tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
2652 if (!tmpfs) {
2653 g_test_message("Can't create temporary directory in %s: %s",
2654 g_get_tmp_dir(), err->message);
2656 g_assert(tmpfs);
2658 module_call_init(MODULE_INIT_QOM);
2660 if (has_uffd) {
2661 qtest_add_func("/migration/postcopy/plain", test_postcopy);
2662 qtest_add_func("/migration/postcopy/recovery/plain",
2663 test_postcopy_recovery);
2664 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
2665 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2666 test_postcopy_preempt_recovery);
2667 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2668 qtest_add_func("/migration/postcopy/compress/plain",
2669 test_postcopy_compress);
2670 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2671 test_postcopy_recovery_compress);
2675 qtest_add_func("/migration/bad_dest", test_baddest);
2676 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2677 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2679 * Compression fails from time to time.
2680 * Put test here but don't enable it until everything is fixed.
2682 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2683 qtest_add_func("/migration/precopy/unix/compress/wait",
2684 test_precopy_unix_compress);
2685 qtest_add_func("/migration/precopy/unix/compress/nowait",
2686 test_precopy_unix_compress_nowait);
2688 #ifdef CONFIG_GNUTLS
2689 qtest_add_func("/migration/precopy/unix/tls/psk",
2690 test_precopy_unix_tls_psk);
2692 if (has_uffd) {
2694 * NOTE: psk test is enough for postcopy, as other types of TLS
2695 * channels are tested under precopy. Here what we want to test is the
2696 * general postcopy path that has TLS channel enabled.
2698 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
2699 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2700 test_postcopy_recovery_tls_psk);
2701 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2702 test_postcopy_preempt_tls_psk);
2703 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2704 test_postcopy_preempt_all);
2706 #ifdef CONFIG_TASN1
2707 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2708 test_precopy_unix_tls_x509_default_host);
2709 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2710 test_precopy_unix_tls_x509_override_host);
2711 #endif /* CONFIG_TASN1 */
2712 #endif /* CONFIG_GNUTLS */
2714 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2715 #ifdef CONFIG_GNUTLS
2716 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2717 test_precopy_tcp_tls_psk_match);
2718 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2719 test_precopy_tcp_tls_psk_mismatch);
2720 #ifdef CONFIG_TASN1
2721 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2722 test_precopy_tcp_tls_x509_default_host);
2723 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2724 test_precopy_tcp_tls_x509_override_host);
2725 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2726 test_precopy_tcp_tls_x509_mismatch_host);
2727 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2728 test_precopy_tcp_tls_x509_friendly_client);
2729 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2730 test_precopy_tcp_tls_x509_hostile_client);
2731 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2732 test_precopy_tcp_tls_x509_allow_anon_client);
2733 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2734 test_precopy_tcp_tls_x509_reject_anon_client);
2735 #endif /* CONFIG_TASN1 */
2736 #endif /* CONFIG_GNUTLS */
2738 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2739 #ifndef _WIN32
2740 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2741 #endif
2742 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2743 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2744 qtest_add_func("/migration/validate_uuid_src_not_set",
2745 test_validate_uuid_src_not_set);
2746 qtest_add_func("/migration/validate_uuid_dst_not_set",
2747 test_validate_uuid_dst_not_set);
2749 * See explanation why this test is slow on function definition
2751 if (g_test_slow()) {
2752 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2754 qtest_add_func("/migration/multifd/tcp/plain/none",
2755 test_multifd_tcp_none);
2757 * This test is flaky and sometimes fails in CI and otherwise:
2758 * don't run unless user opts in via environment variable.
2760 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2761 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2762 test_multifd_tcp_cancel);
2764 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2765 test_multifd_tcp_zlib);
2766 #ifdef CONFIG_ZSTD
2767 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2768 test_multifd_tcp_zstd);
2769 #endif
2770 #ifdef CONFIG_GNUTLS
2771 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2772 test_multifd_tcp_tls_psk_match);
2773 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2774 test_multifd_tcp_tls_psk_mismatch);
2775 #ifdef CONFIG_TASN1
2776 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2777 test_multifd_tcp_tls_x509_default_host);
2778 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2779 test_multifd_tcp_tls_x509_override_host);
2780 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2781 test_multifd_tcp_tls_x509_mismatch_host);
2782 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2783 test_multifd_tcp_tls_x509_allow_anon_client);
2784 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2785 test_multifd_tcp_tls_x509_reject_anon_client);
2786 #endif /* CONFIG_TASN1 */
2787 #endif /* CONFIG_GNUTLS */
2789 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
2790 qtest_add_func("/migration/dirty_ring",
2791 test_precopy_unix_dirty_ring);
2792 qtest_add_func("/migration/vcpu_dirty_limit",
2793 test_vcpu_dirty_limit);
2796 ret = g_test_run();
2798 g_assert_cmpint(ret, ==, 0);
2800 ret = rmdir(tmpfs);
2801 if (ret != 0) {
2802 g_test_message("unable to rmdir: path (%s): %s",
2803 tmpfs, strerror(errno));
2805 g_free(tmpfs);
2807 return ret;