]> Git Repo - qemu.git/blob - tests/migration-test.c
tests/migration-test: Add a test for ignore-shared capability
[qemu.git] / tests / migration-test.c
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 "qapi/qmp/qjson.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "sysemu/sysemu.h"
23
24 #include "migration/migration-test.h"
25
26 /* TODO actually test the results and get rid of this */
27 #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
28
29 unsigned start_address;
30 unsigned end_address;
31 bool got_stop;
32 static bool uffd_feature_thread_id;
33
34 #if defined(__linux__)
35 #include <sys/syscall.h>
36 #include <sys/vfs.h>
37 #endif
38
39 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
40 #include <sys/eventfd.h>
41 #include <sys/ioctl.h>
42 #include <linux/userfaultfd.h>
43
44 static bool ufd_version_check(void)
45 {
46     struct uffdio_api api_struct;
47     uint64_t ioctl_mask;
48
49     int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
50
51     if (ufd == -1) {
52         g_test_message("Skipping test: userfaultfd not available");
53         return false;
54     }
55
56     api_struct.api = UFFD_API;
57     api_struct.features = 0;
58     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
59         g_test_message("Skipping test: UFFDIO_API failed");
60         return false;
61     }
62     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
63
64     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
65                  (__u64)1 << _UFFDIO_UNREGISTER;
66     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
67         g_test_message("Skipping test: Missing userfault feature");
68         return false;
69     }
70
71     return true;
72 }
73
74 #else
75 static bool ufd_version_check(void)
76 {
77     g_test_message("Skipping test: Userfault not available (builtdtime)");
78     return false;
79 }
80
81 #endif
82
83 static const char *tmpfs;
84
85 /* The boot file modifies memory area in [start_address, end_address)
86  * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
87  */
88 #include "tests/migration/i386/a-b-bootblock.h"
89 #include "tests/migration/aarch64/a-b-kernel.h"
90
91 static void init_bootfile(const char *bootpath, void *content)
92 {
93     FILE *bootfile = fopen(bootpath, "wb");
94
95     g_assert_cmpint(fwrite(content, 512, 1, bootfile), ==, 1);
96     fclose(bootfile);
97 }
98
99 #include "tests/migration/s390x/a-b-bios.h"
100
101 static void init_bootfile_s390x(const char *bootpath)
102 {
103     FILE *bootfile = fopen(bootpath, "wb");
104     size_t len = sizeof(s390x_elf);
105
106     g_assert_cmpint(fwrite(s390x_elf, 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     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     g_free(serialpath);
124     do {
125         int readvalue = fgetc(serialfile);
126
127         if (!started) {
128             /* SLOF prints its banner before starting test,
129              * to ignore it, mark the start of the test with '_',
130              * ignore all characters until this marker
131              */
132             switch (readvalue) {
133             case '_':
134                 started = 1;
135                 break;
136             case EOF:
137                 fseek(serialfile, 0, SEEK_SET);
138                 usleep(1000);
139                 break;
140             }
141             continue;
142         }
143         switch (readvalue) {
144         case 'A':
145             /* Fine */
146             break;
147
148         case 'B':
149             /* It's alive! */
150             fclose(serialfile);
151             return;
152
153         case EOF:
154             started = (strcmp(side, "src_serial") == 0 &&
155                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
156             fseek(serialfile, 0, SEEK_SET);
157             usleep(1000);
158             break;
159
160         default:
161             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
162             g_assert_not_reached();
163         }
164     } while (true);
165 }
166
167 static void stop_cb(void *opaque, const char *name, QDict *data)
168 {
169     if (!strcmp(name, "STOP")) {
170         got_stop = true;
171     }
172 }
173
174 /*
175  * Events can get in the way of responses we are actually waiting for.
176  */
177 GCC_FMT_ATTR(2, 3)
178 static QDict *wait_command(QTestState *who, const char *command, ...)
179 {
180     va_list ap;
181
182     va_start(ap, command);
183     qtest_qmp_vsend(who, command, ap);
184     va_end(ap);
185
186     return qtest_qmp_receive_success(who, stop_cb, NULL);
187 }
188
189 /*
190  * Note: caller is responsible to free the returned object via
191  * qobject_unref() after use
192  */
193 static QDict *migrate_query(QTestState *who)
194 {
195     return wait_command(who, "{ 'execute': 'query-migrate' }");
196 }
197
198 /*
199  * Note: caller is responsible to free the returned object via
200  * g_free() after use
201  */
202 static gchar *migrate_query_status(QTestState *who)
203 {
204     QDict *rsp_return = migrate_query(who);
205     gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
206
207     g_assert(status);
208     qobject_unref(rsp_return);
209
210     return status;
211 }
212
213 /*
214  * It's tricky to use qemu's migration event capability with qtest,
215  * events suddenly appearing confuse the qmp()/hmp() responses.
216  */
217
218 static int64_t read_ram_property_int(QTestState *who, const char *property)
219 {
220     QDict *rsp_return, *rsp_ram;
221     int64_t result;
222
223     rsp_return = migrate_query(who);
224     if (!qdict_haskey(rsp_return, "ram")) {
225         /* Still in setup */
226         result = 0;
227     } else {
228         rsp_ram = qdict_get_qdict(rsp_return, "ram");
229         result = qdict_get_try_int(rsp_ram, property, 0);
230     }
231     qobject_unref(rsp_return);
232     return result;
233 }
234
235 static uint64_t get_migration_pass(QTestState *who)
236 {
237     return read_ram_property_int(who, "dirty-sync-count");
238 }
239
240 static void read_blocktime(QTestState *who)
241 {
242     QDict *rsp_return;
243
244     rsp_return = migrate_query(who);
245     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
246     qobject_unref(rsp_return);
247 }
248
249 static void wait_for_migration_status(QTestState *who,
250                                       const char *goal)
251 {
252     while (true) {
253         bool completed;
254         char *status;
255
256         status = migrate_query_status(who);
257         completed = strcmp(status, goal) == 0;
258         g_assert_cmpstr(status, !=,  "failed");
259         g_free(status);
260         if (completed) {
261             return;
262         }
263         usleep(1000);
264     }
265 }
266
267 static void wait_for_migration_complete(QTestState *who)
268 {
269     wait_for_migration_status(who, "completed");
270 }
271
272 static void wait_for_migration_pass(QTestState *who)
273 {
274     uint64_t initial_pass = get_migration_pass(who);
275     uint64_t pass;
276
277     /* Wait for the 1st sync */
278     while (!got_stop && !initial_pass) {
279         usleep(1000);
280         initial_pass = get_migration_pass(who);
281     }
282
283     do {
284         usleep(1000);
285         pass = get_migration_pass(who);
286     } while (pass == initial_pass && !got_stop);
287 }
288
289 static void check_guests_ram(QTestState *who)
290 {
291     /* Our ASM test will have been incrementing one byte from each page from
292      * start_address to < end_address in order. This gives us a constraint
293      * that any page's byte should be equal or less than the previous pages
294      * byte (mod 256); and they should all be equal except for one transition
295      * at the point where we meet the incrementer. (We're running this with
296      * the guest stopped).
297      */
298     unsigned address;
299     uint8_t first_byte;
300     uint8_t last_byte;
301     bool hit_edge = false;
302     bool bad = false;
303
304     qtest_memread(who, start_address, &first_byte, 1);
305     last_byte = first_byte;
306
307     for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
308          address += TEST_MEM_PAGE_SIZE)
309     {
310         uint8_t b;
311         qtest_memread(who, address, &b, 1);
312         if (b != last_byte) {
313             if (((b + 1) % 256) == last_byte && !hit_edge) {
314                 /* This is OK, the guest stopped at the point of
315                  * incrementing the previous page but didn't get
316                  * to us yet.
317                  */
318                 hit_edge = true;
319                 last_byte = b;
320             } else {
321                 fprintf(stderr, "Memory content inconsistency at %x"
322                                 " first_byte = %x last_byte = %x current = %x"
323                                 " hit_edge = %x\n",
324                                 address, first_byte, last_byte, b, hit_edge);
325                 bad = true;
326             }
327         }
328     }
329     g_assert_false(bad);
330 }
331
332 static void cleanup(const char *filename)
333 {
334     char *path = g_strdup_printf("%s/%s", tmpfs, filename);
335
336     unlink(path);
337     g_free(path);
338 }
339
340 static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
341 {
342     return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
343                            ",mem-path=%s,share=on -numa node,memdev=mem0",
344                            mem_size, shmem_path);
345 }
346
347 static void migrate_check_parameter(QTestState *who, const char *parameter,
348                                     long long value)
349 {
350     QDict *rsp_return;
351
352     rsp_return = wait_command(who,
353                               "{ 'execute': 'query-migrate-parameters' }");
354     g_assert_cmpint(qdict_get_int(rsp_return, parameter), ==, value);
355     qobject_unref(rsp_return);
356 }
357
358 static void migrate_set_parameter(QTestState *who, const char *parameter,
359                                   long long value)
360 {
361     QDict *rsp;
362
363     rsp = qtest_qmp(who,
364                     "{ 'execute': 'migrate-set-parameters',"
365                     "'arguments': { %s: %lld } }",
366                     parameter, value);
367     g_assert(qdict_haskey(rsp, "return"));
368     qobject_unref(rsp);
369     migrate_check_parameter(who, parameter, value);
370 }
371
372 static void migrate_pause(QTestState *who)
373 {
374     QDict *rsp;
375
376     rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
377     qobject_unref(rsp);
378 }
379
380 static void migrate_recover(QTestState *who, const char *uri)
381 {
382     QDict *rsp;
383
384     rsp = wait_command(who,
385                        "{ 'execute': 'migrate-recover', "
386                        "  'id': 'recover-cmd', "
387                        "  'arguments': { 'uri': %s } }",
388                        uri);
389     qobject_unref(rsp);
390 }
391
392 static void migrate_set_capability(QTestState *who, const char *capability,
393                                    bool value)
394 {
395     QDict *rsp;
396
397     rsp = qtest_qmp(who,
398                     "{ 'execute': 'migrate-set-capabilities',"
399                     "'arguments': { "
400                     "'capabilities': [ { "
401                     "'capability': %s, 'state': %i } ] } }",
402                     capability, value);
403     g_assert(qdict_haskey(rsp, "return"));
404     qobject_unref(rsp);
405 }
406
407 /*
408  * Send QMP command "migrate".
409  * Arguments are built from @fmt... (formatted like
410  * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
411  */
412 GCC_FMT_ATTR(3, 4)
413 static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
414 {
415     va_list ap;
416     QDict *args, *rsp;
417
418     va_start(ap, fmt);
419     args = qdict_from_vjsonf_nofail(fmt, ap);
420     va_end(ap);
421
422     g_assert(!qdict_haskey(args, "uri"));
423     qdict_put_str(args, "uri", uri);
424
425     rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
426     g_assert(qdict_haskey(rsp, "return"));
427     qobject_unref(rsp);
428 }
429
430 static void migrate_postcopy_start(QTestState *from, QTestState *to)
431 {
432     QDict *rsp;
433
434     rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
435     qobject_unref(rsp);
436
437     if (!got_stop) {
438         qtest_qmp_eventwait(from, "STOP");
439     }
440
441     qtest_qmp_eventwait(to, "RESUME");
442 }
443
444 static int test_migrate_start(QTestState **from, QTestState **to,
445                                const char *uri, bool hide_stderr,
446                                bool use_shmem)
447 {
448     gchar *cmd_src, *cmd_dst;
449     char *bootpath = NULL;
450     char *extra_opts = NULL;
451     char *shmem_path = NULL;
452     const char *arch = qtest_get_arch();
453     const char *accel = "kvm:tcg";
454
455     if (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         shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
461     }
462
463     got_stop = false;
464     bootpath = g_strdup_printf("%s/bootsect", tmpfs);
465     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
466         init_bootfile(bootpath, x86_bootsect);
467         extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
468         cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
469                                   " -name source,debug-threads=on"
470                                   " -serial file:%s/src_serial"
471                                   " -drive file=%s,format=raw %s",
472                                   accel, tmpfs, bootpath,
473                                   extra_opts ? extra_opts : "");
474         cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
475                                   " -name target,debug-threads=on"
476                                   " -serial file:%s/dest_serial"
477                                   " -drive file=%s,format=raw"
478                                   " -incoming %s %s",
479                                   accel, tmpfs, bootpath, uri,
480                                   extra_opts ? extra_opts : "");
481         start_address = X86_TEST_MEM_START;
482         end_address = X86_TEST_MEM_END;
483     } else if (g_str_equal(arch, "s390x")) {
484         init_bootfile_s390x(bootpath);
485         extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
486         cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
487                                   " -name source,debug-threads=on"
488                                   " -serial file:%s/src_serial -bios %s %s",
489                                   accel, tmpfs, bootpath,
490                                   extra_opts ? extra_opts : "");
491         cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
492                                   " -name target,debug-threads=on"
493                                   " -serial file:%s/dest_serial -bios %s"
494                                   " -incoming %s %s",
495                                   accel, tmpfs, bootpath, uri,
496                                   extra_opts ? extra_opts : "");
497         start_address = S390_TEST_MEM_START;
498         end_address = S390_TEST_MEM_END;
499     } else if (strcmp(arch, "ppc64") == 0) {
500         extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
501         cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
502                                   " -name source,debug-threads=on"
503                                   " -serial file:%s/src_serial"
504                                   " -prom-env 'use-nvramrc?=true' -prom-env "
505                                   "'nvramrc=hex .\" _\" begin %x %x "
506                                   "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
507                                   "until' %s",  accel, tmpfs, end_address,
508                                   start_address, extra_opts ? extra_opts : "");
509         cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
510                                   " -name target,debug-threads=on"
511                                   " -serial file:%s/dest_serial"
512                                   " -incoming %s %s",
513                                   accel, tmpfs, uri,
514                                   extra_opts ? extra_opts : "");
515
516         start_address = PPC_TEST_MEM_START;
517         end_address = PPC_TEST_MEM_END;
518     } else if (strcmp(arch, "aarch64") == 0) {
519         init_bootfile(bootpath, aarch64_kernel);
520         extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
521         cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
522                                   "-name vmsource,debug-threads=on -cpu max "
523                                   "-m 150M -serial file:%s/src_serial "
524                                   "-kernel %s %s",
525                                   accel, tmpfs, bootpath,
526                                   extra_opts ? extra_opts : "");
527         cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
528                                   "-name vmdest,debug-threads=on -cpu max "
529                                   "-m 150M -serial file:%s/dest_serial "
530                                   "-kernel %s "
531                                   "-incoming %s %s",
532                                   accel, tmpfs, bootpath, uri,
533                                   extra_opts ? extra_opts : "");
534
535         start_address = ARM_TEST_MEM_START;
536         end_address = ARM_TEST_MEM_END;
537
538         g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
539     } else {
540         g_assert_not_reached();
541     }
542
543     g_free(bootpath);
544     g_free(extra_opts);
545
546     if (hide_stderr) {
547         gchar *tmp;
548         tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
549         g_free(cmd_src);
550         cmd_src = tmp;
551
552         tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
553         g_free(cmd_dst);
554         cmd_dst = tmp;
555     }
556
557     *from = qtest_start(cmd_src);
558     g_free(cmd_src);
559
560     *to = qtest_init(cmd_dst);
561     g_free(cmd_dst);
562
563     /*
564      * Remove shmem file immediately to avoid memory leak in test failed case.
565      * It's valid becase QEMU has already opened this file
566      */
567     if (use_shmem) {
568         unlink(shmem_path);
569         g_free(shmem_path);
570     }
571
572     return 0;
573 }
574
575 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
576 {
577     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
578
579     qtest_quit(from);
580
581     if (test_dest) {
582         qtest_memread(to, start_address, &dest_byte_a, 1);
583
584         /* Destination still running, wait for a byte to change */
585         do {
586             qtest_memread(to, start_address, &dest_byte_b, 1);
587             usleep(1000 * 10);
588         } while (dest_byte_a == dest_byte_b);
589
590         qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
591
592         /* With it stopped, check nothing changes */
593         qtest_memread(to, start_address, &dest_byte_c, 1);
594         usleep(1000 * 200);
595         qtest_memread(to, start_address, &dest_byte_d, 1);
596         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
597
598         check_guests_ram(to);
599     }
600
601     qtest_quit(to);
602
603     cleanup("bootsect");
604     cleanup("migsocket");
605     cleanup("src_serial");
606     cleanup("dest_serial");
607 }
608
609 static void deprecated_set_downtime(QTestState *who, const double value)
610 {
611     QDict *rsp;
612
613     rsp = qtest_qmp(who,
614                     "{ 'execute': 'migrate_set_downtime',"
615                     " 'arguments': { 'value': %f } }", value);
616     g_assert(qdict_haskey(rsp, "return"));
617     qobject_unref(rsp);
618     migrate_check_parameter(who, "downtime-limit", value * 1000);
619 }
620
621 static void deprecated_set_speed(QTestState *who, long long value)
622 {
623     QDict *rsp;
624
625     rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
626                           "'arguments': { 'value': %lld } }", value);
627     g_assert(qdict_haskey(rsp, "return"));
628     qobject_unref(rsp);
629     migrate_check_parameter(who, "max-bandwidth", value);
630 }
631
632 static void test_deprecated(void)
633 {
634     QTestState *from;
635
636     from = qtest_start("-machine none");
637
638     deprecated_set_downtime(from, 0.12345);
639     deprecated_set_speed(from, 12345);
640
641     qtest_quit(from);
642 }
643
644 static int migrate_postcopy_prepare(QTestState **from_ptr,
645                                      QTestState **to_ptr,
646                                      bool hide_error)
647 {
648     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
649     QTestState *from, *to;
650
651     if (test_migrate_start(&from, &to, uri, hide_error, false)) {
652         return -1;
653     }
654
655     migrate_set_capability(from, "postcopy-ram", true);
656     migrate_set_capability(to, "postcopy-ram", true);
657     migrate_set_capability(to, "postcopy-blocktime", true);
658
659     /* We want to pick a speed slow enough that the test completes
660      * quickly, but that it doesn't complete precopy even on a slow
661      * machine, so also set the downtime.
662      */
663     migrate_set_parameter(from, "max-bandwidth", 100000000);
664     migrate_set_parameter(from, "downtime-limit", 1);
665
666     /* Wait for the first serial output from the source */
667     wait_for_serial("src_serial");
668
669     migrate(from, uri, "{}");
670     g_free(uri);
671
672     wait_for_migration_pass(from);
673
674     *from_ptr = from;
675     *to_ptr = to;
676
677     return 0;
678 }
679
680 static void migrate_postcopy_complete(QTestState *from, QTestState *to)
681 {
682     wait_for_migration_complete(from);
683
684     /* Make sure we get at least one "B" on destination */
685     wait_for_serial("dest_serial");
686
687     if (uffd_feature_thread_id) {
688         read_blocktime(to);
689     }
690
691     test_migrate_end(from, to, true);
692 }
693
694 static void test_postcopy(void)
695 {
696     QTestState *from, *to;
697
698     if (migrate_postcopy_prepare(&from, &to, false)) {
699         return;
700     }
701     migrate_postcopy_start(from, to);
702     migrate_postcopy_complete(from, to);
703 }
704
705 static void test_postcopy_recovery(void)
706 {
707     QTestState *from, *to;
708     char *uri;
709
710     if (migrate_postcopy_prepare(&from, &to, true)) {
711         return;
712     }
713
714     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
715     migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
716
717     /* Now we start the postcopy */
718     migrate_postcopy_start(from, to);
719
720     /*
721      * Wait until postcopy is really started; we can only run the
722      * migrate-pause command during a postcopy
723      */
724     wait_for_migration_status(from, "postcopy-active");
725
726     /*
727      * Manually stop the postcopy migration. This emulates a network
728      * failure with the migration socket
729      */
730     migrate_pause(from);
731
732     /*
733      * Wait for destination side to reach postcopy-paused state.  The
734      * migrate-recover command can only succeed if destination machine
735      * is in the paused state
736      */
737     wait_for_migration_status(to, "postcopy-paused");
738
739     /*
740      * Create a new socket to emulate a new channel that is different
741      * from the broken migration channel; tell the destination to
742      * listen to the new port
743      */
744     uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
745     migrate_recover(to, uri);
746
747     /*
748      * Try to rebuild the migration channel using the resume flag and
749      * the newly created channel
750      */
751     wait_for_migration_status(from, "postcopy-paused");
752     migrate(from, uri, "{'resume': true}");
753     g_free(uri);
754
755     /* Restore the postcopy bandwidth to unlimited */
756     migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
757
758     migrate_postcopy_complete(from, to);
759 }
760
761 static void test_baddest(void)
762 {
763     QTestState *from, *to;
764     QDict *rsp_return;
765     char *status;
766     bool failed;
767
768     if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
769         return;
770     }
771     migrate(from, "tcp:0:0", "{}");
772     do {
773         status = migrate_query_status(from);
774         g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
775         failed = !strcmp(status, "failed");
776         g_free(status);
777     } while (!failed);
778
779     /* Is the machine currently running? */
780     rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
781     g_assert(qdict_haskey(rsp_return, "running"));
782     g_assert(qdict_get_bool(rsp_return, "running"));
783     qobject_unref(rsp_return);
784
785     test_migrate_end(from, to, false);
786 }
787
788 static void test_precopy_unix(void)
789 {
790     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
791     QTestState *from, *to;
792
793     if (test_migrate_start(&from, &to, uri, false, false)) {
794         return;
795     }
796
797     /* We want to pick a speed slow enough that the test completes
798      * quickly, but that it doesn't complete precopy even on a slow
799      * machine, so also set the downtime.
800      */
801     /* 1 ms should make it not converge*/
802     migrate_set_parameter(from, "downtime-limit", 1);
803     /* 1GB/s */
804     migrate_set_parameter(from, "max-bandwidth", 1000000000);
805
806     /* Wait for the first serial output from the source */
807     wait_for_serial("src_serial");
808
809     migrate(from, uri, "{}");
810
811     wait_for_migration_pass(from);
812
813     /* 300 ms should converge */
814     migrate_set_parameter(from, "downtime-limit", 300);
815
816     if (!got_stop) {
817         qtest_qmp_eventwait(from, "STOP");
818     }
819
820     qtest_qmp_eventwait(to, "RESUME");
821
822     wait_for_serial("dest_serial");
823     wait_for_migration_complete(from);
824
825     test_migrate_end(from, to, true);
826     g_free(uri);
827 }
828
829 #if 0
830 /* Currently upset on aarch64 TCG */
831 static void test_ignore_shared(void)
832 {
833     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
834     QTestState *from, *to;
835
836     if (test_migrate_start(&from, &to, uri, false, true)) {
837         return;
838     }
839
840     migrate_set_capability(from, "x-ignore-shared", true);
841     migrate_set_capability(to, "x-ignore-shared", true);
842
843     /* Wait for the first serial output from the source */
844     wait_for_serial("src_serial");
845
846     migrate(from, uri, "{}");
847
848     wait_for_migration_pass(from);
849
850     if (!got_stop) {
851         qtest_qmp_eventwait(from, "STOP");
852     }
853
854     qtest_qmp_eventwait(to, "RESUME");
855
856     wait_for_serial("dest_serial");
857     wait_for_migration_complete(from);
858
859     /* Check whether shared RAM has been really skipped */
860     g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
861
862     test_migrate_end(from, to, true);
863     g_free(uri);
864 }
865 #endif
866
867 int main(int argc, char **argv)
868 {
869     char template[] = "/tmp/migration-test-XXXXXX";
870     int ret;
871
872     g_test_init(&argc, &argv, NULL);
873
874     if (!ufd_version_check()) {
875         return g_test_run();
876     }
877
878     /*
879      * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
880      * is touchy due to race conditions on dirty bits (especially on PPC for
881      * some reason)
882      */
883     if (g_str_equal(qtest_get_arch(), "ppc64") &&
884         access("/sys/module/kvm_hv", F_OK)) {
885         g_test_message("Skipping test: kvm_hv not available");
886         return g_test_run();
887     }
888
889     /*
890      * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
891      * there until the problems are resolved
892      */
893     if (g_str_equal(qtest_get_arch(), "s390x")) {
894 #if defined(HOST_S390X)
895         if (access("/dev/kvm", R_OK | W_OK)) {
896             g_test_message("Skipping test: kvm not available");
897             return g_test_run();
898         }
899 #else
900         g_test_message("Skipping test: Need s390x host to work properly");
901         return g_test_run();
902 #endif
903     }
904
905     tmpfs = mkdtemp(template);
906     if (!tmpfs) {
907         g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
908     }
909     g_assert(tmpfs);
910
911     module_call_init(MODULE_INIT_QOM);
912
913     qtest_add_func("/migration/postcopy/unix", test_postcopy);
914     qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
915     qtest_add_func("/migration/deprecated", test_deprecated);
916     qtest_add_func("/migration/bad_dest", test_baddest);
917     qtest_add_func("/migration/precopy/unix", test_precopy_unix);
918     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
919
920     ret = g_test_run();
921
922     g_assert_cmpint(ret, ==, 0);
923
924     ret = rmdir(tmpfs);
925     if (ret != 0) {
926         g_test_message("unable to rmdir: path (%s): %s\n",
927                        tmpfs, strerror(errno));
928     }
929
930     return ret;
931 }
This page took 0.077631 seconds and 4 git commands to generate.