]> git.proxmox.com Git - mirror_qemu.git/blame - hmp.c
block: set device_list.tqe_prev to NULL on BDS removal
[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;
1734 char keyname_buf[16];
1735 char *separator;
9f328977 1736 int keyname_len;
e4c8f004
AK
1737
1738 while (1) {
1739 separator = strchr(keys, '-');
1740 keyname_len = separator ? separator - keys : strlen(keys);
1741 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1742
1743 /* Be compatible with old interface, convert user inputted "<" */
1744 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1745 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1746 keyname_len = 4;
1747 }
1748 keyname_buf[keyname_len] = 0;
1749
e4c8f004 1750 keylist = g_malloc0(sizeof(*keylist));
9f328977 1751 keylist->value = g_malloc0(sizeof(*keylist->value));
e4c8f004
AK
1752
1753 if (!head) {
1754 head = keylist;
1755 }
1756 if (tmp) {
1757 tmp->next = keylist;
1758 }
1759 tmp = keylist;
1760
9f328977
LC
1761 if (strstart(keyname_buf, "0x", NULL)) {
1762 char *endp;
1763 int value = strtoul(keyname_buf, &endp, 0);
1764 if (*endp != '\0') {
1765 goto err_out;
1766 }
568c73a4
EB
1767 keylist->value->type = KEY_VALUE_KIND_NUMBER;
1768 keylist->value->u.number = value;
9f328977
LC
1769 } else {
1770 int idx = index_from_key(keyname_buf);
7fb1cf16 1771 if (idx == Q_KEY_CODE__MAX) {
9f328977
LC
1772 goto err_out;
1773 }
568c73a4
EB
1774 keylist->value->type = KEY_VALUE_KIND_QCODE;
1775 keylist->value->u.qcode = idx;
9f328977
LC
1776 }
1777
e4c8f004
AK
1778 if (!separator) {
1779 break;
1780 }
1781 keys = separator + 1;
1782 }
1783
9f328977 1784 qmp_send_key(head, has_hold_time, hold_time, &err);
e4c8f004 1785 hmp_handle_error(mon, &err);
9f328977
LC
1786
1787out:
1788 qapi_free_KeyValueList(head);
1789 return;
1790
1791err_out:
1792 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1793 goto out;
e4c8f004 1794}
ad39cf6d 1795
3e5a50d6 1796void hmp_screendump(Monitor *mon, const QDict *qdict)
ad39cf6d
LC
1797{
1798 const char *filename = qdict_get_str(qdict, "filename");
1799 Error *err = NULL;
1800
1801 qmp_screendump(filename, &err);
1802 hmp_handle_error(mon, &err);
1803}
4057725f
PB
1804
1805void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1806{
1807 const char *uri = qdict_get_str(qdict, "uri");
34acbc95
EB
1808 bool writable = qdict_get_try_bool(qdict, "writable", false);
1809 bool all = qdict_get_try_bool(qdict, "all", false);
4057725f
PB
1810 Error *local_err = NULL;
1811 BlockInfoList *block_list, *info;
1812 SocketAddress *addr;
1813
1814 if (writable && !all) {
1815 error_setg(&local_err, "-w only valid together with -a");
1816 goto exit;
1817 }
1818
1819 /* First check if the address is valid and start the server. */
1820 addr = socket_parse(uri, &local_err);
1821 if (local_err != NULL) {
1822 goto exit;
1823 }
1824
1825 qmp_nbd_server_start(addr, &local_err);
1826 qapi_free_SocketAddress(addr);
1827 if (local_err != NULL) {
1828 goto exit;
1829 }
1830
1831 if (!all) {
1832 return;
1833 }
1834
1835 /* Then try adding all block devices. If one fails, close all and
1836 * exit.
1837 */
1838 block_list = qmp_query_block(NULL);
1839
1840 for (info = block_list; info; info = info->next) {
1841 if (!info->value->has_inserted) {
1842 continue;
1843 }
1844
1845 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1846
1847 if (local_err != NULL) {
1848 qmp_nbd_server_stop(NULL);
1849 break;
1850 }
1851 }
1852
1853 qapi_free_BlockInfoList(block_list);
1854
1855exit:
1856 hmp_handle_error(mon, &local_err);
1857}
1858
1859void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1860{
1861 const char *device = qdict_get_str(qdict, "device");
34acbc95 1862 bool writable = qdict_get_try_bool(qdict, "writable", false);
4057725f
PB
1863 Error *local_err = NULL;
1864
1865 qmp_nbd_server_add(device, true, writable, &local_err);
1866
1867 if (local_err != NULL) {
1868 hmp_handle_error(mon, &local_err);
1869 }
1870}
1871
1872void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1873{
e940f543 1874 Error *err = NULL;
4057725f 1875
e940f543
MA
1876 qmp_nbd_server_stop(&err);
1877 hmp_handle_error(mon, &err);
4057725f 1878}
f1088908 1879
abf23329
JH
1880void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1881{
1882 int cpuid;
1883 Error *err = NULL;
1884
1885 cpuid = qdict_get_int(qdict, "id");
1886 qmp_cpu_add(cpuid, &err);
1887 hmp_handle_error(mon, &err);
1888}
1889
f1088908
GH
1890void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1891{
1892 const char *args = qdict_get_str(qdict, "args");
1893 Error *err = NULL;
1894 QemuOpts *opts;
1895
70b94331 1896 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
f1088908 1897 if (opts == NULL) {
312fd5f2 1898 error_setg(&err, "Parsing chardev args failed");
f1088908
GH
1899 } else {
1900 qemu_chr_new_from_opts(opts, NULL, &err);
1901 }
1902 hmp_handle_error(mon, &err);
1903}
1904
1905void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1906{
1907 Error *local_err = NULL;
1908
1909 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1910 hmp_handle_error(mon, &local_err);
1911}
587da2c3
KW
1912
1913void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1914{
4c7b7e9b 1915 BlockBackend *blk;
587da2c3
KW
1916 const char* device = qdict_get_str(qdict, "device");
1917 const char* command = qdict_get_str(qdict, "command");
1918 Error *err = NULL;
1919
4c7b7e9b
HR
1920 blk = blk_by_name(device);
1921 if (blk) {
1922 qemuio_command(blk, command);
587da2c3 1923 } else {
75158ebb
MA
1924 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
1925 "Device '%s' not found", device);
587da2c3
KW
1926 }
1927
1928 hmp_handle_error(mon, &err);
1929}
ab2d0531
PB
1930
1931void hmp_object_del(Monitor *mon, const QDict *qdict)
1932{
1933 const char *id = qdict_get_str(qdict, "id");
1934 Error *err = NULL;
1935
1936 qmp_object_del(id, &err);
1937 hmp_handle_error(mon, &err);
1938}
eb1539b2
HT
1939
1940void hmp_info_memdev(Monitor *mon, const QDict *qdict)
1941{
1942 Error *err = NULL;
1943 MemdevList *memdev_list = qmp_query_memdev(&err);
1944 MemdevList *m = memdev_list;
1945 StringOutputVisitor *ov;
976620ac 1946 char *str;
eb1539b2
HT
1947 int i = 0;
1948
1949
1950 while (m) {
1951 ov = string_output_visitor_new(false);
1952 visit_type_uint16List(string_output_get_visitor(ov),
1953 &m->value->host_nodes, NULL, NULL);
8f4e5ac3 1954 monitor_printf(mon, "memory backend: %d\n", i);
eb1539b2
HT
1955 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
1956 monitor_printf(mon, " merge: %s\n",
1957 m->value->merge ? "true" : "false");
1958 monitor_printf(mon, " dump: %s\n",
1959 m->value->dump ? "true" : "false");
1960 monitor_printf(mon, " prealloc: %s\n",
1961 m->value->prealloc ? "true" : "false");
1962 monitor_printf(mon, " policy: %s\n",
1963 HostMemPolicy_lookup[m->value->policy]);
976620ac
CF
1964 str = string_output_get_string(ov);
1965 monitor_printf(mon, " host nodes: %s\n", str);
eb1539b2 1966
976620ac 1967 g_free(str);
eb1539b2
HT
1968 string_output_visitor_cleanup(ov);
1969 m = m->next;
1970 i++;
1971 }
1972
1973 monitor_printf(mon, "\n");
ecaf54a0
CF
1974
1975 qapi_free_MemdevList(memdev_list);
eb1539b2 1976}
a631892f
ZG
1977
1978void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
1979{
1980 Error *err = NULL;
1981 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
1982 MemoryDeviceInfoList *info;
1983 MemoryDeviceInfo *value;
1984 PCDIMMDeviceInfo *di;
1985
1986 for (info = info_list; info; info = info->next) {
1987 value = info->value;
1988
1989 if (value) {
1fd5d4fe 1990 switch (value->type) {
a631892f 1991 case MEMORY_DEVICE_INFO_KIND_DIMM:
1fd5d4fe 1992 di = value->u.dimm;
a631892f
ZG
1993
1994 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1fd5d4fe 1995 MemoryDeviceInfoKind_lookup[value->type],
a631892f
ZG
1996 di->id ? di->id : "");
1997 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
1998 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
1999 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
2000 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
2001 monitor_printf(mon, " memdev: %s\n", di->memdev);
2002 monitor_printf(mon, " hotplugged: %s\n",
2003 di->hotplugged ? "true" : "false");
2004 monitor_printf(mon, " hotpluggable: %s\n",
2005 di->hotpluggable ? "true" : "false");
2006 break;
2007 default:
2008 break;
2009 }
2010 }
2011 }
2012
2013 qapi_free_MemoryDeviceInfoList(info_list);
2014}
89d7fa9e 2015
62313160
TW
2016void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2017{
2018 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2019 IOThreadInfoList *info;
2020
2021 for (info = info_list; info; info = info->next) {
2022 monitor_printf(mon, "%s: thread_id=%" PRId64 "\n",
2023 info->value->id, info->value->thread_id);
2024 }
2025
2026 qapi_free_IOThreadInfoList(info_list);
2027}
2028
89d7fa9e
AF
2029void hmp_qom_list(Monitor *mon, const QDict *qdict)
2030{
2031 const char *path = qdict_get_try_str(qdict, "path");
2032 ObjectPropertyInfoList *list;
2033 Error *err = NULL;
2034
2035 if (path == NULL) {
2036 monitor_printf(mon, "/\n");
2037 return;
2038 }
2039
2040 list = qmp_qom_list(path, &err);
2041 if (err == NULL) {
2042 ObjectPropertyInfoList *start = list;
2043 while (list != NULL) {
2044 ObjectPropertyInfo *value = list->value;
2045
2046 monitor_printf(mon, "%s (%s)\n",
2047 value->name, value->type);
2048 list = list->next;
2049 }
2050 qapi_free_ObjectPropertyInfoList(start);
2051 }
2052 hmp_handle_error(mon, &err);
2053}
c0e6ee9e
AF
2054
2055void hmp_qom_set(Monitor *mon, const QDict *qdict)
2056{
2057 const char *path = qdict_get_str(qdict, "path");
2058 const char *property = qdict_get_str(qdict, "property");
2059 const char *value = qdict_get_str(qdict, "value");
2060 Error *err = NULL;
2061 bool ambiguous = false;
2062 Object *obj;
2063
2064 obj = object_resolve_path(path, &ambiguous);
2065 if (obj == NULL) {
75158ebb
MA
2066 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2067 "Device '%s' not found", path);
c0e6ee9e
AF
2068 } else {
2069 if (ambiguous) {
2070 monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2071 }
2072 object_property_parse(obj, value, property, &err);
2073 }
2074 hmp_handle_error(mon, &err);
2075}
fafa4d50
SF
2076
2077void hmp_rocker(Monitor *mon, const QDict *qdict)
2078{
2079 const char *name = qdict_get_str(qdict, "name");
2080 RockerSwitch *rocker;
533fdaed 2081 Error *err = NULL;
fafa4d50 2082
533fdaed
MA
2083 rocker = qmp_query_rocker(name, &err);
2084 if (err != NULL) {
2085 hmp_handle_error(mon, &err);
fafa4d50
SF
2086 return;
2087 }
2088
2089 monitor_printf(mon, "name: %s\n", rocker->name);
2090 monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2091 monitor_printf(mon, "ports: %d\n", rocker->ports);
2092
2093 qapi_free_RockerSwitch(rocker);
2094}
2095
2096void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2097{
2098 RockerPortList *list, *port;
2099 const char *name = qdict_get_str(qdict, "name");
533fdaed 2100 Error *err = NULL;
fafa4d50 2101
533fdaed
MA
2102 list = qmp_query_rocker_ports(name, &err);
2103 if (err != NULL) {
2104 hmp_handle_error(mon, &err);
fafa4d50
SF
2105 return;
2106 }
2107
2108 monitor_printf(mon, " ena/ speed/ auto\n");
2109 monitor_printf(mon, " port link duplex neg?\n");
2110
2111 for (port = list; port; port = port->next) {
2112 monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
2113 port->value->name,
2114 port->value->enabled ? port->value->link_up ?
2115 "up" : "down" : "!ena",
2116 port->value->speed == 10000 ? "10G" : "??",
2117 port->value->duplex ? "FD" : "HD",
2118 port->value->autoneg ? "Yes" : "No");
2119 }
2120
2121 qapi_free_RockerPortList(list);
2122}
2123
2124void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2125{
2126 RockerOfDpaFlowList *list, *info;
2127 const char *name = qdict_get_str(qdict, "name");
2128 uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
533fdaed 2129 Error *err = NULL;
fafa4d50 2130
533fdaed
MA
2131 list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2132 if (err != NULL) {
2133 hmp_handle_error(mon, &err);
fafa4d50
SF
2134 return;
2135 }
2136
2137 monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2138
2139 for (info = list; info; info = info->next) {
2140 RockerOfDpaFlow *flow = info->value;
2141 RockerOfDpaFlowKey *key = flow->key;
2142 RockerOfDpaFlowMask *mask = flow->mask;
2143 RockerOfDpaFlowAction *action = flow->action;
2144
2145 if (flow->hits) {
2146 monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2147 key->priority, key->tbl_id, flow->hits);
2148 } else {
2149 monitor_printf(mon, "%-4d %-3d ",
2150 key->priority, key->tbl_id);
2151 }
2152
2153 if (key->has_in_pport) {
2154 monitor_printf(mon, " pport %d", key->in_pport);
2155 if (mask->has_in_pport) {
2156 monitor_printf(mon, "(0x%x)", mask->in_pport);
2157 }
2158 }
2159
2160 if (key->has_vlan_id) {
2161 monitor_printf(mon, " vlan %d",
2162 key->vlan_id & VLAN_VID_MASK);
2163 if (mask->has_vlan_id) {
2164 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2165 }
2166 }
2167
2168 if (key->has_tunnel_id) {
2169 monitor_printf(mon, " tunnel %d", key->tunnel_id);
2170 if (mask->has_tunnel_id) {
2171 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2172 }
2173 }
2174
2175 if (key->has_eth_type) {
2176 switch (key->eth_type) {
2177 case 0x0806:
2178 monitor_printf(mon, " ARP");
2179 break;
2180 case 0x0800:
2181 monitor_printf(mon, " IP");
2182 break;
2183 case 0x86dd:
2184 monitor_printf(mon, " IPv6");
2185 break;
2186 case 0x8809:
2187 monitor_printf(mon, " LACP");
2188 break;
2189 case 0x88cc:
2190 monitor_printf(mon, " LLDP");
2191 break;
2192 default:
2193 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2194 break;
2195 }
2196 }
2197
2198 if (key->has_eth_src) {
2199 if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2200 (mask->has_eth_src) &&
2201 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2202 monitor_printf(mon, " src <any mcast/bcast>");
2203 } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2204 (mask->has_eth_src) &&
2205 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2206 monitor_printf(mon, " src <any ucast>");
2207 } else {
2208 monitor_printf(mon, " src %s", key->eth_src);
2209 if (mask->has_eth_src) {
2210 monitor_printf(mon, "(%s)", mask->eth_src);
2211 }
2212 }
2213 }
2214
2215 if (key->has_eth_dst) {
2216 if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2217 (mask->has_eth_dst) &&
2218 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2219 monitor_printf(mon, " dst <any mcast/bcast>");
2220 } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2221 (mask->has_eth_dst) &&
2222 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2223 monitor_printf(mon, " dst <any ucast>");
2224 } else {
2225 monitor_printf(mon, " dst %s", key->eth_dst);
2226 if (mask->has_eth_dst) {
2227 monitor_printf(mon, "(%s)", mask->eth_dst);
2228 }
2229 }
2230 }
2231
2232 if (key->has_ip_proto) {
2233 monitor_printf(mon, " proto %d", key->ip_proto);
2234 if (mask->has_ip_proto) {
2235 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2236 }
2237 }
2238
2239 if (key->has_ip_tos) {
2240 monitor_printf(mon, " TOS %d", key->ip_tos);
2241 if (mask->has_ip_tos) {
2242 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2243 }
2244 }
2245
2246 if (key->has_ip_dst) {
2247 monitor_printf(mon, " dst %s", key->ip_dst);
2248 }
2249
2250 if (action->has_goto_tbl || action->has_group_id ||
2251 action->has_new_vlan_id) {
2252 monitor_printf(mon, " -->");
2253 }
2254
2255 if (action->has_new_vlan_id) {
2256 monitor_printf(mon, " apply new vlan %d",
2257 ntohs(action->new_vlan_id));
2258 }
2259
2260 if (action->has_group_id) {
2261 monitor_printf(mon, " write group 0x%08x", action->group_id);
2262 }
2263
2264 if (action->has_goto_tbl) {
2265 monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2266 }
2267
2268 monitor_printf(mon, "\n");
2269 }
2270
2271 qapi_free_RockerOfDpaFlowList(list);
2272}
2273
2274void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2275{
2276 RockerOfDpaGroupList *list, *g;
2277 const char *name = qdict_get_str(qdict, "name");
2278 uint8_t type = qdict_get_try_int(qdict, "type", 9);
533fdaed 2279 Error *err = NULL;
fafa4d50
SF
2280 bool set = false;
2281
533fdaed
MA
2282 list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2283 if (err != NULL) {
2284 hmp_handle_error(mon, &err);
fafa4d50
SF
2285 return;
2286 }
2287
2288 monitor_printf(mon, "id (decode) --> buckets\n");
2289
2290 for (g = list; g; g = g->next) {
2291 RockerOfDpaGroup *group = g->value;
2292
2293 monitor_printf(mon, "0x%08x", group->id);
2294
2295 monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2296 group->type == 1 ? "L2 rewrite" :
2297 group->type == 2 ? "L3 unicast" :
2298 group->type == 3 ? "L2 multicast" :
2299 group->type == 4 ? "L2 flood" :
2300 group->type == 5 ? "L3 interface" :
2301 group->type == 6 ? "L3 multicast" :
2302 group->type == 7 ? "L3 ECMP" :
2303 group->type == 8 ? "L2 overlay" :
2304 "unknown");
2305
2306 if (group->has_vlan_id) {
2307 monitor_printf(mon, " vlan %d", group->vlan_id);
2308 }
2309
2310 if (group->has_pport) {
2311 monitor_printf(mon, " pport %d", group->pport);
2312 }
2313
2314 if (group->has_index) {
2315 monitor_printf(mon, " index %d", group->index);
2316 }
2317
2318 monitor_printf(mon, ") -->");
2319
2320 if (group->has_set_vlan_id && group->set_vlan_id) {
2321 set = true;
2322 monitor_printf(mon, " set vlan %d",
2323 group->set_vlan_id & VLAN_VID_MASK);
2324 }
2325
2326 if (group->has_set_eth_src) {
2327 if (!set) {
2328 set = true;
2329 monitor_printf(mon, " set");
2330 }
2331 monitor_printf(mon, " src %s", group->set_eth_src);
2332 }
2333
2334 if (group->has_set_eth_dst) {
2335 if (!set) {
2336 set = true;
2337 monitor_printf(mon, " set");
2338 }
2339 monitor_printf(mon, " dst %s", group->set_eth_dst);
2340 }
2341
2342 set = false;
2343
2344 if (group->has_ttl_check && group->ttl_check) {
2345 monitor_printf(mon, " check TTL");
2346 }
2347
2348 if (group->has_group_id && group->group_id) {
2349 monitor_printf(mon, " group id 0x%08x", group->group_id);
2350 }
2351
2352 if (group->has_pop_vlan && group->pop_vlan) {
2353 monitor_printf(mon, " pop vlan");
2354 }
2355
2356 if (group->has_out_pport) {
2357 monitor_printf(mon, " out pport %d", group->out_pport);
2358 }
2359
2360 if (group->has_group_ids) {
2361 struct uint32List *id;
2362
2363 monitor_printf(mon, " groups [");
2364 for (id = group->group_ids; id; id = id->next) {
2365 monitor_printf(mon, "0x%08x", id->value);
2366 if (id->next) {
2367 monitor_printf(mon, ",");
2368 }
2369 }
2370 monitor_printf(mon, "]");
2371 }
2372
2373 monitor_printf(mon, "\n");
2374 }
2375
2376 qapi_free_RockerOfDpaGroupList(list);
2377}