Merge remote-tracking branch 'remotes/ehabkost/tags/x86-and-machine-pull-request...
[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_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1455 {
1456     Error *err = NULL;
1457
1458     qmp_x_colo_lost_heartbeat(&err);
1459     hmp_handle_error(mon, &err);
1460 }
1461
1462 void hmp_set_password(Monitor *mon, const QDict *qdict)
1463 {
1464     const char *protocol  = qdict_get_str(qdict, "protocol");
1465     const char *password  = qdict_get_str(qdict, "password");
1466     const char *connected = qdict_get_try_str(qdict, "connected");
1467     Error *err = NULL;
1468
1469     qmp_set_password(protocol, password, !!connected, connected, &err);
1470     hmp_handle_error(mon, &err);
1471 }
1472
1473 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1474 {
1475     const char *protocol  = qdict_get_str(qdict, "protocol");
1476     const char *whenstr = qdict_get_str(qdict, "time");
1477     Error *err = NULL;
1478
1479     qmp_expire_password(protocol, whenstr, &err);
1480     hmp_handle_error(mon, &err);
1481 }
1482
1483 void hmp_eject(Monitor *mon, const QDict *qdict)
1484 {
1485     bool force = qdict_get_try_bool(qdict, "force", false);
1486     const char *device = qdict_get_str(qdict, "device");
1487     Error *err = NULL;
1488
1489     qmp_eject(true, device, false, NULL, true, force, &err);
1490     hmp_handle_error(mon, &err);
1491 }
1492
1493 static void hmp_change_read_arg(void *opaque, const char *password,
1494                                 void *readline_opaque)
1495 {
1496     qmp_change_vnc_password(password, NULL);
1497     monitor_read_command(opaque, 1);
1498 }
1499
1500 void hmp_change(Monitor *mon, const QDict *qdict)
1501 {
1502     const char *device = qdict_get_str(qdict, "device");
1503     const char *target = qdict_get_str(qdict, "target");
1504     const char *arg = qdict_get_try_str(qdict, "arg");
1505     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1506     BlockdevChangeReadOnlyMode read_only_mode = 0;
1507     Error *err = NULL;
1508
1509     if (strcmp(device, "vnc") == 0) {
1510         if (read_only) {
1511             monitor_printf(mon,
1512                            "Parameter 'read-only-mode' is invalid for VNC\n");
1513             return;
1514         }
1515         if (strcmp(target, "passwd") == 0 ||
1516             strcmp(target, "password") == 0) {
1517             if (!arg) {
1518                 monitor_read_password(mon, hmp_change_read_arg, NULL);
1519                 return;
1520             }
1521         }
1522         qmp_change("vnc", target, !!arg, arg, &err);
1523     } else {
1524         if (read_only) {
1525             read_only_mode =
1526                 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
1527                                 read_only, BLOCKDEV_CHANGE_READ_ONLY_MODE__MAX,
1528                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1529             if (err) {
1530                 hmp_handle_error(mon, &err);
1531                 return;
1532             }
1533         }
1534
1535         qmp_blockdev_change_medium(true, device, false, NULL, target,
1536                                    !!arg, arg, !!read_only, read_only_mode,
1537                                    &err);
1538         if (err &&
1539             error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1540             error_free(err);
1541             monitor_read_block_device_key(mon, device, NULL, NULL);
1542             return;
1543         }
1544     }
1545
1546     hmp_handle_error(mon, &err);
1547 }
1548
1549 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1550 {
1551     Error *err = NULL;
1552     BlockIOThrottle throttle = {
1553         .device = (char *) qdict_get_str(qdict, "device"),
1554         .bps = qdict_get_int(qdict, "bps"),
1555         .bps_rd = qdict_get_int(qdict, "bps_rd"),
1556         .bps_wr = qdict_get_int(qdict, "bps_wr"),
1557         .iops = qdict_get_int(qdict, "iops"),
1558         .iops_rd = qdict_get_int(qdict, "iops_rd"),
1559         .iops_wr = qdict_get_int(qdict, "iops_wr"),
1560     };
1561
1562     qmp_block_set_io_throttle(&throttle, &err);
1563     hmp_handle_error(mon, &err);
1564 }
1565
1566 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1567 {
1568     Error *error = NULL;
1569     const char *device = qdict_get_str(qdict, "device");
1570     const char *base = qdict_get_try_str(qdict, "base");
1571     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1572
1573     qmp_block_stream(false, NULL, device, base != NULL, base, false, NULL,
1574                      false, NULL, qdict_haskey(qdict, "speed"), speed,
1575                      true, BLOCKDEV_ON_ERROR_REPORT, &error);
1576
1577     hmp_handle_error(mon, &error);
1578 }
1579
1580 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1581 {
1582     Error *error = NULL;
1583     const char *device = qdict_get_str(qdict, "device");
1584     int64_t value = qdict_get_int(qdict, "speed");
1585
1586     qmp_block_job_set_speed(device, value, &error);
1587
1588     hmp_handle_error(mon, &error);
1589 }
1590
1591 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1592 {
1593     Error *error = NULL;
1594     const char *device = qdict_get_str(qdict, "device");
1595     bool force = qdict_get_try_bool(qdict, "force", false);
1596
1597     qmp_block_job_cancel(device, true, force, &error);
1598
1599     hmp_handle_error(mon, &error);
1600 }
1601
1602 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1603 {
1604     Error *error = NULL;
1605     const char *device = qdict_get_str(qdict, "device");
1606
1607     qmp_block_job_pause(device, &error);
1608
1609     hmp_handle_error(mon, &error);
1610 }
1611
1612 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1613 {
1614     Error *error = NULL;
1615     const char *device = qdict_get_str(qdict, "device");
1616
1617     qmp_block_job_resume(device, &error);
1618
1619     hmp_handle_error(mon, &error);
1620 }
1621
1622 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1623 {
1624     Error *error = NULL;
1625     const char *device = qdict_get_str(qdict, "device");
1626
1627     qmp_block_job_complete(device, &error);
1628
1629     hmp_handle_error(mon, &error);
1630 }
1631
1632 typedef struct HMPMigrationStatus
1633 {
1634     QEMUTimer *timer;
1635     Monitor *mon;
1636     bool is_block_migration;
1637 } HMPMigrationStatus;
1638
1639 static void hmp_migrate_status_cb(void *opaque)
1640 {
1641     HMPMigrationStatus *status = opaque;
1642     MigrationInfo *info;
1643
1644     info = qmp_query_migrate(NULL);
1645     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1646         info->status == MIGRATION_STATUS_SETUP) {
1647         if (info->has_disk) {
1648             int progress;
1649
1650             if (info->disk->remaining) {
1651                 progress = info->disk->transferred * 100 / info->disk->total;
1652             } else {
1653                 progress = 100;
1654             }
1655
1656             monitor_printf(status->mon, "Completed %d %%\r", progress);
1657             monitor_flush(status->mon);
1658         }
1659
1660         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1661     } else {
1662         if (status->is_block_migration) {
1663             monitor_printf(status->mon, "\n");
1664         }
1665         if (info->has_error_desc) {
1666             error_report("%s", info->error_desc);
1667         }
1668         monitor_resume(status->mon);
1669         timer_del(status->timer);
1670         g_free(status);
1671     }
1672
1673     qapi_free_MigrationInfo(info);
1674 }
1675
1676 void hmp_migrate(Monitor *mon, const QDict *qdict)
1677 {
1678     bool detach = qdict_get_try_bool(qdict, "detach", false);
1679     bool blk = qdict_get_try_bool(qdict, "blk", false);
1680     bool inc = qdict_get_try_bool(qdict, "inc", false);
1681     const char *uri = qdict_get_str(qdict, "uri");
1682     Error *err = NULL;
1683
1684     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1685     if (err) {
1686         error_report_err(err);
1687         return;
1688     }
1689
1690     if (!detach) {
1691         HMPMigrationStatus *status;
1692
1693         if (monitor_suspend(mon) < 0) {
1694             monitor_printf(mon, "terminal does not allow synchronous "
1695                            "migration, continuing detached\n");
1696             return;
1697         }
1698
1699         status = g_malloc0(sizeof(*status));
1700         status->mon = mon;
1701         status->is_block_migration = blk || inc;
1702         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1703                                           status);
1704         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1705     }
1706 }
1707
1708 void hmp_device_add(Monitor *mon, const QDict *qdict)
1709 {
1710     Error *err = NULL;
1711
1712     qmp_device_add((QDict *)qdict, NULL, &err);
1713     hmp_handle_error(mon, &err);
1714 }
1715
1716 void hmp_device_del(Monitor *mon, const QDict *qdict)
1717 {
1718     const char *id = qdict_get_str(qdict, "id");
1719     Error *err = NULL;
1720
1721     qmp_device_del(id, &err);
1722     hmp_handle_error(mon, &err);
1723 }
1724
1725 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1726 {
1727     Error *err = NULL;
1728     bool paging = qdict_get_try_bool(qdict, "paging", false);
1729     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
1730     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
1731     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
1732     const char *file = qdict_get_str(qdict, "filename");
1733     bool has_begin = qdict_haskey(qdict, "begin");
1734     bool has_length = qdict_haskey(qdict, "length");
1735     bool has_detach = qdict_haskey(qdict, "detach");
1736     int64_t begin = 0;
1737     int64_t length = 0;
1738     bool detach = false;
1739     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1740     char *prot;
1741
1742     if (zlib + lzo + snappy > 1) {
1743         error_setg(&err, "only one of '-z|-l|-s' can be set");
1744         hmp_handle_error(mon, &err);
1745         return;
1746     }
1747
1748     if (zlib) {
1749         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1750     }
1751
1752     if (lzo) {
1753         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1754     }
1755
1756     if (snappy) {
1757         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1758     }
1759
1760     if (has_begin) {
1761         begin = qdict_get_int(qdict, "begin");
1762     }
1763     if (has_length) {
1764         length = qdict_get_int(qdict, "length");
1765     }
1766     if (has_detach) {
1767         detach = qdict_get_bool(qdict, "detach");
1768     }
1769
1770     prot = g_strconcat("file:", file, NULL);
1771
1772     qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
1773                           has_length, length, true, dump_format, &err);
1774     hmp_handle_error(mon, &err);
1775     g_free(prot);
1776 }
1777
1778 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1779 {
1780     Error *err = NULL;
1781     QemuOpts *opts;
1782
1783     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1784     if (err) {
1785         goto out;
1786     }
1787
1788     netdev_add(opts, &err);
1789     if (err) {
1790         qemu_opts_del(opts);
1791     }
1792
1793 out:
1794     hmp_handle_error(mon, &err);
1795 }
1796
1797 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1798 {
1799     const char *id = qdict_get_str(qdict, "id");
1800     Error *err = NULL;
1801
1802     qmp_netdev_del(id, &err);
1803     hmp_handle_error(mon, &err);
1804 }
1805
1806 void hmp_object_add(Monitor *mon, const QDict *qdict)
1807 {
1808     Error *err = NULL;
1809     QemuOpts *opts;
1810     Visitor *v;
1811     Object *obj = NULL;
1812
1813     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1814     if (err) {
1815         hmp_handle_error(mon, &err);
1816         return;
1817     }
1818
1819     v = opts_visitor_new(opts);
1820     obj = user_creatable_add(qdict, v, &err);
1821     visit_free(v);
1822     qemu_opts_del(opts);
1823
1824     if (err) {
1825         hmp_handle_error(mon, &err);
1826     }
1827     if (obj) {
1828         object_unref(obj);
1829     }
1830 }
1831
1832 void hmp_getfd(Monitor *mon, const QDict *qdict)
1833 {
1834     const char *fdname = qdict_get_str(qdict, "fdname");
1835     Error *err = NULL;
1836
1837     qmp_getfd(fdname, &err);
1838     hmp_handle_error(mon, &err);
1839 }
1840
1841 void hmp_closefd(Monitor *mon, const QDict *qdict)
1842 {
1843     const char *fdname = qdict_get_str(qdict, "fdname");
1844     Error *err = NULL;
1845
1846     qmp_closefd(fdname, &err);
1847     hmp_handle_error(mon, &err);
1848 }
1849
1850 void hmp_sendkey(Monitor *mon, const QDict *qdict)
1851 {
1852     const char *keys = qdict_get_str(qdict, "keys");
1853     KeyValueList *keylist, *head = NULL, *tmp = NULL;
1854     int has_hold_time = qdict_haskey(qdict, "hold-time");
1855     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1856     Error *err = NULL;
1857     char *separator;
1858     int keyname_len;
1859
1860     while (1) {
1861         separator = strchr(keys, '-');
1862         keyname_len = separator ? separator - keys : strlen(keys);
1863
1864         /* Be compatible with old interface, convert user inputted "<" */
1865         if (keys[0] == '<' && keyname_len == 1) {
1866             keys = "less";
1867             keyname_len = 4;
1868         }
1869
1870         keylist = g_malloc0(sizeof(*keylist));
1871         keylist->value = g_malloc0(sizeof(*keylist->value));
1872
1873         if (!head) {
1874             head = keylist;
1875         }
1876         if (tmp) {
1877             tmp->next = keylist;
1878         }
1879         tmp = keylist;
1880
1881         if (strstart(keys, "0x", NULL)) {
1882             char *endp;
1883             int value = strtoul(keys, &endp, 0);
1884             assert(endp <= keys + keyname_len);
1885             if (endp != keys + keyname_len) {
1886                 goto err_out;
1887             }
1888             keylist->value->type = KEY_VALUE_KIND_NUMBER;
1889             keylist->value->u.number.data = value;
1890         } else {
1891             int idx = index_from_key(keys, keyname_len);
1892             if (idx == Q_KEY_CODE__MAX) {
1893                 goto err_out;
1894             }
1895             keylist->value->type = KEY_VALUE_KIND_QCODE;
1896             keylist->value->u.qcode.data = idx;
1897         }
1898
1899         if (!separator) {
1900             break;
1901         }
1902         keys = separator + 1;
1903     }
1904
1905     qmp_send_key(head, has_hold_time, hold_time, &err);
1906     hmp_handle_error(mon, &err);
1907
1908 out:
1909     qapi_free_KeyValueList(head);
1910     return;
1911
1912 err_out:
1913     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
1914     goto out;
1915 }
1916
1917 void hmp_screendump(Monitor *mon, const QDict *qdict)
1918 {
1919     const char *filename = qdict_get_str(qdict, "filename");
1920     Error *err = NULL;
1921
1922     qmp_screendump(filename, &err);
1923     hmp_handle_error(mon, &err);
1924 }
1925
1926 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1927 {
1928     const char *uri = qdict_get_str(qdict, "uri");
1929     bool writable = qdict_get_try_bool(qdict, "writable", false);
1930     bool all = qdict_get_try_bool(qdict, "all", false);
1931     Error *local_err = NULL;
1932     BlockInfoList *block_list, *info;
1933     SocketAddress *addr;
1934
1935     if (writable && !all) {
1936         error_setg(&local_err, "-w only valid together with -a");
1937         goto exit;
1938     }
1939
1940     /* First check if the address is valid and start the server.  */
1941     addr = socket_parse(uri, &local_err);
1942     if (local_err != NULL) {
1943         goto exit;
1944     }
1945
1946     qmp_nbd_server_start(addr, false, NULL, &local_err);
1947     qapi_free_SocketAddress(addr);
1948     if (local_err != NULL) {
1949         goto exit;
1950     }
1951
1952     if (!all) {
1953         return;
1954     }
1955
1956     /* Then try adding all block devices.  If one fails, close all and
1957      * exit.
1958      */
1959     block_list = qmp_query_block(NULL);
1960
1961     for (info = block_list; info; info = info->next) {
1962         if (!info->value->has_inserted) {
1963             continue;
1964         }
1965
1966         qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1967
1968         if (local_err != NULL) {
1969             qmp_nbd_server_stop(NULL);
1970             break;
1971         }
1972     }
1973
1974     qapi_free_BlockInfoList(block_list);
1975
1976 exit:
1977     hmp_handle_error(mon, &local_err);
1978 }
1979
1980 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1981 {
1982     const char *device = qdict_get_str(qdict, "device");
1983     bool writable = qdict_get_try_bool(qdict, "writable", false);
1984     Error *local_err = NULL;
1985
1986     qmp_nbd_server_add(device, true, writable, &local_err);
1987
1988     if (local_err != NULL) {
1989         hmp_handle_error(mon, &local_err);
1990     }
1991 }
1992
1993 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1994 {
1995     Error *err = NULL;
1996
1997     qmp_nbd_server_stop(&err);
1998     hmp_handle_error(mon, &err);
1999 }
2000
2001 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2002 {
2003     int cpuid;
2004     Error *err = NULL;
2005
2006     cpuid = qdict_get_int(qdict, "id");
2007     qmp_cpu_add(cpuid, &err);
2008     hmp_handle_error(mon, &err);
2009 }
2010
2011 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2012 {
2013     const char *args = qdict_get_str(qdict, "args");
2014     Error *err = NULL;
2015     QemuOpts *opts;
2016
2017     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2018     if (opts == NULL) {
2019         error_setg(&err, "Parsing chardev args failed");
2020     } else {
2021         qemu_chr_new_from_opts(opts, &err);
2022         qemu_opts_del(opts);
2023     }
2024     hmp_handle_error(mon, &err);
2025 }
2026
2027 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2028 {
2029     Error *local_err = NULL;
2030
2031     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2032     hmp_handle_error(mon, &local_err);
2033 }
2034
2035 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2036 {
2037     BlockBackend *blk;
2038     BlockBackend *local_blk = NULL;
2039     AioContext *aio_context;
2040     const char* device = qdict_get_str(qdict, "device");
2041     const char* command = qdict_get_str(qdict, "command");
2042     Error *err = NULL;
2043
2044     blk = blk_by_name(device);
2045     if (!blk) {
2046         BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2047         if (bs) {
2048             blk = local_blk = blk_new();
2049             blk_insert_bs(blk, bs);
2050         } else {
2051             goto fail;
2052         }
2053     }
2054
2055     aio_context = blk_get_aio_context(blk);
2056     aio_context_acquire(aio_context);
2057
2058     qemuio_command(blk, command);
2059
2060     aio_context_release(aio_context);
2061
2062 fail:
2063     blk_unref(local_blk);
2064     hmp_handle_error(mon, &err);
2065 }
2066
2067 void hmp_object_del(Monitor *mon, const QDict *qdict)
2068 {
2069     const char *id = qdict_get_str(qdict, "id");
2070     Error *err = NULL;
2071
2072     user_creatable_del(id, &err);
2073     hmp_handle_error(mon, &err);
2074 }
2075
2076 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2077 {
2078     Error *err = NULL;
2079     MemdevList *memdev_list = qmp_query_memdev(&err);
2080     MemdevList *m = memdev_list;
2081     Visitor *v;
2082     char *str;
2083     int i = 0;
2084
2085
2086     while (m) {
2087         v = string_output_visitor_new(false, &str);
2088         visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2089         monitor_printf(mon, "memory backend: %d\n", i);
2090         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
2091         monitor_printf(mon, "  merge: %s\n",
2092                        m->value->merge ? "true" : "false");
2093         monitor_printf(mon, "  dump: %s\n",
2094                        m->value->dump ? "true" : "false");
2095         monitor_printf(mon, "  prealloc: %s\n",
2096                        m->value->prealloc ? "true" : "false");
2097         monitor_printf(mon, "  policy: %s\n",
2098                        HostMemPolicy_lookup[m->value->policy]);
2099         visit_complete(v, &str);
2100         monitor_printf(mon, "  host nodes: %s\n", str);
2101
2102         g_free(str);
2103         visit_free(v);
2104         m = m->next;
2105         i++;
2106     }
2107
2108     monitor_printf(mon, "\n");
2109
2110     qapi_free_MemdevList(memdev_list);
2111 }
2112
2113 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2114 {
2115     Error *err = NULL;
2116     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2117     MemoryDeviceInfoList *info;
2118     MemoryDeviceInfo *value;
2119     PCDIMMDeviceInfo *di;
2120
2121     for (info = info_list; info; info = info->next) {
2122         value = info->value;
2123
2124         if (value) {
2125             switch (value->type) {
2126             case MEMORY_DEVICE_INFO_KIND_DIMM:
2127                 di = value->u.dimm.data;
2128
2129                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2130                                MemoryDeviceInfoKind_lookup[value->type],
2131                                di->id ? di->id : "");
2132                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
2133                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
2134                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
2135                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
2136                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
2137                 monitor_printf(mon, "  hotplugged: %s\n",
2138                                di->hotplugged ? "true" : "false");
2139                 monitor_printf(mon, "  hotpluggable: %s\n",
2140                                di->hotpluggable ? "true" : "false");
2141                 break;
2142             default:
2143                 break;
2144             }
2145         }
2146     }
2147
2148     qapi_free_MemoryDeviceInfoList(info_list);
2149 }
2150
2151 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2152 {
2153     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2154     IOThreadInfoList *info;
2155
2156     for (info = info_list; info; info = info->next) {
2157         monitor_printf(mon, "%s: thread_id=%" PRId64 "\n",
2158                        info->value->id, info->value->thread_id);
2159     }
2160
2161     qapi_free_IOThreadInfoList(info_list);
2162 }
2163
2164 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2165 {
2166     const char *path = qdict_get_try_str(qdict, "path");
2167     ObjectPropertyInfoList *list;
2168     Error *err = NULL;
2169
2170     if (path == NULL) {
2171         monitor_printf(mon, "/\n");
2172         return;
2173     }
2174
2175     list = qmp_qom_list(path, &err);
2176     if (err == NULL) {
2177         ObjectPropertyInfoList *start = list;
2178         while (list != NULL) {
2179             ObjectPropertyInfo *value = list->value;
2180
2181             monitor_printf(mon, "%s (%s)\n",
2182                            value->name, value->type);
2183             list = list->next;
2184         }
2185         qapi_free_ObjectPropertyInfoList(start);
2186     }
2187     hmp_handle_error(mon, &err);
2188 }
2189
2190 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2191 {
2192     const char *path = qdict_get_str(qdict, "path");
2193     const char *property = qdict_get_str(qdict, "property");
2194     const char *value = qdict_get_str(qdict, "value");
2195     Error *err = NULL;
2196     bool ambiguous = false;
2197     Object *obj;
2198
2199     obj = object_resolve_path(path, &ambiguous);
2200     if (obj == NULL) {
2201         error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2202                   "Device '%s' not found", path);
2203     } else {
2204         if (ambiguous) {
2205             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2206         }
2207         object_property_parse(obj, value, property, &err);
2208     }
2209     hmp_handle_error(mon, &err);
2210 }
2211
2212 void hmp_rocker(Monitor *mon, const QDict *qdict)
2213 {
2214     const char *name = qdict_get_str(qdict, "name");
2215     RockerSwitch *rocker;
2216     Error *err = NULL;
2217
2218     rocker = qmp_query_rocker(name, &err);
2219     if (err != NULL) {
2220         hmp_handle_error(mon, &err);
2221         return;
2222     }
2223
2224     monitor_printf(mon, "name: %s\n", rocker->name);
2225     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2226     monitor_printf(mon, "ports: %d\n", rocker->ports);
2227
2228     qapi_free_RockerSwitch(rocker);
2229 }
2230
2231 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2232 {
2233     RockerPortList *list, *port;
2234     const char *name = qdict_get_str(qdict, "name");
2235     Error *err = NULL;
2236
2237     list = qmp_query_rocker_ports(name, &err);
2238     if (err != NULL) {
2239         hmp_handle_error(mon, &err);
2240         return;
2241     }
2242
2243     monitor_printf(mon, "            ena/    speed/ auto\n");
2244     monitor_printf(mon, "      port  link    duplex neg?\n");
2245
2246     for (port = list; port; port = port->next) {
2247         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
2248                        port->value->name,
2249                        port->value->enabled ? port->value->link_up ?
2250                        "up" : "down" : "!ena",
2251                        port->value->speed == 10000 ? "10G" : "??",
2252                        port->value->duplex ? "FD" : "HD",
2253                        port->value->autoneg ? "Yes" : "No");
2254     }
2255
2256     qapi_free_RockerPortList(list);
2257 }
2258
2259 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2260 {
2261     RockerOfDpaFlowList *list, *info;
2262     const char *name = qdict_get_str(qdict, "name");
2263     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2264     Error *err = NULL;
2265
2266     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2267     if (err != NULL) {
2268         hmp_handle_error(mon, &err);
2269         return;
2270     }
2271
2272     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2273
2274     for (info = list; info; info = info->next) {
2275         RockerOfDpaFlow *flow = info->value;
2276         RockerOfDpaFlowKey *key = flow->key;
2277         RockerOfDpaFlowMask *mask = flow->mask;
2278         RockerOfDpaFlowAction *action = flow->action;
2279
2280         if (flow->hits) {
2281             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2282                            key->priority, key->tbl_id, flow->hits);
2283         } else {
2284             monitor_printf(mon, "%-4d %-3d     ",
2285                            key->priority, key->tbl_id);
2286         }
2287
2288         if (key->has_in_pport) {
2289             monitor_printf(mon, " pport %d", key->in_pport);
2290             if (mask->has_in_pport) {
2291                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2292             }
2293         }
2294
2295         if (key->has_vlan_id) {
2296             monitor_printf(mon, " vlan %d",
2297                            key->vlan_id & VLAN_VID_MASK);
2298             if (mask->has_vlan_id) {
2299                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2300             }
2301         }
2302
2303         if (key->has_tunnel_id) {
2304             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2305             if (mask->has_tunnel_id) {
2306                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2307             }
2308         }
2309
2310         if (key->has_eth_type) {
2311             switch (key->eth_type) {
2312             case 0x0806:
2313                 monitor_printf(mon, " ARP");
2314                 break;
2315             case 0x0800:
2316                 monitor_printf(mon, " IP");
2317                 break;
2318             case 0x86dd:
2319                 monitor_printf(mon, " IPv6");
2320                 break;
2321             case 0x8809:
2322                 monitor_printf(mon, " LACP");
2323                 break;
2324             case 0x88cc:
2325                 monitor_printf(mon, " LLDP");
2326                 break;
2327             default:
2328                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2329                 break;
2330             }
2331         }
2332
2333         if (key->has_eth_src) {
2334             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2335                 (mask->has_eth_src) &&
2336                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2337                 monitor_printf(mon, " src <any mcast/bcast>");
2338             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2339                 (mask->has_eth_src) &&
2340                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2341                 monitor_printf(mon, " src <any ucast>");
2342             } else {
2343                 monitor_printf(mon, " src %s", key->eth_src);
2344                 if (mask->has_eth_src) {
2345                     monitor_printf(mon, "(%s)", mask->eth_src);
2346                 }
2347             }
2348         }
2349
2350         if (key->has_eth_dst) {
2351             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2352                 (mask->has_eth_dst) &&
2353                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2354                 monitor_printf(mon, " dst <any mcast/bcast>");
2355             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2356                 (mask->has_eth_dst) &&
2357                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2358                 monitor_printf(mon, " dst <any ucast>");
2359             } else {
2360                 monitor_printf(mon, " dst %s", key->eth_dst);
2361                 if (mask->has_eth_dst) {
2362                     monitor_printf(mon, "(%s)", mask->eth_dst);
2363                 }
2364             }
2365         }
2366
2367         if (key->has_ip_proto) {
2368             monitor_printf(mon, " proto %d", key->ip_proto);
2369             if (mask->has_ip_proto) {
2370                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2371             }
2372         }
2373
2374         if (key->has_ip_tos) {
2375             monitor_printf(mon, " TOS %d", key->ip_tos);
2376             if (mask->has_ip_tos) {
2377                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2378             }
2379         }
2380
2381         if (key->has_ip_dst) {
2382             monitor_printf(mon, " dst %s", key->ip_dst);
2383         }
2384
2385         if (action->has_goto_tbl || action->has_group_id ||
2386             action->has_new_vlan_id) {
2387             monitor_printf(mon, " -->");
2388         }
2389
2390         if (action->has_new_vlan_id) {
2391             monitor_printf(mon, " apply new vlan %d",
2392                            ntohs(action->new_vlan_id));
2393         }
2394
2395         if (action->has_group_id) {
2396             monitor_printf(mon, " write group 0x%08x", action->group_id);
2397         }
2398
2399         if (action->has_goto_tbl) {
2400             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2401         }
2402
2403         monitor_printf(mon, "\n");
2404     }
2405
2406     qapi_free_RockerOfDpaFlowList(list);
2407 }
2408
2409 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2410 {
2411     RockerOfDpaGroupList *list, *g;
2412     const char *name = qdict_get_str(qdict, "name");
2413     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2414     Error *err = NULL;
2415     bool set = false;
2416
2417     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2418     if (err != NULL) {
2419         hmp_handle_error(mon, &err);
2420         return;
2421     }
2422
2423     monitor_printf(mon, "id (decode) --> buckets\n");
2424
2425     for (g = list; g; g = g->next) {
2426         RockerOfDpaGroup *group = g->value;
2427
2428         monitor_printf(mon, "0x%08x", group->id);
2429
2430         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2431                                          group->type == 1 ? "L2 rewrite" :
2432                                          group->type == 2 ? "L3 unicast" :
2433                                          group->type == 3 ? "L2 multicast" :
2434                                          group->type == 4 ? "L2 flood" :
2435                                          group->type == 5 ? "L3 interface" :
2436                                          group->type == 6 ? "L3 multicast" :
2437                                          group->type == 7 ? "L3 ECMP" :
2438                                          group->type == 8 ? "L2 overlay" :
2439                                          "unknown");
2440
2441         if (group->has_vlan_id) {
2442             monitor_printf(mon, " vlan %d", group->vlan_id);
2443         }
2444
2445         if (group->has_pport) {
2446             monitor_printf(mon, " pport %d", group->pport);
2447         }
2448
2449         if (group->has_index) {
2450             monitor_printf(mon, " index %d", group->index);
2451         }
2452
2453         monitor_printf(mon, ") -->");
2454
2455         if (group->has_set_vlan_id && group->set_vlan_id) {
2456             set = true;
2457             monitor_printf(mon, " set vlan %d",
2458                            group->set_vlan_id & VLAN_VID_MASK);
2459         }
2460
2461         if (group->has_set_eth_src) {
2462             if (!set) {
2463                 set = true;
2464                 monitor_printf(mon, " set");
2465             }
2466             monitor_printf(mon, " src %s", group->set_eth_src);
2467         }
2468
2469         if (group->has_set_eth_dst) {
2470             if (!set) {
2471                 set = true;
2472                 monitor_printf(mon, " set");
2473             }
2474             monitor_printf(mon, " dst %s", group->set_eth_dst);
2475         }
2476
2477         set = false;
2478
2479         if (group->has_ttl_check && group->ttl_check) {
2480             monitor_printf(mon, " check TTL");
2481         }
2482
2483         if (group->has_group_id && group->group_id) {
2484             monitor_printf(mon, " group id 0x%08x", group->group_id);
2485         }
2486
2487         if (group->has_pop_vlan && group->pop_vlan) {
2488             monitor_printf(mon, " pop vlan");
2489         }
2490
2491         if (group->has_out_pport) {
2492             monitor_printf(mon, " out pport %d", group->out_pport);
2493         }
2494
2495         if (group->has_group_ids) {
2496             struct uint32List *id;
2497
2498             monitor_printf(mon, " groups [");
2499             for (id = group->group_ids; id; id = id->next) {
2500                 monitor_printf(mon, "0x%08x", id->value);
2501                 if (id->next) {
2502                     monitor_printf(mon, ",");
2503                 }
2504             }
2505             monitor_printf(mon, "]");
2506         }
2507
2508         monitor_printf(mon, "\n");
2509     }
2510
2511     qapi_free_RockerOfDpaGroupList(list);
2512 }
2513
2514 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2515 {
2516     DumpQueryResult *result = qmp_query_dump(NULL);
2517
2518     assert(result && result->status < DUMP_STATUS__MAX);
2519     monitor_printf(mon, "Status: %s\n", DumpStatus_lookup[result->status]);
2520
2521     if (result->status == DUMP_STATUS_ACTIVE) {
2522         float percent = 0;
2523         assert(result->total != 0);
2524         percent = 100.0 * result->completed / result->total;
2525         monitor_printf(mon, "Finished: %.2f %%\n", percent);
2526     }
2527
2528     qapi_free_DumpQueryResult(result);
2529 }
2530
2531 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2532 {
2533     Error *err = NULL;
2534     HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2535     HotpluggableCPUList *saved = l;
2536     CpuInstanceProperties *c;
2537
2538     if (err != NULL) {
2539         hmp_handle_error(mon, &err);
2540         return;
2541     }
2542
2543     monitor_printf(mon, "Hotpluggable CPUs:\n");
2544     while (l) {
2545         monitor_printf(mon, "  type: \"%s\"\n", l->value->type);
2546         monitor_printf(mon, "  vcpus_count: \"%" PRIu64 "\"\n",
2547                        l->value->vcpus_count);
2548         if (l->value->has_qom_path) {
2549             monitor_printf(mon, "  qom_path: \"%s\"\n", l->value->qom_path);
2550         }
2551
2552         c = l->value->props;
2553         monitor_printf(mon, "  CPUInstance Properties:\n");
2554         if (c->has_node_id) {
2555             monitor_printf(mon, "    node-id: \"%" PRIu64 "\"\n", c->node_id);
2556         }
2557         if (c->has_socket_id) {
2558             monitor_printf(mon, "    socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2559         }
2560         if (c->has_core_id) {
2561             monitor_printf(mon, "    core-id: \"%" PRIu64 "\"\n", c->core_id);
2562         }
2563         if (c->has_thread_id) {
2564             monitor_printf(mon, "    thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2565         }
2566
2567         l = l->next;
2568     }
2569
2570     qapi_free_HotpluggableCPUList(saved);
2571 }