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/qmp-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
));
80 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
81 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
82 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
83 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
84 qemu_uuid
[14], qemu_uuid
[15]);
86 info
->UUID
= g_strdup(uuid
);
90 void qmp_quit(Error
**errp
)
93 qemu_system_shutdown_request();
96 void qmp_stop(Error
**errp
)
98 /* if there is a dump in background, we should wait until the dump
100 if (dump_in_progress()) {
101 error_setg(errp
, "There is a dump in process, please wait.");
105 if (runstate_check(RUN_STATE_INMIGRATE
)) {
108 vm_stop(RUN_STATE_PAUSED
);
112 void qmp_system_reset(Error
**errp
)
114 qemu_system_reset_request();
117 void qmp_system_powerdown(Error
**erp
)
119 qemu_system_powerdown_request();
122 void qmp_cpu(int64_t index
, Error
**errp
)
124 /* Just do nothing */
127 void qmp_cpu_add(int64_t id
, Error
**errp
)
131 mc
= MACHINE_GET_CLASS(current_machine
);
132 if (mc
->hot_add_cpu
) {
133 mc
->hot_add_cpu(id
, errp
);
135 error_setg(errp
, "Not supported");
140 /* If VNC support is enabled, the "true" query-vnc command is
141 defined in the VNC subsystem */
142 VncInfo
*qmp_query_vnc(Error
**errp
)
144 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
148 VncInfo2List
*qmp_query_vnc_servers(Error
**errp
)
150 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
157 * qmp-commands.hx ensures that QMP command query-spice exists only
158 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
159 * result. However, the QAPI schema is blissfully unaware of that,
160 * and the QAPI code generator happily generates a dead
161 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
162 * one, or else linking fails. FIXME Educate the QAPI schema on
165 SpiceInfo
*qmp_query_spice(Error
**errp
)
171 void qmp_cont(Error
**errp
)
173 Error
*local_err
= NULL
;
175 BlockDriverState
*bs
;
178 /* if there is a dump in background, we should wait until the dump
180 if (dump_in_progress()) {
181 error_setg(errp
, "There is a dump in process, please wait.");
185 if (runstate_needs_reset()) {
186 error_setg(errp
, "Resetting the Virtual Machine is required");
188 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
192 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
193 blk_iostatus_reset(blk
);
196 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
197 bdrv_add_key(bs
, NULL
, &local_err
);
199 error_propagate(errp
, local_err
);
204 /* Continuing after completed migration. Images have been inactivated to
205 * allow the destination to take control. Need to get control back now. */
206 if (runstate_check(RUN_STATE_FINISH_MIGRATE
) ||
207 runstate_check(RUN_STATE_POSTMIGRATE
))
209 bdrv_invalidate_cache_all(&local_err
);
211 error_propagate(errp
, local_err
);
216 if (runstate_check(RUN_STATE_INMIGRATE
)) {
223 void qmp_system_wakeup(Error
**errp
)
225 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
228 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
231 bool ambiguous
= false;
232 ObjectPropertyInfoList
*props
= NULL
;
233 ObjectProperty
*prop
;
234 ObjectPropertyIterator iter
;
236 obj
= object_resolve_path(path
, &ambiguous
);
239 error_setg(errp
, "Path '%s' is ambiguous", path
);
241 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
242 "Device '%s' not found", path
);
247 object_property_iter_init(&iter
, obj
);
248 while ((prop
= object_property_iter_next(&iter
))) {
249 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
251 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
255 entry
->value
->name
= g_strdup(prop
->name
);
256 entry
->value
->type
= g_strdup(prop
->type
);
262 void qmp_qom_set(const char *path
, const char *property
, QObject
*value
,
267 obj
= object_resolve_path(path
, NULL
);
269 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
270 "Device '%s' not found", path
);
274 object_property_set_qobject(obj
, value
, property
, errp
);
277 QObject
*qmp_qom_get(const char *path
, const char *property
, Error
**errp
)
281 obj
= object_resolve_path(path
, NULL
);
283 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
284 "Device '%s' not found", path
);
288 return object_property_get_qobject(obj
, property
, errp
);
291 void qmp_set_password(const char *protocol
, const char *password
,
292 bool has_connected
, const char *connected
, Error
**errp
)
294 int disconnect_if_connected
= 0;
295 int fail_if_connected
= 0;
299 if (strcmp(connected
, "fail") == 0) {
300 fail_if_connected
= 1;
301 } else if (strcmp(connected
, "disconnect") == 0) {
302 disconnect_if_connected
= 1;
303 } else if (strcmp(connected
, "keep") == 0) {
306 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
311 if (strcmp(protocol
, "spice") == 0) {
312 if (!qemu_using_spice(errp
)) {
315 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
316 disconnect_if_connected
);
318 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
323 if (strcmp(protocol
, "vnc") == 0) {
324 if (fail_if_connected
|| disconnect_if_connected
) {
325 /* vnc supports "connected=keep" only */
326 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
329 /* Note that setting an empty password will not disable login through
331 rc
= vnc_display_password(NULL
, password
);
333 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
338 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
341 void qmp_expire_password(const char *protocol
, const char *whenstr
,
347 if (strcmp(whenstr
, "now") == 0) {
349 } else if (strcmp(whenstr
, "never") == 0) {
351 } else if (whenstr
[0] == '+') {
352 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
354 when
= strtoull(whenstr
, NULL
, 10);
357 if (strcmp(protocol
, "spice") == 0) {
358 if (!qemu_using_spice(errp
)) {
361 rc
= qemu_spice_set_pw_expire(when
);
363 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
368 if (strcmp(protocol
, "vnc") == 0) {
369 rc
= vnc_display_pw_expire(NULL
, when
);
371 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
376 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
380 void qmp_change_vnc_password(const char *password
, Error
**errp
)
382 if (vnc_display_password(NULL
, password
) < 0) {
383 error_setg(errp
, QERR_SET_PASSWD_FAILED
);
387 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
389 QemuOptsList
*olist
= qemu_find_opts("vnc");
392 if (strstr(target
, "id=")) {
393 error_setg(errp
, "id not supported");
397 opts
= qemu_opts_find(olist
, "default");
401 opts
= vnc_parse(target
, errp
);
406 vnc_display_open("default", errp
);
409 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
412 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
414 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
416 qmp_change_vnc_password(arg
, errp
);
419 qmp_change_vnc_listen(target
, errp
);
423 void qmp_change_vnc_password(const char *password
, Error
**errp
)
425 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
427 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
430 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
432 #endif /* !CONFIG_VNC */
434 void qmp_change(const char *device
, const char *target
,
435 bool has_arg
, const char *arg
, Error
**errp
)
437 if (strcmp(device
, "vnc") == 0) {
438 qmp_change_vnc(target
, has_arg
, arg
, errp
);
440 qmp_blockdev_change_medium(device
, target
, has_arg
, arg
, false, 0,
445 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
447 ObjectTypeInfoList
*e
, **pret
= data
;
448 ObjectTypeInfo
*info
;
450 info
= g_malloc0(sizeof(*info
));
451 info
->name
= g_strdup(object_class_get_name(klass
));
453 e
= g_malloc0(sizeof(*e
));
459 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
460 const char *implements
,
465 ObjectTypeInfoList
*ret
= NULL
;
467 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
472 /* Return a DevicePropertyInfo for a qdev property.
474 * If a qdev property with the given name does not exist, use the given default
475 * type. If the qdev property info should not be shown, return NULL.
477 * The caller must free the return value.
479 static DevicePropertyInfo
*make_device_property_info(ObjectClass
*klass
,
481 const char *default_type
,
482 const char *description
)
484 DevicePropertyInfo
*info
;
488 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
489 if (strcmp(name
, prop
->name
) != 0) {
494 * TODO Properties without a parser are just for dirty hacks.
495 * qdev_prop_ptr is the only such PropertyInfo. It's marked
496 * for removal. This conditional should be removed along with
499 if (!prop
->info
->set
) {
500 return NULL
; /* no way to set it, don't show */
503 info
= g_malloc0(sizeof(*info
));
504 info
->name
= g_strdup(prop
->name
);
505 info
->type
= g_strdup(prop
->info
->name
);
506 info
->has_description
= !!prop
->info
->description
;
507 info
->description
= g_strdup(prop
->info
->description
);
510 klass
= object_class_get_parent(klass
);
511 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
513 /* Not a qdev property, use the default type */
514 info
= g_malloc0(sizeof(*info
));
515 info
->name
= g_strdup(name
);
516 info
->type
= g_strdup(default_type
);
517 info
->has_description
= !!description
;
518 info
->description
= g_strdup(description
);
523 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
528 ObjectProperty
*prop
;
529 ObjectPropertyIterator iter
;
530 DevicePropertyInfoList
*prop_list
= NULL
;
532 klass
= object_class_by_name(typename
);
534 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
535 "Device '%s' not found", typename
);
539 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
541 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name", TYPE_DEVICE
);
545 if (object_class_is_abstract(klass
)) {
546 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "name",
547 "non-abstract device type");
551 if (DEVICE_CLASS(klass
)->cannot_destroy_with_object_finalize_yet
) {
552 error_setg(errp
, "Can't list properties of device '%s'", typename
);
556 obj
= object_new(typename
);
558 object_property_iter_init(&iter
, obj
);
559 while ((prop
= object_property_iter_next(&iter
))) {
560 DevicePropertyInfo
*info
;
561 DevicePropertyInfoList
*entry
;
563 /* Skip Object and DeviceState properties */
564 if (strcmp(prop
->name
, "type") == 0 ||
565 strcmp(prop
->name
, "realized") == 0 ||
566 strcmp(prop
->name
, "hotpluggable") == 0 ||
567 strcmp(prop
->name
, "hotplugged") == 0 ||
568 strcmp(prop
->name
, "parent_bus") == 0) {
572 /* Skip legacy properties since they are just string versions of
573 * properties that we already list.
575 if (strstart(prop
->name
, "legacy-", NULL
)) {
579 info
= make_device_property_info(klass
, prop
->name
, prop
->type
,
585 entry
= g_malloc0(sizeof(*entry
));
587 entry
->next
= prop_list
;
596 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
598 return arch_query_cpu_definitions(errp
);
601 CpuModelExpansionInfo
*qmp_query_cpu_model_expansion(CpuModelExpansionType type
,
605 return arch_query_cpu_model_expansion(type
, model
, errp
);
608 CpuModelCompareInfo
*qmp_query_cpu_model_comparison(CpuModelInfo
*modela
,
609 CpuModelInfo
*modelb
,
612 return arch_query_cpu_model_comparison(modela
, modelb
, errp
);
615 CpuModelBaselineInfo
*qmp_query_cpu_model_baseline(CpuModelInfo
*modela
,
616 CpuModelInfo
*modelb
,
619 return arch_query_cpu_model_baseline(modela
, modelb
, errp
);
622 void qmp_add_client(const char *protocol
, const char *fdname
,
623 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
629 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
634 if (strcmp(protocol
, "spice") == 0) {
635 if (!qemu_using_spice(errp
)) {
639 skipauth
= has_skipauth
? skipauth
: false;
640 tls
= has_tls
? tls
: false;
641 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
642 error_setg(errp
, "spice failed to add client");
647 } else if (strcmp(protocol
, "vnc") == 0) {
648 skipauth
= has_skipauth
? skipauth
: false;
649 vnc_display_add_client(NULL
, fd
, skipauth
);
652 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
653 if (qemu_chr_add_client(s
, fd
) < 0) {
654 error_setg(errp
, "failed to add client");
661 error_setg(errp
, "protocol '%s' is invalid", protocol
);
666 void qmp_object_add(const char *type
, const char *id
,
667 bool has_props
, QObject
*props
, Error
**errp
)
669 const QDict
*pdict
= NULL
;
674 pdict
= qobject_to_qdict(props
);
676 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
681 v
= qmp_input_visitor_new(props
, true);
682 obj
= user_creatable_add_type(type
, id
, pdict
, v
, errp
);
689 void qmp_object_del(const char *id
, Error
**errp
)
691 user_creatable_del(id
, errp
);
694 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
696 MemoryDeviceInfoList
*head
= NULL
;
697 MemoryDeviceInfoList
**prev
= &head
;
699 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);
704 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
707 ACPIOSTInfoList
*head
= NULL
;
708 ACPIOSTInfoList
**prev
= &head
;
709 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
712 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
713 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
715 adevc
->ospm_status(adev
, &prev
);
717 error_setg(errp
, "command is not supported, missing ACPI device");