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