xref: /qemu/tests/qtest/migration-test.c (revision 1f2355f5)
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/qmp/qdict.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "crypto/tlscredspsk.h"
23 #include "qapi/qmp/qlist.h"
24 
25 #include "migration-helpers.h"
26 #include "tests/migration/migration-test.h"
27 #ifdef CONFIG_GNUTLS
28 # include "tests/unit/crypto-tls-psk-helpers.h"
29 # ifdef CONFIG_TASN1
30 #  include "tests/unit/crypto-tls-x509-helpers.h"
31 # endif /* CONFIG_TASN1 */
32 #endif /* CONFIG_GNUTLS */
33 
34 /* For dirty ring test; so far only x86_64 is supported */
35 #if defined(__linux__) && defined(HOST_X86_64)
36 #include "linux/kvm.h"
37 #endif
38 
39 unsigned start_address;
40 unsigned end_address;
41 static bool uffd_feature_thread_id;
42 static QTestMigrationState src_state;
43 static QTestMigrationState dst_state;
44 
45 /*
46  * An initial 3 MB offset is used as that corresponds
47  * to ~1 sec of data transfer with our bandwidth setting.
48  */
49 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
50 /*
51  * A further 1k is added to ensure we're not a multiple
52  * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
53  * from the migration guest workload.
54  */
55 #define MAGIC_OFFSET_SHUFFLE 1024
56 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
57 #define MAGIC_MARKER 0xFEED12345678CAFEULL
58 
59 /*
60  * Dirtylimit stop working if dirty page rate error
61  * value less than DIRTYLIMIT_TOLERANCE_RANGE
62  */
63 #define DIRTYLIMIT_TOLERANCE_RANGE  25  /* MB/s */
64 
65 #define ANALYZE_SCRIPT "scripts/analyze-migration.py"
66 
67 #define QEMU_VM_FILE_MAGIC 0x5145564d
68 #define FILE_TEST_FILENAME "migfile"
69 #define FILE_TEST_OFFSET 0x1000
70 #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
71 #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST"
72 
73 #if defined(__linux__)
74 #include <sys/syscall.h>
75 #include <sys/vfs.h>
76 #endif
77 
78 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
79 #include <sys/eventfd.h>
80 #include <sys/ioctl.h>
81 #include "qemu/userfaultfd.h"
82 
83 static bool ufd_version_check(void)
84 {
85     struct uffdio_api api_struct;
86     uint64_t ioctl_mask;
87 
88     int ufd = uffd_open(O_CLOEXEC);
89 
90     if (ufd == -1) {
91         g_test_message("Skipping test: userfaultfd not available");
92         return false;
93     }
94 
95     api_struct.api = UFFD_API;
96     api_struct.features = 0;
97     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
98         g_test_message("Skipping test: UFFDIO_API failed");
99         return false;
100     }
101     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
102 
103     ioctl_mask = 1ULL << _UFFDIO_REGISTER |
104                  1ULL << _UFFDIO_UNREGISTER;
105     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
106         g_test_message("Skipping test: Missing userfault feature");
107         return false;
108     }
109 
110     return true;
111 }
112 
113 #else
114 static bool ufd_version_check(void)
115 {
116     g_test_message("Skipping test: Userfault not available (builtdtime)");
117     return false;
118 }
119 
120 #endif
121 
122 static char *tmpfs;
123 static char *bootpath;
124 
125 /* The boot file modifies memory area in [start_address, end_address)
126  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
127  */
128 #include "tests/migration/i386/a-b-bootblock.h"
129 #include "tests/migration/aarch64/a-b-kernel.h"
130 #include "tests/migration/s390x/a-b-bios.h"
131 
132 static void bootfile_create(char *dir, bool suspend_me)
133 {
134     const char *arch = qtest_get_arch();
135     unsigned char *content;
136     size_t len;
137 
138     bootpath = g_strdup_printf("%s/bootsect", dir);
139     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
140         /* the assembled x86 boot sector should be exactly one sector large */
141         g_assert(sizeof(x86_bootsect) == 512);
142         x86_bootsect[SYM_suspend_me - SYM_start] = suspend_me;
143         content = x86_bootsect;
144         len = sizeof(x86_bootsect);
145     } else if (g_str_equal(arch, "s390x")) {
146         content = s390x_elf;
147         len = sizeof(s390x_elf);
148     } else if (strcmp(arch, "ppc64") == 0) {
149         /*
150          * sane architectures can be programmed at the boot prompt
151          */
152         return;
153     } else if (strcmp(arch, "aarch64") == 0) {
154         content = aarch64_kernel;
155         len = sizeof(aarch64_kernel);
156         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
157     } else {
158         g_assert_not_reached();
159     }
160 
161     FILE *bootfile = fopen(bootpath, "wb");
162 
163     g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
164     fclose(bootfile);
165 }
166 
167 static void bootfile_delete(void)
168 {
169     unlink(bootpath);
170     g_free(bootpath);
171     bootpath = NULL;
172 }
173 
174 /*
175  * Wait for some output in the serial output file,
176  * we get an 'A' followed by an endless string of 'B's
177  * but on the destination we won't have the A (unless we enabled suspend/resume)
178  */
179 static void wait_for_serial(const char *side)
180 {
181     g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
182     FILE *serialfile = fopen(serialpath, "r");
183     const char *arch = qtest_get_arch();
184     int started = (strcmp(side, "src_serial") == 0 &&
185                    strcmp(arch, "ppc64") == 0) ? 0 : 1;
186 
187     do {
188         int readvalue = fgetc(serialfile);
189 
190         if (!started) {
191             /* SLOF prints its banner before starting test,
192              * to ignore it, mark the start of the test with '_',
193              * ignore all characters until this marker
194              */
195             switch (readvalue) {
196             case '_':
197                 started = 1;
198                 break;
199             case EOF:
200                 fseek(serialfile, 0, SEEK_SET);
201                 usleep(1000);
202                 break;
203             }
204             continue;
205         }
206         switch (readvalue) {
207         case 'A':
208             /* Fine */
209             break;
210 
211         case 'B':
212             /* It's alive! */
213             fclose(serialfile);
214             return;
215 
216         case EOF:
217             started = (strcmp(side, "src_serial") == 0 &&
218                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
219             fseek(serialfile, 0, SEEK_SET);
220             usleep(1000);
221             break;
222 
223         default:
224             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
225             g_assert_not_reached();
226         }
227     } while (true);
228 }
229 
230 static void wait_for_stop(QTestState *who, QTestMigrationState *state)
231 {
232     if (!state->stop_seen) {
233         qtest_qmp_eventwait(who, "STOP");
234     }
235 }
236 
237 static void wait_for_resume(QTestState *who, QTestMigrationState *state)
238 {
239     if (!state->resume_seen) {
240         qtest_qmp_eventwait(who, "RESUME");
241     }
242 }
243 
244 static void wait_for_suspend(QTestState *who, QTestMigrationState *state)
245 {
246     if (state->suspend_me && !state->suspend_seen) {
247         qtest_qmp_eventwait(who, "SUSPEND");
248     }
249 }
250 
251 /*
252  * It's tricky to use qemu's migration event capability with qtest,
253  * events suddenly appearing confuse the qmp()/hmp() responses.
254  */
255 
256 static int64_t read_ram_property_int(QTestState *who, const char *property)
257 {
258     QDict *rsp_return, *rsp_ram;
259     int64_t result;
260 
261     rsp_return = migrate_query_not_failed(who);
262     if (!qdict_haskey(rsp_return, "ram")) {
263         /* Still in setup */
264         result = 0;
265     } else {
266         rsp_ram = qdict_get_qdict(rsp_return, "ram");
267         result = qdict_get_try_int(rsp_ram, property, 0);
268     }
269     qobject_unref(rsp_return);
270     return result;
271 }
272 
273 static int64_t read_migrate_property_int(QTestState *who, const char *property)
274 {
275     QDict *rsp_return;
276     int64_t result;
277 
278     rsp_return = migrate_query_not_failed(who);
279     result = qdict_get_try_int(rsp_return, property, 0);
280     qobject_unref(rsp_return);
281     return result;
282 }
283 
284 static uint64_t get_migration_pass(QTestState *who)
285 {
286     return read_ram_property_int(who, "dirty-sync-count");
287 }
288 
289 static void read_blocktime(QTestState *who)
290 {
291     QDict *rsp_return;
292 
293     rsp_return = migrate_query_not_failed(who);
294     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
295     qobject_unref(rsp_return);
296 }
297 
298 /*
299  * Wait for two changes in the migration pass count, but bail if we stop.
300  */
301 static void wait_for_migration_pass(QTestState *who)
302 {
303     uint64_t pass, prev_pass = 0, changes = 0;
304 
305     while (changes < 2 && !src_state.stop_seen && !src_state.suspend_seen) {
306         usleep(1000);
307         pass = get_migration_pass(who);
308         changes += (pass != prev_pass);
309         prev_pass = pass;
310     }
311 }
312 
313 static void check_guests_ram(QTestState *who)
314 {
315     /* Our ASM test will have been incrementing one byte from each page from
316      * start_address to < end_address in order. This gives us a constraint
317      * that any page's byte should be equal or less than the previous pages
318      * byte (mod 256); and they should all be equal except for one transition
319      * at the point where we meet the incrementer. (We're running this with
320      * the guest stopped).
321      */
322     unsigned address;
323     uint8_t first_byte;
324     uint8_t last_byte;
325     bool hit_edge = false;
326     int bad = 0;
327 
328     qtest_memread(who, start_address, &first_byte, 1);
329     last_byte = first_byte;
330 
331     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
332          address += TEST_MEM_PAGE_SIZE)
333     {
334         uint8_t b;
335         qtest_memread(who, address, &b, 1);
336         if (b != last_byte) {
337             if (((b + 1) % 256) == last_byte && !hit_edge) {
338                 /* This is OK, the guest stopped at the point of
339                  * incrementing the previous page but didn't get
340                  * to us yet.
341                  */
342                 hit_edge = true;
343                 last_byte = b;
344             } else {
345                 bad++;
346                 if (bad <= 10) {
347                     fprintf(stderr, "Memory content inconsistency at %x"
348                             " first_byte = %x last_byte = %x current = %x"
349                             " hit_edge = %x\n",
350                             address, first_byte, last_byte, b, hit_edge);
351                 }
352             }
353         }
354     }
355     if (bad >= 10) {
356         fprintf(stderr, "and in another %d pages", bad - 10);
357     }
358     g_assert(bad == 0);
359 }
360 
361 static void cleanup(const char *filename)
362 {
363     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
364 
365     unlink(path);
366 }
367 
368 static long long migrate_get_parameter_int(QTestState *who,
369                                            const char *parameter)
370 {
371     QDict *rsp;
372     long long result;
373 
374     rsp = qtest_qmp_assert_success_ref(
375         who, "{ 'execute': 'query-migrate-parameters' }");
376     result = qdict_get_int(rsp, parameter);
377     qobject_unref(rsp);
378     return result;
379 }
380 
381 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
382                                         long long value)
383 {
384     long long result;
385 
386     result = migrate_get_parameter_int(who, parameter);
387     g_assert_cmpint(result, ==, value);
388 }
389 
390 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
391                                       long long value)
392 {
393     qtest_qmp_assert_success(who,
394                              "{ 'execute': 'migrate-set-parameters',"
395                              "'arguments': { %s: %lld } }",
396                              parameter, value);
397     migrate_check_parameter_int(who, parameter, value);
398 }
399 
400 static char *migrate_get_parameter_str(QTestState *who,
401                                        const char *parameter)
402 {
403     QDict *rsp;
404     char *result;
405 
406     rsp = qtest_qmp_assert_success_ref(
407         who, "{ 'execute': 'query-migrate-parameters' }");
408     result = g_strdup(qdict_get_str(rsp, parameter));
409     qobject_unref(rsp);
410     return result;
411 }
412 
413 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
414                                         const char *value)
415 {
416     g_autofree char *result = migrate_get_parameter_str(who, parameter);
417     g_assert_cmpstr(result, ==, value);
418 }
419 
420 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
421                                       const char *value)
422 {
423     qtest_qmp_assert_success(who,
424                              "{ 'execute': 'migrate-set-parameters',"
425                              "'arguments': { %s: %s } }",
426                              parameter, value);
427     migrate_check_parameter_str(who, parameter, value);
428 }
429 
430 static long long migrate_get_parameter_bool(QTestState *who,
431                                            const char *parameter)
432 {
433     QDict *rsp;
434     int result;
435 
436     rsp = qtest_qmp_assert_success_ref(
437         who, "{ 'execute': 'query-migrate-parameters' }");
438     result = qdict_get_bool(rsp, parameter);
439     qobject_unref(rsp);
440     return !!result;
441 }
442 
443 static void migrate_check_parameter_bool(QTestState *who, const char *parameter,
444                                         int value)
445 {
446     int result;
447 
448     result = migrate_get_parameter_bool(who, parameter);
449     g_assert_cmpint(result, ==, value);
450 }
451 
452 static void migrate_set_parameter_bool(QTestState *who, const char *parameter,
453                                       int value)
454 {
455     qtest_qmp_assert_success(who,
456                              "{ 'execute': 'migrate-set-parameters',"
457                              "'arguments': { %s: %i } }",
458                              parameter, value);
459     migrate_check_parameter_bool(who, parameter, value);
460 }
461 
462 static void migrate_ensure_non_converge(QTestState *who)
463 {
464     /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
465     migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
466     migrate_set_parameter_int(who, "downtime-limit", 1);
467 }
468 
469 static void migrate_ensure_converge(QTestState *who)
470 {
471     /* Should converge with 30s downtime + 1 gbs bandwidth limit */
472     migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
473     migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
474 }
475 
476 /*
477  * Our goal is to ensure that we run a single full migration
478  * iteration, and also dirty memory, ensuring that at least
479  * one further iteration is required.
480  *
481  * We can't directly synchronize with the start of a migration
482  * so we have to apply some tricks monitoring memory that is
483  * transferred.
484  *
485  * Initially we set the migration bandwidth to an insanely
486  * low value, with tiny max downtime too. This basically
487  * guarantees migration will never complete.
488  *
489  * This will result in a test that is unacceptably slow though,
490  * so we can't let the entire migration pass run at this speed.
491  * Our intent is to let it run just long enough that we can
492  * prove data prior to the marker has been transferred *AND*
493  * also prove this transferred data is dirty again.
494  *
495  * Before migration starts, we write a 64-bit magic marker
496  * into a fixed location in the src VM RAM.
497  *
498  * Then watch dst memory until the marker appears. This is
499  * proof that start_address -> MAGIC_OFFSET_BASE has been
500  * transferred.
501  *
502  * Finally we go back to the source and read a byte just
503  * before the marker until we see it flip in value. This
504  * is proof that start_address -> MAGIC_OFFSET_BASE
505  * is now dirty again.
506  *
507  * IOW, we're guaranteed at least a 2nd migration pass
508  * at this point.
509  *
510  * We can now let migration run at full speed to finish
511  * the test
512  */
513 static void migrate_prepare_for_dirty_mem(QTestState *from)
514 {
515     /*
516      * The guest workflow iterates from start_address to
517      * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
518      * bytes.
519      *
520      * IOW, if we write to mem at a point which is NOT
521      * a multiple of TEST_MEM_PAGE_SIZE, our write won't
522      * conflict with the migration workflow.
523      *
524      * We put in a marker here, that we'll use to determine
525      * when the data has been transferred to the dst.
526      */
527     qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
528 }
529 
530 static void migrate_wait_for_dirty_mem(QTestState *from,
531                                        QTestState *to)
532 {
533     uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
534     uint64_t marker_address = start_address + MAGIC_OFFSET;
535     uint8_t watch_byte;
536 
537     /*
538      * Wait for the MAGIC_MARKER to get transferred, as an
539      * indicator that a migration pass has made some known
540      * amount of progress.
541      */
542     do {
543         usleep(1000 * 10);
544     } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
545 
546 
547     /* If suspended, src only iterates once, and watch_byte may never change */
548     if (src_state.suspend_me) {
549         return;
550     }
551 
552     /*
553      * Now ensure that already transferred bytes are
554      * dirty again from the guest workload. Note the
555      * guest byte value will wrap around and by chance
556      * match the original watch_byte. This is harmless
557      * as we'll eventually see a different value if we
558      * keep watching
559      */
560     watch_byte = qtest_readb(from, watch_address);
561     do {
562         usleep(1000 * 10);
563     } while (qtest_readb(from, watch_address) == watch_byte);
564 }
565 
566 
567 static void migrate_pause(QTestState *who)
568 {
569     qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
570 }
571 
572 static void migrate_continue(QTestState *who, const char *state)
573 {
574     qtest_qmp_assert_success(who,
575                              "{ 'execute': 'migrate-continue',"
576                              "  'arguments': { 'state': %s } }",
577                              state);
578 }
579 
580 static void migrate_recover(QTestState *who, const char *uri)
581 {
582     qtest_qmp_assert_success(who,
583                              "{ 'execute': 'migrate-recover', "
584                              "  'id': 'recover-cmd', "
585                              "  'arguments': { 'uri': %s } }",
586                              uri);
587 }
588 
589 static void migrate_cancel(QTestState *who)
590 {
591     qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
592 }
593 
594 static void migrate_postcopy_start(QTestState *from, QTestState *to)
595 {
596     qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
597 
598     wait_for_stop(from, &src_state);
599     qtest_qmp_eventwait(to, "RESUME");
600 }
601 
602 typedef struct {
603     /*
604      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
605      * unconditionally, because it means the user would like to be verbose.
606      */
607     bool hide_stderr;
608     bool use_shmem;
609     /* only launch the target process */
610     bool only_target;
611     /* Use dirty ring if true; dirty logging otherwise */
612     bool use_dirty_ring;
613     const char *opts_source;
614     const char *opts_target;
615     /* suspend the src before migrating to dest. */
616     bool suspend_me;
617 } MigrateStart;
618 
619 /*
620  * A hook that runs after the src and dst QEMUs have been
621  * created, but before the migration is started. This can
622  * be used to set migration parameters and capabilities.
623  *
624  * Returns: NULL, or a pointer to opaque state to be
625  *          later passed to the TestMigrateFinishHook
626  */
627 typedef void * (*TestMigrateStartHook)(QTestState *from,
628                                        QTestState *to);
629 
630 /*
631  * A hook that runs after the migration has finished,
632  * regardless of whether it succeeded or failed, but
633  * before QEMU has terminated (unless it self-terminated
634  * due to migration error)
635  *
636  * @opaque is a pointer to state previously returned
637  * by the TestMigrateStartHook if any, or NULL.
638  */
639 typedef void (*TestMigrateFinishHook)(QTestState *from,
640                                       QTestState *to,
641                                       void *opaque);
642 
643 typedef struct {
644     /* Optional: fine tune start parameters */
645     MigrateStart start;
646 
647     /* Required: the URI for the dst QEMU to listen on */
648     const char *listen_uri;
649 
650     /*
651      * Optional: the URI for the src QEMU to connect to
652      * If NULL, then it will query the dst QEMU for its actual
653      * listening address and use that as the connect address.
654      * This allows for dynamically picking a free TCP port.
655      */
656     const char *connect_uri;
657 
658     /*
659      * Optional: JSON-formatted list of src QEMU URIs. If a port is
660      * defined as '0' in any QDict key a value of '0' will be
661      * automatically converted to the correct destination port.
662      */
663     const char *connect_channels;
664 
665     /* Optional: callback to run at start to set migration parameters */
666     TestMigrateStartHook start_hook;
667     /* Optional: callback to run at finish to cleanup */
668     TestMigrateFinishHook finish_hook;
669 
670     /*
671      * Optional: normally we expect the migration process to complete.
672      *
673      * There can be a variety of reasons and stages in which failure
674      * can happen during tests.
675      *
676      * If a failure is expected to happen at time of establishing
677      * the connection, then MIG_TEST_FAIL will indicate that the dst
678      * QEMU is expected to stay running and accept future migration
679      * connections.
680      *
681      * If a failure is expected to happen while processing the
682      * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
683      * that the dst QEMU is expected to quit with non-zero exit status
684      */
685     enum {
686         /* This test should succeed, the default */
687         MIG_TEST_SUCCEED = 0,
688         /* This test should fail, dest qemu should keep alive */
689         MIG_TEST_FAIL,
690         /* This test should fail, dest qemu should fail with abnormal status */
691         MIG_TEST_FAIL_DEST_QUIT_ERR,
692         /* The QMP command for this migration should fail with an error */
693         MIG_TEST_QMP_ERROR,
694     } result;
695 
696     /*
697      * Optional: set number of migration passes to wait for, if live==true.
698      * If zero, then merely wait for a few MB of dirty data
699      */
700     unsigned int iterations;
701 
702     /*
703      * Optional: whether the guest CPUs should be running during a precopy
704      * migration test.  We used to always run with live but it took much
705      * longer so we reduced live tests to only the ones that have solid
706      * reason to be tested live-only.  For each of the new test cases for
707      * precopy please provide justifications to use live explicitly (please
708      * refer to existing ones with live=true), or use live=off by default.
709      */
710     bool live;
711 
712     /* Postcopy specific fields */
713     void *postcopy_data;
714     bool postcopy_preempt;
715     bool postcopy_recovery_test_fail;
716 } MigrateCommon;
717 
718 static int test_migrate_start(QTestState **from, QTestState **to,
719                               const char *uri, MigrateStart *args)
720 {
721     g_autofree gchar *arch_source = NULL;
722     g_autofree gchar *arch_target = NULL;
723     /* options for source and target */
724     g_autofree gchar *arch_opts = NULL;
725     g_autofree gchar *cmd_source = NULL;
726     g_autofree gchar *cmd_target = NULL;
727     const gchar *ignore_stderr;
728     g_autofree char *shmem_opts = NULL;
729     g_autofree char *shmem_path = NULL;
730     const char *kvm_opts = NULL;
731     const char *arch = qtest_get_arch();
732     const char *memory_size;
733     const char *machine_alias, *machine_opts = "";
734     g_autofree char *machine = NULL;
735 
736     if (args->use_shmem) {
737         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
738             g_test_skip("/dev/shm is not supported");
739             return -1;
740         }
741     }
742 
743     dst_state = (QTestMigrationState) { };
744     src_state = (QTestMigrationState) { };
745     bootfile_create(tmpfs, args->suspend_me);
746     src_state.suspend_me = args->suspend_me;
747 
748     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
749         memory_size = "150M";
750 
751         if (g_str_equal(arch, "i386")) {
752             machine_alias = "pc";
753         } else {
754             machine_alias = "q35";
755         }
756         arch_opts = g_strdup_printf(
757             "-drive if=none,id=d0,file=%s,format=raw "
758             "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath);
759         start_address = X86_TEST_MEM_START;
760         end_address = X86_TEST_MEM_END;
761     } else if (g_str_equal(arch, "s390x")) {
762         memory_size = "128M";
763         machine_alias = "s390-ccw-virtio";
764         arch_opts = g_strdup_printf("-bios %s", bootpath);
765         start_address = S390_TEST_MEM_START;
766         end_address = S390_TEST_MEM_END;
767     } else if (strcmp(arch, "ppc64") == 0) {
768         memory_size = "256M";
769         start_address = PPC_TEST_MEM_START;
770         end_address = PPC_TEST_MEM_END;
771         arch_source = g_strdup_printf("-prom-env 'use-nvramrc?=true' -prom-env "
772                                       "'nvramrc=hex .\" _\" begin %x %x "
773                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
774                                       "until'", end_address, start_address);
775         machine_alias = "pseries";
776         machine_opts = "vsmt=8";
777         arch_opts = g_strdup("-nodefaults");
778     } else if (strcmp(arch, "aarch64") == 0) {
779         memory_size = "150M";
780         machine_alias = "virt";
781         machine_opts = "gic-version=3";
782         arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath);
783         start_address = ARM_TEST_MEM_START;
784         end_address = ARM_TEST_MEM_END;
785     } else {
786         g_assert_not_reached();
787     }
788 
789     if (!getenv("QTEST_LOG") && args->hide_stderr) {
790 #ifndef _WIN32
791         ignore_stderr = "2>/dev/null";
792 #else
793         /*
794          * On Windows the QEMU executable is created via CreateProcess() and
795          * IO redirection does not work, so don't bother adding IO redirection
796          * to the command line.
797          */
798         ignore_stderr = "";
799 #endif
800     } else {
801         ignore_stderr = "";
802     }
803 
804     if (args->use_shmem) {
805         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
806         shmem_opts = g_strdup_printf(
807             "-object memory-backend-file,id=mem0,size=%s"
808             ",mem-path=%s,share=on -numa node,memdev=mem0",
809             memory_size, shmem_path);
810     }
811 
812     if (args->use_dirty_ring) {
813         kvm_opts = ",dirty-ring-size=4096";
814     }
815 
816     machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC,
817                                       QEMU_ENV_DST);
818 
819     g_test_message("Using machine type: %s", machine);
820 
821     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
822                                  "-machine %s,%s "
823                                  "-name source,debug-threads=on "
824                                  "-m %s "
825                                  "-serial file:%s/src_serial "
826                                  "%s %s %s %s %s",
827                                  kvm_opts ? kvm_opts : "",
828                                  machine, machine_opts,
829                                  memory_size, tmpfs,
830                                  arch_opts ? arch_opts : "",
831                                  arch_source ? arch_source : "",
832                                  shmem_opts ? shmem_opts : "",
833                                  args->opts_source ? args->opts_source : "",
834                                  ignore_stderr);
835     if (!args->only_target) {
836         *from = qtest_init_with_env(QEMU_ENV_SRC, cmd_source);
837         qtest_qmp_set_event_callback(*from,
838                                      migrate_watch_for_events,
839                                      &src_state);
840     }
841 
842     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
843                                  "-machine %s,%s "
844                                  "-name target,debug-threads=on "
845                                  "-m %s "
846                                  "-serial file:%s/dest_serial "
847                                  "-incoming %s "
848                                  "%s %s %s %s %s",
849                                  kvm_opts ? kvm_opts : "",
850                                  machine, machine_opts,
851                                  memory_size, tmpfs, uri,
852                                  arch_opts ? arch_opts : "",
853                                  arch_target ? arch_target : "",
854                                  shmem_opts ? shmem_opts : "",
855                                  args->opts_target ? args->opts_target : "",
856                                  ignore_stderr);
857     *to = qtest_init_with_env(QEMU_ENV_DST, cmd_target);
858     qtest_qmp_set_event_callback(*to,
859                                  migrate_watch_for_events,
860                                  &dst_state);
861 
862     /*
863      * Remove shmem file immediately to avoid memory leak in test failed case.
864      * It's valid because QEMU has already opened this file
865      */
866     if (args->use_shmem) {
867         unlink(shmem_path);
868     }
869 
870     return 0;
871 }
872 
873 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
874 {
875     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
876 
877     qtest_quit(from);
878 
879     if (test_dest) {
880         qtest_memread(to, start_address, &dest_byte_a, 1);
881 
882         /* Destination still running, wait for a byte to change */
883         do {
884             qtest_memread(to, start_address, &dest_byte_b, 1);
885             usleep(1000 * 10);
886         } while (dest_byte_a == dest_byte_b);
887 
888         qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
889 
890         /* With it stopped, check nothing changes */
891         qtest_memread(to, start_address, &dest_byte_c, 1);
892         usleep(1000 * 200);
893         qtest_memread(to, start_address, &dest_byte_d, 1);
894         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
895 
896         check_guests_ram(to);
897     }
898 
899     qtest_quit(to);
900 
901     cleanup("migsocket");
902     cleanup("src_serial");
903     cleanup("dest_serial");
904     cleanup(FILE_TEST_FILENAME);
905 }
906 
907 #ifdef CONFIG_GNUTLS
908 struct TestMigrateTLSPSKData {
909     char *workdir;
910     char *workdiralt;
911     char *pskfile;
912     char *pskfilealt;
913 };
914 
915 static void *
916 test_migrate_tls_psk_start_common(QTestState *from,
917                                   QTestState *to,
918                                   bool mismatch)
919 {
920     struct TestMigrateTLSPSKData *data =
921         g_new0(struct TestMigrateTLSPSKData, 1);
922 
923     data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
924     data->pskfile = g_strdup_printf("%s/%s", data->workdir,
925                                     QCRYPTO_TLS_CREDS_PSKFILE);
926     g_mkdir_with_parents(data->workdir, 0700);
927     test_tls_psk_init(data->pskfile);
928 
929     if (mismatch) {
930         data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
931         data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
932                                            QCRYPTO_TLS_CREDS_PSKFILE);
933         g_mkdir_with_parents(data->workdiralt, 0700);
934         test_tls_psk_init_alt(data->pskfilealt);
935     }
936 
937     qtest_qmp_assert_success(from,
938                              "{ 'execute': 'object-add',"
939                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
940                              "                 'id': 'tlscredspsk0',"
941                              "                 'endpoint': 'client',"
942                              "                 'dir': %s,"
943                              "                 'username': 'qemu'} }",
944                              data->workdir);
945 
946     qtest_qmp_assert_success(to,
947                              "{ 'execute': 'object-add',"
948                              "  'arguments': { 'qom-type': 'tls-creds-psk',"
949                              "                 'id': 'tlscredspsk0',"
950                              "                 'endpoint': 'server',"
951                              "                 'dir': %s } }",
952                              mismatch ? data->workdiralt : data->workdir);
953 
954     migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
955     migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
956 
957     return data;
958 }
959 
960 static void *
961 test_migrate_tls_psk_start_match(QTestState *from,
962                                  QTestState *to)
963 {
964     return test_migrate_tls_psk_start_common(from, to, false);
965 }
966 
967 static void *
968 test_migrate_tls_psk_start_mismatch(QTestState *from,
969                                     QTestState *to)
970 {
971     return test_migrate_tls_psk_start_common(from, to, true);
972 }
973 
974 static void
975 test_migrate_tls_psk_finish(QTestState *from,
976                             QTestState *to,
977                             void *opaque)
978 {
979     struct TestMigrateTLSPSKData *data = opaque;
980 
981     test_tls_psk_cleanup(data->pskfile);
982     if (data->pskfilealt) {
983         test_tls_psk_cleanup(data->pskfilealt);
984     }
985     rmdir(data->workdir);
986     if (data->workdiralt) {
987         rmdir(data->workdiralt);
988     }
989 
990     g_free(data->workdiralt);
991     g_free(data->pskfilealt);
992     g_free(data->workdir);
993     g_free(data->pskfile);
994     g_free(data);
995 }
996 
997 #ifdef CONFIG_TASN1
998 typedef struct {
999     char *workdir;
1000     char *keyfile;
1001     char *cacert;
1002     char *servercert;
1003     char *serverkey;
1004     char *clientcert;
1005     char *clientkey;
1006 } TestMigrateTLSX509Data;
1007 
1008 typedef struct {
1009     bool verifyclient;
1010     bool clientcert;
1011     bool hostileclient;
1012     bool authzclient;
1013     const char *certhostname;
1014     const char *certipaddr;
1015 } TestMigrateTLSX509;
1016 
1017 static void *
1018 test_migrate_tls_x509_start_common(QTestState *from,
1019                                    QTestState *to,
1020                                    TestMigrateTLSX509 *args)
1021 {
1022     TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
1023 
1024     data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
1025     data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
1026 
1027     data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
1028     data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
1029     data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
1030     if (args->clientcert) {
1031         data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
1032         data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
1033     }
1034 
1035     g_mkdir_with_parents(data->workdir, 0700);
1036 
1037     test_tls_init(data->keyfile);
1038 #ifndef _WIN32
1039     g_assert(link(data->keyfile, data->serverkey) == 0);
1040 #else
1041     g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
1042 #endif
1043     if (args->clientcert) {
1044 #ifndef _WIN32
1045         g_assert(link(data->keyfile, data->clientkey) == 0);
1046 #else
1047         g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
1048 #endif
1049     }
1050 
1051     TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
1052     if (args->clientcert) {
1053         TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
1054                                    args->hostileclient ?
1055                                    QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
1056                                    QCRYPTO_TLS_TEST_CLIENT_NAME,
1057                                    data->clientcert);
1058     }
1059 
1060     TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
1061                                data->servercert,
1062                                args->certhostname,
1063                                args->certipaddr);
1064 
1065     qtest_qmp_assert_success(from,
1066                              "{ 'execute': 'object-add',"
1067                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1068                              "                 'id': 'tlscredsx509client0',"
1069                              "                 'endpoint': 'client',"
1070                              "                 'dir': %s,"
1071                              "                 'sanity-check': true,"
1072                              "                 'verify-peer': true} }",
1073                              data->workdir);
1074     migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
1075     if (args->certhostname) {
1076         migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
1077     }
1078 
1079     qtest_qmp_assert_success(to,
1080                              "{ 'execute': 'object-add',"
1081                              "  'arguments': { 'qom-type': 'tls-creds-x509',"
1082                              "                 'id': 'tlscredsx509server0',"
1083                              "                 'endpoint': 'server',"
1084                              "                 'dir': %s,"
1085                              "                 'sanity-check': true,"
1086                              "                 'verify-peer': %i} }",
1087                              data->workdir, args->verifyclient);
1088     migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
1089 
1090     if (args->authzclient) {
1091         qtest_qmp_assert_success(to,
1092                                  "{ 'execute': 'object-add',"
1093                                  "  'arguments': { 'qom-type': 'authz-simple',"
1094                                  "                 'id': 'tlsauthz0',"
1095                                  "                 'identity': %s} }",
1096                                  "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
1097         migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
1098     }
1099 
1100     return data;
1101 }
1102 
1103 /*
1104  * The normal case: match server's cert hostname against
1105  * whatever host we were telling QEMU to connect to (if any)
1106  */
1107 static void *
1108 test_migrate_tls_x509_start_default_host(QTestState *from,
1109                                          QTestState *to)
1110 {
1111     TestMigrateTLSX509 args = {
1112         .verifyclient = true,
1113         .clientcert = true,
1114         .certipaddr = "127.0.0.1"
1115     };
1116     return test_migrate_tls_x509_start_common(from, to, &args);
1117 }
1118 
1119 /*
1120  * The unusual case: the server's cert is different from
1121  * the address we're telling QEMU to connect to (if any),
1122  * so we must give QEMU an explicit hostname to validate
1123  */
1124 static void *
1125 test_migrate_tls_x509_start_override_host(QTestState *from,
1126                                           QTestState *to)
1127 {
1128     TestMigrateTLSX509 args = {
1129         .verifyclient = true,
1130         .clientcert = true,
1131         .certhostname = "qemu.org",
1132     };
1133     return test_migrate_tls_x509_start_common(from, to, &args);
1134 }
1135 
1136 /*
1137  * The unusual case: the server's cert is different from
1138  * the address we're telling QEMU to connect to, and so we
1139  * expect the client to reject the server
1140  */
1141 static void *
1142 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1143                                           QTestState *to)
1144 {
1145     TestMigrateTLSX509 args = {
1146         .verifyclient = true,
1147         .clientcert = true,
1148         .certipaddr = "10.0.0.1",
1149     };
1150     return test_migrate_tls_x509_start_common(from, to, &args);
1151 }
1152 
1153 static void *
1154 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1155                                             QTestState *to)
1156 {
1157     TestMigrateTLSX509 args = {
1158         .verifyclient = true,
1159         .clientcert = true,
1160         .authzclient = true,
1161         .certipaddr = "127.0.0.1",
1162     };
1163     return test_migrate_tls_x509_start_common(from, to, &args);
1164 }
1165 
1166 static void *
1167 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1168                                            QTestState *to)
1169 {
1170     TestMigrateTLSX509 args = {
1171         .verifyclient = true,
1172         .clientcert = true,
1173         .hostileclient = true,
1174         .authzclient = true,
1175         .certipaddr = "127.0.0.1",
1176     };
1177     return test_migrate_tls_x509_start_common(from, to, &args);
1178 }
1179 
1180 /*
1181  * The case with no client certificate presented,
1182  * and no server verification
1183  */
1184 static void *
1185 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1186                                               QTestState *to)
1187 {
1188     TestMigrateTLSX509 args = {
1189         .certipaddr = "127.0.0.1",
1190     };
1191     return test_migrate_tls_x509_start_common(from, to, &args);
1192 }
1193 
1194 /*
1195  * The case with no client certificate presented,
1196  * and server verification rejecting
1197  */
1198 static void *
1199 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1200                                                QTestState *to)
1201 {
1202     TestMigrateTLSX509 args = {
1203         .verifyclient = true,
1204         .certipaddr = "127.0.0.1",
1205     };
1206     return test_migrate_tls_x509_start_common(from, to, &args);
1207 }
1208 
1209 static void
1210 test_migrate_tls_x509_finish(QTestState *from,
1211                              QTestState *to,
1212                              void *opaque)
1213 {
1214     TestMigrateTLSX509Data *data = opaque;
1215 
1216     test_tls_cleanup(data->keyfile);
1217     g_free(data->keyfile);
1218 
1219     unlink(data->cacert);
1220     g_free(data->cacert);
1221     unlink(data->servercert);
1222     g_free(data->servercert);
1223     unlink(data->serverkey);
1224     g_free(data->serverkey);
1225 
1226     if (data->clientcert) {
1227         unlink(data->clientcert);
1228         g_free(data->clientcert);
1229     }
1230     if (data->clientkey) {
1231         unlink(data->clientkey);
1232         g_free(data->clientkey);
1233     }
1234 
1235     rmdir(data->workdir);
1236     g_free(data->workdir);
1237 
1238     g_free(data);
1239 }
1240 #endif /* CONFIG_TASN1 */
1241 #endif /* CONFIG_GNUTLS */
1242 
1243 static void *
1244 test_migrate_compress_start(QTestState *from,
1245                             QTestState *to)
1246 {
1247     migrate_set_parameter_int(from, "compress-level", 1);
1248     migrate_set_parameter_int(from, "compress-threads", 4);
1249     migrate_set_parameter_bool(from, "compress-wait-thread", true);
1250     migrate_set_parameter_int(to, "decompress-threads", 4);
1251 
1252     migrate_set_capability(from, "compress", true);
1253     migrate_set_capability(to, "compress", true);
1254 
1255     return NULL;
1256 }
1257 
1258 static void *
1259 test_migrate_compress_nowait_start(QTestState *from,
1260                                    QTestState *to)
1261 {
1262     migrate_set_parameter_int(from, "compress-level", 9);
1263     migrate_set_parameter_int(from, "compress-threads", 1);
1264     migrate_set_parameter_bool(from, "compress-wait-thread", false);
1265     migrate_set_parameter_int(to, "decompress-threads", 1);
1266 
1267     migrate_set_capability(from, "compress", true);
1268     migrate_set_capability(to, "compress", true);
1269 
1270     return NULL;
1271 }
1272 
1273 static int migrate_postcopy_prepare(QTestState **from_ptr,
1274                                     QTestState **to_ptr,
1275                                     MigrateCommon *args)
1276 {
1277     QTestState *from, *to;
1278 
1279     if (test_migrate_start(&from, &to, "defer", &args->start)) {
1280         return -1;
1281     }
1282 
1283     if (args->start_hook) {
1284         args->postcopy_data = args->start_hook(from, to);
1285     }
1286 
1287     migrate_set_capability(from, "postcopy-ram", true);
1288     migrate_set_capability(to, "postcopy-ram", true);
1289     migrate_set_capability(to, "postcopy-blocktime", true);
1290 
1291     if (args->postcopy_preempt) {
1292         migrate_set_capability(from, "postcopy-preempt", true);
1293         migrate_set_capability(to, "postcopy-preempt", true);
1294     }
1295 
1296     migrate_ensure_non_converge(from);
1297 
1298     migrate_prepare_for_dirty_mem(from);
1299     qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1300                              "  'arguments': { "
1301                              "      'channels': [ { 'channel-type': 'main',"
1302                              "      'addr': { 'transport': 'socket',"
1303                              "                'type': 'inet',"
1304                              "                'host': '127.0.0.1',"
1305                              "                'port': '0' } } ] } }");
1306 
1307     /* Wait for the first serial output from the source */
1308     wait_for_serial("src_serial");
1309     wait_for_suspend(from, &src_state);
1310 
1311     migrate_qmp(from, to, NULL, NULL, "{}");
1312 
1313     migrate_wait_for_dirty_mem(from, to);
1314 
1315     *from_ptr = from;
1316     *to_ptr = to;
1317 
1318     return 0;
1319 }
1320 
1321 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1322                                       MigrateCommon *args)
1323 {
1324     wait_for_migration_complete(from);
1325 
1326     if (args->start.suspend_me) {
1327         /* wakeup succeeds only if guest is suspended */
1328         qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
1329     }
1330 
1331     /* Make sure we get at least one "B" on destination */
1332     wait_for_serial("dest_serial");
1333 
1334     if (uffd_feature_thread_id) {
1335         read_blocktime(to);
1336     }
1337 
1338     if (args->finish_hook) {
1339         args->finish_hook(from, to, args->postcopy_data);
1340         args->postcopy_data = NULL;
1341     }
1342 
1343     test_migrate_end(from, to, true);
1344 }
1345 
1346 static void test_postcopy_common(MigrateCommon *args)
1347 {
1348     QTestState *from, *to;
1349 
1350     if (migrate_postcopy_prepare(&from, &to, args)) {
1351         return;
1352     }
1353     migrate_postcopy_start(from, to);
1354     migrate_postcopy_complete(from, to, args);
1355 }
1356 
1357 static void test_postcopy(void)
1358 {
1359     MigrateCommon args = { };
1360 
1361     test_postcopy_common(&args);
1362 }
1363 
1364 static void test_postcopy_suspend(void)
1365 {
1366     MigrateCommon args = {
1367         .start.suspend_me = true,
1368     };
1369 
1370     test_postcopy_common(&args);
1371 }
1372 
1373 static void test_postcopy_compress(void)
1374 {
1375     MigrateCommon args = {
1376         .start_hook = test_migrate_compress_start
1377     };
1378 
1379     test_postcopy_common(&args);
1380 }
1381 
1382 static void test_postcopy_preempt(void)
1383 {
1384     MigrateCommon args = {
1385         .postcopy_preempt = true,
1386     };
1387 
1388     test_postcopy_common(&args);
1389 }
1390 
1391 #ifdef CONFIG_GNUTLS
1392 static void test_postcopy_tls_psk(void)
1393 {
1394     MigrateCommon args = {
1395         .start_hook = test_migrate_tls_psk_start_match,
1396         .finish_hook = test_migrate_tls_psk_finish,
1397     };
1398 
1399     test_postcopy_common(&args);
1400 }
1401 
1402 static void test_postcopy_preempt_tls_psk(void)
1403 {
1404     MigrateCommon args = {
1405         .postcopy_preempt = true,
1406         .start_hook = test_migrate_tls_psk_start_match,
1407         .finish_hook = test_migrate_tls_psk_finish,
1408     };
1409 
1410     test_postcopy_common(&args);
1411 }
1412 #endif
1413 
1414 static void wait_for_postcopy_status(QTestState *one, const char *status)
1415 {
1416     wait_for_migration_status(one, status,
1417                               (const char * []) { "failed", "active",
1418                                                   "completed", NULL });
1419 }
1420 
1421 #ifndef _WIN32
1422 static void postcopy_recover_fail(QTestState *from, QTestState *to)
1423 {
1424     int ret, pair1[2], pair2[2];
1425     char c;
1426 
1427     /* Create two unrelated socketpairs */
1428     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1);
1429     g_assert_cmpint(ret, ==, 0);
1430 
1431     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2);
1432     g_assert_cmpint(ret, ==, 0);
1433 
1434     /*
1435      * Give the guests unpaired ends of the sockets, so they'll all blocked
1436      * at reading.  This mimics a wrong channel established.
1437      */
1438     qtest_qmp_fds_assert_success(from, &pair1[0], 1,
1439                                  "{ 'execute': 'getfd',"
1440                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1441     qtest_qmp_fds_assert_success(to, &pair2[0], 1,
1442                                  "{ 'execute': 'getfd',"
1443                                  "  'arguments': { 'fdname': 'fd-mig' }}");
1444 
1445     /*
1446      * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
1447      * emulate the 1st byte of a real recovery, but stops from there to
1448      * keep dest QEMU in RECOVER.  This is needed so that we can kick off
1449      * the recover process on dest QEMU (by triggering the G_IO_IN event).
1450      *
1451      * NOTE: this trick is not needed on src QEMUs, because src doesn't
1452      * rely on an pre-existing G_IO_IN event, so it will always trigger the
1453      * upcoming recovery anyway even if it can read nothing.
1454      */
1455 #define QEMU_VM_COMMAND              0x08
1456     c = QEMU_VM_COMMAND;
1457     ret = send(pair2[1], &c, 1, 0);
1458     g_assert_cmpint(ret, ==, 1);
1459 
1460     migrate_recover(to, "fd:fd-mig");
1461     migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}");
1462 
1463     /*
1464      * Make sure both QEMU instances will go into RECOVER stage, then test
1465      * kicking them out using migrate-pause.
1466      */
1467     wait_for_postcopy_status(from, "postcopy-recover");
1468     wait_for_postcopy_status(to, "postcopy-recover");
1469 
1470     /*
1471      * This would be issued by the admin upon noticing the hang, we should
1472      * make sure we're able to kick this out.
1473      */
1474     migrate_pause(from);
1475     wait_for_postcopy_status(from, "postcopy-paused");
1476 
1477     /* Do the same test on dest */
1478     migrate_pause(to);
1479     wait_for_postcopy_status(to, "postcopy-paused");
1480 
1481     close(pair1[0]);
1482     close(pair1[1]);
1483     close(pair2[0]);
1484     close(pair2[1]);
1485 }
1486 #endif /* _WIN32 */
1487 
1488 static void test_postcopy_recovery_common(MigrateCommon *args)
1489 {
1490     QTestState *from, *to;
1491     g_autofree char *uri = NULL;
1492 
1493     /* Always hide errors for postcopy recover tests since they're expected */
1494     args->start.hide_stderr = true;
1495 
1496     if (migrate_postcopy_prepare(&from, &to, args)) {
1497         return;
1498     }
1499 
1500     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1501     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1502 
1503     /* Now we start the postcopy */
1504     migrate_postcopy_start(from, to);
1505 
1506     /*
1507      * Wait until postcopy is really started; we can only run the
1508      * migrate-pause command during a postcopy
1509      */
1510     wait_for_migration_status(from, "postcopy-active", NULL);
1511 
1512     /*
1513      * Manually stop the postcopy migration. This emulates a network
1514      * failure with the migration socket
1515      */
1516     migrate_pause(from);
1517 
1518     /*
1519      * Wait for destination side to reach postcopy-paused state.  The
1520      * migrate-recover command can only succeed if destination machine
1521      * is in the paused state
1522      */
1523     wait_for_postcopy_status(to, "postcopy-paused");
1524     wait_for_postcopy_status(from, "postcopy-paused");
1525 
1526 #ifndef _WIN32
1527     if (args->postcopy_recovery_test_fail) {
1528         /*
1529          * Test when a wrong socket specified for recover, and then the
1530          * ability to kick it out, and continue with a correct socket.
1531          */
1532         postcopy_recover_fail(from, to);
1533         /* continue with a good recovery */
1534     }
1535 #endif /* _WIN32 */
1536 
1537     /*
1538      * Create a new socket to emulate a new channel that is different
1539      * from the broken migration channel; tell the destination to
1540      * listen to the new port
1541      */
1542     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1543     migrate_recover(to, uri);
1544 
1545     /*
1546      * Try to rebuild the migration channel using the resume flag and
1547      * the newly created channel
1548      */
1549     migrate_qmp(from, to, uri, NULL, "{'resume': true}");
1550 
1551     /* Restore the postcopy bandwidth to unlimited */
1552     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1553 
1554     migrate_postcopy_complete(from, to, args);
1555 }
1556 
1557 static void test_postcopy_recovery(void)
1558 {
1559     MigrateCommon args = { };
1560 
1561     test_postcopy_recovery_common(&args);
1562 }
1563 
1564 static void test_postcopy_recovery_compress(void)
1565 {
1566     MigrateCommon args = {
1567         .start_hook = test_migrate_compress_start
1568     };
1569 
1570     test_postcopy_recovery_common(&args);
1571 }
1572 
1573 #ifndef _WIN32
1574 static void test_postcopy_recovery_double_fail(void)
1575 {
1576     MigrateCommon args = {
1577         .postcopy_recovery_test_fail = true,
1578     };
1579 
1580     test_postcopy_recovery_common(&args);
1581 }
1582 #endif /* _WIN32 */
1583 
1584 #ifdef CONFIG_GNUTLS
1585 static void test_postcopy_recovery_tls_psk(void)
1586 {
1587     MigrateCommon args = {
1588         .start_hook = test_migrate_tls_psk_start_match,
1589         .finish_hook = test_migrate_tls_psk_finish,
1590     };
1591 
1592     test_postcopy_recovery_common(&args);
1593 }
1594 #endif
1595 
1596 static void test_postcopy_preempt_recovery(void)
1597 {
1598     MigrateCommon args = {
1599         .postcopy_preempt = true,
1600     };
1601 
1602     test_postcopy_recovery_common(&args);
1603 }
1604 
1605 #ifdef CONFIG_GNUTLS
1606 /* This contains preempt+recovery+tls test altogether */
1607 static void test_postcopy_preempt_all(void)
1608 {
1609     MigrateCommon args = {
1610         .postcopy_preempt = true,
1611         .start_hook = test_migrate_tls_psk_start_match,
1612         .finish_hook = test_migrate_tls_psk_finish,
1613     };
1614 
1615     test_postcopy_recovery_common(&args);
1616 }
1617 
1618 #endif
1619 
1620 static void test_baddest(void)
1621 {
1622     MigrateStart args = {
1623         .hide_stderr = true
1624     };
1625     QTestState *from, *to;
1626 
1627     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1628         return;
1629     }
1630     migrate_qmp(from, to, "tcp:127.0.0.1:0", NULL, "{}");
1631     wait_for_migration_fail(from, false);
1632     test_migrate_end(from, to, false);
1633 }
1634 
1635 #ifndef _WIN32
1636 static void test_analyze_script(void)
1637 {
1638     MigrateStart args = {
1639         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1640     };
1641     QTestState *from, *to;
1642     g_autofree char *uri = NULL;
1643     g_autofree char *file = NULL;
1644     int pid, wstatus;
1645     const char *python = g_getenv("PYTHON");
1646 
1647     if (!python) {
1648         g_test_skip("PYTHON variable not set");
1649         return;
1650     }
1651 
1652     /* dummy url */
1653     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1654         return;
1655     }
1656 
1657     /*
1658      * Setting these two capabilities causes the "configuration"
1659      * vmstate to include subsections for them. The script needs to
1660      * parse those subsections properly.
1661      */
1662     migrate_set_capability(from, "validate-uuid", true);
1663     migrate_set_capability(from, "x-ignore-shared", true);
1664 
1665     file = g_strdup_printf("%s/migfile", tmpfs);
1666     uri = g_strdup_printf("exec:cat > %s", file);
1667 
1668     migrate_ensure_converge(from);
1669     migrate_qmp(from, to, uri, NULL, "{}");
1670     wait_for_migration_complete(from);
1671 
1672     pid = fork();
1673     if (!pid) {
1674         close(1);
1675         open("/dev/null", O_WRONLY);
1676         execl(python, python, ANALYZE_SCRIPT, "-f", file, NULL);
1677         g_assert_not_reached();
1678     }
1679 
1680     g_assert(waitpid(pid, &wstatus, 0) == pid);
1681     if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) {
1682         g_test_message("Failed to analyze the migration stream");
1683         g_test_fail();
1684     }
1685     test_migrate_end(from, to, false);
1686     cleanup("migfile");
1687 }
1688 #endif
1689 
1690 static void test_precopy_common(MigrateCommon *args)
1691 {
1692     QTestState *from, *to;
1693     void *data_hook = NULL;
1694 
1695     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1696         return;
1697     }
1698 
1699     if (args->start_hook) {
1700         data_hook = args->start_hook(from, to);
1701     }
1702 
1703     /* Wait for the first serial output from the source */
1704     if (args->result == MIG_TEST_SUCCEED) {
1705         wait_for_serial("src_serial");
1706         wait_for_suspend(from, &src_state);
1707     }
1708 
1709     if (args->live) {
1710         migrate_ensure_non_converge(from);
1711         migrate_prepare_for_dirty_mem(from);
1712     } else {
1713         /*
1714          * Testing non-live migration, we allow it to run at
1715          * full speed to ensure short test case duration.
1716          * For tests expected to fail, we don't need to
1717          * change anything.
1718          */
1719         if (args->result == MIG_TEST_SUCCEED) {
1720             qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1721             wait_for_stop(from, &src_state);
1722             migrate_ensure_converge(from);
1723         }
1724     }
1725 
1726     if (args->result == MIG_TEST_QMP_ERROR) {
1727         migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
1728         goto finish;
1729     }
1730 
1731     migrate_qmp(from, to, args->connect_uri, args->connect_channels, "{}");
1732 
1733     if (args->result != MIG_TEST_SUCCEED) {
1734         bool allow_active = args->result == MIG_TEST_FAIL;
1735         wait_for_migration_fail(from, allow_active);
1736 
1737         if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1738             qtest_set_expected_status(to, EXIT_FAILURE);
1739         }
1740     } else {
1741         if (args->live) {
1742             /*
1743              * For initial iteration(s) we must do a full pass,
1744              * but for the final iteration, we need only wait
1745              * for some dirty mem before switching to converge
1746              */
1747             while (args->iterations > 1) {
1748                 wait_for_migration_pass(from);
1749                 args->iterations--;
1750             }
1751             migrate_wait_for_dirty_mem(from, to);
1752 
1753             migrate_ensure_converge(from);
1754 
1755             /*
1756              * We do this first, as it has a timeout to stop us
1757              * hanging forever if migration didn't converge
1758              */
1759             wait_for_migration_complete(from);
1760 
1761             wait_for_stop(from, &src_state);
1762 
1763         } else {
1764             wait_for_migration_complete(from);
1765             /*
1766              * Must wait for dst to finish reading all incoming
1767              * data on the socket before issuing 'cont' otherwise
1768              * it'll be ignored
1769              */
1770             wait_for_migration_complete(to);
1771 
1772             qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1773         }
1774 
1775         wait_for_resume(to, &dst_state);
1776 
1777         if (args->start.suspend_me) {
1778             /* wakeup succeeds only if guest is suspended */
1779             qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
1780         }
1781 
1782         wait_for_serial("dest_serial");
1783     }
1784 
1785 finish:
1786     if (args->finish_hook) {
1787         args->finish_hook(from, to, data_hook);
1788     }
1789 
1790     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1791 }
1792 
1793 static void test_file_common(MigrateCommon *args, bool stop_src)
1794 {
1795     QTestState *from, *to;
1796     void *data_hook = NULL;
1797 
1798     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1799         return;
1800     }
1801 
1802     /*
1803      * File migration is never live. We can keep the source VM running
1804      * during migration, but the destination will not be running
1805      * concurrently.
1806      */
1807     g_assert_false(args->live);
1808 
1809     if (args->start_hook) {
1810         data_hook = args->start_hook(from, to);
1811     }
1812 
1813     migrate_ensure_converge(from);
1814     wait_for_serial("src_serial");
1815 
1816     if (stop_src) {
1817         qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1818         wait_for_stop(from, &src_state);
1819     }
1820 
1821     if (args->result == MIG_TEST_QMP_ERROR) {
1822         migrate_qmp_fail(from, args->connect_uri, NULL, "{}");
1823         goto finish;
1824     }
1825 
1826     migrate_qmp(from, to, args->connect_uri, NULL, "{}");
1827     wait_for_migration_complete(from);
1828 
1829     /*
1830      * We need to wait for the source to finish before starting the
1831      * destination.
1832      */
1833     migrate_incoming_qmp(to, args->connect_uri, "{}");
1834     wait_for_migration_complete(to);
1835 
1836     if (stop_src) {
1837         qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1838     }
1839     wait_for_resume(to, &dst_state);
1840 
1841     wait_for_serial("dest_serial");
1842 
1843 finish:
1844     if (args->finish_hook) {
1845         args->finish_hook(from, to, data_hook);
1846     }
1847 
1848     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1849 }
1850 
1851 static void test_precopy_unix_plain(void)
1852 {
1853     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1854     MigrateCommon args = {
1855         .listen_uri = uri,
1856         .connect_uri = uri,
1857         /*
1858          * The simplest use case of precopy, covering smoke tests of
1859          * get-dirty-log dirty tracking.
1860          */
1861         .live = true,
1862     };
1863 
1864     test_precopy_common(&args);
1865 }
1866 
1867 static void test_precopy_unix_suspend_live(void)
1868 {
1869     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1870     MigrateCommon args = {
1871         .listen_uri = uri,
1872         .connect_uri = uri,
1873         /*
1874          * despite being live, the test is fast because the src
1875          * suspends immediately.
1876          */
1877         .live = true,
1878         .start.suspend_me = true,
1879     };
1880 
1881     test_precopy_common(&args);
1882 }
1883 
1884 static void test_precopy_unix_suspend_notlive(void)
1885 {
1886     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1887     MigrateCommon args = {
1888         .listen_uri = uri,
1889         .connect_uri = uri,
1890         .start.suspend_me = true,
1891     };
1892 
1893     test_precopy_common(&args);
1894 }
1895 
1896 static void test_precopy_unix_dirty_ring(void)
1897 {
1898     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1899     MigrateCommon args = {
1900         .start = {
1901             .use_dirty_ring = true,
1902         },
1903         .listen_uri = uri,
1904         .connect_uri = uri,
1905         /*
1906          * Besides the precopy/unix basic test, cover dirty ring interface
1907          * rather than get-dirty-log.
1908          */
1909         .live = true,
1910     };
1911 
1912     test_precopy_common(&args);
1913 }
1914 
1915 #ifdef CONFIG_GNUTLS
1916 static void test_precopy_unix_tls_psk(void)
1917 {
1918     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1919     MigrateCommon args = {
1920         .connect_uri = uri,
1921         .listen_uri = uri,
1922         .start_hook = test_migrate_tls_psk_start_match,
1923         .finish_hook = test_migrate_tls_psk_finish,
1924     };
1925 
1926     test_precopy_common(&args);
1927 }
1928 
1929 #ifdef CONFIG_TASN1
1930 static void test_precopy_unix_tls_x509_default_host(void)
1931 {
1932     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1933     MigrateCommon args = {
1934         .start = {
1935             .hide_stderr = true,
1936         },
1937         .connect_uri = uri,
1938         .listen_uri = uri,
1939         .start_hook = test_migrate_tls_x509_start_default_host,
1940         .finish_hook = test_migrate_tls_x509_finish,
1941         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1942     };
1943 
1944     test_precopy_common(&args);
1945 }
1946 
1947 static void test_precopy_unix_tls_x509_override_host(void)
1948 {
1949     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1950     MigrateCommon args = {
1951         .connect_uri = uri,
1952         .listen_uri = uri,
1953         .start_hook = test_migrate_tls_x509_start_override_host,
1954         .finish_hook = test_migrate_tls_x509_finish,
1955     };
1956 
1957     test_precopy_common(&args);
1958 }
1959 #endif /* CONFIG_TASN1 */
1960 #endif /* CONFIG_GNUTLS */
1961 
1962 #if 0
1963 /* Currently upset on aarch64 TCG */
1964 static void test_ignore_shared(void)
1965 {
1966     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1967     QTestState *from, *to;
1968 
1969     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1970         return;
1971     }
1972 
1973     migrate_ensure_non_converge(from);
1974     migrate_prepare_for_dirty_mem(from);
1975 
1976     migrate_set_capability(from, "x-ignore-shared", true);
1977     migrate_set_capability(to, "x-ignore-shared", true);
1978 
1979     /* Wait for the first serial output from the source */
1980     wait_for_serial("src_serial");
1981 
1982     migrate_qmp(from, to, uri, NULL, "{}");
1983 
1984     migrate_wait_for_dirty_mem(from, to);
1985 
1986     wait_for_stop(from, &src_state);
1987 
1988     qtest_qmp_eventwait(to, "RESUME");
1989 
1990     wait_for_serial("dest_serial");
1991     wait_for_migration_complete(from);
1992 
1993     /* Check whether shared RAM has been really skipped */
1994     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1995 
1996     test_migrate_end(from, to, true);
1997 }
1998 #endif
1999 
2000 static void *
2001 test_migrate_xbzrle_start(QTestState *from,
2002                           QTestState *to)
2003 {
2004     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
2005 
2006     migrate_set_capability(from, "xbzrle", true);
2007     migrate_set_capability(to, "xbzrle", true);
2008 
2009     return NULL;
2010 }
2011 
2012 static void test_precopy_unix_xbzrle(void)
2013 {
2014     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2015     MigrateCommon args = {
2016         .connect_uri = uri,
2017         .listen_uri = uri,
2018         .start_hook = test_migrate_xbzrle_start,
2019         .iterations = 2,
2020         /*
2021          * XBZRLE needs pages to be modified when doing the 2nd+ round
2022          * iteration to have real data pushed to the stream.
2023          */
2024         .live = true,
2025     };
2026 
2027     test_precopy_common(&args);
2028 }
2029 
2030 static void test_precopy_unix_compress(void)
2031 {
2032     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2033     MigrateCommon args = {
2034         .connect_uri = uri,
2035         .listen_uri = uri,
2036         .start_hook = test_migrate_compress_start,
2037         /*
2038          * Test that no invalid thread state is left over from
2039          * the previous iteration.
2040          */
2041         .iterations = 2,
2042         /*
2043          * We make sure the compressor can always work well even if guest
2044          * memory is changing.  See commit 34ab9e9743 where we used to fix
2045          * a bug when only trigger-able with guest memory changing.
2046          */
2047         .live = true,
2048     };
2049 
2050     test_precopy_common(&args);
2051 }
2052 
2053 static void test_precopy_unix_compress_nowait(void)
2054 {
2055     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2056     MigrateCommon args = {
2057         .connect_uri = uri,
2058         .listen_uri = uri,
2059         .start_hook = test_migrate_compress_nowait_start,
2060         /*
2061          * Test that no invalid thread state is left over from
2062          * the previous iteration.
2063          */
2064         .iterations = 2,
2065         /* Same reason for the wait version of precopy compress test */
2066         .live = true,
2067     };
2068 
2069     test_precopy_common(&args);
2070 }
2071 
2072 static void test_precopy_file(void)
2073 {
2074     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2075                                            FILE_TEST_FILENAME);
2076     MigrateCommon args = {
2077         .connect_uri = uri,
2078         .listen_uri = "defer",
2079     };
2080 
2081     test_file_common(&args, true);
2082 }
2083 
2084 static void file_offset_finish_hook(QTestState *from, QTestState *to,
2085                                     void *opaque)
2086 {
2087 #if defined(__linux__)
2088     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2089     size_t size = FILE_TEST_OFFSET + sizeof(QEMU_VM_FILE_MAGIC);
2090     uintptr_t *addr, *p;
2091     int fd;
2092 
2093     fd = open(path, O_RDONLY);
2094     g_assert(fd != -1);
2095     addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2096     g_assert(addr != MAP_FAILED);
2097 
2098     /*
2099      * Ensure the skipped offset contains zeros and the migration
2100      * stream starts at the right place.
2101      */
2102     p = addr;
2103     while (p < addr + FILE_TEST_OFFSET / sizeof(uintptr_t)) {
2104         g_assert(*p == 0);
2105         p++;
2106     }
2107     g_assert_cmpint(cpu_to_be64(*p) >> 32, ==, QEMU_VM_FILE_MAGIC);
2108 
2109     munmap(addr, size);
2110     close(fd);
2111 #endif
2112 }
2113 
2114 static void test_precopy_file_offset(void)
2115 {
2116     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=%d", tmpfs,
2117                                            FILE_TEST_FILENAME,
2118                                            FILE_TEST_OFFSET);
2119     MigrateCommon args = {
2120         .connect_uri = uri,
2121         .listen_uri = "defer",
2122         .finish_hook = file_offset_finish_hook,
2123     };
2124 
2125     test_file_common(&args, false);
2126 }
2127 
2128 static void test_precopy_file_offset_bad(void)
2129 {
2130     /* using a value not supported by qemu_strtosz() */
2131     g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=0x20M",
2132                                            tmpfs, FILE_TEST_FILENAME);
2133     MigrateCommon args = {
2134         .connect_uri = uri,
2135         .listen_uri = "defer",
2136         .result = MIG_TEST_QMP_ERROR,
2137     };
2138 
2139     test_file_common(&args, false);
2140 }
2141 
2142 static void *test_mode_reboot_start(QTestState *from, QTestState *to)
2143 {
2144     migrate_set_parameter_str(from, "mode", "cpr-reboot");
2145     migrate_set_parameter_str(to, "mode", "cpr-reboot");
2146 
2147     migrate_set_capability(from, "x-ignore-shared", true);
2148     migrate_set_capability(to, "x-ignore-shared", true);
2149 
2150     return NULL;
2151 }
2152 
2153 static void *migrate_mapped_ram_start(QTestState *from, QTestState *to)
2154 {
2155     migrate_set_capability(from, "mapped-ram", true);
2156     migrate_set_capability(to, "mapped-ram", true);
2157 
2158     return NULL;
2159 }
2160 
2161 static void test_mode_reboot(void)
2162 {
2163     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2164                                            FILE_TEST_FILENAME);
2165     MigrateCommon args = {
2166         .start.use_shmem = true,
2167         .connect_uri = uri,
2168         .listen_uri = "defer",
2169         .start_hook = test_mode_reboot_start
2170     };
2171 
2172     test_file_common(&args, true);
2173 }
2174 
2175 static void test_precopy_file_mapped_ram_live(void)
2176 {
2177     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2178                                            FILE_TEST_FILENAME);
2179     MigrateCommon args = {
2180         .connect_uri = uri,
2181         .listen_uri = "defer",
2182         .start_hook = migrate_mapped_ram_start,
2183     };
2184 
2185     test_file_common(&args, false);
2186 }
2187 
2188 static void test_precopy_file_mapped_ram(void)
2189 {
2190     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2191                                            FILE_TEST_FILENAME);
2192     MigrateCommon args = {
2193         .connect_uri = uri,
2194         .listen_uri = "defer",
2195         .start_hook = migrate_mapped_ram_start,
2196     };
2197 
2198     test_file_common(&args, true);
2199 }
2200 
2201 static void *migrate_multifd_mapped_ram_start(QTestState *from, QTestState *to)
2202 {
2203     migrate_mapped_ram_start(from, to);
2204 
2205     migrate_set_parameter_int(from, "multifd-channels", 4);
2206     migrate_set_parameter_int(to, "multifd-channels", 4);
2207 
2208     migrate_set_capability(from, "multifd", true);
2209     migrate_set_capability(to, "multifd", true);
2210 
2211     return NULL;
2212 }
2213 
2214 static void test_multifd_file_mapped_ram_live(void)
2215 {
2216     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2217                                            FILE_TEST_FILENAME);
2218     MigrateCommon args = {
2219         .connect_uri = uri,
2220         .listen_uri = "defer",
2221         .start_hook = migrate_multifd_mapped_ram_start,
2222     };
2223 
2224     test_file_common(&args, false);
2225 }
2226 
2227 static void test_multifd_file_mapped_ram(void)
2228 {
2229     g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2230                                            FILE_TEST_FILENAME);
2231     MigrateCommon args = {
2232         .connect_uri = uri,
2233         .listen_uri = "defer",
2234         .start_hook = migrate_multifd_mapped_ram_start,
2235     };
2236 
2237     test_file_common(&args, true);
2238 }
2239 
2240 
2241 static void test_precopy_tcp_plain(void)
2242 {
2243     MigrateCommon args = {
2244         .listen_uri = "tcp:127.0.0.1:0",
2245     };
2246 
2247     test_precopy_common(&args);
2248 }
2249 
2250 static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to)
2251 {
2252 
2253     migrate_set_capability(from, "return-path", true);
2254     migrate_set_capability(to, "return-path", true);
2255 
2256     migrate_set_capability(from, "switchover-ack", true);
2257     migrate_set_capability(to, "switchover-ack", true);
2258 
2259     return NULL;
2260 }
2261 
2262 static void test_precopy_tcp_switchover_ack(void)
2263 {
2264     MigrateCommon args = {
2265         .listen_uri = "tcp:127.0.0.1:0",
2266         .start_hook = test_migrate_switchover_ack_start,
2267         /*
2268          * Source VM must be running in order to consider the switchover ACK
2269          * when deciding to do switchover or not.
2270          */
2271         .live = true,
2272     };
2273 
2274     test_precopy_common(&args);
2275 }
2276 
2277 #ifdef CONFIG_GNUTLS
2278 static void test_precopy_tcp_tls_psk_match(void)
2279 {
2280     MigrateCommon args = {
2281         .listen_uri = "tcp:127.0.0.1:0",
2282         .start_hook = test_migrate_tls_psk_start_match,
2283         .finish_hook = test_migrate_tls_psk_finish,
2284     };
2285 
2286     test_precopy_common(&args);
2287 }
2288 
2289 static void test_precopy_tcp_tls_psk_mismatch(void)
2290 {
2291     MigrateCommon args = {
2292         .start = {
2293             .hide_stderr = true,
2294         },
2295         .listen_uri = "tcp:127.0.0.1:0",
2296         .start_hook = test_migrate_tls_psk_start_mismatch,
2297         .finish_hook = test_migrate_tls_psk_finish,
2298         .result = MIG_TEST_FAIL,
2299     };
2300 
2301     test_precopy_common(&args);
2302 }
2303 
2304 #ifdef CONFIG_TASN1
2305 static void test_precopy_tcp_tls_x509_default_host(void)
2306 {
2307     MigrateCommon args = {
2308         .listen_uri = "tcp:127.0.0.1:0",
2309         .start_hook = test_migrate_tls_x509_start_default_host,
2310         .finish_hook = test_migrate_tls_x509_finish,
2311     };
2312 
2313     test_precopy_common(&args);
2314 }
2315 
2316 static void test_precopy_tcp_tls_x509_override_host(void)
2317 {
2318     MigrateCommon args = {
2319         .listen_uri = "tcp:127.0.0.1:0",
2320         .start_hook = test_migrate_tls_x509_start_override_host,
2321         .finish_hook = test_migrate_tls_x509_finish,
2322     };
2323 
2324     test_precopy_common(&args);
2325 }
2326 
2327 static void test_precopy_tcp_tls_x509_mismatch_host(void)
2328 {
2329     MigrateCommon args = {
2330         .start = {
2331             .hide_stderr = true,
2332         },
2333         .listen_uri = "tcp:127.0.0.1:0",
2334         .start_hook = test_migrate_tls_x509_start_mismatch_host,
2335         .finish_hook = test_migrate_tls_x509_finish,
2336         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
2337     };
2338 
2339     test_precopy_common(&args);
2340 }
2341 
2342 static void test_precopy_tcp_tls_x509_friendly_client(void)
2343 {
2344     MigrateCommon args = {
2345         .listen_uri = "tcp:127.0.0.1:0",
2346         .start_hook = test_migrate_tls_x509_start_friendly_client,
2347         .finish_hook = test_migrate_tls_x509_finish,
2348     };
2349 
2350     test_precopy_common(&args);
2351 }
2352 
2353 static void test_precopy_tcp_tls_x509_hostile_client(void)
2354 {
2355     MigrateCommon args = {
2356         .start = {
2357             .hide_stderr = true,
2358         },
2359         .listen_uri = "tcp:127.0.0.1:0",
2360         .start_hook = test_migrate_tls_x509_start_hostile_client,
2361         .finish_hook = test_migrate_tls_x509_finish,
2362         .result = MIG_TEST_FAIL,
2363     };
2364 
2365     test_precopy_common(&args);
2366 }
2367 
2368 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2369 {
2370     MigrateCommon args = {
2371         .listen_uri = "tcp:127.0.0.1:0",
2372         .start_hook = test_migrate_tls_x509_start_allow_anon_client,
2373         .finish_hook = test_migrate_tls_x509_finish,
2374     };
2375 
2376     test_precopy_common(&args);
2377 }
2378 
2379 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2380 {
2381     MigrateCommon args = {
2382         .start = {
2383             .hide_stderr = true,
2384         },
2385         .listen_uri = "tcp:127.0.0.1:0",
2386         .start_hook = test_migrate_tls_x509_start_reject_anon_client,
2387         .finish_hook = test_migrate_tls_x509_finish,
2388         .result = MIG_TEST_FAIL,
2389     };
2390 
2391     test_precopy_common(&args);
2392 }
2393 #endif /* CONFIG_TASN1 */
2394 #endif /* CONFIG_GNUTLS */
2395 
2396 #ifndef _WIN32
2397 static void *test_migrate_fd_start_hook(QTestState *from,
2398                                         QTestState *to)
2399 {
2400     int ret;
2401     int pair[2];
2402 
2403     /* Create two connected sockets for migration */
2404     ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
2405     g_assert_cmpint(ret, ==, 0);
2406 
2407     /* Send the 1st socket to the target */
2408     qtest_qmp_fds_assert_success(to, &pair[0], 1,
2409                                  "{ 'execute': 'getfd',"
2410                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2411     close(pair[0]);
2412 
2413     /* Start incoming migration from the 1st socket */
2414     migrate_incoming_qmp(to, "fd:fd-mig", "{}");
2415 
2416     /* Send the 2nd socket to the target */
2417     qtest_qmp_fds_assert_success(from, &pair[1], 1,
2418                                  "{ 'execute': 'getfd',"
2419                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2420     close(pair[1]);
2421 
2422     return NULL;
2423 }
2424 
2425 static void test_migrate_fd_finish_hook(QTestState *from,
2426                                         QTestState *to,
2427                                         void *opaque)
2428 {
2429     QDict *rsp;
2430     const char *error_desc;
2431 
2432     /* Test closing fds */
2433     /* We assume, that QEMU removes named fd from its list,
2434      * so this should fail */
2435     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
2436                           "  'arguments': { 'fdname': 'fd-mig' }}");
2437     g_assert_true(qdict_haskey(rsp, "error"));
2438     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2439     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2440     qobject_unref(rsp);
2441 
2442     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
2443                         "  'arguments': { 'fdname': 'fd-mig' }}");
2444     g_assert_true(qdict_haskey(rsp, "error"));
2445     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2446     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2447     qobject_unref(rsp);
2448 }
2449 
2450 static void test_migrate_precopy_fd_socket(void)
2451 {
2452     MigrateCommon args = {
2453         .listen_uri = "defer",
2454         .connect_uri = "fd:fd-mig",
2455         .start_hook = test_migrate_fd_start_hook,
2456         .finish_hook = test_migrate_fd_finish_hook
2457     };
2458     test_precopy_common(&args);
2459 }
2460 
2461 static void *migrate_precopy_fd_file_start(QTestState *from, QTestState *to)
2462 {
2463     g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2464     int src_flags = O_CREAT | O_RDWR;
2465     int dst_flags = O_CREAT | O_RDWR;
2466     int fds[2];
2467 
2468     fds[0] = open(file, src_flags, 0660);
2469     assert(fds[0] != -1);
2470 
2471     fds[1] = open(file, dst_flags, 0660);
2472     assert(fds[1] != -1);
2473 
2474 
2475     qtest_qmp_fds_assert_success(to, &fds[0], 1,
2476                                  "{ 'execute': 'getfd',"
2477                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2478 
2479     qtest_qmp_fds_assert_success(from, &fds[1], 1,
2480                                  "{ 'execute': 'getfd',"
2481                                  "  'arguments': { 'fdname': 'fd-mig' }}");
2482 
2483     close(fds[0]);
2484     close(fds[1]);
2485 
2486     return NULL;
2487 }
2488 
2489 static void test_migrate_precopy_fd_file(void)
2490 {
2491     MigrateCommon args = {
2492         .listen_uri = "defer",
2493         .connect_uri = "fd:fd-mig",
2494         .start_hook = migrate_precopy_fd_file_start,
2495         .finish_hook = test_migrate_fd_finish_hook
2496     };
2497     test_file_common(&args, true);
2498 }
2499 #endif /* _WIN32 */
2500 
2501 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
2502 {
2503     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2504     QTestState *from, *to;
2505 
2506     if (test_migrate_start(&from, &to, uri, args)) {
2507         return;
2508     }
2509 
2510     /*
2511      * UUID validation is at the begin of migration. So, the main process of
2512      * migration is not interesting for us here. Thus, set huge downtime for
2513      * very fast migration.
2514      */
2515     migrate_set_parameter_int(from, "downtime-limit", 1000000);
2516     migrate_set_capability(from, "validate-uuid", true);
2517 
2518     /* Wait for the first serial output from the source */
2519     wait_for_serial("src_serial");
2520 
2521     migrate_qmp(from, to, uri, NULL, "{}");
2522 
2523     if (should_fail) {
2524         qtest_set_expected_status(to, EXIT_FAILURE);
2525         wait_for_migration_fail(from, true);
2526     } else {
2527         wait_for_migration_complete(from);
2528     }
2529 
2530     test_migrate_end(from, to, false);
2531 }
2532 
2533 static void test_validate_uuid(void)
2534 {
2535     MigrateStart args = {
2536         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2537         .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
2538     };
2539 
2540     do_test_validate_uuid(&args, false);
2541 }
2542 
2543 static void test_validate_uuid_error(void)
2544 {
2545     MigrateStart args = {
2546         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2547         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2548         .hide_stderr = true,
2549     };
2550 
2551     do_test_validate_uuid(&args, true);
2552 }
2553 
2554 static void test_validate_uuid_src_not_set(void)
2555 {
2556     MigrateStart args = {
2557         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2558         .hide_stderr = true,
2559     };
2560 
2561     do_test_validate_uuid(&args, false);
2562 }
2563 
2564 static void test_validate_uuid_dst_not_set(void)
2565 {
2566     MigrateStart args = {
2567         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2568         .hide_stderr = true,
2569     };
2570 
2571     do_test_validate_uuid(&args, false);
2572 }
2573 
2574 static void do_test_validate_uri_channel(MigrateCommon *args)
2575 {
2576     QTestState *from, *to;
2577 
2578     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
2579         return;
2580     }
2581 
2582     /* Wait for the first serial output from the source */
2583     wait_for_serial("src_serial");
2584 
2585     /*
2586      * 'uri' and 'channels' validation is checked even before the migration
2587      * starts.
2588      */
2589     migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
2590     test_migrate_end(from, to, false);
2591 }
2592 
2593 static void test_validate_uri_channels_both_set(void)
2594 {
2595     MigrateCommon args = {
2596         .start = {
2597             .hide_stderr = true,
2598         },
2599         .listen_uri = "defer",
2600         .connect_uri = "tcp:127.0.0.1:0",
2601         .connect_channels = "[ { 'channel-type': 'main',"
2602                             "    'addr': { 'transport': 'socket',"
2603                             "              'type': 'inet',"
2604                             "              'host': '127.0.0.1',"
2605                             "              'port': '0' } } ]",
2606     };
2607 
2608     do_test_validate_uri_channel(&args);
2609 }
2610 
2611 static void test_validate_uri_channels_none_set(void)
2612 {
2613     MigrateCommon args = {
2614         .start = {
2615             .hide_stderr = true,
2616         },
2617         .listen_uri = "defer",
2618     };
2619 
2620     do_test_validate_uri_channel(&args);
2621 }
2622 
2623 /*
2624  * The way auto_converge works, we need to do too many passes to
2625  * run this test.  Auto_converge logic is only run once every
2626  * three iterations, so:
2627  *
2628  * - 3 iterations without auto_converge enabled
2629  * - 3 iterations with pct = 5
2630  * - 3 iterations with pct = 30
2631  * - 3 iterations with pct = 55
2632  * - 3 iterations with pct = 80
2633  * - 3 iterations with pct = 95 (max(95, 80 + 25))
2634  *
2635  * To make things even worse, we need to run the initial stage at
2636  * 3MB/s so we enter autoconverge even when host is (over)loaded.
2637  */
2638 static void test_migrate_auto_converge(void)
2639 {
2640     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2641     MigrateStart args = {};
2642     QTestState *from, *to;
2643     int64_t percentage;
2644 
2645     /*
2646      * We want the test to be stable and as fast as possible.
2647      * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2648      * so we need to decrease a bandwidth.
2649      */
2650     const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
2651 
2652     if (test_migrate_start(&from, &to, uri, &args)) {
2653         return;
2654     }
2655 
2656     migrate_set_capability(from, "auto-converge", true);
2657     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
2658     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
2659     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
2660 
2661     /*
2662      * Set the initial parameters so that the migration could not converge
2663      * without throttling.
2664      */
2665     migrate_ensure_non_converge(from);
2666 
2667     /* To check remaining size after precopy */
2668     migrate_set_capability(from, "pause-before-switchover", true);
2669 
2670     /* Wait for the first serial output from the source */
2671     wait_for_serial("src_serial");
2672 
2673     migrate_qmp(from, to, uri, NULL, "{}");
2674 
2675     /* Wait for throttling begins */
2676     percentage = 0;
2677     do {
2678         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2679         if (percentage != 0) {
2680             break;
2681         }
2682         usleep(20);
2683         g_assert_false(src_state.stop_seen);
2684     } while (true);
2685     /* The first percentage of throttling should be at least init_pct */
2686     g_assert_cmpint(percentage, >=, init_pct);
2687     /* Now, when we tested that throttling works, let it converge */
2688     migrate_ensure_converge(from);
2689 
2690     /*
2691      * Wait for pre-switchover status to check last throttle percentage
2692      * and remaining. These values will be zeroed later
2693      */
2694     wait_for_migration_status(from, "pre-switchover", NULL);
2695 
2696     /* The final percentage of throttling shouldn't be greater than max_pct */
2697     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2698     g_assert_cmpint(percentage, <=, max_pct);
2699     migrate_continue(from, "pre-switchover");
2700 
2701     qtest_qmp_eventwait(to, "RESUME");
2702 
2703     wait_for_serial("dest_serial");
2704     wait_for_migration_complete(from);
2705 
2706     test_migrate_end(from, to, true);
2707 }
2708 
2709 static void *
2710 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2711                                               QTestState *to,
2712                                               const char *method)
2713 {
2714     migrate_set_parameter_int(from, "multifd-channels", 16);
2715     migrate_set_parameter_int(to, "multifd-channels", 16);
2716 
2717     migrate_set_parameter_str(from, "multifd-compression", method);
2718     migrate_set_parameter_str(to, "multifd-compression", method);
2719 
2720     migrate_set_capability(from, "multifd", true);
2721     migrate_set_capability(to, "multifd", true);
2722 
2723     /* Start incoming migration from the 1st socket */
2724     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
2725 
2726     return NULL;
2727 }
2728 
2729 static void *
2730 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2731                                        QTestState *to)
2732 {
2733     return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2734 }
2735 
2736 static void *
2737 test_migrate_precopy_tcp_multifd_start_zero_page_legacy(QTestState *from,
2738                                                         QTestState *to)
2739 {
2740     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2741     migrate_set_parameter_str(from, "zero-page-detection", "legacy");
2742     return NULL;
2743 }
2744 
2745 static void *
2746 test_migration_precopy_tcp_multifd_start_no_zero_page(QTestState *from,
2747                                                       QTestState *to)
2748 {
2749     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2750     migrate_set_parameter_str(from, "zero-page-detection", "none");
2751     return NULL;
2752 }
2753 
2754 static void *
2755 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2756                                             QTestState *to)
2757 {
2758     /*
2759      * Overloading this test to also check that set_parameter does not error.
2760      * This is also done in the tests for the other compression methods.
2761      */
2762     migrate_set_parameter_int(from, "multifd-zlib-level", 2);
2763     migrate_set_parameter_int(to, "multifd-zlib-level", 2);
2764 
2765     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2766 }
2767 
2768 #ifdef CONFIG_ZSTD
2769 static void *
2770 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2771                                             QTestState *to)
2772 {
2773     migrate_set_parameter_int(from, "multifd-zstd-level", 2);
2774     migrate_set_parameter_int(to, "multifd-zstd-level", 2);
2775 
2776     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2777 }
2778 #endif /* CONFIG_ZSTD */
2779 
2780 static void test_multifd_tcp_uri_none(void)
2781 {
2782     MigrateCommon args = {
2783         .listen_uri = "defer",
2784         .start_hook = test_migrate_precopy_tcp_multifd_start,
2785         /*
2786          * Multifd is more complicated than most of the features, it
2787          * directly takes guest page buffers when sending, make sure
2788          * everything will work alright even if guest page is changing.
2789          */
2790         .live = true,
2791     };
2792     test_precopy_common(&args);
2793 }
2794 
2795 static void test_multifd_tcp_zero_page_legacy(void)
2796 {
2797     MigrateCommon args = {
2798         .listen_uri = "defer",
2799         .start_hook = test_migrate_precopy_tcp_multifd_start_zero_page_legacy,
2800         /*
2801          * Multifd is more complicated than most of the features, it
2802          * directly takes guest page buffers when sending, make sure
2803          * everything will work alright even if guest page is changing.
2804          */
2805         .live = true,
2806     };
2807     test_precopy_common(&args);
2808 }
2809 
2810 static void test_multifd_tcp_no_zero_page(void)
2811 {
2812     MigrateCommon args = {
2813         .listen_uri = "defer",
2814         .start_hook = test_migration_precopy_tcp_multifd_start_no_zero_page,
2815         /*
2816          * Multifd is more complicated than most of the features, it
2817          * directly takes guest page buffers when sending, make sure
2818          * everything will work alright even if guest page is changing.
2819          */
2820         .live = true,
2821     };
2822     test_precopy_common(&args);
2823 }
2824 
2825 static void test_multifd_tcp_channels_none(void)
2826 {
2827     MigrateCommon args = {
2828         .listen_uri = "defer",
2829         .start_hook = test_migrate_precopy_tcp_multifd_start,
2830         .live = true,
2831         .connect_channels = "[ { 'channel-type': 'main',"
2832                             "    'addr': { 'transport': 'socket',"
2833                             "              'type': 'inet',"
2834                             "              'host': '127.0.0.1',"
2835                             "              'port': '0' } } ]",
2836     };
2837     test_precopy_common(&args);
2838 }
2839 
2840 static void test_multifd_tcp_zlib(void)
2841 {
2842     MigrateCommon args = {
2843         .listen_uri = "defer",
2844         .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2845     };
2846     test_precopy_common(&args);
2847 }
2848 
2849 #ifdef CONFIG_ZSTD
2850 static void test_multifd_tcp_zstd(void)
2851 {
2852     MigrateCommon args = {
2853         .listen_uri = "defer",
2854         .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2855     };
2856     test_precopy_common(&args);
2857 }
2858 #endif
2859 
2860 #ifdef CONFIG_GNUTLS
2861 static void *
2862 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2863                                              QTestState *to)
2864 {
2865     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2866     return test_migrate_tls_psk_start_match(from, to);
2867 }
2868 
2869 static void *
2870 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2871                                                 QTestState *to)
2872 {
2873     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2874     return test_migrate_tls_psk_start_mismatch(from, to);
2875 }
2876 
2877 #ifdef CONFIG_TASN1
2878 static void *
2879 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2880                                                  QTestState *to)
2881 {
2882     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2883     return test_migrate_tls_x509_start_default_host(from, to);
2884 }
2885 
2886 static void *
2887 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2888                                                   QTestState *to)
2889 {
2890     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2891     return test_migrate_tls_x509_start_override_host(from, to);
2892 }
2893 
2894 static void *
2895 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2896                                                   QTestState *to)
2897 {
2898     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2899     return test_migrate_tls_x509_start_mismatch_host(from, to);
2900 }
2901 
2902 static void *
2903 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2904                                                       QTestState *to)
2905 {
2906     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2907     return test_migrate_tls_x509_start_allow_anon_client(from, to);
2908 }
2909 
2910 static void *
2911 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2912                                                        QTestState *to)
2913 {
2914     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2915     return test_migrate_tls_x509_start_reject_anon_client(from, to);
2916 }
2917 #endif /* CONFIG_TASN1 */
2918 
2919 static void test_multifd_tcp_tls_psk_match(void)
2920 {
2921     MigrateCommon args = {
2922         .listen_uri = "defer",
2923         .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2924         .finish_hook = test_migrate_tls_psk_finish,
2925     };
2926     test_precopy_common(&args);
2927 }
2928 
2929 static void test_multifd_tcp_tls_psk_mismatch(void)
2930 {
2931     MigrateCommon args = {
2932         .start = {
2933             .hide_stderr = true,
2934         },
2935         .listen_uri = "defer",
2936         .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2937         .finish_hook = test_migrate_tls_psk_finish,
2938         .result = MIG_TEST_FAIL,
2939     };
2940     test_precopy_common(&args);
2941 }
2942 
2943 #ifdef CONFIG_TASN1
2944 static void test_multifd_tcp_tls_x509_default_host(void)
2945 {
2946     MigrateCommon args = {
2947         .listen_uri = "defer",
2948         .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2949         .finish_hook = test_migrate_tls_x509_finish,
2950     };
2951     test_precopy_common(&args);
2952 }
2953 
2954 static void test_multifd_tcp_tls_x509_override_host(void)
2955 {
2956     MigrateCommon args = {
2957         .listen_uri = "defer",
2958         .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2959         .finish_hook = test_migrate_tls_x509_finish,
2960     };
2961     test_precopy_common(&args);
2962 }
2963 
2964 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2965 {
2966     /*
2967      * This has different behaviour to the non-multifd case.
2968      *
2969      * In non-multifd case when client aborts due to mismatched
2970      * cert host, the server has already started trying to load
2971      * migration state, and so it exits with I/O failure.
2972      *
2973      * In multifd case when client aborts due to mismatched
2974      * cert host, the server is still waiting for the other
2975      * multifd connections to arrive so hasn't started trying
2976      * to load migration state, and thus just aborts the migration
2977      * without exiting.
2978      */
2979     MigrateCommon args = {
2980         .start = {
2981             .hide_stderr = true,
2982         },
2983         .listen_uri = "defer",
2984         .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2985         .finish_hook = test_migrate_tls_x509_finish,
2986         .result = MIG_TEST_FAIL,
2987     };
2988     test_precopy_common(&args);
2989 }
2990 
2991 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2992 {
2993     MigrateCommon args = {
2994         .listen_uri = "defer",
2995         .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2996         .finish_hook = test_migrate_tls_x509_finish,
2997     };
2998     test_precopy_common(&args);
2999 }
3000 
3001 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
3002 {
3003     MigrateCommon args = {
3004         .start = {
3005             .hide_stderr = true,
3006         },
3007         .listen_uri = "defer",
3008         .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
3009         .finish_hook = test_migrate_tls_x509_finish,
3010         .result = MIG_TEST_FAIL,
3011     };
3012     test_precopy_common(&args);
3013 }
3014 #endif /* CONFIG_TASN1 */
3015 #endif /* CONFIG_GNUTLS */
3016 
3017 /*
3018  * This test does:
3019  *  source               target
3020  *                       migrate_incoming
3021  *     migrate
3022  *     migrate_cancel
3023  *                       launch another target
3024  *     migrate
3025  *
3026  *  And see that it works
3027  */
3028 static void test_multifd_tcp_cancel(void)
3029 {
3030     MigrateStart args = {
3031         .hide_stderr = true,
3032     };
3033     QTestState *from, *to, *to2;
3034 
3035     if (test_migrate_start(&from, &to, "defer", &args)) {
3036         return;
3037     }
3038 
3039     migrate_ensure_non_converge(from);
3040     migrate_prepare_for_dirty_mem(from);
3041 
3042     migrate_set_parameter_int(from, "multifd-channels", 16);
3043     migrate_set_parameter_int(to, "multifd-channels", 16);
3044 
3045     migrate_set_capability(from, "multifd", true);
3046     migrate_set_capability(to, "multifd", true);
3047 
3048     /* Start incoming migration from the 1st socket */
3049     migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
3050 
3051     /* Wait for the first serial output from the source */
3052     wait_for_serial("src_serial");
3053 
3054     migrate_qmp(from, to, NULL, NULL, "{}");
3055 
3056     migrate_wait_for_dirty_mem(from, to);
3057 
3058     migrate_cancel(from);
3059 
3060     /* Make sure QEMU process "to" exited */
3061     qtest_set_expected_status(to, EXIT_FAILURE);
3062     qtest_wait_qemu(to);
3063 
3064     args = (MigrateStart){
3065         .only_target = true,
3066     };
3067 
3068     if (test_migrate_start(&from, &to2, "defer", &args)) {
3069         return;
3070     }
3071 
3072     migrate_set_parameter_int(to2, "multifd-channels", 16);
3073 
3074     migrate_set_capability(to2, "multifd", true);
3075 
3076     /* Start incoming migration from the 1st socket */
3077     migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", "{}");
3078 
3079     wait_for_migration_status(from, "cancelled", NULL);
3080 
3081     migrate_ensure_non_converge(from);
3082 
3083     migrate_qmp(from, to2, NULL, NULL, "{}");
3084 
3085     migrate_wait_for_dirty_mem(from, to2);
3086 
3087     migrate_ensure_converge(from);
3088 
3089     wait_for_stop(from, &src_state);
3090     qtest_qmp_eventwait(to2, "RESUME");
3091 
3092     wait_for_serial("dest_serial");
3093     wait_for_migration_complete(from);
3094     test_migrate_end(from, to2, true);
3095 }
3096 
3097 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
3098 {
3099     qtest_qmp_assert_success(who,
3100                              "{ 'execute': 'calc-dirty-rate',"
3101                              "'arguments': { "
3102                              "'calc-time': %" PRIu64 ","
3103                              "'mode': 'dirty-ring' }}",
3104                              calc_time);
3105 }
3106 
3107 static QDict *query_dirty_rate(QTestState *who)
3108 {
3109     return qtest_qmp_assert_success_ref(who,
3110                                         "{ 'execute': 'query-dirty-rate' }");
3111 }
3112 
3113 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
3114 {
3115     qtest_qmp_assert_success(who,
3116                              "{ 'execute': 'set-vcpu-dirty-limit',"
3117                              "'arguments': { "
3118                              "'dirty-rate': %" PRIu64 " } }",
3119                              dirtyrate);
3120 }
3121 
3122 static void cancel_vcpu_dirty_limit(QTestState *who)
3123 {
3124     qtest_qmp_assert_success(who,
3125                              "{ 'execute': 'cancel-vcpu-dirty-limit' }");
3126 }
3127 
3128 static QDict *query_vcpu_dirty_limit(QTestState *who)
3129 {
3130     QDict *rsp;
3131 
3132     rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
3133     g_assert(!qdict_haskey(rsp, "error"));
3134     g_assert(qdict_haskey(rsp, "return"));
3135 
3136     return rsp;
3137 }
3138 
3139 static bool calc_dirtyrate_ready(QTestState *who)
3140 {
3141     QDict *rsp_return;
3142     gchar *status;
3143 
3144     rsp_return = query_dirty_rate(who);
3145     g_assert(rsp_return);
3146 
3147     status = g_strdup(qdict_get_str(rsp_return, "status"));
3148     g_assert(status);
3149 
3150     return g_strcmp0(status, "measuring");
3151 }
3152 
3153 static void wait_for_calc_dirtyrate_complete(QTestState *who,
3154                                              int64_t time_s)
3155 {
3156     int max_try_count = 10000;
3157     usleep(time_s * 1000000);
3158 
3159     while (!calc_dirtyrate_ready(who) && max_try_count--) {
3160         usleep(1000);
3161     }
3162 
3163     /*
3164      * Set the timeout with 10 s(max_try_count * 1000us),
3165      * if dirtyrate measurement not complete, fail test.
3166      */
3167     g_assert_cmpint(max_try_count, !=, 0);
3168 }
3169 
3170 static int64_t get_dirty_rate(QTestState *who)
3171 {
3172     QDict *rsp_return;
3173     gchar *status;
3174     QList *rates;
3175     const QListEntry *entry;
3176     QDict *rate;
3177     int64_t dirtyrate;
3178 
3179     rsp_return = query_dirty_rate(who);
3180     g_assert(rsp_return);
3181 
3182     status = g_strdup(qdict_get_str(rsp_return, "status"));
3183     g_assert(status);
3184     g_assert_cmpstr(status, ==, "measured");
3185 
3186     rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
3187     g_assert(rates && !qlist_empty(rates));
3188 
3189     entry = qlist_first(rates);
3190     g_assert(entry);
3191 
3192     rate = qobject_to(QDict, qlist_entry_obj(entry));
3193     g_assert(rate);
3194 
3195     dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
3196 
3197     qobject_unref(rsp_return);
3198     return dirtyrate;
3199 }
3200 
3201 static int64_t get_limit_rate(QTestState *who)
3202 {
3203     QDict *rsp_return;
3204     QList *rates;
3205     const QListEntry *entry;
3206     QDict *rate;
3207     int64_t dirtyrate;
3208 
3209     rsp_return = query_vcpu_dirty_limit(who);
3210     g_assert(rsp_return);
3211 
3212     rates = qdict_get_qlist(rsp_return, "return");
3213     g_assert(rates && !qlist_empty(rates));
3214 
3215     entry = qlist_first(rates);
3216     g_assert(entry);
3217 
3218     rate = qobject_to(QDict, qlist_entry_obj(entry));
3219     g_assert(rate);
3220 
3221     dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
3222 
3223     qobject_unref(rsp_return);
3224     return dirtyrate;
3225 }
3226 
3227 static QTestState *dirtylimit_start_vm(void)
3228 {
3229     QTestState *vm = NULL;
3230     g_autofree gchar *cmd = NULL;
3231 
3232     bootfile_create(tmpfs, false);
3233     cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
3234                           "-name dirtylimit-test,debug-threads=on "
3235                           "-m 150M -smp 1 "
3236                           "-serial file:%s/vm_serial "
3237                           "-drive file=%s,format=raw ",
3238                           tmpfs, bootpath);
3239 
3240     vm = qtest_init(cmd);
3241     return vm;
3242 }
3243 
3244 static void dirtylimit_stop_vm(QTestState *vm)
3245 {
3246     qtest_quit(vm);
3247     cleanup("vm_serial");
3248 }
3249 
3250 static void test_vcpu_dirty_limit(void)
3251 {
3252     QTestState *vm;
3253     int64_t origin_rate;
3254     int64_t quota_rate;
3255     int64_t rate ;
3256     int max_try_count = 20;
3257     int hit = 0;
3258 
3259     /* Start vm for vcpu dirtylimit test */
3260     vm = dirtylimit_start_vm();
3261 
3262     /* Wait for the first serial output from the vm*/
3263     wait_for_serial("vm_serial");
3264 
3265     /* Do dirtyrate measurement with calc time equals 1s */
3266     calc_dirty_rate(vm, 1);
3267 
3268     /* Sleep calc time and wait for calc dirtyrate complete */
3269     wait_for_calc_dirtyrate_complete(vm, 1);
3270 
3271     /* Query original dirty page rate */
3272     origin_rate = get_dirty_rate(vm);
3273 
3274     /* VM booted from bootsect should dirty memory steadily */
3275     assert(origin_rate != 0);
3276 
3277     /* Setup quota dirty page rate at half of origin */
3278     quota_rate = origin_rate / 2;
3279 
3280     /* Set dirtylimit */
3281     dirtylimit_set_all(vm, quota_rate);
3282 
3283     /*
3284      * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
3285      * works literally
3286      */
3287     g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
3288 
3289     /* Sleep a bit to check if it take effect */
3290     usleep(2000000);
3291 
3292     /*
3293      * Check if dirtylimit take effect realistically, set the
3294      * timeout with 20 s(max_try_count * 1s), if dirtylimit
3295      * doesn't take effect, fail test.
3296      */
3297     while (--max_try_count) {
3298         calc_dirty_rate(vm, 1);
3299         wait_for_calc_dirtyrate_complete(vm, 1);
3300         rate = get_dirty_rate(vm);
3301 
3302         /*
3303          * Assume hitting if current rate is less
3304          * than quota rate (within accepting error)
3305          */
3306         if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3307             hit = 1;
3308             break;
3309         }
3310     }
3311 
3312     g_assert_cmpint(hit, ==, 1);
3313 
3314     hit = 0;
3315     max_try_count = 20;
3316 
3317     /* Check if dirtylimit cancellation take effect */
3318     cancel_vcpu_dirty_limit(vm);
3319     while (--max_try_count) {
3320         calc_dirty_rate(vm, 1);
3321         wait_for_calc_dirtyrate_complete(vm, 1);
3322         rate = get_dirty_rate(vm);
3323 
3324         /*
3325          * Assume dirtylimit be canceled if current rate is
3326          * greater than quota rate (within accepting error)
3327          */
3328         if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3329             hit = 1;
3330             break;
3331         }
3332     }
3333 
3334     g_assert_cmpint(hit, ==, 1);
3335     dirtylimit_stop_vm(vm);
3336 }
3337 
3338 static void migrate_dirty_limit_wait_showup(QTestState *from,
3339                                             const int64_t period,
3340                                             const int64_t value)
3341 {
3342     /* Enable dirty limit capability */
3343     migrate_set_capability(from, "dirty-limit", true);
3344 
3345     /* Set dirty limit parameters */
3346     migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period);
3347     migrate_set_parameter_int(from, "vcpu-dirty-limit", value);
3348 
3349     /* Make sure migrate can't converge */
3350     migrate_ensure_non_converge(from);
3351 
3352     /* To check limit rate after precopy */
3353     migrate_set_capability(from, "pause-before-switchover", true);
3354 
3355     /* Wait for the serial output from the source */
3356     wait_for_serial("src_serial");
3357 }
3358 
3359 /*
3360  * This test does:
3361  *  source                          destination
3362  *  start vm
3363  *                                  start incoming vm
3364  *  migrate
3365  *  wait dirty limit to begin
3366  *  cancel migrate
3367  *  cancellation check
3368  *                                  restart incoming vm
3369  *  migrate
3370  *  wait dirty limit to begin
3371  *  wait pre-switchover event
3372  *  convergence condition check
3373  *
3374  * And see if dirty limit migration works correctly.
3375  * This test case involves many passes, so it runs in slow mode only.
3376  */
3377 static void test_migrate_dirty_limit(void)
3378 {
3379     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
3380     QTestState *from, *to;
3381     int64_t remaining;
3382     uint64_t throttle_us_per_full;
3383     /*
3384      * We want the test to be stable and as fast as possible.
3385      * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
3386      * so we need to decrease a bandwidth.
3387      */
3388     const int64_t dirtylimit_period = 1000, dirtylimit_value = 50;
3389     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
3390     const int64_t downtime_limit = 250; /* 250ms */
3391     /*
3392      * We migrate through unix-socket (> 500Mb/s).
3393      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
3394      * So, we can predict expected_threshold
3395      */
3396     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
3397     int max_try_count = 10;
3398     MigrateCommon args = {
3399         .start = {
3400             .hide_stderr = true,
3401             .use_dirty_ring = true,
3402         },
3403         .listen_uri = uri,
3404         .connect_uri = uri,
3405     };
3406 
3407     /* Start src, dst vm */
3408     if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
3409         return;
3410     }
3411 
3412     /* Prepare for dirty limit migration and wait src vm show up */
3413     migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value);
3414 
3415     /* Start migrate */
3416     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3417 
3418     /* Wait for dirty limit throttle begin */
3419     throttle_us_per_full = 0;
3420     while (throttle_us_per_full == 0) {
3421         throttle_us_per_full =
3422         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3423         usleep(100);
3424         g_assert_false(src_state.stop_seen);
3425     }
3426 
3427     /* Now cancel migrate and wait for dirty limit throttle switch off */
3428     migrate_cancel(from);
3429     wait_for_migration_status(from, "cancelled", NULL);
3430 
3431     /* Check if dirty limit throttle switched off, set timeout 1ms */
3432     do {
3433         throttle_us_per_full =
3434         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3435         usleep(100);
3436         g_assert_false(src_state.stop_seen);
3437     } while (throttle_us_per_full != 0 && --max_try_count);
3438 
3439     /* Assert dirty limit is not in service */
3440     g_assert_cmpint(throttle_us_per_full, ==, 0);
3441 
3442     args = (MigrateCommon) {
3443         .start = {
3444             .only_target = true,
3445             .use_dirty_ring = true,
3446         },
3447         .listen_uri = uri,
3448         .connect_uri = uri,
3449     };
3450 
3451     /* Restart dst vm, src vm already show up so we needn't wait anymore */
3452     if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
3453         return;
3454     }
3455 
3456     /* Start migrate */
3457     migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3458 
3459     /* Wait for dirty limit throttle begin */
3460     throttle_us_per_full = 0;
3461     while (throttle_us_per_full == 0) {
3462         throttle_us_per_full =
3463         read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3464         usleep(100);
3465         g_assert_false(src_state.stop_seen);
3466     }
3467 
3468     /*
3469      * The dirty limit rate should equals the return value of
3470      * query-vcpu-dirty-limit if dirty limit cap set
3471      */
3472     g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from));
3473 
3474     /* Now, we have tested if dirty limit works, let it converge */
3475     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
3476     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
3477 
3478     /*
3479      * Wait for pre-switchover status to check if migration
3480      * satisfy the convergence condition
3481      */
3482     wait_for_migration_status(from, "pre-switchover", NULL);
3483 
3484     remaining = read_ram_property_int(from, "remaining");
3485     g_assert_cmpint(remaining, <,
3486                     (expected_threshold + expected_threshold / 100));
3487 
3488     migrate_continue(from, "pre-switchover");
3489 
3490     qtest_qmp_eventwait(to, "RESUME");
3491 
3492     wait_for_serial("dest_serial");
3493     wait_for_migration_complete(from);
3494 
3495     test_migrate_end(from, to, true);
3496 }
3497 
3498 static bool kvm_dirty_ring_supported(void)
3499 {
3500 #if defined(__linux__) && defined(HOST_X86_64)
3501     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
3502 
3503     if (kvm_fd < 0) {
3504         return false;
3505     }
3506 
3507     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
3508     close(kvm_fd);
3509 
3510     /* We test with 4096 slots */
3511     if (ret < 4096) {
3512         return false;
3513     }
3514 
3515     return true;
3516 #else
3517     return false;
3518 #endif
3519 }
3520 
3521 int main(int argc, char **argv)
3522 {
3523     bool has_kvm, has_tcg;
3524     bool has_uffd, is_x86;
3525     const char *arch;
3526     g_autoptr(GError) err = NULL;
3527     const char *qemu_src = getenv(QEMU_ENV_SRC);
3528     const char *qemu_dst = getenv(QEMU_ENV_DST);
3529     int ret;
3530 
3531     g_test_init(&argc, &argv, NULL);
3532 
3533     /*
3534      * The default QTEST_QEMU_BINARY must always be provided because
3535      * that is what helpers use to query the accel type and
3536      * architecture.
3537      */
3538     if (qemu_src && qemu_dst) {
3539         g_test_message("Only one of %s, %s is allowed",
3540                        QEMU_ENV_SRC, QEMU_ENV_DST);
3541         exit(1);
3542     }
3543 
3544     has_kvm = qtest_has_accel("kvm");
3545     has_tcg = qtest_has_accel("tcg");
3546 
3547     if (!has_tcg && !has_kvm) {
3548         g_test_skip("No KVM or TCG accelerator available");
3549         return 0;
3550     }
3551 
3552     has_uffd = ufd_version_check();
3553     arch = qtest_get_arch();
3554     is_x86 = !strcmp(arch, "i386") || !strcmp(arch, "x86_64");
3555 
3556     /*
3557      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
3558      * is touchy due to race conditions on dirty bits (especially on PPC for
3559      * some reason)
3560      */
3561     if (g_str_equal(arch, "ppc64") &&
3562         (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
3563         g_test_message("Skipping test: kvm_hv not available");
3564         return g_test_run();
3565     }
3566 
3567     /*
3568      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
3569      * there until the problems are resolved
3570      */
3571     if (g_str_equal(arch, "s390x") && !has_kvm) {
3572         g_test_message("Skipping test: s390x host with KVM is required");
3573         return g_test_run();
3574     }
3575 
3576     tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
3577     if (!tmpfs) {
3578         g_test_message("Can't create temporary directory in %s: %s",
3579                        g_get_tmp_dir(), err->message);
3580     }
3581     g_assert(tmpfs);
3582 
3583     module_call_init(MODULE_INIT_QOM);
3584 
3585     if (is_x86) {
3586         migration_test_add("/migration/precopy/unix/suspend/live",
3587                            test_precopy_unix_suspend_live);
3588         migration_test_add("/migration/precopy/unix/suspend/notlive",
3589                            test_precopy_unix_suspend_notlive);
3590     }
3591 
3592     if (has_uffd) {
3593         migration_test_add("/migration/postcopy/plain", test_postcopy);
3594         migration_test_add("/migration/postcopy/recovery/plain",
3595                            test_postcopy_recovery);
3596         migration_test_add("/migration/postcopy/preempt/plain",
3597                            test_postcopy_preempt);
3598         migration_test_add("/migration/postcopy/preempt/recovery/plain",
3599                            test_postcopy_preempt_recovery);
3600         if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3601             migration_test_add("/migration/postcopy/compress/plain",
3602                                test_postcopy_compress);
3603             migration_test_add("/migration/postcopy/recovery/compress/plain",
3604                                test_postcopy_recovery_compress);
3605         }
3606 #ifndef _WIN32
3607         migration_test_add("/migration/postcopy/recovery/double-failures",
3608                            test_postcopy_recovery_double_fail);
3609 #endif /* _WIN32 */
3610         if (is_x86) {
3611             migration_test_add("/migration/postcopy/suspend",
3612                                test_postcopy_suspend);
3613         }
3614     }
3615 
3616     migration_test_add("/migration/bad_dest", test_baddest);
3617 #ifndef _WIN32
3618     if (!g_str_equal(arch, "s390x")) {
3619         migration_test_add("/migration/analyze-script", test_analyze_script);
3620     }
3621 #endif
3622     migration_test_add("/migration/precopy/unix/plain",
3623                        test_precopy_unix_plain);
3624     migration_test_add("/migration/precopy/unix/xbzrle",
3625                        test_precopy_unix_xbzrle);
3626     /*
3627      * Compression fails from time to time.
3628      * Put test here but don't enable it until everything is fixed.
3629      */
3630     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3631         migration_test_add("/migration/precopy/unix/compress/wait",
3632                            test_precopy_unix_compress);
3633         migration_test_add("/migration/precopy/unix/compress/nowait",
3634                            test_precopy_unix_compress_nowait);
3635     }
3636 
3637     migration_test_add("/migration/precopy/file",
3638                        test_precopy_file);
3639     migration_test_add("/migration/precopy/file/offset",
3640                        test_precopy_file_offset);
3641     migration_test_add("/migration/precopy/file/offset/bad",
3642                        test_precopy_file_offset_bad);
3643 
3644     /*
3645      * Our CI system has problems with shared memory.
3646      * Don't run this test until we find a workaround.
3647      */
3648     if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3649         migration_test_add("/migration/mode/reboot", test_mode_reboot);
3650     }
3651 
3652     migration_test_add("/migration/precopy/file/mapped-ram",
3653                        test_precopy_file_mapped_ram);
3654     migration_test_add("/migration/precopy/file/mapped-ram/live",
3655                        test_precopy_file_mapped_ram_live);
3656 
3657     migration_test_add("/migration/multifd/file/mapped-ram",
3658                        test_multifd_file_mapped_ram);
3659     migration_test_add("/migration/multifd/file/mapped-ram/live",
3660                        test_multifd_file_mapped_ram_live);
3661 
3662 #ifdef CONFIG_GNUTLS
3663     migration_test_add("/migration/precopy/unix/tls/psk",
3664                        test_precopy_unix_tls_psk);
3665 
3666     if (has_uffd) {
3667         /*
3668          * NOTE: psk test is enough for postcopy, as other types of TLS
3669          * channels are tested under precopy.  Here what we want to test is the
3670          * general postcopy path that has TLS channel enabled.
3671          */
3672         migration_test_add("/migration/postcopy/tls/psk",
3673                            test_postcopy_tls_psk);
3674         migration_test_add("/migration/postcopy/recovery/tls/psk",
3675                            test_postcopy_recovery_tls_psk);
3676         migration_test_add("/migration/postcopy/preempt/tls/psk",
3677                            test_postcopy_preempt_tls_psk);
3678         migration_test_add("/migration/postcopy/preempt/recovery/tls/psk",
3679                            test_postcopy_preempt_all);
3680     }
3681 #ifdef CONFIG_TASN1
3682     migration_test_add("/migration/precopy/unix/tls/x509/default-host",
3683                        test_precopy_unix_tls_x509_default_host);
3684     migration_test_add("/migration/precopy/unix/tls/x509/override-host",
3685                        test_precopy_unix_tls_x509_override_host);
3686 #endif /* CONFIG_TASN1 */
3687 #endif /* CONFIG_GNUTLS */
3688 
3689     migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
3690 
3691     migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
3692                        test_precopy_tcp_switchover_ack);
3693 
3694 #ifdef CONFIG_GNUTLS
3695     migration_test_add("/migration/precopy/tcp/tls/psk/match",
3696                        test_precopy_tcp_tls_psk_match);
3697     migration_test_add("/migration/precopy/tcp/tls/psk/mismatch",
3698                        test_precopy_tcp_tls_psk_mismatch);
3699 #ifdef CONFIG_TASN1
3700     migration_test_add("/migration/precopy/tcp/tls/x509/default-host",
3701                        test_precopy_tcp_tls_x509_default_host);
3702     migration_test_add("/migration/precopy/tcp/tls/x509/override-host",
3703                        test_precopy_tcp_tls_x509_override_host);
3704     migration_test_add("/migration/precopy/tcp/tls/x509/mismatch-host",
3705                        test_precopy_tcp_tls_x509_mismatch_host);
3706     migration_test_add("/migration/precopy/tcp/tls/x509/friendly-client",
3707                        test_precopy_tcp_tls_x509_friendly_client);
3708     migration_test_add("/migration/precopy/tcp/tls/x509/hostile-client",
3709                        test_precopy_tcp_tls_x509_hostile_client);
3710     migration_test_add("/migration/precopy/tcp/tls/x509/allow-anon-client",
3711                        test_precopy_tcp_tls_x509_allow_anon_client);
3712     migration_test_add("/migration/precopy/tcp/tls/x509/reject-anon-client",
3713                        test_precopy_tcp_tls_x509_reject_anon_client);
3714 #endif /* CONFIG_TASN1 */
3715 #endif /* CONFIG_GNUTLS */
3716 
3717     /* migration_test_add("/migration/ignore_shared", test_ignore_shared); */
3718 #ifndef _WIN32
3719     migration_test_add("/migration/precopy/fd/tcp",
3720                        test_migrate_precopy_fd_socket);
3721     migration_test_add("/migration/precopy/fd/file",
3722                        test_migrate_precopy_fd_file);
3723 #endif
3724     migration_test_add("/migration/validate_uuid", test_validate_uuid);
3725     migration_test_add("/migration/validate_uuid_error",
3726                        test_validate_uuid_error);
3727     migration_test_add("/migration/validate_uuid_src_not_set",
3728                        test_validate_uuid_src_not_set);
3729     migration_test_add("/migration/validate_uuid_dst_not_set",
3730                        test_validate_uuid_dst_not_set);
3731     migration_test_add("/migration/validate_uri/channels/both_set",
3732                        test_validate_uri_channels_both_set);
3733     migration_test_add("/migration/validate_uri/channels/none_set",
3734                        test_validate_uri_channels_none_set);
3735     /*
3736      * See explanation why this test is slow on function definition
3737      */
3738     if (g_test_slow()) {
3739         migration_test_add("/migration/auto_converge",
3740                            test_migrate_auto_converge);
3741         if (g_str_equal(arch, "x86_64") &&
3742             has_kvm && kvm_dirty_ring_supported()) {
3743             migration_test_add("/migration/dirty_limit",
3744                                test_migrate_dirty_limit);
3745         }
3746     }
3747     migration_test_add("/migration/multifd/tcp/uri/plain/none",
3748                        test_multifd_tcp_uri_none);
3749     migration_test_add("/migration/multifd/tcp/channels/plain/none",
3750                        test_multifd_tcp_channels_none);
3751     migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy",
3752                        test_multifd_tcp_zero_page_legacy);
3753     migration_test_add("/migration/multifd/tcp/plain/zero-page/none",
3754                        test_multifd_tcp_no_zero_page);
3755     migration_test_add("/migration/multifd/tcp/plain/cancel",
3756                        test_multifd_tcp_cancel);
3757     migration_test_add("/migration/multifd/tcp/plain/zlib",
3758                        test_multifd_tcp_zlib);
3759 #ifdef CONFIG_ZSTD
3760     migration_test_add("/migration/multifd/tcp/plain/zstd",
3761                        test_multifd_tcp_zstd);
3762 #endif
3763 #ifdef CONFIG_GNUTLS
3764     migration_test_add("/migration/multifd/tcp/tls/psk/match",
3765                        test_multifd_tcp_tls_psk_match);
3766     migration_test_add("/migration/multifd/tcp/tls/psk/mismatch",
3767                        test_multifd_tcp_tls_psk_mismatch);
3768 #ifdef CONFIG_TASN1
3769     migration_test_add("/migration/multifd/tcp/tls/x509/default-host",
3770                        test_multifd_tcp_tls_x509_default_host);
3771     migration_test_add("/migration/multifd/tcp/tls/x509/override-host",
3772                        test_multifd_tcp_tls_x509_override_host);
3773     migration_test_add("/migration/multifd/tcp/tls/x509/mismatch-host",
3774                        test_multifd_tcp_tls_x509_mismatch_host);
3775     migration_test_add("/migration/multifd/tcp/tls/x509/allow-anon-client",
3776                        test_multifd_tcp_tls_x509_allow_anon_client);
3777     migration_test_add("/migration/multifd/tcp/tls/x509/reject-anon-client",
3778                        test_multifd_tcp_tls_x509_reject_anon_client);
3779 #endif /* CONFIG_TASN1 */
3780 #endif /* CONFIG_GNUTLS */
3781 
3782     if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
3783         migration_test_add("/migration/dirty_ring",
3784                            test_precopy_unix_dirty_ring);
3785         migration_test_add("/migration/vcpu_dirty_limit",
3786                            test_vcpu_dirty_limit);
3787     }
3788 
3789     ret = g_test_run();
3790 
3791     g_assert_cmpint(ret, ==, 0);
3792 
3793     bootfile_delete();
3794     ret = rmdir(tmpfs);
3795     if (ret != 0) {
3796         g_test_message("unable to rmdir: path (%s): %s",
3797                        tmpfs, strerror(errno));
3798     }
3799     g_free(tmpfs);
3800 
3801     return ret;
3802 }
3803