]> git.proxmox.com Git - mirror_qemu.git/blob - tests/qtest/migration-test.c
tests/qtest: replace wait_command() with qtest_qmp_assert_success
[mirror_qemu.git] / tests / qtest / migration-test.c
1 /*
2 * QTest testcase for migration
3 *
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.
7 *
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.
10 *
11 */
12
13 #include "qemu/osdep.h"
14
15 #include "libqtest.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
26 #include "crypto/tlscredspsk.h"
27 #include "qapi/qmp/qlist.h"
28
29 #include "migration-helpers.h"
30 #include "tests/migration/migration-test.h"
31 #ifdef CONFIG_GNUTLS
32 # include "tests/unit/crypto-tls-psk-helpers.h"
33 # ifdef CONFIG_TASN1
34 # include "tests/unit/crypto-tls-x509-helpers.h"
35 # endif /* CONFIG_TASN1 */
36 #endif /* CONFIG_GNUTLS */
37
38 /* For dirty ring test; so far only x86_64 is supported */
39 #if defined(__linux__) && defined(HOST_X86_64)
40 #include "linux/kvm.h"
41 #endif
42
43 unsigned start_address;
44 unsigned end_address;
45 static bool uffd_feature_thread_id;
46 static bool got_stop;
47
48 /*
49 * Dirtylimit stop working if dirty page rate error
50 * value less than DIRTYLIMIT_TOLERANCE_RANGE
51 */
52 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
53
54 #if defined(__linux__)
55 #include <sys/syscall.h>
56 #include <sys/vfs.h>
57 #endif
58
59 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
60 #include <sys/eventfd.h>
61 #include <sys/ioctl.h>
62 #include "qemu/userfaultfd.h"
63
64 static bool ufd_version_check(void)
65 {
66 struct uffdio_api api_struct;
67 uint64_t ioctl_mask;
68
69 int ufd = uffd_open(O_CLOEXEC);
70
71 if (ufd == -1) {
72 g_test_message("Skipping test: userfaultfd not available");
73 return false;
74 }
75
76 api_struct.api = UFFD_API;
77 api_struct.features = 0;
78 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
79 g_test_message("Skipping test: UFFDIO_API failed");
80 return false;
81 }
82 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
83
84 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
85 (__u64)1 << _UFFDIO_UNREGISTER;
86 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
87 g_test_message("Skipping test: Missing userfault feature");
88 return false;
89 }
90
91 return true;
92 }
93
94 #else
95 static bool ufd_version_check(void)
96 {
97 g_test_message("Skipping test: Userfault not available (builtdtime)");
98 return false;
99 }
100
101 #endif
102
103 static char *tmpfs;
104
105 /* The boot file modifies memory area in [start_address, end_address)
106 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
107 */
108 #include "tests/migration/i386/a-b-bootblock.h"
109 #include "tests/migration/aarch64/a-b-kernel.h"
110 #include "tests/migration/s390x/a-b-bios.h"
111
112 static void init_bootfile(const char *bootpath, void *content, size_t len)
113 {
114 FILE *bootfile = fopen(bootpath, "wb");
115
116 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
117 fclose(bootfile);
118 }
119
120 /*
121 * Wait for some output in the serial output file,
122 * we get an 'A' followed by an endless string of 'B's
123 * but on the destination we won't have the A.
124 */
125 static void wait_for_serial(const char *side)
126 {
127 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
128 FILE *serialfile = fopen(serialpath, "r");
129 const char *arch = qtest_get_arch();
130 int started = (strcmp(side, "src_serial") == 0 &&
131 strcmp(arch, "ppc64") == 0) ? 0 : 1;
132
133 do {
134 int readvalue = fgetc(serialfile);
135
136 if (!started) {
137 /* SLOF prints its banner before starting test,
138 * to ignore it, mark the start of the test with '_',
139 * ignore all characters until this marker
140 */
141 switch (readvalue) {
142 case '_':
143 started = 1;
144 break;
145 case EOF:
146 fseek(serialfile, 0, SEEK_SET);
147 usleep(1000);
148 break;
149 }
150 continue;
151 }
152 switch (readvalue) {
153 case 'A':
154 /* Fine */
155 break;
156
157 case 'B':
158 /* It's alive! */
159 fclose(serialfile);
160 return;
161
162 case EOF:
163 started = (strcmp(side, "src_serial") == 0 &&
164 strcmp(arch, "ppc64") == 0) ? 0 : 1;
165 fseek(serialfile, 0, SEEK_SET);
166 usleep(1000);
167 break;
168
169 default:
170 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
171 g_assert_not_reached();
172 }
173 } while (true);
174 }
175
176 /*
177 * It's tricky to use qemu's migration event capability with qtest,
178 * events suddenly appearing confuse the qmp()/hmp() responses.
179 */
180
181 static int64_t read_ram_property_int(QTestState *who, const char *property)
182 {
183 QDict *rsp_return, *rsp_ram;
184 int64_t result;
185
186 rsp_return = migrate_query_not_failed(who);
187 if (!qdict_haskey(rsp_return, "ram")) {
188 /* Still in setup */
189 result = 0;
190 } else {
191 rsp_ram = qdict_get_qdict(rsp_return, "ram");
192 result = qdict_get_try_int(rsp_ram, property, 0);
193 }
194 qobject_unref(rsp_return);
195 return result;
196 }
197
198 static int64_t read_migrate_property_int(QTestState *who, const char *property)
199 {
200 QDict *rsp_return;
201 int64_t result;
202
203 rsp_return = migrate_query_not_failed(who);
204 result = qdict_get_try_int(rsp_return, property, 0);
205 qobject_unref(rsp_return);
206 return result;
207 }
208
209 static uint64_t get_migration_pass(QTestState *who)
210 {
211 return read_ram_property_int(who, "dirty-sync-count");
212 }
213
214 static void read_blocktime(QTestState *who)
215 {
216 QDict *rsp_return;
217
218 rsp_return = migrate_query_not_failed(who);
219 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
220 qobject_unref(rsp_return);
221 }
222
223 static void wait_for_migration_pass(QTestState *who)
224 {
225 uint64_t initial_pass = get_migration_pass(who);
226 uint64_t pass;
227
228 /* Wait for the 1st sync */
229 while (!got_stop && !initial_pass) {
230 usleep(1000);
231 initial_pass = get_migration_pass(who);
232 }
233
234 do {
235 usleep(1000);
236 pass = get_migration_pass(who);
237 } while (pass == initial_pass && !got_stop);
238 }
239
240 static void check_guests_ram(QTestState *who)
241 {
242 /* Our ASM test will have been incrementing one byte from each page from
243 * start_address to < end_address in order. This gives us a constraint
244 * that any page's byte should be equal or less than the previous pages
245 * byte (mod 256); and they should all be equal except for one transition
246 * at the point where we meet the incrementer. (We're running this with
247 * the guest stopped).
248 */
249 unsigned address;
250 uint8_t first_byte;
251 uint8_t last_byte;
252 bool hit_edge = false;
253 int bad = 0;
254
255 qtest_memread(who, start_address, &first_byte, 1);
256 last_byte = first_byte;
257
258 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
259 address += TEST_MEM_PAGE_SIZE)
260 {
261 uint8_t b;
262 qtest_memread(who, address, &b, 1);
263 if (b != last_byte) {
264 if (((b + 1) % 256) == last_byte && !hit_edge) {
265 /* This is OK, the guest stopped at the point of
266 * incrementing the previous page but didn't get
267 * to us yet.
268 */
269 hit_edge = true;
270 last_byte = b;
271 } else {
272 bad++;
273 if (bad <= 10) {
274 fprintf(stderr, "Memory content inconsistency at %x"
275 " first_byte = %x last_byte = %x current = %x"
276 " hit_edge = %x\n",
277 address, first_byte, last_byte, b, hit_edge);
278 }
279 }
280 }
281 }
282 if (bad >= 10) {
283 fprintf(stderr, "and in another %d pages", bad - 10);
284 }
285 g_assert(bad == 0);
286 }
287
288 static void cleanup(const char *filename)
289 {
290 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
291
292 unlink(path);
293 }
294
295 static char *SocketAddress_to_str(SocketAddress *addr)
296 {
297 switch (addr->type) {
298 case SOCKET_ADDRESS_TYPE_INET:
299 return g_strdup_printf("tcp:%s:%s",
300 addr->u.inet.host,
301 addr->u.inet.port);
302 case SOCKET_ADDRESS_TYPE_UNIX:
303 return g_strdup_printf("unix:%s",
304 addr->u.q_unix.path);
305 case SOCKET_ADDRESS_TYPE_FD:
306 return g_strdup_printf("fd:%s", addr->u.fd.str);
307 case SOCKET_ADDRESS_TYPE_VSOCK:
308 return g_strdup_printf("tcp:%s:%s",
309 addr->u.vsock.cid,
310 addr->u.vsock.port);
311 default:
312 return g_strdup("unknown address type");
313 }
314 }
315
316 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
317 {
318 QDict *rsp;
319 char *result;
320 SocketAddressList *addrs;
321 Visitor *iv = NULL;
322 QObject *object;
323
324 rsp = migrate_query(who);
325 object = qdict_get(rsp, parameter);
326
327 iv = qobject_input_visitor_new(object);
328 visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
329 visit_free(iv);
330
331 /* we are only using a single address */
332 result = SocketAddress_to_str(addrs->value);
333
334 qapi_free_SocketAddressList(addrs);
335 qobject_unref(rsp);
336 return result;
337 }
338
339 static long long migrate_get_parameter_int(QTestState *who,
340 const char *parameter)
341 {
342 QDict *rsp;
343 long long result;
344
345 rsp = qtest_qmp_assert_success_ref(
346 who, "{ 'execute': 'query-migrate-parameters' }");
347 result = qdict_get_int(rsp, parameter);
348 qobject_unref(rsp);
349 return result;
350 }
351
352 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
353 long long value)
354 {
355 long long result;
356
357 result = migrate_get_parameter_int(who, parameter);
358 g_assert_cmpint(result, ==, value);
359 }
360
361 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
362 long long value)
363 {
364 qtest_qmp_assert_success(who,
365 "{ 'execute': 'migrate-set-parameters',"
366 "'arguments': { %s: %lld } }",
367 parameter, value);
368 migrate_check_parameter_int(who, parameter, value);
369 }
370
371 static char *migrate_get_parameter_str(QTestState *who,
372 const char *parameter)
373 {
374 QDict *rsp;
375 char *result;
376
377 rsp = qtest_qmp_assert_success_ref(
378 who, "{ 'execute': 'query-migrate-parameters' }");
379 result = g_strdup(qdict_get_str(rsp, parameter));
380 qobject_unref(rsp);
381 return result;
382 }
383
384 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
385 const char *value)
386 {
387 g_autofree char *result = migrate_get_parameter_str(who, parameter);
388 g_assert_cmpstr(result, ==, value);
389 }
390
391 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
392 const char *value)
393 {
394 qtest_qmp_assert_success(who,
395 "{ 'execute': 'migrate-set-parameters',"
396 "'arguments': { %s: %s } }",
397 parameter, value);
398 migrate_check_parameter_str(who, parameter, value);
399 }
400
401 static long long migrate_get_parameter_bool(QTestState *who,
402 const char *parameter)
403 {
404 QDict *rsp;
405 int result;
406
407 rsp = qtest_qmp_assert_success_ref(
408 who, "{ 'execute': 'query-migrate-parameters' }");
409 result = qdict_get_bool(rsp, parameter);
410 qobject_unref(rsp);
411 return !!result;
412 }
413
414 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
415 int value)
416 {
417 int result;
418
419 result = migrate_get_parameter_bool(who, parameter);
420 g_assert_cmpint(result, ==, value);
421 }
422
423 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
424 int value)
425 {
426 qtest_qmp_assert_success(who,
427 "{ 'execute': 'migrate-set-parameters',"
428 "'arguments': { %s: %i } }",
429 parameter, value);
430 migrate_check_parameter_bool(who, parameter, value);
431 }
432
433 static void migrate_ensure_non_converge(QTestState *who)
434 {
435 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
436 migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
437 migrate_set_parameter_int(who, "downtime-limit", 1);
438 }
439
440 static void migrate_ensure_converge(QTestState *who)
441 {
442 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
443 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
444 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
445 }
446
447 static void migrate_pause(QTestState *who)
448 {
449 qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
450 }
451
452 static void migrate_continue(QTestState *who, const char *state)
453 {
454 qtest_qmp_assert_success(who,
455 "{ 'execute': 'migrate-continue',"
456 " 'arguments': { 'state': %s } }",
457 state);
458 }
459
460 static void migrate_recover(QTestState *who, const char *uri)
461 {
462 qtest_qmp_assert_success(who,
463 "{ 'execute': 'migrate-recover', "
464 " 'id': 'recover-cmd', "
465 " 'arguments': { 'uri': %s } }",
466 uri);
467 }
468
469 static void migrate_cancel(QTestState *who)
470 {
471 qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
472 }
473
474 static void migrate_set_capability(QTestState *who, const char *capability,
475 bool value)
476 {
477 qtest_qmp_assert_success(who,
478 "{ 'execute': 'migrate-set-capabilities',"
479 "'arguments': { "
480 "'capabilities': [ { "
481 "'capability': %s, 'state': %i } ] } }",
482 capability, value);
483 }
484
485 static void migrate_postcopy_start(QTestState *from, QTestState *to)
486 {
487 qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
488
489 if (!got_stop) {
490 qtest_qmp_eventwait(from, "STOP");
491 }
492
493 qtest_qmp_eventwait(to, "RESUME");
494 }
495
496 typedef struct {
497 /*
498 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
499 * unconditionally, because it means the user would like to be verbose.
500 */
501 bool hide_stderr;
502 bool use_shmem;
503 /* only launch the target process */
504 bool only_target;
505 /* Use dirty ring if true; dirty logging otherwise */
506 bool use_dirty_ring;
507 const char *opts_source;
508 const char *opts_target;
509 } MigrateStart;
510
511 /*
512 * A hook that runs after the src and dst QEMUs have been
513 * created, but before the migration is started. This can
514 * be used to set migration parameters and capabilities.
515 *
516 * Returns: NULL, or a pointer to opaque state to be
517 * later passed to the TestMigrateFinishHook
518 */
519 typedef void * (*TestMigrateStartHook)(QTestState *from,
520 QTestState *to);
521
522 /*
523 * A hook that runs after the migration has finished,
524 * regardless of whether it succeeded or failed, but
525 * before QEMU has terminated (unless it self-terminated
526 * due to migration error)
527 *
528 * @opaque is a pointer to state previously returned
529 * by the TestMigrateStartHook if any, or NULL.
530 */
531 typedef void (*TestMigrateFinishHook)(QTestState *from,
532 QTestState *to,
533 void *opaque);
534
535 typedef struct {
536 /* Optional: fine tune start parameters */
537 MigrateStart start;
538
539 /* Required: the URI for the dst QEMU to listen on */
540 const char *listen_uri;
541
542 /*
543 * Optional: the URI for the src QEMU to connect to
544 * If NULL, then it will query the dst QEMU for its actual
545 * listening address and use that as the connect address.
546 * This allows for dynamically picking a free TCP port.
547 */
548 const char *connect_uri;
549
550 /* Optional: callback to run at start to set migration parameters */
551 TestMigrateStartHook start_hook;
552 /* Optional: callback to run at finish to cleanup */
553 TestMigrateFinishHook finish_hook;
554
555 /*
556 * Optional: normally we expect the migration process to complete.
557 *
558 * There can be a variety of reasons and stages in which failure
559 * can happen during tests.
560 *
561 * If a failure is expected to happen at time of establishing
562 * the connection, then MIG_TEST_FAIL will indicate that the dst
563 * QEMU is expected to stay running and accept future migration
564 * connections.
565 *
566 * If a failure is expected to happen while processing the
567 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
568 * that the dst QEMU is expected to quit with non-zero exit status
569 */
570 enum {
571 /* This test should succeed, the default */
572 MIG_TEST_SUCCEED = 0,
573 /* This test should fail, dest qemu should keep alive */
574 MIG_TEST_FAIL,
575 /* This test should fail, dest qemu should fail with abnormal status */
576 MIG_TEST_FAIL_DEST_QUIT_ERR,
577 } result;
578
579 /* Optional: set number of migration passes to wait for */
580 unsigned int iterations;
581
582 /* Postcopy specific fields */
583 void *postcopy_data;
584 bool postcopy_preempt;
585 } MigrateCommon;
586
587 static int test_migrate_start(QTestState **from, QTestState **to,
588 const char *uri, MigrateStart *args)
589 {
590 g_autofree gchar *arch_source = NULL;
591 g_autofree gchar *arch_target = NULL;
592 g_autofree gchar *cmd_source = NULL;
593 g_autofree gchar *cmd_target = NULL;
594 const gchar *ignore_stderr;
595 g_autofree char *bootpath = NULL;
596 g_autofree char *shmem_opts = NULL;
597 g_autofree char *shmem_path = NULL;
598 const char *arch = qtest_get_arch();
599 const char *machine_opts = NULL;
600 const char *memory_size;
601
602 if (args->use_shmem) {
603 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
604 g_test_skip("/dev/shm is not supported");
605 return -1;
606 }
607 }
608
609 got_stop = false;
610 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
611 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
612 /* the assembled x86 boot sector should be exactly one sector large */
613 assert(sizeof(x86_bootsect) == 512);
614 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
615 memory_size = "150M";
616 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
617 arch_target = g_strdup(arch_source);
618 start_address = X86_TEST_MEM_START;
619 end_address = X86_TEST_MEM_END;
620 } else if (g_str_equal(arch, "s390x")) {
621 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
622 memory_size = "128M";
623 arch_source = g_strdup_printf("-bios %s", bootpath);
624 arch_target = g_strdup(arch_source);
625 start_address = S390_TEST_MEM_START;
626 end_address = S390_TEST_MEM_END;
627 } else if (strcmp(arch, "ppc64") == 0) {
628 machine_opts = "vsmt=8";
629 memory_size = "256M";
630 start_address = PPC_TEST_MEM_START;
631 end_address = PPC_TEST_MEM_END;
632 arch_source = g_strdup_printf("-nodefaults "
633 "-prom-env 'use-nvramrc?=true' -prom-env "
634 "'nvramrc=hex .\" _\" begin %x %x "
635 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
636 "until'", end_address, start_address);
637 arch_target = g_strdup("");
638 } else if (strcmp(arch, "aarch64") == 0) {
639 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
640 machine_opts = "virt,gic-version=max";
641 memory_size = "150M";
642 arch_source = g_strdup_printf("-cpu max "
643 "-kernel %s",
644 bootpath);
645 arch_target = g_strdup(arch_source);
646 start_address = ARM_TEST_MEM_START;
647 end_address = ARM_TEST_MEM_END;
648
649 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
650 } else {
651 g_assert_not_reached();
652 }
653
654 if (!getenv("QTEST_LOG") && args->hide_stderr) {
655 #ifndef _WIN32
656 ignore_stderr = "2>/dev/null";
657 #else
658 /*
659 * On Windows the QEMU executable is created via CreateProcess() and
660 * IO redirection does not work, so don't bother adding IO redirection
661 * to the command line.
662 */
663 ignore_stderr = "";
664 #endif
665 } else {
666 ignore_stderr = "";
667 }
668
669 if (args->use_shmem) {
670 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
671 shmem_opts = g_strdup_printf(
672 "-object memory-backend-file,id=mem0,size=%s"
673 ",mem-path=%s,share=on -numa node,memdev=mem0",
674 memory_size, shmem_path);
675 } else {
676 shmem_path = NULL;
677 shmem_opts = g_strdup("");
678 }
679
680 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
681 "-name source,debug-threads=on "
682 "-m %s "
683 "-serial file:%s/src_serial "
684 "%s %s %s %s",
685 args->use_dirty_ring ?
686 ",dirty-ring-size=4096" : "",
687 machine_opts ? " -machine " : "",
688 machine_opts ? machine_opts : "",
689 memory_size, tmpfs,
690 arch_source, shmem_opts,
691 args->opts_source ? args->opts_source : "",
692 ignore_stderr);
693 if (!args->only_target) {
694 *from = qtest_init(cmd_source);
695 qtest_qmp_set_event_callback(*from,
696 migrate_watch_for_stop,
697 &got_stop);
698 }
699
700 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
701 "-name target,debug-threads=on "
702 "-m %s "
703 "-serial file:%s/dest_serial "
704 "-incoming %s "
705 "%s %s %s %s",
706 args->use_dirty_ring ?
707 ",dirty-ring-size=4096" : "",
708 machine_opts ? " -machine " : "",
709 machine_opts ? machine_opts : "",
710 memory_size, tmpfs, uri,
711 arch_target, shmem_opts,
712 args->opts_target ? args->opts_target : "",
713 ignore_stderr);
714 *to = qtest_init(cmd_target);
715
716 /*
717 * Remove shmem file immediately to avoid memory leak in test failed case.
718 * It's valid becase QEMU has already opened this file
719 */
720 if (args->use_shmem) {
721 unlink(shmem_path);
722 }
723
724 return 0;
725 }
726
727 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
728 {
729 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
730
731 qtest_quit(from);
732
733 if (test_dest) {
734 qtest_memread(to, start_address, &dest_byte_a, 1);
735
736 /* Destination still running, wait for a byte to change */
737 do {
738 qtest_memread(to, start_address, &dest_byte_b, 1);
739 usleep(1000 * 10);
740 } while (dest_byte_a == dest_byte_b);
741
742 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
743
744 /* With it stopped, check nothing changes */
745 qtest_memread(to, start_address, &dest_byte_c, 1);
746 usleep(1000 * 200);
747 qtest_memread(to, start_address, &dest_byte_d, 1);
748 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
749
750 check_guests_ram(to);
751 }
752
753 qtest_quit(to);
754
755 cleanup("bootsect");
756 cleanup("migsocket");
757 cleanup("src_serial");
758 cleanup("dest_serial");
759 }
760
761 #ifdef CONFIG_GNUTLS
762 struct TestMigrateTLSPSKData {
763 char *workdir;
764 char *workdiralt;
765 char *pskfile;
766 char *pskfilealt;
767 };
768
769 static void *
770 test_migrate_tls_psk_start_common(QTestState *from,
771 QTestState *to,
772 bool mismatch)
773 {
774 struct TestMigrateTLSPSKData *data =
775 g_new0(struct TestMigrateTLSPSKData, 1);
776
777 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
778 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
779 QCRYPTO_TLS_CREDS_PSKFILE);
780 g_mkdir_with_parents(data->workdir, 0700);
781 test_tls_psk_init(data->pskfile);
782
783 if (mismatch) {
784 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
785 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
786 QCRYPTO_TLS_CREDS_PSKFILE);
787 g_mkdir_with_parents(data->workdiralt, 0700);
788 test_tls_psk_init_alt(data->pskfilealt);
789 }
790
791 qtest_qmp_assert_success(from,
792 "{ 'execute': 'object-add',"
793 " 'arguments': { 'qom-type': 'tls-creds-psk',"
794 " 'id': 'tlscredspsk0',"
795 " 'endpoint': 'client',"
796 " 'dir': %s,"
797 " 'username': 'qemu'} }",
798 data->workdir);
799
800 qtest_qmp_assert_success(to,
801 "{ 'execute': 'object-add',"
802 " 'arguments': { 'qom-type': 'tls-creds-psk',"
803 " 'id': 'tlscredspsk0',"
804 " 'endpoint': 'server',"
805 " 'dir': %s } }",
806 mismatch ? data->workdiralt : data->workdir);
807
808 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
809 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
810
811 return data;
812 }
813
814 static void *
815 test_migrate_tls_psk_start_match(QTestState *from,
816 QTestState *to)
817 {
818 return test_migrate_tls_psk_start_common(from, to, false);
819 }
820
821 static void *
822 test_migrate_tls_psk_start_mismatch(QTestState *from,
823 QTestState *to)
824 {
825 return test_migrate_tls_psk_start_common(from, to, true);
826 }
827
828 static void
829 test_migrate_tls_psk_finish(QTestState *from,
830 QTestState *to,
831 void *opaque)
832 {
833 struct TestMigrateTLSPSKData *data = opaque;
834
835 test_tls_psk_cleanup(data->pskfile);
836 if (data->pskfilealt) {
837 test_tls_psk_cleanup(data->pskfilealt);
838 }
839 rmdir(data->workdir);
840 if (data->workdiralt) {
841 rmdir(data->workdiralt);
842 }
843
844 g_free(data->workdiralt);
845 g_free(data->pskfilealt);
846 g_free(data->workdir);
847 g_free(data->pskfile);
848 g_free(data);
849 }
850
851 #ifdef CONFIG_TASN1
852 typedef struct {
853 char *workdir;
854 char *keyfile;
855 char *cacert;
856 char *servercert;
857 char *serverkey;
858 char *clientcert;
859 char *clientkey;
860 } TestMigrateTLSX509Data;
861
862 typedef struct {
863 bool verifyclient;
864 bool clientcert;
865 bool hostileclient;
866 bool authzclient;
867 const char *certhostname;
868 const char *certipaddr;
869 } TestMigrateTLSX509;
870
871 static void *
872 test_migrate_tls_x509_start_common(QTestState *from,
873 QTestState *to,
874 TestMigrateTLSX509 *args)
875 {
876 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
877
878 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
879 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
880
881 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
882 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
883 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
884 if (args->clientcert) {
885 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
886 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
887 }
888
889 g_mkdir_with_parents(data->workdir, 0700);
890
891 test_tls_init(data->keyfile);
892 #ifndef _WIN32
893 g_assert(link(data->keyfile, data->serverkey) == 0);
894 #else
895 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
896 #endif
897 if (args->clientcert) {
898 #ifndef _WIN32
899 g_assert(link(data->keyfile, data->clientkey) == 0);
900 #else
901 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
902 #endif
903 }
904
905 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
906 if (args->clientcert) {
907 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
908 args->hostileclient ?
909 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
910 QCRYPTO_TLS_TEST_CLIENT_NAME,
911 data->clientcert);
912 }
913
914 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
915 data->servercert,
916 args->certhostname,
917 args->certipaddr);
918
919 qtest_qmp_assert_success(from,
920 "{ 'execute': 'object-add',"
921 " 'arguments': { 'qom-type': 'tls-creds-x509',"
922 " 'id': 'tlscredsx509client0',"
923 " 'endpoint': 'client',"
924 " 'dir': %s,"
925 " 'sanity-check': true,"
926 " 'verify-peer': true} }",
927 data->workdir);
928 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
929 if (args->certhostname) {
930 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
931 }
932
933 qtest_qmp_assert_success(to,
934 "{ 'execute': 'object-add',"
935 " 'arguments': { 'qom-type': 'tls-creds-x509',"
936 " 'id': 'tlscredsx509server0',"
937 " 'endpoint': 'server',"
938 " 'dir': %s,"
939 " 'sanity-check': true,"
940 " 'verify-peer': %i} }",
941 data->workdir, args->verifyclient);
942 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
943
944 if (args->authzclient) {
945 qtest_qmp_assert_success(to,
946 "{ 'execute': 'object-add',"
947 " 'arguments': { 'qom-type': 'authz-simple',"
948 " 'id': 'tlsauthz0',"
949 " 'identity': %s} }",
950 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
951 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
952 }
953
954 return data;
955 }
956
957 /*
958 * The normal case: match server's cert hostname against
959 * whatever host we were telling QEMU to connect to (if any)
960 */
961 static void *
962 test_migrate_tls_x509_start_default_host(QTestState *from,
963 QTestState *to)
964 {
965 TestMigrateTLSX509 args = {
966 .verifyclient = true,
967 .clientcert = true,
968 .certipaddr = "127.0.0.1"
969 };
970 return test_migrate_tls_x509_start_common(from, to, &args);
971 }
972
973 /*
974 * The unusual case: the server's cert is different from
975 * the address we're telling QEMU to connect to (if any),
976 * so we must give QEMU an explicit hostname to validate
977 */
978 static void *
979 test_migrate_tls_x509_start_override_host(QTestState *from,
980 QTestState *to)
981 {
982 TestMigrateTLSX509 args = {
983 .verifyclient = true,
984 .clientcert = true,
985 .certhostname = "qemu.org",
986 };
987 return test_migrate_tls_x509_start_common(from, to, &args);
988 }
989
990 /*
991 * The unusual case: the server's cert is different from
992 * the address we're telling QEMU to connect to, and so we
993 * expect the client to reject the server
994 */
995 static void *
996 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
997 QTestState *to)
998 {
999 TestMigrateTLSX509 args = {
1000 .verifyclient = true,
1001 .clientcert = true,
1002 .certipaddr = "10.0.0.1",
1003 };
1004 return test_migrate_tls_x509_start_common(from, to, &args);
1005 }
1006
1007 static void *
1008 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1009 QTestState *to)
1010 {
1011 TestMigrateTLSX509 args = {
1012 .verifyclient = true,
1013 .clientcert = true,
1014 .authzclient = true,
1015 .certipaddr = "127.0.0.1",
1016 };
1017 return test_migrate_tls_x509_start_common(from, to, &args);
1018 }
1019
1020 static void *
1021 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1022 QTestState *to)
1023 {
1024 TestMigrateTLSX509 args = {
1025 .verifyclient = true,
1026 .clientcert = true,
1027 .hostileclient = true,
1028 .authzclient = true,
1029 .certipaddr = "127.0.0.1",
1030 };
1031 return test_migrate_tls_x509_start_common(from, to, &args);
1032 }
1033
1034 /*
1035 * The case with no client certificate presented,
1036 * and no server verification
1037 */
1038 static void *
1039 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1040 QTestState *to)
1041 {
1042 TestMigrateTLSX509 args = {
1043 .certipaddr = "127.0.0.1",
1044 };
1045 return test_migrate_tls_x509_start_common(from, to, &args);
1046 }
1047
1048 /*
1049 * The case with no client certificate presented,
1050 * and server verification rejecting
1051 */
1052 static void *
1053 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1054 QTestState *to)
1055 {
1056 TestMigrateTLSX509 args = {
1057 .verifyclient = true,
1058 .certipaddr = "127.0.0.1",
1059 };
1060 return test_migrate_tls_x509_start_common(from, to, &args);
1061 }
1062
1063 static void
1064 test_migrate_tls_x509_finish(QTestState *from,
1065 QTestState *to,
1066 void *opaque)
1067 {
1068 TestMigrateTLSX509Data *data = opaque;
1069
1070 test_tls_cleanup(data->keyfile);
1071 g_free(data->keyfile);
1072
1073 unlink(data->cacert);
1074 g_free(data->cacert);
1075 unlink(data->servercert);
1076 g_free(data->servercert);
1077 unlink(data->serverkey);
1078 g_free(data->serverkey);
1079
1080 if (data->clientcert) {
1081 unlink(data->clientcert);
1082 g_free(data->clientcert);
1083 }
1084 if (data->clientkey) {
1085 unlink(data->clientkey);
1086 g_free(data->clientkey);
1087 }
1088
1089 rmdir(data->workdir);
1090 g_free(data->workdir);
1091
1092 g_free(data);
1093 }
1094 #endif /* CONFIG_TASN1 */
1095 #endif /* CONFIG_GNUTLS */
1096
1097 static void *
1098 test_migrate_compress_start(QTestState *from,
1099 QTestState *to)
1100 {
1101 migrate_set_parameter_int(from, "compress-level", 1);
1102 migrate_set_parameter_int(from, "compress-threads", 4);
1103 migrate_set_parameter_bool(from, "compress-wait-thread", true);
1104 migrate_set_parameter_int(to, "decompress-threads", 4);
1105
1106 migrate_set_capability(from, "compress", true);
1107 migrate_set_capability(to, "compress", true);
1108
1109 return NULL;
1110 }
1111
1112 static void *
1113 test_migrate_compress_nowait_start(QTestState *from,
1114 QTestState *to)
1115 {
1116 migrate_set_parameter_int(from, "compress-level", 9);
1117 migrate_set_parameter_int(from, "compress-threads", 1);
1118 migrate_set_parameter_bool(from, "compress-wait-thread", false);
1119 migrate_set_parameter_int(to, "decompress-threads", 1);
1120
1121 migrate_set_capability(from, "compress", true);
1122 migrate_set_capability(to, "compress", true);
1123
1124 return NULL;
1125 }
1126
1127 static int migrate_postcopy_prepare(QTestState **from_ptr,
1128 QTestState **to_ptr,
1129 MigrateCommon *args)
1130 {
1131 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1132 QTestState *from, *to;
1133
1134 if (test_migrate_start(&from, &to, uri, &args->start)) {
1135 return -1;
1136 }
1137
1138 if (args->start_hook) {
1139 args->postcopy_data = args->start_hook(from, to);
1140 }
1141
1142 migrate_set_capability(from, "postcopy-ram", true);
1143 migrate_set_capability(to, "postcopy-ram", true);
1144 migrate_set_capability(to, "postcopy-blocktime", true);
1145
1146 if (args->postcopy_preempt) {
1147 migrate_set_capability(from, "postcopy-preempt", true);
1148 migrate_set_capability(to, "postcopy-preempt", true);
1149 }
1150
1151 migrate_ensure_non_converge(from);
1152
1153 /* Wait for the first serial output from the source */
1154 wait_for_serial("src_serial");
1155
1156 migrate_qmp(from, uri, "{}");
1157
1158 wait_for_migration_pass(from);
1159
1160 *from_ptr = from;
1161 *to_ptr = to;
1162
1163 return 0;
1164 }
1165
1166 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1167 MigrateCommon *args)
1168 {
1169 wait_for_migration_complete(from);
1170
1171 /* Make sure we get at least one "B" on destination */
1172 wait_for_serial("dest_serial");
1173
1174 if (uffd_feature_thread_id) {
1175 read_blocktime(to);
1176 }
1177
1178 if (args->finish_hook) {
1179 args->finish_hook(from, to, args->postcopy_data);
1180 args->postcopy_data = NULL;
1181 }
1182
1183 test_migrate_end(from, to, true);
1184 }
1185
1186 static void test_postcopy_common(MigrateCommon *args)
1187 {
1188 QTestState *from, *to;
1189
1190 if (migrate_postcopy_prepare(&from, &to, args)) {
1191 return;
1192 }
1193 migrate_postcopy_start(from, to);
1194 migrate_postcopy_complete(from, to, args);
1195 }
1196
1197 static void test_postcopy(void)
1198 {
1199 MigrateCommon args = { };
1200
1201 test_postcopy_common(&args);
1202 }
1203
1204 static void test_postcopy_compress(void)
1205 {
1206 MigrateCommon args = {
1207 .start_hook = test_migrate_compress_start
1208 };
1209
1210 test_postcopy_common(&args);
1211 }
1212
1213 static void test_postcopy_preempt(void)
1214 {
1215 MigrateCommon args = {
1216 .postcopy_preempt = true,
1217 };
1218
1219 test_postcopy_common(&args);
1220 }
1221
1222 #ifdef CONFIG_GNUTLS
1223 static void test_postcopy_tls_psk(void)
1224 {
1225 MigrateCommon args = {
1226 .start_hook = test_migrate_tls_psk_start_match,
1227 .finish_hook = test_migrate_tls_psk_finish,
1228 };
1229
1230 test_postcopy_common(&args);
1231 }
1232
1233 static void test_postcopy_preempt_tls_psk(void)
1234 {
1235 MigrateCommon args = {
1236 .postcopy_preempt = true,
1237 .start_hook = test_migrate_tls_psk_start_match,
1238 .finish_hook = test_migrate_tls_psk_finish,
1239 };
1240
1241 test_postcopy_common(&args);
1242 }
1243 #endif
1244
1245 static void test_postcopy_recovery_common(MigrateCommon *args)
1246 {
1247 QTestState *from, *to;
1248 g_autofree char *uri = NULL;
1249
1250 /* Always hide errors for postcopy recover tests since they're expected */
1251 args->start.hide_stderr = true;
1252
1253 if (migrate_postcopy_prepare(&from, &to, args)) {
1254 return;
1255 }
1256
1257 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1258 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1259
1260 /* Now we start the postcopy */
1261 migrate_postcopy_start(from, to);
1262
1263 /*
1264 * Wait until postcopy is really started; we can only run the
1265 * migrate-pause command during a postcopy
1266 */
1267 wait_for_migration_status(from, "postcopy-active", NULL);
1268
1269 /*
1270 * Manually stop the postcopy migration. This emulates a network
1271 * failure with the migration socket
1272 */
1273 migrate_pause(from);
1274
1275 /*
1276 * Wait for destination side to reach postcopy-paused state. The
1277 * migrate-recover command can only succeed if destination machine
1278 * is in the paused state
1279 */
1280 wait_for_migration_status(to, "postcopy-paused",
1281 (const char * []) { "failed", "active",
1282 "completed", NULL });
1283
1284 /*
1285 * Create a new socket to emulate a new channel that is different
1286 * from the broken migration channel; tell the destination to
1287 * listen to the new port
1288 */
1289 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1290 migrate_recover(to, uri);
1291
1292 /*
1293 * Try to rebuild the migration channel using the resume flag and
1294 * the newly created channel
1295 */
1296 wait_for_migration_status(from, "postcopy-paused",
1297 (const char * []) { "failed", "active",
1298 "completed", NULL });
1299 migrate_qmp(from, uri, "{'resume': true}");
1300
1301 /* Restore the postcopy bandwidth to unlimited */
1302 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1303
1304 migrate_postcopy_complete(from, to, args);
1305 }
1306
1307 static void test_postcopy_recovery(void)
1308 {
1309 MigrateCommon args = { };
1310
1311 test_postcopy_recovery_common(&args);
1312 }
1313
1314 static void test_postcopy_recovery_compress(void)
1315 {
1316 MigrateCommon args = {
1317 .start_hook = test_migrate_compress_start
1318 };
1319
1320 test_postcopy_recovery_common(&args);
1321 }
1322
1323 #ifdef CONFIG_GNUTLS
1324 static void test_postcopy_recovery_tls_psk(void)
1325 {
1326 MigrateCommon args = {
1327 .start_hook = test_migrate_tls_psk_start_match,
1328 .finish_hook = test_migrate_tls_psk_finish,
1329 };
1330
1331 test_postcopy_recovery_common(&args);
1332 }
1333 #endif
1334
1335 static void test_postcopy_preempt_recovery(void)
1336 {
1337 MigrateCommon args = {
1338 .postcopy_preempt = true,
1339 };
1340
1341 test_postcopy_recovery_common(&args);
1342 }
1343
1344 #ifdef CONFIG_GNUTLS
1345 /* This contains preempt+recovery+tls test altogether */
1346 static void test_postcopy_preempt_all(void)
1347 {
1348 MigrateCommon args = {
1349 .postcopy_preempt = true,
1350 .start_hook = test_migrate_tls_psk_start_match,
1351 .finish_hook = test_migrate_tls_psk_finish,
1352 };
1353
1354 test_postcopy_recovery_common(&args);
1355 }
1356
1357 #endif
1358
1359 static void test_baddest(void)
1360 {
1361 MigrateStart args = {
1362 .hide_stderr = true
1363 };
1364 QTestState *from, *to;
1365
1366 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1367 return;
1368 }
1369 migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1370 wait_for_migration_fail(from, false);
1371 test_migrate_end(from, to, false);
1372 }
1373
1374 static void test_precopy_common(MigrateCommon *args)
1375 {
1376 QTestState *from, *to;
1377 void *data_hook = NULL;
1378
1379 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1380 return;
1381 }
1382
1383 migrate_ensure_non_converge(from);
1384
1385 if (args->start_hook) {
1386 data_hook = args->start_hook(from, to);
1387 }
1388
1389 /* Wait for the first serial output from the source */
1390 if (args->result == MIG_TEST_SUCCEED) {
1391 wait_for_serial("src_serial");
1392 }
1393
1394 if (!args->connect_uri) {
1395 g_autofree char *local_connect_uri =
1396 migrate_get_socket_address(to, "socket-address");
1397 migrate_qmp(from, local_connect_uri, "{}");
1398 } else {
1399 migrate_qmp(from, args->connect_uri, "{}");
1400 }
1401
1402
1403 if (args->result != MIG_TEST_SUCCEED) {
1404 bool allow_active = args->result == MIG_TEST_FAIL;
1405 wait_for_migration_fail(from, allow_active);
1406
1407 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1408 qtest_set_expected_status(to, EXIT_FAILURE);
1409 }
1410 } else {
1411 if (args->iterations) {
1412 while (args->iterations--) {
1413 wait_for_migration_pass(from);
1414 }
1415 } else {
1416 wait_for_migration_pass(from);
1417 }
1418
1419 migrate_ensure_converge(from);
1420
1421 /* We do this first, as it has a timeout to stop us
1422 * hanging forever if migration didn't converge */
1423 wait_for_migration_complete(from);
1424
1425 if (!got_stop) {
1426 qtest_qmp_eventwait(from, "STOP");
1427 }
1428
1429 qtest_qmp_eventwait(to, "RESUME");
1430
1431 wait_for_serial("dest_serial");
1432 }
1433
1434 if (args->finish_hook) {
1435 args->finish_hook(from, to, data_hook);
1436 }
1437
1438 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1439 }
1440
1441 static void test_precopy_unix_plain(void)
1442 {
1443 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1444 MigrateCommon args = {
1445 .listen_uri = uri,
1446 .connect_uri = uri,
1447 };
1448
1449 test_precopy_common(&args);
1450 }
1451
1452
1453 static void test_precopy_unix_dirty_ring(void)
1454 {
1455 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1456 MigrateCommon args = {
1457 .start = {
1458 .use_dirty_ring = true,
1459 },
1460 .listen_uri = uri,
1461 .connect_uri = uri,
1462 };
1463
1464 test_precopy_common(&args);
1465 }
1466
1467 #ifdef CONFIG_GNUTLS
1468 static void test_precopy_unix_tls_psk(void)
1469 {
1470 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1471 MigrateCommon args = {
1472 .connect_uri = uri,
1473 .listen_uri = uri,
1474 .start_hook = test_migrate_tls_psk_start_match,
1475 .finish_hook = test_migrate_tls_psk_finish,
1476 };
1477
1478 test_precopy_common(&args);
1479 }
1480
1481 #ifdef CONFIG_TASN1
1482 static void test_precopy_unix_tls_x509_default_host(void)
1483 {
1484 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1485 MigrateCommon args = {
1486 .start = {
1487 .hide_stderr = true,
1488 },
1489 .connect_uri = uri,
1490 .listen_uri = uri,
1491 .start_hook = test_migrate_tls_x509_start_default_host,
1492 .finish_hook = test_migrate_tls_x509_finish,
1493 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1494 };
1495
1496 test_precopy_common(&args);
1497 }
1498
1499 static void test_precopy_unix_tls_x509_override_host(void)
1500 {
1501 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1502 MigrateCommon args = {
1503 .connect_uri = uri,
1504 .listen_uri = uri,
1505 .start_hook = test_migrate_tls_x509_start_override_host,
1506 .finish_hook = test_migrate_tls_x509_finish,
1507 };
1508
1509 test_precopy_common(&args);
1510 }
1511 #endif /* CONFIG_TASN1 */
1512 #endif /* CONFIG_GNUTLS */
1513
1514 #if 0
1515 /* Currently upset on aarch64 TCG */
1516 static void test_ignore_shared(void)
1517 {
1518 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1519 QTestState *from, *to;
1520
1521 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1522 return;
1523 }
1524
1525 migrate_set_capability(from, "x-ignore-shared", true);
1526 migrate_set_capability(to, "x-ignore-shared", true);
1527
1528 /* Wait for the first serial output from the source */
1529 wait_for_serial("src_serial");
1530
1531 migrate_qmp(from, uri, "{}");
1532
1533 wait_for_migration_pass(from);
1534
1535 if (!got_stop) {
1536 qtest_qmp_eventwait(from, "STOP");
1537 }
1538
1539 qtest_qmp_eventwait(to, "RESUME");
1540
1541 wait_for_serial("dest_serial");
1542 wait_for_migration_complete(from);
1543
1544 /* Check whether shared RAM has been really skipped */
1545 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1546
1547 test_migrate_end(from, to, true);
1548 }
1549 #endif
1550
1551 static void *
1552 test_migrate_xbzrle_start(QTestState *from,
1553 QTestState *to)
1554 {
1555 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1556
1557 migrate_set_capability(from, "xbzrle", true);
1558 migrate_set_capability(to, "xbzrle", true);
1559
1560 return NULL;
1561 }
1562
1563 static void test_precopy_unix_xbzrle(void)
1564 {
1565 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1566 MigrateCommon args = {
1567 .connect_uri = uri,
1568 .listen_uri = uri,
1569
1570 .start_hook = test_migrate_xbzrle_start,
1571
1572 .iterations = 2,
1573 };
1574
1575 test_precopy_common(&args);
1576 }
1577
1578 static void test_precopy_unix_compress(void)
1579 {
1580 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1581 MigrateCommon args = {
1582 .connect_uri = uri,
1583 .listen_uri = uri,
1584 .start_hook = test_migrate_compress_start,
1585 /*
1586 * Test that no invalid thread state is left over from
1587 * the previous iteration.
1588 */
1589 .iterations = 2,
1590 };
1591
1592 test_precopy_common(&args);
1593 }
1594
1595 static void test_precopy_unix_compress_nowait(void)
1596 {
1597 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1598 MigrateCommon args = {
1599 .connect_uri = uri,
1600 .listen_uri = uri,
1601 .start_hook = test_migrate_compress_nowait_start,
1602 /*
1603 * Test that no invalid thread state is left over from
1604 * the previous iteration.
1605 */
1606 .iterations = 2,
1607 };
1608
1609 test_precopy_common(&args);
1610 }
1611
1612 static void test_precopy_tcp_plain(void)
1613 {
1614 MigrateCommon args = {
1615 .listen_uri = "tcp:127.0.0.1:0",
1616 };
1617
1618 test_precopy_common(&args);
1619 }
1620
1621 #ifdef CONFIG_GNUTLS
1622 static void test_precopy_tcp_tls_psk_match(void)
1623 {
1624 MigrateCommon args = {
1625 .listen_uri = "tcp:127.0.0.1:0",
1626 .start_hook = test_migrate_tls_psk_start_match,
1627 .finish_hook = test_migrate_tls_psk_finish,
1628 };
1629
1630 test_precopy_common(&args);
1631 }
1632
1633 static void test_precopy_tcp_tls_psk_mismatch(void)
1634 {
1635 MigrateCommon args = {
1636 .start = {
1637 .hide_stderr = true,
1638 },
1639 .listen_uri = "tcp:127.0.0.1:0",
1640 .start_hook = test_migrate_tls_psk_start_mismatch,
1641 .finish_hook = test_migrate_tls_psk_finish,
1642 .result = MIG_TEST_FAIL,
1643 };
1644
1645 test_precopy_common(&args);
1646 }
1647
1648 #ifdef CONFIG_TASN1
1649 static void test_precopy_tcp_tls_x509_default_host(void)
1650 {
1651 MigrateCommon args = {
1652 .listen_uri = "tcp:127.0.0.1:0",
1653 .start_hook = test_migrate_tls_x509_start_default_host,
1654 .finish_hook = test_migrate_tls_x509_finish,
1655 };
1656
1657 test_precopy_common(&args);
1658 }
1659
1660 static void test_precopy_tcp_tls_x509_override_host(void)
1661 {
1662 MigrateCommon args = {
1663 .listen_uri = "tcp:127.0.0.1:0",
1664 .start_hook = test_migrate_tls_x509_start_override_host,
1665 .finish_hook = test_migrate_tls_x509_finish,
1666 };
1667
1668 test_precopy_common(&args);
1669 }
1670
1671 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1672 {
1673 MigrateCommon args = {
1674 .start = {
1675 .hide_stderr = true,
1676 },
1677 .listen_uri = "tcp:127.0.0.1:0",
1678 .start_hook = test_migrate_tls_x509_start_mismatch_host,
1679 .finish_hook = test_migrate_tls_x509_finish,
1680 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1681 };
1682
1683 test_precopy_common(&args);
1684 }
1685
1686 static void test_precopy_tcp_tls_x509_friendly_client(void)
1687 {
1688 MigrateCommon args = {
1689 .listen_uri = "tcp:127.0.0.1:0",
1690 .start_hook = test_migrate_tls_x509_start_friendly_client,
1691 .finish_hook = test_migrate_tls_x509_finish,
1692 };
1693
1694 test_precopy_common(&args);
1695 }
1696
1697 static void test_precopy_tcp_tls_x509_hostile_client(void)
1698 {
1699 MigrateCommon args = {
1700 .start = {
1701 .hide_stderr = true,
1702 },
1703 .listen_uri = "tcp:127.0.0.1:0",
1704 .start_hook = test_migrate_tls_x509_start_hostile_client,
1705 .finish_hook = test_migrate_tls_x509_finish,
1706 .result = MIG_TEST_FAIL,
1707 };
1708
1709 test_precopy_common(&args);
1710 }
1711
1712 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1713 {
1714 MigrateCommon args = {
1715 .listen_uri = "tcp:127.0.0.1:0",
1716 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1717 .finish_hook = test_migrate_tls_x509_finish,
1718 };
1719
1720 test_precopy_common(&args);
1721 }
1722
1723 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1724 {
1725 MigrateCommon args = {
1726 .start = {
1727 .hide_stderr = true,
1728 },
1729 .listen_uri = "tcp:127.0.0.1:0",
1730 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1731 .finish_hook = test_migrate_tls_x509_finish,
1732 .result = MIG_TEST_FAIL,
1733 };
1734
1735 test_precopy_common(&args);
1736 }
1737 #endif /* CONFIG_TASN1 */
1738 #endif /* CONFIG_GNUTLS */
1739
1740 #ifndef _WIN32
1741 static void *test_migrate_fd_start_hook(QTestState *from,
1742 QTestState *to)
1743 {
1744 int ret;
1745 int pair[2];
1746
1747 /* Create two connected sockets for migration */
1748 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1749 g_assert_cmpint(ret, ==, 0);
1750
1751 /* Send the 1st socket to the target */
1752 qtest_qmp_fds_assert_success(to, &pair[0], 1,
1753 "{ 'execute': 'getfd',"
1754 " 'arguments': { 'fdname': 'fd-mig' }}");
1755 close(pair[0]);
1756
1757 /* Start incoming migration from the 1st socket */
1758 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1759 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1760
1761 /* Send the 2nd socket to the target */
1762 qtest_qmp_fds_assert_success(from, &pair[1], 1,
1763 "{ 'execute': 'getfd',"
1764 " 'arguments': { 'fdname': 'fd-mig' }}");
1765 close(pair[1]);
1766
1767 return NULL;
1768 }
1769
1770 static void test_migrate_fd_finish_hook(QTestState *from,
1771 QTestState *to,
1772 void *opaque)
1773 {
1774 QDict *rsp;
1775 const char *error_desc;
1776
1777 /* Test closing fds */
1778 /* We assume, that QEMU removes named fd from its list,
1779 * so this should fail */
1780 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1781 " 'arguments': { 'fdname': 'fd-mig' }}");
1782 g_assert_true(qdict_haskey(rsp, "error"));
1783 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1784 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1785 qobject_unref(rsp);
1786
1787 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1788 " 'arguments': { 'fdname': 'fd-mig' }}");
1789 g_assert_true(qdict_haskey(rsp, "error"));
1790 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1791 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1792 qobject_unref(rsp);
1793 }
1794
1795 static void test_migrate_fd_proto(void)
1796 {
1797 MigrateCommon args = {
1798 .listen_uri = "defer",
1799 .connect_uri = "fd:fd-mig",
1800 .start_hook = test_migrate_fd_start_hook,
1801 .finish_hook = test_migrate_fd_finish_hook
1802 };
1803 test_precopy_common(&args);
1804 }
1805 #endif /* _WIN32 */
1806
1807 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1808 {
1809 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1810 QTestState *from, *to;
1811
1812 if (test_migrate_start(&from, &to, uri, args)) {
1813 return;
1814 }
1815
1816 /*
1817 * UUID validation is at the begin of migration. So, the main process of
1818 * migration is not interesting for us here. Thus, set huge downtime for
1819 * very fast migration.
1820 */
1821 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1822 migrate_set_capability(from, "validate-uuid", true);
1823
1824 /* Wait for the first serial output from the source */
1825 wait_for_serial("src_serial");
1826
1827 migrate_qmp(from, uri, "{}");
1828
1829 if (should_fail) {
1830 qtest_set_expected_status(to, EXIT_FAILURE);
1831 wait_for_migration_fail(from, true);
1832 } else {
1833 wait_for_migration_complete(from);
1834 }
1835
1836 test_migrate_end(from, to, false);
1837 }
1838
1839 static void test_validate_uuid(void)
1840 {
1841 MigrateStart args = {
1842 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1843 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
1844 };
1845
1846 do_test_validate_uuid(&args, false);
1847 }
1848
1849 static void test_validate_uuid_error(void)
1850 {
1851 MigrateStart args = {
1852 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1853 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1854 .hide_stderr = true,
1855 };
1856
1857 do_test_validate_uuid(&args, true);
1858 }
1859
1860 static void test_validate_uuid_src_not_set(void)
1861 {
1862 MigrateStart args = {
1863 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1864 .hide_stderr = true,
1865 };
1866
1867 do_test_validate_uuid(&args, false);
1868 }
1869
1870 static void test_validate_uuid_dst_not_set(void)
1871 {
1872 MigrateStart args = {
1873 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1874 .hide_stderr = true,
1875 };
1876
1877 do_test_validate_uuid(&args, false);
1878 }
1879
1880 /*
1881 * The way auto_converge works, we need to do too many passes to
1882 * run this test. Auto_converge logic is only run once every
1883 * three iterations, so:
1884 *
1885 * - 3 iterations without auto_converge enabled
1886 * - 3 iterations with pct = 5
1887 * - 3 iterations with pct = 30
1888 * - 3 iterations with pct = 55
1889 * - 3 iterations with pct = 80
1890 * - 3 iterations with pct = 95 (max(95, 80 + 25))
1891 *
1892 * To make things even worse, we need to run the initial stage at
1893 * 3MB/s so we enter autoconverge even when host is (over)loaded.
1894 */
1895 static void test_migrate_auto_converge(void)
1896 {
1897 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1898 MigrateStart args = {};
1899 QTestState *from, *to;
1900 int64_t percentage;
1901
1902 /*
1903 * We want the test to be stable and as fast as possible.
1904 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1905 * so we need to decrease a bandwidth.
1906 */
1907 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
1908
1909 if (test_migrate_start(&from, &to, uri, &args)) {
1910 return;
1911 }
1912
1913 migrate_set_capability(from, "auto-converge", true);
1914 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1915 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1916 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1917
1918 /*
1919 * Set the initial parameters so that the migration could not converge
1920 * without throttling.
1921 */
1922 migrate_ensure_non_converge(from);
1923
1924 /* To check remaining size after precopy */
1925 migrate_set_capability(from, "pause-before-switchover", true);
1926
1927 /* Wait for the first serial output from the source */
1928 wait_for_serial("src_serial");
1929
1930 migrate_qmp(from, uri, "{}");
1931
1932 /* Wait for throttling begins */
1933 percentage = 0;
1934 do {
1935 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1936 if (percentage != 0) {
1937 break;
1938 }
1939 usleep(20);
1940 g_assert_false(got_stop);
1941 } while (true);
1942 /* The first percentage of throttling should be at least init_pct */
1943 g_assert_cmpint(percentage, >=, init_pct);
1944 /* Now, when we tested that throttling works, let it converge */
1945 migrate_ensure_converge(from);
1946
1947 /*
1948 * Wait for pre-switchover status to check last throttle percentage
1949 * and remaining. These values will be zeroed later
1950 */
1951 wait_for_migration_status(from, "pre-switchover", NULL);
1952
1953 /* The final percentage of throttling shouldn't be greater than max_pct */
1954 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1955 g_assert_cmpint(percentage, <=, max_pct);
1956 migrate_continue(from, "pre-switchover");
1957
1958 qtest_qmp_eventwait(to, "RESUME");
1959
1960 wait_for_serial("dest_serial");
1961 wait_for_migration_complete(from);
1962
1963 test_migrate_end(from, to, true);
1964 }
1965
1966 static void *
1967 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
1968 QTestState *to,
1969 const char *method)
1970 {
1971 migrate_set_parameter_int(from, "multifd-channels", 16);
1972 migrate_set_parameter_int(to, "multifd-channels", 16);
1973
1974 migrate_set_parameter_str(from, "multifd-compression", method);
1975 migrate_set_parameter_str(to, "multifd-compression", method);
1976
1977 migrate_set_capability(from, "multifd", true);
1978 migrate_set_capability(to, "multifd", true);
1979
1980 /* Start incoming migration from the 1st socket */
1981 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1982 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1983
1984 return NULL;
1985 }
1986
1987 static void *
1988 test_migrate_precopy_tcp_multifd_start(QTestState *from,
1989 QTestState *to)
1990 {
1991 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1992 }
1993
1994 static void *
1995 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
1996 QTestState *to)
1997 {
1998 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
1999 }
2000
2001 #ifdef CONFIG_ZSTD
2002 static void *
2003 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2004 QTestState *to)
2005 {
2006 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2007 }
2008 #endif /* CONFIG_ZSTD */
2009
2010 static void test_multifd_tcp_none(void)
2011 {
2012 MigrateCommon args = {
2013 .listen_uri = "defer",
2014 .start_hook = test_migrate_precopy_tcp_multifd_start,
2015 };
2016 test_precopy_common(&args);
2017 }
2018
2019 static void test_multifd_tcp_zlib(void)
2020 {
2021 MigrateCommon args = {
2022 .listen_uri = "defer",
2023 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2024 };
2025 test_precopy_common(&args);
2026 }
2027
2028 #ifdef CONFIG_ZSTD
2029 static void test_multifd_tcp_zstd(void)
2030 {
2031 MigrateCommon args = {
2032 .listen_uri = "defer",
2033 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2034 };
2035 test_precopy_common(&args);
2036 }
2037 #endif
2038
2039 #ifdef CONFIG_GNUTLS
2040 static void *
2041 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2042 QTestState *to)
2043 {
2044 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2045 return test_migrate_tls_psk_start_match(from, to);
2046 }
2047
2048 static void *
2049 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2050 QTestState *to)
2051 {
2052 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2053 return test_migrate_tls_psk_start_mismatch(from, to);
2054 }
2055
2056 #ifdef CONFIG_TASN1
2057 static void *
2058 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2059 QTestState *to)
2060 {
2061 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2062 return test_migrate_tls_x509_start_default_host(from, to);
2063 }
2064
2065 static void *
2066 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2067 QTestState *to)
2068 {
2069 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2070 return test_migrate_tls_x509_start_override_host(from, to);
2071 }
2072
2073 static void *
2074 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2075 QTestState *to)
2076 {
2077 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2078 return test_migrate_tls_x509_start_mismatch_host(from, to);
2079 }
2080
2081 static void *
2082 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2083 QTestState *to)
2084 {
2085 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2086 return test_migrate_tls_x509_start_allow_anon_client(from, to);
2087 }
2088
2089 static void *
2090 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2091 QTestState *to)
2092 {
2093 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2094 return test_migrate_tls_x509_start_reject_anon_client(from, to);
2095 }
2096 #endif /* CONFIG_TASN1 */
2097
2098 static void test_multifd_tcp_tls_psk_match(void)
2099 {
2100 MigrateCommon args = {
2101 .listen_uri = "defer",
2102 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2103 .finish_hook = test_migrate_tls_psk_finish,
2104 };
2105 test_precopy_common(&args);
2106 }
2107
2108 static void test_multifd_tcp_tls_psk_mismatch(void)
2109 {
2110 MigrateCommon args = {
2111 .start = {
2112 .hide_stderr = true,
2113 },
2114 .listen_uri = "defer",
2115 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2116 .finish_hook = test_migrate_tls_psk_finish,
2117 .result = MIG_TEST_FAIL,
2118 };
2119 test_precopy_common(&args);
2120 }
2121
2122 #ifdef CONFIG_TASN1
2123 static void test_multifd_tcp_tls_x509_default_host(void)
2124 {
2125 MigrateCommon args = {
2126 .listen_uri = "defer",
2127 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2128 .finish_hook = test_migrate_tls_x509_finish,
2129 };
2130 test_precopy_common(&args);
2131 }
2132
2133 static void test_multifd_tcp_tls_x509_override_host(void)
2134 {
2135 MigrateCommon args = {
2136 .listen_uri = "defer",
2137 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2138 .finish_hook = test_migrate_tls_x509_finish,
2139 };
2140 test_precopy_common(&args);
2141 }
2142
2143 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2144 {
2145 /*
2146 * This has different behaviour to the non-multifd case.
2147 *
2148 * In non-multifd case when client aborts due to mismatched
2149 * cert host, the server has already started trying to load
2150 * migration state, and so it exits with I/O failure.
2151 *
2152 * In multifd case when client aborts due to mismatched
2153 * cert host, the server is still waiting for the other
2154 * multifd connections to arrive so hasn't started trying
2155 * to load migration state, and thus just aborts the migration
2156 * without exiting.
2157 */
2158 MigrateCommon args = {
2159 .start = {
2160 .hide_stderr = true,
2161 },
2162 .listen_uri = "defer",
2163 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2164 .finish_hook = test_migrate_tls_x509_finish,
2165 .result = MIG_TEST_FAIL,
2166 };
2167 test_precopy_common(&args);
2168 }
2169
2170 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2171 {
2172 MigrateCommon args = {
2173 .listen_uri = "defer",
2174 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2175 .finish_hook = test_migrate_tls_x509_finish,
2176 };
2177 test_precopy_common(&args);
2178 }
2179
2180 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2181 {
2182 MigrateCommon args = {
2183 .start = {
2184 .hide_stderr = true,
2185 },
2186 .listen_uri = "defer",
2187 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2188 .finish_hook = test_migrate_tls_x509_finish,
2189 .result = MIG_TEST_FAIL,
2190 };
2191 test_precopy_common(&args);
2192 }
2193 #endif /* CONFIG_TASN1 */
2194 #endif /* CONFIG_GNUTLS */
2195
2196 /*
2197 * This test does:
2198 * source target
2199 * migrate_incoming
2200 * migrate
2201 * migrate_cancel
2202 * launch another target
2203 * migrate
2204 *
2205 * And see that it works
2206 */
2207 static void test_multifd_tcp_cancel(void)
2208 {
2209 MigrateStart args = {
2210 .hide_stderr = true,
2211 };
2212 QTestState *from, *to, *to2;
2213 g_autofree char *uri = NULL;
2214
2215 if (test_migrate_start(&from, &to, "defer", &args)) {
2216 return;
2217 }
2218
2219 migrate_ensure_non_converge(from);
2220
2221 migrate_set_parameter_int(from, "multifd-channels", 16);
2222 migrate_set_parameter_int(to, "multifd-channels", 16);
2223
2224 migrate_set_capability(from, "multifd", true);
2225 migrate_set_capability(to, "multifd", true);
2226
2227 /* Start incoming migration from the 1st socket */
2228 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
2229 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2230
2231 /* Wait for the first serial output from the source */
2232 wait_for_serial("src_serial");
2233
2234 uri = migrate_get_socket_address(to, "socket-address");
2235
2236 migrate_qmp(from, uri, "{}");
2237
2238 wait_for_migration_pass(from);
2239
2240 migrate_cancel(from);
2241
2242 /* Make sure QEMU process "to" exited */
2243 qtest_set_expected_status(to, EXIT_FAILURE);
2244 qtest_wait_qemu(to);
2245
2246 args = (MigrateStart){
2247 .only_target = true,
2248 };
2249
2250 if (test_migrate_start(&from, &to2, "defer", &args)) {
2251 return;
2252 }
2253
2254 migrate_set_parameter_int(to2, "multifd-channels", 16);
2255
2256 migrate_set_capability(to2, "multifd", true);
2257
2258 /* Start incoming migration from the 1st socket */
2259 qtest_qmp_assert_success(to2, "{ 'execute': 'migrate-incoming',"
2260 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2261
2262 g_free(uri);
2263 uri = migrate_get_socket_address(to2, "socket-address");
2264
2265 wait_for_migration_status(from, "cancelled", NULL);
2266
2267 migrate_ensure_converge(from);
2268
2269 migrate_qmp(from, uri, "{}");
2270
2271 wait_for_migration_pass(from);
2272
2273 if (!got_stop) {
2274 qtest_qmp_eventwait(from, "STOP");
2275 }
2276 qtest_qmp_eventwait(to2, "RESUME");
2277
2278 wait_for_serial("dest_serial");
2279 wait_for_migration_complete(from);
2280 test_migrate_end(from, to2, true);
2281 }
2282
2283 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2284 {
2285 qtest_qmp_assert_success(who,
2286 "{ 'execute': 'calc-dirty-rate',"
2287 "'arguments': { "
2288 "'calc-time': %" PRIu64 ","
2289 "'mode': 'dirty-ring' }}",
2290 calc_time);
2291 }
2292
2293 static QDict *query_dirty_rate(QTestState *who)
2294 {
2295 return qtest_qmp_assert_success_ref(who,
2296 "{ 'execute': 'query-dirty-rate' }");
2297 }
2298
2299 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
2300 {
2301 qtest_qmp_assert_success(who,
2302 "{ 'execute': 'set-vcpu-dirty-limit',"
2303 "'arguments': { "
2304 "'dirty-rate': %" PRIu64 " } }",
2305 dirtyrate);
2306 }
2307
2308 static void cancel_vcpu_dirty_limit(QTestState *who)
2309 {
2310 qtest_qmp_assert_success(who,
2311 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
2312 }
2313
2314 static QDict *query_vcpu_dirty_limit(QTestState *who)
2315 {
2316 QDict *rsp;
2317
2318 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
2319 g_assert(!qdict_haskey(rsp, "error"));
2320 g_assert(qdict_haskey(rsp, "return"));
2321
2322 return rsp;
2323 }
2324
2325 static bool calc_dirtyrate_ready(QTestState *who)
2326 {
2327 QDict *rsp_return;
2328 gchar *status;
2329
2330 rsp_return = query_dirty_rate(who);
2331 g_assert(rsp_return);
2332
2333 status = g_strdup(qdict_get_str(rsp_return, "status"));
2334 g_assert(status);
2335
2336 return g_strcmp0(status, "measuring");
2337 }
2338
2339 static void wait_for_calc_dirtyrate_complete(QTestState *who,
2340 int64_t time_s)
2341 {
2342 int max_try_count = 10000;
2343 usleep(time_s * 1000000);
2344
2345 while (!calc_dirtyrate_ready(who) && max_try_count--) {
2346 usleep(1000);
2347 }
2348
2349 /*
2350 * Set the timeout with 10 s(max_try_count * 1000us),
2351 * if dirtyrate measurement not complete, fail test.
2352 */
2353 g_assert_cmpint(max_try_count, !=, 0);
2354 }
2355
2356 static int64_t get_dirty_rate(QTestState *who)
2357 {
2358 QDict *rsp_return;
2359 gchar *status;
2360 QList *rates;
2361 const QListEntry *entry;
2362 QDict *rate;
2363 int64_t dirtyrate;
2364
2365 rsp_return = query_dirty_rate(who);
2366 g_assert(rsp_return);
2367
2368 status = g_strdup(qdict_get_str(rsp_return, "status"));
2369 g_assert(status);
2370 g_assert_cmpstr(status, ==, "measured");
2371
2372 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
2373 g_assert(rates && !qlist_empty(rates));
2374
2375 entry = qlist_first(rates);
2376 g_assert(entry);
2377
2378 rate = qobject_to(QDict, qlist_entry_obj(entry));
2379 g_assert(rate);
2380
2381 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
2382
2383 qobject_unref(rsp_return);
2384 return dirtyrate;
2385 }
2386
2387 static int64_t get_limit_rate(QTestState *who)
2388 {
2389 QDict *rsp_return;
2390 QList *rates;
2391 const QListEntry *entry;
2392 QDict *rate;
2393 int64_t dirtyrate;
2394
2395 rsp_return = query_vcpu_dirty_limit(who);
2396 g_assert(rsp_return);
2397
2398 rates = qdict_get_qlist(rsp_return, "return");
2399 g_assert(rates && !qlist_empty(rates));
2400
2401 entry = qlist_first(rates);
2402 g_assert(entry);
2403
2404 rate = qobject_to(QDict, qlist_entry_obj(entry));
2405 g_assert(rate);
2406
2407 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
2408
2409 qobject_unref(rsp_return);
2410 return dirtyrate;
2411 }
2412
2413 static QTestState *dirtylimit_start_vm(void)
2414 {
2415 QTestState *vm = NULL;
2416 g_autofree gchar *cmd = NULL;
2417 const char *arch = qtest_get_arch();
2418 g_autofree char *bootpath = NULL;
2419
2420 assert((strcmp(arch, "x86_64") == 0));
2421 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
2422 assert(sizeof(x86_bootsect) == 512);
2423 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
2424
2425 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
2426 "-name dirtylimit-test,debug-threads=on "
2427 "-m 150M -smp 1 "
2428 "-serial file:%s/vm_serial "
2429 "-drive file=%s,format=raw ",
2430 tmpfs, bootpath);
2431
2432 vm = qtest_init(cmd);
2433 return vm;
2434 }
2435
2436 static void dirtylimit_stop_vm(QTestState *vm)
2437 {
2438 qtest_quit(vm);
2439 cleanup("bootsect");
2440 cleanup("vm_serial");
2441 }
2442
2443 static void test_vcpu_dirty_limit(void)
2444 {
2445 QTestState *vm;
2446 int64_t origin_rate;
2447 int64_t quota_rate;
2448 int64_t rate ;
2449 int max_try_count = 20;
2450 int hit = 0;
2451
2452 /* Start vm for vcpu dirtylimit test */
2453 vm = dirtylimit_start_vm();
2454
2455 /* Wait for the first serial output from the vm*/
2456 wait_for_serial("vm_serial");
2457
2458 /* Do dirtyrate measurement with calc time equals 1s */
2459 calc_dirty_rate(vm, 1);
2460
2461 /* Sleep calc time and wait for calc dirtyrate complete */
2462 wait_for_calc_dirtyrate_complete(vm, 1);
2463
2464 /* Query original dirty page rate */
2465 origin_rate = get_dirty_rate(vm);
2466
2467 /* VM booted from bootsect should dirty memory steadily */
2468 assert(origin_rate != 0);
2469
2470 /* Setup quota dirty page rate at half of origin */
2471 quota_rate = origin_rate / 2;
2472
2473 /* Set dirtylimit */
2474 dirtylimit_set_all(vm, quota_rate);
2475
2476 /*
2477 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
2478 * works literally
2479 */
2480 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
2481
2482 /* Sleep a bit to check if it take effect */
2483 usleep(2000000);
2484
2485 /*
2486 * Check if dirtylimit take effect realistically, set the
2487 * timeout with 20 s(max_try_count * 1s), if dirtylimit
2488 * doesn't take effect, fail test.
2489 */
2490 while (--max_try_count) {
2491 calc_dirty_rate(vm, 1);
2492 wait_for_calc_dirtyrate_complete(vm, 1);
2493 rate = get_dirty_rate(vm);
2494
2495 /*
2496 * Assume hitting if current rate is less
2497 * than quota rate (within accepting error)
2498 */
2499 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2500 hit = 1;
2501 break;
2502 }
2503 }
2504
2505 g_assert_cmpint(hit, ==, 1);
2506
2507 hit = 0;
2508 max_try_count = 20;
2509
2510 /* Check if dirtylimit cancellation take effect */
2511 cancel_vcpu_dirty_limit(vm);
2512 while (--max_try_count) {
2513 calc_dirty_rate(vm, 1);
2514 wait_for_calc_dirtyrate_complete(vm, 1);
2515 rate = get_dirty_rate(vm);
2516
2517 /*
2518 * Assume dirtylimit be canceled if current rate is
2519 * greater than quota rate (within accepting error)
2520 */
2521 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
2522 hit = 1;
2523 break;
2524 }
2525 }
2526
2527 g_assert_cmpint(hit, ==, 1);
2528 dirtylimit_stop_vm(vm);
2529 }
2530
2531 static bool kvm_dirty_ring_supported(void)
2532 {
2533 #if defined(__linux__) && defined(HOST_X86_64)
2534 int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2535
2536 if (kvm_fd < 0) {
2537 return false;
2538 }
2539
2540 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2541 close(kvm_fd);
2542
2543 /* We test with 4096 slots */
2544 if (ret < 4096) {
2545 return false;
2546 }
2547
2548 return true;
2549 #else
2550 return false;
2551 #endif
2552 }
2553
2554 int main(int argc, char **argv)
2555 {
2556 bool has_kvm, has_tcg;
2557 bool has_uffd;
2558 const char *arch;
2559 g_autoptr(GError) err = NULL;
2560 int ret;
2561
2562 g_test_init(&argc, &argv, NULL);
2563
2564 has_kvm = qtest_has_accel("kvm");
2565 has_tcg = qtest_has_accel("tcg");
2566
2567 if (!has_tcg && !has_kvm) {
2568 g_test_skip("No KVM or TCG accelerator available");
2569 return 0;
2570 }
2571
2572 has_uffd = ufd_version_check();
2573 arch = qtest_get_arch();
2574
2575 /*
2576 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2577 * is touchy due to race conditions on dirty bits (especially on PPC for
2578 * some reason)
2579 */
2580 if (g_str_equal(arch, "ppc64") &&
2581 (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2582 g_test_message("Skipping test: kvm_hv not available");
2583 return g_test_run();
2584 }
2585
2586 /*
2587 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2588 * there until the problems are resolved
2589 */
2590 if (g_str_equal(arch, "s390x") && !has_kvm) {
2591 g_test_message("Skipping test: s390x host with KVM is required");
2592 return g_test_run();
2593 }
2594
2595 tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
2596 if (!tmpfs) {
2597 g_test_message("Can't create temporary directory in %s: %s",
2598 g_get_tmp_dir(), err->message);
2599 }
2600 g_assert(tmpfs);
2601
2602 module_call_init(MODULE_INIT_QOM);
2603
2604 if (has_uffd) {
2605 qtest_add_func("/migration/postcopy/plain", test_postcopy);
2606 qtest_add_func("/migration/postcopy/recovery/plain",
2607 test_postcopy_recovery);
2608 qtest_add_func("/migration/postcopy/preempt/plain", test_postcopy_preempt);
2609 qtest_add_func("/migration/postcopy/preempt/recovery/plain",
2610 test_postcopy_preempt_recovery);
2611 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2612 qtest_add_func("/migration/postcopy/compress/plain",
2613 test_postcopy_compress);
2614 qtest_add_func("/migration/postcopy/recovery/compress/plain",
2615 test_postcopy_recovery_compress);
2616 }
2617 }
2618
2619 qtest_add_func("/migration/bad_dest", test_baddest);
2620 qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2621 qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2622 /*
2623 * Compression fails from time to time.
2624 * Put test here but don't enable it until everything is fixed.
2625 */
2626 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2627 qtest_add_func("/migration/precopy/unix/compress/wait",
2628 test_precopy_unix_compress);
2629 qtest_add_func("/migration/precopy/unix/compress/nowait",
2630 test_precopy_unix_compress_nowait);
2631 }
2632 #ifdef CONFIG_GNUTLS
2633 qtest_add_func("/migration/precopy/unix/tls/psk",
2634 test_precopy_unix_tls_psk);
2635
2636 if (has_uffd) {
2637 /*
2638 * NOTE: psk test is enough for postcopy, as other types of TLS
2639 * channels are tested under precopy. Here what we want to test is the
2640 * general postcopy path that has TLS channel enabled.
2641 */
2642 qtest_add_func("/migration/postcopy/tls/psk", test_postcopy_tls_psk);
2643 qtest_add_func("/migration/postcopy/recovery/tls/psk",
2644 test_postcopy_recovery_tls_psk);
2645 qtest_add_func("/migration/postcopy/preempt/tls/psk",
2646 test_postcopy_preempt_tls_psk);
2647 qtest_add_func("/migration/postcopy/preempt/recovery/tls/psk",
2648 test_postcopy_preempt_all);
2649 }
2650 #ifdef CONFIG_TASN1
2651 qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2652 test_precopy_unix_tls_x509_default_host);
2653 qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2654 test_precopy_unix_tls_x509_override_host);
2655 #endif /* CONFIG_TASN1 */
2656 #endif /* CONFIG_GNUTLS */
2657
2658 qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2659 #ifdef CONFIG_GNUTLS
2660 qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2661 test_precopy_tcp_tls_psk_match);
2662 qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2663 test_precopy_tcp_tls_psk_mismatch);
2664 #ifdef CONFIG_TASN1
2665 qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2666 test_precopy_tcp_tls_x509_default_host);
2667 qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2668 test_precopy_tcp_tls_x509_override_host);
2669 qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2670 test_precopy_tcp_tls_x509_mismatch_host);
2671 qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2672 test_precopy_tcp_tls_x509_friendly_client);
2673 qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2674 test_precopy_tcp_tls_x509_hostile_client);
2675 qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2676 test_precopy_tcp_tls_x509_allow_anon_client);
2677 qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2678 test_precopy_tcp_tls_x509_reject_anon_client);
2679 #endif /* CONFIG_TASN1 */
2680 #endif /* CONFIG_GNUTLS */
2681
2682 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2683 #ifndef _WIN32
2684 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2685 #endif
2686 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2687 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2688 qtest_add_func("/migration/validate_uuid_src_not_set",
2689 test_validate_uuid_src_not_set);
2690 qtest_add_func("/migration/validate_uuid_dst_not_set",
2691 test_validate_uuid_dst_not_set);
2692 /*
2693 * See explanation why this test is slow on function definition
2694 */
2695 if (g_test_slow()) {
2696 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2697 }
2698 qtest_add_func("/migration/multifd/tcp/plain/none",
2699 test_multifd_tcp_none);
2700 /*
2701 * This test is flaky and sometimes fails in CI and otherwise:
2702 * don't run unless user opts in via environment variable.
2703 */
2704 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
2705 qtest_add_func("/migration/multifd/tcp/plain/cancel",
2706 test_multifd_tcp_cancel);
2707 }
2708 qtest_add_func("/migration/multifd/tcp/plain/zlib",
2709 test_multifd_tcp_zlib);
2710 #ifdef CONFIG_ZSTD
2711 qtest_add_func("/migration/multifd/tcp/plain/zstd",
2712 test_multifd_tcp_zstd);
2713 #endif
2714 #ifdef CONFIG_GNUTLS
2715 qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2716 test_multifd_tcp_tls_psk_match);
2717 qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2718 test_multifd_tcp_tls_psk_mismatch);
2719 #ifdef CONFIG_TASN1
2720 qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2721 test_multifd_tcp_tls_x509_default_host);
2722 qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2723 test_multifd_tcp_tls_x509_override_host);
2724 qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2725 test_multifd_tcp_tls_x509_mismatch_host);
2726 qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2727 test_multifd_tcp_tls_x509_allow_anon_client);
2728 qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2729 test_multifd_tcp_tls_x509_reject_anon_client);
2730 #endif /* CONFIG_TASN1 */
2731 #endif /* CONFIG_GNUTLS */
2732
2733 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
2734 qtest_add_func("/migration/dirty_ring",
2735 test_precopy_unix_dirty_ring);
2736 qtest_add_func("/migration/vcpu_dirty_limit",
2737 test_vcpu_dirty_limit);
2738 }
2739
2740 ret = g_test_run();
2741
2742 g_assert_cmpint(ret, ==, 0);
2743
2744 ret = rmdir(tmpfs);
2745 if (ret != 0) {
2746 g_test_message("unable to rmdir: path (%s): %s",
2747 tmpfs, strerror(errno));
2748 }
2749 g_free(tmpfs);
2750
2751 return ret;
2752 }