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