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