optimize: more robust statement merge with vmap
Pablo Neira Ayuso
maintainerPATCH v3 1/3 optimize: more robust statement merge with vmap
Check expressions that are expected on the rhs rather than using a catch-all default case.
Actually, lists and sets need to be their own routine, because this needs the set element key expression to be merged.
This is a follow up to 99eb46969f3d ("optimize: fix vmap with anonymous sets").
---
v3: - EXPR_VALUE is possible, parser might allocate this expression before eval.
- EXPR_CONCAT is also supported.
src/optimize.c | 18 ++++++++++++++++-- .../optimizations/dumps/merge_stmts_vmap.nft | 2 +- .../testcases/optimizations/merge_stmts_vmap | 1 + 3 files changed, 18 insertions(+), 3 deletions(-)
src/optimize.c +16 -2
@@ -437,7 +437,6 @@ static void build_verdict_map(struct expr *expr, struct stmt *verdict, struct exswitch (expr->etype) {case EXPR_LIST : -case EXPR_SET :list_for_each_entry (item, &expr->expressions, list) { elem =set_elem_expr_alloc (&internal_location,expr_get (item)); mapping = mapping_expr_alloc(&internal_location, elem,
@@ -445,12 +444,27 @@ static void build_verdict_map(struct expr *expr, struct stmt *verdict, struct excompound_expr_add (set, mapping); }break ; -default : +case EXPR_SET : +list_for_each_entry (item, &expr->expressions, list) { + elem =set_elem_expr_alloc (&internal_location,expr_get (item->key)); + mapping =mapping_expr_alloc (&internal_location, elem, +expr_get (verdict->expr)); +compound_expr_add (set, mapping); + } +break ; +case EXPR_PREFIX : +case EXPR_RANGE : +case EXPR_VALUE : +case EXPR_SYMBOL : +case EXPR_CONCAT : elem =set_elem_expr_alloc (&internal_location,expr_get (expr)); mapping =mapping_expr_alloc (&internal_location, elem,expr_get (verdict->expr));compound_expr_add (set, mapping);break ; +default : +assert (0 ); +break ; } }
tests/shell/testcases/optimizations/dumps/merge_stmts_vmap.nft +1 -1
@@ -4,6 +4,6 @@ table ip x { } chain z { - tcp dport vmap { 1 : accept, 2-3 : drop } + tcp dport vmap { 1 : accept, 2-3 : drop, 4 : accept } } }
tests/shell/testcases/optimizations/merge_stmts_vmap +1 -0
@@ -10,6 +10,7 @@ RULESET="table ip x { chain z { tcp dport { 1 } accept tcp dport 2-3 drop + tcp dport 4 accept } }"
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v3 2/3 optimize: incorrect assert() for unexpected expression type
assert(1) is noop, this should be assert(0) instead.
---
v3: no changes
src/optimize.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
src/optimize.c +3 -3
@@ -362,11 +362,11 @@ static void merge_verdict_stmts(const struct optimize_ctx *ctx,merge_vmap (ctx, stmt_a, stmt_b);break ;default : -assert (1 ); +assert (0 ); }break ;default : -assert (1 ); +assert (0 );break ; } }
@@ -385,7 +385,7 @@ static void merge_stmts(const struct optimize_ctx *ctx,merge_verdict_stmts (ctx, from, to, merge, stmt_a);break ;default : -assert (1 ); +assert (0 ); } }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v3 3/3 optimize: do not merge unsupported statement expressions
In reply to Pablo Neira AyusoOnly value, range, prefix, set and list are supported at this stage.
---
v3: EXPR_VALUE is possible, keep it
src/optimize.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
src/optimize.c +21 -0
@@ -91,6 +91,23 @@ static bool __expr_cmp(const struct expr *expr_a, const struct expr *expr_b)return true; } +static bool stmt_expr_supported (const struct expr *expr) +{ +switch (expr->right->etype) { +case EXPR_SYMBOL : +case EXPR_RANGE : +case EXPR_PREFIX : +case EXPR_SET : +case EXPR_LIST : +case EXPR_VALUE : +return true; +default : +break ; + } + +return false; +} +static bool __stmt_type_eq (const struct stmt *stmt_a,const struct stmt *stmt_b) {struct expr *expr_a, *expr_b;
@@ -103,6 +120,10 @@ static bool __stmt_type_eq(const struct stmt *stmt_a, const struct stmt *stmt_b) expr_a = stmt_a->expr; expr_b = stmt_b->expr; +if (!stmt_expr_supported (expr_a) || + !stmt_expr_supported (expr_b)) +return false; +return __expr_cmp (expr_a->left, expr_b->left);case STMT_COUNTER :case STMT_NOTRACK :
Signature · 1 line
2.30.2