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"
32 #include "hw/acpi/acpi_dev_interface.h"
34 NameInfo *qmp_query_name(Error **errp)
36 NameInfo *info = g_malloc0(sizeof(*info));
39 info->has_name = true;
40 info->name = g_strdup(qemu_name);
46 VersionInfo *qmp_query_version(Error **errp)
48 VersionInfo *info = g_malloc0(sizeof(*info));
49 const char *version = QEMU_VERSION;
52 info->qemu.major = strtol(version, &tmp, 10);
54 info->qemu.minor = strtol(tmp, &tmp, 10);
56 info->qemu.micro = strtol(tmp, &tmp, 10);
57 info->package = g_strdup(QEMU_PKGVERSION);
62 KvmInfo *qmp_query_kvm(Error **errp)
64 KvmInfo *info = g_malloc0(sizeof(*info));
66 info->enabled = kvm_enabled();
67 info->present = kvm_available();
72 UuidInfo *qmp_query_uuid(Error **errp)
74 UuidInfo *info = g_malloc0(sizeof(*info));
77 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
78 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
79 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
80 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
81 qemu_uuid[14], qemu_uuid[15]);
83 info->UUID = g_strdup(uuid);
87 void qmp_quit(Error **errp)
90 qemu_system_shutdown_request();
93 void qmp_stop(Error **errp)
95 if (runstate_check(RUN_STATE_INMIGRATE)) {
98 vm_stop(RUN_STATE_PAUSED);
102 void qmp_system_reset(Error **errp)
104 qemu_system_reset_request();
107 void qmp_system_powerdown(Error **erp)
109 qemu_system_powerdown_request();
112 void qmp_cpu(int64_t index, Error **errp)
114 /* Just do nothing */
117 void qmp_cpu_add(int64_t id, Error **errp)
121 mc = MACHINE_GET_CLASS(current_machine);
122 if (mc->hot_add_cpu) {
123 mc->hot_add_cpu(id, errp);
125 error_setg(errp, "Not supported");
130 /* If VNC support is enabled, the "true" query-vnc command is
131 defined in the VNC subsystem */
132 VncInfo *qmp_query_vnc(Error **errp)
134 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
138 VncInfo2List *qmp_query_vnc_servers(Error **errp)
140 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
147 * qmp-commands.hx ensures that QMP command query-spice exists only
148 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
149 * result. However, the QAPI schema is blissfully unaware of that,
150 * and the QAPI code generator happily generates a dead
151 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
152 * Provide it one, or else linking fails.
153 * FIXME Educate the QAPI schema on CONFIG_SPICE.
155 SpiceInfo *qmp_query_spice(Error **errp)
161 void qmp_cont(Error **errp)
163 Error *local_err = NULL;
164 BlockDriverState *bs;
166 if (runstate_needs_reset()) {
167 error_setg(errp, "Resetting the Virtual Machine is required");
169 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
173 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
174 bdrv_iostatus_reset(bs);
176 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
177 bdrv_add_key(bs, NULL, &local_err);
179 error_propagate(errp, local_err);
184 if (runstate_check(RUN_STATE_INMIGRATE)) {
191 void qmp_system_wakeup(Error **errp)
193 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
196 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
199 bool ambiguous = false;
200 ObjectPropertyInfoList *props = NULL;
201 ObjectProperty *prop;
203 obj = object_resolve_path(path, &ambiguous);
206 error_setg(errp, "Path '%s' is ambiguous", path);
208 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
213 QTAILQ_FOREACH(prop, &obj->properties, node) {
214 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
216 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
220 entry->value->name = g_strdup(prop->name);
221 entry->value->type = g_strdup(prop->type);
227 /* FIXME: teach qapi about how to pass through Visitors */
228 int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
230 const char *path = qdict_get_str(qdict, "path");
231 const char *property = qdict_get_str(qdict, "property");
232 QObject *value = qdict_get(qdict, "value");
233 Error *local_err = NULL;
236 obj = object_resolve_path(path, NULL);
238 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
242 object_property_set_qobject(obj, value, property, &local_err);
246 qerror_report_err(local_err);
247 error_free(local_err);
254 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
256 const char *path = qdict_get_str(qdict, "path");
257 const char *property = qdict_get_str(qdict, "property");
258 Error *local_err = NULL;
261 obj = object_resolve_path(path, NULL);
263 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
267 *ret = object_property_get_qobject(obj, property, &local_err);
271 qerror_report_err(local_err);
272 error_free(local_err);
279 void qmp_set_password(const char *protocol, const char *password,
280 bool has_connected, const char *connected, Error **errp)
282 int disconnect_if_connected = 0;
283 int fail_if_connected = 0;
287 if (strcmp(connected, "fail") == 0) {
288 fail_if_connected = 1;
289 } else if (strcmp(connected, "disconnect") == 0) {
290 disconnect_if_connected = 1;
291 } else if (strcmp(connected, "keep") == 0) {
294 error_set(errp, QERR_INVALID_PARAMETER, "connected");
299 if (strcmp(protocol, "spice") == 0) {
300 if (!qemu_using_spice(errp)) {
303 rc = qemu_spice_set_passwd(password, fail_if_connected,
304 disconnect_if_connected);
306 error_set(errp, QERR_SET_PASSWD_FAILED);
311 if (strcmp(protocol, "vnc") == 0) {
312 if (fail_if_connected || disconnect_if_connected) {
313 /* vnc supports "connected=keep" only */
314 error_set(errp, QERR_INVALID_PARAMETER, "connected");
317 /* Note that setting an empty password will not disable login through
319 rc = vnc_display_password(NULL, password);
321 error_set(errp, QERR_SET_PASSWD_FAILED);
326 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
329 void qmp_expire_password(const char *protocol, const char *whenstr,
335 if (strcmp(whenstr, "now") == 0) {
337 } else if (strcmp(whenstr, "never") == 0) {
339 } else if (whenstr[0] == '+') {
340 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
342 when = strtoull(whenstr, NULL, 10);
345 if (strcmp(protocol, "spice") == 0) {
346 if (!qemu_using_spice(errp)) {
349 rc = qemu_spice_set_pw_expire(when);
351 error_set(errp, QERR_SET_PASSWD_FAILED);
356 if (strcmp(protocol, "vnc") == 0) {
357 rc = vnc_display_pw_expire(NULL, when);
359 error_set(errp, QERR_SET_PASSWD_FAILED);
364 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
368 void qmp_change_vnc_password(const char *password, Error **errp)
370 if (vnc_display_password(NULL, password) < 0) {
371 error_set(errp, QERR_SET_PASSWD_FAILED);
375 static void qmp_change_vnc_listen(const char *target, Error **errp)
377 QemuOptsList *olist = qemu_find_opts("vnc");
380 if (strstr(target, "id=")) {
381 error_setg(errp, "id not supported");
385 opts = qemu_opts_find(olist, "default");
389 opts = vnc_parse_func(target);
394 vnc_display_open("default", errp);
397 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
400 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
402 error_set(errp, QERR_MISSING_PARAMETER, "password");
404 qmp_change_vnc_password(arg, errp);
407 qmp_change_vnc_listen(target, errp);
411 void qmp_change_vnc_password(const char *password, Error **errp)
413 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
415 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
418 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
420 #endif /* !CONFIG_VNC */
422 void qmp_change(const char *device, const char *target,
423 bool has_arg, const char *arg, Error **errp)
425 if (strcmp(device, "vnc") == 0) {
426 qmp_change_vnc(target, has_arg, arg, errp);
428 qmp_change_blockdev(device, target, arg, errp);
432 static void qom_list_types_tramp(ObjectClass *klass, void *data)
434 ObjectTypeInfoList *e, **pret = data;
435 ObjectTypeInfo *info;
437 info = g_malloc0(sizeof(*info));
438 info->name = g_strdup(object_class_get_name(klass));
440 e = g_malloc0(sizeof(*e));
446 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
447 const char *implements,
452 ObjectTypeInfoList *ret = NULL;
454 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
459 /* Return a DevicePropertyInfo for a qdev property.
461 * If a qdev property with the given name does not exist, use the given default
462 * type. If the qdev property info should not be shown, return NULL.
464 * The caller must free the return value.
466 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
468 const char *default_type,
469 const char *description)
471 DevicePropertyInfo *info;
475 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
476 if (strcmp(name, prop->name) != 0) {
481 * TODO Properties without a parser are just for dirty hacks.
482 * qdev_prop_ptr is the only such PropertyInfo. It's marked
483 * for removal. This conditional should be removed along with
486 if (!prop->info->set) {
487 return NULL; /* no way to set it, don't show */
490 info = g_malloc0(sizeof(*info));
491 info->name = g_strdup(prop->name);
492 info->type = g_strdup(prop->info->name);
493 info->has_description = !!prop->info->description;
494 info->description = g_strdup(prop->info->description);
497 klass = object_class_get_parent(klass);
498 } while (klass != object_class_by_name(TYPE_DEVICE));
500 /* Not a qdev property, use the default type */
501 info = g_malloc0(sizeof(*info));
502 info->name = g_strdup(name);
503 info->type = g_strdup(default_type);
504 info->has_description = !!description;
505 info->description = g_strdup(description);
510 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
515 ObjectProperty *prop;
516 DevicePropertyInfoList *prop_list = NULL;
518 klass = object_class_by_name(typename);
520 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
524 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
526 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
527 "name", TYPE_DEVICE);
531 obj = object_new(typename);
533 QTAILQ_FOREACH(prop, &obj->properties, node) {
534 DevicePropertyInfo *info;
535 DevicePropertyInfoList *entry;
537 /* Skip Object and DeviceState properties */
538 if (strcmp(prop->name, "type") == 0 ||
539 strcmp(prop->name, "realized") == 0 ||
540 strcmp(prop->name, "hotpluggable") == 0 ||
541 strcmp(prop->name, "hotplugged") == 0 ||
542 strcmp(prop->name, "parent_bus") == 0) {
546 /* Skip legacy properties since they are just string versions of
547 * properties that we already list.
549 if (strstart(prop->name, "legacy-", NULL)) {
553 info = make_device_property_info(klass, prop->name, prop->type,
559 entry = g_malloc0(sizeof(*entry));
561 entry->next = prop_list;
570 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
572 return arch_query_cpu_definitions(errp);
575 void qmp_add_client(const char *protocol, const char *fdname,
576 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
582 fd = monitor_get_fd(cur_mon, fdname, errp);
587 if (strcmp(protocol, "spice") == 0) {
588 if (!qemu_using_spice(errp)) {
592 skipauth = has_skipauth ? skipauth : false;
593 tls = has_tls ? tls : false;
594 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
595 error_setg(errp, "spice failed to add client");
600 } else if (strcmp(protocol, "vnc") == 0) {
601 skipauth = has_skipauth ? skipauth : false;
602 vnc_display_add_client(NULL, fd, skipauth);
605 } else if ((s = qemu_chr_find(protocol)) != NULL) {
606 if (qemu_chr_add_client(s, fd) < 0) {
607 error_setg(errp, "failed to add client");
614 error_setg(errp, "protocol '%s' is invalid", protocol);
618 void object_add(const char *type, const char *id, const QDict *qdict,
619 Visitor *v, Error **errp)
624 Error *local_err = NULL;
626 klass = object_class_by_name(type);
628 error_setg(errp, "invalid object type: %s", type);
632 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
633 error_setg(errp, "object type '%s' isn't supported by object-add",
638 if (object_class_is_abstract(klass)) {
639 error_setg(errp, "object type '%s' is abstract", type);
643 obj = object_new(type);
645 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
646 object_property_set(obj, v, e->key, &local_err);
653 object_property_add_child(container_get(object_get_root(), "/objects"),
654 id, obj, &local_err);
659 user_creatable_complete(obj, &local_err);
661 object_property_del(container_get(object_get_root(), "/objects"),
667 error_propagate(errp, local_err);
672 int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
674 const char *type = qdict_get_str(qdict, "qom-type");
675 const char *id = qdict_get_str(qdict, "id");
676 QObject *props = qdict_get(qdict, "props");
677 const QDict *pdict = NULL;
678 Error *local_err = NULL;
679 QmpInputVisitor *qiv;
682 pdict = qobject_to_qdict(props);
684 error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
689 qiv = qmp_input_visitor_new(props);
690 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
691 qmp_input_visitor_cleanup(qiv);
695 qerror_report_err(local_err);
696 error_free(local_err);
703 void qmp_object_del(const char *id, Error **errp)
708 container = container_get(object_get_root(), "/objects");
709 obj = object_resolve_path_component(container, id);
711 error_setg(errp, "object id not found");
714 object_unparent(obj);
717 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
719 MemoryDeviceInfoList *head = NULL;
720 MemoryDeviceInfoList **prev = &head;
722 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
727 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
730 ACPIOSTInfoList *head = NULL;
731 ACPIOSTInfoList **prev = &head;
732 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
735 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
736 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
738 adevc->ospm_status(adev, &prev);
740 error_setg(errp, "command is not supported, missing ACPI device");