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