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