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