rule: collapse set element commands
Pablo Neira Ayuso
maintainerPATCH 1/2 rule: collapse set element commands
Robots might generate a long list of singleton element commands such as:
add element t s { 1.0.1.0/24 }
...
add element t s { 1.0.2.0/23 }
collapse them into one single command, ie.
add element t s { 1.0.1.0/24, ..., 1.0.2.0/23 }
Since 3da9643fb9ff9 ("intervals: add support to automerge with kernel elements"), the new interval tracking relies on mergesort. The pattern above triggers the set sorting for each element.
--- include/rule.h | 1 + src/libnftables.c | 2 ++ src/rule.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+)
include/rule.h +1 -0
@@ -728,6 +728,7 @@ extern struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj, const struct handle *h, const struct location *loc, void *data);extern void nft_cmd_expand (struct cmd *cmd); +extern void nft_cmd_collapse (struct list_head *cmds);extern struct cmd *cmd_alloc_obj_ct (enum cmd_ops op,int type,const struct handle *h,const struct location *loc,struct obj *obj);
src/libnftables.c +2 -0
@@ -512,6 +512,8 @@ static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs,nft_cache_filter_fini (filter); +nft_cmd_collapse (cmds); +list_for_each_entry_safe (cmd, next, cmds, list) {struct eval_ctx ectx = { .nft = nft,
src/rule.c +39 -0
@@ -1379,6 +1379,45 @@ void nft_cmd_expand(struct cmd *cmd) } } +void nft_cmd_collapse (struct list_head *cmds) +{ +struct cmd *cmd, *next, *elems =NULL ; + +list_for_each_entry_safe (cmd, next, cmds, list) { +if (cmd->op !=CMD_ADD && + cmd->op !=CMD_CREATE ) { + elems =NULL ; +continue ; + } + +if (cmd->obj !=CMD_OBJ_ELEMENTS ) { + elems =NULL ; +continue ; + } + +if (!elems) { + elems = cmd; +continue ; + } + +if (cmd->op != elems->op) { + elems = cmd; +continue ; + } + +if (strcmp (elems->handle.table.name, cmd->handle.table.name) || +strcmp (elems->handle.set.name, cmd->handle.set.name)) { + elems = cmd; +continue ; + } + +list_splice_init (&cmd->expr->expressions, &elems->expr->expressions); + elems->expr->size += cmd->expr->size; +list_del (&cmd->list); +cmd_free (cmd); + } +} +struct markup *markup_alloc (uint32_t format) {struct markup *markup;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 intervals: do not report exact overlaps for new elements
Two new elements that represent an exact overlap should not trigger an error.
add table t
add set t s { type ipv4_addr; flags interval; } add element t s { 1.0.1.0/24 }
...
add element t s { 1.0.1.0/24 }
result in a bogus error.
# nft -f set.nft
set.nft:1002:19-28: Error: conflicting intervals specified
add element t s { 1.0.1.0/24 }
^^^^^^^^^^--- src/intervals.c | 3 +-- tests/shell/testcases/sets/exact_overlap_0 | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100755 tests/shell/testcases/sets/exact_overlap_0
src/intervals.c +1 -2
@@ -540,8 +540,7 @@ static int setelem_overlap(struct list_head *msgs, struct set *set, }if (mpz_cmp (prev_range.low, range.low) ==0 && -mpz_cmp (prev_range.high, range.high) ==0 && - (elem->flags &EXPR_F_KERNEL || prev->flags &EXPR_F_KERNEL )) +mpz_cmp (prev_range.high, range.high) ==0 ) goto next; if (mpz_cmp(prev_range.low, range.low) <= 0 &&
tests/shell/testcases/sets/exact_overlap_0 new file +22 -0
@@ -0,0 +1,22 @@ +#!/bin/bash + +RULESET="add table t +add set t s { type ipv4_addr; flags interval; } +add element t s { 1.0.1.0/24 } +add element t s { 1.0.2.0/23 } +add element t s { 1.0.8.0/21 } +add element t s { 1.0.32.0/19 } +add element t s { 1.1.0.0/24 } +add element t s { 1.1.2.0/23 } +add element t s { 1.1.4.0/22 } +add element t s { 1.1.8.0/24 } +add element t s { 1.1.9.0/24 } +add element t s { 1.1.10.0/23 } +add element t s { 1.1.12.0/22 } +add element t s { 1.1.16.0/20 } +add element t s { 1.1.32.0/19 } +add element t s { 1.0.1.0/24 }" + +$NFT -f - <<< $RULESET || exit 1 + +$NFT add element t s { 1.0.1.0/24 }
Signature · 1 line
2.30.2