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/osdep.h"
17 #include "qemu-common.h"
18 #include "qemu-version.h"
19 #include "qemu/cutils.h"
20 #include "qemu/option.h"
21 #include "monitor/monitor.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/uuid.h"
25 #include "chardev/char.h"
26 #include "ui/qemu-spice.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/arch_init.h"
31 #include "sysemu/blockdev.h"
32 #include "sysemu/block-backend.h"
33 #include "qom/qom-qobject.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-commands-block-core.h"
36 #include "qapi/qapi-commands-misc.h"
37 #include "qapi/qapi-commands-ui.h"
38 #include "qapi/qmp/qdict.h"
39 #include "qapi/qmp/qerror.h"
40 #include "qapi/qobject-input-visitor.h"
41 #include "hw/boards.h"
42 #include "qom/object_interfaces.h"
43 #include "hw/mem/memory-device.h"
44 #include "hw/acpi/acpi_dev_interface.h"
46 NameInfo
*qmp_query_name(Error
**errp
)
48 NameInfo
*info
= g_malloc0(sizeof(*info
));
51 info
->has_name
= true;
52 info
->name
= g_strdup(qemu_name
);
58 VersionInfo
*qmp_query_version(Error
**errp
)
60 VersionInfo
*info
= g_new0(VersionInfo
, 1);
62 info
->qemu
= g_new0(VersionTriple
, 1);
63 info
->qemu
->major
= QEMU_VERSION_MAJOR
;
64 info
->qemu
->minor
= QEMU_VERSION_MINOR
;
65 info
->qemu
->micro
= QEMU_VERSION_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
));
85 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
89 void qmp_quit(Error
**errp
)
92 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT
);
95 void qmp_stop(Error
**errp
)
97 /* if there is a dump in background, we should wait until the dump
99 if (dump_in_progress()) {
100 error_setg(errp
, "There is a dump in process, please wait.");
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(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
116 void qmp_system_powerdown(Error
**erp
)
118 qemu_system_powerdown_request();
121 void qmp_cpu_add(int64_t id
, Error
**errp
)
125 mc
= MACHINE_GET_CLASS(current_machine
);
126 if (mc
->hot_add_cpu
) {
127 mc
->hot_add_cpu(id
, errp
);
129 error_setg(errp
, "Not supported");
133 void qmp_x_exit_preconfig(Error
**errp
)
135 if (!runstate_check(RUN_STATE_PRECONFIG
)) {
136 error_setg(errp
, "The command is permitted only in '%s' state",
137 RunState_str(RUN_STATE_PRECONFIG
));
140 qemu_exit_preconfig_request();
143 void qmp_cont(Error
**errp
)
146 Error
*local_err
= NULL
;
148 /* if there is a dump in background, we should wait until the dump
150 if (dump_in_progress()) {
151 error_setg(errp
, "There is a dump in process, please wait.");
155 if (runstate_needs_reset()) {
156 error_setg(errp
, "Resetting the Virtual Machine is required");
158 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
160 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
161 error_setg(errp
, "Migration is not finalized yet");
165 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
166 blk_iostatus_reset(blk
);
169 /* Continuing after completed migration. Images have been inactivated to
170 * allow the destination to take control. Need to get control back now.
172 * If there are no inactive block nodes (e.g. because the VM was just
173 * paused rather than completing a migration), bdrv_inactivate_all() simply
174 * doesn't do anything. */
175 bdrv_invalidate_cache_all(&local_err
);
177 error_propagate(errp
, local_err
);
181 if (runstate_check(RUN_STATE_INMIGRATE
)) {
188 void qmp_system_wakeup(Error
**errp
)
190 if (!qemu_wakeup_suspend_enabled()) {
192 "wake-up from suspend is not supported by this guest");
196 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, errp
);
199 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
202 bool ambiguous
= false;
203 ObjectPropertyInfoList
*props
= NULL
;
204 ObjectProperty
*prop
;
205 ObjectPropertyIterator iter
;
207 obj
= object_resolve_path(path
, &ambiguous
);
210 error_setg(errp
, "Path '%s' is ambiguous", path
);
212 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
213 "Device '%s' not found", path
);
218 object_property_iter_init(&iter
, obj
);
219 while ((prop
= object_property_iter_next(&iter
))) {
220 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
222 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
226 entry
->value
->name
= g_strdup(prop
->name
);
227 entry
->value
->type
= g_strdup(prop
->type
);
233 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
238 obj
= object_resolve_path(path
, NULL
);
240 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
241 "Device '%s' not found", path
);
245 object_property_set_qobject(obj
, value
, property
, errp
);
248 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
252 obj
= object_resolve_path(path
, NULL
);
254 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
255 "Device '%s' not found", path
);
259 return object_property_get_qobject(obj
, property
, errp
);
262 void qmp_set_password(const char *protocol
, const char *password
,
263 bool has_connected
, const char *connected
, Error
**errp
)
265 int disconnect_if_connected
= 0;
266 int fail_if_connected
= 0;
270 if (strcmp(connected
, "fail") == 0) {
271 fail_if_connected
= 1;
272 } else if (strcmp(connected
, "disconnect") == 0) {
273 disconnect_if_connected
= 1;
274 } else if (strcmp(connected
, "keep") == 0) {
277 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
282 if (strcmp(protocol
, "spice") == 0) {
283 if (!qemu_using_spice(errp
)) {
286 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
287 disconnect_if_connected
);
289 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
294 if (strcmp(protocol
, "vnc") == 0) {
295 if (fail_if_connected
|| disconnect_if_connected
) {
296 /* vnc supports "connected=keep" only */
297 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
300 /* Note that setting an empty password will not disable login through
302 rc
= vnc_display_password(NULL
, password
);
304 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
309 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
312 void qmp_expire_password(const char *protocol
, const char *whenstr
,
318 if (strcmp(whenstr
, "now") == 0) {
320 } else if (strcmp(whenstr
, "never") == 0) {
322 } else if (whenstr
[0] == '+') {
323 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
325 when
= strtoull(whenstr
, NULL
, 10);
328 if (strcmp(protocol
, "spice") == 0) {
329 if (!qemu_using_spice(errp
)) {
332 rc
= qemu_spice_set_pw_expire(when
);
334 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
339 if (strcmp(protocol
, "vnc") == 0) {
340 rc
= vnc_display_pw_expire(NULL
, when
);
342 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
347 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
351 void qmp_change_vnc_password(const char *password
, Error
**errp
)
353 if (vnc_display_password(NULL
, password
) < 0) {
354 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
358 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
360 QemuOptsList
*olist
= qemu_find_opts("vnc");
363 if (strstr(target
, "id=")) {
364 error_setg(errp
, "id not supported");
368 opts
= qemu_opts_find(olist
, "default");
372 opts
= vnc_parse(target
, errp
);
377 vnc_display_open("default", errp
);
380 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
383 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
385 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
387 qmp_change_vnc_password(arg
, errp
);
390 qmp_change_vnc_listen(target
, errp
);
393 #endif /* !CONFIG_VNC */
395 void qmp_change(const char *device
, const char *target
,
396 bool has_arg
, const char *arg
, Error
**errp
)
398 if (strcmp(device
, "vnc") == 0) {
400 qmp_change_vnc(target
, has_arg
, arg
, errp
);
402 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
405 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
406 has_arg
, arg
, false, 0, errp
);
410 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
412 ObjectTypeInfoList
*e
, **pret
= data
;
413 ObjectTypeInfo
*info
;
414 ObjectClass
*parent
= object_class_get_parent(klass
);
416 info
= g_malloc0(sizeof(*info
));
417 info
->name
= g_strdup(object_class_get_name(klass
));
418 info
->has_abstract
= info
->abstract
= object_class_is_abstract(klass
);
420 info
->has_parent
= true;
421 info
->parent
= g_strdup(object_class_get_name(parent
));
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 ObjectPropertyInfo
*make_device_property_info(ObjectClass
*klass
,
452 const char *default_type
,
453 const char *description
)
455 ObjectPropertyInfo
*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
&& !prop
->info
->create
) {
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
= default_type
? g_strdup(default_type
)
477 : g_strdup(prop
->info
->name
);
478 info
->has_description
= !!prop
->info
->description
;
479 info
->description
= g_strdup(prop
->info
->description
);
482 klass
= object_class_get_parent(klass
);
483 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
485 /* Not a qdev property, use the default type */
486 info
= g_malloc0(sizeof(*info
));
487 info
->name
= g_strdup(name
);
488 info
->type
= g_strdup(default_type
);
489 info
->has_description
= !!description
;
490 info
->description
= g_strdup(description
);
495 ObjectPropertyInfoList
*qmp_device_list_properties(const char *typename
,
500 ObjectProperty
*prop
;
501 ObjectPropertyIterator iter
;
502 ObjectPropertyInfoList
*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
, "typename", TYPE_DEVICE
);
517 if (object_class_is_abstract(klass
)) {
518 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename",
519 "non-abstract device type");
523 obj
= object_new(typename
);
525 object_property_iter_init(&iter
, obj
);
526 while ((prop
= object_property_iter_next(&iter
))) {
527 ObjectPropertyInfo
*info
;
528 ObjectPropertyInfoList
*entry
;
530 /* Skip Object and DeviceState properties */
531 if (strcmp(prop
->name
, "type") == 0 ||
532 strcmp(prop
->name
, "realized") == 0 ||
533 strcmp(prop
->name
, "hotpluggable") == 0 ||
534 strcmp(prop
->name
, "hotplugged") == 0 ||
535 strcmp(prop
->name
, "parent_bus") == 0) {
539 /* Skip legacy properties since they are just string versions of
540 * properties that we already list.
542 if (strstart(prop
->name
, "legacy-", NULL
)) {
546 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
552 entry
= g_malloc0(sizeof(*entry
));
554 entry
->next
= prop_list
;
563 ObjectPropertyInfoList
*qmp_qom_list_properties(const char *typename
,
568 ObjectProperty
*prop
;
569 ObjectPropertyIterator iter
;
570 ObjectPropertyInfoList
*prop_list
= NULL
;
572 klass
= object_class_by_name(typename
);
574 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
575 "Class '%s' not found", typename
);
579 klass
= object_class_dynamic_cast(klass
, TYPE_OBJECT
);
581 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_OBJECT
);
585 if (object_class_is_abstract(klass
)) {
586 object_class_property_iter_init(&iter
, klass
);
588 obj
= object_new(typename
);
589 object_property_iter_init(&iter
, obj
);
591 while ((prop
= object_property_iter_next(&iter
))) {
592 ObjectPropertyInfo
*info
;
593 ObjectPropertyInfoList
*entry
;
595 info
= g_malloc0(sizeof(*info
));
596 info
->name
= g_strdup(prop
->name
);
597 info
->type
= g_strdup(prop
->type
);
598 info
->has_description
= !!prop
->description
;
599 info
->description
= g_strdup(prop
->description
);
601 entry
= g_malloc0(sizeof(*entry
));
603 entry
->next
= prop_list
;
612 void qmp_add_client(const char *protocol
, const char *fdname
,
613 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
619 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
624 if (strcmp(protocol
, "spice") == 0) {
625 if (!qemu_using_spice(errp
)) {
629 skipauth
= has_skipauth
? skipauth
: false;
630 tls
= has_tls
? tls
: false;
631 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
632 error_setg(errp
, "spice failed to add client");
637 } else if (strcmp(protocol
, "vnc") == 0) {
638 skipauth
= has_skipauth
? skipauth
: false;
639 vnc_display_add_client(NULL
, fd
, skipauth
);
642 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
643 if (qemu_chr_add_client(s
, fd
) < 0) {
644 error_setg(errp
, "failed to add client");
651 error_setg(errp
, "protocol '%s' is invalid", protocol
);
656 void qmp_object_add(const char *type
, const char *id
,
657 bool has_props
, QObject
*props
, Error
**errp
)
664 pdict
= qobject_to(QDict
, props
);
666 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
674 v
= qobject_input_visitor_new(QOBJECT(pdict
));
675 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
680 qobject_unref(pdict
);
683 void qmp_object_del(const char *id
, Error
**errp
)
685 user_creatable_del(id
, errp
);
688 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
690 return qmp_memory_device_list();
693 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
696 ACPIOSTInfoList
*head
= NULL
;
697 ACPIOSTInfoList
**prev
= &head
;
698 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
701 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
702 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
704 adevc
->ospm_status(adev
, &prev
);
706 error_setg(errp
, "command is not supported, missing ACPI device");
712 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
714 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
716 mem_info
->base_memory
= ram_size
;
718 mem_info
->plugged_memory
= get_plugged_memory_size();
719 mem_info
->has_plugged_memory
=
720 mem_info
->plugged_memory
!= (uint64_t)-1;