qemu-iotests/117: Avoid blockdev-add with id
[qemu.git] / qmp.c
blob8b4bc980f1657e1c0a8643a90ba1d3d729c7d062
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 "qmp-commands.h"
22 #include "sysemu/char.h"
23 #include "ui/qemu-spice.h"
24 #include "ui/vnc.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/arch_init.h"
27 #include "hw/qdev.h"
28 #include "sysemu/blockdev.h"
29 #include "sysemu/block-backend.h"
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qapi/qmp/qobject.h"
33 #include "qapi/qmp-input-visitor.h"
34 #include "hw/boards.h"
35 #include "qom/object_interfaces.h"
36 #include "hw/mem/pc-dimm.h"
37 #include "hw/acpi/acpi_dev_interface.h"
39 NameInfo *qmp_query_name(Error **errp)
41 NameInfo *info = g_malloc0(sizeof(*info));
43 if (qemu_name) {
44 info->has_name = true;
45 info->name = g_strdup(qemu_name);
48 return info;
51 VersionInfo *qmp_query_version(Error **errp)
53 VersionInfo *info = g_new0(VersionInfo, 1);
55 info->qemu = g_new0(VersionTriple, 1);
56 info->qemu->major = QEMU_VERSION_MAJOR;
57 info->qemu->minor = QEMU_VERSION_MINOR;
58 info->qemu->micro = QEMU_VERSION_MICRO;
59 info->package = g_strdup(QEMU_PKGVERSION);
61 return info;
64 KvmInfo *qmp_query_kvm(Error **errp)
66 KvmInfo *info = g_malloc0(sizeof(*info));
68 info->enabled = kvm_enabled();
69 info->present = kvm_available();
71 return info;
74 UuidInfo *qmp_query_uuid(Error **errp)
76 UuidInfo *info = g_malloc0(sizeof(*info));
77 char uuid[64];
79 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
80 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
81 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
82 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
83 qemu_uuid[14], qemu_uuid[15]);
85 info->UUID = g_strdup(uuid);
86 return info;
89 void qmp_quit(Error **errp)
91 no_shutdown = 0;
92 qemu_system_shutdown_request();
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();
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_query_spice() that calls qmp_query_spice(). Provide it
161 * one, or else linking fails. FIXME Educate the QAPI schema on
162 * 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 BlockBackend *blk;
174 BlockDriverState *bs;
175 BdrvNextIterator it;
177 /* if there is a dump in background, we should wait until the dump
178 * finished */
179 if (dump_in_progress()) {
180 error_setg(errp, "There is a dump in process, please wait.");
181 return;
184 if (runstate_needs_reset()) {
185 error_setg(errp, "Resetting the Virtual Machine is required");
186 return;
187 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
188 return;
191 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
192 blk_iostatus_reset(blk);
195 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
196 bdrv_add_key(bs, NULL, &local_err);
197 if (local_err) {
198 error_propagate(errp, local_err);
199 return;
203 /* Continuing after completed migration. Images have been inactivated to
204 * allow the destination to take control. Need to get control back now. */
205 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
206 runstate_check(RUN_STATE_POSTMIGRATE))
208 bdrv_invalidate_cache_all(&local_err);
209 if (local_err) {
210 error_propagate(errp, local_err);
211 return;
215 if (runstate_check(RUN_STATE_INMIGRATE)) {
216 autostart = 1;
217 } else {
218 vm_start();
222 void qmp_system_wakeup(Error **errp)
224 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
227 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
229 Object *obj;
230 bool ambiguous = false;
231 ObjectPropertyInfoList *props = NULL;
232 ObjectProperty *prop;
233 ObjectPropertyIterator iter;
235 obj = object_resolve_path(path, &ambiguous);
236 if (obj == NULL) {
237 if (ambiguous) {
238 error_setg(errp, "Path '%s' is ambiguous", path);
239 } else {
240 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
241 "Device '%s' not found", path);
243 return NULL;
246 object_property_iter_init(&iter, obj);
247 while ((prop = object_property_iter_next(&iter))) {
248 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
250 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
251 entry->next = props;
252 props = entry;
254 entry->value->name = g_strdup(prop->name);
255 entry->value->type = g_strdup(prop->type);
258 return props;
261 void qmp_qom_set(const char *path, const char *property, QObject *value,
262 Error **errp)
264 Object *obj;
266 obj = object_resolve_path(path, NULL);
267 if (!obj) {
268 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
269 "Device '%s' not found", path);
270 return;
273 object_property_set_qobject(obj, value, property, errp);
276 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
278 Object *obj;
280 obj = object_resolve_path(path, NULL);
281 if (!obj) {
282 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
283 "Device '%s' not found", path);
284 return NULL;
287 return object_property_get_qobject(obj, property, errp);
290 void qmp_set_password(const char *protocol, const char *password,
291 bool has_connected, const char *connected, Error **errp)
293 int disconnect_if_connected = 0;
294 int fail_if_connected = 0;
295 int rc;
297 if (has_connected) {
298 if (strcmp(connected, "fail") == 0) {
299 fail_if_connected = 1;
300 } else if (strcmp(connected, "disconnect") == 0) {
301 disconnect_if_connected = 1;
302 } else if (strcmp(connected, "keep") == 0) {
303 /* nothing */
304 } else {
305 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
306 return;
310 if (strcmp(protocol, "spice") == 0) {
311 if (!qemu_using_spice(errp)) {
312 return;
314 rc = qemu_spice_set_passwd(password, fail_if_connected,
315 disconnect_if_connected);
316 if (rc != 0) {
317 error_setg(errp, QERR_SET_PASSWD_FAILED);
319 return;
322 if (strcmp(protocol, "vnc") == 0) {
323 if (fail_if_connected || disconnect_if_connected) {
324 /* vnc supports "connected=keep" only */
325 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
326 return;
328 /* Note that setting an empty password will not disable login through
329 * this interface. */
330 rc = vnc_display_password(NULL, password);
331 if (rc < 0) {
332 error_setg(errp, QERR_SET_PASSWD_FAILED);
334 return;
337 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
340 void qmp_expire_password(const char *protocol, const char *whenstr,
341 Error **errp)
343 time_t when;
344 int rc;
346 if (strcmp(whenstr, "now") == 0) {
347 when = 0;
348 } else if (strcmp(whenstr, "never") == 0) {
349 when = TIME_MAX;
350 } else if (whenstr[0] == '+') {
351 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
352 } else {
353 when = strtoull(whenstr, NULL, 10);
356 if (strcmp(protocol, "spice") == 0) {
357 if (!qemu_using_spice(errp)) {
358 return;
360 rc = qemu_spice_set_pw_expire(when);
361 if (rc != 0) {
362 error_setg(errp, QERR_SET_PASSWD_FAILED);
364 return;
367 if (strcmp(protocol, "vnc") == 0) {
368 rc = vnc_display_pw_expire(NULL, when);
369 if (rc != 0) {
370 error_setg(errp, QERR_SET_PASSWD_FAILED);
372 return;
375 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
378 #ifdef CONFIG_VNC
379 void qmp_change_vnc_password(const char *password, Error **errp)
381 if (vnc_display_password(NULL, password) < 0) {
382 error_setg(errp, QERR_SET_PASSWD_FAILED);
386 static void qmp_change_vnc_listen(const char *target, Error **errp)
388 QemuOptsList *olist = qemu_find_opts("vnc");
389 QemuOpts *opts;
391 if (strstr(target, "id=")) {
392 error_setg(errp, "id not supported");
393 return;
396 opts = qemu_opts_find(olist, "default");
397 if (opts) {
398 qemu_opts_del(opts);
400 opts = vnc_parse(target, errp);
401 if (!opts) {
402 return;
405 vnc_display_open("default", errp);
408 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
409 Error **errp)
411 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
412 if (!has_arg) {
413 error_setg(errp, QERR_MISSING_PARAMETER, "password");
414 } else {
415 qmp_change_vnc_password(arg, errp);
417 } else {
418 qmp_change_vnc_listen(target, errp);
421 #else
422 void qmp_change_vnc_password(const char *password, Error **errp)
424 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
426 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
427 Error **errp)
429 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
431 #endif /* !CONFIG_VNC */
433 void qmp_change(const char *device, const char *target,
434 bool has_arg, const char *arg, Error **errp)
436 if (strcmp(device, "vnc") == 0) {
437 qmp_change_vnc(target, has_arg, arg, errp);
438 } else {
439 qmp_blockdev_change_medium(true, device, false, NULL, target,
440 has_arg, arg, false, 0, errp);
444 static void qom_list_types_tramp(ObjectClass *klass, void *data)
446 ObjectTypeInfoList *e, **pret = data;
447 ObjectTypeInfo *info;
449 info = g_malloc0(sizeof(*info));
450 info->name = g_strdup(object_class_get_name(klass));
452 e = g_malloc0(sizeof(*e));
453 e->value = info;
454 e->next = *pret;
455 *pret = e;
458 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
459 const char *implements,
460 bool has_abstract,
461 bool abstract,
462 Error **errp)
464 ObjectTypeInfoList *ret = NULL;
466 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
468 return ret;
471 /* Return a DevicePropertyInfo for a qdev property.
473 * If a qdev property with the given name does not exist, use the given default
474 * type. If the qdev property info should not be shown, return NULL.
476 * The caller must free the return value.
478 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
479 const char *name,
480 const char *default_type,
481 const char *description)
483 DevicePropertyInfo *info;
484 Property *prop;
486 do {
487 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
488 if (strcmp(name, prop->name) != 0) {
489 continue;
493 * TODO Properties without a parser are just for dirty hacks.
494 * qdev_prop_ptr is the only such PropertyInfo. It's marked
495 * for removal. This conditional should be removed along with
496 * it.
498 if (!prop->info->set) {
499 return NULL; /* no way to set it, don't show */
502 info = g_malloc0(sizeof(*info));
503 info->name = g_strdup(prop->name);
504 info->type = g_strdup(prop->info->name);
505 info->has_description = !!prop->info->description;
506 info->description = g_strdup(prop->info->description);
507 return info;
509 klass = object_class_get_parent(klass);
510 } while (klass != object_class_by_name(TYPE_DEVICE));
512 /* Not a qdev property, use the default type */
513 info = g_malloc0(sizeof(*info));
514 info->name = g_strdup(name);
515 info->type = g_strdup(default_type);
516 info->has_description = !!description;
517 info->description = g_strdup(description);
519 return info;
522 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
523 Error **errp)
525 ObjectClass *klass;
526 Object *obj;
527 ObjectProperty *prop;
528 ObjectPropertyIterator iter;
529 DevicePropertyInfoList *prop_list = NULL;
531 klass = object_class_by_name(typename);
532 if (klass == NULL) {
533 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
534 "Device '%s' not found", typename);
535 return NULL;
538 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
539 if (klass == NULL) {
540 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
541 return NULL;
544 if (object_class_is_abstract(klass)) {
545 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
546 "non-abstract device type");
547 return NULL;
550 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
551 error_setg(errp, "Can't list properties of device '%s'", typename);
552 return NULL;
555 obj = object_new(typename);
557 object_property_iter_init(&iter, obj);
558 while ((prop = object_property_iter_next(&iter))) {
559 DevicePropertyInfo *info;
560 DevicePropertyInfoList *entry;
562 /* Skip Object and DeviceState properties */
563 if (strcmp(prop->name, "type") == 0 ||
564 strcmp(prop->name, "realized") == 0 ||
565 strcmp(prop->name, "hotpluggable") == 0 ||
566 strcmp(prop->name, "hotplugged") == 0 ||
567 strcmp(prop->name, "parent_bus") == 0) {
568 continue;
571 /* Skip legacy properties since they are just string versions of
572 * properties that we already list.
574 if (strstart(prop->name, "legacy-", NULL)) {
575 continue;
578 info = make_device_property_info(klass, prop->name, prop->type,
579 prop->description);
580 if (!info) {
581 continue;
584 entry = g_malloc0(sizeof(*entry));
585 entry->value = info;
586 entry->next = prop_list;
587 prop_list = entry;
590 object_unref(obj);
592 return prop_list;
595 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
597 return arch_query_cpu_definitions(errp);
600 CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
601 CpuModelInfo *model,
602 Error **errp)
604 return arch_query_cpu_model_expansion(type, model, errp);
607 CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
608 CpuModelInfo *modelb,
609 Error **errp)
611 return arch_query_cpu_model_comparison(modela, modelb, errp);
614 CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
615 CpuModelInfo *modelb,
616 Error **errp)
618 return arch_query_cpu_model_baseline(modela, modelb, errp);
621 void qmp_add_client(const char *protocol, const char *fdname,
622 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
623 Error **errp)
625 CharDriverState *s;
626 int fd;
628 fd = monitor_get_fd(cur_mon, fdname, errp);
629 if (fd < 0) {
630 return;
633 if (strcmp(protocol, "spice") == 0) {
634 if (!qemu_using_spice(errp)) {
635 close(fd);
636 return;
638 skipauth = has_skipauth ? skipauth : false;
639 tls = has_tls ? tls : false;
640 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
641 error_setg(errp, "spice failed to add client");
642 close(fd);
644 return;
645 #ifdef CONFIG_VNC
646 } else if (strcmp(protocol, "vnc") == 0) {
647 skipauth = has_skipauth ? skipauth : false;
648 vnc_display_add_client(NULL, fd, skipauth);
649 return;
650 #endif
651 } else if ((s = qemu_chr_find(protocol)) != NULL) {
652 if (qemu_chr_add_client(s, fd) < 0) {
653 error_setg(errp, "failed to add client");
654 close(fd);
655 return;
657 return;
660 error_setg(errp, "protocol '%s' is invalid", protocol);
661 close(fd);
665 void qmp_object_add(const char *type, const char *id,
666 bool has_props, QObject *props, Error **errp)
668 const QDict *pdict = NULL;
669 Visitor *v;
670 Object *obj;
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 v = qmp_input_visitor_new(props, true);
681 obj = user_creatable_add_type(type, id, pdict, v, errp);
682 visit_free(v);
683 if (obj) {
684 object_unref(obj);
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 MemoryDeviceInfoList *head = NULL;
696 MemoryDeviceInfoList **prev = &head;
698 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
700 return head;
703 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
705 bool ambig;
706 ACPIOSTInfoList *head = NULL;
707 ACPIOSTInfoList **prev = &head;
708 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
710 if (obj) {
711 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
712 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
714 adevc->ospm_status(adev, &prev);
715 } else {
716 error_setg(errp, "command is not supported, missing ACPI device");
719 return head;