]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
libnvme: Introduce functions to generate host identifier and host NQN
authorIsrael Rukshin <israelr@nvidia.com>
Thu, 18 Apr 2024 14:24:37 +0000 (14:24 +0000)
committerDaniel Wagner <dwagner@suse.de>
Wed, 22 May 2024 12:45:19 +0000 (14:45 +0200)
The nvmf_hostid_generate() function generates a machine specific
host identifier. This is useful when the host ID can't be derived
from an NQN that doesn't contain a UUID.
Also, add nvmf_hostnqn_generate_from_hostid() to explicitly set UUID
when hostid is not NULL.

Signed-off-by: Israel Rukshin <israelr@nvidia.com>
Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>
src/libnvme.map
src/nvme/fabrics.c
src/nvme/fabrics.h

index 748c4f7a39a00af4f700036e3b95cae783d7f139..44c5c860bd622d213d48141fb543499d50f80ccb 100644 (file)
@@ -4,6 +4,8 @@ LIBNVME_1.10 {
                nvme_init_default_logging;
                nvme_parse_uri;
                nvme_free_uri;
+               nvmf_hostid_generate;
+               nvmf_hostnqn_generate_from_hostid;
 };
 
 LIBNVME_1.9 {
index e5921f8b48a3b14e600882610e5e9a54265e2c0e..f06ad7974bdd473043e4b5a42c9041828114472a 100644 (file)
@@ -1342,27 +1342,42 @@ static int uuid_from_dmi(char *system_uuid)
        return ret;
 }
 
-char *nvmf_hostnqn_generate()
+char *nvmf_hostid_generate()
 {
-       char *hostnqn;
        int ret;
        char uuid_str[NVME_UUID_LEN_STRING];
        unsigned char uuid[NVME_UUID_LEN];
 
        ret = uuid_from_dmi(uuid_str);
-       if (ret < 0) {
+       if (ret < 0)
                ret = uuid_from_device_tree(uuid_str);
-       }
        if (ret < 0) {
                if (nvme_uuid_random(uuid) < 0)
                        memset(uuid, 0, NVME_UUID_LEN);
                nvme_uuid_to_string(uuid, uuid_str);
        }
 
-       if (asprintf(&hostnqn, "nqn.2014-08.org.nvmexpress:uuid:%s", uuid_str) < 0)
-               return NULL;
+       return strdup(uuid_str);
+}
+
+char *nvmf_hostnqn_generate_from_hostid(char *hostid)
+{
+       char *hid = NULL;
+       char *hostnqn;
+       int ret;
+
+       if (!hostid)
+               hostid = hid = nvmf_hostid_generate();
+
+       ret = asprintf(&hostnqn, "nqn.2014-08.org.nvmexpress:uuid:%s", hostid);
+       free(hid);
 
-       return hostnqn;
+       return (ret < 0) ? NULL : hostnqn;
+}
+
+char *nvmf_hostnqn_generate()
+{
+       return nvmf_hostnqn_generate_from_hostid(NULL);
 }
 
 static char *nvmf_read_file(const char *f, int len)
index 3be35310bc5b913d1af5d4f53a2e2c3a848d8a9f..8e26e9f16ef44bb3a8630dfd8d8e0f6baecd5db4 100644 (file)
@@ -279,6 +279,26 @@ struct nvmf_discovery_log *nvmf_get_discovery_wargs(struct nvme_get_discovery_ar
  */
 char *nvmf_hostnqn_generate();
 
+/**
+ * nvmf_hostnqn_generate_from_hostid() - Generate a host nqn from host identifier
+ * @hostid:            Host identifier
+ *
+ * If @hostid is NULL, the function generates it based on the machine
+ * identifier.
+ *
+ * Return: On success, an NVMe Qualified Name for host identification. This
+ * name is based on the given host identifier. On failure, NULL.
+ */
+char *nvmf_hostnqn_generate_from_hostid(char *hostid);
+
+/**
+ * nvmf_hostid_generate() - Generate a machine specific host identifier
+ *
+ * Return: On success, an identifier string based on the machine identifier to
+ * be used as NVMe Host Identifier, or NULL on failure.
+ */
+char *nvmf_hostid_generate();
+
 /**
  * nvmf_hostnqn_from_file() - Reads the host nvm qualified name from the config
  *                           default location