Skip to content

Commit d05a242

Browse files
committed
fabrics: move config modify code to library
Move configuration update code into the library. This completes the migration. Afterward, nvme-cli depends only on higher-level APIs, enabling internal library refactoring without breaking public interfaces. Signed-off-by: Daniel Wagner <[email protected]>
1 parent ccb0e89 commit d05a242

4 files changed

Lines changed: 109 additions & 103 deletions

File tree

fabrics.c

Lines changed: 16 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -584,34 +584,6 @@ int fabrics_discovery(const char *desc, int argc, char **argv, bool connect)
584584
return ret;
585585
}
586586

587-
static void nvme_parse_tls_args(const char *keyring, const char *tls_key,
588-
const char *tls_key_identity,
589-
struct nvme_fabrics_config *cfg, nvme_ctrl_t c)
590-
{
591-
if (keyring) {
592-
char *endptr;
593-
long id = strtol(keyring, &endptr, 0);
594-
595-
if (endptr != keyring)
596-
cfg->keyring = id;
597-
else
598-
nvme_ctrl_set_keyring(c, keyring);
599-
}
600-
601-
if (tls_key_identity)
602-
nvme_ctrl_set_tls_key_identity(c, tls_key_identity);
603-
604-
if (tls_key) {
605-
char *endptr;
606-
long id = strtol(tls_key, &endptr, 0);
607-
608-
if (endptr != tls_key)
609-
cfg->tls_key = id;
610-
else
611-
nvme_ctrl_set_tls_key(c, tls_key);
612-
}
613-
}
614-
615587
int fabrics_connect(const char *desc, int argc, char **argv)
616588
{
617589
_cleanup_free_ char *hnqn = NULL;
@@ -924,24 +896,15 @@ int fabrics_disconnect_all(const char *desc, int argc, char **argv)
924896

925897
int fabrics_config(const char *desc, int argc, char **argv)
926898
{
927-
char *subsysnqn = NULL;
928-
char *transport = NULL, *traddr = NULL;
929-
char *trsvcid = NULL, *hostnqn = NULL, *hostid = NULL;
930-
char *host_traddr = NULL, *host_iface = NULL;
931-
_cleanup_free_ char *hnqn = NULL;
932-
_cleanup_free_ char *hid = NULL;
933-
char *hostkey = NULL, *ctrlkey = NULL;
934-
char *keyring = NULL, *tls_key = NULL, *tls_key_identity = NULL;
935-
char *config_file = PATH_NVMF_CONFIG;
936-
unsigned int verbose = 0;
899+
bool scan_tree = false, modify_config = false, update_config = false;
937900
_cleanup_nvme_global_ctx_ struct nvme_global_ctx *ctx = NULL;
938-
int ret;
901+
char *config_file = PATH_NVMF_CONFIG;
939902
struct nvme_fabrics_config cfg;
940903
struct fabric_args fa = { };
941-
bool scan_tree = false, modify_config = false, update_config = false;
904+
unsigned int verbose = 0;
905+
int ret;
942906

943907
NVMF_ARGS(opts, fa, cfg,
944-
OPT_STRING("dhchap-ctrl-secret", 'C', "STR", &ctrlkey, nvmf_ctrlkey),
945908
OPT_STRING("config", 'J', "FILE", &config_file, nvmf_config_file),
946909
OPT_INCR("verbose", 'v', &verbose, "Increase logging verbosity"),
947910
OPT_FLAG("scan", 'R', &scan_tree, "Scan current NVMeoF topology"),
@@ -980,52 +943,30 @@ int fabrics_config(const char *desc, int argc, char **argv)
980943
}
981944

982945
if (modify_config) {
983-
nvme_host_t h;
984-
nvme_subsystem_t s;
985-
nvme_ctrl_t c;
946+
_cleanup_free_ struct nvmf_context *fctx = NULL;
986947

987-
if (!subsysnqn) {
948+
if (!fa.subsysnqn) {
988949
fprintf(stderr,
989950
"required argument [--nqn | -n] needed with --modify\n");
990951
return -EINVAL;
991952
}
992953

993-
if (!transport) {
954+
if (!fa.transport) {
994955
fprintf(stderr,
995956
"required argument [--transport | -t] needed with --modify\n");
996957
return -EINVAL;
997958
}
998959

999-
if (!hostnqn)
1000-
hostnqn = hnqn = nvmf_hostnqn_from_file();
1001-
if (!hostid && hnqn)
1002-
hostid = hid = nvmf_hostid_from_file();
1003-
h = nvme_lookup_host(ctx, hostnqn, hostid);
1004-
if (!h) {
1005-
fprintf(stderr, "Failed to lookup host '%s'\n",
1006-
hostnqn);
1007-
return -ENODEV;
1008-
}
1009-
if (hostkey)
1010-
nvme_host_set_dhchap_key(h, hostkey);
1011-
s = nvme_lookup_subsystem(h, NULL, subsysnqn);
1012-
if (!s) {
1013-
fprintf(stderr, "Failed to lookup subsystem '%s'\n",
1014-
subsysnqn);
1015-
return -ENODEV;
1016-
}
1017-
c = nvme_lookup_ctrl(s, transport, traddr,
1018-
host_traddr, host_iface,
1019-
trsvcid, NULL);
1020-
if (!c) {
1021-
fprintf(stderr, "Failed to lookup controller\n");
1022-
return -ENODEV;
1023-
}
1024-
if (ctrlkey)
1025-
nvme_ctrl_set_dhchap_key(c, ctrlkey);
1026-
nvme_parse_tls_args(keyring, tls_key, tls_key_identity, &cfg, c);
960+
ret = create_common_context(ctx, persistent, &fa,
961+
&cfg, NULL, &fctx);
962+
if (ret)
963+
return ret;
1027964

1028-
nvmf_update_config(c, &cfg);
965+
ret = nvmf_config_modify(ctx, fctx);
966+
if (ret) {
967+
fprintf(stderr, "failed to update config\n");
968+
return ret;
969+
}
1029970
}
1030971

1031972
if (update_config)

libnvme/src/libnvme.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ LIBNVME_2_0 {
283283
nvmf_add_ctrl;
284284
nvmf_adrfam_str;
285285
nvmf_cms_str;
286+
nvmf_config_modify;
286287
nvmf_connect;
287288
nvmf_connect_config_json;
288289
nvmf_connect_ctrl;

libnvme/src/nvme/fabrics.c

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,35 @@ static int set_discovery_kato(struct nvmf_context *fctx,
21152115
return tmo;
21162116
}
21172117

2118+
static void nvme_parse_tls_args(const char *keyring, const char *tls_key,
2119+
const char *tls_key_identity,
2120+
struct nvme_fabrics_config *cfg, nvme_ctrl_t c)
2121+
{
2122+
if (keyring) {
2123+
char *endptr;
2124+
long id = strtol(keyring, &endptr, 0);
2125+
2126+
if (endptr != keyring)
2127+
cfg->keyring = id;
2128+
else
2129+
nvme_ctrl_set_keyring(c, keyring);
2130+
}
2131+
2132+
if (tls_key_identity)
2133+
nvme_ctrl_set_tls_key_identity(c, tls_key_identity);
2134+
2135+
if (tls_key) {
2136+
char *endptr;
2137+
long id = strtol(tls_key, &endptr, 0);
2138+
2139+
if (endptr != tls_key)
2140+
cfg->tls_key = id;
2141+
else
2142+
nvme_ctrl_set_tls_key(c, tls_key);
2143+
}
2144+
}
2145+
2146+
21182147
static int _nvmf_discovery(struct nvme_global_ctx *ctx,
21192148
struct nvmf_context *fctx, bool connect,
21202149
struct nvme_ctrl *c)
@@ -2621,6 +2650,56 @@ int nvmf_discovery_config_file(struct nvme_global_ctx *ctx,
26212650
return 0;
26222651
}
26232652

2653+
int nvmf_config_modify(struct nvme_global_ctx *ctx,
2654+
struct nvmf_context *fctx)
2655+
{
2656+
_cleanup_free_ char *hnqn = NULL;
2657+
_cleanup_free_ char *hid = NULL;
2658+
struct nvme_host *h;
2659+
struct nvme_subsystem *s;
2660+
struct nvme_ctrl *c;
2661+
2662+
if (!fctx->hostnqn)
2663+
fctx->hostnqn = hnqn = nvmf_hostnqn_from_file();
2664+
if (!fctx->hostid && hnqn)
2665+
fctx->hostid = hid = nvmf_hostid_from_file();
2666+
2667+
h = nvme_lookup_host(ctx, fctx->hostnqn, fctx->hostid);
2668+
if (!h) {
2669+
nvme_msg(ctx, LOG_ERR, "Failed to lookup host '%s'\n",
2670+
fctx->hostnqn);
2671+
return -ENODEV;
2672+
}
2673+
2674+
if (fctx->hostkey)
2675+
nvme_host_set_dhchap_key(h, fctx->hostkey);
2676+
2677+
s = nvme_lookup_subsystem(h, NULL, fctx->subsysnqn);
2678+
if (!s) {
2679+
nvme_msg(ctx, LOG_ERR, "Failed to lookup subsystem '%s'\n",
2680+
fctx->subsysnqn);
2681+
return -ENODEV;
2682+
}
2683+
2684+
c = nvme_lookup_ctrl(s, fctx->transport, fctx->traddr,
2685+
fctx->host_traddr, fctx->host_iface,
2686+
fctx->trsvcid, NULL);
2687+
if (!c) {
2688+
nvme_msg(ctx, LOG_ERR, "Failed to lookup controller\n");
2689+
return -ENODEV;
2690+
}
2691+
2692+
if (fctx->ctrlkey)
2693+
nvme_ctrl_set_dhchap_key(c, fctx->ctrlkey);
2694+
2695+
nvme_parse_tls_args(fctx->keyring, fctx->tls_key,
2696+
fctx->tls_key_identity, fctx->cfg, c);
2697+
2698+
nvmf_update_config(c, fctx->cfg);
2699+
2700+
return 0;
2701+
}
2702+
26242703
#define NBFT_SYSFS_FILENAME "NBFT*"
26252704

26262705
static int nbft_filter(const struct dirent *dent)
@@ -3283,34 +3362,6 @@ int nvmf_discovery(struct nvme_global_ctx *ctx, struct nvmf_context *fctx,
32833362
return ret;
32843363
}
32853364

3286-
static void nvme_parse_tls_args(const char *keyring, const char *tls_key,
3287-
const char *tls_key_identity,
3288-
struct nvme_fabrics_config *cfg, nvme_ctrl_t c)
3289-
{
3290-
if (keyring) {
3291-
char *endptr;
3292-
long id = strtol(keyring, &endptr, 0);
3293-
3294-
if (endptr != keyring)
3295-
cfg->keyring = id;
3296-
else
3297-
nvme_ctrl_set_keyring(c, keyring);
3298-
}
3299-
3300-
if (tls_key_identity)
3301-
nvme_ctrl_set_tls_key_identity(c, tls_key_identity);
3302-
3303-
if (tls_key) {
3304-
char *endptr;
3305-
long id = strtol(tls_key, &endptr, 0);
3306-
3307-
if (endptr != tls_key)
3308-
cfg->tls_key = id;
3309-
else
3310-
nvme_ctrl_set_tls_key(c, tls_key);
3311-
}
3312-
}
3313-
33143365
int nvmf_connect(struct nvme_global_ctx *ctx, struct nvmf_context *fctx)
33153366
{
33163367
struct nvme_host *h;

libnvme/src/nvme/fabrics.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,19 @@ int nvmf_connect(struct nvme_global_ctx *ctx, struct nvmf_context *fctx);
632632
int nvmf_connect_config_json(struct nvme_global_ctx *ctx,
633633
struct nvmf_context *fctx);
634634

635+
/**
636+
* nvmf_config_modify() - Modify and update the configurtion
637+
* @ctx: Global context
638+
* @fctx: Fabrics context
639+
*
640+
* Update the current configuration by adding the crypto
641+
* information.
642+
*
643+
* Return: 0 on success, or a negative error code on failure.
644+
*/
645+
int nvmf_config_modify(struct nvme_global_ctx *ctx,
646+
struct nvmf_context *fctx);
647+
635648
/**
636649
* struct nbft_file_entry - Linked list entry for NBFT files
637650
* @next: Pointer to next entry

0 commit comments

Comments
 (0)