ahci: clean up initial d2h semantics
[qemu/kevin.git] / qmp.c
blob9623c802abfb7cbbdcc2e16f34e2c99fccb6bc5f
1 /*
2 * QEMU Management Protocol
4 * Copyright IBM, Corp. 2011
6 * Authors:
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 "monitor/monitor.h"
18 #include "sysemu/sysemu.h"
19 #include "qmp-commands.h"
20 #include "sysemu/char.h"
21 #include "ui/qemu-spice.h"
22 #include "ui/vnc.h"
23 #include "sysemu/kvm.h"
24 #include "sysemu/arch_init.h"
25 #include "hw/qdev.h"
26 #include "sysemu/blockdev.h"
27 #include "qom/qom-qobject.h"
28 #include "qapi/qmp/qerror.h"
29 #include "qapi/qmp/qobject.h"
30 #include "qapi/qmp-input-visitor.h"
31 #include "hw/boards.h"
32 #include "qom/object_interfaces.h"
33 #include "hw/mem/pc-dimm.h"
34 #include "hw/acpi/acpi_dev_interface.h"
36 NameInfo *qmp_query_name(Error **errp)
38 NameInfo *info = g_malloc0(sizeof(*info));
40 if (qemu_name) {
41 info->has_name = true;
42 info->name = g_strdup(qemu_name);
45 return info;
48 VersionInfo *qmp_query_version(Error **errp)
50 VersionInfo *info = g_new0(VersionInfo, 1);
51 const char *version = QEMU_VERSION;
52 const char *tmp;
53 int err;
55 info->qemu = g_new0(VersionTriple, 1);
56 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
57 assert(err == 0);
58 tmp++;
60 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
61 assert(err == 0);
62 tmp++;
64 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
65 assert(err == 0);
66 info->package = g_strdup(QEMU_PKGVERSION);
68 return info;
71 KvmInfo *qmp_query_kvm(Error **errp)
73 KvmInfo *info = g_malloc0(sizeof(*info));
75 info->enabled = kvm_enabled();
76 info->present = kvm_available();
78 return info;
81 UuidInfo *qmp_query_uuid(Error **errp)
83 UuidInfo *info = g_malloc0(sizeof(*info));
84 char uuid[64];
86 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
87 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
88 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
89 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
90 qemu_uuid[14], qemu_uuid[15]);
92 info->UUID = g_strdup(uuid);
93 return info;
96 void qmp_quit(Error **errp)
98 no_shutdown = 0;
99 qemu_system_shutdown_request();
102 void qmp_stop(Error **errp)
104 if (runstate_check(RUN_STATE_INMIGRATE)) {
105 autostart = 0;
106 } else {
107 vm_stop(RUN_STATE_PAUSED);
111 void qmp_system_reset(Error **errp)
113 qemu_system_reset_request();
116 void qmp_system_powerdown(Error **erp)
118 qemu_system_powerdown_request();
121 void qmp_cpu(int64_t index, Error **errp)
123 /* Just do nothing */
126 void qmp_cpu_add(int64_t id, Error **errp)
128 MachineClass *mc;
130 mc = MACHINE_GET_CLASS(current_machine);
131 if (mc->hot_add_cpu) {
132 mc->hot_add_cpu(id, errp);
133 } else {
134 error_setg(errp, "Not supported");
138 #ifndef CONFIG_VNC
139 /* If VNC support is enabled, the "true" query-vnc command is
140 defined in the VNC subsystem */
141 VncInfo *qmp_query_vnc(Error **errp)
143 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
144 return NULL;
147 VncInfo2List *qmp_query_vnc_servers(Error **errp)
149 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
150 return NULL;
152 #endif
154 #ifndef CONFIG_SPICE
156 * qmp-commands.hx ensures that QMP command query-spice exists only
157 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
158 * result. However, the QAPI schema is blissfully unaware of that,
159 * and the QAPI code generator happily generates a dead
160 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
161 * Provide it one, or else linking fails.
162 * FIXME Educate the QAPI schema on CONFIG_SPICE.
164 SpiceInfo *qmp_query_spice(Error **errp)
166 abort();
168 #endif
170 void qmp_cont(Error **errp)
172 Error *local_err = NULL;
173 BlockDriverState *bs;
175 if (runstate_needs_reset()) {
176 error_setg(errp, "Resetting the Virtual Machine is required");
177 return;
178 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
179 return;
182 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
183 bdrv_iostatus_reset(bs);
185 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
186 bdrv_add_key(bs, NULL, &local_err);
187 if (local_err) {
188 error_propagate(errp, local_err);
189 return;
193 if (runstate_check(RUN_STATE_INMIGRATE)) {
194 autostart = 1;
195 } else {
196 vm_start();
200 void qmp_system_wakeup(Error **errp)
202 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
205 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
207 Object *obj;
208 bool ambiguous = false;
209 ObjectPropertyInfoList *props = NULL;
210 ObjectProperty *prop;
212 obj = object_resolve_path(path, &ambiguous);
213 if (obj == NULL) {
214 if (ambiguous) {
215 error_setg(errp, "Path '%s' is ambiguous", path);
216 } else {
217 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
218 "Device '%s' not found", path);
220 return NULL;
223 QTAILQ_FOREACH(prop, &obj->properties, node) {
224 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
226 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
227 entry->next = props;
228 props = entry;
230 entry->value->name = g_strdup(prop->name);
231 entry->value->type = g_strdup(prop->type);
234 return props;
237 /* FIXME: teach qapi about how to pass through Visitors */
238 void qmp_qom_set(QDict *qdict, QObject **ret, Error **errp)
240 const char *path = qdict_get_str(qdict, "path");
241 const char *property = qdict_get_str(qdict, "property");
242 QObject *value = qdict_get(qdict, "value");
243 Object *obj;
245 obj = object_resolve_path(path, NULL);
246 if (!obj) {
247 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
248 "Device '%s' not found", path);
249 return;
252 object_property_set_qobject(obj, value, property, errp);
255 void qmp_qom_get(QDict *qdict, QObject **ret, Error **errp)
257 const char *path = qdict_get_str(qdict, "path");
258 const char *property = qdict_get_str(qdict, "property");
259 Object *obj;
261 obj = object_resolve_path(path, NULL);
262 if (!obj) {
263 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
264 "Device '%s' not found", path);
265 return;
268 *ret = object_property_get_qobject(obj, property, errp);
271 void qmp_set_password(const char *protocol, const char *password,
272 bool has_connected, const char *connected, Error **errp)
274 int disconnect_if_connected = 0;
275 int fail_if_connected = 0;
276 int rc;
278 if (has_connected) {
279 if (strcmp(connected, "fail") == 0) {
280 fail_if_connected = 1;
281 } else if (strcmp(connected, "disconnect") == 0) {
282 disconnect_if_connected = 1;
283 } else if (strcmp(connected, "keep") == 0) {
284 /* nothing */
285 } else {
286 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
287 return;
291 if (strcmp(protocol, "spice") == 0) {
292 if (!qemu_using_spice(errp)) {
293 return;
295 rc = qemu_spice_set_passwd(password, fail_if_connected,
296 disconnect_if_connected);
297 if (rc != 0) {
298 error_setg(errp, QERR_SET_PASSWD_FAILED);
300 return;
303 if (strcmp(protocol, "vnc") == 0) {
304 if (fail_if_connected || disconnect_if_connected) {
305 /* vnc supports "connected=keep" only */
306 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
307 return;
309 /* Note that setting an empty password will not disable login through
310 * this interface. */
311 rc = vnc_display_password(NULL, password);
312 if (rc < 0) {
313 error_setg(errp, QERR_SET_PASSWD_FAILED);
315 return;
318 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
321 void qmp_expire_password(const char *protocol, const char *whenstr,
322 Error **errp)
324 time_t when;
325 int rc;
327 if (strcmp(whenstr, "now") == 0) {
328 when = 0;
329 } else if (strcmp(whenstr, "never") == 0) {
330 when = TIME_MAX;
331 } else if (whenstr[0] == '+') {
332 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
333 } else {
334 when = strtoull(whenstr, NULL, 10);
337 if (strcmp(protocol, "spice") == 0) {
338 if (!qemu_using_spice(errp)) {
339 return;
341 rc = qemu_spice_set_pw_expire(when);
342 if (rc != 0) {
343 error_setg(errp, QERR_SET_PASSWD_FAILED);
345 return;
348 if (strcmp(protocol, "vnc") == 0) {
349 rc = vnc_display_pw_expire(NULL, when);
350 if (rc != 0) {
351 error_setg(errp, QERR_SET_PASSWD_FAILED);
353 return;
356 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
359 #ifdef CONFIG_VNC
360 void qmp_change_vnc_password(const char *password, Error **errp)
362 if (vnc_display_password(NULL, password) < 0) {
363 error_setg(errp, QERR_SET_PASSWD_FAILED);
367 static void qmp_change_vnc_listen(const char *target, Error **errp)
369 QemuOptsList *olist = qemu_find_opts("vnc");
370 QemuOpts *opts;
372 if (strstr(target, "id=")) {
373 error_setg(errp, "id not supported");
374 return;
377 opts = qemu_opts_find(olist, "default");
378 if (opts) {
379 qemu_opts_del(opts);
381 opts = vnc_parse(target, errp);
382 if (!opts) {
383 return;
386 vnc_display_open("default", errp);
389 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
390 Error **errp)
392 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
393 if (!has_arg) {
394 error_setg(errp, QERR_MISSING_PARAMETER, "password");
395 } else {
396 qmp_change_vnc_password(arg, errp);
398 } else {
399 qmp_change_vnc_listen(target, errp);
402 #else
403 void qmp_change_vnc_password(const char *password, Error **errp)
405 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
407 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
408 Error **errp)
410 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
412 #endif /* !CONFIG_VNC */
414 void qmp_change(const char *device, const char *target,
415 bool has_arg, const char *arg, Error **errp)
417 if (strcmp(device, "vnc") == 0) {
418 qmp_change_vnc(target, has_arg, arg, errp);
419 } else {
420 qmp_change_blockdev(device, target, arg, errp);
424 static void qom_list_types_tramp(ObjectClass *klass, void *data)
426 ObjectTypeInfoList *e, **pret = data;
427 ObjectTypeInfo *info;
429 info = g_malloc0(sizeof(*info));
430 info->name = g_strdup(object_class_get_name(klass));
432 e = g_malloc0(sizeof(*e));
433 e->value = info;
434 e->next = *pret;
435 *pret = e;
438 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
439 const char *implements,
440 bool has_abstract,
441 bool abstract,
442 Error **errp)
444 ObjectTypeInfoList *ret = NULL;
446 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
448 return ret;
451 /* Return a DevicePropertyInfo for a qdev property.
453 * If a qdev property with the given name does not exist, use the given default
454 * type. If the qdev property info should not be shown, return NULL.
456 * The caller must free the return value.
458 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
459 const char *name,
460 const char *default_type,
461 const char *description)
463 DevicePropertyInfo *info;
464 Property *prop;
466 do {
467 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
468 if (strcmp(name, prop->name) != 0) {
469 continue;
473 * TODO Properties without a parser are just for dirty hacks.
474 * qdev_prop_ptr is the only such PropertyInfo. It's marked
475 * for removal. This conditional should be removed along with
476 * it.
478 if (!prop->info->set) {
479 return NULL; /* no way to set it, don't show */
482 info = g_malloc0(sizeof(*info));
483 info->name = g_strdup(prop->name);
484 info->type = g_strdup(prop->info->name);
485 info->has_description = !!prop->info->description;
486 info->description = g_strdup(prop->info->description);
487 return info;
489 klass = object_class_get_parent(klass);
490 } while (klass != object_class_by_name(TYPE_DEVICE));
492 /* Not a qdev property, use the default type */
493 info = g_malloc0(sizeof(*info));
494 info->name = g_strdup(name);
495 info->type = g_strdup(default_type);
496 info->has_description = !!description;
497 info->description = g_strdup(description);
499 return info;
502 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
503 Error **errp)
505 ObjectClass *klass;
506 Object *obj;
507 ObjectProperty *prop;
508 DevicePropertyInfoList *prop_list = NULL;
510 klass = object_class_by_name(typename);
511 if (klass == NULL) {
512 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
513 "Device '%s' not found", typename);
514 return NULL;
517 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
518 if (klass == NULL) {
519 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
520 return NULL;
523 obj = object_new(typename);
525 QTAILQ_FOREACH(prop, &obj->properties, node) {
526 DevicePropertyInfo *info;
527 DevicePropertyInfoList *entry;
529 /* Skip Object and DeviceState properties */
530 if (strcmp(prop->name, "type") == 0 ||
531 strcmp(prop->name, "realized") == 0 ||
532 strcmp(prop->name, "hotpluggable") == 0 ||
533 strcmp(prop->name, "hotplugged") == 0 ||
534 strcmp(prop->name, "parent_bus") == 0) {
535 continue;
538 /* Skip legacy properties since they are just string versions of
539 * properties that we already list.
541 if (strstart(prop->name, "legacy-", NULL)) {
542 continue;
545 info = make_device_property_info(klass, prop->name, prop->type,
546 prop->description);
547 if (!info) {
548 continue;
551 entry = g_malloc0(sizeof(*entry));
552 entry->value = info;
553 entry->next = prop_list;
554 prop_list = entry;
557 object_unref(obj);
559 return prop_list;
562 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
564 return arch_query_cpu_definitions(errp);
567 void qmp_add_client(const char *protocol, const char *fdname,
568 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
569 Error **errp)
571 CharDriverState *s;
572 int fd;
574 fd = monitor_get_fd(cur_mon, fdname, errp);
575 if (fd < 0) {
576 return;
579 if (strcmp(protocol, "spice") == 0) {
580 if (!qemu_using_spice(errp)) {
581 close(fd);
582 return;
584 skipauth = has_skipauth ? skipauth : false;
585 tls = has_tls ? tls : false;
586 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
587 error_setg(errp, "spice failed to add client");
588 close(fd);
590 return;
591 #ifdef CONFIG_VNC
592 } else if (strcmp(protocol, "vnc") == 0) {
593 skipauth = has_skipauth ? skipauth : false;
594 vnc_display_add_client(NULL, fd, skipauth);
595 return;
596 #endif
597 } else if ((s = qemu_chr_find(protocol)) != NULL) {
598 if (qemu_chr_add_client(s, fd) < 0) {
599 error_setg(errp, "failed to add client");
600 close(fd);
601 return;
603 return;
606 error_setg(errp, "protocol '%s' is invalid", protocol);
607 close(fd);
610 void object_add(const char *type, const char *id, const QDict *qdict,
611 Visitor *v, Error **errp)
613 Object *obj;
614 ObjectClass *klass;
615 const QDictEntry *e;
616 Error *local_err = NULL;
618 klass = object_class_by_name(type);
619 if (!klass) {
620 error_setg(errp, "invalid object type: %s", type);
621 return;
624 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
625 error_setg(errp, "object type '%s' isn't supported by object-add",
626 type);
627 return;
630 if (object_class_is_abstract(klass)) {
631 error_setg(errp, "object type '%s' is abstract", type);
632 return;
635 obj = object_new(type);
636 if (qdict) {
637 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
638 object_property_set(obj, v, e->key, &local_err);
639 if (local_err) {
640 goto out;
645 object_property_add_child(object_get_objects_root(),
646 id, obj, &local_err);
647 if (local_err) {
648 goto out;
651 user_creatable_complete(obj, &local_err);
652 if (local_err) {
653 object_property_del(object_get_objects_root(),
654 id, &error_abort);
655 goto out;
657 out:
658 if (local_err) {
659 error_propagate(errp, local_err);
661 object_unref(obj);
664 void qmp_object_add(QDict *qdict, QObject **ret, Error **errp)
666 const char *type = qdict_get_str(qdict, "qom-type");
667 const char *id = qdict_get_str(qdict, "id");
668 QObject *props = qdict_get(qdict, "props");
669 const QDict *pdict = NULL;
670 QmpInputVisitor *qiv;
672 if (props) {
673 pdict = qobject_to_qdict(props);
674 if (!pdict) {
675 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
676 return;
680 qiv = qmp_input_visitor_new(props);
681 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
682 qmp_input_visitor_cleanup(qiv);
685 void qmp_object_del(const char *id, Error **errp)
687 Object *container;
688 Object *obj;
690 container = object_get_objects_root();
691 obj = object_resolve_path_component(container, id);
692 if (!obj) {
693 error_setg(errp, "object id not found");
694 return;
697 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
698 error_setg(errp, "%s is in use, can not be deleted", id);
699 return;
701 object_unparent(obj);
704 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
706 MemoryDeviceInfoList *head = NULL;
707 MemoryDeviceInfoList **prev = &head;
709 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
711 return head;
714 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
716 bool ambig;
717 ACPIOSTInfoList *head = NULL;
718 ACPIOSTInfoList **prev = &head;
719 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
721 if (obj) {
722 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
723 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
725 adevc->ospm_status(adev, &prev);
726 } else {
727 error_setg(errp, "command is not supported, missing ACPI device");
730 return head;