qdev: Convert busses to QEMU Object Model
[qemu/opensuse.git] / hmp.c
blob2ce8cb9df1791f669ecbf1536358ecce3211f221
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"
22 static void hmp_handle_error(Monitor *mon, Error **errp)
24 if (error_is_set(errp)) {
25 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
26 error_free(*errp);
30 void hmp_info_name(Monitor *mon)
32 NameInfo *info;
34 info = qmp_query_name(NULL);
35 if (info->has_name) {
36 monitor_printf(mon, "%s\n", info->name);
38 qapi_free_NameInfo(info);
41 void hmp_info_version(Monitor *mon)
43 VersionInfo *info;
45 info = qmp_query_version(NULL);
47 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
48 info->qemu.major, info->qemu.minor, info->qemu.micro,
49 info->package);
51 qapi_free_VersionInfo(info);
54 void hmp_info_kvm(Monitor *mon)
56 KvmInfo *info;
58 info = qmp_query_kvm(NULL);
59 monitor_printf(mon, "kvm support: ");
60 if (info->present) {
61 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
62 } else {
63 monitor_printf(mon, "not compiled\n");
66 qapi_free_KvmInfo(info);
69 void hmp_info_status(Monitor *mon)
71 StatusInfo *info;
73 info = qmp_query_status(NULL);
75 monitor_printf(mon, "VM status: %s%s",
76 info->running ? "running" : "paused",
77 info->singlestep ? " (single step mode)" : "");
79 if (!info->running && info->status != RUN_STATE_PAUSED) {
80 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
83 monitor_printf(mon, "\n");
85 qapi_free_StatusInfo(info);
88 void hmp_info_uuid(Monitor *mon)
90 UuidInfo *info;
92 info = qmp_query_uuid(NULL);
93 monitor_printf(mon, "%s\n", info->UUID);
94 qapi_free_UuidInfo(info);
97 void hmp_info_chardev(Monitor *mon)
99 ChardevInfoList *char_info, *info;
101 char_info = qmp_query_chardev(NULL);
102 for (info = char_info; info; info = info->next) {
103 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
104 info->value->filename);
107 qapi_free_ChardevInfoList(char_info);
110 void hmp_info_mice(Monitor *mon)
112 MouseInfoList *mice_list, *mouse;
114 mice_list = qmp_query_mice(NULL);
115 if (!mice_list) {
116 monitor_printf(mon, "No mouse devices connected\n");
117 return;
120 for (mouse = mice_list; mouse; mouse = mouse->next) {
121 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
122 mouse->value->current ? '*' : ' ',
123 mouse->value->index, mouse->value->name,
124 mouse->value->absolute ? " (absolute)" : "");
127 qapi_free_MouseInfoList(mice_list);
130 void hmp_info_migrate(Monitor *mon)
132 MigrationInfo *info;
134 info = qmp_query_migrate(NULL);
136 if (info->has_status) {
137 monitor_printf(mon, "Migration status: %s\n", info->status);
140 if (info->has_ram) {
141 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
142 info->ram->transferred >> 10);
143 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
144 info->ram->remaining >> 10);
145 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
146 info->ram->total >> 10);
149 if (info->has_disk) {
150 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
151 info->disk->transferred >> 10);
152 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
153 info->disk->remaining >> 10);
154 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
155 info->disk->total >> 10);
158 qapi_free_MigrationInfo(info);
161 void hmp_info_cpus(Monitor *mon)
163 CpuInfoList *cpu_list, *cpu;
165 cpu_list = qmp_query_cpus(NULL);
167 for (cpu = cpu_list; cpu; cpu = cpu->next) {
168 int active = ' ';
170 if (cpu->value->CPU == monitor_get_cpu_index()) {
171 active = '*';
174 monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
176 if (cpu->value->has_pc) {
177 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
179 if (cpu->value->has_nip) {
180 monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
182 if (cpu->value->has_npc) {
183 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
184 monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
186 if (cpu->value->has_PC) {
187 monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
190 if (cpu->value->halted) {
191 monitor_printf(mon, " (halted)");
194 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
197 qapi_free_CpuInfoList(cpu_list);
200 void hmp_info_block(Monitor *mon)
202 BlockInfoList *block_list, *info;
204 block_list = qmp_query_block(NULL);
206 for (info = block_list; info; info = info->next) {
207 monitor_printf(mon, "%s: removable=%d",
208 info->value->device, info->value->removable);
210 if (info->value->removable) {
211 monitor_printf(mon, " locked=%d", info->value->locked);
212 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
215 if (info->value->has_io_status) {
216 monitor_printf(mon, " io-status=%s",
217 BlockDeviceIoStatus_lookup[info->value->io_status]);
220 if (info->value->has_inserted) {
221 monitor_printf(mon, " file=");
222 monitor_print_filename(mon, info->value->inserted->file);
224 if (info->value->inserted->has_backing_file) {
225 monitor_printf(mon, " backing_file=");
226 monitor_print_filename(mon, info->value->inserted->backing_file);
228 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
229 info->value->inserted->ro,
230 info->value->inserted->drv,
231 info->value->inserted->encrypted);
233 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
234 " bps_wr=%" PRId64 " iops=%" PRId64
235 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
236 info->value->inserted->bps,
237 info->value->inserted->bps_rd,
238 info->value->inserted->bps_wr,
239 info->value->inserted->iops,
240 info->value->inserted->iops_rd,
241 info->value->inserted->iops_wr);
242 } else {
243 monitor_printf(mon, " [not inserted]");
246 monitor_printf(mon, "\n");
249 qapi_free_BlockInfoList(block_list);
252 void hmp_info_blockstats(Monitor *mon)
254 BlockStatsList *stats_list, *stats;
256 stats_list = qmp_query_blockstats(NULL);
258 for (stats = stats_list; stats; stats = stats->next) {
259 if (!stats->value->has_device) {
260 continue;
263 monitor_printf(mon, "%s:", stats->value->device);
264 monitor_printf(mon, " rd_bytes=%" PRId64
265 " wr_bytes=%" PRId64
266 " rd_operations=%" PRId64
267 " wr_operations=%" PRId64
268 " flush_operations=%" PRId64
269 " wr_total_time_ns=%" PRId64
270 " rd_total_time_ns=%" PRId64
271 " flush_total_time_ns=%" PRId64
272 "\n",
273 stats->value->stats->rd_bytes,
274 stats->value->stats->wr_bytes,
275 stats->value->stats->rd_operations,
276 stats->value->stats->wr_operations,
277 stats->value->stats->flush_operations,
278 stats->value->stats->wr_total_time_ns,
279 stats->value->stats->rd_total_time_ns,
280 stats->value->stats->flush_total_time_ns);
283 qapi_free_BlockStatsList(stats_list);
286 void hmp_info_vnc(Monitor *mon)
288 VncInfo *info;
289 Error *err = NULL;
290 VncClientInfoList *client;
292 info = qmp_query_vnc(&err);
293 if (err) {
294 monitor_printf(mon, "%s\n", error_get_pretty(err));
295 error_free(err);
296 return;
299 if (!info->enabled) {
300 monitor_printf(mon, "Server: disabled\n");
301 goto out;
304 monitor_printf(mon, "Server:\n");
305 if (info->has_host && info->has_service) {
306 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
308 if (info->has_auth) {
309 monitor_printf(mon, " auth: %s\n", info->auth);
312 if (!info->has_clients || info->clients == NULL) {
313 monitor_printf(mon, "Client: none\n");
314 } else {
315 for (client = info->clients; client; client = client->next) {
316 monitor_printf(mon, "Client:\n");
317 monitor_printf(mon, " address: %s:%s\n",
318 client->value->host, client->value->service);
319 monitor_printf(mon, " x509_dname: %s\n",
320 client->value->x509_dname ?
321 client->value->x509_dname : "none");
322 monitor_printf(mon, " username: %s\n",
323 client->value->has_sasl_username ?
324 client->value->sasl_username : "none");
328 out:
329 qapi_free_VncInfo(info);
332 void hmp_info_spice(Monitor *mon)
334 SpiceChannelList *chan;
335 SpiceInfo *info;
337 info = qmp_query_spice(NULL);
339 if (!info->enabled) {
340 monitor_printf(mon, "Server: disabled\n");
341 goto out;
344 monitor_printf(mon, "Server:\n");
345 if (info->has_port) {
346 monitor_printf(mon, " address: %s:%" PRId64 "\n",
347 info->host, info->port);
349 if (info->has_tls_port) {
350 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
351 info->host, info->tls_port);
353 monitor_printf(mon, " auth: %s\n", info->auth);
354 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
355 monitor_printf(mon, " mouse-mode: %s\n",
356 SpiceQueryMouseMode_lookup[info->mouse_mode]);
358 if (!info->has_channels || info->channels == NULL) {
359 monitor_printf(mon, "Channels: none\n");
360 } else {
361 for (chan = info->channels; chan; chan = chan->next) {
362 monitor_printf(mon, "Channel:\n");
363 monitor_printf(mon, " address: %s:%s%s\n",
364 chan->value->host, chan->value->port,
365 chan->value->tls ? " [tls]" : "");
366 monitor_printf(mon, " session: %" PRId64 "\n",
367 chan->value->connection_id);
368 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
369 chan->value->channel_type, chan->value->channel_id);
373 out:
374 qapi_free_SpiceInfo(info);
377 void hmp_info_balloon(Monitor *mon)
379 BalloonInfo *info;
380 Error *err = NULL;
382 info = qmp_query_balloon(&err);
383 if (err) {
384 monitor_printf(mon, "%s\n", error_get_pretty(err));
385 error_free(err);
386 return;
389 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
390 if (info->has_mem_swapped_in) {
391 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
393 if (info->has_mem_swapped_out) {
394 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
396 if (info->has_major_page_faults) {
397 monitor_printf(mon, " major_page_faults=%" PRId64,
398 info->major_page_faults);
400 if (info->has_minor_page_faults) {
401 monitor_printf(mon, " minor_page_faults=%" PRId64,
402 info->minor_page_faults);
404 if (info->has_free_mem) {
405 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
407 if (info->has_total_mem) {
408 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
411 monitor_printf(mon, "\n");
413 qapi_free_BalloonInfo(info);
416 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
418 PciMemoryRegionList *region;
420 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
421 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
422 dev->slot, dev->function);
423 monitor_printf(mon, " ");
425 if (dev->class_info.has_desc) {
426 monitor_printf(mon, "%s", dev->class_info.desc);
427 } else {
428 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
431 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
432 dev->id.vendor, dev->id.device);
434 if (dev->has_irq) {
435 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
438 if (dev->has_pci_bridge) {
439 monitor_printf(mon, " BUS %" PRId64 ".\n",
440 dev->pci_bridge->bus.number);
441 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
442 dev->pci_bridge->bus.secondary);
443 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
444 dev->pci_bridge->bus.subordinate);
446 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
447 dev->pci_bridge->bus.io_range->base,
448 dev->pci_bridge->bus.io_range->limit);
450 monitor_printf(mon,
451 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
452 dev->pci_bridge->bus.memory_range->base,
453 dev->pci_bridge->bus.memory_range->limit);
455 monitor_printf(mon, " prefetchable memory range "
456 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
457 dev->pci_bridge->bus.prefetchable_range->base,
458 dev->pci_bridge->bus.prefetchable_range->limit);
461 for (region = dev->regions; region; region = region->next) {
462 uint64_t addr, size;
464 addr = region->value->address;
465 size = region->value->size;
467 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
469 if (!strcmp(region->value->type, "io")) {
470 monitor_printf(mon, "I/O at 0x%04" PRIx64
471 " [0x%04" PRIx64 "].\n",
472 addr, addr + size - 1);
473 } else {
474 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
475 " [0x%08" PRIx64 "].\n",
476 region->value->mem_type_64 ? 64 : 32,
477 region->value->prefetch ? " prefetchable" : "",
478 addr, addr + size - 1);
482 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
484 if (dev->has_pci_bridge) {
485 if (dev->pci_bridge->has_devices) {
486 PciDeviceInfoList *cdev;
487 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
488 hmp_info_pci_device(mon, cdev->value);
494 void hmp_info_pci(Monitor *mon)
496 PciInfoList *info_list, *info;
497 Error *err = NULL;
499 info_list = qmp_query_pci(&err);
500 if (err) {
501 monitor_printf(mon, "PCI devices not supported\n");
502 error_free(err);
503 return;
506 for (info = info_list; info; info = info->next) {
507 PciDeviceInfoList *dev;
509 for (dev = info->value->devices; dev; dev = dev->next) {
510 hmp_info_pci_device(mon, dev->value);
514 qapi_free_PciInfoList(info_list);
517 void hmp_info_block_jobs(Monitor *mon)
519 BlockJobInfoList *list;
520 Error *err = NULL;
522 list = qmp_query_block_jobs(&err);
523 assert(!err);
525 if (!list) {
526 monitor_printf(mon, "No active jobs\n");
527 return;
530 while (list) {
531 if (strcmp(list->value->type, "stream") == 0) {
532 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
533 " of %" PRId64 " bytes, speed limit %" PRId64
534 " bytes/s\n",
535 list->value->device,
536 list->value->offset,
537 list->value->len,
538 list->value->speed);
539 } else {
540 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
541 " of %" PRId64 " bytes, speed limit %" PRId64
542 " bytes/s\n",
543 list->value->type,
544 list->value->device,
545 list->value->offset,
546 list->value->len,
547 list->value->speed);
549 list = list->next;
553 void hmp_quit(Monitor *mon, const QDict *qdict)
555 monitor_suspend(mon);
556 qmp_quit(NULL);
559 void hmp_stop(Monitor *mon, const QDict *qdict)
561 qmp_stop(NULL);
564 void hmp_system_reset(Monitor *mon, const QDict *qdict)
566 qmp_system_reset(NULL);
569 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
571 qmp_system_powerdown(NULL);
574 void hmp_cpu(Monitor *mon, const QDict *qdict)
576 int64_t cpu_index;
578 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
579 use it are converted to the QAPI */
580 cpu_index = qdict_get_int(qdict, "index");
581 if (monitor_set_cpu(cpu_index) < 0) {
582 monitor_printf(mon, "invalid CPU index\n");
586 void hmp_memsave(Monitor *mon, const QDict *qdict)
588 uint32_t size = qdict_get_int(qdict, "size");
589 const char *filename = qdict_get_str(qdict, "filename");
590 uint64_t addr = qdict_get_int(qdict, "val");
591 Error *errp = NULL;
593 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
594 hmp_handle_error(mon, &errp);
597 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
599 uint32_t size = qdict_get_int(qdict, "size");
600 const char *filename = qdict_get_str(qdict, "filename");
601 uint64_t addr = qdict_get_int(qdict, "val");
602 Error *errp = NULL;
604 qmp_pmemsave(addr, size, filename, &errp);
605 hmp_handle_error(mon, &errp);
608 static void hmp_cont_cb(void *opaque, int err)
610 Monitor *mon = opaque;
612 if (!err) {
613 hmp_cont(mon, NULL);
617 void hmp_cont(Monitor *mon, const QDict *qdict)
619 Error *errp = NULL;
621 qmp_cont(&errp);
622 if (error_is_set(&errp)) {
623 if (error_is_type(errp, QERR_DEVICE_ENCRYPTED)) {
624 const char *device;
626 /* The device is encrypted. Ask the user for the password
627 and retry */
629 device = error_get_field(errp, "device");
630 assert(device != NULL);
632 monitor_read_block_device_key(mon, device, hmp_cont_cb, mon);
633 error_free(errp);
634 return;
636 hmp_handle_error(mon, &errp);
640 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
642 qmp_system_wakeup(NULL);
645 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
647 Error *errp = NULL;
649 qmp_inject_nmi(&errp);
650 hmp_handle_error(mon, &errp);
653 void hmp_set_link(Monitor *mon, const QDict *qdict)
655 const char *name = qdict_get_str(qdict, "name");
656 int up = qdict_get_bool(qdict, "up");
657 Error *errp = NULL;
659 qmp_set_link(name, up, &errp);
660 hmp_handle_error(mon, &errp);
663 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
665 const char *device = qdict_get_str(qdict, "device");
666 const char *password = qdict_get_str(qdict, "password");
667 Error *errp = NULL;
669 qmp_block_passwd(device, password, &errp);
670 hmp_handle_error(mon, &errp);
673 void hmp_balloon(Monitor *mon, const QDict *qdict)
675 int64_t value = qdict_get_int(qdict, "value");
676 Error *errp = NULL;
678 qmp_balloon(value, &errp);
679 if (error_is_set(&errp)) {
680 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
681 error_free(errp);
685 void hmp_block_resize(Monitor *mon, const QDict *qdict)
687 const char *device = qdict_get_str(qdict, "device");
688 int64_t size = qdict_get_int(qdict, "size");
689 Error *errp = NULL;
691 qmp_block_resize(device, size, &errp);
692 hmp_handle_error(mon, &errp);
695 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
697 const char *device = qdict_get_str(qdict, "device");
698 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
699 const char *format = qdict_get_try_str(qdict, "format");
700 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
701 enum NewImageMode mode;
702 Error *errp = NULL;
704 if (!filename) {
705 /* In the future, if 'snapshot-file' is not specified, the snapshot
706 will be taken internally. Today it's actually required. */
707 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
708 hmp_handle_error(mon, &errp);
709 return;
712 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
713 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
714 true, mode, &errp);
715 hmp_handle_error(mon, &errp);
718 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
720 qmp_migrate_cancel(NULL);
723 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
725 double value = qdict_get_double(qdict, "value");
726 qmp_migrate_set_downtime(value, NULL);
729 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
731 int64_t value = qdict_get_int(qdict, "value");
732 qmp_migrate_set_speed(value, NULL);
735 void hmp_set_password(Monitor *mon, const QDict *qdict)
737 const char *protocol = qdict_get_str(qdict, "protocol");
738 const char *password = qdict_get_str(qdict, "password");
739 const char *connected = qdict_get_try_str(qdict, "connected");
740 Error *err = NULL;
742 qmp_set_password(protocol, password, !!connected, connected, &err);
743 hmp_handle_error(mon, &err);
746 void hmp_expire_password(Monitor *mon, const QDict *qdict)
748 const char *protocol = qdict_get_str(qdict, "protocol");
749 const char *whenstr = qdict_get_str(qdict, "time");
750 Error *err = NULL;
752 qmp_expire_password(protocol, whenstr, &err);
753 hmp_handle_error(mon, &err);
756 void hmp_eject(Monitor *mon, const QDict *qdict)
758 int force = qdict_get_try_bool(qdict, "force", 0);
759 const char *device = qdict_get_str(qdict, "device");
760 Error *err = NULL;
762 qmp_eject(device, true, force, &err);
763 hmp_handle_error(mon, &err);
766 static void hmp_change_read_arg(Monitor *mon, const char *password,
767 void *opaque)
769 qmp_change_vnc_password(password, NULL);
770 monitor_read_command(mon, 1);
773 static void cb_hmp_change_bdrv_pwd(Monitor *mon, const char *password,
774 void *opaque)
776 Error *encryption_err = opaque;
777 Error *err = NULL;
778 const char *device;
780 device = error_get_field(encryption_err, "device");
782 qmp_block_passwd(device, password, &err);
783 hmp_handle_error(mon, &err);
784 error_free(encryption_err);
786 monitor_read_command(mon, 1);
789 void hmp_change(Monitor *mon, const QDict *qdict)
791 const char *device = qdict_get_str(qdict, "device");
792 const char *target = qdict_get_str(qdict, "target");
793 const char *arg = qdict_get_try_str(qdict, "arg");
794 Error *err = NULL;
796 if (strcmp(device, "vnc") == 0 &&
797 (strcmp(target, "passwd") == 0 ||
798 strcmp(target, "password") == 0)) {
799 if (!arg) {
800 monitor_read_password(mon, hmp_change_read_arg, NULL);
801 return;
805 qmp_change(device, target, !!arg, arg, &err);
806 if (error_is_type(err, QERR_DEVICE_ENCRYPTED)) {
807 monitor_printf(mon, "%s (%s) is encrypted.\n",
808 error_get_field(err, "device"),
809 error_get_field(err, "filename"));
810 if (!monitor_get_rs(mon)) {
811 monitor_printf(mon,
812 "terminal does not support password prompting\n");
813 error_free(err);
814 return;
816 readline_start(monitor_get_rs(mon), "Password: ", 1,
817 cb_hmp_change_bdrv_pwd, err);
818 return;
820 hmp_handle_error(mon, &err);
823 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
825 Error *err = NULL;
827 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
828 qdict_get_int(qdict, "bps"),
829 qdict_get_int(qdict, "bps_rd"),
830 qdict_get_int(qdict, "bps_wr"),
831 qdict_get_int(qdict, "iops"),
832 qdict_get_int(qdict, "iops_rd"),
833 qdict_get_int(qdict, "iops_wr"), &err);
834 hmp_handle_error(mon, &err);
837 void hmp_block_stream(Monitor *mon, const QDict *qdict)
839 Error *error = NULL;
840 const char *device = qdict_get_str(qdict, "device");
841 const char *base = qdict_get_try_str(qdict, "base");
842 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
844 qmp_block_stream(device, base != NULL, base,
845 qdict_haskey(qdict, "speed"), speed, &error);
847 hmp_handle_error(mon, &error);
850 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
852 Error *error = NULL;
853 const char *device = qdict_get_str(qdict, "device");
854 int64_t value = qdict_get_int(qdict, "speed");
856 qmp_block_job_set_speed(device, value, &error);
858 hmp_handle_error(mon, &error);
861 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
863 Error *error = NULL;
864 const char *device = qdict_get_str(qdict, "device");
866 qmp_block_job_cancel(device, &error);
868 hmp_handle_error(mon, &error);
871 typedef struct MigrationStatus
873 QEMUTimer *timer;
874 Monitor *mon;
875 bool is_block_migration;
876 } MigrationStatus;
878 static void hmp_migrate_status_cb(void *opaque)
880 MigrationStatus *status = opaque;
881 MigrationInfo *info;
883 info = qmp_query_migrate(NULL);
884 if (!info->has_status || strcmp(info->status, "active") == 0) {
885 if (info->has_disk) {
886 int progress;
888 if (info->disk->remaining) {
889 progress = info->disk->transferred * 100 / info->disk->total;
890 } else {
891 progress = 100;
894 monitor_printf(status->mon, "Completed %d %%\r", progress);
895 monitor_flush(status->mon);
898 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
899 } else {
900 if (status->is_block_migration) {
901 monitor_printf(status->mon, "\n");
903 monitor_resume(status->mon);
904 qemu_del_timer(status->timer);
905 g_free(status);
908 qapi_free_MigrationInfo(info);
911 void hmp_migrate(Monitor *mon, const QDict *qdict)
913 int detach = qdict_get_try_bool(qdict, "detach", 0);
914 int blk = qdict_get_try_bool(qdict, "blk", 0);
915 int inc = qdict_get_try_bool(qdict, "inc", 0);
916 const char *uri = qdict_get_str(qdict, "uri");
917 Error *err = NULL;
919 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
920 if (err) {
921 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
922 error_free(err);
923 return;
926 if (!detach) {
927 MigrationStatus *status;
929 if (monitor_suspend(mon) < 0) {
930 monitor_printf(mon, "terminal does not allow synchronous "
931 "migration, continuing detached\n");
932 return;
935 status = g_malloc0(sizeof(*status));
936 status->mon = mon;
937 status->is_block_migration = blk || inc;
938 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
939 status);
940 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
944 void hmp_device_del(Monitor *mon, const QDict *qdict)
946 const char *id = qdict_get_str(qdict, "id");
947 Error *err = NULL;
949 qmp_device_del(id, &err);
950 hmp_handle_error(mon, &err);
953 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
955 Error *errp = NULL;
956 int paging = qdict_get_try_bool(qdict, "paging", 0);
957 const char *file = qdict_get_str(qdict, "protocol");
958 bool has_begin = qdict_haskey(qdict, "begin");
959 bool has_length = qdict_haskey(qdict, "length");
960 int64_t begin = 0;
961 int64_t length = 0;
963 if (has_begin) {
964 begin = qdict_get_int(qdict, "begin");
966 if (has_length) {
967 length = qdict_get_int(qdict, "length");
970 qmp_dump_guest_memory(paging, file, has_begin, begin, has_length, length,
971 &errp);
972 hmp_handle_error(mon, &errp);
975 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
977 Error *err = NULL;
978 QemuOpts *opts;
980 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
981 if (error_is_set(&err)) {
982 goto out;
985 netdev_add(opts, &err);
986 if (error_is_set(&err)) {
987 qemu_opts_del(opts);
990 out:
991 hmp_handle_error(mon, &err);
994 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
996 const char *id = qdict_get_str(qdict, "id");
997 Error *err = NULL;
999 qmp_netdev_del(id, &err);
1000 hmp_handle_error(mon, &err);