]> git.proxmox.com Git - qemu.git/blob - hmp.c
Merge remote-tracking branch 'qmp/queue/qmp' into staging
[qemu.git] / hmp.c
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 *
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.
14 */
15
16 #include "hmp.h"
17 #include "net.h"
18 #include "qemu-option.h"
19 #include "qemu-timer.h"
20 #include "qmp-commands.h"
21 #include "monitor.h"
22
23 static 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
31 void 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 }
41
42 void 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 }
54
55 void 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
70 void 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
89 void 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 }
97
98 void 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 }
110
111 void 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
131 void hmp_info_migrate(Monitor *mon)
132 {
133 MigrationInfo *info;
134
135 info = qmp_query_migrate(NULL);
136
137 if (info->has_status) {
138 monitor_printf(mon, "Migration status: %s\n", info->status);
139 }
140
141 if (info->has_ram) {
142 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
143 info->ram->transferred >> 10);
144 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
145 info->ram->remaining >> 10);
146 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
147 info->ram->total >> 10);
148 }
149
150 if (info->has_disk) {
151 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
152 info->disk->transferred >> 10);
153 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
154 info->disk->remaining >> 10);
155 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
156 info->disk->total >> 10);
157 }
158
159 qapi_free_MigrationInfo(info);
160 }
161
162 void hmp_info_cpus(Monitor *mon)
163 {
164 CpuInfoList *cpu_list, *cpu;
165
166 cpu_list = qmp_query_cpus(NULL);
167
168 for (cpu = cpu_list; cpu; cpu = cpu->next) {
169 int active = ' ';
170
171 if (cpu->value->CPU == monitor_get_cpu_index()) {
172 active = '*';
173 }
174
175 monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
176
177 if (cpu->value->has_pc) {
178 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
179 }
180 if (cpu->value->has_nip) {
181 monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
182 }
183 if (cpu->value->has_npc) {
184 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
185 monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
186 }
187 if (cpu->value->has_PC) {
188 monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
189 }
190
191 if (cpu->value->halted) {
192 monitor_printf(mon, " (halted)");
193 }
194
195 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
196 }
197
198 qapi_free_CpuInfoList(cpu_list);
199 }
200
201 void hmp_info_block(Monitor *mon)
202 {
203 BlockInfoList *block_list, *info;
204
205 block_list = qmp_query_block(NULL);
206
207 for (info = block_list; info; info = info->next) {
208 monitor_printf(mon, "%s: removable=%d",
209 info->value->device, info->value->removable);
210
211 if (info->value->removable) {
212 monitor_printf(mon, " locked=%d", info->value->locked);
213 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
214 }
215
216 if (info->value->has_io_status) {
217 monitor_printf(mon, " io-status=%s",
218 BlockDeviceIoStatus_lookup[info->value->io_status]);
219 }
220
221 if (info->value->has_inserted) {
222 monitor_printf(mon, " file=");
223 monitor_print_filename(mon, info->value->inserted->file);
224
225 if (info->value->inserted->has_backing_file) {
226 monitor_printf(mon, " backing_file=");
227 monitor_print_filename(mon, info->value->inserted->backing_file);
228 }
229 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
230 info->value->inserted->ro,
231 info->value->inserted->drv,
232 info->value->inserted->encrypted);
233
234 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
235 " bps_wr=%" PRId64 " iops=%" PRId64
236 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
237 info->value->inserted->bps,
238 info->value->inserted->bps_rd,
239 info->value->inserted->bps_wr,
240 info->value->inserted->iops,
241 info->value->inserted->iops_rd,
242 info->value->inserted->iops_wr);
243 } else {
244 monitor_printf(mon, " [not inserted]");
245 }
246
247 monitor_printf(mon, "\n");
248 }
249
250 qapi_free_BlockInfoList(block_list);
251 }
252
253 void hmp_info_blockstats(Monitor *mon)
254 {
255 BlockStatsList *stats_list, *stats;
256
257 stats_list = qmp_query_blockstats(NULL);
258
259 for (stats = stats_list; stats; stats = stats->next) {
260 if (!stats->value->has_device) {
261 continue;
262 }
263
264 monitor_printf(mon, "%s:", stats->value->device);
265 monitor_printf(mon, " rd_bytes=%" PRId64
266 " wr_bytes=%" PRId64
267 " rd_operations=%" PRId64
268 " wr_operations=%" PRId64
269 " flush_operations=%" PRId64
270 " wr_total_time_ns=%" PRId64
271 " rd_total_time_ns=%" PRId64
272 " flush_total_time_ns=%" PRId64
273 "\n",
274 stats->value->stats->rd_bytes,
275 stats->value->stats->wr_bytes,
276 stats->value->stats->rd_operations,
277 stats->value->stats->wr_operations,
278 stats->value->stats->flush_operations,
279 stats->value->stats->wr_total_time_ns,
280 stats->value->stats->rd_total_time_ns,
281 stats->value->stats->flush_total_time_ns);
282 }
283
284 qapi_free_BlockStatsList(stats_list);
285 }
286
287 void hmp_info_vnc(Monitor *mon)
288 {
289 VncInfo *info;
290 Error *err = NULL;
291 VncClientInfoList *client;
292
293 info = qmp_query_vnc(&err);
294 if (err) {
295 monitor_printf(mon, "%s\n", error_get_pretty(err));
296 error_free(err);
297 return;
298 }
299
300 if (!info->enabled) {
301 monitor_printf(mon, "Server: disabled\n");
302 goto out;
303 }
304
305 monitor_printf(mon, "Server:\n");
306 if (info->has_host && info->has_service) {
307 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
308 }
309 if (info->has_auth) {
310 monitor_printf(mon, " auth: %s\n", info->auth);
311 }
312
313 if (!info->has_clients || info->clients == NULL) {
314 monitor_printf(mon, "Client: none\n");
315 } else {
316 for (client = info->clients; client; client = client->next) {
317 monitor_printf(mon, "Client:\n");
318 monitor_printf(mon, " address: %s:%s\n",
319 client->value->host, client->value->service);
320 monitor_printf(mon, " x509_dname: %s\n",
321 client->value->x509_dname ?
322 client->value->x509_dname : "none");
323 monitor_printf(mon, " username: %s\n",
324 client->value->has_sasl_username ?
325 client->value->sasl_username : "none");
326 }
327 }
328
329 out:
330 qapi_free_VncInfo(info);
331 }
332
333 void hmp_info_spice(Monitor *mon)
334 {
335 SpiceChannelList *chan;
336 SpiceInfo *info;
337
338 info = qmp_query_spice(NULL);
339
340 if (!info->enabled) {
341 monitor_printf(mon, "Server: disabled\n");
342 goto out;
343 }
344
345 monitor_printf(mon, "Server:\n");
346 if (info->has_port) {
347 monitor_printf(mon, " address: %s:%" PRId64 "\n",
348 info->host, info->port);
349 }
350 if (info->has_tls_port) {
351 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
352 info->host, info->tls_port);
353 }
354 monitor_printf(mon, " auth: %s\n", info->auth);
355 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
356 monitor_printf(mon, " mouse-mode: %s\n",
357 SpiceQueryMouseMode_lookup[info->mouse_mode]);
358
359 if (!info->has_channels || info->channels == NULL) {
360 monitor_printf(mon, "Channels: none\n");
361 } else {
362 for (chan = info->channels; chan; chan = chan->next) {
363 monitor_printf(mon, "Channel:\n");
364 monitor_printf(mon, " address: %s:%s%s\n",
365 chan->value->host, chan->value->port,
366 chan->value->tls ? " [tls]" : "");
367 monitor_printf(mon, " session: %" PRId64 "\n",
368 chan->value->connection_id);
369 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
370 chan->value->channel_type, chan->value->channel_id);
371 }
372 }
373
374 out:
375 qapi_free_SpiceInfo(info);
376 }
377
378 void hmp_info_balloon(Monitor *mon)
379 {
380 BalloonInfo *info;
381 Error *err = NULL;
382
383 info = qmp_query_balloon(&err);
384 if (err) {
385 monitor_printf(mon, "%s\n", error_get_pretty(err));
386 error_free(err);
387 return;
388 }
389
390 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
391 if (info->has_mem_swapped_in) {
392 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
393 }
394 if (info->has_mem_swapped_out) {
395 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
396 }
397 if (info->has_major_page_faults) {
398 monitor_printf(mon, " major_page_faults=%" PRId64,
399 info->major_page_faults);
400 }
401 if (info->has_minor_page_faults) {
402 monitor_printf(mon, " minor_page_faults=%" PRId64,
403 info->minor_page_faults);
404 }
405 if (info->has_free_mem) {
406 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
407 }
408 if (info->has_total_mem) {
409 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
410 }
411
412 monitor_printf(mon, "\n");
413
414 qapi_free_BalloonInfo(info);
415 }
416
417 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
418 {
419 PciMemoryRegionList *region;
420
421 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
422 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
423 dev->slot, dev->function);
424 monitor_printf(mon, " ");
425
426 if (dev->class_info.has_desc) {
427 monitor_printf(mon, "%s", dev->class_info.desc);
428 } else {
429 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
430 }
431
432 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
433 dev->id.vendor, dev->id.device);
434
435 if (dev->has_irq) {
436 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
437 }
438
439 if (dev->has_pci_bridge) {
440 monitor_printf(mon, " BUS %" PRId64 ".\n",
441 dev->pci_bridge->bus.number);
442 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
443 dev->pci_bridge->bus.secondary);
444 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
445 dev->pci_bridge->bus.subordinate);
446
447 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
448 dev->pci_bridge->bus.io_range->base,
449 dev->pci_bridge->bus.io_range->limit);
450
451 monitor_printf(mon,
452 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
453 dev->pci_bridge->bus.memory_range->base,
454 dev->pci_bridge->bus.memory_range->limit);
455
456 monitor_printf(mon, " prefetchable memory range "
457 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
458 dev->pci_bridge->bus.prefetchable_range->base,
459 dev->pci_bridge->bus.prefetchable_range->limit);
460 }
461
462 for (region = dev->regions; region; region = region->next) {
463 uint64_t addr, size;
464
465 addr = region->value->address;
466 size = region->value->size;
467
468 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
469
470 if (!strcmp(region->value->type, "io")) {
471 monitor_printf(mon, "I/O at 0x%04" PRIx64
472 " [0x%04" PRIx64 "].\n",
473 addr, addr + size - 1);
474 } else {
475 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
476 " [0x%08" PRIx64 "].\n",
477 region->value->mem_type_64 ? 64 : 32,
478 region->value->prefetch ? " prefetchable" : "",
479 addr, addr + size - 1);
480 }
481 }
482
483 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
484
485 if (dev->has_pci_bridge) {
486 if (dev->pci_bridge->has_devices) {
487 PciDeviceInfoList *cdev;
488 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
489 hmp_info_pci_device(mon, cdev->value);
490 }
491 }
492 }
493 }
494
495 void hmp_info_pci(Monitor *mon)
496 {
497 PciInfoList *info_list, *info;
498 Error *err = NULL;
499
500 info_list = qmp_query_pci(&err);
501 if (err) {
502 monitor_printf(mon, "PCI devices not supported\n");
503 error_free(err);
504 return;
505 }
506
507 for (info = info_list; info; info = info->next) {
508 PciDeviceInfoList *dev;
509
510 for (dev = info->value->devices; dev; dev = dev->next) {
511 hmp_info_pci_device(mon, dev->value);
512 }
513 }
514
515 qapi_free_PciInfoList(info_list);
516 }
517
518 void hmp_info_block_jobs(Monitor *mon)
519 {
520 BlockJobInfoList *list;
521 Error *err = NULL;
522
523 list = qmp_query_block_jobs(&err);
524 assert(!err);
525
526 if (!list) {
527 monitor_printf(mon, "No active jobs\n");
528 return;
529 }
530
531 while (list) {
532 if (strcmp(list->value->type, "stream") == 0) {
533 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
534 " of %" PRId64 " bytes, speed limit %" PRId64
535 " bytes/s\n",
536 list->value->device,
537 list->value->offset,
538 list->value->len,
539 list->value->speed);
540 } else {
541 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
542 " of %" PRId64 " bytes, speed limit %" PRId64
543 " bytes/s\n",
544 list->value->type,
545 list->value->device,
546 list->value->offset,
547 list->value->len,
548 list->value->speed);
549 }
550 list = list->next;
551 }
552 }
553
554 void hmp_quit(Monitor *mon, const QDict *qdict)
555 {
556 monitor_suspend(mon);
557 qmp_quit(NULL);
558 }
559
560 void hmp_stop(Monitor *mon, const QDict *qdict)
561 {
562 qmp_stop(NULL);
563 }
564
565 void hmp_system_reset(Monitor *mon, const QDict *qdict)
566 {
567 qmp_system_reset(NULL);
568 }
569
570 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
571 {
572 qmp_system_powerdown(NULL);
573 }
574
575 void hmp_cpu(Monitor *mon, const QDict *qdict)
576 {
577 int64_t cpu_index;
578
579 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
580 use it are converted to the QAPI */
581 cpu_index = qdict_get_int(qdict, "index");
582 if (monitor_set_cpu(cpu_index) < 0) {
583 monitor_printf(mon, "invalid CPU index\n");
584 }
585 }
586
587 void hmp_memsave(Monitor *mon, const QDict *qdict)
588 {
589 uint32_t size = qdict_get_int(qdict, "size");
590 const char *filename = qdict_get_str(qdict, "filename");
591 uint64_t addr = qdict_get_int(qdict, "val");
592 Error *errp = NULL;
593
594 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
595 hmp_handle_error(mon, &errp);
596 }
597
598 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
599 {
600 uint32_t size = qdict_get_int(qdict, "size");
601 const char *filename = qdict_get_str(qdict, "filename");
602 uint64_t addr = qdict_get_int(qdict, "val");
603 Error *errp = NULL;
604
605 qmp_pmemsave(addr, size, filename, &errp);
606 hmp_handle_error(mon, &errp);
607 }
608
609 static void hmp_cont_cb(void *opaque, int err)
610 {
611 Monitor *mon = opaque;
612
613 if (!err) {
614 hmp_cont(mon, NULL);
615 }
616 }
617
618 void hmp_cont(Monitor *mon, const QDict *qdict)
619 {
620 Error *errp = NULL;
621
622 qmp_cont(&errp);
623 if (error_is_set(&errp)) {
624 if (error_is_type(errp, QERR_DEVICE_ENCRYPTED)) {
625 const char *device;
626
627 /* The device is encrypted. Ask the user for the password
628 and retry */
629
630 device = error_get_field(errp, "device");
631 assert(device != NULL);
632
633 monitor_read_block_device_key(mon, device, hmp_cont_cb, mon);
634 error_free(errp);
635 return;
636 }
637 hmp_handle_error(mon, &errp);
638 }
639 }
640
641 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
642 {
643 qmp_system_wakeup(NULL);
644 }
645
646 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
647 {
648 Error *errp = NULL;
649
650 qmp_inject_nmi(&errp);
651 hmp_handle_error(mon, &errp);
652 }
653
654 void hmp_set_link(Monitor *mon, const QDict *qdict)
655 {
656 const char *name = qdict_get_str(qdict, "name");
657 int up = qdict_get_bool(qdict, "up");
658 Error *errp = NULL;
659
660 qmp_set_link(name, up, &errp);
661 hmp_handle_error(mon, &errp);
662 }
663
664 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
665 {
666 const char *device = qdict_get_str(qdict, "device");
667 const char *password = qdict_get_str(qdict, "password");
668 Error *errp = NULL;
669
670 qmp_block_passwd(device, password, &errp);
671 hmp_handle_error(mon, &errp);
672 }
673
674 void hmp_balloon(Monitor *mon, const QDict *qdict)
675 {
676 int64_t value = qdict_get_int(qdict, "value");
677 Error *errp = NULL;
678
679 qmp_balloon(value, &errp);
680 if (error_is_set(&errp)) {
681 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
682 error_free(errp);
683 }
684 }
685
686 void hmp_block_resize(Monitor *mon, const QDict *qdict)
687 {
688 const char *device = qdict_get_str(qdict, "device");
689 int64_t size = qdict_get_int(qdict, "size");
690 Error *errp = NULL;
691
692 qmp_block_resize(device, size, &errp);
693 hmp_handle_error(mon, &errp);
694 }
695
696 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
697 {
698 const char *device = qdict_get_str(qdict, "device");
699 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
700 const char *format = qdict_get_try_str(qdict, "format");
701 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
702 enum NewImageMode mode;
703 Error *errp = NULL;
704
705 if (!filename) {
706 /* In the future, if 'snapshot-file' is not specified, the snapshot
707 will be taken internally. Today it's actually required. */
708 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
709 hmp_handle_error(mon, &errp);
710 return;
711 }
712
713 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
714 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
715 true, mode, &errp);
716 hmp_handle_error(mon, &errp);
717 }
718
719 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
720 {
721 qmp_migrate_cancel(NULL);
722 }
723
724 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
725 {
726 double value = qdict_get_double(qdict, "value");
727 qmp_migrate_set_downtime(value, NULL);
728 }
729
730 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
731 {
732 int64_t value = qdict_get_int(qdict, "value");
733 qmp_migrate_set_speed(value, NULL);
734 }
735
736 void hmp_set_password(Monitor *mon, const QDict *qdict)
737 {
738 const char *protocol = qdict_get_str(qdict, "protocol");
739 const char *password = qdict_get_str(qdict, "password");
740 const char *connected = qdict_get_try_str(qdict, "connected");
741 Error *err = NULL;
742
743 qmp_set_password(protocol, password, !!connected, connected, &err);
744 hmp_handle_error(mon, &err);
745 }
746
747 void hmp_expire_password(Monitor *mon, const QDict *qdict)
748 {
749 const char *protocol = qdict_get_str(qdict, "protocol");
750 const char *whenstr = qdict_get_str(qdict, "time");
751 Error *err = NULL;
752
753 qmp_expire_password(protocol, whenstr, &err);
754 hmp_handle_error(mon, &err);
755 }
756
757 void hmp_eject(Monitor *mon, const QDict *qdict)
758 {
759 int force = qdict_get_try_bool(qdict, "force", 0);
760 const char *device = qdict_get_str(qdict, "device");
761 Error *err = NULL;
762
763 qmp_eject(device, true, force, &err);
764 hmp_handle_error(mon, &err);
765 }
766
767 static void hmp_change_read_arg(Monitor *mon, const char *password,
768 void *opaque)
769 {
770 qmp_change_vnc_password(password, NULL);
771 monitor_read_command(mon, 1);
772 }
773
774 static void cb_hmp_change_bdrv_pwd(Monitor *mon, const char *password,
775 void *opaque)
776 {
777 Error *encryption_err = opaque;
778 Error *err = NULL;
779 const char *device;
780
781 device = error_get_field(encryption_err, "device");
782
783 qmp_block_passwd(device, password, &err);
784 hmp_handle_error(mon, &err);
785 error_free(encryption_err);
786
787 monitor_read_command(mon, 1);
788 }
789
790 void hmp_change(Monitor *mon, const QDict *qdict)
791 {
792 const char *device = qdict_get_str(qdict, "device");
793 const char *target = qdict_get_str(qdict, "target");
794 const char *arg = qdict_get_try_str(qdict, "arg");
795 Error *err = NULL;
796
797 if (strcmp(device, "vnc") == 0 &&
798 (strcmp(target, "passwd") == 0 ||
799 strcmp(target, "password") == 0)) {
800 if (!arg) {
801 monitor_read_password(mon, hmp_change_read_arg, NULL);
802 return;
803 }
804 }
805
806 qmp_change(device, target, !!arg, arg, &err);
807 if (error_is_type(err, QERR_DEVICE_ENCRYPTED)) {
808 monitor_printf(mon, "%s (%s) is encrypted.\n",
809 error_get_field(err, "device"),
810 error_get_field(err, "filename"));
811 if (!monitor_get_rs(mon)) {
812 monitor_printf(mon,
813 "terminal does not support password prompting\n");
814 error_free(err);
815 return;
816 }
817 readline_start(monitor_get_rs(mon), "Password: ", 1,
818 cb_hmp_change_bdrv_pwd, err);
819 return;
820 }
821 hmp_handle_error(mon, &err);
822 }
823
824 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
825 {
826 Error *err = NULL;
827
828 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
829 qdict_get_int(qdict, "bps"),
830 qdict_get_int(qdict, "bps_rd"),
831 qdict_get_int(qdict, "bps_wr"),
832 qdict_get_int(qdict, "iops"),
833 qdict_get_int(qdict, "iops_rd"),
834 qdict_get_int(qdict, "iops_wr"), &err);
835 hmp_handle_error(mon, &err);
836 }
837
838 void hmp_block_stream(Monitor *mon, const QDict *qdict)
839 {
840 Error *error = NULL;
841 const char *device = qdict_get_str(qdict, "device");
842 const char *base = qdict_get_try_str(qdict, "base");
843 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
844
845 qmp_block_stream(device, base != NULL, base,
846 qdict_haskey(qdict, "speed"), speed, &error);
847
848 hmp_handle_error(mon, &error);
849 }
850
851 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
852 {
853 Error *error = NULL;
854 const char *device = qdict_get_str(qdict, "device");
855 int64_t value = qdict_get_int(qdict, "speed");
856
857 qmp_block_job_set_speed(device, value, &error);
858
859 hmp_handle_error(mon, &error);
860 }
861
862 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
863 {
864 Error *error = NULL;
865 const char *device = qdict_get_str(qdict, "device");
866
867 qmp_block_job_cancel(device, &error);
868
869 hmp_handle_error(mon, &error);
870 }
871
872 typedef struct MigrationStatus
873 {
874 QEMUTimer *timer;
875 Monitor *mon;
876 bool is_block_migration;
877 } MigrationStatus;
878
879 static void hmp_migrate_status_cb(void *opaque)
880 {
881 MigrationStatus *status = opaque;
882 MigrationInfo *info;
883
884 info = qmp_query_migrate(NULL);
885 if (!info->has_status || strcmp(info->status, "active") == 0) {
886 if (info->has_disk) {
887 int progress;
888
889 if (info->disk->remaining) {
890 progress = info->disk->transferred * 100 / info->disk->total;
891 } else {
892 progress = 100;
893 }
894
895 monitor_printf(status->mon, "Completed %d %%\r", progress);
896 monitor_flush(status->mon);
897 }
898
899 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
900 } else {
901 if (status->is_block_migration) {
902 monitor_printf(status->mon, "\n");
903 }
904 monitor_resume(status->mon);
905 qemu_del_timer(status->timer);
906 g_free(status);
907 }
908
909 qapi_free_MigrationInfo(info);
910 }
911
912 void hmp_migrate(Monitor *mon, const QDict *qdict)
913 {
914 int detach = qdict_get_try_bool(qdict, "detach", 0);
915 int blk = qdict_get_try_bool(qdict, "blk", 0);
916 int inc = qdict_get_try_bool(qdict, "inc", 0);
917 const char *uri = qdict_get_str(qdict, "uri");
918 Error *err = NULL;
919
920 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
921 if (err) {
922 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
923 error_free(err);
924 return;
925 }
926
927 if (!detach) {
928 MigrationStatus *status;
929
930 if (monitor_suspend(mon) < 0) {
931 monitor_printf(mon, "terminal does not allow synchronous "
932 "migration, continuing detached\n");
933 return;
934 }
935
936 status = g_malloc0(sizeof(*status));
937 status->mon = mon;
938 status->is_block_migration = blk || inc;
939 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
940 status);
941 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
942 }
943 }
944
945 void hmp_device_del(Monitor *mon, const QDict *qdict)
946 {
947 const char *id = qdict_get_str(qdict, "id");
948 Error *err = NULL;
949
950 qmp_device_del(id, &err);
951 hmp_handle_error(mon, &err);
952 }
953
954 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
955 {
956 Error *errp = NULL;
957 int paging = qdict_get_try_bool(qdict, "paging", 0);
958 const char *file = qdict_get_str(qdict, "protocol");
959 bool has_begin = qdict_haskey(qdict, "begin");
960 bool has_length = qdict_haskey(qdict, "length");
961 int64_t begin = 0;
962 int64_t length = 0;
963
964 if (has_begin) {
965 begin = qdict_get_int(qdict, "begin");
966 }
967 if (has_length) {
968 length = qdict_get_int(qdict, "length");
969 }
970
971 qmp_dump_guest_memory(paging, file, has_begin, begin, has_length, length,
972 &errp);
973 hmp_handle_error(mon, &errp);
974 }
975
976 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
977 {
978 Error *err = NULL;
979 QemuOpts *opts;
980
981 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
982 if (error_is_set(&err)) {
983 goto out;
984 }
985
986 netdev_add(opts, &err);
987 if (error_is_set(&err)) {
988 qemu_opts_del(opts);
989 }
990
991 out:
992 hmp_handle_error(mon, &err);
993 }
994
995 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
996 {
997 const char *id = qdict_get_str(qdict, "id");
998 Error *err = NULL;
999
1000 qmp_netdev_del(id, &err);
1001 hmp_handle_error(mon, &err);
1002 }