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