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