@@ -211,6 +211,33 @@ void device_listener_unregister(DeviceListener *listener)
QTAILQ_REMOVE(&device_listeners, listener, link);
}
+bool qdev_should_hide_device(QemuOpts *opts, Error **errp)
+{
+ bool res = false;
+ bool match_found = false;
+
+ DeviceListener *listener;
+
+ QTAILQ_FOREACH(listener, &device_listeners, link) {
+ if (listener->should_be_hidden) {
+ listener->should_be_hidden(listener, opts, &match_found, &res);
+ }
+
+ if (match_found)
+ {
+ break;
+ }
+ }
+ /* No suitable pair device was found */
+ if (!match_found)
+ {
+ error_setg(errp, "An error occurred: Couln't attach to standby device"
+ " Please note that the primary device should be"
+ " must be placed after the standby device in the command line");
+ }
+ return res;
+}
+
void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,
int required_for_version)
{
@@ -70,6 +70,7 @@ static Property pci_props[] = {
QEMU_PCIE_LNKSTA_DLLLA_BITNR, true),
DEFINE_PROP_BIT("x-pcie-extcap-init", PCIDevice, cap_present,
QEMU_PCIE_EXTCAP_INIT_BITNR, true),
+ DEFINE_PROP_STRING("standby", PCIDevice, standby_id_str),
DEFINE_PROP_END_OF_LIST()
};
@@ -351,6 +351,8 @@ struct PCIDevice {
MSIVectorUseNotifier msix_vector_use_notifier;
MSIVectorReleaseNotifier msix_vector_release_notifier;
MSIVectorPollNotifier msix_vector_poll_notifier;
+
+ char *standby_id_str;
};
void pci_register_bar(PCIDevice *pci_dev, int region_num,
@@ -144,6 +144,7 @@ struct DeviceState {
char *canonical_path;
bool realized;
bool pending_deleted_event;
+ bool hidden;
QemuOpts *opts;
int hotplugged;
BusState *parent_bus;
@@ -157,6 +158,11 @@ struct DeviceState {
struct DeviceListener {
void (*realize)(DeviceListener *listener, DeviceState *dev);
void (*unrealize)(DeviceListener *listener, DeviceState *dev);
+ /* This callback is called just upon init of the DeviceState
+ * and can be used by a standby device for informing qdev if this
+ * device should be hidden by checking the device opts
+ */
+ void (*should_be_hidden)(DeviceListener *listener, QemuOpts *device_opts,bool *match_found, bool *res);
QTAILQ_ENTRY(DeviceListener) link;
};
@@ -453,4 +459,6 @@ static inline bool qbus_is_hotpluggable(BusState *bus)
void device_listener_register(DeviceListener *listener);
void device_listener_unregister(DeviceListener *listener);
+bool qdev_should_hide_device(QemuOpts *opts, Error **errp);
+
#endif
@@ -33,6 +33,7 @@
#include "qemu/option.h"
#include "sysemu/block-backend.h"
#include "migration/misc.h"
+#include "migration/migration.h"
/*
* Aliases were a bad idea from the start. Let's keep them
@@ -569,13 +570,49 @@ void qdev_set_id(DeviceState *dev, const char *id)
}
}
+static int has_standby_device(void *opaque, const char *name, const char *value,
+ Error **errp)
+{
+ if (strcmp(name, "standby") == 0)
+ {
+ QemuOpts *opts = (QemuOpts *)opaque;
+ if (qdev_should_hide_device(opts, errp) && errp && !*errp)
+ {
+ return 1;
+ }
+ else if (errp && *errp)
+ {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static bool should_hide_device(QemuOpts *opts, Error **err)
+{
+ if (qemu_opt_foreach(opts, has_standby_device, opts, err) == 0)
+ {
+ return false;
+ }
+ return true;
+}
+
DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
{
DeviceClass *dc;
const char *driver, *path;
- DeviceState *dev;
+ DeviceState *dev = NULL;
BusState *bus = NULL;
Error *err = NULL;
+
+ if (opts && should_hide_device(opts, &err))
+ {
+ if(err)
+ {
+ goto err_del_dev;
+ }
+ return NULL;
+ }
driver = qemu_opt_get(opts, "driver");
if (!driver) {
@@ -648,8 +685,11 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
err_del_dev:
error_propagate(errp, err);
- object_unparent(OBJECT(dev));
- object_unref(OBJECT(dev));
+ if (dev)
+ {
+ object_unparent(OBJECT(dev));
+ object_unref(OBJECT(dev));
+ }
return NULL;
}
@@ -875,7 +915,7 @@ void qdev_unplug(DeviceState *dev, Error **errp)
return;
}
- if (!migration_is_idle()) {
+ if (!migration_is_idle() && !migration_in_setup(migrate_get_current())) {
error_setg(errp, "device_del not allowed while migrating");
return;
}
@@ -2337,10 +2337,13 @@ static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
DeviceState *dev;
dev = qdev_device_add(opts, errp);
- if (!dev) {
+ if (!dev && *errp) {
+ error_report_err(*errp);
return -1;
+ } else if (dev)
+ {
+ object_unref(OBJECT(dev));
}
- object_unref(OBJECT(dev));
return 0;
}