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