qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

qmp-cmds.c (16350B)


      1 /*
      2  * QEMU Management Protocol commands
      3  *
      4  * Copyright IBM, Corp. 2011
      5  *
      6  * Authors:
      7  *  Anthony Liguori   <aliguori@us.ibm.com>
      8  *
      9  * This work is licensed under the terms of the GNU GPL, version 2.  See
     10  * the COPYING file in the top-level directory.
     11  *
     12  * Contributions after 2012-01-13 are licensed under the terms of the
     13  * GNU GPL, version 2 or (at your option) any later version.
     14  */
     15 
     16 #include "qemu/osdep.h"
     17 #include "qemu/cutils.h"
     18 #include "qemu/option.h"
     19 #include "monitor/monitor.h"
     20 #include "sysemu/sysemu.h"
     21 #include "qemu/config-file.h"
     22 #include "qemu/uuid.h"
     23 #include "chardev/char.h"
     24 #include "ui/qemu-spice.h"
     25 #include "ui/console.h"
     26 #include "ui/dbus-display.h"
     27 #include "sysemu/kvm.h"
     28 #include "sysemu/runstate.h"
     29 #include "sysemu/runstate-action.h"
     30 #include "sysemu/blockdev.h"
     31 #include "sysemu/block-backend.h"
     32 #include "qapi/error.h"
     33 #include "qapi/qapi-commands-acpi.h"
     34 #include "qapi/qapi-commands-block.h"
     35 #include "qapi/qapi-commands-control.h"
     36 #include "qapi/qapi-commands-machine.h"
     37 #include "qapi/qapi-commands-misc.h"
     38 #include "qapi/qapi-commands-stats.h"
     39 #include "qapi/qapi-commands-ui.h"
     40 #include "qapi/type-helpers.h"
     41 #include "qapi/qmp/qerror.h"
     42 #include "exec/ramlist.h"
     43 #include "hw/mem/memory-device.h"
     44 #include "hw/acpi/acpi_dev_interface.h"
     45 #include "hw/intc/intc.h"
     46 #include "hw/rdma/rdma.h"
     47 #include "monitor/stats.h"
     48 
     49 NameInfo *qmp_query_name(Error **errp)
     50 {
     51     NameInfo *info = g_malloc0(sizeof(*info));
     52 
     53     if (qemu_name) {
     54         info->has_name = true;
     55         info->name = g_strdup(qemu_name);
     56     }
     57 
     58     return info;
     59 }
     60 
     61 KvmInfo *qmp_query_kvm(Error **errp)
     62 {
     63     KvmInfo *info = g_malloc0(sizeof(*info));
     64 
     65     info->enabled = kvm_enabled();
     66     info->present = accel_find("kvm");
     67 
     68     return info;
     69 }
     70 
     71 UuidInfo *qmp_query_uuid(Error **errp)
     72 {
     73     UuidInfo *info = g_malloc0(sizeof(*info));
     74 
     75     info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
     76     return info;
     77 }
     78 
     79 void qmp_quit(Error **errp)
     80 {
     81     shutdown_action = SHUTDOWN_ACTION_POWEROFF;
     82     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
     83 }
     84 
     85 void qmp_stop(Error **errp)
     86 {
     87     /* if there is a dump in background, we should wait until the dump
     88      * finished */
     89     if (qemu_system_dump_in_progress()) {
     90         error_setg(errp, "There is a dump in process, please wait.");
     91         return;
     92     }
     93 
     94     if (runstate_check(RUN_STATE_INMIGRATE)) {
     95         autostart = 0;
     96     } else {
     97         vm_stop(RUN_STATE_PAUSED);
     98     }
     99 }
    100 
    101 void qmp_system_reset(Error **errp)
    102 {
    103     qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
    104 }
    105 
    106 void qmp_system_powerdown(Error **errp)
    107 {
    108     qemu_system_powerdown_request();
    109 }
    110 
    111 void qmp_cont(Error **errp)
    112 {
    113     BlockBackend *blk;
    114     BlockJob *job;
    115     Error *local_err = NULL;
    116 
    117     /* if there is a dump in background, we should wait until the dump
    118      * finished */
    119     if (qemu_system_dump_in_progress()) {
    120         error_setg(errp, "There is a dump in process, please wait.");
    121         return;
    122     }
    123 
    124     if (runstate_needs_reset()) {
    125         error_setg(errp, "Resetting the Virtual Machine is required");
    126         return;
    127     } else if (runstate_check(RUN_STATE_SUSPENDED)) {
    128         return;
    129     } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
    130         error_setg(errp, "Migration is not finalized yet");
    131         return;
    132     }
    133 
    134     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
    135         blk_iostatus_reset(blk);
    136     }
    137 
    138     WITH_JOB_LOCK_GUARD() {
    139         for (job = block_job_next_locked(NULL); job;
    140              job = block_job_next_locked(job)) {
    141             block_job_iostatus_reset_locked(job);
    142         }
    143     }
    144 
    145     /* Continuing after completed migration. Images have been inactivated to
    146      * allow the destination to take control. Need to get control back now.
    147      *
    148      * If there are no inactive block nodes (e.g. because the VM was just
    149      * paused rather than completing a migration), bdrv_inactivate_all() simply
    150      * doesn't do anything. */
    151     bdrv_activate_all(&local_err);
    152     if (local_err) {
    153         error_propagate(errp, local_err);
    154         return;
    155     }
    156 
    157     if (runstate_check(RUN_STATE_INMIGRATE)) {
    158         autostart = 1;
    159     } else {
    160         vm_start();
    161     }
    162 }
    163 
    164 void qmp_system_wakeup(Error **errp)
    165 {
    166     if (!qemu_wakeup_suspend_enabled()) {
    167         error_setg(errp,
    168                    "wake-up from suspend is not supported by this guest");
    169         return;
    170     }
    171 
    172     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
    173 }
    174 
    175 void qmp_set_password(SetPasswordOptions *opts, Error **errp)
    176 {
    177     int rc;
    178 
    179     if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
    180         if (!qemu_using_spice(errp)) {
    181             return;
    182         }
    183         rc = qemu_spice.set_passwd(opts->password,
    184                 opts->connected == SET_PASSWORD_ACTION_FAIL,
    185                 opts->connected == SET_PASSWORD_ACTION_DISCONNECT);
    186     } else {
    187         assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
    188         if (opts->connected != SET_PASSWORD_ACTION_KEEP) {
    189             /* vnc supports "connected=keep" only */
    190             error_setg(errp, QERR_INVALID_PARAMETER, "connected");
    191             return;
    192         }
    193         /* Note that setting an empty password will not disable login through
    194          * this interface. */
    195         rc = vnc_display_password(opts->u.vnc.display, opts->password);
    196     }
    197 
    198     if (rc != 0) {
    199         error_setg(errp, "Could not set password");
    200     }
    201 }
    202 
    203 void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
    204 {
    205     time_t when;
    206     int rc;
    207     const char *whenstr = opts->time;
    208 
    209     if (strcmp(whenstr, "now") == 0) {
    210         when = 0;
    211     } else if (strcmp(whenstr, "never") == 0) {
    212         when = TIME_MAX;
    213     } else if (whenstr[0] == '+') {
    214         when = time(NULL) + strtoull(whenstr+1, NULL, 10);
    215     } else {
    216         when = strtoull(whenstr, NULL, 10);
    217     }
    218 
    219     if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
    220         if (!qemu_using_spice(errp)) {
    221             return;
    222         }
    223         rc = qemu_spice.set_pw_expire(when);
    224     } else {
    225         assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
    226         rc = vnc_display_pw_expire(opts->u.vnc.display, when);
    227     }
    228 
    229     if (rc != 0) {
    230         error_setg(errp, "Could not set password expire time");
    231     }
    232 }
    233 
    234 #ifdef CONFIG_VNC
    235 void qmp_change_vnc_password(const char *password, Error **errp)
    236 {
    237     if (vnc_display_password(NULL, password) < 0) {
    238         error_setg(errp, "Could not set password");
    239     }
    240 }
    241 #endif
    242 
    243 void qmp_add_client(const char *protocol, const char *fdname,
    244                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
    245                     Error **errp)
    246 {
    247     Chardev *s;
    248     int fd;
    249 
    250     fd = monitor_get_fd(monitor_cur(), fdname, errp);
    251     if (fd < 0) {
    252         return;
    253     }
    254 
    255     if (strcmp(protocol, "spice") == 0) {
    256         if (!qemu_using_spice(errp)) {
    257             close(fd);
    258             return;
    259         }
    260         skipauth = has_skipauth ? skipauth : false;
    261         tls = has_tls ? tls : false;
    262         if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
    263             error_setg(errp, "spice failed to add client");
    264             close(fd);
    265         }
    266         return;
    267 #ifdef CONFIG_VNC
    268     } else if (strcmp(protocol, "vnc") == 0) {
    269         skipauth = has_skipauth ? skipauth : false;
    270         vnc_display_add_client(NULL, fd, skipauth);
    271         return;
    272 #endif
    273 #ifdef CONFIG_DBUS_DISPLAY
    274     } else if (strcmp(protocol, "@dbus-display") == 0) {
    275         if (!qemu_using_dbus_display(errp)) {
    276             close(fd);
    277             return;
    278         }
    279         if (!qemu_dbus_display.add_client(fd, errp)) {
    280             close(fd);
    281             return;
    282         }
    283         return;
    284 #endif
    285     } else if ((s = qemu_chr_find(protocol)) != NULL) {
    286         if (qemu_chr_add_client(s, fd) < 0) {
    287             error_setg(errp, "failed to add client");
    288             close(fd);
    289             return;
    290         }
    291         return;
    292     }
    293 
    294     error_setg(errp, "protocol '%s' is invalid", protocol);
    295     close(fd);
    296 }
    297 
    298 
    299 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
    300 {
    301     return qmp_memory_device_list();
    302 }
    303 
    304 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
    305 {
    306     bool ambig;
    307     ACPIOSTInfoList *head = NULL;
    308     ACPIOSTInfoList **prev = &head;
    309     Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
    310 
    311     if (obj) {
    312         AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
    313         AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
    314 
    315         adevc->ospm_status(adev, &prev);
    316     } else {
    317         error_setg(errp, "command is not supported, missing ACPI device");
    318     }
    319 
    320     return head;
    321 }
    322 
    323 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
    324 {
    325     MemoryInfo *mem_info = g_new0(MemoryInfo, 1);
    326     MachineState *ms = MACHINE(qdev_get_machine());
    327 
    328     mem_info->base_memory = ms->ram_size;
    329 
    330     mem_info->plugged_memory = get_plugged_memory_size();
    331     mem_info->has_plugged_memory =
    332         mem_info->plugged_memory != (uint64_t)-1;
    333 
    334     return mem_info;
    335 }
    336 
    337 void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
    338 {
    339     switch (arg->type) {
    340     case DISPLAY_RELOAD_TYPE_VNC:
    341 #ifdef CONFIG_VNC
    342         if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
    343             vnc_display_reload_certs(NULL, errp);
    344         }
    345 #else
    346         error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
    347 #endif
    348         break;
    349     default:
    350         abort();
    351     }
    352 }
    353 
    354 void qmp_display_update(DisplayUpdateOptions *arg, Error **errp)
    355 {
    356     switch (arg->type) {
    357     case DISPLAY_UPDATE_TYPE_VNC:
    358 #ifdef CONFIG_VNC
    359         vnc_display_update(&arg->u.vnc, errp);
    360 #else
    361         error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
    362 #endif
    363         break;
    364     default:
    365         abort();
    366     }
    367 }
    368 
    369 static int qmp_x_query_rdma_foreach(Object *obj, void *opaque)
    370 {
    371     RdmaProvider *rdma;
    372     RdmaProviderClass *k;
    373     GString *buf = opaque;
    374 
    375     if (object_dynamic_cast(obj, INTERFACE_RDMA_PROVIDER)) {
    376         rdma = RDMA_PROVIDER(obj);
    377         k = RDMA_PROVIDER_GET_CLASS(obj);
    378         if (k->format_statistics) {
    379             k->format_statistics(rdma, buf);
    380         } else {
    381             g_string_append_printf(buf,
    382                                    "RDMA statistics not available for %s.\n",
    383                                    object_get_typename(obj));
    384         }
    385     }
    386 
    387     return 0;
    388 }
    389 
    390 HumanReadableText *qmp_x_query_rdma(Error **errp)
    391 {
    392     g_autoptr(GString) buf = g_string_new("");
    393 
    394     object_child_foreach_recursive(object_get_root(),
    395                                    qmp_x_query_rdma_foreach, buf);
    396 
    397     return human_readable_text_from_str(buf);
    398 }
    399 
    400 HumanReadableText *qmp_x_query_ramblock(Error **errp)
    401 {
    402     g_autoptr(GString) buf = ram_block_format();
    403 
    404     return human_readable_text_from_str(buf);
    405 }
    406 
    407 static int qmp_x_query_irq_foreach(Object *obj, void *opaque)
    408 {
    409     InterruptStatsProvider *intc;
    410     InterruptStatsProviderClass *k;
    411     GString *buf = opaque;
    412 
    413     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
    414         intc = INTERRUPT_STATS_PROVIDER(obj);
    415         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
    416         uint64_t *irq_counts;
    417         unsigned int nb_irqs, i;
    418         if (k->get_statistics &&
    419             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
    420             if (nb_irqs > 0) {
    421                 g_string_append_printf(buf, "IRQ statistics for %s:\n",
    422                                        object_get_typename(obj));
    423                 for (i = 0; i < nb_irqs; i++) {
    424                     if (irq_counts[i] > 0) {
    425                         g_string_append_printf(buf, "%2d: %" PRId64 "\n", i,
    426                                                irq_counts[i]);
    427                     }
    428                 }
    429             }
    430         } else {
    431             g_string_append_printf(buf,
    432                                    "IRQ statistics not available for %s.\n",
    433                                    object_get_typename(obj));
    434         }
    435     }
    436 
    437     return 0;
    438 }
    439 
    440 HumanReadableText *qmp_x_query_irq(Error **errp)
    441 {
    442     g_autoptr(GString) buf = g_string_new("");
    443 
    444     object_child_foreach_recursive(object_get_root(),
    445                                    qmp_x_query_irq_foreach, buf);
    446 
    447     return human_readable_text_from_str(buf);
    448 }
    449 
    450 typedef struct StatsCallbacks {
    451     StatsProvider provider;
    452     StatRetrieveFunc *stats_cb;
    453     SchemaRetrieveFunc *schemas_cb;
    454     QTAILQ_ENTRY(StatsCallbacks) next;
    455 } StatsCallbacks;
    456 
    457 static QTAILQ_HEAD(, StatsCallbacks) stats_callbacks =
    458     QTAILQ_HEAD_INITIALIZER(stats_callbacks);
    459 
    460 void add_stats_callbacks(StatsProvider provider,
    461                          StatRetrieveFunc *stats_fn,
    462                          SchemaRetrieveFunc *schemas_fn)
    463 {
    464     StatsCallbacks *entry = g_new(StatsCallbacks, 1);
    465     entry->provider = provider;
    466     entry->stats_cb = stats_fn;
    467     entry->schemas_cb = schemas_fn;
    468 
    469     QTAILQ_INSERT_TAIL(&stats_callbacks, entry, next);
    470 }
    471 
    472 static bool invoke_stats_cb(StatsCallbacks *entry,
    473                             StatsResultList **stats_results,
    474                             StatsFilter *filter, StatsRequest *request,
    475                             Error **errp)
    476 {
    477     strList *targets = NULL;
    478     strList *names = NULL;
    479     ERRP_GUARD();
    480 
    481     if (request) {
    482         if (request->provider != entry->provider) {
    483             return true;
    484         }
    485         if (request->has_names && !request->names) {
    486             return true;
    487         }
    488         names = request->has_names ? request->names : NULL;
    489     }
    490 
    491     switch (filter->target) {
    492     case STATS_TARGET_VM:
    493         break;
    494     case STATS_TARGET_VCPU:
    495         if (filter->u.vcpu.has_vcpus) {
    496             if (!filter->u.vcpu.vcpus) {
    497                 /* No targets allowed?  Return no statistics.  */
    498                 return true;
    499             }
    500             targets = filter->u.vcpu.vcpus;
    501         }
    502         break;
    503     default:
    504         abort();
    505     }
    506 
    507     entry->stats_cb(stats_results, filter->target, names, targets, errp);
    508     if (*errp) {
    509         qapi_free_StatsResultList(*stats_results);
    510         *stats_results = NULL;
    511         return false;
    512     }
    513     return true;
    514 }
    515 
    516 StatsResultList *qmp_query_stats(StatsFilter *filter, Error **errp)
    517 {
    518     StatsResultList *stats_results = NULL;
    519     StatsCallbacks *entry;
    520     StatsRequestList *request;
    521 
    522     QTAILQ_FOREACH(entry, &stats_callbacks, next) {
    523         if (filter->has_providers) {
    524             for (request = filter->providers; request; request = request->next) {
    525                 if (!invoke_stats_cb(entry, &stats_results, filter,
    526                                      request->value, errp)) {
    527                     break;
    528                 }
    529             }
    530         } else {
    531             if (!invoke_stats_cb(entry, &stats_results, filter, NULL, errp)) {
    532                 break;
    533             }
    534         }
    535     }
    536 
    537     return stats_results;
    538 }
    539 
    540 StatsSchemaList *qmp_query_stats_schemas(bool has_provider,
    541                                          StatsProvider provider,
    542                                          Error **errp)
    543 {
    544     StatsSchemaList *stats_results = NULL;
    545     StatsCallbacks *entry;
    546     ERRP_GUARD();
    547 
    548     QTAILQ_FOREACH(entry, &stats_callbacks, next) {
    549         if (!has_provider || provider == entry->provider) {
    550             entry->schemas_cb(&stats_results, errp);
    551             if (*errp) {
    552                 qapi_free_StatsSchemaList(stats_results);
    553                 return NULL;
    554             }
    555         }
    556     }
    557 
    558     return stats_results;
    559 }
    560 
    561 void add_stats_entry(StatsResultList **stats_results, StatsProvider provider,
    562                      const char *qom_path, StatsList *stats_list)
    563 {
    564     StatsResult *entry = g_new0(StatsResult, 1);
    565 
    566     entry->provider = provider;
    567     if (qom_path) {
    568         entry->has_qom_path = true;
    569         entry->qom_path = g_strdup(qom_path);
    570     }
    571     entry->stats = stats_list;
    572 
    573     QAPI_LIST_PREPEND(*stats_results, entry);
    574 }
    575 
    576 void add_stats_schema(StatsSchemaList **schema_results,
    577                       StatsProvider provider, StatsTarget target,
    578                       StatsSchemaValueList *stats_list)
    579 {
    580     StatsSchema *entry = g_new0(StatsSchema, 1);
    581 
    582     entry->provider = provider;
    583     entry->target = target;
    584     entry->stats = stats_list;
    585     QAPI_LIST_PREPEND(*schema_results, entry);
    586 }
    587 
    588 bool apply_str_list_filter(const char *string, strList *list)
    589 {
    590     strList *str_list = NULL;
    591 
    592     if (!list) {
    593         return true;
    594     }
    595     for (str_list = list; str_list; str_list = str_list->next) {
    596         if (g_str_equal(string, str_list->value)) {
    597             return true;
    598         }
    599     }
    600     return false;
    601 }