2 * qdev property parsing
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.
13 #include "qemu/osdep.h"
14 #include "audio/audio.h"
16 #include "hw/qdev-properties.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qerror.h"
19 #include "sysemu/block-backend.h"
20 #include "sysemu/blockdev.h"
21 #include "hw/block/block.h"
23 #include "qapi/visitor.h"
24 #include "chardev/char-fe.h"
25 #include "sysemu/iothread.h"
26 #include "sysemu/tpm_backend.h"
28 static void get_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
29 char *(*print
)(void *ptr
),
30 const char *name
, Error
**errp
)
32 DeviceState
*dev
= DEVICE(obj
);
33 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
36 p
= *ptr
? print(*ptr
) : g_strdup("");
37 visit_type_str(v
, name
, &p
, errp
);
41 static void set_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
42 void (*parse
)(DeviceState
*dev
, const char *str
,
43 void **ptr
, const char *propname
,
45 const char *name
, Error
**errp
)
47 DeviceState
*dev
= DEVICE(obj
);
48 Error
*local_err
= NULL
;
49 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
53 qdev_prop_set_after_realize(dev
, name
, errp
);
57 visit_type_str(v
, name
, &str
, &local_err
);
59 error_propagate(errp
, local_err
);
67 parse(dev
, str
, ptr
, prop
->name
, errp
);
73 static void do_parse_drive(DeviceState
*dev
, const char *str
, void **ptr
,
74 const char *propname
, bool iothread
, Error
**errp
)
77 bool blk_created
= false;
80 blk
= blk_by_name(str
);
82 BlockDriverState
*bs
= bdrv_lookup_bs(NULL
, str
, NULL
);
85 * If the device supports iothreads, it will make sure to move the
86 * block node to the right AioContext if necessary (or fail if this
87 * isn't possible because of other users). Devices that are not
88 * aware of iothreads require their BlockBackends to be in the main
91 AioContext
*ctx
= iothread
? bdrv_get_aio_context(bs
) :
92 qemu_get_aio_context();
93 blk
= blk_new(ctx
, 0, BLK_PERM_ALL
);
96 ret
= blk_insert_bs(blk
, bs
, errp
);
103 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
104 object_get_typename(OBJECT(dev
)), propname
, str
);
107 if (blk_attach_dev(blk
, dev
) < 0) {
108 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
110 if (dinfo
&& dinfo
->type
!= IF_NONE
) {
111 error_setg(errp
, "Drive '%s' is already in use because "
112 "it has been automatically connected to another "
113 "device (did you need 'if=none' in the drive options?)",
116 error_setg(errp
, "Drive '%s' is already in use by another device",
126 /* If we need to keep a reference, blk_attach_dev() took it */
131 static void parse_drive(DeviceState
*dev
, const char *str
, void **ptr
,
132 const char *propname
, Error
**errp
)
134 do_parse_drive(dev
, str
, ptr
, propname
, false, errp
);
137 static void parse_drive_iothread(DeviceState
*dev
, const char *str
, void **ptr
,
138 const char *propname
, Error
**errp
)
140 do_parse_drive(dev
, str
, ptr
, propname
, true, errp
);
143 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
145 DeviceState
*dev
= DEVICE(obj
);
146 Property
*prop
= opaque
;
147 BlockBackend
**ptr
= qdev_get_prop_ptr(dev
, prop
);
150 AioContext
*ctx
= blk_get_aio_context(*ptr
);
152 aio_context_acquire(ctx
);
153 blockdev_auto_del(*ptr
);
154 blk_detach_dev(*ptr
, dev
);
155 aio_context_release(ctx
);
159 static char *print_drive(void *ptr
)
163 name
= blk_name(ptr
);
165 BlockDriverState
*bs
= blk_bs(ptr
);
167 name
= bdrv_get_node_name(bs
);
170 return g_strdup(name
);
173 static void get_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
176 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
179 static void set_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
182 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
185 static void set_drive_iothread(Object
*obj
, Visitor
*v
, const char *name
,
186 void *opaque
, Error
**errp
)
188 set_pointer(obj
, v
, opaque
, parse_drive_iothread
, name
, errp
);
191 const PropertyInfo qdev_prop_drive
= {
193 .description
= "Node name or ID of a block device to use as a backend",
196 .release
= release_drive
,
199 const PropertyInfo qdev_prop_drive_iothread
= {
201 .description
= "Node name or ID of a block device to use as a backend",
203 .set
= set_drive_iothread
,
204 .release
= release_drive
,
207 /* --- character device --- */
209 static void get_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
212 DeviceState
*dev
= DEVICE(obj
);
213 CharBackend
*be
= qdev_get_prop_ptr(dev
, opaque
);
216 p
= g_strdup(be
->chr
&& be
->chr
->label
? be
->chr
->label
: "");
217 visit_type_str(v
, name
, &p
, errp
);
221 static void set_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
224 DeviceState
*dev
= DEVICE(obj
);
225 Error
*local_err
= NULL
;
226 Property
*prop
= opaque
;
227 CharBackend
*be
= qdev_get_prop_ptr(dev
, prop
);
232 qdev_prop_set_after_realize(dev
, name
, errp
);
236 visit_type_str(v
, name
, &str
, &local_err
);
238 error_propagate(errp
, local_err
);
248 s
= qemu_chr_find(str
);
250 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
251 object_get_typename(obj
), prop
->name
, str
);
252 } else if (!qemu_chr_fe_init(be
, s
, errp
)) {
253 error_prepend(errp
, "Property '%s.%s' can't take value '%s': ",
254 object_get_typename(obj
), prop
->name
, str
);
259 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
261 DeviceState
*dev
= DEVICE(obj
);
262 Property
*prop
= opaque
;
263 CharBackend
*be
= qdev_get_prop_ptr(dev
, prop
);
265 qemu_chr_fe_deinit(be
, false);
268 const PropertyInfo qdev_prop_chr
= {
270 .description
= "ID of a chardev to use as a backend",
273 .release
= release_chr
,
276 /* --- netdev device --- */
277 static void get_netdev(Object
*obj
, Visitor
*v
, const char *name
,
278 void *opaque
, Error
**errp
)
280 DeviceState
*dev
= DEVICE(obj
);
281 Property
*prop
= opaque
;
282 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
283 char *p
= g_strdup(peers_ptr
->ncs
[0] ? peers_ptr
->ncs
[0]->name
: "");
285 visit_type_str(v
, name
, &p
, errp
);
289 static void set_netdev(Object
*obj
, Visitor
*v
, const char *name
,
290 void *opaque
, Error
**errp
)
292 DeviceState
*dev
= DEVICE(obj
);
293 Property
*prop
= opaque
;
294 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
295 NetClientState
**ncs
= peers_ptr
->ncs
;
296 NetClientState
*peers
[MAX_QUEUE_NUM
];
297 Error
*local_err
= NULL
;
298 int queues
, err
= 0, i
= 0;
302 qdev_prop_set_after_realize(dev
, name
, errp
);
306 visit_type_str(v
, name
, &str
, &local_err
);
308 error_propagate(errp
, local_err
);
312 queues
= qemu_find_net_clients_except(str
, peers
,
313 NET_CLIENT_DRIVER_NIC
,
320 if (queues
> MAX_QUEUE_NUM
) {
321 error_setg(errp
, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
322 str
, queues
, MAX_QUEUE_NUM
);
326 for (i
= 0; i
< queues
; i
++) {
328 if (peers
[i
]->peer
) {
339 ncs
[i
]->queue_index
= i
;
342 peers_ptr
->queues
= queues
;
345 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
349 const PropertyInfo qdev_prop_netdev
= {
351 .description
= "ID of a netdev to use as a backend",
357 /* --- audiodev --- */
358 static void get_audiodev(Object
*obj
, Visitor
*v
, const char* name
,
359 void *opaque
, Error
**errp
)
361 DeviceState
*dev
= DEVICE(obj
);
362 Property
*prop
= opaque
;
363 QEMUSoundCard
*card
= qdev_get_prop_ptr(dev
, prop
);
364 char *p
= g_strdup(audio_get_id(card
));
366 visit_type_str(v
, name
, &p
, errp
);
370 static void set_audiodev(Object
*obj
, Visitor
*v
, const char* name
,
371 void *opaque
, Error
**errp
)
373 DeviceState
*dev
= DEVICE(obj
);
374 Property
*prop
= opaque
;
375 QEMUSoundCard
*card
= qdev_get_prop_ptr(dev
, prop
);
377 Error
*local_err
= NULL
;
382 qdev_prop_set_after_realize(dev
, name
, errp
);
386 visit_type_str(v
, name
, &str
, &local_err
);
388 error_propagate(errp
, local_err
);
392 state
= audio_state_by_name(str
);
401 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
405 const PropertyInfo qdev_prop_audiodev
= {
407 .description
= "ID of an audiodev to use as a backend",
408 /* release done on shutdown */
413 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
414 BlockBackend
*value
, Error
**errp
)
416 const char *ref
= "";
419 ref
= blk_name(value
);
421 const BlockDriverState
*bs
= blk_bs(value
);
423 ref
= bdrv_get_node_name(bs
);
428 object_property_set_str(OBJECT(dev
), ref
, name
, errp
);
431 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
434 assert(!value
|| value
->label
);
435 object_property_set_str(OBJECT(dev
),
436 value
? value
->label
: "", name
, &error_abort
);
439 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
440 NetClientState
*value
)
442 assert(!value
|| value
->name
);
443 object_property_set_str(OBJECT(dev
),
444 value
? value
->name
: "", name
, &error_abort
);
447 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
449 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
451 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
453 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
454 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
455 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
457 nd
->instantiated
= 1;