qdev: Add JSON -device
PATCH v2 1/15 net: Introduce NetClientInfo.check_peer_type()
In reply to Kevin WolfSome network backends (vhost-user and vhost-vdpa) work only with specific devices. At startup, they second guess what the command line option handling will do and error out if they think a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Add a callback where backends can check compatibility with a device when it actually tries to attach, even on hotplug.
--- include/net/net.h | 2 ++ hw/core/qdev-properties-system.c | 6 ++++++ 2 files changed, 8 insertions(+)
include/net/net.h +2 -0
@@ -62,6 +62,7 @@ typedef struct SocketReadState SocketReadState;typedef void (SocketReadStateFinalize )(SocketReadState *rs);typedef void (NetAnnounce )(NetClientState *);typedef bool (SetSteeringEBPF )(NetClientState *,int ); +typedef bool (NetCheckPeerType )(NetClientState *,ObjectClass *,Error **); typedef struct NetClientInfo { NetClientDriver type;
@@ -84,6 +85,7 @@ typedef struct NetClientInfo {SetVnetBE *set_vnet_be;NetAnnounce *announce;SetSteeringEBPF *set_steering_ebpf; +NetCheckPeerType *check_peer_type; } NetClientInfo;struct NetClientState {
hw/core/qdev-properties-system.c +6 -0
@@ -431,6 +431,12 @@ static void set_netdev(Object *obj, Visitor *v, const char *name, goto out; } +if (peers[i]->info->check_peer_type) { +if (!peers[i]->info->check_peer_type (peers[i], obj->class, errp)) { + goto out; + } + } + ncs[i] = peers[i]; ncs[i]->queue_index = i; }
Signature · 1 line
2.31.1
v2 cover qdev: Add JSON -device
It's still a long way until we'll have QAPIfied devices, but there are some improvements that we can already make now to make the future switch easier.
One important part of this is having code paths without QemuOpts, which we want to get rid of and replace with the keyval parser in the long run. This series adds support for JSON syntax to -device, which bypasses QemuOpts.
While we're not using QAPI yet, devices are based on QOM, so we already do have type checks and an implied schema. JSON syntax supported now can be supported by QAPI later and regarding command line compatibility, actually switching to it becomes an implementation detail this way (of course, it will still add valuable user-visible features like introspection and documentation).
Apart from making things more future proof, this also immediately adds a way to do non-scalar properties on the command line. nvme could have used list support recently, and the lack of it in -device led to some rather unnatural solution in the first version (doing the relationship between a device and objects backwards) and loss of features in the following. With this series, using a list as a device property should be possible without any weird tricks.
Unfortunately, even QMP device_add goes through QemuOpts before this series, which destroys any type safety QOM provides and also can't support non-scalar properties. This is a bug, but it turns out that libvirt actually relies on it and passes only strings for everything. So this series still leaves device_add alone until libvirt is fixed.
v2:
- Drop type safe QMP device_add because libvirt gets it wrong, too
- More network patches to eliminate dependencies on the legacy QemuOpts data structures which would not contain all devices any more after this series. Fix some bugs there as a side effect.
- Remove an unnecessary use of ERRP_GUARD()
- Replaced error handling patch for qdev_set_id() with Damien's
- Drop the deprecation patch until libvirt uses the new JSON syntax
Damien Hedde (1):
softmmu/qdev-monitor: add error handling in qdev_set_id
Kevin Wolf (14):
net: Introduce NetClientInfo.check_peer_type() net/vhost-user: Fix device compatibility check net/vhost-vdpa: Fix device compatibility check
qom: Reduce use of error_propagate()
iotests/245: Fix type for iothread property iotests/051: Fix typo
qdev: Avoid using string visitor for properties
qdev: Make DeviceState.id independent of QemuOpts
qemu-option: Allow deleting opts during qemu_opts_foreach()
qdev: Add Error parameter to hide_device() callbacks
virtio-net: Store failover primary opts pointer locally
virtio-net: Avoid QemuOpts in failover_find_primary_device()
qdev: Base object creation on QDict rather than QemuOpts
vl: Enable JSON syntax for -device
qapi/qdev.json | 15 +++-- include/hw/qdev-core.h | 15 +++-- include/hw/virtio/virtio-net.h | 2 + include/monitor/qdev.h | 27 +++++++- include/net/net.h | 2 + hw/arm/virt.c | 2 +- hw/core/qdev-properties-system.c | 6 ++ hw/core/qdev.c | 11 +++- hw/net/virtio-net.c | 85 ++++++++++++------------- hw/pci-bridge/pci_expander_bridge.c | 2 +- hw/ppc/e500.c | 2 +- hw/vfio/pci.c | 4 +- hw/xen/xen-legacy-backend.c | 3 +- net/vhost-user.c | 41 ++++-------- net/vhost-vdpa.c | 37 ++++------- qom/object.c | 7 +- qom/object_interfaces.c | 19 ++---- softmmu/qdev-monitor.c | 99 +++++++++++++++++++---------- softmmu/vl.c | 63 ++++++++++++++++-- util/qemu-option.c | 4 +- tests/qemu-iotests/051 | 2 +- tests/qemu-iotests/051.pc.out | 4 +- tests/qemu-iotests/245 | 4 +- 23 files changed, 278 insertions(+), 178 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 6/15 iotests/051: Fix typo
The iothread isn't called 'iothread0', but 'thread0'. Depending on the order that properties are parsed, the error message may change from the expected one to another one saying that the iothread doesn't exist.
--- tests/qemu-iotests/051 | 2 +- tests/qemu-iotests/051.pc.out | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
tests/qemu-iotests/051 +1 -1
@@ -199,7 +199,7 @@ case "$QEMU_DEFAULT_MACHINE" in # virtio-blk enables the iothread only when the driver initialises the # device, so a second virtio-blk device can't be added even with the # same iothread. virtio-scsi allows this. - run_qemu $iothread -device virtio-blk-pci,drive=disk,iothread=iothread0,share-rw=on + run_qemu $iothread -device virtio-blk-pci,drive=disk,iothread=thread0,share-rw=on run_qemu $iothread -device virtio-scsi,id=virtio-scsi1,iothread=thread0 -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on ;; *)
tests/qemu-iotests/051.pc.out +2 -2
@@ -183,9 +183,9 @@ Testing: -drive file=TEST_DIR/t.qcow2,if=none,node-name=disk -object iothread,id QEMU X.Y.Z monitor - type 'help' for more information (qemu) QEMU_PROG: -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on: Cannot change iothread of active block backend -Testing: -drive file=TEST_DIR/t.qcow2,if=none,node-name=disk -object iothread,id=thread0 -device virtio-scsi,iothread=thread0,id=virtio-scsi0 -device scsi-hd,bus=virtio-scsi0.0,drive=disk,share-rw=on -device virtio-blk-pci,drive=disk,iothread=iothread0,share-rw=on +Testing: -drive file=TEST_DIR/t.qcow2,if=none,node-name=disk -object iothread,id=thread0 -device virtio-scsi,iothread=thread0,id=virtio-scsi0 -device scsi-hd,bus=virtio-scsi0.0,drive=disk,share-rw=on -device virtio-blk-pci,drive=disk,iothread=thread0,share-rw=on QEMU X.Y.Z monitor - type 'help' for more information -(qemu) QEMU_PROG: -device virtio-blk-pci,drive=disk,iothread=iothread0,share-rw=on: Cannot change iothread of active block backend +(qemu) QEMU_PROG: -device virtio-blk-pci,drive=disk,iothread=thread0,share-rw=on: Cannot change iothread of active block backend Testing: -drive file=TEST_DIR/t.qcow2,if=none,node-name=disk -object iothread,id=thread0 -device virtio-scsi,iothread=thread0,id=virtio-scsi0 -device scsi-hd,bus=virtio-scsi0.0,drive=disk,share-rw=on -device virtio-scsi,id=virtio-scsi1,iothread=thread0 -device scsi-hd,bus=virtio-scsi1.0,drive=disk,share-rw=on QEMU X.Y.Z monitor - type 'help' for more information
Signature · 1 line
2.31.1
PATCH v2 2/15 net/vhost-user: Fix device compatibility check
In reply to Kevin Wolfvhost-user works only with specific devices. At startup, it second guesses what the command line option handling will do and error out if it thinks a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Drop the old checks and implement .check_peer_type() instead to fix this. As a nice side effect, it also removes one more dependency on the legacy QemuOpts infrastructure and even reduces the code size.
--- net/vhost-user.c | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-)
net/vhost-user.c +14 -27
@@ -198,6 +198,19 @@ static bool vhost_user_has_ufo(NetClientState *nc)return true; } +static bool vhost_user_check_peer_type (NetClientState *nc,ObjectClass *oc, +Error **errp) +{ +const char *driver =object_class_get_name (oc); + +if (!g_str_has_prefix (driver,"virtio-net-" )) { +error_setg (errp,"vhost-user requires frontend driver virtio-net-*" ); +return false; + } + +return true; +} +static NetClientInfo net_vhost_user_info = { .type =NET_CLIENT_DRIVER_VHOST_USER , .size =sizeof (NetVhostUserState),
@@ -207,6 +220,7 @@ static NetClientInfo net_vhost_user_info = { .has_ufo = vhost_user_has_ufo, .set_vnet_be = vhost_user_set_vnet_endianness, .set_vnet_le = vhost_user_set_vnet_endianness, + .check_peer_type = vhost_user_check_peer_type, }; static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
@@ -397,27 +411,6 @@ static Chardev *net_vhost_claim_chardev(return chr; } -static int net_vhost_check_net (void *opaque,QemuOpts *opts,Error **errp) -{ -const char *name = opaque; -const char *driver, *netdev; - - driver =qemu_opt_get (opts,"driver" ); - netdev =qemu_opt_get (opts,"netdev" ); - -if (!driver || !netdev) { -return 0 ; - } - -if (strcmp (netdev, name) ==0 && - !g_str_has_prefix (driver,"virtio-net-" )) { -error_setg (errp,"vhost-user requires frontend driver virtio-net-*" ); -return -1 ; - } - -return 0 ; -} -int net_init_vhost_user (const Netdev *netdev,const char *name,NetClientState *peer,Error **errp) {
@@ -433,12 +426,6 @@ int net_init_vhost_user(const Netdev *netdev, const char *name,return -1 ; } -/* verify net frontend */ -if (qemu_opts_foreach (qemu_find_opts ("device" ), net_vhost_check_net, - (char *)name, errp)) { -return -1 ; - } - queues = vhost_user_opts->has_queues ? vhost_user_opts->queues :1 ;if (queues <1 || queues >MAX_QUEUE_NUM ) { error_setg(errp,
Signature · 1 line
2.31.1
PATCH v2 3/15 net/vhost-vdpa: Fix device compatibility check
In reply to Kevin Wolfvhost-vdpa works only with specific devices. At startup, it second guesses what the command line option handling will do and error out if it thinks a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Drop the old checks and implement .check_peer_type() instead to fix this. As a nice side effect, it also removes one more dependency on the legacy QemuOpts infrastructure and even reduces the code size.
--- net/vhost-vdpa.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-)
net/vhost-vdpa.c +14 -23
@@ -147,12 +147,26 @@ static bool vhost_vdpa_has_ufo(NetClientState *nc) } +static bool vhost_vdpa_check_peer_type (NetClientState *nc,ObjectClass *oc, +Error **errp) +{ +const char *driver =object_class_get_name (oc); + +if (!g_str_has_prefix (driver,"virtio-net-" )) { +error_setg (errp,"vhost-vdpa requires frontend driver virtio-net-*" ); +return false; + } + +return true; +} +static NetClientInfo net_vhost_vdpa_info = { .type =NET_CLIENT_DRIVER_VHOST_VDPA , .size =sizeof (VhostVDPAState), .cleanup = vhost_vdpa_cleanup, .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, .has_ufo = vhost_vdpa_has_ufo, + .check_peer_type = vhost_vdpa_check_peer_type, }; static int net_vhost_vdpa_init(NetClientState *peer, const char *device,
@@ -179,24 +193,6 @@ static int net_vhost_vdpa_init(NetClientState *peer, const char *device,return ret; } -static int net_vhost_check_net (void *opaque,QemuOpts *opts,Error **errp) -{ -const char *name = opaque; -const char *driver, *netdev; - - driver =qemu_opt_get (opts,"driver" ); - netdev =qemu_opt_get (opts,"netdev" ); -if (!driver || !netdev) { -return 0 ; - } -if (strcmp (netdev, name) ==0 && - !g_str_has_prefix (driver,"virtio-net-" )) { -error_setg (errp,"vhost-vdpa requires frontend driver virtio-net-*" ); -return -1 ; - } -return 0 ; -} -int net_init_vhost_vdpa (const Netdev *netdev,const char *name,NetClientState *peer,Error **errp) {
@@ -204,10 +200,5 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA ); opts = &netdev->u.vhost_vdpa; -/* verify net frontend */ -if (qemu_opts_foreach (qemu_find_opts ("device" ), net_vhost_check_net, - (char *)name, errp)) { -return -1 ; - }return net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, opts->vhostdev); }
Signature · 1 line
2.31.1
PATCH v2 4/15 qom: Reduce use of error_propagate()
In reply to Kevin WolfERRP_GUARD() makes debugging easier by making sure that &error_abort still fails at the real origin of the error instead of error_propagate().
--- qom/object.c | 7 +++---- qom/object_interfaces.c | 19 ++++++------------- 2 files changed, 9 insertions(+), 17 deletions(-)
qom/object.c +3 -4
@@ -1389,7 +1389,7 @@ bool object_property_get(Object *obj, const char *name, Visitor *v,bool object_property_set (Object *obj,const char *name,Visitor *v,Error **errp) { -Error *err =NULL ; +ERRP_GUARD ();ObjectProperty *prop =object_property_find_err (obj, name, errp);if (prop ==NULL ) {
@@ -1400,9 +1400,8 @@ bool object_property_set(Object *obj, const char *name, Visitor *v,error_setg (errp,QERR_PERMISSION_DENIED );return false; } - prop->set (obj, v, name, prop->opaque, &err); -error_propagate (errp, err); -return !err; + prop->set (obj, v, name, prop->opaque, errp); +return !*errp; } bool object_property_set_str(Object *obj, const char *name,
qom/object_interfaces.c +6 -13
@@ -46,25 +46,18 @@ static void object_set_properties_from_qdict(Object *obj, const QDict *qdict, Visitor *v, Error **errp) {const QDictEntry *e; -Error *local_err =NULL ; -if (!visit_start_struct (v,NULL ,NULL ,0 , &local_err)) { - goto out; +if (!visit_start_struct (v,NULL ,NULL ,0 , errp)) { +return ; }for (e =qdict_first (qdict); e; e =qdict_next (qdict, e)) { -if (!object_property_set (obj, e->key, v, &local_err)) { -break ; +if (!object_property_set (obj, e->key, v, errp)) { + goto out; } } -if (!local_err) { -visit_check_struct (v, &local_err); - } -visit_end_struct (v,NULL ); - +visit_check_struct (v, errp); out: -if (local_err) { -error_propagate (errp, local_err); - } +visit_end_struct (v,NULL ); } void object_set_properties_from_keyval(Object *obj, const QDict *qdict,
Signature · 1 line
2.31.1
PATCH v2 7/15 qdev: Avoid using string visitor for properties
In reply to Kevin WolfThe only thing the string visitor adds compared to a keyval visitor is list support. git grep for 'visit_start_list' and 'visit.*List' shows that devices don't make use of this.
In a world with a QAPIfied command line interface, the keyval visitor is used to parse the command line. In order to make sure that no devices start using this feature that would make backwards compatibility harder, just switch away from object_property_parse(), which internally uses the string visitor, to a keyval visitor and object_property_set().
--- softmmu/qdev-monitor.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
softmmu/qdev-monitor.c +17 -3
@@ -28,6 +28,8 @@#include "qapi/qmp/dispatch.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qerror.h" +#include "qapi/qmp/qstring.h" +#include "qapi/qobject-input-visitor.h" #include "qemu/config-file.h" #include "qemu/error-report.h" #include "qemu/help_option.h"
@@ -198,16 +200,28 @@ static int set_property(void *opaque, const char *name, const char *value, Error **errp) {Object *obj = opaque; +QString *val; +Visitor *v; +int ret;if (strcmp (name,"driver" ) ==0 )return 0 ;if (strcmp (name,"bus" ) ==0 )return 0 ; -if (!object_property_parse (obj, name, value, errp)) { -return -1 ; + val =qstring_from_str (value); + v =qobject_input_visitor_new_keyval (QOBJECT (val)); + +if (!object_property_set (obj, name, v, errp)) { + ret =-1 ; + goto out; } -return 0 ; + + ret =0 ; +out: +visit_free (v); +qobject_unref (val); +return ret; }static const char *find_typename_by_alias (const char *alias)
Signature · 1 line
2.31.1
PATCH v2 9/15 softmmu/qdev-monitor: add error handling in qdev_set_id
In reply to Kevin WolfFrom: Damien Hedde <[email protected]>
qdev_set_id() is mostly used when the user adds a device (using -device cli option or device_add qmp command). This commit adds an error parameter to handle the case where the given id is already taken.
Also document the function and add a return value in order to be able to capture success/failure: the function now returns the id in case of success, or NULL in case of failure.
The commit modifies the 2 calling places (qdev-monitor and xen-legacy-backend) to add the error object parameter.
Note that the id is, right now, guaranteed to be unique because all ids came from the "device" QemuOptsList where the id is used as key. This addition is a preparation for a future commit which will relax the uniqueness.
--- include/monitor/qdev.h | 25 +++++++++++++++++++++++- hw/xen/xen-legacy-backend.c | 3 ++- softmmu/qdev-monitor.c | 38 +++++++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 12 deletions(-)
include/monitor/qdev.h +24 -1
@@ -9,6 +9,29 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp);int qdev_device_help (QemuOpts *opts);DeviceState *qdev_device_add (QemuOpts *opts,Error **errp); -void qdev_set_id (DeviceState *dev,char *id); + +/** +* qdev_set_id: parent the device and set its id if provided. +* @dev: device to handle +* @id: id to be given to the device, or NULL. +* +* Returns: the id of the device in case of success; otherwise NULL. +* +* @dev must be unrealized, unparented and must not have an id. +* +* If @id is non-NULL, this function tries to setup @dev qom path as +* "/peripheral/id". If @id is already taken, it fails. If it succeeds, +* the id field of @dev is set to @id (@dev now owns the given @id +* parameter). +* +* If @id is NULL, this function generates a unique name and setups @dev +* qom path as "/peripheral-anon/name". This name is not set as the id +* of @dev. +* +* Upon success, it returns the id/name (generated or provided). The +* returned string is owned by the corresponding child property and must +* not be freed by the caller. +*/ +const char *qdev_set_id (DeviceState *dev,char *id,Error **errp);#endif
hw/xen/xen-legacy-backend.c +2 -1
@@ -276,7 +276,8 @@ static struct XenLegacyDevice *xen_be_get_xendev(const char *type, int dom, xendev =g_malloc0 (ops->size);object_initialize (&xendev->qdev, ops->size,TYPE_XENBACKEND );OBJECT (xendev)->free = g_free; -qdev_set_id (DEVICE (xendev),g_strdup_printf ("xen-%s-%d" , type, dev)); +qdev_set_id (DEVICE (xendev),g_strdup_printf ("xen-%s-%d" , type, dev), + &error_fatal);qdev_realize (DEVICE (xendev), xen_sysbus, &error_fatal);object_unref (OBJECT (xendev));
softmmu/qdev-monitor.c +28 -10
@@ -593,22 +593,34 @@ static BusState *qbus_find(const char *path, Error **errp) }/* Takes ownership of @id, will be freed when deleting the device */ -void qdev_set_id (DeviceState *dev,char *id) +const char *qdev_set_id (DeviceState *dev,char *id,Error **errp) { -if (id) { - dev->id = id; - } +ObjectProperty *prop; -if (dev->id) { -object_property_add_child (qdev_get_peripheral (), dev->id, -OBJECT (dev)); +assert (!dev->id && !dev->realized); + +/* +* object_property_[try_]add_child() below will assert the device +* has no parent +*/ +if (id) { + prop =object_property_try_add_child (qdev_get_peripheral (), id, +OBJECT (dev),NULL ); +if (prop) { + dev->id = id; + }else { +error_setg (errp,"Duplicate device ID '%s'" , id); +return NULL ; + } }else {static int anon_count;gchar *name =g_strdup_printf ("device[%d]" , anon_count++); -object_property_add_child (qdev_get_peripheral_anon (), name, -OBJECT (dev)); + prop =object_property_add_child (qdev_get_peripheral_anon (), name, +OBJECT (dev));g_free (name); } + +return prop->name; }DeviceState *qdev_device_add (QemuOpts *opts,Error **errp)
@@ -691,7 +703,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) } } -qdev_set_id (dev,g_strdup (qemu_opts_id (opts))); +/* +* set dev's parent and register its id. +* If it fails it means the id is already taken. +*/ +if (!qdev_set_id (dev,g_strdup (qemu_opts_id (opts)), errp)) { + goto err_del_dev; + }/* set properties */ if (qemu_opt_foreach (opts, set_property, dev, errp)) {
Signature · 1 line
2.31.1
PATCH v2 8/15 qdev: Make DeviceState.id independent of QemuOpts
In reply to Kevin WolfDeviceState.id is a pointer to a string that is stored in the QemuOpts object DeviceState.opts and freed together with it. We want to create devices without going through QemuOpts in the future, so make this a separately allocated string.
--- include/hw/qdev-core.h | 2 +- include/monitor/qdev.h | 2 +- hw/arm/virt.c | 2 +- hw/core/qdev.c | 1 + hw/pci-bridge/pci_expander_bridge.c | 2 +- hw/ppc/e500.c | 2 +- softmmu/qdev-monitor.c | 5 +++-- 7 files changed, 9 insertions(+), 7 deletions(-)
include/hw/qdev-core.h +1 -1
@@ -176,7 +176,7 @@ struct DeviceState {Object parent_obj;/*< public >*/ -const char *id; +char *id;char *canonical_path;bool realized;bool pending_deleted_event;
include/monitor/qdev.h +1 -1
@@ -9,6 +9,6 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp);int qdev_device_help (QemuOpts *opts);DeviceState *qdev_device_add (QemuOpts *opts,Error **errp); -void qdev_set_id (DeviceState *dev,const char *id); +void qdev_set_id (DeviceState *dev,char *id);#endif
hw/arm/virt.c +1 -1
@@ -1459,7 +1459,7 @@ static void create_platform_bus(VirtMachineState *vms)MemoryRegion *sysmem =get_system_memory (); dev =qdev_new (TYPE_PLATFORM_BUS_DEVICE ); - dev->id =TYPE_PLATFORM_BUS_DEVICE ; + dev->id =g_strdup (TYPE_PLATFORM_BUS_DEVICE );qdev_prop_set_uint32 (dev,"num_irqs" ,PLATFORM_BUS_NUM_IRQS );qdev_prop_set_uint32 (dev,"mmio_size" , vms->memmap[VIRT_PLATFORM_BUS ].size);sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal);
hw/core/qdev.c +1 -0
@@ -956,6 +956,7 @@ static void device_finalize(Object *obj) }qemu_opts_del (dev->opts); +g_free (dev->id); }static void device_class_base_init (ObjectClass *class,void *data)
hw/pci-bridge/pci_expander_bridge.c +1 -1
@@ -245,7 +245,7 @@ static void pxb_dev_realize_common(PCIDevice *dev, bool pcie, Error **errp) }else { bus =pci_root_bus_new (ds,"pxb-internal" ,NULL ,NULL ,0 ,TYPE_PXB_BUS ); bds =qdev_new ("pci-bridge" ); - bds->id = dev_name; + bds->id =g_strdup (dev_name);qdev_prop_set_uint8 (bds,PCI_BRIDGE_DEV_PROP_CHASSIS_NR , pxb->bus_nr);qdev_prop_set_bit (bds,PCI_BRIDGE_DEV_PROP_SHPC , false); }
hw/ppc/e500.c +1 -1
@@ -1006,7 +1006,7 @@ void ppce500_init(MachineState *machine)/* Platform Bus Device */ if (pmc->has_platform_bus) { dev =qdev_new (TYPE_PLATFORM_BUS_DEVICE ); - dev->id =TYPE_PLATFORM_BUS_DEVICE ; + dev->id =g_strdup (TYPE_PLATFORM_BUS_DEVICE );qdev_prop_set_uint32 (dev,"num_irqs" , pmc->platform_bus_num_irqs);qdev_prop_set_uint32 (dev,"mmio_size" , pmc->platform_bus_size);sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal);
softmmu/qdev-monitor.c +3 -2
@@ -592,7 +592,8 @@ static BusState *qbus_find(const char *path, Error **errp)return bus; } -void qdev_set_id (DeviceState *dev,const char *id) +/* Takes ownership of @id, will be freed when deleting the device */ +void qdev_set_id (DeviceState *dev,char *id) {if (id) { dev->id = id;
@@ -690,7 +691,7 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) } } -qdev_set_id (dev,qemu_opts_id (opts)); +qdev_set_id (dev,g_strdup (qemu_opts_id (opts)));/* set properties */ if (qemu_opt_foreach (opts, set_property, dev, errp)) {
Signature · 1 line
2.31.1
PATCH v2 10/15 qemu-option: Allow deleting opts during qemu_opts_foreach()
In reply to Kevin WolfUse QTAILQ_FOREACH_SAFE() so that the current QemuOpts can be deleted while iterating through the whole list.
--- util/qemu-option.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
util/qemu-option.c +2 -2
@@ -1126,11 +1126,11 @@ int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, Error **errp) { Location loc; -QemuOpts *opts; +QemuOpts *opts, *next;int rc =0 ;loc_push_none (&loc); - QTAILQ_FOREACH(opts, &list->head, next) { + QTAILQ_FOREACH_SAFE(opts, &list->head, next, next) {loc_restore (&opts->loc); rc =func (opaque, opts, errp);if (rc) {
Signature · 1 line
2.31.1
PATCH v2 5/15 iotests/245: Fix type for iothread property
In reply to Kevin Wolfiothread is a string property, so None (= JSON null) is not a valid value for it. Pass the empty string instead to get the default iothread.
--- tests/qemu-iotests/245 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qemu-iotests/245 +2 -2
@@ -1189,10 +1189,10 @@ class TestBlockdevReopen(iotests.QMPTestCase): self.run_test_iothreads('iothread0', 'iothread0') def test_iothreads_switch_backing(self): - self.run_test_iothreads('iothread0', None) + self.run_test_iothreads('iothread0', '') def test_iothreads_switch_overlay(self): - self.run_test_iothreads(None, 'iothread0') + self.run_test_iothreads('', 'iothread0') if __name__ == '__main__': iotests.activate_logging()
Signature · 1 line
2.31.1
PATCH v2 14/15 qdev: Base object creation on QDict rather than QemuOpts
In reply to Kevin WolfQDicts are both what QMP natively uses and what the keyval parser produces. Going through QemuOpts isn't useful for either one, so switch the main device creation function to QDicts. By sharing more code with the -object/object-add code path, we can even reduce the code size a bit.
This commit doesn't remove the detour through QemuOpts from any code path yet, but it allows the following commits to do so.
--- include/hw/qdev-core.h | 11 +++--- include/hw/virtio/virtio-net.h | 3 +- include/monitor/qdev.h | 2 + hw/core/qdev.c | 7 ++-- hw/net/virtio-net.c | 23 +++++++----- hw/vfio/pci.c | 4 +- softmmu/qdev-monitor.c | 69 +++++++++++++++------------------- 7 files changed, 60 insertions(+), 59 deletions(-)
include/hw/qdev-core.h +6 -5
@@ -180,7 +180,7 @@ struct DeviceState {char *canonical_path;bool realized;bool pending_deleted_event; -QemuOpts *opts; +QDict *opts;int hotplugged;bool allow_unplug_during_migration;BusState *parent_bus;
@@ -205,8 +205,8 @@ struct DeviceListener {* On errors, it returns false and errp is set. Device creation * should fail in this case. */ -bool (*hide_device)(DeviceListener *listener,QemuOpts *device_opts, -Error **errp); +bool (*hide_device)(DeviceListener *listener,const QDict *device_opts, +bool from_json,Error **errp);QTAILQ_ENTRY (DeviceListener) link; };
@@ -835,13 +835,14 @@ void device_listener_unregister(DeviceListener *listener);/** * @qdev_should_hide_device: -* @opts: QemuOpts as passed on cmdline. +* @opts: options QDict +* @from_json: true if @opts entries are typed, false for all strings * * Check if a device should be added. * When a device is added via qdev_device_add() this will be called, * and return if the device should be added now or not. */ -bool qdev_should_hide_device (QemuOpts *opts,Error **errp); +bool qdev_should_hide_device (const QDict *opts,bool from_json,Error **errp); typedef enum MachineInitPhase { /* current_machine is NULL. */
include/hw/virtio/virtio-net.h +2 -1
@@ -209,7 +209,8 @@ struct VirtIONet {bool failover_primary_hidden;bool failover;DeviceListener primary_listener; -QemuOpts *primary_opts; +QDict *primary_opts; +bool primary_opts_from_json;Notifier migration_state;VirtioNetRssData rss_data;struct NetRxPkt *rx_pkt;
include/monitor/qdev.h +2 -0
@@ -9,6 +9,8 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp);int qdev_device_help (QemuOpts *opts);DeviceState *qdev_device_add (QemuOpts *opts,Error **errp); +DeviceState *qdev_device_add_from_qdict (const QDict *opts, +bool from_json,Error **errp);/** * qdev_set_id: parent the device and set its id if provided.
hw/core/qdev.c +4 -3
@@ -28,6 +28,7 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "qapi/qapi-events-qdev.h" +#include "qapi/qmp/qdict.h" #include "qapi/qmp/qerror.h" #include "qapi/visitor.h" #include "qemu/error-report.h"
@@ -211,14 +212,14 @@ void device_listener_unregister(DeviceListener *listener)QTAILQ_REMOVE (&device_listeners, listener, link); } -bool qdev_should_hide_device (QemuOpts *opts,Error **errp) +bool qdev_should_hide_device (const QDict *opts,bool from_json,Error **errp) {ERRP_GUARD ();DeviceListener *listener; QTAILQ_FOREACH(listener, &device_listeners, link) {if (listener->hide_device) { -if (listener->hide_device (listener, opts, errp)) { +if (listener->hide_device (listener, opts, from_json, errp)) {return true; }else if (*errp) {return false;
@@ -958,7 +959,7 @@ static void device_finalize(Object *obj) dev->canonical_path =NULL ; } -qemu_opts_del (dev->opts); +qobject_unref (dev->opts);g_free (dev->id); }
hw/net/virtio-net.c +13 -10
@@ -858,9 +858,11 @@ static void failover_add_primary(VirtIONet *n, Error **errp)return ; } - dev =qdev_device_add (n->primary_opts, &err); + dev =qdev_device_add_from_qdict (n->primary_opts, + n->primary_opts_from_json, + &err);if (err) { -qemu_opts_del (n->primary_opts); +qobject_unref (n->primary_opts); n->primary_opts =NULL ; }else {object_unref (OBJECT (dev));
@@ -3287,7 +3289,9 @@ static void virtio_net_migration_state_notifier(Notifier *notifier, void *data) }static bool failover_hide_primary_device (DeviceListener *listener, -QemuOpts *device_opts,Error **errp) +const QDict *device_opts, +bool from_json, +Error **errp) {VirtIONet *n =container_of (listener, VirtIONet, primary_listener);const char *standby_id;
@@ -3295,7 +3299,7 @@ static bool failover_hide_primary_device(DeviceListener *listener,if (!device_opts) {return false; } - standby_id =qemu_opt_get (device_opts,"failover_pair_id" ); + standby_id =qdict_get_try_str (device_opts,"failover_pair_id" );if (g_strcmp0 (standby_id, n->netclient_name) !=0 ) {return false; }
@@ -3306,12 +3310,8 @@ static bool failover_hide_primary_device(DeviceListener *listener,return false; } -/* -* Having a weak reference here should be okay because a device can't be -* deleted while it's hidden. This will be replaced soon with a QDict that -* has a clearer ownership model. -*/ - n->primary_opts = device_opts; + n->primary_opts =qdict_clone_shallow (device_opts); + n->primary_opts_from_json = from_json;/* failover_primary_hidden is set during feature negotiation */ return qatomic_read (&n->failover_primary_hidden);
@@ -3502,8 +3502,11 @@ static void virtio_net_device_unrealize(DeviceState *dev)g_free (n->vlans);if (n->failover) { +qobject_unref (n->primary_opts);device_listener_unregister (&n->primary_listener);remove_migration_state_change_notifier (&n->migration_state); + }else { +assert (n->primary_opts ==NULL ); } max_queues = n->multiqueue ? n->max_queues :1 ;
hw/vfio/pci.c +2 -2
@@ -29,10 +29,10 @@#include "hw/qdev-properties.h" #include "hw/qdev-properties-system.h" #include "migration/vmstate.h" +#include "qapi/qmp/qdict.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/module.h" -#include "qemu/option.h" #include "qemu/range.h" #include "qemu/units.h" #include "sysemu/kvm.h"
@@ -941,7 +941,7 @@ static void vfio_pci_size_rom(VFIOPCIDevice *vdev) }if (vfio_opt_rom_in_denylist (vdev)) { -if (dev->opts &&qemu_opt_get (dev->opts,"rombar" )) { +if (dev->opts &&qdict_haskey (dev->opts,"rombar" )) {warn_report ("Device at %s is known to cause system instability" " issues during option rom execution" , vdev->vbasedev.name);
softmmu/qdev-monitor.c +31 -38
123 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v2 13/15 virtio-net: Avoid QemuOpts in failover_find_primary_device()
In reply to Kevin WolfDon't go through the global QemuOptsList, it is state of the legacy command line parser and we will create devices that are not contained in it. It is also just the command line configuration and not necessarily the current runtime state.
Instead, look at the qdev device tree which has the current state of all existing devices.
--- hw/net/virtio-net.c | 52 +++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 33 deletions(-)
hw/net/virtio-net.c +19 -33
@@ -796,48 +796,34 @@ static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)typedef struct {VirtIONet *n; -char *id; -}FailoverId ; +DeviceState *dev; +}FailoverDevice ;/** -* Set the id of the failover primary device +* Set the failover primary device * * @opaque: FailoverId to setup * @opts: opts for device we are handling * @errp: returns an error if this function fails */ -static int failover_set_primary (void *opaque,QemuOpts *opts,Error **errp) +static int failover_set_primary (DeviceState *dev,void *opaque) { -FailoverId *fid = opaque; -const char *standby_id =qemu_opt_get (opts,"failover_pair_id" ); +FailoverDevice *fdev = opaque; +PCIDevice *pci_dev = (PCIDevice *) +object_dynamic_cast (OBJECT (dev),TYPE_PCI_DEVICE ); -if (g_strcmp0 (standby_id, fid->n->netclient_name) ==0 ) { - fid->id =g_strdup (opts->id); +if (!pci_dev) { +return 0 ; + } + +if (!g_strcmp0 (pci_dev->failover_pair_id, fdev->n->netclient_name)) { + fdev->dev = dev;return 1 ; }return 0 ; } -/** -* Find the primary device id for this failover virtio-net -* -* @n: VirtIONet device -* @errp: returns an error if this function fails -*/ -static char *failover_find_primary_device_id (VirtIONet *n) -{ -Error *err =NULL ; -FailoverId fid; - - fid.n = n; -if (!qemu_opts_foreach (qemu_find_opts ("device" ), - failover_set_primary, &fid, &err)) { -return NULL ; - } -return fid.id; -} -/** * Find the primary device for this failover virtio-net *
@@ -846,13 +832,13 @@ static char *failover_find_primary_device_id(VirtIONet *n)*/ static DeviceState *failover_find_primary_device (VirtIONet *n) { -char *id =failover_find_primary_device_id (n); - -if (!id) { -return NULL ; - } +FailoverDevice fdev = { + .n = n, + }; -return qdev_find_recursive (sysbus_get_default (), id); +qbus_walk_children (sysbus_get_default (), failover_set_primary,NULL , +NULL ,NULL , &fdev); +return fdev.dev; }static void failover_add_primary (VirtIONet *n,Error **errp)
Signature · 1 line
2.31.1
PATCH v2 12/15 virtio-net: Store failover primary opts pointer locally
In reply to Kevin WolfInstead of accessing the global QemuOptsList, which really belong to the command line parser and shouldn't be accessed from devices, store a pointer to the QemuOpts in a new VirtIONet field.
This is not the final state, but just an intermediate step to get rid of QemuOpts in devices. It will later be replaced with an options QDict.
Before this patch, two "primary" devices could be hidden for the same standby device, but only one of them would actually be enabled and the other one would be kept hidden forever, so this doesn't make sense. After this patch, configuring a second primary device is an error.
--- include/hw/virtio/virtio-net.h | 1 + hw/net/virtio-net.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-)
include/hw/virtio/virtio-net.h +1 -0
@@ -209,6 +209,7 @@ struct VirtIONet {bool failover_primary_hidden;bool failover;DeviceListener primary_listener; +QemuOpts *primary_opts;Notifier migration_state;VirtioNetRssData rss_data;struct NetRxPkt *rx_pkt;
hw/net/virtio-net.c +18 -8
@@ -858,27 +858,24 @@ static DeviceState *failover_find_primary_device(VirtIONet *n)static void failover_add_primary (VirtIONet *n,Error **errp) {Error *err =NULL ; -QemuOpts *opts; -char *id;DeviceState *dev =failover_find_primary_device (n);if (dev) {return ; } - id =failover_find_primary_device_id (n); -if (!id) { +if (!n->primary_opts) {error_setg (errp,"Primary device not found" );error_append_hint (errp,"Virtio-net failover will not work. Make " "sure primary device has parameter" " failover_pair_id=%s\n" , n->netclient_name);return ; } - opts =qemu_opts_find (qemu_find_opts ("device" ), id); -g_assert (opts);/* cannot be NULL because id was found using opts list */ - dev =qdev_device_add (opts, &err); + + dev =qdev_device_add (n->primary_opts, &err);if (err) { -qemu_opts_del (opts); +qemu_opts_del (n->primary_opts); + n->primary_opts =NULL ; }else {object_unref (OBJECT (dev)); }
@@ -3317,6 +3314,19 @@ static bool failover_hide_primary_device(DeviceListener *listener,return false; } +if (n->primary_opts) { +error_setg (errp,"Cannot attach more than one primary device to '%s'" , + n->netclient_name); +return false; + } + +/* +* Having a weak reference here should be okay because a device can't be +* deleted while it's hidden. This will be replaced soon with a QDict that +* has a clearer ownership model. +*/ + n->primary_opts = device_opts; +/* failover_primary_hidden is set during feature negotiation */ return qatomic_read (&n->failover_primary_hidden); }
Signature · 1 line
2.31.1
PATCH v2 11/15 qdev: Add Error parameter to hide_device() callbacks
In reply to Kevin Wolfhide_device() is used for virtio-net failover, where the standby virtio device delays creation of the primary device. It only makes sense to have a single primary device for each standby device. Adding a second one should result in an error instead of hiding it and never using it afterwards.
Prepare for this by adding an Error parameter to the hide_device() callback where virtio-net is informed about adding a primary device.
--- include/hw/qdev-core.h | 8 ++++++-- hw/core/qdev.c | 7 +++++-- hw/net/virtio-net.c | 2 +- softmmu/qdev-monitor.c | 5 ++++- 4 files changed, 16 insertions(+), 6 deletions(-)
include/hw/qdev-core.h +6 -2
@@ -201,8 +201,12 @@ struct DeviceListener {* informs qdev if a device should be visible or hidden. We can * hide a failover device depending for example on the device * opts. +* +* On errors, it returns false and errp is set. Device creation +* should fail in this case. */ -bool (*hide_device)(DeviceListener *listener,QemuOpts *device_opts); +bool (*hide_device)(DeviceListener *listener,QemuOpts *device_opts, +Error **errp);QTAILQ_ENTRY (DeviceListener) link; };
@@ -837,7 +841,7 @@ void device_listener_unregister(DeviceListener *listener);* When a device is added via qdev_device_add() this will be called, * and return if the device should be added now or not. */ -bool qdev_should_hide_device (QemuOpts *opts); +bool qdev_should_hide_device (QemuOpts *opts,Error **errp); typedef enum MachineInitPhase { /* current_machine is NULL. */
hw/core/qdev.c +5 -2
@@ -211,14 +211,17 @@ void device_listener_unregister(DeviceListener *listener)QTAILQ_REMOVE (&device_listeners, listener, link); } -bool qdev_should_hide_device (QemuOpts *opts) +bool qdev_should_hide_device (QemuOpts *opts,Error **errp) { +ERRP_GUARD ();DeviceListener *listener; QTAILQ_FOREACH(listener, &device_listeners, link) {if (listener->hide_device) { -if (listener->hide_device (listener, opts)) { +if (listener->hide_device (listener, opts, errp)) {return true; + }else if (*errp) { +return false; } } }
hw/net/virtio-net.c +1 -1
@@ -3304,7 +3304,7 @@ static void virtio_net_migration_state_notifier(Notifier *notifier, void *data) }static bool failover_hide_primary_device (DeviceListener *listener, -QemuOpts *device_opts) +QemuOpts *device_opts,Error **errp) {VirtIONet *n =container_of (listener, VirtIONet, primary_listener);const char *standby_id;
softmmu/qdev-monitor.c +4 -1
@@ -625,6 +625,7 @@ const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)DeviceState *qdev_device_add (QemuOpts *opts,Error **errp) { +ERRP_GUARD ();DeviceClass *dc;const char *driver, *path;DeviceState *dev =NULL ;
@@ -668,11 +669,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)error_setg (errp,"Device with failover_pair_id don't have id" );return NULL ; } -if (qdev_should_hide_device (opts)) { +if (qdev_should_hide_device (opts, errp)) {if (bus && !qbus_is_hotpluggable (bus)) {error_setg (errp,QERR_BUS_NO_HOTPLUG , bus->name); }return NULL ; + }else if (*errp) { +return NULL ; } }
Signature · 1 line
2.31.1
PATCH v2 15/15 vl: Enable JSON syntax for -device
In reply to Kevin WolfLike we already do for -object, introduce support for JSON syntax in -device, which can be kept stable in the long term and guarantees that a single code path with identical behaviour is used for both QMP and the command line. Compared to the QemuOpts based code, the parser contains less surprises and has support for non-scalar options (lists and structs). Switching management tools to JSON means that we can more easily change the "human" CLI syntax from QemuOpts to the keyval parser later.
In the QAPI schema, a feature flag is added to the device-add command to allow management tools to detect support for this.
--- qapi/qdev.json | 15 ++++++++---- softmmu/vl.c | 63 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 67 insertions(+), 11 deletions(-)
qapi/qdev.json +11 -4
@@ -32,17 +32,23 @@ ## # @device_add: # +# Add a device. +# # @driver: the name of the new device's driver # # @bus: the device's parent bus (device tree path) # # @id: the device's ID, must be unique # -# Additional arguments depend on the type. -# -# Add a device. +# Features: +# @json-cli: If present, the "-device" command line option supports JSON +# syntax with a structure identical to the arguments of this +# command. # # Notes: +# +# Additional arguments depend on the type. +# # 1. For detailed information about this command, please refer to the # 'docs/qdev-device-use.txt' file. #
@@ -67,7 +73,8 @@ ## { 'command': 'device_add', 'data': {'driver': 'str', '*bus': 'str', '*id': 'str'}, - 'gen': false } # so we can get the additional arguments + 'gen': false, # so we can get the additional arguments + 'features': ['json-cli'] } ## # @device_del:
softmmu/vl.c +56 -7
116 lines not on this page · raw message
Signature · 1 line
2.31.1
Re: [PATCH v2 14/15] qdev: Base object creation on QDict rather than QemuOpts
In reply to Kevin WolfQuoted 45 lines · Kevin Wolf
QDicts are both what QMP natively uses and what the keyval parser produces. Going through QemuOpts isn't useful for either one, so switch the main device creation function to QDicts. By sharing more code with the -object/object-add code path, we can even reduce the code size a bit.
This commit doesn't remove the detour through QemuOpts from any code path yet, but it allows the following commits to do so.
Signed-off-by Kevin Wolf <[email protected]>--- include/hw/qdev-core.h | 11 +++--- include/hw/virtio/virtio-net.h | 3 +- include/monitor/qdev.h | 2 + hw/core/qdev.c | 7 ++-- hw/net/virtio-net.c | 23 +++++++----- hw/vfio/pci.c | 4 +- softmmu/qdev-monitor.c | 69 +++++++++++++++------------------- 7 files changed, 60 insertions(+), 59 deletions(-)include/hw/qdev-core.h +3
-3@@ -180,7 +180,7 @@ struct DeviceState {char *canonical_path;bool realized;bool pending_deleted_event; -QemuOpts *opts; +QDict *opts;int hotplugged;bool allow_unplug_during_migration;BusState *parent_bus;@@ -205,8 +205,8 @@ struct DeviceListener {* On errors, it returns false and errp is set. Device creation * should fail in this case. */ -bool (*hide_device)(DeviceListener *listener,QemuOpts *device_opts, -Error **errp); +bool (*hide_device)(DeviceListener *listener,const QDict *device_opts, +bool from_json,Error **errp);QTAILQ_ENTRY (DeviceListener) link; };
include/hw/qdev-core.h void device_listener_unregister(DeviceListener *listener); · line 838
Quoted 5 lines
include/hw/qdev-core.h +1
-1@@ -835,13 +835,14 @@ void device_listener_unregister(DeviceListener *listener);/** * @qdev_should_hide_device: -* @opts: QemuOpts as passed on cmdline. +* @opts: options QDict > + * @from_json: true if @opts entries are typed, false for all strings
Add the errp here too:
* @errp: pointer to error object
include/hw/qdev-core.h void device_listener_unregister(DeviceListener *listener); · line 848
Quoted 10 lines
include/hw/qdev-core.h +1
-1* * Check if a device should be added. * When a device is added via qdev_device_add() this will be called, * and return if the device should be added now or not. */ -bool qdev_should_hide_device (QemuOpts *opts,Error **errp); +bool qdev_should_hide_device (const QDict *opts,bool from_json,Error **errp); typedef enum MachineInitPhase { /* current_machine is NULL. */
Thank you to have added the errp pointer in the hide_device helpers, it helps in my series.
Laurent
Re: [PATCH v2 04/15] qom: Reduce use of error_propagate()
In reply to Kevin WolfQuoted 5 lines · Kevin Wolf
ERRP_GUARD()makes debugging easier by making sure that&error_abortstill fails at the real origin of the error instead oferror_propagate().Signed-off-by Kevin Wolf <[email protected]>
Yes, please!
Re: [PATCH v2 09/15] softmmu/qdev-monitor: add error handling in qdev_set_id
In reply to Kevin WolfQuoted 43 lines · Kevin Wolf
From: Damien Hedde <[email protected]>
qdev_set_id()is mostly used when the user adds a device (using -device cli option ordevice_addqmp command). This commit adds an error parameter to handle the case where the given id is already taken.Also document the function and add a return value in order to be able to capture success/failure: the function now returns the id in case of success, or NULL in case of failure.
The commit modifies the 2 calling places (qdev-monitor and xen-legacy-backend) to add the error object parameter.
Note that the id is, right now, guaranteed to be unique because all ids came from the "device" QemuOptsList where the id is used as key. This addition is a preparation for a future commit which will relax the uniqueness.
Signed-off-by Damien Hedde <[email protected]>Signed-off-by Kevin Wolf <[email protected]>---
+++
b/softmmu/qdev-monitor.csoftmmu/qdev-monitor.c +6
-7@@ -593,22 +593,34 @@ static BusState *qbus_find(const char *path, Error **errp) }/* Takes ownership of @id, will be freed when deleting the device */ -void qdev_set_id (DeviceState *dev,char *id) +const char *qdev_set_id (DeviceState *dev,char *id,Error **errp) { -if (id) { - dev->id = id; - } +ObjectProperty *prop; -if (dev->id) { -object_property_add_child (qdev_get_peripheral (), dev->id, -OBJECT (dev)); +assert (!dev->id && !dev->realized); + +/* +* object_property_[try_]add_child() below will assert the device
softmmu/qdev-monitor.c static BusState *qbus_find(const char *path, Error **errp) · line 613
Quoted 10 lines
softmmu/qdev-monitor.c +10
-0+* has no parent +*/ +if (id) { + prop =object_property_try_add_child (qdev_get_peripheral (), id, +OBJECT (dev),NULL ); +if (prop) { + dev->id = id; + }else { +error_setg (errp,"Duplicate device ID '%s'" , id); +return NULL ;
id is not freed on this error path...
Quoted 16 lines
softmmu/qdev-monitor.c +5
-2+ } }else {static int anon_count;gchar *name =g_strdup_printf ("device[%d]" , anon_count++); -object_property_add_child (qdev_get_peripheral_anon (), name, -OBJECT (dev)); + prop =object_property_add_child (qdev_get_peripheral_anon (), name, +OBJECT (dev));g_free (name); } + +return prop->name; }DeviceState *qdev_device_add (QemuOpts *opts,Error **errp)
softmmu/qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 711
Quoted 10 lines
softmmu/qdev-monitor.c +6
-1@@ -691,7 +703,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) } } -qdev_set_id (dev,g_strdup (qemu_opts_id (opts))); +/* +* set dev's parent and register its id. +* If it fails it means the id is already taken. +*/ +if (!qdev_set_id (dev,g_strdup (qemu_opts_id (opts)), errp)) { + goto err_del_dev;
...nor on this, which means the g_strdup() leaks on failure.
Quoted 7 lines
softmmu/qdev-monitor.c +1
-0+ }/* set properties */ if (qemu_opt_foreach (opts, set_property, dev, errp)) {Signature · 1 line
2.31.1
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v2 09/15] softmmu/qdev-monitor: add error handling in qdev_set_id
Quoted 37 lines · Eric Blake
Quoted 23 lines · Kevin Wolf
From: Damien Hedde <[email protected]>
qdev_set_id()is mostly used when the user adds a device (using -device cli option ordevice_addqmp command). This commit adds an error parameter to handle the case where the given id is already taken.Also document the function and add a return value in order to be able to capture success/failure: the function now returns the id in case of success, or NULL in case of failure.
The commit modifies the 2 calling places (qdev-monitor and xen-legacy-backend) to add the error object parameter.
Note that the id is, right now, guaranteed to be unique because all ids came from the "device" QemuOptsList where the id is used as key. This addition is a preparation for a future commit which will relax the uniqueness.
Signed-off-by Damien Hedde <[email protected]>Signed-off-by Kevin Wolf <[email protected]>---
softmmu/qdev-monitor.c static BusState *qbus_find(const char *path, Error **errp) · line 613
Quoted 3 lines
softmmu/qdev-monitor.c +3
-0+ }else { +error_setg (errp,"Duplicate device ID '%s'" , id); +return NULL ;id is not freed on this error path...
Quoted 6 lines
DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
(quoted diff) +0
-0@@ -691,7 +703,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) } }
Quoted 10 lines
softmmu/qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 711
Quoted 7 lines
softmmu/qdev-monitor.c +6
-1-qdev_set_id (dev,g_strdup (qemu_opts_id (opts))); +/* +* set dev's parent and register its id. +* If it fails it means the id is already taken. +*/ +if (!qdev_set_id (dev,g_strdup (qemu_opts_id (opts)), errp)) { + goto err_del_dev;...nor on this, which means the
g_strdup()leaks on failure.
Since we strdup the id just before calling qdev_set_id.
Maybe we should do the the strdup in qdev_set_id (and free things on error there too). It seems simplier than freeing things on the callee side just in case of an error.
Damien
Re: [PATCH v2 01/15] net: Introduce NetClientInfo.check_peer_type()
In reply to Kevin WolfQuoted 10 lines · Kevin Wolf
Some network backends (vhost-user and vhost-vdpa) work only with specific devices. At startup, they second guess what the command line option handling will do and error out if they think a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Quoted 4 lines
Add a callback where backends can check compatibility with a device when it actually tries to attach, even on hotplug.
Signed-off-by Kevin Wolf <[email protected]>
Quoted 43 lines
--- include/net/net.h | 2 ++ hw/core/qdev-properties-system.c | 6 ++++++ 2 files changed, 8 insertions(+)include/net/net.h +2
-0@@ -62,6 +62,7 @@ typedef struct SocketReadState SocketReadState;typedef void (SocketReadStateFinalize )(SocketReadState *rs);typedef void (NetAnnounce )(NetClientState *);typedef bool (SetSteeringEBPF )(NetClientState *,int ); +typedef bool (NetCheckPeerType )(NetClientState *,ObjectClass *,Error **); typedef struct NetClientInfo { NetClientDriver type;@@ -84,6 +85,7 @@ typedef struct NetClientInfo {SetVnetBE *set_vnet_be;NetAnnounce *announce;SetSteeringEBPF *set_steering_ebpf; +NetCheckPeerType *check_peer_type; } NetClientInfo;struct NetClientState {hw/core/qdev-properties-system.c +6
-0@@ -431,6 +431,12 @@ static void set_netdev(Object *obj, Visitor *v, const char *name, goto out; } +if (peers[i]->info->check_peer_type) { +if (!peers[i]->info->check_peer_type (peers[i], obj->class, errp)) { + goto out; + } + } + ncs[i] = peers[i]; ncs[i]->queue_index = i; }
Re: [PATCH v2 02/15] net/vhost-user: Fix device compatibility check
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
vhost-user works only with specific devices. At startup, it second guesses what the command line option handling will do and error out if it thinks a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Quoted 5 lines
Drop the old checks and implement .check_peer_type() instead to fix this. As a nice side effect, it also removes one more dependency on the legacy QemuOpts infrastructure and even reduces the code size.
Signed-off-by Kevin Wolf <[email protected]>
Re: [PATCH v2 03/15] net/vhost-vdpa: Fix device compatibility check
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
vhost-vdpa works only with specific devices. At startup, it second guesses what the command line option handling will do and error out if it thinks a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Quoted 5 lines
Drop the old checks and implement .check_peer_type() instead to fix this. As a nice side effect, it also removes one more dependency on the legacy QemuOpts infrastructure and even reduces the code size.
Signed-off-by Kevin Wolf <[email protected]>
Re: [PATCH v2 08/15] qdev: Make DeviceState.id independent of QemuOpts
In reply to Kevin WolfQuoted 7 lines · Kevin Wolf
DeviceState.id is a pointer to a string that is stored in the QemuOpts object DeviceState.opts and freed together with it. We want to create devices without going through QemuOpts in the future, so make this a separately allocated string.
Signed-off-by Kevin Wolf <[email protected]>Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Quoted 24 lines · Kevin Wolf
It's still a long way until we'll have QAPIfied devices, but there are some improvements that we can already make now to make the future switch easier.
One important part of this is having code paths without QemuOpts, which we want to get rid of and replace with the keyval parser in the long run. This series adds support for JSON syntax to -device, which bypasses QemuOpts.
While we're not using QAPI yet, devices are based on QOM, so we already do have type checks and an implied schema. JSON syntax supported now can be supported by QAPI later and regarding command line compatibility, actually switching to it becomes an implementation detail this way (of course, it will still add valuable user-visible features like introspection and documentation).
Apart from making things more future proof, this also immediately adds a way to do non-scalar properties on the command line. nvme could have used list support recently, and the lack of it in -device led to some rather unnatural solution in the first version (doing the relationship between a device and objects backwards) and loss of features in the following. With this series, using a list as a device property should be possible without any weird tricks.
Quoted 5 lines
Unfortunately, even QMP
device_addgoes through QemuOpts before this series, which destroys any type safety QOM provides and also can't support non-scalar properties. This is a bug, but it turns out that libvirt actually relies on it and passes only strings for everything. So this series still leavesdevice_addalone until libvirt is fixed.
I assume you are merging this?
Quoted 55 lines
v2:
- Drop type safe QMPdevice_addbecause libvirt gets it wrong, too
- More network patches to eliminate dependencies on the legacy QemuOpts data structures which would not contain all devices any more after this series. Fix some bugs there as a side effect.
- Remove an unnecessary use ofERRP_GUARD()
- Replaced error handling patch forqdev_set_id()with Damien's
- Drop the deprecation patch until libvirt uses the new JSON syntaxDamien Hedde (1):
softmmu/qdev-monitor: add error handling inqdev_set_idKevin Wolf (14):
net: IntroduceNetClientInfo.check_peer_type()net/vhost-user: Fix device compatibility checknet/vhost-vdpa: Fix device compatibility check
qom: Reduce use oferror_propagate()
iotests/245: Fix type for iothread property iotests/051: Fix typo
qdev: Avoid using string visitor for properties
qdev: Make DeviceState.id independent of QemuOpts
qemu-option: Allow deleting opts duringqemu_opts_foreach()
qdev: Add Error parameter tohide_device()callbacks
virtio-net: Store failover primary opts pointer locally
virtio-net: Avoid QemuOpts infailover_find_primary_device()
qdev: Base object creation on QDict rather than QemuOpts
vl: Enable JSON syntax for -deviceqapi/qdev.json | 15 +++-- include/hw/qdev-core.h | 15 +++-- include/hw/virtio/virtio-net.h | 2 + include/monitor/qdev.h | 27 +++++++- include/net/net.h | 2 + hw/arm/virt.c | 2 +- hw/core/qdev-properties-system.c | 6 ++ hw/core/qdev.c | 11 +++- hw/net/virtio-net.c | 85 ++++++++++++------------- hw/pci-bridge/pci_expander_bridge.c | 2 +- hw/ppc/e500.c | 2 +- hw/vfio/pci.c | 4 +- hw/xen/xen-legacy-backend.c | 3 +- net/vhost-user.c | 41 ++++-------- net/vhost-vdpa.c | 37 ++++------- qom/object.c | 7 +- qom/object_interfaces.c | 19 ++---- softmmu/qdev-monitor.c | 99 +++++++++++++++++++---------- softmmu/vl.c | 63 ++++++++++++++++-- util/qemu-option.c | 4 +- tests/qemu-iotests/051 | 2 +- tests/qemu-iotests/051.pc.out | 4 +- tests/qemu-iotests/245 | 4 +- 23 files changed, 278 insertions(+), 178 deletions(-)Signature · 1 line
2.31.1
Am 13.10.2021 um 17:30 hat Michael S. Tsirkin geschrieben:
Quoted 32 lines · Kevin Wolf
Quoted 24 lines
It's still a long way until we'll have QAPIfied devices, but there are some improvements that we can already make now to make the future switch easier.
One important part of this is having code paths without QemuOpts, which we want to get rid of and replace with the keyval parser in the long run. This series adds support for JSON syntax to -device, which bypasses QemuOpts.
While we're not using QAPI yet, devices are based on QOM, so we already do have type checks and an implied schema. JSON syntax supported now can be supported by QAPI later and regarding command line compatibility, actually switching to it becomes an implementation detail this way (of course, it will still add valuable user-visible features like introspection and documentation).
Apart from making things more future proof, this also immediately adds a way to do non-scalar properties on the command line. nvme could have used list support recently, and the lack of it in -device led to some rather unnatural solution in the first version (doing the relationship between a device and objects backwards) and loss of features in the following. With this series, using a list as a device property should be possible without any weird tricks.
Quoted 5 lines
Unfortunately, even QMP
device_addgoes through QemuOpts before this series, which destroys any type safety QOM provides and also can't support non-scalar properties. This is a bug, but it turns out that libvirt actually relies on it and passes only strings for everything. So this series still leavesdevice_addalone until libvirt is fixed.
Quoted 3 lines
Reviewed-by Michael S. Tsirkin <[email protected]>I assume you are merging this?
Yes, I can merge it through my tree. Thanks for the review!
Kevin
Re: [PATCH v2 09/15] softmmu/qdev-monitor: add error handling in qdev_set_id
In reply to Damien HeddeQuoted 14 lines · Damien Hedde
Quoted 4 lines
Quoted 4 lines
softmmu/qdev-monitor.c +0
-1@@ -691,7 +703,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) } } -qdev_set_id (dev,g_strdup (qemu_opts_id (opts)));Quoted 9 lines
softmmu/qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 711
Quoted 6 lines
softmmu/qdev-monitor.c +6
-0+/* +* set dev's parent and register its id. +* If it fails it means the id is already taken. +*/ +if (!qdev_set_id (dev,g_strdup (qemu_opts_id (opts)), errp)) { + goto err_del_dev;...nor on this, which means the
g_strdup()leaks on failure.
Quoted 4 lines
Since we strdup the id just before calling
qdev_set_id.
Maybe we should do the the strdup inqdev_set_id(and free things on error there too). It seems simplier than freeing things on the callee side just in case of an error.
Indeed. If we expected qdev_set_id() to be passed something that it can later free, we would have used 'char *'; but because we used 'const char *' for that parameter, it really does make more sense for the callers to pass in any string and for qdev_set_id() to do the necessary strdup()ing, as well as clean up on error.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v2 01/15] net: Introduce NetClientInfo.check_peer_type()
In reply to Kevin WolfQuoted 10 lines · Kevin Wolf
Some network backends (vhost-user and vhost-vdpa) work only with specific devices. At startup, they second guess what the command line option handling will do and error out if they think a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Quoted 4 lines
Add a callback where backends can check compatibility with a device when it actually tries to attach, even on hotplug.
Signed-off-by Kevin Wolf <[email protected]>
Quoted 42 lines
--- include/net/net.h | 2 ++ hw/core/qdev-properties-system.c | 6 ++++++ 2 files changed, 8 insertions(+)include/net/net.h +2
-0@@ -62,6 +62,7 @@ typedef struct SocketReadState SocketReadState;typedef void (SocketReadStateFinalize )(SocketReadState *rs);typedef void (NetAnnounce )(NetClientState *);typedef bool (SetSteeringEBPF )(NetClientState *,int ); +typedef bool (NetCheckPeerType )(NetClientState *,ObjectClass *,Error **); typedef struct NetClientInfo { NetClientDriver type;@@ -84,6 +85,7 @@ typedef struct NetClientInfo {SetVnetBE *set_vnet_be;NetAnnounce *announce;SetSteeringEBPF *set_steering_ebpf; +NetCheckPeerType *check_peer_type; } NetClientInfo;struct NetClientState {hw/core/qdev-properties-system.c +6
-0@@ -431,6 +431,12 @@ static void set_netdev(Object *obj, Visitor *v, const char *name, goto out; } +if (peers[i]->info->check_peer_type) { +if (!peers[i]->info->check_peer_type (peers[i], obj->class, errp)) { + goto out; + } + } + ncs[i] = peers[i]; ncs[i]->queue_index = i; }
Re: [PATCH v2 02/15] net/vhost-user: Fix device compatibility check
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
vhost-user works only with specific devices. At startup, it second guesses what the command line option handling will do and error out if it thinks a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Quoted 5 lines
Drop the old checks and implement .check_peer_type() instead to fix this. As a nice side effect, it also removes one more dependency on the legacy QemuOpts infrastructure and even reduces the code size.
Signed-off-by Kevin Wolf <[email protected]>
Quoted 77 lines
--- net/vhost-user.c | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-)net/vhost-user.c +14
-27@@ -198,6 +198,19 @@ static bool vhost_user_has_ufo(NetClientState *nc)return true; } +static bool vhost_user_check_peer_type (NetClientState *nc,ObjectClass *oc, +Error **errp) +{ +const char *driver =object_class_get_name (oc); + +if (!g_str_has_prefix (driver,"virtio-net-" )) { +error_setg (errp,"vhost-user requires frontend driver virtio-net-*" ); +return false; + } + +return true; +} +static NetClientInfo net_vhost_user_info = { .type =NET_CLIENT_DRIVER_VHOST_USER , .size =sizeof (NetVhostUserState),@@ -207,6 +220,7 @@ static NetClientInfo net_vhost_user_info = { .has_ufo = vhost_user_has_ufo, .set_vnet_be = vhost_user_set_vnet_endianness, .set_vnet_le = vhost_user_set_vnet_endianness, + .check_peer_type = vhost_user_check_peer_type, }; static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,@@ -397,27 +411,6 @@ static Chardev *net_vhost_claim_chardev(return chr; } -static int net_vhost_check_net (void *opaque,QemuOpts *opts,Error **errp) -{ -const char *name = opaque; -const char *driver, *netdev; - - driver =qemu_opt_get (opts,"driver" ); - netdev =qemu_opt_get (opts,"netdev" ); - -if (!driver || !netdev) { -return 0 ; - } - -if (strcmp (netdev, name) ==0 && - !g_str_has_prefix (driver,"virtio-net-" )) { -error_setg (errp,"vhost-user requires frontend driver virtio-net-*" ); -return -1 ; - } - -return 0 ; -} -int net_init_vhost_user (const Netdev *netdev,const char *name,NetClientState *peer,Error **errp) {@@ -433,12 +426,6 @@ int net_init_vhost_user(const Netdev *netdev, const char *name,return -1 ; } -/* verify net frontend */ -if (qemu_opts_foreach (qemu_find_opts ("device" ), net_vhost_check_net, - (char *)name, errp)) { -return -1 ; - } - queues = vhost_user_opts->has_queues ? vhost_user_opts->queues :1 ;if (queues <1 || queues >MAX_QUEUE_NUM ) { error_setg(errp,
Re: [PATCH v2 03/15] net/vhost-vdpa: Fix device compatibility check
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
vhost-vdpa works only with specific devices. At startup, it second guesses what the command line option handling will do and error out if it thinks a non-virtio device will attach to them.
This second guessing is not only ugly, it can lead to wrong error messages ('-device floppy,netdev=foo' should complain about an unknown property, not about the wrong kind of network device being attached) and completely ignores hotplugging.
Quoted 5 lines
Drop the old checks and implement .check_peer_type() instead to fix this. As a nice side effect, it also removes one more dependency on the legacy QemuOpts infrastructure and even reduces the code size.
Signed-off-by Kevin Wolf <[email protected]>
Quoted 71 lines
--- net/vhost-vdpa.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-)net/vhost-vdpa.c +14
-23@@ -147,12 +147,26 @@ static bool vhost_vdpa_has_ufo(NetClientState *nc) } +static bool vhost_vdpa_check_peer_type (NetClientState *nc,ObjectClass *oc, +Error **errp) +{ +const char *driver =object_class_get_name (oc); + +if (!g_str_has_prefix (driver,"virtio-net-" )) { +error_setg (errp,"vhost-vdpa requires frontend driver virtio-net-*" ); +return false; + } + +return true; +} +static NetClientInfo net_vhost_vdpa_info = { .type =NET_CLIENT_DRIVER_VHOST_VDPA , .size =sizeof (VhostVDPAState), .cleanup = vhost_vdpa_cleanup, .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, .has_ufo = vhost_vdpa_has_ufo, + .check_peer_type = vhost_vdpa_check_peer_type, }; static int net_vhost_vdpa_init(NetClientState *peer, const char *device,@@ -179,24 +193,6 @@ static int net_vhost_vdpa_init(NetClientState *peer, const char *device,return ret; } -static int net_vhost_check_net (void *opaque,QemuOpts *opts,Error **errp) -{ -const char *name = opaque; -const char *driver, *netdev; - - driver =qemu_opt_get (opts,"driver" ); - netdev =qemu_opt_get (opts,"netdev" ); -if (!driver || !netdev) { -return 0 ; - } -if (strcmp (netdev, name) ==0 && - !g_str_has_prefix (driver,"virtio-net-" )) { -error_setg (errp,"vhost-vdpa requires frontend driver virtio-net-*" ); -return -1 ; - } -return 0 ; -} -int net_init_vhost_vdpa (const Netdev *netdev,const char *name,NetClientState *peer,Error **errp) {@@ -204,10 +200,5 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_VDPA ); opts = &netdev->u.vhost_vdpa; -/* verify net frontend */ -if (qemu_opts_foreach (qemu_find_opts ("device" ), net_vhost_check_net, - (char *)name, errp)) { -return -1 ; - }return net_vhost_vdpa_init (peer,TYPE_VHOST_VDPA , name, opts->vhostdev); }
Re: [PATCH v2 09/15] softmmu/qdev-monitor: add error handling in qdev_set_id
In reply to Eric BlakeAm 13.10.2021 um 23:37 hat Eric Blake geschrieben:
Quoted 20 lines · Damien Hedde
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
softmmu/qdev-monitor.c +0
-1@@ -691,7 +703,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) } } -qdev_set_id (dev,g_strdup (qemu_opts_id (opts)));Quoted 9 lines
softmmu/qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) · line 711
Quoted 6 lines
softmmu/qdev-monitor.c +6
-0+/* +* set dev's parent and register its id. +* If it fails it means the id is already taken. +*/ +if (!qdev_set_id (dev,g_strdup (qemu_opts_id (opts)), errp)) { + goto err_del_dev;...nor on this, which means the
g_strdup()leaks on failure.Quoted 4 lines
Since we strdup the id just before calling
qdev_set_id.
Maybe we should do the the strdup inqdev_set_id(and free things on error there too). It seems simplier than freeing things on the callee side just in case of an error.
Quoted 5 lines
Indeed. If we expected
qdev_set_id()to be passed something that it can later free, we would have used 'char *'; but because we used 'const char *' for that parameter, it really does make more sense for the callers to pass in any string and forqdev_set_id()to do the necessary strdup()ing, as well as clean up on error.
Since this seems to be the only thing in the series that needs to be addressed, I'll do the minimal fix while applying (adding g_free() to the error path in qemu_opts_id()) and then we can clean up on top.
Kevin
Quoted 24 lines · Kevin Wolf
It's still a long way until we'll have QAPIfied devices, but there are some improvements that we can already make now to make the future switch easier.
One important part of this is having code paths without QemuOpts, which we want to get rid of and replace with the keyval parser in the long run. This series adds support for JSON syntax to -device, which bypasses QemuOpts.
While we're not using QAPI yet, devices are based on QOM, so we already do have type checks and an implied schema. JSON syntax supported now can be supported by QAPI later and regarding command line compatibility, actually switching to it becomes an implementation detail this way (of course, it will still add valuable user-visible features like introspection and documentation).
Apart from making things more future proof, this also immediately adds a way to do non-scalar properties on the command line. nvme could have used list support recently, and the lack of it in -device led to some rather unnatural solution in the first version (doing the relationship between a device and objects backwards) and loss of features in the following. With this series, using a list as a device property should be possible without any weird tricks.
Quoted 5 lines
Unfortunately, even QMP
device_addgoes through QemuOpts before this series, which destroys any type safety QOM provides and also can't support non-scalar properties. This is a bug, but it turns out that libvirt actually relies on it and passes only strings for everything. So this series still leavesdevice_addalone until libvirt is fixed.
I've tested hotplug and standard -device with many (not all) devices libvirt uses and seems that both '-device JSON' and monitor work properly.
I'll enable '-device JSON' in libvirt once this hits upstream.
Quoted 3 lines
v2:
- Drop type safe QMPdevice_addbecause libvirt gets it wrong, too
I've actually applied this patch too to make sure that libvirt is working properly in both modes. It works now well with this too.
I'm not sure about the compatibility promise here, but libvirt is now prepared even for teh strict version of 'device_add'. Libvirt's requirements state that new libvirt needs to be used with new qemu and thus from our view it's okay to do it even now.
Quoted 6 lines
- More network patches to eliminate dependencies on the legacy QemuOpts data structures which would not contain all devices any more after this series. Fix some bugs there as a side effect.
- Remove an unnecessary use ofERRP_GUARD()
- Replaced error handling patch forqdev_set_id()with Damien's
- Drop the deprecation patch until libvirt uses the new JSON syntax
Series:
Am 08.10.2021 um 15:34 hat Kevin Wolf geschrieben:
Quoted 24 lines
It's still a long way until we'll have QAPIfied devices, but there are some improvements that we can already make now to make the future switch easier.
One important part of this is having code paths without QemuOpts, which we want to get rid of and replace with the keyval parser in the long run. This series adds support for JSON syntax to -device, which bypasses QemuOpts.
While we're not using QAPI yet, devices are based on QOM, so we already do have type checks and an implied schema. JSON syntax supported now can be supported by QAPI later and regarding command line compatibility, actually switching to it becomes an implementation detail this way (of course, it will still add valuable user-visible features like introspection and documentation).
Apart from making things more future proof, this also immediately adds a way to do non-scalar properties on the command line. nvme could have used list support recently, and the lack of it in -device led to some rather unnatural solution in the first version (doing the relationship between a device and objects backwards) and loss of features in the following. With this series, using a list as a device property should be possible without any weird tricks.
Quoted 5 lines
Unfortunately, even QMP
device_addgoes through QemuOpts before this series, which destroys any type safety QOM provides and also can't support non-scalar properties. This is a bug, but it turns out that libvirt actually relies on it and passes only strings for everything. So this series still leavesdevice_addalone until libvirt is fixed.
Thanks for the review and testing, applied to my tree.
Kevin
Re: [PATCH v2 12/15] virtio-net: Store failover primary opts pointer locally
In reply to Kevin WolfQuoted 68 lines · Kevin Wolf
Instead of accessing the global QemuOptsList, which really belong to the command line parser and shouldn't be accessed from devices, store a pointer to the QemuOpts in a new VirtIONet field.
This is not the final state, but just an intermediate step to get rid of QemuOpts in devices. It will later be replaced with an options QDict.
Before this patch, two "primary" devices could be hidden for the same standby device, but only one of them would actually be enabled and the other one would be kept hidden forever, so this doesn't make sense. After this patch, configuring a second primary device is an error.
Signed-off-by Kevin Wolf <[email protected]>--- include/hw/virtio/virtio-net.h | 1 + hw/net/virtio-net.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-)include/hw/virtio/virtio-net.h +1
-0@@ -209,6 +209,7 @@ struct VirtIONet {bool failover_primary_hidden;bool failover;DeviceListener primary_listener; +QemuOpts *primary_opts;Notifier migration_state;VirtioNetRssData rss_data;struct NetRxPkt *rx_pkt;hw/net/virtio-net.c +5
-8@@ -858,27 +858,24 @@ static DeviceState *failover_find_primary_device(VirtIONet *n)static void failover_add_primary (VirtIONet *n,Error **errp) {Error *err =NULL ; -QemuOpts *opts; -char *id;DeviceState *dev =failover_find_primary_device (n);if (dev) {return ; } - id =failover_find_primary_device_id (n); -if (!id) { +if (!n->primary_opts) {error_setg (errp,"Primary device not found" );error_append_hint (errp,"Virtio-net failover will not work. Make " "sure primary device has parameter" " failover_pair_id=%s\n" , n->netclient_name);return ; } - opts =qemu_opts_find (qemu_find_opts ("device" ), id); -g_assert (opts);/* cannot be NULL because id was found using opts list */ - dev =qdev_device_add (opts, &err); + + dev =qdev_device_add (n->primary_opts, &err);if (err) { -qemu_opts_del (opts); +qemu_opts_del (n->primary_opts); + n->primary_opts =NULL ; }else {object_unref (OBJECT (dev)); }
hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3322
Quoted 9 lines
hw/net/virtio-net.c +6
-0@@ -3317,6 +3314,19 @@ static bool failover_hide_primary_device(DeviceListener *listener,return false; } +if (n->primary_opts) { +error_setg (errp,"Cannot attach more than one primary device to '%s'" , + n->netclient_name); +return false; + } +
This part has introduced a regression, I've sent a patch to fix that.
https://patchew.org/QEMU/[email protected]/
Thanks,
Laurent
Re: [PATCH v2 12/15] virtio-net: Store failover primary opts pointer locally
Am 19.10.2021 um 10:06 hat Laurent Vivier geschrieben:
Quoted 78 lines · Kevin Wolf
Quoted 66 lines
Instead of accessing the global QemuOptsList, which really belong to the command line parser and shouldn't be accessed from devices, store a pointer to the QemuOpts in a new VirtIONet field.
This is not the final state, but just an intermediate step to get rid of QemuOpts in devices. It will later be replaced with an options QDict.
Before this patch, two "primary" devices could be hidden for the same standby device, but only one of them would actually be enabled and the other one would be kept hidden forever, so this doesn't make sense. After this patch, configuring a second primary device is an error.
Signed-off-by Kevin Wolf <[email protected]>--- include/hw/virtio/virtio-net.h | 1 + hw/net/virtio-net.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-)include/hw/virtio/virtio-net.h +1
-0@@ -209,6 +209,7 @@ struct VirtIONet {bool failover_primary_hidden;bool failover;DeviceListener primary_listener; +QemuOpts *primary_opts;Notifier migration_state;VirtioNetRssData rss_data;struct NetRxPkt *rx_pkt;hw/net/virtio-net.c +5
-8@@ -858,27 +858,24 @@ static DeviceState *failover_find_primary_device(VirtIONet *n)static void failover_add_primary (VirtIONet *n,Error **errp) {Error *err =NULL ; -QemuOpts *opts; -char *id;DeviceState *dev =failover_find_primary_device (n);if (dev) {return ; } - id =failover_find_primary_device_id (n); -if (!id) { +if (!n->primary_opts) {error_setg (errp,"Primary device not found" );error_append_hint (errp,"Virtio-net failover will not work. Make " "sure primary device has parameter" " failover_pair_id=%s\n" , n->netclient_name);return ; } - opts =qemu_opts_find (qemu_find_opts ("device" ), id); -g_assert (opts);/* cannot be NULL because id was found using opts list */ - dev =qdev_device_add (opts, &err); + + dev =qdev_device_add (n->primary_opts, &err);if (err) { -qemu_opts_del (opts); +qemu_opts_del (n->primary_opts); + n->primary_opts =NULL ; }else {object_unref (OBJECT (dev)); }hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3321
Quoted 8 lines
hw/net/virtio-net.c +6
-0@@ -3317,6 +3314,19 @@ static bool failover_hide_primary_device(DeviceListener *listener,return false; } +if (n->primary_opts) { +error_setg (errp,"Cannot attach more than one primary device to '%s'" , + n->netclient_name); +return false; + } +This part has introduced a regression, I've sent a patch to fix that.
Quoted 1 line
Thanks for catching this! The fix looks good to me.
Kevin