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