Skip to contentSkip to conversation

expression: default to print binary operations using nominal representation

RFC PATCH In review Pablo Neira Ayuso 3 participants 12 messages Jan 15, 2014 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

RFC PATCH 1/2 expression: default to print binary operations using nominal representation

Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.

---
 src/expression.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
src/expression.c +10 -10
index 71154cc..6da5c10 100644
@@ -351,17 +351,17 @@ const char *expr_op_symbols[] = {
 	[OP_INVALID]	= "invalid",
 	[OP_HTON]	= "hton",
 	[OP_NTOH]	= "ntoh",
-	[OP_AND]	= "&",
-	[OP_OR]		= "|",
-	[OP_XOR]	= "^",
-	[OP_LSHIFT]	= "<<",
-	[OP_RSHIFT]	= ">>",
+	[OP_AND]	= "and",
+	[OP_OR]		= "or",
+	[OP_XOR]	= "xor",
+	[OP_LSHIFT]	= "lshift",
+	[OP_RSHIFT]	= "rshift",
 	[OP_EQ]		= NULL,
-	[OP_NEQ]	= "!=",
-	[OP_LT]		= "<",
-	[OP_GT]		= ">",
-	[OP_LTE]	= "<=",
-	[OP_GTE]	= ">=",
+	[OP_NEQ]	= "ne",
+	[OP_LT]		= "lt",
+	[OP_GT]		= "gt",
+	[OP_LTE]	= "le",
+	[OP_GTE]	= "ge",
 	[OP_RANGE]	= "within range",
 	[OP_LOOKUP]	= NULL,
 };
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH 2/2 expression: fix printing of binary operation

This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:

& 0x00000003 0x00000001

to:

and 0x00000003 == 0x00000001

An example output of how thing are left after this patch follows:

% nft list table filter

table ip filter {
        chain output {
                 type filter hook output priority 0;
                 meta mark and 0x00000003 == 0x00000001 counter packets 0 bytes 0
                 meta mark 0x00000003 counter packets 0 bytes 0
        }
}
---
 src/expression.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
src/expression.c +10 -1
index 6da5c10..452b0d7 100644
@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
+
 	expr_print(expr->right);
+	printf(" ==");
 }
 
 static void binop_expr_clone(struct expr *new, const struct expr *expr)
@@ -447,10 +449,17 @@ struct expr *binop_expr_alloc(const struct location *loc, enum ops op,
 	return expr;
 }
 
+static void relational_expr_print(const struct expr *expr)
+{
+	expr_print(expr->left);
+	printf(" ");
+	expr_print(expr->right);
+}
+
 static const struct expr_ops relational_expr_ops = {
 	.type		= EXPR_RELATIONAL,
 	.name		= "relational",
-	.print		= binop_expr_print,
+	.print		= relational_expr_print,
 	.destroy	= binop_expr_destroy,
 };
 
Signature · 1 line
1.7.10.4
#9 lore raw
In reply to Pablo Neira Ayuso
Quoted 3 lines · Pablo Neira Ayuso

Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.

I was thinking about this before, but didn't mention it since you hadn't included it in your first patch. I'd prefer to stick to the IMO more readable existing form. Since we don't support deleting rules by rule specification, there should be no reason to copy and paste the output to the command line again.

Quoted 39 lines
---
 src/expression.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
src/expression.c +10 -10
index 71154cc..6da5c10 100644
@@ -351,17 +351,17 @@ const char *expr_op_symbols[] = {
 	[OP_INVALID]	= "invalid",
 	[OP_HTON]	= "hton",
 	[OP_NTOH]	= "ntoh",
-	[OP_AND]	= "&",
-	[OP_OR]		= "|",
-	[OP_XOR]	= "^",
-	[OP_LSHIFT]	= "<<",
-	[OP_RSHIFT]	= ">>",
+	[OP_AND]	= "and",
+	[OP_OR]		= "or",
+	[OP_XOR]	= "xor",
+	[OP_LSHIFT]	= "lshift",
+	[OP_RSHIFT]	= "rshift",
 	[OP_EQ]		= NULL,
-	[OP_NEQ]	= "!=",
-	[OP_LT]		= "<",
-	[OP_GT]		= ">",
-	[OP_LTE]	= "<=",
-	[OP_GTE]	= ">=",
+	[OP_NEQ]	= "ne",
+	[OP_LT]		= "lt",
+	[OP_GT]		= "gt",
+	[OP_LTE]	= "le",
+	[OP_GTE]	= "ge",
 	[OP_RANGE]	= "within range",
 	[OP_LOOKUP]	= NULL,
 };
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 2/2] expression: fix printing of binary operation

In reply to Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso

This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:

& 0x00000003 0x00000001

to:

and 0x00000003 == 0x00000001

src/expression.c +0 -0
index 6da5c10..452b0d7 100644

src/expression.c static void binop_expr_print(const struct expr *expr) · line 416

Quoted 6 lines
src/expression.c +2 -0
@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
+
 	expr_print(expr->right);
+	printf(" ==");

That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.

author maintainer
#3 lore raw

Re: [PATCH 2/2] expression: fix printing of binary operation

Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso

This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:

& 0x00000003 0x00000001

to:

and 0x00000003 == 0x00000001

Quoted 15 lines

src/expression.c static void binop_expr_print(const struct expr *expr) · line 416

Quoted 11 lines
src/expression.c +2 -0
index 6da5c10..452b0d7 100644
@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
+
 	expr_print(expr->right);
+	printf(" ==");

That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.

Indeed, this can be !=.

#5 lore raw

Re: [PATCH 2/2] expression: fix printing of binary operation

In reply to Patrick McHardy
Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso

This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:

& 0x00000003 0x00000001

to:

and 0x00000003 == 0x00000001

Quoted 15 lines

src/expression.c static void binop_expr_print(const struct expr *expr) · line 416

Quoted 11 lines
src/expression.c +2 -0
index 6da5c10..452b0d7 100644
@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
+
 	expr_print(expr->right);
+	printf(" ==");

That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.

Something like this:

src/expression.c +4 -2
index 71154cc..518f71c 100644
@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = {
 	[OP_XOR]	= "^",
 	[OP_LSHIFT]	= "<<",
 	[OP_RSHIFT]	= ">>",
-	[OP_EQ]		= NULL,
+	[OP_EQ]		= "==",
 	[OP_NEQ]	= "!=",
 	[OP_LT]		= "<",
 	[OP_GT]		= ">",
@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,
 static void binop_expr_print(const struct expr *expr)
 {
 	expr_print(expr->left);
-	if (expr_op_symbols[expr->op] != NULL)
+	if (expr_op_symbols[expr->op] &&
+	    (expr->op != OP_EQ ||
+	     expr->left->ops->type == EXPR_BINOP))
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
#4 lore raw

Re: [PATCH 2/2] expression: fix printing of binary operation

In reply to Pablo Neira Ayuso
Quoted 28 lines · Pablo Neira Ayuso
Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso

This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:

& 0x00000003 0x00000001

to:

and 0x00000003 == 0x00000001

Quoted 15 lines

src/expression.c static void binop_expr_print(const struct expr *expr) · line 416

Quoted 11 lines
src/expression.c +2 -0
index 6da5c10..452b0d7 100644
@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
+
 	expr_print(expr->right);
+	printf(" ==");

That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.

Quoted 1 line

Indeed, this can be !=.

Yep, that as well. The patch I just sent should handle it correctly.

#10 lore raw
In reply to Patrick McHardy
Quoted 10 lines · Patrick McHardy
Quoted 3 lines · Pablo Neira Ayuso

Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.

I was thinking about this before, but didn't mention it since you hadn't included it in your first patch. I'd prefer to stick to the IMO more readable existing form. Since we don't support deleting rules by rule specification, there should be no reason to copy and paste the output to the command line again.

What about a file to be loaded with `nft -f'?
In fact, I think most of users will end with a config file to load with `nft -f'.

It may be annoying getting the output different to what you wrote.

I think this patch means that the symbol-free syntax is the default, and I personally like that.

Signature · 5 lines
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#11 lore raw
Quoted 12 lines · Arturo Borrero Gonzalez
Quoted 10 lines · Patrick McHardy
Quoted 3 lines · Pablo Neira Ayuso

Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.

I was thinking about this before, but didn't mention it since you hadn't included it in your first patch. I'd prefer to stick to the IMO more readable existing form. Since we don't support deleting rules by rule specification, there should be no reason to copy and paste the output to the command line again.

Quoted 3 lines

What about a file to be loaded with `nft -f'?
In fact, I think most of users will end with a config file to load with `nft -f'.

Indeed, and those users don't need the long syntax since the shell is not involved.

Quoted 1 line

It may be annoying getting the output different to what you wrote.

That may happen for other reasons anyways, especially with binops we optimize away redundant expressions and calculate constants in userspace.

Quoted 2 lines

I think this patch means that the symbol-free syntax is the default, and I personally like that.

I don't, its longer and harder to read. It has its justification for parsing on the command line, but as you say, most users won't use it that way.

author maintainer
#6 lore raw

Re: [PATCH 2/2] expression: fix printing of binary operation

In reply to Patrick McHardy
Quoted 45 lines · Patrick McHardy
Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso

This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:

& 0x00000003 0x00000001

to:

and 0x00000003 == 0x00000001

Quoted 15 lines

src/expression.c static void binop_expr_print(const struct expr *expr) · line 416

Quoted 11 lines
src/expression.c +2 -0
index 6da5c10..452b0d7 100644
@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");
+
 	expr_print(expr->right);
+	printf(" ==");

That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.

Something like this:

src/expression.c +1 -1
index 71154cc..518f71c 100644
@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = {
 	[OP_XOR]	= "^",
 	[OP_LSHIFT]	= "<<",
 	[OP_RSHIFT]	= ">>",
-	[OP_EQ]		= NULL,
+	[OP_EQ]		= "==",
 	[OP_NEQ]	= "!=",
 	[OP_LT]		= "<",
 	[OP_GT]		= ">",

src/expression.c struct expr *unary_expr_alloc(const struct location *loc, · line 415

Quoted 10 lines
src/expression.c +3 -1
@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,
 static void binop_expr_print(const struct expr *expr)
 {
 	expr_print(expr->left);
-	if (expr_op_symbols[expr->op] != NULL)
+	if (expr_op_symbols[expr->op] &&
+	    (expr->op != OP_EQ ||
+	     expr->left->ops->type == EXPR_BINOP))
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
 		printf(" ");

This looks a bit more complicated. To my understanding, the right-hand side of the relational tree contains the value. The left-hand side contains the binop tree, whose left-hand side is the meta mark and the right-hand side is the value to apply the operation. The print function doesn't have context to know what's on the right-hand side of the upper relational expression. Thinking how to fix this...

#7 lore raw

Re: [PATCH 2/2] expression: fix printing of binary operation

Quoted 33 lines · Pablo Neira Ayuso
Quoted 32 lines · Patrick McHardy
Quoted 4 lines · Patrick McHardy

That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.

Something like this:

src/expression.c +4 -2
index 71154cc..518f71c 100644
@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = {
 	[OP_XOR]	= "^",
 	[OP_LSHIFT]	= "<<",
 	[OP_RSHIFT]	= ">>",
-	[OP_EQ]		= NULL,
+	[OP_EQ]		= "==",
 	[OP_NEQ]	= "!=",
 	[OP_LT]		= "<",
 	[OP_GT]		= ">",
@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,
 static void binop_expr_print(const struct expr *expr)
 {
 	expr_print(expr->left);
-	if (expr_op_symbols[expr->op] != NULL)
+	if (expr_op_symbols[expr->op] &&
+	    (expr->op != OP_EQ ||
+	     expr->left->ops->type == EXPR_BINOP))
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
Quoted 8 lines

src/expression.c struct expr *unary_expr_alloc(const struct location *loc, · line 415

Quoted 1 line
src/expression.c +0 -0
 		printf(" ");

This looks a bit more complicated. To my understanding, the right-hand side of the relational tree contains the value. The left-hand side contains the binop tree, whose left-hand side is the meta mark and the right-hand side is the value to apply the operation. The print function doesn't have context to know what's on the right-hand side of the upper relational expression. Thinking how to fix this...

This OP_EQ case is the upper relational expression. Try it, it works fine :)

author maintainer
#8 lore raw

Re: [PATCH 2/2] expression: fix printing of binary operation

Quoted 43 lines · Patrick McHardy
Quoted 33 lines · Pablo Neira Ayuso
Quoted 32 lines · Patrick McHardy
Quoted 4 lines · Patrick McHardy

That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.

Something like this:

src/expression.c +4 -2
index 71154cc..518f71c 100644
@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = {
 	[OP_XOR]	= "^",
 	[OP_LSHIFT]	= "<<",
 	[OP_RSHIFT]	= ">>",
-	[OP_EQ]		= NULL,
+	[OP_EQ]		= "==",
 	[OP_NEQ]	= "!=",
 	[OP_LT]		= "<",
 	[OP_GT]		= ">",
@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,
 static void binop_expr_print(const struct expr *expr)
 {
 	expr_print(expr->left);
-	if (expr_op_symbols[expr->op] != NULL)
+	if (expr_op_symbols[expr->op] &&
+	    (expr->op != OP_EQ ||
+	     expr->left->ops->type == EXPR_BINOP))
 		printf(" %s ", expr_op_symbols[expr->op]);
 	else
Quoted 8 lines

src/expression.c struct expr *unary_expr_alloc(const struct location *loc, · line 415

Quoted 1 line
src/expression.c +0 -0
 		printf(" ");

This looks a bit more complicated. To my understanding, the right-hand side of the relational tree contains the value. The left-hand side contains the binop tree, whose left-hand side is the meta mark and the right-hand side is the value to apply the operation. The print function doesn't have context to know what's on the right-hand side of the upper relational expression. Thinking how to fix this...

Quoted 1 line

This OP_EQ case is the upper relational expression. Try it, it works fine :)

Indeed, thanks Patrick.