xref: /openbmc/qemu/tests/qtest/migration-test.c (revision e6b5a071)
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 "libqos/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 
27 #include "migration-helpers.h"
28 #include "tests/migration/migration-test.h"
29 
30 /* TODO actually test the results and get rid of this */
31 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
32 
33 unsigned start_address;
34 unsigned end_address;
35 static bool uffd_feature_thread_id;
36 
37 /* A downtime where the test really should converge */
38 #define CONVERGE_DOWNTIME 1000
39 
40 #if defined(__linux__)
41 #include <sys/syscall.h>
42 #include <sys/vfs.h>
43 #endif
44 
45 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
46 #include <sys/eventfd.h>
47 #include <sys/ioctl.h>
48 #include <linux/userfaultfd.h>
49 
50 static bool ufd_version_check(void)
51 {
52     struct uffdio_api api_struct;
53     uint64_t ioctl_mask;
54 
55     int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
56 
57     if (ufd == -1) {
58         g_test_message("Skipping test: userfaultfd not available");
59         return false;
60     }
61 
62     api_struct.api = UFFD_API;
63     api_struct.features = 0;
64     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
65         g_test_message("Skipping test: UFFDIO_API failed");
66         return false;
67     }
68     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
69 
70     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
71                  (__u64)1 << _UFFDIO_UNREGISTER;
72     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
73         g_test_message("Skipping test: Missing userfault feature");
74         return false;
75     }
76 
77     return true;
78 }
79 
80 #else
81 static bool ufd_version_check(void)
82 {
83     g_test_message("Skipping test: Userfault not available (builtdtime)");
84     return false;
85 }
86 
87 #endif
88 
89 static const char *tmpfs;
90 
91 /* The boot file modifies memory area in [start_address, end_address)
92  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
93  */
94 #include "tests/migration/i386/a-b-bootblock.h"
95 #include "tests/migration/aarch64/a-b-kernel.h"
96 #include "tests/migration/s390x/a-b-bios.h"
97 
98 static void init_bootfile(const char *bootpath, void *content, size_t len)
99 {
100     FILE *bootfile = fopen(bootpath, "wb");
101 
102     g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
103     fclose(bootfile);
104 }
105 
106 /*
107  * Wait for some output in the serial output file,
108  * we get an 'A' followed by an endless string of 'B's
109  * but on the destination we won't have the A.
110  */
111 static void wait_for_serial(const char *side)
112 {
113     char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
114     FILE *serialfile = fopen(serialpath, "r");
115     const char *arch = qtest_get_arch();
116     int started = (strcmp(side, "src_serial") == 0 &&
117                    strcmp(arch, "ppc64") == 0) ? 0 : 1;
118 
119     g_free(serialpath);
120     do {
121         int readvalue = fgetc(serialfile);
122 
123         if (!started) {
124             /* SLOF prints its banner before starting test,
125              * to ignore it, mark the start of the test with '_',
126              * ignore all characters until this marker
127              */
128             switch (readvalue) {
129             case '_':
130                 started = 1;
131                 break;
132             case EOF:
133                 fseek(serialfile, 0, SEEK_SET);
134                 usleep(1000);
135                 break;
136             }
137             continue;
138         }
139         switch (readvalue) {
140         case 'A':
141             /* Fine */
142             break;
143 
144         case 'B':
145             /* It's alive! */
146             fclose(serialfile);
147             return;
148 
149         case EOF:
150             started = (strcmp(side, "src_serial") == 0 &&
151                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
152             fseek(serialfile, 0, SEEK_SET);
153             usleep(1000);
154             break;
155 
156         default:
157             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
158             g_assert_not_reached();
159         }
160     } while (true);
161 }
162 
163 /*
164  * It's tricky to use qemu's migration event capability with qtest,
165  * events suddenly appearing confuse the qmp()/hmp() responses.
166  */
167 
168 static int64_t read_ram_property_int(QTestState *who, const char *property)
169 {
170     QDict *rsp_return, *rsp_ram;
171     int64_t result;
172 
173     rsp_return = migrate_query(who);
174     if (!qdict_haskey(rsp_return, "ram")) {
175         /* Still in setup */
176         result = 0;
177     } else {
178         rsp_ram = qdict_get_qdict(rsp_return, "ram");
179         result = qdict_get_try_int(rsp_ram, property, 0);
180     }
181     qobject_unref(rsp_return);
182     return result;
183 }
184 
185 static int64_t read_migrate_property_int(QTestState *who, const char *property)
186 {
187     QDict *rsp_return;
188     int64_t result;
189 
190     rsp_return = migrate_query(who);
191     result = qdict_get_try_int(rsp_return, property, 0);
192     qobject_unref(rsp_return);
193     return result;
194 }
195 
196 static uint64_t get_migration_pass(QTestState *who)
197 {
198     return read_ram_property_int(who, "dirty-sync-count");
199 }
200 
201 static void read_blocktime(QTestState *who)
202 {
203     QDict *rsp_return;
204 
205     rsp_return = migrate_query(who);
206     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
207     qobject_unref(rsp_return);
208 }
209 
210 static void wait_for_migration_pass(QTestState *who)
211 {
212     uint64_t initial_pass = get_migration_pass(who);
213     uint64_t pass;
214 
215     /* Wait for the 1st sync */
216     while (!got_stop && !initial_pass) {
217         usleep(1000);
218         initial_pass = get_migration_pass(who);
219     }
220 
221     do {
222         usleep(1000);
223         pass = get_migration_pass(who);
224     } while (pass == initial_pass && !got_stop);
225 }
226 
227 static void check_guests_ram(QTestState *who)
228 {
229     /* Our ASM test will have been incrementing one byte from each page from
230      * start_address to < end_address in order. This gives us a constraint
231      * that any page's byte should be equal or less than the previous pages
232      * byte (mod 256); and they should all be equal except for one transition
233      * at the point where we meet the incrementer. (We're running this with
234      * the guest stopped).
235      */
236     unsigned address;
237     uint8_t first_byte;
238     uint8_t last_byte;
239     bool hit_edge = false;
240     int bad = 0;
241 
242     qtest_memread(who, start_address, &first_byte, 1);
243     last_byte = first_byte;
244 
245     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
246          address += TEST_MEM_PAGE_SIZE)
247     {
248         uint8_t b;
249         qtest_memread(who, address, &b, 1);
250         if (b != last_byte) {
251             if (((b + 1) % 256) == last_byte && !hit_edge) {
252                 /* This is OK, the guest stopped at the point of
253                  * incrementing the previous page but didn't get
254                  * to us yet.
255                  */
256                 hit_edge = true;
257                 last_byte = b;
258             } else {
259                 bad++;
260                 if (bad <= 10) {
261                     fprintf(stderr, "Memory content inconsistency at %x"
262                             " first_byte = %x last_byte = %x current = %x"
263                             " hit_edge = %x\n",
264                             address, first_byte, last_byte, b, hit_edge);
265                 }
266             }
267         }
268     }
269     if (bad >= 10) {
270         fprintf(stderr, "and in another %d pages", bad - 10);
271     }
272     g_assert(bad == 0);
273 }
274 
275 static void cleanup(const char *filename)
276 {
277     char *path = g_strdup_printf("%s/%s", tmpfs, filename);
278 
279     unlink(path);
280     g_free(path);
281 }
282 
283 static char *SocketAddress_to_str(SocketAddress *addr)
284 {
285     switch (addr->type) {
286     case SOCKET_ADDRESS_TYPE_INET:
287         return g_strdup_printf("tcp:%s:%s",
288                                addr->u.inet.host,
289                                addr->u.inet.port);
290     case SOCKET_ADDRESS_TYPE_UNIX:
291         return g_strdup_printf("unix:%s",
292                                addr->u.q_unix.path);
293     case SOCKET_ADDRESS_TYPE_FD:
294         return g_strdup_printf("fd:%s", addr->u.fd.str);
295     case SOCKET_ADDRESS_TYPE_VSOCK:
296         return g_strdup_printf("tcp:%s:%s",
297                                addr->u.vsock.cid,
298                                addr->u.vsock.port);
299     default:
300         return g_strdup("unknown address type");
301     }
302 }
303 
304 static char *migrate_get_socket_address(QTestState *who, const char *parameter)
305 {
306     QDict *rsp;
307     char *result;
308     SocketAddressList *addrs;
309     Visitor *iv = NULL;
310     QObject *object;
311 
312     rsp = migrate_query(who);
313     object = qdict_get(rsp, parameter);
314 
315     iv = qobject_input_visitor_new(object);
316     visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
317     visit_free(iv);
318 
319     /* we are only using a single address */
320     result = SocketAddress_to_str(addrs->value);
321 
322     qapi_free_SocketAddressList(addrs);
323     qobject_unref(rsp);
324     return result;
325 }
326 
327 static long long migrate_get_parameter_int(QTestState *who,
328                                            const char *parameter)
329 {
330     QDict *rsp;
331     long long result;
332 
333     rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
334     result = qdict_get_int(rsp, parameter);
335     qobject_unref(rsp);
336     return result;
337 }
338 
339 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
340                                         long long value)
341 {
342     long long result;
343 
344     result = migrate_get_parameter_int(who, parameter);
345     g_assert_cmpint(result, ==, value);
346 }
347 
348 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
349                                       long long value)
350 {
351     QDict *rsp;
352 
353     rsp = qtest_qmp(who,
354                     "{ 'execute': 'migrate-set-parameters',"
355                     "'arguments': { %s: %lld } }",
356                     parameter, value);
357     g_assert(qdict_haskey(rsp, "return"));
358     qobject_unref(rsp);
359     migrate_check_parameter_int(who, parameter, value);
360 }
361 
362 static char *migrate_get_parameter_str(QTestState *who,
363                                        const char *parameter)
364 {
365     QDict *rsp;
366     char *result;
367 
368     rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
369     result = g_strdup(qdict_get_str(rsp, parameter));
370     qobject_unref(rsp);
371     return result;
372 }
373 
374 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
375                                         const char *value)
376 {
377     char *result;
378 
379     result = migrate_get_parameter_str(who, parameter);
380     g_assert_cmpstr(result, ==, value);
381     g_free(result);
382 }
383 
384 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
385                                       const char *value)
386 {
387     QDict *rsp;
388 
389     rsp = qtest_qmp(who,
390                     "{ 'execute': 'migrate-set-parameters',"
391                     "'arguments': { %s: %s } }",
392                     parameter, value);
393     g_assert(qdict_haskey(rsp, "return"));
394     qobject_unref(rsp);
395     migrate_check_parameter_str(who, parameter, value);
396 }
397 
398 static void migrate_pause(QTestState *who)
399 {
400     QDict *rsp;
401 
402     rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
403     qobject_unref(rsp);
404 }
405 
406 static void migrate_continue(QTestState *who, const char *state)
407 {
408     QDict *rsp;
409 
410     rsp = wait_command(who,
411                        "{ 'execute': 'migrate-continue',"
412                        "  'arguments': { 'state': %s } }",
413                        state);
414     qobject_unref(rsp);
415 }
416 
417 static void migrate_recover(QTestState *who, const char *uri)
418 {
419     QDict *rsp;
420 
421     rsp = wait_command(who,
422                        "{ 'execute': 'migrate-recover', "
423                        "  'id': 'recover-cmd', "
424                        "  'arguments': { 'uri': %s } }",
425                        uri);
426     qobject_unref(rsp);
427 }
428 
429 static void migrate_cancel(QTestState *who)
430 {
431     QDict *rsp;
432 
433     rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
434     qobject_unref(rsp);
435 }
436 
437 static void migrate_set_capability(QTestState *who, const char *capability,
438                                    bool value)
439 {
440     QDict *rsp;
441 
442     rsp = qtest_qmp(who,
443                     "{ 'execute': 'migrate-set-capabilities',"
444                     "'arguments': { "
445                     "'capabilities': [ { "
446                     "'capability': %s, 'state': %i } ] } }",
447                     capability, value);
448     g_assert(qdict_haskey(rsp, "return"));
449     qobject_unref(rsp);
450 }
451 
452 static void migrate_postcopy_start(QTestState *from, QTestState *to)
453 {
454     QDict *rsp;
455 
456     rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
457     qobject_unref(rsp);
458 
459     if (!got_stop) {
460         qtest_qmp_eventwait(from, "STOP");
461     }
462 
463     qtest_qmp_eventwait(to, "RESUME");
464 }
465 
466 typedef struct {
467     bool hide_stderr;
468     bool use_shmem;
469     /* only launch the target process */
470     bool only_target;
471     char *opts_source;
472     char *opts_target;
473 } MigrateStart;
474 
475 static MigrateStart *migrate_start_new(void)
476 {
477     MigrateStart *args = g_new0(MigrateStart, 1);
478 
479     args->opts_source = g_strdup("");
480     args->opts_target = g_strdup("");
481     return args;
482 }
483 
484 static void migrate_start_destroy(MigrateStart *args)
485 {
486     g_free(args->opts_source);
487     g_free(args->opts_target);
488     g_free(args);
489 }
490 
491 static int test_migrate_start(QTestState **from, QTestState **to,
492                               const char *uri, MigrateStart *args)
493 {
494     gchar *arch_source, *arch_target;
495     gchar *cmd_source, *cmd_target;
496     const gchar *ignore_stderr;
497     char *bootpath = NULL;
498     char *shmem_opts;
499     char *shmem_path;
500     const char *arch = qtest_get_arch();
501     const char *machine_opts = NULL;
502     const char *memory_size;
503     int ret = 0;
504 
505     if (args->use_shmem) {
506         if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
507             g_test_skip("/dev/shm is not supported");
508             ret = -1;
509             goto out;
510         }
511     }
512 
513     got_stop = false;
514     bootpath = g_strdup_printf("%s/bootsect", tmpfs);
515     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
516         /* the assembled x86 boot sector should be exactly one sector large */
517         assert(sizeof(x86_bootsect) == 512);
518         init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
519         memory_size = "150M";
520         arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
521         arch_target = g_strdup(arch_source);
522         start_address = X86_TEST_MEM_START;
523         end_address = X86_TEST_MEM_END;
524     } else if (g_str_equal(arch, "s390x")) {
525         init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
526         memory_size = "128M";
527         arch_source = g_strdup_printf("-bios %s", bootpath);
528         arch_target = g_strdup(arch_source);
529         start_address = S390_TEST_MEM_START;
530         end_address = S390_TEST_MEM_END;
531     } else if (strcmp(arch, "ppc64") == 0) {
532         machine_opts = "vsmt=8";
533         memory_size = "256M";
534         start_address = PPC_TEST_MEM_START;
535         end_address = PPC_TEST_MEM_END;
536         arch_source = g_strdup_printf("-nodefaults "
537                                       "-prom-env 'use-nvramrc?=true' -prom-env "
538                                       "'nvramrc=hex .\" _\" begin %x %x "
539                                       "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
540                                       "until'", end_address, start_address);
541         arch_target = g_strdup("");
542     } else if (strcmp(arch, "aarch64") == 0) {
543         init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
544         machine_opts = "virt,gic-version=max";
545         memory_size = "150M";
546         arch_source = g_strdup_printf("-cpu max "
547                                       "-kernel %s",
548                                       bootpath);
549         arch_target = g_strdup(arch_source);
550         start_address = ARM_TEST_MEM_START;
551         end_address = ARM_TEST_MEM_END;
552 
553         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
554     } else {
555         g_assert_not_reached();
556     }
557 
558     g_free(bootpath);
559 
560     if (args->hide_stderr) {
561         ignore_stderr = "2>/dev/null";
562     } else {
563         ignore_stderr = "";
564     }
565 
566     if (args->use_shmem) {
567         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
568         shmem_opts = g_strdup_printf(
569             "-object memory-backend-file,id=mem0,size=%s"
570             ",mem-path=%s,share=on -numa node,memdev=mem0",
571             memory_size, shmem_path);
572     } else {
573         shmem_path = NULL;
574         shmem_opts = g_strdup("");
575     }
576 
577     cmd_source = g_strdup_printf("-accel kvm -accel tcg%s%s "
578                                  "-name source,debug-threads=on "
579                                  "-m %s "
580                                  "-serial file:%s/src_serial "
581                                  "%s %s %s %s",
582                                  machine_opts ? " -machine " : "",
583                                  machine_opts ? machine_opts : "",
584                                  memory_size, tmpfs,
585                                  arch_source, shmem_opts, args->opts_source,
586                                  ignore_stderr);
587     g_free(arch_source);
588     if (!args->only_target) {
589         *from = qtest_init(cmd_source);
590     }
591     g_free(cmd_source);
592 
593     cmd_target = g_strdup_printf("-accel kvm -accel tcg%s%s "
594                                  "-name target,debug-threads=on "
595                                  "-m %s "
596                                  "-serial file:%s/dest_serial "
597                                  "-incoming %s "
598                                  "%s %s %s %s",
599                                  machine_opts ? " -machine " : "",
600                                  machine_opts ? machine_opts : "",
601                                  memory_size, tmpfs, uri,
602                                  arch_target, shmem_opts,
603                                  args->opts_target, ignore_stderr);
604     g_free(arch_target);
605     *to = qtest_init(cmd_target);
606     g_free(cmd_target);
607 
608     g_free(shmem_opts);
609     /*
610      * Remove shmem file immediately to avoid memory leak in test failed case.
611      * It's valid becase QEMU has already opened this file
612      */
613     if (args->use_shmem) {
614         unlink(shmem_path);
615         g_free(shmem_path);
616     }
617 
618 out:
619     migrate_start_destroy(args);
620     return ret;
621 }
622 
623 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
624 {
625     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
626 
627     qtest_quit(from);
628 
629     if (test_dest) {
630         qtest_memread(to, start_address, &dest_byte_a, 1);
631 
632         /* Destination still running, wait for a byte to change */
633         do {
634             qtest_memread(to, start_address, &dest_byte_b, 1);
635             usleep(1000 * 10);
636         } while (dest_byte_a == dest_byte_b);
637 
638         qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
639 
640         /* With it stopped, check nothing changes */
641         qtest_memread(to, start_address, &dest_byte_c, 1);
642         usleep(1000 * 200);
643         qtest_memread(to, start_address, &dest_byte_d, 1);
644         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
645 
646         check_guests_ram(to);
647     }
648 
649     qtest_quit(to);
650 
651     cleanup("bootsect");
652     cleanup("migsocket");
653     cleanup("src_serial");
654     cleanup("dest_serial");
655 }
656 
657 static void deprecated_set_downtime(QTestState *who, const double value)
658 {
659     QDict *rsp;
660 
661     rsp = qtest_qmp(who,
662                     "{ 'execute': 'migrate_set_downtime',"
663                     " 'arguments': { 'value': %f } }", value);
664     g_assert(qdict_haskey(rsp, "return"));
665     qobject_unref(rsp);
666     migrate_check_parameter_int(who, "downtime-limit", value * 1000);
667 }
668 
669 static void deprecated_set_speed(QTestState *who, long long value)
670 {
671     QDict *rsp;
672 
673     rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
674                           "'arguments': { 'value': %lld } }", value);
675     g_assert(qdict_haskey(rsp, "return"));
676     qobject_unref(rsp);
677     migrate_check_parameter_int(who, "max-bandwidth", value);
678 }
679 
680 static void deprecated_set_cache_size(QTestState *who, long long value)
681 {
682     QDict *rsp;
683 
684     rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
685                          "'arguments': { 'value': %lld } }", value);
686     g_assert(qdict_haskey(rsp, "return"));
687     qobject_unref(rsp);
688     migrate_check_parameter_int(who, "xbzrle-cache-size", value);
689 }
690 
691 static void test_deprecated(void)
692 {
693     QTestState *from;
694 
695     from = qtest_init("-machine none");
696 
697     deprecated_set_downtime(from, 0.12345);
698     deprecated_set_speed(from, 12345);
699     deprecated_set_cache_size(from, 4096);
700 
701     qtest_quit(from);
702 }
703 
704 static int migrate_postcopy_prepare(QTestState **from_ptr,
705                                     QTestState **to_ptr,
706                                     MigrateStart *args)
707 {
708     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
709     QTestState *from, *to;
710 
711     if (test_migrate_start(&from, &to, uri, args)) {
712         return -1;
713     }
714 
715     migrate_set_capability(from, "postcopy-ram", true);
716     migrate_set_capability(to, "postcopy-ram", true);
717     migrate_set_capability(to, "postcopy-blocktime", true);
718 
719     /* We want to pick a speed slow enough that the test completes
720      * quickly, but that it doesn't complete precopy even on a slow
721      * machine, so also set the downtime.
722      */
723     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
724     migrate_set_parameter_int(from, "downtime-limit", 1);
725 
726     /* Wait for the first serial output from the source */
727     wait_for_serial("src_serial");
728 
729     migrate_qmp(from, uri, "{}");
730     g_free(uri);
731 
732     wait_for_migration_pass(from);
733 
734     *from_ptr = from;
735     *to_ptr = to;
736 
737     return 0;
738 }
739 
740 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
741 {
742     wait_for_migration_complete(from);
743 
744     /* Make sure we get at least one "B" on destination */
745     wait_for_serial("dest_serial");
746 
747     if (uffd_feature_thread_id) {
748         read_blocktime(to);
749     }
750 
751     test_migrate_end(from, to, true);
752 }
753 
754 static void test_postcopy(void)
755 {
756     MigrateStart *args = migrate_start_new();
757     QTestState *from, *to;
758 
759     if (migrate_postcopy_prepare(&from, &to, args)) {
760         return;
761     }
762     migrate_postcopy_start(from, to);
763     migrate_postcopy_complete(from, to);
764 }
765 
766 static void test_postcopy_recovery(void)
767 {
768     MigrateStart *args = migrate_start_new();
769     QTestState *from, *to;
770     char *uri;
771 
772     args->hide_stderr = true;
773 
774     if (migrate_postcopy_prepare(&from, &to, args)) {
775         return;
776     }
777 
778     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
779     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
780 
781     /* Now we start the postcopy */
782     migrate_postcopy_start(from, to);
783 
784     /*
785      * Wait until postcopy is really started; we can only run the
786      * migrate-pause command during a postcopy
787      */
788     wait_for_migration_status(from, "postcopy-active", NULL);
789 
790     /*
791      * Manually stop the postcopy migration. This emulates a network
792      * failure with the migration socket
793      */
794     migrate_pause(from);
795 
796     /*
797      * Wait for destination side to reach postcopy-paused state.  The
798      * migrate-recover command can only succeed if destination machine
799      * is in the paused state
800      */
801     wait_for_migration_status(to, "postcopy-paused",
802                               (const char * []) { "failed", "active",
803                                                   "completed", NULL });
804 
805     /*
806      * Create a new socket to emulate a new channel that is different
807      * from the broken migration channel; tell the destination to
808      * listen to the new port
809      */
810     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
811     migrate_recover(to, uri);
812 
813     /*
814      * Try to rebuild the migration channel using the resume flag and
815      * the newly created channel
816      */
817     wait_for_migration_status(from, "postcopy-paused",
818                               (const char * []) { "failed", "active",
819                                                   "completed", NULL });
820     migrate_qmp(from, uri, "{'resume': true}");
821     g_free(uri);
822 
823     /* Restore the postcopy bandwidth to unlimited */
824     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
825 
826     migrate_postcopy_complete(from, to);
827 }
828 
829 static void test_baddest(void)
830 {
831     MigrateStart *args = migrate_start_new();
832     QTestState *from, *to;
833 
834     args->hide_stderr = true;
835 
836     if (test_migrate_start(&from, &to, "tcp:0:0", args)) {
837         return;
838     }
839     migrate_qmp(from, "tcp:0:0", "{}");
840     wait_for_migration_fail(from, false);
841     test_migrate_end(from, to, false);
842 }
843 
844 static void test_precopy_unix(void)
845 {
846     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
847     MigrateStart *args = migrate_start_new();
848     QTestState *from, *to;
849 
850     if (test_migrate_start(&from, &to, uri, args)) {
851         return;
852     }
853 
854     /* We want to pick a speed slow enough that the test completes
855      * quickly, but that it doesn't complete precopy even on a slow
856      * machine, so also set the downtime.
857      */
858     /* 1 ms should make it not converge*/
859     migrate_set_parameter_int(from, "downtime-limit", 1);
860     /* 1GB/s */
861     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
862 
863     /* Wait for the first serial output from the source */
864     wait_for_serial("src_serial");
865 
866     migrate_qmp(from, uri, "{}");
867 
868     wait_for_migration_pass(from);
869 
870     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
871 
872     if (!got_stop) {
873         qtest_qmp_eventwait(from, "STOP");
874     }
875 
876     qtest_qmp_eventwait(to, "RESUME");
877 
878     wait_for_serial("dest_serial");
879     wait_for_migration_complete(from);
880 
881     test_migrate_end(from, to, true);
882     g_free(uri);
883 }
884 
885 #if 0
886 /* Currently upset on aarch64 TCG */
887 static void test_ignore_shared(void)
888 {
889     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
890     QTestState *from, *to;
891 
892     if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
893         return;
894     }
895 
896     migrate_set_capability(from, "x-ignore-shared", true);
897     migrate_set_capability(to, "x-ignore-shared", true);
898 
899     /* Wait for the first serial output from the source */
900     wait_for_serial("src_serial");
901 
902     migrate_qmp(from, uri, "{}");
903 
904     wait_for_migration_pass(from);
905 
906     if (!got_stop) {
907         qtest_qmp_eventwait(from, "STOP");
908     }
909 
910     qtest_qmp_eventwait(to, "RESUME");
911 
912     wait_for_serial("dest_serial");
913     wait_for_migration_complete(from);
914 
915     /* Check whether shared RAM has been really skipped */
916     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
917 
918     test_migrate_end(from, to, true);
919     g_free(uri);
920 }
921 #endif
922 
923 static void test_xbzrle(const char *uri)
924 {
925     MigrateStart *args = migrate_start_new();
926     QTestState *from, *to;
927 
928     if (test_migrate_start(&from, &to, uri, args)) {
929         return;
930     }
931 
932     /*
933      * We want to pick a speed slow enough that the test completes
934      * quickly, but that it doesn't complete precopy even on a slow
935      * machine, so also set the downtime.
936      */
937     /* 1 ms should make it not converge*/
938     migrate_set_parameter_int(from, "downtime-limit", 1);
939     /* 1GB/s */
940     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
941 
942     migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
943 
944     migrate_set_capability(from, "xbzrle", "true");
945     migrate_set_capability(to, "xbzrle", "true");
946     /* Wait for the first serial output from the source */
947     wait_for_serial("src_serial");
948 
949     migrate_qmp(from, uri, "{}");
950 
951     wait_for_migration_pass(from);
952     /* Make sure we have 2 passes, so the xbzrle cache gets a workout */
953     wait_for_migration_pass(from);
954 
955     /* 1000ms should converge */
956     migrate_set_parameter_int(from, "downtime-limit", 1000);
957 
958     if (!got_stop) {
959         qtest_qmp_eventwait(from, "STOP");
960     }
961     qtest_qmp_eventwait(to, "RESUME");
962 
963     wait_for_serial("dest_serial");
964     wait_for_migration_complete(from);
965 
966     test_migrate_end(from, to, true);
967 }
968 
969 static void test_xbzrle_unix(void)
970 {
971     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
972 
973     test_xbzrle(uri);
974     g_free(uri);
975 }
976 
977 static void test_precopy_tcp(void)
978 {
979     MigrateStart *args = migrate_start_new();
980     char *uri;
981     QTestState *from, *to;
982 
983     if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", args)) {
984         return;
985     }
986 
987     /*
988      * We want to pick a speed slow enough that the test completes
989      * quickly, but that it doesn't complete precopy even on a slow
990      * machine, so also set the downtime.
991      */
992     /* 1 ms should make it not converge*/
993     migrate_set_parameter_int(from, "downtime-limit", 1);
994     /* 1GB/s */
995     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
996 
997     /* Wait for the first serial output from the source */
998     wait_for_serial("src_serial");
999 
1000     uri = migrate_get_socket_address(to, "socket-address");
1001 
1002     migrate_qmp(from, uri, "{}");
1003 
1004     wait_for_migration_pass(from);
1005 
1006     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1007 
1008     if (!got_stop) {
1009         qtest_qmp_eventwait(from, "STOP");
1010     }
1011     qtest_qmp_eventwait(to, "RESUME");
1012 
1013     wait_for_serial("dest_serial");
1014     wait_for_migration_complete(from);
1015 
1016     test_migrate_end(from, to, true);
1017     g_free(uri);
1018 }
1019 
1020 static void test_migrate_fd_proto(void)
1021 {
1022     MigrateStart *args = migrate_start_new();
1023     QTestState *from, *to;
1024     int ret;
1025     int pair[2];
1026     QDict *rsp;
1027     const char *error_desc;
1028 
1029     if (test_migrate_start(&from, &to, "defer", args)) {
1030         return;
1031     }
1032 
1033     /*
1034      * We want to pick a speed slow enough that the test completes
1035      * quickly, but that it doesn't complete precopy even on a slow
1036      * machine, so also set the downtime.
1037      */
1038     /* 1 ms should make it not converge */
1039     migrate_set_parameter_int(from, "downtime-limit", 1);
1040     /* 1GB/s */
1041     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1042 
1043     /* Wait for the first serial output from the source */
1044     wait_for_serial("src_serial");
1045 
1046     /* Create two connected sockets for migration */
1047     ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1048     g_assert_cmpint(ret, ==, 0);
1049 
1050     /* Send the 1st socket to the target */
1051     rsp = wait_command_fd(to, pair[0],
1052                           "{ 'execute': 'getfd',"
1053                           "  'arguments': { 'fdname': 'fd-mig' }}");
1054     qobject_unref(rsp);
1055     close(pair[0]);
1056 
1057     /* Start incoming migration from the 1st socket */
1058     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1059                            "  'arguments': { 'uri': 'fd:fd-mig' }}");
1060     qobject_unref(rsp);
1061 
1062     /* Send the 2nd socket to the target */
1063     rsp = wait_command_fd(from, pair[1],
1064                           "{ 'execute': 'getfd',"
1065                           "  'arguments': { 'fdname': 'fd-mig' }}");
1066     qobject_unref(rsp);
1067     close(pair[1]);
1068 
1069     /* Start migration to the 2nd socket*/
1070     migrate_qmp(from, "fd:fd-mig", "{}");
1071 
1072     wait_for_migration_pass(from);
1073 
1074     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1075 
1076     if (!got_stop) {
1077         qtest_qmp_eventwait(from, "STOP");
1078     }
1079     qtest_qmp_eventwait(to, "RESUME");
1080 
1081     /* Test closing fds */
1082     /* We assume, that QEMU removes named fd from its list,
1083      * so this should fail */
1084     rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1085                           "  'arguments': { 'fdname': 'fd-mig' }}");
1086     g_assert_true(qdict_haskey(rsp, "error"));
1087     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1088     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1089     qobject_unref(rsp);
1090 
1091     rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1092                         "  'arguments': { 'fdname': 'fd-mig' }}");
1093     g_assert_true(qdict_haskey(rsp, "error"));
1094     error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1095     g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1096     qobject_unref(rsp);
1097 
1098     /* Complete migration */
1099     wait_for_serial("dest_serial");
1100     wait_for_migration_complete(from);
1101     test_migrate_end(from, to, true);
1102 }
1103 
1104 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
1105 {
1106     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1107     QTestState *from, *to;
1108 
1109     if (test_migrate_start(&from, &to, uri, args)) {
1110         return;
1111     }
1112 
1113     /*
1114      * UUID validation is at the begin of migration. So, the main process of
1115      * migration is not interesting for us here. Thus, set huge downtime for
1116      * very fast migration.
1117      */
1118     migrate_set_parameter_int(from, "downtime-limit", 1000000);
1119     migrate_set_capability(from, "validate-uuid", true);
1120 
1121     /* Wait for the first serial output from the source */
1122     wait_for_serial("src_serial");
1123 
1124     migrate_qmp(from, uri, "{}");
1125 
1126     if (should_fail) {
1127         qtest_set_expected_status(to, 1);
1128         wait_for_migration_fail(from, true);
1129     } else {
1130         wait_for_migration_complete(from);
1131     }
1132 
1133     test_migrate_end(from, to, false);
1134     g_free(uri);
1135 }
1136 
1137 static void test_validate_uuid(void)
1138 {
1139     MigrateStart *args = migrate_start_new();
1140 
1141     g_free(args->opts_source);
1142     g_free(args->opts_target);
1143     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1144     args->opts_target = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1145     do_test_validate_uuid(args, false);
1146 }
1147 
1148 static void test_validate_uuid_error(void)
1149 {
1150     MigrateStart *args = migrate_start_new();
1151 
1152     g_free(args->opts_source);
1153     g_free(args->opts_target);
1154     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1155     args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1156     args->hide_stderr = true;
1157     do_test_validate_uuid(args, true);
1158 }
1159 
1160 static void test_validate_uuid_src_not_set(void)
1161 {
1162     MigrateStart *args = migrate_start_new();
1163 
1164     g_free(args->opts_target);
1165     args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1166     args->hide_stderr = true;
1167     do_test_validate_uuid(args, false);
1168 }
1169 
1170 static void test_validate_uuid_dst_not_set(void)
1171 {
1172     MigrateStart *args = migrate_start_new();
1173 
1174     g_free(args->opts_source);
1175     args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1176     args->hide_stderr = true;
1177     do_test_validate_uuid(args, false);
1178 }
1179 
1180 static void test_migrate_auto_converge(void)
1181 {
1182     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1183     MigrateStart *args = migrate_start_new();
1184     QTestState *from, *to;
1185     int64_t remaining, percentage;
1186 
1187     /*
1188      * We want the test to be stable and as fast as possible.
1189      * E.g., with 1Gb/s bandwith migration may pass without throttling,
1190      * so we need to decrease a bandwidth.
1191      */
1192     const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1193     const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1194     const int64_t downtime_limit = 250; /* 250ms */
1195     /*
1196      * We migrate through unix-socket (> 500Mb/s).
1197      * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1198      * So, we can predict expected_threshold
1199      */
1200     const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1201 
1202     if (test_migrate_start(&from, &to, uri, args)) {
1203         return;
1204     }
1205 
1206     migrate_set_capability(from, "auto-converge", true);
1207     migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1208     migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1209     migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1210 
1211     /*
1212      * Set the initial parameters so that the migration could not converge
1213      * without throttling.
1214      */
1215     migrate_set_parameter_int(from, "downtime-limit", 1);
1216     migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1217 
1218     /* To check remaining size after precopy */
1219     migrate_set_capability(from, "pause-before-switchover", true);
1220 
1221     /* Wait for the first serial output from the source */
1222     wait_for_serial("src_serial");
1223 
1224     migrate_qmp(from, uri, "{}");
1225 
1226     /* Wait for throttling begins */
1227     percentage = 0;
1228     while (percentage == 0) {
1229         percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1230         usleep(100);
1231         g_assert_false(got_stop);
1232     }
1233     /* The first percentage of throttling should be equal to init_pct */
1234     g_assert_cmpint(percentage, ==, init_pct);
1235     /* Now, when we tested that throttling works, let it converge */
1236     migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1237     migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1238 
1239     /*
1240      * Wait for pre-switchover status to check last throttle percentage
1241      * and remaining. These values will be zeroed later
1242      */
1243     wait_for_migration_status(from, "pre-switchover", NULL);
1244 
1245     /* The final percentage of throttling shouldn't be greater than max_pct */
1246     percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1247     g_assert_cmpint(percentage, <=, max_pct);
1248 
1249     remaining = read_ram_property_int(from, "remaining");
1250     g_assert_cmpint(remaining, <,
1251                     (expected_threshold + expected_threshold / 100));
1252 
1253     migrate_continue(from, "pre-switchover");
1254 
1255     qtest_qmp_eventwait(to, "RESUME");
1256 
1257     wait_for_serial("dest_serial");
1258     wait_for_migration_complete(from);
1259 
1260     g_free(uri);
1261 
1262     test_migrate_end(from, to, true);
1263 }
1264 
1265 static void test_multifd_tcp(const char *method)
1266 {
1267     MigrateStart *args = migrate_start_new();
1268     QTestState *from, *to;
1269     QDict *rsp;
1270     char *uri;
1271 
1272     if (test_migrate_start(&from, &to, "defer", args)) {
1273         return;
1274     }
1275 
1276     /*
1277      * We want to pick a speed slow enough that the test completes
1278      * quickly, but that it doesn't complete precopy even on a slow
1279      * machine, so also set the downtime.
1280      */
1281     /* 1 ms should make it not converge*/
1282     migrate_set_parameter_int(from, "downtime-limit", 1);
1283     /* 1GB/s */
1284     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1285 
1286     migrate_set_parameter_int(from, "multifd-channels", 16);
1287     migrate_set_parameter_int(to, "multifd-channels", 16);
1288 
1289     migrate_set_parameter_str(from, "multifd-compression", method);
1290     migrate_set_parameter_str(to, "multifd-compression", method);
1291 
1292     migrate_set_capability(from, "multifd", "true");
1293     migrate_set_capability(to, "multifd", "true");
1294 
1295     /* Start incoming migration from the 1st socket */
1296     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1297                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1298     qobject_unref(rsp);
1299 
1300     /* Wait for the first serial output from the source */
1301     wait_for_serial("src_serial");
1302 
1303     uri = migrate_get_socket_address(to, "socket-address");
1304 
1305     migrate_qmp(from, uri, "{}");
1306 
1307     wait_for_migration_pass(from);
1308 
1309     migrate_set_parameter_int(from, "downtime-limit", CONVERGE_DOWNTIME);
1310 
1311     if (!got_stop) {
1312         qtest_qmp_eventwait(from, "STOP");
1313     }
1314     qtest_qmp_eventwait(to, "RESUME");
1315 
1316     wait_for_serial("dest_serial");
1317     wait_for_migration_complete(from);
1318     test_migrate_end(from, to, true);
1319     g_free(uri);
1320 }
1321 
1322 static void test_multifd_tcp_none(void)
1323 {
1324     test_multifd_tcp("none");
1325 }
1326 
1327 static void test_multifd_tcp_zlib(void)
1328 {
1329     test_multifd_tcp("zlib");
1330 }
1331 
1332 #ifdef CONFIG_ZSTD
1333 static void test_multifd_tcp_zstd(void)
1334 {
1335     test_multifd_tcp("zstd");
1336 }
1337 #endif
1338 
1339 /*
1340  * This test does:
1341  *  source               target
1342  *                       migrate_incoming
1343  *     migrate
1344  *     migrate_cancel
1345  *                       launch another target
1346  *     migrate
1347  *
1348  *  And see that it works
1349  */
1350 static void test_multifd_tcp_cancel(void)
1351 {
1352     MigrateStart *args = migrate_start_new();
1353     QTestState *from, *to, *to2;
1354     QDict *rsp;
1355     char *uri;
1356 
1357     args->hide_stderr = true;
1358 
1359     if (test_migrate_start(&from, &to, "defer", args)) {
1360         return;
1361     }
1362 
1363     /*
1364      * We want to pick a speed slow enough that the test completes
1365      * quickly, but that it doesn't complete precopy even on a slow
1366      * machine, so also set the downtime.
1367      */
1368     /* 1 ms should make it not converge*/
1369     migrate_set_parameter_int(from, "downtime-limit", 1);
1370     /* 300MB/s */
1371     migrate_set_parameter_int(from, "max-bandwidth", 30000000);
1372 
1373     migrate_set_parameter_int(from, "multifd-channels", 16);
1374     migrate_set_parameter_int(to, "multifd-channels", 16);
1375 
1376     migrate_set_capability(from, "multifd", "true");
1377     migrate_set_capability(to, "multifd", "true");
1378 
1379     /* Start incoming migration from the 1st socket */
1380     rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1381                            "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1382     qobject_unref(rsp);
1383 
1384     /* Wait for the first serial output from the source */
1385     wait_for_serial("src_serial");
1386 
1387     uri = migrate_get_socket_address(to, "socket-address");
1388 
1389     migrate_qmp(from, uri, "{}");
1390 
1391     wait_for_migration_pass(from);
1392 
1393     migrate_cancel(from);
1394 
1395     args = migrate_start_new();
1396     args->only_target = true;
1397 
1398     if (test_migrate_start(&from, &to2, "defer", args)) {
1399         return;
1400     }
1401 
1402     migrate_set_parameter_int(to2, "multifd-channels", 16);
1403 
1404     migrate_set_capability(to2, "multifd", "true");
1405 
1406     /* Start incoming migration from the 1st socket */
1407     rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
1408                             "  'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1409     qobject_unref(rsp);
1410 
1411     g_free(uri);
1412     uri = migrate_get_socket_address(to2, "socket-address");
1413 
1414     wait_for_migration_status(from, "cancelled", NULL);
1415 
1416     /* 300ms it should converge */
1417     migrate_set_parameter_int(from, "downtime-limit", 300);
1418     /* 1GB/s */
1419     migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1420 
1421     migrate_qmp(from, uri, "{}");
1422 
1423     wait_for_migration_pass(from);
1424 
1425     if (!got_stop) {
1426         qtest_qmp_eventwait(from, "STOP");
1427     }
1428     qtest_qmp_eventwait(to2, "RESUME");
1429 
1430     wait_for_serial("dest_serial");
1431     wait_for_migration_complete(from);
1432     test_migrate_end(from, to2, true);
1433     g_free(uri);
1434 }
1435 
1436 int main(int argc, char **argv)
1437 {
1438     char template[] = "/tmp/migration-test-XXXXXX";
1439     int ret;
1440 
1441     g_test_init(&argc, &argv, NULL);
1442 
1443     if (!ufd_version_check()) {
1444         return g_test_run();
1445     }
1446 
1447     /*
1448      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1449      * is touchy due to race conditions on dirty bits (especially on PPC for
1450      * some reason)
1451      */
1452     if (g_str_equal(qtest_get_arch(), "ppc64") &&
1453         (access("/sys/module/kvm_hv", F_OK) ||
1454          access("/dev/kvm", R_OK | W_OK))) {
1455         g_test_message("Skipping test: kvm_hv not available");
1456         return g_test_run();
1457     }
1458 
1459     /*
1460      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1461      * there until the problems are resolved
1462      */
1463     if (g_str_equal(qtest_get_arch(), "s390x")) {
1464 #if defined(HOST_S390X)
1465         if (access("/dev/kvm", R_OK | W_OK)) {
1466             g_test_message("Skipping test: kvm not available");
1467             return g_test_run();
1468         }
1469 #else
1470         g_test_message("Skipping test: Need s390x host to work properly");
1471         return g_test_run();
1472 #endif
1473     }
1474 
1475     tmpfs = mkdtemp(template);
1476     if (!tmpfs) {
1477         g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
1478     }
1479     g_assert(tmpfs);
1480 
1481     module_call_init(MODULE_INIT_QOM);
1482 
1483     qtest_add_func("/migration/postcopy/unix", test_postcopy);
1484     qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
1485     qtest_add_func("/migration/deprecated", test_deprecated);
1486     qtest_add_func("/migration/bad_dest", test_baddest);
1487     qtest_add_func("/migration/precopy/unix", test_precopy_unix);
1488     qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
1489     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
1490     qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
1491     qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
1492     qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1493     qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1494     qtest_add_func("/migration/validate_uuid_src_not_set",
1495                    test_validate_uuid_src_not_set);
1496     qtest_add_func("/migration/validate_uuid_dst_not_set",
1497                    test_validate_uuid_dst_not_set);
1498 
1499     qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1500     qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
1501     qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel);
1502     qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
1503 #ifdef CONFIG_ZSTD
1504     qtest_add_func("/migration/multifd/tcp/zstd", test_multifd_tcp_zstd);
1505 #endif
1506 
1507     ret = g_test_run();
1508 
1509     g_assert_cmpint(ret, ==, 0);
1510 
1511     ret = rmdir(tmpfs);
1512     if (ret != 0) {
1513         g_test_message("unable to rmdir: path (%s): %s",
1514                        tmpfs, strerror(errno));
1515     }
1516 
1517     return ret;
1518 }
1519