qmp.c 16.3 KB
Newer Older
A
Anthony Liguori 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * QEMU Management Protocol
 *
 * Copyright IBM, Corp. 2011
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
A
Anthony Liguori 已提交
14 15 16
 */

#include "qemu-common.h"
17
#include "sysemu/sysemu.h"
A
Anthony Liguori 已提交
18
#include "qmp-commands.h"
19
#include "sysemu/char.h"
L
Luiz Capitulino 已提交
20 21
#include "ui/qemu-spice.h"
#include "ui/vnc.h"
22 23
#include "sysemu/kvm.h"
#include "sysemu/arch_init.h"
A
Anthony Liguori 已提交
24
#include "hw/qdev.h"
25
#include "sysemu/blockdev.h"
26
#include "qom/qom-qobject.h"
27 28
#include "qapi/qmp/qobject.h"
#include "qapi/qmp-input-visitor.h"
I
Igor Mammedov 已提交
29
#include "hw/boards.h"
30
#include "qom/object_interfaces.h"
A
Anthony Liguori 已提交
31 32 33 34 35 36 37 38 39 40 41 42

NameInfo *qmp_query_name(Error **errp)
{
    NameInfo *info = g_malloc0(sizeof(*info));

    if (qemu_name) {
        info->has_name = true;
        info->name = g_strdup(qemu_name);
    }

    return info;
}
L
Luiz Capitulino 已提交
43

44
VersionInfo *qmp_query_version(Error **errp)
L
Luiz Capitulino 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
    VersionInfo *info = g_malloc0(sizeof(*info));
    const char *version = QEMU_VERSION;
    char *tmp;

    info->qemu.major = strtol(version, &tmp, 10);
    tmp++;
    info->qemu.minor = strtol(tmp, &tmp, 10);
    tmp++;
    info->qemu.micro = strtol(tmp, &tmp, 10);
    info->package = g_strdup(QEMU_PKGVERSION);

    return info;
}
L
Luiz Capitulino 已提交
59 60 61 62 63 64 65 66 67 68 69

KvmInfo *qmp_query_kvm(Error **errp)
{
    KvmInfo *info = g_malloc0(sizeof(*info));

    info->enabled = kvm_enabled();
    info->present = kvm_available();

    return info;
}

L
Luiz Capitulino 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
UuidInfo *qmp_query_uuid(Error **errp)
{
    UuidInfo *info = g_malloc0(sizeof(*info));
    char uuid[64];

    snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
                   qemu_uuid[14], qemu_uuid[15]);

    info->UUID = g_strdup(uuid);
    return info;
}

85
void qmp_quit(Error **errp)
L
Luiz Capitulino 已提交
86 87 88 89 90
{
    no_shutdown = 0;
    qemu_system_shutdown_request();
}

L
Luiz Capitulino 已提交
91 92
void qmp_stop(Error **errp)
{
93 94 95 96 97
    if (runstate_check(RUN_STATE_INMIGRATE)) {
        autostart = 0;
    } else {
        vm_stop(RUN_STATE_PAUSED);
    }
L
Luiz Capitulino 已提交
98 99
}

L
Luiz Capitulino 已提交
100 101 102 103
void qmp_system_reset(Error **errp)
{
    qemu_system_reset_request();
}
L
Luiz Capitulino 已提交
104 105 106 107 108

void qmp_system_powerdown(Error **erp)
{
    qemu_system_powerdown_request();
}
L
Luiz Capitulino 已提交
109 110 111 112 113

void qmp_cpu(int64_t index, Error **errp)
{
    /* Just do nothing */
}
L
Luiz Capitulino 已提交
114

I
Igor Mammedov 已提交
115 116
void qmp_cpu_add(int64_t id, Error **errp)
{
117 118 119
    MachineClass *mc;

    mc = MACHINE_GET_CLASS(current_machine);
120 121
    if (mc->hot_add_cpu) {
        mc->hot_add_cpu(id, errp);
I
Igor Mammedov 已提交
122 123 124 125 126
    } else {
        error_setg(errp, "Not supported");
    }
}

L
Luiz Capitulino 已提交
127 128 129 130 131 132 133 134 135
#ifndef CONFIG_VNC
/* If VNC support is enabled, the "true" query-vnc command is
   defined in the VNC subsystem */
VncInfo *qmp_query_vnc(Error **errp)
{
    error_set(errp, QERR_FEATURE_DISABLED, "vnc");
    return NULL;
};
#endif
L
Luiz Capitulino 已提交
136 137 138 139 140 141 142 143 144 145 146 147

#ifndef CONFIG_SPICE
/* If SPICE support is enabled, the "true" query-spice command is
   defined in the SPICE subsystem. Also note that we use a small
   trick to maintain query-spice's original behavior, which is not
   to be available in the namespace if SPICE is not compiled in */
SpiceInfo *qmp_query_spice(Error **errp)
{
    error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
    return NULL;
};
#endif
L
Luiz Capitulino 已提交
148 149 150

void qmp_cont(Error **errp)
{
151
    BlockDriverState *bs;
L
Luiz Capitulino 已提交
152

153
    if (runstate_needs_reset()) {
154
        error_setg(errp, "Resetting the Virtual Machine is required");
L
Luiz Capitulino 已提交
155
        return;
156 157
    } else if (runstate_check(RUN_STATE_SUSPENDED)) {
        return;
L
Luiz Capitulino 已提交
158 159
    }

160 161 162 163 164 165 166 167 168 169
    for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
        bdrv_iostatus_reset(bs);
    }
    for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
        if (bdrv_key_required(bs)) {
            error_set(errp, QERR_DEVICE_ENCRYPTED,
                      bdrv_get_device_name(bs),
                      bdrv_get_encrypted_filename(bs));
            return;
        }
L
Luiz Capitulino 已提交
170 171
    }

172 173 174 175 176
    if (runstate_check(RUN_STATE_INMIGRATE)) {
        autostart = 1;
    } else {
        vm_start();
    }
L
Luiz Capitulino 已提交
177
}
A
Anthony Liguori 已提交
178

179 180 181 182 183
void qmp_system_wakeup(Error **errp)
{
    qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
}

184
ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
A
Anthony Liguori 已提交
185
{
186
    Object *obj;
A
Anthony Liguori 已提交
187
    bool ambiguous = false;
188 189
    ObjectPropertyInfoList *props = NULL;
    ObjectProperty *prop;
A
Anthony Liguori 已提交
190

191 192
    obj = object_resolve_path(path, &ambiguous);
    if (obj == NULL) {
193 194 195 196 197
        if (ambiguous) {
            error_setg(errp, "Path '%s' is ambiguous", path);
        } else {
            error_set(errp, QERR_DEVICE_NOT_FOUND, path);
        }
A
Anthony Liguori 已提交
198 199 200
        return NULL;
    }

201 202
    QTAILQ_FOREACH(prop, &obj->properties, node) {
        ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
A
Anthony Liguori 已提交
203

204
        entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
A
Anthony Liguori 已提交
205 206 207 208 209 210 211 212 213
        entry->next = props;
        props = entry;

        entry->value->name = g_strdup(prop->name);
        entry->value->type = g_strdup(prop->type);
    }

    return props;
}
214 215 216 217 218 219 220 221

/* FIXME: teach qapi about how to pass through Visitors */
int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
{
    const char *path = qdict_get_str(qdict, "path");
    const char *property = qdict_get_str(qdict, "property");
    QObject *value = qdict_get(qdict, "value");
    Error *local_err = NULL;
222
    Object *obj;
223

224 225
    obj = object_resolve_path(path, NULL);
    if (!obj) {
226 227 228 229
        error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
        goto out;
    }

230
    object_property_set_qobject(obj, value, property, &local_err);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

out:
    if (local_err) {
        qerror_report_err(local_err);
        error_free(local_err);
        return -1;
    }

    return 0;
}

int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
{
    const char *path = qdict_get_str(qdict, "path");
    const char *property = qdict_get_str(qdict, "property");
    Error *local_err = NULL;
247
    Object *obj;
248

249 250
    obj = object_resolve_path(path, NULL);
    if (!obj) {
251 252 253 254
        error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
        goto out;
    }

255
    *ret = object_property_get_qobject(obj, property, &local_err);
256 257 258 259 260 261 262 263 264 265

out:
    if (local_err) {
        qerror_report_err(local_err);
        error_free(local_err);
        return -1;
    }

    return 0;
}
L
Luiz Capitulino 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

void qmp_set_password(const char *protocol, const char *password,
                      bool has_connected, const char *connected, Error **errp)
{
    int disconnect_if_connected = 0;
    int fail_if_connected = 0;
    int rc;

    if (has_connected) {
        if (strcmp(connected, "fail") == 0) {
            fail_if_connected = 1;
        } else if (strcmp(connected, "disconnect") == 0) {
            disconnect_if_connected = 1;
        } else if (strcmp(connected, "keep") == 0) {
            /* nothing */
        } else {
            error_set(errp, QERR_INVALID_PARAMETER, "connected");
            return;
        }
    }

    if (strcmp(protocol, "spice") == 0) {
        if (!using_spice) {
            /* correct one? spice isn't a device ,,, */
            error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
            return;
        }
        rc = qemu_spice_set_passwd(password, fail_if_connected,
                                   disconnect_if_connected);
        if (rc != 0) {
            error_set(errp, QERR_SET_PASSWD_FAILED);
        }
        return;
    }

    if (strcmp(protocol, "vnc") == 0) {
        if (fail_if_connected || disconnect_if_connected) {
            /* vnc supports "connected=keep" only */
            error_set(errp, QERR_INVALID_PARAMETER, "connected");
            return;
        }
        /* Note that setting an empty password will not disable login through
         * this interface. */
        rc = vnc_display_password(NULL, password);
        if (rc < 0) {
            error_set(errp, QERR_SET_PASSWD_FAILED);
        }
        return;
    }

    error_set(errp, QERR_INVALID_PARAMETER, "protocol");
}
L
Luiz Capitulino 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

void qmp_expire_password(const char *protocol, const char *whenstr,
                         Error **errp)
{
    time_t when;
    int rc;

    if (strcmp(whenstr, "now") == 0) {
        when = 0;
    } else if (strcmp(whenstr, "never") == 0) {
        when = TIME_MAX;
    } else if (whenstr[0] == '+') {
        when = time(NULL) + strtoull(whenstr+1, NULL, 10);
    } else {
        when = strtoull(whenstr, NULL, 10);
    }

    if (strcmp(protocol, "spice") == 0) {
        if (!using_spice) {
            /* correct one? spice isn't a device ,,, */
            error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
            return;
        }
        rc = qemu_spice_set_pw_expire(when);
        if (rc != 0) {
            error_set(errp, QERR_SET_PASSWD_FAILED);
        }
        return;
    }

    if (strcmp(protocol, "vnc") == 0) {
        rc = vnc_display_pw_expire(NULL, when);
        if (rc != 0) {
            error_set(errp, QERR_SET_PASSWD_FAILED);
        }
        return;
    }

    error_set(errp, QERR_INVALID_PARAMETER, "protocol");
}
358

L
Luiz Capitulino 已提交
359
#ifdef CONFIG_VNC
360 361 362 363 364 365
void qmp_change_vnc_password(const char *password, Error **errp)
{
    if (vnc_display_password(NULL, password) < 0) {
        error_set(errp, QERR_SET_PASSWD_FAILED);
    }
}
L
Luiz Capitulino 已提交
366

367
static void qmp_change_vnc_listen(const char *target, Error **errp)
L
Luiz Capitulino 已提交
368
{
369
    vnc_display_open(NULL, target, errp);
L
Luiz Capitulino 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
}

static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
                           Error **errp)
{
    if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
        if (!has_arg) {
            error_set(errp, QERR_MISSING_PARAMETER, "password");
        } else {
            qmp_change_vnc_password(arg, errp);
        }
    } else {
        qmp_change_vnc_listen(target, errp);
    }
}
#else
void qmp_change_vnc_password(const char *password, Error **errp)
{
    error_set(errp, QERR_FEATURE_DISABLED, "vnc");
}
static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
                           Error **errp)
{
    error_set(errp, QERR_FEATURE_DISABLED, "vnc");
}
#endif /* !CONFIG_VNC */

void qmp_change(const char *device, const char *target,
398
                bool has_arg, const char *arg, Error **errp)
L
Luiz Capitulino 已提交
399 400
{
    if (strcmp(device, "vnc") == 0) {
401
        qmp_change_vnc(target, has_arg, arg, errp);
L
Luiz Capitulino 已提交
402
    } else {
403
        qmp_change_blockdev(device, target, arg, errp);
L
Luiz Capitulino 已提交
404 405
    }
}
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

static void qom_list_types_tramp(ObjectClass *klass, void *data)
{
    ObjectTypeInfoList *e, **pret = data;
    ObjectTypeInfo *info;

    info = g_malloc0(sizeof(*info));
    info->name = g_strdup(object_class_get_name(klass));

    e = g_malloc0(sizeof(*e));
    e->value = info;
    e->next = *pret;
    *pret = e;
}

ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
                                       const char *implements,
                                       bool has_abstract,
                                       bool abstract,
                                       Error **errp)
{
    ObjectTypeInfoList *ret = NULL;

    object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);

    return ret;
}
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482

DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
                                                   Error **errp)
{
    ObjectClass *klass;
    Property *prop;
    DevicePropertyInfoList *prop_list = NULL;

    klass = object_class_by_name(typename);
    if (klass == NULL) {
        error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
        return NULL;
    }

    klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
    if (klass == NULL) {
        error_set(errp, QERR_INVALID_PARAMETER_VALUE,
                  "name", TYPE_DEVICE);
        return NULL;
    }

    do {
        for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
            DevicePropertyInfoList *entry;
            DevicePropertyInfo *info;

            /*
             * TODO Properties without a parser are just for dirty hacks.
             * qdev_prop_ptr is the only such PropertyInfo.  It's marked
             * for removal.  This conditional should be removed along with
             * it.
             */
            if (!prop->info->set) {
                continue;           /* no way to set it, don't show */
            }

            info = g_malloc0(sizeof(*info));
            info->name = g_strdup(prop->name);
            info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);

            entry = g_malloc0(sizeof(*entry));
            entry->value = info;
            entry->next = prop_list;
            prop_list = entry;
        }
        klass = object_class_get_parent(klass);
    } while (klass != object_class_by_name(TYPE_DEVICE));

    return prop_list;
}
483

484 485 486 487 488
CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
{
    return arch_query_cpu_definitions(errp);
}

L
Luiz Capitulino 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
void qmp_add_client(const char *protocol, const char *fdname,
                    bool has_skipauth, bool skipauth, bool has_tls, bool tls,
                    Error **errp)
{
    CharDriverState *s;
    int fd;

    fd = monitor_get_fd(cur_mon, fdname, errp);
    if (fd < 0) {
        return;
    }

    if (strcmp(protocol, "spice") == 0) {
        if (!using_spice) {
            error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
            close(fd);
            return;
        }
        skipauth = has_skipauth ? skipauth : false;
        tls = has_tls ? tls : false;
        if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
            error_setg(errp, "spice failed to add client");
            close(fd);
        }
        return;
#ifdef CONFIG_VNC
    } else if (strcmp(protocol, "vnc") == 0) {
        skipauth = has_skipauth ? skipauth : false;
        vnc_display_add_client(NULL, fd, skipauth);
        return;
#endif
    } else if ((s = qemu_chr_find(protocol)) != NULL) {
        if (qemu_chr_add_client(s, fd) < 0) {
            error_setg(errp, "failed to add client");
            close(fd);
            return;
        }
        return;
    }

    error_setg(errp, "protocol '%s' is invalid", protocol);
    close(fd);
}
532

533 534 535 536
void object_add(const char *type, const char *id, const QDict *qdict,
                Visitor *v, Error **errp)
{
    Object *obj;
537
    ObjectClass *klass;
538 539 540
    const QDictEntry *e;
    Error *local_err = NULL;

541 542
    klass = object_class_by_name(type);
    if (!klass) {
543
        error_setg(errp, "invalid object type: %s", type);
544 545 546
        return;
    }

547 548 549 550 551 552 553 554 555 556 557
    if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
        error_setg(errp, "object type '%s' isn't supported by object-add",
                   type);
        return;
    }

    if (object_class_is_abstract(klass)) {
        error_setg(errp, "object type '%s' is abstract", type);
        return;
    }

558 559 560 561 562
    obj = object_new(type);
    if (qdict) {
        for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
            object_property_set(obj, v, e->key, &local_err);
            if (local_err) {
563
                goto out;
564 565 566 567
            }
        }
    }

568 569
    object_property_add_child(container_get(object_get_root(), "/objects"),
                              id, obj, &local_err);
570 571 572 573
    if (local_err) {
        goto out;
    }

574 575 576 577 578 579
    user_creatable_complete(obj, &local_err);
    if (local_err) {
        object_property_del(container_get(object_get_root(), "/objects"),
                            id, &error_abort);
        goto out;
    }
580 581 582 583
out:
    if (local_err) {
        error_propagate(errp, local_err);
    }
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    object_unref(obj);
}

int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
{
    const char *type = qdict_get_str(qdict, "qom-type");
    const char *id = qdict_get_str(qdict, "id");
    QObject *props = qdict_get(qdict, "props");
    const QDict *pdict = NULL;
    Error *local_err = NULL;
    QmpInputVisitor *qiv;

    if (props) {
        pdict = qobject_to_qdict(props);
        if (!pdict) {
            error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
            goto out;
        }
    }

    qiv = qmp_input_visitor_new(props);
    object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
    qmp_input_visitor_cleanup(qiv);

out:
    if (local_err) {
        qerror_report_err(local_err);
        error_free(local_err);
        return -1;
    }

    return 0;
}

618 619 620 621 622 623 624 625 626 627 628 629 630
void qmp_object_del(const char *id, Error **errp)
{
    Object *container;
    Object *obj;

    container = container_get(object_get_root(), "/objects");
    obj = object_resolve_path_component(container, id);
    if (!obj) {
        error_setg(errp, "object id not found");
        return;
    }
    object_unparent(obj);
}