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