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 "qemu/option.h"
20 #include "monitor/monitor.h"
21 #include "sysemu/sysemu.h"
22 #include "qemu/config-file.h"
23 #include "qemu/uuid.h"
24 #include "qmp-commands.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/qmp/qdict.h"
36 #include "qapi/qmp/qerror.h"
37 #include "qapi/qobject-input-visitor.h"
38 #include "hw/boards.h"
39 #include "qom/object_interfaces.h"
40 #include "hw/mem/pc-dimm.h"
41 #include "hw/acpi/acpi_dev_interface.h"
43 NameInfo
*qmp_query_name(Error
**errp
)
45 NameInfo
*info
= g_malloc0(sizeof(*info
));
48 info
->has_name
= true;
49 info
->name
= g_strdup(qemu_name
);
55 VersionInfo
*qmp_query_version(Error
**errp
)
57 VersionInfo
*info
= g_new0(VersionInfo
, 1);
59 info
->qemu
= g_new0(VersionTriple
, 1);
60 info
->qemu
->major
= QEMU_VERSION_MAJOR
;
61 info
->qemu
->minor
= QEMU_VERSION_MINOR
;
62 info
->qemu
->micro
= QEMU_VERSION_MICRO
;
63 info
->package
= g_strdup(QEMU_PKGVERSION
);
68 KvmInfo
*qmp_query_kvm(Error
**errp
)
70 KvmInfo
*info
= g_malloc0(sizeof(*info
));
72 info
->enabled
= kvm_enabled();
73 info
->present
= kvm_available();
78 UuidInfo
*qmp_query_uuid(Error
**errp
)
80 UuidInfo
*info
= g_malloc0(sizeof(*info
));
82 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
86 void qmp_quit(Error
**errp
)
89 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP
);
92 void qmp_stop(Error
**errp
)
94 /* if there is a dump in background, we should wait until the dump
96 if (dump_in_progress()) {
97 error_setg(errp
, "There is a dump in process, please wait.");
101 if (runstate_check(RUN_STATE_INMIGRATE
)) {
104 vm_stop(RUN_STATE_PAUSED
);
108 void qmp_system_reset(Error
**errp
)
110 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP
);
113 void qmp_system_powerdown(Error
**erp
)
115 qemu_system_powerdown_request();
118 void qmp_cpu_add(int64_t id
, Error
**errp
)
122 mc
= MACHINE_GET_CLASS(current_machine
);
123 if (mc
->hot_add_cpu
) {
124 mc
->hot_add_cpu(id
, errp
);
126 error_setg(errp
, "Not supported");
131 /* If VNC support is enabled, the "true" query-vnc command is
132 defined in the VNC subsystem */
133 VncInfo
*qmp_query_vnc(Error
**errp
)
135 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
139 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
141 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
148 * qmp-commands.hx ensures that QMP command query-spice exists only
149 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
150 * result. However, the QAPI schema is blissfully unaware of that,
151 * and the QAPI code generator happily generates a dead
152 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
153 * one, or else linking fails. FIXME Educate the QAPI schema on
156 SpiceInfo
*qmp_query_spice(Error
**errp
)
162 void qmp_cont(Error
**errp
)
165 Error
*local_err
= NULL
;
167 /* if there is a dump in background, we should wait until the dump
169 if (dump_in_progress()) {
170 error_setg(errp
, "There is a dump in process, please wait.");
174 if (runstate_needs_reset()) {
175 error_setg(errp
, "Resetting the Virtual Machine is required");
177 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
181 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
182 blk_iostatus_reset(blk
);
185 /* Continuing after completed migration. Images have been inactivated to
186 * allow the destination to take control. Need to get control back now.
188 * If there are no inactive block nodes (e.g. because the VM was just
189 * paused rather than completing a migration), bdrv_inactivate_all() simply
190 * doesn't do anything. */
191 bdrv_invalidate_cache_all(&local_err
);
193 error_propagate(errp
, local_err
);
197 if (runstate_check(RUN_STATE_INMIGRATE
)) {
204 void qmp_system_wakeup(Error
**errp
)
206 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
209 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
212 bool ambiguous
= false;
213 ObjectPropertyInfoList
*props
= NULL
;
214 ObjectProperty
*prop
;
215 ObjectPropertyIterator iter
;
217 obj
= object_resolve_path(path
, &ambiguous
);
220 error_setg(errp
, "Path '%s' is ambiguous", path
);
222 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
223 "Device '%s' not found", path
);
228 object_property_iter_init(&iter
, obj
);
229 while ((prop
= object_property_iter_next(&iter
))) {
230 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
232 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
236 entry
->value
->name
= g_strdup(prop
->name
);
237 entry
->value
->type
= g_strdup(prop
->type
);
243 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
248 obj
= object_resolve_path(path
, NULL
);
250 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
251 "Device '%s' not found", path
);
255 object_property_set_qobject(obj
, value
, property
, errp
);
258 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
262 obj
= object_resolve_path(path
, NULL
);
264 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
265 "Device '%s' not found", path
);
269 return object_property_get_qobject(obj
, property
, errp
);
272 void qmp_set_password(const char *protocol
, const char *password
,
273 bool has_connected
, const char *connected
, Error
**errp
)
275 int disconnect_if_connected
= 0;
276 int fail_if_connected
= 0;
280 if (strcmp(connected
, "fail") == 0) {
281 fail_if_connected
= 1;
282 } else if (strcmp(connected
, "disconnect") == 0) {
283 disconnect_if_connected
= 1;
284 } else if (strcmp(connected
, "keep") == 0) {
287 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
292 if (strcmp(protocol
, "spice") == 0) {
293 if (!qemu_using_spice(errp
)) {
296 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
297 disconnect_if_connected
);
299 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
304 if (strcmp(protocol
, "vnc") == 0) {
305 if (fail_if_connected
|| disconnect_if_connected
) {
306 /* vnc supports "connected=keep" only */
307 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
310 /* Note that setting an empty password will not disable login through
312 rc
= vnc_display_password(NULL
, password
);
314 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
319 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
322 void qmp_expire_password(const char *protocol
, const char *whenstr
,
328 if (strcmp(whenstr
, "now") == 0) {
330 } else if (strcmp(whenstr
, "never") == 0) {
332 } else if (whenstr
[0] == '+') {
333 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
335 when
= strtoull(whenstr
, NULL
, 10);
338 if (strcmp(protocol
, "spice") == 0) {
339 if (!qemu_using_spice(errp
)) {
342 rc
= qemu_spice_set_pw_expire(when
);
344 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
349 if (strcmp(protocol
, "vnc") == 0) {
350 rc
= vnc_display_pw_expire(NULL
, when
);
352 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
357 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
361 void qmp_change_vnc_password(const char *password
, Error
**errp
)
363 if (vnc_display_password(NULL
, password
) < 0) {
364 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
368 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
370 QemuOptsList
*olist
= qemu_find_opts("vnc");
373 if (strstr(target
, "id=")) {
374 error_setg(errp
, "id not supported");
378 opts
= qemu_opts_find(olist
, "default");
382 opts
= vnc_parse(target
, errp
);
387 vnc_display_open("default", errp
);
390 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
393 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
395 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
397 qmp_change_vnc_password(arg
, errp
);
400 qmp_change_vnc_listen(target
, errp
);
404 void qmp_change_vnc_password(const char *password
, Error
**errp
)
406 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
408 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
411 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
413 #endif /* !CONFIG_VNC */
415 void qmp_change(const char *device
, const char *target
,
416 bool has_arg
, const char *arg
, Error
**errp
)
418 if (strcmp(device
, "vnc") == 0) {
419 qmp_change_vnc(target
, has_arg
, arg
, errp
);
421 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
422 has_arg
, arg
, false, 0, errp
);
426 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
428 ObjectTypeInfoList
*e
, **pret
= data
;
429 ObjectTypeInfo
*info
;
430 ObjectClass
*parent
= object_class_get_parent(klass
);
432 info
= g_malloc0(sizeof(*info
));
433 info
->name
= g_strdup(object_class_get_name(klass
));
434 info
->has_abstract
= info
->abstract
= object_class_is_abstract(klass
);
436 info
->has_parent
= true;
437 info
->parent
= g_strdup(object_class_get_name(parent
));
440 e
= g_malloc0(sizeof(*e
));
446 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
447 const char *implements
,
452 ObjectTypeInfoList
*ret
= NULL
;
454 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
459 /* Return a DevicePropertyInfo for a qdev property.
461 * If a qdev property with the given name does not exist, use the given default
462 * type. If the qdev property info should not be shown, return NULL.
464 * The caller must free the return value.
466 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
468 const char *default_type
,
469 const char *description
)
471 DevicePropertyInfo
*info
;
475 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
476 if (strcmp(name
, prop
->name
) != 0) {
481 * TODO Properties without a parser are just for dirty hacks.
482 * qdev_prop_ptr is the only such PropertyInfo. It's marked
483 * for removal. This conditional should be removed along with
486 if (!prop
->info
->set
&& !prop
->info
->create
) {
487 return NULL
; /* no way to set it, don't show */
490 info
= g_malloc0(sizeof(*info
));
491 info
->name
= g_strdup(prop
->name
);
492 info
->type
= default_type
? g_strdup(default_type
)
493 : g_strdup(prop
->info
->name
);
494 info
->has_description
= !!prop
->info
->description
;
495 info
->description
= g_strdup(prop
->info
->description
);
498 klass
= object_class_get_parent(klass
);
499 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
501 /* Not a qdev property, use the default type */
502 info
= g_malloc0(sizeof(*info
));
503 info
->name
= g_strdup(name
);
504 info
->type
= g_strdup(default_type
);
505 info
->has_description
= !!description
;
506 info
->description
= g_strdup(description
);
511 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
516 ObjectProperty
*prop
;
517 ObjectPropertyIterator iter
;
518 DevicePropertyInfoList
*prop_list
= NULL
;
520 klass
= object_class_by_name(typename
);
522 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
523 "Device '%s' not found", typename
);
527 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
529 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_DEVICE
);
533 if (object_class_is_abstract(klass
)) {
534 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename",
535 "non-abstract device type");
539 obj
= object_new(typename
);
541 object_property_iter_init(&iter
, obj
);
542 while ((prop
= object_property_iter_next(&iter
))) {
543 DevicePropertyInfo
*info
;
544 DevicePropertyInfoList
*entry
;
546 /* Skip Object and DeviceState properties */
547 if (strcmp(prop
->name
, "type") == 0 ||
548 strcmp(prop
->name
, "realized") == 0 ||
549 strcmp(prop
->name
, "hotpluggable") == 0 ||
550 strcmp(prop
->name
, "hotplugged") == 0 ||
551 strcmp(prop
->name
, "parent_bus") == 0) {
555 /* Skip legacy properties since they are just string versions of
556 * properties that we already list.
558 if (strstart(prop
->name
, "legacy-", NULL
)) {
562 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
568 entry
= g_malloc0(sizeof(*entry
));
570 entry
->next
= prop_list
;
579 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
581 return arch_query_cpu_definitions(errp
);
584 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
588 return arch_query_cpu_model_expansion(type
, model
, errp
);
591 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
592 CpuModelInfo
*modelb
,
595 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
598 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
599 CpuModelInfo
*modelb
,
602 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
605 void qmp_add_client(const char *protocol
, const char *fdname
,
606 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
612 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
617 if (strcmp(protocol
, "spice") == 0) {
618 if (!qemu_using_spice(errp
)) {
622 skipauth
= has_skipauth
? skipauth
: false;
623 tls
= has_tls
? tls
: false;
624 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
625 error_setg(errp
, "spice failed to add client");
630 } else if (strcmp(protocol
, "vnc") == 0) {
631 skipauth
= has_skipauth
? skipauth
: false;
632 vnc_display_add_client(NULL
, fd
, skipauth
);
635 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
636 if (qemu_chr_add_client(s
, fd
) < 0) {
637 error_setg(errp
, "failed to add client");
644 error_setg(errp
, "protocol '%s' is invalid", protocol
);
649 void qmp_object_add(const char *type
, const char *id
,
650 bool has_props
, QObject
*props
, Error
**errp
)
657 pdict
= qobject_to_qdict(props
);
659 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
667 v
= qobject_input_visitor_new(QOBJECT(pdict
));
668 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
676 void qmp_object_del(const char *id
, Error
**errp
)
678 user_creatable_del(id
, errp
);
681 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
683 MemoryDeviceInfoList
*head
= NULL
;
684 MemoryDeviceInfoList
**prev
= &head
;
686 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
691 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
694 ACPIOSTInfoList
*head
= NULL
;
695 ACPIOSTInfoList
**prev
= &head
;
696 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
699 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
700 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
702 adevc
->ospm_status(adev
, &prev
);
704 error_setg(errp
, "command is not supported, missing ACPI device");
710 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
712 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
714 mem_info
->base_memory
= ram_size
;
716 mem_info
->plugged_memory
= get_plugged_memory_size();
717 mem_info
->has_plugged_memory
=
718 mem_info
->plugged_memory
!= (uint64_t)-1;