]> git.proxmox.com Git - mirror_qemu.git/blob - qmp.c
block: Allow bdrv_unref_child(bs, NULL)
[mirror_qemu.git] / qmp.c
1 /*
2 * QEMU Management Protocol
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
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.
14 */
15
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 "qom/qom-qobject.h"
28 #include "qapi/qmp/qerror.h"
29 #include "qapi/qmp/qobject.h"
30 #include "qapi/qmp-input-visitor.h"
31 #include "hw/boards.h"
32 #include "qom/object_interfaces.h"
33 #include "hw/mem/pc-dimm.h"
34 #include "hw/acpi/acpi_dev_interface.h"
35
36 NameInfo *qmp_query_name(Error **errp)
37 {
38 NameInfo *info = g_malloc0(sizeof(*info));
39
40 if (qemu_name) {
41 info->has_name = true;
42 info->name = g_strdup(qemu_name);
43 }
44
45 return info;
46 }
47
48 VersionInfo *qmp_query_version(Error **errp)
49 {
50 VersionInfo *info = g_new0(VersionInfo, 1);
51 const char *version = QEMU_VERSION;
52 const char *tmp;
53 int err;
54
55 info->qemu = g_new0(VersionTriple, 1);
56 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
57 assert(err == 0);
58 tmp++;
59
60 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
61 assert(err == 0);
62 tmp++;
63
64 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
65 assert(err == 0);
66 info->package = g_strdup(QEMU_PKGVERSION);
67
68 return info;
69 }
70
71 KvmInfo *qmp_query_kvm(Error **errp)
72 {
73 KvmInfo *info = g_malloc0(sizeof(*info));
74
75 info->enabled = kvm_enabled();
76 info->present = kvm_available();
77
78 return info;
79 }
80
81 UuidInfo *qmp_query_uuid(Error **errp)
82 {
83 UuidInfo *info = g_malloc0(sizeof(*info));
84 char uuid[64];
85
86 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
87 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
88 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
89 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
90 qemu_uuid[14], qemu_uuid[15]);
91
92 info->UUID = g_strdup(uuid);
93 return info;
94 }
95
96 void qmp_quit(Error **errp)
97 {
98 no_shutdown = 0;
99 qemu_system_shutdown_request();
100 }
101
102 void qmp_stop(Error **errp)
103 {
104 if (runstate_check(RUN_STATE_INMIGRATE)) {
105 autostart = 0;
106 } else {
107 vm_stop(RUN_STATE_PAUSED);
108 }
109 }
110
111 void qmp_system_reset(Error **errp)
112 {
113 qemu_system_reset_request();
114 }
115
116 void qmp_system_powerdown(Error **erp)
117 {
118 qemu_system_powerdown_request();
119 }
120
121 void qmp_cpu(int64_t index, Error **errp)
122 {
123 /* Just do nothing */
124 }
125
126 void qmp_cpu_add(int64_t id, Error **errp)
127 {
128 MachineClass *mc;
129
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");
135 }
136 }
137
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)
142 {
143 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
144 return NULL;
145 };
146
147 VncInfo2List *qmp_query_vnc_servers(Error **errp)
148 {
149 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
150 return NULL;
151 };
152 #endif
153
154 #ifndef CONFIG_SPICE
155 /*
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.
163 */
164 SpiceInfo *qmp_query_spice(Error **errp)
165 {
166 abort();
167 };
168 #endif
169
170 void qmp_cont(Error **errp)
171 {
172 Error *local_err = NULL;
173 BlockDriverState *bs;
174
175 if (runstate_needs_reset()) {
176 error_setg(errp, "Resetting the Virtual Machine is required");
177 return;
178 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
179 return;
180 }
181
182 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
183 bdrv_iostatus_reset(bs);
184 }
185 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
186 bdrv_add_key(bs, NULL, &local_err);
187 if (local_err) {
188 error_propagate(errp, local_err);
189 return;
190 }
191 }
192
193 if (runstate_check(RUN_STATE_INMIGRATE)) {
194 autostart = 1;
195 } else {
196 vm_start();
197 }
198 }
199
200 void qmp_system_wakeup(Error **errp)
201 {
202 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
203 }
204
205 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
206 {
207 Object *obj;
208 bool ambiguous = false;
209 ObjectPropertyInfoList *props = NULL;
210 ObjectProperty *prop;
211
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);
219 }
220 return NULL;
221 }
222
223 QTAILQ_FOREACH(prop, &obj->properties, node) {
224 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
225
226 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
227 entry->next = props;
228 props = entry;
229
230 entry->value->name = g_strdup(prop->name);
231 entry->value->type = g_strdup(prop->type);
232 }
233
234 return props;
235 }
236
237 void qmp_qom_set(const char *path, const char *property, QObject *value,
238 Error **errp)
239 {
240 Object *obj;
241
242 obj = object_resolve_path(path, NULL);
243 if (!obj) {
244 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
245 "Device '%s' not found", path);
246 return;
247 }
248
249 object_property_set_qobject(obj, value, property, errp);
250 }
251
252 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
253 {
254 Object *obj;
255
256 obj = object_resolve_path(path, NULL);
257 if (!obj) {
258 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
259 "Device '%s' not found", path);
260 return NULL;
261 }
262
263 return object_property_get_qobject(obj, property, errp);
264 }
265
266 void qmp_set_password(const char *protocol, const char *password,
267 bool has_connected, const char *connected, Error **errp)
268 {
269 int disconnect_if_connected = 0;
270 int fail_if_connected = 0;
271 int rc;
272
273 if (has_connected) {
274 if (strcmp(connected, "fail") == 0) {
275 fail_if_connected = 1;
276 } else if (strcmp(connected, "disconnect") == 0) {
277 disconnect_if_connected = 1;
278 } else if (strcmp(connected, "keep") == 0) {
279 /* nothing */
280 } else {
281 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
282 return;
283 }
284 }
285
286 if (strcmp(protocol, "spice") == 0) {
287 if (!qemu_using_spice(errp)) {
288 return;
289 }
290 rc = qemu_spice_set_passwd(password, fail_if_connected,
291 disconnect_if_connected);
292 if (rc != 0) {
293 error_setg(errp, QERR_SET_PASSWD_FAILED);
294 }
295 return;
296 }
297
298 if (strcmp(protocol, "vnc") == 0) {
299 if (fail_if_connected || disconnect_if_connected) {
300 /* vnc supports "connected=keep" only */
301 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
302 return;
303 }
304 /* Note that setting an empty password will not disable login through
305 * this interface. */
306 rc = vnc_display_password(NULL, password);
307 if (rc < 0) {
308 error_setg(errp, QERR_SET_PASSWD_FAILED);
309 }
310 return;
311 }
312
313 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
314 }
315
316 void qmp_expire_password(const char *protocol, const char *whenstr,
317 Error **errp)
318 {
319 time_t when;
320 int rc;
321
322 if (strcmp(whenstr, "now") == 0) {
323 when = 0;
324 } else if (strcmp(whenstr, "never") == 0) {
325 when = TIME_MAX;
326 } else if (whenstr[0] == '+') {
327 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
328 } else {
329 when = strtoull(whenstr, NULL, 10);
330 }
331
332 if (strcmp(protocol, "spice") == 0) {
333 if (!qemu_using_spice(errp)) {
334 return;
335 }
336 rc = qemu_spice_set_pw_expire(when);
337 if (rc != 0) {
338 error_setg(errp, QERR_SET_PASSWD_FAILED);
339 }
340 return;
341 }
342
343 if (strcmp(protocol, "vnc") == 0) {
344 rc = vnc_display_pw_expire(NULL, when);
345 if (rc != 0) {
346 error_setg(errp, QERR_SET_PASSWD_FAILED);
347 }
348 return;
349 }
350
351 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
352 }
353
354 #ifdef CONFIG_VNC
355 void qmp_change_vnc_password(const char *password, Error **errp)
356 {
357 if (vnc_display_password(NULL, password) < 0) {
358 error_setg(errp, QERR_SET_PASSWD_FAILED);
359 }
360 }
361
362 static void qmp_change_vnc_listen(const char *target, Error **errp)
363 {
364 QemuOptsList *olist = qemu_find_opts("vnc");
365 QemuOpts *opts;
366
367 if (strstr(target, "id=")) {
368 error_setg(errp, "id not supported");
369 return;
370 }
371
372 opts = qemu_opts_find(olist, "default");
373 if (opts) {
374 qemu_opts_del(opts);
375 }
376 opts = vnc_parse(target, errp);
377 if (!opts) {
378 return;
379 }
380
381 vnc_display_open("default", errp);
382 }
383
384 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
385 Error **errp)
386 {
387 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
388 if (!has_arg) {
389 error_setg(errp, QERR_MISSING_PARAMETER, "password");
390 } else {
391 qmp_change_vnc_password(arg, errp);
392 }
393 } else {
394 qmp_change_vnc_listen(target, errp);
395 }
396 }
397 #else
398 void qmp_change_vnc_password(const char *password, Error **errp)
399 {
400 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
401 }
402 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
403 Error **errp)
404 {
405 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
406 }
407 #endif /* !CONFIG_VNC */
408
409 void qmp_change(const char *device, const char *target,
410 bool has_arg, const char *arg, Error **errp)
411 {
412 if (strcmp(device, "vnc") == 0) {
413 qmp_change_vnc(target, has_arg, arg, errp);
414 } else {
415 qmp_change_blockdev(device, target, arg, errp);
416 }
417 }
418
419 static void qom_list_types_tramp(ObjectClass *klass, void *data)
420 {
421 ObjectTypeInfoList *e, **pret = data;
422 ObjectTypeInfo *info;
423
424 info = g_malloc0(sizeof(*info));
425 info->name = g_strdup(object_class_get_name(klass));
426
427 e = g_malloc0(sizeof(*e));
428 e->value = info;
429 e->next = *pret;
430 *pret = e;
431 }
432
433 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
434 const char *implements,
435 bool has_abstract,
436 bool abstract,
437 Error **errp)
438 {
439 ObjectTypeInfoList *ret = NULL;
440
441 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
442
443 return ret;
444 }
445
446 /* Return a DevicePropertyInfo for a qdev property.
447 *
448 * If a qdev property with the given name does not exist, use the given default
449 * type. If the qdev property info should not be shown, return NULL.
450 *
451 * The caller must free the return value.
452 */
453 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
454 const char *name,
455 const char *default_type,
456 const char *description)
457 {
458 DevicePropertyInfo *info;
459 Property *prop;
460
461 do {
462 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
463 if (strcmp(name, prop->name) != 0) {
464 continue;
465 }
466
467 /*
468 * TODO Properties without a parser are just for dirty hacks.
469 * qdev_prop_ptr is the only such PropertyInfo. It's marked
470 * for removal. This conditional should be removed along with
471 * it.
472 */
473 if (!prop->info->set) {
474 return NULL; /* no way to set it, don't show */
475 }
476
477 info = g_malloc0(sizeof(*info));
478 info->name = g_strdup(prop->name);
479 info->type = g_strdup(prop->info->name);
480 info->has_description = !!prop->info->description;
481 info->description = g_strdup(prop->info->description);
482 return info;
483 }
484 klass = object_class_get_parent(klass);
485 } while (klass != object_class_by_name(TYPE_DEVICE));
486
487 /* Not a qdev property, use the default type */
488 info = g_malloc0(sizeof(*info));
489 info->name = g_strdup(name);
490 info->type = g_strdup(default_type);
491 info->has_description = !!description;
492 info->description = g_strdup(description);
493
494 return info;
495 }
496
497 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
498 Error **errp)
499 {
500 ObjectClass *klass;
501 Object *obj;
502 ObjectProperty *prop;
503 DevicePropertyInfoList *prop_list = NULL;
504
505 klass = object_class_by_name(typename);
506 if (klass == NULL) {
507 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
508 "Device '%s' not found", typename);
509 return NULL;
510 }
511
512 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
513 if (klass == NULL) {
514 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
515 return NULL;
516 }
517
518 if (object_class_is_abstract(klass)) {
519 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
520 "non-abstract device type");
521 return NULL;
522 }
523
524 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
525 error_setg(errp, "Can't list properties of device '%s'", typename);
526 return NULL;
527 }
528
529 obj = object_new(typename);
530
531 QTAILQ_FOREACH(prop, &obj->properties, node) {
532 DevicePropertyInfo *info;
533 DevicePropertyInfoList *entry;
534
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;
542 }
543
544 /* Skip legacy properties since they are just string versions of
545 * properties that we already list.
546 */
547 if (strstart(prop->name, "legacy-", NULL)) {
548 continue;
549 }
550
551 info = make_device_property_info(klass, prop->name, prop->type,
552 prop->description);
553 if (!info) {
554 continue;
555 }
556
557 entry = g_malloc0(sizeof(*entry));
558 entry->value = info;
559 entry->next = prop_list;
560 prop_list = entry;
561 }
562
563 object_unref(obj);
564
565 return prop_list;
566 }
567
568 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
569 {
570 return arch_query_cpu_definitions(errp);
571 }
572
573 void qmp_add_client(const char *protocol, const char *fdname,
574 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
575 Error **errp)
576 {
577 CharDriverState *s;
578 int fd;
579
580 fd = monitor_get_fd(cur_mon, fdname, errp);
581 if (fd < 0) {
582 return;
583 }
584
585 if (strcmp(protocol, "spice") == 0) {
586 if (!qemu_using_spice(errp)) {
587 close(fd);
588 return;
589 }
590 skipauth = has_skipauth ? skipauth : false;
591 tls = has_tls ? tls : false;
592 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
593 error_setg(errp, "spice failed to add client");
594 close(fd);
595 }
596 return;
597 #ifdef CONFIG_VNC
598 } else if (strcmp(protocol, "vnc") == 0) {
599 skipauth = has_skipauth ? skipauth : false;
600 vnc_display_add_client(NULL, fd, skipauth);
601 return;
602 #endif
603 } else if ((s = qemu_chr_find(protocol)) != NULL) {
604 if (qemu_chr_add_client(s, fd) < 0) {
605 error_setg(errp, "failed to add client");
606 close(fd);
607 return;
608 }
609 return;
610 }
611
612 error_setg(errp, "protocol '%s' is invalid", protocol);
613 close(fd);
614 }
615
616 void object_add(const char *type, const char *id, const QDict *qdict,
617 Visitor *v, Error **errp)
618 {
619 Object *obj;
620 ObjectClass *klass;
621 const QDictEntry *e;
622 Error *local_err = NULL;
623
624 klass = object_class_by_name(type);
625 if (!klass) {
626 error_setg(errp, "invalid object type: %s", type);
627 return;
628 }
629
630 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
631 error_setg(errp, "object type '%s' isn't supported by object-add",
632 type);
633 return;
634 }
635
636 if (object_class_is_abstract(klass)) {
637 error_setg(errp, "object type '%s' is abstract", type);
638 return;
639 }
640
641 obj = object_new(type);
642 if (qdict) {
643 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
644 object_property_set(obj, v, e->key, &local_err);
645 if (local_err) {
646 goto out;
647 }
648 }
649 }
650
651 object_property_add_child(object_get_objects_root(),
652 id, obj, &local_err);
653 if (local_err) {
654 goto out;
655 }
656
657 user_creatable_complete(obj, &local_err);
658 if (local_err) {
659 object_property_del(object_get_objects_root(),
660 id, &error_abort);
661 goto out;
662 }
663 out:
664 if (local_err) {
665 error_propagate(errp, local_err);
666 }
667 object_unref(obj);
668 }
669
670 void qmp_object_add(const char *type, const char *id,
671 bool has_props, QObject *props, Error **errp)
672 {
673 const QDict *pdict = NULL;
674 QmpInputVisitor *qiv;
675
676 if (props) {
677 pdict = qobject_to_qdict(props);
678 if (!pdict) {
679 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
680 return;
681 }
682 }
683
684 qiv = qmp_input_visitor_new(props);
685 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
686 qmp_input_visitor_cleanup(qiv);
687 }
688
689 void qmp_object_del(const char *id, Error **errp)
690 {
691 Object *container;
692 Object *obj;
693
694 container = object_get_objects_root();
695 obj = object_resolve_path_component(container, id);
696 if (!obj) {
697 error_setg(errp, "object id not found");
698 return;
699 }
700
701 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
702 error_setg(errp, "%s is in use, can not be deleted", id);
703 return;
704 }
705 object_unparent(obj);
706 }
707
708 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
709 {
710 MemoryDeviceInfoList *head = NULL;
711 MemoryDeviceInfoList **prev = &head;
712
713 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
714
715 return head;
716 }
717
718 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
719 {
720 bool ambig;
721 ACPIOSTInfoList *head = NULL;
722 ACPIOSTInfoList **prev = &head;
723 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
724
725 if (obj) {
726 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
727 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
728
729 adevc->ospm_status(adev, &prev);
730 } else {
731 error_setg(errp, "command is not supported, missing ACPI device");
732 }
733
734 return head;
735 }