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