]> git.proxmox.com Git - qemu.git/blame - hmp.c
Add XBZRLE to ram_save_block and ram_save_live
[qemu.git] / hmp.c
CommitLineData
48a32bed
AL
1/*
2 * Human Monitor Interface
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
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.
48a32bed
AL
14 */
15
16#include "hmp.h"
928059a3
LC
17#include "net.h"
18#include "qemu-option.h"
e1c37d0e 19#include "qemu-timer.h"
48a32bed 20#include "qmp-commands.h"
37003adf 21#include "monitor.h"
48a32bed 22
0cfd6a9a
LC
23static void hmp_handle_error(Monitor *mon, Error **errp)
24{
25 if (error_is_set(errp)) {
26 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
27 error_free(*errp);
28 }
29}
30
48a32bed
AL
31void hmp_info_name(Monitor *mon)
32{
33 NameInfo *info;
34
35 info = qmp_query_name(NULL);
36 if (info->has_name) {
37 monitor_printf(mon, "%s\n", info->name);
38 }
39 qapi_free_NameInfo(info);
40}
b9c15f16
LC
41
42void hmp_info_version(Monitor *mon)
43{
44 VersionInfo *info;
45
46 info = qmp_query_version(NULL);
47
48 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
49 info->qemu.major, info->qemu.minor, info->qemu.micro,
50 info->package);
51
52 qapi_free_VersionInfo(info);
53}
292a2602
LC
54
55void hmp_info_kvm(Monitor *mon)
56{
57 KvmInfo *info;
58
59 info = qmp_query_kvm(NULL);
60 monitor_printf(mon, "kvm support: ");
61 if (info->present) {
62 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
63 } else {
64 monitor_printf(mon, "not compiled\n");
65 }
66
67 qapi_free_KvmInfo(info);
68}
69
1fa9a5e4
LC
70void hmp_info_status(Monitor *mon)
71{
72 StatusInfo *info;
73
74 info = qmp_query_status(NULL);
75
76 monitor_printf(mon, "VM status: %s%s",
77 info->running ? "running" : "paused",
78 info->singlestep ? " (single step mode)" : "");
79
80 if (!info->running && info->status != RUN_STATE_PAUSED) {
81 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
82 }
83
84 monitor_printf(mon, "\n");
85
86 qapi_free_StatusInfo(info);
87}
88
efab767e
LC
89void hmp_info_uuid(Monitor *mon)
90{
91 UuidInfo *info;
92
93 info = qmp_query_uuid(NULL);
94 monitor_printf(mon, "%s\n", info->UUID);
95 qapi_free_UuidInfo(info);
96}
c5a415a0
LC
97
98void hmp_info_chardev(Monitor *mon)
99{
100 ChardevInfoList *char_info, *info;
101
102 char_info = qmp_query_chardev(NULL);
103 for (info = char_info; info; info = info->next) {
104 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
105 info->value->filename);
106 }
107
108 qapi_free_ChardevInfoList(char_info);
109}
7a7f325e 110
e235cec3
LC
111void hmp_info_mice(Monitor *mon)
112{
113 MouseInfoList *mice_list, *mouse;
114
115 mice_list = qmp_query_mice(NULL);
116 if (!mice_list) {
117 monitor_printf(mon, "No mouse devices connected\n");
118 return;
119 }
120
121 for (mouse = mice_list; mouse; mouse = mouse->next) {
122 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
123 mouse->value->current ? '*' : ' ',
124 mouse->value->index, mouse->value->name,
125 mouse->value->absolute ? " (absolute)" : "");
126 }
127
128 qapi_free_MouseInfoList(mice_list);
129}
130
791e7c82
LC
131void hmp_info_migrate(Monitor *mon)
132{
133 MigrationInfo *info;
bbf6da32 134 MigrationCapabilityStatusList *caps, *cap;
791e7c82
LC
135
136 info = qmp_query_migrate(NULL);
bbf6da32
OW
137 caps = qmp_query_migrate_capabilities(NULL);
138
139 /* do not display parameters during setup */
140 if (info->has_status && caps) {
141 monitor_printf(mon, "capabilities: ");
142 for (cap = caps; cap; cap = cap->next) {
143 monitor_printf(mon, "%s: %s ",
144 MigrationCapability_lookup[cap->value->capability],
145 cap->value->state ? "on" : "off");
146 }
147 monitor_printf(mon, "\n");
148 }
791e7c82
LC
149
150 if (info->has_status) {
151 monitor_printf(mon, "Migration status: %s\n", info->status);
152 }
153
154 if (info->has_ram) {
155 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
156 info->ram->transferred >> 10);
157 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
158 info->ram->remaining >> 10);
159 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
160 info->ram->total >> 10);
d5f8a570
JQ
161 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
162 info->ram->total_time);
791e7c82
LC
163 }
164
165 if (info->has_disk) {
166 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
167 info->disk->transferred >> 10);
168 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
169 info->disk->remaining >> 10);
170 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
171 info->disk->total >> 10);
172 }
173
174 qapi_free_MigrationInfo(info);
bbf6da32
OW
175 qapi_free_MigrationCapabilityStatusList(caps);
176}
177
178void hmp_info_migrate_capabilities(Monitor *mon)
179{
180 MigrationCapabilityStatusList *caps, *cap;
181
182 caps = qmp_query_migrate_capabilities(NULL);
183
184 if (caps) {
185 monitor_printf(mon, "capabilities: ");
186 for (cap = caps; cap; cap = cap->next) {
187 monitor_printf(mon, "%s: %s ",
188 MigrationCapability_lookup[cap->value->capability],
189 cap->value->state ? "on" : "off");
190 }
191 monitor_printf(mon, "\n");
192 }
193
194 qapi_free_MigrationCapabilityStatusList(caps);
791e7c82
LC
195}
196
de0b36b6
LC
197void hmp_info_cpus(Monitor *mon)
198{
199 CpuInfoList *cpu_list, *cpu;
200
201 cpu_list = qmp_query_cpus(NULL);
202
203 for (cpu = cpu_list; cpu; cpu = cpu->next) {
204 int active = ' ';
205
206 if (cpu->value->CPU == monitor_get_cpu_index()) {
207 active = '*';
208 }
209
210 monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
211
212 if (cpu->value->has_pc) {
213 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
214 }
215 if (cpu->value->has_nip) {
216 monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
217 }
218 if (cpu->value->has_npc) {
219 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
220 monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
221 }
222 if (cpu->value->has_PC) {
223 monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
224 }
225
226 if (cpu->value->halted) {
227 monitor_printf(mon, " (halted)");
228 }
229
230 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
231 }
232
233 qapi_free_CpuInfoList(cpu_list);
234}
235
b2023818
LC
236void hmp_info_block(Monitor *mon)
237{
238 BlockInfoList *block_list, *info;
239
240 block_list = qmp_query_block(NULL);
241
242 for (info = block_list; info; info = info->next) {
243 monitor_printf(mon, "%s: removable=%d",
244 info->value->device, info->value->removable);
245
246 if (info->value->removable) {
247 monitor_printf(mon, " locked=%d", info->value->locked);
248 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
249 }
250
251 if (info->value->has_io_status) {
252 monitor_printf(mon, " io-status=%s",
253 BlockDeviceIoStatus_lookup[info->value->io_status]);
254 }
255
256 if (info->value->has_inserted) {
257 monitor_printf(mon, " file=");
258 monitor_print_filename(mon, info->value->inserted->file);
259
260 if (info->value->inserted->has_backing_file) {
261 monitor_printf(mon, " backing_file=");
262 monitor_print_filename(mon, info->value->inserted->backing_file);
75115d95
BC
263 monitor_printf(mon, " backing_file_depth=%" PRId64,
264 info->value->inserted->backing_file_depth);
b2023818
LC
265 }
266 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
267 info->value->inserted->ro,
268 info->value->inserted->drv,
269 info->value->inserted->encrypted);
727f005e
ZYW
270
271 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
272 " bps_wr=%" PRId64 " iops=%" PRId64
273 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
274 info->value->inserted->bps,
275 info->value->inserted->bps_rd,
276 info->value->inserted->bps_wr,
277 info->value->inserted->iops,
278 info->value->inserted->iops_rd,
279 info->value->inserted->iops_wr);
b2023818
LC
280 } else {
281 monitor_printf(mon, " [not inserted]");
282 }
283
284 monitor_printf(mon, "\n");
285 }
286
287 qapi_free_BlockInfoList(block_list);
288}
289
f11f57e4
LC
290void hmp_info_blockstats(Monitor *mon)
291{
292 BlockStatsList *stats_list, *stats;
293
294 stats_list = qmp_query_blockstats(NULL);
295
296 for (stats = stats_list; stats; stats = stats->next) {
297 if (!stats->value->has_device) {
298 continue;
299 }
300
301 monitor_printf(mon, "%s:", stats->value->device);
302 monitor_printf(mon, " rd_bytes=%" PRId64
303 " wr_bytes=%" PRId64
304 " rd_operations=%" PRId64
305 " wr_operations=%" PRId64
306 " flush_operations=%" PRId64
307 " wr_total_time_ns=%" PRId64
308 " rd_total_time_ns=%" PRId64
309 " flush_total_time_ns=%" PRId64
310 "\n",
311 stats->value->stats->rd_bytes,
312 stats->value->stats->wr_bytes,
313 stats->value->stats->rd_operations,
314 stats->value->stats->wr_operations,
315 stats->value->stats->flush_operations,
316 stats->value->stats->wr_total_time_ns,
317 stats->value->stats->rd_total_time_ns,
318 stats->value->stats->flush_total_time_ns);
319 }
320
321 qapi_free_BlockStatsList(stats_list);
322}
323
2b54aa87
LC
324void hmp_info_vnc(Monitor *mon)
325{
326 VncInfo *info;
327 Error *err = NULL;
328 VncClientInfoList *client;
329
330 info = qmp_query_vnc(&err);
331 if (err) {
332 monitor_printf(mon, "%s\n", error_get_pretty(err));
333 error_free(err);
334 return;
335 }
336
337 if (!info->enabled) {
338 monitor_printf(mon, "Server: disabled\n");
339 goto out;
340 }
341
342 monitor_printf(mon, "Server:\n");
343 if (info->has_host && info->has_service) {
344 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
345 }
346 if (info->has_auth) {
347 monitor_printf(mon, " auth: %s\n", info->auth);
348 }
349
350 if (!info->has_clients || info->clients == NULL) {
351 monitor_printf(mon, "Client: none\n");
352 } else {
353 for (client = info->clients; client; client = client->next) {
354 monitor_printf(mon, "Client:\n");
355 monitor_printf(mon, " address: %s:%s\n",
356 client->value->host, client->value->service);
357 monitor_printf(mon, " x509_dname: %s\n",
358 client->value->x509_dname ?
359 client->value->x509_dname : "none");
360 monitor_printf(mon, " username: %s\n",
361 client->value->has_sasl_username ?
362 client->value->sasl_username : "none");
363 }
364 }
365
366out:
367 qapi_free_VncInfo(info);
368}
369
d1f29646
LC
370void hmp_info_spice(Monitor *mon)
371{
372 SpiceChannelList *chan;
373 SpiceInfo *info;
374
375 info = qmp_query_spice(NULL);
376
377 if (!info->enabled) {
378 monitor_printf(mon, "Server: disabled\n");
379 goto out;
380 }
381
382 monitor_printf(mon, "Server:\n");
383 if (info->has_port) {
384 monitor_printf(mon, " address: %s:%" PRId64 "\n",
385 info->host, info->port);
386 }
387 if (info->has_tls_port) {
388 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
389 info->host, info->tls_port);
390 }
391 monitor_printf(mon, " auth: %s\n", info->auth);
392 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
4efee029
AL
393 monitor_printf(mon, " mouse-mode: %s\n",
394 SpiceQueryMouseMode_lookup[info->mouse_mode]);
d1f29646
LC
395
396 if (!info->has_channels || info->channels == NULL) {
397 monitor_printf(mon, "Channels: none\n");
398 } else {
399 for (chan = info->channels; chan; chan = chan->next) {
400 monitor_printf(mon, "Channel:\n");
401 monitor_printf(mon, " address: %s:%s%s\n",
402 chan->value->host, chan->value->port,
403 chan->value->tls ? " [tls]" : "");
404 monitor_printf(mon, " session: %" PRId64 "\n",
405 chan->value->connection_id);
406 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
407 chan->value->channel_type, chan->value->channel_id);
408 }
409 }
410
411out:
412 qapi_free_SpiceInfo(info);
413}
414
96637bcd
LC
415void hmp_info_balloon(Monitor *mon)
416{
417 BalloonInfo *info;
418 Error *err = NULL;
419
420 info = qmp_query_balloon(&err);
421 if (err) {
422 monitor_printf(mon, "%s\n", error_get_pretty(err));
423 error_free(err);
424 return;
425 }
426
427 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
428 if (info->has_mem_swapped_in) {
429 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
430 }
431 if (info->has_mem_swapped_out) {
432 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
433 }
434 if (info->has_major_page_faults) {
435 monitor_printf(mon, " major_page_faults=%" PRId64,
436 info->major_page_faults);
437 }
438 if (info->has_minor_page_faults) {
439 monitor_printf(mon, " minor_page_faults=%" PRId64,
440 info->minor_page_faults);
441 }
442 if (info->has_free_mem) {
443 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
444 }
445 if (info->has_total_mem) {
446 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
447 }
448
449 monitor_printf(mon, "\n");
450
451 qapi_free_BalloonInfo(info);
452}
453
79627472
LC
454static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
455{
456 PciMemoryRegionList *region;
457
458 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
459 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
460 dev->slot, dev->function);
461 monitor_printf(mon, " ");
462
463 if (dev->class_info.has_desc) {
464 monitor_printf(mon, "%s", dev->class_info.desc);
465 } else {
466 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
467 }
468
469 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
470 dev->id.vendor, dev->id.device);
471
472 if (dev->has_irq) {
473 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
474 }
475
476 if (dev->has_pci_bridge) {
477 monitor_printf(mon, " BUS %" PRId64 ".\n",
478 dev->pci_bridge->bus.number);
479 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
480 dev->pci_bridge->bus.secondary);
481 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
482 dev->pci_bridge->bus.subordinate);
483
484 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
485 dev->pci_bridge->bus.io_range->base,
486 dev->pci_bridge->bus.io_range->limit);
487
488 monitor_printf(mon,
489 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
490 dev->pci_bridge->bus.memory_range->base,
491 dev->pci_bridge->bus.memory_range->limit);
492
493 monitor_printf(mon, " prefetchable memory range "
494 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
495 dev->pci_bridge->bus.prefetchable_range->base,
496 dev->pci_bridge->bus.prefetchable_range->limit);
497 }
498
499 for (region = dev->regions; region; region = region->next) {
500 uint64_t addr, size;
501
502 addr = region->value->address;
503 size = region->value->size;
504
505 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
506
507 if (!strcmp(region->value->type, "io")) {
508 monitor_printf(mon, "I/O at 0x%04" PRIx64
509 " [0x%04" PRIx64 "].\n",
510 addr, addr + size - 1);
511 } else {
512 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
513 " [0x%08" PRIx64 "].\n",
514 region->value->mem_type_64 ? 64 : 32,
515 region->value->prefetch ? " prefetchable" : "",
516 addr, addr + size - 1);
517 }
518 }
519
520 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
521
522 if (dev->has_pci_bridge) {
523 if (dev->pci_bridge->has_devices) {
524 PciDeviceInfoList *cdev;
525 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
526 hmp_info_pci_device(mon, cdev->value);
527 }
528 }
529 }
530}
531
532void hmp_info_pci(Monitor *mon)
533{
f46cee37 534 PciInfoList *info_list, *info;
79627472
LC
535 Error *err = NULL;
536
f46cee37 537 info_list = qmp_query_pci(&err);
79627472
LC
538 if (err) {
539 monitor_printf(mon, "PCI devices not supported\n");
540 error_free(err);
541 return;
542 }
543
f46cee37 544 for (info = info_list; info; info = info->next) {
79627472
LC
545 PciDeviceInfoList *dev;
546
547 for (dev = info->value->devices; dev; dev = dev->next) {
548 hmp_info_pci_device(mon, dev->value);
549 }
550 }
551
f46cee37 552 qapi_free_PciInfoList(info_list);
79627472
LC
553}
554
fb5458cd
SH
555void hmp_info_block_jobs(Monitor *mon)
556{
557 BlockJobInfoList *list;
558 Error *err = NULL;
559
560 list = qmp_query_block_jobs(&err);
561 assert(!err);
562
563 if (!list) {
564 monitor_printf(mon, "No active jobs\n");
565 return;
566 }
567
568 while (list) {
569 if (strcmp(list->value->type, "stream") == 0) {
570 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
571 " of %" PRId64 " bytes, speed limit %" PRId64
572 " bytes/s\n",
573 list->value->device,
574 list->value->offset,
575 list->value->len,
576 list->value->speed);
577 } else {
578 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
579 " of %" PRId64 " bytes, speed limit %" PRId64
580 " bytes/s\n",
581 list->value->type,
582 list->value->device,
583 list->value->offset,
584 list->value->len,
585 list->value->speed);
586 }
587 list = list->next;
588 }
589}
590
7a7f325e
LC
591void hmp_quit(Monitor *mon, const QDict *qdict)
592{
593 monitor_suspend(mon);
594 qmp_quit(NULL);
595}
5f158f21
LC
596
597void hmp_stop(Monitor *mon, const QDict *qdict)
598{
599 qmp_stop(NULL);
600}
38d22653
LC
601
602void hmp_system_reset(Monitor *mon, const QDict *qdict)
603{
604 qmp_system_reset(NULL);
605}
5bc465e4
LC
606
607void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
608{
609 qmp_system_powerdown(NULL);
610}
755f1968
LC
611
612void hmp_cpu(Monitor *mon, const QDict *qdict)
613{
614 int64_t cpu_index;
615
616 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
617 use it are converted to the QAPI */
618 cpu_index = qdict_get_int(qdict, "index");
619 if (monitor_set_cpu(cpu_index) < 0) {
620 monitor_printf(mon, "invalid CPU index\n");
621 }
622}
0cfd6a9a
LC
623
624void hmp_memsave(Monitor *mon, const QDict *qdict)
625{
626 uint32_t size = qdict_get_int(qdict, "size");
627 const char *filename = qdict_get_str(qdict, "filename");
628 uint64_t addr = qdict_get_int(qdict, "val");
629 Error *errp = NULL;
630
631 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
632 hmp_handle_error(mon, &errp);
633}
6d3962bf
LC
634
635void hmp_pmemsave(Monitor *mon, const QDict *qdict)
636{
637 uint32_t size = qdict_get_int(qdict, "size");
638 const char *filename = qdict_get_str(qdict, "filename");
639 uint64_t addr = qdict_get_int(qdict, "val");
640 Error *errp = NULL;
641
642 qmp_pmemsave(addr, size, filename, &errp);
643 hmp_handle_error(mon, &errp);
644}
e42e818b
LC
645
646static void hmp_cont_cb(void *opaque, int err)
647{
648 Monitor *mon = opaque;
649
650 if (!err) {
651 hmp_cont(mon, NULL);
652 }
653}
654
655void hmp_cont(Monitor *mon, const QDict *qdict)
656{
657 Error *errp = NULL;
658
659 qmp_cont(&errp);
660 if (error_is_set(&errp)) {
661 if (error_is_type(errp, QERR_DEVICE_ENCRYPTED)) {
662 const char *device;
663
664 /* The device is encrypted. Ask the user for the password
665 and retry */
666
667 device = error_get_field(errp, "device");
668 assert(device != NULL);
669
670 monitor_read_block_device_key(mon, device, hmp_cont_cb, mon);
671 error_free(errp);
672 return;
673 }
674 hmp_handle_error(mon, &errp);
675 }
676}
ab49ab5c 677
9b9df25a
GH
678void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
679{
680 qmp_system_wakeup(NULL);
681}
682
ab49ab5c
LC
683void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
684{
685 Error *errp = NULL;
686
687 qmp_inject_nmi(&errp);
688 hmp_handle_error(mon, &errp);
689}
4b37156c
LC
690
691void hmp_set_link(Monitor *mon, const QDict *qdict)
692{
693 const char *name = qdict_get_str(qdict, "name");
694 int up = qdict_get_bool(qdict, "up");
695 Error *errp = NULL;
696
697 qmp_set_link(name, up, &errp);
698 hmp_handle_error(mon, &errp);
699}
a4dea8a9
LC
700
701void hmp_block_passwd(Monitor *mon, const QDict *qdict)
702{
703 const char *device = qdict_get_str(qdict, "device");
704 const char *password = qdict_get_str(qdict, "password");
705 Error *errp = NULL;
706
707 qmp_block_passwd(device, password, &errp);
708 hmp_handle_error(mon, &errp);
709}
d72f3264
LC
710
711void hmp_balloon(Monitor *mon, const QDict *qdict)
712{
713 int64_t value = qdict_get_int(qdict, "value");
714 Error *errp = NULL;
715
716 qmp_balloon(value, &errp);
717 if (error_is_set(&errp)) {
718 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
719 error_free(errp);
720 }
721}
5e7caacb
LC
722
723void hmp_block_resize(Monitor *mon, const QDict *qdict)
724{
725 const char *device = qdict_get_str(qdict, "device");
726 int64_t size = qdict_get_int(qdict, "size");
727 Error *errp = NULL;
728
729 qmp_block_resize(device, size, &errp);
730 hmp_handle_error(mon, &errp);
731}
6106e249
LC
732
733void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
734{
735 const char *device = qdict_get_str(qdict, "device");
736 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
737 const char *format = qdict_get_try_str(qdict, "format");
6cc2a415
PB
738 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
739 enum NewImageMode mode;
6106e249
LC
740 Error *errp = NULL;
741
742 if (!filename) {
743 /* In the future, if 'snapshot-file' is not specified, the snapshot
744 will be taken internally. Today it's actually required. */
745 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
746 hmp_handle_error(mon, &errp);
747 return;
748 }
749
6cc2a415
PB
750 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
751 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
752 true, mode, &errp);
6106e249
LC
753 hmp_handle_error(mon, &errp);
754}
6cdedb07
LC
755
756void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
757{
758 qmp_migrate_cancel(NULL);
759}
4f0a993b
LC
760
761void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
762{
763 double value = qdict_get_double(qdict, "value");
764 qmp_migrate_set_downtime(value, NULL);
765}
3dc85383
LC
766
767void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
768{
769 int64_t value = qdict_get_int(qdict, "value");
770 qmp_migrate_set_speed(value, NULL);
771}
fbf796fd 772
00458433
OW
773void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
774{
775 const char *cap = qdict_get_str(qdict, "capability");
776 bool state = qdict_get_bool(qdict, "state");
777 Error *err = NULL;
778 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
779 int i;
780
781 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
782 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
783 caps->value = g_malloc0(sizeof(*caps->value));
784 caps->value->capability = i;
785 caps->value->state = state;
786 caps->next = NULL;
787 qmp_migrate_set_capabilities(caps, &err);
788 break;
789 }
790 }
791
792 if (i == MIGRATION_CAPABILITY_MAX) {
793 error_set(&err, QERR_INVALID_PARAMETER, cap);
794 }
795
796 qapi_free_MigrationCapabilityStatusList(caps);
797
798 if (err) {
799 monitor_printf(mon, "migrate_set_parameter: %s\n",
800 error_get_pretty(err));
801 error_free(err);
802 }
803}
804
fbf796fd
LC
805void hmp_set_password(Monitor *mon, const QDict *qdict)
806{
807 const char *protocol = qdict_get_str(qdict, "protocol");
808 const char *password = qdict_get_str(qdict, "password");
809 const char *connected = qdict_get_try_str(qdict, "connected");
810 Error *err = NULL;
811
812 qmp_set_password(protocol, password, !!connected, connected, &err);
813 hmp_handle_error(mon, &err);
814}
9ad5372d
LC
815
816void hmp_expire_password(Monitor *mon, const QDict *qdict)
817{
818 const char *protocol = qdict_get_str(qdict, "protocol");
819 const char *whenstr = qdict_get_str(qdict, "time");
820 Error *err = NULL;
821
822 qmp_expire_password(protocol, whenstr, &err);
823 hmp_handle_error(mon, &err);
824}
c245b6a3
LC
825
826void hmp_eject(Monitor *mon, const QDict *qdict)
827{
828 int force = qdict_get_try_bool(qdict, "force", 0);
829 const char *device = qdict_get_str(qdict, "device");
830 Error *err = NULL;
831
832 qmp_eject(device, true, force, &err);
833 hmp_handle_error(mon, &err);
834}
333a96ec
LC
835
836static void hmp_change_read_arg(Monitor *mon, const char *password,
837 void *opaque)
838{
839 qmp_change_vnc_password(password, NULL);
840 monitor_read_command(mon, 1);
841}
842
843static void cb_hmp_change_bdrv_pwd(Monitor *mon, const char *password,
844 void *opaque)
845{
846 Error *encryption_err = opaque;
847 Error *err = NULL;
848 const char *device;
849
850 device = error_get_field(encryption_err, "device");
851
852 qmp_block_passwd(device, password, &err);
853 hmp_handle_error(mon, &err);
854 error_free(encryption_err);
855
856 monitor_read_command(mon, 1);
857}
858
859void hmp_change(Monitor *mon, const QDict *qdict)
860{
861 const char *device = qdict_get_str(qdict, "device");
862 const char *target = qdict_get_str(qdict, "target");
863 const char *arg = qdict_get_try_str(qdict, "arg");
864 Error *err = NULL;
865
866 if (strcmp(device, "vnc") == 0 &&
867 (strcmp(target, "passwd") == 0 ||
868 strcmp(target, "password") == 0)) {
869 if (!arg) {
870 monitor_read_password(mon, hmp_change_read_arg, NULL);
871 return;
872 }
873 }
874
875 qmp_change(device, target, !!arg, arg, &err);
876 if (error_is_type(err, QERR_DEVICE_ENCRYPTED)) {
877 monitor_printf(mon, "%s (%s) is encrypted.\n",
878 error_get_field(err, "device"),
879 error_get_field(err, "filename"));
880 if (!monitor_get_rs(mon)) {
881 monitor_printf(mon,
882 "terminal does not support password prompting\n");
883 error_free(err);
884 return;
885 }
886 readline_start(monitor_get_rs(mon), "Password: ", 1,
887 cb_hmp_change_bdrv_pwd, err);
888 return;
889 }
890 hmp_handle_error(mon, &err);
891}
80047da5
LC
892
893void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
894{
895 Error *err = NULL;
896
897 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
898 qdict_get_int(qdict, "bps"),
899 qdict_get_int(qdict, "bps_rd"),
900 qdict_get_int(qdict, "bps_wr"),
901 qdict_get_int(qdict, "iops"),
902 qdict_get_int(qdict, "iops_rd"),
903 qdict_get_int(qdict, "iops_wr"), &err);
904 hmp_handle_error(mon, &err);
905}
12bd451f
SH
906
907void hmp_block_stream(Monitor *mon, const QDict *qdict)
908{
909 Error *error = NULL;
910 const char *device = qdict_get_str(qdict, "device");
911 const char *base = qdict_get_try_str(qdict, "base");
c83c66c3 912 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
12bd451f 913
c83c66c3
SH
914 qmp_block_stream(device, base != NULL, base,
915 qdict_haskey(qdict, "speed"), speed, &error);
12bd451f
SH
916
917 hmp_handle_error(mon, &error);
918}
2d47c6e9
SH
919
920void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
921{
922 Error *error = NULL;
923 const char *device = qdict_get_str(qdict, "device");
c6db2395 924 int64_t value = qdict_get_int(qdict, "speed");
2d47c6e9
SH
925
926 qmp_block_job_set_speed(device, value, &error);
927
928 hmp_handle_error(mon, &error);
929}
370521a1
SH
930
931void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
932{
933 Error *error = NULL;
934 const char *device = qdict_get_str(qdict, "device");
935
936 qmp_block_job_cancel(device, &error);
937
938 hmp_handle_error(mon, &error);
939}
e1c37d0e
LC
940
941typedef struct MigrationStatus
942{
943 QEMUTimer *timer;
944 Monitor *mon;
945 bool is_block_migration;
946} MigrationStatus;
947
948static void hmp_migrate_status_cb(void *opaque)
949{
950 MigrationStatus *status = opaque;
951 MigrationInfo *info;
952
953 info = qmp_query_migrate(NULL);
954 if (!info->has_status || strcmp(info->status, "active") == 0) {
955 if (info->has_disk) {
956 int progress;
957
958 if (info->disk->remaining) {
959 progress = info->disk->transferred * 100 / info->disk->total;
960 } else {
961 progress = 100;
962 }
963
964 monitor_printf(status->mon, "Completed %d %%\r", progress);
965 monitor_flush(status->mon);
966 }
967
968 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
969 } else {
970 if (status->is_block_migration) {
971 monitor_printf(status->mon, "\n");
972 }
973 monitor_resume(status->mon);
974 qemu_del_timer(status->timer);
975 g_free(status);
976 }
977
978 qapi_free_MigrationInfo(info);
979}
980
981void hmp_migrate(Monitor *mon, const QDict *qdict)
982{
983 int detach = qdict_get_try_bool(qdict, "detach", 0);
984 int blk = qdict_get_try_bool(qdict, "blk", 0);
985 int inc = qdict_get_try_bool(qdict, "inc", 0);
986 const char *uri = qdict_get_str(qdict, "uri");
987 Error *err = NULL;
988
989 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
990 if (err) {
991 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
992 error_free(err);
993 return;
994 }
995
996 if (!detach) {
997 MigrationStatus *status;
998
999 if (monitor_suspend(mon) < 0) {
1000 monitor_printf(mon, "terminal does not allow synchronous "
1001 "migration, continuing detached\n");
1002 return;
1003 }
1004
1005 status = g_malloc0(sizeof(*status));
1006 status->mon = mon;
1007 status->is_block_migration = blk || inc;
1008 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1009 status);
1010 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1011 }
1012}
a15fef21
LC
1013
1014void hmp_device_del(Monitor *mon, const QDict *qdict)
1015{
1016 const char *id = qdict_get_str(qdict, "id");
1017 Error *err = NULL;
1018
1019 qmp_device_del(id, &err);
1020 hmp_handle_error(mon, &err);
1021}
783e9b48
WC
1022
1023void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1024{
1025 Error *errp = NULL;
1026 int paging = qdict_get_try_bool(qdict, "paging", 0);
1027 const char *file = qdict_get_str(qdict, "protocol");
1028 bool has_begin = qdict_haskey(qdict, "begin");
1029 bool has_length = qdict_haskey(qdict, "length");
1030 int64_t begin = 0;
1031 int64_t length = 0;
1032
1033 if (has_begin) {
1034 begin = qdict_get_int(qdict, "begin");
1035 }
1036 if (has_length) {
1037 length = qdict_get_int(qdict, "length");
1038 }
1039
1040 qmp_dump_guest_memory(paging, file, has_begin, begin, has_length, length,
1041 &errp);
1042 hmp_handle_error(mon, &errp);
1043}
928059a3
LC
1044
1045void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1046{
1047 Error *err = NULL;
1048 QemuOpts *opts;
1049
1050 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1051 if (error_is_set(&err)) {
1052 goto out;
1053 }
1054
1055 netdev_add(opts, &err);
1056 if (error_is_set(&err)) {
1057 qemu_opts_del(opts);
1058 }
1059
1060out:
1061 hmp_handle_error(mon, &err);
1062}
5f964155
LC
1063
1064void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1065{
1066 const char *id = qdict_get_str(qdict, "id");
1067 Error *err = NULL;
1068
1069 qmp_netdev_del(id, &err);
1070 hmp_handle_error(mon, &err);
1071}
208c9d1b
CB
1072
1073void hmp_getfd(Monitor *mon, const QDict *qdict)
1074{
1075 const char *fdname = qdict_get_str(qdict, "fdname");
1076 Error *errp = NULL;
1077
1078 qmp_getfd(fdname, &errp);
1079 hmp_handle_error(mon, &errp);
1080}
1081
1082void hmp_closefd(Monitor *mon, const QDict *qdict)
1083{
1084 const char *fdname = qdict_get_str(qdict, "fdname");
1085 Error *errp = NULL;
1086
1087 qmp_closefd(fdname, &errp);
1088 hmp_handle_error(mon, &errp);
1089}