]> git.proxmox.com Git - mirror_qemu.git/blame - tests/migration-test.c
migration-test: Move -name handling to common commandline
[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"
b5bbd3f3 17#include "qapi/qmp/qjson.h"
0b8fa32f 18#include "qemu/module.h"
ea0c6d62
DDAG
19#include "qemu/option.h"
20#include "qemu/range.h"
a9c94277 21#include "qemu/sockets.h"
8228e353 22#include "chardev/char.h"
609d3844
JQ
23#include "qapi/qapi-visit-sockets.h"
24#include "qapi/qobject-input-visitor.h"
25#include "qapi/qobject-output-visitor.h"
ea0c6d62 26
e51e711b
WH
27#include "migration/migration-test.h"
28
055a1efc
MA
29/* TODO actually test the results and get rid of this */
30#define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
31
e51e711b
WH
32unsigned start_address;
33unsigned end_address;
ea0c6d62 34bool got_stop;
346f3dab 35static bool uffd_feature_thread_id;
ea0c6d62
DDAG
36
37#if defined(__linux__)
ea0c6d62
DDAG
38#include <sys/syscall.h>
39#include <sys/vfs.h>
40#endif
41
42#if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
43#include <sys/eventfd.h>
44#include <sys/ioctl.h>
45#include <linux/userfaultfd.h>
46
47static bool ufd_version_check(void)
48{
49 struct uffdio_api api_struct;
50 uint64_t ioctl_mask;
51
e1ae9fb6 52 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
ea0c6d62
DDAG
53
54 if (ufd == -1) {
55 g_test_message("Skipping test: userfaultfd not available");
56 return false;
57 }
58
59 api_struct.api = UFFD_API;
60 api_struct.features = 0;
61 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
62 g_test_message("Skipping test: UFFDIO_API failed");
63 return false;
64 }
346f3dab 65 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
ea0c6d62
DDAG
66
67 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
68 (__u64)1 << _UFFDIO_UNREGISTER;
69 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
70 g_test_message("Skipping test: Missing userfault feature");
71 return false;
72 }
73
74 return true;
75}
76
77#else
78static bool ufd_version_check(void)
79{
80 g_test_message("Skipping test: Userfault not available (builtdtime)");
81 return false;
82}
83
84#endif
85
86static const char *tmpfs;
87
e51e711b
WH
88/* The boot file modifies memory area in [start_address, end_address)
89 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
ea0c6d62 90 */
d54927ef 91#include "tests/migration/i386/a-b-bootblock.h"
c02b3781 92#include "tests/migration/aarch64/a-b-kernel.h"
5571dc82
TH
93#include "tests/migration/s390x/a-b-bios.h"
94
2785f196 95static void init_bootfile(const char *bootpath, void *content, size_t len)
5571dc82
TH
96{
97 FILE *bootfile = fopen(bootpath, "wb");
5571dc82 98
2785f196 99 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
5571dc82
TH
100 fclose(bootfile);
101}
102
ea0c6d62
DDAG
103/*
104 * Wait for some output in the serial output file,
105 * we get an 'A' followed by an endless string of 'B's
106 * but on the destination we won't have the A.
107 */
108static void wait_for_serial(const char *side)
109{
110 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
111 FILE *serialfile = fopen(serialpath, "r");
aaf89c8a 112 const char *arch = qtest_get_arch();
113 int started = (strcmp(side, "src_serial") == 0 &&
114 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62 115
e2dd21e5 116 g_free(serialpath);
ea0c6d62
DDAG
117 do {
118 int readvalue = fgetc(serialfile);
119
aaf89c8a 120 if (!started) {
121 /* SLOF prints its banner before starting test,
122 * to ignore it, mark the start of the test with '_',
123 * ignore all characters until this marker
124 */
125 switch (readvalue) {
126 case '_':
127 started = 1;
128 break;
129 case EOF:
130 fseek(serialfile, 0, SEEK_SET);
131 usleep(1000);
132 break;
133 }
134 continue;
135 }
ea0c6d62
DDAG
136 switch (readvalue) {
137 case 'A':
138 /* Fine */
139 break;
140
141 case 'B':
142 /* It's alive! */
143 fclose(serialfile);
ea0c6d62
DDAG
144 return;
145
146 case EOF:
aaf89c8a 147 started = (strcmp(side, "src_serial") == 0 &&
148 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62
DDAG
149 fseek(serialfile, 0, SEEK_SET);
150 usleep(1000);
151 break;
152
153 default:
154 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
155 g_assert_not_reached();
156 }
157 } while (true);
158}
159
3cd46d42
MA
160static void stop_cb(void *opaque, const char *name, QDict *data)
161{
162 if (!strcmp(name, "STOP")) {
163 got_stop = true;
164 }
165}
166
24d5588c
YK
167/*
168 * Events can get in the way of responses we are actually waiting for.
169 */
170GCC_FMT_ATTR(3, 4)
171static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
172{
173 va_list ap;
174
175 va_start(ap, command);
176 qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
177 va_end(ap);
178
179 return qtest_qmp_receive_success(who, stop_cb, NULL);
180}
181
ea0c6d62
DDAG
182/*
183 * Events can get in the way of responses we are actually waiting for.
184 */
b7281c69 185GCC_FMT_ATTR(2, 3)
4399596b 186static QDict *wait_command(QTestState *who, const char *command, ...)
ea0c6d62 187{
4399596b
MA
188 va_list ap;
189
190 va_start(ap, command);
191 qtest_qmp_vsend(who, command, ap);
192 va_end(ap);
193
3cd46d42 194 return qtest_qmp_receive_success(who, stop_cb, NULL);
ea0c6d62
DDAG
195}
196
2f7074c6
PX
197/*
198 * Note: caller is responsible to free the returned object via
199 * qobject_unref() after use
200 */
201static QDict *migrate_query(QTestState *who)
202{
e1454165 203 return wait_command(who, "{ 'execute': 'query-migrate' }");
2f7074c6
PX
204}
205
206/*
207 * Note: caller is responsible to free the returned object via
208 * g_free() after use
209 */
210static gchar *migrate_query_status(QTestState *who)
211{
212 QDict *rsp_return = migrate_query(who);
213 gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
214
215 g_assert(status);
216 qobject_unref(rsp_return);
217
218 return status;
219}
ea0c6d62
DDAG
220
221/*
222 * It's tricky to use qemu's migration event capability with qtest,
223 * events suddenly appearing confuse the qmp()/hmp() responses.
ea0c6d62
DDAG
224 */
225
660a9b68 226static int64_t read_ram_property_int(QTestState *who, const char *property)
ea0c6d62 227{
2f7074c6 228 QDict *rsp_return, *rsp_ram;
660a9b68 229 int64_t result;
ea0c6d62 230
2f7074c6 231 rsp_return = migrate_query(who);
ea0c6d62
DDAG
232 if (!qdict_haskey(rsp_return, "ram")) {
233 /* Still in setup */
234 result = 0;
235 } else {
236 rsp_ram = qdict_get_qdict(rsp_return, "ram");
660a9b68 237 result = qdict_get_try_int(rsp_ram, property, 0);
ea0c6d62 238 }
2f7074c6 239 qobject_unref(rsp_return);
ea0c6d62
DDAG
240 return result;
241}
242
8c51642b
YK
243static int64_t read_migrate_property_int(QTestState *who, const char *property)
244{
245 QDict *rsp_return;
246 int64_t result;
247
248 rsp_return = migrate_query(who);
249 result = qdict_get_try_int(rsp_return, property, 0);
250 qobject_unref(rsp_return);
251 return result;
252}
253
660a9b68
YK
254static uint64_t get_migration_pass(QTestState *who)
255{
256 return read_ram_property_int(who, "dirty-sync-count");
257}
258
346f3dab
AP
259static void read_blocktime(QTestState *who)
260{
2f7074c6 261 QDict *rsp_return;
346f3dab 262
2f7074c6 263 rsp_return = migrate_query(who);
346f3dab 264 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
2f7074c6 265 qobject_unref(rsp_return);
346f3dab
AP
266}
267
8c51642b
YK
268static bool check_migration_status(QTestState *who, const char *goal,
269 const char **ungoals)
270{
271 bool ready;
272 char *current_status;
273 const char **ungoal;
274
275 current_status = migrate_query_status(who);
276 ready = strcmp(current_status, goal) == 0;
277 if (!ungoals) {
278 g_assert_cmpstr(current_status, !=, "failed");
279 /*
280 * If looking for a state other than completed,
281 * completion of migration would cause the test to
282 * hang.
283 */
284 if (strcmp(goal, "completed") != 0) {
285 g_assert_cmpstr(current_status, !=, "completed");
286 }
287 } else {
288 for (ungoal = ungoals; *ungoal; ungoal++) {
289 g_assert_cmpstr(current_status, !=, *ungoal);
290 }
291 }
292 g_free(current_status);
293 return ready;
294}
295
2f6d3138 296static void wait_for_migration_status(QTestState *who,
e15310ea
DDAG
297 const char *goal,
298 const char **ungoals)
ea0c6d62 299{
8c51642b 300 while (!check_migration_status(who, goal, ungoals)) {
6a7724e9
JQ
301 usleep(1000);
302 }
ea0c6d62
DDAG
303}
304
2f6d3138
PX
305static void wait_for_migration_complete(QTestState *who)
306{
8c51642b 307 wait_for_migration_status(who, "completed", NULL);
2f6d3138
PX
308}
309
863e27a8 310static void wait_for_migration_pass(QTestState *who)
ea0c6d62 311{
863e27a8 312 uint64_t initial_pass = get_migration_pass(who);
ea0c6d62
DDAG
313 uint64_t pass;
314
315 /* Wait for the 1st sync */
6a7724e9
JQ
316 while (!got_stop && !initial_pass) {
317 usleep(1000);
863e27a8 318 initial_pass = get_migration_pass(who);
6a7724e9 319 }
ea0c6d62
DDAG
320
321 do {
6a7724e9 322 usleep(1000);
863e27a8 323 pass = get_migration_pass(who);
ea0c6d62
DDAG
324 } while (pass == initial_pass && !got_stop);
325}
326
7195a871 327static void check_guests_ram(QTestState *who)
ea0c6d62
DDAG
328{
329 /* Our ASM test will have been incrementing one byte from each page from
e51e711b
WH
330 * start_address to < end_address in order. This gives us a constraint
331 * that any page's byte should be equal or less than the previous pages
332 * byte (mod 256); and they should all be equal except for one transition
333 * at the point where we meet the incrementer. (We're running this with
334 * the guest stopped).
ea0c6d62
DDAG
335 */
336 unsigned address;
337 uint8_t first_byte;
338 uint8_t last_byte;
339 bool hit_edge = false;
601b5575 340 int bad = 0;
ea0c6d62 341
7195a871 342 qtest_memread(who, start_address, &first_byte, 1);
ea0c6d62
DDAG
343 last_byte = first_byte;
344
e51e711b
WH
345 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
346 address += TEST_MEM_PAGE_SIZE)
ea0c6d62
DDAG
347 {
348 uint8_t b;
7195a871 349 qtest_memread(who, address, &b, 1);
ea0c6d62
DDAG
350 if (b != last_byte) {
351 if (((b + 1) % 256) == last_byte && !hit_edge) {
352 /* This is OK, the guest stopped at the point of
353 * incrementing the previous page but didn't get
354 * to us yet.
355 */
356 hit_edge = true;
829db8b4 357 last_byte = b;
ea0c6d62 358 } else {
601b5575
AB
359 bad++;
360 if (bad <= 10) {
361 fprintf(stderr, "Memory content inconsistency at %x"
362 " first_byte = %x last_byte = %x current = %x"
363 " hit_edge = %x\n",
364 address, first_byte, last_byte, b, hit_edge);
365 }
ea0c6d62
DDAG
366 }
367 }
ea0c6d62 368 }
601b5575
AB
369 if (bad >= 10) {
370 fprintf(stderr, "and in another %d pages", bad - 10);
371 }
372 g_assert(bad == 0);
ea0c6d62
DDAG
373}
374
375static void cleanup(const char *filename)
376{
377 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
378
379 unlink(path);
e2dd21e5 380 g_free(path);
ea0c6d62
DDAG
381}
382
609d3844
JQ
383static char *SocketAddress_to_str(SocketAddress *addr)
384{
385 switch (addr->type) {
386 case SOCKET_ADDRESS_TYPE_INET:
387 return g_strdup_printf("tcp:%s:%s",
388 addr->u.inet.host,
389 addr->u.inet.port);
390 case SOCKET_ADDRESS_TYPE_UNIX:
391 return g_strdup_printf("unix:%s",
392 addr->u.q_unix.path);
393 case SOCKET_ADDRESS_TYPE_FD:
394 return g_strdup_printf("fd:%s", addr->u.fd.str);
395 case SOCKET_ADDRESS_TYPE_VSOCK:
396 return g_strdup_printf("tcp:%s:%s",
397 addr->u.vsock.cid,
398 addr->u.vsock.port);
399 default:
400 return g_strdup("unknown address type");
401 }
402}
403
404static char *migrate_get_socket_address(QTestState *who, const char *parameter)
405{
406 QDict *rsp;
407 char *result;
408 Error *local_err = NULL;
409 SocketAddressList *addrs;
410 Visitor *iv = NULL;
411 QObject *object;
412
413 rsp = migrate_query(who);
414 object = qdict_get(rsp, parameter);
415
416 iv = qobject_input_visitor_new(object);
417 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
1e25879e 418 visit_free(iv);
609d3844
JQ
419
420 /* we are only using a single address */
1e25879e 421 result = SocketAddress_to_str(addrs->value);
609d3844
JQ
422
423 qapi_free_SocketAddressList(addrs);
424 qobject_unref(rsp);
425 return result;
426}
427
8f7798f1
JQ
428static long long migrate_get_parameter_int(QTestState *who,
429 const char *parameter)
609d3844
JQ
430{
431 QDict *rsp;
432 long long result;
433
434 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
435 result = qdict_get_int(rsp, parameter);
436 qobject_unref(rsp);
437 return result;
438}
439
8f7798f1
JQ
440static void migrate_check_parameter_int(QTestState *who, const char *parameter,
441 long long value)
56b4a42a 442{
609d3844 443 long long result;
56b4a42a 444
8f7798f1 445 result = migrate_get_parameter_int(who, parameter);
609d3844 446 g_assert_cmpint(result, ==, value);
56b4a42a
JQ
447}
448
8f7798f1
JQ
449static void migrate_set_parameter_int(QTestState *who, const char *parameter,
450 long long value)
d62fbe60
JQ
451{
452 QDict *rsp;
d62fbe60 453
c44a56d8
MA
454 rsp = qtest_qmp(who,
455 "{ 'execute': 'migrate-set-parameters',"
456 "'arguments': { %s: %lld } }",
457 parameter, value);
d62fbe60 458 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 459 qobject_unref(rsp);
8f7798f1 460 migrate_check_parameter_int(who, parameter, value);
d62fbe60
JQ
461}
462
d5f49640
PX
463static void migrate_pause(QTestState *who)
464{
465 QDict *rsp;
466
467 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
d5f49640
PX
468 qobject_unref(rsp);
469}
470
8c51642b
YK
471static void migrate_continue(QTestState *who, const char *state)
472{
473 QDict *rsp;
474
475 rsp = wait_command(who,
476 "{ 'execute': 'migrate-continue',"
477 " 'arguments': { 'state': %s } }",
478 state);
479 qobject_unref(rsp);
480}
481
d5f49640
PX
482static void migrate_recover(QTestState *who, const char *uri)
483{
484 QDict *rsp;
d5f49640 485
b7281c69
MA
486 rsp = wait_command(who,
487 "{ 'execute': 'migrate-recover', "
488 " 'id': 'recover-cmd', "
489 " 'arguments': { 'uri': %s } }",
490 uri);
d5f49640
PX
491 qobject_unref(rsp);
492}
493
d62fbe60 494static void migrate_set_capability(QTestState *who, const char *capability,
c44a56d8 495 bool value)
d62fbe60
JQ
496{
497 QDict *rsp;
c44a56d8
MA
498
499 rsp = qtest_qmp(who,
500 "{ 'execute': 'migrate-set-capabilities',"
501 "'arguments': { "
502 "'capabilities': [ { "
503 "'capability': %s, 'state': %i } ] } }",
504 capability, value);
d62fbe60 505 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 506 qobject_unref(rsp);
d62fbe60
JQ
507}
508
b5bbd3f3
MA
509/*
510 * Send QMP command "migrate".
511 * Arguments are built from @fmt... (formatted like
512 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
513 */
514GCC_FMT_ATTR(3, 4)
515static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
d62fbe60 516{
b5bbd3f3
MA
517 va_list ap;
518 QDict *args, *rsp;
d62fbe60 519
b5bbd3f3
MA
520 va_start(ap, fmt);
521 args = qdict_from_vjsonf_nofail(fmt, ap);
522 va_end(ap);
523
524 g_assert(!qdict_haskey(args, "uri"));
525 qdict_put_str(args, "uri", uri);
526
a2726593 527 rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args);
24d5588c 528
d62fbe60 529 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 530 qobject_unref(rsp);
d62fbe60
JQ
531}
532
d131662a 533static void migrate_postcopy_start(QTestState *from, QTestState *to)
eb665d7d
JQ
534{
535 QDict *rsp;
536
d131662a 537 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
cb3e7f08 538 qobject_unref(rsp);
d131662a
PX
539
540 if (!got_stop) {
541 qtest_qmp_eventwait(from, "STOP");
542 }
543
544 qtest_qmp_eventwait(to, "RESUME");
eb665d7d
JQ
545}
546
5fd4a9c9 547static int test_migrate_start(QTestState **from, QTestState **to,
660a9b68 548 const char *uri, bool hide_stderr,
3af31a34
YK
549 bool use_shmem, const char *opts_src,
550 const char *opts_dst)
ea0c6d62 551{
d62fbe60 552 gchar *cmd_src, *cmd_dst;
8443415f 553 gchar *cmd_source, *cmd_target;
1b023718 554 const gchar *ignore_stderr;
660a9b68 555 char *bootpath = NULL;
3ed375e7
JQ
556 char *shmem_opts;
557 char *shmem_path;
aaf89c8a 558 const char *arch = qtest_get_arch();
e022c277
JQ
559 const char *machine_type;
560 const char *machine_args;
7b6d44cb 561 const char *memory_size;
ea0c6d62 562
3af31a34
YK
563 opts_src = opts_src ? opts_src : "";
564 opts_dst = opts_dst ? opts_dst : "";
565
660a9b68
YK
566 if (use_shmem) {
567 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
568 g_test_skip("/dev/shm is not supported");
569 return -1;
570 }
660a9b68 571 }
ea0c6d62 572
660a9b68
YK
573 got_stop = false;
574 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
aaf89c8a 575 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
2785f196
PM
576 /* the assembled x86 boot sector should be exactly one sector large */
577 assert(sizeof(x86_bootsect) == 512);
578 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
e022c277
JQ
579 machine_type = "";
580 machine_args = "";
7b6d44cb 581 memory_size = "150M";
d6b43267 582 cmd_src = g_strdup_printf(" -serial file:%s/src_serial"
3ed375e7
JQ
583 " -drive file=%s,format=raw",
584 tmpfs, bootpath);
d6b43267 585 cmd_dst = g_strdup_printf(" -serial file:%s/dest_serial"
aaf89c8a 586 " -drive file=%s,format=raw"
3ed375e7
JQ
587 " -incoming %s",
588 tmpfs, bootpath, uri);
e51e711b
WH
589 start_address = X86_TEST_MEM_START;
590 end_address = X86_TEST_MEM_END;
5571dc82 591 } else if (g_str_equal(arch, "s390x")) {
2785f196 592 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
e022c277
JQ
593 machine_type = "";
594 machine_args = "";
7b6d44cb 595 memory_size = "128M";
d6b43267 596 cmd_src = g_strdup_printf(" -serial file:%s/src_serial -bios %s",
3ed375e7 597 tmpfs, bootpath);
d6b43267 598 cmd_dst = g_strdup_printf(" -serial file:%s/dest_serial -bios %s"
3ed375e7
JQ
599 " -incoming %s",
600 tmpfs, bootpath, uri);
5571dc82
TH
601 start_address = S390_TEST_MEM_START;
602 end_address = S390_TEST_MEM_END;
aaf89c8a 603 } else if (strcmp(arch, "ppc64") == 0) {
e022c277
JQ
604 machine_type = "";
605 machine_args = ",vsmt=8";
7b6d44cb 606 memory_size = "256M";
7b6d44cb 607 cmd_src = g_strdup_printf("-nodefaults"
aaf89c8a 608 " -serial file:%s/src_serial"
fc71e3e5
TH
609 " -prom-env 'use-nvramrc?=true' -prom-env "
610 "'nvramrc=hex .\" _\" begin %x %x "
cdf33815 611 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
3ed375e7
JQ
612 "until'", tmpfs, end_address,
613 start_address);
d6b43267 614 cmd_dst = g_strdup_printf(" -serial file:%s/dest_serial"
3ed375e7
JQ
615 " -incoming %s",
616 tmpfs, uri);
e51e711b
WH
617
618 start_address = PPC_TEST_MEM_START;
619 end_address = PPC_TEST_MEM_END;
c02b3781 620 } else if (strcmp(arch, "aarch64") == 0) {
2785f196 621 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
e022c277
JQ
622 machine_type = "virt,";
623 machine_args = "gic-version=max";
7b6d44cb 624 memory_size = "150M";
d6b43267 625 cmd_src = g_strdup_printf("-cpu max "
7b6d44cb 626 "-serial file:%s/src_serial "
3ed375e7
JQ
627 "-kernel %s",
628 tmpfs, bootpath);
d6b43267 629 cmd_dst = g_strdup_printf("-cpu max "
7b6d44cb 630 "-serial file:%s/dest_serial "
c02b3781 631 "-kernel %s "
3ed375e7
JQ
632 "-incoming %s",
633 tmpfs, bootpath, uri);
c02b3781
WH
634
635 start_address = ARM_TEST_MEM_START;
636 end_address = ARM_TEST_MEM_END;
637
638 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
aaf89c8a 639 } else {
640 g_assert_not_reached();
641 }
642
e2dd21e5
MAL
643 g_free(bootpath);
644
f96d6651 645 if (hide_stderr) {
1b023718
JQ
646 ignore_stderr = "2>/dev/null";
647 } else {
648 ignore_stderr = "";
f96d6651
DDAG
649 }
650
3ed375e7
JQ
651 if (use_shmem) {
652 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
653 shmem_opts = g_strdup_printf(
654 "-object memory-backend-file,id=mem0,size=%s"
655 ",mem-path=%s,share=on -numa node,memdev=mem0",
656 memory_size, shmem_path);
657 } else {
658 shmem_path = NULL;
659 shmem_opts = g_strdup("");
660 }
661
7b6d44cb 662 cmd_source = g_strdup_printf("-machine %saccel=kvm:tcg%s "
d6b43267 663 "-name source,debug-threads=on "
7b6d44cb 664 "-m %s "
3ed375e7 665 "%s %s %s %s",
e022c277 666 machine_type, machine_args,
7b6d44cb 667 memory_size,
3ed375e7 668 cmd_src, shmem_opts, opts_src, ignore_stderr);
aaf89c8a 669 g_free(cmd_src);
8443415f
JQ
670 *from = qtest_init(cmd_source);
671 g_free(cmd_source);
aaf89c8a 672
7b6d44cb 673 cmd_target = g_strdup_printf("-machine %saccel=kvm:tcg%s "
d6b43267 674 "-name target,debug-threads=on "
7b6d44cb 675 "-m %s "
3ed375e7 676 "%s %s %s %s",
e022c277 677 machine_type, machine_args,
7b6d44cb 678 memory_size,
3ed375e7 679 cmd_dst, shmem_opts, opts_dst, ignore_stderr);
aaf89c8a 680 g_free(cmd_dst);
8443415f
JQ
681 *to = qtest_init(cmd_target);
682 g_free(cmd_target);
660a9b68 683
3ed375e7 684 g_free(shmem_opts);
660a9b68
YK
685 /*
686 * Remove shmem file immediately to avoid memory leak in test failed case.
687 * It's valid becase QEMU has already opened this file
688 */
689 if (use_shmem) {
690 unlink(shmem_path);
691 g_free(shmem_path);
692 }
693
5fd4a9c9 694 return 0;
7195a871
JQ
695}
696
2c9bb297 697static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
7195a871
JQ
698{
699 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
700
701 qtest_quit(from);
702
2c9bb297
DDAG
703 if (test_dest) {
704 qtest_memread(to, start_address, &dest_byte_a, 1);
7195a871 705
2c9bb297
DDAG
706 /* Destination still running, wait for a byte to change */
707 do {
708 qtest_memread(to, start_address, &dest_byte_b, 1);
709 usleep(1000 * 10);
710 } while (dest_byte_a == dest_byte_b);
711
712 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
7195a871 713
2c9bb297
DDAG
714 /* With it stopped, check nothing changes */
715 qtest_memread(to, start_address, &dest_byte_c, 1);
716 usleep(1000 * 200);
717 qtest_memread(to, start_address, &dest_byte_d, 1);
718 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
7195a871 719
2c9bb297
DDAG
720 check_guests_ram(to);
721 }
7195a871
JQ
722
723 qtest_quit(to);
724
725 cleanup("bootsect");
726 cleanup("migsocket");
727 cleanup("src_serial");
728 cleanup("dest_serial");
729}
730
4c27486d
JQ
731static void deprecated_set_downtime(QTestState *who, const double value)
732{
733 QDict *rsp;
4c27486d 734
015715f5
MA
735 rsp = qtest_qmp(who,
736 "{ 'execute': 'migrate_set_downtime',"
737 " 'arguments': { 'value': %f } }", value);
4c27486d 738 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 739 qobject_unref(rsp);
8f7798f1 740 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
4c27486d
JQ
741}
742
c44a56d8 743static void deprecated_set_speed(QTestState *who, long long value)
4c27486d
JQ
744{
745 QDict *rsp;
4c27486d 746
c44a56d8
MA
747 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
748 "'arguments': { 'value': %lld } }", value);
4c27486d 749 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 750 qobject_unref(rsp);
8f7798f1 751 migrate_check_parameter_int(who, "max-bandwidth", value);
4c27486d
JQ
752}
753
cdf84229
JQ
754static void deprecated_set_cache_size(QTestState *who, long long value)
755{
756 QDict *rsp;
757
758 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
759 "'arguments': { 'value': %lld } }", value);
760 g_assert(qdict_haskey(rsp, "return"));
761 qobject_unref(rsp);
8f7798f1 762 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
cdf84229
JQ
763}
764
4c27486d
JQ
765static void test_deprecated(void)
766{
767 QTestState *from;
768
a2726593 769 from = qtest_init("-machine none");
4c27486d
JQ
770
771 deprecated_set_downtime(from, 0.12345);
c44a56d8 772 deprecated_set_speed(from, 12345);
cdf84229 773 deprecated_set_cache_size(from, 4096);
4c27486d
JQ
774
775 qtest_quit(from);
776}
777
d131662a 778static int migrate_postcopy_prepare(QTestState **from_ptr,
3e81f73c
PX
779 QTestState **to_ptr,
780 bool hide_error)
7195a871
JQ
781{
782 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
863e27a8 783 QTestState *from, *to;
7195a871 784
3af31a34 785 if (test_migrate_start(&from, &to, uri, hide_error, false, NULL, NULL)) {
d131662a 786 return -1;
5fd4a9c9 787 }
ea0c6d62 788
c44a56d8
MA
789 migrate_set_capability(from, "postcopy-ram", true);
790 migrate_set_capability(to, "postcopy-ram", true);
791 migrate_set_capability(to, "postcopy-blocktime", true);
ea0c6d62
DDAG
792
793 /* We want to pick a speed slow enough that the test completes
794 * quickly, but that it doesn't complete precopy even on a slow
795 * machine, so also set the downtime.
796 */
513aa2c6 797 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
8f7798f1 798 migrate_set_parameter_int(from, "downtime-limit", 1);
ea0c6d62
DDAG
799
800 /* Wait for the first serial output from the source */
801 wait_for_serial("src_serial");
802
b5bbd3f3 803 migrate(from, uri, "{}");
d131662a 804 g_free(uri);
ea0c6d62 805
863e27a8 806 wait_for_migration_pass(from);
ea0c6d62 807
d131662a
PX
808 *from_ptr = from;
809 *to_ptr = to;
ea0c6d62 810
d131662a
PX
811 return 0;
812}
ea0c6d62 813
d131662a
PX
814static void migrate_postcopy_complete(QTestState *from, QTestState *to)
815{
816 wait_for_migration_complete(from);
ea0c6d62 817
d131662a 818 /* Make sure we get at least one "B" on destination */
ea0c6d62 819 wait_for_serial("dest_serial");
ea0c6d62 820
346f3dab
AP
821 if (uffd_feature_thread_id) {
822 read_blocktime(to);
823 }
ea0c6d62 824
2c9bb297
DDAG
825 test_migrate_end(from, to, true);
826}
827
d131662a
PX
828static void test_postcopy(void)
829{
830 QTestState *from, *to;
831
3e81f73c 832 if (migrate_postcopy_prepare(&from, &to, false)) {
d131662a
PX
833 return;
834 }
835 migrate_postcopy_start(from, to);
836 migrate_postcopy_complete(from, to);
837}
838
d5f49640
PX
839static void test_postcopy_recovery(void)
840{
841 QTestState *from, *to;
842 char *uri;
843
3e81f73c 844 if (migrate_postcopy_prepare(&from, &to, true)) {
d5f49640
PX
845 return;
846 }
847
848 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
8f7798f1 849 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
d5f49640
PX
850
851 /* Now we start the postcopy */
852 migrate_postcopy_start(from, to);
853
854 /*
855 * Wait until postcopy is really started; we can only run the
856 * migrate-pause command during a postcopy
857 */
8c51642b 858 wait_for_migration_status(from, "postcopy-active", NULL);
d5f49640
PX
859
860 /*
861 * Manually stop the postcopy migration. This emulates a network
862 * failure with the migration socket
863 */
864 migrate_pause(from);
865
866 /*
867 * Wait for destination side to reach postcopy-paused state. The
868 * migrate-recover command can only succeed if destination machine
869 * is in the paused state
870 */
e15310ea
DDAG
871 wait_for_migration_status(to, "postcopy-paused",
872 (const char * []) { "failed", "active",
873 "completed", NULL });
d5f49640
PX
874
875 /*
876 * Create a new socket to emulate a new channel that is different
877 * from the broken migration channel; tell the destination to
878 * listen to the new port
879 */
880 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
881 migrate_recover(to, uri);
882
883 /*
884 * Try to rebuild the migration channel using the resume flag and
885 * the newly created channel
886 */
e15310ea
DDAG
887 wait_for_migration_status(from, "postcopy-paused",
888 (const char * []) { "failed", "active",
889 "completed", NULL });
b5bbd3f3 890 migrate(from, uri, "{'resume': true}");
d5f49640
PX
891 g_free(uri);
892
893 /* Restore the postcopy bandwidth to unlimited */
8f7798f1 894 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
d5f49640
PX
895
896 migrate_postcopy_complete(from, to);
897}
898
3af31a34 899static void wait_for_migration_fail(QTestState *from, bool allow_active)
2c9bb297 900{
e1454165 901 QDict *rsp_return;
2f7074c6 902 char *status;
2c9bb297
DDAG
903 bool failed;
904
2c9bb297 905 do {
2f7074c6 906 status = migrate_query_status(from);
84b2c7e5
DDAG
907 bool result = !strcmp(status, "setup") || !strcmp(status, "failed") ||
908 (allow_active && !strcmp(status, "active"));
909 if (!result) {
910 fprintf(stderr, "%s: unexpected status status=%s allow_active=%d\n",
911 __func__, status, allow_active);
912 }
913 g_assert(result);
2c9bb297 914 failed = !strcmp(status, "failed");
2f7074c6 915 g_free(status);
2c9bb297
DDAG
916 } while (!failed);
917
918 /* Is the machine currently running? */
e1454165 919 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
2c9bb297
DDAG
920 g_assert(qdict_haskey(rsp_return, "running"));
921 g_assert(qdict_get_bool(rsp_return, "running"));
e1454165 922 qobject_unref(rsp_return);
3af31a34
YK
923}
924
925static void test_baddest(void)
926{
927 QTestState *from, *to;
2c9bb297 928
3af31a34
YK
929 if (test_migrate_start(&from, &to, "tcp:0:0", true, false, NULL, NULL)) {
930 return;
931 }
932 migrate(from, "tcp:0:0", "{}");
933 wait_for_migration_fail(from, false);
2c9bb297 934 test_migrate_end(from, to, false);
ea0c6d62
DDAG
935}
936
2884100c
JQ
937static void test_precopy_unix(void)
938{
939 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
940 QTestState *from, *to;
941
3af31a34 942 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
5fd4a9c9
DDAG
943 return;
944 }
2884100c
JQ
945
946 /* We want to pick a speed slow enough that the test completes
947 * quickly, but that it doesn't complete precopy even on a slow
948 * machine, so also set the downtime.
949 */
950 /* 1 ms should make it not converge*/
8f7798f1 951 migrate_set_parameter_int(from, "downtime-limit", 1);
2884100c 952 /* 1GB/s */
8f7798f1 953 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
2884100c
JQ
954
955 /* Wait for the first serial output from the source */
956 wait_for_serial("src_serial");
957
b5bbd3f3 958 migrate(from, uri, "{}");
2884100c
JQ
959
960 wait_for_migration_pass(from);
961
962 /* 300 ms should converge */
8f7798f1 963 migrate_set_parameter_int(from, "downtime-limit", 300);
2884100c
JQ
964
965 if (!got_stop) {
966 qtest_qmp_eventwait(from, "STOP");
967 }
968
969 qtest_qmp_eventwait(to, "RESUME");
970
971 wait_for_serial("dest_serial");
972 wait_for_migration_complete(from);
973
974 test_migrate_end(from, to, true);
975 g_free(uri);
976}
977
660a9b68
YK
978#if 0
979/* Currently upset on aarch64 TCG */
980static void test_ignore_shared(void)
981{
982 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
983 QTestState *from, *to;
984
3af31a34 985 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
660a9b68
YK
986 return;
987 }
988
989 migrate_set_capability(from, "x-ignore-shared", true);
990 migrate_set_capability(to, "x-ignore-shared", true);
991
992 /* Wait for the first serial output from the source */
993 wait_for_serial("src_serial");
994
995 migrate(from, uri, "{}");
996
997 wait_for_migration_pass(from);
998
999 if (!got_stop) {
1000 qtest_qmp_eventwait(from, "STOP");
1001 }
1002
1003 qtest_qmp_eventwait(to, "RESUME");
1004
1005 wait_for_serial("dest_serial");
1006 wait_for_migration_complete(from);
1007
1008 /* Check whether shared RAM has been really skipped */
1009 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1010
1011 test_migrate_end(from, to, true);
1012 g_free(uri);
1013}
1014#endif
1015
cdf84229
JQ
1016static void test_xbzrle(const char *uri)
1017{
1018 QTestState *from, *to;
1019
3af31a34 1020 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
cdf84229
JQ
1021 return;
1022 }
1023
1024 /*
1025 * We want to pick a speed slow enough that the test completes
1026 * quickly, but that it doesn't complete precopy even on a slow
1027 * machine, so also set the downtime.
1028 */
1029 /* 1 ms should make it not converge*/
8f7798f1 1030 migrate_set_parameter_int(from, "downtime-limit", 1);
cdf84229 1031 /* 1GB/s */
8f7798f1 1032 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
cdf84229 1033
8f7798f1 1034 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
cdf84229
JQ
1035
1036 migrate_set_capability(from, "xbzrle", "true");
1037 migrate_set_capability(to, "xbzrle", "true");
1038 /* Wait for the first serial output from the source */
1039 wait_for_serial("src_serial");
1040
1041 migrate(from, uri, "{}");
1042
1043 wait_for_migration_pass(from);
1044
1045 /* 300ms should converge */
8f7798f1 1046 migrate_set_parameter_int(from, "downtime-limit", 300);
cdf84229
JQ
1047
1048 if (!got_stop) {
1049 qtest_qmp_eventwait(from, "STOP");
1050 }
1051 qtest_qmp_eventwait(to, "RESUME");
1052
1053 wait_for_serial("dest_serial");
1054 wait_for_migration_complete(from);
1055
1056 test_migrate_end(from, to, true);
1057}
1058
1059static void test_xbzrle_unix(void)
1060{
1061 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1062
1063 test_xbzrle(uri);
1064 g_free(uri);
1065}
1066
609d3844
JQ
1067static void test_precopy_tcp(void)
1068{
1069 char *uri;
1070 QTestState *from, *to;
1071
3af31a34
YK
1072 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false,
1073 NULL, NULL)) {
609d3844
JQ
1074 return;
1075 }
1076
1077 /*
1078 * We want to pick a speed slow enough that the test completes
1079 * quickly, but that it doesn't complete precopy even on a slow
1080 * machine, so also set the downtime.
1081 */
1082 /* 1 ms should make it not converge*/
8f7798f1 1083 migrate_set_parameter_int(from, "downtime-limit", 1);
609d3844 1084 /* 1GB/s */
8f7798f1 1085 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
609d3844
JQ
1086
1087 /* Wait for the first serial output from the source */
1088 wait_for_serial("src_serial");
1089
1090 uri = migrate_get_socket_address(to, "socket-address");
1091
1092 migrate(from, uri, "{}");
1093
1094 wait_for_migration_pass(from);
1095
1096 /* 300ms should converge */
8f7798f1 1097 migrate_set_parameter_int(from, "downtime-limit", 300);
609d3844
JQ
1098
1099 if (!got_stop) {
1100 qtest_qmp_eventwait(from, "STOP");
1101 }
1102 qtest_qmp_eventwait(to, "RESUME");
1103
1104 wait_for_serial("dest_serial");
1105 wait_for_migration_complete(from);
1106
1107 test_migrate_end(from, to, true);
1108 g_free(uri);
1109}
1110
24d5588c
YK
1111static void test_migrate_fd_proto(void)
1112{
1113 QTestState *from, *to;
1114 int ret;
1115 int pair[2];
1116 QDict *rsp;
1117 const char *error_desc;
1118
3af31a34 1119 if (test_migrate_start(&from, &to, "defer", false, false, NULL, NULL)) {
24d5588c
YK
1120 return;
1121 }
1122
1123 /*
1124 * We want to pick a speed slow enough that the test completes
1125 * quickly, but that it doesn't complete precopy even on a slow
1126 * machine, so also set the downtime.
1127 */
1128 /* 1 ms should make it not converge */
8f7798f1 1129 migrate_set_parameter_int(from, "downtime-limit", 1);
24d5588c 1130 /* 1GB/s */
8f7798f1 1131 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
24d5588c
YK
1132
1133 /* Wait for the first serial output from the source */
1134 wait_for_serial("src_serial");
1135
1136 /* Create two connected sockets for migration */
1137 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1138 g_assert_cmpint(ret, ==, 0);
1139
1140 /* Send the 1st socket to the target */
1141 rsp = wait_command_fd(to, pair[0],
1142 "{ 'execute': 'getfd',"
1143 " 'arguments': { 'fdname': 'fd-mig' }}");
1144 qobject_unref(rsp);
1145 close(pair[0]);
1146
1147 /* Start incoming migration from the 1st socket */
1148 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1149 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1150 qobject_unref(rsp);
1151
1152 /* Send the 2nd socket to the target */
1153 rsp = wait_command_fd(from, pair[1],
1154 "{ 'execute': 'getfd',"
1155 " 'arguments': { 'fdname': 'fd-mig' }}");
1156 qobject_unref(rsp);
1157 close(pair[1]);
1158
1159 /* Start migration to the 2nd socket*/
1160 migrate(from, "fd:fd-mig", "{}");
1161
1162 wait_for_migration_pass(from);
1163
1164 /* 300ms should converge */
8f7798f1 1165 migrate_set_parameter_int(from, "downtime-limit", 300);
24d5588c
YK
1166
1167 if (!got_stop) {
1168 qtest_qmp_eventwait(from, "STOP");
1169 }
1170 qtest_qmp_eventwait(to, "RESUME");
1171
1172 /* Test closing fds */
1173 /* We assume, that QEMU removes named fd from its list,
1174 * so this should fail */
1175 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1176 " 'arguments': { 'fdname': 'fd-mig' }}");
1177 g_assert_true(qdict_haskey(rsp, "error"));
1178 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1179 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1180 qobject_unref(rsp);
1181
1182 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1183 " 'arguments': { 'fdname': 'fd-mig' }}");
1184 g_assert_true(qdict_haskey(rsp, "error"));
1185 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1186 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1187 qobject_unref(rsp);
1188
1189 /* Complete migration */
1190 wait_for_serial("dest_serial");
1191 wait_for_migration_complete(from);
1192 test_migrate_end(from, to, true);
1193}
1194
3af31a34
YK
1195static void do_test_validate_uuid(const char *uuid_arg_src,
1196 const char *uuid_arg_dst,
1197 bool should_fail, bool hide_stderr)
1198{
1199 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1200 QTestState *from, *to;
1201
1202 if (test_migrate_start(&from, &to, uri, hide_stderr, false,
1203 uuid_arg_src, uuid_arg_dst)) {
1204 return;
1205 }
1206
1207 /*
1208 * UUID validation is at the begin of migration. So, the main process of
1209 * migration is not interesting for us here. Thus, set huge downtime for
1210 * very fast migration.
1211 */
1212 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1213 migrate_set_capability(from, "validate-uuid", true);
1214
1215 /* Wait for the first serial output from the source */
1216 wait_for_serial("src_serial");
1217
1218 migrate(from, uri, "{}");
1219
1220 if (should_fail) {
1221 qtest_set_expected_status(to, 1);
1222 wait_for_migration_fail(from, true);
1223 } else {
1224 wait_for_migration_complete(from);
1225 }
1226
1227 test_migrate_end(from, to, false);
1228 g_free(uri);
1229}
1230
1231static void test_validate_uuid(void)
1232{
1233 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1234 "-uuid 11111111-1111-1111-1111-111111111111",
1235 false, false);
1236}
1237
1238static void test_validate_uuid_error(void)
1239{
1240 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1241 "-uuid 22222222-2222-2222-2222-222222222222",
1242 true, true);
1243}
1244
1245static void test_validate_uuid_src_not_set(void)
1246{
1247 do_test_validate_uuid(NULL, "-uuid 11111111-1111-1111-1111-111111111111",
1248 false, true);
1249}
1250
1251static void test_validate_uuid_dst_not_set(void)
1252{
1253 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", NULL,
1254 false, true);
1255}
1256
8c51642b
YK
1257static void test_migrate_auto_converge(void)
1258{
1259 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1260 QTestState *from, *to;
1261 int64_t remaining, percentage;
1262
1263 /*
1264 * We want the test to be stable and as fast as possible.
1265 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1266 * so we need to decrease a bandwidth.
1267 */
1268 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1269 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1270 const int64_t downtime_limit = 250; /* 250ms */
1271 /*
1272 * We migrate through unix-socket (> 500Mb/s).
1273 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1274 * So, we can predict expected_threshold
1275 */
1276 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1277
1278 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
1279 return;
1280 }
1281
1282 migrate_set_capability(from, "auto-converge", true);
1283 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1284 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1285 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1286
1287 /*
1288 * Set the initial parameters so that the migration could not converge
1289 * without throttling.
1290 */
1291 migrate_set_parameter_int(from, "downtime-limit", 1);
1292 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1293
1294 /* To check remaining size after precopy */
1295 migrate_set_capability(from, "pause-before-switchover", true);
1296
1297 /* Wait for the first serial output from the source */
1298 wait_for_serial("src_serial");
1299
1300 migrate(from, uri, "{}");
1301
1302 /* Wait for throttling begins */
1303 percentage = 0;
1304 while (percentage == 0) {
1305 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1306 usleep(100);
1307 g_assert_false(got_stop);
1308 }
1309 /* The first percentage of throttling should be equal to init_pct */
1310 g_assert_cmpint(percentage, ==, init_pct);
1311 /* Now, when we tested that throttling works, let it converge */
1312 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1313 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1314
1315 /*
1316 * Wait for pre-switchover status to check last throttle percentage
1317 * and remaining. These values will be zeroed later
1318 */
1319 wait_for_migration_status(from, "pre-switchover", NULL);
1320
1321 /* The final percentage of throttling shouldn't be greater than max_pct */
1322 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1323 g_assert_cmpint(percentage, <=, max_pct);
1324
1325 remaining = read_ram_property_int(from, "remaining");
1326 g_assert_cmpint(remaining, <, expected_threshold);
1327
1328 migrate_continue(from, "pre-switchover");
1329
1330 qtest_qmp_eventwait(to, "RESUME");
1331
1332 wait_for_serial("dest_serial");
1333 wait_for_migration_complete(from);
1334
1335 g_free(uri);
1336
1337 test_migrate_end(from, to, true);
1338}
1339
ea0c6d62
DDAG
1340int main(int argc, char **argv)
1341{
2656bfd9 1342 char template[] = "/tmp/migration-test-XXXXXX";
ea0c6d62
DDAG
1343 int ret;
1344
1345 g_test_init(&argc, &argv, NULL);
1346
1347 if (!ufd_version_check()) {
4848cb3d 1348 return g_test_run();
ea0c6d62
DDAG
1349 }
1350
880b169a
TH
1351 /*
1352 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1353 * is touchy due to race conditions on dirty bits (especially on PPC for
1354 * some reason)
1355 */
1356 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1ee5e144
LV
1357 (access("/sys/module/kvm_hv", F_OK) ||
1358 access("/dev/kvm", R_OK | W_OK))) {
880b169a 1359 g_test_message("Skipping test: kvm_hv not available");
4848cb3d 1360 return g_test_run();
880b169a
TH
1361 }
1362
d254b392
TH
1363 /*
1364 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1365 * there until the problems are resolved
1366 */
1367 if (g_str_equal(qtest_get_arch(), "s390x")) {
1368#if defined(HOST_S390X)
1369 if (access("/dev/kvm", R_OK | W_OK)) {
1370 g_test_message("Skipping test: kvm not available");
4848cb3d 1371 return g_test_run();
d254b392
TH
1372 }
1373#else
1374 g_test_message("Skipping test: Need s390x host to work properly");
4848cb3d 1375 return g_test_run();
d254b392
TH
1376#endif
1377 }
1378
ea0c6d62
DDAG
1379 tmpfs = mkdtemp(template);
1380 if (!tmpfs) {
13ee9e30 1381 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
ea0c6d62
DDAG
1382 }
1383 g_assert(tmpfs);
1384
1385 module_call_init(MODULE_INIT_QOM);
1386
2884100c 1387 qtest_add_func("/migration/postcopy/unix", test_postcopy);
d5f49640 1388 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
4c27486d 1389 qtest_add_func("/migration/deprecated", test_deprecated);
2c9bb297 1390 qtest_add_func("/migration/bad_dest", test_baddest);
2884100c 1391 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
609d3844 1392 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
660a9b68 1393 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
cdf84229 1394 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
24d5588c 1395 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
3af31a34
YK
1396 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1397 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1398 qtest_add_func("/migration/validate_uuid_src_not_set",
1399 test_validate_uuid_src_not_set);
1400 qtest_add_func("/migration/validate_uuid_dst_not_set",
1401 test_validate_uuid_dst_not_set);
ea0c6d62 1402
8c51642b
YK
1403 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1404
ea0c6d62
DDAG
1405 ret = g_test_run();
1406
1407 g_assert_cmpint(ret, ==, 0);
1408
1409 ret = rmdir(tmpfs);
1410 if (ret != 0) {
13ee9e30 1411 g_test_message("unable to rmdir: path (%s): %s",
ea0c6d62
DDAG
1412 tmpfs, strerror(errno));
1413 }
1414
1415 return ret;
1416}