ppc/40x: Allocate IRQ lines with qdev_init_gpio_in()
[qemu.git] / tests / qtest / migration-test.c
blob9e64125f027f817a0ea4b487a7666a9f314cf243
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 #if defined(__linux__)
50 #include <sys/syscall.h>
51 #include <sys/vfs.h>
52 #endif
54 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
55 #include <sys/eventfd.h>
56 #include <sys/ioctl.h>
57 #include <linux/userfaultfd.h>
59 static bool ufd_version_check(void)
61 struct uffdio_api api_struct;
62 uint64_t ioctl_mask;
64 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
66 if (ufd == -1) {
67 g_test_message("Skipping test: userfaultfd not available");
68 return false;
71 api_struct.api = UFFD_API;
72 api_struct.features = 0;
73 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
74 g_test_message("Skipping test: UFFDIO_API failed");
75 return false;
77 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
79 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
80 (__u64)1 << _UFFDIO_UNREGISTER;
81 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
82 g_test_message("Skipping test: Missing userfault feature");
83 return false;
86 return true;
89 #else
90 static bool ufd_version_check(void)
92 g_test_message("Skipping test: Userfault not available (builtdtime)");
93 return false;
96 #endif
98 static const char *tmpfs;
100 /* The boot file modifies memory area in [start_address, end_address)
101 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
103 #include "tests/migration/i386/a-b-bootblock.h"
104 #include "tests/migration/aarch64/a-b-kernel.h"
105 #include "tests/migration/s390x/a-b-bios.h"
107 static void init_bootfile(const char *bootpath, void *content, size_t len)
109 FILE *bootfile = fopen(bootpath, "wb");
111 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
112 fclose(bootfile);
116 * Wait for some output in the serial output file,
117 * we get an 'A' followed by an endless string of 'B's
118 * but on the destination we won't have the A.
120 static void wait_for_serial(const char *side)
122 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
123 FILE *serialfile = fopen(serialpath, "r");
124 const char *arch = qtest_get_arch();
125 int started = (strcmp(side, "src_serial") == 0 &&
126 strcmp(arch, "ppc64") == 0) ? 0 : 1;
128 do {
129 int readvalue = fgetc(serialfile);
131 if (!started) {
132 /* SLOF prints its banner before starting test,
133 * to ignore it, mark the start of the test with '_',
134 * ignore all characters until this marker
136 switch (readvalue) {
137 case '_':
138 started = 1;
139 break;
140 case EOF:
141 fseek(serialfile, 0, SEEK_SET);
142 usleep(1000);
143 break;
145 continue;
147 switch (readvalue) {
148 case 'A':
149 /* Fine */
150 break;
152 case 'B':
153 /* It's alive! */
154 fclose(serialfile);
155 return;
157 case EOF:
158 started = (strcmp(side, "src_serial") == 0 &&
159 strcmp(arch, "ppc64") == 0) ? 0 : 1;
160 fseek(serialfile, 0, SEEK_SET);
161 usleep(1000);
162 break;
164 default:
165 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
166 g_assert_not_reached();
168 } while (true);
172 * It's tricky to use qemu's migration event capability with qtest,
173 * events suddenly appearing confuse the qmp()/hmp() responses.
176 static int64_t read_ram_property_int(QTestState *who, const char *property)
178 QDict *rsp_return, *rsp_ram;
179 int64_t result;
181 rsp_return = migrate_query_not_failed(who);
182 if (!qdict_haskey(rsp_return, "ram")) {
183 /* Still in setup */
184 result = 0;
185 } else {
186 rsp_ram = qdict_get_qdict(rsp_return, "ram");
187 result = qdict_get_try_int(rsp_ram, property, 0);
189 qobject_unref(rsp_return);
190 return result;
193 static int64_t read_migrate_property_int(QTestState *who, const char *property)
195 QDict *rsp_return;
196 int64_t result;
198 rsp_return = migrate_query_not_failed(who);
199 result = qdict_get_try_int(rsp_return, property, 0);
200 qobject_unref(rsp_return);
201 return result;
204 static uint64_t get_migration_pass(QTestState *who)
206 return read_ram_property_int(who, "dirty-sync-count");
209 static void read_blocktime(QTestState *who)
211 QDict *rsp_return;
213 rsp_return = migrate_query_not_failed(who);
214 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
215 qobject_unref(rsp_return);
218 static void wait_for_migration_pass(QTestState *who)
220 uint64_t initial_pass = get_migration_pass(who);
221 uint64_t pass;
223 /* Wait for the 1st sync */
224 while (!got_stop && !initial_pass) {
225 usleep(1000);
226 initial_pass = get_migration_pass(who);
229 do {
230 usleep(1000);
231 pass = get_migration_pass(who);
232 } while (pass == initial_pass && !got_stop);
235 static void check_guests_ram(QTestState *who)
237 /* Our ASM test will have been incrementing one byte from each page from
238 * start_address to < end_address in order. This gives us a constraint
239 * that any page's byte should be equal or less than the previous pages
240 * byte (mod 256); and they should all be equal except for one transition
241 * at the point where we meet the incrementer. (We're running this with
242 * the guest stopped).
244 unsigned address;
245 uint8_t first_byte;
246 uint8_t last_byte;
247 bool hit_edge = false;
248 int bad = 0;
250 qtest_memread(who, start_address, &first_byte, 1);
251 last_byte = first_byte;
253 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
254 address += TEST_MEM_PAGE_SIZE)
256 uint8_t b;
257 qtest_memread(who, address, &b, 1);
258 if (b != last_byte) {
259 if (((b + 1) % 256) == last_byte && !hit_edge) {
260 /* This is OK, the guest stopped at the point of
261 * incrementing the previous page but didn't get
262 * to us yet.
264 hit_edge = true;
265 last_byte = b;
266 } else {
267 bad++;
268 if (bad <= 10) {
269 fprintf(stderr, "Memory content inconsistency at %x"
270 " first_byte = %x last_byte = %x current = %x"
271 " hit_edge = %x\n",
272 address, first_byte, last_byte, b, hit_edge);
277 if (bad >= 10) {
278 fprintf(stderr, "and in another %d pages", bad - 10);
280 g_assert(bad == 0);
283 static void cleanup(const char *filename)
285 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
287 unlink(path);
290 static char *SocketAddress_to_str(SocketAddress *addr)
292 switch (addr->type) {
293 case SOCKET_ADDRESS_TYPE_INET:
294 return g_strdup_printf("tcp:%s:%s",
295 addr->u.inet.host,
296 addr->u.inet.port);
297 case SOCKET_ADDRESS_TYPE_UNIX:
298 return g_strdup_printf("unix:%s",
299 addr->u.q_unix.path);
300 case SOCKET_ADDRESS_TYPE_FD:
301 return g_strdup_printf("fd:%s", addr->u.fd.str);
302 case SOCKET_ADDRESS_TYPE_VSOCK:
303 return g_strdup_printf("tcp:%s:%s",
304 addr->u.vsock.cid,
305 addr->u.vsock.port);
306 default:
307 return g_strdup("unknown address type");
311 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
313 QDict *rsp;
314 char *result;
315 SocketAddressList *addrs;
316 Visitor *iv = NULL;
317 QObject *object;
319 rsp = migrate_query(who);
320 object = qdict_get(rsp, parameter);
322 iv = qobject_input_visitor_new(object);
323 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
324 visit_free(iv);
326 /* we are only using a single address */
327 result = SocketAddress_to_str(addrs->value);
329 qapi_free_SocketAddressList(addrs);
330 qobject_unref(rsp);
331 return result;
334 static long long migrate_get_parameter_int(QTestState *who,
335 const char *parameter)
337 QDict *rsp;
338 long long result;
340 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
341 result = qdict_get_int(rsp, parameter);
342 qobject_unref(rsp);
343 return result;
346 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
347 long long value)
349 long long result;
351 result = migrate_get_parameter_int(who, parameter);
352 g_assert_cmpint(result, ==, value);
355 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
356 long long value)
358 QDict *rsp;
360 rsp = qtest_qmp(who,
361 "{ 'execute': 'migrate-set-parameters',"
362 "'arguments': { %s: %lld } }",
363 parameter, value);
364 g_assert(qdict_haskey(rsp, "return"));
365 qobject_unref(rsp);
366 migrate_check_parameter_int(who, parameter, value);
369 static char *migrate_get_parameter_str(QTestState *who,
370 const char *parameter)
372 QDict *rsp;
373 char *result;
375 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
376 result = g_strdup(qdict_get_str(rsp, parameter));
377 qobject_unref(rsp);
378 return result;
381 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
382 const char *value)
384 g_autofree char *result = migrate_get_parameter_str(who, parameter);
385 g_assert_cmpstr(result, ==, value);
388 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
389 const char *value)
391 QDict *rsp;
393 rsp = qtest_qmp(who,
394 "{ 'execute': 'migrate-set-parameters',"
395 "'arguments': { %s: %s } }",
396 parameter, value);
397 g_assert(qdict_haskey(rsp, "return"));
398 qobject_unref(rsp);
399 migrate_check_parameter_str(who, parameter, value);
402 static void migrate_ensure_non_converge(QTestState *who)
404 /* Can't converge with 1ms downtime + 30 mbs bandwidth limit */
405 migrate_set_parameter_int(who, "max-bandwidth", 30 * 1000 * 1000);
406 migrate_set_parameter_int(who, "downtime-limit", 1);
409 static void migrate_ensure_converge(QTestState *who)
411 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
412 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
413 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
416 static void migrate_pause(QTestState *who)
418 QDict *rsp;
420 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
421 qobject_unref(rsp);
424 static void migrate_continue(QTestState *who, const char *state)
426 QDict *rsp;
428 rsp = wait_command(who,
429 "{ 'execute': 'migrate-continue',"
430 " 'arguments': { 'state': %s } }",
431 state);
432 qobject_unref(rsp);
435 static void migrate_recover(QTestState *who, const char *uri)
437 QDict *rsp;
439 rsp = wait_command(who,
440 "{ 'execute': 'migrate-recover', "
441 " 'id': 'recover-cmd', "
442 " 'arguments': { 'uri': %s } }",
443 uri);
444 qobject_unref(rsp);
447 static void migrate_cancel(QTestState *who)
449 QDict *rsp;
451 rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
452 qobject_unref(rsp);
455 static void migrate_set_capability(QTestState *who, const char *capability,
456 bool value)
458 QDict *rsp;
460 rsp = qtest_qmp(who,
461 "{ 'execute': 'migrate-set-capabilities',"
462 "'arguments': { "
463 "'capabilities': [ { "
464 "'capability': %s, 'state': %i } ] } }",
465 capability, value);
466 g_assert(qdict_haskey(rsp, "return"));
467 qobject_unref(rsp);
470 static void migrate_postcopy_start(QTestState *from, QTestState *to)
472 QDict *rsp;
474 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
475 qobject_unref(rsp);
477 if (!got_stop) {
478 qtest_qmp_eventwait(from, "STOP");
481 qtest_qmp_eventwait(to, "RESUME");
484 typedef struct {
486 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
487 * unconditionally, because it means the user would like to be verbose.
489 bool hide_stderr;
490 bool use_shmem;
491 /* only launch the target process */
492 bool only_target;
493 /* Use dirty ring if true; dirty logging otherwise */
494 bool use_dirty_ring;
495 const char *opts_source;
496 const char *opts_target;
497 } MigrateStart;
499 static int test_migrate_start(QTestState **from, QTestState **to,
500 const char *uri, MigrateStart *args)
502 g_autofree gchar *arch_source = NULL;
503 g_autofree gchar *arch_target = NULL;
504 g_autofree gchar *cmd_source = NULL;
505 g_autofree gchar *cmd_target = NULL;
506 const gchar *ignore_stderr;
507 g_autofree char *bootpath = NULL;
508 g_autofree char *shmem_opts = NULL;
509 g_autofree char *shmem_path = NULL;
510 const char *arch = qtest_get_arch();
511 const char *machine_opts = NULL;
512 const char *memory_size;
514 if (args->use_shmem) {
515 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
516 g_test_skip("/dev/shm is not supported");
517 return -1;
521 got_stop = false;
522 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
523 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
524 /* the assembled x86 boot sector should be exactly one sector large */
525 assert(sizeof(x86_bootsect) == 512);
526 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
527 memory_size = "150M";
528 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
529 arch_target = g_strdup(arch_source);
530 start_address = X86_TEST_MEM_START;
531 end_address = X86_TEST_MEM_END;
532 } else if (g_str_equal(arch, "s390x")) {
533 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
534 memory_size = "128M";
535 arch_source = g_strdup_printf("-bios %s", bootpath);
536 arch_target = g_strdup(arch_source);
537 start_address = S390_TEST_MEM_START;
538 end_address = S390_TEST_MEM_END;
539 } else if (strcmp(arch, "ppc64") == 0) {
540 machine_opts = "vsmt=8";
541 memory_size = "256M";
542 start_address = PPC_TEST_MEM_START;
543 end_address = PPC_TEST_MEM_END;
544 arch_source = g_strdup_printf("-nodefaults "
545 "-prom-env 'use-nvramrc?=true' -prom-env "
546 "'nvramrc=hex .\" _\" begin %x %x "
547 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
548 "until'", end_address, start_address);
549 arch_target = g_strdup("");
550 } else if (strcmp(arch, "aarch64") == 0) {
551 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
552 machine_opts = "virt,gic-version=max";
553 memory_size = "150M";
554 arch_source = g_strdup_printf("-cpu max "
555 "-kernel %s",
556 bootpath);
557 arch_target = g_strdup(arch_source);
558 start_address = ARM_TEST_MEM_START;
559 end_address = ARM_TEST_MEM_END;
561 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
562 } else {
563 g_assert_not_reached();
566 if (!getenv("QTEST_LOG") && args->hide_stderr) {
567 ignore_stderr = "2>/dev/null";
568 } else {
569 ignore_stderr = "";
572 if (args->use_shmem) {
573 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
574 shmem_opts = g_strdup_printf(
575 "-object memory-backend-file,id=mem0,size=%s"
576 ",mem-path=%s,share=on -numa node,memdev=mem0",
577 memory_size, shmem_path);
578 } else {
579 shmem_path = NULL;
580 shmem_opts = g_strdup("");
583 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
584 "-name source,debug-threads=on "
585 "-m %s "
586 "-serial file:%s/src_serial "
587 "%s %s %s %s",
588 args->use_dirty_ring ?
589 ",dirty-ring-size=4096" : "",
590 machine_opts ? " -machine " : "",
591 machine_opts ? machine_opts : "",
592 memory_size, tmpfs,
593 arch_source, shmem_opts,
594 args->opts_source ? args->opts_source : "",
595 ignore_stderr);
596 if (!args->only_target) {
597 *from = qtest_init(cmd_source);
600 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
601 "-name target,debug-threads=on "
602 "-m %s "
603 "-serial file:%s/dest_serial "
604 "-incoming %s "
605 "%s %s %s %s",
606 args->use_dirty_ring ?
607 ",dirty-ring-size=4096" : "",
608 machine_opts ? " -machine " : "",
609 machine_opts ? machine_opts : "",
610 memory_size, tmpfs, uri,
611 arch_target, shmem_opts,
612 args->opts_target ? args->opts_target : "",
613 ignore_stderr);
614 *to = qtest_init(cmd_target);
617 * Remove shmem file immediately to avoid memory leak in test failed case.
618 * It's valid becase QEMU has already opened this file
620 if (args->use_shmem) {
621 unlink(shmem_path);
624 return 0;
627 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
629 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
631 qtest_quit(from);
633 if (test_dest) {
634 qtest_memread(to, start_address, &dest_byte_a, 1);
636 /* Destination still running, wait for a byte to change */
637 do {
638 qtest_memread(to, start_address, &dest_byte_b, 1);
639 usleep(1000 * 10);
640 } while (dest_byte_a == dest_byte_b);
642 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
644 /* With it stopped, check nothing changes */
645 qtest_memread(to, start_address, &dest_byte_c, 1);
646 usleep(1000 * 200);
647 qtest_memread(to, start_address, &dest_byte_d, 1);
648 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
650 check_guests_ram(to);
653 qtest_quit(to);
655 cleanup("bootsect");
656 cleanup("migsocket");
657 cleanup("src_serial");
658 cleanup("dest_serial");
661 #ifdef CONFIG_GNUTLS
662 struct TestMigrateTLSPSKData {
663 char *workdir;
664 char *workdiralt;
665 char *pskfile;
666 char *pskfilealt;
669 static void *
670 test_migrate_tls_psk_start_common(QTestState *from,
671 QTestState *to,
672 bool mismatch)
674 struct TestMigrateTLSPSKData *data =
675 g_new0(struct TestMigrateTLSPSKData, 1);
676 QDict *rsp;
678 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
679 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
680 QCRYPTO_TLS_CREDS_PSKFILE);
681 mkdir(data->workdir, 0700);
682 test_tls_psk_init(data->pskfile);
684 if (mismatch) {
685 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
686 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
687 QCRYPTO_TLS_CREDS_PSKFILE);
688 mkdir(data->workdiralt, 0700);
689 test_tls_psk_init_alt(data->pskfilealt);
692 rsp = wait_command(from,
693 "{ 'execute': 'object-add',"
694 " 'arguments': { 'qom-type': 'tls-creds-psk',"
695 " 'id': 'tlscredspsk0',"
696 " 'endpoint': 'client',"
697 " 'dir': %s,"
698 " 'username': 'qemu'} }",
699 data->workdir);
700 qobject_unref(rsp);
702 rsp = wait_command(to,
703 "{ 'execute': 'object-add',"
704 " 'arguments': { 'qom-type': 'tls-creds-psk',"
705 " 'id': 'tlscredspsk0',"
706 " 'endpoint': 'server',"
707 " 'dir': %s } }",
708 mismatch ? data->workdiralt : data->workdir);
709 qobject_unref(rsp);
711 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
712 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
714 return data;
717 static void *
718 test_migrate_tls_psk_start_match(QTestState *from,
719 QTestState *to)
721 return test_migrate_tls_psk_start_common(from, to, false);
724 static void *
725 test_migrate_tls_psk_start_mismatch(QTestState *from,
726 QTestState *to)
728 return test_migrate_tls_psk_start_common(from, to, true);
731 static void
732 test_migrate_tls_psk_finish(QTestState *from,
733 QTestState *to,
734 void *opaque)
736 struct TestMigrateTLSPSKData *data = opaque;
738 test_tls_psk_cleanup(data->pskfile);
739 if (data->pskfilealt) {
740 test_tls_psk_cleanup(data->pskfilealt);
742 rmdir(data->workdir);
743 if (data->workdiralt) {
744 rmdir(data->workdiralt);
747 g_free(data->workdiralt);
748 g_free(data->pskfilealt);
749 g_free(data->workdir);
750 g_free(data->pskfile);
751 g_free(data);
754 #ifdef CONFIG_TASN1
755 typedef struct {
756 char *workdir;
757 char *keyfile;
758 char *cacert;
759 char *servercert;
760 char *serverkey;
761 char *clientcert;
762 char *clientkey;
763 } TestMigrateTLSX509Data;
765 typedef struct {
766 bool verifyclient;
767 bool clientcert;
768 bool hostileclient;
769 bool authzclient;
770 const char *certhostname;
771 const char *certipaddr;
772 } TestMigrateTLSX509;
774 static void *
775 test_migrate_tls_x509_start_common(QTestState *from,
776 QTestState *to,
777 TestMigrateTLSX509 *args)
779 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
780 QDict *rsp;
782 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
783 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
785 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
786 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
787 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
788 if (args->clientcert) {
789 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
790 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
793 mkdir(data->workdir, 0700);
795 test_tls_init(data->keyfile);
796 g_assert(link(data->keyfile, data->serverkey) == 0);
797 if (args->clientcert) {
798 g_assert(link(data->keyfile, data->clientkey) == 0);
801 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
802 if (args->clientcert) {
803 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
804 args->hostileclient ?
805 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
806 QCRYPTO_TLS_TEST_CLIENT_NAME,
807 data->clientcert);
810 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
811 data->servercert,
812 args->certhostname,
813 args->certipaddr);
815 rsp = wait_command(from,
816 "{ 'execute': 'object-add',"
817 " 'arguments': { 'qom-type': 'tls-creds-x509',"
818 " 'id': 'tlscredsx509client0',"
819 " 'endpoint': 'client',"
820 " 'dir': %s,"
821 " 'sanity-check': true,"
822 " 'verify-peer': true} }",
823 data->workdir);
824 qobject_unref(rsp);
825 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
826 if (args->certhostname) {
827 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
830 rsp = wait_command(to,
831 "{ 'execute': 'object-add',"
832 " 'arguments': { 'qom-type': 'tls-creds-x509',"
833 " 'id': 'tlscredsx509server0',"
834 " 'endpoint': 'server',"
835 " 'dir': %s,"
836 " 'sanity-check': true,"
837 " 'verify-peer': %i} }",
838 data->workdir, args->verifyclient);
839 qobject_unref(rsp);
840 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
842 if (args->authzclient) {
843 rsp = wait_command(to,
844 "{ 'execute': 'object-add',"
845 " 'arguments': { 'qom-type': 'authz-simple',"
846 " 'id': 'tlsauthz0',"
847 " 'identity': %s} }",
848 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
849 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
852 return data;
856 * The normal case: match server's cert hostname against
857 * whatever host we were telling QEMU to connect to (if any)
859 static void *
860 test_migrate_tls_x509_start_default_host(QTestState *from,
861 QTestState *to)
863 TestMigrateTLSX509 args = {
864 .verifyclient = true,
865 .clientcert = true,
866 .certipaddr = "127.0.0.1"
868 return test_migrate_tls_x509_start_common(from, to, &args);
872 * The unusual case: the server's cert is different from
873 * the address we're telling QEMU to connect to (if any),
874 * so we must give QEMU an explicit hostname to validate
876 static void *
877 test_migrate_tls_x509_start_override_host(QTestState *from,
878 QTestState *to)
880 TestMigrateTLSX509 args = {
881 .verifyclient = true,
882 .clientcert = true,
883 .certhostname = "qemu.org",
885 return test_migrate_tls_x509_start_common(from, to, &args);
889 * The unusual case: the server's cert is different from
890 * the address we're telling QEMU to connect to, and so we
891 * expect the client to reject the server
893 static void *
894 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
895 QTestState *to)
897 TestMigrateTLSX509 args = {
898 .verifyclient = true,
899 .clientcert = true,
900 .certipaddr = "10.0.0.1",
902 return test_migrate_tls_x509_start_common(from, to, &args);
905 static void *
906 test_migrate_tls_x509_start_friendly_client(QTestState *from,
907 QTestState *to)
909 TestMigrateTLSX509 args = {
910 .verifyclient = true,
911 .clientcert = true,
912 .authzclient = true,
913 .certipaddr = "127.0.0.1",
915 return test_migrate_tls_x509_start_common(from, to, &args);
918 static void *
919 test_migrate_tls_x509_start_hostile_client(QTestState *from,
920 QTestState *to)
922 TestMigrateTLSX509 args = {
923 .verifyclient = true,
924 .clientcert = true,
925 .hostileclient = true,
926 .authzclient = true,
927 .certipaddr = "127.0.0.1",
929 return test_migrate_tls_x509_start_common(from, to, &args);
933 * The case with no client certificate presented,
934 * and no server verification
936 static void *
937 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
938 QTestState *to)
940 TestMigrateTLSX509 args = {
941 .certipaddr = "127.0.0.1",
943 return test_migrate_tls_x509_start_common(from, to, &args);
947 * The case with no client certificate presented,
948 * and server verification rejecting
950 static void *
951 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
952 QTestState *to)
954 TestMigrateTLSX509 args = {
955 .verifyclient = true,
956 .certipaddr = "127.0.0.1",
958 return test_migrate_tls_x509_start_common(from, to, &args);
961 static void
962 test_migrate_tls_x509_finish(QTestState *from,
963 QTestState *to,
964 void *opaque)
966 TestMigrateTLSX509Data *data = opaque;
968 test_tls_cleanup(data->keyfile);
969 unlink(data->cacert);
970 unlink(data->servercert);
971 unlink(data->serverkey);
972 unlink(data->clientcert);
973 unlink(data->clientkey);
974 rmdir(data->workdir);
976 g_free(data->workdir);
977 g_free(data->keyfile);
978 g_free(data);
980 #endif /* CONFIG_TASN1 */
981 #endif /* CONFIG_GNUTLS */
983 static int migrate_postcopy_prepare(QTestState **from_ptr,
984 QTestState **to_ptr,
985 MigrateStart *args)
987 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
988 QTestState *from, *to;
990 if (test_migrate_start(&from, &to, uri, args)) {
991 return -1;
994 migrate_set_capability(from, "postcopy-ram", true);
995 migrate_set_capability(to, "postcopy-ram", true);
996 migrate_set_capability(to, "postcopy-blocktime", true);
998 migrate_ensure_non_converge(from);
1000 /* Wait for the first serial output from the source */
1001 wait_for_serial("src_serial");
1003 migrate_qmp(from, uri, "{}");
1005 wait_for_migration_pass(from);
1007 *from_ptr = from;
1008 *to_ptr = to;
1010 return 0;
1013 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
1015 wait_for_migration_complete(from);
1017 /* Make sure we get at least one "B" on destination */
1018 wait_for_serial("dest_serial");
1020 if (uffd_feature_thread_id) {
1021 read_blocktime(to);
1024 test_migrate_end(from, to, true);
1027 static void test_postcopy(void)
1029 MigrateStart args = {};
1030 QTestState *from, *to;
1032 if (migrate_postcopy_prepare(&from, &to, &args)) {
1033 return;
1035 migrate_postcopy_start(from, to);
1036 migrate_postcopy_complete(from, to);
1039 static void test_postcopy_recovery(void)
1041 MigrateStart args = {
1042 .hide_stderr = true,
1044 QTestState *from, *to;
1045 g_autofree char *uri = NULL;
1047 if (migrate_postcopy_prepare(&from, &to, &args)) {
1048 return;
1051 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1052 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1054 /* Now we start the postcopy */
1055 migrate_postcopy_start(from, to);
1058 * Wait until postcopy is really started; we can only run the
1059 * migrate-pause command during a postcopy
1061 wait_for_migration_status(from, "postcopy-active", NULL);
1064 * Manually stop the postcopy migration. This emulates a network
1065 * failure with the migration socket
1067 migrate_pause(from);
1070 * Wait for destination side to reach postcopy-paused state. The
1071 * migrate-recover command can only succeed if destination machine
1072 * is in the paused state
1074 wait_for_migration_status(to, "postcopy-paused",
1075 (const char * []) { "failed", "active",
1076 "completed", NULL });
1079 * Create a new socket to emulate a new channel that is different
1080 * from the broken migration channel; tell the destination to
1081 * listen to the new port
1083 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1084 migrate_recover(to, uri);
1087 * Try to rebuild the migration channel using the resume flag and
1088 * the newly created channel
1090 wait_for_migration_status(from, "postcopy-paused",
1091 (const char * []) { "failed", "active",
1092 "completed", NULL });
1093 migrate_qmp(from, uri, "{'resume': true}");
1095 /* Restore the postcopy bandwidth to unlimited */
1096 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1098 migrate_postcopy_complete(from, to);
1101 static void test_baddest(void)
1103 MigrateStart args = {
1104 .hide_stderr = true
1106 QTestState *from, *to;
1108 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1109 return;
1111 migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1112 wait_for_migration_fail(from, false);
1113 test_migrate_end(from, to, false);
1117 * A hook that runs after the src and dst QEMUs have been
1118 * created, but before the migration is started. This can
1119 * be used to set migration parameters and capabilities.
1121 * Returns: NULL, or a pointer to opaque state to be
1122 * later passed to the TestMigrateFinishHook
1124 typedef void * (*TestMigrateStartHook)(QTestState *from,
1125 QTestState *to);
1128 * A hook that runs after the migration has finished,
1129 * regardless of whether it succeeded or failed, but
1130 * before QEMU has terminated (unless it self-terminated
1131 * due to migration error)
1133 * @opaque is a pointer to state previously returned
1134 * by the TestMigrateStartHook if any, or NULL.
1136 typedef void (*TestMigrateFinishHook)(QTestState *from,
1137 QTestState *to,
1138 void *opaque);
1140 typedef struct {
1141 /* Optional: fine tune start parameters */
1142 MigrateStart start;
1144 /* Required: the URI for the dst QEMU to listen on */
1145 const char *listen_uri;
1148 * Optional: the URI for the src QEMU to connect to
1149 * If NULL, then it will query the dst QEMU for its actual
1150 * listening address and use that as the connect address.
1151 * This allows for dynamically picking a free TCP port.
1153 const char *connect_uri;
1155 /* Optional: callback to run at start to set migration parameters */
1156 TestMigrateStartHook start_hook;
1157 /* Optional: callback to run at finish to cleanup */
1158 TestMigrateFinishHook finish_hook;
1161 * Optional: normally we expect the migration process to complete.
1163 * There can be a variety of reasons and stages in which failure
1164 * can happen during tests.
1166 * If a failure is expected to happen at time of establishing
1167 * the connection, then MIG_TEST_FAIL will indicate that the dst
1168 * QEMU is expected to stay running and accept future migration
1169 * connections.
1171 * If a failure is expected to happen while processing the
1172 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
1173 * that the dst QEMU is expected to quit with non-zero exit status
1175 enum {
1176 /* This test should succeed, the default */
1177 MIG_TEST_SUCCEED = 0,
1178 /* This test should fail, dest qemu should keep alive */
1179 MIG_TEST_FAIL,
1180 /* This test should fail, dest qemu should fail with abnormal status */
1181 MIG_TEST_FAIL_DEST_QUIT_ERR,
1182 } result;
1184 /* Optional: set number of migration passes to wait for */
1185 unsigned int iterations;
1186 } MigrateCommon;
1188 static void test_precopy_common(MigrateCommon *args)
1190 QTestState *from, *to;
1191 void *data_hook = NULL;
1193 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1194 return;
1197 migrate_ensure_non_converge(from);
1199 if (args->start_hook) {
1200 data_hook = args->start_hook(from, to);
1203 /* Wait for the first serial output from the source */
1204 wait_for_serial("src_serial");
1206 if (!args->connect_uri) {
1207 g_autofree char *local_connect_uri =
1208 migrate_get_socket_address(to, "socket-address");
1209 migrate_qmp(from, local_connect_uri, "{}");
1210 } else {
1211 migrate_qmp(from, args->connect_uri, "{}");
1215 if (args->result != MIG_TEST_SUCCEED) {
1216 bool allow_active = args->result == MIG_TEST_FAIL;
1217 wait_for_migration_fail(from, allow_active);
1219 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1220 qtest_set_expected_status(to, 1);
1222 } else {
1223 if (args->iterations) {
1224 while (args->iterations--) {
1225 wait_for_migration_pass(from);
1227 } else {
1228 wait_for_migration_pass(from);
1231 migrate_ensure_converge(from);
1233 /* We do this first, as it has a timeout to stop us
1234 * hanging forever if migration didn't converge */
1235 wait_for_migration_complete(from);
1237 if (!got_stop) {
1238 qtest_qmp_eventwait(from, "STOP");
1241 qtest_qmp_eventwait(to, "RESUME");
1243 wait_for_serial("dest_serial");
1246 if (args->finish_hook) {
1247 args->finish_hook(from, to, data_hook);
1250 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1253 static void test_precopy_unix_plain(void)
1255 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1256 MigrateCommon args = {
1257 .listen_uri = uri,
1258 .connect_uri = uri,
1261 test_precopy_common(&args);
1265 static void test_precopy_unix_dirty_ring(void)
1267 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1268 MigrateCommon args = {
1269 .start = {
1270 .use_dirty_ring = true,
1272 .listen_uri = uri,
1273 .connect_uri = uri,
1276 test_precopy_common(&args);
1279 #ifdef CONFIG_GNUTLS
1280 static void test_precopy_unix_tls_psk(void)
1282 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1283 MigrateCommon args = {
1284 .connect_uri = uri,
1285 .listen_uri = uri,
1286 .start_hook = test_migrate_tls_psk_start_match,
1287 .finish_hook = test_migrate_tls_psk_finish,
1290 test_precopy_common(&args);
1293 #ifdef CONFIG_TASN1
1294 static void test_precopy_unix_tls_x509_default_host(void)
1296 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1297 MigrateCommon args = {
1298 .start = {
1299 .hide_stderr = true,
1301 .connect_uri = uri,
1302 .listen_uri = uri,
1303 .start_hook = test_migrate_tls_x509_start_default_host,
1304 .finish_hook = test_migrate_tls_x509_finish,
1305 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1308 test_precopy_common(&args);
1311 static void test_precopy_unix_tls_x509_override_host(void)
1313 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1314 MigrateCommon args = {
1315 .connect_uri = uri,
1316 .listen_uri = uri,
1317 .start_hook = test_migrate_tls_x509_start_override_host,
1318 .finish_hook = test_migrate_tls_x509_finish,
1321 test_precopy_common(&args);
1323 #endif /* CONFIG_TASN1 */
1324 #endif /* CONFIG_GNUTLS */
1326 #if 0
1327 /* Currently upset on aarch64 TCG */
1328 static void test_ignore_shared(void)
1330 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1331 QTestState *from, *to;
1333 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1334 return;
1337 migrate_set_capability(from, "x-ignore-shared", true);
1338 migrate_set_capability(to, "x-ignore-shared", true);
1340 /* Wait for the first serial output from the source */
1341 wait_for_serial("src_serial");
1343 migrate_qmp(from, uri, "{}");
1345 wait_for_migration_pass(from);
1347 if (!got_stop) {
1348 qtest_qmp_eventwait(from, "STOP");
1351 qtest_qmp_eventwait(to, "RESUME");
1353 wait_for_serial("dest_serial");
1354 wait_for_migration_complete(from);
1356 /* Check whether shared RAM has been really skipped */
1357 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1359 test_migrate_end(from, to, true);
1361 #endif
1363 static void *
1364 test_migrate_xbzrle_start(QTestState *from,
1365 QTestState *to)
1367 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1369 migrate_set_capability(from, "xbzrle", true);
1370 migrate_set_capability(to, "xbzrle", true);
1372 return NULL;
1375 static void test_precopy_unix_xbzrle(void)
1377 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1378 MigrateCommon args = {
1379 .connect_uri = uri,
1380 .listen_uri = uri,
1382 .start_hook = test_migrate_xbzrle_start,
1384 .iterations = 2,
1387 test_precopy_common(&args);
1390 static void test_precopy_tcp_plain(void)
1392 MigrateCommon args = {
1393 .listen_uri = "tcp:127.0.0.1:0",
1396 test_precopy_common(&args);
1399 #ifdef CONFIG_GNUTLS
1400 static void test_precopy_tcp_tls_psk_match(void)
1402 MigrateCommon args = {
1403 .listen_uri = "tcp:127.0.0.1:0",
1404 .start_hook = test_migrate_tls_psk_start_match,
1405 .finish_hook = test_migrate_tls_psk_finish,
1408 test_precopy_common(&args);
1411 static void test_precopy_tcp_tls_psk_mismatch(void)
1413 MigrateCommon args = {
1414 .start = {
1415 .hide_stderr = true,
1417 .listen_uri = "tcp:127.0.0.1:0",
1418 .start_hook = test_migrate_tls_psk_start_mismatch,
1419 .finish_hook = test_migrate_tls_psk_finish,
1420 .result = MIG_TEST_FAIL,
1423 test_precopy_common(&args);
1426 #ifdef CONFIG_TASN1
1427 static void test_precopy_tcp_tls_x509_default_host(void)
1429 MigrateCommon args = {
1430 .listen_uri = "tcp:127.0.0.1:0",
1431 .start_hook = test_migrate_tls_x509_start_default_host,
1432 .finish_hook = test_migrate_tls_x509_finish,
1435 test_precopy_common(&args);
1438 static void test_precopy_tcp_tls_x509_override_host(void)
1440 MigrateCommon args = {
1441 .listen_uri = "tcp:127.0.0.1:0",
1442 .start_hook = test_migrate_tls_x509_start_override_host,
1443 .finish_hook = test_migrate_tls_x509_finish,
1446 test_precopy_common(&args);
1449 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1451 MigrateCommon args = {
1452 .start = {
1453 .hide_stderr = true,
1455 .listen_uri = "tcp:127.0.0.1:0",
1456 .start_hook = test_migrate_tls_x509_start_mismatch_host,
1457 .finish_hook = test_migrate_tls_x509_finish,
1458 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1461 test_precopy_common(&args);
1464 static void test_precopy_tcp_tls_x509_friendly_client(void)
1466 MigrateCommon args = {
1467 .listen_uri = "tcp:127.0.0.1:0",
1468 .start_hook = test_migrate_tls_x509_start_friendly_client,
1469 .finish_hook = test_migrate_tls_x509_finish,
1472 test_precopy_common(&args);
1475 static void test_precopy_tcp_tls_x509_hostile_client(void)
1477 MigrateCommon args = {
1478 .start = {
1479 .hide_stderr = true,
1481 .listen_uri = "tcp:127.0.0.1:0",
1482 .start_hook = test_migrate_tls_x509_start_hostile_client,
1483 .finish_hook = test_migrate_tls_x509_finish,
1484 .result = MIG_TEST_FAIL,
1487 test_precopy_common(&args);
1490 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1492 MigrateCommon args = {
1493 .listen_uri = "tcp:127.0.0.1:0",
1494 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1495 .finish_hook = test_migrate_tls_x509_finish,
1498 test_precopy_common(&args);
1501 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1503 MigrateCommon args = {
1504 .start = {
1505 .hide_stderr = true,
1507 .listen_uri = "tcp:127.0.0.1:0",
1508 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1509 .finish_hook = test_migrate_tls_x509_finish,
1510 .result = MIG_TEST_FAIL,
1513 test_precopy_common(&args);
1515 #endif /* CONFIG_TASN1 */
1516 #endif /* CONFIG_GNUTLS */
1518 static void *test_migrate_fd_start_hook(QTestState *from,
1519 QTestState *to)
1521 QDict *rsp;
1522 int ret;
1523 int pair[2];
1525 /* Create two connected sockets for migration */
1526 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1527 g_assert_cmpint(ret, ==, 0);
1529 /* Send the 1st socket to the target */
1530 rsp = wait_command_fd(to, pair[0],
1531 "{ 'execute': 'getfd',"
1532 " 'arguments': { 'fdname': 'fd-mig' }}");
1533 qobject_unref(rsp);
1534 close(pair[0]);
1536 /* Start incoming migration from the 1st socket */
1537 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1538 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1539 qobject_unref(rsp);
1541 /* Send the 2nd socket to the target */
1542 rsp = wait_command_fd(from, pair[1],
1543 "{ 'execute': 'getfd',"
1544 " 'arguments': { 'fdname': 'fd-mig' }}");
1545 qobject_unref(rsp);
1546 close(pair[1]);
1548 return NULL;
1551 static void test_migrate_fd_finish_hook(QTestState *from,
1552 QTestState *to,
1553 void *opaque)
1555 QDict *rsp;
1556 const char *error_desc;
1558 /* Test closing fds */
1559 /* We assume, that QEMU removes named fd from its list,
1560 * so this should fail */
1561 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1562 " 'arguments': { 'fdname': 'fd-mig' }}");
1563 g_assert_true(qdict_haskey(rsp, "error"));
1564 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1565 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1566 qobject_unref(rsp);
1568 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1569 " 'arguments': { 'fdname': 'fd-mig' }}");
1570 g_assert_true(qdict_haskey(rsp, "error"));
1571 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1572 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1573 qobject_unref(rsp);
1576 static void test_migrate_fd_proto(void)
1578 MigrateCommon args = {
1579 .listen_uri = "defer",
1580 .connect_uri = "fd:fd-mig",
1581 .start_hook = test_migrate_fd_start_hook,
1582 .finish_hook = test_migrate_fd_finish_hook
1584 test_precopy_common(&args);
1587 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1589 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1590 QTestState *from, *to;
1592 if (test_migrate_start(&from, &to, uri, args)) {
1593 return;
1597 * UUID validation is at the begin of migration. So, the main process of
1598 * migration is not interesting for us here. Thus, set huge downtime for
1599 * very fast migration.
1601 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1602 migrate_set_capability(from, "validate-uuid", true);
1604 /* Wait for the first serial output from the source */
1605 wait_for_serial("src_serial");
1607 migrate_qmp(from, uri, "{}");
1609 if (should_fail) {
1610 qtest_set_expected_status(to, 1);
1611 wait_for_migration_fail(from, true);
1612 } else {
1613 wait_for_migration_complete(from);
1616 test_migrate_end(from, to, false);
1619 static void test_validate_uuid(void)
1621 MigrateStart args = {
1622 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1623 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
1626 do_test_validate_uuid(&args, false);
1629 static void test_validate_uuid_error(void)
1631 MigrateStart args = {
1632 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1633 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1634 .hide_stderr = true,
1637 do_test_validate_uuid(&args, true);
1640 static void test_validate_uuid_src_not_set(void)
1642 MigrateStart args = {
1643 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1644 .hide_stderr = true,
1647 do_test_validate_uuid(&args, false);
1650 static void test_validate_uuid_dst_not_set(void)
1652 MigrateStart args = {
1653 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1654 .hide_stderr = true,
1657 do_test_validate_uuid(&args, false);
1660 static void test_migrate_auto_converge(void)
1662 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1663 MigrateStart args = {};
1664 QTestState *from, *to;
1665 int64_t remaining, percentage;
1668 * We want the test to be stable and as fast as possible.
1669 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1670 * so we need to decrease a bandwidth.
1672 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1673 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1674 const int64_t downtime_limit = 250; /* 250ms */
1676 * We migrate through unix-socket (> 500Mb/s).
1677 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1678 * So, we can predict expected_threshold
1680 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1682 if (test_migrate_start(&from, &to, uri, &args)) {
1683 return;
1686 migrate_set_capability(from, "auto-converge", true);
1687 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1688 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1689 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1692 * Set the initial parameters so that the migration could not converge
1693 * without throttling.
1695 migrate_ensure_non_converge(from);
1697 /* To check remaining size after precopy */
1698 migrate_set_capability(from, "pause-before-switchover", true);
1700 /* Wait for the first serial output from the source */
1701 wait_for_serial("src_serial");
1703 migrate_qmp(from, uri, "{}");
1705 /* Wait for throttling begins */
1706 percentage = 0;
1707 while (percentage == 0) {
1708 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1709 usleep(100);
1710 g_assert_false(got_stop);
1712 /* The first percentage of throttling should be equal to init_pct */
1713 g_assert_cmpint(percentage, ==, init_pct);
1714 /* Now, when we tested that throttling works, let it converge */
1715 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1716 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1719 * Wait for pre-switchover status to check last throttle percentage
1720 * and remaining. These values will be zeroed later
1722 wait_for_migration_status(from, "pre-switchover", NULL);
1724 /* The final percentage of throttling shouldn't be greater than max_pct */
1725 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1726 g_assert_cmpint(percentage, <=, max_pct);
1728 remaining = read_ram_property_int(from, "remaining");
1729 g_assert_cmpint(remaining, <,
1730 (expected_threshold + expected_threshold / 100));
1732 migrate_continue(from, "pre-switchover");
1734 qtest_qmp_eventwait(to, "RESUME");
1736 wait_for_serial("dest_serial");
1737 wait_for_migration_complete(from);
1740 test_migrate_end(from, to, true);
1743 static void *
1744 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
1745 QTestState *to,
1746 const char *method)
1748 QDict *rsp;
1750 migrate_set_parameter_int(from, "multifd-channels", 16);
1751 migrate_set_parameter_int(to, "multifd-channels", 16);
1753 migrate_set_parameter_str(from, "multifd-compression", method);
1754 migrate_set_parameter_str(to, "multifd-compression", method);
1756 migrate_set_capability(from, "multifd", true);
1757 migrate_set_capability(to, "multifd", true);
1759 /* Start incoming migration from the 1st socket */
1760 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1761 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1762 qobject_unref(rsp);
1764 return NULL;
1767 static void *
1768 test_migrate_precopy_tcp_multifd_start(QTestState *from,
1769 QTestState *to)
1771 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1774 static void *
1775 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
1776 QTestState *to)
1778 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
1781 #ifdef CONFIG_ZSTD
1782 static void *
1783 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
1784 QTestState *to)
1786 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
1788 #endif /* CONFIG_ZSTD */
1790 static void test_multifd_tcp_none(void)
1792 MigrateCommon args = {
1793 .listen_uri = "defer",
1794 .start_hook = test_migrate_precopy_tcp_multifd_start,
1796 test_precopy_common(&args);
1799 static void test_multifd_tcp_zlib(void)
1801 MigrateCommon args = {
1802 .listen_uri = "defer",
1803 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
1805 test_precopy_common(&args);
1808 #ifdef CONFIG_ZSTD
1809 static void test_multifd_tcp_zstd(void)
1811 MigrateCommon args = {
1812 .listen_uri = "defer",
1813 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
1815 test_precopy_common(&args);
1817 #endif
1819 #ifdef CONFIG_GNUTLS
1820 static void *
1821 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
1822 QTestState *to)
1824 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1825 return test_migrate_tls_psk_start_match(from, to);
1828 static void *
1829 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
1830 QTestState *to)
1832 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1833 return test_migrate_tls_psk_start_mismatch(from, to);
1836 #ifdef CONFIG_TASN1
1837 static void *
1838 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
1839 QTestState *to)
1841 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1842 return test_migrate_tls_x509_start_default_host(from, to);
1845 static void *
1846 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
1847 QTestState *to)
1849 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1850 return test_migrate_tls_x509_start_override_host(from, to);
1853 static void *
1854 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
1855 QTestState *to)
1857 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1858 return test_migrate_tls_x509_start_mismatch_host(from, to);
1861 static void *
1862 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
1863 QTestState *to)
1865 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1866 return test_migrate_tls_x509_start_allow_anon_client(from, to);
1869 static void *
1870 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
1871 QTestState *to)
1873 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1874 return test_migrate_tls_x509_start_reject_anon_client(from, to);
1876 #endif /* CONFIG_TASN1 */
1878 static void test_multifd_tcp_tls_psk_match(void)
1880 MigrateCommon args = {
1881 .listen_uri = "defer",
1882 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
1883 .finish_hook = test_migrate_tls_psk_finish,
1885 test_precopy_common(&args);
1888 static void test_multifd_tcp_tls_psk_mismatch(void)
1890 MigrateCommon args = {
1891 .start = {
1892 .hide_stderr = true,
1894 .listen_uri = "defer",
1895 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
1896 .finish_hook = test_migrate_tls_psk_finish,
1897 .result = MIG_TEST_FAIL,
1899 test_precopy_common(&args);
1902 #ifdef CONFIG_TASN1
1903 static void test_multifd_tcp_tls_x509_default_host(void)
1905 MigrateCommon args = {
1906 .listen_uri = "defer",
1907 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
1908 .finish_hook = test_migrate_tls_x509_finish,
1910 test_precopy_common(&args);
1913 static void test_multifd_tcp_tls_x509_override_host(void)
1915 MigrateCommon args = {
1916 .listen_uri = "defer",
1917 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
1918 .finish_hook = test_migrate_tls_x509_finish,
1920 test_precopy_common(&args);
1923 static void test_multifd_tcp_tls_x509_mismatch_host(void)
1926 * This has different behaviour to the non-multifd case.
1928 * In non-multifd case when client aborts due to mismatched
1929 * cert host, the server has already started trying to load
1930 * migration state, and so it exits with I/O failure.
1932 * In multifd case when client aborts due to mismatched
1933 * cert host, the server is still waiting for the other
1934 * multifd connections to arrive so hasn't started trying
1935 * to load migration state, and thus just aborts the migration
1936 * without exiting.
1938 MigrateCommon args = {
1939 .start = {
1940 .hide_stderr = true,
1942 .listen_uri = "defer",
1943 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
1944 .finish_hook = test_migrate_tls_x509_finish,
1945 .result = MIG_TEST_FAIL,
1947 test_precopy_common(&args);
1950 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
1952 MigrateCommon args = {
1953 .listen_uri = "defer",
1954 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
1955 .finish_hook = test_migrate_tls_x509_finish,
1957 test_precopy_common(&args);
1960 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
1962 MigrateCommon args = {
1963 .start = {
1964 .hide_stderr = true,
1966 .listen_uri = "defer",
1967 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
1968 .finish_hook = test_migrate_tls_x509_finish,
1969 .result = MIG_TEST_FAIL,
1971 test_precopy_common(&args);
1973 #endif /* CONFIG_TASN1 */
1974 #endif /* CONFIG_GNUTLS */
1977 * This test does:
1978 * source target
1979 * migrate_incoming
1980 * migrate
1981 * migrate_cancel
1982 * launch another target
1983 * migrate
1985 * And see that it works
1987 static void test_multifd_tcp_cancel(void)
1989 MigrateStart args = {
1990 .hide_stderr = true,
1992 QTestState *from, *to, *to2;
1993 QDict *rsp;
1994 g_autofree char *uri = NULL;
1996 if (test_migrate_start(&from, &to, "defer", &args)) {
1997 return;
2000 migrate_ensure_non_converge(from);
2002 migrate_set_parameter_int(from, "multifd-channels", 16);
2003 migrate_set_parameter_int(to, "multifd-channels", 16);
2005 migrate_set_capability(from, "multifd", true);
2006 migrate_set_capability(to, "multifd", true);
2008 /* Start incoming migration from the 1st socket */
2009 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
2010 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2011 qobject_unref(rsp);
2013 /* Wait for the first serial output from the source */
2014 wait_for_serial("src_serial");
2016 uri = migrate_get_socket_address(to, "socket-address");
2018 migrate_qmp(from, uri, "{}");
2020 wait_for_migration_pass(from);
2022 migrate_cancel(from);
2024 args = (MigrateStart){
2025 .only_target = true,
2028 if (test_migrate_start(&from, &to2, "defer", &args)) {
2029 return;
2032 migrate_set_parameter_int(to2, "multifd-channels", 16);
2034 migrate_set_capability(to2, "multifd", true);
2036 /* Start incoming migration from the 1st socket */
2037 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
2038 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2039 qobject_unref(rsp);
2041 g_free(uri);
2042 uri = migrate_get_socket_address(to2, "socket-address");
2044 wait_for_migration_status(from, "cancelled", NULL);
2046 migrate_ensure_converge(from);
2048 migrate_qmp(from, uri, "{}");
2050 wait_for_migration_pass(from);
2052 if (!got_stop) {
2053 qtest_qmp_eventwait(from, "STOP");
2055 qtest_qmp_eventwait(to2, "RESUME");
2057 wait_for_serial("dest_serial");
2058 wait_for_migration_complete(from);
2059 test_migrate_end(from, to2, true);
2062 static bool kvm_dirty_ring_supported(void)
2064 #if defined(__linux__) && defined(HOST_X86_64)
2065 int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2067 if (kvm_fd < 0) {
2068 return false;
2071 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2072 close(kvm_fd);
2074 /* We test with 4096 slots */
2075 if (ret < 4096) {
2076 return false;
2079 return true;
2080 #else
2081 return false;
2082 #endif
2085 int main(int argc, char **argv)
2087 char template[] = "/tmp/migration-test-XXXXXX";
2088 const bool has_kvm = qtest_has_accel("kvm");
2089 int ret;
2091 g_test_init(&argc, &argv, NULL);
2093 if (!ufd_version_check()) {
2094 return g_test_run();
2098 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2099 * is touchy due to race conditions on dirty bits (especially on PPC for
2100 * some reason)
2102 if (g_str_equal(qtest_get_arch(), "ppc64") &&
2103 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2104 g_test_message("Skipping test: kvm_hv not available");
2105 return g_test_run();
2109 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2110 * there until the problems are resolved
2112 if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm) {
2113 g_test_message("Skipping test: s390x host with KVM is required");
2114 return g_test_run();
2117 tmpfs = mkdtemp(template);
2118 if (!tmpfs) {
2119 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
2121 g_assert(tmpfs);
2123 module_call_init(MODULE_INIT_QOM);
2125 qtest_add_func("/migration/postcopy/unix", test_postcopy);
2126 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
2127 qtest_add_func("/migration/bad_dest", test_baddest);
2128 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2129 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2130 #ifdef CONFIG_GNUTLS
2131 qtest_add_func("/migration/precopy/unix/tls/psk",
2132 test_precopy_unix_tls_psk);
2133 #ifdef CONFIG_TASN1
2134 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2135 test_precopy_unix_tls_x509_default_host);
2136 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2137 test_precopy_unix_tls_x509_override_host);
2138 #endif /* CONFIG_TASN1 */
2139 #endif /* CONFIG_GNUTLS */
2141 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2142 #ifdef CONFIG_GNUTLS
2143 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2144 test_precopy_tcp_tls_psk_match);
2145 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2146 test_precopy_tcp_tls_psk_mismatch);
2147 #ifdef CONFIG_TASN1
2148 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2149 test_precopy_tcp_tls_x509_default_host);
2150 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2151 test_precopy_tcp_tls_x509_override_host);
2152 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2153 test_precopy_tcp_tls_x509_mismatch_host);
2154 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2155 test_precopy_tcp_tls_x509_friendly_client);
2156 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2157 test_precopy_tcp_tls_x509_hostile_client);
2158 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2159 test_precopy_tcp_tls_x509_allow_anon_client);
2160 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2161 test_precopy_tcp_tls_x509_reject_anon_client);
2162 #endif /* CONFIG_TASN1 */
2163 #endif /* CONFIG_GNUTLS */
2165 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2166 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2167 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2168 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2169 qtest_add_func("/migration/validate_uuid_src_not_set",
2170 test_validate_uuid_src_not_set);
2171 qtest_add_func("/migration/validate_uuid_dst_not_set",
2172 test_validate_uuid_dst_not_set);
2174 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2175 qtest_add_func("/migration/multifd/tcp/plain/none",
2176 test_multifd_tcp_none);
2177 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2178 test_multifd_tcp_cancel);
2179 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2180 test_multifd_tcp_zlib);
2181 #ifdef CONFIG_ZSTD
2182 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2183 test_multifd_tcp_zstd);
2184 #endif
2185 #ifdef CONFIG_GNUTLS
2186 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2187 test_multifd_tcp_tls_psk_match);
2188 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2189 test_multifd_tcp_tls_psk_mismatch);
2190 #ifdef CONFIG_TASN1
2191 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2192 test_multifd_tcp_tls_x509_default_host);
2193 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2194 test_multifd_tcp_tls_x509_override_host);
2195 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2196 test_multifd_tcp_tls_x509_mismatch_host);
2197 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2198 test_multifd_tcp_tls_x509_allow_anon_client);
2199 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2200 test_multifd_tcp_tls_x509_reject_anon_client);
2201 #endif /* CONFIG_TASN1 */
2202 #endif /* CONFIG_GNUTLS */
2204 if (kvm_dirty_ring_supported()) {
2205 qtest_add_func("/migration/dirty_ring",
2206 test_precopy_unix_dirty_ring);
2209 ret = g_test_run();
2211 g_assert_cmpint(ret, ==, 0);
2213 ret = rmdir(tmpfs);
2214 if (ret != 0) {
2215 g_test_message("unable to rmdir: path (%s): %s",
2216 tmpfs, strerror(errno));
2219 return ret;