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