net: net_client_init(): use error_set()
[qemu/opensuse.git] / hmp.c
blob42ced2a202b4d8deab6416b5d894aba925e7dfa8
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 "qemu-timer.h"
18 #include "qmp-commands.h"
20 static void hmp_handle_error(Monitor *mon, Error **errp)
22 if (error_is_set(errp)) {
23 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
24 error_free(*errp);
28 void hmp_info_name(Monitor *mon)
30 NameInfo *info;
32 info = qmp_query_name(NULL);
33 if (info->has_name) {
34 monitor_printf(mon, "%s\n", info->name);
36 qapi_free_NameInfo(info);
39 void hmp_info_version(Monitor *mon)
41 VersionInfo *info;
43 info = qmp_query_version(NULL);
45 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
46 info->qemu.major, info->qemu.minor, info->qemu.micro,
47 info->package);
49 qapi_free_VersionInfo(info);
52 void hmp_info_kvm(Monitor *mon)
54 KvmInfo *info;
56 info = qmp_query_kvm(NULL);
57 monitor_printf(mon, "kvm support: ");
58 if (info->present) {
59 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
60 } else {
61 monitor_printf(mon, "not compiled\n");
64 qapi_free_KvmInfo(info);
67 void hmp_info_status(Monitor *mon)
69 StatusInfo *info;
71 info = qmp_query_status(NULL);
73 monitor_printf(mon, "VM status: %s%s",
74 info->running ? "running" : "paused",
75 info->singlestep ? " (single step mode)" : "");
77 if (!info->running && info->status != RUN_STATE_PAUSED) {
78 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
81 monitor_printf(mon, "\n");
83 qapi_free_StatusInfo(info);
86 void hmp_info_uuid(Monitor *mon)
88 UuidInfo *info;
90 info = qmp_query_uuid(NULL);
91 monitor_printf(mon, "%s\n", info->UUID);
92 qapi_free_UuidInfo(info);
95 void hmp_info_chardev(Monitor *mon)
97 ChardevInfoList *char_info, *info;
99 char_info = qmp_query_chardev(NULL);
100 for (info = char_info; info; info = info->next) {
101 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
102 info->value->filename);
105 qapi_free_ChardevInfoList(char_info);
108 void hmp_info_mice(Monitor *mon)
110 MouseInfoList *mice_list, *mouse;
112 mice_list = qmp_query_mice(NULL);
113 if (!mice_list) {
114 monitor_printf(mon, "No mouse devices connected\n");
115 return;
118 for (mouse = mice_list; mouse; mouse = mouse->next) {
119 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
120 mouse->value->current ? '*' : ' ',
121 mouse->value->index, mouse->value->name,
122 mouse->value->absolute ? " (absolute)" : "");
125 qapi_free_MouseInfoList(mice_list);
128 void hmp_info_migrate(Monitor *mon)
130 MigrationInfo *info;
132 info = qmp_query_migrate(NULL);
134 if (info->has_status) {
135 monitor_printf(mon, "Migration status: %s\n", info->status);
138 if (info->has_ram) {
139 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
140 info->ram->transferred >> 10);
141 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
142 info->ram->remaining >> 10);
143 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
144 info->ram->total >> 10);
147 if (info->has_disk) {
148 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
149 info->disk->transferred >> 10);
150 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
151 info->disk->remaining >> 10);
152 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
153 info->disk->total >> 10);
156 qapi_free_MigrationInfo(info);
159 void hmp_info_cpus(Monitor *mon)
161 CpuInfoList *cpu_list, *cpu;
163 cpu_list = qmp_query_cpus(NULL);
165 for (cpu = cpu_list; cpu; cpu = cpu->next) {
166 int active = ' ';
168 if (cpu->value->CPU == monitor_get_cpu_index()) {
169 active = '*';
172 monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
174 if (cpu->value->has_pc) {
175 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
177 if (cpu->value->has_nip) {
178 monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
180 if (cpu->value->has_npc) {
181 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
182 monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
184 if (cpu->value->has_PC) {
185 monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
188 if (cpu->value->halted) {
189 monitor_printf(mon, " (halted)");
192 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
195 qapi_free_CpuInfoList(cpu_list);
198 void hmp_info_block(Monitor *mon)
200 BlockInfoList *block_list, *info;
202 block_list = qmp_query_block(NULL);
204 for (info = block_list; info; info = info->next) {
205 monitor_printf(mon, "%s: removable=%d",
206 info->value->device, info->value->removable);
208 if (info->value->removable) {
209 monitor_printf(mon, " locked=%d", info->value->locked);
210 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
213 if (info->value->has_io_status) {
214 monitor_printf(mon, " io-status=%s",
215 BlockDeviceIoStatus_lookup[info->value->io_status]);
218 if (info->value->has_inserted) {
219 monitor_printf(mon, " file=");
220 monitor_print_filename(mon, info->value->inserted->file);
222 if (info->value->inserted->has_backing_file) {
223 monitor_printf(mon, " backing_file=");
224 monitor_print_filename(mon, info->value->inserted->backing_file);
226 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
227 info->value->inserted->ro,
228 info->value->inserted->drv,
229 info->value->inserted->encrypted);
231 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
232 " bps_wr=%" PRId64 " iops=%" PRId64
233 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
234 info->value->inserted->bps,
235 info->value->inserted->bps_rd,
236 info->value->inserted->bps_wr,
237 info->value->inserted->iops,
238 info->value->inserted->iops_rd,
239 info->value->inserted->iops_wr);
240 } else {
241 monitor_printf(mon, " [not inserted]");
244 monitor_printf(mon, "\n");
247 qapi_free_BlockInfoList(block_list);
250 void hmp_info_blockstats(Monitor *mon)
252 BlockStatsList *stats_list, *stats;
254 stats_list = qmp_query_blockstats(NULL);
256 for (stats = stats_list; stats; stats = stats->next) {
257 if (!stats->value->has_device) {
258 continue;
261 monitor_printf(mon, "%s:", stats->value->device);
262 monitor_printf(mon, " rd_bytes=%" PRId64
263 " wr_bytes=%" PRId64
264 " rd_operations=%" PRId64
265 " wr_operations=%" PRId64
266 " flush_operations=%" PRId64
267 " wr_total_time_ns=%" PRId64
268 " rd_total_time_ns=%" PRId64
269 " flush_total_time_ns=%" PRId64
270 "\n",
271 stats->value->stats->rd_bytes,
272 stats->value->stats->wr_bytes,
273 stats->value->stats->rd_operations,
274 stats->value->stats->wr_operations,
275 stats->value->stats->flush_operations,
276 stats->value->stats->wr_total_time_ns,
277 stats->value->stats->rd_total_time_ns,
278 stats->value->stats->flush_total_time_ns);
281 qapi_free_BlockStatsList(stats_list);
284 void hmp_info_vnc(Monitor *mon)
286 VncInfo *info;
287 Error *err = NULL;
288 VncClientInfoList *client;
290 info = qmp_query_vnc(&err);
291 if (err) {
292 monitor_printf(mon, "%s\n", error_get_pretty(err));
293 error_free(err);
294 return;
297 if (!info->enabled) {
298 monitor_printf(mon, "Server: disabled\n");
299 goto out;
302 monitor_printf(mon, "Server:\n");
303 if (info->has_host && info->has_service) {
304 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
306 if (info->has_auth) {
307 monitor_printf(mon, " auth: %s\n", info->auth);
310 if (!info->has_clients || info->clients == NULL) {
311 monitor_printf(mon, "Client: none\n");
312 } else {
313 for (client = info->clients; client; client = client->next) {
314 monitor_printf(mon, "Client:\n");
315 monitor_printf(mon, " address: %s:%s\n",
316 client->value->host, client->value->service);
317 monitor_printf(mon, " x509_dname: %s\n",
318 client->value->x509_dname ?
319 client->value->x509_dname : "none");
320 monitor_printf(mon, " username: %s\n",
321 client->value->has_sasl_username ?
322 client->value->sasl_username : "none");
326 out:
327 qapi_free_VncInfo(info);
330 void hmp_info_spice(Monitor *mon)
332 SpiceChannelList *chan;
333 SpiceInfo *info;
335 info = qmp_query_spice(NULL);
337 if (!info->enabled) {
338 monitor_printf(mon, "Server: disabled\n");
339 goto out;
342 monitor_printf(mon, "Server:\n");
343 if (info->has_port) {
344 monitor_printf(mon, " address: %s:%" PRId64 "\n",
345 info->host, info->port);
347 if (info->has_tls_port) {
348 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
349 info->host, info->tls_port);
351 monitor_printf(mon, " auth: %s\n", info->auth);
352 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
353 monitor_printf(mon, " mouse-mode: %s\n",
354 SpiceQueryMouseMode_lookup[info->mouse_mode]);
356 if (!info->has_channels || info->channels == NULL) {
357 monitor_printf(mon, "Channels: none\n");
358 } else {
359 for (chan = info->channels; chan; chan = chan->next) {
360 monitor_printf(mon, "Channel:\n");
361 monitor_printf(mon, " address: %s:%s%s\n",
362 chan->value->host, chan->value->port,
363 chan->value->tls ? " [tls]" : "");
364 monitor_printf(mon, " session: %" PRId64 "\n",
365 chan->value->connection_id);
366 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
367 chan->value->channel_type, chan->value->channel_id);
371 out:
372 qapi_free_SpiceInfo(info);
375 void hmp_info_balloon(Monitor *mon)
377 BalloonInfo *info;
378 Error *err = NULL;
380 info = qmp_query_balloon(&err);
381 if (err) {
382 monitor_printf(mon, "%s\n", error_get_pretty(err));
383 error_free(err);
384 return;
387 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
388 if (info->has_mem_swapped_in) {
389 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
391 if (info->has_mem_swapped_out) {
392 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
394 if (info->has_major_page_faults) {
395 monitor_printf(mon, " major_page_faults=%" PRId64,
396 info->major_page_faults);
398 if (info->has_minor_page_faults) {
399 monitor_printf(mon, " minor_page_faults=%" PRId64,
400 info->minor_page_faults);
402 if (info->has_free_mem) {
403 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
405 if (info->has_total_mem) {
406 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
409 monitor_printf(mon, "\n");
411 qapi_free_BalloonInfo(info);
414 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
416 PciMemoryRegionList *region;
418 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
419 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
420 dev->slot, dev->function);
421 monitor_printf(mon, " ");
423 if (dev->class_info.has_desc) {
424 monitor_printf(mon, "%s", dev->class_info.desc);
425 } else {
426 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
429 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
430 dev->id.vendor, dev->id.device);
432 if (dev->has_irq) {
433 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
436 if (dev->has_pci_bridge) {
437 monitor_printf(mon, " BUS %" PRId64 ".\n",
438 dev->pci_bridge->bus.number);
439 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
440 dev->pci_bridge->bus.secondary);
441 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
442 dev->pci_bridge->bus.subordinate);
444 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
445 dev->pci_bridge->bus.io_range->base,
446 dev->pci_bridge->bus.io_range->limit);
448 monitor_printf(mon,
449 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
450 dev->pci_bridge->bus.memory_range->base,
451 dev->pci_bridge->bus.memory_range->limit);
453 monitor_printf(mon, " prefetchable memory range "
454 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
455 dev->pci_bridge->bus.prefetchable_range->base,
456 dev->pci_bridge->bus.prefetchable_range->limit);
459 for (region = dev->regions; region; region = region->next) {
460 uint64_t addr, size;
462 addr = region->value->address;
463 size = region->value->size;
465 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
467 if (!strcmp(region->value->type, "io")) {
468 monitor_printf(mon, "I/O at 0x%04" PRIx64
469 " [0x%04" PRIx64 "].\n",
470 addr, addr + size - 1);
471 } else {
472 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
473 " [0x%08" PRIx64 "].\n",
474 region->value->mem_type_64 ? 64 : 32,
475 region->value->prefetch ? " prefetchable" : "",
476 addr, addr + size - 1);
480 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
482 if (dev->has_pci_bridge) {
483 if (dev->pci_bridge->has_devices) {
484 PciDeviceInfoList *cdev;
485 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
486 hmp_info_pci_device(mon, cdev->value);
492 void hmp_info_pci(Monitor *mon)
494 PciInfoList *info_list, *info;
495 Error *err = NULL;
497 info_list = qmp_query_pci(&err);
498 if (err) {
499 monitor_printf(mon, "PCI devices not supported\n");
500 error_free(err);
501 return;
504 for (info = info_list; info; info = info->next) {
505 PciDeviceInfoList *dev;
507 for (dev = info->value->devices; dev; dev = dev->next) {
508 hmp_info_pci_device(mon, dev->value);
512 qapi_free_PciInfoList(info_list);
515 void hmp_info_block_jobs(Monitor *mon)
517 BlockJobInfoList *list;
518 Error *err = NULL;
520 list = qmp_query_block_jobs(&err);
521 assert(!err);
523 if (!list) {
524 monitor_printf(mon, "No active jobs\n");
525 return;
528 while (list) {
529 if (strcmp(list->value->type, "stream") == 0) {
530 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
531 " of %" PRId64 " bytes, speed limit %" PRId64
532 " bytes/s\n",
533 list->value->device,
534 list->value->offset,
535 list->value->len,
536 list->value->speed);
537 } else {
538 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
539 " of %" PRId64 " bytes, speed limit %" PRId64
540 " bytes/s\n",
541 list->value->type,
542 list->value->device,
543 list->value->offset,
544 list->value->len,
545 list->value->speed);
547 list = list->next;
551 void hmp_quit(Monitor *mon, const QDict *qdict)
553 monitor_suspend(mon);
554 qmp_quit(NULL);
557 void hmp_stop(Monitor *mon, const QDict *qdict)
559 qmp_stop(NULL);
562 void hmp_system_reset(Monitor *mon, const QDict *qdict)
564 qmp_system_reset(NULL);
567 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
569 qmp_system_powerdown(NULL);
572 void hmp_cpu(Monitor *mon, const QDict *qdict)
574 int64_t cpu_index;
576 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
577 use it are converted to the QAPI */
578 cpu_index = qdict_get_int(qdict, "index");
579 if (monitor_set_cpu(cpu_index) < 0) {
580 monitor_printf(mon, "invalid CPU index\n");
584 void hmp_memsave(Monitor *mon, const QDict *qdict)
586 uint32_t size = qdict_get_int(qdict, "size");
587 const char *filename = qdict_get_str(qdict, "filename");
588 uint64_t addr = qdict_get_int(qdict, "val");
589 Error *errp = NULL;
591 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
592 hmp_handle_error(mon, &errp);
595 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
597 uint32_t size = qdict_get_int(qdict, "size");
598 const char *filename = qdict_get_str(qdict, "filename");
599 uint64_t addr = qdict_get_int(qdict, "val");
600 Error *errp = NULL;
602 qmp_pmemsave(addr, size, filename, &errp);
603 hmp_handle_error(mon, &errp);
606 static void hmp_cont_cb(void *opaque, int err)
608 Monitor *mon = opaque;
610 if (!err) {
611 hmp_cont(mon, NULL);
615 void hmp_cont(Monitor *mon, const QDict *qdict)
617 Error *errp = NULL;
619 qmp_cont(&errp);
620 if (error_is_set(&errp)) {
621 if (error_is_type(errp, QERR_DEVICE_ENCRYPTED)) {
622 const char *device;
624 /* The device is encrypted. Ask the user for the password
625 and retry */
627 device = error_get_field(errp, "device");
628 assert(device != NULL);
630 monitor_read_block_device_key(mon, device, hmp_cont_cb, mon);
631 error_free(errp);
632 return;
634 hmp_handle_error(mon, &errp);
638 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
640 qmp_system_wakeup(NULL);
643 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
645 Error *errp = NULL;
647 qmp_inject_nmi(&errp);
648 hmp_handle_error(mon, &errp);
651 void hmp_set_link(Monitor *mon, const QDict *qdict)
653 const char *name = qdict_get_str(qdict, "name");
654 int up = qdict_get_bool(qdict, "up");
655 Error *errp = NULL;
657 qmp_set_link(name, up, &errp);
658 hmp_handle_error(mon, &errp);
661 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
663 const char *device = qdict_get_str(qdict, "device");
664 const char *password = qdict_get_str(qdict, "password");
665 Error *errp = NULL;
667 qmp_block_passwd(device, password, &errp);
668 hmp_handle_error(mon, &errp);
671 void hmp_balloon(Monitor *mon, const QDict *qdict)
673 int64_t value = qdict_get_int(qdict, "value");
674 Error *errp = NULL;
676 qmp_balloon(value, &errp);
677 if (error_is_set(&errp)) {
678 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
679 error_free(errp);
683 void hmp_block_resize(Monitor *mon, const QDict *qdict)
685 const char *device = qdict_get_str(qdict, "device");
686 int64_t size = qdict_get_int(qdict, "size");
687 Error *errp = NULL;
689 qmp_block_resize(device, size, &errp);
690 hmp_handle_error(mon, &errp);
693 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
695 const char *device = qdict_get_str(qdict, "device");
696 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
697 const char *format = qdict_get_try_str(qdict, "format");
698 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
699 enum NewImageMode mode;
700 Error *errp = NULL;
702 if (!filename) {
703 /* In the future, if 'snapshot-file' is not specified, the snapshot
704 will be taken internally. Today it's actually required. */
705 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
706 hmp_handle_error(mon, &errp);
707 return;
710 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
711 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
712 true, mode, &errp);
713 hmp_handle_error(mon, &errp);
716 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
718 qmp_migrate_cancel(NULL);
721 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
723 double value = qdict_get_double(qdict, "value");
724 qmp_migrate_set_downtime(value, NULL);
727 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
729 int64_t value = qdict_get_int(qdict, "value");
730 qmp_migrate_set_speed(value, NULL);
733 void hmp_set_password(Monitor *mon, const QDict *qdict)
735 const char *protocol = qdict_get_str(qdict, "protocol");
736 const char *password = qdict_get_str(qdict, "password");
737 const char *connected = qdict_get_try_str(qdict, "connected");
738 Error *err = NULL;
740 qmp_set_password(protocol, password, !!connected, connected, &err);
741 hmp_handle_error(mon, &err);
744 void hmp_expire_password(Monitor *mon, const QDict *qdict)
746 const char *protocol = qdict_get_str(qdict, "protocol");
747 const char *whenstr = qdict_get_str(qdict, "time");
748 Error *err = NULL;
750 qmp_expire_password(protocol, whenstr, &err);
751 hmp_handle_error(mon, &err);
754 void hmp_eject(Monitor *mon, const QDict *qdict)
756 int force = qdict_get_try_bool(qdict, "force", 0);
757 const char *device = qdict_get_str(qdict, "device");
758 Error *err = NULL;
760 qmp_eject(device, true, force, &err);
761 hmp_handle_error(mon, &err);
764 static void hmp_change_read_arg(Monitor *mon, const char *password,
765 void *opaque)
767 qmp_change_vnc_password(password, NULL);
768 monitor_read_command(mon, 1);
771 static void cb_hmp_change_bdrv_pwd(Monitor *mon, const char *password,
772 void *opaque)
774 Error *encryption_err = opaque;
775 Error *err = NULL;
776 const char *device;
778 device = error_get_field(encryption_err, "device");
780 qmp_block_passwd(device, password, &err);
781 hmp_handle_error(mon, &err);
782 error_free(encryption_err);
784 monitor_read_command(mon, 1);
787 void hmp_change(Monitor *mon, const QDict *qdict)
789 const char *device = qdict_get_str(qdict, "device");
790 const char *target = qdict_get_str(qdict, "target");
791 const char *arg = qdict_get_try_str(qdict, "arg");
792 Error *err = NULL;
794 if (strcmp(device, "vnc") == 0 &&
795 (strcmp(target, "passwd") == 0 ||
796 strcmp(target, "password") == 0)) {
797 if (!arg) {
798 monitor_read_password(mon, hmp_change_read_arg, NULL);
799 return;
803 qmp_change(device, target, !!arg, arg, &err);
804 if (error_is_type(err, QERR_DEVICE_ENCRYPTED)) {
805 monitor_printf(mon, "%s (%s) is encrypted.\n",
806 error_get_field(err, "device"),
807 error_get_field(err, "filename"));
808 if (!monitor_get_rs(mon)) {
809 monitor_printf(mon,
810 "terminal does not support password prompting\n");
811 error_free(err);
812 return;
814 readline_start(monitor_get_rs(mon), "Password: ", 1,
815 cb_hmp_change_bdrv_pwd, err);
816 return;
818 hmp_handle_error(mon, &err);
821 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
823 Error *err = NULL;
825 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
826 qdict_get_int(qdict, "bps"),
827 qdict_get_int(qdict, "bps_rd"),
828 qdict_get_int(qdict, "bps_wr"),
829 qdict_get_int(qdict, "iops"),
830 qdict_get_int(qdict, "iops_rd"),
831 qdict_get_int(qdict, "iops_wr"), &err);
832 hmp_handle_error(mon, &err);
835 void hmp_block_stream(Monitor *mon, const QDict *qdict)
837 Error *error = NULL;
838 const char *device = qdict_get_str(qdict, "device");
839 const char *base = qdict_get_try_str(qdict, "base");
840 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
842 qmp_block_stream(device, base != NULL, base,
843 qdict_haskey(qdict, "speed"), speed, &error);
845 hmp_handle_error(mon, &error);
848 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
850 Error *error = NULL;
851 const char *device = qdict_get_str(qdict, "device");
852 int64_t value = qdict_get_int(qdict, "speed");
854 qmp_block_job_set_speed(device, value, &error);
856 hmp_handle_error(mon, &error);
859 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
861 Error *error = NULL;
862 const char *device = qdict_get_str(qdict, "device");
864 qmp_block_job_cancel(device, &error);
866 hmp_handle_error(mon, &error);
869 typedef struct MigrationStatus
871 QEMUTimer *timer;
872 Monitor *mon;
873 bool is_block_migration;
874 } MigrationStatus;
876 static void hmp_migrate_status_cb(void *opaque)
878 MigrationStatus *status = opaque;
879 MigrationInfo *info;
881 info = qmp_query_migrate(NULL);
882 if (!info->has_status || strcmp(info->status, "active") == 0) {
883 if (info->has_disk) {
884 int progress;
886 if (info->disk->remaining) {
887 progress = info->disk->transferred * 100 / info->disk->total;
888 } else {
889 progress = 100;
892 monitor_printf(status->mon, "Completed %d %%\r", progress);
893 monitor_flush(status->mon);
896 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
897 } else {
898 if (status->is_block_migration) {
899 monitor_printf(status->mon, "\n");
901 monitor_resume(status->mon);
902 qemu_del_timer(status->timer);
903 g_free(status);
906 qapi_free_MigrationInfo(info);
909 void hmp_migrate(Monitor *mon, const QDict *qdict)
911 int detach = qdict_get_try_bool(qdict, "detach", 0);
912 int blk = qdict_get_try_bool(qdict, "blk", 0);
913 int inc = qdict_get_try_bool(qdict, "inc", 0);
914 const char *uri = qdict_get_str(qdict, "uri");
915 Error *err = NULL;
917 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
918 if (err) {
919 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
920 error_free(err);
921 return;
924 if (!detach) {
925 MigrationStatus *status;
927 if (monitor_suspend(mon) < 0) {
928 monitor_printf(mon, "terminal does not allow synchronous "
929 "migration, continuing detached\n");
930 return;
933 status = g_malloc0(sizeof(*status));
934 status->mon = mon;
935 status->is_block_migration = blk || inc;
936 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
937 status);
938 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
942 void hmp_device_del(Monitor *mon, const QDict *qdict)
944 const char *id = qdict_get_str(qdict, "id");
945 Error *err = NULL;
947 qmp_device_del(id, &err);
948 hmp_handle_error(mon, &err);
951 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
953 Error *errp = NULL;
954 int paging = qdict_get_try_bool(qdict, "paging", 0);
955 const char *file = qdict_get_str(qdict, "protocol");
956 bool has_begin = qdict_haskey(qdict, "begin");
957 bool has_length = qdict_haskey(qdict, "length");
958 int64_t begin = 0;
959 int64_t length = 0;
961 if (has_begin) {
962 begin = qdict_get_int(qdict, "begin");
964 if (has_length) {
965 length = qdict_get_int(qdict, "length");
968 qmp_dump_guest_memory(paging, file, has_begin, begin, has_length, length,
969 &errp);
970 hmp_handle_error(mon, &errp);