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 "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/error.h"
34 #include "qapi/qapi-commands-block-core.h"
35 #include "qapi/qapi-commands-misc.h"
36 #include "qapi/qapi-commands-ui.h"
37 #include "qapi/qmp/qdict.h"
38 #include "qapi/qmp/qerror.h"
39 #include "qapi/qobject-input-visitor.h"
40 #include "hw/boards.h"
41 #include "qom/object_interfaces.h"
42 #include "hw/mem/memory-device.h"
43 #include "hw/acpi/acpi_dev_interface.h"
45 NameInfo
*qmp_query_name(Error
**errp
)
47 NameInfo
*info
= g_malloc0(sizeof(*info
));
50 info
->has_name
= true;
51 info
->name
= g_strdup(qemu_name
);
57 VersionInfo
*qmp_query_version(Error
**errp
)
59 VersionInfo
*info
= g_new0(VersionInfo
, 1);
61 info
->qemu
= g_new0(VersionTriple
, 1);
62 info
->qemu
->major
= QEMU_VERSION_MAJOR
;
63 info
->qemu
->minor
= QEMU_VERSION_MINOR
;
64 info
->qemu
->micro
= QEMU_VERSION_MICRO
;
65 info
->package
= g_strdup(QEMU_PKGVERSION
);
70 KvmInfo
*qmp_query_kvm(Error
**errp
)
72 KvmInfo
*info
= g_malloc0(sizeof(*info
));
74 info
->enabled
= kvm_enabled();
75 info
->present
= kvm_available();
80 UuidInfo
*qmp_query_uuid(Error
**errp
)
82 UuidInfo
*info
= g_malloc0(sizeof(*info
));
84 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
88 void qmp_quit(Error
**errp
)
91 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP
);
94 void qmp_stop(Error
**errp
)
96 /* if there is a dump in background, we should wait until the dump
98 if (dump_in_progress()) {
99 error_setg(errp
, "There is a dump in process, please wait.");
103 if (runstate_check(RUN_STATE_INMIGRATE
)) {
106 vm_stop(RUN_STATE_PAUSED
);
110 void qmp_system_reset(Error
**errp
)
112 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP
);
115 void qmp_system_powerdown(Error
**erp
)
117 qemu_system_powerdown_request();
120 void qmp_cpu_add(int64_t id
, Error
**errp
)
124 mc
= MACHINE_GET_CLASS(current_machine
);
125 if (mc
->hot_add_cpu
) {
126 mc
->hot_add_cpu(id
, errp
);
128 error_setg(errp
, "Not supported");
132 void qmp_exit_preconfig(Error
**errp
)
134 if (!runstate_check(RUN_STATE_PRECONFIG
)) {
135 error_setg(errp
, "The command is permitted only in '%s' state",
136 RunState_str(RUN_STATE_PRECONFIG
));
139 qemu_exit_preconfig_request();
142 void qmp_cont(Error
**errp
)
145 Error
*local_err
= NULL
;
147 /* if there is a dump in background, we should wait until the dump
149 if (dump_in_progress()) {
150 error_setg(errp
, "There is a dump in process, please wait.");
154 if (runstate_needs_reset()) {
155 error_setg(errp
, "Resetting the Virtual Machine is required");
157 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
161 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
162 blk_iostatus_reset(blk
);
165 /* Continuing after completed migration. Images have been inactivated to
166 * allow the destination to take control. Need to get control back now.
168 * If there are no inactive block nodes (e.g. because the VM was just
169 * paused rather than completing a migration), bdrv_inactivate_all() simply
170 * doesn't do anything. */
171 bdrv_invalidate_cache_all(&local_err
);
173 error_propagate(errp
, local_err
);
177 if (runstate_check(RUN_STATE_INMIGRATE
)) {
184 void qmp_system_wakeup(Error
**errp
)
186 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
189 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
192 bool ambiguous
= false;
193 ObjectPropertyInfoList
*props
= NULL
;
194 ObjectProperty
*prop
;
195 ObjectPropertyIterator iter
;
197 obj
= object_resolve_path(path
, &ambiguous
);
200 error_setg(errp
, "Path '%s' is ambiguous", path
);
202 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
203 "Device '%s' not found", path
);
208 object_property_iter_init(&iter
, obj
);
209 while ((prop
= object_property_iter_next(&iter
))) {
210 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
212 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
216 entry
->value
->name
= g_strdup(prop
->name
);
217 entry
->value
->type
= g_strdup(prop
->type
);
223 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
228 obj
= object_resolve_path(path
, NULL
);
230 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
231 "Device '%s' not found", path
);
235 object_property_set_qobject(obj
, value
, property
, errp
);
238 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
242 obj
= object_resolve_path(path
, NULL
);
244 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
245 "Device '%s' not found", path
);
249 return object_property_get_qobject(obj
, property
, errp
);
252 void qmp_set_password(const char *protocol
, const char *password
,
253 bool has_connected
, const char *connected
, Error
**errp
)
255 int disconnect_if_connected
= 0;
256 int fail_if_connected
= 0;
260 if (strcmp(connected
, "fail") == 0) {
261 fail_if_connected
= 1;
262 } else if (strcmp(connected
, "disconnect") == 0) {
263 disconnect_if_connected
= 1;
264 } else if (strcmp(connected
, "keep") == 0) {
267 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
272 if (strcmp(protocol
, "spice") == 0) {
273 if (!qemu_using_spice(errp
)) {
276 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
277 disconnect_if_connected
);
279 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
284 if (strcmp(protocol
, "vnc") == 0) {
285 if (fail_if_connected
|| disconnect_if_connected
) {
286 /* vnc supports "connected=keep" only */
287 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
290 /* Note that setting an empty password will not disable login through
292 rc
= vnc_display_password(NULL
, password
);
294 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
299 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
302 void qmp_expire_password(const char *protocol
, const char *whenstr
,
308 if (strcmp(whenstr
, "now") == 0) {
310 } else if (strcmp(whenstr
, "never") == 0) {
312 } else if (whenstr
[0] == '+') {
313 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
315 when
= strtoull(whenstr
, NULL
, 10);
318 if (strcmp(protocol
, "spice") == 0) {
319 if (!qemu_using_spice(errp
)) {
322 rc
= qemu_spice_set_pw_expire(when
);
324 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
329 if (strcmp(protocol
, "vnc") == 0) {
330 rc
= vnc_display_pw_expire(NULL
, when
);
332 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
337 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
341 void qmp_change_vnc_password(const char *password
, Error
**errp
)
343 if (vnc_display_password(NULL
, password
) < 0) {
344 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
348 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
350 QemuOptsList
*olist
= qemu_find_opts("vnc");
353 if (strstr(target
, "id=")) {
354 error_setg(errp
, "id not supported");
358 opts
= qemu_opts_find(olist
, "default");
362 opts
= vnc_parse(target
, errp
);
367 vnc_display_open("default", errp
);
370 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
373 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
375 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
377 qmp_change_vnc_password(arg
, errp
);
380 qmp_change_vnc_listen(target
, errp
);
383 #endif /* !CONFIG_VNC */
385 void qmp_change(const char *device
, const char *target
,
386 bool has_arg
, const char *arg
, Error
**errp
)
388 if (strcmp(device
, "vnc") == 0) {
390 qmp_change_vnc(target
, has_arg
, arg
, errp
);
392 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
395 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
396 has_arg
, arg
, false, 0, errp
);
400 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
402 ObjectTypeInfoList
*e
, **pret
= data
;
403 ObjectTypeInfo
*info
;
404 ObjectClass
*parent
= object_class_get_parent(klass
);
406 info
= g_malloc0(sizeof(*info
));
407 info
->name
= g_strdup(object_class_get_name(klass
));
408 info
->has_abstract
= info
->abstract
= object_class_is_abstract(klass
);
410 info
->has_parent
= true;
411 info
->parent
= g_strdup(object_class_get_name(parent
));
414 e
= g_malloc0(sizeof(*e
));
420 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
421 const char *implements
,
426 ObjectTypeInfoList
*ret
= NULL
;
428 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
433 /* Return a DevicePropertyInfo for a qdev property.
435 * If a qdev property with the given name does not exist, use the given default
436 * type. If the qdev property info should not be shown, return NULL.
438 * The caller must free the return value.
440 static ObjectPropertyInfo
*make_device_property_info(ObjectClass
*klass
,
442 const char *default_type
,
443 const char *description
)
445 ObjectPropertyInfo
*info
;
449 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
450 if (strcmp(name
, prop
->name
) != 0) {
455 * TODO Properties without a parser are just for dirty hacks.
456 * qdev_prop_ptr is the only such PropertyInfo. It's marked
457 * for removal. This conditional should be removed along with
460 if (!prop
->info
->set
&& !prop
->info
->create
) {
461 return NULL
; /* no way to set it, don't show */
464 info
= g_malloc0(sizeof(*info
));
465 info
->name
= g_strdup(prop
->name
);
466 info
->type
= default_type
? g_strdup(default_type
)
467 : g_strdup(prop
->info
->name
);
468 info
->has_description
= !!prop
->info
->description
;
469 info
->description
= g_strdup(prop
->info
->description
);
472 klass
= object_class_get_parent(klass
);
473 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
475 /* Not a qdev property, use the default type */
476 info
= g_malloc0(sizeof(*info
));
477 info
->name
= g_strdup(name
);
478 info
->type
= g_strdup(default_type
);
479 info
->has_description
= !!description
;
480 info
->description
= g_strdup(description
);
485 ObjectPropertyInfoList
*qmp_device_list_properties(const char *typename
,
490 ObjectProperty
*prop
;
491 ObjectPropertyIterator iter
;
492 ObjectPropertyInfoList
*prop_list
= NULL
;
494 klass
= object_class_by_name(typename
);
496 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
497 "Device '%s' not found", typename
);
501 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
503 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_DEVICE
);
507 if (object_class_is_abstract(klass
)) {
508 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename",
509 "non-abstract device type");
513 obj
= object_new(typename
);
515 object_property_iter_init(&iter
, obj
);
516 while ((prop
= object_property_iter_next(&iter
))) {
517 ObjectPropertyInfo
*info
;
518 ObjectPropertyInfoList
*entry
;
520 /* Skip Object and DeviceState properties */
521 if (strcmp(prop
->name
, "type") == 0 ||
522 strcmp(prop
->name
, "realized") == 0 ||
523 strcmp(prop
->name
, "hotpluggable") == 0 ||
524 strcmp(prop
->name
, "hotplugged") == 0 ||
525 strcmp(prop
->name
, "parent_bus") == 0) {
529 /* Skip legacy properties since they are just string versions of
530 * properties that we already list.
532 if (strstart(prop
->name
, "legacy-", NULL
)) {
536 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
542 entry
= g_malloc0(sizeof(*entry
));
544 entry
->next
= prop_list
;
553 ObjectPropertyInfoList
*qmp_qom_list_properties(const char *typename
,
558 ObjectProperty
*prop
;
559 ObjectPropertyIterator iter
;
560 ObjectPropertyInfoList
*prop_list
= NULL
;
562 klass
= object_class_by_name(typename
);
564 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
565 "Class '%s' not found", typename
);
569 klass
= object_class_dynamic_cast(klass
, TYPE_OBJECT
);
571 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "typename", TYPE_OBJECT
);
575 if (object_class_is_abstract(klass
)) {
576 object_class_property_iter_init(&iter
, klass
);
578 obj
= object_new(typename
);
579 object_property_iter_init(&iter
, obj
);
581 while ((prop
= object_property_iter_next(&iter
))) {
582 ObjectPropertyInfo
*info
;
583 ObjectPropertyInfoList
*entry
;
585 info
= g_malloc0(sizeof(*info
));
586 info
->name
= g_strdup(prop
->name
);
587 info
->type
= g_strdup(prop
->type
);
588 info
->has_description
= !!prop
->description
;
589 info
->description
= g_strdup(prop
->description
);
591 entry
= g_malloc0(sizeof(*entry
));
593 entry
->next
= prop_list
;
602 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
604 return arch_query_cpu_definitions(errp
);
607 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
611 return arch_query_cpu_model_expansion(type
, model
, errp
);
614 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
615 CpuModelInfo
*modelb
,
618 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
621 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
622 CpuModelInfo
*modelb
,
625 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
628 void qmp_add_client(const char *protocol
, const char *fdname
,
629 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
635 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
640 if (strcmp(protocol
, "spice") == 0) {
641 if (!qemu_using_spice(errp
)) {
645 skipauth
= has_skipauth
? skipauth
: false;
646 tls
= has_tls
? tls
: false;
647 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
648 error_setg(errp
, "spice failed to add client");
653 } else if (strcmp(protocol
, "vnc") == 0) {
654 skipauth
= has_skipauth
? skipauth
: false;
655 vnc_display_add_client(NULL
, fd
, skipauth
);
658 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
659 if (qemu_chr_add_client(s
, fd
) < 0) {
660 error_setg(errp
, "failed to add client");
667 error_setg(errp
, "protocol '%s' is invalid", protocol
);
672 void qmp_object_add(const char *type
, const char *id
,
673 bool has_props
, QObject
*props
, Error
**errp
)
680 pdict
= qobject_to(QDict
, props
);
682 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
690 v
= qobject_input_visitor_new(QOBJECT(pdict
));
691 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
696 qobject_unref(pdict
);
699 void qmp_object_del(const char *id
, Error
**errp
)
701 user_creatable_del(id
, errp
);
704 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
706 return qmp_memory_device_list();
709 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
712 ACPIOSTInfoList
*head
= NULL
;
713 ACPIOSTInfoList
**prev
= &head
;
714 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
717 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
718 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
720 adevc
->ospm_status(adev
, &prev
);
722 error_setg(errp
, "command is not supported, missing ACPI device");
728 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
730 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
732 mem_info
->base_memory
= ram_size
;
734 mem_info
->plugged_memory
= get_plugged_memory_size();
735 mem_info
->has_plugged_memory
=
736 mem_info
->plugged_memory
!= (uint64_t)-1;