[libvirt PATCH v3 04/11] nodedev: refactor nodeDeviceFindNewDevice()

Jonathon Jongsma posted 11 patches 5 years, 7 months ago
There is a newer version of this series
[libvirt PATCH v3 04/11] nodedev: refactor nodeDeviceFindNewDevice()
Posted by Jonathon Jongsma 5 years, 7 months ago
In preparation for creating mediated devices in libvirt, we will need to
wait for new mediated devices to be created as well. Refactor
nodeDeviceFindNewDevice() so that we can re-use the main logic from this
function to wait for different device types by passing a different
'find' function.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
---
 src/node_device/node_device_driver.c | 39 +++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c
index ba7ea50e5b..629d4bcf91 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -447,6 +447,10 @@ nodeDeviceGetTime(time_t *t)
 }
 
 
+typedef virNodeDevicePtr (*nodeDeviceFindNewDeviceFunc)(virConnectPtr conn,
+                                                        const void* opaque);
+
+
 /* When large numbers of devices are present on the host, it's
  * possible for udev not to realize that it has work to do before we
  * get here.  We thus keep trying to find the new device we just
@@ -462,8 +466,8 @@ nodeDeviceGetTime(time_t *t)
  */
 static virNodeDevicePtr
 nodeDeviceFindNewDevice(virConnectPtr conn,
-                        const char *wwnn,
-                        const char *wwpn)
+                        nodeDeviceFindNewDeviceFunc func,
+                        const void *opaque)
 {
     virNodeDevicePtr device = NULL;
     time_t start = 0, now = 0;
@@ -474,7 +478,7 @@ nodeDeviceFindNewDevice(virConnectPtr conn,
 
         virWaitForDevices();
 
-        device = nodeDeviceLookupSCSIHostByWWN(conn, wwnn, wwpn, 0);
+        device = func(conn, opaque);
 
         if (device != NULL)
             break;
@@ -488,6 +492,33 @@ nodeDeviceFindNewDevice(virConnectPtr conn,
 }
 
 
+typedef struct _NewSCSIHostFuncData NewSCSIHostFuncData;
+struct _NewSCSIHostFuncData
+{
+    const char *wwnn;
+    const char *wwpn;
+};
+
+
+static virNodeDevicePtr
+nodeDeviceFindNewSCSIHostFunc(virConnectPtr conn,
+                              const void *opaque)
+{
+    const NewSCSIHostFuncData *data = opaque;
+    return nodeDeviceLookupSCSIHostByWWN(conn, data->wwnn, data->wwpn, 0);
+}
+
+
+static virNodeDevicePtr
+nodeDeviceFindNewSCSIHost(virConnectPtr conn,
+                          const char *wwnn,
+                          const char *wwpn)
+{
+    NewSCSIHostFuncData data = { .wwnn = wwnn, .wwpn = wwpn};
+    return nodeDeviceFindNewDevice(conn, nodeDeviceFindNewSCSIHostFunc, &data);
+}
+
+
 static bool
 nodeDeviceHasCapability(virNodeDeviceDefPtr def, virNodeDevCapType type)
 {
@@ -538,7 +569,7 @@ nodeDeviceCreateXML(virConnectPtr conn,
         if (virVHBAManageVport(parent_host, wwpn, wwnn, VPORT_CREATE) < 0)
             return NULL;
 
-        device = nodeDeviceFindNewDevice(conn, wwnn, wwpn);
+        device = nodeDeviceFindNewSCSIHost(conn, wwnn, wwpn);
         /* We don't check the return value, because one way or another,
          * we're returning what we get... */
 
-- 
2.21.3

Re: [libvirt PATCH v3 04/11] nodedev: refactor nodeDeviceFindNewDevice()
Posted by Erik Skultety 5 years, 7 months ago
On Tue, Jun 16, 2020 at 09:27:52AM -0500, Jonathon Jongsma wrote:
> In preparation for creating mediated devices in libvirt, we will need to
> wait for new mediated devices to be created as well. Refactor
> nodeDeviceFindNewDevice() so that we can re-use the main logic from this
> function to wait for different device types by passing a different
> 'find' function.
>
> Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
> ---
Reviewed-by: Erik Skultety <eskultet@redhat.com>

Re: [libvirt PATCH v3 04/11] nodedev: refactor nodeDeviceFindNewDevice()
Posted by Erik Skultety 5 years, 7 months ago
On Tue, Jun 16, 2020 at 09:27:52AM -0500, Jonathon Jongsma wrote:
> In preparation for creating mediated devices in libvirt, we will need to
> wait for new mediated devices to be created as well. Refactor
> nodeDeviceFindNewDevice() so that we can re-use the main logic from this
> function to wait for different device types by passing a different
> 'find' function.
>
> Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
> ---
...
> +
> +static virNodeDevicePtr
> +nodeDeviceFindNewSCSIHostFunc(virConnectPtr conn,
> +                              const void *opaque)
> +{
> +    const NewSCSIHostFuncData *data = opaque;

Technically, there should be an empty line here since ^here we're defining a
variable.

> +    return nodeDeviceLookupSCSIHostByWWN(conn, data->wwnn, data->wwpn, 0);
> +}
> +
> +
> +static virNodeDevicePtr
> +nodeDeviceFindNewSCSIHost(virConnectPtr conn,
> +                          const char *wwnn,
> +                          const char *wwpn)
> +{
> +    NewSCSIHostFuncData data = { .wwnn = wwnn, .wwpn = wwpn};

...same here...

> +    return nodeDeviceFindNewDevice(conn, nodeDeviceFindNewSCSIHostFunc, &data);
> +}

Reviewed-by: Erik Skultety <eskultet@redhat.com>