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"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "sysemu/block-backend.h"
19 #include "sysemu/blockdev.h"
20 #include "hw/block/block.h"
22 #include "qapi/visitor.h"
23 #include "chardev/char-fe.h"
24 #include "sysemu/iothread.h"
25 #include "sysemu/tpm_backend.h"
27 static void get_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
28 char *(*print
)(void *ptr
),
29 const char *name
, Error
**errp
)
31 DeviceState
*dev
= DEVICE(obj
);
32 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
35 p
= *ptr
? print(*ptr
) : g_strdup("");
36 visit_type_str(v
, name
, &p
, errp
);
40 static void set_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
41 void (*parse
)(DeviceState
*dev
, const char *str
,
42 void **ptr
, const char *propname
,
44 const char *name
, Error
**errp
)
46 DeviceState
*dev
= DEVICE(obj
);
47 Error
*local_err
= NULL
;
48 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
52 qdev_prop_set_after_realize(dev
, name
, errp
);
56 visit_type_str(v
, name
, &str
, &local_err
);
58 error_propagate(errp
, local_err
);
66 parse(dev
, str
, ptr
, prop
->name
, errp
);
72 static void parse_drive(DeviceState
*dev
, const char *str
, void **ptr
,
73 const char *propname
, Error
**errp
)
76 bool blk_created
= false;
79 blk
= blk_by_name(str
);
81 BlockDriverState
*bs
= bdrv_lookup_bs(NULL
, str
, NULL
);
83 blk
= blk_new(0, BLK_PERM_ALL
);
86 ret
= blk_insert_bs(blk
, bs
, errp
);
93 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
94 object_get_typename(OBJECT(dev
)), propname
, str
);
97 if (blk_attach_dev(blk
, dev
) < 0) {
98 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
100 if (dinfo
&& dinfo
->type
!= IF_NONE
) {
101 error_setg(errp
, "Drive '%s' is already in use because "
102 "it has been automatically connected to another "
103 "device (did you need 'if=none' in the drive options?)",
106 error_setg(errp
, "Drive '%s' is already in use by another device",
116 /* If we need to keep a reference, blk_attach_dev() took it */
121 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
123 DeviceState
*dev
= DEVICE(obj
);
124 Property
*prop
= opaque
;
125 BlockBackend
**ptr
= qdev_get_prop_ptr(dev
, prop
);
128 AioContext
*ctx
= blk_get_aio_context(*ptr
);
130 aio_context_acquire(ctx
);
131 blockdev_auto_del(*ptr
);
132 blk_detach_dev(*ptr
, dev
);
133 aio_context_release(ctx
);
137 static char *print_drive(void *ptr
)
141 name
= blk_name(ptr
);
143 BlockDriverState
*bs
= blk_bs(ptr
);
145 name
= bdrv_get_node_name(bs
);
148 return g_strdup(name
);
151 static void get_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
154 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
157 static void set_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
160 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
163 const PropertyInfo qdev_prop_drive
= {
165 .description
= "Node name or ID of a block device to use as a backend",
168 .release
= release_drive
,
171 /* --- character device --- */
173 static void get_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
176 DeviceState
*dev
= DEVICE(obj
);
177 CharBackend
*be
= qdev_get_prop_ptr(dev
, opaque
);
180 p
= g_strdup(be
->chr
&& be
->chr
->label
? be
->chr
->label
: "");
181 visit_type_str(v
, name
, &p
, errp
);
185 static void set_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
188 DeviceState
*dev
= DEVICE(obj
);
189 Error
*local_err
= NULL
;
190 Property
*prop
= opaque
;
191 CharBackend
*be
= qdev_get_prop_ptr(dev
, prop
);
196 qdev_prop_set_after_realize(dev
, name
, errp
);
200 visit_type_str(v
, name
, &str
, &local_err
);
202 error_propagate(errp
, local_err
);
212 s
= qemu_chr_find(str
);
214 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
215 object_get_typename(obj
), prop
->name
, str
);
216 } else if (!qemu_chr_fe_init(be
, s
, errp
)) {
217 error_prepend(errp
, "Property '%s.%s' can't take value '%s': ",
218 object_get_typename(obj
), prop
->name
, str
);
223 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
225 DeviceState
*dev
= DEVICE(obj
);
226 Property
*prop
= opaque
;
227 CharBackend
*be
= qdev_get_prop_ptr(dev
, prop
);
229 qemu_chr_fe_deinit(be
, false);
232 const PropertyInfo qdev_prop_chr
= {
234 .description
= "ID of a chardev to use as a backend",
237 .release
= release_chr
,
240 /* --- netdev device --- */
241 static void get_netdev(Object
*obj
, Visitor
*v
, const char *name
,
242 void *opaque
, Error
**errp
)
244 DeviceState
*dev
= DEVICE(obj
);
245 Property
*prop
= opaque
;
246 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
247 char *p
= g_strdup(peers_ptr
->ncs
[0] ? peers_ptr
->ncs
[0]->name
: "");
249 visit_type_str(v
, name
, &p
, errp
);
253 static void set_netdev(Object
*obj
, Visitor
*v
, const char *name
,
254 void *opaque
, Error
**errp
)
256 DeviceState
*dev
= DEVICE(obj
);
257 Property
*prop
= opaque
;
258 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
259 NetClientState
**ncs
= peers_ptr
->ncs
;
260 NetClientState
*peers
[MAX_QUEUE_NUM
];
261 Error
*local_err
= NULL
;
262 int queues
, err
= 0, i
= 0;
266 qdev_prop_set_after_realize(dev
, name
, errp
);
270 visit_type_str(v
, name
, &str
, &local_err
);
272 error_propagate(errp
, local_err
);
276 queues
= qemu_find_net_clients_except(str
, peers
,
277 NET_CLIENT_DRIVER_NIC
,
284 if (queues
> MAX_QUEUE_NUM
) {
285 error_setg(errp
, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
286 str
, queues
, MAX_QUEUE_NUM
);
290 for (i
= 0; i
< queues
; i
++) {
291 if (peers
[i
] == NULL
) {
296 if (peers
[i
]->peer
) {
307 ncs
[i
]->queue_index
= i
;
310 peers_ptr
->queues
= queues
;
313 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
317 const PropertyInfo qdev_prop_netdev
= {
319 .description
= "ID of a netdev to use as a backend",
326 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
328 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
332 if (!net_hub_id_for_client(*ptr
, &id
)) {
333 return snprintf(dest
, len
, "%d", id
);
337 return snprintf(dest
, len
, "<null>");
340 static void get_vlan(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
343 DeviceState
*dev
= DEVICE(obj
);
344 Property
*prop
= opaque
;
345 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
350 if (!net_hub_id_for_client(*ptr
, &hub_id
)) {
355 visit_type_int32(v
, name
, &id
, errp
);
358 static void set_vlan(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
361 DeviceState
*dev
= DEVICE(obj
);
362 Property
*prop
= opaque
;
363 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
364 NetClientState
**ptr
= &peers_ptr
->ncs
[0];
365 Error
*local_err
= NULL
;
367 NetClientState
*hubport
;
370 qdev_prop_set_after_realize(dev
, name
, errp
);
374 visit_type_int32(v
, name
, &id
, &local_err
);
376 error_propagate(errp
, local_err
);
384 error_set_from_qdev_prop_error(errp
, -EINVAL
, dev
, prop
, name
);
388 hubport
= net_hub_port_find(id
);
390 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
391 name
, prop
->info
->name
);
397 const PropertyInfo qdev_prop_vlan
= {
399 .description
= "Integer VLAN id to connect to",
405 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
406 BlockBackend
*value
, Error
**errp
)
408 const char *ref
= "";
411 ref
= blk_name(value
);
413 const BlockDriverState
*bs
= blk_bs(value
);
415 ref
= bdrv_get_node_name(bs
);
420 object_property_set_str(OBJECT(dev
), ref
, name
, errp
);
423 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
426 assert(!value
|| value
->label
);
427 object_property_set_str(OBJECT(dev
),
428 value
? value
->label
: "", name
, &error_abort
);
431 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
432 NetClientState
*value
)
434 assert(!value
|| value
->name
);
435 object_property_set_str(OBJECT(dev
),
436 value
? value
->name
: "", name
, &error_abort
);
439 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
441 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
443 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
445 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
446 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
447 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
449 nd
->instantiated
= 1;