xref: /openbmc/qemu/qom/qom-qmp-cmds.c (revision 0ce46ab5)
1 /*
2  * QMP commands related to QOM
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/osdep.h"
17 #include "hw/qdev-core.h"
18 #include "qapi/error.h"
19 #include "qapi/qapi-commands-qdev.h"
20 #include "qapi/qapi-commands-qom.h"
21 #include "qapi/qmp/qdict.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qemu/cutils.h"
25 #include "qom/object_interfaces.h"
26 #include "qom/qom-qobject.h"
27 
28 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
29 {
30     Object *obj;
31     bool ambiguous = false;
32     ObjectPropertyInfoList *props = NULL;
33     ObjectProperty *prop;
34     ObjectPropertyIterator iter;
35 
36     obj = object_resolve_path(path, &ambiguous);
37     if (obj == NULL) {
38         if (ambiguous) {
39             error_setg(errp, "Path '%s' is ambiguous", path);
40         } else {
41             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
42                       "Device '%s' not found", path);
43         }
44         return NULL;
45     }
46 
47     object_property_iter_init(&iter, obj);
48     while ((prop = object_property_iter_next(&iter))) {
49         ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
50 
51         entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
52         entry->next = props;
53         props = entry;
54 
55         entry->value->name = g_strdup(prop->name);
56         entry->value->type = g_strdup(prop->type);
57     }
58 
59     return props;
60 }
61 
62 void qmp_qom_set(const char *path, const char *property, QObject *value,
63                  Error **errp)
64 {
65     Object *obj;
66 
67     obj = object_resolve_path(path, NULL);
68     if (!obj) {
69         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
70                   "Device '%s' not found", path);
71         return;
72     }
73 
74     object_property_set_qobject(obj, value, property, errp);
75 }
76 
77 QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
78 {
79     Object *obj;
80 
81     obj = object_resolve_path(path, NULL);
82     if (!obj) {
83         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
84                   "Device '%s' not found", path);
85         return NULL;
86     }
87 
88     return object_property_get_qobject(obj, property, errp);
89 }
90 
91 static void qom_list_types_tramp(ObjectClass *klass, void *data)
92 {
93     ObjectTypeInfoList *e, **pret = data;
94     ObjectTypeInfo *info;
95     ObjectClass *parent = object_class_get_parent(klass);
96 
97     info = g_malloc0(sizeof(*info));
98     info->name = g_strdup(object_class_get_name(klass));
99     info->has_abstract = info->abstract = object_class_is_abstract(klass);
100     if (parent) {
101         info->has_parent = true;
102         info->parent = g_strdup(object_class_get_name(parent));
103     }
104 
105     e = g_malloc0(sizeof(*e));
106     e->value = info;
107     e->next = *pret;
108     *pret = e;
109 }
110 
111 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
112                                        const char *implements,
113                                        bool has_abstract,
114                                        bool abstract,
115                                        Error **errp)
116 {
117     ObjectTypeInfoList *ret = NULL;
118 
119     object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
120 
121     return ret;
122 }
123 
124 /* Return a DevicePropertyInfo for a qdev property.
125  *
126  * If a qdev property with the given name does not exist, use the given default
127  * type.  If the qdev property info should not be shown, return NULL.
128  *
129  * The caller must free the return value.
130  */
131 static ObjectPropertyInfo *make_device_property_info(ObjectClass *klass,
132                                                   const char *name,
133                                                   const char *default_type,
134                                                   const char *description)
135 {
136     ObjectPropertyInfo *info;
137     Property *prop;
138 
139     do {
140         for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
141             if (strcmp(name, prop->name) != 0) {
142                 continue;
143             }
144 
145             info = g_malloc0(sizeof(*info));
146             info->name = g_strdup(prop->name);
147             info->type = default_type ? g_strdup(default_type)
148                                       : g_strdup(prop->info->name);
149             info->has_description = !!prop->info->description;
150             info->description = g_strdup(prop->info->description);
151             return info;
152         }
153         klass = object_class_get_parent(klass);
154     } while (klass != object_class_by_name(TYPE_DEVICE));
155 
156     /* Not a qdev property, use the default type */
157     info = g_malloc0(sizeof(*info));
158     info->name = g_strdup(name);
159     info->type = g_strdup(default_type);
160     info->has_description = !!description;
161     info->description = g_strdup(description);
162 
163     return info;
164 }
165 
166 ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
167                                                 Error **errp)
168 {
169     ObjectClass *klass;
170     Object *obj;
171     ObjectProperty *prop;
172     ObjectPropertyIterator iter;
173     ObjectPropertyInfoList *prop_list = NULL;
174 
175     klass = object_class_by_name(typename);
176     if (klass == NULL) {
177         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
178                   "Device '%s' not found", typename);
179         return NULL;
180     }
181 
182     klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
183     if (klass == NULL) {
184         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
185         return NULL;
186     }
187 
188     if (object_class_is_abstract(klass)) {
189         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
190                    "non-abstract device type");
191         return NULL;
192     }
193 
194     obj = object_new(typename);
195 
196     object_property_iter_init(&iter, obj);
197     while ((prop = object_property_iter_next(&iter))) {
198         ObjectPropertyInfo *info;
199         ObjectPropertyInfoList *entry;
200 
201         /* Skip Object and DeviceState properties */
202         if (strcmp(prop->name, "type") == 0 ||
203             strcmp(prop->name, "realized") == 0 ||
204             strcmp(prop->name, "hotpluggable") == 0 ||
205             strcmp(prop->name, "hotplugged") == 0 ||
206             strcmp(prop->name, "parent_bus") == 0) {
207             continue;
208         }
209 
210         /* Skip legacy properties since they are just string versions of
211          * properties that we already list.
212          */
213         if (strstart(prop->name, "legacy-", NULL)) {
214             continue;
215         }
216 
217         info = make_device_property_info(klass, prop->name, prop->type,
218                                          prop->description);
219         if (!info) {
220             continue;
221         }
222 
223         entry = g_malloc0(sizeof(*entry));
224         entry->value = info;
225         entry->next = prop_list;
226         prop_list = entry;
227     }
228 
229     object_unref(obj);
230 
231     return prop_list;
232 }
233 
234 ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
235                                              Error **errp)
236 {
237     ObjectClass *klass;
238     Object *obj = NULL;
239     ObjectProperty *prop;
240     ObjectPropertyIterator iter;
241     ObjectPropertyInfoList *prop_list = NULL;
242 
243     klass = object_class_by_name(typename);
244     if (klass == NULL) {
245         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
246                   "Class '%s' not found", typename);
247         return NULL;
248     }
249 
250     klass = object_class_dynamic_cast(klass, TYPE_OBJECT);
251     if (klass == NULL) {
252         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_OBJECT);
253         return NULL;
254     }
255 
256     if (object_class_is_abstract(klass)) {
257         object_class_property_iter_init(&iter, klass);
258     } else {
259         obj = object_new(typename);
260         object_property_iter_init(&iter, obj);
261     }
262     while ((prop = object_property_iter_next(&iter))) {
263         ObjectPropertyInfo *info;
264         ObjectPropertyInfoList *entry;
265 
266         info = g_malloc0(sizeof(*info));
267         info->name = g_strdup(prop->name);
268         info->type = g_strdup(prop->type);
269         info->has_description = !!prop->description;
270         info->description = g_strdup(prop->description);
271 
272         entry = g_malloc0(sizeof(*entry));
273         entry->value = info;
274         entry->next = prop_list;
275         prop_list = entry;
276     }
277 
278     object_unref(obj);
279 
280     return prop_list;
281 }
282 
283 void qmp_object_add(const char *type, const char *id,
284                     bool has_props, QObject *props, Error **errp)
285 {
286     QDict *pdict;
287     Visitor *v;
288     Object *obj;
289 
290     if (props) {
291         pdict = qobject_to(QDict, props);
292         if (!pdict) {
293             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
294             return;
295         }
296         qobject_ref(pdict);
297     } else {
298         pdict = qdict_new();
299     }
300 
301     v = qobject_input_visitor_new(QOBJECT(pdict));
302     obj = user_creatable_add_type(type, id, pdict, v, errp);
303     visit_free(v);
304     if (obj) {
305         object_unref(obj);
306     }
307     qobject_unref(pdict);
308 }
309 
310 void qmp_object_del(const char *id, Error **errp)
311 {
312     user_creatable_del(id, errp);
313 }
314