buffer_is_zero: use vector optimizations if possible
[qemu-kvm.git] / hmp.c
blobb0a861cfbb0eba7c93b8698a1a53da30cd73b9ff
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/net.h"
18 #include "char/char.h"
19 #include "qemu/option.h"
20 #include "qemu/timer.h"
21 #include "qmp-commands.h"
22 #include "qemu/sockets.h"
23 #include "monitor/monitor.h"
24 #include "ui/console.h"
26 static void hmp_handle_error(Monitor *mon, Error **errp)
28 if (error_is_set(errp)) {
29 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
30 error_free(*errp);
34 void hmp_info_name(Monitor *mon, const QDict *qdict)
36 NameInfo *info;
38 info = qmp_query_name(NULL);
39 if (info->has_name) {
40 monitor_printf(mon, "%s\n", info->name);
42 qapi_free_NameInfo(info);
45 void hmp_info_version(Monitor *mon, const QDict *qdict)
47 VersionInfo *info;
49 info = qmp_query_version(NULL);
51 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
52 info->qemu.major, info->qemu.minor, info->qemu.micro,
53 info->package);
55 qapi_free_VersionInfo(info);
58 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
60 KvmInfo *info;
62 info = qmp_query_kvm(NULL);
63 monitor_printf(mon, "kvm support: ");
64 if (info->present) {
65 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
66 } else {
67 monitor_printf(mon, "not compiled\n");
70 qapi_free_KvmInfo(info);
73 void hmp_info_status(Monitor *mon, const QDict *qdict)
75 StatusInfo *info;
77 info = qmp_query_status(NULL);
79 monitor_printf(mon, "VM status: %s%s",
80 info->running ? "running" : "paused",
81 info->singlestep ? " (single step mode)" : "");
83 if (!info->running && info->status != RUN_STATE_PAUSED) {
84 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
87 monitor_printf(mon, "\n");
89 qapi_free_StatusInfo(info);
92 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
94 UuidInfo *info;
96 info = qmp_query_uuid(NULL);
97 monitor_printf(mon, "%s\n", info->UUID);
98 qapi_free_UuidInfo(info);
101 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
103 ChardevInfoList *char_info, *info;
105 char_info = qmp_query_chardev(NULL);
106 for (info = char_info; info; info = info->next) {
107 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
108 info->value->filename);
111 qapi_free_ChardevInfoList(char_info);
114 void hmp_info_mice(Monitor *mon, const QDict *qdict)
116 MouseInfoList *mice_list, *mouse;
118 mice_list = qmp_query_mice(NULL);
119 if (!mice_list) {
120 monitor_printf(mon, "No mouse devices connected\n");
121 return;
124 for (mouse = mice_list; mouse; mouse = mouse->next) {
125 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
126 mouse->value->current ? '*' : ' ',
127 mouse->value->index, mouse->value->name,
128 mouse->value->absolute ? " (absolute)" : "");
131 qapi_free_MouseInfoList(mice_list);
134 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
136 MigrationInfo *info;
137 MigrationCapabilityStatusList *caps, *cap;
139 info = qmp_query_migrate(NULL);
140 caps = qmp_query_migrate_capabilities(NULL);
142 /* do not display parameters during setup */
143 if (info->has_status && caps) {
144 monitor_printf(mon, "capabilities: ");
145 for (cap = caps; cap; cap = cap->next) {
146 monitor_printf(mon, "%s: %s ",
147 MigrationCapability_lookup[cap->value->capability],
148 cap->value->state ? "on" : "off");
150 monitor_printf(mon, "\n");
153 if (info->has_status) {
154 monitor_printf(mon, "Migration status: %s\n", info->status);
155 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
156 info->total_time);
157 if (info->has_expected_downtime) {
158 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
159 info->expected_downtime);
161 if (info->has_downtime) {
162 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
163 info->downtime);
167 if (info->has_ram) {
168 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
169 info->ram->transferred >> 10);
170 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
171 info->ram->remaining >> 10);
172 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
173 info->ram->total >> 10);
174 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
175 info->ram->duplicate);
176 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
177 info->ram->normal);
178 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
179 info->ram->normal_bytes >> 10);
180 if (info->ram->dirty_pages_rate) {
181 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
182 info->ram->dirty_pages_rate);
186 if (info->has_disk) {
187 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
188 info->disk->transferred >> 10);
189 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
190 info->disk->remaining >> 10);
191 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
192 info->disk->total >> 10);
195 if (info->has_xbzrle_cache) {
196 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
197 info->xbzrle_cache->cache_size);
198 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
199 info->xbzrle_cache->bytes >> 10);
200 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
201 info->xbzrle_cache->pages);
202 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
203 info->xbzrle_cache->cache_miss);
204 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
205 info->xbzrle_cache->overflow);
208 qapi_free_MigrationInfo(info);
209 qapi_free_MigrationCapabilityStatusList(caps);
212 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
214 MigrationCapabilityStatusList *caps, *cap;
216 caps = qmp_query_migrate_capabilities(NULL);
218 if (caps) {
219 monitor_printf(mon, "capabilities: ");
220 for (cap = caps; cap; cap = cap->next) {
221 monitor_printf(mon, "%s: %s ",
222 MigrationCapability_lookup[cap->value->capability],
223 cap->value->state ? "on" : "off");
225 monitor_printf(mon, "\n");
228 qapi_free_MigrationCapabilityStatusList(caps);
231 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
233 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
234 qmp_query_migrate_cache_size(NULL) >> 10);
237 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
239 CpuInfoList *cpu_list, *cpu;
241 cpu_list = qmp_query_cpus(NULL);
243 for (cpu = cpu_list; cpu; cpu = cpu->next) {
244 int active = ' ';
246 if (cpu->value->CPU == monitor_get_cpu_index()) {
247 active = '*';
250 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
252 if (cpu->value->has_pc) {
253 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
255 if (cpu->value->has_nip) {
256 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
258 if (cpu->value->has_npc) {
259 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
261 if (cpu->value->has_PC) {
262 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
265 if (cpu->value->halted) {
266 monitor_printf(mon, " (halted)");
269 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
272 qapi_free_CpuInfoList(cpu_list);
275 void hmp_info_block(Monitor *mon, const QDict *qdict)
277 BlockInfoList *block_list, *info;
279 block_list = qmp_query_block(NULL);
281 for (info = block_list; info; info = info->next) {
282 monitor_printf(mon, "%s: removable=%d",
283 info->value->device, info->value->removable);
285 if (info->value->removable) {
286 monitor_printf(mon, " locked=%d", info->value->locked);
287 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
290 if (info->value->has_io_status) {
291 monitor_printf(mon, " io-status=%s",
292 BlockDeviceIoStatus_lookup[info->value->io_status]);
295 if (info->value->has_inserted) {
296 monitor_printf(mon, " file=");
297 monitor_print_filename(mon, info->value->inserted->file);
299 if (info->value->inserted->has_backing_file) {
300 monitor_printf(mon, " backing_file=");
301 monitor_print_filename(mon, info->value->inserted->backing_file);
302 monitor_printf(mon, " backing_file_depth=%" PRId64,
303 info->value->inserted->backing_file_depth);
305 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
306 info->value->inserted->ro,
307 info->value->inserted->drv,
308 info->value->inserted->encrypted);
310 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
311 " bps_wr=%" PRId64 " iops=%" PRId64
312 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
313 info->value->inserted->bps,
314 info->value->inserted->bps_rd,
315 info->value->inserted->bps_wr,
316 info->value->inserted->iops,
317 info->value->inserted->iops_rd,
318 info->value->inserted->iops_wr);
319 } else {
320 monitor_printf(mon, " [not inserted]");
323 monitor_printf(mon, "\n");
326 qapi_free_BlockInfoList(block_list);
329 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
331 BlockStatsList *stats_list, *stats;
333 stats_list = qmp_query_blockstats(NULL);
335 for (stats = stats_list; stats; stats = stats->next) {
336 if (!stats->value->has_device) {
337 continue;
340 monitor_printf(mon, "%s:", stats->value->device);
341 monitor_printf(mon, " rd_bytes=%" PRId64
342 " wr_bytes=%" PRId64
343 " rd_operations=%" PRId64
344 " wr_operations=%" PRId64
345 " flush_operations=%" PRId64
346 " wr_total_time_ns=%" PRId64
347 " rd_total_time_ns=%" PRId64
348 " flush_total_time_ns=%" PRId64
349 "\n",
350 stats->value->stats->rd_bytes,
351 stats->value->stats->wr_bytes,
352 stats->value->stats->rd_operations,
353 stats->value->stats->wr_operations,
354 stats->value->stats->flush_operations,
355 stats->value->stats->wr_total_time_ns,
356 stats->value->stats->rd_total_time_ns,
357 stats->value->stats->flush_total_time_ns);
360 qapi_free_BlockStatsList(stats_list);
363 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
365 VncInfo *info;
366 Error *err = NULL;
367 VncClientInfoList *client;
369 info = qmp_query_vnc(&err);
370 if (err) {
371 monitor_printf(mon, "%s\n", error_get_pretty(err));
372 error_free(err);
373 return;
376 if (!info->enabled) {
377 monitor_printf(mon, "Server: disabled\n");
378 goto out;
381 monitor_printf(mon, "Server:\n");
382 if (info->has_host && info->has_service) {
383 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
385 if (info->has_auth) {
386 monitor_printf(mon, " auth: %s\n", info->auth);
389 if (!info->has_clients || info->clients == NULL) {
390 monitor_printf(mon, "Client: none\n");
391 } else {
392 for (client = info->clients; client; client = client->next) {
393 monitor_printf(mon, "Client:\n");
394 monitor_printf(mon, " address: %s:%s\n",
395 client->value->host, client->value->service);
396 monitor_printf(mon, " x509_dname: %s\n",
397 client->value->x509_dname ?
398 client->value->x509_dname : "none");
399 monitor_printf(mon, " username: %s\n",
400 client->value->has_sasl_username ?
401 client->value->sasl_username : "none");
405 out:
406 qapi_free_VncInfo(info);
409 void hmp_info_spice(Monitor *mon, const QDict *qdict)
411 SpiceChannelList *chan;
412 SpiceInfo *info;
414 info = qmp_query_spice(NULL);
416 if (!info->enabled) {
417 monitor_printf(mon, "Server: disabled\n");
418 goto out;
421 monitor_printf(mon, "Server:\n");
422 if (info->has_port) {
423 monitor_printf(mon, " address: %s:%" PRId64 "\n",
424 info->host, info->port);
426 if (info->has_tls_port) {
427 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
428 info->host, info->tls_port);
430 monitor_printf(mon, " migrated: %s\n",
431 info->migrated ? "true" : "false");
432 monitor_printf(mon, " auth: %s\n", info->auth);
433 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
434 monitor_printf(mon, " mouse-mode: %s\n",
435 SpiceQueryMouseMode_lookup[info->mouse_mode]);
437 if (!info->has_channels || info->channels == NULL) {
438 monitor_printf(mon, "Channels: none\n");
439 } else {
440 for (chan = info->channels; chan; chan = chan->next) {
441 monitor_printf(mon, "Channel:\n");
442 monitor_printf(mon, " address: %s:%s%s\n",
443 chan->value->host, chan->value->port,
444 chan->value->tls ? " [tls]" : "");
445 monitor_printf(mon, " session: %" PRId64 "\n",
446 chan->value->connection_id);
447 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
448 chan->value->channel_type, chan->value->channel_id);
452 out:
453 qapi_free_SpiceInfo(info);
456 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
458 BalloonInfo *info;
459 Error *err = NULL;
461 info = qmp_query_balloon(&err);
462 if (err) {
463 monitor_printf(mon, "%s\n", error_get_pretty(err));
464 error_free(err);
465 return;
468 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
470 qapi_free_BalloonInfo(info);
473 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
475 PciMemoryRegionList *region;
477 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
478 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
479 dev->slot, dev->function);
480 monitor_printf(mon, " ");
482 if (dev->class_info.has_desc) {
483 monitor_printf(mon, "%s", dev->class_info.desc);
484 } else {
485 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
488 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
489 dev->id.vendor, dev->id.device);
491 if (dev->has_irq) {
492 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
495 if (dev->has_pci_bridge) {
496 monitor_printf(mon, " BUS %" PRId64 ".\n",
497 dev->pci_bridge->bus.number);
498 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
499 dev->pci_bridge->bus.secondary);
500 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
501 dev->pci_bridge->bus.subordinate);
503 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
504 dev->pci_bridge->bus.io_range->base,
505 dev->pci_bridge->bus.io_range->limit);
507 monitor_printf(mon,
508 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
509 dev->pci_bridge->bus.memory_range->base,
510 dev->pci_bridge->bus.memory_range->limit);
512 monitor_printf(mon, " prefetchable memory range "
513 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
514 dev->pci_bridge->bus.prefetchable_range->base,
515 dev->pci_bridge->bus.prefetchable_range->limit);
518 for (region = dev->regions; region; region = region->next) {
519 uint64_t addr, size;
521 addr = region->value->address;
522 size = region->value->size;
524 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
526 if (!strcmp(region->value->type, "io")) {
527 monitor_printf(mon, "I/O at 0x%04" PRIx64
528 " [0x%04" PRIx64 "].\n",
529 addr, addr + size - 1);
530 } else {
531 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
532 " [0x%08" PRIx64 "].\n",
533 region->value->mem_type_64 ? 64 : 32,
534 region->value->prefetch ? " prefetchable" : "",
535 addr, addr + size - 1);
539 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
541 if (dev->has_pci_bridge) {
542 if (dev->pci_bridge->has_devices) {
543 PciDeviceInfoList *cdev;
544 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
545 hmp_info_pci_device(mon, cdev->value);
551 void hmp_info_pci(Monitor *mon, const QDict *qdict)
553 PciInfoList *info_list, *info;
554 Error *err = NULL;
556 info_list = qmp_query_pci(&err);
557 if (err) {
558 monitor_printf(mon, "PCI devices not supported\n");
559 error_free(err);
560 return;
563 for (info = info_list; info; info = info->next) {
564 PciDeviceInfoList *dev;
566 for (dev = info->value->devices; dev; dev = dev->next) {
567 hmp_info_pci_device(mon, dev->value);
571 qapi_free_PciInfoList(info_list);
574 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
576 BlockJobInfoList *list;
577 Error *err = NULL;
579 list = qmp_query_block_jobs(&err);
580 assert(!err);
582 if (!list) {
583 monitor_printf(mon, "No active jobs\n");
584 return;
587 while (list) {
588 if (strcmp(list->value->type, "stream") == 0) {
589 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
590 " of %" PRId64 " bytes, speed limit %" PRId64
591 " bytes/s\n",
592 list->value->device,
593 list->value->offset,
594 list->value->len,
595 list->value->speed);
596 } else {
597 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
598 " of %" PRId64 " bytes, speed limit %" PRId64
599 " bytes/s\n",
600 list->value->type,
601 list->value->device,
602 list->value->offset,
603 list->value->len,
604 list->value->speed);
606 list = list->next;
610 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
612 TPMInfoList *info_list, *info;
613 Error *err = NULL;
614 unsigned int c = 0;
615 TPMPassthroughOptions *tpo;
617 info_list = qmp_query_tpm(&err);
618 if (err) {
619 monitor_printf(mon, "TPM device not supported\n");
620 error_free(err);
621 return;
624 if (info_list) {
625 monitor_printf(mon, "TPM device:\n");
628 for (info = info_list; info; info = info->next) {
629 TPMInfo *ti = info->value;
630 monitor_printf(mon, " tpm%d: model=%s\n",
631 c, TpmModel_lookup[ti->model]);
633 monitor_printf(mon, " \\ %s: type=%s",
634 ti->id, TpmType_lookup[ti->type]);
636 switch (ti->tpm_options->kind) {
637 case TPM_TYPE_OPTIONS_KIND_TPM_PASSTHROUGH_OPTIONS:
638 tpo = ti->tpm_options->tpm_passthrough_options;
639 monitor_printf(mon, "%s%s%s%s",
640 tpo->has_path ? ",path=" : "",
641 tpo->has_path ? tpo->path : "",
642 tpo->has_cancel_path ? ",cancel-path=" : "",
643 tpo->has_cancel_path ? tpo->cancel_path : "");
644 break;
645 case TPM_TYPE_OPTIONS_KIND_MAX:
646 break;
648 monitor_printf(mon, "\n");
649 c++;
651 qapi_free_TPMInfoList(info_list);
654 void hmp_quit(Monitor *mon, const QDict *qdict)
656 monitor_suspend(mon);
657 qmp_quit(NULL);
660 void hmp_stop(Monitor *mon, const QDict *qdict)
662 qmp_stop(NULL);
665 void hmp_system_reset(Monitor *mon, const QDict *qdict)
667 qmp_system_reset(NULL);
670 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
672 qmp_system_powerdown(NULL);
675 void hmp_cpu(Monitor *mon, const QDict *qdict)
677 int64_t cpu_index;
679 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
680 use it are converted to the QAPI */
681 cpu_index = qdict_get_int(qdict, "index");
682 if (monitor_set_cpu(cpu_index) < 0) {
683 monitor_printf(mon, "invalid CPU index\n");
687 void hmp_memsave(Monitor *mon, const QDict *qdict)
689 uint32_t size = qdict_get_int(qdict, "size");
690 const char *filename = qdict_get_str(qdict, "filename");
691 uint64_t addr = qdict_get_int(qdict, "val");
692 Error *errp = NULL;
694 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
695 hmp_handle_error(mon, &errp);
698 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
700 uint32_t size = qdict_get_int(qdict, "size");
701 const char *filename = qdict_get_str(qdict, "filename");
702 uint64_t addr = qdict_get_int(qdict, "val");
703 Error *errp = NULL;
705 qmp_pmemsave(addr, size, filename, &errp);
706 hmp_handle_error(mon, &errp);
709 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
711 const char *chardev = qdict_get_str(qdict, "device");
712 const char *data = qdict_get_str(qdict, "data");
713 Error *errp = NULL;
715 qmp_ringbuf_write(chardev, data, false, 0, &errp);
717 hmp_handle_error(mon, &errp);
720 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
722 uint32_t size = qdict_get_int(qdict, "size");
723 const char *chardev = qdict_get_str(qdict, "device");
724 char *data;
725 Error *errp = NULL;
726 int i;
728 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
729 if (errp) {
730 monitor_printf(mon, "%s\n", error_get_pretty(errp));
731 error_free(errp);
732 return;
735 for (i = 0; data[i]; i++) {
736 unsigned char ch = data[i];
738 if (ch == '\\') {
739 monitor_printf(mon, "\\\\");
740 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
741 monitor_printf(mon, "\\u%04X", ch);
742 } else {
743 monitor_printf(mon, "%c", ch);
747 monitor_printf(mon, "\n");
748 g_free(data);
751 static void hmp_cont_cb(void *opaque, int err)
753 if (!err) {
754 qmp_cont(NULL);
758 static bool key_is_missing(const BlockInfo *bdev)
760 return (bdev->inserted && bdev->inserted->encryption_key_missing);
763 void hmp_cont(Monitor *mon, const QDict *qdict)
765 BlockInfoList *bdev_list, *bdev;
766 Error *errp = NULL;
768 bdev_list = qmp_query_block(NULL);
769 for (bdev = bdev_list; bdev; bdev = bdev->next) {
770 if (key_is_missing(bdev->value)) {
771 monitor_read_block_device_key(mon, bdev->value->device,
772 hmp_cont_cb, NULL);
773 goto out;
777 qmp_cont(&errp);
778 hmp_handle_error(mon, &errp);
780 out:
781 qapi_free_BlockInfoList(bdev_list);
784 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
786 qmp_system_wakeup(NULL);
789 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
791 Error *errp = NULL;
793 qmp_inject_nmi(&errp);
794 hmp_handle_error(mon, &errp);
797 void hmp_set_link(Monitor *mon, const QDict *qdict)
799 const char *name = qdict_get_str(qdict, "name");
800 int up = qdict_get_bool(qdict, "up");
801 Error *errp = NULL;
803 qmp_set_link(name, up, &errp);
804 hmp_handle_error(mon, &errp);
807 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
809 const char *device = qdict_get_str(qdict, "device");
810 const char *password = qdict_get_str(qdict, "password");
811 Error *errp = NULL;
813 qmp_block_passwd(device, password, &errp);
814 hmp_handle_error(mon, &errp);
817 void hmp_balloon(Monitor *mon, const QDict *qdict)
819 int64_t value = qdict_get_int(qdict, "value");
820 Error *errp = NULL;
822 qmp_balloon(value, &errp);
823 if (error_is_set(&errp)) {
824 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
825 error_free(errp);
829 void hmp_block_resize(Monitor *mon, const QDict *qdict)
831 const char *device = qdict_get_str(qdict, "device");
832 int64_t size = qdict_get_int(qdict, "size");
833 Error *errp = NULL;
835 qmp_block_resize(device, size, &errp);
836 hmp_handle_error(mon, &errp);
839 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
841 const char *device = qdict_get_str(qdict, "device");
842 const char *filename = qdict_get_str(qdict, "target");
843 const char *format = qdict_get_try_str(qdict, "format");
844 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
845 int full = qdict_get_try_bool(qdict, "full", 0);
846 enum NewImageMode mode;
847 Error *errp = NULL;
849 if (!filename) {
850 error_set(&errp, QERR_MISSING_PARAMETER, "target");
851 hmp_handle_error(mon, &errp);
852 return;
855 if (reuse) {
856 mode = NEW_IMAGE_MODE_EXISTING;
857 } else {
858 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
861 qmp_drive_mirror(device, filename, !!format, format,
862 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
863 true, mode, false, 0, false, 0, false, 0,
864 false, 0, false, 0, &errp);
865 hmp_handle_error(mon, &errp);
868 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
870 const char *device = qdict_get_str(qdict, "device");
871 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
872 const char *format = qdict_get_try_str(qdict, "format");
873 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
874 enum NewImageMode mode;
875 Error *errp = NULL;
877 if (!filename) {
878 /* In the future, if 'snapshot-file' is not specified, the snapshot
879 will be taken internally. Today it's actually required. */
880 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
881 hmp_handle_error(mon, &errp);
882 return;
885 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
886 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
887 true, mode, &errp);
888 hmp_handle_error(mon, &errp);
891 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
893 qmp_migrate_cancel(NULL);
896 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
898 double value = qdict_get_double(qdict, "value");
899 qmp_migrate_set_downtime(value, NULL);
902 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
904 int64_t value = qdict_get_int(qdict, "value");
905 Error *err = NULL;
907 qmp_migrate_set_cache_size(value, &err);
908 if (err) {
909 monitor_printf(mon, "%s\n", error_get_pretty(err));
910 error_free(err);
911 return;
915 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
917 int64_t value = qdict_get_int(qdict, "value");
918 qmp_migrate_set_speed(value, NULL);
921 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
923 const char *cap = qdict_get_str(qdict, "capability");
924 bool state = qdict_get_bool(qdict, "state");
925 Error *err = NULL;
926 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
927 int i;
929 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
930 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
931 caps->value = g_malloc0(sizeof(*caps->value));
932 caps->value->capability = i;
933 caps->value->state = state;
934 caps->next = NULL;
935 qmp_migrate_set_capabilities(caps, &err);
936 break;
940 if (i == MIGRATION_CAPABILITY_MAX) {
941 error_set(&err, QERR_INVALID_PARAMETER, cap);
944 qapi_free_MigrationCapabilityStatusList(caps);
946 if (err) {
947 monitor_printf(mon, "migrate_set_capability: %s\n",
948 error_get_pretty(err));
949 error_free(err);
953 void hmp_set_password(Monitor *mon, const QDict *qdict)
955 const char *protocol = qdict_get_str(qdict, "protocol");
956 const char *password = qdict_get_str(qdict, "password");
957 const char *connected = qdict_get_try_str(qdict, "connected");
958 Error *err = NULL;
960 qmp_set_password(protocol, password, !!connected, connected, &err);
961 hmp_handle_error(mon, &err);
964 void hmp_expire_password(Monitor *mon, const QDict *qdict)
966 const char *protocol = qdict_get_str(qdict, "protocol");
967 const char *whenstr = qdict_get_str(qdict, "time");
968 Error *err = NULL;
970 qmp_expire_password(protocol, whenstr, &err);
971 hmp_handle_error(mon, &err);
974 void hmp_eject(Monitor *mon, const QDict *qdict)
976 int force = qdict_get_try_bool(qdict, "force", 0);
977 const char *device = qdict_get_str(qdict, "device");
978 Error *err = NULL;
980 qmp_eject(device, true, force, &err);
981 hmp_handle_error(mon, &err);
984 static void hmp_change_read_arg(Monitor *mon, const char *password,
985 void *opaque)
987 qmp_change_vnc_password(password, NULL);
988 monitor_read_command(mon, 1);
991 void hmp_change(Monitor *mon, const QDict *qdict)
993 const char *device = qdict_get_str(qdict, "device");
994 const char *target = qdict_get_str(qdict, "target");
995 const char *arg = qdict_get_try_str(qdict, "arg");
996 Error *err = NULL;
998 if (strcmp(device, "vnc") == 0 &&
999 (strcmp(target, "passwd") == 0 ||
1000 strcmp(target, "password") == 0)) {
1001 if (!arg) {
1002 monitor_read_password(mon, hmp_change_read_arg, NULL);
1003 return;
1007 qmp_change(device, target, !!arg, arg, &err);
1008 if (error_is_set(&err) &&
1009 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1010 error_free(err);
1011 monitor_read_block_device_key(mon, device, NULL, NULL);
1012 return;
1014 hmp_handle_error(mon, &err);
1017 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1019 Error *err = NULL;
1021 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1022 qdict_get_int(qdict, "bps"),
1023 qdict_get_int(qdict, "bps_rd"),
1024 qdict_get_int(qdict, "bps_wr"),
1025 qdict_get_int(qdict, "iops"),
1026 qdict_get_int(qdict, "iops_rd"),
1027 qdict_get_int(qdict, "iops_wr"), &err);
1028 hmp_handle_error(mon, &err);
1031 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1033 Error *error = NULL;
1034 const char *device = qdict_get_str(qdict, "device");
1035 const char *base = qdict_get_try_str(qdict, "base");
1036 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1038 qmp_block_stream(device, base != NULL, base,
1039 qdict_haskey(qdict, "speed"), speed,
1040 BLOCKDEV_ON_ERROR_REPORT, true, &error);
1042 hmp_handle_error(mon, &error);
1045 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1047 Error *error = NULL;
1048 const char *device = qdict_get_str(qdict, "device");
1049 int64_t value = qdict_get_int(qdict, "speed");
1051 qmp_block_job_set_speed(device, value, &error);
1053 hmp_handle_error(mon, &error);
1056 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1058 Error *error = NULL;
1059 const char *device = qdict_get_str(qdict, "device");
1060 bool force = qdict_get_try_bool(qdict, "force", 0);
1062 qmp_block_job_cancel(device, true, force, &error);
1064 hmp_handle_error(mon, &error);
1067 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1069 Error *error = NULL;
1070 const char *device = qdict_get_str(qdict, "device");
1072 qmp_block_job_pause(device, &error);
1074 hmp_handle_error(mon, &error);
1077 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1079 Error *error = NULL;
1080 const char *device = qdict_get_str(qdict, "device");
1082 qmp_block_job_resume(device, &error);
1084 hmp_handle_error(mon, &error);
1087 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1089 Error *error = NULL;
1090 const char *device = qdict_get_str(qdict, "device");
1092 qmp_block_job_complete(device, &error);
1094 hmp_handle_error(mon, &error);
1097 typedef struct MigrationStatus
1099 QEMUTimer *timer;
1100 Monitor *mon;
1101 bool is_block_migration;
1102 } MigrationStatus;
1104 static void hmp_migrate_status_cb(void *opaque)
1106 MigrationStatus *status = opaque;
1107 MigrationInfo *info;
1109 info = qmp_query_migrate(NULL);
1110 if (!info->has_status || strcmp(info->status, "active") == 0) {
1111 if (info->has_disk) {
1112 int progress;
1114 if (info->disk->remaining) {
1115 progress = info->disk->transferred * 100 / info->disk->total;
1116 } else {
1117 progress = 100;
1120 monitor_printf(status->mon, "Completed %d %%\r", progress);
1121 monitor_flush(status->mon);
1124 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1125 } else {
1126 if (status->is_block_migration) {
1127 monitor_printf(status->mon, "\n");
1129 monitor_resume(status->mon);
1130 qemu_del_timer(status->timer);
1131 g_free(status);
1134 qapi_free_MigrationInfo(info);
1137 void hmp_migrate(Monitor *mon, const QDict *qdict)
1139 int detach = qdict_get_try_bool(qdict, "detach", 0);
1140 int blk = qdict_get_try_bool(qdict, "blk", 0);
1141 int inc = qdict_get_try_bool(qdict, "inc", 0);
1142 const char *uri = qdict_get_str(qdict, "uri");
1143 Error *err = NULL;
1145 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1146 if (err) {
1147 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1148 error_free(err);
1149 return;
1152 if (!detach) {
1153 MigrationStatus *status;
1155 if (monitor_suspend(mon) < 0) {
1156 monitor_printf(mon, "terminal does not allow synchronous "
1157 "migration, continuing detached\n");
1158 return;
1161 status = g_malloc0(sizeof(*status));
1162 status->mon = mon;
1163 status->is_block_migration = blk || inc;
1164 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1165 status);
1166 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1170 void hmp_device_del(Monitor *mon, const QDict *qdict)
1172 const char *id = qdict_get_str(qdict, "id");
1173 Error *err = NULL;
1175 qmp_device_del(id, &err);
1176 hmp_handle_error(mon, &err);
1179 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1181 Error *errp = NULL;
1182 int paging = qdict_get_try_bool(qdict, "paging", 0);
1183 const char *file = qdict_get_str(qdict, "filename");
1184 bool has_begin = qdict_haskey(qdict, "begin");
1185 bool has_length = qdict_haskey(qdict, "length");
1186 int64_t begin = 0;
1187 int64_t length = 0;
1188 char *prot;
1190 if (has_begin) {
1191 begin = qdict_get_int(qdict, "begin");
1193 if (has_length) {
1194 length = qdict_get_int(qdict, "length");
1197 prot = g_strconcat("file:", file, NULL);
1199 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1200 &errp);
1201 hmp_handle_error(mon, &errp);
1202 g_free(prot);
1205 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1207 Error *err = NULL;
1208 QemuOpts *opts;
1210 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1211 if (error_is_set(&err)) {
1212 goto out;
1215 netdev_add(opts, &err);
1216 if (error_is_set(&err)) {
1217 qemu_opts_del(opts);
1220 out:
1221 hmp_handle_error(mon, &err);
1224 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1226 const char *id = qdict_get_str(qdict, "id");
1227 Error *err = NULL;
1229 qmp_netdev_del(id, &err);
1230 hmp_handle_error(mon, &err);
1233 void hmp_getfd(Monitor *mon, const QDict *qdict)
1235 const char *fdname = qdict_get_str(qdict, "fdname");
1236 Error *errp = NULL;
1238 qmp_getfd(fdname, &errp);
1239 hmp_handle_error(mon, &errp);
1242 void hmp_closefd(Monitor *mon, const QDict *qdict)
1244 const char *fdname = qdict_get_str(qdict, "fdname");
1245 Error *errp = NULL;
1247 qmp_closefd(fdname, &errp);
1248 hmp_handle_error(mon, &errp);
1251 void hmp_send_key(Monitor *mon, const QDict *qdict)
1253 const char *keys = qdict_get_str(qdict, "keys");
1254 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1255 int has_hold_time = qdict_haskey(qdict, "hold-time");
1256 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1257 Error *err = NULL;
1258 char keyname_buf[16];
1259 char *separator;
1260 int keyname_len;
1262 while (1) {
1263 separator = strchr(keys, '-');
1264 keyname_len = separator ? separator - keys : strlen(keys);
1265 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1267 /* Be compatible with old interface, convert user inputted "<" */
1268 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1269 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1270 keyname_len = 4;
1272 keyname_buf[keyname_len] = 0;
1274 keylist = g_malloc0(sizeof(*keylist));
1275 keylist->value = g_malloc0(sizeof(*keylist->value));
1277 if (!head) {
1278 head = keylist;
1280 if (tmp) {
1281 tmp->next = keylist;
1283 tmp = keylist;
1285 if (strstart(keyname_buf, "0x", NULL)) {
1286 char *endp;
1287 int value = strtoul(keyname_buf, &endp, 0);
1288 if (*endp != '\0') {
1289 goto err_out;
1291 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1292 keylist->value->number = value;
1293 } else {
1294 int idx = index_from_key(keyname_buf);
1295 if (idx == Q_KEY_CODE_MAX) {
1296 goto err_out;
1298 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1299 keylist->value->qcode = idx;
1302 if (!separator) {
1303 break;
1305 keys = separator + 1;
1308 qmp_send_key(head, has_hold_time, hold_time, &err);
1309 hmp_handle_error(mon, &err);
1311 out:
1312 qapi_free_KeyValueList(head);
1313 return;
1315 err_out:
1316 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1317 goto out;
1320 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1322 const char *filename = qdict_get_str(qdict, "filename");
1323 Error *err = NULL;
1325 qmp_screendump(filename, &err);
1326 hmp_handle_error(mon, &err);
1329 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1331 const char *uri = qdict_get_str(qdict, "uri");
1332 int writable = qdict_get_try_bool(qdict, "writable", 0);
1333 int all = qdict_get_try_bool(qdict, "all", 0);
1334 Error *local_err = NULL;
1335 BlockInfoList *block_list, *info;
1336 SocketAddress *addr;
1338 if (writable && !all) {
1339 error_setg(&local_err, "-w only valid together with -a");
1340 goto exit;
1343 /* First check if the address is valid and start the server. */
1344 addr = socket_parse(uri, &local_err);
1345 if (local_err != NULL) {
1346 goto exit;
1349 qmp_nbd_server_start(addr, &local_err);
1350 qapi_free_SocketAddress(addr);
1351 if (local_err != NULL) {
1352 goto exit;
1355 if (!all) {
1356 return;
1359 /* Then try adding all block devices. If one fails, close all and
1360 * exit.
1362 block_list = qmp_query_block(NULL);
1364 for (info = block_list; info; info = info->next) {
1365 if (!info->value->has_inserted) {
1366 continue;
1369 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1371 if (local_err != NULL) {
1372 qmp_nbd_server_stop(NULL);
1373 break;
1377 qapi_free_BlockInfoList(block_list);
1379 exit:
1380 hmp_handle_error(mon, &local_err);
1383 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1385 const char *device = qdict_get_str(qdict, "device");
1386 int writable = qdict_get_try_bool(qdict, "writable", 0);
1387 Error *local_err = NULL;
1389 qmp_nbd_server_add(device, true, writable, &local_err);
1391 if (local_err != NULL) {
1392 hmp_handle_error(mon, &local_err);
1396 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1398 Error *errp = NULL;
1400 qmp_nbd_server_stop(&errp);
1401 hmp_handle_error(mon, &errp);
1404 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1406 const char *args = qdict_get_str(qdict, "args");
1407 Error *err = NULL;
1408 QemuOpts *opts;
1410 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1411 if (opts == NULL) {
1412 error_setg(&err, "Parsing chardev args failed");
1413 } else {
1414 qemu_chr_new_from_opts(opts, NULL, &err);
1416 hmp_handle_error(mon, &err);
1419 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1421 Error *local_err = NULL;
1423 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1424 hmp_handle_error(mon, &local_err);