]> Git Repo - qemu.git/blob - tests/migration-test.c
Merge remote-tracking branch 'remotes/cody/tags/block-pull-request' into staging
[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 "qemu/option.h"
18 #include "qemu/range.h"
19 #include "qemu/sockets.h"
20 #include "chardev/char.h"
21 #include "sysemu/sysemu.h"
22
23 const unsigned start_address = 1024 * 1024;
24 const unsigned end_address = 100 * 1024 * 1024;
25 bool got_stop;
26 static bool uffd_feature_thread_id;
27
28 #if defined(__linux__)
29 #include <sys/syscall.h>
30 #include <sys/vfs.h>
31 #endif
32
33 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
34 #include <sys/eventfd.h>
35 #include <sys/ioctl.h>
36 #include <linux/userfaultfd.h>
37
38 static bool ufd_version_check(void)
39 {
40     struct uffdio_api api_struct;
41     uint64_t ioctl_mask;
42
43     int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
44
45     if (ufd == -1) {
46         g_test_message("Skipping test: userfaultfd not available");
47         return false;
48     }
49
50     api_struct.api = UFFD_API;
51     api_struct.features = 0;
52     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
53         g_test_message("Skipping test: UFFDIO_API failed");
54         return false;
55     }
56     uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
57
58     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
59                  (__u64)1 << _UFFDIO_UNREGISTER;
60     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
61         g_test_message("Skipping test: Missing userfault feature");
62         return false;
63     }
64
65     return true;
66 }
67
68 #else
69 static bool ufd_version_check(void)
70 {
71     g_test_message("Skipping test: Userfault not available (builtdtime)");
72     return false;
73 }
74
75 #endif
76
77 static const char *tmpfs;
78
79 /* A simple PC boot sector that modifies memory (1-100MB) quickly
80  * outputting a 'B' every so often if it's still running.
81  */
82 #include "tests/migration/x86-a-b-bootblock.h"
83
84 static void init_bootfile_x86(const char *bootpath)
85 {
86     FILE *bootfile = fopen(bootpath, "wb");
87
88     g_assert_cmpint(fwrite(x86_bootsect, 512, 1, bootfile), ==, 1);
89     fclose(bootfile);
90 }
91
92 /*
93  * Wait for some output in the serial output file,
94  * we get an 'A' followed by an endless string of 'B's
95  * but on the destination we won't have the A.
96  */
97 static void wait_for_serial(const char *side)
98 {
99     char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
100     FILE *serialfile = fopen(serialpath, "r");
101     const char *arch = qtest_get_arch();
102     int started = (strcmp(side, "src_serial") == 0 &&
103                    strcmp(arch, "ppc64") == 0) ? 0 : 1;
104
105     g_free(serialpath);
106     do {
107         int readvalue = fgetc(serialfile);
108
109         if (!started) {
110             /* SLOF prints its banner before starting test,
111              * to ignore it, mark the start of the test with '_',
112              * ignore all characters until this marker
113              */
114             switch (readvalue) {
115             case '_':
116                 started = 1;
117                 break;
118             case EOF:
119                 fseek(serialfile, 0, SEEK_SET);
120                 usleep(1000);
121                 break;
122             }
123             continue;
124         }
125         switch (readvalue) {
126         case 'A':
127             /* Fine */
128             break;
129
130         case 'B':
131             /* It's alive! */
132             fclose(serialfile);
133             return;
134
135         case EOF:
136             started = (strcmp(side, "src_serial") == 0 &&
137                        strcmp(arch, "ppc64") == 0) ? 0 : 1;
138             fseek(serialfile, 0, SEEK_SET);
139             usleep(1000);
140             break;
141
142         default:
143             fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
144             g_assert_not_reached();
145         }
146     } while (true);
147 }
148
149 /*
150  * Events can get in the way of responses we are actually waiting for.
151  */
152 static QDict *wait_command(QTestState *who, const char *command)
153 {
154     const char *event_string;
155     QDict *response;
156
157     response = qtest_qmp(who, command);
158
159     while (qdict_haskey(response, "event")) {
160         /* OK, it was an event */
161         event_string = qdict_get_str(response, "event");
162         if (!strcmp(event_string, "STOP")) {
163             got_stop = true;
164         }
165         qobject_unref(response);
166         response = qtest_qmp_receive(who);
167     }
168     return response;
169 }
170
171
172 /*
173  * It's tricky to use qemu's migration event capability with qtest,
174  * events suddenly appearing confuse the qmp()/hmp() responses.
175  */
176
177 static uint64_t get_migration_pass(QTestState *who)
178 {
179     QDict *rsp, *rsp_return, *rsp_ram;
180     uint64_t result;
181
182     rsp = wait_command(who, "{ 'execute': 'query-migrate' }");
183     rsp_return = qdict_get_qdict(rsp, "return");
184     if (!qdict_haskey(rsp_return, "ram")) {
185         /* Still in setup */
186         result = 0;
187     } else {
188         rsp_ram = qdict_get_qdict(rsp_return, "ram");
189         result = qdict_get_try_int(rsp_ram, "dirty-sync-count", 0);
190     }
191     qobject_unref(rsp);
192     return result;
193 }
194
195 static void read_blocktime(QTestState *who)
196 {
197     QDict *rsp, *rsp_return;
198
199     rsp = wait_command(who, "{ 'execute': 'query-migrate' }");
200     rsp_return = qdict_get_qdict(rsp, "return");
201     g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
202     qobject_unref(rsp);
203 }
204
205 static void wait_for_migration_complete(QTestState *who)
206 {
207     while (true) {
208         QDict *rsp, *rsp_return;
209         bool completed;
210         const char *status;
211
212         rsp = wait_command(who, "{ 'execute': 'query-migrate' }");
213         rsp_return = qdict_get_qdict(rsp, "return");
214         status = qdict_get_str(rsp_return, "status");
215         completed = strcmp(status, "completed") == 0;
216         g_assert_cmpstr(status, !=,  "failed");
217         qobject_unref(rsp);
218         if (completed) {
219             return;
220         }
221         usleep(1000);
222     }
223 }
224
225 static void wait_for_migration_pass(QTestState *who)
226 {
227     uint64_t initial_pass = get_migration_pass(who);
228     uint64_t pass;
229
230     /* Wait for the 1st sync */
231     while (!got_stop && !initial_pass) {
232         usleep(1000);
233         initial_pass = get_migration_pass(who);
234     }
235
236     do {
237         usleep(1000);
238         pass = get_migration_pass(who);
239     } while (pass == initial_pass && !got_stop);
240 }
241
242 static void check_guests_ram(QTestState *who)
243 {
244     /* Our ASM test will have been incrementing one byte from each page from
245      * 1MB to <100MB in order.
246      * This gives us a constraint that any page's byte should be equal or less
247      * than the previous pages byte (mod 256); and they should all be equal
248      * except for one transition at the point where we meet the incrementer.
249      * (We're running this with the guest stopped).
250      */
251     unsigned address;
252     uint8_t first_byte;
253     uint8_t last_byte;
254     bool hit_edge = false;
255     bool bad = false;
256
257     qtest_memread(who, start_address, &first_byte, 1);
258     last_byte = first_byte;
259
260     for (address = start_address + 4096; address < end_address; address += 4096)
261     {
262         uint8_t b;
263         qtest_memread(who, address, &b, 1);
264         if (b != last_byte) {
265             if (((b + 1) % 256) == last_byte && !hit_edge) {
266                 /* This is OK, the guest stopped at the point of
267                  * incrementing the previous page but didn't get
268                  * to us yet.
269                  */
270                 hit_edge = true;
271             } else {
272                 fprintf(stderr, "Memory content inconsistency at %x"
273                                 " first_byte = %x last_byte = %x current = %x"
274                                 " hit_edge = %x\n",
275                                 address, first_byte, last_byte, b, hit_edge);
276                 bad = true;
277             }
278         }
279         last_byte = b;
280     }
281     g_assert_false(bad);
282 }
283
284 static void cleanup(const char *filename)
285 {
286     char *path = g_strdup_printf("%s/%s", tmpfs, filename);
287
288     unlink(path);
289     g_free(path);
290 }
291
292 static void migrate_check_parameter(QTestState *who, const char *parameter,
293                                     const char *value)
294 {
295     QDict *rsp, *rsp_return;
296     char *result;
297
298     rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
299     rsp_return = qdict_get_qdict(rsp, "return");
300     result = g_strdup_printf("%" PRId64,
301                              qdict_get_try_int(rsp_return,  parameter, -1));
302     g_assert_cmpstr(result, ==, value);
303     g_free(result);
304     qobject_unref(rsp);
305 }
306
307 static void migrate_set_parameter(QTestState *who, const char *parameter,
308                                   const char *value)
309 {
310     QDict *rsp;
311     gchar *cmd;
312
313     cmd = g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
314                           "'arguments': { '%s': %s } }",
315                           parameter, value);
316     rsp = qtest_qmp(who, cmd);
317     g_free(cmd);
318     g_assert(qdict_haskey(rsp, "return"));
319     qobject_unref(rsp);
320     migrate_check_parameter(who, parameter, value);
321 }
322
323 static void migrate_set_capability(QTestState *who, const char *capability,
324                                    const char *value)
325 {
326     QDict *rsp;
327     gchar *cmd;
328
329     cmd = g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
330                           "'arguments': { "
331                           "'capabilities': [ { "
332                           "'capability': '%s', 'state': %s } ] } }",
333                           capability, value);
334     rsp = qtest_qmp(who, cmd);
335     g_free(cmd);
336     g_assert(qdict_haskey(rsp, "return"));
337     qobject_unref(rsp);
338 }
339
340 static void migrate(QTestState *who, const char *uri)
341 {
342     QDict *rsp;
343     gchar *cmd;
344
345     cmd = g_strdup_printf("{ 'execute': 'migrate',"
346                           "'arguments': { 'uri': '%s' } }",
347                           uri);
348     rsp = qtest_qmp(who, cmd);
349     g_free(cmd);
350     g_assert(qdict_haskey(rsp, "return"));
351     qobject_unref(rsp);
352 }
353
354 static void migrate_start_postcopy(QTestState *who)
355 {
356     QDict *rsp;
357
358     rsp = wait_command(who, "{ 'execute': 'migrate-start-postcopy' }");
359     g_assert(qdict_haskey(rsp, "return"));
360     qobject_unref(rsp);
361 }
362
363 static void test_migrate_start(QTestState **from, QTestState **to,
364                                const char *uri, bool hide_stderr)
365 {
366     gchar *cmd_src, *cmd_dst;
367     char *bootpath = g_strdup_printf("%s/bootsect", tmpfs);
368     const char *arch = qtest_get_arch();
369     const char *accel = "kvm:tcg";
370
371     got_stop = false;
372
373     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
374         init_bootfile_x86(bootpath);
375         cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
376                                   " -name source,debug-threads=on"
377                                   " -serial file:%s/src_serial"
378                                   " -drive file=%s,format=raw",
379                                   accel, tmpfs, bootpath);
380         cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
381                                   " -name target,debug-threads=on"
382                                   " -serial file:%s/dest_serial"
383                                   " -drive file=%s,format=raw"
384                                   " -incoming %s",
385                                   accel, tmpfs, bootpath, uri);
386     } else if (strcmp(arch, "ppc64") == 0) {
387
388         /* On ppc64, the test only works with kvm-hv, but not with kvm-pr */
389         if (access("/sys/module/kvm_hv", F_OK)) {
390             accel = "tcg";
391         }
392         cmd_src = g_strdup_printf("-machine accel=%s -m 256M"
393                                   " -name source,debug-threads=on"
394                                   " -serial file:%s/src_serial"
395                                   " -prom-env '"
396                                   "boot-command=hex .\" _\" begin %x %x "
397                                   "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
398                                   "until'",  accel, tmpfs, end_address,
399                                   start_address);
400         cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
401                                   " -name target,debug-threads=on"
402                                   " -serial file:%s/dest_serial"
403                                   " -incoming %s",
404                                   accel, tmpfs, uri);
405     } else {
406         g_assert_not_reached();
407     }
408
409     g_free(bootpath);
410
411     if (hide_stderr) {
412         gchar *tmp;
413         tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
414         g_free(cmd_src);
415         cmd_src = tmp;
416
417         tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
418         g_free(cmd_dst);
419         cmd_dst = tmp;
420     }
421
422     *from = qtest_start(cmd_src);
423     g_free(cmd_src);
424
425     *to = qtest_init(cmd_dst);
426     g_free(cmd_dst);
427 }
428
429 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
430 {
431     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
432
433     qtest_quit(from);
434
435     if (test_dest) {
436         qtest_memread(to, start_address, &dest_byte_a, 1);
437
438         /* Destination still running, wait for a byte to change */
439         do {
440             qtest_memread(to, start_address, &dest_byte_b, 1);
441             usleep(1000 * 10);
442         } while (dest_byte_a == dest_byte_b);
443
444         qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
445
446         /* With it stopped, check nothing changes */
447         qtest_memread(to, start_address, &dest_byte_c, 1);
448         usleep(1000 * 200);
449         qtest_memread(to, start_address, &dest_byte_d, 1);
450         g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
451
452         check_guests_ram(to);
453     }
454
455     qtest_quit(to);
456
457     cleanup("bootsect");
458     cleanup("migsocket");
459     cleanup("src_serial");
460     cleanup("dest_serial");
461 }
462
463 static void deprecated_set_downtime(QTestState *who, const double value)
464 {
465     QDict *rsp;
466     gchar *cmd;
467     char *expected;
468     int64_t result_int;
469
470     cmd = g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
471                           "'arguments': { 'value': %g } }", value);
472     rsp = qtest_qmp(who, cmd);
473     g_free(cmd);
474     g_assert(qdict_haskey(rsp, "return"));
475     qobject_unref(rsp);
476     result_int = value * 1000L;
477     expected = g_strdup_printf("%" PRId64, result_int);
478     migrate_check_parameter(who, "downtime-limit", expected);
479     g_free(expected);
480 }
481
482 static void deprecated_set_speed(QTestState *who, const char *value)
483 {
484     QDict *rsp;
485     gchar *cmd;
486
487     cmd = g_strdup_printf("{ 'execute': 'migrate_set_speed',"
488                           "'arguments': { 'value': %s } }", value);
489     rsp = qtest_qmp(who, cmd);
490     g_free(cmd);
491     g_assert(qdict_haskey(rsp, "return"));
492     qobject_unref(rsp);
493     migrate_check_parameter(who, "max-bandwidth", value);
494 }
495
496 static void test_deprecated(void)
497 {
498     QTestState *from;
499
500     from = qtest_start("");
501
502     deprecated_set_downtime(from, 0.12345);
503     deprecated_set_speed(from, "12345");
504
505     qtest_quit(from);
506 }
507
508 static void test_postcopy(void)
509 {
510     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
511     QTestState *from, *to;
512
513     test_migrate_start(&from, &to, uri, false);
514
515     migrate_set_capability(from, "postcopy-ram", "true");
516     migrate_set_capability(to, "postcopy-ram", "true");
517     migrate_set_capability(to, "postcopy-blocktime", "true");
518
519     /* We want to pick a speed slow enough that the test completes
520      * quickly, but that it doesn't complete precopy even on a slow
521      * machine, so also set the downtime.
522      */
523     migrate_set_parameter(from, "max-bandwidth", "100000000");
524     migrate_set_parameter(from, "downtime-limit", "1");
525
526     /* Wait for the first serial output from the source */
527     wait_for_serial("src_serial");
528
529     migrate(from, uri);
530
531     wait_for_migration_pass(from);
532
533     migrate_start_postcopy(from);
534
535     if (!got_stop) {
536         qtest_qmp_eventwait(from, "STOP");
537     }
538
539     qtest_qmp_eventwait(to, "RESUME");
540
541     wait_for_serial("dest_serial");
542     wait_for_migration_complete(from);
543
544     if (uffd_feature_thread_id) {
545         read_blocktime(to);
546     }
547     g_free(uri);
548
549     test_migrate_end(from, to, true);
550 }
551
552 static void test_baddest(void)
553 {
554     QTestState *from, *to;
555     QDict *rsp, *rsp_return;
556     const char *status;
557     bool failed;
558
559     test_migrate_start(&from, &to, "tcp:0:0", true);
560     migrate(from, "tcp:0:0");
561     do {
562         rsp = wait_command(from, "{ 'execute': 'query-migrate' }");
563         rsp_return = qdict_get_qdict(rsp, "return");
564
565         status = qdict_get_str(rsp_return, "status");
566
567         g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
568         failed = !strcmp(status, "failed");
569         qobject_unref(rsp);
570     } while (!failed);
571
572     /* Is the machine currently running? */
573     rsp = wait_command(from, "{ 'execute': 'query-status' }");
574     g_assert(qdict_haskey(rsp, "return"));
575     rsp_return = qdict_get_qdict(rsp, "return");
576     g_assert(qdict_haskey(rsp_return, "running"));
577     g_assert(qdict_get_bool(rsp_return, "running"));
578     qobject_unref(rsp);
579
580     test_migrate_end(from, to, false);
581 }
582
583 static void test_precopy_unix(void)
584 {
585     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
586     QTestState *from, *to;
587
588     test_migrate_start(&from, &to, uri, false);
589
590     /* We want to pick a speed slow enough that the test completes
591      * quickly, but that it doesn't complete precopy even on a slow
592      * machine, so also set the downtime.
593      */
594     /* 1 ms should make it not converge*/
595     migrate_set_parameter(from, "downtime-limit", "1");
596     /* 1GB/s */
597     migrate_set_parameter(from, "max-bandwidth", "1000000000");
598
599     /* Wait for the first serial output from the source */
600     wait_for_serial("src_serial");
601
602     migrate(from, uri);
603
604     wait_for_migration_pass(from);
605
606     /* 300 ms should converge */
607     migrate_set_parameter(from, "downtime-limit", "300");
608
609     if (!got_stop) {
610         qtest_qmp_eventwait(from, "STOP");
611     }
612
613     qtest_qmp_eventwait(to, "RESUME");
614
615     wait_for_serial("dest_serial");
616     wait_for_migration_complete(from);
617
618     test_migrate_end(from, to, true);
619     g_free(uri);
620 }
621
622 int main(int argc, char **argv)
623 {
624     char template[] = "/tmp/migration-test-XXXXXX";
625     int ret;
626
627     g_test_init(&argc, &argv, NULL);
628
629     if (!ufd_version_check()) {
630         return 0;
631     }
632
633     tmpfs = mkdtemp(template);
634     if (!tmpfs) {
635         g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
636     }
637     g_assert(tmpfs);
638
639     module_call_init(MODULE_INIT_QOM);
640
641     qtest_add_func("/migration/postcopy/unix", test_postcopy);
642     qtest_add_func("/migration/deprecated", test_deprecated);
643     qtest_add_func("/migration/bad_dest", test_baddest);
644     qtest_add_func("/migration/precopy/unix", test_precopy_unix);
645
646     ret = g_test_run();
647
648     g_assert_cmpint(ret, ==, 0);
649
650     ret = rmdir(tmpfs);
651     if (ret != 0) {
652         g_test_message("unable to rmdir: path (%s): %s\n",
653                        tmpfs, strerror(errno));
654     }
655
656     return ret;
657 }
This page took 0.061012 seconds and 4 git commands to generate.