]> Git Repo - qemu.git/blob - hmp.c
qapi/block-stream: expose new job properties
[qemu.git] / hmp.c
1 /*
2  * Human Monitor Interface
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
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 "chardev/char.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/option.h"
25 #include "qemu/timer.h"
26 #include "qemu/sockets.h"
27 #include "monitor/monitor.h"
28 #include "monitor/qdev.h"
29 #include "qapi/error.h"
30 #include "qapi/opts-visitor.h"
31 #include "qapi/qapi-builtin-visit.h"
32 #include "qapi/qapi-commands-block.h"
33 #include "qapi/qapi-commands-char.h"
34 #include "qapi/qapi-commands-migration.h"
35 #include "qapi/qapi-commands-misc.h"
36 #include "qapi/qapi-commands-net.h"
37 #include "qapi/qapi-commands-rocker.h"
38 #include "qapi/qapi-commands-run-state.h"
39 #include "qapi/qapi-commands-tpm.h"
40 #include "qapi/qapi-commands-ui.h"
41 #include "qapi/qmp/qdict.h"
42 #include "qapi/qmp/qerror.h"
43 #include "qapi/string-input-visitor.h"
44 #include "qapi/string-output-visitor.h"
45 #include "qom/object_interfaces.h"
46 #include "ui/console.h"
47 #include "block/nbd.h"
48 #include "block/qapi.h"
49 #include "qemu-io.h"
50 #include "qemu/cutils.h"
51 #include "qemu/error-report.h"
52 #include "exec/ramlist.h"
53 #include "hw/intc/intc.h"
54 #include "migration/snapshot.h"
55 #include "migration/misc.h"
56
57 #ifdef CONFIG_SPICE
58 #include <spice/enums.h>
59 #endif
60
61 static void hmp_handle_error(Monitor *mon, Error **errp)
62 {
63     assert(errp);
64     if (*errp) {
65         error_report_err(*errp);
66     }
67 }
68
69 void hmp_info_name(Monitor *mon, const QDict *qdict)
70 {
71     NameInfo *info;
72
73     info = qmp_query_name(NULL);
74     if (info->has_name) {
75         monitor_printf(mon, "%s\n", info->name);
76     }
77     qapi_free_NameInfo(info);
78 }
79
80 void hmp_info_version(Monitor *mon, const QDict *qdict)
81 {
82     VersionInfo *info;
83
84     info = qmp_query_version(NULL);
85
86     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
87                    info->qemu->major, info->qemu->minor, info->qemu->micro,
88                    info->package);
89
90     qapi_free_VersionInfo(info);
91 }
92
93 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
94 {
95     KvmInfo *info;
96
97     info = qmp_query_kvm(NULL);
98     monitor_printf(mon, "kvm support: ");
99     if (info->present) {
100         monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
101     } else {
102         monitor_printf(mon, "not compiled\n");
103     }
104
105     qapi_free_KvmInfo(info);
106 }
107
108 void hmp_info_status(Monitor *mon, const QDict *qdict)
109 {
110     StatusInfo *info;
111
112     info = qmp_query_status(NULL);
113
114     monitor_printf(mon, "VM status: %s%s",
115                    info->running ? "running" : "paused",
116                    info->singlestep ? " (single step mode)" : "");
117
118     if (!info->running && info->status != RUN_STATE_PAUSED) {
119         monitor_printf(mon, " (%s)", RunState_str(info->status));
120     }
121
122     monitor_printf(mon, "\n");
123
124     qapi_free_StatusInfo(info);
125 }
126
127 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
128 {
129     UuidInfo *info;
130
131     info = qmp_query_uuid(NULL);
132     monitor_printf(mon, "%s\n", info->UUID);
133     qapi_free_UuidInfo(info);
134 }
135
136 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
137 {
138     ChardevInfoList *char_info, *info;
139
140     char_info = qmp_query_chardev(NULL);
141     for (info = char_info; info; info = info->next) {
142         monitor_printf(mon, "%s: filename=%s\n", info->value->label,
143                                                  info->value->filename);
144     }
145
146     qapi_free_ChardevInfoList(char_info);
147 }
148
149 void hmp_info_mice(Monitor *mon, const QDict *qdict)
150 {
151     MouseInfoList *mice_list, *mouse;
152
153     mice_list = qmp_query_mice(NULL);
154     if (!mice_list) {
155         monitor_printf(mon, "No mouse devices connected\n");
156         return;
157     }
158
159     for (mouse = mice_list; mouse; mouse = mouse->next) {
160         monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
161                        mouse->value->current ? '*' : ' ',
162                        mouse->value->index, mouse->value->name,
163                        mouse->value->absolute ? " (absolute)" : "");
164     }
165
166     qapi_free_MouseInfoList(mice_list);
167 }
168
169 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
170 {
171     MigrationInfo *info;
172     MigrationCapabilityStatusList *caps, *cap;
173
174     info = qmp_query_migrate(NULL);
175     caps = qmp_query_migrate_capabilities(NULL);
176
177     migration_global_dump(mon);
178
179     /* do not display parameters during setup */
180     if (info->has_status && caps) {
181         monitor_printf(mon, "capabilities: ");
182         for (cap = caps; cap; cap = cap->next) {
183             monitor_printf(mon, "%s: %s ",
184                            MigrationCapability_str(cap->value->capability),
185                            cap->value->state ? "on" : "off");
186         }
187         monitor_printf(mon, "\n");
188     }
189
190     if (info->has_status) {
191         monitor_printf(mon, "Migration status: %s",
192                        MigrationStatus_str(info->status));
193         if (info->status == MIGRATION_STATUS_FAILED &&
194             info->has_error_desc) {
195             monitor_printf(mon, " (%s)\n", info->error_desc);
196         } else {
197             monitor_printf(mon, "\n");
198         }
199
200         monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
201                        info->total_time);
202         if (info->has_expected_downtime) {
203             monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
204                            info->expected_downtime);
205         }
206         if (info->has_downtime) {
207             monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
208                            info->downtime);
209         }
210         if (info->has_setup_time) {
211             monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
212                            info->setup_time);
213         }
214     }
215
216     if (info->has_ram) {
217         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
218                        info->ram->transferred >> 10);
219         monitor_printf(mon, "throughput: %0.2f mbps\n",
220                        info->ram->mbps);
221         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
222                        info->ram->remaining >> 10);
223         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
224                        info->ram->total >> 10);
225         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
226                        info->ram->duplicate);
227         monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
228                        info->ram->skipped);
229         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
230                        info->ram->normal);
231         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
232                        info->ram->normal_bytes >> 10);
233         monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
234                        info->ram->dirty_sync_count);
235         monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
236                        info->ram->page_size >> 10);
237         monitor_printf(mon, "multifd bytes: %" PRIu64 " kbytes\n",
238                        info->ram->multifd_bytes >> 10);
239
240         if (info->ram->dirty_pages_rate) {
241             monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
242                            info->ram->dirty_pages_rate);
243         }
244         if (info->ram->postcopy_requests) {
245             monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
246                            info->ram->postcopy_requests);
247         }
248     }
249
250     if (info->has_disk) {
251         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
252                        info->disk->transferred >> 10);
253         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
254                        info->disk->remaining >> 10);
255         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
256                        info->disk->total >> 10);
257     }
258
259     if (info->has_xbzrle_cache) {
260         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
261                        info->xbzrle_cache->cache_size);
262         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
263                        info->xbzrle_cache->bytes >> 10);
264         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
265                        info->xbzrle_cache->pages);
266         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
267                        info->xbzrle_cache->cache_miss);
268         monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
269                        info->xbzrle_cache->cache_miss_rate);
270         monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
271                        info->xbzrle_cache->overflow);
272     }
273
274     if (info->has_cpu_throttle_percentage) {
275         monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
276                        info->cpu_throttle_percentage);
277     }
278
279     if (info->has_postcopy_blocktime) {
280         monitor_printf(mon, "postcopy blocktime: %u\n",
281                        info->postcopy_blocktime);
282     }
283
284     if (info->has_postcopy_vcpu_blocktime) {
285         Visitor *v;
286         char *str;
287         v = string_output_visitor_new(false, &str);
288         visit_type_uint32List(v, NULL, &info->postcopy_vcpu_blocktime, NULL);
289         visit_complete(v, &str);
290         monitor_printf(mon, "postcopy vcpu blocktime: %s\n", str);
291         g_free(str);
292         visit_free(v);
293     }
294     qapi_free_MigrationInfo(info);
295     qapi_free_MigrationCapabilityStatusList(caps);
296 }
297
298 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
299 {
300     MigrationCapabilityStatusList *caps, *cap;
301
302     caps = qmp_query_migrate_capabilities(NULL);
303
304     if (caps) {
305         for (cap = caps; cap; cap = cap->next) {
306             monitor_printf(mon, "%s: %s\n",
307                            MigrationCapability_str(cap->value->capability),
308                            cap->value->state ? "on" : "off");
309         }
310     }
311
312     qapi_free_MigrationCapabilityStatusList(caps);
313 }
314
315 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
316 {
317     MigrationParameters *params;
318
319     params = qmp_query_migrate_parameters(NULL);
320
321     if (params) {
322         assert(params->has_compress_level);
323         monitor_printf(mon, "%s: %u\n",
324             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
325             params->compress_level);
326         assert(params->has_compress_threads);
327         monitor_printf(mon, "%s: %u\n",
328             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
329             params->compress_threads);
330         assert(params->has_compress_wait_thread);
331         monitor_printf(mon, "%s: %s\n",
332             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD),
333             params->compress_wait_thread ? "on" : "off");
334         assert(params->has_decompress_threads);
335         monitor_printf(mon, "%s: %u\n",
336             MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
337             params->decompress_threads);
338         assert(params->has_cpu_throttle_initial);
339         monitor_printf(mon, "%s: %u\n",
340             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
341             params->cpu_throttle_initial);
342         assert(params->has_cpu_throttle_increment);
343         monitor_printf(mon, "%s: %u\n",
344             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
345             params->cpu_throttle_increment);
346         assert(params->has_max_cpu_throttle);
347         monitor_printf(mon, "%s: %u\n",
348             MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
349             params->max_cpu_throttle);
350         assert(params->has_tls_creds);
351         monitor_printf(mon, "%s: '%s'\n",
352             MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
353             params->tls_creds);
354         assert(params->has_tls_hostname);
355         monitor_printf(mon, "%s: '%s'\n",
356             MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
357             params->tls_hostname);
358         assert(params->has_max_bandwidth);
359         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
360             MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
361             params->max_bandwidth);
362         assert(params->has_downtime_limit);
363         monitor_printf(mon, "%s: %" PRIu64 " milliseconds\n",
364             MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
365             params->downtime_limit);
366         assert(params->has_x_checkpoint_delay);
367         monitor_printf(mon, "%s: %u\n",
368             MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
369             params->x_checkpoint_delay);
370         assert(params->has_block_incremental);
371         monitor_printf(mon, "%s: %s\n",
372             MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
373             params->block_incremental ? "on" : "off");
374         monitor_printf(mon, "%s: %u\n",
375             MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_CHANNELS),
376             params->x_multifd_channels);
377         monitor_printf(mon, "%s: %u\n",
378             MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT),
379             params->x_multifd_page_count);
380         monitor_printf(mon, "%s: %" PRIu64 "\n",
381             MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
382             params->xbzrle_cache_size);
383         monitor_printf(mon, "%s: %" PRIu64 "\n",
384             MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
385             params->max_postcopy_bandwidth);
386     }
387
388     qapi_free_MigrationParameters(params);
389 }
390
391 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
392 {
393     monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
394                    qmp_query_migrate_cache_size(NULL) >> 10);
395 }
396
397 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
398 {
399     CpuInfoFastList *cpu_list, *cpu;
400
401     cpu_list = qmp_query_cpus_fast(NULL);
402
403     for (cpu = cpu_list; cpu; cpu = cpu->next) {
404         int active = ' ';
405
406         if (cpu->value->cpu_index == monitor_get_cpu_index()) {
407             active = '*';
408         }
409
410         monitor_printf(mon, "%c CPU #%" PRId64 ":", active,
411                        cpu->value->cpu_index);
412         monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
413     }
414
415     qapi_free_CpuInfoFastList(cpu_list);
416 }
417
418 static void print_block_info(Monitor *mon, BlockInfo *info,
419                              BlockDeviceInfo *inserted, bool verbose)
420 {
421     ImageInfo *image_info;
422
423     assert(!info || !info->has_inserted || info->inserted == inserted);
424
425     if (info && *info->device) {
426         monitor_printf(mon, "%s", info->device);
427         if (inserted && inserted->has_node_name) {
428             monitor_printf(mon, " (%s)", inserted->node_name);
429         }
430     } else {
431         assert(info || inserted);
432         monitor_printf(mon, "%s",
433                        inserted && inserted->has_node_name ? inserted->node_name
434                        : info && info->has_qdev ? info->qdev
435                        : "<anonymous>");
436     }
437
438     if (inserted) {
439         monitor_printf(mon, ": %s (%s%s%s)\n",
440                        inserted->file,
441                        inserted->drv,
442                        inserted->ro ? ", read-only" : "",
443                        inserted->encrypted ? ", encrypted" : "");
444     } else {
445         monitor_printf(mon, ": [not inserted]\n");
446     }
447
448     if (info) {
449         if (info->has_qdev) {
450             monitor_printf(mon, "    Attached to:      %s\n", info->qdev);
451         }
452         if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
453             monitor_printf(mon, "    I/O status:       %s\n",
454                            BlockDeviceIoStatus_str(info->io_status));
455         }
456
457         if (info->removable) {
458             monitor_printf(mon, "    Removable device: %slocked, tray %s\n",
459                            info->locked ? "" : "not ",
460                            info->tray_open ? "open" : "closed");
461         }
462     }
463
464
465     if (!inserted) {
466         return;
467     }
468
469     monitor_printf(mon, "    Cache mode:       %s%s%s\n",
470                    inserted->cache->writeback ? "writeback" : "writethrough",
471                    inserted->cache->direct ? ", direct" : "",
472                    inserted->cache->no_flush ? ", ignore flushes" : "");
473
474     if (inserted->has_backing_file) {
475         monitor_printf(mon,
476                        "    Backing file:     %s "
477                        "(chain depth: %" PRId64 ")\n",
478                        inserted->backing_file,
479                        inserted->backing_file_depth);
480     }
481
482     if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
483         monitor_printf(mon, "    Detect zeroes:    %s\n",
484                 BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
485     }
486
487     if (inserted->bps  || inserted->bps_rd  || inserted->bps_wr  ||
488         inserted->iops || inserted->iops_rd || inserted->iops_wr)
489     {
490         monitor_printf(mon, "    I/O throttling:   bps=%" PRId64
491                         " bps_rd=%" PRId64  " bps_wr=%" PRId64
492                         " bps_max=%" PRId64
493                         " bps_rd_max=%" PRId64
494                         " bps_wr_max=%" PRId64
495                         " iops=%" PRId64 " iops_rd=%" PRId64
496                         " iops_wr=%" PRId64
497                         " iops_max=%" PRId64
498                         " iops_rd_max=%" PRId64
499                         " iops_wr_max=%" PRId64
500                         " iops_size=%" PRId64
501                         " group=%s\n",
502                         inserted->bps,
503                         inserted->bps_rd,
504                         inserted->bps_wr,
505                         inserted->bps_max,
506                         inserted->bps_rd_max,
507                         inserted->bps_wr_max,
508                         inserted->iops,
509                         inserted->iops_rd,
510                         inserted->iops_wr,
511                         inserted->iops_max,
512                         inserted->iops_rd_max,
513                         inserted->iops_wr_max,
514                         inserted->iops_size,
515                         inserted->group);
516     }
517
518     if (verbose) {
519         monitor_printf(mon, "\nImages:\n");
520         image_info = inserted->image;
521         while (1) {
522                 bdrv_image_info_dump((fprintf_function)monitor_printf,
523                                      mon, image_info);
524             if (image_info->has_backing_image) {
525                 image_info = image_info->backing_image;
526             } else {
527                 break;
528             }
529         }
530     }
531 }
532
533 void hmp_info_block(Monitor *mon, const QDict *qdict)
534 {
535     BlockInfoList *block_list, *info;
536     BlockDeviceInfoList *blockdev_list, *blockdev;
537     const char *device = qdict_get_try_str(qdict, "device");
538     bool verbose = qdict_get_try_bool(qdict, "verbose", false);
539     bool nodes = qdict_get_try_bool(qdict, "nodes", false);
540     bool printed = false;
541
542     /* Print BlockBackend information */
543     if (!nodes) {
544         block_list = qmp_query_block(NULL);
545     } else {
546         block_list = NULL;
547     }
548
549     for (info = block_list; info; info = info->next) {
550         if (device && strcmp(device, info->value->device)) {
551             continue;
552         }
553
554         if (info != block_list) {
555             monitor_printf(mon, "\n");
556         }
557
558         print_block_info(mon, info->value, info->value->has_inserted
559                                            ? info->value->inserted : NULL,
560                          verbose);
561         printed = true;
562     }
563
564     qapi_free_BlockInfoList(block_list);
565
566     if ((!device && !nodes) || printed) {
567         return;
568     }
569
570     /* Print node information */
571     blockdev_list = qmp_query_named_block_nodes(NULL);
572     for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
573         assert(blockdev->value->has_node_name);
574         if (device && strcmp(device, blockdev->value->node_name)) {
575             continue;
576         }
577
578         if (blockdev != blockdev_list) {
579             monitor_printf(mon, "\n");
580         }
581
582         print_block_info(mon, NULL, blockdev->value, verbose);
583     }
584     qapi_free_BlockDeviceInfoList(blockdev_list);
585 }
586
587 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
588 {
589     BlockStatsList *stats_list, *stats;
590
591     stats_list = qmp_query_blockstats(false, false, NULL);
592
593     for (stats = stats_list; stats; stats = stats->next) {
594         if (!stats->value->has_device) {
595             continue;
596         }
597
598         monitor_printf(mon, "%s:", stats->value->device);
599         monitor_printf(mon, " rd_bytes=%" PRId64
600                        " wr_bytes=%" PRId64
601                        " rd_operations=%" PRId64
602                        " wr_operations=%" PRId64
603                        " flush_operations=%" PRId64
604                        " wr_total_time_ns=%" PRId64
605                        " rd_total_time_ns=%" PRId64
606                        " flush_total_time_ns=%" PRId64
607                        " rd_merged=%" PRId64
608                        " wr_merged=%" PRId64
609                        " idle_time_ns=%" PRId64
610                        "\n",
611                        stats->value->stats->rd_bytes,
612                        stats->value->stats->wr_bytes,
613                        stats->value->stats->rd_operations,
614                        stats->value->stats->wr_operations,
615                        stats->value->stats->flush_operations,
616                        stats->value->stats->wr_total_time_ns,
617                        stats->value->stats->rd_total_time_ns,
618                        stats->value->stats->flush_total_time_ns,
619                        stats->value->stats->rd_merged,
620                        stats->value->stats->wr_merged,
621                        stats->value->stats->idle_time_ns);
622     }
623
624     qapi_free_BlockStatsList(stats_list);
625 }
626
627 #ifdef CONFIG_VNC
628 /* Helper for hmp_info_vnc_clients, _servers */
629 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
630                                   const char *name)
631 {
632     monitor_printf(mon, "  %s: %s:%s (%s%s)\n",
633                    name,
634                    info->host,
635                    info->service,
636                    NetworkAddressFamily_str(info->family),
637                    info->websocket ? " (Websocket)" : "");
638 }
639
640 /* Helper displaying and auth and crypt info */
641 static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
642                                    VncPrimaryAuth auth,
643                                    VncVencryptSubAuth *vencrypt)
644 {
645     monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
646                    VncPrimaryAuth_str(auth),
647                    vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
648 }
649
650 static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
651 {
652     while (client) {
653         VncClientInfo *cinfo = client->value;
654
655         hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
656         monitor_printf(mon, "    x509_dname: %s\n",
657                        cinfo->has_x509_dname ?
658                        cinfo->x509_dname : "none");
659         monitor_printf(mon, "    sasl_username: %s\n",
660                        cinfo->has_sasl_username ?
661                        cinfo->sasl_username : "none");
662
663         client = client->next;
664     }
665 }
666
667 static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
668 {
669     while (server) {
670         VncServerInfo2 *sinfo = server->value;
671         hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
672         hmp_info_vnc_authcrypt(mon, "    ", sinfo->auth,
673                                sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
674         server = server->next;
675     }
676 }
677
678 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
679 {
680     VncInfo2List *info2l;
681     Error *err = NULL;
682
683     info2l = qmp_query_vnc_servers(&err);
684     if (err) {
685         hmp_handle_error(mon, &err);
686         return;
687     }
688     if (!info2l) {
689         monitor_printf(mon, "None\n");
690         return;
691     }
692
693     while (info2l) {
694         VncInfo2 *info = info2l->value;
695         monitor_printf(mon, "%s:\n", info->id);
696         hmp_info_vnc_servers(mon, info->server);
697         hmp_info_vnc_clients(mon, info->clients);
698         if (!info->server) {
699             /* The server entry displays its auth, we only
700              * need to display in the case of 'reverse' connections
701              * where there's no server.
702              */
703             hmp_info_vnc_authcrypt(mon, "  ", info->auth,
704                                info->has_vencrypt ? &info->vencrypt : NULL);
705         }
706         if (info->has_display) {
707             monitor_printf(mon, "  Display: %s\n", info->display);
708         }
709         info2l = info2l->next;
710     }
711
712     qapi_free_VncInfo2List(info2l);
713
714 }
715 #endif
716
717 #ifdef CONFIG_SPICE
718 void hmp_info_spice(Monitor *mon, const QDict *qdict)
719 {
720     SpiceChannelList *chan;
721     SpiceInfo *info;
722     const char *channel_name;
723     const char * const channel_names[] = {
724         [SPICE_CHANNEL_MAIN] = "main",
725         [SPICE_CHANNEL_DISPLAY] = "display",
726         [SPICE_CHANNEL_INPUTS] = "inputs",
727         [SPICE_CHANNEL_CURSOR] = "cursor",
728         [SPICE_CHANNEL_PLAYBACK] = "playback",
729         [SPICE_CHANNEL_RECORD] = "record",
730         [SPICE_CHANNEL_TUNNEL] = "tunnel",
731         [SPICE_CHANNEL_SMARTCARD] = "smartcard",
732         [SPICE_CHANNEL_USBREDIR] = "usbredir",
733         [SPICE_CHANNEL_PORT] = "port",
734 #if 0
735         /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
736          * no easy way to #ifdef (SPICE_CHANNEL_* is a enum).  Disable
737          * as quick fix for build failures with older versions. */
738         [SPICE_CHANNEL_WEBDAV] = "webdav",
739 #endif
740     };
741
742     info = qmp_query_spice(NULL);
743
744     if (!info->enabled) {
745         monitor_printf(mon, "Server: disabled\n");
746         goto out;
747     }
748
749     monitor_printf(mon, "Server:\n");
750     if (info->has_port) {
751         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
752                        info->host, info->port);
753     }
754     if (info->has_tls_port) {
755         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
756                        info->host, info->tls_port);
757     }
758     monitor_printf(mon, "    migrated: %s\n",
759                    info->migrated ? "true" : "false");
760     monitor_printf(mon, "        auth: %s\n", info->auth);
761     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
762     monitor_printf(mon, "  mouse-mode: %s\n",
763                    SpiceQueryMouseMode_str(info->mouse_mode));
764
765     if (!info->has_channels || info->channels == NULL) {
766         monitor_printf(mon, "Channels: none\n");
767     } else {
768         for (chan = info->channels; chan; chan = chan->next) {
769             monitor_printf(mon, "Channel:\n");
770             monitor_printf(mon, "     address: %s:%s%s\n",
771                            chan->value->host, chan->value->port,
772                            chan->value->tls ? " [tls]" : "");
773             monitor_printf(mon, "     session: %" PRId64 "\n",
774                            chan->value->connection_id);
775             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
776                            chan->value->channel_type, chan->value->channel_id);
777
778             channel_name = "unknown";
779             if (chan->value->channel_type > 0 &&
780                 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
781                 channel_names[chan->value->channel_type]) {
782                 channel_name = channel_names[chan->value->channel_type];
783             }
784
785             monitor_printf(mon, "     channel name: %s\n", channel_name);
786         }
787     }
788
789 out:
790     qapi_free_SpiceInfo(info);
791 }
792 #endif
793
794 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
795 {
796     BalloonInfo *info;
797     Error *err = NULL;
798
799     info = qmp_query_balloon(&err);
800     if (err) {
801         hmp_handle_error(mon, &err);
802         return;
803     }
804
805     monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
806
807     qapi_free_BalloonInfo(info);
808 }
809
810 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
811 {
812     PciMemoryRegionList *region;
813
814     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
815     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
816                    dev->slot, dev->function);
817     monitor_printf(mon, "    ");
818
819     if (dev->class_info->has_desc) {
820         monitor_printf(mon, "%s", dev->class_info->desc);
821     } else {
822         monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
823     }
824
825     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
826                    dev->id->vendor, dev->id->device);
827
828     if (dev->has_irq) {
829         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
830     }
831
832     if (dev->has_pci_bridge) {
833         monitor_printf(mon, "      BUS %" PRId64 ".\n",
834                        dev->pci_bridge->bus->number);
835         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
836                        dev->pci_bridge->bus->secondary);
837         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
838                        dev->pci_bridge->bus->subordinate);
839
840         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
841                        dev->pci_bridge->bus->io_range->base,
842                        dev->pci_bridge->bus->io_range->limit);
843
844         monitor_printf(mon,
845                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
846                        dev->pci_bridge->bus->memory_range->base,
847                        dev->pci_bridge->bus->memory_range->limit);
848
849         monitor_printf(mon, "      prefetchable memory range "
850                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
851                        dev->pci_bridge->bus->prefetchable_range->base,
852                        dev->pci_bridge->bus->prefetchable_range->limit);
853     }
854
855     for (region = dev->regions; region; region = region->next) {
856         uint64_t addr, size;
857
858         addr = region->value->address;
859         size = region->value->size;
860
861         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
862
863         if (!strcmp(region->value->type, "io")) {
864             monitor_printf(mon, "I/O at 0x%04" PRIx64
865                                 " [0x%04" PRIx64 "].\n",
866                            addr, addr + size - 1);
867         } else {
868             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
869                                " [0x%08" PRIx64 "].\n",
870                            region->value->mem_type_64 ? 64 : 32,
871                            region->value->prefetch ? " prefetchable" : "",
872                            addr, addr + size - 1);
873         }
874     }
875
876     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
877
878     if (dev->has_pci_bridge) {
879         if (dev->pci_bridge->has_devices) {
880             PciDeviceInfoList *cdev;
881             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
882                 hmp_info_pci_device(mon, cdev->value);
883             }
884         }
885     }
886 }
887
888 static int hmp_info_irq_foreach(Object *obj, void *opaque)
889 {
890     InterruptStatsProvider *intc;
891     InterruptStatsProviderClass *k;
892     Monitor *mon = opaque;
893
894     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
895         intc = INTERRUPT_STATS_PROVIDER(obj);
896         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
897         uint64_t *irq_counts;
898         unsigned int nb_irqs, i;
899         if (k->get_statistics &&
900             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
901             if (nb_irqs > 0) {
902                 monitor_printf(mon, "IRQ statistics for %s:\n",
903                                object_get_typename(obj));
904                 for (i = 0; i < nb_irqs; i++) {
905                     if (irq_counts[i] > 0) {
906                         monitor_printf(mon, "%2d: %" PRId64 "\n", i,
907                                        irq_counts[i]);
908                     }
909                 }
910             }
911         } else {
912             monitor_printf(mon, "IRQ statistics not available for %s.\n",
913                            object_get_typename(obj));
914         }
915     }
916
917     return 0;
918 }
919
920 void hmp_info_irq(Monitor *mon, const QDict *qdict)
921 {
922     object_child_foreach_recursive(object_get_root(),
923                                    hmp_info_irq_foreach, mon);
924 }
925
926 static int hmp_info_pic_foreach(Object *obj, void *opaque)
927 {
928     InterruptStatsProvider *intc;
929     InterruptStatsProviderClass *k;
930     Monitor *mon = opaque;
931
932     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
933         intc = INTERRUPT_STATS_PROVIDER(obj);
934         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
935         if (k->print_info) {
936             k->print_info(intc, mon);
937         } else {
938             monitor_printf(mon, "Interrupt controller information not available for %s.\n",
939                            object_get_typename(obj));
940         }
941     }
942
943     return 0;
944 }
945
946 void hmp_info_pic(Monitor *mon, const QDict *qdict)
947 {
948     object_child_foreach_recursive(object_get_root(),
949                                    hmp_info_pic_foreach, mon);
950 }
951
952 void hmp_info_pci(Monitor *mon, const QDict *qdict)
953 {
954     PciInfoList *info_list, *info;
955     Error *err = NULL;
956
957     info_list = qmp_query_pci(&err);
958     if (err) {
959         monitor_printf(mon, "PCI devices not supported\n");
960         error_free(err);
961         return;
962     }
963
964     for (info = info_list; info; info = info->next) {
965         PciDeviceInfoList *dev;
966
967         for (dev = info->value->devices; dev; dev = dev->next) {
968             hmp_info_pci_device(mon, dev->value);
969         }
970     }
971
972     qapi_free_PciInfoList(info_list);
973 }
974
975 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
976 {
977     BlockJobInfoList *list;
978     Error *err = NULL;
979
980     list = qmp_query_block_jobs(&err);
981     assert(!err);
982
983     if (!list) {
984         monitor_printf(mon, "No active jobs\n");
985         return;
986     }
987
988     while (list) {
989         if (strcmp(list->value->type, "stream") == 0) {
990             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
991                            " of %" PRId64 " bytes, speed limit %" PRId64
992                            " bytes/s\n",
993                            list->value->device,
994                            list->value->offset,
995                            list->value->len,
996                            list->value->speed);
997         } else {
998             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
999                            " of %" PRId64 " bytes, speed limit %" PRId64
1000                            " bytes/s\n",
1001                            list->value->type,
1002                            list->value->device,
1003                            list->value->offset,
1004                            list->value->len,
1005                            list->value->speed);
1006         }
1007         list = list->next;
1008     }
1009
1010     qapi_free_BlockJobInfoList(list);
1011 }
1012
1013 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
1014 {
1015     TPMInfoList *info_list, *info;
1016     Error *err = NULL;
1017     unsigned int c = 0;
1018     TPMPassthroughOptions *tpo;
1019     TPMEmulatorOptions *teo;
1020
1021     info_list = qmp_query_tpm(&err);
1022     if (err) {
1023         monitor_printf(mon, "TPM device not supported\n");
1024         error_free(err);
1025         return;
1026     }
1027
1028     if (info_list) {
1029         monitor_printf(mon, "TPM device:\n");
1030     }
1031
1032     for (info = info_list; info; info = info->next) {
1033         TPMInfo *ti = info->value;
1034         monitor_printf(mon, " tpm%d: model=%s\n",
1035                        c, TpmModel_str(ti->model));
1036
1037         monitor_printf(mon, "  \\ %s: type=%s",
1038                        ti->id, TpmTypeOptionsKind_str(ti->options->type));
1039
1040         switch (ti->options->type) {
1041         case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
1042             tpo = ti->options->u.passthrough.data;
1043             monitor_printf(mon, "%s%s%s%s",
1044                            tpo->has_path ? ",path=" : "",
1045                            tpo->has_path ? tpo->path : "",
1046                            tpo->has_cancel_path ? ",cancel-path=" : "",
1047                            tpo->has_cancel_path ? tpo->cancel_path : "");
1048             break;
1049         case TPM_TYPE_OPTIONS_KIND_EMULATOR:
1050             teo = ti->options->u.emulator.data;
1051             monitor_printf(mon, ",chardev=%s", teo->chardev);
1052             break;
1053         case TPM_TYPE_OPTIONS_KIND__MAX:
1054             break;
1055         }
1056         monitor_printf(mon, "\n");
1057         c++;
1058     }
1059     qapi_free_TPMInfoList(info_list);
1060 }
1061
1062 void hmp_quit(Monitor *mon, const QDict *qdict)
1063 {
1064     monitor_suspend(mon);
1065     qmp_quit(NULL);
1066 }
1067
1068 void hmp_stop(Monitor *mon, const QDict *qdict)
1069 {
1070     qmp_stop(NULL);
1071 }
1072
1073 void hmp_sync_profile(Monitor *mon, const QDict *qdict)
1074 {
1075     const char *op = qdict_get_try_str(qdict, "op");
1076
1077     if (op == NULL) {
1078         bool on = qsp_is_enabled();
1079
1080         monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
1081         return;
1082     }
1083     if (!strcmp(op, "on")) {
1084         qsp_enable();
1085     } else if (!strcmp(op, "off")) {
1086         qsp_disable();
1087     } else if (!strcmp(op, "reset")) {
1088         qsp_reset();
1089     } else {
1090         Error *err = NULL;
1091
1092         error_setg(&err, QERR_INVALID_PARAMETER, op);
1093         hmp_handle_error(mon, &err);
1094     }
1095 }
1096
1097 void hmp_system_reset(Monitor *mon, const QDict *qdict)
1098 {
1099     qmp_system_reset(NULL);
1100 }
1101
1102 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1103 {
1104     qmp_system_powerdown(NULL);
1105 }
1106
1107 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
1108 {
1109     Error *err = NULL;
1110
1111     qmp_x_exit_preconfig(&err);
1112     hmp_handle_error(mon, &err);
1113 }
1114
1115 void hmp_cpu(Monitor *mon, const QDict *qdict)
1116 {
1117     int64_t cpu_index;
1118
1119     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1120             use it are converted to the QAPI */
1121     cpu_index = qdict_get_int(qdict, "index");
1122     if (monitor_set_cpu(cpu_index) < 0) {
1123         monitor_printf(mon, "invalid CPU index\n");
1124     }
1125 }
1126
1127 void hmp_memsave(Monitor *mon, const QDict *qdict)
1128 {
1129     uint32_t size = qdict_get_int(qdict, "size");
1130     const char *filename = qdict_get_str(qdict, "filename");
1131     uint64_t addr = qdict_get_int(qdict, "val");
1132     Error *err = NULL;
1133     int cpu_index = monitor_get_cpu_index();
1134
1135     if (cpu_index < 0) {
1136         monitor_printf(mon, "No CPU available\n");
1137         return;
1138     }
1139
1140     qmp_memsave(addr, size, filename, true, cpu_index, &err);
1141     hmp_handle_error(mon, &err);
1142 }
1143
1144 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1145 {
1146     uint32_t size = qdict_get_int(qdict, "size");
1147     const char *filename = qdict_get_str(qdict, "filename");
1148     uint64_t addr = qdict_get_int(qdict, "val");
1149     Error *err = NULL;
1150
1151     qmp_pmemsave(addr, size, filename, &err);
1152     hmp_handle_error(mon, &err);
1153 }
1154
1155 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1156 {
1157     const char *chardev = qdict_get_str(qdict, "device");
1158     const char *data = qdict_get_str(qdict, "data");
1159     Error *err = NULL;
1160
1161     qmp_ringbuf_write(chardev, data, false, 0, &err);
1162
1163     hmp_handle_error(mon, &err);
1164 }
1165
1166 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1167 {
1168     uint32_t size = qdict_get_int(qdict, "size");
1169     const char *chardev = qdict_get_str(qdict, "device");
1170     char *data;
1171     Error *err = NULL;
1172     int i;
1173
1174     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1175     if (err) {
1176         hmp_handle_error(mon, &err);
1177         return;
1178     }
1179
1180     for (i = 0; data[i]; i++) {
1181         unsigned char ch = data[i];
1182
1183         if (ch == '\\') {
1184             monitor_printf(mon, "\\\\");
1185         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1186             monitor_printf(mon, "\\u%04X", ch);
1187         } else {
1188             monitor_printf(mon, "%c", ch);
1189         }
1190
1191     }
1192     monitor_printf(mon, "\n");
1193     g_free(data);
1194 }
1195
1196 void hmp_cont(Monitor *mon, const QDict *qdict)
1197 {
1198     Error *err = NULL;
1199
1200     qmp_cont(&err);
1201     hmp_handle_error(mon, &err);
1202 }
1203
1204 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1205 {
1206     qmp_system_wakeup(NULL);
1207 }
1208
1209 void hmp_nmi(Monitor *mon, const QDict *qdict)
1210 {
1211     Error *err = NULL;
1212
1213     qmp_inject_nmi(&err);
1214     hmp_handle_error(mon, &err);
1215 }
1216
1217 void hmp_set_link(Monitor *mon, const QDict *qdict)
1218 {
1219     const char *name = qdict_get_str(qdict, "name");
1220     bool up = qdict_get_bool(qdict, "up");
1221     Error *err = NULL;
1222
1223     qmp_set_link(name, up, &err);
1224     hmp_handle_error(mon, &err);
1225 }
1226
1227 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1228 {
1229     const char *device = qdict_get_str(qdict, "device");
1230     const char *password = qdict_get_str(qdict, "password");
1231     Error *err = NULL;
1232
1233     qmp_block_passwd(true, device, false, NULL, password, &err);
1234     hmp_handle_error(mon, &err);
1235 }
1236
1237 void hmp_balloon(Monitor *mon, const QDict *qdict)
1238 {
1239     int64_t value = qdict_get_int(qdict, "value");
1240     Error *err = NULL;
1241
1242     qmp_balloon(value, &err);
1243     hmp_handle_error(mon, &err);
1244 }
1245
1246 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1247 {
1248     const char *device = qdict_get_str(qdict, "device");
1249     int64_t size = qdict_get_int(qdict, "size");
1250     Error *err = NULL;
1251
1252     qmp_block_resize(true, device, false, NULL, size, &err);
1253     hmp_handle_error(mon, &err);
1254 }
1255
1256 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1257 {
1258     const char *filename = qdict_get_str(qdict, "target");
1259     const char *format = qdict_get_try_str(qdict, "format");
1260     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1261     bool full = qdict_get_try_bool(qdict, "full", false);
1262     Error *err = NULL;
1263     DriveMirror mirror = {
1264         .device = (char *)qdict_get_str(qdict, "device"),
1265         .target = (char *)filename,
1266         .has_format = !!format,
1267         .format = (char *)format,
1268         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1269         .has_mode = true,
1270         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1271         .unmap = true,
1272     };
1273
1274     if (!filename) {
1275         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1276         hmp_handle_error(mon, &err);
1277         return;
1278     }
1279     qmp_drive_mirror(&mirror, &err);
1280     hmp_handle_error(mon, &err);
1281 }
1282
1283 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1284 {
1285     const char *device = qdict_get_str(qdict, "device");
1286     const char *filename = qdict_get_str(qdict, "target");
1287     const char *format = qdict_get_try_str(qdict, "format");
1288     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1289     bool full = qdict_get_try_bool(qdict, "full", false);
1290     bool compress = qdict_get_try_bool(qdict, "compress", false);
1291     Error *err = NULL;
1292     DriveBackup backup = {
1293         .device = (char *)device,
1294         .target = (char *)filename,
1295         .has_format = !!format,
1296         .format = (char *)format,
1297         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1298         .has_mode = true,
1299         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1300         .has_compress = !!compress,
1301         .compress = compress,
1302     };
1303
1304     if (!filename) {
1305         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1306         hmp_handle_error(mon, &err);
1307         return;
1308     }
1309
1310     qmp_drive_backup(&backup, &err);
1311     hmp_handle_error(mon, &err);
1312 }
1313
1314 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1315 {
1316     const char *device = qdict_get_str(qdict, "device");
1317     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1318     const char *format = qdict_get_try_str(qdict, "format");
1319     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1320     enum NewImageMode mode;
1321     Error *err = NULL;
1322
1323     if (!filename) {
1324         /* In the future, if 'snapshot-file' is not specified, the snapshot
1325            will be taken internally. Today it's actually required. */
1326         error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1327         hmp_handle_error(mon, &err);
1328         return;
1329     }
1330
1331     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1332     qmp_blockdev_snapshot_sync(true, device, false, NULL,
1333                                filename, false, NULL,
1334                                !!format, format,
1335                                true, mode, &err);
1336     hmp_handle_error(mon, &err);
1337 }
1338
1339 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1340 {
1341     const char *device = qdict_get_str(qdict, "device");
1342     const char *name = qdict_get_str(qdict, "name");
1343     Error *err = NULL;
1344
1345     qmp_blockdev_snapshot_internal_sync(device, name, &err);
1346     hmp_handle_error(mon, &err);
1347 }
1348
1349 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1350 {
1351     const char *device = qdict_get_str(qdict, "device");
1352     const char *name = qdict_get_str(qdict, "name");
1353     const char *id = qdict_get_try_str(qdict, "id");
1354     Error *err = NULL;
1355
1356     qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1357                                                true, name, &err);
1358     hmp_handle_error(mon, &err);
1359 }
1360
1361 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1362 {
1363     int saved_vm_running  = runstate_is_running();
1364     const char *name = qdict_get_str(qdict, "name");
1365     Error *err = NULL;
1366
1367     vm_stop(RUN_STATE_RESTORE_VM);
1368
1369     if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1370         vm_start();
1371     }
1372     hmp_handle_error(mon, &err);
1373 }
1374
1375 void hmp_savevm(Monitor *mon, const QDict *qdict)
1376 {
1377     Error *err = NULL;
1378
1379     save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1380     hmp_handle_error(mon, &err);
1381 }
1382
1383 void hmp_delvm(Monitor *mon, const QDict *qdict)
1384 {
1385     BlockDriverState *bs;
1386     Error *err = NULL;
1387     const char *name = qdict_get_str(qdict, "name");
1388
1389     if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1390         error_reportf_err(err,
1391                           "Error while deleting snapshot on device '%s': ",
1392                           bdrv_get_device_name(bs));
1393     }
1394 }
1395
1396 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1397 {
1398     BlockDriverState *bs, *bs1;
1399     BdrvNextIterator it1;
1400     QEMUSnapshotInfo *sn_tab, *sn;
1401     bool no_snapshot = true;
1402     int nb_sns, i;
1403     int total;
1404     int *global_snapshots;
1405     AioContext *aio_context;
1406
1407     typedef struct SnapshotEntry {
1408         QEMUSnapshotInfo sn;
1409         QTAILQ_ENTRY(SnapshotEntry) next;
1410     } SnapshotEntry;
1411
1412     typedef struct ImageEntry {
1413         const char *imagename;
1414         QTAILQ_ENTRY(ImageEntry) next;
1415         QTAILQ_HEAD(, SnapshotEntry) snapshots;
1416     } ImageEntry;
1417
1418     QTAILQ_HEAD(, ImageEntry) image_list =
1419         QTAILQ_HEAD_INITIALIZER(image_list);
1420
1421     ImageEntry *image_entry, *next_ie;
1422     SnapshotEntry *snapshot_entry;
1423
1424     bs = bdrv_all_find_vmstate_bs();
1425     if (!bs) {
1426         monitor_printf(mon, "No available block device supports snapshots\n");
1427         return;
1428     }
1429     aio_context = bdrv_get_aio_context(bs);
1430
1431     aio_context_acquire(aio_context);
1432     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1433     aio_context_release(aio_context);
1434
1435     if (nb_sns < 0) {
1436         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1437         return;
1438     }
1439
1440     for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1441         int bs1_nb_sns = 0;
1442         ImageEntry *ie;
1443         SnapshotEntry *se;
1444         AioContext *ctx = bdrv_get_aio_context(bs1);
1445
1446         aio_context_acquire(ctx);
1447         if (bdrv_can_snapshot(bs1)) {
1448             sn = NULL;
1449             bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1450             if (bs1_nb_sns > 0) {
1451                 no_snapshot = false;
1452                 ie = g_new0(ImageEntry, 1);
1453                 ie->imagename = bdrv_get_device_name(bs1);
1454                 QTAILQ_INIT(&ie->snapshots);
1455                 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1456                 for (i = 0; i < bs1_nb_sns; i++) {
1457                     se = g_new0(SnapshotEntry, 1);
1458                     se->sn = sn[i];
1459                     QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1460                 }
1461             }
1462             g_free(sn);
1463         }
1464         aio_context_release(ctx);
1465     }
1466
1467     if (no_snapshot) {
1468         monitor_printf(mon, "There is no snapshot available.\n");
1469         return;
1470     }
1471
1472     global_snapshots = g_new0(int, nb_sns);
1473     total = 0;
1474     for (i = 0; i < nb_sns; i++) {
1475         SnapshotEntry *next_sn;
1476         if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1477             global_snapshots[total] = i;
1478             total++;
1479             QTAILQ_FOREACH(image_entry, &image_list, next) {
1480                 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1481                                     next, next_sn) {
1482                     if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1483                         QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1484                                       next);
1485                         g_free(snapshot_entry);
1486                     }
1487                 }
1488             }
1489         }
1490     }
1491
1492     monitor_printf(mon, "List of snapshots present on all disks:\n");
1493
1494     if (total > 0) {
1495         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1496         monitor_printf(mon, "\n");
1497         for (i = 0; i < total; i++) {
1498             sn = &sn_tab[global_snapshots[i]];
1499             /* The ID is not guaranteed to be the same on all images, so
1500              * overwrite it.
1501              */
1502             pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1503             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1504             monitor_printf(mon, "\n");
1505         }
1506     } else {
1507         monitor_printf(mon, "None\n");
1508     }
1509
1510     QTAILQ_FOREACH(image_entry, &image_list, next) {
1511         if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1512             continue;
1513         }
1514         monitor_printf(mon,
1515                        "\nList of partial (non-loadable) snapshots on '%s':\n",
1516                        image_entry->imagename);
1517         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1518         monitor_printf(mon, "\n");
1519         QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1520             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1521                                &snapshot_entry->sn);
1522             monitor_printf(mon, "\n");
1523         }
1524     }
1525
1526     QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1527         SnapshotEntry *next_sn;
1528         QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1529                             next_sn) {
1530             g_free(snapshot_entry);
1531         }
1532         g_free(image_entry);
1533     }
1534     g_free(sn_tab);
1535     g_free(global_snapshots);
1536
1537 }
1538
1539 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1540 {
1541     qmp_migrate_cancel(NULL);
1542 }
1543
1544 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
1545 {
1546     Error *err = NULL;
1547     const char *state = qdict_get_str(qdict, "state");
1548     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
1549
1550     if (val >= 0) {
1551         qmp_migrate_continue(val, &err);
1552     }
1553
1554     hmp_handle_error(mon, &err);
1555 }
1556
1557 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1558 {
1559     Error *err = NULL;
1560     const char *uri = qdict_get_str(qdict, "uri");
1561
1562     qmp_migrate_incoming(uri, &err);
1563
1564     hmp_handle_error(mon, &err);
1565 }
1566
1567 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
1568 {
1569     Error *err = NULL;
1570     const char *uri = qdict_get_str(qdict, "uri");
1571
1572     qmp_migrate_recover(uri, &err);
1573
1574     hmp_handle_error(mon, &err);
1575 }
1576
1577 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
1578 {
1579     Error *err = NULL;
1580
1581     qmp_migrate_pause(&err);
1582
1583     hmp_handle_error(mon, &err);
1584 }
1585
1586 /* Kept for backwards compatibility */
1587 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1588 {
1589     double value = qdict_get_double(qdict, "value");
1590     qmp_migrate_set_downtime(value, NULL);
1591 }
1592
1593 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1594 {
1595     int64_t value = qdict_get_int(qdict, "value");
1596     Error *err = NULL;
1597
1598     qmp_migrate_set_cache_size(value, &err);
1599     hmp_handle_error(mon, &err);
1600 }
1601
1602 /* Kept for backwards compatibility */
1603 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1604 {
1605     int64_t value = qdict_get_int(qdict, "value");
1606     qmp_migrate_set_speed(value, NULL);
1607 }
1608
1609 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1610 {
1611     const char *cap = qdict_get_str(qdict, "capability");
1612     bool state = qdict_get_bool(qdict, "state");
1613     Error *err = NULL;
1614     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1615     int val;
1616
1617     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
1618     if (val < 0) {
1619         goto end;
1620     }
1621
1622     caps->value = g_malloc0(sizeof(*caps->value));
1623     caps->value->capability = val;
1624     caps->value->state = state;
1625     caps->next = NULL;
1626     qmp_migrate_set_capabilities(caps, &err);
1627
1628 end:
1629     qapi_free_MigrationCapabilityStatusList(caps);
1630     hmp_handle_error(mon, &err);
1631 }
1632
1633 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1634 {
1635     const char *param = qdict_get_str(qdict, "parameter");
1636     const char *valuestr = qdict_get_str(qdict, "value");
1637     Visitor *v = string_input_visitor_new(valuestr);
1638     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
1639     uint64_t valuebw = 0;
1640     uint64_t cache_size;
1641     Error *err = NULL;
1642     int val, ret;
1643
1644     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
1645     if (val < 0) {
1646         goto cleanup;
1647     }
1648
1649     switch (val) {
1650     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1651         p->has_compress_level = true;
1652         visit_type_int(v, param, &p->compress_level, &err);
1653         break;
1654     case MIGRATION_PARAMETER_COMPRESS_THREADS:
1655         p->has_compress_threads = true;
1656         visit_type_int(v, param, &p->compress_threads, &err);
1657         break;
1658     case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
1659         p->has_compress_wait_thread = true;
1660         visit_type_bool(v, param, &p->compress_wait_thread, &err);
1661         break;
1662     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1663         p->has_decompress_threads = true;
1664         visit_type_int(v, param, &p->decompress_threads, &err);
1665         break;
1666     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1667         p->has_cpu_throttle_initial = true;
1668         visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1669         break;
1670     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1671         p->has_cpu_throttle_increment = true;
1672         visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1673         break;
1674     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
1675         p->has_max_cpu_throttle = true;
1676         visit_type_int(v, param, &p->max_cpu_throttle, &err);
1677         break;
1678     case MIGRATION_PARAMETER_TLS_CREDS:
1679         p->has_tls_creds = true;
1680         p->tls_creds = g_new0(StrOrNull, 1);
1681         p->tls_creds->type = QTYPE_QSTRING;
1682         visit_type_str(v, param, &p->tls_creds->u.s, &err);
1683         break;
1684     case MIGRATION_PARAMETER_TLS_HOSTNAME:
1685         p->has_tls_hostname = true;
1686         p->tls_hostname = g_new0(StrOrNull, 1);
1687         p->tls_hostname->type = QTYPE_QSTRING;
1688         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
1689         break;
1690     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1691         p->has_max_bandwidth = true;
1692         /*
1693          * Can't use visit_type_size() here, because it
1694          * defaults to Bytes rather than Mebibytes.
1695          */
1696         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1697         if (ret < 0 || valuebw > INT64_MAX
1698             || (size_t)valuebw != valuebw) {
1699             error_setg(&err, "Invalid size %s", valuestr);
1700             break;
1701         }
1702         p->max_bandwidth = valuebw;
1703         break;
1704     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1705         p->has_downtime_limit = true;
1706         visit_type_int(v, param, &p->downtime_limit, &err);
1707         break;
1708     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1709         p->has_x_checkpoint_delay = true;
1710         visit_type_int(v, param, &p->x_checkpoint_delay, &err);
1711         break;
1712     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1713         p->has_block_incremental = true;
1714         visit_type_bool(v, param, &p->block_incremental, &err);
1715         break;
1716     case MIGRATION_PARAMETER_X_MULTIFD_CHANNELS:
1717         p->has_x_multifd_channels = true;
1718         visit_type_int(v, param, &p->x_multifd_channels, &err);
1719         break;
1720     case MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT:
1721         p->has_x_multifd_page_count = true;
1722         visit_type_int(v, param, &p->x_multifd_page_count, &err);
1723         break;
1724     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
1725         p->has_xbzrle_cache_size = true;
1726         visit_type_size(v, param, &cache_size, &err);
1727         if (err || cache_size > INT64_MAX
1728             || (size_t)cache_size != cache_size) {
1729             error_setg(&err, "Invalid size %s", valuestr);
1730             break;
1731         }
1732         p->xbzrle_cache_size = cache_size;
1733         break;
1734     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
1735         p->has_max_postcopy_bandwidth = true;
1736         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
1737         break;
1738     default:
1739         assert(0);
1740     }
1741
1742     if (err) {
1743         goto cleanup;
1744     }
1745
1746     qmp_migrate_set_parameters(p, &err);
1747
1748  cleanup:
1749     qapi_free_MigrateSetParameters(p);
1750     visit_free(v);
1751     hmp_handle_error(mon, &err);
1752 }
1753
1754 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1755 {
1756     Error *err = NULL;
1757     const char *protocol = qdict_get_str(qdict, "protocol");
1758     const char *hostname = qdict_get_str(qdict, "hostname");
1759     bool has_port        = qdict_haskey(qdict, "port");
1760     int port             = qdict_get_try_int(qdict, "port", -1);
1761     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1762     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1763     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1764
1765     qmp_client_migrate_info(protocol, hostname,
1766                             has_port, port, has_tls_port, tls_port,
1767                             !!cert_subject, cert_subject, &err);
1768     hmp_handle_error(mon, &err);
1769 }
1770
1771 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1772 {
1773     Error *err = NULL;
1774     qmp_migrate_start_postcopy(&err);
1775     hmp_handle_error(mon, &err);
1776 }
1777
1778 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1779 {
1780     Error *err = NULL;
1781
1782     qmp_x_colo_lost_heartbeat(&err);
1783     hmp_handle_error(mon, &err);
1784 }
1785
1786 void hmp_set_password(Monitor *mon, const QDict *qdict)
1787 {
1788     const char *protocol  = qdict_get_str(qdict, "protocol");
1789     const char *password  = qdict_get_str(qdict, "password");
1790     const char *connected = qdict_get_try_str(qdict, "connected");
1791     Error *err = NULL;
1792
1793     qmp_set_password(protocol, password, !!connected, connected, &err);
1794     hmp_handle_error(mon, &err);
1795 }
1796
1797 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1798 {
1799     const char *protocol  = qdict_get_str(qdict, "protocol");
1800     const char *whenstr = qdict_get_str(qdict, "time");
1801     Error *err = NULL;
1802
1803     qmp_expire_password(protocol, whenstr, &err);
1804     hmp_handle_error(mon, &err);
1805 }
1806
1807 void hmp_eject(Monitor *mon, const QDict *qdict)
1808 {
1809     bool force = qdict_get_try_bool(qdict, "force", false);
1810     const char *device = qdict_get_str(qdict, "device");
1811     Error *err = NULL;
1812
1813     qmp_eject(true, device, false, NULL, true, force, &err);
1814     hmp_handle_error(mon, &err);
1815 }
1816
1817 #ifdef CONFIG_VNC
1818 static void hmp_change_read_arg(void *opaque, const char *password,
1819                                 void *readline_opaque)
1820 {
1821     qmp_change_vnc_password(password, NULL);
1822     monitor_read_command(opaque, 1);
1823 }
1824 #endif
1825
1826 void hmp_change(Monitor *mon, const QDict *qdict)
1827 {
1828     const char *device = qdict_get_str(qdict, "device");
1829     const char *target = qdict_get_str(qdict, "target");
1830     const char *arg = qdict_get_try_str(qdict, "arg");
1831     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1832     BlockdevChangeReadOnlyMode read_only_mode = 0;
1833     Error *err = NULL;
1834
1835 #ifdef CONFIG_VNC
1836     if (strcmp(device, "vnc") == 0) {
1837         if (read_only) {
1838             monitor_printf(mon,
1839                            "Parameter 'read-only-mode' is invalid for VNC\n");
1840             return;
1841         }
1842         if (strcmp(target, "passwd") == 0 ||
1843             strcmp(target, "password") == 0) {
1844             if (!arg) {
1845                 monitor_read_password(mon, hmp_change_read_arg, NULL);
1846                 return;
1847             }
1848         }
1849         qmp_change("vnc", target, !!arg, arg, &err);
1850     } else
1851 #endif
1852     {
1853         if (read_only) {
1854             read_only_mode =
1855                 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1856                                 read_only,
1857                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1858             if (err) {
1859                 hmp_handle_error(mon, &err);
1860                 return;
1861             }
1862         }
1863
1864         qmp_blockdev_change_medium(true, device, false, NULL, target,
1865                                    !!arg, arg, !!read_only, read_only_mode,
1866                                    &err);
1867     }
1868
1869     hmp_handle_error(mon, &err);
1870 }
1871
1872 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1873 {
1874     Error *err = NULL;
1875     char *device = (char *) qdict_get_str(qdict, "device");
1876     BlockIOThrottle throttle = {
1877         .bps = qdict_get_int(qdict, "bps"),
1878         .bps_rd = qdict_get_int(qdict, "bps_rd"),
1879         .bps_wr = qdict_get_int(qdict, "bps_wr"),
1880         .iops = qdict_get_int(qdict, "iops"),
1881         .iops_rd = qdict_get_int(qdict, "iops_rd"),
1882         .iops_wr = qdict_get_int(qdict, "iops_wr"),
1883     };
1884
1885     /* qmp_block_set_io_throttle has separate parameters for the
1886      * (deprecated) block device name and the qdev ID but the HMP
1887      * version has only one, so we must decide which one to pass. */
1888     if (blk_by_name(device)) {
1889         throttle.has_device = true;
1890         throttle.device = device;
1891     } else {
1892         throttle.has_id = true;
1893         throttle.id = device;
1894     }
1895
1896     qmp_block_set_io_throttle(&throttle, &err);
1897     hmp_handle_error(mon, &err);
1898 }
1899
1900 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1901 {
1902     Error *error = NULL;
1903     const char *device = qdict_get_str(qdict, "device");
1904     const char *base = qdict_get_try_str(qdict, "base");
1905     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1906
1907     qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
1908                      false, NULL, qdict_haskey(qdict, "speed"), speed, true,
1909                      BLOCKDEV_ON_ERROR_REPORT, false, false, false, false,
1910                      &error);
1911
1912     hmp_handle_error(mon, &error);
1913 }
1914
1915 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1916 {
1917     Error *error = NULL;
1918     const char *device = qdict_get_str(qdict, "device");
1919     int64_t value = qdict_get_int(qdict, "speed");
1920
1921     qmp_block_job_set_speed(device, value, &error);
1922
1923     hmp_handle_error(mon, &error);
1924 }
1925
1926 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1927 {
1928     Error *error = NULL;
1929     const char *device = qdict_get_str(qdict, "device");
1930     bool force = qdict_get_try_bool(qdict, "force", false);
1931
1932     qmp_block_job_cancel(device, true, force, &error);
1933
1934     hmp_handle_error(mon, &error);
1935 }
1936
1937 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1938 {
1939     Error *error = NULL;
1940     const char *device = qdict_get_str(qdict, "device");
1941
1942     qmp_block_job_pause(device, &error);
1943
1944     hmp_handle_error(mon, &error);
1945 }
1946
1947 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1948 {
1949     Error *error = NULL;
1950     const char *device = qdict_get_str(qdict, "device");
1951
1952     qmp_block_job_resume(device, &error);
1953
1954     hmp_handle_error(mon, &error);
1955 }
1956
1957 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1958 {
1959     Error *error = NULL;
1960     const char *device = qdict_get_str(qdict, "device");
1961
1962     qmp_block_job_complete(device, &error);
1963
1964     hmp_handle_error(mon, &error);
1965 }
1966
1967 typedef struct HMPMigrationStatus
1968 {
1969     QEMUTimer *timer;
1970     Monitor *mon;
1971     bool is_block_migration;
1972 } HMPMigrationStatus;
1973
1974 static void hmp_migrate_status_cb(void *opaque)
1975 {
1976     HMPMigrationStatus *status = opaque;
1977     MigrationInfo *info;
1978
1979     info = qmp_query_migrate(NULL);
1980     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1981         info->status == MIGRATION_STATUS_SETUP) {
1982         if (info->has_disk) {
1983             int progress;
1984
1985             if (info->disk->remaining) {
1986                 progress = info->disk->transferred * 100 / info->disk->total;
1987             } else {
1988                 progress = 100;
1989             }
1990
1991             monitor_printf(status->mon, "Completed %d %%\r", progress);
1992             monitor_flush(status->mon);
1993         }
1994
1995         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1996     } else {
1997         if (status->is_block_migration) {
1998             monitor_printf(status->mon, "\n");
1999         }
2000         if (info->has_error_desc) {
2001             error_report("%s", info->error_desc);
2002         }
2003         monitor_resume(status->mon);
2004         timer_del(status->timer);
2005         g_free(status);
2006     }
2007
2008     qapi_free_MigrationInfo(info);
2009 }
2010
2011 void hmp_migrate(Monitor *mon, const QDict *qdict)
2012 {
2013     bool detach = qdict_get_try_bool(qdict, "detach", false);
2014     bool blk = qdict_get_try_bool(qdict, "blk", false);
2015     bool inc = qdict_get_try_bool(qdict, "inc", false);
2016     bool resume = qdict_get_try_bool(qdict, "resume", false);
2017     const char *uri = qdict_get_str(qdict, "uri");
2018     Error *err = NULL;
2019
2020     qmp_migrate(uri, !!blk, blk, !!inc, inc,
2021                 false, false, true, resume, &err);
2022     if (err) {
2023         hmp_handle_error(mon, &err);
2024         return;
2025     }
2026
2027     if (!detach) {
2028         HMPMigrationStatus *status;
2029
2030         if (monitor_suspend(mon) < 0) {
2031             monitor_printf(mon, "terminal does not allow synchronous "
2032                            "migration, continuing detached\n");
2033             return;
2034         }
2035
2036         status = g_malloc0(sizeof(*status));
2037         status->mon = mon;
2038         status->is_block_migration = blk || inc;
2039         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
2040                                           status);
2041         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
2042     }
2043 }
2044
2045 void hmp_device_add(Monitor *mon, const QDict *qdict)
2046 {
2047     Error *err = NULL;
2048
2049     qmp_device_add((QDict *)qdict, NULL, &err);
2050     hmp_handle_error(mon, &err);
2051 }
2052
2053 void hmp_device_del(Monitor *mon, const QDict *qdict)
2054 {
2055     const char *id = qdict_get_str(qdict, "id");
2056     Error *err = NULL;
2057
2058     qmp_device_del(id, &err);
2059     hmp_handle_error(mon, &err);
2060 }
2061
2062 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
2063 {
2064     Error *err = NULL;
2065     bool win_dmp = qdict_get_try_bool(qdict, "windmp", false);
2066     bool paging = qdict_get_try_bool(qdict, "paging", false);
2067     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
2068     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
2069     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
2070     const char *file = qdict_get_str(qdict, "filename");
2071     bool has_begin = qdict_haskey(qdict, "begin");
2072     bool has_length = qdict_haskey(qdict, "length");
2073     bool has_detach = qdict_haskey(qdict, "detach");
2074     int64_t begin = 0;
2075     int64_t length = 0;
2076     bool detach = false;
2077     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
2078     char *prot;
2079
2080     if (zlib + lzo + snappy + win_dmp > 1) {
2081         error_setg(&err, "only one of '-z|-l|-s|-w' can be set");
2082         hmp_handle_error(mon, &err);
2083         return;
2084     }
2085
2086     if (win_dmp) {
2087         dump_format = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
2088     }
2089
2090     if (zlib) {
2091         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2092     }
2093
2094     if (lzo) {
2095         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2096     }
2097
2098     if (snappy) {
2099         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2100     }
2101
2102     if (has_begin) {
2103         begin = qdict_get_int(qdict, "begin");
2104     }
2105     if (has_length) {
2106         length = qdict_get_int(qdict, "length");
2107     }
2108     if (has_detach) {
2109         detach = qdict_get_bool(qdict, "detach");
2110     }
2111
2112     prot = g_strconcat("file:", file, NULL);
2113
2114     qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
2115                           has_length, length, true, dump_format, &err);
2116     hmp_handle_error(mon, &err);
2117     g_free(prot);
2118 }
2119
2120 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
2121 {
2122     Error *err = NULL;
2123     QemuOpts *opts;
2124
2125     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
2126     if (err) {
2127         goto out;
2128     }
2129
2130     netdev_add(opts, &err);
2131     if (err) {
2132         qemu_opts_del(opts);
2133     }
2134
2135 out:
2136     hmp_handle_error(mon, &err);
2137 }
2138
2139 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2140 {
2141     const char *id = qdict_get_str(qdict, "id");
2142     Error *err = NULL;
2143
2144     qmp_netdev_del(id, &err);
2145     hmp_handle_error(mon, &err);
2146 }
2147
2148 void hmp_object_add(Monitor *mon, const QDict *qdict)
2149 {
2150     Error *err = NULL;
2151     QemuOpts *opts;
2152     Object *obj = NULL;
2153
2154     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2155     if (err) {
2156         hmp_handle_error(mon, &err);
2157         return;
2158     }
2159
2160     obj = user_creatable_add_opts(opts, &err);
2161     qemu_opts_del(opts);
2162
2163     if (err) {
2164         hmp_handle_error(mon, &err);
2165     }
2166     if (obj) {
2167         object_unref(obj);
2168     }
2169 }
2170
2171 void hmp_getfd(Monitor *mon, const QDict *qdict)
2172 {
2173     const char *fdname = qdict_get_str(qdict, "fdname");
2174     Error *err = NULL;
2175
2176     qmp_getfd(fdname, &err);
2177     hmp_handle_error(mon, &err);
2178 }
2179
2180 void hmp_closefd(Monitor *mon, const QDict *qdict)
2181 {
2182     const char *fdname = qdict_get_str(qdict, "fdname");
2183     Error *err = NULL;
2184
2185     qmp_closefd(fdname, &err);
2186     hmp_handle_error(mon, &err);
2187 }
2188
2189 void hmp_sendkey(Monitor *mon, const QDict *qdict)
2190 {
2191     const char *keys = qdict_get_str(qdict, "keys");
2192     KeyValueList *keylist, *head = NULL, *tmp = NULL;
2193     int has_hold_time = qdict_haskey(qdict, "hold-time");
2194     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2195     Error *err = NULL;
2196     const char *separator;
2197     int keyname_len;
2198
2199     while (1) {
2200         separator = qemu_strchrnul(keys, '-');
2201         keyname_len = separator - keys;
2202
2203         /* Be compatible with old interface, convert user inputted "<" */
2204         if (keys[0] == '<' && keyname_len == 1) {
2205             keys = "less";
2206             keyname_len = 4;
2207         }
2208
2209         keylist = g_malloc0(sizeof(*keylist));
2210         keylist->value = g_malloc0(sizeof(*keylist->value));
2211
2212         if (!head) {
2213             head = keylist;
2214         }
2215         if (tmp) {
2216             tmp->next = keylist;
2217         }
2218         tmp = keylist;
2219
2220         if (strstart(keys, "0x", NULL)) {
2221             char *endp;
2222             int value = strtoul(keys, &endp, 0);
2223             assert(endp <= keys + keyname_len);
2224             if (endp != keys + keyname_len) {
2225                 goto err_out;
2226             }
2227             keylist->value->type = KEY_VALUE_KIND_NUMBER;
2228             keylist->value->u.number.data = value;
2229         } else {
2230             int idx = index_from_key(keys, keyname_len);
2231             if (idx == Q_KEY_CODE__MAX) {
2232                 goto err_out;
2233             }
2234             keylist->value->type = KEY_VALUE_KIND_QCODE;
2235             keylist->value->u.qcode.data = idx;
2236         }
2237
2238         if (!*separator) {
2239             break;
2240         }
2241         keys = separator + 1;
2242     }
2243
2244     qmp_send_key(head, has_hold_time, hold_time, &err);
2245     hmp_handle_error(mon, &err);
2246
2247 out:
2248     qapi_free_KeyValueList(head);
2249     return;
2250
2251 err_out:
2252     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
2253     goto out;
2254 }
2255
2256 void hmp_screendump(Monitor *mon, const QDict *qdict)
2257 {
2258     const char *filename = qdict_get_str(qdict, "filename");
2259     const char *id = qdict_get_try_str(qdict, "device");
2260     int64_t head = qdict_get_try_int(qdict, "head", 0);
2261     Error *err = NULL;
2262
2263     qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
2264     hmp_handle_error(mon, &err);
2265 }
2266
2267 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2268 {
2269     const char *uri = qdict_get_str(qdict, "uri");
2270     bool writable = qdict_get_try_bool(qdict, "writable", false);
2271     bool all = qdict_get_try_bool(qdict, "all", false);
2272     Error *local_err = NULL;
2273     BlockInfoList *block_list, *info;
2274     SocketAddress *addr;
2275
2276     if (writable && !all) {
2277         error_setg(&local_err, "-w only valid together with -a");
2278         goto exit;
2279     }
2280
2281     /* First check if the address is valid and start the server.  */
2282     addr = socket_parse(uri, &local_err);
2283     if (local_err != NULL) {
2284         goto exit;
2285     }
2286
2287     nbd_server_start(addr, NULL, &local_err);
2288     qapi_free_SocketAddress(addr);
2289     if (local_err != NULL) {
2290         goto exit;
2291     }
2292
2293     if (!all) {
2294         return;
2295     }
2296
2297     /* Then try adding all block devices.  If one fails, close all and
2298      * exit.
2299      */
2300     block_list = qmp_query_block(NULL);
2301
2302     for (info = block_list; info; info = info->next) {
2303         if (!info->value->has_inserted) {
2304             continue;
2305         }
2306
2307         qmp_nbd_server_add(info->value->device, false, NULL,
2308                            true, writable, &local_err);
2309
2310         if (local_err != NULL) {
2311             qmp_nbd_server_stop(NULL);
2312             break;
2313         }
2314     }
2315
2316     qapi_free_BlockInfoList(block_list);
2317
2318 exit:
2319     hmp_handle_error(mon, &local_err);
2320 }
2321
2322 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2323 {
2324     const char *device = qdict_get_str(qdict, "device");
2325     const char *name = qdict_get_try_str(qdict, "name");
2326     bool writable = qdict_get_try_bool(qdict, "writable", false);
2327     Error *local_err = NULL;
2328
2329     qmp_nbd_server_add(device, !!name, name, true, writable, &local_err);
2330     hmp_handle_error(mon, &local_err);
2331 }
2332
2333 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
2334 {
2335     const char *name = qdict_get_str(qdict, "name");
2336     bool force = qdict_get_try_bool(qdict, "force", false);
2337     Error *err = NULL;
2338
2339     /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
2340     qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
2341     hmp_handle_error(mon, &err);
2342 }
2343
2344 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2345 {
2346     Error *err = NULL;
2347
2348     qmp_nbd_server_stop(&err);
2349     hmp_handle_error(mon, &err);
2350 }
2351
2352 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2353 {
2354     int cpuid;
2355     Error *err = NULL;
2356
2357     cpuid = qdict_get_int(qdict, "id");
2358     qmp_cpu_add(cpuid, &err);
2359     hmp_handle_error(mon, &err);
2360 }
2361
2362 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2363 {
2364     const char *args = qdict_get_str(qdict, "args");
2365     Error *err = NULL;
2366     QemuOpts *opts;
2367
2368     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2369     if (opts == NULL) {
2370         error_setg(&err, "Parsing chardev args failed");
2371     } else {
2372         qemu_chr_new_from_opts(opts, &err);
2373         qemu_opts_del(opts);
2374     }
2375     hmp_handle_error(mon, &err);
2376 }
2377
2378 void hmp_chardev_change(Monitor *mon, const QDict *qdict)
2379 {
2380     const char *args = qdict_get_str(qdict, "args");
2381     const char *id;
2382     Error *err = NULL;
2383     ChardevBackend *backend = NULL;
2384     ChardevReturn *ret = NULL;
2385     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
2386                                              true);
2387     if (!opts) {
2388         error_setg(&err, "Parsing chardev args failed");
2389         goto end;
2390     }
2391
2392     id = qdict_get_str(qdict, "id");
2393     if (qemu_opts_id(opts)) {
2394         error_setg(&err, "Unexpected 'id' parameter");
2395         goto end;
2396     }
2397
2398     backend = qemu_chr_parse_opts(opts, &err);
2399     if (!backend) {
2400         goto end;
2401     }
2402
2403     ret = qmp_chardev_change(id, backend, &err);
2404
2405 end:
2406     qapi_free_ChardevReturn(ret);
2407     qapi_free_ChardevBackend(backend);
2408     qemu_opts_del(opts);
2409     hmp_handle_error(mon, &err);
2410 }
2411
2412 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2413 {
2414     Error *local_err = NULL;
2415
2416     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2417     hmp_handle_error(mon, &local_err);
2418 }
2419
2420 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2421 {
2422     Error *local_err = NULL;
2423
2424     qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2425     hmp_handle_error(mon, &local_err);
2426 }
2427
2428 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2429 {
2430     BlockBackend *blk;
2431     BlockBackend *local_blk = NULL;
2432     const char* device = qdict_get_str(qdict, "device");
2433     const char* command = qdict_get_str(qdict, "command");
2434     Error *err = NULL;
2435     int ret;
2436
2437     blk = blk_by_name(device);
2438     if (!blk) {
2439         BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2440         if (bs) {
2441             blk = local_blk = blk_new(0, BLK_PERM_ALL);
2442             ret = blk_insert_bs(blk, bs, &err);
2443             if (ret < 0) {
2444                 goto fail;
2445             }
2446         } else {
2447             goto fail;
2448         }
2449     }
2450
2451     /*
2452      * Notably absent: Proper permission management. This is sad, but it seems
2453      * almost impossible to achieve without changing the semantics and thereby
2454      * limiting the use cases of the qemu-io HMP command.
2455      *
2456      * In an ideal world we would unconditionally create a new BlockBackend for
2457      * qemuio_command(), but we have commands like 'reopen' and want them to
2458      * take effect on the exact BlockBackend whose name the user passed instead
2459      * of just on a temporary copy of it.
2460      *
2461      * Another problem is that deleting the temporary BlockBackend involves
2462      * draining all requests on it first, but some qemu-iotests cases want to
2463      * issue multiple aio_read/write requests and expect them to complete in
2464      * the background while the monitor has already returned.
2465      *
2466      * This is also what prevents us from saving the original permissions and
2467      * restoring them later: We can't revoke permissions until all requests
2468      * have completed, and we don't know when that is nor can we really let
2469      * anything else run before we have revoken them to avoid race conditions.
2470      *
2471      * What happens now is that command() in qemu-io-cmds.c can extend the
2472      * permissions if necessary for the qemu-io command. And they simply stay
2473      * extended, possibly resulting in a read-only guest device keeping write
2474      * permissions. Ugly, but it appears to be the lesser evil.
2475      */
2476     qemuio_command(blk, command);
2477
2478 fail:
2479     blk_unref(local_blk);
2480     hmp_handle_error(mon, &err);
2481 }
2482
2483 void hmp_object_del(Monitor *mon, const QDict *qdict)
2484 {
2485     const char *id = qdict_get_str(qdict, "id");
2486     Error *err = NULL;
2487
2488     user_creatable_del(id, &err);
2489     hmp_handle_error(mon, &err);
2490 }
2491
2492 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2493 {
2494     Error *err = NULL;
2495     MemdevList *memdev_list = qmp_query_memdev(&err);
2496     MemdevList *m = memdev_list;
2497     Visitor *v;
2498     char *str;
2499
2500     while (m) {
2501         v = string_output_visitor_new(false, &str);
2502         visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2503         monitor_printf(mon, "memory backend: %s\n", m->value->id);
2504         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
2505         monitor_printf(mon, "  merge: %s\n",
2506                        m->value->merge ? "true" : "false");
2507         monitor_printf(mon, "  dump: %s\n",
2508                        m->value->dump ? "true" : "false");
2509         monitor_printf(mon, "  prealloc: %s\n",
2510                        m->value->prealloc ? "true" : "false");
2511         monitor_printf(mon, "  policy: %s\n",
2512                        HostMemPolicy_str(m->value->policy));
2513         visit_complete(v, &str);
2514         monitor_printf(mon, "  host nodes: %s\n", str);
2515
2516         g_free(str);
2517         visit_free(v);
2518         m = m->next;
2519     }
2520
2521     monitor_printf(mon, "\n");
2522
2523     qapi_free_MemdevList(memdev_list);
2524     hmp_handle_error(mon, &err);
2525 }
2526
2527 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2528 {
2529     Error *err = NULL;
2530     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2531     MemoryDeviceInfoList *info;
2532     MemoryDeviceInfo *value;
2533     PCDIMMDeviceInfo *di;
2534
2535     for (info = info_list; info; info = info->next) {
2536         value = info->value;
2537
2538         if (value) {
2539             switch (value->type) {
2540             case MEMORY_DEVICE_INFO_KIND_DIMM:
2541                 di = value->u.dimm.data;
2542                 break;
2543
2544             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
2545                 di = value->u.nvdimm.data;
2546                 break;
2547
2548             default:
2549                 di = NULL;
2550                 break;
2551             }
2552
2553             if (di) {
2554                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2555                                MemoryDeviceInfoKind_str(value->type),
2556                                di->id ? di->id : "");
2557                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
2558                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
2559                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
2560                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
2561                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
2562                 monitor_printf(mon, "  hotplugged: %s\n",
2563                                di->hotplugged ? "true" : "false");
2564                 monitor_printf(mon, "  hotpluggable: %s\n",
2565                                di->hotpluggable ? "true" : "false");
2566             }
2567         }
2568     }
2569
2570     qapi_free_MemoryDeviceInfoList(info_list);
2571     hmp_handle_error(mon, &err);
2572 }
2573
2574 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2575 {
2576     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2577     IOThreadInfoList *info;
2578     IOThreadInfo *value;
2579
2580     for (info = info_list; info; info = info->next) {
2581         value = info->value;
2582         monitor_printf(mon, "%s:\n", value->id);
2583         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
2584         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2585         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
2586         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
2587     }
2588
2589     qapi_free_IOThreadInfoList(info_list);
2590 }
2591
2592 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2593 {
2594     const char *path = qdict_get_try_str(qdict, "path");
2595     ObjectPropertyInfoList *list;
2596     Error *err = NULL;
2597
2598     if (path == NULL) {
2599         monitor_printf(mon, "/\n");
2600         return;
2601     }
2602
2603     list = qmp_qom_list(path, &err);
2604     if (err == NULL) {
2605         ObjectPropertyInfoList *start = list;
2606         while (list != NULL) {
2607             ObjectPropertyInfo *value = list->value;
2608
2609             monitor_printf(mon, "%s (%s)\n",
2610                            value->name, value->type);
2611             list = list->next;
2612         }
2613         qapi_free_ObjectPropertyInfoList(start);
2614     }
2615     hmp_handle_error(mon, &err);
2616 }
2617
2618 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2619 {
2620     const char *path = qdict_get_str(qdict, "path");
2621     const char *property = qdict_get_str(qdict, "property");
2622     const char *value = qdict_get_str(qdict, "value");
2623     Error *err = NULL;
2624     bool ambiguous = false;
2625     Object *obj;
2626
2627     obj = object_resolve_path(path, &ambiguous);
2628     if (obj == NULL) {
2629         error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2630                   "Device '%s' not found", path);
2631     } else {
2632         if (ambiguous) {
2633             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2634         }
2635         object_property_parse(obj, value, property, &err);
2636     }
2637     hmp_handle_error(mon, &err);
2638 }
2639
2640 void hmp_rocker(Monitor *mon, const QDict *qdict)
2641 {
2642     const char *name = qdict_get_str(qdict, "name");
2643     RockerSwitch *rocker;
2644     Error *err = NULL;
2645
2646     rocker = qmp_query_rocker(name, &err);
2647     if (err != NULL) {
2648         hmp_handle_error(mon, &err);
2649         return;
2650     }
2651
2652     monitor_printf(mon, "name: %s\n", rocker->name);
2653     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2654     monitor_printf(mon, "ports: %d\n", rocker->ports);
2655
2656     qapi_free_RockerSwitch(rocker);
2657 }
2658
2659 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2660 {
2661     RockerPortList *list, *port;
2662     const char *name = qdict_get_str(qdict, "name");
2663     Error *err = NULL;
2664
2665     list = qmp_query_rocker_ports(name, &err);
2666     if (err != NULL) {
2667         hmp_handle_error(mon, &err);
2668         return;
2669     }
2670
2671     monitor_printf(mon, "            ena/    speed/ auto\n");
2672     monitor_printf(mon, "      port  link    duplex neg?\n");
2673
2674     for (port = list; port; port = port->next) {
2675         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
2676                        port->value->name,
2677                        port->value->enabled ? port->value->link_up ?
2678                        "up" : "down" : "!ena",
2679                        port->value->speed == 10000 ? "10G" : "??",
2680                        port->value->duplex ? "FD" : "HD",
2681                        port->value->autoneg ? "Yes" : "No");
2682     }
2683
2684     qapi_free_RockerPortList(list);
2685 }
2686
2687 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2688 {
2689     RockerOfDpaFlowList *list, *info;
2690     const char *name = qdict_get_str(qdict, "name");
2691     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2692     Error *err = NULL;
2693
2694     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2695     if (err != NULL) {
2696         hmp_handle_error(mon, &err);
2697         return;
2698     }
2699
2700     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2701
2702     for (info = list; info; info = info->next) {
2703         RockerOfDpaFlow *flow = info->value;
2704         RockerOfDpaFlowKey *key = flow->key;
2705         RockerOfDpaFlowMask *mask = flow->mask;
2706         RockerOfDpaFlowAction *action = flow->action;
2707
2708         if (flow->hits) {
2709             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2710                            key->priority, key->tbl_id, flow->hits);
2711         } else {
2712             monitor_printf(mon, "%-4d %-3d     ",
2713                            key->priority, key->tbl_id);
2714         }
2715
2716         if (key->has_in_pport) {
2717             monitor_printf(mon, " pport %d", key->in_pport);
2718             if (mask->has_in_pport) {
2719                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2720             }
2721         }
2722
2723         if (key->has_vlan_id) {
2724             monitor_printf(mon, " vlan %d",
2725                            key->vlan_id & VLAN_VID_MASK);
2726             if (mask->has_vlan_id) {
2727                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2728             }
2729         }
2730
2731         if (key->has_tunnel_id) {
2732             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2733             if (mask->has_tunnel_id) {
2734                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2735             }
2736         }
2737
2738         if (key->has_eth_type) {
2739             switch (key->eth_type) {
2740             case 0x0806:
2741                 monitor_printf(mon, " ARP");
2742                 break;
2743             case 0x0800:
2744                 monitor_printf(mon, " IP");
2745                 break;
2746             case 0x86dd:
2747                 monitor_printf(mon, " IPv6");
2748                 break;
2749             case 0x8809:
2750                 monitor_printf(mon, " LACP");
2751                 break;
2752             case 0x88cc:
2753                 monitor_printf(mon, " LLDP");
2754                 break;
2755             default:
2756                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2757                 break;
2758             }
2759         }
2760
2761         if (key->has_eth_src) {
2762             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2763                 (mask->has_eth_src) &&
2764                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2765                 monitor_printf(mon, " src <any mcast/bcast>");
2766             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2767                 (mask->has_eth_src) &&
2768                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2769                 monitor_printf(mon, " src <any ucast>");
2770             } else {
2771                 monitor_printf(mon, " src %s", key->eth_src);
2772                 if (mask->has_eth_src) {
2773                     monitor_printf(mon, "(%s)", mask->eth_src);
2774                 }
2775             }
2776         }
2777
2778         if (key->has_eth_dst) {
2779             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2780                 (mask->has_eth_dst) &&
2781                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2782                 monitor_printf(mon, " dst <any mcast/bcast>");
2783             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2784                 (mask->has_eth_dst) &&
2785                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2786                 monitor_printf(mon, " dst <any ucast>");
2787             } else {
2788                 monitor_printf(mon, " dst %s", key->eth_dst);
2789                 if (mask->has_eth_dst) {
2790                     monitor_printf(mon, "(%s)", mask->eth_dst);
2791                 }
2792             }
2793         }
2794
2795         if (key->has_ip_proto) {
2796             monitor_printf(mon, " proto %d", key->ip_proto);
2797             if (mask->has_ip_proto) {
2798                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2799             }
2800         }
2801
2802         if (key->has_ip_tos) {
2803             monitor_printf(mon, " TOS %d", key->ip_tos);
2804             if (mask->has_ip_tos) {
2805                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2806             }
2807         }
2808
2809         if (key->has_ip_dst) {
2810             monitor_printf(mon, " dst %s", key->ip_dst);
2811         }
2812
2813         if (action->has_goto_tbl || action->has_group_id ||
2814             action->has_new_vlan_id) {
2815             monitor_printf(mon, " -->");
2816         }
2817
2818         if (action->has_new_vlan_id) {
2819             monitor_printf(mon, " apply new vlan %d",
2820                            ntohs(action->new_vlan_id));
2821         }
2822
2823         if (action->has_group_id) {
2824             monitor_printf(mon, " write group 0x%08x", action->group_id);
2825         }
2826
2827         if (action->has_goto_tbl) {
2828             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2829         }
2830
2831         monitor_printf(mon, "\n");
2832     }
2833
2834     qapi_free_RockerOfDpaFlowList(list);
2835 }
2836
2837 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2838 {
2839     RockerOfDpaGroupList *list, *g;
2840     const char *name = qdict_get_str(qdict, "name");
2841     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2842     Error *err = NULL;
2843     bool set = false;
2844
2845     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2846     if (err != NULL) {
2847         hmp_handle_error(mon, &err);
2848         return;
2849     }
2850
2851     monitor_printf(mon, "id (decode) --> buckets\n");
2852
2853     for (g = list; g; g = g->next) {
2854         RockerOfDpaGroup *group = g->value;
2855
2856         monitor_printf(mon, "0x%08x", group->id);
2857
2858         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2859                                          group->type == 1 ? "L2 rewrite" :
2860                                          group->type == 2 ? "L3 unicast" :
2861                                          group->type == 3 ? "L2 multicast" :
2862                                          group->type == 4 ? "L2 flood" :
2863                                          group->type == 5 ? "L3 interface" :
2864                                          group->type == 6 ? "L3 multicast" :
2865                                          group->type == 7 ? "L3 ECMP" :
2866                                          group->type == 8 ? "L2 overlay" :
2867                                          "unknown");
2868
2869         if (group->has_vlan_id) {
2870             monitor_printf(mon, " vlan %d", group->vlan_id);
2871         }
2872
2873         if (group->has_pport) {
2874             monitor_printf(mon, " pport %d", group->pport);
2875         }
2876
2877         if (group->has_index) {
2878             monitor_printf(mon, " index %d", group->index);
2879         }
2880
2881         monitor_printf(mon, ") -->");
2882
2883         if (group->has_set_vlan_id && group->set_vlan_id) {
2884             set = true;
2885             monitor_printf(mon, " set vlan %d",
2886                            group->set_vlan_id & VLAN_VID_MASK);
2887         }
2888
2889         if (group->has_set_eth_src) {
2890             if (!set) {
2891                 set = true;
2892                 monitor_printf(mon, " set");
2893             }
2894             monitor_printf(mon, " src %s", group->set_eth_src);
2895         }
2896
2897         if (group->has_set_eth_dst) {
2898             if (!set) {
2899                 set = true;
2900                 monitor_printf(mon, " set");
2901             }
2902             monitor_printf(mon, " dst %s", group->set_eth_dst);
2903         }
2904
2905         set = false;
2906
2907         if (group->has_ttl_check && group->ttl_check) {
2908             monitor_printf(mon, " check TTL");
2909         }
2910
2911         if (group->has_group_id && group->group_id) {
2912             monitor_printf(mon, " group id 0x%08x", group->group_id);
2913         }
2914
2915         if (group->has_pop_vlan && group->pop_vlan) {
2916             monitor_printf(mon, " pop vlan");
2917         }
2918
2919         if (group->has_out_pport) {
2920             monitor_printf(mon, " out pport %d", group->out_pport);
2921         }
2922
2923         if (group->has_group_ids) {
2924             struct uint32List *id;
2925
2926             monitor_printf(mon, " groups [");
2927             for (id = group->group_ids; id; id = id->next) {
2928                 monitor_printf(mon, "0x%08x", id->value);
2929                 if (id->next) {
2930                     monitor_printf(mon, ",");
2931                 }
2932             }
2933             monitor_printf(mon, "]");
2934         }
2935
2936         monitor_printf(mon, "\n");
2937     }
2938
2939     qapi_free_RockerOfDpaGroupList(list);
2940 }
2941
2942 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2943 {
2944     DumpQueryResult *result = qmp_query_dump(NULL);
2945
2946     assert(result && result->status < DUMP_STATUS__MAX);
2947     monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
2948
2949     if (result->status == DUMP_STATUS_ACTIVE) {
2950         float percent = 0;
2951         assert(result->total != 0);
2952         percent = 100.0 * result->completed / result->total;
2953         monitor_printf(mon, "Finished: %.2f %%\n", percent);
2954     }
2955
2956     qapi_free_DumpQueryResult(result);
2957 }
2958
2959 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2960 {
2961     ram_block_dump(mon);
2962 }
2963
2964 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2965 {
2966     Error *err = NULL;
2967     HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2968     HotpluggableCPUList *saved = l;
2969     CpuInstanceProperties *c;
2970
2971     if (err != NULL) {
2972         hmp_handle_error(mon, &err);
2973         return;
2974     }
2975
2976     monitor_printf(mon, "Hotpluggable CPUs:\n");
2977     while (l) {
2978         monitor_printf(mon, "  type: \"%s\"\n", l->value->type);
2979         monitor_printf(mon, "  vcpus_count: \"%" PRIu64 "\"\n",
2980                        l->value->vcpus_count);
2981         if (l->value->has_qom_path) {
2982             monitor_printf(mon, "  qom_path: \"%s\"\n", l->value->qom_path);
2983         }
2984
2985         c = l->value->props;
2986         monitor_printf(mon, "  CPUInstance Properties:\n");
2987         if (c->has_node_id) {
2988             monitor_printf(mon, "    node-id: \"%" PRIu64 "\"\n", c->node_id);
2989         }
2990         if (c->has_socket_id) {
2991             monitor_printf(mon, "    socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2992         }
2993         if (c->has_core_id) {
2994             monitor_printf(mon, "    core-id: \"%" PRIu64 "\"\n", c->core_id);
2995         }
2996         if (c->has_thread_id) {
2997             monitor_printf(mon, "    thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2998         }
2999
3000         l = l->next;
3001     }
3002
3003     qapi_free_HotpluggableCPUList(saved);
3004 }
3005
3006 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
3007 {
3008     Error *err = NULL;
3009     GuidInfo *info = qmp_query_vm_generation_id(&err);
3010     if (info) {
3011         monitor_printf(mon, "%s\n", info->guid);
3012     }
3013     hmp_handle_error(mon, &err);
3014     qapi_free_GuidInfo(info);
3015 }
3016
3017 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
3018 {
3019     Error *err = NULL;
3020     MemoryInfo *info = qmp_query_memory_size_summary(&err);
3021     if (info) {
3022         monitor_printf(mon, "base memory: %" PRIu64 "\n",
3023                        info->base_memory);
3024
3025         if (info->has_plugged_memory) {
3026             monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
3027                            info->plugged_memory);
3028         }
3029
3030         qapi_free_MemoryInfo(info);
3031     }
3032     hmp_handle_error(mon, &err);
3033 }
This page took 0.191778 seconds and 4 git commands to generate.