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