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