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