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_malloc0(sizeof(*info
));
49 const char *version
= QEMU_VERSION
;
52 info
->qemu
.major
= strtol(version
, &tmp
, 10);
54 info
->qemu
.minor
= strtol(tmp
, &tmp
, 10);
56 info
->qemu
.micro
= strtol(tmp
, &tmp
, 10);
57 info
->package
= g_strdup(QEMU_PKGVERSION
);
62 KvmInfo
*qmp_query_kvm(Error
**errp
)
64 KvmInfo
*info
= g_malloc0(sizeof(*info
));
66 info
->enabled
= kvm_enabled();
67 info
->present
= kvm_available();
72 UuidInfo
*qmp_query_uuid(Error
**errp
)
74 UuidInfo
*info
= g_malloc0(sizeof(*info
));
77 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
78 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
79 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
80 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
81 qemu_uuid
[14], qemu_uuid
[15]);
83 info
->UUID
= g_strdup(uuid
);
87 void qmp_quit(Error
**errp
)
90 qemu_system_shutdown_request();
93 void qmp_stop(Error
**errp
)
95 if (runstate_check(RUN_STATE_INMIGRATE
)) {
98 vm_stop(RUN_STATE_PAUSED
);
102 void qmp_system_reset(Error
**errp
)
104 qemu_system_reset_request();
107 void qmp_system_powerdown(Error
**erp
)
109 qemu_system_powerdown_request();
112 void qmp_cpu(int64_t index
, Error
**errp
)
114 /* Just do nothing */
117 void qmp_cpu_add(int64_t id
, Error
**errp
)
121 mc
= MACHINE_GET_CLASS(current_machine
);
122 if (mc
->hot_add_cpu
) {
123 mc
->hot_add_cpu(id
, errp
);
125 error_setg(errp
, "Not supported");
130 /* If VNC support is enabled, the "true" query-vnc command is
131 defined in the VNC subsystem */
132 VncInfo
*qmp_query_vnc(Error
**errp
)
134 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
141 * qmp-commands.hx ensures that QMP command query-spice exists only
142 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
143 * result. However, the QAPI schema is blissfully unaware of that,
144 * and the QAPI code generator happily generates a dead
145 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
146 * Provide it one, or else linking fails.
147 * FIXME Educate the QAPI schema on CONFIG_SPICE.
149 SpiceInfo
*qmp_query_spice(Error
**errp
)
155 void qmp_cont(Error
**errp
)
157 BlockDriverState
*bs
;
159 if (runstate_needs_reset()) {
160 error_setg(errp
, "Resetting the Virtual Machine is required");
162 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
166 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
167 bdrv_iostatus_reset(bs
);
169 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
170 if (bdrv_key_required(bs
)) {
171 error_set(errp
, QERR_DEVICE_ENCRYPTED
,
172 bdrv_get_device_name(bs
),
173 bdrv_get_encrypted_filename(bs
));
178 if (runstate_check(RUN_STATE_INMIGRATE
)) {
185 void qmp_system_wakeup(Error
**errp
)
187 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
190 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
193 bool ambiguous
= false;
194 ObjectPropertyInfoList
*props
= NULL
;
195 ObjectProperty
*prop
;
197 obj
= object_resolve_path(path
, &ambiguous
);
200 error_setg(errp
, "Path '%s' is ambiguous", path
);
202 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
207 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
208 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
210 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
214 entry
->value
->name
= g_strdup(prop
->name
);
215 entry
->value
->type
= g_strdup(prop
->type
);
221 /* FIXME: teach qapi about how to pass through Visitors */
222 int qmp_qom_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
224 const char *path
= qdict_get_str(qdict
, "path");
225 const char *property
= qdict_get_str(qdict
, "property");
226 QObject
*value
= qdict_get(qdict
, "value");
227 Error
*local_err
= NULL
;
230 obj
= object_resolve_path(path
, NULL
);
232 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
236 object_property_set_qobject(obj
, value
, property
, &local_err
);
240 qerror_report_err(local_err
);
241 error_free(local_err
);
248 int qmp_qom_get(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
250 const char *path
= qdict_get_str(qdict
, "path");
251 const char *property
= qdict_get_str(qdict
, "property");
252 Error
*local_err
= NULL
;
255 obj
= object_resolve_path(path
, NULL
);
257 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
261 *ret
= object_property_get_qobject(obj
, property
, &local_err
);
265 qerror_report_err(local_err
);
266 error_free(local_err
);
273 void qmp_set_password(const char *protocol
, const char *password
,
274 bool has_connected
, const char *connected
, Error
**errp
)
276 int disconnect_if_connected
= 0;
277 int fail_if_connected
= 0;
281 if (strcmp(connected
, "fail") == 0) {
282 fail_if_connected
= 1;
283 } else if (strcmp(connected
, "disconnect") == 0) {
284 disconnect_if_connected
= 1;
285 } else if (strcmp(connected
, "keep") == 0) {
288 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
293 if (strcmp(protocol
, "spice") == 0) {
294 if (!qemu_using_spice(errp
)) {
297 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
298 disconnect_if_connected
);
300 error_set(errp
, QERR_SET_PASSWD_FAILED
);
305 if (strcmp(protocol
, "vnc") == 0) {
306 if (fail_if_connected
|| disconnect_if_connected
) {
307 /* vnc supports "connected=keep" only */
308 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
311 /* Note that setting an empty password will not disable login through
313 rc
= vnc_display_password(NULL
, password
);
315 error_set(errp
, QERR_SET_PASSWD_FAILED
);
320 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
323 void qmp_expire_password(const char *protocol
, const char *whenstr
,
329 if (strcmp(whenstr
, "now") == 0) {
331 } else if (strcmp(whenstr
, "never") == 0) {
333 } else if (whenstr
[0] == '+') {
334 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
336 when
= strtoull(whenstr
, NULL
, 10);
339 if (strcmp(protocol
, "spice") == 0) {
340 if (!qemu_using_spice(errp
)) {
343 rc
= qemu_spice_set_pw_expire(when
);
345 error_set(errp
, QERR_SET_PASSWD_FAILED
);
350 if (strcmp(protocol
, "vnc") == 0) {
351 rc
= vnc_display_pw_expire(NULL
, when
);
353 error_set(errp
, QERR_SET_PASSWD_FAILED
);
358 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
362 void qmp_change_vnc_password(const char *password
, Error
**errp
)
364 if (vnc_display_password(NULL
, password
) < 0) {
365 error_set(errp
, QERR_SET_PASSWD_FAILED
);
369 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
371 QemuOptsList
*olist
= qemu_find_opts("vnc");
374 if (strstr(target
, "id=")) {
375 error_setg(errp
, "id not supported");
379 opts
= qemu_opts_find(olist
, "default");
383 opts
= vnc_parse_func(target
);
384 vnc_display_open("default", errp
);
387 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
390 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
392 error_set(errp
, QERR_MISSING_PARAMETER
, "password");
394 qmp_change_vnc_password(arg
, errp
);
397 qmp_change_vnc_listen(target
, errp
);
401 void qmp_change_vnc_password(const char *password
, Error
**errp
)
403 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
405 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
408 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
410 #endif /* !CONFIG_VNC */
412 void qmp_change(const char *device
, const char *target
,
413 bool has_arg
, const char *arg
, Error
**errp
)
415 if (strcmp(device
, "vnc") == 0) {
416 qmp_change_vnc(target
, has_arg
, arg
, errp
);
418 qmp_change_blockdev(device
, target
, arg
, errp
);
422 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
424 ObjectTypeInfoList
*e
, **pret
= data
;
425 ObjectTypeInfo
*info
;
427 info
= g_malloc0(sizeof(*info
));
428 info
->name
= g_strdup(object_class_get_name(klass
));
430 e
= g_malloc0(sizeof(*e
));
436 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
437 const char *implements
,
442 ObjectTypeInfoList
*ret
= NULL
;
444 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
449 /* Return a DevicePropertyInfo for a qdev property.
451 * If a qdev property with the given name does not exist, use the given default
452 * type. If the qdev property info should not be shown, return NULL.
454 * The caller must free the return value.
456 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
458 const char *default_type
,
459 const char *description
)
461 DevicePropertyInfo
*info
;
465 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
466 if (strcmp(name
, prop
->name
) != 0) {
471 * TODO Properties without a parser are just for dirty hacks.
472 * qdev_prop_ptr is the only such PropertyInfo. It's marked
473 * for removal. This conditional should be removed along with
476 if (!prop
->info
->set
) {
477 return NULL
; /* no way to set it, don't show */
480 info
= g_malloc0(sizeof(*info
));
481 info
->name
= g_strdup(prop
->name
);
482 info
->type
= g_strdup(prop
->info
->name
);
483 info
->has_description
= !!prop
->info
->description
;
484 info
->description
= g_strdup(prop
->info
->description
);
487 klass
= object_class_get_parent(klass
);
488 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
490 /* Not a qdev property, use the default type */
491 info
= g_malloc0(sizeof(*info
));
492 info
->name
= g_strdup(name
);
493 info
->type
= g_strdup(default_type
);
494 info
->has_description
= !!description
;
495 info
->description
= g_strdup(description
);
500 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
505 ObjectProperty
*prop
;
506 DevicePropertyInfoList
*prop_list
= NULL
;
508 klass
= object_class_by_name(typename
);
510 error_set(errp
, QERR_DEVICE_NOT_FOUND
, typename
);
514 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
516 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
517 "name", TYPE_DEVICE
);
521 obj
= object_new(typename
);
523 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
524 DevicePropertyInfo
*info
;
525 DevicePropertyInfoList
*entry
;
527 /* Skip Object and DeviceState properties */
528 if (strcmp(prop
->name
, "type") == 0 ||
529 strcmp(prop
->name
, "realized") == 0 ||
530 strcmp(prop
->name
, "hotpluggable") == 0 ||
531 strcmp(prop
->name
, "hotplugged") == 0 ||
532 strcmp(prop
->name
, "parent_bus") == 0) {
536 /* Skip legacy properties since they are just string versions of
537 * properties that we already list.
539 if (strstart(prop
->name
, "legacy-", NULL
)) {
543 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
549 entry
= g_malloc0(sizeof(*entry
));
551 entry
->next
= prop_list
;
560 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
562 return arch_query_cpu_definitions(errp
);
565 void qmp_add_client(const char *protocol
, const char *fdname
,
566 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
572 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
577 if (strcmp(protocol
, "spice") == 0) {
578 if (!qemu_using_spice(errp
)) {
582 skipauth
= has_skipauth
? skipauth
: false;
583 tls
= has_tls
? tls
: false;
584 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
585 error_setg(errp
, "spice failed to add client");
590 } else if (strcmp(protocol
, "vnc") == 0) {
591 skipauth
= has_skipauth
? skipauth
: false;
592 vnc_display_add_client(NULL
, fd
, skipauth
);
595 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
596 if (qemu_chr_add_client(s
, fd
) < 0) {
597 error_setg(errp
, "failed to add client");
604 error_setg(errp
, "protocol '%s' is invalid", protocol
);
608 void object_add(const char *type
, const char *id
, const QDict
*qdict
,
609 Visitor
*v
, Error
**errp
)
614 Error
*local_err
= NULL
;
616 klass
= object_class_by_name(type
);
618 error_setg(errp
, "invalid object type: %s", type
);
622 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
623 error_setg(errp
, "object type '%s' isn't supported by object-add",
628 if (object_class_is_abstract(klass
)) {
629 error_setg(errp
, "object type '%s' is abstract", type
);
633 obj
= object_new(type
);
635 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
636 object_property_set(obj
, v
, e
->key
, &local_err
);
643 object_property_add_child(container_get(object_get_root(), "/objects"),
644 id
, obj
, &local_err
);
649 user_creatable_complete(obj
, &local_err
);
651 object_property_del(container_get(object_get_root(), "/objects"),
657 error_propagate(errp
, local_err
);
662 int qmp_object_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
664 const char *type
= qdict_get_str(qdict
, "qom-type");
665 const char *id
= qdict_get_str(qdict
, "id");
666 QObject
*props
= qdict_get(qdict
, "props");
667 const QDict
*pdict
= NULL
;
668 Error
*local_err
= NULL
;
669 QmpInputVisitor
*qiv
;
672 pdict
= qobject_to_qdict(props
);
674 error_set(&local_err
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
679 qiv
= qmp_input_visitor_new(props
);
680 object_add(type
, id
, pdict
, qmp_input_get_visitor(qiv
), &local_err
);
681 qmp_input_visitor_cleanup(qiv
);
685 qerror_report_err(local_err
);
686 error_free(local_err
);
693 void qmp_object_del(const char *id
, Error
**errp
)
698 container
= container_get(object_get_root(), "/objects");
699 obj
= object_resolve_path_component(container
, id
);
701 error_setg(errp
, "object id not found");
704 object_unparent(obj
);
707 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
709 MemoryDeviceInfoList
*head
= NULL
;
710 MemoryDeviceInfoList
**prev
= &head
;
712 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
717 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
720 ACPIOSTInfoList
*head
= NULL
;
721 ACPIOSTInfoList
**prev
= &head
;
722 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
725 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
726 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
728 adevc
->ospm_status(adev
, &prev
);
730 error_setg(errp
, "command is not supported, missing ACPI device");