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/cutils.h"
18 #include "monitor/monitor.h"
19 #include "sysemu/sysemu.h"
20 #include "qmp-commands.h"
21 #include "sysemu/char.h"
22 #include "ui/qemu-spice.h"
24 #include "sysemu/kvm.h"
25 #include "sysemu/arch_init.h"
27 #include "sysemu/blockdev.h"
28 #include "sysemu/block-backend.h"
29 #include "qom/qom-qobject.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qobject.h"
32 #include "qapi/qmp-input-visitor.h"
33 #include "hw/boards.h"
34 #include "qom/object_interfaces.h"
35 #include "hw/mem/pc-dimm.h"
36 #include "hw/acpi/acpi_dev_interface.h"
38 NameInfo
*qmp_query_name(Error
**errp
)
40 NameInfo
*info
= g_malloc0(sizeof(*info
));
43 info
->has_name
= true;
44 info
->name
= g_strdup(qemu_name
);
50 VersionInfo
*qmp_query_version(Error
**errp
)
52 VersionInfo
*info
= g_new0(VersionInfo
, 1);
53 const char *version
= QEMU_VERSION
;
57 info
->qemu
= g_new0(VersionTriple
, 1);
58 err
= qemu_strtoll(version
, &tmp
, 10, &info
->qemu
->major
);
62 err
= qemu_strtoll(tmp
, &tmp
, 10, &info
->qemu
->minor
);
66 err
= qemu_strtoll(tmp
, &tmp
, 10, &info
->qemu
->micro
);
68 info
->package
= g_strdup(QEMU_PKGVERSION
);
73 KvmInfo
*qmp_query_kvm(Error
**errp
)
75 KvmInfo
*info
= g_malloc0(sizeof(*info
));
77 info
->enabled
= kvm_enabled();
78 info
->present
= kvm_available();
83 UuidInfo
*qmp_query_uuid(Error
**errp
)
85 UuidInfo
*info
= g_malloc0(sizeof(*info
));
88 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
89 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
90 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
91 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
92 qemu_uuid
[14], qemu_uuid
[15]);
94 info
->UUID
= g_strdup(uuid
);
98 void qmp_quit(Error
**errp
)
101 qemu_system_shutdown_request();
104 void qmp_stop(Error
**errp
)
106 /* if there is a dump in background, we should wait until the dump
108 if (dump_in_progress()) {
109 error_setg(errp
, "There is a dump in process, please wait.");
113 if (runstate_check(RUN_STATE_INMIGRATE
)) {
116 vm_stop(RUN_STATE_PAUSED
);
120 void qmp_system_reset(Error
**errp
)
122 qemu_system_reset_request();
125 void qmp_system_powerdown(Error
**erp
)
127 qemu_system_powerdown_request();
130 void qmp_cpu(int64_t index
, Error
**errp
)
132 /* Just do nothing */
135 void qmp_cpu_add(int64_t id
, Error
**errp
)
139 mc
= MACHINE_GET_CLASS(current_machine
);
140 if (mc
->hot_add_cpu
) {
141 mc
->hot_add_cpu(id
, errp
);
143 error_setg(errp
, "Not supported");
148 /* If VNC support is enabled, the "true" query-vnc command is
149 defined in the VNC subsystem */
150 VncInfo
*qmp_query_vnc(Error
**errp
)
152 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
156 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
158 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
165 * qmp-commands.hx ensures that QMP command query-spice exists only
166 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
167 * result. However, the QAPI schema is blissfully unaware of that,
168 * and the QAPI code generator happily generates a dead
169 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
170 * one, or else linking fails. FIXME Educate the QAPI schema on
173 SpiceInfo
*qmp_query_spice(Error
**errp
)
179 void qmp_cont(Error
**errp
)
181 Error
*local_err
= NULL
;
183 BlockDriverState
*bs
;
185 /* if there is a dump in background, we should wait until the dump
187 if (dump_in_progress()) {
188 error_setg(errp
, "There is a dump in process, please wait.");
192 if (runstate_needs_reset()) {
193 error_setg(errp
, "Resetting the Virtual Machine is required");
195 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
199 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
200 blk_iostatus_reset(blk
);
202 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
203 bdrv_add_key(bs
, NULL
, &local_err
);
205 error_propagate(errp
, local_err
);
210 /* Continuing after completed migration. Images have been inactivated to
211 * allow the destination to take control. Need to get control back now. */
212 if (runstate_check(RUN_STATE_FINISH_MIGRATE
) ||
213 runstate_check(RUN_STATE_POSTMIGRATE
))
215 bdrv_invalidate_cache_all(&local_err
);
217 error_propagate(errp
, local_err
);
222 if (runstate_check(RUN_STATE_INMIGRATE
)) {
229 void qmp_system_wakeup(Error
**errp
)
231 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
234 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
237 bool ambiguous
= false;
238 ObjectPropertyInfoList
*props
= NULL
;
239 ObjectProperty
*prop
;
240 ObjectPropertyIterator iter
;
242 obj
= object_resolve_path(path
, &ambiguous
);
245 error_setg(errp
, "Path '%s' is ambiguous", path
);
247 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
248 "Device '%s' not found", path
);
253 object_property_iter_init(&iter
, obj
);
254 while ((prop
= object_property_iter_next(&iter
))) {
255 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
257 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
261 entry
->value
->name
= g_strdup(prop
->name
);
262 entry
->value
->type
= g_strdup(prop
->type
);
268 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
273 obj
= object_resolve_path(path
, NULL
);
275 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
276 "Device '%s' not found", path
);
280 object_property_set_qobject(obj
, value
, property
, errp
);
283 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
287 obj
= object_resolve_path(path
, NULL
);
289 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
290 "Device '%s' not found", path
);
294 return object_property_get_qobject(obj
, property
, errp
);
297 void qmp_set_password(const char *protocol
, const char *password
,
298 bool has_connected
, const char *connected
, Error
**errp
)
300 int disconnect_if_connected
= 0;
301 int fail_if_connected
= 0;
305 if (strcmp(connected
, "fail") == 0) {
306 fail_if_connected
= 1;
307 } else if (strcmp(connected
, "disconnect") == 0) {
308 disconnect_if_connected
= 1;
309 } else if (strcmp(connected
, "keep") == 0) {
312 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
317 if (strcmp(protocol
, "spice") == 0) {
318 if (!qemu_using_spice(errp
)) {
321 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
322 disconnect_if_connected
);
324 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
329 if (strcmp(protocol
, "vnc") == 0) {
330 if (fail_if_connected
|| disconnect_if_connected
) {
331 /* vnc supports "connected=keep" only */
332 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
335 /* Note that setting an empty password will not disable login through
337 rc
= vnc_display_password(NULL
, password
);
339 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
344 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
347 void qmp_expire_password(const char *protocol
, const char *whenstr
,
353 if (strcmp(whenstr
, "now") == 0) {
355 } else if (strcmp(whenstr
, "never") == 0) {
357 } else if (whenstr
[0] == '+') {
358 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
360 when
= strtoull(whenstr
, NULL
, 10);
363 if (strcmp(protocol
, "spice") == 0) {
364 if (!qemu_using_spice(errp
)) {
367 rc
= qemu_spice_set_pw_expire(when
);
369 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
374 if (strcmp(protocol
, "vnc") == 0) {
375 rc
= vnc_display_pw_expire(NULL
, when
);
377 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
382 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
386 void qmp_change_vnc_password(const char *password
, Error
**errp
)
388 if (vnc_display_password(NULL
, password
) < 0) {
389 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
393 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
395 QemuOptsList
*olist
= qemu_find_opts("vnc");
398 if (strstr(target
, "id=")) {
399 error_setg(errp
, "id not supported");
403 opts
= qemu_opts_find(olist
, "default");
407 opts
= vnc_parse(target
, errp
);
412 vnc_display_open("default", errp
);
415 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
418 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
420 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
422 qmp_change_vnc_password(arg
, errp
);
425 qmp_change_vnc_listen(target
, errp
);
429 void qmp_change_vnc_password(const char *password
, Error
**errp
)
431 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
433 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
436 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
438 #endif /* !CONFIG_VNC */
440 void qmp_change(const char *device
, const char *target
,
441 bool has_arg
, const char *arg
, Error
**errp
)
443 if (strcmp(device
, "vnc") == 0) {
444 qmp_change_vnc(target
, has_arg
, arg
, errp
);
446 qmp_blockdev_change_medium(device
, target
, has_arg
, arg
, false, 0,
451 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
453 ObjectTypeInfoList
*e
, **pret
= data
;
454 ObjectTypeInfo
*info
;
456 info
= g_malloc0(sizeof(*info
));
457 info
->name
= g_strdup(object_class_get_name(klass
));
459 e
= g_malloc0(sizeof(*e
));
465 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
466 const char *implements
,
471 ObjectTypeInfoList
*ret
= NULL
;
473 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
478 /* Return a DevicePropertyInfo for a qdev property.
480 * If a qdev property with the given name does not exist, use the given default
481 * type. If the qdev property info should not be shown, return NULL.
483 * The caller must free the return value.
485 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
487 const char *default_type
,
488 const char *description
)
490 DevicePropertyInfo
*info
;
494 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
495 if (strcmp(name
, prop
->name
) != 0) {
500 * TODO Properties without a parser are just for dirty hacks.
501 * qdev_prop_ptr is the only such PropertyInfo. It's marked
502 * for removal. This conditional should be removed along with
505 if (!prop
->info
->set
) {
506 return NULL
; /* no way to set it, don't show */
509 info
= g_malloc0(sizeof(*info
));
510 info
->name
= g_strdup(prop
->name
);
511 info
->type
= g_strdup(prop
->info
->name
);
512 info
->has_description
= !!prop
->info
->description
;
513 info
->description
= g_strdup(prop
->info
->description
);
516 klass
= object_class_get_parent(klass
);
517 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
519 /* Not a qdev property, use the default type */
520 info
= g_malloc0(sizeof(*info
));
521 info
->name
= g_strdup(name
);
522 info
->type
= g_strdup(default_type
);
523 info
->has_description
= !!description
;
524 info
->description
= g_strdup(description
);
529 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
534 ObjectProperty
*prop
;
535 ObjectPropertyIterator iter
;
536 DevicePropertyInfoList
*prop_list
= NULL
;
538 klass
= object_class_by_name(typename
);
540 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
541 "Device '%s' not found", typename
);
545 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
547 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name", TYPE_DEVICE
);
551 if (object_class_is_abstract(klass
)) {
552 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name",
553 "non-abstract device type");
557 if (DEVICE_CLASS(klass
)->cannot_destroy_with_object_finalize_yet
) {
558 error_setg(errp
, "Can't list properties of device '%s'", typename
);
562 obj
= object_new(typename
);
564 object_property_iter_init(&iter
, obj
);
565 while ((prop
= object_property_iter_next(&iter
))) {
566 DevicePropertyInfo
*info
;
567 DevicePropertyInfoList
*entry
;
569 /* Skip Object and DeviceState properties */
570 if (strcmp(prop
->name
, "type") == 0 ||
571 strcmp(prop
->name
, "realized") == 0 ||
572 strcmp(prop
->name
, "hotpluggable") == 0 ||
573 strcmp(prop
->name
, "hotplugged") == 0 ||
574 strcmp(prop
->name
, "parent_bus") == 0) {
578 /* Skip legacy properties since they are just string versions of
579 * properties that we already list.
581 if (strstart(prop
->name
, "legacy-", NULL
)) {
585 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
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 void qmp_add_client(const char *protocol
, const char *fdname
,
608 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
614 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
619 if (strcmp(protocol
, "spice") == 0) {
620 if (!qemu_using_spice(errp
)) {
624 skipauth
= has_skipauth
? skipauth
: false;
625 tls
= has_tls
? tls
: false;
626 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
627 error_setg(errp
, "spice failed to add client");
632 } else if (strcmp(protocol
, "vnc") == 0) {
633 skipauth
= has_skipauth
? skipauth
: false;
634 vnc_display_add_client(NULL
, fd
, skipauth
);
637 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
638 if (qemu_chr_add_client(s
, fd
) < 0) {
639 error_setg(errp
, "failed to add client");
646 error_setg(errp
, "protocol '%s' is invalid", protocol
);
651 void qmp_object_add(const char *type
, const char *id
,
652 bool has_props
, QObject
*props
, Error
**errp
)
654 const QDict
*pdict
= NULL
;
655 QmpInputVisitor
*qiv
;
659 pdict
= qobject_to_qdict(props
);
661 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
666 qiv
= qmp_input_visitor_new(props
);
667 obj
= user_creatable_add_type(type
, id
, pdict
,
668 qmp_input_get_visitor(qiv
), errp
);
669 qmp_input_visitor_cleanup(qiv
);
675 void qmp_object_del(const char *id
, Error
**errp
)
677 user_creatable_del(id
, errp
);
680 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
682 MemoryDeviceInfoList
*head
= NULL
;
683 MemoryDeviceInfoList
**prev
= &head
;
685 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
690 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
693 ACPIOSTInfoList
*head
= NULL
;
694 ACPIOSTInfoList
**prev
= &head
;
695 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
698 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
699 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
701 adevc
->ospm_status(adev
, &prev
);
703 error_setg(errp
, "command is not supported, missing ACPI device");