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