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