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