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