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