[PATCH] libnvme: Introduce nvmf_hostid_generate function

Israel Rukshin israelr at nvidia.com
Mon Apr 15 23:32:56 PDT 2024


The 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 an host identifier parameter to nvmf_hostnqn_generate(),
to explicitly set UUID when it is not NULL.

Signed-off-by: Israel Rukshin <israelr at nvidia.com>
Reviewed-by: Max Gurtovoy <mgurtovoy at nvidia.com>
---
 src/libnvme.map    |  1 +
 src/nvme/fabrics.c | 24 +++++++++++++++++-------
 src/nvme/fabrics.h | 19 ++++++++++++++++---
 src/nvme/tree.c    |  2 +-
 4 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/src/libnvme.map b/src/libnvme.map
index 8710c41f..787cd6c0 100644
--- a/src/libnvme.map
+++ b/src/libnvme.map
@@ -10,6 +10,7 @@ LIBNVME_1.9 {
 		nvme_submit_passthru64;
 		nvme_update_key;
 		nvme_ctrl_get_cntlid;
+		nvmf_hostid_generate;
 };
 
 LIBNVME_1_8 {
diff --git a/src/nvme/fabrics.c b/src/nvme/fabrics.c
index 6738e9dc..684b2daf 100644
--- a/src/nvme/fabrics.c
+++ b/src/nvme/fabrics.c
@@ -1342,27 +1342,37 @@ 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(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;
 }
 
 static char *nvmf_read_file(const char *f, int len)
diff --git a/src/nvme/fabrics.h b/src/nvme/fabrics.h
index 4ebeb35e..bedef9a4 100644
--- a/src/nvme/fabrics.h
+++ b/src/nvme/fabrics.h
@@ -252,10 +252,23 @@ struct nvmf_discovery_log *nvmf_get_discovery_wargs(struct nvme_get_discovery_ar
 
 /**
  * nvmf_hostnqn_generate() - Generate a machine specific host nqn
- * Returns: An nvm namespace qualified name string based on the machine
- * identifier, or NULL if not successful.
+ * @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(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_hostnqn_generate();
+char *nvmf_hostid_generate();
 
 /**
  * nvmf_hostnqn_from_file() - Reads the host nvm qualified name from the config
diff --git a/src/nvme/tree.c b/src/nvme/tree.c
index 2218d5a8..b2b83bbb 100644
--- a/src/nvme/tree.c
+++ b/src/nvme/tree.c
@@ -125,7 +125,7 @@ nvme_host_t nvme_default_host(nvme_root_t r)
 
 	hostnqn = nvmf_hostnqn_from_file();
 	if (!hostnqn)
-		hostnqn = nvmf_hostnqn_generate();
+		hostnqn = nvmf_hostnqn_generate(NULL);
 	hostid = nvmf_hostid_from_file();
 
 	h = nvme_lookup_host(r, hostnqn, hostid);
-- 
2.18.2




More information about the Linux-nvme mailing list