src: allow variables in the chain priority specification
PATCH 1/1 src: allow variables in the chain priority specification
This patch introduces the use of nft input files variables in chain priority specification. e.g.
define pri = filter
define prinum = 10
add table ip foo
add chain ip foo bar {type filter hook input priority $pri;} add chain ip foo ber {type filter hook input priority $prinum;} add chain ip foo bor {type filter hook input priority $pri + 10;}
table ip foo {
chain bar {
type filter hook input priority filter; policy accept;
}
chain ber {
type filter hook input priority filter + 10; policy accept;
}
chain bor {
type filter hook input priority filter + 10; policy accept;
}
}Signed-off-by Fernando Fernandez Mancera <[email protected]>
--- include/datatype.h | 1 + include/rule.h | 8 ++-- src/datatype.c | 25 +++++++++++ src/evaluate.c | 41 ++++++++++++++----- src/parser_bison.y | 30 ++++++++++---- src/rule.c | 4 +- .../testcases/nft-f/0021priority_variable_0 | 17 ++++++++ .../testcases/nft-f/0022priority_variable_0 | 17 ++++++++ .../testcases/nft-f/0023priority_variable_1 | 18 ++++++++ .../testcases/nft-f/0024priority_variable_1 | 18 ++++++++ .../nft-f/dumps/0021priority_variable_0.nft | 5 +++ .../nft-f/dumps/0022priority_variable_0.nft | 5 +++ 12 files changed, 165 insertions(+), 24 deletions(-) mode change 100644 => 100755 src/evaluate.c create mode 100755 tests/shell/testcases/nft-f/0021priority_variable_0 create mode 100755 tests/shell/testcases/nft-f/0022priority_variable_0 create mode 100755 tests/shell/testcases/nft-f/0023priority_variable_1 create mode 100755 tests/shell/testcases/nft-f/0024priority_variable_1 create mode 100644 tests/shell/testcases/nft-f/dumps/0021priority_variable_0.nft create mode 100644 tests/shell/testcases/nft-f/dumps/0022priority_variable_0.nft
include/datatype.h +1 -0
@@ -256,6 +256,7 @@ extern const struct datatype icmpx_code_type;extern const struct datatype igmp_type_type;extern const struct datatype time_type;extern const struct datatype boolean_type; +extern const struct datatype priority_type;void inet_service_type_print (const struct expr *expr,struct output_ctx *octx);
include/rule.h +4 -4
@@ -174,13 +174,13 @@ enum chain_flags {* struct prio_spec - extendend priority specification for mixed * textual/numerical parsing. * -* @str: name of the standard priority value -* @num: Numerical value. This MUST contain the parsed value of str after +* @expr: expr of the standard priority value +* @num: Numerical value. This MUST contain the parsed value of expr after * evaluation. */ struct prio_spec { -const char *str; -int num; +struct expr *expr; +int num;struct location loc; };
src/datatype.c +25 -0
@@ -1246,3 +1246,28 @@ const struct datatype boolean_type = { .sym_tbl = &boolean_tbl, .json = boolean_type_json, }; + +static struct error_record *priority_type_parse (const struct expr *sym, +struct expr **res) +{ +int num; + +if (isdigit (sym->identifier[0 ])) { + num =atoi (sym->identifier); + *res =constant_expr_alloc (&sym->location, &integer_type, +BYTEORDER_HOST_ENDIAN , +32 , &num); + }else + *res =constant_expr_alloc (&sym->location, &string_type, +BYTEORDER_HOST_ENDIAN , +strlen (sym->identifier) * +BITS_PER_BYTE , sym->identifier); +return NULL ; +} + +const struct datatype priority_type = { + .type =TYPE_STRING , + .name ="priority" , + .desc ="priority type" , + .parse = priority_type_parse, +};
src/evaluate.c +31 -10
67 lines not on this page · raw message
src/parser_bison.y +22 -8
@@ -636,8 +636,8 @@ int nft_lex(void *, void *, void *); %type <stmt> meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc %destructor { stmt_free($$); } meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc -%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr -%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr +%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr +%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr %type <expr> primary_expr shift_expr and_expr %destructor { expr_free($$); } primary_expr shift_expr and_expr %type <expr> exclusive_or_expr inclusive_or_expr
@@ -1969,30 +1969,44 @@ extended_prio_name : OUT | STRING ; +prio_expr : variable_expr + { + datatype_set($1->sym->expr, &priority_type); + $$ = $1; + } + | extended_prio_name + { + $$ = constant_expr_alloc(&@$, &string_type, + BYTEORDER_HOST_ENDIAN, + strlen($1) * + BITS_PER_BYTE, $1); + } + ; + extended_prio_spec : int_num { struct prio_spec spec = {0}; spec.num = $1; $$ = spec; } - | extended_prio_name + | prio_expr { struct prio_spec spec = {0}; - spec.str = $1; + spec.expr = $1; $$ = spec; } - | extended_prio_name PLUS NUM + | prio_expr PLUS NUM { struct prio_spec spec = {0}; spec.num = $3; - spec.str = $1; + spec.expr = $1; $$ = spec; } - | extended_prio_name DASH NUM + | prio_expr DASH NUM { struct prio_spec spec = {0}; spec.num = -$3; - spec.str = $1; + spec.expr = $1; $$ = spec; } ;
src/rule.c +2 -2
@@ -823,7 +823,7 @@ void chain_free(struct chain *chain)xfree (chain->type);if (chain->dev !=NULL )xfree (chain->dev); -xfree (chain->priority.str); +expr_free (chain->priority.expr);xfree (chain); }
@@ -2049,7 +2049,7 @@ void flowtable_free(struct flowtable *flowtable)if (--flowtable->refcnt >0 )return ;handle_free (&flowtable->handle); -xfree (flowtable->priority.str); +expr_free (flowtable->priority.expr);xfree (flowtable); }
tests/shell/testcases/nft-f/0021priority_variable_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = filter + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/nft-f/0022priority_variable_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = 10 + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET"
tests/shell/testcases/nft-f/0023priority_variable_1 new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = * + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET" && exit 1 +exit 0
tests/shell/testcases/nft-f/0024priority_variable_1 new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash + +# Tests use of variables in priority specification + +set -e + +RULESET=" +define pri = { 127.0.0.1 } + +table inet global { + chain prerouting { + type filter hook prerouting priority \$pri + policy accept + } +}" + +$NFT -f - <<< "$RULESET" && exit 1 +exit 0
tests/shell/testcases/nft-f/dumps/0021priority_variable_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table inet global { + chain prerouting { + type filter hook prerouting priority filter; policy accept; + } +}
tests/shell/testcases/nft-f/dumps/0022priority_variable_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table inet global { + chain prerouting { + type filter hook prerouting priority filter + 10; policy accept; + } +}
Signature · 1 line
2.20.1