xref: /openbmc/qemu/monitor/hmp-cmds.c (revision 3950a377)
1 /*
2  * Human Monitor Interface commands
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 "monitor/hmp.h"
18 #include "net/net.h"
19 #include "net/eth.h"
20 #include "chardev/char.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/option.h"
25 #include "qemu/timer.h"
26 #include "qemu/sockets.h"
27 #include "monitor/monitor-internal.h"
28 #include "monitor/qdev.h"
29 #include "qapi/error.h"
30 #include "qapi/opts-visitor.h"
31 #include "qapi/qapi-builtin-visit.h"
32 #include "qapi/qapi-commands-block.h"
33 #include "qapi/qapi-commands-char.h"
34 #include "qapi/qapi-commands-migration.h"
35 #include "qapi/qapi-commands-misc.h"
36 #include "qapi/qapi-commands-net.h"
37 #include "qapi/qapi-commands-qdev.h"
38 #include "qapi/qapi-commands-rocker.h"
39 #include "qapi/qapi-commands-run-state.h"
40 #include "qapi/qapi-commands-tpm.h"
41 #include "qapi/qapi-commands-ui.h"
42 #include "qapi/qmp/qdict.h"
43 #include "qapi/qmp/qerror.h"
44 #include "qapi/string-input-visitor.h"
45 #include "qapi/string-output-visitor.h"
46 #include "qom/object_interfaces.h"
47 #include "ui/console.h"
48 #include "block/nbd.h"
49 #include "block/qapi.h"
50 #include "qemu-io.h"
51 #include "qemu/cutils.h"
52 #include "qemu/error-report.h"
53 #include "exec/ramlist.h"
54 #include "hw/intc/intc.h"
55 #include "hw/rdma/rdma.h"
56 #include "migration/snapshot.h"
57 #include "migration/misc.h"
58 
59 #ifdef CONFIG_SPICE
60 #include <spice/enums.h>
61 #endif
62 
63 void hmp_handle_error(Monitor *mon, Error **errp)
64 {
65     assert(errp);
66     if (*errp) {
67         error_reportf_err(*errp, "Error: ");
68     }
69 }
70 
71 void hmp_info_name(Monitor *mon, const QDict *qdict)
72 {
73     NameInfo *info;
74 
75     info = qmp_query_name(NULL);
76     if (info->has_name) {
77         monitor_printf(mon, "%s\n", info->name);
78     }
79     qapi_free_NameInfo(info);
80 }
81 
82 void hmp_info_version(Monitor *mon, const QDict *qdict)
83 {
84     VersionInfo *info;
85 
86     info = qmp_query_version(NULL);
87 
88     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
89                    info->qemu->major, info->qemu->minor, info->qemu->micro,
90                    info->package);
91 
92     qapi_free_VersionInfo(info);
93 }
94 
95 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
96 {
97     KvmInfo *info;
98 
99     info = qmp_query_kvm(NULL);
100     monitor_printf(mon, "kvm support: ");
101     if (info->present) {
102         monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
103     } else {
104         monitor_printf(mon, "not compiled\n");
105     }
106 
107     qapi_free_KvmInfo(info);
108 }
109 
110 void hmp_info_status(Monitor *mon, const QDict *qdict)
111 {
112     StatusInfo *info;
113 
114     info = qmp_query_status(NULL);
115 
116     monitor_printf(mon, "VM status: %s%s",
117                    info->running ? "running" : "paused",
118                    info->singlestep ? " (single step mode)" : "");
119 
120     if (!info->running && info->status != RUN_STATE_PAUSED) {
121         monitor_printf(mon, " (%s)", RunState_str(info->status));
122     }
123 
124     monitor_printf(mon, "\n");
125 
126     qapi_free_StatusInfo(info);
127 }
128 
129 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
130 {
131     UuidInfo *info;
132 
133     info = qmp_query_uuid(NULL);
134     monitor_printf(mon, "%s\n", info->UUID);
135     qapi_free_UuidInfo(info);
136 }
137 
138 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
139 {
140     ChardevInfoList *char_info, *info;
141 
142     char_info = qmp_query_chardev(NULL);
143     for (info = char_info; info; info = info->next) {
144         monitor_printf(mon, "%s: filename=%s\n", info->value->label,
145                                                  info->value->filename);
146     }
147 
148     qapi_free_ChardevInfoList(char_info);
149 }
150 
151 void hmp_info_mice(Monitor *mon, const QDict *qdict)
152 {
153     MouseInfoList *mice_list, *mouse;
154 
155     mice_list = qmp_query_mice(NULL);
156     if (!mice_list) {
157         monitor_printf(mon, "No mouse devices connected\n");
158         return;
159     }
160 
161     for (mouse = mice_list; mouse; mouse = mouse->next) {
162         monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
163                        mouse->value->current ? '*' : ' ',
164                        mouse->value->index, mouse->value->name,
165                        mouse->value->absolute ? " (absolute)" : "");
166     }
167 
168     qapi_free_MouseInfoList(mice_list);
169 }
170 
171 static char *SocketAddress_to_str(SocketAddress *addr)
172 {
173     switch (addr->type) {
174     case SOCKET_ADDRESS_TYPE_INET:
175         return g_strdup_printf("tcp:%s:%s",
176                                addr->u.inet.host,
177                                addr->u.inet.port);
178     case SOCKET_ADDRESS_TYPE_UNIX:
179         return g_strdup_printf("unix:%s",
180                                addr->u.q_unix.path);
181     case SOCKET_ADDRESS_TYPE_FD:
182         return g_strdup_printf("fd:%s", addr->u.fd.str);
183     case SOCKET_ADDRESS_TYPE_VSOCK:
184         return g_strdup_printf("tcp:%s:%s",
185                                addr->u.vsock.cid,
186                                addr->u.vsock.port);
187     default:
188         return g_strdup("unknown address type");
189     }
190 }
191 
192 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
193 {
194     MigrationInfo *info;
195     MigrationCapabilityStatusList *caps, *cap;
196 
197     info = qmp_query_migrate(NULL);
198     caps = qmp_query_migrate_capabilities(NULL);
199 
200     migration_global_dump(mon);
201 
202     /* do not display parameters during setup */
203     if (info->has_status && caps) {
204         monitor_printf(mon, "capabilities: ");
205         for (cap = caps; cap; cap = cap->next) {
206             monitor_printf(mon, "%s: %s ",
207                            MigrationCapability_str(cap->value->capability),
208                            cap->value->state ? "on" : "off");
209         }
210         monitor_printf(mon, "\n");
211     }
212 
213     if (info->has_status) {
214         monitor_printf(mon, "Migration status: %s",
215                        MigrationStatus_str(info->status));
216         if (info->status == MIGRATION_STATUS_FAILED &&
217             info->has_error_desc) {
218             monitor_printf(mon, " (%s)\n", info->error_desc);
219         } else {
220             monitor_printf(mon, "\n");
221         }
222 
223         monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
224                        info->total_time);
225         if (info->has_expected_downtime) {
226             monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
227                            info->expected_downtime);
228         }
229         if (info->has_downtime) {
230             monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
231                            info->downtime);
232         }
233         if (info->has_setup_time) {
234             monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
235                            info->setup_time);
236         }
237     }
238 
239     if (info->has_ram) {
240         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
241                        info->ram->transferred >> 10);
242         monitor_printf(mon, "throughput: %0.2f mbps\n",
243                        info->ram->mbps);
244         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
245                        info->ram->remaining >> 10);
246         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
247                        info->ram->total >> 10);
248         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
249                        info->ram->duplicate);
250         monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
251                        info->ram->skipped);
252         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
253                        info->ram->normal);
254         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
255                        info->ram->normal_bytes >> 10);
256         monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
257                        info->ram->dirty_sync_count);
258         monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
259                        info->ram->page_size >> 10);
260         monitor_printf(mon, "multifd bytes: %" PRIu64 " kbytes\n",
261                        info->ram->multifd_bytes >> 10);
262         monitor_printf(mon, "pages-per-second: %" PRIu64 "\n",
263                        info->ram->pages_per_second);
264 
265         if (info->ram->dirty_pages_rate) {
266             monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
267                            info->ram->dirty_pages_rate);
268         }
269         if (info->ram->postcopy_requests) {
270             monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
271                            info->ram->postcopy_requests);
272         }
273     }
274 
275     if (info->has_disk) {
276         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
277                        info->disk->transferred >> 10);
278         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
279                        info->disk->remaining >> 10);
280         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
281                        info->disk->total >> 10);
282     }
283 
284     if (info->has_xbzrle_cache) {
285         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
286                        info->xbzrle_cache->cache_size);
287         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
288                        info->xbzrle_cache->bytes >> 10);
289         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
290                        info->xbzrle_cache->pages);
291         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
292                        info->xbzrle_cache->cache_miss);
293         monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
294                        info->xbzrle_cache->cache_miss_rate);
295         monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
296                        info->xbzrle_cache->overflow);
297     }
298 
299     if (info->has_compression) {
300         monitor_printf(mon, "compression pages: %" PRIu64 " pages\n",
301                        info->compression->pages);
302         monitor_printf(mon, "compression busy: %" PRIu64 "\n",
303                        info->compression->busy);
304         monitor_printf(mon, "compression busy rate: %0.2f\n",
305                        info->compression->busy_rate);
306         monitor_printf(mon, "compressed size: %" PRIu64 "\n",
307                        info->compression->compressed_size);
308         monitor_printf(mon, "compression rate: %0.2f\n",
309                        info->compression->compression_rate);
310     }
311 
312     if (info->has_cpu_throttle_percentage) {
313         monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
314                        info->cpu_throttle_percentage);
315     }
316 
317     if (info->has_postcopy_blocktime) {
318         monitor_printf(mon, "postcopy blocktime: %u\n",
319                        info->postcopy_blocktime);
320     }
321 
322     if (info->has_postcopy_vcpu_blocktime) {
323         Visitor *v;
324         char *str;
325         v = string_output_visitor_new(false, &str);
326         visit_type_uint32List(v, NULL, &info->postcopy_vcpu_blocktime, NULL);
327         visit_complete(v, &str);
328         monitor_printf(mon, "postcopy vcpu blocktime: %s\n", str);
329         g_free(str);
330         visit_free(v);
331     }
332     if (info->has_socket_address) {
333         SocketAddressList *addr;
334 
335         monitor_printf(mon, "socket address: [\n");
336 
337         for (addr = info->socket_address; addr; addr = addr->next) {
338             char *s = SocketAddress_to_str(addr->value);
339             monitor_printf(mon, "\t%s\n", s);
340             g_free(s);
341         }
342         monitor_printf(mon, "]\n");
343     }
344     qapi_free_MigrationInfo(info);
345     qapi_free_MigrationCapabilityStatusList(caps);
346 }
347 
348 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
349 {
350     MigrationCapabilityStatusList *caps, *cap;
351 
352     caps = qmp_query_migrate_capabilities(NULL);
353 
354     if (caps) {
355         for (cap = caps; cap; cap = cap->next) {
356             monitor_printf(mon, "%s: %s\n",
357                            MigrationCapability_str(cap->value->capability),
358                            cap->value->state ? "on" : "off");
359         }
360     }
361 
362     qapi_free_MigrationCapabilityStatusList(caps);
363 }
364 
365 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
366 {
367     MigrationParameters *params;
368 
369     params = qmp_query_migrate_parameters(NULL);
370 
371     if (params) {
372         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
373             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_INITIAL),
374             params->announce_initial);
375         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
376             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_MAX),
377             params->announce_max);
378         monitor_printf(mon, "%s: %" PRIu64 "\n",
379             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_ROUNDS),
380             params->announce_rounds);
381         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
382             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
383             params->announce_step);
384         assert(params->has_compress_level);
385         monitor_printf(mon, "%s: %u\n",
386             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
387             params->compress_level);
388         assert(params->has_compress_threads);
389         monitor_printf(mon, "%s: %u\n",
390             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
391             params->compress_threads);
392         assert(params->has_compress_wait_thread);
393         monitor_printf(mon, "%s: %s\n",
394             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD),
395             params->compress_wait_thread ? "on" : "off");
396         assert(params->has_decompress_threads);
397         monitor_printf(mon, "%s: %u\n",
398             MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
399             params->decompress_threads);
400         assert(params->has_cpu_throttle_initial);
401         monitor_printf(mon, "%s: %u\n",
402             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
403             params->cpu_throttle_initial);
404         assert(params->has_cpu_throttle_increment);
405         monitor_printf(mon, "%s: %u\n",
406             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
407             params->cpu_throttle_increment);
408         assert(params->has_max_cpu_throttle);
409         monitor_printf(mon, "%s: %u\n",
410             MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
411             params->max_cpu_throttle);
412         assert(params->has_tls_creds);
413         monitor_printf(mon, "%s: '%s'\n",
414             MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
415             params->tls_creds);
416         assert(params->has_tls_hostname);
417         monitor_printf(mon, "%s: '%s'\n",
418             MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
419             params->tls_hostname);
420         assert(params->has_max_bandwidth);
421         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
422             MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
423             params->max_bandwidth);
424         assert(params->has_downtime_limit);
425         monitor_printf(mon, "%s: %" PRIu64 " milliseconds\n",
426             MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
427             params->downtime_limit);
428         assert(params->has_x_checkpoint_delay);
429         monitor_printf(mon, "%s: %u\n",
430             MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
431             params->x_checkpoint_delay);
432         assert(params->has_block_incremental);
433         monitor_printf(mon, "%s: %s\n",
434             MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
435             params->block_incremental ? "on" : "off");
436         monitor_printf(mon, "%s: %u\n",
437             MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_CHANNELS),
438             params->multifd_channels);
439         monitor_printf(mon, "%s: %" PRIu64 "\n",
440             MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
441             params->xbzrle_cache_size);
442         monitor_printf(mon, "%s: %" PRIu64 "\n",
443             MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
444             params->max_postcopy_bandwidth);
445         monitor_printf(mon, " %s: '%s'\n",
446             MigrationParameter_str(MIGRATION_PARAMETER_TLS_AUTHZ),
447             params->has_tls_authz ? params->tls_authz : "");
448     }
449 
450     qapi_free_MigrationParameters(params);
451 }
452 
453 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
454 {
455     monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
456                    qmp_query_migrate_cache_size(NULL) >> 10);
457 }
458 
459 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
460 {
461     CpuInfoFastList *cpu_list, *cpu;
462 
463     cpu_list = qmp_query_cpus_fast(NULL);
464 
465     for (cpu = cpu_list; cpu; cpu = cpu->next) {
466         int active = ' ';
467 
468         if (cpu->value->cpu_index == monitor_get_cpu_index()) {
469             active = '*';
470         }
471 
472         monitor_printf(mon, "%c CPU #%" PRId64 ":", active,
473                        cpu->value->cpu_index);
474         monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
475     }
476 
477     qapi_free_CpuInfoFastList(cpu_list);
478 }
479 
480 static void print_block_info(Monitor *mon, BlockInfo *info,
481                              BlockDeviceInfo *inserted, bool verbose)
482 {
483     ImageInfo *image_info;
484 
485     assert(!info || !info->has_inserted || info->inserted == inserted);
486 
487     if (info && *info->device) {
488         monitor_printf(mon, "%s", info->device);
489         if (inserted && inserted->has_node_name) {
490             monitor_printf(mon, " (%s)", inserted->node_name);
491         }
492     } else {
493         assert(info || inserted);
494         monitor_printf(mon, "%s",
495                        inserted && inserted->has_node_name ? inserted->node_name
496                        : info && info->has_qdev ? info->qdev
497                        : "<anonymous>");
498     }
499 
500     if (inserted) {
501         monitor_printf(mon, ": %s (%s%s%s)\n",
502                        inserted->file,
503                        inserted->drv,
504                        inserted->ro ? ", read-only" : "",
505                        inserted->encrypted ? ", encrypted" : "");
506     } else {
507         monitor_printf(mon, ": [not inserted]\n");
508     }
509 
510     if (info) {
511         if (info->has_qdev) {
512             monitor_printf(mon, "    Attached to:      %s\n", info->qdev);
513         }
514         if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
515             monitor_printf(mon, "    I/O status:       %s\n",
516                            BlockDeviceIoStatus_str(info->io_status));
517         }
518 
519         if (info->removable) {
520             monitor_printf(mon, "    Removable device: %slocked, tray %s\n",
521                            info->locked ? "" : "not ",
522                            info->tray_open ? "open" : "closed");
523         }
524     }
525 
526 
527     if (!inserted) {
528         return;
529     }
530 
531     monitor_printf(mon, "    Cache mode:       %s%s%s\n",
532                    inserted->cache->writeback ? "writeback" : "writethrough",
533                    inserted->cache->direct ? ", direct" : "",
534                    inserted->cache->no_flush ? ", ignore flushes" : "");
535 
536     if (inserted->has_backing_file) {
537         monitor_printf(mon,
538                        "    Backing file:     %s "
539                        "(chain depth: %" PRId64 ")\n",
540                        inserted->backing_file,
541                        inserted->backing_file_depth);
542     }
543 
544     if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
545         monitor_printf(mon, "    Detect zeroes:    %s\n",
546                 BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
547     }
548 
549     if (inserted->bps  || inserted->bps_rd  || inserted->bps_wr  ||
550         inserted->iops || inserted->iops_rd || inserted->iops_wr)
551     {
552         monitor_printf(mon, "    I/O throttling:   bps=%" PRId64
553                         " bps_rd=%" PRId64  " bps_wr=%" PRId64
554                         " bps_max=%" PRId64
555                         " bps_rd_max=%" PRId64
556                         " bps_wr_max=%" PRId64
557                         " iops=%" PRId64 " iops_rd=%" PRId64
558                         " iops_wr=%" PRId64
559                         " iops_max=%" PRId64
560                         " iops_rd_max=%" PRId64
561                         " iops_wr_max=%" PRId64
562                         " iops_size=%" PRId64
563                         " group=%s\n",
564                         inserted->bps,
565                         inserted->bps_rd,
566                         inserted->bps_wr,
567                         inserted->bps_max,
568                         inserted->bps_rd_max,
569                         inserted->bps_wr_max,
570                         inserted->iops,
571                         inserted->iops_rd,
572                         inserted->iops_wr,
573                         inserted->iops_max,
574                         inserted->iops_rd_max,
575                         inserted->iops_wr_max,
576                         inserted->iops_size,
577                         inserted->group);
578     }
579 
580     if (verbose) {
581         monitor_printf(mon, "\nImages:\n");
582         image_info = inserted->image;
583         while (1) {
584                 bdrv_image_info_dump(image_info);
585             if (image_info->has_backing_image) {
586                 image_info = image_info->backing_image;
587             } else {
588                 break;
589             }
590         }
591     }
592 }
593 
594 void hmp_info_block(Monitor *mon, const QDict *qdict)
595 {
596     BlockInfoList *block_list, *info;
597     BlockDeviceInfoList *blockdev_list, *blockdev;
598     const char *device = qdict_get_try_str(qdict, "device");
599     bool verbose = qdict_get_try_bool(qdict, "verbose", false);
600     bool nodes = qdict_get_try_bool(qdict, "nodes", false);
601     bool printed = false;
602 
603     /* Print BlockBackend information */
604     if (!nodes) {
605         block_list = qmp_query_block(NULL);
606     } else {
607         block_list = NULL;
608     }
609 
610     for (info = block_list; info; info = info->next) {
611         if (device && strcmp(device, info->value->device)) {
612             continue;
613         }
614 
615         if (info != block_list) {
616             monitor_printf(mon, "\n");
617         }
618 
619         print_block_info(mon, info->value, info->value->has_inserted
620                                            ? info->value->inserted : NULL,
621                          verbose);
622         printed = true;
623     }
624 
625     qapi_free_BlockInfoList(block_list);
626 
627     if ((!device && !nodes) || printed) {
628         return;
629     }
630 
631     /* Print node information */
632     blockdev_list = qmp_query_named_block_nodes(NULL);
633     for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
634         assert(blockdev->value->has_node_name);
635         if (device && strcmp(device, blockdev->value->node_name)) {
636             continue;
637         }
638 
639         if (blockdev != blockdev_list) {
640             monitor_printf(mon, "\n");
641         }
642 
643         print_block_info(mon, NULL, blockdev->value, verbose);
644     }
645     qapi_free_BlockDeviceInfoList(blockdev_list);
646 }
647 
648 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
649 {
650     BlockStatsList *stats_list, *stats;
651 
652     stats_list = qmp_query_blockstats(false, false, NULL);
653 
654     for (stats = stats_list; stats; stats = stats->next) {
655         if (!stats->value->has_device) {
656             continue;
657         }
658 
659         monitor_printf(mon, "%s:", stats->value->device);
660         monitor_printf(mon, " rd_bytes=%" PRId64
661                        " wr_bytes=%" PRId64
662                        " rd_operations=%" PRId64
663                        " wr_operations=%" PRId64
664                        " flush_operations=%" PRId64
665                        " wr_total_time_ns=%" PRId64
666                        " rd_total_time_ns=%" PRId64
667                        " flush_total_time_ns=%" PRId64
668                        " rd_merged=%" PRId64
669                        " wr_merged=%" PRId64
670                        " idle_time_ns=%" PRId64
671                        "\n",
672                        stats->value->stats->rd_bytes,
673                        stats->value->stats->wr_bytes,
674                        stats->value->stats->rd_operations,
675                        stats->value->stats->wr_operations,
676                        stats->value->stats->flush_operations,
677                        stats->value->stats->wr_total_time_ns,
678                        stats->value->stats->rd_total_time_ns,
679                        stats->value->stats->flush_total_time_ns,
680                        stats->value->stats->rd_merged,
681                        stats->value->stats->wr_merged,
682                        stats->value->stats->idle_time_ns);
683     }
684 
685     qapi_free_BlockStatsList(stats_list);
686 }
687 
688 #ifdef CONFIG_VNC
689 /* Helper for hmp_info_vnc_clients, _servers */
690 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
691                                   const char *name)
692 {
693     monitor_printf(mon, "  %s: %s:%s (%s%s)\n",
694                    name,
695                    info->host,
696                    info->service,
697                    NetworkAddressFamily_str(info->family),
698                    info->websocket ? " (Websocket)" : "");
699 }
700 
701 /* Helper displaying and auth and crypt info */
702 static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
703                                    VncPrimaryAuth auth,
704                                    VncVencryptSubAuth *vencrypt)
705 {
706     monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
707                    VncPrimaryAuth_str(auth),
708                    vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
709 }
710 
711 static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
712 {
713     while (client) {
714         VncClientInfo *cinfo = client->value;
715 
716         hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
717         monitor_printf(mon, "    x509_dname: %s\n",
718                        cinfo->has_x509_dname ?
719                        cinfo->x509_dname : "none");
720         monitor_printf(mon, "    sasl_username: %s\n",
721                        cinfo->has_sasl_username ?
722                        cinfo->sasl_username : "none");
723 
724         client = client->next;
725     }
726 }
727 
728 static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
729 {
730     while (server) {
731         VncServerInfo2 *sinfo = server->value;
732         hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
733         hmp_info_vnc_authcrypt(mon, "    ", sinfo->auth,
734                                sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
735         server = server->next;
736     }
737 }
738 
739 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
740 {
741     VncInfo2List *info2l;
742     Error *err = NULL;
743 
744     info2l = qmp_query_vnc_servers(&err);
745     if (err) {
746         hmp_handle_error(mon, &err);
747         return;
748     }
749     if (!info2l) {
750         monitor_printf(mon, "None\n");
751         return;
752     }
753 
754     while (info2l) {
755         VncInfo2 *info = info2l->value;
756         monitor_printf(mon, "%s:\n", info->id);
757         hmp_info_vnc_servers(mon, info->server);
758         hmp_info_vnc_clients(mon, info->clients);
759         if (!info->server) {
760             /* The server entry displays its auth, we only
761              * need to display in the case of 'reverse' connections
762              * where there's no server.
763              */
764             hmp_info_vnc_authcrypt(mon, "  ", info->auth,
765                                info->has_vencrypt ? &info->vencrypt : NULL);
766         }
767         if (info->has_display) {
768             monitor_printf(mon, "  Display: %s\n", info->display);
769         }
770         info2l = info2l->next;
771     }
772 
773     qapi_free_VncInfo2List(info2l);
774 
775 }
776 #endif
777 
778 #ifdef CONFIG_SPICE
779 void hmp_info_spice(Monitor *mon, const QDict *qdict)
780 {
781     SpiceChannelList *chan;
782     SpiceInfo *info;
783     const char *channel_name;
784     const char * const channel_names[] = {
785         [SPICE_CHANNEL_MAIN] = "main",
786         [SPICE_CHANNEL_DISPLAY] = "display",
787         [SPICE_CHANNEL_INPUTS] = "inputs",
788         [SPICE_CHANNEL_CURSOR] = "cursor",
789         [SPICE_CHANNEL_PLAYBACK] = "playback",
790         [SPICE_CHANNEL_RECORD] = "record",
791         [SPICE_CHANNEL_TUNNEL] = "tunnel",
792         [SPICE_CHANNEL_SMARTCARD] = "smartcard",
793         [SPICE_CHANNEL_USBREDIR] = "usbredir",
794         [SPICE_CHANNEL_PORT] = "port",
795 #if 0
796         /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
797          * no easy way to #ifdef (SPICE_CHANNEL_* is a enum).  Disable
798          * as quick fix for build failures with older versions. */
799         [SPICE_CHANNEL_WEBDAV] = "webdav",
800 #endif
801     };
802 
803     info = qmp_query_spice(NULL);
804 
805     if (!info->enabled) {
806         monitor_printf(mon, "Server: disabled\n");
807         goto out;
808     }
809 
810     monitor_printf(mon, "Server:\n");
811     if (info->has_port) {
812         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
813                        info->host, info->port);
814     }
815     if (info->has_tls_port) {
816         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
817                        info->host, info->tls_port);
818     }
819     monitor_printf(mon, "    migrated: %s\n",
820                    info->migrated ? "true" : "false");
821     monitor_printf(mon, "        auth: %s\n", info->auth);
822     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
823     monitor_printf(mon, "  mouse-mode: %s\n",
824                    SpiceQueryMouseMode_str(info->mouse_mode));
825 
826     if (!info->has_channels || info->channels == NULL) {
827         monitor_printf(mon, "Channels: none\n");
828     } else {
829         for (chan = info->channels; chan; chan = chan->next) {
830             monitor_printf(mon, "Channel:\n");
831             monitor_printf(mon, "     address: %s:%s%s\n",
832                            chan->value->host, chan->value->port,
833                            chan->value->tls ? " [tls]" : "");
834             monitor_printf(mon, "     session: %" PRId64 "\n",
835                            chan->value->connection_id);
836             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
837                            chan->value->channel_type, chan->value->channel_id);
838 
839             channel_name = "unknown";
840             if (chan->value->channel_type > 0 &&
841                 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
842                 channel_names[chan->value->channel_type]) {
843                 channel_name = channel_names[chan->value->channel_type];
844             }
845 
846             monitor_printf(mon, "     channel name: %s\n", channel_name);
847         }
848     }
849 
850 out:
851     qapi_free_SpiceInfo(info);
852 }
853 #endif
854 
855 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
856 {
857     BalloonInfo *info;
858     Error *err = NULL;
859 
860     info = qmp_query_balloon(&err);
861     if (err) {
862         hmp_handle_error(mon, &err);
863         return;
864     }
865 
866     monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
867 
868     qapi_free_BalloonInfo(info);
869 }
870 
871 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
872 {
873     PciMemoryRegionList *region;
874 
875     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
876     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
877                    dev->slot, dev->function);
878     monitor_printf(mon, "    ");
879 
880     if (dev->class_info->has_desc) {
881         monitor_printf(mon, "%s", dev->class_info->desc);
882     } else {
883         monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
884     }
885 
886     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
887                    dev->id->vendor, dev->id->device);
888     if (dev->id->has_subsystem_vendor && dev->id->has_subsystem) {
889         monitor_printf(mon, "      PCI subsystem %04" PRIx64 ":%04" PRIx64 "\n",
890                        dev->id->subsystem_vendor, dev->id->subsystem);
891     }
892 
893     if (dev->has_irq) {
894         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
895     }
896 
897     if (dev->has_pci_bridge) {
898         monitor_printf(mon, "      BUS %" PRId64 ".\n",
899                        dev->pci_bridge->bus->number);
900         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
901                        dev->pci_bridge->bus->secondary);
902         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
903                        dev->pci_bridge->bus->subordinate);
904 
905         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
906                        dev->pci_bridge->bus->io_range->base,
907                        dev->pci_bridge->bus->io_range->limit);
908 
909         monitor_printf(mon,
910                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
911                        dev->pci_bridge->bus->memory_range->base,
912                        dev->pci_bridge->bus->memory_range->limit);
913 
914         monitor_printf(mon, "      prefetchable memory range "
915                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
916                        dev->pci_bridge->bus->prefetchable_range->base,
917                        dev->pci_bridge->bus->prefetchable_range->limit);
918     }
919 
920     for (region = dev->regions; region; region = region->next) {
921         uint64_t addr, size;
922 
923         addr = region->value->address;
924         size = region->value->size;
925 
926         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
927 
928         if (!strcmp(region->value->type, "io")) {
929             monitor_printf(mon, "I/O at 0x%04" PRIx64
930                                 " [0x%04" PRIx64 "].\n",
931                            addr, addr + size - 1);
932         } else {
933             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
934                                " [0x%08" PRIx64 "].\n",
935                            region->value->mem_type_64 ? 64 : 32,
936                            region->value->prefetch ? " prefetchable" : "",
937                            addr, addr + size - 1);
938         }
939     }
940 
941     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
942 
943     if (dev->has_pci_bridge) {
944         if (dev->pci_bridge->has_devices) {
945             PciDeviceInfoList *cdev;
946             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
947                 hmp_info_pci_device(mon, cdev->value);
948             }
949         }
950     }
951 }
952 
953 static int hmp_info_irq_foreach(Object *obj, void *opaque)
954 {
955     InterruptStatsProvider *intc;
956     InterruptStatsProviderClass *k;
957     Monitor *mon = opaque;
958 
959     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
960         intc = INTERRUPT_STATS_PROVIDER(obj);
961         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
962         uint64_t *irq_counts;
963         unsigned int nb_irqs, i;
964         if (k->get_statistics &&
965             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
966             if (nb_irqs > 0) {
967                 monitor_printf(mon, "IRQ statistics for %s:\n",
968                                object_get_typename(obj));
969                 for (i = 0; i < nb_irqs; i++) {
970                     if (irq_counts[i] > 0) {
971                         monitor_printf(mon, "%2d: %" PRId64 "\n", i,
972                                        irq_counts[i]);
973                     }
974                 }
975             }
976         } else {
977             monitor_printf(mon, "IRQ statistics not available for %s.\n",
978                            object_get_typename(obj));
979         }
980     }
981 
982     return 0;
983 }
984 
985 void hmp_info_irq(Monitor *mon, const QDict *qdict)
986 {
987     object_child_foreach_recursive(object_get_root(),
988                                    hmp_info_irq_foreach, mon);
989 }
990 
991 static int hmp_info_pic_foreach(Object *obj, void *opaque)
992 {
993     InterruptStatsProvider *intc;
994     InterruptStatsProviderClass *k;
995     Monitor *mon = opaque;
996 
997     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
998         intc = INTERRUPT_STATS_PROVIDER(obj);
999         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
1000         if (k->print_info) {
1001             k->print_info(intc, mon);
1002         } else {
1003             monitor_printf(mon, "Interrupt controller information not available for %s.\n",
1004                            object_get_typename(obj));
1005         }
1006     }
1007 
1008     return 0;
1009 }
1010 
1011 void hmp_info_pic(Monitor *mon, const QDict *qdict)
1012 {
1013     object_child_foreach_recursive(object_get_root(),
1014                                    hmp_info_pic_foreach, mon);
1015 }
1016 
1017 static int hmp_info_rdma_foreach(Object *obj, void *opaque)
1018 {
1019     RdmaProvider *rdma;
1020     RdmaProviderClass *k;
1021     Monitor *mon = opaque;
1022 
1023     if (object_dynamic_cast(obj, INTERFACE_RDMA_PROVIDER)) {
1024         rdma = RDMA_PROVIDER(obj);
1025         k = RDMA_PROVIDER_GET_CLASS(obj);
1026         if (k->print_statistics) {
1027             k->print_statistics(mon, rdma);
1028         } else {
1029             monitor_printf(mon, "RDMA statistics not available for %s.\n",
1030                            object_get_typename(obj));
1031         }
1032     }
1033 
1034     return 0;
1035 }
1036 
1037 void hmp_info_rdma(Monitor *mon, const QDict *qdict)
1038 {
1039     object_child_foreach_recursive(object_get_root(),
1040                                    hmp_info_rdma_foreach, mon);
1041 }
1042 
1043 void hmp_info_pci(Monitor *mon, const QDict *qdict)
1044 {
1045     PciInfoList *info_list, *info;
1046     Error *err = NULL;
1047 
1048     info_list = qmp_query_pci(&err);
1049     if (err) {
1050         monitor_printf(mon, "PCI devices not supported\n");
1051         error_free(err);
1052         return;
1053     }
1054 
1055     for (info = info_list; info; info = info->next) {
1056         PciDeviceInfoList *dev;
1057 
1058         for (dev = info->value->devices; dev; dev = dev->next) {
1059             hmp_info_pci_device(mon, dev->value);
1060         }
1061     }
1062 
1063     qapi_free_PciInfoList(info_list);
1064 }
1065 
1066 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
1067 {
1068     BlockJobInfoList *list;
1069     Error *err = NULL;
1070 
1071     list = qmp_query_block_jobs(&err);
1072     assert(!err);
1073 
1074     if (!list) {
1075         monitor_printf(mon, "No active jobs\n");
1076         return;
1077     }
1078 
1079     while (list) {
1080         if (strcmp(list->value->type, "stream") == 0) {
1081             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
1082                            " of %" PRId64 " bytes, speed limit %" PRId64
1083                            " bytes/s\n",
1084                            list->value->device,
1085                            list->value->offset,
1086                            list->value->len,
1087                            list->value->speed);
1088         } else {
1089             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
1090                            " of %" PRId64 " bytes, speed limit %" PRId64
1091                            " bytes/s\n",
1092                            list->value->type,
1093                            list->value->device,
1094                            list->value->offset,
1095                            list->value->len,
1096                            list->value->speed);
1097         }
1098         list = list->next;
1099     }
1100 
1101     qapi_free_BlockJobInfoList(list);
1102 }
1103 
1104 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
1105 {
1106     TPMInfoList *info_list, *info;
1107     Error *err = NULL;
1108     unsigned int c = 0;
1109     TPMPassthroughOptions *tpo;
1110     TPMEmulatorOptions *teo;
1111 
1112     info_list = qmp_query_tpm(&err);
1113     if (err) {
1114         monitor_printf(mon, "TPM device not supported\n");
1115         error_free(err);
1116         return;
1117     }
1118 
1119     if (info_list) {
1120         monitor_printf(mon, "TPM device:\n");
1121     }
1122 
1123     for (info = info_list; info; info = info->next) {
1124         TPMInfo *ti = info->value;
1125         monitor_printf(mon, " tpm%d: model=%s\n",
1126                        c, TpmModel_str(ti->model));
1127 
1128         monitor_printf(mon, "  \\ %s: type=%s",
1129                        ti->id, TpmTypeOptionsKind_str(ti->options->type));
1130 
1131         switch (ti->options->type) {
1132         case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
1133             tpo = ti->options->u.passthrough.data;
1134             monitor_printf(mon, "%s%s%s%s",
1135                            tpo->has_path ? ",path=" : "",
1136                            tpo->has_path ? tpo->path : "",
1137                            tpo->has_cancel_path ? ",cancel-path=" : "",
1138                            tpo->has_cancel_path ? tpo->cancel_path : "");
1139             break;
1140         case TPM_TYPE_OPTIONS_KIND_EMULATOR:
1141             teo = ti->options->u.emulator.data;
1142             monitor_printf(mon, ",chardev=%s", teo->chardev);
1143             break;
1144         case TPM_TYPE_OPTIONS_KIND__MAX:
1145             break;
1146         }
1147         monitor_printf(mon, "\n");
1148         c++;
1149     }
1150     qapi_free_TPMInfoList(info_list);
1151 }
1152 
1153 void hmp_quit(Monitor *mon, const QDict *qdict)
1154 {
1155     monitor_suspend(mon);
1156     qmp_quit(NULL);
1157 }
1158 
1159 void hmp_stop(Monitor *mon, const QDict *qdict)
1160 {
1161     qmp_stop(NULL);
1162 }
1163 
1164 void hmp_sync_profile(Monitor *mon, const QDict *qdict)
1165 {
1166     const char *op = qdict_get_try_str(qdict, "op");
1167 
1168     if (op == NULL) {
1169         bool on = qsp_is_enabled();
1170 
1171         monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
1172         return;
1173     }
1174     if (!strcmp(op, "on")) {
1175         qsp_enable();
1176     } else if (!strcmp(op, "off")) {
1177         qsp_disable();
1178     } else if (!strcmp(op, "reset")) {
1179         qsp_reset();
1180     } else {
1181         Error *err = NULL;
1182 
1183         error_setg(&err, QERR_INVALID_PARAMETER, op);
1184         hmp_handle_error(mon, &err);
1185     }
1186 }
1187 
1188 void hmp_system_reset(Monitor *mon, const QDict *qdict)
1189 {
1190     qmp_system_reset(NULL);
1191 }
1192 
1193 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1194 {
1195     qmp_system_powerdown(NULL);
1196 }
1197 
1198 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
1199 {
1200     Error *err = NULL;
1201 
1202     qmp_x_exit_preconfig(&err);
1203     hmp_handle_error(mon, &err);
1204 }
1205 
1206 void hmp_cpu(Monitor *mon, const QDict *qdict)
1207 {
1208     int64_t cpu_index;
1209 
1210     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1211             use it are converted to the QAPI */
1212     cpu_index = qdict_get_int(qdict, "index");
1213     if (monitor_set_cpu(cpu_index) < 0) {
1214         monitor_printf(mon, "invalid CPU index\n");
1215     }
1216 }
1217 
1218 void hmp_memsave(Monitor *mon, const QDict *qdict)
1219 {
1220     uint32_t size = qdict_get_int(qdict, "size");
1221     const char *filename = qdict_get_str(qdict, "filename");
1222     uint64_t addr = qdict_get_int(qdict, "val");
1223     Error *err = NULL;
1224     int cpu_index = monitor_get_cpu_index();
1225 
1226     if (cpu_index < 0) {
1227         monitor_printf(mon, "No CPU available\n");
1228         return;
1229     }
1230 
1231     qmp_memsave(addr, size, filename, true, cpu_index, &err);
1232     hmp_handle_error(mon, &err);
1233 }
1234 
1235 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1236 {
1237     uint32_t size = qdict_get_int(qdict, "size");
1238     const char *filename = qdict_get_str(qdict, "filename");
1239     uint64_t addr = qdict_get_int(qdict, "val");
1240     Error *err = NULL;
1241 
1242     qmp_pmemsave(addr, size, filename, &err);
1243     hmp_handle_error(mon, &err);
1244 }
1245 
1246 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1247 {
1248     const char *chardev = qdict_get_str(qdict, "device");
1249     const char *data = qdict_get_str(qdict, "data");
1250     Error *err = NULL;
1251 
1252     qmp_ringbuf_write(chardev, data, false, 0, &err);
1253 
1254     hmp_handle_error(mon, &err);
1255 }
1256 
1257 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1258 {
1259     uint32_t size = qdict_get_int(qdict, "size");
1260     const char *chardev = qdict_get_str(qdict, "device");
1261     char *data;
1262     Error *err = NULL;
1263     int i;
1264 
1265     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1266     if (err) {
1267         hmp_handle_error(mon, &err);
1268         return;
1269     }
1270 
1271     for (i = 0; data[i]; i++) {
1272         unsigned char ch = data[i];
1273 
1274         if (ch == '\\') {
1275             monitor_printf(mon, "\\\\");
1276         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1277             monitor_printf(mon, "\\u%04X", ch);
1278         } else {
1279             monitor_printf(mon, "%c", ch);
1280         }
1281 
1282     }
1283     monitor_printf(mon, "\n");
1284     g_free(data);
1285 }
1286 
1287 void hmp_cont(Monitor *mon, const QDict *qdict)
1288 {
1289     Error *err = NULL;
1290 
1291     qmp_cont(&err);
1292     hmp_handle_error(mon, &err);
1293 }
1294 
1295 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1296 {
1297     Error *err = NULL;
1298 
1299     qmp_system_wakeup(&err);
1300     hmp_handle_error(mon, &err);
1301 }
1302 
1303 void hmp_nmi(Monitor *mon, const QDict *qdict)
1304 {
1305     Error *err = NULL;
1306 
1307     qmp_inject_nmi(&err);
1308     hmp_handle_error(mon, &err);
1309 }
1310 
1311 void hmp_set_link(Monitor *mon, const QDict *qdict)
1312 {
1313     const char *name = qdict_get_str(qdict, "name");
1314     bool up = qdict_get_bool(qdict, "up");
1315     Error *err = NULL;
1316 
1317     qmp_set_link(name, up, &err);
1318     hmp_handle_error(mon, &err);
1319 }
1320 
1321 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1322 {
1323     const char *device = qdict_get_str(qdict, "device");
1324     const char *password = qdict_get_str(qdict, "password");
1325     Error *err = NULL;
1326 
1327     qmp_block_passwd(true, device, false, NULL, password, &err);
1328     hmp_handle_error(mon, &err);
1329 }
1330 
1331 void hmp_balloon(Monitor *mon, const QDict *qdict)
1332 {
1333     int64_t value = qdict_get_int(qdict, "value");
1334     Error *err = NULL;
1335 
1336     qmp_balloon(value, &err);
1337     hmp_handle_error(mon, &err);
1338 }
1339 
1340 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1341 {
1342     const char *device = qdict_get_str(qdict, "device");
1343     int64_t size = qdict_get_int(qdict, "size");
1344     Error *err = NULL;
1345 
1346     qmp_block_resize(true, device, false, NULL, size, &err);
1347     hmp_handle_error(mon, &err);
1348 }
1349 
1350 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1351 {
1352     const char *filename = qdict_get_str(qdict, "target");
1353     const char *format = qdict_get_try_str(qdict, "format");
1354     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1355     bool full = qdict_get_try_bool(qdict, "full", false);
1356     Error *err = NULL;
1357     DriveMirror mirror = {
1358         .device = (char *)qdict_get_str(qdict, "device"),
1359         .target = (char *)filename,
1360         .has_format = !!format,
1361         .format = (char *)format,
1362         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1363         .has_mode = true,
1364         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1365         .unmap = true,
1366     };
1367 
1368     if (!filename) {
1369         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1370         hmp_handle_error(mon, &err);
1371         return;
1372     }
1373     qmp_drive_mirror(&mirror, &err);
1374     hmp_handle_error(mon, &err);
1375 }
1376 
1377 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1378 {
1379     const char *device = qdict_get_str(qdict, "device");
1380     const char *filename = qdict_get_str(qdict, "target");
1381     const char *format = qdict_get_try_str(qdict, "format");
1382     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1383     bool full = qdict_get_try_bool(qdict, "full", false);
1384     bool compress = qdict_get_try_bool(qdict, "compress", false);
1385     Error *err = NULL;
1386     DriveBackup backup = {
1387         .device = (char *)device,
1388         .target = (char *)filename,
1389         .has_format = !!format,
1390         .format = (char *)format,
1391         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1392         .has_mode = true,
1393         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1394         .has_compress = !!compress,
1395         .compress = compress,
1396     };
1397 
1398     if (!filename) {
1399         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1400         hmp_handle_error(mon, &err);
1401         return;
1402     }
1403 
1404     qmp_drive_backup(&backup, &err);
1405     hmp_handle_error(mon, &err);
1406 }
1407 
1408 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1409 {
1410     const char *device = qdict_get_str(qdict, "device");
1411     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1412     const char *format = qdict_get_try_str(qdict, "format");
1413     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1414     enum NewImageMode mode;
1415     Error *err = NULL;
1416 
1417     if (!filename) {
1418         /* In the future, if 'snapshot-file' is not specified, the snapshot
1419            will be taken internally. Today it's actually required. */
1420         error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1421         hmp_handle_error(mon, &err);
1422         return;
1423     }
1424 
1425     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1426     qmp_blockdev_snapshot_sync(true, device, false, NULL,
1427                                filename, false, NULL,
1428                                !!format, format,
1429                                true, mode, &err);
1430     hmp_handle_error(mon, &err);
1431 }
1432 
1433 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1434 {
1435     const char *device = qdict_get_str(qdict, "device");
1436     const char *name = qdict_get_str(qdict, "name");
1437     Error *err = NULL;
1438 
1439     qmp_blockdev_snapshot_internal_sync(device, name, &err);
1440     hmp_handle_error(mon, &err);
1441 }
1442 
1443 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1444 {
1445     const char *device = qdict_get_str(qdict, "device");
1446     const char *name = qdict_get_str(qdict, "name");
1447     const char *id = qdict_get_try_str(qdict, "id");
1448     Error *err = NULL;
1449 
1450     qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1451                                                true, name, &err);
1452     hmp_handle_error(mon, &err);
1453 }
1454 
1455 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1456 {
1457     int saved_vm_running  = runstate_is_running();
1458     const char *name = qdict_get_str(qdict, "name");
1459     Error *err = NULL;
1460 
1461     vm_stop(RUN_STATE_RESTORE_VM);
1462 
1463     if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1464         vm_start();
1465     }
1466     hmp_handle_error(mon, &err);
1467 }
1468 
1469 void hmp_savevm(Monitor *mon, const QDict *qdict)
1470 {
1471     Error *err = NULL;
1472 
1473     save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1474     hmp_handle_error(mon, &err);
1475 }
1476 
1477 void hmp_delvm(Monitor *mon, const QDict *qdict)
1478 {
1479     BlockDriverState *bs;
1480     Error *err = NULL;
1481     const char *name = qdict_get_str(qdict, "name");
1482 
1483     if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1484         error_prepend(&err,
1485                       "deleting snapshot on device '%s': ",
1486                       bdrv_get_device_name(bs));
1487     }
1488     hmp_handle_error(mon, &err);
1489 }
1490 
1491 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1492 {
1493     BlockDriverState *bs, *bs1;
1494     BdrvNextIterator it1;
1495     QEMUSnapshotInfo *sn_tab, *sn;
1496     bool no_snapshot = true;
1497     int nb_sns, i;
1498     int total;
1499     int *global_snapshots;
1500     AioContext *aio_context;
1501 
1502     typedef struct SnapshotEntry {
1503         QEMUSnapshotInfo sn;
1504         QTAILQ_ENTRY(SnapshotEntry) next;
1505     } SnapshotEntry;
1506 
1507     typedef struct ImageEntry {
1508         const char *imagename;
1509         QTAILQ_ENTRY(ImageEntry) next;
1510         QTAILQ_HEAD(, SnapshotEntry) snapshots;
1511     } ImageEntry;
1512 
1513     QTAILQ_HEAD(, ImageEntry) image_list =
1514         QTAILQ_HEAD_INITIALIZER(image_list);
1515 
1516     ImageEntry *image_entry, *next_ie;
1517     SnapshotEntry *snapshot_entry;
1518 
1519     bs = bdrv_all_find_vmstate_bs();
1520     if (!bs) {
1521         monitor_printf(mon, "No available block device supports snapshots\n");
1522         return;
1523     }
1524     aio_context = bdrv_get_aio_context(bs);
1525 
1526     aio_context_acquire(aio_context);
1527     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1528     aio_context_release(aio_context);
1529 
1530     if (nb_sns < 0) {
1531         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1532         return;
1533     }
1534 
1535     for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1536         int bs1_nb_sns = 0;
1537         ImageEntry *ie;
1538         SnapshotEntry *se;
1539         AioContext *ctx = bdrv_get_aio_context(bs1);
1540 
1541         aio_context_acquire(ctx);
1542         if (bdrv_can_snapshot(bs1)) {
1543             sn = NULL;
1544             bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1545             if (bs1_nb_sns > 0) {
1546                 no_snapshot = false;
1547                 ie = g_new0(ImageEntry, 1);
1548                 ie->imagename = bdrv_get_device_name(bs1);
1549                 QTAILQ_INIT(&ie->snapshots);
1550                 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1551                 for (i = 0; i < bs1_nb_sns; i++) {
1552                     se = g_new0(SnapshotEntry, 1);
1553                     se->sn = sn[i];
1554                     QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1555                 }
1556             }
1557             g_free(sn);
1558         }
1559         aio_context_release(ctx);
1560     }
1561 
1562     if (no_snapshot) {
1563         monitor_printf(mon, "There is no snapshot available.\n");
1564         return;
1565     }
1566 
1567     global_snapshots = g_new0(int, nb_sns);
1568     total = 0;
1569     for (i = 0; i < nb_sns; i++) {
1570         SnapshotEntry *next_sn;
1571         if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1572             global_snapshots[total] = i;
1573             total++;
1574             QTAILQ_FOREACH(image_entry, &image_list, next) {
1575                 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1576                                     next, next_sn) {
1577                     if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1578                         QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1579                                       next);
1580                         g_free(snapshot_entry);
1581                     }
1582                 }
1583             }
1584         }
1585     }
1586 
1587     monitor_printf(mon, "List of snapshots present on all disks:\n");
1588 
1589     if (total > 0) {
1590         bdrv_snapshot_dump(NULL);
1591         monitor_printf(mon, "\n");
1592         for (i = 0; i < total; i++) {
1593             sn = &sn_tab[global_snapshots[i]];
1594             /* The ID is not guaranteed to be the same on all images, so
1595              * overwrite it.
1596              */
1597             pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1598             bdrv_snapshot_dump(sn);
1599             monitor_printf(mon, "\n");
1600         }
1601     } else {
1602         monitor_printf(mon, "None\n");
1603     }
1604 
1605     QTAILQ_FOREACH(image_entry, &image_list, next) {
1606         if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1607             continue;
1608         }
1609         monitor_printf(mon,
1610                        "\nList of partial (non-loadable) snapshots on '%s':\n",
1611                        image_entry->imagename);
1612         bdrv_snapshot_dump(NULL);
1613         monitor_printf(mon, "\n");
1614         QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1615             bdrv_snapshot_dump(&snapshot_entry->sn);
1616             monitor_printf(mon, "\n");
1617         }
1618     }
1619 
1620     QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1621         SnapshotEntry *next_sn;
1622         QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1623                             next_sn) {
1624             g_free(snapshot_entry);
1625         }
1626         g_free(image_entry);
1627     }
1628     g_free(sn_tab);
1629     g_free(global_snapshots);
1630 
1631 }
1632 
1633 void hmp_announce_self(Monitor *mon, const QDict *qdict)
1634 {
1635     qmp_announce_self(migrate_announce_params(), NULL);
1636 }
1637 
1638 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1639 {
1640     qmp_migrate_cancel(NULL);
1641 }
1642 
1643 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
1644 {
1645     Error *err = NULL;
1646     const char *state = qdict_get_str(qdict, "state");
1647     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
1648 
1649     if (val >= 0) {
1650         qmp_migrate_continue(val, &err);
1651     }
1652 
1653     hmp_handle_error(mon, &err);
1654 }
1655 
1656 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1657 {
1658     Error *err = NULL;
1659     const char *uri = qdict_get_str(qdict, "uri");
1660 
1661     qmp_migrate_incoming(uri, &err);
1662 
1663     hmp_handle_error(mon, &err);
1664 }
1665 
1666 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
1667 {
1668     Error *err = NULL;
1669     const char *uri = qdict_get_str(qdict, "uri");
1670 
1671     qmp_migrate_recover(uri, &err);
1672 
1673     hmp_handle_error(mon, &err);
1674 }
1675 
1676 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
1677 {
1678     Error *err = NULL;
1679 
1680     qmp_migrate_pause(&err);
1681 
1682     hmp_handle_error(mon, &err);
1683 }
1684 
1685 /* Kept for backwards compatibility */
1686 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1687 {
1688     double value = qdict_get_double(qdict, "value");
1689     qmp_migrate_set_downtime(value, NULL);
1690 }
1691 
1692 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1693 {
1694     int64_t value = qdict_get_int(qdict, "value");
1695     Error *err = NULL;
1696 
1697     qmp_migrate_set_cache_size(value, &err);
1698     hmp_handle_error(mon, &err);
1699 }
1700 
1701 /* Kept for backwards compatibility */
1702 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1703 {
1704     int64_t value = qdict_get_int(qdict, "value");
1705     qmp_migrate_set_speed(value, NULL);
1706 }
1707 
1708 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1709 {
1710     const char *cap = qdict_get_str(qdict, "capability");
1711     bool state = qdict_get_bool(qdict, "state");
1712     Error *err = NULL;
1713     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1714     int val;
1715 
1716     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
1717     if (val < 0) {
1718         goto end;
1719     }
1720 
1721     caps->value = g_malloc0(sizeof(*caps->value));
1722     caps->value->capability = val;
1723     caps->value->state = state;
1724     caps->next = NULL;
1725     qmp_migrate_set_capabilities(caps, &err);
1726 
1727 end:
1728     qapi_free_MigrationCapabilityStatusList(caps);
1729     hmp_handle_error(mon, &err);
1730 }
1731 
1732 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1733 {
1734     const char *param = qdict_get_str(qdict, "parameter");
1735     const char *valuestr = qdict_get_str(qdict, "value");
1736     Visitor *v = string_input_visitor_new(valuestr);
1737     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
1738     uint64_t valuebw = 0;
1739     uint64_t cache_size;
1740     Error *err = NULL;
1741     int val, ret;
1742 
1743     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
1744     if (val < 0) {
1745         goto cleanup;
1746     }
1747 
1748     switch (val) {
1749     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1750         p->has_compress_level = true;
1751         visit_type_int(v, param, &p->compress_level, &err);
1752         break;
1753     case MIGRATION_PARAMETER_COMPRESS_THREADS:
1754         p->has_compress_threads = true;
1755         visit_type_int(v, param, &p->compress_threads, &err);
1756         break;
1757     case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
1758         p->has_compress_wait_thread = true;
1759         visit_type_bool(v, param, &p->compress_wait_thread, &err);
1760         break;
1761     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1762         p->has_decompress_threads = true;
1763         visit_type_int(v, param, &p->decompress_threads, &err);
1764         break;
1765     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1766         p->has_cpu_throttle_initial = true;
1767         visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1768         break;
1769     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1770         p->has_cpu_throttle_increment = true;
1771         visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1772         break;
1773     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
1774         p->has_max_cpu_throttle = true;
1775         visit_type_int(v, param, &p->max_cpu_throttle, &err);
1776         break;
1777     case MIGRATION_PARAMETER_TLS_CREDS:
1778         p->has_tls_creds = true;
1779         p->tls_creds = g_new0(StrOrNull, 1);
1780         p->tls_creds->type = QTYPE_QSTRING;
1781         visit_type_str(v, param, &p->tls_creds->u.s, &err);
1782         break;
1783     case MIGRATION_PARAMETER_TLS_HOSTNAME:
1784         p->has_tls_hostname = true;
1785         p->tls_hostname = g_new0(StrOrNull, 1);
1786         p->tls_hostname->type = QTYPE_QSTRING;
1787         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
1788         break;
1789     case MIGRATION_PARAMETER_TLS_AUTHZ:
1790         p->has_tls_authz = true;
1791         p->tls_authz = g_new0(StrOrNull, 1);
1792         p->tls_authz->type = QTYPE_QSTRING;
1793         visit_type_str(v, param, &p->tls_authz->u.s, &err);
1794         break;
1795     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1796         p->has_max_bandwidth = true;
1797         /*
1798          * Can't use visit_type_size() here, because it
1799          * defaults to Bytes rather than Mebibytes.
1800          */
1801         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1802         if (ret < 0 || valuebw > INT64_MAX
1803             || (size_t)valuebw != valuebw) {
1804             error_setg(&err, "Invalid size %s", valuestr);
1805             break;
1806         }
1807         p->max_bandwidth = valuebw;
1808         break;
1809     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1810         p->has_downtime_limit = true;
1811         visit_type_int(v, param, &p->downtime_limit, &err);
1812         break;
1813     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1814         p->has_x_checkpoint_delay = true;
1815         visit_type_int(v, param, &p->x_checkpoint_delay, &err);
1816         break;
1817     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1818         p->has_block_incremental = true;
1819         visit_type_bool(v, param, &p->block_incremental, &err);
1820         break;
1821     case MIGRATION_PARAMETER_MULTIFD_CHANNELS:
1822         p->has_multifd_channels = true;
1823         visit_type_int(v, param, &p->multifd_channels, &err);
1824         break;
1825     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
1826         p->has_xbzrle_cache_size = true;
1827         visit_type_size(v, param, &cache_size, &err);
1828         if (err) {
1829             break;
1830         }
1831         if (cache_size > INT64_MAX || (size_t)cache_size != cache_size) {
1832             error_setg(&err, "Invalid size %s", valuestr);
1833             break;
1834         }
1835         p->xbzrle_cache_size = cache_size;
1836         break;
1837     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
1838         p->has_max_postcopy_bandwidth = true;
1839         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
1840         break;
1841     case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
1842         p->has_announce_initial = true;
1843         visit_type_size(v, param, &p->announce_initial, &err);
1844         break;
1845     case MIGRATION_PARAMETER_ANNOUNCE_MAX:
1846         p->has_announce_max = true;
1847         visit_type_size(v, param, &p->announce_max, &err);
1848         break;
1849     case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
1850         p->has_announce_rounds = true;
1851         visit_type_size(v, param, &p->announce_rounds, &err);
1852         break;
1853     case MIGRATION_PARAMETER_ANNOUNCE_STEP:
1854         p->has_announce_step = true;
1855         visit_type_size(v, param, &p->announce_step, &err);
1856         break;
1857     default:
1858         assert(0);
1859     }
1860 
1861     if (err) {
1862         goto cleanup;
1863     }
1864 
1865     qmp_migrate_set_parameters(p, &err);
1866 
1867  cleanup:
1868     qapi_free_MigrateSetParameters(p);
1869     visit_free(v);
1870     hmp_handle_error(mon, &err);
1871 }
1872 
1873 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1874 {
1875     Error *err = NULL;
1876     const char *protocol = qdict_get_str(qdict, "protocol");
1877     const char *hostname = qdict_get_str(qdict, "hostname");
1878     bool has_port        = qdict_haskey(qdict, "port");
1879     int port             = qdict_get_try_int(qdict, "port", -1);
1880     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1881     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1882     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1883 
1884     qmp_client_migrate_info(protocol, hostname,
1885                             has_port, port, has_tls_port, tls_port,
1886                             !!cert_subject, cert_subject, &err);
1887     hmp_handle_error(mon, &err);
1888 }
1889 
1890 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1891 {
1892     Error *err = NULL;
1893     qmp_migrate_start_postcopy(&err);
1894     hmp_handle_error(mon, &err);
1895 }
1896 
1897 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1898 {
1899     Error *err = NULL;
1900 
1901     qmp_x_colo_lost_heartbeat(&err);
1902     hmp_handle_error(mon, &err);
1903 }
1904 
1905 void hmp_set_password(Monitor *mon, const QDict *qdict)
1906 {
1907     const char *protocol  = qdict_get_str(qdict, "protocol");
1908     const char *password  = qdict_get_str(qdict, "password");
1909     const char *connected = qdict_get_try_str(qdict, "connected");
1910     Error *err = NULL;
1911 
1912     qmp_set_password(protocol, password, !!connected, connected, &err);
1913     hmp_handle_error(mon, &err);
1914 }
1915 
1916 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1917 {
1918     const char *protocol  = qdict_get_str(qdict, "protocol");
1919     const char *whenstr = qdict_get_str(qdict, "time");
1920     Error *err = NULL;
1921 
1922     qmp_expire_password(protocol, whenstr, &err);
1923     hmp_handle_error(mon, &err);
1924 }
1925 
1926 void hmp_eject(Monitor *mon, const QDict *qdict)
1927 {
1928     bool force = qdict_get_try_bool(qdict, "force", false);
1929     const char *device = qdict_get_str(qdict, "device");
1930     Error *err = NULL;
1931 
1932     qmp_eject(true, device, false, NULL, true, force, &err);
1933     hmp_handle_error(mon, &err);
1934 }
1935 
1936 #ifdef CONFIG_VNC
1937 static void hmp_change_read_arg(void *opaque, const char *password,
1938                                 void *readline_opaque)
1939 {
1940     qmp_change_vnc_password(password, NULL);
1941     monitor_read_command(opaque, 1);
1942 }
1943 #endif
1944 
1945 void hmp_change(Monitor *mon, const QDict *qdict)
1946 {
1947     MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
1948     const char *device = qdict_get_str(qdict, "device");
1949     const char *target = qdict_get_str(qdict, "target");
1950     const char *arg = qdict_get_try_str(qdict, "arg");
1951     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1952     BlockdevChangeReadOnlyMode read_only_mode = 0;
1953     Error *err = NULL;
1954 
1955 #ifdef CONFIG_VNC
1956     if (strcmp(device, "vnc") == 0) {
1957         if (read_only) {
1958             monitor_printf(mon,
1959                            "Parameter 'read-only-mode' is invalid for VNC\n");
1960             return;
1961         }
1962         if (strcmp(target, "passwd") == 0 ||
1963             strcmp(target, "password") == 0) {
1964             if (!arg) {
1965                 monitor_read_password(hmp_mon, hmp_change_read_arg, NULL);
1966                 return;
1967             }
1968         }
1969         qmp_change("vnc", target, !!arg, arg, &err);
1970     } else
1971 #endif
1972     {
1973         if (read_only) {
1974             read_only_mode =
1975                 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1976                                 read_only,
1977                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1978             if (err) {
1979                 hmp_handle_error(mon, &err);
1980                 return;
1981             }
1982         }
1983 
1984         qmp_blockdev_change_medium(true, device, false, NULL, target,
1985                                    !!arg, arg, !!read_only, read_only_mode,
1986                                    &err);
1987     }
1988 
1989     hmp_handle_error(mon, &err);
1990 }
1991 
1992 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1993 {
1994     Error *err = NULL;
1995     char *device = (char *) qdict_get_str(qdict, "device");
1996     BlockIOThrottle throttle = {
1997         .bps = qdict_get_int(qdict, "bps"),
1998         .bps_rd = qdict_get_int(qdict, "bps_rd"),
1999         .bps_wr = qdict_get_int(qdict, "bps_wr"),
2000         .iops = qdict_get_int(qdict, "iops"),
2001         .iops_rd = qdict_get_int(qdict, "iops_rd"),
2002         .iops_wr = qdict_get_int(qdict, "iops_wr"),
2003     };
2004 
2005     /* qmp_block_set_io_throttle has separate parameters for the
2006      * (deprecated) block device name and the qdev ID but the HMP
2007      * version has only one, so we must decide which one to pass. */
2008     if (blk_by_name(device)) {
2009         throttle.has_device = true;
2010         throttle.device = device;
2011     } else {
2012         throttle.has_id = true;
2013         throttle.id = device;
2014     }
2015 
2016     qmp_block_set_io_throttle(&throttle, &err);
2017     hmp_handle_error(mon, &err);
2018 }
2019 
2020 void hmp_block_stream(Monitor *mon, const QDict *qdict)
2021 {
2022     Error *error = NULL;
2023     const char *device = qdict_get_str(qdict, "device");
2024     const char *base = qdict_get_try_str(qdict, "base");
2025     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
2026 
2027     qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
2028                      false, NULL, qdict_haskey(qdict, "speed"), speed, true,
2029                      BLOCKDEV_ON_ERROR_REPORT, false, false, false, false,
2030                      &error);
2031 
2032     hmp_handle_error(mon, &error);
2033 }
2034 
2035 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
2036 {
2037     Error *error = NULL;
2038     const char *device = qdict_get_str(qdict, "device");
2039     int64_t value = qdict_get_int(qdict, "speed");
2040 
2041     qmp_block_job_set_speed(device, value, &error);
2042 
2043     hmp_handle_error(mon, &error);
2044 }
2045 
2046 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
2047 {
2048     Error *error = NULL;
2049     const char *device = qdict_get_str(qdict, "device");
2050     bool force = qdict_get_try_bool(qdict, "force", false);
2051 
2052     qmp_block_job_cancel(device, true, force, &error);
2053 
2054     hmp_handle_error(mon, &error);
2055 }
2056 
2057 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
2058 {
2059     Error *error = NULL;
2060     const char *device = qdict_get_str(qdict, "device");
2061 
2062     qmp_block_job_pause(device, &error);
2063 
2064     hmp_handle_error(mon, &error);
2065 }
2066 
2067 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
2068 {
2069     Error *error = NULL;
2070     const char *device = qdict_get_str(qdict, "device");
2071 
2072     qmp_block_job_resume(device, &error);
2073 
2074     hmp_handle_error(mon, &error);
2075 }
2076 
2077 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
2078 {
2079     Error *error = NULL;
2080     const char *device = qdict_get_str(qdict, "device");
2081 
2082     qmp_block_job_complete(device, &error);
2083 
2084     hmp_handle_error(mon, &error);
2085 }
2086 
2087 typedef struct HMPMigrationStatus
2088 {
2089     QEMUTimer *timer;
2090     Monitor *mon;
2091     bool is_block_migration;
2092 } HMPMigrationStatus;
2093 
2094 static void hmp_migrate_status_cb(void *opaque)
2095 {
2096     HMPMigrationStatus *status = opaque;
2097     MigrationInfo *info;
2098 
2099     info = qmp_query_migrate(NULL);
2100     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
2101         info->status == MIGRATION_STATUS_SETUP) {
2102         if (info->has_disk) {
2103             int progress;
2104 
2105             if (info->disk->remaining) {
2106                 progress = info->disk->transferred * 100 / info->disk->total;
2107             } else {
2108                 progress = 100;
2109             }
2110 
2111             monitor_printf(status->mon, "Completed %d %%\r", progress);
2112             monitor_flush(status->mon);
2113         }
2114 
2115         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
2116     } else {
2117         if (status->is_block_migration) {
2118             monitor_printf(status->mon, "\n");
2119         }
2120         if (info->has_error_desc) {
2121             error_report("%s", info->error_desc);
2122         }
2123         monitor_resume(status->mon);
2124         timer_del(status->timer);
2125         timer_free(status->timer);
2126         g_free(status);
2127     }
2128 
2129     qapi_free_MigrationInfo(info);
2130 }
2131 
2132 void hmp_migrate(Monitor *mon, const QDict *qdict)
2133 {
2134     bool detach = qdict_get_try_bool(qdict, "detach", false);
2135     bool blk = qdict_get_try_bool(qdict, "blk", false);
2136     bool inc = qdict_get_try_bool(qdict, "inc", false);
2137     bool resume = qdict_get_try_bool(qdict, "resume", false);
2138     const char *uri = qdict_get_str(qdict, "uri");
2139     Error *err = NULL;
2140 
2141     qmp_migrate(uri, !!blk, blk, !!inc, inc,
2142                 false, false, true, resume, &err);
2143     if (err) {
2144         hmp_handle_error(mon, &err);
2145         return;
2146     }
2147 
2148     if (!detach) {
2149         HMPMigrationStatus *status;
2150 
2151         if (monitor_suspend(mon) < 0) {
2152             monitor_printf(mon, "terminal does not allow synchronous "
2153                            "migration, continuing detached\n");
2154             return;
2155         }
2156 
2157         status = g_malloc0(sizeof(*status));
2158         status->mon = mon;
2159         status->is_block_migration = blk || inc;
2160         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
2161                                           status);
2162         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
2163     }
2164 }
2165 
2166 void hmp_device_add(Monitor *mon, const QDict *qdict)
2167 {
2168     Error *err = NULL;
2169 
2170     qmp_device_add((QDict *)qdict, NULL, &err);
2171     hmp_handle_error(mon, &err);
2172 }
2173 
2174 void hmp_device_del(Monitor *mon, const QDict *qdict)
2175 {
2176     const char *id = qdict_get_str(qdict, "id");
2177     Error *err = NULL;
2178 
2179     qmp_device_del(id, &err);
2180     hmp_handle_error(mon, &err);
2181 }
2182 
2183 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
2184 {
2185     Error *err = NULL;
2186     bool win_dmp = qdict_get_try_bool(qdict, "windmp", false);
2187     bool paging = qdict_get_try_bool(qdict, "paging", false);
2188     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
2189     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
2190     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
2191     const char *file = qdict_get_str(qdict, "filename");
2192     bool has_begin = qdict_haskey(qdict, "begin");
2193     bool has_length = qdict_haskey(qdict, "length");
2194     bool has_detach = qdict_haskey(qdict, "detach");
2195     int64_t begin = 0;
2196     int64_t length = 0;
2197     bool detach = false;
2198     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
2199     char *prot;
2200 
2201     if (zlib + lzo + snappy + win_dmp > 1) {
2202         error_setg(&err, "only one of '-z|-l|-s|-w' can be set");
2203         hmp_handle_error(mon, &err);
2204         return;
2205     }
2206 
2207     if (win_dmp) {
2208         dump_format = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
2209     }
2210 
2211     if (zlib) {
2212         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2213     }
2214 
2215     if (lzo) {
2216         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2217     }
2218 
2219     if (snappy) {
2220         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2221     }
2222 
2223     if (has_begin) {
2224         begin = qdict_get_int(qdict, "begin");
2225     }
2226     if (has_length) {
2227         length = qdict_get_int(qdict, "length");
2228     }
2229     if (has_detach) {
2230         detach = qdict_get_bool(qdict, "detach");
2231     }
2232 
2233     prot = g_strconcat("file:", file, NULL);
2234 
2235     qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
2236                           has_length, length, true, dump_format, &err);
2237     hmp_handle_error(mon, &err);
2238     g_free(prot);
2239 }
2240 
2241 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
2242 {
2243     Error *err = NULL;
2244     QemuOpts *opts;
2245 
2246     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
2247     if (err) {
2248         goto out;
2249     }
2250 
2251     netdev_add(opts, &err);
2252     if (err) {
2253         qemu_opts_del(opts);
2254     }
2255 
2256 out:
2257     hmp_handle_error(mon, &err);
2258 }
2259 
2260 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2261 {
2262     const char *id = qdict_get_str(qdict, "id");
2263     Error *err = NULL;
2264 
2265     qmp_netdev_del(id, &err);
2266     hmp_handle_error(mon, &err);
2267 }
2268 
2269 void hmp_object_add(Monitor *mon, const QDict *qdict)
2270 {
2271     Error *err = NULL;
2272     QemuOpts *opts;
2273     Object *obj = NULL;
2274 
2275     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2276     if (err) {
2277         hmp_handle_error(mon, &err);
2278         return;
2279     }
2280 
2281     obj = user_creatable_add_opts(opts, &err);
2282     qemu_opts_del(opts);
2283 
2284     if (err) {
2285         hmp_handle_error(mon, &err);
2286     }
2287     if (obj) {
2288         object_unref(obj);
2289     }
2290 }
2291 
2292 void hmp_getfd(Monitor *mon, const QDict *qdict)
2293 {
2294     const char *fdname = qdict_get_str(qdict, "fdname");
2295     Error *err = NULL;
2296 
2297     qmp_getfd(fdname, &err);
2298     hmp_handle_error(mon, &err);
2299 }
2300 
2301 void hmp_closefd(Monitor *mon, const QDict *qdict)
2302 {
2303     const char *fdname = qdict_get_str(qdict, "fdname");
2304     Error *err = NULL;
2305 
2306     qmp_closefd(fdname, &err);
2307     hmp_handle_error(mon, &err);
2308 }
2309 
2310 void hmp_sendkey(Monitor *mon, const QDict *qdict)
2311 {
2312     const char *keys = qdict_get_str(qdict, "keys");
2313     KeyValueList *keylist, *head = NULL, *tmp = NULL;
2314     int has_hold_time = qdict_haskey(qdict, "hold-time");
2315     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2316     Error *err = NULL;
2317     const char *separator;
2318     int keyname_len;
2319 
2320     while (1) {
2321         separator = qemu_strchrnul(keys, '-');
2322         keyname_len = separator - keys;
2323 
2324         /* Be compatible with old interface, convert user inputted "<" */
2325         if (keys[0] == '<' && keyname_len == 1) {
2326             keys = "less";
2327             keyname_len = 4;
2328         }
2329 
2330         keylist = g_malloc0(sizeof(*keylist));
2331         keylist->value = g_malloc0(sizeof(*keylist->value));
2332 
2333         if (!head) {
2334             head = keylist;
2335         }
2336         if (tmp) {
2337             tmp->next = keylist;
2338         }
2339         tmp = keylist;
2340 
2341         if (strstart(keys, "0x", NULL)) {
2342             char *endp;
2343             int value = strtoul(keys, &endp, 0);
2344             assert(endp <= keys + keyname_len);
2345             if (endp != keys + keyname_len) {
2346                 goto err_out;
2347             }
2348             keylist->value->type = KEY_VALUE_KIND_NUMBER;
2349             keylist->value->u.number.data = value;
2350         } else {
2351             int idx = index_from_key(keys, keyname_len);
2352             if (idx == Q_KEY_CODE__MAX) {
2353                 goto err_out;
2354             }
2355             keylist->value->type = KEY_VALUE_KIND_QCODE;
2356             keylist->value->u.qcode.data = idx;
2357         }
2358 
2359         if (!*separator) {
2360             break;
2361         }
2362         keys = separator + 1;
2363     }
2364 
2365     qmp_send_key(head, has_hold_time, hold_time, &err);
2366     hmp_handle_error(mon, &err);
2367 
2368 out:
2369     qapi_free_KeyValueList(head);
2370     return;
2371 
2372 err_out:
2373     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
2374     goto out;
2375 }
2376 
2377 void hmp_screendump(Monitor *mon, const QDict *qdict)
2378 {
2379     const char *filename = qdict_get_str(qdict, "filename");
2380     const char *id = qdict_get_try_str(qdict, "device");
2381     int64_t head = qdict_get_try_int(qdict, "head", 0);
2382     Error *err = NULL;
2383 
2384     qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
2385     hmp_handle_error(mon, &err);
2386 }
2387 
2388 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2389 {
2390     const char *uri = qdict_get_str(qdict, "uri");
2391     bool writable = qdict_get_try_bool(qdict, "writable", false);
2392     bool all = qdict_get_try_bool(qdict, "all", false);
2393     Error *local_err = NULL;
2394     BlockInfoList *block_list, *info;
2395     SocketAddress *addr;
2396 
2397     if (writable && !all) {
2398         error_setg(&local_err, "-w only valid together with -a");
2399         goto exit;
2400     }
2401 
2402     /* First check if the address is valid and start the server.  */
2403     addr = socket_parse(uri, &local_err);
2404     if (local_err != NULL) {
2405         goto exit;
2406     }
2407 
2408     nbd_server_start(addr, NULL, NULL, &local_err);
2409     qapi_free_SocketAddress(addr);
2410     if (local_err != NULL) {
2411         goto exit;
2412     }
2413 
2414     if (!all) {
2415         return;
2416     }
2417 
2418     /* Then try adding all block devices.  If one fails, close all and
2419      * exit.
2420      */
2421     block_list = qmp_query_block(NULL);
2422 
2423     for (info = block_list; info; info = info->next) {
2424         if (!info->value->has_inserted) {
2425             continue;
2426         }
2427 
2428         qmp_nbd_server_add(info->value->device, false, NULL,
2429                            true, writable, false, NULL, &local_err);
2430 
2431         if (local_err != NULL) {
2432             qmp_nbd_server_stop(NULL);
2433             break;
2434         }
2435     }
2436 
2437     qapi_free_BlockInfoList(block_list);
2438 
2439 exit:
2440     hmp_handle_error(mon, &local_err);
2441 }
2442 
2443 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2444 {
2445     const char *device = qdict_get_str(qdict, "device");
2446     const char *name = qdict_get_try_str(qdict, "name");
2447     bool writable = qdict_get_try_bool(qdict, "writable", false);
2448     Error *local_err = NULL;
2449 
2450     qmp_nbd_server_add(device, !!name, name, true, writable,
2451                        false, NULL, &local_err);
2452     hmp_handle_error(mon, &local_err);
2453 }
2454 
2455 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
2456 {
2457     const char *name = qdict_get_str(qdict, "name");
2458     bool force = qdict_get_try_bool(qdict, "force", false);
2459     Error *err = NULL;
2460 
2461     /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
2462     qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
2463     hmp_handle_error(mon, &err);
2464 }
2465 
2466 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2467 {
2468     Error *err = NULL;
2469 
2470     qmp_nbd_server_stop(&err);
2471     hmp_handle_error(mon, &err);
2472 }
2473 
2474 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2475 {
2476     int cpuid;
2477     Error *err = NULL;
2478 
2479     error_report("cpu_add is deprecated, please use device_add instead");
2480 
2481     cpuid = qdict_get_int(qdict, "id");
2482     qmp_cpu_add(cpuid, &err);
2483     hmp_handle_error(mon, &err);
2484 }
2485 
2486 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2487 {
2488     const char *args = qdict_get_str(qdict, "args");
2489     Error *err = NULL;
2490     QemuOpts *opts;
2491 
2492     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2493     if (opts == NULL) {
2494         error_setg(&err, "Parsing chardev args failed");
2495     } else {
2496         qemu_chr_new_from_opts(opts, NULL, &err);
2497         qemu_opts_del(opts);
2498     }
2499     hmp_handle_error(mon, &err);
2500 }
2501 
2502 void hmp_chardev_change(Monitor *mon, const QDict *qdict)
2503 {
2504     const char *args = qdict_get_str(qdict, "args");
2505     const char *id;
2506     Error *err = NULL;
2507     ChardevBackend *backend = NULL;
2508     ChardevReturn *ret = NULL;
2509     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
2510                                              true);
2511     if (!opts) {
2512         error_setg(&err, "Parsing chardev args failed");
2513         goto end;
2514     }
2515 
2516     id = qdict_get_str(qdict, "id");
2517     if (qemu_opts_id(opts)) {
2518         error_setg(&err, "Unexpected 'id' parameter");
2519         goto end;
2520     }
2521 
2522     backend = qemu_chr_parse_opts(opts, &err);
2523     if (!backend) {
2524         goto end;
2525     }
2526 
2527     ret = qmp_chardev_change(id, backend, &err);
2528 
2529 end:
2530     qapi_free_ChardevReturn(ret);
2531     qapi_free_ChardevBackend(backend);
2532     qemu_opts_del(opts);
2533     hmp_handle_error(mon, &err);
2534 }
2535 
2536 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2537 {
2538     Error *local_err = NULL;
2539 
2540     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2541     hmp_handle_error(mon, &local_err);
2542 }
2543 
2544 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2545 {
2546     Error *local_err = NULL;
2547 
2548     qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2549     hmp_handle_error(mon, &local_err);
2550 }
2551 
2552 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2553 {
2554     BlockBackend *blk;
2555     BlockBackend *local_blk = NULL;
2556     const char* device = qdict_get_str(qdict, "device");
2557     const char* command = qdict_get_str(qdict, "command");
2558     Error *err = NULL;
2559     int ret;
2560 
2561     blk = blk_by_name(device);
2562     if (!blk) {
2563         BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2564         if (bs) {
2565             blk = local_blk = blk_new(bdrv_get_aio_context(bs),
2566                                       0, BLK_PERM_ALL);
2567             ret = blk_insert_bs(blk, bs, &err);
2568             if (ret < 0) {
2569                 goto fail;
2570             }
2571         } else {
2572             goto fail;
2573         }
2574     }
2575 
2576     /*
2577      * Notably absent: Proper permission management. This is sad, but it seems
2578      * almost impossible to achieve without changing the semantics and thereby
2579      * limiting the use cases of the qemu-io HMP command.
2580      *
2581      * In an ideal world we would unconditionally create a new BlockBackend for
2582      * qemuio_command(), but we have commands like 'reopen' and want them to
2583      * take effect on the exact BlockBackend whose name the user passed instead
2584      * of just on a temporary copy of it.
2585      *
2586      * Another problem is that deleting the temporary BlockBackend involves
2587      * draining all requests on it first, but some qemu-iotests cases want to
2588      * issue multiple aio_read/write requests and expect them to complete in
2589      * the background while the monitor has already returned.
2590      *
2591      * This is also what prevents us from saving the original permissions and
2592      * restoring them later: We can't revoke permissions until all requests
2593      * have completed, and we don't know when that is nor can we really let
2594      * anything else run before we have revoken them to avoid race conditions.
2595      *
2596      * What happens now is that command() in qemu-io-cmds.c can extend the
2597      * permissions if necessary for the qemu-io command. And they simply stay
2598      * extended, possibly resulting in a read-only guest device keeping write
2599      * permissions. Ugly, but it appears to be the lesser evil.
2600      */
2601     qemuio_command(blk, command);
2602 
2603 fail:
2604     blk_unref(local_blk);
2605     hmp_handle_error(mon, &err);
2606 }
2607 
2608 void hmp_object_del(Monitor *mon, const QDict *qdict)
2609 {
2610     const char *id = qdict_get_str(qdict, "id");
2611     Error *err = NULL;
2612 
2613     user_creatable_del(id, &err);
2614     hmp_handle_error(mon, &err);
2615 }
2616 
2617 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2618 {
2619     Error *err = NULL;
2620     MemdevList *memdev_list = qmp_query_memdev(&err);
2621     MemdevList *m = memdev_list;
2622     Visitor *v;
2623     char *str;
2624 
2625     while (m) {
2626         v = string_output_visitor_new(false, &str);
2627         visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2628         monitor_printf(mon, "memory backend: %s\n", m->value->id);
2629         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
2630         monitor_printf(mon, "  merge: %s\n",
2631                        m->value->merge ? "true" : "false");
2632         monitor_printf(mon, "  dump: %s\n",
2633                        m->value->dump ? "true" : "false");
2634         monitor_printf(mon, "  prealloc: %s\n",
2635                        m->value->prealloc ? "true" : "false");
2636         monitor_printf(mon, "  policy: %s\n",
2637                        HostMemPolicy_str(m->value->policy));
2638         visit_complete(v, &str);
2639         monitor_printf(mon, "  host nodes: %s\n", str);
2640 
2641         g_free(str);
2642         visit_free(v);
2643         m = m->next;
2644     }
2645 
2646     monitor_printf(mon, "\n");
2647 
2648     qapi_free_MemdevList(memdev_list);
2649     hmp_handle_error(mon, &err);
2650 }
2651 
2652 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2653 {
2654     Error *err = NULL;
2655     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2656     MemoryDeviceInfoList *info;
2657     MemoryDeviceInfo *value;
2658     PCDIMMDeviceInfo *di;
2659 
2660     for (info = info_list; info; info = info->next) {
2661         value = info->value;
2662 
2663         if (value) {
2664             switch (value->type) {
2665             case MEMORY_DEVICE_INFO_KIND_DIMM:
2666                 di = value->u.dimm.data;
2667                 break;
2668 
2669             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
2670                 di = value->u.nvdimm.data;
2671                 break;
2672 
2673             default:
2674                 di = NULL;
2675                 break;
2676             }
2677 
2678             if (di) {
2679                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2680                                MemoryDeviceInfoKind_str(value->type),
2681                                di->id ? di->id : "");
2682                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
2683                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
2684                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
2685                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
2686                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
2687                 monitor_printf(mon, "  hotplugged: %s\n",
2688                                di->hotplugged ? "true" : "false");
2689                 monitor_printf(mon, "  hotpluggable: %s\n",
2690                                di->hotpluggable ? "true" : "false");
2691             }
2692         }
2693     }
2694 
2695     qapi_free_MemoryDeviceInfoList(info_list);
2696     hmp_handle_error(mon, &err);
2697 }
2698 
2699 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2700 {
2701     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2702     IOThreadInfoList *info;
2703     IOThreadInfo *value;
2704 
2705     for (info = info_list; info; info = info->next) {
2706         value = info->value;
2707         monitor_printf(mon, "%s:\n", value->id);
2708         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
2709         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2710         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
2711         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
2712     }
2713 
2714     qapi_free_IOThreadInfoList(info_list);
2715 }
2716 
2717 void hmp_rocker(Monitor *mon, const QDict *qdict)
2718 {
2719     const char *name = qdict_get_str(qdict, "name");
2720     RockerSwitch *rocker;
2721     Error *err = NULL;
2722 
2723     rocker = qmp_query_rocker(name, &err);
2724     if (err != NULL) {
2725         hmp_handle_error(mon, &err);
2726         return;
2727     }
2728 
2729     monitor_printf(mon, "name: %s\n", rocker->name);
2730     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2731     monitor_printf(mon, "ports: %d\n", rocker->ports);
2732 
2733     qapi_free_RockerSwitch(rocker);
2734 }
2735 
2736 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2737 {
2738     RockerPortList *list, *port;
2739     const char *name = qdict_get_str(qdict, "name");
2740     Error *err = NULL;
2741 
2742     list = qmp_query_rocker_ports(name, &err);
2743     if (err != NULL) {
2744         hmp_handle_error(mon, &err);
2745         return;
2746     }
2747 
2748     monitor_printf(mon, "            ena/    speed/ auto\n");
2749     monitor_printf(mon, "      port  link    duplex neg?\n");
2750 
2751     for (port = list; port; port = port->next) {
2752         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
2753                        port->value->name,
2754                        port->value->enabled ? port->value->link_up ?
2755                        "up" : "down" : "!ena",
2756                        port->value->speed == 10000 ? "10G" : "??",
2757                        port->value->duplex ? "FD" : "HD",
2758                        port->value->autoneg ? "Yes" : "No");
2759     }
2760 
2761     qapi_free_RockerPortList(list);
2762 }
2763 
2764 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2765 {
2766     RockerOfDpaFlowList *list, *info;
2767     const char *name = qdict_get_str(qdict, "name");
2768     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2769     Error *err = NULL;
2770 
2771     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2772     if (err != NULL) {
2773         hmp_handle_error(mon, &err);
2774         return;
2775     }
2776 
2777     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2778 
2779     for (info = list; info; info = info->next) {
2780         RockerOfDpaFlow *flow = info->value;
2781         RockerOfDpaFlowKey *key = flow->key;
2782         RockerOfDpaFlowMask *mask = flow->mask;
2783         RockerOfDpaFlowAction *action = flow->action;
2784 
2785         if (flow->hits) {
2786             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2787                            key->priority, key->tbl_id, flow->hits);
2788         } else {
2789             monitor_printf(mon, "%-4d %-3d     ",
2790                            key->priority, key->tbl_id);
2791         }
2792 
2793         if (key->has_in_pport) {
2794             monitor_printf(mon, " pport %d", key->in_pport);
2795             if (mask->has_in_pport) {
2796                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2797             }
2798         }
2799 
2800         if (key->has_vlan_id) {
2801             monitor_printf(mon, " vlan %d",
2802                            key->vlan_id & VLAN_VID_MASK);
2803             if (mask->has_vlan_id) {
2804                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2805             }
2806         }
2807 
2808         if (key->has_tunnel_id) {
2809             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2810             if (mask->has_tunnel_id) {
2811                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2812             }
2813         }
2814 
2815         if (key->has_eth_type) {
2816             switch (key->eth_type) {
2817             case 0x0806:
2818                 monitor_printf(mon, " ARP");
2819                 break;
2820             case 0x0800:
2821                 monitor_printf(mon, " IP");
2822                 break;
2823             case 0x86dd:
2824                 monitor_printf(mon, " IPv6");
2825                 break;
2826             case 0x8809:
2827                 monitor_printf(mon, " LACP");
2828                 break;
2829             case 0x88cc:
2830                 monitor_printf(mon, " LLDP");
2831                 break;
2832             default:
2833                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2834                 break;
2835             }
2836         }
2837 
2838         if (key->has_eth_src) {
2839             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2840                 (mask->has_eth_src) &&
2841                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2842                 monitor_printf(mon, " src <any mcast/bcast>");
2843             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2844                 (mask->has_eth_src) &&
2845                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2846                 monitor_printf(mon, " src <any ucast>");
2847             } else {
2848                 monitor_printf(mon, " src %s", key->eth_src);
2849                 if (mask->has_eth_src) {
2850                     monitor_printf(mon, "(%s)", mask->eth_src);
2851                 }
2852             }
2853         }
2854 
2855         if (key->has_eth_dst) {
2856             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2857                 (mask->has_eth_dst) &&
2858                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2859                 monitor_printf(mon, " dst <any mcast/bcast>");
2860             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2861                 (mask->has_eth_dst) &&
2862                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2863                 monitor_printf(mon, " dst <any ucast>");
2864             } else {
2865                 monitor_printf(mon, " dst %s", key->eth_dst);
2866                 if (mask->has_eth_dst) {
2867                     monitor_printf(mon, "(%s)", mask->eth_dst);
2868                 }
2869             }
2870         }
2871 
2872         if (key->has_ip_proto) {
2873             monitor_printf(mon, " proto %d", key->ip_proto);
2874             if (mask->has_ip_proto) {
2875                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2876             }
2877         }
2878 
2879         if (key->has_ip_tos) {
2880             monitor_printf(mon, " TOS %d", key->ip_tos);
2881             if (mask->has_ip_tos) {
2882                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2883             }
2884         }
2885 
2886         if (key->has_ip_dst) {
2887             monitor_printf(mon, " dst %s", key->ip_dst);
2888         }
2889 
2890         if (action->has_goto_tbl || action->has_group_id ||
2891             action->has_new_vlan_id) {
2892             monitor_printf(mon, " -->");
2893         }
2894 
2895         if (action->has_new_vlan_id) {
2896             monitor_printf(mon, " apply new vlan %d",
2897                            ntohs(action->new_vlan_id));
2898         }
2899 
2900         if (action->has_group_id) {
2901             monitor_printf(mon, " write group 0x%08x", action->group_id);
2902         }
2903 
2904         if (action->has_goto_tbl) {
2905             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2906         }
2907 
2908         monitor_printf(mon, "\n");
2909     }
2910 
2911     qapi_free_RockerOfDpaFlowList(list);
2912 }
2913 
2914 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2915 {
2916     RockerOfDpaGroupList *list, *g;
2917     const char *name = qdict_get_str(qdict, "name");
2918     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2919     Error *err = NULL;
2920     bool set = false;
2921 
2922     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2923     if (err != NULL) {
2924         hmp_handle_error(mon, &err);
2925         return;
2926     }
2927 
2928     monitor_printf(mon, "id (decode) --> buckets\n");
2929 
2930     for (g = list; g; g = g->next) {
2931         RockerOfDpaGroup *group = g->value;
2932 
2933         monitor_printf(mon, "0x%08x", group->id);
2934 
2935         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2936                                          group->type == 1 ? "L2 rewrite" :
2937                                          group->type == 2 ? "L3 unicast" :
2938                                          group->type == 3 ? "L2 multicast" :
2939                                          group->type == 4 ? "L2 flood" :
2940                                          group->type == 5 ? "L3 interface" :
2941                                          group->type == 6 ? "L3 multicast" :
2942                                          group->type == 7 ? "L3 ECMP" :
2943                                          group->type == 8 ? "L2 overlay" :
2944                                          "unknown");
2945 
2946         if (group->has_vlan_id) {
2947             monitor_printf(mon, " vlan %d", group->vlan_id);
2948         }
2949 
2950         if (group->has_pport) {
2951             monitor_printf(mon, " pport %d", group->pport);
2952         }
2953 
2954         if (group->has_index) {
2955             monitor_printf(mon, " index %d", group->index);
2956         }
2957 
2958         monitor_printf(mon, ") -->");
2959 
2960         if (group->has_set_vlan_id && group->set_vlan_id) {
2961             set = true;
2962             monitor_printf(mon, " set vlan %d",
2963                            group->set_vlan_id & VLAN_VID_MASK);
2964         }
2965 
2966         if (group->has_set_eth_src) {
2967             if (!set) {
2968                 set = true;
2969                 monitor_printf(mon, " set");
2970             }
2971             monitor_printf(mon, " src %s", group->set_eth_src);
2972         }
2973 
2974         if (group->has_set_eth_dst) {
2975             if (!set) {
2976                 set = true;
2977                 monitor_printf(mon, " set");
2978             }
2979             monitor_printf(mon, " dst %s", group->set_eth_dst);
2980         }
2981 
2982         set = false;
2983 
2984         if (group->has_ttl_check && group->ttl_check) {
2985             monitor_printf(mon, " check TTL");
2986         }
2987 
2988         if (group->has_group_id && group->group_id) {
2989             monitor_printf(mon, " group id 0x%08x", group->group_id);
2990         }
2991 
2992         if (group->has_pop_vlan && group->pop_vlan) {
2993             monitor_printf(mon, " pop vlan");
2994         }
2995 
2996         if (group->has_out_pport) {
2997             monitor_printf(mon, " out pport %d", group->out_pport);
2998         }
2999 
3000         if (group->has_group_ids) {
3001             struct uint32List *id;
3002 
3003             monitor_printf(mon, " groups [");
3004             for (id = group->group_ids; id; id = id->next) {
3005                 monitor_printf(mon, "0x%08x", id->value);
3006                 if (id->next) {
3007                     monitor_printf(mon, ",");
3008                 }
3009             }
3010             monitor_printf(mon, "]");
3011         }
3012 
3013         monitor_printf(mon, "\n");
3014     }
3015 
3016     qapi_free_RockerOfDpaGroupList(list);
3017 }
3018 
3019 void hmp_info_dump(Monitor *mon, const QDict *qdict)
3020 {
3021     DumpQueryResult *result = qmp_query_dump(NULL);
3022 
3023     assert(result && result->status < DUMP_STATUS__MAX);
3024     monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
3025 
3026     if (result->status == DUMP_STATUS_ACTIVE) {
3027         float percent = 0;
3028         assert(result->total != 0);
3029         percent = 100.0 * result->completed / result->total;
3030         monitor_printf(mon, "Finished: %.2f %%\n", percent);
3031     }
3032 
3033     qapi_free_DumpQueryResult(result);
3034 }
3035 
3036 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
3037 {
3038     ram_block_dump(mon);
3039 }
3040 
3041 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
3042 {
3043     Error *err = NULL;
3044     HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
3045     HotpluggableCPUList *saved = l;
3046     CpuInstanceProperties *c;
3047 
3048     if (err != NULL) {
3049         hmp_handle_error(mon, &err);
3050         return;
3051     }
3052 
3053     monitor_printf(mon, "Hotpluggable CPUs:\n");
3054     while (l) {
3055         monitor_printf(mon, "  type: \"%s\"\n", l->value->type);
3056         monitor_printf(mon, "  vcpus_count: \"%" PRIu64 "\"\n",
3057                        l->value->vcpus_count);
3058         if (l->value->has_qom_path) {
3059             monitor_printf(mon, "  qom_path: \"%s\"\n", l->value->qom_path);
3060         }
3061 
3062         c = l->value->props;
3063         monitor_printf(mon, "  CPUInstance Properties:\n");
3064         if (c->has_node_id) {
3065             monitor_printf(mon, "    node-id: \"%" PRIu64 "\"\n", c->node_id);
3066         }
3067         if (c->has_socket_id) {
3068             monitor_printf(mon, "    socket-id: \"%" PRIu64 "\"\n", c->socket_id);
3069         }
3070         if (c->has_core_id) {
3071             monitor_printf(mon, "    core-id: \"%" PRIu64 "\"\n", c->core_id);
3072         }
3073         if (c->has_thread_id) {
3074             monitor_printf(mon, "    thread-id: \"%" PRIu64 "\"\n", c->thread_id);
3075         }
3076 
3077         l = l->next;
3078     }
3079 
3080     qapi_free_HotpluggableCPUList(saved);
3081 }
3082 
3083 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
3084 {
3085     Error *err = NULL;
3086     GuidInfo *info = qmp_query_vm_generation_id(&err);
3087     if (info) {
3088         monitor_printf(mon, "%s\n", info->guid);
3089     }
3090     hmp_handle_error(mon, &err);
3091     qapi_free_GuidInfo(info);
3092 }
3093 
3094 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
3095 {
3096     Error *err = NULL;
3097     MemoryInfo *info = qmp_query_memory_size_summary(&err);
3098     if (info) {
3099         monitor_printf(mon, "base memory: %" PRIu64 "\n",
3100                        info->base_memory);
3101 
3102         if (info->has_plugged_memory) {
3103             monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
3104                            info->plugged_memory);
3105         }
3106 
3107         qapi_free_MemoryInfo(info);
3108     }
3109     hmp_handle_error(mon, &err);
3110 }
3111