master
c 1,612 lines 49.9 KB
Raw
1 /*
2 * QEMU migration capabilities
3 *
4 * Copyright (c) 2012-2023 Red Hat Inc
5 *
6 * Authors:
7 * Orit Wasserman <owasserm@redhat.com>
8 * Juan Quintela <quintela@redhat.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
12 */
13
14 #include "qemu/osdep.h"
15 #include "qemu/error-report.h"
16 #include "qemu/units.h"
17 #include "exec/target_page.h"
18 #include "qapi/clone-visitor.h"
19 #include "qapi/error.h"
20 #include "qapi/qapi-commands-migration.h"
21 #include "qapi/qapi-visit-migration.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qobject/qnull.h"
24 #include "system/runstate.h"
25 #include "migration/colo.h"
26 #include "migration/cpr.h"
27 #include "migration/misc.h"
28 #include "migration.h"
29 #include "migration-stats.h"
30 #include "qemu-file.h"
31 #include "ram.h"
32 #include "options.h"
33 #include "system/kvm.h"
34
35 /* Maximum migrate downtime set to 2000 seconds */
36 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
37 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
38
39 #define MAX_THROTTLE (128 << 20) /* Migration transfer speed throttling */
40
41 /* Time in milliseconds we are allowed to stop the source,
42 * for sending the last part */
43 #define DEFAULT_MIGRATE_SET_DOWNTIME 300
44
45 /* Define default autoconverge cpu throttle migration parameters */
46 #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
47 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
48 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
49 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
50
51 /* Migration XBZRLE default cache size */
52 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
53
54 /* The delay time (in ms) between two COLO checkpoints */
55 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
56 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
57 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
58 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
59 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
60 /*
61 * 1: best speed, ... 9: best compress ratio
62 * There is some nuance here. Refer to QATzip documentation to understand
63 * the mapping of QATzip levels to standard deflate levels.
64 */
65 #define DEFAULT_MIGRATE_MULTIFD_QATZIP_LEVEL 1
66
67 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
68 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
69
70 /* Background transfer rate for postcopy, 0 means unlimited, note
71 * that page requests can still exceed this limit.
72 */
73 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
74
75 /*
76 * Parameters for self_announce_delay giving a stream of RARP/ARP
77 * packets after migration.
78 */
79 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
80 #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
81 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
82 #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
83
84 #define DEFINE_PROP_MIG_CAP(name, x) \
85 DEFINE_PROP_BOOL(name, MigrationState, capabilities[x], false)
86
87 const PropertyInfo qdev_prop_StrOrNull;
88 #define DEFINE_PROP_STR_OR_NULL(_name, _state, _field) \
89 DEFINE_PROP(_name, _state, _field, qdev_prop_StrOrNull, StrOrNull *, \
90 .set_default = true)
91
92 #define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD 1000 /* milliseconds */
93 #define DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT 1 /* MB/s */
94 #define DEFAULT_MIGRATE_X_RDMA_CHUNK_SIZE MiB
95
96 const Property migration_properties[] = {
97 DEFINE_PROP_BOOL("store-global-state", MigrationState,
98 store_global_state, true),
99 DEFINE_PROP_BOOL("send-configuration", MigrationState,
100 send_configuration, true),
101 DEFINE_PROP_BOOL("send-section-footer", MigrationState,
102 send_section_footer, true),
103 DEFINE_PROP_BOOL("send-switchover-start", MigrationState,
104 send_switchover_start, true),
105 DEFINE_PROP_BOOL("multifd-flush-after-each-section", MigrationState,
106 multifd_flush_after_each_section, false),
107 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
108 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
109 DEFINE_PROP_BOOL("x-preempt-pre-7-2", MigrationState,
110 preempt_pre_7_2, false),
111 DEFINE_PROP_BOOL("multifd-clean-tls-termination", MigrationState,
112 multifd_clean_tls_termination, true),
113 DEFINE_PROP_BOOL("switchover-ack-legacy", MigrationState,
114 switchover_ack_legacy, false),
115
116 /* Migration parameters */
117 DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
118 parameters.throttle_trigger_threshold,
119 DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
120 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
121 parameters.cpu_throttle_initial,
122 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
123 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
124 parameters.cpu_throttle_increment,
125 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
126 DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
127 parameters.cpu_throttle_tailslow, false),
128 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
129 parameters.max_bandwidth, MAX_THROTTLE),
130 DEFINE_PROP_SIZE("avail-switchover-bandwidth", MigrationState,
131 parameters.avail_switchover_bandwidth, 0),
132 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
133 parameters.downtime_limit,
134 DEFAULT_MIGRATE_SET_DOWNTIME),
135 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
136 parameters.x_checkpoint_delay,
137 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
138 DEFINE_PROP_UINT8("multifd-channels", MigrationState,
139 parameters.multifd_channels,
140 DEFAULT_MIGRATE_MULTIFD_CHANNELS),
141 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
142 parameters.multifd_compression,
143 DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
144 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
145 parameters.multifd_zlib_level,
146 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
147 DEFINE_PROP_UINT8("multifd-qatzip-level", MigrationState,
148 parameters.multifd_qatzip_level,
149 DEFAULT_MIGRATE_MULTIFD_QATZIP_LEVEL),
150 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
151 parameters.multifd_zstd_level,
152 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
153 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
154 parameters.xbzrle_cache_size,
155 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
156 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
157 parameters.max_postcopy_bandwidth,
158 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
159 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
160 parameters.max_cpu_throttle,
161 DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
162 DEFINE_PROP_SIZE("announce-initial", MigrationState,
163 parameters.announce_initial,
164 DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
165 DEFINE_PROP_SIZE("announce-max", MigrationState,
166 parameters.announce_max,
167 DEFAULT_MIGRATE_ANNOUNCE_MAX),
168 DEFINE_PROP_SIZE("announce-rounds", MigrationState,
169 parameters.announce_rounds,
170 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
171 DEFINE_PROP_SIZE("announce-step", MigrationState,
172 parameters.announce_step,
173 DEFAULT_MIGRATE_ANNOUNCE_STEP),
174 DEFINE_PROP_STR_OR_NULL("tls-creds", MigrationState, parameters.tls_creds),
175 DEFINE_PROP_STR_OR_NULL("tls-hostname", MigrationState,
176 parameters.tls_hostname),
177 DEFINE_PROP_STR_OR_NULL("tls-authz", MigrationState, parameters.tls_authz),
178 DEFINE_PROP_UINT64("x-vcpu-dirty-limit-period", MigrationState,
179 parameters.x_vcpu_dirty_limit_period,
180 DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT_PERIOD),
181 DEFINE_PROP_UINT64("vcpu-dirty-limit", MigrationState,
182 parameters.vcpu_dirty_limit,
183 DEFAULT_MIGRATE_VCPU_DIRTY_LIMIT),
184 DEFINE_PROP_MIG_MODE("mode", MigrationState,
185 parameters.mode,
186 MIG_MODE_NORMAL),
187 DEFINE_PROP_ZERO_PAGE_DETECTION("zero-page-detection", MigrationState,
188 parameters.zero_page_detection,
189 ZERO_PAGE_DETECTION_MULTIFD),
190 DEFINE_PROP_UINT64("x-rdma-chunk-size", MigrationState,
191 parameters.x_rdma_chunk_size,
192 DEFAULT_MIGRATE_X_RDMA_CHUNK_SIZE),
193
194 /* Migration capabilities */
195 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
196 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
197 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
198 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
199 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
200 DEFINE_PROP_MIG_CAP("x-postcopy-preempt",
201 MIGRATION_CAPABILITY_POSTCOPY_PREEMPT),
202 DEFINE_PROP_MIG_CAP("postcopy-blocktime",
203 MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME),
204 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
205 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
206 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
207 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
208 DEFINE_PROP_MIG_CAP("x-background-snapshot",
209 MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
210 #ifdef CONFIG_LINUX
211 DEFINE_PROP_MIG_CAP("x-zero-copy-send",
212 MIGRATION_CAPABILITY_ZERO_COPY_SEND),
213 #endif
214 DEFINE_PROP_MIG_CAP("x-switchover-ack",
215 MIGRATION_CAPABILITY_SWITCHOVER_ACK),
216 DEFINE_PROP_MIG_CAP("x-dirty-limit", MIGRATION_CAPABILITY_DIRTY_LIMIT),
217 DEFINE_PROP_MIG_CAP("mapped-ram", MIGRATION_CAPABILITY_MAPPED_RAM),
218 DEFINE_PROP_MIG_CAP("x-ignore-shared",
219 MIGRATION_CAPABILITY_X_IGNORE_SHARED),
220 };
221 const size_t migration_properties_count = ARRAY_SIZE(migration_properties);
222
223 static void get_StrOrNull(Object *obj, Visitor *v, const char *name,
224 void *opaque, Error **errp)
225 {
226 const Property *prop = opaque;
227 StrOrNull **ptr = object_field_prop_ptr(obj, prop);
228 StrOrNull *str_or_null = *ptr;
229
230 /*
231 * The property should never be NULL because it's part of
232 * s->parameters and a default value is always set by qdev. It
233 * should also never be QNULL as the setter doesn't allow it.
234 */
235 assert(str_or_null && str_or_null->type != QTYPE_QNULL);
236 visit_type_str(v, name, &str_or_null->u.s, errp);
237 }
238
239 static void set_StrOrNull(Object *obj, Visitor *v, const char *name,
240 void *opaque, Error **errp)
241 {
242 const Property *prop = opaque;
243 StrOrNull **ptr = object_field_prop_ptr(obj, prop);
244 StrOrNull *str_or_null;
245 char *str;
246
247 if (!visit_type_str(v, name, &str, errp)) {
248 return;
249 }
250
251 /*
252 * This property only applies to the command line usage of
253 * migration's TLS options (-global migration.tls-*) where the
254 * NULL value cannot be provided as input (only strings are
255 * allowed). Therefore, this StrOrNull implementation never
256 * produces a QNULL value to avoid ever returning values outside
257 * the range of what was previously handled by consumers of the
258 * TLS options.
259 */
260 str_or_null = g_new0(StrOrNull, 1);
261 str_or_null->type = QTYPE_QSTRING;
262 str_or_null->u.s = str;
263
264 qapi_free_StrOrNull(*ptr);
265 *ptr = str_or_null;
266 }
267
268 static void release_StrOrNull(Object *obj, const char *name, void *opaque)
269 {
270 const Property *prop = opaque;
271 qapi_free_StrOrNull(*(StrOrNull **)object_field_prop_ptr(obj, prop));
272 }
273
274 static void set_default_value_tls_opt(ObjectProperty *op, const Property *prop)
275 {
276 /*
277 * Initialization to the empty string here is important so
278 * query-migrate-parameters doesn't need to deal with a NULL value
279 * when it's called before any TLS option has been set.
280 */
281 object_property_set_default_str(op, "");
282 }
283
284 /*
285 * String property like qdev_prop_string, except it's backed by a
286 * StrOrNull instead of a char *. This is intended for
287 * TYPE_MIGRATION's TLS options.
288 */
289 const PropertyInfo qdev_prop_StrOrNull = {
290 .type = "StrOrNull",
291 .get = get_StrOrNull,
292 .set = set_StrOrNull,
293 .release = release_StrOrNull,
294 .set_default_value = set_default_value_tls_opt,
295 };
296
297 bool migrate_auto_converge(void)
298 {
299 MigrationState *s = migrate_get_current();
300
301 return s->capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
302 }
303
304 bool migrate_send_switchover_start(void)
305 {
306 MigrationState *s = migrate_get_current();
307
308 return s->send_switchover_start;
309 }
310
311 bool migrate_background_snapshot(void)
312 {
313 MigrationState *s = migrate_get_current();
314
315 return s->capabilities[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT];
316 }
317
318 bool migrate_colo(void)
319 {
320 MigrationState *s = migrate_get_current();
321
322 return s->capabilities[MIGRATION_CAPABILITY_X_COLO];
323 }
324
325 bool migrate_dirty_bitmaps(void)
326 {
327 MigrationState *s = migrate_get_current();
328
329 return s->capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
330 }
331
332 bool migrate_dirty_limit(void)
333 {
334 MigrationState *s = migrate_get_current();
335
336 return s->capabilities[MIGRATION_CAPABILITY_DIRTY_LIMIT];
337 }
338
339 bool migrate_events(void)
340 {
341 MigrationState *s = migrate_get_current();
342
343 return s->capabilities[MIGRATION_CAPABILITY_EVENTS];
344 }
345
346 bool migrate_mapped_ram(void)
347 {
348 MigrationState *s = migrate_get_current();
349
350 return s->capabilities[MIGRATION_CAPABILITY_MAPPED_RAM];
351 }
352
353 bool migrate_ignore_shared(void)
354 {
355 MigrationState *s = migrate_get_current();
356
357 return s->capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
358 }
359
360 bool migrate_late_block_activate(void)
361 {
362 MigrationState *s = migrate_get_current();
363
364 return s->capabilities[MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
365 }
366
367 bool migrate_multifd(void)
368 {
369 MigrationState *s = migrate_get_current();
370
371 return s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
372 }
373
374 bool migrate_pause_before_switchover(void)
375 {
376 MigrationState *s = migrate_get_current();
377
378 return s->capabilities[MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
379 }
380
381 bool migrate_postcopy_blocktime(void)
382 {
383 MigrationState *s = migrate_get_current();
384
385 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
386 }
387
388 bool migrate_postcopy_preempt(void)
389 {
390 MigrationState *s = migrate_get_current();
391
392 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
393 }
394
395 bool migrate_postcopy_ram(void)
396 {
397 MigrationState *s = migrate_get_current();
398
399 return s->capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
400 }
401
402 bool migrate_rdma_pin_all(void)
403 {
404 MigrationState *s = migrate_get_current();
405
406 return s->capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
407 }
408
409 bool migrate_release_ram(void)
410 {
411 MigrationState *s = migrate_get_current();
412
413 return s->capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
414 }
415
416 bool migrate_return_path(void)
417 {
418 MigrationState *s = migrate_get_current();
419
420 return s->capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
421 }
422
423 bool migrate_switchover_ack(void)
424 {
425 MigrationState *s = migrate_get_current();
426
427 return s->capabilities[MIGRATION_CAPABILITY_SWITCHOVER_ACK];
428 }
429
430 bool migrate_validate_uuid(void)
431 {
432 MigrationState *s = migrate_get_current();
433
434 return s->capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
435 }
436
437 bool migrate_xbzrle(void)
438 {
439 MigrationState *s = migrate_get_current();
440
441 return s->capabilities[MIGRATION_CAPABILITY_XBZRLE];
442 }
443
444 bool migrate_zero_copy_send(void)
445 {
446 MigrationState *s = migrate_get_current();
447
448 return s->capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND];
449 }
450
451 /* pseudo capabilities */
452
453 bool migrate_multifd_flush_after_each_section(void)
454 {
455 MigrationState *s = migrate_get_current();
456
457 return s->multifd_flush_after_each_section;
458 }
459
460 bool migrate_postcopy(void)
461 {
462 return migrate_postcopy_ram() || migrate_dirty_bitmaps();
463 }
464
465 bool migrate_rdma(void)
466 {
467 MigrationState *s = migrate_get_current();
468
469 return s->rdma_migration;
470 }
471
472 bool migrate_switchover_ack_legacy(void)
473 {
474 MigrationState *s = migrate_get_current();
475
476 return s->switchover_ack_legacy;
477 }
478
479 typedef enum WriteTrackingSupport {
480 WT_SUPPORT_UNKNOWN = 0,
481 WT_SUPPORT_ABSENT,
482 WT_SUPPORT_AVAILABLE,
483 WT_SUPPORT_COMPATIBLE
484 } WriteTrackingSupport;
485
486 static
487 WriteTrackingSupport migrate_query_write_tracking(void)
488 {
489 /* Check if kernel supports required UFFD features */
490 if (!ram_write_tracking_available()) {
491 return WT_SUPPORT_ABSENT;
492 }
493 /*
494 * Check if current memory configuration is
495 * compatible with required UFFD features.
496 */
497 if (!ram_write_tracking_compatible()) {
498 return WT_SUPPORT_AVAILABLE;
499 }
500
501 return WT_SUPPORT_COMPATIBLE;
502 }
503
504 /* Migration capabilities set */
505 struct MigrateCapsSet {
506 int size; /* Capability set size */
507 MigrationCapability caps[]; /* Variadic array of capabilities */
508 };
509 typedef struct MigrateCapsSet MigrateCapsSet;
510
511 /* Define and initialize MigrateCapsSet */
512 #define INITIALIZE_MIGRATE_CAPS_SET(_name, ...) \
513 MigrateCapsSet _name = { \
514 .size = sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
515 .caps = { __VA_ARGS__ } \
516 }
517
518 /* Background-snapshot compatibility check list */
519 static const
520 INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot,
521 MIGRATION_CAPABILITY_POSTCOPY_RAM,
522 MIGRATION_CAPABILITY_DIRTY_BITMAPS,
523 MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME,
524 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE,
525 MIGRATION_CAPABILITY_RETURN_PATH,
526 MIGRATION_CAPABILITY_MULTIFD,
527 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER,
528 MIGRATION_CAPABILITY_AUTO_CONVERGE,
529 MIGRATION_CAPABILITY_RELEASE_RAM,
530 MIGRATION_CAPABILITY_RDMA_PIN_ALL,
531 MIGRATION_CAPABILITY_XBZRLE,
532 MIGRATION_CAPABILITY_X_COLO,
533 MIGRATION_CAPABILITY_VALIDATE_UUID,
534 MIGRATION_CAPABILITY_ZERO_COPY_SEND);
535
536 /* Snapshot compatibility check list */
537 static const
538 INITIALIZE_MIGRATE_CAPS_SET(check_caps_savevm,
539 MIGRATION_CAPABILITY_MULTIFD,
540 );
541
542 static bool migrate_incoming_started(void)
543 {
544 return !!migration_incoming_get_current()->transport_data;
545 }
546
547 bool migrate_can_snapshot(Error **errp)
548 {
549 MigrationState *s = migrate_get_current();
550 int i;
551
552 for (i = 0; i < check_caps_savevm.size; i++) {
553 int incomp_cap = check_caps_savevm.caps[i];
554
555 if (s->capabilities[incomp_cap]) {
556 error_setg(errp,
557 "Snapshots are not compatible with %s",
558 MigrationCapability_str(incomp_cap));
559 return false;
560 }
561 }
562
563 return true;
564 }
565
566
567 bool migrate_rdma_caps_check(bool *caps, Error **errp)
568 {
569 if (caps[MIGRATION_CAPABILITY_XBZRLE]) {
570 error_setg(errp, "RDMA and XBZRLE can't be used together");
571 return false;
572 }
573 if (caps[MIGRATION_CAPABILITY_MULTIFD]) {
574 error_setg(errp, "RDMA and multifd can't be used together");
575 return false;
576 }
577 if (caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
578 error_setg(errp, "RDMA and postcopy-ram can't be used together");
579 return false;
580 }
581
582 return true;
583 }
584
585 /**
586 * @migration_caps_check - check capability compatibility
587 *
588 * @old_caps: old capability list
589 * @new_caps: new capability list
590 * @errp: set *errp if the check failed, with reason
591 *
592 * Returns true if check passed, otherwise false.
593 */
594 bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
595 {
596 ERRP_GUARD();
597 MigrationIncomingState *mis = migration_incoming_get_current();
598
599 #ifdef CONFIG_REPLICATION
600 if (new_caps[MIGRATION_CAPABILITY_X_COLO]) {
601 if (!new_caps[MIGRATION_CAPABILITY_RETURN_PATH]) {
602 error_setg(errp, "Capability 'x-colo' requires capability "
603 "'return-path'");
604 return false;
605 }
606 }
607 #else
608 if (new_caps[MIGRATION_CAPABILITY_X_COLO]) {
609 error_setg(errp, "QEMU compiled without replication module"
610 " can't enable COLO");
611 error_append_hint(errp, "Please enable replication before COLO.\n");
612 return false;
613 }
614 #endif
615
616 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
617 /* This check is reasonably expensive, so only when it's being
618 * set the first time, also it's only the destination that needs
619 * special support.
620 */
621 if (!old_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM] &&
622 runstate_check(RUN_STATE_INMIGRATE) &&
623 !postcopy_ram_supported_by_host(mis, errp)) {
624 error_prepend(errp, "Postcopy is not supported: ");
625 return false;
626 }
627
628 if (new_caps[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
629 error_setg(errp, "Postcopy is not compatible with ignore-shared");
630 return false;
631 }
632 }
633
634 if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
635 WriteTrackingSupport wt_support;
636 int idx;
637 /*
638 * Check if 'background-snapshot' capability is supported by
639 * host kernel and compatible with guest memory configuration.
640 */
641 wt_support = migrate_query_write_tracking();
642 if (wt_support < WT_SUPPORT_AVAILABLE) {
643 error_setg(errp, "Background-snapshot is not supported by host kernel");
644 return false;
645 }
646 if (wt_support < WT_SUPPORT_COMPATIBLE) {
647 error_setg(errp, "Background-snapshot is not compatible "
648 "with guest memory configuration");
649 return false;
650 }
651
652 /*
653 * Check if there are any migration capabilities
654 * incompatible with 'background-snapshot'.
655 */
656 for (idx = 0; idx < check_caps_background_snapshot.size; idx++) {
657 int incomp_cap = check_caps_background_snapshot.caps[idx];
658 if (new_caps[incomp_cap]) {
659 error_setg(errp,
660 "Background-snapshot is not compatible with %s",
661 MigrationCapability_str(incomp_cap));
662 return false;
663 }
664 }
665 }
666
667 #ifdef CONFIG_LINUX
668 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND] &&
669 (!new_caps[MIGRATION_CAPABILITY_MULTIFD] ||
670 new_caps[MIGRATION_CAPABILITY_XBZRLE] ||
671 migrate_multifd_compression() ||
672 migrate_tls())) {
673 error_setg(errp,
674 "Zero copy only available for non-compressed non-TLS multifd migration");
675 return false;
676 }
677 #else
678 if (new_caps[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
679 error_setg(errp,
680 "Zero copy currently only available on Linux");
681 return false;
682 }
683 #endif
684
685 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
686 if (!new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
687 error_setg(errp, "Postcopy preempt requires postcopy-ram");
688 return false;
689 }
690
691 if (!migrate_postcopy_preempt() && migrate_incoming_started()) {
692 error_setg(errp,
693 "Postcopy preempt must be set before incoming starts");
694 return false;
695 }
696 }
697
698 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
699 if (!migrate_multifd() && migrate_incoming_started()) {
700 error_setg(errp, "Multifd must be set before incoming starts");
701 return false;
702 }
703 }
704
705 if (new_caps[MIGRATION_CAPABILITY_SWITCHOVER_ACK]) {
706 if (!new_caps[MIGRATION_CAPABILITY_RETURN_PATH]) {
707 error_setg(errp, "Capability 'switchover-ack' requires capability "
708 "'return-path'");
709 return false;
710 }
711 }
712 if (new_caps[MIGRATION_CAPABILITY_DIRTY_LIMIT]) {
713 if (new_caps[MIGRATION_CAPABILITY_AUTO_CONVERGE]) {
714 error_setg(errp, "dirty-limit conflicts with auto-converge"
715 " either of then available currently");
716 return false;
717 }
718
719 if (!kvm_enabled() || !kvm_dirty_ring_enabled()) {
720 error_setg(errp, "dirty-limit requires KVM with accelerator"
721 " property 'dirty-ring-size' set");
722 return false;
723 }
724 }
725
726 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
727 if (new_caps[MIGRATION_CAPABILITY_XBZRLE]) {
728 error_setg(errp, "Multifd is not compatible with xbzrle");
729 return false;
730 }
731 }
732
733 if (new_caps[MIGRATION_CAPABILITY_MAPPED_RAM]) {
734 if (new_caps[MIGRATION_CAPABILITY_XBZRLE]) {
735 error_setg(errp,
736 "Mapped-ram migration is incompatible with xbzrle");
737 return false;
738 }
739 }
740
741 if (new_caps[MIGRATION_CAPABILITY_MAPPED_RAM] &&
742 new_caps[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
743 if (new_caps[MIGRATION_CAPABILITY_MULTIFD]) {
744 error_setg(errp,
745 "Multifd is not supported with fast snapshot load");
746 return false;
747 }
748
749 if (new_caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
750 error_setg(
751 errp,
752 "Postcopy Preempt is incompatible with fast snapshot load");
753 return false;
754 }
755
756 if (!postcopy_notifier_list_empty()) {
757 error_setg(errp,
758 "vhost-user is not supported with fast snapshot load");
759 return false;
760 }
761 }
762
763 /*
764 * On destination side, check the cases that capability is being set
765 * after incoming thread has started.
766 */
767 if (migrate_rdma() && !migrate_rdma_caps_check(new_caps, errp)) {
768 return false;
769 }
770 return true;
771 }
772
773 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
774 {
775 MigrationCapabilityStatusList *head = NULL, **tail = &head;
776 MigrationCapabilityStatus *caps;
777 MigrationState *s = migrate_get_current();
778 int i;
779
780 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
781 caps = g_malloc0(sizeof(*caps));
782 caps->capability = i;
783 caps->state = s->capabilities[i];
784 QAPI_LIST_APPEND(tail, caps);
785 }
786
787 return head;
788 }
789
790 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
791 Error **errp)
792 {
793 MigrationState *s = migrate_get_current();
794 MigrationCapabilityStatusList *cap;
795 bool new_caps[MIGRATION_CAPABILITY__MAX];
796
797 if (migration_is_running() || migration_in_colo_state()) {
798 error_setg(errp, "There's a migration process in progress");
799 return;
800 }
801
802 memcpy(new_caps, s->capabilities, sizeof(new_caps));
803 for (cap = params; cap; cap = cap->next) {
804 new_caps[cap->value->capability] = cap->value->state;
805 }
806
807 if (!migrate_caps_check(s->capabilities, new_caps, errp)) {
808 return;
809 }
810
811 for (cap = params; cap; cap = cap->next) {
812 s->capabilities[cap->value->capability] = cap->value->state;
813 }
814 }
815
816 /* parameters */
817
818 const BitmapMigrationNodeAliasList *migrate_block_bitmap_mapping(void)
819 {
820 MigrationState *s = migrate_get_current();
821
822 return s->parameters.block_bitmap_mapping;
823 }
824
825 bool migrate_has_block_bitmap_mapping(void)
826 {
827 MigrationState *s = migrate_get_current();
828
829 return s->has_block_bitmap_mapping;
830 }
831
832 uint32_t migrate_checkpoint_delay(void)
833 {
834 MigrationState *s = migrate_get_current();
835
836 return s->parameters.x_checkpoint_delay;
837 }
838
839 uint8_t migrate_cpu_throttle_increment(void)
840 {
841 MigrationState *s = migrate_get_current();
842
843 return s->parameters.cpu_throttle_increment;
844 }
845
846 uint8_t migrate_cpu_throttle_initial(void)
847 {
848 MigrationState *s = migrate_get_current();
849
850 return s->parameters.cpu_throttle_initial;
851 }
852
853 bool migrate_cpu_throttle_tailslow(void)
854 {
855 MigrationState *s = migrate_get_current();
856
857 return s->parameters.cpu_throttle_tailslow;
858 }
859
860 bool migrate_direct_io(void)
861 {
862 MigrationState *s = migrate_get_current();
863
864 /*
865 * O_DIRECT is only supported with mapped-ram and multifd.
866 *
867 * mapped-ram is needed because filesystems impose restrictions on
868 * O_DIRECT IO alignment (see MAPPED_RAM_FILE_OFFSET_ALIGNMENT).
869 *
870 * multifd is needed to keep the unaligned portion of the stream
871 * isolated to the main migration thread while multifd channels
872 * process the aligned data with O_DIRECT enabled.
873 */
874 return s->parameters.direct_io &&
875 s->capabilities[MIGRATION_CAPABILITY_MAPPED_RAM] &&
876 s->capabilities[MIGRATION_CAPABILITY_MULTIFD];
877 }
878
879 uint64_t migrate_downtime_limit(void)
880 {
881 MigrationState *s = migrate_get_current();
882
883 return s->parameters.downtime_limit;
884 }
885
886 uint8_t migrate_max_cpu_throttle(void)
887 {
888 MigrationState *s = migrate_get_current();
889
890 return s->parameters.max_cpu_throttle;
891 }
892
893 uint64_t migrate_max_bandwidth(void)
894 {
895 MigrationState *s = migrate_get_current();
896
897 return s->parameters.max_bandwidth;
898 }
899
900 uint64_t migrate_avail_switchover_bandwidth(void)
901 {
902 MigrationState *s = migrate_get_current();
903
904 return s->parameters.avail_switchover_bandwidth;
905 }
906
907 uint64_t migrate_max_postcopy_bandwidth(void)
908 {
909 MigrationState *s = migrate_get_current();
910
911 return s->parameters.max_postcopy_bandwidth;
912 }
913
914 MigMode migrate_mode(void)
915 {
916 MigMode mode = cpr_get_incoming_mode();
917
918 if (mode == MIG_MODE_NONE) {
919 mode = migrate_get_current()->parameters.mode;
920 }
921
922 assert(mode >= 0 && mode < MIG_MODE__MAX);
923 return mode;
924 }
925
926 int migrate_multifd_channels(void)
927 {
928 MigrationState *s = migrate_get_current();
929
930 return s->parameters.multifd_channels;
931 }
932
933 MultiFDCompression migrate_multifd_compression(void)
934 {
935 MigrationState *s = migrate_get_current();
936
937 assert(s->parameters.multifd_compression < MULTIFD_COMPRESSION__MAX);
938 return s->parameters.multifd_compression;
939 }
940
941 int migrate_multifd_zlib_level(void)
942 {
943 MigrationState *s = migrate_get_current();
944
945 return s->parameters.multifd_zlib_level;
946 }
947
948 int migrate_multifd_qatzip_level(void)
949 {
950 MigrationState *s = migrate_get_current();
951
952 return s->parameters.multifd_qatzip_level;
953 }
954
955 int migrate_multifd_zstd_level(void)
956 {
957 MigrationState *s = migrate_get_current();
958
959 return s->parameters.multifd_zstd_level;
960 }
961
962 uint8_t migrate_throttle_trigger_threshold(void)
963 {
964 MigrationState *s = migrate_get_current();
965
966 return s->parameters.throttle_trigger_threshold;
967 }
968
969 const char *migrate_tls_authz(void)
970 {
971 MigrationState *s = migrate_get_current();
972
973 if (*s->parameters.tls_authz->u.s) {
974 return s->parameters.tls_authz->u.s;
975 }
976
977 return NULL;
978 }
979
980 const char *migrate_tls_creds(void)
981 {
982 MigrationState *s = migrate_get_current();
983
984 if (*s->parameters.tls_creds->u.s) {
985 return s->parameters.tls_creds->u.s;
986 }
987
988 return NULL;
989 }
990
991 const char *migrate_tls_hostname(void)
992 {
993 MigrationState *s = migrate_get_current();
994
995 if (*s->parameters.tls_hostname->u.s) {
996 return s->parameters.tls_hostname->u.s;
997 }
998
999 /* hostname saved from a previously connected channel */
1000 if (s->hostname) {
1001 return s->hostname;
1002 }
1003
1004 return NULL;
1005 }
1006
1007 bool migrate_tls(void)
1008 {
1009 return !!migrate_tls_creds();
1010 }
1011
1012 uint64_t migrate_vcpu_dirty_limit_period(void)
1013 {
1014 MigrationState *s = migrate_get_current();
1015
1016 return s->parameters.x_vcpu_dirty_limit_period;
1017 }
1018
1019 uint64_t migrate_xbzrle_cache_size(void)
1020 {
1021 MigrationState *s = migrate_get_current();
1022
1023 return s->parameters.xbzrle_cache_size;
1024 }
1025
1026 ZeroPageDetection migrate_zero_page_detection(void)
1027 {
1028 MigrationState *s = migrate_get_current();
1029
1030 return s->parameters.zero_page_detection;
1031 }
1032
1033 uint64_t migrate_rdma_chunk_size(void)
1034 {
1035 MigrationState *s = migrate_get_current();
1036 uint64_t size = s->parameters.x_rdma_chunk_size;
1037
1038 assert(MiB <= size && size <= GiB && is_power_of_2(size));
1039 return size;
1040 }
1041
1042 /* parameters helpers */
1043
1044 AnnounceParameters *migrate_announce_params(void)
1045 {
1046 static AnnounceParameters ap;
1047
1048 MigrationState *s = migrate_get_current();
1049
1050 ap.initial = s->parameters.announce_initial;
1051 ap.max = s->parameters.announce_max;
1052 ap.rounds = s->parameters.announce_rounds;
1053 ap.step = s->parameters.announce_step;
1054
1055 return &ap;
1056 }
1057
1058 void migrate_tls_opts_free(MigrationParameters *params)
1059 {
1060 qapi_free_StrOrNull(params->tls_creds);
1061 qapi_free_StrOrNull(params->tls_hostname);
1062 qapi_free_StrOrNull(params->tls_authz);
1063 }
1064
1065 /* normalize QTYPE_QNULL to QTYPE_QSTRING "" */
1066 static void tls_opt_to_str(StrOrNull *opt)
1067 {
1068 if (!opt || opt->type == QTYPE_QSTRING) {
1069 return;
1070 }
1071
1072 qobject_unref(opt->u.n);
1073 opt->type = QTYPE_QSTRING;
1074 opt->u.s = g_strdup("");
1075 }
1076
1077 /*
1078 * query-migrate-parameters expects all members of MigrationParameters
1079 * to be present, but we cannot mark them non-optional in QAPI because
1080 * the structure is also used for migrate-set-parameters, which needs
1081 * the optionality. Force all parameters to be seen as present
1082 * now. Note that this depends on some form of default being set for
1083 * every member of MigrationParameters, currently done during qdev
1084 * init using migration_properties defined in this file. The TLS
1085 * options are a special case because they don't have a default and
1086 * need to be normalized before use.
1087 */
1088 static void migrate_mark_all_params_present(MigrationParameters *p)
1089 {
1090 int len, n_str_args = 3; /* tls-creds, tls-hostname, tls-authz */
1091 bool *has_fields[] = {
1092 &p->has_throttle_trigger_threshold, &p->has_cpu_throttle_initial,
1093 &p->has_cpu_throttle_increment, &p->has_cpu_throttle_tailslow,
1094 &p->has_max_bandwidth, &p->has_avail_switchover_bandwidth,
1095 &p->has_downtime_limit, &p->has_x_checkpoint_delay,
1096 &p->has_multifd_channels, &p->has_multifd_compression,
1097 &p->has_multifd_zlib_level, &p->has_multifd_qatzip_level,
1098 &p->has_multifd_zstd_level, &p->has_xbzrle_cache_size,
1099 &p->has_max_postcopy_bandwidth, &p->has_max_cpu_throttle,
1100 &p->has_announce_initial, &p->has_announce_max, &p->has_announce_rounds,
1101 &p->has_announce_step, &p->has_block_bitmap_mapping,
1102 &p->has_x_vcpu_dirty_limit_period, &p->has_vcpu_dirty_limit,
1103 &p->has_mode, &p->has_zero_page_detection, &p->has_direct_io,
1104 &p->has_x_rdma_chunk_size, &p->has_cpr_exec_command,
1105 };
1106
1107 len = ARRAY_SIZE(has_fields);
1108 assert(len + n_str_args == MIGRATION_PARAMETER__MAX);
1109
1110 for (int i = 0; i < len; i++) {
1111 *has_fields[i] = true;
1112 }
1113 }
1114
1115 MigrationParameters *qmp_query_migrate_parameters(Error **errp)
1116 {
1117 MigrationState *s = migrate_get_current();
1118 MigrationParameters *params = QAPI_CLONE(MigrationParameters,
1119 &s->parameters);
1120
1121 /*
1122 * The block-bitmap-mapping breaks the expected API of
1123 * query-migrate-parameters of having all members present. To keep
1124 * compatibility, only emit this field if it's actually been
1125 * set. The empty list is a valid value.
1126 */
1127 if (!s->has_block_bitmap_mapping) {
1128 params->has_block_bitmap_mapping = false;
1129 qapi_free_BitmapMigrationNodeAliasList(params->block_bitmap_mapping);
1130 }
1131
1132 return params;
1133 }
1134
1135 void migrate_params_init(MigrationParameters *params)
1136 {
1137 migrate_mark_all_params_present(params);
1138 }
1139
1140 static void migrate_post_update_params(MigrationParameters *new, Error **errp)
1141 {
1142 MigrationState *s = migrate_get_current();
1143
1144 if (new->has_max_bandwidth) {
1145 if (s->to_dst_file && !migration_in_postcopy()) {
1146 migration_rate_set(new->max_bandwidth);
1147 }
1148 }
1149
1150 if (new->has_x_checkpoint_delay) {
1151 colo_checkpoint_delay_set();
1152 }
1153
1154 if (new->has_xbzrle_cache_size) {
1155 xbzrle_cache_resize(new->xbzrle_cache_size, errp);
1156 }
1157
1158 if (new->has_max_postcopy_bandwidth) {
1159 if (s->to_dst_file && migration_in_postcopy()) {
1160 migration_rate_set(new->max_postcopy_bandwidth);
1161 }
1162 }
1163 }
1164
1165 /*
1166 * Check whether the parameters are valid. Error will be put into errp
1167 * (if provided). Return true if valid, otherwise false.
1168 */
1169 bool migrate_params_check(MigrationParameters *params, Error **errp)
1170 {
1171 ERRP_GUARD();
1172
1173 if (params->throttle_trigger_threshold < 1 ||
1174 params->throttle_trigger_threshold > 100) {
1175 error_setg(errp, "Option throttle-trigger-threshold expects "
1176 "an integer in the range of 1 to 100");
1177 return false;
1178 }
1179
1180 if (params->cpu_throttle_initial < 1 ||
1181 params->cpu_throttle_initial > 99) {
1182 error_setg(errp, "Option cpu-throttle-initial expects "
1183 "an integer in the range of 1 to 99");
1184 return false;
1185 }
1186
1187 if (params->cpu_throttle_increment < 1 ||
1188 params->cpu_throttle_increment > 99) {
1189 error_setg(errp, "Option cpu-throttle-increment expects "
1190 "an integer in the range of 1 to 99");
1191 return false;
1192 }
1193
1194 if (params->max_bandwidth > SIZE_MAX) {
1195 error_setg(errp, "Option max-bandwidth expects "
1196 "an integer in the range of 0 to "stringify(SIZE_MAX)
1197 " bytes/second");
1198 return false;
1199 }
1200
1201 if (params->avail_switchover_bandwidth > SIZE_MAX) {
1202 error_setg(errp, "Option avail-switchover-bandwidth expects "
1203 "an integer in the range of 0 to "stringify(SIZE_MAX)
1204 " bytes/second");
1205 return false;
1206 }
1207
1208 if (params->downtime_limit > MAX_MIGRATE_DOWNTIME) {
1209 error_setg(errp, "Option downtime-limit expects "
1210 "an integer in the range of 0 to "
1211 stringify(MAX_MIGRATE_DOWNTIME)" ms");
1212 return false;
1213 }
1214
1215 if (params->multifd_channels < 1) {
1216 error_setg(errp, "Option multifd-channels expects "
1217 "a value between 1 and 255");
1218 return false;
1219 }
1220
1221 if (params->multifd_zlib_level > 9) {
1222 error_setg(errp, "Option multifd-zlib-level expects "
1223 "a value between 0 and 9");
1224 return false;
1225 }
1226
1227 if (params->multifd_qatzip_level > 9 ||
1228 params->multifd_qatzip_level < 1) {
1229 error_setg(errp, "Option multifd-qatzip-level expects "
1230 "a value between 1 and 9");
1231 return false;
1232 }
1233
1234 if (params->multifd_zstd_level > 20) {
1235 error_setg(errp, "Option multifd-zstd-level expects "
1236 "a value between 0 and 20");
1237 return false;
1238 }
1239
1240 if (params->xbzrle_cache_size < qemu_target_page_size() ||
1241 !is_power_of_2(params->xbzrle_cache_size)) {
1242 error_setg(errp, "Option xbzrle-cache-size expects "
1243 "a power of two no less than the target page size");
1244 return false;
1245 }
1246
1247 if (params->max_cpu_throttle < params->cpu_throttle_initial ||
1248 params->max_cpu_throttle > 99) {
1249 error_setg(errp, "Option max-cpu-throttle expects "
1250 "an integer in the range of cpu-throttle-initial to 99");
1251 return false;
1252 }
1253
1254 if (params->announce_initial > 100000) {
1255 error_setg(errp, "Option announce-initial expects "
1256 "a value between 0 and 100000");
1257 return false;
1258 }
1259 if (params->announce_max > 100000) {
1260 error_setg(errp, "Option announce-max expects "
1261 "a value between 0 and 100000");
1262 return false;
1263 }
1264 if (params->announce_rounds > 1000) {
1265 error_setg(errp, "Option announce-rounds expects "
1266 "a value between 0 and 1000");
1267 return false;
1268 }
1269 if (params->announce_step < 1 ||
1270 params->announce_step > 10000) {
1271 error_setg(errp, "Option announce-step expects "
1272 "a value between 0 and 10000");
1273 return false;
1274 }
1275
1276 if (!check_dirty_bitmap_mig_alias_map(params->block_bitmap_mapping, errp)) {
1277 error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: ");
1278 return false;
1279 }
1280
1281 #ifdef CONFIG_LINUX
1282 if (migrate_zero_copy_send() &&
1283 (params->multifd_compression || *params->tls_creds->u.s)) {
1284 error_setg(errp,
1285 "Zero copy only available for non-compressed non-TLS multifd migration");
1286 return false;
1287 }
1288 #endif
1289
1290 if (migrate_mapped_ram() &&
1291 (migrate_multifd_compression() || migrate_tls())) {
1292 error_setg(errp,
1293 "Mapped-ram only available for non-compressed non-TLS multifd migration");
1294 return false;
1295 }
1296
1297 if (params->x_vcpu_dirty_limit_period < 1 ||
1298 params->x_vcpu_dirty_limit_period > 1000) {
1299 error_setg(errp, "Option x-vcpu-dirty-limit-period expects "
1300 "a value between 1 and 1000");
1301 return false;
1302 }
1303
1304 if (params->vcpu_dirty_limit < 1) {
1305 error_setg(errp,
1306 "Parameter 'vcpu-dirty-limit' must be greater than 1 MB/s");
1307 return false;
1308 }
1309
1310 if (params->direct_io && !qemu_has_direct_io()) {
1311 error_setg(errp, "No build-time support for direct-io");
1312 return false;
1313 }
1314
1315 if (params->has_x_rdma_chunk_size &&
1316 (params->x_rdma_chunk_size < MiB ||
1317 params->x_rdma_chunk_size > GiB ||
1318 !is_power_of_2(params->x_rdma_chunk_size))) {
1319 error_setg(errp, "Option x_rdma_chunk_size expects "
1320 "a power of 2 in the range 1MiB to 1024MiB");
1321 return false;
1322 }
1323
1324 return true;
1325 }
1326
1327 static void migrate_params_test_apply(MigrationParameters *params,
1328 MigrationParameters *dest)
1329 {
1330 MigrationState *s = migrate_get_current();
1331
1332 QAPI_CLONE_MEMBERS(MigrationParameters, dest, &s->parameters);
1333
1334 if (params->has_throttle_trigger_threshold) {
1335 dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
1336 }
1337
1338 if (params->has_cpu_throttle_initial) {
1339 dest->cpu_throttle_initial = params->cpu_throttle_initial;
1340 }
1341
1342 if (params->has_cpu_throttle_increment) {
1343 dest->cpu_throttle_increment = params->cpu_throttle_increment;
1344 }
1345
1346 if (params->has_cpu_throttle_tailslow) {
1347 dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1348 }
1349
1350 if (params->tls_creds) {
1351 qapi_free_StrOrNull(dest->tls_creds);
1352 dest->tls_creds = QAPI_CLONE(StrOrNull, params->tls_creds);
1353 }
1354
1355 if (params->tls_hostname) {
1356 qapi_free_StrOrNull(dest->tls_hostname);
1357 dest->tls_hostname = QAPI_CLONE(StrOrNull, params->tls_hostname);
1358 }
1359
1360 if (params->tls_authz) {
1361 qapi_free_StrOrNull(dest->tls_authz);
1362 dest->tls_authz = QAPI_CLONE(StrOrNull, params->tls_authz);
1363 }
1364
1365 if (params->has_max_bandwidth) {
1366 dest->max_bandwidth = params->max_bandwidth;
1367 }
1368
1369 if (params->has_avail_switchover_bandwidth) {
1370 dest->avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1371 }
1372
1373 if (params->has_downtime_limit) {
1374 dest->downtime_limit = params->downtime_limit;
1375 }
1376
1377 if (params->has_x_checkpoint_delay) {
1378 dest->x_checkpoint_delay = params->x_checkpoint_delay;
1379 }
1380
1381 if (params->has_multifd_channels) {
1382 dest->multifd_channels = params->multifd_channels;
1383 }
1384 if (params->has_multifd_compression) {
1385 dest->multifd_compression = params->multifd_compression;
1386 }
1387 if (params->has_multifd_qatzip_level) {
1388 dest->multifd_qatzip_level = params->multifd_qatzip_level;
1389 }
1390 if (params->has_multifd_zlib_level) {
1391 dest->multifd_zlib_level = params->multifd_zlib_level;
1392 }
1393 if (params->has_multifd_zstd_level) {
1394 dest->multifd_zstd_level = params->multifd_zstd_level;
1395 }
1396 if (params->has_xbzrle_cache_size) {
1397 dest->xbzrle_cache_size = params->xbzrle_cache_size;
1398 }
1399 if (params->has_max_postcopy_bandwidth) {
1400 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1401 }
1402 if (params->has_max_cpu_throttle) {
1403 dest->max_cpu_throttle = params->max_cpu_throttle;
1404 }
1405 if (params->has_announce_initial) {
1406 dest->announce_initial = params->announce_initial;
1407 }
1408 if (params->has_announce_max) {
1409 dest->announce_max = params->announce_max;
1410 }
1411 if (params->has_announce_rounds) {
1412 dest->announce_rounds = params->announce_rounds;
1413 }
1414 if (params->has_announce_step) {
1415 dest->announce_step = params->announce_step;
1416 }
1417
1418 if (params->has_block_bitmap_mapping) {
1419 qapi_free_BitmapMigrationNodeAliasList(dest->block_bitmap_mapping);
1420 dest->block_bitmap_mapping = QAPI_CLONE(BitmapMigrationNodeAliasList,
1421 params->block_bitmap_mapping);
1422 }
1423
1424 if (params->has_x_vcpu_dirty_limit_period) {
1425 dest->x_vcpu_dirty_limit_period =
1426 params->x_vcpu_dirty_limit_period;
1427 }
1428 if (params->has_vcpu_dirty_limit) {
1429 dest->vcpu_dirty_limit = params->vcpu_dirty_limit;
1430 }
1431
1432 if (params->has_mode) {
1433 dest->mode = params->mode;
1434 }
1435
1436 if (params->has_zero_page_detection) {
1437 dest->zero_page_detection = params->zero_page_detection;
1438 }
1439
1440 if (params->has_direct_io) {
1441 dest->direct_io = params->direct_io;
1442 }
1443
1444 if (params->has_x_rdma_chunk_size) {
1445 dest->x_rdma_chunk_size = params->x_rdma_chunk_size;
1446 }
1447
1448 if (params->has_cpr_exec_command) {
1449 qapi_free_strList(dest->cpr_exec_command);
1450 dest->cpr_exec_command = QAPI_CLONE(strList, params->cpr_exec_command);
1451 }
1452 }
1453
1454 static void migrate_params_apply(MigrationParameters *params)
1455 {
1456 MigrationState *s = migrate_get_current();
1457
1458 /* TODO use QAPI_CLONE() instead of duplicating it inline */
1459
1460 if (params->has_throttle_trigger_threshold) {
1461 s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
1462 }
1463
1464 if (params->has_cpu_throttle_initial) {
1465 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
1466 }
1467
1468 if (params->has_cpu_throttle_increment) {
1469 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
1470 }
1471
1472 if (params->has_cpu_throttle_tailslow) {
1473 s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
1474 }
1475
1476 if (params->tls_creds) {
1477 qapi_free_StrOrNull(s->parameters.tls_creds);
1478 s->parameters.tls_creds = QAPI_CLONE(StrOrNull, params->tls_creds);
1479 }
1480
1481 if (params->tls_hostname) {
1482 qapi_free_StrOrNull(s->parameters.tls_hostname);
1483 s->parameters.tls_hostname = QAPI_CLONE(StrOrNull,
1484 params->tls_hostname);
1485 }
1486
1487 if (params->tls_authz) {
1488 qapi_free_StrOrNull(s->parameters.tls_authz);
1489 s->parameters.tls_authz = QAPI_CLONE(StrOrNull, params->tls_authz);
1490 }
1491
1492 if (params->has_max_bandwidth) {
1493 s->parameters.max_bandwidth = params->max_bandwidth;
1494 }
1495
1496 if (params->has_avail_switchover_bandwidth) {
1497 s->parameters.avail_switchover_bandwidth = params->avail_switchover_bandwidth;
1498 }
1499
1500 if (params->has_downtime_limit) {
1501 s->parameters.downtime_limit = params->downtime_limit;
1502 }
1503
1504 if (params->has_x_checkpoint_delay) {
1505 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
1506 }
1507
1508 if (params->has_multifd_channels) {
1509 s->parameters.multifd_channels = params->multifd_channels;
1510 }
1511 if (params->has_multifd_compression) {
1512 s->parameters.multifd_compression = params->multifd_compression;
1513 }
1514 if (params->has_multifd_qatzip_level) {
1515 s->parameters.multifd_qatzip_level = params->multifd_qatzip_level;
1516 }
1517 if (params->has_multifd_zlib_level) {
1518 s->parameters.multifd_zlib_level = params->multifd_zlib_level;
1519 }
1520 if (params->has_multifd_zstd_level) {
1521 s->parameters.multifd_zstd_level = params->multifd_zstd_level;
1522 }
1523 if (params->has_xbzrle_cache_size) {
1524 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
1525 }
1526 if (params->has_max_postcopy_bandwidth) {
1527 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
1528 }
1529 if (params->has_max_cpu_throttle) {
1530 s->parameters.max_cpu_throttle = params->max_cpu_throttle;
1531 }
1532 if (params->has_announce_initial) {
1533 s->parameters.announce_initial = params->announce_initial;
1534 }
1535 if (params->has_announce_max) {
1536 s->parameters.announce_max = params->announce_max;
1537 }
1538 if (params->has_announce_rounds) {
1539 s->parameters.announce_rounds = params->announce_rounds;
1540 }
1541 if (params->has_announce_step) {
1542 s->parameters.announce_step = params->announce_step;
1543 }
1544
1545 if (params->has_block_bitmap_mapping) {
1546 qapi_free_BitmapMigrationNodeAliasList(
1547 s->parameters.block_bitmap_mapping);
1548
1549 s->has_block_bitmap_mapping = true;
1550 s->parameters.block_bitmap_mapping =
1551 QAPI_CLONE(BitmapMigrationNodeAliasList,
1552 params->block_bitmap_mapping);
1553 }
1554
1555 if (params->has_x_vcpu_dirty_limit_period) {
1556 s->parameters.x_vcpu_dirty_limit_period =
1557 params->x_vcpu_dirty_limit_period;
1558 }
1559 if (params->has_vcpu_dirty_limit) {
1560 s->parameters.vcpu_dirty_limit = params->vcpu_dirty_limit;
1561 }
1562
1563 if (params->has_mode) {
1564 s->parameters.mode = params->mode;
1565 }
1566
1567 if (params->has_zero_page_detection) {
1568 s->parameters.zero_page_detection = params->zero_page_detection;
1569 }
1570
1571 if (params->has_direct_io) {
1572 s->parameters.direct_io = params->direct_io;
1573 }
1574
1575 if (params->has_x_rdma_chunk_size) {
1576 s->parameters.x_rdma_chunk_size = params->x_rdma_chunk_size;
1577 }
1578
1579 if (params->has_cpr_exec_command) {
1580 qapi_free_strList(s->parameters.cpr_exec_command);
1581 s->parameters.cpr_exec_command =
1582 QAPI_CLONE(strList, params->cpr_exec_command);
1583 }
1584 }
1585
1586 void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
1587 {
1588 MigrationParameters tmp;
1589
1590 /*
1591 * Convert QTYPE_QNULL and NULL to the empty string (""). Even
1592 * though NULL is cleaner to deal with in C code, that would force
1593 * query-migrate-parameters to convert it once more to the empty
1594 * string, so avoid that. The migrate_tls_*() helpers that expose
1595 * the options to the rest of the migration code already use
1596 * return NULL when the empty string is found.
1597 */
1598 tls_opt_to_str(params->tls_creds);
1599 tls_opt_to_str(params->tls_hostname);
1600 tls_opt_to_str(params->tls_authz);
1601
1602 migrate_params_test_apply(params, &tmp);
1603
1604 if (migrate_params_check(&tmp, errp)) {
1605 migrate_params_apply(params);
1606 migrate_post_update_params(params, errp);
1607 }
1608
1609 migrate_tls_opts_free(&tmp);
1610 qapi_free_BitmapMigrationNodeAliasList(tmp.block_bitmap_mapping);
1611 qapi_free_strList(tmp.cpr_exec_command);
1612 }