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 "hw/boards.h"
29 NameInfo
*qmp_query_name(Error
**errp
)
31 NameInfo
*info
= g_malloc0(sizeof(*info
));
34 info
->has_name
= true;
35 info
->name
= g_strdup(qemu_name
);
41 VersionInfo
*qmp_query_version(Error
**err
)
43 VersionInfo
*info
= g_malloc0(sizeof(*info
));
44 const char *version
= QEMU_VERSION
;
47 info
->qemu
.major
= strtol(version
, &tmp
, 10);
49 info
->qemu
.minor
= strtol(tmp
, &tmp
, 10);
51 info
->qemu
.micro
= strtol(tmp
, &tmp
, 10);
52 info
->package
= g_strdup(QEMU_PKGVERSION
);
57 KvmInfo
*qmp_query_kvm(Error
**errp
)
59 KvmInfo
*info
= g_malloc0(sizeof(*info
));
61 info
->enabled
= kvm_enabled();
62 info
->present
= kvm_available();
67 UuidInfo
*qmp_query_uuid(Error
**errp
)
69 UuidInfo
*info
= g_malloc0(sizeof(*info
));
72 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
73 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
74 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
75 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
76 qemu_uuid
[14], qemu_uuid
[15]);
78 info
->UUID
= g_strdup(uuid
);
82 void qmp_quit(Error
**err
)
85 qemu_system_shutdown_request();
88 void qmp_stop(Error
**errp
)
90 if (runstate_check(RUN_STATE_INMIGRATE
)) {
93 vm_stop(RUN_STATE_PAUSED
);
97 void qmp_system_reset(Error
**errp
)
99 qemu_system_reset_request();
102 void qmp_system_powerdown(Error
**erp
)
104 qemu_system_powerdown_request();
107 void qmp_cpu(int64_t index
, Error
**errp
)
109 /* Just do nothing */
112 void qmp_cpu_add(int64_t id
, Error
**errp
)
114 if (current_machine
->hot_add_cpu
) {
115 current_machine
->hot_add_cpu(id
, errp
);
117 error_setg(errp
, "Not supported");
122 /* If VNC support is enabled, the "true" query-vnc command is
123 defined in the VNC subsystem */
124 VncInfo
*qmp_query_vnc(Error
**errp
)
126 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
132 /* If SPICE support is enabled, the "true" query-spice command is
133 defined in the SPICE subsystem. Also note that we use a small
134 trick to maintain query-spice's original behavior, which is not
135 to be available in the namespace if SPICE is not compiled in */
136 SpiceInfo
*qmp_query_spice(Error
**errp
)
138 error_set(errp
, QERR_COMMAND_NOT_FOUND
, "query-spice");
143 static void iostatus_bdrv_it(void *opaque
, BlockDriverState
*bs
)
145 bdrv_iostatus_reset(bs
);
148 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
150 Error
**err
= opaque
;
152 if (!error_is_set(err
) && bdrv_key_required(bs
)) {
153 error_set(err
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
154 bdrv_get_encrypted_filename(bs
));
158 void qmp_cont(Error
**errp
)
160 Error
*local_err
= NULL
;
162 if (runstate_needs_reset()) {
163 error_set(errp
, QERR_RESET_REQUIRED
);
165 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
169 bdrv_iterate(iostatus_bdrv_it
, NULL
);
170 bdrv_iterate(encrypted_bdrv_it
, &local_err
);
172 error_propagate(errp
, local_err
);
176 if (runstate_check(RUN_STATE_INMIGRATE
)) {
183 void qmp_system_wakeup(Error
**errp
)
185 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
188 ObjectPropertyInfoList
*qmp_qom_list(const char *path
, Error
**errp
)
191 bool ambiguous
= false;
192 ObjectPropertyInfoList
*props
= NULL
;
193 ObjectProperty
*prop
;
195 obj
= object_resolve_path(path
, &ambiguous
);
197 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
201 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
202 ObjectPropertyInfoList
*entry
= g_malloc0(sizeof(*entry
));
204 entry
->value
= g_malloc0(sizeof(ObjectPropertyInfo
));
208 entry
->value
->name
= g_strdup(prop
->name
);
209 entry
->value
->type
= g_strdup(prop
->type
);
215 /* FIXME: teach qapi about how to pass through Visitors */
216 int qmp_qom_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
218 const char *path
= qdict_get_str(qdict
, "path");
219 const char *property
= qdict_get_str(qdict
, "property");
220 QObject
*value
= qdict_get(qdict
, "value");
221 Error
*local_err
= NULL
;
224 obj
= object_resolve_path(path
, NULL
);
226 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
230 object_property_set_qobject(obj
, value
, property
, &local_err
);
234 qerror_report_err(local_err
);
235 error_free(local_err
);
242 int qmp_qom_get(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
)
244 const char *path
= qdict_get_str(qdict
, "path");
245 const char *property
= qdict_get_str(qdict
, "property");
246 Error
*local_err
= NULL
;
249 obj
= object_resolve_path(path
, NULL
);
251 error_set(&local_err
, QERR_DEVICE_NOT_FOUND
, path
);
255 *ret
= object_property_get_qobject(obj
, property
, &local_err
);
259 qerror_report_err(local_err
);
260 error_free(local_err
);
267 void qmp_set_password(const char *protocol
, const char *password
,
268 bool has_connected
, const char *connected
, Error
**errp
)
270 int disconnect_if_connected
= 0;
271 int fail_if_connected
= 0;
275 if (strcmp(connected
, "fail") == 0) {
276 fail_if_connected
= 1;
277 } else if (strcmp(connected
, "disconnect") == 0) {
278 disconnect_if_connected
= 1;
279 } else if (strcmp(connected
, "keep") == 0) {
282 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
287 if (strcmp(protocol
, "spice") == 0) {
289 /* correct one? spice isn't a device ,,, */
290 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
293 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
294 disconnect_if_connected
);
296 error_set(errp
, QERR_SET_PASSWD_FAILED
);
301 if (strcmp(protocol
, "vnc") == 0) {
302 if (fail_if_connected
|| disconnect_if_connected
) {
303 /* vnc supports "connected=keep" only */
304 error_set(errp
, QERR_INVALID_PARAMETER
, "connected");
307 /* Note that setting an empty password will not disable login through
309 rc
= vnc_display_password(NULL
, password
);
311 error_set(errp
, QERR_SET_PASSWD_FAILED
);
316 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
319 void qmp_expire_password(const char *protocol
, const char *whenstr
,
325 if (strcmp(whenstr
, "now") == 0) {
327 } else if (strcmp(whenstr
, "never") == 0) {
329 } else if (whenstr
[0] == '+') {
330 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
332 when
= strtoull(whenstr
, NULL
, 10);
335 if (strcmp(protocol
, "spice") == 0) {
337 /* correct one? spice isn't a device ,,, */
338 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
341 rc
= qemu_spice_set_pw_expire(when
);
343 error_set(errp
, QERR_SET_PASSWD_FAILED
);
348 if (strcmp(protocol
, "vnc") == 0) {
349 rc
= vnc_display_pw_expire(NULL
, when
);
351 error_set(errp
, QERR_SET_PASSWD_FAILED
);
356 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
360 void qmp_change_vnc_password(const char *password
, Error
**errp
)
362 if (vnc_display_password(NULL
, password
) < 0) {
363 error_set(errp
, QERR_SET_PASSWD_FAILED
);
367 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
369 vnc_display_open(NULL
, target
, errp
);
372 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
375 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
377 error_set(errp
, QERR_MISSING_PARAMETER
, "password");
379 qmp_change_vnc_password(arg
, errp
);
382 qmp_change_vnc_listen(target
, errp
);
386 void qmp_change_vnc_password(const char *password
, Error
**errp
)
388 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
390 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
393 error_set(errp
, QERR_FEATURE_DISABLED
, "vnc");
395 #endif /* !CONFIG_VNC */
397 void qmp_change(const char *device
, const char *target
,
398 bool has_arg
, const char *arg
, Error
**err
)
400 if (strcmp(device
, "vnc") == 0) {
401 qmp_change_vnc(target
, has_arg
, arg
, err
);
403 qmp_change_blockdev(device
, target
, arg
, err
);
407 static void qom_list_types_tramp(ObjectClass
*klass
, void *data
)
409 ObjectTypeInfoList
*e
, **pret
= data
;
410 ObjectTypeInfo
*info
;
412 info
= g_malloc0(sizeof(*info
));
413 info
->name
= g_strdup(object_class_get_name(klass
));
415 e
= g_malloc0(sizeof(*e
));
421 ObjectTypeInfoList
*qmp_qom_list_types(bool has_implements
,
422 const char *implements
,
427 ObjectTypeInfoList
*ret
= NULL
;
429 object_class_foreach(qom_list_types_tramp
, implements
, abstract
, &ret
);
434 DevicePropertyInfoList
*qmp_device_list_properties(const char *typename
,
439 DevicePropertyInfoList
*prop_list
= NULL
;
441 klass
= object_class_by_name(typename
);
443 error_set(errp
, QERR_DEVICE_NOT_FOUND
, typename
);
447 klass
= object_class_dynamic_cast(klass
, TYPE_DEVICE
);
449 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
450 "name", TYPE_DEVICE
);
455 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
456 DevicePropertyInfoList
*entry
;
457 DevicePropertyInfo
*info
;
460 * TODO Properties without a parser are just for dirty hacks.
461 * qdev_prop_ptr is the only such PropertyInfo. It's marked
462 * for removal. This conditional should be removed along with
465 if (!prop
->info
->set
) {
466 continue; /* no way to set it, don't show */
469 info
= g_malloc0(sizeof(*info
));
470 info
->name
= g_strdup(prop
->name
);
471 info
->type
= g_strdup(prop
->info
->legacy_name
?: prop
->info
->name
);
473 entry
= g_malloc0(sizeof(*entry
));
475 entry
->next
= prop_list
;
478 klass
= object_class_get_parent(klass
);
479 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
484 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
486 return arch_query_cpu_definitions(errp
);
489 void qmp_add_client(const char *protocol
, const char *fdname
,
490 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
496 fd
= monitor_get_fd(cur_mon
, fdname
, errp
);
501 if (strcmp(protocol
, "spice") == 0) {
503 error_set(errp
, QERR_DEVICE_NOT_ACTIVE
, "spice");
507 skipauth
= has_skipauth
? skipauth
: false;
508 tls
= has_tls
? tls
: false;
509 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
510 error_setg(errp
, "spice failed to add client");
515 } else if (strcmp(protocol
, "vnc") == 0) {
516 skipauth
= has_skipauth
? skipauth
: false;
517 vnc_display_add_client(NULL
, fd
, skipauth
);
520 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
521 if (qemu_chr_add_client(s
, fd
) < 0) {
522 error_setg(errp
, "failed to add client");
529 error_setg(errp
, "protocol '%s' is invalid", protocol
);