2 * QEMU Management Protocol commands
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
)
147 Error
*local_err
= NULL
;
149 /* if there is a dump in background, we should wait until the dump
151 if (dump_in_progress()) {
152 error_setg(errp
, "There is a dump in process, please wait.");
156 if (runstate_needs_reset()) {
157 error_setg(errp
, "Resetting the Virtual Machine is required");
159 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
161 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
162 error_setg(errp
, "Migration is not finalized yet");
166 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
167 blk_iostatus_reset(blk
);
170 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
171 block_job_iostatus_reset(job
);
174 /* Continuing after completed migration. Images have been inactivated to
175 * allow the destination to take control. Need to get control back now.
177 * If there are no inactive block nodes (e.g. because the VM was just
178 * paused rather than completing a migration), bdrv_inactivate_all() simply
179 * doesn't do anything. */
180 bdrv_invalidate_cache_all(&local_err
);
182 error_propagate(errp
, local_err
);
186 if (runstate_check(RUN_STATE_INMIGRATE
)) {
193 void qmp_system_wakeup(Error
**errp
)
195 if (!qemu_wakeup_suspend_enabled()) {
197 "wake-up from suspend is not supported by this guest");
201 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, errp
);
204 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
207 bool ambiguous
= false;
208 ObjectPropertyInfoList
*props
= NULL
;
209 ObjectProperty
*prop
;
210 ObjectPropertyIterator iter
;
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 object_property_iter_init(&iter
, obj
);
224 while ((prop
= object_property_iter_next(&iter
))) {
225 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
227 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
231 entry
->value
->name
= g_strdup(prop
->name
);
232 entry
->value
->type
= g_strdup(prop
->type
);
238 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
243 obj
= object_resolve_path(path
, NULL
);
245 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
246 "Device '%s' not found", path
);
250 object_property_set_qobject(obj
, value
, property
, errp
);
253 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
257 obj
= object_resolve_path(path
, NULL
);
259 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
260 "Device '%s' not found", path
);
264 return object_property_get_qobject(obj
, property
, errp
);
267 void qmp_set_password(const char *protocol
, const char *password
,
268 bool has_connected
, const char *connected
, Error
**errp
)
270 int disconnect_if_connected
= 0;
271 int fail_if_connected
= 0;
275 if (strcmp(connected
, "fail") == 0) {
276 fail_if_connected
= 1;
277 } else if (strcmp(connected
, "disconnect") == 0) {
278 disconnect_if_connected
= 1;
279 } else if (strcmp(connected
, "keep") == 0) {
282 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
287 if (strcmp(protocol
, "spice") == 0) {
288 if (!qemu_using_spice(errp
)) {
291 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
292 disconnect_if_connected
);
294 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
299 if (strcmp(protocol
, "vnc") == 0) {
300 if (fail_if_connected
|| disconnect_if_connected
) {
301 /* vnc supports "connected=keep" only */
302 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
305 /* Note that setting an empty password will not disable login through
307 rc
= vnc_display_password(NULL
, password
);
309 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
314 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
317 void qmp_expire_password(const char *protocol
, const char *whenstr
,
323 if (strcmp(whenstr
, "now") == 0) {
325 } else if (strcmp(whenstr
, "never") == 0) {
327 } else if (whenstr
[0] == '+') {
328 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
330 when
= strtoull(whenstr
, NULL
, 10);
333 if (strcmp(protocol
, "spice") == 0) {
334 if (!qemu_using_spice(errp
)) {
337 rc
= qemu_spice_set_pw_expire(when
);
339 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
344 if (strcmp(protocol
, "vnc") == 0) {
345 rc
= vnc_display_pw_expire(NULL
, when
);
347 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
352 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
356 void qmp_change_vnc_password(const char *password
, Error
**errp
)
358 if (vnc_display_password(NULL
, password
) < 0) {
359 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
363 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
365 QemuOptsList
*olist
= qemu_find_opts("vnc");
368 if (strstr(target
, "id=")) {
369 error_setg(errp
, "id not supported");
373 opts
= qemu_opts_find(olist
, "default");
377 opts
= vnc_parse(target
, errp
);
382 vnc_display_open("default", errp
);
385 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
388 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
390 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
392 qmp_change_vnc_password(arg
, errp
);
395 qmp_change_vnc_listen(target
, errp
);
398 #endif /* !CONFIG_VNC */
400 void qmp_change(const char *device
, const char *target
,
401 bool has_arg
, const char *arg
, Error
**errp
)
403 if (strcmp(device
, "vnc") == 0) {
405 qmp_change_vnc(target
, has_arg
, arg
, errp
);
407 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
410 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
411 has_arg
, arg
, false, 0, errp
);
415 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
417 ObjectTypeInfoList
*e
, **pret
= data
;
418 ObjectTypeInfo
*info
;
419 ObjectClass
*parent
= object_class_get_parent(klass
);
421 info
= g_malloc0(sizeof(*info
));
422 info
->name
= g_strdup(object_class_get_name(klass
));
423 info
->has_abstract
= info
->abstract
= object_class_is_abstract(klass
);
425 info
->has_parent
= true;
426 info
->parent
= g_strdup(object_class_get_name(parent
));
429 e
= g_malloc0(sizeof(*e
));
435 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
436 const char *implements
,
441 ObjectTypeInfoList
*ret
= NULL
;
443 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
448 /* Return a DevicePropertyInfo for a qdev property.
450 * If a qdev property with the given name does not exist, use the given default
451 * type. If the qdev property info should not be shown, return NULL.
453 * The caller must free the return value.
455 static ObjectPropertyInfo
*make_device_property_info(ObjectClass
*klass
,
457 const char *default_type
,
458 const char *description
)
460 ObjectPropertyInfo
*info
;
464 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
465 if (strcmp(name
, prop
->name
) != 0) {
470 * TODO Properties without a parser are just for dirty hacks.
471 * qdev_prop_ptr is the only such PropertyInfo. It's marked
472 * for removal. This conditional should be removed along with
475 if (!prop
->info
->set
&& !prop
->info
->create
) {
476 return NULL
; /* no way to set it, don't show */
479 info
= g_malloc0(sizeof(*info
));
480 info
->name
= g_strdup(prop
->name
);
481 info
->type
= default_type
? g_strdup(default_type
)
482 : 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 ObjectPropertyInfoList
*qmp_device_list_properties(const char *typename
,
505 ObjectProperty
*prop
;
506 ObjectPropertyIterator iter
;
507 ObjectPropertyInfoList
*prop_list
= NULL
;
509 klass
= object_class_by_name(typename
);
511 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
512 "Device '%s' not found", typename
);
516 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
518 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_DEVICE
);
522 if (object_class_is_abstract(klass
)) {
523 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename",
524 "non-abstract device type");
528 obj
= object_new(typename
);
530 object_property_iter_init(&iter
, obj
);
531 while ((prop
= object_property_iter_next(&iter
))) {
532 ObjectPropertyInfo
*info
;
533 ObjectPropertyInfoList
*entry
;
535 /* Skip Object and DeviceState properties */
536 if (strcmp(prop
->name
, "type") == 0 ||
537 strcmp(prop
->name
, "realized") == 0 ||
538 strcmp(prop
->name
, "hotpluggable") == 0 ||
539 strcmp(prop
->name
, "hotplugged") == 0 ||
540 strcmp(prop
->name
, "parent_bus") == 0) {
544 /* Skip legacy properties since they are just string versions of
545 * properties that we already list.
547 if (strstart(prop
->name
, "legacy-", NULL
)) {
551 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
557 entry
= g_malloc0(sizeof(*entry
));
559 entry
->next
= prop_list
;
568 ObjectPropertyInfoList
*qmp_qom_list_properties(const char *typename
,
573 ObjectProperty
*prop
;
574 ObjectPropertyIterator iter
;
575 ObjectPropertyInfoList
*prop_list
= NULL
;
577 klass
= object_class_by_name(typename
);
579 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
580 "Class '%s' not found", typename
);
584 klass
= object_class_dynamic_cast(klass
, TYPE_OBJECT
);
586 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_OBJECT
);
590 if (object_class_is_abstract(klass
)) {
591 object_class_property_iter_init(&iter
, klass
);
593 obj
= object_new(typename
);
594 object_property_iter_init(&iter
, obj
);
596 while ((prop
= object_property_iter_next(&iter
))) {
597 ObjectPropertyInfo
*info
;
598 ObjectPropertyInfoList
*entry
;
600 info
= g_malloc0(sizeof(*info
));
601 info
->name
= g_strdup(prop
->name
);
602 info
->type
= g_strdup(prop
->type
);
603 info
->has_description
= !!prop
->description
;
604 info
->description
= g_strdup(prop
->description
);
606 entry
= g_malloc0(sizeof(*entry
));
608 entry
->next
= prop_list
;
617 void qmp_add_client(const char *protocol
, const char *fdname
,
618 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
624 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
629 if (strcmp(protocol
, "spice") == 0) {
630 if (!qemu_using_spice(errp
)) {
634 skipauth
= has_skipauth
? skipauth
: false;
635 tls
= has_tls
? tls
: false;
636 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
637 error_setg(errp
, "spice failed to add client");
642 } else if (strcmp(protocol
, "vnc") == 0) {
643 skipauth
= has_skipauth
? skipauth
: false;
644 vnc_display_add_client(NULL
, fd
, skipauth
);
647 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
648 if (qemu_chr_add_client(s
, fd
) < 0) {
649 error_setg(errp
, "failed to add client");
656 error_setg(errp
, "protocol '%s' is invalid", protocol
);
661 void qmp_object_add(const char *type
, const char *id
,
662 bool has_props
, QObject
*props
, Error
**errp
)
669 pdict
= qobject_to(QDict
, props
);
671 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
679 v
= qobject_input_visitor_new(QOBJECT(pdict
));
680 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
685 qobject_unref(pdict
);
688 void qmp_object_del(const char *id
, Error
**errp
)
690 user_creatable_del(id
, errp
);
693 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
695 return qmp_memory_device_list();
698 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
701 ACPIOSTInfoList
*head
= NULL
;
702 ACPIOSTInfoList
**prev
= &head
;
703 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
706 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
707 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
709 adevc
->ospm_status(adev
, &prev
);
711 error_setg(errp
, "command is not supported, missing ACPI device");
717 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
719 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
721 mem_info
->base_memory
= ram_size
;
723 mem_info
->plugged_memory
= get_plugged_memory_size();
724 mem_info
->has_plugged_memory
=
725 mem_info
->plugged_memory
!= (uint64_t)-1;