pc: Simplify xen_load_linux() signature
[qemu/kevin.git] / qmp.c
blob53affe2cfd468ea93c1e3b5e6df6744d44db71d7
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 /* Continuing after completed migration. Images have been inactivated to
196 * allow the destination to take control. Need to get control back now. */
197 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
198 runstate_check(RUN_STATE_POSTMIGRATE))
200 bdrv_invalidate_cache_all(&local_err);
201 if (local_err) {
202 error_propagate(errp, local_err);
203 return;
207 if (runstate_check(RUN_STATE_INMIGRATE)) {
208 autostart = 1;
209 } else {
210 vm_start();
214 void qmp_system_wakeup(Error **errp)
216 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
219 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
221 Object *obj;
222 bool ambiguous = false;
223 ObjectPropertyInfoList *props = NULL;
224 ObjectProperty *prop;
225 ObjectPropertyIterator iter;
227 obj = object_resolve_path(path, &ambiguous);
228 if (obj == NULL) {
229 if (ambiguous) {
230 error_setg(errp, "Path '%s' is ambiguous", path);
231 } else {
232 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
233 "Device '%s' not found", path);
235 return NULL;
238 object_property_iter_init(&iter, obj);
239 while ((prop = object_property_iter_next(&iter))) {
240 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
242 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
243 entry->next = props;
244 props = entry;
246 entry->value->name = g_strdup(prop->name);
247 entry->value->type = g_strdup(prop->type);
250 return props;
253 void qmp_qom_set(const char *path, const char *property, QObject *value,
254 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;
265 object_property_set_qobject(obj, value, property, errp);
268 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
270 Object *obj;
272 obj = object_resolve_path(path, NULL);
273 if (!obj) {
274 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
275 "Device '%s' not found", path);
276 return NULL;
279 return object_property_get_qobject(obj, property, errp);
282 void qmp_set_password(const char *protocol, const char *password,
283 bool has_connected, const char *connected, Error **errp)
285 int disconnect_if_connected = 0;
286 int fail_if_connected = 0;
287 int rc;
289 if (has_connected) {
290 if (strcmp(connected, "fail") == 0) {
291 fail_if_connected = 1;
292 } else if (strcmp(connected, "disconnect") == 0) {
293 disconnect_if_connected = 1;
294 } else if (strcmp(connected, "keep") == 0) {
295 /* nothing */
296 } else {
297 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
298 return;
302 if (strcmp(protocol, "spice") == 0) {
303 if (!qemu_using_spice(errp)) {
304 return;
306 rc = qemu_spice_set_passwd(password, fail_if_connected,
307 disconnect_if_connected);
308 if (rc != 0) {
309 error_setg(errp, QERR_SET_PASSWD_FAILED);
311 return;
314 if (strcmp(protocol, "vnc") == 0) {
315 if (fail_if_connected || disconnect_if_connected) {
316 /* vnc supports "connected=keep" only */
317 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
318 return;
320 /* Note that setting an empty password will not disable login through
321 * this interface. */
322 rc = vnc_display_password(NULL, password);
323 if (rc < 0) {
324 error_setg(errp, QERR_SET_PASSWD_FAILED);
326 return;
329 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
332 void qmp_expire_password(const char *protocol, const char *whenstr,
333 Error **errp)
335 time_t when;
336 int rc;
338 if (strcmp(whenstr, "now") == 0) {
339 when = 0;
340 } else if (strcmp(whenstr, "never") == 0) {
341 when = TIME_MAX;
342 } else if (whenstr[0] == '+') {
343 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
344 } else {
345 when = strtoull(whenstr, NULL, 10);
348 if (strcmp(protocol, "spice") == 0) {
349 if (!qemu_using_spice(errp)) {
350 return;
352 rc = qemu_spice_set_pw_expire(when);
353 if (rc != 0) {
354 error_setg(errp, QERR_SET_PASSWD_FAILED);
356 return;
359 if (strcmp(protocol, "vnc") == 0) {
360 rc = vnc_display_pw_expire(NULL, when);
361 if (rc != 0) {
362 error_setg(errp, QERR_SET_PASSWD_FAILED);
364 return;
367 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
370 #ifdef CONFIG_VNC
371 void qmp_change_vnc_password(const char *password, Error **errp)
373 if (vnc_display_password(NULL, password) < 0) {
374 error_setg(errp, QERR_SET_PASSWD_FAILED);
378 static void qmp_change_vnc_listen(const char *target, Error **errp)
380 QemuOptsList *olist = qemu_find_opts("vnc");
381 QemuOpts *opts;
383 if (strstr(target, "id=")) {
384 error_setg(errp, "id not supported");
385 return;
388 opts = qemu_opts_find(olist, "default");
389 if (opts) {
390 qemu_opts_del(opts);
392 opts = vnc_parse(target, errp);
393 if (!opts) {
394 return;
397 vnc_display_open("default", errp);
400 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
401 Error **errp)
403 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
404 if (!has_arg) {
405 error_setg(errp, QERR_MISSING_PARAMETER, "password");
406 } else {
407 qmp_change_vnc_password(arg, errp);
409 } else {
410 qmp_change_vnc_listen(target, errp);
413 #else
414 void qmp_change_vnc_password(const char *password, Error **errp)
416 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
418 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
419 Error **errp)
421 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
423 #endif /* !CONFIG_VNC */
425 void qmp_change(const char *device, const char *target,
426 bool has_arg, const char *arg, Error **errp)
428 if (strcmp(device, "vnc") == 0) {
429 qmp_change_vnc(target, has_arg, arg, errp);
430 } else {
431 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
432 errp);
436 static void qom_list_types_tramp(ObjectClass *klass, void *data)
438 ObjectTypeInfoList *e, **pret = data;
439 ObjectTypeInfo *info;
441 info = g_malloc0(sizeof(*info));
442 info->name = g_strdup(object_class_get_name(klass));
444 e = g_malloc0(sizeof(*e));
445 e->value = info;
446 e->next = *pret;
447 *pret = e;
450 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
451 const char *implements,
452 bool has_abstract,
453 bool abstract,
454 Error **errp)
456 ObjectTypeInfoList *ret = NULL;
458 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
460 return ret;
463 /* Return a DevicePropertyInfo for a qdev property.
465 * If a qdev property with the given name does not exist, use the given default
466 * type. If the qdev property info should not be shown, return NULL.
468 * The caller must free the return value.
470 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
471 const char *name,
472 const char *default_type,
473 const char *description)
475 DevicePropertyInfo *info;
476 Property *prop;
478 do {
479 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
480 if (strcmp(name, prop->name) != 0) {
481 continue;
485 * TODO Properties without a parser are just for dirty hacks.
486 * qdev_prop_ptr is the only such PropertyInfo. It's marked
487 * for removal. This conditional should be removed along with
488 * it.
490 if (!prop->info->set) {
491 return NULL; /* no way to set it, don't show */
494 info = g_malloc0(sizeof(*info));
495 info->name = g_strdup(prop->name);
496 info->type = g_strdup(prop->info->name);
497 info->has_description = !!prop->info->description;
498 info->description = g_strdup(prop->info->description);
499 return info;
501 klass = object_class_get_parent(klass);
502 } while (klass != object_class_by_name(TYPE_DEVICE));
504 /* Not a qdev property, use the default type */
505 info = g_malloc0(sizeof(*info));
506 info->name = g_strdup(name);
507 info->type = g_strdup(default_type);
508 info->has_description = !!description;
509 info->description = g_strdup(description);
511 return info;
514 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
515 Error **errp)
517 ObjectClass *klass;
518 Object *obj;
519 ObjectProperty *prop;
520 ObjectPropertyIterator iter;
521 DevicePropertyInfoList *prop_list = NULL;
523 klass = object_class_by_name(typename);
524 if (klass == NULL) {
525 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
526 "Device '%s' not found", typename);
527 return NULL;
530 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
531 if (klass == NULL) {
532 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
533 return NULL;
536 if (object_class_is_abstract(klass)) {
537 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
538 "non-abstract device type");
539 return NULL;
542 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
543 error_setg(errp, "Can't list properties of device '%s'", typename);
544 return NULL;
547 obj = object_new(typename);
549 object_property_iter_init(&iter, obj);
550 while ((prop = object_property_iter_next(&iter))) {
551 DevicePropertyInfo *info;
552 DevicePropertyInfoList *entry;
554 /* Skip Object and DeviceState properties */
555 if (strcmp(prop->name, "type") == 0 ||
556 strcmp(prop->name, "realized") == 0 ||
557 strcmp(prop->name, "hotpluggable") == 0 ||
558 strcmp(prop->name, "hotplugged") == 0 ||
559 strcmp(prop->name, "parent_bus") == 0) {
560 continue;
563 /* Skip legacy properties since they are just string versions of
564 * properties that we already list.
566 if (strstart(prop->name, "legacy-", NULL)) {
567 continue;
570 info = make_device_property_info(klass, prop->name, prop->type,
571 prop->description);
572 if (!info) {
573 continue;
576 entry = g_malloc0(sizeof(*entry));
577 entry->value = info;
578 entry->next = prop_list;
579 prop_list = entry;
582 object_unref(obj);
584 return prop_list;
587 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
589 return arch_query_cpu_definitions(errp);
592 void qmp_add_client(const char *protocol, const char *fdname,
593 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
594 Error **errp)
596 CharDriverState *s;
597 int fd;
599 fd = monitor_get_fd(cur_mon, fdname, errp);
600 if (fd < 0) {
601 return;
604 if (strcmp(protocol, "spice") == 0) {
605 if (!qemu_using_spice(errp)) {
606 close(fd);
607 return;
609 skipauth = has_skipauth ? skipauth : false;
610 tls = has_tls ? tls : false;
611 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
612 error_setg(errp, "spice failed to add client");
613 close(fd);
615 return;
616 #ifdef CONFIG_VNC
617 } else if (strcmp(protocol, "vnc") == 0) {
618 skipauth = has_skipauth ? skipauth : false;
619 vnc_display_add_client(NULL, fd, skipauth);
620 return;
621 #endif
622 } else if ((s = qemu_chr_find(protocol)) != NULL) {
623 if (qemu_chr_add_client(s, fd) < 0) {
624 error_setg(errp, "failed to add client");
625 close(fd);
626 return;
628 return;
631 error_setg(errp, "protocol '%s' is invalid", protocol);
632 close(fd);
635 void object_add(const char *type, const char *id, const QDict *qdict,
636 Visitor *v, Error **errp)
638 Object *obj;
639 ObjectClass *klass;
640 const QDictEntry *e;
641 Error *local_err = NULL;
643 klass = object_class_by_name(type);
644 if (!klass) {
645 error_setg(errp, "invalid object type: %s", type);
646 return;
649 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
650 error_setg(errp, "object type '%s' isn't supported by object-add",
651 type);
652 return;
655 if (object_class_is_abstract(klass)) {
656 error_setg(errp, "object type '%s' is abstract", type);
657 return;
660 obj = object_new(type);
661 if (qdict) {
662 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
663 object_property_set(obj, v, e->key, &local_err);
664 if (local_err) {
665 goto out;
670 object_property_add_child(object_get_objects_root(),
671 id, obj, &local_err);
672 if (local_err) {
673 goto out;
676 user_creatable_complete(obj, &local_err);
677 if (local_err) {
678 object_property_del(object_get_objects_root(),
679 id, &error_abort);
680 goto out;
682 out:
683 if (local_err) {
684 error_propagate(errp, local_err);
686 object_unref(obj);
689 void qmp_object_add(const char *type, const char *id,
690 bool has_props, QObject *props, Error **errp)
692 const QDict *pdict = NULL;
693 QmpInputVisitor *qiv;
695 if (props) {
696 pdict = qobject_to_qdict(props);
697 if (!pdict) {
698 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
699 return;
703 qiv = qmp_input_visitor_new(props);
704 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
705 qmp_input_visitor_cleanup(qiv);
708 void qmp_object_del(const char *id, Error **errp)
710 Object *container;
711 Object *obj;
713 container = object_get_objects_root();
714 obj = object_resolve_path_component(container, id);
715 if (!obj) {
716 error_setg(errp, "object id not found");
717 return;
720 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
721 error_setg(errp, "%s is in use, can not be deleted", id);
722 return;
724 object_unparent(obj);
727 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
729 MemoryDeviceInfoList *head = NULL;
730 MemoryDeviceInfoList **prev = &head;
732 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
734 return head;
737 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
739 bool ambig;
740 ACPIOSTInfoList *head = NULL;
741 ACPIOSTInfoList **prev = &head;
742 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
744 if (obj) {
745 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
746 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
748 adevc->ospm_status(adev, &prev);
749 } else {
750 error_setg(errp, "command is not supported, missing ACPI device");
753 return head;