xref: /openbmc/qemu/tests/qtest/migration-test.c (revision d6cd3ae0)
1 /*
2  * QTest testcase for migration
3  *
4  * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5  *   based on the vhost-user-test.c that is:
6  *      Copyright (c) 2014 Virtual Open Systems Sarl.
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  *
11  */
12 
13 #include "qemu/osdep.h"
14 
15 #include "libqtest.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/module.h"
19 #include "qemu/option.h"
20 #include "qemu/range.h"
21 #include "qemu/sockets.h"
22 #include "chardev/char.h"
23 #include "qapi/qapi-visit-sockets.h"
24 #include "qapi/qobject-input-visitor.h"
25 #include "qapi/qobject-output-visitor.h"
26 #include "crypto/tlscredspsk.h"
27 
28 #include "migration-helpers.h"
29 #include "tests/migration/migration-test.h"
30 #ifdef CONFIG_GNUTLS
31 # include "tests/unit/crypto-tls-psk-helpers.h"
32 # ifdef CONFIG_TASN1
33 #  include "tests/unit/crypto-tls-x509-helpers.h"
34 # endif /* CONFIG_TASN1 */
35 #endif /* CONFIG_GNUTLS */
36 
37 /* For dirty ring test; so far only x86_64 is supported */
38 #if defined(__linux__) && defined(HOST_X86_64)
39 #include "linux/kvm.h"
40 #endif
41 
42 /* TODO actually test the results and get rid of this */
43 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
44 
45 unsigned start_address;
46 unsigned end_address;
47 static bool uffd_feature_thread_id;
48 
49 /* A downtime where the test really should converge */
50 #define CONVERGE_DOWNTIME 1000
51 
52 #if defined(__linux__)
53 #include <sys/syscall.h>
54 #include <sys/vfs.h>
55 #endif
56 
57 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
58 #include <sys/eventfd.h>
59 #include <sys/ioctl.h>
60 #include <linux/userfaultfd.h>
61 
62 static bool ufd_version_check(void)
63 {
64     struct uffdio_api api_struct;
65     uint64_t ioctl_mask;
66 
67     int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
68 
69     if (ufd == -1) {
70         g_test_message("Skipping test: userfaultfd not available");
71         return false;
72     }
73 
74     api_struct.api = UFFD_API;
75     api_struct.features = 0;
76     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
77         g_test_message("Skipping test: UFFDIO_API failed");
78         return false;
79     }
80     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
81 
82     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
83                  (__u64)1 << _UFFDIO_UNREGISTER;
84     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
85         g_test_message("Skipping test: Missing userfault feature");
86         return false;
87     }
88 
89     return true;
90 }
91 
92 #else
93 static bool ufd_version_check(void)
94 {
95     g_test_message("Skipping test: Userfault not available (builtdtime)");
96     return false;
97 }
98 
99 #endif
100 
101 static const char *tmpfs;
102 
103 /* The boot file modifies memory area in [start_address, end_address)
104  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
105  */
106 #include "tests/migration/i386/a-b-bootblock.h"
107 #include "tests/migration/aarch64/a-b-kernel.h"
108 #include "tests/migration/s390x/a-b-bios.h"
109 
110 static void init_bootfile(const char *bootpath, void *content, size_t len)
111 {
112     FILE *bootfile = fopen(bootpath, "wb");
113 
114     g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
115     fclose(bootfile);
116 }
117 
118 /*
119  * Wait for some output in the serial output file,
120  * we get an 'A' followed by an endless string of 'B's
121  * but on the destination we won't have the A.
122  */
123 static void wait_for_serial(const char *side)
124 {
125     g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
126     FILE *serialfile = fopen(serialpath, "r");
127     const char *arch = qtest_get_arch();
128     int started = (strcmp(side, "src_serial") == 0 &&
129                    strcmp(arch, "ppc64") == 0) ? 0 : 1;
130 
131     do {
132         int readvalue = fgetc(serialfile);
133 
134         if (!started) {
135             /* SLOF prints its banner before starting test,
136              * to ignore it, mark the start of the test with '_',
137              * ignore all characters until this marker
138              */
139             switch (readvalue) {
140             case '_':
141                 started = 1;
142                 break;
143             case EOF:
144                 fseek(serialfile, 0, SEEK_SET);
145                 usleep(1000);
146                 break;
147             }
148             continue;
149         }
150         switch (readvalue) {
151         case 'A':
152             /* Fine */
153             break;
154 
155         case 'B':
156             /* It's alive! */
157             fclose(serialfile);
158             return;
159 
160         case EOF:
161             started = (strcmp(side, "src_serial") == 0 &&
162                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
163             fseek(serialfile, 0, SEEK_SET);
164             usleep(1000);
165             break;
166 
167         default:
168             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
169             g_assert_not_reached();
170         }
171     } while (true);
172 }
173 
174 /*
175  * It's tricky to use qemu's migration event capability with qtest,
176  * events suddenly appearing confuse the qmp()/hmp() responses.
177  */
178 
179 static int64_t read_ram_property_int(QTestState *who, const char *property)
180 {
181     QDict *rsp_return, *rsp_ram;
182     int64_t result;
183 
184     rsp_return = migrate_query_not_failed(who);
185     if (!qdict_haskey(rsp_return, "ram")) {
186         /* Still in setup */
187         result = 0;
188     } else {
189         rsp_ram = qdict_get_qdict(rsp_return, "ram");
190         result = qdict_get_try_int(rsp_ram, property, 0);
191     }
192     qobject_unref(rsp_return);
193     return result;
194 }
195 
196 static int64_t read_migrate_property_int(QTestState *who, const char *property)
197 {
198     QDict *rsp_return;
199     int64_t result;
200 
201     rsp_return = migrate_query_not_failed(who);
202     result = qdict_get_try_int(rsp_return, property, 0);
203     qobject_unref(rsp_return);
204     return result;
205 }
206 
207 static uint64_t get_migration_pass(QTestState *who)
208 {
209     return read_ram_property_int(who, "dirty-sync-count");
210 }
211 
212 static void read_blocktime(QTestState *who)
213 {
214     QDict *rsp_return;
215 
216     rsp_return = migrate_query_not_failed(who);
217     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
218     qobject_unref(rsp_return);
219 }
220 
221 static void wait_for_migration_pass(QTestState *who)
222 {
223     uint64_t initial_pass = get_migration_pass(who);
224     uint64_t pass;
225 
226     /* Wait for the 1st sync */
227     while (!got_stop && !initial_pass) {
228         usleep(1000);
229         initial_pass = get_migration_pass(who);
230     }
231 
232     do {
233         usleep(1000);
234         pass = get_migration_pass(who);
235     } while (pass == initial_pass && !got_stop);
236 }
237 
238 static void check_guests_ram(QTestState *who)
239 {
240     /* Our ASM test will have been incrementing one byte from each page from
241      * start_address to < end_address in order. This gives us a constraint
242      * that any page's byte should be equal or less than the previous pages
243      * byte (mod 256); and they should all be equal except for one transition
244      * at the point where we meet the incrementer. (We're running this with
245      * the guest stopped).
246      */
247     unsigned address;
248     uint8_t first_byte;
249     uint8_t last_byte;
250     bool hit_edge = false;
251     int bad = 0;
252 
253     qtest_memread(who, start_address, &first_byte, 1);
254     last_byte = first_byte;
255 
256     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
257          address += TEST_MEM_PAGE_SIZE)
258     {
259         uint8_t b;
260         qtest_memread(who, address, &b, 1);
261         if (b != last_byte) {
262             if (((b + 1) % 256) == last_byte && !hit_edge) {
263                 /* This is OK, the guest stopped at the point of
264                  * incrementing the previous page but didn't get
265                  * to us yet.
266                  */
267                 hit_edge = true;
268                 last_byte = b;
269             } else {
270                 bad++;
271                 if (bad <= 10) {
272                     fprintf(stderr, "Memory content inconsistency at %x"
273                             " first_byte = %x last_byte = %x current = %x"
274                             " hit_edge = %x\n",
275                             address, first_byte, last_byte, b, hit_edge);
276                 }
277             }
278         }
279     }
280     if (bad >= 10) {
281         fprintf(stderr, "and in another %d pages", bad - 10);
282     }
283     g_assert(bad == 0);
284 }
285 
286 static void cleanup(const char *filename)
287 {
288     g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
289 
290     unlink(path);
291 }
292 
293 static char *SocketAddress_to_str(SocketAddress *addr)
294 {
295     switch (addr->type) {
296     case SOCKET_ADDRESS_TYPE_INET:
297         return g_strdup_printf("tcp:%s:%s",
298                                addr->u.inet.host,
299                                addr->u.inet.port);
300     case SOCKET_ADDRESS_TYPE_UNIX:
301         return g_strdup_printf("unix:%s",
302                                addr->u.q_unix.path);
303     case SOCKET_ADDRESS_TYPE_FD:
304         return g_strdup_printf("fd:%s", addr->u.fd.str);
305     case SOCKET_ADDRESS_TYPE_VSOCK:
306         return g_strdup_printf("tcp:%s:%s",
307                                addr->u.vsock.cid,
308                                addr->u.vsock.port);
309     default:
310         return g_strdup("unknown address type");
311     }
312 }
313 
314 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
315 {
316     QDict *rsp;
317     char *result;
318     SocketAddressList *addrs;
319     Visitor *iv = NULL;
320     QObject *object;
321 
322     rsp = migrate_query(who);
323     object = qdict_get(rsp, parameter);
324 
325     iv = qobject_input_visitor_new(object);
326     visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
327     visit_free(iv);
328 
329     /* we are only using a single address */
330     result = SocketAddress_to_str(addrs->value);
331 
332     qapi_free_SocketAddressList(addrs);
333     qobject_unref(rsp);
334     return result;
335 }
336 
337 static long long migrate_get_parameter_int(QTestState *who,
338                                            const char *parameter)
339 {
340     QDict *rsp;
341     long long result;
342 
343     rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
344     result = qdict_get_int(rsp, parameter);
345     qobject_unref(rsp);
346     return result;
347 }
348 
349 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
350                                         long long value)
351 {
352     long long result;
353 
354     result = migrate_get_parameter_int(who, parameter);
355     g_assert_cmpint(result, ==, value);
356 }
357 
358 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
359                                       long long value)
360 {
361     QDict *rsp;
362 
363     rsp = qtest_qmp(who,
364                     "{ 'execute': 'migrate-set-parameters',"
365                     "'arguments': { %s: %lld } }",
366                     parameter, value);
367     g_assert(qdict_haskey(rsp, "return"));
368     qobject_unref(rsp);
369     migrate_check_parameter_int(who, parameter, value);
370 }
371 
372 static char *migrate_get_parameter_str(QTestState *who,
373                                        const char *parameter)
374 {
375     QDict *rsp;
376     char *result;
377 
378     rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
379     result = g_strdup(qdict_get_str(rsp, parameter));
380     qobject_unref(rsp);
381     return result;
382 }
383 
384 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
385                                         const char *value)
386 {
387     g_autofree char *result = migrate_get_parameter_str(who, parameter);
388     g_assert_cmpstr(result, ==, value);
389 }
390 
391 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
392                                       const char *value)
393 {
394     QDict *rsp;
395 
396     rsp = qtest_qmp(who,
397                     "{ 'execute': 'migrate-set-parameters',"
398                     "'arguments': { %s: %s } }",
399                     parameter, value);
400     g_assert(qdict_haskey(rsp, "return"));
401     qobject_unref(rsp);
402     migrate_check_parameter_str(who, parameter, value);
403 }
404 
405 static void migrate_pause(QTestState *who)
406 {
407     QDict *rsp;
408 
409     rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
410     qobject_unref(rsp);
411 }
412 
413 static void migrate_continue(QTestState *who, const char *state)
414 {
415     QDict *rsp;
416 
417     rsp = wait_command(who,
418                        "{ 'execute': 'migrate-continue',"
419                        "  'arguments': { 'state': %s } }",
420                        state);
421     qobject_unref(rsp);
422 }
423 
424 static void migrate_recover(QTestState *who, const char *uri)
425 {
426     QDict *rsp;
427 
428     rsp = wait_command(who,
429                        "{ 'execute': 'migrate-recover', "
430                        "  'id': 'recover-cmd', "
431                        "  'arguments': { 'uri': %s } }",
432                        uri);
433     qobject_unref(rsp);
434 }
435 
436 static void migrate_cancel(QTestState *who)
437 {
438     QDict *rsp;
439 
440     rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
441     qobject_unref(rsp);
442 }
443 
444 static void migrate_set_capability(QTestState *who, const char *capability,
445                                    bool value)
446 {
447     QDict *rsp;
448 
449     rsp = qtest_qmp(who,
450                     "{ 'execute': 'migrate-set-capabilities',"
451                     "'arguments': { "
452                     "'capabilities': [ { "
453                     "'capability': %s, 'state': %i } ] } }",
454                     capability, value);
455     g_assert(qdict_haskey(rsp, "return"));
456     qobject_unref(rsp);
457 }
458 
459 static void migrate_postcopy_start(QTestState *from, QTestState *to)
460 {
461     QDict *rsp;
462 
463     rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
464     qobject_unref(rsp);
465 
466     if (!got_stop) {
467         qtest_qmp_eventwait(from, "STOP");
468     }
469 
470     qtest_qmp_eventwait(to, "RESUME");
471 }
472 
473 typedef struct {
474     /*
475      * QTEST_LOG=1 may override this.  When QTEST_LOG=1, we always dump errors
476      * unconditionally, because it means the user would like to be verbose.
477      */
478     bool hide_stderr;
479     bool use_shmem;
480     /* only launch the target process */
481     bool only_target;
482     /* Use dirty ring if true; dirty logging otherwise */
483     bool use_dirty_ring;
484     const char *opts_source;
485     const char *opts_target;
486 } MigrateStart;
487 
488 static int test_migrate_start(QTestState **from, QTestState **to,
489                               const char *uri, MigrateStart *args)
490 {
491     g_autofree gchar *arch_source = NULL;
492     g_autofree gchar *arch_target = NULL;
493     g_autofree gchar *cmd_source = NULL;
494     g_autofree gchar *cmd_target = NULL;
495     const gchar *ignore_stderr;
496     g_autofree char *bootpath = NULL;
497     g_autofree char *shmem_opts = NULL;
498     g_autofree char *shmem_path = NULL;
499     const char *arch = qtest_get_arch();
500     const char *machine_opts = NULL;
501     const char *memory_size;
502 
503     if (args->use_shmem) {
504         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
505             g_test_skip("/dev/shm is not supported");
506             return -1;
507         }
508     }
509 
510     got_stop = false;
511     bootpath = g_strdup_printf("%s/bootsect", tmpfs);
512     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
513         /* the assembled x86 boot sector should be exactly one sector large */
514         assert(sizeof(x86_bootsect) == 512);
515         init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
516         memory_size = "150M";
517         arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
518         arch_target = g_strdup(arch_source);
519         start_address = X86_TEST_MEM_START;
520         end_address = X86_TEST_MEM_END;
521     } else if (g_str_equal(arch, "s390x")) {
522         init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
523         memory_size = "128M";
524         arch_source = g_strdup_printf("-bios %s", bootpath);
525         arch_target = g_strdup(arch_source);
526         start_address = S390_TEST_MEM_START;
527         end_address = S390_TEST_MEM_END;
528     } else if (strcmp(arch, "ppc64") == 0) {
529         machine_opts = "vsmt=8";
530         memory_size = "256M";
531         start_address = PPC_TEST_MEM_START;
532         end_address = PPC_TEST_MEM_END;
533         arch_source = g_strdup_printf("-nodefaults "
534                                       "-prom-env 'use-nvramrc?=true' -prom-env "
535                                       "'nvramrc=hex .\" _\" begin %x %x "
536                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
537                                       "until'", end_address, start_address);
538         arch_target = g_strdup("");
539     } else if (strcmp(arch, "aarch64") == 0) {
540         init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
541         machine_opts = "virt,gic-version=max";
542         memory_size = "150M";
543         arch_source = g_strdup_printf("-cpu max "
544                                       "-kernel %s",
545                                       bootpath);
546         arch_target = g_strdup(arch_source);
547         start_address = ARM_TEST_MEM_START;
548         end_address = ARM_TEST_MEM_END;
549 
550         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
551     } else {
552         g_assert_not_reached();
553     }
554 
555     if (!getenv("QTEST_LOG") && args->hide_stderr) {
556         ignore_stderr = "2>/dev/null";
557     } else {
558         ignore_stderr = "";
559     }
560 
561     if (args->use_shmem) {
562         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
563         shmem_opts = g_strdup_printf(
564             "-object memory-backend-file,id=mem0,size=%s"
565             ",mem-path=%s,share=on -numa node,memdev=mem0",
566             memory_size, shmem_path);
567     } else {
568         shmem_path = NULL;
569         shmem_opts = g_strdup("");
570     }
571 
572     cmd_source = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
573                                  "-name source,debug-threads=on "
574                                  "-m %s "
575                                  "-serial file:%s/src_serial "
576                                  "%s %s %s %s",
577                                  args->use_dirty_ring ?
578                                  ",dirty-ring-size=4096" : "",
579                                  machine_opts ? " -machine " : "",
580                                  machine_opts ? machine_opts : "",
581                                  memory_size, tmpfs,
582                                  arch_source, shmem_opts,
583                                  args->opts_source ? args->opts_source : "",
584                                  ignore_stderr);
585     if (!args->only_target) {
586         *from = qtest_init(cmd_source);
587     }
588 
589     cmd_target = g_strdup_printf("-accel kvm%s -accel tcg%s%s "
590                                  "-name target,debug-threads=on "
591                                  "-m %s "
592                                  "-serial file:%s/dest_serial "
593                                  "-incoming %s "
594                                  "%s %s %s %s",
595                                  args->use_dirty_ring ?
596                                  ",dirty-ring-size=4096" : "",
597                                  machine_opts ? " -machine " : "",
598                                  machine_opts ? machine_opts : "",
599                                  memory_size, tmpfs, uri,
600                                  arch_target, shmem_opts,
601                                  args->opts_target ? args->opts_target : "",
602                                  ignore_stderr);
603     *to = qtest_init(cmd_target);
604 
605     /*
606      * Remove shmem file immediately to avoid memory leak in test failed case.
607      * It's valid becase QEMU has already opened this file
608      */
609     if (args->use_shmem) {
610         unlink(shmem_path);
611     }
612 
613     return 0;
614 }
615 
616 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
617 {
618     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
619 
620     qtest_quit(from);
621 
622     if (test_dest) {
623         qtest_memread(to, start_address, &dest_byte_a, 1);
624 
625         /* Destination still running, wait for a byte to change */
626         do {
627             qtest_memread(to, start_address, &dest_byte_b, 1);
628             usleep(1000 * 10);
629         } while (dest_byte_a == dest_byte_b);
630 
631         qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
632 
633         /* With it stopped, check nothing changes */
634         qtest_memread(to, start_address, &dest_byte_c, 1);
635         usleep(1000 * 200);
636         qtest_memread(to, start_address, &dest_byte_d, 1);
637         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
638 
639         check_guests_ram(to);
640     }
641 
642     qtest_quit(to);
643 
644     cleanup("bootsect");
645     cleanup("migsocket");
646     cleanup("src_serial");
647     cleanup("dest_serial");
648 }
649 
650 #ifdef CONFIG_GNUTLS
651 struct TestMigrateTLSPSKData {
652     char *workdir;
653     char *workdiralt;
654     char *pskfile;
655     char *pskfilealt;
656 };
657 
658 static void *
659 test_migrate_tls_psk_start_common(QTestState *from,
660                                   QTestState *to,
661                                   bool mismatch)
662 {
663     struct TestMigrateTLSPSKData *data =
664         g_new0(struct TestMigrateTLSPSKData, 1);
665     QDict *rsp;
666 
667     data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
668     data->pskfile = g_strdup_printf("%s/%s", data->workdir,
669                                     QCRYPTO_TLS_CREDS_PSKFILE);
670     mkdir(data->workdir, 0700);
671     test_tls_psk_init(data->pskfile);
672 
673     if (mismatch) {
674         data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
675         data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
676                                            QCRYPTO_TLS_CREDS_PSKFILE);
677         mkdir(data->workdiralt, 0700);
678         test_tls_psk_init_alt(data->pskfilealt);
679     }
680 
681     rsp = wait_command(from,
682                        "{ 'execute': 'object-add',"
683                        "  'arguments': { 'qom-type': 'tls-creds-psk',"
684                        "                 'id': 'tlscredspsk0',"
685                        "                 'endpoint': 'client',"
686                        "                 'dir': %s,"
687                        "                 'username': 'qemu'} }",
688                        data->workdir);
689     qobject_unref(rsp);
690 
691     rsp = wait_command(to,
692                        "{ 'execute': 'object-add',"
693                        "  'arguments': { 'qom-type': 'tls-creds-psk',"
694                        "                 'id': 'tlscredspsk0',"
695                        "                 'endpoint': 'server',"
696                        "                 'dir': %s } }",
697                        mismatch ? data->workdiralt : data->workdir);
698     qobject_unref(rsp);
699 
700     migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
701     migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
702 
703     return data;
704 }
705 
706 static void *
707 test_migrate_tls_psk_start_match(QTestState *from,
708                                  QTestState *to)
709 {
710     return test_migrate_tls_psk_start_common(from, to, false);
711 }
712 
713 static void *
714 test_migrate_tls_psk_start_mismatch(QTestState *from,
715                                     QTestState *to)
716 {
717     return test_migrate_tls_psk_start_common(from, to, true);
718 }
719 
720 static void
721 test_migrate_tls_psk_finish(QTestState *from,
722                             QTestState *to,
723                             void *opaque)
724 {
725     struct TestMigrateTLSPSKData *data = opaque;
726 
727     test_tls_psk_cleanup(data->pskfile);
728     if (data->pskfilealt) {
729         test_tls_psk_cleanup(data->pskfilealt);
730     }
731     rmdir(data->workdir);
732     if (data->workdiralt) {
733         rmdir(data->workdiralt);
734     }
735 
736     g_free(data->workdiralt);
737     g_free(data->pskfilealt);
738     g_free(data->workdir);
739     g_free(data->pskfile);
740     g_free(data);
741 }
742 
743 #ifdef CONFIG_TASN1
744 typedef struct {
745     char *workdir;
746     char *keyfile;
747     char *cacert;
748     char *servercert;
749     char *serverkey;
750     char *clientcert;
751     char *clientkey;
752 } TestMigrateTLSX509Data;
753 
754 typedef struct {
755     bool verifyclient;
756     bool clientcert;
757     bool hostileclient;
758     bool authzclient;
759     const char *certhostname;
760     const char *certipaddr;
761 } TestMigrateTLSX509;
762 
763 static void *
764 test_migrate_tls_x509_start_common(QTestState *from,
765                                    QTestState *to,
766                                    TestMigrateTLSX509 *args)
767 {
768     TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
769     QDict *rsp;
770 
771     data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
772     data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
773 
774     data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
775     data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
776     data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
777     if (args->clientcert) {
778         data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
779         data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
780     }
781 
782     mkdir(data->workdir, 0700);
783 
784     test_tls_init(data->keyfile);
785     g_assert(link(data->keyfile, data->serverkey) == 0);
786     if (args->clientcert) {
787         g_assert(link(data->keyfile, data->clientkey) == 0);
788     }
789 
790     TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
791     if (args->clientcert) {
792         TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
793                                    args->hostileclient ?
794                                    QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
795                                    QCRYPTO_TLS_TEST_CLIENT_NAME,
796                                    data->clientcert);
797     }
798 
799     TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
800                                data->servercert,
801                                args->certhostname,
802                                args->certipaddr);
803 
804     rsp = wait_command(from,
805                        "{ 'execute': 'object-add',"
806                        "  'arguments': { 'qom-type': 'tls-creds-x509',"
807                        "                 'id': 'tlscredsx509client0',"
808                        "                 'endpoint': 'client',"
809                        "                 'dir': %s,"
810                        "                 'sanity-check': true,"
811                        "                 'verify-peer': true} }",
812                        data->workdir);
813     qobject_unref(rsp);
814     migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
815     if (args->certhostname) {
816         migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
817     }
818 
819     rsp = wait_command(to,
820                        "{ 'execute': 'object-add',"
821                        "  'arguments': { 'qom-type': 'tls-creds-x509',"
822                        "                 'id': 'tlscredsx509server0',"
823                        "                 'endpoint': 'server',"
824                        "                 'dir': %s,"
825                        "                 'sanity-check': true,"
826                        "                 'verify-peer': %i} }",
827                        data->workdir, args->verifyclient);
828     qobject_unref(rsp);
829     migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
830 
831     if (args->authzclient) {
832         rsp = wait_command(to,
833                            "{ 'execute': 'object-add',"
834                            "  'arguments': { 'qom-type': 'authz-simple',"
835                            "                 'id': 'tlsauthz0',"
836                            "                 'identity': %s} }",
837                            "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
838         migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
839     }
840 
841     return data;
842 }
843 
844 /*
845  * The normal case: match server's cert hostname against
846  * whatever host we were telling QEMU to connect to (if any)
847  */
848 static void *
849 test_migrate_tls_x509_start_default_host(QTestState *from,
850                                          QTestState *to)
851 {
852     TestMigrateTLSX509 args = {
853         .verifyclient = true,
854         .clientcert = true,
855         .certipaddr = "127.0.0.1"
856     };
857     return test_migrate_tls_x509_start_common(from, to, &args);
858 }
859 
860 /*
861  * The unusual case: the server's cert is different from
862  * the address we're telling QEMU to connect to (if any),
863  * so we must give QEMU an explicit hostname to validate
864  */
865 static void *
866 test_migrate_tls_x509_start_override_host(QTestState *from,
867                                           QTestState *to)
868 {
869     TestMigrateTLSX509 args = {
870         .verifyclient = true,
871         .clientcert = true,
872         .certhostname = "qemu.org",
873     };
874     return test_migrate_tls_x509_start_common(from, to, &args);
875 }
876 
877 /*
878  * The unusual case: the server's cert is different from
879  * the address we're telling QEMU to connect to, and so we
880  * expect the client to reject the server
881  */
882 static void *
883 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
884                                           QTestState *to)
885 {
886     TestMigrateTLSX509 args = {
887         .verifyclient = true,
888         .clientcert = true,
889         .certipaddr = "10.0.0.1",
890     };
891     return test_migrate_tls_x509_start_common(from, to, &args);
892 }
893 
894 static void *
895 test_migrate_tls_x509_start_friendly_client(QTestState *from,
896                                             QTestState *to)
897 {
898     TestMigrateTLSX509 args = {
899         .verifyclient = true,
900         .clientcert = true,
901         .authzclient = true,
902         .certipaddr = "127.0.0.1",
903     };
904     return test_migrate_tls_x509_start_common(from, to, &args);
905 }
906 
907 static void *
908 test_migrate_tls_x509_start_hostile_client(QTestState *from,
909                                            QTestState *to)
910 {
911     TestMigrateTLSX509 args = {
912         .verifyclient = true,
913         .clientcert = true,
914         .hostileclient = true,
915         .authzclient = true,
916         .certipaddr = "127.0.0.1",
917     };
918     return test_migrate_tls_x509_start_common(from, to, &args);
919 }
920 
921 /*
922  * The case with no client certificate presented,
923  * and no server verification
924  */
925 static void *
926 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
927                                               QTestState *to)
928 {
929     TestMigrateTLSX509 args = {
930         .certipaddr = "127.0.0.1",
931     };
932     return test_migrate_tls_x509_start_common(from, to, &args);
933 }
934 
935 /*
936  * The case with no client certificate presented,
937  * and server verification rejecting
938  */
939 static void *
940 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
941                                                QTestState *to)
942 {
943     TestMigrateTLSX509 args = {
944         .verifyclient = true,
945         .certipaddr = "127.0.0.1",
946     };
947     return test_migrate_tls_x509_start_common(from, to, &args);
948 }
949 
950 static void
951 test_migrate_tls_x509_finish(QTestState *from,
952                              QTestState *to,
953                              void *opaque)
954 {
955     TestMigrateTLSX509Data *data = opaque;
956 
957     test_tls_cleanup(data->keyfile);
958     unlink(data->cacert);
959     unlink(data->servercert);
960     unlink(data->serverkey);
961     unlink(data->clientcert);
962     unlink(data->clientkey);
963     rmdir(data->workdir);
964 
965     g_free(data->workdir);
966     g_free(data->keyfile);
967     g_free(data);
968 }
969 #endif /* CONFIG_TASN1 */
970 #endif /* CONFIG_GNUTLS */
971 
972 static int migrate_postcopy_prepare(QTestState **from_ptr,
973                                     QTestState **to_ptr,
974                                     MigrateStart *args)
975 {
976     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
977     QTestState *from, *to;
978 
979     if (test_migrate_start(&from, &to, uri, args)) {
980         return -1;
981     }
982 
983     migrate_set_capability(from, "postcopy-ram", true);
984     migrate_set_capability(to, "postcopy-ram", true);
985     migrate_set_capability(to, "postcopy-blocktime", true);
986 
987     /* We want to pick a speed slow enough that the test completes
988      * quickly, but that it doesn't complete precopy even on a slow
989      * machine, so also set the downtime.
990      */
991     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
992     migrate_set_parameter_int(from, "downtime-limit", 1);
993 
994     /* Wait for the first serial output from the source */
995     wait_for_serial("src_serial");
996 
997     migrate_qmp(from, uri, "{}");
998 
999     wait_for_migration_pass(from);
1000 
1001     *from_ptr = from;
1002     *to_ptr = to;
1003 
1004     return 0;
1005 }
1006 
1007 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
1008 {
1009     wait_for_migration_complete(from);
1010 
1011     /* Make sure we get at least one "B" on destination */
1012     wait_for_serial("dest_serial");
1013 
1014     if (uffd_feature_thread_id) {
1015         read_blocktime(to);
1016     }
1017 
1018     test_migrate_end(from, to, true);
1019 }
1020 
1021 static void test_postcopy(void)
1022 {
1023     MigrateStart args = {};
1024     QTestState *from, *to;
1025 
1026     if (migrate_postcopy_prepare(&from, &to, &args)) {
1027         return;
1028     }
1029     migrate_postcopy_start(from, to);
1030     migrate_postcopy_complete(from, to);
1031 }
1032 
1033 static void test_postcopy_recovery(void)
1034 {
1035     MigrateStart args = {
1036         .hide_stderr = true,
1037     };
1038     QTestState *from, *to;
1039     g_autofree char *uri = NULL;
1040 
1041     if (migrate_postcopy_prepare(&from, &to, &args)) {
1042         return;
1043     }
1044 
1045     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1046     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1047 
1048     /* Now we start the postcopy */
1049     migrate_postcopy_start(from, to);
1050 
1051     /*
1052      * Wait until postcopy is really started; we can only run the
1053      * migrate-pause command during a postcopy
1054      */
1055     wait_for_migration_status(from, "postcopy-active", NULL);
1056 
1057     /*
1058      * Manually stop the postcopy migration. This emulates a network
1059      * failure with the migration socket
1060      */
1061     migrate_pause(from);
1062 
1063     /*
1064      * Wait for destination side to reach postcopy-paused state.  The
1065      * migrate-recover command can only succeed if destination machine
1066      * is in the paused state
1067      */
1068     wait_for_migration_status(to, "postcopy-paused",
1069                               (const char * []) { "failed", "active",
1070                                                   "completed", NULL });
1071 
1072     /*
1073      * Create a new socket to emulate a new channel that is different
1074      * from the broken migration channel; tell the destination to
1075      * listen to the new port
1076      */
1077     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1078     migrate_recover(to, uri);
1079 
1080     /*
1081      * Try to rebuild the migration channel using the resume flag and
1082      * the newly created channel
1083      */
1084     wait_for_migration_status(from, "postcopy-paused",
1085                               (const char * []) { "failed", "active",
1086                                                   "completed", NULL });
1087     migrate_qmp(from, uri, "{'resume': true}");
1088 
1089     /* Restore the postcopy bandwidth to unlimited */
1090     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1091 
1092     migrate_postcopy_complete(from, to);
1093 }
1094 
1095 static void test_baddest(void)
1096 {
1097     MigrateStart args = {
1098         .hide_stderr = true
1099     };
1100     QTestState *from, *to;
1101 
1102     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1103         return;
1104     }
1105     migrate_qmp(from, "tcp:127.0.0.1:0", "{}");
1106     wait_for_migration_fail(from, false);
1107     test_migrate_end(from, to, false);
1108 }
1109 
1110 /*
1111  * A hook that runs after the src and dst QEMUs have been
1112  * created, but before the migration is started. This can
1113  * be used to set migration parameters and capabilities.
1114  *
1115  * Returns: NULL, or a pointer to opaque state to be
1116  *          later passed to the TestMigrateFinishHook
1117  */
1118 typedef void * (*TestMigrateStartHook)(QTestState *from,
1119                                        QTestState *to);
1120 
1121 /*
1122  * A hook that runs after the migration has finished,
1123  * regardless of whether it succeeded or failed, but
1124  * before QEMU has terminated (unless it self-terminated
1125  * due to migration error)
1126  *
1127  * @opaque is a pointer to state previously returned
1128  * by the TestMigrateStartHook if any, or NULL.
1129  */
1130 typedef void (*TestMigrateFinishHook)(QTestState *from,
1131                                       QTestState *to,
1132                                       void *opaque);
1133 
1134 typedef struct {
1135     /* Optional: fine tune start parameters */
1136     MigrateStart start;
1137 
1138     /* Required: the URI for the dst QEMU to listen on */
1139     const char *listen_uri;
1140 
1141     /*
1142      * Optional: the URI for the src QEMU to connect to
1143      * If NULL, then it will query the dst QEMU for its actual
1144      * listening address and use that as the connect address.
1145      * This allows for dynamically picking a free TCP port.
1146      */
1147     const char *connect_uri;
1148 
1149     /* Optional: callback to run at start to set migration parameters */
1150     TestMigrateStartHook start_hook;
1151     /* Optional: callback to run at finish to cleanup */
1152     TestMigrateFinishHook finish_hook;
1153 
1154     /*
1155      * Optional: normally we expect the migration process to complete.
1156      *
1157      * There can be a variety of reasons and stages in which failure
1158      * can happen during tests.
1159      *
1160      * If a failure is expected to happen at time of establishing
1161      * the connection, then MIG_TEST_FAIL will indicate that the dst
1162      * QEMU is expected to stay running and accept future migration
1163      * connections.
1164      *
1165      * If a failure is expected to happen while processing the
1166      * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
1167      * that the dst QEMU is expected to quit with non-zero exit status
1168      */
1169     enum {
1170         /* This test should succeed, the default */
1171         MIG_TEST_SUCCEED = 0,
1172         /* This test should fail, dest qemu should keep alive */
1173         MIG_TEST_FAIL,
1174         /* This test should fail, dest qemu should fail with abnormal status */
1175         MIG_TEST_FAIL_DEST_QUIT_ERR,
1176     } result;
1177 
1178     /* Optional: set number of migration passes to wait for */
1179     unsigned int iterations;
1180 } MigrateCommon;
1181 
1182 static void test_precopy_common(MigrateCommon *args)
1183 {
1184     QTestState *from, *to;
1185     void *data_hook = NULL;
1186 
1187     if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1188         return;
1189     }
1190 
1191     /*
1192      * We want to pick a speed slow enough that the test completes
1193      * quickly, but that it doesn't complete precopy even on a slow
1194      * machine, so also set the downtime.
1195      */
1196     /* 1 ms should make it not converge*/
1197     migrate_set_parameter_int(from, "downtime-limit", 1);
1198     /* 1GB/s */
1199     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1200 
1201     if (args->start_hook) {
1202         data_hook = args->start_hook(from, to);
1203     }
1204 
1205     /* Wait for the first serial output from the source */
1206     wait_for_serial("src_serial");
1207 
1208     if (!args->connect_uri) {
1209         g_autofree char *local_connect_uri =
1210             migrate_get_socket_address(to, "socket-address");
1211         migrate_qmp(from, local_connect_uri, "{}");
1212     } else {
1213         migrate_qmp(from, args->connect_uri, "{}");
1214     }
1215 
1216 
1217     if (args->result != MIG_TEST_SUCCEED) {
1218         bool allow_active = args->result == MIG_TEST_FAIL;
1219         wait_for_migration_fail(from, allow_active);
1220 
1221         if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1222             qtest_set_expected_status(to, 1);
1223         }
1224     } else {
1225         if (args->iterations) {
1226             while (args->iterations--) {
1227                 wait_for_migration_pass(from);
1228             }
1229         } else {
1230             wait_for_migration_pass(from);
1231         }
1232 
1233         migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1234 
1235         if (!got_stop) {
1236             qtest_qmp_eventwait(from, "STOP");
1237         }
1238 
1239         qtest_qmp_eventwait(to, "RESUME");
1240 
1241         wait_for_serial("dest_serial");
1242         wait_for_migration_complete(from);
1243     }
1244 
1245     if (args->finish_hook) {
1246         args->finish_hook(from, to, data_hook);
1247     }
1248 
1249     test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1250 }
1251 
1252 static void test_precopy_unix_plain(void)
1253 {
1254     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1255     MigrateCommon args = {
1256         .listen_uri = uri,
1257         .connect_uri = uri,
1258     };
1259 
1260     test_precopy_common(&args);
1261 }
1262 
1263 
1264 static void test_precopy_unix_dirty_ring(void)
1265 {
1266     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1267     MigrateCommon args = {
1268         .start = {
1269             .use_dirty_ring = true,
1270         },
1271         .listen_uri = uri,
1272         .connect_uri = uri,
1273     };
1274 
1275     test_precopy_common(&args);
1276 }
1277 
1278 #ifdef CONFIG_GNUTLS
1279 static void test_precopy_unix_tls_psk(void)
1280 {
1281     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1282     MigrateCommon args = {
1283         .connect_uri = uri,
1284         .listen_uri = uri,
1285         .start_hook = test_migrate_tls_psk_start_match,
1286         .finish_hook = test_migrate_tls_psk_finish,
1287     };
1288 
1289     test_precopy_common(&args);
1290 }
1291 
1292 #ifdef CONFIG_TASN1
1293 static void test_precopy_unix_tls_x509_default_host(void)
1294 {
1295     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1296     MigrateCommon args = {
1297         .start = {
1298             .hide_stderr = true,
1299         },
1300         .connect_uri = uri,
1301         .listen_uri = uri,
1302         .start_hook = test_migrate_tls_x509_start_default_host,
1303         .finish_hook = test_migrate_tls_x509_finish,
1304         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1305     };
1306 
1307     test_precopy_common(&args);
1308 }
1309 
1310 static void test_precopy_unix_tls_x509_override_host(void)
1311 {
1312     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1313     MigrateCommon args = {
1314         .connect_uri = uri,
1315         .listen_uri = uri,
1316         .start_hook = test_migrate_tls_x509_start_override_host,
1317         .finish_hook = test_migrate_tls_x509_finish,
1318     };
1319 
1320     test_precopy_common(&args);
1321 }
1322 #endif /* CONFIG_TASN1 */
1323 #endif /* CONFIG_GNUTLS */
1324 
1325 #if 0
1326 /* Currently upset on aarch64 TCG */
1327 static void test_ignore_shared(void)
1328 {
1329     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1330     QTestState *from, *to;
1331 
1332     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1333         return;
1334     }
1335 
1336     migrate_set_capability(from, "x-ignore-shared", true);
1337     migrate_set_capability(to, "x-ignore-shared", true);
1338 
1339     /* Wait for the first serial output from the source */
1340     wait_for_serial("src_serial");
1341 
1342     migrate_qmp(from, uri, "{}");
1343 
1344     wait_for_migration_pass(from);
1345 
1346     if (!got_stop) {
1347         qtest_qmp_eventwait(from, "STOP");
1348     }
1349 
1350     qtest_qmp_eventwait(to, "RESUME");
1351 
1352     wait_for_serial("dest_serial");
1353     wait_for_migration_complete(from);
1354 
1355     /* Check whether shared RAM has been really skipped */
1356     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1357 
1358     test_migrate_end(from, to, true);
1359 }
1360 #endif
1361 
1362 static void *
1363 test_migrate_xbzrle_start(QTestState *from,
1364                           QTestState *to)
1365 {
1366     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1367 
1368     migrate_set_capability(from, "xbzrle", true);
1369     migrate_set_capability(to, "xbzrle", true);
1370 
1371     return NULL;
1372 }
1373 
1374 static void test_precopy_unix_xbzrle(void)
1375 {
1376     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1377     MigrateCommon args = {
1378         .connect_uri = uri,
1379         .listen_uri = uri,
1380 
1381         .start_hook = test_migrate_xbzrle_start,
1382 
1383         .iterations = 2,
1384     };
1385 
1386     test_precopy_common(&args);
1387 }
1388 
1389 static void test_precopy_tcp_plain(void)
1390 {
1391     MigrateCommon args = {
1392         .listen_uri = "tcp:127.0.0.1:0",
1393     };
1394 
1395     test_precopy_common(&args);
1396 }
1397 
1398 #ifdef CONFIG_GNUTLS
1399 static void test_precopy_tcp_tls_psk_match(void)
1400 {
1401     MigrateCommon args = {
1402         .listen_uri = "tcp:127.0.0.1:0",
1403         .start_hook = test_migrate_tls_psk_start_match,
1404         .finish_hook = test_migrate_tls_psk_finish,
1405     };
1406 
1407     test_precopy_common(&args);
1408 }
1409 
1410 static void test_precopy_tcp_tls_psk_mismatch(void)
1411 {
1412     MigrateCommon args = {
1413         .start = {
1414             .hide_stderr = true,
1415         },
1416         .listen_uri = "tcp:127.0.0.1:0",
1417         .start_hook = test_migrate_tls_psk_start_mismatch,
1418         .finish_hook = test_migrate_tls_psk_finish,
1419         .result = MIG_TEST_FAIL,
1420     };
1421 
1422     test_precopy_common(&args);
1423 }
1424 
1425 #ifdef CONFIG_TASN1
1426 static void test_precopy_tcp_tls_x509_default_host(void)
1427 {
1428     MigrateCommon args = {
1429         .listen_uri = "tcp:127.0.0.1:0",
1430         .start_hook = test_migrate_tls_x509_start_default_host,
1431         .finish_hook = test_migrate_tls_x509_finish,
1432     };
1433 
1434     test_precopy_common(&args);
1435 }
1436 
1437 static void test_precopy_tcp_tls_x509_override_host(void)
1438 {
1439     MigrateCommon args = {
1440         .listen_uri = "tcp:127.0.0.1:0",
1441         .start_hook = test_migrate_tls_x509_start_override_host,
1442         .finish_hook = test_migrate_tls_x509_finish,
1443     };
1444 
1445     test_precopy_common(&args);
1446 }
1447 
1448 static void test_precopy_tcp_tls_x509_mismatch_host(void)
1449 {
1450     MigrateCommon args = {
1451         .start = {
1452             .hide_stderr = true,
1453         },
1454         .listen_uri = "tcp:127.0.0.1:0",
1455         .start_hook = test_migrate_tls_x509_start_mismatch_host,
1456         .finish_hook = test_migrate_tls_x509_finish,
1457         .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1458     };
1459 
1460     test_precopy_common(&args);
1461 }
1462 
1463 static void test_precopy_tcp_tls_x509_friendly_client(void)
1464 {
1465     MigrateCommon args = {
1466         .listen_uri = "tcp:127.0.0.1:0",
1467         .start_hook = test_migrate_tls_x509_start_friendly_client,
1468         .finish_hook = test_migrate_tls_x509_finish,
1469     };
1470 
1471     test_precopy_common(&args);
1472 }
1473 
1474 static void test_precopy_tcp_tls_x509_hostile_client(void)
1475 {
1476     MigrateCommon args = {
1477         .start = {
1478             .hide_stderr = true,
1479         },
1480         .listen_uri = "tcp:127.0.0.1:0",
1481         .start_hook = test_migrate_tls_x509_start_hostile_client,
1482         .finish_hook = test_migrate_tls_x509_finish,
1483         .result = MIG_TEST_FAIL,
1484     };
1485 
1486     test_precopy_common(&args);
1487 }
1488 
1489 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
1490 {
1491     MigrateCommon args = {
1492         .listen_uri = "tcp:127.0.0.1:0",
1493         .start_hook = test_migrate_tls_x509_start_allow_anon_client,
1494         .finish_hook = test_migrate_tls_x509_finish,
1495     };
1496 
1497     test_precopy_common(&args);
1498 }
1499 
1500 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
1501 {
1502     MigrateCommon args = {
1503         .start = {
1504             .hide_stderr = true,
1505         },
1506         .listen_uri = "tcp:127.0.0.1:0",
1507         .start_hook = test_migrate_tls_x509_start_reject_anon_client,
1508         .finish_hook = test_migrate_tls_x509_finish,
1509         .result = MIG_TEST_FAIL,
1510     };
1511 
1512     test_precopy_common(&args);
1513 }
1514 #endif /* CONFIG_TASN1 */
1515 #endif /* CONFIG_GNUTLS */
1516 
1517 static void *test_migrate_fd_start_hook(QTestState *from,
1518                                         QTestState *to)
1519 {
1520     QDict *rsp;
1521     int ret;
1522     int pair[2];
1523 
1524     /* Create two connected sockets for migration */
1525     ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1526     g_assert_cmpint(ret, ==, 0);
1527 
1528     /* Send the 1st socket to the target */
1529     rsp = wait_command_fd(to, pair[0],
1530                           "{ 'execute': 'getfd',"
1531                           "  'arguments': { 'fdname': 'fd-mig' }}");
1532     qobject_unref(rsp);
1533     close(pair[0]);
1534 
1535     /* Start incoming migration from the 1st socket */
1536     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1537                            "  'arguments': { 'uri': 'fd:fd-mig' }}");
1538     qobject_unref(rsp);
1539 
1540     /* Send the 2nd socket to the target */
1541     rsp = wait_command_fd(from, pair[1],
1542                           "{ 'execute': 'getfd',"
1543                           "  'arguments': { 'fdname': 'fd-mig' }}");
1544     qobject_unref(rsp);
1545     close(pair[1]);
1546 
1547     return NULL;
1548 }
1549 
1550 static void test_migrate_fd_finish_hook(QTestState *from,
1551                                         QTestState *to,
1552                                         void *opaque)
1553 {
1554     QDict *rsp;
1555     const char *error_desc;
1556 
1557     /* Test closing fds */
1558     /* We assume, that QEMU removes named fd from its list,
1559      * so this should fail */
1560     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1561                           "  'arguments': { 'fdname': 'fd-mig' }}");
1562     g_assert_true(qdict_haskey(rsp, "error"));
1563     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1564     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1565     qobject_unref(rsp);
1566 
1567     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1568                         "  'arguments': { 'fdname': 'fd-mig' }}");
1569     g_assert_true(qdict_haskey(rsp, "error"));
1570     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1571     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1572     qobject_unref(rsp);
1573 }
1574 
1575 static void test_migrate_fd_proto(void)
1576 {
1577     MigrateCommon args = {
1578         .listen_uri = "defer",
1579         .connect_uri = "fd:fd-mig",
1580         .start_hook = test_migrate_fd_start_hook,
1581         .finish_hook = test_migrate_fd_finish_hook
1582     };
1583     test_precopy_common(&args);
1584 }
1585 
1586 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1587 {
1588     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1589     QTestState *from, *to;
1590 
1591     if (test_migrate_start(&from, &to, uri, args)) {
1592         return;
1593     }
1594 
1595     /*
1596      * UUID validation is at the begin of migration. So, the main process of
1597      * migration is not interesting for us here. Thus, set huge downtime for
1598      * very fast migration.
1599      */
1600     migrate_set_parameter_int(from, "downtime-limit", 1000000);
1601     migrate_set_capability(from, "validate-uuid", true);
1602 
1603     /* Wait for the first serial output from the source */
1604     wait_for_serial("src_serial");
1605 
1606     migrate_qmp(from, uri, "{}");
1607 
1608     if (should_fail) {
1609         qtest_set_expected_status(to, 1);
1610         wait_for_migration_fail(from, true);
1611     } else {
1612         wait_for_migration_complete(from);
1613     }
1614 
1615     test_migrate_end(from, to, false);
1616 }
1617 
1618 static void test_validate_uuid(void)
1619 {
1620     MigrateStart args = {
1621         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1622         .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
1623     };
1624 
1625     do_test_validate_uuid(&args, false);
1626 }
1627 
1628 static void test_validate_uuid_error(void)
1629 {
1630     MigrateStart args = {
1631         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1632         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1633         .hide_stderr = true,
1634     };
1635 
1636     do_test_validate_uuid(&args, true);
1637 }
1638 
1639 static void test_validate_uuid_src_not_set(void)
1640 {
1641     MigrateStart args = {
1642         .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
1643         .hide_stderr = true,
1644     };
1645 
1646     do_test_validate_uuid(&args, false);
1647 }
1648 
1649 static void test_validate_uuid_dst_not_set(void)
1650 {
1651     MigrateStart args = {
1652         .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1653         .hide_stderr = true,
1654     };
1655 
1656     do_test_validate_uuid(&args, false);
1657 }
1658 
1659 static void test_migrate_auto_converge(void)
1660 {
1661     g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1662     MigrateStart args = {};
1663     QTestState *from, *to;
1664     int64_t remaining, percentage;
1665 
1666     /*
1667      * We want the test to be stable and as fast as possible.
1668      * E.g., with 1Gb/s bandwith migration may pass without throttling,
1669      * so we need to decrease a bandwidth.
1670      */
1671     const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1672     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1673     const int64_t downtime_limit = 250; /* 250ms */
1674     /*
1675      * We migrate through unix-socket (> 500Mb/s).
1676      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1677      * So, we can predict expected_threshold
1678      */
1679     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1680 
1681     if (test_migrate_start(&from, &to, uri, &args)) {
1682         return;
1683     }
1684 
1685     migrate_set_capability(from, "auto-converge", true);
1686     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1687     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1688     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1689 
1690     /*
1691      * Set the initial parameters so that the migration could not converge
1692      * without throttling.
1693      */
1694     migrate_set_parameter_int(from, "downtime-limit", 1);
1695     migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1696 
1697     /* To check remaining size after precopy */
1698     migrate_set_capability(from, "pause-before-switchover", true);
1699 
1700     /* Wait for the first serial output from the source */
1701     wait_for_serial("src_serial");
1702 
1703     migrate_qmp(from, uri, "{}");
1704 
1705     /* Wait for throttling begins */
1706     percentage = 0;
1707     while (percentage == 0) {
1708         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1709         usleep(100);
1710         g_assert_false(got_stop);
1711     }
1712     /* The first percentage of throttling should be equal to init_pct */
1713     g_assert_cmpint(percentage, ==, init_pct);
1714     /* Now, when we tested that throttling works, let it converge */
1715     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1716     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1717 
1718     /*
1719      * Wait for pre-switchover status to check last throttle percentage
1720      * and remaining. These values will be zeroed later
1721      */
1722     wait_for_migration_status(from, "pre-switchover", NULL);
1723 
1724     /* The final percentage of throttling shouldn't be greater than max_pct */
1725     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1726     g_assert_cmpint(percentage, <=, max_pct);
1727 
1728     remaining = read_ram_property_int(from, "remaining");
1729     g_assert_cmpint(remaining, <,
1730                     (expected_threshold + expected_threshold / 100));
1731 
1732     migrate_continue(from, "pre-switchover");
1733 
1734     qtest_qmp_eventwait(to, "RESUME");
1735 
1736     wait_for_serial("dest_serial");
1737     wait_for_migration_complete(from);
1738 
1739 
1740     test_migrate_end(from, to, true);
1741 }
1742 
1743 static void *
1744 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
1745                                               QTestState *to,
1746                                               const char *method)
1747 {
1748     QDict *rsp;
1749 
1750     migrate_set_parameter_int(from, "multifd-channels", 16);
1751     migrate_set_parameter_int(to, "multifd-channels", 16);
1752 
1753     migrate_set_parameter_str(from, "multifd-compression", method);
1754     migrate_set_parameter_str(to, "multifd-compression", method);
1755 
1756     migrate_set_capability(from, "multifd", true);
1757     migrate_set_capability(to, "multifd", true);
1758 
1759     /* Start incoming migration from the 1st socket */
1760     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1761                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1762     qobject_unref(rsp);
1763 
1764     return NULL;
1765 }
1766 
1767 static void *
1768 test_migrate_precopy_tcp_multifd_start(QTestState *from,
1769                                        QTestState *to)
1770 {
1771     return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1772 }
1773 
1774 static void *
1775 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
1776                                             QTestState *to)
1777 {
1778     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
1779 }
1780 
1781 #ifdef CONFIG_ZSTD
1782 static void *
1783 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
1784                                             QTestState *to)
1785 {
1786     return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
1787 }
1788 #endif /* CONFIG_ZSTD */
1789 
1790 static void test_multifd_tcp_none(void)
1791 {
1792     MigrateCommon args = {
1793         .listen_uri = "defer",
1794         .start_hook = test_migrate_precopy_tcp_multifd_start,
1795     };
1796     test_precopy_common(&args);
1797 }
1798 
1799 static void test_multifd_tcp_zlib(void)
1800 {
1801     MigrateCommon args = {
1802         .listen_uri = "defer",
1803         .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
1804     };
1805     test_precopy_common(&args);
1806 }
1807 
1808 #ifdef CONFIG_ZSTD
1809 static void test_multifd_tcp_zstd(void)
1810 {
1811     MigrateCommon args = {
1812         .listen_uri = "defer",
1813         .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
1814     };
1815     test_precopy_common(&args);
1816 }
1817 #endif
1818 
1819 #ifdef CONFIG_GNUTLS
1820 static void *
1821 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
1822                                              QTestState *to)
1823 {
1824     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1825     return test_migrate_tls_psk_start_match(from, to);
1826 }
1827 
1828 static void *
1829 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
1830                                                 QTestState *to)
1831 {
1832     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1833     return test_migrate_tls_psk_start_mismatch(from, to);
1834 }
1835 
1836 #ifdef CONFIG_TASN1
1837 static void *
1838 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
1839                                                  QTestState *to)
1840 {
1841     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1842     return test_migrate_tls_x509_start_default_host(from, to);
1843 }
1844 
1845 static void *
1846 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
1847                                                   QTestState *to)
1848 {
1849     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1850     return test_migrate_tls_x509_start_override_host(from, to);
1851 }
1852 
1853 static void *
1854 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
1855                                                   QTestState *to)
1856 {
1857     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1858     return test_migrate_tls_x509_start_mismatch_host(from, to);
1859 }
1860 
1861 static void *
1862 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
1863                                                       QTestState *to)
1864 {
1865     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1866     return test_migrate_tls_x509_start_allow_anon_client(from, to);
1867 }
1868 
1869 static void *
1870 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
1871                                                        QTestState *to)
1872 {
1873     test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
1874     return test_migrate_tls_x509_start_reject_anon_client(from, to);
1875 }
1876 #endif /* CONFIG_TASN1 */
1877 
1878 static void test_multifd_tcp_tls_psk_match(void)
1879 {
1880     MigrateCommon args = {
1881         .listen_uri = "defer",
1882         .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
1883         .finish_hook = test_migrate_tls_psk_finish,
1884     };
1885     test_precopy_common(&args);
1886 }
1887 
1888 static void test_multifd_tcp_tls_psk_mismatch(void)
1889 {
1890     MigrateCommon args = {
1891         .start = {
1892             .hide_stderr = true,
1893         },
1894         .listen_uri = "defer",
1895         .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
1896         .finish_hook = test_migrate_tls_psk_finish,
1897         .result = MIG_TEST_FAIL,
1898     };
1899     test_precopy_common(&args);
1900 }
1901 
1902 #ifdef CONFIG_TASN1
1903 static void test_multifd_tcp_tls_x509_default_host(void)
1904 {
1905     MigrateCommon args = {
1906         .listen_uri = "defer",
1907         .start_hook = test_migrate_multifd_tls_x509_start_default_host,
1908         .finish_hook = test_migrate_tls_x509_finish,
1909     };
1910     test_precopy_common(&args);
1911 }
1912 
1913 static void test_multifd_tcp_tls_x509_override_host(void)
1914 {
1915     MigrateCommon args = {
1916         .listen_uri = "defer",
1917         .start_hook = test_migrate_multifd_tls_x509_start_override_host,
1918         .finish_hook = test_migrate_tls_x509_finish,
1919     };
1920     test_precopy_common(&args);
1921 }
1922 
1923 static void test_multifd_tcp_tls_x509_mismatch_host(void)
1924 {
1925     /*
1926      * This has different behaviour to the non-multifd case.
1927      *
1928      * In non-multifd case when client aborts due to mismatched
1929      * cert host, the server has already started trying to load
1930      * migration state, and so it exits with I/O failure.
1931      *
1932      * In multifd case when client aborts due to mismatched
1933      * cert host, the server is still waiting for the other
1934      * multifd connections to arrive so hasn't started trying
1935      * to load migration state, and thus just aborts the migration
1936      * without exiting.
1937      */
1938     MigrateCommon args = {
1939         .start = {
1940             .hide_stderr = true,
1941         },
1942         .listen_uri = "defer",
1943         .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
1944         .finish_hook = test_migrate_tls_x509_finish,
1945         .result = MIG_TEST_FAIL,
1946     };
1947     test_precopy_common(&args);
1948 }
1949 
1950 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
1951 {
1952     MigrateCommon args = {
1953         .listen_uri = "defer",
1954         .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
1955         .finish_hook = test_migrate_tls_x509_finish,
1956     };
1957     test_precopy_common(&args);
1958 }
1959 
1960 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
1961 {
1962     MigrateCommon args = {
1963         .start = {
1964             .hide_stderr = true,
1965         },
1966         .listen_uri = "defer",
1967         .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
1968         .finish_hook = test_migrate_tls_x509_finish,
1969         .result = MIG_TEST_FAIL,
1970     };
1971     test_precopy_common(&args);
1972 }
1973 #endif /* CONFIG_TASN1 */
1974 #endif /* CONFIG_GNUTLS */
1975 
1976 /*
1977  * This test does:
1978  *  source               target
1979  *                       migrate_incoming
1980  *     migrate
1981  *     migrate_cancel
1982  *                       launch another target
1983  *     migrate
1984  *
1985  *  And see that it works
1986  */
1987 static void test_multifd_tcp_cancel(void)
1988 {
1989     MigrateStart args = {
1990         .hide_stderr = true,
1991     };
1992     QTestState *from, *to, *to2;
1993     QDict *rsp;
1994     g_autofree char *uri = NULL;
1995 
1996     if (test_migrate_start(&from, &to, "defer", &args)) {
1997         return;
1998     }
1999 
2000     /*
2001      * We want to pick a speed slow enough that the test completes
2002      * quickly, but that it doesn't complete precopy even on a slow
2003      * machine, so also set the downtime.
2004      */
2005     /* 1 ms should make it not converge*/
2006     migrate_set_parameter_int(from, "downtime-limit", 1);
2007     /* 300MB/s */
2008     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
2009 
2010     migrate_set_parameter_int(from, "multifd-channels", 16);
2011     migrate_set_parameter_int(to, "multifd-channels", 16);
2012 
2013     migrate_set_capability(from, "multifd", true);
2014     migrate_set_capability(to, "multifd", true);
2015 
2016     /* Start incoming migration from the 1st socket */
2017     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
2018                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2019     qobject_unref(rsp);
2020 
2021     /* Wait for the first serial output from the source */
2022     wait_for_serial("src_serial");
2023 
2024     uri = migrate_get_socket_address(to, "socket-address");
2025 
2026     migrate_qmp(from, uri, "{}");
2027 
2028     wait_for_migration_pass(from);
2029 
2030     migrate_cancel(from);
2031 
2032     args = (MigrateStart){
2033         .only_target = true,
2034     };
2035 
2036     if (test_migrate_start(&from, &to2, "defer", &args)) {
2037         return;
2038     }
2039 
2040     migrate_set_parameter_int(to2, "multifd-channels", 16);
2041 
2042     migrate_set_capability(to2, "multifd", true);
2043 
2044     /* Start incoming migration from the 1st socket */
2045     rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
2046                             "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
2047     qobject_unref(rsp);
2048 
2049     g_free(uri);
2050     uri = migrate_get_socket_address(to2, "socket-address");
2051 
2052     wait_for_migration_status(from, "cancelled", NULL);
2053 
2054     /* 300ms it should converge */
2055     migrate_set_parameter_int(from, "downtime-limit", 300);
2056     /* 1GB/s */
2057     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
2058 
2059     migrate_qmp(from, uri, "{}");
2060 
2061     wait_for_migration_pass(from);
2062 
2063     if (!got_stop) {
2064         qtest_qmp_eventwait(from, "STOP");
2065     }
2066     qtest_qmp_eventwait(to2, "RESUME");
2067 
2068     wait_for_serial("dest_serial");
2069     wait_for_migration_complete(from);
2070     test_migrate_end(from, to2, true);
2071 }
2072 
2073 static bool kvm_dirty_ring_supported(void)
2074 {
2075 #if defined(__linux__) && defined(HOST_X86_64)
2076     int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
2077 
2078     if (kvm_fd < 0) {
2079         return false;
2080     }
2081 
2082     ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
2083     close(kvm_fd);
2084 
2085     /* We test with 4096 slots */
2086     if (ret < 4096) {
2087         return false;
2088     }
2089 
2090     return true;
2091 #else
2092     return false;
2093 #endif
2094 }
2095 
2096 int main(int argc, char **argv)
2097 {
2098     char template[] = "/tmp/migration-test-XXXXXX";
2099     const bool has_kvm = qtest_has_accel("kvm");
2100     int ret;
2101 
2102     g_test_init(&argc, &argv, NULL);
2103 
2104     if (!ufd_version_check()) {
2105         return g_test_run();
2106     }
2107 
2108     /*
2109      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
2110      * is touchy due to race conditions on dirty bits (especially on PPC for
2111      * some reason)
2112      */
2113     if (g_str_equal(qtest_get_arch(), "ppc64") &&
2114         (!has_kvm || access("/sys/module/kvm_hv", F_OK))) {
2115         g_test_message("Skipping test: kvm_hv not available");
2116         return g_test_run();
2117     }
2118 
2119     /*
2120      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
2121      * there until the problems are resolved
2122      */
2123     if (g_str_equal(qtest_get_arch(), "s390x") && !has_kvm) {
2124         g_test_message("Skipping test: s390x host with KVM is required");
2125         return g_test_run();
2126     }
2127 
2128     tmpfs = mkdtemp(template);
2129     if (!tmpfs) {
2130         g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
2131     }
2132     g_assert(tmpfs);
2133 
2134     module_call_init(MODULE_INIT_QOM);
2135 
2136     qtest_add_func("/migration/postcopy/unix", test_postcopy);
2137     qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
2138     qtest_add_func("/migration/bad_dest", test_baddest);
2139     qtest_add_func("/migration/precopy/unix/plain", test_precopy_unix_plain);
2140     qtest_add_func("/migration/precopy/unix/xbzrle", test_precopy_unix_xbzrle);
2141 #ifdef CONFIG_GNUTLS
2142     qtest_add_func("/migration/precopy/unix/tls/psk",
2143                    test_precopy_unix_tls_psk);
2144 #ifdef CONFIG_TASN1
2145     qtest_add_func("/migration/precopy/unix/tls/x509/default-host",
2146                    test_precopy_unix_tls_x509_default_host);
2147     qtest_add_func("/migration/precopy/unix/tls/x509/override-host",
2148                    test_precopy_unix_tls_x509_override_host);
2149 #endif /* CONFIG_TASN1 */
2150 #endif /* CONFIG_GNUTLS */
2151 
2152     qtest_add_func("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
2153 #ifdef CONFIG_GNUTLS
2154     qtest_add_func("/migration/precopy/tcp/tls/psk/match",
2155                    test_precopy_tcp_tls_psk_match);
2156     qtest_add_func("/migration/precopy/tcp/tls/psk/mismatch",
2157                    test_precopy_tcp_tls_psk_mismatch);
2158 #ifdef CONFIG_TASN1
2159     qtest_add_func("/migration/precopy/tcp/tls/x509/default-host",
2160                    test_precopy_tcp_tls_x509_default_host);
2161     qtest_add_func("/migration/precopy/tcp/tls/x509/override-host",
2162                    test_precopy_tcp_tls_x509_override_host);
2163     qtest_add_func("/migration/precopy/tcp/tls/x509/mismatch-host",
2164                    test_precopy_tcp_tls_x509_mismatch_host);
2165     qtest_add_func("/migration/precopy/tcp/tls/x509/friendly-client",
2166                    test_precopy_tcp_tls_x509_friendly_client);
2167     qtest_add_func("/migration/precopy/tcp/tls/x509/hostile-client",
2168                    test_precopy_tcp_tls_x509_hostile_client);
2169     qtest_add_func("/migration/precopy/tcp/tls/x509/allow-anon-client",
2170                    test_precopy_tcp_tls_x509_allow_anon_client);
2171     qtest_add_func("/migration/precopy/tcp/tls/x509/reject-anon-client",
2172                    test_precopy_tcp_tls_x509_reject_anon_client);
2173 #endif /* CONFIG_TASN1 */
2174 #endif /* CONFIG_GNUTLS */
2175 
2176     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
2177     qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
2178     qtest_add_func("/migration/validate_uuid", test_validate_uuid);
2179     qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
2180     qtest_add_func("/migration/validate_uuid_src_not_set",
2181                    test_validate_uuid_src_not_set);
2182     qtest_add_func("/migration/validate_uuid_dst_not_set",
2183                    test_validate_uuid_dst_not_set);
2184 
2185     qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
2186     qtest_add_func("/migration/multifd/tcp/plain/none",
2187                    test_multifd_tcp_none);
2188     qtest_add_func("/migration/multifd/tcp/plain/cancel",
2189                    test_multifd_tcp_cancel);
2190     qtest_add_func("/migration/multifd/tcp/plain/zlib",
2191                    test_multifd_tcp_zlib);
2192 #ifdef CONFIG_ZSTD
2193     qtest_add_func("/migration/multifd/tcp/plain/zstd",
2194                    test_multifd_tcp_zstd);
2195 #endif
2196 #ifdef CONFIG_GNUTLS
2197     qtest_add_func("/migration/multifd/tcp/tls/psk/match",
2198                    test_multifd_tcp_tls_psk_match);
2199     qtest_add_func("/migration/multifd/tcp/tls/psk/mismatch",
2200                    test_multifd_tcp_tls_psk_mismatch);
2201 #ifdef CONFIG_TASN1
2202     qtest_add_func("/migration/multifd/tcp/tls/x509/default-host",
2203                    test_multifd_tcp_tls_x509_default_host);
2204     qtest_add_func("/migration/multifd/tcp/tls/x509/override-host",
2205                    test_multifd_tcp_tls_x509_override_host);
2206     qtest_add_func("/migration/multifd/tcp/tls/x509/mismatch-host",
2207                    test_multifd_tcp_tls_x509_mismatch_host);
2208     qtest_add_func("/migration/multifd/tcp/tls/x509/allow-anon-client",
2209                    test_multifd_tcp_tls_x509_allow_anon_client);
2210     qtest_add_func("/migration/multifd/tcp/tls/x509/reject-anon-client",
2211                    test_multifd_tcp_tls_x509_reject_anon_client);
2212 #endif /* CONFIG_TASN1 */
2213 #endif /* CONFIG_GNUTLS */
2214 
2215     if (kvm_dirty_ring_supported()) {
2216         qtest_add_func("/migration/dirty_ring",
2217                        test_precopy_unix_dirty_ring);
2218     }
2219 
2220     ret = g_test_run();
2221 
2222     g_assert_cmpint(ret, ==, 0);
2223 
2224     ret = rmdir(tmpfs);
2225     if (ret != 0) {
2226         g_test_message("unable to rmdir: path (%s): %s",
2227                        tmpfs, strerror(errno));
2228     }
2229 
2230     return ret;
2231 }
2232