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