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