Skip to content

Commit 589e50a

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 <[email protected]>
1 parent 751d26d commit 589e50a

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
@@ -108,6 +108,11 @@ LIBNVME_2_0 {
108108
nvme_host_set_dhchap_key;
109109
nvme_host_set_hostsymname;
110110
nvme_host_set_pdc_enabled;
111+
nvme_hostid_from_file;
112+
nvme_hostid_generate;
113+
nvme_hostnqn_from_file;
114+
nvme_hostnqn_generate;
115+
nvme_hostnqn_generate_from_hostid;
111116
nvme_import_tls_key;
112117
nvme_import_tls_key_versioned;
113118
nvme_init_copy_range;
@@ -320,11 +325,6 @@ LIBNVME_2_0 {
320325
nvmf_get_default_trsvcid;
321326
nvmf_get_discovery_log;
322327
nvmf_get_discovery_wargs;
323-
nvmf_hostid_from_file;
324-
nvmf_hostid_generate;
325-
nvmf_hostnqn_from_file;
326-
nvmf_hostnqn_generate;
327-
nvmf_hostnqn_generate_from_hostid;
328328
nvmf_is_registration_supported;
329329
nvmf_nbft_free;
330330
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
/**
@@ -1391,244 +1386,6 @@ int nvmf_get_discovery_wargs(struct nvme_get_discovery_args *args,
13911386
return 0;
13921387
}
13931388

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

26772434
if (!fctx->hostnqn)
2678-
fctx->hostnqn = hnqn = nvmf_hostnqn_from_file();
2435+
fctx->hostnqn = hnqn = nvme_hostnqn_from_file();
26792436
if (!fctx->hostid && hnqn)
2680-
fctx->hostid = hid = nvmf_hostid_from_file();
2437+
fctx->hostid = hid = nvme_hostid_from_file();
26812438

26822439
h = nvme_lookup_host(ctx, fctx->hostnqn, fctx->hostid);
26832440
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)