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