Skip to content

Commit 7a8c9e0

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 9552455 commit 7a8c9e0

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), \
@@ -368,8 +369,7 @@ static void nvmf_connected(struct nvmf_discovery_ctx *dctx,
368369
}
369370

370371
static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
371-
bool persistent,
372-
const char *host_traddr, const char *host_iface,
372+
bool persistent, struct tr_config *trcfg,
373373
struct nvme_fabrics_config *defcfg,
374374
void *user_data, struct nvmf_discovery_ctx **dctxp)
375375
{
@@ -408,11 +408,11 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
408408
if (err)
409409
goto err;
410410

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

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

@@ -432,21 +432,17 @@ static int discover_from_conf_file(struct nvme_global_ctx *ctx, nvme_host_t h,
432432
const char *desc, bool connect,
433433
const struct nvme_fabrics_config *defcfg)
434434
{
435-
char *transport = NULL, *traddr = NULL, *trsvcid = NULL;
436-
char *hostnqn = NULL, *hostid = NULL, *hostkey = NULL;
437-
char *subsysnqn = NULL, *keyring = NULL, *tls_key = NULL;
438435
_cleanup_free_ struct nvmf_discovery_ctx *dctx = NULL;
439-
char *host_iface = NULL, *host_traddr = NULL;
440-
char *tls_key_identity = NULL;
441436
char *ptr, **argv, *p, line[4096];
442437
int argc, ret = 0;
443438
unsigned int verbose = 0;
444439
_cleanup_file_ FILE *f = NULL;
445440
nvme_print_flags_t flags;
446441
char *format = "normal";
447442
struct nvme_fabrics_config cfg;
443+
struct tr_config trcfg;
448444
bool force = false;
449-
NVMF_ARGS(opts, cfg,
445+
NVMF_ARGS(opts, trcfg, cfg,
450446
OPT_FMT("output-format", 'o', &format, output_format),
451447
OPT_FILE("raw", 'r', &raw, "save raw output to file"),
452448
OPT_FLAG("persistent", 'p', &persistent, "persistent discovery connection"),
@@ -487,31 +483,23 @@ static int discover_from_conf_file(struct nvme_global_ctx *ctx, nvme_host_t h,
487483
argv[argc] = NULL;
488484

489485
memcpy(&cfg, defcfg, sizeof(cfg));
490-
subsysnqn = NVME_DISC_SUBSYS_NAME;
486+
trcfg.subsysnqn = NVME_DISC_SUBSYS_NAME;
491487
ret = argconfig_parse(argc, argv, desc, opts);
492488
if (ret)
493489
goto next;
494-
if (!transport && !traddr)
490+
if (!trcfg.transport && !trcfg.traddr)
495491
goto next;
496492

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

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

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

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

671-
ret = nvme_host_get_ids(ctx, hostnqn, hostid, &hnqn, &hid);
654+
ret = nvme_host_get_ids(ctx, trcfg.hostnqn, trcfg.hostid, &hnqn, &hid);
672655
if (ret < 0)
673656
return ret;
674657

@@ -684,55 +667,47 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
684667
else if (!strncmp(device, "/dev/", 5))
685668
device += 5;
686669
}
687-
if (hostkey)
688-
nvme_host_set_dhchap_key(h, hostkey);
670+
if (trcfg.hostkey)
671+
nvme_host_set_dhchap_key(h, trcfg.hostkey);
689672

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

700-
if (!device && !transport && !traddr) {
682+
if (!device && !trcfg.transport && !trcfg.traddr) {
701683
if (!nonbft)
702-
ret = nvmf_discovery_nbft(ctx, dctx, hostnqn, hostid,
703-
hnqn, hid, connect,
704-
&cfg, nbft_path);
684+
ret = nvmf_discovery_nbft(ctx, dctx,
685+
trcfg.hostnqn, trcfg.hostid, hnqn, hid, connect,
686+
&cfg, nbft_path);
705687
if (nbft)
706688
goto out_free;
707689

708690
if (json_config)
709691
ret = nvmf_discovery_config_json(ctx, dctx,
710-
hostnqn, hostid, connect, force);
692+
trcfg.hostnqn, trcfg.hostid, connect, force);
711693
if (ret || access(PATH_NVMF_DISC, F_OK))
712694
goto out_free;
713695

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

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

730703
if (device && !force) {
731704
ret = nvme_scan_ctrl(ctx, device, &c);
732705
if (!ret) {
733706
/* Check if device matches command-line options */
734-
if (!nvme_ctrl_config_match(c, transport, traddr, trsvcid, subsysnqn,
735-
host_traddr, host_iface)) {
707+
if (!nvme_ctrl_config_match(c, trcfg.transport,
708+
trcfg.traddr, trcfg.trsvcid,
709+
trcfg.subsysnqn, trcfg.host_traddr,
710+
trcfg.host_iface)) {
736711
fprintf(stderr,
737712
"ctrl device %s found, ignoring non matching command-line options\n",
738713
device);
@@ -763,10 +738,10 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
763738
* for the udev rules). This ensures that host-traddr/
764739
* host-iface are consistent with the discovery controller (c).
765740
*/
766-
if (!host_traddr)
767-
host_traddr = (char *)nvme_ctrl_get_host_traddr(c);
768-
if (!host_iface)
769-
host_iface = (char *)nvme_ctrl_get_host_iface(c);
741+
if (!trcfg.host_traddr)
742+
trcfg.host_traddr = (char *)nvme_ctrl_get_host_traddr(c);
743+
if (!trcfg.host_iface)
744+
trcfg.host_iface = (char *)nvme_ctrl_get_host_iface(c);
770745
}
771746
} else {
772747
/*
@@ -838,12 +813,6 @@ static void nvme_parse_tls_args(const char *keyring, const char *tls_key,
838813

839814
int nvmf_connect(const char *desc, int argc, char **argv)
840815
{
841-
char *subsysnqn = NULL;
842-
char *transport = NULL, *traddr = NULL;
843-
char *trsvcid = NULL, *hostnqn = NULL, *hostid = NULL;
844-
char *hostkey = NULL, *ctrlkey = NULL, *keyring = NULL;
845-
char *tls_key = NULL, *tls_key_identity = NULL;
846-
char *host_iface = NULL, *host_traddr = NULL;
847816
_cleanup_free_ char *hnqn = NULL;
848817
_cleanup_free_ char *hid = NULL;
849818
char *config_file = NULL;
@@ -855,10 +824,10 @@ int nvmf_connect(const char *desc, int argc, char **argv)
855824
int ret;
856825
nvme_print_flags_t flags;
857826
struct nvme_fabrics_config cfg = { 0 };
827+
struct tr_config trcfg = { 0 };
858828
char *format = "normal";
859829

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

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

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

895-
if (strcmp(transport, "loop")) {
896-
if (!traddr) {
864+
if (strcmp(trcfg.transport, "loop")) {
865+
if (!trcfg.traddr) {
897866
fprintf(stderr,
898867
"required argument [--traddr | -a] not specified for transport %s\n",
899-
transport);
868+
trcfg.transport);
900869
return -EINVAL;
901870
}
902871
}
@@ -924,51 +893,45 @@ int nvmf_connect(const char *desc, int argc, char **argv)
924893
return ret;
925894
}
926895

927-
ret = nvme_host_get_ids(ctx, hostnqn, hostid, &hnqn, &hid);
896+
ret = nvme_host_get_ids(ctx, trcfg.hostnqn, trcfg.hostid, &hnqn, &hid);
928897
if (ret < 0)
929898
return ret;
930899

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

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

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

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

962-
if (ctrlkey)
963-
nvme_ctrl_set_dhchap_key(c, ctrlkey);
924+
if (trcfg.ctrlkey)
925+
nvme_ctrl_set_dhchap_key(c, trcfg.ctrlkey);
964926

965-
nvme_parse_tls_args(keyring, tls_key, tls_key_identity, &cfg, c);
927+
nvme_parse_tls_args(trcfg.keyring, trcfg.tls_key,
928+
trcfg.tls_key_identity, &cfg, c);
966929

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

974937
set_discovery_kato(&cfg);
@@ -1208,9 +1171,10 @@ int nvmf_config(const char *desc, int argc, char **argv)
12081171
_cleanup_nvme_global_ctx_ struct nvme_global_ctx *ctx = NULL;
12091172
int ret;
12101173
struct nvme_fabrics_config cfg;
1174+
struct tr_config trcfg = { };
12111175
bool scan_tree = false, modify_config = false, update_config = false;
12121176

1213-
NVMF_ARGS(opts, cfg,
1177+
NVMF_ARGS(opts, trcfg, cfg,
12141178
OPT_STRING("dhchap-ctrl-secret", 'C', "STR", &ctrlkey, nvmf_ctrlkey),
12151179
OPT_STRING("config", 'J', "FILE", &config_file, nvmf_config_file),
12161180
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)