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