Fix printing of range elements in named sets
Phil Sutter
reviewerv3 cover Fix printing of range elements in named sets
This is the third attempt at fixing 'nft monitor' for range elements. This time introduce a flag which allows to identify half-open ranges, then reuse Arturo's patch since it works nicely after a few adjustments. Finally resubmit my 'nft monitor' test framework without any changes to it's first version.
Arturo Borrero Gonzalez (1):
monitor: Fix printing of range elements in named sets
Phil Sutter (2):
segtree: Introduce flag for half-open range elements
tests: Add basic monitor testing framework
include/expression.h | 1 + include/rule.h | 9 +++ src/netlink.c | 121 ++++++++++++++++++++++++++++----- src/segtree.c | 5 ++ tests/monitor/run-tests.sh | 78 +++++++++++++++++++++ tests/monitor/testcases/set-mixed.t | 21 ++++++ tests/monitor/testcases/set-multiple.t | 15 ++++ tests/monitor/testcases/set-simple.t | 49 +++++++++++++ 8 files changed, 282 insertions(+), 17 deletions(-) create mode 100755 tests/monitor/run-tests.sh create mode 100644 tests/monitor/testcases/set-mixed.t create mode 100644 tests/monitor/testcases/set-multiple.t create mode 100644 tests/monitor/testcases/set-simple.t
Signature · 1 line
2.13.1
Phil Sutter
reviewerPATCH v3 1/3 segtree: Introduce flag for half-open range elements
This flag is required by userspace only, so can live within userdata. It's sole purpose is for 'nft monitor' to detect half-open ranges (which are comprised of a single element only).
--- include/expression.h | 1 + include/rule.h | 7 ++++++ src/netlink.c | 66 ++++++++++++++++++++++++++++++++++++++-------------- src/segtree.c | 5 ++++ 4 files changed, 62 insertions(+), 17 deletions(-)
include/expression.h +1 -0
@@ -180,6 +180,7 @@ enum expr_flags {EXPR_F_PROTOCOL =0x4 ,EXPR_F_INTERVAL_END =0x8 ,EXPR_F_BOOLEAN =0x10 , + EXPR_F_INTERVAL_OPEN = 0x20, };#include <payload.h>
include/rule.h +7 -0
@@ -503,4 +503,11 @@ enum udata_set_type { };#define UDATA_SET_MAX (__UDATA_SET_MAX - 1) +enum udata_set_elem_type { +UDATA_SET_ELEM_COMMENT , +UDATA_SET_ELEM_FLAGS , + __UDATA_SET_ELEM_MAX, +}; +#define UDATA_SET_ELEM_MAX (__UDATA_SET_ELEM_MAX - 1) +#endif /* NFTABLES_RULE_H */
src/netlink.c +49 -17
99 lines not on this page · raw message
src/segtree.c +5 -0
@@ -37,6 +37,7 @@ struct seg_tree {enum elementary_interval_flags {EI_F_INTERVAL_END =0x1 , +EI_F_INTERVAL_OPEN =0x2 , };/**
@@ -512,6 +513,8 @@ static void segtree_linearize(struct list_head *list, const struct set *set,mpz_bitmask (q, tree->keylen); nei =ei_alloc (p, q,NULL ,EI_F_INTERVAL_END );list_add_tail (&nei->list, list); + }else { + prev->flags |=EI_F_INTERVAL_OPEN ; }mpz_clear (p);
@@ -538,6 +541,8 @@ static void set_insert_interval(struct expr *set, struct seg_tree *tree,if (ei->flags &EI_F_INTERVAL_END ) expr->flags |=EXPR_F_INTERVAL_END ; +if (ei->flags &EI_F_INTERVAL_OPEN ) + expr->flags |=EXPR_F_INTERVAL_OPEN ;compound_expr_add (set, expr); }
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [nft PATCH 1/3] segtree: Introduce flag for half-open range elements
Hi Phil,
This series looks to good to me. Only one comment below.
Quoted 17 lines · Phil Sutter
This flag is required by userspace only, so can live within userdata. It's sole purpose is for 'nft monitor' to detect half-open ranges (which are comprised of a single element only).
Signed-off-by Phil Sutter <[email protected]>--- include/expression.h | 1 + include/rule.h | 7 ++++++ src/netlink.c | 66 ++++++++++++++++++++++++++++++++++++++-------------- src/segtree.c | 5 ++++ 4 files changed, 62 insertions(+), 17 deletions(-)include/expression.h +0
-0
include/expression.h enum expr_flags { · line 183
Quoted 4 lines
include/expression.h +1
-0@@ -180,6 +180,7 @@ enum expr_flags {EXPR_F_PROTOCOL =0x4 ,EXPR_F_INTERVAL_END =0x8 ,EXPR_F_BOOLEAN =0x10 , + EXPR_F_INTERVAL_OPEN = 0x20,
Could you place this here?
include/expression.h +1 -0
@@ -256,6 +256,7 @@ struct expr {uint64_t expiration;const char *comment;struct stmt *stmt; +unsigned int elem_flags; };struct {/* EXPR_UNARY */
We can probably move EXPR_F_INTERVAL_END there too in a follow up patch.
Quoted 41 lines
include/expression.h +0
-0};#include <payload.h> include/rule.h +7
-0@@ -503,4 +503,11 @@ enum udata_set_type { };#define UDATA_SET_MAX (__UDATA_SET_MAX - 1) +enum udata_set_elem_type { +UDATA_SET_ELEM_COMMENT , +UDATA_SET_ELEM_FLAGS , + __UDATA_SET_ELEM_MAX, +}; +#define UDATA_SET_ELEM_MAX (__UDATA_SET_ELEM_MAX - 1) +#endif /* NFTABLES_RULE_H */ src/netlink.c +2
-2@@ -211,7 +211,7 @@ static struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,const struct expr *elem, *key, *data;struct nftnl_set_elem *nlse;struct nft_data_linearize nld; -struct nftnl_udata_buf *udbuf; +struct nftnl_udata_buf *udbuf =NULL ; nlse =nftnl_set_elem_alloc ();if (nlse ==NULL )@@ -232,13 +232,22 @@ static struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,if (elem->timeout)nftnl_set_elem_set_u64 (nlse,NFTNL_SET_ELEM_TIMEOUT , elem->timeout); -if (elem->comment) { +if (elem->comment || expr->flags &EXPR_F_INTERVAL_OPEN ) { udbuf =nftnl_udata_buf_alloc (NFT_USERDATA_MAXLEN );if (!udbuf)memory_allocation_error ();
src/netlink.c static struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set, · line 247
Quoted 10 lines
src/netlink.c +7
-1-if (!nftnl_udata_put_strz (udbuf,UDATA_TYPE_COMMENT , + } +if (elem->comment) { +if (!nftnl_udata_put_strz (udbuf,UDATA_SET_ELEM_COMMENT , elem->comment))memory_allocation_error (); + } +if (expr->flags &EXPR_F_INTERVAL_OPEN ) { +if (!nftnl_udata_put_u32 (udbuf,UDATA_SET_ELEM_FLAGS , +EXPR_F_INTERVAL_OPEN ))
Better make a new definition for this? Instead of using EXPR_F_INTERVAL_OPEN which is 0x20.
Once we expose this value, we cannot change it otherwise we may break nft between software updates, we should avoid this.
Phil Sutter
reviewerPATCH v2 2/3 monitor: Fix printing of range elements in named sets
In reply to Phil SutterFrom: Arturo Borrero Gonzalez <[email protected]>
If you add set elements to interval sets, the output is wrong. Fix this by caching first element of the range (first event), then wait for the second element of the range (second event) to print them both at the same time.
We also avoid printing the first null element required in the RB tree.
Before this patch:
% nft add element t s {10-20, 30-40}
add element ip t s { 0 }
add element ip t s { 10 }
add element ip t s { ftp }
add element ip t s { 30 }
add element ip t s { 41 }
After this patch:
% nft add element t s {10-20, 30-40}
add element ip t s { 10-20 }
add element ip t s { 30-40 }
---
Changes since v1:
- Rewrite netlink_event_range_cache() so it correctly handles half-open ranges.
- Enable elem caching for NFT_MSG_DELSETELEM events also.
- Drop call to expr_free() for set->rg_cache since that will be done when freeing dummyset.
--- include/rule.h | 2 ++ src/netlink.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)
include/rule.h +2 -0
@@ -217,6 +217,7 @@ extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle);* @datalen: mapping data len * @objtype: mapping object type * @init: initializer +* @rg_cache: cached range element (left) * @policy: set mechanism policy * @desc: set mechanism desc */
@@ -234,6 +235,7 @@ struct set {unsigned int datalen;uint32_t objtype;struct expr *init; +struct expr *rg_cache;uint32_t policy;struct {uint32_t size;
src/netlink.c +55 -0
73 lines not on this page · raw message
Signature · 1 line
2.13.1
Phil Sutter
reviewerPATCH v3 3/3 tests: Add basic monitor testing framework
In reply to Phil SutterThis implements testing of 'nft monitor' output correctness and adds a number of testcases for named sets.
--- tests/monitor/run-tests.sh | 78 ++++++++++++++++++++++++++++++++++ tests/monitor/testcases/set-mixed.t | 21 +++++++++ tests/monitor/testcases/set-multiple.t | 15 +++++++ tests/monitor/testcases/set-simple.t | 49 +++++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100755 tests/monitor/run-tests.sh create mode 100644 tests/monitor/testcases/set-mixed.t create mode 100644 tests/monitor/testcases/set-multiple.t create mode 100644 tests/monitor/testcases/set-simple.t
tests/monitor/run-tests.sh new file +78 -0
@@ -0,0 +1,78 @@ +#!/bin/bash + +cd $(dirname $0) + +testdir=$(mktemp -d) +if [ ! -d $testdir ]; then + echo "Failed to create test directory" >&2 + exit 0 +fi +trap "rm -rf $testdir" EXIT + +nft=../../src/nft +command_file=$(mktemp -p $testdir) +output_file=$(mktemp -p $testdir) + +cmd_append() { + echo "$*" >>$command_file +} +output_append() { + echo "$*" >>$output_file +} +run_test() { + monitor_output=$(mktemp -p $testdir) + $nft monitor >$monitor_output & + monitor_pid=$! + + sleep 0.5 + + $nft -f $command_file || { + echo "nft command failed!" + kill $monitor_pid + wait >/dev/null 2>&1 + exit 1 + } + sleep 0.5 + kill $monitor_pid + wait >/dev/null 2>&1 + if ! diff -Z -q $monitor_output $output_file >/dev/null 2>&1; then + echo "monitor output differs!" + diff -Z -u $output_file $monitor_output + exit 1 + fi + rm $command_file + rm $output_file + touch $command_file + touch $output_file +} + +for testcase in testcases/*.t; do + echo "running tests from file $(basename $testcase)" + # files are like this: + # + # I add table ip t + # O add table ip t + # I add chain ip t c + # O add chain ip t c + + $nft flush ruleset + + input_complete=false + while read dir line; do + case $dir in + I) + $input_complete && run_test + input_complete=false + cmd_append "$line" + ;; + O) + input_complete=true + output_append "$line" + ;; + '#'|'') + # ignore comments and empty lines + ;; + esac + done <$testcase + $input_complete && run_test +done
tests/monitor/testcases/set-mixed.t new file +21 -0
@@ -0,0 +1,21 @@ +# first the setup +I add table ip t +O add table ip t +I add chain ip t c +O add chain ip t c +I add set ip t portrange { type inet_service; flags interval; } +O add set ip t portrange { type inet_service;flags interval } +I add set ip t ports { type inet_service; } +O add set ip t ports { type inet_service;} + +# make sure concurrent adds work +I add element ip t portrange { 1024-65535 } +I add element ip t ports { 10 } +O add element ip t portrange { 1024-65535 } +O add element ip t ports { 10 } + +# delete items again +I delete element ip t portrange { 1024-65535 } +I delete element ip t ports { 10 } +O delete element ip t portrange { 1024-65535 } +O delete element ip t ports { 10 }
tests/monitor/testcases/set-multiple.t new file +15 -0
@@ -0,0 +1,15 @@ +# first the setup +I add table ip t +O add table ip t +I add chain ip t c +O add chain ip t c +I add set ip t portrange { type inet_service; flags interval; } +O add set ip t portrange { type inet_service;flags interval } +I add set ip t portrange2 { type inet_service; flags interval; } +O add set ip t portrange2 { type inet_service;flags interval } + +# make sure concurrent adds work +I add element ip t portrange { 1024-65535 } +I add element ip t portrange2 { 10-20 } +O add element ip t portrange { 1024-65535 } +O add element ip t portrange2 { 10-20 }
tests/monitor/testcases/set-simple.t new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.13.1