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