All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/4] tools: bpftool: improve batch mode
@ 2018-03-02  4:20 Jakub Kicinski
  2018-03-02  4:20 ` [PATCH bpf-next 1/4] tools: bpftool: support comments in batch files Jakub Kicinski
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Jakub Kicinski @ 2018-03-02  4:20 UTC (permalink / raw)
  To: alexei.starovoitov, daniel; +Cc: netdev, oss-drivers, Jakub Kicinski

Quentin says:

Several enhancements for bpftool batch mode are introduced in this series.

More specifically, input files for batch mode gain support for:
  * comments (starting with '#'),
  * continuation lines (after a line ending with '\'),
  * arguments enclosed between quotes.

Also, make bpftool able to read from standard input when "-" is provided as
input file name.


Quentin Monnet (4):
  tools: bpftool: support comments in batch files
  tools: bpftool: support continuation lines in batch files
  tools: bpftool: read from stdin when batch file name is "-"
  tools: bpftool: add support for quotations in batch files

 tools/bpf/bpftool/main.c | 104 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 89 insertions(+), 15 deletions(-)

-- 
2.15.1

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

* [PATCH bpf-next 1/4] tools: bpftool: support comments in batch files
  2018-03-02  4:20 [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Jakub Kicinski
@ 2018-03-02  4:20 ` Jakub Kicinski
  2018-03-02  4:20 ` [PATCH bpf-next 2/4] tools: bpftool: support continuation lines " Jakub Kicinski
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2018-03-02  4:20 UTC (permalink / raw)
  To: alexei.starovoitov, daniel; +Cc: netdev, oss-drivers, Quentin Monnet

From: Quentin Monnet <quentin.monnet@netronome.com>

Replace '#' by '\0' in commands read from batch files in order to avoid
processing the remaining part of the line, thus allowing users to use
comments in the files.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
---
 tools/bpf/bpftool/main.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 185acfa229b5..79587e6decae 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -176,6 +176,7 @@ static int do_batch(int argc, char **argv)
 	char buf[65536];
 	int n_argc;
 	FILE *fp;
+	char *cp;
 	int err;
 	int i;
 
@@ -200,6 +201,10 @@ static int do_batch(int argc, char **argv)
 	if (json_output)
 		jsonw_start_array(json_wtr);
 	while (fgets(buf, sizeof(buf), fp)) {
+		cp = strchr(buf, '#');
+		if (cp)
+			*cp = '\0';
+
 		if (strlen(buf) == sizeof(buf) - 1) {
 			errno = E2BIG;
 			break;
-- 
2.15.1

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

* [PATCH bpf-next 2/4] tools: bpftool: support continuation lines in batch files
  2018-03-02  4:20 [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Jakub Kicinski
  2018-03-02  4:20 ` [PATCH bpf-next 1/4] tools: bpftool: support comments in batch files Jakub Kicinski
@ 2018-03-02  4:20 ` Jakub Kicinski
  2018-03-02  4:20 ` [PATCH bpf-next 3/4] tools: bpftool: read from stdin when batch file name is "-" Jakub Kicinski
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2018-03-02  4:20 UTC (permalink / raw)
  To: alexei.starovoitov, daniel; +Cc: netdev, oss-drivers, Quentin Monnet

From: Quentin Monnet <quentin.monnet@netronome.com>

Add support for continuation lines, such as in the following example:

    prog show
    prog dump xlated \
        id 1337 opcodes

This patch is based after the code for support for continuation lines
from file lib/utils.c from package iproute2.

"Lines" in error messages are renamed as "commands", as we count the
number of commands (but we ignore empty lines, comments, and do not add
continuation lines to the count).

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
---
 tools/bpf/bpftool/main.c | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 79587e6decae..cdee4c3d30c3 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -46,6 +46,9 @@
 
 #include "main.h"
 
+#define BATCH_LINE_LEN_MAX 65536
+#define BATCH_ARG_NB_MAX 4096
+
 const char *bin_name;
 static int last_argc;
 static char **last_argv;
@@ -171,9 +174,9 @@ static const struct cmd cmds[] = {
 
 static int do_batch(int argc, char **argv)
 {
+	char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
+	char *n_argv[BATCH_ARG_NB_MAX];
 	unsigned int lines = 0;
-	char *n_argv[4096];
-	char buf[65536];
 	int n_argc;
 	FILE *fp;
 	char *cp;
@@ -210,13 +213,38 @@ static int do_batch(int argc, char **argv)
 			break;
 		}
 
+		/* Append continuation lines if any (coming after a line ending
+		 * with '\' in the batch file).
+		 */
+		while ((cp = strstr(buf, "\\\n")) != NULL) {
+			if (!fgets(contline, sizeof(contline), fp) ||
+			    strlen(contline) == 0) {
+				p_err("missing continuation line on command %d",
+				      lines);
+				err = -1;
+				goto err_close;
+			}
+
+			cp = strchr(contline, '#');
+			if (cp)
+				*cp = '\0';
+
+			if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
+				p_err("command %d is too long", lines);
+				err = -1;
+				goto err_close;
+			}
+			buf[strlen(buf) - 2] = '\0';
+			strcat(buf, contline);
+		}
+
 		n_argc = 0;
 		n_argv[n_argc] = strtok(buf, " \t\n");
 
 		while (n_argv[n_argc]) {
 			n_argc++;
 			if (n_argc == ARRAY_SIZE(n_argv)) {
-				p_err("line %d has too many arguments, skip",
+				p_err("command %d has too many arguments, skip",
 				      lines);
 				n_argc = 0;
 				break;
@@ -252,7 +280,7 @@ static int do_batch(int argc, char **argv)
 		p_err("reading batch file failed: %s", strerror(errno));
 		err = -1;
 	} else {
-		p_info("processed %d lines", lines);
+		p_info("processed %d commands", lines);
 		err = 0;
 	}
 err_close:
-- 
2.15.1

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

* [PATCH bpf-next 3/4] tools: bpftool: read from stdin when batch file name is "-"
  2018-03-02  4:20 [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Jakub Kicinski
  2018-03-02  4:20 ` [PATCH bpf-next 1/4] tools: bpftool: support comments in batch files Jakub Kicinski
  2018-03-02  4:20 ` [PATCH bpf-next 2/4] tools: bpftool: support continuation lines " Jakub Kicinski
@ 2018-03-02  4:20 ` Jakub Kicinski
  2018-03-02  4:20 ` [PATCH bpf-next 4/4] tools: bpftool: add support for quotations in batch files Jakub Kicinski
  2018-03-02  8:51 ` [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Daniel Borkmann
  4 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2018-03-02  4:20 UTC (permalink / raw)
  To: alexei.starovoitov, daniel; +Cc: netdev, oss-drivers, Quentin Monnet

From: Quentin Monnet <quentin.monnet@netronome.com>

Make bpftool read its command list from standard input when the name if
the input file is a single dash.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
---
 tools/bpf/bpftool/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index cdee4c3d30c3..1da54a9b5ea3 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -195,7 +195,10 @@ static int do_batch(int argc, char **argv)
 	}
 	NEXT_ARG();
 
-	fp = fopen(*argv, "r");
+	if (!strcmp(*argv, "-"))
+		fp = stdin;
+	else
+		fp = fopen(*argv, "r");
 	if (!fp) {
 		p_err("Can't open file (%s): %s", *argv, strerror(errno));
 		return -1;
@@ -284,7 +287,8 @@ static int do_batch(int argc, char **argv)
 		err = 0;
 	}
 err_close:
-	fclose(fp);
+	if (fp != stdin)
+		fclose(fp);
 
 	if (json_output)
 		jsonw_end_array(json_wtr);
-- 
2.15.1

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

* [PATCH bpf-next 4/4] tools: bpftool: add support for quotations in batch files
  2018-03-02  4:20 [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Jakub Kicinski
                   ` (2 preceding siblings ...)
  2018-03-02  4:20 ` [PATCH bpf-next 3/4] tools: bpftool: read from stdin when batch file name is "-" Jakub Kicinski
@ 2018-03-02  4:20 ` Jakub Kicinski
  2018-03-02  8:51 ` [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Daniel Borkmann
  4 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2018-03-02  4:20 UTC (permalink / raw)
  To: alexei.starovoitov, daniel; +Cc: netdev, oss-drivers, Quentin Monnet

From: Quentin Monnet <quentin.monnet@netronome.com>

Improve argument parsing from batch input files in order to support
arguments enclosed between single (') or double quotes ("). For example,
this command can now be parsed in batch mode:

    bpftool prog dump xlated id 1337 file "/tmp/my file with spaces"

The function responsible for parsing command arguments is copied from
its counterpart in lib/utils.c in iproute2 package.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
---
 tools/bpf/bpftool/main.c | 65 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 51 insertions(+), 14 deletions(-)

diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index 1da54a9b5ea3..1ec852d21d44 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -160,6 +160,54 @@ void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
 	}
 }
 
+/* Split command line into argument vector. */
+static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
+{
+	static const char ws[] = " \t\r\n";
+	char *cp = line;
+	int n_argc = 0;
+
+	while (*cp) {
+		/* Skip leading whitespace. */
+		cp += strspn(cp, ws);
+
+		if (*cp == '\0')
+			break;
+
+		if (n_argc >= (maxargs - 1)) {
+			p_err("too many arguments to command %d", cmd_nb);
+			return -1;
+		}
+
+		/* Word begins with quote. */
+		if (*cp == '\'' || *cp == '"') {
+			char quote = *cp++;
+
+			n_argv[n_argc++] = cp;
+			/* Find ending quote. */
+			cp = strchr(cp, quote);
+			if (!cp) {
+				p_err("unterminated quoted string in command %d",
+				      cmd_nb);
+				return -1;
+			}
+		} else {
+			n_argv[n_argc++] = cp;
+
+			/* Find end of word. */
+			cp += strcspn(cp, ws);
+			if (*cp == '\0')
+				break;
+		}
+
+		/* Separate words. */
+		*cp++ = 0;
+	}
+	n_argv[n_argc] = NULL;
+
+	return n_argc;
+}
+
 static int do_batch(int argc, char **argv);
 
 static const struct cmd cmds[] = {
@@ -241,22 +289,11 @@ static int do_batch(int argc, char **argv)
 			strcat(buf, contline);
 		}
 
-		n_argc = 0;
-		n_argv[n_argc] = strtok(buf, " \t\n");
-
-		while (n_argv[n_argc]) {
-			n_argc++;
-			if (n_argc == ARRAY_SIZE(n_argv)) {
-				p_err("command %d has too many arguments, skip",
-				      lines);
-				n_argc = 0;
-				break;
-			}
-			n_argv[n_argc] = strtok(NULL, " \t\n");
-		}
-
+		n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
 		if (!n_argc)
 			continue;
+		if (n_argc < 0)
+			goto err_close;
 
 		if (json_output) {
 			jsonw_start_object(json_wtr);
-- 
2.15.1

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

* Re: [PATCH bpf-next 0/4] tools: bpftool: improve batch mode
  2018-03-02  4:20 [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Jakub Kicinski
                   ` (3 preceding siblings ...)
  2018-03-02  4:20 ` [PATCH bpf-next 4/4] tools: bpftool: add support for quotations in batch files Jakub Kicinski
@ 2018-03-02  8:51 ` Daniel Borkmann
  4 siblings, 0 replies; 6+ messages in thread
From: Daniel Borkmann @ 2018-03-02  8:51 UTC (permalink / raw)
  To: Jakub Kicinski, alexei.starovoitov; +Cc: netdev, oss-drivers

On 03/02/2018 05:20 AM, Jakub Kicinski wrote:
> Quentin says:
> 
> Several enhancements for bpftool batch mode are introduced in this series.
> 
> More specifically, input files for batch mode gain support for:
>   * comments (starting with '#'),
>   * continuation lines (after a line ending with '\'),
>   * arguments enclosed between quotes.
> 
> Also, make bpftool able to read from standard input when "-" is provided as
> input file name.

Applied to bpf-next, thanks Quentin and Jakub!

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

end of thread, other threads:[~2018-03-02  8:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-02  4:20 [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Jakub Kicinski
2018-03-02  4:20 ` [PATCH bpf-next 1/4] tools: bpftool: support comments in batch files Jakub Kicinski
2018-03-02  4:20 ` [PATCH bpf-next 2/4] tools: bpftool: support continuation lines " Jakub Kicinski
2018-03-02  4:20 ` [PATCH bpf-next 3/4] tools: bpftool: read from stdin when batch file name is "-" Jakub Kicinski
2018-03-02  4:20 ` [PATCH bpf-next 4/4] tools: bpftool: add support for quotations in batch files Jakub Kicinski
2018-03-02  8:51 ` [PATCH bpf-next 0/4] tools: bpftool: improve batch mode Daniel Borkmann

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.