Skip to contentSkip to conversation

make set flags output parsable

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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

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 dclaration.

As usual, if no objections I'll push it to master.

src/parser.y +19 -0
index d4a7929..bc89887 100644
@@ -183,6 +183,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
@@ -354,6 +356,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
 
@@ -738,6 +742,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 */
src/rule.c +9 -6
index ec8b6a4..0719c8d 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_CONSTANT | 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 936c035..f22fbe8 100644
@@ -257,6 +257,8 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 "position"		{ return POSITION; }
 
+"interval"		{ return INTERVAL; }
+
 "counter"		{ return COUNTER; }
 "packets"		{ return PACKETS; }
 "bytes"			{ return BYTES; }
#1 lore raw

PATCH 1/1 make set initializer parsable

commit 92dfa9b19853c367efbb083d5ad1b7cb56ce3d8a

Author: Patrick McHardy <[email protected]>
Date:   Thu Jan 16 19:37:53 2014 +0000

set: make set initializer parsable

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/parser.y +14 -1
index eab973c..34d14ec 100644
@@ -183,7 +183,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"
@@ -747,6 +749,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
@@ -756,7 +763,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 */
@@ -794,6 +802,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 f1fe355..acdddcb 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 f22fbe8..721b551 100644
@@ -257,7 +257,9 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 "position"		{ return POSITION; }
 
+"constant"		{ return CONSTANT; }
 "interval"		{ return INTERVAL; }
+"elements"		{ return ELEMENTS; }
 
 "counter"		{ return COUNTER; }
 "packets"		{ return PACKETS; }