public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Leon Romanovsky <leon@kernel.org>
To: David Ahern <dsahern@gmail.com>
Cc: Leon Romanovsky <leonro@mellanox.com>,
	netdev <netdev@vger.kernel.org>,
	Stephen Hemminger <stephen@networkplumber.org>,
	RDMA mailing list <linux-rdma@vger.kernel.org>
Subject: [PATCH iproute2-next] rdma: Add batch command support
Date: Wed, 21 Feb 2018 14:38:25 +0200	[thread overview]
Message-ID: <20180221123825.3042-1-leon@kernel.org> (raw)

From: Leon Romanovsky <leonro@mellanox.com>

Implement an option (-b) to execute RDMAtool commands
from supplied file. This follows the same model as
in use for ip and devlink tools, by expecting
every new command to be on new line.

These commands are expected to be without any -*
(e.g. -d, -j, e.t.c) global flags, which should be
called externally.

Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
---
 man/man8/rdma.8 | 16 +++++++++++++
 rdma/rdma.c     | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 78 insertions(+), 8 deletions(-)

diff --git a/man/man8/rdma.8 b/man/man8/rdma.8
index 798b33d3..fba77693 100644
--- a/man/man8/rdma.8
+++ b/man/man8/rdma.8
@@ -11,6 +11,12 @@ rdma \- RDMA tool
 .BR help " }"
 .sp

+.ti -8
+.B rdma
+.RB "[ " -force " ] "
+.BI "-batch " filename
+.sp
+
 .ti -8
 .IR OBJECT " := { "
 .BR dev " | " link " }"
@@ -31,6 +37,16 @@ Print the version of the
 .B rdma
 tool and exit.

+.TP
+.BR "\-b", " \-batch " <FILENAME>
+Read commands from provided file or standard input and invoke them.
+First failure will cause termination of rdma.
+
+.TP
+.BR "\-force"
+Don't terminate rdma on errors in batch mode.
+If there were any errors during execution of the commands, the application return code will be non zero.
+
 .TP
 .BR "\-d" , " --details"
 Otuput detailed information.
diff --git a/rdma/rdma.c b/rdma/rdma.c
index 19608f41..ab8f98d2 100644
--- a/rdma/rdma.c
+++ b/rdma/rdma.c
@@ -15,8 +15,9 @@
 static void help(char *name)
 {
 	pr_out("Usage: %s [ OPTIONS ] OBJECT { COMMAND | help }\n"
+	       "       %s [ -f[orce] ] -b[atch] filename\n"
 	       "where  OBJECT := { dev | link | resource | help }\n"
-	       "       OPTIONS := { -V[ersion] | -d[etails] | -j[son] | -p[retty]}\n", name);
+	       "       OPTIONS := { -V[ersion] | -d[etails] | -j[son] | -p[retty]}\n", name, name);
 }

 static int cmd_help(struct rd *rd)
@@ -25,7 +26,7 @@ static int cmd_help(struct rd *rd)
 	return 0;
 }

-static int rd_cmd(struct rd *rd)
+static int rd_cmd(struct rd *rd, int argc, char **argv)
 {
 	const struct rd_cmd cmds[] = {
 		{ NULL,		cmd_help },
@@ -36,17 +37,54 @@ static int rd_cmd(struct rd *rd)
 		{ 0 }
 	};

+	rd->argc = argc;
+	rd->argv = argv;
+
 	return rd_exec_cmd(rd, cmds, "object");
 }

-static int rd_init(struct rd *rd, int argc, char **argv, char *filename)
+static int rd_batch(struct rd *rd, const char *name, bool force)
+{
+	char *line = NULL;
+	size_t len = 0;
+	int ret = 0;
+
+	if (name && strcmp(name, "-") != 0) {
+		if (!freopen(name, "r", stdin)) {
+			pr_err("Cannot open file \"%s\" for reading: %s\n",
+			       name, strerror(errno));
+			return errno;
+		}
+	}
+
+	cmdlineno = 0;
+	while (getcmdline(&line, &len, stdin) != -1) {
+		char *largv[512];
+		int largc;
+
+		largc = makeargs(line, largv, 100);
+		if (!largc)
+			continue;	/* blank line */
+
+		ret = rd_cmd(rd, largc, largv);
+		if (ret) {
+			pr_err("Command failed %s:%d\n", name, cmdlineno);
+			if (!force)
+				break;
+		}
+	}
+
+	free(line);
+
+	return ret;
+}
+
+static int rd_init(struct rd *rd, char *filename)
 {
 	uint32_t seq;
 	int ret;

 	rd->filename = filename;
-	rd->argc = argc;
-	rd->argv = argv;
 	INIT_LIST_HEAD(&rd->dev_map_list);
 	INIT_LIST_HEAD(&rd->filter_list);

@@ -87,11 +125,15 @@ int main(int argc, char **argv)
 		{ "json",		no_argument,		NULL, 'j' },
 		{ "pretty",		no_argument,		NULL, 'p' },
 		{ "details",		no_argument,		NULL, 'd' },
+		{ "force",		no_argument,		NULL, 'f' },
+		{ "batch",		required_argument,	NULL, 'b' },
 		{ NULL, 0, NULL, 0 }
 	};
+	const char *batch_file = NULL;
 	bool pretty_output = false;
 	bool show_details = false;
 	bool json_output = false;
+	bool force = false;
 	char *filename;
 	struct rd rd;
 	int opt;
@@ -99,7 +141,7 @@ int main(int argc, char **argv)

 	filename = basename(argv[0]);

-	while ((opt = getopt_long(argc, argv, "Vhdpj",
+	while ((opt = getopt_long(argc, argv, ":Vhdpjfb:",
 				  long_options, NULL)) >= 0) {
 		switch (opt) {
 		case 'V':
@@ -115,9 +157,18 @@ int main(int argc, char **argv)
 		case 'j':
 			json_output = true;
 			break;
+		case 'f':
+			force = true;
+			break;
+		case 'b':
+			batch_file = optarg;
+			break;
 		case 'h':
 			help(filename);
 			return EXIT_SUCCESS;
+		case ':':
+			pr_err("-%c option requires an argument\n", optopt);
+			return EXIT_FAILURE;
 		default:
 			pr_err("Unknown option.\n");
 			help(filename);
@@ -132,11 +183,14 @@ int main(int argc, char **argv)
 	rd.json_output = json_output;
 	rd.pretty_output = pretty_output;

-	err = rd_init(&rd, argc, argv, filename);
+	err = rd_init(&rd, filename);
 	if (err)
 		goto out;

-	err = rd_cmd(&rd);
+	if (batch_file)
+		err = rd_batch(&rd, batch_file, force);
+	else
+		err = rd_cmd(&rd, argc, argv);
 out:
 	/* Always cleanup */
 	rd_cleanup(&rd);
--
2.16.1

             reply	other threads:[~2018-02-21 12:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-21 12:38 Leon Romanovsky [this message]
2018-02-21 16:10 ` [PATCH iproute2-next] rdma: Add batch command support David Ahern
2018-02-21 16:27   ` Leon Romanovsky

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=20180221123825.3042-1-leon@kernel.org \
    --to=leon@kernel.org \
    --cc=dsahern@gmail.com \
    --cc=leonro@mellanox.com \
    --cc=linux-rdma@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.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