target/arm: Makefile cleanup (softmmu)
[qemu/ar7.git] / monitor / qmp-cmds.c
blob01ce77e12923a31ccb5f67d09b7067ddeaeb7a8d
1 /*
2 * QEMU Management Protocol commands
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/osdep.h"
17 #include "qemu-common.h"
18 #include "qemu-version.h"
19 #include "qemu/cutils.h"
20 #include "qemu/option.h"
21 #include "monitor/monitor.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/uuid.h"
25 #include "chardev/char.h"
26 #include "ui/qemu-spice.h"
27 #include "ui/vnc.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/arch_init.h"
30 #include "hw/qdev.h"
31 #include "sysemu/blockdev.h"
32 #include "sysemu/block-backend.h"
33 #include "qom/qom-qobject.h"
34 #include "qapi/error.h"
35 #include "qapi/qapi-commands-block-core.h"
36 #include "qapi/qapi-commands-misc.h"
37 #include "qapi/qapi-commands-ui.h"
38 #include "qapi/qmp/qdict.h"
39 #include "qapi/qmp/qerror.h"
40 #include "qapi/qobject-input-visitor.h"
41 #include "hw/boards.h"
42 #include "qom/object_interfaces.h"
43 #include "hw/mem/memory-device.h"
44 #include "hw/acpi/acpi_dev_interface.h"
46 NameInfo *qmp_query_name(Error **errp)
48 NameInfo *info = g_malloc0(sizeof(*info));
50 if (qemu_name) {
51 info->has_name = true;
52 info->name = g_strdup(qemu_name);
55 return info;
58 VersionInfo *qmp_query_version(Error **errp)
60 VersionInfo *info = g_new0(VersionInfo, 1);
62 info->qemu = g_new0(VersionTriple, 1);
63 info->qemu->major = QEMU_VERSION_MAJOR;
64 info->qemu->minor = QEMU_VERSION_MINOR;
65 info->qemu->micro = QEMU_VERSION_MICRO;
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));
85 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
86 return info;
89 void qmp_quit(Error **errp)
91 no_shutdown = 0;
92 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
95 void qmp_stop(Error **errp)
97 /* if there is a dump in background, we should wait until the dump
98 * finished */
99 if (dump_in_progress()) {
100 error_setg(errp, "There is a dump in process, please wait.");
101 return;
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(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
116 void qmp_system_powerdown(Error **erp)
118 qemu_system_powerdown_request();
121 void qmp_cpu_add(int64_t id, Error **errp)
123 MachineClass *mc;
125 mc = MACHINE_GET_CLASS(current_machine);
126 if (mc->hot_add_cpu) {
127 mc->hot_add_cpu(id, errp);
128 } else {
129 error_setg(errp, "Not supported");
133 void qmp_x_exit_preconfig(Error **errp)
135 if (!runstate_check(RUN_STATE_PRECONFIG)) {
136 error_setg(errp, "The command is permitted only in '%s' state",
137 RunState_str(RUN_STATE_PRECONFIG));
138 return;
140 qemu_exit_preconfig_request();
143 void qmp_cont(Error **errp)
145 BlockBackend *blk;
146 BlockJob *job;
147 Error *local_err = NULL;
149 /* if there is a dump in background, we should wait until the dump
150 * finished */
151 if (dump_in_progress()) {
152 error_setg(errp, "There is a dump in process, please wait.");
153 return;
156 if (runstate_needs_reset()) {
157 error_setg(errp, "Resetting the Virtual Machine is required");
158 return;
159 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
160 return;
161 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
162 error_setg(errp, "Migration is not finalized yet");
163 return;
166 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
167 blk_iostatus_reset(blk);
170 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
171 block_job_iostatus_reset(job);
174 /* Continuing after completed migration. Images have been inactivated to
175 * allow the destination to take control. Need to get control back now.
177 * If there are no inactive block nodes (e.g. because the VM was just
178 * paused rather than completing a migration), bdrv_inactivate_all() simply
179 * doesn't do anything. */
180 bdrv_invalidate_cache_all(&local_err);
181 if (local_err) {
182 error_propagate(errp, local_err);
183 return;
186 if (runstate_check(RUN_STATE_INMIGRATE)) {
187 autostart = 1;
188 } else {
189 vm_start();
193 void qmp_system_wakeup(Error **errp)
195 if (!qemu_wakeup_suspend_enabled()) {
196 error_setg(errp,
197 "wake-up from suspend is not supported by this guest");
198 return;
201 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
204 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
206 Object *obj;
207 bool ambiguous = false;
208 ObjectPropertyInfoList *props = NULL;
209 ObjectProperty *prop;
210 ObjectPropertyIterator iter;
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 object_property_iter_init(&iter, obj);
224 while ((prop = object_property_iter_next(&iter))) {
225 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
227 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
228 entry->next = props;
229 props = entry;
231 entry->value->name = g_strdup(prop->name);
232 entry->value->type = g_strdup(prop->type);
235 return props;
238 void qmp_qom_set(const char *path, const char *property, QObject *value,
239 Error **errp)
241 Object *obj;
243 obj = object_resolve_path(path, NULL);
244 if (!obj) {
245 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
246 "Device '%s' not found", path);
247 return;
250 object_property_set_qobject(obj, value, property, errp);
253 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
255 Object *obj;
257 obj = object_resolve_path(path, NULL);
258 if (!obj) {
259 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
260 "Device '%s' not found", path);
261 return NULL;
264 return object_property_get_qobject(obj, property, errp);
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;
272 int rc;
274 if (has_connected) {
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) {
280 /* nothing */
281 } else {
282 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
283 return;
287 if (strcmp(protocol, "spice") == 0) {
288 if (!qemu_using_spice(errp)) {
289 return;
291 rc = qemu_spice_set_passwd(password, fail_if_connected,
292 disconnect_if_connected);
293 if (rc != 0) {
294 error_setg(errp, QERR_SET_PASSWD_FAILED);
296 return;
299 if (strcmp(protocol, "vnc") == 0) {
300 if (fail_if_connected || disconnect_if_connected) {
301 /* vnc supports "connected=keep" only */
302 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
303 return;
305 /* Note that setting an empty password will not disable login through
306 * this interface. */
307 rc = vnc_display_password(NULL, password);
308 if (rc < 0) {
309 error_setg(errp, QERR_SET_PASSWD_FAILED);
311 return;
314 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
317 void qmp_expire_password(const char *protocol, const char *whenstr,
318 Error **errp)
320 time_t when;
321 int rc;
323 if (strcmp(whenstr, "now") == 0) {
324 when = 0;
325 } else if (strcmp(whenstr, "never") == 0) {
326 when = TIME_MAX;
327 } else if (whenstr[0] == '+') {
328 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
329 } else {
330 when = strtoull(whenstr, NULL, 10);
333 if (strcmp(protocol, "spice") == 0) {
334 if (!qemu_using_spice(errp)) {
335 return;
337 rc = qemu_spice_set_pw_expire(when);
338 if (rc != 0) {
339 error_setg(errp, QERR_SET_PASSWD_FAILED);
341 return;
344 if (strcmp(protocol, "vnc") == 0) {
345 rc = vnc_display_pw_expire(NULL, when);
346 if (rc != 0) {
347 error_setg(errp, QERR_SET_PASSWD_FAILED);
349 return;
352 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
355 #ifdef CONFIG_VNC
356 void qmp_change_vnc_password(const char *password, Error **errp)
358 if (vnc_display_password(NULL, password) < 0) {
359 error_setg(errp, QERR_SET_PASSWD_FAILED);
363 static void qmp_change_vnc_listen(const char *target, Error **errp)
365 QemuOptsList *olist = qemu_find_opts("vnc");
366 QemuOpts *opts;
368 if (strstr(target, "id=")) {
369 error_setg(errp, "id not supported");
370 return;
373 opts = qemu_opts_find(olist, "default");
374 if (opts) {
375 qemu_opts_del(opts);
377 opts = vnc_parse(target, errp);
378 if (!opts) {
379 return;
382 vnc_display_open("default", errp);
385 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
386 Error **errp)
388 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
389 if (!has_arg) {
390 error_setg(errp, QERR_MISSING_PARAMETER, "password");
391 } else {
392 qmp_change_vnc_password(arg, errp);
394 } else {
395 qmp_change_vnc_listen(target, errp);
398 #endif /* !CONFIG_VNC */
400 void qmp_change(const char *device, const char *target,
401 bool has_arg, const char *arg, Error **errp)
403 if (strcmp(device, "vnc") == 0) {
404 #ifdef CONFIG_VNC
405 qmp_change_vnc(target, has_arg, arg, errp);
406 #else
407 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
408 #endif
409 } else {
410 qmp_blockdev_change_medium(true, device, false, NULL, target,
411 has_arg, arg, false, 0, errp);
415 static void qom_list_types_tramp(ObjectClass *klass, void *data)
417 ObjectTypeInfoList *e, **pret = data;
418 ObjectTypeInfo *info;
419 ObjectClass *parent = object_class_get_parent(klass);
421 info = g_malloc0(sizeof(*info));
422 info->name = g_strdup(object_class_get_name(klass));
423 info->has_abstract = info->abstract = object_class_is_abstract(klass);
424 if (parent) {
425 info->has_parent = true;
426 info->parent = g_strdup(object_class_get_name(parent));
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 ObjectPropertyInfo *make_device_property_info(ObjectClass *klass,
456 const char *name,
457 const char *default_type,
458 const char *description)
460 ObjectPropertyInfo *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 && !prop->info->create) {
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 = default_type ? g_strdup(default_type)
482 : g_strdup(prop->info->name);
483 info->has_description = !!prop->info->description;
484 info->description = g_strdup(prop->info->description);
485 return info;
487 klass = object_class_get_parent(klass);
488 } while (klass != object_class_by_name(TYPE_DEVICE));
490 /* Not a qdev property, use the default type */
491 info = g_malloc0(sizeof(*info));
492 info->name = g_strdup(name);
493 info->type = g_strdup(default_type);
494 info->has_description = !!description;
495 info->description = g_strdup(description);
497 return info;
500 ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
501 Error **errp)
503 ObjectClass *klass;
504 Object *obj;
505 ObjectProperty *prop;
506 ObjectPropertyIterator iter;
507 ObjectPropertyInfoList *prop_list = NULL;
509 klass = object_class_by_name(typename);
510 if (klass == NULL) {
511 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
512 "Device '%s' not found", typename);
513 return NULL;
516 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
517 if (klass == NULL) {
518 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
519 return NULL;
522 if (object_class_is_abstract(klass)) {
523 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
524 "non-abstract device type");
525 return NULL;
528 obj = object_new(typename);
530 object_property_iter_init(&iter, obj);
531 while ((prop = object_property_iter_next(&iter))) {
532 ObjectPropertyInfo *info;
533 ObjectPropertyInfoList *entry;
535 /* Skip Object and DeviceState properties */
536 if (strcmp(prop->name, "type") == 0 ||
537 strcmp(prop->name, "realized") == 0 ||
538 strcmp(prop->name, "hotpluggable") == 0 ||
539 strcmp(prop->name, "hotplugged") == 0 ||
540 strcmp(prop->name, "parent_bus") == 0) {
541 continue;
544 /* Skip legacy properties since they are just string versions of
545 * properties that we already list.
547 if (strstart(prop->name, "legacy-", NULL)) {
548 continue;
551 info = make_device_property_info(klass, prop->name, prop->type,
552 prop->description);
553 if (!info) {
554 continue;
557 entry = g_malloc0(sizeof(*entry));
558 entry->value = info;
559 entry->next = prop_list;
560 prop_list = entry;
563 object_unref(obj);
565 return prop_list;
568 ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
569 Error **errp)
571 ObjectClass *klass;
572 Object *obj = NULL;
573 ObjectProperty *prop;
574 ObjectPropertyIterator iter;
575 ObjectPropertyInfoList *prop_list = NULL;
577 klass = object_class_by_name(typename);
578 if (klass == NULL) {
579 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
580 "Class '%s' not found", typename);
581 return NULL;
584 klass = object_class_dynamic_cast(klass, TYPE_OBJECT);
585 if (klass == NULL) {
586 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_OBJECT);
587 return NULL;
590 if (object_class_is_abstract(klass)) {
591 object_class_property_iter_init(&iter, klass);
592 } else {
593 obj = object_new(typename);
594 object_property_iter_init(&iter, obj);
596 while ((prop = object_property_iter_next(&iter))) {
597 ObjectPropertyInfo *info;
598 ObjectPropertyInfoList *entry;
600 info = g_malloc0(sizeof(*info));
601 info->name = g_strdup(prop->name);
602 info->type = g_strdup(prop->type);
603 info->has_description = !!prop->description;
604 info->description = g_strdup(prop->description);
606 entry = g_malloc0(sizeof(*entry));
607 entry->value = info;
608 entry->next = prop_list;
609 prop_list = entry;
612 object_unref(obj);
614 return prop_list;
617 void qmp_add_client(const char *protocol, const char *fdname,
618 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
619 Error **errp)
621 Chardev *s;
622 int fd;
624 fd = monitor_get_fd(cur_mon, fdname, errp);
625 if (fd < 0) {
626 return;
629 if (strcmp(protocol, "spice") == 0) {
630 if (!qemu_using_spice(errp)) {
631 close(fd);
632 return;
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");
638 close(fd);
640 return;
641 #ifdef CONFIG_VNC
642 } else if (strcmp(protocol, "vnc") == 0) {
643 skipauth = has_skipauth ? skipauth : false;
644 vnc_display_add_client(NULL, fd, skipauth);
645 return;
646 #endif
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");
650 close(fd);
651 return;
653 return;
656 error_setg(errp, "protocol '%s' is invalid", protocol);
657 close(fd);
661 void qmp_object_add(const char *type, const char *id,
662 bool has_props, QObject *props, Error **errp)
664 QDict *pdict;
665 Visitor *v;
666 Object *obj;
668 if (props) {
669 pdict = qobject_to(QDict, props);
670 if (!pdict) {
671 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
672 return;
674 qobject_ref(pdict);
675 } else {
676 pdict = qdict_new();
679 v = qobject_input_visitor_new(QOBJECT(pdict));
680 obj = user_creatable_add_type(type, id, pdict, v, errp);
681 visit_free(v);
682 if (obj) {
683 object_unref(obj);
685 qobject_unref(pdict);
688 void qmp_object_del(const char *id, Error **errp)
690 user_creatable_del(id, errp);
693 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
695 return qmp_memory_device_list();
698 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
700 bool ambig;
701 ACPIOSTInfoList *head = NULL;
702 ACPIOSTInfoList **prev = &head;
703 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
705 if (obj) {
706 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
707 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
709 adevc->ospm_status(adev, &prev);
710 } else {
711 error_setg(errp, "command is not supported, missing ACPI device");
714 return head;
717 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
719 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
721 mem_info->base_memory = ram_size;
723 mem_info->plugged_memory = get_plugged_memory_size();
724 mem_info->has_plugged_memory =
725 mem_info->plugged_memory != (uint64_t)-1;
727 return mem_info;