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