]> Git Repo - qemu.git/blame - tests/qtest/migration-test.c
multifd: Add multifd-zstd-level parameter
[qemu.git] / tests / qtest / 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"
0b8fa32f 17#include "qemu/module.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"
609d3844
JQ
22#include "qapi/qapi-visit-sockets.h"
23#include "qapi/qobject-input-visitor.h"
24#include "qapi/qobject-output-visitor.h"
ea0c6d62 25
d77799cc 26#include "migration-helpers.h"
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;
346f3dab 34static bool uffd_feature_thread_id;
ea0c6d62
DDAG
35
36#if defined(__linux__)
ea0c6d62
DDAG
37#include <sys/syscall.h>
38#include <sys/vfs.h>
39#endif
40
41#if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
42#include <sys/eventfd.h>
43#include <sys/ioctl.h>
44#include <linux/userfaultfd.h>
45
46static bool ufd_version_check(void)
47{
48 struct uffdio_api api_struct;
49 uint64_t ioctl_mask;
50
e1ae9fb6 51 int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
ea0c6d62
DDAG
52
53 if (ufd == -1) {
54 g_test_message("Skipping test: userfaultfd not available");
55 return false;
56 }
57
58 api_struct.api = UFFD_API;
59 api_struct.features = 0;
60 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
61 g_test_message("Skipping test: UFFDIO_API failed");
62 return false;
63 }
346f3dab 64 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
ea0c6d62
DDAG
65
66 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
67 (__u64)1 << _UFFDIO_UNREGISTER;
68 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
69 g_test_message("Skipping test: Missing userfault feature");
70 return false;
71 }
72
73 return true;
74}
75
76#else
77static bool ufd_version_check(void)
78{
79 g_test_message("Skipping test: Userfault not available (builtdtime)");
80 return false;
81}
82
83#endif
84
85static const char *tmpfs;
86
e51e711b
WH
87/* The boot file modifies memory area in [start_address, end_address)
88 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
ea0c6d62 89 */
d54927ef 90#include "tests/migration/i386/a-b-bootblock.h"
c02b3781 91#include "tests/migration/aarch64/a-b-kernel.h"
5571dc82
TH
92#include "tests/migration/s390x/a-b-bios.h"
93
2785f196 94static void init_bootfile(const char *bootpath, void *content, size_t len)
5571dc82
TH
95{
96 FILE *bootfile = fopen(bootpath, "wb");
5571dc82 97
2785f196 98 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
5571dc82
TH
99 fclose(bootfile);
100}
101
ea0c6d62
DDAG
102/*
103 * Wait for some output in the serial output file,
104 * we get an 'A' followed by an endless string of 'B's
105 * but on the destination we won't have the A.
106 */
107static void wait_for_serial(const char *side)
108{
109 char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
110 FILE *serialfile = fopen(serialpath, "r");
aaf89c8a 111 const char *arch = qtest_get_arch();
112 int started = (strcmp(side, "src_serial") == 0 &&
113 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62 114
e2dd21e5 115 g_free(serialpath);
ea0c6d62
DDAG
116 do {
117 int readvalue = fgetc(serialfile);
118
aaf89c8a 119 if (!started) {
120 /* SLOF prints its banner before starting test,
121 * to ignore it, mark the start of the test with '_',
122 * ignore all characters until this marker
123 */
124 switch (readvalue) {
125 case '_':
126 started = 1;
127 break;
128 case EOF:
129 fseek(serialfile, 0, SEEK_SET);
130 usleep(1000);
131 break;
132 }
133 continue;
134 }
ea0c6d62
DDAG
135 switch (readvalue) {
136 case 'A':
137 /* Fine */
138 break;
139
140 case 'B':
141 /* It's alive! */
142 fclose(serialfile);
ea0c6d62
DDAG
143 return;
144
145 case EOF:
aaf89c8a 146 started = (strcmp(side, "src_serial") == 0 &&
147 strcmp(arch, "ppc64") == 0) ? 0 : 1;
ea0c6d62
DDAG
148 fseek(serialfile, 0, SEEK_SET);
149 usleep(1000);
150 break;
151
152 default:
153 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
154 g_assert_not_reached();
155 }
156 } while (true);
157}
158
ea0c6d62
DDAG
159/*
160 * It's tricky to use qemu's migration event capability with qtest,
161 * events suddenly appearing confuse the qmp()/hmp() responses.
ea0c6d62
DDAG
162 */
163
660a9b68 164static int64_t read_ram_property_int(QTestState *who, const char *property)
ea0c6d62 165{
2f7074c6 166 QDict *rsp_return, *rsp_ram;
660a9b68 167 int64_t result;
ea0c6d62 168
2f7074c6 169 rsp_return = migrate_query(who);
ea0c6d62
DDAG
170 if (!qdict_haskey(rsp_return, "ram")) {
171 /* Still in setup */
172 result = 0;
173 } else {
174 rsp_ram = qdict_get_qdict(rsp_return, "ram");
660a9b68 175 result = qdict_get_try_int(rsp_ram, property, 0);
ea0c6d62 176 }
2f7074c6 177 qobject_unref(rsp_return);
ea0c6d62
DDAG
178 return result;
179}
180
8c51642b
YK
181static int64_t read_migrate_property_int(QTestState *who, const char *property)
182{
183 QDict *rsp_return;
184 int64_t result;
185
186 rsp_return = migrate_query(who);
187 result = qdict_get_try_int(rsp_return, property, 0);
188 qobject_unref(rsp_return);
189 return result;
190}
191
660a9b68
YK
192static uint64_t get_migration_pass(QTestState *who)
193{
194 return read_ram_property_int(who, "dirty-sync-count");
195}
196
346f3dab
AP
197static void read_blocktime(QTestState *who)
198{
2f7074c6 199 QDict *rsp_return;
346f3dab 200
2f7074c6 201 rsp_return = migrate_query(who);
346f3dab 202 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
2f7074c6 203 qobject_unref(rsp_return);
346f3dab
AP
204}
205
863e27a8 206static void wait_for_migration_pass(QTestState *who)
ea0c6d62 207{
863e27a8 208 uint64_t initial_pass = get_migration_pass(who);
ea0c6d62
DDAG
209 uint64_t pass;
210
211 /* Wait for the 1st sync */
6a7724e9
JQ
212 while (!got_stop && !initial_pass) {
213 usleep(1000);
863e27a8 214 initial_pass = get_migration_pass(who);
6a7724e9 215 }
ea0c6d62
DDAG
216
217 do {
6a7724e9 218 usleep(1000);
863e27a8 219 pass = get_migration_pass(who);
ea0c6d62
DDAG
220 } while (pass == initial_pass && !got_stop);
221}
222
7195a871 223static void check_guests_ram(QTestState *who)
ea0c6d62
DDAG
224{
225 /* Our ASM test will have been incrementing one byte from each page from
e51e711b
WH
226 * start_address to < end_address in order. This gives us a constraint
227 * that any page's byte should be equal or less than the previous pages
228 * byte (mod 256); and they should all be equal except for one transition
229 * at the point where we meet the incrementer. (We're running this with
230 * the guest stopped).
ea0c6d62
DDAG
231 */
232 unsigned address;
233 uint8_t first_byte;
234 uint8_t last_byte;
235 bool hit_edge = false;
601b5575 236 int bad = 0;
ea0c6d62 237
7195a871 238 qtest_memread(who, start_address, &first_byte, 1);
ea0c6d62
DDAG
239 last_byte = first_byte;
240
e51e711b
WH
241 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
242 address += TEST_MEM_PAGE_SIZE)
ea0c6d62
DDAG
243 {
244 uint8_t b;
7195a871 245 qtest_memread(who, address, &b, 1);
ea0c6d62
DDAG
246 if (b != last_byte) {
247 if (((b + 1) % 256) == last_byte && !hit_edge) {
248 /* This is OK, the guest stopped at the point of
249 * incrementing the previous page but didn't get
250 * to us yet.
251 */
252 hit_edge = true;
829db8b4 253 last_byte = b;
ea0c6d62 254 } else {
601b5575
AB
255 bad++;
256 if (bad <= 10) {
257 fprintf(stderr, "Memory content inconsistency at %x"
258 " first_byte = %x last_byte = %x current = %x"
259 " hit_edge = %x\n",
260 address, first_byte, last_byte, b, hit_edge);
261 }
ea0c6d62
DDAG
262 }
263 }
ea0c6d62 264 }
601b5575
AB
265 if (bad >= 10) {
266 fprintf(stderr, "and in another %d pages", bad - 10);
267 }
268 g_assert(bad == 0);
ea0c6d62
DDAG
269}
270
271static void cleanup(const char *filename)
272{
273 char *path = g_strdup_printf("%s/%s", tmpfs, filename);
274
275 unlink(path);
e2dd21e5 276 g_free(path);
ea0c6d62
DDAG
277}
278
609d3844
JQ
279static char *SocketAddress_to_str(SocketAddress *addr)
280{
281 switch (addr->type) {
282 case SOCKET_ADDRESS_TYPE_INET:
283 return g_strdup_printf("tcp:%s:%s",
284 addr->u.inet.host,
285 addr->u.inet.port);
286 case SOCKET_ADDRESS_TYPE_UNIX:
287 return g_strdup_printf("unix:%s",
288 addr->u.q_unix.path);
289 case SOCKET_ADDRESS_TYPE_FD:
290 return g_strdup_printf("fd:%s", addr->u.fd.str);
291 case SOCKET_ADDRESS_TYPE_VSOCK:
292 return g_strdup_printf("tcp:%s:%s",
293 addr->u.vsock.cid,
294 addr->u.vsock.port);
295 default:
296 return g_strdup("unknown address type");
297 }
298}
299
300static char *migrate_get_socket_address(QTestState *who, const char *parameter)
301{
302 QDict *rsp;
303 char *result;
304 Error *local_err = NULL;
305 SocketAddressList *addrs;
306 Visitor *iv = NULL;
307 QObject *object;
308
309 rsp = migrate_query(who);
310 object = qdict_get(rsp, parameter);
311
312 iv = qobject_input_visitor_new(object);
313 visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
1e25879e 314 visit_free(iv);
609d3844
JQ
315
316 /* we are only using a single address */
1e25879e 317 result = SocketAddress_to_str(addrs->value);
609d3844
JQ
318
319 qapi_free_SocketAddressList(addrs);
320 qobject_unref(rsp);
321 return result;
322}
323
8f7798f1
JQ
324static long long migrate_get_parameter_int(QTestState *who,
325 const char *parameter)
609d3844
JQ
326{
327 QDict *rsp;
328 long long result;
329
330 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
331 result = qdict_get_int(rsp, parameter);
332 qobject_unref(rsp);
333 return result;
334}
335
8f7798f1
JQ
336static void migrate_check_parameter_int(QTestState *who, const char *parameter,
337 long long value)
56b4a42a 338{
609d3844 339 long long result;
56b4a42a 340
8f7798f1 341 result = migrate_get_parameter_int(who, parameter);
609d3844 342 g_assert_cmpint(result, ==, value);
56b4a42a
JQ
343}
344
8f7798f1
JQ
345static void migrate_set_parameter_int(QTestState *who, const char *parameter,
346 long long value)
d62fbe60
JQ
347{
348 QDict *rsp;
d62fbe60 349
c44a56d8
MA
350 rsp = qtest_qmp(who,
351 "{ 'execute': 'migrate-set-parameters',"
352 "'arguments': { %s: %lld } }",
353 parameter, value);
d62fbe60 354 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 355 qobject_unref(rsp);
8f7798f1 356 migrate_check_parameter_int(who, parameter, value);
d62fbe60
JQ
357}
358
6a22c544
JQ
359static char *migrate_get_parameter_str(QTestState *who,
360 const char *parameter)
361{
362 QDict *rsp;
363 char *result;
364
365 rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
366 result = g_strdup(qdict_get_str(rsp, parameter));
367 qobject_unref(rsp);
368 return result;
369}
370
371static void migrate_check_parameter_str(QTestState *who, const char *parameter,
372 const char *value)
373{
374 char *result;
375
376 result = migrate_get_parameter_str(who, parameter);
377 g_assert_cmpstr(result, ==, value);
378 g_free(result);
379}
380
6a22c544
JQ
381static void migrate_set_parameter_str(QTestState *who, const char *parameter,
382 const char *value)
383{
384 QDict *rsp;
385
386 rsp = qtest_qmp(who,
387 "{ 'execute': 'migrate-set-parameters',"
388 "'arguments': { %s: %s } }",
389 parameter, value);
390 g_assert(qdict_haskey(rsp, "return"));
391 qobject_unref(rsp);
392 migrate_check_parameter_str(who, parameter, value);
393}
394
d5f49640
PX
395static void migrate_pause(QTestState *who)
396{
397 QDict *rsp;
398
399 rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
d5f49640
PX
400 qobject_unref(rsp);
401}
402
8c51642b
YK
403static void migrate_continue(QTestState *who, const char *state)
404{
405 QDict *rsp;
406
407 rsp = wait_command(who,
408 "{ 'execute': 'migrate-continue',"
409 " 'arguments': { 'state': %s } }",
410 state);
411 qobject_unref(rsp);
412}
413
d5f49640
PX
414static void migrate_recover(QTestState *who, const char *uri)
415{
416 QDict *rsp;
d5f49640 417
b7281c69
MA
418 rsp = wait_command(who,
419 "{ 'execute': 'migrate-recover', "
420 " 'id': 'recover-cmd', "
421 " 'arguments': { 'uri': %s } }",
422 uri);
d5f49640
PX
423 qobject_unref(rsp);
424}
425
d795f474
JQ
426static void migrate_cancel(QTestState *who)
427{
428 QDict *rsp;
429
430 rsp = wait_command(who, "{ 'execute': 'migrate_cancel' }");
431 qobject_unref(rsp);
432}
433
d62fbe60 434static void migrate_set_capability(QTestState *who, const char *capability,
c44a56d8 435 bool value)
d62fbe60
JQ
436{
437 QDict *rsp;
c44a56d8
MA
438
439 rsp = qtest_qmp(who,
440 "{ 'execute': 'migrate-set-capabilities',"
441 "'arguments': { "
442 "'capabilities': [ { "
443 "'capability': %s, 'state': %i } ] } }",
444 capability, value);
d62fbe60 445 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 446 qobject_unref(rsp);
d62fbe60
JQ
447}
448
d131662a 449static void migrate_postcopy_start(QTestState *from, QTestState *to)
eb665d7d
JQ
450{
451 QDict *rsp;
452
d131662a 453 rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
cb3e7f08 454 qobject_unref(rsp);
d131662a
PX
455
456 if (!got_stop) {
457 qtest_qmp_eventwait(from, "STOP");
458 }
459
460 qtest_qmp_eventwait(to, "RESUME");
eb665d7d
JQ
461}
462
5d3b575d
JQ
463typedef struct {
464 bool hide_stderr;
465 bool use_shmem;
d795f474
JQ
466 /* only launch the target process */
467 bool only_target;
5d3b575d
JQ
468 char *opts_source;
469 char *opts_target;
470} MigrateStart;
471
472static MigrateStart *migrate_start_new(void)
473{
474 MigrateStart *args = g_new0(MigrateStart, 1);
475
476 args->opts_source = g_strdup("");
477 args->opts_target = g_strdup("");
478 return args;
479}
480
481static void migrate_start_destroy(MigrateStart *args)
482{
483 g_free(args->opts_source);
484 g_free(args->opts_target);
485 g_free(args);
486}
487
5fd4a9c9 488static int test_migrate_start(QTestState **from, QTestState **to,
5d3b575d 489 const char *uri, MigrateStart *args)
ea0c6d62 490{
68d95609 491 gchar *arch_source, *arch_target;
8443415f 492 gchar *cmd_source, *cmd_target;
1b023718 493 const gchar *ignore_stderr;
660a9b68 494 char *bootpath = NULL;
3ed375e7
JQ
495 char *shmem_opts;
496 char *shmem_path;
aaf89c8a 497 const char *arch = qtest_get_arch();
6f6e1698 498 const char *machine_opts = NULL;
7b6d44cb 499 const char *memory_size;
e022d473 500 int ret = 0;
ea0c6d62 501
5d3b575d 502 if (args->use_shmem) {
660a9b68
YK
503 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
504 g_test_skip("/dev/shm is not supported");
e022d473
PN
505 ret = -1;
506 goto out;
660a9b68 507 }
660a9b68 508 }
ea0c6d62 509
660a9b68
YK
510 got_stop = false;
511 bootpath = g_strdup_printf("%s/bootsect", tmpfs);
aaf89c8a 512 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
2785f196
PM
513 /* the assembled x86 boot sector should be exactly one sector large */
514 assert(sizeof(x86_bootsect) == 512);
515 init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
7b6d44cb 516 memory_size = "150M";
68d95609
JQ
517 arch_source = g_strdup_printf("-drive file=%s,format=raw", bootpath);
518 arch_target = g_strdup(arch_source);
e51e711b
WH
519 start_address = X86_TEST_MEM_START;
520 end_address = X86_TEST_MEM_END;
5571dc82 521 } else if (g_str_equal(arch, "s390x")) {
2785f196 522 init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
7b6d44cb 523 memory_size = "128M";
68d95609
JQ
524 arch_source = g_strdup_printf("-bios %s", bootpath);
525 arch_target = g_strdup(arch_source);
5571dc82
TH
526 start_address = S390_TEST_MEM_START;
527 end_address = S390_TEST_MEM_END;
aaf89c8a 528 } else if (strcmp(arch, "ppc64") == 0) {
6f6e1698 529 machine_opts = "vsmt=8";
7b6d44cb 530 memory_size = "256M";
16c5c692
LV
531 start_address = PPC_TEST_MEM_START;
532 end_address = PPC_TEST_MEM_END;
68d95609
JQ
533 arch_source = g_strdup_printf("-nodefaults "
534 "-prom-env 'use-nvramrc?=true' -prom-env "
535 "'nvramrc=hex .\" _\" begin %x %x "
536 "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
537 "until'", end_address, start_address);
538 arch_target = g_strdup("");
c02b3781 539 } else if (strcmp(arch, "aarch64") == 0) {
2785f196 540 init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
6f6e1698 541 machine_opts = "virt,gic-version=max";
7b6d44cb 542 memory_size = "150M";
68d95609
JQ
543 arch_source = g_strdup_printf("-cpu max "
544 "-kernel %s",
545 bootpath);
546 arch_target = g_strdup(arch_source);
c02b3781
WH
547 start_address = ARM_TEST_MEM_START;
548 end_address = ARM_TEST_MEM_END;
549
550 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
aaf89c8a 551 } else {
552 g_assert_not_reached();
553 }
554
e2dd21e5
MAL
555 g_free(bootpath);
556
5d3b575d 557 if (args->hide_stderr) {
1b023718
JQ
558 ignore_stderr = "2>/dev/null";
559 } else {
560 ignore_stderr = "";
f96d6651
DDAG
561 }
562
5d3b575d 563 if (args->use_shmem) {
3ed375e7
JQ
564 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
565 shmem_opts = g_strdup_printf(
566 "-object memory-backend-file,id=mem0,size=%s"
567 ",mem-path=%s,share=on -numa node,memdev=mem0",
568 memory_size, shmem_path);
569 } else {
570 shmem_path = NULL;
571 shmem_opts = g_strdup("");
572 }
573
6f6e1698 574 cmd_source = g_strdup_printf("-accel kvm -accel tcg%s%s "
d6b43267 575 "-name source,debug-threads=on "
7b6d44cb 576 "-m %s "
cd496731 577 "-serial file:%s/src_serial "
3ed375e7 578 "%s %s %s %s",
6f6e1698
PB
579 machine_opts ? " -machine " : "",
580 machine_opts ? machine_opts : "",
cd496731 581 memory_size, tmpfs,
5d3b575d 582 arch_source, shmem_opts, args->opts_source,
68d95609
JQ
583 ignore_stderr);
584 g_free(arch_source);
d795f474
JQ
585 if (!args->only_target) {
586 *from = qtest_init(cmd_source);
587 }
8443415f 588 g_free(cmd_source);
aaf89c8a 589
6f6e1698 590 cmd_target = g_strdup_printf("-accel kvm -accel tcg%s%s "
d6b43267 591 "-name target,debug-threads=on "
7b6d44cb 592 "-m %s "
c5f40ff9 593 "-serial file:%s/dest_serial "
cd496731 594 "-incoming %s "
3ed375e7 595 "%s %s %s %s",
6f6e1698
PB
596 machine_opts ? " -machine " : "",
597 machine_opts ? machine_opts : "",
cd496731 598 memory_size, tmpfs, uri,
5d3b575d
JQ
599 arch_target, shmem_opts,
600 args->opts_target, ignore_stderr);
68d95609 601 g_free(arch_target);
8443415f
JQ
602 *to = qtest_init(cmd_target);
603 g_free(cmd_target);
660a9b68 604
3ed375e7 605 g_free(shmem_opts);
660a9b68
YK
606 /*
607 * Remove shmem file immediately to avoid memory leak in test failed case.
608 * It's valid becase QEMU has already opened this file
609 */
5d3b575d 610 if (args->use_shmem) {
660a9b68
YK
611 unlink(shmem_path);
612 g_free(shmem_path);
613 }
614
e022d473 615out:
5d3b575d 616 migrate_start_destroy(args);
e022d473 617 return ret;
7195a871
JQ
618}
619
2c9bb297 620static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
7195a871
JQ
621{
622 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
623
624 qtest_quit(from);
625
2c9bb297
DDAG
626 if (test_dest) {
627 qtest_memread(to, start_address, &dest_byte_a, 1);
7195a871 628
2c9bb297
DDAG
629 /* Destination still running, wait for a byte to change */
630 do {
631 qtest_memread(to, start_address, &dest_byte_b, 1);
632 usleep(1000 * 10);
633 } while (dest_byte_a == dest_byte_b);
634
635 qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
7195a871 636
2c9bb297
DDAG
637 /* With it stopped, check nothing changes */
638 qtest_memread(to, start_address, &dest_byte_c, 1);
639 usleep(1000 * 200);
640 qtest_memread(to, start_address, &dest_byte_d, 1);
641 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
7195a871 642
2c9bb297
DDAG
643 check_guests_ram(to);
644 }
7195a871
JQ
645
646 qtest_quit(to);
647
648 cleanup("bootsect");
649 cleanup("migsocket");
650 cleanup("src_serial");
651 cleanup("dest_serial");
652}
653
4c27486d
JQ
654static void deprecated_set_downtime(QTestState *who, const double value)
655{
656 QDict *rsp;
4c27486d 657
015715f5
MA
658 rsp = qtest_qmp(who,
659 "{ 'execute': 'migrate_set_downtime',"
660 " 'arguments': { 'value': %f } }", value);
4c27486d 661 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 662 qobject_unref(rsp);
8f7798f1 663 migrate_check_parameter_int(who, "downtime-limit", value * 1000);
4c27486d
JQ
664}
665
c44a56d8 666static void deprecated_set_speed(QTestState *who, long long value)
4c27486d
JQ
667{
668 QDict *rsp;
4c27486d 669
c44a56d8
MA
670 rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
671 "'arguments': { 'value': %lld } }", value);
4c27486d 672 g_assert(qdict_haskey(rsp, "return"));
cb3e7f08 673 qobject_unref(rsp);
8f7798f1 674 migrate_check_parameter_int(who, "max-bandwidth", value);
4c27486d
JQ
675}
676
cdf84229
JQ
677static void deprecated_set_cache_size(QTestState *who, long long value)
678{
679 QDict *rsp;
680
681 rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
682 "'arguments': { 'value': %lld } }", value);
683 g_assert(qdict_haskey(rsp, "return"));
684 qobject_unref(rsp);
8f7798f1 685 migrate_check_parameter_int(who, "xbzrle-cache-size", value);
cdf84229
JQ
686}
687
4c27486d
JQ
688static void test_deprecated(void)
689{
690 QTestState *from;
691
a2726593 692 from = qtest_init("-machine none");
4c27486d
JQ
693
694 deprecated_set_downtime(from, 0.12345);
c44a56d8 695 deprecated_set_speed(from, 12345);
cdf84229 696 deprecated_set_cache_size(from, 4096);
4c27486d
JQ
697
698 qtest_quit(from);
699}
700
d131662a 701static int migrate_postcopy_prepare(QTestState **from_ptr,
5d3b575d
JQ
702 QTestState **to_ptr,
703 MigrateStart *args)
7195a871
JQ
704{
705 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
863e27a8 706 QTestState *from, *to;
7195a871 707
5d3b575d 708 if (test_migrate_start(&from, &to, uri, args)) {
d131662a 709 return -1;
5fd4a9c9 710 }
ea0c6d62 711
c44a56d8
MA
712 migrate_set_capability(from, "postcopy-ram", true);
713 migrate_set_capability(to, "postcopy-ram", true);
714 migrate_set_capability(to, "postcopy-blocktime", true);
ea0c6d62
DDAG
715
716 /* We want to pick a speed slow enough that the test completes
717 * quickly, but that it doesn't complete precopy even on a slow
718 * machine, so also set the downtime.
719 */
513aa2c6 720 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
8f7798f1 721 migrate_set_parameter_int(from, "downtime-limit", 1);
ea0c6d62
DDAG
722
723 /* Wait for the first serial output from the source */
724 wait_for_serial("src_serial");
725
d77799cc 726 migrate_qmp(from, uri, "{}");
d131662a 727 g_free(uri);
ea0c6d62 728
863e27a8 729 wait_for_migration_pass(from);
ea0c6d62 730
d131662a
PX
731 *from_ptr = from;
732 *to_ptr = to;
ea0c6d62 733
d131662a
PX
734 return 0;
735}
ea0c6d62 736
d131662a
PX
737static void migrate_postcopy_complete(QTestState *from, QTestState *to)
738{
739 wait_for_migration_complete(from);
ea0c6d62 740
d131662a 741 /* Make sure we get at least one "B" on destination */
ea0c6d62 742 wait_for_serial("dest_serial");
ea0c6d62 743
346f3dab
AP
744 if (uffd_feature_thread_id) {
745 read_blocktime(to);
746 }
ea0c6d62 747
2c9bb297
DDAG
748 test_migrate_end(from, to, true);
749}
750
d131662a
PX
751static void test_postcopy(void)
752{
5d3b575d 753 MigrateStart *args = migrate_start_new();
d131662a
PX
754 QTestState *from, *to;
755
5d3b575d 756 if (migrate_postcopy_prepare(&from, &to, args)) {
d131662a
PX
757 return;
758 }
759 migrate_postcopy_start(from, to);
760 migrate_postcopy_complete(from, to);
761}
762
d5f49640
PX
763static void test_postcopy_recovery(void)
764{
5d3b575d 765 MigrateStart *args = migrate_start_new();
d5f49640
PX
766 QTestState *from, *to;
767 char *uri;
768
5d3b575d
JQ
769 args->hide_stderr = true;
770
771 if (migrate_postcopy_prepare(&from, &to, args)) {
d5f49640
PX
772 return;
773 }
774
775 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
8f7798f1 776 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
d5f49640
PX
777
778 /* Now we start the postcopy */
779 migrate_postcopy_start(from, to);
780
781 /*
782 * Wait until postcopy is really started; we can only run the
783 * migrate-pause command during a postcopy
784 */
8c51642b 785 wait_for_migration_status(from, "postcopy-active", NULL);
d5f49640
PX
786
787 /*
788 * Manually stop the postcopy migration. This emulates a network
789 * failure with the migration socket
790 */
791 migrate_pause(from);
792
793 /*
794 * Wait for destination side to reach postcopy-paused state. The
795 * migrate-recover command can only succeed if destination machine
796 * is in the paused state
797 */
e15310ea
DDAG
798 wait_for_migration_status(to, "postcopy-paused",
799 (const char * []) { "failed", "active",
800 "completed", NULL });
d5f49640
PX
801
802 /*
803 * Create a new socket to emulate a new channel that is different
804 * from the broken migration channel; tell the destination to
805 * listen to the new port
806 */
807 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
808 migrate_recover(to, uri);
809
810 /*
811 * Try to rebuild the migration channel using the resume flag and
812 * the newly created channel
813 */
e15310ea
DDAG
814 wait_for_migration_status(from, "postcopy-paused",
815 (const char * []) { "failed", "active",
816 "completed", NULL });
d77799cc 817 migrate_qmp(from, uri, "{'resume': true}");
d5f49640
PX
818 g_free(uri);
819
820 /* Restore the postcopy bandwidth to unlimited */
8f7798f1 821 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
d5f49640
PX
822
823 migrate_postcopy_complete(from, to);
824}
825
3af31a34
YK
826static void test_baddest(void)
827{
5d3b575d 828 MigrateStart *args = migrate_start_new();
3af31a34 829 QTestState *from, *to;
2c9bb297 830
5d3b575d
JQ
831 args->hide_stderr = true;
832
833 if (test_migrate_start(&from, &to, "tcp:0:0", args)) {
3af31a34
YK
834 return;
835 }
d77799cc 836 migrate_qmp(from, "tcp:0:0", "{}");
3af31a34 837 wait_for_migration_fail(from, false);
2c9bb297 838 test_migrate_end(from, to, false);
ea0c6d62
DDAG
839}
840
2884100c
JQ
841static void test_precopy_unix(void)
842{
843 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
5d3b575d 844 MigrateStart *args = migrate_start_new();
2884100c
JQ
845 QTestState *from, *to;
846
5d3b575d 847 if (test_migrate_start(&from, &to, uri, args)) {
5fd4a9c9
DDAG
848 return;
849 }
2884100c
JQ
850
851 /* We want to pick a speed slow enough that the test completes
852 * quickly, but that it doesn't complete precopy even on a slow
853 * machine, so also set the downtime.
854 */
855 /* 1 ms should make it not converge*/
8f7798f1 856 migrate_set_parameter_int(from, "downtime-limit", 1);
2884100c 857 /* 1GB/s */
8f7798f1 858 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
2884100c
JQ
859
860 /* Wait for the first serial output from the source */
861 wait_for_serial("src_serial");
862
d77799cc 863 migrate_qmp(from, uri, "{}");
2884100c
JQ
864
865 wait_for_migration_pass(from);
866
867 /* 300 ms should converge */
8f7798f1 868 migrate_set_parameter_int(from, "downtime-limit", 300);
2884100c
JQ
869
870 if (!got_stop) {
871 qtest_qmp_eventwait(from, "STOP");
872 }
873
874 qtest_qmp_eventwait(to, "RESUME");
875
876 wait_for_serial("dest_serial");
877 wait_for_migration_complete(from);
878
879 test_migrate_end(from, to, true);
880 g_free(uri);
881}
882
660a9b68
YK
883#if 0
884/* Currently upset on aarch64 TCG */
885static void test_ignore_shared(void)
886{
887 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
888 QTestState *from, *to;
889
3af31a34 890 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
660a9b68
YK
891 return;
892 }
893
894 migrate_set_capability(from, "x-ignore-shared", true);
895 migrate_set_capability(to, "x-ignore-shared", true);
896
897 /* Wait for the first serial output from the source */
898 wait_for_serial("src_serial");
899
d77799cc 900 migrate_qmp(from, uri, "{}");
660a9b68
YK
901
902 wait_for_migration_pass(from);
903
904 if (!got_stop) {
905 qtest_qmp_eventwait(from, "STOP");
906 }
907
908 qtest_qmp_eventwait(to, "RESUME");
909
910 wait_for_serial("dest_serial");
911 wait_for_migration_complete(from);
912
913 /* Check whether shared RAM has been really skipped */
914 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
915
916 test_migrate_end(from, to, true);
917 g_free(uri);
918}
919#endif
920
cdf84229
JQ
921static void test_xbzrle(const char *uri)
922{
5d3b575d 923 MigrateStart *args = migrate_start_new();
cdf84229
JQ
924 QTestState *from, *to;
925
5d3b575d 926 if (test_migrate_start(&from, &to, uri, args)) {
cdf84229
JQ
927 return;
928 }
929
930 /*
931 * We want to pick a speed slow enough that the test completes
932 * quickly, but that it doesn't complete precopy even on a slow
933 * machine, so also set the downtime.
934 */
935 /* 1 ms should make it not converge*/
8f7798f1 936 migrate_set_parameter_int(from, "downtime-limit", 1);
cdf84229 937 /* 1GB/s */
8f7798f1 938 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
cdf84229 939
8f7798f1 940 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
cdf84229
JQ
941
942 migrate_set_capability(from, "xbzrle", "true");
943 migrate_set_capability(to, "xbzrle", "true");
944 /* Wait for the first serial output from the source */
945 wait_for_serial("src_serial");
946
d77799cc 947 migrate_qmp(from, uri, "{}");
cdf84229
JQ
948
949 wait_for_migration_pass(from);
950
951 /* 300ms should converge */
8f7798f1 952 migrate_set_parameter_int(from, "downtime-limit", 300);
cdf84229
JQ
953
954 if (!got_stop) {
955 qtest_qmp_eventwait(from, "STOP");
956 }
957 qtest_qmp_eventwait(to, "RESUME");
958
959 wait_for_serial("dest_serial");
960 wait_for_migration_complete(from);
961
962 test_migrate_end(from, to, true);
963}
964
965static void test_xbzrle_unix(void)
966{
967 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
968
969 test_xbzrle(uri);
970 g_free(uri);
971}
972
609d3844
JQ
973static void test_precopy_tcp(void)
974{
5d3b575d 975 MigrateStart *args = migrate_start_new();
609d3844
JQ
976 char *uri;
977 QTestState *from, *to;
978
5d3b575d 979 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", args)) {
609d3844
JQ
980 return;
981 }
982
983 /*
984 * We want to pick a speed slow enough that the test completes
985 * quickly, but that it doesn't complete precopy even on a slow
986 * machine, so also set the downtime.
987 */
988 /* 1 ms should make it not converge*/
8f7798f1 989 migrate_set_parameter_int(from, "downtime-limit", 1);
609d3844 990 /* 1GB/s */
8f7798f1 991 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
609d3844
JQ
992
993 /* Wait for the first serial output from the source */
994 wait_for_serial("src_serial");
995
996 uri = migrate_get_socket_address(to, "socket-address");
997
d77799cc 998 migrate_qmp(from, uri, "{}");
609d3844
JQ
999
1000 wait_for_migration_pass(from);
1001
1002 /* 300ms should converge */
8f7798f1 1003 migrate_set_parameter_int(from, "downtime-limit", 300);
609d3844
JQ
1004
1005 if (!got_stop) {
1006 qtest_qmp_eventwait(from, "STOP");
1007 }
1008 qtest_qmp_eventwait(to, "RESUME");
1009
1010 wait_for_serial("dest_serial");
1011 wait_for_migration_complete(from);
1012
1013 test_migrate_end(from, to, true);
1014 g_free(uri);
1015}
1016
24d5588c
YK
1017static void test_migrate_fd_proto(void)
1018{
5d3b575d 1019 MigrateStart *args = migrate_start_new();
24d5588c
YK
1020 QTestState *from, *to;
1021 int ret;
1022 int pair[2];
1023 QDict *rsp;
1024 const char *error_desc;
1025
5d3b575d 1026 if (test_migrate_start(&from, &to, "defer", args)) {
24d5588c
YK
1027 return;
1028 }
1029
1030 /*
1031 * We want to pick a speed slow enough that the test completes
1032 * quickly, but that it doesn't complete precopy even on a slow
1033 * machine, so also set the downtime.
1034 */
1035 /* 1 ms should make it not converge */
8f7798f1 1036 migrate_set_parameter_int(from, "downtime-limit", 1);
24d5588c 1037 /* 1GB/s */
8f7798f1 1038 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
24d5588c
YK
1039
1040 /* Wait for the first serial output from the source */
1041 wait_for_serial("src_serial");
1042
1043 /* Create two connected sockets for migration */
1044 ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
1045 g_assert_cmpint(ret, ==, 0);
1046
1047 /* Send the 1st socket to the target */
1048 rsp = wait_command_fd(to, pair[0],
1049 "{ 'execute': 'getfd',"
1050 " 'arguments': { 'fdname': 'fd-mig' }}");
1051 qobject_unref(rsp);
1052 close(pair[0]);
1053
1054 /* Start incoming migration from the 1st socket */
1055 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1056 " 'arguments': { 'uri': 'fd:fd-mig' }}");
1057 qobject_unref(rsp);
1058
1059 /* Send the 2nd socket to the target */
1060 rsp = wait_command_fd(from, pair[1],
1061 "{ 'execute': 'getfd',"
1062 " 'arguments': { 'fdname': 'fd-mig' }}");
1063 qobject_unref(rsp);
1064 close(pair[1]);
1065
1066 /* Start migration to the 2nd socket*/
d77799cc 1067 migrate_qmp(from, "fd:fd-mig", "{}");
24d5588c
YK
1068
1069 wait_for_migration_pass(from);
1070
1071 /* 300ms should converge */
8f7798f1 1072 migrate_set_parameter_int(from, "downtime-limit", 300);
24d5588c
YK
1073
1074 if (!got_stop) {
1075 qtest_qmp_eventwait(from, "STOP");
1076 }
1077 qtest_qmp_eventwait(to, "RESUME");
1078
1079 /* Test closing fds */
1080 /* We assume, that QEMU removes named fd from its list,
1081 * so this should fail */
1082 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
1083 " 'arguments': { 'fdname': 'fd-mig' }}");
1084 g_assert_true(qdict_haskey(rsp, "error"));
1085 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1086 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1087 qobject_unref(rsp);
1088
1089 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
1090 " 'arguments': { 'fdname': 'fd-mig' }}");
1091 g_assert_true(qdict_haskey(rsp, "error"));
1092 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
1093 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
1094 qobject_unref(rsp);
1095
1096 /* Complete migration */
1097 wait_for_serial("dest_serial");
1098 wait_for_migration_complete(from);
1099 test_migrate_end(from, to, true);
1100}
1101
5d3b575d 1102static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
3af31a34
YK
1103{
1104 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1105 QTestState *from, *to;
1106
5d3b575d 1107 if (test_migrate_start(&from, &to, uri, args)) {
3af31a34
YK
1108 return;
1109 }
1110
1111 /*
1112 * UUID validation is at the begin of migration. So, the main process of
1113 * migration is not interesting for us here. Thus, set huge downtime for
1114 * very fast migration.
1115 */
1116 migrate_set_parameter_int(from, "downtime-limit", 1000000);
1117 migrate_set_capability(from, "validate-uuid", true);
1118
1119 /* Wait for the first serial output from the source */
1120 wait_for_serial("src_serial");
1121
d77799cc 1122 migrate_qmp(from, uri, "{}");
3af31a34
YK
1123
1124 if (should_fail) {
1125 qtest_set_expected_status(to, 1);
1126 wait_for_migration_fail(from, true);
1127 } else {
1128 wait_for_migration_complete(from);
1129 }
1130
1131 test_migrate_end(from, to, false);
1132 g_free(uri);
1133}
1134
1135static void test_validate_uuid(void)
1136{
5d3b575d
JQ
1137 MigrateStart *args = migrate_start_new();
1138
e022d473
PN
1139 g_free(args->opts_source);
1140 g_free(args->opts_target);
5d3b575d
JQ
1141 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1142 args->opts_target = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1143 do_test_validate_uuid(args, false);
3af31a34
YK
1144}
1145
1146static void test_validate_uuid_error(void)
1147{
5d3b575d
JQ
1148 MigrateStart *args = migrate_start_new();
1149
e022d473
PN
1150 g_free(args->opts_source);
1151 g_free(args->opts_target);
5d3b575d
JQ
1152 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1153 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1154 args->hide_stderr = true;
1155 do_test_validate_uuid(args, true);
3af31a34
YK
1156}
1157
1158static void test_validate_uuid_src_not_set(void)
1159{
5d3b575d
JQ
1160 MigrateStart *args = migrate_start_new();
1161
e022d473 1162 g_free(args->opts_target);
5d3b575d
JQ
1163 args->opts_target = g_strdup("-uuid 22222222-2222-2222-2222-222222222222");
1164 args->hide_stderr = true;
1165 do_test_validate_uuid(args, false);
3af31a34
YK
1166}
1167
1168static void test_validate_uuid_dst_not_set(void)
1169{
5d3b575d
JQ
1170 MigrateStart *args = migrate_start_new();
1171
e022d473 1172 g_free(args->opts_source);
5d3b575d
JQ
1173 args->opts_source = g_strdup("-uuid 11111111-1111-1111-1111-111111111111");
1174 args->hide_stderr = true;
1175 do_test_validate_uuid(args, false);
3af31a34
YK
1176}
1177
8c51642b
YK
1178static void test_migrate_auto_converge(void)
1179{
1180 char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
5d3b575d 1181 MigrateStart *args = migrate_start_new();
8c51642b
YK
1182 QTestState *from, *to;
1183 int64_t remaining, percentage;
1184
1185 /*
1186 * We want the test to be stable and as fast as possible.
1187 * E.g., with 1Gb/s bandwith migration may pass without throttling,
1188 * so we need to decrease a bandwidth.
1189 */
1190 const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
1191 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
1192 const int64_t downtime_limit = 250; /* 250ms */
1193 /*
1194 * We migrate through unix-socket (> 500Mb/s).
1195 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
1196 * So, we can predict expected_threshold
1197 */
1198 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
1199
5d3b575d 1200 if (test_migrate_start(&from, &to, uri, args)) {
8c51642b
YK
1201 return;
1202 }
1203
1204 migrate_set_capability(from, "auto-converge", true);
1205 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
1206 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
1207 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
1208
1209 /*
1210 * Set the initial parameters so that the migration could not converge
1211 * without throttling.
1212 */
1213 migrate_set_parameter_int(from, "downtime-limit", 1);
1214 migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
1215
1216 /* To check remaining size after precopy */
1217 migrate_set_capability(from, "pause-before-switchover", true);
1218
1219 /* Wait for the first serial output from the source */
1220 wait_for_serial("src_serial");
1221
d77799cc 1222 migrate_qmp(from, uri, "{}");
8c51642b
YK
1223
1224 /* Wait for throttling begins */
1225 percentage = 0;
1226 while (percentage == 0) {
1227 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1228 usleep(100);
1229 g_assert_false(got_stop);
1230 }
1231 /* The first percentage of throttling should be equal to init_pct */
1232 g_assert_cmpint(percentage, ==, init_pct);
1233 /* Now, when we tested that throttling works, let it converge */
1234 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1235 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1236
1237 /*
1238 * Wait for pre-switchover status to check last throttle percentage
1239 * and remaining. These values will be zeroed later
1240 */
1241 wait_for_migration_status(from, "pre-switchover", NULL);
1242
1243 /* The final percentage of throttling shouldn't be greater than max_pct */
1244 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
1245 g_assert_cmpint(percentage, <=, max_pct);
1246
1247 remaining = read_ram_property_int(from, "remaining");
6e1f837a
DDAG
1248 g_assert_cmpint(remaining, <,
1249 (expected_threshold + expected_threshold / 100));
8c51642b
YK
1250
1251 migrate_continue(from, "pre-switchover");
1252
1253 qtest_qmp_eventwait(to, "RESUME");
1254
1255 wait_for_serial("dest_serial");
1256 wait_for_migration_complete(from);
1257
1258 g_free(uri);
1259
1260 test_migrate_end(from, to, true);
1261}
1262
96eef042 1263static void test_multifd_tcp(const char *method)
b99784ef
JQ
1264{
1265 MigrateStart *args = migrate_start_new();
1266 QTestState *from, *to;
1267 QDict *rsp;
1268 char *uri;
1269
1270 if (test_migrate_start(&from, &to, "defer", args)) {
1271 return;
1272 }
1273
1274 /*
1275 * We want to pick a speed slow enough that the test completes
1276 * quickly, but that it doesn't complete precopy even on a slow
1277 * machine, so also set the downtime.
1278 */
1279 /* 1 ms should make it not converge*/
1280 migrate_set_parameter_int(from, "downtime-limit", 1);
1281 /* 1GB/s */
1282 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1283
1284 migrate_set_parameter_int(from, "multifd-channels", 16);
1285 migrate_set_parameter_int(to, "multifd-channels", 16);
1286
96eef042
JQ
1287 migrate_set_parameter_str(from, "multifd-compression", method);
1288 migrate_set_parameter_str(to, "multifd-compression", method);
1289
b99784ef
JQ
1290 migrate_set_capability(from, "multifd", "true");
1291 migrate_set_capability(to, "multifd", "true");
1292
1293 /* Start incoming migration from the 1st socket */
1294 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1295 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1296 qobject_unref(rsp);
1297
1298 /* Wait for the first serial output from the source */
1299 wait_for_serial("src_serial");
1300
1301 uri = migrate_get_socket_address(to, "socket-address");
1302
1303 migrate_qmp(from, uri, "{}");
1304
1305 wait_for_migration_pass(from);
1306
1307 /* 300ms it should converge */
1308 migrate_set_parameter_int(from, "downtime-limit", 300);
1309
1310 if (!got_stop) {
1311 qtest_qmp_eventwait(from, "STOP");
1312 }
1313 qtest_qmp_eventwait(to, "RESUME");
1314
1315 wait_for_serial("dest_serial");
1316 wait_for_migration_complete(from);
1317 test_migrate_end(from, to, true);
334d15d5 1318 g_free(uri);
b99784ef
JQ
1319}
1320
96eef042
JQ
1321static void test_multifd_tcp_none(void)
1322{
1323 test_multifd_tcp("none");
1324}
1325
7ec2c2b3
JQ
1326static void test_multifd_tcp_zlib(void)
1327{
1328 test_multifd_tcp("zlib");
1329}
1330
d795f474
JQ
1331/*
1332 * This test does:
1333 * source target
1334 * migrate_incoming
1335 * migrate
1336 * migrate_cancel
1337 * launch another target
1338 * migrate
1339 *
1340 * And see that it works
1341 */
d795f474
JQ
1342static void test_multifd_tcp_cancel(void)
1343{
1344 MigrateStart *args = migrate_start_new();
1345 QTestState *from, *to, *to2;
1346 QDict *rsp;
1347 char *uri;
1348
1349 args->hide_stderr = true;
1350
1351 if (test_migrate_start(&from, &to, "defer", args)) {
1352 return;
1353 }
1354
1355 /*
1356 * We want to pick a speed slow enough that the test completes
1357 * quickly, but that it doesn't complete precopy even on a slow
1358 * machine, so also set the downtime.
1359 */
1360 /* 1 ms should make it not converge*/
1361 migrate_set_parameter_int(from, "downtime-limit", 1);
1362 /* 300MB/s */
1363 migrate_set_parameter_int(from, "max-bandwidth", 30000000);
1364
1365 migrate_set_parameter_int(from, "multifd-channels", 16);
1366 migrate_set_parameter_int(to, "multifd-channels", 16);
1367
1368 migrate_set_capability(from, "multifd", "true");
1369 migrate_set_capability(to, "multifd", "true");
1370
1371 /* Start incoming migration from the 1st socket */
1372 rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
1373 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1374 qobject_unref(rsp);
1375
1376 /* Wait for the first serial output from the source */
1377 wait_for_serial("src_serial");
1378
1379 uri = migrate_get_socket_address(to, "socket-address");
1380
1381 migrate_qmp(from, uri, "{}");
1382
1383 wait_for_migration_pass(from);
1384
1385 migrate_cancel(from);
1386
1387 args = migrate_start_new();
1388 args->only_target = true;
1389
1390 if (test_migrate_start(&from, &to2, "defer", args)) {
1391 return;
1392 }
1393
1394 migrate_set_parameter_int(to2, "multifd-channels", 16);
1395
1396 migrate_set_capability(to2, "multifd", "true");
1397
1398 /* Start incoming migration from the 1st socket */
1399 rsp = wait_command(to2, "{ 'execute': 'migrate-incoming',"
1400 " 'arguments': { 'uri': 'tcp:127.0.0.1:0' }}");
1401 qobject_unref(rsp);
1402
e022d473 1403 g_free(uri);
d795f474
JQ
1404 uri = migrate_get_socket_address(to2, "socket-address");
1405
1406 wait_for_migration_status(from, "cancelled", NULL);
1407
1408 /* 300ms it should converge */
1409 migrate_set_parameter_int(from, "downtime-limit", 300);
1410 /* 1GB/s */
1411 migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
1412
1413 migrate_qmp(from, uri, "{}");
1414
1415 wait_for_migration_pass(from);
1416
1417 if (!got_stop) {
1418 qtest_qmp_eventwait(from, "STOP");
1419 }
1420 qtest_qmp_eventwait(to2, "RESUME");
1421
1422 wait_for_serial("dest_serial");
1423 wait_for_migration_complete(from);
1424 test_migrate_end(from, to2, true);
1425 g_free(uri);
1426}
1427
ea0c6d62
DDAG
1428int main(int argc, char **argv)
1429{
2656bfd9 1430 char template[] = "/tmp/migration-test-XXXXXX";
ea0c6d62
DDAG
1431 int ret;
1432
1433 g_test_init(&argc, &argv, NULL);
1434
1435 if (!ufd_version_check()) {
4848cb3d 1436 return g_test_run();
ea0c6d62
DDAG
1437 }
1438
880b169a
TH
1439 /*
1440 * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
1441 * is touchy due to race conditions on dirty bits (especially on PPC for
1442 * some reason)
1443 */
1444 if (g_str_equal(qtest_get_arch(), "ppc64") &&
1ee5e144
LV
1445 (access("/sys/module/kvm_hv", F_OK) ||
1446 access("/dev/kvm", R_OK | W_OK))) {
880b169a 1447 g_test_message("Skipping test: kvm_hv not available");
4848cb3d 1448 return g_test_run();
880b169a
TH
1449 }
1450
d254b392
TH
1451 /*
1452 * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
1453 * there until the problems are resolved
1454 */
1455 if (g_str_equal(qtest_get_arch(), "s390x")) {
1456#if defined(HOST_S390X)
1457 if (access("/dev/kvm", R_OK | W_OK)) {
1458 g_test_message("Skipping test: kvm not available");
4848cb3d 1459 return g_test_run();
d254b392
TH
1460 }
1461#else
1462 g_test_message("Skipping test: Need s390x host to work properly");
4848cb3d 1463 return g_test_run();
d254b392
TH
1464#endif
1465 }
1466
ea0c6d62
DDAG
1467 tmpfs = mkdtemp(template);
1468 if (!tmpfs) {
13ee9e30 1469 g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
ea0c6d62
DDAG
1470 }
1471 g_assert(tmpfs);
1472
1473 module_call_init(MODULE_INIT_QOM);
1474
2884100c 1475 qtest_add_func("/migration/postcopy/unix", test_postcopy);
d5f49640 1476 qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
4c27486d 1477 qtest_add_func("/migration/deprecated", test_deprecated);
2c9bb297 1478 qtest_add_func("/migration/bad_dest", test_baddest);
2884100c 1479 qtest_add_func("/migration/precopy/unix", test_precopy_unix);
609d3844 1480 qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
660a9b68 1481 /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
cdf84229 1482 qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
24d5588c 1483 qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
3af31a34
YK
1484 qtest_add_func("/migration/validate_uuid", test_validate_uuid);
1485 qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
1486 qtest_add_func("/migration/validate_uuid_src_not_set",
1487 test_validate_uuid_src_not_set);
1488 qtest_add_func("/migration/validate_uuid_dst_not_set",
1489 test_validate_uuid_dst_not_set);
ea0c6d62 1490
8c51642b 1491 qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
96eef042 1492 qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
d795f474 1493 qtest_add_func("/migration/multifd/tcp/cancel", test_multifd_tcp_cancel);
7ec2c2b3 1494 qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
8c51642b 1495
ea0c6d62
DDAG
1496 ret = g_test_run();
1497
1498 g_assert_cmpint(ret, ==, 0);
1499
1500 ret = rmdir(tmpfs);
1501 if (ret != 0) {
13ee9e30 1502 g_test_message("unable to rmdir: path (%s): %s",
ea0c6d62
DDAG
1503 tmpfs, strerror(errno));
1504 }
1505
1506 return ret;
1507}
This page took 0.477808 seconds and 4 git commands to generate.