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