]> git.proxmox.com Git - mirror_qemu.git/blame - hmp.c
tpm: Clean up model registration & lookup
[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
d38ea87a 16#include "qemu/osdep.h"
48a32bed 17#include "hmp.h"
1422e32d 18#include "net/net.h"
fafa4d50 19#include "net/eth.h"
8228e353 20#include "chardev/char.h"
4c7b7e9b 21#include "sysemu/block-backend.h"
52b26205 22#include "sysemu/sysemu.h"
213dcb06 23#include "qemu/config-file.h"
1de7afc9
PB
24#include "qemu/option.h"
25#include "qemu/timer.h"
48a32bed 26#include "qmp-commands.h"
1de7afc9 27#include "qemu/sockets.h"
83c9089e 28#include "monitor/monitor.h"
318660f8 29#include "monitor/qdev.h"
cff8b2c6 30#include "qapi/opts-visitor.h"
cc7a8ea7 31#include "qapi/qmp/qerror.h"
f4a06d13 32#include "qapi/string-input-visitor.h"
eb1539b2 33#include "qapi/string-output-visitor.h"
baead0ab 34#include "qapi/util.h"
eb1539b2 35#include "qapi-visit.h"
90998d58 36#include "qom/object_interfaces.h"
28ecbaee 37#include "ui/console.h"
bd269ebc 38#include "block/nbd.h"
bd093a36 39#include "block/qapi.h"
587da2c3 40#include "qemu-io.h"
f348b6d1 41#include "qemu/cutils.h"
d59ce6f3 42#include "qemu/error-report.h"
be9b23c4 43#include "exec/ramlist.h"
61b97833 44#include "hw/intc/intc.h"
5e22479a 45#include "migration/snapshot.h"
9d18af93 46#include "migration/misc.h"
48a32bed 47
22fa7da0
CR
48#ifdef CONFIG_SPICE
49#include <spice/enums.h>
50#endif
51
0cfd6a9a
LC
52static void hmp_handle_error(Monitor *mon, Error **errp)
53{
415168e0
MA
54 assert(errp);
55 if (*errp) {
193227f9 56 error_report_err(*errp);
0cfd6a9a
LC
57 }
58}
59
84f2d0ea 60void hmp_info_name(Monitor *mon, const QDict *qdict)
48a32bed
AL
61{
62 NameInfo *info;
63
64 info = qmp_query_name(NULL);
65 if (info->has_name) {
66 monitor_printf(mon, "%s\n", info->name);
67 }
68 qapi_free_NameInfo(info);
69}
b9c15f16 70
84f2d0ea 71void hmp_info_version(Monitor *mon, const QDict *qdict)
b9c15f16
LC
72{
73 VersionInfo *info;
74
75 info = qmp_query_version(NULL);
76
77 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
4752cdbb 78 info->qemu->major, info->qemu->minor, info->qemu->micro,
b9c15f16
LC
79 info->package);
80
81 qapi_free_VersionInfo(info);
82}
292a2602 83
84f2d0ea 84void hmp_info_kvm(Monitor *mon, const QDict *qdict)
292a2602
LC
85{
86 KvmInfo *info;
87
88 info = qmp_query_kvm(NULL);
89 monitor_printf(mon, "kvm support: ");
90 if (info->present) {
91 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
92 } else {
93 monitor_printf(mon, "not compiled\n");
94 }
95
96 qapi_free_KvmInfo(info);
97}
98
84f2d0ea 99void hmp_info_status(Monitor *mon, const QDict *qdict)
1fa9a5e4
LC
100{
101 StatusInfo *info;
102
103 info = qmp_query_status(NULL);
104
105 monitor_printf(mon, "VM status: %s%s",
106 info->running ? "running" : "paused",
107 info->singlestep ? " (single step mode)" : "");
108
109 if (!info->running && info->status != RUN_STATE_PAUSED) {
110 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
111 }
112
113 monitor_printf(mon, "\n");
114
115 qapi_free_StatusInfo(info);
116}
117
84f2d0ea 118void hmp_info_uuid(Monitor *mon, const QDict *qdict)
efab767e
LC
119{
120 UuidInfo *info;
121
122 info = qmp_query_uuid(NULL);
123 monitor_printf(mon, "%s\n", info->UUID);
124 qapi_free_UuidInfo(info);
125}
c5a415a0 126
84f2d0ea 127void hmp_info_chardev(Monitor *mon, const QDict *qdict)
c5a415a0
LC
128{
129 ChardevInfoList *char_info, *info;
130
131 char_info = qmp_query_chardev(NULL);
132 for (info = char_info; info; info = info->next) {
133 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
134 info->value->filename);
135 }
136
137 qapi_free_ChardevInfoList(char_info);
138}
7a7f325e 139
84f2d0ea 140void hmp_info_mice(Monitor *mon, const QDict *qdict)
e235cec3
LC
141{
142 MouseInfoList *mice_list, *mouse;
143
144 mice_list = qmp_query_mice(NULL);
145 if (!mice_list) {
146 monitor_printf(mon, "No mouse devices connected\n");
147 return;
148 }
149
150 for (mouse = mice_list; mouse; mouse = mouse->next) {
151 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
152 mouse->value->current ? '*' : ' ',
153 mouse->value->index, mouse->value->name,
154 mouse->value->absolute ? " (absolute)" : "");
155 }
156
157 qapi_free_MouseInfoList(mice_list);
158}
159
84f2d0ea 160void hmp_info_migrate(Monitor *mon, const QDict *qdict)
791e7c82
LC
161{
162 MigrationInfo *info;
bbf6da32 163 MigrationCapabilityStatusList *caps, *cap;
791e7c82
LC
164
165 info = qmp_query_migrate(NULL);
bbf6da32
OW
166 caps = qmp_query_migrate_capabilities(NULL);
167
9d18af93
PX
168 migration_global_dump(mon);
169
bbf6da32
OW
170 /* do not display parameters during setup */
171 if (info->has_status && caps) {
172 monitor_printf(mon, "capabilities: ");
173 for (cap = caps; cap; cap = cap->next) {
174 monitor_printf(mon, "%s: %s ",
175 MigrationCapability_lookup[cap->value->capability],
176 cap->value->state ? "on" : "off");
177 }
178 monitor_printf(mon, "\n");
179 }
791e7c82
LC
180
181 if (info->has_status) {
d59ce6f3 182 monitor_printf(mon, "Migration status: %s",
24b8c39b 183 MigrationStatus_lookup[info->status]);
d59ce6f3
DB
184 if (info->status == MIGRATION_STATUS_FAILED &&
185 info->has_error_desc) {
186 monitor_printf(mon, " (%s)\n", info->error_desc);
187 } else {
188 monitor_printf(mon, "\n");
189 }
190
7aa939af
JQ
191 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
192 info->total_time);
2c52ddf1
JQ
193 if (info->has_expected_downtime) {
194 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
195 info->expected_downtime);
196 }
9c5a9fcf
JQ
197 if (info->has_downtime) {
198 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
199 info->downtime);
200 }
ed4fbd10
MH
201 if (info->has_setup_time) {
202 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
203 info->setup_time);
204 }
791e7c82
LC
205 }
206
207 if (info->has_ram) {
208 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
209 info->ram->transferred >> 10);
7e114f8c
MH
210 monitor_printf(mon, "throughput: %0.2f mbps\n",
211 info->ram->mbps);
791e7c82
LC
212 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
213 info->ram->remaining >> 10);
214 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
215 info->ram->total >> 10);
004d4c10
OW
216 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
217 info->ram->duplicate);
f1c72795
PL
218 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
219 info->ram->skipped);
004d4c10
OW
220 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
221 info->ram->normal);
222 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
223 info->ram->normal_bytes >> 10);
58570ed8
C
224 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
225 info->ram->dirty_sync_count);
030ce1f8
CF
226 monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
227 info->ram->page_size >> 10);
228
8d017193
JQ
229 if (info->ram->dirty_pages_rate) {
230 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
231 info->ram->dirty_pages_rate);
232 }
d3bf5418
DDAG
233 if (info->ram->postcopy_requests) {
234 monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
235 info->ram->postcopy_requests);
236 }
791e7c82
LC
237 }
238
239 if (info->has_disk) {
240 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
241 info->disk->transferred >> 10);
242 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
243 info->disk->remaining >> 10);
244 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
245 info->disk->total >> 10);
246 }
247
f36d55af
OW
248 if (info->has_xbzrle_cache) {
249 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
250 info->xbzrle_cache->cache_size);
251 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
252 info->xbzrle_cache->bytes >> 10);
253 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
254 info->xbzrle_cache->pages);
255 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
256 info->xbzrle_cache->cache_miss);
8bc39233
C
257 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
258 info->xbzrle_cache->cache_miss_rate);
f36d55af
OW
259 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
260 info->xbzrle_cache->overflow);
261 }
262
d85a31d1 263 if (info->has_cpu_throttle_percentage) {
4782893e 264 monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
d85a31d1 265 info->cpu_throttle_percentage);
4782893e
JH
266 }
267
791e7c82 268 qapi_free_MigrationInfo(info);
bbf6da32
OW
269 qapi_free_MigrationCapabilityStatusList(caps);
270}
271
84f2d0ea 272void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
bbf6da32
OW
273{
274 MigrationCapabilityStatusList *caps, *cap;
275
276 caps = qmp_query_migrate_capabilities(NULL);
277
278 if (caps) {
bbf6da32 279 for (cap = caps; cap; cap = cap->next) {
d80a0169 280 monitor_printf(mon, "%s: %s\n",
bbf6da32
OW
281 MigrationCapability_lookup[cap->value->capability],
282 cap->value->state ? "on" : "off");
283 }
bbf6da32
OW
284 }
285
286 qapi_free_MigrationCapabilityStatusList(caps);
791e7c82
LC
287}
288
50e9a629
LL
289void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
290{
291 MigrationParameters *params;
292
293 params = qmp_query_migrate_parameters(NULL);
294
295 if (params) {
de63ab61 296 assert(params->has_compress_level);
2c02468c 297 monitor_printf(mon, "%s: %" PRId64 "\n",
50e9a629
LL
298 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
299 params->compress_level);
de63ab61 300 assert(params->has_compress_threads);
2c02468c 301 monitor_printf(mon, "%s: %" PRId64 "\n",
50e9a629
LL
302 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
303 params->compress_threads);
de63ab61 304 assert(params->has_decompress_threads);
2c02468c 305 monitor_printf(mon, "%s: %" PRId64 "\n",
50e9a629
LL
306 MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
307 params->decompress_threads);
de63ab61 308 assert(params->has_cpu_throttle_initial);
2c02468c 309 monitor_printf(mon, "%s: %" PRId64 "\n",
d85a31d1
JH
310 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL],
311 params->cpu_throttle_initial);
de63ab61 312 assert(params->has_cpu_throttle_increment);
2c02468c 313 monitor_printf(mon, "%s: %" PRId64 "\n",
d85a31d1
JH
314 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT],
315 params->cpu_throttle_increment);
8cc99dcd 316 assert(params->has_tls_creds);
2c02468c 317 monitor_printf(mon, "%s: '%s'\n",
69ef1f36 318 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_CREDS],
8cc99dcd
MA
319 params->tls_creds);
320 assert(params->has_tls_hostname);
2c02468c 321 monitor_printf(mon, "%s: '%s'\n",
69ef1f36 322 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_HOSTNAME],
8cc99dcd 323 params->tls_hostname);
2ff30257 324 assert(params->has_max_bandwidth);
2c02468c 325 monitor_printf(mon, "%s: %" PRId64 " bytes/second\n",
2ff30257
AA
326 MigrationParameter_lookup[MIGRATION_PARAMETER_MAX_BANDWIDTH],
327 params->max_bandwidth);
328 assert(params->has_downtime_limit);
2c02468c 329 monitor_printf(mon, "%s: %" PRId64 " milliseconds\n",
2ff30257
AA
330 MigrationParameter_lookup[MIGRATION_PARAMETER_DOWNTIME_LIMIT],
331 params->downtime_limit);
fe39a4d4 332 assert(params->has_x_checkpoint_delay);
2c02468c 333 monitor_printf(mon, "%s: %" PRId64 "\n",
68b53591
HZ
334 MigrationParameter_lookup[MIGRATION_PARAMETER_X_CHECKPOINT_DELAY],
335 params->x_checkpoint_delay);
2833c59b
JQ
336 assert(params->has_block_incremental);
337 monitor_printf(mon, "%s: %s\n",
338 MigrationParameter_lookup[MIGRATION_PARAMETER_BLOCK_INCREMENTAL],
339 params->block_incremental ? "on" : "off");
50e9a629
LL
340 }
341
342 qapi_free_MigrationParameters(params);
343}
344
84f2d0ea 345void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
9e1ba4cc
OW
346{
347 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
348 qmp_query_migrate_cache_size(NULL) >> 10);
349}
350
84f2d0ea 351void hmp_info_cpus(Monitor *mon, const QDict *qdict)
de0b36b6
LC
352{
353 CpuInfoList *cpu_list, *cpu;
354
355 cpu_list = qmp_query_cpus(NULL);
356
357 for (cpu = cpu_list; cpu; cpu = cpu->next) {
358 int active = ' ';
359
360 if (cpu->value->CPU == monitor_get_cpu_index()) {
361 active = '*';
362 }
363
852bef0e 364 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
de0b36b6 365
86f4b687
EB
366 switch (cpu->value->arch) {
367 case CPU_INFO_ARCH_X86:
544a3731 368 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->u.x86.pc);
86f4b687
EB
369 break;
370 case CPU_INFO_ARCH_PPC:
544a3731 371 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->u.ppc.nip);
86f4b687
EB
372 break;
373 case CPU_INFO_ARCH_SPARC:
86ae1911 374 monitor_printf(mon, " pc=0x%016" PRIx64,
544a3731 375 cpu->value->u.q_sparc.pc);
86ae1911 376 monitor_printf(mon, " npc=0x%016" PRIx64,
544a3731 377 cpu->value->u.q_sparc.npc);
86f4b687
EB
378 break;
379 case CPU_INFO_ARCH_MIPS:
544a3731 380 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.q_mips.PC);
86f4b687
EB
381 break;
382 case CPU_INFO_ARCH_TRICORE:
544a3731 383 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.tricore.PC);
86f4b687
EB
384 break;
385 default:
386 break;
de0b36b6
LC
387 }
388
389 if (cpu->value->halted) {
390 monitor_printf(mon, " (halted)");
391 }
392
393 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
394 }
395
396 qapi_free_CpuInfoList(cpu_list);
397}
398
289b276c
KW
399static void print_block_info(Monitor *mon, BlockInfo *info,
400 BlockDeviceInfo *inserted, bool verbose)
b2023818 401{
bd093a36 402 ImageInfo *image_info;
b2023818 403
8d6adccd
KW
404 assert(!info || !info->has_inserted || info->inserted == inserted);
405
ec18b0a9 406 if (info && *info->device) {
8d6adccd
KW
407 monitor_printf(mon, "%s", info->device);
408 if (inserted && inserted->has_node_name) {
409 monitor_printf(mon, " (%s)", inserted->node_name);
410 }
411 } else {
ec18b0a9 412 assert(info || inserted);
8d6adccd 413 monitor_printf(mon, "%s",
ec18b0a9
KW
414 inserted && inserted->has_node_name ? inserted->node_name
415 : info && info->has_qdev ? info->qdev
8d6adccd
KW
416 : "<anonymous>");
417 }
418
289b276c
KW
419 if (inserted) {
420 monitor_printf(mon, ": %s (%s%s%s)\n",
421 inserted->file,
422 inserted->drv,
423 inserted->ro ? ", read-only" : "",
424 inserted->encrypted ? ", encrypted" : "");
425 } else {
426 monitor_printf(mon, ": [not inserted]\n");
427 }
b2023818 428
8d6adccd 429 if (info) {
46eade7b
KW
430 if (info->has_qdev) {
431 monitor_printf(mon, " Attached to: %s\n", info->qdev);
432 }
8d6adccd
KW
433 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
434 monitor_printf(mon, " I/O status: %s\n",
435 BlockDeviceIoStatus_lookup[info->io_status]);
436 }
b2023818 437
8d6adccd
KW
438 if (info->removable) {
439 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
440 info->locked ? "" : "not ",
441 info->tray_open ? "open" : "closed");
442 }
289b276c 443 }
fbe2e26c 444
b2023818 445
289b276c
KW
446 if (!inserted) {
447 return;
448 }
b2023818 449
289b276c
KW
450 monitor_printf(mon, " Cache mode: %s%s%s\n",
451 inserted->cache->writeback ? "writeback" : "writethrough",
452 inserted->cache->direct ? ", direct" : "",
453 inserted->cache->no_flush ? ", ignore flushes" : "");
454
455 if (inserted->has_backing_file) {
456 monitor_printf(mon,
457 " Backing file: %s "
458 "(chain depth: %" PRId64 ")\n",
459 inserted->backing_file,
460 inserted->backing_file_depth);
461 }
727f005e 462
289b276c
KW
463 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
464 monitor_printf(mon, " Detect zeroes: %s\n",
465 BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
466 }
fbe2e26c 467
289b276c
KW
468 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
469 inserted->iops || inserted->iops_rd || inserted->iops_wr)
470 {
471 monitor_printf(mon, " I/O throttling: bps=%" PRId64
472 " bps_rd=%" PRId64 " bps_wr=%" PRId64
473 " bps_max=%" PRId64
474 " bps_rd_max=%" PRId64
475 " bps_wr_max=%" PRId64
476 " iops=%" PRId64 " iops_rd=%" PRId64
477 " iops_wr=%" PRId64
478 " iops_max=%" PRId64
479 " iops_rd_max=%" PRId64
480 " iops_wr_max=%" PRId64
b8fe1694
AG
481 " iops_size=%" PRId64
482 " group=%s\n",
289b276c
KW
483 inserted->bps,
484 inserted->bps_rd,
485 inserted->bps_wr,
486 inserted->bps_max,
487 inserted->bps_rd_max,
488 inserted->bps_wr_max,
489 inserted->iops,
490 inserted->iops_rd,
491 inserted->iops_wr,
492 inserted->iops_max,
493 inserted->iops_rd_max,
494 inserted->iops_wr_max,
b8fe1694
AG
495 inserted->iops_size,
496 inserted->group);
289b276c 497 }
fbe2e26c 498
9419874f 499 if (verbose) {
289b276c
KW
500 monitor_printf(mon, "\nImages:\n");
501 image_info = inserted->image;
502 while (1) {
503 bdrv_image_info_dump((fprintf_function)monitor_printf,
504 mon, image_info);
505 if (image_info->has_backing_image) {
506 image_info = image_info->backing_image;
507 } else {
508 break;
509 }
510 }
511 }
512}
9e193c5a 513
289b276c
KW
514void hmp_info_block(Monitor *mon, const QDict *qdict)
515{
516 BlockInfoList *block_list, *info;
e6bb31ec 517 BlockDeviceInfoList *blockdev_list, *blockdev;
289b276c 518 const char *device = qdict_get_try_str(qdict, "device");
34acbc95
EB
519 bool verbose = qdict_get_try_bool(qdict, "verbose", false);
520 bool nodes = qdict_get_try_bool(qdict, "nodes", false);
e6bb31ec 521 bool printed = false;
9e193c5a 522
e6bb31ec
KW
523 /* Print BlockBackend information */
524 if (!nodes) {
f19e44bc 525 block_list = qmp_query_block(NULL);
e6bb31ec
KW
526 } else {
527 block_list = NULL;
528 }
fbe2e26c 529
289b276c
KW
530 for (info = block_list; info; info = info->next) {
531 if (device && strcmp(device, info->value->device)) {
532 continue;
465bee1d
PL
533 }
534
289b276c
KW
535 if (info != block_list) {
536 monitor_printf(mon, "\n");
fbe2e26c 537 }
bd093a36 538
289b276c
KW
539 print_block_info(mon, info->value, info->value->has_inserted
540 ? info->value->inserted : NULL,
541 verbose);
e6bb31ec 542 printed = true;
b2023818
LC
543 }
544
545 qapi_free_BlockInfoList(block_list);
e6bb31ec
KW
546
547 if ((!device && !nodes) || printed) {
548 return;
549 }
550
551 /* Print node information */
552 blockdev_list = qmp_query_named_block_nodes(NULL);
553 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
554 assert(blockdev->value->has_node_name);
555 if (device && strcmp(device, blockdev->value->node_name)) {
556 continue;
557 }
558
559 if (blockdev != blockdev_list) {
560 monitor_printf(mon, "\n");
561 }
562
563 print_block_info(mon, NULL, blockdev->value, verbose);
564 }
565 qapi_free_BlockDeviceInfoList(blockdev_list);
b2023818
LC
566}
567
84f2d0ea 568void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
f11f57e4
LC
569{
570 BlockStatsList *stats_list, *stats;
571
f71eaa74 572 stats_list = qmp_query_blockstats(false, false, NULL);
f11f57e4
LC
573
574 for (stats = stats_list; stats; stats = stats->next) {
575 if (!stats->value->has_device) {
576 continue;
577 }
578
579 monitor_printf(mon, "%s:", stats->value->device);
580 monitor_printf(mon, " rd_bytes=%" PRId64
581 " wr_bytes=%" PRId64
582 " rd_operations=%" PRId64
583 " wr_operations=%" PRId64
584 " flush_operations=%" PRId64
585 " wr_total_time_ns=%" PRId64
586 " rd_total_time_ns=%" PRId64
587 " flush_total_time_ns=%" PRId64
f4564d53
PL
588 " rd_merged=%" PRId64
589 " wr_merged=%" PRId64
cb38fffb 590 " idle_time_ns=%" PRId64
f11f57e4
LC
591 "\n",
592 stats->value->stats->rd_bytes,
593 stats->value->stats->wr_bytes,
594 stats->value->stats->rd_operations,
595 stats->value->stats->wr_operations,
596 stats->value->stats->flush_operations,
597 stats->value->stats->wr_total_time_ns,
598 stats->value->stats->rd_total_time_ns,
f4564d53
PL
599 stats->value->stats->flush_total_time_ns,
600 stats->value->stats->rd_merged,
cb38fffb
AG
601 stats->value->stats->wr_merged,
602 stats->value->stats->idle_time_ns);
f11f57e4
LC
603 }
604
605 qapi_free_BlockStatsList(stats_list);
606}
607
0a9667ec
DDAG
608/* Helper for hmp_info_vnc_clients, _servers */
609static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
610 const char *name)
611{
612 monitor_printf(mon, " %s: %s:%s (%s%s)\n",
613 name,
614 info->host,
615 info->service,
616 NetworkAddressFamily_lookup[info->family],
617 info->websocket ? " (Websocket)" : "");
618}
619
620/* Helper displaying and auth and crypt info */
621static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
622 VncPrimaryAuth auth,
623 VncVencryptSubAuth *vencrypt)
624{
625 monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
626 VncPrimaryAuth_lookup[auth],
627 vencrypt ? VncVencryptSubAuth_lookup[*vencrypt] : "none");
628}
629
630static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
631{
632 while (client) {
633 VncClientInfo *cinfo = client->value;
634
635 hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
636 monitor_printf(mon, " x509_dname: %s\n",
637 cinfo->has_x509_dname ?
638 cinfo->x509_dname : "none");
639 monitor_printf(mon, " sasl_username: %s\n",
640 cinfo->has_sasl_username ?
641 cinfo->sasl_username : "none");
642
643 client = client->next;
644 }
645}
646
647static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
648{
649 while (server) {
650 VncServerInfo2 *sinfo = server->value;
651 hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
652 hmp_info_vnc_authcrypt(mon, " ", sinfo->auth,
653 sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
654 server = server->next;
655 }
656}
657
84f2d0ea 658void hmp_info_vnc(Monitor *mon, const QDict *qdict)
2b54aa87 659{
0a9667ec 660 VncInfo2List *info2l;
2b54aa87 661 Error *err = NULL;
2b54aa87 662
0a9667ec 663 info2l = qmp_query_vnc_servers(&err);
2b54aa87 664 if (err) {
193227f9 665 error_report_err(err);
2b54aa87
LC
666 return;
667 }
0a9667ec
DDAG
668 if (!info2l) {
669 monitor_printf(mon, "None\n");
670 return;
2b54aa87
LC
671 }
672
0a9667ec
DDAG
673 while (info2l) {
674 VncInfo2 *info = info2l->value;
675 monitor_printf(mon, "%s:\n", info->id);
676 hmp_info_vnc_servers(mon, info->server);
677 hmp_info_vnc_clients(mon, info->clients);
678 if (!info->server) {
679 /* The server entry displays its auth, we only
680 * need to display in the case of 'reverse' connections
681 * where there's no server.
682 */
683 hmp_info_vnc_authcrypt(mon, " ", info->auth,
684 info->has_vencrypt ? &info->vencrypt : NULL);
685 }
686 if (info->has_display) {
687 monitor_printf(mon, " Display: %s\n", info->display);
2b54aa87 688 }
0a9667ec 689 info2l = info2l->next;
2b54aa87
LC
690 }
691
0a9667ec
DDAG
692 qapi_free_VncInfo2List(info2l);
693
2b54aa87
LC
694}
695
206addd5 696#ifdef CONFIG_SPICE
84f2d0ea 697void hmp_info_spice(Monitor *mon, const QDict *qdict)
d1f29646
LC
698{
699 SpiceChannelList *chan;
700 SpiceInfo *info;
22fa7da0
CR
701 const char *channel_name;
702 const char * const channel_names[] = {
703 [SPICE_CHANNEL_MAIN] = "main",
704 [SPICE_CHANNEL_DISPLAY] = "display",
705 [SPICE_CHANNEL_INPUTS] = "inputs",
706 [SPICE_CHANNEL_CURSOR] = "cursor",
707 [SPICE_CHANNEL_PLAYBACK] = "playback",
708 [SPICE_CHANNEL_RECORD] = "record",
709 [SPICE_CHANNEL_TUNNEL] = "tunnel",
710 [SPICE_CHANNEL_SMARTCARD] = "smartcard",
711 [SPICE_CHANNEL_USBREDIR] = "usbredir",
712 [SPICE_CHANNEL_PORT] = "port",
7c6044a9
GH
713#if 0
714 /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
715 * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
716 * as quick fix for build failures with older versions. */
22fa7da0 717 [SPICE_CHANNEL_WEBDAV] = "webdav",
7c6044a9 718#endif
22fa7da0 719 };
d1f29646
LC
720
721 info = qmp_query_spice(NULL);
722
723 if (!info->enabled) {
724 monitor_printf(mon, "Server: disabled\n");
725 goto out;
726 }
727
728 monitor_printf(mon, "Server:\n");
729 if (info->has_port) {
730 monitor_printf(mon, " address: %s:%" PRId64 "\n",
731 info->host, info->port);
732 }
733 if (info->has_tls_port) {
734 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
735 info->host, info->tls_port);
736 }
61c4efe2
YH
737 monitor_printf(mon, " migrated: %s\n",
738 info->migrated ? "true" : "false");
d1f29646
LC
739 monitor_printf(mon, " auth: %s\n", info->auth);
740 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
4efee029
AL
741 monitor_printf(mon, " mouse-mode: %s\n",
742 SpiceQueryMouseMode_lookup[info->mouse_mode]);
d1f29646
LC
743
744 if (!info->has_channels || info->channels == NULL) {
745 monitor_printf(mon, "Channels: none\n");
746 } else {
747 for (chan = info->channels; chan; chan = chan->next) {
748 monitor_printf(mon, "Channel:\n");
749 monitor_printf(mon, " address: %s:%s%s\n",
ddf21908 750 chan->value->host, chan->value->port,
d1f29646
LC
751 chan->value->tls ? " [tls]" : "");
752 monitor_printf(mon, " session: %" PRId64 "\n",
753 chan->value->connection_id);
754 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
755 chan->value->channel_type, chan->value->channel_id);
22fa7da0
CR
756
757 channel_name = "unknown";
758 if (chan->value->channel_type > 0 &&
759 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
760 channel_names[chan->value->channel_type]) {
761 channel_name = channel_names[chan->value->channel_type];
762 }
763
764 monitor_printf(mon, " channel name: %s\n", channel_name);
d1f29646
LC
765 }
766 }
767
768out:
769 qapi_free_SpiceInfo(info);
770}
206addd5 771#endif
d1f29646 772
84f2d0ea 773void hmp_info_balloon(Monitor *mon, const QDict *qdict)
96637bcd
LC
774{
775 BalloonInfo *info;
776 Error *err = NULL;
777
778 info = qmp_query_balloon(&err);
779 if (err) {
193227f9 780 error_report_err(err);
96637bcd
LC
781 return;
782 }
783
01ceb97e 784 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
96637bcd
LC
785
786 qapi_free_BalloonInfo(info);
787}
788
79627472
LC
789static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
790{
791 PciMemoryRegionList *region;
792
793 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
794 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
795 dev->slot, dev->function);
796 monitor_printf(mon, " ");
797
9fa02cd1
EB
798 if (dev->class_info->has_desc) {
799 monitor_printf(mon, "%s", dev->class_info->desc);
79627472 800 } else {
9fa02cd1 801 monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
79627472
LC
802 }
803
804 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
9fa02cd1 805 dev->id->vendor, dev->id->device);
79627472
LC
806
807 if (dev->has_irq) {
808 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
809 }
810
811 if (dev->has_pci_bridge) {
812 monitor_printf(mon, " BUS %" PRId64 ".\n",
9fa02cd1 813 dev->pci_bridge->bus->number);
79627472 814 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
9fa02cd1 815 dev->pci_bridge->bus->secondary);
79627472 816 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
9fa02cd1 817 dev->pci_bridge->bus->subordinate);
79627472
LC
818
819 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
9fa02cd1
EB
820 dev->pci_bridge->bus->io_range->base,
821 dev->pci_bridge->bus->io_range->limit);
79627472
LC
822
823 monitor_printf(mon,
824 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
9fa02cd1
EB
825 dev->pci_bridge->bus->memory_range->base,
826 dev->pci_bridge->bus->memory_range->limit);
79627472
LC
827
828 monitor_printf(mon, " prefetchable memory range "
829 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
9fa02cd1
EB
830 dev->pci_bridge->bus->prefetchable_range->base,
831 dev->pci_bridge->bus->prefetchable_range->limit);
79627472
LC
832 }
833
834 for (region = dev->regions; region; region = region->next) {
835 uint64_t addr, size;
836
837 addr = region->value->address;
838 size = region->value->size;
839
840 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
841
842 if (!strcmp(region->value->type, "io")) {
843 monitor_printf(mon, "I/O at 0x%04" PRIx64
844 " [0x%04" PRIx64 "].\n",
845 addr, addr + size - 1);
846 } else {
847 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
848 " [0x%08" PRIx64 "].\n",
849 region->value->mem_type_64 ? 64 : 32,
850 region->value->prefetch ? " prefetchable" : "",
851 addr, addr + size - 1);
852 }
853 }
854
855 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
856
857 if (dev->has_pci_bridge) {
858 if (dev->pci_bridge->has_devices) {
859 PciDeviceInfoList *cdev;
860 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
861 hmp_info_pci_device(mon, cdev->value);
862 }
863 }
864 }
865}
866
61b97833
HP
867static int hmp_info_irq_foreach(Object *obj, void *opaque)
868{
869 InterruptStatsProvider *intc;
870 InterruptStatsProviderClass *k;
871 Monitor *mon = opaque;
872
873 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
874 intc = INTERRUPT_STATS_PROVIDER(obj);
875 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
876 uint64_t *irq_counts;
877 unsigned int nb_irqs, i;
878 if (k->get_statistics &&
879 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
880 if (nb_irqs > 0) {
881 monitor_printf(mon, "IRQ statistics for %s:\n",
882 object_get_typename(obj));
883 for (i = 0; i < nb_irqs; i++) {
884 if (irq_counts[i] > 0) {
885 monitor_printf(mon, "%2d: %" PRId64 "\n", i,
886 irq_counts[i]);
887 }
888 }
889 }
890 } else {
891 monitor_printf(mon, "IRQ statistics not available for %s.\n",
892 object_get_typename(obj));
893 }
894 }
895
896 return 0;
897}
898
899void hmp_info_irq(Monitor *mon, const QDict *qdict)
900{
901 object_child_foreach_recursive(object_get_root(),
902 hmp_info_irq_foreach, mon);
903}
904
905static int hmp_info_pic_foreach(Object *obj, void *opaque)
906{
907 InterruptStatsProvider *intc;
908 InterruptStatsProviderClass *k;
909 Monitor *mon = opaque;
910
911 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
912 intc = INTERRUPT_STATS_PROVIDER(obj);
913 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
914 if (k->print_info) {
915 k->print_info(intc, mon);
916 } else {
917 monitor_printf(mon, "Interrupt controller information not available for %s.\n",
918 object_get_typename(obj));
919 }
920 }
921
922 return 0;
923}
924
925void hmp_info_pic(Monitor *mon, const QDict *qdict)
926{
927 object_child_foreach_recursive(object_get_root(),
928 hmp_info_pic_foreach, mon);
929}
930
84f2d0ea 931void hmp_info_pci(Monitor *mon, const QDict *qdict)
79627472 932{
f46cee37 933 PciInfoList *info_list, *info;
79627472
LC
934 Error *err = NULL;
935
f46cee37 936 info_list = qmp_query_pci(&err);
79627472
LC
937 if (err) {
938 monitor_printf(mon, "PCI devices not supported\n");
939 error_free(err);
940 return;
941 }
942
f46cee37 943 for (info = info_list; info; info = info->next) {
79627472
LC
944 PciDeviceInfoList *dev;
945
946 for (dev = info->value->devices; dev; dev = dev->next) {
947 hmp_info_pci_device(mon, dev->value);
948 }
949 }
950
f46cee37 951 qapi_free_PciInfoList(info_list);
79627472
LC
952}
953
84f2d0ea 954void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
fb5458cd
SH
955{
956 BlockJobInfoList *list;
957 Error *err = NULL;
958
959 list = qmp_query_block_jobs(&err);
960 assert(!err);
961
962 if (!list) {
963 monitor_printf(mon, "No active jobs\n");
964 return;
965 }
966
967 while (list) {
968 if (strcmp(list->value->type, "stream") == 0) {
969 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
970 " of %" PRId64 " bytes, speed limit %" PRId64
971 " bytes/s\n",
972 list->value->device,
973 list->value->offset,
974 list->value->len,
975 list->value->speed);
976 } else {
977 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
978 " of %" PRId64 " bytes, speed limit %" PRId64
979 " bytes/s\n",
980 list->value->type,
981 list->value->device,
982 list->value->offset,
983 list->value->len,
984 list->value->speed);
985 }
986 list = list->next;
987 }
93bb1315
GA
988
989 qapi_free_BlockJobInfoList(list);
fb5458cd
SH
990}
991
d1a0cf73
SB
992void hmp_info_tpm(Monitor *mon, const QDict *qdict)
993{
994 TPMInfoList *info_list, *info;
995 Error *err = NULL;
996 unsigned int c = 0;
997 TPMPassthroughOptions *tpo;
998
999 info_list = qmp_query_tpm(&err);
1000 if (err) {
1001 monitor_printf(mon, "TPM device not supported\n");
1002 error_free(err);
1003 return;
1004 }
1005
1006 if (info_list) {
1007 monitor_printf(mon, "TPM device:\n");
1008 }
1009
1010 for (info = info_list; info; info = info->next) {
1011 TPMInfo *ti = info->value;
1012 monitor_printf(mon, " tpm%d: model=%s\n",
1013 c, TpmModel_lookup[ti->model]);
1014
1015 monitor_printf(mon, " \\ %s: type=%s",
ce21131a 1016 ti->id, TpmTypeOptionsKind_lookup[ti->options->type]);
d1a0cf73 1017
ce21131a 1018 switch (ti->options->type) {
88ca7bcf 1019 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
32bafa8f 1020 tpo = ti->options->u.passthrough.data;
d1a0cf73
SB
1021 monitor_printf(mon, "%s%s%s%s",
1022 tpo->has_path ? ",path=" : "",
1023 tpo->has_path ? tpo->path : "",
1024 tpo->has_cancel_path ? ",cancel-path=" : "",
1025 tpo->has_cancel_path ? tpo->cancel_path : "");
1026 break;
7fb1cf16 1027 case TPM_TYPE_OPTIONS_KIND__MAX:
d1a0cf73
SB
1028 break;
1029 }
1030 monitor_printf(mon, "\n");
1031 c++;
1032 }
1033 qapi_free_TPMInfoList(info_list);
1034}
1035
7a7f325e
LC
1036void hmp_quit(Monitor *mon, const QDict *qdict)
1037{
1038 monitor_suspend(mon);
1039 qmp_quit(NULL);
1040}
5f158f21
LC
1041
1042void hmp_stop(Monitor *mon, const QDict *qdict)
1043{
1044 qmp_stop(NULL);
1045}
38d22653
LC
1046
1047void hmp_system_reset(Monitor *mon, const QDict *qdict)
1048{
1049 qmp_system_reset(NULL);
1050}
5bc465e4
LC
1051
1052void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1053{
1054 qmp_system_powerdown(NULL);
1055}
755f1968
LC
1056
1057void hmp_cpu(Monitor *mon, const QDict *qdict)
1058{
1059 int64_t cpu_index;
1060
1061 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1062 use it are converted to the QAPI */
1063 cpu_index = qdict_get_int(qdict, "index");
1064 if (monitor_set_cpu(cpu_index) < 0) {
1065 monitor_printf(mon, "invalid CPU index\n");
1066 }
1067}
0cfd6a9a
LC
1068
1069void hmp_memsave(Monitor *mon, const QDict *qdict)
1070{
1071 uint32_t size = qdict_get_int(qdict, "size");
1072 const char *filename = qdict_get_str(qdict, "filename");
1073 uint64_t addr = qdict_get_int(qdict, "val");
e940f543 1074 Error *err = NULL;
854e67fe 1075 int cpu_index = monitor_get_cpu_index();
0cfd6a9a 1076
854e67fe
TH
1077 if (cpu_index < 0) {
1078 monitor_printf(mon, "No CPU available\n");
1079 return;
1080 }
1081
1082 qmp_memsave(addr, size, filename, true, cpu_index, &err);
e940f543 1083 hmp_handle_error(mon, &err);
0cfd6a9a 1084}
6d3962bf
LC
1085
1086void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1087{
1088 uint32_t size = qdict_get_int(qdict, "size");
1089 const char *filename = qdict_get_str(qdict, "filename");
1090 uint64_t addr = qdict_get_int(qdict, "val");
e940f543 1091 Error *err = NULL;
6d3962bf 1092
e940f543
MA
1093 qmp_pmemsave(addr, size, filename, &err);
1094 hmp_handle_error(mon, &err);
1f590cf9
LL
1095}
1096
3949e594 1097void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1f590cf9 1098{
1f590cf9
LL
1099 const char *chardev = qdict_get_str(qdict, "device");
1100 const char *data = qdict_get_str(qdict, "data");
e940f543 1101 Error *err = NULL;
1f590cf9 1102
e940f543 1103 qmp_ringbuf_write(chardev, data, false, 0, &err);
1f590cf9 1104
e940f543 1105 hmp_handle_error(mon, &err);
6d3962bf 1106}
e42e818b 1107
3949e594 1108void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
49b6d722
LL
1109{
1110 uint32_t size = qdict_get_int(qdict, "size");
1111 const char *chardev = qdict_get_str(qdict, "device");
3ab651fc 1112 char *data;
e940f543 1113 Error *err = NULL;
543f3412 1114 int i;
49b6d722 1115
e940f543
MA
1116 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1117 if (err) {
193227f9 1118 error_report_err(err);
49b6d722
LL
1119 return;
1120 }
1121
543f3412
MA
1122 for (i = 0; data[i]; i++) {
1123 unsigned char ch = data[i];
1124
1125 if (ch == '\\') {
1126 monitor_printf(mon, "\\\\");
1127 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1128 monitor_printf(mon, "\\u%04X", ch);
1129 } else {
1130 monitor_printf(mon, "%c", ch);
1131 }
1132
1133 }
1134 monitor_printf(mon, "\n");
3ab651fc 1135 g_free(data);
49b6d722
LL
1136}
1137
e42e818b
LC
1138void hmp_cont(Monitor *mon, const QDict *qdict)
1139{
e940f543 1140 Error *err = NULL;
e42e818b 1141
e940f543
MA
1142 qmp_cont(&err);
1143 hmp_handle_error(mon, &err);
e42e818b 1144}
ab49ab5c 1145
9b9df25a
GH
1146void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1147{
1148 qmp_system_wakeup(NULL);
1149}
1150
3e5a50d6 1151void hmp_nmi(Monitor *mon, const QDict *qdict)
ab49ab5c 1152{
e940f543 1153 Error *err = NULL;
ab49ab5c 1154
e940f543
MA
1155 qmp_inject_nmi(&err);
1156 hmp_handle_error(mon, &err);
ab49ab5c 1157}
4b37156c
LC
1158
1159void hmp_set_link(Monitor *mon, const QDict *qdict)
1160{
1161 const char *name = qdict_get_str(qdict, "name");
34acbc95 1162 bool up = qdict_get_bool(qdict, "up");
e940f543 1163 Error *err = NULL;
4b37156c 1164
e940f543
MA
1165 qmp_set_link(name, up, &err);
1166 hmp_handle_error(mon, &err);
4b37156c 1167}
a4dea8a9
LC
1168
1169void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1170{
1171 const char *device = qdict_get_str(qdict, "device");
1172 const char *password = qdict_get_str(qdict, "password");
e940f543 1173 Error *err = NULL;
a4dea8a9 1174
e940f543
MA
1175 qmp_block_passwd(true, device, false, NULL, password, &err);
1176 hmp_handle_error(mon, &err);
a4dea8a9 1177}
d72f3264
LC
1178
1179void hmp_balloon(Monitor *mon, const QDict *qdict)
1180{
1181 int64_t value = qdict_get_int(qdict, "value");
e940f543 1182 Error *err = NULL;
d72f3264 1183
e940f543
MA
1184 qmp_balloon(value, &err);
1185 if (err) {
193227f9 1186 error_report_err(err);
d72f3264
LC
1187 }
1188}
5e7caacb
LC
1189
1190void hmp_block_resize(Monitor *mon, const QDict *qdict)
1191{
1192 const char *device = qdict_get_str(qdict, "device");
1193 int64_t size = qdict_get_int(qdict, "size");
e940f543 1194 Error *err = NULL;
5e7caacb 1195
e940f543
MA
1196 qmp_block_resize(true, device, false, NULL, size, &err);
1197 hmp_handle_error(mon, &err);
5e7caacb 1198}
6106e249 1199
d9b902db
PB
1200void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1201{
d9b902db
PB
1202 const char *filename = qdict_get_str(qdict, "target");
1203 const char *format = qdict_get_try_str(qdict, "format");
34acbc95
EB
1204 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1205 bool full = qdict_get_try_bool(qdict, "full", false);
e940f543 1206 Error *err = NULL;
faecd40a
EB
1207 DriveMirror mirror = {
1208 .device = (char *)qdict_get_str(qdict, "device"),
1209 .target = (char *)filename,
1210 .has_format = !!format,
1211 .format = (char *)format,
1212 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1213 .has_mode = true,
1214 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1215 .unmap = true,
1216 };
d9b902db
PB
1217
1218 if (!filename) {
c6bd8c70 1219 error_setg(&err, QERR_MISSING_PARAMETER, "target");
e940f543 1220 hmp_handle_error(mon, &err);
d9b902db
PB
1221 return;
1222 }
faecd40a 1223 qmp_drive_mirror(&mirror, &err);
e940f543 1224 hmp_handle_error(mon, &err);
d9b902db
PB
1225}
1226
de90930a
SH
1227void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1228{
1229 const char *device = qdict_get_str(qdict, "device");
1230 const char *filename = qdict_get_str(qdict, "target");
1231 const char *format = qdict_get_try_str(qdict, "format");
34acbc95
EB
1232 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1233 bool full = qdict_get_try_bool(qdict, "full", false);
13b9414b 1234 bool compress = qdict_get_try_bool(qdict, "compress", false);
e940f543 1235 Error *err = NULL;
81206a89
PB
1236 DriveBackup backup = {
1237 .device = (char *)device,
1238 .target = (char *)filename,
1239 .has_format = !!format,
1240 .format = (char *)format,
1241 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1242 .has_mode = true,
1243 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
13b9414b
PB
1244 .has_compress = !!compress,
1245 .compress = compress,
81206a89 1246 };
de90930a
SH
1247
1248 if (!filename) {
c6bd8c70 1249 error_setg(&err, QERR_MISSING_PARAMETER, "target");
e940f543 1250 hmp_handle_error(mon, &err);
de90930a
SH
1251 return;
1252 }
1253
81206a89 1254 qmp_drive_backup(&backup, &err);
e940f543 1255 hmp_handle_error(mon, &err);
de90930a
SH
1256}
1257
6106e249
LC
1258void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1259{
1260 const char *device = qdict_get_str(qdict, "device");
1261 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1262 const char *format = qdict_get_try_str(qdict, "format");
34acbc95 1263 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
6cc2a415 1264 enum NewImageMode mode;
e940f543 1265 Error *err = NULL;
6106e249
LC
1266
1267 if (!filename) {
1268 /* In the future, if 'snapshot-file' is not specified, the snapshot
1269 will be taken internally. Today it's actually required. */
c6bd8c70 1270 error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
e940f543 1271 hmp_handle_error(mon, &err);
6106e249
LC
1272 return;
1273 }
1274
6cc2a415 1275 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
0901f67e
BC
1276 qmp_blockdev_snapshot_sync(true, device, false, NULL,
1277 filename, false, NULL,
1278 !!format, format,
e940f543
MA
1279 true, mode, &err);
1280 hmp_handle_error(mon, &err);
6106e249 1281}
6cdedb07 1282
775ca88e
WX
1283void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1284{
1285 const char *device = qdict_get_str(qdict, "device");
1286 const char *name = qdict_get_str(qdict, "name");
e940f543 1287 Error *err = NULL;
775ca88e 1288
e940f543
MA
1289 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1290 hmp_handle_error(mon, &err);
775ca88e
WX
1291}
1292
7a4ed2ee
WX
1293void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1294{
1295 const char *device = qdict_get_str(qdict, "device");
1296 const char *name = qdict_get_str(qdict, "name");
1297 const char *id = qdict_get_try_str(qdict, "id");
e940f543 1298 Error *err = NULL;
7a4ed2ee
WX
1299
1300 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
e940f543
MA
1301 true, name, &err);
1302 hmp_handle_error(mon, &err);
7a4ed2ee
WX
1303}
1304
52b26205
JQ
1305void hmp_loadvm(Monitor *mon, const QDict *qdict)
1306{
1307 int saved_vm_running = runstate_is_running();
1308 const char *name = qdict_get_str(qdict, "name");
927d6638 1309 Error *err = NULL;
52b26205
JQ
1310
1311 vm_stop(RUN_STATE_RESTORE_VM);
1312
5e22479a 1313 if (load_snapshot(name, &err) == 0 && saved_vm_running) {
52b26205
JQ
1314 vm_start();
1315 }
927d6638 1316 hmp_handle_error(mon, &err);
52b26205
JQ
1317}
1318
d9c7d137
JQ
1319void hmp_savevm(Monitor *mon, const QDict *qdict)
1320{
927d6638
JQ
1321 Error *err = NULL;
1322
5e22479a 1323 save_snapshot(qdict_get_try_str(qdict, "name"), &err);
927d6638 1324 hmp_handle_error(mon, &err);
d9c7d137
JQ
1325}
1326
d905bb7b
JQ
1327void hmp_delvm(Monitor *mon, const QDict *qdict)
1328{
1329 BlockDriverState *bs;
1330 Error *err;
1331 const char *name = qdict_get_str(qdict, "name");
1332
1333 if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1334 error_reportf_err(err,
1335 "Error while deleting snapshot on device '%s': ",
1336 bdrv_get_device_name(bs));
1337 }
1338}
1339
66830618
JQ
1340void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1341{
1342 BlockDriverState *bs, *bs1;
1343 BdrvNextIterator it1;
1344 QEMUSnapshotInfo *sn_tab, *sn;
1345 bool no_snapshot = true;
1346 int nb_sns, i;
1347 int total;
1348 int *global_snapshots;
1349 AioContext *aio_context;
1350
1351 typedef struct SnapshotEntry {
1352 QEMUSnapshotInfo sn;
1353 QTAILQ_ENTRY(SnapshotEntry) next;
1354 } SnapshotEntry;
1355
1356 typedef struct ImageEntry {
1357 const char *imagename;
1358 QTAILQ_ENTRY(ImageEntry) next;
1359 QTAILQ_HEAD(, SnapshotEntry) snapshots;
1360 } ImageEntry;
1361
1362 QTAILQ_HEAD(, ImageEntry) image_list =
1363 QTAILQ_HEAD_INITIALIZER(image_list);
1364
1365 ImageEntry *image_entry, *next_ie;
1366 SnapshotEntry *snapshot_entry;
1367
1368 bs = bdrv_all_find_vmstate_bs();
1369 if (!bs) {
1370 monitor_printf(mon, "No available block device supports snapshots\n");
1371 return;
1372 }
1373 aio_context = bdrv_get_aio_context(bs);
1374
1375 aio_context_acquire(aio_context);
1376 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1377 aio_context_release(aio_context);
1378
1379 if (nb_sns < 0) {
1380 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1381 return;
1382 }
1383
1384 for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1385 int bs1_nb_sns = 0;
1386 ImageEntry *ie;
1387 SnapshotEntry *se;
1388 AioContext *ctx = bdrv_get_aio_context(bs1);
1389
1390 aio_context_acquire(ctx);
1391 if (bdrv_can_snapshot(bs1)) {
1392 sn = NULL;
1393 bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1394 if (bs1_nb_sns > 0) {
1395 no_snapshot = false;
1396 ie = g_new0(ImageEntry, 1);
1397 ie->imagename = bdrv_get_device_name(bs1);
1398 QTAILQ_INIT(&ie->snapshots);
1399 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1400 for (i = 0; i < bs1_nb_sns; i++) {
1401 se = g_new0(SnapshotEntry, 1);
1402 se->sn = sn[i];
1403 QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1404 }
1405 }
1406 g_free(sn);
1407 }
1408 aio_context_release(ctx);
1409 }
1410
1411 if (no_snapshot) {
1412 monitor_printf(mon, "There is no snapshot available.\n");
1413 return;
1414 }
1415
1416 global_snapshots = g_new0(int, nb_sns);
1417 total = 0;
1418 for (i = 0; i < nb_sns; i++) {
1419 SnapshotEntry *next_sn;
1420 if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1421 global_snapshots[total] = i;
1422 total++;
1423 QTAILQ_FOREACH(image_entry, &image_list, next) {
1424 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1425 next, next_sn) {
1426 if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1427 QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1428 next);
1429 g_free(snapshot_entry);
1430 }
1431 }
1432 }
1433 }
1434 }
1435
1436 monitor_printf(mon, "List of snapshots present on all disks:\n");
1437
1438 if (total > 0) {
1439 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1440 monitor_printf(mon, "\n");
1441 for (i = 0; i < total; i++) {
1442 sn = &sn_tab[global_snapshots[i]];
1443 /* The ID is not guaranteed to be the same on all images, so
1444 * overwrite it.
1445 */
1446 pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1447 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1448 monitor_printf(mon, "\n");
1449 }
1450 } else {
1451 monitor_printf(mon, "None\n");
1452 }
1453
1454 QTAILQ_FOREACH(image_entry, &image_list, next) {
1455 if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1456 continue;
1457 }
1458 monitor_printf(mon,
1459 "\nList of partial (non-loadable) snapshots on '%s':\n",
1460 image_entry->imagename);
1461 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1462 monitor_printf(mon, "\n");
1463 QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1464 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1465 &snapshot_entry->sn);
1466 monitor_printf(mon, "\n");
1467 }
1468 }
1469
1470 QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1471 SnapshotEntry *next_sn;
1472 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1473 next_sn) {
1474 g_free(snapshot_entry);
1475 }
1476 g_free(image_entry);
1477 }
1478 g_free(sn_tab);
1479 g_free(global_snapshots);
1480
1481}
1482
6cdedb07
LC
1483void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1484{
1485 qmp_migrate_cancel(NULL);
1486}
4f0a993b 1487
bf1ae1f4
DDAG
1488void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1489{
1490 Error *err = NULL;
1491 const char *uri = qdict_get_str(qdict, "uri");
1492
1493 qmp_migrate_incoming(uri, &err);
1494
1fa57f55 1495 hmp_handle_error(mon, &err);
bf1ae1f4
DDAG
1496}
1497
2ff30257 1498/* Kept for backwards compatibility */
4f0a993b
LC
1499void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1500{
1501 double value = qdict_get_double(qdict, "value");
1502 qmp_migrate_set_downtime(value, NULL);
1503}
3dc85383 1504
9e1ba4cc
OW
1505void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1506{
1507 int64_t value = qdict_get_int(qdict, "value");
1508 Error *err = NULL;
1509
1510 qmp_migrate_set_cache_size(value, &err);
1511 if (err) {
193227f9 1512 error_report_err(err);
9e1ba4cc
OW
1513 return;
1514 }
1515}
1516
2ff30257 1517/* Kept for backwards compatibility */
3dc85383
LC
1518void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1519{
1520 int64_t value = qdict_get_int(qdict, "value");
1521 qmp_migrate_set_speed(value, NULL);
1522}
fbf796fd 1523
00458433
OW
1524void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1525{
1526 const char *cap = qdict_get_str(qdict, "capability");
1527 bool state = qdict_get_bool(qdict, "state");
1528 Error *err = NULL;
1529 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1530 int i;
1531
7fb1cf16 1532 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
00458433
OW
1533 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1534 caps->value = g_malloc0(sizeof(*caps->value));
1535 caps->value->capability = i;
1536 caps->value->state = state;
1537 caps->next = NULL;
1538 qmp_migrate_set_capabilities(caps, &err);
1539 break;
1540 }
1541 }
1542
7fb1cf16 1543 if (i == MIGRATION_CAPABILITY__MAX) {
c6bd8c70 1544 error_setg(&err, QERR_INVALID_PARAMETER, cap);
00458433
OW
1545 }
1546
1547 qapi_free_MigrationCapabilityStatusList(caps);
1548
1549 if (err) {
193227f9 1550 error_report_err(err);
00458433
OW
1551 }
1552}
1553
50e9a629
LL
1554void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1555{
1556 const char *param = qdict_get_str(qdict, "parameter");
69ef1f36 1557 const char *valuestr = qdict_get_str(qdict, "value");
f4a06d13 1558 Visitor *v = string_input_visitor_new(valuestr);
1bda8b3c 1559 MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
f46bfdbf 1560 uint64_t valuebw = 0;
50e9a629 1561 Error *err = NULL;
f17fd4fd 1562 int i, ret;
50e9a629 1563
7fb1cf16 1564 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
50e9a629
LL
1565 if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
1566 switch (i) {
1567 case MIGRATION_PARAMETER_COMPRESS_LEVEL:
7e91e820
MA
1568 p->has_compress_level = true;
1569 visit_type_int(v, param, &p->compress_level, &err);
50e9a629
LL
1570 break;
1571 case MIGRATION_PARAMETER_COMPRESS_THREADS:
7e91e820
MA
1572 p->has_compress_threads = true;
1573 visit_type_int(v, param, &p->compress_threads, &err);
50e9a629
LL
1574 break;
1575 case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
7e91e820
MA
1576 p->has_decompress_threads = true;
1577 visit_type_int(v, param, &p->decompress_threads, &err);
50e9a629 1578 break;
d85a31d1 1579 case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
7e91e820
MA
1580 p->has_cpu_throttle_initial = true;
1581 visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1626fee3 1582 break;
d85a31d1 1583 case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
7e91e820
MA
1584 p->has_cpu_throttle_increment = true;
1585 visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1626fee3 1586 break;
69ef1f36 1587 case MIGRATION_PARAMETER_TLS_CREDS:
7e91e820 1588 p->has_tls_creds = true;
01fa5598
MA
1589 p->tls_creds = g_new0(StrOrNull, 1);
1590 p->tls_creds->type = QTYPE_QSTRING;
1591 visit_type_str(v, param, &p->tls_creds->u.s, &err);
69ef1f36
DB
1592 break;
1593 case MIGRATION_PARAMETER_TLS_HOSTNAME:
7e91e820 1594 p->has_tls_hostname = true;
01fa5598
MA
1595 p->tls_hostname = g_new0(StrOrNull, 1);
1596 p->tls_hostname->type = QTYPE_QSTRING;
1597 visit_type_str(v, param, &p->tls_hostname->u.s, &err);
69ef1f36 1598 break;
2ff30257 1599 case MIGRATION_PARAMETER_MAX_BANDWIDTH:
7e91e820
MA
1600 p->has_max_bandwidth = true;
1601 /*
1602 * Can't use visit_type_size() here, because it
1603 * defaults to Bytes rather than Mebibytes.
1604 */
f17fd4fd 1605 ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
f46bfdbf
MA
1606 if (ret < 0 || valuebw > INT64_MAX
1607 || (size_t)valuebw != valuebw) {
2ff30257 1608 error_setg(&err, "Invalid size %s", valuestr);
7e91e820 1609 break;
2ff30257 1610 }
7e91e820 1611 p->max_bandwidth = valuebw;
2ff30257
AA
1612 break;
1613 case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
7e91e820
MA
1614 p->has_downtime_limit = true;
1615 visit_type_int(v, param, &p->downtime_limit, &err);
2ff30257 1616 break;
68b53591 1617 case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
7e91e820
MA
1618 p->has_x_checkpoint_delay = true;
1619 visit_type_int(v, param, &p->x_checkpoint_delay, &err);
68b53591 1620 break;
2833c59b 1621 case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
7e91e820
MA
1622 p->has_block_incremental = true;
1623 visit_type_bool(v, param, &p->block_incremental, &err);
2833c59b 1624 break;
50e9a629 1625 }
69ef1f36 1626
7e91e820
MA
1627 if (err) {
1628 goto cleanup;
69ef1f36
DB
1629 }
1630
7e91e820 1631 qmp_migrate_set_parameters(p, &err);
50e9a629
LL
1632 break;
1633 }
1634 }
1635
7fb1cf16 1636 if (i == MIGRATION_PARAMETER__MAX) {
c6bd8c70 1637 error_setg(&err, QERR_INVALID_PARAMETER, param);
50e9a629
LL
1638 }
1639
69ef1f36 1640 cleanup:
1bda8b3c 1641 qapi_free_MigrateSetParameters(p);
f4a06d13 1642 visit_free(v);
50e9a629 1643 if (err) {
193227f9 1644 error_report_err(err);
50e9a629
LL
1645 }
1646}
1647
b8a185bc
MA
1648void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1649{
1650 Error *err = NULL;
1651 const char *protocol = qdict_get_str(qdict, "protocol");
1652 const char *hostname = qdict_get_str(qdict, "hostname");
1653 bool has_port = qdict_haskey(qdict, "port");
1654 int port = qdict_get_try_int(qdict, "port", -1);
1655 bool has_tls_port = qdict_haskey(qdict, "tls-port");
1656 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1657 const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1658
1659 qmp_client_migrate_info(protocol, hostname,
1660 has_port, port, has_tls_port, tls_port,
1661 !!cert_subject, cert_subject, &err);
1662 hmp_handle_error(mon, &err);
1663}
1664
4886a1bc
DDAG
1665void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1666{
1667 Error *err = NULL;
1668 qmp_migrate_start_postcopy(&err);
1669 hmp_handle_error(mon, &err);
1670}
1671
d89e666e
HZ
1672void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1673{
1674 Error *err = NULL;
1675
1676 qmp_x_colo_lost_heartbeat(&err);
1677 hmp_handle_error(mon, &err);
1678}
1679
fbf796fd
LC
1680void hmp_set_password(Monitor *mon, const QDict *qdict)
1681{
1682 const char *protocol = qdict_get_str(qdict, "protocol");
1683 const char *password = qdict_get_str(qdict, "password");
1684 const char *connected = qdict_get_try_str(qdict, "connected");
1685 Error *err = NULL;
1686
1687 qmp_set_password(protocol, password, !!connected, connected, &err);
1688 hmp_handle_error(mon, &err);
1689}
9ad5372d
LC
1690
1691void hmp_expire_password(Monitor *mon, const QDict *qdict)
1692{
1693 const char *protocol = qdict_get_str(qdict, "protocol");
1694 const char *whenstr = qdict_get_str(qdict, "time");
1695 Error *err = NULL;
1696
1697 qmp_expire_password(protocol, whenstr, &err);
1698 hmp_handle_error(mon, &err);
1699}
c245b6a3
LC
1700
1701void hmp_eject(Monitor *mon, const QDict *qdict)
1702{
34acbc95 1703 bool force = qdict_get_try_bool(qdict, "force", false);
c245b6a3
LC
1704 const char *device = qdict_get_str(qdict, "device");
1705 Error *err = NULL;
1706
fbe2d816 1707 qmp_eject(true, device, false, NULL, true, force, &err);
c245b6a3
LC
1708 hmp_handle_error(mon, &err);
1709}
333a96ec 1710
c60bf339
SH
1711static void hmp_change_read_arg(void *opaque, const char *password,
1712 void *readline_opaque)
333a96ec
LC
1713{
1714 qmp_change_vnc_password(password, NULL);
c60bf339 1715 monitor_read_command(opaque, 1);
333a96ec
LC
1716}
1717
333a96ec
LC
1718void hmp_change(Monitor *mon, const QDict *qdict)
1719{
1720 const char *device = qdict_get_str(qdict, "device");
1721 const char *target = qdict_get_str(qdict, "target");
1722 const char *arg = qdict_get_try_str(qdict, "arg");
baead0ab
HR
1723 const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1724 BlockdevChangeReadOnlyMode read_only_mode = 0;
333a96ec
LC
1725 Error *err = NULL;
1726
10686749 1727 if (strcmp(device, "vnc") == 0) {
baead0ab
HR
1728 if (read_only) {
1729 monitor_printf(mon,
1730 "Parameter 'read-only-mode' is invalid for VNC\n");
1731 return;
1732 }
10686749
HR
1733 if (strcmp(target, "passwd") == 0 ||
1734 strcmp(target, "password") == 0) {
1735 if (!arg) {
1736 monitor_read_password(mon, hmp_change_read_arg, NULL);
1737 return;
1738 }
1739 }
1740 qmp_change("vnc", target, !!arg, arg, &err);
1741 } else {
baead0ab
HR
1742 if (read_only) {
1743 read_only_mode =
1744 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
06c60b6c 1745 read_only,
baead0ab
HR
1746 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1747 if (err) {
1748 hmp_handle_error(mon, &err);
1749 return;
1750 }
1751 }
1752
70e2cb3b
KW
1753 qmp_blockdev_change_medium(true, device, false, NULL, target,
1754 !!arg, arg, !!read_only, read_only_mode,
1755 &err);
333a96ec
LC
1756 }
1757
333a96ec
LC
1758 hmp_handle_error(mon, &err);
1759}
80047da5
LC
1760
1761void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1762{
1763 Error *err = NULL;
4dc9397b 1764 BlockIOThrottle throttle = {
3f35c3b1 1765 .has_device = true,
4dc9397b
EB
1766 .device = (char *) qdict_get_str(qdict, "device"),
1767 .bps = qdict_get_int(qdict, "bps"),
1768 .bps_rd = qdict_get_int(qdict, "bps_rd"),
1769 .bps_wr = qdict_get_int(qdict, "bps_wr"),
1770 .iops = qdict_get_int(qdict, "iops"),
1771 .iops_rd = qdict_get_int(qdict, "iops_rd"),
1772 .iops_wr = qdict_get_int(qdict, "iops_wr"),
1773 };
80047da5 1774
4dc9397b 1775 qmp_block_set_io_throttle(&throttle, &err);
80047da5
LC
1776 hmp_handle_error(mon, &err);
1777}
12bd451f
SH
1778
1779void hmp_block_stream(Monitor *mon, const QDict *qdict)
1780{
1781 Error *error = NULL;
1782 const char *device = qdict_get_str(qdict, "device");
1783 const char *base = qdict_get_try_str(qdict, "base");
c83c66c3 1784 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
12bd451f 1785
11d6fbe0 1786 qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
312fe09c 1787 false, NULL, qdict_haskey(qdict, "speed"), speed,
46663e5e 1788 true, BLOCKDEV_ON_ERROR_REPORT, &error);
12bd451f
SH
1789
1790 hmp_handle_error(mon, &error);
1791}
2d47c6e9
SH
1792
1793void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1794{
1795 Error *error = NULL;
1796 const char *device = qdict_get_str(qdict, "device");
c6db2395 1797 int64_t value = qdict_get_int(qdict, "speed");
2d47c6e9
SH
1798
1799 qmp_block_job_set_speed(device, value, &error);
1800
1801 hmp_handle_error(mon, &error);
1802}
370521a1
SH
1803
1804void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1805{
1806 Error *error = NULL;
1807 const char *device = qdict_get_str(qdict, "device");
34acbc95 1808 bool force = qdict_get_try_bool(qdict, "force", false);
370521a1 1809
6e37fb81
PB
1810 qmp_block_job_cancel(device, true, force, &error);
1811
1812 hmp_handle_error(mon, &error);
1813}
1814
1815void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1816{
1817 Error *error = NULL;
1818 const char *device = qdict_get_str(qdict, "device");
1819
1820 qmp_block_job_pause(device, &error);
1821
1822 hmp_handle_error(mon, &error);
1823}
1824
1825void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1826{
1827 Error *error = NULL;
1828 const char *device = qdict_get_str(qdict, "device");
1829
1830 qmp_block_job_resume(device, &error);
370521a1
SH
1831
1832 hmp_handle_error(mon, &error);
1833}
e1c37d0e 1834
aeae883b
PB
1835void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1836{
1837 Error *error = NULL;
1838 const char *device = qdict_get_str(qdict, "device");
1839
1840 qmp_block_job_complete(device, &error);
1841
1842 hmp_handle_error(mon, &error);
1843}
1844
e49f35bd 1845typedef struct HMPMigrationStatus
e1c37d0e
LC
1846{
1847 QEMUTimer *timer;
1848 Monitor *mon;
1849 bool is_block_migration;
e49f35bd 1850} HMPMigrationStatus;
e1c37d0e
LC
1851
1852static void hmp_migrate_status_cb(void *opaque)
1853{
e49f35bd 1854 HMPMigrationStatus *status = opaque;
e1c37d0e
LC
1855 MigrationInfo *info;
1856
1857 info = qmp_query_migrate(NULL);
24b8c39b
HZ
1858 if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1859 info->status == MIGRATION_STATUS_SETUP) {
e1c37d0e
LC
1860 if (info->has_disk) {
1861 int progress;
1862
1863 if (info->disk->remaining) {
1864 progress = info->disk->transferred * 100 / info->disk->total;
1865 } else {
1866 progress = 100;
1867 }
1868
1869 monitor_printf(status->mon, "Completed %d %%\r", progress);
1870 monitor_flush(status->mon);
1871 }
1872
bc72ad67 1873 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
e1c37d0e
LC
1874 } else {
1875 if (status->is_block_migration) {
1876 monitor_printf(status->mon, "\n");
1877 }
d59ce6f3
DB
1878 if (info->has_error_desc) {
1879 error_report("%s", info->error_desc);
1880 }
e1c37d0e 1881 monitor_resume(status->mon);
bc72ad67 1882 timer_del(status->timer);
e1c37d0e
LC
1883 g_free(status);
1884 }
1885
1886 qapi_free_MigrationInfo(info);
1887}
1888
1889void hmp_migrate(Monitor *mon, const QDict *qdict)
1890{
34acbc95
EB
1891 bool detach = qdict_get_try_bool(qdict, "detach", false);
1892 bool blk = qdict_get_try_bool(qdict, "blk", false);
1893 bool inc = qdict_get_try_bool(qdict, "inc", false);
e1c37d0e
LC
1894 const char *uri = qdict_get_str(qdict, "uri");
1895 Error *err = NULL;
1896
1897 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1898 if (err) {
193227f9 1899 error_report_err(err);
e1c37d0e
LC
1900 return;
1901 }
1902
1903 if (!detach) {
e49f35bd 1904 HMPMigrationStatus *status;
e1c37d0e
LC
1905
1906 if (monitor_suspend(mon) < 0) {
1907 monitor_printf(mon, "terminal does not allow synchronous "
1908 "migration, continuing detached\n");
1909 return;
1910 }
1911
1912 status = g_malloc0(sizeof(*status));
1913 status->mon = mon;
1914 status->is_block_migration = blk || inc;
bc72ad67 1915 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
e1c37d0e 1916 status);
bc72ad67 1917 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
e1c37d0e
LC
1918 }
1919}
a15fef21 1920
318660f8
MA
1921void hmp_device_add(Monitor *mon, const QDict *qdict)
1922{
485febc6
MA
1923 Error *err = NULL;
1924
1925 qmp_device_add((QDict *)qdict, NULL, &err);
1926 hmp_handle_error(mon, &err);
318660f8
MA
1927}
1928
a15fef21
LC
1929void hmp_device_del(Monitor *mon, const QDict *qdict)
1930{
1931 const char *id = qdict_get_str(qdict, "id");
1932 Error *err = NULL;
1933
1934 qmp_device_del(id, &err);
1935 hmp_handle_error(mon, &err);
1936}
783e9b48
WC
1937
1938void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1939{
e940f543 1940 Error *err = NULL;
34acbc95
EB
1941 bool paging = qdict_get_try_bool(qdict, "paging", false);
1942 bool zlib = qdict_get_try_bool(qdict, "zlib", false);
1943 bool lzo = qdict_get_try_bool(qdict, "lzo", false);
1944 bool snappy = qdict_get_try_bool(qdict, "snappy", false);
75363769 1945 const char *file = qdict_get_str(qdict, "filename");
783e9b48
WC
1946 bool has_begin = qdict_haskey(qdict, "begin");
1947 bool has_length = qdict_haskey(qdict, "length");
228de9cf 1948 bool has_detach = qdict_haskey(qdict, "detach");
783e9b48
WC
1949 int64_t begin = 0;
1950 int64_t length = 0;
228de9cf 1951 bool detach = false;
b53ccc30 1952 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
75363769 1953 char *prot;
783e9b48 1954
1b7a0f75 1955 if (zlib + lzo + snappy > 1) {
e940f543
MA
1956 error_setg(&err, "only one of '-z|-l|-s' can be set");
1957 hmp_handle_error(mon, &err);
1b7a0f75
QN
1958 return;
1959 }
1960
1961 if (zlib) {
1962 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1963 }
1964
1965 if (lzo) {
1966 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1967 }
1968
1969 if (snappy) {
1970 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1971 }
1972
783e9b48
WC
1973 if (has_begin) {
1974 begin = qdict_get_int(qdict, "begin");
1975 }
1976 if (has_length) {
1977 length = qdict_get_int(qdict, "length");
1978 }
228de9cf
PX
1979 if (has_detach) {
1980 detach = qdict_get_bool(qdict, "detach");
1981 }
783e9b48 1982
75363769
LC
1983 prot = g_strconcat("file:", file, NULL);
1984
228de9cf
PX
1985 qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
1986 has_length, length, true, dump_format, &err);
e940f543 1987 hmp_handle_error(mon, &err);
75363769 1988 g_free(prot);
783e9b48 1989}
928059a3
LC
1990
1991void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1992{
1993 Error *err = NULL;
1994 QemuOpts *opts;
1995
1996 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
84d18f06 1997 if (err) {
928059a3
LC
1998 goto out;
1999 }
2000
2001 netdev_add(opts, &err);
84d18f06 2002 if (err) {
928059a3
LC
2003 qemu_opts_del(opts);
2004 }
2005
2006out:
2007 hmp_handle_error(mon, &err);
2008}
5f964155
LC
2009
2010void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2011{
2012 const char *id = qdict_get_str(qdict, "id");
2013 Error *err = NULL;
2014
2015 qmp_netdev_del(id, &err);
2016 hmp_handle_error(mon, &err);
2017}
208c9d1b 2018
cff8b2c6
PB
2019void hmp_object_add(Monitor *mon, const QDict *qdict)
2020{
2021 Error *err = NULL;
2022 QemuOpts *opts;
90998d58 2023 Object *obj = NULL;
cff8b2c6
PB
2024
2025 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2026 if (err) {
90998d58
DB
2027 hmp_handle_error(mon, &err);
2028 return;
cff8b2c6
PB
2029 }
2030
3a464105 2031 obj = user_creatable_add_opts(opts, &err);
90998d58 2032 qemu_opts_del(opts);
cff8b2c6 2033
cff8b2c6 2034 if (err) {
90998d58 2035 hmp_handle_error(mon, &err);
cff8b2c6 2036 }
90998d58
DB
2037 if (obj) {
2038 object_unref(obj);
cff8b2c6 2039 }
cff8b2c6
PB
2040}
2041
208c9d1b
CB
2042void hmp_getfd(Monitor *mon, const QDict *qdict)
2043{
2044 const char *fdname = qdict_get_str(qdict, "fdname");
e940f543 2045 Error *err = NULL;
208c9d1b 2046
e940f543
MA
2047 qmp_getfd(fdname, &err);
2048 hmp_handle_error(mon, &err);
208c9d1b
CB
2049}
2050
2051void hmp_closefd(Monitor *mon, const QDict *qdict)
2052{
2053 const char *fdname = qdict_get_str(qdict, "fdname");
e940f543 2054 Error *err = NULL;
208c9d1b 2055
e940f543
MA
2056 qmp_closefd(fdname, &err);
2057 hmp_handle_error(mon, &err);
208c9d1b 2058}
e4c8f004 2059
3e5a50d6 2060void hmp_sendkey(Monitor *mon, const QDict *qdict)
e4c8f004
AK
2061{
2062 const char *keys = qdict_get_str(qdict, "keys");
9f328977 2063 KeyValueList *keylist, *head = NULL, *tmp = NULL;
e4c8f004
AK
2064 int has_hold_time = qdict_haskey(qdict, "hold-time");
2065 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2066 Error *err = NULL;
e4c8f004 2067 char *separator;
9f328977 2068 int keyname_len;
e4c8f004
AK
2069
2070 while (1) {
2071 separator = strchr(keys, '-');
2072 keyname_len = separator ? separator - keys : strlen(keys);
e4c8f004
AK
2073
2074 /* Be compatible with old interface, convert user inputted "<" */
64ffbe04
WB
2075 if (keys[0] == '<' && keyname_len == 1) {
2076 keys = "less";
e4c8f004
AK
2077 keyname_len = 4;
2078 }
e4c8f004 2079
e4c8f004 2080 keylist = g_malloc0(sizeof(*keylist));
9f328977 2081 keylist->value = g_malloc0(sizeof(*keylist->value));
e4c8f004
AK
2082
2083 if (!head) {
2084 head = keylist;
2085 }
2086 if (tmp) {
2087 tmp->next = keylist;
2088 }
2089 tmp = keylist;
2090
64ffbe04 2091 if (strstart(keys, "0x", NULL)) {
9f328977 2092 char *endp;
64ffbe04
WB
2093 int value = strtoul(keys, &endp, 0);
2094 assert(endp <= keys + keyname_len);
2095 if (endp != keys + keyname_len) {
9f328977
LC
2096 goto err_out;
2097 }
568c73a4 2098 keylist->value->type = KEY_VALUE_KIND_NUMBER;
32bafa8f 2099 keylist->value->u.number.data = value;
9f328977 2100 } else {
64ffbe04 2101 int idx = index_from_key(keys, keyname_len);
7fb1cf16 2102 if (idx == Q_KEY_CODE__MAX) {
9f328977
LC
2103 goto err_out;
2104 }
568c73a4 2105 keylist->value->type = KEY_VALUE_KIND_QCODE;
32bafa8f 2106 keylist->value->u.qcode.data = idx;
9f328977
LC
2107 }
2108
e4c8f004
AK
2109 if (!separator) {
2110 break;
2111 }
2112 keys = separator + 1;
2113 }
2114
9f328977 2115 qmp_send_key(head, has_hold_time, hold_time, &err);
e4c8f004 2116 hmp_handle_error(mon, &err);
9f328977
LC
2117
2118out:
2119 qapi_free_KeyValueList(head);
2120 return;
2121
2122err_out:
64ffbe04 2123 monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
9f328977 2124 goto out;
e4c8f004 2125}
ad39cf6d 2126
3e5a50d6 2127void hmp_screendump(Monitor *mon, const QDict *qdict)
ad39cf6d
LC
2128{
2129 const char *filename = qdict_get_str(qdict, "filename");
2130 Error *err = NULL;
2131
2132 qmp_screendump(filename, &err);
2133 hmp_handle_error(mon, &err);
2134}
4057725f
PB
2135
2136void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2137{
2138 const char *uri = qdict_get_str(qdict, "uri");
34acbc95
EB
2139 bool writable = qdict_get_try_bool(qdict, "writable", false);
2140 bool all = qdict_get_try_bool(qdict, "all", false);
4057725f
PB
2141 Error *local_err = NULL;
2142 BlockInfoList *block_list, *info;
bd269ebc 2143 SocketAddress *addr;
4057725f
PB
2144
2145 if (writable && !all) {
2146 error_setg(&local_err, "-w only valid together with -a");
2147 goto exit;
2148 }
2149
2150 /* First check if the address is valid and start the server. */
2151 addr = socket_parse(uri, &local_err);
2152 if (local_err != NULL) {
2153 goto exit;
2154 }
2155
bd269ebc
MA
2156 nbd_server_start(addr, NULL, &local_err);
2157 qapi_free_SocketAddress(addr);
4057725f
PB
2158 if (local_err != NULL) {
2159 goto exit;
2160 }
2161
2162 if (!all) {
2163 return;
2164 }
2165
2166 /* Then try adding all block devices. If one fails, close all and
2167 * exit.
2168 */
2169 block_list = qmp_query_block(NULL);
2170
2171 for (info = block_list; info; info = info->next) {
2172 if (!info->value->has_inserted) {
2173 continue;
2174 }
2175
2176 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
2177
2178 if (local_err != NULL) {
2179 qmp_nbd_server_stop(NULL);
2180 break;
2181 }
2182 }
2183
2184 qapi_free_BlockInfoList(block_list);
2185
2186exit:
2187 hmp_handle_error(mon, &local_err);
2188}
2189
2190void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2191{
2192 const char *device = qdict_get_str(qdict, "device");
34acbc95 2193 bool writable = qdict_get_try_bool(qdict, "writable", false);
4057725f
PB
2194 Error *local_err = NULL;
2195
2196 qmp_nbd_server_add(device, true, writable, &local_err);
2197
2198 if (local_err != NULL) {
2199 hmp_handle_error(mon, &local_err);
2200 }
2201}
2202
2203void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2204{
e940f543 2205 Error *err = NULL;
4057725f 2206
e940f543
MA
2207 qmp_nbd_server_stop(&err);
2208 hmp_handle_error(mon, &err);
4057725f 2209}
f1088908 2210
abf23329
JH
2211void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2212{
2213 int cpuid;
2214 Error *err = NULL;
2215
2216 cpuid = qdict_get_int(qdict, "id");
2217 qmp_cpu_add(cpuid, &err);
2218 hmp_handle_error(mon, &err);
2219}
2220
f1088908
GH
2221void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2222{
2223 const char *args = qdict_get_str(qdict, "args");
2224 Error *err = NULL;
2225 QemuOpts *opts;
2226
70b94331 2227 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
f1088908 2228 if (opts == NULL) {
312fd5f2 2229 error_setg(&err, "Parsing chardev args failed");
f1088908 2230 } else {
b4948be9 2231 qemu_chr_new_from_opts(opts, &err);
0a73336d 2232 qemu_opts_del(opts);
f1088908
GH
2233 }
2234 hmp_handle_error(mon, &err);
2235}
2236
75b60160
AN
2237void hmp_chardev_change(Monitor *mon, const QDict *qdict)
2238{
2239 const char *args = qdict_get_str(qdict, "args");
2240 const char *id;
2241 Error *err = NULL;
2242 ChardevBackend *backend = NULL;
2243 ChardevReturn *ret = NULL;
2244 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
2245 true);
2246 if (!opts) {
2247 error_setg(&err, "Parsing chardev args failed");
2248 goto end;
2249 }
2250
2251 id = qdict_get_str(qdict, "id");
2252 if (qemu_opts_id(opts)) {
2253 error_setg(&err, "Unexpected 'id' parameter");
2254 goto end;
2255 }
2256
2257 backend = qemu_chr_parse_opts(opts, &err);
2258 if (!backend) {
2259 goto end;
2260 }
2261
2262 ret = qmp_chardev_change(id, backend, &err);
2263
2264end:
2265 qapi_free_ChardevReturn(ret);
2266 qapi_free_ChardevBackend(backend);
2267 qemu_opts_del(opts);
2268 hmp_handle_error(mon, &err);
2269}
2270
f1088908
GH
2271void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2272{
2273 Error *local_err = NULL;
2274
2275 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2276 hmp_handle_error(mon, &local_err);
2277}
587da2c3 2278
bd1d5ad9
SF
2279void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2280{
2281 Error *local_err = NULL;
2282
2283 qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2284 hmp_handle_error(mon, &local_err);
2285}
2286
587da2c3
KW
2287void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2288{
4c7b7e9b 2289 BlockBackend *blk;
e7f98f2f 2290 BlockBackend *local_blk = NULL;
1813d330 2291 AioContext *aio_context;
587da2c3
KW
2292 const char* device = qdict_get_str(qdict, "device");
2293 const char* command = qdict_get_str(qdict, "command");
2294 Error *err = NULL;
d7086422 2295 int ret;
587da2c3 2296
4c7b7e9b 2297 blk = blk_by_name(device);
e7f98f2f
KW
2298 if (!blk) {
2299 BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2300 if (bs) {
6d0eb64d 2301 blk = local_blk = blk_new(0, BLK_PERM_ALL);
d7086422
KW
2302 ret = blk_insert_bs(blk, bs, &err);
2303 if (ret < 0) {
2304 goto fail;
2305 }
e7f98f2f
KW
2306 } else {
2307 goto fail;
2308 }
2309 }
2310
1813d330
KW
2311 aio_context = blk_get_aio_context(blk);
2312 aio_context_acquire(aio_context);
479b5998 2313
887354bd
KW
2314 /*
2315 * Notably absent: Proper permission management. This is sad, but it seems
2316 * almost impossible to achieve without changing the semantics and thereby
2317 * limiting the use cases of the qemu-io HMP command.
2318 *
2319 * In an ideal world we would unconditionally create a new BlockBackend for
2320 * qemuio_command(), but we have commands like 'reopen' and want them to
2321 * take effect on the exact BlockBackend whose name the user passed instead
2322 * of just on a temporary copy of it.
2323 *
2324 * Another problem is that deleting the temporary BlockBackend involves
2325 * draining all requests on it first, but some qemu-iotests cases want to
2326 * issue multiple aio_read/write requests and expect them to complete in
2327 * the background while the monitor has already returned.
2328 *
2329 * This is also what prevents us from saving the original permissions and
2330 * restoring them later: We can't revoke permissions until all requests
2331 * have completed, and we don't know when that is nor can we really let
2332 * anything else run before we have revoken them to avoid race conditions.
2333 *
2334 * What happens now is that command() in qemu-io-cmds.c can extend the
2335 * permissions if necessary for the qemu-io command. And they simply stay
2336 * extended, possibly resulting in a read-only guest device keeping write
2337 * permissions. Ugly, but it appears to be the lesser evil.
2338 */
1813d330 2339 qemuio_command(blk, command);
479b5998 2340
1813d330 2341 aio_context_release(aio_context);
587da2c3 2342
e7f98f2f
KW
2343fail:
2344 blk_unref(local_blk);
587da2c3
KW
2345 hmp_handle_error(mon, &err);
2346}
ab2d0531
PB
2347
2348void hmp_object_del(Monitor *mon, const QDict *qdict)
2349{
2350 const char *id = qdict_get_str(qdict, "id");
2351 Error *err = NULL;
2352
90998d58 2353 user_creatable_del(id, &err);
ab2d0531
PB
2354 hmp_handle_error(mon, &err);
2355}
eb1539b2
HT
2356
2357void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2358{
2359 Error *err = NULL;
2360 MemdevList *memdev_list = qmp_query_memdev(&err);
2361 MemdevList *m = memdev_list;
3b098d56 2362 Visitor *v;
976620ac 2363 char *str;
eb1539b2
HT
2364
2365 while (m) {
3b098d56
EB
2366 v = string_output_visitor_new(false, &str);
2367 visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
e1ff3c67 2368 monitor_printf(mon, "memory backend: %s\n", m->value->id);
eb1539b2
HT
2369 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
2370 monitor_printf(mon, " merge: %s\n",
2371 m->value->merge ? "true" : "false");
2372 monitor_printf(mon, " dump: %s\n",
2373 m->value->dump ? "true" : "false");
2374 monitor_printf(mon, " prealloc: %s\n",
2375 m->value->prealloc ? "true" : "false");
2376 monitor_printf(mon, " policy: %s\n",
2377 HostMemPolicy_lookup[m->value->policy]);
3b098d56 2378 visit_complete(v, &str);
976620ac 2379 monitor_printf(mon, " host nodes: %s\n", str);
eb1539b2 2380
976620ac 2381 g_free(str);
3b098d56 2382 visit_free(v);
eb1539b2 2383 m = m->next;
eb1539b2
HT
2384 }
2385
2386 monitor_printf(mon, "\n");
ecaf54a0
CF
2387
2388 qapi_free_MemdevList(memdev_list);
eb1539b2 2389}
a631892f
ZG
2390
2391void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2392{
2393 Error *err = NULL;
2394 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2395 MemoryDeviceInfoList *info;
2396 MemoryDeviceInfo *value;
2397 PCDIMMDeviceInfo *di;
2398
2399 for (info = info_list; info; info = info->next) {
2400 value = info->value;
2401
2402 if (value) {
1fd5d4fe 2403 switch (value->type) {
a631892f 2404 case MEMORY_DEVICE_INFO_KIND_DIMM:
32bafa8f 2405 di = value->u.dimm.data;
a631892f
ZG
2406
2407 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1fd5d4fe 2408 MemoryDeviceInfoKind_lookup[value->type],
a631892f
ZG
2409 di->id ? di->id : "");
2410 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
2411 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
2412 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
2413 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
2414 monitor_printf(mon, " memdev: %s\n", di->memdev);
2415 monitor_printf(mon, " hotplugged: %s\n",
2416 di->hotplugged ? "true" : "false");
2417 monitor_printf(mon, " hotpluggable: %s\n",
2418 di->hotpluggable ? "true" : "false");
2419 break;
2420 default:
2421 break;
2422 }
2423 }
2424 }
2425
2426 qapi_free_MemoryDeviceInfoList(info_list);
2427}
89d7fa9e 2428
62313160
TW
2429void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2430{
2431 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2432 IOThreadInfoList *info;
5fc00480 2433 IOThreadInfo *value;
62313160
TW
2434
2435 for (info = info_list; info; info = info->next) {
5fc00480
PH
2436 value = info->value;
2437 monitor_printf(mon, "%s:\n", value->id);
2438 monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
2439 monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2440 monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
2441 monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
62313160
TW
2442 }
2443
2444 qapi_free_IOThreadInfoList(info_list);
2445}
2446
89d7fa9e
AF
2447void hmp_qom_list(Monitor *mon, const QDict *qdict)
2448{
2449 const char *path = qdict_get_try_str(qdict, "path");
2450 ObjectPropertyInfoList *list;
2451 Error *err = NULL;
2452
2453 if (path == NULL) {
2454 monitor_printf(mon, "/\n");
2455 return;
2456 }
2457
2458 list = qmp_qom_list(path, &err);
2459 if (err == NULL) {
2460 ObjectPropertyInfoList *start = list;
2461 while (list != NULL) {
2462 ObjectPropertyInfo *value = list->value;
2463
2464 monitor_printf(mon, "%s (%s)\n",
2465 value->name, value->type);
2466 list = list->next;
2467 }
2468 qapi_free_ObjectPropertyInfoList(start);
2469 }
2470 hmp_handle_error(mon, &err);
2471}
c0e6ee9e
AF
2472
2473void hmp_qom_set(Monitor *mon, const QDict *qdict)
2474{
2475 const char *path = qdict_get_str(qdict, "path");
2476 const char *property = qdict_get_str(qdict, "property");
2477 const char *value = qdict_get_str(qdict, "value");
2478 Error *err = NULL;
2479 bool ambiguous = false;
2480 Object *obj;
2481
2482 obj = object_resolve_path(path, &ambiguous);
2483 if (obj == NULL) {
75158ebb
MA
2484 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2485 "Device '%s' not found", path);
c0e6ee9e
AF
2486 } else {
2487 if (ambiguous) {
2488 monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2489 }
2490 object_property_parse(obj, value, property, &err);
2491 }
2492 hmp_handle_error(mon, &err);
2493}
fafa4d50
SF
2494
2495void hmp_rocker(Monitor *mon, const QDict *qdict)
2496{
2497 const char *name = qdict_get_str(qdict, "name");
2498 RockerSwitch *rocker;
533fdaed 2499 Error *err = NULL;
fafa4d50 2500
533fdaed
MA
2501 rocker = qmp_query_rocker(name, &err);
2502 if (err != NULL) {
2503 hmp_handle_error(mon, &err);
fafa4d50
SF
2504 return;
2505 }
2506
2507 monitor_printf(mon, "name: %s\n", rocker->name);
2508 monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2509 monitor_printf(mon, "ports: %d\n", rocker->ports);
2510
2511 qapi_free_RockerSwitch(rocker);
2512}
2513
2514void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2515{
2516 RockerPortList *list, *port;
2517 const char *name = qdict_get_str(qdict, "name");
533fdaed 2518 Error *err = NULL;
fafa4d50 2519
533fdaed
MA
2520 list = qmp_query_rocker_ports(name, &err);
2521 if (err != NULL) {
2522 hmp_handle_error(mon, &err);
fafa4d50
SF
2523 return;
2524 }
2525
2526 monitor_printf(mon, " ena/ speed/ auto\n");
2527 monitor_printf(mon, " port link duplex neg?\n");
2528
2529 for (port = list; port; port = port->next) {
2530 monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
2531 port->value->name,
2532 port->value->enabled ? port->value->link_up ?
2533 "up" : "down" : "!ena",
2534 port->value->speed == 10000 ? "10G" : "??",
2535 port->value->duplex ? "FD" : "HD",
2536 port->value->autoneg ? "Yes" : "No");
2537 }
2538
2539 qapi_free_RockerPortList(list);
2540}
2541
2542void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2543{
2544 RockerOfDpaFlowList *list, *info;
2545 const char *name = qdict_get_str(qdict, "name");
2546 uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
533fdaed 2547 Error *err = NULL;
fafa4d50 2548
533fdaed
MA
2549 list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2550 if (err != NULL) {
2551 hmp_handle_error(mon, &err);
fafa4d50
SF
2552 return;
2553 }
2554
2555 monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2556
2557 for (info = list; info; info = info->next) {
2558 RockerOfDpaFlow *flow = info->value;
2559 RockerOfDpaFlowKey *key = flow->key;
2560 RockerOfDpaFlowMask *mask = flow->mask;
2561 RockerOfDpaFlowAction *action = flow->action;
2562
2563 if (flow->hits) {
2564 monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2565 key->priority, key->tbl_id, flow->hits);
2566 } else {
2567 monitor_printf(mon, "%-4d %-3d ",
2568 key->priority, key->tbl_id);
2569 }
2570
2571 if (key->has_in_pport) {
2572 monitor_printf(mon, " pport %d", key->in_pport);
2573 if (mask->has_in_pport) {
2574 monitor_printf(mon, "(0x%x)", mask->in_pport);
2575 }
2576 }
2577
2578 if (key->has_vlan_id) {
2579 monitor_printf(mon, " vlan %d",
2580 key->vlan_id & VLAN_VID_MASK);
2581 if (mask->has_vlan_id) {
2582 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2583 }
2584 }
2585
2586 if (key->has_tunnel_id) {
2587 monitor_printf(mon, " tunnel %d", key->tunnel_id);
2588 if (mask->has_tunnel_id) {
2589 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2590 }
2591 }
2592
2593 if (key->has_eth_type) {
2594 switch (key->eth_type) {
2595 case 0x0806:
2596 monitor_printf(mon, " ARP");
2597 break;
2598 case 0x0800:
2599 monitor_printf(mon, " IP");
2600 break;
2601 case 0x86dd:
2602 monitor_printf(mon, " IPv6");
2603 break;
2604 case 0x8809:
2605 monitor_printf(mon, " LACP");
2606 break;
2607 case 0x88cc:
2608 monitor_printf(mon, " LLDP");
2609 break;
2610 default:
2611 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2612 break;
2613 }
2614 }
2615
2616 if (key->has_eth_src) {
2617 if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2618 (mask->has_eth_src) &&
2619 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2620 monitor_printf(mon, " src <any mcast/bcast>");
2621 } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2622 (mask->has_eth_src) &&
2623 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2624 monitor_printf(mon, " src <any ucast>");
2625 } else {
2626 monitor_printf(mon, " src %s", key->eth_src);
2627 if (mask->has_eth_src) {
2628 monitor_printf(mon, "(%s)", mask->eth_src);
2629 }
2630 }
2631 }
2632
2633 if (key->has_eth_dst) {
2634 if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2635 (mask->has_eth_dst) &&
2636 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2637 monitor_printf(mon, " dst <any mcast/bcast>");
2638 } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2639 (mask->has_eth_dst) &&
2640 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2641 monitor_printf(mon, " dst <any ucast>");
2642 } else {
2643 monitor_printf(mon, " dst %s", key->eth_dst);
2644 if (mask->has_eth_dst) {
2645 monitor_printf(mon, "(%s)", mask->eth_dst);
2646 }
2647 }
2648 }
2649
2650 if (key->has_ip_proto) {
2651 monitor_printf(mon, " proto %d", key->ip_proto);
2652 if (mask->has_ip_proto) {
2653 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2654 }
2655 }
2656
2657 if (key->has_ip_tos) {
2658 monitor_printf(mon, " TOS %d", key->ip_tos);
2659 if (mask->has_ip_tos) {
2660 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2661 }
2662 }
2663
2664 if (key->has_ip_dst) {
2665 monitor_printf(mon, " dst %s", key->ip_dst);
2666 }
2667
2668 if (action->has_goto_tbl || action->has_group_id ||
2669 action->has_new_vlan_id) {
2670 monitor_printf(mon, " -->");
2671 }
2672
2673 if (action->has_new_vlan_id) {
2674 monitor_printf(mon, " apply new vlan %d",
2675 ntohs(action->new_vlan_id));
2676 }
2677
2678 if (action->has_group_id) {
2679 monitor_printf(mon, " write group 0x%08x", action->group_id);
2680 }
2681
2682 if (action->has_goto_tbl) {
2683 monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2684 }
2685
2686 monitor_printf(mon, "\n");
2687 }
2688
2689 qapi_free_RockerOfDpaFlowList(list);
2690}
2691
2692void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2693{
2694 RockerOfDpaGroupList *list, *g;
2695 const char *name = qdict_get_str(qdict, "name");
2696 uint8_t type = qdict_get_try_int(qdict, "type", 9);
533fdaed 2697 Error *err = NULL;
fafa4d50
SF
2698 bool set = false;
2699
533fdaed
MA
2700 list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2701 if (err != NULL) {
2702 hmp_handle_error(mon, &err);
fafa4d50
SF
2703 return;
2704 }
2705
2706 monitor_printf(mon, "id (decode) --> buckets\n");
2707
2708 for (g = list; g; g = g->next) {
2709 RockerOfDpaGroup *group = g->value;
2710
2711 monitor_printf(mon, "0x%08x", group->id);
2712
2713 monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2714 group->type == 1 ? "L2 rewrite" :
2715 group->type == 2 ? "L3 unicast" :
2716 group->type == 3 ? "L2 multicast" :
2717 group->type == 4 ? "L2 flood" :
2718 group->type == 5 ? "L3 interface" :
2719 group->type == 6 ? "L3 multicast" :
2720 group->type == 7 ? "L3 ECMP" :
2721 group->type == 8 ? "L2 overlay" :
2722 "unknown");
2723
2724 if (group->has_vlan_id) {
2725 monitor_printf(mon, " vlan %d", group->vlan_id);
2726 }
2727
2728 if (group->has_pport) {
2729 monitor_printf(mon, " pport %d", group->pport);
2730 }
2731
2732 if (group->has_index) {
2733 monitor_printf(mon, " index %d", group->index);
2734 }
2735
2736 monitor_printf(mon, ") -->");
2737
2738 if (group->has_set_vlan_id && group->set_vlan_id) {
2739 set = true;
2740 monitor_printf(mon, " set vlan %d",
2741 group->set_vlan_id & VLAN_VID_MASK);
2742 }
2743
2744 if (group->has_set_eth_src) {
2745 if (!set) {
2746 set = true;
2747 monitor_printf(mon, " set");
2748 }
2749 monitor_printf(mon, " src %s", group->set_eth_src);
2750 }
2751
2752 if (group->has_set_eth_dst) {
2753 if (!set) {
2754 set = true;
2755 monitor_printf(mon, " set");
2756 }
2757 monitor_printf(mon, " dst %s", group->set_eth_dst);
2758 }
2759
2760 set = false;
2761
2762 if (group->has_ttl_check && group->ttl_check) {
2763 monitor_printf(mon, " check TTL");
2764 }
2765
2766 if (group->has_group_id && group->group_id) {
2767 monitor_printf(mon, " group id 0x%08x", group->group_id);
2768 }
2769
2770 if (group->has_pop_vlan && group->pop_vlan) {
2771 monitor_printf(mon, " pop vlan");
2772 }
2773
2774 if (group->has_out_pport) {
2775 monitor_printf(mon, " out pport %d", group->out_pport);
2776 }
2777
2778 if (group->has_group_ids) {
2779 struct uint32List *id;
2780
2781 monitor_printf(mon, " groups [");
2782 for (id = group->group_ids; id; id = id->next) {
2783 monitor_printf(mon, "0x%08x", id->value);
2784 if (id->next) {
2785 monitor_printf(mon, ",");
2786 }
2787 }
2788 monitor_printf(mon, "]");
2789 }
2790
2791 monitor_printf(mon, "\n");
2792 }
2793
2794 qapi_free_RockerOfDpaGroupList(list);
2795}
4a6b52d6
PX
2796
2797void hmp_info_dump(Monitor *mon, const QDict *qdict)
2798{
2799 DumpQueryResult *result = qmp_query_dump(NULL);
2800
2801 assert(result && result->status < DUMP_STATUS__MAX);
2802 monitor_printf(mon, "Status: %s\n", DumpStatus_lookup[result->status]);
2803
2804 if (result->status == DUMP_STATUS_ACTIVE) {
2805 float percent = 0;
2806 assert(result->total != 0);
2807 percent = 100.0 * result->completed / result->total;
2808 monitor_printf(mon, "Finished: %.2f %%\n", percent);
2809 }
2810
2811 qapi_free_DumpQueryResult(result);
2812}
d2d8d46f 2813
be9b23c4
PX
2814void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2815{
2816 ram_block_dump(mon);
2817}
2818
d2d8d46f
BR
2819void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2820{
2821 Error *err = NULL;
2822 HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2823 HotpluggableCPUList *saved = l;
2824 CpuInstanceProperties *c;
2825
2826 if (err != NULL) {
2827 hmp_handle_error(mon, &err);
2828 return;
2829 }
2830
2831 monitor_printf(mon, "Hotpluggable CPUs:\n");
2832 while (l) {
2833 monitor_printf(mon, " type: \"%s\"\n", l->value->type);
2834 monitor_printf(mon, " vcpus_count: \"%" PRIu64 "\"\n",
2835 l->value->vcpus_count);
2836 if (l->value->has_qom_path) {
2837 monitor_printf(mon, " qom_path: \"%s\"\n", l->value->qom_path);
2838 }
2839
2840 c = l->value->props;
2841 monitor_printf(mon, " CPUInstance Properties:\n");
27393c33
PK
2842 if (c->has_node_id) {
2843 monitor_printf(mon, " node-id: \"%" PRIu64 "\"\n", c->node_id);
d2d8d46f 2844 }
27393c33
PK
2845 if (c->has_socket_id) {
2846 monitor_printf(mon, " socket-id: \"%" PRIu64 "\"\n", c->socket_id);
d2d8d46f 2847 }
27393c33
PK
2848 if (c->has_core_id) {
2849 monitor_printf(mon, " core-id: \"%" PRIu64 "\"\n", c->core_id);
d2d8d46f 2850 }
27393c33
PK
2851 if (c->has_thread_id) {
2852 monitor_printf(mon, " thread-id: \"%" PRIu64 "\"\n", c->thread_id);
d2d8d46f
BR
2853 }
2854
2855 l = l->next;
2856 }
2857
2858 qapi_free_HotpluggableCPUList(saved);
2859}
39164c13
IM
2860
2861void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2862{
72d9196f
BW
2863 Error *err = NULL;
2864 GuidInfo *info = qmp_query_vm_generation_id(&err);
39164c13
IM
2865 if (info) {
2866 monitor_printf(mon, "%s\n", info->guid);
2867 }
72d9196f 2868 hmp_handle_error(mon, &err);
39164c13
IM
2869 qapi_free_GuidInfo(info);
2870}