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