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-version.h"
18 #include "qemu/cutils.h"
19 #include "monitor/monitor.h"
20 #include "sysemu/sysemu.h"
21 #include "qemu/uuid.h"
22 #include "qmp-commands.h"
23 #include "sysemu/char.h"
24 #include "ui/qemu-spice.h"
26 #include "sysemu/kvm.h"
27 #include "sysemu/arch_init.h"
29 #include "sysemu/blockdev.h"
30 #include "sysemu/block-backend.h"
31 #include "qom/qom-qobject.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/qmp/qobject.h"
34 #include "qapi/qmp-input-visitor.h"
35 #include "hw/boards.h"
36 #include "qom/object_interfaces.h"
37 #include "hw/mem/pc-dimm.h"
38 #include "hw/acpi/acpi_dev_interface.h"
39 #include "qemu/uuid.h"
41 NameInfo
*qmp_query_name(Error
**errp
)
43 NameInfo
*info
= g_malloc0(sizeof(*info
));
46 info
->has_name
= true;
47 info
->name
= g_strdup(qemu_name
);
53 VersionInfo
*qmp_query_version(Error
**errp
)
55 VersionInfo
*info
= g_new0(VersionInfo
, 1);
57 info
->qemu
= g_new0(VersionTriple
, 1);
58 info
->qemu
->major
= QEMU_VERSION_MAJOR
;
59 info
->qemu
->minor
= QEMU_VERSION_MINOR
;
60 info
->qemu
->micro
= QEMU_VERSION_MICRO
;
61 info
->package
= g_strdup(QEMU_PKGVERSION
);
66 KvmInfo
*qmp_query_kvm(Error
**errp
)
68 KvmInfo
*info
= g_malloc0(sizeof(*info
));
70 info
->enabled
= kvm_enabled();
71 info
->present
= kvm_available();
76 UuidInfo
*qmp_query_uuid(Error
**errp
)
78 UuidInfo
*info
= g_malloc0(sizeof(*info
));
80 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
84 void qmp_quit(Error
**errp
)
87 qemu_system_shutdown_request();
90 void qmp_stop(Error
**errp
)
92 /* if there is a dump in background, we should wait until the dump
94 if (dump_in_progress()) {
95 error_setg(errp
, "There is a dump in process, please wait.");
99 if (runstate_check(RUN_STATE_INMIGRATE
)) {
102 vm_stop(RUN_STATE_PAUSED
);
106 void qmp_system_reset(Error
**errp
)
108 qemu_system_reset_request();
111 void qmp_system_powerdown(Error
**erp
)
113 qemu_system_powerdown_request();
116 void qmp_cpu(int64_t index
, Error
**errp
)
118 /* Just do nothing */
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");
134 /* If VNC support is enabled, the "true" query-vnc command is
135 defined in the VNC subsystem */
136 VncInfo
*qmp_query_vnc(Error
**errp
)
138 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
142 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
144 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
151 * qmp-commands.hx ensures that QMP command query-spice exists only
152 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
153 * result. However, the QAPI schema is blissfully unaware of that,
154 * and the QAPI code generator happily generates a dead
155 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
156 * one, or else linking fails. FIXME Educate the QAPI schema on
159 SpiceInfo
*qmp_query_spice(Error
**errp
)
165 void qmp_cont(Error
**errp
)
167 Error
*local_err
= NULL
;
169 BlockDriverState
*bs
;
172 /* if there is a dump in background, we should wait until the dump
174 if (dump_in_progress()) {
175 error_setg(errp
, "There is a dump in process, please wait.");
179 if (runstate_needs_reset()) {
180 error_setg(errp
, "Resetting the Virtual Machine is required");
182 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
186 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
187 blk_iostatus_reset(blk
);
190 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
191 bdrv_add_key(bs
, NULL
, &local_err
);
193 error_propagate(errp
, local_err
);
198 /* Continuing after completed migration. Images have been inactivated to
199 * allow the destination to take control. Need to get control back now. */
200 if (runstate_check(RUN_STATE_FINISH_MIGRATE
) ||
201 runstate_check(RUN_STATE_POSTMIGRATE
))
203 bdrv_invalidate_cache_all(&local_err
);
205 error_propagate(errp
, local_err
);
210 if (runstate_check(RUN_STATE_INMIGRATE
)) {
217 void qmp_system_wakeup(Error
**errp
)
219 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
222 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
225 bool ambiguous
= false;
226 ObjectPropertyInfoList
*props
= NULL
;
227 ObjectProperty
*prop
;
228 ObjectPropertyIterator iter
;
230 obj
= object_resolve_path(path
, &ambiguous
);
233 error_setg(errp
, "Path '%s' is ambiguous", path
);
235 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
236 "Device '%s' not found", path
);
241 object_property_iter_init(&iter
, obj
);
242 while ((prop
= object_property_iter_next(&iter
))) {
243 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
245 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
249 entry
->value
->name
= g_strdup(prop
->name
);
250 entry
->value
->type
= g_strdup(prop
->type
);
256 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
261 obj
= object_resolve_path(path
, NULL
);
263 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
264 "Device '%s' not found", path
);
268 object_property_set_qobject(obj
, value
, property
, errp
);
271 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
275 obj
= object_resolve_path(path
, NULL
);
277 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
278 "Device '%s' not found", path
);
282 return object_property_get_qobject(obj
, property
, errp
);
285 void qmp_set_password(const char *protocol
, const char *password
,
286 bool has_connected
, const char *connected
, Error
**errp
)
288 int disconnect_if_connected
= 0;
289 int fail_if_connected
= 0;
293 if (strcmp(connected
, "fail") == 0) {
294 fail_if_connected
= 1;
295 } else if (strcmp(connected
, "disconnect") == 0) {
296 disconnect_if_connected
= 1;
297 } else if (strcmp(connected
, "keep") == 0) {
300 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
305 if (strcmp(protocol
, "spice") == 0) {
306 if (!qemu_using_spice(errp
)) {
309 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
310 disconnect_if_connected
);
312 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
317 if (strcmp(protocol
, "vnc") == 0) {
318 if (fail_if_connected
|| disconnect_if_connected
) {
319 /* vnc supports "connected=keep" only */
320 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
323 /* Note that setting an empty password will not disable login through
325 rc
= vnc_display_password(NULL
, password
);
327 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
332 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
335 void qmp_expire_password(const char *protocol
, const char *whenstr
,
341 if (strcmp(whenstr
, "now") == 0) {
343 } else if (strcmp(whenstr
, "never") == 0) {
345 } else if (whenstr
[0] == '+') {
346 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
348 when
= strtoull(whenstr
, NULL
, 10);
351 if (strcmp(protocol
, "spice") == 0) {
352 if (!qemu_using_spice(errp
)) {
355 rc
= qemu_spice_set_pw_expire(when
);
357 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
362 if (strcmp(protocol
, "vnc") == 0) {
363 rc
= vnc_display_pw_expire(NULL
, when
);
365 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
370 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
374 void qmp_change_vnc_password(const char *password
, Error
**errp
)
376 if (vnc_display_password(NULL
, password
) < 0) {
377 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
381 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
383 QemuOptsList
*olist
= qemu_find_opts("vnc");
386 if (strstr(target
, "id=")) {
387 error_setg(errp
, "id not supported");
391 opts
= qemu_opts_find(olist
, "default");
395 opts
= vnc_parse(target
, errp
);
400 vnc_display_open("default", errp
);
403 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
406 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
408 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
410 qmp_change_vnc_password(arg
, errp
);
413 qmp_change_vnc_listen(target
, errp
);
417 void qmp_change_vnc_password(const char *password
, Error
**errp
)
419 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
421 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
424 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
426 #endif /* !CONFIG_VNC */
428 void qmp_change(const char *device
, const char *target
,
429 bool has_arg
, const char *arg
, Error
**errp
)
431 if (strcmp(device
, "vnc") == 0) {
432 qmp_change_vnc(target
, has_arg
, arg
, errp
);
434 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
435 has_arg
, arg
, false, 0, errp
);
439 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
441 ObjectTypeInfoList
*e
, **pret
= data
;
442 ObjectTypeInfo
*info
;
444 info
= g_malloc0(sizeof(*info
));
445 info
->name
= g_strdup(object_class_get_name(klass
));
447 e
= g_malloc0(sizeof(*e
));
453 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
454 const char *implements
,
459 ObjectTypeInfoList
*ret
= NULL
;
461 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
466 /* Return a DevicePropertyInfo for a qdev property.
468 * If a qdev property with the given name does not exist, use the given default
469 * type. If the qdev property info should not be shown, return NULL.
471 * The caller must free the return value.
473 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
475 const char *default_type
,
476 const char *description
)
478 DevicePropertyInfo
*info
;
482 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
483 if (strcmp(name
, prop
->name
) != 0) {
488 * TODO Properties without a parser are just for dirty hacks.
489 * qdev_prop_ptr is the only such PropertyInfo. It's marked
490 * for removal. This conditional should be removed along with
493 if (!prop
->info
->set
) {
494 return NULL
; /* no way to set it, don't show */
497 info
= g_malloc0(sizeof(*info
));
498 info
->name
= g_strdup(prop
->name
);
499 info
->type
= g_strdup(prop
->info
->name
);
500 info
->has_description
= !!prop
->info
->description
;
501 info
->description
= g_strdup(prop
->info
->description
);
504 klass
= object_class_get_parent(klass
);
505 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
507 /* Not a qdev property, use the default type */
508 info
= g_malloc0(sizeof(*info
));
509 info
->name
= g_strdup(name
);
510 info
->type
= g_strdup(default_type
);
511 info
->has_description
= !!description
;
512 info
->description
= g_strdup(description
);
517 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
522 ObjectProperty
*prop
;
523 ObjectPropertyIterator iter
;
524 DevicePropertyInfoList
*prop_list
= NULL
;
526 klass
= object_class_by_name(typename
);
528 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
529 "Device '%s' not found", typename
);
533 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
535 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name", TYPE_DEVICE
);
539 if (object_class_is_abstract(klass
)) {
540 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name",
541 "non-abstract device type");
545 if (DEVICE_CLASS(klass
)->cannot_destroy_with_object_finalize_yet
) {
546 error_setg(errp
, "Can't list properties of device '%s'", typename
);
550 obj
= object_new(typename
);
552 object_property_iter_init(&iter
, obj
);
553 while ((prop
= object_property_iter_next(&iter
))) {
554 DevicePropertyInfo
*info
;
555 DevicePropertyInfoList
*entry
;
557 /* Skip Object and DeviceState properties */
558 if (strcmp(prop
->name
, "type") == 0 ||
559 strcmp(prop
->name
, "realized") == 0 ||
560 strcmp(prop
->name
, "hotpluggable") == 0 ||
561 strcmp(prop
->name
, "hotplugged") == 0 ||
562 strcmp(prop
->name
, "parent_bus") == 0) {
566 /* Skip legacy properties since they are just string versions of
567 * properties that we already list.
569 if (strstart(prop
->name
, "legacy-", NULL
)) {
573 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
579 entry
= g_malloc0(sizeof(*entry
));
581 entry
->next
= prop_list
;
590 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
592 return arch_query_cpu_definitions(errp
);
595 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
599 return arch_query_cpu_model_expansion(type
, model
, errp
);
602 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
603 CpuModelInfo
*modelb
,
606 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
609 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
610 CpuModelInfo
*modelb
,
613 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
616 void qmp_add_client(const char *protocol
, const char *fdname
,
617 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
623 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
628 if (strcmp(protocol
, "spice") == 0) {
629 if (!qemu_using_spice(errp
)) {
633 skipauth
= has_skipauth
? skipauth
: false;
634 tls
= has_tls
? tls
: false;
635 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
636 error_setg(errp
, "spice failed to add client");
641 } else if (strcmp(protocol
, "vnc") == 0) {
642 skipauth
= has_skipauth
? skipauth
: false;
643 vnc_display_add_client(NULL
, fd
, skipauth
);
646 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
647 if (qemu_chr_add_client(s
, fd
) < 0) {
648 error_setg(errp
, "failed to add client");
655 error_setg(errp
, "protocol '%s' is invalid", protocol
);
660 void qmp_object_add(const char *type
, const char *id
,
661 bool has_props
, QObject
*props
, Error
**errp
)
663 const QDict
*pdict
= NULL
;
668 pdict
= qobject_to_qdict(props
);
670 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
675 v
= qmp_input_visitor_new(props
, true);
676 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
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 MemoryDeviceInfoList
*head
= NULL
;
691 MemoryDeviceInfoList
**prev
= &head
;
693 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
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");