Skip to content

Commit 71c25d1

Browse files
committed
util: Add simple UUID type
Provide our own UUID type as there is no point to have a dependency on libuuid just for a couple function we use. Signed-off-by: Daniel Wagner <[email protected]>
1 parent b394bdc commit 71c25d1

12 files changed

Lines changed: 241 additions & 38 deletions

File tree

meson.build

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ conf.set('PROJECT_VERSION', '"@0@"'.format(meson.project_version()))
5252

5353
conf.set('SYSCONFDIR', '"@0@"'.format(sysconfdir))
5454

55-
# Check for libuuid availability
56-
libuuid_dep = dependency('uuid', required: true, fallback : ['uuid', 'uuid_dep'])
57-
conf.set('CONFIG_LIBUUID', libuuid_dep.found(), description: 'Is libuuid required?')
58-
5955
# Check for json-c availability
6056
json_c_dep = dependency('json-c',
6157
version: '>=0.13',

src/libnvme.map

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ LIBNVME_1_2 {
77
nvmf_get_discovery_wargs;
88
nvme_get_feature_length2;
99
nvme_ctrl_is_persistent;
10+
nvme_uuid_from_string;
11+
nvme_uuid_to_string;
12+
nvme_uuid_random;
1013
};
1114

1215
LIBNVME_1_1 {

src/meson.build

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ if conf.get('CONFIG_JSONC')
2828
endif
2929

3030
deps = [
31-
libuuid_dep,
3231
json_c_dep,
3332
openssl_dep,
3433
]
3534

3635
mi_deps = [
37-
libuuid_dep,
3836
libsystemd_dep,
3937
]
4038

@@ -68,7 +66,6 @@ pkg.generate(libnvme,
6866
libnvme_dep = declare_dependency(
6967
include_directories: ['.'],
7068
dependencies: [
71-
libuuid_dep.partial_dependency(compile_args: true, includes: true),
7269
json_c_dep.partial_dependency(compile_args: true, includes: true),
7370
],
7471
link_with: libnvme,
@@ -88,9 +85,6 @@ libnvme_mi = library(
8885

8986
libnvme_mi_dep = declare_dependency(
9087
include_directories: ['.'],
91-
dependencies: [
92-
libuuid_dep.partial_dependency(compile_args: true, includes: true),
93-
],
9488
link_with: libnvme_mi,
9589
)
9690

@@ -107,9 +101,6 @@ libnvme_mi_test = library(
107101

108102
libnvme_mi_test_dep = declare_dependency(
109103
include_directories: ['.'],
110-
dependencies: [
111-
libuuid_dep.partial_dependency(compile_args: true, includes: true),
112-
],
113104
link_with: libnvme_mi_test,
114105
)
115106

src/nvme/fabrics.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include "private.h"
4040

4141
#define NVMF_HOSTID_SIZE 37
42-
#define UUID_SIZE 37 /* 1b4e28ba-2fa1-11d2-883f-0016d3cca427 + \0 */
4342

4443
#define NVMF_HOSTNQN_FILE SYSCONFDIR "/nvme/hostnqn"
4544
#define NVMF_HOSTID_FILE SYSCONFDIR "/nvme/hostid"
@@ -929,8 +928,8 @@ static int uuid_from_device_tree(char *system_uuid)
929928
if (f < 0)
930929
return -ENXIO;
931930

932-
memset(system_uuid, 0, UUID_SIZE);
933-
len = read(f, system_uuid, UUID_SIZE - 1);
931+
memset(system_uuid, 0, NVME_UUID_LEN_STRING);
932+
len = read(f, system_uuid, NVME_UUID_LEN_STRING - 1);
934933
close(f);
935934
if (len < 0)
936935
return -ENXIO;
@@ -1018,16 +1017,16 @@ static int uuid_from_product_uuid(char *system_uuid)
10181017
system_uuid[0] = '\0';
10191018

10201019
nread = getline(&line, &len, stream);
1021-
if (nread != UUID_SIZE) {
1020+
if (nread != NVME_UUID_LEN_STRING) {
10221021
ret = -ENXIO;
10231022
goto out;
10241023
}
10251024

10261025
/* The kernel is handling the byte swapping according DMTF
10271026
* SMBIOS 3.0 Section 7.2.1 System UUID */
10281027

1029-
memcpy(system_uuid, line, UUID_SIZE - 1);
1030-
system_uuid[UUID_SIZE - 1] = '\0';
1028+
memcpy(system_uuid, line, NVME_UUID_LEN_STRING - 1);
1029+
system_uuid[NVME_UUID_LEN_STRING - 1] = '\0';
10311030

10321031
ret = 0;
10331032

@@ -1063,16 +1062,17 @@ char *nvmf_hostnqn_generate()
10631062
{
10641063
char *hostnqn;
10651064
int ret;
1066-
char uuid_str[UUID_SIZE];
1067-
uuid_t uuid;
1065+
char uuid_str[NVME_UUID_LEN_STRING];
1066+
unsigned char uuid[NVME_UUID_LEN];
10681067

10691068
ret = uuid_from_dmi(uuid_str);
10701069
if (ret < 0) {
10711070
ret = uuid_from_device_tree(uuid_str);
10721071
}
10731072
if (ret < 0) {
1074-
uuid_generate_random(uuid);
1075-
uuid_unparse_lower(uuid, uuid_str);
1073+
if (nvme_uuid_random(uuid) < 0)
1074+
memset(uuid, 0, NVME_UUID_LEN);
1075+
nvme_uuid_to_string(uuid, uuid_str);
10761076
}
10771077

10781078
if (asprintf(&hostnqn, "nqn.2014-08.org.nvmexpress:uuid:%s", uuid_str) < 0)
@@ -1125,7 +1125,7 @@ static __u32 nvmf_get_tel(const char *hostsymname)
11251125
__u16 len;
11261126

11271127
/* Host ID is mandatory */
1128-
tel += nvmf_exat_size(sizeof(uuid_t));
1128+
tel += nvmf_exat_size(NVME_UUID_LEN_STRING);
11291129

11301130
/* Symbolic name is optional */
11311131
len = hostsymname ? strlen(hostsymname) : 0;
@@ -1169,8 +1169,8 @@ static void nvmf_fill_die(struct nvmf_ext_die *die, struct nvme_host *h,
11691169
numexat++;
11701170
exat = die->exat;
11711171
exat->exattype = cpu_to_le16(NVMF_EXATTYPE_HOSTID);
1172-
exat->exatlen = cpu_to_le16(nvmf_exat_len(sizeof(uuid_t)));
1173-
uuid_parse(h->hostid, exat->exatval);
1172+
exat->exatlen = cpu_to_le16(nvmf_exat_len(NVME_UUID_LEN));
1173+
nvme_uuid_from_string(h->hostid, exat->exatval);
11741174

11751175
/* Extended Attribute for the Symbolic Name (optional) */
11761176
symname_len = h->hostsymname ? strlen(h->hostsymname) : 0;

src/nvme/private.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include "fabrics.h"
1717
#include "mi.h"
1818

19-
#include <uuid.h>
20-
2119

2220
extern const char *nvme_ctrl_sysfs_dir;
2321
extern const char *nvme_subsys_sysfs_dir;
@@ -57,7 +55,7 @@ struct nvme_ns {
5755

5856
uint8_t eui64[8];
5957
uint8_t nguid[16];
60-
uuid_t uuid;
58+
unsigned char uuid[NVME_UUID_LEN];
6159
enum nvme_csi csi;
6260
};
6361

src/nvme/tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,9 +1551,9 @@ const uint8_t *nvme_ns_get_nguid(nvme_ns_t n)
15511551
return n->nguid;
15521552
}
15531553

1554-
void nvme_ns_get_uuid(nvme_ns_t n, uuid_t out)
1554+
void nvme_ns_get_uuid(nvme_ns_t n, unsigned char out[NVME_UUID_LEN])
15551555
{
1556-
uuid_copy(out, n->uuid);
1556+
memcpy(out, n->uuid, NVME_UUID_LEN);
15571557
}
15581558

15591559
int nvme_ns_identify(nvme_ns_t n, struct nvme_id_ns *ns)

src/nvme/tree.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <stddef.h>
1616

1717
#include <sys/types.h>
18-
#include <uuid.h>
1918

2019
#include "ioctl.h"
2120
#include "util.h"
@@ -521,7 +520,7 @@ const uint8_t *nvme_ns_get_nguid(nvme_ns_t n);
521520
*
522521
* Copies the namespace's uuid into @out
523522
*/
524-
void nvme_ns_get_uuid(nvme_ns_t n, uuid_t out);
523+
void nvme_ns_get_uuid(nvme_ns_t n, unsigned char out[NVME_UUID_LEN]);
525524

526525
/**
527526
* nvme_ns_get_sysfs_dir() - sysfs directory of a namespace

src/nvme/util.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <string.h>
1212
#include <errno.h>
1313

14+
#include <sys/stat.h>
15+
#include <fcntl.h>
1416
#include <sys/param.h>
1517
#include <sys/types.h>
1618
#include <arpa/inet.h>
@@ -833,3 +835,57 @@ const char *nvme_get_version(enum nvme_version type)
833835
return "n/a";
834836
}
835837
}
838+
839+
int nvme_uuid_to_string(unsigned char uuid[NVME_UUID_LEN], char *str)
840+
{
841+
int n;
842+
n = snprintf(str, NVME_UUID_LEN_STRING,
843+
"%02x%02x%02x%02x-%02x%02x-%02x%02x-"
844+
"%02x%02x-%02x%02x%02x%02x%02x%02x",
845+
uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
846+
uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
847+
uuid[12], uuid[13], uuid[14], uuid[15]);
848+
return n != NVME_UUID_LEN_STRING - 1 ? -EINVAL : 0;
849+
}
850+
851+
int nvme_uuid_from_string(const char *str, unsigned char uuid[NVME_UUID_LEN])
852+
{
853+
int n;
854+
855+
n = sscanf(str,
856+
"%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-"
857+
"%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
858+
&uuid[0], &uuid[1], &uuid[2], &uuid[3], &uuid[4], &uuid[5],
859+
&uuid[6], &uuid[7], &uuid[8], &uuid[9], &uuid[10], &uuid[11],
860+
&uuid[12], &uuid[13], &uuid[14], &uuid[15]);
861+
return n != NVME_UUID_LEN ? -EINVAL : 0;
862+
863+
}
864+
865+
int nvme_uuid_random(unsigned char uuid[NVME_UUID_LEN])
866+
{
867+
int f;
868+
ssize_t n;
869+
870+
f = open("/dev/urandom", O_RDONLY);
871+
if (f < 0)
872+
return -errno;
873+
n = read(f, uuid, NVME_UUID_LEN);
874+
if (n < 0) {
875+
close(f);
876+
return -errno;
877+
} else if (n != NVME_UUID_LEN) {
878+
close(f);
879+
return -EIO;
880+
}
881+
882+
/*
883+
* See https://www.rfc-editor.org/rfc/rfc4122#section-4.4
884+
* Algorithms for Creating a UUID from Truly Random
885+
* or Pseudo-Random Numbers
886+
*/
887+
uuid[6] = (uuid[6] & 0x0f) | 0x40;
888+
uuid[8] = (uuid[8] & 0x3f) | 0x80;
889+
890+
return 0;
891+
}

src/nvme/util.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,36 @@ enum nvme_version {
606606
*/
607607
const char *nvme_get_version(enum nvme_version type);
608608

609+
#define NVME_UUID_LEN_STRING 37 /* 1b4e28ba-2fa1-11d2-883f-0016d3cca427 + \0 */
610+
#define NVME_UUID_LEN 16
611+
612+
/**
613+
* nvme_uuid_to_string - Return string represenation of encoded UUID
614+
* @uuid: Binary encoded input UUID
615+
* @str: Output string represenation of UUID
616+
*
617+
* Return: Returns error code if type conversion fails.
618+
*/
619+
int nvme_uuid_to_string(unsigned char uuid[NVME_UUID_LEN], char *str);
620+
621+
/**
622+
* nvme_uuid_from_string - Return encoded UUID represenation of string UUID
623+
* @uuid: Binary encoded input UUID
624+
* @str: Output string represenation of UUID
625+
*
626+
* Return: Returns error code if type conversion fails.
627+
*/
628+
int nvme_uuid_from_string(const char *str, unsigned char uuid[NVME_UUID_LEN]);
629+
630+
/**
631+
* nvme_uuid_random - Generate random UUID
632+
* @uuid: Generated random UUID
633+
*
634+
* Generate random number according
635+
* https://www.rfc-editor.org/rfc/rfc4122#section-4.4
636+
*
637+
* Return: Returns error code if generating of random number fails.
638+
*/
639+
int nvme_uuid_random(unsigned char uuid[NVME_UUID_LEN]);
640+
609641
#endif /* _LIBNVME_UTIL_H */

test/meson.build

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
main = executable(
1313
'main-test',
1414
['test.c'],
15-
dependencies: [libnvme_dep, libuuid_dep],
15+
dependencies: [libnvme_dep],
1616
include_directories: [incdir, internal_incdir]
1717
)
1818

@@ -56,3 +56,12 @@ mi_mctp = executable(
5656
)
5757

5858
test('mi-mctp', mi_mctp)
59+
60+
uuid = executable(
61+
'test-uuid',
62+
['uuid.c'],
63+
dependencies: libnvme_dep,
64+
include_directories: [incdir, internal_incdir]
65+
)
66+
67+
test('uuid', uuid)

0 commit comments

Comments
 (0)