2 * QEMU Management Protocol
4 * Copyright IBM, Corp. 2011
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "monitor/monitor.h"
19 #include "sysemu/sysemu.h"
20 #include "qmp-commands.h"
21 #include "sysemu/char.h"
22 #include "ui/qemu-spice.h"
24 #include "sysemu/kvm.h"
25 #include "sysemu/arch_init.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/block-backend.h"
29 #include "qom/qom-qobject.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qobject.h"
32 #include "qapi/qmp-input-visitor.h"
33 #include "hw/boards.h"
34 #include "qom/object_interfaces.h"
35 #include "hw/mem/pc-dimm.h"
36 #include "hw/acpi/acpi_dev_interface.h"
38 NameInfo *qmp_query_name(Error **errp)
40 NameInfo *info = g_malloc0(sizeof(*info));
43 info->has_name = true;
44 info->name = g_strdup(qemu_name);
50 VersionInfo *qmp_query_version(Error **errp)
52 VersionInfo *info = g_new0(VersionInfo, 1);
53 const char *version = QEMU_VERSION;
57 info->qemu = g_new0(VersionTriple, 1);
58 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
62 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
66 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
68 info->package = g_strdup(QEMU_PKGVERSION);
73 KvmInfo *qmp_query_kvm(Error **errp)
75 KvmInfo *info = g_malloc0(sizeof(*info));
77 info->enabled = kvm_enabled();
78 info->present = kvm_available();
83 UuidInfo *qmp_query_uuid(Error **errp)
85 UuidInfo *info = g_malloc0(sizeof(*info));
88 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
89 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
90 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
91 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
92 qemu_uuid[14], qemu_uuid[15]);
94 info->UUID = g_strdup(uuid);
98 void qmp_quit(Error **errp)
101 qemu_system_shutdown_request();
104 void qmp_stop(Error **errp)
106 if (runstate_check(RUN_STATE_INMIGRATE)) {
109 vm_stop(RUN_STATE_PAUSED);
113 void qmp_system_reset(Error **errp)
115 qemu_system_reset_request();
118 void qmp_system_powerdown(Error **erp)
120 qemu_system_powerdown_request();
123 void qmp_cpu(int64_t index, Error **errp)
125 /* Just do nothing */
128 void qmp_cpu_add(int64_t id, Error **errp)
132 mc = MACHINE_GET_CLASS(current_machine);
133 if (mc->hot_add_cpu) {
134 mc->hot_add_cpu(id, errp);
136 error_setg(errp, "Not supported");
141 /* If VNC support is enabled, the "true" query-vnc command is
142 defined in the VNC subsystem */
143 VncInfo *qmp_query_vnc(Error **errp)
145 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
149 VncInfo2List *qmp_query_vnc_servers(Error **errp)
151 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
158 * qmp-commands.hx ensures that QMP command query-spice exists only
159 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
160 * result. However, the QAPI schema is blissfully unaware of that,
161 * and the QAPI code generator happily generates a dead
162 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
163 * one, or else linking fails. FIXME Educate the QAPI schema on
166 SpiceInfo *qmp_query_spice(Error **errp)
172 void qmp_cont(Error **errp)
174 Error *local_err = NULL;
176 BlockDriverState *bs;
178 if (runstate_needs_reset()) {
179 error_setg(errp, "Resetting the Virtual Machine is required");
181 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
185 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
186 blk_iostatus_reset(blk);
188 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
189 bdrv_add_key(bs, NULL, &local_err);
191 error_propagate(errp, local_err);
196 /* Continuing after completed migration. Images have been inactivated to
197 * allow the destination to take control. Need to get control back now. */
198 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
199 runstate_check(RUN_STATE_POSTMIGRATE))
201 bdrv_invalidate_cache_all(&local_err);
203 error_propagate(errp, local_err);
208 if (runstate_check(RUN_STATE_INMIGRATE)) {
215 void qmp_system_wakeup(Error **errp)
217 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
220 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
223 bool ambiguous = false;
224 ObjectPropertyInfoList *props = NULL;
225 ObjectProperty *prop;
226 ObjectPropertyIterator iter;
228 obj = object_resolve_path(path, &ambiguous);
231 error_setg(errp, "Path '%s' is ambiguous", path);
233 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
234 "Device '%s' not found", path);
239 object_property_iter_init(&iter, obj);
240 while ((prop = object_property_iter_next(&iter))) {
241 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
243 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
247 entry->value->name = g_strdup(prop->name);
248 entry->value->type = g_strdup(prop->type);
254 void qmp_qom_set(const char *path, const char *property, QObject *value,
259 obj = object_resolve_path(path, NULL);
261 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
262 "Device '%s' not found", path);
266 object_property_set_qobject(obj, value, property, errp);
269 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
273 obj = object_resolve_path(path, NULL);
275 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
276 "Device '%s' not found", path);
280 return object_property_get_qobject(obj, property, errp);
283 void qmp_set_password(const char *protocol, const char *password,
284 bool has_connected, const char *connected, Error **errp)
286 int disconnect_if_connected = 0;
287 int fail_if_connected = 0;
291 if (strcmp(connected, "fail") == 0) {
292 fail_if_connected = 1;
293 } else if (strcmp(connected, "disconnect") == 0) {
294 disconnect_if_connected = 1;
295 } else if (strcmp(connected, "keep") == 0) {
298 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
303 if (strcmp(protocol, "spice") == 0) {
304 if (!qemu_using_spice(errp)) {
307 rc = qemu_spice_set_passwd(password, fail_if_connected,
308 disconnect_if_connected);
310 error_setg(errp, QERR_SET_PASSWD_FAILED);
315 if (strcmp(protocol, "vnc") == 0) {
316 if (fail_if_connected || disconnect_if_connected) {
317 /* vnc supports "connected=keep" only */
318 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
321 /* Note that setting an empty password will not disable login through
323 rc = vnc_display_password(NULL, password);
325 error_setg(errp, QERR_SET_PASSWD_FAILED);
330 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
333 void qmp_expire_password(const char *protocol, const char *whenstr,
339 if (strcmp(whenstr, "now") == 0) {
341 } else if (strcmp(whenstr, "never") == 0) {
343 } else if (whenstr[0] == '+') {
344 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
346 when = strtoull(whenstr, NULL, 10);
349 if (strcmp(protocol, "spice") == 0) {
350 if (!qemu_using_spice(errp)) {
353 rc = qemu_spice_set_pw_expire(when);
355 error_setg(errp, QERR_SET_PASSWD_FAILED);
360 if (strcmp(protocol, "vnc") == 0) {
361 rc = vnc_display_pw_expire(NULL, when);
363 error_setg(errp, QERR_SET_PASSWD_FAILED);
368 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
372 void qmp_change_vnc_password(const char *password, Error **errp)
374 if (vnc_display_password(NULL, password) < 0) {
375 error_setg(errp, QERR_SET_PASSWD_FAILED);
379 static void qmp_change_vnc_listen(const char *target, Error **errp)
381 QemuOptsList *olist = qemu_find_opts("vnc");
384 if (strstr(target, "id=")) {
385 error_setg(errp, "id not supported");
389 opts = qemu_opts_find(olist, "default");
393 opts = vnc_parse(target, errp);
398 vnc_display_open("default", errp);
401 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
404 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
406 error_setg(errp, QERR_MISSING_PARAMETER, "password");
408 qmp_change_vnc_password(arg, errp);
411 qmp_change_vnc_listen(target, errp);
415 void qmp_change_vnc_password(const char *password, Error **errp)
417 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
419 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
422 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
424 #endif /* !CONFIG_VNC */
426 void qmp_change(const char *device, const char *target,
427 bool has_arg, const char *arg, Error **errp)
429 if (strcmp(device, "vnc") == 0) {
430 qmp_change_vnc(target, has_arg, arg, errp);
432 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
437 static void qom_list_types_tramp(ObjectClass *klass, void *data)
439 ObjectTypeInfoList *e, **pret = data;
440 ObjectTypeInfo *info;
442 info = g_malloc0(sizeof(*info));
443 info->name = g_strdup(object_class_get_name(klass));
445 e = g_malloc0(sizeof(*e));
451 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
452 const char *implements,
457 ObjectTypeInfoList *ret = NULL;
459 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
464 /* Return a DevicePropertyInfo for a qdev property.
466 * If a qdev property with the given name does not exist, use the given default
467 * type. If the qdev property info should not be shown, return NULL.
469 * The caller must free the return value.
471 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
473 const char *default_type,
474 const char *description)
476 DevicePropertyInfo *info;
480 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
481 if (strcmp(name, prop->name) != 0) {
486 * TODO Properties without a parser are just for dirty hacks.
487 * qdev_prop_ptr is the only such PropertyInfo. It's marked
488 * for removal. This conditional should be removed along with
491 if (!prop->info->set) {
492 return NULL; /* no way to set it, don't show */
495 info = g_malloc0(sizeof(*info));
496 info->name = g_strdup(prop->name);
497 info->type = g_strdup(prop->info->name);
498 info->has_description = !!prop->info->description;
499 info->description = g_strdup(prop->info->description);
502 klass = object_class_get_parent(klass);
503 } while (klass != object_class_by_name(TYPE_DEVICE));
505 /* Not a qdev property, use the default type */
506 info = g_malloc0(sizeof(*info));
507 info->name = g_strdup(name);
508 info->type = g_strdup(default_type);
509 info->has_description = !!description;
510 info->description = g_strdup(description);
515 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
520 ObjectProperty *prop;
521 ObjectPropertyIterator iter;
522 DevicePropertyInfoList *prop_list = NULL;
524 klass = object_class_by_name(typename);
526 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
527 "Device '%s' not found", typename);
531 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
533 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
537 if (object_class_is_abstract(klass)) {
538 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
539 "non-abstract device type");
543 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
544 error_setg(errp, "Can't list properties of device '%s'", typename);
548 obj = object_new(typename);
550 object_property_iter_init(&iter, obj);
551 while ((prop = object_property_iter_next(&iter))) {
552 DevicePropertyInfo *info;
553 DevicePropertyInfoList *entry;
555 /* Skip Object and DeviceState properties */
556 if (strcmp(prop->name, "type") == 0 ||
557 strcmp(prop->name, "realized") == 0 ||
558 strcmp(prop->name, "hotpluggable") == 0 ||
559 strcmp(prop->name, "hotplugged") == 0 ||
560 strcmp(prop->name, "parent_bus") == 0) {
564 /* Skip legacy properties since they are just string versions of
565 * properties that we already list.
567 if (strstart(prop->name, "legacy-", NULL)) {
571 info = make_device_property_info(klass, prop->name, prop->type,
577 entry = g_malloc0(sizeof(*entry));
579 entry->next = prop_list;
588 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
590 return arch_query_cpu_definitions(errp);
593 void qmp_add_client(const char *protocol, const char *fdname,
594 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
600 fd = monitor_get_fd(cur_mon, fdname, errp);
605 if (strcmp(protocol, "spice") == 0) {
606 if (!qemu_using_spice(errp)) {
610 skipauth = has_skipauth ? skipauth : false;
611 tls = has_tls ? tls : false;
612 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
613 error_setg(errp, "spice failed to add client");
618 } else if (strcmp(protocol, "vnc") == 0) {
619 skipauth = has_skipauth ? skipauth : false;
620 vnc_display_add_client(NULL, fd, skipauth);
623 } else if ((s = qemu_chr_find(protocol)) != NULL) {
624 if (qemu_chr_add_client(s, fd) < 0) {
625 error_setg(errp, "failed to add client");
632 error_setg(errp, "protocol '%s' is invalid", protocol);
636 void object_add(const char *type, const char *id, const QDict *qdict,
637 Visitor *v, Error **errp)
642 Error *local_err = NULL;
644 klass = object_class_by_name(type);
646 error_setg(errp, "invalid object type: %s", type);
650 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
651 error_setg(errp, "object type '%s' isn't supported by object-add",
656 if (object_class_is_abstract(klass)) {
657 error_setg(errp, "object type '%s' is abstract", type);
661 obj = object_new(type);
663 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
664 object_property_set(obj, v, e->key, &local_err);
671 object_property_add_child(object_get_objects_root(),
672 id, obj, &local_err);
677 user_creatable_complete(obj, &local_err);
679 object_property_del(object_get_objects_root(),
685 error_propagate(errp, local_err);
690 void qmp_object_add(const char *type, const char *id,
691 bool has_props, QObject *props, Error **errp)
693 const QDict *pdict = NULL;
694 QmpInputVisitor *qiv;
697 pdict = qobject_to_qdict(props);
699 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
704 qiv = qmp_input_visitor_new(props);
705 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
706 qmp_input_visitor_cleanup(qiv);
709 void qmp_object_del(const char *id, Error **errp)
714 container = object_get_objects_root();
715 obj = object_resolve_path_component(container, id);
717 error_setg(errp, "object id not found");
721 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
722 error_setg(errp, "%s is in use, can not be deleted", id);
725 object_unparent(obj);
728 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
730 MemoryDeviceInfoList *head = NULL;
731 MemoryDeviceInfoList **prev = &head;
733 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
738 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
741 ACPIOSTInfoList *head = NULL;
742 ACPIOSTInfoList **prev = &head;
743 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
746 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
747 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
749 adevc->ospm_status(adev, &prev);
751 error_setg(errp, "command is not supported, missing ACPI device");