]> git.proxmox.com Git - mirror_qemu.git/blame - tests/migration-test.c
migration-test: Rename cmd_src/dst to arch_source/arch_target
[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{
68d95609 552 gchar *arch_source, *arch_target;
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";
68d95609
JQ
582 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
583 arch_target = g_strdup(arch_source);
e51e711b
WH
584 start_address = X86_TEST_MEM_START;
585 end_address = X86_TEST_MEM_END;
5571dc82 586 } else if (g_str_equal(arch, "s390x")) {
2785f196 587 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
e022c277
JQ
588 machine_type = "";
589 machine_args = "";
7b6d44cb 590 memory_size = "128M";
68d95609
JQ
591 arch_source = g_strdup_printf("-bios %s", bootpath);
592 arch_target = g_strdup(arch_source);
5571dc82
TH
593 start_address = S390_TEST_MEM_START;
594 end_address = S390_TEST_MEM_END;
aaf89c8a 595 } else if (strcmp(arch, "ppc64") == 0) {
e022c277
JQ
596 machine_type = "";
597 machine_args = ",vsmt=8";
7b6d44cb 598 memory_size = "256M";
68d95609
JQ
599 arch_source = g_strdup_printf("-nodefaults "
600 "-prom-env 'use-nvramrc?=true' -prom-env "
601 "'nvramrc=hex .\" _\" begin %x %x "
602 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
603 "until'", end_address, start_address);
604 arch_target = g_strdup("");
e51e711b
WH
605 start_address = PPC_TEST_MEM_START;
606 end_address = PPC_TEST_MEM_END;
c02b3781 607 } else if (strcmp(arch, "aarch64") == 0) {
2785f196 608 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
e022c277
JQ
609 machine_type = "virt,";
610 machine_args = "gic-version=max";
7b6d44cb 611 memory_size = "150M";
68d95609
JQ
612 arch_source = g_strdup_printf("-cpu max "
613 "-kernel %s",
614 bootpath);
615 arch_target = g_strdup(arch_source);
c02b3781
WH
616 start_address = ARM_TEST_MEM_START;
617 end_address = ARM_TEST_MEM_END;
618
619 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
aaf89c8a 620 } else {
621 g_assert_not_reached();
622 }
623
e2dd21e5
MAL
624 g_free(bootpath);
625
f96d6651 626 if (hide_stderr) {
1b023718
JQ
627 ignore_stderr = "2>/dev/null";
628 } else {
629 ignore_stderr = "";
f96d6651
DDAG
630 }
631
3ed375e7
JQ
632 if (use_shmem) {
633 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
634 shmem_opts = g_strdup_printf(
635 "-object memory-backend-file,id=mem0,size=%s"
636 ",mem-path=%s,share=on -numa node,memdev=mem0",
637 memory_size, shmem_path);
638 } else {
639 shmem_path = NULL;
640 shmem_opts = g_strdup("");
641 }
642
7b6d44cb 643 cmd_source = g_strdup_printf("-machine %saccel=kvm:tcg%s "
d6b43267 644 "-name source,debug-threads=on "
7b6d44cb 645 "-m %s "
cd496731 646 "-serial file:%s/src_serial "
3ed375e7 647 "%s %s %s %s",
e022c277 648 machine_type, machine_args,
cd496731 649 memory_size, tmpfs,
68d95609
JQ
650 arch_source, shmem_opts, opts_src,
651 ignore_stderr);
652 g_free(arch_source);
8443415f
JQ
653 *from = qtest_init(cmd_source);
654 g_free(cmd_source);
aaf89c8a 655
7b6d44cb 656 cmd_target = g_strdup_printf("-machine %saccel=kvm:tcg%s "
d6b43267 657 "-name target,debug-threads=on "
7b6d44cb 658 "-m %s "
c5f40ff9 659 "-serial file:%s/dest_serial "
cd496731 660 "-incoming %s "
3ed375e7 661 "%s %s %s %s",
e022c277 662 machine_type, machine_args,
cd496731 663 memory_size, tmpfs, uri,
68d95609
JQ
664 arch_target, shmem_opts, opts_dst,
665 ignore_stderr);
666 g_free(arch_target);
8443415f
JQ
667 *to = qtest_init(cmd_target);
668 g_free(cmd_target);
660a9b68 669
3ed375e7 670 g_free(shmem_opts);
660a9b68
YK
671 /*
672 * Remove shmem file immediately to avoid memory leak in test failed case.
673 * It's valid becase QEMU has already opened this file
674 */
675 if (use_shmem) {
676 unlink(shmem_path);
677 g_free(shmem_path);
678 }
679
5fd4a9c9 680 return 0;
7195a871
JQ
681}
682
2c9bb297 683static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
7195a871
JQ
684{
685 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
686
687 qtest_quit(from);
688
2c9bb297
DDAG
689 if (test_dest) {
690 qtest_memread(to, start_address, &dest_byte_a, 1);
7195a871 691
2c9bb297
DDAG
692 /* Destination still running, wait for a byte to change */
693 do {
694 qtest_memread(to, start_address, &dest_byte_b, 1);
695 usleep(1000 * 10);
696 } while (dest_byte_a == dest_byte_b);
697
698 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
7195a871 699
2c9bb297
DDAG
700 /* With it stopped, check nothing changes */
701 qtest_memread(to, start_address, &dest_byte_c, 1);
702 usleep(1000 * 200);
703 qtest_memread(to, start_address, &dest_byte_d, 1);
704 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
7195a871 705
2c9bb297
DDAG
706 check_guests_ram(to);
707 }
7195a871
JQ
708
709 qtest_quit(to);
710
711 cleanup("bootsect");
712 cleanup("migsocket");
713 cleanup("src_serial");
714 cleanup("dest_serial");
715}
716
4c27486d
JQ
717static void deprecated_set_downtime(QTestState *who, const double value)
718{
719 QDict *rsp;
4c27486d 720
015715f5
MA
721 rsp = qtest_qmp(who,
722 "{ 'execute': 'migrate_set_downtime',"
723 " 'arguments': { 'value': %f } }", value);
4c27486d 724 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 725 qobject_unref(rsp);
8f7798f1 726 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
4c27486d
JQ
727}
728
c44a56d8 729static void deprecated_set_speed(QTestState *who, long long value)
4c27486d
JQ
730{
731 QDict *rsp;
4c27486d 732
c44a56d8
MA
733 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
734 "'arguments': { 'value': %lld } }", value);
4c27486d 735 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 736 qobject_unref(rsp);
8f7798f1 737 migrate_check_parameter_int(who, "max-bandwidth", value);
4c27486d
JQ
738}
739
cdf84229
JQ
740static void deprecated_set_cache_size(QTestState *who, long long value)
741{
742 QDict *rsp;
743
744 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
745 "'arguments': { 'value': %lld } }", value);
746 g_assert(qdict_haskey(rsp, "return"));
747 qobject_unref(rsp);
8f7798f1 748 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
cdf84229
JQ
749}
750
4c27486d
JQ
751static void test_deprecated(void)
752{
753 QTestState *from;
754
a2726593 755 from = qtest_init("-machine none");
4c27486d
JQ
756
757 deprecated_set_downtime(from, 0.12345);
c44a56d8 758 deprecated_set_speed(from, 12345);
cdf84229 759 deprecated_set_cache_size(from, 4096);
4c27486d
JQ
760
761 qtest_quit(from);
762}
763
d131662a 764static int migrate_postcopy_prepare(QTestState **from_ptr,
3e81f73c
PX
765 QTestState **to_ptr,
766 bool hide_error)
7195a871
JQ
767{
768 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
863e27a8 769 QTestState *from, *to;
7195a871 770
3af31a34 771 if (test_migrate_start(&from, &to, uri, hide_error, false, NULL, NULL)) {
d131662a 772 return -1;
5fd4a9c9 773 }
ea0c6d62 774
c44a56d8
MA
775 migrate_set_capability(from, "postcopy-ram", true);
776 migrate_set_capability(to, "postcopy-ram", true);
777 migrate_set_capability(to, "postcopy-blocktime", true);
ea0c6d62
DDAG
778
779 /* We want to pick a speed slow enough that the test completes
780 * quickly, but that it doesn't complete precopy even on a slow
781 * machine, so also set the downtime.
782 */
513aa2c6 783 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
8f7798f1 784 migrate_set_parameter_int(from, "downtime-limit", 1);
ea0c6d62
DDAG
785
786 /* Wait for the first serial output from the source */
787 wait_for_serial("src_serial");
788
b5bbd3f3 789 migrate(from, uri, "{}");
d131662a 790 g_free(uri);
ea0c6d62 791
863e27a8 792 wait_for_migration_pass(from);
ea0c6d62 793
d131662a
PX
794 *from_ptr = from;
795 *to_ptr = to;
ea0c6d62 796
d131662a
PX
797 return 0;
798}
ea0c6d62 799
d131662a
PX
800static void migrate_postcopy_complete(QTestState *from, QTestState *to)
801{
802 wait_for_migration_complete(from);
ea0c6d62 803
d131662a 804 /* Make sure we get at least one "B" on destination */
ea0c6d62 805 wait_for_serial("dest_serial");
ea0c6d62 806
346f3dab
AP
807 if (uffd_feature_thread_id) {
808 read_blocktime(to);
809 }
ea0c6d62 810
2c9bb297
DDAG
811 test_migrate_end(from, to, true);
812}
813
d131662a
PX
814static void test_postcopy(void)
815{
816 QTestState *from, *to;
817
3e81f73c 818 if (migrate_postcopy_prepare(&from, &to, false)) {
d131662a
PX
819 return;
820 }
821 migrate_postcopy_start(from, to);
822 migrate_postcopy_complete(from, to);
823}
824
d5f49640
PX
825static void test_postcopy_recovery(void)
826{
827 QTestState *from, *to;
828 char *uri;
829
3e81f73c 830 if (migrate_postcopy_prepare(&from, &to, true)) {
d5f49640
PX
831 return;
832 }
833
834 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
8f7798f1 835 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
d5f49640
PX
836
837 /* Now we start the postcopy */
838 migrate_postcopy_start(from, to);
839
840 /*
841 * Wait until postcopy is really started; we can only run the
842 * migrate-pause command during a postcopy
843 */
8c51642b 844 wait_for_migration_status(from, "postcopy-active", NULL);
d5f49640
PX
845
846 /*
847 * Manually stop the postcopy migration. This emulates a network
848 * failure with the migration socket
849 */
850 migrate_pause(from);
851
852 /*
853 * Wait for destination side to reach postcopy-paused state. The
854 * migrate-recover command can only succeed if destination machine
855 * is in the paused state
856 */
e15310ea
DDAG
857 wait_for_migration_status(to, "postcopy-paused",
858 (const char * []) { "failed", "active",
859 "completed", NULL });
d5f49640
PX
860
861 /*
862 * Create a new socket to emulate a new channel that is different
863 * from the broken migration channel; tell the destination to
864 * listen to the new port
865 */
866 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
867 migrate_recover(to, uri);
868
869 /*
870 * Try to rebuild the migration channel using the resume flag and
871 * the newly created channel
872 */
e15310ea
DDAG
873 wait_for_migration_status(from, "postcopy-paused",
874 (const char * []) { "failed", "active",
875 "completed", NULL });
b5bbd3f3 876 migrate(from, uri, "{'resume': true}");
d5f49640
PX
877 g_free(uri);
878
879 /* Restore the postcopy bandwidth to unlimited */
8f7798f1 880 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
d5f49640
PX
881
882 migrate_postcopy_complete(from, to);
883}
884
3af31a34 885static void wait_for_migration_fail(QTestState *from, bool allow_active)
2c9bb297 886{
e1454165 887 QDict *rsp_return;
2f7074c6 888 char *status;
2c9bb297
DDAG
889 bool failed;
890
2c9bb297 891 do {
2f7074c6 892 status = migrate_query_status(from);
84b2c7e5
DDAG
893 bool result = !strcmp(status, "setup") || !strcmp(status, "failed") ||
894 (allow_active && !strcmp(status, "active"));
895 if (!result) {
896 fprintf(stderr, "%s: unexpected status status=%s allow_active=%d\n",
897 __func__, status, allow_active);
898 }
899 g_assert(result);
2c9bb297 900 failed = !strcmp(status, "failed");
2f7074c6 901 g_free(status);
2c9bb297
DDAG
902 } while (!failed);
903
904 /* Is the machine currently running? */
e1454165 905 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
2c9bb297
DDAG
906 g_assert(qdict_haskey(rsp_return, "running"));
907 g_assert(qdict_get_bool(rsp_return, "running"));
e1454165 908 qobject_unref(rsp_return);
3af31a34
YK
909}
910
911static void test_baddest(void)
912{
913 QTestState *from, *to;
2c9bb297 914
3af31a34
YK
915 if (test_migrate_start(&from, &to, "tcp:0:0", true, false, NULL, NULL)) {
916 return;
917 }
918 migrate(from, "tcp:0:0", "{}");
919 wait_for_migration_fail(from, false);
2c9bb297 920 test_migrate_end(from, to, false);
ea0c6d62
DDAG
921}
922
2884100c
JQ
923static void test_precopy_unix(void)
924{
925 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
926 QTestState *from, *to;
927
3af31a34 928 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
5fd4a9c9
DDAG
929 return;
930 }
2884100c
JQ
931
932 /* We want to pick a speed slow enough that the test completes
933 * quickly, but that it doesn't complete precopy even on a slow
934 * machine, so also set the downtime.
935 */
936 /* 1 ms should make it not converge*/
8f7798f1 937 migrate_set_parameter_int(from, "downtime-limit", 1);
2884100c 938 /* 1GB/s */
8f7798f1 939 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
2884100c
JQ
940
941 /* Wait for the first serial output from the source */
942 wait_for_serial("src_serial");
943
b5bbd3f3 944 migrate(from, uri, "{}");
2884100c
JQ
945
946 wait_for_migration_pass(from);
947
948 /* 300 ms should converge */
8f7798f1 949 migrate_set_parameter_int(from, "downtime-limit", 300);
2884100c
JQ
950
951 if (!got_stop) {
952 qtest_qmp_eventwait(from, "STOP");
953 }
954
955 qtest_qmp_eventwait(to, "RESUME");
956
957 wait_for_serial("dest_serial");
958 wait_for_migration_complete(from);
959
960 test_migrate_end(from, to, true);
961 g_free(uri);
962}
963
660a9b68
YK
964#if 0
965/* Currently upset on aarch64 TCG */
966static void test_ignore_shared(void)
967{
968 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
969 QTestState *from, *to;
970
3af31a34 971 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
660a9b68
YK
972 return;
973 }
974
975 migrate_set_capability(from, "x-ignore-shared", true);
976 migrate_set_capability(to, "x-ignore-shared", true);
977
978 /* Wait for the first serial output from the source */
979 wait_for_serial("src_serial");
980
981 migrate(from, uri, "{}");
982
983 wait_for_migration_pass(from);
984
985 if (!got_stop) {
986 qtest_qmp_eventwait(from, "STOP");
987 }
988
989 qtest_qmp_eventwait(to, "RESUME");
990
991 wait_for_serial("dest_serial");
992 wait_for_migration_complete(from);
993
994 /* Check whether shared RAM has been really skipped */
995 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
996
997 test_migrate_end(from, to, true);
998 g_free(uri);
999}
1000#endif
1001
cdf84229
JQ
1002static void test_xbzrle(const char *uri)
1003{
1004 QTestState *from, *to;
1005
3af31a34 1006 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
cdf84229
JQ
1007 return;
1008 }
1009
1010 /*
1011 * We want to pick a speed slow enough that the test completes
1012 * quickly, but that it doesn't complete precopy even on a slow
1013 * machine, so also set the downtime.
1014 */
1015 /* 1 ms should make it not converge*/
8f7798f1 1016 migrate_set_parameter_int(from, "downtime-limit", 1);
cdf84229 1017 /* 1GB/s */
8f7798f1 1018 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
cdf84229 1019
8f7798f1 1020 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
cdf84229
JQ
1021
1022 migrate_set_capability(from, "xbzrle", "true");
1023 migrate_set_capability(to, "xbzrle", "true");
1024 /* Wait for the first serial output from the source */
1025 wait_for_serial("src_serial");
1026
1027 migrate(from, uri, "{}");
1028
1029 wait_for_migration_pass(from);
1030
1031 /* 300ms should converge */
8f7798f1 1032 migrate_set_parameter_int(from, "downtime-limit", 300);
cdf84229
JQ
1033
1034 if (!got_stop) {
1035 qtest_qmp_eventwait(from, "STOP");
1036 }
1037 qtest_qmp_eventwait(to, "RESUME");
1038
1039 wait_for_serial("dest_serial");
1040 wait_for_migration_complete(from);
1041
1042 test_migrate_end(from, to, true);
1043}
1044
1045static void test_xbzrle_unix(void)
1046{
1047 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1048
1049 test_xbzrle(uri);
1050 g_free(uri);
1051}
1052
609d3844
JQ
1053static void test_precopy_tcp(void)
1054{
1055 char *uri;
1056 QTestState *from, *to;
1057
3af31a34
YK
1058 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false,
1059 NULL, NULL)) {
609d3844
JQ
1060 return;
1061 }
1062
1063 /*
1064 * We want to pick a speed slow enough that the test completes
1065 * quickly, but that it doesn't complete precopy even on a slow
1066 * machine, so also set the downtime.
1067 */
1068 /* 1 ms should make it not converge*/
8f7798f1 1069 migrate_set_parameter_int(from, "downtime-limit", 1);
609d3844 1070 /* 1GB/s */
8f7798f1 1071 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
609d3844
JQ
1072
1073 /* Wait for the first serial output from the source */
1074 wait_for_serial("src_serial");
1075
1076 uri = migrate_get_socket_address(to, "socket-address");
1077
1078 migrate(from, uri, "{}");
1079
1080 wait_for_migration_pass(from);
1081
1082 /* 300ms should converge */
8f7798f1 1083 migrate_set_parameter_int(from, "downtime-limit", 300);
609d3844
JQ
1084
1085 if (!got_stop) {
1086 qtest_qmp_eventwait(from, "STOP");
1087 }
1088 qtest_qmp_eventwait(to, "RESUME");
1089
1090 wait_for_serial("dest_serial");
1091 wait_for_migration_complete(from);
1092
1093 test_migrate_end(from, to, true);
1094 g_free(uri);
1095}
1096
24d5588c
YK
1097static void test_migrate_fd_proto(void)
1098{
1099 QTestState *from, *to;
1100 int ret;
1101 int pair[2];
1102 QDict *rsp;
1103 const char *error_desc;
1104
3af31a34 1105 if (test_migrate_start(&from, &to, "defer", false, false, NULL, NULL)) {
24d5588c
YK
1106 return;
1107 }
1108
1109 /*
1110 * We want to pick a speed slow enough that the test completes
1111 * quickly, but that it doesn't complete precopy even on a slow
1112 * machine, so also set the downtime.
1113 */
1114 /* 1 ms should make it not converge */
8f7798f1 1115 migrate_set_parameter_int(from, "downtime-limit", 1);
24d5588c 1116 /* 1GB/s */
8f7798f1 1117 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
24d5588c
YK
1118
1119 /* Wait for the first serial output from the source */
1120 wait_for_serial("src_serial");
1121
1122 /* Create two connected sockets for migration */
1123 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1124 g_assert_cmpint(ret, ==, 0);
1125
1126 /* Send the 1st socket to the target */
1127 rsp = wait_command_fd(to, pair[0],
1128 "{ 'execute': 'getfd',"
1129 " 'arguments': { 'fdname': 'fd-mig' }}");
1130 qobject_unref(rsp);
1131 close(pair[0]);
1132
1133 /* Start incoming migration from the 1st socket */
1134 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1135 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1136 qobject_unref(rsp);
1137
1138 /* Send the 2nd socket to the target */
1139 rsp = wait_command_fd(from, pair[1],
1140 "{ 'execute': 'getfd',"
1141 " 'arguments': { 'fdname': 'fd-mig' }}");
1142 qobject_unref(rsp);
1143 close(pair[1]);
1144
1145 /* Start migration to the 2nd socket*/
1146 migrate(from, "fd:fd-mig", "{}");
1147
1148 wait_for_migration_pass(from);
1149
1150 /* 300ms should converge */
8f7798f1 1151 migrate_set_parameter_int(from, "downtime-limit", 300);
24d5588c
YK
1152
1153 if (!got_stop) {
1154 qtest_qmp_eventwait(from, "STOP");
1155 }
1156 qtest_qmp_eventwait(to, "RESUME");
1157
1158 /* Test closing fds */
1159 /* We assume, that QEMU removes named fd from its list,
1160 * so this should fail */
1161 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1162 " 'arguments': { 'fdname': 'fd-mig' }}");
1163 g_assert_true(qdict_haskey(rsp, "error"));
1164 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1165 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1166 qobject_unref(rsp);
1167
1168 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1169 " 'arguments': { 'fdname': 'fd-mig' }}");
1170 g_assert_true(qdict_haskey(rsp, "error"));
1171 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1172 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1173 qobject_unref(rsp);
1174
1175 /* Complete migration */
1176 wait_for_serial("dest_serial");
1177 wait_for_migration_complete(from);
1178 test_migrate_end(from, to, true);
1179}
1180
3af31a34
YK
1181static void do_test_validate_uuid(const char *uuid_arg_src,
1182 const char *uuid_arg_dst,
1183 bool should_fail, bool hide_stderr)
1184{
1185 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1186 QTestState *from, *to;
1187
1188 if (test_migrate_start(&from, &to, uri, hide_stderr, false,
1189 uuid_arg_src, uuid_arg_dst)) {
1190 return;
1191 }
1192
1193 /*
1194 * UUID validation is at the begin of migration. So, the main process of
1195 * migration is not interesting for us here. Thus, set huge downtime for
1196 * very fast migration.
1197 */
1198 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1199 migrate_set_capability(from, "validate-uuid", true);
1200
1201 /* Wait for the first serial output from the source */
1202 wait_for_serial("src_serial");
1203
1204 migrate(from, uri, "{}");
1205
1206 if (should_fail) {
1207 qtest_set_expected_status(to, 1);
1208 wait_for_migration_fail(from, true);
1209 } else {
1210 wait_for_migration_complete(from);
1211 }
1212
1213 test_migrate_end(from, to, false);
1214 g_free(uri);
1215}
1216
1217static void test_validate_uuid(void)
1218{
1219 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1220 "-uuid 11111111-1111-1111-1111-111111111111",
1221 false, false);
1222}
1223
1224static void test_validate_uuid_error(void)
1225{
1226 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
1227 "-uuid 22222222-2222-2222-2222-222222222222",
1228 true, true);
1229}
1230
1231static void test_validate_uuid_src_not_set(void)
1232{
1233 do_test_validate_uuid(NULL, "-uuid 11111111-1111-1111-1111-111111111111",
1234 false, true);
1235}
1236
1237static void test_validate_uuid_dst_not_set(void)
1238{
1239 do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", NULL,
1240 false, true);
1241}
1242
8c51642b
YK
1243static void test_migrate_auto_converge(void)
1244{
1245 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1246 QTestState *from, *to;
1247 int64_t remaining, percentage;
1248
1249 /*
1250 * We want the test to be stable and as fast as possible.
1251 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1252 * so we need to decrease a bandwidth.
1253 */
1254 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1255 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1256 const int64_t downtime_limit = 250; /* 250ms */
1257 /*
1258 * We migrate through unix-socket (> 500Mb/s).
1259 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1260 * So, we can predict expected_threshold
1261 */
1262 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1263
1264 if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
1265 return;
1266 }
1267
1268 migrate_set_capability(from, "auto-converge", true);
1269 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1270 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1271 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1272
1273 /*
1274 * Set the initial parameters so that the migration could not converge
1275 * without throttling.
1276 */
1277 migrate_set_parameter_int(from, "downtime-limit", 1);
1278 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1279
1280 /* To check remaining size after precopy */
1281 migrate_set_capability(from, "pause-before-switchover", true);
1282
1283 /* Wait for the first serial output from the source */
1284 wait_for_serial("src_serial");
1285
1286 migrate(from, uri, "{}");
1287
1288 /* Wait for throttling begins */
1289 percentage = 0;
1290 while (percentage == 0) {
1291 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1292 usleep(100);
1293 g_assert_false(got_stop);
1294 }
1295 /* The first percentage of throttling should be equal to init_pct */
1296 g_assert_cmpint(percentage, ==, init_pct);
1297 /* Now, when we tested that throttling works, let it converge */
1298 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1299 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1300
1301 /*
1302 * Wait for pre-switchover status to check last throttle percentage
1303 * and remaining. These values will be zeroed later
1304 */
1305 wait_for_migration_status(from, "pre-switchover", NULL);
1306
1307 /* The final percentage of throttling shouldn't be greater than max_pct */
1308 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1309 g_assert_cmpint(percentage, <=, max_pct);
1310
1311 remaining = read_ram_property_int(from, "remaining");
1312 g_assert_cmpint(remaining, <, expected_threshold);
1313
1314 migrate_continue(from, "pre-switchover");
1315
1316 qtest_qmp_eventwait(to, "RESUME");
1317
1318 wait_for_serial("dest_serial");
1319 wait_for_migration_complete(from);
1320
1321 g_free(uri);
1322
1323 test_migrate_end(from, to, true);
1324}
1325
ea0c6d62
DDAG
1326int main(int argc, char **argv)
1327{
2656bfd9 1328 char template[] = "/tmp/migration-test-XXXXXX";
ea0c6d62
DDAG
1329 int ret;
1330
1331 g_test_init(&argc, &argv, NULL);
1332
1333 if (!ufd_version_check()) {
4848cb3d 1334 return g_test_run();
ea0c6d62
DDAG
1335 }
1336
880b169a
TH
1337 /*
1338 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1339 * is touchy due to race conditions on dirty bits (especially on PPC for
1340 * some reason)
1341 */
1342 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1ee5e144
LV
1343 (access("/sys/module/kvm_hv", F_OK) ||
1344 access("/dev/kvm", R_OK | W_OK))) {
880b169a 1345 g_test_message("Skipping test: kvm_hv not available");
4848cb3d 1346 return g_test_run();
880b169a
TH
1347 }
1348
d254b392
TH
1349 /*
1350 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1351 * there until the problems are resolved
1352 */
1353 if (g_str_equal(qtest_get_arch(), "s390x")) {
1354#if defined(HOST_S390X)
1355 if (access("/dev/kvm", R_OK | W_OK)) {
1356 g_test_message("Skipping test: kvm not available");
4848cb3d 1357 return g_test_run();
d254b392
TH
1358 }
1359#else
1360 g_test_message("Skipping test: Need s390x host to work properly");
4848cb3d 1361 return g_test_run();
d254b392
TH
1362#endif
1363 }
1364
ea0c6d62
DDAG
1365 tmpfs = mkdtemp(template);
1366 if (!tmpfs) {
13ee9e30 1367 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
ea0c6d62
DDAG
1368 }
1369 g_assert(tmpfs);
1370
1371 module_call_init(MODULE_INIT_QOM);
1372
2884100c 1373 qtest_add_func("/migration/postcopy/unix", test_postcopy);
d5f49640 1374 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
4c27486d 1375 qtest_add_func("/migration/deprecated", test_deprecated);
2c9bb297 1376 qtest_add_func("/migration/bad_dest", test_baddest);
2884100c 1377 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
609d3844 1378 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
660a9b68 1379 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
cdf84229 1380 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
24d5588c 1381 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
3af31a34
YK
1382 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1383 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1384 qtest_add_func("/migration/validate_uuid_src_not_set",
1385 test_validate_uuid_src_not_set);
1386 qtest_add_func("/migration/validate_uuid_dst_not_set",
1387 test_validate_uuid_dst_not_set);
ea0c6d62 1388
8c51642b
YK
1389 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
1390
ea0c6d62
DDAG
1391 ret = g_test_run();
1392
1393 g_assert_cmpint(ret, ==, 0);
1394
1395 ret = rmdir(tmpfs);
1396 if (ret != 0) {
13ee9e30 1397 g_test_message("unable to rmdir: path (%s): %s",
ea0c6d62
DDAG
1398 tmpfs, strerror(errno));
1399 }
1400
1401 return ret;
1402}