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 "sysemu/sysemu.h"
18 #include "qmp-commands.h"
19 #include "sysemu/char.h"
20 #include "ui/qemu-spice.h"
22 #include "sysemu/kvm.h"
23 #include "sysemu/arch_init.h"
25 #include "sysemu/blockdev.h"
26 #include "qom/qom-qobject.h"
27 #include "qapi/qmp/qobject.h"
28 #include "qapi/qmp-input-visitor.h"
29 #include "hw/boards.h"
30 #include "qom/object_interfaces.h"
31 #include "hw/mem/pc-dimm.h"
32 #include "hw/acpi/acpi_dev_interface.h"
34 NameInfo
*qmp_query_name(Error
**errp
)
36 NameInfo
*info
= g_malloc0(sizeof(*info
));
39 info
->has_name
= true;
40 info
->name
= g_strdup(qemu_name
);
46 VersionInfo
*qmp_query_version(Error
**errp
)
48 VersionInfo
*info
= g_new0(VersionInfo
, 1);
49 const char *version
= QEMU_VERSION
;
52 info
->qemu
= g_new0(VersionTriple
, 1);
53 info
->qemu
->major
= strtol(version
, &tmp
, 10);
55 info
->qemu
->minor
= strtol(tmp
, &tmp
, 10);
57 info
->qemu
->micro
= strtol(tmp
, &tmp
, 10);
58 info
->package
= g_strdup(QEMU_PKGVERSION
);
63 KvmInfo
*qmp_query_kvm(Error
**errp
)
65 KvmInfo
*info
= g_malloc0(sizeof(*info
));
67 info
->enabled
= kvm_enabled();
68 info
->present
= kvm_available();
73 UuidInfo
*qmp_query_uuid(Error
**errp
)
75 UuidInfo
*info
= g_malloc0(sizeof(*info
));
78 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
79 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
80 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
81 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
82 qemu_uuid
[14], qemu_uuid
[15]);
84 info
->UUID
= g_strdup(uuid
);
88 void qmp_quit(Error
**errp
)
91 qemu_system_shutdown_request();
94 void qmp_stop(Error
**errp
)
96 if (runstate_check(RUN_STATE_INMIGRATE
)) {
99 vm_stop(RUN_STATE_PAUSED
);
103 void qmp_system_reset(Error
**errp
)
105 qemu_system_reset_request();
108 void qmp_system_powerdown(Error
**erp
)
110 qemu_system_powerdown_request();
113 void qmp_cpu(int64_t index
, Error
**errp
)
115 /* Just do nothing */
118 void qmp_cpu_add(int64_t id
, Error
**errp
)
122 mc
= MACHINE_GET_CLASS(current_machine
);
123 if (mc
->hot_add_cpu
) {
124 mc
->hot_add_cpu(id
, errp
);
126 error_setg(errp
, "Not supported");
131 /* If VNC support is enabled, the "true" query-vnc command is
132 defined in the VNC subsystem */
133 VncInfo
*qmp_query_vnc(Error
**errp
)
135 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
139 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
141 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
148 * qmp-commands.hx ensures that QMP command query-spice exists only
149 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
150 * result. However, the QAPI schema is blissfully unaware of that,
151 * and the QAPI code generator happily generates a dead
152 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
153 * Provide it one, or else linking fails.
154 * FIXME Educate the QAPI schema on CONFIG_SPICE.
156 SpiceInfo
*qmp_query_spice(Error
**errp
)
162 void qmp_cont(Error
**errp
)
164 Error
*local_err
= NULL
;
165 BlockDriverState
*bs
;
167 if (runstate_needs_reset()) {
168 error_setg(errp
, "Resetting the Virtual Machine is required");
170 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
174 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
175 bdrv_iostatus_reset(bs
);
177 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
178 bdrv_add_key(bs
, NULL
, &local_err
);
180 error_propagate(errp
, local_err
);
185 if (runstate_check(RUN_STATE_INMIGRATE
)) {
192 void qmp_system_wakeup(Error
**errp
)
194 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
197 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
200 bool ambiguous
= false;
201 ObjectPropertyInfoList
*props
= NULL
;
202 ObjectProperty
*prop
;
204 obj
= object_resolve_path(path
, &ambiguous
);
207 error_setg(errp
, "Path '%s' is ambiguous", path
);
209 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
210 "Device '%s' not found", path
);
215 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
216 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
218 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
222 entry
->value
->name
= g_strdup(prop
->name
);
223 entry
->value
->type
= g_strdup(prop
->type
);
229 /* FIXME: teach qapi about how to pass through Visitors */
230 void qmp_qom_set(QDict
*qdict
, QObject
**ret
, Error
**errp
)
232 const char *path
= qdict_get_str(qdict
, "path");
233 const char *property
= qdict_get_str(qdict
, "property");
234 QObject
*value
= qdict_get(qdict
, "value");
237 obj
= object_resolve_path(path
, NULL
);
239 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
240 "Device '%s' not found", path
);
244 object_property_set_qobject(obj
, value
, property
, errp
);
247 void qmp_qom_get(QDict
*qdict
, QObject
**ret
, Error
**errp
)
249 const char *path
= qdict_get_str(qdict
, "path");
250 const char *property
= qdict_get_str(qdict
, "property");
253 obj
= object_resolve_path(path
, NULL
);
255 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
256 "Device '%s' not found", path
);
260 *ret
= object_property_get_qobject(obj
, property
, errp
);
263 void qmp_set_password(const char *protocol
, const char *password
,
264 bool has_connected
, const char *connected
, Error
**errp
)
266 int disconnect_if_connected
= 0;
267 int fail_if_connected
= 0;
271 if (strcmp(connected
, "fail") == 0) {
272 fail_if_connected
= 1;
273 } else if (strcmp(connected
, "disconnect") == 0) {
274 disconnect_if_connected
= 1;
275 } else if (strcmp(connected
, "keep") == 0) {
278 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
283 if (strcmp(protocol
, "spice") == 0) {
284 if (!qemu_using_spice(errp
)) {
287 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
288 disconnect_if_connected
);
290 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
295 if (strcmp(protocol
, "vnc") == 0) {
296 if (fail_if_connected
|| disconnect_if_connected
) {
297 /* vnc supports "connected=keep" only */
298 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
301 /* Note that setting an empty password will not disable login through
303 rc
= vnc_display_password(NULL
, password
);
305 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
310 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
313 void qmp_expire_password(const char *protocol
, const char *whenstr
,
319 if (strcmp(whenstr
, "now") == 0) {
321 } else if (strcmp(whenstr
, "never") == 0) {
323 } else if (whenstr
[0] == '+') {
324 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
326 when
= strtoull(whenstr
, NULL
, 10);
329 if (strcmp(protocol
, "spice") == 0) {
330 if (!qemu_using_spice(errp
)) {
333 rc
= qemu_spice_set_pw_expire(when
);
335 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
340 if (strcmp(protocol
, "vnc") == 0) {
341 rc
= vnc_display_pw_expire(NULL
, when
);
343 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
348 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
352 void qmp_change_vnc_password(const char *password
, Error
**errp
)
354 if (vnc_display_password(NULL
, password
) < 0) {
355 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
359 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
361 QemuOptsList
*olist
= qemu_find_opts("vnc");
364 if (strstr(target
, "id=")) {
365 error_setg(errp
, "id not supported");
369 opts
= qemu_opts_find(olist
, "default");
373 opts
= vnc_parse(target
, errp
);
378 vnc_display_open("default", errp
);
381 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
384 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
386 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
388 qmp_change_vnc_password(arg
, errp
);
391 qmp_change_vnc_listen(target
, errp
);
395 void qmp_change_vnc_password(const char *password
, Error
**errp
)
397 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
399 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
402 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
404 #endif /* !CONFIG_VNC */
406 void qmp_change(const char *device
, const char *target
,
407 bool has_arg
, const char *arg
, Error
**errp
)
409 if (strcmp(device
, "vnc") == 0) {
410 qmp_change_vnc(target
, has_arg
, arg
, errp
);
412 qmp_change_blockdev(device
, target
, arg
, errp
);
416 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
418 ObjectTypeInfoList
*e
, **pret
= data
;
419 ObjectTypeInfo
*info
;
421 info
= g_malloc0(sizeof(*info
));
422 info
->name
= g_strdup(object_class_get_name(klass
));
424 e
= g_malloc0(sizeof(*e
));
430 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
431 const char *implements
,
436 ObjectTypeInfoList
*ret
= NULL
;
438 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
443 /* Return a DevicePropertyInfo for a qdev property.
445 * If a qdev property with the given name does not exist, use the given default
446 * type. If the qdev property info should not be shown, return NULL.
448 * The caller must free the return value.
450 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
452 const char *default_type
,
453 const char *description
)
455 DevicePropertyInfo
*info
;
459 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
460 if (strcmp(name
, prop
->name
) != 0) {
465 * TODO Properties without a parser are just for dirty hacks.
466 * qdev_prop_ptr is the only such PropertyInfo. It's marked
467 * for removal. This conditional should be removed along with
470 if (!prop
->info
->set
) {
471 return NULL
; /* no way to set it, don't show */
474 info
= g_malloc0(sizeof(*info
));
475 info
->name
= g_strdup(prop
->name
);
476 info
->type
= g_strdup(prop
->info
->name
);
477 info
->has_description
= !!prop
->info
->description
;
478 info
->description
= g_strdup(prop
->info
->description
);
481 klass
= object_class_get_parent(klass
);
482 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
484 /* Not a qdev property, use the default type */
485 info
= g_malloc0(sizeof(*info
));
486 info
->name
= g_strdup(name
);
487 info
->type
= g_strdup(default_type
);
488 info
->has_description
= !!description
;
489 info
->description
= g_strdup(description
);
494 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
499 ObjectProperty
*prop
;
500 DevicePropertyInfoList
*prop_list
= NULL
;
502 klass
= object_class_by_name(typename
);
504 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
505 "Device '%s' not found", typename
);
509 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
511 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name", TYPE_DEVICE
);
515 obj
= object_new(typename
);
517 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
518 DevicePropertyInfo
*info
;
519 DevicePropertyInfoList
*entry
;
521 /* Skip Object and DeviceState properties */
522 if (strcmp(prop
->name
, "type") == 0 ||
523 strcmp(prop
->name
, "realized") == 0 ||
524 strcmp(prop
->name
, "hotpluggable") == 0 ||
525 strcmp(prop
->name
, "hotplugged") == 0 ||
526 strcmp(prop
->name
, "parent_bus") == 0) {
530 /* Skip legacy properties since they are just string versions of
531 * properties that we already list.
533 if (strstart(prop
->name
, "legacy-", NULL
)) {
537 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
543 entry
= g_malloc0(sizeof(*entry
));
545 entry
->next
= prop_list
;
554 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
556 return arch_query_cpu_definitions(errp
);
559 void qmp_add_client(const char *protocol
, const char *fdname
,
560 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
566 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
571 if (strcmp(protocol
, "spice") == 0) {
572 if (!qemu_using_spice(errp
)) {
576 skipauth
= has_skipauth
? skipauth
: false;
577 tls
= has_tls
? tls
: false;
578 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
579 error_setg(errp
, "spice failed to add client");
584 } else if (strcmp(protocol
, "vnc") == 0) {
585 skipauth
= has_skipauth
? skipauth
: false;
586 vnc_display_add_client(NULL
, fd
, skipauth
);
589 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
590 if (qemu_chr_add_client(s
, fd
) < 0) {
591 error_setg(errp
, "failed to add client");
598 error_setg(errp
, "protocol '%s' is invalid", protocol
);
602 void object_add(const char *type
, const char *id
, const QDict
*qdict
,
603 Visitor
*v
, Error
**errp
)
608 Error
*local_err
= NULL
;
610 klass
= object_class_by_name(type
);
612 error_setg(errp
, "invalid object type: %s", type
);
616 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
617 error_setg(errp
, "object type '%s' isn't supported by object-add",
622 if (object_class_is_abstract(klass
)) {
623 error_setg(errp
, "object type '%s' is abstract", type
);
627 obj
= object_new(type
);
629 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
630 object_property_set(obj
, v
, e
->key
, &local_err
);
637 object_property_add_child(object_get_objects_root(),
638 id
, obj
, &local_err
);
643 user_creatable_complete(obj
, &local_err
);
645 object_property_del(object_get_objects_root(),
651 error_propagate(errp
, local_err
);
656 void qmp_object_add(QDict
*qdict
, QObject
**ret
, Error
**errp
)
658 const char *type
= qdict_get_str(qdict
, "qom-type");
659 const char *id
= qdict_get_str(qdict
, "id");
660 QObject
*props
= qdict_get(qdict
, "props");
661 const QDict
*pdict
= NULL
;
662 QmpInputVisitor
*qiv
;
665 pdict
= qobject_to_qdict(props
);
667 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
672 qiv
= qmp_input_visitor_new(props
);
673 object_add(type
, id
, pdict
, qmp_input_get_visitor(qiv
), errp
);
674 qmp_input_visitor_cleanup(qiv
);
677 void qmp_object_del(const char *id
, Error
**errp
)
682 container
= object_get_objects_root();
683 obj
= object_resolve_path_component(container
, id
);
685 error_setg(errp
, "object id not found");
689 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj
), errp
)) {
690 error_setg(errp
, "%s is in use, can not be deleted", id
);
693 object_unparent(obj
);
696 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
698 MemoryDeviceInfoList
*head
= NULL
;
699 MemoryDeviceInfoList
**prev
= &head
;
701 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
706 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
709 ACPIOSTInfoList
*head
= NULL
;
710 ACPIOSTInfoList
**prev
= &head
;
711 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
714 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
715 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
717 adevc
->ospm_status(adev
, &prev
);
719 error_setg(errp
, "command is not supported, missing ACPI device");