master
c 1,168 lines 34.1 KB
Raw
1 /*
2 * QTest testcase for precopy 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 #include "chardev/char.h"
15 #include "crypto/tlscredspsk.h"
16 #include "libqtest.h"
17 #include "migration/bootfile.h"
18 #include "migration/framework.h"
19 #include "migration/migration-qmp.h"
20 #include "migration/migration-util.h"
21 #include "ppc-util.h"
22 #include "qobject/qlist.h"
23 #include "qapi-types-migration.h"
24 #include "qemu/module.h"
25 #include "qemu/option.h"
26 #include "qemu/range.h"
27 #include "qemu/sockets.h"
28
29
30 /*
31 * Dirtylimit stop working if dirty page rate error
32 * value less than DIRTYLIMIT_TOLERANCE_RANGE
33 */
34 #define DIRTYLIMIT_TOLERANCE_RANGE 25 /* MB/s */
35
36 static char *tmpfs;
37
38 static void test_precopy_unix_plain(char *name, MigrateCommon *args)
39 {
40 /*
41 * The simplest use case of precopy, covering smoke tests of
42 * get-dirty-log dirty tracking.
43 */
44 args->live = true;
45 test_precopy_unix_common(args);
46 }
47
48 static void test_precopy_unix_suspend_live(char *name, MigrateCommon *args)
49 {
50 /*
51 * despite being live, the test is fast because the src
52 * suspends immediately.
53 */
54 args->live = true;
55 args->start.suspend_me = true;
56 test_precopy_unix_common(args);
57 }
58
59 static void test_precopy_unix_suspend_notlive(char *name, MigrateCommon *args)
60 {
61 args->start.suspend_me = true;
62 test_precopy_unix_common(args);
63 }
64
65 static void test_precopy_unix_dirty_ring(char *name, MigrateCommon *args)
66 {
67 /*
68 * Besides the precopy/unix basic test, cover dirty ring interface
69 * rather than get-dirty-log.
70 */
71 args->live = true;
72 args->start.use_dirty_ring = true;
73 test_precopy_unix_common(args);
74 }
75
76 #ifdef CONFIG_RDMA
77
78 #include <sys/resource.h>
79
80 /*
81 * During migration over RDMA, it will try to pin portions of guest memory,
82 * typically exceeding 100MB in this test, while the remainder will be
83 * transmitted as compressed zero pages.
84 *
85 * REQUIRED_MEMLOCK_SZ indicates the minimal mlock size in the current context.
86 */
87 #define REQUIRED_MEMLOCK_SZ (128 << 20) /* 128MB */
88
89 /* check 'ulimit -l' */
90 static bool mlock_check(void)
91 {
92 uid_t uid;
93 struct rlimit rlim;
94
95 uid = getuid();
96 if (uid == 0) {
97 return true;
98 }
99
100 if (getrlimit(RLIMIT_MEMLOCK, &rlim) != 0) {
101 return false;
102 }
103
104 return rlim.rlim_cur >= REQUIRED_MEMLOCK_SZ;
105 }
106
107 #define RDMA_MIGRATION_HELPER "scripts/rdma-migration-helper.sh"
108 static int new_rdma_link(char *buffer, bool ipv6)
109 {
110 char cmd[256];
111 bool verbose = qtest_verbose("test");
112
113 snprintf(cmd, sizeof(cmd), "IP_FAMILY=%s %s detect %s",
114 ipv6 ? "ipv6" : "ipv4", RDMA_MIGRATION_HELPER,
115 verbose ? "" : "2>/dev/null");
116
117 FILE *pipe = popen(cmd, "r");
118 if (pipe == NULL) {
119 perror("Failed to run script");
120 return -1;
121 }
122
123 int idx = 0;
124 while (fgets(buffer + idx, 128 - idx, pipe) != NULL) {
125 idx += strlen(buffer);
126 }
127
128 int status = pclose(pipe);
129 if (status == -1) {
130 perror("Error reported by pclose()");
131 return -1;
132 } else if (WIFEXITED(status)) {
133 return WEXITSTATUS(status);
134 }
135
136 return -1;
137 }
138
139 static void __test_precopy_rdma_plain(MigrateCommon *args, bool ipv6)
140 {
141 char buffer[128] = {};
142
143 if (!mlock_check()) {
144 g_test_skip("'ulimit -l' is too small, require >=128M");
145 return;
146 }
147
148 if (new_rdma_link(buffer, ipv6)) {
149 g_test_skip("No rdma link available\n"
150 "# To enable the test:\n"
151 "# Run \'" RDMA_MIGRATION_HELPER " setup\' with root to "
152 "setup a new rdma/rxe link and rerun the test\n"
153 "# Optional: run 'scripts/rdma-migration-helper.sh clean' "
154 "to revert the 'setup'");
155 return;
156 }
157
158 /*
159 * TODO: query a free port instead of hard code.
160 * 29200=('R'+'D'+'M'+'A')*100
161 **/
162 g_autofree char *uri = g_strdup_printf("rdma:%s:29200", buffer);
163
164 args->uri = uri;
165
166 test_precopy_common(args);
167 }
168
169 static void test_precopy_rdma_plain(char *name, MigrateCommon *args)
170 {
171 __test_precopy_rdma_plain(args, false);
172 }
173
174 static void test_precopy_rdma_plain_ipv6(char *name, MigrateCommon *args)
175 {
176 __test_precopy_rdma_plain(args, true);
177 }
178 #endif
179
180 static void test_precopy_tcp_plain(char *name, MigrateCommon *args)
181 {
182 test_precopy_common(args);
183 }
184
185 static void test_precopy_tcp_switchover_ack(char *name, MigrateCommon *args)
186 {
187 /*
188 * Source VM must be running in order to consider the switchover ACK
189 * when deciding to do switchover or not.
190 */
191 args->live = true;
192
193 args->start.caps[MIGRATION_CAPABILITY_RETURN_PATH] = true;
194 args->start.caps[MIGRATION_CAPABILITY_SWITCHOVER_ACK] = true;
195
196 test_precopy_common(args);
197 }
198
199 #ifndef _WIN32
200 static void *migrate_hook_start_fd(QTestState *from,
201 QTestState *to)
202 {
203 int ret;
204 int pair[2];
205
206 /* Create two connected sockets for migration */
207 ret = qemu_socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
208 g_assert_cmpint(ret, ==, 0);
209
210 /* Send the 1st socket to the target */
211 qtest_qmp_fds_assert_success(to, &pair[0], 1,
212 "{ 'execute': 'getfd',"
213 " 'arguments': { 'fdname': 'fd-mig' }}");
214 close(pair[0]);
215
216 /* Send the 2nd socket to the target */
217 qtest_qmp_fds_assert_success(from, &pair[1], 1,
218 "{ 'execute': 'getfd',"
219 " 'arguments': { 'fdname': 'fd-mig' }}");
220 close(pair[1]);
221
222 return NULL;
223 }
224
225 static void migrate_hook_end_fd(QTestState *from,
226 QTestState *to,
227 void *opaque)
228 {
229 QDict *rsp;
230 const char *error_desc;
231
232 /* Test closing fds */
233 /*
234 * We assume, that QEMU removes named fd from its list,
235 * so this should fail.
236 */
237 rsp = qtest_qmp(from,
238 "{ 'execute': 'closefd',"
239 " 'arguments': { 'fdname': 'fd-mig' }}");
240 g_assert_true(qdict_haskey(rsp, "error"));
241 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
242 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
243 qobject_unref(rsp);
244
245 rsp = qtest_qmp(to,
246 "{ 'execute': 'closefd',"
247 " 'arguments': { 'fdname': 'fd-mig' }}");
248 g_assert_true(qdict_haskey(rsp, "error"));
249 error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
250 g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
251 qobject_unref(rsp);
252 }
253
254 static void test_precopy_fd_socket(char *name, MigrateCommon *args)
255 {
256 args->uri = "fd:fd-mig";
257 args->start_hook = migrate_hook_start_fd;
258 args->end_hook = migrate_hook_end_fd;
259
260 test_precopy_common(args);
261 }
262 #endif /* _WIN32 */
263
264 static void test_auto_converge(char *name, MigrateCommon *args)
265 {
266 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
267 QTestState *from, *to;
268 int64_t percentage;
269 const int64_t init_pct = 5, inc_pct = 25, max_pct = 95;
270
271 if (migrate_start(&from, &to, &args->start)) {
272 return;
273 }
274
275 migrate_set_capability(from, "auto-converge", true);
276 migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
277 migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
278 migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
279
280 migrate_ensure_non_converge(from);
281
282 /* To check remaining size after precopy */
283 migrate_set_capability(from, "pause-before-switchover", true);
284
285 wait_for_serial("src_serial");
286
287 migrate_incoming_qmp(to, uri, NULL, "{}");
288 migrate_qmp(from, to, uri, NULL, "{}");
289
290 /* Wait until throttling begins */
291 percentage = 0;
292 do {
293 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
294 if (percentage != 0) {
295 break;
296 }
297 usleep(20);
298 g_assert_false(get_src()->stop_seen);
299 } while (true);
300 /* The first percentage of throttling should be at least init_pct */
301 g_assert_cmpint(percentage, >=, init_pct);
302
303 /* throttling always ignores the first pass */
304 assert(get_migration_pass(from) == 2);
305 migrate_ensure_converge(from);
306
307 /*
308 * Wait for pre-switchover status to check last throttle percentage
309 * and remaining. These values will be zeroed later
310 */
311 wait_for_migration_status(from, "pre-switchover", NULL);
312
313 /* The final percentage of throttling shouldn't be greater than max_pct */
314 percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
315 g_assert_cmpint(percentage, <=, max_pct);
316 migrate_continue(from, "pre-switchover");
317
318 qtest_qmp_eventwait(to, "RESUME");
319
320 wait_for_serial("dest_serial");
321 wait_for_migration_complete(from);
322
323 migrate_end(from, to, true);
324 }
325
326 static void *
327 migrate_hook_start_precopy_tcp_multifd_zero_page_legacy(QTestState *from,
328 QTestState *to)
329 {
330 migrate_set_parameter_str(from, "zero-page-detection", "legacy");
331 return NULL;
332 }
333
334 static void *
335 migrate_hook_start_precopy_tcp_multifd_no_zero_page(QTestState *from,
336 QTestState *to)
337 {
338 migrate_set_parameter_str(from, "zero-page-detection", "none");
339 return NULL;
340 }
341
342 static void test_multifd_tcp_uri_none(char *name, MigrateCommon *args)
343 {
344 /*
345 * Multifd is more complicated than most of the features, it
346 * directly takes guest page buffers when sending, make sure
347 * everything will work alright even if guest page is changing.
348 */
349 args->live = true;
350
351 args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
352
353 test_precopy_common(args);
354 }
355
356 static void test_multifd_tcp_zero_page_legacy(char *name, MigrateCommon *args)
357 {
358 args->start_hook = migrate_hook_start_precopy_tcp_multifd_zero_page_legacy;
359 /*
360 * Multifd is more complicated than most of the features, it
361 * directly takes guest page buffers when sending, make sure
362 * everything will work alright even if guest page is changing.
363 */
364 args->live = true;
365
366 args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
367
368 test_precopy_common(args);
369 }
370
371 static void test_multifd_tcp_no_zero_page(char *name, MigrateCommon *args)
372 {
373 args->start_hook = migrate_hook_start_precopy_tcp_multifd_no_zero_page;
374 /*
375 * Multifd is more complicated than most of the features, it
376 * directly takes guest page buffers when sending, make sure
377 * everything will work alright even if guest page is changing.
378 */
379 args->live = true;
380
381 args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
382
383 test_precopy_common(args);
384 }
385
386 static void test_multifd_tcp_channels_none(char *name, MigrateCommon *args)
387 {
388 args->live = true;
389 args->connect_channels = ("[ { 'channel-type': 'main',"
390 " 'addr': { 'transport': 'socket',"
391 " 'type': 'inet',"
392 " 'host': '127.0.0.1',"
393 " 'port': '0' } } ]");
394
395 args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
396
397 test_precopy_common(args);
398 }
399
400 /*
401 * This test does:
402 * source target
403 * migrate_incoming
404 * migrate
405 * migrate_cancel
406 * launch another target
407 * migrate
408 *
409 * And see that it works
410 */
411 static void test_multifd_tcp_cancel(MigrateCommon *args, bool postcopy_ram)
412 {
413 QTestState *from, *to, *to2;
414
415 args->start.hide_stderr = true;
416
417 if (migrate_start(&from, &to, &args->start)) {
418 return;
419 }
420
421 migrate_ensure_non_converge(from);
422 migrate_prepare_for_dirty_mem(from);
423
424 if (postcopy_ram) {
425 migrate_set_capability(from, "postcopy-ram", true);
426 migrate_set_capability(to, "postcopy-ram", true);
427 }
428
429 migrate_set_parameter_int(from, "multifd-channels", 16);
430 migrate_set_parameter_int(to, "multifd-channels", 16);
431
432 migrate_set_capability(from, "multifd", true);
433 migrate_set_capability(to, "multifd", true);
434
435 /* Start incoming migration from the 1st socket */
436 migrate_incoming_qmp(to, "tcp:127.0.0.1:0", NULL, "{}");
437
438 /* Wait for the first serial output from the source */
439 wait_for_serial("src_serial");
440
441 migrate_qmp(from, to, NULL, NULL, "{}");
442
443 migrate_wait_for_dirty_mem(from, to);
444
445 migrate_cancel(from);
446
447 /* Make sure QEMU process "to" exited */
448 migration_event_wait(to, "failed");
449 qtest_quit(to);
450
451 /*
452 * Ensure the source QEMU finishes its cancellation process before we
453 * proceed with the setup of the next migration. The migrate_start()
454 * function and others might want to interact with the source in a way that
455 * is not possible while the migration is not canceled properly. For
456 * example, setting migration capabilities when the migration is still
457 * running leads to an error.
458 */
459 wait_for_migration_status(from, "cancelled", NULL);
460
461 args->start.only_target = true;
462
463 if (migrate_start(&from, &to2, &args->start)) {
464 return;
465 }
466
467 if (postcopy_ram) {
468 migrate_set_capability(to2, "postcopy-ram", true);
469 }
470
471 migrate_set_parameter_int(to2, "multifd-channels", 16);
472
473 migrate_set_capability(to2, "multifd", true);
474
475 /* Start incoming migration from the 1st socket */
476 migrate_incoming_qmp(to2, "tcp:127.0.0.1:0", NULL, "{}");
477
478 migrate_ensure_non_converge(from);
479
480 migrate_qmp(from, to2, NULL, NULL, "{}");
481
482 migrate_wait_for_dirty_mem(from, to2);
483
484 migrate_ensure_converge(from);
485
486 wait_for_stop(from, get_src());
487 qtest_qmp_eventwait(to2, "RESUME");
488
489 wait_for_serial("dest_serial");
490 wait_for_migration_complete(from);
491 migrate_end(from, to2, true);
492 }
493
494 static void test_multifd_precopy_tcp_cancel(char *name, MigrateCommon *args)
495 {
496 test_multifd_tcp_cancel(args, false);
497 }
498
499 static void test_multifd_postcopy_tcp_cancel(char *name, MigrateCommon *args)
500 {
501 test_multifd_tcp_cancel(args, true);
502 }
503
504 static void test_cancel_src_after_failed(QTestState *from, QTestState *to,
505 const char *uri, const char *phase,
506 MigrateStart *args)
507 {
508 /*
509 * No migrate_incoming_qmp() at the start to force source into
510 * failed state during migrate_qmp().
511 */
512
513 wait_for_serial("src_serial");
514 migrate_ensure_converge(from);
515
516 migrate_qmp(from, to, uri, NULL, "{}");
517
518 migration_event_wait(from, phase);
519 migrate_cancel(from);
520
521 /* cancelling will not move the migration out of 'failed' */
522
523 wait_for_migration_status(from, "failed",
524 (const char * []) { "completed", NULL });
525
526 /*
527 * Not waiting for the destination because it never started
528 * migration.
529 */
530 }
531
532 static void test_cancel_src_after_cancelled(QTestState *from, QTestState *to,
533 const char *uri, const char *phase,
534 MigrateStart *args)
535 {
536 migrate_incoming_qmp(to, uri, NULL, "{}");
537
538 wait_for_serial("src_serial");
539 migrate_ensure_converge(from);
540
541 migrate_qmp(from, to, uri, NULL, "{}");
542
543 /* To move to cancelled/cancelling */
544 migrate_cancel(from);
545 migration_event_wait(from, phase);
546
547 /* The migrate_cancel under test */
548 migrate_cancel(from);
549
550 wait_for_migration_status(from, "cancelled",
551 (const char * []) { "completed", NULL });
552
553 wait_for_migration_status(to, "failed",
554 (const char * []) { "completed", NULL });
555 }
556
557 static void test_cancel_src_after_complete(QTestState *from, QTestState *to,
558 const char *uri, const char *phase,
559 MigrateStart *args)
560 {
561 migrate_incoming_qmp(to, uri, NULL, "{}");
562
563 wait_for_serial("src_serial");
564 migrate_ensure_converge(from);
565
566 migrate_qmp(from, to, uri, NULL, "{}");
567
568 migration_event_wait(from, phase);
569 migrate_cancel(from);
570
571 /*
572 * qmp_migrate_cancel() exits early if migration is not running
573 * anymore, the status will not change to cancelled.
574 */
575 wait_for_migration_complete(from);
576 wait_for_migration_complete(to);
577 }
578
579 static void test_cancel_src_after_none(QTestState *from, QTestState *to,
580 const char *uri, const char *phase,
581 MigrateStart *args)
582 {
583 /*
584 * Test that cancelling without a migration happening does not
585 * affect subsequent migrations
586 */
587 migrate_cancel(to);
588
589 wait_for_serial("src_serial");
590 migrate_cancel(from);
591
592 migrate_incoming_qmp(to, uri, NULL, "{}");
593
594 migrate_ensure_converge(from);
595 migrate_qmp(from, to, uri, NULL, "{}");
596
597 wait_for_migration_complete(from);
598 wait_for_migration_complete(to);
599 }
600
601 static void test_cancel_src_pre_switchover(QTestState *from, QTestState *to,
602 const char *uri, const char *phase,
603 MigrateStart *args)
604 {
605 migrate_set_capability(from, "pause-before-switchover", true);
606 migrate_set_capability(to, "pause-before-switchover", true);
607
608 migrate_set_capability(from, "multifd", true);
609 migrate_set_capability(to, "multifd", true);
610
611 migrate_incoming_qmp(to, uri, NULL, "{}");
612
613 wait_for_serial("src_serial");
614 migrate_ensure_converge(from);
615
616 migrate_qmp(from, to, uri, NULL, "{}");
617
618 migration_event_wait(from, phase);
619 migrate_cancel(from);
620 migration_event_wait(from, "cancelling");
621
622 wait_for_migration_status(from, "cancelled",
623 (const char * []) { "completed", NULL });
624
625 wait_for_migration_status(to, "failed",
626 (const char * []) { "completed", NULL });
627 }
628
629 static void test_cancel_src_after_status(char *test_path, MigrateCommon *args)
630 {
631 g_autofree char *phase = g_path_get_basename(test_path);
632 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
633 QTestState *from, *to;
634
635 args->start.hide_stderr = true;
636
637 if (migrate_start(&from, &to, &args->start)) {
638 return;
639 }
640
641 if (g_str_equal(phase, "cancelling") ||
642 g_str_equal(phase, "cancelled")) {
643 test_cancel_src_after_cancelled(from, to, uri, phase, &args->start);
644
645 } else if (g_str_equal(phase, "completed")) {
646 test_cancel_src_after_complete(from, to, uri, phase, &args->start);
647
648 } else if (g_str_equal(phase, "failed")) {
649 test_cancel_src_after_failed(from, to, uri, phase, &args->start);
650
651 } else if (g_str_equal(phase, "none")) {
652 test_cancel_src_after_none(from, to, uri, phase, &args->start);
653
654 } else {
655 /* any state that comes before pre-switchover */
656 test_cancel_src_pre_switchover(from, to, uri, phase, &args->start);
657 }
658
659 migrate_end(from, to, false);
660 }
661
662 static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
663 {
664 qtest_qmp_assert_success(who,
665 "{ 'execute': 'calc-dirty-rate',"
666 "'arguments': { "
667 "'calc-time': %" PRIu64 ","
668 "'mode': 'dirty-ring' }}",
669 calc_time);
670 }
671
672 static QDict *query_dirty_rate(QTestState *who)
673 {
674 return qtest_qmp_assert_success_ref(who,
675 "{ 'execute': 'query-dirty-rate' }");
676 }
677
678 static void dirtylimit_set_all(QTestState *who, uint64_t dirtyrate)
679 {
680 qtest_qmp_assert_success(who,
681 "{ 'execute': 'set-vcpu-dirty-limit',"
682 "'arguments': { "
683 "'dirty-rate': %" PRIu64 " } }",
684 dirtyrate);
685 }
686
687 static void cancel_vcpu_dirty_limit(QTestState *who)
688 {
689 qtest_qmp_assert_success(who,
690 "{ 'execute': 'cancel-vcpu-dirty-limit' }");
691 }
692
693 static QDict *query_vcpu_dirty_limit(QTestState *who)
694 {
695 QDict *rsp;
696
697 rsp = qtest_qmp(who, "{ 'execute': 'query-vcpu-dirty-limit' }");
698 g_assert(!qdict_haskey(rsp, "error"));
699 g_assert(qdict_haskey(rsp, "return"));
700
701 return rsp;
702 }
703
704 static bool calc_dirtyrate_ready(QTestState *who)
705 {
706 QDict *rsp_return;
707 const char *status;
708 bool ready;
709
710 rsp_return = query_dirty_rate(who);
711 g_assert(rsp_return);
712
713 status = qdict_get_str(rsp_return, "status");
714 g_assert(status);
715 ready = g_strcmp0(status, "measuring");
716 qobject_unref(rsp_return);
717
718 return ready;
719 }
720
721 static void wait_for_calc_dirtyrate_complete(QTestState *who,
722 int64_t time_s)
723 {
724 int max_try_count = 10000;
725 usleep(time_s * 1000000);
726
727 while (!calc_dirtyrate_ready(who) && max_try_count--) {
728 usleep(1000);
729 }
730
731 /*
732 * Set the timeout with 10 s(max_try_count * 1000us),
733 * if dirtyrate measurement not complete, fail test.
734 */
735 g_assert_cmpint(max_try_count, !=, 0);
736 }
737
738 static int64_t get_dirty_rate(QTestState *who)
739 {
740 QDict *rsp_return;
741 const char *status;
742 QList *rates;
743 const QListEntry *entry;
744 QDict *rate;
745 int64_t dirtyrate;
746
747 rsp_return = query_dirty_rate(who);
748 g_assert(rsp_return);
749
750 status = qdict_get_str(rsp_return, "status");
751 g_assert(status);
752 g_assert_cmpstr(status, ==, "measured");
753
754 rates = qdict_get_qlist(rsp_return, "vcpu-dirty-rate");
755 g_assert(rates && !qlist_empty(rates));
756
757 entry = qlist_first(rates);
758 g_assert(entry);
759
760 rate = qobject_to(QDict, qlist_entry_obj(entry));
761 g_assert(rate);
762
763 dirtyrate = qdict_get_try_int(rate, "dirty-rate", -1);
764
765 qobject_unref(rsp_return);
766 return dirtyrate;
767 }
768
769 static int64_t get_limit_rate(QTestState *who)
770 {
771 QDict *rsp_return;
772 QList *rates;
773 const QListEntry *entry;
774 QDict *rate;
775 int64_t dirtyrate;
776
777 rsp_return = query_vcpu_dirty_limit(who);
778 g_assert(rsp_return);
779
780 rates = qdict_get_qlist(rsp_return, "return");
781 g_assert(rates && !qlist_empty(rates));
782
783 entry = qlist_first(rates);
784 g_assert(entry);
785
786 rate = qobject_to(QDict, qlist_entry_obj(entry));
787 g_assert(rate);
788
789 dirtyrate = qdict_get_try_int(rate, "limit-rate", -1);
790
791 qobject_unref(rsp_return);
792 return dirtyrate;
793 }
794
795 static QTestState *dirtylimit_start_vm(void)
796 {
797 QTestState *vm = NULL;
798 g_autofree gchar *cmd = NULL;
799 const char *bootpath;
800
801 bootpath = bootfile_create(qtest_get_arch(), tmpfs, false);
802 cmd = g_strdup_printf("-accel kvm,dirty-ring-size=4096 "
803 "-name dirtylimit-test,debug-threads=on "
804 "-m 150M -smp 1 "
805 "-serial file:%s/vm_serial "
806 "-drive file=%s,format=raw ",
807 tmpfs, bootpath);
808
809 vm = qtest_init(cmd);
810 return vm;
811 }
812
813 static void dirtylimit_stop_vm(QTestState *vm)
814 {
815 g_autofree char *path = g_strdup_printf("%s/%s", tmpfs, "vm_serial");
816
817 qtest_quit(vm);
818 unlink(path);
819 }
820
821 static void test_vcpu_dirty_limit(char *name, MigrateCommon *args)
822 {
823 QTestState *vm;
824 int64_t origin_rate;
825 int64_t quota_rate;
826 int64_t rate ;
827 int max_try_count = 20;
828 int hit = 0;
829
830 /* Start vm for vcpu dirtylimit test */
831 vm = dirtylimit_start_vm();
832
833 /* Wait for the first serial output from the vm*/
834 wait_for_serial("vm_serial");
835
836 /* Do dirtyrate measurement with calc time equals 1s */
837 calc_dirty_rate(vm, 1);
838
839 /* Sleep calc time and wait for calc dirtyrate complete */
840 wait_for_calc_dirtyrate_complete(vm, 1);
841
842 /* Query original dirty page rate */
843 origin_rate = get_dirty_rate(vm);
844
845 /* VM booted from bootsect should dirty memory steadily */
846 assert(origin_rate != 0);
847
848 /* Setup quota dirty page rate at half of origin */
849 quota_rate = origin_rate / 2;
850
851 /* Set dirtylimit */
852 dirtylimit_set_all(vm, quota_rate);
853
854 /*
855 * Check if set-vcpu-dirty-limit and query-vcpu-dirty-limit
856 * works literally
857 */
858 g_assert_cmpint(quota_rate, ==, get_limit_rate(vm));
859
860 /* Sleep a bit to check if it take effect */
861 usleep(2000000);
862
863 /*
864 * Check if dirtylimit take effect realistically, set the
865 * timeout with 20 s(max_try_count * 1s), if dirtylimit
866 * doesn't take effect, fail test.
867 */
868 while (--max_try_count) {
869 calc_dirty_rate(vm, 1);
870 wait_for_calc_dirtyrate_complete(vm, 1);
871 rate = get_dirty_rate(vm);
872
873 /*
874 * Assume hitting if current rate is less
875 * than quota rate (within accepting error)
876 */
877 if (rate < (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
878 hit = 1;
879 break;
880 }
881 }
882
883 g_assert_cmpint(hit, ==, 1);
884
885 hit = 0;
886 max_try_count = 20;
887
888 /* Check if dirtylimit cancellation take effect */
889 cancel_vcpu_dirty_limit(vm);
890 while (--max_try_count) {
891 calc_dirty_rate(vm, 1);
892 wait_for_calc_dirtyrate_complete(vm, 1);
893 rate = get_dirty_rate(vm);
894
895 /*
896 * Assume dirtylimit be canceled if current rate is
897 * greater than quota rate (within accepting error)
898 */
899 if (rate > (quota_rate + DIRTYLIMIT_TOLERANCE_RANGE)) {
900 hit = 1;
901 break;
902 }
903 }
904
905 g_assert_cmpint(hit, ==, 1);
906 dirtylimit_stop_vm(vm);
907 }
908
909 static void migrate_dirty_limit_wait_showup(QTestState *from,
910 const int64_t period,
911 const int64_t value)
912 {
913 /* Enable dirty limit capability */
914 migrate_set_capability(from, "dirty-limit", true);
915
916 /* Set dirty limit parameters */
917 migrate_set_parameter_int(from, "x-vcpu-dirty-limit-period", period);
918 migrate_set_parameter_int(from, "vcpu-dirty-limit", value);
919
920 /* Make sure migrate can't converge */
921 migrate_ensure_non_converge(from);
922
923 /* To check limit rate after precopy */
924 migrate_set_capability(from, "pause-before-switchover", true);
925
926 /* Wait for the serial output from the source */
927 wait_for_serial("src_serial");
928 }
929
930 /*
931 * This test does:
932 * source destination
933 * start vm
934 * start incoming vm
935 * migrate
936 * wait dirty limit to begin
937 * cancel migrate
938 * cancellation check
939 * restart incoming vm
940 * migrate
941 * wait dirty limit to begin
942 * wait pre-switchover event
943 * convergence condition check
944 *
945 * And see if dirty limit migration works correctly.
946 * This test case involves many passes, so it runs in slow mode only.
947 */
948 static void test_dirty_limit(char *name, MigrateCommon *args)
949 {
950 g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
951 QTestState *from, *to;
952 int64_t remaining;
953 uint64_t throttle_us_per_full;
954 /*
955 * We want the test to be stable and as fast as possible.
956 * E.g., with 1Gb/s bandwidth migration may pass without dirty limit,
957 * so we need to decrease a bandwidth.
958 */
959 const int64_t dirtylimit_period = 1000, dirtylimit_value = 50;
960 const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
961 const int64_t downtime_limit = 250; /* 250ms */
962 /*
963 * We migrate through unix-socket (> 500Mb/s).
964 * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
965 * So, we can predict expected_threshold
966 */
967 const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
968 int max_try_count = 10;
969
970 args->start.hide_stderr = true;
971 args->start.use_dirty_ring = true;
972
973 args->uri = uri;
974
975 /* Start src, dst vm */
976 if (migrate_start(&from, &to, &args->start)) {
977 return;
978 }
979
980 /* Prepare for dirty limit migration and wait src vm show up */
981 migrate_dirty_limit_wait_showup(from, dirtylimit_period, dirtylimit_value);
982
983 /* Start migrate */
984 migrate_incoming_qmp(to, args->uri, NULL, "{}");
985 migrate_qmp(from, to, args->uri, NULL, "{}");
986
987 /* Wait for dirty limit throttle begin */
988 throttle_us_per_full = 0;
989 while (throttle_us_per_full == 0) {
990 throttle_us_per_full =
991 read_migrate_property_int(from,
992 "dirty-limit-throttle-time-per-round");
993 usleep(100);
994 g_assert_false(get_src()->stop_seen);
995 }
996
997 /* Now cancel migrate and wait for dirty limit throttle switch off */
998 migrate_cancel(from);
999 wait_for_migration_status(from, "cancelled", NULL);
1000
1001 /* destination always fails after cancel */
1002 migration_event_wait(to, "failed");
1003 qtest_quit(to);
1004
1005 /* Check if dirty limit throttle switched off, set timeout 1ms */
1006 do {
1007 throttle_us_per_full =
1008 read_migrate_property_int(from,
1009 "dirty-limit-throttle-time-per-round");
1010 usleep(100);
1011 g_assert_false(get_src()->stop_seen);
1012 } while (throttle_us_per_full != 0 && --max_try_count);
1013
1014 /* Assert dirty limit is not in service */
1015 g_assert_cmpint(throttle_us_per_full, ==, 0);
1016
1017 args->uri = uri;
1018
1019 args->start.only_target = true;
1020 args->start.use_dirty_ring = true;
1021
1022 /* Restart dst vm, src vm already show up so we needn't wait anymore */
1023 if (migrate_start(&from, &to, &args->start)) {
1024 return;
1025 }
1026
1027 /* Start migrate */
1028 migrate_incoming_qmp(to, args->uri, NULL, "{}");
1029 migrate_qmp(from, to, args->uri, NULL, "{}");
1030
1031 /* Wait for dirty limit throttle begin */
1032 throttle_us_per_full = 0;
1033 while (throttle_us_per_full == 0) {
1034 throttle_us_per_full =
1035 read_migrate_property_int(from,
1036 "dirty-limit-throttle-time-per-round");
1037 usleep(100);
1038 g_assert_false(get_src()->stop_seen);
1039 }
1040
1041 /*
1042 * The dirty limit rate should equals the return value of
1043 * query-vcpu-dirty-limit if dirty limit cap set
1044 */
1045 g_assert_cmpint(dirtylimit_value, ==, get_limit_rate(from));
1046
1047 /* Now, we have tested if dirty limit works, let it converge */
1048 migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
1049 migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
1050
1051 /*
1052 * Wait for pre-switchover status to check if migration
1053 * satisfy the convergence condition
1054 */
1055 wait_for_migration_status(from, "pre-switchover", NULL);
1056
1057 remaining = read_ram_property_int(from, "remaining");
1058 g_assert_cmpint(remaining, <,
1059 (expected_threshold + expected_threshold / 100));
1060
1061 migrate_continue(from, "pre-switchover");
1062
1063 qtest_qmp_eventwait(to, "RESUME");
1064
1065 wait_for_serial("dest_serial");
1066 wait_for_migration_complete(from);
1067
1068 migrate_end(from, to, true);
1069 }
1070
1071 static void migration_test_add_precopy_smoke(MigrationTestEnv *env)
1072 {
1073 if (env->is_x86) {
1074 migration_test_add("/migration/precopy/unix/suspend/live",
1075 test_precopy_unix_suspend_live);
1076 migration_test_add("/migration/precopy/unix/suspend/notlive",
1077 test_precopy_unix_suspend_notlive);
1078 }
1079
1080 migration_test_add("/migration/precopy/unix/plain",
1081 test_precopy_unix_plain);
1082
1083 migration_test_add("/migration/precopy/tcp/plain", test_precopy_tcp_plain);
1084 migration_test_add("/migration/multifd/tcp/uri/plain/none",
1085 test_multifd_tcp_uri_none);
1086 migration_test_add("/migration/multifd/tcp/plain/cancel",
1087 test_multifd_precopy_tcp_cancel);
1088 if (env->has_uffd) {
1089 migration_test_add("/migration/multifd+postcopy/tcp/plain/cancel",
1090 test_multifd_postcopy_tcp_cancel);
1091 }
1092
1093 #ifdef CONFIG_RDMA
1094 migration_test_add("/migration/precopy/rdma/plain",
1095 test_precopy_rdma_plain);
1096 migration_test_add("/migration/precopy/rdma/plain/ipv6",
1097 test_precopy_rdma_plain_ipv6);
1098 #endif
1099 }
1100
1101 void migration_test_add_precopy(MigrationTestEnv *env)
1102 {
1103 tmpfs = env->tmpfs;
1104
1105 migration_test_add_precopy_smoke(env);
1106
1107 if (!env->full_set) {
1108 return;
1109 }
1110
1111 migration_test_add("/migration/precopy/tcp/plain/switchover-ack",
1112 test_precopy_tcp_switchover_ack);
1113
1114 #ifndef _WIN32
1115 migration_test_add("/migration/precopy/fd/tcp",
1116 test_precopy_fd_socket);
1117 #endif
1118
1119 /*
1120 * See explanation why this test is slow on function definition
1121 */
1122 if (g_test_slow()) {
1123 migration_test_add("/migration/auto_converge",
1124 test_auto_converge);
1125 if (g_str_equal(env->arch, "x86_64") && env->has_dirty_ring) {
1126 migration_test_add("/dirty_limit",
1127 test_dirty_limit);
1128 }
1129 }
1130 migration_test_add("/migration/multifd/tcp/channels/plain/none",
1131 test_multifd_tcp_channels_none);
1132 migration_test_add("/migration/multifd/tcp/plain/zero-page/legacy",
1133 test_multifd_tcp_zero_page_legacy);
1134 migration_test_add("/migration/multifd/tcp/plain/zero-page/none",
1135 test_multifd_tcp_no_zero_page);
1136 if (g_str_equal(env->arch, "x86_64")
1137 && env->has_kvm && env->has_dirty_ring) {
1138
1139 migration_test_add("/migration/dirty_ring",
1140 test_precopy_unix_dirty_ring);
1141 if (qtest_has_machine("pc") && g_test_slow()) {
1142 migration_test_add("/migration/vcpu_dirty_limit",
1143 test_vcpu_dirty_limit);
1144 }
1145 }
1146
1147 /* ensure new status don't go unnoticed */
1148 assert(MIGRATION_STATUS__MAX == 17);
1149
1150 for (int i = MIGRATION_STATUS_NONE; i < MIGRATION_STATUS__MAX; i++) {
1151 switch (i) {
1152 case MIGRATION_STATUS_DEVICE: /* happens too fast */
1153 case MIGRATION_STATUS_WAIT_UNPLUG: /* no support in tests */
1154 case MIGRATION_STATUS_COLO: /* no support in tests */
1155 case MIGRATION_STATUS_POSTCOPY_DEVICE: /* postcopy can't be cancelled */
1156 case MIGRATION_STATUS_POSTCOPY_ACTIVE: /* postcopy can't be cancelled */
1157 case MIGRATION_STATUS_POSTCOPY_PAUSED:
1158 case MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP:
1159 case MIGRATION_STATUS_POSTCOPY_RECOVER:
1160 case MIGRATION_STATUS_FAILING:
1161 continue;
1162 default:
1163 migration_test_add_suffix("/migration/cancel/src/after/",
1164 MigrationStatus_str(i),
1165 test_cancel_src_after_status);
1166 }
1167 }
1168 }