]> git.proxmox.com Git - mirror_qemu.git/blob - hmp.c
monitor: Move hmp_delvm from savevm.c to hmp.c
[mirror_qemu.git] / hmp.c
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 *
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.
14 */
15
16 #include "qemu/osdep.h"
17 #include "hmp.h"
18 #include "net/net.h"
19 #include "net/eth.h"
20 #include "sysemu/char.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/option.h"
25 #include "qemu/timer.h"
26 #include "qmp-commands.h"
27 #include "qemu/sockets.h"
28 #include "monitor/monitor.h"
29 #include "monitor/qdev.h"
30 #include "qapi/opts-visitor.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/string-output-visitor.h"
33 #include "qapi/util.h"
34 #include "qapi-visit.h"
35 #include "qom/object_interfaces.h"
36 #include "ui/console.h"
37 #include "block/qapi.h"
38 #include "qemu-io.h"
39 #include "qemu/cutils.h"
40 #include "qemu/error-report.h"
41 #include "hw/intc/intc.h"
42
43 #ifdef CONFIG_SPICE
44 #include <spice/enums.h>
45 #endif
46
47 static void hmp_handle_error(Monitor *mon, Error **errp)
48 {
49 assert(errp);
50 if (*errp) {
51 error_report_err(*errp);
52 }
53 }
54
55 void hmp_info_name(Monitor *mon, const QDict *qdict)
56 {
57 NameInfo *info;
58
59 info = qmp_query_name(NULL);
60 if (info->has_name) {
61 monitor_printf(mon, "%s\n", info->name);
62 }
63 qapi_free_NameInfo(info);
64 }
65
66 void hmp_info_version(Monitor *mon, const QDict *qdict)
67 {
68 VersionInfo *info;
69
70 info = qmp_query_version(NULL);
71
72 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
73 info->qemu->major, info->qemu->minor, info->qemu->micro,
74 info->package);
75
76 qapi_free_VersionInfo(info);
77 }
78
79 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
80 {
81 KvmInfo *info;
82
83 info = qmp_query_kvm(NULL);
84 monitor_printf(mon, "kvm support: ");
85 if (info->present) {
86 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
87 } else {
88 monitor_printf(mon, "not compiled\n");
89 }
90
91 qapi_free_KvmInfo(info);
92 }
93
94 void hmp_info_status(Monitor *mon, const QDict *qdict)
95 {
96 StatusInfo *info;
97
98 info = qmp_query_status(NULL);
99
100 monitor_printf(mon, "VM status: %s%s",
101 info->running ? "running" : "paused",
102 info->singlestep ? " (single step mode)" : "");
103
104 if (!info->running && info->status != RUN_STATE_PAUSED) {
105 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
106 }
107
108 monitor_printf(mon, "\n");
109
110 qapi_free_StatusInfo(info);
111 }
112
113 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
114 {
115 UuidInfo *info;
116
117 info = qmp_query_uuid(NULL);
118 monitor_printf(mon, "%s\n", info->UUID);
119 qapi_free_UuidInfo(info);
120 }
121
122 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
123 {
124 ChardevInfoList *char_info, *info;
125
126 char_info = qmp_query_chardev(NULL);
127 for (info = char_info; info; info = info->next) {
128 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
129 info->value->filename);
130 }
131
132 qapi_free_ChardevInfoList(char_info);
133 }
134
135 void hmp_info_mice(Monitor *mon, const QDict *qdict)
136 {
137 MouseInfoList *mice_list, *mouse;
138
139 mice_list = qmp_query_mice(NULL);
140 if (!mice_list) {
141 monitor_printf(mon, "No mouse devices connected\n");
142 return;
143 }
144
145 for (mouse = mice_list; mouse; mouse = mouse->next) {
146 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
147 mouse->value->current ? '*' : ' ',
148 mouse->value->index, mouse->value->name,
149 mouse->value->absolute ? " (absolute)" : "");
150 }
151
152 qapi_free_MouseInfoList(mice_list);
153 }
154
155 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
156 {
157 MigrationInfo *info;
158 MigrationCapabilityStatusList *caps, *cap;
159
160 info = qmp_query_migrate(NULL);
161 caps = qmp_query_migrate_capabilities(NULL);
162
163 /* do not display parameters during setup */
164 if (info->has_status && caps) {
165 monitor_printf(mon, "capabilities: ");
166 for (cap = caps; cap; cap = cap->next) {
167 monitor_printf(mon, "%s: %s ",
168 MigrationCapability_lookup[cap->value->capability],
169 cap->value->state ? "on" : "off");
170 }
171 monitor_printf(mon, "\n");
172 }
173
174 if (info->has_status) {
175 monitor_printf(mon, "Migration status: %s",
176 MigrationStatus_lookup[info->status]);
177 if (info->status == MIGRATION_STATUS_FAILED &&
178 info->has_error_desc) {
179 monitor_printf(mon, " (%s)\n", info->error_desc);
180 } else {
181 monitor_printf(mon, "\n");
182 }
183
184 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
185 info->total_time);
186 if (info->has_expected_downtime) {
187 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
188 info->expected_downtime);
189 }
190 if (info->has_downtime) {
191 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
192 info->downtime);
193 }
194 if (info->has_setup_time) {
195 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
196 info->setup_time);
197 }
198 }
199
200 if (info->has_ram) {
201 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
202 info->ram->transferred >> 10);
203 monitor_printf(mon, "throughput: %0.2f mbps\n",
204 info->ram->mbps);
205 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
206 info->ram->remaining >> 10);
207 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
208 info->ram->total >> 10);
209 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
210 info->ram->duplicate);
211 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
212 info->ram->skipped);
213 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
214 info->ram->normal);
215 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
216 info->ram->normal_bytes >> 10);
217 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
218 info->ram->dirty_sync_count);
219 monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
220 info->ram->page_size >> 10);
221
222 if (info->ram->dirty_pages_rate) {
223 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
224 info->ram->dirty_pages_rate);
225 }
226 if (info->ram->postcopy_requests) {
227 monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
228 info->ram->postcopy_requests);
229 }
230 }
231
232 if (info->has_disk) {
233 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
234 info->disk->transferred >> 10);
235 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
236 info->disk->remaining >> 10);
237 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
238 info->disk->total >> 10);
239 }
240
241 if (info->has_xbzrle_cache) {
242 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
243 info->xbzrle_cache->cache_size);
244 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
245 info->xbzrle_cache->bytes >> 10);
246 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
247 info->xbzrle_cache->pages);
248 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
249 info->xbzrle_cache->cache_miss);
250 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
251 info->xbzrle_cache->cache_miss_rate);
252 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
253 info->xbzrle_cache->overflow);
254 }
255
256 if (info->has_cpu_throttle_percentage) {
257 monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
258 info->cpu_throttle_percentage);
259 }
260
261 qapi_free_MigrationInfo(info);
262 qapi_free_MigrationCapabilityStatusList(caps);
263 }
264
265 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
266 {
267 MigrationCapabilityStatusList *caps, *cap;
268
269 caps = qmp_query_migrate_capabilities(NULL);
270
271 if (caps) {
272 for (cap = caps; cap; cap = cap->next) {
273 monitor_printf(mon, "%s: %s\n",
274 MigrationCapability_lookup[cap->value->capability],
275 cap->value->state ? "on" : "off");
276 }
277 }
278
279 qapi_free_MigrationCapabilityStatusList(caps);
280 }
281
282 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
283 {
284 MigrationParameters *params;
285
286 params = qmp_query_migrate_parameters(NULL);
287
288 if (params) {
289 assert(params->has_compress_level);
290 monitor_printf(mon, "%s: %" PRId64 "\n",
291 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
292 params->compress_level);
293 assert(params->has_compress_threads);
294 monitor_printf(mon, "%s: %" PRId64 "\n",
295 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
296 params->compress_threads);
297 assert(params->has_decompress_threads);
298 monitor_printf(mon, "%s: %" PRId64 "\n",
299 MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
300 params->decompress_threads);
301 assert(params->has_cpu_throttle_initial);
302 monitor_printf(mon, "%s: %" PRId64 "\n",
303 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL],
304 params->cpu_throttle_initial);
305 assert(params->has_cpu_throttle_increment);
306 monitor_printf(mon, "%s: %" PRId64 "\n",
307 MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT],
308 params->cpu_throttle_increment);
309 monitor_printf(mon, "%s: '%s'\n",
310 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_CREDS],
311 params->has_tls_creds ? params->tls_creds : "");
312 monitor_printf(mon, "%s: '%s'\n",
313 MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_HOSTNAME],
314 params->has_tls_hostname ? params->tls_hostname : "");
315 assert(params->has_max_bandwidth);
316 monitor_printf(mon, "%s: %" PRId64 " bytes/second\n",
317 MigrationParameter_lookup[MIGRATION_PARAMETER_MAX_BANDWIDTH],
318 params->max_bandwidth);
319 assert(params->has_downtime_limit);
320 monitor_printf(mon, "%s: %" PRId64 " milliseconds\n",
321 MigrationParameter_lookup[MIGRATION_PARAMETER_DOWNTIME_LIMIT],
322 params->downtime_limit);
323 assert(params->has_x_checkpoint_delay);
324 monitor_printf(mon, "%s: %" PRId64 "\n",
325 MigrationParameter_lookup[MIGRATION_PARAMETER_X_CHECKPOINT_DELAY],
326 params->x_checkpoint_delay);
327 }
328
329 qapi_free_MigrationParameters(params);
330 }
331
332 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
333 {
334 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
335 qmp_query_migrate_cache_size(NULL) >> 10);
336 }
337
338 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
339 {
340 CpuInfoList *cpu_list, *cpu;
341
342 cpu_list = qmp_query_cpus(NULL);
343
344 for (cpu = cpu_list; cpu; cpu = cpu->next) {
345 int active = ' ';
346
347 if (cpu->value->CPU == monitor_get_cpu_index()) {
348 active = '*';
349 }
350
351 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
352
353 switch (cpu->value->arch) {
354 case CPU_INFO_ARCH_X86:
355 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->u.x86.pc);
356 break;
357 case CPU_INFO_ARCH_PPC:
358 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->u.ppc.nip);
359 break;
360 case CPU_INFO_ARCH_SPARC:
361 monitor_printf(mon, " pc=0x%016" PRIx64,
362 cpu->value->u.q_sparc.pc);
363 monitor_printf(mon, " npc=0x%016" PRIx64,
364 cpu->value->u.q_sparc.npc);
365 break;
366 case CPU_INFO_ARCH_MIPS:
367 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.q_mips.PC);
368 break;
369 case CPU_INFO_ARCH_TRICORE:
370 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.tricore.PC);
371 break;
372 default:
373 break;
374 }
375
376 if (cpu->value->halted) {
377 monitor_printf(mon, " (halted)");
378 }
379
380 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
381 }
382
383 qapi_free_CpuInfoList(cpu_list);
384 }
385
386 static void print_block_info(Monitor *mon, BlockInfo *info,
387 BlockDeviceInfo *inserted, bool verbose)
388 {
389 ImageInfo *image_info;
390
391 assert(!info || !info->has_inserted || info->inserted == inserted);
392
393 if (info) {
394 monitor_printf(mon, "%s", info->device);
395 if (inserted && inserted->has_node_name) {
396 monitor_printf(mon, " (%s)", inserted->node_name);
397 }
398 } else {
399 assert(inserted);
400 monitor_printf(mon, "%s",
401 inserted->has_node_name
402 ? inserted->node_name
403 : "<anonymous>");
404 }
405
406 if (inserted) {
407 monitor_printf(mon, ": %s (%s%s%s)\n",
408 inserted->file,
409 inserted->drv,
410 inserted->ro ? ", read-only" : "",
411 inserted->encrypted ? ", encrypted" : "");
412 } else {
413 monitor_printf(mon, ": [not inserted]\n");
414 }
415
416 if (info) {
417 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
418 monitor_printf(mon, " I/O status: %s\n",
419 BlockDeviceIoStatus_lookup[info->io_status]);
420 }
421
422 if (info->removable) {
423 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
424 info->locked ? "" : "not ",
425 info->tray_open ? "open" : "closed");
426 }
427 }
428
429
430 if (!inserted) {
431 return;
432 }
433
434 monitor_printf(mon, " Cache mode: %s%s%s\n",
435 inserted->cache->writeback ? "writeback" : "writethrough",
436 inserted->cache->direct ? ", direct" : "",
437 inserted->cache->no_flush ? ", ignore flushes" : "");
438
439 if (inserted->has_backing_file) {
440 monitor_printf(mon,
441 " Backing file: %s "
442 "(chain depth: %" PRId64 ")\n",
443 inserted->backing_file,
444 inserted->backing_file_depth);
445 }
446
447 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
448 monitor_printf(mon, " Detect zeroes: %s\n",
449 BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
450 }
451
452 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
453 inserted->iops || inserted->iops_rd || inserted->iops_wr)
454 {
455 monitor_printf(mon, " I/O throttling: bps=%" PRId64
456 " bps_rd=%" PRId64 " bps_wr=%" PRId64
457 " bps_max=%" PRId64
458 " bps_rd_max=%" PRId64
459 " bps_wr_max=%" PRId64
460 " iops=%" PRId64 " iops_rd=%" PRId64
461 " iops_wr=%" PRId64
462 " iops_max=%" PRId64
463 " iops_rd_max=%" PRId64
464 " iops_wr_max=%" PRId64
465 " iops_size=%" PRId64
466 " group=%s\n",
467 inserted->bps,
468 inserted->bps_rd,
469 inserted->bps_wr,
470 inserted->bps_max,
471 inserted->bps_rd_max,
472 inserted->bps_wr_max,
473 inserted->iops,
474 inserted->iops_rd,
475 inserted->iops_wr,
476 inserted->iops_max,
477 inserted->iops_rd_max,
478 inserted->iops_wr_max,
479 inserted->iops_size,
480 inserted->group);
481 }
482
483 if (verbose) {
484 monitor_printf(mon, "\nImages:\n");
485 image_info = inserted->image;
486 while (1) {
487 bdrv_image_info_dump((fprintf_function)monitor_printf,
488 mon, image_info);
489 if (image_info->has_backing_image) {
490 image_info = image_info->backing_image;
491 } else {
492 break;
493 }
494 }
495 }
496 }
497
498 void hmp_info_block(Monitor *mon, const QDict *qdict)
499 {
500 BlockInfoList *block_list, *info;
501 BlockDeviceInfoList *blockdev_list, *blockdev;
502 const char *device = qdict_get_try_str(qdict, "device");
503 bool verbose = qdict_get_try_bool(qdict, "verbose", false);
504 bool nodes = qdict_get_try_bool(qdict, "nodes", false);
505 bool printed = false;
506
507 /* Print BlockBackend information */
508 if (!nodes) {
509 block_list = qmp_query_block(NULL);
510 } else {
511 block_list = NULL;
512 }
513
514 for (info = block_list; info; info = info->next) {
515 if (device && strcmp(device, info->value->device)) {
516 continue;
517 }
518
519 if (info != block_list) {
520 monitor_printf(mon, "\n");
521 }
522
523 print_block_info(mon, info->value, info->value->has_inserted
524 ? info->value->inserted : NULL,
525 verbose);
526 printed = true;
527 }
528
529 qapi_free_BlockInfoList(block_list);
530
531 if ((!device && !nodes) || printed) {
532 return;
533 }
534
535 /* Print node information */
536 blockdev_list = qmp_query_named_block_nodes(NULL);
537 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
538 assert(blockdev->value->has_node_name);
539 if (device && strcmp(device, blockdev->value->node_name)) {
540 continue;
541 }
542
543 if (blockdev != blockdev_list) {
544 monitor_printf(mon, "\n");
545 }
546
547 print_block_info(mon, NULL, blockdev->value, verbose);
548 }
549 qapi_free_BlockDeviceInfoList(blockdev_list);
550 }
551
552 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
553 {
554 BlockStatsList *stats_list, *stats;
555
556 stats_list = qmp_query_blockstats(false, false, NULL);
557
558 for (stats = stats_list; stats; stats = stats->next) {
559 if (!stats->value->has_device) {
560 continue;
561 }
562
563 monitor_printf(mon, "%s:", stats->value->device);
564 monitor_printf(mon, " rd_bytes=%" PRId64
565 " wr_bytes=%" PRId64
566 " rd_operations=%" PRId64
567 " wr_operations=%" PRId64
568 " flush_operations=%" PRId64
569 " wr_total_time_ns=%" PRId64
570 " rd_total_time_ns=%" PRId64
571 " flush_total_time_ns=%" PRId64
572 " rd_merged=%" PRId64
573 " wr_merged=%" PRId64
574 " idle_time_ns=%" PRId64
575 "\n",
576 stats->value->stats->rd_bytes,
577 stats->value->stats->wr_bytes,
578 stats->value->stats->rd_operations,
579 stats->value->stats->wr_operations,
580 stats->value->stats->flush_operations,
581 stats->value->stats->wr_total_time_ns,
582 stats->value->stats->rd_total_time_ns,
583 stats->value->stats->flush_total_time_ns,
584 stats->value->stats->rd_merged,
585 stats->value->stats->wr_merged,
586 stats->value->stats->idle_time_ns);
587 }
588
589 qapi_free_BlockStatsList(stats_list);
590 }
591
592 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
593 {
594 VncInfo *info;
595 Error *err = NULL;
596 VncClientInfoList *client;
597
598 info = qmp_query_vnc(&err);
599 if (err) {
600 error_report_err(err);
601 return;
602 }
603
604 if (!info->enabled) {
605 monitor_printf(mon, "Server: disabled\n");
606 goto out;
607 }
608
609 monitor_printf(mon, "Server:\n");
610 if (info->has_host && info->has_service) {
611 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
612 }
613 if (info->has_auth) {
614 monitor_printf(mon, " auth: %s\n", info->auth);
615 }
616
617 if (!info->has_clients || info->clients == NULL) {
618 monitor_printf(mon, "Client: none\n");
619 } else {
620 for (client = info->clients; client; client = client->next) {
621 monitor_printf(mon, "Client:\n");
622 monitor_printf(mon, " address: %s:%s\n",
623 client->value->host,
624 client->value->service);
625 monitor_printf(mon, " x509_dname: %s\n",
626 client->value->x509_dname ?
627 client->value->x509_dname : "none");
628 monitor_printf(mon, " username: %s\n",
629 client->value->has_sasl_username ?
630 client->value->sasl_username : "none");
631 }
632 }
633
634 out:
635 qapi_free_VncInfo(info);
636 }
637
638 #ifdef CONFIG_SPICE
639 void hmp_info_spice(Monitor *mon, const QDict *qdict)
640 {
641 SpiceChannelList *chan;
642 SpiceInfo *info;
643 const char *channel_name;
644 const char * const channel_names[] = {
645 [SPICE_CHANNEL_MAIN] = "main",
646 [SPICE_CHANNEL_DISPLAY] = "display",
647 [SPICE_CHANNEL_INPUTS] = "inputs",
648 [SPICE_CHANNEL_CURSOR] = "cursor",
649 [SPICE_CHANNEL_PLAYBACK] = "playback",
650 [SPICE_CHANNEL_RECORD] = "record",
651 [SPICE_CHANNEL_TUNNEL] = "tunnel",
652 [SPICE_CHANNEL_SMARTCARD] = "smartcard",
653 [SPICE_CHANNEL_USBREDIR] = "usbredir",
654 [SPICE_CHANNEL_PORT] = "port",
655 #if 0
656 /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
657 * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
658 * as quick fix for build failures with older versions. */
659 [SPICE_CHANNEL_WEBDAV] = "webdav",
660 #endif
661 };
662
663 info = qmp_query_spice(NULL);
664
665 if (!info->enabled) {
666 monitor_printf(mon, "Server: disabled\n");
667 goto out;
668 }
669
670 monitor_printf(mon, "Server:\n");
671 if (info->has_port) {
672 monitor_printf(mon, " address: %s:%" PRId64 "\n",
673 info->host, info->port);
674 }
675 if (info->has_tls_port) {
676 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
677 info->host, info->tls_port);
678 }
679 monitor_printf(mon, " migrated: %s\n",
680 info->migrated ? "true" : "false");
681 monitor_printf(mon, " auth: %s\n", info->auth);
682 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
683 monitor_printf(mon, " mouse-mode: %s\n",
684 SpiceQueryMouseMode_lookup[info->mouse_mode]);
685
686 if (!info->has_channels || info->channels == NULL) {
687 monitor_printf(mon, "Channels: none\n");
688 } else {
689 for (chan = info->channels; chan; chan = chan->next) {
690 monitor_printf(mon, "Channel:\n");
691 monitor_printf(mon, " address: %s:%s%s\n",
692 chan->value->host, chan->value->port,
693 chan->value->tls ? " [tls]" : "");
694 monitor_printf(mon, " session: %" PRId64 "\n",
695 chan->value->connection_id);
696 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
697 chan->value->channel_type, chan->value->channel_id);
698
699 channel_name = "unknown";
700 if (chan->value->channel_type > 0 &&
701 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
702 channel_names[chan->value->channel_type]) {
703 channel_name = channel_names[chan->value->channel_type];
704 }
705
706 monitor_printf(mon, " channel name: %s\n", channel_name);
707 }
708 }
709
710 out:
711 qapi_free_SpiceInfo(info);
712 }
713 #endif
714
715 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
716 {
717 BalloonInfo *info;
718 Error *err = NULL;
719
720 info = qmp_query_balloon(&err);
721 if (err) {
722 error_report_err(err);
723 return;
724 }
725
726 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
727
728 qapi_free_BalloonInfo(info);
729 }
730
731 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
732 {
733 PciMemoryRegionList *region;
734
735 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
736 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
737 dev->slot, dev->function);
738 monitor_printf(mon, " ");
739
740 if (dev->class_info->has_desc) {
741 monitor_printf(mon, "%s", dev->class_info->desc);
742 } else {
743 monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
744 }
745
746 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
747 dev->id->vendor, dev->id->device);
748
749 if (dev->has_irq) {
750 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
751 }
752
753 if (dev->has_pci_bridge) {
754 monitor_printf(mon, " BUS %" PRId64 ".\n",
755 dev->pci_bridge->bus->number);
756 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
757 dev->pci_bridge->bus->secondary);
758 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
759 dev->pci_bridge->bus->subordinate);
760
761 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
762 dev->pci_bridge->bus->io_range->base,
763 dev->pci_bridge->bus->io_range->limit);
764
765 monitor_printf(mon,
766 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
767 dev->pci_bridge->bus->memory_range->base,
768 dev->pci_bridge->bus->memory_range->limit);
769
770 monitor_printf(mon, " prefetchable memory range "
771 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
772 dev->pci_bridge->bus->prefetchable_range->base,
773 dev->pci_bridge->bus->prefetchable_range->limit);
774 }
775
776 for (region = dev->regions; region; region = region->next) {
777 uint64_t addr, size;
778
779 addr = region->value->address;
780 size = region->value->size;
781
782 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
783
784 if (!strcmp(region->value->type, "io")) {
785 monitor_printf(mon, "I/O at 0x%04" PRIx64
786 " [0x%04" PRIx64 "].\n",
787 addr, addr + size - 1);
788 } else {
789 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
790 " [0x%08" PRIx64 "].\n",
791 region->value->mem_type_64 ? 64 : 32,
792 region->value->prefetch ? " prefetchable" : "",
793 addr, addr + size - 1);
794 }
795 }
796
797 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
798
799 if (dev->has_pci_bridge) {
800 if (dev->pci_bridge->has_devices) {
801 PciDeviceInfoList *cdev;
802 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
803 hmp_info_pci_device(mon, cdev->value);
804 }
805 }
806 }
807 }
808
809 static int hmp_info_irq_foreach(Object *obj, void *opaque)
810 {
811 InterruptStatsProvider *intc;
812 InterruptStatsProviderClass *k;
813 Monitor *mon = opaque;
814
815 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
816 intc = INTERRUPT_STATS_PROVIDER(obj);
817 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
818 uint64_t *irq_counts;
819 unsigned int nb_irqs, i;
820 if (k->get_statistics &&
821 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
822 if (nb_irqs > 0) {
823 monitor_printf(mon, "IRQ statistics for %s:\n",
824 object_get_typename(obj));
825 for (i = 0; i < nb_irqs; i++) {
826 if (irq_counts[i] > 0) {
827 monitor_printf(mon, "%2d: %" PRId64 "\n", i,
828 irq_counts[i]);
829 }
830 }
831 }
832 } else {
833 monitor_printf(mon, "IRQ statistics not available for %s.\n",
834 object_get_typename(obj));
835 }
836 }
837
838 return 0;
839 }
840
841 void hmp_info_irq(Monitor *mon, const QDict *qdict)
842 {
843 object_child_foreach_recursive(object_get_root(),
844 hmp_info_irq_foreach, mon);
845 }
846
847 static int hmp_info_pic_foreach(Object *obj, void *opaque)
848 {
849 InterruptStatsProvider *intc;
850 InterruptStatsProviderClass *k;
851 Monitor *mon = opaque;
852
853 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
854 intc = INTERRUPT_STATS_PROVIDER(obj);
855 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
856 if (k->print_info) {
857 k->print_info(intc, mon);
858 } else {
859 monitor_printf(mon, "Interrupt controller information not available for %s.\n",
860 object_get_typename(obj));
861 }
862 }
863
864 return 0;
865 }
866
867 void hmp_info_pic(Monitor *mon, const QDict *qdict)
868 {
869 object_child_foreach_recursive(object_get_root(),
870 hmp_info_pic_foreach, mon);
871 }
872
873 void hmp_info_pci(Monitor *mon, const QDict *qdict)
874 {
875 PciInfoList *info_list, *info;
876 Error *err = NULL;
877
878 info_list = qmp_query_pci(&err);
879 if (err) {
880 monitor_printf(mon, "PCI devices not supported\n");
881 error_free(err);
882 return;
883 }
884
885 for (info = info_list; info; info = info->next) {
886 PciDeviceInfoList *dev;
887
888 for (dev = info->value->devices; dev; dev = dev->next) {
889 hmp_info_pci_device(mon, dev->value);
890 }
891 }
892
893 qapi_free_PciInfoList(info_list);
894 }
895
896 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
897 {
898 BlockJobInfoList *list;
899 Error *err = NULL;
900
901 list = qmp_query_block_jobs(&err);
902 assert(!err);
903
904 if (!list) {
905 monitor_printf(mon, "No active jobs\n");
906 return;
907 }
908
909 while (list) {
910 if (strcmp(list->value->type, "stream") == 0) {
911 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
912 " of %" PRId64 " bytes, speed limit %" PRId64
913 " bytes/s\n",
914 list->value->device,
915 list->value->offset,
916 list->value->len,
917 list->value->speed);
918 } else {
919 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
920 " of %" PRId64 " bytes, speed limit %" PRId64
921 " bytes/s\n",
922 list->value->type,
923 list->value->device,
924 list->value->offset,
925 list->value->len,
926 list->value->speed);
927 }
928 list = list->next;
929 }
930
931 qapi_free_BlockJobInfoList(list);
932 }
933
934 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
935 {
936 TPMInfoList *info_list, *info;
937 Error *err = NULL;
938 unsigned int c = 0;
939 TPMPassthroughOptions *tpo;
940
941 info_list = qmp_query_tpm(&err);
942 if (err) {
943 monitor_printf(mon, "TPM device not supported\n");
944 error_free(err);
945 return;
946 }
947
948 if (info_list) {
949 monitor_printf(mon, "TPM device:\n");
950 }
951
952 for (info = info_list; info; info = info->next) {
953 TPMInfo *ti = info->value;
954 monitor_printf(mon, " tpm%d: model=%s\n",
955 c, TpmModel_lookup[ti->model]);
956
957 monitor_printf(mon, " \\ %s: type=%s",
958 ti->id, TpmTypeOptionsKind_lookup[ti->options->type]);
959
960 switch (ti->options->type) {
961 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
962 tpo = ti->options->u.passthrough.data;
963 monitor_printf(mon, "%s%s%s%s",
964 tpo->has_path ? ",path=" : "",
965 tpo->has_path ? tpo->path : "",
966 tpo->has_cancel_path ? ",cancel-path=" : "",
967 tpo->has_cancel_path ? tpo->cancel_path : "");
968 break;
969 case TPM_TYPE_OPTIONS_KIND__MAX:
970 break;
971 }
972 monitor_printf(mon, "\n");
973 c++;
974 }
975 qapi_free_TPMInfoList(info_list);
976 }
977
978 void hmp_quit(Monitor *mon, const QDict *qdict)
979 {
980 monitor_suspend(mon);
981 qmp_quit(NULL);
982 }
983
984 void hmp_stop(Monitor *mon, const QDict *qdict)
985 {
986 qmp_stop(NULL);
987 }
988
989 void hmp_system_reset(Monitor *mon, const QDict *qdict)
990 {
991 qmp_system_reset(NULL);
992 }
993
994 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
995 {
996 qmp_system_powerdown(NULL);
997 }
998
999 void hmp_cpu(Monitor *mon, const QDict *qdict)
1000 {
1001 int64_t cpu_index;
1002
1003 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1004 use it are converted to the QAPI */
1005 cpu_index = qdict_get_int(qdict, "index");
1006 if (monitor_set_cpu(cpu_index) < 0) {
1007 monitor_printf(mon, "invalid CPU index\n");
1008 }
1009 }
1010
1011 void hmp_memsave(Monitor *mon, const QDict *qdict)
1012 {
1013 uint32_t size = qdict_get_int(qdict, "size");
1014 const char *filename = qdict_get_str(qdict, "filename");
1015 uint64_t addr = qdict_get_int(qdict, "val");
1016 Error *err = NULL;
1017 int cpu_index = monitor_get_cpu_index();
1018
1019 if (cpu_index < 0) {
1020 monitor_printf(mon, "No CPU available\n");
1021 return;
1022 }
1023
1024 qmp_memsave(addr, size, filename, true, cpu_index, &err);
1025 hmp_handle_error(mon, &err);
1026 }
1027
1028 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1029 {
1030 uint32_t size = qdict_get_int(qdict, "size");
1031 const char *filename = qdict_get_str(qdict, "filename");
1032 uint64_t addr = qdict_get_int(qdict, "val");
1033 Error *err = NULL;
1034
1035 qmp_pmemsave(addr, size, filename, &err);
1036 hmp_handle_error(mon, &err);
1037 }
1038
1039 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1040 {
1041 const char *chardev = qdict_get_str(qdict, "device");
1042 const char *data = qdict_get_str(qdict, "data");
1043 Error *err = NULL;
1044
1045 qmp_ringbuf_write(chardev, data, false, 0, &err);
1046
1047 hmp_handle_error(mon, &err);
1048 }
1049
1050 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1051 {
1052 uint32_t size = qdict_get_int(qdict, "size");
1053 const char *chardev = qdict_get_str(qdict, "device");
1054 char *data;
1055 Error *err = NULL;
1056 int i;
1057
1058 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1059 if (err) {
1060 error_report_err(err);
1061 return;
1062 }
1063
1064 for (i = 0; data[i]; i++) {
1065 unsigned char ch = data[i];
1066
1067 if (ch == '\\') {
1068 monitor_printf(mon, "\\\\");
1069 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1070 monitor_printf(mon, "\\u%04X", ch);
1071 } else {
1072 monitor_printf(mon, "%c", ch);
1073 }
1074
1075 }
1076 monitor_printf(mon, "\n");
1077 g_free(data);
1078 }
1079
1080 static void hmp_cont_cb(void *opaque, int err)
1081 {
1082 if (!err) {
1083 qmp_cont(NULL);
1084 }
1085 }
1086
1087 static bool key_is_missing(const BlockInfo *bdev)
1088 {
1089 return (bdev->inserted && bdev->inserted->encryption_key_missing);
1090 }
1091
1092 void hmp_cont(Monitor *mon, const QDict *qdict)
1093 {
1094 BlockInfoList *bdev_list, *bdev;
1095 Error *err = NULL;
1096
1097 bdev_list = qmp_query_block(NULL);
1098 for (bdev = bdev_list; bdev; bdev = bdev->next) {
1099 if (key_is_missing(bdev->value)) {
1100 monitor_read_block_device_key(mon, bdev->value->device,
1101 hmp_cont_cb, NULL);
1102 goto out;
1103 }
1104 }
1105
1106 qmp_cont(&err);
1107 hmp_handle_error(mon, &err);
1108
1109 out:
1110 qapi_free_BlockInfoList(bdev_list);
1111 }
1112
1113 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1114 {
1115 qmp_system_wakeup(NULL);
1116 }
1117
1118 void hmp_nmi(Monitor *mon, const QDict *qdict)
1119 {
1120 Error *err = NULL;
1121
1122 qmp_inject_nmi(&err);
1123 hmp_handle_error(mon, &err);
1124 }
1125
1126 void hmp_set_link(Monitor *mon, const QDict *qdict)
1127 {
1128 const char *name = qdict_get_str(qdict, "name");
1129 bool up = qdict_get_bool(qdict, "up");
1130 Error *err = NULL;
1131
1132 qmp_set_link(name, up, &err);
1133 hmp_handle_error(mon, &err);
1134 }
1135
1136 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1137 {
1138 const char *device = qdict_get_str(qdict, "device");
1139 const char *password = qdict_get_str(qdict, "password");
1140 Error *err = NULL;
1141
1142 qmp_block_passwd(true, device, false, NULL, password, &err);
1143 hmp_handle_error(mon, &err);
1144 }
1145
1146 void hmp_balloon(Monitor *mon, const QDict *qdict)
1147 {
1148 int64_t value = qdict_get_int(qdict, "value");
1149 Error *err = NULL;
1150
1151 qmp_balloon(value, &err);
1152 if (err) {
1153 error_report_err(err);
1154 }
1155 }
1156
1157 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1158 {
1159 const char *device = qdict_get_str(qdict, "device");
1160 int64_t size = qdict_get_int(qdict, "size");
1161 Error *err = NULL;
1162
1163 qmp_block_resize(true, device, false, NULL, size, &err);
1164 hmp_handle_error(mon, &err);
1165 }
1166
1167 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1168 {
1169 const char *filename = qdict_get_str(qdict, "target");
1170 const char *format = qdict_get_try_str(qdict, "format");
1171 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1172 bool full = qdict_get_try_bool(qdict, "full", false);
1173 Error *err = NULL;
1174 DriveMirror mirror = {
1175 .device = (char *)qdict_get_str(qdict, "device"),
1176 .target = (char *)filename,
1177 .has_format = !!format,
1178 .format = (char *)format,
1179 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1180 .has_mode = true,
1181 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1182 .unmap = true,
1183 };
1184
1185 if (!filename) {
1186 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1187 hmp_handle_error(mon, &err);
1188 return;
1189 }
1190 qmp_drive_mirror(&mirror, &err);
1191 hmp_handle_error(mon, &err);
1192 }
1193
1194 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1195 {
1196 const char *device = qdict_get_str(qdict, "device");
1197 const char *filename = qdict_get_str(qdict, "target");
1198 const char *format = qdict_get_try_str(qdict, "format");
1199 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1200 bool full = qdict_get_try_bool(qdict, "full", false);
1201 bool compress = qdict_get_try_bool(qdict, "compress", false);
1202 Error *err = NULL;
1203 DriveBackup backup = {
1204 .device = (char *)device,
1205 .target = (char *)filename,
1206 .has_format = !!format,
1207 .format = (char *)format,
1208 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1209 .has_mode = true,
1210 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1211 .has_compress = !!compress,
1212 .compress = compress,
1213 };
1214
1215 if (!filename) {
1216 error_setg(&err, QERR_MISSING_PARAMETER, "target");
1217 hmp_handle_error(mon, &err);
1218 return;
1219 }
1220
1221 qmp_drive_backup(&backup, &err);
1222 hmp_handle_error(mon, &err);
1223 }
1224
1225 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1226 {
1227 const char *device = qdict_get_str(qdict, "device");
1228 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1229 const char *format = qdict_get_try_str(qdict, "format");
1230 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1231 enum NewImageMode mode;
1232 Error *err = NULL;
1233
1234 if (!filename) {
1235 /* In the future, if 'snapshot-file' is not specified, the snapshot
1236 will be taken internally. Today it's actually required. */
1237 error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1238 hmp_handle_error(mon, &err);
1239 return;
1240 }
1241
1242 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1243 qmp_blockdev_snapshot_sync(true, device, false, NULL,
1244 filename, false, NULL,
1245 !!format, format,
1246 true, mode, &err);
1247 hmp_handle_error(mon, &err);
1248 }
1249
1250 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1251 {
1252 const char *device = qdict_get_str(qdict, "device");
1253 const char *name = qdict_get_str(qdict, "name");
1254 Error *err = NULL;
1255
1256 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1257 hmp_handle_error(mon, &err);
1258 }
1259
1260 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1261 {
1262 const char *device = qdict_get_str(qdict, "device");
1263 const char *name = qdict_get_str(qdict, "name");
1264 const char *id = qdict_get_try_str(qdict, "id");
1265 Error *err = NULL;
1266
1267 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1268 true, name, &err);
1269 hmp_handle_error(mon, &err);
1270 }
1271
1272 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1273 {
1274 int saved_vm_running = runstate_is_running();
1275 const char *name = qdict_get_str(qdict, "name");
1276
1277 vm_stop(RUN_STATE_RESTORE_VM);
1278
1279 if (load_vmstate(name) == 0 && saved_vm_running) {
1280 vm_start();
1281 }
1282 }
1283
1284 void hmp_savevm(Monitor *mon, const QDict *qdict)
1285 {
1286 save_vmstate(qdict_get_try_str(qdict, "name"));
1287 }
1288
1289 void hmp_delvm(Monitor *mon, const QDict *qdict)
1290 {
1291 BlockDriverState *bs;
1292 Error *err;
1293 const char *name = qdict_get_str(qdict, "name");
1294
1295 if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1296 error_reportf_err(err,
1297 "Error while deleting snapshot on device '%s': ",
1298 bdrv_get_device_name(bs));
1299 }
1300 }
1301
1302 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1303 {
1304 qmp_migrate_cancel(NULL);
1305 }
1306
1307 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1308 {
1309 Error *err = NULL;
1310 const char *uri = qdict_get_str(qdict, "uri");
1311
1312 qmp_migrate_incoming(uri, &err);
1313
1314 hmp_handle_error(mon, &err);
1315 }
1316
1317 /* Kept for backwards compatibility */
1318 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1319 {
1320 double value = qdict_get_double(qdict, "value");
1321 qmp_migrate_set_downtime(value, NULL);
1322 }
1323
1324 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1325 {
1326 int64_t value = qdict_get_int(qdict, "value");
1327 Error *err = NULL;
1328
1329 qmp_migrate_set_cache_size(value, &err);
1330 if (err) {
1331 error_report_err(err);
1332 return;
1333 }
1334 }
1335
1336 /* Kept for backwards compatibility */
1337 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1338 {
1339 int64_t value = qdict_get_int(qdict, "value");
1340 qmp_migrate_set_speed(value, NULL);
1341 }
1342
1343 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1344 {
1345 const char *cap = qdict_get_str(qdict, "capability");
1346 bool state = qdict_get_bool(qdict, "state");
1347 Error *err = NULL;
1348 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1349 int i;
1350
1351 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1352 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1353 caps->value = g_malloc0(sizeof(*caps->value));
1354 caps->value->capability = i;
1355 caps->value->state = state;
1356 caps->next = NULL;
1357 qmp_migrate_set_capabilities(caps, &err);
1358 break;
1359 }
1360 }
1361
1362 if (i == MIGRATION_CAPABILITY__MAX) {
1363 error_setg(&err, QERR_INVALID_PARAMETER, cap);
1364 }
1365
1366 qapi_free_MigrationCapabilityStatusList(caps);
1367
1368 if (err) {
1369 error_report_err(err);
1370 }
1371 }
1372
1373 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1374 {
1375 const char *param = qdict_get_str(qdict, "parameter");
1376 const char *valuestr = qdict_get_str(qdict, "value");
1377 uint64_t valuebw = 0;
1378 long valueint = 0;
1379 Error *err = NULL;
1380 bool use_int_value = false;
1381 int i, ret;
1382
1383 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1384 if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
1385 MigrationParameters p = { 0 };
1386 switch (i) {
1387 case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1388 p.has_compress_level = true;
1389 use_int_value = true;
1390 break;
1391 case MIGRATION_PARAMETER_COMPRESS_THREADS:
1392 p.has_compress_threads = true;
1393 use_int_value = true;
1394 break;
1395 case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1396 p.has_decompress_threads = true;
1397 use_int_value = true;
1398 break;
1399 case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1400 p.has_cpu_throttle_initial = true;
1401 use_int_value = true;
1402 break;
1403 case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1404 p.has_cpu_throttle_increment = true;
1405 use_int_value = true;
1406 break;
1407 case MIGRATION_PARAMETER_TLS_CREDS:
1408 p.has_tls_creds = true;
1409 p.tls_creds = (char *) valuestr;
1410 break;
1411 case MIGRATION_PARAMETER_TLS_HOSTNAME:
1412 p.has_tls_hostname = true;
1413 p.tls_hostname = (char *) valuestr;
1414 break;
1415 case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1416 p.has_max_bandwidth = true;
1417 ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1418 if (ret < 0 || valuebw > INT64_MAX
1419 || (size_t)valuebw != valuebw) {
1420 error_setg(&err, "Invalid size %s", valuestr);
1421 goto cleanup;
1422 }
1423 p.max_bandwidth = valuebw;
1424 break;
1425 case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1426 p.has_downtime_limit = true;
1427 use_int_value = true;
1428 break;
1429 case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1430 p.has_x_checkpoint_delay = true;
1431 use_int_value = true;
1432 break;
1433 }
1434
1435 if (use_int_value) {
1436 if (qemu_strtol(valuestr, NULL, 10, &valueint) < 0) {
1437 error_setg(&err, "Unable to parse '%s' as an int",
1438 valuestr);
1439 goto cleanup;
1440 }
1441 /* Set all integers; only one has_FOO will be set, and
1442 * the code ignores the remaining values */
1443 p.compress_level = valueint;
1444 p.compress_threads = valueint;
1445 p.decompress_threads = valueint;
1446 p.cpu_throttle_initial = valueint;
1447 p.cpu_throttle_increment = valueint;
1448 p.downtime_limit = valueint;
1449 p.x_checkpoint_delay = valueint;
1450 }
1451
1452 qmp_migrate_set_parameters(&p, &err);
1453 break;
1454 }
1455 }
1456
1457 if (i == MIGRATION_PARAMETER__MAX) {
1458 error_setg(&err, QERR_INVALID_PARAMETER, param);
1459 }
1460
1461 cleanup:
1462 if (err) {
1463 error_report_err(err);
1464 }
1465 }
1466
1467 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1468 {
1469 Error *err = NULL;
1470 const char *protocol = qdict_get_str(qdict, "protocol");
1471 const char *hostname = qdict_get_str(qdict, "hostname");
1472 bool has_port = qdict_haskey(qdict, "port");
1473 int port = qdict_get_try_int(qdict, "port", -1);
1474 bool has_tls_port = qdict_haskey(qdict, "tls-port");
1475 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1476 const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1477
1478 qmp_client_migrate_info(protocol, hostname,
1479 has_port, port, has_tls_port, tls_port,
1480 !!cert_subject, cert_subject, &err);
1481 hmp_handle_error(mon, &err);
1482 }
1483
1484 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1485 {
1486 Error *err = NULL;
1487 qmp_migrate_start_postcopy(&err);
1488 hmp_handle_error(mon, &err);
1489 }
1490
1491 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1492 {
1493 Error *err = NULL;
1494
1495 qmp_x_colo_lost_heartbeat(&err);
1496 hmp_handle_error(mon, &err);
1497 }
1498
1499 void hmp_set_password(Monitor *mon, const QDict *qdict)
1500 {
1501 const char *protocol = qdict_get_str(qdict, "protocol");
1502 const char *password = qdict_get_str(qdict, "password");
1503 const char *connected = qdict_get_try_str(qdict, "connected");
1504 Error *err = NULL;
1505
1506 qmp_set_password(protocol, password, !!connected, connected, &err);
1507 hmp_handle_error(mon, &err);
1508 }
1509
1510 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1511 {
1512 const char *protocol = qdict_get_str(qdict, "protocol");
1513 const char *whenstr = qdict_get_str(qdict, "time");
1514 Error *err = NULL;
1515
1516 qmp_expire_password(protocol, whenstr, &err);
1517 hmp_handle_error(mon, &err);
1518 }
1519
1520 void hmp_eject(Monitor *mon, const QDict *qdict)
1521 {
1522 bool force = qdict_get_try_bool(qdict, "force", false);
1523 const char *device = qdict_get_str(qdict, "device");
1524 Error *err = NULL;
1525
1526 qmp_eject(true, device, false, NULL, true, force, &err);
1527 hmp_handle_error(mon, &err);
1528 }
1529
1530 static void hmp_change_read_arg(void *opaque, const char *password,
1531 void *readline_opaque)
1532 {
1533 qmp_change_vnc_password(password, NULL);
1534 monitor_read_command(opaque, 1);
1535 }
1536
1537 void hmp_change(Monitor *mon, const QDict *qdict)
1538 {
1539 const char *device = qdict_get_str(qdict, "device");
1540 const char *target = qdict_get_str(qdict, "target");
1541 const char *arg = qdict_get_try_str(qdict, "arg");
1542 const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1543 BlockdevChangeReadOnlyMode read_only_mode = 0;
1544 Error *err = NULL;
1545
1546 if (strcmp(device, "vnc") == 0) {
1547 if (read_only) {
1548 monitor_printf(mon,
1549 "Parameter 'read-only-mode' is invalid for VNC\n");
1550 return;
1551 }
1552 if (strcmp(target, "passwd") == 0 ||
1553 strcmp(target, "password") == 0) {
1554 if (!arg) {
1555 monitor_read_password(mon, hmp_change_read_arg, NULL);
1556 return;
1557 }
1558 }
1559 qmp_change("vnc", target, !!arg, arg, &err);
1560 } else {
1561 if (read_only) {
1562 read_only_mode =
1563 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
1564 read_only, BLOCKDEV_CHANGE_READ_ONLY_MODE__MAX,
1565 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1566 if (err) {
1567 hmp_handle_error(mon, &err);
1568 return;
1569 }
1570 }
1571
1572 qmp_blockdev_change_medium(true, device, false, NULL, target,
1573 !!arg, arg, !!read_only, read_only_mode,
1574 &err);
1575 if (err &&
1576 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1577 error_free(err);
1578 monitor_read_block_device_key(mon, device, NULL, NULL);
1579 return;
1580 }
1581 }
1582
1583 hmp_handle_error(mon, &err);
1584 }
1585
1586 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1587 {
1588 Error *err = NULL;
1589 BlockIOThrottle throttle = {
1590 .has_device = true,
1591 .device = (char *) qdict_get_str(qdict, "device"),
1592 .bps = qdict_get_int(qdict, "bps"),
1593 .bps_rd = qdict_get_int(qdict, "bps_rd"),
1594 .bps_wr = qdict_get_int(qdict, "bps_wr"),
1595 .iops = qdict_get_int(qdict, "iops"),
1596 .iops_rd = qdict_get_int(qdict, "iops_rd"),
1597 .iops_wr = qdict_get_int(qdict, "iops_wr"),
1598 };
1599
1600 qmp_block_set_io_throttle(&throttle, &err);
1601 hmp_handle_error(mon, &err);
1602 }
1603
1604 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1605 {
1606 Error *error = NULL;
1607 const char *device = qdict_get_str(qdict, "device");
1608 const char *base = qdict_get_try_str(qdict, "base");
1609 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1610
1611 qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
1612 false, NULL, qdict_haskey(qdict, "speed"), speed,
1613 true, BLOCKDEV_ON_ERROR_REPORT, &error);
1614
1615 hmp_handle_error(mon, &error);
1616 }
1617
1618 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1619 {
1620 Error *error = NULL;
1621 const char *device = qdict_get_str(qdict, "device");
1622 int64_t value = qdict_get_int(qdict, "speed");
1623
1624 qmp_block_job_set_speed(device, value, &error);
1625
1626 hmp_handle_error(mon, &error);
1627 }
1628
1629 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1630 {
1631 Error *error = NULL;
1632 const char *device = qdict_get_str(qdict, "device");
1633 bool force = qdict_get_try_bool(qdict, "force", false);
1634
1635 qmp_block_job_cancel(device, true, force, &error);
1636
1637 hmp_handle_error(mon, &error);
1638 }
1639
1640 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1641 {
1642 Error *error = NULL;
1643 const char *device = qdict_get_str(qdict, "device");
1644
1645 qmp_block_job_pause(device, &error);
1646
1647 hmp_handle_error(mon, &error);
1648 }
1649
1650 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1651 {
1652 Error *error = NULL;
1653 const char *device = qdict_get_str(qdict, "device");
1654
1655 qmp_block_job_resume(device, &error);
1656
1657 hmp_handle_error(mon, &error);
1658 }
1659
1660 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1661 {
1662 Error *error = NULL;
1663 const char *device = qdict_get_str(qdict, "device");
1664
1665 qmp_block_job_complete(device, &error);
1666
1667 hmp_handle_error(mon, &error);
1668 }
1669
1670 typedef struct HMPMigrationStatus
1671 {
1672 QEMUTimer *timer;
1673 Monitor *mon;
1674 bool is_block_migration;
1675 } HMPMigrationStatus;
1676
1677 static void hmp_migrate_status_cb(void *opaque)
1678 {
1679 HMPMigrationStatus *status = opaque;
1680 MigrationInfo *info;
1681
1682 info = qmp_query_migrate(NULL);
1683 if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1684 info->status == MIGRATION_STATUS_SETUP) {
1685 if (info->has_disk) {
1686 int progress;
1687
1688 if (info->disk->remaining) {
1689 progress = info->disk->transferred * 100 / info->disk->total;
1690 } else {
1691 progress = 100;
1692 }
1693
1694 monitor_printf(status->mon, "Completed %d %%\r", progress);
1695 monitor_flush(status->mon);
1696 }
1697
1698 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1699 } else {
1700 if (status->is_block_migration) {
1701 monitor_printf(status->mon, "\n");
1702 }
1703 if (info->has_error_desc) {
1704 error_report("%s", info->error_desc);
1705 }
1706 monitor_resume(status->mon);
1707 timer_del(status->timer);
1708 g_free(status);
1709 }
1710
1711 qapi_free_MigrationInfo(info);
1712 }
1713
1714 void hmp_migrate(Monitor *mon, const QDict *qdict)
1715 {
1716 bool detach = qdict_get_try_bool(qdict, "detach", false);
1717 bool blk = qdict_get_try_bool(qdict, "blk", false);
1718 bool inc = qdict_get_try_bool(qdict, "inc", false);
1719 const char *uri = qdict_get_str(qdict, "uri");
1720 Error *err = NULL;
1721
1722 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1723 if (err) {
1724 error_report_err(err);
1725 return;
1726 }
1727
1728 if (!detach) {
1729 HMPMigrationStatus *status;
1730
1731 if (monitor_suspend(mon) < 0) {
1732 monitor_printf(mon, "terminal does not allow synchronous "
1733 "migration, continuing detached\n");
1734 return;
1735 }
1736
1737 status = g_malloc0(sizeof(*status));
1738 status->mon = mon;
1739 status->is_block_migration = blk || inc;
1740 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1741 status);
1742 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1743 }
1744 }
1745
1746 void hmp_device_add(Monitor *mon, const QDict *qdict)
1747 {
1748 Error *err = NULL;
1749
1750 qmp_device_add((QDict *)qdict, NULL, &err);
1751 hmp_handle_error(mon, &err);
1752 }
1753
1754 void hmp_device_del(Monitor *mon, const QDict *qdict)
1755 {
1756 const char *id = qdict_get_str(qdict, "id");
1757 Error *err = NULL;
1758
1759 qmp_device_del(id, &err);
1760 hmp_handle_error(mon, &err);
1761 }
1762
1763 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1764 {
1765 Error *err = NULL;
1766 bool paging = qdict_get_try_bool(qdict, "paging", false);
1767 bool zlib = qdict_get_try_bool(qdict, "zlib", false);
1768 bool lzo = qdict_get_try_bool(qdict, "lzo", false);
1769 bool snappy = qdict_get_try_bool(qdict, "snappy", false);
1770 const char *file = qdict_get_str(qdict, "filename");
1771 bool has_begin = qdict_haskey(qdict, "begin");
1772 bool has_length = qdict_haskey(qdict, "length");
1773 bool has_detach = qdict_haskey(qdict, "detach");
1774 int64_t begin = 0;
1775 int64_t length = 0;
1776 bool detach = false;
1777 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1778 char *prot;
1779
1780 if (zlib + lzo + snappy > 1) {
1781 error_setg(&err, "only one of '-z|-l|-s' can be set");
1782 hmp_handle_error(mon, &err);
1783 return;
1784 }
1785
1786 if (zlib) {
1787 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1788 }
1789
1790 if (lzo) {
1791 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1792 }
1793
1794 if (snappy) {
1795 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1796 }
1797
1798 if (has_begin) {
1799 begin = qdict_get_int(qdict, "begin");
1800 }
1801 if (has_length) {
1802 length = qdict_get_int(qdict, "length");
1803 }
1804 if (has_detach) {
1805 detach = qdict_get_bool(qdict, "detach");
1806 }
1807
1808 prot = g_strconcat("file:", file, NULL);
1809
1810 qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
1811 has_length, length, true, dump_format, &err);
1812 hmp_handle_error(mon, &err);
1813 g_free(prot);
1814 }
1815
1816 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1817 {
1818 Error *err = NULL;
1819 QemuOpts *opts;
1820
1821 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1822 if (err) {
1823 goto out;
1824 }
1825
1826 netdev_add(opts, &err);
1827 if (err) {
1828 qemu_opts_del(opts);
1829 }
1830
1831 out:
1832 hmp_handle_error(mon, &err);
1833 }
1834
1835 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1836 {
1837 const char *id = qdict_get_str(qdict, "id");
1838 Error *err = NULL;
1839
1840 qmp_netdev_del(id, &err);
1841 hmp_handle_error(mon, &err);
1842 }
1843
1844 void hmp_object_add(Monitor *mon, const QDict *qdict)
1845 {
1846 Error *err = NULL;
1847 QemuOpts *opts;
1848 Object *obj = NULL;
1849
1850 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1851 if (err) {
1852 hmp_handle_error(mon, &err);
1853 return;
1854 }
1855
1856 obj = user_creatable_add_opts(opts, &err);
1857 qemu_opts_del(opts);
1858
1859 if (err) {
1860 hmp_handle_error(mon, &err);
1861 }
1862 if (obj) {
1863 object_unref(obj);
1864 }
1865 }
1866
1867 void hmp_getfd(Monitor *mon, const QDict *qdict)
1868 {
1869 const char *fdname = qdict_get_str(qdict, "fdname");
1870 Error *err = NULL;
1871
1872 qmp_getfd(fdname, &err);
1873 hmp_handle_error(mon, &err);
1874 }
1875
1876 void hmp_closefd(Monitor *mon, const QDict *qdict)
1877 {
1878 const char *fdname = qdict_get_str(qdict, "fdname");
1879 Error *err = NULL;
1880
1881 qmp_closefd(fdname, &err);
1882 hmp_handle_error(mon, &err);
1883 }
1884
1885 void hmp_sendkey(Monitor *mon, const QDict *qdict)
1886 {
1887 const char *keys = qdict_get_str(qdict, "keys");
1888 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1889 int has_hold_time = qdict_haskey(qdict, "hold-time");
1890 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1891 Error *err = NULL;
1892 char *separator;
1893 int keyname_len;
1894
1895 while (1) {
1896 separator = strchr(keys, '-');
1897 keyname_len = separator ? separator - keys : strlen(keys);
1898
1899 /* Be compatible with old interface, convert user inputted "<" */
1900 if (keys[0] == '<' && keyname_len == 1) {
1901 keys = "less";
1902 keyname_len = 4;
1903 }
1904
1905 keylist = g_malloc0(sizeof(*keylist));
1906 keylist->value = g_malloc0(sizeof(*keylist->value));
1907
1908 if (!head) {
1909 head = keylist;
1910 }
1911 if (tmp) {
1912 tmp->next = keylist;
1913 }
1914 tmp = keylist;
1915
1916 if (strstart(keys, "0x", NULL)) {
1917 char *endp;
1918 int value = strtoul(keys, &endp, 0);
1919 assert(endp <= keys + keyname_len);
1920 if (endp != keys + keyname_len) {
1921 goto err_out;
1922 }
1923 keylist->value->type = KEY_VALUE_KIND_NUMBER;
1924 keylist->value->u.number.data = value;
1925 } else {
1926 int idx = index_from_key(keys, keyname_len);
1927 if (idx == Q_KEY_CODE__MAX) {
1928 goto err_out;
1929 }
1930 keylist->value->type = KEY_VALUE_KIND_QCODE;
1931 keylist->value->u.qcode.data = idx;
1932 }
1933
1934 if (!separator) {
1935 break;
1936 }
1937 keys = separator + 1;
1938 }
1939
1940 qmp_send_key(head, has_hold_time, hold_time, &err);
1941 hmp_handle_error(mon, &err);
1942
1943 out:
1944 qapi_free_KeyValueList(head);
1945 return;
1946
1947 err_out:
1948 monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
1949 goto out;
1950 }
1951
1952 void hmp_screendump(Monitor *mon, const QDict *qdict)
1953 {
1954 const char *filename = qdict_get_str(qdict, "filename");
1955 Error *err = NULL;
1956
1957 qmp_screendump(filename, &err);
1958 hmp_handle_error(mon, &err);
1959 }
1960
1961 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1962 {
1963 const char *uri = qdict_get_str(qdict, "uri");
1964 bool writable = qdict_get_try_bool(qdict, "writable", false);
1965 bool all = qdict_get_try_bool(qdict, "all", false);
1966 Error *local_err = NULL;
1967 BlockInfoList *block_list, *info;
1968 SocketAddress *addr;
1969
1970 if (writable && !all) {
1971 error_setg(&local_err, "-w only valid together with -a");
1972 goto exit;
1973 }
1974
1975 /* First check if the address is valid and start the server. */
1976 addr = socket_parse(uri, &local_err);
1977 if (local_err != NULL) {
1978 goto exit;
1979 }
1980
1981 qmp_nbd_server_start(addr, false, NULL, &local_err);
1982 qapi_free_SocketAddress(addr);
1983 if (local_err != NULL) {
1984 goto exit;
1985 }
1986
1987 if (!all) {
1988 return;
1989 }
1990
1991 /* Then try adding all block devices. If one fails, close all and
1992 * exit.
1993 */
1994 block_list = qmp_query_block(NULL);
1995
1996 for (info = block_list; info; info = info->next) {
1997 if (!info->value->has_inserted) {
1998 continue;
1999 }
2000
2001 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
2002
2003 if (local_err != NULL) {
2004 qmp_nbd_server_stop(NULL);
2005 break;
2006 }
2007 }
2008
2009 qapi_free_BlockInfoList(block_list);
2010
2011 exit:
2012 hmp_handle_error(mon, &local_err);
2013 }
2014
2015 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2016 {
2017 const char *device = qdict_get_str(qdict, "device");
2018 bool writable = qdict_get_try_bool(qdict, "writable", false);
2019 Error *local_err = NULL;
2020
2021 qmp_nbd_server_add(device, true, writable, &local_err);
2022
2023 if (local_err != NULL) {
2024 hmp_handle_error(mon, &local_err);
2025 }
2026 }
2027
2028 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2029 {
2030 Error *err = NULL;
2031
2032 qmp_nbd_server_stop(&err);
2033 hmp_handle_error(mon, &err);
2034 }
2035
2036 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2037 {
2038 int cpuid;
2039 Error *err = NULL;
2040
2041 cpuid = qdict_get_int(qdict, "id");
2042 qmp_cpu_add(cpuid, &err);
2043 hmp_handle_error(mon, &err);
2044 }
2045
2046 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2047 {
2048 const char *args = qdict_get_str(qdict, "args");
2049 Error *err = NULL;
2050 QemuOpts *opts;
2051
2052 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2053 if (opts == NULL) {
2054 error_setg(&err, "Parsing chardev args failed");
2055 } else {
2056 qemu_chr_new_from_opts(opts, &err);
2057 qemu_opts_del(opts);
2058 }
2059 hmp_handle_error(mon, &err);
2060 }
2061
2062 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2063 {
2064 Error *local_err = NULL;
2065
2066 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2067 hmp_handle_error(mon, &local_err);
2068 }
2069
2070 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2071 {
2072 BlockBackend *blk;
2073 BlockBackend *local_blk = NULL;
2074 AioContext *aio_context;
2075 const char* device = qdict_get_str(qdict, "device");
2076 const char* command = qdict_get_str(qdict, "command");
2077 Error *err = NULL;
2078 int ret;
2079
2080 blk = blk_by_name(device);
2081 if (!blk) {
2082 BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2083 if (bs) {
2084 blk = local_blk = blk_new(0, BLK_PERM_ALL);
2085 ret = blk_insert_bs(blk, bs, &err);
2086 if (ret < 0) {
2087 goto fail;
2088 }
2089 } else {
2090 goto fail;
2091 }
2092 }
2093
2094 aio_context = blk_get_aio_context(blk);
2095 aio_context_acquire(aio_context);
2096
2097 /*
2098 * Notably absent: Proper permission management. This is sad, but it seems
2099 * almost impossible to achieve without changing the semantics and thereby
2100 * limiting the use cases of the qemu-io HMP command.
2101 *
2102 * In an ideal world we would unconditionally create a new BlockBackend for
2103 * qemuio_command(), but we have commands like 'reopen' and want them to
2104 * take effect on the exact BlockBackend whose name the user passed instead
2105 * of just on a temporary copy of it.
2106 *
2107 * Another problem is that deleting the temporary BlockBackend involves
2108 * draining all requests on it first, but some qemu-iotests cases want to
2109 * issue multiple aio_read/write requests and expect them to complete in
2110 * the background while the monitor has already returned.
2111 *
2112 * This is also what prevents us from saving the original permissions and
2113 * restoring them later: We can't revoke permissions until all requests
2114 * have completed, and we don't know when that is nor can we really let
2115 * anything else run before we have revoken them to avoid race conditions.
2116 *
2117 * What happens now is that command() in qemu-io-cmds.c can extend the
2118 * permissions if necessary for the qemu-io command. And they simply stay
2119 * extended, possibly resulting in a read-only guest device keeping write
2120 * permissions. Ugly, but it appears to be the lesser evil.
2121 */
2122 qemuio_command(blk, command);
2123
2124 aio_context_release(aio_context);
2125
2126 fail:
2127 blk_unref(local_blk);
2128 hmp_handle_error(mon, &err);
2129 }
2130
2131 void hmp_object_del(Monitor *mon, const QDict *qdict)
2132 {
2133 const char *id = qdict_get_str(qdict, "id");
2134 Error *err = NULL;
2135
2136 user_creatable_del(id, &err);
2137 hmp_handle_error(mon, &err);
2138 }
2139
2140 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2141 {
2142 Error *err = NULL;
2143 MemdevList *memdev_list = qmp_query_memdev(&err);
2144 MemdevList *m = memdev_list;
2145 Visitor *v;
2146 char *str;
2147
2148 while (m) {
2149 v = string_output_visitor_new(false, &str);
2150 visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2151 monitor_printf(mon, "memory backend: %s\n", m->value->id);
2152 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
2153 monitor_printf(mon, " merge: %s\n",
2154 m->value->merge ? "true" : "false");
2155 monitor_printf(mon, " dump: %s\n",
2156 m->value->dump ? "true" : "false");
2157 monitor_printf(mon, " prealloc: %s\n",
2158 m->value->prealloc ? "true" : "false");
2159 monitor_printf(mon, " policy: %s\n",
2160 HostMemPolicy_lookup[m->value->policy]);
2161 visit_complete(v, &str);
2162 monitor_printf(mon, " host nodes: %s\n", str);
2163
2164 g_free(str);
2165 visit_free(v);
2166 m = m->next;
2167 }
2168
2169 monitor_printf(mon, "\n");
2170
2171 qapi_free_MemdevList(memdev_list);
2172 }
2173
2174 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2175 {
2176 Error *err = NULL;
2177 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2178 MemoryDeviceInfoList *info;
2179 MemoryDeviceInfo *value;
2180 PCDIMMDeviceInfo *di;
2181
2182 for (info = info_list; info; info = info->next) {
2183 value = info->value;
2184
2185 if (value) {
2186 switch (value->type) {
2187 case MEMORY_DEVICE_INFO_KIND_DIMM:
2188 di = value->u.dimm.data;
2189
2190 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2191 MemoryDeviceInfoKind_lookup[value->type],
2192 di->id ? di->id : "");
2193 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
2194 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
2195 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
2196 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
2197 monitor_printf(mon, " memdev: %s\n", di->memdev);
2198 monitor_printf(mon, " hotplugged: %s\n",
2199 di->hotplugged ? "true" : "false");
2200 monitor_printf(mon, " hotpluggable: %s\n",
2201 di->hotpluggable ? "true" : "false");
2202 break;
2203 default:
2204 break;
2205 }
2206 }
2207 }
2208
2209 qapi_free_MemoryDeviceInfoList(info_list);
2210 }
2211
2212 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2213 {
2214 IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2215 IOThreadInfoList *info;
2216 IOThreadInfo *value;
2217
2218 for (info = info_list; info; info = info->next) {
2219 value = info->value;
2220 monitor_printf(mon, "%s:\n", value->id);
2221 monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
2222 monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2223 monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
2224 monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
2225 }
2226
2227 qapi_free_IOThreadInfoList(info_list);
2228 }
2229
2230 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2231 {
2232 const char *path = qdict_get_try_str(qdict, "path");
2233 ObjectPropertyInfoList *list;
2234 Error *err = NULL;
2235
2236 if (path == NULL) {
2237 monitor_printf(mon, "/\n");
2238 return;
2239 }
2240
2241 list = qmp_qom_list(path, &err);
2242 if (err == NULL) {
2243 ObjectPropertyInfoList *start = list;
2244 while (list != NULL) {
2245 ObjectPropertyInfo *value = list->value;
2246
2247 monitor_printf(mon, "%s (%s)\n",
2248 value->name, value->type);
2249 list = list->next;
2250 }
2251 qapi_free_ObjectPropertyInfoList(start);
2252 }
2253 hmp_handle_error(mon, &err);
2254 }
2255
2256 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2257 {
2258 const char *path = qdict_get_str(qdict, "path");
2259 const char *property = qdict_get_str(qdict, "property");
2260 const char *value = qdict_get_str(qdict, "value");
2261 Error *err = NULL;
2262 bool ambiguous = false;
2263 Object *obj;
2264
2265 obj = object_resolve_path(path, &ambiguous);
2266 if (obj == NULL) {
2267 error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2268 "Device '%s' not found", path);
2269 } else {
2270 if (ambiguous) {
2271 monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2272 }
2273 object_property_parse(obj, value, property, &err);
2274 }
2275 hmp_handle_error(mon, &err);
2276 }
2277
2278 void hmp_rocker(Monitor *mon, const QDict *qdict)
2279 {
2280 const char *name = qdict_get_str(qdict, "name");
2281 RockerSwitch *rocker;
2282 Error *err = NULL;
2283
2284 rocker = qmp_query_rocker(name, &err);
2285 if (err != NULL) {
2286 hmp_handle_error(mon, &err);
2287 return;
2288 }
2289
2290 monitor_printf(mon, "name: %s\n", rocker->name);
2291 monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2292 monitor_printf(mon, "ports: %d\n", rocker->ports);
2293
2294 qapi_free_RockerSwitch(rocker);
2295 }
2296
2297 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2298 {
2299 RockerPortList *list, *port;
2300 const char *name = qdict_get_str(qdict, "name");
2301 Error *err = NULL;
2302
2303 list = qmp_query_rocker_ports(name, &err);
2304 if (err != NULL) {
2305 hmp_handle_error(mon, &err);
2306 return;
2307 }
2308
2309 monitor_printf(mon, " ena/ speed/ auto\n");
2310 monitor_printf(mon, " port link duplex neg?\n");
2311
2312 for (port = list; port; port = port->next) {
2313 monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
2314 port->value->name,
2315 port->value->enabled ? port->value->link_up ?
2316 "up" : "down" : "!ena",
2317 port->value->speed == 10000 ? "10G" : "??",
2318 port->value->duplex ? "FD" : "HD",
2319 port->value->autoneg ? "Yes" : "No");
2320 }
2321
2322 qapi_free_RockerPortList(list);
2323 }
2324
2325 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2326 {
2327 RockerOfDpaFlowList *list, *info;
2328 const char *name = qdict_get_str(qdict, "name");
2329 uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2330 Error *err = NULL;
2331
2332 list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2333 if (err != NULL) {
2334 hmp_handle_error(mon, &err);
2335 return;
2336 }
2337
2338 monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2339
2340 for (info = list; info; info = info->next) {
2341 RockerOfDpaFlow *flow = info->value;
2342 RockerOfDpaFlowKey *key = flow->key;
2343 RockerOfDpaFlowMask *mask = flow->mask;
2344 RockerOfDpaFlowAction *action = flow->action;
2345
2346 if (flow->hits) {
2347 monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2348 key->priority, key->tbl_id, flow->hits);
2349 } else {
2350 monitor_printf(mon, "%-4d %-3d ",
2351 key->priority, key->tbl_id);
2352 }
2353
2354 if (key->has_in_pport) {
2355 monitor_printf(mon, " pport %d", key->in_pport);
2356 if (mask->has_in_pport) {
2357 monitor_printf(mon, "(0x%x)", mask->in_pport);
2358 }
2359 }
2360
2361 if (key->has_vlan_id) {
2362 monitor_printf(mon, " vlan %d",
2363 key->vlan_id & VLAN_VID_MASK);
2364 if (mask->has_vlan_id) {
2365 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2366 }
2367 }
2368
2369 if (key->has_tunnel_id) {
2370 monitor_printf(mon, " tunnel %d", key->tunnel_id);
2371 if (mask->has_tunnel_id) {
2372 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2373 }
2374 }
2375
2376 if (key->has_eth_type) {
2377 switch (key->eth_type) {
2378 case 0x0806:
2379 monitor_printf(mon, " ARP");
2380 break;
2381 case 0x0800:
2382 monitor_printf(mon, " IP");
2383 break;
2384 case 0x86dd:
2385 monitor_printf(mon, " IPv6");
2386 break;
2387 case 0x8809:
2388 monitor_printf(mon, " LACP");
2389 break;
2390 case 0x88cc:
2391 monitor_printf(mon, " LLDP");
2392 break;
2393 default:
2394 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2395 break;
2396 }
2397 }
2398
2399 if (key->has_eth_src) {
2400 if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2401 (mask->has_eth_src) &&
2402 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2403 monitor_printf(mon, " src <any mcast/bcast>");
2404 } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2405 (mask->has_eth_src) &&
2406 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2407 monitor_printf(mon, " src <any ucast>");
2408 } else {
2409 monitor_printf(mon, " src %s", key->eth_src);
2410 if (mask->has_eth_src) {
2411 monitor_printf(mon, "(%s)", mask->eth_src);
2412 }
2413 }
2414 }
2415
2416 if (key->has_eth_dst) {
2417 if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2418 (mask->has_eth_dst) &&
2419 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2420 monitor_printf(mon, " dst <any mcast/bcast>");
2421 } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2422 (mask->has_eth_dst) &&
2423 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2424 monitor_printf(mon, " dst <any ucast>");
2425 } else {
2426 monitor_printf(mon, " dst %s", key->eth_dst);
2427 if (mask->has_eth_dst) {
2428 monitor_printf(mon, "(%s)", mask->eth_dst);
2429 }
2430 }
2431 }
2432
2433 if (key->has_ip_proto) {
2434 monitor_printf(mon, " proto %d", key->ip_proto);
2435 if (mask->has_ip_proto) {
2436 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2437 }
2438 }
2439
2440 if (key->has_ip_tos) {
2441 monitor_printf(mon, " TOS %d", key->ip_tos);
2442 if (mask->has_ip_tos) {
2443 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2444 }
2445 }
2446
2447 if (key->has_ip_dst) {
2448 monitor_printf(mon, " dst %s", key->ip_dst);
2449 }
2450
2451 if (action->has_goto_tbl || action->has_group_id ||
2452 action->has_new_vlan_id) {
2453 monitor_printf(mon, " -->");
2454 }
2455
2456 if (action->has_new_vlan_id) {
2457 monitor_printf(mon, " apply new vlan %d",
2458 ntohs(action->new_vlan_id));
2459 }
2460
2461 if (action->has_group_id) {
2462 monitor_printf(mon, " write group 0x%08x", action->group_id);
2463 }
2464
2465 if (action->has_goto_tbl) {
2466 monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2467 }
2468
2469 monitor_printf(mon, "\n");
2470 }
2471
2472 qapi_free_RockerOfDpaFlowList(list);
2473 }
2474
2475 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2476 {
2477 RockerOfDpaGroupList *list, *g;
2478 const char *name = qdict_get_str(qdict, "name");
2479 uint8_t type = qdict_get_try_int(qdict, "type", 9);
2480 Error *err = NULL;
2481 bool set = false;
2482
2483 list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2484 if (err != NULL) {
2485 hmp_handle_error(mon, &err);
2486 return;
2487 }
2488
2489 monitor_printf(mon, "id (decode) --> buckets\n");
2490
2491 for (g = list; g; g = g->next) {
2492 RockerOfDpaGroup *group = g->value;
2493
2494 monitor_printf(mon, "0x%08x", group->id);
2495
2496 monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2497 group->type == 1 ? "L2 rewrite" :
2498 group->type == 2 ? "L3 unicast" :
2499 group->type == 3 ? "L2 multicast" :
2500 group->type == 4 ? "L2 flood" :
2501 group->type == 5 ? "L3 interface" :
2502 group->type == 6 ? "L3 multicast" :
2503 group->type == 7 ? "L3 ECMP" :
2504 group->type == 8 ? "L2 overlay" :
2505 "unknown");
2506
2507 if (group->has_vlan_id) {
2508 monitor_printf(mon, " vlan %d", group->vlan_id);
2509 }
2510
2511 if (group->has_pport) {
2512 monitor_printf(mon, " pport %d", group->pport);
2513 }
2514
2515 if (group->has_index) {
2516 monitor_printf(mon, " index %d", group->index);
2517 }
2518
2519 monitor_printf(mon, ") -->");
2520
2521 if (group->has_set_vlan_id && group->set_vlan_id) {
2522 set = true;
2523 monitor_printf(mon, " set vlan %d",
2524 group->set_vlan_id & VLAN_VID_MASK);
2525 }
2526
2527 if (group->has_set_eth_src) {
2528 if (!set) {
2529 set = true;
2530 monitor_printf(mon, " set");
2531 }
2532 monitor_printf(mon, " src %s", group->set_eth_src);
2533 }
2534
2535 if (group->has_set_eth_dst) {
2536 if (!set) {
2537 set = true;
2538 monitor_printf(mon, " set");
2539 }
2540 monitor_printf(mon, " dst %s", group->set_eth_dst);
2541 }
2542
2543 set = false;
2544
2545 if (group->has_ttl_check && group->ttl_check) {
2546 monitor_printf(mon, " check TTL");
2547 }
2548
2549 if (group->has_group_id && group->group_id) {
2550 monitor_printf(mon, " group id 0x%08x", group->group_id);
2551 }
2552
2553 if (group->has_pop_vlan && group->pop_vlan) {
2554 monitor_printf(mon, " pop vlan");
2555 }
2556
2557 if (group->has_out_pport) {
2558 monitor_printf(mon, " out pport %d", group->out_pport);
2559 }
2560
2561 if (group->has_group_ids) {
2562 struct uint32List *id;
2563
2564 monitor_printf(mon, " groups [");
2565 for (id = group->group_ids; id; id = id->next) {
2566 monitor_printf(mon, "0x%08x", id->value);
2567 if (id->next) {
2568 monitor_printf(mon, ",");
2569 }
2570 }
2571 monitor_printf(mon, "]");
2572 }
2573
2574 monitor_printf(mon, "\n");
2575 }
2576
2577 qapi_free_RockerOfDpaGroupList(list);
2578 }
2579
2580 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2581 {
2582 DumpQueryResult *result = qmp_query_dump(NULL);
2583
2584 assert(result && result->status < DUMP_STATUS__MAX);
2585 monitor_printf(mon, "Status: %s\n", DumpStatus_lookup[result->status]);
2586
2587 if (result->status == DUMP_STATUS_ACTIVE) {
2588 float percent = 0;
2589 assert(result->total != 0);
2590 percent = 100.0 * result->completed / result->total;
2591 monitor_printf(mon, "Finished: %.2f %%\n", percent);
2592 }
2593
2594 qapi_free_DumpQueryResult(result);
2595 }
2596
2597 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2598 {
2599 Error *err = NULL;
2600 HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2601 HotpluggableCPUList *saved = l;
2602 CpuInstanceProperties *c;
2603
2604 if (err != NULL) {
2605 hmp_handle_error(mon, &err);
2606 return;
2607 }
2608
2609 monitor_printf(mon, "Hotpluggable CPUs:\n");
2610 while (l) {
2611 monitor_printf(mon, " type: \"%s\"\n", l->value->type);
2612 monitor_printf(mon, " vcpus_count: \"%" PRIu64 "\"\n",
2613 l->value->vcpus_count);
2614 if (l->value->has_qom_path) {
2615 monitor_printf(mon, " qom_path: \"%s\"\n", l->value->qom_path);
2616 }
2617
2618 c = l->value->props;
2619 monitor_printf(mon, " CPUInstance Properties:\n");
2620 if (c->has_node_id) {
2621 monitor_printf(mon, " node-id: \"%" PRIu64 "\"\n", c->node_id);
2622 }
2623 if (c->has_socket_id) {
2624 monitor_printf(mon, " socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2625 }
2626 if (c->has_core_id) {
2627 monitor_printf(mon, " core-id: \"%" PRIu64 "\"\n", c->core_id);
2628 }
2629 if (c->has_thread_id) {
2630 monitor_printf(mon, " thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2631 }
2632
2633 l = l->next;
2634 }
2635
2636 qapi_free_HotpluggableCPUList(saved);
2637 }
2638
2639 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2640 {
2641 Error *err = NULL;
2642 GuidInfo *info = qmp_query_vm_generation_id(&err);
2643 if (info) {
2644 monitor_printf(mon, "%s\n", info->guid);
2645 }
2646 hmp_handle_error(mon, &err);
2647 qapi_free_GuidInfo(info);
2648 }