Skip to contentSkip to conversation

Fix printing of range elements in named sets

PATCH In review Phil Sutter 2 participants 7 messages Jul 19, 2017 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

v4 cover Fix printing of range elements in named sets

This is v4 of 'nft monitor' fixes for range elements. Apart from changing the new flag's location in struct expr as requested, this series now also covers mappings.

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                         |  18 +++++
 src/netlink.c                          | 123 +++++++++++++++++++++++++++------
 src/segtree.c                          |   5 ++
 tests/monitor/run-tests.sh             |  78 +++++++++++++++++++++
 tests/monitor/testcases/set-maps.t     |  11 +++
 tests/monitor/testcases/set-mixed.t    |  19 +++++
 tests/monitor/testcases/set-multiple.t |  13 ++++
 tests/monitor/testcases/set-simple.t   |  47 +++++++++++++
 9 files changed, 294 insertions(+), 21 deletions(-)
 create mode 100755 tests/monitor/run-tests.sh
 create mode 100644 tests/monitor/testcases/set-maps.t
 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
author reviewer
#1 lore raw

PATCH v2 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).

---
Changes since v1:
- Introduce dedicated EXPR_SET_ELEM field 'elem_flags' to hold userdata flags.
- Drop now unused function parse_udata_cb().

---
 include/expression.h |  1 +
 include/rule.h       | 16 +++++++++++++++
 src/netlink.c        | 55 ++++++++++++++++++++++++++++++++--------------------
 src/segtree.c        |  5 +++++
 4 files changed, 56 insertions(+), 21 deletions(-)
include/expression.h +1 -0
index 68a36e8af792a..828dbaee63383 100644
@@ -256,6 +256,7 @@ struct expr {
 			uint64_t		expiration;
 			const char		*comment;
 			struct stmt		*stmt;
+			uint32_t		elem_flags;
 		};
 		struct {
 			/* EXPR_UNARY */
include/rule.h +16 -0
index ddad6d40470e4..a25e99bdf4cfd 100644
@@ -506,4 +506,20 @@ 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)
+
+/**
+ * enum udata_set_elem_flags - meaning of bits in UDATA_SET_ELEM_FLAGS
+ *
+ * @SET_ELEM_F_INTERVAL_OPEN:	set element denotes a half-open range
+ */
+enum udata_set_elem_flags {
+	SET_ELEM_F_INTERVAL_OPEN	= 0x1,
+};
+
 #endif /* NFTABLES_RULE_H */
src/netlink.c +34 -21

102 lines not on this page · raw message

src/segtree.c +5 -0
index f53536210018d..34a001613eaba 100644
@@ -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->elem_flags |= SET_ELEM_F_INTERVAL_OPEN;
 
 	compound_expr_add(set, expr);
 }
Signature · 1 line
2.13.1
author reviewer
#2 lore raw

PATCH v2 3/3 tests: Add basic monitor testing framework

In reply to Phil Sutter

This implements testing of 'nft monitor' output correctness and adds a number of testcases for named sets.

---
Changes since v1:
- Drop unused chain creation in testcases.
- Add testcase for mappings.

---
 tests/monitor/run-tests.sh             | 78 ++++++++++++++++++++++++++++++++++
 tests/monitor/testcases/set-maps.t     | 11 +++++
 tests/monitor/testcases/set-mixed.t    | 19 +++++++++
 tests/monitor/testcases/set-multiple.t | 13 ++++++
 tests/monitor/testcases/set-simple.t   | 47 ++++++++++++++++++++
 5 files changed, 168 insertions(+)
 create mode 100755 tests/monitor/run-tests.sh
 create mode 100644 tests/monitor/testcases/set-maps.t
 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
new file mode 100755
index 0000000000000..7447adf1febd6
@@ -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-maps.t new file +11 -0
new file mode 100644
index 0000000000000..d94016beb0767
@@ -0,0 +1,11 @@
+# first the setup
+I add table ip t
+O add table ip t
+I add map ip t portip { type inet_service: ipv4_addr; flags interval; }
+O add map ip t portip { type inet_service : ipv4_addr;flags interval }
+
+I add element ip t portip { 80-100: 10.0.0.1 }
+O add element ip t portip { 80-100 : 10.0.0.1 }
+
+I add element ip t portip { 1024-65535: 10.0.0.1 }
+O add element ip t portip { 1024-65535 : 10.0.0.1 }
tests/monitor/testcases/set-mixed.t new file +19 -0
new file mode 100644
index 0000000000000..c4699edacec11
@@ -0,0 +1,19 @@
+# first the setup
+I add table ip t
+O add table ip t
+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 +13 -0
new file mode 100644
index 0000000000000..d94f941b39693
@@ -0,0 +1,13 @@
+# first the setup
+I add table ip t
+O add table ip t
+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 +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.13.1
author reviewer
#3 lore raw

PATCH v3 2/3 monitor: Fix printing of range elements in named sets

In reply to Phil Sutter

From: 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.

Changes since v2:
- Adjust for changes to segtree patch.
- Fix printing of mappings (delinearizing looks at dummyset->flags to detect it's a mapping.
- Fix printing of mappings with half-open ranges (SET_ELEM_F_INTERVAL_OPEN occurs in elem->left, not elem itself).

---
 include/rule.h |  2 ++
 src/netlink.c  | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)
include/rule.h +2 -0
index a25e99bdf4cfd..6acd5fa810ef5 100644
@@ -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 +68 -0

92 lines not on this page · raw message

Signature · 1 line
2.13.1
#4 lore raw

Re: [nft PATCH v3 2/3] monitor: Fix printing of range elements in named sets

include/rule.h extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle); · line 220

Quoted 9 lines · Phil Sutter
include/rule.h +1 -0
index a25e99bdf4cfd..6acd5fa810ef5 100644
@@ -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)

Just a side note. This field is very much exclusive to event monitor printing, I would prefer this is cached somewhere else, away from the structure, if possible.

Thanks.

Quoted 11 lines
include/rule.h +1 -0
  * @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;
author reviewer
#5 lore raw

Re: [nft PATCH v3 2/3] monitor: Fix printing of range elements in named sets

Quoted 14 lines · Pablo Neira Ayuso

include/rule.h extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle); · line 220

Quoted 9 lines · Phil Sutter
include/rule.h +1 -0
index a25e99bdf4cfd..6acd5fa810ef5 100644
@@ -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)

Just a side note. This field is very much exclusive to event monitor printing, I would prefer this is cached somewhere else, away from the structure, if possible.

My solution was to make dummyset static and use it as a cache. Would that be acceptible? I went with Arturo's solution since you seemed to appreciate it and I didn't want to "steal" his credit. :)

Cheers, Phil

#6 lore raw
In reply to Phil Sutter
Quoted 3 lines · Phil Sutter

This is v4 of 'nft monitor' fixes for range elements. Apart from changing the new flag's location in struct expr as requested, this series now also covers mappings.

Series applied, thanks Phil!