qemu_ram_block_by_name
[qemu/cris-port.git] / qmp.c
blobff54e5a765dd0d00aacad8e9b30571b631fdee5f
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 "sysemu/block-backend.h"
28 #include "qom/qom-qobject.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qapi/qmp/qobject.h"
31 #include "qapi/qmp-input-visitor.h"
32 #include "hw/boards.h"
33 #include "qom/object_interfaces.h"
34 #include "hw/mem/pc-dimm.h"
35 #include "hw/acpi/acpi_dev_interface.h"
37 NameInfo *qmp_query_name(Error **errp)
39 NameInfo *info = g_malloc0(sizeof(*info));
41 if (qemu_name) {
42 info->has_name = true;
43 info->name = g_strdup(qemu_name);
46 return info;
49 VersionInfo *qmp_query_version(Error **errp)
51 VersionInfo *info = g_new0(VersionInfo, 1);
52 const char *version = QEMU_VERSION;
53 const char *tmp;
54 int err;
56 info->qemu = g_new0(VersionTriple, 1);
57 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
58 assert(err == 0);
59 tmp++;
61 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
62 assert(err == 0);
63 tmp++;
65 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
66 assert(err == 0);
67 info->package = g_strdup(QEMU_PKGVERSION);
69 return info;
72 KvmInfo *qmp_query_kvm(Error **errp)
74 KvmInfo *info = g_malloc0(sizeof(*info));
76 info->enabled = kvm_enabled();
77 info->present = kvm_available();
79 return info;
82 UuidInfo *qmp_query_uuid(Error **errp)
84 UuidInfo *info = g_malloc0(sizeof(*info));
85 char uuid[64];
87 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
88 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
89 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
90 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
91 qemu_uuid[14], qemu_uuid[15]);
93 info->UUID = g_strdup(uuid);
94 return info;
97 void qmp_quit(Error **errp)
99 no_shutdown = 0;
100 qemu_system_shutdown_request();
103 void qmp_stop(Error **errp)
105 if (runstate_check(RUN_STATE_INMIGRATE)) {
106 autostart = 0;
107 } else {
108 vm_stop(RUN_STATE_PAUSED);
112 void qmp_system_reset(Error **errp)
114 qemu_system_reset_request();
117 void qmp_system_powerdown(Error **erp)
119 qemu_system_powerdown_request();
122 void qmp_cpu(int64_t index, Error **errp)
124 /* Just do nothing */
127 void qmp_cpu_add(int64_t id, Error **errp)
129 MachineClass *mc;
131 mc = MACHINE_GET_CLASS(current_machine);
132 if (mc->hot_add_cpu) {
133 mc->hot_add_cpu(id, errp);
134 } else {
135 error_setg(errp, "Not supported");
139 #ifndef CONFIG_VNC
140 /* If VNC support is enabled, the "true" query-vnc command is
141 defined in the VNC subsystem */
142 VncInfo *qmp_query_vnc(Error **errp)
144 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
145 return NULL;
148 VncInfo2List *qmp_query_vnc_servers(Error **errp)
150 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
151 return NULL;
153 #endif
155 #ifndef CONFIG_SPICE
157 * qmp-commands.hx ensures that QMP command query-spice exists only
158 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
159 * result. However, the QAPI schema is blissfully unaware of that,
160 * and the QAPI code generator happily generates a dead
161 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
162 * one, or else linking fails. FIXME Educate the QAPI schema on
163 * CONFIG_SPICE.
165 SpiceInfo *qmp_query_spice(Error **errp)
167 abort();
169 #endif
171 void qmp_cont(Error **errp)
173 Error *local_err = NULL;
174 BlockBackend *blk;
175 BlockDriverState *bs;
177 if (runstate_needs_reset()) {
178 error_setg(errp, "Resetting the Virtual Machine is required");
179 return;
180 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
181 return;
184 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
185 blk_iostatus_reset(blk);
187 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
188 bdrv_add_key(bs, NULL, &local_err);
189 if (local_err) {
190 error_propagate(errp, local_err);
191 return;
195 if (runstate_check(RUN_STATE_INMIGRATE)) {
196 autostart = 1;
197 } else {
198 vm_start();
202 void qmp_system_wakeup(Error **errp)
204 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
207 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
209 Object *obj;
210 bool ambiguous = false;
211 ObjectPropertyInfoList *props = NULL;
212 ObjectProperty *prop;
214 obj = object_resolve_path(path, &ambiguous);
215 if (obj == NULL) {
216 if (ambiguous) {
217 error_setg(errp, "Path '%s' is ambiguous", path);
218 } else {
219 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
220 "Device '%s' not found", path);
222 return NULL;
225 QTAILQ_FOREACH(prop, &obj->properties, node) {
226 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
228 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
229 entry->next = props;
230 props = entry;
232 entry->value->name = g_strdup(prop->name);
233 entry->value->type = g_strdup(prop->type);
236 return props;
239 void qmp_qom_set(const char *path, const char *property, QObject *value,
240 Error **errp)
242 Object *obj;
244 obj = object_resolve_path(path, NULL);
245 if (!obj) {
246 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
247 "Device '%s' not found", path);
248 return;
251 object_property_set_qobject(obj, value, property, errp);
254 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
256 Object *obj;
258 obj = object_resolve_path(path, NULL);
259 if (!obj) {
260 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
261 "Device '%s' not found", path);
262 return NULL;
265 return object_property_get_qobject(obj, property, errp);
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;
273 int rc;
275 if (has_connected) {
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) {
281 /* nothing */
282 } else {
283 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
284 return;
288 if (strcmp(protocol, "spice") == 0) {
289 if (!qemu_using_spice(errp)) {
290 return;
292 rc = qemu_spice_set_passwd(password, fail_if_connected,
293 disconnect_if_connected);
294 if (rc != 0) {
295 error_setg(errp, QERR_SET_PASSWD_FAILED);
297 return;
300 if (strcmp(protocol, "vnc") == 0) {
301 if (fail_if_connected || disconnect_if_connected) {
302 /* vnc supports "connected=keep" only */
303 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
304 return;
306 /* Note that setting an empty password will not disable login through
307 * this interface. */
308 rc = vnc_display_password(NULL, password);
309 if (rc < 0) {
310 error_setg(errp, QERR_SET_PASSWD_FAILED);
312 return;
315 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
318 void qmp_expire_password(const char *protocol, const char *whenstr,
319 Error **errp)
321 time_t when;
322 int rc;
324 if (strcmp(whenstr, "now") == 0) {
325 when = 0;
326 } else if (strcmp(whenstr, "never") == 0) {
327 when = TIME_MAX;
328 } else if (whenstr[0] == '+') {
329 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
330 } else {
331 when = strtoull(whenstr, NULL, 10);
334 if (strcmp(protocol, "spice") == 0) {
335 if (!qemu_using_spice(errp)) {
336 return;
338 rc = qemu_spice_set_pw_expire(when);
339 if (rc != 0) {
340 error_setg(errp, QERR_SET_PASSWD_FAILED);
342 return;
345 if (strcmp(protocol, "vnc") == 0) {
346 rc = vnc_display_pw_expire(NULL, when);
347 if (rc != 0) {
348 error_setg(errp, QERR_SET_PASSWD_FAILED);
350 return;
353 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
356 #ifdef CONFIG_VNC
357 void qmp_change_vnc_password(const char *password, Error **errp)
359 if (vnc_display_password(NULL, password) < 0) {
360 error_setg(errp, QERR_SET_PASSWD_FAILED);
364 static void qmp_change_vnc_listen(const char *target, Error **errp)
366 QemuOptsList *olist = qemu_find_opts("vnc");
367 QemuOpts *opts;
369 if (strstr(target, "id=")) {
370 error_setg(errp, "id not supported");
371 return;
374 opts = qemu_opts_find(olist, "default");
375 if (opts) {
376 qemu_opts_del(opts);
378 opts = vnc_parse(target, errp);
379 if (!opts) {
380 return;
383 vnc_display_open("default", errp);
386 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
387 Error **errp)
389 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
390 if (!has_arg) {
391 error_setg(errp, QERR_MISSING_PARAMETER, "password");
392 } else {
393 qmp_change_vnc_password(arg, errp);
395 } else {
396 qmp_change_vnc_listen(target, errp);
399 #else
400 void qmp_change_vnc_password(const char *password, Error **errp)
402 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
404 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
405 Error **errp)
407 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
409 #endif /* !CONFIG_VNC */
411 void qmp_change(const char *device, const char *target,
412 bool has_arg, const char *arg, Error **errp)
414 if (strcmp(device, "vnc") == 0) {
415 qmp_change_vnc(target, has_arg, arg, errp);
416 } else {
417 qmp_change_blockdev(device, target, arg, errp);
421 static void qom_list_types_tramp(ObjectClass *klass, void *data)
423 ObjectTypeInfoList *e, **pret = data;
424 ObjectTypeInfo *info;
426 info = g_malloc0(sizeof(*info));
427 info->name = g_strdup(object_class_get_name(klass));
429 e = g_malloc0(sizeof(*e));
430 e->value = info;
431 e->next = *pret;
432 *pret = e;
435 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
436 const char *implements,
437 bool has_abstract,
438 bool abstract,
439 Error **errp)
441 ObjectTypeInfoList *ret = NULL;
443 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
445 return ret;
448 /* Return a DevicePropertyInfo for a qdev property.
450 * If a qdev property with the given name does not exist, use the given default
451 * type. If the qdev property info should not be shown, return NULL.
453 * The caller must free the return value.
455 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
456 const char *name,
457 const char *default_type,
458 const char *description)
460 DevicePropertyInfo *info;
461 Property *prop;
463 do {
464 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
465 if (strcmp(name, prop->name) != 0) {
466 continue;
470 * TODO Properties without a parser are just for dirty hacks.
471 * qdev_prop_ptr is the only such PropertyInfo. It's marked
472 * for removal. This conditional should be removed along with
473 * it.
475 if (!prop->info->set) {
476 return NULL; /* no way to set it, don't show */
479 info = g_malloc0(sizeof(*info));
480 info->name = g_strdup(prop->name);
481 info->type = g_strdup(prop->info->name);
482 info->has_description = !!prop->info->description;
483 info->description = g_strdup(prop->info->description);
484 return info;
486 klass = object_class_get_parent(klass);
487 } while (klass != object_class_by_name(TYPE_DEVICE));
489 /* Not a qdev property, use the default type */
490 info = g_malloc0(sizeof(*info));
491 info->name = g_strdup(name);
492 info->type = g_strdup(default_type);
493 info->has_description = !!description;
494 info->description = g_strdup(description);
496 return info;
499 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
500 Error **errp)
502 ObjectClass *klass;
503 Object *obj;
504 ObjectProperty *prop;
505 DevicePropertyInfoList *prop_list = NULL;
507 klass = object_class_by_name(typename);
508 if (klass == NULL) {
509 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
510 "Device '%s' not found", typename);
511 return NULL;
514 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
515 if (klass == NULL) {
516 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
517 return NULL;
520 if (object_class_is_abstract(klass)) {
521 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
522 "non-abstract device type");
523 return NULL;
526 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
527 error_setg(errp, "Can't list properties of device '%s'", typename);
528 return NULL;
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) {
543 continue;
546 /* Skip legacy properties since they are just string versions of
547 * properties that we already list.
549 if (strstart(prop->name, "legacy-", NULL)) {
550 continue;
553 info = make_device_property_info(klass, prop->name, prop->type,
554 prop->description);
555 if (!info) {
556 continue;
559 entry = g_malloc0(sizeof(*entry));
560 entry->value = info;
561 entry->next = prop_list;
562 prop_list = entry;
565 object_unref(obj);
567 return 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,
577 Error **errp)
579 CharDriverState *s;
580 int fd;
582 fd = monitor_get_fd(cur_mon, fdname, errp);
583 if (fd < 0) {
584 return;
587 if (strcmp(protocol, "spice") == 0) {
588 if (!qemu_using_spice(errp)) {
589 close(fd);
590 return;
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");
596 close(fd);
598 return;
599 #ifdef CONFIG_VNC
600 } else if (strcmp(protocol, "vnc") == 0) {
601 skipauth = has_skipauth ? skipauth : false;
602 vnc_display_add_client(NULL, fd, skipauth);
603 return;
604 #endif
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");
608 close(fd);
609 return;
611 return;
614 error_setg(errp, "protocol '%s' is invalid", protocol);
615 close(fd);
618 void object_add(const char *type, const char *id, const QDict *qdict,
619 Visitor *v, Error **errp)
621 Object *obj;
622 ObjectClass *klass;
623 const QDictEntry *e;
624 Error *local_err = NULL;
626 klass = object_class_by_name(type);
627 if (!klass) {
628 error_setg(errp, "invalid object type: %s", type);
629 return;
632 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
633 error_setg(errp, "object type '%s' isn't supported by object-add",
634 type);
635 return;
638 if (object_class_is_abstract(klass)) {
639 error_setg(errp, "object type '%s' is abstract", type);
640 return;
643 obj = object_new(type);
644 if (qdict) {
645 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
646 object_property_set(obj, v, e->key, &local_err);
647 if (local_err) {
648 goto out;
653 object_property_add_child(object_get_objects_root(),
654 id, obj, &local_err);
655 if (local_err) {
656 goto out;
659 user_creatable_complete(obj, &local_err);
660 if (local_err) {
661 object_property_del(object_get_objects_root(),
662 id, &error_abort);
663 goto out;
665 out:
666 if (local_err) {
667 error_propagate(errp, local_err);
669 object_unref(obj);
672 void qmp_object_add(const char *type, const char *id,
673 bool has_props, QObject *props, Error **errp)
675 const QDict *pdict = NULL;
676 QmpInputVisitor *qiv;
678 if (props) {
679 pdict = qobject_to_qdict(props);
680 if (!pdict) {
681 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
682 return;
686 qiv = qmp_input_visitor_new(props);
687 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
688 qmp_input_visitor_cleanup(qiv);
691 void qmp_object_del(const char *id, Error **errp)
693 Object *container;
694 Object *obj;
696 container = object_get_objects_root();
697 obj = object_resolve_path_component(container, id);
698 if (!obj) {
699 error_setg(errp, "object id not found");
700 return;
703 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
704 error_setg(errp, "%s is in use, can not be deleted", id);
705 return;
707 object_unparent(obj);
710 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
712 MemoryDeviceInfoList *head = NULL;
713 MemoryDeviceInfoList **prev = &head;
715 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
717 return head;
720 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
722 bool ambig;
723 ACPIOSTInfoList *head = NULL;
724 ACPIOSTInfoList **prev = &head;
725 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
727 if (obj) {
728 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
729 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
731 adevc->ospm_status(adev, &prev);
732 } else {
733 error_setg(errp, "command is not supported, missing ACPI device");
736 return head;