All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH libnvme] fabrics: restore hostname traddr support
@ 2021-09-26 18:29 Sagi Grimberg
  2021-09-27 19:39 ` Chaitanya Kulkarni
  0 siblings, 1 reply; 3+ messages in thread
From: Sagi Grimberg @ 2021-09-26 18:29 UTC (permalink / raw)
  To: linux-nvme, Keith Busch; +Cc: Hannes Reinecke

We used to support hostname passed in traddr, with the move of nvme-cli
to use libnvme we lost this capability, now lets restore it.

Signed-off-by: Sagi Grimberg <sagi@grimberg.me>
---
 src/nvme/fabrics.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/src/nvme/fabrics.c b/src/nvme/fabrics.c
index 4b0ba16e66a8..ebfc6356844d 100644
--- a/src/nvme/fabrics.c
+++ b/src/nvme/fabrics.c
@@ -21,6 +21,8 @@
 
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <arpa/inet.h>
+#include <netdb.h>
 
 #ifdef CONFIG_SYSTEMD
 #include <systemd/sd-id128.h>
@@ -213,6 +215,63 @@ static int add_argument(char **argstr, const char *tok, const char *arg)
 	return 0;
 }
 
+static bool traddr_is_hostname(nvme_ctrl_t c)
+{
+	char addrstr[NVMF_TRADDR_SIZE];
+
+	if (!c->traddr)
+		return false;
+	if (strcmp(c->transport, "tcp") && strcmp(c->transport, "rdma"))
+		return false;
+	if (inet_pton(AF_INET, c->traddr, addrstr) > 0 ||
+	    inet_pton(AF_INET6, c->traddr, addrstr) > 0)
+		return false;
+	return true;
+}
+
+static int hostname2traddr(nvme_ctrl_t c)
+{
+	struct addrinfo *host_info, hints = {.ai_family = AF_UNSPEC};
+	char addrstr[NVMF_TRADDR_SIZE];
+	const char *p;
+	int ret;
+
+	ret = getaddrinfo(c->traddr, NULL, &hints, &host_info);
+	if (ret) {
+		fprintf(stderr, "failed to resolve host %s info\n", c->traddr);
+		return ret;
+	}
+
+	switch (host_info->ai_family) {
+	case AF_INET:
+		p = inet_ntop(host_info->ai_family,
+			&(((struct sockaddr_in *)host_info->ai_addr)->sin_addr),
+			addrstr, NVMF_TRADDR_SIZE);
+		break;
+	case AF_INET6:
+		p = inet_ntop(host_info->ai_family,
+			&(((struct sockaddr_in6 *)host_info->ai_addr)->sin6_addr),
+			addrstr, NVMF_TRADDR_SIZE);
+		break;
+	default:
+		fprintf(stderr, "unrecognized address family (%d) %s\n",
+			host_info->ai_family, c->traddr);
+		ret = -EINVAL;
+		goto free_addrinfo;
+	}
+
+	if (!p) {
+		fprintf(stderr, "failed to get traddr for %s\n", c->traddr);
+		ret = -errno;
+		goto free_addrinfo;
+	}
+	c->traddr = strdup(addrstr);
+
+free_addrinfo:
+	freeaddrinfo(host_info);
+	return ret;
+}
+
 static int build_options(nvme_host_t h, nvme_ctrl_t c, char **argstr)
 {
 	struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
@@ -353,6 +412,11 @@ int nvmf_add_ctrl_opts(nvme_ctrl_t c, struct nvme_fabrics_config *cfg)
 	int ret;
 
 	cfg = merge_config(c, cfg);
+	if (traddr_is_hostname(c)) {
+		ret = hostname2traddr(c);
+		if (ret)
+			return ret;
+	}
 
 	ret = build_options(h, c, &argstr);
 	if (ret)
@@ -375,6 +439,11 @@ int nvmf_add_ctrl(nvme_host_t h, nvme_ctrl_t c,
 	cfg = merge_config(c, cfg);
 	nvme_ctrl_disable_sqflow(c, disable_sqflow);
 	nvme_ctrl_set_discovered(c, true);
+	if (traddr_is_hostname(c)) {
+		ret = hostname2traddr(c);
+		if (ret)
+			return ret;
+	}
 
 	ret = build_options(h, c, &argstr);
 	if (ret)
-- 
2.30.2


_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH libnvme] fabrics: restore hostname traddr support
  2021-09-26 18:29 [PATCH libnvme] fabrics: restore hostname traddr support Sagi Grimberg
@ 2021-09-27 19:39 ` Chaitanya Kulkarni
  2021-09-27 20:22   ` Keith Busch
  0 siblings, 1 reply; 3+ messages in thread
From: Chaitanya Kulkarni @ 2021-09-27 19:39 UTC (permalink / raw)
  To: linux-nvme


> +static int hostname2traddr(nvme_ctrl_t c)
> +{
> +	struct addrinfo *host_info, hints = {.ai_family = AF_UNSPEC};
> +	char addrstr[NVMF_TRADDR_SIZE];

do we need to setup the addrstr to 0 with = { 0 }; ?

> +	const char *p;
> +	int ret;

With that comment looks good to me.

Looks good.

Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH libnvme] fabrics: restore hostname traddr support
  2021-09-27 19:39 ` Chaitanya Kulkarni
@ 2021-09-27 20:22   ` Keith Busch
  0 siblings, 0 replies; 3+ messages in thread
From: Keith Busch @ 2021-09-27 20:22 UTC (permalink / raw)
  To: Chaitanya Kulkarni; +Cc: linux-nvme

On Mon, Sep 27, 2021 at 07:39:53PM +0000, Chaitanya Kulkarni wrote:
> 
> > +static int hostname2traddr(nvme_ctrl_t c)
> > +{
> > +	struct addrinfo *host_info, hints = {.ai_family = AF_UNSPEC};
> > +	char addrstr[NVMF_TRADDR_SIZE];
> 
> do we need to setup the addrstr to 0 with = { 0 }; ?

Looks like it's unconditionally set if it is actually used, so should be
fine as-is.

Patch applied.

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-27 20:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-26 18:29 [PATCH libnvme] fabrics: restore hostname traddr support Sagi Grimberg
2021-09-27 19:39 ` Chaitanya Kulkarni
2021-09-27 20:22   ` Keith Busch

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.