Skip to content

Commit 2510496

Browse files
committed
libnvme: Separate out _nvme_create_ctrl()
Separate out _nvme_create_ctrl() as a simpler interface to nvme_create_ctrl() which accepts a 'struct nvmf_context' as argument. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent c5ec40a commit 2510496

3 files changed

Lines changed: 61 additions & 45 deletions

File tree

libnvme/src/nvme/fabrics.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,10 @@ static int nvmf_connect_disc_entry(nvme_host_t h,
11021102
const struct nvme_fabrics_config *cfg,
11031103
bool *discover, nvme_ctrl_t *cp)
11041104
{
1105-
char *traddr = NULL, *trsvcid = NULL;
1106-
const char *transport;
1105+
struct nvmf_context fctx = {
1106+
.host_traddr = host_traddr,
1107+
.host_iface = host_iface,
1108+
};
11071109
nvme_ctrl_t c;
11081110
int ret;
11091111

@@ -1113,8 +1115,8 @@ static int nvmf_connect_disc_entry(nvme_host_t h,
11131115
switch (e->adrfam) {
11141116
case NVMF_ADDR_FAMILY_IP4:
11151117
case NVMF_ADDR_FAMILY_IP6:
1116-
traddr = e->traddr;
1117-
trsvcid = e->trsvcid;
1118+
fctx.traddr = e->traddr;
1119+
fctx.trsvcid = e->trsvcid;
11181120
break;
11191121
default:
11201122
nvme_msg(h->ctx, LOG_ERR,
@@ -1126,7 +1128,7 @@ static int nvmf_connect_disc_entry(nvme_host_t h,
11261128
case NVMF_TRTYPE_FC:
11271129
switch (e->adrfam) {
11281130
case NVMF_ADDR_FAMILY_FC:
1129-
traddr = e->traddr;
1131+
fctx.traddr = e->traddr;
11301132
break;
11311133
default:
11321134
nvme_msg(h->ctx, LOG_ERR,
@@ -1136,26 +1138,26 @@ static int nvmf_connect_disc_entry(nvme_host_t h,
11361138
}
11371139
break;
11381140
case NVMF_TRTYPE_LOOP:
1139-
traddr = strlen(e->traddr) ? e->traddr : NULL;
1141+
fctx.traddr = strlen(e->traddr) ? e->traddr : NULL;
11401142
break;
11411143
default:
11421144
nvme_msg(h->ctx, LOG_ERR, "skipping unsupported transport %d\n",
11431145
e->trtype);
11441146
return -EINVAL;
11451147
}
11461148

1147-
transport = nvmf_trtype_str(e->trtype);
1149+
fctx.transport = nvmf_trtype_str(e->trtype);
1150+
fctx.subsysnqn = e->subnqn;
11481151

11491152
nvme_msg(h->ctx, LOG_DEBUG, "lookup ctrl "
11501153
"(transport: %s, traddr: %s, trsvcid %s)\n",
1151-
transport, traddr, trsvcid);
1154+
fctx.transport, fctx.traddr, fctx.trsvcid);
11521155

1153-
ret = nvme_create_ctrl(h->ctx, e->subnqn, transport, traddr,
1154-
host_traddr, host_iface, trsvcid, &c);
1156+
ret = _nvme_create_ctrl(h->ctx, &fctx, &c);
11551157
if (ret) {
11561158
nvme_msg(h->ctx, LOG_DEBUG, "skipping discovery entry, "
11571159
"failed to allocate %s controller with traddr %s\n",
1158-
transport, traddr);
1160+
fctx.transport, fctx.traddr);
11591161
return ret;
11601162
}
11611163

@@ -2070,9 +2072,7 @@ static int __create_discovery_ctrl(struct nvme_global_ctx *ctx,
20702072
nvme_ctrl_t c;
20712073
int tmo, ret;
20722074

2073-
ret = nvme_create_ctrl(ctx, fctx->subsysnqn, fctx->transport,
2074-
fctx->traddr, fctx->host_traddr,
2075-
fctx->host_iface, fctx->trsvcid, &c);
2075+
ret = _nvme_create_ctrl(ctx, fctx, &c);
20762076
if (ret)
20772077
return ret;
20782078

@@ -2551,9 +2551,7 @@ static int nbft_connect(struct nvme_global_ctx *ctx,
25512551
if (c && nvme_ctrl_get_name(c))
25522552
return 0;
25532553

2554-
ret = nvme_create_ctrl(ctx, fctx->subsysnqn, fctx->transport,
2555-
fctx->traddr, fctx->host_traddr,
2556-
fctx->host_iface, fctx->trsvcid, &c);
2554+
ret = _nvme_create_ctrl(ctx, fctx, &c);
25572555
if (ret)
25582556
return ret;
25592557

@@ -3034,9 +3032,7 @@ __public int nvmf_connect(struct nvme_global_ctx *ctx, struct nvmf_context *fctx
30343032
return -EALREADY;
30353033
}
30363034

3037-
err = nvme_create_ctrl(ctx, fctx->subsysnqn, fctx->transport,
3038-
fctx->traddr, fctx->host_traddr, fctx->host_iface,
3039-
fctx->trsvcid, &c);
3035+
err = _nvme_create_ctrl(ctx, fctx, &c);
30403036
if (err)
30413037
return err;
30423038

libnvme/src/nvme/private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ int __nvme_transport_handle_open_mi(struct nvme_transport_handle *hdl, const cha
381381
int __nvme_transport_handle_init_mi(struct nvme_transport_handle *hdl);
382382
void __nvme_transport_handle_close_mi(struct nvme_transport_handle *hdl);
383383

384+
int _nvme_create_ctrl(struct nvme_global_ctx *ctx,
385+
struct nvmf_context *fctx,
386+
nvme_ctrl_t *cp);
384387
nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s,
385388
struct nvmf_context *fctx,
386389
nvme_ctrl_t p);

libnvme/src/nvme/tree.c

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,26 +1062,24 @@ __public void nvmf_default_config(struct nvme_fabrics_config *cfg)
10621062
cfg->ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
10631063
}
10641064

1065-
__public int nvme_create_ctrl(struct nvme_global_ctx *ctx,
1066-
const char *subsysnqn, const char *transport,
1067-
const char *traddr, const char *host_traddr,
1068-
const char *host_iface, const char *trsvcid,
1069-
nvme_ctrl_t *cp)
1065+
int _nvme_create_ctrl(struct nvme_global_ctx *ctx,
1066+
struct nvmf_context *fctx,
1067+
nvme_ctrl_t *cp)
10701068
{
10711069
struct nvme_ctrl *c;
10721070

1073-
if (!transport) {
1071+
if (!fctx->transport) {
10741072
nvme_msg(ctx, LOG_ERR, "No transport specified\n");
10751073
return -EINVAL;
10761074
}
1077-
if (strncmp(transport, "loop", 4) &&
1078-
strncmp(transport, "pcie", 4) &&
1079-
strncmp(transport, "apple-nvme", 10) && !traddr) {
1075+
if (strncmp(fctx->transport, "loop", 4) &&
1076+
strncmp(fctx->transport, "pcie", 4) &&
1077+
strncmp(fctx->transport, "apple-nvme", 10) && !fctx->traddr) {
10801078
nvme_msg(ctx, LOG_ERR, "No transport address for '%s'\n",
1081-
transport);
1079+
fctx->transport);
10821080
return -EINVAL;
10831081
}
1084-
if (!subsysnqn) {
1082+
if (!fctx->subsysnqn) {
10851083
nvme_msg(ctx, LOG_ERR, "No subsystem NQN specified\n");
10861084
return -EINVAL;
10871085
}
@@ -1095,25 +1093,44 @@ __public int nvme_create_ctrl(struct nvme_global_ctx *ctx,
10951093
list_head_init(&c->namespaces);
10961094
list_head_init(&c->paths);
10971095
list_node_init(&c->entry);
1098-
c->transport = strdup(transport);
1099-
c->subsysnqn = strdup(subsysnqn);
1100-
if (traddr)
1101-
c->traddr = strdup(traddr);
1102-
if (host_traddr) {
1103-
if (traddr_is_hostname(transport, host_traddr))
1104-
hostname2traddr(ctx, host_traddr, &c->host_traddr);
1096+
c->transport = strdup(fctx->transport);
1097+
c->subsysnqn = strdup(fctx->subsysnqn);
1098+
if (fctx->traddr)
1099+
c->traddr = strdup(fctx->traddr);
1100+
if (fctx->host_traddr) {
1101+
if (traddr_is_hostname(fctx->transport, fctx->host_traddr))
1102+
hostname2traddr(ctx, fctx->host_traddr,
1103+
&c->host_traddr);
11051104
if (!c->host_traddr)
1106-
c->host_traddr = strdup(host_traddr);
1105+
c->host_traddr = strdup(fctx->host_traddr);
11071106
}
1108-
if (host_iface)
1109-
c->host_iface = strdup(host_iface);
1110-
if (trsvcid)
1111-
c->trsvcid = strdup(trsvcid);
1107+
if (fctx->host_iface)
1108+
c->host_iface = strdup(fctx->host_iface);
1109+
if (fctx->trsvcid)
1110+
c->trsvcid = strdup(fctx->trsvcid);
11121111

11131112
*cp = c;
11141113
return 0;
11151114
}
11161115

1116+
__public int nvme_create_ctrl(struct nvme_global_ctx *ctx,
1117+
const char *subsysnqn, const char *transport,
1118+
const char *traddr, const char *host_traddr,
1119+
const char *host_iface, const char *trsvcid,
1120+
nvme_ctrl_t *cp)
1121+
{
1122+
struct nvmf_context fctx = {
1123+
.transport = transport,
1124+
.traddr = traddr,
1125+
.host_traddr = host_traddr,
1126+
.host_iface = host_iface,
1127+
.trsvcid = trsvcid,
1128+
.subsysnqn = subsysnqn,
1129+
};
1130+
1131+
return _nvme_create_ctrl(ctx, &fctx, cp);
1132+
}
1133+
11171134
/**
11181135
* _tcp_ctrl_match_host_traddr_no_src_addr() - Match host_traddr w/o src_addr
11191136
* @c: An existing controller instance
@@ -1508,8 +1525,8 @@ nvme_ctrl_t nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,
15081525
return c;
15091526

15101527
ctx = s->h ? s->h->ctx : NULL;
1511-
ret = nvme_create_ctrl(ctx, s->subsysnqn, transport, traddr,
1512-
host_traddr, host_iface, trsvcid, &c);
1528+
fctx.subsysnqn = s->subsysnqn;
1529+
ret = _nvme_create_ctrl(ctx, &fctx, &c);
15131530
if (ret)
15141531
return NULL;
15151532

0 commit comments

Comments
 (0)