[libvirt PATCH v5 23/30] api: add virNodeDeviceCreate()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This new API function provides a way to start a persistently-defined
mediate device that was defined by virNodeDeviceDefineXML() (or one that
was defined externally via mdevctl)

Signed-off-by: Jonathon Jongsma <jjongsma@xxxxxxxxxx>
Reviewed-by: Erik Skultety <eskultet@xxxxxxxxxx>
---
 include/libvirt/libvirt-nodedev.h            |  2 +
 src/driver-nodedev.h                         |  4 ++
 src/libvirt-nodedev.c                        | 35 ++++++++++
 src/libvirt_public.syms                      |  1 +
 src/node_device/node_device_driver.c         | 67 ++++++++++++++++++++
 src/node_device/node_device_driver.h         |  7 ++
 src/node_device/node_device_udev.c           |  1 +
 src/remote/remote_driver.c                   |  1 +
 src/remote/remote_protocol.x                 | 13 +++-
 src/remote_protocol-structs                  |  4 ++
 tests/nodedevmdevctldata/mdevctl-create.argv |  1 +
 tests/nodedevmdevctltest.c                   | 11 +++-
 12 files changed, 145 insertions(+), 2 deletions(-)
 create mode 100644 tests/nodedevmdevctldata/mdevctl-create.argv

diff --git a/include/libvirt/libvirt-nodedev.h b/include/libvirt/libvirt-nodedev.h
index 623017f1fd..cf51c3d085 100644
--- a/include/libvirt/libvirt-nodedev.h
+++ b/include/libvirt/libvirt-nodedev.h
@@ -137,6 +137,8 @@ virNodeDevicePtr virNodeDeviceDefineXML(virConnectPtr conn,
 
 int virNodeDeviceUndefine(virNodeDevicePtr dev);
 
+int virNodeDeviceCreate(virNodeDevicePtr dev);
+
 /**
  * VIR_NODE_DEVICE_EVENT_CALLBACK:
  *
diff --git a/src/driver-nodedev.h b/src/driver-nodedev.h
index e18029ab48..d00dd5845c 100644
--- a/src/driver-nodedev.h
+++ b/src/driver-nodedev.h
@@ -82,6 +82,9 @@ typedef virNodeDevicePtr
 typedef int
 (*virDrvNodeDeviceUndefine)(virNodeDevicePtr dev);
 
+typedef int
+(*virDrvNodeDeviceCreate)(virNodeDevicePtr def);
+
 typedef int
 (*virDrvConnectNodeDeviceEventRegisterAny)(virConnectPtr conn,
                                            virNodeDevicePtr dev,
@@ -123,4 +126,5 @@ struct _virNodeDeviceDriver {
     virDrvNodeDeviceDestroy nodeDeviceDestroy;
     virDrvNodeDeviceDefineXML nodeDeviceDefineXML;
     virDrvNodeDeviceUndefine nodeDeviceUndefine;
+    virDrvNodeDeviceCreate nodeDeviceCreate;
 };
diff --git a/src/libvirt-nodedev.c b/src/libvirt-nodedev.c
index a31f2bd124..7551b701e5 100644
--- a/src/libvirt-nodedev.c
+++ b/src/libvirt-nodedev.c
@@ -815,6 +815,41 @@ virNodeDeviceUndefine(virNodeDevicePtr dev)
 }
 
 
+/**
+ * virNodeDeviceCreate:
+ * @dev: a device object
+ *
+ * Start a defined node device:
+ *
+ * Returns 0 in case of success and -1 in case of failure.
+ */
+int
+virNodeDeviceCreate(virNodeDevicePtr dev)
+{
+    VIR_DEBUG("dev=%p", dev);
+
+    virResetLastError();
+
+    virCheckNodeDeviceReturn(dev, -1);
+    virCheckReadOnlyGoto(dev->conn->flags, error);
+
+    if (dev->conn->nodeDeviceDriver &&
+        dev->conn->nodeDeviceDriver->nodeDeviceCreate) {
+        int retval = dev->conn->nodeDeviceDriver->nodeDeviceCreate(dev);
+        if (retval < 0)
+            goto error;
+
+        return 0;
+    }
+
+    virReportUnsupportedError();
+
+ error:
+    virDispatchError(dev->conn);
+    return -1;
+}
+
+
 /**
  * virConnectNodeDeviceEventRegisterAny:
  * @conn: pointer to the connection
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index 1dec85fa80..f799db4884 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -888,6 +888,7 @@ LIBVIRT_7.2.0 {
     global:
         virNodeDeviceDefineXML;
         virNodeDeviceUndefine;
+        virNodeDeviceCreate;
 } LIBVIRT_7.1.0;
 
 # .... define new API here using predicted next version number ....
diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c
index 4770801ebc..e89d317152 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -901,6 +901,18 @@ nodeDeviceGetMdevctlUndefineCommand(const char *uuid, char **errmsg)
     return cmd;
 }
 
+virCommandPtr
+nodeDeviceGetMdevctlCreateCommand(const char *uuid, char **errmsg)
+{
+    virCommandPtr cmd = virCommandNewArgList(MDEVCTL,
+                                             "start",
+                                             "-u",
+                                             uuid,
+                                             NULL);
+    virCommandSetErrorBuffer(cmd, errmsg);
+    return cmd;
+}
+
 static int
 virMdevctlStop(virNodeDeviceDefPtr def, char **errmsg)
 {
@@ -932,6 +944,21 @@ virMdevctlUndefine(virNodeDeviceDefPtr def, char **errmsg)
 }
 
 
+static int
+virMdevctlCreate(virNodeDeviceDefPtr def, char **errmsg)
+{
+    int status;
+    g_autoptr(virCommand) cmd = NULL;
+
+    cmd = nodeDeviceGetMdevctlCreateCommand(def->caps->data.mdev.uuid, errmsg);
+
+    if (virCommandRun(cmd, &status) < 0 || status != 0)
+        return -1;
+
+    return 0;
+}
+
+
 virCommandPtr
 nodeDeviceGetMdevctlListCommand(bool defined,
                                 char **output)
@@ -1257,6 +1284,46 @@ nodeDeviceUndefine(virNodeDevicePtr device)
 }
 
 
+int nodeDeviceCreate(virNodeDevicePtr device)
+{
+    int ret = -1;
+    virNodeDeviceObjPtr obj = NULL;
+    virNodeDeviceDefPtr def = NULL;
+
+    if (!(obj = nodeDeviceObjFindByName(device->name)))
+        return -1;
+
+    if (virNodeDeviceObjIsActive(obj)) {
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                       _("Device is already active"));
+        goto cleanup;
+    }
+    def = virNodeDeviceObjGetDef(obj);
+
+    if (virNodeDeviceCreateEnsureACL(device->conn, def) < 0)
+        goto cleanup;
+
+    if (nodeDeviceHasCapability(def, VIR_NODE_DEV_CAP_MDEV)) {
+        g_autofree char *errmsg = NULL;
+
+        if (virMdevctlCreate(def, &errmsg) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Unable to create mediated device: %s"),
+                           errmsg && errmsg[0] ? errmsg : "Unknown Error");
+            goto cleanup;
+        }
+        ret = 0;
+    } else {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Unsupported device type"));
+    }
+
+ cleanup:
+    virNodeDeviceObjEndAPI(&obj);
+    return ret;
+}
+
+
 int
 nodeConnectNodeDeviceEventRegisterAny(virConnectPtr conn,
                                       virNodeDevicePtr device,
diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_device_driver.h
index 52be69c5d7..93b45e5cc8 100644
--- a/src/node_device/node_device_driver.h
+++ b/src/node_device/node_device_driver.h
@@ -157,3 +157,10 @@ nodeDeviceGenerateName(virNodeDeviceDefPtr def,
 
 bool nodeDeviceDefCopyFromMdevctl(virNodeDeviceDefPtr dst,
                                   virNodeDeviceDefPtr src);
+
+virCommandPtr
+nodeDeviceGetMdevctlCreateCommand(const char *uuid,
+                                  char **errmsg);
+
+int
+nodeDeviceCreate(virNodeDevicePtr dev);
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index 6558d6ce57..43971a3e76 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -2318,6 +2318,7 @@ static virNodeDeviceDriver udevNodeDeviceDriver = {
     .nodeDeviceDestroy = nodeDeviceDestroy, /* 0.7.3 */
     .nodeDeviceDefineXML = nodeDeviceDefineXML, /* 7.2.0 */
     .nodeDeviceUndefine = nodeDeviceUndefine, /* 7.2.0 */
+    .nodeDeviceCreate = nodeDeviceCreate, /* 7.2.0 */
 };
 
 
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 5c2c1a1641..507cad273d 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -8701,6 +8701,7 @@ static virNodeDeviceDriver node_device_driver = {
     .nodeDeviceNumOfCaps = remoteNodeDeviceNumOfCaps, /* 0.5.0 */
     .nodeDeviceListCaps = remoteNodeDeviceListCaps, /* 0.5.0 */
     .nodeDeviceCreateXML = remoteNodeDeviceCreateXML, /* 0.6.3 */
+    .nodeDeviceCreate = remoteNodeDeviceCreate, /* 7.2.0 */
     .nodeDeviceDefineXML = remoteNodeDeviceDefineXML, /* 7.2.0 */
     .nodeDeviceUndefine = remoteNodeDeviceUndefine, /* 7.2.0 */
     .nodeDeviceDestroy = remoteNodeDeviceDestroy /* 0.6.3 */
diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x
index 43f5c0cf36..755ba91676 100644
--- a/src/remote/remote_protocol.x
+++ b/src/remote/remote_protocol.x
@@ -2158,6 +2158,10 @@ struct remote_node_device_undefine_args {
     remote_nonnull_string name;
 };
 
+struct remote_node_device_create_args {
+    remote_nonnull_string name;
+};
+
 
 /*
  * Events Register/Deregister:
@@ -6759,6 +6763,13 @@ enum remote_procedure {
      * @priority: high
      * @acl: node_device:delete
      */
-    REMOTE_PROC_NODE_DEVICE_UNDEFINE = 428
+    REMOTE_PROC_NODE_DEVICE_UNDEFINE = 428,
+
+    /**
+     * @generate: both
+     * @priority: high
+     * @acl: node_device:start
+     */
+    REMOTE_PROC_NODE_DEVICE_CREATE = 429
 
 };
diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs
index b9074bbad0..87e0c40fac 100644
--- a/src/remote_protocol-structs
+++ b/src/remote_protocol-structs
@@ -1610,6 +1610,9 @@ struct remote_node_device_define_xml_ret {
 struct remote_node_device_undefine_args {
         remote_nonnull_string      name;
 };
+struct remote_node_device_create_args {
+        remote_nonnull_string      name;
+};
 struct remote_connect_domain_event_register_ret {
         int                        cb_registered;
 };
@@ -3611,4 +3614,5 @@ enum remote_procedure {
         REMOTE_PROC_DOMAIN_GET_MESSAGES = 426,
         REMOTE_PROC_NODE_DEVICE_DEFINE_XML = 427,
         REMOTE_PROC_NODE_DEVICE_UNDEFINE = 428,
+        REMOTE_PROC_NODE_DEVICE_CREATE = 429,
 };
diff --git a/tests/nodedevmdevctldata/mdevctl-create.argv b/tests/nodedevmdevctldata/mdevctl-create.argv
new file mode 100644
index 0000000000..802109340c
--- /dev/null
+++ b/tests/nodedevmdevctldata/mdevctl-create.argv
@@ -0,0 +1 @@
+$MDEVCTL_BINARY$ start -u 8a05ad83-3472-497d-8631-8142f31460e8
diff --git a/tests/nodedevmdevctltest.c b/tests/nodedevmdevctltest.c
index 577359cc21..324c882b53 100644
--- a/tests/nodedevmdevctltest.c
+++ b/tests/nodedevmdevctltest.c
@@ -14,7 +14,8 @@ typedef enum {
     MDEVCTL_CMD_START,
     MDEVCTL_CMD_STOP,
     MDEVCTL_CMD_DEFINE,
-    MDEVCTL_CMD_UNDEFINE
+    MDEVCTL_CMD_UNDEFINE,
+    MDEVCTL_CMD_CREATE,
 } MdevctlCmd;
 
 struct startTestInfo {
@@ -188,6 +189,9 @@ testMdevctlUuidCommandHelper(const void *data)
     } else if (info->command == MDEVCTL_CMD_UNDEFINE) {
         cmd = "undefine";
         func = nodeDeviceGetMdevctlUndefineCommand;
+    }else if (info->command == MDEVCTL_CMD_CREATE) {
+        cmd = "create";
+        func = nodeDeviceGetMdevctlCreateCommand;
     } else {
         return -1;
     }
@@ -428,6 +432,9 @@ mymain(void)
 #define DO_TEST_UNDEFINE(uuid) \
     DO_TEST_UUID_COMMAND_FULL("mdevctl undefine " uuid, uuid, MDEVCTL_CMD_UNDEFINE)
 
+#define DO_TEST_CREATE(uuid) \
+    DO_TEST_UUID_COMMAND_FULL("mdevctl create " uuid, uuid, MDEVCTL_CMD_CREATE)
+
 #define DO_TEST_LIST_DEFINED() \
     DO_TEST_FULL("mdevctl list --defined", testMdevctlListDefined, NULL)
 
@@ -452,6 +459,8 @@ mymain(void)
 
     DO_TEST_UNDEFINE("d76a6b78-45ed-4149-a325-005f9abc5281");
 
+    DO_TEST_CREATE("8a05ad83-3472-497d-8631-8142f31460e8");
+
  done:
     nodedevTestDriverFree(driver);
 
-- 
2.26.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux