Skip to content

Commit 6462f36

Browse files
committed
libnvme: clarify dhchap key usage
There are two dhchap keys, a 'host' and a 'ctrl' key. Additionally, the libnvme interface prefixes function calls with 'nvme_ctrl_XX' and 'nvme_host_XX', depending on whether the first argument is a controller or a host. But that means that the function 'nvme_ctrl_dhchap_key' does _not_ refer to the dhchap ctrl key, but in fact to the dhchap host key. So to avoid all confusion make the naming consistent, and use the '_host_key' suffix for all functions referring to the dhchap host key, and '_ctrl_key' to all functions referring to the dhchap ctrl key. Which, incidentally, uncovered quite a few mixups. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent aeb4894 commit 6462f36

9 files changed

Lines changed: 61 additions & 86 deletions

File tree

libnvme/libnvme/nvme.i

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ struct nvme_host {
369369
char *hostid;
370370
char *hostsymname;
371371
%extend {
372-
char *dhchap_key;
372+
char *dhchap_host_key;
373373
}
374374
};
375375

@@ -464,7 +464,7 @@ struct nvme_ctrl {
464464
* C code Python (SWIG)
465465
* ===================== =====================
466466
* ctrl->s ctrl->subsystem
467-
* ctrl->dhchap_key ctrl->dhchap_host_key
467+
* ctrl->dhchap_host_key ctrl->dhchap_host_key
468468
* ctrl->dhchap_ctrl_key ctrl->dhchap_key
469469
*/
470470
struct nvme_subsystem *subsystem; // Maps to "s" in the C code
@@ -562,7 +562,7 @@ struct nvme_ns {
562562
if (hostsymname)
563563
nvme_host_set_hostsymname(h, hostsymname);
564564
if (hostkey)
565-
nvme_host_set_dhchap_key(h, hostkey);
565+
nvme_host_set_dhchap_host_key(h, hostkey);
566566
return h;
567567
}
568568
~nvme_host() {
@@ -594,10 +594,10 @@ struct nvme_ns {
594594

595595
%{
596596
const char *nvme_host_dhchap_key_get(struct nvme_host *h) {
597-
return nvme_host_get_dhchap_key(h);
597+
return nvme_host_get_dhchap_host_key(h);
598598
}
599599
void nvme_host_dhchap_key_set(struct nvme_host *h, char *key) {
600-
nvme_host_set_dhchap_key(h, key);
600+
nvme_host_set_dhchap_host_key(h, key);
601601
}
602602
%};
603603

@@ -881,10 +881,10 @@ struct nvme_ns {
881881
return nvme_ctrl_get_state(c);
882882
}
883883
const char *nvme_ctrl_dhchap_key_get(struct nvme_ctrl *c) {
884-
return nvme_ctrl_get_dhchap_key(c);
884+
return nvme_ctrl_get_dhchap_ctrl_key(c);
885885
}
886886
void nvme_ctrl_dhchap_key_set(struct nvme_ctrl *c, const char *key) {
887-
nvme_ctrl_set_dhchap_key(c, key);
887+
nvme_ctrl_set_dhchap_ctrl_key(c, key);
888888
}
889889
const char *nvme_ctrl_dhchap_host_key_get(struct nvme_ctrl *c) {
890890
return nvme_ctrl_get_dhchap_host_key(c);

libnvme/src/libnvme.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LIBNVME_3 {
1111
nvme_ctrl_first_path;
1212
nvme_ctrl_get_address;
1313
nvme_ctrl_get_config;
14+
nvme_ctrl_get_dhchap_ctrl_key;
1415
nvme_ctrl_get_dhchap_host_key;
1516
nvme_ctrl_get_phy_slot;
1617
nvme_ctrl_get_src_addr;
@@ -26,6 +27,7 @@ LIBNVME_3 {
2627
nvme_ctrl_next_path;
2728
nvme_ctrl_release_transport_handle;
2829
nvme_ctrl_reset;
30+
nvme_ctrl_set_dhchap_ctrl_key;
2931
nvme_ctrl_set_dhchap_host_key;
3032
nvme_ctrls_filter;
3133
nvme_describe_key_serial;

libnvme/src/nvme/accessors.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -289,23 +289,21 @@ const char *nvme_ctrl_get_trsvcid(const struct nvme_ctrl *p)
289289
return p->trsvcid;
290290
}
291291

292-
void nvme_ctrl_set_dhchap_key(struct nvme_ctrl *p, const char *dhchap_key)
292+
void nvme_ctrl_set_dhchap_host_key(struct nvme_ctrl *p, const char *dhchap_key)
293293
{
294-
free(p->dhchap_key);
295-
p->dhchap_key = dhchap_key ? strdup(dhchap_key) : NULL;
294+
free(p->dhchap_host_key);
295+
p->dhchap_host_key = dhchap_key ? strdup(dhchap_key) : NULL;
296296
}
297297

298-
const char *nvme_ctrl_get_dhchap_key(const struct nvme_ctrl *p)
298+
const char *nvme_ctrl_get_dhchap_host_key(const struct nvme_ctrl *p)
299299
{
300-
return p->dhchap_key;
300+
return p->dhchap_host_key;
301301
}
302302

303-
void nvme_ctrl_set_dhchap_ctrl_key(
304-
struct nvme_ctrl *p,
305-
const char *dhchap_ctrl_key)
303+
void nvme_ctrl_set_dhchap_ctrl_key(struct nvme_ctrl *p, const char *dhchap_key)
306304
{
307305
free(p->dhchap_ctrl_key);
308-
p->dhchap_ctrl_key = dhchap_ctrl_key ? strdup(dhchap_ctrl_key) : NULL;
306+
p->dhchap_ctrl_key = dhchap_key ? strdup(dhchap_key) : NULL;
309307
}
310308

311309
const char *nvme_ctrl_get_dhchap_ctrl_key(const struct nvme_ctrl *p)
@@ -583,15 +581,15 @@ const char *nvme_host_get_hostid(const struct nvme_host *p)
583581
return p->hostid;
584582
}
585583

586-
void nvme_host_set_dhchap_key(struct nvme_host *p, const char *dhchap_key)
584+
void nvme_host_set_dhchap_host_key(struct nvme_host *p, const char *dhchap_key)
587585
{
588-
free(p->dhchap_key);
589-
p->dhchap_key = dhchap_key ? strdup(dhchap_key) : NULL;
586+
free(p->dhchap_host_key);
587+
p->dhchap_host_key = dhchap_key ? strdup(dhchap_key) : NULL;
590588
}
591589

592-
const char *nvme_host_get_dhchap_key(const struct nvme_host *p)
590+
const char *nvme_host_get_dhchap_host_key(const struct nvme_host *p)
593591
{
594-
return p->dhchap_key;
592+
return p->dhchap_host_key;
595593
}
596594

597595
void nvme_host_set_hostsymname(struct nvme_host *p, const char *hostsymname)

libnvme/src/nvme/accessors.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,31 +404,31 @@ void nvme_ctrl_set_trsvcid(struct nvme_ctrl *p, const char *trsvcid);
404404
const char *nvme_ctrl_get_trsvcid(const struct nvme_ctrl *p);
405405

406406
/**
407-
* nvme_ctrl_set_dhchap_key() - Set dhchap_key.
407+
* nvme_ctrl_set_dhchap_host_key() - Set dhchap host key.
408408
* @p: The &struct nvme_ctrl instance to update.
409409
* @dhchap_key: New string; a copy is stored. Pass NULL to clear.
410410
*/
411-
void nvme_ctrl_set_dhchap_key(struct nvme_ctrl *p, const char *dhchap_key);
411+
void nvme_ctrl_set_dhchap_host_key(struct nvme_ctrl *p,
412+
const char *dhchap_key);
412413

413414
/**
414-
* nvme_ctrl_get_dhchap_key() - Get dhchap_key.
415+
* nvme_ctrl_get_dhchap_host_key() - Get dhchap host key.
415416
* @p: The &struct nvme_ctrl instance to query.
416417
*
417418
* Return: The value of the dhchap_key field, or NULL if not set.
418419
*/
419-
const char *nvme_ctrl_get_dhchap_key(const struct nvme_ctrl *p);
420+
const char *nvme_ctrl_get_dhchap_host_key(const struct nvme_ctrl *p);
420421

421422
/**
422-
* nvme_ctrl_set_dhchap_ctrl_key() - Set dhchap_ctrl_key.
423+
* nvme_ctrl_set_dhchap_ctrl_key() - Set dhchap ctrl key.
423424
* @p: The &struct nvme_ctrl instance to update.
424425
* @dhchap_ctrl_key: New string; a copy is stored. Pass NULL to clear.
425426
*/
426-
void nvme_ctrl_set_dhchap_ctrl_key(
427-
struct nvme_ctrl *p,
427+
void nvme_ctrl_set_dhchap_ctrl_key(struct nvme_ctrl *p,
428428
const char *dhchap_ctrl_key);
429429

430430
/**
431-
* nvme_ctrl_get_dhchap_ctrl_key() - Get dhchap_ctrl_key.
431+
* nvme_ctrl_get_dhchap_ctrl_key() - Get dhchap ctrl key.
432432
* @p: The &struct nvme_ctrl instance to query.
433433
*
434434
* Return: The value of the dhchap_ctrl_key field, or NULL if not set.
@@ -805,19 +805,19 @@ void nvme_host_set_hostid(struct nvme_host *p, const char *hostid);
805805
const char *nvme_host_get_hostid(const struct nvme_host *p);
806806

807807
/**
808-
* nvme_host_set_dhchap_key() - Set dhchap_key.
808+
* nvme_host_set_dhchap_key() - Set dhchap host key for the host.
809809
* @p: The &struct nvme_host instance to update.
810810
* @dhchap_key: New string; a copy is stored. Pass NULL to clear.
811811
*/
812-
void nvme_host_set_dhchap_key(struct nvme_host *p, const char *dhchap_key);
812+
void nvme_host_set_dhchap_host_key(struct nvme_host *p, const char *dhchap_key);
813813

814814
/**
815-
* nvme_host_get_dhchap_key() - Get dhchap_key.
815+
* nvme_host_get_dhchap_key() - Get dhchap host key from the host.
816816
* @p: The &struct nvme_host instance to query.
817817
*
818818
* Return: The value of the dhchap_key field, or NULL if not set.
819819
*/
820-
const char *nvme_host_get_dhchap_key(const struct nvme_host *p);
820+
const char *nvme_host_get_dhchap_host_key(const struct nvme_host *p);
821821

822822
/**
823823
* nvme_host_set_hostsymname() - Set hostsymname.

libnvme/src/nvme/fabrics.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
672672
{
673673
struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
674674
const char *transport = nvme_ctrl_get_transport(c);
675-
const char *hostnqn, *hostid, *hostkey, *ctrlkey;
675+
const char *hostnqn, *hostid, *hostkey, *ctrlkey = NULL;
676676
bool discover = false, discovery_nqn = false;
677677
struct nvme_global_ctx *ctx = h->ctx;
678678
long keyring_id = 0;
@@ -708,11 +708,12 @@ static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
708708

709709
hostnqn = nvme_host_get_hostnqn(h);
710710
hostid = nvme_host_get_hostid(h);
711-
hostkey = nvme_host_get_dhchap_key(h);
711+
hostkey = nvme_host_get_dhchap_host_key(h);
712712
if (!hostkey)
713713
hostkey = nvme_ctrl_get_dhchap_host_key(c);
714714

715-
ctrlkey = nvme_ctrl_get_dhchap_key(c);
715+
if (hostkey)
716+
ctrlkey = nvme_ctrl_get_dhchap_ctrl_key(c);
716717

717718
if (cfg->tls && cfg->concat) {
718719
nvme_msg(h->ctx, LOG_ERR, "cannot specify --tls and --concat together\n");
@@ -1020,9 +1021,9 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
10201021
key = nvme_ctrl_get_dhchap_host_key(fc);
10211022
if (key)
10221023
nvme_ctrl_set_dhchap_host_key(c, key);
1023-
key = nvme_ctrl_get_dhchap_key(fc);
1024+
key = nvme_ctrl_get_dhchap_ctrl_key(fc);
10241025
if (key)
1025-
nvme_ctrl_set_dhchap_key(c, key);
1026+
nvme_ctrl_set_dhchap_ctrl_key(c, key);
10261027
key = nvme_ctrl_get_keyring(fc);
10271028
if (key)
10281029
nvme_ctrl_set_keyring(c, key);
@@ -1854,7 +1855,7 @@ static int setup_connection(struct nvmf_context *fctx, struct nvme_host *h,
18541855
bool discovery)
18551856
{
18561857
if (fctx->hostkey)
1857-
nvme_host_set_dhchap_key(h, fctx->hostkey);
1858+
nvme_host_set_dhchap_host_key(h, fctx->hostkey);
18581859

18591860
if (!fctx->trsvcid)
18601861
fctx->trsvcid = nvmf_get_default_trsvcid(fctx->transport,
@@ -2437,7 +2438,7 @@ int nvmf_config_modify(struct nvme_global_ctx *ctx,
24372438
}
24382439

24392440
if (fctx->hostkey)
2440-
nvme_host_set_dhchap_key(h, fctx->hostkey);
2441+
nvme_host_set_dhchap_host_key(h, fctx->hostkey);
24412442

24422443
s = nvme_lookup_subsystem(h, NULL, fctx->subsysnqn);
24432444
if (!s) {
@@ -2455,7 +2456,7 @@ int nvmf_config_modify(struct nvme_global_ctx *ctx,
24552456
}
24562457

24572458
if (fctx->ctrlkey)
2458-
nvme_ctrl_set_dhchap_key(c, fctx->ctrlkey);
2459+
nvme_ctrl_set_dhchap_ctrl_key(c, fctx->ctrlkey);
24592460

24602461
nvme_parse_tls_args(fctx->keyring, fctx->tls_key,
24612462
fctx->tls_key_identity, fctx->cfg, c);
@@ -3081,8 +3082,11 @@ int nvmf_connect(struct nvme_global_ctx *ctx, struct nvmf_context *fctx)
30813082
if (err)
30823083
return err;
30833084

3084-
if (fctx->ctrlkey)
3085-
nvme_ctrl_set_dhchap_key(c, fctx->ctrlkey);
3085+
if (fctx->hostkey) {
3086+
nvme_ctrl_set_dhchap_host_key(c, fctx->hostkey);
3087+
if (fctx->ctrlkey)
3088+
nvme_ctrl_set_dhchap_ctrl_key(c, fctx->ctrlkey);
3089+
}
30863090

30873091
nvme_parse_tls_args(fctx->keyring, fctx->tls_key,
30883092
fctx->tls_key_identity, fctx->cfg, c);

libnvme/src/nvme/json.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj)
112112
nvme_ctrl_set_dhchap_host_key(c, json_object_get_string(attr_obj));
113113
attr_obj = json_object_object_get(port_obj, "dhchap_ctrl_key");
114114
if (attr_obj)
115-
nvme_ctrl_set_dhchap_key(c, json_object_get_string(attr_obj));
115+
nvme_ctrl_set_dhchap_ctrl_key(c, json_object_get_string(attr_obj));
116116
attr_obj = json_object_object_get(port_obj, "keyring");
117117
if (attr_obj)
118118
nvme_ctrl_set_keyring(c, json_object_get_string(attr_obj));
@@ -175,7 +175,7 @@ static void json_parse_host(struct nvme_global_ctx *ctx, struct json_object *hos
175175
h = nvme_lookup_host(ctx, hostnqn, hostid);
176176
attr_obj = json_object_object_get(host_obj, "dhchap_key");
177177
if (attr_obj)
178-
nvme_host_set_dhchap_key(h, json_object_get_string(attr_obj));
178+
nvme_host_set_dhchap_host_key(h, json_object_get_string(attr_obj));
179179
attr_obj = json_object_object_get(host_obj, "hostsymname");
180180
if (attr_obj)
181181
nvme_host_set_hostsymname(h, json_object_get_string(attr_obj));
@@ -308,7 +308,7 @@ static void json_update_port(struct json_object *ctrl_array, nvme_ctrl_t c)
308308
if (value)
309309
json_object_object_add(port_obj, "dhchap_key",
310310
json_object_new_string(value));
311-
value = nvme_ctrl_get_dhchap_key(c);
311+
value = nvme_ctrl_get_dhchap_ctrl_key(c);
312312
if (value)
313313
json_object_object_add(port_obj, "dhchap_ctrl_key",
314314
json_object_new_string(value));
@@ -405,7 +405,7 @@ int json_update_config(struct nvme_global_ctx *ctx, int fd)
405405
if (hostid)
406406
json_object_object_add(host_obj, "hostid",
407407
json_object_new_string(hostid));
408-
dhchap_key = nvme_host_get_dhchap_key(h);
408+
dhchap_key = nvme_host_get_dhchap_host_key(h);
409409
if (dhchap_key)
410410
json_object_object_add(host_obj, "dhchap_key",
411411
json_object_new_string(dhchap_key));
@@ -475,7 +475,7 @@ static void json_dump_ctrl(struct json_object *ctrl_array, nvme_ctrl_t c)
475475
if (value)
476476
json_object_object_add(ctrl_obj, "dhchap_key",
477477
json_object_new_string(value));
478-
value = nvme_ctrl_get_dhchap_key(c);
478+
value = nvme_ctrl_get_dhchap_ctrl_key(c);
479479
if (value)
480480
json_object_object_add(ctrl_obj, "dhchap_ctrl_key",
481481
json_object_new_string(value));
@@ -635,7 +635,7 @@ int json_dump_tree(struct nvme_global_ctx *ctx)
635635
if (hostid)
636636
json_object_object_add(host_obj, "hostid",
637637
json_object_new_string(hostid));
638-
dhchap_key = nvme_host_get_dhchap_key(h);
638+
dhchap_key = nvme_host_get_dhchap_host_key(h);
639639
if (dhchap_key)
640640
json_object_object_add(host_obj, "dhchap_key",
641641
json_object_new_string(dhchap_key));

libnvme/src/nvme/private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ struct nvme_ctrl {
196196
char *subsysnqn;
197197
char *traddr;
198198
char *trsvcid;
199-
char *dhchap_key;
199+
char *dhchap_host_key;
200200
char *dhchap_ctrl_key;
201201
char *keyring;
202202
char *tls_key_identity;
@@ -238,7 +238,7 @@ struct nvme_host {
238238

239239
char *hostnqn;
240240
char *hostid;
241-
char *dhchap_key;
241+
char *dhchap_host_key;
242242
char *hostsymname;
243243
bool pdc_enabled;
244244
bool pdc_enabled_valid; /* set if pdc_enabled doesn't have an undefined

0 commit comments

Comments
 (0)