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 "qmp-commands.h"
22 #include "sysemu/char.h"
23 #include "ui/qemu-spice.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/arch_init.h"
28 #include "sysemu/blockdev.h"
29 #include "sysemu/block-backend.h"
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qobject.h"
33 #include "qapi/qmp-input-visitor.h"
34 #include "hw/boards.h"
35 #include "qom/object_interfaces.h"
36 #include "hw/mem/pc-dimm.h"
37 #include "hw/acpi/acpi_dev_interface.h"
39 NameInfo
*qmp_query_name(Error
**errp
)
41 NameInfo
*info
= g_malloc0(sizeof(*info
));
44 info
->has_name
= true;
45 info
->name
= g_strdup(qemu_name
);
51 VersionInfo
*qmp_query_version(Error
**errp
)
53 VersionInfo
*info
= g_new0(VersionInfo
, 1);
55 info
->qemu
= g_new0(VersionTriple
, 1);
56 info
->qemu
->major
= QEMU_VERSION_MAJOR
;
57 info
->qemu
->minor
= QEMU_VERSION_MINOR
;
58 info
->qemu
->micro
= QEMU_VERSION_MICRO
;
59 info
->package
= g_strdup(QEMU_PKGVERSION
);
64 KvmInfo
*qmp_query_kvm(Error
**errp
)
66 KvmInfo
*info
= g_malloc0(sizeof(*info
));
68 info
->enabled
= kvm_enabled();
69 info
->present
= kvm_available();
74 UuidInfo
*qmp_query_uuid(Error
**errp
)
76 UuidInfo
*info
= g_malloc0(sizeof(*info
));
79 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
80 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
81 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
82 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
83 qemu_uuid
[14], qemu_uuid
[15]);
85 info
->UUID
= g_strdup(uuid
);
89 void qmp_quit(Error
**errp
)
92 qemu_system_shutdown_request();
95 void qmp_stop(Error
**errp
)
97 /* if there is a dump in background, we should wait until the dump
99 if (dump_in_progress()) {
100 error_setg(errp
, "There is a dump in process, please wait.");
104 if (runstate_check(RUN_STATE_INMIGRATE
)) {
107 vm_stop(RUN_STATE_PAUSED
);
111 void qmp_system_reset(Error
**errp
)
113 qemu_system_reset_request();
116 void qmp_system_powerdown(Error
**erp
)
118 qemu_system_powerdown_request();
121 void qmp_cpu(int64_t index
, Error
**errp
)
123 /* Just do nothing */
126 void qmp_cpu_add(int64_t id
, Error
**errp
)
130 mc
= MACHINE_GET_CLASS(current_machine
);
131 if (mc
->hot_add_cpu
) {
132 mc
->hot_add_cpu(id
, errp
);
134 error_setg(errp
, "Not supported");
139 /* If VNC support is enabled, the "true" query-vnc command is
140 defined in the VNC subsystem */
141 VncInfo
*qmp_query_vnc(Error
**errp
)
143 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
147 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
149 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
156 * qmp-commands.hx ensures that QMP command query-spice exists only
157 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
158 * result. However, the QAPI schema is blissfully unaware of that,
159 * and the QAPI code generator happily generates a dead
160 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
161 * one, or else linking fails. FIXME Educate the QAPI schema on
164 SpiceInfo
*qmp_query_spice(Error
**errp
)
170 void qmp_cont(Error
**errp
)
172 Error
*local_err
= NULL
;
174 BlockDriverState
*bs
;
177 /* if there is a dump in background, we should wait until the dump
179 if (dump_in_progress()) {
180 error_setg(errp
, "There is a dump in process, please wait.");
184 if (runstate_needs_reset()) {
185 error_setg(errp
, "Resetting the Virtual Machine is required");
187 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
191 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
192 blk_iostatus_reset(blk
);
195 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
196 bdrv_add_key(bs
, NULL
, &local_err
);
198 error_propagate(errp
, local_err
);
203 /* Continuing after completed migration. Images have been inactivated to
204 * allow the destination to take control. Need to get control back now. */
205 if (runstate_check(RUN_STATE_FINISH_MIGRATE
) ||
206 runstate_check(RUN_STATE_POSTMIGRATE
))
208 bdrv_invalidate_cache_all(&local_err
);
210 error_propagate(errp
, local_err
);
215 if (runstate_check(RUN_STATE_INMIGRATE
)) {
222 void qmp_system_wakeup(Error
**errp
)
224 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
227 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
230 bool ambiguous
= false;
231 ObjectPropertyInfoList
*props
= NULL
;
232 ObjectProperty
*prop
;
233 ObjectPropertyIterator iter
;
235 obj
= object_resolve_path(path
, &ambiguous
);
238 error_setg(errp
, "Path '%s' is ambiguous", path
);
240 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
241 "Device '%s' not found", path
);
246 object_property_iter_init(&iter
, obj
);
247 while ((prop
= object_property_iter_next(&iter
))) {
248 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
250 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
254 entry
->value
->name
= g_strdup(prop
->name
);
255 entry
->value
->type
= g_strdup(prop
->type
);
261 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
266 obj
= object_resolve_path(path
, NULL
);
268 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
269 "Device '%s' not found", path
);
273 object_property_set_qobject(obj
, value
, property
, errp
);
276 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
280 obj
= object_resolve_path(path
, NULL
);
282 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
283 "Device '%s' not found", path
);
287 return object_property_get_qobject(obj
, property
, errp
);
290 void qmp_set_password(const char *protocol
, const char *password
,
291 bool has_connected
, const char *connected
, Error
**errp
)
293 int disconnect_if_connected
= 0;
294 int fail_if_connected
= 0;
298 if (strcmp(connected
, "fail") == 0) {
299 fail_if_connected
= 1;
300 } else if (strcmp(connected
, "disconnect") == 0) {
301 disconnect_if_connected
= 1;
302 } else if (strcmp(connected
, "keep") == 0) {
305 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
310 if (strcmp(protocol
, "spice") == 0) {
311 if (!qemu_using_spice(errp
)) {
314 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
315 disconnect_if_connected
);
317 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
322 if (strcmp(protocol
, "vnc") == 0) {
323 if (fail_if_connected
|| disconnect_if_connected
) {
324 /* vnc supports "connected=keep" only */
325 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
328 /* Note that setting an empty password will not disable login through
330 rc
= vnc_display_password(NULL
, password
);
332 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
337 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
340 void qmp_expire_password(const char *protocol
, const char *whenstr
,
346 if (strcmp(whenstr
, "now") == 0) {
348 } else if (strcmp(whenstr
, "never") == 0) {
350 } else if (whenstr
[0] == '+') {
351 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
353 when
= strtoull(whenstr
, NULL
, 10);
356 if (strcmp(protocol
, "spice") == 0) {
357 if (!qemu_using_spice(errp
)) {
360 rc
= qemu_spice_set_pw_expire(when
);
362 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
367 if (strcmp(protocol
, "vnc") == 0) {
368 rc
= vnc_display_pw_expire(NULL
, when
);
370 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
375 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
379 void qmp_change_vnc_password(const char *password
, Error
**errp
)
381 if (vnc_display_password(NULL
, password
) < 0) {
382 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
386 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
388 QemuOptsList
*olist
= qemu_find_opts("vnc");
391 if (strstr(target
, "id=")) {
392 error_setg(errp
, "id not supported");
396 opts
= qemu_opts_find(olist
, "default");
400 opts
= vnc_parse(target
, errp
);
405 vnc_display_open("default", errp
);
408 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
411 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
413 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
415 qmp_change_vnc_password(arg
, errp
);
418 qmp_change_vnc_listen(target
, errp
);
422 void qmp_change_vnc_password(const char *password
, Error
**errp
)
424 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
426 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
429 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
431 #endif /* !CONFIG_VNC */
433 void qmp_change(const char *device
, const char *target
,
434 bool has_arg
, const char *arg
, Error
**errp
)
436 if (strcmp(device
, "vnc") == 0) {
437 qmp_change_vnc(target
, has_arg
, arg
, errp
);
439 qmp_blockdev_change_medium(device
, target
, has_arg
, arg
, false, 0,
444 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
446 ObjectTypeInfoList
*e
, **pret
= data
;
447 ObjectTypeInfo
*info
;
449 info
= g_malloc0(sizeof(*info
));
450 info
->name
= g_strdup(object_class_get_name(klass
));
452 e
= g_malloc0(sizeof(*e
));
458 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
459 const char *implements
,
464 ObjectTypeInfoList
*ret
= NULL
;
466 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
471 /* Return a DevicePropertyInfo for a qdev property.
473 * If a qdev property with the given name does not exist, use the given default
474 * type. If the qdev property info should not be shown, return NULL.
476 * The caller must free the return value.
478 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
480 const char *default_type
,
481 const char *description
)
483 DevicePropertyInfo
*info
;
487 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
488 if (strcmp(name
, prop
->name
) != 0) {
493 * TODO Properties without a parser are just for dirty hacks.
494 * qdev_prop_ptr is the only such PropertyInfo. It's marked
495 * for removal. This conditional should be removed along with
498 if (!prop
->info
->set
) {
499 return NULL
; /* no way to set it, don't show */
502 info
= g_malloc0(sizeof(*info
));
503 info
->name
= g_strdup(prop
->name
);
504 info
->type
= g_strdup(prop
->info
->name
);
505 info
->has_description
= !!prop
->info
->description
;
506 info
->description
= g_strdup(prop
->info
->description
);
509 klass
= object_class_get_parent(klass
);
510 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
512 /* Not a qdev property, use the default type */
513 info
= g_malloc0(sizeof(*info
));
514 info
->name
= g_strdup(name
);
515 info
->type
= g_strdup(default_type
);
516 info
->has_description
= !!description
;
517 info
->description
= g_strdup(description
);
522 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
527 ObjectProperty
*prop
;
528 ObjectPropertyIterator iter
;
529 DevicePropertyInfoList
*prop_list
= NULL
;
531 klass
= object_class_by_name(typename
);
533 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
534 "Device '%s' not found", typename
);
538 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
540 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name", TYPE_DEVICE
);
544 if (object_class_is_abstract(klass
)) {
545 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name",
546 "non-abstract device type");
550 if (DEVICE_CLASS(klass
)->cannot_destroy_with_object_finalize_yet
) {
551 error_setg(errp
, "Can't list properties of device '%s'", typename
);
555 obj
= object_new(typename
);
557 object_property_iter_init(&iter
, obj
);
558 while ((prop
= object_property_iter_next(&iter
))) {
559 DevicePropertyInfo
*info
;
560 DevicePropertyInfoList
*entry
;
562 /* Skip Object and DeviceState properties */
563 if (strcmp(prop
->name
, "type") == 0 ||
564 strcmp(prop
->name
, "realized") == 0 ||
565 strcmp(prop
->name
, "hotpluggable") == 0 ||
566 strcmp(prop
->name
, "hotplugged") == 0 ||
567 strcmp(prop
->name
, "parent_bus") == 0) {
571 /* Skip legacy properties since they are just string versions of
572 * properties that we already list.
574 if (strstart(prop
->name
, "legacy-", NULL
)) {
578 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
584 entry
= g_malloc0(sizeof(*entry
));
586 entry
->next
= prop_list
;
595 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
597 return arch_query_cpu_definitions(errp
);
600 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
604 return arch_query_cpu_model_expansion(type
, model
, errp
);
607 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
608 CpuModelInfo
*modelb
,
611 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
614 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
615 CpuModelInfo
*modelb
,
618 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
621 void qmp_add_client(const char *protocol
, const char *fdname
,
622 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
628 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
633 if (strcmp(protocol
, "spice") == 0) {
634 if (!qemu_using_spice(errp
)) {
638 skipauth
= has_skipauth
? skipauth
: false;
639 tls
= has_tls
? tls
: false;
640 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
641 error_setg(errp
, "spice failed to add client");
646 } else if (strcmp(protocol
, "vnc") == 0) {
647 skipauth
= has_skipauth
? skipauth
: false;
648 vnc_display_add_client(NULL
, fd
, skipauth
);
651 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
652 if (qemu_chr_add_client(s
, fd
) < 0) {
653 error_setg(errp
, "failed to add client");
660 error_setg(errp
, "protocol '%s' is invalid", protocol
);
665 void qmp_object_add(const char *type
, const char *id
,
666 bool has_props
, QObject
*props
, Error
**errp
)
668 const QDict
*pdict
= NULL
;
673 pdict
= qobject_to_qdict(props
);
675 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
680 v
= qmp_input_visitor_new(props
, true);
681 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
688 void qmp_object_del(const char *id
, Error
**errp
)
690 user_creatable_del(id
, errp
);
693 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
695 MemoryDeviceInfoList
*head
= NULL
;
696 MemoryDeviceInfoList
**prev
= &head
;
698 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
703 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
706 ACPIOSTInfoList
*head
= NULL
;
707 ACPIOSTInfoList
**prev
= &head
;
708 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
711 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
712 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
714 adevc
->ospm_status(adev
, &prev
);
716 error_setg(errp
, "command is not supported, missing ACPI device");