migration: rename expected_time to expected_downtime
[qemu/ar7.git] / hmp.c
bloba8e5aea7c341ec70d87803e9cebb359569306758
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 "hmp.h"
17 #include "net.h"
18 #include "qemu-option.h"
19 #include "qemu-timer.h"
20 #include "qmp-commands.h"
21 #include "monitor.h"
22 #include "console.h"
24 static void hmp_handle_error(Monitor *mon, Error **errp)
26 if (error_is_set(errp)) {
27 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
28 error_free(*errp);
32 void hmp_info_name(Monitor *mon)
34 NameInfo *info;
36 info = qmp_query_name(NULL);
37 if (info->has_name) {
38 monitor_printf(mon, "%s\n", info->name);
40 qapi_free_NameInfo(info);
43 void hmp_info_version(Monitor *mon)
45 VersionInfo *info;
47 info = qmp_query_version(NULL);
49 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
50 info->qemu.major, info->qemu.minor, info->qemu.micro,
51 info->package);
53 qapi_free_VersionInfo(info);
56 void hmp_info_kvm(Monitor *mon)
58 KvmInfo *info;
60 info = qmp_query_kvm(NULL);
61 monitor_printf(mon, "kvm support: ");
62 if (info->present) {
63 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
64 } else {
65 monitor_printf(mon, "not compiled\n");
68 qapi_free_KvmInfo(info);
71 void hmp_info_status(Monitor *mon)
73 StatusInfo *info;
75 info = qmp_query_status(NULL);
77 monitor_printf(mon, "VM status: %s%s",
78 info->running ? "running" : "paused",
79 info->singlestep ? " (single step mode)" : "");
81 if (!info->running && info->status != RUN_STATE_PAUSED) {
82 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
85 monitor_printf(mon, "\n");
87 qapi_free_StatusInfo(info);
90 void hmp_info_uuid(Monitor *mon)
92 UuidInfo *info;
94 info = qmp_query_uuid(NULL);
95 monitor_printf(mon, "%s\n", info->UUID);
96 qapi_free_UuidInfo(info);
99 void hmp_info_chardev(Monitor *mon)
101 ChardevInfoList *char_info, *info;
103 char_info = qmp_query_chardev(NULL);
104 for (info = char_info; info; info = info->next) {
105 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
106 info->value->filename);
109 qapi_free_ChardevInfoList(char_info);
112 void hmp_info_mice(Monitor *mon)
114 MouseInfoList *mice_list, *mouse;
116 mice_list = qmp_query_mice(NULL);
117 if (!mice_list) {
118 monitor_printf(mon, "No mouse devices connected\n");
119 return;
122 for (mouse = mice_list; mouse; mouse = mouse->next) {
123 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
124 mouse->value->current ? '*' : ' ',
125 mouse->value->index, mouse->value->name,
126 mouse->value->absolute ? " (absolute)" : "");
129 qapi_free_MouseInfoList(mice_list);
132 void hmp_info_migrate(Monitor *mon)
134 MigrationInfo *info;
135 MigrationCapabilityStatusList *caps, *cap;
137 info = qmp_query_migrate(NULL);
138 caps = qmp_query_migrate_capabilities(NULL);
140 /* do not display parameters during setup */
141 if (info->has_status && caps) {
142 monitor_printf(mon, "capabilities: ");
143 for (cap = caps; cap; cap = cap->next) {
144 monitor_printf(mon, "%s: %s ",
145 MigrationCapability_lookup[cap->value->capability],
146 cap->value->state ? "on" : "off");
148 monitor_printf(mon, "\n");
151 if (info->has_status) {
152 monitor_printf(mon, "Migration status: %s\n", info->status);
153 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
154 info->total_time);
155 if (info->has_downtime) {
156 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
157 info->downtime);
161 if (info->has_ram) {
162 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
163 info->ram->transferred >> 10);
164 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
165 info->ram->remaining >> 10);
166 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
167 info->ram->total >> 10);
168 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
169 info->ram->duplicate);
170 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
171 info->ram->normal);
172 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
173 info->ram->normal_bytes >> 10);
176 if (info->has_disk) {
177 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
178 info->disk->transferred >> 10);
179 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
180 info->disk->remaining >> 10);
181 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
182 info->disk->total >> 10);
185 if (info->has_xbzrle_cache) {
186 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
187 info->xbzrle_cache->cache_size);
188 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
189 info->xbzrle_cache->bytes >> 10);
190 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
191 info->xbzrle_cache->pages);
192 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
193 info->xbzrle_cache->cache_miss);
194 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
195 info->xbzrle_cache->overflow);
198 qapi_free_MigrationInfo(info);
199 qapi_free_MigrationCapabilityStatusList(caps);
202 void hmp_info_migrate_capabilities(Monitor *mon)
204 MigrationCapabilityStatusList *caps, *cap;
206 caps = qmp_query_migrate_capabilities(NULL);
208 if (caps) {
209 monitor_printf(mon, "capabilities: ");
210 for (cap = caps; cap; cap = cap->next) {
211 monitor_printf(mon, "%s: %s ",
212 MigrationCapability_lookup[cap->value->capability],
213 cap->value->state ? "on" : "off");
215 monitor_printf(mon, "\n");
218 qapi_free_MigrationCapabilityStatusList(caps);
221 void hmp_info_migrate_cache_size(Monitor *mon)
223 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
224 qmp_query_migrate_cache_size(NULL) >> 10);
227 void hmp_info_cpus(Monitor *mon)
229 CpuInfoList *cpu_list, *cpu;
231 cpu_list = qmp_query_cpus(NULL);
233 for (cpu = cpu_list; cpu; cpu = cpu->next) {
234 int active = ' ';
236 if (cpu->value->CPU == monitor_get_cpu_index()) {
237 active = '*';
240 monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
242 if (cpu->value->has_pc) {
243 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
245 if (cpu->value->has_nip) {
246 monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
248 if (cpu->value->has_npc) {
249 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
250 monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
252 if (cpu->value->has_PC) {
253 monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
256 if (cpu->value->halted) {
257 monitor_printf(mon, " (halted)");
260 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
263 qapi_free_CpuInfoList(cpu_list);
266 void hmp_info_block(Monitor *mon)
268 BlockInfoList *block_list, *info;
270 block_list = qmp_query_block(NULL);
272 for (info = block_list; info; info = info->next) {
273 monitor_printf(mon, "%s: removable=%d",
274 info->value->device, info->value->removable);
276 if (info->value->removable) {
277 monitor_printf(mon, " locked=%d", info->value->locked);
278 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
281 if (info->value->has_io_status) {
282 monitor_printf(mon, " io-status=%s",
283 BlockDeviceIoStatus_lookup[info->value->io_status]);
286 if (info->value->has_inserted) {
287 monitor_printf(mon, " file=");
288 monitor_print_filename(mon, info->value->inserted->file);
290 if (info->value->inserted->has_backing_file) {
291 monitor_printf(mon, " backing_file=");
292 monitor_print_filename(mon, info->value->inserted->backing_file);
293 monitor_printf(mon, " backing_file_depth=%" PRId64,
294 info->value->inserted->backing_file_depth);
296 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
297 info->value->inserted->ro,
298 info->value->inserted->drv,
299 info->value->inserted->encrypted);
301 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
302 " bps_wr=%" PRId64 " iops=%" PRId64
303 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
304 info->value->inserted->bps,
305 info->value->inserted->bps_rd,
306 info->value->inserted->bps_wr,
307 info->value->inserted->iops,
308 info->value->inserted->iops_rd,
309 info->value->inserted->iops_wr);
310 } else {
311 monitor_printf(mon, " [not inserted]");
314 monitor_printf(mon, "\n");
317 qapi_free_BlockInfoList(block_list);
320 void hmp_info_blockstats(Monitor *mon)
322 BlockStatsList *stats_list, *stats;
324 stats_list = qmp_query_blockstats(NULL);
326 for (stats = stats_list; stats; stats = stats->next) {
327 if (!stats->value->has_device) {
328 continue;
331 monitor_printf(mon, "%s:", stats->value->device);
332 monitor_printf(mon, " rd_bytes=%" PRId64
333 " wr_bytes=%" PRId64
334 " rd_operations=%" PRId64
335 " wr_operations=%" PRId64
336 " flush_operations=%" PRId64
337 " wr_total_time_ns=%" PRId64
338 " rd_total_time_ns=%" PRId64
339 " flush_total_time_ns=%" PRId64
340 "\n",
341 stats->value->stats->rd_bytes,
342 stats->value->stats->wr_bytes,
343 stats->value->stats->rd_operations,
344 stats->value->stats->wr_operations,
345 stats->value->stats->flush_operations,
346 stats->value->stats->wr_total_time_ns,
347 stats->value->stats->rd_total_time_ns,
348 stats->value->stats->flush_total_time_ns);
351 qapi_free_BlockStatsList(stats_list);
354 void hmp_info_vnc(Monitor *mon)
356 VncInfo *info;
357 Error *err = NULL;
358 VncClientInfoList *client;
360 info = qmp_query_vnc(&err);
361 if (err) {
362 monitor_printf(mon, "%s\n", error_get_pretty(err));
363 error_free(err);
364 return;
367 if (!info->enabled) {
368 monitor_printf(mon, "Server: disabled\n");
369 goto out;
372 monitor_printf(mon, "Server:\n");
373 if (info->has_host && info->has_service) {
374 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
376 if (info->has_auth) {
377 monitor_printf(mon, " auth: %s\n", info->auth);
380 if (!info->has_clients || info->clients == NULL) {
381 monitor_printf(mon, "Client: none\n");
382 } else {
383 for (client = info->clients; client; client = client->next) {
384 monitor_printf(mon, "Client:\n");
385 monitor_printf(mon, " address: %s:%s\n",
386 client->value->host, client->value->service);
387 monitor_printf(mon, " x509_dname: %s\n",
388 client->value->x509_dname ?
389 client->value->x509_dname : "none");
390 monitor_printf(mon, " username: %s\n",
391 client->value->has_sasl_username ?
392 client->value->sasl_username : "none");
396 out:
397 qapi_free_VncInfo(info);
400 void hmp_info_spice(Monitor *mon)
402 SpiceChannelList *chan;
403 SpiceInfo *info;
405 info = qmp_query_spice(NULL);
407 if (!info->enabled) {
408 monitor_printf(mon, "Server: disabled\n");
409 goto out;
412 monitor_printf(mon, "Server:\n");
413 if (info->has_port) {
414 monitor_printf(mon, " address: %s:%" PRId64 "\n",
415 info->host, info->port);
417 if (info->has_tls_port) {
418 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
419 info->host, info->tls_port);
421 monitor_printf(mon, " migrated: %s\n",
422 info->migrated ? "true" : "false");
423 monitor_printf(mon, " auth: %s\n", info->auth);
424 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
425 monitor_printf(mon, " mouse-mode: %s\n",
426 SpiceQueryMouseMode_lookup[info->mouse_mode]);
428 if (!info->has_channels || info->channels == NULL) {
429 monitor_printf(mon, "Channels: none\n");
430 } else {
431 for (chan = info->channels; chan; chan = chan->next) {
432 monitor_printf(mon, "Channel:\n");
433 monitor_printf(mon, " address: %s:%s%s\n",
434 chan->value->host, chan->value->port,
435 chan->value->tls ? " [tls]" : "");
436 monitor_printf(mon, " session: %" PRId64 "\n",
437 chan->value->connection_id);
438 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
439 chan->value->channel_type, chan->value->channel_id);
443 out:
444 qapi_free_SpiceInfo(info);
447 void hmp_info_balloon(Monitor *mon)
449 BalloonInfo *info;
450 Error *err = NULL;
452 info = qmp_query_balloon(&err);
453 if (err) {
454 monitor_printf(mon, "%s\n", error_get_pretty(err));
455 error_free(err);
456 return;
459 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
460 if (info->has_mem_swapped_in) {
461 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
463 if (info->has_mem_swapped_out) {
464 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
466 if (info->has_major_page_faults) {
467 monitor_printf(mon, " major_page_faults=%" PRId64,
468 info->major_page_faults);
470 if (info->has_minor_page_faults) {
471 monitor_printf(mon, " minor_page_faults=%" PRId64,
472 info->minor_page_faults);
474 if (info->has_free_mem) {
475 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
477 if (info->has_total_mem) {
478 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
481 monitor_printf(mon, "\n");
483 qapi_free_BalloonInfo(info);
486 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
488 PciMemoryRegionList *region;
490 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
491 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
492 dev->slot, dev->function);
493 monitor_printf(mon, " ");
495 if (dev->class_info.has_desc) {
496 monitor_printf(mon, "%s", dev->class_info.desc);
497 } else {
498 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
501 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
502 dev->id.vendor, dev->id.device);
504 if (dev->has_irq) {
505 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
508 if (dev->has_pci_bridge) {
509 monitor_printf(mon, " BUS %" PRId64 ".\n",
510 dev->pci_bridge->bus.number);
511 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
512 dev->pci_bridge->bus.secondary);
513 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
514 dev->pci_bridge->bus.subordinate);
516 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
517 dev->pci_bridge->bus.io_range->base,
518 dev->pci_bridge->bus.io_range->limit);
520 monitor_printf(mon,
521 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
522 dev->pci_bridge->bus.memory_range->base,
523 dev->pci_bridge->bus.memory_range->limit);
525 monitor_printf(mon, " prefetchable memory range "
526 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
527 dev->pci_bridge->bus.prefetchable_range->base,
528 dev->pci_bridge->bus.prefetchable_range->limit);
531 for (region = dev->regions; region; region = region->next) {
532 uint64_t addr, size;
534 addr = region->value->address;
535 size = region->value->size;
537 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
539 if (!strcmp(region->value->type, "io")) {
540 monitor_printf(mon, "I/O at 0x%04" PRIx64
541 " [0x%04" PRIx64 "].\n",
542 addr, addr + size - 1);
543 } else {
544 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
545 " [0x%08" PRIx64 "].\n",
546 region->value->mem_type_64 ? 64 : 32,
547 region->value->prefetch ? " prefetchable" : "",
548 addr, addr + size - 1);
552 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
554 if (dev->has_pci_bridge) {
555 if (dev->pci_bridge->has_devices) {
556 PciDeviceInfoList *cdev;
557 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
558 hmp_info_pci_device(mon, cdev->value);
564 void hmp_info_pci(Monitor *mon)
566 PciInfoList *info_list, *info;
567 Error *err = NULL;
569 info_list = qmp_query_pci(&err);
570 if (err) {
571 monitor_printf(mon, "PCI devices not supported\n");
572 error_free(err);
573 return;
576 for (info = info_list; info; info = info->next) {
577 PciDeviceInfoList *dev;
579 for (dev = info->value->devices; dev; dev = dev->next) {
580 hmp_info_pci_device(mon, dev->value);
584 qapi_free_PciInfoList(info_list);
587 void hmp_info_block_jobs(Monitor *mon)
589 BlockJobInfoList *list;
590 Error *err = NULL;
592 list = qmp_query_block_jobs(&err);
593 assert(!err);
595 if (!list) {
596 monitor_printf(mon, "No active jobs\n");
597 return;
600 while (list) {
601 if (strcmp(list->value->type, "stream") == 0) {
602 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
603 " of %" PRId64 " bytes, speed limit %" PRId64
604 " bytes/s\n",
605 list->value->device,
606 list->value->offset,
607 list->value->len,
608 list->value->speed);
609 } else {
610 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
611 " of %" PRId64 " bytes, speed limit %" PRId64
612 " bytes/s\n",
613 list->value->type,
614 list->value->device,
615 list->value->offset,
616 list->value->len,
617 list->value->speed);
619 list = list->next;
623 void hmp_quit(Monitor *mon, const QDict *qdict)
625 monitor_suspend(mon);
626 qmp_quit(NULL);
629 void hmp_stop(Monitor *mon, const QDict *qdict)
631 qmp_stop(NULL);
634 void hmp_system_reset(Monitor *mon, const QDict *qdict)
636 qmp_system_reset(NULL);
639 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
641 qmp_system_powerdown(NULL);
644 void hmp_cpu(Monitor *mon, const QDict *qdict)
646 int64_t cpu_index;
648 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
649 use it are converted to the QAPI */
650 cpu_index = qdict_get_int(qdict, "index");
651 if (monitor_set_cpu(cpu_index) < 0) {
652 monitor_printf(mon, "invalid CPU index\n");
656 void hmp_memsave(Monitor *mon, const QDict *qdict)
658 uint32_t size = qdict_get_int(qdict, "size");
659 const char *filename = qdict_get_str(qdict, "filename");
660 uint64_t addr = qdict_get_int(qdict, "val");
661 Error *errp = NULL;
663 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
664 hmp_handle_error(mon, &errp);
667 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
669 uint32_t size = qdict_get_int(qdict, "size");
670 const char *filename = qdict_get_str(qdict, "filename");
671 uint64_t addr = qdict_get_int(qdict, "val");
672 Error *errp = NULL;
674 qmp_pmemsave(addr, size, filename, &errp);
675 hmp_handle_error(mon, &errp);
678 static void hmp_cont_cb(void *opaque, int err)
680 if (!err) {
681 qmp_cont(NULL);
685 static bool key_is_missing(const BlockInfo *bdev)
687 return (bdev->inserted && bdev->inserted->encryption_key_missing);
690 void hmp_cont(Monitor *mon, const QDict *qdict)
692 BlockInfoList *bdev_list, *bdev;
693 Error *errp = NULL;
695 bdev_list = qmp_query_block(NULL);
696 for (bdev = bdev_list; bdev; bdev = bdev->next) {
697 if (key_is_missing(bdev->value)) {
698 monitor_read_block_device_key(mon, bdev->value->device,
699 hmp_cont_cb, NULL);
700 goto out;
704 qmp_cont(&errp);
705 hmp_handle_error(mon, &errp);
707 out:
708 qapi_free_BlockInfoList(bdev_list);
711 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
713 qmp_system_wakeup(NULL);
716 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
718 Error *errp = NULL;
720 qmp_inject_nmi(&errp);
721 hmp_handle_error(mon, &errp);
724 void hmp_set_link(Monitor *mon, const QDict *qdict)
726 const char *name = qdict_get_str(qdict, "name");
727 int up = qdict_get_bool(qdict, "up");
728 Error *errp = NULL;
730 qmp_set_link(name, up, &errp);
731 hmp_handle_error(mon, &errp);
734 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
736 const char *device = qdict_get_str(qdict, "device");
737 const char *password = qdict_get_str(qdict, "password");
738 Error *errp = NULL;
740 qmp_block_passwd(device, password, &errp);
741 hmp_handle_error(mon, &errp);
744 void hmp_balloon(Monitor *mon, const QDict *qdict)
746 int64_t value = qdict_get_int(qdict, "value");
747 Error *errp = NULL;
749 qmp_balloon(value, &errp);
750 if (error_is_set(&errp)) {
751 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
752 error_free(errp);
756 void hmp_block_resize(Monitor *mon, const QDict *qdict)
758 const char *device = qdict_get_str(qdict, "device");
759 int64_t size = qdict_get_int(qdict, "size");
760 Error *errp = NULL;
762 qmp_block_resize(device, size, &errp);
763 hmp_handle_error(mon, &errp);
766 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
768 const char *device = qdict_get_str(qdict, "device");
769 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
770 const char *format = qdict_get_try_str(qdict, "format");
771 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
772 enum NewImageMode mode;
773 Error *errp = NULL;
775 if (!filename) {
776 /* In the future, if 'snapshot-file' is not specified, the snapshot
777 will be taken internally. Today it's actually required. */
778 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
779 hmp_handle_error(mon, &errp);
780 return;
783 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
784 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
785 true, mode, &errp);
786 hmp_handle_error(mon, &errp);
789 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
791 qmp_migrate_cancel(NULL);
794 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
796 double value = qdict_get_double(qdict, "value");
797 qmp_migrate_set_downtime(value, NULL);
800 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
802 int64_t value = qdict_get_int(qdict, "value");
803 Error *err = NULL;
805 qmp_migrate_set_cache_size(value, &err);
806 if (err) {
807 monitor_printf(mon, "%s\n", error_get_pretty(err));
808 error_free(err);
809 return;
813 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
815 int64_t value = qdict_get_int(qdict, "value");
816 qmp_migrate_set_speed(value, NULL);
819 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
821 const char *cap = qdict_get_str(qdict, "capability");
822 bool state = qdict_get_bool(qdict, "state");
823 Error *err = NULL;
824 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
825 int i;
827 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
828 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
829 caps->value = g_malloc0(sizeof(*caps->value));
830 caps->value->capability = i;
831 caps->value->state = state;
832 caps->next = NULL;
833 qmp_migrate_set_capabilities(caps, &err);
834 break;
838 if (i == MIGRATION_CAPABILITY_MAX) {
839 error_set(&err, QERR_INVALID_PARAMETER, cap);
842 qapi_free_MigrationCapabilityStatusList(caps);
844 if (err) {
845 monitor_printf(mon, "migrate_set_parameter: %s\n",
846 error_get_pretty(err));
847 error_free(err);
851 void hmp_set_password(Monitor *mon, const QDict *qdict)
853 const char *protocol = qdict_get_str(qdict, "protocol");
854 const char *password = qdict_get_str(qdict, "password");
855 const char *connected = qdict_get_try_str(qdict, "connected");
856 Error *err = NULL;
858 qmp_set_password(protocol, password, !!connected, connected, &err);
859 hmp_handle_error(mon, &err);
862 void hmp_expire_password(Monitor *mon, const QDict *qdict)
864 const char *protocol = qdict_get_str(qdict, "protocol");
865 const char *whenstr = qdict_get_str(qdict, "time");
866 Error *err = NULL;
868 qmp_expire_password(protocol, whenstr, &err);
869 hmp_handle_error(mon, &err);
872 void hmp_eject(Monitor *mon, const QDict *qdict)
874 int force = qdict_get_try_bool(qdict, "force", 0);
875 const char *device = qdict_get_str(qdict, "device");
876 Error *err = NULL;
878 qmp_eject(device, true, force, &err);
879 hmp_handle_error(mon, &err);
882 static void hmp_change_read_arg(Monitor *mon, const char *password,
883 void *opaque)
885 qmp_change_vnc_password(password, NULL);
886 monitor_read_command(mon, 1);
889 void hmp_change(Monitor *mon, const QDict *qdict)
891 const char *device = qdict_get_str(qdict, "device");
892 const char *target = qdict_get_str(qdict, "target");
893 const char *arg = qdict_get_try_str(qdict, "arg");
894 Error *err = NULL;
896 if (strcmp(device, "vnc") == 0 &&
897 (strcmp(target, "passwd") == 0 ||
898 strcmp(target, "password") == 0)) {
899 if (!arg) {
900 monitor_read_password(mon, hmp_change_read_arg, NULL);
901 return;
905 qmp_change(device, target, !!arg, arg, &err);
906 if (error_is_set(&err) &&
907 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
908 error_free(err);
909 monitor_read_block_device_key(mon, device, NULL, NULL);
910 return;
912 hmp_handle_error(mon, &err);
915 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
917 Error *err = NULL;
919 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
920 qdict_get_int(qdict, "bps"),
921 qdict_get_int(qdict, "bps_rd"),
922 qdict_get_int(qdict, "bps_wr"),
923 qdict_get_int(qdict, "iops"),
924 qdict_get_int(qdict, "iops_rd"),
925 qdict_get_int(qdict, "iops_wr"), &err);
926 hmp_handle_error(mon, &err);
929 void hmp_block_stream(Monitor *mon, const QDict *qdict)
931 Error *error = NULL;
932 const char *device = qdict_get_str(qdict, "device");
933 const char *base = qdict_get_try_str(qdict, "base");
934 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
936 qmp_block_stream(device, base != NULL, base,
937 qdict_haskey(qdict, "speed"), speed,
938 BLOCKDEV_ON_ERROR_REPORT, true, &error);
940 hmp_handle_error(mon, &error);
943 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
945 Error *error = NULL;
946 const char *device = qdict_get_str(qdict, "device");
947 int64_t value = qdict_get_int(qdict, "speed");
949 qmp_block_job_set_speed(device, value, &error);
951 hmp_handle_error(mon, &error);
954 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
956 Error *error = NULL;
957 const char *device = qdict_get_str(qdict, "device");
958 bool force = qdict_get_try_bool(qdict, "force", 0);
960 qmp_block_job_cancel(device, true, force, &error);
962 hmp_handle_error(mon, &error);
965 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
967 Error *error = NULL;
968 const char *device = qdict_get_str(qdict, "device");
970 qmp_block_job_pause(device, &error);
972 hmp_handle_error(mon, &error);
975 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
977 Error *error = NULL;
978 const char *device = qdict_get_str(qdict, "device");
980 qmp_block_job_resume(device, &error);
982 hmp_handle_error(mon, &error);
985 typedef struct MigrationStatus
987 QEMUTimer *timer;
988 Monitor *mon;
989 bool is_block_migration;
990 } MigrationStatus;
992 static void hmp_migrate_status_cb(void *opaque)
994 MigrationStatus *status = opaque;
995 MigrationInfo *info;
997 info = qmp_query_migrate(NULL);
998 if (!info->has_status || strcmp(info->status, "active") == 0) {
999 if (info->has_disk) {
1000 int progress;
1002 if (info->disk->remaining) {
1003 progress = info->disk->transferred * 100 / info->disk->total;
1004 } else {
1005 progress = 100;
1008 monitor_printf(status->mon, "Completed %d %%\r", progress);
1009 monitor_flush(status->mon);
1012 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1013 } else {
1014 if (status->is_block_migration) {
1015 monitor_printf(status->mon, "\n");
1017 monitor_resume(status->mon);
1018 qemu_del_timer(status->timer);
1019 g_free(status);
1022 qapi_free_MigrationInfo(info);
1025 void hmp_migrate(Monitor *mon, const QDict *qdict)
1027 int detach = qdict_get_try_bool(qdict, "detach", 0);
1028 int blk = qdict_get_try_bool(qdict, "blk", 0);
1029 int inc = qdict_get_try_bool(qdict, "inc", 0);
1030 const char *uri = qdict_get_str(qdict, "uri");
1031 Error *err = NULL;
1033 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1034 if (err) {
1035 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1036 error_free(err);
1037 return;
1040 if (!detach) {
1041 MigrationStatus *status;
1043 if (monitor_suspend(mon) < 0) {
1044 monitor_printf(mon, "terminal does not allow synchronous "
1045 "migration, continuing detached\n");
1046 return;
1049 status = g_malloc0(sizeof(*status));
1050 status->mon = mon;
1051 status->is_block_migration = blk || inc;
1052 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1053 status);
1054 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1058 void hmp_device_del(Monitor *mon, const QDict *qdict)
1060 const char *id = qdict_get_str(qdict, "id");
1061 Error *err = NULL;
1063 qmp_device_del(id, &err);
1064 hmp_handle_error(mon, &err);
1067 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1069 Error *errp = NULL;
1070 int paging = qdict_get_try_bool(qdict, "paging", 0);
1071 const char *file = qdict_get_str(qdict, "filename");
1072 bool has_begin = qdict_haskey(qdict, "begin");
1073 bool has_length = qdict_haskey(qdict, "length");
1074 int64_t begin = 0;
1075 int64_t length = 0;
1076 char *prot;
1078 if (has_begin) {
1079 begin = qdict_get_int(qdict, "begin");
1081 if (has_length) {
1082 length = qdict_get_int(qdict, "length");
1085 prot = g_strconcat("file:", file, NULL);
1087 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1088 &errp);
1089 hmp_handle_error(mon, &errp);
1090 g_free(prot);
1093 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1095 Error *err = NULL;
1096 QemuOpts *opts;
1098 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1099 if (error_is_set(&err)) {
1100 goto out;
1103 netdev_add(opts, &err);
1104 if (error_is_set(&err)) {
1105 qemu_opts_del(opts);
1108 out:
1109 hmp_handle_error(mon, &err);
1112 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1114 const char *id = qdict_get_str(qdict, "id");
1115 Error *err = NULL;
1117 qmp_netdev_del(id, &err);
1118 hmp_handle_error(mon, &err);
1121 void hmp_getfd(Monitor *mon, const QDict *qdict)
1123 const char *fdname = qdict_get_str(qdict, "fdname");
1124 Error *errp = NULL;
1126 qmp_getfd(fdname, &errp);
1127 hmp_handle_error(mon, &errp);
1130 void hmp_closefd(Monitor *mon, const QDict *qdict)
1132 const char *fdname = qdict_get_str(qdict, "fdname");
1133 Error *errp = NULL;
1135 qmp_closefd(fdname, &errp);
1136 hmp_handle_error(mon, &errp);
1139 void hmp_send_key(Monitor *mon, const QDict *qdict)
1141 const char *keys = qdict_get_str(qdict, "keys");
1142 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1143 int has_hold_time = qdict_haskey(qdict, "hold-time");
1144 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1145 Error *err = NULL;
1146 char keyname_buf[16];
1147 char *separator;
1148 int keyname_len;
1150 while (1) {
1151 separator = strchr(keys, '-');
1152 keyname_len = separator ? separator - keys : strlen(keys);
1153 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1155 /* Be compatible with old interface, convert user inputted "<" */
1156 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1157 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1158 keyname_len = 4;
1160 keyname_buf[keyname_len] = 0;
1162 keylist = g_malloc0(sizeof(*keylist));
1163 keylist->value = g_malloc0(sizeof(*keylist->value));
1165 if (!head) {
1166 head = keylist;
1168 if (tmp) {
1169 tmp->next = keylist;
1171 tmp = keylist;
1173 if (strstart(keyname_buf, "0x", NULL)) {
1174 char *endp;
1175 int value = strtoul(keyname_buf, &endp, 0);
1176 if (*endp != '\0') {
1177 goto err_out;
1179 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1180 keylist->value->number = value;
1181 } else {
1182 int idx = index_from_key(keyname_buf);
1183 if (idx == Q_KEY_CODE_MAX) {
1184 goto err_out;
1186 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1187 keylist->value->qcode = idx;
1190 if (!separator) {
1191 break;
1193 keys = separator + 1;
1196 qmp_send_key(head, has_hold_time, hold_time, &err);
1197 hmp_handle_error(mon, &err);
1199 out:
1200 qapi_free_KeyValueList(head);
1201 return;
1203 err_out:
1204 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1205 goto out;
1208 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1210 const char *filename = qdict_get_str(qdict, "filename");
1211 Error *err = NULL;
1213 qmp_screendump(filename, &err);
1214 hmp_handle_error(mon, &err);