Merge remote-tracking branch 'mcayland/tags/qemu-openbios-signed' into staging
[qemu/ar7.git] / hmp.c
blob20f5daba5e042ebc6993c633980bd9a88d1b518d
1 /*
2 * Human Monitor Interface
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "hmp.h"
18 #include "net/net.h"
19 #include "net/eth.h"
20 #include "sysemu/char.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/option.h"
25 #include "qemu/timer.h"
26 #include "qmp-commands.h"
27 #include "qemu/sockets.h"
28 #include "monitor/monitor.h"
29 #include "monitor/qdev.h"
30 #include "qapi/opts-visitor.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/string-input-visitor.h"
33 #include "qapi/string-output-visitor.h"
34 #include "qapi/util.h"
35 #include "qapi-visit.h"
36 #include "qom/object_interfaces.h"
37 #include "ui/console.h"
38 #include "block/nbd.h"
39 #include "block/qapi.h"
40 #include "qemu-io.h"
41 #include "qemu/cutils.h"
42 #include "qemu/error-report.h"
43 #include "exec/ramlist.h"
44 #include "hw/intc/intc.h"
46 #ifdef CONFIG_SPICE
47 #include <spice/enums.h>
48 #endif
50 static void hmp_handle_error(Monitor *mon, Error **errp)
52 assert(errp);
53 if (*errp) {
54 error_report_err(*errp);
58 void hmp_info_name(Monitor *mon, const QDict *qdict)
60 NameInfo *info;
62 info = qmp_query_name(NULL);
63 if (info->has_name) {
64 monitor_printf(mon, "%s\n", info->name);
66 qapi_free_NameInfo(info);
69 void hmp_info_version(Monitor *mon, const QDict *qdict)
71 VersionInfo *info;
73 info = qmp_query_version(NULL);
75 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
76 info->qemu->major, info->qemu->minor, info->qemu->micro,
77 info->package);
79 qapi_free_VersionInfo(info);
82 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
84 KvmInfo *info;
86 info = qmp_query_kvm(NULL);
87 monitor_printf(mon, "kvm support: ");
88 if (info->present) {
89 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
90 } else {
91 monitor_printf(mon, "not compiled\n");
94 qapi_free_KvmInfo(info);
97 void hmp_info_status(Monitor *mon, const QDict *qdict)
99 StatusInfo *info;
101 info = qmp_query_status(NULL);
103 monitor_printf(mon, "VM status: %s%s",
104 info->running ? "running" : "paused",
105 info->singlestep ? " (single step mode)" : "");
107 if (!info->running && info->status != RUN_STATE_PAUSED) {
108 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
111 monitor_printf(mon, "\n");
113 qapi_free_StatusInfo(info);
116 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
118 UuidInfo *info;
120 info = qmp_query_uuid(NULL);
121 monitor_printf(mon, "%s\n", info->UUID);
122 qapi_free_UuidInfo(info);
125 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
127 ChardevInfoList *char_info, *info;
129 char_info = qmp_query_chardev(NULL);
130 for (info = char_info; info; info = info->next) {
131 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
132 info->value->filename);
135 qapi_free_ChardevInfoList(char_info);
138 void hmp_info_mice(Monitor *mon, const QDict *qdict)
140 MouseInfoList *mice_list, *mouse;
142 mice_list = qmp_query_mice(NULL);
143 if (!mice_list) {
144 monitor_printf(mon, "No mouse devices connected\n");
145 return;
148 for (mouse = mice_list; mouse; mouse = mouse->next) {
149 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
150 mouse->value->current ? '*' : ' ',
151 mouse->value->index, mouse->value->name,
152 mouse->value->absolute ? " (absolute)" : "");
155 qapi_free_MouseInfoList(mice_list);
158 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
160 MigrationInfo *info;
161 MigrationCapabilityStatusList *caps, *cap;
163 info = qmp_query_migrate(NULL);
164 caps = qmp_query_migrate_capabilities(NULL);
166 /* do not display parameters during setup */
167 if (info->has_status && caps) {
168 monitor_printf(mon, "capabilities: ");
169 for (cap = caps; cap; cap = cap->next) {
170 monitor_printf(mon, "%s: %s ",
171 MigrationCapability_lookup[cap->value->capability],
172 cap->value->state ? "on" : "off");
174 monitor_printf(mon, "\n");
177 if (info->has_status) {
178 monitor_printf(mon, "Migration status: %s",
179 MigrationStatus_lookup[info->status]);
180 if (info->status == MIGRATION_STATUS_FAILED &&
181 info->has_error_desc) {
182 monitor_printf(mon, " (%s)\n", info->error_desc);
183 } else {
184 monitor_printf(mon, "\n");
187 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
188 info->total_time);
189 if (info->has_expected_downtime) {
190 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
191 info->expected_downtime);
193 if (info->has_downtime) {
194 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
195 info->downtime);
197 if (info->has_setup_time) {
198 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
199 info->setup_time);
203 if (info->has_ram) {
204 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
205 info->ram->transferred >> 10);
206 monitor_printf(mon, "throughput: %0.2f mbps\n",
207 info->ram->mbps);
208 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
209 info->ram->remaining >> 10);
210 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
211 info->ram->total >> 10);
212 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
213 info->ram->duplicate);
214 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
215 info->ram->skipped);
216 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
217 info->ram->normal);
218 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
219 info->ram->normal_bytes >> 10);
220 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
221 info->ram->dirty_sync_count);
222 monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
223 info->ram->page_size >> 10);
225 if (info->ram->dirty_pages_rate) {
226 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
227 info->ram->dirty_pages_rate);
229 if (info->ram->postcopy_requests) {
230 monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
231 info->ram->postcopy_requests);
235 if (info->has_disk) {
236 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
237 info->disk->transferred >> 10);
238 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
239 info->disk->remaining >> 10);
240 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
241 info->disk->total >> 10);
244 if (info->has_xbzrle_cache) {
245 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
246 info->xbzrle_cache->cache_size);
247 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
248 info->xbzrle_cache->bytes >> 10);
249 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
250 info->xbzrle_cache->pages);
251 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
252 info->xbzrle_cache->cache_miss);
253 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
254 info->xbzrle_cache->cache_miss_rate);
255 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
256 info->xbzrle_cache->overflow);
259 if (info->has_cpu_throttle_percentage) {
260 monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
261 info->cpu_throttle_percentage);
264 qapi_free_MigrationInfo(info);
265 qapi_free_MigrationCapabilityStatusList(caps);
268 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
270 MigrationCapabilityStatusList *caps, *cap;
272 caps = qmp_query_migrate_capabilities(NULL);
274 if (caps) {
275 for (cap = caps; cap; cap = cap->next) {
276 monitor_printf(mon, "%s: %s\n",
277 MigrationCapability_lookup[cap->value->capability],
278 cap->value->state ? "on" : "off");
282 qapi_free_MigrationCapabilityStatusList(caps);
285 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
287 MigrationParameters *params;
289 params = qmp_query_migrate_parameters(NULL);
291 if (params) {
292 assert(params->has_compress_level);
293 monitor_printf(mon, "%s: %" PRId64 "\n",
294 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
295 params->compress_level);
296 assert(params->has_compress_threads);
297 monitor_printf(mon, "%s: %" PRId64 "\n",
298 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
299 params->compress_threads);
300 assert(params->has_decompress_threads);
301 monitor_printf(mon, "%s: %" PRId64 "\n",
302 MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
303 params->decompress_threads);
304 assert(params->has_cpu_throttle_initial);
305 monitor_printf(mon, "%s: %" PRId64 "\n",
306 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL],
307 params->cpu_throttle_initial);
308 assert(params->has_cpu_throttle_increment);
309 monitor_printf(mon, "%s: %" PRId64 "\n",
310 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT],
311 params->cpu_throttle_increment);
312 monitor_printf(mon, "%s: '%s'\n",
313 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_CREDS],
314 params->has_tls_creds ? params->tls_creds : "");
315 monitor_printf(mon, "%s: '%s'\n",
316 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_HOSTNAME],
317 params->has_tls_hostname ? params->tls_hostname : "");
318 assert(params->has_max_bandwidth);
319 monitor_printf(mon, "%s: %" PRId64 " bytes/second\n",
320 MigrationParameter_lookup[MIGRATION_PARAMETER_MAX_BANDWIDTH],
321 params->max_bandwidth);
322 assert(params->has_downtime_limit);
323 monitor_printf(mon, "%s: %" PRId64 " milliseconds\n",
324 MigrationParameter_lookup[MIGRATION_PARAMETER_DOWNTIME_LIMIT],
325 params->downtime_limit);
326 assert(params->has_x_checkpoint_delay);
327 monitor_printf(mon, "%s: %" PRId64 "\n",
328 MigrationParameter_lookup[MIGRATION_PARAMETER_X_CHECKPOINT_DELAY],
329 params->x_checkpoint_delay);
330 assert(params->has_block_incremental);
331 monitor_printf(mon, "%s: %s\n",
332 MigrationParameter_lookup[MIGRATION_PARAMETER_BLOCK_INCREMENTAL],
333 params->block_incremental ? "on" : "off");
336 qapi_free_MigrationParameters(params);
339 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
341 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
342 qmp_query_migrate_cache_size(NULL) >> 10);
345 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
347 CpuInfoList *cpu_list, *cpu;
349 cpu_list = qmp_query_cpus(NULL);
351 for (cpu = cpu_list; cpu; cpu = cpu->next) {
352 int active = ' ';
354 if (cpu->value->CPU == monitor_get_cpu_index()) {
355 active = '*';
358 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
360 switch (cpu->value->arch) {
361 case CPU_INFO_ARCH_X86:
362 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->u.x86.pc);
363 break;
364 case CPU_INFO_ARCH_PPC:
365 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->u.ppc.nip);
366 break;
367 case CPU_INFO_ARCH_SPARC:
368 monitor_printf(mon, " pc=0x%016" PRIx64,
369 cpu->value->u.q_sparc.pc);
370 monitor_printf(mon, " npc=0x%016" PRIx64,
371 cpu->value->u.q_sparc.npc);
372 break;
373 case CPU_INFO_ARCH_MIPS:
374 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.q_mips.PC);
375 break;
376 case CPU_INFO_ARCH_TRICORE:
377 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.tricore.PC);
378 break;
379 default:
380 break;
383 if (cpu->value->halted) {
384 monitor_printf(mon, " (halted)");
387 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
390 qapi_free_CpuInfoList(cpu_list);
393 static void print_block_info(Monitor *mon, BlockInfo *info,
394 BlockDeviceInfo *inserted, bool verbose)
396 ImageInfo *image_info;
398 assert(!info || !info->has_inserted || info->inserted == inserted);
400 if (info) {
401 monitor_printf(mon, "%s", info->device);
402 if (inserted && inserted->has_node_name) {
403 monitor_printf(mon, " (%s)", inserted->node_name);
405 } else {
406 assert(inserted);
407 monitor_printf(mon, "%s",
408 inserted->has_node_name
409 ? inserted->node_name
410 : "<anonymous>");
413 if (inserted) {
414 monitor_printf(mon, ": %s (%s%s%s)\n",
415 inserted->file,
416 inserted->drv,
417 inserted->ro ? ", read-only" : "",
418 inserted->encrypted ? ", encrypted" : "");
419 } else {
420 monitor_printf(mon, ": [not inserted]\n");
423 if (info) {
424 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
425 monitor_printf(mon, " I/O status: %s\n",
426 BlockDeviceIoStatus_lookup[info->io_status]);
429 if (info->removable) {
430 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
431 info->locked ? "" : "not ",
432 info->tray_open ? "open" : "closed");
437 if (!inserted) {
438 return;
441 monitor_printf(mon, " Cache mode: %s%s%s\n",
442 inserted->cache->writeback ? "writeback" : "writethrough",
443 inserted->cache->direct ? ", direct" : "",
444 inserted->cache->no_flush ? ", ignore flushes" : "");
446 if (inserted->has_backing_file) {
447 monitor_printf(mon,
448 " Backing file: %s "
449 "(chain depth: %" PRId64 ")\n",
450 inserted->backing_file,
451 inserted->backing_file_depth);
454 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
455 monitor_printf(mon, " Detect zeroes: %s\n",
456 BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
459 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
460 inserted->iops || inserted->iops_rd || inserted->iops_wr)
462 monitor_printf(mon, " I/O throttling: bps=%" PRId64
463 " bps_rd=%" PRId64 " bps_wr=%" PRId64
464 " bps_max=%" PRId64
465 " bps_rd_max=%" PRId64
466 " bps_wr_max=%" PRId64
467 " iops=%" PRId64 " iops_rd=%" PRId64
468 " iops_wr=%" PRId64
469 " iops_max=%" PRId64
470 " iops_rd_max=%" PRId64
471 " iops_wr_max=%" PRId64
472 " iops_size=%" PRId64
473 " group=%s\n",
474 inserted->bps,
475 inserted->bps_rd,
476 inserted->bps_wr,
477 inserted->bps_max,
478 inserted->bps_rd_max,
479 inserted->bps_wr_max,
480 inserted->iops,
481 inserted->iops_rd,
482 inserted->iops_wr,
483 inserted->iops_max,
484 inserted->iops_rd_max,
485 inserted->iops_wr_max,
486 inserted->iops_size,
487 inserted->group);
490 if (verbose) {
491 monitor_printf(mon, "\nImages:\n");
492 image_info = inserted->image;
493 while (1) {
494 bdrv_image_info_dump((fprintf_function)monitor_printf,
495 mon, image_info);
496 if (image_info->has_backing_image) {
497 image_info = image_info->backing_image;
498 } else {
499 break;
505 void hmp_info_block(Monitor *mon, const QDict *qdict)
507 BlockInfoList *block_list, *info;
508 BlockDeviceInfoList *blockdev_list, *blockdev;
509 const char *device = qdict_get_try_str(qdict, "device");
510 bool verbose = qdict_get_try_bool(qdict, "verbose", false);
511 bool nodes = qdict_get_try_bool(qdict, "nodes", false);
512 bool printed = false;
514 /* Print BlockBackend information */
515 if (!nodes) {
516 block_list = qmp_query_block(NULL);
517 } else {
518 block_list = NULL;
521 for (info = block_list; info; info = info->next) {
522 if (device && strcmp(device, info->value->device)) {
523 continue;
526 if (info != block_list) {
527 monitor_printf(mon, "\n");
530 print_block_info(mon, info->value, info->value->has_inserted
531 ? info->value->inserted : NULL,
532 verbose);
533 printed = true;
536 qapi_free_BlockInfoList(block_list);
538 if ((!device && !nodes) || printed) {
539 return;
542 /* Print node information */
543 blockdev_list = qmp_query_named_block_nodes(NULL);
544 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
545 assert(blockdev->value->has_node_name);
546 if (device && strcmp(device, blockdev->value->node_name)) {
547 continue;
550 if (blockdev != blockdev_list) {
551 monitor_printf(mon, "\n");
554 print_block_info(mon, NULL, blockdev->value, verbose);
556 qapi_free_BlockDeviceInfoList(blockdev_list);
559 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
561 BlockStatsList *stats_list, *stats;
563 stats_list = qmp_query_blockstats(false, false, NULL);
565 for (stats = stats_list; stats; stats = stats->next) {
566 if (!stats->value->has_device) {
567 continue;
570 monitor_printf(mon, "%s:", stats->value->device);
571 monitor_printf(mon, " rd_bytes=%" PRId64
572 " wr_bytes=%" PRId64
573 " rd_operations=%" PRId64
574 " wr_operations=%" PRId64
575 " flush_operations=%" PRId64
576 " wr_total_time_ns=%" PRId64
577 " rd_total_time_ns=%" PRId64
578 " flush_total_time_ns=%" PRId64
579 " rd_merged=%" PRId64
580 " wr_merged=%" PRId64
581 " idle_time_ns=%" PRId64
582 "\n",
583 stats->value->stats->rd_bytes,
584 stats->value->stats->wr_bytes,
585 stats->value->stats->rd_operations,
586 stats->value->stats->wr_operations,
587 stats->value->stats->flush_operations,
588 stats->value->stats->wr_total_time_ns,
589 stats->value->stats->rd_total_time_ns,
590 stats->value->stats->flush_total_time_ns,
591 stats->value->stats->rd_merged,
592 stats->value->stats->wr_merged,
593 stats->value->stats->idle_time_ns);
596 qapi_free_BlockStatsList(stats_list);
599 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
601 VncInfo *info;
602 Error *err = NULL;
603 VncClientInfoList *client;
605 info = qmp_query_vnc(&err);
606 if (err) {
607 error_report_err(err);
608 return;
611 if (!info->enabled) {
612 monitor_printf(mon, "Server: disabled\n");
613 goto out;
616 monitor_printf(mon, "Server:\n");
617 if (info->has_host && info->has_service) {
618 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
620 if (info->has_auth) {
621 monitor_printf(mon, " auth: %s\n", info->auth);
624 if (!info->has_clients || info->clients == NULL) {
625 monitor_printf(mon, "Client: none\n");
626 } else {
627 for (client = info->clients; client; client = client->next) {
628 monitor_printf(mon, "Client:\n");
629 monitor_printf(mon, " address: %s:%s\n",
630 client->value->host,
631 client->value->service);
632 monitor_printf(mon, " x509_dname: %s\n",
633 client->value->x509_dname ?
634 client->value->x509_dname : "none");
635 monitor_printf(mon, " username: %s\n",
636 client->value->has_sasl_username ?
637 client->value->sasl_username : "none");
641 out:
642 qapi_free_VncInfo(info);
645 #ifdef CONFIG_SPICE
646 void hmp_info_spice(Monitor *mon, const QDict *qdict)
648 SpiceChannelList *chan;
649 SpiceInfo *info;
650 const char *channel_name;
651 const char * const channel_names[] = {
652 [SPICE_CHANNEL_MAIN] = "main",
653 [SPICE_CHANNEL_DISPLAY] = "display",
654 [SPICE_CHANNEL_INPUTS] = "inputs",
655 [SPICE_CHANNEL_CURSOR] = "cursor",
656 [SPICE_CHANNEL_PLAYBACK] = "playback",
657 [SPICE_CHANNEL_RECORD] = "record",
658 [SPICE_CHANNEL_TUNNEL] = "tunnel",
659 [SPICE_CHANNEL_SMARTCARD] = "smartcard",
660 [SPICE_CHANNEL_USBREDIR] = "usbredir",
661 [SPICE_CHANNEL_PORT] = "port",
662 #if 0
663 /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
664 * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
665 * as quick fix for build failures with older versions. */
666 [SPICE_CHANNEL_WEBDAV] = "webdav",
667 #endif
670 info = qmp_query_spice(NULL);
672 if (!info->enabled) {
673 monitor_printf(mon, "Server: disabled\n");
674 goto out;
677 monitor_printf(mon, "Server:\n");
678 if (info->has_port) {
679 monitor_printf(mon, " address: %s:%" PRId64 "\n",
680 info->host, info->port);
682 if (info->has_tls_port) {
683 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
684 info->host, info->tls_port);
686 monitor_printf(mon, " migrated: %s\n",
687 info->migrated ? "true" : "false");
688 monitor_printf(mon, " auth: %s\n", info->auth);
689 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
690 monitor_printf(mon, " mouse-mode: %s\n",
691 SpiceQueryMouseMode_lookup[info->mouse_mode]);
693 if (!info->has_channels || info->channels == NULL) {
694 monitor_printf(mon, "Channels: none\n");
695 } else {
696 for (chan = info->channels; chan; chan = chan->next) {
697 monitor_printf(mon, "Channel:\n");
698 monitor_printf(mon, " address: %s:%s%s\n",
699 chan->value->host, chan->value->port,
700 chan->value->tls ? " [tls]" : "");
701 monitor_printf(mon, " session: %" PRId64 "\n",
702 chan->value->connection_id);
703 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
704 chan->value->channel_type, chan->value->channel_id);
706 channel_name = "unknown";
707 if (chan->value->channel_type > 0 &&
708 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
709 channel_names[chan->value->channel_type]) {
710 channel_name = channel_names[chan->value->channel_type];
713 monitor_printf(mon, " channel name: %s\n", channel_name);
717 out:
718 qapi_free_SpiceInfo(info);
720 #endif
722 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
724 BalloonInfo *info;
725 Error *err = NULL;
727 info = qmp_query_balloon(&err);
728 if (err) {
729 error_report_err(err);
730 return;
733 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
735 qapi_free_BalloonInfo(info);
738 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
740 PciMemoryRegionList *region;
742 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
743 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
744 dev->slot, dev->function);
745 monitor_printf(mon, " ");
747 if (dev->class_info->has_desc) {
748 monitor_printf(mon, "%s", dev->class_info->desc);
749 } else {
750 monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
753 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
754 dev->id->vendor, dev->id->device);
756 if (dev->has_irq) {
757 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
760 if (dev->has_pci_bridge) {
761 monitor_printf(mon, " BUS %" PRId64 ".\n",
762 dev->pci_bridge->bus->number);
763 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
764 dev->pci_bridge->bus->secondary);
765 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
766 dev->pci_bridge->bus->subordinate);
768 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
769 dev->pci_bridge->bus->io_range->base,
770 dev->pci_bridge->bus->io_range->limit);
772 monitor_printf(mon,
773 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
774 dev->pci_bridge->bus->memory_range->base,
775 dev->pci_bridge->bus->memory_range->limit);
777 monitor_printf(mon, " prefetchable memory range "
778 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
779 dev->pci_bridge->bus->prefetchable_range->base,
780 dev->pci_bridge->bus->prefetchable_range->limit);
783 for (region = dev->regions; region; region = region->next) {
784 uint64_t addr, size;
786 addr = region->value->address;
787 size = region->value->size;
789 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
791 if (!strcmp(region->value->type, "io")) {
792 monitor_printf(mon, "I/O at 0x%04" PRIx64
793 " [0x%04" PRIx64 "].\n",
794 addr, addr + size - 1);
795 } else {
796 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
797 " [0x%08" PRIx64 "].\n",
798 region->value->mem_type_64 ? 64 : 32,
799 region->value->prefetch ? " prefetchable" : "",
800 addr, addr + size - 1);
804 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
806 if (dev->has_pci_bridge) {
807 if (dev->pci_bridge->has_devices) {
808 PciDeviceInfoList *cdev;
809 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
810 hmp_info_pci_device(mon, cdev->value);
816 static int hmp_info_irq_foreach(Object *obj, void *opaque)
818 InterruptStatsProvider *intc;
819 InterruptStatsProviderClass *k;
820 Monitor *mon = opaque;
822 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
823 intc = INTERRUPT_STATS_PROVIDER(obj);
824 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
825 uint64_t *irq_counts;
826 unsigned int nb_irqs, i;
827 if (k->get_statistics &&
828 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
829 if (nb_irqs > 0) {
830 monitor_printf(mon, "IRQ statistics for %s:\n",
831 object_get_typename(obj));
832 for (i = 0; i < nb_irqs; i++) {
833 if (irq_counts[i] > 0) {
834 monitor_printf(mon, "%2d: %" PRId64 "\n", i,
835 irq_counts[i]);
839 } else {
840 monitor_printf(mon, "IRQ statistics not available for %s.\n",
841 object_get_typename(obj));
845 return 0;
848 void hmp_info_irq(Monitor *mon, const QDict *qdict)
850 object_child_foreach_recursive(object_get_root(),
851 hmp_info_irq_foreach, mon);
854 static int hmp_info_pic_foreach(Object *obj, void *opaque)
856 InterruptStatsProvider *intc;
857 InterruptStatsProviderClass *k;
858 Monitor *mon = opaque;
860 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
861 intc = INTERRUPT_STATS_PROVIDER(obj);
862 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
863 if (k->print_info) {
864 k->print_info(intc, mon);
865 } else {
866 monitor_printf(mon, "Interrupt controller information not available for %s.\n",
867 object_get_typename(obj));
871 return 0;
874 void hmp_info_pic(Monitor *mon, const QDict *qdict)
876 object_child_foreach_recursive(object_get_root(),
877 hmp_info_pic_foreach, mon);
880 void hmp_info_pci(Monitor *mon, const QDict *qdict)
882 PciInfoList *info_list, *info;
883 Error *err = NULL;
885 info_list = qmp_query_pci(&err);
886 if (err) {
887 monitor_printf(mon, "PCI devices not supported\n");
888 error_free(err);
889 return;
892 for (info = info_list; info; info = info->next) {
893 PciDeviceInfoList *dev;
895 for (dev = info->value->devices; dev; dev = dev->next) {
896 hmp_info_pci_device(mon, dev->value);
900 qapi_free_PciInfoList(info_list);
903 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
905 BlockJobInfoList *list;
906 Error *err = NULL;
908 list = qmp_query_block_jobs(&err);
909 assert(!err);
911 if (!list) {
912 monitor_printf(mon, "No active jobs\n");
913 return;
916 while (list) {
917 if (strcmp(list->value->type, "stream") == 0) {
918 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
919 " of %" PRId64 " bytes, speed limit %" PRId64
920 " bytes/s\n",
921 list->value->device,
922 list->value->offset,
923 list->value->len,
924 list->value->speed);
925 } else {
926 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
927 " of %" PRId64 " bytes, speed limit %" PRId64
928 " bytes/s\n",
929 list->value->type,
930 list->value->device,
931 list->value->offset,
932 list->value->len,
933 list->value->speed);
935 list = list->next;
938 qapi_free_BlockJobInfoList(list);
941 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
943 TPMInfoList *info_list, *info;
944 Error *err = NULL;
945 unsigned int c = 0;
946 TPMPassthroughOptions *tpo;
948 info_list = qmp_query_tpm(&err);
949 if (err) {
950 monitor_printf(mon, "TPM device not supported\n");
951 error_free(err);
952 return;
955 if (info_list) {
956 monitor_printf(mon, "TPM device:\n");
959 for (info = info_list; info; info = info->next) {
960 TPMInfo *ti = info->value;
961 monitor_printf(mon, " tpm%d: model=%s\n",
962 c, TpmModel_lookup[ti->model]);
964 monitor_printf(mon, " \\ %s: type=%s",
965 ti->id, TpmTypeOptionsKind_lookup[ti->options->type]);
967 switch (ti->options->type) {
968 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
969 tpo = ti->options->u.passthrough.data;
970 monitor_printf(mon, "%s%s%s%s",
971 tpo->has_path ? ",path=" : "",
972 tpo->has_path ? tpo->path : "",
973 tpo->has_cancel_path ? ",cancel-path=" : "",
974 tpo->has_cancel_path ? tpo->cancel_path : "");
975 break;
976 case TPM_TYPE_OPTIONS_KIND__MAX:
977 break;
979 monitor_printf(mon, "\n");
980 c++;
982 qapi_free_TPMInfoList(info_list);
985 void hmp_quit(Monitor *mon, const QDict *qdict)
987 monitor_suspend(mon);
988 qmp_quit(NULL);
991 void hmp_stop(Monitor *mon, const QDict *qdict)
993 qmp_stop(NULL);
996 void hmp_system_reset(Monitor *mon, const QDict *qdict)
998 qmp_system_reset(NULL);
1001 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1003 qmp_system_powerdown(NULL);
1006 void hmp_cpu(Monitor *mon, const QDict *qdict)
1008 int64_t cpu_index;
1010 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1011 use it are converted to the QAPI */
1012 cpu_index = qdict_get_int(qdict, "index");
1013 if (monitor_set_cpu(cpu_index) < 0) {
1014 monitor_printf(mon, "invalid CPU index\n");
1018 void hmp_memsave(Monitor *mon, const QDict *qdict)
1020 uint32_t size = qdict_get_int(qdict, "size");
1021 const char *filename = qdict_get_str(qdict, "filename");
1022 uint64_t addr = qdict_get_int(qdict, "val");
1023 Error *err = NULL;
1024 int cpu_index = monitor_get_cpu_index();
1026 if (cpu_index < 0) {
1027 monitor_printf(mon, "No CPU available\n");
1028 return;
1031 qmp_memsave(addr, size, filename, true, cpu_index, &err);
1032 hmp_handle_error(mon, &err);
1035 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1037 uint32_t size = qdict_get_int(qdict, "size");
1038 const char *filename = qdict_get_str(qdict, "filename");
1039 uint64_t addr = qdict_get_int(qdict, "val");
1040 Error *err = NULL;
1042 qmp_pmemsave(addr, size, filename, &err);
1043 hmp_handle_error(mon, &err);
1046 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1048 const char *chardev = qdict_get_str(qdict, "device");
1049 const char *data = qdict_get_str(qdict, "data");
1050 Error *err = NULL;
1052 qmp_ringbuf_write(chardev, data, false, 0, &err);
1054 hmp_handle_error(mon, &err);
1057 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1059 uint32_t size = qdict_get_int(qdict, "size");
1060 const char *chardev = qdict_get_str(qdict, "device");
1061 char *data;
1062 Error *err = NULL;
1063 int i;
1065 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1066 if (err) {
1067 error_report_err(err);
1068 return;
1071 for (i = 0; data[i]; i++) {
1072 unsigned char ch = data[i];
1074 if (ch == '\\') {
1075 monitor_printf(mon, "\\\\");
1076 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1077 monitor_printf(mon, "\\u%04X", ch);
1078 } else {
1079 monitor_printf(mon, "%c", ch);
1083 monitor_printf(mon, "\n");
1084 g_free(data);
1087 static void hmp_cont_cb(void *opaque, int err)
1089 if (!err) {
1090 qmp_cont(NULL);
1094 static bool key_is_missing(const BlockInfo *bdev)
1096 return (bdev->inserted && bdev->inserted->encryption_key_missing);
1099 void hmp_cont(Monitor *mon, const QDict *qdict)
1101 BlockInfoList *bdev_list, *bdev;
1102 Error *err = NULL;
1104 bdev_list = qmp_query_block(NULL);
1105 for (bdev = bdev_list; bdev; bdev = bdev->next) {
1106 if (key_is_missing(bdev->value)) {
1107 monitor_read_block_device_key(mon, bdev->value->device,
1108 hmp_cont_cb, NULL);
1109 goto out;
1113 qmp_cont(&err);
1114 hmp_handle_error(mon, &err);
1116 out:
1117 qapi_free_BlockInfoList(bdev_list);
1120 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1122 qmp_system_wakeup(NULL);
1125 void hmp_nmi(Monitor *mon, const QDict *qdict)
1127 Error *err = NULL;
1129 qmp_inject_nmi(&err);
1130 hmp_handle_error(mon, &err);
1133 void hmp_set_link(Monitor *mon, const QDict *qdict)
1135 const char *name = qdict_get_str(qdict, "name");
1136 bool up = qdict_get_bool(qdict, "up");
1137 Error *err = NULL;
1139 qmp_set_link(name, up, &err);
1140 hmp_handle_error(mon, &err);
1143 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1145 const char *device = qdict_get_str(qdict, "device");
1146 const char *password = qdict_get_str(qdict, "password");
1147 Error *err = NULL;
1149 qmp_block_passwd(true, device, false, NULL, password, &err);
1150 hmp_handle_error(mon, &err);
1153 void hmp_balloon(Monitor *mon, const QDict *qdict)
1155 int64_t value = qdict_get_int(qdict, "value");
1156 Error *err = NULL;
1158 qmp_balloon(value, &err);
1159 if (err) {
1160 error_report_err(err);
1164 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1166 const char *device = qdict_get_str(qdict, "device");
1167 int64_t size = qdict_get_int(qdict, "size");
1168 Error *err = NULL;
1170 qmp_block_resize(true, device, false, NULL, size, &err);
1171 hmp_handle_error(mon, &err);
1174 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1176 const char *filename = qdict_get_str(qdict, "target");
1177 const char *format = qdict_get_try_str(qdict, "format");
1178 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1179 bool full = qdict_get_try_bool(qdict, "full", false);
1180 Error *err = NULL;
1181 DriveMirror mirror = {
1182 .device = (char *)qdict_get_str(qdict, "device"),
1183 .target = (char *)filename,
1184 .has_format = !!format,
1185 .format = (char *)format,
1186 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1187 .has_mode = true,
1188 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1189 .unmap = true,
1192 if (!filename) {
1193 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1194 hmp_handle_error(mon, &err);
1195 return;
1197 qmp_drive_mirror(&mirror, &err);
1198 hmp_handle_error(mon, &err);
1201 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1203 const char *device = qdict_get_str(qdict, "device");
1204 const char *filename = qdict_get_str(qdict, "target");
1205 const char *format = qdict_get_try_str(qdict, "format");
1206 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1207 bool full = qdict_get_try_bool(qdict, "full", false);
1208 bool compress = qdict_get_try_bool(qdict, "compress", false);
1209 Error *err = NULL;
1210 DriveBackup backup = {
1211 .device = (char *)device,
1212 .target = (char *)filename,
1213 .has_format = !!format,
1214 .format = (char *)format,
1215 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1216 .has_mode = true,
1217 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1218 .has_compress = !!compress,
1219 .compress = compress,
1222 if (!filename) {
1223 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1224 hmp_handle_error(mon, &err);
1225 return;
1228 qmp_drive_backup(&backup, &err);
1229 hmp_handle_error(mon, &err);
1232 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1234 const char *device = qdict_get_str(qdict, "device");
1235 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1236 const char *format = qdict_get_try_str(qdict, "format");
1237 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1238 enum NewImageMode mode;
1239 Error *err = NULL;
1241 if (!filename) {
1242 /* In the future, if 'snapshot-file' is not specified, the snapshot
1243 will be taken internally. Today it's actually required. */
1244 error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1245 hmp_handle_error(mon, &err);
1246 return;
1249 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1250 qmp_blockdev_snapshot_sync(true, device, false, NULL,
1251 filename, false, NULL,
1252 !!format, format,
1253 true, mode, &err);
1254 hmp_handle_error(mon, &err);
1257 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1259 const char *device = qdict_get_str(qdict, "device");
1260 const char *name = qdict_get_str(qdict, "name");
1261 Error *err = NULL;
1263 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1264 hmp_handle_error(mon, &err);
1267 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1269 const char *device = qdict_get_str(qdict, "device");
1270 const char *name = qdict_get_str(qdict, "name");
1271 const char *id = qdict_get_try_str(qdict, "id");
1272 Error *err = NULL;
1274 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1275 true, name, &err);
1276 hmp_handle_error(mon, &err);
1279 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1281 int saved_vm_running = runstate_is_running();
1282 const char *name = qdict_get_str(qdict, "name");
1283 Error *err = NULL;
1285 vm_stop(RUN_STATE_RESTORE_VM);
1287 if (load_vmstate(name, &err) == 0 && saved_vm_running) {
1288 vm_start();
1290 hmp_handle_error(mon, &err);
1293 void hmp_savevm(Monitor *mon, const QDict *qdict)
1295 Error *err = NULL;
1297 save_vmstate(qdict_get_try_str(qdict, "name"), &err);
1298 hmp_handle_error(mon, &err);
1301 void hmp_delvm(Monitor *mon, const QDict *qdict)
1303 BlockDriverState *bs;
1304 Error *err;
1305 const char *name = qdict_get_str(qdict, "name");
1307 if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1308 error_reportf_err(err,
1309 "Error while deleting snapshot on device '%s': ",
1310 bdrv_get_device_name(bs));
1314 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1316 BlockDriverState *bs, *bs1;
1317 BdrvNextIterator it1;
1318 QEMUSnapshotInfo *sn_tab, *sn;
1319 bool no_snapshot = true;
1320 int nb_sns, i;
1321 int total;
1322 int *global_snapshots;
1323 AioContext *aio_context;
1325 typedef struct SnapshotEntry {
1326 QEMUSnapshotInfo sn;
1327 QTAILQ_ENTRY(SnapshotEntry) next;
1328 } SnapshotEntry;
1330 typedef struct ImageEntry {
1331 const char *imagename;
1332 QTAILQ_ENTRY(ImageEntry) next;
1333 QTAILQ_HEAD(, SnapshotEntry) snapshots;
1334 } ImageEntry;
1336 QTAILQ_HEAD(, ImageEntry) image_list =
1337 QTAILQ_HEAD_INITIALIZER(image_list);
1339 ImageEntry *image_entry, *next_ie;
1340 SnapshotEntry *snapshot_entry;
1342 bs = bdrv_all_find_vmstate_bs();
1343 if (!bs) {
1344 monitor_printf(mon, "No available block device supports snapshots\n");
1345 return;
1347 aio_context = bdrv_get_aio_context(bs);
1349 aio_context_acquire(aio_context);
1350 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1351 aio_context_release(aio_context);
1353 if (nb_sns < 0) {
1354 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1355 return;
1358 for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1359 int bs1_nb_sns = 0;
1360 ImageEntry *ie;
1361 SnapshotEntry *se;
1362 AioContext *ctx = bdrv_get_aio_context(bs1);
1364 aio_context_acquire(ctx);
1365 if (bdrv_can_snapshot(bs1)) {
1366 sn = NULL;
1367 bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1368 if (bs1_nb_sns > 0) {
1369 no_snapshot = false;
1370 ie = g_new0(ImageEntry, 1);
1371 ie->imagename = bdrv_get_device_name(bs1);
1372 QTAILQ_INIT(&ie->snapshots);
1373 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1374 for (i = 0; i < bs1_nb_sns; i++) {
1375 se = g_new0(SnapshotEntry, 1);
1376 se->sn = sn[i];
1377 QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1380 g_free(sn);
1382 aio_context_release(ctx);
1385 if (no_snapshot) {
1386 monitor_printf(mon, "There is no snapshot available.\n");
1387 return;
1390 global_snapshots = g_new0(int, nb_sns);
1391 total = 0;
1392 for (i = 0; i < nb_sns; i++) {
1393 SnapshotEntry *next_sn;
1394 if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1395 global_snapshots[total] = i;
1396 total++;
1397 QTAILQ_FOREACH(image_entry, &image_list, next) {
1398 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1399 next, next_sn) {
1400 if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1401 QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1402 next);
1403 g_free(snapshot_entry);
1410 monitor_printf(mon, "List of snapshots present on all disks:\n");
1412 if (total > 0) {
1413 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1414 monitor_printf(mon, "\n");
1415 for (i = 0; i < total; i++) {
1416 sn = &sn_tab[global_snapshots[i]];
1417 /* The ID is not guaranteed to be the same on all images, so
1418 * overwrite it.
1420 pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1421 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1422 monitor_printf(mon, "\n");
1424 } else {
1425 monitor_printf(mon, "None\n");
1428 QTAILQ_FOREACH(image_entry, &image_list, next) {
1429 if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1430 continue;
1432 monitor_printf(mon,
1433 "\nList of partial (non-loadable) snapshots on '%s':\n",
1434 image_entry->imagename);
1435 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1436 monitor_printf(mon, "\n");
1437 QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1438 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1439 &snapshot_entry->sn);
1440 monitor_printf(mon, "\n");
1444 QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1445 SnapshotEntry *next_sn;
1446 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1447 next_sn) {
1448 g_free(snapshot_entry);
1450 g_free(image_entry);
1452 g_free(sn_tab);
1453 g_free(global_snapshots);
1457 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1459 qmp_migrate_cancel(NULL);
1462 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1464 Error *err = NULL;
1465 const char *uri = qdict_get_str(qdict, "uri");
1467 qmp_migrate_incoming(uri, &err);
1469 hmp_handle_error(mon, &err);
1472 /* Kept for backwards compatibility */
1473 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1475 double value = qdict_get_double(qdict, "value");
1476 qmp_migrate_set_downtime(value, NULL);
1479 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1481 int64_t value = qdict_get_int(qdict, "value");
1482 Error *err = NULL;
1484 qmp_migrate_set_cache_size(value, &err);
1485 if (err) {
1486 error_report_err(err);
1487 return;
1491 /* Kept for backwards compatibility */
1492 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1494 int64_t value = qdict_get_int(qdict, "value");
1495 qmp_migrate_set_speed(value, NULL);
1498 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1500 const char *cap = qdict_get_str(qdict, "capability");
1501 bool state = qdict_get_bool(qdict, "state");
1502 Error *err = NULL;
1503 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1504 int i;
1506 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1507 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1508 caps->value = g_malloc0(sizeof(*caps->value));
1509 caps->value->capability = i;
1510 caps->value->state = state;
1511 caps->next = NULL;
1512 qmp_migrate_set_capabilities(caps, &err);
1513 break;
1517 if (i == MIGRATION_CAPABILITY__MAX) {
1518 error_setg(&err, QERR_INVALID_PARAMETER, cap);
1521 qapi_free_MigrationCapabilityStatusList(caps);
1523 if (err) {
1524 error_report_err(err);
1528 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1530 const char *param = qdict_get_str(qdict, "parameter");
1531 const char *valuestr = qdict_get_str(qdict, "value");
1532 Visitor *v = string_input_visitor_new(valuestr);
1533 uint64_t valuebw = 0;
1534 int64_t valueint = 0;
1535 bool valuebool = false;
1536 Error *err = NULL;
1537 bool use_int_value = false;
1538 int i, ret;
1540 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1541 if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
1542 MigrationParameters p = { 0 };
1543 switch (i) {
1544 case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1545 p.has_compress_level = true;
1546 use_int_value = true;
1547 break;
1548 case MIGRATION_PARAMETER_COMPRESS_THREADS:
1549 p.has_compress_threads = true;
1550 use_int_value = true;
1551 break;
1552 case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1553 p.has_decompress_threads = true;
1554 use_int_value = true;
1555 break;
1556 case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1557 p.has_cpu_throttle_initial = true;
1558 use_int_value = true;
1559 break;
1560 case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1561 p.has_cpu_throttle_increment = true;
1562 use_int_value = true;
1563 break;
1564 case MIGRATION_PARAMETER_TLS_CREDS:
1565 p.has_tls_creds = true;
1566 p.tls_creds = (char *) valuestr;
1567 break;
1568 case MIGRATION_PARAMETER_TLS_HOSTNAME:
1569 p.has_tls_hostname = true;
1570 p.tls_hostname = (char *) valuestr;
1571 break;
1572 case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1573 p.has_max_bandwidth = true;
1574 ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1575 if (ret < 0 || valuebw > INT64_MAX
1576 || (size_t)valuebw != valuebw) {
1577 error_setg(&err, "Invalid size %s", valuestr);
1578 goto cleanup;
1580 p.max_bandwidth = valuebw;
1581 break;
1582 case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1583 p.has_downtime_limit = true;
1584 use_int_value = true;
1585 break;
1586 case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1587 p.has_x_checkpoint_delay = true;
1588 use_int_value = true;
1589 break;
1590 case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1591 p.has_block_incremental = true;
1592 visit_type_bool(v, param, &valuebool, &err);
1593 if (err) {
1594 goto cleanup;
1596 p.block_incremental = valuebool;
1597 break;
1600 if (use_int_value) {
1601 visit_type_int(v, param, &valueint, &err);
1602 if (err) {
1603 goto cleanup;
1605 /* Set all integers; only one has_FOO will be set, and
1606 * the code ignores the remaining values */
1607 p.compress_level = valueint;
1608 p.compress_threads = valueint;
1609 p.decompress_threads = valueint;
1610 p.cpu_throttle_initial = valueint;
1611 p.cpu_throttle_increment = valueint;
1612 p.downtime_limit = valueint;
1613 p.x_checkpoint_delay = valueint;
1616 qmp_migrate_set_parameters(&p, &err);
1617 break;
1621 if (i == MIGRATION_PARAMETER__MAX) {
1622 error_setg(&err, QERR_INVALID_PARAMETER, param);
1625 cleanup:
1626 visit_free(v);
1627 if (err) {
1628 error_report_err(err);
1632 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1634 Error *err = NULL;
1635 const char *protocol = qdict_get_str(qdict, "protocol");
1636 const char *hostname = qdict_get_str(qdict, "hostname");
1637 bool has_port = qdict_haskey(qdict, "port");
1638 int port = qdict_get_try_int(qdict, "port", -1);
1639 bool has_tls_port = qdict_haskey(qdict, "tls-port");
1640 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1641 const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1643 qmp_client_migrate_info(protocol, hostname,
1644 has_port, port, has_tls_port, tls_port,
1645 !!cert_subject, cert_subject, &err);
1646 hmp_handle_error(mon, &err);
1649 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1651 Error *err = NULL;
1652 qmp_migrate_start_postcopy(&err);
1653 hmp_handle_error(mon, &err);
1656 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1658 Error *err = NULL;
1660 qmp_x_colo_lost_heartbeat(&err);
1661 hmp_handle_error(mon, &err);
1664 void hmp_set_password(Monitor *mon, const QDict *qdict)
1666 const char *protocol = qdict_get_str(qdict, "protocol");
1667 const char *password = qdict_get_str(qdict, "password");
1668 const char *connected = qdict_get_try_str(qdict, "connected");
1669 Error *err = NULL;
1671 qmp_set_password(protocol, password, !!connected, connected, &err);
1672 hmp_handle_error(mon, &err);
1675 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1677 const char *protocol = qdict_get_str(qdict, "protocol");
1678 const char *whenstr = qdict_get_str(qdict, "time");
1679 Error *err = NULL;
1681 qmp_expire_password(protocol, whenstr, &err);
1682 hmp_handle_error(mon, &err);
1685 void hmp_eject(Monitor *mon, const QDict *qdict)
1687 bool force = qdict_get_try_bool(qdict, "force", false);
1688 const char *device = qdict_get_str(qdict, "device");
1689 Error *err = NULL;
1691 qmp_eject(true, device, false, NULL, true, force, &err);
1692 hmp_handle_error(mon, &err);
1695 static void hmp_change_read_arg(void *opaque, const char *password,
1696 void *readline_opaque)
1698 qmp_change_vnc_password(password, NULL);
1699 monitor_read_command(opaque, 1);
1702 void hmp_change(Monitor *mon, const QDict *qdict)
1704 const char *device = qdict_get_str(qdict, "device");
1705 const char *target = qdict_get_str(qdict, "target");
1706 const char *arg = qdict_get_try_str(qdict, "arg");
1707 const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1708 BlockdevChangeReadOnlyMode read_only_mode = 0;
1709 Error *err = NULL;
1711 if (strcmp(device, "vnc") == 0) {
1712 if (read_only) {
1713 monitor_printf(mon,
1714 "Parameter 'read-only-mode' is invalid for VNC\n");
1715 return;
1717 if (strcmp(target, "passwd") == 0 ||
1718 strcmp(target, "password") == 0) {
1719 if (!arg) {
1720 monitor_read_password(mon, hmp_change_read_arg, NULL);
1721 return;
1724 qmp_change("vnc", target, !!arg, arg, &err);
1725 } else {
1726 if (read_only) {
1727 read_only_mode =
1728 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
1729 read_only, BLOCKDEV_CHANGE_READ_ONLY_MODE__MAX,
1730 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1731 if (err) {
1732 hmp_handle_error(mon, &err);
1733 return;
1737 qmp_blockdev_change_medium(true, device, false, NULL, target,
1738 !!arg, arg, !!read_only, read_only_mode,
1739 &err);
1740 if (err &&
1741 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1742 error_free(err);
1743 monitor_read_block_device_key(mon, device, NULL, NULL);
1744 return;
1748 hmp_handle_error(mon, &err);
1751 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1753 Error *err = NULL;
1754 BlockIOThrottle throttle = {
1755 .has_device = true,
1756 .device = (char *) qdict_get_str(qdict, "device"),
1757 .bps = qdict_get_int(qdict, "bps"),
1758 .bps_rd = qdict_get_int(qdict, "bps_rd"),
1759 .bps_wr = qdict_get_int(qdict, "bps_wr"),
1760 .iops = qdict_get_int(qdict, "iops"),
1761 .iops_rd = qdict_get_int(qdict, "iops_rd"),
1762 .iops_wr = qdict_get_int(qdict, "iops_wr"),
1765 qmp_block_set_io_throttle(&throttle, &err);
1766 hmp_handle_error(mon, &err);
1769 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1771 Error *error = NULL;
1772 const char *device = qdict_get_str(qdict, "device");
1773 const char *base = qdict_get_try_str(qdict, "base");
1774 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1776 qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
1777 false, NULL, qdict_haskey(qdict, "speed"), speed,
1778 true, BLOCKDEV_ON_ERROR_REPORT, &error);
1780 hmp_handle_error(mon, &error);
1783 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1785 Error *error = NULL;
1786 const char *device = qdict_get_str(qdict, "device");
1787 int64_t value = qdict_get_int(qdict, "speed");
1789 qmp_block_job_set_speed(device, value, &error);
1791 hmp_handle_error(mon, &error);
1794 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1796 Error *error = NULL;
1797 const char *device = qdict_get_str(qdict, "device");
1798 bool force = qdict_get_try_bool(qdict, "force", false);
1800 qmp_block_job_cancel(device, true, force, &error);
1802 hmp_handle_error(mon, &error);
1805 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1807 Error *error = NULL;
1808 const char *device = qdict_get_str(qdict, "device");
1810 qmp_block_job_pause(device, &error);
1812 hmp_handle_error(mon, &error);
1815 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1817 Error *error = NULL;
1818 const char *device = qdict_get_str(qdict, "device");
1820 qmp_block_job_resume(device, &error);
1822 hmp_handle_error(mon, &error);
1825 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1827 Error *error = NULL;
1828 const char *device = qdict_get_str(qdict, "device");
1830 qmp_block_job_complete(device, &error);
1832 hmp_handle_error(mon, &error);
1835 typedef struct HMPMigrationStatus
1837 QEMUTimer *timer;
1838 Monitor *mon;
1839 bool is_block_migration;
1840 } HMPMigrationStatus;
1842 static void hmp_migrate_status_cb(void *opaque)
1844 HMPMigrationStatus *status = opaque;
1845 MigrationInfo *info;
1847 info = qmp_query_migrate(NULL);
1848 if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1849 info->status == MIGRATION_STATUS_SETUP) {
1850 if (info->has_disk) {
1851 int progress;
1853 if (info->disk->remaining) {
1854 progress = info->disk->transferred * 100 / info->disk->total;
1855 } else {
1856 progress = 100;
1859 monitor_printf(status->mon, "Completed %d %%\r", progress);
1860 monitor_flush(status->mon);
1863 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1864 } else {
1865 if (status->is_block_migration) {
1866 monitor_printf(status->mon, "\n");
1868 if (info->has_error_desc) {
1869 error_report("%s", info->error_desc);
1871 monitor_resume(status->mon);
1872 timer_del(status->timer);
1873 g_free(status);
1876 qapi_free_MigrationInfo(info);
1879 void hmp_migrate(Monitor *mon, const QDict *qdict)
1881 bool detach = qdict_get_try_bool(qdict, "detach", false);
1882 bool blk = qdict_get_try_bool(qdict, "blk", false);
1883 bool inc = qdict_get_try_bool(qdict, "inc", false);
1884 const char *uri = qdict_get_str(qdict, "uri");
1885 Error *err = NULL;
1887 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1888 if (err) {
1889 error_report_err(err);
1890 return;
1893 if (!detach) {
1894 HMPMigrationStatus *status;
1896 if (monitor_suspend(mon) < 0) {
1897 monitor_printf(mon, "terminal does not allow synchronous "
1898 "migration, continuing detached\n");
1899 return;
1902 status = g_malloc0(sizeof(*status));
1903 status->mon = mon;
1904 status->is_block_migration = blk || inc;
1905 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1906 status);
1907 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1911 void hmp_device_add(Monitor *mon, const QDict *qdict)
1913 Error *err = NULL;
1915 qmp_device_add((QDict *)qdict, NULL, &err);
1916 hmp_handle_error(mon, &err);
1919 void hmp_device_del(Monitor *mon, const QDict *qdict)
1921 const char *id = qdict_get_str(qdict, "id");
1922 Error *err = NULL;
1924 qmp_device_del(id, &err);
1925 hmp_handle_error(mon, &err);
1928 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1930 Error *err = NULL;
1931 bool paging = qdict_get_try_bool(qdict, "paging", false);
1932 bool zlib = qdict_get_try_bool(qdict, "zlib", false);
1933 bool lzo = qdict_get_try_bool(qdict, "lzo", false);
1934 bool snappy = qdict_get_try_bool(qdict, "snappy", false);
1935 const char *file = qdict_get_str(qdict, "filename");
1936 bool has_begin = qdict_haskey(qdict, "begin");
1937 bool has_length = qdict_haskey(qdict, "length");
1938 bool has_detach = qdict_haskey(qdict, "detach");
1939 int64_t begin = 0;
1940 int64_t length = 0;
1941 bool detach = false;
1942 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1943 char *prot;
1945 if (zlib + lzo + snappy > 1) {
1946 error_setg(&err, "only one of '-z|-l|-s' can be set");
1947 hmp_handle_error(mon, &err);
1948 return;
1951 if (zlib) {
1952 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1955 if (lzo) {
1956 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1959 if (snappy) {
1960 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1963 if (has_begin) {
1964 begin = qdict_get_int(qdict, "begin");
1966 if (has_length) {
1967 length = qdict_get_int(qdict, "length");
1969 if (has_detach) {
1970 detach = qdict_get_bool(qdict, "detach");
1973 prot = g_strconcat("file:", file, NULL);
1975 qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
1976 has_length, length, true, dump_format, &err);
1977 hmp_handle_error(mon, &err);
1978 g_free(prot);
1981 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1983 Error *err = NULL;
1984 QemuOpts *opts;
1986 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1987 if (err) {
1988 goto out;
1991 netdev_add(opts, &err);
1992 if (err) {
1993 qemu_opts_del(opts);
1996 out:
1997 hmp_handle_error(mon, &err);
2000 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2002 const char *id = qdict_get_str(qdict, "id");
2003 Error *err = NULL;
2005 qmp_netdev_del(id, &err);
2006 hmp_handle_error(mon, &err);
2009 void hmp_object_add(Monitor *mon, const QDict *qdict)
2011 Error *err = NULL;
2012 QemuOpts *opts;
2013 Object *obj = NULL;
2015 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2016 if (err) {
2017 hmp_handle_error(mon, &err);
2018 return;
2021 obj = user_creatable_add_opts(opts, &err);
2022 qemu_opts_del(opts);
2024 if (err) {
2025 hmp_handle_error(mon, &err);
2027 if (obj) {
2028 object_unref(obj);
2032 void hmp_getfd(Monitor *mon, const QDict *qdict)
2034 const char *fdname = qdict_get_str(qdict, "fdname");
2035 Error *err = NULL;
2037 qmp_getfd(fdname, &err);
2038 hmp_handle_error(mon, &err);
2041 void hmp_closefd(Monitor *mon, const QDict *qdict)
2043 const char *fdname = qdict_get_str(qdict, "fdname");
2044 Error *err = NULL;
2046 qmp_closefd(fdname, &err);
2047 hmp_handle_error(mon, &err);
2050 void hmp_sendkey(Monitor *mon, const QDict *qdict)
2052 const char *keys = qdict_get_str(qdict, "keys");
2053 KeyValueList *keylist, *head = NULL, *tmp = NULL;
2054 int has_hold_time = qdict_haskey(qdict, "hold-time");
2055 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2056 Error *err = NULL;
2057 char *separator;
2058 int keyname_len;
2060 while (1) {
2061 separator = strchr(keys, '-');
2062 keyname_len = separator ? separator - keys : strlen(keys);
2064 /* Be compatible with old interface, convert user inputted "<" */
2065 if (keys[0] == '<' && keyname_len == 1) {
2066 keys = "less";
2067 keyname_len = 4;
2070 keylist = g_malloc0(sizeof(*keylist));
2071 keylist->value = g_malloc0(sizeof(*keylist->value));
2073 if (!head) {
2074 head = keylist;
2076 if (tmp) {
2077 tmp->next = keylist;
2079 tmp = keylist;
2081 if (strstart(keys, "0x", NULL)) {
2082 char *endp;
2083 int value = strtoul(keys, &endp, 0);
2084 assert(endp <= keys + keyname_len);
2085 if (endp != keys + keyname_len) {
2086 goto err_out;
2088 keylist->value->type = KEY_VALUE_KIND_NUMBER;
2089 keylist->value->u.number.data = value;
2090 } else {
2091 int idx = index_from_key(keys, keyname_len);
2092 if (idx == Q_KEY_CODE__MAX) {
2093 goto err_out;
2095 keylist->value->type = KEY_VALUE_KIND_QCODE;
2096 keylist->value->u.qcode.data = idx;
2099 if (!separator) {
2100 break;
2102 keys = separator + 1;
2105 qmp_send_key(head, has_hold_time, hold_time, &err);
2106 hmp_handle_error(mon, &err);
2108 out:
2109 qapi_free_KeyValueList(head);
2110 return;
2112 err_out:
2113 monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
2114 goto out;
2117 void hmp_screendump(Monitor *mon, const QDict *qdict)
2119 const char *filename = qdict_get_str(qdict, "filename");
2120 Error *err = NULL;
2122 qmp_screendump(filename, &err);
2123 hmp_handle_error(mon, &err);
2126 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2128 const char *uri = qdict_get_str(qdict, "uri");
2129 bool writable = qdict_get_try_bool(qdict, "writable", false);
2130 bool all = qdict_get_try_bool(qdict, "all", false);
2131 Error *local_err = NULL;
2132 BlockInfoList *block_list, *info;
2133 SocketAddress *addr;
2135 if (writable && !all) {
2136 error_setg(&local_err, "-w only valid together with -a");
2137 goto exit;
2140 /* First check if the address is valid and start the server. */
2141 addr = socket_parse(uri, &local_err);
2142 if (local_err != NULL) {
2143 goto exit;
2146 nbd_server_start(addr, NULL, &local_err);
2147 qapi_free_SocketAddress(addr);
2148 if (local_err != NULL) {
2149 goto exit;
2152 if (!all) {
2153 return;
2156 /* Then try adding all block devices. If one fails, close all and
2157 * exit.
2159 block_list = qmp_query_block(NULL);
2161 for (info = block_list; info; info = info->next) {
2162 if (!info->value->has_inserted) {
2163 continue;
2166 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
2168 if (local_err != NULL) {
2169 qmp_nbd_server_stop(NULL);
2170 break;
2174 qapi_free_BlockInfoList(block_list);
2176 exit:
2177 hmp_handle_error(mon, &local_err);
2180 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2182 const char *device = qdict_get_str(qdict, "device");
2183 bool writable = qdict_get_try_bool(qdict, "writable", false);
2184 Error *local_err = NULL;
2186 qmp_nbd_server_add(device, true, writable, &local_err);
2188 if (local_err != NULL) {
2189 hmp_handle_error(mon, &local_err);
2193 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2195 Error *err = NULL;
2197 qmp_nbd_server_stop(&err);
2198 hmp_handle_error(mon, &err);
2201 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2203 int cpuid;
2204 Error *err = NULL;
2206 cpuid = qdict_get_int(qdict, "id");
2207 qmp_cpu_add(cpuid, &err);
2208 hmp_handle_error(mon, &err);
2211 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2213 const char *args = qdict_get_str(qdict, "args");
2214 Error *err = NULL;
2215 QemuOpts *opts;
2217 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2218 if (opts == NULL) {
2219 error_setg(&err, "Parsing chardev args failed");
2220 } else {
2221 qemu_chr_new_from_opts(opts, &err);
2222 qemu_opts_del(opts);
2224 hmp_handle_error(mon, &err);
2227 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2229 Error *local_err = NULL;
2231 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2232 hmp_handle_error(mon, &local_err);
2235 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2237 BlockBackend *blk;
2238 BlockBackend *local_blk = NULL;
2239 AioContext *aio_context;
2240 const char* device = qdict_get_str(qdict, "device");
2241 const char* command = qdict_get_str(qdict, "command");
2242 Error *err = NULL;
2243 int ret;
2245 blk = blk_by_name(device);
2246 if (!blk) {
2247 BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2248 if (bs) {
2249 blk = local_blk = blk_new(0, BLK_PERM_ALL);
2250 ret = blk_insert_bs(blk, bs, &err);
2251 if (ret < 0) {
2252 goto fail;
2254 } else {
2255 goto fail;
2259 aio_context = blk_get_aio_context(blk);
2260 aio_context_acquire(aio_context);
2263 * Notably absent: Proper permission management. This is sad, but it seems
2264 * almost impossible to achieve without changing the semantics and thereby
2265 * limiting the use cases of the qemu-io HMP command.
2267 * In an ideal world we would unconditionally create a new BlockBackend for
2268 * qemuio_command(), but we have commands like 'reopen' and want them to
2269 * take effect on the exact BlockBackend whose name the user passed instead
2270 * of just on a temporary copy of it.
2272 * Another problem is that deleting the temporary BlockBackend involves
2273 * draining all requests on it first, but some qemu-iotests cases want to
2274 * issue multiple aio_read/write requests and expect them to complete in
2275 * the background while the monitor has already returned.
2277 * This is also what prevents us from saving the original permissions and
2278 * restoring them later: We can't revoke permissions until all requests
2279 * have completed, and we don't know when that is nor can we really let
2280 * anything else run before we have revoken them to avoid race conditions.
2282 * What happens now is that command() in qemu-io-cmds.c can extend the
2283 * permissions if necessary for the qemu-io command. And they simply stay
2284 * extended, possibly resulting in a read-only guest device keeping write
2285 * permissions. Ugly, but it appears to be the lesser evil.
2287 qemuio_command(blk, command);
2289 aio_context_release(aio_context);
2291 fail:
2292 blk_unref(local_blk);
2293 hmp_handle_error(mon, &err);
2296 void hmp_object_del(Monitor *mon, const QDict *qdict)
2298 const char *id = qdict_get_str(qdict, "id");
2299 Error *err = NULL;
2301 user_creatable_del(id, &err);
2302 hmp_handle_error(mon, &err);
2305 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2307 Error *err = NULL;
2308 MemdevList *memdev_list = qmp_query_memdev(&err);
2309 MemdevList *m = memdev_list;
2310 Visitor *v;
2311 char *str;
2313 while (m) {
2314 v = string_output_visitor_new(false, &str);
2315 visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2316 monitor_printf(mon, "memory backend: %s\n", m->value->id);
2317 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
2318 monitor_printf(mon, " merge: %s\n",
2319 m->value->merge ? "true" : "false");
2320 monitor_printf(mon, " dump: %s\n",
2321 m->value->dump ? "true" : "false");
2322 monitor_printf(mon, " prealloc: %s\n",
2323 m->value->prealloc ? "true" : "false");
2324 monitor_printf(mon, " policy: %s\n",
2325 HostMemPolicy_lookup[m->value->policy]);
2326 visit_complete(v, &str);
2327 monitor_printf(mon, " host nodes: %s\n", str);
2329 g_free(str);
2330 visit_free(v);
2331 m = m->next;
2334 monitor_printf(mon, "\n");
2336 qapi_free_MemdevList(memdev_list);
2339 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2341 Error *err = NULL;
2342 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2343 MemoryDeviceInfoList *info;
2344 MemoryDeviceInfo *value;
2345 PCDIMMDeviceInfo *di;
2347 for (info = info_list; info; info = info->next) {
2348 value = info->value;
2350 if (value) {
2351 switch (value->type) {
2352 case MEMORY_DEVICE_INFO_KIND_DIMM:
2353 di = value->u.dimm.data;
2355 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2356 MemoryDeviceInfoKind_lookup[value->type],
2357 di->id ? di->id : "");
2358 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
2359 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
2360 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
2361 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
2362 monitor_printf(mon, " memdev: %s\n", di->memdev);
2363 monitor_printf(mon, " hotplugged: %s\n",
2364 di->hotplugged ? "true" : "false");
2365 monitor_printf(mon, " hotpluggable: %s\n",
2366 di->hotpluggable ? "true" : "false");
2367 break;
2368 default:
2369 break;
2374 qapi_free_MemoryDeviceInfoList(info_list);
2377 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2379 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2380 IOThreadInfoList *info;
2381 IOThreadInfo *value;
2383 for (info = info_list; info; info = info->next) {
2384 value = info->value;
2385 monitor_printf(mon, "%s:\n", value->id);
2386 monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
2387 monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2388 monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
2389 monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
2392 qapi_free_IOThreadInfoList(info_list);
2395 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2397 const char *path = qdict_get_try_str(qdict, "path");
2398 ObjectPropertyInfoList *list;
2399 Error *err = NULL;
2401 if (path == NULL) {
2402 monitor_printf(mon, "/\n");
2403 return;
2406 list = qmp_qom_list(path, &err);
2407 if (err == NULL) {
2408 ObjectPropertyInfoList *start = list;
2409 while (list != NULL) {
2410 ObjectPropertyInfo *value = list->value;
2412 monitor_printf(mon, "%s (%s)\n",
2413 value->name, value->type);
2414 list = list->next;
2416 qapi_free_ObjectPropertyInfoList(start);
2418 hmp_handle_error(mon, &err);
2421 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2423 const char *path = qdict_get_str(qdict, "path");
2424 const char *property = qdict_get_str(qdict, "property");
2425 const char *value = qdict_get_str(qdict, "value");
2426 Error *err = NULL;
2427 bool ambiguous = false;
2428 Object *obj;
2430 obj = object_resolve_path(path, &ambiguous);
2431 if (obj == NULL) {
2432 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2433 "Device '%s' not found", path);
2434 } else {
2435 if (ambiguous) {
2436 monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2438 object_property_parse(obj, value, property, &err);
2440 hmp_handle_error(mon, &err);
2443 void hmp_rocker(Monitor *mon, const QDict *qdict)
2445 const char *name = qdict_get_str(qdict, "name");
2446 RockerSwitch *rocker;
2447 Error *err = NULL;
2449 rocker = qmp_query_rocker(name, &err);
2450 if (err != NULL) {
2451 hmp_handle_error(mon, &err);
2452 return;
2455 monitor_printf(mon, "name: %s\n", rocker->name);
2456 monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2457 monitor_printf(mon, "ports: %d\n", rocker->ports);
2459 qapi_free_RockerSwitch(rocker);
2462 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2464 RockerPortList *list, *port;
2465 const char *name = qdict_get_str(qdict, "name");
2466 Error *err = NULL;
2468 list = qmp_query_rocker_ports(name, &err);
2469 if (err != NULL) {
2470 hmp_handle_error(mon, &err);
2471 return;
2474 monitor_printf(mon, " ena/ speed/ auto\n");
2475 monitor_printf(mon, " port link duplex neg?\n");
2477 for (port = list; port; port = port->next) {
2478 monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
2479 port->value->name,
2480 port->value->enabled ? port->value->link_up ?
2481 "up" : "down" : "!ena",
2482 port->value->speed == 10000 ? "10G" : "??",
2483 port->value->duplex ? "FD" : "HD",
2484 port->value->autoneg ? "Yes" : "No");
2487 qapi_free_RockerPortList(list);
2490 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2492 RockerOfDpaFlowList *list, *info;
2493 const char *name = qdict_get_str(qdict, "name");
2494 uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2495 Error *err = NULL;
2497 list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2498 if (err != NULL) {
2499 hmp_handle_error(mon, &err);
2500 return;
2503 monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2505 for (info = list; info; info = info->next) {
2506 RockerOfDpaFlow *flow = info->value;
2507 RockerOfDpaFlowKey *key = flow->key;
2508 RockerOfDpaFlowMask *mask = flow->mask;
2509 RockerOfDpaFlowAction *action = flow->action;
2511 if (flow->hits) {
2512 monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2513 key->priority, key->tbl_id, flow->hits);
2514 } else {
2515 monitor_printf(mon, "%-4d %-3d ",
2516 key->priority, key->tbl_id);
2519 if (key->has_in_pport) {
2520 monitor_printf(mon, " pport %d", key->in_pport);
2521 if (mask->has_in_pport) {
2522 monitor_printf(mon, "(0x%x)", mask->in_pport);
2526 if (key->has_vlan_id) {
2527 monitor_printf(mon, " vlan %d",
2528 key->vlan_id & VLAN_VID_MASK);
2529 if (mask->has_vlan_id) {
2530 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2534 if (key->has_tunnel_id) {
2535 monitor_printf(mon, " tunnel %d", key->tunnel_id);
2536 if (mask->has_tunnel_id) {
2537 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2541 if (key->has_eth_type) {
2542 switch (key->eth_type) {
2543 case 0x0806:
2544 monitor_printf(mon, " ARP");
2545 break;
2546 case 0x0800:
2547 monitor_printf(mon, " IP");
2548 break;
2549 case 0x86dd:
2550 monitor_printf(mon, " IPv6");
2551 break;
2552 case 0x8809:
2553 monitor_printf(mon, " LACP");
2554 break;
2555 case 0x88cc:
2556 monitor_printf(mon, " LLDP");
2557 break;
2558 default:
2559 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2560 break;
2564 if (key->has_eth_src) {
2565 if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2566 (mask->has_eth_src) &&
2567 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2568 monitor_printf(mon, " src <any mcast/bcast>");
2569 } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2570 (mask->has_eth_src) &&
2571 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2572 monitor_printf(mon, " src <any ucast>");
2573 } else {
2574 monitor_printf(mon, " src %s", key->eth_src);
2575 if (mask->has_eth_src) {
2576 monitor_printf(mon, "(%s)", mask->eth_src);
2581 if (key->has_eth_dst) {
2582 if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2583 (mask->has_eth_dst) &&
2584 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2585 monitor_printf(mon, " dst <any mcast/bcast>");
2586 } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2587 (mask->has_eth_dst) &&
2588 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2589 monitor_printf(mon, " dst <any ucast>");
2590 } else {
2591 monitor_printf(mon, " dst %s", key->eth_dst);
2592 if (mask->has_eth_dst) {
2593 monitor_printf(mon, "(%s)", mask->eth_dst);
2598 if (key->has_ip_proto) {
2599 monitor_printf(mon, " proto %d", key->ip_proto);
2600 if (mask->has_ip_proto) {
2601 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2605 if (key->has_ip_tos) {
2606 monitor_printf(mon, " TOS %d", key->ip_tos);
2607 if (mask->has_ip_tos) {
2608 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2612 if (key->has_ip_dst) {
2613 monitor_printf(mon, " dst %s", key->ip_dst);
2616 if (action->has_goto_tbl || action->has_group_id ||
2617 action->has_new_vlan_id) {
2618 monitor_printf(mon, " -->");
2621 if (action->has_new_vlan_id) {
2622 monitor_printf(mon, " apply new vlan %d",
2623 ntohs(action->new_vlan_id));
2626 if (action->has_group_id) {
2627 monitor_printf(mon, " write group 0x%08x", action->group_id);
2630 if (action->has_goto_tbl) {
2631 monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2634 monitor_printf(mon, "\n");
2637 qapi_free_RockerOfDpaFlowList(list);
2640 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2642 RockerOfDpaGroupList *list, *g;
2643 const char *name = qdict_get_str(qdict, "name");
2644 uint8_t type = qdict_get_try_int(qdict, "type", 9);
2645 Error *err = NULL;
2646 bool set = false;
2648 list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2649 if (err != NULL) {
2650 hmp_handle_error(mon, &err);
2651 return;
2654 monitor_printf(mon, "id (decode) --> buckets\n");
2656 for (g = list; g; g = g->next) {
2657 RockerOfDpaGroup *group = g->value;
2659 monitor_printf(mon, "0x%08x", group->id);
2661 monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2662 group->type == 1 ? "L2 rewrite" :
2663 group->type == 2 ? "L3 unicast" :
2664 group->type == 3 ? "L2 multicast" :
2665 group->type == 4 ? "L2 flood" :
2666 group->type == 5 ? "L3 interface" :
2667 group->type == 6 ? "L3 multicast" :
2668 group->type == 7 ? "L3 ECMP" :
2669 group->type == 8 ? "L2 overlay" :
2670 "unknown");
2672 if (group->has_vlan_id) {
2673 monitor_printf(mon, " vlan %d", group->vlan_id);
2676 if (group->has_pport) {
2677 monitor_printf(mon, " pport %d", group->pport);
2680 if (group->has_index) {
2681 monitor_printf(mon, " index %d", group->index);
2684 monitor_printf(mon, ") -->");
2686 if (group->has_set_vlan_id && group->set_vlan_id) {
2687 set = true;
2688 monitor_printf(mon, " set vlan %d",
2689 group->set_vlan_id & VLAN_VID_MASK);
2692 if (group->has_set_eth_src) {
2693 if (!set) {
2694 set = true;
2695 monitor_printf(mon, " set");
2697 monitor_printf(mon, " src %s", group->set_eth_src);
2700 if (group->has_set_eth_dst) {
2701 if (!set) {
2702 set = true;
2703 monitor_printf(mon, " set");
2705 monitor_printf(mon, " dst %s", group->set_eth_dst);
2708 set = false;
2710 if (group->has_ttl_check && group->ttl_check) {
2711 monitor_printf(mon, " check TTL");
2714 if (group->has_group_id && group->group_id) {
2715 monitor_printf(mon, " group id 0x%08x", group->group_id);
2718 if (group->has_pop_vlan && group->pop_vlan) {
2719 monitor_printf(mon, " pop vlan");
2722 if (group->has_out_pport) {
2723 monitor_printf(mon, " out pport %d", group->out_pport);
2726 if (group->has_group_ids) {
2727 struct uint32List *id;
2729 monitor_printf(mon, " groups [");
2730 for (id = group->group_ids; id; id = id->next) {
2731 monitor_printf(mon, "0x%08x", id->value);
2732 if (id->next) {
2733 monitor_printf(mon, ",");
2736 monitor_printf(mon, "]");
2739 monitor_printf(mon, "\n");
2742 qapi_free_RockerOfDpaGroupList(list);
2745 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2747 DumpQueryResult *result = qmp_query_dump(NULL);
2749 assert(result && result->status < DUMP_STATUS__MAX);
2750 monitor_printf(mon, "Status: %s\n", DumpStatus_lookup[result->status]);
2752 if (result->status == DUMP_STATUS_ACTIVE) {
2753 float percent = 0;
2754 assert(result->total != 0);
2755 percent = 100.0 * result->completed / result->total;
2756 monitor_printf(mon, "Finished: %.2f %%\n", percent);
2759 qapi_free_DumpQueryResult(result);
2762 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2764 ram_block_dump(mon);
2767 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2769 Error *err = NULL;
2770 HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2771 HotpluggableCPUList *saved = l;
2772 CpuInstanceProperties *c;
2774 if (err != NULL) {
2775 hmp_handle_error(mon, &err);
2776 return;
2779 monitor_printf(mon, "Hotpluggable CPUs:\n");
2780 while (l) {
2781 monitor_printf(mon, " type: \"%s\"\n", l->value->type);
2782 monitor_printf(mon, " vcpus_count: \"%" PRIu64 "\"\n",
2783 l->value->vcpus_count);
2784 if (l->value->has_qom_path) {
2785 monitor_printf(mon, " qom_path: \"%s\"\n", l->value->qom_path);
2788 c = l->value->props;
2789 monitor_printf(mon, " CPUInstance Properties:\n");
2790 if (c->has_node_id) {
2791 monitor_printf(mon, " node-id: \"%" PRIu64 "\"\n", c->node_id);
2793 if (c->has_socket_id) {
2794 monitor_printf(mon, " socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2796 if (c->has_core_id) {
2797 monitor_printf(mon, " core-id: \"%" PRIu64 "\"\n", c->core_id);
2799 if (c->has_thread_id) {
2800 monitor_printf(mon, " thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2803 l = l->next;
2806 qapi_free_HotpluggableCPUList(saved);
2809 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2811 Error *err = NULL;
2812 GuidInfo *info = qmp_query_vm_generation_id(&err);
2813 if (info) {
2814 monitor_printf(mon, "%s\n", info->guid);
2816 hmp_handle_error(mon, &err);
2817 qapi_free_GuidInfo(info);