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/config-file.h"
22 #include "qemu/uuid.h"
23 #include "qmp-commands.h"
24 #include "chardev/char.h"
25 #include "ui/qemu-spice.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/arch_init.h"
30 #include "sysemu/blockdev.h"
31 #include "sysemu/block-backend.h"
32 #include "qom/qom-qobject.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qapi/qmp/qobject.h"
35 #include "qapi/qobject-input-visitor.h"
36 #include "hw/boards.h"
37 #include "qom/object_interfaces.h"
38 #include "hw/mem/pc-dimm.h"
39 #include "hw/acpi/acpi_dev_interface.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(SHUTDOWN_CAUSE_HOST_QMP
);
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(SHUTDOWN_CAUSE_HOST_QMP
);
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
)
168 Error
*local_err
= NULL
;
170 /* if there is a dump in background, we should wait until the dump
172 if (dump_in_progress()) {
173 error_setg(errp
, "There is a dump in process, please wait.");
177 if (runstate_needs_reset()) {
178 error_setg(errp
, "Resetting the Virtual Machine is required");
180 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
184 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
185 blk_iostatus_reset(blk
);
188 /* Continuing after completed migration. Images have been inactivated to
189 * allow the destination to take control. Need to get control back now.
191 * If there are no inactive block nodes (e.g. because the VM was just
192 * paused rather than completing a migration), bdrv_inactivate_all() simply
193 * doesn't do anything. */
194 bdrv_invalidate_cache_all(&local_err
);
196 error_propagate(errp
, local_err
);
200 if (runstate_check(RUN_STATE_INMIGRATE
)) {
207 void qmp_system_wakeup(Error
**errp
)
209 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
212 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
215 bool ambiguous
= false;
216 ObjectPropertyInfoList
*props
= NULL
;
217 ObjectProperty
*prop
;
218 ObjectPropertyIterator iter
;
220 obj
= object_resolve_path(path
, &ambiguous
);
223 error_setg(errp
, "Path '%s' is ambiguous", path
);
225 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
226 "Device '%s' not found", path
);
231 object_property_iter_init(&iter
, obj
);
232 while ((prop
= object_property_iter_next(&iter
))) {
233 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
235 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
239 entry
->value
->name
= g_strdup(prop
->name
);
240 entry
->value
->type
= g_strdup(prop
->type
);
246 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
251 obj
= object_resolve_path(path
, NULL
);
253 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
254 "Device '%s' not found", path
);
258 object_property_set_qobject(obj
, value
, property
, errp
);
261 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
265 obj
= object_resolve_path(path
, NULL
);
267 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
268 "Device '%s' not found", path
);
272 return object_property_get_qobject(obj
, property
, errp
);
275 void qmp_set_password(const char *protocol
, const char *password
,
276 bool has_connected
, const char *connected
, Error
**errp
)
278 int disconnect_if_connected
= 0;
279 int fail_if_connected
= 0;
283 if (strcmp(connected
, "fail") == 0) {
284 fail_if_connected
= 1;
285 } else if (strcmp(connected
, "disconnect") == 0) {
286 disconnect_if_connected
= 1;
287 } else if (strcmp(connected
, "keep") == 0) {
290 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
295 if (strcmp(protocol
, "spice") == 0) {
296 if (!qemu_using_spice(errp
)) {
299 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
300 disconnect_if_connected
);
302 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
307 if (strcmp(protocol
, "vnc") == 0) {
308 if (fail_if_connected
|| disconnect_if_connected
) {
309 /* vnc supports "connected=keep" only */
310 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
313 /* Note that setting an empty password will not disable login through
315 rc
= vnc_display_password(NULL
, password
);
317 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
322 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
325 void qmp_expire_password(const char *protocol
, const char *whenstr
,
331 if (strcmp(whenstr
, "now") == 0) {
333 } else if (strcmp(whenstr
, "never") == 0) {
335 } else if (whenstr
[0] == '+') {
336 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
338 when
= strtoull(whenstr
, NULL
, 10);
341 if (strcmp(protocol
, "spice") == 0) {
342 if (!qemu_using_spice(errp
)) {
345 rc
= qemu_spice_set_pw_expire(when
);
347 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
352 if (strcmp(protocol
, "vnc") == 0) {
353 rc
= vnc_display_pw_expire(NULL
, when
);
355 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
360 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
364 void qmp_change_vnc_password(const char *password
, Error
**errp
)
366 if (vnc_display_password(NULL
, password
) < 0) {
367 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
371 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
373 QemuOptsList
*olist
= qemu_find_opts("vnc");
376 if (strstr(target
, "id=")) {
377 error_setg(errp
, "id not supported");
381 opts
= qemu_opts_find(olist
, "default");
385 opts
= vnc_parse(target
, errp
);
390 vnc_display_open("default", errp
);
393 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
396 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
398 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
400 qmp_change_vnc_password(arg
, errp
);
403 qmp_change_vnc_listen(target
, errp
);
407 void qmp_change_vnc_password(const char *password
, Error
**errp
)
409 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
411 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
414 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
416 #endif /* !CONFIG_VNC */
418 void qmp_change(const char *device
, const char *target
,
419 bool has_arg
, const char *arg
, Error
**errp
)
421 if (strcmp(device
, "vnc") == 0) {
422 qmp_change_vnc(target
, has_arg
, arg
, errp
);
424 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
425 has_arg
, arg
, false, 0, errp
);
429 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
431 ObjectTypeInfoList
*e
, **pret
= data
;
432 ObjectTypeInfo
*info
;
434 info
= g_malloc0(sizeof(*info
));
435 info
->name
= g_strdup(object_class_get_name(klass
));
437 e
= g_malloc0(sizeof(*e
));
443 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
444 const char *implements
,
449 ObjectTypeInfoList
*ret
= NULL
;
451 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
456 /* Return a DevicePropertyInfo for a qdev property.
458 * If a qdev property with the given name does not exist, use the given default
459 * type. If the qdev property info should not be shown, return NULL.
461 * The caller must free the return value.
463 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
465 const char *default_type
,
466 const char *description
)
468 DevicePropertyInfo
*info
;
472 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
473 if (strcmp(name
, prop
->name
) != 0) {
478 * TODO Properties without a parser are just for dirty hacks.
479 * qdev_prop_ptr is the only such PropertyInfo. It's marked
480 * for removal. This conditional should be removed along with
483 if (!prop
->info
->set
&& !prop
->info
->create
) {
484 return NULL
; /* no way to set it, don't show */
487 info
= g_malloc0(sizeof(*info
));
488 info
->name
= g_strdup(prop
->name
);
489 info
->type
= default_type
? g_strdup(default_type
)
490 : g_strdup(prop
->info
->name
);
491 info
->has_description
= !!prop
->info
->description
;
492 info
->description
= g_strdup(prop
->info
->description
);
495 klass
= object_class_get_parent(klass
);
496 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
498 /* Not a qdev property, use the default type */
499 info
= g_malloc0(sizeof(*info
));
500 info
->name
= g_strdup(name
);
501 info
->type
= g_strdup(default_type
);
502 info
->has_description
= !!description
;
503 info
->description
= g_strdup(description
);
508 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
513 ObjectProperty
*prop
;
514 ObjectPropertyIterator iter
;
515 DevicePropertyInfoList
*prop_list
= NULL
;
517 klass
= object_class_by_name(typename
);
519 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
520 "Device '%s' not found", typename
);
524 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
526 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_DEVICE
);
530 if (object_class_is_abstract(klass
)) {
531 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename",
532 "non-abstract device type");
536 obj
= object_new(typename
);
538 object_property_iter_init(&iter
, obj
);
539 while ((prop
= object_property_iter_next(&iter
))) {
540 DevicePropertyInfo
*info
;
541 DevicePropertyInfoList
*entry
;
543 /* Skip Object and DeviceState properties */
544 if (strcmp(prop
->name
, "type") == 0 ||
545 strcmp(prop
->name
, "realized") == 0 ||
546 strcmp(prop
->name
, "hotpluggable") == 0 ||
547 strcmp(prop
->name
, "hotplugged") == 0 ||
548 strcmp(prop
->name
, "parent_bus") == 0) {
552 /* Skip legacy properties since they are just string versions of
553 * properties that we already list.
555 if (strstart(prop
->name
, "legacy-", NULL
)) {
559 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
565 entry
= g_malloc0(sizeof(*entry
));
567 entry
->next
= prop_list
;
576 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
578 return arch_query_cpu_definitions(errp
);
581 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
585 return arch_query_cpu_model_expansion(type
, model
, errp
);
588 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
589 CpuModelInfo
*modelb
,
592 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
595 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
596 CpuModelInfo
*modelb
,
599 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
602 void qmp_add_client(const char *protocol
, const char *fdname
,
603 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
609 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
614 if (strcmp(protocol
, "spice") == 0) {
615 if (!qemu_using_spice(errp
)) {
619 skipauth
= has_skipauth
? skipauth
: false;
620 tls
= has_tls
? tls
: false;
621 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
622 error_setg(errp
, "spice failed to add client");
627 } else if (strcmp(protocol
, "vnc") == 0) {
628 skipauth
= has_skipauth
? skipauth
: false;
629 vnc_display_add_client(NULL
, fd
, skipauth
);
632 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
633 if (qemu_chr_add_client(s
, fd
) < 0) {
634 error_setg(errp
, "failed to add client");
641 error_setg(errp
, "protocol '%s' is invalid", protocol
);
646 void qmp_object_add(const char *type
, const char *id
,
647 bool has_props
, QObject
*props
, Error
**errp
)
654 pdict
= qobject_to_qdict(props
);
656 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
664 v
= qobject_input_visitor_new(QOBJECT(pdict
));
665 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
673 void qmp_object_del(const char *id
, Error
**errp
)
675 user_creatable_del(id
, errp
);
678 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
680 MemoryDeviceInfoList
*head
= NULL
;
681 MemoryDeviceInfoList
**prev
= &head
;
683 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
688 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
691 ACPIOSTInfoList
*head
= NULL
;
692 ACPIOSTInfoList
**prev
= &head
;
693 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
696 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
697 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
699 adevc
->ospm_status(adev
, &prev
);
701 error_setg(errp
, "command is not supported, missing ACPI device");