Skip to content

Commit

Permalink
nbft: Report proper error codes from discover_from_nbft()
Browse files Browse the repository at this point in the history
In case of a connection failure of any SSNS records that are not
marked as 'unavailable', report proper error code so that nvme-cli
callers may retry the operation.

The semantics of most functions is to return 0 zero for success
and negative errno numbers in case of an error.

Signed-off-by: Tomas Bzatek <[email protected]>
  • Loading branch information
tbzatek committed Dec 11, 2024
1 parent cbfd136 commit 5743cc2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
14 changes: 7 additions & 7 deletions fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ static int nvme_read_config_checked(nvme_root_t r, const char *filename)
return 0;
}

/* returns negative errno values */
int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
{
char *subsysnqn = NVME_DISC_SUBSYS_NAME;
Expand Down Expand Up @@ -757,7 +758,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
if (ret < 0) {
fprintf(stderr, "Failed to scan topology: %s\n",
nvme_strerror(errno));
return ret;
return -errno;
}

ret = nvme_host_get_ids(r, hostnqn, hostid, &hnqn, &hid);
Expand All @@ -766,7 +767,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)

h = nvme_lookup_host(r, hnqn, hid);
if (!h) {
ret = ENOMEM;
ret = -ENOMEM;
goto out_free;
}

Expand All @@ -781,10 +782,9 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)

if (!device && !transport && !traddr) {
if (!nonbft)
discover_from_nbft(r, hostnqn, hostid,
hnqn, hid, desc, connect,
&cfg, nbft_path, flags, verbose);

ret = discover_from_nbft(r, hostnqn, hostid,
hnqn, hid, desc, connect,
&cfg, nbft_path, flags, verbose);
if (nbft)
goto out_free;

Expand Down Expand Up @@ -876,7 +876,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
fprintf(stderr,
"failed to add controller, error %s\n",
nvme_strerror(errno));
ret = errno;
ret = -errno;
goto out_free;
}
}
Expand Down
46 changes: 29 additions & 17 deletions nbft.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ static int do_discover(struct nbft_info_discovery *dd,
return 0;
}

/* returns negative errno values */
int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
char *hostnqn_sys, char *hostid_sys,
const char *desc, bool connect,
Expand All @@ -290,22 +291,24 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
{
char *hostnqn = NULL, *hostid = NULL, *host_traddr = NULL;
nvme_host_t h;
int ret, i;
int ret, rr, i;
struct list_head nbft_list;
struct nbft_file_entry *entry = NULL;
struct nbft_info_subsystem_ns **ss;
struct nbft_info_hfi *hfi;
struct nbft_info_discovery **dd;

if (!connect)
/* to do: print discovery-type info from NBFT tables */
/* TODO: print discovery-type info from NBFT tables */
return 0;

list_head_init(&nbft_list);
ret = read_nbft_files(&nbft_list, nbft_path);
if (ret) {
if (ret != -ENOENT)
nvme_show_perror("Failed to access ACPI tables directory");
else
ret = 0; /* nothing to connect */
goto out_free;
}

Expand All @@ -327,8 +330,10 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
}

h = nvme_lookup_host(r, hostnqn, hostid);
if (!h)
if (!h) {
ret = -ENOENT;
goto out_free;
}

/* Subsystem Namespace Descriptor List */
for (ss = entry->nbft->subsystem_ns_list; ss && *ss; ss++)
Expand Down Expand Up @@ -358,34 +363,37 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
.trsvcid = (*ss)->trsvcid,
};

ret = do_connect(r, h, NULL, *ss, &trcfg,
cfg, flags, verbose);
rr = do_connect(r, h, NULL, *ss, &trcfg,
cfg, flags, verbose);

/*
* With TCP/DHCP, it can happen that the OS
* obtains a different local IP address than the
* firmware had. Retry without host_traddr.
*/
if (ret == -ENVME_CONNECT_ADDRNOTAVAIL &&
if (rr == -ENVME_CONNECT_ADDRNOTAVAIL &&
!strcmp(trcfg.transport, "tcp") &&
strlen(hfi->tcp_info.dhcp_server_ipaddr) > 0) {
trcfg.host_traddr = NULL;

ret = do_connect(r, h, NULL, *ss, &trcfg,
cfg, flags, verbose);
rr = do_connect(r, h, NULL, *ss, &trcfg,
cfg, flags, verbose);

if (ret == 0 && verbose >= 1)
if (rr == 0 && verbose >= 1)
fprintf(stderr,
"SSNS %d: connect with host_traddr=\"%s\" failed, success after omitting host_traddr\n",
(*ss)->index,
host_traddr);
}

if (ret)
if (rr) {
fprintf(stderr, "SSNS %d: no controller found\n",
(*ss)->index);
/* report an error */
ret = rr;
}

if (ret == -ENOMEM)
if (rr == -ENOMEM)
goto out_free;
}

Expand Down Expand Up @@ -423,7 +431,7 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
host_traddr = hfi->tcp_info.ipaddr;
if (uri->port > 0) {
if (asprintf(&trsvcid, "%d", uri->port) < 0) {
errno = ENOMEM;
ret = -ENOMEM;
goto out_free;
}
} else
Expand Down Expand Up @@ -458,21 +466,25 @@ int discover_from_nbft(nvme_root_t r, char *hostnqn_arg, char *hostid_arg,
"Discovery Descriptor %d: failed to add discovery controller: %s\n",
(*dd)->index,
nvme_strerror(errno));
if (errno == ENOMEM)
if (errno == ENOMEM) {
ret = -ENOMEM;
goto out_free;
}
continue;
}

ret = do_discover(*dd, r, h, c, cfg, &trcfg,
flags, verbose);
rr = do_discover(*dd, r, h, c, cfg, &trcfg,
flags, verbose);
if (!persistent)
nvme_disconnect_ctrl(c);
nvme_free_ctrl(c);
if (ret == -ENOMEM)
if (rr == -ENOMEM) {
ret = rr;
goto out_free;
}
}
}
out_free:
free_nbfts(&nbft_list);
return errno;
return ret;
}

0 comments on commit 5743cc2

Please sign in to comment.