]> Git Repo - qemu.git/blob - hmp.c
migration: do not wait for free thread
[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_system_reset(Monitor *mon, const QDict *qdict)
1074 {
1075     qmp_system_reset(NULL);
1076 }
1077
1078 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1079 {
1080     qmp_system_powerdown(NULL);
1081 }
1082
1083 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
1084 {
1085     Error *err = NULL;
1086
1087     qmp_x_exit_preconfig(&err);
1088     hmp_handle_error(mon, &err);
1089 }
1090
1091 void hmp_cpu(Monitor *mon, const QDict *qdict)
1092 {
1093     int64_t cpu_index;
1094
1095     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1096             use it are converted to the QAPI */
1097     cpu_index = qdict_get_int(qdict, "index");
1098     if (monitor_set_cpu(cpu_index) < 0) {
1099         monitor_printf(mon, "invalid CPU index\n");
1100     }
1101 }
1102
1103 void hmp_memsave(Monitor *mon, const QDict *qdict)
1104 {
1105     uint32_t size = qdict_get_int(qdict, "size");
1106     const char *filename = qdict_get_str(qdict, "filename");
1107     uint64_t addr = qdict_get_int(qdict, "val");
1108     Error *err = NULL;
1109     int cpu_index = monitor_get_cpu_index();
1110
1111     if (cpu_index < 0) {
1112         monitor_printf(mon, "No CPU available\n");
1113         return;
1114     }
1115
1116     qmp_memsave(addr, size, filename, true, cpu_index, &err);
1117     hmp_handle_error(mon, &err);
1118 }
1119
1120 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1121 {
1122     uint32_t size = qdict_get_int(qdict, "size");
1123     const char *filename = qdict_get_str(qdict, "filename");
1124     uint64_t addr = qdict_get_int(qdict, "val");
1125     Error *err = NULL;
1126
1127     qmp_pmemsave(addr, size, filename, &err);
1128     hmp_handle_error(mon, &err);
1129 }
1130
1131 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1132 {
1133     const char *chardev = qdict_get_str(qdict, "device");
1134     const char *data = qdict_get_str(qdict, "data");
1135     Error *err = NULL;
1136
1137     qmp_ringbuf_write(chardev, data, false, 0, &err);
1138
1139     hmp_handle_error(mon, &err);
1140 }
1141
1142 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1143 {
1144     uint32_t size = qdict_get_int(qdict, "size");
1145     const char *chardev = qdict_get_str(qdict, "device");
1146     char *data;
1147     Error *err = NULL;
1148     int i;
1149
1150     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1151     if (err) {
1152         hmp_handle_error(mon, &err);
1153         return;
1154     }
1155
1156     for (i = 0; data[i]; i++) {
1157         unsigned char ch = data[i];
1158
1159         if (ch == '\\') {
1160             monitor_printf(mon, "\\\\");
1161         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1162             monitor_printf(mon, "\\u%04X", ch);
1163         } else {
1164             monitor_printf(mon, "%c", ch);
1165         }
1166
1167     }
1168     monitor_printf(mon, "\n");
1169     g_free(data);
1170 }
1171
1172 void hmp_cont(Monitor *mon, const QDict *qdict)
1173 {
1174     Error *err = NULL;
1175
1176     qmp_cont(&err);
1177     hmp_handle_error(mon, &err);
1178 }
1179
1180 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1181 {
1182     qmp_system_wakeup(NULL);
1183 }
1184
1185 void hmp_nmi(Monitor *mon, const QDict *qdict)
1186 {
1187     Error *err = NULL;
1188
1189     qmp_inject_nmi(&err);
1190     hmp_handle_error(mon, &err);
1191 }
1192
1193 void hmp_set_link(Monitor *mon, const QDict *qdict)
1194 {
1195     const char *name = qdict_get_str(qdict, "name");
1196     bool up = qdict_get_bool(qdict, "up");
1197     Error *err = NULL;
1198
1199     qmp_set_link(name, up, &err);
1200     hmp_handle_error(mon, &err);
1201 }
1202
1203 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1204 {
1205     const char *device = qdict_get_str(qdict, "device");
1206     const char *password = qdict_get_str(qdict, "password");
1207     Error *err = NULL;
1208
1209     qmp_block_passwd(true, device, false, NULL, password, &err);
1210     hmp_handle_error(mon, &err);
1211 }
1212
1213 void hmp_balloon(Monitor *mon, const QDict *qdict)
1214 {
1215     int64_t value = qdict_get_int(qdict, "value");
1216     Error *err = NULL;
1217
1218     qmp_balloon(value, &err);
1219     hmp_handle_error(mon, &err);
1220 }
1221
1222 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1223 {
1224     const char *device = qdict_get_str(qdict, "device");
1225     int64_t size = qdict_get_int(qdict, "size");
1226     Error *err = NULL;
1227
1228     qmp_block_resize(true, device, false, NULL, size, &err);
1229     hmp_handle_error(mon, &err);
1230 }
1231
1232 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1233 {
1234     const char *filename = qdict_get_str(qdict, "target");
1235     const char *format = qdict_get_try_str(qdict, "format");
1236     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1237     bool full = qdict_get_try_bool(qdict, "full", false);
1238     Error *err = NULL;
1239     DriveMirror mirror = {
1240         .device = (char *)qdict_get_str(qdict, "device"),
1241         .target = (char *)filename,
1242         .has_format = !!format,
1243         .format = (char *)format,
1244         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1245         .has_mode = true,
1246         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1247         .unmap = true,
1248     };
1249
1250     if (!filename) {
1251         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1252         hmp_handle_error(mon, &err);
1253         return;
1254     }
1255     qmp_drive_mirror(&mirror, &err);
1256     hmp_handle_error(mon, &err);
1257 }
1258
1259 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1260 {
1261     const char *device = qdict_get_str(qdict, "device");
1262     const char *filename = qdict_get_str(qdict, "target");
1263     const char *format = qdict_get_try_str(qdict, "format");
1264     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1265     bool full = qdict_get_try_bool(qdict, "full", false);
1266     bool compress = qdict_get_try_bool(qdict, "compress", false);
1267     Error *err = NULL;
1268     DriveBackup backup = {
1269         .device = (char *)device,
1270         .target = (char *)filename,
1271         .has_format = !!format,
1272         .format = (char *)format,
1273         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1274         .has_mode = true,
1275         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1276         .has_compress = !!compress,
1277         .compress = compress,
1278     };
1279
1280     if (!filename) {
1281         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1282         hmp_handle_error(mon, &err);
1283         return;
1284     }
1285
1286     qmp_drive_backup(&backup, &err);
1287     hmp_handle_error(mon, &err);
1288 }
1289
1290 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1291 {
1292     const char *device = qdict_get_str(qdict, "device");
1293     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1294     const char *format = qdict_get_try_str(qdict, "format");
1295     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1296     enum NewImageMode mode;
1297     Error *err = NULL;
1298
1299     if (!filename) {
1300         /* In the future, if 'snapshot-file' is not specified, the snapshot
1301            will be taken internally. Today it's actually required. */
1302         error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1303         hmp_handle_error(mon, &err);
1304         return;
1305     }
1306
1307     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1308     qmp_blockdev_snapshot_sync(true, device, false, NULL,
1309                                filename, false, NULL,
1310                                !!format, format,
1311                                true, mode, &err);
1312     hmp_handle_error(mon, &err);
1313 }
1314
1315 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1316 {
1317     const char *device = qdict_get_str(qdict, "device");
1318     const char *name = qdict_get_str(qdict, "name");
1319     Error *err = NULL;
1320
1321     qmp_blockdev_snapshot_internal_sync(device, name, &err);
1322     hmp_handle_error(mon, &err);
1323 }
1324
1325 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1326 {
1327     const char *device = qdict_get_str(qdict, "device");
1328     const char *name = qdict_get_str(qdict, "name");
1329     const char *id = qdict_get_try_str(qdict, "id");
1330     Error *err = NULL;
1331
1332     qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1333                                                true, name, &err);
1334     hmp_handle_error(mon, &err);
1335 }
1336
1337 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1338 {
1339     int saved_vm_running  = runstate_is_running();
1340     const char *name = qdict_get_str(qdict, "name");
1341     Error *err = NULL;
1342
1343     vm_stop(RUN_STATE_RESTORE_VM);
1344
1345     if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1346         vm_start();
1347     }
1348     hmp_handle_error(mon, &err);
1349 }
1350
1351 void hmp_savevm(Monitor *mon, const QDict *qdict)
1352 {
1353     Error *err = NULL;
1354
1355     save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1356     hmp_handle_error(mon, &err);
1357 }
1358
1359 void hmp_delvm(Monitor *mon, const QDict *qdict)
1360 {
1361     BlockDriverState *bs;
1362     Error *err = NULL;
1363     const char *name = qdict_get_str(qdict, "name");
1364
1365     if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1366         error_reportf_err(err,
1367                           "Error while deleting snapshot on device '%s': ",
1368                           bdrv_get_device_name(bs));
1369     }
1370 }
1371
1372 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1373 {
1374     BlockDriverState *bs, *bs1;
1375     BdrvNextIterator it1;
1376     QEMUSnapshotInfo *sn_tab, *sn;
1377     bool no_snapshot = true;
1378     int nb_sns, i;
1379     int total;
1380     int *global_snapshots;
1381     AioContext *aio_context;
1382
1383     typedef struct SnapshotEntry {
1384         QEMUSnapshotInfo sn;
1385         QTAILQ_ENTRY(SnapshotEntry) next;
1386     } SnapshotEntry;
1387
1388     typedef struct ImageEntry {
1389         const char *imagename;
1390         QTAILQ_ENTRY(ImageEntry) next;
1391         QTAILQ_HEAD(, SnapshotEntry) snapshots;
1392     } ImageEntry;
1393
1394     QTAILQ_HEAD(, ImageEntry) image_list =
1395         QTAILQ_HEAD_INITIALIZER(image_list);
1396
1397     ImageEntry *image_entry, *next_ie;
1398     SnapshotEntry *snapshot_entry;
1399
1400     bs = bdrv_all_find_vmstate_bs();
1401     if (!bs) {
1402         monitor_printf(mon, "No available block device supports snapshots\n");
1403         return;
1404     }
1405     aio_context = bdrv_get_aio_context(bs);
1406
1407     aio_context_acquire(aio_context);
1408     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1409     aio_context_release(aio_context);
1410
1411     if (nb_sns < 0) {
1412         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1413         return;
1414     }
1415
1416     for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1417         int bs1_nb_sns = 0;
1418         ImageEntry *ie;
1419         SnapshotEntry *se;
1420         AioContext *ctx = bdrv_get_aio_context(bs1);
1421
1422         aio_context_acquire(ctx);
1423         if (bdrv_can_snapshot(bs1)) {
1424             sn = NULL;
1425             bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1426             if (bs1_nb_sns > 0) {
1427                 no_snapshot = false;
1428                 ie = g_new0(ImageEntry, 1);
1429                 ie->imagename = bdrv_get_device_name(bs1);
1430                 QTAILQ_INIT(&ie->snapshots);
1431                 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1432                 for (i = 0; i < bs1_nb_sns; i++) {
1433                     se = g_new0(SnapshotEntry, 1);
1434                     se->sn = sn[i];
1435                     QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1436                 }
1437             }
1438             g_free(sn);
1439         }
1440         aio_context_release(ctx);
1441     }
1442
1443     if (no_snapshot) {
1444         monitor_printf(mon, "There is no snapshot available.\n");
1445         return;
1446     }
1447
1448     global_snapshots = g_new0(int, nb_sns);
1449     total = 0;
1450     for (i = 0; i < nb_sns; i++) {
1451         SnapshotEntry *next_sn;
1452         if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1453             global_snapshots[total] = i;
1454             total++;
1455             QTAILQ_FOREACH(image_entry, &image_list, next) {
1456                 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1457                                     next, next_sn) {
1458                     if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1459                         QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1460                                       next);
1461                         g_free(snapshot_entry);
1462                     }
1463                 }
1464             }
1465         }
1466     }
1467
1468     monitor_printf(mon, "List of snapshots present on all disks:\n");
1469
1470     if (total > 0) {
1471         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1472         monitor_printf(mon, "\n");
1473         for (i = 0; i < total; i++) {
1474             sn = &sn_tab[global_snapshots[i]];
1475             /* The ID is not guaranteed to be the same on all images, so
1476              * overwrite it.
1477              */
1478             pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1479             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1480             monitor_printf(mon, "\n");
1481         }
1482     } else {
1483         monitor_printf(mon, "None\n");
1484     }
1485
1486     QTAILQ_FOREACH(image_entry, &image_list, next) {
1487         if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1488             continue;
1489         }
1490         monitor_printf(mon,
1491                        "\nList of partial (non-loadable) snapshots on '%s':\n",
1492                        image_entry->imagename);
1493         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1494         monitor_printf(mon, "\n");
1495         QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1496             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1497                                &snapshot_entry->sn);
1498             monitor_printf(mon, "\n");
1499         }
1500     }
1501
1502     QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1503         SnapshotEntry *next_sn;
1504         QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1505                             next_sn) {
1506             g_free(snapshot_entry);
1507         }
1508         g_free(image_entry);
1509     }
1510     g_free(sn_tab);
1511     g_free(global_snapshots);
1512
1513 }
1514
1515 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1516 {
1517     qmp_migrate_cancel(NULL);
1518 }
1519
1520 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
1521 {
1522     Error *err = NULL;
1523     const char *state = qdict_get_str(qdict, "state");
1524     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
1525
1526     if (val >= 0) {
1527         qmp_migrate_continue(val, &err);
1528     }
1529
1530     hmp_handle_error(mon, &err);
1531 }
1532
1533 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1534 {
1535     Error *err = NULL;
1536     const char *uri = qdict_get_str(qdict, "uri");
1537
1538     qmp_migrate_incoming(uri, &err);
1539
1540     hmp_handle_error(mon, &err);
1541 }
1542
1543 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
1544 {
1545     Error *err = NULL;
1546     const char *uri = qdict_get_str(qdict, "uri");
1547
1548     qmp_migrate_recover(uri, &err);
1549
1550     hmp_handle_error(mon, &err);
1551 }
1552
1553 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
1554 {
1555     Error *err = NULL;
1556
1557     qmp_migrate_pause(&err);
1558
1559     hmp_handle_error(mon, &err);
1560 }
1561
1562 /* Kept for backwards compatibility */
1563 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1564 {
1565     double value = qdict_get_double(qdict, "value");
1566     qmp_migrate_set_downtime(value, NULL);
1567 }
1568
1569 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1570 {
1571     int64_t value = qdict_get_int(qdict, "value");
1572     Error *err = NULL;
1573
1574     qmp_migrate_set_cache_size(value, &err);
1575     hmp_handle_error(mon, &err);
1576 }
1577
1578 /* Kept for backwards compatibility */
1579 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1580 {
1581     int64_t value = qdict_get_int(qdict, "value");
1582     qmp_migrate_set_speed(value, NULL);
1583 }
1584
1585 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1586 {
1587     const char *cap = qdict_get_str(qdict, "capability");
1588     bool state = qdict_get_bool(qdict, "state");
1589     Error *err = NULL;
1590     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1591     int val;
1592
1593     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
1594     if (val < 0) {
1595         goto end;
1596     }
1597
1598     caps->value = g_malloc0(sizeof(*caps->value));
1599     caps->value->capability = val;
1600     caps->value->state = state;
1601     caps->next = NULL;
1602     qmp_migrate_set_capabilities(caps, &err);
1603
1604 end:
1605     qapi_free_MigrationCapabilityStatusList(caps);
1606     hmp_handle_error(mon, &err);
1607 }
1608
1609 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1610 {
1611     const char *param = qdict_get_str(qdict, "parameter");
1612     const char *valuestr = qdict_get_str(qdict, "value");
1613     Visitor *v = string_input_visitor_new(valuestr);
1614     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
1615     uint64_t valuebw = 0;
1616     uint64_t cache_size;
1617     Error *err = NULL;
1618     int val, ret;
1619
1620     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
1621     if (val < 0) {
1622         goto cleanup;
1623     }
1624
1625     switch (val) {
1626     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1627         p->has_compress_level = true;
1628         visit_type_int(v, param, &p->compress_level, &err);
1629         break;
1630     case MIGRATION_PARAMETER_COMPRESS_THREADS:
1631         p->has_compress_threads = true;
1632         visit_type_int(v, param, &p->compress_threads, &err);
1633         break;
1634     case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
1635         p->has_compress_wait_thread = true;
1636         visit_type_bool(v, param, &p->compress_wait_thread, &err);
1637         break;
1638     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1639         p->has_decompress_threads = true;
1640         visit_type_int(v, param, &p->decompress_threads, &err);
1641         break;
1642     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1643         p->has_cpu_throttle_initial = true;
1644         visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1645         break;
1646     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1647         p->has_cpu_throttle_increment = true;
1648         visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1649         break;
1650     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
1651         p->has_max_cpu_throttle = true;
1652         visit_type_int(v, param, &p->max_cpu_throttle, &err);
1653         break;
1654     case MIGRATION_PARAMETER_TLS_CREDS:
1655         p->has_tls_creds = true;
1656         p->tls_creds = g_new0(StrOrNull, 1);
1657         p->tls_creds->type = QTYPE_QSTRING;
1658         visit_type_str(v, param, &p->tls_creds->u.s, &err);
1659         break;
1660     case MIGRATION_PARAMETER_TLS_HOSTNAME:
1661         p->has_tls_hostname = true;
1662         p->tls_hostname = g_new0(StrOrNull, 1);
1663         p->tls_hostname->type = QTYPE_QSTRING;
1664         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
1665         break;
1666     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1667         p->has_max_bandwidth = true;
1668         /*
1669          * Can't use visit_type_size() here, because it
1670          * defaults to Bytes rather than Mebibytes.
1671          */
1672         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1673         if (ret < 0 || valuebw > INT64_MAX
1674             || (size_t)valuebw != valuebw) {
1675             error_setg(&err, "Invalid size %s", valuestr);
1676             break;
1677         }
1678         p->max_bandwidth = valuebw;
1679         break;
1680     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1681         p->has_downtime_limit = true;
1682         visit_type_int(v, param, &p->downtime_limit, &err);
1683         break;
1684     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1685         p->has_x_checkpoint_delay = true;
1686         visit_type_int(v, param, &p->x_checkpoint_delay, &err);
1687         break;
1688     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1689         p->has_block_incremental = true;
1690         visit_type_bool(v, param, &p->block_incremental, &err);
1691         break;
1692     case MIGRATION_PARAMETER_X_MULTIFD_CHANNELS:
1693         p->has_x_multifd_channels = true;
1694         visit_type_int(v, param, &p->x_multifd_channels, &err);
1695         break;
1696     case MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT:
1697         p->has_x_multifd_page_count = true;
1698         visit_type_int(v, param, &p->x_multifd_page_count, &err);
1699         break;
1700     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
1701         p->has_xbzrle_cache_size = true;
1702         visit_type_size(v, param, &cache_size, &err);
1703         if (err || cache_size > INT64_MAX
1704             || (size_t)cache_size != cache_size) {
1705             error_setg(&err, "Invalid size %s", valuestr);
1706             break;
1707         }
1708         p->xbzrle_cache_size = cache_size;
1709         break;
1710     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
1711         p->has_max_postcopy_bandwidth = true;
1712         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
1713         break;
1714     default:
1715         assert(0);
1716     }
1717
1718     if (err) {
1719         goto cleanup;
1720     }
1721
1722     qmp_migrate_set_parameters(p, &err);
1723
1724  cleanup:
1725     qapi_free_MigrateSetParameters(p);
1726     visit_free(v);
1727     hmp_handle_error(mon, &err);
1728 }
1729
1730 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1731 {
1732     Error *err = NULL;
1733     const char *protocol = qdict_get_str(qdict, "protocol");
1734     const char *hostname = qdict_get_str(qdict, "hostname");
1735     bool has_port        = qdict_haskey(qdict, "port");
1736     int port             = qdict_get_try_int(qdict, "port", -1);
1737     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1738     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1739     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1740
1741     qmp_client_migrate_info(protocol, hostname,
1742                             has_port, port, has_tls_port, tls_port,
1743                             !!cert_subject, cert_subject, &err);
1744     hmp_handle_error(mon, &err);
1745 }
1746
1747 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1748 {
1749     Error *err = NULL;
1750     qmp_migrate_start_postcopy(&err);
1751     hmp_handle_error(mon, &err);
1752 }
1753
1754 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1755 {
1756     Error *err = NULL;
1757
1758     qmp_x_colo_lost_heartbeat(&err);
1759     hmp_handle_error(mon, &err);
1760 }
1761
1762 void hmp_set_password(Monitor *mon, const QDict *qdict)
1763 {
1764     const char *protocol  = qdict_get_str(qdict, "protocol");
1765     const char *password  = qdict_get_str(qdict, "password");
1766     const char *connected = qdict_get_try_str(qdict, "connected");
1767     Error *err = NULL;
1768
1769     qmp_set_password(protocol, password, !!connected, connected, &err);
1770     hmp_handle_error(mon, &err);
1771 }
1772
1773 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1774 {
1775     const char *protocol  = qdict_get_str(qdict, "protocol");
1776     const char *whenstr = qdict_get_str(qdict, "time");
1777     Error *err = NULL;
1778
1779     qmp_expire_password(protocol, whenstr, &err);
1780     hmp_handle_error(mon, &err);
1781 }
1782
1783 void hmp_eject(Monitor *mon, const QDict *qdict)
1784 {
1785     bool force = qdict_get_try_bool(qdict, "force", false);
1786     const char *device = qdict_get_str(qdict, "device");
1787     Error *err = NULL;
1788
1789     qmp_eject(true, device, false, NULL, true, force, &err);
1790     hmp_handle_error(mon, &err);
1791 }
1792
1793 #ifdef CONFIG_VNC
1794 static void hmp_change_read_arg(void *opaque, const char *password,
1795                                 void *readline_opaque)
1796 {
1797     qmp_change_vnc_password(password, NULL);
1798     monitor_read_command(opaque, 1);
1799 }
1800 #endif
1801
1802 void hmp_change(Monitor *mon, const QDict *qdict)
1803 {
1804     const char *device = qdict_get_str(qdict, "device");
1805     const char *target = qdict_get_str(qdict, "target");
1806     const char *arg = qdict_get_try_str(qdict, "arg");
1807     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1808     BlockdevChangeReadOnlyMode read_only_mode = 0;
1809     Error *err = NULL;
1810
1811 #ifdef CONFIG_VNC
1812     if (strcmp(device, "vnc") == 0) {
1813         if (read_only) {
1814             monitor_printf(mon,
1815                            "Parameter 'read-only-mode' is invalid for VNC\n");
1816             return;
1817         }
1818         if (strcmp(target, "passwd") == 0 ||
1819             strcmp(target, "password") == 0) {
1820             if (!arg) {
1821                 monitor_read_password(mon, hmp_change_read_arg, NULL);
1822                 return;
1823             }
1824         }
1825         qmp_change("vnc", target, !!arg, arg, &err);
1826     } else
1827 #endif
1828     {
1829         if (read_only) {
1830             read_only_mode =
1831                 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1832                                 read_only,
1833                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1834             if (err) {
1835                 hmp_handle_error(mon, &err);
1836                 return;
1837             }
1838         }
1839
1840         qmp_blockdev_change_medium(true, device, false, NULL, target,
1841                                    !!arg, arg, !!read_only, read_only_mode,
1842                                    &err);
1843     }
1844
1845     hmp_handle_error(mon, &err);
1846 }
1847
1848 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1849 {
1850     Error *err = NULL;
1851     char *device = (char *) qdict_get_str(qdict, "device");
1852     BlockIOThrottle throttle = {
1853         .bps = qdict_get_int(qdict, "bps"),
1854         .bps_rd = qdict_get_int(qdict, "bps_rd"),
1855         .bps_wr = qdict_get_int(qdict, "bps_wr"),
1856         .iops = qdict_get_int(qdict, "iops"),
1857         .iops_rd = qdict_get_int(qdict, "iops_rd"),
1858         .iops_wr = qdict_get_int(qdict, "iops_wr"),
1859     };
1860
1861     /* qmp_block_set_io_throttle has separate parameters for the
1862      * (deprecated) block device name and the qdev ID but the HMP
1863      * version has only one, so we must decide which one to pass. */
1864     if (blk_by_name(device)) {
1865         throttle.has_device = true;
1866         throttle.device = device;
1867     } else {
1868         throttle.has_id = true;
1869         throttle.id = device;
1870     }
1871
1872     qmp_block_set_io_throttle(&throttle, &err);
1873     hmp_handle_error(mon, &err);
1874 }
1875
1876 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1877 {
1878     Error *error = NULL;
1879     const char *device = qdict_get_str(qdict, "device");
1880     const char *base = qdict_get_try_str(qdict, "base");
1881     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1882
1883     qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
1884                      false, NULL, qdict_haskey(qdict, "speed"), speed,
1885                      true, BLOCKDEV_ON_ERROR_REPORT, &error);
1886
1887     hmp_handle_error(mon, &error);
1888 }
1889
1890 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1891 {
1892     Error *error = NULL;
1893     const char *device = qdict_get_str(qdict, "device");
1894     int64_t value = qdict_get_int(qdict, "speed");
1895
1896     qmp_block_job_set_speed(device, value, &error);
1897
1898     hmp_handle_error(mon, &error);
1899 }
1900
1901 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1902 {
1903     Error *error = NULL;
1904     const char *device = qdict_get_str(qdict, "device");
1905     bool force = qdict_get_try_bool(qdict, "force", false);
1906
1907     qmp_block_job_cancel(device, true, force, &error);
1908
1909     hmp_handle_error(mon, &error);
1910 }
1911
1912 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1913 {
1914     Error *error = NULL;
1915     const char *device = qdict_get_str(qdict, "device");
1916
1917     qmp_block_job_pause(device, &error);
1918
1919     hmp_handle_error(mon, &error);
1920 }
1921
1922 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1923 {
1924     Error *error = NULL;
1925     const char *device = qdict_get_str(qdict, "device");
1926
1927     qmp_block_job_resume(device, &error);
1928
1929     hmp_handle_error(mon, &error);
1930 }
1931
1932 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1933 {
1934     Error *error = NULL;
1935     const char *device = qdict_get_str(qdict, "device");
1936
1937     qmp_block_job_complete(device, &error);
1938
1939     hmp_handle_error(mon, &error);
1940 }
1941
1942 typedef struct HMPMigrationStatus
1943 {
1944     QEMUTimer *timer;
1945     Monitor *mon;
1946     bool is_block_migration;
1947 } HMPMigrationStatus;
1948
1949 static void hmp_migrate_status_cb(void *opaque)
1950 {
1951     HMPMigrationStatus *status = opaque;
1952     MigrationInfo *info;
1953
1954     info = qmp_query_migrate(NULL);
1955     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1956         info->status == MIGRATION_STATUS_SETUP) {
1957         if (info->has_disk) {
1958             int progress;
1959
1960             if (info->disk->remaining) {
1961                 progress = info->disk->transferred * 100 / info->disk->total;
1962             } else {
1963                 progress = 100;
1964             }
1965
1966             monitor_printf(status->mon, "Completed %d %%\r", progress);
1967             monitor_flush(status->mon);
1968         }
1969
1970         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1971     } else {
1972         if (status->is_block_migration) {
1973             monitor_printf(status->mon, "\n");
1974         }
1975         if (info->has_error_desc) {
1976             error_report("%s", info->error_desc);
1977         }
1978         monitor_resume(status->mon);
1979         timer_del(status->timer);
1980         g_free(status);
1981     }
1982
1983     qapi_free_MigrationInfo(info);
1984 }
1985
1986 void hmp_migrate(Monitor *mon, const QDict *qdict)
1987 {
1988     bool detach = qdict_get_try_bool(qdict, "detach", false);
1989     bool blk = qdict_get_try_bool(qdict, "blk", false);
1990     bool inc = qdict_get_try_bool(qdict, "inc", false);
1991     bool resume = qdict_get_try_bool(qdict, "resume", false);
1992     const char *uri = qdict_get_str(qdict, "uri");
1993     Error *err = NULL;
1994
1995     qmp_migrate(uri, !!blk, blk, !!inc, inc,
1996                 false, false, true, resume, &err);
1997     if (err) {
1998         hmp_handle_error(mon, &err);
1999         return;
2000     }
2001
2002     if (!detach) {
2003         HMPMigrationStatus *status;
2004
2005         if (monitor_suspend(mon) < 0) {
2006             monitor_printf(mon, "terminal does not allow synchronous "
2007                            "migration, continuing detached\n");
2008             return;
2009         }
2010
2011         status = g_malloc0(sizeof(*status));
2012         status->mon = mon;
2013         status->is_block_migration = blk || inc;
2014         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
2015                                           status);
2016         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
2017     }
2018 }
2019
2020 void hmp_device_add(Monitor *mon, const QDict *qdict)
2021 {
2022     Error *err = NULL;
2023
2024     qmp_device_add((QDict *)qdict, NULL, &err);
2025     hmp_handle_error(mon, &err);
2026 }
2027
2028 void hmp_device_del(Monitor *mon, const QDict *qdict)
2029 {
2030     const char *id = qdict_get_str(qdict, "id");
2031     Error *err = NULL;
2032
2033     qmp_device_del(id, &err);
2034     hmp_handle_error(mon, &err);
2035 }
2036
2037 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
2038 {
2039     Error *err = NULL;
2040     bool win_dmp = qdict_get_try_bool(qdict, "windmp", false);
2041     bool paging = qdict_get_try_bool(qdict, "paging", false);
2042     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
2043     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
2044     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
2045     const char *file = qdict_get_str(qdict, "filename");
2046     bool has_begin = qdict_haskey(qdict, "begin");
2047     bool has_length = qdict_haskey(qdict, "length");
2048     bool has_detach = qdict_haskey(qdict, "detach");
2049     int64_t begin = 0;
2050     int64_t length = 0;
2051     bool detach = false;
2052     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
2053     char *prot;
2054
2055     if (zlib + lzo + snappy + win_dmp > 1) {
2056         error_setg(&err, "only one of '-z|-l|-s|-w' can be set");
2057         hmp_handle_error(mon, &err);
2058         return;
2059     }
2060
2061     if (win_dmp) {
2062         dump_format = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
2063     }
2064
2065     if (zlib) {
2066         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2067     }
2068
2069     if (lzo) {
2070         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2071     }
2072
2073     if (snappy) {
2074         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2075     }
2076
2077     if (has_begin) {
2078         begin = qdict_get_int(qdict, "begin");
2079     }
2080     if (has_length) {
2081         length = qdict_get_int(qdict, "length");
2082     }
2083     if (has_detach) {
2084         detach = qdict_get_bool(qdict, "detach");
2085     }
2086
2087     prot = g_strconcat("file:", file, NULL);
2088
2089     qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
2090                           has_length, length, true, dump_format, &err);
2091     hmp_handle_error(mon, &err);
2092     g_free(prot);
2093 }
2094
2095 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
2096 {
2097     Error *err = NULL;
2098     QemuOpts *opts;
2099
2100     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
2101     if (err) {
2102         goto out;
2103     }
2104
2105     netdev_add(opts, &err);
2106     if (err) {
2107         qemu_opts_del(opts);
2108     }
2109
2110 out:
2111     hmp_handle_error(mon, &err);
2112 }
2113
2114 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2115 {
2116     const char *id = qdict_get_str(qdict, "id");
2117     Error *err = NULL;
2118
2119     qmp_netdev_del(id, &err);
2120     hmp_handle_error(mon, &err);
2121 }
2122
2123 void hmp_object_add(Monitor *mon, const QDict *qdict)
2124 {
2125     Error *err = NULL;
2126     QemuOpts *opts;
2127     Object *obj = NULL;
2128
2129     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2130     if (err) {
2131         hmp_handle_error(mon, &err);
2132         return;
2133     }
2134
2135     obj = user_creatable_add_opts(opts, &err);
2136     qemu_opts_del(opts);
2137
2138     if (err) {
2139         hmp_handle_error(mon, &err);
2140     }
2141     if (obj) {
2142         object_unref(obj);
2143     }
2144 }
2145
2146 void hmp_getfd(Monitor *mon, const QDict *qdict)
2147 {
2148     const char *fdname = qdict_get_str(qdict, "fdname");
2149     Error *err = NULL;
2150
2151     qmp_getfd(fdname, &err);
2152     hmp_handle_error(mon, &err);
2153 }
2154
2155 void hmp_closefd(Monitor *mon, const QDict *qdict)
2156 {
2157     const char *fdname = qdict_get_str(qdict, "fdname");
2158     Error *err = NULL;
2159
2160     qmp_closefd(fdname, &err);
2161     hmp_handle_error(mon, &err);
2162 }
2163
2164 void hmp_sendkey(Monitor *mon, const QDict *qdict)
2165 {
2166     const char *keys = qdict_get_str(qdict, "keys");
2167     KeyValueList *keylist, *head = NULL, *tmp = NULL;
2168     int has_hold_time = qdict_haskey(qdict, "hold-time");
2169     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2170     Error *err = NULL;
2171     const char *separator;
2172     int keyname_len;
2173
2174     while (1) {
2175         separator = qemu_strchrnul(keys, '-');
2176         keyname_len = separator - keys;
2177
2178         /* Be compatible with old interface, convert user inputted "<" */
2179         if (keys[0] == '<' && keyname_len == 1) {
2180             keys = "less";
2181             keyname_len = 4;
2182         }
2183
2184         keylist = g_malloc0(sizeof(*keylist));
2185         keylist->value = g_malloc0(sizeof(*keylist->value));
2186
2187         if (!head) {
2188             head = keylist;
2189         }
2190         if (tmp) {
2191             tmp->next = keylist;
2192         }
2193         tmp = keylist;
2194
2195         if (strstart(keys, "0x", NULL)) {
2196             char *endp;
2197             int value = strtoul(keys, &endp, 0);
2198             assert(endp <= keys + keyname_len);
2199             if (endp != keys + keyname_len) {
2200                 goto err_out;
2201             }
2202             keylist->value->type = KEY_VALUE_KIND_NUMBER;
2203             keylist->value->u.number.data = value;
2204         } else {
2205             int idx = index_from_key(keys, keyname_len);
2206             if (idx == Q_KEY_CODE__MAX) {
2207                 goto err_out;
2208             }
2209             keylist->value->type = KEY_VALUE_KIND_QCODE;
2210             keylist->value->u.qcode.data = idx;
2211         }
2212
2213         if (!*separator) {
2214             break;
2215         }
2216         keys = separator + 1;
2217     }
2218
2219     qmp_send_key(head, has_hold_time, hold_time, &err);
2220     hmp_handle_error(mon, &err);
2221
2222 out:
2223     qapi_free_KeyValueList(head);
2224     return;
2225
2226 err_out:
2227     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
2228     goto out;
2229 }
2230
2231 void hmp_screendump(Monitor *mon, const QDict *qdict)
2232 {
2233     const char *filename = qdict_get_str(qdict, "filename");
2234     const char *id = qdict_get_try_str(qdict, "device");
2235     int64_t head = qdict_get_try_int(qdict, "head", 0);
2236     Error *err = NULL;
2237
2238     qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
2239     hmp_handle_error(mon, &err);
2240 }
2241
2242 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2243 {
2244     const char *uri = qdict_get_str(qdict, "uri");
2245     bool writable = qdict_get_try_bool(qdict, "writable", false);
2246     bool all = qdict_get_try_bool(qdict, "all", false);
2247     Error *local_err = NULL;
2248     BlockInfoList *block_list, *info;
2249     SocketAddress *addr;
2250
2251     if (writable && !all) {
2252         error_setg(&local_err, "-w only valid together with -a");
2253         goto exit;
2254     }
2255
2256     /* First check if the address is valid and start the server.  */
2257     addr = socket_parse(uri, &local_err);
2258     if (local_err != NULL) {
2259         goto exit;
2260     }
2261
2262     nbd_server_start(addr, NULL, &local_err);
2263     qapi_free_SocketAddress(addr);
2264     if (local_err != NULL) {
2265         goto exit;
2266     }
2267
2268     if (!all) {
2269         return;
2270     }
2271
2272     /* Then try adding all block devices.  If one fails, close all and
2273      * exit.
2274      */
2275     block_list = qmp_query_block(NULL);
2276
2277     for (info = block_list; info; info = info->next) {
2278         if (!info->value->has_inserted) {
2279             continue;
2280         }
2281
2282         qmp_nbd_server_add(info->value->device, false, NULL,
2283                            true, writable, &local_err);
2284
2285         if (local_err != NULL) {
2286             qmp_nbd_server_stop(NULL);
2287             break;
2288         }
2289     }
2290
2291     qapi_free_BlockInfoList(block_list);
2292
2293 exit:
2294     hmp_handle_error(mon, &local_err);
2295 }
2296
2297 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2298 {
2299     const char *device = qdict_get_str(qdict, "device");
2300     const char *name = qdict_get_try_str(qdict, "name");
2301     bool writable = qdict_get_try_bool(qdict, "writable", false);
2302     Error *local_err = NULL;
2303
2304     qmp_nbd_server_add(device, !!name, name, true, writable, &local_err);
2305     hmp_handle_error(mon, &local_err);
2306 }
2307
2308 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
2309 {
2310     const char *name = qdict_get_str(qdict, "name");
2311     bool force = qdict_get_try_bool(qdict, "force", false);
2312     Error *err = NULL;
2313
2314     /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
2315     qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
2316     hmp_handle_error(mon, &err);
2317 }
2318
2319 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2320 {
2321     Error *err = NULL;
2322
2323     qmp_nbd_server_stop(&err);
2324     hmp_handle_error(mon, &err);
2325 }
2326
2327 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2328 {
2329     int cpuid;
2330     Error *err = NULL;
2331
2332     cpuid = qdict_get_int(qdict, "id");
2333     qmp_cpu_add(cpuid, &err);
2334     hmp_handle_error(mon, &err);
2335 }
2336
2337 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2338 {
2339     const char *args = qdict_get_str(qdict, "args");
2340     Error *err = NULL;
2341     QemuOpts *opts;
2342
2343     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2344     if (opts == NULL) {
2345         error_setg(&err, "Parsing chardev args failed");
2346     } else {
2347         qemu_chr_new_from_opts(opts, &err);
2348         qemu_opts_del(opts);
2349     }
2350     hmp_handle_error(mon, &err);
2351 }
2352
2353 void hmp_chardev_change(Monitor *mon, const QDict *qdict)
2354 {
2355     const char *args = qdict_get_str(qdict, "args");
2356     const char *id;
2357     Error *err = NULL;
2358     ChardevBackend *backend = NULL;
2359     ChardevReturn *ret = NULL;
2360     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
2361                                              true);
2362     if (!opts) {
2363         error_setg(&err, "Parsing chardev args failed");
2364         goto end;
2365     }
2366
2367     id = qdict_get_str(qdict, "id");
2368     if (qemu_opts_id(opts)) {
2369         error_setg(&err, "Unexpected 'id' parameter");
2370         goto end;
2371     }
2372
2373     backend = qemu_chr_parse_opts(opts, &err);
2374     if (!backend) {
2375         goto end;
2376     }
2377
2378     ret = qmp_chardev_change(id, backend, &err);
2379
2380 end:
2381     qapi_free_ChardevReturn(ret);
2382     qapi_free_ChardevBackend(backend);
2383     qemu_opts_del(opts);
2384     hmp_handle_error(mon, &err);
2385 }
2386
2387 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2388 {
2389     Error *local_err = NULL;
2390
2391     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2392     hmp_handle_error(mon, &local_err);
2393 }
2394
2395 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2396 {
2397     Error *local_err = NULL;
2398
2399     qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2400     hmp_handle_error(mon, &local_err);
2401 }
2402
2403 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2404 {
2405     BlockBackend *blk;
2406     BlockBackend *local_blk = NULL;
2407     const char* device = qdict_get_str(qdict, "device");
2408     const char* command = qdict_get_str(qdict, "command");
2409     Error *err = NULL;
2410     int ret;
2411
2412     blk = blk_by_name(device);
2413     if (!blk) {
2414         BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2415         if (bs) {
2416             blk = local_blk = blk_new(0, BLK_PERM_ALL);
2417             ret = blk_insert_bs(blk, bs, &err);
2418             if (ret < 0) {
2419                 goto fail;
2420             }
2421         } else {
2422             goto fail;
2423         }
2424     }
2425
2426     /*
2427      * Notably absent: Proper permission management. This is sad, but it seems
2428      * almost impossible to achieve without changing the semantics and thereby
2429      * limiting the use cases of the qemu-io HMP command.
2430      *
2431      * In an ideal world we would unconditionally create a new BlockBackend for
2432      * qemuio_command(), but we have commands like 'reopen' and want them to
2433      * take effect on the exact BlockBackend whose name the user passed instead
2434      * of just on a temporary copy of it.
2435      *
2436      * Another problem is that deleting the temporary BlockBackend involves
2437      * draining all requests on it first, but some qemu-iotests cases want to
2438      * issue multiple aio_read/write requests and expect them to complete in
2439      * the background while the monitor has already returned.
2440      *
2441      * This is also what prevents us from saving the original permissions and
2442      * restoring them later: We can't revoke permissions until all requests
2443      * have completed, and we don't know when that is nor can we really let
2444      * anything else run before we have revoken them to avoid race conditions.
2445      *
2446      * What happens now is that command() in qemu-io-cmds.c can extend the
2447      * permissions if necessary for the qemu-io command. And they simply stay
2448      * extended, possibly resulting in a read-only guest device keeping write
2449      * permissions. Ugly, but it appears to be the lesser evil.
2450      */
2451     qemuio_command(blk, command);
2452
2453 fail:
2454     blk_unref(local_blk);
2455     hmp_handle_error(mon, &err);
2456 }
2457
2458 void hmp_object_del(Monitor *mon, const QDict *qdict)
2459 {
2460     const char *id = qdict_get_str(qdict, "id");
2461     Error *err = NULL;
2462
2463     user_creatable_del(id, &err);
2464     hmp_handle_error(mon, &err);
2465 }
2466
2467 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2468 {
2469     Error *err = NULL;
2470     MemdevList *memdev_list = qmp_query_memdev(&err);
2471     MemdevList *m = memdev_list;
2472     Visitor *v;
2473     char *str;
2474
2475     while (m) {
2476         v = string_output_visitor_new(false, &str);
2477         visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2478         monitor_printf(mon, "memory backend: %s\n", m->value->id);
2479         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
2480         monitor_printf(mon, "  merge: %s\n",
2481                        m->value->merge ? "true" : "false");
2482         monitor_printf(mon, "  dump: %s\n",
2483                        m->value->dump ? "true" : "false");
2484         monitor_printf(mon, "  prealloc: %s\n",
2485                        m->value->prealloc ? "true" : "false");
2486         monitor_printf(mon, "  policy: %s\n",
2487                        HostMemPolicy_str(m->value->policy));
2488         visit_complete(v, &str);
2489         monitor_printf(mon, "  host nodes: %s\n", str);
2490
2491         g_free(str);
2492         visit_free(v);
2493         m = m->next;
2494     }
2495
2496     monitor_printf(mon, "\n");
2497
2498     qapi_free_MemdevList(memdev_list);
2499     hmp_handle_error(mon, &err);
2500 }
2501
2502 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2503 {
2504     Error *err = NULL;
2505     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2506     MemoryDeviceInfoList *info;
2507     MemoryDeviceInfo *value;
2508     PCDIMMDeviceInfo *di;
2509
2510     for (info = info_list; info; info = info->next) {
2511         value = info->value;
2512
2513         if (value) {
2514             switch (value->type) {
2515             case MEMORY_DEVICE_INFO_KIND_DIMM:
2516                 di = value->u.dimm.data;
2517                 break;
2518
2519             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
2520                 di = value->u.nvdimm.data;
2521                 break;
2522
2523             default:
2524                 di = NULL;
2525                 break;
2526             }
2527
2528             if (di) {
2529                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2530                                MemoryDeviceInfoKind_str(value->type),
2531                                di->id ? di->id : "");
2532                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
2533                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
2534                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
2535                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
2536                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
2537                 monitor_printf(mon, "  hotplugged: %s\n",
2538                                di->hotplugged ? "true" : "false");
2539                 monitor_printf(mon, "  hotpluggable: %s\n",
2540                                di->hotpluggable ? "true" : "false");
2541             }
2542         }
2543     }
2544
2545     qapi_free_MemoryDeviceInfoList(info_list);
2546     hmp_handle_error(mon, &err);
2547 }
2548
2549 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2550 {
2551     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2552     IOThreadInfoList *info;
2553     IOThreadInfo *value;
2554
2555     for (info = info_list; info; info = info->next) {
2556         value = info->value;
2557         monitor_printf(mon, "%s:\n", value->id);
2558         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
2559         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2560         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
2561         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
2562     }
2563
2564     qapi_free_IOThreadInfoList(info_list);
2565 }
2566
2567 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2568 {
2569     const char *path = qdict_get_try_str(qdict, "path");
2570     ObjectPropertyInfoList *list;
2571     Error *err = NULL;
2572
2573     if (path == NULL) {
2574         monitor_printf(mon, "/\n");
2575         return;
2576     }
2577
2578     list = qmp_qom_list(path, &err);
2579     if (err == NULL) {
2580         ObjectPropertyInfoList *start = list;
2581         while (list != NULL) {
2582             ObjectPropertyInfo *value = list->value;
2583
2584             monitor_printf(mon, "%s (%s)\n",
2585                            value->name, value->type);
2586             list = list->next;
2587         }
2588         qapi_free_ObjectPropertyInfoList(start);
2589     }
2590     hmp_handle_error(mon, &err);
2591 }
2592
2593 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2594 {
2595     const char *path = qdict_get_str(qdict, "path");
2596     const char *property = qdict_get_str(qdict, "property");
2597     const char *value = qdict_get_str(qdict, "value");
2598     Error *err = NULL;
2599     bool ambiguous = false;
2600     Object *obj;
2601
2602     obj = object_resolve_path(path, &ambiguous);
2603     if (obj == NULL) {
2604         error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2605                   "Device '%s' not found", path);
2606     } else {
2607         if (ambiguous) {
2608             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2609         }
2610         object_property_parse(obj, value, property, &err);
2611     }
2612     hmp_handle_error(mon, &err);
2613 }
2614
2615 void hmp_rocker(Monitor *mon, const QDict *qdict)
2616 {
2617     const char *name = qdict_get_str(qdict, "name");
2618     RockerSwitch *rocker;
2619     Error *err = NULL;
2620
2621     rocker = qmp_query_rocker(name, &err);
2622     if (err != NULL) {
2623         hmp_handle_error(mon, &err);
2624         return;
2625     }
2626
2627     monitor_printf(mon, "name: %s\n", rocker->name);
2628     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2629     monitor_printf(mon, "ports: %d\n", rocker->ports);
2630
2631     qapi_free_RockerSwitch(rocker);
2632 }
2633
2634 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2635 {
2636     RockerPortList *list, *port;
2637     const char *name = qdict_get_str(qdict, "name");
2638     Error *err = NULL;
2639
2640     list = qmp_query_rocker_ports(name, &err);
2641     if (err != NULL) {
2642         hmp_handle_error(mon, &err);
2643         return;
2644     }
2645
2646     monitor_printf(mon, "            ena/    speed/ auto\n");
2647     monitor_printf(mon, "      port  link    duplex neg?\n");
2648
2649     for (port = list; port; port = port->next) {
2650         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
2651                        port->value->name,
2652                        port->value->enabled ? port->value->link_up ?
2653                        "up" : "down" : "!ena",
2654                        port->value->speed == 10000 ? "10G" : "??",
2655                        port->value->duplex ? "FD" : "HD",
2656                        port->value->autoneg ? "Yes" : "No");
2657     }
2658
2659     qapi_free_RockerPortList(list);
2660 }
2661
2662 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2663 {
2664     RockerOfDpaFlowList *list, *info;
2665     const char *name = qdict_get_str(qdict, "name");
2666     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2667     Error *err = NULL;
2668
2669     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2670     if (err != NULL) {
2671         hmp_handle_error(mon, &err);
2672         return;
2673     }
2674
2675     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2676
2677     for (info = list; info; info = info->next) {
2678         RockerOfDpaFlow *flow = info->value;
2679         RockerOfDpaFlowKey *key = flow->key;
2680         RockerOfDpaFlowMask *mask = flow->mask;
2681         RockerOfDpaFlowAction *action = flow->action;
2682
2683         if (flow->hits) {
2684             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2685                            key->priority, key->tbl_id, flow->hits);
2686         } else {
2687             monitor_printf(mon, "%-4d %-3d     ",
2688                            key->priority, key->tbl_id);
2689         }
2690
2691         if (key->has_in_pport) {
2692             monitor_printf(mon, " pport %d", key->in_pport);
2693             if (mask->has_in_pport) {
2694                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2695             }
2696         }
2697
2698         if (key->has_vlan_id) {
2699             monitor_printf(mon, " vlan %d",
2700                            key->vlan_id & VLAN_VID_MASK);
2701             if (mask->has_vlan_id) {
2702                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2703             }
2704         }
2705
2706         if (key->has_tunnel_id) {
2707             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2708             if (mask->has_tunnel_id) {
2709                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2710             }
2711         }
2712
2713         if (key->has_eth_type) {
2714             switch (key->eth_type) {
2715             case 0x0806:
2716                 monitor_printf(mon, " ARP");
2717                 break;
2718             case 0x0800:
2719                 monitor_printf(mon, " IP");
2720                 break;
2721             case 0x86dd:
2722                 monitor_printf(mon, " IPv6");
2723                 break;
2724             case 0x8809:
2725                 monitor_printf(mon, " LACP");
2726                 break;
2727             case 0x88cc:
2728                 monitor_printf(mon, " LLDP");
2729                 break;
2730             default:
2731                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2732                 break;
2733             }
2734         }
2735
2736         if (key->has_eth_src) {
2737             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2738                 (mask->has_eth_src) &&
2739                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2740                 monitor_printf(mon, " src <any mcast/bcast>");
2741             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2742                 (mask->has_eth_src) &&
2743                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2744                 monitor_printf(mon, " src <any ucast>");
2745             } else {
2746                 monitor_printf(mon, " src %s", key->eth_src);
2747                 if (mask->has_eth_src) {
2748                     monitor_printf(mon, "(%s)", mask->eth_src);
2749                 }
2750             }
2751         }
2752
2753         if (key->has_eth_dst) {
2754             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2755                 (mask->has_eth_dst) &&
2756                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2757                 monitor_printf(mon, " dst <any mcast/bcast>");
2758             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2759                 (mask->has_eth_dst) &&
2760                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2761                 monitor_printf(mon, " dst <any ucast>");
2762             } else {
2763                 monitor_printf(mon, " dst %s", key->eth_dst);
2764                 if (mask->has_eth_dst) {
2765                     monitor_printf(mon, "(%s)", mask->eth_dst);
2766                 }
2767             }
2768         }
2769
2770         if (key->has_ip_proto) {
2771             monitor_printf(mon, " proto %d", key->ip_proto);
2772             if (mask->has_ip_proto) {
2773                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2774             }
2775         }
2776
2777         if (key->has_ip_tos) {
2778             monitor_printf(mon, " TOS %d", key->ip_tos);
2779             if (mask->has_ip_tos) {
2780                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2781             }
2782         }
2783
2784         if (key->has_ip_dst) {
2785             monitor_printf(mon, " dst %s", key->ip_dst);
2786         }
2787
2788         if (action->has_goto_tbl || action->has_group_id ||
2789             action->has_new_vlan_id) {
2790             monitor_printf(mon, " -->");
2791         }
2792
2793         if (action->has_new_vlan_id) {
2794             monitor_printf(mon, " apply new vlan %d",
2795                            ntohs(action->new_vlan_id));
2796         }
2797
2798         if (action->has_group_id) {
2799             monitor_printf(mon, " write group 0x%08x", action->group_id);
2800         }
2801
2802         if (action->has_goto_tbl) {
2803             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2804         }
2805
2806         monitor_printf(mon, "\n");
2807     }
2808
2809     qapi_free_RockerOfDpaFlowList(list);
2810 }
2811
2812 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2813 {
2814     RockerOfDpaGroupList *list, *g;
2815     const char *name = qdict_get_str(qdict, "name");
2816     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2817     Error *err = NULL;
2818     bool set = false;
2819
2820     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2821     if (err != NULL) {
2822         hmp_handle_error(mon, &err);
2823         return;
2824     }
2825
2826     monitor_printf(mon, "id (decode) --> buckets\n");
2827
2828     for (g = list; g; g = g->next) {
2829         RockerOfDpaGroup *group = g->value;
2830
2831         monitor_printf(mon, "0x%08x", group->id);
2832
2833         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2834                                          group->type == 1 ? "L2 rewrite" :
2835                                          group->type == 2 ? "L3 unicast" :
2836                                          group->type == 3 ? "L2 multicast" :
2837                                          group->type == 4 ? "L2 flood" :
2838                                          group->type == 5 ? "L3 interface" :
2839                                          group->type == 6 ? "L3 multicast" :
2840                                          group->type == 7 ? "L3 ECMP" :
2841                                          group->type == 8 ? "L2 overlay" :
2842                                          "unknown");
2843
2844         if (group->has_vlan_id) {
2845             monitor_printf(mon, " vlan %d", group->vlan_id);
2846         }
2847
2848         if (group->has_pport) {
2849             monitor_printf(mon, " pport %d", group->pport);
2850         }
2851
2852         if (group->has_index) {
2853             monitor_printf(mon, " index %d", group->index);
2854         }
2855
2856         monitor_printf(mon, ") -->");
2857
2858         if (group->has_set_vlan_id && group->set_vlan_id) {
2859             set = true;
2860             monitor_printf(mon, " set vlan %d",
2861                            group->set_vlan_id & VLAN_VID_MASK);
2862         }
2863
2864         if (group->has_set_eth_src) {
2865             if (!set) {
2866                 set = true;
2867                 monitor_printf(mon, " set");
2868             }
2869             monitor_printf(mon, " src %s", group->set_eth_src);
2870         }
2871
2872         if (group->has_set_eth_dst) {
2873             if (!set) {
2874                 set = true;
2875                 monitor_printf(mon, " set");
2876             }
2877             monitor_printf(mon, " dst %s", group->set_eth_dst);
2878         }
2879
2880         set = false;
2881
2882         if (group->has_ttl_check && group->ttl_check) {
2883             monitor_printf(mon, " check TTL");
2884         }
2885
2886         if (group->has_group_id && group->group_id) {
2887             monitor_printf(mon, " group id 0x%08x", group->group_id);
2888         }
2889
2890         if (group->has_pop_vlan && group->pop_vlan) {
2891             monitor_printf(mon, " pop vlan");
2892         }
2893
2894         if (group->has_out_pport) {
2895             monitor_printf(mon, " out pport %d", group->out_pport);
2896         }
2897
2898         if (group->has_group_ids) {
2899             struct uint32List *id;
2900
2901             monitor_printf(mon, " groups [");
2902             for (id = group->group_ids; id; id = id->next) {
2903                 monitor_printf(mon, "0x%08x", id->value);
2904                 if (id->next) {
2905                     monitor_printf(mon, ",");
2906                 }
2907             }
2908             monitor_printf(mon, "]");
2909         }
2910
2911         monitor_printf(mon, "\n");
2912     }
2913
2914     qapi_free_RockerOfDpaGroupList(list);
2915 }
2916
2917 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2918 {
2919     DumpQueryResult *result = qmp_query_dump(NULL);
2920
2921     assert(result && result->status < DUMP_STATUS__MAX);
2922     monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
2923
2924     if (result->status == DUMP_STATUS_ACTIVE) {
2925         float percent = 0;
2926         assert(result->total != 0);
2927         percent = 100.0 * result->completed / result->total;
2928         monitor_printf(mon, "Finished: %.2f %%\n", percent);
2929     }
2930
2931     qapi_free_DumpQueryResult(result);
2932 }
2933
2934 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2935 {
2936     ram_block_dump(mon);
2937 }
2938
2939 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2940 {
2941     Error *err = NULL;
2942     HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2943     HotpluggableCPUList *saved = l;
2944     CpuInstanceProperties *c;
2945
2946     if (err != NULL) {
2947         hmp_handle_error(mon, &err);
2948         return;
2949     }
2950
2951     monitor_printf(mon, "Hotpluggable CPUs:\n");
2952     while (l) {
2953         monitor_printf(mon, "  type: \"%s\"\n", l->value->type);
2954         monitor_printf(mon, "  vcpus_count: \"%" PRIu64 "\"\n",
2955                        l->value->vcpus_count);
2956         if (l->value->has_qom_path) {
2957             monitor_printf(mon, "  qom_path: \"%s\"\n", l->value->qom_path);
2958         }
2959
2960         c = l->value->props;
2961         monitor_printf(mon, "  CPUInstance Properties:\n");
2962         if (c->has_node_id) {
2963             monitor_printf(mon, "    node-id: \"%" PRIu64 "\"\n", c->node_id);
2964         }
2965         if (c->has_socket_id) {
2966             monitor_printf(mon, "    socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2967         }
2968         if (c->has_core_id) {
2969             monitor_printf(mon, "    core-id: \"%" PRIu64 "\"\n", c->core_id);
2970         }
2971         if (c->has_thread_id) {
2972             monitor_printf(mon, "    thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2973         }
2974
2975         l = l->next;
2976     }
2977
2978     qapi_free_HotpluggableCPUList(saved);
2979 }
2980
2981 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2982 {
2983     Error *err = NULL;
2984     GuidInfo *info = qmp_query_vm_generation_id(&err);
2985     if (info) {
2986         monitor_printf(mon, "%s\n", info->guid);
2987     }
2988     hmp_handle_error(mon, &err);
2989     qapi_free_GuidInfo(info);
2990 }
2991
2992 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
2993 {
2994     Error *err = NULL;
2995     MemoryInfo *info = qmp_query_memory_size_summary(&err);
2996     if (info) {
2997         monitor_printf(mon, "base memory: %" PRIu64 "\n",
2998                        info->base_memory);
2999
3000         if (info->has_plugged_memory) {
3001             monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
3002                            info->plugged_memory);
3003         }
3004
3005         qapi_free_MemoryInfo(info);
3006     }
3007     hmp_handle_error(mon, &err);
3008 }
This page took 0.193585 seconds and 4 git commands to generate.