1 /*
2  * HMP commands related to migration
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  * Contributions after 2012-01-13 are licensed under the terms of the
13  * GNU GPL, version 2 or (at your option) any later version.
14  */
15 
16 #include "qemu/osdep.h"
17 #include "block/qapi.h"
18 #include "migration/snapshot.h"
19 #include "monitor/hmp.h"
20 #include "monitor/monitor.h"
21 #include "qapi/error.h"
22 #include "qapi/qapi-commands-migration.h"
23 #include "qapi/qapi-visit-migration.h"
24 #include "qapi/qmp/qdict.h"
25 #include "qapi/string-input-visitor.h"
26 #include "qapi/string-output-visitor.h"
27 #include "qemu/cutils.h"
28 #include "qemu/error-report.h"
29 #include "qemu/sockets.h"
30 #include "sysemu/runstate.h"
31 #include "ui/qemu-spice.h"
32 #include "sysemu/sysemu.h"
33 #include "migration.h"
34 
35 static void migration_global_dump(Monitor *mon)
36 {
37     MigrationState *ms = migrate_get_current();
38 
39     monitor_printf(mon, "globals:\n");
40     monitor_printf(mon, "store-global-state: %s\n",
41                    ms->store_global_state ? "on" : "off");
42     monitor_printf(mon, "only-migratable: %s\n",
43                    only_migratable ? "on" : "off");
44     monitor_printf(mon, "send-configuration: %s\n",
45                    ms->send_configuration ? "on" : "off");
46     monitor_printf(mon, "send-section-footer: %s\n",
47                    ms->send_section_footer ? "on" : "off");
48     monitor_printf(mon, "decompress-error-check: %s\n",
49                    ms->decompress_error_check ? "on" : "off");
50     monitor_printf(mon, "clear-bitmap-shift: %u\n",
51                    ms->clear_bitmap_shift);
52 }
53 
54 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
55 {
56     MigrationInfo *info;
57 
58     info = qmp_query_migrate(NULL);
59 
60     migration_global_dump(mon);
61 
62     if (info->blocked_reasons) {
63         strList *reasons = info->blocked_reasons;
64         monitor_printf(mon, "Outgoing migration blocked:\n");
65         while (reasons) {
66             monitor_printf(mon, "  %s\n", reasons->value);
67             reasons = reasons->next;
68         }
69     }
70 
71     if (info->has_status) {
72         monitor_printf(mon, "Migration status: %s",
73                        MigrationStatus_str(info->status));
74         if (info->status == MIGRATION_STATUS_FAILED && info->error_desc) {
75             monitor_printf(mon, " (%s)\n", info->error_desc);
76         } else {
77             monitor_printf(mon, "\n");
78         }
79 
80         monitor_printf(mon, "total time: %" PRIu64 " ms\n",
81                        info->total_time);
82         if (info->has_expected_downtime) {
83             monitor_printf(mon, "expected downtime: %" PRIu64 " ms\n",
84                            info->expected_downtime);
85         }
86         if (info->has_downtime) {
87             monitor_printf(mon, "downtime: %" PRIu64 " ms\n",
88                            info->downtime);
89         }
90         if (info->has_setup_time) {
91             monitor_printf(mon, "setup: %" PRIu64 " ms\n",
92                            info->setup_time);
93         }
94     }
95 
96     if (info->ram) {
97         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
98                        info->ram->transferred >> 10);
99         monitor_printf(mon, "throughput: %0.2f mbps\n",
100                        info->ram->mbps);
101         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
102                        info->ram->remaining >> 10);
103         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
104                        info->ram->total >> 10);
105         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
106                        info->ram->duplicate);
107         monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
108                        info->ram->skipped);
109         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
110                        info->ram->normal);
111         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
112                        info->ram->normal_bytes >> 10);
113         monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
114                        info->ram->dirty_sync_count);
115         monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
116                        info->ram->page_size >> 10);
117         monitor_printf(mon, "multifd bytes: %" PRIu64 " kbytes\n",
118                        info->ram->multifd_bytes >> 10);
119         monitor_printf(mon, "pages-per-second: %" PRIu64 "\n",
120                        info->ram->pages_per_second);
121 
122         if (info->ram->dirty_pages_rate) {
123             monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
124                            info->ram->dirty_pages_rate);
125         }
126         if (info->ram->postcopy_requests) {
127             monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
128                            info->ram->postcopy_requests);
129         }
130         if (info->ram->precopy_bytes) {
131             monitor_printf(mon, "precopy ram: %" PRIu64 " kbytes\n",
132                            info->ram->precopy_bytes >> 10);
133         }
134         if (info->ram->downtime_bytes) {
135             monitor_printf(mon, "downtime ram: %" PRIu64 " kbytes\n",
136                            info->ram->downtime_bytes >> 10);
137         }
138         if (info->ram->postcopy_bytes) {
139             monitor_printf(mon, "postcopy ram: %" PRIu64 " kbytes\n",
140                            info->ram->postcopy_bytes >> 10);
141         }
142         if (info->ram->dirty_sync_missed_zero_copy) {
143             monitor_printf(mon,
144                            "Zero-copy-send fallbacks happened: %" PRIu64 " times\n",
145                            info->ram->dirty_sync_missed_zero_copy);
146         }
147     }
148 
149     if (info->disk) {
150         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
151                        info->disk->transferred >> 10);
152         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
153                        info->disk->remaining >> 10);
154         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
155                        info->disk->total >> 10);
156     }
157 
158     if (info->xbzrle_cache) {
159         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
160                        info->xbzrle_cache->cache_size);
161         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
162                        info->xbzrle_cache->bytes >> 10);
163         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
164                        info->xbzrle_cache->pages);
165         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 " pages\n",
166                        info->xbzrle_cache->cache_miss);
167         monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
168                        info->xbzrle_cache->cache_miss_rate);
169         monitor_printf(mon, "xbzrle encoding rate: %0.2f\n",
170                        info->xbzrle_cache->encoding_rate);
171         monitor_printf(mon, "xbzrle overflow: %" PRIu64 "\n",
172                        info->xbzrle_cache->overflow);
173     }
174 
175     if (info->compression) {
176         monitor_printf(mon, "compression pages: %" PRIu64 " pages\n",
177                        info->compression->pages);
178         monitor_printf(mon, "compression busy: %" PRIu64 "\n",
179                        info->compression->busy);
180         monitor_printf(mon, "compression busy rate: %0.2f\n",
181                        info->compression->busy_rate);
182         monitor_printf(mon, "compressed size: %" PRIu64 " kbytes\n",
183                        info->compression->compressed_size >> 10);
184         monitor_printf(mon, "compression rate: %0.2f\n",
185                        info->compression->compression_rate);
186     }
187 
188     if (info->has_cpu_throttle_percentage) {
189         monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
190                        info->cpu_throttle_percentage);
191     }
192 
193     if (info->has_postcopy_blocktime) {
194         monitor_printf(mon, "postcopy blocktime: %u\n",
195                        info->postcopy_blocktime);
196     }
197 
198     if (info->has_postcopy_vcpu_blocktime) {
199         Visitor *v;
200         char *str;
201         v = string_output_visitor_new(false, &str);
202         visit_type_uint32List(v, NULL, &info->postcopy_vcpu_blocktime,
203                               &error_abort);
204         visit_complete(v, &str);
205         monitor_printf(mon, "postcopy vcpu blocktime: %s\n", str);
206         g_free(str);
207         visit_free(v);
208     }
209     if (info->has_socket_address) {
210         SocketAddressList *addr;
211 
212         monitor_printf(mon, "socket address: [\n");
213 
214         for (addr = info->socket_address; addr; addr = addr->next) {
215             char *s = socket_uri(addr->value);
216             monitor_printf(mon, "\t%s\n", s);
217             g_free(s);
218         }
219         monitor_printf(mon, "]\n");
220     }
221 
222     if (info->vfio) {
223         monitor_printf(mon, "vfio device transferred: %" PRIu64 " kbytes\n",
224                        info->vfio->transferred >> 10);
225     }
226 
227     qapi_free_MigrationInfo(info);
228 }
229 
230 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
231 {
232     MigrationCapabilityStatusList *caps, *cap;
233 
234     caps = qmp_query_migrate_capabilities(NULL);
235 
236     if (caps) {
237         for (cap = caps; cap; cap = cap->next) {
238             monitor_printf(mon, "%s: %s\n",
239                            MigrationCapability_str(cap->value->capability),
240                            cap->value->state ? "on" : "off");
241         }
242     }
243 
244     qapi_free_MigrationCapabilityStatusList(caps);
245 }
246 
247 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
248 {
249     MigrationParameters *params;
250 
251     params = qmp_query_migrate_parameters(NULL);
252 
253     if (params) {
254         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
255             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_INITIAL),
256             params->announce_initial);
257         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
258             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_MAX),
259             params->announce_max);
260         monitor_printf(mon, "%s: %" PRIu64 "\n",
261             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_ROUNDS),
262             params->announce_rounds);
263         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
264             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
265             params->announce_step);
266         assert(params->has_compress_level);
267         monitor_printf(mon, "%s: %u\n",
268             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
269             params->compress_level);
270         assert(params->has_compress_threads);
271         monitor_printf(mon, "%s: %u\n",
272             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
273             params->compress_threads);
274         assert(params->has_compress_wait_thread);
275         monitor_printf(mon, "%s: %s\n",
276             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD),
277             params->compress_wait_thread ? "on" : "off");
278         assert(params->has_decompress_threads);
279         monitor_printf(mon, "%s: %u\n",
280             MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
281             params->decompress_threads);
282         assert(params->has_throttle_trigger_threshold);
283         monitor_printf(mon, "%s: %u\n",
284             MigrationParameter_str(MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD),
285             params->throttle_trigger_threshold);
286         assert(params->has_cpu_throttle_initial);
287         monitor_printf(mon, "%s: %u\n",
288             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
289             params->cpu_throttle_initial);
290         assert(params->has_cpu_throttle_increment);
291         monitor_printf(mon, "%s: %u\n",
292             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
293             params->cpu_throttle_increment);
294         assert(params->has_cpu_throttle_tailslow);
295         monitor_printf(mon, "%s: %s\n",
296             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW),
297             params->cpu_throttle_tailslow ? "on" : "off");
298         assert(params->has_max_cpu_throttle);
299         monitor_printf(mon, "%s: %u\n",
300             MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
301             params->max_cpu_throttle);
302         assert(params->tls_creds);
303         monitor_printf(mon, "%s: '%s'\n",
304             MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
305             params->tls_creds);
306         assert(params->tls_hostname);
307         monitor_printf(mon, "%s: '%s'\n",
308             MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
309             params->tls_hostname);
310         assert(params->has_max_bandwidth);
311         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
312             MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
313             params->max_bandwidth);
314         assert(params->has_downtime_limit);
315         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
316             MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
317             params->downtime_limit);
318         assert(params->has_x_checkpoint_delay);
319         monitor_printf(mon, "%s: %u ms\n",
320             MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
321             params->x_checkpoint_delay);
322         assert(params->has_block_incremental);
323         monitor_printf(mon, "%s: %s\n",
324             MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
325             params->block_incremental ? "on" : "off");
326         monitor_printf(mon, "%s: %u\n",
327             MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_CHANNELS),
328             params->multifd_channels);
329         monitor_printf(mon, "%s: %s\n",
330             MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_COMPRESSION),
331             MultiFDCompression_str(params->multifd_compression));
332         monitor_printf(mon, "%s: %" PRIu64 " bytes\n",
333             MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
334             params->xbzrle_cache_size);
335         monitor_printf(mon, "%s: %" PRIu64 "\n",
336             MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
337             params->max_postcopy_bandwidth);
338         monitor_printf(mon, "%s: '%s'\n",
339             MigrationParameter_str(MIGRATION_PARAMETER_TLS_AUTHZ),
340             params->tls_authz);
341 
342         if (params->has_block_bitmap_mapping) {
343             const BitmapMigrationNodeAliasList *bmnal;
344 
345             monitor_printf(mon, "%s:\n",
346                            MigrationParameter_str(
347                                MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING));
348 
349             for (bmnal = params->block_bitmap_mapping;
350                  bmnal;
351                  bmnal = bmnal->next)
352             {
353                 const BitmapMigrationNodeAlias *bmna = bmnal->value;
354                 const BitmapMigrationBitmapAliasList *bmbal;
355 
356                 monitor_printf(mon, "  '%s' -> '%s'\n",
357                                bmna->node_name, bmna->alias);
358 
359                 for (bmbal = bmna->bitmaps; bmbal; bmbal = bmbal->next) {
360                     const BitmapMigrationBitmapAlias *bmba = bmbal->value;
361 
362                     monitor_printf(mon, "    '%s' -> '%s'\n",
363                                    bmba->name, bmba->alias);
364                 }
365             }
366         }
367 
368         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
369         MigrationParameter_str(MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD),
370         params->x_vcpu_dirty_limit_period);
371     }
372 
373     qapi_free_MigrationParameters(params);
374 }
375 
376 void hmp_loadvm(Monitor *mon, const QDict *qdict)
377 {
378     int saved_vm_running  = runstate_is_running();
379     const char *name = qdict_get_str(qdict, "name");
380     Error *err = NULL;
381 
382     vm_stop(RUN_STATE_RESTORE_VM);
383 
384     if (load_snapshot(name, NULL, false, NULL, &err) && saved_vm_running) {
385         vm_start();
386     }
387     hmp_handle_error(mon, err);
388 }
389 
390 void hmp_savevm(Monitor *mon, const QDict *qdict)
391 {
392     Error *err = NULL;
393 
394     save_snapshot(qdict_get_try_str(qdict, "name"),
395                   true, NULL, false, NULL, &err);
396     hmp_handle_error(mon, err);
397 }
398 
399 void hmp_delvm(Monitor *mon, const QDict *qdict)
400 {
401     Error *err = NULL;
402     const char *name = qdict_get_str(qdict, "name");
403 
404     delete_snapshot(name, false, NULL, &err);
405     hmp_handle_error(mon, err);
406 }
407 
408 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
409 {
410     qmp_migrate_cancel(NULL);
411 }
412 
413 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
414 {
415     Error *err = NULL;
416     const char *state = qdict_get_str(qdict, "state");
417     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
418 
419     if (val >= 0) {
420         qmp_migrate_continue(val, &err);
421     }
422 
423     hmp_handle_error(mon, err);
424 }
425 
426 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
427 {
428     Error *err = NULL;
429     const char *uri = qdict_get_str(qdict, "uri");
430 
431     qmp_migrate_incoming(uri, &err);
432 
433     hmp_handle_error(mon, err);
434 }
435 
436 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
437 {
438     Error *err = NULL;
439     const char *uri = qdict_get_str(qdict, "uri");
440 
441     qmp_migrate_recover(uri, &err);
442 
443     hmp_handle_error(mon, err);
444 }
445 
446 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
447 {
448     Error *err = NULL;
449 
450     qmp_migrate_pause(&err);
451 
452     hmp_handle_error(mon, err);
453 }
454 
455 
456 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
457 {
458     const char *cap = qdict_get_str(qdict, "capability");
459     bool state = qdict_get_bool(qdict, "state");
460     Error *err = NULL;
461     MigrationCapabilityStatusList *caps = NULL;
462     MigrationCapabilityStatus *value;
463     int val;
464 
465     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
466     if (val < 0) {
467         goto end;
468     }
469 
470     value = g_malloc0(sizeof(*value));
471     value->capability = val;
472     value->state = state;
473     QAPI_LIST_PREPEND(caps, value);
474     qmp_migrate_set_capabilities(caps, &err);
475     qapi_free_MigrationCapabilityStatusList(caps);
476 
477 end:
478     hmp_handle_error(mon, err);
479 }
480 
481 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
482 {
483     const char *param = qdict_get_str(qdict, "parameter");
484     const char *valuestr = qdict_get_str(qdict, "value");
485     Visitor *v = string_input_visitor_new(valuestr);
486     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
487     uint64_t valuebw = 0;
488     uint64_t cache_size;
489     Error *err = NULL;
490     int val, ret;
491 
492     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
493     if (val < 0) {
494         goto cleanup;
495     }
496 
497     switch (val) {
498     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
499         p->has_compress_level = true;
500         visit_type_uint8(v, param, &p->compress_level, &err);
501         break;
502     case MIGRATION_PARAMETER_COMPRESS_THREADS:
503         p->has_compress_threads = true;
504         visit_type_uint8(v, param, &p->compress_threads, &err);
505         break;
506     case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
507         p->has_compress_wait_thread = true;
508         visit_type_bool(v, param, &p->compress_wait_thread, &err);
509         break;
510     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
511         p->has_decompress_threads = true;
512         visit_type_uint8(v, param, &p->decompress_threads, &err);
513         break;
514     case MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD:
515         p->has_throttle_trigger_threshold = true;
516         visit_type_uint8(v, param, &p->throttle_trigger_threshold, &err);
517         break;
518     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
519         p->has_cpu_throttle_initial = true;
520         visit_type_uint8(v, param, &p->cpu_throttle_initial, &err);
521         break;
522     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
523         p->has_cpu_throttle_increment = true;
524         visit_type_uint8(v, param, &p->cpu_throttle_increment, &err);
525         break;
526     case MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW:
527         p->has_cpu_throttle_tailslow = true;
528         visit_type_bool(v, param, &p->cpu_throttle_tailslow, &err);
529         break;
530     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
531         p->has_max_cpu_throttle = true;
532         visit_type_uint8(v, param, &p->max_cpu_throttle, &err);
533         break;
534     case MIGRATION_PARAMETER_TLS_CREDS:
535         p->tls_creds = g_new0(StrOrNull, 1);
536         p->tls_creds->type = QTYPE_QSTRING;
537         visit_type_str(v, param, &p->tls_creds->u.s, &err);
538         break;
539     case MIGRATION_PARAMETER_TLS_HOSTNAME:
540         p->tls_hostname = g_new0(StrOrNull, 1);
541         p->tls_hostname->type = QTYPE_QSTRING;
542         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
543         break;
544     case MIGRATION_PARAMETER_TLS_AUTHZ:
545         p->tls_authz = g_new0(StrOrNull, 1);
546         p->tls_authz->type = QTYPE_QSTRING;
547         visit_type_str(v, param, &p->tls_authz->u.s, &err);
548         break;
549     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
550         p->has_max_bandwidth = true;
551         /*
552          * Can't use visit_type_size() here, because it
553          * defaults to Bytes rather than Mebibytes.
554          */
555         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
556         if (ret < 0 || valuebw > INT64_MAX
557             || (size_t)valuebw != valuebw) {
558             error_setg(&err, "Invalid size %s", valuestr);
559             break;
560         }
561         p->max_bandwidth = valuebw;
562         break;
563     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
564         p->has_downtime_limit = true;
565         visit_type_size(v, param, &p->downtime_limit, &err);
566         break;
567     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
568         p->has_x_checkpoint_delay = true;
569         visit_type_uint32(v, param, &p->x_checkpoint_delay, &err);
570         break;
571     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
572         p->has_block_incremental = true;
573         visit_type_bool(v, param, &p->block_incremental, &err);
574         break;
575     case MIGRATION_PARAMETER_MULTIFD_CHANNELS:
576         p->has_multifd_channels = true;
577         visit_type_uint8(v, param, &p->multifd_channels, &err);
578         break;
579     case MIGRATION_PARAMETER_MULTIFD_COMPRESSION:
580         p->has_multifd_compression = true;
581         visit_type_MultiFDCompression(v, param, &p->multifd_compression,
582                                       &err);
583         break;
584     case MIGRATION_PARAMETER_MULTIFD_ZLIB_LEVEL:
585         p->has_multifd_zlib_level = true;
586         visit_type_uint8(v, param, &p->multifd_zlib_level, &err);
587         break;
588     case MIGRATION_PARAMETER_MULTIFD_ZSTD_LEVEL:
589         p->has_multifd_zstd_level = true;
590         visit_type_uint8(v, param, &p->multifd_zstd_level, &err);
591         break;
592     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
593         p->has_xbzrle_cache_size = true;
594         if (!visit_type_size(v, param, &cache_size, &err)) {
595             break;
596         }
597         if (cache_size > INT64_MAX || (size_t)cache_size != cache_size) {
598             error_setg(&err, "Invalid size %s", valuestr);
599             break;
600         }
601         p->xbzrle_cache_size = cache_size;
602         break;
603     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
604         p->has_max_postcopy_bandwidth = true;
605         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
606         break;
607     case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
608         p->has_announce_initial = true;
609         visit_type_size(v, param, &p->announce_initial, &err);
610         break;
611     case MIGRATION_PARAMETER_ANNOUNCE_MAX:
612         p->has_announce_max = true;
613         visit_type_size(v, param, &p->announce_max, &err);
614         break;
615     case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
616         p->has_announce_rounds = true;
617         visit_type_size(v, param, &p->announce_rounds, &err);
618         break;
619     case MIGRATION_PARAMETER_ANNOUNCE_STEP:
620         p->has_announce_step = true;
621         visit_type_size(v, param, &p->announce_step, &err);
622         break;
623     case MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING:
624         error_setg(&err, "The block-bitmap-mapping parameter can only be set "
625                    "through QMP");
626         break;
627     case MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD:
628         p->has_x_vcpu_dirty_limit_period = true;
629         visit_type_size(v, param, &p->x_vcpu_dirty_limit_period, &err);
630         break;
631     default:
632         assert(0);
633     }
634 
635     if (err) {
636         goto cleanup;
637     }
638 
639     qmp_migrate_set_parameters(p, &err);
640 
641  cleanup:
642     qapi_free_MigrateSetParameters(p);
643     visit_free(v);
644     hmp_handle_error(mon, err);
645 }
646 
647 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
648 {
649     Error *err = NULL;
650     qmp_migrate_start_postcopy(&err);
651     hmp_handle_error(mon, err);
652 }
653 
654 #ifdef CONFIG_REPLICATION
655 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
656 {
657     Error *err = NULL;
658 
659     qmp_x_colo_lost_heartbeat(&err);
660     hmp_handle_error(mon, err);
661 }
662 #endif
663 
664 typedef struct HMPMigrationStatus {
665     QEMUTimer *timer;
666     Monitor *mon;
667     bool is_block_migration;
668 } HMPMigrationStatus;
669 
670 static void hmp_migrate_status_cb(void *opaque)
671 {
672     HMPMigrationStatus *status = opaque;
673     MigrationInfo *info;
674 
675     info = qmp_query_migrate(NULL);
676     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
677         info->status == MIGRATION_STATUS_SETUP) {
678         if (info->disk) {
679             int progress;
680 
681             if (info->disk->remaining) {
682                 progress = info->disk->transferred * 100 / info->disk->total;
683             } else {
684                 progress = 100;
685             }
686 
687             monitor_printf(status->mon, "Completed %d %%\r", progress);
688             monitor_flush(status->mon);
689         }
690 
691         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
692     } else {
693         if (status->is_block_migration) {
694             monitor_printf(status->mon, "\n");
695         }
696         if (info->error_desc) {
697             error_report("%s", info->error_desc);
698         }
699         monitor_resume(status->mon);
700         timer_free(status->timer);
701         g_free(status);
702     }
703 
704     qapi_free_MigrationInfo(info);
705 }
706 
707 void hmp_migrate(Monitor *mon, const QDict *qdict)
708 {
709     bool detach = qdict_get_try_bool(qdict, "detach", false);
710     bool blk = qdict_get_try_bool(qdict, "blk", false);
711     bool inc = qdict_get_try_bool(qdict, "inc", false);
712     bool resume = qdict_get_try_bool(qdict, "resume", false);
713     const char *uri = qdict_get_str(qdict, "uri");
714     Error *err = NULL;
715 
716     qmp_migrate(uri, !!blk, blk, !!inc, inc,
717                 false, false, true, resume, &err);
718     if (hmp_handle_error(mon, err)) {
719         return;
720     }
721 
722     if (!detach) {
723         HMPMigrationStatus *status;
724 
725         if (monitor_suspend(mon) < 0) {
726             monitor_printf(mon, "terminal does not allow synchronous "
727                            "migration, continuing detached\n");
728             return;
729         }
730 
731         status = g_malloc0(sizeof(*status));
732         status->mon = mon;
733         status->is_block_migration = blk || inc;
734         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
735                                           status);
736         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
737     }
738 }
739 
740 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
741                                        const char *str)
742 {
743     size_t len;
744 
745     len = strlen(str);
746     readline_set_completion_index(rs, len);
747     if (nb_args == 2) {
748         int i;
749         for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
750             readline_add_completion_of(rs, str, MigrationCapability_str(i));
751         }
752     } else if (nb_args == 3) {
753         readline_add_completion_of(rs, str, "on");
754         readline_add_completion_of(rs, str, "off");
755     }
756 }
757 
758 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
759                                       const char *str)
760 {
761     size_t len;
762 
763     len = strlen(str);
764     readline_set_completion_index(rs, len);
765     if (nb_args == 2) {
766         int i;
767         for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
768             readline_add_completion_of(rs, str, MigrationParameter_str(i));
769         }
770     }
771 }
772 
773 static void vm_completion(ReadLineState *rs, const char *str)
774 {
775     size_t len;
776     BlockDriverState *bs;
777     BdrvNextIterator it;
778 
779     len = strlen(str);
780     readline_set_completion_index(rs, len);
781 
782     for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
783         SnapshotInfoList *snapshots, *snapshot;
784         AioContext *ctx = bdrv_get_aio_context(bs);
785         bool ok = false;
786 
787         aio_context_acquire(ctx);
788         if (bdrv_can_snapshot(bs)) {
789             ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
790         }
791         aio_context_release(ctx);
792         if (!ok) {
793             continue;
794         }
795 
796         snapshot = snapshots;
797         while (snapshot) {
798             readline_add_completion_of(rs, str, snapshot->value->name);
799             readline_add_completion_of(rs, str, snapshot->value->id);
800             snapshot = snapshot->next;
801         }
802         qapi_free_SnapshotInfoList(snapshots);
803     }
804 
805 }
806 
807 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
808 {
809     if (nb_args == 2) {
810         vm_completion(rs, str);
811     }
812 }
813 
814 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
815 {
816     if (nb_args == 2) {
817         vm_completion(rs, str);
818     }
819 }
820