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