monitor: Convert client_migrate_info to QAPI
[sdk/emulator/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 "hmp.h"
17 #include "net/net.h"
18 #include "sysemu/char.h"
19 #include "sysemu/block-backend.h"
20 #include "qemu/option.h"
21 #include "qemu/timer.h"
22 #include "qmp-commands.h"
23 #include "qemu/sockets.h"
24 #include "monitor/monitor.h"
25 #include "qapi/opts-visitor.h"
26 #include "qapi/string-output-visitor.h"
27 #include "qapi-visit.h"
28 #include "ui/console.h"
29 #include "block/qapi.h"
30 #include "qemu-io.h"
31
32 #ifdef CONFIG_SPICE
33 #include <spice/enums.h>
34 #endif
35
36 static void hmp_handle_error(Monitor *mon, Error **errp)
37 {
38     assert(errp);
39     if (*errp) {
40         monitor_printf(mon, "%s\n", error_get_pretty(*errp));
41         error_free(*errp);
42     }
43 }
44
45 void hmp_info_name(Monitor *mon, const QDict *qdict)
46 {
47     NameInfo *info;
48
49     info = qmp_query_name(NULL);
50     if (info->has_name) {
51         monitor_printf(mon, "%s\n", info->name);
52     }
53     qapi_free_NameInfo(info);
54 }
55
56 void hmp_info_version(Monitor *mon, const QDict *qdict)
57 {
58     VersionInfo *info;
59
60     info = qmp_query_version(NULL);
61
62     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
63                    info->qemu->major, info->qemu->minor, info->qemu->micro,
64                    info->package);
65
66     qapi_free_VersionInfo(info);
67 }
68
69 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
70 {
71     KvmInfo *info;
72
73     info = qmp_query_kvm(NULL);
74     monitor_printf(mon, "kvm support: ");
75     if (info->present) {
76         monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
77     } else {
78         monitor_printf(mon, "not compiled\n");
79     }
80
81     qapi_free_KvmInfo(info);
82 }
83
84 void hmp_info_status(Monitor *mon, const QDict *qdict)
85 {
86     StatusInfo *info;
87
88     info = qmp_query_status(NULL);
89
90     monitor_printf(mon, "VM status: %s%s",
91                    info->running ? "running" : "paused",
92                    info->singlestep ? " (single step mode)" : "");
93
94     if (!info->running && info->status != RUN_STATE_PAUSED) {
95         monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
96     }
97
98     monitor_printf(mon, "\n");
99
100     qapi_free_StatusInfo(info);
101 }
102
103 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
104 {
105     UuidInfo *info;
106
107     info = qmp_query_uuid(NULL);
108     monitor_printf(mon, "%s\n", info->UUID);
109     qapi_free_UuidInfo(info);
110 }
111
112 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
113 {
114     ChardevInfoList *char_info, *info;
115
116     char_info = qmp_query_chardev(NULL);
117     for (info = char_info; info; info = info->next) {
118         monitor_printf(mon, "%s: filename=%s\n", info->value->label,
119                                                  info->value->filename);
120     }
121
122     qapi_free_ChardevInfoList(char_info);
123 }
124
125 void hmp_info_mice(Monitor *mon, const QDict *qdict)
126 {
127     MouseInfoList *mice_list, *mouse;
128
129     mice_list = qmp_query_mice(NULL);
130     if (!mice_list) {
131         monitor_printf(mon, "No mouse devices connected\n");
132         return;
133     }
134
135     for (mouse = mice_list; mouse; mouse = mouse->next) {
136         monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
137                        mouse->value->current ? '*' : ' ',
138                        mouse->value->index, mouse->value->name,
139                        mouse->value->absolute ? " (absolute)" : "");
140     }
141
142     qapi_free_MouseInfoList(mice_list);
143 }
144
145 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
146 {
147     MigrationInfo *info;
148     MigrationCapabilityStatusList *caps, *cap;
149
150     info = qmp_query_migrate(NULL);
151     caps = qmp_query_migrate_capabilities(NULL);
152
153     /* do not display parameters during setup */
154     if (info->has_status && caps) {
155         monitor_printf(mon, "capabilities: ");
156         for (cap = caps; cap; cap = cap->next) {
157             monitor_printf(mon, "%s: %s ",
158                            MigrationCapability_lookup[cap->value->capability],
159                            cap->value->state ? "on" : "off");
160         }
161         monitor_printf(mon, "\n");
162     }
163
164     if (info->has_status) {
165         monitor_printf(mon, "Migration status: %s\n",
166                        MigrationStatus_lookup[info->status]);
167         monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
168                        info->total_time);
169         if (info->has_expected_downtime) {
170             monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
171                            info->expected_downtime);
172         }
173         if (info->has_downtime) {
174             monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
175                            info->downtime);
176         }
177         if (info->has_setup_time) {
178             monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
179                            info->setup_time);
180         }
181     }
182
183     if (info->has_ram) {
184         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
185                        info->ram->transferred >> 10);
186         monitor_printf(mon, "throughput: %0.2f mbps\n",
187                        info->ram->mbps);
188         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
189                        info->ram->remaining >> 10);
190         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
191                        info->ram->total >> 10);
192         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
193                        info->ram->duplicate);
194         monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
195                        info->ram->skipped);
196         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
197                        info->ram->normal);
198         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
199                        info->ram->normal_bytes >> 10);
200         monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
201                        info->ram->dirty_sync_count);
202         if (info->ram->dirty_pages_rate) {
203             monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
204                            info->ram->dirty_pages_rate);
205         }
206     }
207
208     if (info->has_disk) {
209         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
210                        info->disk->transferred >> 10);
211         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
212                        info->disk->remaining >> 10);
213         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
214                        info->disk->total >> 10);
215     }
216
217     if (info->has_xbzrle_cache) {
218         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
219                        info->xbzrle_cache->cache_size);
220         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
221                        info->xbzrle_cache->bytes >> 10);
222         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
223                        info->xbzrle_cache->pages);
224         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
225                        info->xbzrle_cache->cache_miss);
226         monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
227                        info->xbzrle_cache->cache_miss_rate);
228         monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
229                        info->xbzrle_cache->overflow);
230     }
231
232     qapi_free_MigrationInfo(info);
233     qapi_free_MigrationCapabilityStatusList(caps);
234 }
235
236 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
237 {
238     MigrationCapabilityStatusList *caps, *cap;
239
240     caps = qmp_query_migrate_capabilities(NULL);
241
242     if (caps) {
243         monitor_printf(mon, "capabilities: ");
244         for (cap = caps; cap; cap = cap->next) {
245             monitor_printf(mon, "%s: %s ",
246                            MigrationCapability_lookup[cap->value->capability],
247                            cap->value->state ? "on" : "off");
248         }
249         monitor_printf(mon, "\n");
250     }
251
252     qapi_free_MigrationCapabilityStatusList(caps);
253 }
254
255 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
256 {
257     MigrationParameters *params;
258
259     params = qmp_query_migrate_parameters(NULL);
260
261     if (params) {
262         monitor_printf(mon, "parameters:");
263         monitor_printf(mon, " %s: %" PRId64,
264             MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
265             params->compress_level);
266         monitor_printf(mon, " %s: %" PRId64,
267             MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
268             params->compress_threads);
269         monitor_printf(mon, " %s: %" PRId64,
270             MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
271             params->decompress_threads);
272         monitor_printf(mon, "\n");
273     }
274
275     qapi_free_MigrationParameters(params);
276 }
277
278 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
279 {
280     monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
281                    qmp_query_migrate_cache_size(NULL) >> 10);
282 }
283
284 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
285 {
286     CpuInfoList *cpu_list, *cpu;
287
288     cpu_list = qmp_query_cpus(NULL);
289
290     for (cpu = cpu_list; cpu; cpu = cpu->next) {
291         int active = ' ';
292
293         if (cpu->value->CPU == monitor_get_cpu_index()) {
294             active = '*';
295         }
296
297         monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
298
299         if (cpu->value->has_pc) {
300             monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
301         }
302         if (cpu->value->has_nip) {
303             monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
304         }
305         if (cpu->value->has_npc) {
306             monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
307         }
308         if (cpu->value->has_PC) {
309             monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
310         }
311
312         if (cpu->value->halted) {
313             monitor_printf(mon, " (halted)");
314         }
315
316         monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
317     }
318
319     qapi_free_CpuInfoList(cpu_list);
320 }
321
322 static void print_block_info(Monitor *mon, BlockInfo *info,
323                              BlockDeviceInfo *inserted, bool verbose)
324 {
325     ImageInfo *image_info;
326
327     assert(!info || !info->has_inserted || info->inserted == inserted);
328
329     if (info) {
330         monitor_printf(mon, "%s", info->device);
331         if (inserted && inserted->has_node_name) {
332             monitor_printf(mon, " (%s)", inserted->node_name);
333         }
334     } else {
335         assert(inserted);
336         monitor_printf(mon, "%s",
337                        inserted->has_node_name
338                        ? inserted->node_name
339                        : "<anonymous>");
340     }
341
342     if (inserted) {
343         monitor_printf(mon, ": %s (%s%s%s)\n",
344                        inserted->file,
345                        inserted->drv,
346                        inserted->ro ? ", read-only" : "",
347                        inserted->encrypted ? ", encrypted" : "");
348     } else {
349         monitor_printf(mon, ": [not inserted]\n");
350     }
351
352     if (info) {
353         if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
354             monitor_printf(mon, "    I/O status:       %s\n",
355                            BlockDeviceIoStatus_lookup[info->io_status]);
356         }
357
358         if (info->removable) {
359             monitor_printf(mon, "    Removable device: %slocked, tray %s\n",
360                            info->locked ? "" : "not ",
361                            info->tray_open ? "open" : "closed");
362         }
363     }
364
365
366     if (!inserted) {
367         return;
368     }
369
370     monitor_printf(mon, "    Cache mode:       %s%s%s\n",
371                    inserted->cache->writeback ? "writeback" : "writethrough",
372                    inserted->cache->direct ? ", direct" : "",
373                    inserted->cache->no_flush ? ", ignore flushes" : "");
374
375     if (inserted->has_backing_file) {
376         monitor_printf(mon,
377                        "    Backing file:     %s "
378                        "(chain depth: %" PRId64 ")\n",
379                        inserted->backing_file,
380                        inserted->backing_file_depth);
381     }
382
383     if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
384         monitor_printf(mon, "    Detect zeroes:    %s\n",
385                        BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
386     }
387
388     if (inserted->bps  || inserted->bps_rd  || inserted->bps_wr  ||
389         inserted->iops || inserted->iops_rd || inserted->iops_wr)
390     {
391         monitor_printf(mon, "    I/O throttling:   bps=%" PRId64
392                         " bps_rd=%" PRId64  " bps_wr=%" PRId64
393                         " bps_max=%" PRId64
394                         " bps_rd_max=%" PRId64
395                         " bps_wr_max=%" PRId64
396                         " iops=%" PRId64 " iops_rd=%" PRId64
397                         " iops_wr=%" PRId64
398                         " iops_max=%" PRId64
399                         " iops_rd_max=%" PRId64
400                         " iops_wr_max=%" PRId64
401                         " iops_size=%" PRId64 "\n",
402                         inserted->bps,
403                         inserted->bps_rd,
404                         inserted->bps_wr,
405                         inserted->bps_max,
406                         inserted->bps_rd_max,
407                         inserted->bps_wr_max,
408                         inserted->iops,
409                         inserted->iops_rd,
410                         inserted->iops_wr,
411                         inserted->iops_max,
412                         inserted->iops_rd_max,
413                         inserted->iops_wr_max,
414                         inserted->iops_size);
415     }
416
417     if (verbose) {
418         monitor_printf(mon, "\nImages:\n");
419         image_info = inserted->image;
420         while (1) {
421                 bdrv_image_info_dump((fprintf_function)monitor_printf,
422                                      mon, image_info);
423             if (image_info->has_backing_image) {
424                 image_info = image_info->backing_image;
425             } else {
426                 break;
427             }
428         }
429     }
430 }
431
432 void hmp_info_block(Monitor *mon, const QDict *qdict)
433 {
434     BlockInfoList *block_list, *info;
435     BlockDeviceInfoList *blockdev_list, *blockdev;
436     const char *device = qdict_get_try_str(qdict, "device");
437     bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
438     bool nodes = qdict_get_try_bool(qdict, "nodes", 0);
439     bool printed = false;
440
441     /* Print BlockBackend information */
442     if (!nodes) {
443         block_list = qmp_query_block(NULL);
444     } else {
445         block_list = NULL;
446     }
447
448     for (info = block_list; info; info = info->next) {
449         if (device && strcmp(device, info->value->device)) {
450             continue;
451         }
452
453         if (info != block_list) {
454             monitor_printf(mon, "\n");
455         }
456
457         print_block_info(mon, info->value, info->value->has_inserted
458                                            ? info->value->inserted : NULL,
459                          verbose);
460         printed = true;
461     }
462
463     qapi_free_BlockInfoList(block_list);
464
465     if ((!device && !nodes) || printed) {
466         return;
467     }
468
469     /* Print node information */
470     blockdev_list = qmp_query_named_block_nodes(NULL);
471     for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
472         assert(blockdev->value->has_node_name);
473         if (device && strcmp(device, blockdev->value->node_name)) {
474             continue;
475         }
476
477         if (blockdev != blockdev_list) {
478             monitor_printf(mon, "\n");
479         }
480
481         print_block_info(mon, NULL, blockdev->value, verbose);
482     }
483     qapi_free_BlockDeviceInfoList(blockdev_list);
484 }
485
486 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
487 {
488     BlockStatsList *stats_list, *stats;
489
490     stats_list = qmp_query_blockstats(false, false, NULL);
491
492     for (stats = stats_list; stats; stats = stats->next) {
493         if (!stats->value->has_device) {
494             continue;
495         }
496
497         monitor_printf(mon, "%s:", stats->value->device);
498         monitor_printf(mon, " rd_bytes=%" PRId64
499                        " wr_bytes=%" PRId64
500                        " rd_operations=%" PRId64
501                        " wr_operations=%" PRId64
502                        " flush_operations=%" PRId64
503                        " wr_total_time_ns=%" PRId64
504                        " rd_total_time_ns=%" PRId64
505                        " flush_total_time_ns=%" PRId64
506                        " rd_merged=%" PRId64
507                        " wr_merged=%" PRId64
508                        "\n",
509                        stats->value->stats->rd_bytes,
510                        stats->value->stats->wr_bytes,
511                        stats->value->stats->rd_operations,
512                        stats->value->stats->wr_operations,
513                        stats->value->stats->flush_operations,
514                        stats->value->stats->wr_total_time_ns,
515                        stats->value->stats->rd_total_time_ns,
516                        stats->value->stats->flush_total_time_ns,
517                        stats->value->stats->rd_merged,
518                        stats->value->stats->wr_merged);
519     }
520
521     qapi_free_BlockStatsList(stats_list);
522 }
523
524 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
525 {
526     VncInfo *info;
527     Error *err = NULL;
528     VncClientInfoList *client;
529
530     info = qmp_query_vnc(&err);
531     if (err) {
532         monitor_printf(mon, "%s\n", error_get_pretty(err));
533         error_free(err);
534         return;
535     }
536
537     if (!info->enabled) {
538         monitor_printf(mon, "Server: disabled\n");
539         goto out;
540     }
541
542     monitor_printf(mon, "Server:\n");
543     if (info->has_host && info->has_service) {
544         monitor_printf(mon, "     address: %s:%s\n", info->host, info->service);
545     }
546     if (info->has_auth) {
547         monitor_printf(mon, "        auth: %s\n", info->auth);
548     }
549
550     if (!info->has_clients || info->clients == NULL) {
551         monitor_printf(mon, "Client: none\n");
552     } else {
553         for (client = info->clients; client; client = client->next) {
554             monitor_printf(mon, "Client:\n");
555             monitor_printf(mon, "     address: %s:%s\n",
556                            client->value->base->host,
557                            client->value->base->service);
558             monitor_printf(mon, "  x509_dname: %s\n",
559                            client->value->x509_dname ?
560                            client->value->x509_dname : "none");
561             monitor_printf(mon, "    username: %s\n",
562                            client->value->has_sasl_username ?
563                            client->value->sasl_username : "none");
564         }
565     }
566
567 out:
568     qapi_free_VncInfo(info);
569 }
570
571 #ifdef CONFIG_SPICE
572 void hmp_info_spice(Monitor *mon, const QDict *qdict)
573 {
574     SpiceChannelList *chan;
575     SpiceInfo *info;
576     const char *channel_name;
577     const char * const channel_names[] = {
578         [SPICE_CHANNEL_MAIN] = "main",
579         [SPICE_CHANNEL_DISPLAY] = "display",
580         [SPICE_CHANNEL_INPUTS] = "inputs",
581         [SPICE_CHANNEL_CURSOR] = "cursor",
582         [SPICE_CHANNEL_PLAYBACK] = "playback",
583         [SPICE_CHANNEL_RECORD] = "record",
584         [SPICE_CHANNEL_TUNNEL] = "tunnel",
585         [SPICE_CHANNEL_SMARTCARD] = "smartcard",
586         [SPICE_CHANNEL_USBREDIR] = "usbredir",
587         [SPICE_CHANNEL_PORT] = "port",
588 #if 0
589         /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
590          * no easy way to #ifdef (SPICE_CHANNEL_* is a enum).  Disable
591          * as quick fix for build failures with older versions. */
592         [SPICE_CHANNEL_WEBDAV] = "webdav",
593 #endif
594     };
595
596     info = qmp_query_spice(NULL);
597
598     if (!info->enabled) {
599         monitor_printf(mon, "Server: disabled\n");
600         goto out;
601     }
602
603     monitor_printf(mon, "Server:\n");
604     if (info->has_port) {
605         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
606                        info->host, info->port);
607     }
608     if (info->has_tls_port) {
609         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
610                        info->host, info->tls_port);
611     }
612     monitor_printf(mon, "    migrated: %s\n",
613                    info->migrated ? "true" : "false");
614     monitor_printf(mon, "        auth: %s\n", info->auth);
615     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
616     monitor_printf(mon, "  mouse-mode: %s\n",
617                    SpiceQueryMouseMode_lookup[info->mouse_mode]);
618
619     if (!info->has_channels || info->channels == NULL) {
620         monitor_printf(mon, "Channels: none\n");
621     } else {
622         for (chan = info->channels; chan; chan = chan->next) {
623             monitor_printf(mon, "Channel:\n");
624             monitor_printf(mon, "     address: %s:%s%s\n",
625                            chan->value->base->host, chan->value->base->port,
626                            chan->value->tls ? " [tls]" : "");
627             monitor_printf(mon, "     session: %" PRId64 "\n",
628                            chan->value->connection_id);
629             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
630                            chan->value->channel_type, chan->value->channel_id);
631
632             channel_name = "unknown";
633             if (chan->value->channel_type > 0 &&
634                 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
635                 channel_names[chan->value->channel_type]) {
636                 channel_name = channel_names[chan->value->channel_type];
637             }
638
639             monitor_printf(mon, "     channel name: %s\n", channel_name);
640         }
641     }
642
643 out:
644     qapi_free_SpiceInfo(info);
645 }
646 #endif
647
648 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
649 {
650     BalloonInfo *info;
651     Error *err = NULL;
652
653     info = qmp_query_balloon(&err);
654     if (err) {
655         monitor_printf(mon, "%s\n", error_get_pretty(err));
656         error_free(err);
657         return;
658     }
659
660     monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
661
662     qapi_free_BalloonInfo(info);
663 }
664
665 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
666 {
667     PciMemoryRegionList *region;
668
669     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
670     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
671                    dev->slot, dev->function);
672     monitor_printf(mon, "    ");
673
674     if (dev->class_info->has_desc) {
675         monitor_printf(mon, "%s", dev->class_info->desc);
676     } else {
677         monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
678     }
679
680     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
681                    dev->id->vendor, dev->id->device);
682
683     if (dev->has_irq) {
684         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
685     }
686
687     if (dev->has_pci_bridge) {
688         monitor_printf(mon, "      BUS %" PRId64 ".\n",
689                        dev->pci_bridge->bus->number);
690         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
691                        dev->pci_bridge->bus->secondary);
692         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
693                        dev->pci_bridge->bus->subordinate);
694
695         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
696                        dev->pci_bridge->bus->io_range->base,
697                        dev->pci_bridge->bus->io_range->limit);
698
699         monitor_printf(mon,
700                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
701                        dev->pci_bridge->bus->memory_range->base,
702                        dev->pci_bridge->bus->memory_range->limit);
703
704         monitor_printf(mon, "      prefetchable memory range "
705                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
706                        dev->pci_bridge->bus->prefetchable_range->base,
707                        dev->pci_bridge->bus->prefetchable_range->limit);
708     }
709
710     for (region = dev->regions; region; region = region->next) {
711         uint64_t addr, size;
712
713         addr = region->value->address;
714         size = region->value->size;
715
716         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
717
718         if (!strcmp(region->value->type, "io")) {
719             monitor_printf(mon, "I/O at 0x%04" PRIx64
720                                 " [0x%04" PRIx64 "].\n",
721                            addr, addr + size - 1);
722         } else {
723             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
724                                " [0x%08" PRIx64 "].\n",
725                            region->value->mem_type_64 ? 64 : 32,
726                            region->value->prefetch ? " prefetchable" : "",
727                            addr, addr + size - 1);
728         }
729     }
730
731     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
732
733     if (dev->has_pci_bridge) {
734         if (dev->pci_bridge->has_devices) {
735             PciDeviceInfoList *cdev;
736             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
737                 hmp_info_pci_device(mon, cdev->value);
738             }
739         }
740     }
741 }
742
743 void hmp_info_pci(Monitor *mon, const QDict *qdict)
744 {
745     PciInfoList *info_list, *info;
746     Error *err = NULL;
747
748     info_list = qmp_query_pci(&err);
749     if (err) {
750         monitor_printf(mon, "PCI devices not supported\n");
751         error_free(err);
752         return;
753     }
754
755     for (info = info_list; info; info = info->next) {
756         PciDeviceInfoList *dev;
757
758         for (dev = info->value->devices; dev; dev = dev->next) {
759             hmp_info_pci_device(mon, dev->value);
760         }
761     }
762
763     qapi_free_PciInfoList(info_list);
764 }
765
766 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
767 {
768     BlockJobInfoList *list;
769     Error *err = NULL;
770
771     list = qmp_query_block_jobs(&err);
772     assert(!err);
773
774     if (!list) {
775         monitor_printf(mon, "No active jobs\n");
776         return;
777     }
778
779     while (list) {
780         if (strcmp(list->value->type, "stream") == 0) {
781             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
782                            " of %" PRId64 " bytes, speed limit %" PRId64
783                            " bytes/s\n",
784                            list->value->device,
785                            list->value->offset,
786                            list->value->len,
787                            list->value->speed);
788         } else {
789             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
790                            " of %" PRId64 " bytes, speed limit %" PRId64
791                            " bytes/s\n",
792                            list->value->type,
793                            list->value->device,
794                            list->value->offset,
795                            list->value->len,
796                            list->value->speed);
797         }
798         list = list->next;
799     }
800
801     qapi_free_BlockJobInfoList(list);
802 }
803
804 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
805 {
806     TPMInfoList *info_list, *info;
807     Error *err = NULL;
808     unsigned int c = 0;
809     TPMPassthroughOptions *tpo;
810
811     info_list = qmp_query_tpm(&err);
812     if (err) {
813         monitor_printf(mon, "TPM device not supported\n");
814         error_free(err);
815         return;
816     }
817
818     if (info_list) {
819         monitor_printf(mon, "TPM device:\n");
820     }
821
822     for (info = info_list; info; info = info->next) {
823         TPMInfo *ti = info->value;
824         monitor_printf(mon, " tpm%d: model=%s\n",
825                        c, TpmModel_lookup[ti->model]);
826
827         monitor_printf(mon, "  \\ %s: type=%s",
828                        ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
829
830         switch (ti->options->kind) {
831         case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
832             tpo = ti->options->passthrough;
833             monitor_printf(mon, "%s%s%s%s",
834                            tpo->has_path ? ",path=" : "",
835                            tpo->has_path ? tpo->path : "",
836                            tpo->has_cancel_path ? ",cancel-path=" : "",
837                            tpo->has_cancel_path ? tpo->cancel_path : "");
838             break;
839         case TPM_TYPE_OPTIONS_KIND_MAX:
840             break;
841         }
842         monitor_printf(mon, "\n");
843         c++;
844     }
845     qapi_free_TPMInfoList(info_list);
846 }
847
848 void hmp_quit(Monitor *mon, const QDict *qdict)
849 {
850     monitor_suspend(mon);
851     qmp_quit(NULL);
852 }
853
854 void hmp_stop(Monitor *mon, const QDict *qdict)
855 {
856     qmp_stop(NULL);
857 }
858
859 void hmp_system_reset(Monitor *mon, const QDict *qdict)
860 {
861     qmp_system_reset(NULL);
862 }
863
864 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
865 {
866     qmp_system_powerdown(NULL);
867 }
868
869 void hmp_cpu(Monitor *mon, const QDict *qdict)
870 {
871     int64_t cpu_index;
872
873     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
874             use it are converted to the QAPI */
875     cpu_index = qdict_get_int(qdict, "index");
876     if (monitor_set_cpu(cpu_index) < 0) {
877         monitor_printf(mon, "invalid CPU index\n");
878     }
879 }
880
881 void hmp_memsave(Monitor *mon, const QDict *qdict)
882 {
883     uint32_t size = qdict_get_int(qdict, "size");
884     const char *filename = qdict_get_str(qdict, "filename");
885     uint64_t addr = qdict_get_int(qdict, "val");
886     Error *err = NULL;
887
888     qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
889     hmp_handle_error(mon, &err);
890 }
891
892 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
893 {
894     uint32_t size = qdict_get_int(qdict, "size");
895     const char *filename = qdict_get_str(qdict, "filename");
896     uint64_t addr = qdict_get_int(qdict, "val");
897     Error *err = NULL;
898
899     qmp_pmemsave(addr, size, filename, &err);
900     hmp_handle_error(mon, &err);
901 }
902
903 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
904 {
905     const char *chardev = qdict_get_str(qdict, "device");
906     const char *data = qdict_get_str(qdict, "data");
907     Error *err = NULL;
908
909     qmp_ringbuf_write(chardev, data, false, 0, &err);
910
911     hmp_handle_error(mon, &err);
912 }
913
914 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
915 {
916     uint32_t size = qdict_get_int(qdict, "size");
917     const char *chardev = qdict_get_str(qdict, "device");
918     char *data;
919     Error *err = NULL;
920     int i;
921
922     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
923     if (err) {
924         monitor_printf(mon, "%s\n", error_get_pretty(err));
925         error_free(err);
926         return;
927     }
928
929     for (i = 0; data[i]; i++) {
930         unsigned char ch = data[i];
931
932         if (ch == '\\') {
933             monitor_printf(mon, "\\\\");
934         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
935             monitor_printf(mon, "\\u%04X", ch);
936         } else {
937             monitor_printf(mon, "%c", ch);
938         }
939
940     }
941     monitor_printf(mon, "\n");
942     g_free(data);
943 }
944
945 static void hmp_cont_cb(void *opaque, int err)
946 {
947     if (!err) {
948         qmp_cont(NULL);
949     }
950 }
951
952 static bool key_is_missing(const BlockInfo *bdev)
953 {
954     return (bdev->inserted && bdev->inserted->encryption_key_missing);
955 }
956
957 void hmp_cont(Monitor *mon, const QDict *qdict)
958 {
959     BlockInfoList *bdev_list, *bdev;
960     Error *err = NULL;
961
962     bdev_list = qmp_query_block(NULL);
963     for (bdev = bdev_list; bdev; bdev = bdev->next) {
964         if (key_is_missing(bdev->value)) {
965             monitor_read_block_device_key(mon, bdev->value->device,
966                                           hmp_cont_cb, NULL);
967             goto out;
968         }
969     }
970
971     qmp_cont(&err);
972     hmp_handle_error(mon, &err);
973
974 out:
975     qapi_free_BlockInfoList(bdev_list);
976 }
977
978 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
979 {
980     qmp_system_wakeup(NULL);
981 }
982
983 void hmp_nmi(Monitor *mon, const QDict *qdict)
984 {
985     Error *err = NULL;
986
987     qmp_inject_nmi(&err);
988     hmp_handle_error(mon, &err);
989 }
990
991 void hmp_set_link(Monitor *mon, const QDict *qdict)
992 {
993     const char *name = qdict_get_str(qdict, "name");
994     int up = qdict_get_bool(qdict, "up");
995     Error *err = NULL;
996
997     qmp_set_link(name, up, &err);
998     hmp_handle_error(mon, &err);
999 }
1000
1001 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1002 {
1003     const char *device = qdict_get_str(qdict, "device");
1004     const char *password = qdict_get_str(qdict, "password");
1005     Error *err = NULL;
1006
1007     qmp_block_passwd(true, device, false, NULL, password, &err);
1008     hmp_handle_error(mon, &err);
1009 }
1010
1011 void hmp_balloon(Monitor *mon, const QDict *qdict)
1012 {
1013     int64_t value = qdict_get_int(qdict, "value");
1014     Error *err = NULL;
1015
1016     qmp_balloon(value, &err);
1017     if (err) {
1018         monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
1019         error_free(err);
1020     }
1021 }
1022
1023 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1024 {
1025     const char *device = qdict_get_str(qdict, "device");
1026     int64_t size = qdict_get_int(qdict, "size");
1027     Error *err = NULL;
1028
1029     qmp_block_resize(true, device, false, NULL, size, &err);
1030     hmp_handle_error(mon, &err);
1031 }
1032
1033 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1034 {
1035     const char *device = qdict_get_str(qdict, "device");
1036     const char *filename = qdict_get_str(qdict, "target");
1037     const char *format = qdict_get_try_str(qdict, "format");
1038     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
1039     int full = qdict_get_try_bool(qdict, "full", 0);
1040     enum NewImageMode mode;
1041     Error *err = NULL;
1042
1043     if (!filename) {
1044         error_set(&err, QERR_MISSING_PARAMETER, "target");
1045         hmp_handle_error(mon, &err);
1046         return;
1047     }
1048
1049     if (reuse) {
1050         mode = NEW_IMAGE_MODE_EXISTING;
1051     } else {
1052         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1053     }
1054
1055     qmp_drive_mirror(device, filename, !!format, format,
1056                      false, NULL, false, NULL,
1057                      full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1058                      true, mode, false, 0, false, 0, false, 0,
1059                      false, 0, false, 0, &err);
1060     hmp_handle_error(mon, &err);
1061 }
1062
1063 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1064 {
1065     const char *device = qdict_get_str(qdict, "device");
1066     const char *filename = qdict_get_str(qdict, "target");
1067     const char *format = qdict_get_try_str(qdict, "format");
1068     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
1069     int full = qdict_get_try_bool(qdict, "full", 0);
1070     enum NewImageMode mode;
1071     Error *err = NULL;
1072
1073     if (!filename) {
1074         error_set(&err, QERR_MISSING_PARAMETER, "target");
1075         hmp_handle_error(mon, &err);
1076         return;
1077     }
1078
1079     if (reuse) {
1080         mode = NEW_IMAGE_MODE_EXISTING;
1081     } else {
1082         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1083     }
1084
1085     qmp_drive_backup(device, filename, !!format, format,
1086                      full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1087                      true, mode, false, 0, false, NULL,
1088                      false, 0, false, 0, &err);
1089     hmp_handle_error(mon, &err);
1090 }
1091
1092 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1093 {
1094     const char *device = qdict_get_str(qdict, "device");
1095     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1096     const char *format = qdict_get_try_str(qdict, "format");
1097     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
1098     enum NewImageMode mode;
1099     Error *err = NULL;
1100
1101     if (!filename) {
1102         /* In the future, if 'snapshot-file' is not specified, the snapshot
1103            will be taken internally. Today it's actually required. */
1104         error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1105         hmp_handle_error(mon, &err);
1106         return;
1107     }
1108
1109     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1110     qmp_blockdev_snapshot_sync(true, device, false, NULL,
1111                                filename, false, NULL,
1112                                !!format, format,
1113                                true, mode, &err);
1114     hmp_handle_error(mon, &err);
1115 }
1116
1117 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1118 {
1119     const char *device = qdict_get_str(qdict, "device");
1120     const char *name = qdict_get_str(qdict, "name");
1121     Error *err = NULL;
1122
1123     qmp_blockdev_snapshot_internal_sync(device, name, &err);
1124     hmp_handle_error(mon, &err);
1125 }
1126
1127 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1128 {
1129     const char *device = qdict_get_str(qdict, "device");
1130     const char *name = qdict_get_str(qdict, "name");
1131     const char *id = qdict_get_try_str(qdict, "id");
1132     Error *err = NULL;
1133
1134     qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1135                                                true, name, &err);
1136     hmp_handle_error(mon, &err);
1137 }
1138
1139 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1140 {
1141     qmp_migrate_cancel(NULL);
1142 }
1143
1144 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1145 {
1146     Error *err = NULL;
1147     const char *uri = qdict_get_str(qdict, "uri");
1148
1149     qmp_migrate_incoming(uri, &err);
1150
1151     hmp_handle_error(mon, &err);
1152 }
1153
1154 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1155 {
1156     double value = qdict_get_double(qdict, "value");
1157     qmp_migrate_set_downtime(value, NULL);
1158 }
1159
1160 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1161 {
1162     int64_t value = qdict_get_int(qdict, "value");
1163     Error *err = NULL;
1164
1165     qmp_migrate_set_cache_size(value, &err);
1166     if (err) {
1167         monitor_printf(mon, "%s\n", error_get_pretty(err));
1168         error_free(err);
1169         return;
1170     }
1171 }
1172
1173 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1174 {
1175     int64_t value = qdict_get_int(qdict, "value");
1176     qmp_migrate_set_speed(value, NULL);
1177 }
1178
1179 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1180 {
1181     const char *cap = qdict_get_str(qdict, "capability");
1182     bool state = qdict_get_bool(qdict, "state");
1183     Error *err = NULL;
1184     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1185     int i;
1186
1187     for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1188         if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1189             caps->value = g_malloc0(sizeof(*caps->value));
1190             caps->value->capability = i;
1191             caps->value->state = state;
1192             caps->next = NULL;
1193             qmp_migrate_set_capabilities(caps, &err);
1194             break;
1195         }
1196     }
1197
1198     if (i == MIGRATION_CAPABILITY_MAX) {
1199         error_set(&err, QERR_INVALID_PARAMETER, cap);
1200     }
1201
1202     qapi_free_MigrationCapabilityStatusList(caps);
1203
1204     if (err) {
1205         monitor_printf(mon, "migrate_set_capability: %s\n",
1206                        error_get_pretty(err));
1207         error_free(err);
1208     }
1209 }
1210
1211 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1212 {
1213     const char *param = qdict_get_str(qdict, "parameter");
1214     int value = qdict_get_int(qdict, "value");
1215     Error *err = NULL;
1216     bool has_compress_level = false;
1217     bool has_compress_threads = false;
1218     bool has_decompress_threads = false;
1219     int i;
1220
1221     for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
1222         if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
1223             switch (i) {
1224             case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1225                 has_compress_level = true;
1226                 break;
1227             case MIGRATION_PARAMETER_COMPRESS_THREADS:
1228                 has_compress_threads = true;
1229                 break;
1230             case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1231                 has_decompress_threads = true;
1232                 break;
1233             }
1234             qmp_migrate_set_parameters(has_compress_level, value,
1235                                        has_compress_threads, value,
1236                                        has_decompress_threads, value,
1237                                        &err);
1238             break;
1239         }
1240     }
1241
1242     if (i == MIGRATION_PARAMETER_MAX) {
1243         error_set(&err, QERR_INVALID_PARAMETER, param);
1244     }
1245
1246     if (err) {
1247         monitor_printf(mon, "migrate_set_parameter: %s\n",
1248                        error_get_pretty(err));
1249         error_free(err);
1250     }
1251 }
1252
1253 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1254 {
1255     Error *err = NULL;
1256     const char *protocol = qdict_get_str(qdict, "protocol");
1257     const char *hostname = qdict_get_str(qdict, "hostname");
1258     bool has_port        = qdict_haskey(qdict, "port");
1259     int port             = qdict_get_try_int(qdict, "port", -1);
1260     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1261     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1262     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1263
1264     qmp_client_migrate_info(protocol, hostname,
1265                             has_port, port, has_tls_port, tls_port,
1266                             !!cert_subject, cert_subject, &err);
1267     hmp_handle_error(mon, &err);
1268 }
1269
1270 void hmp_set_password(Monitor *mon, const QDict *qdict)
1271 {
1272     const char *protocol  = qdict_get_str(qdict, "protocol");
1273     const char *password  = qdict_get_str(qdict, "password");
1274     const char *connected = qdict_get_try_str(qdict, "connected");
1275     Error *err = NULL;
1276
1277     qmp_set_password(protocol, password, !!connected, connected, &err);
1278     hmp_handle_error(mon, &err);
1279 }
1280
1281 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1282 {
1283     const char *protocol  = qdict_get_str(qdict, "protocol");
1284     const char *whenstr = qdict_get_str(qdict, "time");
1285     Error *err = NULL;
1286
1287     qmp_expire_password(protocol, whenstr, &err);
1288     hmp_handle_error(mon, &err);
1289 }
1290
1291 void hmp_eject(Monitor *mon, const QDict *qdict)
1292 {
1293     int force = qdict_get_try_bool(qdict, "force", 0);
1294     const char *device = qdict_get_str(qdict, "device");
1295     Error *err = NULL;
1296
1297     qmp_eject(device, true, force, &err);
1298     hmp_handle_error(mon, &err);
1299 }
1300
1301 static void hmp_change_read_arg(void *opaque, const char *password,
1302                                 void *readline_opaque)
1303 {
1304     qmp_change_vnc_password(password, NULL);
1305     monitor_read_command(opaque, 1);
1306 }
1307
1308 void hmp_change(Monitor *mon, const QDict *qdict)
1309 {
1310     const char *device = qdict_get_str(qdict, "device");
1311     const char *target = qdict_get_str(qdict, "target");
1312     const char *arg = qdict_get_try_str(qdict, "arg");
1313     Error *err = NULL;
1314
1315     if (strcmp(device, "vnc") == 0 &&
1316             (strcmp(target, "passwd") == 0 ||
1317              strcmp(target, "password") == 0)) {
1318         if (!arg) {
1319             monitor_read_password(mon, hmp_change_read_arg, NULL);
1320             return;
1321         }
1322     }
1323
1324     qmp_change(device, target, !!arg, arg, &err);
1325     if (err &&
1326         error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1327         error_free(err);
1328         monitor_read_block_device_key(mon, device, NULL, NULL);
1329         return;
1330     }
1331     hmp_handle_error(mon, &err);
1332 }
1333
1334 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1335 {
1336     Error *err = NULL;
1337
1338     qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1339                               qdict_get_int(qdict, "bps"),
1340                               qdict_get_int(qdict, "bps_rd"),
1341                               qdict_get_int(qdict, "bps_wr"),
1342                               qdict_get_int(qdict, "iops"),
1343                               qdict_get_int(qdict, "iops_rd"),
1344                               qdict_get_int(qdict, "iops_wr"),
1345                               false, /* no burst max via HMP */
1346                               0,
1347                               false,
1348                               0,
1349                               false,
1350                               0,
1351                               false,
1352                               0,
1353                               false,
1354                               0,
1355                               false,
1356                               0,
1357                               false, /* No default I/O size */
1358                               0, &err);
1359     hmp_handle_error(mon, &err);
1360 }
1361
1362 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1363 {
1364     Error *error = NULL;
1365     const char *device = qdict_get_str(qdict, "device");
1366     const char *base = qdict_get_try_str(qdict, "base");
1367     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1368
1369     qmp_block_stream(device, base != NULL, base, false, NULL,
1370                      qdict_haskey(qdict, "speed"), speed,
1371                      true, BLOCKDEV_ON_ERROR_REPORT, &error);
1372
1373     hmp_handle_error(mon, &error);
1374 }
1375
1376 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1377 {
1378     Error *error = NULL;
1379     const char *device = qdict_get_str(qdict, "device");
1380     int64_t value = qdict_get_int(qdict, "speed");
1381
1382     qmp_block_job_set_speed(device, value, &error);
1383
1384     hmp_handle_error(mon, &error);
1385 }
1386
1387 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1388 {
1389     Error *error = NULL;
1390     const char *device = qdict_get_str(qdict, "device");
1391     bool force = qdict_get_try_bool(qdict, "force", 0);
1392
1393     qmp_block_job_cancel(device, true, force, &error);
1394
1395     hmp_handle_error(mon, &error);
1396 }
1397
1398 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1399 {
1400     Error *error = NULL;
1401     const char *device = qdict_get_str(qdict, "device");
1402
1403     qmp_block_job_pause(device, &error);
1404
1405     hmp_handle_error(mon, &error);
1406 }
1407
1408 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1409 {
1410     Error *error = NULL;
1411     const char *device = qdict_get_str(qdict, "device");
1412
1413     qmp_block_job_resume(device, &error);
1414
1415     hmp_handle_error(mon, &error);
1416 }
1417
1418 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1419 {
1420     Error *error = NULL;
1421     const char *device = qdict_get_str(qdict, "device");
1422
1423     qmp_block_job_complete(device, &error);
1424
1425     hmp_handle_error(mon, &error);
1426 }
1427
1428 typedef struct HMPMigrationStatus
1429 {
1430     QEMUTimer *timer;
1431     Monitor *mon;
1432     bool is_block_migration;
1433 } HMPMigrationStatus;
1434
1435 static void hmp_migrate_status_cb(void *opaque)
1436 {
1437     HMPMigrationStatus *status = opaque;
1438     MigrationInfo *info;
1439
1440     info = qmp_query_migrate(NULL);
1441     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1442         info->status == MIGRATION_STATUS_SETUP) {
1443         if (info->has_disk) {
1444             int progress;
1445
1446             if (info->disk->remaining) {
1447                 progress = info->disk->transferred * 100 / info->disk->total;
1448             } else {
1449                 progress = 100;
1450             }
1451
1452             monitor_printf(status->mon, "Completed %d %%\r", progress);
1453             monitor_flush(status->mon);
1454         }
1455
1456         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1457     } else {
1458         if (status->is_block_migration) {
1459             monitor_printf(status->mon, "\n");
1460         }
1461         monitor_resume(status->mon);
1462         timer_del(status->timer);
1463         g_free(status);
1464     }
1465
1466     qapi_free_MigrationInfo(info);
1467 }
1468
1469 void hmp_migrate(Monitor *mon, const QDict *qdict)
1470 {
1471     int detach = qdict_get_try_bool(qdict, "detach", 0);
1472     int blk = qdict_get_try_bool(qdict, "blk", 0);
1473     int inc = qdict_get_try_bool(qdict, "inc", 0);
1474     const char *uri = qdict_get_str(qdict, "uri");
1475     Error *err = NULL;
1476
1477     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1478     if (err) {
1479         monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1480         error_free(err);
1481         return;
1482     }
1483
1484     if (!detach) {
1485         HMPMigrationStatus *status;
1486
1487         if (monitor_suspend(mon) < 0) {
1488             monitor_printf(mon, "terminal does not allow synchronous "
1489                            "migration, continuing detached\n");
1490             return;
1491         }
1492
1493         status = g_malloc0(sizeof(*status));
1494         status->mon = mon;
1495         status->is_block_migration = blk || inc;
1496         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1497                                           status);
1498         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1499     }
1500 }
1501
1502 void hmp_device_del(Monitor *mon, const QDict *qdict)
1503 {
1504     const char *id = qdict_get_str(qdict, "id");
1505     Error *err = NULL;
1506
1507     qmp_device_del(id, &err);
1508     hmp_handle_error(mon, &err);
1509 }
1510
1511 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1512 {
1513     Error *err = NULL;
1514     int paging = qdict_get_try_bool(qdict, "paging", 0);
1515     int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1516     int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1517     int snappy = qdict_get_try_bool(qdict, "snappy", 0);
1518     const char *file = qdict_get_str(qdict, "filename");
1519     bool has_begin = qdict_haskey(qdict, "begin");
1520     bool has_length = qdict_haskey(qdict, "length");
1521     int64_t begin = 0;
1522     int64_t length = 0;
1523     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1524     char *prot;
1525
1526     if (zlib + lzo + snappy > 1) {
1527         error_setg(&err, "only one of '-z|-l|-s' can be set");
1528         hmp_handle_error(mon, &err);
1529         return;
1530     }
1531
1532     if (zlib) {
1533         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1534     }
1535
1536     if (lzo) {
1537         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1538     }
1539
1540     if (snappy) {
1541         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1542     }
1543
1544     if (has_begin) {
1545         begin = qdict_get_int(qdict, "begin");
1546     }
1547     if (has_length) {
1548         length = qdict_get_int(qdict, "length");
1549     }
1550
1551     prot = g_strconcat("file:", file, NULL);
1552
1553     qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1554                           true, dump_format, &err);
1555     hmp_handle_error(mon, &err);
1556     g_free(prot);
1557 }
1558
1559 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1560 {
1561     Error *err = NULL;
1562     QemuOpts *opts;
1563
1564     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1565     if (err) {
1566         goto out;
1567     }
1568
1569     netdev_add(opts, &err);
1570     if (err) {
1571         qemu_opts_del(opts);
1572     }
1573
1574 out:
1575     hmp_handle_error(mon, &err);
1576 }
1577
1578 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1579 {
1580     const char *id = qdict_get_str(qdict, "id");
1581     Error *err = NULL;
1582
1583     qmp_netdev_del(id, &err);
1584     hmp_handle_error(mon, &err);
1585 }
1586
1587 void hmp_object_add(Monitor *mon, const QDict *qdict)
1588 {
1589     Error *err = NULL;
1590     Error *err_end = NULL;
1591     QemuOpts *opts;
1592     char *type = NULL;
1593     char *id = NULL;
1594     void *dummy = NULL;
1595     OptsVisitor *ov;
1596     QDict *pdict;
1597
1598     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1599     if (err) {
1600         goto out;
1601     }
1602
1603     ov = opts_visitor_new(opts);
1604     pdict = qdict_clone_shallow(qdict);
1605
1606     visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1607     if (err) {
1608         goto out_clean;
1609     }
1610
1611     qdict_del(pdict, "qom-type");
1612     visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1613     if (err) {
1614         goto out_end;
1615     }
1616
1617     qdict_del(pdict, "id");
1618     visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1619     if (err) {
1620         goto out_end;
1621     }
1622
1623     object_add(type, id, pdict, opts_get_visitor(ov), &err);
1624
1625 out_end:
1626     visit_end_struct(opts_get_visitor(ov), &err_end);
1627     if (!err && err_end) {
1628         qmp_object_del(id, NULL);
1629     }
1630     error_propagate(&err, err_end);
1631 out_clean:
1632     opts_visitor_cleanup(ov);
1633
1634     QDECREF(pdict);
1635     qemu_opts_del(opts);
1636     g_free(id);
1637     g_free(type);
1638     g_free(dummy);
1639
1640 out:
1641     hmp_handle_error(mon, &err);
1642 }
1643
1644 void hmp_getfd(Monitor *mon, const QDict *qdict)
1645 {
1646     const char *fdname = qdict_get_str(qdict, "fdname");
1647     Error *err = NULL;
1648
1649     qmp_getfd(fdname, &err);
1650     hmp_handle_error(mon, &err);
1651 }
1652
1653 void hmp_closefd(Monitor *mon, const QDict *qdict)
1654 {
1655     const char *fdname = qdict_get_str(qdict, "fdname");
1656     Error *err = NULL;
1657
1658     qmp_closefd(fdname, &err);
1659     hmp_handle_error(mon, &err);
1660 }
1661
1662 void hmp_sendkey(Monitor *mon, const QDict *qdict)
1663 {
1664     const char *keys = qdict_get_str(qdict, "keys");
1665     KeyValueList *keylist, *head = NULL, *tmp = NULL;
1666     int has_hold_time = qdict_haskey(qdict, "hold-time");
1667     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1668     Error *err = NULL;
1669     char keyname_buf[16];
1670     char *separator;
1671     int keyname_len;
1672
1673     while (1) {
1674         separator = strchr(keys, '-');
1675         keyname_len = separator ? separator - keys : strlen(keys);
1676         pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1677
1678         /* Be compatible with old interface, convert user inputted "<" */
1679         if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1680             pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1681             keyname_len = 4;
1682         }
1683         keyname_buf[keyname_len] = 0;
1684
1685         keylist = g_malloc0(sizeof(*keylist));
1686         keylist->value = g_malloc0(sizeof(*keylist->value));
1687
1688         if (!head) {
1689             head = keylist;
1690         }
1691         if (tmp) {
1692             tmp->next = keylist;
1693         }
1694         tmp = keylist;
1695
1696         if (strstart(keyname_buf, "0x", NULL)) {
1697             char *endp;
1698             int value = strtoul(keyname_buf, &endp, 0);
1699             if (*endp != '\0') {
1700                 goto err_out;
1701             }
1702             keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1703             keylist->value->number = value;
1704         } else {
1705             int idx = index_from_key(keyname_buf);
1706             if (idx == Q_KEY_CODE_MAX) {
1707                 goto err_out;
1708             }
1709             keylist->value->kind = KEY_VALUE_KIND_QCODE;
1710             keylist->value->qcode = idx;
1711         }
1712
1713         if (!separator) {
1714             break;
1715         }
1716         keys = separator + 1;
1717     }
1718
1719     qmp_send_key(head, has_hold_time, hold_time, &err);
1720     hmp_handle_error(mon, &err);
1721
1722 out:
1723     qapi_free_KeyValueList(head);
1724     return;
1725
1726 err_out:
1727     monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1728     goto out;
1729 }
1730
1731 void hmp_screendump(Monitor *mon, const QDict *qdict)
1732 {
1733     const char *filename = qdict_get_str(qdict, "filename");
1734     Error *err = NULL;
1735
1736     qmp_screendump(filename, &err);
1737     hmp_handle_error(mon, &err);
1738 }
1739
1740 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1741 {
1742     const char *uri = qdict_get_str(qdict, "uri");
1743     int writable = qdict_get_try_bool(qdict, "writable", 0);
1744     int all = qdict_get_try_bool(qdict, "all", 0);
1745     Error *local_err = NULL;
1746     BlockInfoList *block_list, *info;
1747     SocketAddress *addr;
1748
1749     if (writable && !all) {
1750         error_setg(&local_err, "-w only valid together with -a");
1751         goto exit;
1752     }
1753
1754     /* First check if the address is valid and start the server.  */
1755     addr = socket_parse(uri, &local_err);
1756     if (local_err != NULL) {
1757         goto exit;
1758     }
1759
1760     qmp_nbd_server_start(addr, &local_err);
1761     qapi_free_SocketAddress(addr);
1762     if (local_err != NULL) {
1763         goto exit;
1764     }
1765
1766     if (!all) {
1767         return;
1768     }
1769
1770     /* Then try adding all block devices.  If one fails, close all and
1771      * exit.
1772      */
1773     block_list = qmp_query_block(NULL);
1774
1775     for (info = block_list; info; info = info->next) {
1776         if (!info->value->has_inserted) {
1777             continue;
1778         }
1779
1780         qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1781
1782         if (local_err != NULL) {
1783             qmp_nbd_server_stop(NULL);
1784             break;
1785         }
1786     }
1787
1788     qapi_free_BlockInfoList(block_list);
1789
1790 exit:
1791     hmp_handle_error(mon, &local_err);
1792 }
1793
1794 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1795 {
1796     const char *device = qdict_get_str(qdict, "device");
1797     int writable = qdict_get_try_bool(qdict, "writable", 0);
1798     Error *local_err = NULL;
1799
1800     qmp_nbd_server_add(device, true, writable, &local_err);
1801
1802     if (local_err != NULL) {
1803         hmp_handle_error(mon, &local_err);
1804     }
1805 }
1806
1807 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1808 {
1809     Error *err = NULL;
1810
1811     qmp_nbd_server_stop(&err);
1812     hmp_handle_error(mon, &err);
1813 }
1814
1815 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1816 {
1817     int cpuid;
1818     Error *err = NULL;
1819
1820     cpuid = qdict_get_int(qdict, "id");
1821     qmp_cpu_add(cpuid, &err);
1822     hmp_handle_error(mon, &err);
1823 }
1824
1825 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1826 {
1827     const char *args = qdict_get_str(qdict, "args");
1828     Error *err = NULL;
1829     QemuOpts *opts;
1830
1831     opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1832     if (opts == NULL) {
1833         error_setg(&err, "Parsing chardev args failed");
1834     } else {
1835         qemu_chr_new_from_opts(opts, NULL, &err);
1836     }
1837     hmp_handle_error(mon, &err);
1838 }
1839
1840 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1841 {
1842     Error *local_err = NULL;
1843
1844     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1845     hmp_handle_error(mon, &local_err);
1846 }
1847
1848 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1849 {
1850     BlockBackend *blk;
1851     const char* device = qdict_get_str(qdict, "device");
1852     const char* command = qdict_get_str(qdict, "command");
1853     Error *err = NULL;
1854
1855     blk = blk_by_name(device);
1856     if (blk) {
1857         qemuio_command(blk, command);
1858     } else {
1859         error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1860     }
1861
1862     hmp_handle_error(mon, &err);
1863 }
1864
1865 void hmp_object_del(Monitor *mon, const QDict *qdict)
1866 {
1867     const char *id = qdict_get_str(qdict, "id");
1868     Error *err = NULL;
1869
1870     qmp_object_del(id, &err);
1871     hmp_handle_error(mon, &err);
1872 }
1873
1874 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
1875 {
1876     Error *err = NULL;
1877     MemdevList *memdev_list = qmp_query_memdev(&err);
1878     MemdevList *m = memdev_list;
1879     StringOutputVisitor *ov;
1880     char *str;
1881     int i = 0;
1882
1883
1884     while (m) {
1885         ov = string_output_visitor_new(false);
1886         visit_type_uint16List(string_output_get_visitor(ov),
1887                               &m->value->host_nodes, NULL, NULL);
1888         monitor_printf(mon, "memory backend: %d\n", i);
1889         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
1890         monitor_printf(mon, "  merge: %s\n",
1891                        m->value->merge ? "true" : "false");
1892         monitor_printf(mon, "  dump: %s\n",
1893                        m->value->dump ? "true" : "false");
1894         monitor_printf(mon, "  prealloc: %s\n",
1895                        m->value->prealloc ? "true" : "false");
1896         monitor_printf(mon, "  policy: %s\n",
1897                        HostMemPolicy_lookup[m->value->policy]);
1898         str = string_output_get_string(ov);
1899         monitor_printf(mon, "  host nodes: %s\n", str);
1900
1901         g_free(str);
1902         string_output_visitor_cleanup(ov);
1903         m = m->next;
1904         i++;
1905     }
1906
1907     monitor_printf(mon, "\n");
1908
1909     qapi_free_MemdevList(memdev_list);
1910 }
1911
1912 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
1913 {
1914     Error *err = NULL;
1915     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
1916     MemoryDeviceInfoList *info;
1917     MemoryDeviceInfo *value;
1918     PCDIMMDeviceInfo *di;
1919
1920     for (info = info_list; info; info = info->next) {
1921         value = info->value;
1922
1923         if (value) {
1924             switch (value->kind) {
1925             case MEMORY_DEVICE_INFO_KIND_DIMM:
1926                 di = value->dimm;
1927
1928                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1929                                MemoryDeviceInfoKind_lookup[value->kind],
1930                                di->id ? di->id : "");
1931                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
1932                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
1933                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
1934                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
1935                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
1936                 monitor_printf(mon, "  hotplugged: %s\n",
1937                                di->hotplugged ? "true" : "false");
1938                 monitor_printf(mon, "  hotpluggable: %s\n",
1939                                di->hotpluggable ? "true" : "false");
1940                 break;
1941             default:
1942                 break;
1943             }
1944         }
1945     }
1946
1947     qapi_free_MemoryDeviceInfoList(info_list);
1948 }
1949
1950 void hmp_qom_list(Monitor *mon, const QDict *qdict)
1951 {
1952     const char *path = qdict_get_try_str(qdict, "path");
1953     ObjectPropertyInfoList *list;
1954     Error *err = NULL;
1955
1956     if (path == NULL) {
1957         monitor_printf(mon, "/\n");
1958         return;
1959     }
1960
1961     list = qmp_qom_list(path, &err);
1962     if (err == NULL) {
1963         ObjectPropertyInfoList *start = list;
1964         while (list != NULL) {
1965             ObjectPropertyInfo *value = list->value;
1966
1967             monitor_printf(mon, "%s (%s)\n",
1968                            value->name, value->type);
1969             list = list->next;
1970         }
1971         qapi_free_ObjectPropertyInfoList(start);
1972     }
1973     hmp_handle_error(mon, &err);
1974 }
1975
1976 void hmp_qom_set(Monitor *mon, const QDict *qdict)
1977 {
1978     const char *path = qdict_get_str(qdict, "path");
1979     const char *property = qdict_get_str(qdict, "property");
1980     const char *value = qdict_get_str(qdict, "value");
1981     Error *err = NULL;
1982     bool ambiguous = false;
1983     Object *obj;
1984
1985     obj = object_resolve_path(path, &ambiguous);
1986     if (obj == NULL) {
1987         error_set(&err, QERR_DEVICE_NOT_FOUND, path);
1988     } else {
1989         if (ambiguous) {
1990             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
1991         }
1992         object_property_parse(obj, value, property, &err);
1993     }
1994     hmp_handle_error(mon, &err);
1995 }