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/block-backend.h"
17 #include "sysemu/blockdev.h"
18 #include "hw/block/block.h"
20 #include "qapi/visitor.h"
21 #include "sysemu/char.h"
22 #include "sysemu/iothread.h"
24 static void get_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
25 char *(*print
)(void *ptr
),
26 const char *name
, Error
**errp
)
28 DeviceState
*dev
= DEVICE(obj
);
29 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
32 p
= *ptr
? print(*ptr
) : g_strdup("");
33 visit_type_str(v
, &p
, name
, errp
);
37 static void set_pointer(Object
*obj
, Visitor
*v
, Property
*prop
,
38 void (*parse
)(DeviceState
*dev
, const char *str
,
39 void **ptr
, const char *propname
,
41 const char *name
, Error
**errp
)
43 DeviceState
*dev
= DEVICE(obj
);
44 Error
*local_err
= NULL
;
45 void **ptr
= qdev_get_prop_ptr(dev
, prop
);
49 qdev_prop_set_after_realize(dev
, name
, errp
);
53 visit_type_str(v
, &str
, name
, &local_err
);
55 error_propagate(errp
, local_err
);
63 parse(dev
, str
, ptr
, prop
->name
, errp
);
69 static void parse_drive(DeviceState
*dev
, const char *str
, void **ptr
,
70 const char *propname
, Error
**errp
)
74 blk
= blk_by_name(str
);
76 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
77 object_get_typename(OBJECT(dev
)), propname
, str
);
80 if (blk_attach_dev(blk
, dev
) < 0) {
81 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
83 if (dinfo
->type
!= IF_NONE
) {
84 error_setg(errp
, "Drive '%s' is already in use because "
85 "it has been automatically connected to another "
86 "device (did you need 'if=none' in the drive options?)",
89 error_setg(errp
, "Drive '%s' is already in use by another device",
97 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
99 DeviceState
*dev
= DEVICE(obj
);
100 Property
*prop
= opaque
;
101 BlockBackend
**ptr
= qdev_get_prop_ptr(dev
, prop
);
104 blk_detach_dev(*ptr
, dev
);
105 blockdev_auto_del(*ptr
);
109 static char *print_drive(void *ptr
)
111 return g_strdup(blk_name(ptr
));
114 static void get_drive(Object
*obj
, Visitor
*v
, void *opaque
,
115 const char *name
, Error
**errp
)
117 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
120 static void set_drive(Object
*obj
, Visitor
*v
, void *opaque
,
121 const char *name
, Error
**errp
)
123 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
126 PropertyInfo qdev_prop_drive
= {
128 .description
= "ID of a drive to use as a backend",
131 .release
= release_drive
,
134 /* --- character device --- */
136 static void parse_chr(DeviceState
*dev
, const char *str
, void **ptr
,
137 const char *propname
, Error
**errp
)
139 CharDriverState
*chr
= qemu_chr_find(str
);
141 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
142 object_get_typename(OBJECT(dev
)), propname
, str
);
145 if (qemu_chr_fe_claim(chr
) != 0) {
146 error_setg(errp
, "Property '%s.%s' can't take value '%s', it's in use",
147 object_get_typename(OBJECT(dev
)), propname
, str
);
153 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
155 DeviceState
*dev
= DEVICE(obj
);
156 Property
*prop
= opaque
;
157 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
158 CharDriverState
*chr
= *ptr
;
161 qemu_chr_add_handlers(chr
, NULL
, NULL
, NULL
, NULL
);
162 qemu_chr_fe_release(chr
);
167 static char *print_chr(void *ptr
)
169 CharDriverState
*chr
= ptr
;
170 const char *val
= chr
->label
? chr
->label
: "";
172 return g_strdup(val
);
175 static void get_chr(Object
*obj
, Visitor
*v
, void *opaque
,
176 const char *name
, Error
**errp
)
178 get_pointer(obj
, v
, opaque
, print_chr
, name
, errp
);
181 static void set_chr(Object
*obj
, Visitor
*v
, void *opaque
,
182 const char *name
, Error
**errp
)
184 set_pointer(obj
, v
, opaque
, parse_chr
, name
, errp
);
187 PropertyInfo qdev_prop_chr
= {
189 .description
= "ID of a chardev to use as a backend",
192 .release
= release_chr
,
195 /* --- netdev device --- */
196 static void get_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
197 const char *name
, Error
**errp
)
199 DeviceState
*dev
= DEVICE(obj
);
200 Property
*prop
= opaque
;
201 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
202 char *p
= g_strdup(peers_ptr
->ncs
[0] ? peers_ptr
->ncs
[0]->name
: "");
204 visit_type_str(v
, &p
, name
, errp
);
208 static void set_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
209 const char *name
, Error
**errp
)
211 DeviceState
*dev
= DEVICE(obj
);
212 Property
*prop
= opaque
;
213 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
214 NetClientState
**ncs
= peers_ptr
->ncs
;
215 NetClientState
*peers
[MAX_QUEUE_NUM
];
216 Error
*local_err
= NULL
;
217 int queues
, err
= 0, i
= 0;
221 qdev_prop_set_after_realize(dev
, name
, errp
);
225 visit_type_str(v
, &str
, name
, &local_err
);
227 error_propagate(errp
, local_err
);
231 queues
= qemu_find_net_clients_except(str
, peers
,
232 NET_CLIENT_OPTIONS_KIND_NIC
,
239 if (queues
> MAX_QUEUE_NUM
) {
240 error_setg(errp
, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
241 str
, queues
, MAX_QUEUE_NUM
);
245 for (i
= 0; i
< queues
; i
++) {
246 if (peers
[i
] == NULL
) {
251 if (peers
[i
]->peer
) {
262 ncs
[i
]->queue_index
= i
;
265 peers_ptr
->queues
= queues
;
268 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
272 PropertyInfo qdev_prop_netdev
= {
274 .description
= "ID of a netdev to use as a backend",
281 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
283 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
287 if (!net_hub_id_for_client(*ptr
, &id
)) {
288 return snprintf(dest
, len
, "%d", id
);
292 return snprintf(dest
, len
, "<null>");
295 static void get_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
296 const char *name
, Error
**errp
)
298 DeviceState
*dev
= DEVICE(obj
);
299 Property
*prop
= opaque
;
300 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
305 if (!net_hub_id_for_client(*ptr
, &hub_id
)) {
310 visit_type_int32(v
, &id
, name
, errp
);
313 static void set_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
314 const char *name
, Error
**errp
)
316 DeviceState
*dev
= DEVICE(obj
);
317 Property
*prop
= opaque
;
318 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
319 NetClientState
**ptr
= &peers_ptr
->ncs
[0];
320 Error
*local_err
= NULL
;
322 NetClientState
*hubport
;
325 qdev_prop_set_after_realize(dev
, name
, errp
);
329 visit_type_int32(v
, &id
, name
, &local_err
);
331 error_propagate(errp
, local_err
);
339 error_set_from_qdev_prop_error(errp
, -EINVAL
, dev
, prop
, name
);
343 hubport
= net_hub_port_find(id
);
345 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
346 name
, prop
->info
->name
);
352 PropertyInfo qdev_prop_vlan
= {
354 .description
= "Integer VLAN id to connect to",
360 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
361 BlockBackend
*value
, Error
**errp
)
363 object_property_set_str(OBJECT(dev
), value
? blk_name(value
) : "",
367 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
,
372 qdev_prop_set_drive(dev
, name
, value
, &err
);
374 error_report_err(err
);
379 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
380 CharDriverState
*value
)
382 assert(!value
|| value
->label
);
383 object_property_set_str(OBJECT(dev
),
384 value
? value
->label
: "", name
, &error_abort
);
387 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
388 NetClientState
*value
)
390 assert(!value
|| value
->name
);
391 object_property_set_str(OBJECT(dev
),
392 value
? value
->name
: "", name
, &error_abort
);
395 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
397 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
399 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
401 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
402 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
403 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
405 nd
->instantiated
= 1;
408 static int qdev_add_one_global(void *opaque
, QemuOpts
*opts
, Error
**errp
)
412 g
= g_malloc0(sizeof(*g
));
413 g
->driver
= qemu_opt_get(opts
, "driver");
414 g
->property
= qemu_opt_get(opts
, "property");
415 g
->value
= qemu_opt_get(opts
, "value");
416 g
->user_provided
= true;
417 qdev_prop_register_global(g
);
421 void qemu_add_globals(void)
423 qemu_opts_foreach(qemu_find_opts("global"),
424 qdev_add_one_global
, NULL
, NULL
);