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