]> git.proxmox.com Git - mirror_qemu.git/blame - tests/migration-test.c
tests/tcg: fix typo when calling clean-tcg
[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
660a9b68
YK
243static uint64_t get_migration_pass(QTestState *who)
244{
245 return read_ram_property_int(who, "dirty-sync-count");
246}
247
346f3dab
AP
248static void read_blocktime(QTestState *who)
249{
2f7074c6 250 QDict *rsp_return;
346f3dab 251
2f7074c6 252 rsp_return = migrate_query(who);
346f3dab 253 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
2f7074c6 254 qobject_unref(rsp_return);
346f3dab
AP
255}
256
2f6d3138
PX
257static void wait_for_migration_status(QTestState *who,
258 const char *goal)
ea0c6d62 259{
6a7724e9 260 while (true) {
6a7724e9 261 bool completed;
2f7074c6 262 char *status;
ea0c6d62 263
2f7074c6 264 status = migrate_query_status(who);
2f6d3138 265 completed = strcmp(status, goal) == 0;
ea0c6d62 266 g_assert_cmpstr(status, !=, "failed");
2f7074c6 267 g_free(status);
6a7724e9
JQ
268 if (completed) {
269 return;
270 }
271 usleep(1000);
272 }
ea0c6d62
DDAG
273}
274
2f6d3138
PX
275static void wait_for_migration_complete(QTestState *who)
276{
277 wait_for_migration_status(who, "completed");
278}
279
863e27a8 280static void wait_for_migration_pass(QTestState *who)
ea0c6d62 281{
863e27a8 282 uint64_t initial_pass = get_migration_pass(who);
ea0c6d62
DDAG
283 uint64_t pass;
284
285 /* Wait for the 1st sync */
6a7724e9
JQ
286 while (!got_stop && !initial_pass) {
287 usleep(1000);
863e27a8 288 initial_pass = get_migration_pass(who);
6a7724e9 289 }
ea0c6d62
DDAG
290
291 do {
6a7724e9 292 usleep(1000);
863e27a8 293 pass = get_migration_pass(who);
ea0c6d62
DDAG
294 } while (pass == initial_pass && !got_stop);
295}
296
7195a871 297static void check_guests_ram(QTestState *who)
ea0c6d62
DDAG
298{
299 /* Our ASM test will have been incrementing one byte from each page from
e51e711b
WH
300 * start_address to < end_address in order. This gives us a constraint
301 * that any page's byte should be equal or less than the previous pages
302 * byte (mod 256); and they should all be equal except for one transition
303 * at the point where we meet the incrementer. (We're running this with
304 * the guest stopped).
ea0c6d62
DDAG
305 */
306 unsigned address;
307 uint8_t first_byte;
308 uint8_t last_byte;
309 bool hit_edge = false;
601b5575 310 int bad = 0;
ea0c6d62 311
7195a871 312 qtest_memread(who, start_address, &first_byte, 1);
ea0c6d62
DDAG
313 last_byte = first_byte;
314
e51e711b
WH
315 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
316 address += TEST_MEM_PAGE_SIZE)
ea0c6d62
DDAG
317 {
318 uint8_t b;
7195a871 319 qtest_memread(who, address, &b, 1);
ea0c6d62
DDAG
320 if (b != last_byte) {
321 if (((b + 1) % 256) == last_byte && !hit_edge) {
322 /* This is OK, the guest stopped at the point of
323 * incrementing the previous page but didn't get
324 * to us yet.
325 */
326 hit_edge = true;
829db8b4 327 last_byte = b;
ea0c6d62 328 } else {
601b5575
AB
329 bad++;
330 if (bad <= 10) {
331 fprintf(stderr, "Memory content inconsistency at %x"
332 " first_byte = %x last_byte = %x current = %x"
333 " hit_edge = %x\n",
334 address, first_byte, last_byte, b, hit_edge);
335 }
ea0c6d62
DDAG
336 }
337 }
ea0c6d62 338 }
601b5575
AB
339 if (bad >= 10) {
340 fprintf(stderr, "and in another %d pages", bad - 10);
341 }
342 g_assert(bad == 0);
ea0c6d62
DDAG
343}
344
345static void cleanup(const char *filename)
346{
347 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
348
349 unlink(path);
e2dd21e5 350 g_free(path);
ea0c6d62
DDAG
351}
352
660a9b68
YK
353static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
354{
355 return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
356 ",mem-path=%s,share=on -numa node,memdev=mem0",
357 mem_size, shmem_path);
358}
359
609d3844
JQ
360static char *SocketAddress_to_str(SocketAddress *addr)
361{
362 switch (addr->type) {
363 case SOCKET_ADDRESS_TYPE_INET:
364 return g_strdup_printf("tcp:%s:%s",
365 addr->u.inet.host,
366 addr->u.inet.port);
367 case SOCKET_ADDRESS_TYPE_UNIX:
368 return g_strdup_printf("unix:%s",
369 addr->u.q_unix.path);
370 case SOCKET_ADDRESS_TYPE_FD:
371 return g_strdup_printf("fd:%s", addr->u.fd.str);
372 case SOCKET_ADDRESS_TYPE_VSOCK:
373 return g_strdup_printf("tcp:%s:%s",
374 addr->u.vsock.cid,
375 addr->u.vsock.port);
376 default:
377 return g_strdup("unknown address type");
378 }
379}
380
381static char *migrate_get_socket_address(QTestState *who, const char *parameter)
382{
383 QDict *rsp;
384 char *result;
385 Error *local_err = NULL;
386 SocketAddressList *addrs;
387 Visitor *iv = NULL;
388 QObject *object;
389
390 rsp = migrate_query(who);
391 object = qdict_get(rsp, parameter);
392
393 iv = qobject_input_visitor_new(object);
394 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
1e25879e 395 visit_free(iv);
609d3844
JQ
396
397 /* we are only using a single address */
1e25879e 398 result = SocketAddress_to_str(addrs->value);
609d3844
JQ
399
400 qapi_free_SocketAddressList(addrs);
401 qobject_unref(rsp);
402 return result;
403}
404
8f7798f1
JQ
405static long long migrate_get_parameter_int(QTestState *who,
406 const char *parameter)
609d3844
JQ
407{
408 QDict *rsp;
409 long long result;
410
411 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
412 result = qdict_get_int(rsp, parameter);
413 qobject_unref(rsp);
414 return result;
415}
416
8f7798f1
JQ
417static void migrate_check_parameter_int(QTestState *who, const char *parameter,
418 long long value)
56b4a42a 419{
609d3844 420 long long result;
56b4a42a 421
8f7798f1 422 result = migrate_get_parameter_int(who, parameter);
609d3844 423 g_assert_cmpint(result, ==, value);
56b4a42a
JQ
424}
425
8f7798f1
JQ
426static void migrate_set_parameter_int(QTestState *who, const char *parameter,
427 long long value)
d62fbe60
JQ
428{
429 QDict *rsp;
d62fbe60 430
c44a56d8
MA
431 rsp = qtest_qmp(who,
432 "{ 'execute': 'migrate-set-parameters',"
433 "'arguments': { %s: %lld } }",
434 parameter, value);
d62fbe60 435 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 436 qobject_unref(rsp);
8f7798f1 437 migrate_check_parameter_int(who, parameter, value);
d62fbe60
JQ
438}
439
d5f49640
PX
440static void migrate_pause(QTestState *who)
441{
442 QDict *rsp;
443
444 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
d5f49640
PX
445 qobject_unref(rsp);
446}
447
448static void migrate_recover(QTestState *who, const char *uri)
449{
450 QDict *rsp;
d5f49640 451
b7281c69
MA
452 rsp = wait_command(who,
453 "{ 'execute': 'migrate-recover', "
454 " 'id': 'recover-cmd', "
455 " 'arguments': { 'uri': %s } }",
456 uri);
d5f49640
PX
457 qobject_unref(rsp);
458}
459
d62fbe60 460static void migrate_set_capability(QTestState *who, const char *capability,
c44a56d8 461 bool value)
d62fbe60
JQ
462{
463 QDict *rsp;
c44a56d8
MA
464
465 rsp = qtest_qmp(who,
466 "{ 'execute': 'migrate-set-capabilities',"
467 "'arguments': { "
468 "'capabilities': [ { "
469 "'capability': %s, 'state': %i } ] } }",
470 capability, value);
d62fbe60 471 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 472 qobject_unref(rsp);
d62fbe60
JQ
473}
474
b5bbd3f3
MA
475/*
476 * Send QMP command "migrate".
477 * Arguments are built from @fmt... (formatted like
478 * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
479 */
480GCC_FMT_ATTR(3, 4)
481static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
d62fbe60 482{
b5bbd3f3
MA
483 va_list ap;
484 QDict *args, *rsp;
d62fbe60 485
b5bbd3f3
MA
486 va_start(ap, fmt);
487 args = qdict_from_vjsonf_nofail(fmt, ap);
488 va_end(ap);
489
490 g_assert(!qdict_haskey(args, "uri"));
491 qdict_put_str(args, "uri", uri);
492
a2726593 493 rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args);
24d5588c 494
d62fbe60 495 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 496 qobject_unref(rsp);
d62fbe60
JQ
497}
498
d131662a 499static void migrate_postcopy_start(QTestState *from, QTestState *to)
eb665d7d
JQ
500{
501 QDict *rsp;
502
d131662a 503 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
cb3e7f08 504 qobject_unref(rsp);
d131662a
PX
505
506 if (!got_stop) {
507 qtest_qmp_eventwait(from, "STOP");
508 }
509
510 qtest_qmp_eventwait(to, "RESUME");
eb665d7d
JQ
511}
512
5fd4a9c9 513static int test_migrate_start(QTestState **from, QTestState **to,
660a9b68
YK
514 const char *uri, bool hide_stderr,
515 bool use_shmem)
ea0c6d62 516{
d62fbe60 517 gchar *cmd_src, *cmd_dst;
660a9b68
YK
518 char *bootpath = NULL;
519 char *extra_opts = NULL;
520 char *shmem_path = NULL;
aaf89c8a 521 const char *arch = qtest_get_arch();
63b2d935 522 const char *accel = "kvm:tcg";
ea0c6d62 523
660a9b68
YK
524 if (use_shmem) {
525 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
526 g_test_skip("/dev/shm is not supported");
527 return -1;
528 }
529 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
530 }
ea0c6d62 531
660a9b68
YK
532 got_stop = false;
533 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
aaf89c8a 534 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
2785f196
PM
535 /* the assembled x86 boot sector should be exactly one sector large */
536 assert(sizeof(x86_bootsect) == 512);
537 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
660a9b68 538 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
63b2d935 539 cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
31a6bb74 540 " -name source,debug-threads=on"
aaf89c8a 541 " -serial file:%s/src_serial"
660a9b68
YK
542 " -drive file=%s,format=raw %s",
543 accel, tmpfs, bootpath,
544 extra_opts ? extra_opts : "");
63b2d935 545 cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
31a6bb74 546 " -name target,debug-threads=on"
aaf89c8a 547 " -serial file:%s/dest_serial"
548 " -drive file=%s,format=raw"
660a9b68
YK
549 " -incoming %s %s",
550 accel, tmpfs, bootpath, uri,
551 extra_opts ? extra_opts : "");
e51e711b
WH
552 start_address = X86_TEST_MEM_START;
553 end_address = X86_TEST_MEM_END;
5571dc82 554 } else if (g_str_equal(arch, "s390x")) {
2785f196 555 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
660a9b68 556 extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
5571dc82
TH
557 cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
558 " -name source,debug-threads=on"
660a9b68
YK
559 " -serial file:%s/src_serial -bios %s %s",
560 accel, tmpfs, bootpath,
561 extra_opts ? extra_opts : "");
5571dc82
TH
562 cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
563 " -name target,debug-threads=on"
564 " -serial file:%s/dest_serial -bios %s"
660a9b68
YK
565 " -incoming %s %s",
566 accel, tmpfs, bootpath, uri,
567 extra_opts ? extra_opts : "");
5571dc82
TH
568 start_address = S390_TEST_MEM_START;
569 end_address = S390_TEST_MEM_END;
aaf89c8a 570 } else if (strcmp(arch, "ppc64") == 0) {
660a9b68 571 extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
fc71e3e5 572 cmd_src = g_strdup_printf("-machine accel=%s -m 256M -nodefaults"
31a6bb74 573 " -name source,debug-threads=on"
aaf89c8a 574 " -serial file:%s/src_serial"
fc71e3e5
TH
575 " -prom-env 'use-nvramrc?=true' -prom-env "
576 "'nvramrc=hex .\" _\" begin %x %x "
cdf33815 577 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
660a9b68
YK
578 "until' %s", accel, tmpfs, end_address,
579 start_address, extra_opts ? extra_opts : "");
171da9d5 580 cmd_dst = g_strdup_printf("-machine accel=%s -m 256M"
31a6bb74 581 " -name target,debug-threads=on"
aaf89c8a 582 " -serial file:%s/dest_serial"
660a9b68
YK
583 " -incoming %s %s",
584 accel, tmpfs, uri,
585 extra_opts ? extra_opts : "");
e51e711b
WH
586
587 start_address = PPC_TEST_MEM_START;
588 end_address = PPC_TEST_MEM_END;
c02b3781 589 } else if (strcmp(arch, "aarch64") == 0) {
2785f196 590 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
660a9b68 591 extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
c02b3781
WH
592 cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
593 "-name vmsource,debug-threads=on -cpu max "
594 "-m 150M -serial file:%s/src_serial "
660a9b68
YK
595 "-kernel %s %s",
596 accel, tmpfs, bootpath,
597 extra_opts ? extra_opts : "");
c02b3781
WH
598 cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
599 "-name vmdest,debug-threads=on -cpu max "
600 "-m 150M -serial file:%s/dest_serial "
601 "-kernel %s "
660a9b68
YK
602 "-incoming %s %s",
603 accel, tmpfs, bootpath, uri,
604 extra_opts ? extra_opts : "");
c02b3781
WH
605
606 start_address = ARM_TEST_MEM_START;
607 end_address = ARM_TEST_MEM_END;
608
609 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
aaf89c8a 610 } else {
611 g_assert_not_reached();
612 }
613
e2dd21e5 614 g_free(bootpath);
660a9b68 615 g_free(extra_opts);
e2dd21e5 616
f96d6651
DDAG
617 if (hide_stderr) {
618 gchar *tmp;
619 tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
620 g_free(cmd_src);
621 cmd_src = tmp;
622
623 tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
624 g_free(cmd_dst);
625 cmd_dst = tmp;
626 }
627
a2726593 628 *from = qtest_init(cmd_src);
aaf89c8a 629 g_free(cmd_src);
630
7195a871 631 *to = qtest_init(cmd_dst);
aaf89c8a 632 g_free(cmd_dst);
660a9b68
YK
633
634 /*
635 * Remove shmem file immediately to avoid memory leak in test failed case.
636 * It's valid becase QEMU has already opened this file
637 */
638 if (use_shmem) {
639 unlink(shmem_path);
640 g_free(shmem_path);
641 }
642
5fd4a9c9 643 return 0;
7195a871
JQ
644}
645
2c9bb297 646static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
7195a871
JQ
647{
648 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
649
650 qtest_quit(from);
651
2c9bb297
DDAG
652 if (test_dest) {
653 qtest_memread(to, start_address, &dest_byte_a, 1);
7195a871 654
2c9bb297
DDAG
655 /* Destination still running, wait for a byte to change */
656 do {
657 qtest_memread(to, start_address, &dest_byte_b, 1);
658 usleep(1000 * 10);
659 } while (dest_byte_a == dest_byte_b);
660
661 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
7195a871 662
2c9bb297
DDAG
663 /* With it stopped, check nothing changes */
664 qtest_memread(to, start_address, &dest_byte_c, 1);
665 usleep(1000 * 200);
666 qtest_memread(to, start_address, &dest_byte_d, 1);
667 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
7195a871 668
2c9bb297
DDAG
669 check_guests_ram(to);
670 }
7195a871
JQ
671
672 qtest_quit(to);
673
674 cleanup("bootsect");
675 cleanup("migsocket");
676 cleanup("src_serial");
677 cleanup("dest_serial");
678}
679
4c27486d
JQ
680static void deprecated_set_downtime(QTestState *who, const double value)
681{
682 QDict *rsp;
4c27486d 683
015715f5
MA
684 rsp = qtest_qmp(who,
685 "{ 'execute': 'migrate_set_downtime',"
686 " 'arguments': { 'value': %f } }", value);
4c27486d 687 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 688 qobject_unref(rsp);
8f7798f1 689 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
4c27486d
JQ
690}
691
c44a56d8 692static void deprecated_set_speed(QTestState *who, long long value)
4c27486d
JQ
693{
694 QDict *rsp;
4c27486d 695
c44a56d8
MA
696 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
697 "'arguments': { 'value': %lld } }", value);
4c27486d 698 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 699 qobject_unref(rsp);
8f7798f1 700 migrate_check_parameter_int(who, "max-bandwidth", value);
4c27486d
JQ
701}
702
cdf84229
JQ
703static void deprecated_set_cache_size(QTestState *who, long long value)
704{
705 QDict *rsp;
706
707 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
708 "'arguments': { 'value': %lld } }", value);
709 g_assert(qdict_haskey(rsp, "return"));
710 qobject_unref(rsp);
8f7798f1 711 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
cdf84229
JQ
712}
713
4c27486d
JQ
714static void test_deprecated(void)
715{
716 QTestState *from;
717
a2726593 718 from = qtest_init("-machine none");
4c27486d
JQ
719
720 deprecated_set_downtime(from, 0.12345);
c44a56d8 721 deprecated_set_speed(from, 12345);
cdf84229 722 deprecated_set_cache_size(from, 4096);
4c27486d
JQ
723
724 qtest_quit(from);
725}
726
d131662a 727static int migrate_postcopy_prepare(QTestState **from_ptr,
3e81f73c
PX
728 QTestState **to_ptr,
729 bool hide_error)
7195a871
JQ
730{
731 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
863e27a8 732 QTestState *from, *to;
7195a871 733
660a9b68 734 if (test_migrate_start(&from, &to, uri, hide_error, false)) {
d131662a 735 return -1;
5fd4a9c9 736 }
ea0c6d62 737
c44a56d8
MA
738 migrate_set_capability(from, "postcopy-ram", true);
739 migrate_set_capability(to, "postcopy-ram", true);
740 migrate_set_capability(to, "postcopy-blocktime", true);
ea0c6d62
DDAG
741
742 /* We want to pick a speed slow enough that the test completes
743 * quickly, but that it doesn't complete precopy even on a slow
744 * machine, so also set the downtime.
745 */
8f7798f1
JQ
746 migrate_set_parameter_int(from, "max-bandwidth", 100000000);
747 migrate_set_parameter_int(from, "downtime-limit", 1);
ea0c6d62
DDAG
748
749 /* Wait for the first serial output from the source */
750 wait_for_serial("src_serial");
751
b5bbd3f3 752 migrate(from, uri, "{}");
d131662a 753 g_free(uri);
ea0c6d62 754
863e27a8 755 wait_for_migration_pass(from);
ea0c6d62 756
d131662a
PX
757 *from_ptr = from;
758 *to_ptr = to;
ea0c6d62 759
d131662a
PX
760 return 0;
761}
ea0c6d62 762
d131662a
PX
763static void migrate_postcopy_complete(QTestState *from, QTestState *to)
764{
765 wait_for_migration_complete(from);
ea0c6d62 766
d131662a 767 /* Make sure we get at least one "B" on destination */
ea0c6d62 768 wait_for_serial("dest_serial");
ea0c6d62 769
346f3dab
AP
770 if (uffd_feature_thread_id) {
771 read_blocktime(to);
772 }
ea0c6d62 773
2c9bb297
DDAG
774 test_migrate_end(from, to, true);
775}
776
d131662a
PX
777static void test_postcopy(void)
778{
779 QTestState *from, *to;
780
3e81f73c 781 if (migrate_postcopy_prepare(&from, &to, false)) {
d131662a
PX
782 return;
783 }
784 migrate_postcopy_start(from, to);
785 migrate_postcopy_complete(from, to);
786}
787
d5f49640
PX
788static void test_postcopy_recovery(void)
789{
790 QTestState *from, *to;
791 char *uri;
792
3e81f73c 793 if (migrate_postcopy_prepare(&from, &to, true)) {
d5f49640
PX
794 return;
795 }
796
797 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
8f7798f1 798 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
d5f49640
PX
799
800 /* Now we start the postcopy */
801 migrate_postcopy_start(from, to);
802
803 /*
804 * Wait until postcopy is really started; we can only run the
805 * migrate-pause command during a postcopy
806 */
807 wait_for_migration_status(from, "postcopy-active");
808
809 /*
810 * Manually stop the postcopy migration. This emulates a network
811 * failure with the migration socket
812 */
813 migrate_pause(from);
814
815 /*
816 * Wait for destination side to reach postcopy-paused state. The
817 * migrate-recover command can only succeed if destination machine
818 * is in the paused state
819 */
820 wait_for_migration_status(to, "postcopy-paused");
821
822 /*
823 * Create a new socket to emulate a new channel that is different
824 * from the broken migration channel; tell the destination to
825 * listen to the new port
826 */
827 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
828 migrate_recover(to, uri);
829
830 /*
831 * Try to rebuild the migration channel using the resume flag and
832 * the newly created channel
833 */
834 wait_for_migration_status(from, "postcopy-paused");
b5bbd3f3 835 migrate(from, uri, "{'resume': true}");
d5f49640
PX
836 g_free(uri);
837
838 /* Restore the postcopy bandwidth to unlimited */
8f7798f1 839 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
d5f49640
PX
840
841 migrate_postcopy_complete(from, to);
842}
843
2c9bb297
DDAG
844static void test_baddest(void)
845{
846 QTestState *from, *to;
e1454165 847 QDict *rsp_return;
2f7074c6 848 char *status;
2c9bb297
DDAG
849 bool failed;
850
660a9b68 851 if (test_migrate_start(&from, &to, "tcp:0:0", true, false)) {
5fd4a9c9
DDAG
852 return;
853 }
b5bbd3f3 854 migrate(from, "tcp:0:0", "{}");
2c9bb297 855 do {
2f7074c6 856 status = migrate_query_status(from);
2c9bb297
DDAG
857 g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
858 failed = !strcmp(status, "failed");
2f7074c6 859 g_free(status);
2c9bb297
DDAG
860 } while (!failed);
861
862 /* Is the machine currently running? */
e1454165 863 rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
2c9bb297
DDAG
864 g_assert(qdict_haskey(rsp_return, "running"));
865 g_assert(qdict_get_bool(rsp_return, "running"));
e1454165 866 qobject_unref(rsp_return);
2c9bb297
DDAG
867
868 test_migrate_end(from, to, false);
ea0c6d62
DDAG
869}
870
2884100c
JQ
871static void test_precopy_unix(void)
872{
873 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
874 QTestState *from, *to;
875
660a9b68 876 if (test_migrate_start(&from, &to, uri, false, false)) {
5fd4a9c9
DDAG
877 return;
878 }
2884100c
JQ
879
880 /* We want to pick a speed slow enough that the test completes
881 * quickly, but that it doesn't complete precopy even on a slow
882 * machine, so also set the downtime.
883 */
884 /* 1 ms should make it not converge*/
8f7798f1 885 migrate_set_parameter_int(from, "downtime-limit", 1);
2884100c 886 /* 1GB/s */
8f7798f1 887 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
2884100c
JQ
888
889 /* Wait for the first serial output from the source */
890 wait_for_serial("src_serial");
891
b5bbd3f3 892 migrate(from, uri, "{}");
2884100c
JQ
893
894 wait_for_migration_pass(from);
895
896 /* 300 ms should converge */
8f7798f1 897 migrate_set_parameter_int(from, "downtime-limit", 300);
2884100c
JQ
898
899 if (!got_stop) {
900 qtest_qmp_eventwait(from, "STOP");
901 }
902
903 qtest_qmp_eventwait(to, "RESUME");
904
905 wait_for_serial("dest_serial");
906 wait_for_migration_complete(from);
907
908 test_migrate_end(from, to, true);
909 g_free(uri);
910}
911
660a9b68
YK
912#if 0
913/* Currently upset on aarch64 TCG */
914static void test_ignore_shared(void)
915{
916 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
917 QTestState *from, *to;
918
919 if (test_migrate_start(&from, &to, uri, false, true)) {
920 return;
921 }
922
923 migrate_set_capability(from, "x-ignore-shared", true);
924 migrate_set_capability(to, "x-ignore-shared", true);
925
926 /* Wait for the first serial output from the source */
927 wait_for_serial("src_serial");
928
929 migrate(from, uri, "{}");
930
931 wait_for_migration_pass(from);
932
933 if (!got_stop) {
934 qtest_qmp_eventwait(from, "STOP");
935 }
936
937 qtest_qmp_eventwait(to, "RESUME");
938
939 wait_for_serial("dest_serial");
940 wait_for_migration_complete(from);
941
942 /* Check whether shared RAM has been really skipped */
943 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
944
945 test_migrate_end(from, to, true);
946 g_free(uri);
947}
948#endif
949
cdf84229
JQ
950static void test_xbzrle(const char *uri)
951{
952 QTestState *from, *to;
953
954 if (test_migrate_start(&from, &to, uri, false, false)) {
955 return;
956 }
957
958 /*
959 * We want to pick a speed slow enough that the test completes
960 * quickly, but that it doesn't complete precopy even on a slow
961 * machine, so also set the downtime.
962 */
963 /* 1 ms should make it not converge*/
8f7798f1 964 migrate_set_parameter_int(from, "downtime-limit", 1);
cdf84229 965 /* 1GB/s */
8f7798f1 966 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
cdf84229 967
8f7798f1 968 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
cdf84229
JQ
969
970 migrate_set_capability(from, "xbzrle", "true");
971 migrate_set_capability(to, "xbzrle", "true");
972 /* Wait for the first serial output from the source */
973 wait_for_serial("src_serial");
974
975 migrate(from, uri, "{}");
976
977 wait_for_migration_pass(from);
978
979 /* 300ms should converge */
8f7798f1 980 migrate_set_parameter_int(from, "downtime-limit", 300);
cdf84229
JQ
981
982 if (!got_stop) {
983 qtest_qmp_eventwait(from, "STOP");
984 }
985 qtest_qmp_eventwait(to, "RESUME");
986
987 wait_for_serial("dest_serial");
988 wait_for_migration_complete(from);
989
990 test_migrate_end(from, to, true);
991}
992
993static void test_xbzrle_unix(void)
994{
995 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
996
997 test_xbzrle(uri);
998 g_free(uri);
999}
1000
609d3844
JQ
1001static void test_precopy_tcp(void)
1002{
1003 char *uri;
1004 QTestState *from, *to;
1005
1006 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false)) {
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);
609d3844 1017 /* 1GB/s */
8f7798f1 1018 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
609d3844
JQ
1019
1020 /* Wait for the first serial output from the source */
1021 wait_for_serial("src_serial");
1022
1023 uri = migrate_get_socket_address(to, "socket-address");
1024
1025 migrate(from, uri, "{}");
1026
1027 wait_for_migration_pass(from);
1028
1029 /* 300ms should converge */
8f7798f1 1030 migrate_set_parameter_int(from, "downtime-limit", 300);
609d3844
JQ
1031
1032 if (!got_stop) {
1033 qtest_qmp_eventwait(from, "STOP");
1034 }
1035 qtest_qmp_eventwait(to, "RESUME");
1036
1037 wait_for_serial("dest_serial");
1038 wait_for_migration_complete(from);
1039
1040 test_migrate_end(from, to, true);
1041 g_free(uri);
1042}
1043
24d5588c
YK
1044static void test_migrate_fd_proto(void)
1045{
1046 QTestState *from, *to;
1047 int ret;
1048 int pair[2];
1049 QDict *rsp;
1050 const char *error_desc;
1051
1052 if (test_migrate_start(&from, &to, "defer", false, false)) {
1053 return;
1054 }
1055
1056 /*
1057 * We want to pick a speed slow enough that the test completes
1058 * quickly, but that it doesn't complete precopy even on a slow
1059 * machine, so also set the downtime.
1060 */
1061 /* 1 ms should make it not converge */
8f7798f1 1062 migrate_set_parameter_int(from, "downtime-limit", 1);
24d5588c 1063 /* 1GB/s */
8f7798f1 1064 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
24d5588c
YK
1065
1066 /* Wait for the first serial output from the source */
1067 wait_for_serial("src_serial");
1068
1069 /* Create two connected sockets for migration */
1070 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1071 g_assert_cmpint(ret, ==, 0);
1072
1073 /* Send the 1st socket to the target */
1074 rsp = wait_command_fd(to, pair[0],
1075 "{ 'execute': 'getfd',"
1076 " 'arguments': { 'fdname': 'fd-mig' }}");
1077 qobject_unref(rsp);
1078 close(pair[0]);
1079
1080 /* Start incoming migration from the 1st socket */
1081 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1082 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1083 qobject_unref(rsp);
1084
1085 /* Send the 2nd socket to the target */
1086 rsp = wait_command_fd(from, pair[1],
1087 "{ 'execute': 'getfd',"
1088 " 'arguments': { 'fdname': 'fd-mig' }}");
1089 qobject_unref(rsp);
1090 close(pair[1]);
1091
1092 /* Start migration to the 2nd socket*/
1093 migrate(from, "fd:fd-mig", "{}");
1094
1095 wait_for_migration_pass(from);
1096
1097 /* 300ms should converge */
8f7798f1 1098 migrate_set_parameter_int(from, "downtime-limit", 300);
24d5588c
YK
1099
1100 if (!got_stop) {
1101 qtest_qmp_eventwait(from, "STOP");
1102 }
1103 qtest_qmp_eventwait(to, "RESUME");
1104
1105 /* Test closing fds */
1106 /* We assume, that QEMU removes named fd from its list,
1107 * so this should fail */
1108 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1109 " 'arguments': { 'fdname': 'fd-mig' }}");
1110 g_assert_true(qdict_haskey(rsp, "error"));
1111 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1112 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1113 qobject_unref(rsp);
1114
1115 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1116 " 'arguments': { 'fdname': 'fd-mig' }}");
1117 g_assert_true(qdict_haskey(rsp, "error"));
1118 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1119 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1120 qobject_unref(rsp);
1121
1122 /* Complete migration */
1123 wait_for_serial("dest_serial");
1124 wait_for_migration_complete(from);
1125 test_migrate_end(from, to, true);
1126}
1127
ea0c6d62
DDAG
1128int main(int argc, char **argv)
1129{
2656bfd9 1130 char template[] = "/tmp/migration-test-XXXXXX";
ea0c6d62
DDAG
1131 int ret;
1132
1133 g_test_init(&argc, &argv, NULL);
1134
1135 if (!ufd_version_check()) {
4848cb3d 1136 return g_test_run();
ea0c6d62
DDAG
1137 }
1138
880b169a
TH
1139 /*
1140 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1141 * is touchy due to race conditions on dirty bits (especially on PPC for
1142 * some reason)
1143 */
1144 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1145 access("/sys/module/kvm_hv", F_OK)) {
1146 g_test_message("Skipping test: kvm_hv not available");
4848cb3d 1147 return g_test_run();
880b169a
TH
1148 }
1149
d254b392
TH
1150 /*
1151 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1152 * there until the problems are resolved
1153 */
1154 if (g_str_equal(qtest_get_arch(), "s390x")) {
1155#if defined(HOST_S390X)
1156 if (access("/dev/kvm", R_OK | W_OK)) {
1157 g_test_message("Skipping test: kvm not available");
4848cb3d 1158 return g_test_run();
d254b392
TH
1159 }
1160#else
1161 g_test_message("Skipping test: Need s390x host to work properly");
4848cb3d 1162 return g_test_run();
d254b392
TH
1163#endif
1164 }
1165
ea0c6d62
DDAG
1166 tmpfs = mkdtemp(template);
1167 if (!tmpfs) {
13ee9e30 1168 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
ea0c6d62
DDAG
1169 }
1170 g_assert(tmpfs);
1171
1172 module_call_init(MODULE_INIT_QOM);
1173
2884100c 1174 qtest_add_func("/migration/postcopy/unix", test_postcopy);
d5f49640 1175 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
4c27486d 1176 qtest_add_func("/migration/deprecated", test_deprecated);
2c9bb297 1177 qtest_add_func("/migration/bad_dest", test_baddest);
2884100c 1178 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
609d3844 1179 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
660a9b68 1180 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
cdf84229 1181 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
24d5588c 1182 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
ea0c6d62
DDAG
1183
1184 ret = g_test_run();
1185
1186 g_assert_cmpint(ret, ==, 0);
1187
1188 ret = rmdir(tmpfs);
1189 if (ret != 0) {
13ee9e30 1190 g_test_message("unable to rmdir: path (%s): %s",
ea0c6d62
DDAG
1191 tmpfs, strerror(errno));
1192 }
1193
1194 return ret;
1195}