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