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