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/uuid.h"
22 #include "qmp-commands.h"
23 #include "sysemu/char.h"
24 #include "ui/qemu-spice.h"
26 #include "sysemu/kvm.h"
27 #include "sysemu/arch_init.h"
29 #include "sysemu/blockdev.h"
30 #include "sysemu/block-backend.h"
31 #include "qom/qom-qobject.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qapi/qmp/qobject.h"
34 #include "qapi/qobject-input-visitor.h"
35 #include "hw/boards.h"
36 #include "qom/object_interfaces.h"
37 #include "hw/mem/pc-dimm.h"
38 #include "hw/acpi/acpi_dev_interface.h"
40 NameInfo
*qmp_query_name(Error
**errp
)
42 NameInfo
*info
= g_malloc0(sizeof(*info
));
45 info
->has_name
= true;
46 info
->name
= g_strdup(qemu_name
);
52 VersionInfo
*qmp_query_version(Error
**errp
)
54 VersionInfo
*info
= g_new0(VersionInfo
, 1);
56 info
->qemu
= g_new0(VersionTriple
, 1);
57 info
->qemu
->major
= QEMU_VERSION_MAJOR
;
58 info
->qemu
->minor
= QEMU_VERSION_MINOR
;
59 info
->qemu
->micro
= QEMU_VERSION_MICRO
;
60 info
->package
= g_strdup(QEMU_PKGVERSION
);
65 KvmInfo
*qmp_query_kvm(Error
**errp
)
67 KvmInfo
*info
= g_malloc0(sizeof(*info
));
69 info
->enabled
= kvm_enabled();
70 info
->present
= kvm_available();
75 UuidInfo
*qmp_query_uuid(Error
**errp
)
77 UuidInfo
*info
= g_malloc0(sizeof(*info
));
79 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
83 void qmp_quit(Error
**errp
)
86 qemu_system_shutdown_request();
89 void qmp_stop(Error
**errp
)
91 /* if there is a dump in background, we should wait until the dump
93 if (dump_in_progress()) {
94 error_setg(errp
, "There is a dump in process, please wait.");
98 if (runstate_check(RUN_STATE_INMIGRATE
)) {
101 vm_stop(RUN_STATE_PAUSED
);
105 void qmp_system_reset(Error
**errp
)
107 qemu_system_reset_request();
110 void qmp_system_powerdown(Error
**erp
)
112 qemu_system_powerdown_request();
115 void qmp_cpu(int64_t index
, Error
**errp
)
117 /* Just do nothing */
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");
133 /* If VNC support is enabled, the "true" query-vnc command is
134 defined in the VNC subsystem */
135 VncInfo
*qmp_query_vnc(Error
**errp
)
137 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
141 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
143 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
150 * qmp-commands.hx ensures that QMP command query-spice exists only
151 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
152 * result. However, the QAPI schema is blissfully unaware of that,
153 * and the QAPI code generator happily generates a dead
154 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
155 * one, or else linking fails. FIXME Educate the QAPI schema on
158 SpiceInfo
*qmp_query_spice(Error
**errp
)
164 void qmp_cont(Error
**errp
)
166 Error
*local_err
= NULL
;
168 BlockDriverState
*bs
;
171 /* if there is a dump in background, we should wait until the dump
173 if (dump_in_progress()) {
174 error_setg(errp
, "There is a dump in process, please wait.");
178 if (runstate_needs_reset()) {
179 error_setg(errp
, "Resetting the Virtual Machine is required");
181 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
185 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
186 blk_iostatus_reset(blk
);
189 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
190 bdrv_add_key(bs
, NULL
, &local_err
);
192 error_propagate(errp
, local_err
);
197 /* Continuing after completed migration. Images have been inactivated to
198 * allow the destination to take control. Need to get control back now. */
199 if (runstate_check(RUN_STATE_FINISH_MIGRATE
) ||
200 runstate_check(RUN_STATE_POSTMIGRATE
))
202 bdrv_invalidate_cache_all(&local_err
);
204 error_propagate(errp
, local_err
);
209 if (runstate_check(RUN_STATE_INMIGRATE
)) {
216 void qmp_system_wakeup(Error
**errp
)
218 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
221 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
224 bool ambiguous
= false;
225 ObjectPropertyInfoList
*props
= NULL
;
226 ObjectProperty
*prop
;
227 ObjectPropertyIterator iter
;
229 obj
= object_resolve_path(path
, &ambiguous
);
232 error_setg(errp
, "Path '%s' is ambiguous", path
);
234 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
235 "Device '%s' not found", path
);
240 object_property_iter_init(&iter
, obj
);
241 while ((prop
= object_property_iter_next(&iter
))) {
242 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
244 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
248 entry
->value
->name
= g_strdup(prop
->name
);
249 entry
->value
->type
= g_strdup(prop
->type
);
255 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
260 obj
= object_resolve_path(path
, NULL
);
262 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
263 "Device '%s' not found", path
);
267 object_property_set_qobject(obj
, value
, property
, errp
);
270 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
274 obj
= object_resolve_path(path
, NULL
);
276 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
277 "Device '%s' not found", path
);
281 return object_property_get_qobject(obj
, property
, errp
);
284 void qmp_set_password(const char *protocol
, const char *password
,
285 bool has_connected
, const char *connected
, Error
**errp
)
287 int disconnect_if_connected
= 0;
288 int fail_if_connected
= 0;
292 if (strcmp(connected
, "fail") == 0) {
293 fail_if_connected
= 1;
294 } else if (strcmp(connected
, "disconnect") == 0) {
295 disconnect_if_connected
= 1;
296 } else if (strcmp(connected
, "keep") == 0) {
299 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
304 if (strcmp(protocol
, "spice") == 0) {
305 if (!qemu_using_spice(errp
)) {
308 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
309 disconnect_if_connected
);
311 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
316 if (strcmp(protocol
, "vnc") == 0) {
317 if (fail_if_connected
|| disconnect_if_connected
) {
318 /* vnc supports "connected=keep" only */
319 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
322 /* Note that setting an empty password will not disable login through
324 rc
= vnc_display_password(NULL
, password
);
326 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
331 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
334 void qmp_expire_password(const char *protocol
, const char *whenstr
,
340 if (strcmp(whenstr
, "now") == 0) {
342 } else if (strcmp(whenstr
, "never") == 0) {
344 } else if (whenstr
[0] == '+') {
345 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
347 when
= strtoull(whenstr
, NULL
, 10);
350 if (strcmp(protocol
, "spice") == 0) {
351 if (!qemu_using_spice(errp
)) {
354 rc
= qemu_spice_set_pw_expire(when
);
356 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
361 if (strcmp(protocol
, "vnc") == 0) {
362 rc
= vnc_display_pw_expire(NULL
, when
);
364 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
369 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
373 void qmp_change_vnc_password(const char *password
, Error
**errp
)
375 if (vnc_display_password(NULL
, password
) < 0) {
376 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
380 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
382 QemuOptsList
*olist
= qemu_find_opts("vnc");
385 if (strstr(target
, "id=")) {
386 error_setg(errp
, "id not supported");
390 opts
= qemu_opts_find(olist
, "default");
394 opts
= vnc_parse(target
, errp
);
399 vnc_display_open("default", errp
);
402 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
405 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
407 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
409 qmp_change_vnc_password(arg
, errp
);
412 qmp_change_vnc_listen(target
, errp
);
416 void qmp_change_vnc_password(const char *password
, Error
**errp
)
418 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
420 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
423 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
425 #endif /* !CONFIG_VNC */
427 void qmp_change(const char *device
, const char *target
,
428 bool has_arg
, const char *arg
, Error
**errp
)
430 if (strcmp(device
, "vnc") == 0) {
431 qmp_change_vnc(target
, has_arg
, arg
, errp
);
433 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
434 has_arg
, arg
, false, 0, errp
);
438 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
440 ObjectTypeInfoList
*e
, **pret
= data
;
441 ObjectTypeInfo
*info
;
443 info
= g_malloc0(sizeof(*info
));
444 info
->name
= g_strdup(object_class_get_name(klass
));
446 e
= g_malloc0(sizeof(*e
));
452 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
453 const char *implements
,
458 ObjectTypeInfoList
*ret
= NULL
;
460 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
465 /* Return a DevicePropertyInfo for a qdev property.
467 * If a qdev property with the given name does not exist, use the given default
468 * type. If the qdev property info should not be shown, return NULL.
470 * The caller must free the return value.
472 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
474 const char *default_type
,
475 const char *description
)
477 DevicePropertyInfo
*info
;
481 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
482 if (strcmp(name
, prop
->name
) != 0) {
487 * TODO Properties without a parser are just for dirty hacks.
488 * qdev_prop_ptr is the only such PropertyInfo. It's marked
489 * for removal. This conditional should be removed along with
492 if (!prop
->info
->set
) {
493 return NULL
; /* no way to set it, don't show */
496 info
= g_malloc0(sizeof(*info
));
497 info
->name
= g_strdup(prop
->name
);
498 info
->type
= g_strdup(prop
->info
->name
);
499 info
->has_description
= !!prop
->info
->description
;
500 info
->description
= g_strdup(prop
->info
->description
);
503 klass
= object_class_get_parent(klass
);
504 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
506 /* Not a qdev property, use the default type */
507 info
= g_malloc0(sizeof(*info
));
508 info
->name
= g_strdup(name
);
509 info
->type
= g_strdup(default_type
);
510 info
->has_description
= !!description
;
511 info
->description
= g_strdup(description
);
516 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
521 ObjectProperty
*prop
;
522 ObjectPropertyIterator iter
;
523 DevicePropertyInfoList
*prop_list
= NULL
;
525 klass
= object_class_by_name(typename
);
527 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
528 "Device '%s' not found", typename
);
532 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
534 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name", TYPE_DEVICE
);
538 if (object_class_is_abstract(klass
)) {
539 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name",
540 "non-abstract device type");
544 if (DEVICE_CLASS(klass
)->cannot_destroy_with_object_finalize_yet
) {
545 error_setg(errp
, "Can't list properties of device '%s'", typename
);
549 obj
= object_new(typename
);
551 object_property_iter_init(&iter
, obj
);
552 while ((prop
= object_property_iter_next(&iter
))) {
553 DevicePropertyInfo
*info
;
554 DevicePropertyInfoList
*entry
;
556 /* Skip Object and DeviceState properties */
557 if (strcmp(prop
->name
, "type") == 0 ||
558 strcmp(prop
->name
, "realized") == 0 ||
559 strcmp(prop
->name
, "hotpluggable") == 0 ||
560 strcmp(prop
->name
, "hotplugged") == 0 ||
561 strcmp(prop
->name
, "parent_bus") == 0) {
565 /* Skip legacy properties since they are just string versions of
566 * properties that we already list.
568 if (strstart(prop
->name
, "legacy-", NULL
)) {
572 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
578 entry
= g_malloc0(sizeof(*entry
));
580 entry
->next
= prop_list
;
589 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
591 return arch_query_cpu_definitions(errp
);
594 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
598 return arch_query_cpu_model_expansion(type
, model
, errp
);
601 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
602 CpuModelInfo
*modelb
,
605 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
608 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
609 CpuModelInfo
*modelb
,
612 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
615 void qmp_add_client(const char *protocol
, const char *fdname
,
616 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
622 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
627 if (strcmp(protocol
, "spice") == 0) {
628 if (!qemu_using_spice(errp
)) {
632 skipauth
= has_skipauth
? skipauth
: false;
633 tls
= has_tls
? tls
: false;
634 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
635 error_setg(errp
, "spice failed to add client");
640 } else if (strcmp(protocol
, "vnc") == 0) {
641 skipauth
= has_skipauth
? skipauth
: false;
642 vnc_display_add_client(NULL
, fd
, skipauth
);
645 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
646 if (qemu_chr_add_client(s
, fd
) < 0) {
647 error_setg(errp
, "failed to add client");
654 error_setg(errp
, "protocol '%s' is invalid", protocol
);
659 void qmp_object_add(const char *type
, const char *id
,
660 bool has_props
, QObject
*props
, Error
**errp
)
667 pdict
= qobject_to_qdict(props
);
669 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
677 v
= qobject_input_visitor_new(QOBJECT(pdict
), true);
678 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
686 void qmp_object_del(const char *id
, Error
**errp
)
688 user_creatable_del(id
, errp
);
691 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
693 MemoryDeviceInfoList
*head
= NULL
;
694 MemoryDeviceInfoList
**prev
= &head
;
696 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
701 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
704 ACPIOSTInfoList
*head
= NULL
;
705 ACPIOSTInfoList
**prev
= &head
;
706 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
709 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
710 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
712 adevc
->ospm_status(adev
, &prev
);
714 error_setg(errp
, "command is not supported, missing ACPI device");