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