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