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