]> git.proxmox.com Git - mirror_qemu.git/blame - hmp.c
NUMA: Add numa_info structure to contain numa nodes info
[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"
dccfcd0e 18#include "sysemu/char.h"
1de7afc9
PB
19#include "qemu/option.h"
20#include "qemu/timer.h"
48a32bed 21#include "qmp-commands.h"
1de7afc9 22#include "qemu/sockets.h"
83c9089e 23#include "monitor/monitor.h"
cff8b2c6 24#include "qapi/opts-visitor.h"
28ecbaee 25#include "ui/console.h"
bd093a36 26#include "block/qapi.h"
587da2c3 27#include "qemu-io.h"
48a32bed 28
0cfd6a9a
LC
29static void hmp_handle_error(Monitor *mon, Error **errp)
30{
415168e0
MA
31 assert(errp);
32 if (*errp) {
0cfd6a9a
LC
33 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
34 error_free(*errp);
35 }
36}
37
84f2d0ea 38void hmp_info_name(Monitor *mon, const QDict *qdict)
48a32bed
AL
39{
40 NameInfo *info;
41
42 info = qmp_query_name(NULL);
43 if (info->has_name) {
44 monitor_printf(mon, "%s\n", info->name);
45 }
46 qapi_free_NameInfo(info);
47}
b9c15f16 48
84f2d0ea 49void hmp_info_version(Monitor *mon, const QDict *qdict)
b9c15f16
LC
50{
51 VersionInfo *info;
52
53 info = qmp_query_version(NULL);
54
55 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
56 info->qemu.major, info->qemu.minor, info->qemu.micro,
57 info->package);
58
59 qapi_free_VersionInfo(info);
60}
292a2602 61
84f2d0ea 62void hmp_info_kvm(Monitor *mon, const QDict *qdict)
292a2602
LC
63{
64 KvmInfo *info;
65
66 info = qmp_query_kvm(NULL);
67 monitor_printf(mon, "kvm support: ");
68 if (info->present) {
69 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
70 } else {
71 monitor_printf(mon, "not compiled\n");
72 }
73
74 qapi_free_KvmInfo(info);
75}
76
84f2d0ea 77void hmp_info_status(Monitor *mon, const QDict *qdict)
1fa9a5e4
LC
78{
79 StatusInfo *info;
80
81 info = qmp_query_status(NULL);
82
83 monitor_printf(mon, "VM status: %s%s",
84 info->running ? "running" : "paused",
85 info->singlestep ? " (single step mode)" : "");
86
87 if (!info->running && info->status != RUN_STATE_PAUSED) {
88 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
89 }
90
91 monitor_printf(mon, "\n");
92
93 qapi_free_StatusInfo(info);
94}
95
84f2d0ea 96void hmp_info_uuid(Monitor *mon, const QDict *qdict)
efab767e
LC
97{
98 UuidInfo *info;
99
100 info = qmp_query_uuid(NULL);
101 monitor_printf(mon, "%s\n", info->UUID);
102 qapi_free_UuidInfo(info);
103}
c5a415a0 104
84f2d0ea 105void hmp_info_chardev(Monitor *mon, const QDict *qdict)
c5a415a0
LC
106{
107 ChardevInfoList *char_info, *info;
108
109 char_info = qmp_query_chardev(NULL);
110 for (info = char_info; info; info = info->next) {
111 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
112 info->value->filename);
113 }
114
115 qapi_free_ChardevInfoList(char_info);
116}
7a7f325e 117
84f2d0ea 118void hmp_info_mice(Monitor *mon, const QDict *qdict)
e235cec3
LC
119{
120 MouseInfoList *mice_list, *mouse;
121
122 mice_list = qmp_query_mice(NULL);
123 if (!mice_list) {
124 monitor_printf(mon, "No mouse devices connected\n");
125 return;
126 }
127
128 for (mouse = mice_list; mouse; mouse = mouse->next) {
129 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
130 mouse->value->current ? '*' : ' ',
131 mouse->value->index, mouse->value->name,
132 mouse->value->absolute ? " (absolute)" : "");
133 }
134
135 qapi_free_MouseInfoList(mice_list);
136}
137
84f2d0ea 138void hmp_info_migrate(Monitor *mon, const QDict *qdict)
791e7c82
LC
139{
140 MigrationInfo *info;
bbf6da32 141 MigrationCapabilityStatusList *caps, *cap;
791e7c82
LC
142
143 info = qmp_query_migrate(NULL);
bbf6da32
OW
144 caps = qmp_query_migrate_capabilities(NULL);
145
146 /* do not display parameters during setup */
147 if (info->has_status && caps) {
148 monitor_printf(mon, "capabilities: ");
149 for (cap = caps; cap; cap = cap->next) {
150 monitor_printf(mon, "%s: %s ",
151 MigrationCapability_lookup[cap->value->capability],
152 cap->value->state ? "on" : "off");
153 }
154 monitor_printf(mon, "\n");
155 }
791e7c82
LC
156
157 if (info->has_status) {
158 monitor_printf(mon, "Migration status: %s\n", info->status);
7aa939af
JQ
159 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
160 info->total_time);
2c52ddf1
JQ
161 if (info->has_expected_downtime) {
162 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
163 info->expected_downtime);
164 }
9c5a9fcf
JQ
165 if (info->has_downtime) {
166 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
167 info->downtime);
168 }
ed4fbd10
MH
169 if (info->has_setup_time) {
170 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
171 info->setup_time);
172 }
791e7c82
LC
173 }
174
175 if (info->has_ram) {
176 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
177 info->ram->transferred >> 10);
7e114f8c
MH
178 monitor_printf(mon, "throughput: %0.2f mbps\n",
179 info->ram->mbps);
791e7c82
LC
180 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
181 info->ram->remaining >> 10);
182 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
183 info->ram->total >> 10);
004d4c10
OW
184 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
185 info->ram->duplicate);
f1c72795
PL
186 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
187 info->ram->skipped);
004d4c10
OW
188 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
189 info->ram->normal);
190 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
191 info->ram->normal_bytes >> 10);
58570ed8
C
192 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
193 info->ram->dirty_sync_count);
8d017193
JQ
194 if (info->ram->dirty_pages_rate) {
195 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
196 info->ram->dirty_pages_rate);
197 }
791e7c82
LC
198 }
199
200 if (info->has_disk) {
201 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
202 info->disk->transferred >> 10);
203 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
204 info->disk->remaining >> 10);
205 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
206 info->disk->total >> 10);
207 }
208
f36d55af
OW
209 if (info->has_xbzrle_cache) {
210 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
211 info->xbzrle_cache->cache_size);
212 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
213 info->xbzrle_cache->bytes >> 10);
214 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
215 info->xbzrle_cache->pages);
216 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
217 info->xbzrle_cache->cache_miss);
8bc39233
C
218 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
219 info->xbzrle_cache->cache_miss_rate);
f36d55af
OW
220 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
221 info->xbzrle_cache->overflow);
222 }
223
791e7c82 224 qapi_free_MigrationInfo(info);
bbf6da32
OW
225 qapi_free_MigrationCapabilityStatusList(caps);
226}
227
84f2d0ea 228void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
bbf6da32
OW
229{
230 MigrationCapabilityStatusList *caps, *cap;
231
232 caps = qmp_query_migrate_capabilities(NULL);
233
234 if (caps) {
235 monitor_printf(mon, "capabilities: ");
236 for (cap = caps; cap; cap = cap->next) {
237 monitor_printf(mon, "%s: %s ",
238 MigrationCapability_lookup[cap->value->capability],
239 cap->value->state ? "on" : "off");
240 }
241 monitor_printf(mon, "\n");
242 }
243
244 qapi_free_MigrationCapabilityStatusList(caps);
791e7c82
LC
245}
246
84f2d0ea 247void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
9e1ba4cc
OW
248{
249 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
250 qmp_query_migrate_cache_size(NULL) >> 10);
251}
252
84f2d0ea 253void hmp_info_cpus(Monitor *mon, const QDict *qdict)
de0b36b6
LC
254{
255 CpuInfoList *cpu_list, *cpu;
256
257 cpu_list = qmp_query_cpus(NULL);
258
259 for (cpu = cpu_list; cpu; cpu = cpu->next) {
260 int active = ' ';
261
262 if (cpu->value->CPU == monitor_get_cpu_index()) {
263 active = '*';
264 }
265
852bef0e 266 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
de0b36b6
LC
267
268 if (cpu->value->has_pc) {
852bef0e 269 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
de0b36b6
LC
270 }
271 if (cpu->value->has_nip) {
852bef0e 272 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
de0b36b6
LC
273 }
274 if (cpu->value->has_npc) {
852bef0e 275 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
de0b36b6
LC
276 }
277 if (cpu->value->has_PC) {
852bef0e 278 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
de0b36b6
LC
279 }
280
281 if (cpu->value->halted) {
282 monitor_printf(mon, " (halted)");
283 }
284
285 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
286 }
287
288 qapi_free_CpuInfoList(cpu_list);
289}
290
84f2d0ea 291void hmp_info_block(Monitor *mon, const QDict *qdict)
b2023818
LC
292{
293 BlockInfoList *block_list, *info;
bd093a36 294 ImageInfo *image_info;
e73fe2b4
WX
295 const char *device = qdict_get_try_str(qdict, "device");
296 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
b2023818
LC
297
298 block_list = qmp_query_block(NULL);
299
300 for (info = block_list; info; info = info->next) {
e73fe2b4
WX
301 if (device && strcmp(device, info->value->device)) {
302 continue;
303 }
b2023818 304
fbe2e26c
KW
305 if (info != block_list) {
306 monitor_printf(mon, "\n");
307 }
308
309 monitor_printf(mon, "%s", info->value->device);
310 if (info->value->has_inserted) {
311 monitor_printf(mon, ": %s (%s%s%s)\n",
312 info->value->inserted->file,
313 info->value->inserted->drv,
314 info->value->inserted->ro ? ", read-only" : "",
315 info->value->inserted->encrypted ? ", encrypted" : "");
316 } else {
317 monitor_printf(mon, ": [not inserted]\n");
b2023818
LC
318 }
319
fbe2e26c
KW
320 if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
321 monitor_printf(mon, " I/O status: %s\n",
b2023818
LC
322 BlockDeviceIoStatus_lookup[info->value->io_status]);
323 }
324
fbe2e26c
KW
325 if (info->value->removable) {
326 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
327 info->value->locked ? "" : "not ",
328 info->value->tray_open ? "open" : "closed");
329 }
727f005e 330
fbe2e26c
KW
331
332 if (!info->value->has_inserted) {
333 continue;
334 }
335
336 if (info->value->inserted->has_backing_file) {
337 monitor_printf(mon,
338 " Backing file: %s "
339 "(chain depth: %" PRId64 ")\n",
340 info->value->inserted->backing_file,
341 info->value->inserted->backing_file_depth);
342 }
343
465bee1d
PL
344 if (info->value->inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
345 monitor_printf(mon, " Detect zeroes: %s\n",
346 BlockdevDetectZeroesOptions_lookup[info->value->inserted->detect_zeroes]);
347 }
348
fbe2e26c
KW
349 if (info->value->inserted->bps
350 || info->value->inserted->bps_rd
351 || info->value->inserted->bps_wr
352 || info->value->inserted->iops
353 || info->value->inserted->iops_rd
354 || info->value->inserted->iops_wr)
355 {
356 monitor_printf(mon, " I/O throttling: bps=%" PRId64
357 " bps_rd=%" PRId64 " bps_wr=%" PRId64
3e9fab69
BC
358 " bps_max=%" PRId64
359 " bps_rd_max=%" PRId64
360 " bps_wr_max=%" PRId64
fbe2e26c 361 " iops=%" PRId64 " iops_rd=%" PRId64
3e9fab69
BC
362 " iops_wr=%" PRId64
363 " iops_max=%" PRId64
364 " iops_rd_max=%" PRId64
2024c1df
BC
365 " iops_wr_max=%" PRId64
366 " iops_size=%" PRId64 "\n",
727f005e
ZYW
367 info->value->inserted->bps,
368 info->value->inserted->bps_rd,
369 info->value->inserted->bps_wr,
3e9fab69
BC
370 info->value->inserted->bps_max,
371 info->value->inserted->bps_rd_max,
372 info->value->inserted->bps_wr_max,
727f005e
ZYW
373 info->value->inserted->iops,
374 info->value->inserted->iops_rd,
3e9fab69
BC
375 info->value->inserted->iops_wr,
376 info->value->inserted->iops_max,
377 info->value->inserted->iops_rd_max,
2024c1df
BC
378 info->value->inserted->iops_wr_max,
379 info->value->inserted->iops_size);
fbe2e26c 380 }
bd093a36 381
fbe2e26c
KW
382 if (verbose) {
383 monitor_printf(mon, "\nImages:\n");
384 image_info = info->value->inserted->image;
385 while (1) {
386 bdrv_image_info_dump((fprintf_function)monitor_printf,
387 mon, image_info);
388 if (image_info->has_backing_image) {
389 image_info = image_info->backing_image;
390 } else {
391 break;
bd093a36
WX
392 }
393 }
b2023818 394 }
b2023818
LC
395 }
396
397 qapi_free_BlockInfoList(block_list);
398}
399
84f2d0ea 400void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
f11f57e4
LC
401{
402 BlockStatsList *stats_list, *stats;
403
404 stats_list = qmp_query_blockstats(NULL);
405
406 for (stats = stats_list; stats; stats = stats->next) {
407 if (!stats->value->has_device) {
408 continue;
409 }
410
411 monitor_printf(mon, "%s:", stats->value->device);
412 monitor_printf(mon, " rd_bytes=%" PRId64
413 " wr_bytes=%" PRId64
414 " rd_operations=%" PRId64
415 " wr_operations=%" PRId64
416 " flush_operations=%" PRId64
417 " wr_total_time_ns=%" PRId64
418 " rd_total_time_ns=%" PRId64
419 " flush_total_time_ns=%" PRId64
420 "\n",
421 stats->value->stats->rd_bytes,
422 stats->value->stats->wr_bytes,
423 stats->value->stats->rd_operations,
424 stats->value->stats->wr_operations,
425 stats->value->stats->flush_operations,
426 stats->value->stats->wr_total_time_ns,
427 stats->value->stats->rd_total_time_ns,
428 stats->value->stats->flush_total_time_ns);
429 }
430
431 qapi_free_BlockStatsList(stats_list);
432}
433
84f2d0ea 434void hmp_info_vnc(Monitor *mon, const QDict *qdict)
2b54aa87
LC
435{
436 VncInfo *info;
437 Error *err = NULL;
438 VncClientInfoList *client;
439
440 info = qmp_query_vnc(&err);
441 if (err) {
442 monitor_printf(mon, "%s\n", error_get_pretty(err));
443 error_free(err);
444 return;
445 }
446
447 if (!info->enabled) {
448 monitor_printf(mon, "Server: disabled\n");
449 goto out;
450 }
451
452 monitor_printf(mon, "Server:\n");
453 if (info->has_host && info->has_service) {
454 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
455 }
456 if (info->has_auth) {
457 monitor_printf(mon, " auth: %s\n", info->auth);
458 }
459
460 if (!info->has_clients || info->clients == NULL) {
461 monitor_printf(mon, "Client: none\n");
462 } else {
463 for (client = info->clients; client; client = client->next) {
464 monitor_printf(mon, "Client:\n");
465 monitor_printf(mon, " address: %s:%s\n",
466 client->value->host, client->value->service);
467 monitor_printf(mon, " x509_dname: %s\n",
468 client->value->x509_dname ?
469 client->value->x509_dname : "none");
470 monitor_printf(mon, " username: %s\n",
471 client->value->has_sasl_username ?
472 client->value->sasl_username : "none");
473 }
474 }
475
476out:
477 qapi_free_VncInfo(info);
478}
479
84f2d0ea 480void hmp_info_spice(Monitor *mon, const QDict *qdict)
d1f29646
LC
481{
482 SpiceChannelList *chan;
483 SpiceInfo *info;
484
485 info = qmp_query_spice(NULL);
486
487 if (!info->enabled) {
488 monitor_printf(mon, "Server: disabled\n");
489 goto out;
490 }
491
492 monitor_printf(mon, "Server:\n");
493 if (info->has_port) {
494 monitor_printf(mon, " address: %s:%" PRId64 "\n",
495 info->host, info->port);
496 }
497 if (info->has_tls_port) {
498 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
499 info->host, info->tls_port);
500 }
61c4efe2
YH
501 monitor_printf(mon, " migrated: %s\n",
502 info->migrated ? "true" : "false");
d1f29646
LC
503 monitor_printf(mon, " auth: %s\n", info->auth);
504 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
4efee029
AL
505 monitor_printf(mon, " mouse-mode: %s\n",
506 SpiceQueryMouseMode_lookup[info->mouse_mode]);
d1f29646
LC
507
508 if (!info->has_channels || info->channels == NULL) {
509 monitor_printf(mon, "Channels: none\n");
510 } else {
511 for (chan = info->channels; chan; chan = chan->next) {
512 monitor_printf(mon, "Channel:\n");
513 monitor_printf(mon, " address: %s:%s%s\n",
514 chan->value->host, chan->value->port,
515 chan->value->tls ? " [tls]" : "");
516 monitor_printf(mon, " session: %" PRId64 "\n",
517 chan->value->connection_id);
518 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
519 chan->value->channel_type, chan->value->channel_id);
520 }
521 }
522
523out:
524 qapi_free_SpiceInfo(info);
525}
526
84f2d0ea 527void hmp_info_balloon(Monitor *mon, const QDict *qdict)
96637bcd
LC
528{
529 BalloonInfo *info;
530 Error *err = NULL;
531
532 info = qmp_query_balloon(&err);
533 if (err) {
534 monitor_printf(mon, "%s\n", error_get_pretty(err));
535 error_free(err);
536 return;
537 }
538
01ceb97e 539 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
96637bcd
LC
540
541 qapi_free_BalloonInfo(info);
542}
543
79627472
LC
544static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
545{
546 PciMemoryRegionList *region;
547
548 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
549 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
550 dev->slot, dev->function);
551 monitor_printf(mon, " ");
552
553 if (dev->class_info.has_desc) {
554 monitor_printf(mon, "%s", dev->class_info.desc);
555 } else {
6f88009e 556 monitor_printf(mon, "Class %04" PRId64, dev->class_info.q_class);
79627472
LC
557 }
558
559 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
560 dev->id.vendor, dev->id.device);
561
562 if (dev->has_irq) {
563 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
564 }
565
566 if (dev->has_pci_bridge) {
567 monitor_printf(mon, " BUS %" PRId64 ".\n",
568 dev->pci_bridge->bus.number);
569 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
570 dev->pci_bridge->bus.secondary);
571 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
572 dev->pci_bridge->bus.subordinate);
573
574 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
575 dev->pci_bridge->bus.io_range->base,
576 dev->pci_bridge->bus.io_range->limit);
577
578 monitor_printf(mon,
579 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
580 dev->pci_bridge->bus.memory_range->base,
581 dev->pci_bridge->bus.memory_range->limit);
582
583 monitor_printf(mon, " prefetchable memory range "
584 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
585 dev->pci_bridge->bus.prefetchable_range->base,
586 dev->pci_bridge->bus.prefetchable_range->limit);
587 }
588
589 for (region = dev->regions; region; region = region->next) {
590 uint64_t addr, size;
591
592 addr = region->value->address;
593 size = region->value->size;
594
595 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
596
597 if (!strcmp(region->value->type, "io")) {
598 monitor_printf(mon, "I/O at 0x%04" PRIx64
599 " [0x%04" PRIx64 "].\n",
600 addr, addr + size - 1);
601 } else {
602 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
603 " [0x%08" PRIx64 "].\n",
604 region->value->mem_type_64 ? 64 : 32,
605 region->value->prefetch ? " prefetchable" : "",
606 addr, addr + size - 1);
607 }
608 }
609
610 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
611
612 if (dev->has_pci_bridge) {
613 if (dev->pci_bridge->has_devices) {
614 PciDeviceInfoList *cdev;
615 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
616 hmp_info_pci_device(mon, cdev->value);
617 }
618 }
619 }
620}
621
84f2d0ea 622void hmp_info_pci(Monitor *mon, const QDict *qdict)
79627472 623{
f46cee37 624 PciInfoList *info_list, *info;
79627472
LC
625 Error *err = NULL;
626
f46cee37 627 info_list = qmp_query_pci(&err);
79627472
LC
628 if (err) {
629 monitor_printf(mon, "PCI devices not supported\n");
630 error_free(err);
631 return;
632 }
633
f46cee37 634 for (info = info_list; info; info = info->next) {
79627472
LC
635 PciDeviceInfoList *dev;
636
637 for (dev = info->value->devices; dev; dev = dev->next) {
638 hmp_info_pci_device(mon, dev->value);
639 }
640 }
641
f46cee37 642 qapi_free_PciInfoList(info_list);
79627472
LC
643}
644
84f2d0ea 645void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
fb5458cd
SH
646{
647 BlockJobInfoList *list;
648 Error *err = NULL;
649
650 list = qmp_query_block_jobs(&err);
651 assert(!err);
652
653 if (!list) {
654 monitor_printf(mon, "No active jobs\n");
655 return;
656 }
657
658 while (list) {
659 if (strcmp(list->value->type, "stream") == 0) {
660 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
661 " of %" PRId64 " bytes, speed limit %" PRId64
662 " bytes/s\n",
663 list->value->device,
664 list->value->offset,
665 list->value->len,
666 list->value->speed);
667 } else {
668 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
669 " of %" PRId64 " bytes, speed limit %" PRId64
670 " bytes/s\n",
671 list->value->type,
672 list->value->device,
673 list->value->offset,
674 list->value->len,
675 list->value->speed);
676 }
677 list = list->next;
678 }
679}
680
d1a0cf73
SB
681void hmp_info_tpm(Monitor *mon, const QDict *qdict)
682{
683 TPMInfoList *info_list, *info;
684 Error *err = NULL;
685 unsigned int c = 0;
686 TPMPassthroughOptions *tpo;
687
688 info_list = qmp_query_tpm(&err);
689 if (err) {
690 monitor_printf(mon, "TPM device not supported\n");
691 error_free(err);
692 return;
693 }
694
695 if (info_list) {
696 monitor_printf(mon, "TPM device:\n");
697 }
698
699 for (info = info_list; info; info = info->next) {
700 TPMInfo *ti = info->value;
701 monitor_printf(mon, " tpm%d: model=%s\n",
702 c, TpmModel_lookup[ti->model]);
703
704 monitor_printf(mon, " \\ %s: type=%s",
88ca7bcf 705 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
d1a0cf73 706
88ca7bcf
CB
707 switch (ti->options->kind) {
708 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
709 tpo = ti->options->passthrough;
d1a0cf73
SB
710 monitor_printf(mon, "%s%s%s%s",
711 tpo->has_path ? ",path=" : "",
712 tpo->has_path ? tpo->path : "",
713 tpo->has_cancel_path ? ",cancel-path=" : "",
714 tpo->has_cancel_path ? tpo->cancel_path : "");
715 break;
716 case TPM_TYPE_OPTIONS_KIND_MAX:
717 break;
718 }
719 monitor_printf(mon, "\n");
720 c++;
721 }
722 qapi_free_TPMInfoList(info_list);
723}
724
7a7f325e
LC
725void hmp_quit(Monitor *mon, const QDict *qdict)
726{
727 monitor_suspend(mon);
728 qmp_quit(NULL);
729}
5f158f21
LC
730
731void hmp_stop(Monitor *mon, const QDict *qdict)
732{
733 qmp_stop(NULL);
734}
38d22653
LC
735
736void hmp_system_reset(Monitor *mon, const QDict *qdict)
737{
738 qmp_system_reset(NULL);
739}
5bc465e4
LC
740
741void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
742{
743 qmp_system_powerdown(NULL);
744}
755f1968
LC
745
746void hmp_cpu(Monitor *mon, const QDict *qdict)
747{
748 int64_t cpu_index;
749
750 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
751 use it are converted to the QAPI */
752 cpu_index = qdict_get_int(qdict, "index");
753 if (monitor_set_cpu(cpu_index) < 0) {
754 monitor_printf(mon, "invalid CPU index\n");
755 }
756}
0cfd6a9a
LC
757
758void hmp_memsave(Monitor *mon, const QDict *qdict)
759{
760 uint32_t size = qdict_get_int(qdict, "size");
761 const char *filename = qdict_get_str(qdict, "filename");
762 uint64_t addr = qdict_get_int(qdict, "val");
e940f543 763 Error *err = NULL;
0cfd6a9a 764
e940f543
MA
765 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
766 hmp_handle_error(mon, &err);
0cfd6a9a 767}
6d3962bf
LC
768
769void hmp_pmemsave(Monitor *mon, const QDict *qdict)
770{
771 uint32_t size = qdict_get_int(qdict, "size");
772 const char *filename = qdict_get_str(qdict, "filename");
773 uint64_t addr = qdict_get_int(qdict, "val");
e940f543 774 Error *err = NULL;
6d3962bf 775
e940f543
MA
776 qmp_pmemsave(addr, size, filename, &err);
777 hmp_handle_error(mon, &err);
1f590cf9
LL
778}
779
3949e594 780void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1f590cf9 781{
1f590cf9
LL
782 const char *chardev = qdict_get_str(qdict, "device");
783 const char *data = qdict_get_str(qdict, "data");
e940f543 784 Error *err = NULL;
1f590cf9 785
e940f543 786 qmp_ringbuf_write(chardev, data, false, 0, &err);
1f590cf9 787
e940f543 788 hmp_handle_error(mon, &err);
6d3962bf 789}
e42e818b 790
3949e594 791void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
49b6d722
LL
792{
793 uint32_t size = qdict_get_int(qdict, "size");
794 const char *chardev = qdict_get_str(qdict, "device");
3ab651fc 795 char *data;
e940f543 796 Error *err = NULL;
543f3412 797 int i;
49b6d722 798
e940f543
MA
799 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
800 if (err) {
801 monitor_printf(mon, "%s\n", error_get_pretty(err));
802 error_free(err);
49b6d722
LL
803 return;
804 }
805
543f3412
MA
806 for (i = 0; data[i]; i++) {
807 unsigned char ch = data[i];
808
809 if (ch == '\\') {
810 monitor_printf(mon, "\\\\");
811 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
812 monitor_printf(mon, "\\u%04X", ch);
813 } else {
814 monitor_printf(mon, "%c", ch);
815 }
816
817 }
818 monitor_printf(mon, "\n");
3ab651fc 819 g_free(data);
49b6d722
LL
820}
821
e42e818b
LC
822static void hmp_cont_cb(void *opaque, int err)
823{
e42e818b 824 if (!err) {
8b7f6fbb 825 qmp_cont(NULL);
e42e818b
LC
826 }
827}
828
8b7f6fbb
LC
829static bool key_is_missing(const BlockInfo *bdev)
830{
831 return (bdev->inserted && bdev->inserted->encryption_key_missing);
832}
833
e42e818b
LC
834void hmp_cont(Monitor *mon, const QDict *qdict)
835{
8b7f6fbb 836 BlockInfoList *bdev_list, *bdev;
e940f543 837 Error *err = NULL;
e42e818b 838
8b7f6fbb
LC
839 bdev_list = qmp_query_block(NULL);
840 for (bdev = bdev_list; bdev; bdev = bdev->next) {
841 if (key_is_missing(bdev->value)) {
842 monitor_read_block_device_key(mon, bdev->value->device,
843 hmp_cont_cb, NULL);
844 goto out;
e42e818b 845 }
e42e818b 846 }
8b7f6fbb 847
e940f543
MA
848 qmp_cont(&err);
849 hmp_handle_error(mon, &err);
8b7f6fbb
LC
850
851out:
852 qapi_free_BlockInfoList(bdev_list);
e42e818b 853}
ab49ab5c 854
9b9df25a
GH
855void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
856{
857 qmp_system_wakeup(NULL);
858}
859
ab49ab5c
LC
860void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
861{
e940f543 862 Error *err = NULL;
ab49ab5c 863
e940f543
MA
864 qmp_inject_nmi(&err);
865 hmp_handle_error(mon, &err);
ab49ab5c 866}
4b37156c
LC
867
868void hmp_set_link(Monitor *mon, const QDict *qdict)
869{
870 const char *name = qdict_get_str(qdict, "name");
871 int up = qdict_get_bool(qdict, "up");
e940f543 872 Error *err = NULL;
4b37156c 873
e940f543
MA
874 qmp_set_link(name, up, &err);
875 hmp_handle_error(mon, &err);
4b37156c 876}
a4dea8a9
LC
877
878void hmp_block_passwd(Monitor *mon, const QDict *qdict)
879{
880 const char *device = qdict_get_str(qdict, "device");
881 const char *password = qdict_get_str(qdict, "password");
e940f543 882 Error *err = NULL;
a4dea8a9 883
e940f543
MA
884 qmp_block_passwd(true, device, false, NULL, password, &err);
885 hmp_handle_error(mon, &err);
a4dea8a9 886}
d72f3264
LC
887
888void hmp_balloon(Monitor *mon, const QDict *qdict)
889{
890 int64_t value = qdict_get_int(qdict, "value");
e940f543 891 Error *err = NULL;
d72f3264 892
e940f543
MA
893 qmp_balloon(value, &err);
894 if (err) {
895 monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
896 error_free(err);
d72f3264
LC
897 }
898}
5e7caacb
LC
899
900void hmp_block_resize(Monitor *mon, const QDict *qdict)
901{
902 const char *device = qdict_get_str(qdict, "device");
903 int64_t size = qdict_get_int(qdict, "size");
e940f543 904 Error *err = NULL;
5e7caacb 905
e940f543
MA
906 qmp_block_resize(true, device, false, NULL, size, &err);
907 hmp_handle_error(mon, &err);
5e7caacb 908}
6106e249 909
d9b902db
PB
910void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
911{
912 const char *device = qdict_get_str(qdict, "device");
913 const char *filename = qdict_get_str(qdict, "target");
914 const char *format = qdict_get_try_str(qdict, "format");
915 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
916 int full = qdict_get_try_bool(qdict, "full", 0);
917 enum NewImageMode mode;
e940f543 918 Error *err = NULL;
d9b902db
PB
919
920 if (!filename) {
e940f543
MA
921 error_set(&err, QERR_MISSING_PARAMETER, "target");
922 hmp_handle_error(mon, &err);
d9b902db
PB
923 return;
924 }
925
926 if (reuse) {
927 mode = NEW_IMAGE_MODE_EXISTING;
928 } else {
929 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
930 }
931
932 qmp_drive_mirror(device, filename, !!format, format,
933 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
08e4ed6c 934 true, mode, false, 0, false, 0, false, 0,
e940f543
MA
935 false, 0, false, 0, &err);
936 hmp_handle_error(mon, &err);
d9b902db
PB
937}
938
de90930a
SH
939void hmp_drive_backup(Monitor *mon, const QDict *qdict)
940{
941 const char *device = qdict_get_str(qdict, "device");
942 const char *filename = qdict_get_str(qdict, "target");
943 const char *format = qdict_get_try_str(qdict, "format");
944 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
945 int full = qdict_get_try_bool(qdict, "full", 0);
946 enum NewImageMode mode;
e940f543 947 Error *err = NULL;
de90930a
SH
948
949 if (!filename) {
e940f543
MA
950 error_set(&err, QERR_MISSING_PARAMETER, "target");
951 hmp_handle_error(mon, &err);
de90930a
SH
952 return;
953 }
954
955 if (reuse) {
956 mode = NEW_IMAGE_MODE_EXISTING;
957 } else {
958 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
959 }
960
961 qmp_drive_backup(device, filename, !!format, format,
962 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
e940f543
MA
963 true, mode, false, 0, false, 0, false, 0, &err);
964 hmp_handle_error(mon, &err);
de90930a
SH
965}
966
6106e249
LC
967void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
968{
969 const char *device = qdict_get_str(qdict, "device");
970 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
971 const char *format = qdict_get_try_str(qdict, "format");
6cc2a415
PB
972 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
973 enum NewImageMode mode;
e940f543 974 Error *err = NULL;
6106e249
LC
975
976 if (!filename) {
977 /* In the future, if 'snapshot-file' is not specified, the snapshot
978 will be taken internally. Today it's actually required. */
e940f543
MA
979 error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
980 hmp_handle_error(mon, &err);
6106e249
LC
981 return;
982 }
983
6cc2a415 984 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
0901f67e
BC
985 qmp_blockdev_snapshot_sync(true, device, false, NULL,
986 filename, false, NULL,
987 !!format, format,
e940f543
MA
988 true, mode, &err);
989 hmp_handle_error(mon, &err);
6106e249 990}
6cdedb07 991
775ca88e
WX
992void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
993{
994 const char *device = qdict_get_str(qdict, "device");
995 const char *name = qdict_get_str(qdict, "name");
e940f543 996 Error *err = NULL;
775ca88e 997
e940f543
MA
998 qmp_blockdev_snapshot_internal_sync(device, name, &err);
999 hmp_handle_error(mon, &err);
775ca88e
WX
1000}
1001
7a4ed2ee
WX
1002void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1003{
1004 const char *device = qdict_get_str(qdict, "device");
1005 const char *name = qdict_get_str(qdict, "name");
1006 const char *id = qdict_get_try_str(qdict, "id");
e940f543 1007 Error *err = NULL;
7a4ed2ee
WX
1008
1009 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
e940f543
MA
1010 true, name, &err);
1011 hmp_handle_error(mon, &err);
7a4ed2ee
WX
1012}
1013
6cdedb07
LC
1014void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1015{
1016 qmp_migrate_cancel(NULL);
1017}
4f0a993b
LC
1018
1019void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1020{
1021 double value = qdict_get_double(qdict, "value");
1022 qmp_migrate_set_downtime(value, NULL);
1023}
3dc85383 1024
9e1ba4cc
OW
1025void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1026{
1027 int64_t value = qdict_get_int(qdict, "value");
1028 Error *err = NULL;
1029
1030 qmp_migrate_set_cache_size(value, &err);
1031 if (err) {
1032 monitor_printf(mon, "%s\n", error_get_pretty(err));
1033 error_free(err);
1034 return;
1035 }
1036}
1037
3dc85383
LC
1038void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1039{
1040 int64_t value = qdict_get_int(qdict, "value");
1041 qmp_migrate_set_speed(value, NULL);
1042}
fbf796fd 1043
00458433
OW
1044void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1045{
1046 const char *cap = qdict_get_str(qdict, "capability");
1047 bool state = qdict_get_bool(qdict, "state");
1048 Error *err = NULL;
1049 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1050 int i;
1051
1052 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1053 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1054 caps->value = g_malloc0(sizeof(*caps->value));
1055 caps->value->capability = i;
1056 caps->value->state = state;
1057 caps->next = NULL;
1058 qmp_migrate_set_capabilities(caps, &err);
1059 break;
1060 }
1061 }
1062
1063 if (i == MIGRATION_CAPABILITY_MAX) {
1064 error_set(&err, QERR_INVALID_PARAMETER, cap);
1065 }
1066
1067 qapi_free_MigrationCapabilityStatusList(caps);
1068
1069 if (err) {
a31ca017 1070 monitor_printf(mon, "migrate_set_capability: %s\n",
00458433
OW
1071 error_get_pretty(err));
1072 error_free(err);
1073 }
1074}
1075
fbf796fd
LC
1076void hmp_set_password(Monitor *mon, const QDict *qdict)
1077{
1078 const char *protocol = qdict_get_str(qdict, "protocol");
1079 const char *password = qdict_get_str(qdict, "password");
1080 const char *connected = qdict_get_try_str(qdict, "connected");
1081 Error *err = NULL;
1082
1083 qmp_set_password(protocol, password, !!connected, connected, &err);
1084 hmp_handle_error(mon, &err);
1085}
9ad5372d
LC
1086
1087void hmp_expire_password(Monitor *mon, const QDict *qdict)
1088{
1089 const char *protocol = qdict_get_str(qdict, "protocol");
1090 const char *whenstr = qdict_get_str(qdict, "time");
1091 Error *err = NULL;
1092
1093 qmp_expire_password(protocol, whenstr, &err);
1094 hmp_handle_error(mon, &err);
1095}
c245b6a3
LC
1096
1097void hmp_eject(Monitor *mon, const QDict *qdict)
1098{
1099 int force = qdict_get_try_bool(qdict, "force", 0);
1100 const char *device = qdict_get_str(qdict, "device");
1101 Error *err = NULL;
1102
1103 qmp_eject(device, true, force, &err);
1104 hmp_handle_error(mon, &err);
1105}
333a96ec 1106
c60bf339
SH
1107static void hmp_change_read_arg(void *opaque, const char *password,
1108 void *readline_opaque)
333a96ec
LC
1109{
1110 qmp_change_vnc_password(password, NULL);
c60bf339 1111 monitor_read_command(opaque, 1);
333a96ec
LC
1112}
1113
333a96ec
LC
1114void hmp_change(Monitor *mon, const QDict *qdict)
1115{
1116 const char *device = qdict_get_str(qdict, "device");
1117 const char *target = qdict_get_str(qdict, "target");
1118 const char *arg = qdict_get_try_str(qdict, "arg");
1119 Error *err = NULL;
1120
1121 if (strcmp(device, "vnc") == 0 &&
1122 (strcmp(target, "passwd") == 0 ||
1123 strcmp(target, "password") == 0)) {
1124 if (!arg) {
1125 monitor_read_password(mon, hmp_change_read_arg, NULL);
1126 return;
1127 }
1128 }
1129
1130 qmp_change(device, target, !!arg, arg, &err);
84d18f06 1131 if (err &&
ab878ddf 1132 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
eef5ad10
LC
1133 error_free(err);
1134 monitor_read_block_device_key(mon, device, NULL, NULL);
333a96ec
LC
1135 return;
1136 }
1137 hmp_handle_error(mon, &err);
1138}
80047da5
LC
1139
1140void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1141{
1142 Error *err = NULL;
1143
1144 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1145 qdict_get_int(qdict, "bps"),
1146 qdict_get_int(qdict, "bps_rd"),
1147 qdict_get_int(qdict, "bps_wr"),
1148 qdict_get_int(qdict, "iops"),
1149 qdict_get_int(qdict, "iops_rd"),
3e9fab69
BC
1150 qdict_get_int(qdict, "iops_wr"),
1151 false, /* no burst max via HMP */
1152 0,
1153 false,
1154 0,
1155 false,
1156 0,
1157 false,
1158 0,
1159 false,
1160 0,
1161 false,
2024c1df
BC
1162 0,
1163 false, /* No default I/O size */
3e9fab69 1164 0, &err);
80047da5
LC
1165 hmp_handle_error(mon, &err);
1166}
12bd451f
SH
1167
1168void hmp_block_stream(Monitor *mon, const QDict *qdict)
1169{
1170 Error *error = NULL;
1171 const char *device = qdict_get_str(qdict, "device");
1172 const char *base = qdict_get_try_str(qdict, "base");
c83c66c3 1173 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
12bd451f 1174
c83c66c3 1175 qmp_block_stream(device, base != NULL, base,
1d809098 1176 qdict_haskey(qdict, "speed"), speed,
46663e5e 1177 true, BLOCKDEV_ON_ERROR_REPORT, &error);
12bd451f
SH
1178
1179 hmp_handle_error(mon, &error);
1180}
2d47c6e9
SH
1181
1182void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1183{
1184 Error *error = NULL;
1185 const char *device = qdict_get_str(qdict, "device");
c6db2395 1186 int64_t value = qdict_get_int(qdict, "speed");
2d47c6e9
SH
1187
1188 qmp_block_job_set_speed(device, value, &error);
1189
1190 hmp_handle_error(mon, &error);
1191}
370521a1
SH
1192
1193void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1194{
1195 Error *error = NULL;
1196 const char *device = qdict_get_str(qdict, "device");
6e37fb81 1197 bool force = qdict_get_try_bool(qdict, "force", 0);
370521a1 1198
6e37fb81
PB
1199 qmp_block_job_cancel(device, true, force, &error);
1200
1201 hmp_handle_error(mon, &error);
1202}
1203
1204void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1205{
1206 Error *error = NULL;
1207 const char *device = qdict_get_str(qdict, "device");
1208
1209 qmp_block_job_pause(device, &error);
1210
1211 hmp_handle_error(mon, &error);
1212}
1213
1214void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1215{
1216 Error *error = NULL;
1217 const char *device = qdict_get_str(qdict, "device");
1218
1219 qmp_block_job_resume(device, &error);
370521a1
SH
1220
1221 hmp_handle_error(mon, &error);
1222}
e1c37d0e 1223
aeae883b
PB
1224void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1225{
1226 Error *error = NULL;
1227 const char *device = qdict_get_str(qdict, "device");
1228
1229 qmp_block_job_complete(device, &error);
1230
1231 hmp_handle_error(mon, &error);
1232}
1233
e1c37d0e
LC
1234typedef struct MigrationStatus
1235{
1236 QEMUTimer *timer;
1237 Monitor *mon;
1238 bool is_block_migration;
1239} MigrationStatus;
1240
1241static void hmp_migrate_status_cb(void *opaque)
1242{
1243 MigrationStatus *status = opaque;
1244 MigrationInfo *info;
1245
1246 info = qmp_query_migrate(NULL);
dde3a218
SA
1247 if (!info->has_status || strcmp(info->status, "active") == 0 ||
1248 strcmp(info->status, "setup") == 0) {
e1c37d0e
LC
1249 if (info->has_disk) {
1250 int progress;
1251
1252 if (info->disk->remaining) {
1253 progress = info->disk->transferred * 100 / info->disk->total;
1254 } else {
1255 progress = 100;
1256 }
1257
1258 monitor_printf(status->mon, "Completed %d %%\r", progress);
1259 monitor_flush(status->mon);
1260 }
1261
bc72ad67 1262 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
e1c37d0e
LC
1263 } else {
1264 if (status->is_block_migration) {
1265 monitor_printf(status->mon, "\n");
1266 }
1267 monitor_resume(status->mon);
bc72ad67 1268 timer_del(status->timer);
e1c37d0e
LC
1269 g_free(status);
1270 }
1271
1272 qapi_free_MigrationInfo(info);
1273}
1274
1275void hmp_migrate(Monitor *mon, const QDict *qdict)
1276{
1277 int detach = qdict_get_try_bool(qdict, "detach", 0);
1278 int blk = qdict_get_try_bool(qdict, "blk", 0);
1279 int inc = qdict_get_try_bool(qdict, "inc", 0);
1280 const char *uri = qdict_get_str(qdict, "uri");
1281 Error *err = NULL;
1282
1283 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1284 if (err) {
1285 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1286 error_free(err);
1287 return;
1288 }
1289
1290 if (!detach) {
1291 MigrationStatus *status;
1292
1293 if (monitor_suspend(mon) < 0) {
1294 monitor_printf(mon, "terminal does not allow synchronous "
1295 "migration, continuing detached\n");
1296 return;
1297 }
1298
1299 status = g_malloc0(sizeof(*status));
1300 status->mon = mon;
1301 status->is_block_migration = blk || inc;
bc72ad67 1302 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
e1c37d0e 1303 status);
bc72ad67 1304 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
e1c37d0e
LC
1305 }
1306}
a15fef21
LC
1307
1308void hmp_device_del(Monitor *mon, const QDict *qdict)
1309{
1310 const char *id = qdict_get_str(qdict, "id");
1311 Error *err = NULL;
1312
1313 qmp_device_del(id, &err);
1314 hmp_handle_error(mon, &err);
1315}
783e9b48
WC
1316
1317void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1318{
e940f543 1319 Error *err = NULL;
783e9b48 1320 int paging = qdict_get_try_bool(qdict, "paging", 0);
1b7a0f75
QN
1321 int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1322 int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1323 int snappy = qdict_get_try_bool(qdict, "snappy", 0);
75363769 1324 const char *file = qdict_get_str(qdict, "filename");
783e9b48
WC
1325 bool has_begin = qdict_haskey(qdict, "begin");
1326 bool has_length = qdict_haskey(qdict, "length");
1327 int64_t begin = 0;
1328 int64_t length = 0;
b53ccc30 1329 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
75363769 1330 char *prot;
783e9b48 1331
1b7a0f75 1332 if (zlib + lzo + snappy > 1) {
e940f543
MA
1333 error_setg(&err, "only one of '-z|-l|-s' can be set");
1334 hmp_handle_error(mon, &err);
1b7a0f75
QN
1335 return;
1336 }
1337
1338 if (zlib) {
1339 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1340 }
1341
1342 if (lzo) {
1343 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1344 }
1345
1346 if (snappy) {
1347 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1348 }
1349
783e9b48
WC
1350 if (has_begin) {
1351 begin = qdict_get_int(qdict, "begin");
1352 }
1353 if (has_length) {
1354 length = qdict_get_int(qdict, "length");
1355 }
1356
75363769
LC
1357 prot = g_strconcat("file:", file, NULL);
1358
1359 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
e940f543
MA
1360 true, dump_format, &err);
1361 hmp_handle_error(mon, &err);
75363769 1362 g_free(prot);
783e9b48 1363}
928059a3
LC
1364
1365void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1366{
1367 Error *err = NULL;
1368 QemuOpts *opts;
1369
1370 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
84d18f06 1371 if (err) {
928059a3
LC
1372 goto out;
1373 }
1374
1375 netdev_add(opts, &err);
84d18f06 1376 if (err) {
928059a3
LC
1377 qemu_opts_del(opts);
1378 }
1379
1380out:
1381 hmp_handle_error(mon, &err);
1382}
5f964155
LC
1383
1384void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1385{
1386 const char *id = qdict_get_str(qdict, "id");
1387 Error *err = NULL;
1388
1389 qmp_netdev_del(id, &err);
1390 hmp_handle_error(mon, &err);
1391}
208c9d1b 1392
cff8b2c6
PB
1393void hmp_object_add(Monitor *mon, const QDict *qdict)
1394{
1395 Error *err = NULL;
f9f3a5ec 1396 Error *err_end = NULL;
cff8b2c6
PB
1397 QemuOpts *opts;
1398 char *type = NULL;
1399 char *id = NULL;
1400 void *dummy = NULL;
1401 OptsVisitor *ov;
1402 QDict *pdict;
1403
1404 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1405 if (err) {
1406 goto out;
1407 }
1408
1409 ov = opts_visitor_new(opts);
1410 pdict = qdict_clone_shallow(qdict);
1411
1412 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1413 if (err) {
1414 goto out_clean;
1415 }
1416
1417 qdict_del(pdict, "qom-type");
1418 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1419 if (err) {
f9f3a5ec 1420 goto out_end;
cff8b2c6
PB
1421 }
1422
1423 qdict_del(pdict, "id");
1424 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1425 if (err) {
f9f3a5ec 1426 goto out_end;
cff8b2c6
PB
1427 }
1428
1429 object_add(type, id, pdict, opts_get_visitor(ov), &err);
f9f3a5ec
MA
1430
1431out_end:
1432 visit_end_struct(opts_get_visitor(ov), &err_end);
1433 if (!err && err_end) {
cff8b2c6
PB
1434 qmp_object_del(id, NULL);
1435 }
f9f3a5ec 1436 error_propagate(&err, err_end);
cff8b2c6
PB
1437out_clean:
1438 opts_visitor_cleanup(ov);
1439
1440 QDECREF(pdict);
1441 qemu_opts_del(opts);
1442 g_free(id);
1443 g_free(type);
1444 g_free(dummy);
1445
1446out:
1447 hmp_handle_error(mon, &err);
1448}
1449
208c9d1b
CB
1450void hmp_getfd(Monitor *mon, const QDict *qdict)
1451{
1452 const char *fdname = qdict_get_str(qdict, "fdname");
e940f543 1453 Error *err = NULL;
208c9d1b 1454
e940f543
MA
1455 qmp_getfd(fdname, &err);
1456 hmp_handle_error(mon, &err);
208c9d1b
CB
1457}
1458
1459void hmp_closefd(Monitor *mon, const QDict *qdict)
1460{
1461 const char *fdname = qdict_get_str(qdict, "fdname");
e940f543 1462 Error *err = NULL;
208c9d1b 1463
e940f543
MA
1464 qmp_closefd(fdname, &err);
1465 hmp_handle_error(mon, &err);
208c9d1b 1466}
e4c8f004
AK
1467
1468void hmp_send_key(Monitor *mon, const QDict *qdict)
1469{
1470 const char *keys = qdict_get_str(qdict, "keys");
9f328977 1471 KeyValueList *keylist, *head = NULL, *tmp = NULL;
e4c8f004
AK
1472 int has_hold_time = qdict_haskey(qdict, "hold-time");
1473 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1474 Error *err = NULL;
1475 char keyname_buf[16];
1476 char *separator;
9f328977 1477 int keyname_len;
e4c8f004
AK
1478
1479 while (1) {
1480 separator = strchr(keys, '-');
1481 keyname_len = separator ? separator - keys : strlen(keys);
1482 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1483
1484 /* Be compatible with old interface, convert user inputted "<" */
1485 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1486 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1487 keyname_len = 4;
1488 }
1489 keyname_buf[keyname_len] = 0;
1490
e4c8f004 1491 keylist = g_malloc0(sizeof(*keylist));
9f328977 1492 keylist->value = g_malloc0(sizeof(*keylist->value));
e4c8f004
AK
1493
1494 if (!head) {
1495 head = keylist;
1496 }
1497 if (tmp) {
1498 tmp->next = keylist;
1499 }
1500 tmp = keylist;
1501
9f328977
LC
1502 if (strstart(keyname_buf, "0x", NULL)) {
1503 char *endp;
1504 int value = strtoul(keyname_buf, &endp, 0);
1505 if (*endp != '\0') {
1506 goto err_out;
1507 }
1508 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1509 keylist->value->number = value;
1510 } else {
1511 int idx = index_from_key(keyname_buf);
1512 if (idx == Q_KEY_CODE_MAX) {
1513 goto err_out;
1514 }
1515 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1516 keylist->value->qcode = idx;
1517 }
1518
e4c8f004
AK
1519 if (!separator) {
1520 break;
1521 }
1522 keys = separator + 1;
1523 }
1524
9f328977 1525 qmp_send_key(head, has_hold_time, hold_time, &err);
e4c8f004 1526 hmp_handle_error(mon, &err);
9f328977
LC
1527
1528out:
1529 qapi_free_KeyValueList(head);
1530 return;
1531
1532err_out:
1533 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1534 goto out;
e4c8f004 1535}
ad39cf6d
LC
1536
1537void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1538{
1539 const char *filename = qdict_get_str(qdict, "filename");
1540 Error *err = NULL;
1541
1542 qmp_screendump(filename, &err);
1543 hmp_handle_error(mon, &err);
1544}
4057725f
PB
1545
1546void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1547{
1548 const char *uri = qdict_get_str(qdict, "uri");
1549 int writable = qdict_get_try_bool(qdict, "writable", 0);
1550 int all = qdict_get_try_bool(qdict, "all", 0);
1551 Error *local_err = NULL;
1552 BlockInfoList *block_list, *info;
1553 SocketAddress *addr;
1554
1555 if (writable && !all) {
1556 error_setg(&local_err, "-w only valid together with -a");
1557 goto exit;
1558 }
1559
1560 /* First check if the address is valid and start the server. */
1561 addr = socket_parse(uri, &local_err);
1562 if (local_err != NULL) {
1563 goto exit;
1564 }
1565
1566 qmp_nbd_server_start(addr, &local_err);
1567 qapi_free_SocketAddress(addr);
1568 if (local_err != NULL) {
1569 goto exit;
1570 }
1571
1572 if (!all) {
1573 return;
1574 }
1575
1576 /* Then try adding all block devices. If one fails, close all and
1577 * exit.
1578 */
1579 block_list = qmp_query_block(NULL);
1580
1581 for (info = block_list; info; info = info->next) {
1582 if (!info->value->has_inserted) {
1583 continue;
1584 }
1585
1586 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1587
1588 if (local_err != NULL) {
1589 qmp_nbd_server_stop(NULL);
1590 break;
1591 }
1592 }
1593
1594 qapi_free_BlockInfoList(block_list);
1595
1596exit:
1597 hmp_handle_error(mon, &local_err);
1598}
1599
1600void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1601{
1602 const char *device = qdict_get_str(qdict, "device");
1603 int writable = qdict_get_try_bool(qdict, "writable", 0);
1604 Error *local_err = NULL;
1605
1606 qmp_nbd_server_add(device, true, writable, &local_err);
1607
1608 if (local_err != NULL) {
1609 hmp_handle_error(mon, &local_err);
1610 }
1611}
1612
1613void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1614{
e940f543 1615 Error *err = NULL;
4057725f 1616
e940f543
MA
1617 qmp_nbd_server_stop(&err);
1618 hmp_handle_error(mon, &err);
4057725f 1619}
f1088908 1620
abf23329
JH
1621void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1622{
1623 int cpuid;
1624 Error *err = NULL;
1625
1626 cpuid = qdict_get_int(qdict, "id");
1627 qmp_cpu_add(cpuid, &err);
1628 hmp_handle_error(mon, &err);
1629}
1630
f1088908
GH
1631void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1632{
1633 const char *args = qdict_get_str(qdict, "args");
1634 Error *err = NULL;
1635 QemuOpts *opts;
1636
1637 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1638 if (opts == NULL) {
312fd5f2 1639 error_setg(&err, "Parsing chardev args failed");
f1088908
GH
1640 } else {
1641 qemu_chr_new_from_opts(opts, NULL, &err);
1642 }
1643 hmp_handle_error(mon, &err);
1644}
1645
1646void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1647{
1648 Error *local_err = NULL;
1649
1650 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1651 hmp_handle_error(mon, &local_err);
1652}
587da2c3
KW
1653
1654void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1655{
1656 BlockDriverState *bs;
1657 const char* device = qdict_get_str(qdict, "device");
1658 const char* command = qdict_get_str(qdict, "command");
1659 Error *err = NULL;
1660
1661 bs = bdrv_find(device);
1662 if (bs) {
1663 qemuio_command(bs, command);
1664 } else {
1665 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1666 }
1667
1668 hmp_handle_error(mon, &err);
1669}
ab2d0531
PB
1670
1671void hmp_object_del(Monitor *mon, const QDict *qdict)
1672{
1673 const char *id = qdict_get_str(qdict, "id");
1674 Error *err = NULL;
1675
1676 qmp_object_del(id, &err);
1677 hmp_handle_error(mon, &err);
1678}