Skip to contentSkip to conversation

Support intra-transaction rule references

PATCH In review Phil Sutter 2 participants 7 messages Jun 7, 2019 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

v6 cover Support intra-transaction rule references

After Pablo's evaluation sequence rework, this series (formerly fixing cache updates as well) has shrunken considerably:

Patch 1 contains a proper fix for that workaround in evaluate_cache_add().

Patch 2 removes the cache-related workaround in tests/json_echo.

Patches 3 and 4 contain prerequisites for the last one, which actually implements the support for referencing rules of the same transation with 'index' keyword.

Phil Sutter (5):
cache: Fix evaluation for rules with index reference tests/json_echo: Drop needless workaround
rule: Introduce rule_lookup_by_index()
src: Make cache_is_complete() public
src: Support intra-transaction rule references

 include/rule.h                                |  5 +
 src/cache.c                                   |  8 +-
 src/evaluate.c                                | 94 +++++++++++++++----
 src/mnl.c                                     |  4 +
 src/rule.c                                    | 13 ++-
 tests/json_echo/run-test.py                   |  6 +-
 .../shell/testcases/cache/0003_cache_update_0 |  7 ++
 tests/shell/testcases/transactions/0024rule_0 | 17 ++++
 tests/shell/testcases/transactions/0025rule_0 | 21 +++++
 .../transactions/dumps/0024rule_0.nft         |  8 ++
 .../transactions/dumps/0025rule_0.nft         |  6 ++
 11 files changed, 157 insertions(+), 32 deletions(-)
 create mode 100755 tests/shell/testcases/transactions/0024rule_0
 create mode 100755 tests/shell/testcases/transactions/0025rule_0
 create mode 100644 tests/shell/testcases/transactions/dumps/0024rule_0.nft
 create mode 100644 tests/shell/testcases/transactions/dumps/0025rule_0.nft
Signature · 1 line
2.21.0
author reviewer
#1 lore raw

PATCH v6 1/5 cache: Fix evaluation for rules with index reference

After parsing input, rule location data (index or handle) is contained in cmd->handle, not yet in cmd->rule->handle.

Fixes 7df42800cf89e ("src: single cache_update() call to build cache before evaluation")
---
 src/cache.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
src/cache.c +1 -7
index 2a0f04d12e259..532ef425906ad 100644
@@ -24,13 +24,7 @@ static unsigned int evaluate_cache_add(struct cmd *cmd)
 		completeness = cmd->op;
 		break;
 	case CMD_OBJ_RULE:
-		/* XXX index is set to zero unless this handle_merge() call is
-		 * invoked, this handle_merge() call is done from the
-		 * evaluation, which is too late.
-		 */
-		handle_merge(&cmd->rule->handle, &cmd->handle);
-
-		if (cmd->rule->handle.index.id)
+		if (cmd->handle.index.id)
 			completeness = CMD_LIST;
 		break;
 	default:
Signature · 1 line
2.21.0
author reviewer
#2 lore raw

PATCH v6 2/5 tests/json_echo: Drop needless workaround

In reply to Phil Sutter

With cache issues now resolved, there is no need for the multi add test workaround anymore.

---
 tests/json_echo/run-test.py | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tests/json_echo/run-test.py +2 -4
index dd7797fb6f041..a636d5f247702 100755
@@ -271,12 +271,10 @@ add_quota["add"]["quota"]["name"] = "q"
 do_flush()
 
 print("doing multi add")
-# XXX: Add table separately, otherwise this triggers cache bug
-out = do_command(add_table)
-thandle = get_handle(out, add_table["add"])
-add_multi = [ add_chain, add_set, add_rule ]
+add_multi = [ add_table, add_chain, add_set, add_rule ]
 out = do_command(add_multi)
 
+thandle = get_handle(out, add_table["add"])
 chandle = get_handle(out, add_chain["add"])
 shandle = get_handle(out, add_set["add"])
 rhandle = get_handle(out, add_rule["add"])
Signature · 1 line
2.21.0
author reviewer
#3 lore raw

PATCH v6 3/5 rule: Introduce rule_lookup_by_index()

In reply to Phil Sutter

In contrast to rule_lookup(), this function returns a chain's rule at a given index instead of by handle.

---
 include/rule.h |  2 ++
 src/rule.c     | 11 +++++++++++
 2 files changed, 13 insertions(+)
include/rule.h +2 -0
index bf3f39636efb5..87b440b63ba5c 100644
@@ -260,6 +260,8 @@ extern struct rule *rule_get(struct rule *rule);
 extern void rule_free(struct rule *rule);
 extern void rule_print(const struct rule *rule, struct output_ctx *octx);
 extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle);
+extern struct rule *rule_lookup_by_index(const struct chain *chain,
+					 uint64_t index);
 
 /**
  * struct set - nftables set
src/rule.c +11 -0
index e570238a40f5b..20fe6f3758cbc 100644
@@ -641,6 +641,17 @@ struct rule *rule_lookup(const struct chain *chain, uint64_t handle)
 	return NULL;
 }
 
+struct rule *rule_lookup_by_index(const struct chain *chain, uint64_t index)
+{
+	struct rule *rule;
+
+	list_for_each_entry(rule, &chain->rules, list) {
+		if (!--index)
+			return rule;
+	}
+	return NULL;
+}
+
 struct scope *scope_init(struct scope *scope, const struct scope *parent)
 {
 	scope->parent = parent;
Signature · 1 line
2.21.0
author reviewer
#4 lore raw

PATCH v6 4/5 src: Make cache_is_complete() public

In reply to Phil Sutter
---
 include/rule.h | 1 +
 src/rule.c     | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/rule.h +1 -0
index 87b440b63ba5c..8ccdc2e1c143f 100644
@@ -639,6 +639,7 @@ extern int cache_update(struct nft_ctx *ctx, enum cmd_ops cmd,
 extern void cache_flush(struct nft_ctx *ctx, enum cmd_ops cmd,
 			struct list_head *msgs);
 extern void cache_release(struct nft_cache *cache);
+extern bool cache_is_complete(struct nft_cache *cache, enum cmd_ops cmd);
 
 struct timeout_protocol {
 	uint32_t array_size;
src/rule.c +1 -1
index 20fe6f3758cbc..ad549b1eee8ac 100644
@@ -232,7 +232,7 @@ static int cache_completeness(enum cmd_ops cmd)
 	return 1;
 }
 
-static bool cache_is_complete(struct nft_cache *cache, enum cmd_ops cmd)
+bool cache_is_complete(struct nft_cache *cache, enum cmd_ops cmd)
 {
 	return cache_completeness(cache->cmd) >= cache_completeness(cmd);
 }
Signature · 1 line
2.21.0
author reviewer
#5 lore raw

PATCH v6 5/5 src: Support intra-transaction rule references

In reply to Phil Sutter

A rule may be added before or after another one using index keyword. To support for the other rule being added within the same batch, one has to make use of NFTNL_RULE_ID and NFTNL_RULE_POSITION_ID attributes. This patch does just that among a few more crucial things:

* If cache is complete enough to contain rules, update cache when evaluating rule commands so later index references resolve correctly.

* Reduce rule_translate_index() to its core code which is the actual linking of rules and consequently rename the function. The removed bits are pulled into the calling rule_evaluate() to reduce code duplication in between cache updates with and without rule reference.

* Pass the current command op to rule_evaluate() as indicator whether to insert before or after a referenced rule or at beginning or end of chain in cache. Exploit this from chain_evaluate() to avoid adding the chain's rules a second time.

---
Changes since v5:
- Adjust to new cache update fix.
- Move rule_cache_update() to evaluate.c, the only place where it is called from.
- Simplify rule_cache_update() signature: Since it is only called from rule_evaluate(), passed rule pointer is always available in ctx->rule so use that instead.

Changes since v4:
- Move whole rule reference finding and linking code into rule_cache_update() to simplify callers.
- Move rule_cache_update() to right before cache_add_rule_cmd().
- Fix the code regarding which handle and which rule pointer to use, resolves The segfault Eric mentioned.
- Skip cache updates for rule delete and replace commands, this restores old behaviour regarding "guessed" rule handles.
- Also simplify skp logic itself: If cache is complete, add all rules while evaluating them. Previous cache completeness level lowering was problematic after cache flush command.
- Add additional test case for delete/replace commands.

Changes since v1:
- Move rule list manipulation into a dedicated function rule_cache_update().
- Restore old performance for simple commands by fetching a full rule cache only if the currently evaluated rule references another one.
- Extend 0024rule_0 test a bit to make sure things work with interactive nft also.

---
 include/rule.h                                |  2 +
 src/evaluate.c                                | 94 +++++++++++++++----
 src/mnl.c                                     |  4 +
 .../shell/testcases/cache/0003_cache_update_0 |  7 ++
 tests/shell/testcases/transactions/0024rule_0 | 17 ++++
 tests/shell/testcases/transactions/0025rule_0 | 21 +++++
 .../transactions/dumps/0024rule_0.nft         |  8 ++
 .../transactions/dumps/0025rule_0.nft         |  6 ++
 8 files changed, 139 insertions(+), 20 deletions(-)
 create mode 100755 tests/shell/testcases/transactions/0024rule_0
 create mode 100755 tests/shell/testcases/transactions/0025rule_0
 create mode 100644 tests/shell/testcases/transactions/dumps/0024rule_0.nft
 create mode 100644 tests/shell/testcases/transactions/dumps/0025rule_0.nft
include/rule.h +2 -0
index 8ccdc2e1c143f..dd9df9ec6dd82 100644
@@ -73,6 +73,8 @@ struct handle {
 	struct position_spec	position;
 	struct position_spec	index;
 	uint32_t		set_id;
+	uint32_t		rule_id;
+	uint32_t		position_id;
 };
 
 extern void handle_merge(struct handle *dst, const struct handle *src);
src/evaluate.c +74 -20

133 lines not on this page · raw message

src/mnl.c +4 -0
index 83dfb9d2da20a..6ebad28bfc7d2 100644
@@ -378,6 +378,10 @@ int mnl_nft_rule_add(struct netlink_ctx *ctx, const struct cmd *cmd,
 	nftnl_rule_set_str(nlr, NFTNL_RULE_CHAIN, h->chain.name);
 	if (h->position.id)
 		nftnl_rule_set_u64(nlr, NFTNL_RULE_POSITION, h->position.id);
+	if (h->rule_id)
+		nftnl_rule_set_u32(nlr, NFTNL_RULE_ID, h->rule_id);
+	if (h->position_id)
+		nftnl_rule_set_u32(nlr, NFTNL_RULE_POSITION_ID, h->position_id);
 
 	netlink_linearize_rule(ctx, nlr, rule);
 	nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch),
tests/shell/testcases/cache/0003_cache_update_0 +7 -0
index fa9b5df380a41..05edc9c7c33eb 100755
@@ -34,6 +34,9 @@ EOF
 # add rule ip t4 c meta l4proto icmp accept -> rule to reference in next step
 # add rule ip t4 c index 0 drop -> index 0 is not found due to rule cache not
 #                                  being updated
+# add rule ip t4 c index 2 drop -> index 2 is not found due to igmp rule being
+#                                  in same transaction and therefore not having
+#                                  an allocated handle
 $NFT -i >/dev/null <<EOF
 add table ip t4; add chain ip t4 c
 add rule ip t4 c meta l4proto icmp accept
@@ -41,3 +44,7 @@ EOF
 $NFT -f - >/dev/null <<EOF
 add rule ip t4 c index 0 drop
 EOF
+$NFT -f - >/dev/null <<EOF
+add rule ip t4 c meta l4proto igmp accept
+add rule ip t4 c index 2 drop
+EOF
tests/shell/testcases/transactions/0024rule_0 new file +17 -0
new file mode 100755
index 0000000000000..4c1ac41db3b47
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+RULESET="flush ruleset
+add table x
+add chain x y
+add rule x y accept comment rule1
+add rule x y accept comment rule4
+add rule x y index 0 accept comment rule2
+insert rule x y index 2 accept comment rule3"
+
+$NFT -f - <<< "$RULESET" && \
+	$NFT -f - <<< "$RULESET" && \
+	echo "$RULESET" | tr '\n' ';' | $NFT -i >/dev/null && \
+	exit 0
+echo "E: intra-transaction rule reference failed"
+exit 1
+
tests/shell/testcases/transactions/0025rule_0 new file +21 -0
new file mode 100755
index 0000000000000..d72d5cfcc75d4
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+# make sure stored delete/replace rule commands are correctly applied
+
+set -e
+
+$NFT -f - <<EOF
+flush ruleset
+table x {
+	chain y {
+		accept
+		log
+	}
+}
+EOF
+
+$NFT -f - <<EOF
+replace rule x y handle 2 log
+delete rule x y handle 3
+add rule x y index 0 drop
+EOF
tests/shell/testcases/transactions/dumps/0024rule_0.nft new file +8 -0
new file mode 100644
index 0000000000000..7860ff654c5e2
@@ -0,0 +1,8 @@
+table ip x {
+	chain y {
+		accept comment "rule1"
+		accept comment "rule2"
+		accept comment "rule3"
+		accept comment "rule4"
+	}
+}
tests/shell/testcases/transactions/dumps/0025rule_0.nft new file +6 -0
new file mode 100644
index 0000000000000..dcb61ae65fbde
@@ -0,0 +1,6 @@
+table ip x {
+	chain y {
+		log
+		drop
+	}
+}
Signature · 1 line
2.21.0
#6 lore raw
In reply to Phil Sutter
Quoted 11 lines · Phil Sutter

After Pablo's evaluation sequence rework, this series (formerly fixing cache updates as well) has shrunken considerably:

Patch 1 contains a proper fix for that workaround in evaluate_cache_add().

Patch 2 removes the cache-related workaround in tests/json_echo.

Patches 3 and 4 contain prerequisites for the last one, which actually implements the support for referencing rules of the same transation with 'index' keyword.

Series applied, thanks Phil.