tests: wait for migration completion before looking for STOP event
[qemu.git] / tests / qtest / migration-test.c
blobac9e303b1f17a618b848bde477abf9521eda1256
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"
28 #include "migration-helpers.h"
29 #include "tests/migration/migration-test.h"
30 #ifdef CONFIG_GNUTLS
31 # include "tests/unit/crypto-tls-psk-helpers.h"
32 # ifdef CONFIG_TASN1
33 # include "tests/unit/crypto-tls-x509-helpers.h"
34 # endif /* CONFIG_TASN1 */
35 #endif /* CONFIG_GNUTLS */
37 /* For dirty ring test; so far only x86_64 is supported */
38 #if defined(__linux__) && defined(HOST_X86_64)
39 #include "linux/kvm.h"
40 #endif
42 /* TODO actually test the results and get rid of this */
43 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
45 unsigned start_address;
46 unsigned end_address;
47 static bool uffd_feature_thread_id;
49 /* A downtime where the test really should converge */
50 #define CONVERGE_DOWNTIME 1000
52 #if defined(__linux__)
53 #include <sys/syscall.h>
54 #include <sys/vfs.h>
55 #endif
57 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
58 #include <sys/eventfd.h>
59 #include <sys/ioctl.h>
60 #include <linux/userfaultfd.h>
62 static bool ufd_version_check(void)
64 struct uffdio_api api_struct;
65 uint64_t ioctl_mask;
67 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
69 if (ufd == -1) {
70 g_test_message("Skipping test: userfaultfd not available");
71 return false;
74 api_struct.api = UFFD_API;
75 api_struct.features = 0;
76 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
77 g_test_message("Skipping test: UFFDIO_API failed");
78 return false;
80 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
82 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
83 (__u64)1 << _UFFDIO_UNREGISTER;
84 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
85 g_test_message("Skipping test: Missing userfault feature");
86 return false;
89 return true;
92 #else
93 static bool ufd_version_check(void)
95 g_test_message("Skipping test: Userfault not available (builtdtime)");
96 return false;
99 #endif
101 static const char *tmpfs;
103 /* The boot file modifies memory area in [start_address, end_address)
104 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
106 #include "tests/migration/i386/a-b-bootblock.h"
107 #include "tests/migration/aarch64/a-b-kernel.h"
108 #include "tests/migration/s390x/a-b-bios.h"
110 static void init_bootfile(const char *bootpath, void *content, size_t len)
112 FILE *bootfile = fopen(bootpath, "wb");
114 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
115 fclose(bootfile);
119 * Wait for some output in the serial output file,
120 * we get an 'A' followed by an endless string of 'B's
121 * but on the destination we won't have the A.
123 static void wait_for_serial(const char *side)
125 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
126 FILE *serialfile = fopen(serialpath, "r");
127 const char *arch = qtest_get_arch();
128 int started = (strcmp(side, "src_serial") == 0 &&
129 strcmp(arch, "ppc64") == 0) ? 0 : 1;
131 do {
132 int readvalue = fgetc(serialfile);
134 if (!started) {
135 /* SLOF prints its banner before starting test,
136 * to ignore it, mark the start of the test with '_',
137 * ignore all characters until this marker
139 switch (readvalue) {
140 case '_':
141 started = 1;
142 break;
143 case EOF:
144 fseek(serialfile, 0, SEEK_SET);
145 usleep(1000);
146 break;
148 continue;
150 switch (readvalue) {
151 case 'A':
152 /* Fine */
153 break;
155 case 'B':
156 /* It's alive! */
157 fclose(serialfile);
158 return;
160 case EOF:
161 started = (strcmp(side, "src_serial") == 0 &&
162 strcmp(arch, "ppc64") == 0) ? 0 : 1;
163 fseek(serialfile, 0, SEEK_SET);
164 usleep(1000);
165 break;
167 default:
168 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
169 g_assert_not_reached();
171 } while (true);
175 * It's tricky to use qemu's migration event capability with qtest,
176 * events suddenly appearing confuse the qmp()/hmp() responses.
179 static int64_t read_ram_property_int(QTestState *who, const char *property)
181 QDict *rsp_return, *rsp_ram;
182 int64_t result;
184 rsp_return = migrate_query_not_failed(who);
185 if (!qdict_haskey(rsp_return, "ram")) {
186 /* Still in setup */
187 result = 0;
188 } else {
189 rsp_ram = qdict_get_qdict(rsp_return, "ram");
190 result = qdict_get_try_int(rsp_ram, property, 0);
192 qobject_unref(rsp_return);
193 return result;
196 static int64_t read_migrate_property_int(QTestState *who, const char *property)
198 QDict *rsp_return;
199 int64_t result;
201 rsp_return = migrate_query_not_failed(who);
202 result = qdict_get_try_int(rsp_return, property, 0);
203 qobject_unref(rsp_return);
204 return result;
207 static uint64_t get_migration_pass(QTestState *who)
209 return read_ram_property_int(who, "dirty-sync-count");
212 static void read_blocktime(QTestState *who)
214 QDict *rsp_return;
216 rsp_return = migrate_query_not_failed(who);
217 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
218 qobject_unref(rsp_return);
221 static void wait_for_migration_pass(QTestState *who)
223 uint64_t initial_pass = get_migration_pass(who);
224 uint64_t pass;
226 /* Wait for the 1st sync */
227 while (!got_stop && !initial_pass) {
228 usleep(1000);
229 initial_pass = get_migration_pass(who);
232 do {
233 usleep(1000);
234 pass = get_migration_pass(who);
235 } while (pass == initial_pass && !got_stop);
238 static void check_guests_ram(QTestState *who)
240 /* Our ASM test will have been incrementing one byte from each page from
241 * start_address to < end_address in order. This gives us a constraint
242 * that any page's byte should be equal or less than the previous pages
243 * byte (mod 256); and they should all be equal except for one transition
244 * at the point where we meet the incrementer. (We're running this with
245 * the guest stopped).
247 unsigned address;
248 uint8_t first_byte;
249 uint8_t last_byte;
250 bool hit_edge = false;
251 int bad = 0;
253 qtest_memread(who, start_address, &first_byte, 1);
254 last_byte = first_byte;
256 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
257 address += TEST_MEM_PAGE_SIZE)
259 uint8_t b;
260 qtest_memread(who, address, &b, 1);
261 if (b != last_byte) {
262 if (((b + 1) % 256) == last_byte && !hit_edge) {
263 /* This is OK, the guest stopped at the point of
264 * incrementing the previous page but didn't get
265 * to us yet.
267 hit_edge = true;
268 last_byte = b;
269 } else {
270 bad++;
271 if (bad <= 10) {
272 fprintf(stderr, "Memory content inconsistency at %x"
273 " first_byte = %x last_byte = %x current = %x"
274 " hit_edge = %x\n",
275 address, first_byte, last_byte, b, hit_edge);
280 if (bad >= 10) {
281 fprintf(stderr, "and in another %d pages", bad - 10);
283 g_assert(bad == 0);
286 static void cleanup(const char *filename)
288 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
290 unlink(path);
293 static char *SocketAddress_to_str(SocketAddress *addr)
295 switch (addr->type) {
296 case SOCKET_ADDRESS_TYPE_INET:
297 return g_strdup_printf("tcp:%s:%s",
298 addr->u.inet.host,
299 addr->u.inet.port);
300 case SOCKET_ADDRESS_TYPE_UNIX:
301 return g_strdup_printf("unix:%s",
302 addr->u.q_unix.path);
303 case SOCKET_ADDRESS_TYPE_FD:
304 return g_strdup_printf("fd:%s", addr->u.fd.str);
305 case SOCKET_ADDRESS_TYPE_VSOCK:
306 return g_strdup_printf("tcp:%s:%s",
307 addr->u.vsock.cid,
308 addr->u.vsock.port);
309 default:
310 return g_strdup("unknown address type");
314 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
316 QDict *rsp;
317 char *result;
318 SocketAddressList *addrs;
319 Visitor *iv = NULL;
320 QObject *object;
322 rsp = migrate_query(who);
323 object = qdict_get(rsp, parameter);
325 iv = qobject_input_visitor_new(object);
326 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
327 visit_free(iv);
329 /* we are only using a single address */
330 result = SocketAddress_to_str(addrs->value);
332 qapi_free_SocketAddressList(addrs);
333 qobject_unref(rsp);
334 return result;
337 static long long migrate_get_parameter_int(QTestState *who,
338 const char *parameter)
340 QDict *rsp;
341 long long result;
343 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
344 result = qdict_get_int(rsp, parameter);
345 qobject_unref(rsp);
346 return result;
349 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
350 long long value)
352 long long result;
354 result = migrate_get_parameter_int(who, parameter);
355 g_assert_cmpint(result, ==, value);
358 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
359 long long value)
361 QDict *rsp;
363 rsp = qtest_qmp(who,
364 "{ 'execute': 'migrate-set-parameters',"
365 "'arguments': { %s: %lld } }",
366 parameter, value);
367 g_assert(qdict_haskey(rsp, "return"));
368 qobject_unref(rsp);
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 = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
379 result = g_strdup(qdict_get_str(rsp, parameter));
380 qobject_unref(rsp);
381 return result;
384 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
385 const char *value)
387 g_autofree char *result = migrate_get_parameter_str(who, parameter);
388 g_assert_cmpstr(result, ==, value);
391 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
392 const char *value)
394 QDict *rsp;
396 rsp = qtest_qmp(who,
397 "{ 'execute': 'migrate-set-parameters',"
398 "'arguments': { %s: %s } }",
399 parameter, value);
400 g_assert(qdict_haskey(rsp, "return"));
401 qobject_unref(rsp);
402 migrate_check_parameter_str(who, parameter, value);
405 static void migrate_pause(QTestState *who)
407 QDict *rsp;
409 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
410 qobject_unref(rsp);
413 static void migrate_continue(QTestState *who, const char *state)
415 QDict *rsp;
417 rsp = wait_command(who,
418 "{ 'execute': 'migrate-continue',"
419 " 'arguments': { 'state': %s } }",
420 state);
421 qobject_unref(rsp);
424 static void migrate_recover(QTestState *who, const char *uri)
426 QDict *rsp;
428 rsp = wait_command(who,
429 "{ 'execute': 'migrate-recover', "
430 " 'id': 'recover-cmd', "
431 " 'arguments': { 'uri': %s } }",
432 uri);
433 qobject_unref(rsp);
436 static void migrate_cancel(QTestState *who)
438 QDict *rsp;
440 rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
441 qobject_unref(rsp);
444 static void migrate_set_capability(QTestState *who, const char *capability,
445 bool value)
447 QDict *rsp;
449 rsp = qtest_qmp(who,
450 "{ 'execute': 'migrate-set-capabilities',"
451 "'arguments': { "
452 "'capabilities': [ { "
453 "'capability': %s, 'state': %i } ] } }",
454 capability, value);
455 g_assert(qdict_haskey(rsp, "return"));
456 qobject_unref(rsp);
459 static void migrate_postcopy_start(QTestState *from, QTestState *to)
461 QDict *rsp;
463 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
464 qobject_unref(rsp);
466 if (!got_stop) {
467 qtest_qmp_eventwait(from, "STOP");
470 qtest_qmp_eventwait(to, "RESUME");
473 typedef struct {
475 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
476 * unconditionally, because it means the user would like to be verbose.
478 bool hide_stderr;
479 bool use_shmem;
480 /* only launch the target process */
481 bool only_target;
482 /* Use dirty ring if true; dirty logging otherwise */
483 bool use_dirty_ring;
484 const char *opts_source;
485 const char *opts_target;
486 } MigrateStart;
488 static int test_migrate_start(QTestState **from, QTestState **to,
489 const char *uri, MigrateStart *args)
491 g_autofree gchar *arch_source = NULL;
492 g_autofree gchar *arch_target = NULL;
493 g_autofree gchar *cmd_source = NULL;
494 g_autofree gchar *cmd_target = NULL;
495 const gchar *ignore_stderr;
496 g_autofree char *bootpath = NULL;
497 g_autofree char *shmem_opts = NULL;
498 g_autofree char *shmem_path = NULL;
499 const char *arch = qtest_get_arch();
500 const char *machine_opts = NULL;
501 const char *memory_size;
503 if (args->use_shmem) {
504 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
505 g_test_skip("/dev/shm is not supported");
506 return -1;
510 got_stop = false;
511 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
512 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
513 /* the assembled x86 boot sector should be exactly one sector large */
514 assert(sizeof(x86_bootsect) == 512);
515 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
516 memory_size = "150M";
517 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
518 arch_target = g_strdup(arch_source);
519 start_address = X86_TEST_MEM_START;
520 end_address = X86_TEST_MEM_END;
521 } else if (g_str_equal(arch, "s390x")) {
522 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
523 memory_size = "128M";
524 arch_source = g_strdup_printf("-bios %s", bootpath);
525 arch_target = g_strdup(arch_source);
526 start_address = S390_TEST_MEM_START;
527 end_address = S390_TEST_MEM_END;
528 } else if (strcmp(arch, "ppc64") == 0) {
529 machine_opts = "vsmt=8";
530 memory_size = "256M";
531 start_address = PPC_TEST_MEM_START;
532 end_address = PPC_TEST_MEM_END;
533 arch_source = g_strdup_printf("-nodefaults "
534 "-prom-env 'use-nvramrc?=true' -prom-env "
535 "'nvramrc=hex .\" _\" begin %x %x "
536 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
537 "until'", end_address, start_address);
538 arch_target = g_strdup("");
539 } else if (strcmp(arch, "aarch64") == 0) {
540 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
541 machine_opts = "virt,gic-version=max";
542 memory_size = "150M";
543 arch_source = g_strdup_printf("-cpu max "
544 "-kernel %s",
545 bootpath);
546 arch_target = g_strdup(arch_source);
547 start_address = ARM_TEST_MEM_START;
548 end_address = ARM_TEST_MEM_END;
550 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
551 } else {
552 g_assert_not_reached();
555 if (!getenv("QTEST_LOG") && args->hide_stderr) {
556 ignore_stderr = "2>/dev/null";
557 } else {
558 ignore_stderr = "";
561 if (args->use_shmem) {
562 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
563 shmem_opts = g_strdup_printf(
564 "-object memory-backend-file,id=mem0,size=%s"
565 ",mem-path=%s,share=on -numa node,memdev=mem0",
566 memory_size, shmem_path);
567 } else {
568 shmem_path = NULL;
569 shmem_opts = g_strdup("");
572 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
573 "-name source,debug-threads=on "
574 "-m %s "
575 "-serial file:%s/src_serial "
576 "%s %s %s %s",
577 args->use_dirty_ring ?
578 ",dirty-ring-size=4096" : "",
579 machine_opts ? " -machine " : "",
580 machine_opts ? machine_opts : "",
581 memory_size, tmpfs,
582 arch_source, shmem_opts,
583 args->opts_source ? args->opts_source : "",
584 ignore_stderr);
585 if (!args->only_target) {
586 *from = qtest_init(cmd_source);
589 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
590 "-name target,debug-threads=on "
591 "-m %s "
592 "-serial file:%s/dest_serial "
593 "-incoming %s "
594 "%s %s %s %s",
595 args->use_dirty_ring ?
596 ",dirty-ring-size=4096" : "",
597 machine_opts ? " -machine " : "",
598 machine_opts ? machine_opts : "",
599 memory_size, tmpfs, uri,
600 arch_target, shmem_opts,
601 args->opts_target ? args->opts_target : "",
602 ignore_stderr);
603 *to = qtest_init(cmd_target);
606 * Remove shmem file immediately to avoid memory leak in test failed case.
607 * It's valid becase QEMU has already opened this file
609 if (args->use_shmem) {
610 unlink(shmem_path);
613 return 0;
616 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
618 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
620 qtest_quit(from);
622 if (test_dest) {
623 qtest_memread(to, start_address, &dest_byte_a, 1);
625 /* Destination still running, wait for a byte to change */
626 do {
627 qtest_memread(to, start_address, &dest_byte_b, 1);
628 usleep(1000 * 10);
629 } while (dest_byte_a == dest_byte_b);
631 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
633 /* With it stopped, check nothing changes */
634 qtest_memread(to, start_address, &dest_byte_c, 1);
635 usleep(1000 * 200);
636 qtest_memread(to, start_address, &dest_byte_d, 1);
637 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
639 check_guests_ram(to);
642 qtest_quit(to);
644 cleanup("bootsect");
645 cleanup("migsocket");
646 cleanup("src_serial");
647 cleanup("dest_serial");
650 #ifdef CONFIG_GNUTLS
651 struct TestMigrateTLSPSKData {
652 char *workdir;
653 char *workdiralt;
654 char *pskfile;
655 char *pskfilealt;
658 static void *
659 test_migrate_tls_psk_start_common(QTestState *from,
660 QTestState *to,
661 bool mismatch)
663 struct TestMigrateTLSPSKData *data =
664 g_new0(struct TestMigrateTLSPSKData, 1);
665 QDict *rsp;
667 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
668 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
669 QCRYPTO_TLS_CREDS_PSKFILE);
670 mkdir(data->workdir, 0700);
671 test_tls_psk_init(data->pskfile);
673 if (mismatch) {
674 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
675 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
676 QCRYPTO_TLS_CREDS_PSKFILE);
677 mkdir(data->workdiralt, 0700);
678 test_tls_psk_init_alt(data->pskfilealt);
681 rsp = wait_command(from,
682 "{ 'execute': 'object-add',"
683 " 'arguments': { 'qom-type': 'tls-creds-psk',"
684 " 'id': 'tlscredspsk0',"
685 " 'endpoint': 'client',"
686 " 'dir': %s,"
687 " 'username': 'qemu'} }",
688 data->workdir);
689 qobject_unref(rsp);
691 rsp = wait_command(to,
692 "{ 'execute': 'object-add',"
693 " 'arguments': { 'qom-type': 'tls-creds-psk',"
694 " 'id': 'tlscredspsk0',"
695 " 'endpoint': 'server',"
696 " 'dir': %s } }",
697 mismatch ? data->workdiralt : data->workdir);
698 qobject_unref(rsp);
700 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
701 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
703 return data;
706 static void *
707 test_migrate_tls_psk_start_match(QTestState *from,
708 QTestState *to)
710 return test_migrate_tls_psk_start_common(from, to, false);
713 static void *
714 test_migrate_tls_psk_start_mismatch(QTestState *from,
715 QTestState *to)
717 return test_migrate_tls_psk_start_common(from, to, true);
720 static void
721 test_migrate_tls_psk_finish(QTestState *from,
722 QTestState *to,
723 void *opaque)
725 struct TestMigrateTLSPSKData *data = opaque;
727 test_tls_psk_cleanup(data->pskfile);
728 if (data->pskfilealt) {
729 test_tls_psk_cleanup(data->pskfilealt);
731 rmdir(data->workdir);
732 if (data->workdiralt) {
733 rmdir(data->workdiralt);
736 g_free(data->workdiralt);
737 g_free(data->pskfilealt);
738 g_free(data->workdir);
739 g_free(data->pskfile);
740 g_free(data);
743 #ifdef CONFIG_TASN1
744 typedef struct {
745 char *workdir;
746 char *keyfile;
747 char *cacert;
748 char *servercert;
749 char *serverkey;
750 char *clientcert;
751 char *clientkey;
752 } TestMigrateTLSX509Data;
754 typedef struct {
755 bool verifyclient;
756 bool clientcert;
757 bool hostileclient;
758 bool authzclient;
759 const char *certhostname;
760 const char *certipaddr;
761 } TestMigrateTLSX509;
763 static void *
764 test_migrate_tls_x509_start_common(QTestState *from,
765 QTestState *to,
766 TestMigrateTLSX509 *args)
768 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
769 QDict *rsp;
771 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
772 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
774 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
775 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
776 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
777 if (args->clientcert) {
778 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
779 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
782 mkdir(data->workdir, 0700);
784 test_tls_init(data->keyfile);
785 g_assert(link(data->keyfile, data->serverkey) == 0);
786 if (args->clientcert) {
787 g_assert(link(data->keyfile, data->clientkey) == 0);
790 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
791 if (args->clientcert) {
792 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
793 args->hostileclient ?
794 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
795 QCRYPTO_TLS_TEST_CLIENT_NAME,
796 data->clientcert);
799 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
800 data->servercert,
801 args->certhostname,
802 args->certipaddr);
804 rsp = wait_command(from,
805 "{ 'execute': 'object-add',"
806 " 'arguments': { 'qom-type': 'tls-creds-x509',"
807 " 'id': 'tlscredsx509client0',"
808 " 'endpoint': 'client',"
809 " 'dir': %s,"
810 " 'sanity-check': true,"
811 " 'verify-peer': true} }",
812 data->workdir);
813 qobject_unref(rsp);
814 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
815 if (args->certhostname) {
816 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
819 rsp = wait_command(to,
820 "{ 'execute': 'object-add',"
821 " 'arguments': { 'qom-type': 'tls-creds-x509',"
822 " 'id': 'tlscredsx509server0',"
823 " 'endpoint': 'server',"
824 " 'dir': %s,"
825 " 'sanity-check': true,"
826 " 'verify-peer': %i} }",
827 data->workdir, args->verifyclient);
828 qobject_unref(rsp);
829 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
831 if (args->authzclient) {
832 rsp = wait_command(to,
833 "{ 'execute': 'object-add',"
834 " 'arguments': { 'qom-type': 'authz-simple',"
835 " 'id': 'tlsauthz0',"
836 " 'identity': %s} }",
837 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
838 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
841 return data;
845 * The normal case: match server's cert hostname against
846 * whatever host we were telling QEMU to connect to (if any)
848 static void *
849 test_migrate_tls_x509_start_default_host(QTestState *from,
850 QTestState *to)
852 TestMigrateTLSX509 args = {
853 .verifyclient = true,
854 .clientcert = true,
855 .certipaddr = "127.0.0.1"
857 return test_migrate_tls_x509_start_common(from, to, &args);
861 * The unusual case: the server's cert is different from
862 * the address we're telling QEMU to connect to (if any),
863 * so we must give QEMU an explicit hostname to validate
865 static void *
866 test_migrate_tls_x509_start_override_host(QTestState *from,
867 QTestState *to)
869 TestMigrateTLSX509 args = {
870 .verifyclient = true,
871 .clientcert = true,
872 .certhostname = "qemu.org",
874 return test_migrate_tls_x509_start_common(from, to, &args);
878 * The unusual case: the server's cert is different from
879 * the address we're telling QEMU to connect to, and so we
880 * expect the client to reject the server
882 static void *
883 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
884 QTestState *to)
886 TestMigrateTLSX509 args = {
887 .verifyclient = true,
888 .clientcert = true,
889 .certipaddr = "10.0.0.1",
891 return test_migrate_tls_x509_start_common(from, to, &args);
894 static void *
895 test_migrate_tls_x509_start_friendly_client(QTestState *from,
896 QTestState *to)
898 TestMigrateTLSX509 args = {
899 .verifyclient = true,
900 .clientcert = true,
901 .authzclient = true,
902 .certipaddr = "127.0.0.1",
904 return test_migrate_tls_x509_start_common(from, to, &args);
907 static void *
908 test_migrate_tls_x509_start_hostile_client(QTestState *from,
909 QTestState *to)
911 TestMigrateTLSX509 args = {
912 .verifyclient = true,
913 .clientcert = true,
914 .hostileclient = true,
915 .authzclient = true,
916 .certipaddr = "127.0.0.1",
918 return test_migrate_tls_x509_start_common(from, to, &args);
922 * The case with no client certificate presented,
923 * and no server verification
925 static void *
926 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
927 QTestState *to)
929 TestMigrateTLSX509 args = {
930 .certipaddr = "127.0.0.1",
932 return test_migrate_tls_x509_start_common(from, to, &args);
936 * The case with no client certificate presented,
937 * and server verification rejecting
939 static void *
940 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
941 QTestState *to)
943 TestMigrateTLSX509 args = {
944 .verifyclient = true,
945 .certipaddr = "127.0.0.1",
947 return test_migrate_tls_x509_start_common(from, to, &args);
950 static void
951 test_migrate_tls_x509_finish(QTestState *from,
952 QTestState *to,
953 void *opaque)
955 TestMigrateTLSX509Data *data = opaque;
957 test_tls_cleanup(data->keyfile);
958 unlink(data->cacert);
959 unlink(data->servercert);
960 unlink(data->serverkey);
961 unlink(data->clientcert);
962 unlink(data->clientkey);
963 rmdir(data->workdir);
965 g_free(data->workdir);
966 g_free(data->keyfile);
967 g_free(data);
969 #endif /* CONFIG_TASN1 */
970 #endif /* CONFIG_GNUTLS */
972 static int migrate_postcopy_prepare(QTestState **from_ptr,
973 QTestState **to_ptr,
974 MigrateStart *args)
976 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
977 QTestState *from, *to;
979 if (test_migrate_start(&from, &to, uri, args)) {
980 return -1;
983 migrate_set_capability(from, "postcopy-ram", true);
984 migrate_set_capability(to, "postcopy-ram", true);
985 migrate_set_capability(to, "postcopy-blocktime", true);
987 /* We want to pick a speed slow enough that the test completes
988 * quickly, but that it doesn't complete precopy even on a slow
989 * machine, so also set the downtime.
991 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
992 migrate_set_parameter_int(from, "downtime-limit", 1);
994 /* Wait for the first serial output from the source */
995 wait_for_serial("src_serial");
997 migrate_qmp(from, uri, "{}");
999 wait_for_migration_pass(from);
1001 *from_ptr = from;
1002 *to_ptr = to;
1004 return 0;
1007 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
1009 wait_for_migration_complete(from);
1011 /* Make sure we get at least one "B" on destination */
1012 wait_for_serial("dest_serial");
1014 if (uffd_feature_thread_id) {
1015 read_blocktime(to);
1018 test_migrate_end(from, to, true);
1021 static void test_postcopy(void)
1023 MigrateStart args = {};
1024 QTestState *from, *to;
1026 if (migrate_postcopy_prepare(&from, &to, &args)) {
1027 return;
1029 migrate_postcopy_start(from, to);
1030 migrate_postcopy_complete(from, to);
1033 static void test_postcopy_recovery(void)
1035 MigrateStart args = {
1036 .hide_stderr = true,
1038 QTestState *from, *to;
1039 g_autofree char *uri = NULL;
1041 if (migrate_postcopy_prepare(&from, &to, &args)) {
1042 return;
1045 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1046 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1048 /* Now we start the postcopy */
1049 migrate_postcopy_start(from, to);
1052 * Wait until postcopy is really started; we can only run the
1053 * migrate-pause command during a postcopy
1055 wait_for_migration_status(from, "postcopy-active", NULL);
1058 * Manually stop the postcopy migration. This emulates a network
1059 * failure with the migration socket
1061 migrate_pause(from);
1064 * Wait for destination side to reach postcopy-paused state. The
1065 * migrate-recover command can only succeed if destination machine
1066 * is in the paused state
1068 wait_for_migration_status(to, "postcopy-paused",
1069 (const char * []) { "failed", "active",
1070 "completed", NULL });
1073 * Create a new socket to emulate a new channel that is different
1074 * from the broken migration channel; tell the destination to
1075 * listen to the new port
1077 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1078 migrate_recover(to, uri);
1081 * Try to rebuild the migration channel using the resume flag and
1082 * the newly created channel
1084 wait_for_migration_status(from, "postcopy-paused",
1085 (const char * []) { "failed", "active",
1086 "completed", NULL });
1087 migrate_qmp(from, uri, "{'resume': true}");
1089 /* Restore the postcopy bandwidth to unlimited */
1090 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1092 migrate_postcopy_complete(from, to);
1095 static void test_baddest(void)
1097 MigrateStart args = {
1098 .hide_stderr = true
1100 QTestState *from, *to;
1102 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1103 return;
1105 migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1106 wait_for_migration_fail(from, false);
1107 test_migrate_end(from, to, false);
1111 * A hook that runs after the src and dst QEMUs have been
1112 * created, but before the migration is started. This can
1113 * be used to set migration parameters and capabilities.
1115 * Returns: NULL, or a pointer to opaque state to be
1116 * later passed to the TestMigrateFinishHook
1118 typedef void * (*TestMigrateStartHook)(QTestState *from,
1119 QTestState *to);
1122 * A hook that runs after the migration has finished,
1123 * regardless of whether it succeeded or failed, but
1124 * before QEMU has terminated (unless it self-terminated
1125 * due to migration error)
1127 * @opaque is a pointer to state previously returned
1128 * by the TestMigrateStartHook if any, or NULL.
1130 typedef void (*TestMigrateFinishHook)(QTestState *from,
1131 QTestState *to,
1132 void *opaque);
1134 typedef struct {
1135 /* Optional: fine tune start parameters */
1136 MigrateStart start;
1138 /* Required: the URI for the dst QEMU to listen on */
1139 const char *listen_uri;
1142 * Optional: the URI for the src QEMU to connect to
1143 * If NULL, then it will query the dst QEMU for its actual
1144 * listening address and use that as the connect address.
1145 * This allows for dynamically picking a free TCP port.
1147 const char *connect_uri;
1149 /* Optional: callback to run at start to set migration parameters */
1150 TestMigrateStartHook start_hook;
1151 /* Optional: callback to run at finish to cleanup */
1152 TestMigrateFinishHook finish_hook;
1155 * Optional: normally we expect the migration process to complete.
1157 * There can be a variety of reasons and stages in which failure
1158 * can happen during tests.
1160 * If a failure is expected to happen at time of establishing
1161 * the connection, then MIG_TEST_FAIL will indicate that the dst
1162 * QEMU is expected to stay running and accept future migration
1163 * connections.
1165 * If a failure is expected to happen while processing the
1166 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
1167 * that the dst QEMU is expected to quit with non-zero exit status
1169 enum {
1170 /* This test should succeed, the default */
1171 MIG_TEST_SUCCEED = 0,
1172 /* This test should fail, dest qemu should keep alive */
1173 MIG_TEST_FAIL,
1174 /* This test should fail, dest qemu should fail with abnormal status */
1175 MIG_TEST_FAIL_DEST_QUIT_ERR,
1176 } result;
1178 /* Optional: set number of migration passes to wait for */
1179 unsigned int iterations;
1180 } MigrateCommon;
1182 static void test_precopy_common(MigrateCommon *args)
1184 QTestState *from, *to;
1185 void *data_hook = NULL;
1187 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1188 return;
1192 * We want to pick a speed slow enough that the test completes
1193 * quickly, but that it doesn't complete precopy even on a slow
1194 * machine, so also set the downtime.
1196 /* 1 ms should make it not converge*/
1197 migrate_set_parameter_int(from, "downtime-limit", 1);
1198 /* 1GB/s */
1199 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1201 if (args->start_hook) {
1202 data_hook = args->start_hook(from, to);
1205 /* Wait for the first serial output from the source */
1206 wait_for_serial("src_serial");
1208 if (!args->connect_uri) {
1209 g_autofree char *local_connect_uri =
1210 migrate_get_socket_address(to, "socket-address");
1211 migrate_qmp(from, local_connect_uri, "{}");
1212 } else {
1213 migrate_qmp(from, args->connect_uri, "{}");
1217 if (args->result != MIG_TEST_SUCCEED) {
1218 bool allow_active = args->result == MIG_TEST_FAIL;
1219 wait_for_migration_fail(from, allow_active);
1221 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1222 qtest_set_expected_status(to, 1);
1224 } else {
1225 if (args->iterations) {
1226 while (args->iterations--) {
1227 wait_for_migration_pass(from);
1229 } else {
1230 wait_for_migration_pass(from);
1233 migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1235 /* We do this first, as it has a timeout to stop us
1236 * hanging forever if migration didn't converge */
1237 wait_for_migration_complete(from);
1239 if (!got_stop) {
1240 qtest_qmp_eventwait(from, "STOP");
1243 qtest_qmp_eventwait(to, "RESUME");
1245 wait_for_serial("dest_serial");
1248 if (args->finish_hook) {
1249 args->finish_hook(from, to, data_hook);
1252 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1255 static void test_precopy_unix_plain(void)
1257 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1258 MigrateCommon args = {
1259 .listen_uri = uri,
1260 .connect_uri = uri,
1263 test_precopy_common(&args);
1267 static void test_precopy_unix_dirty_ring(void)
1269 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1270 MigrateCommon args = {
1271 .start = {
1272 .use_dirty_ring = true,
1274 .listen_uri = uri,
1275 .connect_uri = uri,
1278 test_precopy_common(&args);
1281 #ifdef CONFIG_GNUTLS
1282 static void test_precopy_unix_tls_psk(void)
1284 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1285 MigrateCommon args = {
1286 .connect_uri = uri,
1287 .listen_uri = uri,
1288 .start_hook = test_migrate_tls_psk_start_match,
1289 .finish_hook = test_migrate_tls_psk_finish,
1292 test_precopy_common(&args);
1295 #ifdef CONFIG_TASN1
1296 static void test_precopy_unix_tls_x509_default_host(void)
1298 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1299 MigrateCommon args = {
1300 .start = {
1301 .hide_stderr = true,
1303 .connect_uri = uri,
1304 .listen_uri = uri,
1305 .start_hook = test_migrate_tls_x509_start_default_host,
1306 .finish_hook = test_migrate_tls_x509_finish,
1307 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1310 test_precopy_common(&args);
1313 static void test_precopy_unix_tls_x509_override_host(void)
1315 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1316 MigrateCommon args = {
1317 .connect_uri = uri,
1318 .listen_uri = uri,
1319 .start_hook = test_migrate_tls_x509_start_override_host,
1320 .finish_hook = test_migrate_tls_x509_finish,
1323 test_precopy_common(&args);
1325 #endif /* CONFIG_TASN1 */
1326 #endif /* CONFIG_GNUTLS */
1328 #if 0
1329 /* Currently upset on aarch64 TCG */
1330 static void test_ignore_shared(void)
1332 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1333 QTestState *from, *to;
1335 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1336 return;
1339 migrate_set_capability(from, "x-ignore-shared", true);
1340 migrate_set_capability(to, "x-ignore-shared", true);
1342 /* Wait for the first serial output from the source */
1343 wait_for_serial("src_serial");
1345 migrate_qmp(from, uri, "{}");
1347 wait_for_migration_pass(from);
1349 if (!got_stop) {
1350 qtest_qmp_eventwait(from, "STOP");
1353 qtest_qmp_eventwait(to, "RESUME");
1355 wait_for_serial("dest_serial");
1356 wait_for_migration_complete(from);
1358 /* Check whether shared RAM has been really skipped */
1359 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1361 test_migrate_end(from, to, true);
1363 #endif
1365 static void *
1366 test_migrate_xbzrle_start(QTestState *from,
1367 QTestState *to)
1369 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1371 migrate_set_capability(from, "xbzrle", true);
1372 migrate_set_capability(to, "xbzrle", true);
1374 return NULL;
1377 static void test_precopy_unix_xbzrle(void)
1379 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1380 MigrateCommon args = {
1381 .connect_uri = uri,
1382 .listen_uri = uri,
1384 .start_hook = test_migrate_xbzrle_start,
1386 .iterations = 2,
1389 test_precopy_common(&args);
1392 static void test_precopy_tcp_plain(void)
1394 MigrateCommon args = {
1395 .listen_uri = "tcp:127.0.0.1:0",
1398 test_precopy_common(&args);
1401 #ifdef CONFIG_GNUTLS
1402 static void test_precopy_tcp_tls_psk_match(void)
1404 MigrateCommon args = {
1405 .listen_uri = "tcp:127.0.0.1:0",
1406 .start_hook = test_migrate_tls_psk_start_match,
1407 .finish_hook = test_migrate_tls_psk_finish,
1410 test_precopy_common(&args);
1413 static void test_precopy_tcp_tls_psk_mismatch(void)
1415 MigrateCommon args = {
1416 .start = {
1417 .hide_stderr = true,
1419 .listen_uri = "tcp:127.0.0.1:0",
1420 .start_hook = test_migrate_tls_psk_start_mismatch,
1421 .finish_hook = test_migrate_tls_psk_finish,
1422 .result = MIG_TEST_FAIL,
1425 test_precopy_common(&args);
1428 #ifdef CONFIG_TASN1
1429 static void test_precopy_tcp_tls_x509_default_host(void)
1431 MigrateCommon args = {
1432 .listen_uri = "tcp:127.0.0.1:0",
1433 .start_hook = test_migrate_tls_x509_start_default_host,
1434 .finish_hook = test_migrate_tls_x509_finish,
1437 test_precopy_common(&args);
1440 static void test_precopy_tcp_tls_x509_override_host(void)
1442 MigrateCommon args = {
1443 .listen_uri = "tcp:127.0.0.1:0",
1444 .start_hook = test_migrate_tls_x509_start_override_host,
1445 .finish_hook = test_migrate_tls_x509_finish,
1448 test_precopy_common(&args);
1451 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1453 MigrateCommon args = {
1454 .start = {
1455 .hide_stderr = true,
1457 .listen_uri = "tcp:127.0.0.1:0",
1458 .start_hook = test_migrate_tls_x509_start_mismatch_host,
1459 .finish_hook = test_migrate_tls_x509_finish,
1460 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1463 test_precopy_common(&args);
1466 static void test_precopy_tcp_tls_x509_friendly_client(void)
1468 MigrateCommon args = {
1469 .listen_uri = "tcp:127.0.0.1:0",
1470 .start_hook = test_migrate_tls_x509_start_friendly_client,
1471 .finish_hook = test_migrate_tls_x509_finish,
1474 test_precopy_common(&args);
1477 static void test_precopy_tcp_tls_x509_hostile_client(void)
1479 MigrateCommon args = {
1480 .start = {
1481 .hide_stderr = true,
1483 .listen_uri = "tcp:127.0.0.1:0",
1484 .start_hook = test_migrate_tls_x509_start_hostile_client,
1485 .finish_hook = test_migrate_tls_x509_finish,
1486 .result = MIG_TEST_FAIL,
1489 test_precopy_common(&args);
1492 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1494 MigrateCommon args = {
1495 .listen_uri = "tcp:127.0.0.1:0",
1496 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1497 .finish_hook = test_migrate_tls_x509_finish,
1500 test_precopy_common(&args);
1503 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1505 MigrateCommon args = {
1506 .start = {
1507 .hide_stderr = true,
1509 .listen_uri = "tcp:127.0.0.1:0",
1510 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1511 .finish_hook = test_migrate_tls_x509_finish,
1512 .result = MIG_TEST_FAIL,
1515 test_precopy_common(&args);
1517 #endif /* CONFIG_TASN1 */
1518 #endif /* CONFIG_GNUTLS */
1520 static void *test_migrate_fd_start_hook(QTestState *from,
1521 QTestState *to)
1523 QDict *rsp;
1524 int ret;
1525 int pair[2];
1527 /* Create two connected sockets for migration */
1528 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1529 g_assert_cmpint(ret, ==, 0);
1531 /* Send the 1st socket to the target */
1532 rsp = wait_command_fd(to, pair[0],
1533 "{ 'execute': 'getfd',"
1534 " 'arguments': { 'fdname': 'fd-mig' }}");
1535 qobject_unref(rsp);
1536 close(pair[0]);
1538 /* Start incoming migration from the 1st socket */
1539 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1540 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1541 qobject_unref(rsp);
1543 /* Send the 2nd socket to the target */
1544 rsp = wait_command_fd(from, pair[1],
1545 "{ 'execute': 'getfd',"
1546 " 'arguments': { 'fdname': 'fd-mig' }}");
1547 qobject_unref(rsp);
1548 close(pair[1]);
1550 return NULL;
1553 static void test_migrate_fd_finish_hook(QTestState *from,
1554 QTestState *to,
1555 void *opaque)
1557 QDict *rsp;
1558 const char *error_desc;
1560 /* Test closing fds */
1561 /* We assume, that QEMU removes named fd from its list,
1562 * so this should fail */
1563 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1564 " 'arguments': { 'fdname': 'fd-mig' }}");
1565 g_assert_true(qdict_haskey(rsp, "error"));
1566 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1567 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1568 qobject_unref(rsp);
1570 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1571 " 'arguments': { 'fdname': 'fd-mig' }}");
1572 g_assert_true(qdict_haskey(rsp, "error"));
1573 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1574 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1575 qobject_unref(rsp);
1578 static void test_migrate_fd_proto(void)
1580 MigrateCommon args = {
1581 .listen_uri = "defer",
1582 .connect_uri = "fd:fd-mig",
1583 .start_hook = test_migrate_fd_start_hook,
1584 .finish_hook = test_migrate_fd_finish_hook
1586 test_precopy_common(&args);
1589 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1591 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1592 QTestState *from, *to;
1594 if (test_migrate_start(&from, &to, uri, args)) {
1595 return;
1599 * UUID validation is at the begin of migration. So, the main process of
1600 * migration is not interesting for us here. Thus, set huge downtime for
1601 * very fast migration.
1603 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1604 migrate_set_capability(from, "validate-uuid", true);
1606 /* Wait for the first serial output from the source */
1607 wait_for_serial("src_serial");
1609 migrate_qmp(from, uri, "{}");
1611 if (should_fail) {
1612 qtest_set_expected_status(to, 1);
1613 wait_for_migration_fail(from, true);
1614 } else {
1615 wait_for_migration_complete(from);
1618 test_migrate_end(from, to, false);
1621 static void test_validate_uuid(void)
1623 MigrateStart args = {
1624 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1625 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
1628 do_test_validate_uuid(&args, false);
1631 static void test_validate_uuid_error(void)
1633 MigrateStart args = {
1634 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1635 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1636 .hide_stderr = true,
1639 do_test_validate_uuid(&args, true);
1642 static void test_validate_uuid_src_not_set(void)
1644 MigrateStart args = {
1645 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1646 .hide_stderr = true,
1649 do_test_validate_uuid(&args, false);
1652 static void test_validate_uuid_dst_not_set(void)
1654 MigrateStart args = {
1655 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1656 .hide_stderr = true,
1659 do_test_validate_uuid(&args, false);
1662 static void test_migrate_auto_converge(void)
1664 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1665 MigrateStart args = {};
1666 QTestState *from, *to;
1667 int64_t remaining, percentage;
1670 * We want the test to be stable and as fast as possible.
1671 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1672 * so we need to decrease a bandwidth.
1674 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1675 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1676 const int64_t downtime_limit = 250; /* 250ms */
1678 * We migrate through unix-socket (> 500Mb/s).
1679 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1680 * So, we can predict expected_threshold
1682 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1684 if (test_migrate_start(&from, &to, uri, &args)) {
1685 return;
1688 migrate_set_capability(from, "auto-converge", true);
1689 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1690 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1691 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1694 * Set the initial parameters so that the migration could not converge
1695 * without throttling.
1697 migrate_set_parameter_int(from, "downtime-limit", 1);
1698 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1700 /* To check remaining size after precopy */
1701 migrate_set_capability(from, "pause-before-switchover", true);
1703 /* Wait for the first serial output from the source */
1704 wait_for_serial("src_serial");
1706 migrate_qmp(from, uri, "{}");
1708 /* Wait for throttling begins */
1709 percentage = 0;
1710 while (percentage == 0) {
1711 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1712 usleep(100);
1713 g_assert_false(got_stop);
1715 /* The first percentage of throttling should be equal to init_pct */
1716 g_assert_cmpint(percentage, ==, init_pct);
1717 /* Now, when we tested that throttling works, let it converge */
1718 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1719 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1722 * Wait for pre-switchover status to check last throttle percentage
1723 * and remaining. These values will be zeroed later
1725 wait_for_migration_status(from, "pre-switchover", NULL);
1727 /* The final percentage of throttling shouldn't be greater than max_pct */
1728 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1729 g_assert_cmpint(percentage, <=, max_pct);
1731 remaining = read_ram_property_int(from, "remaining");
1732 g_assert_cmpint(remaining, <,
1733 (expected_threshold + expected_threshold / 100));
1735 migrate_continue(from, "pre-switchover");
1737 qtest_qmp_eventwait(to, "RESUME");
1739 wait_for_serial("dest_serial");
1740 wait_for_migration_complete(from);
1743 test_migrate_end(from, to, true);
1746 static void *
1747 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
1748 QTestState *to,
1749 const char *method)
1751 QDict *rsp;
1753 migrate_set_parameter_int(from, "multifd-channels", 16);
1754 migrate_set_parameter_int(to, "multifd-channels", 16);
1756 migrate_set_parameter_str(from, "multifd-compression", method);
1757 migrate_set_parameter_str(to, "multifd-compression", method);
1759 migrate_set_capability(from, "multifd", true);
1760 migrate_set_capability(to, "multifd", true);
1762 /* Start incoming migration from the 1st socket */
1763 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1764 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1765 qobject_unref(rsp);
1767 return NULL;
1770 static void *
1771 test_migrate_precopy_tcp_multifd_start(QTestState *from,
1772 QTestState *to)
1774 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1777 static void *
1778 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
1779 QTestState *to)
1781 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
1784 #ifdef CONFIG_ZSTD
1785 static void *
1786 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
1787 QTestState *to)
1789 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
1791 #endif /* CONFIG_ZSTD */
1793 static void test_multifd_tcp_none(void)
1795 MigrateCommon args = {
1796 .listen_uri = "defer",
1797 .start_hook = test_migrate_precopy_tcp_multifd_start,
1799 test_precopy_common(&args);
1802 static void test_multifd_tcp_zlib(void)
1804 MigrateCommon args = {
1805 .listen_uri = "defer",
1806 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
1808 test_precopy_common(&args);
1811 #ifdef CONFIG_ZSTD
1812 static void test_multifd_tcp_zstd(void)
1814 MigrateCommon args = {
1815 .listen_uri = "defer",
1816 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
1818 test_precopy_common(&args);
1820 #endif
1822 #ifdef CONFIG_GNUTLS
1823 static void *
1824 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
1825 QTestState *to)
1827 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1828 return test_migrate_tls_psk_start_match(from, to);
1831 static void *
1832 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
1833 QTestState *to)
1835 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1836 return test_migrate_tls_psk_start_mismatch(from, to);
1839 #ifdef CONFIG_TASN1
1840 static void *
1841 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
1842 QTestState *to)
1844 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1845 return test_migrate_tls_x509_start_default_host(from, to);
1848 static void *
1849 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
1850 QTestState *to)
1852 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1853 return test_migrate_tls_x509_start_override_host(from, to);
1856 static void *
1857 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
1858 QTestState *to)
1860 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1861 return test_migrate_tls_x509_start_mismatch_host(from, to);
1864 static void *
1865 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
1866 QTestState *to)
1868 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1869 return test_migrate_tls_x509_start_allow_anon_client(from, to);
1872 static void *
1873 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
1874 QTestState *to)
1876 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1877 return test_migrate_tls_x509_start_reject_anon_client(from, to);
1879 #endif /* CONFIG_TASN1 */
1881 static void test_multifd_tcp_tls_psk_match(void)
1883 MigrateCommon args = {
1884 .listen_uri = "defer",
1885 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
1886 .finish_hook = test_migrate_tls_psk_finish,
1888 test_precopy_common(&args);
1891 static void test_multifd_tcp_tls_psk_mismatch(void)
1893 MigrateCommon args = {
1894 .start = {
1895 .hide_stderr = true,
1897 .listen_uri = "defer",
1898 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
1899 .finish_hook = test_migrate_tls_psk_finish,
1900 .result = MIG_TEST_FAIL,
1902 test_precopy_common(&args);
1905 #ifdef CONFIG_TASN1
1906 static void test_multifd_tcp_tls_x509_default_host(void)
1908 MigrateCommon args = {
1909 .listen_uri = "defer",
1910 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
1911 .finish_hook = test_migrate_tls_x509_finish,
1913 test_precopy_common(&args);
1916 static void test_multifd_tcp_tls_x509_override_host(void)
1918 MigrateCommon args = {
1919 .listen_uri = "defer",
1920 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
1921 .finish_hook = test_migrate_tls_x509_finish,
1923 test_precopy_common(&args);
1926 static void test_multifd_tcp_tls_x509_mismatch_host(void)
1929 * This has different behaviour to the non-multifd case.
1931 * In non-multifd case when client aborts due to mismatched
1932 * cert host, the server has already started trying to load
1933 * migration state, and so it exits with I/O failure.
1935 * In multifd case when client aborts due to mismatched
1936 * cert host, the server is still waiting for the other
1937 * multifd connections to arrive so hasn't started trying
1938 * to load migration state, and thus just aborts the migration
1939 * without exiting.
1941 MigrateCommon args = {
1942 .start = {
1943 .hide_stderr = true,
1945 .listen_uri = "defer",
1946 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
1947 .finish_hook = test_migrate_tls_x509_finish,
1948 .result = MIG_TEST_FAIL,
1950 test_precopy_common(&args);
1953 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
1955 MigrateCommon args = {
1956 .listen_uri = "defer",
1957 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
1958 .finish_hook = test_migrate_tls_x509_finish,
1960 test_precopy_common(&args);
1963 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
1965 MigrateCommon args = {
1966 .start = {
1967 .hide_stderr = true,
1969 .listen_uri = "defer",
1970 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
1971 .finish_hook = test_migrate_tls_x509_finish,
1972 .result = MIG_TEST_FAIL,
1974 test_precopy_common(&args);
1976 #endif /* CONFIG_TASN1 */
1977 #endif /* CONFIG_GNUTLS */
1980 * This test does:
1981 * source target
1982 * migrate_incoming
1983 * migrate
1984 * migrate_cancel
1985 * launch another target
1986 * migrate
1988 * And see that it works
1990 static void test_multifd_tcp_cancel(void)
1992 MigrateStart args = {
1993 .hide_stderr = true,
1995 QTestState *from, *to, *to2;
1996 QDict *rsp;
1997 g_autofree char *uri = NULL;
1999 if (test_migrate_start(&from, &to, "defer", &args)) {
2000 return;
2004 * We want to pick a speed slow enough that the test completes
2005 * quickly, but that it doesn't complete precopy even on a slow
2006 * machine, so also set the downtime.
2008 /* 1 ms should make it not converge*/
2009 migrate_set_parameter_int(from, "downtime-limit", 1);
2010 /* 300MB/s */
2011 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
2013 migrate_set_parameter_int(from, "multifd-channels", 16);
2014 migrate_set_parameter_int(to, "multifd-channels", 16);
2016 migrate_set_capability(from, "multifd", true);
2017 migrate_set_capability(to, "multifd", true);
2019 /* Start incoming migration from the 1st socket */
2020 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
2021 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2022 qobject_unref(rsp);
2024 /* Wait for the first serial output from the source */
2025 wait_for_serial("src_serial");
2027 uri = migrate_get_socket_address(to, "socket-address");
2029 migrate_qmp(from, uri, "{}");
2031 wait_for_migration_pass(from);
2033 migrate_cancel(from);
2035 args = (MigrateStart){
2036 .only_target = true,
2039 if (test_migrate_start(&from, &to2, "defer", &args)) {
2040 return;
2043 migrate_set_parameter_int(to2, "multifd-channels", 16);
2045 migrate_set_capability(to2, "multifd", true);
2047 /* Start incoming migration from the 1st socket */
2048 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
2049 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2050 qobject_unref(rsp);
2052 g_free(uri);
2053 uri = migrate_get_socket_address(to2, "socket-address");
2055 wait_for_migration_status(from, "cancelled", NULL);
2057 /* 300ms it should converge */
2058 migrate_set_parameter_int(from, "downtime-limit", 300);
2059 /* 1GB/s */
2060 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
2062 migrate_qmp(from, uri, "{}");
2064 wait_for_migration_pass(from);
2066 if (!got_stop) {
2067 qtest_qmp_eventwait(from, "STOP");
2069 qtest_qmp_eventwait(to2, "RESUME");
2071 wait_for_serial("dest_serial");
2072 wait_for_migration_complete(from);
2073 test_migrate_end(from, to2, true);
2076 static bool kvm_dirty_ring_supported(void)
2078 #if defined(__linux__) && defined(HOST_X86_64)
2079 int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2081 if (kvm_fd < 0) {
2082 return false;
2085 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2086 close(kvm_fd);
2088 /* We test with 4096 slots */
2089 if (ret < 4096) {
2090 return false;
2093 return true;
2094 #else
2095 return false;
2096 #endif
2099 int main(int argc, char **argv)
2101 char template[] = "/tmp/migration-test-XXXXXX";
2102 const bool has_kvm = qtest_has_accel("kvm");
2103 int ret;
2105 g_test_init(&argc, &argv, NULL);
2107 if (!ufd_version_check()) {
2108 return g_test_run();
2112 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2113 * is touchy due to race conditions on dirty bits (especially on PPC for
2114 * some reason)
2116 if (g_str_equal(qtest_get_arch(), "ppc64") &&
2117 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2118 g_test_message("Skipping test: kvm_hv not available");
2119 return g_test_run();
2123 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2124 * there until the problems are resolved
2126 if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm) {
2127 g_test_message("Skipping test: s390x host with KVM is required");
2128 return g_test_run();
2131 tmpfs = mkdtemp(template);
2132 if (!tmpfs) {
2133 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
2135 g_assert(tmpfs);
2137 module_call_init(MODULE_INIT_QOM);
2139 qtest_add_func("/migration/postcopy/unix", test_postcopy);
2140 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
2141 qtest_add_func("/migration/bad_dest", test_baddest);
2142 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2143 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2144 #ifdef CONFIG_GNUTLS
2145 qtest_add_func("/migration/precopy/unix/tls/psk",
2146 test_precopy_unix_tls_psk);
2147 #ifdef CONFIG_TASN1
2148 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2149 test_precopy_unix_tls_x509_default_host);
2150 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2151 test_precopy_unix_tls_x509_override_host);
2152 #endif /* CONFIG_TASN1 */
2153 #endif /* CONFIG_GNUTLS */
2155 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2156 #ifdef CONFIG_GNUTLS
2157 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2158 test_precopy_tcp_tls_psk_match);
2159 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2160 test_precopy_tcp_tls_psk_mismatch);
2161 #ifdef CONFIG_TASN1
2162 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2163 test_precopy_tcp_tls_x509_default_host);
2164 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2165 test_precopy_tcp_tls_x509_override_host);
2166 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2167 test_precopy_tcp_tls_x509_mismatch_host);
2168 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2169 test_precopy_tcp_tls_x509_friendly_client);
2170 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2171 test_precopy_tcp_tls_x509_hostile_client);
2172 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2173 test_precopy_tcp_tls_x509_allow_anon_client);
2174 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2175 test_precopy_tcp_tls_x509_reject_anon_client);
2176 #endif /* CONFIG_TASN1 */
2177 #endif /* CONFIG_GNUTLS */
2179 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2180 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2181 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2182 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2183 qtest_add_func("/migration/validate_uuid_src_not_set",
2184 test_validate_uuid_src_not_set);
2185 qtest_add_func("/migration/validate_uuid_dst_not_set",
2186 test_validate_uuid_dst_not_set);
2188 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2189 qtest_add_func("/migration/multifd/tcp/plain/none",
2190 test_multifd_tcp_none);
2191 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2192 test_multifd_tcp_cancel);
2193 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2194 test_multifd_tcp_zlib);
2195 #ifdef CONFIG_ZSTD
2196 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2197 test_multifd_tcp_zstd);
2198 #endif
2199 #ifdef CONFIG_GNUTLS
2200 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2201 test_multifd_tcp_tls_psk_match);
2202 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2203 test_multifd_tcp_tls_psk_mismatch);
2204 #ifdef CONFIG_TASN1
2205 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2206 test_multifd_tcp_tls_x509_default_host);
2207 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2208 test_multifd_tcp_tls_x509_override_host);
2209 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2210 test_multifd_tcp_tls_x509_mismatch_host);
2211 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2212 test_multifd_tcp_tls_x509_allow_anon_client);
2213 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2214 test_multifd_tcp_tls_x509_reject_anon_client);
2215 #endif /* CONFIG_TASN1 */
2216 #endif /* CONFIG_GNUTLS */
2218 if (kvm_dirty_ring_supported()) {
2219 qtest_add_func("/migration/dirty_ring",
2220 test_precopy_unix_dirty_ring);
2223 ret = g_test_run();
2225 g_assert_cmpint(ret, ==, 0);
2227 ret = rmdir(tmpfs);
2228 if (ret != 0) {
2229 g_test_message("unable to rmdir: path (%s): %s",
2230 tmpfs, strerror(errno));
2233 return ret;