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