Skip to content

Commit b5bb3c1

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 86a7da4 commit b5bb3c1

File tree

6 files changed

+280
-239
lines changed

6 files changed

+280
-239
lines changed

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);
@@ -398,6 +304,67 @@ static void parser_cleanup(struct nvmf_discovery_ctx *dctx, void *user_data)
398304
fclose(cdd->f);
399305
}
400306

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

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

@@ -497,7 +415,8 @@ static int parser_next_line(struct nvmf_discovery_ctx *dctx, void *user_data)
497415

498416

499417
static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
500-
bool persistent, struct tr_config *trcfg,
418+
bool persistent, const char *device,
419+
struct tr_config *trcfg,
501420
struct nvme_fabrics_config *defcfg,
502421
void *user_data, struct nvmf_discovery_ctx **dctxp)
503422
{
@@ -544,15 +463,15 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
544463
if (err)
545464
goto err;
546465

547-
err = nvmf_discovery_ctx_persistent_set(dctx, persistent);
466+
err = nvmf_discovery_ctx_device_set(dctx, device);
548467
if (err)
549468
goto err;
550469

551-
err = nvmf_discovery_ctx_host_traddr_set(dctx, trcfg->host_traddr);
470+
err = nvmf_discovery_ctx_persistent_set(dctx, persistent);
552471
if (err)
553472
goto err;
554473

555-
err = nvmf_discovery_ctx_host_iface_set(dctx, trcfg->host_iface);
474+
err = discovery_ctx_set_trcfg(dctx, trcfg);
556475
if (err)
557476
goto err;
558477

@@ -624,7 +543,6 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
624543
_cleanup_nvme_global_ctx_ struct nvme_global_ctx *ctx = NULL;
625544
_cleanup_free_ struct nvmf_discovery_ctx *dctx = NULL;
626545
nvme_host_t h;
627-
nvme_ctrl_t c = NULL;
628546
unsigned int verbose = 0;
629547
int ret;
630548
char *format = "normal";
@@ -713,7 +631,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
713631
.flags = flags,
714632
.raw = raw,
715633
};
716-
ret = create_discovery_log_ctx(ctx, persistent, &trcfg,
634+
ret = create_discovery_log_ctx(ctx, persistent, device, &trcfg,
717635
&cfg, &dld, &dctx);
718636
if (ret)
719637
return ret;
@@ -739,81 +657,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
739657
if (!trcfg.trsvcid)
740658
trcfg.trsvcid = nvmf_get_default_trsvcid(trcfg.transport, true);
741659

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

818662
out_free:
819663
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)