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