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