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