Merge remote-tracking branch 'remotes/dgilbert/tags/pull-migration-20170906a' into...
[qemu/ar7.git] / hmp.c
blobcd046c6d714cb26a88eecb8cf81399202fa926df
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-visit.h"
35 #include "qom/object_interfaces.h"
36 #include "ui/console.h"
37 #include "block/nbd.h"
38 #include "block/qapi.h"
39 #include "qemu-io.h"
40 #include "qemu/cutils.h"
41 #include "qemu/error-report.h"
42 #include "exec/ramlist.h"
43 #include "hw/intc/intc.h"
44 #include "migration/snapshot.h"
45 #include "migration/misc.h"
47 #ifdef CONFIG_SPICE
48 #include <spice/enums.h>
49 #endif
51 static void hmp_handle_error(Monitor *mon, Error **errp)
53 assert(errp);
54 if (*errp) {
55 error_report_err(*errp);
59 void hmp_info_name(Monitor *mon, const QDict *qdict)
61 NameInfo *info;
63 info = qmp_query_name(NULL);
64 if (info->has_name) {
65 monitor_printf(mon, "%s\n", info->name);
67 qapi_free_NameInfo(info);
70 void hmp_info_version(Monitor *mon, const QDict *qdict)
72 VersionInfo *info;
74 info = qmp_query_version(NULL);
76 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
77 info->qemu->major, info->qemu->minor, info->qemu->micro,
78 info->package);
80 qapi_free_VersionInfo(info);
83 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
85 KvmInfo *info;
87 info = qmp_query_kvm(NULL);
88 monitor_printf(mon, "kvm support: ");
89 if (info->present) {
90 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
91 } else {
92 monitor_printf(mon, "not compiled\n");
95 qapi_free_KvmInfo(info);
98 void hmp_info_status(Monitor *mon, const QDict *qdict)
100 StatusInfo *info;
102 info = qmp_query_status(NULL);
104 monitor_printf(mon, "VM status: %s%s",
105 info->running ? "running" : "paused",
106 info->singlestep ? " (single step mode)" : "");
108 if (!info->running && info->status != RUN_STATE_PAUSED) {
109 monitor_printf(mon, " (%s)", RunState_str(info->status));
112 monitor_printf(mon, "\n");
114 qapi_free_StatusInfo(info);
117 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
119 UuidInfo *info;
121 info = qmp_query_uuid(NULL);
122 monitor_printf(mon, "%s\n", info->UUID);
123 qapi_free_UuidInfo(info);
126 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
128 ChardevInfoList *char_info, *info;
130 char_info = qmp_query_chardev(NULL);
131 for (info = char_info; info; info = info->next) {
132 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
133 info->value->filename);
136 qapi_free_ChardevInfoList(char_info);
139 void hmp_info_mice(Monitor *mon, const QDict *qdict)
141 MouseInfoList *mice_list, *mouse;
143 mice_list = qmp_query_mice(NULL);
144 if (!mice_list) {
145 monitor_printf(mon, "No mouse devices connected\n");
146 return;
149 for (mouse = mice_list; mouse; mouse = mouse->next) {
150 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
151 mouse->value->current ? '*' : ' ',
152 mouse->value->index, mouse->value->name,
153 mouse->value->absolute ? " (absolute)" : "");
156 qapi_free_MouseInfoList(mice_list);
159 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
161 MigrationInfo *info;
162 MigrationCapabilityStatusList *caps, *cap;
164 info = qmp_query_migrate(NULL);
165 caps = qmp_query_migrate_capabilities(NULL);
167 migration_global_dump(mon);
169 /* do not display parameters during setup */
170 if (info->has_status && caps) {
171 monitor_printf(mon, "capabilities: ");
172 for (cap = caps; cap; cap = cap->next) {
173 monitor_printf(mon, "%s: %s ",
174 MigrationCapability_str(cap->value->capability),
175 cap->value->state ? "on" : "off");
177 monitor_printf(mon, "\n");
180 if (info->has_status) {
181 monitor_printf(mon, "Migration status: %s",
182 MigrationStatus_str(info->status));
183 if (info->status == MIGRATION_STATUS_FAILED &&
184 info->has_error_desc) {
185 monitor_printf(mon, " (%s)\n", info->error_desc);
186 } else {
187 monitor_printf(mon, "\n");
190 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
191 info->total_time);
192 if (info->has_expected_downtime) {
193 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
194 info->expected_downtime);
196 if (info->has_downtime) {
197 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
198 info->downtime);
200 if (info->has_setup_time) {
201 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
202 info->setup_time);
206 if (info->has_ram) {
207 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
208 info->ram->transferred >> 10);
209 monitor_printf(mon, "throughput: %0.2f mbps\n",
210 info->ram->mbps);
211 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
212 info->ram->remaining >> 10);
213 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
214 info->ram->total >> 10);
215 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
216 info->ram->duplicate);
217 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
218 info->ram->skipped);
219 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
220 info->ram->normal);
221 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
222 info->ram->normal_bytes >> 10);
223 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
224 info->ram->dirty_sync_count);
225 monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
226 info->ram->page_size >> 10);
228 if (info->ram->dirty_pages_rate) {
229 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
230 info->ram->dirty_pages_rate);
232 if (info->ram->postcopy_requests) {
233 monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
234 info->ram->postcopy_requests);
238 if (info->has_disk) {
239 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
240 info->disk->transferred >> 10);
241 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
242 info->disk->remaining >> 10);
243 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
244 info->disk->total >> 10);
247 if (info->has_xbzrle_cache) {
248 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
249 info->xbzrle_cache->cache_size);
250 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
251 info->xbzrle_cache->bytes >> 10);
252 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
253 info->xbzrle_cache->pages);
254 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
255 info->xbzrle_cache->cache_miss);
256 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
257 info->xbzrle_cache->cache_miss_rate);
258 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
259 info->xbzrle_cache->overflow);
262 if (info->has_cpu_throttle_percentage) {
263 monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
264 info->cpu_throttle_percentage);
267 qapi_free_MigrationInfo(info);
268 qapi_free_MigrationCapabilityStatusList(caps);
271 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
273 MigrationCapabilityStatusList *caps, *cap;
275 caps = qmp_query_migrate_capabilities(NULL);
277 if (caps) {
278 for (cap = caps; cap; cap = cap->next) {
279 monitor_printf(mon, "%s: %s\n",
280 MigrationCapability_str(cap->value->capability),
281 cap->value->state ? "on" : "off");
285 qapi_free_MigrationCapabilityStatusList(caps);
288 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
290 MigrationParameters *params;
292 params = qmp_query_migrate_parameters(NULL);
294 if (params) {
295 assert(params->has_compress_level);
296 monitor_printf(mon, "%s: %" PRId64 "\n",
297 MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
298 params->compress_level);
299 assert(params->has_compress_threads);
300 monitor_printf(mon, "%s: %" PRId64 "\n",
301 MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
302 params->compress_threads);
303 assert(params->has_decompress_threads);
304 monitor_printf(mon, "%s: %" PRId64 "\n",
305 MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
306 params->decompress_threads);
307 assert(params->has_cpu_throttle_initial);
308 monitor_printf(mon, "%s: %" PRId64 "\n",
309 MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
310 params->cpu_throttle_initial);
311 assert(params->has_cpu_throttle_increment);
312 monitor_printf(mon, "%s: %" PRId64 "\n",
313 MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
314 params->cpu_throttle_increment);
315 assert(params->has_tls_creds);
316 monitor_printf(mon, "%s: '%s'\n",
317 MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
318 params->tls_creds);
319 assert(params->has_tls_hostname);
320 monitor_printf(mon, "%s: '%s'\n",
321 MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
322 params->tls_hostname);
323 assert(params->has_max_bandwidth);
324 monitor_printf(mon, "%s: %" PRId64 " bytes/second\n",
325 MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
326 params->max_bandwidth);
327 assert(params->has_downtime_limit);
328 monitor_printf(mon, "%s: %" PRId64 " milliseconds\n",
329 MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
330 params->downtime_limit);
331 assert(params->has_x_checkpoint_delay);
332 monitor_printf(mon, "%s: %" PRId64 "\n",
333 MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
334 params->x_checkpoint_delay);
335 assert(params->has_block_incremental);
336 monitor_printf(mon, "%s: %s\n",
337 MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
338 params->block_incremental ? "on" : "off");
341 qapi_free_MigrationParameters(params);
344 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
346 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
347 qmp_query_migrate_cache_size(NULL) >> 10);
350 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
352 CpuInfoList *cpu_list, *cpu;
354 cpu_list = qmp_query_cpus(NULL);
356 for (cpu = cpu_list; cpu; cpu = cpu->next) {
357 int active = ' ';
359 if (cpu->value->CPU == monitor_get_cpu_index()) {
360 active = '*';
363 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
365 switch (cpu->value->arch) {
366 case CPU_INFO_ARCH_X86:
367 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->u.x86.pc);
368 break;
369 case CPU_INFO_ARCH_PPC:
370 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->u.ppc.nip);
371 break;
372 case CPU_INFO_ARCH_SPARC:
373 monitor_printf(mon, " pc=0x%016" PRIx64,
374 cpu->value->u.q_sparc.pc);
375 monitor_printf(mon, " npc=0x%016" PRIx64,
376 cpu->value->u.q_sparc.npc);
377 break;
378 case CPU_INFO_ARCH_MIPS:
379 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.q_mips.PC);
380 break;
381 case CPU_INFO_ARCH_TRICORE:
382 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.tricore.PC);
383 break;
384 default:
385 break;
388 if (cpu->value->halted) {
389 monitor_printf(mon, " (halted)");
392 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
395 qapi_free_CpuInfoList(cpu_list);
398 static void print_block_info(Monitor *mon, BlockInfo *info,
399 BlockDeviceInfo *inserted, bool verbose)
401 ImageInfo *image_info;
403 assert(!info || !info->has_inserted || info->inserted == inserted);
405 if (info && *info->device) {
406 monitor_printf(mon, "%s", info->device);
407 if (inserted && inserted->has_node_name) {
408 monitor_printf(mon, " (%s)", inserted->node_name);
410 } else {
411 assert(info || inserted);
412 monitor_printf(mon, "%s",
413 inserted && inserted->has_node_name ? inserted->node_name
414 : info && info->has_qdev ? info->qdev
415 : "<anonymous>");
418 if (inserted) {
419 monitor_printf(mon, ": %s (%s%s%s)\n",
420 inserted->file,
421 inserted->drv,
422 inserted->ro ? ", read-only" : "",
423 inserted->encrypted ? ", encrypted" : "");
424 } else {
425 monitor_printf(mon, ": [not inserted]\n");
428 if (info) {
429 if (info->has_qdev) {
430 monitor_printf(mon, " Attached to: %s\n", info->qdev);
432 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
433 monitor_printf(mon, " I/O status: %s\n",
434 BlockDeviceIoStatus_str(info->io_status));
437 if (info->removable) {
438 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
439 info->locked ? "" : "not ",
440 info->tray_open ? "open" : "closed");
445 if (!inserted) {
446 return;
449 monitor_printf(mon, " Cache mode: %s%s%s\n",
450 inserted->cache->writeback ? "writeback" : "writethrough",
451 inserted->cache->direct ? ", direct" : "",
452 inserted->cache->no_flush ? ", ignore flushes" : "");
454 if (inserted->has_backing_file) {
455 monitor_printf(mon,
456 " Backing file: %s "
457 "(chain depth: %" PRId64 ")\n",
458 inserted->backing_file,
459 inserted->backing_file_depth);
462 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
463 monitor_printf(mon, " Detect zeroes: %s\n",
464 BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
467 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
468 inserted->iops || inserted->iops_rd || inserted->iops_wr)
470 monitor_printf(mon, " I/O throttling: bps=%" PRId64
471 " bps_rd=%" PRId64 " bps_wr=%" PRId64
472 " bps_max=%" PRId64
473 " bps_rd_max=%" PRId64
474 " bps_wr_max=%" PRId64
475 " iops=%" PRId64 " iops_rd=%" PRId64
476 " iops_wr=%" PRId64
477 " iops_max=%" PRId64
478 " iops_rd_max=%" PRId64
479 " iops_wr_max=%" PRId64
480 " iops_size=%" PRId64
481 " group=%s\n",
482 inserted->bps,
483 inserted->bps_rd,
484 inserted->bps_wr,
485 inserted->bps_max,
486 inserted->bps_rd_max,
487 inserted->bps_wr_max,
488 inserted->iops,
489 inserted->iops_rd,
490 inserted->iops_wr,
491 inserted->iops_max,
492 inserted->iops_rd_max,
493 inserted->iops_wr_max,
494 inserted->iops_size,
495 inserted->group);
498 if (verbose) {
499 monitor_printf(mon, "\nImages:\n");
500 image_info = inserted->image;
501 while (1) {
502 bdrv_image_info_dump((fprintf_function)monitor_printf,
503 mon, image_info);
504 if (image_info->has_backing_image) {
505 image_info = image_info->backing_image;
506 } else {
507 break;
513 void hmp_info_block(Monitor *mon, const QDict *qdict)
515 BlockInfoList *block_list, *info;
516 BlockDeviceInfoList *blockdev_list, *blockdev;
517 const char *device = qdict_get_try_str(qdict, "device");
518 bool verbose = qdict_get_try_bool(qdict, "verbose", false);
519 bool nodes = qdict_get_try_bool(qdict, "nodes", false);
520 bool printed = false;
522 /* Print BlockBackend information */
523 if (!nodes) {
524 block_list = qmp_query_block(NULL);
525 } else {
526 block_list = NULL;
529 for (info = block_list; info; info = info->next) {
530 if (device && strcmp(device, info->value->device)) {
531 continue;
534 if (info != block_list) {
535 monitor_printf(mon, "\n");
538 print_block_info(mon, info->value, info->value->has_inserted
539 ? info->value->inserted : NULL,
540 verbose);
541 printed = true;
544 qapi_free_BlockInfoList(block_list);
546 if ((!device && !nodes) || printed) {
547 return;
550 /* Print node information */
551 blockdev_list = qmp_query_named_block_nodes(NULL);
552 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
553 assert(blockdev->value->has_node_name);
554 if (device && strcmp(device, blockdev->value->node_name)) {
555 continue;
558 if (blockdev != blockdev_list) {
559 monitor_printf(mon, "\n");
562 print_block_info(mon, NULL, blockdev->value, verbose);
564 qapi_free_BlockDeviceInfoList(blockdev_list);
567 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
569 BlockStatsList *stats_list, *stats;
571 stats_list = qmp_query_blockstats(false, false, NULL);
573 for (stats = stats_list; stats; stats = stats->next) {
574 if (!stats->value->has_device) {
575 continue;
578 monitor_printf(mon, "%s:", stats->value->device);
579 monitor_printf(mon, " rd_bytes=%" PRId64
580 " wr_bytes=%" PRId64
581 " rd_operations=%" PRId64
582 " wr_operations=%" PRId64
583 " flush_operations=%" PRId64
584 " wr_total_time_ns=%" PRId64
585 " rd_total_time_ns=%" PRId64
586 " flush_total_time_ns=%" PRId64
587 " rd_merged=%" PRId64
588 " wr_merged=%" PRId64
589 " idle_time_ns=%" PRId64
590 "\n",
591 stats->value->stats->rd_bytes,
592 stats->value->stats->wr_bytes,
593 stats->value->stats->rd_operations,
594 stats->value->stats->wr_operations,
595 stats->value->stats->flush_operations,
596 stats->value->stats->wr_total_time_ns,
597 stats->value->stats->rd_total_time_ns,
598 stats->value->stats->flush_total_time_ns,
599 stats->value->stats->rd_merged,
600 stats->value->stats->wr_merged,
601 stats->value->stats->idle_time_ns);
604 qapi_free_BlockStatsList(stats_list);
607 /* Helper for hmp_info_vnc_clients, _servers */
608 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
609 const char *name)
611 monitor_printf(mon, " %s: %s:%s (%s%s)\n",
612 name,
613 info->host,
614 info->service,
615 NetworkAddressFamily_str(info->family),
616 info->websocket ? " (Websocket)" : "");
619 /* Helper displaying and auth and crypt info */
620 static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
621 VncPrimaryAuth auth,
622 VncVencryptSubAuth *vencrypt)
624 monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
625 VncPrimaryAuth_str(auth),
626 vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
629 static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
631 while (client) {
632 VncClientInfo *cinfo = client->value;
634 hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
635 monitor_printf(mon, " x509_dname: %s\n",
636 cinfo->has_x509_dname ?
637 cinfo->x509_dname : "none");
638 monitor_printf(mon, " sasl_username: %s\n",
639 cinfo->has_sasl_username ?
640 cinfo->sasl_username : "none");
642 client = client->next;
646 static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
648 while (server) {
649 VncServerInfo2 *sinfo = server->value;
650 hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
651 hmp_info_vnc_authcrypt(mon, " ", sinfo->auth,
652 sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
653 server = server->next;
657 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
659 VncInfo2List *info2l;
660 Error *err = NULL;
662 info2l = qmp_query_vnc_servers(&err);
663 if (err) {
664 error_report_err(err);
665 return;
667 if (!info2l) {
668 monitor_printf(mon, "None\n");
669 return;
672 while (info2l) {
673 VncInfo2 *info = info2l->value;
674 monitor_printf(mon, "%s:\n", info->id);
675 hmp_info_vnc_servers(mon, info->server);
676 hmp_info_vnc_clients(mon, info->clients);
677 if (!info->server) {
678 /* The server entry displays its auth, we only
679 * need to display in the case of 'reverse' connections
680 * where there's no server.
682 hmp_info_vnc_authcrypt(mon, " ", info->auth,
683 info->has_vencrypt ? &info->vencrypt : NULL);
685 if (info->has_display) {
686 monitor_printf(mon, " Display: %s\n", info->display);
688 info2l = info2l->next;
691 qapi_free_VncInfo2List(info2l);
695 #ifdef CONFIG_SPICE
696 void hmp_info_spice(Monitor *mon, const QDict *qdict)
698 SpiceChannelList *chan;
699 SpiceInfo *info;
700 const char *channel_name;
701 const char * const channel_names[] = {
702 [SPICE_CHANNEL_MAIN] = "main",
703 [SPICE_CHANNEL_DISPLAY] = "display",
704 [SPICE_CHANNEL_INPUTS] = "inputs",
705 [SPICE_CHANNEL_CURSOR] = "cursor",
706 [SPICE_CHANNEL_PLAYBACK] = "playback",
707 [SPICE_CHANNEL_RECORD] = "record",
708 [SPICE_CHANNEL_TUNNEL] = "tunnel",
709 [SPICE_CHANNEL_SMARTCARD] = "smartcard",
710 [SPICE_CHANNEL_USBREDIR] = "usbredir",
711 [SPICE_CHANNEL_PORT] = "port",
712 #if 0
713 /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
714 * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
715 * as quick fix for build failures with older versions. */
716 [SPICE_CHANNEL_WEBDAV] = "webdav",
717 #endif
720 info = qmp_query_spice(NULL);
722 if (!info->enabled) {
723 monitor_printf(mon, "Server: disabled\n");
724 goto out;
727 monitor_printf(mon, "Server:\n");
728 if (info->has_port) {
729 monitor_printf(mon, " address: %s:%" PRId64 "\n",
730 info->host, info->port);
732 if (info->has_tls_port) {
733 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
734 info->host, info->tls_port);
736 monitor_printf(mon, " migrated: %s\n",
737 info->migrated ? "true" : "false");
738 monitor_printf(mon, " auth: %s\n", info->auth);
739 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
740 monitor_printf(mon, " mouse-mode: %s\n",
741 SpiceQueryMouseMode_str(info->mouse_mode));
743 if (!info->has_channels || info->channels == NULL) {
744 monitor_printf(mon, "Channels: none\n");
745 } else {
746 for (chan = info->channels; chan; chan = chan->next) {
747 monitor_printf(mon, "Channel:\n");
748 monitor_printf(mon, " address: %s:%s%s\n",
749 chan->value->host, chan->value->port,
750 chan->value->tls ? " [tls]" : "");
751 monitor_printf(mon, " session: %" PRId64 "\n",
752 chan->value->connection_id);
753 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
754 chan->value->channel_type, chan->value->channel_id);
756 channel_name = "unknown";
757 if (chan->value->channel_type > 0 &&
758 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
759 channel_names[chan->value->channel_type]) {
760 channel_name = channel_names[chan->value->channel_type];
763 monitor_printf(mon, " channel name: %s\n", channel_name);
767 out:
768 qapi_free_SpiceInfo(info);
770 #endif
772 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
774 BalloonInfo *info;
775 Error *err = NULL;
777 info = qmp_query_balloon(&err);
778 if (err) {
779 error_report_err(err);
780 return;
783 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
785 qapi_free_BalloonInfo(info);
788 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
790 PciMemoryRegionList *region;
792 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
793 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
794 dev->slot, dev->function);
795 monitor_printf(mon, " ");
797 if (dev->class_info->has_desc) {
798 monitor_printf(mon, "%s", dev->class_info->desc);
799 } else {
800 monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
803 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
804 dev->id->vendor, dev->id->device);
806 if (dev->has_irq) {
807 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
810 if (dev->has_pci_bridge) {
811 monitor_printf(mon, " BUS %" PRId64 ".\n",
812 dev->pci_bridge->bus->number);
813 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
814 dev->pci_bridge->bus->secondary);
815 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
816 dev->pci_bridge->bus->subordinate);
818 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
819 dev->pci_bridge->bus->io_range->base,
820 dev->pci_bridge->bus->io_range->limit);
822 monitor_printf(mon,
823 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
824 dev->pci_bridge->bus->memory_range->base,
825 dev->pci_bridge->bus->memory_range->limit);
827 monitor_printf(mon, " prefetchable memory range "
828 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
829 dev->pci_bridge->bus->prefetchable_range->base,
830 dev->pci_bridge->bus->prefetchable_range->limit);
833 for (region = dev->regions; region; region = region->next) {
834 uint64_t addr, size;
836 addr = region->value->address;
837 size = region->value->size;
839 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
841 if (!strcmp(region->value->type, "io")) {
842 monitor_printf(mon, "I/O at 0x%04" PRIx64
843 " [0x%04" PRIx64 "].\n",
844 addr, addr + size - 1);
845 } else {
846 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
847 " [0x%08" PRIx64 "].\n",
848 region->value->mem_type_64 ? 64 : 32,
849 region->value->prefetch ? " prefetchable" : "",
850 addr, addr + size - 1);
854 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
856 if (dev->has_pci_bridge) {
857 if (dev->pci_bridge->has_devices) {
858 PciDeviceInfoList *cdev;
859 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
860 hmp_info_pci_device(mon, cdev->value);
866 static int hmp_info_irq_foreach(Object *obj, void *opaque)
868 InterruptStatsProvider *intc;
869 InterruptStatsProviderClass *k;
870 Monitor *mon = opaque;
872 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
873 intc = INTERRUPT_STATS_PROVIDER(obj);
874 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
875 uint64_t *irq_counts;
876 unsigned int nb_irqs, i;
877 if (k->get_statistics &&
878 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
879 if (nb_irqs > 0) {
880 monitor_printf(mon, "IRQ statistics for %s:\n",
881 object_get_typename(obj));
882 for (i = 0; i < nb_irqs; i++) {
883 if (irq_counts[i] > 0) {
884 monitor_printf(mon, "%2d: %" PRId64 "\n", i,
885 irq_counts[i]);
889 } else {
890 monitor_printf(mon, "IRQ statistics not available for %s.\n",
891 object_get_typename(obj));
895 return 0;
898 void hmp_info_irq(Monitor *mon, const QDict *qdict)
900 object_child_foreach_recursive(object_get_root(),
901 hmp_info_irq_foreach, mon);
904 static int hmp_info_pic_foreach(Object *obj, void *opaque)
906 InterruptStatsProvider *intc;
907 InterruptStatsProviderClass *k;
908 Monitor *mon = opaque;
910 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
911 intc = INTERRUPT_STATS_PROVIDER(obj);
912 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
913 if (k->print_info) {
914 k->print_info(intc, mon);
915 } else {
916 monitor_printf(mon, "Interrupt controller information not available for %s.\n",
917 object_get_typename(obj));
921 return 0;
924 void hmp_info_pic(Monitor *mon, const QDict *qdict)
926 object_child_foreach_recursive(object_get_root(),
927 hmp_info_pic_foreach, mon);
930 void hmp_info_pci(Monitor *mon, const QDict *qdict)
932 PciInfoList *info_list, *info;
933 Error *err = NULL;
935 info_list = qmp_query_pci(&err);
936 if (err) {
937 monitor_printf(mon, "PCI devices not supported\n");
938 error_free(err);
939 return;
942 for (info = info_list; info; info = info->next) {
943 PciDeviceInfoList *dev;
945 for (dev = info->value->devices; dev; dev = dev->next) {
946 hmp_info_pci_device(mon, dev->value);
950 qapi_free_PciInfoList(info_list);
953 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
955 BlockJobInfoList *list;
956 Error *err = NULL;
958 list = qmp_query_block_jobs(&err);
959 assert(!err);
961 if (!list) {
962 monitor_printf(mon, "No active jobs\n");
963 return;
966 while (list) {
967 if (strcmp(list->value->type, "stream") == 0) {
968 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
969 " of %" PRId64 " bytes, speed limit %" PRId64
970 " bytes/s\n",
971 list->value->device,
972 list->value->offset,
973 list->value->len,
974 list->value->speed);
975 } else {
976 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
977 " of %" PRId64 " bytes, speed limit %" PRId64
978 " bytes/s\n",
979 list->value->type,
980 list->value->device,
981 list->value->offset,
982 list->value->len,
983 list->value->speed);
985 list = list->next;
988 qapi_free_BlockJobInfoList(list);
991 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
993 TPMInfoList *info_list, *info;
994 Error *err = NULL;
995 unsigned int c = 0;
996 TPMPassthroughOptions *tpo;
998 info_list = qmp_query_tpm(&err);
999 if (err) {
1000 monitor_printf(mon, "TPM device not supported\n");
1001 error_free(err);
1002 return;
1005 if (info_list) {
1006 monitor_printf(mon, "TPM device:\n");
1009 for (info = info_list; info; info = info->next) {
1010 TPMInfo *ti = info->value;
1011 monitor_printf(mon, " tpm%d: model=%s\n",
1012 c, TpmModel_str(ti->model));
1014 monitor_printf(mon, " \\ %s: type=%s",
1015 ti->id, TpmTypeOptionsKind_str(ti->options->type));
1017 switch (ti->options->type) {
1018 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
1019 tpo = ti->options->u.passthrough.data;
1020 monitor_printf(mon, "%s%s%s%s",
1021 tpo->has_path ? ",path=" : "",
1022 tpo->has_path ? tpo->path : "",
1023 tpo->has_cancel_path ? ",cancel-path=" : "",
1024 tpo->has_cancel_path ? tpo->cancel_path : "");
1025 break;
1026 case TPM_TYPE_OPTIONS_KIND__MAX:
1027 break;
1029 monitor_printf(mon, "\n");
1030 c++;
1032 qapi_free_TPMInfoList(info_list);
1035 void hmp_quit(Monitor *mon, const QDict *qdict)
1037 monitor_suspend(mon);
1038 qmp_quit(NULL);
1041 void hmp_stop(Monitor *mon, const QDict *qdict)
1043 qmp_stop(NULL);
1046 void hmp_system_reset(Monitor *mon, const QDict *qdict)
1048 qmp_system_reset(NULL);
1051 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1053 qmp_system_powerdown(NULL);
1056 void hmp_cpu(Monitor *mon, const QDict *qdict)
1058 int64_t cpu_index;
1060 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1061 use it are converted to the QAPI */
1062 cpu_index = qdict_get_int(qdict, "index");
1063 if (monitor_set_cpu(cpu_index) < 0) {
1064 monitor_printf(mon, "invalid CPU index\n");
1068 void hmp_memsave(Monitor *mon, const QDict *qdict)
1070 uint32_t size = qdict_get_int(qdict, "size");
1071 const char *filename = qdict_get_str(qdict, "filename");
1072 uint64_t addr = qdict_get_int(qdict, "val");
1073 Error *err = NULL;
1074 int cpu_index = monitor_get_cpu_index();
1076 if (cpu_index < 0) {
1077 monitor_printf(mon, "No CPU available\n");
1078 return;
1081 qmp_memsave(addr, size, filename, true, cpu_index, &err);
1082 hmp_handle_error(mon, &err);
1085 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1087 uint32_t size = qdict_get_int(qdict, "size");
1088 const char *filename = qdict_get_str(qdict, "filename");
1089 uint64_t addr = qdict_get_int(qdict, "val");
1090 Error *err = NULL;
1092 qmp_pmemsave(addr, size, filename, &err);
1093 hmp_handle_error(mon, &err);
1096 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1098 const char *chardev = qdict_get_str(qdict, "device");
1099 const char *data = qdict_get_str(qdict, "data");
1100 Error *err = NULL;
1102 qmp_ringbuf_write(chardev, data, false, 0, &err);
1104 hmp_handle_error(mon, &err);
1107 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1109 uint32_t size = qdict_get_int(qdict, "size");
1110 const char *chardev = qdict_get_str(qdict, "device");
1111 char *data;
1112 Error *err = NULL;
1113 int i;
1115 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1116 if (err) {
1117 error_report_err(err);
1118 return;
1121 for (i = 0; data[i]; i++) {
1122 unsigned char ch = data[i];
1124 if (ch == '\\') {
1125 monitor_printf(mon, "\\\\");
1126 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1127 monitor_printf(mon, "\\u%04X", ch);
1128 } else {
1129 monitor_printf(mon, "%c", ch);
1133 monitor_printf(mon, "\n");
1134 g_free(data);
1137 void hmp_cont(Monitor *mon, const QDict *qdict)
1139 Error *err = NULL;
1141 qmp_cont(&err);
1142 hmp_handle_error(mon, &err);
1145 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1147 qmp_system_wakeup(NULL);
1150 void hmp_nmi(Monitor *mon, const QDict *qdict)
1152 Error *err = NULL;
1154 qmp_inject_nmi(&err);
1155 hmp_handle_error(mon, &err);
1158 void hmp_set_link(Monitor *mon, const QDict *qdict)
1160 const char *name = qdict_get_str(qdict, "name");
1161 bool up = qdict_get_bool(qdict, "up");
1162 Error *err = NULL;
1164 qmp_set_link(name, up, &err);
1165 hmp_handle_error(mon, &err);
1168 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1170 const char *device = qdict_get_str(qdict, "device");
1171 const char *password = qdict_get_str(qdict, "password");
1172 Error *err = NULL;
1174 qmp_block_passwd(true, device, false, NULL, password, &err);
1175 hmp_handle_error(mon, &err);
1178 void hmp_balloon(Monitor *mon, const QDict *qdict)
1180 int64_t value = qdict_get_int(qdict, "value");
1181 Error *err = NULL;
1183 qmp_balloon(value, &err);
1184 if (err) {
1185 error_report_err(err);
1189 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1191 const char *device = qdict_get_str(qdict, "device");
1192 int64_t size = qdict_get_int(qdict, "size");
1193 Error *err = NULL;
1195 qmp_block_resize(true, device, false, NULL, size, &err);
1196 hmp_handle_error(mon, &err);
1199 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1201 const char *filename = qdict_get_str(qdict, "target");
1202 const char *format = qdict_get_try_str(qdict, "format");
1203 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1204 bool full = qdict_get_try_bool(qdict, "full", false);
1205 Error *err = NULL;
1206 DriveMirror mirror = {
1207 .device = (char *)qdict_get_str(qdict, "device"),
1208 .target = (char *)filename,
1209 .has_format = !!format,
1210 .format = (char *)format,
1211 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1212 .has_mode = true,
1213 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1214 .unmap = true,
1217 if (!filename) {
1218 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1219 hmp_handle_error(mon, &err);
1220 return;
1222 qmp_drive_mirror(&mirror, &err);
1223 hmp_handle_error(mon, &err);
1226 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1228 const char *device = qdict_get_str(qdict, "device");
1229 const char *filename = qdict_get_str(qdict, "target");
1230 const char *format = qdict_get_try_str(qdict, "format");
1231 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1232 bool full = qdict_get_try_bool(qdict, "full", false);
1233 bool compress = qdict_get_try_bool(qdict, "compress", false);
1234 Error *err = NULL;
1235 DriveBackup backup = {
1236 .device = (char *)device,
1237 .target = (char *)filename,
1238 .has_format = !!format,
1239 .format = (char *)format,
1240 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1241 .has_mode = true,
1242 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1243 .has_compress = !!compress,
1244 .compress = compress,
1247 if (!filename) {
1248 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1249 hmp_handle_error(mon, &err);
1250 return;
1253 qmp_drive_backup(&backup, &err);
1254 hmp_handle_error(mon, &err);
1257 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1259 const char *device = qdict_get_str(qdict, "device");
1260 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1261 const char *format = qdict_get_try_str(qdict, "format");
1262 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1263 enum NewImageMode mode;
1264 Error *err = NULL;
1266 if (!filename) {
1267 /* In the future, if 'snapshot-file' is not specified, the snapshot
1268 will be taken internally. Today it's actually required. */
1269 error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1270 hmp_handle_error(mon, &err);
1271 return;
1274 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1275 qmp_blockdev_snapshot_sync(true, device, false, NULL,
1276 filename, false, NULL,
1277 !!format, format,
1278 true, mode, &err);
1279 hmp_handle_error(mon, &err);
1282 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1284 const char *device = qdict_get_str(qdict, "device");
1285 const char *name = qdict_get_str(qdict, "name");
1286 Error *err = NULL;
1288 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1289 hmp_handle_error(mon, &err);
1292 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1294 const char *device = qdict_get_str(qdict, "device");
1295 const char *name = qdict_get_str(qdict, "name");
1296 const char *id = qdict_get_try_str(qdict, "id");
1297 Error *err = NULL;
1299 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1300 true, name, &err);
1301 hmp_handle_error(mon, &err);
1304 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1306 int saved_vm_running = runstate_is_running();
1307 const char *name = qdict_get_str(qdict, "name");
1308 Error *err = NULL;
1310 vm_stop(RUN_STATE_RESTORE_VM);
1312 if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1313 vm_start();
1315 hmp_handle_error(mon, &err);
1318 void hmp_savevm(Monitor *mon, const QDict *qdict)
1320 Error *err = NULL;
1322 save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1323 hmp_handle_error(mon, &err);
1326 void hmp_delvm(Monitor *mon, const QDict *qdict)
1328 BlockDriverState *bs;
1329 Error *err;
1330 const char *name = qdict_get_str(qdict, "name");
1332 if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1333 error_reportf_err(err,
1334 "Error while deleting snapshot on device '%s': ",
1335 bdrv_get_device_name(bs));
1339 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1341 BlockDriverState *bs, *bs1;
1342 BdrvNextIterator it1;
1343 QEMUSnapshotInfo *sn_tab, *sn;
1344 bool no_snapshot = true;
1345 int nb_sns, i;
1346 int total;
1347 int *global_snapshots;
1348 AioContext *aio_context;
1350 typedef struct SnapshotEntry {
1351 QEMUSnapshotInfo sn;
1352 QTAILQ_ENTRY(SnapshotEntry) next;
1353 } SnapshotEntry;
1355 typedef struct ImageEntry {
1356 const char *imagename;
1357 QTAILQ_ENTRY(ImageEntry) next;
1358 QTAILQ_HEAD(, SnapshotEntry) snapshots;
1359 } ImageEntry;
1361 QTAILQ_HEAD(, ImageEntry) image_list =
1362 QTAILQ_HEAD_INITIALIZER(image_list);
1364 ImageEntry *image_entry, *next_ie;
1365 SnapshotEntry *snapshot_entry;
1367 bs = bdrv_all_find_vmstate_bs();
1368 if (!bs) {
1369 monitor_printf(mon, "No available block device supports snapshots\n");
1370 return;
1372 aio_context = bdrv_get_aio_context(bs);
1374 aio_context_acquire(aio_context);
1375 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1376 aio_context_release(aio_context);
1378 if (nb_sns < 0) {
1379 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1380 return;
1383 for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1384 int bs1_nb_sns = 0;
1385 ImageEntry *ie;
1386 SnapshotEntry *se;
1387 AioContext *ctx = bdrv_get_aio_context(bs1);
1389 aio_context_acquire(ctx);
1390 if (bdrv_can_snapshot(bs1)) {
1391 sn = NULL;
1392 bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1393 if (bs1_nb_sns > 0) {
1394 no_snapshot = false;
1395 ie = g_new0(ImageEntry, 1);
1396 ie->imagename = bdrv_get_device_name(bs1);
1397 QTAILQ_INIT(&ie->snapshots);
1398 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1399 for (i = 0; i < bs1_nb_sns; i++) {
1400 se = g_new0(SnapshotEntry, 1);
1401 se->sn = sn[i];
1402 QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1405 g_free(sn);
1407 aio_context_release(ctx);
1410 if (no_snapshot) {
1411 monitor_printf(mon, "There is no snapshot available.\n");
1412 return;
1415 global_snapshots = g_new0(int, nb_sns);
1416 total = 0;
1417 for (i = 0; i < nb_sns; i++) {
1418 SnapshotEntry *next_sn;
1419 if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1420 global_snapshots[total] = i;
1421 total++;
1422 QTAILQ_FOREACH(image_entry, &image_list, next) {
1423 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1424 next, next_sn) {
1425 if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1426 QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1427 next);
1428 g_free(snapshot_entry);
1435 monitor_printf(mon, "List of snapshots present on all disks:\n");
1437 if (total > 0) {
1438 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1439 monitor_printf(mon, "\n");
1440 for (i = 0; i < total; i++) {
1441 sn = &sn_tab[global_snapshots[i]];
1442 /* The ID is not guaranteed to be the same on all images, so
1443 * overwrite it.
1445 pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1446 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1447 monitor_printf(mon, "\n");
1449 } else {
1450 monitor_printf(mon, "None\n");
1453 QTAILQ_FOREACH(image_entry, &image_list, next) {
1454 if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1455 continue;
1457 monitor_printf(mon,
1458 "\nList of partial (non-loadable) snapshots on '%s':\n",
1459 image_entry->imagename);
1460 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1461 monitor_printf(mon, "\n");
1462 QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1463 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1464 &snapshot_entry->sn);
1465 monitor_printf(mon, "\n");
1469 QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1470 SnapshotEntry *next_sn;
1471 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1472 next_sn) {
1473 g_free(snapshot_entry);
1475 g_free(image_entry);
1477 g_free(sn_tab);
1478 g_free(global_snapshots);
1482 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1484 qmp_migrate_cancel(NULL);
1487 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1489 Error *err = NULL;
1490 const char *uri = qdict_get_str(qdict, "uri");
1492 qmp_migrate_incoming(uri, &err);
1494 hmp_handle_error(mon, &err);
1497 /* Kept for backwards compatibility */
1498 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1500 double value = qdict_get_double(qdict, "value");
1501 qmp_migrate_set_downtime(value, NULL);
1504 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1506 int64_t value = qdict_get_int(qdict, "value");
1507 Error *err = NULL;
1509 qmp_migrate_set_cache_size(value, &err);
1510 if (err) {
1511 error_report_err(err);
1512 return;
1516 /* Kept for backwards compatibility */
1517 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1519 int64_t value = qdict_get_int(qdict, "value");
1520 qmp_migrate_set_speed(value, NULL);
1523 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1525 const char *cap = qdict_get_str(qdict, "capability");
1526 bool state = qdict_get_bool(qdict, "state");
1527 Error *err = NULL;
1528 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1529 int val;
1531 val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
1532 if (val < 0) {
1533 goto end;
1536 caps->value = g_malloc0(sizeof(*caps->value));
1537 caps->value->capability = val;
1538 caps->value->state = state;
1539 caps->next = NULL;
1540 qmp_migrate_set_capabilities(caps, &err);
1542 end:
1543 qapi_free_MigrationCapabilityStatusList(caps);
1545 if (err) {
1546 error_report_err(err);
1550 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1552 const char *param = qdict_get_str(qdict, "parameter");
1553 const char *valuestr = qdict_get_str(qdict, "value");
1554 Visitor *v = string_input_visitor_new(valuestr);
1555 MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
1556 uint64_t valuebw = 0;
1557 Error *err = NULL;
1558 int val, ret;
1560 val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
1561 if (val < 0) {
1562 goto cleanup;
1565 switch (val) {
1566 case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1567 p->has_compress_level = true;
1568 visit_type_int(v, param, &p->compress_level, &err);
1569 break;
1570 case MIGRATION_PARAMETER_COMPRESS_THREADS:
1571 p->has_compress_threads = true;
1572 visit_type_int(v, param, &p->compress_threads, &err);
1573 break;
1574 case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1575 p->has_decompress_threads = true;
1576 visit_type_int(v, param, &p->decompress_threads, &err);
1577 break;
1578 case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1579 p->has_cpu_throttle_initial = true;
1580 visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1581 break;
1582 case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1583 p->has_cpu_throttle_increment = true;
1584 visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1585 break;
1586 case MIGRATION_PARAMETER_TLS_CREDS:
1587 p->has_tls_creds = true;
1588 p->tls_creds = g_new0(StrOrNull, 1);
1589 p->tls_creds->type = QTYPE_QSTRING;
1590 visit_type_str(v, param, &p->tls_creds->u.s, &err);
1591 break;
1592 case MIGRATION_PARAMETER_TLS_HOSTNAME:
1593 p->has_tls_hostname = true;
1594 p->tls_hostname = g_new0(StrOrNull, 1);
1595 p->tls_hostname->type = QTYPE_QSTRING;
1596 visit_type_str(v, param, &p->tls_hostname->u.s, &err);
1597 break;
1598 case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1599 p->has_max_bandwidth = true;
1601 * Can't use visit_type_size() here, because it
1602 * defaults to Bytes rather than Mebibytes.
1604 ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1605 if (ret < 0 || valuebw > INT64_MAX
1606 || (size_t)valuebw != valuebw) {
1607 error_setg(&err, "Invalid size %s", valuestr);
1608 break;
1610 p->max_bandwidth = valuebw;
1611 break;
1612 case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1613 p->has_downtime_limit = true;
1614 visit_type_int(v, param, &p->downtime_limit, &err);
1615 break;
1616 case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1617 p->has_x_checkpoint_delay = true;
1618 visit_type_int(v, param, &p->x_checkpoint_delay, &err);
1619 break;
1620 case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1621 p->has_block_incremental = true;
1622 visit_type_bool(v, param, &p->block_incremental, &err);
1623 break;
1624 default:
1625 assert(0);
1628 if (err) {
1629 goto cleanup;
1632 qmp_migrate_set_parameters(p, &err);
1634 cleanup:
1635 qapi_free_MigrateSetParameters(p);
1636 visit_free(v);
1637 if (err) {
1638 error_report_err(err);
1642 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1644 Error *err = NULL;
1645 const char *protocol = qdict_get_str(qdict, "protocol");
1646 const char *hostname = qdict_get_str(qdict, "hostname");
1647 bool has_port = qdict_haskey(qdict, "port");
1648 int port = qdict_get_try_int(qdict, "port", -1);
1649 bool has_tls_port = qdict_haskey(qdict, "tls-port");
1650 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1651 const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1653 qmp_client_migrate_info(protocol, hostname,
1654 has_port, port, has_tls_port, tls_port,
1655 !!cert_subject, cert_subject, &err);
1656 hmp_handle_error(mon, &err);
1659 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1661 Error *err = NULL;
1662 qmp_migrate_start_postcopy(&err);
1663 hmp_handle_error(mon, &err);
1666 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1668 Error *err = NULL;
1670 qmp_x_colo_lost_heartbeat(&err);
1671 hmp_handle_error(mon, &err);
1674 void hmp_set_password(Monitor *mon, const QDict *qdict)
1676 const char *protocol = qdict_get_str(qdict, "protocol");
1677 const char *password = qdict_get_str(qdict, "password");
1678 const char *connected = qdict_get_try_str(qdict, "connected");
1679 Error *err = NULL;
1681 qmp_set_password(protocol, password, !!connected, connected, &err);
1682 hmp_handle_error(mon, &err);
1685 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1687 const char *protocol = qdict_get_str(qdict, "protocol");
1688 const char *whenstr = qdict_get_str(qdict, "time");
1689 Error *err = NULL;
1691 qmp_expire_password(protocol, whenstr, &err);
1692 hmp_handle_error(mon, &err);
1695 void hmp_eject(Monitor *mon, const QDict *qdict)
1697 bool force = qdict_get_try_bool(qdict, "force", false);
1698 const char *device = qdict_get_str(qdict, "device");
1699 Error *err = NULL;
1701 qmp_eject(true, device, false, NULL, true, force, &err);
1702 hmp_handle_error(mon, &err);
1705 static void hmp_change_read_arg(void *opaque, const char *password,
1706 void *readline_opaque)
1708 qmp_change_vnc_password(password, NULL);
1709 monitor_read_command(opaque, 1);
1712 void hmp_change(Monitor *mon, const QDict *qdict)
1714 const char *device = qdict_get_str(qdict, "device");
1715 const char *target = qdict_get_str(qdict, "target");
1716 const char *arg = qdict_get_try_str(qdict, "arg");
1717 const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1718 BlockdevChangeReadOnlyMode read_only_mode = 0;
1719 Error *err = NULL;
1721 if (strcmp(device, "vnc") == 0) {
1722 if (read_only) {
1723 monitor_printf(mon,
1724 "Parameter 'read-only-mode' is invalid for VNC\n");
1725 return;
1727 if (strcmp(target, "passwd") == 0 ||
1728 strcmp(target, "password") == 0) {
1729 if (!arg) {
1730 monitor_read_password(mon, hmp_change_read_arg, NULL);
1731 return;
1734 qmp_change("vnc", target, !!arg, arg, &err);
1735 } else {
1736 if (read_only) {
1737 read_only_mode =
1738 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1739 read_only,
1740 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1741 if (err) {
1742 hmp_handle_error(mon, &err);
1743 return;
1747 qmp_blockdev_change_medium(true, device, false, NULL, target,
1748 !!arg, arg, !!read_only, read_only_mode,
1749 &err);
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_change(Monitor *mon, const QDict *qdict)
2233 const char *args = qdict_get_str(qdict, "args");
2234 const char *id;
2235 Error *err = NULL;
2236 ChardevBackend *backend = NULL;
2237 ChardevReturn *ret = NULL;
2238 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
2239 true);
2240 if (!opts) {
2241 error_setg(&err, "Parsing chardev args failed");
2242 goto end;
2245 id = qdict_get_str(qdict, "id");
2246 if (qemu_opts_id(opts)) {
2247 error_setg(&err, "Unexpected 'id' parameter");
2248 goto end;
2251 backend = qemu_chr_parse_opts(opts, &err);
2252 if (!backend) {
2253 goto end;
2256 ret = qmp_chardev_change(id, backend, &err);
2258 end:
2259 qapi_free_ChardevReturn(ret);
2260 qapi_free_ChardevBackend(backend);
2261 qemu_opts_del(opts);
2262 hmp_handle_error(mon, &err);
2265 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2267 Error *local_err = NULL;
2269 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2270 hmp_handle_error(mon, &local_err);
2273 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2275 Error *local_err = NULL;
2277 qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2278 hmp_handle_error(mon, &local_err);
2281 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2283 BlockBackend *blk;
2284 BlockBackend *local_blk = NULL;
2285 AioContext *aio_context;
2286 const char* device = qdict_get_str(qdict, "device");
2287 const char* command = qdict_get_str(qdict, "command");
2288 Error *err = NULL;
2289 int ret;
2291 blk = blk_by_name(device);
2292 if (!blk) {
2293 BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2294 if (bs) {
2295 blk = local_blk = blk_new(0, BLK_PERM_ALL);
2296 ret = blk_insert_bs(blk, bs, &err);
2297 if (ret < 0) {
2298 goto fail;
2300 } else {
2301 goto fail;
2305 aio_context = blk_get_aio_context(blk);
2306 aio_context_acquire(aio_context);
2309 * Notably absent: Proper permission management. This is sad, but it seems
2310 * almost impossible to achieve without changing the semantics and thereby
2311 * limiting the use cases of the qemu-io HMP command.
2313 * In an ideal world we would unconditionally create a new BlockBackend for
2314 * qemuio_command(), but we have commands like 'reopen' and want them to
2315 * take effect on the exact BlockBackend whose name the user passed instead
2316 * of just on a temporary copy of it.
2318 * Another problem is that deleting the temporary BlockBackend involves
2319 * draining all requests on it first, but some qemu-iotests cases want to
2320 * issue multiple aio_read/write requests and expect them to complete in
2321 * the background while the monitor has already returned.
2323 * This is also what prevents us from saving the original permissions and
2324 * restoring them later: We can't revoke permissions until all requests
2325 * have completed, and we don't know when that is nor can we really let
2326 * anything else run before we have revoken them to avoid race conditions.
2328 * What happens now is that command() in qemu-io-cmds.c can extend the
2329 * permissions if necessary for the qemu-io command. And they simply stay
2330 * extended, possibly resulting in a read-only guest device keeping write
2331 * permissions. Ugly, but it appears to be the lesser evil.
2333 qemuio_command(blk, command);
2335 aio_context_release(aio_context);
2337 fail:
2338 blk_unref(local_blk);
2339 hmp_handle_error(mon, &err);
2342 void hmp_object_del(Monitor *mon, const QDict *qdict)
2344 const char *id = qdict_get_str(qdict, "id");
2345 Error *err = NULL;
2347 user_creatable_del(id, &err);
2348 hmp_handle_error(mon, &err);
2351 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2353 Error *err = NULL;
2354 MemdevList *memdev_list = qmp_query_memdev(&err);
2355 MemdevList *m = memdev_list;
2356 Visitor *v;
2357 char *str;
2359 while (m) {
2360 v = string_output_visitor_new(false, &str);
2361 visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2362 monitor_printf(mon, "memory backend: %s\n", m->value->id);
2363 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
2364 monitor_printf(mon, " merge: %s\n",
2365 m->value->merge ? "true" : "false");
2366 monitor_printf(mon, " dump: %s\n",
2367 m->value->dump ? "true" : "false");
2368 monitor_printf(mon, " prealloc: %s\n",
2369 m->value->prealloc ? "true" : "false");
2370 monitor_printf(mon, " policy: %s\n",
2371 HostMemPolicy_str(m->value->policy));
2372 visit_complete(v, &str);
2373 monitor_printf(mon, " host nodes: %s\n", str);
2375 g_free(str);
2376 visit_free(v);
2377 m = m->next;
2380 monitor_printf(mon, "\n");
2382 qapi_free_MemdevList(memdev_list);
2385 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2387 Error *err = NULL;
2388 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2389 MemoryDeviceInfoList *info;
2390 MemoryDeviceInfo *value;
2391 PCDIMMDeviceInfo *di;
2393 for (info = info_list; info; info = info->next) {
2394 value = info->value;
2396 if (value) {
2397 switch (value->type) {
2398 case MEMORY_DEVICE_INFO_KIND_DIMM:
2399 di = value->u.dimm.data;
2401 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2402 MemoryDeviceInfoKind_str(value->type),
2403 di->id ? di->id : "");
2404 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
2405 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
2406 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
2407 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
2408 monitor_printf(mon, " memdev: %s\n", di->memdev);
2409 monitor_printf(mon, " hotplugged: %s\n",
2410 di->hotplugged ? "true" : "false");
2411 monitor_printf(mon, " hotpluggable: %s\n",
2412 di->hotpluggable ? "true" : "false");
2413 break;
2414 default:
2415 break;
2420 qapi_free_MemoryDeviceInfoList(info_list);
2423 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2425 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2426 IOThreadInfoList *info;
2427 IOThreadInfo *value;
2429 for (info = info_list; info; info = info->next) {
2430 value = info->value;
2431 monitor_printf(mon, "%s:\n", value->id);
2432 monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
2433 monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2434 monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
2435 monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
2438 qapi_free_IOThreadInfoList(info_list);
2441 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2443 const char *path = qdict_get_try_str(qdict, "path");
2444 ObjectPropertyInfoList *list;
2445 Error *err = NULL;
2447 if (path == NULL) {
2448 monitor_printf(mon, "/\n");
2449 return;
2452 list = qmp_qom_list(path, &err);
2453 if (err == NULL) {
2454 ObjectPropertyInfoList *start = list;
2455 while (list != NULL) {
2456 ObjectPropertyInfo *value = list->value;
2458 monitor_printf(mon, "%s (%s)\n",
2459 value->name, value->type);
2460 list = list->next;
2462 qapi_free_ObjectPropertyInfoList(start);
2464 hmp_handle_error(mon, &err);
2467 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2469 const char *path = qdict_get_str(qdict, "path");
2470 const char *property = qdict_get_str(qdict, "property");
2471 const char *value = qdict_get_str(qdict, "value");
2472 Error *err = NULL;
2473 bool ambiguous = false;
2474 Object *obj;
2476 obj = object_resolve_path(path, &ambiguous);
2477 if (obj == NULL) {
2478 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2479 "Device '%s' not found", path);
2480 } else {
2481 if (ambiguous) {
2482 monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2484 object_property_parse(obj, value, property, &err);
2486 hmp_handle_error(mon, &err);
2489 void hmp_rocker(Monitor *mon, const QDict *qdict)
2491 const char *name = qdict_get_str(qdict, "name");
2492 RockerSwitch *rocker;
2493 Error *err = NULL;
2495 rocker = qmp_query_rocker(name, &err);
2496 if (err != NULL) {
2497 hmp_handle_error(mon, &err);
2498 return;
2501 monitor_printf(mon, "name: %s\n", rocker->name);
2502 monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2503 monitor_printf(mon, "ports: %d\n", rocker->ports);
2505 qapi_free_RockerSwitch(rocker);
2508 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2510 RockerPortList *list, *port;
2511 const char *name = qdict_get_str(qdict, "name");
2512 Error *err = NULL;
2514 list = qmp_query_rocker_ports(name, &err);
2515 if (err != NULL) {
2516 hmp_handle_error(mon, &err);
2517 return;
2520 monitor_printf(mon, " ena/ speed/ auto\n");
2521 monitor_printf(mon, " port link duplex neg?\n");
2523 for (port = list; port; port = port->next) {
2524 monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
2525 port->value->name,
2526 port->value->enabled ? port->value->link_up ?
2527 "up" : "down" : "!ena",
2528 port->value->speed == 10000 ? "10G" : "??",
2529 port->value->duplex ? "FD" : "HD",
2530 port->value->autoneg ? "Yes" : "No");
2533 qapi_free_RockerPortList(list);
2536 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2538 RockerOfDpaFlowList *list, *info;
2539 const char *name = qdict_get_str(qdict, "name");
2540 uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2541 Error *err = NULL;
2543 list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2544 if (err != NULL) {
2545 hmp_handle_error(mon, &err);
2546 return;
2549 monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2551 for (info = list; info; info = info->next) {
2552 RockerOfDpaFlow *flow = info->value;
2553 RockerOfDpaFlowKey *key = flow->key;
2554 RockerOfDpaFlowMask *mask = flow->mask;
2555 RockerOfDpaFlowAction *action = flow->action;
2557 if (flow->hits) {
2558 monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2559 key->priority, key->tbl_id, flow->hits);
2560 } else {
2561 monitor_printf(mon, "%-4d %-3d ",
2562 key->priority, key->tbl_id);
2565 if (key->has_in_pport) {
2566 monitor_printf(mon, " pport %d", key->in_pport);
2567 if (mask->has_in_pport) {
2568 monitor_printf(mon, "(0x%x)", mask->in_pport);
2572 if (key->has_vlan_id) {
2573 monitor_printf(mon, " vlan %d",
2574 key->vlan_id & VLAN_VID_MASK);
2575 if (mask->has_vlan_id) {
2576 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2580 if (key->has_tunnel_id) {
2581 monitor_printf(mon, " tunnel %d", key->tunnel_id);
2582 if (mask->has_tunnel_id) {
2583 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2587 if (key->has_eth_type) {
2588 switch (key->eth_type) {
2589 case 0x0806:
2590 monitor_printf(mon, " ARP");
2591 break;
2592 case 0x0800:
2593 monitor_printf(mon, " IP");
2594 break;
2595 case 0x86dd:
2596 monitor_printf(mon, " IPv6");
2597 break;
2598 case 0x8809:
2599 monitor_printf(mon, " LACP");
2600 break;
2601 case 0x88cc:
2602 monitor_printf(mon, " LLDP");
2603 break;
2604 default:
2605 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2606 break;
2610 if (key->has_eth_src) {
2611 if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2612 (mask->has_eth_src) &&
2613 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2614 monitor_printf(mon, " src <any mcast/bcast>");
2615 } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2616 (mask->has_eth_src) &&
2617 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2618 monitor_printf(mon, " src <any ucast>");
2619 } else {
2620 monitor_printf(mon, " src %s", key->eth_src);
2621 if (mask->has_eth_src) {
2622 monitor_printf(mon, "(%s)", mask->eth_src);
2627 if (key->has_eth_dst) {
2628 if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2629 (mask->has_eth_dst) &&
2630 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2631 monitor_printf(mon, " dst <any mcast/bcast>");
2632 } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2633 (mask->has_eth_dst) &&
2634 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2635 monitor_printf(mon, " dst <any ucast>");
2636 } else {
2637 monitor_printf(mon, " dst %s", key->eth_dst);
2638 if (mask->has_eth_dst) {
2639 monitor_printf(mon, "(%s)", mask->eth_dst);
2644 if (key->has_ip_proto) {
2645 monitor_printf(mon, " proto %d", key->ip_proto);
2646 if (mask->has_ip_proto) {
2647 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2651 if (key->has_ip_tos) {
2652 monitor_printf(mon, " TOS %d", key->ip_tos);
2653 if (mask->has_ip_tos) {
2654 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2658 if (key->has_ip_dst) {
2659 monitor_printf(mon, " dst %s", key->ip_dst);
2662 if (action->has_goto_tbl || action->has_group_id ||
2663 action->has_new_vlan_id) {
2664 monitor_printf(mon, " -->");
2667 if (action->has_new_vlan_id) {
2668 monitor_printf(mon, " apply new vlan %d",
2669 ntohs(action->new_vlan_id));
2672 if (action->has_group_id) {
2673 monitor_printf(mon, " write group 0x%08x", action->group_id);
2676 if (action->has_goto_tbl) {
2677 monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2680 monitor_printf(mon, "\n");
2683 qapi_free_RockerOfDpaFlowList(list);
2686 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2688 RockerOfDpaGroupList *list, *g;
2689 const char *name = qdict_get_str(qdict, "name");
2690 uint8_t type = qdict_get_try_int(qdict, "type", 9);
2691 Error *err = NULL;
2692 bool set = false;
2694 list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2695 if (err != NULL) {
2696 hmp_handle_error(mon, &err);
2697 return;
2700 monitor_printf(mon, "id (decode) --> buckets\n");
2702 for (g = list; g; g = g->next) {
2703 RockerOfDpaGroup *group = g->value;
2705 monitor_printf(mon, "0x%08x", group->id);
2707 monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2708 group->type == 1 ? "L2 rewrite" :
2709 group->type == 2 ? "L3 unicast" :
2710 group->type == 3 ? "L2 multicast" :
2711 group->type == 4 ? "L2 flood" :
2712 group->type == 5 ? "L3 interface" :
2713 group->type == 6 ? "L3 multicast" :
2714 group->type == 7 ? "L3 ECMP" :
2715 group->type == 8 ? "L2 overlay" :
2716 "unknown");
2718 if (group->has_vlan_id) {
2719 monitor_printf(mon, " vlan %d", group->vlan_id);
2722 if (group->has_pport) {
2723 monitor_printf(mon, " pport %d", group->pport);
2726 if (group->has_index) {
2727 monitor_printf(mon, " index %d", group->index);
2730 monitor_printf(mon, ") -->");
2732 if (group->has_set_vlan_id && group->set_vlan_id) {
2733 set = true;
2734 monitor_printf(mon, " set vlan %d",
2735 group->set_vlan_id & VLAN_VID_MASK);
2738 if (group->has_set_eth_src) {
2739 if (!set) {
2740 set = true;
2741 monitor_printf(mon, " set");
2743 monitor_printf(mon, " src %s", group->set_eth_src);
2746 if (group->has_set_eth_dst) {
2747 if (!set) {
2748 set = true;
2749 monitor_printf(mon, " set");
2751 monitor_printf(mon, " dst %s", group->set_eth_dst);
2754 set = false;
2756 if (group->has_ttl_check && group->ttl_check) {
2757 monitor_printf(mon, " check TTL");
2760 if (group->has_group_id && group->group_id) {
2761 monitor_printf(mon, " group id 0x%08x", group->group_id);
2764 if (group->has_pop_vlan && group->pop_vlan) {
2765 monitor_printf(mon, " pop vlan");
2768 if (group->has_out_pport) {
2769 monitor_printf(mon, " out pport %d", group->out_pport);
2772 if (group->has_group_ids) {
2773 struct uint32List *id;
2775 monitor_printf(mon, " groups [");
2776 for (id = group->group_ids; id; id = id->next) {
2777 monitor_printf(mon, "0x%08x", id->value);
2778 if (id->next) {
2779 monitor_printf(mon, ",");
2782 monitor_printf(mon, "]");
2785 monitor_printf(mon, "\n");
2788 qapi_free_RockerOfDpaGroupList(list);
2791 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2793 DumpQueryResult *result = qmp_query_dump(NULL);
2795 assert(result && result->status < DUMP_STATUS__MAX);
2796 monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
2798 if (result->status == DUMP_STATUS_ACTIVE) {
2799 float percent = 0;
2800 assert(result->total != 0);
2801 percent = 100.0 * result->completed / result->total;
2802 monitor_printf(mon, "Finished: %.2f %%\n", percent);
2805 qapi_free_DumpQueryResult(result);
2808 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2810 ram_block_dump(mon);
2813 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2815 Error *err = NULL;
2816 HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2817 HotpluggableCPUList *saved = l;
2818 CpuInstanceProperties *c;
2820 if (err != NULL) {
2821 hmp_handle_error(mon, &err);
2822 return;
2825 monitor_printf(mon, "Hotpluggable CPUs:\n");
2826 while (l) {
2827 monitor_printf(mon, " type: \"%s\"\n", l->value->type);
2828 monitor_printf(mon, " vcpus_count: \"%" PRIu64 "\"\n",
2829 l->value->vcpus_count);
2830 if (l->value->has_qom_path) {
2831 monitor_printf(mon, " qom_path: \"%s\"\n", l->value->qom_path);
2834 c = l->value->props;
2835 monitor_printf(mon, " CPUInstance Properties:\n");
2836 if (c->has_node_id) {
2837 monitor_printf(mon, " node-id: \"%" PRIu64 "\"\n", c->node_id);
2839 if (c->has_socket_id) {
2840 monitor_printf(mon, " socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2842 if (c->has_core_id) {
2843 monitor_printf(mon, " core-id: \"%" PRIu64 "\"\n", c->core_id);
2845 if (c->has_thread_id) {
2846 monitor_printf(mon, " thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2849 l = l->next;
2852 qapi_free_HotpluggableCPUList(saved);
2855 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2857 Error *err = NULL;
2858 GuidInfo *info = qmp_query_vm_generation_id(&err);
2859 if (info) {
2860 monitor_printf(mon, "%s\n", info->guid);
2862 hmp_handle_error(mon, &err);
2863 qapi_free_GuidInfo(info);