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 bool check_prop_still_unset(DeviceState
*dev
, const char *name
,
29 const void *old_val
, const char *new_val
,
32 const GlobalProperty
*prop
= qdev_find_global_prop(dev
, name
);
39 error_setg(errp
, "-global %s.%s=... conflicts with %s=%s",
40 prop
->driver
, prop
->property
, name
, new_val
);
42 /* Error message is vague, but a better one would be hard */
43 error_setg(errp
, "%s=%s conflicts, and override is not implemented",
52 static void get_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
55 DeviceState
*dev
= DEVICE(obj
);
56 Property
*prop
= opaque
;
57 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
62 value
= blk_name(*ptr
);
64 BlockDriverState
*bs
= blk_bs(*ptr
);
66 value
= bdrv_get_node_name(bs
);
74 visit_type_str(v
, name
, &p
, errp
);
78 static void set_drive_helper(Object
*obj
, Visitor
*v
, const char *name
,
79 void *opaque
, bool iothread
, Error
**errp
)
81 DeviceState
*dev
= DEVICE(obj
);
82 Property
*prop
= opaque
;
83 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
86 bool blk_created
= false;
90 qdev_prop_set_after_realize(dev
, name
, errp
);
94 if (!visit_type_str(v
, name
, &str
, errp
)) {
99 * TODO Should this really be an error? If no, the old value
100 * needs to be released before we store the new one.
102 if (!check_prop_still_unset(dev
, name
, *ptr
, str
, errp
)) {
112 blk
= blk_by_name(str
);
114 BlockDriverState
*bs
= bdrv_lookup_bs(NULL
, str
, NULL
);
117 * If the device supports iothreads, it will make sure to move the
118 * block node to the right AioContext if necessary (or fail if this
119 * isn't possible because of other users). Devices that are not
120 * aware of iothreads require their BlockBackends to be in the main
123 AioContext
*ctx
= iothread
? bdrv_get_aio_context(bs
) :
124 qemu_get_aio_context();
125 blk
= blk_new(ctx
, 0, BLK_PERM_ALL
);
128 ret
= blk_insert_bs(blk
, bs
, errp
);
135 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
136 object_get_typename(OBJECT(dev
)), prop
->name
, str
);
139 if (blk_attach_dev(blk
, dev
) < 0) {
140 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
142 if (dinfo
&& dinfo
->type
!= IF_NONE
) {
143 error_setg(errp
, "Drive '%s' is already in use because "
144 "it has been automatically connected to another "
145 "device (did you need 'if=none' in the drive options?)",
148 error_setg(errp
, "Drive '%s' is already in use by another device",
158 /* If we need to keep a reference, blk_attach_dev() took it */
165 static void set_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
168 set_drive_helper(obj
, v
, name
, opaque
, false, errp
);
171 static void set_drive_iothread(Object
*obj
, Visitor
*v
, const char *name
,
172 void *opaque
, Error
**errp
)
174 set_drive_helper(obj
, v
, name
, opaque
, true, errp
);
177 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
179 DeviceState
*dev
= DEVICE(obj
);
180 Property
*prop
= opaque
;
181 BlockBackend
**ptr
= qdev_get_prop_ptr(dev
, prop
);
184 AioContext
*ctx
= blk_get_aio_context(*ptr
);
186 aio_context_acquire(ctx
);
187 blockdev_auto_del(*ptr
);
188 blk_detach_dev(*ptr
, dev
);
189 aio_context_release(ctx
);
193 const PropertyInfo qdev_prop_drive
= {
195 .description
= "Node name or ID of a block device to use as a backend",
198 .release
= release_drive
,
201 const PropertyInfo qdev_prop_drive_iothread
= {
203 .description
= "Node name or ID of a block device to use as a backend",
205 .set
= set_drive_iothread
,
206 .release
= release_drive
,
209 /* --- character device --- */
211 static void get_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
214 DeviceState
*dev
= DEVICE(obj
);
215 CharBackend
*be
= qdev_get_prop_ptr(dev
, opaque
);
218 p
= g_strdup(be
->chr
&& be
->chr
->label
? be
->chr
->label
: "");
219 visit_type_str(v
, name
, &p
, errp
);
223 static void set_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
226 DeviceState
*dev
= DEVICE(obj
);
227 Property
*prop
= opaque
;
228 CharBackend
*be
= qdev_get_prop_ptr(dev
, prop
);
233 qdev_prop_set_after_realize(dev
, name
, errp
);
237 if (!visit_type_str(v
, name
, &str
, errp
)) {
242 * TODO Should this really be an error? If no, the old value
243 * needs to be released before we store the new one.
245 if (!check_prop_still_unset(dev
, name
, be
->chr
, str
, errp
)) {
255 s
= qemu_chr_find(str
);
257 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
258 object_get_typename(obj
), prop
->name
, str
);
259 } else if (!qemu_chr_fe_init(be
, s
, errp
)) {
260 error_prepend(errp
, "Property '%s.%s' can't take value '%s': ",
261 object_get_typename(obj
), prop
->name
, str
);
266 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
268 DeviceState
*dev
= DEVICE(obj
);
269 Property
*prop
= opaque
;
270 CharBackend
*be
= qdev_get_prop_ptr(dev
, prop
);
272 qemu_chr_fe_deinit(be
, false);
275 const PropertyInfo qdev_prop_chr
= {
277 .description
= "ID of a chardev to use as a backend",
280 .release
= release_chr
,
283 /* --- netdev device --- */
284 static void get_netdev(Object
*obj
, Visitor
*v
, const char *name
,
285 void *opaque
, Error
**errp
)
287 DeviceState
*dev
= DEVICE(obj
);
288 Property
*prop
= opaque
;
289 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
290 char *p
= g_strdup(peers_ptr
->ncs
[0] ? peers_ptr
->ncs
[0]->name
: "");
292 visit_type_str(v
, name
, &p
, errp
);
296 static void set_netdev(Object
*obj
, Visitor
*v
, const char *name
,
297 void *opaque
, Error
**errp
)
299 DeviceState
*dev
= DEVICE(obj
);
300 Property
*prop
= opaque
;
301 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
302 NetClientState
**ncs
= peers_ptr
->ncs
;
303 NetClientState
*peers
[MAX_QUEUE_NUM
];
304 int queues
, err
= 0, i
= 0;
308 qdev_prop_set_after_realize(dev
, name
, errp
);
312 if (!visit_type_str(v
, name
, &str
, errp
)) {
316 queues
= qemu_find_net_clients_except(str
, peers
,
317 NET_CLIENT_DRIVER_NIC
,
324 if (queues
> MAX_QUEUE_NUM
) {
325 error_setg(errp
, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
326 str
, queues
, MAX_QUEUE_NUM
);
330 for (i
= 0; i
< queues
; i
++) {
331 if (peers
[i
]->peer
) {
337 * TODO Should this really be an error? If no, the old value
338 * needs to be released before we store the new one.
340 if (!check_prop_still_unset(dev
, name
, ncs
[i
], str
, errp
)) {
345 ncs
[i
]->queue_index
= i
;
348 peers_ptr
->queues
= queues
;
351 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
355 const PropertyInfo qdev_prop_netdev
= {
357 .description
= "ID of a netdev to use as a backend",
363 /* --- audiodev --- */
364 static void get_audiodev(Object
*obj
, Visitor
*v
, const char* name
,
365 void *opaque
, Error
**errp
)
367 DeviceState
*dev
= DEVICE(obj
);
368 Property
*prop
= opaque
;
369 QEMUSoundCard
*card
= qdev_get_prop_ptr(dev
, prop
);
370 char *p
= g_strdup(audio_get_id(card
));
372 visit_type_str(v
, name
, &p
, errp
);
376 static void set_audiodev(Object
*obj
, Visitor
*v
, const char* name
,
377 void *opaque
, Error
**errp
)
379 DeviceState
*dev
= DEVICE(obj
);
380 Property
*prop
= opaque
;
381 QEMUSoundCard
*card
= qdev_get_prop_ptr(dev
, prop
);
387 qdev_prop_set_after_realize(dev
, name
, errp
);
391 if (!visit_type_str(v
, name
, &str
, errp
)) {
395 state
= audio_state_by_name(str
);
404 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
408 const PropertyInfo qdev_prop_audiodev
= {
410 .description
= "ID of an audiodev to use as a backend",
411 /* release done on shutdown */
416 bool qdev_prop_set_drive_err(DeviceState
*dev
, const char *name
,
417 BlockBackend
*value
, Error
**errp
)
419 const char *ref
= "";
422 ref
= blk_name(value
);
424 const BlockDriverState
*bs
= blk_bs(value
);
426 ref
= bdrv_get_node_name(bs
);
431 return object_property_set_str(OBJECT(dev
), name
, ref
, errp
);
434 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
437 qdev_prop_set_drive_err(dev
, name
, value
, &error_abort
);
440 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
443 assert(!value
|| value
->label
);
444 object_property_set_str(OBJECT(dev
), name
, value
? value
->label
: "",
448 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
449 NetClientState
*value
)
451 assert(!value
|| value
->name
);
452 object_property_set_str(OBJECT(dev
), name
, value
? value
->name
: "",
456 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
458 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
460 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
462 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
463 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
464 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
466 nd
->instantiated
= 1;