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