From: Sagi Grimberg <sagi@grimberg.me>
To: linux-nvme@lists.infradead.org, Keith Busch <kbusch@kernel.org>
Subject: [PATCH rfc 2/2] fabrics: allow user to pass hostname instead of traddr
Date: Tue, 24 Mar 2020 02:03:24 -0700 [thread overview]
Message-ID: <20200324090324.24459-3-sagi@grimberg.me> (raw)
In-Reply-To: <20200324090324.24459-1-sagi@grimberg.me>
Some users would like to use well known hostnames instead
of remembering ip addresses. So, allow users to pass --hostname
and we will attempt to resolve against the DNS.
Signed-off-by: Sagi Grimberg <sagi@grimberg.me>
---
fabrics.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git a/fabrics.c b/fabrics.c
index a7d628b1f0c9..7bd95c4b0d10 100644
--- a/fabrics.c
+++ b/fabrics.c
@@ -33,6 +33,10 @@
#include <sys/stat.h>
#include <stddef.h>
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
#include "util/parser.h"
#include "nvme-ioctl.h"
#include "nvme-status.h"
@@ -60,6 +64,7 @@ static struct config {
char *nqn;
char *transport;
char *traddr;
+ char *hostname;
char *trsvcid;
char *host_traddr;
char *hostnqn;
@@ -857,6 +862,54 @@ static int build_options(char *argstr, int max_len, bool discover)
return 0;
}
+static int hostname2traddr(struct config *cfg)
+{
+ struct addrinfo *host_info, hints = {.ai_family = AF_UNSPEC};
+ char addrstr[NVMF_TRADDR_SIZE];
+ const char *p;
+ int ret;
+
+ if (cfg->traddr) {
+ fprintf(stderr, "hostname and traddr cannot be passed together\n");
+ return -EINVAL;
+ }
+
+ ret = getaddrinfo(cfg->hostname, NULL, &hints, &host_info);
+ if (ret) {
+ fprintf(stderr, "failed to get host info for %s\n", cfg->hostname);
+ 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, cfg->hostname);
+ ret = -EINVAL;
+ goto free_addrinfo;
+ }
+
+ if (!p) {
+ fprintf(stderr, "failed to get traddr for %s\n", cfg->hostname);
+ ret = -errno;
+ goto free_addrinfo;
+ }
+ cfg->traddr = strdup(addrstr);
+
+free_addrinfo:
+ freeaddrinfo(host_info);
+ return ret;
+}
+
static int connect_ctrl(struct nvmf_disc_rsp_page_entry *e)
{
char argstr[BUF_SIZE], *p;
@@ -1230,6 +1283,13 @@ static int discover_from_conf_file(const char *desc, char *argstr,
if (cfg.persistent && !cfg.keep_alive_tmo)
cfg.keep_alive_tmo = NVMF_DEF_DISC_TMO;
+ if (cfg.hostname) {
+ ret = hostname2traddr(&cfg);
+ if (ret)
+ goto out;
+ cfg.hostname = NULL;
+ }
+
err = build_options(argstr, BUF_SIZE, true);
if (err) {
ret = err;
@@ -1259,6 +1319,7 @@ int fabrics_discover(const char *desc, int argc, char **argv, bool connect)
OPT_ARGS(opts) = {
OPT_LIST("transport", 't', &cfg.transport, "transport type"),
OPT_LIST("traddr", 'a', &cfg.traddr, "transport address"),
+ OPT_LIST("hostname", 'A', &cfg.hostname, "transport address in a hostname form"),
OPT_LIST("trsvcid", 's', &cfg.trsvcid, "transport service id (e.g. IP port)"),
OPT_LIST("host-traddr", 'w', &cfg.host_traddr, "host traddr (e.g. FC WWN's)"),
OPT_LIST("hostnqn", 'q', &cfg.hostnqn, "user-defined hostnqn (if default not used)"),
@@ -1295,6 +1356,13 @@ int fabrics_discover(const char *desc, int argc, char **argv, bool connect)
} else {
if (cfg.persistent && !cfg.keep_alive_tmo)
cfg.keep_alive_tmo = NVMF_DEF_DISC_TMO;
+
+ if (cfg.hostname) {
+ ret = hostname2traddr(&cfg);
+ if (ret)
+ goto out;
+ }
+
ret = build_options(argstr, BUF_SIZE, true);
if (ret)
goto out;
@@ -1315,6 +1383,7 @@ int fabrics_connect(const char *desc, int argc, char **argv)
OPT_LIST("transport", 't', &cfg.transport, "transport type"),
OPT_LIST("nqn", 'n', &cfg.nqn, "nqn name"),
OPT_LIST("traddr", 'a', &cfg.traddr, "transport address"),
+ OPT_LIST("hostname", 'A', &cfg.hostname, "transport address in a hostname form"),
OPT_LIST("trsvcid", 's', &cfg.trsvcid, "transport service id (e.g. IP port)"),
OPT_LIST("host-traddr", 'w', &cfg.host_traddr, "host traddr (e.g. FC WWN's)"),
OPT_LIST("hostnqn", 'q', &cfg.hostnqn, "user-defined hostnqn"),
@@ -1339,6 +1408,12 @@ int fabrics_connect(const char *desc, int argc, char **argv)
if (ret)
goto out;
+ if (cfg.hostname) {
+ ret = hostname2traddr(&cfg);
+ if (ret)
+ goto out;
+ }
+
ret = build_options(argstr, BUF_SIZE, false);
if (ret)
goto out;
--
2.20.1
_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme
next prev parent reply other threads:[~2020-03-24 9:03 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-24 9:03 [PATCH rfc 0/2] support ip resolution with hostnames Sagi Grimberg
2020-03-24 9:03 ` [PATCH rfc 1/2] fabrics: add fabrics_ prefix to fabrics operations Sagi Grimberg
2020-03-24 9:03 ` Sagi Grimberg [this message]
2020-03-24 9:46 ` [PATCH rfc 2/2] fabrics: allow user to pass hostname instead of traddr Max Gurtovoy
2020-03-24 16:21 ` Sagi Grimberg
2020-03-24 17:24 ` Max Gurtovoy
2020-03-24 19:11 ` Sagi Grimberg
2020-03-24 22:57 ` Max Gurtovoy
2020-04-01 19:39 ` Keith Busch
2020-04-01 20:33 ` Sagi Grimberg
2020-04-01 20:43 ` Keith Busch
2020-03-24 9:06 ` [PATCH rfc 0/2] support ip resolution with hostnames Sagi Grimberg
2020-04-01 6:03 ` Sagi Grimberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200324090324.24459-3-sagi@grimberg.me \
--to=sagi@grimberg.me \
--cc=kbusch@kernel.org \
--cc=linux-nvme@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).