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