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