Skip to content

Commit bb9a386

Browse files
committed
fabrics: move final discovery code to library
The final discovery logic can be moved to the library now. This leaves the nvme-cli with just the parsing operations and setting up the discovery context and then the whole logic happens in the library. The APIs are not final there are very rough and these need more cleanups. But the important tasks was to move the logic code into the library. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 3e69b41 commit bb9a386

6 files changed

Lines changed: 269 additions & 238 deletions

File tree

fabrics.c

Lines changed: 69 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,6 @@ static const char *nvmf_context = "execution context identification string";
129129
OPT_END() \
130130
}
131131

132-
static bool is_persistent_discovery_ctrl(nvme_host_t h, nvme_ctrl_t c)
133-
{
134-
if (nvme_host_is_pdc_enabled(h, DEFAULT_PDC_ENABLED))
135-
return nvme_ctrl_is_unique_discovery_ctrl(c);
136-
137-
return false;
138-
}
139-
140132
nvme_ctrl_t lookup_ctrl(nvme_host_t h, struct tr_config *trcfg)
141133
{
142134
nvme_subsystem_t s;
@@ -192,92 +184,6 @@ static int nvme_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
192184

193185
return ret;
194186
}
195-
196-
static int __create_discover_ctrl(struct nvme_global_ctx *ctx, nvme_host_t h,
197-
struct nvme_fabrics_config *cfg,
198-
struct tr_config *trcfg,
199-
nvme_ctrl_t *ctrl)
200-
{
201-
nvme_ctrl_t c;
202-
int tmo, ret;
203-
204-
ret = nvme_create_ctrl(ctx, trcfg->subsysnqn, trcfg->transport,
205-
trcfg->traddr, trcfg->host_traddr,
206-
trcfg->host_iface, trcfg->trsvcid, &c);
207-
if (ret)
208-
return ret;
209-
210-
nvme_ctrl_set_discovery_ctrl(c, true);
211-
nvme_ctrl_set_unique_discovery_ctrl(c,
212-
strcmp(trcfg->subsysnqn, NVME_DISC_SUBSYS_NAME));
213-
tmo = set_discovery_kato(cfg);
214-
215-
ret = nvme_add_ctrl(h, c, cfg);
216-
cfg->keep_alive_tmo = tmo;
217-
if (ret) {
218-
nvme_free_ctrl(c);
219-
return ret;
220-
}
221-
222-
*ctrl = c;
223-
return 0;
224-
}
225-
226-
int nvmf_create_discover_ctrl(struct nvme_global_ctx *ctx, nvme_host_t h,
227-
struct nvme_fabrics_config *cfg,
228-
struct tr_config *trcfg,
229-
nvme_ctrl_t *ctrl)
230-
{
231-
_cleanup_free_ struct nvme_id_ctrl *id = NULL;
232-
nvme_ctrl_t c;
233-
int ret;
234-
235-
ret = __create_discover_ctrl(ctx, h, cfg, trcfg, &c);
236-
if (ret)
237-
return ret;
238-
239-
if (nvme_ctrl_is_unique_discovery_ctrl(c)) {
240-
*ctrl = c;
241-
return 0;
242-
}
243-
244-
id = nvme_alloc(sizeof(*id));
245-
if (!id) {
246-
nvme_free_ctrl(c);
247-
return -ENOMEM;
248-
}
249-
250-
/* Find out the name of discovery controller */
251-
ret = nvme_ctrl_identify(c, id);
252-
if (ret) {
253-
fprintf(stderr, "failed to identify controller, error %s\n",
254-
nvme_strerror(-ret));
255-
nvme_disconnect_ctrl(c);
256-
nvme_free_ctrl(c);
257-
return ret;
258-
}
259-
260-
if (!strcmp(id->subnqn, NVME_DISC_SUBSYS_NAME)) {
261-
*ctrl = c;
262-
return 0;
263-
}
264-
265-
/*
266-
* The subsysnqn is not the well-known name. Prefer the unique
267-
* subsysnqn over the well-known one.
268-
*/
269-
nvme_disconnect_ctrl(c);
270-
nvme_free_ctrl(c);
271-
272-
trcfg->subsysnqn = id->subnqn;
273-
ret = __create_discover_ctrl(ctx, h, cfg, trcfg, &c);
274-
if (ret)
275-
return ret;
276-
277-
*ctrl = c;
278-
return 0;
279-
}
280-
281187
static void save_discovery_log(char *raw, struct nvmf_discovery_log *log)
282188
{
283189
uint64_t numrec = le64_to_cpu(log->numrec);
@@ -397,6 +303,67 @@ static void parser_cleanup(struct nvmf_discovery_ctx *dctx, void *user_data)
397303
fclose(cdd->f);
398304
}
399305

306+
static int discovery_ctx_set_trcfg(struct nvmf_discovery_ctx *dctx,
307+
struct tr_config *trcfg)
308+
{
309+
int err;
310+
311+
err = nvmf_discovery_ctx_subsysnqn_set(dctx, trcfg->subsysnqn);
312+
if (err)
313+
return err;
314+
315+
err = nvmf_discovery_ctx_transport_set(dctx, trcfg->transport);
316+
if (err)
317+
return err;
318+
319+
err = nvmf_discovery_ctx_traddr_set(dctx, trcfg->traddr);
320+
if (err)
321+
return err;
322+
323+
err = nvmf_discovery_ctx_host_traddr_set(dctx, trcfg->host_traddr);
324+
if (err)
325+
return err;
326+
327+
err = nvmf_discovery_ctx_host_iface_set(dctx, trcfg->host_iface);
328+
if (err)
329+
return err;
330+
331+
err = nvmf_discovery_ctx_trsvcid_set(dctx, trcfg->trsvcid);
332+
if (err)
333+
return err;
334+
335+
err = nvmf_discovery_ctx_hostnqn_set(dctx, trcfg->hostnqn);
336+
if (err)
337+
return err;
338+
339+
err = nvmf_discovery_ctx_hostid_set(dctx, trcfg->hostid);
340+
if (err)
341+
return err;
342+
343+
err = nvmf_discovery_ctx_hostkey_set(dctx, trcfg->hostkey);
344+
if (err)
345+
return err;
346+
347+
err = nvmf_discovery_ctx_ctrlkey_set(dctx, trcfg->ctrlkey);
348+
if (err)
349+
return err;
350+
351+
err = nvmf_discovery_ctx_keyring_set(dctx, trcfg->keyring);
352+
if (err)
353+
return err;
354+
355+
err = nvmf_discovery_ctx_tls_key_set(dctx, trcfg->tls_key);
356+
if (err)
357+
return err;
358+
359+
err = nvmf_discovery_ctx_tls_key_identity_set(dctx,
360+
trcfg->tls_key_identity);
361+
if (err)
362+
return err;
363+
364+
return 0;
365+
}
366+
400367
static int parser_next_line(struct nvmf_discovery_ctx *dctx, void *user_data)
401368
{
402369
struct cb_discovery_data *cdd = user_data;
@@ -434,56 +401,7 @@ static int parser_next_line(struct nvmf_discovery_ctx *dctx, void *user_data)
434401
if (!trcfg.trsvcid)
435402
trcfg.trsvcid = nvmf_get_default_trsvcid(trcfg.transport, true);
436403

437-
ret = nvmf_discovery_ctx_subsysnqn_set(dctx, trcfg.subsysnqn);
438-
if (ret)
439-
return ret;
440-
441-
ret = nvmf_discovery_ctx_transport_set(dctx, trcfg.transport);
442-
if (ret)
443-
return ret;
444-
445-
ret = nvmf_discovery_ctx_traddr_set(dctx, trcfg.traddr);
446-
if (ret)
447-
return ret;
448-
449-
ret = nvmf_discovery_ctx_host_traddr_set(dctx, trcfg.host_traddr);
450-
if (ret)
451-
return ret;
452-
453-
ret = nvmf_discovery_ctx_host_iface_set(dctx, trcfg.host_iface);
454-
if (ret)
455-
return ret;
456-
457-
ret = nvmf_discovery_ctx_trsvcid_set(dctx, trcfg.trsvcid);
458-
if (ret)
459-
return ret;
460-
461-
ret = nvmf_discovery_ctx_hostnqn_set(dctx, trcfg.hostnqn);
462-
if (ret)
463-
return ret;
464-
465-
ret = nvmf_discovery_ctx_hostid_set(dctx, trcfg.hostid);
466-
if (ret)
467-
return ret;
468-
469-
ret = nvmf_discovery_ctx_hostkey_set(dctx, trcfg.hostkey);
470-
if (ret)
471-
return ret;
472-
473-
ret = nvmf_discovery_ctx_ctrlkey_set(dctx, trcfg.ctrlkey);
474-
if (ret)
475-
return ret;
476-
477-
ret = nvmf_discovery_ctx_keyring_set(dctx, trcfg.keyring);
478-
if (ret)
479-
return ret;
480-
481-
ret = nvmf_discovery_ctx_tls_key_set(dctx, trcfg.tls_key);
482-
if (ret)
483-
return ret;
484-
485-
ret = nvmf_discovery_ctx_tls_key_identity_set(dctx,
486-
trcfg.tls_key_identity);
404+
ret = discovery_ctx_set_trcfg(dctx, &trcfg);
487405
if (ret)
488406
return ret;
489407

@@ -496,7 +414,8 @@ static int parser_next_line(struct nvmf_discovery_ctx *dctx, void *user_data)
496414

497415

498416
static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
499-
bool persistent, struct tr_config *trcfg,
417+
bool persistent, const char *device,
418+
struct tr_config *trcfg,
500419
struct nvme_fabrics_config *defcfg,
501420
void *user_data, struct nvmf_discovery_ctx **dctxp)
502421
{
@@ -543,15 +462,15 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
543462
if (err)
544463
goto err;
545464

546-
err = nvmf_discovery_ctx_persistent_set(dctx, persistent);
465+
err = nvmf_discovery_ctx_device_set(dctx, device);
547466
if (err)
548467
goto err;
549468

550-
err = nvmf_discovery_ctx_host_traddr_set(dctx, trcfg->host_traddr);
469+
err = nvmf_discovery_ctx_persistent_set(dctx, persistent);
551470
if (err)
552471
goto err;
553472

554-
err = nvmf_discovery_ctx_host_iface_set(dctx, trcfg->host_iface);
473+
err = discovery_ctx_set_trcfg(dctx, trcfg);
555474
if (err)
556475
goto err;
557476

@@ -623,7 +542,6 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
623542
_cleanup_nvme_global_ctx_ struct nvme_global_ctx *ctx = NULL;
624543
_cleanup_free_ struct nvmf_discovery_ctx *dctx = NULL;
625544
nvme_host_t h;
626-
nvme_ctrl_t c = NULL;
627545
unsigned int verbose = 0;
628546
int ret;
629547
char *format = "normal";
@@ -712,7 +630,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
712630
.flags = flags,
713631
.raw = raw,
714632
};
715-
ret = create_discovery_log_ctx(ctx, persistent, &trcfg,
633+
ret = create_discovery_log_ctx(ctx, persistent, device, &trcfg,
716634
&cfg, &dld, &dctx);
717635
if (ret)
718636
return ret;
@@ -738,81 +656,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
738656
if (!trcfg.trsvcid)
739657
trcfg.trsvcid = nvmf_get_default_trsvcid(trcfg.transport, true);
740658

741-
if (device && !force) {
742-
ret = nvme_scan_ctrl(ctx, device, &c);
743-
if (!ret) {
744-
/* Check if device matches command-line options */
745-
if (!nvme_ctrl_config_match(c, trcfg.transport,
746-
trcfg.traddr, trcfg.trsvcid,
747-
trcfg.subsysnqn, trcfg.host_traddr,
748-
trcfg.host_iface)) {
749-
fprintf(stderr,
750-
"ctrl device %s found, ignoring non matching command-line options\n",
751-
device);
752-
}
753-
754-
if (!nvme_ctrl_is_discovery_ctrl(c)) {
755-
fprintf(stderr,
756-
"ctrl device %s found, ignoring non discovery controller\n",
757-
device);
758-
759-
nvme_free_ctrl(c);
760-
c = NULL;
761-
persistent = false;
762-
} else {
763-
/*
764-
* If the controller device is found it must
765-
* be persistent, and shouldn't be disconnected
766-
* on exit.
767-
*/
768-
persistent = true;
769-
/*
770-
* When --host-traddr/--host-iface are not specified on the
771-
* command line, use the discovery controller's (c) host-
772-
* traddr/host-iface for the connections to controllers
773-
* returned in the Discovery Log Pages. This is essential
774-
* when invoking "connect-all" with --device to reuse an
775-
* existing persistent discovery controller (as is done
776-
* for the udev rules). This ensures that host-traddr/
777-
* host-iface are consistent with the discovery controller (c).
778-
*/
779-
if (!trcfg.host_traddr)
780-
trcfg.host_traddr = (char *)nvme_ctrl_get_host_traddr(c);
781-
if (!trcfg.host_iface)
782-
trcfg.host_iface = (char *)nvme_ctrl_get_host_iface(c);
783-
}
784-
} else {
785-
/*
786-
* No controller found, fall back to create one.
787-
* But that controller cannot be persistent.
788-
*/
789-
fprintf(stderr,
790-
"ctrl device %s not found%s\n", device,
791-
persistent ? ", ignoring --persistent" : "");
792-
persistent = false;
793-
}
794-
}
795-
if (!c && !force) {
796-
c = lookup_ctrl(h, &trcfg);
797-
if (c)
798-
persistent = true;
799-
}
800-
if (!c) {
801-
/* No device or non-matching device, create a new controller */
802-
ret = nvmf_create_discover_ctrl(ctx, h, &cfg, &trcfg, &c);
803-
if (ret) {
804-
if (ret != -ENVME_CONNECT_IGNORED)
805-
fprintf(stderr,
806-
"failed to add controller, error %s\n",
807-
nvme_strerror(-ret));
808-
goto out_free;
809-
}
810-
}
811-
812-
ret = nvmf_discovery(ctx, dctx, connect, c);
813-
if (!(persistent || is_persistent_discovery_ctrl(h, c)))
814-
nvme_disconnect_ctrl(c);
815-
nvme_free_ctrl(c);
659+
ret = nvmf_discovery(ctx, dctx, h, connect, force);
816660

817661
out_free:
818662
if (dump_config)

fabrics.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ extern int nvmf_disconnect(const char *desc, int argc, char **argv);
2626
extern int nvmf_disconnect_all(const char *desc, int argc, char **argv);
2727
extern int nvmf_config(const char *desc, int argc, char **argv);
2828
extern int nvmf_dim(const char *desc, int argc, char **argv);
29-
extern int nvmf_create_discover_ctrl(struct nvme_global_ctx *ctx, nvme_host_t h,
30-
struct nvme_fabrics_config *cfg,
31-
struct tr_config *trcfg,
32-
nvme_ctrl_t *ctrl);
33-
extern char *nvmf_get_default_trsvcid(const char *transport,
34-
bool discovery_ctrl);
3529

3630

3731
#endif

libnvme/src/libnvme.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ LIBNVME_2_0 {
300300
nvmf_discovery_ctx_ctrlkey_set;
301301
nvmf_discovery_ctx_decide_retry_set;
302302
nvmf_discovery_ctx_default_fabrics_config_set;
303+
nvmf_discovery_ctx_device_set;
303304
nvmf_discovery_ctx_discovery_log_set;
304305
nvmf_discovery_ctx_host_iface_set;
305306
nvmf_discovery_ctx_host_traddr_set;

0 commit comments

Comments
 (0)