1 /*
2 * QTest testcase for migration
3 *
4 * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
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"
14
15 #include "libqtest.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "qemu/range.h"
20 #include "qemu/sockets.h"
21 #include "chardev/char.h"
22 #include "crypto/tlscredspsk.h"
23 #include "qapi/qmp/qlist.h"
24 #include "ppc-util.h"
25
26 #include "migration-helpers.h"
27 #include "tests/migration/migration-test.h"
28 #ifdef CONFIG_GNUTLS
29 # include "tests/unit/crypto-tls-psk-helpers.h"
30 # ifdef CONFIG_TASN1
31 # include "tests/unit/crypto-tls-x509-helpers.h"
32 # endif /* CONFIG_TASN1 */
33 #endif /* CONFIG_GNUTLS */
34
35 /* For dirty ring test; so far only x86_64 is supported */
36 #if defined(__linux__) && defined(HOST_X86_64)
37 #include "linux/kvm.h"
38 #endif
39
40 unsigned start_address;
41 unsigned end_address;
42 static bool uffd_feature_thread_id;
43 static QTestMigrationState src_state;
44 static QTestMigrationState dst_state;
45
46 /*
47 * An initial 3 MB offset is used as that corresponds
48 * to ~1 sec of data transfer with our bandwidth setting.
49 */
50 #define MAGIC_OFFSET_BASE (3 * 1024 * 1024)
51 /*
52 * A further 1k is added to ensure we're not a multiple
53 * of TEST_MEM_PAGE_SIZE, thus avoid clash with writes
54 * from the migration guest workload.
55 */
56 #define MAGIC_OFFSET_SHUFFLE 1024
57 #define MAGIC_OFFSET (MAGIC_OFFSET_BASE + MAGIC_OFFSET_SHUFFLE)
58 #define MAGIC_MARKER 0xFEED12345678CAFEULL
59
60 /*
61 * Dirtylimit stop working if dirty page rate error
62 * value less than DIRTYLIMIT_TOLERANCE_RANGE
63 */
64 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
65
66 #define ANALYZE_SCRIPT "scripts/analyze-migration.py"
67
68 #define QEMU_VM_FILE_MAGIC 0x5145564d
69 #define FILE_TEST_FILENAME "migfile"
70 #define FILE_TEST_OFFSET 0x1000
71 #define QEMU_ENV_SRC "QTEST_QEMU_BINARY_SRC"
72 #define QEMU_ENV_DST "QTEST_QEMU_BINARY_DST"
73
74 #if defined(__linux__)
75 #include <sys/syscall.h>
76 #include <sys/vfs.h>
77 #endif
78
79 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
80 #include <sys/eventfd.h>
81 #include <sys/ioctl.h>
82 #include "qemu/userfaultfd.h"
83
ufd_version_check(void)84 static bool ufd_version_check(void)
85 {
86 struct uffdio_api api_struct;
87 uint64_t ioctl_mask;
88
89 int ufd = uffd_open(O_CLOEXEC);
90
91 if (ufd == -1) {
92 g_test_message("Skipping test: userfaultfd not available");
93 return false;
94 }
95
96 api_struct.api = UFFD_API;
97 api_struct.features = 0;
98 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
99 g_test_message("Skipping test: UFFDIO_API failed");
100 return false;
101 }
102 uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
103
104 ioctl_mask = 1ULL << _UFFDIO_REGISTER |
105 1ULL << _UFFDIO_UNREGISTER;
106 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
107 g_test_message("Skipping test: Missing userfault feature");
108 return false;
109 }
110
111 return true;
112 }
113
114 #else
ufd_version_check(void)115 static bool ufd_version_check(void)
116 {
117 g_test_message("Skipping test: Userfault not available (builtdtime)");
118 return false;
119 }
120
121 #endif
122
123 static char *tmpfs;
124 static char *bootpath;
125
126 /* The boot file modifies memory area in [start_address, end_address)
127 * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
128 */
129 #include "tests/migration/i386/a-b-bootblock.h"
130 #include "tests/migration/aarch64/a-b-kernel.h"
131 #include "tests/migration/ppc64/a-b-kernel.h"
132 #include "tests/migration/s390x/a-b-bios.h"
133
bootfile_create(char * dir,bool suspend_me)134 static void bootfile_create(char *dir, bool suspend_me)
135 {
136 const char *arch = qtest_get_arch();
137 unsigned char *content;
138 size_t len;
139
140 bootpath = g_strdup_printf("%s/bootsect", dir);
141 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
142 /* the assembled x86 boot sector should be exactly one sector large */
143 g_assert(sizeof(x86_bootsect) == 512);
144 x86_bootsect[SYM_suspend_me - SYM_start] = suspend_me;
145 content = x86_bootsect;
146 len = sizeof(x86_bootsect);
147 } else if (g_str_equal(arch, "s390x")) {
148 content = s390x_elf;
149 len = sizeof(s390x_elf);
150 } else if (strcmp(arch, "ppc64") == 0) {
151 content = ppc64_kernel;
152 len = sizeof(ppc64_kernel);
153 } else if (strcmp(arch, "aarch64") == 0) {
154 content = aarch64_kernel;
155 len = sizeof(aarch64_kernel);
156 g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
157 } else {
158 g_assert_not_reached();
159 }
160
161 FILE *bootfile = fopen(bootpath, "wb");
162
163 g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
164 fclose(bootfile);
165 }
166
bootfile_delete(void)167 static void bootfile_delete(void)
168 {
169 unlink(bootpath);
170 g_free(bootpath);
171 bootpath = NULL;
172 }
173
174 /*
175 * Wait for some output in the serial output file,
176 * we get an 'A' followed by an endless string of 'B's
177 * but on the destination we won't have the A (unless we enabled suspend/resume)
178 */
wait_for_serial(const char * side)179 static void wait_for_serial(const char *side)
180 {
181 g_autofree char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
182 FILE *serialfile = fopen(serialpath, "r");
183
184 do {
185 int readvalue = fgetc(serialfile);
186
187 switch (readvalue) {
188 case 'A':
189 /* Fine */
190 break;
191
192 case 'B':
193 /* It's alive! */
194 fclose(serialfile);
195 return;
196
197 case EOF:
198 fseek(serialfile, 0, SEEK_SET);
199 usleep(1000);
200 break;
201
202 default:
203 fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
204 g_assert_not_reached();
205 }
206 } while (true);
207 }
208
wait_for_stop(QTestState * who,QTestMigrationState * state)209 static void wait_for_stop(QTestState *who, QTestMigrationState *state)
210 {
211 if (!state->stop_seen) {
212 qtest_qmp_eventwait(who, "STOP");
213 }
214 }
215
wait_for_resume(QTestState * who,QTestMigrationState * state)216 static void wait_for_resume(QTestState *who, QTestMigrationState *state)
217 {
218 if (!state->resume_seen) {
219 qtest_qmp_eventwait(who, "RESUME");
220 }
221 }
222
wait_for_suspend(QTestState * who,QTestMigrationState * state)223 static void wait_for_suspend(QTestState *who, QTestMigrationState *state)
224 {
225 if (state->suspend_me && !state->suspend_seen) {
226 qtest_qmp_eventwait(who, "SUSPEND");
227 }
228 }
229
230 /*
231 * It's tricky to use qemu's migration event capability with qtest,
232 * events suddenly appearing confuse the qmp()/hmp() responses.
233 */
234
read_ram_property_int(QTestState * who,const char * property)235 static int64_t read_ram_property_int(QTestState *who, const char *property)
236 {
237 QDict *rsp_return, *rsp_ram;
238 int64_t result;
239
240 rsp_return = migrate_query_not_failed(who);
241 if (!qdict_haskey(rsp_return, "ram")) {
242 /* Still in setup */
243 result = 0;
244 } else {
245 rsp_ram = qdict_get_qdict(rsp_return, "ram");
246 result = qdict_get_try_int(rsp_ram, property, 0);
247 }
248 qobject_unref(rsp_return);
249 return result;
250 }
251
read_migrate_property_int(QTestState * who,const char * property)252 static int64_t read_migrate_property_int(QTestState *who, const char *property)
253 {
254 QDict *rsp_return;
255 int64_t result;
256
257 rsp_return = migrate_query_not_failed(who);
258 result = qdict_get_try_int(rsp_return, property, 0);
259 qobject_unref(rsp_return);
260 return result;
261 }
262
get_migration_pass(QTestState * who)263 static uint64_t get_migration_pass(QTestState *who)
264 {
265 return read_ram_property_int(who, "dirty-sync-count");
266 }
267
read_blocktime(QTestState * who)268 static void read_blocktime(QTestState *who)
269 {
270 QDict *rsp_return;
271
272 rsp_return = migrate_query_not_failed(who);
273 g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
274 qobject_unref(rsp_return);
275 }
276
277 /*
278 * Wait for two changes in the migration pass count, but bail if we stop.
279 */
wait_for_migration_pass(QTestState * who)280 static void wait_for_migration_pass(QTestState *who)
281 {
282 uint64_t pass, prev_pass = 0, changes = 0;
283
284 while (changes < 2 && !src_state.stop_seen && !src_state.suspend_seen) {
285 usleep(1000);
286 pass = get_migration_pass(who);
287 changes += (pass != prev_pass);
288 prev_pass = pass;
289 }
290 }
291
check_guests_ram(QTestState * who)292 static void check_guests_ram(QTestState *who)
293 {
294 /* Our ASM test will have been incrementing one byte from each page from
295 * start_address to < end_address in order. This gives us a constraint
296 * that any page's byte should be equal or less than the previous pages
297 * byte (mod 256); and they should all be equal except for one transition
298 * at the point where we meet the incrementer. (We're running this with
299 * the guest stopped).
300 */
301 unsigned address;
302 uint8_t first_byte;
303 uint8_t last_byte;
304 bool hit_edge = false;
305 int bad = 0;
306
307 qtest_memread(who, start_address, &first_byte, 1);
308 last_byte = first_byte;
309
310 for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
311 address += TEST_MEM_PAGE_SIZE)
312 {
313 uint8_t b;
314 qtest_memread(who, address, &b, 1);
315 if (b != last_byte) {
316 if (((b + 1) % 256) == last_byte && !hit_edge) {
317 /* This is OK, the guest stopped at the point of
318 * incrementing the previous page but didn't get
319 * to us yet.
320 */
321 hit_edge = true;
322 last_byte = b;
323 } else {
324 bad++;
325 if (bad <= 10) {
326 fprintf(stderr, "Memory content inconsistency at %x"
327 " first_byte = %x last_byte = %x current = %x"
328 " hit_edge = %x\n",
329 address, first_byte, last_byte, b, hit_edge);
330 }
331 }
332 }
333 }
334 if (bad >= 10) {
335 fprintf(stderr, "and in another %d pages", bad - 10);
336 }
337 g_assert(bad == 0);
338 }
339
cleanup(const char * filename)340 static void cleanup(const char *filename)
341 {
342 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, filename);
343
344 unlink(path);
345 }
346
migrate_get_parameter_int(QTestState * who,const char * parameter)347 static long long migrate_get_parameter_int(QTestState *who,
348 const char *parameter)
349 {
350 QDict *rsp;
351 long long result;
352
353 rsp = qtest_qmp_assert_success_ref(
354 who, "{ 'execute': 'query-migrate-parameters' }");
355 result = qdict_get_int(rsp, parameter);
356 qobject_unref(rsp);
357 return result;
358 }
359
migrate_check_parameter_int(QTestState * who,const char * parameter,long long value)360 static void migrate_check_parameter_int(QTestState *who, const char *parameter,
361 long long value)
362 {
363 long long result;
364
365 result = migrate_get_parameter_int(who, parameter);
366 g_assert_cmpint(result, ==, value);
367 }
368
migrate_set_parameter_int(QTestState * who,const char * parameter,long long value)369 static void migrate_set_parameter_int(QTestState *who, const char *parameter,
370 long long value)
371 {
372 qtest_qmp_assert_success(who,
373 "{ 'execute': 'migrate-set-parameters',"
374 "'arguments': { %s: %lld } }",
375 parameter, value);
376 migrate_check_parameter_int(who, parameter, value);
377 }
378
migrate_get_parameter_str(QTestState * who,const char * parameter)379 static char *migrate_get_parameter_str(QTestState *who,
380 const char *parameter)
381 {
382 QDict *rsp;
383 char *result;
384
385 rsp = qtest_qmp_assert_success_ref(
386 who, "{ 'execute': 'query-migrate-parameters' }");
387 result = g_strdup(qdict_get_str(rsp, parameter));
388 qobject_unref(rsp);
389 return result;
390 }
391
migrate_check_parameter_str(QTestState * who,const char * parameter,const char * value)392 static void migrate_check_parameter_str(QTestState *who, const char *parameter,
393 const char *value)
394 {
395 g_autofree char *result = migrate_get_parameter_str(who, parameter);
396 g_assert_cmpstr(result, ==, value);
397 }
398
migrate_set_parameter_str(QTestState * who,const char * parameter,const char * value)399 static void migrate_set_parameter_str(QTestState *who, const char *parameter,
400 const char *value)
401 {
402 qtest_qmp_assert_success(who,
403 "{ 'execute': 'migrate-set-parameters',"
404 "'arguments': { %s: %s } }",
405 parameter, value);
406 migrate_check_parameter_str(who, parameter, value);
407 }
408
migrate_ensure_non_converge(QTestState * who)409 static void migrate_ensure_non_converge(QTestState *who)
410 {
411 /* Can't converge with 1ms downtime + 3 mbs bandwidth limit */
412 migrate_set_parameter_int(who, "max-bandwidth", 3 * 1000 * 1000);
413 migrate_set_parameter_int(who, "downtime-limit", 1);
414 }
415
migrate_ensure_converge(QTestState * who)416 static void migrate_ensure_converge(QTestState *who)
417 {
418 /* Should converge with 30s downtime + 1 gbs bandwidth limit */
419 migrate_set_parameter_int(who, "max-bandwidth", 1 * 1000 * 1000 * 1000);
420 migrate_set_parameter_int(who, "downtime-limit", 30 * 1000);
421 }
422
423 /*
424 * Our goal is to ensure that we run a single full migration
425 * iteration, and also dirty memory, ensuring that at least
426 * one further iteration is required.
427 *
428 * We can't directly synchronize with the start of a migration
429 * so we have to apply some tricks monitoring memory that is
430 * transferred.
431 *
432 * Initially we set the migration bandwidth to an insanely
433 * low value, with tiny max downtime too. This basically
434 * guarantees migration will never complete.
435 *
436 * This will result in a test that is unacceptably slow though,
437 * so we can't let the entire migration pass run at this speed.
438 * Our intent is to let it run just long enough that we can
439 * prove data prior to the marker has been transferred *AND*
440 * also prove this transferred data is dirty again.
441 *
442 * Before migration starts, we write a 64-bit magic marker
443 * into a fixed location in the src VM RAM.
444 *
445 * Then watch dst memory until the marker appears. This is
446 * proof that start_address -> MAGIC_OFFSET_BASE has been
447 * transferred.
448 *
449 * Finally we go back to the source and read a byte just
450 * before the marker until we see it flip in value. This
451 * is proof that start_address -> MAGIC_OFFSET_BASE
452 * is now dirty again.
453 *
454 * IOW, we're guaranteed at least a 2nd migration pass
455 * at this point.
456 *
457 * We can now let migration run at full speed to finish
458 * the test
459 */
migrate_prepare_for_dirty_mem(QTestState * from)460 static void migrate_prepare_for_dirty_mem(QTestState *from)
461 {
462 /*
463 * The guest workflow iterates from start_address to
464 * end_address, writing 1 byte every TEST_MEM_PAGE_SIZE
465 * bytes.
466 *
467 * IOW, if we write to mem at a point which is NOT
468 * a multiple of TEST_MEM_PAGE_SIZE, our write won't
469 * conflict with the migration workflow.
470 *
471 * We put in a marker here, that we'll use to determine
472 * when the data has been transferred to the dst.
473 */
474 qtest_writeq(from, start_address + MAGIC_OFFSET, MAGIC_MARKER);
475 }
476
migrate_wait_for_dirty_mem(QTestState * from,QTestState * to)477 static void migrate_wait_for_dirty_mem(QTestState *from,
478 QTestState *to)
479 {
480 uint64_t watch_address = start_address + MAGIC_OFFSET_BASE;
481 uint64_t marker_address = start_address + MAGIC_OFFSET;
482 uint8_t watch_byte;
483
484 /*
485 * Wait for the MAGIC_MARKER to get transferred, as an
486 * indicator that a migration pass has made some known
487 * amount of progress.
488 */
489 do {
490 usleep(1000 * 10);
491 } while (qtest_readq(to, marker_address) != MAGIC_MARKER);
492
493
494 /* If suspended, src only iterates once, and watch_byte may never change */
495 if (src_state.suspend_me) {
496 return;
497 }
498
499 /*
500 * Now ensure that already transferred bytes are
501 * dirty again from the guest workload. Note the
502 * guest byte value will wrap around and by chance
503 * match the original watch_byte. This is harmless
504 * as we'll eventually see a different value if we
505 * keep watching
506 */
507 watch_byte = qtest_readb(from, watch_address);
508 do {
509 usleep(1000 * 10);
510 } while (qtest_readb(from, watch_address) == watch_byte);
511 }
512
513
migrate_pause(QTestState * who)514 static void migrate_pause(QTestState *who)
515 {
516 qtest_qmp_assert_success(who, "{ 'execute': 'migrate-pause' }");
517 }
518
migrate_continue(QTestState * who,const char * state)519 static void migrate_continue(QTestState *who, const char *state)
520 {
521 qtest_qmp_assert_success(who,
522 "{ 'execute': 'migrate-continue',"
523 " 'arguments': { 'state': %s } }",
524 state);
525 }
526
migrate_recover(QTestState * who,const char * uri)527 static void migrate_recover(QTestState *who, const char *uri)
528 {
529 qtest_qmp_assert_success(who,
530 "{ 'execute': 'migrate-recover', "
531 " 'id': 'recover-cmd', "
532 " 'arguments': { 'uri': %s } }",
533 uri);
534 }
535
migrate_cancel(QTestState * who)536 static void migrate_cancel(QTestState *who)
537 {
538 qtest_qmp_assert_success(who, "{ 'execute': 'migrate_cancel' }");
539 }
540
migrate_postcopy_start(QTestState * from,QTestState * to)541 static void migrate_postcopy_start(QTestState *from, QTestState *to)
542 {
543 qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
544
545 wait_for_stop(from, &src_state);
546 qtest_qmp_eventwait(to, "RESUME");
547 }
548
549 typedef struct {
550 /*
551 * QTEST_LOG=1 may override this. When QTEST_LOG=1, we always dump errors
552 * unconditionally, because it means the user would like to be verbose.
553 */
554 bool hide_stderr;
555 bool use_shmem;
556 /* only launch the target process */
557 bool only_target;
558 /* Use dirty ring if true; dirty logging otherwise */
559 bool use_dirty_ring;
560 const char *opts_source;
561 const char *opts_target;
562 /* suspend the src before migrating to dest. */
563 bool suspend_me;
564 } MigrateStart;
565
566 /*
567 * A hook that runs after the src and dst QEMUs have been
568 * created, but before the migration is started. This can
569 * be used to set migration parameters and capabilities.
570 *
571 * Returns: NULL, or a pointer to opaque state to be
572 * later passed to the TestMigrateFinishHook
573 */
574 typedef void * (*TestMigrateStartHook)(QTestState *from,
575 QTestState *to);
576
577 /*
578 * A hook that runs after the migration has finished,
579 * regardless of whether it succeeded or failed, but
580 * before QEMU has terminated (unless it self-terminated
581 * due to migration error)
582 *
583 * @opaque is a pointer to state previously returned
584 * by the TestMigrateStartHook if any, or NULL.
585 */
586 typedef void (*TestMigrateFinishHook)(QTestState *from,
587 QTestState *to,
588 void *opaque);
589
590 typedef struct {
591 /* Optional: fine tune start parameters */
592 MigrateStart start;
593
594 /* Required: the URI for the dst QEMU to listen on */
595 const char *listen_uri;
596
597 /*
598 * Optional: the URI for the src QEMU to connect to
599 * If NULL, then it will query the dst QEMU for its actual
600 * listening address and use that as the connect address.
601 * This allows for dynamically picking a free TCP port.
602 */
603 const char *connect_uri;
604
605 /*
606 * Optional: JSON-formatted list of src QEMU URIs. If a port is
607 * defined as '0' in any QDict key a value of '0' will be
608 * automatically converted to the correct destination port.
609 */
610 const char *connect_channels;
611
612 /* Optional: callback to run at start to set migration parameters */
613 TestMigrateStartHook start_hook;
614 /* Optional: callback to run at finish to cleanup */
615 TestMigrateFinishHook finish_hook;
616
617 /*
618 * Optional: normally we expect the migration process to complete.
619 *
620 * There can be a variety of reasons and stages in which failure
621 * can happen during tests.
622 *
623 * If a failure is expected to happen at time of establishing
624 * the connection, then MIG_TEST_FAIL will indicate that the dst
625 * QEMU is expected to stay running and accept future migration
626 * connections.
627 *
628 * If a failure is expected to happen while processing the
629 * migration stream, then MIG_TEST_FAIL_DEST_QUIT_ERR will indicate
630 * that the dst QEMU is expected to quit with non-zero exit status
631 */
632 enum {
633 /* This test should succeed, the default */
634 MIG_TEST_SUCCEED = 0,
635 /* This test should fail, dest qemu should keep alive */
636 MIG_TEST_FAIL,
637 /* This test should fail, dest qemu should fail with abnormal status */
638 MIG_TEST_FAIL_DEST_QUIT_ERR,
639 /* The QMP command for this migration should fail with an error */
640 MIG_TEST_QMP_ERROR,
641 } result;
642
643 /*
644 * Optional: set number of migration passes to wait for, if live==true.
645 * If zero, then merely wait for a few MB of dirty data
646 */
647 unsigned int iterations;
648
649 /*
650 * Optional: whether the guest CPUs should be running during a precopy
651 * migration test. We used to always run with live but it took much
652 * longer so we reduced live tests to only the ones that have solid
653 * reason to be tested live-only. For each of the new test cases for
654 * precopy please provide justifications to use live explicitly (please
655 * refer to existing ones with live=true), or use live=off by default.
656 */
657 bool live;
658
659 /* Postcopy specific fields */
660 void *postcopy_data;
661 bool postcopy_preempt;
662 bool postcopy_recovery_test_fail;
663 } MigrateCommon;
664
test_migrate_start(QTestState ** from,QTestState ** to,const char * uri,MigrateStart * args)665 static int test_migrate_start(QTestState **from, QTestState **to,
666 const char *uri, MigrateStart *args)
667 {
668 g_autofree gchar *arch_source = NULL;
669 g_autofree gchar *arch_target = NULL;
670 /* options for source and target */
671 g_autofree gchar *arch_opts = NULL;
672 g_autofree gchar *cmd_source = NULL;
673 g_autofree gchar *cmd_target = NULL;
674 const gchar *ignore_stderr;
675 g_autofree char *shmem_opts = NULL;
676 g_autofree char *shmem_path = NULL;
677 const char *kvm_opts = NULL;
678 const char *arch = qtest_get_arch();
679 const char *memory_size;
680 const char *machine_alias, *machine_opts = "";
681 g_autofree char *machine = NULL;
682
683 if (args->use_shmem) {
684 if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
685 g_test_skip("/dev/shm is not supported");
686 return -1;
687 }
688 }
689
690 dst_state = (QTestMigrationState) { };
691 src_state = (QTestMigrationState) { };
692 bootfile_create(tmpfs, args->suspend_me);
693 src_state.suspend_me = args->suspend_me;
694
695 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
696 memory_size = "150M";
697
698 if (g_str_equal(arch, "i386")) {
699 machine_alias = "pc";
700 } else {
701 machine_alias = "q35";
702 }
703 arch_opts = g_strdup_printf(
704 "-drive if=none,id=d0,file=%s,format=raw "
705 "-device ide-hd,drive=d0,secs=1,cyls=1,heads=1", bootpath);
706 start_address = X86_TEST_MEM_START;
707 end_address = X86_TEST_MEM_END;
708 } else if (g_str_equal(arch, "s390x")) {
709 memory_size = "128M";
710 machine_alias = "s390-ccw-virtio";
711 arch_opts = g_strdup_printf("-bios %s", bootpath);
712 start_address = S390_TEST_MEM_START;
713 end_address = S390_TEST_MEM_END;
714 } else if (strcmp(arch, "ppc64") == 0) {
715 memory_size = "256M";
716 start_address = PPC_TEST_MEM_START;
717 end_address = PPC_TEST_MEM_END;
718 machine_alias = "pseries";
719 machine_opts = "vsmt=8";
720 arch_opts = g_strdup_printf(
721 "-nodefaults -machine " PSERIES_DEFAULT_CAPABILITIES " "
722 "-bios %s", bootpath);
723 } else if (strcmp(arch, "aarch64") == 0) {
724 memory_size = "150M";
725 machine_alias = "virt";
726 machine_opts = "gic-version=3";
727 arch_opts = g_strdup_printf("-cpu max -kernel %s", bootpath);
728 start_address = ARM_TEST_MEM_START;
729 end_address = ARM_TEST_MEM_END;
730 } else {
731 g_assert_not_reached();
732 }
733
734 if (!getenv("QTEST_LOG") && args->hide_stderr) {
735 #ifndef _WIN32
736 ignore_stderr = "2>/dev/null";
737 #else
738 /*
739 * On Windows the QEMU executable is created via CreateProcess() and
740 * IO redirection does not work, so don't bother adding IO redirection
741 * to the command line.
742 */
743 ignore_stderr = "";
744 #endif
745 } else {
746 ignore_stderr = "";
747 }
748
749 if (args->use_shmem) {
750 shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
751 shmem_opts = g_strdup_printf(
752 "-object memory-backend-file,id=mem0,size=%s"
753 ",mem-path=%s,share=on -numa node,memdev=mem0",
754 memory_size, shmem_path);
755 }
756
757 if (args->use_dirty_ring) {
758 kvm_opts = ",dirty-ring-size=4096";
759 }
760
761 if (!qtest_has_machine(machine_alias)) {
762 g_autofree char *msg = g_strdup_printf("machine %s not supported", machine_alias);
763 g_test_skip(msg);
764 return -1;
765 }
766
767 machine = resolve_machine_version(machine_alias, QEMU_ENV_SRC,
768 QEMU_ENV_DST);
769
770 g_test_message("Using machine type: %s", machine);
771
772 cmd_source = g_strdup_printf("-accel kvm%s -accel tcg "
773 "-machine %s,%s "
774 "-name source,debug-threads=on "
775 "-m %s "
776 "-serial file:%s/src_serial "
777 "%s %s %s %s %s",
778 kvm_opts ? kvm_opts : "",
779 machine, machine_opts,
780 memory_size, tmpfs,
781 arch_opts ? arch_opts : "",
782 arch_source ? arch_source : "",
783 shmem_opts ? shmem_opts : "",
784 args->opts_source ? args->opts_source : "",
785 ignore_stderr);
786 if (!args->only_target) {
787 *from = qtest_init_with_env(QEMU_ENV_SRC, cmd_source);
788 qtest_qmp_set_event_callback(*from,
789 migrate_watch_for_events,
790 &src_state);
791 }
792
793 cmd_target = g_strdup_printf("-accel kvm%s -accel tcg "
794 "-machine %s,%s "
795 "-name target,debug-threads=on "
796 "-m %s "
797 "-serial file:%s/dest_serial "
798 "-incoming %s "
799 "%s %s %s %s %s",
800 kvm_opts ? kvm_opts : "",
801 machine, machine_opts,
802 memory_size, tmpfs, uri,
803 arch_opts ? arch_opts : "",
804 arch_target ? arch_target : "",
805 shmem_opts ? shmem_opts : "",
806 args->opts_target ? args->opts_target : "",
807 ignore_stderr);
808 *to = qtest_init_with_env(QEMU_ENV_DST, cmd_target);
809 qtest_qmp_set_event_callback(*to,
810 migrate_watch_for_events,
811 &dst_state);
812
813 /*
814 * Remove shmem file immediately to avoid memory leak in test failed case.
815 * It's valid because QEMU has already opened this file
816 */
817 if (args->use_shmem) {
818 unlink(shmem_path);
819 }
820
821 return 0;
822 }
823
test_migrate_end(QTestState * from,QTestState * to,bool test_dest)824 static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
825 {
826 unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
827
828 qtest_quit(from);
829
830 if (test_dest) {
831 qtest_memread(to, start_address, &dest_byte_a, 1);
832
833 /* Destination still running, wait for a byte to change */
834 do {
835 qtest_memread(to, start_address, &dest_byte_b, 1);
836 usleep(1000 * 10);
837 } while (dest_byte_a == dest_byte_b);
838
839 qtest_qmp_assert_success(to, "{ 'execute' : 'stop'}");
840
841 /* With it stopped, check nothing changes */
842 qtest_memread(to, start_address, &dest_byte_c, 1);
843 usleep(1000 * 200);
844 qtest_memread(to, start_address, &dest_byte_d, 1);
845 g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
846
847 check_guests_ram(to);
848 }
849
850 qtest_quit(to);
851
852 cleanup("migsocket");
853 cleanup("src_serial");
854 cleanup("dest_serial");
855 cleanup(FILE_TEST_FILENAME);
856 }
857
858 #ifdef CONFIG_GNUTLS
859 struct TestMigrateTLSPSKData {
860 char *workdir;
861 char *workdiralt;
862 char *pskfile;
863 char *pskfilealt;
864 };
865
866 static void *
test_migrate_tls_psk_start_common(QTestState * from,QTestState * to,bool mismatch)867 test_migrate_tls_psk_start_common(QTestState *from,
868 QTestState *to,
869 bool mismatch)
870 {
871 struct TestMigrateTLSPSKData *data =
872 g_new0(struct TestMigrateTLSPSKData, 1);
873
874 data->workdir = g_strdup_printf("%s/tlscredspsk0", tmpfs);
875 data->pskfile = g_strdup_printf("%s/%s", data->workdir,
876 QCRYPTO_TLS_CREDS_PSKFILE);
877 g_mkdir_with_parents(data->workdir, 0700);
878 test_tls_psk_init(data->pskfile);
879
880 if (mismatch) {
881 data->workdiralt = g_strdup_printf("%s/tlscredspskalt0", tmpfs);
882 data->pskfilealt = g_strdup_printf("%s/%s", data->workdiralt,
883 QCRYPTO_TLS_CREDS_PSKFILE);
884 g_mkdir_with_parents(data->workdiralt, 0700);
885 test_tls_psk_init_alt(data->pskfilealt);
886 }
887
888 qtest_qmp_assert_success(from,
889 "{ 'execute': 'object-add',"
890 " 'arguments': { 'qom-type': 'tls-creds-psk',"
891 " 'id': 'tlscredspsk0',"
892 " 'endpoint': 'client',"
893 " 'dir': %s,"
894 " 'username': 'qemu'} }",
895 data->workdir);
896
897 qtest_qmp_assert_success(to,
898 "{ 'execute': 'object-add',"
899 " 'arguments': { 'qom-type': 'tls-creds-psk',"
900 " 'id': 'tlscredspsk0',"
901 " 'endpoint': 'server',"
902 " 'dir': %s } }",
903 mismatch ? data->workdiralt : data->workdir);
904
905 migrate_set_parameter_str(from, "tls-creds", "tlscredspsk0");
906 migrate_set_parameter_str(to, "tls-creds", "tlscredspsk0");
907
908 return data;
909 }
910
911 static void *
test_migrate_tls_psk_start_match(QTestState * from,QTestState * to)912 test_migrate_tls_psk_start_match(QTestState *from,
913 QTestState *to)
914 {
915 return test_migrate_tls_psk_start_common(from, to, false);
916 }
917
918 static void *
test_migrate_tls_psk_start_mismatch(QTestState * from,QTestState * to)919 test_migrate_tls_psk_start_mismatch(QTestState *from,
920 QTestState *to)
921 {
922 return test_migrate_tls_psk_start_common(from, to, true);
923 }
924
925 static void
test_migrate_tls_psk_finish(QTestState * from,QTestState * to,void * opaque)926 test_migrate_tls_psk_finish(QTestState *from,
927 QTestState *to,
928 void *opaque)
929 {
930 struct TestMigrateTLSPSKData *data = opaque;
931
932 test_tls_psk_cleanup(data->pskfile);
933 if (data->pskfilealt) {
934 test_tls_psk_cleanup(data->pskfilealt);
935 }
936 rmdir(data->workdir);
937 if (data->workdiralt) {
938 rmdir(data->workdiralt);
939 }
940
941 g_free(data->workdiralt);
942 g_free(data->pskfilealt);
943 g_free(data->workdir);
944 g_free(data->pskfile);
945 g_free(data);
946 }
947
948 #ifdef CONFIG_TASN1
949 typedef struct {
950 char *workdir;
951 char *keyfile;
952 char *cacert;
953 char *servercert;
954 char *serverkey;
955 char *clientcert;
956 char *clientkey;
957 } TestMigrateTLSX509Data;
958
959 typedef struct {
960 bool verifyclient;
961 bool clientcert;
962 bool hostileclient;
963 bool authzclient;
964 const char *certhostname;
965 const char *certipaddr;
966 } TestMigrateTLSX509;
967
968 static void *
test_migrate_tls_x509_start_common(QTestState * from,QTestState * to,TestMigrateTLSX509 * args)969 test_migrate_tls_x509_start_common(QTestState *from,
970 QTestState *to,
971 TestMigrateTLSX509 *args)
972 {
973 TestMigrateTLSX509Data *data = g_new0(TestMigrateTLSX509Data, 1);
974
975 data->workdir = g_strdup_printf("%s/tlscredsx5090", tmpfs);
976 data->keyfile = g_strdup_printf("%s/key.pem", data->workdir);
977
978 data->cacert = g_strdup_printf("%s/ca-cert.pem", data->workdir);
979 data->serverkey = g_strdup_printf("%s/server-key.pem", data->workdir);
980 data->servercert = g_strdup_printf("%s/server-cert.pem", data->workdir);
981 if (args->clientcert) {
982 data->clientkey = g_strdup_printf("%s/client-key.pem", data->workdir);
983 data->clientcert = g_strdup_printf("%s/client-cert.pem", data->workdir);
984 }
985
986 g_mkdir_with_parents(data->workdir, 0700);
987
988 test_tls_init(data->keyfile);
989 #ifndef _WIN32
990 g_assert(link(data->keyfile, data->serverkey) == 0);
991 #else
992 g_assert(CreateHardLink(data->serverkey, data->keyfile, NULL) != 0);
993 #endif
994 if (args->clientcert) {
995 #ifndef _WIN32
996 g_assert(link(data->keyfile, data->clientkey) == 0);
997 #else
998 g_assert(CreateHardLink(data->clientkey, data->keyfile, NULL) != 0);
999 #endif
1000 }
1001
1002 TLS_ROOT_REQ_SIMPLE(cacertreq, data->cacert);
1003 if (args->clientcert) {
1004 TLS_CERT_REQ_SIMPLE_CLIENT(servercertreq, cacertreq,
1005 args->hostileclient ?
1006 QCRYPTO_TLS_TEST_CLIENT_HOSTILE_NAME :
1007 QCRYPTO_TLS_TEST_CLIENT_NAME,
1008 data->clientcert);
1009 }
1010
1011 TLS_CERT_REQ_SIMPLE_SERVER(clientcertreq, cacertreq,
1012 data->servercert,
1013 args->certhostname,
1014 args->certipaddr);
1015
1016 qtest_qmp_assert_success(from,
1017 "{ 'execute': 'object-add',"
1018 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1019 " 'id': 'tlscredsx509client0',"
1020 " 'endpoint': 'client',"
1021 " 'dir': %s,"
1022 " 'sanity-check': true,"
1023 " 'verify-peer': true} }",
1024 data->workdir);
1025 migrate_set_parameter_str(from, "tls-creds", "tlscredsx509client0");
1026 if (args->certhostname) {
1027 migrate_set_parameter_str(from, "tls-hostname", args->certhostname);
1028 }
1029
1030 qtest_qmp_assert_success(to,
1031 "{ 'execute': 'object-add',"
1032 " 'arguments': { 'qom-type': 'tls-creds-x509',"
1033 " 'id': 'tlscredsx509server0',"
1034 " 'endpoint': 'server',"
1035 " 'dir': %s,"
1036 " 'sanity-check': true,"
1037 " 'verify-peer': %i} }",
1038 data->workdir, args->verifyclient);
1039 migrate_set_parameter_str(to, "tls-creds", "tlscredsx509server0");
1040
1041 if (args->authzclient) {
1042 qtest_qmp_assert_success(to,
1043 "{ 'execute': 'object-add',"
1044 " 'arguments': { 'qom-type': 'authz-simple',"
1045 " 'id': 'tlsauthz0',"
1046 " 'identity': %s} }",
1047 "CN=" QCRYPTO_TLS_TEST_CLIENT_NAME);
1048 migrate_set_parameter_str(to, "tls-authz", "tlsauthz0");
1049 }
1050
1051 return data;
1052 }
1053
1054 /*
1055 * The normal case: match server's cert hostname against
1056 * whatever host we were telling QEMU to connect to (if any)
1057 */
1058 static void *
test_migrate_tls_x509_start_default_host(QTestState * from,QTestState * to)1059 test_migrate_tls_x509_start_default_host(QTestState *from,
1060 QTestState *to)
1061 {
1062 TestMigrateTLSX509 args = {
1063 .verifyclient = true,
1064 .clientcert = true,
1065 .certipaddr = "127.0.0.1"
1066 };
1067 return test_migrate_tls_x509_start_common(from, to, &args);
1068 }
1069
1070 /*
1071 * The unusual case: the server's cert is different from
1072 * the address we're telling QEMU to connect to (if any),
1073 * so we must give QEMU an explicit hostname to validate
1074 */
1075 static void *
test_migrate_tls_x509_start_override_host(QTestState * from,QTestState * to)1076 test_migrate_tls_x509_start_override_host(QTestState *from,
1077 QTestState *to)
1078 {
1079 TestMigrateTLSX509 args = {
1080 .verifyclient = true,
1081 .clientcert = true,
1082 .certhostname = "qemu.org",
1083 };
1084 return test_migrate_tls_x509_start_common(from, to, &args);
1085 }
1086
1087 /*
1088 * The unusual case: the server's cert is different from
1089 * the address we're telling QEMU to connect to, and so we
1090 * expect the client to reject the server
1091 */
1092 static void *
test_migrate_tls_x509_start_mismatch_host(QTestState * from,QTestState * to)1093 test_migrate_tls_x509_start_mismatch_host(QTestState *from,
1094 QTestState *to)
1095 {
1096 TestMigrateTLSX509 args = {
1097 .verifyclient = true,
1098 .clientcert = true,
1099 .certipaddr = "10.0.0.1",
1100 };
1101 return test_migrate_tls_x509_start_common(from, to, &args);
1102 }
1103
1104 static void *
test_migrate_tls_x509_start_friendly_client(QTestState * from,QTestState * to)1105 test_migrate_tls_x509_start_friendly_client(QTestState *from,
1106 QTestState *to)
1107 {
1108 TestMigrateTLSX509 args = {
1109 .verifyclient = true,
1110 .clientcert = true,
1111 .authzclient = true,
1112 .certipaddr = "127.0.0.1",
1113 };
1114 return test_migrate_tls_x509_start_common(from, to, &args);
1115 }
1116
1117 static void *
test_migrate_tls_x509_start_hostile_client(QTestState * from,QTestState * to)1118 test_migrate_tls_x509_start_hostile_client(QTestState *from,
1119 QTestState *to)
1120 {
1121 TestMigrateTLSX509 args = {
1122 .verifyclient = true,
1123 .clientcert = true,
1124 .hostileclient = true,
1125 .authzclient = true,
1126 .certipaddr = "127.0.0.1",
1127 };
1128 return test_migrate_tls_x509_start_common(from, to, &args);
1129 }
1130
1131 /*
1132 * The case with no client certificate presented,
1133 * and no server verification
1134 */
1135 static void *
test_migrate_tls_x509_start_allow_anon_client(QTestState * from,QTestState * to)1136 test_migrate_tls_x509_start_allow_anon_client(QTestState *from,
1137 QTestState *to)
1138 {
1139 TestMigrateTLSX509 args = {
1140 .certipaddr = "127.0.0.1",
1141 };
1142 return test_migrate_tls_x509_start_common(from, to, &args);
1143 }
1144
1145 /*
1146 * The case with no client certificate presented,
1147 * and server verification rejecting
1148 */
1149 static void *
test_migrate_tls_x509_start_reject_anon_client(QTestState * from,QTestState * to)1150 test_migrate_tls_x509_start_reject_anon_client(QTestState *from,
1151 QTestState *to)
1152 {
1153 TestMigrateTLSX509 args = {
1154 .verifyclient = true,
1155 .certipaddr = "127.0.0.1",
1156 };
1157 return test_migrate_tls_x509_start_common(from, to, &args);
1158 }
1159
1160 static void
test_migrate_tls_x509_finish(QTestState * from,QTestState * to,void * opaque)1161 test_migrate_tls_x509_finish(QTestState *from,
1162 QTestState *to,
1163 void *opaque)
1164 {
1165 TestMigrateTLSX509Data *data = opaque;
1166
1167 test_tls_cleanup(data->keyfile);
1168 g_free(data->keyfile);
1169
1170 unlink(data->cacert);
1171 g_free(data->cacert);
1172 unlink(data->servercert);
1173 g_free(data->servercert);
1174 unlink(data->serverkey);
1175 g_free(data->serverkey);
1176
1177 if (data->clientcert) {
1178 unlink(data->clientcert);
1179 g_free(data->clientcert);
1180 }
1181 if (data->clientkey) {
1182 unlink(data->clientkey);
1183 g_free(data->clientkey);
1184 }
1185
1186 rmdir(data->workdir);
1187 g_free(data->workdir);
1188
1189 g_free(data);
1190 }
1191 #endif /* CONFIG_TASN1 */
1192 #endif /* CONFIG_GNUTLS */
1193
migrate_postcopy_prepare(QTestState ** from_ptr,QTestState ** to_ptr,MigrateCommon * args)1194 static int migrate_postcopy_prepare(QTestState **from_ptr,
1195 QTestState **to_ptr,
1196 MigrateCommon *args)
1197 {
1198 QTestState *from, *to;
1199
1200 if (test_migrate_start(&from, &to, "defer", &args->start)) {
1201 return -1;
1202 }
1203
1204 if (args->start_hook) {
1205 args->postcopy_data = args->start_hook(from, to);
1206 }
1207
1208 migrate_set_capability(from, "postcopy-ram", true);
1209 migrate_set_capability(to, "postcopy-ram", true);
1210 migrate_set_capability(to, "postcopy-blocktime", true);
1211
1212 if (args->postcopy_preempt) {
1213 migrate_set_capability(from, "postcopy-preempt", true);
1214 migrate_set_capability(to, "postcopy-preempt", true);
1215 }
1216
1217 migrate_ensure_non_converge(from);
1218
1219 migrate_prepare_for_dirty_mem(from);
1220 qtest_qmp_assert_success(to, "{ 'execute': 'migrate-incoming',"
1221 " 'arguments': { "
1222 " 'channels': [ { 'channel-type': 'main',"
1223 " 'addr': { 'transport': 'socket',"
1224 " 'type': 'inet',"
1225 " 'host': '127.0.0.1',"
1226 " 'port': '0' } } ] } }");
1227
1228 /* Wait for the first serial output from the source */
1229 wait_for_serial("src_serial");
1230 wait_for_suspend(from, &src_state);
1231
1232 migrate_qmp(from, to, NULL, NULL, "{}");
1233
1234 migrate_wait_for_dirty_mem(from, to);
1235
1236 *from_ptr = from;
1237 *to_ptr = to;
1238
1239 return 0;
1240 }
1241
migrate_postcopy_complete(QTestState * from,QTestState * to,MigrateCommon * args)1242 static void migrate_postcopy_complete(QTestState *from, QTestState *to,
1243 MigrateCommon *args)
1244 {
1245 wait_for_migration_complete(from);
1246
1247 if (args->start.suspend_me) {
1248 /* wakeup succeeds only if guest is suspended */
1249 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
1250 }
1251
1252 /* Make sure we get at least one "B" on destination */
1253 wait_for_serial("dest_serial");
1254
1255 if (uffd_feature_thread_id) {
1256 read_blocktime(to);
1257 }
1258
1259 if (args->finish_hook) {
1260 args->finish_hook(from, to, args->postcopy_data);
1261 args->postcopy_data = NULL;
1262 }
1263
1264 test_migrate_end(from, to, true);
1265 }
1266
test_postcopy_common(MigrateCommon * args)1267 static void test_postcopy_common(MigrateCommon *args)
1268 {
1269 QTestState *from, *to;
1270
1271 if (migrate_postcopy_prepare(&from, &to, args)) {
1272 return;
1273 }
1274 migrate_postcopy_start(from, to);
1275 migrate_postcopy_complete(from, to, args);
1276 }
1277
test_postcopy(void)1278 static void test_postcopy(void)
1279 {
1280 MigrateCommon args = { };
1281
1282 test_postcopy_common(&args);
1283 }
1284
test_postcopy_suspend(void)1285 static void test_postcopy_suspend(void)
1286 {
1287 MigrateCommon args = {
1288 .start.suspend_me = true,
1289 };
1290
1291 test_postcopy_common(&args);
1292 }
1293
test_postcopy_preempt(void)1294 static void test_postcopy_preempt(void)
1295 {
1296 MigrateCommon args = {
1297 .postcopy_preempt = true,
1298 };
1299
1300 test_postcopy_common(&args);
1301 }
1302
1303 #ifdef CONFIG_GNUTLS
test_postcopy_tls_psk(void)1304 static void test_postcopy_tls_psk(void)
1305 {
1306 MigrateCommon args = {
1307 .start_hook = test_migrate_tls_psk_start_match,
1308 .finish_hook = test_migrate_tls_psk_finish,
1309 };
1310
1311 test_postcopy_common(&args);
1312 }
1313
test_postcopy_preempt_tls_psk(void)1314 static void test_postcopy_preempt_tls_psk(void)
1315 {
1316 MigrateCommon args = {
1317 .postcopy_preempt = true,
1318 .start_hook = test_migrate_tls_psk_start_match,
1319 .finish_hook = test_migrate_tls_psk_finish,
1320 };
1321
1322 test_postcopy_common(&args);
1323 }
1324 #endif
1325
wait_for_postcopy_status(QTestState * one,const char * status)1326 static void wait_for_postcopy_status(QTestState *one, const char *status)
1327 {
1328 wait_for_migration_status(one, status,
1329 (const char * []) { "failed", "active",
1330 "completed", NULL });
1331 }
1332
1333 #ifndef _WIN32
postcopy_recover_fail(QTestState * from,QTestState * to)1334 static void postcopy_recover_fail(QTestState *from, QTestState *to)
1335 {
1336 int ret, pair1[2], pair2[2];
1337 char c;
1338
1339 /* Create two unrelated socketpairs */
1340 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair1);
1341 g_assert_cmpint(ret, ==, 0);
1342
1343 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair2);
1344 g_assert_cmpint(ret, ==, 0);
1345
1346 /*
1347 * Give the guests unpaired ends of the sockets, so they'll all blocked
1348 * at reading. This mimics a wrong channel established.
1349 */
1350 qtest_qmp_fds_assert_success(from, &pair1[0], 1,
1351 "{ 'execute': 'getfd',"
1352 " 'arguments': { 'fdname': 'fd-mig' }}");
1353 qtest_qmp_fds_assert_success(to, &pair2[0], 1,
1354 "{ 'execute': 'getfd',"
1355 " 'arguments': { 'fdname': 'fd-mig' }}");
1356
1357 /*
1358 * Write the 1st byte as QEMU_VM_COMMAND (0x8) for the dest socket, to
1359 * emulate the 1st byte of a real recovery, but stops from there to
1360 * keep dest QEMU in RECOVER. This is needed so that we can kick off
1361 * the recover process on dest QEMU (by triggering the G_IO_IN event).
1362 *
1363 * NOTE: this trick is not needed on src QEMUs, because src doesn't
1364 * rely on an pre-existing G_IO_IN event, so it will always trigger the
1365 * upcoming recovery anyway even if it can read nothing.
1366 */
1367 #define QEMU_VM_COMMAND 0x08
1368 c = QEMU_VM_COMMAND;
1369 ret = send(pair2[1], &c, 1, 0);
1370 g_assert_cmpint(ret, ==, 1);
1371
1372 migrate_recover(to, "fd:fd-mig");
1373 migrate_qmp(from, to, "fd:fd-mig", NULL, "{'resume': true}");
1374
1375 /*
1376 * Make sure both QEMU instances will go into RECOVER stage, then test
1377 * kicking them out using migrate-pause.
1378 */
1379 wait_for_postcopy_status(from, "postcopy-recover");
1380 wait_for_postcopy_status(to, "postcopy-recover");
1381
1382 /*
1383 * This would be issued by the admin upon noticing the hang, we should
1384 * make sure we're able to kick this out.
1385 */
1386 migrate_pause(from);
1387 wait_for_postcopy_status(from, "postcopy-paused");
1388
1389 /* Do the same test on dest */
1390 migrate_pause(to);
1391 wait_for_postcopy_status(to, "postcopy-paused");
1392
1393 close(pair1[0]);
1394 close(pair1[1]);
1395 close(pair2[0]);
1396 close(pair2[1]);
1397 }
1398 #endif /* _WIN32 */
1399
test_postcopy_recovery_common(MigrateCommon * args)1400 static void test_postcopy_recovery_common(MigrateCommon *args)
1401 {
1402 QTestState *from, *to;
1403 g_autofree char *uri = NULL;
1404
1405 /* Always hide errors for postcopy recover tests since they're expected */
1406 args->start.hide_stderr = true;
1407
1408 if (migrate_postcopy_prepare(&from, &to, args)) {
1409 return;
1410 }
1411
1412 /* Turn postcopy speed down, 4K/s is slow enough on any machines */
1413 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
1414
1415 /* Now we start the postcopy */
1416 migrate_postcopy_start(from, to);
1417
1418 /*
1419 * Wait until postcopy is really started; we can only run the
1420 * migrate-pause command during a postcopy
1421 */
1422 wait_for_migration_status(from, "postcopy-active", NULL);
1423
1424 /*
1425 * Manually stop the postcopy migration. This emulates a network
1426 * failure with the migration socket
1427 */
1428 migrate_pause(from);
1429
1430 /*
1431 * Wait for destination side to reach postcopy-paused state. The
1432 * migrate-recover command can only succeed if destination machine
1433 * is in the paused state
1434 */
1435 wait_for_postcopy_status(to, "postcopy-paused");
1436 wait_for_postcopy_status(from, "postcopy-paused");
1437
1438 #ifndef _WIN32
1439 if (args->postcopy_recovery_test_fail) {
1440 /*
1441 * Test when a wrong socket specified for recover, and then the
1442 * ability to kick it out, and continue with a correct socket.
1443 */
1444 postcopy_recover_fail(from, to);
1445 /* continue with a good recovery */
1446 }
1447 #endif /* _WIN32 */
1448
1449 /*
1450 * Create a new socket to emulate a new channel that is different
1451 * from the broken migration channel; tell the destination to
1452 * listen to the new port
1453 */
1454 uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
1455 migrate_recover(to, uri);
1456
1457 /*
1458 * Try to rebuild the migration channel using the resume flag and
1459 * the newly created channel
1460 */
1461 migrate_qmp(from, to, uri, NULL, "{'resume': true}");
1462
1463 /* Restore the postcopy bandwidth to unlimited */
1464 migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
1465
1466 migrate_postcopy_complete(from, to, args);
1467 }
1468
test_postcopy_recovery(void)1469 static void test_postcopy_recovery(void)
1470 {
1471 MigrateCommon args = { };
1472
1473 test_postcopy_recovery_common(&args);
1474 }
1475
1476 #ifndef _WIN32
test_postcopy_recovery_double_fail(void)1477 static void test_postcopy_recovery_double_fail(void)
1478 {
1479 MigrateCommon args = {
1480 .postcopy_recovery_test_fail = true,
1481 };
1482
1483 test_postcopy_recovery_common(&args);
1484 }
1485 #endif /* _WIN32 */
1486
1487 #ifdef CONFIG_GNUTLS
test_postcopy_recovery_tls_psk(void)1488 static void test_postcopy_recovery_tls_psk(void)
1489 {
1490 MigrateCommon args = {
1491 .start_hook = test_migrate_tls_psk_start_match,
1492 .finish_hook = test_migrate_tls_psk_finish,
1493 };
1494
1495 test_postcopy_recovery_common(&args);
1496 }
1497 #endif
1498
test_postcopy_preempt_recovery(void)1499 static void test_postcopy_preempt_recovery(void)
1500 {
1501 MigrateCommon args = {
1502 .postcopy_preempt = true,
1503 };
1504
1505 test_postcopy_recovery_common(&args);
1506 }
1507
1508 #ifdef CONFIG_GNUTLS
1509 /* This contains preempt+recovery+tls test altogether */
test_postcopy_preempt_all(void)1510 static void test_postcopy_preempt_all(void)
1511 {
1512 MigrateCommon args = {
1513 .postcopy_preempt = true,
1514 .start_hook = test_migrate_tls_psk_start_match,
1515 .finish_hook = test_migrate_tls_psk_finish,
1516 };
1517
1518 test_postcopy_recovery_common(&args);
1519 }
1520
1521 #endif
1522
test_baddest(void)1523 static void test_baddest(void)
1524 {
1525 MigrateStart args = {
1526 .hide_stderr = true
1527 };
1528 QTestState *from, *to;
1529
1530 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1531 return;
1532 }
1533 migrate_qmp(from, to, "tcp:127.0.0.1:0", NULL, "{}");
1534 wait_for_migration_fail(from, false);
1535 test_migrate_end(from, to, false);
1536 }
1537
1538 #ifndef _WIN32
test_analyze_script(void)1539 static void test_analyze_script(void)
1540 {
1541 MigrateStart args = {
1542 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
1543 };
1544 QTestState *from, *to;
1545 g_autofree char *uri = NULL;
1546 g_autofree char *file = NULL;
1547 int pid, wstatus;
1548 const char *python = g_getenv("PYTHON");
1549
1550 if (!python) {
1551 g_test_skip("PYTHON variable not set");
1552 return;
1553 }
1554
1555 /* dummy url */
1556 if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", &args)) {
1557 return;
1558 }
1559
1560 /*
1561 * Setting these two capabilities causes the "configuration"
1562 * vmstate to include subsections for them. The script needs to
1563 * parse those subsections properly.
1564 */
1565 migrate_set_capability(from, "validate-uuid", true);
1566 migrate_set_capability(from, "x-ignore-shared", true);
1567
1568 file = g_strdup_printf("%s/migfile", tmpfs);
1569 uri = g_strdup_printf("exec:cat > %s", file);
1570
1571 migrate_ensure_converge(from);
1572 migrate_qmp(from, to, uri, NULL, "{}");
1573 wait_for_migration_complete(from);
1574
1575 pid = fork();
1576 if (!pid) {
1577 close(1);
1578 open("/dev/null", O_WRONLY);
1579 execl(python, python, ANALYZE_SCRIPT, "-f", file, NULL);
1580 g_assert_not_reached();
1581 }
1582
1583 g_assert(waitpid(pid, &wstatus, 0) == pid);
1584 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
1585 g_test_message("Failed to analyze the migration stream");
1586 g_test_fail();
1587 }
1588 test_migrate_end(from, to, false);
1589 cleanup("migfile");
1590 }
1591 #endif
1592
test_precopy_common(MigrateCommon * args)1593 static void test_precopy_common(MigrateCommon *args)
1594 {
1595 QTestState *from, *to;
1596 void *data_hook = NULL;
1597
1598 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1599 return;
1600 }
1601
1602 if (args->start_hook) {
1603 data_hook = args->start_hook(from, to);
1604 }
1605
1606 /* Wait for the first serial output from the source */
1607 if (args->result == MIG_TEST_SUCCEED) {
1608 wait_for_serial("src_serial");
1609 wait_for_suspend(from, &src_state);
1610 }
1611
1612 if (args->live) {
1613 migrate_ensure_non_converge(from);
1614 migrate_prepare_for_dirty_mem(from);
1615 } else {
1616 /*
1617 * Testing non-live migration, we allow it to run at
1618 * full speed to ensure short test case duration.
1619 * For tests expected to fail, we don't need to
1620 * change anything.
1621 */
1622 if (args->result == MIG_TEST_SUCCEED) {
1623 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1624 wait_for_stop(from, &src_state);
1625 migrate_ensure_converge(from);
1626 }
1627 }
1628
1629 if (args->result == MIG_TEST_QMP_ERROR) {
1630 migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
1631 goto finish;
1632 }
1633
1634 migrate_qmp(from, to, args->connect_uri, args->connect_channels, "{}");
1635
1636 if (args->result != MIG_TEST_SUCCEED) {
1637 bool allow_active = args->result == MIG_TEST_FAIL;
1638 wait_for_migration_fail(from, allow_active);
1639
1640 if (args->result == MIG_TEST_FAIL_DEST_QUIT_ERR) {
1641 qtest_set_expected_status(to, EXIT_FAILURE);
1642 }
1643 } else {
1644 if (args->live) {
1645 /*
1646 * For initial iteration(s) we must do a full pass,
1647 * but for the final iteration, we need only wait
1648 * for some dirty mem before switching to converge
1649 */
1650 while (args->iterations > 1) {
1651 wait_for_migration_pass(from);
1652 args->iterations--;
1653 }
1654 migrate_wait_for_dirty_mem(from, to);
1655
1656 migrate_ensure_converge(from);
1657
1658 /*
1659 * We do this first, as it has a timeout to stop us
1660 * hanging forever if migration didn't converge
1661 */
1662 wait_for_migration_complete(from);
1663
1664 wait_for_stop(from, &src_state);
1665
1666 } else {
1667 wait_for_migration_complete(from);
1668 /*
1669 * Must wait for dst to finish reading all incoming
1670 * data on the socket before issuing 'cont' otherwise
1671 * it'll be ignored
1672 */
1673 wait_for_migration_complete(to);
1674
1675 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1676 }
1677
1678 wait_for_resume(to, &dst_state);
1679
1680 if (args->start.suspend_me) {
1681 /* wakeup succeeds only if guest is suspended */
1682 qtest_qmp_assert_success(to, "{'execute': 'system_wakeup'}");
1683 }
1684
1685 wait_for_serial("dest_serial");
1686 }
1687
1688 finish:
1689 if (args->finish_hook) {
1690 args->finish_hook(from, to, data_hook);
1691 }
1692
1693 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1694 }
1695
test_file_common(MigrateCommon * args,bool stop_src)1696 static void test_file_common(MigrateCommon *args, bool stop_src)
1697 {
1698 QTestState *from, *to;
1699 void *data_hook = NULL;
1700
1701 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
1702 return;
1703 }
1704
1705 /*
1706 * File migration is never live. We can keep the source VM running
1707 * during migration, but the destination will not be running
1708 * concurrently.
1709 */
1710 g_assert_false(args->live);
1711
1712 if (args->start_hook) {
1713 data_hook = args->start_hook(from, to);
1714 }
1715
1716 migrate_ensure_converge(from);
1717 wait_for_serial("src_serial");
1718
1719 if (stop_src) {
1720 qtest_qmp_assert_success(from, "{ 'execute' : 'stop'}");
1721 wait_for_stop(from, &src_state);
1722 }
1723
1724 if (args->result == MIG_TEST_QMP_ERROR) {
1725 migrate_qmp_fail(from, args->connect_uri, NULL, "{}");
1726 goto finish;
1727 }
1728
1729 migrate_qmp(from, to, args->connect_uri, NULL, "{}");
1730 wait_for_migration_complete(from);
1731
1732 /*
1733 * We need to wait for the source to finish before starting the
1734 * destination.
1735 */
1736 migrate_incoming_qmp(to, args->connect_uri, "{}");
1737 wait_for_migration_complete(to);
1738
1739 if (stop_src) {
1740 qtest_qmp_assert_success(to, "{ 'execute' : 'cont'}");
1741 }
1742 wait_for_resume(to, &dst_state);
1743
1744 wait_for_serial("dest_serial");
1745
1746 finish:
1747 if (args->finish_hook) {
1748 args->finish_hook(from, to, data_hook);
1749 }
1750
1751 test_migrate_end(from, to, args->result == MIG_TEST_SUCCEED);
1752 }
1753
test_precopy_unix_plain(void)1754 static void test_precopy_unix_plain(void)
1755 {
1756 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1757 MigrateCommon args = {
1758 .listen_uri = uri,
1759 .connect_uri = uri,
1760 /*
1761 * The simplest use case of precopy, covering smoke tests of
1762 * get-dirty-log dirty tracking.
1763 */
1764 .live = true,
1765 };
1766
1767 test_precopy_common(&args);
1768 }
1769
test_precopy_unix_suspend_live(void)1770 static void test_precopy_unix_suspend_live(void)
1771 {
1772 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1773 MigrateCommon args = {
1774 .listen_uri = uri,
1775 .connect_uri = uri,
1776 /*
1777 * despite being live, the test is fast because the src
1778 * suspends immediately.
1779 */
1780 .live = true,
1781 .start.suspend_me = true,
1782 };
1783
1784 test_precopy_common(&args);
1785 }
1786
test_precopy_unix_suspend_notlive(void)1787 static void test_precopy_unix_suspend_notlive(void)
1788 {
1789 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1790 MigrateCommon args = {
1791 .listen_uri = uri,
1792 .connect_uri = uri,
1793 .start.suspend_me = true,
1794 };
1795
1796 test_precopy_common(&args);
1797 }
1798
test_precopy_unix_dirty_ring(void)1799 static void test_precopy_unix_dirty_ring(void)
1800 {
1801 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1802 MigrateCommon args = {
1803 .start = {
1804 .use_dirty_ring = true,
1805 },
1806 .listen_uri = uri,
1807 .connect_uri = uri,
1808 /*
1809 * Besides the precopy/unix basic test, cover dirty ring interface
1810 * rather than get-dirty-log.
1811 */
1812 .live = true,
1813 };
1814
1815 test_precopy_common(&args);
1816 }
1817
1818 #ifdef CONFIG_GNUTLS
test_precopy_unix_tls_psk(void)1819 static void test_precopy_unix_tls_psk(void)
1820 {
1821 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1822 MigrateCommon args = {
1823 .connect_uri = uri,
1824 .listen_uri = uri,
1825 .start_hook = test_migrate_tls_psk_start_match,
1826 .finish_hook = test_migrate_tls_psk_finish,
1827 };
1828
1829 test_precopy_common(&args);
1830 }
1831
1832 #ifdef CONFIG_TASN1
test_precopy_unix_tls_x509_default_host(void)1833 static void test_precopy_unix_tls_x509_default_host(void)
1834 {
1835 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1836 MigrateCommon args = {
1837 .start = {
1838 .hide_stderr = true,
1839 },
1840 .connect_uri = uri,
1841 .listen_uri = uri,
1842 .start_hook = test_migrate_tls_x509_start_default_host,
1843 .finish_hook = test_migrate_tls_x509_finish,
1844 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
1845 };
1846
1847 test_precopy_common(&args);
1848 }
1849
test_precopy_unix_tls_x509_override_host(void)1850 static void test_precopy_unix_tls_x509_override_host(void)
1851 {
1852 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1853 MigrateCommon args = {
1854 .connect_uri = uri,
1855 .listen_uri = uri,
1856 .start_hook = test_migrate_tls_x509_start_override_host,
1857 .finish_hook = test_migrate_tls_x509_finish,
1858 };
1859
1860 test_precopy_common(&args);
1861 }
1862 #endif /* CONFIG_TASN1 */
1863 #endif /* CONFIG_GNUTLS */
1864
1865 #if 0
1866 /* Currently upset on aarch64 TCG */
1867 static void test_ignore_shared(void)
1868 {
1869 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1870 QTestState *from, *to;
1871
1872 if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
1873 return;
1874 }
1875
1876 migrate_ensure_non_converge(from);
1877 migrate_prepare_for_dirty_mem(from);
1878
1879 migrate_set_capability(from, "x-ignore-shared", true);
1880 migrate_set_capability(to, "x-ignore-shared", true);
1881
1882 /* Wait for the first serial output from the source */
1883 wait_for_serial("src_serial");
1884
1885 migrate_qmp(from, to, uri, NULL, "{}");
1886
1887 migrate_wait_for_dirty_mem(from, to);
1888
1889 wait_for_stop(from, &src_state);
1890
1891 qtest_qmp_eventwait(to, "RESUME");
1892
1893 wait_for_serial("dest_serial");
1894 wait_for_migration_complete(from);
1895
1896 /* Check whether shared RAM has been really skipped */
1897 g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
1898
1899 test_migrate_end(from, to, true);
1900 }
1901 #endif
1902
1903 static void *
test_migrate_xbzrle_start(QTestState * from,QTestState * to)1904 test_migrate_xbzrle_start(QTestState *from,
1905 QTestState *to)
1906 {
1907 migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
1908
1909 migrate_set_capability(from, "xbzrle", true);
1910 migrate_set_capability(to, "xbzrle", true);
1911
1912 return NULL;
1913 }
1914
test_precopy_unix_xbzrle(void)1915 static void test_precopy_unix_xbzrle(void)
1916 {
1917 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
1918 MigrateCommon args = {
1919 .connect_uri = uri,
1920 .listen_uri = uri,
1921 .start_hook = test_migrate_xbzrle_start,
1922 .iterations = 2,
1923 /*
1924 * XBZRLE needs pages to be modified when doing the 2nd+ round
1925 * iteration to have real data pushed to the stream.
1926 */
1927 .live = true,
1928 };
1929
1930 test_precopy_common(&args);
1931 }
1932
test_precopy_file(void)1933 static void test_precopy_file(void)
1934 {
1935 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
1936 FILE_TEST_FILENAME);
1937 MigrateCommon args = {
1938 .connect_uri = uri,
1939 .listen_uri = "defer",
1940 };
1941
1942 test_file_common(&args, true);
1943 }
1944
file_offset_finish_hook(QTestState * from,QTestState * to,void * opaque)1945 static void file_offset_finish_hook(QTestState *from, QTestState *to,
1946 void *opaque)
1947 {
1948 #if defined(__linux__)
1949 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
1950 size_t size = FILE_TEST_OFFSET + sizeof(QEMU_VM_FILE_MAGIC);
1951 uintptr_t *addr, *p;
1952 int fd;
1953
1954 fd = open(path, O_RDONLY);
1955 g_assert(fd != -1);
1956 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1957 g_assert(addr != MAP_FAILED);
1958
1959 /*
1960 * Ensure the skipped offset contains zeros and the migration
1961 * stream starts at the right place.
1962 */
1963 p = addr;
1964 while (p < addr + FILE_TEST_OFFSET / sizeof(uintptr_t)) {
1965 g_assert(*p == 0);
1966 p++;
1967 }
1968 g_assert_cmpint(cpu_to_be64(*p) >> 32, ==, QEMU_VM_FILE_MAGIC);
1969
1970 munmap(addr, size);
1971 close(fd);
1972 #endif
1973 }
1974
test_precopy_file_offset(void)1975 static void test_precopy_file_offset(void)
1976 {
1977 g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=%d", tmpfs,
1978 FILE_TEST_FILENAME,
1979 FILE_TEST_OFFSET);
1980 MigrateCommon args = {
1981 .connect_uri = uri,
1982 .listen_uri = "defer",
1983 .finish_hook = file_offset_finish_hook,
1984 };
1985
1986 test_file_common(&args, false);
1987 }
1988
test_precopy_file_offset_bad(void)1989 static void test_precopy_file_offset_bad(void)
1990 {
1991 /* using a value not supported by qemu_strtosz() */
1992 g_autofree char *uri = g_strdup_printf("file:%s/%s,offset=0x20M",
1993 tmpfs, FILE_TEST_FILENAME);
1994 MigrateCommon args = {
1995 .connect_uri = uri,
1996 .listen_uri = "defer",
1997 .result = MIG_TEST_QMP_ERROR,
1998 };
1999
2000 test_file_common(&args, false);
2001 }
2002
test_mode_reboot_start(QTestState * from,QTestState * to)2003 static void *test_mode_reboot_start(QTestState *from, QTestState *to)
2004 {
2005 migrate_set_parameter_str(from, "mode", "cpr-reboot");
2006 migrate_set_parameter_str(to, "mode", "cpr-reboot");
2007
2008 migrate_set_capability(from, "x-ignore-shared", true);
2009 migrate_set_capability(to, "x-ignore-shared", true);
2010
2011 return NULL;
2012 }
2013
migrate_mapped_ram_start(QTestState * from,QTestState * to)2014 static void *migrate_mapped_ram_start(QTestState *from, QTestState *to)
2015 {
2016 migrate_set_capability(from, "mapped-ram", true);
2017 migrate_set_capability(to, "mapped-ram", true);
2018
2019 return NULL;
2020 }
2021
test_mode_reboot(void)2022 static void test_mode_reboot(void)
2023 {
2024 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2025 FILE_TEST_FILENAME);
2026 MigrateCommon args = {
2027 .start.use_shmem = true,
2028 .connect_uri = uri,
2029 .listen_uri = "defer",
2030 .start_hook = test_mode_reboot_start
2031 };
2032
2033 test_file_common(&args, true);
2034 }
2035
test_precopy_file_mapped_ram_live(void)2036 static void test_precopy_file_mapped_ram_live(void)
2037 {
2038 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2039 FILE_TEST_FILENAME);
2040 MigrateCommon args = {
2041 .connect_uri = uri,
2042 .listen_uri = "defer",
2043 .start_hook = migrate_mapped_ram_start,
2044 };
2045
2046 test_file_common(&args, false);
2047 }
2048
test_precopy_file_mapped_ram(void)2049 static void test_precopy_file_mapped_ram(void)
2050 {
2051 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2052 FILE_TEST_FILENAME);
2053 MigrateCommon args = {
2054 .connect_uri = uri,
2055 .listen_uri = "defer",
2056 .start_hook = migrate_mapped_ram_start,
2057 };
2058
2059 test_file_common(&args, true);
2060 }
2061
migrate_multifd_mapped_ram_start(QTestState * from,QTestState * to)2062 static void *migrate_multifd_mapped_ram_start(QTestState *from, QTestState *to)
2063 {
2064 migrate_mapped_ram_start(from, to);
2065
2066 migrate_set_parameter_int(from, "multifd-channels", 4);
2067 migrate_set_parameter_int(to, "multifd-channels", 4);
2068
2069 migrate_set_capability(from, "multifd", true);
2070 migrate_set_capability(to, "multifd", true);
2071
2072 return NULL;
2073 }
2074
test_multifd_file_mapped_ram_live(void)2075 static void test_multifd_file_mapped_ram_live(void)
2076 {
2077 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2078 FILE_TEST_FILENAME);
2079 MigrateCommon args = {
2080 .connect_uri = uri,
2081 .listen_uri = "defer",
2082 .start_hook = migrate_multifd_mapped_ram_start,
2083 };
2084
2085 test_file_common(&args, false);
2086 }
2087
test_multifd_file_mapped_ram(void)2088 static void test_multifd_file_mapped_ram(void)
2089 {
2090 g_autofree char *uri = g_strdup_printf("file:%s/%s", tmpfs,
2091 FILE_TEST_FILENAME);
2092 MigrateCommon args = {
2093 .connect_uri = uri,
2094 .listen_uri = "defer",
2095 .start_hook = migrate_multifd_mapped_ram_start,
2096 };
2097
2098 test_file_common(&args, true);
2099 }
2100
2101
test_precopy_tcp_plain(void)2102 static void test_precopy_tcp_plain(void)
2103 {
2104 MigrateCommon args = {
2105 .listen_uri = "tcp:127.0.0.1:0",
2106 };
2107
2108 test_precopy_common(&args);
2109 }
2110
test_migrate_switchover_ack_start(QTestState * from,QTestState * to)2111 static void *test_migrate_switchover_ack_start(QTestState *from, QTestState *to)
2112 {
2113
2114 migrate_set_capability(from, "return-path", true);
2115 migrate_set_capability(to, "return-path", true);
2116
2117 migrate_set_capability(from, "switchover-ack", true);
2118 migrate_set_capability(to, "switchover-ack", true);
2119
2120 return NULL;
2121 }
2122
test_precopy_tcp_switchover_ack(void)2123 static void test_precopy_tcp_switchover_ack(void)
2124 {
2125 MigrateCommon args = {
2126 .listen_uri = "tcp:127.0.0.1:0",
2127 .start_hook = test_migrate_switchover_ack_start,
2128 /*
2129 * Source VM must be running in order to consider the switchover ACK
2130 * when deciding to do switchover or not.
2131 */
2132 .live = true,
2133 };
2134
2135 test_precopy_common(&args);
2136 }
2137
2138 #ifdef CONFIG_GNUTLS
test_precopy_tcp_tls_psk_match(void)2139 static void test_precopy_tcp_tls_psk_match(void)
2140 {
2141 MigrateCommon args = {
2142 .listen_uri = "tcp:127.0.0.1:0",
2143 .start_hook = test_migrate_tls_psk_start_match,
2144 .finish_hook = test_migrate_tls_psk_finish,
2145 };
2146
2147 test_precopy_common(&args);
2148 }
2149
test_precopy_tcp_tls_psk_mismatch(void)2150 static void test_precopy_tcp_tls_psk_mismatch(void)
2151 {
2152 MigrateCommon args = {
2153 .start = {
2154 .hide_stderr = true,
2155 },
2156 .listen_uri = "tcp:127.0.0.1:0",
2157 .start_hook = test_migrate_tls_psk_start_mismatch,
2158 .finish_hook = test_migrate_tls_psk_finish,
2159 .result = MIG_TEST_FAIL,
2160 };
2161
2162 test_precopy_common(&args);
2163 }
2164
2165 #ifdef CONFIG_TASN1
test_precopy_tcp_tls_x509_default_host(void)2166 static void test_precopy_tcp_tls_x509_default_host(void)
2167 {
2168 MigrateCommon args = {
2169 .listen_uri = "tcp:127.0.0.1:0",
2170 .start_hook = test_migrate_tls_x509_start_default_host,
2171 .finish_hook = test_migrate_tls_x509_finish,
2172 };
2173
2174 test_precopy_common(&args);
2175 }
2176
test_precopy_tcp_tls_x509_override_host(void)2177 static void test_precopy_tcp_tls_x509_override_host(void)
2178 {
2179 MigrateCommon args = {
2180 .listen_uri = "tcp:127.0.0.1:0",
2181 .start_hook = test_migrate_tls_x509_start_override_host,
2182 .finish_hook = test_migrate_tls_x509_finish,
2183 };
2184
2185 test_precopy_common(&args);
2186 }
2187
test_precopy_tcp_tls_x509_mismatch_host(void)2188 static void test_precopy_tcp_tls_x509_mismatch_host(void)
2189 {
2190 MigrateCommon args = {
2191 .start = {
2192 .hide_stderr = true,
2193 },
2194 .listen_uri = "tcp:127.0.0.1:0",
2195 .start_hook = test_migrate_tls_x509_start_mismatch_host,
2196 .finish_hook = test_migrate_tls_x509_finish,
2197 .result = MIG_TEST_FAIL_DEST_QUIT_ERR,
2198 };
2199
2200 test_precopy_common(&args);
2201 }
2202
test_precopy_tcp_tls_x509_friendly_client(void)2203 static void test_precopy_tcp_tls_x509_friendly_client(void)
2204 {
2205 MigrateCommon args = {
2206 .listen_uri = "tcp:127.0.0.1:0",
2207 .start_hook = test_migrate_tls_x509_start_friendly_client,
2208 .finish_hook = test_migrate_tls_x509_finish,
2209 };
2210
2211 test_precopy_common(&args);
2212 }
2213
test_precopy_tcp_tls_x509_hostile_client(void)2214 static void test_precopy_tcp_tls_x509_hostile_client(void)
2215 {
2216 MigrateCommon args = {
2217 .start = {
2218 .hide_stderr = true,
2219 },
2220 .listen_uri = "tcp:127.0.0.1:0",
2221 .start_hook = test_migrate_tls_x509_start_hostile_client,
2222 .finish_hook = test_migrate_tls_x509_finish,
2223 .result = MIG_TEST_FAIL,
2224 };
2225
2226 test_precopy_common(&args);
2227 }
2228
test_precopy_tcp_tls_x509_allow_anon_client(void)2229 static void test_precopy_tcp_tls_x509_allow_anon_client(void)
2230 {
2231 MigrateCommon args = {
2232 .listen_uri = "tcp:127.0.0.1:0",
2233 .start_hook = test_migrate_tls_x509_start_allow_anon_client,
2234 .finish_hook = test_migrate_tls_x509_finish,
2235 };
2236
2237 test_precopy_common(&args);
2238 }
2239
test_precopy_tcp_tls_x509_reject_anon_client(void)2240 static void test_precopy_tcp_tls_x509_reject_anon_client(void)
2241 {
2242 MigrateCommon args = {
2243 .start = {
2244 .hide_stderr = true,
2245 },
2246 .listen_uri = "tcp:127.0.0.1:0",
2247 .start_hook = test_migrate_tls_x509_start_reject_anon_client,
2248 .finish_hook = test_migrate_tls_x509_finish,
2249 .result = MIG_TEST_FAIL,
2250 };
2251
2252 test_precopy_common(&args);
2253 }
2254 #endif /* CONFIG_TASN1 */
2255 #endif /* CONFIG_GNUTLS */
2256
2257 #ifndef _WIN32
test_migrate_fd_start_hook(QTestState * from,QTestState * to)2258 static void *test_migrate_fd_start_hook(QTestState *from,
2259 QTestState *to)
2260 {
2261 int ret;
2262 int pair[2];
2263
2264 /* Create two connected sockets for migration */
2265 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
2266 g_assert_cmpint(ret, ==, 0);
2267
2268 /* Send the 1st socket to the target */
2269 qtest_qmp_fds_assert_success(to, &pair[0], 1,
2270 "{ 'execute': 'getfd',"
2271 " 'arguments': { 'fdname': 'fd-mig' }}");
2272 close(pair[0]);
2273
2274 /* Start incoming migration from the 1st socket */
2275 migrate_incoming_qmp(to, "fd:fd-mig", "{}");
2276
2277 /* Send the 2nd socket to the target */
2278 qtest_qmp_fds_assert_success(from, &pair[1], 1,
2279 "{ 'execute': 'getfd',"
2280 " 'arguments': { 'fdname': 'fd-mig' }}");
2281 close(pair[1]);
2282
2283 return NULL;
2284 }
2285
test_migrate_fd_finish_hook(QTestState * from,QTestState * to,void * opaque)2286 static void test_migrate_fd_finish_hook(QTestState *from,
2287 QTestState *to,
2288 void *opaque)
2289 {
2290 QDict *rsp;
2291 const char *error_desc;
2292
2293 /* Test closing fds */
2294 /* We assume, that QEMU removes named fd from its list,
2295 * so this should fail */
2296 rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
2297 " 'arguments': { 'fdname': 'fd-mig' }}");
2298 g_assert_true(qdict_haskey(rsp, "error"));
2299 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2300 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2301 qobject_unref(rsp);
2302
2303 rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
2304 " 'arguments': { 'fdname': 'fd-mig' }}");
2305 g_assert_true(qdict_haskey(rsp, "error"));
2306 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
2307 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
2308 qobject_unref(rsp);
2309 }
2310
test_migrate_precopy_fd_socket(void)2311 static void test_migrate_precopy_fd_socket(void)
2312 {
2313 MigrateCommon args = {
2314 .listen_uri = "defer",
2315 .connect_uri = "fd:fd-mig",
2316 .start_hook = test_migrate_fd_start_hook,
2317 .finish_hook = test_migrate_fd_finish_hook
2318 };
2319 test_precopy_common(&args);
2320 }
2321
migrate_precopy_fd_file_start(QTestState * from,QTestState * to)2322 static void *migrate_precopy_fd_file_start(QTestState *from, QTestState *to)
2323 {
2324 g_autofree char *file = g_strdup_printf("%s/%s", tmpfs, FILE_TEST_FILENAME);
2325 int src_flags = O_CREAT | O_RDWR;
2326 int dst_flags = O_CREAT | O_RDWR;
2327 int fds[2];
2328
2329 fds[0] = open(file, src_flags, 0660);
2330 assert(fds[0] != -1);
2331
2332 fds[1] = open(file, dst_flags, 0660);
2333 assert(fds[1] != -1);
2334
2335
2336 qtest_qmp_fds_assert_success(to, &fds[0], 1,
2337 "{ 'execute': 'getfd',"
2338 " 'arguments': { 'fdname': 'fd-mig' }}");
2339
2340 qtest_qmp_fds_assert_success(from, &fds[1], 1,
2341 "{ 'execute': 'getfd',"
2342 " 'arguments': { 'fdname': 'fd-mig' }}");
2343
2344 close(fds[0]);
2345 close(fds[1]);
2346
2347 return NULL;
2348 }
2349
test_migrate_precopy_fd_file(void)2350 static void test_migrate_precopy_fd_file(void)
2351 {
2352 MigrateCommon args = {
2353 .listen_uri = "defer",
2354 .connect_uri = "fd:fd-mig",
2355 .start_hook = migrate_precopy_fd_file_start,
2356 .finish_hook = test_migrate_fd_finish_hook
2357 };
2358 test_file_common(&args, true);
2359 }
2360 #endif /* _WIN32 */
2361
do_test_validate_uuid(MigrateStart * args,bool should_fail)2362 static void do_test_validate_uuid(MigrateStart *args, bool should_fail)
2363 {
2364 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2365 QTestState *from, *to;
2366
2367 if (test_migrate_start(&from, &to, uri, args)) {
2368 return;
2369 }
2370
2371 /*
2372 * UUID validation is at the begin of migration. So, the main process of
2373 * migration is not interesting for us here. Thus, set huge downtime for
2374 * very fast migration.
2375 */
2376 migrate_set_parameter_int(from, "downtime-limit", 1000000);
2377 migrate_set_capability(from, "validate-uuid", true);
2378
2379 /* Wait for the first serial output from the source */
2380 wait_for_serial("src_serial");
2381
2382 migrate_qmp(from, to, uri, NULL, "{}");
2383
2384 if (should_fail) {
2385 qtest_set_expected_status(to, EXIT_FAILURE);
2386 wait_for_migration_fail(from, true);
2387 } else {
2388 wait_for_migration_complete(from);
2389 }
2390
2391 test_migrate_end(from, to, false);
2392 }
2393
test_validate_uuid(void)2394 static void test_validate_uuid(void)
2395 {
2396 MigrateStart args = {
2397 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2398 .opts_target = "-uuid 11111111-1111-1111-1111-111111111111",
2399 };
2400
2401 do_test_validate_uuid(&args, false);
2402 }
2403
test_validate_uuid_error(void)2404 static void test_validate_uuid_error(void)
2405 {
2406 MigrateStart args = {
2407 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2408 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2409 .hide_stderr = true,
2410 };
2411
2412 do_test_validate_uuid(&args, true);
2413 }
2414
test_validate_uuid_src_not_set(void)2415 static void test_validate_uuid_src_not_set(void)
2416 {
2417 MigrateStart args = {
2418 .opts_target = "-uuid 22222222-2222-2222-2222-222222222222",
2419 .hide_stderr = true,
2420 };
2421
2422 do_test_validate_uuid(&args, false);
2423 }
2424
test_validate_uuid_dst_not_set(void)2425 static void test_validate_uuid_dst_not_set(void)
2426 {
2427 MigrateStart args = {
2428 .opts_source = "-uuid 11111111-1111-1111-1111-111111111111",
2429 .hide_stderr = true,
2430 };
2431
2432 do_test_validate_uuid(&args, false);
2433 }
2434
do_test_validate_uri_channel(MigrateCommon * args)2435 static void do_test_validate_uri_channel(MigrateCommon *args)
2436 {
2437 QTestState *from, *to;
2438
2439 if (test_migrate_start(&from, &to, args->listen_uri, &args->start)) {
2440 return;
2441 }
2442
2443 /* Wait for the first serial output from the source */
2444 wait_for_serial("src_serial");
2445
2446 /*
2447 * 'uri' and 'channels' validation is checked even before the migration
2448 * starts.
2449 */
2450 migrate_qmp_fail(from, args->connect_uri, args->connect_channels, "{}");
2451 test_migrate_end(from, to, false);
2452 }
2453
test_validate_uri_channels_both_set(void)2454 static void test_validate_uri_channels_both_set(void)
2455 {
2456 MigrateCommon args = {
2457 .start = {
2458 .hide_stderr = true,
2459 },
2460 .listen_uri = "defer",
2461 .connect_uri = "tcp:127.0.0.1:0",
2462 .connect_channels = "[ { 'channel-type': 'main',"
2463 " 'addr': { 'transport': 'socket',"
2464 " 'type': 'inet',"
2465 " 'host': '127.0.0.1',"
2466 " 'port': '0' } } ]",
2467 };
2468
2469 do_test_validate_uri_channel(&args);
2470 }
2471
test_validate_uri_channels_none_set(void)2472 static void test_validate_uri_channels_none_set(void)
2473 {
2474 MigrateCommon args = {
2475 .start = {
2476 .hide_stderr = true,
2477 },
2478 .listen_uri = "defer",
2479 };
2480
2481 do_test_validate_uri_channel(&args);
2482 }
2483
2484 /*
2485 * The way auto_converge works, we need to do too many passes to
2486 * run this test. Auto_converge logic is only run once every
2487 * three iterations, so:
2488 *
2489 * - 3 iterations without auto_converge enabled
2490 * - 3 iterations with pct = 5
2491 * - 3 iterations with pct = 30
2492 * - 3 iterations with pct = 55
2493 * - 3 iterations with pct = 80
2494 * - 3 iterations with pct = 95 (max(95, 80 + 25))
2495 *
2496 * To make things even worse, we need to run the initial stage at
2497 * 3MB/s so we enter autoconverge even when host is (over)loaded.
2498 */
test_migrate_auto_converge(void)2499 static void test_migrate_auto_converge(void)
2500 {
2501 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
2502 MigrateStart args = {};
2503 QTestState *from, *to;
2504 int64_t percentage;
2505
2506 /*
2507 * We want the test to be stable and as fast as possible.
2508 * E.g., with 1Gb/s bandwidth migration may pass without throttling,
2509 * so we need to decrease a bandwidth.
2510 */
2511 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
2512
2513 if (test_migrate_start(&from, &to, uri, &args)) {
2514 return;
2515 }
2516
2517 migrate_set_capability(from, "auto-converge", true);
2518 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
2519 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
2520 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
2521
2522 /*
2523 * Set the initial parameters so that the migration could not converge
2524 * without throttling.
2525 */
2526 migrate_ensure_non_converge(from);
2527
2528 /* To check remaining size after precopy */
2529 migrate_set_capability(from, "pause-before-switchover", true);
2530
2531 /* Wait for the first serial output from the source */
2532 wait_for_serial("src_serial");
2533
2534 migrate_qmp(from, to, uri, NULL, "{}");
2535
2536 /* Wait for throttling begins */
2537 percentage = 0;
2538 do {
2539 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2540 if (percentage != 0) {
2541 break;
2542 }
2543 usleep(20);
2544 g_assert_false(src_state.stop_seen);
2545 } while (true);
2546 /* The first percentage of throttling should be at least init_pct */
2547 g_assert_cmpint(percentage, >=, init_pct);
2548 /* Now, when we tested that throttling works, let it converge */
2549 migrate_ensure_converge(from);
2550
2551 /*
2552 * Wait for pre-switchover status to check last throttle percentage
2553 * and remaining. These values will be zeroed later
2554 */
2555 wait_for_migration_status(from, "pre-switchover", NULL);
2556
2557 /* The final percentage of throttling shouldn't be greater than max_pct */
2558 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
2559 g_assert_cmpint(percentage, <=, max_pct);
2560 migrate_continue(from, "pre-switchover");
2561
2562 qtest_qmp_eventwait(to, "RESUME");
2563
2564 wait_for_serial("dest_serial");
2565 wait_for_migration_complete(from);
2566
2567 test_migrate_end(from, to, true);
2568 }
2569
2570 static void *
test_migrate_precopy_tcp_multifd_start_common(QTestState * from,QTestState * to,const char * method)2571 test_migrate_precopy_tcp_multifd_start_common(QTestState *from,
2572 QTestState *to,
2573 const char *method)
2574 {
2575 migrate_set_parameter_int(from, "multifd-channels", 16);
2576 migrate_set_parameter_int(to, "multifd-channels", 16);
2577
2578 migrate_set_parameter_str(from, "multifd-compression", method);
2579 migrate_set_parameter_str(to, "multifd-compression", method);
2580
2581 migrate_set_capability(from, "multifd", true);
2582 migrate_set_capability(to, "multifd", true);
2583
2584 /* Start incoming migration from the 1st socket */
2585 migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
2586
2587 return NULL;
2588 }
2589
2590 static void *
test_migrate_precopy_tcp_multifd_start(QTestState * from,QTestState * to)2591 test_migrate_precopy_tcp_multifd_start(QTestState *from,
2592 QTestState *to)
2593 {
2594 return test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2595 }
2596
2597 static void *
test_migrate_precopy_tcp_multifd_start_zero_page_legacy(QTestState * from,QTestState * to)2598 test_migrate_precopy_tcp_multifd_start_zero_page_legacy(QTestState *from,
2599 QTestState *to)
2600 {
2601 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2602 migrate_set_parameter_str(from, "zero-page-detection", "legacy");
2603 return NULL;
2604 }
2605
2606 static void *
test_migration_precopy_tcp_multifd_start_no_zero_page(QTestState * from,QTestState * to)2607 test_migration_precopy_tcp_multifd_start_no_zero_page(QTestState *from,
2608 QTestState *to)
2609 {
2610 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2611 migrate_set_parameter_str(from, "zero-page-detection", "none");
2612 return NULL;
2613 }
2614
2615 static void *
test_migrate_precopy_tcp_multifd_zlib_start(QTestState * from,QTestState * to)2616 test_migrate_precopy_tcp_multifd_zlib_start(QTestState *from,
2617 QTestState *to)
2618 {
2619 /*
2620 * Overloading this test to also check that set_parameter does not error.
2621 * This is also done in the tests for the other compression methods.
2622 */
2623 migrate_set_parameter_int(from, "multifd-zlib-level", 2);
2624 migrate_set_parameter_int(to, "multifd-zlib-level", 2);
2625
2626 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zlib");
2627 }
2628
2629 #ifdef CONFIG_ZSTD
2630 static void *
test_migrate_precopy_tcp_multifd_zstd_start(QTestState * from,QTestState * to)2631 test_migrate_precopy_tcp_multifd_zstd_start(QTestState *from,
2632 QTestState *to)
2633 {
2634 migrate_set_parameter_int(from, "multifd-zstd-level", 2);
2635 migrate_set_parameter_int(to, "multifd-zstd-level", 2);
2636
2637 return test_migrate_precopy_tcp_multifd_start_common(from, to, "zstd");
2638 }
2639 #endif /* CONFIG_ZSTD */
2640
2641 #ifdef CONFIG_QPL
2642 static void *
test_migrate_precopy_tcp_multifd_qpl_start(QTestState * from,QTestState * to)2643 test_migrate_precopy_tcp_multifd_qpl_start(QTestState *from,
2644 QTestState *to)
2645 {
2646 return test_migrate_precopy_tcp_multifd_start_common(from, to, "qpl");
2647 }
2648 #endif /* CONFIG_QPL */
2649 #ifdef CONFIG_UADK
2650 static void *
test_migrate_precopy_tcp_multifd_uadk_start(QTestState * from,QTestState * to)2651 test_migrate_precopy_tcp_multifd_uadk_start(QTestState *from,
2652 QTestState *to)
2653 {
2654 return test_migrate_precopy_tcp_multifd_start_common(from, to, "uadk");
2655 }
2656 #endif /* CONFIG_UADK */
2657
test_multifd_tcp_uri_none(void)2658 static void test_multifd_tcp_uri_none(void)
2659 {
2660 MigrateCommon args = {
2661 .listen_uri = "defer",
2662 .start_hook = test_migrate_precopy_tcp_multifd_start,
2663 /*
2664 * Multifd is more complicated than most of the features, it
2665 * directly takes guest page buffers when sending, make sure
2666 * everything will work alright even if guest page is changing.
2667 */
2668 .live = true,
2669 };
2670 test_precopy_common(&args);
2671 }
2672
test_multifd_tcp_zero_page_legacy(void)2673 static void test_multifd_tcp_zero_page_legacy(void)
2674 {
2675 MigrateCommon args = {
2676 .listen_uri = "defer",
2677 .start_hook = test_migrate_precopy_tcp_multifd_start_zero_page_legacy,
2678 /*
2679 * Multifd is more complicated than most of the features, it
2680 * directly takes guest page buffers when sending, make sure
2681 * everything will work alright even if guest page is changing.
2682 */
2683 .live = true,
2684 };
2685 test_precopy_common(&args);
2686 }
2687
test_multifd_tcp_no_zero_page(void)2688 static void test_multifd_tcp_no_zero_page(void)
2689 {
2690 MigrateCommon args = {
2691 .listen_uri = "defer",
2692 .start_hook = test_migration_precopy_tcp_multifd_start_no_zero_page,
2693 /*
2694 * Multifd is more complicated than most of the features, it
2695 * directly takes guest page buffers when sending, make sure
2696 * everything will work alright even if guest page is changing.
2697 */
2698 .live = true,
2699 };
2700 test_precopy_common(&args);
2701 }
2702
test_multifd_tcp_channels_none(void)2703 static void test_multifd_tcp_channels_none(void)
2704 {
2705 MigrateCommon args = {
2706 .listen_uri = "defer",
2707 .start_hook = test_migrate_precopy_tcp_multifd_start,
2708 .live = true,
2709 .connect_channels = "[ { 'channel-type': 'main',"
2710 " 'addr': { 'transport': 'socket',"
2711 " 'type': 'inet',"
2712 " 'host': '127.0.0.1',"
2713 " 'port': '0' } } ]",
2714 };
2715 test_precopy_common(&args);
2716 }
2717
test_multifd_tcp_zlib(void)2718 static void test_multifd_tcp_zlib(void)
2719 {
2720 MigrateCommon args = {
2721 .listen_uri = "defer",
2722 .start_hook = test_migrate_precopy_tcp_multifd_zlib_start,
2723 };
2724 test_precopy_common(&args);
2725 }
2726
2727 #ifdef CONFIG_ZSTD
test_multifd_tcp_zstd(void)2728 static void test_multifd_tcp_zstd(void)
2729 {
2730 MigrateCommon args = {
2731 .listen_uri = "defer",
2732 .start_hook = test_migrate_precopy_tcp_multifd_zstd_start,
2733 };
2734 test_precopy_common(&args);
2735 }
2736 #endif
2737
2738 #ifdef CONFIG_QPL
test_multifd_tcp_qpl(void)2739 static void test_multifd_tcp_qpl(void)
2740 {
2741 MigrateCommon args = {
2742 .listen_uri = "defer",
2743 .start_hook = test_migrate_precopy_tcp_multifd_qpl_start,
2744 };
2745 test_precopy_common(&args);
2746 }
2747 #endif
2748
2749 #ifdef CONFIG_UADK
test_multifd_tcp_uadk(void)2750 static void test_multifd_tcp_uadk(void)
2751 {
2752 MigrateCommon args = {
2753 .listen_uri = "defer",
2754 .start_hook = test_migrate_precopy_tcp_multifd_uadk_start,
2755 };
2756 test_precopy_common(&args);
2757 }
2758 #endif
2759
2760 #ifdef CONFIG_GNUTLS
2761 static void *
test_migrate_multifd_tcp_tls_psk_start_match(QTestState * from,QTestState * to)2762 test_migrate_multifd_tcp_tls_psk_start_match(QTestState *from,
2763 QTestState *to)
2764 {
2765 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2766 return test_migrate_tls_psk_start_match(from, to);
2767 }
2768
2769 static void *
test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState * from,QTestState * to)2770 test_migrate_multifd_tcp_tls_psk_start_mismatch(QTestState *from,
2771 QTestState *to)
2772 {
2773 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2774 return test_migrate_tls_psk_start_mismatch(from, to);
2775 }
2776
2777 #ifdef CONFIG_TASN1
2778 static void *
test_migrate_multifd_tls_x509_start_default_host(QTestState * from,QTestState * to)2779 test_migrate_multifd_tls_x509_start_default_host(QTestState *from,
2780 QTestState *to)
2781 {
2782 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2783 return test_migrate_tls_x509_start_default_host(from, to);
2784 }
2785
2786 static void *
test_migrate_multifd_tls_x509_start_override_host(QTestState * from,QTestState * to)2787 test_migrate_multifd_tls_x509_start_override_host(QTestState *from,
2788 QTestState *to)
2789 {
2790 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2791 return test_migrate_tls_x509_start_override_host(from, to);
2792 }
2793
2794 static void *
test_migrate_multifd_tls_x509_start_mismatch_host(QTestState * from,QTestState * to)2795 test_migrate_multifd_tls_x509_start_mismatch_host(QTestState *from,
2796 QTestState *to)
2797 {
2798 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2799 return test_migrate_tls_x509_start_mismatch_host(from, to);
2800 }
2801
2802 static void *
test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState * from,QTestState * to)2803 test_migrate_multifd_tls_x509_start_allow_anon_client(QTestState *from,
2804 QTestState *to)
2805 {
2806 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2807 return test_migrate_tls_x509_start_allow_anon_client(from, to);
2808 }
2809
2810 static void *
test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState * from,QTestState * to)2811 test_migrate_multifd_tls_x509_start_reject_anon_client(QTestState *from,
2812 QTestState *to)
2813 {
2814 test_migrate_precopy_tcp_multifd_start_common(from, to, "none");
2815 return test_migrate_tls_x509_start_reject_anon_client(from, to);
2816 }
2817 #endif /* CONFIG_TASN1 */
2818
test_multifd_tcp_tls_psk_match(void)2819 static void test_multifd_tcp_tls_psk_match(void)
2820 {
2821 MigrateCommon args = {
2822 .listen_uri = "defer",
2823 .start_hook = test_migrate_multifd_tcp_tls_psk_start_match,
2824 .finish_hook = test_migrate_tls_psk_finish,
2825 };
2826 test_precopy_common(&args);
2827 }
2828
test_multifd_tcp_tls_psk_mismatch(void)2829 static void test_multifd_tcp_tls_psk_mismatch(void)
2830 {
2831 MigrateCommon args = {
2832 .start = {
2833 .hide_stderr = true,
2834 },
2835 .listen_uri = "defer",
2836 .start_hook = test_migrate_multifd_tcp_tls_psk_start_mismatch,
2837 .finish_hook = test_migrate_tls_psk_finish,
2838 .result = MIG_TEST_FAIL,
2839 };
2840 test_precopy_common(&args);
2841 }
2842
2843 #ifdef CONFIG_TASN1
test_multifd_tcp_tls_x509_default_host(void)2844 static void test_multifd_tcp_tls_x509_default_host(void)
2845 {
2846 MigrateCommon args = {
2847 .listen_uri = "defer",
2848 .start_hook = test_migrate_multifd_tls_x509_start_default_host,
2849 .finish_hook = test_migrate_tls_x509_finish,
2850 };
2851 test_precopy_common(&args);
2852 }
2853
test_multifd_tcp_tls_x509_override_host(void)2854 static void test_multifd_tcp_tls_x509_override_host(void)
2855 {
2856 MigrateCommon args = {
2857 .listen_uri = "defer",
2858 .start_hook = test_migrate_multifd_tls_x509_start_override_host,
2859 .finish_hook = test_migrate_tls_x509_finish,
2860 };
2861 test_precopy_common(&args);
2862 }
2863
test_multifd_tcp_tls_x509_mismatch_host(void)2864 static void test_multifd_tcp_tls_x509_mismatch_host(void)
2865 {
2866 /*
2867 * This has different behaviour to the non-multifd case.
2868 *
2869 * In non-multifd case when client aborts due to mismatched
2870 * cert host, the server has already started trying to load
2871 * migration state, and so it exits with I/O failure.
2872 *
2873 * In multifd case when client aborts due to mismatched
2874 * cert host, the server is still waiting for the other
2875 * multifd connections to arrive so hasn't started trying
2876 * to load migration state, and thus just aborts the migration
2877 * without exiting.
2878 */
2879 MigrateCommon args = {
2880 .start = {
2881 .hide_stderr = true,
2882 },
2883 .listen_uri = "defer",
2884 .start_hook = test_migrate_multifd_tls_x509_start_mismatch_host,
2885 .finish_hook = test_migrate_tls_x509_finish,
2886 .result = MIG_TEST_FAIL,
2887 };
2888 test_precopy_common(&args);
2889 }
2890
test_multifd_tcp_tls_x509_allow_anon_client(void)2891 static void test_multifd_tcp_tls_x509_allow_anon_client(void)
2892 {
2893 MigrateCommon args = {
2894 .listen_uri = "defer",
2895 .start_hook = test_migrate_multifd_tls_x509_start_allow_anon_client,
2896 .finish_hook = test_migrate_tls_x509_finish,
2897 };
2898 test_precopy_common(&args);
2899 }
2900
test_multifd_tcp_tls_x509_reject_anon_client(void)2901 static void test_multifd_tcp_tls_x509_reject_anon_client(void)
2902 {
2903 MigrateCommon args = {
2904 .start = {
2905 .hide_stderr = true,
2906 },
2907 .listen_uri = "defer",
2908 .start_hook = test_migrate_multifd_tls_x509_start_reject_anon_client,
2909 .finish_hook = test_migrate_tls_x509_finish,
2910 .result = MIG_TEST_FAIL,
2911 };
2912 test_precopy_common(&args);
2913 }
2914 #endif /* CONFIG_TASN1 */
2915 #endif /* CONFIG_GNUTLS */
2916
2917 /*
2918 * This test does:
2919 * source target
2920 * migrate_incoming
2921 * migrate
2922 * migrate_cancel
2923 * launch another target
2924 * migrate
2925 *
2926 * And see that it works
2927 */
test_multifd_tcp_cancel(void)2928 static void test_multifd_tcp_cancel(void)
2929 {
2930 MigrateStart args = {
2931 .hide_stderr = true,
2932 };
2933 QTestState *from, *to, *to2;
2934
2935 if (test_migrate_start(&from, &to, "defer", &args)) {
2936 return;
2937 }
2938
2939 migrate_ensure_non_converge(from);
2940 migrate_prepare_for_dirty_mem(from);
2941
2942 migrate_set_parameter_int(from, "multifd-channels", 16);
2943 migrate_set_parameter_int(to, "multifd-channels", 16);
2944
2945 migrate_set_capability(from, "multifd", true);
2946 migrate_set_capability(to, "multifd", true);
2947
2948 /* Start incoming migration from the 1st socket */
2949 migrate_incoming_qmp(to, "tcp:127.0.0.1:0", "{}");
2950
2951 /* Wait for the first serial output from the source */
2952 wait_for_serial("src_serial");
2953
2954 migrate_qmp(from, to, NULL, NULL, "{}");
2955
2956 migrate_wait_for_dirty_mem(from, to);
2957
2958 migrate_cancel(from);
2959
2960 /* Make sure QEMU process "to" exited */
2961 qtest_set_expected_status(to, EXIT_FAILURE);
2962 qtest_wait_qemu(to);
2963
2964 args = (MigrateStart){
2965 .only_target = true,
2966 };
2967
2968 if (test_migrate_start(&from, &to2, "defer", &args)) {
2969 return;
2970 }
2971
2972 migrate_set_parameter_int(to2, "multifd-channels", 16);
2973
2974 migrate_set_capability(to2, "multifd", true);
2975
2976 /* Start incoming migration from the 1st socket */
2977 migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", "{}");
2978
2979 wait_for_migration_status(from, "cancelled", NULL);
2980
2981 migrate_ensure_non_converge(from);
2982
2983 migrate_qmp(from, to2, NULL, NULL, "{}");
2984
2985 migrate_wait_for_dirty_mem(from, to2);
2986
2987 migrate_ensure_converge(from);
2988
2989 wait_for_stop(from, &src_state);
2990 qtest_qmp_eventwait(to2, "RESUME");
2991
2992 wait_for_serial("dest_serial");
2993 wait_for_migration_complete(from);
2994 test_migrate_end(from, to2, true);
2995 }
2996
calc_dirty_rate(QTestState * who,uint64_t calc_time)2997 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
2998 {
2999 qtest_qmp_assert_success(who,
3000 "{ 'execute': 'calc-dirty-rate',"
3001 "'arguments': { "
3002 "'calc-time': %" PRIu64 ","
3003 "'mode': 'dirty-ring' }}",
3004 calc_time);
3005 }
3006
query_dirty_rate(QTestState * who)3007 static QDict *query_dirty_rate(QTestState *who)
3008 {
3009 return qtest_qmp_assert_success_ref(who,
3010 "{ 'execute': 'query-dirty-rate' }");
3011 }
3012
dirtylimit_set_all(QTestState * who,uint64_t dirtyrate)3013 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
3014 {
3015 qtest_qmp_assert_success(who,
3016 "{ 'execute': 'set-vcpu-dirty-limit',"
3017 "'arguments': { "
3018 "'dirty-rate': %" PRIu64 " } }",
3019 dirtyrate);
3020 }
3021
cancel_vcpu_dirty_limit(QTestState * who)3022 static void cancel_vcpu_dirty_limit(QTestState *who)
3023 {
3024 qtest_qmp_assert_success(who,
3025 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
3026 }
3027
query_vcpu_dirty_limit(QTestState * who)3028 static QDict *query_vcpu_dirty_limit(QTestState *who)
3029 {
3030 QDict *rsp;
3031
3032 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
3033 g_assert(!qdict_haskey(rsp, "error"));
3034 g_assert(qdict_haskey(rsp, "return"));
3035
3036 return rsp;
3037 }
3038
calc_dirtyrate_ready(QTestState * who)3039 static bool calc_dirtyrate_ready(QTestState *who)
3040 {
3041 QDict *rsp_return;
3042 gchar *status;
3043
3044 rsp_return = query_dirty_rate(who);
3045 g_assert(rsp_return);
3046
3047 status = g_strdup(qdict_get_str(rsp_return, "status"));
3048 g_assert(status);
3049
3050 return g_strcmp0(status, "measuring");
3051 }
3052
wait_for_calc_dirtyrate_complete(QTestState * who,int64_t time_s)3053 static void wait_for_calc_dirtyrate_complete(QTestState *who,
3054 int64_t time_s)
3055 {
3056 int max_try_count = 10000;
3057 usleep(time_s * 1000000);
3058
3059 while (!calc_dirtyrate_ready(who) && max_try_count--) {
3060 usleep(1000);
3061 }
3062
3063 /*
3064 * Set the timeout with 10 s(max_try_count * 1000us),
3065 * if dirtyrate measurement not complete, fail test.
3066 */
3067 g_assert_cmpint(max_try_count, !=, 0);
3068 }
3069
get_dirty_rate(QTestState * who)3070 static int64_t get_dirty_rate(QTestState *who)
3071 {
3072 QDict *rsp_return;
3073 gchar *status;
3074 QList *rates;
3075 const QListEntry *entry;
3076 QDict *rate;
3077 int64_t dirtyrate;
3078
3079 rsp_return = query_dirty_rate(who);
3080 g_assert(rsp_return);
3081
3082 status = g_strdup(qdict_get_str(rsp_return, "status"));
3083 g_assert(status);
3084 g_assert_cmpstr(status, ==, "measured");
3085
3086 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
3087 g_assert(rates && !qlist_empty(rates));
3088
3089 entry = qlist_first(rates);
3090 g_assert(entry);
3091
3092 rate = qobject_to(QDict, qlist_entry_obj(entry));
3093 g_assert(rate);
3094
3095 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
3096
3097 qobject_unref(rsp_return);
3098 return dirtyrate;
3099 }
3100
get_limit_rate(QTestState * who)3101 static int64_t get_limit_rate(QTestState *who)
3102 {
3103 QDict *rsp_return;
3104 QList *rates;
3105 const QListEntry *entry;
3106 QDict *rate;
3107 int64_t dirtyrate;
3108
3109 rsp_return = query_vcpu_dirty_limit(who);
3110 g_assert(rsp_return);
3111
3112 rates = qdict_get_qlist(rsp_return, "return");
3113 g_assert(rates && !qlist_empty(rates));
3114
3115 entry = qlist_first(rates);
3116 g_assert(entry);
3117
3118 rate = qobject_to(QDict, qlist_entry_obj(entry));
3119 g_assert(rate);
3120
3121 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
3122
3123 qobject_unref(rsp_return);
3124 return dirtyrate;
3125 }
3126
dirtylimit_start_vm(void)3127 static QTestState *dirtylimit_start_vm(void)
3128 {
3129 QTestState *vm = NULL;
3130 g_autofree gchar *cmd = NULL;
3131
3132 bootfile_create(tmpfs, false);
3133 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
3134 "-name dirtylimit-test,debug-threads=on "
3135 "-m 150M -smp 1 "
3136 "-serial file:%s/vm_serial "
3137 "-drive file=%s,format=raw ",
3138 tmpfs, bootpath);
3139
3140 vm = qtest_init(cmd);
3141 return vm;
3142 }
3143
dirtylimit_stop_vm(QTestState * vm)3144 static void dirtylimit_stop_vm(QTestState *vm)
3145 {
3146 qtest_quit(vm);
3147 cleanup("vm_serial");
3148 }
3149
test_vcpu_dirty_limit(void)3150 static void test_vcpu_dirty_limit(void)
3151 {
3152 QTestState *vm;
3153 int64_t origin_rate;
3154 int64_t quota_rate;
3155 int64_t rate ;
3156 int max_try_count = 20;
3157 int hit = 0;
3158
3159 /* Start vm for vcpu dirtylimit test */
3160 vm = dirtylimit_start_vm();
3161
3162 /* Wait for the first serial output from the vm*/
3163 wait_for_serial("vm_serial");
3164
3165 /* Do dirtyrate measurement with calc time equals 1s */
3166 calc_dirty_rate(vm, 1);
3167
3168 /* Sleep calc time and wait for calc dirtyrate complete */
3169 wait_for_calc_dirtyrate_complete(vm, 1);
3170
3171 /* Query original dirty page rate */
3172 origin_rate = get_dirty_rate(vm);
3173
3174 /* VM booted from bootsect should dirty memory steadily */
3175 assert(origin_rate != 0);
3176
3177 /* Setup quota dirty page rate at half of origin */
3178 quota_rate = origin_rate / 2;
3179
3180 /* Set dirtylimit */
3181 dirtylimit_set_all(vm, quota_rate);
3182
3183 /*
3184 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
3185 * works literally
3186 */
3187 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
3188
3189 /* Sleep a bit to check if it take effect */
3190 usleep(2000000);
3191
3192 /*
3193 * Check if dirtylimit take effect realistically, set the
3194 * timeout with 20 s(max_try_count * 1s), if dirtylimit
3195 * doesn't take effect, fail test.
3196 */
3197 while (--max_try_count) {
3198 calc_dirty_rate(vm, 1);
3199 wait_for_calc_dirtyrate_complete(vm, 1);
3200 rate = get_dirty_rate(vm);
3201
3202 /*
3203 * Assume hitting if current rate is less
3204 * than quota rate (within accepting error)
3205 */
3206 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3207 hit = 1;
3208 break;
3209 }
3210 }
3211
3212 g_assert_cmpint(hit, ==, 1);
3213
3214 hit = 0;
3215 max_try_count = 20;
3216
3217 /* Check if dirtylimit cancellation take effect */
3218 cancel_vcpu_dirty_limit(vm);
3219 while (--max_try_count) {
3220 calc_dirty_rate(vm, 1);
3221 wait_for_calc_dirtyrate_complete(vm, 1);
3222 rate = get_dirty_rate(vm);
3223
3224 /*
3225 * Assume dirtylimit be canceled if current rate is
3226 * greater than quota rate (within accepting error)
3227 */
3228 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
3229 hit = 1;
3230 break;
3231 }
3232 }
3233
3234 g_assert_cmpint(hit, ==, 1);
3235 dirtylimit_stop_vm(vm);
3236 }
3237
migrate_dirty_limit_wait_showup(QTestState * from,const int64_t period,const int64_t value)3238 static void migrate_dirty_limit_wait_showup(QTestState *from,
3239 const int64_t period,
3240 const int64_t value)
3241 {
3242 /* Enable dirty limit capability */
3243 migrate_set_capability(from, "dirty-limit", true);
3244
3245 /* Set dirty limit parameters */
3246 migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period);
3247 migrate_set_parameter_int(from, "vcpu-dirty-limit", value);
3248
3249 /* Make sure migrate can't converge */
3250 migrate_ensure_non_converge(from);
3251
3252 /* To check limit rate after precopy */
3253 migrate_set_capability(from, "pause-before-switchover", true);
3254
3255 /* Wait for the serial output from the source */
3256 wait_for_serial("src_serial");
3257 }
3258
3259 /*
3260 * This test does:
3261 * source destination
3262 * start vm
3263 * start incoming vm
3264 * migrate
3265 * wait dirty limit to begin
3266 * cancel migrate
3267 * cancellation check
3268 * restart incoming vm
3269 * migrate
3270 * wait dirty limit to begin
3271 * wait pre-switchover event
3272 * convergence condition check
3273 *
3274 * And see if dirty limit migration works correctly.
3275 * This test case involves many passes, so it runs in slow mode only.
3276 */
test_migrate_dirty_limit(void)3277 static void test_migrate_dirty_limit(void)
3278 {
3279 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
3280 QTestState *from, *to;
3281 int64_t remaining;
3282 uint64_t throttle_us_per_full;
3283 /*
3284 * We want the test to be stable and as fast as possible.
3285 * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
3286 * so we need to decrease a bandwidth.
3287 */
3288 const int64_t dirtylimit_period = 1000, dirtylimit_value = 50;
3289 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
3290 const int64_t downtime_limit = 250; /* 250ms */
3291 /*
3292 * We migrate through unix-socket (> 500Mb/s).
3293 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
3294 * So, we can predict expected_threshold
3295 */
3296 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
3297 int max_try_count = 10;
3298 MigrateCommon args = {
3299 .start = {
3300 .hide_stderr = true,
3301 .use_dirty_ring = true,
3302 },
3303 .listen_uri = uri,
3304 .connect_uri = uri,
3305 };
3306
3307 /* Start src, dst vm */
3308 if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
3309 return;
3310 }
3311
3312 /* Prepare for dirty limit migration and wait src vm show up */
3313 migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value);
3314
3315 /* Start migrate */
3316 migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3317
3318 /* Wait for dirty limit throttle begin */
3319 throttle_us_per_full = 0;
3320 while (throttle_us_per_full == 0) {
3321 throttle_us_per_full =
3322 read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3323 usleep(100);
3324 g_assert_false(src_state.stop_seen);
3325 }
3326
3327 /* Now cancel migrate and wait for dirty limit throttle switch off */
3328 migrate_cancel(from);
3329 wait_for_migration_status(from, "cancelled", NULL);
3330
3331 /* Check if dirty limit throttle switched off, set timeout 1ms */
3332 do {
3333 throttle_us_per_full =
3334 read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3335 usleep(100);
3336 g_assert_false(src_state.stop_seen);
3337 } while (throttle_us_per_full != 0 && --max_try_count);
3338
3339 /* Assert dirty limit is not in service */
3340 g_assert_cmpint(throttle_us_per_full, ==, 0);
3341
3342 args = (MigrateCommon) {
3343 .start = {
3344 .only_target = true,
3345 .use_dirty_ring = true,
3346 },
3347 .listen_uri = uri,
3348 .connect_uri = uri,
3349 };
3350
3351 /* Restart dst vm, src vm already show up so we needn't wait anymore */
3352 if (test_migrate_start(&from, &to, args.listen_uri, &args.start)) {
3353 return;
3354 }
3355
3356 /* Start migrate */
3357 migrate_qmp(from, to, args.connect_uri, NULL, "{}");
3358
3359 /* Wait for dirty limit throttle begin */
3360 throttle_us_per_full = 0;
3361 while (throttle_us_per_full == 0) {
3362 throttle_us_per_full =
3363 read_migrate_property_int(from, "dirty-limit-throttle-time-per-round");
3364 usleep(100);
3365 g_assert_false(src_state.stop_seen);
3366 }
3367
3368 /*
3369 * The dirty limit rate should equals the return value of
3370 * query-vcpu-dirty-limit if dirty limit cap set
3371 */
3372 g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from));
3373
3374 /* Now, we have tested if dirty limit works, let it converge */
3375 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
3376 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
3377
3378 /*
3379 * Wait for pre-switchover status to check if migration
3380 * satisfy the convergence condition
3381 */
3382 wait_for_migration_status(from, "pre-switchover", NULL);
3383
3384 remaining = read_ram_property_int(from, "remaining");
3385 g_assert_cmpint(remaining, <,
3386 (expected_threshold + expected_threshold / 100));
3387
3388 migrate_continue(from, "pre-switchover");
3389
3390 qtest_qmp_eventwait(to, "RESUME");
3391
3392 wait_for_serial("dest_serial");
3393 wait_for_migration_complete(from);
3394
3395 test_migrate_end(from, to, true);
3396 }
3397
kvm_dirty_ring_supported(void)3398 static bool kvm_dirty_ring_supported(void)
3399 {
3400 #if defined(__linux__) && defined(HOST_X86_64)
3401 int ret, kvm_fd = open("/dev/kvm", O_RDONLY);
3402
3403 if (kvm_fd < 0) {
3404 return false;
3405 }
3406
3407 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, KVM_CAP_DIRTY_LOG_RING);
3408 close(kvm_fd);
3409
3410 /* We test with 4096 slots */
3411 if (ret < 4096) {
3412 return false;
3413 }
3414
3415 return true;
3416 #else
3417 return false;
3418 #endif
3419 }
3420
main(int argc,char ** argv)3421 int main(int argc, char **argv)
3422 {
3423 bool has_kvm, has_tcg;
3424 bool has_uffd, is_x86;
3425 const char *arch;
3426 g_autoptr(GError) err = NULL;
3427 const char *qemu_src = getenv(QEMU_ENV_SRC);
3428 const char *qemu_dst = getenv(QEMU_ENV_DST);
3429 int ret;
3430
3431 g_test_init(&argc, &argv, NULL);
3432
3433 /*
3434 * The default QTEST_QEMU_BINARY must always be provided because
3435 * that is what helpers use to query the accel type and
3436 * architecture.
3437 */
3438 if (qemu_src && qemu_dst) {
3439 g_test_message("Only one of %s, %s is allowed",
3440 QEMU_ENV_SRC, QEMU_ENV_DST);
3441 exit(1);
3442 }
3443
3444 has_kvm = qtest_has_accel("kvm");
3445 has_tcg = qtest_has_accel("tcg");
3446
3447 if (!has_tcg && !has_kvm) {
3448 g_test_skip("No KVM or TCG accelerator available");
3449 return 0;
3450 }
3451
3452 has_uffd = ufd_version_check();
3453 arch = qtest_get_arch();
3454 is_x86 = !strcmp(arch, "i386") || !strcmp(arch, "x86_64");
3455
3456 tmpfs = g_dir_make_tmp("migration-test-XXXXXX", &err);
3457 if (!tmpfs) {
3458 g_test_message("Can't create temporary directory in %s: %s",
3459 g_get_tmp_dir(), err->message);
3460 }
3461 g_assert(tmpfs);
3462
3463 module_call_init(MODULE_INIT_QOM);
3464
3465 migration_test_add("/migration/bad_dest", test_baddest);
3466 #ifndef _WIN32
3467 migration_test_add("/migration/analyze-script", test_analyze_script);
3468 #endif
3469
3470 /*
3471 * On s390x with TCG, migration is observed to hang due to the 'pending'
3472 * state of the flic interrupt controller not being migrated or
3473 * reconstructed post-migration. Disable it until the problem is resolved.
3474 */
3475 if (g_str_equal(arch, "s390x") && !has_kvm) {
3476 g_test_message("Skipping tests: s390x host with KVM is required");
3477 goto test_add_done;
3478 }
3479
3480 if (is_x86) {
3481 migration_test_add("/migration/precopy/unix/suspend/live",
3482 test_precopy_unix_suspend_live);
3483 migration_test_add("/migration/precopy/unix/suspend/notlive",
3484 test_precopy_unix_suspend_notlive);
3485 }
3486
3487 if (has_uffd) {
3488 migration_test_add("/migration/postcopy/plain", test_postcopy);
3489 migration_test_add("/migration/postcopy/recovery/plain",
3490 test_postcopy_recovery);
3491 migration_test_add("/migration/postcopy/preempt/plain",
3492 test_postcopy_preempt);
3493 migration_test_add("/migration/postcopy/preempt/recovery/plain",
3494 test_postcopy_preempt_recovery);
3495 #ifndef _WIN32
3496 migration_test_add("/migration/postcopy/recovery/double-failures",
3497 test_postcopy_recovery_double_fail);
3498 #endif /* _WIN32 */
3499 if (is_x86) {
3500 migration_test_add("/migration/postcopy/suspend",
3501 test_postcopy_suspend);
3502 }
3503 }
3504
3505 migration_test_add("/migration/precopy/unix/plain",
3506 test_precopy_unix_plain);
3507 migration_test_add("/migration/precopy/unix/xbzrle",
3508 test_precopy_unix_xbzrle);
3509 migration_test_add("/migration/precopy/file",
3510 test_precopy_file);
3511 migration_test_add("/migration/precopy/file/offset",
3512 test_precopy_file_offset);
3513 migration_test_add("/migration/precopy/file/offset/bad",
3514 test_precopy_file_offset_bad);
3515
3516 /*
3517 * Our CI system has problems with shared memory.
3518 * Don't run this test until we find a workaround.
3519 */
3520 if (getenv("QEMU_TEST_FLAKY_TESTS")) {
3521 migration_test_add("/migration/mode/reboot", test_mode_reboot);
3522 }
3523
3524 migration_test_add("/migration/precopy/file/mapped-ram",
3525 test_precopy_file_mapped_ram);
3526 migration_test_add("/migration/precopy/file/mapped-ram/live",
3527 test_precopy_file_mapped_ram_live);
3528
3529 migration_test_add("/migration/multifd/file/mapped-ram",
3530 test_multifd_file_mapped_ram);
3531 migration_test_add("/migration/multifd/file/mapped-ram/live",
3532 test_multifd_file_mapped_ram_live);
3533
3534 #ifdef CONFIG_GNUTLS
3535 migration_test_add("/migration/precopy/unix/tls/psk",
3536 test_precopy_unix_tls_psk);
3537
3538 if (has_uffd) {
3539 /*
3540 * NOTE: psk test is enough for postcopy, as other types of TLS
3541 * channels are tested under precopy. Here what we want to test is the
3542 * general postcopy path that has TLS channel enabled.
3543 */
3544 migration_test_add("/migration/postcopy/tls/psk",
3545 test_postcopy_tls_psk);
3546 migration_test_add("/migration/postcopy/recovery/tls/psk",
3547 test_postcopy_recovery_tls_psk);
3548 migration_test_add("/migration/postcopy/preempt/tls/psk",
3549 test_postcopy_preempt_tls_psk);
3550 migration_test_add("/migration/postcopy/preempt/recovery/tls/psk",
3551 test_postcopy_preempt_all);
3552 }
3553 #ifdef CONFIG_TASN1
3554 migration_test_add("/migration/precopy/unix/tls/x509/default-host",
3555 test_precopy_unix_tls_x509_default_host);
3556 migration_test_add("/migration/precopy/unix/tls/x509/override-host",
3557 test_precopy_unix_tls_x509_override_host);
3558 #endif /* CONFIG_TASN1 */
3559 #endif /* CONFIG_GNUTLS */
3560
3561 migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
3562
3563 migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
3564 test_precopy_tcp_switchover_ack);
3565
3566 #ifdef CONFIG_GNUTLS
3567 migration_test_add("/migration/precopy/tcp/tls/psk/match",
3568 test_precopy_tcp_tls_psk_match);
3569 migration_test_add("/migration/precopy/tcp/tls/psk/mismatch",
3570 test_precopy_tcp_tls_psk_mismatch);
3571 #ifdef CONFIG_TASN1
3572 migration_test_add("/migration/precopy/tcp/tls/x509/default-host",
3573 test_precopy_tcp_tls_x509_default_host);
3574 migration_test_add("/migration/precopy/tcp/tls/x509/override-host",
3575 test_precopy_tcp_tls_x509_override_host);
3576 migration_test_add("/migration/precopy/tcp/tls/x509/mismatch-host",
3577 test_precopy_tcp_tls_x509_mismatch_host);
3578 migration_test_add("/migration/precopy/tcp/tls/x509/friendly-client",
3579 test_precopy_tcp_tls_x509_friendly_client);
3580 migration_test_add("/migration/precopy/tcp/tls/x509/hostile-client",
3581 test_precopy_tcp_tls_x509_hostile_client);
3582 migration_test_add("/migration/precopy/tcp/tls/x509/allow-anon-client",
3583 test_precopy_tcp_tls_x509_allow_anon_client);
3584 migration_test_add("/migration/precopy/tcp/tls/x509/reject-anon-client",
3585 test_precopy_tcp_tls_x509_reject_anon_client);
3586 #endif /* CONFIG_TASN1 */
3587 #endif /* CONFIG_GNUTLS */
3588
3589 /* migration_test_add("/migration/ignore_shared", test_ignore_shared); */
3590 #ifndef _WIN32
3591 migration_test_add("/migration/precopy/fd/tcp",
3592 test_migrate_precopy_fd_socket);
3593 migration_test_add("/migration/precopy/fd/file",
3594 test_migrate_precopy_fd_file);
3595 #endif
3596 migration_test_add("/migration/validate_uuid", test_validate_uuid);
3597 migration_test_add("/migration/validate_uuid_error",
3598 test_validate_uuid_error);
3599 migration_test_add("/migration/validate_uuid_src_not_set",
3600 test_validate_uuid_src_not_set);
3601 migration_test_add("/migration/validate_uuid_dst_not_set",
3602 test_validate_uuid_dst_not_set);
3603 migration_test_add("/migration/validate_uri/channels/both_set",
3604 test_validate_uri_channels_both_set);
3605 migration_test_add("/migration/validate_uri/channels/none_set",
3606 test_validate_uri_channels_none_set);
3607 /*
3608 * See explanation why this test is slow on function definition
3609 */
3610 if (g_test_slow()) {
3611 migration_test_add("/migration/auto_converge",
3612 test_migrate_auto_converge);
3613 if (g_str_equal(arch, "x86_64") &&
3614 has_kvm && kvm_dirty_ring_supported()) {
3615 migration_test_add("/migration/dirty_limit",
3616 test_migrate_dirty_limit);
3617 }
3618 }
3619 migration_test_add("/migration/multifd/tcp/uri/plain/none",
3620 test_multifd_tcp_uri_none);
3621 migration_test_add("/migration/multifd/tcp/channels/plain/none",
3622 test_multifd_tcp_channels_none);
3623 migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy",
3624 test_multifd_tcp_zero_page_legacy);
3625 migration_test_add("/migration/multifd/tcp/plain/zero-page/none",
3626 test_multifd_tcp_no_zero_page);
3627 migration_test_add("/migration/multifd/tcp/plain/cancel",
3628 test_multifd_tcp_cancel);
3629 migration_test_add("/migration/multifd/tcp/plain/zlib",
3630 test_multifd_tcp_zlib);
3631 #ifdef CONFIG_ZSTD
3632 migration_test_add("/migration/multifd/tcp/plain/zstd",
3633 test_multifd_tcp_zstd);
3634 #endif
3635 #ifdef CONFIG_QPL
3636 migration_test_add("/migration/multifd/tcp/plain/qpl",
3637 test_multifd_tcp_qpl);
3638 #endif
3639 #ifdef CONFIG_UADK
3640 migration_test_add("/migration/multifd/tcp/plain/uadk",
3641 test_multifd_tcp_uadk);
3642 #endif
3643 #ifdef CONFIG_GNUTLS
3644 migration_test_add("/migration/multifd/tcp/tls/psk/match",
3645 test_multifd_tcp_tls_psk_match);
3646 migration_test_add("/migration/multifd/tcp/tls/psk/mismatch",
3647 test_multifd_tcp_tls_psk_mismatch);
3648 #ifdef CONFIG_TASN1
3649 migration_test_add("/migration/multifd/tcp/tls/x509/default-host",
3650 test_multifd_tcp_tls_x509_default_host);
3651 migration_test_add("/migration/multifd/tcp/tls/x509/override-host",
3652 test_multifd_tcp_tls_x509_override_host);
3653 migration_test_add("/migration/multifd/tcp/tls/x509/mismatch-host",
3654 test_multifd_tcp_tls_x509_mismatch_host);
3655 migration_test_add("/migration/multifd/tcp/tls/x509/allow-anon-client",
3656 test_multifd_tcp_tls_x509_allow_anon_client);
3657 migration_test_add("/migration/multifd/tcp/tls/x509/reject-anon-client",
3658 test_multifd_tcp_tls_x509_reject_anon_client);
3659 #endif /* CONFIG_TASN1 */
3660 #endif /* CONFIG_GNUTLS */
3661
3662 if (g_str_equal(arch, "x86_64") && has_kvm && kvm_dirty_ring_supported()) {
3663 migration_test_add("/migration/dirty_ring",
3664 test_precopy_unix_dirty_ring);
3665 migration_test_add("/migration/vcpu_dirty_limit",
3666 test_vcpu_dirty_limit);
3667 }
3668
3669 test_add_done:
3670
3671 ret = g_test_run();
3672
3673 g_assert_cmpint(ret, ==, 0);
3674
3675 bootfile_delete();
3676 ret = rmdir(tmpfs);
3677 if (ret != 0) {
3678 g_test_message("unable to rmdir: path (%s): %s",
3679 tmpfs, strerror(errno));
3680 }
3681 g_free(tmpfs);
3682
3683 return ret;
3684 }
3685