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