Skip to content

Commit fb51505

Browse files
committed
linux: move the hostid/hostnqn function
The hostid and hostnqn are used through out the library and are not fabric specific. Because tree.c depends on it move it to linux.c, so it's possible to make the fabrics code optional. Signed-off-by: Daniel Wagner <wagi@kernel.org>
1 parent 6da12d0 commit fb51505

8 files changed

Lines changed: 314 additions & 313 deletions

File tree

libnvme/libnvme/nvme.i

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
Py_XDECREF(val); /* .. therefore decrement ref. count. */
4040
}
4141
PyObject *hostnqn_from_file() {
42-
char * val = nvmf_hostnqn_from_file();
42+
char * val = nvme_hostnqn_from_file();
4343
PyObject * obj = PyUnicode_FromString(val);
4444
free(val);
4545
return obj;
4646
}
4747
PyObject *hostid_from_file() {
48-
char * val = nvmf_hostid_from_file();
48+
char * val = nvme_hostid_from_file();
4949
PyObject * obj = PyUnicode_FromString(val);
5050
free(val);
5151
return obj;

libnvme/src/libnvme.ld

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ LIBNVME_2_0 {
7777
nvme_host_is_pdc_enabled;
7878
nvme_host_release_fds;
7979
nvme_host_set_pdc_enabled;
80+
nvme_hostid_from_file;
81+
nvme_hostid_generate;
82+
nvme_hostnqn_from_file;
83+
nvme_hostnqn_generate;
84+
nvme_hostnqn_generate_from_hostid;
8085
nvme_import_tls_key;
8186
nvme_import_tls_key_versioned;
8287
nvme_init_copy_range;
@@ -271,11 +276,6 @@ LIBNVME_2_0 {
271276
nvmf_get_default_trsvcid;
272277
nvmf_get_discovery_log;
273278
nvmf_get_discovery_wargs;
274-
nvmf_hostid_from_file;
275-
nvmf_hostid_generate;
276-
nvmf_hostnqn_from_file;
277-
nvmf_hostnqn_generate;
278-
nvmf_hostnqn_generate_from_hostid;
279279
nvmf_is_registration_supported;
280280
nvmf_nbft_free;
281281
nvmf_nbft_read_files;

libnvme/src/nvme/fabrics.c

Lines changed: 2 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737
#include "cleanup.h"
3838
#include "private.h"
3939

40-
#define NVMF_HOSTID_SIZE 37
41-
42-
#define NVMF_HOSTNQN_FILE SYSCONFDIR "/nvme/hostnqn"
43-
#define NVMF_HOSTID_FILE SYSCONFDIR "/nvme/hostid"
44-
4540
const char *nvmf_dev = "/dev/nvme-fabrics";
4641

4742
/**
@@ -1384,244 +1379,6 @@ int nvmf_get_discovery_wargs(struct nvme_get_discovery_args *args,
13841379
return 0;
13851380
}
13861381

1387-
static int uuid_from_device_tree(char *system_uuid)
1388-
{
1389-
_cleanup_fd_ int f = -1;
1390-
ssize_t len;
1391-
1392-
f = open(nvme_uuid_ibm_filename(), O_RDONLY);
1393-
if (f < 0)
1394-
return -ENXIO;
1395-
1396-
memset(system_uuid, 0, NVME_UUID_LEN_STRING);
1397-
len = read(f, system_uuid, NVME_UUID_LEN_STRING - 1);
1398-
if (len < 0)
1399-
return -ENXIO;
1400-
1401-
return strlen(system_uuid) ? 0 : -ENXIO;
1402-
}
1403-
1404-
/*
1405-
* See System Management BIOS (SMBIOS) Reference Specification
1406-
* https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.2.0.pdf
1407-
*/
1408-
#define DMI_SYSTEM_INFORMATION 1
1409-
1410-
static bool is_dmi_uuid_valid(const char *buf, size_t len)
1411-
{
1412-
int i;
1413-
1414-
/* UUID bytes are from byte 8 to 23 */
1415-
if (len < 24)
1416-
return false;
1417-
1418-
/* Test it's a invalid UUID with all zeros */
1419-
for (i = 8; i < 24; i++) {
1420-
if (buf[i])
1421-
break;
1422-
}
1423-
if (i == 24)
1424-
return false;
1425-
1426-
return true;
1427-
}
1428-
1429-
static int uuid_from_dmi_entries(char *system_uuid)
1430-
{
1431-
_cleanup_dir_ DIR *d = NULL;
1432-
const char *entries_dir = nvme_dmi_entries_dir();
1433-
int f;
1434-
struct dirent *de;
1435-
char buf[512] = {0};
1436-
1437-
system_uuid[0] = '\0';
1438-
d = opendir(entries_dir);
1439-
if (!d)
1440-
return -ENXIO;
1441-
while ((de = readdir(d))) {
1442-
char filename[PATH_MAX];
1443-
int len, type;
1444-
1445-
if (de->d_name[0] == '.')
1446-
continue;
1447-
sprintf(filename, "%s/%s/type", entries_dir, de->d_name);
1448-
f = open(filename, O_RDONLY);
1449-
if (f < 0)
1450-
continue;
1451-
len = read(f, buf, 512);
1452-
close(f);
1453-
if (len <= 0)
1454-
continue;
1455-
if (sscanf(buf, "%d", &type) != 1)
1456-
continue;
1457-
if (type != DMI_SYSTEM_INFORMATION)
1458-
continue;
1459-
sprintf(filename, "%s/%s/raw", entries_dir, de->d_name);
1460-
f = open(filename, O_RDONLY);
1461-
if (f < 0)
1462-
continue;
1463-
len = read(f, buf, 512);
1464-
close(f);
1465-
if (len <= 0)
1466-
continue;
1467-
1468-
if (!is_dmi_uuid_valid(buf, len))
1469-
continue;
1470-
1471-
/* Sigh. https://en.wikipedia.org/wiki/Overengineering */
1472-
/* DMTF SMBIOS 3.0 Section 7.2.1 System UUID */
1473-
sprintf(system_uuid,
1474-
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
1475-
"%02x%02x%02x%02x%02x%02x",
1476-
(uint8_t)buf[8 + 3], (uint8_t)buf[8 + 2],
1477-
(uint8_t)buf[8 + 1], (uint8_t)buf[8 + 0],
1478-
(uint8_t)buf[8 + 5], (uint8_t)buf[8 + 4],
1479-
(uint8_t)buf[8 + 7], (uint8_t)buf[8 + 6],
1480-
(uint8_t)buf[8 + 8], (uint8_t)buf[8 + 9],
1481-
(uint8_t)buf[8 + 10], (uint8_t)buf[8 + 11],
1482-
(uint8_t)buf[8 + 12], (uint8_t)buf[8 + 13],
1483-
(uint8_t)buf[8 + 14], (uint8_t)buf[8 + 15]);
1484-
break;
1485-
}
1486-
return strlen(system_uuid) ? 0 : -ENXIO;
1487-
}
1488-
1489-
#define PATH_DMI_PROD_UUID "/sys/class/dmi/id/product_uuid"
1490-
1491-
/**
1492-
* uuid_from_product_uuid() - Get system UUID from product_uuid
1493-
* @system_uuid: Where to save the system UUID.
1494-
*
1495-
* Return: 0 on success, -ENXIO otherwise.
1496-
*/
1497-
static int uuid_from_product_uuid(char *system_uuid)
1498-
{
1499-
_cleanup_file_ FILE *stream = NULL;
1500-
ssize_t nread;
1501-
_cleanup_free_ char *line = NULL;
1502-
size_t len = 0;
1503-
1504-
stream = fopen(PATH_DMI_PROD_UUID, "re");
1505-
if (!stream)
1506-
return -ENXIO;
1507-
system_uuid[0] = '\0';
1508-
1509-
nread = getline(&line, &len, stream);
1510-
if (nread != NVME_UUID_LEN_STRING)
1511-
return -ENXIO;
1512-
1513-
/* The kernel is handling the byte swapping according DMTF
1514-
* SMBIOS 3.0 Section 7.2.1 System UUID */
1515-
1516-
memcpy(system_uuid, line, NVME_UUID_LEN_STRING - 1);
1517-
system_uuid[NVME_UUID_LEN_STRING - 1] = '\0';
1518-
1519-
return 0;
1520-
}
1521-
1522-
/**
1523-
* uuid_from_dmi() - read system UUID
1524-
* @system_uuid: buffer for the UUID
1525-
*
1526-
* The system UUID can be read from two different locations:
1527-
*
1528-
* 1) /sys/class/dmi/id/product_uuid
1529-
* 2) /sys/firmware/dmi/entries
1530-
*
1531-
* Note that the second location is not present on Debian-based systems.
1532-
*
1533-
* Return: 0 on success, negative errno otherwise.
1534-
*/
1535-
static int uuid_from_dmi(char *system_uuid)
1536-
{
1537-
int ret = uuid_from_product_uuid(system_uuid);
1538-
if (ret != 0)
1539-
ret = uuid_from_dmi_entries(system_uuid);
1540-
return ret;
1541-
}
1542-
1543-
char *nvmf_hostid_generate()
1544-
{
1545-
int ret;
1546-
char uuid_str[NVME_UUID_LEN_STRING];
1547-
unsigned char uuid[NVME_UUID_LEN];
1548-
1549-
ret = uuid_from_dmi(uuid_str);
1550-
if (ret < 0)
1551-
ret = uuid_from_device_tree(uuid_str);
1552-
if (ret < 0) {
1553-
if (nvme_uuid_random(uuid) < 0)
1554-
memset(uuid, 0, NVME_UUID_LEN);
1555-
nvme_uuid_to_string(uuid, uuid_str);
1556-
}
1557-
1558-
return strdup(uuid_str);
1559-
}
1560-
1561-
char *nvmf_hostnqn_generate_from_hostid(char *hostid)
1562-
{
1563-
char *hid = NULL;
1564-
char *hostnqn;
1565-
int ret;
1566-
1567-
if (!hostid)
1568-
hostid = hid = nvmf_hostid_generate();
1569-
1570-
ret = asprintf(&hostnqn, "nqn.2014-08.org.nvmexpress:uuid:%s", hostid);
1571-
free(hid);
1572-
1573-
return (ret < 0) ? NULL : hostnqn;
1574-
}
1575-
1576-
char *nvmf_hostnqn_generate()
1577-
{
1578-
return nvmf_hostnqn_generate_from_hostid(NULL);
1579-
}
1580-
1581-
static char *nvmf_read_file(const char *f, int len)
1582-
{
1583-
char buf[len];
1584-
_cleanup_fd_ int fd = -1;
1585-
int ret;
1586-
1587-
fd = open(f, O_RDONLY);
1588-
if (fd < 0)
1589-
return NULL;
1590-
1591-
memset(buf, 0, len);
1592-
ret = read(fd, buf, len - 1);
1593-
1594-
if (ret < 0 || !strlen(buf))
1595-
return NULL;
1596-
return strndup(buf, strcspn(buf, "\n"));
1597-
}
1598-
1599-
char *nvmf_hostnqn_from_file()
1600-
{
1601-
char *hostnqn = getenv("LIBNVME_HOSTNQN");
1602-
1603-
if (hostnqn) {
1604-
if (!strcmp(hostnqn, ""))
1605-
return NULL;
1606-
return strdup(hostnqn);
1607-
}
1608-
1609-
return nvmf_read_file(NVMF_HOSTNQN_FILE, NVMF_NQN_SIZE);
1610-
}
1611-
1612-
char *nvmf_hostid_from_file()
1613-
{
1614-
char *hostid = getenv("LIBNVME_HOSTID");
1615-
1616-
if (hostid) {
1617-
if (!strcmp(hostid, ""))
1618-
return NULL;
1619-
return strdup(hostid);
1620-
}
1621-
1622-
return nvmf_read_file(NVMF_HOSTID_FILE, NVMF_HOSTID_SIZE);
1623-
}
1624-
16251382
/**
16261383
* nvmf_get_tel() - Calculate the amount of memory needed for a DIE.
16271384
* @hostsymname: Symbolic name (may be NULL)
@@ -2668,9 +2425,9 @@ int nvmf_config_modify(struct nvme_global_ctx *ctx,
26682425
struct nvme_ctrl *c;
26692426

26702427
if (!fctx->hostnqn)
2671-
fctx->hostnqn = hnqn = nvmf_hostnqn_from_file();
2428+
fctx->hostnqn = hnqn = nvme_hostnqn_from_file();
26722429
if (!fctx->hostid && hnqn)
2673-
fctx->hostid = hid = nvmf_hostid_from_file();
2430+
fctx->hostid = hid = nvme_hostid_from_file();
26742431

26752432
h = nvme_lookup_host(ctx, fctx->hostnqn, fctx->hostid);
26762433
if (!h) {

libnvme/src/nvme/fabrics.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -283,57 +283,6 @@ struct nvme_get_discovery_args {
283283
int nvmf_get_discovery_wargs(struct nvme_get_discovery_args *args,
284284
struct nvmf_discovery_log **log);
285285

286-
/**
287-
* nvmf_hostnqn_generate() - Generate a machine specific host nqn
288-
* Returns: An nvm namespace qualified name string based on the machine
289-
* identifier, or NULL if not successful.
290-
*/
291-
char *nvmf_hostnqn_generate();
292-
293-
/**
294-
* nvmf_hostnqn_generate_from_hostid() - Generate a host nqn from host identifier
295-
* @hostid: Host identifier
296-
*
297-
* If @hostid is NULL, the function generates it based on the machine
298-
* identifier.
299-
*
300-
* Return: On success, an NVMe Qualified Name for host identification. This
301-
* name is based on the given host identifier. On failure, NULL.
302-
*/
303-
char *nvmf_hostnqn_generate_from_hostid(char *hostid);
304-
305-
/**
306-
* nvmf_hostid_generate() - Generate a machine specific host identifier
307-
*
308-
* Return: On success, an identifier string based on the machine identifier to
309-
* be used as NVMe Host Identifier, or NULL on failure.
310-
*/
311-
char *nvmf_hostid_generate();
312-
313-
/**
314-
* nvmf_hostnqn_from_file() - Reads the host nvm qualified name from the config
315-
* default location
316-
*
317-
* Retrieve the qualified name from the config file located in $SYSCONFIDR/nvme.
318-
* $SYSCONFDIR is usually /etc.
319-
*
320-
* Return: The host nqn, or NULL if unsuccessful. If found, the caller
321-
* is responsible to free the string.
322-
*/
323-
char *nvmf_hostnqn_from_file();
324-
325-
/**
326-
* nvmf_hostid_from_file() - Reads the host identifier from the config default
327-
* location
328-
*
329-
* Retrieve the host idenditifer from the config file located in $SYSCONFDIR/nvme/.
330-
* $SYSCONFDIR is usually /etc.
331-
*
332-
* Return: The host identifier, or NULL if unsuccessful. If found, the caller
333-
* is responsible to free the string.
334-
*/
335-
char *nvmf_hostid_from_file();
336-
337286
/**
338287
* nvmf_is_registration_supported - check whether registration can be performed.
339288
* @c: Controller instance

0 commit comments

Comments
 (0)