Skip to contentSkip to conversation

optimize: more robust statement merge with vmap

PATCH Superseded Pablo Neira Ayuso 1 participant 2 messages Mar 3, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/2 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").

Fixes 1542082e259b ("optimize: merge same selector with different verdict into verdict map")

---
v2: - split EXPR_LIST and EXPR_SET handling.
- check for EXPR_SYMBOL (EXPR_VALUE is not yet available before evaluation).

 src/optimize.c                                   | 16 ++++++++++++++--
 .../optimizations/dumps/merge_stmts_vmap.nft     |  2 +-
 .../testcases/optimizations/merge_stmts_vmap     |  1 +
 3 files changed, 16 insertions(+), 3 deletions(-)
src/optimize.c +14 -2
index 64c0a4dbe764..cd799e0d3407 100644
@@ -437,7 +437,6 @@ static void build_verdict_map(struct expr *expr, struct stmt *verdict, struct ex
 
 	switch (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,25 @@ static void build_verdict_map(struct expr *expr, struct stmt *verdict, struct ex
 			compound_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_SYMBOL:
 		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
index 427572954a18..5a9b3006743b 100644
@@ -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
index 6511c7b20cb6..79350076d6c6 100755
@@ -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
author maintainer
#1 lore raw

PATCH v2 2/2 optimize: incorrect assert() for unexpected expression type

assert(1) is noop, this should be assert(0) instead.

Fixes 561aa3cfa8da ("optimize: merge verdict maps with same lookup key")

---
v2: no changes

 src/optimize.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/optimize.c +3 -3
index cd799e0d3407..c81d19d4739b 100644
@@ -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