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