log-for-trace.h: Split out parts of log.h used by trace.h
[qemu/ar7.git] / tests / migration-test.c
blob74f9361bdd4c07a5c79c2a1846c6590623a72ba9
1 /*
2 * QTest testcase for migration
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.
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.
13 #include "qemu/osdep.h"
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 #include "hw/nvram/chrp_nvram.h"
24 #define MIN_NVRAM_SIZE 8192 /* from spapr_nvram.c */
26 const unsigned start_address = 1024 * 1024;
27 const unsigned end_address = 100 * 1024 * 1024;
28 bool got_stop;
30 #if defined(__linux__)
31 #include <sys/syscall.h>
32 #include <sys/vfs.h>
33 #endif
35 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
36 #include <sys/eventfd.h>
37 #include <sys/ioctl.h>
38 #include <linux/userfaultfd.h>
40 static bool ufd_version_check(void)
42 struct uffdio_api api_struct;
43 uint64_t ioctl_mask;
45 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
47 if (ufd == -1) {
48 g_test_message("Skipping test: userfaultfd not available");
49 return false;
52 api_struct.api = UFFD_API;
53 api_struct.features = 0;
54 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
55 g_test_message("Skipping test: UFFDIO_API failed");
56 return false;
59 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
60 (__u64)1 << _UFFDIO_UNREGISTER;
61 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
62 g_test_message("Skipping test: Missing userfault feature");
63 return false;
66 return true;
69 #else
70 static bool ufd_version_check(void)
72 g_test_message("Skipping test: Userfault not available (builtdtime)");
73 return false;
76 #endif
78 static const char *tmpfs;
80 /* A simple PC boot sector that modifies memory (1-100MB) quickly
81 * outputting a 'B' every so often if it's still running.
83 #include "tests/migration/x86-a-b-bootblock.h"
85 static void init_bootfile_x86(const char *bootpath)
87 FILE *bootfile = fopen(bootpath, "wb");
89 g_assert_cmpint(fwrite(x86_bootsect, 512, 1, bootfile), ==, 1);
90 fclose(bootfile);
93 static void init_bootfile_ppc(const char *bootpath)
95 FILE *bootfile;
96 char buf[MIN_NVRAM_SIZE];
97 ChrpNvramPartHdr *header = (ChrpNvramPartHdr *)buf;
99 memset(buf, 0, MIN_NVRAM_SIZE);
101 /* Create a "common" partition in nvram to store boot-command property */
103 header->signature = CHRP_NVPART_SYSTEM;
104 memcpy(header->name, "common", 6);
105 chrp_nvram_finish_partition(header, MIN_NVRAM_SIZE);
107 /* FW_MAX_SIZE is 4MB, but slof.bin is only 900KB,
108 * so let's modify memory between 1MB and 100MB
109 * to do like PC bootsector
112 sprintf(buf + 16,
113 "boot-command=hex .\" _\" begin %x %x do i c@ 1 + i c! 1000 +loop "
114 ".\" B\" 0 until", end_address, start_address);
116 /* Write partition to the NVRAM file */
118 bootfile = fopen(bootpath, "wb");
119 g_assert_cmpint(fwrite(buf, MIN_NVRAM_SIZE, 1, bootfile), ==, 1);
120 fclose(bootfile);
124 * Wait for some output in the serial output file,
125 * we get an 'A' followed by an endless string of 'B's
126 * but on the destination we won't have the A.
128 static void wait_for_serial(const char *side)
130 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
131 FILE *serialfile = fopen(serialpath, "r");
132 const char *arch = qtest_get_arch();
133 int started = (strcmp(side, "src_serial") == 0 &&
134 strcmp(arch, "ppc64") == 0) ? 0 : 1;
136 g_free(serialpath);
137 do {
138 int readvalue = fgetc(serialfile);
140 if (!started) {
141 /* SLOF prints its banner before starting test,
142 * to ignore it, mark the start of the test with '_',
143 * ignore all characters until this marker
145 switch (readvalue) {
146 case '_':
147 started = 1;
148 break;
149 case EOF:
150 fseek(serialfile, 0, SEEK_SET);
151 usleep(1000);
152 break;
154 continue;
156 switch (readvalue) {
157 case 'A':
158 /* Fine */
159 break;
161 case 'B':
162 /* It's alive! */
163 fclose(serialfile);
164 return;
166 case EOF:
167 started = (strcmp(side, "src_serial") == 0 &&
168 strcmp(arch, "ppc64") == 0) ? 0 : 1;
169 fseek(serialfile, 0, SEEK_SET);
170 usleep(1000);
171 break;
173 default:
174 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
175 g_assert_not_reached();
177 } while (true);
181 * Events can get in the way of responses we are actually waiting for.
183 static QDict *wait_command(QTestState *who, const char *command)
185 const char *event_string;
186 QDict *response;
188 response = qtest_qmp(who, command);
190 while (qdict_haskey(response, "event")) {
191 /* OK, it was an event */
192 event_string = qdict_get_str(response, "event");
193 if (!strcmp(event_string, "STOP")) {
194 got_stop = true;
196 QDECREF(response);
197 response = qtest_qmp_receive(who);
199 return response;
204 * It's tricky to use qemu's migration event capability with qtest,
205 * events suddenly appearing confuse the qmp()/hmp() responses.
208 static uint64_t get_migration_pass(QTestState *who)
210 QDict *rsp, *rsp_return, *rsp_ram;
211 uint64_t result;
213 rsp = wait_command(who, "{ 'execute': 'query-migrate' }");
214 rsp_return = qdict_get_qdict(rsp, "return");
215 if (!qdict_haskey(rsp_return, "ram")) {
216 /* Still in setup */
217 result = 0;
218 } else {
219 rsp_ram = qdict_get_qdict(rsp_return, "ram");
220 result = qdict_get_try_int(rsp_ram, "dirty-sync-count", 0);
222 QDECREF(rsp);
223 return result;
226 static void wait_for_migration_complete(QTestState *who)
228 while (true) {
229 QDict *rsp, *rsp_return;
230 bool completed;
231 const char *status;
233 rsp = wait_command(who, "{ 'execute': 'query-migrate' }");
234 rsp_return = qdict_get_qdict(rsp, "return");
235 status = qdict_get_str(rsp_return, "status");
236 completed = strcmp(status, "completed") == 0;
237 g_assert_cmpstr(status, !=, "failed");
238 QDECREF(rsp);
239 if (completed) {
240 return;
242 usleep(1000);
246 static void wait_for_migration_pass(QTestState *who)
248 uint64_t initial_pass = get_migration_pass(who);
249 uint64_t pass;
251 /* Wait for the 1st sync */
252 while (!got_stop && !initial_pass) {
253 usleep(1000);
254 initial_pass = get_migration_pass(who);
257 do {
258 usleep(1000);
259 pass = get_migration_pass(who);
260 } while (pass == initial_pass && !got_stop);
263 static void check_guests_ram(QTestState *who)
265 /* Our ASM test will have been incrementing one byte from each page from
266 * 1MB to <100MB in order.
267 * This gives us a constraint that any page's byte should be equal or less
268 * than the previous pages byte (mod 256); and they should all be equal
269 * except for one transition at the point where we meet the incrementer.
270 * (We're running this with the guest stopped).
272 unsigned address;
273 uint8_t first_byte;
274 uint8_t last_byte;
275 bool hit_edge = false;
276 bool bad = false;
278 qtest_memread(who, start_address, &first_byte, 1);
279 last_byte = first_byte;
281 for (address = start_address + 4096; address < end_address; address += 4096)
283 uint8_t b;
284 qtest_memread(who, address, &b, 1);
285 if (b != last_byte) {
286 if (((b + 1) % 256) == last_byte && !hit_edge) {
287 /* This is OK, the guest stopped at the point of
288 * incrementing the previous page but didn't get
289 * to us yet.
291 hit_edge = true;
292 } else {
293 fprintf(stderr, "Memory content inconsistency at %x"
294 " first_byte = %x last_byte = %x current = %x"
295 " hit_edge = %x\n",
296 address, first_byte, last_byte, b, hit_edge);
297 bad = true;
300 last_byte = b;
302 g_assert_false(bad);
305 static void cleanup(const char *filename)
307 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
309 unlink(path);
310 g_free(path);
313 static void migrate_check_parameter(QTestState *who, const char *parameter,
314 const char *value)
316 QDict *rsp, *rsp_return;
317 char *result;
319 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
320 rsp_return = qdict_get_qdict(rsp, "return");
321 result = g_strdup_printf("%" PRId64,
322 qdict_get_try_int(rsp_return, parameter, -1));
323 g_assert_cmpstr(result, ==, value);
324 g_free(result);
325 QDECREF(rsp);
328 static void migrate_set_parameter(QTestState *who, const char *parameter,
329 const char *value)
331 QDict *rsp;
332 gchar *cmd;
334 cmd = g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
335 "'arguments': { '%s': %s } }",
336 parameter, value);
337 rsp = qtest_qmp(who, cmd);
338 g_free(cmd);
339 g_assert(qdict_haskey(rsp, "return"));
340 QDECREF(rsp);
341 migrate_check_parameter(who, parameter, value);
344 static void migrate_set_capability(QTestState *who, const char *capability,
345 const char *value)
347 QDict *rsp;
348 gchar *cmd;
350 cmd = g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
351 "'arguments': { "
352 "'capabilities': [ { "
353 "'capability': '%s', 'state': %s } ] } }",
354 capability, value);
355 rsp = qtest_qmp(who, cmd);
356 g_free(cmd);
357 g_assert(qdict_haskey(rsp, "return"));
358 QDECREF(rsp);
361 static void migrate(QTestState *who, const char *uri)
363 QDict *rsp;
364 gchar *cmd;
366 cmd = g_strdup_printf("{ 'execute': 'migrate',"
367 "'arguments': { 'uri': '%s' } }",
368 uri);
369 rsp = qtest_qmp(who, cmd);
370 g_free(cmd);
371 g_assert(qdict_haskey(rsp, "return"));
372 QDECREF(rsp);
375 static void migrate_start_postcopy(QTestState *who)
377 QDict *rsp;
379 rsp = wait_command(who, "{ 'execute': 'migrate-start-postcopy' }");
380 g_assert(qdict_haskey(rsp, "return"));
381 QDECREF(rsp);
384 static void test_migrate_start(QTestState **from, QTestState **to,
385 const char *uri)
387 gchar *cmd_src, *cmd_dst;
388 char *bootpath = g_strdup_printf("%s/bootsect", tmpfs);
389 const char *arch = qtest_get_arch();
390 const char *accel = "kvm:tcg";
392 got_stop = false;
394 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
395 init_bootfile_x86(bootpath);
396 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
397 " -name source,debug-threads=on"
398 " -serial file:%s/src_serial"
399 " -drive file=%s,format=raw",
400 accel, tmpfs, bootpath);
401 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
402 " -name target,debug-threads=on"
403 " -serial file:%s/dest_serial"
404 " -drive file=%s,format=raw"
405 " -incoming %s",
406 accel, tmpfs, bootpath, uri);
407 } else if (strcmp(arch, "ppc64") == 0) {
409 /* On ppc64, the test only works with kvm-hv, but not with kvm-pr */
410 if (access("/sys/module/kvm_hv", F_OK)) {
411 accel = "tcg";
413 init_bootfile_ppc(bootpath);
414 cmd_src = g_strdup_printf("-machine accel=%s -m 256M"
415 " -name source,debug-threads=on"
416 " -serial file:%s/src_serial"
417 " -drive file=%s,if=pflash,format=raw",
418 accel, tmpfs, bootpath);
419 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
420 " -name target,debug-threads=on"
421 " -serial file:%s/dest_serial"
422 " -incoming %s",
423 accel, tmpfs, uri);
424 } else {
425 g_assert_not_reached();
428 g_free(bootpath);
430 *from = qtest_start(cmd_src);
431 g_free(cmd_src);
433 *to = qtest_init(cmd_dst);
434 g_free(cmd_dst);
437 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
439 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
441 qtest_quit(from);
443 if (test_dest) {
444 qtest_memread(to, start_address, &dest_byte_a, 1);
446 /* Destination still running, wait for a byte to change */
447 do {
448 qtest_memread(to, start_address, &dest_byte_b, 1);
449 usleep(1000 * 10);
450 } while (dest_byte_a == dest_byte_b);
452 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
454 /* With it stopped, check nothing changes */
455 qtest_memread(to, start_address, &dest_byte_c, 1);
456 usleep(1000 * 200);
457 qtest_memread(to, start_address, &dest_byte_d, 1);
458 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
460 check_guests_ram(to);
463 qtest_quit(to);
465 cleanup("bootsect");
466 cleanup("migsocket");
467 cleanup("src_serial");
468 cleanup("dest_serial");
471 static void deprecated_set_downtime(QTestState *who, const double value)
473 QDict *rsp;
474 gchar *cmd;
475 char *expected;
476 int64_t result_int;
478 cmd = g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
479 "'arguments': { 'value': %g } }", value);
480 rsp = qtest_qmp(who, cmd);
481 g_free(cmd);
482 g_assert(qdict_haskey(rsp, "return"));
483 QDECREF(rsp);
484 result_int = value * 1000L;
485 expected = g_strdup_printf("%" PRId64, result_int);
486 migrate_check_parameter(who, "downtime-limit", expected);
487 g_free(expected);
490 static void deprecated_set_speed(QTestState *who, const char *value)
492 QDict *rsp;
493 gchar *cmd;
495 cmd = g_strdup_printf("{ 'execute': 'migrate_set_speed',"
496 "'arguments': { 'value': %s } }", value);
497 rsp = qtest_qmp(who, cmd);
498 g_free(cmd);
499 g_assert(qdict_haskey(rsp, "return"));
500 QDECREF(rsp);
501 migrate_check_parameter(who, "max-bandwidth", value);
504 static void test_deprecated(void)
506 QTestState *from;
508 from = qtest_start("");
510 deprecated_set_downtime(from, 0.12345);
511 deprecated_set_speed(from, "12345");
513 qtest_quit(from);
516 static void test_migrate(void)
518 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
519 QTestState *from, *to;
521 test_migrate_start(&from, &to, uri);
523 migrate_set_capability(from, "postcopy-ram", "true");
524 migrate_set_capability(to, "postcopy-ram", "true");
526 /* We want to pick a speed slow enough that the test completes
527 * quickly, but that it doesn't complete precopy even on a slow
528 * machine, so also set the downtime.
530 migrate_set_parameter(from, "max-bandwidth", "100000000");
531 migrate_set_parameter(from, "downtime-limit", "1");
533 /* Wait for the first serial output from the source */
534 wait_for_serial("src_serial");
536 migrate(from, uri);
538 wait_for_migration_pass(from);
540 migrate_start_postcopy(from);
542 if (!got_stop) {
543 qtest_qmp_eventwait(from, "STOP");
546 qtest_qmp_eventwait(to, "RESUME");
548 wait_for_serial("dest_serial");
549 wait_for_migration_complete(from);
551 g_free(uri);
553 test_migrate_end(from, to, true);
556 static void test_baddest(void)
558 QTestState *from, *to;
559 QDict *rsp, *rsp_return;
560 const char *status;
561 bool failed;
563 test_migrate_start(&from, &to, "tcp:0:0");
564 migrate(from, "tcp:0:0");
565 do {
566 rsp = wait_command(from, "{ 'execute': 'query-migrate' }");
567 rsp_return = qdict_get_qdict(rsp, "return");
569 status = qdict_get_str(rsp_return, "status");
571 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
572 failed = !strcmp(status, "failed");
573 QDECREF(rsp);
574 } while (!failed);
576 /* Is the machine currently running? */
577 rsp = wait_command(from, "{ 'execute': 'query-status' }");
578 g_assert(qdict_haskey(rsp, "return"));
579 rsp_return = qdict_get_qdict(rsp, "return");
580 g_assert(qdict_haskey(rsp_return, "running"));
581 g_assert(qdict_get_bool(rsp_return, "running"));
582 QDECREF(rsp);
584 test_migrate_end(from, to, false);
587 int main(int argc, char **argv)
589 char template[] = "/tmp/migration-test-XXXXXX";
590 int ret;
592 g_test_init(&argc, &argv, NULL);
594 if (!ufd_version_check()) {
595 return 0;
598 tmpfs = mkdtemp(template);
599 if (!tmpfs) {
600 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
602 g_assert(tmpfs);
604 module_call_init(MODULE_INIT_QOM);
606 qtest_add_func("/migration/postcopy/unix", test_migrate);
607 qtest_add_func("/migration/deprecated", test_deprecated);
608 qtest_add_func("/migration/bad_dest", test_baddest);
610 ret = g_test_run();
612 g_assert_cmpint(ret, ==, 0);
614 ret = rmdir(tmpfs);
615 if (ret != 0) {
616 g_test_message("unable to rmdir: path (%s): %s\n",
617 tmpfs, strerror(errno));
620 return ret;