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