Drop superfluous includes of qapi/qmp/qjson.h
[qemu/armbru.git] / qmp.c
blob1b750293e4eaed2e6e3800f4b9c0c113daa02938
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/osdep.h"
17 #include "qemu-version.h"
18 #include "qemu/cutils.h"
19 #include "monitor/monitor.h"
20 #include "sysemu/sysemu.h"
21 #include "qemu/config-file.h"
22 #include "qemu/uuid.h"
23 #include "qmp-commands.h"
24 #include "chardev/char.h"
25 #include "ui/qemu-spice.h"
26 #include "ui/vnc.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/arch_init.h"
29 #include "hw/qdev.h"
30 #include "sysemu/blockdev.h"
31 #include "sysemu/block-backend.h"
32 #include "qom/qom-qobject.h"
33 #include "qapi/error.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qerror.h"
36 #include "qapi/qobject-input-visitor.h"
37 #include "hw/boards.h"
38 #include "qom/object_interfaces.h"
39 #include "hw/mem/pc-dimm.h"
40 #include "hw/acpi/acpi_dev_interface.h"
42 NameInfo *qmp_query_name(Error **errp)
44 NameInfo *info = g_malloc0(sizeof(*info));
46 if (qemu_name) {
47 info->has_name = true;
48 info->name = g_strdup(qemu_name);
51 return info;
54 VersionInfo *qmp_query_version(Error **errp)
56 VersionInfo *info = g_new0(VersionInfo, 1);
58 info->qemu = g_new0(VersionTriple, 1);
59 info->qemu->major = QEMU_VERSION_MAJOR;
60 info->qemu->minor = QEMU_VERSION_MINOR;
61 info->qemu->micro = QEMU_VERSION_MICRO;
62 info->package = g_strdup(QEMU_PKGVERSION);
64 return info;
67 KvmInfo *qmp_query_kvm(Error **errp)
69 KvmInfo *info = g_malloc0(sizeof(*info));
71 info->enabled = kvm_enabled();
72 info->present = kvm_available();
74 return info;
77 UuidInfo *qmp_query_uuid(Error **errp)
79 UuidInfo *info = g_malloc0(sizeof(*info));
81 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
82 return info;
85 void qmp_quit(Error **errp)
87 no_shutdown = 0;
88 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP);
91 void qmp_stop(Error **errp)
93 /* if there is a dump in background, we should wait until the dump
94 * finished */
95 if (dump_in_progress()) {
96 error_setg(errp, "There is a dump in process, please wait.");
97 return;
100 if (runstate_check(RUN_STATE_INMIGRATE)) {
101 autostart = 0;
102 } else {
103 vm_stop(RUN_STATE_PAUSED);
107 void qmp_system_reset(Error **errp)
109 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP);
112 void qmp_system_powerdown(Error **erp)
114 qemu_system_powerdown_request();
117 void qmp_cpu_add(int64_t id, Error **errp)
119 MachineClass *mc;
121 mc = MACHINE_GET_CLASS(current_machine);
122 if (mc->hot_add_cpu) {
123 mc->hot_add_cpu(id, errp);
124 } else {
125 error_setg(errp, "Not supported");
129 #ifndef CONFIG_VNC
130 /* If VNC support is enabled, the "true" query-vnc command is
131 defined in the VNC subsystem */
132 VncInfo *qmp_query_vnc(Error **errp)
134 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
135 return NULL;
138 VncInfo2List *qmp_query_vnc_servers(Error **errp)
140 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
141 return NULL;
143 #endif
145 #ifndef CONFIG_SPICE
147 * qmp-commands.hx ensures that QMP command query-spice exists only
148 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
149 * result. However, the QAPI schema is blissfully unaware of that,
150 * and the QAPI code generator happily generates a dead
151 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
152 * one, or else linking fails. FIXME Educate the QAPI schema on
153 * CONFIG_SPICE.
155 SpiceInfo *qmp_query_spice(Error **errp)
157 abort();
159 #endif
161 void qmp_cont(Error **errp)
163 BlockBackend *blk;
164 Error *local_err = NULL;
166 /* if there is a dump in background, we should wait until the dump
167 * finished */
168 if (dump_in_progress()) {
169 error_setg(errp, "There is a dump in process, please wait.");
170 return;
173 if (runstate_needs_reset()) {
174 error_setg(errp, "Resetting the Virtual Machine is required");
175 return;
176 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
177 return;
180 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
181 blk_iostatus_reset(blk);
184 /* Continuing after completed migration. Images have been inactivated to
185 * allow the destination to take control. Need to get control back now.
187 * If there are no inactive block nodes (e.g. because the VM was just
188 * paused rather than completing a migration), bdrv_inactivate_all() simply
189 * doesn't do anything. */
190 bdrv_invalidate_cache_all(&local_err);
191 if (local_err) {
192 error_propagate(errp, local_err);
193 return;
196 if (runstate_check(RUN_STATE_INMIGRATE)) {
197 autostart = 1;
198 } else {
199 vm_start();
203 void qmp_system_wakeup(Error **errp)
205 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
208 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
210 Object *obj;
211 bool ambiguous = false;
212 ObjectPropertyInfoList *props = NULL;
213 ObjectProperty *prop;
214 ObjectPropertyIterator iter;
216 obj = object_resolve_path(path, &ambiguous);
217 if (obj == NULL) {
218 if (ambiguous) {
219 error_setg(errp, "Path '%s' is ambiguous", path);
220 } else {
221 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
222 "Device '%s' not found", path);
224 return NULL;
227 object_property_iter_init(&iter, obj);
228 while ((prop = object_property_iter_next(&iter))) {
229 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
231 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
232 entry->next = props;
233 props = entry;
235 entry->value->name = g_strdup(prop->name);
236 entry->value->type = g_strdup(prop->type);
239 return props;
242 void qmp_qom_set(const char *path, const char *property, QObject *value,
243 Error **errp)
245 Object *obj;
247 obj = object_resolve_path(path, NULL);
248 if (!obj) {
249 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
250 "Device '%s' not found", path);
251 return;
254 object_property_set_qobject(obj, value, property, errp);
257 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
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 NULL;
268 return 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_blockdev_change_medium(true, device, false, NULL, target,
421 has_arg, arg, false, 0, errp);
425 static void qom_list_types_tramp(ObjectClass *klass, void *data)
427 ObjectTypeInfoList *e, **pret = data;
428 ObjectTypeInfo *info;
429 ObjectClass *parent = object_class_get_parent(klass);
431 info = g_malloc0(sizeof(*info));
432 info->name = g_strdup(object_class_get_name(klass));
433 info->has_abstract = info->abstract = object_class_is_abstract(klass);
434 if (parent) {
435 info->has_parent = true;
436 info->parent = g_strdup(object_class_get_name(parent));
439 e = g_malloc0(sizeof(*e));
440 e->value = info;
441 e->next = *pret;
442 *pret = e;
445 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
446 const char *implements,
447 bool has_abstract,
448 bool abstract,
449 Error **errp)
451 ObjectTypeInfoList *ret = NULL;
453 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
455 return ret;
458 /* Return a DevicePropertyInfo for a qdev property.
460 * If a qdev property with the given name does not exist, use the given default
461 * type. If the qdev property info should not be shown, return NULL.
463 * The caller must free the return value.
465 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
466 const char *name,
467 const char *default_type,
468 const char *description)
470 DevicePropertyInfo *info;
471 Property *prop;
473 do {
474 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
475 if (strcmp(name, prop->name) != 0) {
476 continue;
480 * TODO Properties without a parser are just for dirty hacks.
481 * qdev_prop_ptr is the only such PropertyInfo. It's marked
482 * for removal. This conditional should be removed along with
483 * it.
485 if (!prop->info->set && !prop->info->create) {
486 return NULL; /* no way to set it, don't show */
489 info = g_malloc0(sizeof(*info));
490 info->name = g_strdup(prop->name);
491 info->type = default_type ? g_strdup(default_type)
492 : g_strdup(prop->info->name);
493 info->has_description = !!prop->info->description;
494 info->description = g_strdup(prop->info->description);
495 return info;
497 klass = object_class_get_parent(klass);
498 } while (klass != object_class_by_name(TYPE_DEVICE));
500 /* Not a qdev property, use the default type */
501 info = g_malloc0(sizeof(*info));
502 info->name = g_strdup(name);
503 info->type = g_strdup(default_type);
504 info->has_description = !!description;
505 info->description = g_strdup(description);
507 return info;
510 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
511 Error **errp)
513 ObjectClass *klass;
514 Object *obj;
515 ObjectProperty *prop;
516 ObjectPropertyIterator iter;
517 DevicePropertyInfoList *prop_list = NULL;
519 klass = object_class_by_name(typename);
520 if (klass == NULL) {
521 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
522 "Device '%s' not found", typename);
523 return NULL;
526 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
527 if (klass == NULL) {
528 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
529 return NULL;
532 if (object_class_is_abstract(klass)) {
533 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
534 "non-abstract device type");
535 return NULL;
538 obj = object_new(typename);
540 object_property_iter_init(&iter, obj);
541 while ((prop = object_property_iter_next(&iter))) {
542 DevicePropertyInfo *info;
543 DevicePropertyInfoList *entry;
545 /* Skip Object and DeviceState properties */
546 if (strcmp(prop->name, "type") == 0 ||
547 strcmp(prop->name, "realized") == 0 ||
548 strcmp(prop->name, "hotpluggable") == 0 ||
549 strcmp(prop->name, "hotplugged") == 0 ||
550 strcmp(prop->name, "parent_bus") == 0) {
551 continue;
554 /* Skip legacy properties since they are just string versions of
555 * properties that we already list.
557 if (strstart(prop->name, "legacy-", NULL)) {
558 continue;
561 info = make_device_property_info(klass, prop->name, prop->type,
562 prop->description);
563 if (!info) {
564 continue;
567 entry = g_malloc0(sizeof(*entry));
568 entry->value = info;
569 entry->next = prop_list;
570 prop_list = entry;
573 object_unref(obj);
575 return prop_list;
578 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
580 return arch_query_cpu_definitions(errp);
583 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
584 CpuModelInfo *model,
585 Error **errp)
587 return arch_query_cpu_model_expansion(type, model, errp);
590 CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
591 CpuModelInfo *modelb,
592 Error **errp)
594 return arch_query_cpu_model_comparison(modela, modelb, errp);
597 CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
598 CpuModelInfo *modelb,
599 Error **errp)
601 return arch_query_cpu_model_baseline(modela, modelb, errp);
604 void qmp_add_client(const char *protocol, const char *fdname,
605 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
606 Error **errp)
608 Chardev *s;
609 int fd;
611 fd = monitor_get_fd(cur_mon, fdname, errp);
612 if (fd < 0) {
613 return;
616 if (strcmp(protocol, "spice") == 0) {
617 if (!qemu_using_spice(errp)) {
618 close(fd);
619 return;
621 skipauth = has_skipauth ? skipauth : false;
622 tls = has_tls ? tls : false;
623 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
624 error_setg(errp, "spice failed to add client");
625 close(fd);
627 return;
628 #ifdef CONFIG_VNC
629 } else if (strcmp(protocol, "vnc") == 0) {
630 skipauth = has_skipauth ? skipauth : false;
631 vnc_display_add_client(NULL, fd, skipauth);
632 return;
633 #endif
634 } else if ((s = qemu_chr_find(protocol)) != NULL) {
635 if (qemu_chr_add_client(s, fd) < 0) {
636 error_setg(errp, "failed to add client");
637 close(fd);
638 return;
640 return;
643 error_setg(errp, "protocol '%s' is invalid", protocol);
644 close(fd);
648 void qmp_object_add(const char *type, const char *id,
649 bool has_props, QObject *props, Error **errp)
651 QDict *pdict;
652 Visitor *v;
653 Object *obj;
655 if (props) {
656 pdict = qobject_to_qdict(props);
657 if (!pdict) {
658 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
659 return;
661 QINCREF(pdict);
662 } else {
663 pdict = qdict_new();
666 v = qobject_input_visitor_new(QOBJECT(pdict));
667 obj = user_creatable_add_type(type, id, pdict, v, errp);
668 visit_free(v);
669 if (obj) {
670 object_unref(obj);
672 QDECREF(pdict);
675 void qmp_object_del(const char *id, Error **errp)
677 user_creatable_del(id, errp);
680 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
682 MemoryDeviceInfoList *head = NULL;
683 MemoryDeviceInfoList **prev = &head;
685 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
687 return head;
690 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
692 bool ambig;
693 ACPIOSTInfoList *head = NULL;
694 ACPIOSTInfoList **prev = &head;
695 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
697 if (obj) {
698 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
699 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
701 adevc->ospm_status(adev, &prev);
702 } else {
703 error_setg(errp, "command is not supported, missing ACPI device");
706 return head;
709 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
711 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
713 mem_info->base_memory = ram_size;
715 mem_info->plugged_memory = get_plugged_memory_size();
716 mem_info->has_plugged_memory =
717 mem_info->plugged_memory != (uint64_t)-1;
719 return mem_info;