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