Skip to content

Commit 7d61808

Browse files
committed
qdev: Add Error parameter to hide_device() callbacks
hide_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. Signed-off-by: Kevin Wolf <[email protected]> Message-Id: <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Tested-by: Peter Krempa <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent 30648dd commit 7d61808

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

hw/core/qdev.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,17 @@ void device_listener_unregister(DeviceListener *listener)
211211
QTAILQ_REMOVE(&device_listeners, listener, link);
212212
}
213213

214-
bool qdev_should_hide_device(QemuOpts *opts)
214+
bool qdev_should_hide_device(QemuOpts *opts, Error **errp)
215215
{
216+
ERRP_GUARD();
216217
DeviceListener *listener;
217218

218219
QTAILQ_FOREACH(listener, &device_listeners, link) {
219220
if (listener->hide_device) {
220-
if (listener->hide_device(listener, opts)) {
221+
if (listener->hide_device(listener, opts, errp)) {
221222
return true;
223+
} else if (*errp) {
224+
return false;
222225
}
223226
}
224227
}

hw/net/virtio-net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3304,7 +3304,7 @@ static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
33043304
}
33053305

33063306
static bool failover_hide_primary_device(DeviceListener *listener,
3307-
QemuOpts *device_opts)
3307+
QemuOpts *device_opts, Error **errp)
33083308
{
33093309
VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
33103310
const char *standby_id;

include/hw/qdev-core.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ struct DeviceListener {
201201
* informs qdev if a device should be visible or hidden. We can
202202
* hide a failover device depending for example on the device
203203
* opts.
204+
*
205+
* On errors, it returns false and errp is set. Device creation
206+
* should fail in this case.
204207
*/
205-
bool (*hide_device)(DeviceListener *listener, QemuOpts *device_opts);
208+
bool (*hide_device)(DeviceListener *listener, QemuOpts *device_opts,
209+
Error **errp);
206210
QTAILQ_ENTRY(DeviceListener) link;
207211
};
208212

@@ -837,7 +841,7 @@ void device_listener_unregister(DeviceListener *listener);
837841
* When a device is added via qdev_device_add() this will be called,
838842
* and return if the device should be added now or not.
839843
*/
840-
bool qdev_should_hide_device(QemuOpts *opts);
844+
bool qdev_should_hide_device(QemuOpts *opts, Error **errp);
841845

842846
typedef enum MachineInitPhase {
843847
/* current_machine is NULL. */

softmmu/qdev-monitor.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
626626

627627
DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
628628
{
629+
ERRP_GUARD();
629630
DeviceClass *dc;
630631
const char *driver, *path;
631632
DeviceState *dev = NULL;
@@ -669,11 +670,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
669670
error_setg(errp, "Device with failover_pair_id don't have id");
670671
return NULL;
671672
}
672-
if (qdev_should_hide_device(opts)) {
673+
if (qdev_should_hide_device(opts, errp)) {
673674
if (bus && !qbus_is_hotpluggable(bus)) {
674675
error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
675676
}
676677
return NULL;
678+
} else if (*errp) {
679+
return NULL;
677680
}
678681
}
679682

0 commit comments

Comments
 (0)