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
;
54 info
->qemu
= g_new0(VersionTriple
, 1);
55 info
->qemu
->major
= strtol(version
, &tmp
, 10);
57 info
->qemu
->minor
= strtol(tmp
, &tmp
, 10);
59 info
->qemu
->micro
= strtol(tmp
, &tmp
, 10);
60 info
->package
= g_strdup(QEMU_PKGVERSION
);
65 KvmInfo
*qmp_query_kvm(Error
**errp
)
67 KvmInfo
*info
= g_malloc0(sizeof(*info
));
69 info
->enabled
= kvm_enabled();
70 info
->present
= kvm_available();
75 UuidInfo
*qmp_query_uuid(Error
**errp
)
77 UuidInfo
*info
= g_malloc0(sizeof(*info
));
80 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
81 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
82 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
83 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
84 qemu_uuid
[14], qemu_uuid
[15]);
86 info
->UUID
= g_strdup(uuid
);
90 void qmp_quit(Error
**errp
)
93 qemu_system_shutdown_request();
96 void qmp_stop(Error
**errp
)
98 if (runstate_check(RUN_STATE_INMIGRATE
)) {
101 vm_stop(RUN_STATE_PAUSED
);
105 void qmp_system_reset(Error
**errp
)
107 qemu_system_reset_request();
110 void qmp_system_powerdown(Error
**erp
)
112 qemu_system_powerdown_request();
115 void qmp_cpu(int64_t index
, Error
**errp
)
117 /* Just do nothing */
120 void qmp_cpu_add(int64_t id
, Error
**errp
)
124 mc
= MACHINE_GET_CLASS(current_machine
);
125 if (mc
->hot_add_cpu
) {
126 mc
->hot_add_cpu(id
, errp
);
128 error_setg(errp
, "Not supported");
133 /* If VNC support is enabled, the "true" query-vnc command is
134 defined in the VNC subsystem */
135 VncInfo
*qmp_query_vnc(Error
**errp
)
137 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
141 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
143 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
150 * qmp-commands.hx ensures that QMP command query-spice exists only
151 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
152 * result. However, the QAPI schema is blissfully unaware of that,
153 * and the QAPI code generator happily generates a dead
154 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
155 * Provide it one, or else linking fails.
156 * FIXME Educate the QAPI schema on CONFIG_SPICE.
158 SpiceInfo
*qmp_query_spice(Error
**errp
)
164 void qmp_cont(Error
**errp
)
166 Error
*local_err
= NULL
;
167 BlockDriverState
*bs
;
169 if (runstate_needs_reset()) {
170 error_setg(errp
, "Resetting the Virtual Machine is required");
172 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
176 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
177 bdrv_iostatus_reset(bs
);
179 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
180 bdrv_add_key(bs
, NULL
, &local_err
);
182 error_propagate(errp
, local_err
);
187 if (runstate_check(RUN_STATE_INMIGRATE
)) {
194 void qmp_system_wakeup(Error
**errp
)
196 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
199 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
202 bool ambiguous
= false;
203 ObjectPropertyInfoList
*props
= NULL
;
204 ObjectProperty
*prop
;
206 obj
= object_resolve_path(path
, &ambiguous
);
209 error_setg(errp
, "Path '%s' is ambiguous", path
);
211 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
212 "Device '%s' not found", path
);
217 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
218 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
220 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
224 entry
->value
->name
= g_strdup(prop
->name
);
225 entry
->value
->type
= g_strdup(prop
->type
);
231 /* FIXME: teach qapi about how to pass through Visitors */
232 void qmp_qom_set(QDict
*qdict
, QObject
**ret
, Error
**errp
)
234 const char *path
= qdict_get_str(qdict
, "path");
235 const char *property
= qdict_get_str(qdict
, "property");
236 QObject
*value
= qdict_get(qdict
, "value");
239 obj
= object_resolve_path(path
, NULL
);
241 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
242 "Device '%s' not found", path
);
246 object_property_set_qobject(obj
, value
, property
, errp
);
249 void qmp_qom_get(QDict
*qdict
, QObject
**ret
, Error
**errp
)
251 const char *path
= qdict_get_str(qdict
, "path");
252 const char *property
= qdict_get_str(qdict
, "property");
255 obj
= object_resolve_path(path
, NULL
);
257 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
258 "Device '%s' not found", path
);
262 *ret
= object_property_get_qobject(obj
, property
, errp
);
265 void qmp_set_password(const char *protocol
, const char *password
,
266 bool has_connected
, const char *connected
, Error
**errp
)
268 int disconnect_if_connected
= 0;
269 int fail_if_connected
= 0;
273 if (strcmp(connected
, "fail") == 0) {
274 fail_if_connected
= 1;
275 } else if (strcmp(connected
, "disconnect") == 0) {
276 disconnect_if_connected
= 1;
277 } else if (strcmp(connected
, "keep") == 0) {
280 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
285 if (strcmp(protocol
, "spice") == 0) {
286 if (!qemu_using_spice(errp
)) {
289 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
290 disconnect_if_connected
);
292 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
297 if (strcmp(protocol
, "vnc") == 0) {
298 if (fail_if_connected
|| disconnect_if_connected
) {
299 /* vnc supports "connected=keep" only */
300 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
303 /* Note that setting an empty password will not disable login through
305 rc
= vnc_display_password(NULL
, password
);
307 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
312 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
315 void qmp_expire_password(const char *protocol
, const char *whenstr
,
321 if (strcmp(whenstr
, "now") == 0) {
323 } else if (strcmp(whenstr
, "never") == 0) {
325 } else if (whenstr
[0] == '+') {
326 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
328 when
= strtoull(whenstr
, NULL
, 10);
331 if (strcmp(protocol
, "spice") == 0) {
332 if (!qemu_using_spice(errp
)) {
335 rc
= qemu_spice_set_pw_expire(when
);
337 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
342 if (strcmp(protocol
, "vnc") == 0) {
343 rc
= vnc_display_pw_expire(NULL
, when
);
345 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
350 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
354 void qmp_change_vnc_password(const char *password
, Error
**errp
)
356 if (vnc_display_password(NULL
, password
) < 0) {
357 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
361 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
363 QemuOptsList
*olist
= qemu_find_opts("vnc");
366 if (strstr(target
, "id=")) {
367 error_setg(errp
, "id not supported");
371 opts
= qemu_opts_find(olist
, "default");
375 opts
= vnc_parse(target
, errp
);
380 vnc_display_open("default", errp
);
383 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
386 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
388 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
390 qmp_change_vnc_password(arg
, errp
);
393 qmp_change_vnc_listen(target
, errp
);
397 void qmp_change_vnc_password(const char *password
, Error
**errp
)
399 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
401 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
404 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
406 #endif /* !CONFIG_VNC */
408 void qmp_change(const char *device
, const char *target
,
409 bool has_arg
, const char *arg
, Error
**errp
)
411 if (strcmp(device
, "vnc") == 0) {
412 qmp_change_vnc(target
, has_arg
, arg
, errp
);
414 qmp_change_blockdev(device
, target
, arg
, errp
);
418 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
420 ObjectTypeInfoList
*e
, **pret
= data
;
421 ObjectTypeInfo
*info
;
423 info
= g_malloc0(sizeof(*info
));
424 info
->name
= g_strdup(object_class_get_name(klass
));
426 e
= g_malloc0(sizeof(*e
));
432 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
433 const char *implements
,
438 ObjectTypeInfoList
*ret
= NULL
;
440 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
445 /* Return a DevicePropertyInfo for a qdev property.
447 * If a qdev property with the given name does not exist, use the given default
448 * type. If the qdev property info should not be shown, return NULL.
450 * The caller must free the return value.
452 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
454 const char *default_type
,
455 const char *description
)
457 DevicePropertyInfo
*info
;
461 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
462 if (strcmp(name
, prop
->name
) != 0) {
467 * TODO Properties without a parser are just for dirty hacks.
468 * qdev_prop_ptr is the only such PropertyInfo. It's marked
469 * for removal. This conditional should be removed along with
472 if (!prop
->info
->set
) {
473 return NULL
; /* no way to set it, don't show */
476 info
= g_malloc0(sizeof(*info
));
477 info
->name
= g_strdup(prop
->name
);
478 info
->type
= g_strdup(prop
->info
->name
);
479 info
->has_description
= !!prop
->info
->description
;
480 info
->description
= g_strdup(prop
->info
->description
);
483 klass
= object_class_get_parent(klass
);
484 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
486 /* Not a qdev property, use the default type */
487 info
= g_malloc0(sizeof(*info
));
488 info
->name
= g_strdup(name
);
489 info
->type
= g_strdup(default_type
);
490 info
->has_description
= !!description
;
491 info
->description
= g_strdup(description
);
496 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
501 ObjectProperty
*prop
;
502 DevicePropertyInfoList
*prop_list
= NULL
;
504 klass
= object_class_by_name(typename
);
506 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
507 "Device '%s' not found", typename
);
511 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
513 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name", TYPE_DEVICE
);
517 if (object_class_is_abstract(klass
)) {
518 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name",
519 "non-abstract device type");
523 if (DEVICE_CLASS(klass
)->cannot_destroy_with_object_finalize_yet
) {
524 error_setg(errp
, "Can't list properties of device '%s'", typename
);
528 obj
= object_new(typename
);
530 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
531 DevicePropertyInfo
*info
;
532 DevicePropertyInfoList
*entry
;
534 /* Skip Object and DeviceState properties */
535 if (strcmp(prop
->name
, "type") == 0 ||
536 strcmp(prop
->name
, "realized") == 0 ||
537 strcmp(prop
->name
, "hotpluggable") == 0 ||
538 strcmp(prop
->name
, "hotplugged") == 0 ||
539 strcmp(prop
->name
, "parent_bus") == 0) {
543 /* Skip legacy properties since they are just string versions of
544 * properties that we already list.
546 if (strstart(prop
->name
, "legacy-", NULL
)) {
550 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
556 entry
= g_malloc0(sizeof(*entry
));
558 entry
->next
= prop_list
;
567 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
569 return arch_query_cpu_definitions(errp
);
572 void qmp_add_client(const char *protocol
, const char *fdname
,
573 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
579 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
584 if (strcmp(protocol
, "spice") == 0) {
585 if (!qemu_using_spice(errp
)) {
589 skipauth
= has_skipauth
? skipauth
: false;
590 tls
= has_tls
? tls
: false;
591 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
592 error_setg(errp
, "spice failed to add client");
597 } else if (strcmp(protocol
, "vnc") == 0) {
598 skipauth
= has_skipauth
? skipauth
: false;
599 vnc_display_add_client(NULL
, fd
, skipauth
);
602 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
603 if (qemu_chr_add_client(s
, fd
) < 0) {
604 error_setg(errp
, "failed to add client");
611 error_setg(errp
, "protocol '%s' is invalid", protocol
);
615 void object_add(const char *type
, const char *id
, const QDict
*qdict
,
616 Visitor
*v
, Error
**errp
)
621 Error
*local_err
= NULL
;
623 klass
= object_class_by_name(type
);
625 error_setg(errp
, "invalid object type: %s", type
);
629 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
630 error_setg(errp
, "object type '%s' isn't supported by object-add",
635 if (object_class_is_abstract(klass
)) {
636 error_setg(errp
, "object type '%s' is abstract", type
);
640 obj
= object_new(type
);
642 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
643 object_property_set(obj
, v
, e
->key
, &local_err
);
650 object_property_add_child(object_get_objects_root(),
651 id
, obj
, &local_err
);
656 user_creatable_complete(obj
, &local_err
);
658 object_property_del(object_get_objects_root(),
664 error_propagate(errp
, local_err
);
669 void qmp_object_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
671 const char *type
= qdict_get_str(qdict
, "qom-type");
672 const char *id
= qdict_get_str(qdict
, "id");
673 QObject
*props
= qdict_get(qdict
, "props");
674 const QDict
*pdict
= NULL
;
675 QmpInputVisitor
*qiv
;
678 pdict
= qobject_to_qdict(props
);
680 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
685 qiv
= qmp_input_visitor_new(props
);
686 object_add(type
, id
, pdict
, qmp_input_get_visitor(qiv
), errp
);
687 qmp_input_visitor_cleanup(qiv
);
690 void qmp_object_del(const char *id
, Error
**errp
)
695 container
= object_get_objects_root();
696 obj
= object_resolve_path_component(container
, id
);
698 error_setg(errp
, "object id not found");
702 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
), errp
)) {
703 error_setg(errp
, "%s is in use, can not be deleted", id
);
706 object_unparent(obj
);
709 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
711 MemoryDeviceInfoList
*head
= NULL
;
712 MemoryDeviceInfoList
**prev
= &head
;
714 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
719 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
722 ACPIOSTInfoList
*head
= NULL
;
723 ACPIOSTInfoList
**prev
= &head
;
724 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
727 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
728 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
730 adevc
->ospm_status(adev
, &prev
);
732 error_setg(errp
, "command is not supported, missing ACPI device");