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