scsi: remove superfluous call to scsi_device_set_ua
[qemu/opensuse.git] / hmp.c
blob895a343dc3714f9dfa3cd8b5a1306039712cd4ae
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_expected_downtime) {
156 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
157 info->expected_downtime);
159 if (info->has_downtime) {
160 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
161 info->downtime);
165 if (info->has_ram) {
166 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
167 info->ram->transferred >> 10);
168 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
169 info->ram->remaining >> 10);
170 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
171 info->ram->total >> 10);
172 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
173 info->ram->duplicate);
174 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
175 info->ram->normal);
176 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
177 info->ram->normal_bytes >> 10);
178 if (info->ram->dirty_pages_rate) {
179 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
180 info->ram->dirty_pages_rate);
184 if (info->has_disk) {
185 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
186 info->disk->transferred >> 10);
187 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
188 info->disk->remaining >> 10);
189 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
190 info->disk->total >> 10);
193 if (info->has_xbzrle_cache) {
194 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
195 info->xbzrle_cache->cache_size);
196 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
197 info->xbzrle_cache->bytes >> 10);
198 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
199 info->xbzrle_cache->pages);
200 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
201 info->xbzrle_cache->cache_miss);
202 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
203 info->xbzrle_cache->overflow);
206 qapi_free_MigrationInfo(info);
207 qapi_free_MigrationCapabilityStatusList(caps);
210 void hmp_info_migrate_capabilities(Monitor *mon)
212 MigrationCapabilityStatusList *caps, *cap;
214 caps = qmp_query_migrate_capabilities(NULL);
216 if (caps) {
217 monitor_printf(mon, "capabilities: ");
218 for (cap = caps; cap; cap = cap->next) {
219 monitor_printf(mon, "%s: %s ",
220 MigrationCapability_lookup[cap->value->capability],
221 cap->value->state ? "on" : "off");
223 monitor_printf(mon, "\n");
226 qapi_free_MigrationCapabilityStatusList(caps);
229 void hmp_info_migrate_cache_size(Monitor *mon)
231 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
232 qmp_query_migrate_cache_size(NULL) >> 10);
235 void hmp_info_cpus(Monitor *mon)
237 CpuInfoList *cpu_list, *cpu;
239 cpu_list = qmp_query_cpus(NULL);
241 for (cpu = cpu_list; cpu; cpu = cpu->next) {
242 int active = ' ';
244 if (cpu->value->CPU == monitor_get_cpu_index()) {
245 active = '*';
248 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
250 if (cpu->value->has_pc) {
251 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
253 if (cpu->value->has_nip) {
254 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
256 if (cpu->value->has_npc) {
257 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
259 if (cpu->value->has_PC) {
260 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
263 if (cpu->value->halted) {
264 monitor_printf(mon, " (halted)");
267 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
270 qapi_free_CpuInfoList(cpu_list);
273 void hmp_info_block(Monitor *mon)
275 BlockInfoList *block_list, *info;
277 block_list = qmp_query_block(NULL);
279 for (info = block_list; info; info = info->next) {
280 monitor_printf(mon, "%s: removable=%d",
281 info->value->device, info->value->removable);
283 if (info->value->removable) {
284 monitor_printf(mon, " locked=%d", info->value->locked);
285 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
288 if (info->value->has_io_status) {
289 monitor_printf(mon, " io-status=%s",
290 BlockDeviceIoStatus_lookup[info->value->io_status]);
293 if (info->value->has_inserted) {
294 monitor_printf(mon, " file=");
295 monitor_print_filename(mon, info->value->inserted->file);
297 if (info->value->inserted->has_backing_file) {
298 monitor_printf(mon, " backing_file=");
299 monitor_print_filename(mon, info->value->inserted->backing_file);
300 monitor_printf(mon, " backing_file_depth=%" PRId64,
301 info->value->inserted->backing_file_depth);
303 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
304 info->value->inserted->ro,
305 info->value->inserted->drv,
306 info->value->inserted->encrypted);
308 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
309 " bps_wr=%" PRId64 " iops=%" PRId64
310 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
311 info->value->inserted->bps,
312 info->value->inserted->bps_rd,
313 info->value->inserted->bps_wr,
314 info->value->inserted->iops,
315 info->value->inserted->iops_rd,
316 info->value->inserted->iops_wr);
317 } else {
318 monitor_printf(mon, " [not inserted]");
321 monitor_printf(mon, "\n");
324 qapi_free_BlockInfoList(block_list);
327 void hmp_info_blockstats(Monitor *mon)
329 BlockStatsList *stats_list, *stats;
331 stats_list = qmp_query_blockstats(NULL);
333 for (stats = stats_list; stats; stats = stats->next) {
334 if (!stats->value->has_device) {
335 continue;
338 monitor_printf(mon, "%s:", stats->value->device);
339 monitor_printf(mon, " rd_bytes=%" PRId64
340 " wr_bytes=%" PRId64
341 " rd_operations=%" PRId64
342 " wr_operations=%" PRId64
343 " flush_operations=%" PRId64
344 " wr_total_time_ns=%" PRId64
345 " rd_total_time_ns=%" PRId64
346 " flush_total_time_ns=%" PRId64
347 "\n",
348 stats->value->stats->rd_bytes,
349 stats->value->stats->wr_bytes,
350 stats->value->stats->rd_operations,
351 stats->value->stats->wr_operations,
352 stats->value->stats->flush_operations,
353 stats->value->stats->wr_total_time_ns,
354 stats->value->stats->rd_total_time_ns,
355 stats->value->stats->flush_total_time_ns);
358 qapi_free_BlockStatsList(stats_list);
361 void hmp_info_vnc(Monitor *mon)
363 VncInfo *info;
364 Error *err = NULL;
365 VncClientInfoList *client;
367 info = qmp_query_vnc(&err);
368 if (err) {
369 monitor_printf(mon, "%s\n", error_get_pretty(err));
370 error_free(err);
371 return;
374 if (!info->enabled) {
375 monitor_printf(mon, "Server: disabled\n");
376 goto out;
379 monitor_printf(mon, "Server:\n");
380 if (info->has_host && info->has_service) {
381 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
383 if (info->has_auth) {
384 monitor_printf(mon, " auth: %s\n", info->auth);
387 if (!info->has_clients || info->clients == NULL) {
388 monitor_printf(mon, "Client: none\n");
389 } else {
390 for (client = info->clients; client; client = client->next) {
391 monitor_printf(mon, "Client:\n");
392 monitor_printf(mon, " address: %s:%s\n",
393 client->value->host, client->value->service);
394 monitor_printf(mon, " x509_dname: %s\n",
395 client->value->x509_dname ?
396 client->value->x509_dname : "none");
397 monitor_printf(mon, " username: %s\n",
398 client->value->has_sasl_username ?
399 client->value->sasl_username : "none");
403 out:
404 qapi_free_VncInfo(info);
407 void hmp_info_spice(Monitor *mon)
409 SpiceChannelList *chan;
410 SpiceInfo *info;
412 info = qmp_query_spice(NULL);
414 if (!info->enabled) {
415 monitor_printf(mon, "Server: disabled\n");
416 goto out;
419 monitor_printf(mon, "Server:\n");
420 if (info->has_port) {
421 monitor_printf(mon, " address: %s:%" PRId64 "\n",
422 info->host, info->port);
424 if (info->has_tls_port) {
425 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
426 info->host, info->tls_port);
428 monitor_printf(mon, " migrated: %s\n",
429 info->migrated ? "true" : "false");
430 monitor_printf(mon, " auth: %s\n", info->auth);
431 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
432 monitor_printf(mon, " mouse-mode: %s\n",
433 SpiceQueryMouseMode_lookup[info->mouse_mode]);
435 if (!info->has_channels || info->channels == NULL) {
436 monitor_printf(mon, "Channels: none\n");
437 } else {
438 for (chan = info->channels; chan; chan = chan->next) {
439 monitor_printf(mon, "Channel:\n");
440 monitor_printf(mon, " address: %s:%s%s\n",
441 chan->value->host, chan->value->port,
442 chan->value->tls ? " [tls]" : "");
443 monitor_printf(mon, " session: %" PRId64 "\n",
444 chan->value->connection_id);
445 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
446 chan->value->channel_type, chan->value->channel_id);
450 out:
451 qapi_free_SpiceInfo(info);
454 void hmp_info_balloon(Monitor *mon)
456 BalloonInfo *info;
457 Error *err = NULL;
459 info = qmp_query_balloon(&err);
460 if (err) {
461 monitor_printf(mon, "%s\n", error_get_pretty(err));
462 error_free(err);
463 return;
466 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
467 if (info->has_mem_swapped_in) {
468 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
470 if (info->has_mem_swapped_out) {
471 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
473 if (info->has_major_page_faults) {
474 monitor_printf(mon, " major_page_faults=%" PRId64,
475 info->major_page_faults);
477 if (info->has_minor_page_faults) {
478 monitor_printf(mon, " minor_page_faults=%" PRId64,
479 info->minor_page_faults);
481 if (info->has_free_mem) {
482 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
484 if (info->has_total_mem) {
485 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
488 monitor_printf(mon, "\n");
490 qapi_free_BalloonInfo(info);
493 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
495 PciMemoryRegionList *region;
497 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
498 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
499 dev->slot, dev->function);
500 monitor_printf(mon, " ");
502 if (dev->class_info.has_desc) {
503 monitor_printf(mon, "%s", dev->class_info.desc);
504 } else {
505 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
508 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
509 dev->id.vendor, dev->id.device);
511 if (dev->has_irq) {
512 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
515 if (dev->has_pci_bridge) {
516 monitor_printf(mon, " BUS %" PRId64 ".\n",
517 dev->pci_bridge->bus.number);
518 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
519 dev->pci_bridge->bus.secondary);
520 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
521 dev->pci_bridge->bus.subordinate);
523 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
524 dev->pci_bridge->bus.io_range->base,
525 dev->pci_bridge->bus.io_range->limit);
527 monitor_printf(mon,
528 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
529 dev->pci_bridge->bus.memory_range->base,
530 dev->pci_bridge->bus.memory_range->limit);
532 monitor_printf(mon, " prefetchable memory range "
533 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
534 dev->pci_bridge->bus.prefetchable_range->base,
535 dev->pci_bridge->bus.prefetchable_range->limit);
538 for (region = dev->regions; region; region = region->next) {
539 uint64_t addr, size;
541 addr = region->value->address;
542 size = region->value->size;
544 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
546 if (!strcmp(region->value->type, "io")) {
547 monitor_printf(mon, "I/O at 0x%04" PRIx64
548 " [0x%04" PRIx64 "].\n",
549 addr, addr + size - 1);
550 } else {
551 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
552 " [0x%08" PRIx64 "].\n",
553 region->value->mem_type_64 ? 64 : 32,
554 region->value->prefetch ? " prefetchable" : "",
555 addr, addr + size - 1);
559 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
561 if (dev->has_pci_bridge) {
562 if (dev->pci_bridge->has_devices) {
563 PciDeviceInfoList *cdev;
564 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
565 hmp_info_pci_device(mon, cdev->value);
571 void hmp_info_pci(Monitor *mon)
573 PciInfoList *info_list, *info;
574 Error *err = NULL;
576 info_list = qmp_query_pci(&err);
577 if (err) {
578 monitor_printf(mon, "PCI devices not supported\n");
579 error_free(err);
580 return;
583 for (info = info_list; info; info = info->next) {
584 PciDeviceInfoList *dev;
586 for (dev = info->value->devices; dev; dev = dev->next) {
587 hmp_info_pci_device(mon, dev->value);
591 qapi_free_PciInfoList(info_list);
594 void hmp_info_block_jobs(Monitor *mon)
596 BlockJobInfoList *list;
597 Error *err = NULL;
599 list = qmp_query_block_jobs(&err);
600 assert(!err);
602 if (!list) {
603 monitor_printf(mon, "No active jobs\n");
604 return;
607 while (list) {
608 if (strcmp(list->value->type, "stream") == 0) {
609 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
610 " of %" PRId64 " bytes, speed limit %" PRId64
611 " bytes/s\n",
612 list->value->device,
613 list->value->offset,
614 list->value->len,
615 list->value->speed);
616 } else {
617 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
618 " of %" PRId64 " bytes, speed limit %" PRId64
619 " bytes/s\n",
620 list->value->type,
621 list->value->device,
622 list->value->offset,
623 list->value->len,
624 list->value->speed);
626 list = list->next;
630 void hmp_quit(Monitor *mon, const QDict *qdict)
632 monitor_suspend(mon);
633 qmp_quit(NULL);
636 void hmp_stop(Monitor *mon, const QDict *qdict)
638 qmp_stop(NULL);
641 void hmp_system_reset(Monitor *mon, const QDict *qdict)
643 qmp_system_reset(NULL);
646 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
648 qmp_system_powerdown(NULL);
651 void hmp_cpu(Monitor *mon, const QDict *qdict)
653 int64_t cpu_index;
655 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
656 use it are converted to the QAPI */
657 cpu_index = qdict_get_int(qdict, "index");
658 if (monitor_set_cpu(cpu_index) < 0) {
659 monitor_printf(mon, "invalid CPU index\n");
663 void hmp_memsave(Monitor *mon, const QDict *qdict)
665 uint32_t size = qdict_get_int(qdict, "size");
666 const char *filename = qdict_get_str(qdict, "filename");
667 uint64_t addr = qdict_get_int(qdict, "val");
668 Error *errp = NULL;
670 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
671 hmp_handle_error(mon, &errp);
674 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
676 uint32_t size = qdict_get_int(qdict, "size");
677 const char *filename = qdict_get_str(qdict, "filename");
678 uint64_t addr = qdict_get_int(qdict, "val");
679 Error *errp = NULL;
681 qmp_pmemsave(addr, size, filename, &errp);
682 hmp_handle_error(mon, &errp);
685 static void hmp_cont_cb(void *opaque, int err)
687 if (!err) {
688 qmp_cont(NULL);
692 static bool key_is_missing(const BlockInfo *bdev)
694 return (bdev->inserted && bdev->inserted->encryption_key_missing);
697 void hmp_cont(Monitor *mon, const QDict *qdict)
699 BlockInfoList *bdev_list, *bdev;
700 Error *errp = NULL;
702 bdev_list = qmp_query_block(NULL);
703 for (bdev = bdev_list; bdev; bdev = bdev->next) {
704 if (key_is_missing(bdev->value)) {
705 monitor_read_block_device_key(mon, bdev->value->device,
706 hmp_cont_cb, NULL);
707 goto out;
711 qmp_cont(&errp);
712 hmp_handle_error(mon, &errp);
714 out:
715 qapi_free_BlockInfoList(bdev_list);
718 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
720 qmp_system_wakeup(NULL);
723 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
725 Error *errp = NULL;
727 qmp_inject_nmi(&errp);
728 hmp_handle_error(mon, &errp);
731 void hmp_set_link(Monitor *mon, const QDict *qdict)
733 const char *name = qdict_get_str(qdict, "name");
734 int up = qdict_get_bool(qdict, "up");
735 Error *errp = NULL;
737 qmp_set_link(name, up, &errp);
738 hmp_handle_error(mon, &errp);
741 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
743 const char *device = qdict_get_str(qdict, "device");
744 const char *password = qdict_get_str(qdict, "password");
745 Error *errp = NULL;
747 qmp_block_passwd(device, password, &errp);
748 hmp_handle_error(mon, &errp);
751 void hmp_balloon(Monitor *mon, const QDict *qdict)
753 int64_t value = qdict_get_int(qdict, "value");
754 Error *errp = NULL;
756 qmp_balloon(value, &errp);
757 if (error_is_set(&errp)) {
758 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
759 error_free(errp);
763 void hmp_block_resize(Monitor *mon, const QDict *qdict)
765 const char *device = qdict_get_str(qdict, "device");
766 int64_t size = qdict_get_int(qdict, "size");
767 Error *errp = NULL;
769 qmp_block_resize(device, size, &errp);
770 hmp_handle_error(mon, &errp);
773 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
775 const char *device = qdict_get_str(qdict, "device");
776 const char *filename = qdict_get_str(qdict, "target");
777 const char *format = qdict_get_try_str(qdict, "format");
778 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
779 int full = qdict_get_try_bool(qdict, "full", 0);
780 enum NewImageMode mode;
781 Error *errp = NULL;
783 if (!filename) {
784 error_set(&errp, QERR_MISSING_PARAMETER, "target");
785 hmp_handle_error(mon, &errp);
786 return;
789 if (reuse) {
790 mode = NEW_IMAGE_MODE_EXISTING;
791 } else {
792 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
795 qmp_drive_mirror(device, filename, !!format, format,
796 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
797 true, mode, false, 0,
798 false, 0, false, 0, &errp);
799 hmp_handle_error(mon, &errp);
802 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
804 const char *device = qdict_get_str(qdict, "device");
805 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
806 const char *format = qdict_get_try_str(qdict, "format");
807 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
808 enum NewImageMode mode;
809 Error *errp = NULL;
811 if (!filename) {
812 /* In the future, if 'snapshot-file' is not specified, the snapshot
813 will be taken internally. Today it's actually required. */
814 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
815 hmp_handle_error(mon, &errp);
816 return;
819 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
820 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
821 true, mode, &errp);
822 hmp_handle_error(mon, &errp);
825 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
827 qmp_migrate_cancel(NULL);
830 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
832 double value = qdict_get_double(qdict, "value");
833 qmp_migrate_set_downtime(value, NULL);
836 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
838 int64_t value = qdict_get_int(qdict, "value");
839 Error *err = NULL;
841 qmp_migrate_set_cache_size(value, &err);
842 if (err) {
843 monitor_printf(mon, "%s\n", error_get_pretty(err));
844 error_free(err);
845 return;
849 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
851 int64_t value = qdict_get_int(qdict, "value");
852 qmp_migrate_set_speed(value, NULL);
855 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
857 const char *cap = qdict_get_str(qdict, "capability");
858 bool state = qdict_get_bool(qdict, "state");
859 Error *err = NULL;
860 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
861 int i;
863 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
864 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
865 caps->value = g_malloc0(sizeof(*caps->value));
866 caps->value->capability = i;
867 caps->value->state = state;
868 caps->next = NULL;
869 qmp_migrate_set_capabilities(caps, &err);
870 break;
874 if (i == MIGRATION_CAPABILITY_MAX) {
875 error_set(&err, QERR_INVALID_PARAMETER, cap);
878 qapi_free_MigrationCapabilityStatusList(caps);
880 if (err) {
881 monitor_printf(mon, "migrate_set_parameter: %s\n",
882 error_get_pretty(err));
883 error_free(err);
887 void hmp_set_password(Monitor *mon, const QDict *qdict)
889 const char *protocol = qdict_get_str(qdict, "protocol");
890 const char *password = qdict_get_str(qdict, "password");
891 const char *connected = qdict_get_try_str(qdict, "connected");
892 Error *err = NULL;
894 qmp_set_password(protocol, password, !!connected, connected, &err);
895 hmp_handle_error(mon, &err);
898 void hmp_expire_password(Monitor *mon, const QDict *qdict)
900 const char *protocol = qdict_get_str(qdict, "protocol");
901 const char *whenstr = qdict_get_str(qdict, "time");
902 Error *err = NULL;
904 qmp_expire_password(protocol, whenstr, &err);
905 hmp_handle_error(mon, &err);
908 void hmp_eject(Monitor *mon, const QDict *qdict)
910 int force = qdict_get_try_bool(qdict, "force", 0);
911 const char *device = qdict_get_str(qdict, "device");
912 Error *err = NULL;
914 qmp_eject(device, true, force, &err);
915 hmp_handle_error(mon, &err);
918 static void hmp_change_read_arg(Monitor *mon, const char *password,
919 void *opaque)
921 qmp_change_vnc_password(password, NULL);
922 monitor_read_command(mon, 1);
925 void hmp_change(Monitor *mon, const QDict *qdict)
927 const char *device = qdict_get_str(qdict, "device");
928 const char *target = qdict_get_str(qdict, "target");
929 const char *arg = qdict_get_try_str(qdict, "arg");
930 Error *err = NULL;
932 if (strcmp(device, "vnc") == 0 &&
933 (strcmp(target, "passwd") == 0 ||
934 strcmp(target, "password") == 0)) {
935 if (!arg) {
936 monitor_read_password(mon, hmp_change_read_arg, NULL);
937 return;
941 qmp_change(device, target, !!arg, arg, &err);
942 if (error_is_set(&err) &&
943 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
944 error_free(err);
945 monitor_read_block_device_key(mon, device, NULL, NULL);
946 return;
948 hmp_handle_error(mon, &err);
951 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
953 Error *err = NULL;
955 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
956 qdict_get_int(qdict, "bps"),
957 qdict_get_int(qdict, "bps_rd"),
958 qdict_get_int(qdict, "bps_wr"),
959 qdict_get_int(qdict, "iops"),
960 qdict_get_int(qdict, "iops_rd"),
961 qdict_get_int(qdict, "iops_wr"), &err);
962 hmp_handle_error(mon, &err);
965 void hmp_block_stream(Monitor *mon, const QDict *qdict)
967 Error *error = NULL;
968 const char *device = qdict_get_str(qdict, "device");
969 const char *base = qdict_get_try_str(qdict, "base");
970 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
972 qmp_block_stream(device, base != NULL, base,
973 qdict_haskey(qdict, "speed"), speed,
974 BLOCKDEV_ON_ERROR_REPORT, true, &error);
976 hmp_handle_error(mon, &error);
979 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
981 Error *error = NULL;
982 const char *device = qdict_get_str(qdict, "device");
983 int64_t value = qdict_get_int(qdict, "speed");
985 qmp_block_job_set_speed(device, value, &error);
987 hmp_handle_error(mon, &error);
990 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
992 Error *error = NULL;
993 const char *device = qdict_get_str(qdict, "device");
994 bool force = qdict_get_try_bool(qdict, "force", 0);
996 qmp_block_job_cancel(device, true, force, &error);
998 hmp_handle_error(mon, &error);
1001 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1003 Error *error = NULL;
1004 const char *device = qdict_get_str(qdict, "device");
1006 qmp_block_job_pause(device, &error);
1008 hmp_handle_error(mon, &error);
1011 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1013 Error *error = NULL;
1014 const char *device = qdict_get_str(qdict, "device");
1016 qmp_block_job_resume(device, &error);
1018 hmp_handle_error(mon, &error);
1021 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1023 Error *error = NULL;
1024 const char *device = qdict_get_str(qdict, "device");
1026 qmp_block_job_complete(device, &error);
1028 hmp_handle_error(mon, &error);
1031 typedef struct MigrationStatus
1033 QEMUTimer *timer;
1034 Monitor *mon;
1035 bool is_block_migration;
1036 } MigrationStatus;
1038 static void hmp_migrate_status_cb(void *opaque)
1040 MigrationStatus *status = opaque;
1041 MigrationInfo *info;
1043 info = qmp_query_migrate(NULL);
1044 if (!info->has_status || strcmp(info->status, "active") == 0) {
1045 if (info->has_disk) {
1046 int progress;
1048 if (info->disk->remaining) {
1049 progress = info->disk->transferred * 100 / info->disk->total;
1050 } else {
1051 progress = 100;
1054 monitor_printf(status->mon, "Completed %d %%\r", progress);
1055 monitor_flush(status->mon);
1058 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1059 } else {
1060 if (status->is_block_migration) {
1061 monitor_printf(status->mon, "\n");
1063 monitor_resume(status->mon);
1064 qemu_del_timer(status->timer);
1065 g_free(status);
1068 qapi_free_MigrationInfo(info);
1071 void hmp_migrate(Monitor *mon, const QDict *qdict)
1073 int detach = qdict_get_try_bool(qdict, "detach", 0);
1074 int blk = qdict_get_try_bool(qdict, "blk", 0);
1075 int inc = qdict_get_try_bool(qdict, "inc", 0);
1076 const char *uri = qdict_get_str(qdict, "uri");
1077 Error *err = NULL;
1079 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1080 if (err) {
1081 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1082 error_free(err);
1083 return;
1086 if (!detach) {
1087 MigrationStatus *status;
1089 if (monitor_suspend(mon) < 0) {
1090 monitor_printf(mon, "terminal does not allow synchronous "
1091 "migration, continuing detached\n");
1092 return;
1095 status = g_malloc0(sizeof(*status));
1096 status->mon = mon;
1097 status->is_block_migration = blk || inc;
1098 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1099 status);
1100 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1104 void hmp_device_del(Monitor *mon, const QDict *qdict)
1106 const char *id = qdict_get_str(qdict, "id");
1107 Error *err = NULL;
1109 qmp_device_del(id, &err);
1110 hmp_handle_error(mon, &err);
1113 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1115 Error *errp = NULL;
1116 int paging = qdict_get_try_bool(qdict, "paging", 0);
1117 const char *file = qdict_get_str(qdict, "filename");
1118 bool has_begin = qdict_haskey(qdict, "begin");
1119 bool has_length = qdict_haskey(qdict, "length");
1120 int64_t begin = 0;
1121 int64_t length = 0;
1122 char *prot;
1124 if (has_begin) {
1125 begin = qdict_get_int(qdict, "begin");
1127 if (has_length) {
1128 length = qdict_get_int(qdict, "length");
1131 prot = g_strconcat("file:", file, NULL);
1133 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1134 &errp);
1135 hmp_handle_error(mon, &errp);
1136 g_free(prot);
1139 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1141 Error *err = NULL;
1142 QemuOpts *opts;
1144 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1145 if (error_is_set(&err)) {
1146 goto out;
1149 netdev_add(opts, &err);
1150 if (error_is_set(&err)) {
1151 qemu_opts_del(opts);
1154 out:
1155 hmp_handle_error(mon, &err);
1158 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1160 const char *id = qdict_get_str(qdict, "id");
1161 Error *err = NULL;
1163 qmp_netdev_del(id, &err);
1164 hmp_handle_error(mon, &err);
1167 void hmp_getfd(Monitor *mon, const QDict *qdict)
1169 const char *fdname = qdict_get_str(qdict, "fdname");
1170 Error *errp = NULL;
1172 qmp_getfd(fdname, &errp);
1173 hmp_handle_error(mon, &errp);
1176 void hmp_closefd(Monitor *mon, const QDict *qdict)
1178 const char *fdname = qdict_get_str(qdict, "fdname");
1179 Error *errp = NULL;
1181 qmp_closefd(fdname, &errp);
1182 hmp_handle_error(mon, &errp);
1185 void hmp_send_key(Monitor *mon, const QDict *qdict)
1187 const char *keys = qdict_get_str(qdict, "keys");
1188 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1189 int has_hold_time = qdict_haskey(qdict, "hold-time");
1190 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1191 Error *err = NULL;
1192 char keyname_buf[16];
1193 char *separator;
1194 int keyname_len;
1196 while (1) {
1197 separator = strchr(keys, '-');
1198 keyname_len = separator ? separator - keys : strlen(keys);
1199 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1201 /* Be compatible with old interface, convert user inputted "<" */
1202 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1203 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1204 keyname_len = 4;
1206 keyname_buf[keyname_len] = 0;
1208 keylist = g_malloc0(sizeof(*keylist));
1209 keylist->value = g_malloc0(sizeof(*keylist->value));
1211 if (!head) {
1212 head = keylist;
1214 if (tmp) {
1215 tmp->next = keylist;
1217 tmp = keylist;
1219 if (strstart(keyname_buf, "0x", NULL)) {
1220 char *endp;
1221 int value = strtoul(keyname_buf, &endp, 0);
1222 if (*endp != '\0') {
1223 goto err_out;
1225 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1226 keylist->value->number = value;
1227 } else {
1228 int idx = index_from_key(keyname_buf);
1229 if (idx == Q_KEY_CODE_MAX) {
1230 goto err_out;
1232 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1233 keylist->value->qcode = idx;
1236 if (!separator) {
1237 break;
1239 keys = separator + 1;
1242 qmp_send_key(head, has_hold_time, hold_time, &err);
1243 hmp_handle_error(mon, &err);
1245 out:
1246 qapi_free_KeyValueList(head);
1247 return;
1249 err_out:
1250 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1251 goto out;
1254 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1256 const char *filename = qdict_get_str(qdict, "filename");
1257 Error *err = NULL;
1259 qmp_screendump(filename, &err);
1260 hmp_handle_error(mon, &err);