Skip to content

Commit b547cdb

Browse files
committed
Implement 'dhchap_key' host and controller attributes
Implement a 'dhchap_key' attribute for the nvme_host structure to support the 'dhchap_secret' connection argument, and a 'dhchap_key' attribute for the nvme_controller structure to support the 'dhchap_ctrl_secret' connection argument. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 38d5f95 commit b547cdb

7 files changed

Lines changed: 103 additions & 2 deletions

File tree

libnvme/nvme.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ struct nvme_host {
268268
%immutable hostid;
269269
char *hostnqn;
270270
char *hostid;
271+
char *dhchap_key;
271272
};
272273

273274
struct nvme_subsystem {
@@ -301,6 +302,7 @@ struct nvme_ctrl {
301302
char *traddr;
302303
char *host_traddr;
303304
char *trsvcid;
305+
char *dhchap_key;
304306
char *address;
305307
char *firmware;
306308
char *model;

src/libnvme.map

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LIBNVME_1_0 {
1313
nvme_ctrl_first_ns;
1414
nvme_ctrl_first_path;
1515
nvme_ctrl_get_address;
16+
nvme_ctrl_get_dhchap_key;
1617
nvme_ctrl_get_discovery_ctrl;
1718
nvme_ctrl_get_fd;
1819
nvme_ctrl_get_firmware;
@@ -36,6 +37,7 @@ LIBNVME_1_0 {
3637
nvme_ctrl_next_ns;
3738
nvme_ctrl_next_path;
3839
nvme_ctrl_reset;
40+
nvme_ctrl_set_dhchap_key;
3941
nvme_ctrl_set_discovery_ctrl;
4042
nvme_ctrl_set_persistent;
4143
nvme_ctrls_filter;
@@ -136,9 +138,11 @@ LIBNVME_1_0 {
136138
nvme_get_property;
137139
nvme_get_subsys_attr;
138140
nvme_get_telemetry_log;
141+
nvme_host_get_dhchap_key;
139142
nvme_host_get_hostid;
140143
nvme_host_get_hostnqn;
141144
nvme_host_get_root;
145+
nvme_host_set_dhchap_key;
142146
nvme_identify;
143147
nvme_identify_active_ns_list;
144148
nvme_identify_allocated_ns;

src/nvme/fabrics.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
397397
{
398398
struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
399399
const char *transport = nvme_ctrl_get_transport(c);
400-
const char *hostnqn, *hostid;
400+
const char *hostnqn, *hostid, *hostkey, *ctrlkey;
401401
bool discover = false, discovery_nqn = false;
402402

403403
if (!transport) {
@@ -431,6 +431,8 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
431431
discover = true;
432432
hostnqn = nvme_host_get_hostnqn(h);
433433
hostid = nvme_host_get_hostid(h);
434+
hostkey = nvme_host_get_dhchap_key(h);
435+
ctrlkey = nvme_ctrl_get_dhchap_key(c);
434436
if (add_argument(argstr, "transport", transport) ||
435437
add_argument(argstr, "traddr",
436438
nvme_ctrl_get_traddr(c)) ||
@@ -444,6 +446,10 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
444446
(hostid && add_argument(argstr, "hostid", hostid)) ||
445447
(discover && !discovery_nqn &&
446448
add_bool_argument(argstr, "discovery", true)) ||
449+
(!discover && hostkey &&
450+
add_argument(argstr, "dhchap_secret", hostkey)) ||
451+
(!discover && ctrlkey &&
452+
add_argument(argstr, "dhchap_ctrl_secret", ctrlkey)) ||
447453
(!discover &&
448454
add_int_argument(argstr, "nr_io_queues",
449455
cfg->nr_io_queues, false)) ||

src/nvme/json.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj)
9797
c = nvme_lookup_ctrl(s, transport, traddr, host_traddr,
9898
host_iface, trsvcid);
9999
if (c) {
100+
attr_obj = json_object_object_get(port_obj, "dhchap_key");
101+
if (attr_obj)
102+
nvme_ctrl_set_dhchap_key(c, json_object_get_string(attr_obj));
100103
json_update_attributes(c, port_obj);
101104
}
102105
}
@@ -139,6 +142,9 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj)
139142
if (attr_obj)
140143
hostid = json_object_get_string(attr_obj);
141144
h = nvme_lookup_host(r, hostnqn, hostid);
145+
attr_obj = json_object_object_get(host_obj, "dhchap_key");
146+
if (attr_obj)
147+
nvme_host_set_dhchap_key(h, json_object_get_string(attr_obj));
142148
subsys_array = json_object_object_get(host_obj, "subsystems");
143149
if (!subsys_array)
144150
return;
@@ -194,6 +200,10 @@ static void json_update_port(struct json_object *ctrl_array, nvme_ctrl_t c)
194200
value = nvme_ctrl_get_trsvcid(c);
195201
if (value)
196202
json_object_add_value_string(port_obj, "trsvcid", value);
203+
value = nvme_ctrl_get_dhchap_key(c);
204+
if (value)
205+
json_object_add_value_string(port_obj, "dhchap_key",
206+
value);
197207
JSON_INT_OPTION(cfg, port_obj, nr_io_queues, 0);
198208
JSON_INT_OPTION(cfg, port_obj, nr_write_queues, 0);
199209
JSON_INT_OPTION(cfg, port_obj, nr_poll_queues, 0);
@@ -252,7 +262,7 @@ int json_update_config(nvme_root_t r, const char *config_file)
252262
json_root = json_object_new_array();
253263
nvme_for_each_host(r, h) {
254264
nvme_subsystem_t s;
255-
const char *hostid;
265+
const char *hostid, *dhchap_key;
256266

257267
host_obj = json_object_new_object();
258268
json_object_add_value_string(host_obj, "hostnqn",
@@ -261,6 +271,10 @@ int json_update_config(nvme_root_t r, const char *config_file)
261271
if (hostid)
262272
json_object_add_value_string(host_obj, "hostid",
263273
hostid);
274+
dhchap_key = nvme_host_get_dhchap_key(h);
275+
if (dhchap_key)
276+
json_object_add_value_string(host_obj, "dhchap_key",
277+
dhchap_key);
264278
subsys_array = json_object_new_array();
265279
nvme_for_each_subsystem(h, s) {
266280
json_update_subsys(subsys_array, s);

src/nvme/private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct nvme_ctrl {
8787
char *trsvcid;
8888
char *host_traddr;
8989
char *host_iface;
90+
char *dhchap_key;
9091
bool discovery_ctrl;
9192
bool discovered;
9293
bool persistent;
@@ -115,6 +116,7 @@ struct nvme_host {
115116

116117
char *hostnqn;
117118
char *hostid;
119+
char *dhchap_key;
118120
};
119121

120122
struct nvme_root {

src/nvme/tree.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ const char *nvme_host_get_hostid(nvme_host_t h)
168168
return h->hostid;
169169
}
170170

171+
const char *nvme_host_get_dhchap_key(nvme_host_t h)
172+
{
173+
return h->dhchap_key;
174+
}
175+
176+
void nvme_host_set_dhchap_key(nvme_host_t h, const char *key)
177+
{
178+
if (h->dhchap_key) {
179+
free(h->dhchap_key);
180+
h->dhchap_key = NULL;
181+
}
182+
if (key)
183+
h->dhchap_key = strdup(key);
184+
}
185+
171186
nvme_subsystem_t nvme_first_subsystem(nvme_host_t h)
172187
{
173188
return list_top(&h->subsystems, struct nvme_subsystem, entry);
@@ -338,6 +353,8 @@ static void __nvme_free_host(struct nvme_host *h)
338353
free(h->hostnqn);
339354
if (h->hostid)
340355
free(h->hostid);
356+
if (h->dhchap_key)
357+
free(h->dhchap_key);
341358
h->r->modified = true;
342359
free(h);
343360
}
@@ -468,6 +485,11 @@ static int nvme_scan_subsystem(struct nvme_root *r, char *name,
468485
free(hostnqn);
469486
if (hostid)
470487
free(hostid);
488+
if (h) {
489+
if (h->dhchap_key)
490+
free(h->dhchap_key);
491+
h->dhchap_key = nvme_get_attr(path, "dhchap_secret");
492+
}
471493
}
472494
if (!h)
473495
h = nvme_default_host(r);
@@ -707,6 +729,21 @@ struct nvme_fabrics_config *nvme_ctrl_get_config(nvme_ctrl_t c)
707729
return &c->cfg;
708730
}
709731

732+
const char *nvme_ctrl_get_dhchap_key(nvme_ctrl_t c)
733+
{
734+
return c->dhchap_key;
735+
}
736+
737+
void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key)
738+
{
739+
if (c->dhchap_key) {
740+
free(c->dhchap_key);
741+
c->dhchap_key = NULL;
742+
}
743+
if (key)
744+
c->dhchap_key = strdup(key);
745+
}
746+
710747
void nvme_ctrl_disable_sqflow(nvme_ctrl_t c, bool disable_sqflow)
711748
{
712749
c->cfg.disable_sqflow = disable_sqflow;
@@ -1088,6 +1125,7 @@ static int nvme_configure_ctrl(nvme_ctrl_t c, const char *path,
10881125
c->queue_count = nvme_get_ctrl_attr(c, "queue_count");
10891126
c->serial = nvme_get_ctrl_attr(c, "serial");
10901127
c->sqsize = nvme_get_ctrl_attr(c, "sqsize");
1128+
c->dhchap_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret");
10911129
return 0;
10921130
}
10931131

@@ -1261,6 +1299,11 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
12611299
free(hostnqn);
12621300
if (hostid)
12631301
free(hostid);
1302+
if (h) {
1303+
if (h->dhchap_key)
1304+
free(h->dhchap_key);
1305+
h->dhchap_key = nvme_get_attr(path, "dhchap_secret");
1306+
}
12641307
if (!h) {
12651308
h = nvme_default_host(r);
12661309
if (!h) {

src/nvme/tree.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ const char *nvme_host_get_hostnqn(nvme_host_t h);
105105
*/
106106
const char *nvme_host_get_hostid(nvme_host_t h);
107107

108+
/**
109+
* nvme_host_get_dhchap_key() - return host key
110+
* @h: Host for which the key should be returned
111+
*
112+
* Return: DH-HMAC-CHAP host key or NULL if not set
113+
*/
114+
const char *nvme_host_get_dhchap_key(nvme_host_t h);
115+
116+
/**
117+
* nvme_host_set_dhchap_key() - set host key
118+
* @h: Host for which the key should be set
119+
* @key: DH-HMAC-CHAP Key to set or NULL to clear existing key
120+
*/
121+
void nvme_host_set_dhchap_key(nvme_host_t h, const char *key);
122+
108123
/**
109124
* nvme_default_host() -
110125
* @r:
@@ -779,6 +794,21 @@ const char *nvme_ctrl_get_host_traddr(nvme_ctrl_t c);
779794
*/
780795
const char *nvme_ctrl_get_host_iface(nvme_ctrl_t c);
781796

797+
/**
798+
* nvme_ctrl_get_dhchap_key() - return controller key
799+
* @c: controller for which the key should be returned
800+
*
801+
* Return: DH-HMAC-CHAP controller key or NULL if not set
802+
*/
803+
const char *nvme_ctrl_get_dhchap_key(nvme_ctrl_t c);
804+
805+
/**
806+
* nvme_ctrl_set_dhchap_key() - set controller key
807+
* @c: Controller for which the key should be set
808+
* @key: DH-HMAC-CHAP Key to set or NULL to clear existing key
809+
*/
810+
void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key);
811+
782812
/**
783813
* nvme_ctrl_get_config() -
784814
* @c:

0 commit comments

Comments
 (0)