2 * QEMU Management Protocol
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
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-common.h"
17 #include "monitor/monitor.h"
18 #include "sysemu/sysemu.h"
19 #include "qmp-commands.h"
20 #include "sysemu/char.h"
21 #include "ui/qemu-spice.h"
23 #include "sysemu/kvm.h"
24 #include "sysemu/arch_init.h"
26 #include "sysemu/blockdev.h"
27 #include "qom/qom-qobject.h"
28 #include "qapi/qmp/qerror.h"
29 #include "qapi/qmp/qobject.h"
30 #include "qapi/qmp-input-visitor.h"
31 #include "hw/boards.h"
32 #include "qom/object_interfaces.h"
33 #include "hw/mem/pc-dimm.h"
34 #include "hw/acpi/acpi_dev_interface.h"
36 NameInfo *qmp_query_name(Error **errp)
38 NameInfo *info = g_malloc0(sizeof(*info));
41 info->has_name = true;
42 info->name = g_strdup(qemu_name);
48 VersionInfo *qmp_query_version(Error **errp)
50 VersionInfo *info = g_new0(VersionInfo, 1);
51 const char *version = QEMU_VERSION;
55 info->qemu = g_new0(VersionTriple, 1);
56 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
60 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
64 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
66 info->package = g_strdup(QEMU_PKGVERSION);
71 KvmInfo *qmp_query_kvm(Error **errp)
73 KvmInfo *info = g_malloc0(sizeof(*info));
75 info->enabled = kvm_enabled();
76 info->present = kvm_available();
81 UuidInfo *qmp_query_uuid(Error **errp)
83 UuidInfo *info = g_malloc0(sizeof(*info));
86 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
87 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
88 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
89 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
90 qemu_uuid[14], qemu_uuid[15]);
92 info->UUID = g_strdup(uuid);
96 void qmp_quit(Error **errp)
99 qemu_system_shutdown_request();
102 void qmp_stop(Error **errp)
104 if (runstate_check(RUN_STATE_INMIGRATE)) {
107 vm_stop(RUN_STATE_PAUSED);
111 void qmp_system_reset(Error **errp)
113 qemu_system_reset_request();
116 void qmp_system_powerdown(Error **erp)
118 qemu_system_powerdown_request();
121 void qmp_cpu(int64_t index, Error **errp)
123 /* Just do nothing */
126 void qmp_cpu_add(int64_t id, Error **errp)
130 mc = MACHINE_GET_CLASS(current_machine);
131 if (mc->hot_add_cpu) {
132 mc->hot_add_cpu(id, errp);
134 error_setg(errp, "Not supported");
139 /* If VNC support is enabled, the "true" query-vnc command is
140 defined in the VNC subsystem */
141 VncInfo *qmp_query_vnc(Error **errp)
143 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
147 VncInfo2List *qmp_query_vnc_servers(Error **errp)
149 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
156 * qmp-commands.hx ensures that QMP command query-spice exists only
157 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
158 * result. However, the QAPI schema is blissfully unaware of that,
159 * and the QAPI code generator happily generates a dead
160 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
161 * one, or else linking fails. FIXME Educate the QAPI schema on
164 SpiceInfo *qmp_query_spice(Error **errp)
170 void qmp_cont(Error **errp)
172 Error *local_err = NULL;
173 BlockDriverState *bs;
175 if (runstate_needs_reset()) {
176 error_setg(errp, "Resetting the Virtual Machine is required");
178 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
182 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
183 bdrv_iostatus_reset(bs);
185 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
186 bdrv_add_key(bs, NULL, &local_err);
188 error_propagate(errp, local_err);
193 if (runstate_check(RUN_STATE_INMIGRATE)) {
200 void qmp_system_wakeup(Error **errp)
202 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
205 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
208 bool ambiguous = false;
209 ObjectPropertyInfoList *props = NULL;
210 ObjectProperty *prop;
212 obj = object_resolve_path(path, &ambiguous);
215 error_setg(errp, "Path '%s' is ambiguous", path);
217 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
218 "Device '%s' not found", path);
223 QTAILQ_FOREACH(prop, &obj->properties, node) {
224 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
226 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
230 entry->value->name = g_strdup(prop->name);
231 entry->value->type = g_strdup(prop->type);
237 void qmp_qom_set(const char *path, const char *property, QObject *value,
242 obj = object_resolve_path(path, NULL);
244 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
245 "Device '%s' not found", path);
249 object_property_set_qobject(obj, value, property, errp);
252 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
256 obj = object_resolve_path(path, NULL);
258 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
259 "Device '%s' not found", path);
263 return object_property_get_qobject(obj, property, errp);
266 void qmp_set_password(const char *protocol, const char *password,
267 bool has_connected, const char *connected, Error **errp)
269 int disconnect_if_connected = 0;
270 int fail_if_connected = 0;
274 if (strcmp(connected, "fail") == 0) {
275 fail_if_connected = 1;
276 } else if (strcmp(connected, "disconnect") == 0) {
277 disconnect_if_connected = 1;
278 } else if (strcmp(connected, "keep") == 0) {
281 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
286 if (strcmp(protocol, "spice") == 0) {
287 if (!qemu_using_spice(errp)) {
290 rc = qemu_spice_set_passwd(password, fail_if_connected,
291 disconnect_if_connected);
293 error_setg(errp, QERR_SET_PASSWD_FAILED);
298 if (strcmp(protocol, "vnc") == 0) {
299 if (fail_if_connected || disconnect_if_connected) {
300 /* vnc supports "connected=keep" only */
301 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
304 /* Note that setting an empty password will not disable login through
306 rc = vnc_display_password(NULL, password);
308 error_setg(errp, QERR_SET_PASSWD_FAILED);
313 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
316 void qmp_expire_password(const char *protocol, const char *whenstr,
322 if (strcmp(whenstr, "now") == 0) {
324 } else if (strcmp(whenstr, "never") == 0) {
326 } else if (whenstr[0] == '+') {
327 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
329 when = strtoull(whenstr, NULL, 10);
332 if (strcmp(protocol, "spice") == 0) {
333 if (!qemu_using_spice(errp)) {
336 rc = qemu_spice_set_pw_expire(when);
338 error_setg(errp, QERR_SET_PASSWD_FAILED);
343 if (strcmp(protocol, "vnc") == 0) {
344 rc = vnc_display_pw_expire(NULL, when);
346 error_setg(errp, QERR_SET_PASSWD_FAILED);
351 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
355 void qmp_change_vnc_password(const char *password, Error **errp)
357 if (vnc_display_password(NULL, password) < 0) {
358 error_setg(errp, QERR_SET_PASSWD_FAILED);
362 static void qmp_change_vnc_listen(const char *target, Error **errp)
364 QemuOptsList *olist = qemu_find_opts("vnc");
367 if (strstr(target, "id=")) {
368 error_setg(errp, "id not supported");
372 opts = qemu_opts_find(olist, "default");
376 opts = vnc_parse(target, errp);
381 vnc_display_open("default", errp);
384 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
387 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
389 error_setg(errp, QERR_MISSING_PARAMETER, "password");
391 qmp_change_vnc_password(arg, errp);
394 qmp_change_vnc_listen(target, errp);
398 void qmp_change_vnc_password(const char *password, Error **errp)
400 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
402 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
405 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
407 #endif /* !CONFIG_VNC */
409 void qmp_change(const char *device, const char *target,
410 bool has_arg, const char *arg, Error **errp)
412 if (strcmp(device, "vnc") == 0) {
413 qmp_change_vnc(target, has_arg, arg, errp);
415 qmp_change_blockdev(device, target, arg, errp);
419 static void qom_list_types_tramp(ObjectClass *klass, void *data)
421 ObjectTypeInfoList *e, **pret = data;
422 ObjectTypeInfo *info;
424 info = g_malloc0(sizeof(*info));
425 info->name = g_strdup(object_class_get_name(klass));
427 e = g_malloc0(sizeof(*e));
433 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
434 const char *implements,
439 ObjectTypeInfoList *ret = NULL;
441 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
446 /* Return a DevicePropertyInfo for a qdev property.
448 * If a qdev property with the given name does not exist, use the given default
449 * type. If the qdev property info should not be shown, return NULL.
451 * The caller must free the return value.
453 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
455 const char *default_type,
456 const char *description)
458 DevicePropertyInfo *info;
462 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
463 if (strcmp(name, prop->name) != 0) {
468 * TODO Properties without a parser are just for dirty hacks.
469 * qdev_prop_ptr is the only such PropertyInfo. It's marked
470 * for removal. This conditional should be removed along with
473 if (!prop->info->set) {
474 return NULL; /* no way to set it, don't show */
477 info = g_malloc0(sizeof(*info));
478 info->name = g_strdup(prop->name);
479 info->type = g_strdup(prop->info->name);
480 info->has_description = !!prop->info->description;
481 info->description = g_strdup(prop->info->description);
484 klass = object_class_get_parent(klass);
485 } while (klass != object_class_by_name(TYPE_DEVICE));
487 /* Not a qdev property, use the default type */
488 info = g_malloc0(sizeof(*info));
489 info->name = g_strdup(name);
490 info->type = g_strdup(default_type);
491 info->has_description = !!description;
492 info->description = g_strdup(description);
497 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
502 ObjectProperty *prop;
503 DevicePropertyInfoList *prop_list = NULL;
505 klass = object_class_by_name(typename);
507 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
508 "Device '%s' not found", typename);
512 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
514 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
518 obj = object_new(typename);
520 QTAILQ_FOREACH(prop, &obj->properties, node) {
521 DevicePropertyInfo *info;
522 DevicePropertyInfoList *entry;
524 /* Skip Object and DeviceState properties */
525 if (strcmp(prop->name, "type") == 0 ||
526 strcmp(prop->name, "realized") == 0 ||
527 strcmp(prop->name, "hotpluggable") == 0 ||
528 strcmp(prop->name, "hotplugged") == 0 ||
529 strcmp(prop->name, "parent_bus") == 0) {
533 /* Skip legacy properties since they are just string versions of
534 * properties that we already list.
536 if (strstart(prop->name, "legacy-", NULL)) {
540 info = make_device_property_info(klass, prop->name, prop->type,
546 entry = g_malloc0(sizeof(*entry));
548 entry->next = prop_list;
557 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
559 return arch_query_cpu_definitions(errp);
562 void qmp_add_client(const char *protocol, const char *fdname,
563 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
569 fd = monitor_get_fd(cur_mon, fdname, errp);
574 if (strcmp(protocol, "spice") == 0) {
575 if (!qemu_using_spice(errp)) {
579 skipauth = has_skipauth ? skipauth : false;
580 tls = has_tls ? tls : false;
581 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
582 error_setg(errp, "spice failed to add client");
587 } else if (strcmp(protocol, "vnc") == 0) {
588 skipauth = has_skipauth ? skipauth : false;
589 vnc_display_add_client(NULL, fd, skipauth);
592 } else if ((s = qemu_chr_find(protocol)) != NULL) {
593 if (qemu_chr_add_client(s, fd) < 0) {
594 error_setg(errp, "failed to add client");
601 error_setg(errp, "protocol '%s' is invalid", protocol);
605 void object_add(const char *type, const char *id, const QDict *qdict,
606 Visitor *v, Error **errp)
611 Error *local_err = NULL;
613 klass = object_class_by_name(type);
615 error_setg(errp, "invalid object type: %s", type);
619 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
620 error_setg(errp, "object type '%s' isn't supported by object-add",
625 if (object_class_is_abstract(klass)) {
626 error_setg(errp, "object type '%s' is abstract", type);
630 obj = object_new(type);
632 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
633 object_property_set(obj, v, e->key, &local_err);
640 object_property_add_child(object_get_objects_root(),
641 id, obj, &local_err);
646 user_creatable_complete(obj, &local_err);
648 object_property_del(object_get_objects_root(),
654 error_propagate(errp, local_err);
659 void qmp_object_add(const char *type, const char *id,
660 bool has_props, QObject *props, Error **errp)
662 const QDict *pdict = NULL;
663 QmpInputVisitor *qiv;
666 pdict = qobject_to_qdict(props);
668 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
673 qiv = qmp_input_visitor_new(props);
674 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
675 qmp_input_visitor_cleanup(qiv);
678 void qmp_object_del(const char *id, Error **errp)
683 container = object_get_objects_root();
684 obj = object_resolve_path_component(container, id);
686 error_setg(errp, "object id not found");
690 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
691 error_setg(errp, "%s is in use, can not be deleted", id);
694 object_unparent(obj);
697 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
699 MemoryDeviceInfoList *head = NULL;
700 MemoryDeviceInfoList **prev = &head;
702 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
707 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
710 ACPIOSTInfoList *head = NULL;
711 ACPIOSTInfoList **prev = &head;
712 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
715 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
716 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
718 adevc->ospm_status(adev, &prev);
720 error_setg(errp, "command is not supported, missing ACPI device");