xref: /openbmc/qemu/migration/migration-hmp-cmds.c (revision abe80c8ae24cc853b21e9574cf99bf9b97a78bc8)
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 static const gchar *format_time_str(uint64_t us)
56 {
57     const char *units[] = {"us", "ms", "sec"};
58     int index = 0;
59 
60     while (us >= 1000 && index + 1 < ARRAY_SIZE(units)) {
61         us /= 1000;
62         index++;
63     }
64 
65     return g_strdup_printf("%"PRIu64" %s", us, units[index]);
66 }
67 
68 static void migration_dump_blocktime(Monitor *mon, MigrationInfo *info)
69 {
70     if (info->has_postcopy_blocktime) {
71         monitor_printf(mon, "Postcopy Blocktime (ms): %" PRIu32 "\n",
72                        info->postcopy_blocktime);
73     }
74 
75     if (info->has_postcopy_vcpu_blocktime) {
76         uint32List *item = info->postcopy_vcpu_blocktime;
77         const char *sep = "";
78         int count = 0;
79 
80         monitor_printf(mon, "Postcopy vCPU Blocktime (ms):\n [");
81 
82         while (item) {
83             monitor_printf(mon, "%s%"PRIu32, sep, item->value);
84             item = item->next;
85             /* Each line 10 vcpu results, newline if there's more */
86             sep = ((++count % 10 == 0) && item) ? ",\n  " : ", ";
87         }
88         monitor_printf(mon, "]\n");
89     }
90 
91     if (info->has_postcopy_latency) {
92         monitor_printf(mon, "Postcopy Latency (ns): %" PRIu64 "\n",
93                        info->postcopy_latency);
94     }
95 
96     if (info->has_postcopy_non_vcpu_latency) {
97         monitor_printf(mon, "Postcopy non-vCPU Latencies (ns): %" PRIu64 "\n",
98                        info->postcopy_non_vcpu_latency);
99     }
100 
101     if (info->has_postcopy_vcpu_latency) {
102         uint64List *item = info->postcopy_vcpu_latency;
103         const char *sep = "";
104         int count = 0;
105 
106         monitor_printf(mon, "Postcopy vCPU Latencies (ns):\n [");
107 
108         while (item) {
109             monitor_printf(mon, "%s%"PRIu64, sep, item->value);
110             item = item->next;
111             /* Each line 10 vcpu results, newline if there's more */
112             sep = ((++count % 10 == 0) && item) ? ",\n  " : ", ";
113         }
114         monitor_printf(mon, "]\n");
115     }
116 
117     if (info->has_postcopy_latency_dist) {
118         uint64List *item = info->postcopy_latency_dist;
119         int count = 0;
120 
121         monitor_printf(mon, "Postcopy Latency Distribution:\n");
122 
123         while (item) {
124             g_autofree const gchar *from = format_time_str(1UL << count);
125             g_autofree const gchar *to = format_time_str(1UL << (count + 1));
126 
127             monitor_printf(mon, "  [ %8s - %8s ]: %10"PRIu64"\n",
128                            from, to, item->value);
129             item = item->next;
130             count++;
131         }
132     }
133 }
134 
135 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
136 {
137     bool show_all = qdict_get_try_bool(qdict, "all", false);
138     MigrationInfo *info;
139 
140     info = qmp_query_migrate(NULL);
141 
142     if (info->blocked_reasons) {
143         strList *reasons = info->blocked_reasons;
144         monitor_printf(mon, "Outgoing migration blocked:\n");
145         while (reasons) {
146             monitor_printf(mon, "  %s\n", reasons->value);
147             reasons = reasons->next;
148         }
149     }
150 
151     if (info->has_status) {
152         monitor_printf(mon, "Status: \t\t%s",
153                        MigrationStatus_str(info->status));
154         if ((info->status == MIGRATION_STATUS_FAILED ||
155              info->status == MIGRATION_STATUS_POSTCOPY_PAUSED) &&
156             info->error_desc) {
157             monitor_printf(mon, " (%s)\n", info->error_desc);
158         } else {
159             monitor_printf(mon, "\n");
160         }
161 
162         if (info->total_time) {
163             monitor_printf(mon, "Time (ms): \t\ttotal=%" PRIu64,
164                            info->total_time);
165             if (info->has_setup_time) {
166                 monitor_printf(mon, ", setup=%" PRIu64,
167                                info->setup_time);
168             }
169             if (info->has_expected_downtime) {
170                 monitor_printf(mon, ", exp_down=%" PRIu64,
171                                info->expected_downtime);
172             }
173             if (info->has_downtime) {
174                 monitor_printf(mon, ", down=%" PRIu64,
175                                info->downtime);
176             }
177             monitor_printf(mon, "\n");
178         }
179     }
180 
181     if (info->has_socket_address) {
182         SocketAddressList *addr;
183 
184         monitor_printf(mon, "Sockets: [\n");
185 
186         for (addr = info->socket_address; addr; addr = addr->next) {
187             char *s = socket_uri(addr->value);
188             monitor_printf(mon, "\t%s\n", s);
189             g_free(s);
190         }
191         monitor_printf(mon, "]\n");
192     }
193 
194     if (info->ram) {
195         g_autofree char *str_psize = size_to_str(info->ram->page_size);
196         g_autofree char *str_total = size_to_str(info->ram->total);
197         g_autofree char *str_transferred = size_to_str(info->ram->transferred);
198         g_autofree char *str_remaining = size_to_str(info->ram->remaining);
199         g_autofree char *str_precopy = size_to_str(info->ram->precopy_bytes);
200         g_autofree char *str_multifd = size_to_str(info->ram->multifd_bytes);
201         g_autofree char *str_postcopy = size_to_str(info->ram->postcopy_bytes);
202 
203         monitor_printf(mon, "RAM info:\n");
204         monitor_printf(mon, "  Throughput (Mbps): \t%0.2f\n",
205                        info->ram->mbps);
206         monitor_printf(mon, "  Sizes: \t\tpagesize=%s, total=%s\n",
207                        str_psize, str_total);
208         monitor_printf(mon, "  Transfers: \t\ttransferred=%s, remain=%s\n",
209                        str_transferred, str_remaining);
210         monitor_printf(mon, "    Channels: \t\tprecopy=%s, "
211                        "multifd=%s, postcopy=%s",
212                        str_precopy, str_multifd, str_postcopy);
213 
214         if (info->vfio) {
215             g_autofree char *str_vfio = size_to_str(info->vfio->transferred);
216 
217             monitor_printf(mon, ", vfio=%s", str_vfio);
218         }
219         monitor_printf(mon, "\n");
220 
221         monitor_printf(mon, "    Page Types: \tnormal=%" PRIu64
222                        ", zero=%" PRIu64 "\n",
223                        info->ram->normal, info->ram->duplicate);
224         monitor_printf(mon, "  Page Rates (pps): \ttransfer=%" PRIu64,
225                        info->ram->pages_per_second);
226         if (info->ram->dirty_pages_rate) {
227             monitor_printf(mon, ", dirty=%" PRIu64,
228                            info->ram->dirty_pages_rate);
229         }
230         monitor_printf(mon, "\n");
231 
232         monitor_printf(mon, "  Others: \t\tdirty_syncs=%" PRIu64,
233                        info->ram->dirty_sync_count);
234         if (info->ram->postcopy_requests) {
235             monitor_printf(mon, ", postcopy_req=%" PRIu64,
236                            info->ram->postcopy_requests);
237         }
238         if (info->ram->downtime_bytes) {
239             monitor_printf(mon, ", downtime_bytes=%" PRIu64,
240                            info->ram->downtime_bytes);
241         }
242         if (info->ram->dirty_sync_missed_zero_copy) {
243             monitor_printf(mon, ", zerocopy_fallbacks=%" PRIu64,
244                            info->ram->dirty_sync_missed_zero_copy);
245         }
246         monitor_printf(mon, "\n");
247     }
248 
249     if (!show_all) {
250         goto out;
251     }
252 
253     migration_global_dump(mon);
254 
255     if (info->xbzrle_cache) {
256         monitor_printf(mon, "XBZRLE: size=%" PRIu64
257                        ", transferred=%" PRIu64
258                        ", pages=%" PRIu64
259                        ", miss=%" PRIu64 "\n"
260                        "  miss_rate=%0.2f"
261                        ", encode_rate=%0.2f"
262                        ", overflow=%" PRIu64 "\n",
263                        info->xbzrle_cache->cache_size,
264                        info->xbzrle_cache->bytes,
265                        info->xbzrle_cache->pages,
266                        info->xbzrle_cache->cache_miss,
267                        info->xbzrle_cache->cache_miss_rate,
268                        info->xbzrle_cache->encoding_rate,
269                        info->xbzrle_cache->overflow);
270     }
271 
272     if (info->has_cpu_throttle_percentage) {
273         monitor_printf(mon, "CPU Throttle (%%): %" PRIu64 "\n",
274                        info->cpu_throttle_percentage);
275     }
276 
277     if (info->has_dirty_limit_throttle_time_per_round) {
278         monitor_printf(mon, "Dirty-limit Throttle (us): %" PRIu64 "\n",
279                        info->dirty_limit_throttle_time_per_round);
280     }
281 
282     if (info->has_dirty_limit_ring_full_time) {
283         monitor_printf(mon, "Dirty-limit Ring Full (us): %" PRIu64 "\n",
284                        info->dirty_limit_ring_full_time);
285     }
286 
287     migration_dump_blocktime(mon, info);
288 out:
289     qapi_free_MigrationInfo(info);
290 }
291 
292 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
293 {
294     MigrationCapabilityStatusList *caps, *cap;
295 
296     caps = qmp_query_migrate_capabilities(NULL);
297 
298     if (caps) {
299         for (cap = caps; cap; cap = cap->next) {
300             monitor_printf(mon, "%s: %s\n",
301                            MigrationCapability_str(cap->value->capability),
302                            cap->value->state ? "on" : "off");
303         }
304     }
305 
306     qapi_free_MigrationCapabilityStatusList(caps);
307 }
308 
309 static void monitor_print_cpr_exec_command(Monitor *mon, strList *args)
310 {
311     monitor_printf(mon, "%s:",
312         MigrationParameter_str(MIGRATION_PARAMETER_CPR_EXEC_COMMAND));
313 
314     while (args) {
315         monitor_printf(mon, " %s", args->value);
316         args = args->next;
317     }
318     monitor_printf(mon, "\n");
319 }
320 
321 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
322 {
323     MigrationParameters *params;
324 
325     params = qmp_query_migrate_parameters(NULL);
326 
327     if (params) {
328         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
329             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_INITIAL),
330             params->announce_initial);
331         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
332             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_MAX),
333             params->announce_max);
334         monitor_printf(mon, "%s: %" PRIu64 "\n",
335             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_ROUNDS),
336             params->announce_rounds);
337         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
338             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
339             params->announce_step);
340         assert(params->has_throttle_trigger_threshold);
341         monitor_printf(mon, "%s: %u\n",
342             MigrationParameter_str(MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD),
343             params->throttle_trigger_threshold);
344         assert(params->has_cpu_throttle_initial);
345         monitor_printf(mon, "%s: %u\n",
346             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
347             params->cpu_throttle_initial);
348         assert(params->has_cpu_throttle_increment);
349         monitor_printf(mon, "%s: %u\n",
350             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
351             params->cpu_throttle_increment);
352         assert(params->has_cpu_throttle_tailslow);
353         monitor_printf(mon, "%s: %s\n",
354             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW),
355             params->cpu_throttle_tailslow ? "on" : "off");
356         assert(params->has_max_cpu_throttle);
357         monitor_printf(mon, "%s: %u\n",
358             MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
359             params->max_cpu_throttle);
360         assert(params->tls_creds);
361         monitor_printf(mon, "%s: '%s'\n",
362             MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
363             params->tls_creds);
364         assert(params->tls_hostname);
365         monitor_printf(mon, "%s: '%s'\n",
366             MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
367             params->tls_hostname);
368         assert(params->tls_authz);
369         monitor_printf(mon, "%s: '%s'\n",
370             MigrationParameter_str(MIGRATION_PARAMETER_TLS_AUTHZ),
371             params->tls_authz);
372         assert(params->has_max_bandwidth);
373         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
374             MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
375             params->max_bandwidth);
376         assert(params->has_avail_switchover_bandwidth);
377         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
378             MigrationParameter_str(MIGRATION_PARAMETER_AVAIL_SWITCHOVER_BANDWIDTH),
379             params->avail_switchover_bandwidth);
380         assert(params->has_max_postcopy_bandwidth);
381         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
382             MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
383             params->max_postcopy_bandwidth);
384         assert(params->has_downtime_limit);
385         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
386             MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
387             params->downtime_limit);
388         assert(params->has_x_checkpoint_delay);
389         monitor_printf(mon, "%s: %u ms\n",
390             MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
391             params->x_checkpoint_delay);
392         monitor_printf(mon, "%s: %u\n",
393             MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_CHANNELS),
394             params->multifd_channels);
395         monitor_printf(mon, "%s: %s\n",
396             MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_COMPRESSION),
397             MultiFDCompression_str(params->multifd_compression));
398         assert(params->has_zero_page_detection);
399         monitor_printf(mon, "%s: %s\n",
400             MigrationParameter_str(MIGRATION_PARAMETER_ZERO_PAGE_DETECTION),
401             qapi_enum_lookup(&ZeroPageDetection_lookup,
402                 params->zero_page_detection));
403         monitor_printf(mon, "%s: %" PRIu64 " bytes\n",
404             MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
405             params->xbzrle_cache_size);
406 
407         if (params->has_block_bitmap_mapping) {
408             const BitmapMigrationNodeAliasList *bmnal;
409 
410             monitor_printf(mon, "%s:\n",
411                            MigrationParameter_str(
412                                MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING));
413 
414             for (bmnal = params->block_bitmap_mapping;
415                  bmnal;
416                  bmnal = bmnal->next)
417             {
418                 const BitmapMigrationNodeAlias *bmna = bmnal->value;
419                 const BitmapMigrationBitmapAliasList *bmbal;
420 
421                 monitor_printf(mon, "  '%s' -> '%s'\n",
422                                bmna->node_name, bmna->alias);
423 
424                 for (bmbal = bmna->bitmaps; bmbal; bmbal = bmbal->next) {
425                     const BitmapMigrationBitmapAlias *bmba = bmbal->value;
426 
427                     monitor_printf(mon, "    '%s' -> '%s'\n",
428                                    bmba->name, bmba->alias);
429                 }
430             }
431         }
432 
433         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
434         MigrationParameter_str(MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD),
435         params->x_vcpu_dirty_limit_period);
436 
437         monitor_printf(mon, "%s: %" PRIu64 " MB/s\n",
438             MigrationParameter_str(MIGRATION_PARAMETER_VCPU_DIRTY_LIMIT),
439             params->vcpu_dirty_limit);
440 
441         assert(params->has_mode);
442         monitor_printf(mon, "%s: %s\n",
443             MigrationParameter_str(MIGRATION_PARAMETER_MODE),
444             qapi_enum_lookup(&MigMode_lookup, params->mode));
445 
446         if (params->has_direct_io) {
447             monitor_printf(mon, "%s: %s\n",
448                            MigrationParameter_str(
449                                MIGRATION_PARAMETER_DIRECT_IO),
450                            params->direct_io ? "on" : "off");
451         }
452 
453         assert(params->has_cpr_exec_command);
454         monitor_print_cpr_exec_command(mon, params->cpr_exec_command);
455     }
456 
457     qapi_free_MigrationParameters(params);
458 }
459 
460 void hmp_loadvm(Monitor *mon, const QDict *qdict)
461 {
462     RunState saved_state = runstate_get();
463 
464     const char *name = qdict_get_str(qdict, "name");
465     Error *err = NULL;
466 
467     vm_stop(RUN_STATE_RESTORE_VM);
468 
469     if (load_snapshot(name, NULL, false, NULL, &err)) {
470         load_snapshot_resume(saved_state);
471     }
472 
473     hmp_handle_error(mon, err);
474 }
475 
476 void hmp_savevm(Monitor *mon, const QDict *qdict)
477 {
478     Error *err = NULL;
479 
480     save_snapshot(qdict_get_try_str(qdict, "name"),
481                   true, NULL, false, NULL, &err);
482     hmp_handle_error(mon, err);
483 }
484 
485 void hmp_delvm(Monitor *mon, const QDict *qdict)
486 {
487     Error *err = NULL;
488     const char *name = qdict_get_str(qdict, "name");
489 
490     delete_snapshot(name, false, NULL, &err);
491     hmp_handle_error(mon, err);
492 }
493 
494 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
495 {
496     qmp_migrate_cancel(NULL);
497 }
498 
499 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
500 {
501     Error *err = NULL;
502     const char *state = qdict_get_str(qdict, "state");
503     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
504 
505     if (val >= 0) {
506         qmp_migrate_continue(val, &err);
507     }
508 
509     hmp_handle_error(mon, err);
510 }
511 
512 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
513 {
514     Error *err = NULL;
515     const char *uri = qdict_get_str(qdict, "uri");
516     MigrationChannelList *caps = NULL;
517     g_autoptr(MigrationChannel) channel = NULL;
518 
519     if (!migrate_uri_parse(uri, &channel, &err)) {
520         goto end;
521     }
522     QAPI_LIST_PREPEND(caps, g_steal_pointer(&channel));
523 
524     qmp_migrate_incoming(NULL, true, caps, true, false, &err);
525     qapi_free_MigrationChannelList(caps);
526 
527 end:
528     hmp_handle_error(mon, err);
529 }
530 
531 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
532 {
533     Error *err = NULL;
534     const char *uri = qdict_get_str(qdict, "uri");
535 
536     qmp_migrate_recover(uri, &err);
537 
538     hmp_handle_error(mon, err);
539 }
540 
541 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
542 {
543     Error *err = NULL;
544 
545     qmp_migrate_pause(&err);
546 
547     hmp_handle_error(mon, err);
548 }
549 
550 
551 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
552 {
553     const char *cap = qdict_get_str(qdict, "capability");
554     bool state = qdict_get_bool(qdict, "state");
555     Error *err = NULL;
556     MigrationCapabilityStatusList *caps = NULL;
557     MigrationCapabilityStatus *value;
558     int val;
559 
560     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
561     if (val < 0) {
562         goto end;
563     }
564 
565     value = g_malloc0(sizeof(*value));
566     value->capability = val;
567     value->state = state;
568     QAPI_LIST_PREPEND(caps, value);
569     qmp_migrate_set_capabilities(caps, &err);
570     qapi_free_MigrationCapabilityStatusList(caps);
571 
572 end:
573     hmp_handle_error(mon, err);
574 }
575 
576 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
577 {
578     const char *param = qdict_get_str(qdict, "parameter");
579     const char *valuestr = qdict_get_str(qdict, "value");
580     Visitor *v = string_input_visitor_new(valuestr);
581     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
582     uint64_t valuebw = 0;
583     uint64_t cache_size;
584     Error *err = NULL;
585     int val, ret;
586 
587     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
588     if (val < 0) {
589         goto cleanup;
590     }
591 
592     switch (val) {
593     case MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD:
594         p->has_throttle_trigger_threshold = true;
595         visit_type_uint8(v, param, &p->throttle_trigger_threshold, &err);
596         break;
597     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
598         p->has_cpu_throttle_initial = true;
599         visit_type_uint8(v, param, &p->cpu_throttle_initial, &err);
600         break;
601     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
602         p->has_cpu_throttle_increment = true;
603         visit_type_uint8(v, param, &p->cpu_throttle_increment, &err);
604         break;
605     case MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW:
606         p->has_cpu_throttle_tailslow = true;
607         visit_type_bool(v, param, &p->cpu_throttle_tailslow, &err);
608         break;
609     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
610         p->has_max_cpu_throttle = true;
611         visit_type_uint8(v, param, &p->max_cpu_throttle, &err);
612         break;
613     case MIGRATION_PARAMETER_TLS_CREDS:
614         p->tls_creds = g_new0(StrOrNull, 1);
615         p->tls_creds->type = QTYPE_QSTRING;
616         visit_type_str(v, param, &p->tls_creds->u.s, &err);
617         break;
618     case MIGRATION_PARAMETER_TLS_HOSTNAME:
619         p->tls_hostname = g_new0(StrOrNull, 1);
620         p->tls_hostname->type = QTYPE_QSTRING;
621         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
622         break;
623     case MIGRATION_PARAMETER_TLS_AUTHZ:
624         p->tls_authz = g_new0(StrOrNull, 1);
625         p->tls_authz->type = QTYPE_QSTRING;
626         visit_type_str(v, param, &p->tls_authz->u.s, &err);
627         break;
628     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
629         p->has_max_bandwidth = true;
630         /*
631          * Can't use visit_type_size() here, because it
632          * defaults to Bytes rather than Mebibytes.
633          */
634         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
635         if (ret < 0 || valuebw > INT64_MAX
636             || (size_t)valuebw != valuebw) {
637             error_setg(&err, "Invalid size %s", valuestr);
638             break;
639         }
640         p->max_bandwidth = valuebw;
641         break;
642     case MIGRATION_PARAMETER_AVAIL_SWITCHOVER_BANDWIDTH:
643         p->has_avail_switchover_bandwidth = true;
644         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
645         if (ret < 0 || valuebw > INT64_MAX
646             || (size_t)valuebw != valuebw) {
647             error_setg(&err, "Invalid size %s", valuestr);
648             break;
649         }
650         p->avail_switchover_bandwidth = valuebw;
651         break;
652     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
653         p->has_downtime_limit = true;
654         visit_type_size(v, param, &p->downtime_limit, &err);
655         break;
656     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
657         p->has_x_checkpoint_delay = true;
658         visit_type_uint32(v, param, &p->x_checkpoint_delay, &err);
659         break;
660     case MIGRATION_PARAMETER_MULTIFD_CHANNELS:
661         p->has_multifd_channels = true;
662         visit_type_uint8(v, param, &p->multifd_channels, &err);
663         break;
664     case MIGRATION_PARAMETER_MULTIFD_COMPRESSION:
665         p->has_multifd_compression = true;
666         visit_type_MultiFDCompression(v, param, &p->multifd_compression,
667                                       &err);
668         break;
669     case MIGRATION_PARAMETER_MULTIFD_ZLIB_LEVEL:
670         p->has_multifd_zlib_level = true;
671         visit_type_uint8(v, param, &p->multifd_zlib_level, &err);
672         break;
673     case MIGRATION_PARAMETER_MULTIFD_QATZIP_LEVEL:
674         p->has_multifd_qatzip_level = true;
675         visit_type_uint8(v, param, &p->multifd_qatzip_level, &err);
676         break;
677     case MIGRATION_PARAMETER_MULTIFD_ZSTD_LEVEL:
678         p->has_multifd_zstd_level = true;
679         visit_type_uint8(v, param, &p->multifd_zstd_level, &err);
680         break;
681     case MIGRATION_PARAMETER_ZERO_PAGE_DETECTION:
682         p->has_zero_page_detection = true;
683         visit_type_ZeroPageDetection(v, param, &p->zero_page_detection, &err);
684         break;
685     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
686         p->has_xbzrle_cache_size = true;
687         if (!visit_type_size(v, param, &cache_size, &err)) {
688             break;
689         }
690         if (cache_size > INT64_MAX || (size_t)cache_size != cache_size) {
691             error_setg(&err, "Invalid size %s", valuestr);
692             break;
693         }
694         p->xbzrle_cache_size = cache_size;
695         break;
696     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
697         p->has_max_postcopy_bandwidth = true;
698         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
699         break;
700     case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
701         p->has_announce_initial = true;
702         visit_type_size(v, param, &p->announce_initial, &err);
703         break;
704     case MIGRATION_PARAMETER_ANNOUNCE_MAX:
705         p->has_announce_max = true;
706         visit_type_size(v, param, &p->announce_max, &err);
707         break;
708     case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
709         p->has_announce_rounds = true;
710         visit_type_size(v, param, &p->announce_rounds, &err);
711         break;
712     case MIGRATION_PARAMETER_ANNOUNCE_STEP:
713         p->has_announce_step = true;
714         visit_type_size(v, param, &p->announce_step, &err);
715         break;
716     case MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING:
717         error_setg(&err, "The block-bitmap-mapping parameter can only be set "
718                    "through QMP");
719         break;
720     case MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD:
721         p->has_x_vcpu_dirty_limit_period = true;
722         visit_type_size(v, param, &p->x_vcpu_dirty_limit_period, &err);
723         break;
724     case MIGRATION_PARAMETER_VCPU_DIRTY_LIMIT:
725         p->has_vcpu_dirty_limit = true;
726         visit_type_size(v, param, &p->vcpu_dirty_limit, &err);
727         break;
728     case MIGRATION_PARAMETER_MODE:
729         p->has_mode = true;
730         visit_type_MigMode(v, param, &p->mode, &err);
731         break;
732     case MIGRATION_PARAMETER_DIRECT_IO:
733         p->has_direct_io = true;
734         visit_type_bool(v, param, &p->direct_io, &err);
735         break;
736     case MIGRATION_PARAMETER_CPR_EXEC_COMMAND: {
737         /*
738          * NOTE: g_autofree will only auto g_free() the strv array when
739          * needed, it will not free the strings within the array. It's
740          * intentional: when strv is set, the ownership of the strings will
741          * always be passed to p->cpr_exec_command via QAPI_LIST_APPEND().
742          */
743         g_autofree char **strv = NULL;
744         g_autoptr(GError) gerr = NULL;
745         strList **tail = &p->cpr_exec_command;
746 
747         if (!g_shell_parse_argv(valuestr, NULL, &strv, &gerr)) {
748             error_setg(&err, "%s", gerr->message);
749             break;
750         }
751         for (int i = 0; strv[i]; i++) {
752             QAPI_LIST_APPEND(tail, strv[i]);
753         }
754         p->has_cpr_exec_command = true;
755         break;
756     }
757     default:
758         g_assert_not_reached();
759     }
760 
761     if (err) {
762         goto cleanup;
763     }
764 
765     qmp_migrate_set_parameters(p, &err);
766 
767  cleanup:
768     qapi_free_MigrateSetParameters(p);
769     visit_free(v);
770     hmp_handle_error(mon, err);
771 }
772 
773 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
774 {
775     Error *err = NULL;
776     qmp_migrate_start_postcopy(&err);
777     hmp_handle_error(mon, err);
778 }
779 
780 #ifdef CONFIG_REPLICATION
781 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
782 {
783     Error *err = NULL;
784 
785     qmp_x_colo_lost_heartbeat(&err);
786     hmp_handle_error(mon, err);
787 }
788 #endif
789 
790 typedef struct HMPMigrationStatus {
791     QEMUTimer *timer;
792     Monitor *mon;
793 } HMPMigrationStatus;
794 
795 static void hmp_migrate_status_cb(void *opaque)
796 {
797     HMPMigrationStatus *status = opaque;
798     MigrationInfo *info;
799 
800     info = qmp_query_migrate(NULL);
801     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
802         info->status == MIGRATION_STATUS_SETUP) {
803         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
804     } else {
805         if (info->error_desc) {
806             error_report("%s", info->error_desc);
807         }
808         monitor_resume(status->mon);
809         timer_free(status->timer);
810         g_free(status);
811     }
812 
813     qapi_free_MigrationInfo(info);
814 }
815 
816 void hmp_migrate(Monitor *mon, const QDict *qdict)
817 {
818     bool detach = qdict_get_try_bool(qdict, "detach", false);
819     bool resume = qdict_get_try_bool(qdict, "resume", false);
820     const char *uri = qdict_get_str(qdict, "uri");
821     Error *err = NULL;
822     g_autoptr(MigrationChannelList) caps = NULL;
823     g_autoptr(MigrationChannel) channel = NULL;
824 
825     if (!migrate_uri_parse(uri, &channel, &err)) {
826         hmp_handle_error(mon, err);
827         return;
828     }
829     QAPI_LIST_PREPEND(caps, g_steal_pointer(&channel));
830 
831     qmp_migrate(NULL, true, caps, false, false, true, resume, &err);
832     if (hmp_handle_error(mon, err)) {
833         return;
834     }
835 
836     if (!detach) {
837         HMPMigrationStatus *status;
838 
839         if (monitor_suspend(mon) < 0) {
840             monitor_printf(mon, "terminal does not allow synchronous "
841                            "migration, continuing detached\n");
842             return;
843         }
844 
845         status = g_malloc0(sizeof(*status));
846         status->mon = mon;
847         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
848                                           status);
849         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
850     }
851 }
852 
853 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
854                                        const char *str)
855 {
856     size_t len;
857 
858     len = strlen(str);
859     readline_set_completion_index(rs, len);
860     if (nb_args == 2) {
861         int i;
862         for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
863             readline_add_completion_of(rs, str, MigrationCapability_str(i));
864         }
865     } else if (nb_args == 3) {
866         readline_add_completion_of(rs, str, "on");
867         readline_add_completion_of(rs, str, "off");
868     }
869 }
870 
871 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
872                                       const char *str)
873 {
874     size_t len;
875 
876     len = strlen(str);
877     readline_set_completion_index(rs, len);
878     if (nb_args == 2) {
879         int i;
880         for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
881             readline_add_completion_of(rs, str, MigrationParameter_str(i));
882         }
883     }
884 }
885 
886 static void vm_completion(ReadLineState *rs, const char *str)
887 {
888     size_t len;
889     BlockDriverState *bs;
890     BdrvNextIterator it;
891 
892     GRAPH_RDLOCK_GUARD_MAINLOOP();
893 
894     len = strlen(str);
895     readline_set_completion_index(rs, len);
896 
897     for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
898         SnapshotInfoList *snapshots, *snapshot;
899         bool ok = false;
900 
901         if (bdrv_can_snapshot(bs)) {
902             ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
903         }
904         if (!ok) {
905             continue;
906         }
907 
908         snapshot = snapshots;
909         while (snapshot) {
910             readline_add_completion_of(rs, str, snapshot->value->name);
911             readline_add_completion_of(rs, str, snapshot->value->id);
912             snapshot = snapshot->next;
913         }
914         qapi_free_SnapshotInfoList(snapshots);
915     }
916 
917 }
918 
919 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
920 {
921     if (nb_args == 2) {
922         vm_completion(rs, str);
923     }
924 }
925 
926 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
927 {
928     if (nb_args == 2) {
929         vm_completion(rs, str);
930     }
931 }
932