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