tcg: consistently access cpu->tb_jmp_cache atomically
[qemu/ar7.git] / hmp.c
blobdee40284c16f0cd50e1df9a7411b3b1b10cc797b
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 "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 "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"
45 #include "migration/snapshot.h"
46 #include "migration/misc.h"
48 #ifdef CONFIG_SPICE
49 #include <spice/enums.h>
50 #endif
52 static void hmp_handle_error(Monitor *mon, Error **errp)
54 assert(errp);
55 if (*errp) {
56 error_report_err(*errp);
60 void hmp_info_name(Monitor *mon, const QDict *qdict)
62 NameInfo *info;
64 info = qmp_query_name(NULL);
65 if (info->has_name) {
66 monitor_printf(mon, "%s\n", info->name);
68 qapi_free_NameInfo(info);
71 void hmp_info_version(Monitor *mon, const QDict *qdict)
73 VersionInfo *info;
75 info = qmp_query_version(NULL);
77 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
78 info->qemu->major, info->qemu->minor, info->qemu->micro,
79 info->package);
81 qapi_free_VersionInfo(info);
84 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
86 KvmInfo *info;
88 info = qmp_query_kvm(NULL);
89 monitor_printf(mon, "kvm support: ");
90 if (info->present) {
91 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
92 } else {
93 monitor_printf(mon, "not compiled\n");
96 qapi_free_KvmInfo(info);
99 void hmp_info_status(Monitor *mon, const QDict *qdict)
101 StatusInfo *info;
103 info = qmp_query_status(NULL);
105 monitor_printf(mon, "VM status: %s%s",
106 info->running ? "running" : "paused",
107 info->singlestep ? " (single step mode)" : "");
109 if (!info->running && info->status != RUN_STATE_PAUSED) {
110 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
113 monitor_printf(mon, "\n");
115 qapi_free_StatusInfo(info);
118 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
120 UuidInfo *info;
122 info = qmp_query_uuid(NULL);
123 monitor_printf(mon, "%s\n", info->UUID);
124 qapi_free_UuidInfo(info);
127 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
129 ChardevInfoList *char_info, *info;
131 char_info = qmp_query_chardev(NULL);
132 for (info = char_info; info; info = info->next) {
133 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
134 info->value->filename);
137 qapi_free_ChardevInfoList(char_info);
140 void hmp_info_mice(Monitor *mon, const QDict *qdict)
142 MouseInfoList *mice_list, *mouse;
144 mice_list = qmp_query_mice(NULL);
145 if (!mice_list) {
146 monitor_printf(mon, "No mouse devices connected\n");
147 return;
150 for (mouse = mice_list; mouse; mouse = mouse->next) {
151 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
152 mouse->value->current ? '*' : ' ',
153 mouse->value->index, mouse->value->name,
154 mouse->value->absolute ? " (absolute)" : "");
157 qapi_free_MouseInfoList(mice_list);
160 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
162 MigrationInfo *info;
163 MigrationCapabilityStatusList *caps, *cap;
165 info = qmp_query_migrate(NULL);
166 caps = qmp_query_migrate_capabilities(NULL);
168 migration_global_dump(mon);
170 /* do not display parameters during setup */
171 if (info->has_status && caps) {
172 monitor_printf(mon, "capabilities: ");
173 for (cap = caps; cap; cap = cap->next) {
174 monitor_printf(mon, "%s: %s ",
175 MigrationCapability_lookup[cap->value->capability],
176 cap->value->state ? "on" : "off");
178 monitor_printf(mon, "\n");
181 if (info->has_status) {
182 monitor_printf(mon, "Migration status: %s",
183 MigrationStatus_lookup[info->status]);
184 if (info->status == MIGRATION_STATUS_FAILED &&
185 info->has_error_desc) {
186 monitor_printf(mon, " (%s)\n", info->error_desc);
187 } else {
188 monitor_printf(mon, "\n");
191 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
192 info->total_time);
193 if (info->has_expected_downtime) {
194 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
195 info->expected_downtime);
197 if (info->has_downtime) {
198 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
199 info->downtime);
201 if (info->has_setup_time) {
202 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
203 info->setup_time);
207 if (info->has_ram) {
208 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
209 info->ram->transferred >> 10);
210 monitor_printf(mon, "throughput: %0.2f mbps\n",
211 info->ram->mbps);
212 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
213 info->ram->remaining >> 10);
214 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
215 info->ram->total >> 10);
216 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
217 info->ram->duplicate);
218 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
219 info->ram->skipped);
220 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
221 info->ram->normal);
222 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
223 info->ram->normal_bytes >> 10);
224 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
225 info->ram->dirty_sync_count);
226 monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
227 info->ram->page_size >> 10);
229 if (info->ram->dirty_pages_rate) {
230 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
231 info->ram->dirty_pages_rate);
233 if (info->ram->postcopy_requests) {
234 monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
235 info->ram->postcopy_requests);
239 if (info->has_disk) {
240 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
241 info->disk->transferred >> 10);
242 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
243 info->disk->remaining >> 10);
244 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
245 info->disk->total >> 10);
248 if (info->has_xbzrle_cache) {
249 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
250 info->xbzrle_cache->cache_size);
251 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
252 info->xbzrle_cache->bytes >> 10);
253 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
254 info->xbzrle_cache->pages);
255 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
256 info->xbzrle_cache->cache_miss);
257 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
258 info->xbzrle_cache->cache_miss_rate);
259 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
260 info->xbzrle_cache->overflow);
263 if (info->has_cpu_throttle_percentage) {
264 monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
265 info->cpu_throttle_percentage);
268 qapi_free_MigrationInfo(info);
269 qapi_free_MigrationCapabilityStatusList(caps);
272 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
274 MigrationCapabilityStatusList *caps, *cap;
276 caps = qmp_query_migrate_capabilities(NULL);
278 if (caps) {
279 for (cap = caps; cap; cap = cap->next) {
280 monitor_printf(mon, "%s: %s\n",
281 MigrationCapability_lookup[cap->value->capability],
282 cap->value->state ? "on" : "off");
286 qapi_free_MigrationCapabilityStatusList(caps);
289 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
291 MigrationParameters *params;
293 params = qmp_query_migrate_parameters(NULL);
295 if (params) {
296 assert(params->has_compress_level);
297 monitor_printf(mon, "%s: %" PRId64 "\n",
298 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
299 params->compress_level);
300 assert(params->has_compress_threads);
301 monitor_printf(mon, "%s: %" PRId64 "\n",
302 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
303 params->compress_threads);
304 assert(params->has_decompress_threads);
305 monitor_printf(mon, "%s: %" PRId64 "\n",
306 MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
307 params->decompress_threads);
308 assert(params->has_cpu_throttle_initial);
309 monitor_printf(mon, "%s: %" PRId64 "\n",
310 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL],
311 params->cpu_throttle_initial);
312 assert(params->has_cpu_throttle_increment);
313 monitor_printf(mon, "%s: %" PRId64 "\n",
314 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT],
315 params->cpu_throttle_increment);
316 monitor_printf(mon, "%s: '%s'\n",
317 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_CREDS],
318 params->has_tls_creds ? params->tls_creds : "");
319 monitor_printf(mon, "%s: '%s'\n",
320 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_HOSTNAME],
321 params->has_tls_hostname ? params->tls_hostname : "");
322 assert(params->has_max_bandwidth);
323 monitor_printf(mon, "%s: %" PRId64 " bytes/second\n",
324 MigrationParameter_lookup[MIGRATION_PARAMETER_MAX_BANDWIDTH],
325 params->max_bandwidth);
326 assert(params->has_downtime_limit);
327 monitor_printf(mon, "%s: %" PRId64 " milliseconds\n",
328 MigrationParameter_lookup[MIGRATION_PARAMETER_DOWNTIME_LIMIT],
329 params->downtime_limit);
330 assert(params->has_x_checkpoint_delay);
331 monitor_printf(mon, "%s: %" PRId64 "\n",
332 MigrationParameter_lookup[MIGRATION_PARAMETER_X_CHECKPOINT_DELAY],
333 params->x_checkpoint_delay);
334 assert(params->has_block_incremental);
335 monitor_printf(mon, "%s: %s\n",
336 MigrationParameter_lookup[MIGRATION_PARAMETER_BLOCK_INCREMENTAL],
337 params->block_incremental ? "on" : "off");
340 qapi_free_MigrationParameters(params);
343 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
345 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
346 qmp_query_migrate_cache_size(NULL) >> 10);
349 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
351 CpuInfoList *cpu_list, *cpu;
353 cpu_list = qmp_query_cpus(NULL);
355 for (cpu = cpu_list; cpu; cpu = cpu->next) {
356 int active = ' ';
358 if (cpu->value->CPU == monitor_get_cpu_index()) {
359 active = '*';
362 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
364 switch (cpu->value->arch) {
365 case CPU_INFO_ARCH_X86:
366 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->u.x86.pc);
367 break;
368 case CPU_INFO_ARCH_PPC:
369 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->u.ppc.nip);
370 break;
371 case CPU_INFO_ARCH_SPARC:
372 monitor_printf(mon, " pc=0x%016" PRIx64,
373 cpu->value->u.q_sparc.pc);
374 monitor_printf(mon, " npc=0x%016" PRIx64,
375 cpu->value->u.q_sparc.npc);
376 break;
377 case CPU_INFO_ARCH_MIPS:
378 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.q_mips.PC);
379 break;
380 case CPU_INFO_ARCH_TRICORE:
381 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.tricore.PC);
382 break;
383 default:
384 break;
387 if (cpu->value->halted) {
388 monitor_printf(mon, " (halted)");
391 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
394 qapi_free_CpuInfoList(cpu_list);
397 static void print_block_info(Monitor *mon, BlockInfo *info,
398 BlockDeviceInfo *inserted, bool verbose)
400 ImageInfo *image_info;
402 assert(!info || !info->has_inserted || info->inserted == inserted);
404 if (info) {
405 monitor_printf(mon, "%s", info->device);
406 if (inserted && inserted->has_node_name) {
407 monitor_printf(mon, " (%s)", inserted->node_name);
409 } else {
410 assert(inserted);
411 monitor_printf(mon, "%s",
412 inserted->has_node_name
413 ? inserted->node_name
414 : "<anonymous>");
417 if (inserted) {
418 monitor_printf(mon, ": %s (%s%s%s)\n",
419 inserted->file,
420 inserted->drv,
421 inserted->ro ? ", read-only" : "",
422 inserted->encrypted ? ", encrypted" : "");
423 } else {
424 monitor_printf(mon, ": [not inserted]\n");
427 if (info) {
428 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
429 monitor_printf(mon, " I/O status: %s\n",
430 BlockDeviceIoStatus_lookup[info->io_status]);
433 if (info->removable) {
434 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
435 info->locked ? "" : "not ",
436 info->tray_open ? "open" : "closed");
441 if (!inserted) {
442 return;
445 monitor_printf(mon, " Cache mode: %s%s%s\n",
446 inserted->cache->writeback ? "writeback" : "writethrough",
447 inserted->cache->direct ? ", direct" : "",
448 inserted->cache->no_flush ? ", ignore flushes" : "");
450 if (inserted->has_backing_file) {
451 monitor_printf(mon,
452 " Backing file: %s "
453 "(chain depth: %" PRId64 ")\n",
454 inserted->backing_file,
455 inserted->backing_file_depth);
458 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
459 monitor_printf(mon, " Detect zeroes: %s\n",
460 BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
463 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
464 inserted->iops || inserted->iops_rd || inserted->iops_wr)
466 monitor_printf(mon, " I/O throttling: bps=%" PRId64
467 " bps_rd=%" PRId64 " bps_wr=%" PRId64
468 " bps_max=%" PRId64
469 " bps_rd_max=%" PRId64
470 " bps_wr_max=%" PRId64
471 " iops=%" PRId64 " iops_rd=%" PRId64
472 " iops_wr=%" PRId64
473 " iops_max=%" PRId64
474 " iops_rd_max=%" PRId64
475 " iops_wr_max=%" PRId64
476 " iops_size=%" PRId64
477 " group=%s\n",
478 inserted->bps,
479 inserted->bps_rd,
480 inserted->bps_wr,
481 inserted->bps_max,
482 inserted->bps_rd_max,
483 inserted->bps_wr_max,
484 inserted->iops,
485 inserted->iops_rd,
486 inserted->iops_wr,
487 inserted->iops_max,
488 inserted->iops_rd_max,
489 inserted->iops_wr_max,
490 inserted->iops_size,
491 inserted->group);
494 if (verbose) {
495 monitor_printf(mon, "\nImages:\n");
496 image_info = inserted->image;
497 while (1) {
498 bdrv_image_info_dump((fprintf_function)monitor_printf,
499 mon, image_info);
500 if (image_info->has_backing_image) {
501 image_info = image_info->backing_image;
502 } else {
503 break;
509 void hmp_info_block(Monitor *mon, const QDict *qdict)
511 BlockInfoList *block_list, *info;
512 BlockDeviceInfoList *blockdev_list, *blockdev;
513 const char *device = qdict_get_try_str(qdict, "device");
514 bool verbose = qdict_get_try_bool(qdict, "verbose", false);
515 bool nodes = qdict_get_try_bool(qdict, "nodes", false);
516 bool printed = false;
518 /* Print BlockBackend information */
519 if (!nodes) {
520 block_list = qmp_query_block(NULL);
521 } else {
522 block_list = NULL;
525 for (info = block_list; info; info = info->next) {
526 if (device && strcmp(device, info->value->device)) {
527 continue;
530 if (info != block_list) {
531 monitor_printf(mon, "\n");
534 print_block_info(mon, info->value, info->value->has_inserted
535 ? info->value->inserted : NULL,
536 verbose);
537 printed = true;
540 qapi_free_BlockInfoList(block_list);
542 if ((!device && !nodes) || printed) {
543 return;
546 /* Print node information */
547 blockdev_list = qmp_query_named_block_nodes(NULL);
548 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
549 assert(blockdev->value->has_node_name);
550 if (device && strcmp(device, blockdev->value->node_name)) {
551 continue;
554 if (blockdev != blockdev_list) {
555 monitor_printf(mon, "\n");
558 print_block_info(mon, NULL, blockdev->value, verbose);
560 qapi_free_BlockDeviceInfoList(blockdev_list);
563 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
565 BlockStatsList *stats_list, *stats;
567 stats_list = qmp_query_blockstats(false, false, NULL);
569 for (stats = stats_list; stats; stats = stats->next) {
570 if (!stats->value->has_device) {
571 continue;
574 monitor_printf(mon, "%s:", stats->value->device);
575 monitor_printf(mon, " rd_bytes=%" PRId64
576 " wr_bytes=%" PRId64
577 " rd_operations=%" PRId64
578 " wr_operations=%" PRId64
579 " flush_operations=%" PRId64
580 " wr_total_time_ns=%" PRId64
581 " rd_total_time_ns=%" PRId64
582 " flush_total_time_ns=%" PRId64
583 " rd_merged=%" PRId64
584 " wr_merged=%" PRId64
585 " idle_time_ns=%" PRId64
586 "\n",
587 stats->value->stats->rd_bytes,
588 stats->value->stats->wr_bytes,
589 stats->value->stats->rd_operations,
590 stats->value->stats->wr_operations,
591 stats->value->stats->flush_operations,
592 stats->value->stats->wr_total_time_ns,
593 stats->value->stats->rd_total_time_ns,
594 stats->value->stats->flush_total_time_ns,
595 stats->value->stats->rd_merged,
596 stats->value->stats->wr_merged,
597 stats->value->stats->idle_time_ns);
600 qapi_free_BlockStatsList(stats_list);
603 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
605 VncInfo *info;
606 Error *err = NULL;
607 VncClientInfoList *client;
609 info = qmp_query_vnc(&err);
610 if (err) {
611 error_report_err(err);
612 return;
615 if (!info->enabled) {
616 monitor_printf(mon, "Server: disabled\n");
617 goto out;
620 monitor_printf(mon, "Server:\n");
621 if (info->has_host && info->has_service) {
622 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
624 if (info->has_auth) {
625 monitor_printf(mon, " auth: %s\n", info->auth);
628 if (!info->has_clients || info->clients == NULL) {
629 monitor_printf(mon, "Client: none\n");
630 } else {
631 for (client = info->clients; client; client = client->next) {
632 monitor_printf(mon, "Client:\n");
633 monitor_printf(mon, " address: %s:%s\n",
634 client->value->host,
635 client->value->service);
636 monitor_printf(mon, " x509_dname: %s\n",
637 client->value->x509_dname ?
638 client->value->x509_dname : "none");
639 monitor_printf(mon, " username: %s\n",
640 client->value->has_sasl_username ?
641 client->value->sasl_username : "none");
645 out:
646 qapi_free_VncInfo(info);
649 #ifdef CONFIG_SPICE
650 void hmp_info_spice(Monitor *mon, const QDict *qdict)
652 SpiceChannelList *chan;
653 SpiceInfo *info;
654 const char *channel_name;
655 const char * const channel_names[] = {
656 [SPICE_CHANNEL_MAIN] = "main",
657 [SPICE_CHANNEL_DISPLAY] = "display",
658 [SPICE_CHANNEL_INPUTS] = "inputs",
659 [SPICE_CHANNEL_CURSOR] = "cursor",
660 [SPICE_CHANNEL_PLAYBACK] = "playback",
661 [SPICE_CHANNEL_RECORD] = "record",
662 [SPICE_CHANNEL_TUNNEL] = "tunnel",
663 [SPICE_CHANNEL_SMARTCARD] = "smartcard",
664 [SPICE_CHANNEL_USBREDIR] = "usbredir",
665 [SPICE_CHANNEL_PORT] = "port",
666 #if 0
667 /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
668 * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
669 * as quick fix for build failures with older versions. */
670 [SPICE_CHANNEL_WEBDAV] = "webdav",
671 #endif
674 info = qmp_query_spice(NULL);
676 if (!info->enabled) {
677 monitor_printf(mon, "Server: disabled\n");
678 goto out;
681 monitor_printf(mon, "Server:\n");
682 if (info->has_port) {
683 monitor_printf(mon, " address: %s:%" PRId64 "\n",
684 info->host, info->port);
686 if (info->has_tls_port) {
687 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
688 info->host, info->tls_port);
690 monitor_printf(mon, " migrated: %s\n",
691 info->migrated ? "true" : "false");
692 monitor_printf(mon, " auth: %s\n", info->auth);
693 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
694 monitor_printf(mon, " mouse-mode: %s\n",
695 SpiceQueryMouseMode_lookup[info->mouse_mode]);
697 if (!info->has_channels || info->channels == NULL) {
698 monitor_printf(mon, "Channels: none\n");
699 } else {
700 for (chan = info->channels; chan; chan = chan->next) {
701 monitor_printf(mon, "Channel:\n");
702 monitor_printf(mon, " address: %s:%s%s\n",
703 chan->value->host, chan->value->port,
704 chan->value->tls ? " [tls]" : "");
705 monitor_printf(mon, " session: %" PRId64 "\n",
706 chan->value->connection_id);
707 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
708 chan->value->channel_type, chan->value->channel_id);
710 channel_name = "unknown";
711 if (chan->value->channel_type > 0 &&
712 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
713 channel_names[chan->value->channel_type]) {
714 channel_name = channel_names[chan->value->channel_type];
717 monitor_printf(mon, " channel name: %s\n", channel_name);
721 out:
722 qapi_free_SpiceInfo(info);
724 #endif
726 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
728 BalloonInfo *info;
729 Error *err = NULL;
731 info = qmp_query_balloon(&err);
732 if (err) {
733 error_report_err(err);
734 return;
737 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
739 qapi_free_BalloonInfo(info);
742 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
744 PciMemoryRegionList *region;
746 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
747 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
748 dev->slot, dev->function);
749 monitor_printf(mon, " ");
751 if (dev->class_info->has_desc) {
752 monitor_printf(mon, "%s", dev->class_info->desc);
753 } else {
754 monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
757 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
758 dev->id->vendor, dev->id->device);
760 if (dev->has_irq) {
761 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
764 if (dev->has_pci_bridge) {
765 monitor_printf(mon, " BUS %" PRId64 ".\n",
766 dev->pci_bridge->bus->number);
767 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
768 dev->pci_bridge->bus->secondary);
769 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
770 dev->pci_bridge->bus->subordinate);
772 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
773 dev->pci_bridge->bus->io_range->base,
774 dev->pci_bridge->bus->io_range->limit);
776 monitor_printf(mon,
777 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
778 dev->pci_bridge->bus->memory_range->base,
779 dev->pci_bridge->bus->memory_range->limit);
781 monitor_printf(mon, " prefetchable memory range "
782 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
783 dev->pci_bridge->bus->prefetchable_range->base,
784 dev->pci_bridge->bus->prefetchable_range->limit);
787 for (region = dev->regions; region; region = region->next) {
788 uint64_t addr, size;
790 addr = region->value->address;
791 size = region->value->size;
793 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
795 if (!strcmp(region->value->type, "io")) {
796 monitor_printf(mon, "I/O at 0x%04" PRIx64
797 " [0x%04" PRIx64 "].\n",
798 addr, addr + size - 1);
799 } else {
800 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
801 " [0x%08" PRIx64 "].\n",
802 region->value->mem_type_64 ? 64 : 32,
803 region->value->prefetch ? " prefetchable" : "",
804 addr, addr + size - 1);
808 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
810 if (dev->has_pci_bridge) {
811 if (dev->pci_bridge->has_devices) {
812 PciDeviceInfoList *cdev;
813 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
814 hmp_info_pci_device(mon, cdev->value);
820 static int hmp_info_irq_foreach(Object *obj, void *opaque)
822 InterruptStatsProvider *intc;
823 InterruptStatsProviderClass *k;
824 Monitor *mon = opaque;
826 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
827 intc = INTERRUPT_STATS_PROVIDER(obj);
828 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
829 uint64_t *irq_counts;
830 unsigned int nb_irqs, i;
831 if (k->get_statistics &&
832 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
833 if (nb_irqs > 0) {
834 monitor_printf(mon, "IRQ statistics for %s:\n",
835 object_get_typename(obj));
836 for (i = 0; i < nb_irqs; i++) {
837 if (irq_counts[i] > 0) {
838 monitor_printf(mon, "%2d: %" PRId64 "\n", i,
839 irq_counts[i]);
843 } else {
844 monitor_printf(mon, "IRQ statistics not available for %s.\n",
845 object_get_typename(obj));
849 return 0;
852 void hmp_info_irq(Monitor *mon, const QDict *qdict)
854 object_child_foreach_recursive(object_get_root(),
855 hmp_info_irq_foreach, mon);
858 static int hmp_info_pic_foreach(Object *obj, void *opaque)
860 InterruptStatsProvider *intc;
861 InterruptStatsProviderClass *k;
862 Monitor *mon = opaque;
864 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
865 intc = INTERRUPT_STATS_PROVIDER(obj);
866 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
867 if (k->print_info) {
868 k->print_info(intc, mon);
869 } else {
870 monitor_printf(mon, "Interrupt controller information not available for %s.\n",
871 object_get_typename(obj));
875 return 0;
878 void hmp_info_pic(Monitor *mon, const QDict *qdict)
880 object_child_foreach_recursive(object_get_root(),
881 hmp_info_pic_foreach, mon);
884 void hmp_info_pci(Monitor *mon, const QDict *qdict)
886 PciInfoList *info_list, *info;
887 Error *err = NULL;
889 info_list = qmp_query_pci(&err);
890 if (err) {
891 monitor_printf(mon, "PCI devices not supported\n");
892 error_free(err);
893 return;
896 for (info = info_list; info; info = info->next) {
897 PciDeviceInfoList *dev;
899 for (dev = info->value->devices; dev; dev = dev->next) {
900 hmp_info_pci_device(mon, dev->value);
904 qapi_free_PciInfoList(info_list);
907 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
909 BlockJobInfoList *list;
910 Error *err = NULL;
912 list = qmp_query_block_jobs(&err);
913 assert(!err);
915 if (!list) {
916 monitor_printf(mon, "No active jobs\n");
917 return;
920 while (list) {
921 if (strcmp(list->value->type, "stream") == 0) {
922 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
923 " of %" PRId64 " bytes, speed limit %" PRId64
924 " bytes/s\n",
925 list->value->device,
926 list->value->offset,
927 list->value->len,
928 list->value->speed);
929 } else {
930 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
931 " of %" PRId64 " bytes, speed limit %" PRId64
932 " bytes/s\n",
933 list->value->type,
934 list->value->device,
935 list->value->offset,
936 list->value->len,
937 list->value->speed);
939 list = list->next;
942 qapi_free_BlockJobInfoList(list);
945 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
947 TPMInfoList *info_list, *info;
948 Error *err = NULL;
949 unsigned int c = 0;
950 TPMPassthroughOptions *tpo;
952 info_list = qmp_query_tpm(&err);
953 if (err) {
954 monitor_printf(mon, "TPM device not supported\n");
955 error_free(err);
956 return;
959 if (info_list) {
960 monitor_printf(mon, "TPM device:\n");
963 for (info = info_list; info; info = info->next) {
964 TPMInfo *ti = info->value;
965 monitor_printf(mon, " tpm%d: model=%s\n",
966 c, TpmModel_lookup[ti->model]);
968 monitor_printf(mon, " \\ %s: type=%s",
969 ti->id, TpmTypeOptionsKind_lookup[ti->options->type]);
971 switch (ti->options->type) {
972 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
973 tpo = ti->options->u.passthrough.data;
974 monitor_printf(mon, "%s%s%s%s",
975 tpo->has_path ? ",path=" : "",
976 tpo->has_path ? tpo->path : "",
977 tpo->has_cancel_path ? ",cancel-path=" : "",
978 tpo->has_cancel_path ? tpo->cancel_path : "");
979 break;
980 case TPM_TYPE_OPTIONS_KIND__MAX:
981 break;
983 monitor_printf(mon, "\n");
984 c++;
986 qapi_free_TPMInfoList(info_list);
989 void hmp_quit(Monitor *mon, const QDict *qdict)
991 monitor_suspend(mon);
992 qmp_quit(NULL);
995 void hmp_stop(Monitor *mon, const QDict *qdict)
997 qmp_stop(NULL);
1000 void hmp_system_reset(Monitor *mon, const QDict *qdict)
1002 qmp_system_reset(NULL);
1005 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1007 qmp_system_powerdown(NULL);
1010 void hmp_cpu(Monitor *mon, const QDict *qdict)
1012 int64_t cpu_index;
1014 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1015 use it are converted to the QAPI */
1016 cpu_index = qdict_get_int(qdict, "index");
1017 if (monitor_set_cpu(cpu_index) < 0) {
1018 monitor_printf(mon, "invalid CPU index\n");
1022 void hmp_memsave(Monitor *mon, const QDict *qdict)
1024 uint32_t size = qdict_get_int(qdict, "size");
1025 const char *filename = qdict_get_str(qdict, "filename");
1026 uint64_t addr = qdict_get_int(qdict, "val");
1027 Error *err = NULL;
1028 int cpu_index = monitor_get_cpu_index();
1030 if (cpu_index < 0) {
1031 monitor_printf(mon, "No CPU available\n");
1032 return;
1035 qmp_memsave(addr, size, filename, true, cpu_index, &err);
1036 hmp_handle_error(mon, &err);
1039 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1041 uint32_t size = qdict_get_int(qdict, "size");
1042 const char *filename = qdict_get_str(qdict, "filename");
1043 uint64_t addr = qdict_get_int(qdict, "val");
1044 Error *err = NULL;
1046 qmp_pmemsave(addr, size, filename, &err);
1047 hmp_handle_error(mon, &err);
1050 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1052 const char *chardev = qdict_get_str(qdict, "device");
1053 const char *data = qdict_get_str(qdict, "data");
1054 Error *err = NULL;
1056 qmp_ringbuf_write(chardev, data, false, 0, &err);
1058 hmp_handle_error(mon, &err);
1061 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1063 uint32_t size = qdict_get_int(qdict, "size");
1064 const char *chardev = qdict_get_str(qdict, "device");
1065 char *data;
1066 Error *err = NULL;
1067 int i;
1069 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1070 if (err) {
1071 error_report_err(err);
1072 return;
1075 for (i = 0; data[i]; i++) {
1076 unsigned char ch = data[i];
1078 if (ch == '\\') {
1079 monitor_printf(mon, "\\\\");
1080 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1081 monitor_printf(mon, "\\u%04X", ch);
1082 } else {
1083 monitor_printf(mon, "%c", ch);
1087 monitor_printf(mon, "\n");
1088 g_free(data);
1091 static void hmp_cont_cb(void *opaque, int err)
1093 if (!err) {
1094 qmp_cont(NULL);
1098 static bool key_is_missing(const BlockInfo *bdev)
1100 return (bdev->inserted && bdev->inserted->encryption_key_missing);
1103 void hmp_cont(Monitor *mon, const QDict *qdict)
1105 BlockInfoList *bdev_list, *bdev;
1106 Error *err = NULL;
1108 bdev_list = qmp_query_block(NULL);
1109 for (bdev = bdev_list; bdev; bdev = bdev->next) {
1110 if (key_is_missing(bdev->value)) {
1111 monitor_read_block_device_key(mon, bdev->value->device,
1112 hmp_cont_cb, NULL);
1113 goto out;
1117 qmp_cont(&err);
1118 hmp_handle_error(mon, &err);
1120 out:
1121 qapi_free_BlockInfoList(bdev_list);
1124 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1126 qmp_system_wakeup(NULL);
1129 void hmp_nmi(Monitor *mon, const QDict *qdict)
1131 Error *err = NULL;
1133 qmp_inject_nmi(&err);
1134 hmp_handle_error(mon, &err);
1137 void hmp_set_link(Monitor *mon, const QDict *qdict)
1139 const char *name = qdict_get_str(qdict, "name");
1140 bool up = qdict_get_bool(qdict, "up");
1141 Error *err = NULL;
1143 qmp_set_link(name, up, &err);
1144 hmp_handle_error(mon, &err);
1147 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1149 const char *device = qdict_get_str(qdict, "device");
1150 const char *password = qdict_get_str(qdict, "password");
1151 Error *err = NULL;
1153 qmp_block_passwd(true, device, false, NULL, password, &err);
1154 hmp_handle_error(mon, &err);
1157 void hmp_balloon(Monitor *mon, const QDict *qdict)
1159 int64_t value = qdict_get_int(qdict, "value");
1160 Error *err = NULL;
1162 qmp_balloon(value, &err);
1163 if (err) {
1164 error_report_err(err);
1168 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1170 const char *device = qdict_get_str(qdict, "device");
1171 int64_t size = qdict_get_int(qdict, "size");
1172 Error *err = NULL;
1174 qmp_block_resize(true, device, false, NULL, size, &err);
1175 hmp_handle_error(mon, &err);
1178 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1180 const char *filename = qdict_get_str(qdict, "target");
1181 const char *format = qdict_get_try_str(qdict, "format");
1182 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1183 bool full = qdict_get_try_bool(qdict, "full", false);
1184 Error *err = NULL;
1185 DriveMirror mirror = {
1186 .device = (char *)qdict_get_str(qdict, "device"),
1187 .target = (char *)filename,
1188 .has_format = !!format,
1189 .format = (char *)format,
1190 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1191 .has_mode = true,
1192 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1193 .unmap = true,
1196 if (!filename) {
1197 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1198 hmp_handle_error(mon, &err);
1199 return;
1201 qmp_drive_mirror(&mirror, &err);
1202 hmp_handle_error(mon, &err);
1205 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1207 const char *device = qdict_get_str(qdict, "device");
1208 const char *filename = qdict_get_str(qdict, "target");
1209 const char *format = qdict_get_try_str(qdict, "format");
1210 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1211 bool full = qdict_get_try_bool(qdict, "full", false);
1212 bool compress = qdict_get_try_bool(qdict, "compress", false);
1213 Error *err = NULL;
1214 DriveBackup backup = {
1215 .device = (char *)device,
1216 .target = (char *)filename,
1217 .has_format = !!format,
1218 .format = (char *)format,
1219 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1220 .has_mode = true,
1221 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1222 .has_compress = !!compress,
1223 .compress = compress,
1226 if (!filename) {
1227 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1228 hmp_handle_error(mon, &err);
1229 return;
1232 qmp_drive_backup(&backup, &err);
1233 hmp_handle_error(mon, &err);
1236 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1238 const char *device = qdict_get_str(qdict, "device");
1239 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1240 const char *format = qdict_get_try_str(qdict, "format");
1241 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1242 enum NewImageMode mode;
1243 Error *err = NULL;
1245 if (!filename) {
1246 /* In the future, if 'snapshot-file' is not specified, the snapshot
1247 will be taken internally. Today it's actually required. */
1248 error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1249 hmp_handle_error(mon, &err);
1250 return;
1253 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1254 qmp_blockdev_snapshot_sync(true, device, false, NULL,
1255 filename, false, NULL,
1256 !!format, format,
1257 true, mode, &err);
1258 hmp_handle_error(mon, &err);
1261 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1263 const char *device = qdict_get_str(qdict, "device");
1264 const char *name = qdict_get_str(qdict, "name");
1265 Error *err = NULL;
1267 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1268 hmp_handle_error(mon, &err);
1271 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1273 const char *device = qdict_get_str(qdict, "device");
1274 const char *name = qdict_get_str(qdict, "name");
1275 const char *id = qdict_get_try_str(qdict, "id");
1276 Error *err = NULL;
1278 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1279 true, name, &err);
1280 hmp_handle_error(mon, &err);
1283 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1285 int saved_vm_running = runstate_is_running();
1286 const char *name = qdict_get_str(qdict, "name");
1287 Error *err = NULL;
1289 vm_stop(RUN_STATE_RESTORE_VM);
1291 if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1292 vm_start();
1294 hmp_handle_error(mon, &err);
1297 void hmp_savevm(Monitor *mon, const QDict *qdict)
1299 Error *err = NULL;
1301 save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1302 hmp_handle_error(mon, &err);
1305 void hmp_delvm(Monitor *mon, const QDict *qdict)
1307 BlockDriverState *bs;
1308 Error *err;
1309 const char *name = qdict_get_str(qdict, "name");
1311 if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1312 error_reportf_err(err,
1313 "Error while deleting snapshot on device '%s': ",
1314 bdrv_get_device_name(bs));
1318 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1320 BlockDriverState *bs, *bs1;
1321 BdrvNextIterator it1;
1322 QEMUSnapshotInfo *sn_tab, *sn;
1323 bool no_snapshot = true;
1324 int nb_sns, i;
1325 int total;
1326 int *global_snapshots;
1327 AioContext *aio_context;
1329 typedef struct SnapshotEntry {
1330 QEMUSnapshotInfo sn;
1331 QTAILQ_ENTRY(SnapshotEntry) next;
1332 } SnapshotEntry;
1334 typedef struct ImageEntry {
1335 const char *imagename;
1336 QTAILQ_ENTRY(ImageEntry) next;
1337 QTAILQ_HEAD(, SnapshotEntry) snapshots;
1338 } ImageEntry;
1340 QTAILQ_HEAD(, ImageEntry) image_list =
1341 QTAILQ_HEAD_INITIALIZER(image_list);
1343 ImageEntry *image_entry, *next_ie;
1344 SnapshotEntry *snapshot_entry;
1346 bs = bdrv_all_find_vmstate_bs();
1347 if (!bs) {
1348 monitor_printf(mon, "No available block device supports snapshots\n");
1349 return;
1351 aio_context = bdrv_get_aio_context(bs);
1353 aio_context_acquire(aio_context);
1354 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1355 aio_context_release(aio_context);
1357 if (nb_sns < 0) {
1358 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1359 return;
1362 for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1363 int bs1_nb_sns = 0;
1364 ImageEntry *ie;
1365 SnapshotEntry *se;
1366 AioContext *ctx = bdrv_get_aio_context(bs1);
1368 aio_context_acquire(ctx);
1369 if (bdrv_can_snapshot(bs1)) {
1370 sn = NULL;
1371 bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1372 if (bs1_nb_sns > 0) {
1373 no_snapshot = false;
1374 ie = g_new0(ImageEntry, 1);
1375 ie->imagename = bdrv_get_device_name(bs1);
1376 QTAILQ_INIT(&ie->snapshots);
1377 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1378 for (i = 0; i < bs1_nb_sns; i++) {
1379 se = g_new0(SnapshotEntry, 1);
1380 se->sn = sn[i];
1381 QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1384 g_free(sn);
1386 aio_context_release(ctx);
1389 if (no_snapshot) {
1390 monitor_printf(mon, "There is no snapshot available.\n");
1391 return;
1394 global_snapshots = g_new0(int, nb_sns);
1395 total = 0;
1396 for (i = 0; i < nb_sns; i++) {
1397 SnapshotEntry *next_sn;
1398 if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1399 global_snapshots[total] = i;
1400 total++;
1401 QTAILQ_FOREACH(image_entry, &image_list, next) {
1402 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1403 next, next_sn) {
1404 if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1405 QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1406 next);
1407 g_free(snapshot_entry);
1414 monitor_printf(mon, "List of snapshots present on all disks:\n");
1416 if (total > 0) {
1417 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1418 monitor_printf(mon, "\n");
1419 for (i = 0; i < total; i++) {
1420 sn = &sn_tab[global_snapshots[i]];
1421 /* The ID is not guaranteed to be the same on all images, so
1422 * overwrite it.
1424 pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1425 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1426 monitor_printf(mon, "\n");
1428 } else {
1429 monitor_printf(mon, "None\n");
1432 QTAILQ_FOREACH(image_entry, &image_list, next) {
1433 if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1434 continue;
1436 monitor_printf(mon,
1437 "\nList of partial (non-loadable) snapshots on '%s':\n",
1438 image_entry->imagename);
1439 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1440 monitor_printf(mon, "\n");
1441 QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1442 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1443 &snapshot_entry->sn);
1444 monitor_printf(mon, "\n");
1448 QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1449 SnapshotEntry *next_sn;
1450 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1451 next_sn) {
1452 g_free(snapshot_entry);
1454 g_free(image_entry);
1456 g_free(sn_tab);
1457 g_free(global_snapshots);
1461 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1463 qmp_migrate_cancel(NULL);
1466 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1468 Error *err = NULL;
1469 const char *uri = qdict_get_str(qdict, "uri");
1471 qmp_migrate_incoming(uri, &err);
1473 hmp_handle_error(mon, &err);
1476 /* Kept for backwards compatibility */
1477 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1479 double value = qdict_get_double(qdict, "value");
1480 qmp_migrate_set_downtime(value, NULL);
1483 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1485 int64_t value = qdict_get_int(qdict, "value");
1486 Error *err = NULL;
1488 qmp_migrate_set_cache_size(value, &err);
1489 if (err) {
1490 error_report_err(err);
1491 return;
1495 /* Kept for backwards compatibility */
1496 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1498 int64_t value = qdict_get_int(qdict, "value");
1499 qmp_migrate_set_speed(value, NULL);
1502 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1504 const char *cap = qdict_get_str(qdict, "capability");
1505 bool state = qdict_get_bool(qdict, "state");
1506 Error *err = NULL;
1507 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1508 int i;
1510 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1511 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1512 caps->value = g_malloc0(sizeof(*caps->value));
1513 caps->value->capability = i;
1514 caps->value->state = state;
1515 caps->next = NULL;
1516 qmp_migrate_set_capabilities(caps, &err);
1517 break;
1521 if (i == MIGRATION_CAPABILITY__MAX) {
1522 error_setg(&err, QERR_INVALID_PARAMETER, cap);
1525 qapi_free_MigrationCapabilityStatusList(caps);
1527 if (err) {
1528 error_report_err(err);
1532 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1534 const char *param = qdict_get_str(qdict, "parameter");
1535 const char *valuestr = qdict_get_str(qdict, "value");
1536 Visitor *v = string_input_visitor_new(valuestr);
1537 uint64_t valuebw = 0;
1538 int64_t valueint = 0;
1539 bool valuebool = false;
1540 Error *err = NULL;
1541 bool use_int_value = false;
1542 int i, ret;
1544 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1545 if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
1546 MigrationParameters p = { 0 };
1547 switch (i) {
1548 case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1549 p.has_compress_level = true;
1550 use_int_value = true;
1551 break;
1552 case MIGRATION_PARAMETER_COMPRESS_THREADS:
1553 p.has_compress_threads = true;
1554 use_int_value = true;
1555 break;
1556 case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1557 p.has_decompress_threads = true;
1558 use_int_value = true;
1559 break;
1560 case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1561 p.has_cpu_throttle_initial = true;
1562 use_int_value = true;
1563 break;
1564 case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1565 p.has_cpu_throttle_increment = true;
1566 use_int_value = true;
1567 break;
1568 case MIGRATION_PARAMETER_TLS_CREDS:
1569 p.has_tls_creds = true;
1570 p.tls_creds = (char *) valuestr;
1571 break;
1572 case MIGRATION_PARAMETER_TLS_HOSTNAME:
1573 p.has_tls_hostname = true;
1574 p.tls_hostname = (char *) valuestr;
1575 break;
1576 case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1577 p.has_max_bandwidth = true;
1578 ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1579 if (ret < 0 || valuebw > INT64_MAX
1580 || (size_t)valuebw != valuebw) {
1581 error_setg(&err, "Invalid size %s", valuestr);
1582 goto cleanup;
1584 p.max_bandwidth = valuebw;
1585 break;
1586 case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1587 p.has_downtime_limit = true;
1588 use_int_value = true;
1589 break;
1590 case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1591 p.has_x_checkpoint_delay = true;
1592 use_int_value = true;
1593 break;
1594 case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1595 p.has_block_incremental = true;
1596 visit_type_bool(v, param, &valuebool, &err);
1597 if (err) {
1598 goto cleanup;
1600 p.block_incremental = valuebool;
1601 break;
1604 if (use_int_value) {
1605 visit_type_int(v, param, &valueint, &err);
1606 if (err) {
1607 goto cleanup;
1609 /* Set all integers; only one has_FOO will be set, and
1610 * the code ignores the remaining values */
1611 p.compress_level = valueint;
1612 p.compress_threads = valueint;
1613 p.decompress_threads = valueint;
1614 p.cpu_throttle_initial = valueint;
1615 p.cpu_throttle_increment = valueint;
1616 p.downtime_limit = valueint;
1617 p.x_checkpoint_delay = valueint;
1620 qmp_migrate_set_parameters(&p, &err);
1621 break;
1625 if (i == MIGRATION_PARAMETER__MAX) {
1626 error_setg(&err, QERR_INVALID_PARAMETER, param);
1629 cleanup:
1630 visit_free(v);
1631 if (err) {
1632 error_report_err(err);
1636 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1638 Error *err = NULL;
1639 const char *protocol = qdict_get_str(qdict, "protocol");
1640 const char *hostname = qdict_get_str(qdict, "hostname");
1641 bool has_port = qdict_haskey(qdict, "port");
1642 int port = qdict_get_try_int(qdict, "port", -1);
1643 bool has_tls_port = qdict_haskey(qdict, "tls-port");
1644 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1645 const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1647 qmp_client_migrate_info(protocol, hostname,
1648 has_port, port, has_tls_port, tls_port,
1649 !!cert_subject, cert_subject, &err);
1650 hmp_handle_error(mon, &err);
1653 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1655 Error *err = NULL;
1656 qmp_migrate_start_postcopy(&err);
1657 hmp_handle_error(mon, &err);
1660 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1662 Error *err = NULL;
1664 qmp_x_colo_lost_heartbeat(&err);
1665 hmp_handle_error(mon, &err);
1668 void hmp_set_password(Monitor *mon, const QDict *qdict)
1670 const char *protocol = qdict_get_str(qdict, "protocol");
1671 const char *password = qdict_get_str(qdict, "password");
1672 const char *connected = qdict_get_try_str(qdict, "connected");
1673 Error *err = NULL;
1675 qmp_set_password(protocol, password, !!connected, connected, &err);
1676 hmp_handle_error(mon, &err);
1679 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1681 const char *protocol = qdict_get_str(qdict, "protocol");
1682 const char *whenstr = qdict_get_str(qdict, "time");
1683 Error *err = NULL;
1685 qmp_expire_password(protocol, whenstr, &err);
1686 hmp_handle_error(mon, &err);
1689 void hmp_eject(Monitor *mon, const QDict *qdict)
1691 bool force = qdict_get_try_bool(qdict, "force", false);
1692 const char *device = qdict_get_str(qdict, "device");
1693 Error *err = NULL;
1695 qmp_eject(true, device, false, NULL, true, force, &err);
1696 hmp_handle_error(mon, &err);
1699 static void hmp_change_read_arg(void *opaque, const char *password,
1700 void *readline_opaque)
1702 qmp_change_vnc_password(password, NULL);
1703 monitor_read_command(opaque, 1);
1706 void hmp_change(Monitor *mon, const QDict *qdict)
1708 const char *device = qdict_get_str(qdict, "device");
1709 const char *target = qdict_get_str(qdict, "target");
1710 const char *arg = qdict_get_try_str(qdict, "arg");
1711 const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1712 BlockdevChangeReadOnlyMode read_only_mode = 0;
1713 Error *err = NULL;
1715 if (strcmp(device, "vnc") == 0) {
1716 if (read_only) {
1717 monitor_printf(mon,
1718 "Parameter 'read-only-mode' is invalid for VNC\n");
1719 return;
1721 if (strcmp(target, "passwd") == 0 ||
1722 strcmp(target, "password") == 0) {
1723 if (!arg) {
1724 monitor_read_password(mon, hmp_change_read_arg, NULL);
1725 return;
1728 qmp_change("vnc", target, !!arg, arg, &err);
1729 } else {
1730 if (read_only) {
1731 read_only_mode =
1732 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
1733 read_only, BLOCKDEV_CHANGE_READ_ONLY_MODE__MAX,
1734 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1735 if (err) {
1736 hmp_handle_error(mon, &err);
1737 return;
1741 qmp_blockdev_change_medium(true, device, false, NULL, target,
1742 !!arg, arg, !!read_only, read_only_mode,
1743 &err);
1744 if (err &&
1745 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1746 error_free(err);
1747 monitor_read_block_device_key(mon, device, NULL, NULL);
1748 return;
1752 hmp_handle_error(mon, &err);
1755 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1757 Error *err = NULL;
1758 BlockIOThrottle throttle = {
1759 .has_device = true,
1760 .device = (char *) qdict_get_str(qdict, "device"),
1761 .bps = qdict_get_int(qdict, "bps"),
1762 .bps_rd = qdict_get_int(qdict, "bps_rd"),
1763 .bps_wr = qdict_get_int(qdict, "bps_wr"),
1764 .iops = qdict_get_int(qdict, "iops"),
1765 .iops_rd = qdict_get_int(qdict, "iops_rd"),
1766 .iops_wr = qdict_get_int(qdict, "iops_wr"),
1769 qmp_block_set_io_throttle(&throttle, &err);
1770 hmp_handle_error(mon, &err);
1773 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1775 Error *error = NULL;
1776 const char *device = qdict_get_str(qdict, "device");
1777 const char *base = qdict_get_try_str(qdict, "base");
1778 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1780 qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
1781 false, NULL, qdict_haskey(qdict, "speed"), speed,
1782 true, BLOCKDEV_ON_ERROR_REPORT, &error);
1784 hmp_handle_error(mon, &error);
1787 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1789 Error *error = NULL;
1790 const char *device = qdict_get_str(qdict, "device");
1791 int64_t value = qdict_get_int(qdict, "speed");
1793 qmp_block_job_set_speed(device, value, &error);
1795 hmp_handle_error(mon, &error);
1798 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1800 Error *error = NULL;
1801 const char *device = qdict_get_str(qdict, "device");
1802 bool force = qdict_get_try_bool(qdict, "force", false);
1804 qmp_block_job_cancel(device, true, force, &error);
1806 hmp_handle_error(mon, &error);
1809 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1811 Error *error = NULL;
1812 const char *device = qdict_get_str(qdict, "device");
1814 qmp_block_job_pause(device, &error);
1816 hmp_handle_error(mon, &error);
1819 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1821 Error *error = NULL;
1822 const char *device = qdict_get_str(qdict, "device");
1824 qmp_block_job_resume(device, &error);
1826 hmp_handle_error(mon, &error);
1829 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1831 Error *error = NULL;
1832 const char *device = qdict_get_str(qdict, "device");
1834 qmp_block_job_complete(device, &error);
1836 hmp_handle_error(mon, &error);
1839 typedef struct HMPMigrationStatus
1841 QEMUTimer *timer;
1842 Monitor *mon;
1843 bool is_block_migration;
1844 } HMPMigrationStatus;
1846 static void hmp_migrate_status_cb(void *opaque)
1848 HMPMigrationStatus *status = opaque;
1849 MigrationInfo *info;
1851 info = qmp_query_migrate(NULL);
1852 if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1853 info->status == MIGRATION_STATUS_SETUP) {
1854 if (info->has_disk) {
1855 int progress;
1857 if (info->disk->remaining) {
1858 progress = info->disk->transferred * 100 / info->disk->total;
1859 } else {
1860 progress = 100;
1863 monitor_printf(status->mon, "Completed %d %%\r", progress);
1864 monitor_flush(status->mon);
1867 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1868 } else {
1869 if (status->is_block_migration) {
1870 monitor_printf(status->mon, "\n");
1872 if (info->has_error_desc) {
1873 error_report("%s", info->error_desc);
1875 monitor_resume(status->mon);
1876 timer_del(status->timer);
1877 g_free(status);
1880 qapi_free_MigrationInfo(info);
1883 void hmp_migrate(Monitor *mon, const QDict *qdict)
1885 bool detach = qdict_get_try_bool(qdict, "detach", false);
1886 bool blk = qdict_get_try_bool(qdict, "blk", false);
1887 bool inc = qdict_get_try_bool(qdict, "inc", false);
1888 const char *uri = qdict_get_str(qdict, "uri");
1889 Error *err = NULL;
1891 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1892 if (err) {
1893 error_report_err(err);
1894 return;
1897 if (!detach) {
1898 HMPMigrationStatus *status;
1900 if (monitor_suspend(mon) < 0) {
1901 monitor_printf(mon, "terminal does not allow synchronous "
1902 "migration, continuing detached\n");
1903 return;
1906 status = g_malloc0(sizeof(*status));
1907 status->mon = mon;
1908 status->is_block_migration = blk || inc;
1909 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1910 status);
1911 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1915 void hmp_device_add(Monitor *mon, const QDict *qdict)
1917 Error *err = NULL;
1919 qmp_device_add((QDict *)qdict, NULL, &err);
1920 hmp_handle_error(mon, &err);
1923 void hmp_device_del(Monitor *mon, const QDict *qdict)
1925 const char *id = qdict_get_str(qdict, "id");
1926 Error *err = NULL;
1928 qmp_device_del(id, &err);
1929 hmp_handle_error(mon, &err);
1932 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1934 Error *err = NULL;
1935 bool paging = qdict_get_try_bool(qdict, "paging", false);
1936 bool zlib = qdict_get_try_bool(qdict, "zlib", false);
1937 bool lzo = qdict_get_try_bool(qdict, "lzo", false);
1938 bool snappy = qdict_get_try_bool(qdict, "snappy", false);
1939 const char *file = qdict_get_str(qdict, "filename");
1940 bool has_begin = qdict_haskey(qdict, "begin");
1941 bool has_length = qdict_haskey(qdict, "length");
1942 bool has_detach = qdict_haskey(qdict, "detach");
1943 int64_t begin = 0;
1944 int64_t length = 0;
1945 bool detach = false;
1946 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1947 char *prot;
1949 if (zlib + lzo + snappy > 1) {
1950 error_setg(&err, "only one of '-z|-l|-s' can be set");
1951 hmp_handle_error(mon, &err);
1952 return;
1955 if (zlib) {
1956 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1959 if (lzo) {
1960 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1963 if (snappy) {
1964 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1967 if (has_begin) {
1968 begin = qdict_get_int(qdict, "begin");
1970 if (has_length) {
1971 length = qdict_get_int(qdict, "length");
1973 if (has_detach) {
1974 detach = qdict_get_bool(qdict, "detach");
1977 prot = g_strconcat("file:", file, NULL);
1979 qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
1980 has_length, length, true, dump_format, &err);
1981 hmp_handle_error(mon, &err);
1982 g_free(prot);
1985 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1987 Error *err = NULL;
1988 QemuOpts *opts;
1990 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1991 if (err) {
1992 goto out;
1995 netdev_add(opts, &err);
1996 if (err) {
1997 qemu_opts_del(opts);
2000 out:
2001 hmp_handle_error(mon, &err);
2004 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2006 const char *id = qdict_get_str(qdict, "id");
2007 Error *err = NULL;
2009 qmp_netdev_del(id, &err);
2010 hmp_handle_error(mon, &err);
2013 void hmp_object_add(Monitor *mon, const QDict *qdict)
2015 Error *err = NULL;
2016 QemuOpts *opts;
2017 Object *obj = NULL;
2019 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2020 if (err) {
2021 hmp_handle_error(mon, &err);
2022 return;
2025 obj = user_creatable_add_opts(opts, &err);
2026 qemu_opts_del(opts);
2028 if (err) {
2029 hmp_handle_error(mon, &err);
2031 if (obj) {
2032 object_unref(obj);
2036 void hmp_getfd(Monitor *mon, const QDict *qdict)
2038 const char *fdname = qdict_get_str(qdict, "fdname");
2039 Error *err = NULL;
2041 qmp_getfd(fdname, &err);
2042 hmp_handle_error(mon, &err);
2045 void hmp_closefd(Monitor *mon, const QDict *qdict)
2047 const char *fdname = qdict_get_str(qdict, "fdname");
2048 Error *err = NULL;
2050 qmp_closefd(fdname, &err);
2051 hmp_handle_error(mon, &err);
2054 void hmp_sendkey(Monitor *mon, const QDict *qdict)
2056 const char *keys = qdict_get_str(qdict, "keys");
2057 KeyValueList *keylist, *head = NULL, *tmp = NULL;
2058 int has_hold_time = qdict_haskey(qdict, "hold-time");
2059 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2060 Error *err = NULL;
2061 char *separator;
2062 int keyname_len;
2064 while (1) {
2065 separator = strchr(keys, '-');
2066 keyname_len = separator ? separator - keys : strlen(keys);
2068 /* Be compatible with old interface, convert user inputted "<" */
2069 if (keys[0] == '<' && keyname_len == 1) {
2070 keys = "less";
2071 keyname_len = 4;
2074 keylist = g_malloc0(sizeof(*keylist));
2075 keylist->value = g_malloc0(sizeof(*keylist->value));
2077 if (!head) {
2078 head = keylist;
2080 if (tmp) {
2081 tmp->next = keylist;
2083 tmp = keylist;
2085 if (strstart(keys, "0x", NULL)) {
2086 char *endp;
2087 int value = strtoul(keys, &endp, 0);
2088 assert(endp <= keys + keyname_len);
2089 if (endp != keys + keyname_len) {
2090 goto err_out;
2092 keylist->value->type = KEY_VALUE_KIND_NUMBER;
2093 keylist->value->u.number.data = value;
2094 } else {
2095 int idx = index_from_key(keys, keyname_len);
2096 if (idx == Q_KEY_CODE__MAX) {
2097 goto err_out;
2099 keylist->value->type = KEY_VALUE_KIND_QCODE;
2100 keylist->value->u.qcode.data = idx;
2103 if (!separator) {
2104 break;
2106 keys = separator + 1;
2109 qmp_send_key(head, has_hold_time, hold_time, &err);
2110 hmp_handle_error(mon, &err);
2112 out:
2113 qapi_free_KeyValueList(head);
2114 return;
2116 err_out:
2117 monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
2118 goto out;
2121 void hmp_screendump(Monitor *mon, const QDict *qdict)
2123 const char *filename = qdict_get_str(qdict, "filename");
2124 Error *err = NULL;
2126 qmp_screendump(filename, &err);
2127 hmp_handle_error(mon, &err);
2130 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2132 const char *uri = qdict_get_str(qdict, "uri");
2133 bool writable = qdict_get_try_bool(qdict, "writable", false);
2134 bool all = qdict_get_try_bool(qdict, "all", false);
2135 Error *local_err = NULL;
2136 BlockInfoList *block_list, *info;
2137 SocketAddress *addr;
2139 if (writable && !all) {
2140 error_setg(&local_err, "-w only valid together with -a");
2141 goto exit;
2144 /* First check if the address is valid and start the server. */
2145 addr = socket_parse(uri, &local_err);
2146 if (local_err != NULL) {
2147 goto exit;
2150 nbd_server_start(addr, NULL, &local_err);
2151 qapi_free_SocketAddress(addr);
2152 if (local_err != NULL) {
2153 goto exit;
2156 if (!all) {
2157 return;
2160 /* Then try adding all block devices. If one fails, close all and
2161 * exit.
2163 block_list = qmp_query_block(NULL);
2165 for (info = block_list; info; info = info->next) {
2166 if (!info->value->has_inserted) {
2167 continue;
2170 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
2172 if (local_err != NULL) {
2173 qmp_nbd_server_stop(NULL);
2174 break;
2178 qapi_free_BlockInfoList(block_list);
2180 exit:
2181 hmp_handle_error(mon, &local_err);
2184 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2186 const char *device = qdict_get_str(qdict, "device");
2187 bool writable = qdict_get_try_bool(qdict, "writable", false);
2188 Error *local_err = NULL;
2190 qmp_nbd_server_add(device, true, writable, &local_err);
2192 if (local_err != NULL) {
2193 hmp_handle_error(mon, &local_err);
2197 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2199 Error *err = NULL;
2201 qmp_nbd_server_stop(&err);
2202 hmp_handle_error(mon, &err);
2205 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2207 int cpuid;
2208 Error *err = NULL;
2210 cpuid = qdict_get_int(qdict, "id");
2211 qmp_cpu_add(cpuid, &err);
2212 hmp_handle_error(mon, &err);
2215 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2217 const char *args = qdict_get_str(qdict, "args");
2218 Error *err = NULL;
2219 QemuOpts *opts;
2221 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2222 if (opts == NULL) {
2223 error_setg(&err, "Parsing chardev args failed");
2224 } else {
2225 qemu_chr_new_from_opts(opts, &err);
2226 qemu_opts_del(opts);
2228 hmp_handle_error(mon, &err);
2231 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2233 Error *local_err = NULL;
2235 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2236 hmp_handle_error(mon, &local_err);
2239 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2241 Error *local_err = NULL;
2243 qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2244 hmp_handle_error(mon, &local_err);
2247 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2249 BlockBackend *blk;
2250 BlockBackend *local_blk = NULL;
2251 AioContext *aio_context;
2252 const char* device = qdict_get_str(qdict, "device");
2253 const char* command = qdict_get_str(qdict, "command");
2254 Error *err = NULL;
2255 int ret;
2257 blk = blk_by_name(device);
2258 if (!blk) {
2259 BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2260 if (bs) {
2261 blk = local_blk = blk_new(0, BLK_PERM_ALL);
2262 ret = blk_insert_bs(blk, bs, &err);
2263 if (ret < 0) {
2264 goto fail;
2266 } else {
2267 goto fail;
2271 aio_context = blk_get_aio_context(blk);
2272 aio_context_acquire(aio_context);
2275 * Notably absent: Proper permission management. This is sad, but it seems
2276 * almost impossible to achieve without changing the semantics and thereby
2277 * limiting the use cases of the qemu-io HMP command.
2279 * In an ideal world we would unconditionally create a new BlockBackend for
2280 * qemuio_command(), but we have commands like 'reopen' and want them to
2281 * take effect on the exact BlockBackend whose name the user passed instead
2282 * of just on a temporary copy of it.
2284 * Another problem is that deleting the temporary BlockBackend involves
2285 * draining all requests on it first, but some qemu-iotests cases want to
2286 * issue multiple aio_read/write requests and expect them to complete in
2287 * the background while the monitor has already returned.
2289 * This is also what prevents us from saving the original permissions and
2290 * restoring them later: We can't revoke permissions until all requests
2291 * have completed, and we don't know when that is nor can we really let
2292 * anything else run before we have revoken them to avoid race conditions.
2294 * What happens now is that command() in qemu-io-cmds.c can extend the
2295 * permissions if necessary for the qemu-io command. And they simply stay
2296 * extended, possibly resulting in a read-only guest device keeping write
2297 * permissions. Ugly, but it appears to be the lesser evil.
2299 qemuio_command(blk, command);
2301 aio_context_release(aio_context);
2303 fail:
2304 blk_unref(local_blk);
2305 hmp_handle_error(mon, &err);
2308 void hmp_object_del(Monitor *mon, const QDict *qdict)
2310 const char *id = qdict_get_str(qdict, "id");
2311 Error *err = NULL;
2313 user_creatable_del(id, &err);
2314 hmp_handle_error(mon, &err);
2317 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2319 Error *err = NULL;
2320 MemdevList *memdev_list = qmp_query_memdev(&err);
2321 MemdevList *m = memdev_list;
2322 Visitor *v;
2323 char *str;
2325 while (m) {
2326 v = string_output_visitor_new(false, &str);
2327 visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2328 monitor_printf(mon, "memory backend: %s\n", m->value->id);
2329 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
2330 monitor_printf(mon, " merge: %s\n",
2331 m->value->merge ? "true" : "false");
2332 monitor_printf(mon, " dump: %s\n",
2333 m->value->dump ? "true" : "false");
2334 monitor_printf(mon, " prealloc: %s\n",
2335 m->value->prealloc ? "true" : "false");
2336 monitor_printf(mon, " policy: %s\n",
2337 HostMemPolicy_lookup[m->value->policy]);
2338 visit_complete(v, &str);
2339 monitor_printf(mon, " host nodes: %s\n", str);
2341 g_free(str);
2342 visit_free(v);
2343 m = m->next;
2346 monitor_printf(mon, "\n");
2348 qapi_free_MemdevList(memdev_list);
2351 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2353 Error *err = NULL;
2354 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2355 MemoryDeviceInfoList *info;
2356 MemoryDeviceInfo *value;
2357 PCDIMMDeviceInfo *di;
2359 for (info = info_list; info; info = info->next) {
2360 value = info->value;
2362 if (value) {
2363 switch (value->type) {
2364 case MEMORY_DEVICE_INFO_KIND_DIMM:
2365 di = value->u.dimm.data;
2367 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2368 MemoryDeviceInfoKind_lookup[value->type],
2369 di->id ? di->id : "");
2370 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
2371 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
2372 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
2373 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
2374 monitor_printf(mon, " memdev: %s\n", di->memdev);
2375 monitor_printf(mon, " hotplugged: %s\n",
2376 di->hotplugged ? "true" : "false");
2377 monitor_printf(mon, " hotpluggable: %s\n",
2378 di->hotpluggable ? "true" : "false");
2379 break;
2380 default:
2381 break;
2386 qapi_free_MemoryDeviceInfoList(info_list);
2389 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2391 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2392 IOThreadInfoList *info;
2393 IOThreadInfo *value;
2395 for (info = info_list; info; info = info->next) {
2396 value = info->value;
2397 monitor_printf(mon, "%s:\n", value->id);
2398 monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
2399 monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2400 monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
2401 monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
2404 qapi_free_IOThreadInfoList(info_list);
2407 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2409 const char *path = qdict_get_try_str(qdict, "path");
2410 ObjectPropertyInfoList *list;
2411 Error *err = NULL;
2413 if (path == NULL) {
2414 monitor_printf(mon, "/\n");
2415 return;
2418 list = qmp_qom_list(path, &err);
2419 if (err == NULL) {
2420 ObjectPropertyInfoList *start = list;
2421 while (list != NULL) {
2422 ObjectPropertyInfo *value = list->value;
2424 monitor_printf(mon, "%s (%s)\n",
2425 value->name, value->type);
2426 list = list->next;
2428 qapi_free_ObjectPropertyInfoList(start);
2430 hmp_handle_error(mon, &err);
2433 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2435 const char *path = qdict_get_str(qdict, "path");
2436 const char *property = qdict_get_str(qdict, "property");
2437 const char *value = qdict_get_str(qdict, "value");
2438 Error *err = NULL;
2439 bool ambiguous = false;
2440 Object *obj;
2442 obj = object_resolve_path(path, &ambiguous);
2443 if (obj == NULL) {
2444 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2445 "Device '%s' not found", path);
2446 } else {
2447 if (ambiguous) {
2448 monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2450 object_property_parse(obj, value, property, &err);
2452 hmp_handle_error(mon, &err);
2455 void hmp_rocker(Monitor *mon, const QDict *qdict)
2457 const char *name = qdict_get_str(qdict, "name");
2458 RockerSwitch *rocker;
2459 Error *err = NULL;
2461 rocker = qmp_query_rocker(name, &err);
2462 if (err != NULL) {
2463 hmp_handle_error(mon, &err);
2464 return;
2467 monitor_printf(mon, "name: %s\n", rocker->name);
2468 monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2469 monitor_printf(mon, "ports: %d\n", rocker->ports);
2471 qapi_free_RockerSwitch(rocker);
2474 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2476 RockerPortList *list, *port;
2477 const char *name = qdict_get_str(qdict, "name");
2478 Error *err = NULL;
2480 list = qmp_query_rocker_ports(name, &err);
2481 if (err != NULL) {
2482 hmp_handle_error(mon, &err);
2483 return;
2486 monitor_printf(mon, " ena/ speed/ auto\n");
2487 monitor_printf(mon, " port link duplex neg?\n");
2489 for (port = list; port; port = port->next) {
2490 monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
2491 port->value->name,
2492 port->value->enabled ? port->value->link_up ?
2493 "up" : "down" : "!ena",
2494 port->value->speed == 10000 ? "10G" : "??",
2495 port->value->duplex ? "FD" : "HD",
2496 port->value->autoneg ? "Yes" : "No");
2499 qapi_free_RockerPortList(list);
2502 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2504 RockerOfDpaFlowList *list, *info;
2505 const char *name = qdict_get_str(qdict, "name");
2506 uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2507 Error *err = NULL;
2509 list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2510 if (err != NULL) {
2511 hmp_handle_error(mon, &err);
2512 return;
2515 monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2517 for (info = list; info; info = info->next) {
2518 RockerOfDpaFlow *flow = info->value;
2519 RockerOfDpaFlowKey *key = flow->key;
2520 RockerOfDpaFlowMask *mask = flow->mask;
2521 RockerOfDpaFlowAction *action = flow->action;
2523 if (flow->hits) {
2524 monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2525 key->priority, key->tbl_id, flow->hits);
2526 } else {
2527 monitor_printf(mon, "%-4d %-3d ",
2528 key->priority, key->tbl_id);
2531 if (key->has_in_pport) {
2532 monitor_printf(mon, " pport %d", key->in_pport);
2533 if (mask->has_in_pport) {
2534 monitor_printf(mon, "(0x%x)", mask->in_pport);
2538 if (key->has_vlan_id) {
2539 monitor_printf(mon, " vlan %d",
2540 key->vlan_id & VLAN_VID_MASK);
2541 if (mask->has_vlan_id) {
2542 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2546 if (key->has_tunnel_id) {
2547 monitor_printf(mon, " tunnel %d", key->tunnel_id);
2548 if (mask->has_tunnel_id) {
2549 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2553 if (key->has_eth_type) {
2554 switch (key->eth_type) {
2555 case 0x0806:
2556 monitor_printf(mon, " ARP");
2557 break;
2558 case 0x0800:
2559 monitor_printf(mon, " IP");
2560 break;
2561 case 0x86dd:
2562 monitor_printf(mon, " IPv6");
2563 break;
2564 case 0x8809:
2565 monitor_printf(mon, " LACP");
2566 break;
2567 case 0x88cc:
2568 monitor_printf(mon, " LLDP");
2569 break;
2570 default:
2571 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2572 break;
2576 if (key->has_eth_src) {
2577 if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2578 (mask->has_eth_src) &&
2579 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2580 monitor_printf(mon, " src <any mcast/bcast>");
2581 } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2582 (mask->has_eth_src) &&
2583 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2584 monitor_printf(mon, " src <any ucast>");
2585 } else {
2586 monitor_printf(mon, " src %s", key->eth_src);
2587 if (mask->has_eth_src) {
2588 monitor_printf(mon, "(%s)", mask->eth_src);
2593 if (key->has_eth_dst) {
2594 if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2595 (mask->has_eth_dst) &&
2596 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2597 monitor_printf(mon, " dst <any mcast/bcast>");
2598 } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2599 (mask->has_eth_dst) &&
2600 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2601 monitor_printf(mon, " dst <any ucast>");
2602 } else {
2603 monitor_printf(mon, " dst %s", key->eth_dst);
2604 if (mask->has_eth_dst) {
2605 monitor_printf(mon, "(%s)", mask->eth_dst);
2610 if (key->has_ip_proto) {
2611 monitor_printf(mon, " proto %d", key->ip_proto);
2612 if (mask->has_ip_proto) {
2613 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2617 if (key->has_ip_tos) {
2618 monitor_printf(mon, " TOS %d", key->ip_tos);
2619 if (mask->has_ip_tos) {
2620 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2624 if (key->has_ip_dst) {
2625 monitor_printf(mon, " dst %s", key->ip_dst);
2628 if (action->has_goto_tbl || action->has_group_id ||
2629 action->has_new_vlan_id) {
2630 monitor_printf(mon, " -->");
2633 if (action->has_new_vlan_id) {
2634 monitor_printf(mon, " apply new vlan %d",
2635 ntohs(action->new_vlan_id));
2638 if (action->has_group_id) {
2639 monitor_printf(mon, " write group 0x%08x", action->group_id);
2642 if (action->has_goto_tbl) {
2643 monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2646 monitor_printf(mon, "\n");
2649 qapi_free_RockerOfDpaFlowList(list);
2652 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2654 RockerOfDpaGroupList *list, *g;
2655 const char *name = qdict_get_str(qdict, "name");
2656 uint8_t type = qdict_get_try_int(qdict, "type", 9);
2657 Error *err = NULL;
2658 bool set = false;
2660 list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2661 if (err != NULL) {
2662 hmp_handle_error(mon, &err);
2663 return;
2666 monitor_printf(mon, "id (decode) --> buckets\n");
2668 for (g = list; g; g = g->next) {
2669 RockerOfDpaGroup *group = g->value;
2671 monitor_printf(mon, "0x%08x", group->id);
2673 monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2674 group->type == 1 ? "L2 rewrite" :
2675 group->type == 2 ? "L3 unicast" :
2676 group->type == 3 ? "L2 multicast" :
2677 group->type == 4 ? "L2 flood" :
2678 group->type == 5 ? "L3 interface" :
2679 group->type == 6 ? "L3 multicast" :
2680 group->type == 7 ? "L3 ECMP" :
2681 group->type == 8 ? "L2 overlay" :
2682 "unknown");
2684 if (group->has_vlan_id) {
2685 monitor_printf(mon, " vlan %d", group->vlan_id);
2688 if (group->has_pport) {
2689 monitor_printf(mon, " pport %d", group->pport);
2692 if (group->has_index) {
2693 monitor_printf(mon, " index %d", group->index);
2696 monitor_printf(mon, ") -->");
2698 if (group->has_set_vlan_id && group->set_vlan_id) {
2699 set = true;
2700 monitor_printf(mon, " set vlan %d",
2701 group->set_vlan_id & VLAN_VID_MASK);
2704 if (group->has_set_eth_src) {
2705 if (!set) {
2706 set = true;
2707 monitor_printf(mon, " set");
2709 monitor_printf(mon, " src %s", group->set_eth_src);
2712 if (group->has_set_eth_dst) {
2713 if (!set) {
2714 set = true;
2715 monitor_printf(mon, " set");
2717 monitor_printf(mon, " dst %s", group->set_eth_dst);
2720 set = false;
2722 if (group->has_ttl_check && group->ttl_check) {
2723 monitor_printf(mon, " check TTL");
2726 if (group->has_group_id && group->group_id) {
2727 monitor_printf(mon, " group id 0x%08x", group->group_id);
2730 if (group->has_pop_vlan && group->pop_vlan) {
2731 monitor_printf(mon, " pop vlan");
2734 if (group->has_out_pport) {
2735 monitor_printf(mon, " out pport %d", group->out_pport);
2738 if (group->has_group_ids) {
2739 struct uint32List *id;
2741 monitor_printf(mon, " groups [");
2742 for (id = group->group_ids; id; id = id->next) {
2743 monitor_printf(mon, "0x%08x", id->value);
2744 if (id->next) {
2745 monitor_printf(mon, ",");
2748 monitor_printf(mon, "]");
2751 monitor_printf(mon, "\n");
2754 qapi_free_RockerOfDpaGroupList(list);
2757 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2759 DumpQueryResult *result = qmp_query_dump(NULL);
2761 assert(result && result->status < DUMP_STATUS__MAX);
2762 monitor_printf(mon, "Status: %s\n", DumpStatus_lookup[result->status]);
2764 if (result->status == DUMP_STATUS_ACTIVE) {
2765 float percent = 0;
2766 assert(result->total != 0);
2767 percent = 100.0 * result->completed / result->total;
2768 monitor_printf(mon, "Finished: %.2f %%\n", percent);
2771 qapi_free_DumpQueryResult(result);
2774 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2776 ram_block_dump(mon);
2779 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2781 Error *err = NULL;
2782 HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2783 HotpluggableCPUList *saved = l;
2784 CpuInstanceProperties *c;
2786 if (err != NULL) {
2787 hmp_handle_error(mon, &err);
2788 return;
2791 monitor_printf(mon, "Hotpluggable CPUs:\n");
2792 while (l) {
2793 monitor_printf(mon, " type: \"%s\"\n", l->value->type);
2794 monitor_printf(mon, " vcpus_count: \"%" PRIu64 "\"\n",
2795 l->value->vcpus_count);
2796 if (l->value->has_qom_path) {
2797 monitor_printf(mon, " qom_path: \"%s\"\n", l->value->qom_path);
2800 c = l->value->props;
2801 monitor_printf(mon, " CPUInstance Properties:\n");
2802 if (c->has_node_id) {
2803 monitor_printf(mon, " node-id: \"%" PRIu64 "\"\n", c->node_id);
2805 if (c->has_socket_id) {
2806 monitor_printf(mon, " socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2808 if (c->has_core_id) {
2809 monitor_printf(mon, " core-id: \"%" PRIu64 "\"\n", c->core_id);
2811 if (c->has_thread_id) {
2812 monitor_printf(mon, " thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2815 l = l->next;
2818 qapi_free_HotpluggableCPUList(saved);
2821 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2823 Error *err = NULL;
2824 GuidInfo *info = qmp_query_vm_generation_id(&err);
2825 if (info) {
2826 monitor_printf(mon, "%s\n", info->guid);
2828 hmp_handle_error(mon, &err);
2829 qapi_free_GuidInfo(info);