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