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 "qmp-commands.h"
22 #include "sysemu/char.h"
23 #include "ui/qemu-spice.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/arch_init.h"
28 #include "sysemu/blockdev.h"
29 #include "sysemu/block-backend.h"
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qobject.h"
33 #include "qapi/qmp-input-visitor.h"
34 #include "hw/boards.h"
35 #include "qom/object_interfaces.h"
36 #include "hw/mem/pc-dimm.h"
37 #include "hw/acpi/acpi_dev_interface.h"
39 NameInfo *qmp_query_name(Error **errp)
41 NameInfo *info = g_malloc0(sizeof(*info));
44 info->has_name = true;
45 info->name = g_strdup(qemu_name);
51 VersionInfo *qmp_query_version(Error **errp)
53 VersionInfo *info = g_new0(VersionInfo, 1);
54 const char *version = QEMU_VERSION;
58 info->qemu = g_new0(VersionTriple, 1);
59 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
63 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
67 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
69 info->package = g_strdup(QEMU_PKGVERSION);
74 KvmInfo *qmp_query_kvm(Error **errp)
76 KvmInfo *info = g_malloc0(sizeof(*info));
78 info->enabled = kvm_enabled();
79 info->present = kvm_available();
84 UuidInfo *qmp_query_uuid(Error **errp)
86 UuidInfo *info = g_malloc0(sizeof(*info));
89 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
90 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
91 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
92 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
93 qemu_uuid[14], qemu_uuid[15]);
95 info->UUID = g_strdup(uuid);
99 void qmp_quit(Error **errp)
102 qemu_system_shutdown_request();
105 void qmp_stop(Error **errp)
107 /* if there is a dump in background, we should wait until the dump
109 if (dump_in_progress()) {
110 error_setg(errp, "There is a dump in process, please wait.");
114 if (runstate_check(RUN_STATE_INMIGRATE)) {
117 vm_stop(RUN_STATE_PAUSED);
121 void qmp_system_reset(Error **errp)
123 qemu_system_reset_request();
126 void qmp_system_powerdown(Error **erp)
128 qemu_system_powerdown_request();
131 void qmp_cpu(int64_t index, Error **errp)
133 /* Just do nothing */
136 void qmp_cpu_add(int64_t id, Error **errp)
140 mc = MACHINE_GET_CLASS(current_machine);
141 if (mc->hot_add_cpu) {
142 mc->hot_add_cpu(id, errp);
144 error_setg(errp, "Not supported");
149 /* If VNC support is enabled, the "true" query-vnc command is
150 defined in the VNC subsystem */
151 VncInfo *qmp_query_vnc(Error **errp)
153 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
157 VncInfo2List *qmp_query_vnc_servers(Error **errp)
159 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
166 * qmp-commands.hx ensures that QMP command query-spice exists only
167 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
168 * result. However, the QAPI schema is blissfully unaware of that,
169 * and the QAPI code generator happily generates a dead
170 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
171 * one, or else linking fails. FIXME Educate the QAPI schema on
174 SpiceInfo *qmp_query_spice(Error **errp)
180 void qmp_cont(Error **errp)
182 Error *local_err = NULL;
184 BlockDriverState *bs;
187 /* if there is a dump in background, we should wait until the dump
189 if (dump_in_progress()) {
190 error_setg(errp, "There is a dump in process, please wait.");
194 if (runstate_needs_reset()) {
195 error_setg(errp, "Resetting the Virtual Machine is required");
197 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
201 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
202 blk_iostatus_reset(blk);
205 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
206 bdrv_add_key(bs, NULL, &local_err);
208 error_propagate(errp, local_err);
213 /* Continuing after completed migration. Images have been inactivated to
214 * allow the destination to take control. Need to get control back now. */
215 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
216 runstate_check(RUN_STATE_POSTMIGRATE))
218 bdrv_invalidate_cache_all(&local_err);
220 error_propagate(errp, local_err);
225 if (runstate_check(RUN_STATE_INMIGRATE)) {
232 void qmp_system_wakeup(Error **errp)
234 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
237 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
240 bool ambiguous = false;
241 ObjectPropertyInfoList *props = NULL;
242 ObjectProperty *prop;
243 ObjectPropertyIterator iter;
245 obj = object_resolve_path(path, &ambiguous);
248 error_setg(errp, "Path '%s' is ambiguous", path);
250 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
251 "Device '%s' not found", path);
256 object_property_iter_init(&iter, obj);
257 while ((prop = object_property_iter_next(&iter))) {
258 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
260 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
264 entry->value->name = g_strdup(prop->name);
265 entry->value->type = g_strdup(prop->type);
271 void qmp_qom_set(const char *path, const char *property, QObject *value,
276 obj = object_resolve_path(path, NULL);
278 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
279 "Device '%s' not found", path);
283 object_property_set_qobject(obj, value, property, errp);
286 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
290 obj = object_resolve_path(path, NULL);
292 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
293 "Device '%s' not found", path);
297 return object_property_get_qobject(obj, property, errp);
300 void qmp_set_password(const char *protocol, const char *password,
301 bool has_connected, const char *connected, Error **errp)
303 int disconnect_if_connected = 0;
304 int fail_if_connected = 0;
308 if (strcmp(connected, "fail") == 0) {
309 fail_if_connected = 1;
310 } else if (strcmp(connected, "disconnect") == 0) {
311 disconnect_if_connected = 1;
312 } else if (strcmp(connected, "keep") == 0) {
315 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
320 if (strcmp(protocol, "spice") == 0) {
321 if (!qemu_using_spice(errp)) {
324 rc = qemu_spice_set_passwd(password, fail_if_connected,
325 disconnect_if_connected);
327 error_setg(errp, QERR_SET_PASSWD_FAILED);
332 if (strcmp(protocol, "vnc") == 0) {
333 if (fail_if_connected || disconnect_if_connected) {
334 /* vnc supports "connected=keep" only */
335 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
338 /* Note that setting an empty password will not disable login through
340 rc = vnc_display_password(NULL, password);
342 error_setg(errp, QERR_SET_PASSWD_FAILED);
347 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
350 void qmp_expire_password(const char *protocol, const char *whenstr,
356 if (strcmp(whenstr, "now") == 0) {
358 } else if (strcmp(whenstr, "never") == 0) {
360 } else if (whenstr[0] == '+') {
361 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
363 when = strtoull(whenstr, NULL, 10);
366 if (strcmp(protocol, "spice") == 0) {
367 if (!qemu_using_spice(errp)) {
370 rc = qemu_spice_set_pw_expire(when);
372 error_setg(errp, QERR_SET_PASSWD_FAILED);
377 if (strcmp(protocol, "vnc") == 0) {
378 rc = vnc_display_pw_expire(NULL, when);
380 error_setg(errp, QERR_SET_PASSWD_FAILED);
385 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
389 void qmp_change_vnc_password(const char *password, Error **errp)
391 if (vnc_display_password(NULL, password) < 0) {
392 error_setg(errp, QERR_SET_PASSWD_FAILED);
396 static void qmp_change_vnc_listen(const char *target, Error **errp)
398 QemuOptsList *olist = qemu_find_opts("vnc");
401 if (strstr(target, "id=")) {
402 error_setg(errp, "id not supported");
406 opts = qemu_opts_find(olist, "default");
410 opts = vnc_parse(target, errp);
415 vnc_display_open("default", errp);
418 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
421 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
423 error_setg(errp, QERR_MISSING_PARAMETER, "password");
425 qmp_change_vnc_password(arg, errp);
428 qmp_change_vnc_listen(target, errp);
432 void qmp_change_vnc_password(const char *password, Error **errp)
434 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
436 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
439 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
441 #endif /* !CONFIG_VNC */
443 void qmp_change(const char *device, const char *target,
444 bool has_arg, const char *arg, Error **errp)
446 if (strcmp(device, "vnc") == 0) {
447 qmp_change_vnc(target, has_arg, arg, errp);
449 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
454 static void qom_list_types_tramp(ObjectClass *klass, void *data)
456 ObjectTypeInfoList *e, **pret = data;
457 ObjectTypeInfo *info;
459 info = g_malloc0(sizeof(*info));
460 info->name = g_strdup(object_class_get_name(klass));
462 e = g_malloc0(sizeof(*e));
468 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
469 const char *implements,
474 ObjectTypeInfoList *ret = NULL;
476 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
481 /* Return a DevicePropertyInfo for a qdev property.
483 * If a qdev property with the given name does not exist, use the given default
484 * type. If the qdev property info should not be shown, return NULL.
486 * The caller must free the return value.
488 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
490 const char *default_type,
491 const char *description)
493 DevicePropertyInfo *info;
497 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
498 if (strcmp(name, prop->name) != 0) {
503 * TODO Properties without a parser are just for dirty hacks.
504 * qdev_prop_ptr is the only such PropertyInfo. It's marked
505 * for removal. This conditional should be removed along with
508 if (!prop->info->set) {
509 return NULL; /* no way to set it, don't show */
512 info = g_malloc0(sizeof(*info));
513 info->name = g_strdup(prop->name);
514 info->type = g_strdup(prop->info->name);
515 info->has_description = !!prop->info->description;
516 info->description = g_strdup(prop->info->description);
519 klass = object_class_get_parent(klass);
520 } while (klass != object_class_by_name(TYPE_DEVICE));
522 /* Not a qdev property, use the default type */
523 info = g_malloc0(sizeof(*info));
524 info->name = g_strdup(name);
525 info->type = g_strdup(default_type);
526 info->has_description = !!description;
527 info->description = g_strdup(description);
532 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
537 ObjectProperty *prop;
538 ObjectPropertyIterator iter;
539 DevicePropertyInfoList *prop_list = NULL;
541 klass = object_class_by_name(typename);
543 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
544 "Device '%s' not found", typename);
548 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
550 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
554 if (object_class_is_abstract(klass)) {
555 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
556 "non-abstract device type");
560 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
561 error_setg(errp, "Can't list properties of device '%s'", typename);
565 obj = object_new(typename);
567 object_property_iter_init(&iter, obj);
568 while ((prop = object_property_iter_next(&iter))) {
569 DevicePropertyInfo *info;
570 DevicePropertyInfoList *entry;
572 /* Skip Object and DeviceState properties */
573 if (strcmp(prop->name, "type") == 0 ||
574 strcmp(prop->name, "realized") == 0 ||
575 strcmp(prop->name, "hotpluggable") == 0 ||
576 strcmp(prop->name, "hotplugged") == 0 ||
577 strcmp(prop->name, "parent_bus") == 0) {
581 /* Skip legacy properties since they are just string versions of
582 * properties that we already list.
584 if (strstart(prop->name, "legacy-", NULL)) {
588 info = make_device_property_info(klass, prop->name, prop->type,
594 entry = g_malloc0(sizeof(*entry));
596 entry->next = prop_list;
605 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
607 return arch_query_cpu_definitions(errp);
610 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
614 return arch_query_cpu_model_expansion(type, model, errp);
617 void qmp_add_client(const char *protocol, const char *fdname,
618 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
624 fd = monitor_get_fd(cur_mon, fdname, errp);
629 if (strcmp(protocol, "spice") == 0) {
630 if (!qemu_using_spice(errp)) {
634 skipauth = has_skipauth ? skipauth : false;
635 tls = has_tls ? tls : false;
636 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
637 error_setg(errp, "spice failed to add client");
642 } else if (strcmp(protocol, "vnc") == 0) {
643 skipauth = has_skipauth ? skipauth : false;
644 vnc_display_add_client(NULL, fd, skipauth);
647 } else if ((s = qemu_chr_find(protocol)) != NULL) {
648 if (qemu_chr_add_client(s, fd) < 0) {
649 error_setg(errp, "failed to add client");
656 error_setg(errp, "protocol '%s' is invalid", protocol);
661 void qmp_object_add(const char *type, const char *id,
662 bool has_props, QObject *props, Error **errp)
664 const QDict *pdict = NULL;
669 pdict = qobject_to_qdict(props);
671 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
676 v = qmp_input_visitor_new(props, true);
677 obj = user_creatable_add_type(type, id, pdict, v, errp);
684 void qmp_object_del(const char *id, Error **errp)
686 user_creatable_del(id, errp);
689 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
691 MemoryDeviceInfoList *head = NULL;
692 MemoryDeviceInfoList **prev = &head;
694 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
699 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
702 ACPIOSTInfoList *head = NULL;
703 ACPIOSTInfoList **prev = &head;
704 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
707 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
708 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
710 adevc->ospm_status(adev, &prev);
712 error_setg(errp, "command is not supported, missing ACPI device");