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