All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ivan Vecera <ivecera@redhat.com>
To: netdev@vger.kernel.org
Cc: jiri@mellanox.com, arkadis@mellanox.com
Subject: [PATCH iproute2 2/2] devlink: add batch command support
Date: Fri, 10 Nov 2017 07:20:14 +0100	[thread overview]
Message-ID: <20171110062014.2693-3-ivecera@redhat.com> (raw)
In-Reply-To: <20171110062014.2693-1-ivecera@redhat.com>

The patch adds support to batch devlink commands.

Cc: Jiri Pirko <jiri@mellanox.com>
Cc: Arkadi Sharshevsky <arkadis@mellanox.com>
Signed-off-by: Ivan Vecera <ivecera@redhat.com>
---
 devlink/devlink.c  | 70 +++++++++++++++++++++++++++++++++++++++++++++++-------
 man/man8/devlink.8 | 16 +++++++++++++
 2 files changed, 78 insertions(+), 8 deletions(-)

diff --git a/devlink/devlink.c b/devlink/devlink.c
index 39cda067..1b15eef8 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -3803,12 +3803,16 @@ static int cmd_dpipe(struct dl *dl)
 static void help(void)
 {
 	pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
+	       "       devlink [ -f[orce] ] -b[atch] filename\n"
 	       "where  OBJECT := { dev | port | sb | monitor | dpipe }\n"
 	       "       OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] | -v[verbose] }\n");
 }
 
-static int dl_cmd(struct dl *dl)
+static int dl_cmd(struct dl *dl, int argc, char **argv)
 {
+	dl->argc = argc;
+	dl->argv = argv;
+
 	if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
 		help();
 		return 0;
@@ -3832,13 +3836,10 @@ static int dl_cmd(struct dl *dl)
 	return -ENOENT;
 }
 
-static int dl_init(struct dl *dl, int argc, char **argv)
+static int dl_init(struct dl *dl)
 {
 	int err;
 
-	dl->argc = argc;
-	dl->argv = argv;
-
 	dl->nlg = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION);
 	if (!dl->nlg) {
 		pr_err("Failed to connect to devlink Netlink\n");
@@ -3890,16 +3891,59 @@ static void dl_free(struct dl *dl)
 	free(dl);
 }
 
+static int dl_batch(struct dl *dl, const char *name, bool force)
+{
+	char *line = NULL;
+	size_t len = 0;
+	int ret = EXIT_SUCCESS;
+
+	if (name && strcmp(name, "-") != 0) {
+		if (freopen(name, "r", stdin) == NULL) {
+			fprintf(stderr,
+				"Cannot open file \"%s\" for reading: %s\n",
+				name, strerror(errno));
+			return EXIT_FAILURE;
+		}
+	}
+
+	cmdlineno = 0;
+	while (getcmdline(&line, &len, stdin) != -1) {
+		char *largv[100];
+		int largc;
+
+		largc = makeargs(line, largv, 100);
+		if (!largc)
+			continue;	/* blank line */
+
+		if (dl_cmd(dl, largc, largv)) {
+			fprintf(stderr, "Command failed %s:%d\n",
+				name, cmdlineno);
+			ret = EXIT_FAILURE;
+			if (!force)
+				break;
+		}
+	}
+
+	if (line)
+		free(line);
+
+	return ret;
+}
+
 int main(int argc, char **argv)
 {
 	static const struct option long_options[] = {
 		{ "Version",		no_argument,		NULL, 'V' },
+		{ "force",		no_argument,		NULL, 'f' },
+		{ "batch",		required_argument,	NULL, 'b' },
 		{ "no-nice-names",	no_argument,		NULL, 'n' },
 		{ "json",		no_argument,		NULL, 'j' },
 		{ "pretty",		no_argument,		NULL, 'p' },
 		{ "verbose",		no_argument,		NULL, 'v' },
 		{ NULL, 0, NULL, 0 }
 	};
+	const char *batch_file = NULL;
+	bool force = false;
 	struct dl *dl;
 	int opt;
 	int err;
@@ -3911,7 +3955,7 @@ int main(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	while ((opt = getopt_long(argc, argv, "Vnjpv",
+	while ((opt = getopt_long(argc, argv, "Vfb:njpv",
 				  long_options, NULL)) >= 0) {
 
 		switch (opt) {
@@ -3919,6 +3963,12 @@ int main(int argc, char **argv)
 			printf("devlink utility, iproute2-ss%s\n", SNAPSHOT);
 			ret = EXIT_SUCCESS;
 			goto dl_free;
+		case 'f':
+			force = true;
+			break;
+		case 'b':
+			batch_file = optarg;
+			break;
 		case 'n':
 			dl->no_nice_names = true;
 			break;
@@ -3942,13 +3992,17 @@ int main(int argc, char **argv)
 	argc -= optind;
 	argv += optind;
 
-	err = dl_init(dl, argc, argv);
+	err = dl_init(dl);
 	if (err) {
 		ret = EXIT_FAILURE;
 		goto dl_free;
 	}
 
-	err = dl_cmd(dl);
+	if (batch_file)
+		err = dl_batch(dl, batch_file, force);
+	else
+		err = dl_cmd(dl, argc, argv);
+
 	if (err) {
 		ret = EXIT_FAILURE;
 		goto dl_fini;
diff --git a/man/man8/devlink.8 b/man/man8/devlink.8
index a480766c..a975ef34 100644
--- a/man/man8/devlink.8
+++ b/man/man8/devlink.8
@@ -12,6 +12,12 @@ devlink \- Devlink tool
 .sp
 
 .ti -8
+.B devlink
+.RB "[ " -force " ] "
+.BI "-batch " filename
+.sp
+
+.ti -8
 .IR OBJECT " := { "
 .BR dev " | " port " | " monitor " }"
 .sp
@@ -32,6 +38,16 @@ Print the version of the
 utility and exit.
 
 .TP
+.BR "\-b", " \-batch " <FILENAME>
+Read commands from provided file or standard input and invoke them.
+First failure will cause termination of devlink.
+
+.TP
+.BR "\-force"
+Don't terminate devlink 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 "\-n" , " --no-nice-names"
 Turn off printing out nice names, for example netdevice ifnames instead of devlink port identification.
 
-- 
2.13.6

  parent reply	other threads:[~2017-11-10  6:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-10  6:20 [PATCH iproute2 0/2] add batch command support to devlink Ivan Vecera
2017-11-10  6:20 ` [PATCH iproute2 1/2] lib: make resolve_hosts variable common Ivan Vecera
2017-11-10  6:20 ` Ivan Vecera [this message]
2017-11-10  6:57   ` [PATCH iproute2 2/2] devlink: add batch command support Leon Romanovsky
2017-11-10  7:10     ` Ivan Vecera
2017-11-10 19:47       ` Leon Romanovsky
2017-11-12  5:06         ` Jiri Pirko
2017-11-12  8:19           ` Leon Romanovsky
2017-11-12  8:21             ` Jiri Pirko
2017-11-13  0:16         ` Stephen Hemminger
2017-11-13  8:59           ` Leon Romanovsky
2017-11-12  5:08   ` Jiri Pirko
2017-11-13  0:17 ` [PATCH iproute2 0/2] add batch command support to devlink Stephen Hemminger

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=20171110062014.2693-3-ivecera@redhat.com \
    --to=ivecera@redhat.com \
    --cc=arkadis@mellanox.com \
    --cc=jiri@mellanox.com \
    --cc=netdev@vger.kernel.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 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.