2 * qdev property parsing and global properties
3 * (parts specific for qemu-system-*)
5 * This file is based on code from hw/qdev-properties.c from
6 * commit 074a86fccd185616469dfcdc0e157f438aebba18,
7 * Copyright (c) Gerd Hoffmann <kraxel@redhat.com> and other contributors.
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.
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/blockdev.h"
17 #include "hw/block/block.h"
19 #include "qapi/visitor.h"
20 #include "sysemu/char.h"
21 #include "sysemu/iothread.h"
23 static void get_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
24 char *(*print
)(void *ptr
),
25 const char *name
, Error
**errp
)
27 DeviceState
*dev
= DEVICE(obj
);
28 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
31 p
= *ptr
? print(*ptr
) : g_strdup("");
32 visit_type_str(v
, &p
, name
, errp
);
36 static void set_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
37 int (*parse
)(DeviceState
*dev
, const char *str
,
39 const char *name
, Error
**errp
)
41 DeviceState
*dev
= DEVICE(obj
);
42 Error
*local_err
= NULL
;
43 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
48 qdev_prop_set_after_realize(dev
, name
, errp
);
52 visit_type_str(v
, &str
, name
, &local_err
);
54 error_propagate(errp
, local_err
);
62 ret
= parse(dev
, str
, ptr
);
63 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, str
);
69 static int parse_drive(DeviceState
*dev
, const char *str
, void **ptr
)
77 if (bdrv_attach_dev(bs
, dev
) < 0) {
84 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
86 DeviceState
*dev
= DEVICE(obj
);
87 Property
*prop
= opaque
;
88 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
91 bdrv_detach_dev(*ptr
, dev
);
92 blockdev_auto_del(*ptr
);
96 static char *print_drive(void *ptr
)
98 return g_strdup(bdrv_get_device_name(ptr
));
101 static void get_drive(Object
*obj
, Visitor
*v
, void *opaque
,
102 const char *name
, Error
**errp
)
104 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
107 static void set_drive(Object
*obj
, Visitor
*v
, void *opaque
,
108 const char *name
, Error
**errp
)
110 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
113 PropertyInfo qdev_prop_drive
= {
115 .legacy_name
= "drive",
118 .release
= release_drive
,
121 /* --- character device --- */
123 static int parse_chr(DeviceState
*dev
, const char *str
, void **ptr
)
125 CharDriverState
*chr
= qemu_chr_find(str
);
129 if (qemu_chr_fe_claim(chr
) != 0) {
136 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
138 DeviceState
*dev
= DEVICE(obj
);
139 Property
*prop
= opaque
;
140 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
141 CharDriverState
*chr
= *ptr
;
144 qemu_chr_add_handlers(chr
, NULL
, NULL
, NULL
, NULL
);
145 qemu_chr_fe_release(chr
);
150 static char *print_chr(void *ptr
)
152 CharDriverState
*chr
= ptr
;
153 const char *val
= chr
->label
? chr
->label
: "";
155 return g_strdup(val
);
158 static void get_chr(Object
*obj
, Visitor
*v
, void *opaque
,
159 const char *name
, Error
**errp
)
161 get_pointer(obj
, v
, opaque
, print_chr
, name
, errp
);
164 static void set_chr(Object
*obj
, Visitor
*v
, void *opaque
,
165 const char *name
, Error
**errp
)
167 set_pointer(obj
, v
, opaque
, parse_chr
, name
, errp
);
170 PropertyInfo qdev_prop_chr
= {
172 .legacy_name
= "chr",
175 .release
= release_chr
,
178 /* --- netdev device --- */
180 static int parse_netdev(DeviceState
*dev
, const char *str
, void **ptr
)
182 NICPeers
*peers_ptr
= (NICPeers
*)ptr
;
183 NetClientState
**ncs
= peers_ptr
->ncs
;
184 NetClientState
*peers
[MAX_QUEUE_NUM
];
188 queues
= qemu_find_net_clients_except(str
, peers
,
189 NET_CLIENT_OPTIONS_KIND_NIC
,
196 if (queues
> MAX_QUEUE_NUM
) {
201 for (i
= 0; i
< queues
; i
++) {
202 if (peers
[i
] == NULL
) {
207 if (peers
[i
]->peer
) {
218 ncs
[i
]->queue_index
= i
;
221 peers_ptr
->queues
= queues
;
229 static char *print_netdev(void *ptr
)
231 NetClientState
*netdev
= ptr
;
232 const char *val
= netdev
->name
? netdev
->name
: "";
234 return g_strdup(val
);
237 static void get_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
238 const char *name
, Error
**errp
)
240 get_pointer(obj
, v
, opaque
, print_netdev
, name
, errp
);
243 static void set_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
244 const char *name
, Error
**errp
)
246 set_pointer(obj
, v
, opaque
, parse_netdev
, name
, errp
);
249 PropertyInfo qdev_prop_netdev
= {
251 .legacy_name
= "netdev",
258 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
260 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
264 if (!net_hub_id_for_client(*ptr
, &id
)) {
265 return snprintf(dest
, len
, "%d", id
);
269 return snprintf(dest
, len
, "<null>");
272 static void get_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
273 const char *name
, Error
**errp
)
275 DeviceState
*dev
= DEVICE(obj
);
276 Property
*prop
= opaque
;
277 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
282 if (!net_hub_id_for_client(*ptr
, &hub_id
)) {
287 visit_type_int32(v
, &id
, name
, errp
);
290 static void set_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
291 const char *name
, Error
**errp
)
293 DeviceState
*dev
= DEVICE(obj
);
294 Property
*prop
= opaque
;
295 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
296 NetClientState
**ptr
= &peers_ptr
->ncs
[0];
297 Error
*local_err
= NULL
;
299 NetClientState
*hubport
;
302 qdev_prop_set_after_realize(dev
, name
, errp
);
306 visit_type_int32(v
, &id
, name
, &local_err
);
308 error_propagate(errp
, local_err
);
316 error_set_from_qdev_prop_error(errp
, -EINVAL
, dev
, prop
, name
);
320 hubport
= net_hub_port_find(id
);
322 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
323 name
, prop
->info
->name
);
329 PropertyInfo qdev_prop_vlan
= {
331 .legacy_name
= "vlan",
337 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
338 BlockDriverState
*value
)
341 const char *bdrv_name
= value
? bdrv_get_device_name(value
) : "";
342 object_property_set_str(OBJECT(dev
), bdrv_name
,
345 qerror_report_err(err
);
352 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
,
353 BlockDriverState
*value
)
355 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
359 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
360 CharDriverState
*value
)
362 assert(!value
|| value
->label
);
363 object_property_set_str(OBJECT(dev
),
364 value
? value
->label
: "", name
, &error_abort
);
367 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
368 NetClientState
*value
)
370 assert(!value
|| value
->name
);
371 object_property_set_str(OBJECT(dev
),
372 value
? value
->name
: "", name
, &error_abort
);
375 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
377 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
379 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
381 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
382 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
383 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
385 nd
->instantiated
= 1;
388 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
393 g
= g_malloc0(sizeof(*g
));
394 g
->driver
= qemu_opt_get(opts
, "driver");
395 g
->property
= qemu_opt_get(opts
, "property");
396 g
->value
= qemu_opt_get(opts
, "value");
397 oc
= object_class_dynamic_cast(object_class_by_name(g
->driver
),
400 DeviceClass
*dc
= DEVICE_CLASS(oc
);
402 if (dc
->hotpluggable
) {
403 /* If hotpluggable then skip not_used checking. */
413 qdev_prop_register_global(g
);
417 void qemu_add_globals(void)
419 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);