Skip to content

Commit 0aad423

Browse files
committed
tree: add nvme_host_get
Replace nvme_default_host() with generic nvme_host_get(), which creates the host object on demand. This enables retiring nvme_lookup_host() from the public API. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 4888eaa commit 0aad423

7 files changed

Lines changed: 49 additions & 57 deletions

File tree

libnvme/examples/discover-loop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int main()
6262
ret = nvme_scan(NULL, &ctx);
6363
if (ret)
6464
return ret;
65-
ret = nvme_default_host(ctx, &h);
65+
ret = nvme_host_get(ctx, NULL, NULL, &h);
6666
if (ret) {
6767
fprintf(stderr, "Failed to allocated memory\n");
6868
return ret;

libnvme/libnvme/nvme.i

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,7 @@ struct nvme_ns {
556556
const char *hostkey = NULL,
557557
const char *hostsymname = NULL) {
558558
nvme_host_t h;
559-
if (hostnqn)
560-
h = nvme_lookup_host(ctx, hostnqn, hostid);
561-
if (nvme_default_host(ctx, &h))
559+
if (nvme_host_get(ctx, hostnqn, hostid, &h))
562560
return NULL;
563561
if (hostsymname)
564562
nvme_host_set_hostsymname(h, hostsymname);

libnvme/src/libnvme.ld

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ LIBNVME_2_0 {
5656
nvme_ctrl_set_tls_key_identity;
5757
nvme_ctrl_set_unique_discovery_ctrl;
5858
nvme_ctrls_filter;
59-
nvme_default_host;
6059
nvme_describe_key_serial;
6160
nvme_disconnect_ctrl;
6261
nvme_dump_config;
@@ -100,6 +99,7 @@ LIBNVME_2_0 {
10099
nvme_get_telemetry_max;
101100
nvme_get_uuid_list;
102101
nvme_get_version;
102+
nvme_host_get;
103103
nvme_host_get_dhchap_key;
104104
nvme_host_get_global_ctx;
105105
nvme_host_get_hostid;
@@ -127,7 +127,6 @@ LIBNVME_2_0 {
127127
nvme_insert_tls_key_versioned;
128128
nvme_ipaddrs_eq;
129129
nvme_lookup_ctrl;
130-
nvme_lookup_host;
131130
nvme_lookup_key;
132131
nvme_lookup_keyring;
133132
nvme_lookup_subsystem;

libnvme/src/nvme/private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ void *__nvme_alloc(size_t len);
411411

412412
void *__nvme_realloc(void *p, size_t len);
413413

414+
nvme_host_t nvme_lookup_host(struct nvme_global_ctx *ctx, const char *hostnqn,
415+
const char *hostid);
416+
414417
#if (LOG_FUNCNAME == 1)
415418
#define __nvme_log_func __func__
416419
#else

libnvme/src/nvme/tree.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,19 @@ int nvme_host_get_ids(struct nvme_global_ctx *ctx,
194194
return 0;
195195
}
196196

197-
int nvme_default_host(struct nvme_global_ctx *ctx, nvme_host_t *hp)
197+
int nvme_host_get(struct nvme_global_ctx *ctx, const char *hostnqn,
198+
const char *hostid, nvme_host_t *hp)
198199
{
199-
_cleanup_free_ char *hostnqn = NULL;
200-
_cleanup_free_ char *hostid = NULL;
200+
_cleanup_free_ char *hnqn = NULL;
201+
_cleanup_free_ char *hid = NULL;
201202
struct nvme_host *h;
202203
int err;
203204

204-
err = nvme_host_get_ids(ctx, NULL, NULL, &hostnqn, &hostid);
205+
err = nvme_host_get_ids(ctx, hostnqn, hostid, &hnqn, &hid);
205206
if (err)
206207
return err;
207208

208-
h = nvme_lookup_host(ctx, hostnqn, hostid);
209+
h = nvme_lookup_host(ctx, hnqn, hid);
209210

210211
nvme_host_set_hostsymname(h, NULL);
211212

@@ -888,7 +889,7 @@ static int nvme_scan_subsystem(struct nvme_global_ctx *ctx, const char *name)
888889
*/
889890
nvme_msg(ctx, LOG_DEBUG, "creating detached subsystem '%s'\n",
890891
name);
891-
ret = nvme_default_host(ctx, &h);
892+
ret = nvme_host_get(ctx, NULL, NULL, &h);
892893
if (ret)
893894
return ret;
894895
s = nvme_alloc_subsystem(h, name, subsysnqn);
@@ -2255,21 +2256,17 @@ int nvme_scan_ctrl(struct nvme_global_ctx *ctx, const char *name,
22552256

22562257
hostnqn = nvme_get_attr(path, "hostnqn");
22572258
hostid = nvme_get_attr(path, "hostid");
2258-
h = nvme_lookup_host(ctx, hostnqn, hostid);
2259-
if (h) {
2260-
host_key = nvme_get_attr(path, "dhchap_secret");
2261-
if (host_key && strcmp(host_key, "none")) {
2262-
free(h->dhchap_key);
2263-
h->dhchap_key = host_key;
2264-
host_key = NULL;
2265-
}
2266-
free(host_key);
2267-
}
2268-
if (!h) {
2269-
ret = nvme_default_host(ctx, &h);
2270-
if (ret)
2271-
return ret;
2259+
ret = nvme_host_get(ctx, hostnqn, hostid, &h);
2260+
if (ret)
2261+
return ret;
2262+
2263+
host_key = nvme_get_attr(path, "dhchap_secret");
2264+
if (host_key && strcmp(host_key, "none")) {
2265+
free(h->dhchap_key);
2266+
h->dhchap_key = host_key;
2267+
host_key = NULL;
22722268
}
2269+
free(host_key);
22732270

22742271
subsysnqn = nvme_get_attr(path, "subsysnqn");
22752272
if (!subsysnqn)

libnvme/src/nvme/tree.h

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,6 @@ nvme_host_t nvme_next_host(struct nvme_global_ctx *ctx, nvme_host_t h);
9696
*/
9797
struct nvme_global_ctx *nvme_host_get_global_ctx(nvme_host_t h);
9898

99-
/**
100-
* nvme_lookup_host() - Lookup nvme_host_t object
101-
* @ctx: struct nvme_global_ctx object
102-
* @hostnqn: Host NQN
103-
* @hostid: Host ID
104-
*
105-
* Lookup a nvme_host_t object based on @hostnqn and @hostid
106-
* or create one if not found.
107-
*
108-
* Return: &nvme_host_t object
109-
*/
110-
nvme_host_t nvme_lookup_host(struct nvme_global_ctx *ctx, const char *hostnqn,
111-
const char *hostid);
112-
11399
/**
114100
* nvme_host_get_dhchap_key() - Return host key
115101
* @h: Host for which the key should be returned
@@ -148,16 +134,19 @@ void nvme_host_set_pdc_enabled(nvme_host_t h, bool enabled);
148134
bool nvme_host_is_pdc_enabled(nvme_host_t h, bool fallback);
149135

150136
/**
151-
* nvme_default_host() - Initializes the default host
137+
* nvme_host_get() - Returns a host object
152138
* @ctx: struct nvme_global_ctx object
153-
* @h: &nvme_host_t object to return
139+
* @hostnqn: Host NQN (optional)
140+
* @hostid: Host ID (optional)
141+
* @h: &nvme_host_t object to return
154142
*
155-
* Initializes the default host object based on the hostnqn/hostid
156-
* values returned by nvme_host_get_ids() and attaches it to @r.
143+
* Returns a host object based on the hostnqn/hostid values or the default if
144+
* hostnqn/hostid are NULL.
157145
*
158146
* Return: 0 on success or negative error code otherwise
159147
*/
160-
int nvme_default_host(struct nvme_global_ctx *ctx, nvme_host_t *h);
148+
int nvme_host_get(struct nvme_global_ctx *ctx, const char *hostnqn,
149+
const char *hostid, nvme_host_t *h);
161150

162151
/**
163152
* nvme_host_get_ids - Retrieve host ids from various sources

libnvme/test/tree.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
struct test_data {
1919
/* input data */
20+
const char *hostnqn;
21+
const char *hostid;
2022
const char *subsysname;
2123
const char *subsysnqn;
2224
const char *transport;
@@ -31,23 +33,27 @@ struct test_data {
3133
int ctrl_id;
3234
};
3335

36+
#define DEFAULT_HOSTID "9ba1651a-ed36-11f0-9858-6c1ff71ba506"
37+
#define DEFAULT_HOSTNQN "nqn.2014-08.org.nvmexpress:uuid:DEFAULT_HOSTID"
3438
#define DEFAULT_SUBSYSNAME "subsysname"
3539
#define DEFAULT_SUBSYSNQN "subsysnqn"
3640
#define SRC_ADDR4 "192.168.56.100"
3741
#define SRC_ADDR6 "1234:5678:abcd:EF01:1234:5678:abcd:EF01"
3842

43+
#define DEFAULTS DEFAULT_HOSTNQN, DEFAULT_HOSTNQN, DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN
44+
3945
struct test_data test_data[] = {
40-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.1", "192.168.1.20", NULL, "4420" },
41-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.1", "192.168.1.20", NULL, "4421" },
42-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.2", "192.168.1.20", "eth1", "4420" },
43-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.2", "192.168.1.20", "eth1", "4421" },
44-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "rdma", "192.168.1.3", "192.168.1.20", NULL, NULL },
45-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "rdma", "192.168.1.4", "192.168.1.20", NULL, NULL },
46-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "fc",
46+
{ DEFAULTS, "tcp", "192.168.1.1", "192.168.1.20", NULL, "4420" },
47+
{ DEFAULTS, "tcp", "192.168.1.1", "192.168.1.20", NULL, "4421" },
48+
{ DEFAULTS, "tcp", "192.168.1.2", "192.168.1.20", "eth1", "4420" },
49+
{ DEFAULTS, "tcp", "192.168.1.2", "192.168.1.20", "eth1", "4421" },
50+
{ DEFAULTS, "rdma", "192.168.1.3", "192.168.1.20", NULL, NULL },
51+
{ DEFAULTS, "rdma", "192.168.1.4", "192.168.1.20", NULL, NULL },
52+
{ DEFAULTS, "fc",
4753
"nn-0x201700a09890f5bf:pn-0x201900a09890f5bf",
4854
"nn-0x200000109b579ef3:pn-0x100000109b579ef3"
4955
},
50-
{ DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "fc",
56+
{ DEFAULTS, "fc",
5157
"nn-0x201700a09890f5bf:pn-0x201900a09890f5bf",
5258
"nn-0x200000109b579ef6:pn-0x100000109b579ef6",
5359
},
@@ -125,7 +131,7 @@ static struct nvme_global_ctx *create_tree()
125131

126132
ctx = nvme_create_global_ctx(stdout, LOG_DEBUG);
127133
assert(ctx);
128-
nvme_default_host(ctx, &h);
134+
nvme_host_get(ctx, DEFAULT_HOSTNQN, DEFAULT_HOSTID, &h);
129135
assert(h);
130136

131137
printf(" ctrls created:\n");
@@ -281,7 +287,7 @@ static bool test_src_addr()
281287
ctx = nvme_create_global_ctx(stdout, LOG_DEBUG);
282288
assert(ctx);
283289

284-
nvme_default_host(ctx, &h);
290+
nvme_host_get(ctx, DEFAULT_HOSTNQN, DEFAULT_HOSTID, &h);
285291
assert(h);
286292

287293
s = nvme_lookup_subsystem(h, DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN);
@@ -457,7 +463,7 @@ static bool ctrl_match(const char *tag,
457463
ctx = nvme_create_global_ctx(stdout, LOG_INFO);
458464
assert(ctx);
459465

460-
nvme_default_host(ctx, &h);
466+
nvme_host_get(ctx, DEFAULT_HOSTNQN, DEFAULT_HOSTID, &h);
461467
assert(h);
462468

463469
s = nvme_lookup_subsystem(h, DEFAULT_SUBSYSNAME, reference->subsysnqn ? reference->subsysnqn : DEFAULT_SUBSYSNQN);
@@ -1070,7 +1076,7 @@ static bool ctrl_config_match(const char *tag,
10701076
ctx = nvme_create_global_ctx(stdout, LOG_INFO);
10711077
assert(ctx);
10721078

1073-
nvme_default_host(ctx, &h);
1079+
nvme_host_get(ctx, DEFAULT_HOSTNQN, DEFAULT_HOSTID, &h);
10741080
assert(h);
10751081

10761082
s = nvme_lookup_subsystem(h, DEFAULT_SUBSYSNAME, reference->subsysnqn ? reference->subsysnqn : DEFAULT_SUBSYSNQN);

0 commit comments

Comments
 (0)