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