Skip to contentSkip to conversation

libnftables: Implement JSON output support

RFC PATCH In review Phil Sutter 2 participants 8 messages Jan 17, 2018 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

RFC PATCH 1/1 libnftables: Implement JSON output support

Although technically there already is support for JSON output via 'nft export json' command, it is hardly useable since it exports all the gory details of nftables VM. Also, libnftables has no control over what is exported since the content comes directly from libnftnl.

Instead, implement JSON format support for regular 'nft list' commands.

---
Note that this is incomplete and merely meant as foundation for a discussion about the implementation. A few things I am not happy with:

* The amount of ifdef's introduced is certainly not optimal, though I don't see how this could be avoided if JSON support is to be kept optional.

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

* JSON output is completely numeric. While this is intentional as it helps applications parsing e.g. port numbers, other things like e.g. TCP header flags become a bit cryptic.

---
 configure.ac                |  14 ++-
 include/datatype.h          |  14 +++
 include/expression.h        |   9 ++
 include/nftables.h          |   3 +
 include/nftables/nftables.h |   2 +
 include/statement.h         |   7 ++
 src/Makefile.am             |   4 +
 src/datatype.c              |  72 +++++++++++++
 src/expression.c            |  64 ++++++++++++
 src/libnftables.c           |  16 +++
 src/main.c                  |  11 +-
 src/meta.c                  |  16 +++
 src/payload.c               |  23 +++++
 src/rule.c                  | 242 +++++++++++++++++++++++++++++++++++++++++++-
 src/statement.c             |  10 ++
 15 files changed, 504 insertions(+), 3 deletions(-)
configure.ac +13 -1
index 6ed3edca1fe5c..ad1c068656d88 100644
@@ -111,6 +111,17 @@ AC_DEFINE([HAVE_LIBXTABLES], [1], [0])
 AC_SUBST(with_libxtables)
 AM_CONDITIONAL([BUILD_XTABLES], [test "x$with_libxtables" == xyes])
 
+AC_ARG_WITH([json], [AS_HELP_STRING([--with-json],
+            [Enable JSON output support)])],
+	    [], [with_json=no])
+AS_IF([test "x$with_json" != xno], [
+AC_CHECK_LIB([jansson], [json_object], ,
+	AC_MSG_ERROR([No suitable version of libjansson found]))
+AC_DEFINE([HAVE_LIBJANSSON], [1], [Define if you have libjansson])
+])
+AC_SUBST(with_json)
+AM_CONDITIONAL([BUILD_JSON], [test "x$with_json" != xno])
+
 # Checks for header files.
 AC_HEADER_STDC
 AC_HEADER_ASSERT
@@ -163,4 +174,5 @@ nft configuration:
   enable debugging symbols:	${with_debug}
   use mini-gmp:			${with_mini_gmp}
   enable pdf documentation:	${enable_pdf_doc}
-  libxtables support:		${with_libxtables}"
+  libxtables support:		${with_libxtables}
+  json output support:          ${with_json}"
include/datatype.h +14 -0
index e9f60798d1a0c..05a64ffd447c9 100644
@@ -1,6 +1,10 @@
 #ifndef NFTABLES_DATATYPE_H
 #define NFTABLES_DATATYPE_H
 
+#ifdef HAVE_LIBJANSSON
+#include <jansson.h>
+#endif
+
 /**
  * enum datatypes
  *
@@ -147,6 +151,9 @@ struct datatype {
 	const char			*basefmt;
 	void				(*print)(const struct expr *expr,
 						 struct output_ctx *octx);
+#ifdef HAVE_LIBJANSSON
+	json_t				*(*json)(const struct expr *expr);
+#endif
 	struct error_record		*(*parse)(const struct expr *sym,
 						  struct expr **res);
 	const struct symbol_table	*sym_tbl;
@@ -158,6 +165,9 @@ extern const struct datatype *datatype_lookup_byname(const char *name);
 extern struct error_record *symbol_parse(const struct expr *sym,
 					 struct expr **res);
 extern void datatype_print(const struct expr *expr, struct output_ctx *octx);
+#ifdef HAVE_LIBJANSSON
+extern json_t *datatype_json(const struct expr *expr);
+#endif
 
 static inline bool datatype_equal(const struct datatype *d1,
 				  const struct datatype *d2)
@@ -207,6 +217,10 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,
 extern void symbolic_constant_print(const struct symbol_table *tbl,
 				    const struct expr *expr, bool quotes,
 				    struct output_ctx *octx);
+#ifdef HAVE_LIBJANSSON
+extern json_t *symbolic_constant_json(const struct symbol_table *tbl,
+				      const struct expr *expr);
+#endif
 extern void symbol_table_print(const struct symbol_table *tbl,
 			       const struct datatype *dtype,
 			       enum byteorder byteorder,
include/expression.h +9 -0
index 0a0e178fe4680..4b57c3d1b3697 100644
@@ -9,6 +9,9 @@
 #include <datatype.h>
 #include <utils.h>
 #include <list.h>
+#ifdef HAVE_LIBJANSSON
+#include <jansson.h>
+#endif
 
 /**
  * enum expr_types
@@ -159,6 +162,9 @@ struct expr_ops {
 					    enum byteorder byteorder);
 	void			(*print)(const struct expr *expr,
 					 struct output_ctx *octx);
+#ifdef HAVE_LIBJANSSON
+	json_t *		(*json)(const struct expr *expr);
+#endif
 	bool			(*cmp)(const struct expr *e1,
 				       const struct expr *e2);
 	void			(*pctx_update)(struct proto_ctx *ctx,
@@ -335,6 +341,9 @@ extern struct expr *expr_clone(const struct expr *expr);
 extern struct expr *expr_get(struct expr *expr);
 extern void expr_free(struct expr *expr);
 extern void expr_print(const struct expr *expr, struct output_ctx *octx);
+#ifdef HAVE_LIBJANSSON
+extern json_t *expr_print_json(const struct expr *expr);
+#endif
 extern bool expr_cmp(const struct expr *e1, const struct expr *e2);
 extern void expr_describe(const struct expr *expr, struct output_ctx *octx);
 
include/nftables.h +3 -0
index f22df0d1ddc15..f8fb7b38f37ac 100644
@@ -12,6 +12,9 @@ struct output_ctx {
 	unsigned int ip2name;
 	unsigned int handle;
 	unsigned int echo;
+#ifdef HAVE_LIBJANSSON
+	unsigned int json;
+#endif
 	FILE *output_fp;
 };
 
include/nftables/nftables.h +2 -0
index 8e59f2b2a59ab..5f53b932802b3 100644
@@ -55,6 +55,8 @@ bool nft_ctx_output_get_handle(struct nft_ctx *ctx);
 void nft_ctx_output_set_handle(struct nft_ctx *ctx, bool val);
 bool nft_ctx_output_get_echo(struct nft_ctx *ctx);
 void nft_ctx_output_set_echo(struct nft_ctx *ctx, bool val);
+bool nft_ctx_output_get_json(struct nft_ctx *ctx);
+void nft_ctx_output_set_json(struct nft_ctx *ctx, bool val);
 
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
 int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path);
include/statement.h +7 -0
index 23a551b67f2b9..17d7b65151c20 100644
@@ -256,6 +256,10 @@ enum stmt_types {
 	STMT_EXTHDR,
 };
 
+#ifdef HAVE_LIBJANSSON
+#include <jansson.h>
+#endif
+
 /**
  * struct stmt_ops
  *
@@ -271,6 +275,9 @@ struct stmt_ops {
 	void			(*destroy)(struct stmt *stmt);
 	void			(*print)(const struct stmt *stmt,
 					 struct output_ctx *octx);
+#ifdef HAVE_LIBJANSSON
+	json_t *		(*json)(const struct stmt *stmt);
+#endif
 };
 
 enum stmt_flags {
src/Makefile.am +4 -0
index 7fa72a8ea5bc2..42bb49ca04941 100644
@@ -87,4 +87,8 @@ if BUILD_CLI
 nft_SOURCES += cli.c
 endif
 
+if BUILD_JSON
+libnftables_la_LIBADD += ${JANSSON_LIBS}
+endif
+
 nft_LDADD = libnftables.la
src/datatype.c +72 -0

111 lines not on this page · raw message

src/expression.c +64 -0

112 lines not on this page · raw message

src/libnftables.c +16 -0
index 8a18bb78f0d92..368862e153376 100644
@@ -270,6 +270,22 @@ void nft_ctx_output_set_echo(struct nft_ctx *ctx, bool val)
 	ctx->output.echo = val;
 }
 
+bool nft_ctx_output_get_json(struct nft_ctx *ctx)
+{
+#ifdef HAVE_LIBJANSSON
+	return ctx->output.json;
+#else
+	return false;
+#endif
+}
+
+void nft_ctx_output_set_json(struct nft_ctx *ctx, bool val)
+{
+#ifdef HAVE_LIBJANSSON
+	ctx->output.json = val;
+#endif
+}
+
 static const struct input_descriptor indesc_cmdline = {
 	.type	= INDESC_BUFFER,
 	.name	= "<cmdline>",
src/main.c +10 -1
index 353b87bc66631..348f1c1e4044e 100644
@@ -31,6 +31,7 @@ enum opt_vals {
 	OPT_FILE		= 'f',
 	OPT_INTERACTIVE		= 'i',
 	OPT_INCLUDEPATH		= 'I',
+	OPT_JSON		= 'j',
 	OPT_NUMERIC		= 'n',
 	OPT_STATELESS		= 's',
 	OPT_IP2NAME		= 'N',
@@ -40,7 +41,7 @@ enum opt_vals {
 	OPT_INVALID		= '?',
 };
 
-#define OPTSTRING	"hvcf:iI:vnsNae"
+#define OPTSTRING	"hvcf:iI:jvnsNae"
 
 static const struct option options[] = {
 	{
@@ -94,6 +95,10 @@ static const struct option options[] = {
 		.name		= "echo",
 		.val		= OPT_ECHO,
 	},
+	{
+		.name		= "json",
+		.val		= OPT_JSON,
+	},
 	{
 		.name		= NULL
 	}
@@ -112,6 +117,7 @@ static void show_help(const char *name)
 "  -f, --file <filename>		Read input from <filename>\n"
 "  -i, --interactive		Read input from interactive CLI\n"
 "\n"
+"  -j, --json			Format output in JSON\n"
 "  -n, --numeric			When specified once, show network addresses numerically (default behaviour).\n"
 "  				Specify twice to also show Internet services (port numbers) numerically.\n"
 "				Specify three times to also show protocols, user IDs, and group IDs numerically.\n"
@@ -255,6 +261,9 @@ int main(int argc, char * const *argv)
 		case OPT_ECHO:
 			nft_ctx_output_set_echo(nft, true);
 			break;
+		case OPT_JSON:
+			nft_ctx_output_set_json(nft, true);
+			break;
 		case OPT_INVALID:
 			exit(EXIT_FAILURE);
 		}
src/meta.c +16 -0
index 687de8cda8c35..ab600a05149c7 100644
@@ -445,6 +445,19 @@ static bool meta_key_is_qualified(enum nft_meta_keys key)
 	}
 }
 
+#ifdef HAVE_LIBJANSSON
+static json_t *meta_expr_json(const struct expr *expr)
+{
+	json_t *root = json_object();
+
+	json_object_set_new(root, "type", json_pack("s", "meta"));
+	json_object_set_new(root, "name",
+			json_pack("s", meta_templates[expr->meta.key].token));
+
+	return root;
+}
+#endif
+
 static void meta_expr_print(const struct expr *expr, struct output_ctx *octx)
 {
 	if (meta_key_is_qualified(expr->meta.key))
@@ -535,6 +548,9 @@ static const struct expr_ops meta_expr_ops = {
 	.type		= EXPR_META,
 	.name		= "meta",
 	.print		= meta_expr_print,
+#ifdef HAVE_LIBJANSSON
+	.json		= meta_expr_json,
+#endif
 	.cmp		= meta_expr_cmp,
 	.clone		= meta_expr_clone,
 	.pctx_update	= meta_expr_pctx_update,
src/payload.c +23 -0
index 60090accbcd8b..2dbc7a95c4cc7 100644
@@ -38,6 +38,26 @@ bool payload_is_known(const struct expr *expr)
 	       tmpl != &proto_unknown_template;
 }
 
+#ifdef HAVE_LIBJANSSON
+static json_t *payload_expr_json(const struct expr *expr)
+{
+	const struct proto_hdr_template *tmpl;
+	const struct proto_desc *desc;
+
+	desc = expr->payload.desc;
+	tmpl = expr->payload.tmpl;
+	if (payload_is_known(expr))
+		return json_pack("{ssssss}", "type", "payload",
+				"name", desc->name,
+				"field", tmpl->token);
+	else
+		return json_pack("{sssssisi}", "type", "payload",
+				"base", proto_base_tokens[expr->payload.base],
+				"offset", expr->payload.offset,
+				"len", expr->len);
+}
+#endif
+
 static void payload_expr_print(const struct expr *expr, struct output_ctx *octx)
 {
 	const struct proto_desc *desc;
@@ -107,6 +127,9 @@ static const struct expr_ops payload_expr_ops = {
 	.type		= EXPR_PAYLOAD,
 	.name		= "payload",
 	.print		= payload_expr_print,
+#ifdef HAVE_LIBJANSSON
+	.json		= payload_expr_json,
+#endif
 	.cmp		= payload_expr_cmp,
 	.clone		= payload_expr_clone,
 	.pctx_update	= payload_expr_pctx_update,
src/rule.c +241 -1

302 lines not on this page · raw message

src/statement.c +10 -0
index 1f93260bd3d5f..fc606ccc45664 100644
@@ -70,6 +70,13 @@ static void expr_stmt_print(const struct stmt *stmt, struct output_ctx *octx)
 	expr_print(stmt->expr, octx);
 }
 
+#ifdef HAVE_LIBJANSSON
+static json_t *expr_stmt_json(const struct stmt *stmt)
+{
+	return expr_print_json(stmt->expr);
+}
+#endif
+
 static void expr_stmt_destroy(struct stmt *stmt)
 {
 	expr_free(stmt->expr);
@@ -79,6 +86,9 @@ static const struct stmt_ops expr_stmt_ops = {
 	.type		= STMT_EXPRESSION,
 	.name		= "expression",
 	.print		= expr_stmt_print,
+#ifdef HAVE_LIBJANSSON
+	.json		= expr_stmt_json,
+#endif
 	.destroy	= expr_stmt_destroy,
 };
 
Signature · 1 line
2.15.1
#1 lore raw

Hi Phil,

Quoted 4 lines · Phil Sutter

Although technically there already is support for JSON output via 'nft export json' command, it is hardly useable since it exports all the gory details of nftables VM. Also, libnftables has no control over what is exported since the content comes directly from libnftnl.

I'm going to apply this:

        http://patchwork.ozlabs.org/patch/844762/

now that nft 0.8.1 is out. Basically, renaming 'nft export json' to 'nft export vm json' so this clearly shows this is the low-level representation, and we leave room for your high level json representation.

Quoted 10 lines

Instead, implement JSON format support for regular 'nft list' commands.

---
Note that this is incomplete and merely meant as foundation for a discussion about the implementation. A few things I am not happy with:

* The amount of ifdef's introduced is certainly not optimal, though I don't see how this could be avoided if JSON support is to be kept optional.

Usual trick is to add a header file with function declaration like this:

#ifdef NFT_JSON
int nft_json_parse_blah(...);
#else
static inline int nft_json_parse_blah(...) { return -1; } #endif

So all ifdef pollution remains only in that json header file. And place all json code in a single .c file.

Quoted 3 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

Quoted 3 lines

* JSON output is completely numeric. While this is intentional as it helps applications parsing e.g. port numbers, other things like e.g. TCP header flags become a bit cryptic.

Can't we have list in json too?

#2 lore raw
Quoted 1 line · Pablo Neira Ayuso

On Wed, Jan 17, 2018 at 12:51:40PM +0100, Phil Sutter wrote:

[...]

Quoted 7 lines
Quoted 4 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

I'd rather see you map the abstract syntax tree that is represented through parser_bison.y to your json representation. I think this patch is mapping the tree that we obtain after the evaluation phase, which comes with low level expressions such as bitwise/byteorder.

author reviewer
#3 lore raw

Hi Pablo,

Quoted 6 lines · Pablo Neira Ayuso
Quoted 4 lines · Phil Sutter

Although technically there already is support for JSON output via 'nft export json' command, it is hardly useable since it exports all the gory details of nftables VM. Also, libnftables has no control over what is exported since the content comes directly from libnftnl.

Quoted 8 lines

I'm going to apply this:

        http://patchwork.ozlabs.org/patch/844762/

now that nft 0.8.1 is out. Basically, renaming 'nft export json' to 'nft export vm json' so this clearly shows this is the low-level representation, and we leave room for your high level json representation.

Ah, I had forgotten about that. Good news! :)

Quoted 13 lines
Quoted 10 lines

Instead, implement JSON format support for regular 'nft list' commands.

---
Note that this is incomplete and merely meant as foundation for a discussion about the implementation. A few things I am not happy with:

* The amount of ifdef's introduced is certainly not optimal, though I don't see how this could be avoided if JSON support is to be kept optional.

Usual trick is to add a header file with function declaration like this:

Quoted 8 lines

#ifdef NFT_JSON
int nft_json_parse_blah(...);
#else
static inline int nft_json_parse_blah(...) { return -1; } #endif

So all ifdef pollution remains only in that json header file. And place all json code in a single .c file.

OK, I'll try that approach. What do you think about the introduced callbacks in structs datatype, expr_ops, etc.? Note that I can't branch to json printers from regular print callback easily since that doesn't return anything and (I guess) the full JSON tree needs to be built before being printed (unless I implement everything manually, which is probably not optimal, either).

Quoted 6 lines
Quoted 3 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

With "affected code path" I meant functions being called for any 'nft list' command. It is easily possible to control how low-level JSON output will be.

Quoted 5 lines
Quoted 3 lines

* JSON output is completely numeric. While this is intentional as it helps applications parsing e.g. port numbers, other things like e.g. TCP header flags become a bit cryptic.

Can't we have list in json too?

Sorry, I don't get that?

Quoted 8 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

On Wed, Jan 17, 2018 at 12:51:40PM +0100, Phil Sutter wrote:

[...]

Quoted 5 lines
Quoted 4 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

Quoted 7 lines
Quoted 2 lines

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

I'd rather see you map the abstract syntax tree that is represented through parser_bison.y to your json representation. I think this patch is mapping the tree that we obtain after the evaluation phase, which comes with low level expressions such as bitwise/byteorder.

I don't get your point here, either: Not sure what this has to do with parser_bison.y - my patch handles output only for now, I didn't bother with input yet. Or am I on the completely wrong track now?

Thanks for the quick reply though,

Phil

#4 lore raw
Quoted 11 lines · Phil Sutter

Hi Pablo,

Quoted 8 lines · Pablo Neira Ayuso
Quoted 4 lines · Phil Sutter

Although technically there already is support for JSON output via 'nft export json' command, it is hardly useable since it exports all the gory details of nftables VM. Also, libnftables has no control over what is exported since the content comes directly from libnftnl.

I'm going to apply this:

Quoted 8 lines
Quoted 6 lines
        http://patchwork.ozlabs.org/patch/844762/

now that nft 0.8.1 is out. Basically, renaming 'nft export json' to 'nft export vm json' so this clearly shows this is the low-level representation, and we leave room for your high level json representation.

Ah, I had forgotten about that. Good news! :)

:)

Quoted 22 lines
Quoted 13 lines
Quoted 10 lines

Instead, implement JSON format support for regular 'nft list' commands.

---
Note that this is incomplete and merely meant as foundation for a discussion about the implementation. A few things I am not happy with:

* The amount of ifdef's introduced is certainly not optimal, though I don't see how this could be avoided if JSON support is to be kept optional.

Usual trick is to add a header file with function declaration like this:

Quoted 8 lines

#ifdef NFT_JSON
int nft_json_parse_blah(...);
#else
static inline int nft_json_parse_blah(...) { return -1; } #endif

So all ifdef pollution remains only in that json header file. And place all json code in a single .c file.

Quoted 6 lines

OK, I'll try that approach. What do you think about the introduced callbacks in structs datatype, expr_ops, etc.? Note that I can't branch to json printers from regular print callback easily since that doesn't return anything and (I guess) the full JSON tree needs to be built before being printed (unless I implement everything manually, which is probably not optimal, either).

Fine with me. Just a side note, we have to build the json tree after the postprocess phase, when there is no more bitwise/byteorder and such.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

With "affected code path" I meant functions being called for any 'nft list' command. It is easily possible to control how low-level JSON output will be.

OK.

Quoted 20 lines
Quoted 5 lines
Quoted 3 lines

* JSON output is completely numeric. While this is intentional as it helps applications parsing e.g. port numbers, other things like e.g. TCP header flags become a bit cryptic.

Can't we have list in json too?

Sorry, I don't get that?

Quoted 11 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

On Wed, Jan 17, 2018 at 12:51:40PM +0100, Phil Sutter wrote:

[...]

Quoted 7 lines
Quoted 4 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

Quoted 8 lines
Quoted 4 lines

I'd rather see you map the abstract syntax tree that is represented through parser_bison.y to your json representation. I think this patch is mapping the tree that we obtain after the evaluation phase, which comes with low level expressions such as bitwise/byteorder.

I don't get your point here, either: Not sure what this has to do with parser_bison.y - my patch handles output only for now, I didn't bother with input yet. Or am I on the completely wrong track now?

Sorry, I was refering about the input support.

author reviewer
#5 lore raw

On Wed, Jan 17, 2018 at 07:45:04PM +0100, Pablo Neira Ayuso wrote:
[...]

Quoted 17 lines
Quoted 17 lines · Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso
Quoted 1 line · Phil Sutter

[...]

Quoted 7 lines
Quoted 4 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

I'd rather see you map the abstract syntax tree that is represented through parser_bison.y to your json representation. I think this patch is mapping the tree that we obtain after the evaluation phase, which comes with low level expressions such as bitwise/byteorder.

Quoted 5 lines
Quoted 3 lines

I don't get your point here, either: Not sure what this has to do with parser_bison.y - my patch handles output only for now, I didn't bother with input yet. Or am I on the completely wrong track now?

Sorry, I was refering about the input support.

Ah, I see. For input, I did consider using bison BTW, but doubt it will lead to less code than implementing everything with libjansson. And yes, this will then be on the same level as parser_bison.y, so what is parsed will then be fed to cmd_evaluate().

Cheers, Phil

#6 lore raw
Quoted 23 lines · Phil Sutter

On Wed, Jan 17, 2018 at 07:45:04PM +0100, Pablo Neira Ayuso wrote:
[...]

Quoted 21 lines
Quoted 20 lines · Pablo Neira Ayuso
Quoted 8 lines · Pablo Neira Ayuso
Quoted 1 line · Phil Sutter

[...]

Quoted 5 lines
Quoted 4 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

Quoted 7 lines
Quoted 2 lines

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

I'd rather see you map the abstract syntax tree that is represented through parser_bison.y to your json representation. I think this patch is mapping the tree that we obtain after the evaluation phase, which comes with low level expressions such as bitwise/byteorder.

I don't get your point here, either: Not sure what this has to do with parser_bison.y - my patch handles output only for now, I didn't bother with input yet. Or am I on the completely wrong track now?

Quoted 6 lines
Quoted 1 line

Sorry, I was refering about the input support.

Ah, I see. For input, I did consider using bison BTW, but doubt it will lead to less code than implementing everything with libjansson. And yes, this will then be on the same level as parser_bison.y, so what is parsed will then be fed to cmd_evaluate().

Not asking to build a json parser in bison. I mean, json layout should result in an abstract syntax tree, that is exactly what nft bison would generate.

Does this clarify?

author reviewer
#7 lore raw
Quoted 31 lines · Pablo Neira Ayuso
Quoted 23 lines · Phil Sutter

On Wed, Jan 17, 2018 at 07:45:04PM +0100, Pablo Neira Ayuso wrote:
[...]

Quoted 21 lines
Quoted 20 lines · Pablo Neira Ayuso
Quoted 8 lines · Pablo Neira Ayuso
Quoted 1 line · Phil Sutter

[...]

Quoted 5 lines
Quoted 4 lines

* There is quite some code-duplication involved given that this introduces an alternative function for almost any function in the affected code path.

Quoted 7 lines
Quoted 2 lines

You mean, a new callback for each expr/datatype? We should not expose bitwise/byteorder and such, it's too low level.

I'd rather see you map the abstract syntax tree that is represented through parser_bison.y to your json representation. I think this patch is mapping the tree that we obtain after the evaluation phase, which comes with low level expressions such as bitwise/byteorder.

I don't get your point here, either: Not sure what this has to do with parser_bison.y - my patch handles output only for now, I didn't bother with input yet. Or am I on the completely wrong track now?

Quoted 6 lines
Quoted 1 line

Sorry, I was refering about the input support.

Ah, I see. For input, I did consider using bison BTW, but doubt it will lead to less code than implementing everything with libjansson. And yes, this will then be on the same level as parser_bison.y, so what is parsed will then be fed to cmd_evaluate().

Quoted 5 lines

Not asking to build a json parser in bison. I mean, json layout should result in an abstract syntax tree, that is exactly what nft bison would generate.

Does this clarify?

Well, it was clear all the time. :)

We were talking about maybe using bison as code generator to keep the layer that translates from JSON to the abstract syntax tree you are talking about slim in matters of lines of code. Hence why I mentioned it.

Cheers, Phil