]> git.proxmox.com Git - qemu.git/blob - hmp.c
block: Add support for throttling burst max in QMP and the command line.
[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/net.h"
18 #include "sysemu/char.h"
19 #include "qemu/option.h"
20 #include "qemu/timer.h"
21 #include "qmp-commands.h"
22 #include "qemu/sockets.h"
23 #include "monitor/monitor.h"
24 #include "ui/console.h"
25 #include "block/qapi.h"
26 #include "qemu-io.h"
27
28 static void hmp_handle_error(Monitor *mon, Error **errp)
29 {
30 if (error_is_set(errp)) {
31 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
32 error_free(*errp);
33 }
34 }
35
36 void hmp_info_name(Monitor *mon, const QDict *qdict)
37 {
38 NameInfo *info;
39
40 info = qmp_query_name(NULL);
41 if (info->has_name) {
42 monitor_printf(mon, "%s\n", info->name);
43 }
44 qapi_free_NameInfo(info);
45 }
46
47 void hmp_info_version(Monitor *mon, const QDict *qdict)
48 {
49 VersionInfo *info;
50
51 info = qmp_query_version(NULL);
52
53 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
54 info->qemu.major, info->qemu.minor, info->qemu.micro,
55 info->package);
56
57 qapi_free_VersionInfo(info);
58 }
59
60 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
61 {
62 KvmInfo *info;
63
64 info = qmp_query_kvm(NULL);
65 monitor_printf(mon, "kvm support: ");
66 if (info->present) {
67 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
68 } else {
69 monitor_printf(mon, "not compiled\n");
70 }
71
72 qapi_free_KvmInfo(info);
73 }
74
75 void hmp_info_status(Monitor *mon, const QDict *qdict)
76 {
77 StatusInfo *info;
78
79 info = qmp_query_status(NULL);
80
81 monitor_printf(mon, "VM status: %s%s",
82 info->running ? "running" : "paused",
83 info->singlestep ? " (single step mode)" : "");
84
85 if (!info->running && info->status != RUN_STATE_PAUSED) {
86 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
87 }
88
89 monitor_printf(mon, "\n");
90
91 qapi_free_StatusInfo(info);
92 }
93
94 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
95 {
96 UuidInfo *info;
97
98 info = qmp_query_uuid(NULL);
99 monitor_printf(mon, "%s\n", info->UUID);
100 qapi_free_UuidInfo(info);
101 }
102
103 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
104 {
105 ChardevInfoList *char_info, *info;
106
107 char_info = qmp_query_chardev(NULL);
108 for (info = char_info; info; info = info->next) {
109 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
110 info->value->filename);
111 }
112
113 qapi_free_ChardevInfoList(char_info);
114 }
115
116 void hmp_info_mice(Monitor *mon, const QDict *qdict)
117 {
118 MouseInfoList *mice_list, *mouse;
119
120 mice_list = qmp_query_mice(NULL);
121 if (!mice_list) {
122 monitor_printf(mon, "No mouse devices connected\n");
123 return;
124 }
125
126 for (mouse = mice_list; mouse; mouse = mouse->next) {
127 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
128 mouse->value->current ? '*' : ' ',
129 mouse->value->index, mouse->value->name,
130 mouse->value->absolute ? " (absolute)" : "");
131 }
132
133 qapi_free_MouseInfoList(mice_list);
134 }
135
136 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
137 {
138 MigrationInfo *info;
139 MigrationCapabilityStatusList *caps, *cap;
140
141 info = qmp_query_migrate(NULL);
142 caps = qmp_query_migrate_capabilities(NULL);
143
144 /* do not display parameters during setup */
145 if (info->has_status && caps) {
146 monitor_printf(mon, "capabilities: ");
147 for (cap = caps; cap; cap = cap->next) {
148 monitor_printf(mon, "%s: %s ",
149 MigrationCapability_lookup[cap->value->capability],
150 cap->value->state ? "on" : "off");
151 }
152 monitor_printf(mon, "\n");
153 }
154
155 if (info->has_status) {
156 monitor_printf(mon, "Migration status: %s\n", info->status);
157 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
158 info->total_time);
159 if (info->has_expected_downtime) {
160 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
161 info->expected_downtime);
162 }
163 if (info->has_downtime) {
164 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
165 info->downtime);
166 }
167 if (info->has_setup_time) {
168 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
169 info->setup_time);
170 }
171 }
172
173 if (info->has_ram) {
174 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
175 info->ram->transferred >> 10);
176 monitor_printf(mon, "throughput: %0.2f mbps\n",
177 info->ram->mbps);
178 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
179 info->ram->remaining >> 10);
180 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
181 info->ram->total >> 10);
182 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
183 info->ram->duplicate);
184 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
185 info->ram->skipped);
186 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
187 info->ram->normal);
188 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
189 info->ram->normal_bytes >> 10);
190 if (info->ram->dirty_pages_rate) {
191 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
192 info->ram->dirty_pages_rate);
193 }
194 }
195
196 if (info->has_disk) {
197 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
198 info->disk->transferred >> 10);
199 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
200 info->disk->remaining >> 10);
201 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
202 info->disk->total >> 10);
203 }
204
205 if (info->has_xbzrle_cache) {
206 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
207 info->xbzrle_cache->cache_size);
208 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
209 info->xbzrle_cache->bytes >> 10);
210 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
211 info->xbzrle_cache->pages);
212 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
213 info->xbzrle_cache->cache_miss);
214 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
215 info->xbzrle_cache->overflow);
216 }
217
218 qapi_free_MigrationInfo(info);
219 qapi_free_MigrationCapabilityStatusList(caps);
220 }
221
222 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
223 {
224 MigrationCapabilityStatusList *caps, *cap;
225
226 caps = qmp_query_migrate_capabilities(NULL);
227
228 if (caps) {
229 monitor_printf(mon, "capabilities: ");
230 for (cap = caps; cap; cap = cap->next) {
231 monitor_printf(mon, "%s: %s ",
232 MigrationCapability_lookup[cap->value->capability],
233 cap->value->state ? "on" : "off");
234 }
235 monitor_printf(mon, "\n");
236 }
237
238 qapi_free_MigrationCapabilityStatusList(caps);
239 }
240
241 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
242 {
243 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
244 qmp_query_migrate_cache_size(NULL) >> 10);
245 }
246
247 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
248 {
249 CpuInfoList *cpu_list, *cpu;
250
251 cpu_list = qmp_query_cpus(NULL);
252
253 for (cpu = cpu_list; cpu; cpu = cpu->next) {
254 int active = ' ';
255
256 if (cpu->value->CPU == monitor_get_cpu_index()) {
257 active = '*';
258 }
259
260 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
261
262 if (cpu->value->has_pc) {
263 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
264 }
265 if (cpu->value->has_nip) {
266 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
267 }
268 if (cpu->value->has_npc) {
269 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
270 }
271 if (cpu->value->has_PC) {
272 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
273 }
274
275 if (cpu->value->halted) {
276 monitor_printf(mon, " (halted)");
277 }
278
279 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
280 }
281
282 qapi_free_CpuInfoList(cpu_list);
283 }
284
285 void hmp_info_block(Monitor *mon, const QDict *qdict)
286 {
287 BlockInfoList *block_list, *info;
288 ImageInfo *image_info;
289 const char *device = qdict_get_try_str(qdict, "device");
290 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
291
292 block_list = qmp_query_block(NULL);
293
294 for (info = block_list; info; info = info->next) {
295 if (device && strcmp(device, info->value->device)) {
296 continue;
297 }
298
299 if (info != block_list) {
300 monitor_printf(mon, "\n");
301 }
302
303 monitor_printf(mon, "%s", info->value->device);
304 if (info->value->has_inserted) {
305 monitor_printf(mon, ": %s (%s%s%s)\n",
306 info->value->inserted->file,
307 info->value->inserted->drv,
308 info->value->inserted->ro ? ", read-only" : "",
309 info->value->inserted->encrypted ? ", encrypted" : "");
310 } else {
311 monitor_printf(mon, ": [not inserted]\n");
312 }
313
314 if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
315 monitor_printf(mon, " I/O status: %s\n",
316 BlockDeviceIoStatus_lookup[info->value->io_status]);
317 }
318
319 if (info->value->removable) {
320 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
321 info->value->locked ? "" : "not ",
322 info->value->tray_open ? "open" : "closed");
323 }
324
325
326 if (!info->value->has_inserted) {
327 continue;
328 }
329
330 if (info->value->inserted->has_backing_file) {
331 monitor_printf(mon,
332 " Backing file: %s "
333 "(chain depth: %" PRId64 ")\n",
334 info->value->inserted->backing_file,
335 info->value->inserted->backing_file_depth);
336 }
337
338 if (info->value->inserted->bps
339 || info->value->inserted->bps_rd
340 || info->value->inserted->bps_wr
341 || info->value->inserted->iops
342 || info->value->inserted->iops_rd
343 || info->value->inserted->iops_wr)
344 {
345 monitor_printf(mon, " I/O throttling: bps=%" PRId64
346 " bps_rd=%" PRId64 " bps_wr=%" PRId64
347 " bps_max=%" PRId64
348 " bps_rd_max=%" PRId64
349 " bps_wr_max=%" PRId64
350 " iops=%" PRId64 " iops_rd=%" PRId64
351 " iops_wr=%" PRId64
352 " iops_max=%" PRId64
353 " iops_rd_max=%" PRId64
354 " iops_wr_max=%" PRId64 "\n",
355 info->value->inserted->bps,
356 info->value->inserted->bps_rd,
357 info->value->inserted->bps_wr,
358 info->value->inserted->bps_max,
359 info->value->inserted->bps_rd_max,
360 info->value->inserted->bps_wr_max,
361 info->value->inserted->iops,
362 info->value->inserted->iops_rd,
363 info->value->inserted->iops_wr,
364 info->value->inserted->iops_max,
365 info->value->inserted->iops_rd_max,
366 info->value->inserted->iops_wr_max);
367 } else {
368 monitor_printf(mon, " [not inserted]");
369 }
370
371 if (verbose) {
372 monitor_printf(mon, "\nImages:\n");
373 image_info = info->value->inserted->image;
374 while (1) {
375 bdrv_image_info_dump((fprintf_function)monitor_printf,
376 mon, image_info);
377 if (image_info->has_backing_image) {
378 image_info = image_info->backing_image;
379 } else {
380 break;
381 }
382 }
383 }
384 }
385
386 qapi_free_BlockInfoList(block_list);
387 }
388
389 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
390 {
391 BlockStatsList *stats_list, *stats;
392
393 stats_list = qmp_query_blockstats(NULL);
394
395 for (stats = stats_list; stats; stats = stats->next) {
396 if (!stats->value->has_device) {
397 continue;
398 }
399
400 monitor_printf(mon, "%s:", stats->value->device);
401 monitor_printf(mon, " rd_bytes=%" PRId64
402 " wr_bytes=%" PRId64
403 " rd_operations=%" PRId64
404 " wr_operations=%" PRId64
405 " flush_operations=%" PRId64
406 " wr_total_time_ns=%" PRId64
407 " rd_total_time_ns=%" PRId64
408 " flush_total_time_ns=%" PRId64
409 "\n",
410 stats->value->stats->rd_bytes,
411 stats->value->stats->wr_bytes,
412 stats->value->stats->rd_operations,
413 stats->value->stats->wr_operations,
414 stats->value->stats->flush_operations,
415 stats->value->stats->wr_total_time_ns,
416 stats->value->stats->rd_total_time_ns,
417 stats->value->stats->flush_total_time_ns);
418 }
419
420 qapi_free_BlockStatsList(stats_list);
421 }
422
423 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
424 {
425 VncInfo *info;
426 Error *err = NULL;
427 VncClientInfoList *client;
428
429 info = qmp_query_vnc(&err);
430 if (err) {
431 monitor_printf(mon, "%s\n", error_get_pretty(err));
432 error_free(err);
433 return;
434 }
435
436 if (!info->enabled) {
437 monitor_printf(mon, "Server: disabled\n");
438 goto out;
439 }
440
441 monitor_printf(mon, "Server:\n");
442 if (info->has_host && info->has_service) {
443 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
444 }
445 if (info->has_auth) {
446 monitor_printf(mon, " auth: %s\n", info->auth);
447 }
448
449 if (!info->has_clients || info->clients == NULL) {
450 monitor_printf(mon, "Client: none\n");
451 } else {
452 for (client = info->clients; client; client = client->next) {
453 monitor_printf(mon, "Client:\n");
454 monitor_printf(mon, " address: %s:%s\n",
455 client->value->host, client->value->service);
456 monitor_printf(mon, " x509_dname: %s\n",
457 client->value->x509_dname ?
458 client->value->x509_dname : "none");
459 monitor_printf(mon, " username: %s\n",
460 client->value->has_sasl_username ?
461 client->value->sasl_username : "none");
462 }
463 }
464
465 out:
466 qapi_free_VncInfo(info);
467 }
468
469 void hmp_info_spice(Monitor *mon, const QDict *qdict)
470 {
471 SpiceChannelList *chan;
472 SpiceInfo *info;
473
474 info = qmp_query_spice(NULL);
475
476 if (!info->enabled) {
477 monitor_printf(mon, "Server: disabled\n");
478 goto out;
479 }
480
481 monitor_printf(mon, "Server:\n");
482 if (info->has_port) {
483 monitor_printf(mon, " address: %s:%" PRId64 "\n",
484 info->host, info->port);
485 }
486 if (info->has_tls_port) {
487 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
488 info->host, info->tls_port);
489 }
490 monitor_printf(mon, " migrated: %s\n",
491 info->migrated ? "true" : "false");
492 monitor_printf(mon, " auth: %s\n", info->auth);
493 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
494 monitor_printf(mon, " mouse-mode: %s\n",
495 SpiceQueryMouseMode_lookup[info->mouse_mode]);
496
497 if (!info->has_channels || info->channels == NULL) {
498 monitor_printf(mon, "Channels: none\n");
499 } else {
500 for (chan = info->channels; chan; chan = chan->next) {
501 monitor_printf(mon, "Channel:\n");
502 monitor_printf(mon, " address: %s:%s%s\n",
503 chan->value->host, chan->value->port,
504 chan->value->tls ? " [tls]" : "");
505 monitor_printf(mon, " session: %" PRId64 "\n",
506 chan->value->connection_id);
507 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
508 chan->value->channel_type, chan->value->channel_id);
509 }
510 }
511
512 out:
513 qapi_free_SpiceInfo(info);
514 }
515
516 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
517 {
518 BalloonInfo *info;
519 Error *err = NULL;
520
521 info = qmp_query_balloon(&err);
522 if (err) {
523 monitor_printf(mon, "%s\n", error_get_pretty(err));
524 error_free(err);
525 return;
526 }
527
528 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
529
530 qapi_free_BalloonInfo(info);
531 }
532
533 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
534 {
535 PciMemoryRegionList *region;
536
537 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
538 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
539 dev->slot, dev->function);
540 monitor_printf(mon, " ");
541
542 if (dev->class_info.has_desc) {
543 monitor_printf(mon, "%s", dev->class_info.desc);
544 } else {
545 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
546 }
547
548 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
549 dev->id.vendor, dev->id.device);
550
551 if (dev->has_irq) {
552 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
553 }
554
555 if (dev->has_pci_bridge) {
556 monitor_printf(mon, " BUS %" PRId64 ".\n",
557 dev->pci_bridge->bus.number);
558 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
559 dev->pci_bridge->bus.secondary);
560 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
561 dev->pci_bridge->bus.subordinate);
562
563 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
564 dev->pci_bridge->bus.io_range->base,
565 dev->pci_bridge->bus.io_range->limit);
566
567 monitor_printf(mon,
568 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
569 dev->pci_bridge->bus.memory_range->base,
570 dev->pci_bridge->bus.memory_range->limit);
571
572 monitor_printf(mon, " prefetchable memory range "
573 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
574 dev->pci_bridge->bus.prefetchable_range->base,
575 dev->pci_bridge->bus.prefetchable_range->limit);
576 }
577
578 for (region = dev->regions; region; region = region->next) {
579 uint64_t addr, size;
580
581 addr = region->value->address;
582 size = region->value->size;
583
584 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
585
586 if (!strcmp(region->value->type, "io")) {
587 monitor_printf(mon, "I/O at 0x%04" PRIx64
588 " [0x%04" PRIx64 "].\n",
589 addr, addr + size - 1);
590 } else {
591 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
592 " [0x%08" PRIx64 "].\n",
593 region->value->mem_type_64 ? 64 : 32,
594 region->value->prefetch ? " prefetchable" : "",
595 addr, addr + size - 1);
596 }
597 }
598
599 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
600
601 if (dev->has_pci_bridge) {
602 if (dev->pci_bridge->has_devices) {
603 PciDeviceInfoList *cdev;
604 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
605 hmp_info_pci_device(mon, cdev->value);
606 }
607 }
608 }
609 }
610
611 void hmp_info_pci(Monitor *mon, const QDict *qdict)
612 {
613 PciInfoList *info_list, *info;
614 Error *err = NULL;
615
616 info_list = qmp_query_pci(&err);
617 if (err) {
618 monitor_printf(mon, "PCI devices not supported\n");
619 error_free(err);
620 return;
621 }
622
623 for (info = info_list; info; info = info->next) {
624 PciDeviceInfoList *dev;
625
626 for (dev = info->value->devices; dev; dev = dev->next) {
627 hmp_info_pci_device(mon, dev->value);
628 }
629 }
630
631 qapi_free_PciInfoList(info_list);
632 }
633
634 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
635 {
636 BlockJobInfoList *list;
637 Error *err = NULL;
638
639 list = qmp_query_block_jobs(&err);
640 assert(!err);
641
642 if (!list) {
643 monitor_printf(mon, "No active jobs\n");
644 return;
645 }
646
647 while (list) {
648 if (strcmp(list->value->type, "stream") == 0) {
649 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
650 " of %" PRId64 " bytes, speed limit %" PRId64
651 " bytes/s\n",
652 list->value->device,
653 list->value->offset,
654 list->value->len,
655 list->value->speed);
656 } else {
657 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
658 " of %" PRId64 " bytes, speed limit %" PRId64
659 " bytes/s\n",
660 list->value->type,
661 list->value->device,
662 list->value->offset,
663 list->value->len,
664 list->value->speed);
665 }
666 list = list->next;
667 }
668 }
669
670 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
671 {
672 TPMInfoList *info_list, *info;
673 Error *err = NULL;
674 unsigned int c = 0;
675 TPMPassthroughOptions *tpo;
676
677 info_list = qmp_query_tpm(&err);
678 if (err) {
679 monitor_printf(mon, "TPM device not supported\n");
680 error_free(err);
681 return;
682 }
683
684 if (info_list) {
685 monitor_printf(mon, "TPM device:\n");
686 }
687
688 for (info = info_list; info; info = info->next) {
689 TPMInfo *ti = info->value;
690 monitor_printf(mon, " tpm%d: model=%s\n",
691 c, TpmModel_lookup[ti->model]);
692
693 monitor_printf(mon, " \\ %s: type=%s",
694 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
695
696 switch (ti->options->kind) {
697 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
698 tpo = ti->options->passthrough;
699 monitor_printf(mon, "%s%s%s%s",
700 tpo->has_path ? ",path=" : "",
701 tpo->has_path ? tpo->path : "",
702 tpo->has_cancel_path ? ",cancel-path=" : "",
703 tpo->has_cancel_path ? tpo->cancel_path : "");
704 break;
705 case TPM_TYPE_OPTIONS_KIND_MAX:
706 break;
707 }
708 monitor_printf(mon, "\n");
709 c++;
710 }
711 qapi_free_TPMInfoList(info_list);
712 }
713
714 void hmp_quit(Monitor *mon, const QDict *qdict)
715 {
716 monitor_suspend(mon);
717 qmp_quit(NULL);
718 }
719
720 void hmp_stop(Monitor *mon, const QDict *qdict)
721 {
722 qmp_stop(NULL);
723 }
724
725 void hmp_system_reset(Monitor *mon, const QDict *qdict)
726 {
727 qmp_system_reset(NULL);
728 }
729
730 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
731 {
732 qmp_system_powerdown(NULL);
733 }
734
735 void hmp_cpu(Monitor *mon, const QDict *qdict)
736 {
737 int64_t cpu_index;
738
739 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
740 use it are converted to the QAPI */
741 cpu_index = qdict_get_int(qdict, "index");
742 if (monitor_set_cpu(cpu_index) < 0) {
743 monitor_printf(mon, "invalid CPU index\n");
744 }
745 }
746
747 void hmp_memsave(Monitor *mon, const QDict *qdict)
748 {
749 uint32_t size = qdict_get_int(qdict, "size");
750 const char *filename = qdict_get_str(qdict, "filename");
751 uint64_t addr = qdict_get_int(qdict, "val");
752 Error *errp = NULL;
753
754 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
755 hmp_handle_error(mon, &errp);
756 }
757
758 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
759 {
760 uint32_t size = qdict_get_int(qdict, "size");
761 const char *filename = qdict_get_str(qdict, "filename");
762 uint64_t addr = qdict_get_int(qdict, "val");
763 Error *errp = NULL;
764
765 qmp_pmemsave(addr, size, filename, &errp);
766 hmp_handle_error(mon, &errp);
767 }
768
769 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
770 {
771 const char *chardev = qdict_get_str(qdict, "device");
772 const char *data = qdict_get_str(qdict, "data");
773 Error *errp = NULL;
774
775 qmp_ringbuf_write(chardev, data, false, 0, &errp);
776
777 hmp_handle_error(mon, &errp);
778 }
779
780 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
781 {
782 uint32_t size = qdict_get_int(qdict, "size");
783 const char *chardev = qdict_get_str(qdict, "device");
784 char *data;
785 Error *errp = NULL;
786 int i;
787
788 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
789 if (errp) {
790 monitor_printf(mon, "%s\n", error_get_pretty(errp));
791 error_free(errp);
792 return;
793 }
794
795 for (i = 0; data[i]; i++) {
796 unsigned char ch = data[i];
797
798 if (ch == '\\') {
799 monitor_printf(mon, "\\\\");
800 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
801 monitor_printf(mon, "\\u%04X", ch);
802 } else {
803 monitor_printf(mon, "%c", ch);
804 }
805
806 }
807 monitor_printf(mon, "\n");
808 g_free(data);
809 }
810
811 static void hmp_cont_cb(void *opaque, int err)
812 {
813 if (!err) {
814 qmp_cont(NULL);
815 }
816 }
817
818 static bool key_is_missing(const BlockInfo *bdev)
819 {
820 return (bdev->inserted && bdev->inserted->encryption_key_missing);
821 }
822
823 void hmp_cont(Monitor *mon, const QDict *qdict)
824 {
825 BlockInfoList *bdev_list, *bdev;
826 Error *errp = NULL;
827
828 bdev_list = qmp_query_block(NULL);
829 for (bdev = bdev_list; bdev; bdev = bdev->next) {
830 if (key_is_missing(bdev->value)) {
831 monitor_read_block_device_key(mon, bdev->value->device,
832 hmp_cont_cb, NULL);
833 goto out;
834 }
835 }
836
837 qmp_cont(&errp);
838 hmp_handle_error(mon, &errp);
839
840 out:
841 qapi_free_BlockInfoList(bdev_list);
842 }
843
844 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
845 {
846 qmp_system_wakeup(NULL);
847 }
848
849 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
850 {
851 Error *errp = NULL;
852
853 qmp_inject_nmi(&errp);
854 hmp_handle_error(mon, &errp);
855 }
856
857 void hmp_set_link(Monitor *mon, const QDict *qdict)
858 {
859 const char *name = qdict_get_str(qdict, "name");
860 int up = qdict_get_bool(qdict, "up");
861 Error *errp = NULL;
862
863 qmp_set_link(name, up, &errp);
864 hmp_handle_error(mon, &errp);
865 }
866
867 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
868 {
869 const char *device = qdict_get_str(qdict, "device");
870 const char *password = qdict_get_str(qdict, "password");
871 Error *errp = NULL;
872
873 qmp_block_passwd(device, password, &errp);
874 hmp_handle_error(mon, &errp);
875 }
876
877 void hmp_balloon(Monitor *mon, const QDict *qdict)
878 {
879 int64_t value = qdict_get_int(qdict, "value");
880 Error *errp = NULL;
881
882 qmp_balloon(value, &errp);
883 if (error_is_set(&errp)) {
884 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
885 error_free(errp);
886 }
887 }
888
889 void hmp_block_resize(Monitor *mon, const QDict *qdict)
890 {
891 const char *device = qdict_get_str(qdict, "device");
892 int64_t size = qdict_get_int(qdict, "size");
893 Error *errp = NULL;
894
895 qmp_block_resize(device, size, &errp);
896 hmp_handle_error(mon, &errp);
897 }
898
899 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
900 {
901 const char *device = qdict_get_str(qdict, "device");
902 const char *filename = qdict_get_str(qdict, "target");
903 const char *format = qdict_get_try_str(qdict, "format");
904 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
905 int full = qdict_get_try_bool(qdict, "full", 0);
906 enum NewImageMode mode;
907 Error *errp = NULL;
908
909 if (!filename) {
910 error_set(&errp, QERR_MISSING_PARAMETER, "target");
911 hmp_handle_error(mon, &errp);
912 return;
913 }
914
915 if (reuse) {
916 mode = NEW_IMAGE_MODE_EXISTING;
917 } else {
918 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
919 }
920
921 qmp_drive_mirror(device, filename, !!format, format,
922 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
923 true, mode, false, 0, false, 0, false, 0,
924 false, 0, false, 0, &errp);
925 hmp_handle_error(mon, &errp);
926 }
927
928 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
929 {
930 const char *device = qdict_get_str(qdict, "device");
931 const char *filename = qdict_get_str(qdict, "target");
932 const char *format = qdict_get_try_str(qdict, "format");
933 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
934 int full = qdict_get_try_bool(qdict, "full", 0);
935 enum NewImageMode mode;
936 Error *errp = NULL;
937
938 if (!filename) {
939 error_set(&errp, QERR_MISSING_PARAMETER, "target");
940 hmp_handle_error(mon, &errp);
941 return;
942 }
943
944 if (reuse) {
945 mode = NEW_IMAGE_MODE_EXISTING;
946 } else {
947 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
948 }
949
950 qmp_drive_backup(device, filename, !!format, format,
951 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
952 true, mode, false, 0, false, 0, false, 0, &errp);
953 hmp_handle_error(mon, &errp);
954 }
955
956 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
957 {
958 const char *device = qdict_get_str(qdict, "device");
959 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
960 const char *format = qdict_get_try_str(qdict, "format");
961 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
962 enum NewImageMode mode;
963 Error *errp = NULL;
964
965 if (!filename) {
966 /* In the future, if 'snapshot-file' is not specified, the snapshot
967 will be taken internally. Today it's actually required. */
968 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
969 hmp_handle_error(mon, &errp);
970 return;
971 }
972
973 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
974 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
975 true, mode, &errp);
976 hmp_handle_error(mon, &errp);
977 }
978
979 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
980 {
981 qmp_migrate_cancel(NULL);
982 }
983
984 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
985 {
986 double value = qdict_get_double(qdict, "value");
987 qmp_migrate_set_downtime(value, NULL);
988 }
989
990 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
991 {
992 int64_t value = qdict_get_int(qdict, "value");
993 Error *err = NULL;
994
995 qmp_migrate_set_cache_size(value, &err);
996 if (err) {
997 monitor_printf(mon, "%s\n", error_get_pretty(err));
998 error_free(err);
999 return;
1000 }
1001 }
1002
1003 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1004 {
1005 int64_t value = qdict_get_int(qdict, "value");
1006 qmp_migrate_set_speed(value, NULL);
1007 }
1008
1009 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1010 {
1011 const char *cap = qdict_get_str(qdict, "capability");
1012 bool state = qdict_get_bool(qdict, "state");
1013 Error *err = NULL;
1014 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1015 int i;
1016
1017 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1018 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1019 caps->value = g_malloc0(sizeof(*caps->value));
1020 caps->value->capability = i;
1021 caps->value->state = state;
1022 caps->next = NULL;
1023 qmp_migrate_set_capabilities(caps, &err);
1024 break;
1025 }
1026 }
1027
1028 if (i == MIGRATION_CAPABILITY_MAX) {
1029 error_set(&err, QERR_INVALID_PARAMETER, cap);
1030 }
1031
1032 qapi_free_MigrationCapabilityStatusList(caps);
1033
1034 if (err) {
1035 monitor_printf(mon, "migrate_set_capability: %s\n",
1036 error_get_pretty(err));
1037 error_free(err);
1038 }
1039 }
1040
1041 void hmp_set_password(Monitor *mon, const QDict *qdict)
1042 {
1043 const char *protocol = qdict_get_str(qdict, "protocol");
1044 const char *password = qdict_get_str(qdict, "password");
1045 const char *connected = qdict_get_try_str(qdict, "connected");
1046 Error *err = NULL;
1047
1048 qmp_set_password(protocol, password, !!connected, connected, &err);
1049 hmp_handle_error(mon, &err);
1050 }
1051
1052 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1053 {
1054 const char *protocol = qdict_get_str(qdict, "protocol");
1055 const char *whenstr = qdict_get_str(qdict, "time");
1056 Error *err = NULL;
1057
1058 qmp_expire_password(protocol, whenstr, &err);
1059 hmp_handle_error(mon, &err);
1060 }
1061
1062 void hmp_eject(Monitor *mon, const QDict *qdict)
1063 {
1064 int force = qdict_get_try_bool(qdict, "force", 0);
1065 const char *device = qdict_get_str(qdict, "device");
1066 Error *err = NULL;
1067
1068 qmp_eject(device, true, force, &err);
1069 hmp_handle_error(mon, &err);
1070 }
1071
1072 static void hmp_change_read_arg(Monitor *mon, const char *password,
1073 void *opaque)
1074 {
1075 qmp_change_vnc_password(password, NULL);
1076 monitor_read_command(mon, 1);
1077 }
1078
1079 void hmp_change(Monitor *mon, const QDict *qdict)
1080 {
1081 const char *device = qdict_get_str(qdict, "device");
1082 const char *target = qdict_get_str(qdict, "target");
1083 const char *arg = qdict_get_try_str(qdict, "arg");
1084 Error *err = NULL;
1085
1086 if (strcmp(device, "vnc") == 0 &&
1087 (strcmp(target, "passwd") == 0 ||
1088 strcmp(target, "password") == 0)) {
1089 if (!arg) {
1090 monitor_read_password(mon, hmp_change_read_arg, NULL);
1091 return;
1092 }
1093 }
1094
1095 qmp_change(device, target, !!arg, arg, &err);
1096 if (error_is_set(&err) &&
1097 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1098 error_free(err);
1099 monitor_read_block_device_key(mon, device, NULL, NULL);
1100 return;
1101 }
1102 hmp_handle_error(mon, &err);
1103 }
1104
1105 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1106 {
1107 Error *err = NULL;
1108
1109 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1110 qdict_get_int(qdict, "bps"),
1111 qdict_get_int(qdict, "bps_rd"),
1112 qdict_get_int(qdict, "bps_wr"),
1113 qdict_get_int(qdict, "iops"),
1114 qdict_get_int(qdict, "iops_rd"),
1115 qdict_get_int(qdict, "iops_wr"),
1116 false, /* no burst max via HMP */
1117 0,
1118 false,
1119 0,
1120 false,
1121 0,
1122 false,
1123 0,
1124 false,
1125 0,
1126 false,
1127 0, &err);
1128 hmp_handle_error(mon, &err);
1129 }
1130
1131 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1132 {
1133 Error *error = NULL;
1134 const char *device = qdict_get_str(qdict, "device");
1135 const char *base = qdict_get_try_str(qdict, "base");
1136 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1137
1138 qmp_block_stream(device, base != NULL, base,
1139 qdict_haskey(qdict, "speed"), speed,
1140 BLOCKDEV_ON_ERROR_REPORT, true, &error);
1141
1142 hmp_handle_error(mon, &error);
1143 }
1144
1145 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1146 {
1147 Error *error = NULL;
1148 const char *device = qdict_get_str(qdict, "device");
1149 int64_t value = qdict_get_int(qdict, "speed");
1150
1151 qmp_block_job_set_speed(device, value, &error);
1152
1153 hmp_handle_error(mon, &error);
1154 }
1155
1156 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1157 {
1158 Error *error = NULL;
1159 const char *device = qdict_get_str(qdict, "device");
1160 bool force = qdict_get_try_bool(qdict, "force", 0);
1161
1162 qmp_block_job_cancel(device, true, force, &error);
1163
1164 hmp_handle_error(mon, &error);
1165 }
1166
1167 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1168 {
1169 Error *error = NULL;
1170 const char *device = qdict_get_str(qdict, "device");
1171
1172 qmp_block_job_pause(device, &error);
1173
1174 hmp_handle_error(mon, &error);
1175 }
1176
1177 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1178 {
1179 Error *error = NULL;
1180 const char *device = qdict_get_str(qdict, "device");
1181
1182 qmp_block_job_resume(device, &error);
1183
1184 hmp_handle_error(mon, &error);
1185 }
1186
1187 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1188 {
1189 Error *error = NULL;
1190 const char *device = qdict_get_str(qdict, "device");
1191
1192 qmp_block_job_complete(device, &error);
1193
1194 hmp_handle_error(mon, &error);
1195 }
1196
1197 typedef struct MigrationStatus
1198 {
1199 QEMUTimer *timer;
1200 Monitor *mon;
1201 bool is_block_migration;
1202 } MigrationStatus;
1203
1204 static void hmp_migrate_status_cb(void *opaque)
1205 {
1206 MigrationStatus *status = opaque;
1207 MigrationInfo *info;
1208
1209 info = qmp_query_migrate(NULL);
1210 if (!info->has_status || strcmp(info->status, "active") == 0) {
1211 if (info->has_disk) {
1212 int progress;
1213
1214 if (info->disk->remaining) {
1215 progress = info->disk->transferred * 100 / info->disk->total;
1216 } else {
1217 progress = 100;
1218 }
1219
1220 monitor_printf(status->mon, "Completed %d %%\r", progress);
1221 monitor_flush(status->mon);
1222 }
1223
1224 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1225 } else {
1226 if (status->is_block_migration) {
1227 monitor_printf(status->mon, "\n");
1228 }
1229 monitor_resume(status->mon);
1230 timer_del(status->timer);
1231 g_free(status);
1232 }
1233
1234 qapi_free_MigrationInfo(info);
1235 }
1236
1237 void hmp_migrate(Monitor *mon, const QDict *qdict)
1238 {
1239 int detach = qdict_get_try_bool(qdict, "detach", 0);
1240 int blk = qdict_get_try_bool(qdict, "blk", 0);
1241 int inc = qdict_get_try_bool(qdict, "inc", 0);
1242 const char *uri = qdict_get_str(qdict, "uri");
1243 Error *err = NULL;
1244
1245 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1246 if (err) {
1247 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1248 error_free(err);
1249 return;
1250 }
1251
1252 if (!detach) {
1253 MigrationStatus *status;
1254
1255 if (monitor_suspend(mon) < 0) {
1256 monitor_printf(mon, "terminal does not allow synchronous "
1257 "migration, continuing detached\n");
1258 return;
1259 }
1260
1261 status = g_malloc0(sizeof(*status));
1262 status->mon = mon;
1263 status->is_block_migration = blk || inc;
1264 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1265 status);
1266 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1267 }
1268 }
1269
1270 void hmp_device_del(Monitor *mon, const QDict *qdict)
1271 {
1272 const char *id = qdict_get_str(qdict, "id");
1273 Error *err = NULL;
1274
1275 qmp_device_del(id, &err);
1276 hmp_handle_error(mon, &err);
1277 }
1278
1279 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1280 {
1281 Error *errp = NULL;
1282 int paging = qdict_get_try_bool(qdict, "paging", 0);
1283 const char *file = qdict_get_str(qdict, "filename");
1284 bool has_begin = qdict_haskey(qdict, "begin");
1285 bool has_length = qdict_haskey(qdict, "length");
1286 int64_t begin = 0;
1287 int64_t length = 0;
1288 char *prot;
1289
1290 if (has_begin) {
1291 begin = qdict_get_int(qdict, "begin");
1292 }
1293 if (has_length) {
1294 length = qdict_get_int(qdict, "length");
1295 }
1296
1297 prot = g_strconcat("file:", file, NULL);
1298
1299 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1300 &errp);
1301 hmp_handle_error(mon, &errp);
1302 g_free(prot);
1303 }
1304
1305 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1306 {
1307 Error *err = NULL;
1308 QemuOpts *opts;
1309
1310 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1311 if (error_is_set(&err)) {
1312 goto out;
1313 }
1314
1315 netdev_add(opts, &err);
1316 if (error_is_set(&err)) {
1317 qemu_opts_del(opts);
1318 }
1319
1320 out:
1321 hmp_handle_error(mon, &err);
1322 }
1323
1324 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1325 {
1326 const char *id = qdict_get_str(qdict, "id");
1327 Error *err = NULL;
1328
1329 qmp_netdev_del(id, &err);
1330 hmp_handle_error(mon, &err);
1331 }
1332
1333 void hmp_getfd(Monitor *mon, const QDict *qdict)
1334 {
1335 const char *fdname = qdict_get_str(qdict, "fdname");
1336 Error *errp = NULL;
1337
1338 qmp_getfd(fdname, &errp);
1339 hmp_handle_error(mon, &errp);
1340 }
1341
1342 void hmp_closefd(Monitor *mon, const QDict *qdict)
1343 {
1344 const char *fdname = qdict_get_str(qdict, "fdname");
1345 Error *errp = NULL;
1346
1347 qmp_closefd(fdname, &errp);
1348 hmp_handle_error(mon, &errp);
1349 }
1350
1351 void hmp_send_key(Monitor *mon, const QDict *qdict)
1352 {
1353 const char *keys = qdict_get_str(qdict, "keys");
1354 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1355 int has_hold_time = qdict_haskey(qdict, "hold-time");
1356 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1357 Error *err = NULL;
1358 char keyname_buf[16];
1359 char *separator;
1360 int keyname_len;
1361
1362 while (1) {
1363 separator = strchr(keys, '-');
1364 keyname_len = separator ? separator - keys : strlen(keys);
1365 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1366
1367 /* Be compatible with old interface, convert user inputted "<" */
1368 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1369 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1370 keyname_len = 4;
1371 }
1372 keyname_buf[keyname_len] = 0;
1373
1374 keylist = g_malloc0(sizeof(*keylist));
1375 keylist->value = g_malloc0(sizeof(*keylist->value));
1376
1377 if (!head) {
1378 head = keylist;
1379 }
1380 if (tmp) {
1381 tmp->next = keylist;
1382 }
1383 tmp = keylist;
1384
1385 if (strstart(keyname_buf, "0x", NULL)) {
1386 char *endp;
1387 int value = strtoul(keyname_buf, &endp, 0);
1388 if (*endp != '\0') {
1389 goto err_out;
1390 }
1391 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1392 keylist->value->number = value;
1393 } else {
1394 int idx = index_from_key(keyname_buf);
1395 if (idx == Q_KEY_CODE_MAX) {
1396 goto err_out;
1397 }
1398 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1399 keylist->value->qcode = idx;
1400 }
1401
1402 if (!separator) {
1403 break;
1404 }
1405 keys = separator + 1;
1406 }
1407
1408 qmp_send_key(head, has_hold_time, hold_time, &err);
1409 hmp_handle_error(mon, &err);
1410
1411 out:
1412 qapi_free_KeyValueList(head);
1413 return;
1414
1415 err_out:
1416 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1417 goto out;
1418 }
1419
1420 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1421 {
1422 const char *filename = qdict_get_str(qdict, "filename");
1423 Error *err = NULL;
1424
1425 qmp_screendump(filename, &err);
1426 hmp_handle_error(mon, &err);
1427 }
1428
1429 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1430 {
1431 const char *uri = qdict_get_str(qdict, "uri");
1432 int writable = qdict_get_try_bool(qdict, "writable", 0);
1433 int all = qdict_get_try_bool(qdict, "all", 0);
1434 Error *local_err = NULL;
1435 BlockInfoList *block_list, *info;
1436 SocketAddress *addr;
1437
1438 if (writable && !all) {
1439 error_setg(&local_err, "-w only valid together with -a");
1440 goto exit;
1441 }
1442
1443 /* First check if the address is valid and start the server. */
1444 addr = socket_parse(uri, &local_err);
1445 if (local_err != NULL) {
1446 goto exit;
1447 }
1448
1449 qmp_nbd_server_start(addr, &local_err);
1450 qapi_free_SocketAddress(addr);
1451 if (local_err != NULL) {
1452 goto exit;
1453 }
1454
1455 if (!all) {
1456 return;
1457 }
1458
1459 /* Then try adding all block devices. If one fails, close all and
1460 * exit.
1461 */
1462 block_list = qmp_query_block(NULL);
1463
1464 for (info = block_list; info; info = info->next) {
1465 if (!info->value->has_inserted) {
1466 continue;
1467 }
1468
1469 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1470
1471 if (local_err != NULL) {
1472 qmp_nbd_server_stop(NULL);
1473 break;
1474 }
1475 }
1476
1477 qapi_free_BlockInfoList(block_list);
1478
1479 exit:
1480 hmp_handle_error(mon, &local_err);
1481 }
1482
1483 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1484 {
1485 const char *device = qdict_get_str(qdict, "device");
1486 int writable = qdict_get_try_bool(qdict, "writable", 0);
1487 Error *local_err = NULL;
1488
1489 qmp_nbd_server_add(device, true, writable, &local_err);
1490
1491 if (local_err != NULL) {
1492 hmp_handle_error(mon, &local_err);
1493 }
1494 }
1495
1496 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1497 {
1498 Error *errp = NULL;
1499
1500 qmp_nbd_server_stop(&errp);
1501 hmp_handle_error(mon, &errp);
1502 }
1503
1504 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1505 {
1506 const char *args = qdict_get_str(qdict, "args");
1507 Error *err = NULL;
1508 QemuOpts *opts;
1509
1510 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1511 if (opts == NULL) {
1512 error_setg(&err, "Parsing chardev args failed");
1513 } else {
1514 qemu_chr_new_from_opts(opts, NULL, &err);
1515 }
1516 hmp_handle_error(mon, &err);
1517 }
1518
1519 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1520 {
1521 Error *local_err = NULL;
1522
1523 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1524 hmp_handle_error(mon, &local_err);
1525 }
1526
1527 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1528 {
1529 BlockDriverState *bs;
1530 const char* device = qdict_get_str(qdict, "device");
1531 const char* command = qdict_get_str(qdict, "command");
1532 Error *err = NULL;
1533
1534 bs = bdrv_find(device);
1535 if (bs) {
1536 qemuio_command(bs, command);
1537 } else {
1538 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1539 }
1540
1541 hmp_handle_error(mon, &err);
1542 }