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 int (*parse
)(DeviceState
*dev
, const char *str
,
40 const char *name
, Error
**errp
)
42 DeviceState
*dev
= DEVICE(obj
);
43 Error
*local_err
= NULL
;
44 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 ret
= parse(dev
, str
, ptr
);
64 error_set_from_qdev_prop_error(errp
, ret
, dev
, prop
, str
);
70 static int parse_drive(DeviceState
*dev
, const char *str
, void **ptr
)
74 blk
= blk_by_name(str
);
78 if (blk_attach_dev(blk
, dev
) < 0) {
85 static void release_drive(Object
*obj
, const char *name
, void *opaque
)
87 DeviceState
*dev
= DEVICE(obj
);
88 Property
*prop
= opaque
;
89 BlockBackend
**ptr
= qdev_get_prop_ptr(dev
, prop
);
92 blk_detach_dev(*ptr
, dev
);
93 blockdev_auto_del(*ptr
);
97 static char *print_drive(void *ptr
)
99 return g_strdup(blk_name(ptr
));
102 static void get_drive(Object
*obj
, Visitor
*v
, void *opaque
,
103 const char *name
, Error
**errp
)
105 get_pointer(obj
, v
, opaque
, print_drive
, name
, errp
);
108 static void set_drive(Object
*obj
, Visitor
*v
, void *opaque
,
109 const char *name
, Error
**errp
)
111 set_pointer(obj
, v
, opaque
, parse_drive
, name
, errp
);
114 PropertyInfo qdev_prop_drive
= {
116 .description
= "ID of a drive to use as a backend",
119 .release
= release_drive
,
122 /* --- character device --- */
124 static int parse_chr(DeviceState
*dev
, const char *str
, void **ptr
)
126 CharDriverState
*chr
= qemu_chr_find(str
);
130 if (qemu_chr_fe_claim(chr
) != 0) {
137 static void release_chr(Object
*obj
, const char *name
, void *opaque
)
139 DeviceState
*dev
= DEVICE(obj
);
140 Property
*prop
= opaque
;
141 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
142 CharDriverState
*chr
= *ptr
;
145 qemu_chr_add_handlers(chr
, NULL
, NULL
, NULL
, NULL
);
146 qemu_chr_fe_release(chr
);
151 static char *print_chr(void *ptr
)
153 CharDriverState
*chr
= ptr
;
154 const char *val
= chr
->label
? chr
->label
: "";
156 return g_strdup(val
);
159 static void get_chr(Object
*obj
, Visitor
*v
, void *opaque
,
160 const char *name
, Error
**errp
)
162 get_pointer(obj
, v
, opaque
, print_chr
, name
, errp
);
165 static void set_chr(Object
*obj
, Visitor
*v
, void *opaque
,
166 const char *name
, Error
**errp
)
168 set_pointer(obj
, v
, opaque
, parse_chr
, name
, errp
);
171 PropertyInfo qdev_prop_chr
= {
173 .description
= "ID of a chardev to use as a backend",
176 .release
= release_chr
,
179 /* --- netdev device --- */
180 static void get_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
181 const char *name
, Error
**errp
)
183 DeviceState
*dev
= DEVICE(obj
);
184 Property
*prop
= opaque
;
185 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
186 char *p
= g_strdup(peers_ptr
->ncs
[0] ? peers_ptr
->ncs
[0]->name
: "");
188 visit_type_str(v
, &p
, name
, errp
);
192 static void set_netdev(Object
*obj
, Visitor
*v
, void *opaque
,
193 const char *name
, Error
**errp
)
195 DeviceState
*dev
= DEVICE(obj
);
196 Property
*prop
= opaque
;
197 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
198 NetClientState
**ncs
= peers_ptr
->ncs
;
199 NetClientState
*peers
[MAX_QUEUE_NUM
];
200 Error
*local_err
= NULL
;
201 int queues
, err
= 0, i
= 0;
205 qdev_prop_set_after_realize(dev
, name
, errp
);
209 visit_type_str(v
, &str
, name
, &local_err
);
211 error_propagate(errp
, local_err
);
215 queues
= qemu_find_net_clients_except(str
, peers
,
216 NET_CLIENT_OPTIONS_KIND_NIC
,
223 if (queues
> MAX_QUEUE_NUM
) {
224 error_setg(errp
, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
225 str
, queues
, MAX_QUEUE_NUM
);
229 for (i
= 0; i
< queues
; i
++) {
230 if (peers
[i
] == NULL
) {
235 if (peers
[i
]->peer
) {
246 ncs
[i
]->queue_index
= i
;
249 peers_ptr
->queues
= queues
;
252 error_set_from_qdev_prop_error(errp
, err
, dev
, prop
, str
);
256 PropertyInfo qdev_prop_netdev
= {
258 .description
= "ID of a netdev to use as a backend",
265 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
267 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
271 if (!net_hub_id_for_client(*ptr
, &id
)) {
272 return snprintf(dest
, len
, "%d", id
);
276 return snprintf(dest
, len
, "<null>");
279 static void get_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
280 const char *name
, Error
**errp
)
282 DeviceState
*dev
= DEVICE(obj
);
283 Property
*prop
= opaque
;
284 NetClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
289 if (!net_hub_id_for_client(*ptr
, &hub_id
)) {
294 visit_type_int32(v
, &id
, name
, errp
);
297 static void set_vlan(Object
*obj
, Visitor
*v
, void *opaque
,
298 const char *name
, Error
**errp
)
300 DeviceState
*dev
= DEVICE(obj
);
301 Property
*prop
= opaque
;
302 NICPeers
*peers_ptr
= qdev_get_prop_ptr(dev
, prop
);
303 NetClientState
**ptr
= &peers_ptr
->ncs
[0];
304 Error
*local_err
= NULL
;
306 NetClientState
*hubport
;
309 qdev_prop_set_after_realize(dev
, name
, errp
);
313 visit_type_int32(v
, &id
, name
, &local_err
);
315 error_propagate(errp
, local_err
);
323 error_set_from_qdev_prop_error(errp
, -EINVAL
, dev
, prop
, name
);
327 hubport
= net_hub_port_find(id
);
329 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
,
330 name
, prop
->info
->name
);
336 PropertyInfo qdev_prop_vlan
= {
338 .description
= "Integer VLAN id to connect to",
344 int qdev_prop_set_drive(DeviceState
*dev
, const char *name
,
348 object_property_set_str(OBJECT(dev
),
349 value
? blk_name(value
) : "", name
, &err
);
351 qerror_report_err(err
);
358 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
,
361 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
365 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
,
366 CharDriverState
*value
)
368 assert(!value
|| value
->label
);
369 object_property_set_str(OBJECT(dev
),
370 value
? value
->label
: "", name
, &error_abort
);
373 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
,
374 NetClientState
*value
)
376 assert(!value
|| value
->name
);
377 object_property_set_str(OBJECT(dev
),
378 value
? value
->name
: "", name
, &error_abort
);
381 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
)
383 qdev_prop_set_macaddr(dev
, "mac", nd
->macaddr
.a
);
385 qdev_prop_set_netdev(dev
, "netdev", nd
->netdev
);
387 if (nd
->nvectors
!= DEV_NVECTORS_UNSPECIFIED
&&
388 object_property_find(OBJECT(dev
), "vectors", NULL
)) {
389 qdev_prop_set_uint32(dev
, "vectors", nd
->nvectors
);
391 nd
->instantiated
= 1;
394 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
398 g
= g_malloc0(sizeof(*g
));
399 g
->driver
= qemu_opt_get(opts
, "driver");
400 g
->property
= qemu_opt_get(opts
, "property");
401 g
->value
= qemu_opt_get(opts
, "value");
402 g
->user_provided
= true;
403 qdev_prop_register_global(g
);
407 void qemu_add_globals(void)
409 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);