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