]> git.proxmox.com Git - mirror_qemu.git/blame - tests/migration-test.c
tests: Pass literal format strings directly to qmp_FOO()
[mirror_qemu.git] / tests / migration-test.c
CommitLineData
ea0c6d62 1/*
2656bfd9 2 * QTest testcase for migration
ea0c6d62 3 *
17ca7746 4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
ea0c6d62
DDAG
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"
ea0c6d62
DDAG
14
15#include "libqtest.h"
452fcdbc 16#include "qapi/qmp/qdict.h"
ea0c6d62
DDAG
17#include "qemu/option.h"
18#include "qemu/range.h"
a9c94277 19#include "qemu/sockets.h"
8228e353 20#include "chardev/char.h"
ea0c6d62
DDAG
21#include "sysemu/sysemu.h"
22
055a1efc
MA
23/* TODO actually test the results and get rid of this */
24#define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
25
ea0c6d62
DDAG
26const unsigned start_address = 1024 * 1024;
27const unsigned end_address = 100 * 1024 * 1024;
28bool got_stop;
346f3dab 29static bool uffd_feature_thread_id;
ea0c6d62
DDAG
30
31#if defined(__linux__)
ea0c6d62
DDAG
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
41static bool ufd_version_check(void)
42{
43 struct uffdio_api api_struct;
44 uint64_t ioctl_mask;
45
e1ae9fb6 46 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
ea0c6d62
DDAG
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 }
346f3dab 59 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
ea0c6d62
DDAG
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
72static bool ufd_version_check(void)
73{
74 g_test_message("Skipping test: Userfault not available (builtdtime)");
75 return false;
76}
77
78#endif
79
80static const char *tmpfs;
81
82/* A simple PC boot sector that modifies memory (1-100MB) quickly
17ca7746 83 * outputting a 'B' every so often if it's still running.
ea0c6d62 84 */
17ca7746 85#include "tests/migration/x86-a-b-bootblock.h"
ea0c6d62 86
aaf89c8a 87static void init_bootfile_x86(const char *bootpath)
88{
89 FILE *bootfile = fopen(bootpath, "wb");
90
17ca7746 91 g_assert_cmpint(fwrite(x86_bootsect, 512, 1, bootfile), ==, 1);
aaf89c8a 92 fclose(bootfile);
93}
94
ea0c6d62
DDAG
95/*
96 * Wait for some output in the serial output file,
97 * we get an 'A' followed by an endless string of 'B's
98 * but on the destination we won't have the A.
99 */
100static void wait_for_serial(const char *side)
101{
102 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
103 FILE *serialfile = fopen(serialpath, "r");
aaf89c8a 104 const char *arch = qtest_get_arch();
105 int started = (strcmp(side, "src_serial") == 0 &&
106 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62 107
e2dd21e5 108 g_free(serialpath);
ea0c6d62
DDAG
109 do {
110 int readvalue = fgetc(serialfile);
111
aaf89c8a 112 if (!started) {
113 /* SLOF prints its banner before starting test,
114 * to ignore it, mark the start of the test with '_',
115 * ignore all characters until this marker
116 */
117 switch (readvalue) {
118 case '_':
119 started = 1;
120 break;
121 case EOF:
122 fseek(serialfile, 0, SEEK_SET);
123 usleep(1000);
124 break;
125 }
126 continue;
127 }
ea0c6d62
DDAG
128 switch (readvalue) {
129 case 'A':
130 /* Fine */
131 break;
132
133 case 'B':
134 /* It's alive! */
135 fclose(serialfile);
ea0c6d62
DDAG
136 return;
137
138 case EOF:
aaf89c8a 139 started = (strcmp(side, "src_serial") == 0 &&
140 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62
DDAG
141 fseek(serialfile, 0, SEEK_SET);
142 usleep(1000);
143 break;
144
145 default:
146 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
147 g_assert_not_reached();
148 }
149 } while (true);
150}
151
152/*
153 * Events can get in the way of responses we are actually waiting for.
154 */
863e27a8 155static QDict *wait_command(QTestState *who, const char *command)
ea0c6d62
DDAG
156{
157 const char *event_string;
863e27a8
JQ
158 QDict *response;
159
160 response = qtest_qmp(who, command);
ea0c6d62 161
863e27a8
JQ
162 while (qdict_haskey(response, "event")) {
163 /* OK, it was an event */
164 event_string = qdict_get_str(response, "event");
165 if (!strcmp(event_string, "STOP")) {
166 got_stop = true;
167 }
cb3e7f08 168 qobject_unref(response);
863e27a8 169 response = qtest_qmp_receive(who);
ea0c6d62 170 }
863e27a8 171 return response;
ea0c6d62
DDAG
172}
173
2f7074c6
PX
174/*
175 * Note: caller is responsible to free the returned object via
176 * qobject_unref() after use
177 */
178static QDict *migrate_query(QTestState *who)
179{
180 QDict *rsp, *rsp_return;
181
182 rsp = wait_command(who, "{ 'execute': 'query-migrate' }");
183 rsp_return = qdict_get_qdict(rsp, "return");
184 g_assert(rsp_return);
185 qobject_ref(rsp_return);
186 qobject_unref(rsp);
187
188 return rsp_return;
189}
190
191/*
192 * Note: caller is responsible to free the returned object via
193 * g_free() after use
194 */
195static gchar *migrate_query_status(QTestState *who)
196{
197 QDict *rsp_return = migrate_query(who);
198 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
199
200 g_assert(status);
201 qobject_unref(rsp_return);
202
203 return status;
204}
ea0c6d62
DDAG
205
206/*
207 * It's tricky to use qemu's migration event capability with qtest,
208 * events suddenly appearing confuse the qmp()/hmp() responses.
ea0c6d62
DDAG
209 */
210
863e27a8 211static uint64_t get_migration_pass(QTestState *who)
ea0c6d62 212{
2f7074c6 213 QDict *rsp_return, *rsp_ram;
ea0c6d62
DDAG
214 uint64_t result;
215
2f7074c6 216 rsp_return = migrate_query(who);
ea0c6d62
DDAG
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);
ea0c6d62 223 }
2f7074c6 224 qobject_unref(rsp_return);
ea0c6d62
DDAG
225 return result;
226}
227
346f3dab
AP
228static void read_blocktime(QTestState *who)
229{
2f7074c6 230 QDict *rsp_return;
346f3dab 231
2f7074c6 232 rsp_return = migrate_query(who);
346f3dab 233 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
2f7074c6 234 qobject_unref(rsp_return);
346f3dab
AP
235}
236
2f6d3138
PX
237static void wait_for_migration_status(QTestState *who,
238 const char *goal)
ea0c6d62 239{
6a7724e9 240 while (true) {
6a7724e9 241 bool completed;
2f7074c6 242 char *status;
ea0c6d62 243
2f7074c6 244 status = migrate_query_status(who);
2f6d3138 245 completed = strcmp(status, goal) == 0;
ea0c6d62 246 g_assert_cmpstr(status, !=, "failed");
2f7074c6 247 g_free(status);
6a7724e9
JQ
248 if (completed) {
249 return;
250 }
251 usleep(1000);
252 }
ea0c6d62
DDAG
253}
254
2f6d3138
PX
255static void wait_for_migration_complete(QTestState *who)
256{
257 wait_for_migration_status(who, "completed");
258}
259
863e27a8 260static void wait_for_migration_pass(QTestState *who)
ea0c6d62 261{
863e27a8 262 uint64_t initial_pass = get_migration_pass(who);
ea0c6d62
DDAG
263 uint64_t pass;
264
265 /* Wait for the 1st sync */
6a7724e9
JQ
266 while (!got_stop && !initial_pass) {
267 usleep(1000);
863e27a8 268 initial_pass = get_migration_pass(who);
6a7724e9 269 }
ea0c6d62
DDAG
270
271 do {
6a7724e9 272 usleep(1000);
863e27a8 273 pass = get_migration_pass(who);
ea0c6d62
DDAG
274 } while (pass == initial_pass && !got_stop);
275}
276
7195a871 277static void check_guests_ram(QTestState *who)
ea0c6d62
DDAG
278{
279 /* Our ASM test will have been incrementing one byte from each page from
280 * 1MB to <100MB in order.
281 * This gives us a constraint that any page's byte should be equal or less
282 * than the previous pages byte (mod 256); and they should all be equal
283 * except for one transition at the point where we meet the incrementer.
284 * (We're running this with the guest stopped).
285 */
286 unsigned address;
287 uint8_t first_byte;
288 uint8_t last_byte;
289 bool hit_edge = false;
290 bool bad = false;
291
7195a871 292 qtest_memread(who, start_address, &first_byte, 1);
ea0c6d62
DDAG
293 last_byte = first_byte;
294
295 for (address = start_address + 4096; address < end_address; address += 4096)
296 {
297 uint8_t b;
7195a871 298 qtest_memread(who, address, &b, 1);
ea0c6d62
DDAG
299 if (b != last_byte) {
300 if (((b + 1) % 256) == last_byte && !hit_edge) {
301 /* This is OK, the guest stopped at the point of
302 * incrementing the previous page but didn't get
303 * to us yet.
304 */
305 hit_edge = true;
829db8b4 306 last_byte = b;
ea0c6d62
DDAG
307 } else {
308 fprintf(stderr, "Memory content inconsistency at %x"
309 " first_byte = %x last_byte = %x current = %x"
310 " hit_edge = %x\n",
311 address, first_byte, last_byte, b, hit_edge);
312 bad = true;
313 }
314 }
ea0c6d62
DDAG
315 }
316 g_assert_false(bad);
317}
318
319static void cleanup(const char *filename)
320{
321 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
322
323 unlink(path);
e2dd21e5 324 g_free(path);
ea0c6d62
DDAG
325}
326
56b4a42a
JQ
327static void migrate_check_parameter(QTestState *who, const char *parameter,
328 const char *value)
329{
330 QDict *rsp, *rsp_return;
9c43435d 331 char *result;
56b4a42a
JQ
332
333 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
334 rsp_return = qdict_get_qdict(rsp, "return");
335 result = g_strdup_printf("%" PRId64,
336 qdict_get_try_int(rsp_return, parameter, -1));
337 g_assert_cmpstr(result, ==, value);
9c43435d 338 g_free(result);
cb3e7f08 339 qobject_unref(rsp);
56b4a42a
JQ
340}
341
1f90d797
JQ
342static void migrate_set_parameter(QTestState *who, const char *parameter,
343 const char *value)
d62fbe60
JQ
344{
345 QDict *rsp;
346 gchar *cmd;
347
1f90d797
JQ
348 cmd = g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
349 "'arguments': { '%s': %s } }",
350 parameter, value);
d62fbe60
JQ
351 rsp = qtest_qmp(who, cmd);
352 g_free(cmd);
353 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 354 qobject_unref(rsp);
1f90d797 355 migrate_check_parameter(who, parameter, value);
d62fbe60
JQ
356}
357
d5f49640
PX
358static void migrate_pause(QTestState *who)
359{
360 QDict *rsp;
361
362 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
363 g_assert(qdict_haskey(rsp, "return"));
364 qobject_unref(rsp);
365}
366
367static void migrate_recover(QTestState *who, const char *uri)
368{
369 QDict *rsp;
370 gchar *cmd = g_strdup_printf(
371 "{ 'execute': 'migrate-recover', "
372 " 'id': 'recover-cmd', "
373 " 'arguments': { 'uri': '%s' } }", uri);
374
375 rsp = wait_command(who, cmd);
376 g_assert(qdict_haskey(rsp, "return"));
377 g_free(cmd);
378 qobject_unref(rsp);
379}
380
d62fbe60
JQ
381static void migrate_set_capability(QTestState *who, const char *capability,
382 const char *value)
383{
384 QDict *rsp;
385 gchar *cmd;
386
387 cmd = g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
388 "'arguments': { "
389 "'capabilities': [ { "
390 "'capability': '%s', 'state': %s } ] } }",
391 capability, value);
392 rsp = qtest_qmp(who, cmd);
393 g_free(cmd);
394 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 395 qobject_unref(rsp);
d62fbe60
JQ
396}
397
7e1d7427 398static void migrate(QTestState *who, const char *uri, const char *extra)
d62fbe60
JQ
399{
400 QDict *rsp;
401 gchar *cmd;
402
403 cmd = g_strdup_printf("{ 'execute': 'migrate',"
7e1d7427
PX
404 " 'arguments': { 'uri': '%s' %s } }",
405 uri, extra ? extra : "");
d62fbe60
JQ
406 rsp = qtest_qmp(who, cmd);
407 g_free(cmd);
408 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 409 qobject_unref(rsp);
d62fbe60
JQ
410}
411
d131662a 412static void migrate_postcopy_start(QTestState *from, QTestState *to)
eb665d7d
JQ
413{
414 QDict *rsp;
415
d131662a 416 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
eb665d7d 417 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 418 qobject_unref(rsp);
d131662a
PX
419
420 if (!got_stop) {
421 qtest_qmp_eventwait(from, "STOP");
422 }
423
424 qtest_qmp_eventwait(to, "RESUME");
eb665d7d
JQ
425}
426
5fd4a9c9 427static int test_migrate_start(QTestState **from, QTestState **to,
f96d6651 428 const char *uri, bool hide_stderr)
ea0c6d62 429{
d62fbe60 430 gchar *cmd_src, *cmd_dst;
ea0c6d62 431 char *bootpath = g_strdup_printf("%s/bootsect", tmpfs);
aaf89c8a 432 const char *arch = qtest_get_arch();
63b2d935 433 const char *accel = "kvm:tcg";
ea0c6d62
DDAG
434
435 got_stop = false;
ea0c6d62 436
aaf89c8a 437 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
438 init_bootfile_x86(bootpath);
63b2d935 439 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
31a6bb74 440 " -name source,debug-threads=on"
aaf89c8a 441 " -serial file:%s/src_serial"
442 " -drive file=%s,format=raw",
63b2d935
JQ
443 accel, tmpfs, bootpath);
444 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
31a6bb74 445 " -name target,debug-threads=on"
aaf89c8a 446 " -serial file:%s/dest_serial"
447 " -drive file=%s,format=raw"
448 " -incoming %s",
63b2d935 449 accel, tmpfs, bootpath, uri);
aaf89c8a 450 } else if (strcmp(arch, "ppc64") == 0) {
171da9d5 451
5fd4a9c9
DDAG
452 /* On ppc64, the test only works with kvm-hv, but not with kvm-pr
453 * and TCG is touchy due to race conditions on dirty bits
454 * (especially on PPC for some reason)
455 */
63b2d935 456 if (access("/sys/module/kvm_hv", F_OK)) {
5fd4a9c9
DDAG
457 g_print("Skipping test: kvm_hv not available ");
458 return -1;
63b2d935 459 }
171da9d5 460 cmd_src = g_strdup_printf("-machine accel=%s -m 256M"
31a6bb74 461 " -name source,debug-threads=on"
aaf89c8a 462 " -serial file:%s/src_serial"
cdf33815
JQ
463 " -prom-env '"
464 "boot-command=hex .\" _\" begin %x %x "
465 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
466 "until'", accel, tmpfs, end_address,
467 start_address);
171da9d5 468 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
31a6bb74 469 " -name target,debug-threads=on"
aaf89c8a 470 " -serial file:%s/dest_serial"
471 " -incoming %s",
171da9d5 472 accel, tmpfs, uri);
aaf89c8a 473 } else {
474 g_assert_not_reached();
475 }
476
e2dd21e5
MAL
477 g_free(bootpath);
478
f96d6651
DDAG
479 if (hide_stderr) {
480 gchar *tmp;
481 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
482 g_free(cmd_src);
483 cmd_src = tmp;
484
485 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
486 g_free(cmd_dst);
487 cmd_dst = tmp;
488 }
489
7195a871 490 *from = qtest_start(cmd_src);
aaf89c8a 491 g_free(cmd_src);
492
7195a871 493 *to = qtest_init(cmd_dst);
aaf89c8a 494 g_free(cmd_dst);
5fd4a9c9 495 return 0;
7195a871
JQ
496}
497
2c9bb297 498static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
7195a871
JQ
499{
500 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
501
502 qtest_quit(from);
503
2c9bb297
DDAG
504 if (test_dest) {
505 qtest_memread(to, start_address, &dest_byte_a, 1);
7195a871 506
2c9bb297
DDAG
507 /* Destination still running, wait for a byte to change */
508 do {
509 qtest_memread(to, start_address, &dest_byte_b, 1);
510 usleep(1000 * 10);
511 } while (dest_byte_a == dest_byte_b);
512
513 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
7195a871 514
2c9bb297
DDAG
515 /* With it stopped, check nothing changes */
516 qtest_memread(to, start_address, &dest_byte_c, 1);
517 usleep(1000 * 200);
518 qtest_memread(to, start_address, &dest_byte_d, 1);
519 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
7195a871 520
2c9bb297
DDAG
521 check_guests_ram(to);
522 }
7195a871
JQ
523
524 qtest_quit(to);
525
526 cleanup("bootsect");
527 cleanup("migsocket");
528 cleanup("src_serial");
529 cleanup("dest_serial");
530}
531
4c27486d
JQ
532static void deprecated_set_downtime(QTestState *who, const double value)
533{
534 QDict *rsp;
535 gchar *cmd;
536 char *expected;
537 int64_t result_int;
538
539 cmd = g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
540 "'arguments': { 'value': %g } }", value);
541 rsp = qtest_qmp(who, cmd);
542 g_free(cmd);
543 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 544 qobject_unref(rsp);
4c27486d
JQ
545 result_int = value * 1000L;
546 expected = g_strdup_printf("%" PRId64, result_int);
547 migrate_check_parameter(who, "downtime-limit", expected);
548 g_free(expected);
549}
550
551static void deprecated_set_speed(QTestState *who, const char *value)
552{
553 QDict *rsp;
554 gchar *cmd;
555
556 cmd = g_strdup_printf("{ 'execute': 'migrate_set_speed',"
557 "'arguments': { 'value': %s } }", value);
558 rsp = qtest_qmp(who, cmd);
559 g_free(cmd);
560 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 561 qobject_unref(rsp);
4c27486d
JQ
562 migrate_check_parameter(who, "max-bandwidth", value);
563}
564
565static void test_deprecated(void)
566{
567 QTestState *from;
568
569 from = qtest_start("");
570
571 deprecated_set_downtime(from, 0.12345);
572 deprecated_set_speed(from, "12345");
573
574 qtest_quit(from);
575}
576
d131662a 577static int migrate_postcopy_prepare(QTestState **from_ptr,
3e81f73c
PX
578 QTestState **to_ptr,
579 bool hide_error)
7195a871
JQ
580{
581 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
863e27a8 582 QTestState *from, *to;
7195a871 583
3e81f73c 584 if (test_migrate_start(&from, &to, uri, hide_error)) {
d131662a 585 return -1;
5fd4a9c9 586 }
ea0c6d62 587
d62fbe60
JQ
588 migrate_set_capability(from, "postcopy-ram", "true");
589 migrate_set_capability(to, "postcopy-ram", "true");
346f3dab 590 migrate_set_capability(to, "postcopy-blocktime", "true");
ea0c6d62
DDAG
591
592 /* We want to pick a speed slow enough that the test completes
593 * quickly, but that it doesn't complete precopy even on a slow
594 * machine, so also set the downtime.
595 */
1f90d797
JQ
596 migrate_set_parameter(from, "max-bandwidth", "100000000");
597 migrate_set_parameter(from, "downtime-limit", "1");
ea0c6d62
DDAG
598
599 /* Wait for the first serial output from the source */
600 wait_for_serial("src_serial");
601
7e1d7427 602 migrate(from, uri, NULL);
d131662a 603 g_free(uri);
ea0c6d62 604
863e27a8 605 wait_for_migration_pass(from);
ea0c6d62 606
d131662a
PX
607 *from_ptr = from;
608 *to_ptr = to;
ea0c6d62 609
d131662a
PX
610 return 0;
611}
ea0c6d62 612
d131662a
PX
613static void migrate_postcopy_complete(QTestState *from, QTestState *to)
614{
615 wait_for_migration_complete(from);
ea0c6d62 616
d131662a 617 /* Make sure we get at least one "B" on destination */
ea0c6d62 618 wait_for_serial("dest_serial");
ea0c6d62 619
346f3dab
AP
620 if (uffd_feature_thread_id) {
621 read_blocktime(to);
622 }
ea0c6d62 623
2c9bb297
DDAG
624 test_migrate_end(from, to, true);
625}
626
d131662a
PX
627static void test_postcopy(void)
628{
629 QTestState *from, *to;
630
3e81f73c 631 if (migrate_postcopy_prepare(&from, &to, false)) {
d131662a
PX
632 return;
633 }
634 migrate_postcopy_start(from, to);
635 migrate_postcopy_complete(from, to);
636}
637
d5f49640
PX
638static void test_postcopy_recovery(void)
639{
640 QTestState *from, *to;
641 char *uri;
642
3e81f73c 643 if (migrate_postcopy_prepare(&from, &to, true)) {
d5f49640
PX
644 return;
645 }
646
647 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
648 migrate_set_parameter(from, "max-postcopy-bandwidth", "4096");
649
650 /* Now we start the postcopy */
651 migrate_postcopy_start(from, to);
652
653 /*
654 * Wait until postcopy is really started; we can only run the
655 * migrate-pause command during a postcopy
656 */
657 wait_for_migration_status(from, "postcopy-active");
658
659 /*
660 * Manually stop the postcopy migration. This emulates a network
661 * failure with the migration socket
662 */
663 migrate_pause(from);
664
665 /*
666 * Wait for destination side to reach postcopy-paused state. The
667 * migrate-recover command can only succeed if destination machine
668 * is in the paused state
669 */
670 wait_for_migration_status(to, "postcopy-paused");
671
672 /*
673 * Create a new socket to emulate a new channel that is different
674 * from the broken migration channel; tell the destination to
675 * listen to the new port
676 */
677 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
678 migrate_recover(to, uri);
679
680 /*
681 * Try to rebuild the migration channel using the resume flag and
682 * the newly created channel
683 */
684 wait_for_migration_status(from, "postcopy-paused");
685 migrate(from, uri, ", 'resume': true");
686 g_free(uri);
687
688 /* Restore the postcopy bandwidth to unlimited */
689 migrate_set_parameter(from, "max-postcopy-bandwidth", "0");
690
691 migrate_postcopy_complete(from, to);
692}
693
2c9bb297
DDAG
694static void test_baddest(void)
695{
696 QTestState *from, *to;
697 QDict *rsp, *rsp_return;
2f7074c6 698 char *status;
2c9bb297
DDAG
699 bool failed;
700
5fd4a9c9
DDAG
701 if (test_migrate_start(&from, &to, "tcp:0:0", true)) {
702 return;
703 }
7e1d7427 704 migrate(from, "tcp:0:0", NULL);
2c9bb297 705 do {
2f7074c6 706 status = migrate_query_status(from);
2c9bb297
DDAG
707 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
708 failed = !strcmp(status, "failed");
2f7074c6 709 g_free(status);
2c9bb297
DDAG
710 } while (!failed);
711
712 /* Is the machine currently running? */
713 rsp = wait_command(from, "{ 'execute': 'query-status' }");
714 g_assert(qdict_haskey(rsp, "return"));
715 rsp_return = qdict_get_qdict(rsp, "return");
716 g_assert(qdict_haskey(rsp_return, "running"));
717 g_assert(qdict_get_bool(rsp_return, "running"));
cb3e7f08 718 qobject_unref(rsp);
2c9bb297
DDAG
719
720 test_migrate_end(from, to, false);
ea0c6d62
DDAG
721}
722
2884100c
JQ
723static void test_precopy_unix(void)
724{
725 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
726 QTestState *from, *to;
727
5fd4a9c9
DDAG
728 if (test_migrate_start(&from, &to, uri, false)) {
729 return;
730 }
2884100c
JQ
731
732 /* We want to pick a speed slow enough that the test completes
733 * quickly, but that it doesn't complete precopy even on a slow
734 * machine, so also set the downtime.
735 */
736 /* 1 ms should make it not converge*/
737 migrate_set_parameter(from, "downtime-limit", "1");
738 /* 1GB/s */
739 migrate_set_parameter(from, "max-bandwidth", "1000000000");
740
741 /* Wait for the first serial output from the source */
742 wait_for_serial("src_serial");
743
7e1d7427 744 migrate(from, uri, NULL);
2884100c
JQ
745
746 wait_for_migration_pass(from);
747
748 /* 300 ms should converge */
749 migrate_set_parameter(from, "downtime-limit", "300");
750
751 if (!got_stop) {
752 qtest_qmp_eventwait(from, "STOP");
753 }
754
755 qtest_qmp_eventwait(to, "RESUME");
756
757 wait_for_serial("dest_serial");
758 wait_for_migration_complete(from);
759
760 test_migrate_end(from, to, true);
761 g_free(uri);
762}
763
ea0c6d62
DDAG
764int main(int argc, char **argv)
765{
2656bfd9 766 char template[] = "/tmp/migration-test-XXXXXX";
ea0c6d62
DDAG
767 int ret;
768
769 g_test_init(&argc, &argv, NULL);
770
771 if (!ufd_version_check()) {
772 return 0;
773 }
774
775 tmpfs = mkdtemp(template);
776 if (!tmpfs) {
777 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
778 }
779 g_assert(tmpfs);
780
781 module_call_init(MODULE_INIT_QOM);
782
2884100c 783 qtest_add_func("/migration/postcopy/unix", test_postcopy);
d5f49640 784 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
4c27486d 785 qtest_add_func("/migration/deprecated", test_deprecated);
2c9bb297 786 qtest_add_func("/migration/bad_dest", test_baddest);
2884100c 787 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
ea0c6d62
DDAG
788
789 ret = g_test_run();
790
791 g_assert_cmpint(ret, ==, 0);
792
793 ret = rmdir(tmpfs);
794 if (ret != 0) {
795 g_test_message("unable to rmdir: path (%s): %s\n",
796 tmpfs, strerror(errno));
797 }
798
799 return ret;
800}