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