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