OSDN Git Service

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