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