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