]> Git Repo - qemu.git/blob - hmp.c
Merge remote-tracking branch 'remotes/vivier2/tags/trivial-patches-pull-request'...
[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     if (dev->id->has_subsystem_vendor && dev->id->has_subsystem) {
841         monitor_printf(mon, "      PCI subsystem %04" PRIx64 ":%04" PRIx64 "\n",
842                        dev->id->subsystem_vendor, dev->id->subsystem);
843     }
844
845     if (dev->has_irq) {
846         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
847     }
848
849     if (dev->has_pci_bridge) {
850         monitor_printf(mon, "      BUS %" PRId64 ".\n",
851                        dev->pci_bridge->bus->number);
852         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
853                        dev->pci_bridge->bus->secondary);
854         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
855                        dev->pci_bridge->bus->subordinate);
856
857         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
858                        dev->pci_bridge->bus->io_range->base,
859                        dev->pci_bridge->bus->io_range->limit);
860
861         monitor_printf(mon,
862                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
863                        dev->pci_bridge->bus->memory_range->base,
864                        dev->pci_bridge->bus->memory_range->limit);
865
866         monitor_printf(mon, "      prefetchable memory range "
867                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
868                        dev->pci_bridge->bus->prefetchable_range->base,
869                        dev->pci_bridge->bus->prefetchable_range->limit);
870     }
871
872     for (region = dev->regions; region; region = region->next) {
873         uint64_t addr, size;
874
875         addr = region->value->address;
876         size = region->value->size;
877
878         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
879
880         if (!strcmp(region->value->type, "io")) {
881             monitor_printf(mon, "I/O at 0x%04" PRIx64
882                                 " [0x%04" PRIx64 "].\n",
883                            addr, addr + size - 1);
884         } else {
885             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
886                                " [0x%08" PRIx64 "].\n",
887                            region->value->mem_type_64 ? 64 : 32,
888                            region->value->prefetch ? " prefetchable" : "",
889                            addr, addr + size - 1);
890         }
891     }
892
893     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
894
895     if (dev->has_pci_bridge) {
896         if (dev->pci_bridge->has_devices) {
897             PciDeviceInfoList *cdev;
898             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
899                 hmp_info_pci_device(mon, cdev->value);
900             }
901         }
902     }
903 }
904
905 static int hmp_info_irq_foreach(Object *obj, void *opaque)
906 {
907     InterruptStatsProvider *intc;
908     InterruptStatsProviderClass *k;
909     Monitor *mon = opaque;
910
911     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
912         intc = INTERRUPT_STATS_PROVIDER(obj);
913         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
914         uint64_t *irq_counts;
915         unsigned int nb_irqs, i;
916         if (k->get_statistics &&
917             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
918             if (nb_irqs > 0) {
919                 monitor_printf(mon, "IRQ statistics for %s:\n",
920                                object_get_typename(obj));
921                 for (i = 0; i < nb_irqs; i++) {
922                     if (irq_counts[i] > 0) {
923                         monitor_printf(mon, "%2d: %" PRId64 "\n", i,
924                                        irq_counts[i]);
925                     }
926                 }
927             }
928         } else {
929             monitor_printf(mon, "IRQ statistics not available for %s.\n",
930                            object_get_typename(obj));
931         }
932     }
933
934     return 0;
935 }
936
937 void hmp_info_irq(Monitor *mon, const QDict *qdict)
938 {
939     object_child_foreach_recursive(object_get_root(),
940                                    hmp_info_irq_foreach, mon);
941 }
942
943 static int hmp_info_pic_foreach(Object *obj, void *opaque)
944 {
945     InterruptStatsProvider *intc;
946     InterruptStatsProviderClass *k;
947     Monitor *mon = opaque;
948
949     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
950         intc = INTERRUPT_STATS_PROVIDER(obj);
951         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
952         if (k->print_info) {
953             k->print_info(intc, mon);
954         } else {
955             monitor_printf(mon, "Interrupt controller information not available for %s.\n",
956                            object_get_typename(obj));
957         }
958     }
959
960     return 0;
961 }
962
963 void hmp_info_pic(Monitor *mon, const QDict *qdict)
964 {
965     object_child_foreach_recursive(object_get_root(),
966                                    hmp_info_pic_foreach, mon);
967 }
968
969 void hmp_info_pci(Monitor *mon, const QDict *qdict)
970 {
971     PciInfoList *info_list, *info;
972     Error *err = NULL;
973
974     info_list = qmp_query_pci(&err);
975     if (err) {
976         monitor_printf(mon, "PCI devices not supported\n");
977         error_free(err);
978         return;
979     }
980
981     for (info = info_list; info; info = info->next) {
982         PciDeviceInfoList *dev;
983
984         for (dev = info->value->devices; dev; dev = dev->next) {
985             hmp_info_pci_device(mon, dev->value);
986         }
987     }
988
989     qapi_free_PciInfoList(info_list);
990 }
991
992 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
993 {
994     BlockJobInfoList *list;
995     Error *err = NULL;
996
997     list = qmp_query_block_jobs(&err);
998     assert(!err);
999
1000     if (!list) {
1001         monitor_printf(mon, "No active jobs\n");
1002         return;
1003     }
1004
1005     while (list) {
1006         if (strcmp(list->value->type, "stream") == 0) {
1007             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
1008                            " of %" PRId64 " bytes, speed limit %" PRId64
1009                            " bytes/s\n",
1010                            list->value->device,
1011                            list->value->offset,
1012                            list->value->len,
1013                            list->value->speed);
1014         } else {
1015             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
1016                            " of %" PRId64 " bytes, speed limit %" PRId64
1017                            " bytes/s\n",
1018                            list->value->type,
1019                            list->value->device,
1020                            list->value->offset,
1021                            list->value->len,
1022                            list->value->speed);
1023         }
1024         list = list->next;
1025     }
1026
1027     qapi_free_BlockJobInfoList(list);
1028 }
1029
1030 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
1031 {
1032     TPMInfoList *info_list, *info;
1033     Error *err = NULL;
1034     unsigned int c = 0;
1035     TPMPassthroughOptions *tpo;
1036     TPMEmulatorOptions *teo;
1037
1038     info_list = qmp_query_tpm(&err);
1039     if (err) {
1040         monitor_printf(mon, "TPM device not supported\n");
1041         error_free(err);
1042         return;
1043     }
1044
1045     if (info_list) {
1046         monitor_printf(mon, "TPM device:\n");
1047     }
1048
1049     for (info = info_list; info; info = info->next) {
1050         TPMInfo *ti = info->value;
1051         monitor_printf(mon, " tpm%d: model=%s\n",
1052                        c, TpmModel_str(ti->model));
1053
1054         monitor_printf(mon, "  \\ %s: type=%s",
1055                        ti->id, TpmTypeOptionsKind_str(ti->options->type));
1056
1057         switch (ti->options->type) {
1058         case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
1059             tpo = ti->options->u.passthrough.data;
1060             monitor_printf(mon, "%s%s%s%s",
1061                            tpo->has_path ? ",path=" : "",
1062                            tpo->has_path ? tpo->path : "",
1063                            tpo->has_cancel_path ? ",cancel-path=" : "",
1064                            tpo->has_cancel_path ? tpo->cancel_path : "");
1065             break;
1066         case TPM_TYPE_OPTIONS_KIND_EMULATOR:
1067             teo = ti->options->u.emulator.data;
1068             monitor_printf(mon, ",chardev=%s", teo->chardev);
1069             break;
1070         case TPM_TYPE_OPTIONS_KIND__MAX:
1071             break;
1072         }
1073         monitor_printf(mon, "\n");
1074         c++;
1075     }
1076     qapi_free_TPMInfoList(info_list);
1077 }
1078
1079 void hmp_quit(Monitor *mon, const QDict *qdict)
1080 {
1081     monitor_suspend(mon);
1082     qmp_quit(NULL);
1083 }
1084
1085 void hmp_stop(Monitor *mon, const QDict *qdict)
1086 {
1087     qmp_stop(NULL);
1088 }
1089
1090 void hmp_sync_profile(Monitor *mon, const QDict *qdict)
1091 {
1092     const char *op = qdict_get_try_str(qdict, "op");
1093
1094     if (op == NULL) {
1095         bool on = qsp_is_enabled();
1096
1097         monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
1098         return;
1099     }
1100     if (!strcmp(op, "on")) {
1101         qsp_enable();
1102     } else if (!strcmp(op, "off")) {
1103         qsp_disable();
1104     } else if (!strcmp(op, "reset")) {
1105         qsp_reset();
1106     } else {
1107         Error *err = NULL;
1108
1109         error_setg(&err, QERR_INVALID_PARAMETER, op);
1110         hmp_handle_error(mon, &err);
1111     }
1112 }
1113
1114 void hmp_system_reset(Monitor *mon, const QDict *qdict)
1115 {
1116     qmp_system_reset(NULL);
1117 }
1118
1119 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1120 {
1121     qmp_system_powerdown(NULL);
1122 }
1123
1124 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
1125 {
1126     Error *err = NULL;
1127
1128     qmp_x_exit_preconfig(&err);
1129     hmp_handle_error(mon, &err);
1130 }
1131
1132 void hmp_cpu(Monitor *mon, const QDict *qdict)
1133 {
1134     int64_t cpu_index;
1135
1136     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1137             use it are converted to the QAPI */
1138     cpu_index = qdict_get_int(qdict, "index");
1139     if (monitor_set_cpu(cpu_index) < 0) {
1140         monitor_printf(mon, "invalid CPU index\n");
1141     }
1142 }
1143
1144 void hmp_memsave(Monitor *mon, const QDict *qdict)
1145 {
1146     uint32_t size = qdict_get_int(qdict, "size");
1147     const char *filename = qdict_get_str(qdict, "filename");
1148     uint64_t addr = qdict_get_int(qdict, "val");
1149     Error *err = NULL;
1150     int cpu_index = monitor_get_cpu_index();
1151
1152     if (cpu_index < 0) {
1153         monitor_printf(mon, "No CPU available\n");
1154         return;
1155     }
1156
1157     qmp_memsave(addr, size, filename, true, cpu_index, &err);
1158     hmp_handle_error(mon, &err);
1159 }
1160
1161 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1162 {
1163     uint32_t size = qdict_get_int(qdict, "size");
1164     const char *filename = qdict_get_str(qdict, "filename");
1165     uint64_t addr = qdict_get_int(qdict, "val");
1166     Error *err = NULL;
1167
1168     qmp_pmemsave(addr, size, filename, &err);
1169     hmp_handle_error(mon, &err);
1170 }
1171
1172 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1173 {
1174     const char *chardev = qdict_get_str(qdict, "device");
1175     const char *data = qdict_get_str(qdict, "data");
1176     Error *err = NULL;
1177
1178     qmp_ringbuf_write(chardev, data, false, 0, &err);
1179
1180     hmp_handle_error(mon, &err);
1181 }
1182
1183 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1184 {
1185     uint32_t size = qdict_get_int(qdict, "size");
1186     const char *chardev = qdict_get_str(qdict, "device");
1187     char *data;
1188     Error *err = NULL;
1189     int i;
1190
1191     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1192     if (err) {
1193         hmp_handle_error(mon, &err);
1194         return;
1195     }
1196
1197     for (i = 0; data[i]; i++) {
1198         unsigned char ch = data[i];
1199
1200         if (ch == '\\') {
1201             monitor_printf(mon, "\\\\");
1202         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1203             monitor_printf(mon, "\\u%04X", ch);
1204         } else {
1205             monitor_printf(mon, "%c", ch);
1206         }
1207
1208     }
1209     monitor_printf(mon, "\n");
1210     g_free(data);
1211 }
1212
1213 void hmp_cont(Monitor *mon, const QDict *qdict)
1214 {
1215     Error *err = NULL;
1216
1217     qmp_cont(&err);
1218     hmp_handle_error(mon, &err);
1219 }
1220
1221 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1222 {
1223     Error *err = NULL;
1224
1225     qmp_system_wakeup(&err);
1226     hmp_handle_error(mon, &err);
1227 }
1228
1229 void hmp_nmi(Monitor *mon, const QDict *qdict)
1230 {
1231     Error *err = NULL;
1232
1233     qmp_inject_nmi(&err);
1234     hmp_handle_error(mon, &err);
1235 }
1236
1237 void hmp_set_link(Monitor *mon, const QDict *qdict)
1238 {
1239     const char *name = qdict_get_str(qdict, "name");
1240     bool up = qdict_get_bool(qdict, "up");
1241     Error *err = NULL;
1242
1243     qmp_set_link(name, up, &err);
1244     hmp_handle_error(mon, &err);
1245 }
1246
1247 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1248 {
1249     const char *device = qdict_get_str(qdict, "device");
1250     const char *password = qdict_get_str(qdict, "password");
1251     Error *err = NULL;
1252
1253     qmp_block_passwd(true, device, false, NULL, password, &err);
1254     hmp_handle_error(mon, &err);
1255 }
1256
1257 void hmp_balloon(Monitor *mon, const QDict *qdict)
1258 {
1259     int64_t value = qdict_get_int(qdict, "value");
1260     Error *err = NULL;
1261
1262     qmp_balloon(value, &err);
1263     hmp_handle_error(mon, &err);
1264 }
1265
1266 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1267 {
1268     const char *device = qdict_get_str(qdict, "device");
1269     int64_t size = qdict_get_int(qdict, "size");
1270     Error *err = NULL;
1271
1272     qmp_block_resize(true, device, false, NULL, size, &err);
1273     hmp_handle_error(mon, &err);
1274 }
1275
1276 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1277 {
1278     const char *filename = qdict_get_str(qdict, "target");
1279     const char *format = qdict_get_try_str(qdict, "format");
1280     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1281     bool full = qdict_get_try_bool(qdict, "full", false);
1282     Error *err = NULL;
1283     DriveMirror mirror = {
1284         .device = (char *)qdict_get_str(qdict, "device"),
1285         .target = (char *)filename,
1286         .has_format = !!format,
1287         .format = (char *)format,
1288         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1289         .has_mode = true,
1290         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1291         .unmap = true,
1292     };
1293
1294     if (!filename) {
1295         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1296         hmp_handle_error(mon, &err);
1297         return;
1298     }
1299     qmp_drive_mirror(&mirror, &err);
1300     hmp_handle_error(mon, &err);
1301 }
1302
1303 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1304 {
1305     const char *device = qdict_get_str(qdict, "device");
1306     const char *filename = qdict_get_str(qdict, "target");
1307     const char *format = qdict_get_try_str(qdict, "format");
1308     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1309     bool full = qdict_get_try_bool(qdict, "full", false);
1310     bool compress = qdict_get_try_bool(qdict, "compress", false);
1311     Error *err = NULL;
1312     DriveBackup backup = {
1313         .device = (char *)device,
1314         .target = (char *)filename,
1315         .has_format = !!format,
1316         .format = (char *)format,
1317         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1318         .has_mode = true,
1319         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1320         .has_compress = !!compress,
1321         .compress = compress,
1322     };
1323
1324     if (!filename) {
1325         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1326         hmp_handle_error(mon, &err);
1327         return;
1328     }
1329
1330     qmp_drive_backup(&backup, &err);
1331     hmp_handle_error(mon, &err);
1332 }
1333
1334 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1335 {
1336     const char *device = qdict_get_str(qdict, "device");
1337     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1338     const char *format = qdict_get_try_str(qdict, "format");
1339     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1340     enum NewImageMode mode;
1341     Error *err = NULL;
1342
1343     if (!filename) {
1344         /* In the future, if 'snapshot-file' is not specified, the snapshot
1345            will be taken internally. Today it's actually required. */
1346         error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1347         hmp_handle_error(mon, &err);
1348         return;
1349     }
1350
1351     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1352     qmp_blockdev_snapshot_sync(true, device, false, NULL,
1353                                filename, false, NULL,
1354                                !!format, format,
1355                                true, mode, &err);
1356     hmp_handle_error(mon, &err);
1357 }
1358
1359 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1360 {
1361     const char *device = qdict_get_str(qdict, "device");
1362     const char *name = qdict_get_str(qdict, "name");
1363     Error *err = NULL;
1364
1365     qmp_blockdev_snapshot_internal_sync(device, name, &err);
1366     hmp_handle_error(mon, &err);
1367 }
1368
1369 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1370 {
1371     const char *device = qdict_get_str(qdict, "device");
1372     const char *name = qdict_get_str(qdict, "name");
1373     const char *id = qdict_get_try_str(qdict, "id");
1374     Error *err = NULL;
1375
1376     qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1377                                                true, name, &err);
1378     hmp_handle_error(mon, &err);
1379 }
1380
1381 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1382 {
1383     int saved_vm_running  = runstate_is_running();
1384     const char *name = qdict_get_str(qdict, "name");
1385     Error *err = NULL;
1386
1387     vm_stop(RUN_STATE_RESTORE_VM);
1388
1389     if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1390         vm_start();
1391     }
1392     hmp_handle_error(mon, &err);
1393 }
1394
1395 void hmp_savevm(Monitor *mon, const QDict *qdict)
1396 {
1397     Error *err = NULL;
1398
1399     save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1400     hmp_handle_error(mon, &err);
1401 }
1402
1403 void hmp_delvm(Monitor *mon, const QDict *qdict)
1404 {
1405     BlockDriverState *bs;
1406     Error *err = NULL;
1407     const char *name = qdict_get_str(qdict, "name");
1408
1409     if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1410         error_reportf_err(err,
1411                           "Error while deleting snapshot on device '%s': ",
1412                           bdrv_get_device_name(bs));
1413     }
1414 }
1415
1416 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1417 {
1418     BlockDriverState *bs, *bs1;
1419     BdrvNextIterator it1;
1420     QEMUSnapshotInfo *sn_tab, *sn;
1421     bool no_snapshot = true;
1422     int nb_sns, i;
1423     int total;
1424     int *global_snapshots;
1425     AioContext *aio_context;
1426
1427     typedef struct SnapshotEntry {
1428         QEMUSnapshotInfo sn;
1429         QTAILQ_ENTRY(SnapshotEntry) next;
1430     } SnapshotEntry;
1431
1432     typedef struct ImageEntry {
1433         const char *imagename;
1434         QTAILQ_ENTRY(ImageEntry) next;
1435         QTAILQ_HEAD(, SnapshotEntry) snapshots;
1436     } ImageEntry;
1437
1438     QTAILQ_HEAD(, ImageEntry) image_list =
1439         QTAILQ_HEAD_INITIALIZER(image_list);
1440
1441     ImageEntry *image_entry, *next_ie;
1442     SnapshotEntry *snapshot_entry;
1443
1444     bs = bdrv_all_find_vmstate_bs();
1445     if (!bs) {
1446         monitor_printf(mon, "No available block device supports snapshots\n");
1447         return;
1448     }
1449     aio_context = bdrv_get_aio_context(bs);
1450
1451     aio_context_acquire(aio_context);
1452     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1453     aio_context_release(aio_context);
1454
1455     if (nb_sns < 0) {
1456         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1457         return;
1458     }
1459
1460     for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1461         int bs1_nb_sns = 0;
1462         ImageEntry *ie;
1463         SnapshotEntry *se;
1464         AioContext *ctx = bdrv_get_aio_context(bs1);
1465
1466         aio_context_acquire(ctx);
1467         if (bdrv_can_snapshot(bs1)) {
1468             sn = NULL;
1469             bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1470             if (bs1_nb_sns > 0) {
1471                 no_snapshot = false;
1472                 ie = g_new0(ImageEntry, 1);
1473                 ie->imagename = bdrv_get_device_name(bs1);
1474                 QTAILQ_INIT(&ie->snapshots);
1475                 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1476                 for (i = 0; i < bs1_nb_sns; i++) {
1477                     se = g_new0(SnapshotEntry, 1);
1478                     se->sn = sn[i];
1479                     QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1480                 }
1481             }
1482             g_free(sn);
1483         }
1484         aio_context_release(ctx);
1485     }
1486
1487     if (no_snapshot) {
1488         monitor_printf(mon, "There is no snapshot available.\n");
1489         return;
1490     }
1491
1492     global_snapshots = g_new0(int, nb_sns);
1493     total = 0;
1494     for (i = 0; i < nb_sns; i++) {
1495         SnapshotEntry *next_sn;
1496         if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1497             global_snapshots[total] = i;
1498             total++;
1499             QTAILQ_FOREACH(image_entry, &image_list, next) {
1500                 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1501                                     next, next_sn) {
1502                     if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1503                         QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1504                                       next);
1505                         g_free(snapshot_entry);
1506                     }
1507                 }
1508             }
1509         }
1510     }
1511
1512     monitor_printf(mon, "List of snapshots present on all disks:\n");
1513
1514     if (total > 0) {
1515         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1516         monitor_printf(mon, "\n");
1517         for (i = 0; i < total; i++) {
1518             sn = &sn_tab[global_snapshots[i]];
1519             /* The ID is not guaranteed to be the same on all images, so
1520              * overwrite it.
1521              */
1522             pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1523             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1524             monitor_printf(mon, "\n");
1525         }
1526     } else {
1527         monitor_printf(mon, "None\n");
1528     }
1529
1530     QTAILQ_FOREACH(image_entry, &image_list, next) {
1531         if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1532             continue;
1533         }
1534         monitor_printf(mon,
1535                        "\nList of partial (non-loadable) snapshots on '%s':\n",
1536                        image_entry->imagename);
1537         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1538         monitor_printf(mon, "\n");
1539         QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1540             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1541                                &snapshot_entry->sn);
1542             monitor_printf(mon, "\n");
1543         }
1544     }
1545
1546     QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1547         SnapshotEntry *next_sn;
1548         QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1549                             next_sn) {
1550             g_free(snapshot_entry);
1551         }
1552         g_free(image_entry);
1553     }
1554     g_free(sn_tab);
1555     g_free(global_snapshots);
1556
1557 }
1558
1559 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1560 {
1561     qmp_migrate_cancel(NULL);
1562 }
1563
1564 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
1565 {
1566     Error *err = NULL;
1567     const char *state = qdict_get_str(qdict, "state");
1568     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
1569
1570     if (val >= 0) {
1571         qmp_migrate_continue(val, &err);
1572     }
1573
1574     hmp_handle_error(mon, &err);
1575 }
1576
1577 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1578 {
1579     Error *err = NULL;
1580     const char *uri = qdict_get_str(qdict, "uri");
1581
1582     qmp_migrate_incoming(uri, &err);
1583
1584     hmp_handle_error(mon, &err);
1585 }
1586
1587 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
1588 {
1589     Error *err = NULL;
1590     const char *uri = qdict_get_str(qdict, "uri");
1591
1592     qmp_migrate_recover(uri, &err);
1593
1594     hmp_handle_error(mon, &err);
1595 }
1596
1597 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
1598 {
1599     Error *err = NULL;
1600
1601     qmp_migrate_pause(&err);
1602
1603     hmp_handle_error(mon, &err);
1604 }
1605
1606 /* Kept for backwards compatibility */
1607 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1608 {
1609     double value = qdict_get_double(qdict, "value");
1610     qmp_migrate_set_downtime(value, NULL);
1611 }
1612
1613 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1614 {
1615     int64_t value = qdict_get_int(qdict, "value");
1616     Error *err = NULL;
1617
1618     qmp_migrate_set_cache_size(value, &err);
1619     hmp_handle_error(mon, &err);
1620 }
1621
1622 /* Kept for backwards compatibility */
1623 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1624 {
1625     int64_t value = qdict_get_int(qdict, "value");
1626     qmp_migrate_set_speed(value, NULL);
1627 }
1628
1629 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1630 {
1631     const char *cap = qdict_get_str(qdict, "capability");
1632     bool state = qdict_get_bool(qdict, "state");
1633     Error *err = NULL;
1634     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1635     int val;
1636
1637     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
1638     if (val < 0) {
1639         goto end;
1640     }
1641
1642     caps->value = g_malloc0(sizeof(*caps->value));
1643     caps->value->capability = val;
1644     caps->value->state = state;
1645     caps->next = NULL;
1646     qmp_migrate_set_capabilities(caps, &err);
1647
1648 end:
1649     qapi_free_MigrationCapabilityStatusList(caps);
1650     hmp_handle_error(mon, &err);
1651 }
1652
1653 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1654 {
1655     const char *param = qdict_get_str(qdict, "parameter");
1656     const char *valuestr = qdict_get_str(qdict, "value");
1657     Visitor *v = string_input_visitor_new(valuestr);
1658     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
1659     uint64_t valuebw = 0;
1660     uint64_t cache_size;
1661     Error *err = NULL;
1662     int val, ret;
1663
1664     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
1665     if (val < 0) {
1666         goto cleanup;
1667     }
1668
1669     switch (val) {
1670     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1671         p->has_compress_level = true;
1672         visit_type_int(v, param, &p->compress_level, &err);
1673         break;
1674     case MIGRATION_PARAMETER_COMPRESS_THREADS:
1675         p->has_compress_threads = true;
1676         visit_type_int(v, param, &p->compress_threads, &err);
1677         break;
1678     case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
1679         p->has_compress_wait_thread = true;
1680         visit_type_bool(v, param, &p->compress_wait_thread, &err);
1681         break;
1682     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1683         p->has_decompress_threads = true;
1684         visit_type_int(v, param, &p->decompress_threads, &err);
1685         break;
1686     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1687         p->has_cpu_throttle_initial = true;
1688         visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1689         break;
1690     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1691         p->has_cpu_throttle_increment = true;
1692         visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1693         break;
1694     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
1695         p->has_max_cpu_throttle = true;
1696         visit_type_int(v, param, &p->max_cpu_throttle, &err);
1697         break;
1698     case MIGRATION_PARAMETER_TLS_CREDS:
1699         p->has_tls_creds = true;
1700         p->tls_creds = g_new0(StrOrNull, 1);
1701         p->tls_creds->type = QTYPE_QSTRING;
1702         visit_type_str(v, param, &p->tls_creds->u.s, &err);
1703         break;
1704     case MIGRATION_PARAMETER_TLS_HOSTNAME:
1705         p->has_tls_hostname = true;
1706         p->tls_hostname = g_new0(StrOrNull, 1);
1707         p->tls_hostname->type = QTYPE_QSTRING;
1708         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
1709         break;
1710     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1711         p->has_max_bandwidth = true;
1712         /*
1713          * Can't use visit_type_size() here, because it
1714          * defaults to Bytes rather than Mebibytes.
1715          */
1716         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1717         if (ret < 0 || valuebw > INT64_MAX
1718             || (size_t)valuebw != valuebw) {
1719             error_setg(&err, "Invalid size %s", valuestr);
1720             break;
1721         }
1722         p->max_bandwidth = valuebw;
1723         break;
1724     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1725         p->has_downtime_limit = true;
1726         visit_type_int(v, param, &p->downtime_limit, &err);
1727         break;
1728     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1729         p->has_x_checkpoint_delay = true;
1730         visit_type_int(v, param, &p->x_checkpoint_delay, &err);
1731         break;
1732     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1733         p->has_block_incremental = true;
1734         visit_type_bool(v, param, &p->block_incremental, &err);
1735         break;
1736     case MIGRATION_PARAMETER_X_MULTIFD_CHANNELS:
1737         p->has_x_multifd_channels = true;
1738         visit_type_int(v, param, &p->x_multifd_channels, &err);
1739         break;
1740     case MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT:
1741         p->has_x_multifd_page_count = true;
1742         visit_type_int(v, param, &p->x_multifd_page_count, &err);
1743         break;
1744     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
1745         p->has_xbzrle_cache_size = true;
1746         visit_type_size(v, param, &cache_size, &err);
1747         if (err || cache_size > INT64_MAX
1748             || (size_t)cache_size != cache_size) {
1749             error_setg(&err, "Invalid size %s", valuestr);
1750             break;
1751         }
1752         p->xbzrle_cache_size = cache_size;
1753         break;
1754     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
1755         p->has_max_postcopy_bandwidth = true;
1756         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
1757         break;
1758     default:
1759         assert(0);
1760     }
1761
1762     if (err) {
1763         goto cleanup;
1764     }
1765
1766     qmp_migrate_set_parameters(p, &err);
1767
1768  cleanup:
1769     qapi_free_MigrateSetParameters(p);
1770     visit_free(v);
1771     hmp_handle_error(mon, &err);
1772 }
1773
1774 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1775 {
1776     Error *err = NULL;
1777     const char *protocol = qdict_get_str(qdict, "protocol");
1778     const char *hostname = qdict_get_str(qdict, "hostname");
1779     bool has_port        = qdict_haskey(qdict, "port");
1780     int port             = qdict_get_try_int(qdict, "port", -1);
1781     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1782     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1783     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1784
1785     qmp_client_migrate_info(protocol, hostname,
1786                             has_port, port, has_tls_port, tls_port,
1787                             !!cert_subject, cert_subject, &err);
1788     hmp_handle_error(mon, &err);
1789 }
1790
1791 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1792 {
1793     Error *err = NULL;
1794     qmp_migrate_start_postcopy(&err);
1795     hmp_handle_error(mon, &err);
1796 }
1797
1798 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1799 {
1800     Error *err = NULL;
1801
1802     qmp_x_colo_lost_heartbeat(&err);
1803     hmp_handle_error(mon, &err);
1804 }
1805
1806 void hmp_set_password(Monitor *mon, const QDict *qdict)
1807 {
1808     const char *protocol  = qdict_get_str(qdict, "protocol");
1809     const char *password  = qdict_get_str(qdict, "password");
1810     const char *connected = qdict_get_try_str(qdict, "connected");
1811     Error *err = NULL;
1812
1813     qmp_set_password(protocol, password, !!connected, connected, &err);
1814     hmp_handle_error(mon, &err);
1815 }
1816
1817 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1818 {
1819     const char *protocol  = qdict_get_str(qdict, "protocol");
1820     const char *whenstr = qdict_get_str(qdict, "time");
1821     Error *err = NULL;
1822
1823     qmp_expire_password(protocol, whenstr, &err);
1824     hmp_handle_error(mon, &err);
1825 }
1826
1827 void hmp_eject(Monitor *mon, const QDict *qdict)
1828 {
1829     bool force = qdict_get_try_bool(qdict, "force", false);
1830     const char *device = qdict_get_str(qdict, "device");
1831     Error *err = NULL;
1832
1833     qmp_eject(true, device, false, NULL, true, force, &err);
1834     hmp_handle_error(mon, &err);
1835 }
1836
1837 #ifdef CONFIG_VNC
1838 static void hmp_change_read_arg(void *opaque, const char *password,
1839                                 void *readline_opaque)
1840 {
1841     qmp_change_vnc_password(password, NULL);
1842     monitor_read_command(opaque, 1);
1843 }
1844 #endif
1845
1846 void hmp_change(Monitor *mon, const QDict *qdict)
1847 {
1848     const char *device = qdict_get_str(qdict, "device");
1849     const char *target = qdict_get_str(qdict, "target");
1850     const char *arg = qdict_get_try_str(qdict, "arg");
1851     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1852     BlockdevChangeReadOnlyMode read_only_mode = 0;
1853     Error *err = NULL;
1854
1855 #ifdef CONFIG_VNC
1856     if (strcmp(device, "vnc") == 0) {
1857         if (read_only) {
1858             monitor_printf(mon,
1859                            "Parameter 'read-only-mode' is invalid for VNC\n");
1860             return;
1861         }
1862         if (strcmp(target, "passwd") == 0 ||
1863             strcmp(target, "password") == 0) {
1864             if (!arg) {
1865                 monitor_read_password(mon, hmp_change_read_arg, NULL);
1866                 return;
1867             }
1868         }
1869         qmp_change("vnc", target, !!arg, arg, &err);
1870     } else
1871 #endif
1872     {
1873         if (read_only) {
1874             read_only_mode =
1875                 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1876                                 read_only,
1877                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1878             if (err) {
1879                 hmp_handle_error(mon, &err);
1880                 return;
1881             }
1882         }
1883
1884         qmp_blockdev_change_medium(true, device, false, NULL, target,
1885                                    !!arg, arg, !!read_only, read_only_mode,
1886                                    &err);
1887     }
1888
1889     hmp_handle_error(mon, &err);
1890 }
1891
1892 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1893 {
1894     Error *err = NULL;
1895     char *device = (char *) qdict_get_str(qdict, "device");
1896     BlockIOThrottle throttle = {
1897         .bps = qdict_get_int(qdict, "bps"),
1898         .bps_rd = qdict_get_int(qdict, "bps_rd"),
1899         .bps_wr = qdict_get_int(qdict, "bps_wr"),
1900         .iops = qdict_get_int(qdict, "iops"),
1901         .iops_rd = qdict_get_int(qdict, "iops_rd"),
1902         .iops_wr = qdict_get_int(qdict, "iops_wr"),
1903     };
1904
1905     /* qmp_block_set_io_throttle has separate parameters for the
1906      * (deprecated) block device name and the qdev ID but the HMP
1907      * version has only one, so we must decide which one to pass. */
1908     if (blk_by_name(device)) {
1909         throttle.has_device = true;
1910         throttle.device = device;
1911     } else {
1912         throttle.has_id = true;
1913         throttle.id = device;
1914     }
1915
1916     qmp_block_set_io_throttle(&throttle, &err);
1917     hmp_handle_error(mon, &err);
1918 }
1919
1920 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1921 {
1922     Error *error = NULL;
1923     const char *device = qdict_get_str(qdict, "device");
1924     const char *base = qdict_get_try_str(qdict, "base");
1925     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1926
1927     qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
1928                      false, NULL, qdict_haskey(qdict, "speed"), speed, true,
1929                      BLOCKDEV_ON_ERROR_REPORT, false, false, false, false,
1930                      &error);
1931
1932     hmp_handle_error(mon, &error);
1933 }
1934
1935 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1936 {
1937     Error *error = NULL;
1938     const char *device = qdict_get_str(qdict, "device");
1939     int64_t value = qdict_get_int(qdict, "speed");
1940
1941     qmp_block_job_set_speed(device, value, &error);
1942
1943     hmp_handle_error(mon, &error);
1944 }
1945
1946 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1947 {
1948     Error *error = NULL;
1949     const char *device = qdict_get_str(qdict, "device");
1950     bool force = qdict_get_try_bool(qdict, "force", false);
1951
1952     qmp_block_job_cancel(device, true, force, &error);
1953
1954     hmp_handle_error(mon, &error);
1955 }
1956
1957 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1958 {
1959     Error *error = NULL;
1960     const char *device = qdict_get_str(qdict, "device");
1961
1962     qmp_block_job_pause(device, &error);
1963
1964     hmp_handle_error(mon, &error);
1965 }
1966
1967 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1968 {
1969     Error *error = NULL;
1970     const char *device = qdict_get_str(qdict, "device");
1971
1972     qmp_block_job_resume(device, &error);
1973
1974     hmp_handle_error(mon, &error);
1975 }
1976
1977 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1978 {
1979     Error *error = NULL;
1980     const char *device = qdict_get_str(qdict, "device");
1981
1982     qmp_block_job_complete(device, &error);
1983
1984     hmp_handle_error(mon, &error);
1985 }
1986
1987 typedef struct HMPMigrationStatus
1988 {
1989     QEMUTimer *timer;
1990     Monitor *mon;
1991     bool is_block_migration;
1992 } HMPMigrationStatus;
1993
1994 static void hmp_migrate_status_cb(void *opaque)
1995 {
1996     HMPMigrationStatus *status = opaque;
1997     MigrationInfo *info;
1998
1999     info = qmp_query_migrate(NULL);
2000     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
2001         info->status == MIGRATION_STATUS_SETUP) {
2002         if (info->has_disk) {
2003             int progress;
2004
2005             if (info->disk->remaining) {
2006                 progress = info->disk->transferred * 100 / info->disk->total;
2007             } else {
2008                 progress = 100;
2009             }
2010
2011             monitor_printf(status->mon, "Completed %d %%\r", progress);
2012             monitor_flush(status->mon);
2013         }
2014
2015         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
2016     } else {
2017         if (status->is_block_migration) {
2018             monitor_printf(status->mon, "\n");
2019         }
2020         if (info->has_error_desc) {
2021             error_report("%s", info->error_desc);
2022         }
2023         monitor_resume(status->mon);
2024         timer_del(status->timer);
2025         timer_free(status->timer);
2026         g_free(status);
2027     }
2028
2029     qapi_free_MigrationInfo(info);
2030 }
2031
2032 void hmp_migrate(Monitor *mon, const QDict *qdict)
2033 {
2034     bool detach = qdict_get_try_bool(qdict, "detach", false);
2035     bool blk = qdict_get_try_bool(qdict, "blk", false);
2036     bool inc = qdict_get_try_bool(qdict, "inc", false);
2037     bool resume = qdict_get_try_bool(qdict, "resume", false);
2038     const char *uri = qdict_get_str(qdict, "uri");
2039     Error *err = NULL;
2040
2041     qmp_migrate(uri, !!blk, blk, !!inc, inc,
2042                 false, false, true, resume, &err);
2043     if (err) {
2044         hmp_handle_error(mon, &err);
2045         return;
2046     }
2047
2048     if (!detach) {
2049         HMPMigrationStatus *status;
2050
2051         if (monitor_suspend(mon) < 0) {
2052             monitor_printf(mon, "terminal does not allow synchronous "
2053                            "migration, continuing detached\n");
2054             return;
2055         }
2056
2057         status = g_malloc0(sizeof(*status));
2058         status->mon = mon;
2059         status->is_block_migration = blk || inc;
2060         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
2061                                           status);
2062         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
2063     }
2064 }
2065
2066 void hmp_device_add(Monitor *mon, const QDict *qdict)
2067 {
2068     Error *err = NULL;
2069
2070     qmp_device_add((QDict *)qdict, NULL, &err);
2071     hmp_handle_error(mon, &err);
2072 }
2073
2074 void hmp_device_del(Monitor *mon, const QDict *qdict)
2075 {
2076     const char *id = qdict_get_str(qdict, "id");
2077     Error *err = NULL;
2078
2079     qmp_device_del(id, &err);
2080     hmp_handle_error(mon, &err);
2081 }
2082
2083 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
2084 {
2085     Error *err = NULL;
2086     bool win_dmp = qdict_get_try_bool(qdict, "windmp", false);
2087     bool paging = qdict_get_try_bool(qdict, "paging", false);
2088     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
2089     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
2090     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
2091     const char *file = qdict_get_str(qdict, "filename");
2092     bool has_begin = qdict_haskey(qdict, "begin");
2093     bool has_length = qdict_haskey(qdict, "length");
2094     bool has_detach = qdict_haskey(qdict, "detach");
2095     int64_t begin = 0;
2096     int64_t length = 0;
2097     bool detach = false;
2098     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
2099     char *prot;
2100
2101     if (zlib + lzo + snappy + win_dmp > 1) {
2102         error_setg(&err, "only one of '-z|-l|-s|-w' can be set");
2103         hmp_handle_error(mon, &err);
2104         return;
2105     }
2106
2107     if (win_dmp) {
2108         dump_format = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
2109     }
2110
2111     if (zlib) {
2112         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2113     }
2114
2115     if (lzo) {
2116         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2117     }
2118
2119     if (snappy) {
2120         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2121     }
2122
2123     if (has_begin) {
2124         begin = qdict_get_int(qdict, "begin");
2125     }
2126     if (has_length) {
2127         length = qdict_get_int(qdict, "length");
2128     }
2129     if (has_detach) {
2130         detach = qdict_get_bool(qdict, "detach");
2131     }
2132
2133     prot = g_strconcat("file:", file, NULL);
2134
2135     qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
2136                           has_length, length, true, dump_format, &err);
2137     hmp_handle_error(mon, &err);
2138     g_free(prot);
2139 }
2140
2141 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
2142 {
2143     Error *err = NULL;
2144     QemuOpts *opts;
2145
2146     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
2147     if (err) {
2148         goto out;
2149     }
2150
2151     netdev_add(opts, &err);
2152     if (err) {
2153         qemu_opts_del(opts);
2154     }
2155
2156 out:
2157     hmp_handle_error(mon, &err);
2158 }
2159
2160 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2161 {
2162     const char *id = qdict_get_str(qdict, "id");
2163     Error *err = NULL;
2164
2165     qmp_netdev_del(id, &err);
2166     hmp_handle_error(mon, &err);
2167 }
2168
2169 void hmp_object_add(Monitor *mon, const QDict *qdict)
2170 {
2171     Error *err = NULL;
2172     QemuOpts *opts;
2173     Object *obj = NULL;
2174
2175     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2176     if (err) {
2177         hmp_handle_error(mon, &err);
2178         return;
2179     }
2180
2181     obj = user_creatable_add_opts(opts, &err);
2182     qemu_opts_del(opts);
2183
2184     if (err) {
2185         hmp_handle_error(mon, &err);
2186     }
2187     if (obj) {
2188         object_unref(obj);
2189     }
2190 }
2191
2192 void hmp_getfd(Monitor *mon, const QDict *qdict)
2193 {
2194     const char *fdname = qdict_get_str(qdict, "fdname");
2195     Error *err = NULL;
2196
2197     qmp_getfd(fdname, &err);
2198     hmp_handle_error(mon, &err);
2199 }
2200
2201 void hmp_closefd(Monitor *mon, const QDict *qdict)
2202 {
2203     const char *fdname = qdict_get_str(qdict, "fdname");
2204     Error *err = NULL;
2205
2206     qmp_closefd(fdname, &err);
2207     hmp_handle_error(mon, &err);
2208 }
2209
2210 void hmp_sendkey(Monitor *mon, const QDict *qdict)
2211 {
2212     const char *keys = qdict_get_str(qdict, "keys");
2213     KeyValueList *keylist, *head = NULL, *tmp = NULL;
2214     int has_hold_time = qdict_haskey(qdict, "hold-time");
2215     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2216     Error *err = NULL;
2217     const char *separator;
2218     int keyname_len;
2219
2220     while (1) {
2221         separator = qemu_strchrnul(keys, '-');
2222         keyname_len = separator - keys;
2223
2224         /* Be compatible with old interface, convert user inputted "<" */
2225         if (keys[0] == '<' && keyname_len == 1) {
2226             keys = "less";
2227             keyname_len = 4;
2228         }
2229
2230         keylist = g_malloc0(sizeof(*keylist));
2231         keylist->value = g_malloc0(sizeof(*keylist->value));
2232
2233         if (!head) {
2234             head = keylist;
2235         }
2236         if (tmp) {
2237             tmp->next = keylist;
2238         }
2239         tmp = keylist;
2240
2241         if (strstart(keys, "0x", NULL)) {
2242             char *endp;
2243             int value = strtoul(keys, &endp, 0);
2244             assert(endp <= keys + keyname_len);
2245             if (endp != keys + keyname_len) {
2246                 goto err_out;
2247             }
2248             keylist->value->type = KEY_VALUE_KIND_NUMBER;
2249             keylist->value->u.number.data = value;
2250         } else {
2251             int idx = index_from_key(keys, keyname_len);
2252             if (idx == Q_KEY_CODE__MAX) {
2253                 goto err_out;
2254             }
2255             keylist->value->type = KEY_VALUE_KIND_QCODE;
2256             keylist->value->u.qcode.data = idx;
2257         }
2258
2259         if (!*separator) {
2260             break;
2261         }
2262         keys = separator + 1;
2263     }
2264
2265     qmp_send_key(head, has_hold_time, hold_time, &err);
2266     hmp_handle_error(mon, &err);
2267
2268 out:
2269     qapi_free_KeyValueList(head);
2270     return;
2271
2272 err_out:
2273     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
2274     goto out;
2275 }
2276
2277 void hmp_screendump(Monitor *mon, const QDict *qdict)
2278 {
2279     const char *filename = qdict_get_str(qdict, "filename");
2280     const char *id = qdict_get_try_str(qdict, "device");
2281     int64_t head = qdict_get_try_int(qdict, "head", 0);
2282     Error *err = NULL;
2283
2284     qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
2285     hmp_handle_error(mon, &err);
2286 }
2287
2288 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2289 {
2290     const char *uri = qdict_get_str(qdict, "uri");
2291     bool writable = qdict_get_try_bool(qdict, "writable", false);
2292     bool all = qdict_get_try_bool(qdict, "all", false);
2293     Error *local_err = NULL;
2294     BlockInfoList *block_list, *info;
2295     SocketAddress *addr;
2296
2297     if (writable && !all) {
2298         error_setg(&local_err, "-w only valid together with -a");
2299         goto exit;
2300     }
2301
2302     /* First check if the address is valid and start the server.  */
2303     addr = socket_parse(uri, &local_err);
2304     if (local_err != NULL) {
2305         goto exit;
2306     }
2307
2308     nbd_server_start(addr, NULL, &local_err);
2309     qapi_free_SocketAddress(addr);
2310     if (local_err != NULL) {
2311         goto exit;
2312     }
2313
2314     if (!all) {
2315         return;
2316     }
2317
2318     /* Then try adding all block devices.  If one fails, close all and
2319      * exit.
2320      */
2321     block_list = qmp_query_block(NULL);
2322
2323     for (info = block_list; info; info = info->next) {
2324         if (!info->value->has_inserted) {
2325             continue;
2326         }
2327
2328         qmp_nbd_server_add(info->value->device, false, NULL,
2329                            true, writable, &local_err);
2330
2331         if (local_err != NULL) {
2332             qmp_nbd_server_stop(NULL);
2333             break;
2334         }
2335     }
2336
2337     qapi_free_BlockInfoList(block_list);
2338
2339 exit:
2340     hmp_handle_error(mon, &local_err);
2341 }
2342
2343 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2344 {
2345     const char *device = qdict_get_str(qdict, "device");
2346     const char *name = qdict_get_try_str(qdict, "name");
2347     bool writable = qdict_get_try_bool(qdict, "writable", false);
2348     Error *local_err = NULL;
2349
2350     qmp_nbd_server_add(device, !!name, name, true, writable, &local_err);
2351     hmp_handle_error(mon, &local_err);
2352 }
2353
2354 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
2355 {
2356     const char *name = qdict_get_str(qdict, "name");
2357     bool force = qdict_get_try_bool(qdict, "force", false);
2358     Error *err = NULL;
2359
2360     /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
2361     qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
2362     hmp_handle_error(mon, &err);
2363 }
2364
2365 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2366 {
2367     Error *err = NULL;
2368
2369     qmp_nbd_server_stop(&err);
2370     hmp_handle_error(mon, &err);
2371 }
2372
2373 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2374 {
2375     int cpuid;
2376     Error *err = NULL;
2377
2378     error_report("cpu_add is deprecated, please use device_add instead");
2379
2380     cpuid = qdict_get_int(qdict, "id");
2381     qmp_cpu_add(cpuid, &err);
2382     hmp_handle_error(mon, &err);
2383 }
2384
2385 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2386 {
2387     const char *args = qdict_get_str(qdict, "args");
2388     Error *err = NULL;
2389     QemuOpts *opts;
2390
2391     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2392     if (opts == NULL) {
2393         error_setg(&err, "Parsing chardev args failed");
2394     } else {
2395         qemu_chr_new_from_opts(opts, &err);
2396         qemu_opts_del(opts);
2397     }
2398     hmp_handle_error(mon, &err);
2399 }
2400
2401 void hmp_chardev_change(Monitor *mon, const QDict *qdict)
2402 {
2403     const char *args = qdict_get_str(qdict, "args");
2404     const char *id;
2405     Error *err = NULL;
2406     ChardevBackend *backend = NULL;
2407     ChardevReturn *ret = NULL;
2408     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
2409                                              true);
2410     if (!opts) {
2411         error_setg(&err, "Parsing chardev args failed");
2412         goto end;
2413     }
2414
2415     id = qdict_get_str(qdict, "id");
2416     if (qemu_opts_id(opts)) {
2417         error_setg(&err, "Unexpected 'id' parameter");
2418         goto end;
2419     }
2420
2421     backend = qemu_chr_parse_opts(opts, &err);
2422     if (!backend) {
2423         goto end;
2424     }
2425
2426     ret = qmp_chardev_change(id, backend, &err);
2427
2428 end:
2429     qapi_free_ChardevReturn(ret);
2430     qapi_free_ChardevBackend(backend);
2431     qemu_opts_del(opts);
2432     hmp_handle_error(mon, &err);
2433 }
2434
2435 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2436 {
2437     Error *local_err = NULL;
2438
2439     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2440     hmp_handle_error(mon, &local_err);
2441 }
2442
2443 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2444 {
2445     Error *local_err = NULL;
2446
2447     qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2448     hmp_handle_error(mon, &local_err);
2449 }
2450
2451 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2452 {
2453     BlockBackend *blk;
2454     BlockBackend *local_blk = NULL;
2455     const char* device = qdict_get_str(qdict, "device");
2456     const char* command = qdict_get_str(qdict, "command");
2457     Error *err = NULL;
2458     int ret;
2459
2460     blk = blk_by_name(device);
2461     if (!blk) {
2462         BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2463         if (bs) {
2464             blk = local_blk = blk_new(0, BLK_PERM_ALL);
2465             ret = blk_insert_bs(blk, bs, &err);
2466             if (ret < 0) {
2467                 goto fail;
2468             }
2469         } else {
2470             goto fail;
2471         }
2472     }
2473
2474     /*
2475      * Notably absent: Proper permission management. This is sad, but it seems
2476      * almost impossible to achieve without changing the semantics and thereby
2477      * limiting the use cases of the qemu-io HMP command.
2478      *
2479      * In an ideal world we would unconditionally create a new BlockBackend for
2480      * qemuio_command(), but we have commands like 'reopen' and want them to
2481      * take effect on the exact BlockBackend whose name the user passed instead
2482      * of just on a temporary copy of it.
2483      *
2484      * Another problem is that deleting the temporary BlockBackend involves
2485      * draining all requests on it first, but some qemu-iotests cases want to
2486      * issue multiple aio_read/write requests and expect them to complete in
2487      * the background while the monitor has already returned.
2488      *
2489      * This is also what prevents us from saving the original permissions and
2490      * restoring them later: We can't revoke permissions until all requests
2491      * have completed, and we don't know when that is nor can we really let
2492      * anything else run before we have revoken them to avoid race conditions.
2493      *
2494      * What happens now is that command() in qemu-io-cmds.c can extend the
2495      * permissions if necessary for the qemu-io command. And they simply stay
2496      * extended, possibly resulting in a read-only guest device keeping write
2497      * permissions. Ugly, but it appears to be the lesser evil.
2498      */
2499     qemuio_command(blk, command);
2500
2501 fail:
2502     blk_unref(local_blk);
2503     hmp_handle_error(mon, &err);
2504 }
2505
2506 void hmp_object_del(Monitor *mon, const QDict *qdict)
2507 {
2508     const char *id = qdict_get_str(qdict, "id");
2509     Error *err = NULL;
2510
2511     user_creatable_del(id, &err);
2512     hmp_handle_error(mon, &err);
2513 }
2514
2515 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2516 {
2517     Error *err = NULL;
2518     MemdevList *memdev_list = qmp_query_memdev(&err);
2519     MemdevList *m = memdev_list;
2520     Visitor *v;
2521     char *str;
2522
2523     while (m) {
2524         v = string_output_visitor_new(false, &str);
2525         visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2526         monitor_printf(mon, "memory backend: %s\n", m->value->id);
2527         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
2528         monitor_printf(mon, "  merge: %s\n",
2529                        m->value->merge ? "true" : "false");
2530         monitor_printf(mon, "  dump: %s\n",
2531                        m->value->dump ? "true" : "false");
2532         monitor_printf(mon, "  prealloc: %s\n",
2533                        m->value->prealloc ? "true" : "false");
2534         monitor_printf(mon, "  policy: %s\n",
2535                        HostMemPolicy_str(m->value->policy));
2536         visit_complete(v, &str);
2537         monitor_printf(mon, "  host nodes: %s\n", str);
2538
2539         g_free(str);
2540         visit_free(v);
2541         m = m->next;
2542     }
2543
2544     monitor_printf(mon, "\n");
2545
2546     qapi_free_MemdevList(memdev_list);
2547     hmp_handle_error(mon, &err);
2548 }
2549
2550 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2551 {
2552     Error *err = NULL;
2553     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2554     MemoryDeviceInfoList *info;
2555     MemoryDeviceInfo *value;
2556     PCDIMMDeviceInfo *di;
2557
2558     for (info = info_list; info; info = info->next) {
2559         value = info->value;
2560
2561         if (value) {
2562             switch (value->type) {
2563             case MEMORY_DEVICE_INFO_KIND_DIMM:
2564                 di = value->u.dimm.data;
2565                 break;
2566
2567             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
2568                 di = value->u.nvdimm.data;
2569                 break;
2570
2571             default:
2572                 di = NULL;
2573                 break;
2574             }
2575
2576             if (di) {
2577                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2578                                MemoryDeviceInfoKind_str(value->type),
2579                                di->id ? di->id : "");
2580                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
2581                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
2582                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
2583                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
2584                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
2585                 monitor_printf(mon, "  hotplugged: %s\n",
2586                                di->hotplugged ? "true" : "false");
2587                 monitor_printf(mon, "  hotpluggable: %s\n",
2588                                di->hotpluggable ? "true" : "false");
2589             }
2590         }
2591     }
2592
2593     qapi_free_MemoryDeviceInfoList(info_list);
2594     hmp_handle_error(mon, &err);
2595 }
2596
2597 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2598 {
2599     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2600     IOThreadInfoList *info;
2601     IOThreadInfo *value;
2602
2603     for (info = info_list; info; info = info->next) {
2604         value = info->value;
2605         monitor_printf(mon, "%s:\n", value->id);
2606         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
2607         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2608         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
2609         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
2610     }
2611
2612     qapi_free_IOThreadInfoList(info_list);
2613 }
2614
2615 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2616 {
2617     const char *path = qdict_get_try_str(qdict, "path");
2618     ObjectPropertyInfoList *list;
2619     Error *err = NULL;
2620
2621     if (path == NULL) {
2622         monitor_printf(mon, "/\n");
2623         return;
2624     }
2625
2626     list = qmp_qom_list(path, &err);
2627     if (err == NULL) {
2628         ObjectPropertyInfoList *start = list;
2629         while (list != NULL) {
2630             ObjectPropertyInfo *value = list->value;
2631
2632             monitor_printf(mon, "%s (%s)\n",
2633                            value->name, value->type);
2634             list = list->next;
2635         }
2636         qapi_free_ObjectPropertyInfoList(start);
2637     }
2638     hmp_handle_error(mon, &err);
2639 }
2640
2641 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2642 {
2643     const char *path = qdict_get_str(qdict, "path");
2644     const char *property = qdict_get_str(qdict, "property");
2645     const char *value = qdict_get_str(qdict, "value");
2646     Error *err = NULL;
2647     bool ambiguous = false;
2648     Object *obj;
2649
2650     obj = object_resolve_path(path, &ambiguous);
2651     if (obj == NULL) {
2652         error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2653                   "Device '%s' not found", path);
2654     } else {
2655         if (ambiguous) {
2656             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2657         }
2658         object_property_parse(obj, value, property, &err);
2659     }
2660     hmp_handle_error(mon, &err);
2661 }
2662
2663 void hmp_rocker(Monitor *mon, const QDict *qdict)
2664 {
2665     const char *name = qdict_get_str(qdict, "name");
2666     RockerSwitch *rocker;
2667     Error *err = NULL;
2668
2669     rocker = qmp_query_rocker(name, &err);
2670     if (err != NULL) {
2671         hmp_handle_error(mon, &err);
2672         return;
2673     }
2674
2675     monitor_printf(mon, "name: %s\n", rocker->name);
2676     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2677     monitor_printf(mon, "ports: %d\n", rocker->ports);
2678
2679     qapi_free_RockerSwitch(rocker);
2680 }
2681
2682 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2683 {
2684     RockerPortList *list, *port;
2685     const char *name = qdict_get_str(qdict, "name");
2686     Error *err = NULL;
2687
2688     list = qmp_query_rocker_ports(name, &err);
2689     if (err != NULL) {
2690         hmp_handle_error(mon, &err);
2691         return;
2692     }
2693
2694     monitor_printf(mon, "            ena/    speed/ auto\n");
2695     monitor_printf(mon, "      port  link    duplex neg?\n");
2696
2697     for (port = list; port; port = port->next) {
2698         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
2699                        port->value->name,
2700                        port->value->enabled ? port->value->link_up ?
2701                        "up" : "down" : "!ena",
2702                        port->value->speed == 10000 ? "10G" : "??",
2703                        port->value->duplex ? "FD" : "HD",
2704                        port->value->autoneg ? "Yes" : "No");
2705     }
2706
2707     qapi_free_RockerPortList(list);
2708 }
2709
2710 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2711 {
2712     RockerOfDpaFlowList *list, *info;
2713     const char *name = qdict_get_str(qdict, "name");
2714     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2715     Error *err = NULL;
2716
2717     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2718     if (err != NULL) {
2719         hmp_handle_error(mon, &err);
2720         return;
2721     }
2722
2723     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2724
2725     for (info = list; info; info = info->next) {
2726         RockerOfDpaFlow *flow = info->value;
2727         RockerOfDpaFlowKey *key = flow->key;
2728         RockerOfDpaFlowMask *mask = flow->mask;
2729         RockerOfDpaFlowAction *action = flow->action;
2730
2731         if (flow->hits) {
2732             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2733                            key->priority, key->tbl_id, flow->hits);
2734         } else {
2735             monitor_printf(mon, "%-4d %-3d     ",
2736                            key->priority, key->tbl_id);
2737         }
2738
2739         if (key->has_in_pport) {
2740             monitor_printf(mon, " pport %d", key->in_pport);
2741             if (mask->has_in_pport) {
2742                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2743             }
2744         }
2745
2746         if (key->has_vlan_id) {
2747             monitor_printf(mon, " vlan %d",
2748                            key->vlan_id & VLAN_VID_MASK);
2749             if (mask->has_vlan_id) {
2750                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2751             }
2752         }
2753
2754         if (key->has_tunnel_id) {
2755             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2756             if (mask->has_tunnel_id) {
2757                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2758             }
2759         }
2760
2761         if (key->has_eth_type) {
2762             switch (key->eth_type) {
2763             case 0x0806:
2764                 monitor_printf(mon, " ARP");
2765                 break;
2766             case 0x0800:
2767                 monitor_printf(mon, " IP");
2768                 break;
2769             case 0x86dd:
2770                 monitor_printf(mon, " IPv6");
2771                 break;
2772             case 0x8809:
2773                 monitor_printf(mon, " LACP");
2774                 break;
2775             case 0x88cc:
2776                 monitor_printf(mon, " LLDP");
2777                 break;
2778             default:
2779                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2780                 break;
2781             }
2782         }
2783
2784         if (key->has_eth_src) {
2785             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2786                 (mask->has_eth_src) &&
2787                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2788                 monitor_printf(mon, " src <any mcast/bcast>");
2789             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2790                 (mask->has_eth_src) &&
2791                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2792                 monitor_printf(mon, " src <any ucast>");
2793             } else {
2794                 monitor_printf(mon, " src %s", key->eth_src);
2795                 if (mask->has_eth_src) {
2796                     monitor_printf(mon, "(%s)", mask->eth_src);
2797                 }
2798             }
2799         }
2800
2801         if (key->has_eth_dst) {
2802             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2803                 (mask->has_eth_dst) &&
2804                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2805                 monitor_printf(mon, " dst <any mcast/bcast>");
2806             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2807                 (mask->has_eth_dst) &&
2808                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2809                 monitor_printf(mon, " dst <any ucast>");
2810             } else {
2811                 monitor_printf(mon, " dst %s", key->eth_dst);
2812                 if (mask->has_eth_dst) {
2813                     monitor_printf(mon, "(%s)", mask->eth_dst);
2814                 }
2815             }
2816         }
2817
2818         if (key->has_ip_proto) {
2819             monitor_printf(mon, " proto %d", key->ip_proto);
2820             if (mask->has_ip_proto) {
2821                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2822             }
2823         }
2824
2825         if (key->has_ip_tos) {
2826             monitor_printf(mon, " TOS %d", key->ip_tos);
2827             if (mask->has_ip_tos) {
2828                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2829             }
2830         }
2831
2832         if (key->has_ip_dst) {
2833             monitor_printf(mon, " dst %s", key->ip_dst);
2834         }
2835
2836         if (action->has_goto_tbl || action->has_group_id ||
2837             action->has_new_vlan_id) {
2838             monitor_printf(mon, " -->");
2839         }
2840
2841         if (action->has_new_vlan_id) {
2842             monitor_printf(mon, " apply new vlan %d",
2843                            ntohs(action->new_vlan_id));
2844         }
2845
2846         if (action->has_group_id) {
2847             monitor_printf(mon, " write group 0x%08x", action->group_id);
2848         }
2849
2850         if (action->has_goto_tbl) {
2851             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2852         }
2853
2854         monitor_printf(mon, "\n");
2855     }
2856
2857     qapi_free_RockerOfDpaFlowList(list);
2858 }
2859
2860 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2861 {
2862     RockerOfDpaGroupList *list, *g;
2863     const char *name = qdict_get_str(qdict, "name");
2864     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2865     Error *err = NULL;
2866     bool set = false;
2867
2868     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2869     if (err != NULL) {
2870         hmp_handle_error(mon, &err);
2871         return;
2872     }
2873
2874     monitor_printf(mon, "id (decode) --> buckets\n");
2875
2876     for (g = list; g; g = g->next) {
2877         RockerOfDpaGroup *group = g->value;
2878
2879         monitor_printf(mon, "0x%08x", group->id);
2880
2881         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2882                                          group->type == 1 ? "L2 rewrite" :
2883                                          group->type == 2 ? "L3 unicast" :
2884                                          group->type == 3 ? "L2 multicast" :
2885                                          group->type == 4 ? "L2 flood" :
2886                                          group->type == 5 ? "L3 interface" :
2887                                          group->type == 6 ? "L3 multicast" :
2888                                          group->type == 7 ? "L3 ECMP" :
2889                                          group->type == 8 ? "L2 overlay" :
2890                                          "unknown");
2891
2892         if (group->has_vlan_id) {
2893             monitor_printf(mon, " vlan %d", group->vlan_id);
2894         }
2895
2896         if (group->has_pport) {
2897             monitor_printf(mon, " pport %d", group->pport);
2898         }
2899
2900         if (group->has_index) {
2901             monitor_printf(mon, " index %d", group->index);
2902         }
2903
2904         monitor_printf(mon, ") -->");
2905
2906         if (group->has_set_vlan_id && group->set_vlan_id) {
2907             set = true;
2908             monitor_printf(mon, " set vlan %d",
2909                            group->set_vlan_id & VLAN_VID_MASK);
2910         }
2911
2912         if (group->has_set_eth_src) {
2913             if (!set) {
2914                 set = true;
2915                 monitor_printf(mon, " set");
2916             }
2917             monitor_printf(mon, " src %s", group->set_eth_src);
2918         }
2919
2920         if (group->has_set_eth_dst) {
2921             if (!set) {
2922                 set = true;
2923                 monitor_printf(mon, " set");
2924             }
2925             monitor_printf(mon, " dst %s", group->set_eth_dst);
2926         }
2927
2928         set = false;
2929
2930         if (group->has_ttl_check && group->ttl_check) {
2931             monitor_printf(mon, " check TTL");
2932         }
2933
2934         if (group->has_group_id && group->group_id) {
2935             monitor_printf(mon, " group id 0x%08x", group->group_id);
2936         }
2937
2938         if (group->has_pop_vlan && group->pop_vlan) {
2939             monitor_printf(mon, " pop vlan");
2940         }
2941
2942         if (group->has_out_pport) {
2943             monitor_printf(mon, " out pport %d", group->out_pport);
2944         }
2945
2946         if (group->has_group_ids) {
2947             struct uint32List *id;
2948
2949             monitor_printf(mon, " groups [");
2950             for (id = group->group_ids; id; id = id->next) {
2951                 monitor_printf(mon, "0x%08x", id->value);
2952                 if (id->next) {
2953                     monitor_printf(mon, ",");
2954                 }
2955             }
2956             monitor_printf(mon, "]");
2957         }
2958
2959         monitor_printf(mon, "\n");
2960     }
2961
2962     qapi_free_RockerOfDpaGroupList(list);
2963 }
2964
2965 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2966 {
2967     DumpQueryResult *result = qmp_query_dump(NULL);
2968
2969     assert(result && result->status < DUMP_STATUS__MAX);
2970     monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
2971
2972     if (result->status == DUMP_STATUS_ACTIVE) {
2973         float percent = 0;
2974         assert(result->total != 0);
2975         percent = 100.0 * result->completed / result->total;
2976         monitor_printf(mon, "Finished: %.2f %%\n", percent);
2977     }
2978
2979     qapi_free_DumpQueryResult(result);
2980 }
2981
2982 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2983 {
2984     ram_block_dump(mon);
2985 }
2986
2987 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2988 {
2989     Error *err = NULL;
2990     HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2991     HotpluggableCPUList *saved = l;
2992     CpuInstanceProperties *c;
2993
2994     if (err != NULL) {
2995         hmp_handle_error(mon, &err);
2996         return;
2997     }
2998
2999     monitor_printf(mon, "Hotpluggable CPUs:\n");
3000     while (l) {
3001         monitor_printf(mon, "  type: \"%s\"\n", l->value->type);
3002         monitor_printf(mon, "  vcpus_count: \"%" PRIu64 "\"\n",
3003                        l->value->vcpus_count);
3004         if (l->value->has_qom_path) {
3005             monitor_printf(mon, "  qom_path: \"%s\"\n", l->value->qom_path);
3006         }
3007
3008         c = l->value->props;
3009         monitor_printf(mon, "  CPUInstance Properties:\n");
3010         if (c->has_node_id) {
3011             monitor_printf(mon, "    node-id: \"%" PRIu64 "\"\n", c->node_id);
3012         }
3013         if (c->has_socket_id) {
3014             monitor_printf(mon, "    socket-id: \"%" PRIu64 "\"\n", c->socket_id);
3015         }
3016         if (c->has_core_id) {
3017             monitor_printf(mon, "    core-id: \"%" PRIu64 "\"\n", c->core_id);
3018         }
3019         if (c->has_thread_id) {
3020             monitor_printf(mon, "    thread-id: \"%" PRIu64 "\"\n", c->thread_id);
3021         }
3022
3023         l = l->next;
3024     }
3025
3026     qapi_free_HotpluggableCPUList(saved);
3027 }
3028
3029 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
3030 {
3031     Error *err = NULL;
3032     GuidInfo *info = qmp_query_vm_generation_id(&err);
3033     if (info) {
3034         monitor_printf(mon, "%s\n", info->guid);
3035     }
3036     hmp_handle_error(mon, &err);
3037     qapi_free_GuidInfo(info);
3038 }
3039
3040 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
3041 {
3042     Error *err = NULL;
3043     MemoryInfo *info = qmp_query_memory_size_summary(&err);
3044     if (info) {
3045         monitor_printf(mon, "base memory: %" PRIu64 "\n",
3046                        info->base_memory);
3047
3048         if (info->has_plugged_memory) {
3049             monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
3050                            info->plugged_memory);
3051         }
3052
3053         qapi_free_MemoryInfo(info);
3054     }
3055     hmp_handle_error(mon, &err);
3056 }
This page took 0.20385 seconds and 4 git commands to generate.