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