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