Skip to contentSkip to conversation

evaluate: clone variable expression if there is more than one reference

PATCH New Pablo Neira Ayuso 1 participant 2 messages Nov 5, 2021 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 evaluate: clone variable expression if there is more than one reference

Clone the expression that defines the variable value if there are multiple reference to it in the ruleset. This saves us heap consumption specifically in case the variable defines a set with a huge number of elements.

---
 src/evaluate.c                                | 11 ++++++++++-
 .../testcases/nft-f/0030variable_reuse_0      | 19 +++++++++++++++++++
 .../nft-f/dumps/0030variable_reuse_0.nft      | 11 +++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100755 tests/shell/testcases/nft-f/0030variable_reuse_0
 create mode 100644 tests/shell/testcases/nft-f/dumps/0030variable_reuse_0.nft
src/evaluate.c +10 -1
index a268b3cb9ee2..fd7818da1116 100644
@@ -2187,7 +2187,16 @@ static int expr_evaluate_osf(struct eval_ctx *ctx, struct expr **expr)
 
 static int expr_evaluate_variable(struct eval_ctx *ctx, struct expr **exprp)
 {
-	struct expr *new = expr_clone((*exprp)->sym->expr);
+	struct symbol *sym = (*exprp)->sym;
+	struct expr *new;
+
+	/* If variable is reused from different locations in the ruleset, then
+	 * clone expression.
+	 */
+	if (sym->refcnt > 2)
+		new = expr_clone(sym->expr);
+	else
+		new = expr_get(sym->expr);
 
 	if (expr_evaluate(ctx, &new) < 0) {
 		expr_free(new);
tests/shell/testcases/nft-f/0030variable_reuse_0 new file +19 -0
new file mode 100755
index 000000000000..8afc54aa0c10
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+set -e
+
+RULESET="define test = { 1.1.1.1 }
+
+table ip x {
+        set y {
+                type ipv4_addr
+                elements = { 2.2.2.2, \$test }
+        }
+
+        set z {
+                type ipv4_addr
+                elements = { 3.3.3.3, \$test }
+        }
+}"
+
+$NFT -f - <<< "$RULESET"
tests/shell/testcases/nft-f/dumps/0030variable_reuse_0.nft new file +11 -0
new file mode 100644
index 000000000000..d895539bbfa5
@@ -0,0 +1,11 @@
+table ip x {
+        set y {
+                type ipv4_addr
+                elements = { 1.1.1.1, 2.2.2.2 }
+        }
+
+        set z {
+                type ipv4_addr
+                elements = { 1.1.1.1, 3.3.3.3 }
+        }
+}
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 evaluate: grab reference in set expression evaluation

Do not clone expression when evaluation a set expression, grabbing the reference counter to reuse the object is sufficient.

---
 src/evaluate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/evaluate.c +2 -2
index fd7818da1116..eebd992159a1 100644
@@ -1439,7 +1439,7 @@ static int expr_evaluate_set(struct eval_ctx *ctx, struct expr **expr)
 			list_for_each_entry(j, &i->left->key->expressions, list) {
 				new = mapping_expr_alloc(&i->location,
 							 expr_get(j),
-							 expr_clone(i->right));
+							 expr_get(i->right));
 				list_add_tail(&new->list, &set->expressions);
 				set->size++;
 			}
@@ -1457,7 +1457,7 @@ static int expr_evaluate_set(struct eval_ctx *ctx, struct expr **expr)
 
 		if (elem->etype == EXPR_SET_ELEM &&
 		    elem->key->etype == EXPR_SET) {
-			struct expr *new = expr_clone(elem->key);
+			struct expr *new = expr_get(elem->key);
 
 			set->set_flags |= elem->key->set_flags;
 			list_replace(&i->list, &new->list);
Signature · 1 line
2.30.2