Merge remote-tracking branch 'remotes/juanquintela/tags/migration/20150507-1' into...
[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_set_password(Monitor *mon, const QDict *qdict)
1254 {
1255     const char *protocol  = qdict_get_str(qdict, "protocol");
1256     const char *password  = qdict_get_str(qdict, "password");
1257     const char *connected = qdict_get_try_str(qdict, "connected");
1258     Error *err = NULL;
1259
1260     qmp_set_password(protocol, password, !!connected, connected, &err);
1261     hmp_handle_error(mon, &err);
1262 }
1263
1264 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1265 {
1266     const char *protocol  = qdict_get_str(qdict, "protocol");
1267     const char *whenstr = qdict_get_str(qdict, "time");
1268     Error *err = NULL;
1269
1270     qmp_expire_password(protocol, whenstr, &err);
1271     hmp_handle_error(mon, &err);
1272 }
1273
1274 void hmp_eject(Monitor *mon, const QDict *qdict)
1275 {
1276     int force = qdict_get_try_bool(qdict, "force", 0);
1277     const char *device = qdict_get_str(qdict, "device");
1278     Error *err = NULL;
1279
1280     qmp_eject(device, true, force, &err);
1281     hmp_handle_error(mon, &err);
1282 }
1283
1284 static void hmp_change_read_arg(void *opaque, const char *password,
1285                                 void *readline_opaque)
1286 {
1287     qmp_change_vnc_password(password, NULL);
1288     monitor_read_command(opaque, 1);
1289 }
1290
1291 void hmp_change(Monitor *mon, const QDict *qdict)
1292 {
1293     const char *device = qdict_get_str(qdict, "device");
1294     const char *target = qdict_get_str(qdict, "target");
1295     const char *arg = qdict_get_try_str(qdict, "arg");
1296     Error *err = NULL;
1297
1298     if (strcmp(device, "vnc") == 0 &&
1299             (strcmp(target, "passwd") == 0 ||
1300              strcmp(target, "password") == 0)) {
1301         if (!arg) {
1302             monitor_read_password(mon, hmp_change_read_arg, NULL);
1303             return;
1304         }
1305     }
1306
1307     qmp_change(device, target, !!arg, arg, &err);
1308     if (err &&
1309         error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1310         error_free(err);
1311         monitor_read_block_device_key(mon, device, NULL, NULL);
1312         return;
1313     }
1314     hmp_handle_error(mon, &err);
1315 }
1316
1317 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1318 {
1319     Error *err = NULL;
1320
1321     qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1322                               qdict_get_int(qdict, "bps"),
1323                               qdict_get_int(qdict, "bps_rd"),
1324                               qdict_get_int(qdict, "bps_wr"),
1325                               qdict_get_int(qdict, "iops"),
1326                               qdict_get_int(qdict, "iops_rd"),
1327                               qdict_get_int(qdict, "iops_wr"),
1328                               false, /* no burst max via HMP */
1329                               0,
1330                               false,
1331                               0,
1332                               false,
1333                               0,
1334                               false,
1335                               0,
1336                               false,
1337                               0,
1338                               false,
1339                               0,
1340                               false, /* No default I/O size */
1341                               0, &err);
1342     hmp_handle_error(mon, &err);
1343 }
1344
1345 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1346 {
1347     Error *error = NULL;
1348     const char *device = qdict_get_str(qdict, "device");
1349     const char *base = qdict_get_try_str(qdict, "base");
1350     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1351
1352     qmp_block_stream(device, base != NULL, base, false, NULL,
1353                      qdict_haskey(qdict, "speed"), speed,
1354                      true, BLOCKDEV_ON_ERROR_REPORT, &error);
1355
1356     hmp_handle_error(mon, &error);
1357 }
1358
1359 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1360 {
1361     Error *error = NULL;
1362     const char *device = qdict_get_str(qdict, "device");
1363     int64_t value = qdict_get_int(qdict, "speed");
1364
1365     qmp_block_job_set_speed(device, value, &error);
1366
1367     hmp_handle_error(mon, &error);
1368 }
1369
1370 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1371 {
1372     Error *error = NULL;
1373     const char *device = qdict_get_str(qdict, "device");
1374     bool force = qdict_get_try_bool(qdict, "force", 0);
1375
1376     qmp_block_job_cancel(device, true, force, &error);
1377
1378     hmp_handle_error(mon, &error);
1379 }
1380
1381 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1382 {
1383     Error *error = NULL;
1384     const char *device = qdict_get_str(qdict, "device");
1385
1386     qmp_block_job_pause(device, &error);
1387
1388     hmp_handle_error(mon, &error);
1389 }
1390
1391 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1392 {
1393     Error *error = NULL;
1394     const char *device = qdict_get_str(qdict, "device");
1395
1396     qmp_block_job_resume(device, &error);
1397
1398     hmp_handle_error(mon, &error);
1399 }
1400
1401 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1402 {
1403     Error *error = NULL;
1404     const char *device = qdict_get_str(qdict, "device");
1405
1406     qmp_block_job_complete(device, &error);
1407
1408     hmp_handle_error(mon, &error);
1409 }
1410
1411 typedef struct HMPMigrationStatus
1412 {
1413     QEMUTimer *timer;
1414     Monitor *mon;
1415     bool is_block_migration;
1416 } HMPMigrationStatus;
1417
1418 static void hmp_migrate_status_cb(void *opaque)
1419 {
1420     HMPMigrationStatus *status = opaque;
1421     MigrationInfo *info;
1422
1423     info = qmp_query_migrate(NULL);
1424     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1425         info->status == MIGRATION_STATUS_SETUP) {
1426         if (info->has_disk) {
1427             int progress;
1428
1429             if (info->disk->remaining) {
1430                 progress = info->disk->transferred * 100 / info->disk->total;
1431             } else {
1432                 progress = 100;
1433             }
1434
1435             monitor_printf(status->mon, "Completed %d %%\r", progress);
1436             monitor_flush(status->mon);
1437         }
1438
1439         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1440     } else {
1441         if (status->is_block_migration) {
1442             monitor_printf(status->mon, "\n");
1443         }
1444         monitor_resume(status->mon);
1445         timer_del(status->timer);
1446         g_free(status);
1447     }
1448
1449     qapi_free_MigrationInfo(info);
1450 }
1451
1452 void hmp_migrate(Monitor *mon, const QDict *qdict)
1453 {
1454     int detach = qdict_get_try_bool(qdict, "detach", 0);
1455     int blk = qdict_get_try_bool(qdict, "blk", 0);
1456     int inc = qdict_get_try_bool(qdict, "inc", 0);
1457     const char *uri = qdict_get_str(qdict, "uri");
1458     Error *err = NULL;
1459
1460     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1461     if (err) {
1462         monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1463         error_free(err);
1464         return;
1465     }
1466
1467     if (!detach) {
1468         HMPMigrationStatus *status;
1469
1470         if (monitor_suspend(mon) < 0) {
1471             monitor_printf(mon, "terminal does not allow synchronous "
1472                            "migration, continuing detached\n");
1473             return;
1474         }
1475
1476         status = g_malloc0(sizeof(*status));
1477         status->mon = mon;
1478         status->is_block_migration = blk || inc;
1479         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1480                                           status);
1481         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1482     }
1483 }
1484
1485 void hmp_device_del(Monitor *mon, const QDict *qdict)
1486 {
1487     const char *id = qdict_get_str(qdict, "id");
1488     Error *err = NULL;
1489
1490     qmp_device_del(id, &err);
1491     hmp_handle_error(mon, &err);
1492 }
1493
1494 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1495 {
1496     Error *err = NULL;
1497     int paging = qdict_get_try_bool(qdict, "paging", 0);
1498     int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1499     int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1500     int snappy = qdict_get_try_bool(qdict, "snappy", 0);
1501     const char *file = qdict_get_str(qdict, "filename");
1502     bool has_begin = qdict_haskey(qdict, "begin");
1503     bool has_length = qdict_haskey(qdict, "length");
1504     int64_t begin = 0;
1505     int64_t length = 0;
1506     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1507     char *prot;
1508
1509     if (zlib + lzo + snappy > 1) {
1510         error_setg(&err, "only one of '-z|-l|-s' can be set");
1511         hmp_handle_error(mon, &err);
1512         return;
1513     }
1514
1515     if (zlib) {
1516         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1517     }
1518
1519     if (lzo) {
1520         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1521     }
1522
1523     if (snappy) {
1524         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1525     }
1526
1527     if (has_begin) {
1528         begin = qdict_get_int(qdict, "begin");
1529     }
1530     if (has_length) {
1531         length = qdict_get_int(qdict, "length");
1532     }
1533
1534     prot = g_strconcat("file:", file, NULL);
1535
1536     qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1537                           true, dump_format, &err);
1538     hmp_handle_error(mon, &err);
1539     g_free(prot);
1540 }
1541
1542 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1543 {
1544     Error *err = NULL;
1545     QemuOpts *opts;
1546
1547     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1548     if (err) {
1549         goto out;
1550     }
1551
1552     netdev_add(opts, &err);
1553     if (err) {
1554         qemu_opts_del(opts);
1555     }
1556
1557 out:
1558     hmp_handle_error(mon, &err);
1559 }
1560
1561 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1562 {
1563     const char *id = qdict_get_str(qdict, "id");
1564     Error *err = NULL;
1565
1566     qmp_netdev_del(id, &err);
1567     hmp_handle_error(mon, &err);
1568 }
1569
1570 void hmp_object_add(Monitor *mon, const QDict *qdict)
1571 {
1572     Error *err = NULL;
1573     Error *err_end = NULL;
1574     QemuOpts *opts;
1575     char *type = NULL;
1576     char *id = NULL;
1577     void *dummy = NULL;
1578     OptsVisitor *ov;
1579     QDict *pdict;
1580
1581     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1582     if (err) {
1583         goto out;
1584     }
1585
1586     ov = opts_visitor_new(opts);
1587     pdict = qdict_clone_shallow(qdict);
1588
1589     visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1590     if (err) {
1591         goto out_clean;
1592     }
1593
1594     qdict_del(pdict, "qom-type");
1595     visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1596     if (err) {
1597         goto out_end;
1598     }
1599
1600     qdict_del(pdict, "id");
1601     visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1602     if (err) {
1603         goto out_end;
1604     }
1605
1606     object_add(type, id, pdict, opts_get_visitor(ov), &err);
1607
1608 out_end:
1609     visit_end_struct(opts_get_visitor(ov), &err_end);
1610     if (!err && err_end) {
1611         qmp_object_del(id, NULL);
1612     }
1613     error_propagate(&err, err_end);
1614 out_clean:
1615     opts_visitor_cleanup(ov);
1616
1617     QDECREF(pdict);
1618     qemu_opts_del(opts);
1619     g_free(id);
1620     g_free(type);
1621     g_free(dummy);
1622
1623 out:
1624     hmp_handle_error(mon, &err);
1625 }
1626
1627 void hmp_getfd(Monitor *mon, const QDict *qdict)
1628 {
1629     const char *fdname = qdict_get_str(qdict, "fdname");
1630     Error *err = NULL;
1631
1632     qmp_getfd(fdname, &err);
1633     hmp_handle_error(mon, &err);
1634 }
1635
1636 void hmp_closefd(Monitor *mon, const QDict *qdict)
1637 {
1638     const char *fdname = qdict_get_str(qdict, "fdname");
1639     Error *err = NULL;
1640
1641     qmp_closefd(fdname, &err);
1642     hmp_handle_error(mon, &err);
1643 }
1644
1645 void hmp_sendkey(Monitor *mon, const QDict *qdict)
1646 {
1647     const char *keys = qdict_get_str(qdict, "keys");
1648     KeyValueList *keylist, *head = NULL, *tmp = NULL;
1649     int has_hold_time = qdict_haskey(qdict, "hold-time");
1650     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1651     Error *err = NULL;
1652     char keyname_buf[16];
1653     char *separator;
1654     int keyname_len;
1655
1656     while (1) {
1657         separator = strchr(keys, '-');
1658         keyname_len = separator ? separator - keys : strlen(keys);
1659         pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1660
1661         /* Be compatible with old interface, convert user inputted "<" */
1662         if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1663             pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1664             keyname_len = 4;
1665         }
1666         keyname_buf[keyname_len] = 0;
1667
1668         keylist = g_malloc0(sizeof(*keylist));
1669         keylist->value = g_malloc0(sizeof(*keylist->value));
1670
1671         if (!head) {
1672             head = keylist;
1673         }
1674         if (tmp) {
1675             tmp->next = keylist;
1676         }
1677         tmp = keylist;
1678
1679         if (strstart(keyname_buf, "0x", NULL)) {
1680             char *endp;
1681             int value = strtoul(keyname_buf, &endp, 0);
1682             if (*endp != '\0') {
1683                 goto err_out;
1684             }
1685             keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1686             keylist->value->number = value;
1687         } else {
1688             int idx = index_from_key(keyname_buf);
1689             if (idx == Q_KEY_CODE_MAX) {
1690                 goto err_out;
1691             }
1692             keylist->value->kind = KEY_VALUE_KIND_QCODE;
1693             keylist->value->qcode = idx;
1694         }
1695
1696         if (!separator) {
1697             break;
1698         }
1699         keys = separator + 1;
1700     }
1701
1702     qmp_send_key(head, has_hold_time, hold_time, &err);
1703     hmp_handle_error(mon, &err);
1704
1705 out:
1706     qapi_free_KeyValueList(head);
1707     return;
1708
1709 err_out:
1710     monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1711     goto out;
1712 }
1713
1714 void hmp_screendump(Monitor *mon, const QDict *qdict)
1715 {
1716     const char *filename = qdict_get_str(qdict, "filename");
1717     Error *err = NULL;
1718
1719     qmp_screendump(filename, &err);
1720     hmp_handle_error(mon, &err);
1721 }
1722
1723 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1724 {
1725     const char *uri = qdict_get_str(qdict, "uri");
1726     int writable = qdict_get_try_bool(qdict, "writable", 0);
1727     int all = qdict_get_try_bool(qdict, "all", 0);
1728     Error *local_err = NULL;
1729     BlockInfoList *block_list, *info;
1730     SocketAddress *addr;
1731
1732     if (writable && !all) {
1733         error_setg(&local_err, "-w only valid together with -a");
1734         goto exit;
1735     }
1736
1737     /* First check if the address is valid and start the server.  */
1738     addr = socket_parse(uri, &local_err);
1739     if (local_err != NULL) {
1740         goto exit;
1741     }
1742
1743     qmp_nbd_server_start(addr, &local_err);
1744     qapi_free_SocketAddress(addr);
1745     if (local_err != NULL) {
1746         goto exit;
1747     }
1748
1749     if (!all) {
1750         return;
1751     }
1752
1753     /* Then try adding all block devices.  If one fails, close all and
1754      * exit.
1755      */
1756     block_list = qmp_query_block(NULL);
1757
1758     for (info = block_list; info; info = info->next) {
1759         if (!info->value->has_inserted) {
1760             continue;
1761         }
1762
1763         qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1764
1765         if (local_err != NULL) {
1766             qmp_nbd_server_stop(NULL);
1767             break;
1768         }
1769     }
1770
1771     qapi_free_BlockInfoList(block_list);
1772
1773 exit:
1774     hmp_handle_error(mon, &local_err);
1775 }
1776
1777 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1778 {
1779     const char *device = qdict_get_str(qdict, "device");
1780     int writable = qdict_get_try_bool(qdict, "writable", 0);
1781     Error *local_err = NULL;
1782
1783     qmp_nbd_server_add(device, true, writable, &local_err);
1784
1785     if (local_err != NULL) {
1786         hmp_handle_error(mon, &local_err);
1787     }
1788 }
1789
1790 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1791 {
1792     Error *err = NULL;
1793
1794     qmp_nbd_server_stop(&err);
1795     hmp_handle_error(mon, &err);
1796 }
1797
1798 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1799 {
1800     int cpuid;
1801     Error *err = NULL;
1802
1803     cpuid = qdict_get_int(qdict, "id");
1804     qmp_cpu_add(cpuid, &err);
1805     hmp_handle_error(mon, &err);
1806 }
1807
1808 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1809 {
1810     const char *args = qdict_get_str(qdict, "args");
1811     Error *err = NULL;
1812     QemuOpts *opts;
1813
1814     opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1815     if (opts == NULL) {
1816         error_setg(&err, "Parsing chardev args failed");
1817     } else {
1818         qemu_chr_new_from_opts(opts, NULL, &err);
1819     }
1820     hmp_handle_error(mon, &err);
1821 }
1822
1823 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1824 {
1825     Error *local_err = NULL;
1826
1827     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1828     hmp_handle_error(mon, &local_err);
1829 }
1830
1831 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1832 {
1833     BlockBackend *blk;
1834     const char* device = qdict_get_str(qdict, "device");
1835     const char* command = qdict_get_str(qdict, "command");
1836     Error *err = NULL;
1837
1838     blk = blk_by_name(device);
1839     if (blk) {
1840         qemuio_command(blk, command);
1841     } else {
1842         error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1843     }
1844
1845     hmp_handle_error(mon, &err);
1846 }
1847
1848 void hmp_object_del(Monitor *mon, const QDict *qdict)
1849 {
1850     const char *id = qdict_get_str(qdict, "id");
1851     Error *err = NULL;
1852
1853     qmp_object_del(id, &err);
1854     hmp_handle_error(mon, &err);
1855 }
1856
1857 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
1858 {
1859     Error *err = NULL;
1860     MemdevList *memdev_list = qmp_query_memdev(&err);
1861     MemdevList *m = memdev_list;
1862     StringOutputVisitor *ov;
1863     char *str;
1864     int i = 0;
1865
1866
1867     while (m) {
1868         ov = string_output_visitor_new(false);
1869         visit_type_uint16List(string_output_get_visitor(ov),
1870                               &m->value->host_nodes, NULL, NULL);
1871         monitor_printf(mon, "memory backend: %d\n", i);
1872         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
1873         monitor_printf(mon, "  merge: %s\n",
1874                        m->value->merge ? "true" : "false");
1875         monitor_printf(mon, "  dump: %s\n",
1876                        m->value->dump ? "true" : "false");
1877         monitor_printf(mon, "  prealloc: %s\n",
1878                        m->value->prealloc ? "true" : "false");
1879         monitor_printf(mon, "  policy: %s\n",
1880                        HostMemPolicy_lookup[m->value->policy]);
1881         str = string_output_get_string(ov);
1882         monitor_printf(mon, "  host nodes: %s\n", str);
1883
1884         g_free(str);
1885         string_output_visitor_cleanup(ov);
1886         m = m->next;
1887         i++;
1888     }
1889
1890     monitor_printf(mon, "\n");
1891
1892     qapi_free_MemdevList(memdev_list);
1893 }
1894
1895 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
1896 {
1897     Error *err = NULL;
1898     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
1899     MemoryDeviceInfoList *info;
1900     MemoryDeviceInfo *value;
1901     PCDIMMDeviceInfo *di;
1902
1903     for (info = info_list; info; info = info->next) {
1904         value = info->value;
1905
1906         if (value) {
1907             switch (value->kind) {
1908             case MEMORY_DEVICE_INFO_KIND_DIMM:
1909                 di = value->dimm;
1910
1911                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1912                                MemoryDeviceInfoKind_lookup[value->kind],
1913                                di->id ? di->id : "");
1914                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
1915                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
1916                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
1917                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
1918                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
1919                 monitor_printf(mon, "  hotplugged: %s\n",
1920                                di->hotplugged ? "true" : "false");
1921                 monitor_printf(mon, "  hotpluggable: %s\n",
1922                                di->hotpluggable ? "true" : "false");
1923                 break;
1924             default:
1925                 break;
1926             }
1927         }
1928     }
1929
1930     qapi_free_MemoryDeviceInfoList(info_list);
1931 }
1932
1933 void hmp_qom_list(Monitor *mon, const QDict *qdict)
1934 {
1935     const char *path = qdict_get_try_str(qdict, "path");
1936     ObjectPropertyInfoList *list;
1937     Error *err = NULL;
1938
1939     if (path == NULL) {
1940         monitor_printf(mon, "/\n");
1941         return;
1942     }
1943
1944     list = qmp_qom_list(path, &err);
1945     if (err == NULL) {
1946         ObjectPropertyInfoList *start = list;
1947         while (list != NULL) {
1948             ObjectPropertyInfo *value = list->value;
1949
1950             monitor_printf(mon, "%s (%s)\n",
1951                            value->name, value->type);
1952             list = list->next;
1953         }
1954         qapi_free_ObjectPropertyInfoList(start);
1955     }
1956     hmp_handle_error(mon, &err);
1957 }
1958
1959 void hmp_qom_set(Monitor *mon, const QDict *qdict)
1960 {
1961     const char *path = qdict_get_str(qdict, "path");
1962     const char *property = qdict_get_str(qdict, "property");
1963     const char *value = qdict_get_str(qdict, "value");
1964     Error *err = NULL;
1965     bool ambiguous = false;
1966     Object *obj;
1967
1968     obj = object_resolve_path(path, &ambiguous);
1969     if (obj == NULL) {
1970         error_set(&err, QERR_DEVICE_NOT_FOUND, path);
1971     } else {
1972         if (ambiguous) {
1973             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
1974         }
1975         object_property_parse(obj, value, property, &err);
1976     }
1977     hmp_handle_error(mon, &err);
1978 }