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-common.h"
17 #include "sysemu/sysemu.h"
18 #include "qmp-commands.h"
19 #include "sysemu/char.h"
20 #include "ui/qemu-spice.h"
22 #include "sysemu/kvm.h"
23 #include "sysemu/arch_init.h"
25 #include "sysemu/blockdev.h"
26 #include "qom/qom-qobject.h"
27 #include "qapi/qmp/qobject.h"
28 #include "qapi/qmp-input-visitor.h"
29 #include "hw/boards.h"
30 #include "qom/object_interfaces.h"
31 #include "hw/mem/pc-dimm.h"
33 NameInfo
*qmp_query_name(Error
**errp
)
35 NameInfo
*info
= g_malloc0(sizeof(*info
));
38 info
->has_name
= true;
39 info
->name
= g_strdup(qemu_name
);
45 VersionInfo
*qmp_query_version(Error
**errp
)
47 VersionInfo
*info
= g_malloc0(sizeof(*info
));
48 const char *version
= QEMU_VERSION
;
51 info
->qemu
.major
= strtol(version
, &tmp
, 10);
53 info
->qemu
.minor
= strtol(tmp
, &tmp
, 10);
55 info
->qemu
.micro
= strtol(tmp
, &tmp
, 10);
56 info
->package
= g_strdup(QEMU_PKGVERSION
);
61 KvmInfo
*qmp_query_kvm(Error
**errp
)
63 KvmInfo
*info
= g_malloc0(sizeof(*info
));
65 info
->enabled
= kvm_enabled();
66 info
->present
= kvm_available();
71 UuidInfo
*qmp_query_uuid(Error
**errp
)
73 UuidInfo
*info
= g_malloc0(sizeof(*info
));
76 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
77 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
78 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
79 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
80 qemu_uuid
[14], qemu_uuid
[15]);
82 info
->UUID
= g_strdup(uuid
);
86 void qmp_quit(Error
**errp
)
89 qemu_system_shutdown_request();
92 void qmp_stop(Error
**errp
)
94 if (runstate_check(RUN_STATE_INMIGRATE
)) {
97 vm_stop(RUN_STATE_PAUSED
);
101 void qmp_system_reset(Error
**errp
)
103 qemu_system_reset_request();
106 void qmp_system_powerdown(Error
**erp
)
108 qemu_system_powerdown_request();
111 void qmp_cpu(int64_t index
, Error
**errp
)
113 /* Just do nothing */
116 void qmp_cpu_add(int64_t id
, Error
**errp
)
120 mc
= MACHINE_GET_CLASS(current_machine
);
121 if (mc
->hot_add_cpu
) {
122 mc
->hot_add_cpu(id
, errp
);
124 error_setg(errp
, "Not supported");
129 /* If VNC support is enabled, the "true" query-vnc command is
130 defined in the VNC subsystem */
131 VncInfo
*qmp_query_vnc(Error
**errp
)
133 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
139 /* If SPICE support is enabled, the "true" query-spice command is
140 defined in the SPICE subsystem. Also note that we use a small
141 trick to maintain query-spice's original behavior, which is not
142 to be available in the namespace if SPICE is not compiled in */
143 SpiceInfo
*qmp_query_spice(Error
**errp
)
145 error_set(errp
, QERR_COMMAND_NOT_FOUND
, "query-spice");
150 void qmp_cont(Error
**errp
)
152 BlockDriverState
*bs
;
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 (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
162 bdrv_iostatus_reset(bs
);
164 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
165 if (bdrv_key_required(bs
)) {
166 error_set(errp
, QERR_DEVICE_ENCRYPTED
,
167 bdrv_get_device_name(bs
),
168 bdrv_get_encrypted_filename(bs
));
173 if (runstate_check(RUN_STATE_INMIGRATE
)) {
180 void qmp_system_wakeup(Error
**errp
)
182 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
185 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
188 bool ambiguous
= false;
189 ObjectPropertyInfoList
*props
= NULL
;
190 ObjectProperty
*prop
;
192 obj
= object_resolve_path(path
, &ambiguous
);
195 error_setg(errp
, "Path '%s' is ambiguous", path
);
197 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
202 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
203 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
205 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
209 entry
->value
->name
= g_strdup(prop
->name
);
210 entry
->value
->type
= g_strdup(prop
->type
);
216 /* FIXME: teach qapi about how to pass through Visitors */
217 int qmp_qom_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
219 const char *path
= qdict_get_str(qdict
, "path");
220 const char *property
= qdict_get_str(qdict
, "property");
221 QObject
*value
= qdict_get(qdict
, "value");
222 Error
*local_err
= NULL
;
225 obj
= object_resolve_path(path
, NULL
);
227 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
231 object_property_set_qobject(obj
, value
, property
, &local_err
);
235 qerror_report_err(local_err
);
236 error_free(local_err
);
243 int qmp_qom_get(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
245 const char *path
= qdict_get_str(qdict
, "path");
246 const char *property
= qdict_get_str(qdict
, "property");
247 Error
*local_err
= NULL
;
250 obj
= object_resolve_path(path
, NULL
);
252 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
256 *ret
= object_property_get_qobject(obj
, property
, &local_err
);
260 qerror_report_err(local_err
);
261 error_free(local_err
);
268 void qmp_set_password(const char *protocol
, const char *password
,
269 bool has_connected
, const char *connected
, Error
**errp
)
271 int disconnect_if_connected
= 0;
272 int fail_if_connected
= 0;
276 if (strcmp(connected
, "fail") == 0) {
277 fail_if_connected
= 1;
278 } else if (strcmp(connected
, "disconnect") == 0) {
279 disconnect_if_connected
= 1;
280 } else if (strcmp(connected
, "keep") == 0) {
283 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
288 if (strcmp(protocol
, "spice") == 0) {
290 /* correct one? spice isn't a device ,,, */
291 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
294 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
295 disconnect_if_connected
);
297 error_set(errp
, QERR_SET_PASSWD_FAILED
);
302 if (strcmp(protocol
, "vnc") == 0) {
303 if (fail_if_connected
|| disconnect_if_connected
) {
304 /* vnc supports "connected=keep" only */
305 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
308 /* Note that setting an empty password will not disable login through
310 rc
= vnc_display_password(NULL
, password
);
312 error_set(errp
, QERR_SET_PASSWD_FAILED
);
317 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
320 void qmp_expire_password(const char *protocol
, const char *whenstr
,
326 if (strcmp(whenstr
, "now") == 0) {
328 } else if (strcmp(whenstr
, "never") == 0) {
330 } else if (whenstr
[0] == '+') {
331 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
333 when
= strtoull(whenstr
, NULL
, 10);
336 if (strcmp(protocol
, "spice") == 0) {
338 /* correct one? spice isn't a device ,,, */
339 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
342 rc
= qemu_spice_set_pw_expire(when
);
344 error_set(errp
, QERR_SET_PASSWD_FAILED
);
349 if (strcmp(protocol
, "vnc") == 0) {
350 rc
= vnc_display_pw_expire(NULL
, when
);
352 error_set(errp
, QERR_SET_PASSWD_FAILED
);
357 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
361 void qmp_change_vnc_password(const char *password
, Error
**errp
)
363 if (vnc_display_password(NULL
, password
) < 0) {
364 error_set(errp
, QERR_SET_PASSWD_FAILED
);
368 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
370 vnc_display_open(NULL
, target
, errp
);
373 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
376 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
378 error_set(errp
, QERR_MISSING_PARAMETER
, "password");
380 qmp_change_vnc_password(arg
, errp
);
383 qmp_change_vnc_listen(target
, errp
);
387 void qmp_change_vnc_password(const char *password
, Error
**errp
)
389 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
391 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
394 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
396 #endif /* !CONFIG_VNC */
398 void qmp_change(const char *device
, const char *target
,
399 bool has_arg
, const char *arg
, Error
**errp
)
401 if (strcmp(device
, "vnc") == 0) {
402 qmp_change_vnc(target
, has_arg
, arg
, errp
);
404 qmp_change_blockdev(device
, target
, arg
, errp
);
408 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
410 ObjectTypeInfoList
*e
, **pret
= data
;
411 ObjectTypeInfo
*info
;
413 info
= g_malloc0(sizeof(*info
));
414 info
->name
= g_strdup(object_class_get_name(klass
));
416 e
= g_malloc0(sizeof(*e
));
422 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
423 const char *implements
,
428 ObjectTypeInfoList
*ret
= NULL
;
430 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
435 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
440 DevicePropertyInfoList
*prop_list
= NULL
;
442 klass
= object_class_by_name(typename
);
444 error_set(errp
, QERR_DEVICE_NOT_FOUND
, typename
);
448 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
450 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
451 "name", TYPE_DEVICE
);
456 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
457 DevicePropertyInfoList
*entry
;
458 DevicePropertyInfo
*info
;
461 * TODO Properties without a parser are just for dirty hacks.
462 * qdev_prop_ptr is the only such PropertyInfo. It's marked
463 * for removal. This conditional should be removed along with
466 if (!prop
->info
->set
) {
467 continue; /* no way to set it, don't show */
470 info
= g_malloc0(sizeof(*info
));
471 info
->name
= g_strdup(prop
->name
);
472 info
->type
= g_strdup(prop
->info
->legacy_name ?
: prop
->info
->name
);
474 entry
= g_malloc0(sizeof(*entry
));
476 entry
->next
= prop_list
;
479 klass
= object_class_get_parent(klass
);
480 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
485 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
487 return arch_query_cpu_definitions(errp
);
490 void qmp_add_client(const char *protocol
, const char *fdname
,
491 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
497 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
502 if (strcmp(protocol
, "spice") == 0) {
504 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
508 skipauth
= has_skipauth ? skipauth
: false;
509 tls
= has_tls ? tls
: false;
510 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
511 error_setg(errp
, "spice failed to add client");
516 } else if (strcmp(protocol
, "vnc") == 0) {
517 skipauth
= has_skipauth ? skipauth
: false;
518 vnc_display_add_client(NULL
, fd
, skipauth
);
521 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
522 if (qemu_chr_add_client(s
, fd
) < 0) {
523 error_setg(errp
, "failed to add client");
530 error_setg(errp
, "protocol '%s' is invalid", protocol
);
534 void object_add(const char *type
, const char *id
, const QDict
*qdict
,
535 Visitor
*v
, Error
**errp
)
540 Error
*local_err
= NULL
;
542 klass
= object_class_by_name(type
);
544 error_setg(errp
, "invalid object type: %s", type
);
548 if (!object_class_dynamic_cast(klass
, TYPE_USER_CREATABLE
)) {
549 error_setg(errp
, "object type '%s' isn't supported by object-add",
554 if (object_class_is_abstract(klass
)) {
555 error_setg(errp
, "object type '%s' is abstract", type
);
559 obj
= object_new(type
);
561 for (e
= qdict_first(qdict
); e
; e
= qdict_next(qdict
, e
)) {
562 object_property_set(obj
, v
, e
->key
, &local_err
);
569 object_property_add_child(container_get(object_get_root(), "/objects"),
570 id
, obj
, &local_err
);
575 user_creatable_complete(obj
, &local_err
);
577 object_property_del(container_get(object_get_root(), "/objects"),
583 error_propagate(errp
, local_err
);
588 int qmp_object_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
590 const char *type
= qdict_get_str(qdict
, "qom-type");
591 const char *id
= qdict_get_str(qdict
, "id");
592 QObject
*props
= qdict_get(qdict
, "props");
593 const QDict
*pdict
= NULL
;
594 Error
*local_err
= NULL
;
595 QmpInputVisitor
*qiv
;
598 pdict
= qobject_to_qdict(props
);
600 error_set(&local_err
, QERR_INVALID_PARAMETER_TYPE
, "props", "dict");
605 qiv
= qmp_input_visitor_new(props
);
606 object_add(type
, id
, pdict
, qmp_input_get_visitor(qiv
), &local_err
);
607 qmp_input_visitor_cleanup(qiv
);
611 qerror_report_err(local_err
);
612 error_free(local_err
);
619 void qmp_object_del(const char *id
, Error
**errp
)
624 container
= container_get(object_get_root(), "/objects");
625 obj
= object_resolve_path_component(container
, id
);
627 error_setg(errp
, "object id not found");
630 object_unparent(obj
);
633 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
635 MemoryDeviceInfoList
*head
= NULL
;
636 MemoryDeviceInfoList
**prev
= &head
;
638 qmp_pc_dimm_device_list(qdev_get_machine(), &prev
);