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-common.h"
19 #include "qapi/visitor.h"
20 #include "char/char.h"
22 static void get_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
23 const char *(*print
)(void *ptr
),
24 const char *name
, Error
**errp
)
26 DeviceState
*dev
= DEVICE(obj
);
27 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
30 p
= (char *) (*ptr
? print(*ptr
) : "");
31 visit_type_str(v
, &p
, name
, errp
);
34 static void set_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
35 int (*parse
)(DeviceState
*dev
, const char *str
,
37 const char *name
, Error
**errp
)
39 DeviceState
*dev
= DEVICE(obj
);
40 Error
*local_err
= NULL
;
41 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
46 error_set(errp
, QERR_PERMISSION_DENIED
);
50 visit_type_str(v
, &str
, name
, &local_err
);
52 error_propagate(errp
, local_err
);
60 ret
= parse(dev
, str
, ptr
);
61 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, str
);
67 static int parse_drive(DeviceState
*dev
, const char *str
, void **ptr
)
75 if (bdrv_attach_dev(bs
, dev
) < 0) {
82 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
84 DeviceState
*dev
= DEVICE(obj
);
85 Property
*prop
= opaque
;
86 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
89 bdrv_detach_dev(*ptr
, dev
);
90 blockdev_auto_del(*ptr
);
94 static const char *print_drive(void *ptr
)
96 return bdrv_get_device_name(ptr
);
99 static void get_drive(Object
*obj
, Visitor
*v
, void *opaque
,
100 const char *name
, Error
**errp
)
102 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
105 static void set_drive(Object
*obj
, Visitor
*v
, void *opaque
,
106 const char *name
, Error
**errp
)
108 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
111 PropertyInfo qdev_prop_drive
= {
115 .release
= release_drive
,
118 /* --- character device --- */
120 static int parse_chr(DeviceState
*dev
, const char *str
, void **ptr
)
122 CharDriverState
*chr
= qemu_chr_find(str
);
126 if (chr
->avail_connections
< 1) {
130 --chr
->avail_connections
;
134 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
136 DeviceState
*dev
= DEVICE(obj
);
137 Property
*prop
= opaque
;
138 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
141 qemu_chr_add_handlers(*ptr
, NULL
, NULL
, NULL
, NULL
);
146 static const char *print_chr(void *ptr
)
148 CharDriverState
*chr
= ptr
;
150 return chr
->label
? chr
->label
: "";
153 static void get_chr(Object
*obj
, Visitor
*v
, void *opaque
,
154 const char *name
, Error
**errp
)
156 get_pointer(obj
, v
, opaque
, print_chr
, name
, errp
);
159 static void set_chr(Object
*obj
, Visitor
*v
, void *opaque
,
160 const char *name
, Error
**errp
)
162 set_pointer(obj
, v
, opaque
, parse_chr
, name
, errp
);
165 PropertyInfo qdev_prop_chr
= {
169 .release
= release_chr
,
172 /* --- netdev device --- */
174 static int parse_netdev(DeviceState
*dev
, const char *str
, void **ptr
)
176 NICPeers
*peers_ptr
= (NICPeers
*)ptr
;
177 NICConf
*conf
= container_of(peers_ptr
, NICConf
, peers
);
178 NetClientState
**ncs
= peers_ptr
->ncs
;
179 NetClientState
*peers
[MAX_QUEUE_NUM
];
183 queues
= qemu_find_net_clients_except(str
, peers
,
184 NET_CLIENT_OPTIONS_KIND_NIC
,
191 if (queues
> MAX_QUEUE_NUM
) {
196 for (i
= 0; i
< queues
; i
++) {
197 if (peers
[i
] == NULL
) {
202 if (peers
[i
]->peer
) {
208 ncs
[i
]->queue_index
= i
;
211 conf
->queues
= queues
;
219 static const char *print_netdev(void *ptr
)
221 NetClientState
*netdev
= ptr
;
223 return netdev
->name
? netdev
->name
: "";
226 static void get_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
227 const char *name
, Error
**errp
)
229 get_pointer(obj
, v
, opaque
, print_netdev
, name
, errp
);
232 static void set_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
233 const char *name
, Error
**errp
)
235 set_pointer(obj
, v
, opaque
, parse_netdev
, name
, errp
);
238 PropertyInfo qdev_prop_netdev
= {
246 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
248 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
252 if (!net_hub_id_for_client(*ptr
, &id
)) {
253 return snprintf(dest
, len
, "%d", id
);
257 return snprintf(dest
, len
, "<null>");
260 static void get_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
261 const char *name
, Error
**errp
)
263 DeviceState
*dev
= DEVICE(obj
);
264 Property
*prop
= opaque
;
265 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
270 if (!net_hub_id_for_client(*ptr
, &hub_id
)) {
275 visit_type_int32(v
, &id
, name
, errp
);
278 static void set_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
279 const char *name
, Error
**errp
)
281 DeviceState
*dev
= DEVICE(obj
);
282 Property
*prop
= opaque
;
283 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
284 NetClientState
**ptr
= &peers_ptr
->ncs
[0];
285 Error
*local_err
= NULL
;
287 NetClientState
*hubport
;
290 error_set(errp
, QERR_PERMISSION_DENIED
);
294 visit_type_int32(v
, &id
, name
, &local_err
);
296 error_propagate(errp
, local_err
);
304 hubport
= net_hub_port_find(id
);
306 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
307 name
, prop
->info
->name
);
313 PropertyInfo qdev_prop_vlan
= {
320 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
321 BlockDriverState
*value
)
324 const char *bdrv_name
= value
? bdrv_get_device_name(value
) : "";
325 object_property_set_str(OBJECT(dev
), bdrv_name
,
328 qerror_report_err(errp
);
335 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
,
336 BlockDriverState
*value
)
338 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
342 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
343 CharDriverState
*value
)
346 assert(!value
|| value
->label
);
347 object_property_set_str(OBJECT(dev
),
348 value
? value
->label
: "", name
, &errp
);
349 assert_no_error(errp
);
352 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
353 NetClientState
*value
)
356 assert(!value
|| value
->name
);
357 object_property_set_str(OBJECT(dev
),
358 value
? value
->name
: "", name
, &errp
);
359 assert_no_error(errp
);
362 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
364 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
366 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
368 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
369 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
370 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
372 nd
->instantiated
= 1;
375 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
379 g
= g_malloc0(sizeof(*g
));
380 g
->driver
= qemu_opt_get(opts
, "driver");
381 g
->property
= qemu_opt_get(opts
, "property");
382 g
->value
= qemu_opt_get(opts
, "value");
383 qdev_prop_register_global(g
);
387 void qemu_add_globals(void)
389 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);