blob: d4a001cf4117a680a0b78825340fac303e065557 [file] [log] [blame]
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001/*
2 * QEMU Object Model
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 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
Peter Maydell9bbc8532016-01-29 17:50:02 +000013#include "qemu/osdep.h"
Markus Armbruster13d4ff02019-08-12 07:23:37 +020014#include "hw/qdev-core.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010015#include "qapi/error.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010016#include "qom/object.h"
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +010017#include "qom/object_interfaces.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020018#include "qemu/cutils.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000019#include "qemu/memalign.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010020#include "qapi/visitor.h"
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +010021#include "qapi/string-input-visitor.h"
22#include "qapi/string-output-visitor.h"
Marc-André Lureau0e76ed02020-01-10 19:30:23 +040023#include "qapi/qobject-input-visitor.h"
Paolo Bonzinicbc94d92021-07-18 08:50:44 +020024#include "qapi/forward-visitor.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010025#include "qapi/qapi-builtin-visit.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010026#include "qapi/qmp/qerror.h"
Marc-André Lureau0e76ed02020-01-10 19:30:23 +040027#include "qapi/qmp/qjson.h"
Paolo Bonzinifa131d92013-05-10 14:16:39 +020028#include "trace.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060029
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010030/* TODO: replace QObject with a simpler visitor to avoid a dependency
31 * of the QOM core on QObject? */
Paolo Bonzini14cccb62012-12-17 18:19:50 +010032#include "qom/qom-qobject.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010033#include "qapi/qmp/qbool.h"
Kevin Wolf125062e2023-11-09 18:42:38 +010034#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010035#include "qapi/qmp/qnum.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010036#include "qapi/qmp/qstring.h"
Philippe Mathieu-Daudée02bdf12019-04-27 15:56:42 +020037#include "qemu/error-report.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010038
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060039#define MAX_INTERFACES 32
40
41typedef struct InterfaceImpl InterfaceImpl;
42typedef struct TypeImpl TypeImpl;
43
44struct InterfaceImpl
45{
Anthony Liguori33e95c62012-08-10 13:16:10 +100046 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060047};
48
49struct TypeImpl
50{
51 const char *name;
52
53 size_t class_size;
54
55 size_t instance_size;
Richard Henderson4c880f32020-09-15 17:46:34 -070056 size_t instance_align;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060057
58 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020059 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060060
61 void *class_data;
62
63 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -030064 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060065 void (*instance_finalize)(Object *obj);
66
67 bool abstract;
68
69 const char *parent;
70 TypeImpl *parent_type;
71
72 ObjectClass *class;
73
74 int num_interfaces;
75 InterfaceImpl interfaces[MAX_INTERFACES];
76};
77
Paolo Bonzini9970bd82012-02-03 11:51:39 +010078static Type type_interface;
79
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060080static GHashTable *type_table_get(void)
81{
82 static GHashTable *type_table;
83
84 if (type_table == NULL) {
85 type_table = g_hash_table_new(g_str_hash, g_str_equal);
86 }
87
88 return type_table;
89}
90
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010091static bool enumerating_types;
92
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060093static void type_table_add(TypeImpl *ti)
94{
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010095 assert(!enumerating_types);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060096 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
97}
98
99static TypeImpl *type_table_lookup(const char *name)
100{
101 return g_hash_table_lookup(type_table_get(), name);
102}
103
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100104static TypeImpl *type_new(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600105{
106 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +1000107 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600108
109 g_assert(info->name != NULL);
110
Anthony Liguori73093352012-01-25 13:37:36 -0600111 if (type_table_lookup(info->name) != NULL) {
112 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
113 abort();
114 }
115
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600116 ti->name = g_strdup(info->name);
117 ti->parent = g_strdup(info->parent);
118
119 ti->class_size = info->class_size;
120 ti->instance_size = info->instance_size;
Richard Henderson4c880f32020-09-15 17:46:34 -0700121 ti->instance_align = info->instance_align;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600122
123 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200124 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600125 ti->class_data = info->class_data;
126
127 ti->instance_init = info->instance_init;
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300128 ti->instance_post_init = info->instance_post_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600129 ti->instance_finalize = info->instance_finalize;
130
131 ti->abstract = info->abstract;
132
Anthony Liguori33e95c62012-08-10 13:16:10 +1000133 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
134 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600135 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000136 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600137
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100138 return ti;
139}
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600140
Thomas Huthb4473782023-11-17 12:44:57 +0100141static bool type_name_is_valid(const char *name)
142{
143 const int slen = strlen(name);
144 int plen;
145
146 g_assert(slen > 1);
147
148 /*
149 * Ideally, the name should start with a letter - however, we've got
150 * too many names starting with a digit already, so allow digits here,
151 * too (except '0' which is not used yet)
152 */
153 if (!g_ascii_isalnum(name[0]) || name[0] == '0') {
154 return false;
155 }
156
157 plen = strspn(name, "abcdefghijklmnopqrstuvwxyz"
158 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
159 "0123456789-_.");
160
161 /* Allow some legacy names with '+' in it for compatibility reasons */
162 if (name[plen] == '+') {
Thomas Huthb4473782023-11-17 12:44:57 +0100163 if (plen >= 17 && g_str_has_prefix(name, "Sun-UltraSparc-I")) {
164 /* Allow "Sun-UltraSparc-IV+" and "Sun-UltraSparc-IIIi+" */
165 return true;
166 }
167 }
168
169 return plen == slen;
170}
171
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100172static TypeImpl *type_register_internal(const TypeInfo *info)
173{
174 TypeImpl *ti;
Thomas Huthb4473782023-11-17 12:44:57 +0100175
176 if (!type_name_is_valid(info->name)) {
177 fprintf(stderr, "Registering '%s' with illegal type name\n", info->name);
178 abort();
179 }
180
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100181 ti = type_new(info);
182
183 type_table_add(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600184 return ti;
185}
186
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200187TypeImpl *type_register(const TypeInfo *info)
188{
189 assert(info->parent);
190 return type_register_internal(info);
191}
192
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600193TypeImpl *type_register_static(const TypeInfo *info)
194{
195 return type_register(info);
196}
197
Igor Mammedovaa04c9d22017-10-09 21:50:49 +0200198void type_register_static_array(const TypeInfo *infos, int nr_infos)
199{
200 int i;
201
202 for (i = 0; i < nr_infos; i++) {
203 type_register_static(&infos[i]);
204 }
205}
206
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600207static TypeImpl *type_get_by_name(const char *name)
208{
209 if (name == NULL) {
210 return NULL;
211 }
212
213 return type_table_lookup(name);
214}
215
216static TypeImpl *type_get_parent(TypeImpl *type)
217{
218 if (!type->parent_type && type->parent) {
219 type->parent_type = type_get_by_name(type->parent);
Philippe Mathieu-Daudé89d337f2020-01-21 12:03:45 +0100220 if (!type->parent_type) {
221 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
222 type->name, type->parent);
223 abort();
224 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600225 }
226
227 return type->parent_type;
228}
229
230static bool type_has_parent(TypeImpl *type)
231{
232 return (type->parent != NULL);
233}
234
235static size_t type_class_get_size(TypeImpl *ti)
236{
237 if (ti->class_size) {
238 return ti->class_size;
239 }
240
241 if (type_has_parent(ti)) {
242 return type_class_get_size(type_get_parent(ti));
243 }
244
245 return sizeof(ObjectClass);
246}
247
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400248static size_t type_object_get_size(TypeImpl *ti)
249{
250 if (ti->instance_size) {
251 return ti->instance_size;
252 }
253
254 if (type_has_parent(ti)) {
255 return type_object_get_size(type_get_parent(ti));
256 }
257
258 return 0;
259}
260
Richard Hendersonad4ec272023-09-13 14:46:17 -0700261static size_t type_object_get_align(TypeImpl *ti)
262{
263 if (ti->instance_align) {
264 return ti->instance_align;
265 }
266
267 if (type_has_parent(ti)) {
268 return type_object_get_align(type_get_parent(ti));
269 }
270
271 return 0;
272}
273
Bharata B Rao3f97b532016-06-10 06:29:00 +0530274size_t object_type_get_instance_size(const char *typename)
275{
276 TypeImpl *type = type_get_by_name(typename);
277
278 g_assert(type != NULL);
279 return type_object_get_size(type);
280}
281
Anthony Liguori33e95c62012-08-10 13:16:10 +1000282static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600283{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000284 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600285
Cao jinb30d8052015-11-03 10:36:42 +0800286 /* Check if target_type is a direct ancestor of type */
Anthony Liguori33e95c62012-08-10 13:16:10 +1000287 while (type) {
288 if (type == target_type) {
289 return true;
290 }
291
292 type = type_get_parent(type);
293 }
294
295 return false;
296}
297
298static void type_initialize(TypeImpl *ti);
299
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100300static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
301 TypeImpl *parent_type)
Anthony Liguori33e95c62012-08-10 13:16:10 +1000302{
303 InterfaceClass *new_iface;
304 TypeInfo info = { };
305 TypeImpl *iface_impl;
306
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100307 info.parent = parent_type->name;
308 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000309 info.abstract = true;
310
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100311 iface_impl = type_new(&info);
312 iface_impl->parent_type = parent_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000313 type_initialize(iface_impl);
314 g_free((char *)info.name);
315
316 new_iface = (InterfaceClass *)iface_impl->class;
317 new_iface->concrete_class = ti->class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100318 new_iface->interface_type = interface_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000319
Masahiro Yamada3d912932020-05-13 03:25:01 +0900320 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600321}
322
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100323static void object_property_free(gpointer data)
324{
325 ObjectProperty *prop = data;
326
Marc-André Lureau0e76ed02020-01-10 19:30:23 +0400327 if (prop->defval) {
328 qobject_unref(prop->defval);
329 prop->defval = NULL;
330 }
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100331 g_free(prop->name);
332 g_free(prop->type);
333 g_free(prop->description);
334 g_free(prop);
335}
336
Igor Mitsyankoac451032012-02-28 15:57:11 +0400337static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600338{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200339 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600340
341 if (ti->class) {
342 return;
343 }
344
345 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400346 ti->instance_size = type_object_get_size(ti);
Richard Hendersonad4ec272023-09-13 14:46:17 -0700347 ti->instance_align = type_object_get_align(ti);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200348 /* Any type with zero instance_size is implicitly abstract.
349 * This means interface types are all abstract.
350 */
351 if (ti->instance_size == 0) {
352 ti->abstract = true;
353 }
Marc-André Lureau422ca142018-09-12 16:53:03 +0400354 if (type_is_ancestor(ti, type_interface)) {
355 assert(ti->instance_size == 0);
356 assert(ti->abstract);
357 assert(!ti->instance_init);
358 assert(!ti->instance_post_init);
359 assert(!ti->instance_finalize);
360 assert(!ti->num_interfaces);
361 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600362 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600363
Paolo Bonzini745549c2012-03-31 16:45:54 +0200364 parent = type_get_parent(ti);
365 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400366 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000367 GSList *e;
368 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600369
Markus Armbruster719a3072018-06-08 19:02:31 +0200370 g_assert(parent->class_size <= ti->class_size);
Marc-André Lureaud5e633f2020-01-10 19:30:14 +0400371 g_assert(parent->instance_size <= ti->instance_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200372 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000373 ti->class->interfaces = NULL;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000374
375 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100376 InterfaceClass *iface = e->data;
377 ObjectClass *klass = OBJECT_CLASS(iface);
378
379 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000380 }
381
382 for (i = 0; i < ti->num_interfaces; i++) {
383 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
Philippe Mathieu-Daudéa9ee3a92020-01-18 17:23:48 +0100384 if (!t) {
385 error_report("missing interface '%s' for object '%s'",
386 ti->interfaces[i].typename, parent->name);
387 abort();
388 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000389 for (e = ti->class->interfaces; e; e = e->next) {
390 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
391
392 if (type_is_ancestor(target_type, t)) {
393 break;
394 }
395 }
396
397 if (e) {
398 continue;
399 }
400
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100401 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000402 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600403 }
404
Masahiro Yamadae5a0cc52020-05-13 02:26:15 +0900405 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
406 object_property_free);
407
Paolo Bonzini745549c2012-03-31 16:45:54 +0200408 ti->class->type = ti;
409
410 while (parent) {
411 if (parent->class_base_init) {
412 parent->class_base_init(ti->class, ti->class_data);
413 }
414 parent = type_get_parent(parent);
415 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600416
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600417 if (ti->class_init) {
418 ti->class_init(ti->class, ti->class_data);
419 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600420}
421
422static void object_init_with_type(Object *obj, TypeImpl *ti)
423{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600424 if (type_has_parent(ti)) {
425 object_init_with_type(obj, type_get_parent(ti));
426 }
427
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600428 if (ti->instance_init) {
429 ti->instance_init(obj);
430 }
431}
432
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300433static void object_post_init_with_type(Object *obj, TypeImpl *ti)
434{
435 if (ti->instance_post_init) {
436 ti->instance_post_init(obj);
437 }
438
439 if (type_has_parent(ti)) {
440 object_post_init_with_type(obj, type_get_parent(ti));
441 }
442}
443
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200444bool object_apply_global_props(Object *obj, const GPtrArray *props,
445 Error **errp)
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400446{
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400447 int i;
448
449 if (!props) {
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200450 return true;
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400451 }
452
453 for (i = 0; i < props->len; i++) {
454 GlobalProperty *p = g_ptr_array_index(props, i);
Eduardo Habkostd769f0d2019-01-10 00:02:57 -0200455 Error *err = NULL;
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400456
457 if (object_dynamic_cast(obj, p->driver) == NULL) {
458 continue;
459 }
Daniel P. Berrangéefba1592020-09-14 14:56:17 +0100460 if (p->optional && !object_property_find(obj, p->property)) {
Dr. David Alan Gilbert92fd4532019-07-29 17:29:02 +0100461 continue;
462 }
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400463 p->used = true;
Markus Armbruster778a2dc2020-07-07 18:05:56 +0200464 if (!object_property_parse(obj, p->property, p->value, &err)) {
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400465 error_prepend(&err, "can't apply global %s.%s=%s: ",
466 p->driver, p->property, p->value);
Marc-André Lureau50545b22018-11-07 15:35:34 +0400467 /*
468 * If errp != NULL, propagate error and return.
469 * If errp == NULL, report a warning, but keep going
470 * with the remaining globals.
471 */
472 if (errp) {
473 error_propagate(errp, err);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200474 return false;
Marc-André Lureau50545b22018-11-07 15:35:34 +0400475 } else {
476 warn_report_err(err);
477 }
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400478 }
479 }
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200480
481 return true;
Marc-André Lureauea9ce892018-11-26 22:04:32 +0400482}
483
Markus Armbruster617902a2019-03-08 14:14:35 +0100484/*
485 * Global property defaults
486 * Slot 0: accelerator's global property defaults
487 * Slot 1: machine's global property defaults
Paolo Bonzini1fff3c22019-11-13 13:33:44 +0100488 * Slot 2: global properties from legacy command line option
Markus Armbruster617902a2019-03-08 14:14:35 +0100489 * Each is a GPtrArray of of GlobalProperty.
490 * Applied in order, later entries override earlier ones.
491 */
Paolo Bonzini1fff3c22019-11-13 13:33:44 +0100492static GPtrArray *object_compat_props[3];
493
494/*
495 * Retrieve @GPtrArray for global property defined with options
496 * other than "-global". These are generally used for syntactic
497 * sugar and legacy command line options.
498 */
Greg Kurza8dc82c2020-08-14 09:24:50 +0200499void object_register_sugar_prop(const char *driver, const char *prop,
500 const char *value, bool optional)
Paolo Bonzini1fff3c22019-11-13 13:33:44 +0100501{
502 GlobalProperty *g;
503 if (!object_compat_props[2]) {
504 object_compat_props[2] = g_ptr_array_new();
505 }
506 g = g_new0(GlobalProperty, 1);
507 g->driver = g_strdup(driver);
508 g->property = g_strdup(prop);
509 g->value = g_strdup(value);
Greg Kurza8dc82c2020-08-14 09:24:50 +0200510 g->optional = optional;
Paolo Bonzini1fff3c22019-11-13 13:33:44 +0100511 g_ptr_array_add(object_compat_props[2], g);
512}
Markus Armbruster617902a2019-03-08 14:14:35 +0100513
514/*
515 * Set machine's global property defaults to @compat_props.
516 * May be called at most once.
517 */
518void object_set_machine_compat_props(GPtrArray *compat_props)
519{
520 assert(!object_compat_props[1]);
521 object_compat_props[1] = compat_props;
522}
523
524/*
525 * Set accelerator's global property defaults to @compat_props.
526 * May be called at most once.
527 */
528void object_set_accelerator_compat_props(GPtrArray *compat_props)
529{
530 assert(!object_compat_props[0]);
531 object_compat_props[0] = compat_props;
532}
533
534void object_apply_compat_props(Object *obj)
535{
536 int i;
537
538 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
539 object_apply_global_props(obj, object_compat_props[i],
Paolo Bonzini1fff3c22019-11-13 13:33:44 +0100540 i == 2 ? &error_fatal : &error_abort);
Markus Armbruster617902a2019-03-08 14:14:35 +0100541 }
542}
543
Marc-André Lureau2a1be4b2020-01-10 19:30:19 +0400544static void object_class_property_init_all(Object *obj)
545{
546 ObjectPropertyIterator iter;
547 ObjectProperty *prop;
548
549 object_class_property_iter_init(&iter, object_get_class(obj));
550 while ((prop = object_property_iter_next(&iter))) {
551 if (prop->init) {
552 prop->init(obj, prop);
553 }
554 }
555}
556
Masahiro Yamadae27a9592020-05-13 02:31:04 +0900557static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600558{
Igor Mitsyankoac451032012-02-28 15:57:11 +0400559 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400560
Markus Armbruster719a3072018-06-08 19:02:31 +0200561 g_assert(type->instance_size >= sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600562 g_assert(type->abstract == false);
Markus Armbruster719a3072018-06-08 19:02:31 +0200563 g_assert(size >= type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600564
565 memset(obj, 0, type->instance_size);
566 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100567 object_ref(obj);
Marc-André Lureau2a1be4b2020-01-10 19:30:19 +0400568 object_class_property_init_all(obj);
Pavel Fedinb604a852015-10-13 13:37:45 +0100569 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
570 NULL, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600571 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300572 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600573}
574
Andreas Färber213f0c42013-08-23 19:37:12 +0200575void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600576{
577 TypeImpl *type = type_get_by_name(typename);
578
Gerd Hoffmann64f7aec2020-09-14 15:42:22 +0200579#ifdef CONFIG_MODULES
580 if (!type) {
Claudio Fontanac551fb02022-09-29 11:30:33 +0200581 int rv = module_load_qom(typename, &error_fatal);
582 if (rv > 0) {
583 type = type_get_by_name(typename);
584 } else {
585 error_report("missing object type '%s'", typename);
586 exit(1);
587 }
Gerd Hoffmann64f7aec2020-09-14 15:42:22 +0200588 }
589#endif
Philippe Mathieu-Daudée02bdf12019-04-27 15:56:42 +0200590 if (!type) {
591 error_report("missing object type '%s'", typename);
592 abort();
593 }
594
Andreas Färber5b9237f2013-08-30 18:28:37 +0200595 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600596}
597
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200598bool object_initialize_child_with_props(Object *parentobj,
599 const char *propname,
600 void *childobj, size_t size,
601 const char *type,
602 Error **errp, ...)
Thomas Huth0210b392018-07-16 14:59:18 +0200603{
604 va_list vargs;
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200605 bool ok;
Thomas Huth0210b392018-07-16 14:59:18 +0200606
607 va_start(vargs, errp);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200608 ok = object_initialize_child_with_propsv(parentobj, propname,
609 childobj, size, type, errp,
610 vargs);
Thomas Huth0210b392018-07-16 14:59:18 +0200611 va_end(vargs);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200612 return ok;
Thomas Huth0210b392018-07-16 14:59:18 +0200613}
614
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200615bool object_initialize_child_with_propsv(Object *parentobj,
616 const char *propname,
617 void *childobj, size_t size,
618 const char *type,
619 Error **errp, va_list vargs)
Thomas Huth0210b392018-07-16 14:59:18 +0200620{
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200621 bool ok = false;
Thomas Huth0210b392018-07-16 14:59:18 +0200622 Object *obj;
Marc-André Lureau3650b2d2018-12-04 18:20:07 +0400623 UserCreatable *uc;
Thomas Huth0210b392018-07-16 14:59:18 +0200624
625 object_initialize(childobj, size, type);
626 obj = OBJECT(childobj);
627
Markus Armbrusterb783f542020-07-07 18:05:58 +0200628 if (!object_set_propv(obj, errp, vargs)) {
Thomas Huth0210b392018-07-16 14:59:18 +0200629 goto out;
630 }
631
Markus Armbrusterd2623122020-05-05 17:29:22 +0200632 object_property_add_child(parentobj, propname, obj);
Thomas Huth0210b392018-07-16 14:59:18 +0200633
Marc-André Lureau3650b2d2018-12-04 18:20:07 +0400634 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
635 if (uc) {
Markus Armbruster992861f2020-07-07 18:06:04 +0200636 if (!user_creatable_complete(uc, errp)) {
Thomas Huth0210b392018-07-16 14:59:18 +0200637 object_unparent(obj);
638 goto out;
639 }
640 }
641
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200642 ok = true;
643
Markus Armbruster975ac452020-05-05 17:29:09 +0200644out:
Thomas Huth0210b392018-07-16 14:59:18 +0200645 /*
Markus Armbruster975ac452020-05-05 17:29:09 +0200646 * We want @obj's reference to be 1 on success, 0 on failure.
647 * On success, it's 2: one taken by object_initialize(), and one
648 * by object_property_add_child().
649 * On failure in object_initialize() or earlier, it's 1.
650 * On failure afterwards, it's also 1: object_unparent() releases
651 * the reference taken by object_property_add_child().
Thomas Huth0210b392018-07-16 14:59:18 +0200652 */
653 object_unref(obj);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +0200654 return ok;
Thomas Huth0210b392018-07-16 14:59:18 +0200655}
656
Markus Armbruster9fc7fc42020-06-10 07:32:25 +0200657void object_initialize_child_internal(Object *parent,
658 const char *propname,
659 void *child, size_t size,
660 const char *type)
661{
662 object_initialize_child_with_props(parent, propname, child, size, type,
663 &error_abort, NULL);
664}
665
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200666static inline bool object_property_is_child(ObjectProperty *prop)
667{
668 return strstart(prop->type, "child<", NULL);
669}
670
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600671static void object_property_del_all(Object *obj)
672{
Marc-André Lureau9859fac2020-01-10 19:30:30 +0400673 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
Pavel Fedinb604a852015-10-13 13:37:45 +0100674 ObjectProperty *prop;
Marc-André Lureau9859fac2020-01-10 19:30:30 +0400675 ObjectPropertyIterator iter;
Pavel Fedinb604a852015-10-13 13:37:45 +0100676 bool released;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600677
Pavel Fedinb604a852015-10-13 13:37:45 +0100678 do {
679 released = false;
Marc-André Lureau9859fac2020-01-10 19:30:30 +0400680 object_property_iter_init(&iter, obj);
681 while ((prop = object_property_iter_next(&iter)) != NULL) {
682 if (g_hash_table_add(done, prop)) {
683 if (prop->release) {
684 prop->release(obj, prop->name, prop->opaque);
685 released = true;
686 break;
687 }
Pavel Fedinb604a852015-10-13 13:37:45 +0100688 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600689 }
Pavel Fedinb604a852015-10-13 13:37:45 +0100690 } while (released);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600691
Pavel Fedinb604a852015-10-13 13:37:45 +0100692 g_hash_table_unref(obj->properties);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600693}
694
Markus Armbrusterf73a32a2020-05-05 17:29:11 +0200695static void object_property_del_child(Object *obj, Object *child)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600696{
697 ObjectProperty *prop;
Pavel Fedinb604a852015-10-13 13:37:45 +0100698 GHashTableIter iter;
699 gpointer key, value;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600700
Pavel Fedinb604a852015-10-13 13:37:45 +0100701 g_hash_table_iter_init(&iter, obj->properties);
702 while (g_hash_table_iter_next(&iter, &key, &value)) {
703 prop = value;
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200704 if (object_property_is_child(prop) && prop->opaque == child) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100705 if (prop->release) {
706 prop->release(obj, prop->name, prop->opaque);
707 prop->release = NULL;
708 }
709 break;
710 }
711 }
712 g_hash_table_iter_init(&iter, obj->properties);
713 while (g_hash_table_iter_next(&iter, &key, &value)) {
714 prop = value;
715 if (object_property_is_child(prop) && prop->opaque == child) {
716 g_hash_table_iter_remove(&iter);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100717 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600718 }
719 }
720}
721
722void object_unparent(Object *obj)
723{
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200724 if (obj->parent) {
Markus Armbrusterf73a32a2020-05-05 17:29:11 +0200725 object_property_del_child(obj->parent, obj);
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200726 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600727}
728
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600729static void object_deinit(Object *obj, TypeImpl *type)
730{
731 if (type->instance_finalize) {
732 type->instance_finalize(obj);
733 }
734
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600735 if (type_has_parent(type)) {
736 object_deinit(obj, type_get_parent(type));
737 }
738}
739
Paolo Bonzini339c2702012-11-23 09:47:16 +0100740static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600741{
742 Object *obj = data;
743 TypeImpl *ti = obj->class->type;
744
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600745 object_property_del_all(obj);
Paolo Bonzini76a6e1c2014-06-11 11:58:30 +0200746 object_deinit(obj, ti);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600747
Markus Armbruster719a3072018-06-08 19:02:31 +0200748 g_assert(obj->ref == 0);
Eduardo Habkost63f957a2020-12-15 17:41:33 -0500749 g_assert(obj->parent == NULL);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100750 if (obj->free) {
751 obj->free(obj);
752 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600753}
754
Richard Henderson4c880f32020-09-15 17:46:34 -0700755/* Find the minimum alignment guaranteed by the system malloc. */
756#if __STDC_VERSION__ >= 201112L
Doug Evans3a2e9822021-02-12 00:56:30 +0000757typedef max_align_t qemu_max_align_t;
Richard Henderson4c880f32020-09-15 17:46:34 -0700758#else
759typedef union {
760 long l;
761 void *p;
762 double d;
763 long double ld;
764} qemu_max_align_t;
765#endif
766
Marc-André Lureau63f7b102016-12-12 20:31:51 +0300767static Object *object_new_with_type(Type type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600768{
769 Object *obj;
Richard Henderson4c880f32020-09-15 17:46:34 -0700770 size_t size, align;
771 void (*obj_free)(void *);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600772
773 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400774 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600775
Richard Henderson4c880f32020-09-15 17:46:34 -0700776 size = type->instance_size;
777 align = type->instance_align;
778
779 /*
780 * Do not use qemu_memalign unless required. Depending on the
781 * implementation, extra alignment implies extra overhead.
782 */
783 if (likely(align <= __alignof__(qemu_max_align_t))) {
784 obj = g_malloc(size);
785 obj_free = g_free;
786 } else {
787 obj = qemu_memalign(align, size);
788 obj_free = qemu_vfree;
789 }
790
791 object_initialize_with_type(obj, size, type);
792 obj->free = obj_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600793
794 return obj;
795}
796
Paolo Bonzini3c75e122019-11-13 13:57:55 +0100797Object *object_new_with_class(ObjectClass *klass)
798{
799 return object_new_with_type(klass->type);
800}
801
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600802Object *object_new(const char *typename)
803{
804 TypeImpl *ti = type_get_by_name(typename);
805
806 return object_new_with_type(ti);
807}
808
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100809
810Object *object_new_with_props(const char *typename,
811 Object *parent,
812 const char *id,
813 Error **errp,
814 ...)
815{
816 va_list vargs;
817 Object *obj;
818
819 va_start(vargs, errp);
820 obj = object_new_with_propv(typename, parent, id, errp, vargs);
821 va_end(vargs);
822
823 return obj;
824}
825
826
827Object *object_new_with_propv(const char *typename,
828 Object *parent,
829 const char *id,
830 Error **errp,
831 va_list vargs)
832{
833 Object *obj;
834 ObjectClass *klass;
Marc-André Lureau3650b2d2018-12-04 18:20:07 +0400835 UserCreatable *uc;
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100836
837 klass = object_class_by_name(typename);
838 if (!klass) {
839 error_setg(errp, "invalid object type: %s", typename);
840 return NULL;
841 }
842
843 if (object_class_is_abstract(klass)) {
844 error_setg(errp, "object type '%s' is abstract", typename);
845 return NULL;
846 }
Wei Yang66e11552019-03-11 16:32:34 +0800847 obj = object_new_with_type(klass->type);
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100848
Markus Armbrusterb783f542020-07-07 18:05:58 +0200849 if (!object_set_propv(obj, errp, vargs)) {
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100850 goto error;
851 }
852
Daniel P. Berrangé6134d752018-06-15 16:39:35 +0100853 if (id != NULL) {
Markus Armbrusterd2623122020-05-05 17:29:22 +0200854 object_property_add_child(parent, id, obj);
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100855 }
856
Marc-André Lureau3650b2d2018-12-04 18:20:07 +0400857 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
858 if (uc) {
Markus Armbruster992861f2020-07-07 18:06:04 +0200859 if (!user_creatable_complete(uc, errp)) {
Daniel P. Berrangé6134d752018-06-15 16:39:35 +0100860 if (id != NULL) {
861 object_unparent(obj);
862 }
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100863 goto error;
864 }
865 }
866
Philippe Mathieu-Daudé688ffbb2020-05-12 09:00:19 +0200867 object_unref(obj);
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100868 return obj;
869
870 error:
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100871 object_unref(obj);
872 return NULL;
873}
874
875
Markus Armbrusterb783f542020-07-07 18:05:58 +0200876bool object_set_props(Object *obj,
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100877 Error **errp,
878 ...)
879{
880 va_list vargs;
Markus Armbrusterb783f542020-07-07 18:05:58 +0200881 bool ret;
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100882
883 va_start(vargs, errp);
884 ret = object_set_propv(obj, errp, vargs);
885 va_end(vargs);
886
887 return ret;
888}
889
890
Markus Armbrusterb783f542020-07-07 18:05:58 +0200891bool object_set_propv(Object *obj,
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100892 Error **errp,
893 va_list vargs)
894{
895 const char *propname;
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100896
897 propname = va_arg(vargs, char *);
898 while (propname != NULL) {
899 const char *value = va_arg(vargs, char *);
900
901 g_assert(value != NULL);
Markus Armbruster668f62e2020-07-07 18:06:02 +0200902 if (!object_property_parse(obj, propname, value, errp)) {
Markus Armbrusterb783f542020-07-07 18:05:58 +0200903 return false;
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100904 }
905 propname = va_arg(vargs, char *);
906 }
907
Markus Armbrusterb783f542020-07-07 18:05:58 +0200908 return true;
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100909}
910
911
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600912Object *object_dynamic_cast(Object *obj, const char *typename)
913{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100914 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100915 return obj;
916 }
917
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600918 return NULL;
919}
920
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200921Object *object_dynamic_cast_assert(Object *obj, const char *typename,
922 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600923{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200924 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
925 typename, file, line, func);
926
Paolo Bonzini3556c232013-05-10 14:16:40 +0200927#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500928 int i;
929 Object *inst;
930
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000931 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100932 if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500933 goto out;
934 }
935 }
936
937 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600938
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100939 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200940 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
941 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600942 abort();
943 }
944
Paolo Bonzini3556c232013-05-10 14:16:40 +0200945 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500946
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000947 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500948 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100949 qatomic_set(&obj->class->object_cast_cache[i - 1],
950 qatomic_read(&obj->class->object_cast_cache[i]));
Anthony Liguori03587322013-05-13 15:22:24 -0500951 }
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100952 qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
Anthony Liguori03587322013-05-13 15:22:24 -0500953 }
954
955out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200956#endif
957 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600958}
959
960ObjectClass *object_class_dynamic_cast(ObjectClass *class,
961 const char *typename)
962{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000963 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200964 TypeImpl *target_type;
965 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600966
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200967 if (!class) {
968 return NULL;
969 }
970
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200971 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200972 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200973 if (type->name == typename) {
974 return class;
975 }
976
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200977 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200978 if (!target_type) {
979 /* target class type unknown, so fail the cast */
980 return NULL;
981 }
982
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000983 if (type->class->interfaces &&
984 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000985 int found = 0;
986 GSList *i;
987
988 for (i = class->interfaces; i; i = i->next) {
989 ObjectClass *target_class = i->data;
990
991 if (type_is_ancestor(target_class->type, target_type)) {
992 ret = target_class;
993 found++;
994 }
995 }
996
997 /* The match was ambiguous, don't allow a cast */
998 if (found > 1) {
999 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001000 }
Anthony Liguori33e95c62012-08-10 13:16:10 +10001001 } else if (type_is_ancestor(type, target_type)) {
1002 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001003 }
1004
Anthony Liguori33e95c62012-08-10 13:16:10 +10001005 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001006}
1007
1008ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +02001009 const char *typename,
1010 const char *file, int line,
1011 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001012{
Paolo Bonzinifa131d92013-05-10 14:16:39 +02001013 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001014
Paolo Bonzinifa131d92013-05-10 14:16:39 +02001015 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
1016 typename, file, line, func);
1017
Anthony Liguori03587322013-05-13 15:22:24 -05001018#ifdef CONFIG_QOM_CAST_DEBUG
1019 int i;
1020
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +10001021 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001022 if (qatomic_read(&class->class_cast_cache[i]) == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -05001023 ret = class;
1024 goto out;
1025 }
1026 }
1027#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +10001028 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +02001029 return class;
1030 }
1031#endif
1032
Paolo Bonzinifa131d92013-05-10 14:16:39 +02001033 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +02001034 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +02001035 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
1036 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001037 abort();
1038 }
1039
Anthony Liguori03587322013-05-13 15:22:24 -05001040#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +10001041 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -05001042 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001043 qatomic_set(&class->class_cast_cache[i - 1],
1044 qatomic_read(&class->class_cast_cache[i]));
Anthony Liguori03587322013-05-13 15:22:24 -05001045 }
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001046 qatomic_set(&class->class_cast_cache[i - 1], typename);
Anthony Liguori03587322013-05-13 15:22:24 -05001047 }
1048out:
1049#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001050 return ret;
1051}
1052
Igor Mammedov8f5d58e2017-07-14 10:14:50 +08001053const char *object_get_typename(const Object *obj)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001054{
1055 return obj->class->type->name;
1056}
1057
1058ObjectClass *object_get_class(Object *obj)
1059{
1060 return obj->class;
1061}
1062
Andreas Färber17862372013-01-23 12:20:18 +01001063bool object_class_is_abstract(ObjectClass *klass)
1064{
1065 return klass->type->abstract;
1066}
1067
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001068const char *object_class_get_name(ObjectClass *klass)
1069{
1070 return klass->type->name;
1071}
1072
1073ObjectClass *object_class_by_name(const char *typename)
1074{
1075 TypeImpl *type = type_get_by_name(typename);
1076
1077 if (!type) {
1078 return NULL;
1079 }
1080
Igor Mitsyankoac451032012-02-28 15:57:11 +04001081 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001082
1083 return type->class;
1084}
1085
Gerd Hoffmann0f8198f2020-06-24 15:10:37 +02001086ObjectClass *module_object_class_by_name(const char *typename)
1087{
1088 ObjectClass *oc;
1089
1090 oc = object_class_by_name(typename);
1091#ifdef CONFIG_MODULES
1092 if (!oc) {
Claudio Fontanac551fb02022-09-29 11:30:33 +02001093 Error *local_err = NULL;
1094 int rv = module_load_qom(typename, &local_err);
1095 if (rv > 0) {
1096 oc = object_class_by_name(typename);
1097 } else if (rv < 0) {
1098 error_report_err(local_err);
1099 }
Gerd Hoffmann0f8198f2020-06-24 15:10:37 +02001100 }
1101#endif
1102 return oc;
1103}
1104
Paolo Bonzinie7cce672012-05-02 13:30:54 +02001105ObjectClass *object_class_get_parent(ObjectClass *class)
1106{
1107 TypeImpl *type = type_get_parent(class->type);
1108
1109 if (!type) {
1110 return NULL;
1111 }
1112
1113 type_initialize(type);
1114
1115 return type->class;
1116}
1117
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001118typedef struct OCFData
1119{
1120 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -06001121 const char *implements_type;
1122 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001123 void *opaque;
1124} OCFData;
1125
1126static void object_class_foreach_tramp(gpointer key, gpointer value,
1127 gpointer opaque)
1128{
1129 OCFData *data = opaque;
1130 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -06001131 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001132
Igor Mitsyankoac451032012-02-28 15:57:11 +04001133 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -06001134 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001135
Anthony Liguori93c511a2011-12-22 14:11:53 -06001136 if (!data->include_abstract && type->abstract) {
1137 return;
1138 }
1139
1140 if (data->implements_type &&
1141 !object_class_dynamic_cast(k, data->implements_type)) {
1142 return;
1143 }
1144
1145 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001146}
1147
1148void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -06001149 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001150 void *opaque)
1151{
Anthony Liguori93c511a2011-12-22 14:11:53 -06001152 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001153
Hervé Poussineauf54c19c2013-12-03 16:42:00 +01001154 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001155 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +01001156 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001157}
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001158
Peter Crosthwaited714b8d2015-09-08 17:38:43 +01001159static int do_object_child_foreach(Object *obj,
1160 int (*fn)(Object *child, void *opaque),
1161 void *opaque, bool recurse)
Paolo Bonzini32efc532012-04-11 23:30:20 +02001162{
Pavel Fedinb604a852015-10-13 13:37:45 +01001163 GHashTableIter iter;
1164 ObjectProperty *prop;
Paolo Bonzini32efc532012-04-11 23:30:20 +02001165 int ret = 0;
1166
Pavel Fedinb604a852015-10-13 13:37:45 +01001167 g_hash_table_iter_init(&iter, obj->properties);
1168 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Paolo Bonzini32efc532012-04-11 23:30:20 +02001169 if (object_property_is_child(prop)) {
Peter Crosthwaited714b8d2015-09-08 17:38:43 +01001170 Object *child = prop->opaque;
1171
1172 ret = fn(child, opaque);
Paolo Bonzini32efc532012-04-11 23:30:20 +02001173 if (ret != 0) {
1174 break;
1175 }
Peter Crosthwaited714b8d2015-09-08 17:38:43 +01001176 if (recurse) {
Cédric Le Goater4a391812020-04-04 17:33:40 +02001177 ret = do_object_child_foreach(child, fn, opaque, true);
1178 if (ret != 0) {
1179 break;
1180 }
Peter Crosthwaited714b8d2015-09-08 17:38:43 +01001181 }
Paolo Bonzini32efc532012-04-11 23:30:20 +02001182 }
1183 }
1184 return ret;
1185}
1186
Peter Crosthwaited714b8d2015-09-08 17:38:43 +01001187int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1188 void *opaque)
1189{
1190 return do_object_child_foreach(obj, fn, opaque, false);
1191}
1192
1193int object_child_foreach_recursive(Object *obj,
1194 int (*fn)(Object *child, void *opaque),
1195 void *opaque)
1196{
1197 return do_object_child_foreach(obj, fn, opaque, true);
1198}
1199
Andreas Färber418ba9e2012-02-25 23:07:34 +01001200static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1201{
1202 GSList **list = opaque;
1203
1204 *list = g_slist_prepend(*list, klass);
1205}
1206
1207GSList *object_class_get_list(const char *implements_type,
1208 bool include_abstract)
1209{
1210 GSList *list = NULL;
1211
1212 object_class_foreach(object_class_get_list_tramp,
1213 implements_type, include_abstract, &list);
1214 return list;
1215}
1216
Paolo Bonzini47c66002018-03-03 08:33:10 +01001217static gint object_class_cmp(gconstpointer a, gconstpointer b)
1218{
1219 return strcasecmp(object_class_get_name((ObjectClass *)a),
1220 object_class_get_name((ObjectClass *)b));
1221}
1222
1223GSList *object_class_get_list_sorted(const char *implements_type,
1224 bool include_abstract)
1225{
1226 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1227 object_class_cmp);
1228}
1229
Daniel P. Berrangéc5a61e52020-08-31 17:07:23 -04001230Object *object_ref(void *objptr)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001231{
Daniel P. Berrangéc5a61e52020-08-31 17:07:23 -04001232 Object *obj = OBJECT(objptr);
Michael S. Tsirkine3682872022-02-25 08:40:27 -05001233 uint32_t ref;
1234
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001235 if (!obj) {
Marc-André Lureaub77ade92020-01-10 19:30:31 +04001236 return NULL;
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001237 }
Michael S. Tsirkine3682872022-02-25 08:40:27 -05001238 ref = qatomic_fetch_inc(&obj->ref);
1239 /* Assert waaay before the integer overflows */
1240 g_assert(ref < INT_MAX);
Marc-André Lureaub77ade92020-01-10 19:30:31 +04001241 return obj;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001242}
1243
Daniel P. Berrangéc5a61e52020-08-31 17:07:23 -04001244void object_unref(void *objptr)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001245{
Daniel P. Berrangéc5a61e52020-08-31 17:07:23 -04001246 Object *obj = OBJECT(objptr);
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001247 if (!obj) {
1248 return;
1249 }
Markus Armbruster719a3072018-06-08 19:02:31 +02001250 g_assert(obj->ref > 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001251
1252 /* parent always holds a reference to its children */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001253 if (qatomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001254 object_finalize(obj);
1255 }
1256}
1257
Eric Augerdb57fef2020-06-29 21:34:22 +02001258ObjectProperty *
Markus Armbrusterd2623122020-05-05 17:29:22 +02001259object_property_try_add(Object *obj, const char *name, const char *type,
1260 ObjectPropertyAccessor *get,
1261 ObjectPropertyAccessor *set,
1262 ObjectPropertyRelease *release,
1263 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001264{
Peter Maydell54852b02013-03-25 13:15:13 +00001265 ObjectProperty *prop;
Peter Crosthwaite33965902014-08-19 23:55:52 -07001266 size_t name_len = strlen(name);
1267
1268 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1269 int i;
Ani Sinha1bf8b882020-09-21 15:03:25 +05301270 ObjectProperty *ret = NULL;
Peter Crosthwaite33965902014-08-19 23:55:52 -07001271 char *name_no_array = g_strdup(name);
1272
1273 name_no_array[name_len - 3] = '\0';
Ani Sinha1bf8b882020-09-21 15:03:25 +05301274 for (i = 0; i < INT16_MAX; ++i) {
Peter Crosthwaite33965902014-08-19 23:55:52 -07001275 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1276
Markus Armbrusterd2623122020-05-05 17:29:22 +02001277 ret = object_property_try_add(obj, full_name, type, get, set,
1278 release, opaque, NULL);
Peter Crosthwaite33965902014-08-19 23:55:52 -07001279 g_free(full_name);
1280 if (ret) {
1281 break;
1282 }
1283 }
1284 g_free(name_no_array);
Ani Sinha1bf8b882020-09-21 15:03:25 +05301285 assert(ret);
Peter Crosthwaite33965902014-08-19 23:55:52 -07001286 return ret;
1287 }
Peter Maydell54852b02013-03-25 13:15:13 +00001288
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001289 if (object_property_find(obj, name) != NULL) {
Greg Kurzd55e9372019-11-04 14:23:55 +01001290 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1291 name, object_get_typename(obj));
Pavel Fedinb604a852015-10-13 13:37:45 +01001292 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +00001293 }
1294
1295 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001296
1297 prop->name = g_strdup(name);
1298 prop->type = g_strdup(type);
1299
1300 prop->get = get;
1301 prop->set = set;
1302 prop->release = release;
1303 prop->opaque = opaque;
1304
Pavel Fedinb604a852015-10-13 13:37:45 +01001305 g_hash_table_insert(obj->properties, prop->name, prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001306 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001307}
1308
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001309ObjectProperty *
Markus Armbrusterd2623122020-05-05 17:29:22 +02001310object_property_add(Object *obj, const char *name, const char *type,
1311 ObjectPropertyAccessor *get,
1312 ObjectPropertyAccessor *set,
1313 ObjectPropertyRelease *release,
1314 void *opaque)
1315{
1316 return object_property_try_add(obj, name, type, get, set, release,
1317 opaque, &error_abort);
1318}
1319
1320ObjectProperty *
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001321object_class_property_add(ObjectClass *klass,
1322 const char *name,
1323 const char *type,
1324 ObjectPropertyAccessor *get,
1325 ObjectPropertyAccessor *set,
1326 ObjectPropertyRelease *release,
Markus Armbrusterd2623122020-05-05 17:29:22 +02001327 void *opaque)
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001328{
1329 ObjectProperty *prop;
1330
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001331 assert(!object_class_property_find(klass, name));
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001332
1333 prop = g_malloc0(sizeof(*prop));
1334
1335 prop->name = g_strdup(name);
1336 prop->type = g_strdup(type);
1337
1338 prop->get = get;
1339 prop->set = set;
1340 prop->release = release;
1341 prop->opaque = opaque;
1342
Marc-André Lureauba806ff2020-01-10 19:30:18 +04001343 g_hash_table_insert(klass->properties, prop->name, prop);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001344
1345 return prop;
1346}
1347
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001348ObjectProperty *object_property_find(Object *obj, const char *name)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001349{
1350 ObjectProperty *prop;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001351 ObjectClass *klass = object_get_class(obj);
1352
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001353 prop = object_class_property_find(klass, name);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001354 if (prop) {
1355 return prop;
1356 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001357
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001358 return g_hash_table_lookup(obj->properties, name);
1359}
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001360
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001361ObjectProperty *object_property_find_err(Object *obj, const char *name,
1362 Error **errp)
1363{
1364 ObjectProperty *prop = object_property_find(obj, name);
1365 if (!prop) {
Philippe Mathieu-Daudé4d9c7c82020-09-20 17:53:40 +02001366 error_setg(errp, "Property '%s.%s' not found",
1367 object_get_typename(obj), name);
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001368 }
1369 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001370}
1371
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001372void object_property_iter_init(ObjectPropertyIterator *iter,
1373 Object *obj)
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001374{
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001375 g_hash_table_iter_init(&iter->iter, obj->properties);
1376 iter->nextclass = object_get_class(obj);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001377}
1378
1379ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1380{
Pavel Fedinb604a852015-10-13 13:37:45 +01001381 gpointer key, val;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001382 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1383 if (!iter->nextclass) {
1384 return NULL;
1385 }
1386 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1387 iter->nextclass = object_class_get_parent(iter->nextclass);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001388 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001389 return val;
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001390}
1391
Alexey Kardashevskiy961c47b2018-03-02 00:09:39 +11001392void object_class_property_iter_init(ObjectPropertyIterator *iter,
1393 ObjectClass *klass)
1394{
1395 g_hash_table_iter_init(&iter->iter, klass->properties);
Marc-André Lureau684546d2018-09-06 17:37:36 +04001396 iter->nextclass = object_class_get_parent(klass);
Alexey Kardashevskiy961c47b2018-03-02 00:09:39 +11001397}
1398
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001399ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name)
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001400{
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001401 ObjectClass *parent_klass;
1402
1403 parent_klass = object_class_get_parent(klass);
1404 if (parent_klass) {
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001405 ObjectProperty *prop =
1406 object_class_property_find(parent_klass, name);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001407 if (prop) {
1408 return prop;
1409 }
1410 }
1411
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001412 return g_hash_table_lookup(klass->properties, name);
1413}
1414
1415ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1416 const char *name,
1417 Error **errp)
1418{
1419 ObjectProperty *prop = object_class_property_find(klass, name);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001420 if (!prop) {
1421 error_setg(errp, "Property '.%s' not found", name);
1422 }
1423 return prop;
1424}
1425
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001426
Markus Armbrusterdf4fe0b2020-05-05 17:29:26 +02001427void object_property_del(Object *obj, const char *name)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001428{
Pavel Fedinb604a852015-10-13 13:37:45 +01001429 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1430
Anthony Liguori0866aca2011-12-23 15:34:39 -06001431 if (prop->release) {
1432 prop->release(obj, name, prop->opaque);
1433 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001434 g_hash_table_remove(obj->properties, name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001435}
1436
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001437bool object_property_get(Object *obj, const char *name, Visitor *v,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001438 Error **errp)
1439{
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001440 Error *err = NULL;
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001441 ObjectProperty *prop = object_property_find_err(obj, name, errp);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001442
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001443 if (prop == NULL) {
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001444 return false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001445 }
1446
1447 if (!prop->get) {
Markus Armbruster3f7febc2022-10-12 17:37:58 +02001448 error_setg(errp, "Property '%s.%s' is not readable",
1449 object_get_typename(obj), name);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001450 return false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001451 }
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001452 prop->get(obj, v, name, prop->opaque, &err);
1453 error_propagate(errp, err);
1454 return !err;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001455}
1456
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001457bool object_property_set(Object *obj, const char *name, Visitor *v,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001458 Error **errp)
1459{
Kevin Wolfdbc82212021-10-08 15:34:31 +02001460 ERRP_GUARD();
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001461 ObjectProperty *prop = object_property_find_err(obj, name, errp);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001462
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001463 if (prop == NULL) {
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001464 return false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001465 }
1466
1467 if (!prop->set) {
Markus Armbruster3f7febc2022-10-12 17:37:58 +02001468 error_setg(errp, "Property '%s.%s' is not writable",
1469 object_get_typename(obj), name);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001470 return false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001471 }
Kevin Wolfdbc82212021-10-08 15:34:31 +02001472 prop->set(obj, v, name, prop->opaque, errp);
1473 return !*errp;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001474}
1475
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001476bool object_property_set_str(Object *obj, const char *name,
Markus Armbruster5325cc32020-07-07 18:05:54 +02001477 const char *value, Error **errp)
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001478{
1479 QString *qstr = qstring_from_str(value);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001480 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001481
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001482 qobject_unref(qstr);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001483 return ok;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001484}
1485
1486char *object_property_get_str(Object *obj, const char *name,
1487 Error **errp)
1488{
1489 QObject *ret = object_property_get_qobject(obj, name, errp);
Markus Armbruster26c52822020-12-11 18:11:43 +01001490 QString *qstring;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001491 char *retval;
1492
1493 if (!ret) {
1494 return NULL;
1495 }
Markus Armbruster26c52822020-12-11 18:11:43 +01001496 qstring = qobject_to(QString, ret);
1497 if (!qstring) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001498 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
Markus Armbruster26c52822020-12-11 18:11:43 +01001499 retval = NULL;
1500 } else {
1501 retval = g_strdup(qstring_get_str(qstring));
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001502 }
1503
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001504 qobject_unref(ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001505 return retval;
1506}
1507
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001508bool object_property_set_link(Object *obj, const char *name,
Markus Armbruster5325cc32020-07-07 18:05:54 +02001509 Object *value, Error **errp)
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001510{
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001511 g_autofree char *path = NULL;
1512
Peter Crosthwaited3c49312014-09-25 22:19:19 -07001513 if (value) {
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001514 path = object_get_canonical_path(value);
Peter Crosthwaited3c49312014-09-25 22:19:19 -07001515 }
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001516 return object_property_set_str(obj, name, path ?: "", errp);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001517}
1518
1519Object *object_property_get_link(Object *obj, const char *name,
1520 Error **errp)
1521{
1522 char *str = object_property_get_str(obj, name, errp);
1523 Object *target = NULL;
1524
1525 if (str && *str) {
1526 target = object_resolve_path(str, NULL);
1527 if (!target) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001528 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1529 "Device '%s' not found", str);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001530 }
1531 }
1532
1533 g_free(str);
1534 return target;
1535}
1536
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001537bool object_property_set_bool(Object *obj, const char *name,
Markus Armbruster5325cc32020-07-07 18:05:54 +02001538 bool value, Error **errp)
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001539{
Eric Blakefc48ffc2015-05-15 16:24:59 -06001540 QBool *qbool = qbool_from_bool(value);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001541 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001542
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001543 qobject_unref(qbool);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001544 return ok;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001545}
1546
1547bool object_property_get_bool(Object *obj, const char *name,
1548 Error **errp)
1549{
1550 QObject *ret = object_property_get_qobject(obj, name, errp);
1551 QBool *qbool;
1552 bool retval;
1553
1554 if (!ret) {
1555 return false;
1556 }
Max Reitz7dc847e2018-02-24 16:40:29 +01001557 qbool = qobject_to(QBool, ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001558 if (!qbool) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001559 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001560 retval = false;
1561 } else {
Eric Blakefc48ffc2015-05-15 16:24:59 -06001562 retval = qbool_get_bool(qbool);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001563 }
1564
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001565 qobject_unref(ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001566 return retval;
1567}
1568
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001569bool object_property_set_int(Object *obj, const char *name,
Markus Armbruster5325cc32020-07-07 18:05:54 +02001570 int64_t value, Error **errp)
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001571{
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001572 QNum *qnum = qnum_from_int(value);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001573 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001574
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001575 qobject_unref(qnum);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001576 return ok;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001577}
1578
1579int64_t object_property_get_int(Object *obj, const char *name,
1580 Error **errp)
1581{
1582 QObject *ret = object_property_get_qobject(obj, name, errp);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001583 QNum *qnum;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001584 int64_t retval;
1585
1586 if (!ret) {
1587 return -1;
1588 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001589
Max Reitz7dc847e2018-02-24 16:40:29 +01001590 qnum = qobject_to(QNum, ret);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001591 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001592 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001593 retval = -1;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001594 }
1595
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001596 qobject_unref(ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001597 return retval;
1598}
1599
Marc-André Lureau0e76ed02020-01-10 19:30:23 +04001600static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1601{
1602 Visitor *v = qobject_input_visitor_new(prop->defval);
1603
1604 assert(prop->set != NULL);
1605 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1606
1607 visit_free(v);
1608}
1609
1610static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1611{
1612 assert(!prop->defval);
1613 assert(!prop->init);
1614
1615 prop->defval = defval;
1616 prop->init = object_property_init_defval;
1617}
1618
1619void object_property_set_default_bool(ObjectProperty *prop, bool value)
1620{
1621 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1622}
1623
1624void object_property_set_default_str(ObjectProperty *prop, const char *value)
1625{
1626 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1627}
1628
Kevin Wolf125062e2023-11-09 18:42:38 +01001629void object_property_set_default_list(ObjectProperty *prop)
1630{
1631 object_property_set_default(prop, QOBJECT(qlist_new()));
1632}
1633
Marc-André Lureau0e76ed02020-01-10 19:30:23 +04001634void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1635{
1636 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1637}
1638
1639void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1640{
1641 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1642}
1643
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001644bool object_property_set_uint(Object *obj, const char *name,
Markus Armbruster5325cc32020-07-07 18:05:54 +02001645 uint64_t value, Error **errp)
Marc-André Lureau31527792017-06-07 20:36:04 +04001646{
1647 QNum *qnum = qnum_from_uint(value);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001648 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
Marc-André Lureau31527792017-06-07 20:36:04 +04001649
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001650 qobject_unref(qnum);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001651 return ok;
Marc-André Lureau31527792017-06-07 20:36:04 +04001652}
1653
1654uint64_t object_property_get_uint(Object *obj, const char *name,
1655 Error **errp)
1656{
1657 QObject *ret = object_property_get_qobject(obj, name, errp);
1658 QNum *qnum;
1659 uint64_t retval;
1660
1661 if (!ret) {
1662 return 0;
1663 }
Max Reitz7dc847e2018-02-24 16:40:29 +01001664 qnum = qobject_to(QNum, ret);
Marc-André Lureau31527792017-06-07 20:36:04 +04001665 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1666 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1667 retval = 0;
1668 }
1669
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001670 qobject_unref(ret);
Marc-André Lureau31527792017-06-07 20:36:04 +04001671 return retval;
1672}
1673
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001674typedef struct EnumProperty {
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02001675 const QEnumLookup *lookup;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001676 int (*get)(Object *, Error **);
1677 void (*set)(Object *, int, Error **);
1678} EnumProperty;
1679
Hu Tao1f217722014-05-14 17:43:33 +08001680int object_property_get_enum(Object *obj, const char *name,
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001681 const char *typename, Error **errp)
Hu Tao1f217722014-05-14 17:43:33 +08001682{
Chen Fan976620a2014-08-18 14:46:34 +08001683 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001684 int ret;
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001685 ObjectProperty *prop = object_property_find_err(obj, name, errp);
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001686 EnumProperty *enumprop;
1687
1688 if (prop == NULL) {
Markus Armbrusterd20f6162020-09-17 14:55:39 +02001689 return -1;
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001690 }
1691
1692 if (!g_str_equal(prop->type, typename)) {
1693 error_setg(errp, "Property %s on %s is not '%s' enum type",
1694 name, object_class_get_name(
1695 object_get_class(obj)), typename);
Markus Armbrusterd20f6162020-09-17 14:55:39 +02001696 return -1;
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001697 }
1698
1699 enumprop = prop->opaque;
Hu Tao1f217722014-05-14 17:43:33 +08001700
Markus Armbrusterb555f892020-05-05 17:29:12 +02001701 str = object_property_get_str(obj, name, errp);
1702 if (!str) {
Markus Armbrusterd20f6162020-09-17 14:55:39 +02001703 return -1;
Markus Armbruster4715d422015-08-25 20:00:45 +02001704 }
Chen Fan976620a2014-08-18 14:46:34 +08001705
Markus Armbrusterea097df2020-04-24 10:43:36 +02001706 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001707 g_free(str);
Hu Tao1f217722014-05-14 17:43:33 +08001708
1709 return ret;
1710}
1711
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001712bool object_property_parse(Object *obj, const char *name,
Markus Armbruster5325cc32020-07-07 18:05:54 +02001713 const char *string, Error **errp)
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001714{
Eric Blake7a0525c2016-06-09 10:48:37 -06001715 Visitor *v = string_input_visitor_new(string);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001716 bool ok = object_property_set(obj, name, v, errp);
1717
Eric Blake7a0525c2016-06-09 10:48:37 -06001718 visit_free(v);
Markus Armbruster6fd5bef2020-07-07 18:05:55 +02001719 return ok;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001720}
1721
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001722char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001723 Error **errp)
1724{
Eric Blake3b098d52016-06-09 10:48:43 -06001725 Visitor *v;
Gonglei3a530092014-09-27 13:13:55 +08001726 char *string = NULL;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001727
Eric Blake3b098d52016-06-09 10:48:43 -06001728 v = string_output_visitor_new(human, &string);
Markus Armbrusteraf175e82020-07-07 18:06:03 +02001729 if (!object_property_get(obj, name, v, errp)) {
Gonglei3a530092014-09-27 13:13:55 +08001730 goto out;
1731 }
1732
Eric Blake3b098d52016-06-09 10:48:43 -06001733 visit_complete(v, &string);
Gonglei3a530092014-09-27 13:13:55 +08001734
1735out:
Eric Blake3b098d52016-06-09 10:48:43 -06001736 visit_free(v);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001737 return string;
1738}
1739
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001740const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1741{
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01001742 ObjectProperty *prop = object_property_find_err(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001743 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001744 return NULL;
1745 }
1746
1747 return prop->type;
1748}
1749
1750Object *object_get_root(void)
1751{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001752 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001753
Anthony Liguori8b45d442011-12-23 09:08:05 -06001754 if (!root) {
1755 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001756 }
1757
Anthony Liguori8b45d442011-12-23 09:08:05 -06001758 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001759}
1760
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +01001761Object *object_get_objects_root(void)
1762{
1763 return container_get(object_get_root(), "/objects");
1764}
1765
Peter Xu7c47c4e2017-09-28 10:59:54 +08001766Object *object_get_internal_root(void)
1767{
1768 static Object *internal_root;
1769
1770 if (!internal_root) {
1771 internal_root = object_new("container");
1772 }
1773
1774 return internal_root;
1775}
1776
Eric Blaked7bce992016-01-29 06:48:55 -07001777static void object_get_child_property(Object *obj, Visitor *v,
1778 const char *name, void *opaque,
1779 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001780{
1781 Object *child = opaque;
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02001782 char *path;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001783
1784 path = object_get_canonical_path(child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001785 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001786 g_free(path);
1787}
1788
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02001789static Object *object_resolve_child_property(Object *parent, void *opaque,
1790 const char *part)
Paolo Bonzini64607d02014-06-05 13:11:51 +02001791{
1792 return opaque;
1793}
1794
Anthony Liguoridb85b572011-12-23 08:47:39 -06001795static void object_finalize_child_property(Object *obj, const char *name,
1796 void *opaque)
1797{
1798 Object *child = opaque;
1799
Paolo Bonzinibffc6872014-06-11 11:57:38 +02001800 if (child->class->unparent) {
1801 (child->class->unparent)(child);
1802 }
1803 child->parent = NULL;
Anthony Liguoridb85b572011-12-23 08:47:39 -06001804 object_unref(child);
1805}
1806
Markus Armbruster70251882020-05-05 17:29:14 +02001807ObjectProperty *
Eric Augerdb57fef2020-06-29 21:34:22 +02001808object_property_try_add_child(Object *obj, const char *name,
1809 Object *child, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001810{
Markus Armbruster70251882020-05-05 17:29:14 +02001811 g_autofree char *type = NULL;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001812 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001813
Markus Armbrusterd2623122020-05-05 17:29:22 +02001814 assert(!child->parent);
Peter Crosthwaite8faa2f82014-09-25 22:19:52 -07001815
Philippe Mathieu-Daudé688ffbb2020-05-12 09:00:19 +02001816 type = g_strdup_printf("child<%s>", object_get_typename(child));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001817
Eric Augerdb57fef2020-06-29 21:34:22 +02001818 op = object_property_try_add(obj, name, type, object_get_child_property,
1819 NULL, object_finalize_child_property,
1820 child, errp);
1821 if (!op) {
1822 return NULL;
1823 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001824 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001825 object_ref(child);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001826 child->parent = obj;
Markus Armbruster70251882020-05-05 17:29:14 +02001827 return op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001828}
1829
Eric Augerdb57fef2020-06-29 21:34:22 +02001830ObjectProperty *
1831object_property_add_child(Object *obj, const char *name,
1832 Object *child)
1833{
1834 return object_property_try_add_child(obj, name, child, &error_abort);
1835}
1836
Igor Mammedov8f5d58e2017-07-14 10:14:50 +08001837void object_property_allow_set_link(const Object *obj, const char *name,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001838 Object *val, Error **errp)
1839{
1840 /* Allow the link to be set, always */
1841}
1842
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001843typedef struct {
Marc-André Lureau9941d372020-01-10 19:30:27 +04001844 union {
1845 Object **targetp;
1846 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04001847 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
Marc-André Lureau9941d372020-01-10 19:30:27 +04001848 };
Igor Mammedov8f5d58e2017-07-14 10:14:50 +08001849 void (*check)(const Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001850 ObjectPropertyLinkFlags flags;
1851} LinkProperty;
1852
Marc-André Lureau9941d372020-01-10 19:30:27 +04001853static Object **
1854object_link_get_targetp(Object *obj, LinkProperty *lprop)
1855{
1856 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1857 return &lprop->target;
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04001858 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1859 return (void *)obj + lprop->offset;
Marc-André Lureau9941d372020-01-10 19:30:27 +04001860 } else {
1861 return lprop->targetp;
1862 }
1863}
1864
Eric Blaked7bce992016-01-29 06:48:55 -07001865static void object_get_link_property(Object *obj, Visitor *v,
1866 const char *name, void *opaque,
1867 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001868{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001869 LinkProperty *lprop = opaque;
Marc-André Lureau9941d372020-01-10 19:30:27 +04001870 Object **targetp = object_link_get_targetp(obj, lprop);
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02001871 char *path;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001872
Marc-André Lureau36854202020-01-10 19:30:26 +04001873 if (*targetp) {
1874 path = object_get_canonical_path(*targetp);
Eric Blake51e72bc2016-01-29 06:48:54 -07001875 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001876 g_free(path);
1877 } else {
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02001878 path = (char *)"";
Eric Blake51e72bc2016-01-29 06:48:54 -07001879 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001880 }
1881}
1882
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001883/*
1884 * object_resolve_link:
1885 *
1886 * Lookup an object and ensure its type matches the link property type. This
1887 * is similar to object_resolve_path() except type verification against the
1888 * link property is performed.
1889 *
1890 * Returns: The matched object or NULL on path lookup failures.
1891 */
1892static Object *object_resolve_link(Object *obj, const char *name,
1893 const char *path, Error **errp)
1894{
1895 const char *type;
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02001896 char *target_type;
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001897 bool ambiguous = false;
1898 Object *target;
1899
1900 /* Go from link<FOO> to FOO. */
1901 type = object_property_get_type(obj, name, NULL);
1902 target_type = g_strndup(&type[5], strlen(type) - 6);
1903 target = object_resolve_path_type(path, target_type, &ambiguous);
1904
1905 if (ambiguous) {
Eric Blake455b0fd2015-11-10 23:51:20 -07001906 error_setg(errp, "Path '%s' does not uniquely identify an object",
1907 path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001908 } else if (!target) {
1909 target = object_resolve_path(path, &ambiguous);
1910 if (target || ambiguous) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001911 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001912 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001913 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1914 "Device '%s' not found", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001915 }
1916 target = NULL;
1917 }
1918 g_free(target_type);
1919
1920 return target;
1921}
1922
Eric Blaked7bce992016-01-29 06:48:55 -07001923static void object_set_link_property(Object *obj, Visitor *v,
1924 const char *name, void *opaque,
1925 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001926{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001927 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001928 LinkProperty *prop = opaque;
Marc-André Lureau9941d372020-01-10 19:30:27 +04001929 Object **targetp = object_link_get_targetp(obj, prop);
Marc-André Lureau36854202020-01-10 19:30:26 +04001930 Object *old_target = *targetp;
Markus Armbruster1c94a352020-07-07 18:05:53 +02001931 Object *new_target;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001932 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001933
Markus Armbruster1c94a352020-07-07 18:05:53 +02001934 if (!visit_type_str(v, name, &path, errp)) {
1935 return;
1936 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001937
Markus Armbruster1c94a352020-07-07 18:05:53 +02001938 if (*path) {
1939 new_target = object_resolve_link(obj, name, path, errp);
1940 if (!new_target) {
1941 g_free(path);
1942 return;
1943 }
1944 } else {
1945 new_target = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001946 }
1947
1948 g_free(path);
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001949
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001950 prop->check(obj, name, new_target, &local_err);
1951 if (local_err) {
1952 error_propagate(errp, local_err);
1953 return;
1954 }
1955
Marc-André Lureau36854202020-01-10 19:30:26 +04001956 *targetp = new_target;
Marc-André Lureau8770baf2020-01-10 19:30:25 +04001957 if (prop->flags & OBJ_PROP_LINK_STRONG) {
Marc-André Lureau265b5782018-05-31 21:51:17 +02001958 object_ref(new_target);
1959 object_unref(old_target);
1960 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001961}
1962
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02001963static Object *object_resolve_link_property(Object *parent, void *opaque,
1964 const char *part)
Paolo Bonzini64607d02014-06-05 13:11:51 +02001965{
1966 LinkProperty *lprop = opaque;
1967
Marc-André Lureau9941d372020-01-10 19:30:27 +04001968 return *object_link_get_targetp(parent, lprop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001969}
1970
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001971static void object_release_link_property(Object *obj, const char *name,
1972 void *opaque)
1973{
1974 LinkProperty *prop = opaque;
Marc-André Lureau9941d372020-01-10 19:30:27 +04001975 Object **targetp = object_link_get_targetp(obj, prop);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001976
Marc-André Lureau9941d372020-01-10 19:30:27 +04001977 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1978 object_unref(*targetp);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001979 }
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04001980 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1981 g_free(prop);
1982 }
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001983}
1984
Markus Armbruster70251882020-05-05 17:29:14 +02001985static ObjectProperty *
1986object_add_link_prop(Object *obj, const char *name,
1987 const char *type, void *ptr,
1988 void (*check)(const Object *, const char *,
1989 Object *, Error **),
Markus Armbrusterd2623122020-05-05 17:29:22 +02001990 ObjectPropertyLinkFlags flags)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001991{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001992 LinkProperty *prop = g_malloc(sizeof(*prop));
Markus Armbruster70251882020-05-05 17:29:14 +02001993 g_autofree char *full_type = NULL;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001994 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001995
Marc-André Lureau4a8d5792020-01-10 19:30:28 +04001996 if (flags & OBJ_PROP_LINK_DIRECT) {
1997 prop->target = ptr;
1998 } else {
1999 prop->targetp = ptr;
2000 }
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01002001 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01002002 prop->flags = flags;
2003
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002004 full_type = g_strdup_printf("link<%s>", type);
2005
Paolo Bonzini64607d02014-06-05 13:11:51 +02002006 op = object_property_add(obj, name, full_type,
2007 object_get_link_property,
2008 check ? object_set_link_property : NULL,
2009 object_release_link_property,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002010 prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02002011 op->resolve = object_resolve_link_property;
Markus Armbruster70251882020-05-05 17:29:14 +02002012 return op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002013}
2014
Markus Armbruster70251882020-05-05 17:29:14 +02002015ObjectProperty *
2016object_property_add_link(Object *obj, const char *name,
2017 const char *type, Object **targetp,
2018 void (*check)(const Object *, const char *,
2019 Object *, Error **),
Markus Armbrusterd2623122020-05-05 17:29:22 +02002020 ObjectPropertyLinkFlags flags)
Marc-André Lureau4a8d5792020-01-10 19:30:28 +04002021{
Markus Armbrusterd2623122020-05-05 17:29:22 +02002022 return object_add_link_prop(obj, name, type, targetp, check, flags);
Marc-André Lureau4a8d5792020-01-10 19:30:28 +04002023}
2024
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04002025ObjectProperty *
2026object_class_property_add_link(ObjectClass *oc,
2027 const char *name,
2028 const char *type, ptrdiff_t offset,
2029 void (*check)(const Object *obj, const char *name,
2030 Object *val, Error **errp),
Markus Armbrusterd2623122020-05-05 17:29:22 +02002031 ObjectPropertyLinkFlags flags)
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04002032{
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04002033 LinkProperty *prop = g_new0(LinkProperty, 1);
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02002034 char *full_type;
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04002035 ObjectProperty *op;
2036
2037 prop->offset = offset;
2038 prop->check = check;
2039 prop->flags = flags | OBJ_PROP_LINK_CLASS;
2040
2041 full_type = g_strdup_printf("link<%s>", type);
2042
2043 op = object_class_property_add(oc, name, full_type,
2044 object_get_link_property,
2045 check ? object_set_link_property : NULL,
2046 object_release_link_property,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002047 prop);
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04002048
2049 op->resolve = object_resolve_link_property;
2050
Marc-André Lureau840ecdf2020-01-10 19:30:29 +04002051 g_free(full_type);
2052 return op;
2053}
2054
Markus Armbruster70251882020-05-05 17:29:14 +02002055ObjectProperty *
2056object_property_add_const_link(Object *obj, const char *name,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002057 Object *target)
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02002058{
Markus Armbruster70251882020-05-05 17:29:14 +02002059 return object_add_link_prop(obj, name,
2060 object_get_typename(target), target,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002061 NULL, OBJ_PROP_LINK_DIRECT);
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02002062}
2063
Markus Armbruster7a309cc2020-07-14 18:02:00 +02002064const char *object_get_canonical_path_component(const Object *obj)
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01002065{
2066 ObjectProperty *prop = NULL;
Pavel Fedinb604a852015-10-13 13:37:45 +01002067 GHashTableIter iter;
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01002068
Paolo Bonzini770dec22018-04-30 11:44:17 +02002069 if (obj->parent == NULL) {
2070 return NULL;
2071 }
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01002072
Pavel Fedinb604a852015-10-13 13:37:45 +01002073 g_hash_table_iter_init(&iter, obj->parent->properties);
2074 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01002075 if (!object_property_is_child(prop)) {
2076 continue;
2077 }
2078
2079 if (prop->opaque == obj) {
Markus Armbruster7a309cc2020-07-14 18:02:00 +02002080 return prop->name;
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01002081 }
2082 }
2083
2084 /* obj had a parent but was not a child, should never happen */
2085 g_assert_not_reached();
2086 return NULL;
2087}
2088
Markus Armbrustere8512df2020-05-27 10:47:53 +02002089char *object_get_canonical_path(const Object *obj)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002090{
2091 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01002092 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002093
Paolo Bonzinie40077f2018-05-30 18:16:04 +02002094 if (obj == root) {
2095 return g_strdup("/");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002096 }
2097
Paolo Bonzinie40077f2018-05-30 18:16:04 +02002098 do {
Markus Armbruster7a309cc2020-07-14 18:02:00 +02002099 const char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002100
Paolo Bonzinie40077f2018-05-30 18:16:04 +02002101 if (!component) {
2102 /* A canonical path must be complete, so discard what was
2103 * collected so far.
2104 */
2105 g_free(path);
2106 return NULL;
2107 }
2108
2109 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
2110 g_free(path);
Paolo Bonzinie40077f2018-05-30 18:16:04 +02002111 path = newpath;
2112 obj = obj->parent;
2113 } while (obj != root);
2114
2115 return path;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002116}
2117
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02002118Object *object_resolve_path_component(Object *parent, const char *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02002119{
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01002120 ObjectProperty *prop = object_property_find(parent, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02002121 if (prop == NULL) {
2122 return NULL;
2123 }
2124
Paolo Bonzini64607d02014-06-05 13:11:51 +02002125 if (prop->resolve) {
2126 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02002127 } else {
2128 return NULL;
2129 }
2130}
2131
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002132static Object *object_resolve_abs_path(Object *parent,
Masahiro Yamadaad195c82020-05-10 10:32:35 +09002133 char **parts,
2134 const char *typename)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002135{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002136 Object *child;
2137
Masahiro Yamadaad195c82020-05-10 10:32:35 +09002138 if (*parts == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01002139 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002140 }
2141
Masahiro Yamadaad195c82020-05-10 10:32:35 +09002142 if (strcmp(*parts, "") == 0) {
2143 return object_resolve_abs_path(parent, parts + 1, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002144 }
2145
Masahiro Yamadaad195c82020-05-10 10:32:35 +09002146 child = object_resolve_path_component(parent, *parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002147 if (!child) {
2148 return NULL;
2149 }
2150
Masahiro Yamadaad195c82020-05-10 10:32:35 +09002151 return object_resolve_abs_path(child, parts + 1, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002152}
2153
2154static Object *object_resolve_partial_path(Object *parent,
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02002155 char **parts,
2156 const char *typename,
2157 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002158{
2159 Object *obj;
Pavel Fedinb604a852015-10-13 13:37:45 +01002160 GHashTableIter iter;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002161 ObjectProperty *prop;
2162
Masahiro Yamadaad195c82020-05-10 10:32:35 +09002163 obj = object_resolve_abs_path(parent, parts, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002164
Pavel Fedinb604a852015-10-13 13:37:45 +01002165 g_hash_table_iter_init(&iter, parent->properties);
2166 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002167 Object *found;
2168
Andreas Färber5d9d3f42012-05-27 00:32:40 +02002169 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002170 continue;
2171 }
2172
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01002173 found = object_resolve_partial_path(prop->opaque, parts,
2174 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002175 if (found) {
2176 if (obj) {
Eduardo Habkostebcc4792017-07-07 18:30:52 -03002177 *ambiguous = true;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002178 return NULL;
2179 }
2180 obj = found;
2181 }
2182
Eduardo Habkostebcc4792017-07-07 18:30:52 -03002183 if (*ambiguous) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002184 return NULL;
2185 }
2186 }
2187
2188 return obj;
2189}
2190
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01002191Object *object_resolve_path_type(const char *path, const char *typename,
Eduardo Habkostebcc4792017-07-07 18:30:52 -03002192 bool *ambiguousp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002193{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002194 Object *obj;
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02002195 char **parts;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002196
2197 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02002198 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002199
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02002200 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Eduardo Habkostebcc4792017-07-07 18:30:52 -03002201 bool ambiguous = false;
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01002202 obj = object_resolve_partial_path(object_get_root(), parts,
Eduardo Habkostebcc4792017-07-07 18:30:52 -03002203 typename, &ambiguous);
2204 if (ambiguousp) {
2205 *ambiguousp = ambiguous;
2206 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002207 } else {
Masahiro Yamadaad195c82020-05-10 10:32:35 +09002208 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002209 }
2210
2211 g_strfreev(parts);
2212
2213 return obj;
2214}
2215
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01002216Object *object_resolve_path(const char *path, bool *ambiguous)
2217{
2218 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2219}
2220
Markus Armbruster1bf4d322021-10-19 10:57:11 +02002221Object *object_resolve_path_at(Object *parent, const char *path)
2222{
2223 g_auto(GStrv) parts = g_strsplit(path, "/", 0);
2224
2225 if (*path == '/') {
2226 return object_resolve_abs_path(object_get_root(), parts + 1,
2227 TYPE_OBJECT);
2228 }
2229 return object_resolve_abs_path(parent, parts, TYPE_OBJECT);
2230}
2231
Paolo Bonziniee3b34c2024-02-23 13:43:57 +01002232Object *object_resolve_type_unambiguous(const char *typename, Error **errp)
2233{
2234 bool ambig;
2235 Object *o = object_resolve_path_type("", typename, &ambig);
2236
2237 if (ambig) {
2238 error_setg(errp, "More than one object of type %s", typename);
2239 return NULL;
2240 }
2241 if (!o) {
2242 error_setg(errp, "No object found of type %s", typename);
2243 return NULL;
2244 }
2245 return o;
2246}
2247
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002248typedef struct StringProperty
2249{
2250 char *(*get)(Object *, Error **);
2251 void (*set)(Object *, const char *, Error **);
2252} StringProperty;
2253
Eric Blaked7bce992016-01-29 06:48:55 -07002254static void property_get_str(Object *obj, Visitor *v, const char *name,
2255 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002256{
2257 StringProperty *prop = opaque;
2258 char *value;
Markus Armbrustere1c82372015-08-25 20:00:46 +02002259 Error *err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002260
Markus Armbrustere1c82372015-08-25 20:00:46 +02002261 value = prop->get(obj, &err);
2262 if (err) {
2263 error_propagate(errp, err);
2264 return;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002265 }
Markus Armbrustere1c82372015-08-25 20:00:46 +02002266
Eric Blake51e72bc2016-01-29 06:48:54 -07002267 visit_type_str(v, name, &value, errp);
Markus Armbrustere1c82372015-08-25 20:00:46 +02002268 g_free(value);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002269}
2270
Eric Blaked7bce992016-01-29 06:48:55 -07002271static void property_set_str(Object *obj, Visitor *v, const char *name,
2272 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002273{
2274 StringProperty *prop = opaque;
2275 char *value;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002276
Markus Armbruster668f62e2020-07-07 18:06:02 +02002277 if (!visit_type_str(v, name, &value, errp)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002278 return;
2279 }
2280
2281 prop->set(obj, value, errp);
2282 g_free(value);
2283}
2284
Paolo Bonzini169cbac2020-10-27 12:12:57 -04002285static void property_release_data(Object *obj, const char *name,
2286 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002287{
Paolo Bonzini169cbac2020-10-27 12:12:57 -04002288 g_free(opaque);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002289}
2290
Markus Armbruster70251882020-05-05 17:29:14 +02002291ObjectProperty *
2292object_property_add_str(Object *obj, const char *name,
2293 char *(*get)(Object *, Error **),
Markus Armbrusterd2623122020-05-05 17:29:22 +02002294 void (*set)(Object *, const char *, Error **))
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002295{
2296 StringProperty *prop = g_malloc0(sizeof(*prop));
2297
2298 prop->get = get;
2299 prop->set = set;
2300
Markus Armbrusterd2623122020-05-05 17:29:22 +02002301 return object_property_add(obj, name, "string",
2302 get ? property_get_str : NULL,
2303 set ? property_set_str : NULL,
Paolo Bonzini169cbac2020-10-27 12:12:57 -04002304 property_release_data,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002305 prop);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06002306}
Paolo Bonzini745549c2012-03-31 16:45:54 +02002307
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002308ObjectProperty *
2309object_class_property_add_str(ObjectClass *klass, const char *name,
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002310 char *(*get)(Object *, Error **),
2311 void (*set)(Object *, const char *,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002312 Error **))
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002313{
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002314 StringProperty *prop = g_malloc0(sizeof(*prop));
2315
2316 prop->get = get;
2317 prop->set = set;
2318
Markus Armbrusterd2623122020-05-05 17:29:22 +02002319 return object_class_property_add(klass, name, "string",
2320 get ? property_get_str : NULL,
2321 set ? property_set_str : NULL,
2322 NULL,
2323 prop);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002324}
2325
Anthony Liguori0e558842012-06-25 10:32:46 -05002326typedef struct BoolProperty
2327{
2328 bool (*get)(Object *, Error **);
2329 void (*set)(Object *, bool, Error **);
2330} BoolProperty;
2331
Eric Blaked7bce992016-01-29 06:48:55 -07002332static void property_get_bool(Object *obj, Visitor *v, const char *name,
2333 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05002334{
2335 BoolProperty *prop = opaque;
2336 bool value;
Markus Armbruster4715d422015-08-25 20:00:45 +02002337 Error *err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05002338
Markus Armbruster4715d422015-08-25 20:00:45 +02002339 value = prop->get(obj, &err);
2340 if (err) {
2341 error_propagate(errp, err);
2342 return;
2343 }
2344
Eric Blake51e72bc2016-01-29 06:48:54 -07002345 visit_type_bool(v, name, &value, errp);
Anthony Liguori0e558842012-06-25 10:32:46 -05002346}
2347
Eric Blaked7bce992016-01-29 06:48:55 -07002348static void property_set_bool(Object *obj, Visitor *v, const char *name,
2349 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05002350{
2351 BoolProperty *prop = opaque;
2352 bool value;
Anthony Liguori0e558842012-06-25 10:32:46 -05002353
Markus Armbruster668f62e2020-07-07 18:06:02 +02002354 if (!visit_type_bool(v, name, &value, errp)) {
Anthony Liguori0e558842012-06-25 10:32:46 -05002355 return;
2356 }
2357
2358 prop->set(obj, value, errp);
2359}
2360
Markus Armbruster70251882020-05-05 17:29:14 +02002361ObjectProperty *
2362object_property_add_bool(Object *obj, const char *name,
2363 bool (*get)(Object *, Error **),
Markus Armbrusterd2623122020-05-05 17:29:22 +02002364 void (*set)(Object *, bool, Error **))
Anthony Liguori0e558842012-06-25 10:32:46 -05002365{
2366 BoolProperty *prop = g_malloc0(sizeof(*prop));
2367
2368 prop->get = get;
2369 prop->set = set;
2370
Markus Armbrusterd2623122020-05-05 17:29:22 +02002371 return object_property_add(obj, name, "bool",
2372 get ? property_get_bool : NULL,
2373 set ? property_set_bool : NULL,
Paolo Bonzini169cbac2020-10-27 12:12:57 -04002374 property_release_data,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002375 prop);
Anthony Liguori0e558842012-06-25 10:32:46 -05002376}
2377
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002378ObjectProperty *
2379object_class_property_add_bool(ObjectClass *klass, const char *name,
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002380 bool (*get)(Object *, Error **),
Markus Armbrusterd2623122020-05-05 17:29:22 +02002381 void (*set)(Object *, bool, Error **))
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002382{
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002383 BoolProperty *prop = g_malloc0(sizeof(*prop));
2384
2385 prop->get = get;
2386 prop->set = set;
2387
Markus Armbrusterd2623122020-05-05 17:29:22 +02002388 return object_class_property_add(klass, name, "bool",
2389 get ? property_get_bool : NULL,
2390 set ? property_set_bool : NULL,
2391 NULL,
2392 prop);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002393}
2394
Eric Blaked7bce992016-01-29 06:48:55 -07002395static void property_get_enum(Object *obj, Visitor *v, const char *name,
2396 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002397{
2398 EnumProperty *prop = opaque;
2399 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02002400 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002401
Markus Armbruster4715d422015-08-25 20:00:45 +02002402 value = prop->get(obj, &err);
2403 if (err) {
2404 error_propagate(errp, err);
2405 return;
2406 }
2407
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002408 visit_type_enum(v, name, &value, prop->lookup, errp);
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002409}
2410
Eric Blaked7bce992016-01-29 06:48:55 -07002411static void property_set_enum(Object *obj, Visitor *v, const char *name,
2412 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002413{
2414 EnumProperty *prop = opaque;
2415 int value;
2416
Markus Armbruster668f62e2020-07-07 18:06:02 +02002417 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
Markus Armbruster4715d422015-08-25 20:00:45 +02002418 return;
2419 }
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002420 prop->set(obj, value, errp);
2421}
2422
Markus Armbruster70251882020-05-05 17:29:14 +02002423ObjectProperty *
2424object_property_add_enum(Object *obj, const char *name,
2425 const char *typename,
2426 const QEnumLookup *lookup,
2427 int (*get)(Object *, Error **),
Markus Armbrusterd2623122020-05-05 17:29:22 +02002428 void (*set)(Object *, int, Error **))
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002429{
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002430 EnumProperty *prop = g_malloc(sizeof(*prop));
2431
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002432 prop->lookup = lookup;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002433 prop->get = get;
2434 prop->set = set;
2435
Markus Armbrusterd2623122020-05-05 17:29:22 +02002436 return object_property_add(obj, name, typename,
2437 get ? property_get_enum : NULL,
2438 set ? property_set_enum : NULL,
Paolo Bonzini169cbac2020-10-27 12:12:57 -04002439 property_release_data,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002440 prop);
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002441}
2442
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002443ObjectProperty *
2444object_class_property_add_enum(ObjectClass *klass, const char *name,
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002445 const char *typename,
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002446 const QEnumLookup *lookup,
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002447 int (*get)(Object *, Error **),
Markus Armbrusterd2623122020-05-05 17:29:22 +02002448 void (*set)(Object *, int, Error **))
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002449{
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002450 EnumProperty *prop = g_malloc(sizeof(*prop));
2451
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002452 prop->lookup = lookup;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002453 prop->get = get;
2454 prop->set = set;
2455
Markus Armbrusterd2623122020-05-05 17:29:22 +02002456 return object_class_property_add(klass, name, typename,
2457 get ? property_get_enum : NULL,
2458 set ? property_set_enum : NULL,
2459 NULL,
2460 prop);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002461}
2462
David Gibson8e099d12015-02-06 14:55:45 +11002463typedef struct TMProperty {
2464 void (*get)(Object *, struct tm *, Error **);
2465} TMProperty;
2466
Eric Blaked7bce992016-01-29 06:48:55 -07002467static void property_get_tm(Object *obj, Visitor *v, const char *name,
2468 void *opaque, Error **errp)
David Gibson8e099d12015-02-06 14:55:45 +11002469{
2470 TMProperty *prop = opaque;
2471 Error *err = NULL;
2472 struct tm value;
2473
2474 prop->get(obj, &value, &err);
2475 if (err) {
Markus Armbruster992861f2020-07-07 18:06:04 +02002476 error_propagate(errp, err);
2477 return;
David Gibson8e099d12015-02-06 14:55:45 +11002478 }
2479
Markus Armbruster992861f2020-07-07 18:06:04 +02002480 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2481 return;
David Gibson8e099d12015-02-06 14:55:45 +11002482 }
Markus Armbruster992861f2020-07-07 18:06:04 +02002483 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
David Gibson8e099d12015-02-06 14:55:45 +11002484 goto out_end;
2485 }
Markus Armbruster992861f2020-07-07 18:06:04 +02002486 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
David Gibson8e099d12015-02-06 14:55:45 +11002487 goto out_end;
2488 }
Markus Armbruster992861f2020-07-07 18:06:04 +02002489 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
David Gibson8e099d12015-02-06 14:55:45 +11002490 goto out_end;
2491 }
Markus Armbruster992861f2020-07-07 18:06:04 +02002492 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
David Gibson8e099d12015-02-06 14:55:45 +11002493 goto out_end;
2494 }
Markus Armbruster992861f2020-07-07 18:06:04 +02002495 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
David Gibson8e099d12015-02-06 14:55:45 +11002496 goto out_end;
2497 }
Markus Armbruster992861f2020-07-07 18:06:04 +02002498 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
David Gibson8e099d12015-02-06 14:55:45 +11002499 goto out_end;
2500 }
Markus Armbruster992861f2020-07-07 18:06:04 +02002501 visit_check_struct(v, errp);
David Gibson8e099d12015-02-06 14:55:45 +11002502out_end:
Eric Blake1158bb22016-06-09 10:48:34 -06002503 visit_end_struct(v, NULL);
David Gibson8e099d12015-02-06 14:55:45 +11002504}
2505
Markus Armbruster70251882020-05-05 17:29:14 +02002506ObjectProperty *
2507object_property_add_tm(Object *obj, const char *name,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002508 void (*get)(Object *, struct tm *, Error **))
David Gibson8e099d12015-02-06 14:55:45 +11002509{
David Gibson8e099d12015-02-06 14:55:45 +11002510 TMProperty *prop = g_malloc0(sizeof(*prop));
2511
2512 prop->get = get;
2513
Markus Armbrusterd2623122020-05-05 17:29:22 +02002514 return object_property_add(obj, name, "struct tm",
2515 get ? property_get_tm : NULL, NULL,
Paolo Bonzini169cbac2020-10-27 12:12:57 -04002516 property_release_data,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002517 prop);
David Gibson8e099d12015-02-06 14:55:45 +11002518}
2519
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002520ObjectProperty *
2521object_class_property_add_tm(ObjectClass *klass, const char *name,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002522 void (*get)(Object *, struct tm *, Error **))
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002523{
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002524 TMProperty *prop = g_malloc0(sizeof(*prop));
2525
2526 prop->get = get;
2527
Markus Armbrusterd2623122020-05-05 17:29:22 +02002528 return object_class_property_add(klass, name, "struct tm",
2529 get ? property_get_tm : NULL,
2530 NULL, NULL, prop);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002531}
2532
Markus Armbruster90c69fb2020-07-07 18:05:50 +02002533static char *object_get_type(Object *obj, Error **errp)
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002534{
2535 return g_strdup(object_get_typename(obj));
2536}
2537
Eric Blaked7bce992016-01-29 06:48:55 -07002538static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2539 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002540{
2541 uint8_t value = *(uint8_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002542 visit_type_uint8(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002543}
2544
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002545static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2546 void *opaque, Error **errp)
2547{
2548 uint8_t *field = opaque;
2549 uint8_t value;
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002550
Markus Armbruster668f62e2020-07-07 18:06:02 +02002551 if (!visit_type_uint8(v, name, &value, errp)) {
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002552 return;
2553 }
2554
2555 *field = value;
2556}
2557
Eric Blaked7bce992016-01-29 06:48:55 -07002558static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2559 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002560{
2561 uint16_t value = *(uint16_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002562 visit_type_uint16(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002563}
2564
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002565static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2566 void *opaque, Error **errp)
2567{
2568 uint16_t *field = opaque;
2569 uint16_t value;
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002570
Markus Armbruster668f62e2020-07-07 18:06:02 +02002571 if (!visit_type_uint16(v, name, &value, errp)) {
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002572 return;
2573 }
2574
2575 *field = value;
2576}
2577
Eric Blaked7bce992016-01-29 06:48:55 -07002578static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2579 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002580{
2581 uint32_t value = *(uint32_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002582 visit_type_uint32(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002583}
2584
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002585static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2586 void *opaque, Error **errp)
2587{
2588 uint32_t *field = opaque;
2589 uint32_t value;
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002590
Markus Armbruster668f62e2020-07-07 18:06:02 +02002591 if (!visit_type_uint32(v, name, &value, errp)) {
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002592 return;
2593 }
2594
2595 *field = value;
2596}
2597
Eric Blaked7bce992016-01-29 06:48:55 -07002598static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2599 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002600{
2601 uint64_t value = *(uint64_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002602 visit_type_uint64(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002603}
2604
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002605static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2606 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002607{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002608 uint64_t *field = opaque;
2609 uint64_t value;
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002610
Markus Armbruster668f62e2020-07-07 18:06:02 +02002611 if (!visit_type_uint64(v, name, &value, errp)) {
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002612 return;
2613 }
2614
2615 *field = value;
2616}
2617
Markus Armbruster70251882020-05-05 17:29:14 +02002618ObjectProperty *
2619object_property_add_uint8_ptr(Object *obj, const char *name,
2620 const uint8_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002621 ObjectPropertyFlags flags)
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002622{
2623 ObjectPropertyAccessor *getter = NULL;
2624 ObjectPropertyAccessor *setter = NULL;
2625
2626 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2627 getter = property_get_uint8_ptr;
2628 }
2629
2630 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2631 setter = property_set_uint8_ptr;
2632 }
2633
Markus Armbruster70251882020-05-05 17:29:14 +02002634 return object_property_add(obj, name, "uint8",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002635 getter, setter, NULL, (void *)v);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002636}
2637
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002638ObjectProperty *
2639object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002640 const uint8_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002641 ObjectPropertyFlags flags)
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002642{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002643 ObjectPropertyAccessor *getter = NULL;
2644 ObjectPropertyAccessor *setter = NULL;
2645
2646 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2647 getter = property_get_uint8_ptr;
2648 }
2649
2650 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2651 setter = property_set_uint8_ptr;
2652 }
2653
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002654 return object_class_property_add(klass, name, "uint8",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002655 getter, setter, NULL, (void *)v);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002656}
2657
Markus Armbruster70251882020-05-05 17:29:14 +02002658ObjectProperty *
2659object_property_add_uint16_ptr(Object *obj, const char *name,
2660 const uint16_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002661 ObjectPropertyFlags flags)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002662{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002663 ObjectPropertyAccessor *getter = NULL;
2664 ObjectPropertyAccessor *setter = NULL;
2665
2666 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2667 getter = property_get_uint16_ptr;
2668 }
2669
2670 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2671 setter = property_set_uint16_ptr;
2672 }
2673
Markus Armbruster70251882020-05-05 17:29:14 +02002674 return object_property_add(obj, name, "uint16",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002675 getter, setter, NULL, (void *)v);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002676}
2677
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002678ObjectProperty *
2679object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002680 const uint16_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002681 ObjectPropertyFlags flags)
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002682{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002683 ObjectPropertyAccessor *getter = NULL;
2684 ObjectPropertyAccessor *setter = NULL;
2685
2686 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2687 getter = property_get_uint16_ptr;
2688 }
2689
2690 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2691 setter = property_set_uint16_ptr;
2692 }
2693
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002694 return object_class_property_add(klass, name, "uint16",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002695 getter, setter, NULL, (void *)v);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002696}
2697
Markus Armbruster70251882020-05-05 17:29:14 +02002698ObjectProperty *
2699object_property_add_uint32_ptr(Object *obj, const char *name,
2700 const uint32_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002701 ObjectPropertyFlags flags)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002702{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002703 ObjectPropertyAccessor *getter = NULL;
2704 ObjectPropertyAccessor *setter = NULL;
2705
2706 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2707 getter = property_get_uint32_ptr;
2708 }
2709
2710 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2711 setter = property_set_uint32_ptr;
2712 }
2713
Markus Armbruster70251882020-05-05 17:29:14 +02002714 return object_property_add(obj, name, "uint32",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002715 getter, setter, NULL, (void *)v);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002716}
2717
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002718ObjectProperty *
2719object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002720 const uint32_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002721 ObjectPropertyFlags flags)
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002722{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002723 ObjectPropertyAccessor *getter = NULL;
2724 ObjectPropertyAccessor *setter = NULL;
2725
2726 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2727 getter = property_get_uint32_ptr;
2728 }
2729
2730 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2731 setter = property_set_uint32_ptr;
2732 }
2733
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002734 return object_class_property_add(klass, name, "uint32",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002735 getter, setter, NULL, (void *)v);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002736}
2737
Markus Armbruster70251882020-05-05 17:29:14 +02002738ObjectProperty *
2739object_property_add_uint64_ptr(Object *obj, const char *name,
2740 const uint64_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002741 ObjectPropertyFlags flags)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002742{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002743 ObjectPropertyAccessor *getter = NULL;
2744 ObjectPropertyAccessor *setter = NULL;
2745
2746 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2747 getter = property_get_uint64_ptr;
2748 }
2749
2750 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2751 setter = property_set_uint64_ptr;
2752 }
2753
Markus Armbruster70251882020-05-05 17:29:14 +02002754 return object_property_add(obj, name, "uint64",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002755 getter, setter, NULL, (void *)v);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002756}
2757
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002758ObjectProperty *
2759object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002760 const uint64_t *v,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002761 ObjectPropertyFlags flags)
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002762{
Felipe Franciosi836e1b32020-02-04 13:15:58 +00002763 ObjectPropertyAccessor *getter = NULL;
2764 ObjectPropertyAccessor *setter = NULL;
2765
2766 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2767 getter = property_get_uint64_ptr;
2768 }
2769
2770 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2771 setter = property_set_uint64_ptr;
2772 }
2773
Marc-André Lureaua3a16212020-01-10 19:30:21 +04002774 return object_class_property_add(klass, name, "uint64",
Markus Armbrusterd2623122020-05-05 17:29:22 +02002775 getter, setter, NULL, (void *)v);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002776}
2777
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002778typedef struct {
2779 Object *target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002780 char *target_name;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002781} AliasProperty;
2782
Eric Blaked7bce992016-01-29 06:48:55 -07002783static void property_get_alias(Object *obj, Visitor *v, const char *name,
2784 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002785{
2786 AliasProperty *prop = opaque;
Paolo Bonzinicbc94d92021-07-18 08:50:44 +02002787 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002788
Paolo Bonzinicbc94d92021-07-18 08:50:44 +02002789 object_property_get(prop->target_obj, prop->target_name, alias_v, errp);
2790 visit_free(alias_v);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002791}
2792
Eric Blaked7bce992016-01-29 06:48:55 -07002793static void property_set_alias(Object *obj, Visitor *v, const char *name,
2794 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002795{
2796 AliasProperty *prop = opaque;
Paolo Bonzinicbc94d92021-07-18 08:50:44 +02002797 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002798
Paolo Bonzinicbc94d92021-07-18 08:50:44 +02002799 object_property_set(prop->target_obj, prop->target_name, alias_v, errp);
2800 visit_free(alias_v);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002801}
2802
Paolo Bonzini64607d02014-06-05 13:11:51 +02002803static Object *property_resolve_alias(Object *obj, void *opaque,
Markus Armbrusterddfb0ba2020-05-05 17:29:10 +02002804 const char *part)
Paolo Bonzini64607d02014-06-05 13:11:51 +02002805{
2806 AliasProperty *prop = opaque;
2807
2808 return object_resolve_path_component(prop->target_obj, prop->target_name);
2809}
2810
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002811static void property_release_alias(Object *obj, const char *name, void *opaque)
2812{
2813 AliasProperty *prop = opaque;
2814
Eduardo Habkost1590d262015-04-09 16:57:29 -03002815 g_free(prop->target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002816 g_free(prop);
2817}
2818
Markus Armbruster70251882020-05-05 17:29:14 +02002819ObjectProperty *
2820object_property_add_alias(Object *obj, const char *name,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002821 Object *target_obj, const char *target_name)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002822{
2823 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02002824 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002825 ObjectProperty *target_prop;
Markus Armbruster70251882020-05-05 17:29:14 +02002826 g_autofree char *prop_type = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002827
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01002828 target_prop = object_property_find_err(target_obj, target_name,
2829 &error_abort);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002830
Paolo Bonzinid1906982014-06-10 11:17:35 +02002831 if (object_property_is_child(target_prop)) {
2832 prop_type = g_strdup_printf("link%s",
2833 target_prop->type + strlen("child"));
2834 } else {
2835 prop_type = g_strdup(target_prop->type);
2836 }
2837
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002838 prop = g_malloc(sizeof(*prop));
2839 prop->target_obj = target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002840 prop->target_name = g_strdup(target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002841
Paolo Bonzinid1906982014-06-10 11:17:35 +02002842 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02002843 property_get_alias,
2844 property_set_alias,
2845 property_release_alias,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002846 prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02002847 op->resolve = property_resolve_alias;
Marc-André Lureau0e76ed02020-01-10 19:30:23 +04002848 if (target_prop->defval) {
2849 op->defval = qobject_ref(target_prop->defval);
2850 }
Paolo Bonzinid1906982014-06-10 11:17:35 +02002851
Andreas Färbera18bb412015-03-27 17:34:10 +01002852 object_property_set_description(obj, op->name,
Markus Armbruster7eecec72020-05-05 17:29:15 +02002853 target_prop->description);
Markus Armbruster70251882020-05-05 17:29:14 +02002854 return op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002855}
2856
Gonglei80742642014-10-07 14:33:21 +08002857void object_property_set_description(Object *obj, const char *name,
Markus Armbruster7eecec72020-05-05 17:29:15 +02002858 const char *description)
Gonglei80742642014-10-07 14:33:21 +08002859{
2860 ObjectProperty *op;
2861
Daniel P. Berrangéefba1592020-09-14 14:56:17 +01002862 op = object_property_find_err(obj, name, &error_abort);
Gonglei80742642014-10-07 14:33:21 +08002863 g_free(op->description);
2864 op->description = g_strdup(description);
2865}
2866
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002867void object_class_property_set_description(ObjectClass *klass,
2868 const char *name,
Markus Armbruster7eecec72020-05-05 17:29:15 +02002869 const char *description)
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002870{
2871 ObjectProperty *op;
2872
2873 op = g_hash_table_lookup(klass->properties, name);
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002874 g_free(op->description);
2875 op->description = g_strdup(description);
2876}
2877
Marc-André Lureau7439a032018-09-06 18:09:32 +04002878static void object_class_init(ObjectClass *klass, void *data)
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002879{
Markus Armbruster90c69fb2020-07-07 18:05:50 +02002880 object_class_property_add_str(klass, "type", object_get_type,
Markus Armbrusterd2623122020-05-05 17:29:22 +02002881 NULL);
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002882}
2883
Paolo Bonzini745549c2012-03-31 16:45:54 +02002884static void register_types(void)
2885{
Bernhard Beschow5e78c982022-01-17 15:58:04 +01002886 static const TypeInfo interface_info = {
Paolo Bonzini745549c2012-03-31 16:45:54 +02002887 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10002888 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02002889 .abstract = true,
2890 };
2891
Bernhard Beschow5e78c982022-01-17 15:58:04 +01002892 static const TypeInfo object_info = {
Paolo Bonzini745549c2012-03-31 16:45:54 +02002893 .name = TYPE_OBJECT,
2894 .instance_size = sizeof(Object),
Marc-André Lureau7439a032018-09-06 18:09:32 +04002895 .class_init = object_class_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02002896 .abstract = true,
2897 };
2898
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02002899 type_interface = type_register_internal(&interface_info);
2900 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02002901}
2902
2903type_init(register_types)