Skip to content

Commit 1557dd0

Browse files
committed
fabrics: add context to common fabrics arg parser
The NVMF_ARGS macro parses the common arguments for the transport configuration. Move all those open coded variables into a context, so it's simpler to pass a complete configuration around. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 9ba7489 commit 1557dd0

6 files changed

Lines changed: 91 additions & 117 deletions

File tree

fabrics.c

Lines changed: 72 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,21 @@ static const char *nvmf_concat = "enable secure concatenation";
9494
static const char *nvmf_config_file = "Use specified JSON configuration file or 'none' to disable";
9595
static const char *nvmf_context = "execution context identification string";
9696

97-
#define NVMF_ARGS(n, c, ...) \
97+
#define NVMF_ARGS(n, t, c, ...) \
9898
struct argconfig_commandline_options n[] = { \
99-
OPT_STRING("transport", 't', "STR", &transport, nvmf_tport), \
100-
OPT_STRING("nqn", 'n', "STR", &subsysnqn, nvmf_nqn), \
101-
OPT_STRING("traddr", 'a', "STR", &traddr, nvmf_traddr), \
102-
OPT_STRING("trsvcid", 's', "STR", &trsvcid, nvmf_trsvcid), \
103-
OPT_STRING("host-traddr", 'w', "STR", &host_traddr, nvmf_htraddr), \
104-
OPT_STRING("host-iface", 'f', "STR", &host_iface, nvmf_hiface), \
105-
OPT_STRING("hostnqn", 'q', "STR", &hostnqn, nvmf_hostnqn), \
106-
OPT_STRING("hostid", 'I', "STR", &hostid, nvmf_hostid), \
107-
OPT_STRING("dhchap-secret", 'S', "STR", &hostkey, nvmf_hostkey), \
108-
OPT_STRING("keyring", 0, "STR", &keyring, nvmf_keyring), \
109-
OPT_STRING("tls-key", 0, "STR", &tls_key, nvmf_tls_key), \
110-
OPT_STRING("tls-key-identity", 0, "STR", &tls_key_identity, nvmf_tls_key_identity), \
99+
OPT_STRING("transport", 't', "STR", &t.transport, nvmf_tport), \
100+
OPT_STRING("nqn", 'n', "STR", &t.subsysnqn, nvmf_nqn), \
101+
OPT_STRING("traddr", 'a', "STR", &t.traddr, nvmf_traddr), \
102+
OPT_STRING("trsvcid", 's', "STR", &t.trsvcid, nvmf_trsvcid), \
103+
OPT_STRING("host-traddr", 'w', "STR", &t.host_traddr, nvmf_htraddr), \
104+
OPT_STRING("host-iface", 'f', "STR", &t.host_iface, nvmf_hiface), \
105+
OPT_STRING("hostnqn", 'q', "STR", &t.hostnqn, nvmf_hostnqn), \
106+
OPT_STRING("hostid", 'I', "STR", &t.hostid, nvmf_hostid), \
107+
OPT_STRING("dhchap-secret", 'S', "STR", &t.hostkey, nvmf_hostkey), \
108+
OPT_STRING("dhchap-ctrl-secret", 'C', "STR", &t.ctrlkey, nvmf_ctrlkey), \
109+
OPT_STRING("keyring", 0, "STR", &t.keyring, nvmf_keyring), \
110+
OPT_STRING("tls-key", 0, "STR", &t.tls_key, nvmf_tls_key), \
111+
OPT_STRING("tls-key-identity", 0, "STR", &t.tls_key_identity, nvmf_tls_key_identity), \
111112
OPT_INT("nr-io-queues", 'i', &c.nr_io_queues, nvmf_nr_io_queues), \
112113
OPT_INT("nr-write-queues", 'W', &c.nr_write_queues, nvmf_nr_write_queues), \
113114
OPT_INT("nr-poll-queues", 'P', &c.nr_poll_queues, nvmf_nr_poll_queues), \
@@ -367,8 +368,7 @@ static void nvmf_connected(struct nvmf_discovery_ctx *dctx,
367368
}
368369

369370
static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
370-
bool persistent,
371-
const char *host_traddr, const char *host_iface,
371+
bool persistent, struct tr_config *trcfg,
372372
struct nvme_fabrics_config *defcfg,
373373
void *user_data, struct nvmf_discovery_ctx **dctxp)
374374
{
@@ -407,11 +407,11 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
407407
if (err)
408408
goto err;
409409

410-
err = nvmf_discovery_ctx_host_traddr_set(dctx, host_traddr);
410+
err = nvmf_discovery_ctx_host_traddr_set(dctx, trcfg->host_traddr);
411411
if (err)
412412
goto err;
413413

414-
err = nvmf_discovery_ctx_host_iface_set(dctx, host_iface);
414+
err = nvmf_discovery_ctx_host_iface_set(dctx, trcfg->host_iface);
415415
if (err)
416416
goto err;
417417

@@ -431,21 +431,17 @@ static int discover_from_conf_file(struct nvme_global_ctx *ctx, nvme_host_t h,
431431
const char *desc, bool connect,
432432
const struct nvme_fabrics_config *defcfg)
433433
{
434-
char *transport = NULL, *traddr = NULL, *trsvcid = NULL;
435-
char *hostnqn = NULL, *hostid = NULL, *hostkey = NULL;
436-
char *subsysnqn = NULL, *keyring = NULL, *tls_key = NULL;
437434
_cleanup_free_ struct nvmf_discovery_ctx *dctx = NULL;
438-
char *host_iface = NULL, *host_traddr = NULL;
439-
char *tls_key_identity = NULL;
440435
char *ptr, **argv, *p, line[4096];
441436
int argc, ret = 0;
442437
unsigned int verbose = 0;
443438
_cleanup_file_ FILE *f = NULL;
444439
nvme_print_flags_t flags;
445440
char *format = "normal";
446441
struct nvme_fabrics_config cfg;
442+
struct tr_config trcfg;
447443
bool force = false;
448-
NVMF_ARGS(opts, cfg,
444+
NVMF_ARGS(opts, trcfg, cfg,
449445
OPT_FMT("output-format", 'o', &format, output_format),
450446
OPT_FILE("raw", 'r', &raw, "save raw output to file"),
451447
OPT_FLAG("persistent", 'p', &persistent, "persistent discovery connection"),
@@ -486,31 +482,23 @@ static int discover_from_conf_file(struct nvme_global_ctx *ctx, nvme_host_t h,
486482
argv[argc] = NULL;
487483

488484
memcpy(&cfg, defcfg, sizeof(cfg));
489-
subsysnqn = NVME_DISC_SUBSYS_NAME;
485+
trcfg.subsysnqn = NVME_DISC_SUBSYS_NAME;
490486
ret = argconfig_parse(argc, argv, desc, opts);
491487
if (ret)
492488
goto next;
493-
if (!transport && !traddr)
489+
if (!trcfg.transport && !trcfg.traddr)
494490
goto next;
495491

496-
if (!trsvcid)
497-
trsvcid = nvmf_get_default_trsvcid(transport, true);
498-
499-
struct tr_config trcfg = {
500-
.subsysnqn = subsysnqn,
501-
.transport = transport,
502-
.traddr = traddr,
503-
.host_traddr = host_traddr,
504-
.host_iface = host_iface,
505-
.trsvcid = trsvcid,
506-
};
492+
if (!trcfg.trsvcid)
493+
trcfg.trsvcid =
494+
nvmf_get_default_trsvcid(trcfg.transport, true);
507495

508496
struct cb_discovery_log_data dld = {
509497
.flags = flags,
510498
.raw = raw,
511499
};
512-
ret = create_discovery_log_ctx(ctx, true, host_traddr,
513-
host_iface, &cfg, &dld, &dctx);
500+
ret = create_discovery_log_ctx(ctx, true, &trcfg, &cfg,
501+
&dld, &dctx);
514502
if (ret)
515503
return ret;
516504

@@ -588,12 +576,6 @@ static int nvme_read_config_checked(struct nvme_global_ctx *ctx,
588576
/* returns negative errno values */
589577
int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
590578
{
591-
char *subsysnqn = NVME_DISC_SUBSYS_NAME;
592-
char *hostnqn = NULL, *hostid = NULL, *hostkey = NULL;
593-
char *transport = NULL, *traddr = NULL, *trsvcid = NULL;
594-
char *host_iface = NULL, *host_traddr = NULL;
595-
char *keyring = NULL, *tls_key = NULL;
596-
char *tls_key_identity = NULL;
597579
char *config_file = PATH_NVMF_CONFIG;
598580
_cleanup_free_ char *hnqn = NULL;
599581
_cleanup_free_ char *hid = NULL;
@@ -607,13 +589,14 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
607589
int ret;
608590
char *format = "normal";
609591
struct nvme_fabrics_config cfg;
592+
struct tr_config trcfg = { .subsysnqn = NVME_DISC_SUBSYS_NAME };
610593
char *device = NULL;
611594
bool force = false;
612595
bool json_config = false;
613596
bool nbft = false, nonbft = false;
614597
char *nbft_path = NBFT_SYSFS_PATH;
615598

616-
NVMF_ARGS(opts, cfg,
599+
NVMF_ARGS(opts, trcfg, cfg,
617600
OPT_STRING("device", 'd', "DEV", &device, "use existing discovery controller device"),
618601
OPT_FMT("output-format", 'o', &format, output_format),
619602
OPT_FILE("raw", 'r', &raw, "save raw output to file"),
@@ -667,7 +650,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
667650
return ret;
668651
}
669652

670-
ret = nvme_host_get_ids(ctx, hostnqn, hostid, &hnqn, &hid);
653+
ret = nvme_host_get_ids(ctx, trcfg.hostnqn, trcfg.hostid, &hnqn, &hid);
671654
if (ret < 0)
672655
return ret;
673656

@@ -683,55 +666,47 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
683666
else if (!strncmp(device, "/dev/", 5))
684667
device += 5;
685668
}
686-
if (hostkey)
687-
nvme_host_set_dhchap_key(h, hostkey);
669+
if (trcfg.hostkey)
670+
nvme_host_set_dhchap_key(h, trcfg.hostkey);
688671

689672
struct cb_discovery_log_data dld = {
690673
.flags = flags,
691674
.raw = raw,
692675
};
693-
694-
ret = create_discovery_log_ctx(ctx, persistent, host_traddr,
695-
host_iface, &cfg, &dld, &dctx);
676+
ret = create_discovery_log_ctx(ctx, persistent, &trcfg,
677+
&cfg, &dld, &dctx);
696678
if (ret)
697679
return ret;
698680

699-
if (!device && !transport && !traddr) {
681+
if (!device && !trcfg.transport && !trcfg.traddr) {
700682
if (!nonbft)
701-
ret = nvmf_discovery_nbft(ctx, dctx, hostnqn, hostid,
702-
hnqn, hid, connect,
703-
&cfg, nbft_path);
683+
ret = nvmf_discovery_nbft(ctx, dctx,
684+
trcfg.hostnqn, trcfg.hostid, hnqn, hid, connect,
685+
&cfg, nbft_path);
704686
if (nbft)
705687
goto out_free;
706688

707689
if (json_config)
708690
ret = nvmf_discovery_config_json(ctx, dctx,
709-
hostnqn, hostid, connect, force);
691+
trcfg.hostnqn, trcfg.hostid, connect, force);
710692
if (ret || access(PATH_NVMF_DISC, F_OK))
711693
goto out_free;
712694

713695
ret = discover_from_conf_file(ctx, h, desc, connect, &cfg);
714696
goto out_free;
715697
}
716698

717-
if (!trsvcid)
718-
trsvcid = nvmf_get_default_trsvcid(transport, true);
719-
720-
struct tr_config trcfg = {
721-
.subsysnqn = subsysnqn,
722-
.transport = transport,
723-
.traddr = traddr,
724-
.host_traddr = host_traddr,
725-
.host_iface = host_iface,
726-
.trsvcid = trsvcid,
727-
};
699+
if (!trcfg.trsvcid)
700+
trcfg.trsvcid = nvmf_get_default_trsvcid(trcfg.transport, true);
728701

729702
if (device && !force) {
730703
ret = nvme_scan_ctrl(ctx, device, &c);
731704
if (!ret) {
732705
/* Check if device matches command-line options */
733-
if (!nvme_ctrl_config_match(c, transport, traddr, trsvcid, subsysnqn,
734-
host_traddr, host_iface)) {
706+
if (!nvme_ctrl_config_match(c, trcfg.transport,
707+
trcfg.traddr, trcfg.trsvcid,
708+
trcfg.subsysnqn, trcfg.host_traddr,
709+
trcfg.host_iface)) {
735710
fprintf(stderr,
736711
"ctrl device %s found, ignoring non matching command-line options\n",
737712
device);
@@ -762,10 +737,10 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
762737
* for the udev rules). This ensures that host-traddr/
763738
* host-iface are consistent with the discovery controller (c).
764739
*/
765-
if (!host_traddr)
766-
host_traddr = (char *)nvme_ctrl_get_host_traddr(c);
767-
if (!host_iface)
768-
host_iface = (char *)nvme_ctrl_get_host_iface(c);
740+
if (!trcfg.host_traddr)
741+
trcfg.host_traddr = (char *)nvme_ctrl_get_host_traddr(c);
742+
if (!trcfg.host_iface)
743+
trcfg.host_iface = (char *)nvme_ctrl_get_host_iface(c);
769744
}
770745
} else {
771746
/*
@@ -837,12 +812,6 @@ static void nvme_parse_tls_args(const char *keyring, const char *tls_key,
837812

838813
int nvmf_connect(const char *desc, int argc, char **argv)
839814
{
840-
char *subsysnqn = NULL;
841-
char *transport = NULL, *traddr = NULL;
842-
char *trsvcid = NULL, *hostnqn = NULL, *hostid = NULL;
843-
char *hostkey = NULL, *ctrlkey = NULL, *keyring = NULL;
844-
char *tls_key = NULL, *tls_key_identity = NULL;
845-
char *host_iface = NULL, *host_traddr = NULL;
846815
_cleanup_free_ char *hnqn = NULL;
847816
_cleanup_free_ char *hid = NULL;
848817
char *config_file = NULL;
@@ -854,10 +823,10 @@ int nvmf_connect(const char *desc, int argc, char **argv)
854823
int ret;
855824
nvme_print_flags_t flags;
856825
struct nvme_fabrics_config cfg = { 0 };
826+
struct tr_config trcfg = { 0 };
857827
char *format = "normal";
858828

859-
NVMF_ARGS(opts, cfg,
860-
OPT_STRING("dhchap-ctrl-secret", 'C', "STR", &ctrlkey, nvmf_ctrlkey),
829+
NVMF_ARGS(opts, trcfg, cfg,
861830
OPT_STRING("config", 'J', "FILE", &config_file, nvmf_config_file),
862831
OPT_INCR("verbose", 'v', &verbose, "Increase logging verbosity"),
863832
OPT_FLAG("dump-config", 'O', &dump_config, "Dump JSON configuration to stdout"),
@@ -879,23 +848,23 @@ int nvmf_connect(const char *desc, int argc, char **argv)
879848
if (config_file && strcmp(config_file, "none"))
880849
goto do_connect;
881850

882-
if (!subsysnqn) {
851+
if (!trcfg.subsysnqn) {
883852
fprintf(stderr,
884853
"required argument [--nqn | -n] not specified\n");
885854
return -EINVAL;
886855
}
887856

888-
if (!transport) {
857+
if (!trcfg.transport) {
889858
fprintf(stderr,
890859
"required argument [--transport | -t] not specified\n");
891860
return -EINVAL;
892861
}
893862

894-
if (strcmp(transport, "loop")) {
895-
if (!traddr) {
863+
if (strcmp(trcfg.transport, "loop")) {
864+
if (!trcfg.traddr) {
896865
fprintf(stderr,
897866
"required argument [--traddr | -a] not specified for transport %s\n",
898-
transport);
867+
trcfg.transport);
899868
return -EINVAL;
900869
}
901870
}
@@ -923,51 +892,45 @@ int nvmf_connect(const char *desc, int argc, char **argv)
923892
return ret;
924893
}
925894

926-
ret = nvme_host_get_ids(ctx, hostnqn, hostid, &hnqn, &hid);
895+
ret = nvme_host_get_ids(ctx, trcfg.hostnqn, trcfg.hostid, &hnqn, &hid);
927896
if (ret < 0)
928897
return ret;
929898

930899
h = nvme_lookup_host(ctx, hnqn, hid);
931900
if (!h)
932901
return -ENOMEM;
933-
if (hostkey)
934-
nvme_host_set_dhchap_key(h, hostkey);
935-
if (!trsvcid)
936-
trsvcid = nvmf_get_default_trsvcid(transport, false);
902+
if (trcfg.hostkey)
903+
nvme_host_set_dhchap_key(h, trcfg.hostkey);
904+
if (!trcfg.trsvcid)
905+
trcfg.trsvcid = nvmf_get_default_trsvcid(trcfg.transport, false);
937906

938907
if (config_file)
939-
return nvmf_connect_config_json(ctx, hostnqn, hostid, &cfg);
940-
941-
struct tr_config trcfg = {
942-
.subsysnqn = subsysnqn,
943-
.transport = transport,
944-
.traddr = traddr,
945-
.host_traddr = host_traddr,
946-
.host_iface = host_iface,
947-
.trsvcid = trsvcid,
948-
};
908+
return nvmf_connect_config_json(ctx, trcfg.hostnqn,
909+
trcfg.hostid, &cfg);
949910

950911
c = lookup_ctrl(h, &trcfg);
951912
if (c && nvme_ctrl_get_name(c) && !cfg.duplicate_connect) {
952913
fprintf(stderr, "already connected\n");
953914
return -EALREADY;
954915
}
955916

956-
ret = nvme_create_ctrl(ctx, subsysnqn, transport, traddr,
957-
host_traddr, host_iface, trsvcid, &c);
917+
ret = nvme_create_ctrl(ctx, trcfg.subsysnqn, trcfg.transport,
918+
trcfg.traddr, trcfg.host_traddr, trcfg.host_iface,
919+
trcfg.trsvcid, &c);
958920
if (ret)
959921
return ret;
960922

961-
if (ctrlkey)
962-
nvme_ctrl_set_dhchap_key(c, ctrlkey);
923+
if (trcfg.ctrlkey)
924+
nvme_ctrl_set_dhchap_key(c, trcfg.ctrlkey);
963925

964-
nvme_parse_tls_args(keyring, tls_key, tls_key_identity, &cfg, c);
926+
nvme_parse_tls_args(trcfg.keyring, trcfg.tls_key,
927+
trcfg.tls_key_identity, &cfg, c);
965928

966929
/*
967930
* We are connecting to a discovery controller, so let's treat
968931
* this as a persistent connection and specify a KATO.
969932
*/
970-
if (!strcmp(subsysnqn, NVME_DISC_SUBSYS_NAME)) {
933+
if (!strcmp(trcfg.subsysnqn, NVME_DISC_SUBSYS_NAME)) {
971934
persistent = true;
972935

973936
set_discovery_kato(&cfg);
@@ -1207,9 +1170,10 @@ int nvmf_config(const char *desc, int argc, char **argv)
12071170
_cleanup_nvme_global_ctx_ struct nvme_global_ctx *ctx = NULL;
12081171
int ret;
12091172
struct nvme_fabrics_config cfg;
1173+
struct tr_config trcfg = { };
12101174
bool scan_tree = false, modify_config = false, update_config = false;
12111175

1212-
NVMF_ARGS(opts, cfg,
1176+
NVMF_ARGS(opts, trcfg, cfg,
12131177
OPT_STRING("dhchap-ctrl-secret", 'C', "STR", &ctrlkey, nvmf_ctrlkey),
12141178
OPT_STRING("config", 'J', "FILE", &config_file, nvmf_config_file),
12151179
OPT_INCR("verbose", 'v', &verbose, "Increase logging verbosity"),

fabrics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ struct tr_config {
99
const char *host_traddr;
1010
const char *host_iface;
1111
const char *trsvcid;
12+
13+
const char *hostnqn;
14+
const char *hostid;
15+
const char *hostkey;
16+
const char *ctrlkey;
17+
const char *keyring;
18+
const char *tls_key;
19+
const char *tls_key_identity;
1220
};
1321

1422
extern nvme_ctrl_t lookup_ctrl(nvme_host_t h, struct tr_config *trcfg);

0 commit comments

Comments
 (0)