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