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