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 
369     qapi_free_MigrationParameters(params);
370 }
371 
372 void hmp_loadvm(Monitor *mon, const QDict *qdict)
373 {
374     int saved_vm_running  = runstate_is_running();
375     const char *name = qdict_get_str(qdict, "name");
376     Error *err = NULL;
377 
378     vm_stop(RUN_STATE_RESTORE_VM);
379 
380     if (load_snapshot(name, NULL, false, NULL, &err) && saved_vm_running) {
381         vm_start();
382     }
383     hmp_handle_error(mon, err);
384 }
385 
386 void hmp_savevm(Monitor *mon, const QDict *qdict)
387 {
388     Error *err = NULL;
389 
390     save_snapshot(qdict_get_try_str(qdict, "name"),
391                   true, NULL, false, NULL, &err);
392     hmp_handle_error(mon, err);
393 }
394 
395 void hmp_delvm(Monitor *mon, const QDict *qdict)
396 {
397     Error *err = NULL;
398     const char *name = qdict_get_str(qdict, "name");
399 
400     delete_snapshot(name, false, NULL, &err);
401     hmp_handle_error(mon, err);
402 }
403 
404 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
405 {
406     qmp_migrate_cancel(NULL);
407 }
408 
409 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
410 {
411     Error *err = NULL;
412     const char *state = qdict_get_str(qdict, "state");
413     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
414 
415     if (val >= 0) {
416         qmp_migrate_continue(val, &err);
417     }
418 
419     hmp_handle_error(mon, err);
420 }
421 
422 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
423 {
424     Error *err = NULL;
425     const char *uri = qdict_get_str(qdict, "uri");
426 
427     qmp_migrate_incoming(uri, &err);
428 
429     hmp_handle_error(mon, err);
430 }
431 
432 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
433 {
434     Error *err = NULL;
435     const char *uri = qdict_get_str(qdict, "uri");
436 
437     qmp_migrate_recover(uri, &err);
438 
439     hmp_handle_error(mon, err);
440 }
441 
442 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
443 {
444     Error *err = NULL;
445 
446     qmp_migrate_pause(&err);
447 
448     hmp_handle_error(mon, err);
449 }
450 
451 
452 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
453 {
454     const char *cap = qdict_get_str(qdict, "capability");
455     bool state = qdict_get_bool(qdict, "state");
456     Error *err = NULL;
457     MigrationCapabilityStatusList *caps = NULL;
458     MigrationCapabilityStatus *value;
459     int val;
460 
461     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
462     if (val < 0) {
463         goto end;
464     }
465 
466     value = g_malloc0(sizeof(*value));
467     value->capability = val;
468     value->state = state;
469     QAPI_LIST_PREPEND(caps, value);
470     qmp_migrate_set_capabilities(caps, &err);
471     qapi_free_MigrationCapabilityStatusList(caps);
472 
473 end:
474     hmp_handle_error(mon, err);
475 }
476 
477 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
478 {
479     const char *param = qdict_get_str(qdict, "parameter");
480     const char *valuestr = qdict_get_str(qdict, "value");
481     Visitor *v = string_input_visitor_new(valuestr);
482     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
483     uint64_t valuebw = 0;
484     uint64_t cache_size;
485     Error *err = NULL;
486     int val, ret;
487 
488     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
489     if (val < 0) {
490         goto cleanup;
491     }
492 
493     switch (val) {
494     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
495         p->has_compress_level = true;
496         visit_type_uint8(v, param, &p->compress_level, &err);
497         break;
498     case MIGRATION_PARAMETER_COMPRESS_THREADS:
499         p->has_compress_threads = true;
500         visit_type_uint8(v, param, &p->compress_threads, &err);
501         break;
502     case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
503         p->has_compress_wait_thread = true;
504         visit_type_bool(v, param, &p->compress_wait_thread, &err);
505         break;
506     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
507         p->has_decompress_threads = true;
508         visit_type_uint8(v, param, &p->decompress_threads, &err);
509         break;
510     case MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD:
511         p->has_throttle_trigger_threshold = true;
512         visit_type_uint8(v, param, &p->throttle_trigger_threshold, &err);
513         break;
514     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
515         p->has_cpu_throttle_initial = true;
516         visit_type_uint8(v, param, &p->cpu_throttle_initial, &err);
517         break;
518     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
519         p->has_cpu_throttle_increment = true;
520         visit_type_uint8(v, param, &p->cpu_throttle_increment, &err);
521         break;
522     case MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW:
523         p->has_cpu_throttle_tailslow = true;
524         visit_type_bool(v, param, &p->cpu_throttle_tailslow, &err);
525         break;
526     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
527         p->has_max_cpu_throttle = true;
528         visit_type_uint8(v, param, &p->max_cpu_throttle, &err);
529         break;
530     case MIGRATION_PARAMETER_TLS_CREDS:
531         p->tls_creds = g_new0(StrOrNull, 1);
532         p->tls_creds->type = QTYPE_QSTRING;
533         visit_type_str(v, param, &p->tls_creds->u.s, &err);
534         break;
535     case MIGRATION_PARAMETER_TLS_HOSTNAME:
536         p->tls_hostname = g_new0(StrOrNull, 1);
537         p->tls_hostname->type = QTYPE_QSTRING;
538         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
539         break;
540     case MIGRATION_PARAMETER_TLS_AUTHZ:
541         p->tls_authz = g_new0(StrOrNull, 1);
542         p->tls_authz->type = QTYPE_QSTRING;
543         visit_type_str(v, param, &p->tls_authz->u.s, &err);
544         break;
545     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
546         p->has_max_bandwidth = true;
547         /*
548          * Can't use visit_type_size() here, because it
549          * defaults to Bytes rather than Mebibytes.
550          */
551         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
552         if (ret < 0 || valuebw > INT64_MAX
553             || (size_t)valuebw != valuebw) {
554             error_setg(&err, "Invalid size %s", valuestr);
555             break;
556         }
557         p->max_bandwidth = valuebw;
558         break;
559     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
560         p->has_downtime_limit = true;
561         visit_type_size(v, param, &p->downtime_limit, &err);
562         break;
563     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
564         p->has_x_checkpoint_delay = true;
565         visit_type_uint32(v, param, &p->x_checkpoint_delay, &err);
566         break;
567     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
568         p->has_block_incremental = true;
569         visit_type_bool(v, param, &p->block_incremental, &err);
570         break;
571     case MIGRATION_PARAMETER_MULTIFD_CHANNELS:
572         p->has_multifd_channels = true;
573         visit_type_uint8(v, param, &p->multifd_channels, &err);
574         break;
575     case MIGRATION_PARAMETER_MULTIFD_COMPRESSION:
576         p->has_multifd_compression = true;
577         visit_type_MultiFDCompression(v, param, &p->multifd_compression,
578                                       &err);
579         break;
580     case MIGRATION_PARAMETER_MULTIFD_ZLIB_LEVEL:
581         p->has_multifd_zlib_level = true;
582         visit_type_uint8(v, param, &p->multifd_zlib_level, &err);
583         break;
584     case MIGRATION_PARAMETER_MULTIFD_ZSTD_LEVEL:
585         p->has_multifd_zstd_level = true;
586         visit_type_uint8(v, param, &p->multifd_zstd_level, &err);
587         break;
588     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
589         p->has_xbzrle_cache_size = true;
590         if (!visit_type_size(v, param, &cache_size, &err)) {
591             break;
592         }
593         if (cache_size > INT64_MAX || (size_t)cache_size != cache_size) {
594             error_setg(&err, "Invalid size %s", valuestr);
595             break;
596         }
597         p->xbzrle_cache_size = cache_size;
598         break;
599     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
600         p->has_max_postcopy_bandwidth = true;
601         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
602         break;
603     case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
604         p->has_announce_initial = true;
605         visit_type_size(v, param, &p->announce_initial, &err);
606         break;
607     case MIGRATION_PARAMETER_ANNOUNCE_MAX:
608         p->has_announce_max = true;
609         visit_type_size(v, param, &p->announce_max, &err);
610         break;
611     case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
612         p->has_announce_rounds = true;
613         visit_type_size(v, param, &p->announce_rounds, &err);
614         break;
615     case MIGRATION_PARAMETER_ANNOUNCE_STEP:
616         p->has_announce_step = true;
617         visit_type_size(v, param, &p->announce_step, &err);
618         break;
619     case MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING:
620         error_setg(&err, "The block-bitmap-mapping parameter can only be set "
621                    "through QMP");
622         break;
623     default:
624         assert(0);
625     }
626 
627     if (err) {
628         goto cleanup;
629     }
630 
631     qmp_migrate_set_parameters(p, &err);
632 
633  cleanup:
634     qapi_free_MigrateSetParameters(p);
635     visit_free(v);
636     hmp_handle_error(mon, err);
637 }
638 
639 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
640 {
641     Error *err = NULL;
642     qmp_migrate_start_postcopy(&err);
643     hmp_handle_error(mon, err);
644 }
645 
646 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
647 {
648     Error *err = NULL;
649 
650     qmp_x_colo_lost_heartbeat(&err);
651     hmp_handle_error(mon, err);
652 }
653 
654 typedef struct HMPMigrationStatus {
655     QEMUTimer *timer;
656     Monitor *mon;
657     bool is_block_migration;
658 } HMPMigrationStatus;
659 
660 static void hmp_migrate_status_cb(void *opaque)
661 {
662     HMPMigrationStatus *status = opaque;
663     MigrationInfo *info;
664 
665     info = qmp_query_migrate(NULL);
666     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
667         info->status == MIGRATION_STATUS_SETUP) {
668         if (info->disk) {
669             int progress;
670 
671             if (info->disk->remaining) {
672                 progress = info->disk->transferred * 100 / info->disk->total;
673             } else {
674                 progress = 100;
675             }
676 
677             monitor_printf(status->mon, "Completed %d %%\r", progress);
678             monitor_flush(status->mon);
679         }
680 
681         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
682     } else {
683         if (status->is_block_migration) {
684             monitor_printf(status->mon, "\n");
685         }
686         if (info->error_desc) {
687             error_report("%s", info->error_desc);
688         }
689         monitor_resume(status->mon);
690         timer_free(status->timer);
691         g_free(status);
692     }
693 
694     qapi_free_MigrationInfo(info);
695 }
696 
697 void hmp_migrate(Monitor *mon, const QDict *qdict)
698 {
699     bool detach = qdict_get_try_bool(qdict, "detach", false);
700     bool blk = qdict_get_try_bool(qdict, "blk", false);
701     bool inc = qdict_get_try_bool(qdict, "inc", false);
702     bool resume = qdict_get_try_bool(qdict, "resume", false);
703     const char *uri = qdict_get_str(qdict, "uri");
704     Error *err = NULL;
705 
706     qmp_migrate(uri, !!blk, blk, !!inc, inc,
707                 false, false, true, resume, &err);
708     if (hmp_handle_error(mon, err)) {
709         return;
710     }
711 
712     if (!detach) {
713         HMPMigrationStatus *status;
714 
715         if (monitor_suspend(mon) < 0) {
716             monitor_printf(mon, "terminal does not allow synchronous "
717                            "migration, continuing detached\n");
718             return;
719         }
720 
721         status = g_malloc0(sizeof(*status));
722         status->mon = mon;
723         status->is_block_migration = blk || inc;
724         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
725                                           status);
726         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
727     }
728 }
729 
730 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
731                                        const char *str)
732 {
733     size_t len;
734 
735     len = strlen(str);
736     readline_set_completion_index(rs, len);
737     if (nb_args == 2) {
738         int i;
739         for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
740             readline_add_completion_of(rs, str, MigrationCapability_str(i));
741         }
742     } else if (nb_args == 3) {
743         readline_add_completion_of(rs, str, "on");
744         readline_add_completion_of(rs, str, "off");
745     }
746 }
747 
748 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
749                                       const char *str)
750 {
751     size_t len;
752 
753     len = strlen(str);
754     readline_set_completion_index(rs, len);
755     if (nb_args == 2) {
756         int i;
757         for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
758             readline_add_completion_of(rs, str, MigrationParameter_str(i));
759         }
760     }
761 }
762 
763 static void vm_completion(ReadLineState *rs, const char *str)
764 {
765     size_t len;
766     BlockDriverState *bs;
767     BdrvNextIterator it;
768 
769     len = strlen(str);
770     readline_set_completion_index(rs, len);
771 
772     for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
773         SnapshotInfoList *snapshots, *snapshot;
774         AioContext *ctx = bdrv_get_aio_context(bs);
775         bool ok = false;
776 
777         aio_context_acquire(ctx);
778         if (bdrv_can_snapshot(bs)) {
779             ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
780         }
781         aio_context_release(ctx);
782         if (!ok) {
783             continue;
784         }
785 
786         snapshot = snapshots;
787         while (snapshot) {
788             readline_add_completion_of(rs, str, snapshot->value->name);
789             readline_add_completion_of(rs, str, snapshot->value->id);
790             snapshot = snapshot->next;
791         }
792         qapi_free_SnapshotInfoList(snapshots);
793     }
794 
795 }
796 
797 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
798 {
799     if (nb_args == 2) {
800         vm_completion(rs, str);
801     }
802 }
803 
804 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
805 {
806     if (nb_args == 2) {
807         vm_completion(rs, str);
808     }
809 }
810