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.
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 "sysemu/char.h"
24 #include "sysemu/iothread.h"
26 static void get_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
27 char *(*print
)(void *ptr
),
28 const char *name
, Error
**errp
)
30 DeviceState
*dev
= DEVICE(obj
);
31 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
34 p
= *ptr
? print(*ptr
) : g_strdup("");
35 visit_type_str(v
, name
, &p
, errp
);
39 static void set_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
40 void (*parse
)(DeviceState
*dev
, const char *str
,
41 void **ptr
, const char *propname
,
43 const char *name
, Error
**errp
)
45 DeviceState
*dev
= DEVICE(obj
);
46 Error
*local_err
= NULL
;
47 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
51 qdev_prop_set_after_realize(dev
, name
, errp
);
55 visit_type_str(v
, name
, &str
, &local_err
);
57 error_propagate(errp
, local_err
);
65 parse(dev
, str
, ptr
, prop
->name
, errp
);
71 static void parse_drive(DeviceState
*dev
, const char *str
, void **ptr
,
72 const char *propname
, Error
**errp
)
76 blk
= blk_by_name(str
);
78 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
79 object_get_typename(OBJECT(dev
)), propname
, str
);
82 if (blk_attach_dev(blk
, dev
) < 0) {
83 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
85 if (dinfo
->type
!= IF_NONE
) {
86 error_setg(errp
, "Drive '%s' is already in use because "
87 "it has been automatically connected to another "
88 "device (did you need 'if=none' in the drive options?)",
91 error_setg(errp
, "Drive '%s' is already in use by another device",
99 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
101 DeviceState
*dev
= DEVICE(obj
);
102 Property
*prop
= opaque
;
103 BlockBackend
**ptr
= qdev_get_prop_ptr(dev
, prop
);
106 blk_detach_dev(*ptr
, dev
);
107 blockdev_auto_del(*ptr
);
111 static char *print_drive(void *ptr
)
113 return g_strdup(blk_name(ptr
));
116 static void get_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
119 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
122 static void set_drive(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
125 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
128 PropertyInfo qdev_prop_drive
= {
130 .description
= "ID of a drive to use as a backend",
133 .release
= release_drive
,
136 /* --- character device --- */
138 static void parse_chr(DeviceState
*dev
, const char *str
, void **ptr
,
139 const char *propname
, Error
**errp
)
141 CharDriverState
*chr
= qemu_chr_find(str
);
143 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
144 object_get_typename(OBJECT(dev
)), propname
, str
);
147 if (qemu_chr_fe_claim(chr
) != 0) {
148 error_setg(errp
, "Property '%s.%s' can't take value '%s', it's in use",
149 object_get_typename(OBJECT(dev
)), propname
, str
);
155 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
157 DeviceState
*dev
= DEVICE(obj
);
158 Property
*prop
= opaque
;
159 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
160 CharDriverState
*chr
= *ptr
;
163 qemu_chr_add_handlers(chr
, NULL
, NULL
, NULL
, NULL
);
164 qemu_chr_fe_release(chr
);
169 static char *print_chr(void *ptr
)
171 CharDriverState
*chr
= ptr
;
172 const char *val
= chr
->label
? chr
->label
: "";
174 return g_strdup(val
);
177 static void get_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
180 get_pointer(obj
, v
, opaque
, print_chr
, name
, errp
);
183 static void set_chr(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
186 set_pointer(obj
, v
, opaque
, parse_chr
, name
, errp
);
189 PropertyInfo qdev_prop_chr
= {
191 .description
= "ID of a chardev to use as a backend",
194 .release
= release_chr
,
197 /* --- netdev device --- */
198 static void get_netdev(Object
*obj
, Visitor
*v
, const char *name
,
199 void *opaque
, Error
**errp
)
201 DeviceState
*dev
= DEVICE(obj
);
202 Property
*prop
= opaque
;
203 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
204 char *p
= g_strdup(peers_ptr
->ncs
[0] ? peers_ptr
->ncs
[0]->name
: "");
206 visit_type_str(v
, name
, &p
, errp
);
210 static void set_netdev(Object
*obj
, Visitor
*v
, const char *name
,
211 void *opaque
, Error
**errp
)
213 DeviceState
*dev
= DEVICE(obj
);
214 Property
*prop
= opaque
;
215 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
216 NetClientState
**ncs
= peers_ptr
->ncs
;
217 NetClientState
*peers
[MAX_QUEUE_NUM
];
218 Error
*local_err
= NULL
;
219 int queues
, err
= 0, i
= 0;
223 qdev_prop_set_after_realize(dev
, name
, errp
);
227 visit_type_str(v
, name
, &str
, &local_err
);
229 error_propagate(errp
, local_err
);
233 queues
= qemu_find_net_clients_except(str
, peers
,
234 NET_CLIENT_OPTIONS_KIND_NIC
,
241 if (queues
> MAX_QUEUE_NUM
) {
242 error_setg(errp
, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
243 str
, queues
, MAX_QUEUE_NUM
);
247 for (i
= 0; i
< queues
; i
++) {
248 if (peers
[i
] == NULL
) {
253 if (peers
[i
]->peer
) {
264 ncs
[i
]->queue_index
= i
;
267 peers_ptr
->queues
= queues
;
270 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
274 PropertyInfo qdev_prop_netdev
= {
276 .description
= "ID of a netdev to use as a backend",
283 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
285 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
289 if (!net_hub_id_for_client(*ptr
, &id
)) {
290 return snprintf(dest
, len
, "%d", id
);
294 return snprintf(dest
, len
, "<null>");
297 static void get_vlan(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
300 DeviceState
*dev
= DEVICE(obj
);
301 Property
*prop
= opaque
;
302 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
307 if (!net_hub_id_for_client(*ptr
, &hub_id
)) {
312 visit_type_int32(v
, name
, &id
, errp
);
315 static void set_vlan(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
318 DeviceState
*dev
= DEVICE(obj
);
319 Property
*prop
= opaque
;
320 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
321 NetClientState
**ptr
= &peers_ptr
->ncs
[0];
322 Error
*local_err
= NULL
;
324 NetClientState
*hubport
;
327 qdev_prop_set_after_realize(dev
, name
, errp
);
331 visit_type_int32(v
, name
, &id
, &local_err
);
333 error_propagate(errp
, local_err
);
341 error_set_from_qdev_prop_error(errp
, -EINVAL
, dev
, prop
, name
);
345 hubport
= net_hub_port_find(id
);
347 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
348 name
, prop
->info
->name
);
354 PropertyInfo qdev_prop_vlan
= {
356 .description
= "Integer VLAN id to connect to",
362 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
363 BlockBackend
*value
, Error
**errp
)
365 object_property_set_str(OBJECT(dev
), value
? blk_name(value
) : "",
369 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
370 CharDriverState
*value
)
372 assert(!value
|| value
->label
);
373 object_property_set_str(OBJECT(dev
),
374 value
? value
->label
: "", name
, &error_abort
);
377 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
378 NetClientState
*value
)
380 assert(!value
|| value
->name
);
381 object_property_set_str(OBJECT(dev
),
382 value
? value
->name
: "", name
, &error_abort
);
385 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
387 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
389 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
391 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
392 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
393 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
395 nd
->instantiated
= 1;
398 static int qdev_add_one_global(void *opaque
, QemuOpts
*opts
, Error
**errp
)
402 g
= g_malloc0(sizeof(*g
));
403 g
->driver
= qemu_opt_get(opts
, "driver");
404 g
->property
= qemu_opt_get(opts
, "property");
405 g
->value
= qemu_opt_get(opts
, "value");
406 g
->user_provided
= true;
407 qdev_prop_register_global(g
);
411 void qemu_add_globals(void)
413 qemu_opts_foreach(qemu_find_opts("global"),
414 qdev_add_one_global
, NULL
, NULL
);