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_add(int64_t id
, Error
**errp
)
120 mc
= MACHINE_GET_CLASS(current_machine
);
121 if (mc
->hot_add_cpu
) {
122 mc
->hot_add_cpu(id
, errp
);
124 error_setg(errp
, "Not supported");
129 /* If VNC support is enabled, the "true" query-vnc command is
130 defined in the VNC subsystem */
131 VncInfo
*qmp_query_vnc(Error
**errp
)
133 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
137 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
139 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
146 * qmp-commands.hx ensures that QMP command query-spice exists only
147 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
148 * result. However, the QAPI schema is blissfully unaware of that,
149 * and the QAPI code generator happily generates a dead
150 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
151 * one, or else linking fails. FIXME Educate the QAPI schema on
154 SpiceInfo
*qmp_query_spice(Error
**errp
)
160 void qmp_cont(Error
**errp
)
163 Error
*local_err
= NULL
;
165 /* if there is a dump in background, we should wait until the dump
167 if (dump_in_progress()) {
168 error_setg(errp
, "There is a dump in process, please wait.");
172 if (runstate_needs_reset()) {
173 error_setg(errp
, "Resetting the Virtual Machine is required");
175 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
179 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
180 blk_iostatus_reset(blk
);
183 /* Continuing after completed migration. Images have been inactivated to
184 * allow the destination to take control. Need to get control back now.
186 * If there are no inactive block nodes (e.g. because the VM was just
187 * paused rather than completing a migration), bdrv_inactivate_all() simply
188 * doesn't do anything. */
189 bdrv_invalidate_cache_all(&local_err
);
191 error_propagate(errp
, local_err
);
195 if (runstate_check(RUN_STATE_INMIGRATE
)) {
202 void qmp_system_wakeup(Error
**errp
)
204 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
207 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
210 bool ambiguous
= false;
211 ObjectPropertyInfoList
*props
= NULL
;
212 ObjectProperty
*prop
;
213 ObjectPropertyIterator iter
;
215 obj
= object_resolve_path(path
, &ambiguous
);
218 error_setg(errp
, "Path '%s' is ambiguous", path
);
220 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
221 "Device '%s' not found", path
);
226 object_property_iter_init(&iter
, obj
);
227 while ((prop
= object_property_iter_next(&iter
))) {
228 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
230 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
234 entry
->value
->name
= g_strdup(prop
->name
);
235 entry
->value
->type
= g_strdup(prop
->type
);
241 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
246 obj
= object_resolve_path(path
, NULL
);
248 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
249 "Device '%s' not found", path
);
253 object_property_set_qobject(obj
, value
, property
, errp
);
256 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
260 obj
= object_resolve_path(path
, NULL
);
262 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
263 "Device '%s' not found", path
);
267 return object_property_get_qobject(obj
, property
, errp
);
270 void qmp_set_password(const char *protocol
, const char *password
,
271 bool has_connected
, const char *connected
, Error
**errp
)
273 int disconnect_if_connected
= 0;
274 int fail_if_connected
= 0;
278 if (strcmp(connected
, "fail") == 0) {
279 fail_if_connected
= 1;
280 } else if (strcmp(connected
, "disconnect") == 0) {
281 disconnect_if_connected
= 1;
282 } else if (strcmp(connected
, "keep") == 0) {
285 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
290 if (strcmp(protocol
, "spice") == 0) {
291 if (!qemu_using_spice(errp
)) {
294 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
295 disconnect_if_connected
);
297 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
302 if (strcmp(protocol
, "vnc") == 0) {
303 if (fail_if_connected
|| disconnect_if_connected
) {
304 /* vnc supports "connected=keep" only */
305 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
308 /* Note that setting an empty password will not disable login through
310 rc
= vnc_display_password(NULL
, password
);
312 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
317 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
320 void qmp_expire_password(const char *protocol
, const char *whenstr
,
326 if (strcmp(whenstr
, "now") == 0) {
328 } else if (strcmp(whenstr
, "never") == 0) {
330 } else if (whenstr
[0] == '+') {
331 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
333 when
= strtoull(whenstr
, NULL
, 10);
336 if (strcmp(protocol
, "spice") == 0) {
337 if (!qemu_using_spice(errp
)) {
340 rc
= qemu_spice_set_pw_expire(when
);
342 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
347 if (strcmp(protocol
, "vnc") == 0) {
348 rc
= vnc_display_pw_expire(NULL
, when
);
350 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
355 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
359 void qmp_change_vnc_password(const char *password
, Error
**errp
)
361 if (vnc_display_password(NULL
, password
) < 0) {
362 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
366 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
368 QemuOptsList
*olist
= qemu_find_opts("vnc");
371 if (strstr(target
, "id=")) {
372 error_setg(errp
, "id not supported");
376 opts
= qemu_opts_find(olist
, "default");
380 opts
= vnc_parse(target
, errp
);
385 vnc_display_open("default", errp
);
388 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
391 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
393 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
395 qmp_change_vnc_password(arg
, errp
);
398 qmp_change_vnc_listen(target
, errp
);
402 void qmp_change_vnc_password(const char *password
, Error
**errp
)
404 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
406 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
409 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
411 #endif /* !CONFIG_VNC */
413 void qmp_change(const char *device
, const char *target
,
414 bool has_arg
, const char *arg
, Error
**errp
)
416 if (strcmp(device
, "vnc") == 0) {
417 qmp_change_vnc(target
, has_arg
, arg
, errp
);
419 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
420 has_arg
, arg
, false, 0, errp
);
424 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
426 ObjectTypeInfoList
*e
, **pret
= data
;
427 ObjectTypeInfo
*info
;
428 ObjectClass
*parent
= object_class_get_parent(klass
);
430 info
= g_malloc0(sizeof(*info
));
431 info
->name
= g_strdup(object_class_get_name(klass
));
432 info
->has_abstract
= info
->abstract
= object_class_is_abstract(klass
);
434 info
->has_parent
= true;
435 info
->parent
= g_strdup(object_class_get_name(parent
));
438 e
= g_malloc0(sizeof(*e
));
444 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
445 const char *implements
,
450 ObjectTypeInfoList
*ret
= NULL
;
452 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
457 /* Return a DevicePropertyInfo for a qdev property.
459 * If a qdev property with the given name does not exist, use the given default
460 * type. If the qdev property info should not be shown, return NULL.
462 * The caller must free the return value.
464 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
466 const char *default_type
,
467 const char *description
)
469 DevicePropertyInfo
*info
;
473 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
474 if (strcmp(name
, prop
->name
) != 0) {
479 * TODO Properties without a parser are just for dirty hacks.
480 * qdev_prop_ptr is the only such PropertyInfo. It's marked
481 * for removal. This conditional should be removed along with
484 if (!prop
->info
->set
&& !prop
->info
->create
) {
485 return NULL
; /* no way to set it, don't show */
488 info
= g_malloc0(sizeof(*info
));
489 info
->name
= g_strdup(prop
->name
);
490 info
->type
= default_type
? g_strdup(default_type
)
491 : g_strdup(prop
->info
->name
);
492 info
->has_description
= !!prop
->info
->description
;
493 info
->description
= g_strdup(prop
->info
->description
);
496 klass
= object_class_get_parent(klass
);
497 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
499 /* Not a qdev property, use the default type */
500 info
= g_malloc0(sizeof(*info
));
501 info
->name
= g_strdup(name
);
502 info
->type
= g_strdup(default_type
);
503 info
->has_description
= !!description
;
504 info
->description
= g_strdup(description
);
509 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
514 ObjectProperty
*prop
;
515 ObjectPropertyIterator iter
;
516 DevicePropertyInfoList
*prop_list
= NULL
;
518 klass
= object_class_by_name(typename
);
520 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
521 "Device '%s' not found", typename
);
525 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
527 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_DEVICE
);
531 if (object_class_is_abstract(klass
)) {
532 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename",
533 "non-abstract device type");
537 obj
= object_new(typename
);
539 object_property_iter_init(&iter
, obj
);
540 while ((prop
= object_property_iter_next(&iter
))) {
541 DevicePropertyInfo
*info
;
542 DevicePropertyInfoList
*entry
;
544 /* Skip Object and DeviceState properties */
545 if (strcmp(prop
->name
, "type") == 0 ||
546 strcmp(prop
->name
, "realized") == 0 ||
547 strcmp(prop
->name
, "hotpluggable") == 0 ||
548 strcmp(prop
->name
, "hotplugged") == 0 ||
549 strcmp(prop
->name
, "parent_bus") == 0) {
553 /* Skip legacy properties since they are just string versions of
554 * properties that we already list.
556 if (strstart(prop
->name
, "legacy-", NULL
)) {
560 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
566 entry
= g_malloc0(sizeof(*entry
));
568 entry
->next
= prop_list
;
577 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
579 return arch_query_cpu_definitions(errp
);
582 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
586 return arch_query_cpu_model_expansion(type
, model
, errp
);
589 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
590 CpuModelInfo
*modelb
,
593 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
596 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
597 CpuModelInfo
*modelb
,
600 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
603 void qmp_add_client(const char *protocol
, const char *fdname
,
604 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
610 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
615 if (strcmp(protocol
, "spice") == 0) {
616 if (!qemu_using_spice(errp
)) {
620 skipauth
= has_skipauth
? skipauth
: false;
621 tls
= has_tls
? tls
: false;
622 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
623 error_setg(errp
, "spice failed to add client");
628 } else if (strcmp(protocol
, "vnc") == 0) {
629 skipauth
= has_skipauth
? skipauth
: false;
630 vnc_display_add_client(NULL
, fd
, skipauth
);
633 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
634 if (qemu_chr_add_client(s
, fd
) < 0) {
635 error_setg(errp
, "failed to add client");
642 error_setg(errp
, "protocol '%s' is invalid", protocol
);
647 void qmp_object_add(const char *type
, const char *id
,
648 bool has_props
, QObject
*props
, Error
**errp
)
655 pdict
= qobject_to_qdict(props
);
657 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
665 v
= qobject_input_visitor_new(QOBJECT(pdict
));
666 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
674 void qmp_object_del(const char *id
, Error
**errp
)
676 user_creatable_del(id
, errp
);
679 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
681 MemoryDeviceInfoList
*head
= NULL
;
682 MemoryDeviceInfoList
**prev
= &head
;
684 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
689 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
692 ACPIOSTInfoList
*head
= NULL
;
693 ACPIOSTInfoList
**prev
= &head
;
694 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
697 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
698 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
700 adevc
->ospm_status(adev
, &prev
);
702 error_setg(errp
, "command is not supported, missing ACPI device");
708 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
710 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
712 mem_info
->base_memory
= ram_size
;
714 mem_info
->plugged_memory
= get_plugged_memory_size();
715 mem_info
->has_plugged_memory
=
716 mem_info
->plugged_memory
!= (uint64_t)-1;