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