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