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