diff mbox series

[RFC,1/2] qdev/qbus: Add hidden device support

Message ID 20190322134447.14831-2-jfreimann@redhat.com (mailing list archive)
State New, archived
Headers show
Series implement the failover feature for assigned network devices | expand

Commit Message

Jens Freimann March 22, 2019, 1:44 p.m. UTC
From: Sameeh Jubran <sjubran@redhat.com>

Signed-off-by: Jens Freimann <jfreimann@redhat.com>
---
 hw/core/qdev.c         | 27 ++++++++++++++++++++++++
 hw/pci/pci.c           |  1 +
 include/hw/pci/pci.h   |  2 ++
 include/hw/qdev-core.h |  8 +++++++
 qdev-monitor.c         | 48 ++++++++++++++++++++++++++++++++++++++----
 vl.c                   |  7 ++++--
 6 files changed, 87 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index f9b6efe509..5e1c8f0120 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -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)
 {
diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 35451c1e99..67b1b7c500 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -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()
 };
 
diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h
index d87f5f93e9..f75dace1ec 100644
--- a/include/hw/pci/pci.h
+++ b/include/hw/pci/pci.h
@@ -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,
diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index 33ed3b8dde..e3b11f5503 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -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
diff --git a/qdev-monitor.c b/qdev-monitor.c
index d4320986a2..f4cfc8d4a1 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -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;
     }
diff --git a/vl.c b/vl.c
index d61d5604e5..51e773a8a9 100644
--- a/vl.c
+++ b/vl.c
@@ -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;
 }