Skip to contentSkip to conversation

set: set parsing fixes

PATCH New Patrick McHardy 1 participant 3 messages Jan 16, 2014 netfilter

lore
Share & tools
lore
#0 lore raw

cover set: set parsing fixes

The following two patches fix a couple of problems with printed set outputs from "nft list table ..." and parsing the output again with "nft -f".

- internal flags are printed
- flags are not parsable
- initializers (elements = { }) is not parsable

With these patches, parsing the output of "nft list table ..." works in at least more cases than before when sets are present. We still have ordering problems, verdict maps referencing chains will fail since chains are created after the maps. The same problem exists generally for jump rules.

Changes to last version: set data type and evaluate initializers.

These patches have been tested to work as described. I'll push them to master now.

#1 lore raw

PATCH 1/2 set: make set flags output parsable

This patch fixes two problems:

- the output of "nft list table ..." is not parsable if sets are included because the parser can't parse the flags.

- set flags can't be specified during set creation.

To fix this, the set output is changed to:

- not print each flag on a single line
- prefix the flags with "flags "
- only show the interval flag since all others are for internal use only

The parser is changed to parse the flags specified in a set declaration. This allows to parse empty sets. The following patch will take care of parsing sets that are already populated.

---
 src/parser.y  | 24 ++++++++++++++++++++++++
 src/rule.c    | 15 +++++++++------
 src/scanner.l |  2 ++
 3 files changed, 35 insertions(+), 6 deletions(-)
src/parser.y +24 -0
index 5cd8ef6..1b09e61 100644
@@ -182,6 +182,8 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %token RETURN			"return"
 %token QUEUE			"queue"
 
+%token INTERVAL			"interval"
+
 %token <val> NUM		"number"
 %token <string> STRING		"string"
 %token <string> QUOTED_STRING
@@ -353,6 +355,8 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %type <rule>			rule
 %destructor { rule_free($$); }	rule
 
+%type <val>			set_flag_list	set_flag
+
 %type <set>			set_block_alloc set_block
 %destructor { set_free($$); }	set_block_alloc
 
@@ -737,6 +741,21 @@ set_block		:	/* empty */	{ $$ = $<set>-1; }
 				}
 				$$ = $1;
 			}
+			|	set_block	FLAGS		set_flag_list	stmt_seperator
+			{
+				$1->flags = $3;
+				$$ = $1;
+			}
+			;
+
+set_flag_list		:	set_flag_list	COMMA		set_flag
+			{
+				$$ = $1 | $3;
+			}
+			|	set_flag
+			;
+
+set_flag		:	INTERVAL	{ $$ = SET_F_INTERVAL; }
 			;
 
 map_block_alloc		:	/* empty */
@@ -769,6 +788,11 @@ map_block		:	/* empty */	{ $$ = $<set>-1; }
 
 				$$ = $1;
 			}
+			|	map_block	FLAGS		set_flag_list	stmt_seperator
+			{
+				$1->flags = $3;
+				$$ = $1;
+			}
 			;
 
 hook_spec		:	TYPE		STRING		HOOK		STRING		PRIORITY	NUM
src/rule.c +9 -6
index 04dd6c7..0f7f4b5 100644
@@ -89,6 +89,7 @@ struct set *set_lookup(const struct table *table, const char *name)
 
 void set_print(const struct set *set)
 {
+	const char *delim = "";
 	const char *type;
 
 	type = set->flags & SET_F_MAP ? "map" : "set";
@@ -99,12 +100,14 @@ void set_print(const struct set *set)
 		printf(" : %s", set->datatype->name);
 	printf("\n");
 
-	if (set->flags & SET_F_ANONYMOUS)
-		printf("\t\tanonymous\n");
-	if (set->flags & SET_F_CONSTANT)
-		printf("\t\tconstant\n");
-	if (set->flags & SET_F_INTERVAL)
-		printf("\t\tinterval\n");
+	if (set->flags & (SET_F_INTERVAL)) {
+		printf("\t\tflags ");
+		if (set->flags & SET_F_INTERVAL) {
+			printf("%sinterval", delim);
+			delim = ",";
+		}
+		printf("\n");
+	}
 
 	if (set->init != NULL && set->init->size > 0) {
 		printf("\t\telements = ");
src/scanner.l +2 -0
index 25fbc61..904d6fb 100644
@@ -256,6 +256,8 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 "position"		{ return POSITION; }
 
+"interval"		{ return INTERVAL; }
+
 "counter"		{ return COUNTER; }
 "packets"		{ return PACKETS; }
 "bytes"			{ return BYTES; }
Signature · 1 line
1.8.4.2
#2 lore raw

PATCH 2/2 set: make set initializer parsable

In reply to Patrick McHardy

If a set contains elements, the output is not parsable since the elements = { ... } is not understood by the parser. Fix this and also add support for creating constant sets (which only makes sense when using an initializer).

---
 src/evaluate.c |  6 ++++++
 src/parser.y   | 15 ++++++++++++++-
 src/rule.c     |  6 +++++-
 src/scanner.l  |  2 ++
 4 files changed, 27 insertions(+), 2 deletions(-)
src/evaluate.c +6 -0
index 94fee64..21ca558 100644
@@ -1210,6 +1210,12 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set)
 		return set_error(ctx, set, "unqualified key data type "
 				 "specified in %s definition", type);
 
+	if (set->init != NULL) {
+		expr_set_context(&ctx->ectx, set->keytype, set->keylen);
+		if (expr_evaluate(ctx, &set->init) < 0)
+			return -1;
+	}
+
 	if (!(set->flags & SET_F_MAP))
 		return 0;
 
src/parser.y +14 -1
index 1b09e61..345d8d0 100644
@@ -182,7 +182,9 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %token RETURN			"return"
 %token QUEUE			"queue"
 
+%token CONSTANT			"constant"
 %token INTERVAL			"interval"
+%token ELEMENTS			"elements"
 
 %token <val> NUM		"number"
 %token <string> STRING		"string"
@@ -746,6 +748,11 @@ set_block		:	/* empty */	{ $$ = $<set>-1; }
 				$1->flags = $3;
 				$$ = $1;
 			}
+			|	set_block	ELEMENTS	'='		set_expr
+			{
+				$1->init = $4;
+				$$ = $1;
+			}
 			;
 
 set_flag_list		:	set_flag_list	COMMA		set_flag
@@ -755,7 +762,8 @@ set_flag_list		:	set_flag_list	COMMA		set_flag
 			|	set_flag
 			;
 
-set_flag		:	INTERVAL	{ $$ = SET_F_INTERVAL; }
+set_flag		:	CONSTANT	{ $$ = SET_F_CONSTANT; }
+			|	INTERVAL	{ $$ = SET_F_INTERVAL; }
 			;
 
 map_block_alloc		:	/* empty */
@@ -793,6 +801,11 @@ map_block		:	/* empty */	{ $$ = $<set>-1; }
 				$1->flags = $3;
 				$$ = $1;
 			}
+			|	map_block	ELEMENTS	'='		set_expr
+			{
+				$1->init = $4;
+				$$ = $1;
+			}
 			;
 
 hook_spec		:	TYPE		STRING		HOOK		STRING		PRIORITY	NUM
src/rule.c +5 -1
index 0f7f4b5..9f6c04b 100644
@@ -100,8 +100,12 @@ void set_print(const struct set *set)
 		printf(" : %s", set->datatype->name);
 	printf("\n");
 
-	if (set->flags & (SET_F_INTERVAL)) {
+	if (set->flags & (SET_F_CONSTANT | SET_F_INTERVAL)) {
 		printf("\t\tflags ");
+		if (set->flags & SET_F_CONSTANT) {
+			printf("%sconstant", delim);
+			delim = ",";
+		}
 		if (set->flags & SET_F_INTERVAL) {
 			printf("%sinterval", delim);
 			delim = ",";
src/scanner.l +2 -0
index 904d6fb..c47e610 100644
@@ -256,7 +256,9 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 "position"		{ return POSITION; }
 
+"constant"		{ return CONSTANT; }
 "interval"		{ return INTERVAL; }
+"elements"		{ return ELEMENTS; }
 
 "counter"		{ return COUNTER; }
 "packets"		{ return PACKETS; }
Signature · 1 line
1.8.4.2