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