Skip to contentSkip to conversation

src: print set element with multi-word description in single one line

PATCH In review Pablo Neira Ayuso 2 participants 2 messages Mar 13, 2025 – Mar 18, 2025

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: print set element with multi-word description in single one line

If the set element:

- represents a mapping
- has a timeout
- has a comment
- has counter/quota/linux
- concatenation (already printed in a single line before this patch)

ie. if the set element requires several words, then print it in one single line.

Before this patch:

 table ip x {
      set y {
            typeof ip saddr
            counter
            elements = { 192.168.10.35 counter packets 0 bytes 0, 192.168.10.101 counter packets 0 bytes 0,
                         192.168.10.135 counter packets 0 bytes 0 }
      }

}

After this patch:

 table ip x {
      set y {
            typeof ip saddr
            counter
            elements = { 192.168.10.35 counter packets 0 bytes 0,
			 192.168.10.101 counter packets 0 bytes 0,
                         192.168.10.135 counter packets 0 bytes 0 }
      }

}

---
 include/nftables.h                            |    1 +
 include/rule.h                                |    2 +
 src/expression.c                              |    8 +-
 src/netlink.c                                 |    5 +-
 src/rule.c                                    |    9 +
 .../maps/0003map_add_many_elements_0          |    9 +-
 .../maps/0004interval_map_create_once_0       |    9 +-
 .../testcases/maps/0008interval_map_delete_0  |    3 +-
 .../dumps/0003map_add_many_elements_0.nft     | 1440 +++++++++++------
 .../0005interval_map_add_many_elements_0.nft  |    6 +-
 .../maps/dumps/0006interval_map_overlap_0.nft |    3 +-
 .../maps/dumps/0008interval_map_delete_0.nft  |    3 +-
 .../shell/testcases/maps/dumps/0011vmap_0.nft |    3 +-
 .../maps/dumps/0017_map_variable_0.nft        |    6 +-
 .../maps/dumps/0024named_objects_0.nft        |    3 +-
 .../testcases/maps/dumps/typeof_maps_0.nft    |    3 +-
 .../maps/dumps/vmap_mark_bitwise_0.nft        |    3 +-
 tests/shell/testcases/maps/typeof_maps_0      |    3 +-
 .../testcases/sets/dumps/0024synproxy_0.nft   |    3 +-
 .../shell/testcases/sets/dumps/0047nat_0.nft  |    3 +-
 .../sets/dumps/0048set_counters_0.nft         |    3 +-
 .../sets/dumps/0060set_multistmt_0.nft        |    3 +-
 .../sets/dumps/0060set_multistmt_1.nft        |    3 +-
 .../sets/dumps/0063set_catchall_0.nft         |    6 +-
 .../sets/dumps/0064map_catchall_0.nft         |    6 +-
 .../sets/dumps/0067nat_interval_0.nft         |    3 +-
 .../transactions/dumps/0047set_0.nft          |   15 +-
 27 files changed, 1038 insertions(+), 526 deletions(-)
include/nftables.h +1 -0
index 2e0d91486a29..c058667c03fa 100644
@@ -38,6 +38,7 @@ static inline bool nft_input_json(const struct input_ctx *ictx)
 
 struct output_ctx {
 	unsigned int flags;
+	bool force_newline;
 	union {
 		FILE *output_fp;
 		struct cookie output_cookie;
include/rule.h +2 -0
index 86477c709544..85a0d9c0b524 100644
@@ -332,6 +332,7 @@ void rule_stmt_insert_at(struct rule *rule, struct stmt *nstmt,
  * @automerge:	merge adjacents and overlapping elements, if possible
  * @comment:	comment
  * @errors:	expr evaluation errors seen
+ * @elem_has_comment: element with comment seen (for printing)
  * @desc.size:		count of set elements
  * @desc.field_len:	length of single concatenated fields, bytes
  * @desc.field_count:	count of concatenated fields
@@ -357,6 +358,7 @@ struct set {
 	bool			automerge;
 	bool			key_typeof_valid;
 	bool			errors;
+	bool			elem_has_comment;
 	const char		*comment;
 	struct {
 		uint32_t	size;
src/expression.c +6 -2
index 8a90e09dd1c5..413f446772bb 100644
@@ -1252,11 +1252,15 @@ struct expr *list_expr_alloc(const struct location *loc)
 	return compound_expr_alloc(loc, EXPR_LIST);
 }
 
-static const char *calculate_delim(const struct expr *expr, int *count)
+static const char *calculate_delim(const struct expr *expr, int *count,
+				   struct output_ctx *octx)
 {
 	const char *newline = ",\n\t\t\t     ";
 	const char *singleline = ", ";
 
+	if (octx->force_newline)
+		return newline;
+
 	if (set_is_anonymous(expr->set_flags))
 		return singleline;
 
@@ -1309,7 +1313,7 @@ static void set_expr_print(const struct expr *expr, struct output_ctx *octx)
 		nft_print(octx, "%s", d);
 		expr_print(i, octx);
 		count++;
-		d = calculate_delim(expr, &count);
+		d = calculate_delim(expr, &count, octx);
 	}
 
 	nft_print(octx, " }");
src/netlink.c +4 -1
index 8e6e2066fe2a..8d7377e4e86b 100644
@@ -1435,8 +1435,11 @@ key_end:
 
 	if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_EXPIRATION))
 		expr->expiration = nftnl_set_elem_get_u64(nlse, NFTNL_SET_ELEM_EXPIRATION);
-	if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_USERDATA))
+	if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_USERDATA)) {
 		set_elem_parse_udata(nlse, expr);
+		if (expr->comment)
+			set->elem_has_comment = true;
+	}
 	if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_EXPR)) {
 		const struct nftnl_expr *nle;
 		struct stmt *stmt;
src/rule.c +9 -0
index f75829140b8c..9c317934139c 100644
@@ -420,7 +420,16 @@ static void do_set_print(const struct set *set, struct print_fmt_options *opts,
 
 	if (set->init != NULL && set->init->size > 0) {
 		nft_print(octx, "%s%selements = ", opts->tab, opts->tab);
+
+		if (set->timeout || set->elem_has_comment ||
+		    (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT |
+				   NFT_SET_TIMEOUT | NFT_SET_CONCAT)) ||
+		    !list_empty(&set->stmt_list))
+			octx->force_newline = true;
+
 		expr_print(set->init, octx);
+		octx->force_newline = false;
+
 		nft_print(octx, "%s", opts->nl);
 	}
 	nft_print(octx, "%s}%s", opts->tab, opts->nl);
tests/shell/testcases/maps/0003map_add_many_elements_0 +1 -8
index 2b254c51b3c3..427d94dfbfbe 100755
@@ -24,19 +24,12 @@ generate_add() {
 }
 
 generate_test() {
-	count=0
 	elements=""
 	for ((i=1; i<=HOWMANY; i++)) ; do
 		for ((j=1; j<=HOWMANY; j++)) ; do
-			((count++))
 			elements="${elements}10.0.${i}.${j} : 10.0.${i}.${j}"
 			[ "$i" == "$HOWMANY" ] && [ "$j" == "$HOWMANY" ] && break
-			if [ "$count" == "2" ] ; then
-				count=0
-				elements="${elements},\\n\\t\\t\\t     "
-			else
-				elements="${elements}, "
-			fi
+			elements="${elements},\\n\\t\\t\\t     "
 		done
 	done
 	echo -e "$elements"
tests/shell/testcases/maps/0004interval_map_create_once_0 +1 -8
index 64f434ad6b00..7d3825596c49 100755
@@ -30,19 +30,12 @@ generate_add() {
 }
 
 generate_test() {
-	count=0
 	elements=""
 	for ((i=1; i<=HOWMANY; i++)) ; do
 		for ((j=1; j<=HOWMANY; j++)) ; do
-			((count++))
 			elements="${elements}10.${i}.${j}.0/24 : 10.0.${i}.${j}"
 			[ "$i" == "$HOWMANY" ] && [ "$j" == "$HOWMANY" ] && break
-			if [ "$count" == "2" ] ; then
-				count=0
-				elements="${elements},\\n\\t\\t\\t     "
-			else
-				elements="${elements}, "
-			fi
+			elements="${elements},\\n\\t\\t\\t     "
 		done
 	done
 	echo -e "$elements"
tests/shell/testcases/maps/0008interval_map_delete_0 +2 -1
index 39ea3127be89..86e54b68ecd4 100755
@@ -6,7 +6,8 @@ EXPECTED="table ip filter {
 	map m {
 		type ipv4_addr : mark
 		flags interval
-		elements = { 127.0.0.2 : 0x00000002, 127.0.0.3 : 0x00000003 }
+		elements = { 127.0.0.2 : 0x00000002,
+			     127.0.0.3 : 0x00000003 }
 	}
 
 	chain input {
tests/shell/testcases/maps/dumps/0003map_add_many_elements_0.nft +960 -480

1,446 lines not on this page · raw message

tests/shell/testcases/maps/dumps/0005interval_map_add_many_elements_0.nft +4 -2
index ab992c4a0618..b1e017bdcae7 100644
@@ -2,7 +2,9 @@ table ip x {
 	map y {
 		type ipv4_addr : ipv4_addr
 		flags interval
-		elements = { 10.1.1.0/24 : 10.0.1.1, 10.1.2.0/24 : 10.0.1.2,
-			     10.2.1.0/24 : 10.0.2.1, 10.2.2.0/24 : 10.0.2.2 }
+		elements = { 10.1.1.0/24 : 10.0.1.1,
+			     10.1.2.0/24 : 10.0.1.2,
+			     10.2.1.0/24 : 10.0.2.1,
+			     10.2.2.0/24 : 10.0.2.2 }
 	}
 }
tests/shell/testcases/maps/dumps/0006interval_map_overlap_0.nft +2 -1
index 1f5343f4fada..74380c29490b 100644
@@ -2,6 +2,7 @@ table ip x {
 	map y {
 		type ipv4_addr : ipv4_addr
 		flags interval
-		elements = { 10.0.1.0/24 : 10.0.0.1, 10.0.2.0/24 : 10.0.0.2 }
+		elements = { 10.0.1.0/24 : 10.0.0.1,
+			     10.0.2.0/24 : 10.0.0.2 }
 	}
 }
tests/shell/testcases/maps/dumps/0008interval_map_delete_0.nft +2 -1
index a470a340655f..1b199ff2d688 100644
@@ -2,7 +2,8 @@ table ip filter {
 	map m {
 		type ipv4_addr : mark
 		flags interval
-		elements = { 127.0.0.2 : 0x00000002, 127.0.0.3 : 0x00000003 }
+		elements = { 127.0.0.2 : 0x00000002,
+			     127.0.0.3 : 0x00000003 }
 	}
 
 	chain input {
tests/shell/testcases/maps/dumps/0011vmap_0.nft +2 -1
index 4a72b5e7ea38..94b85a61813f 100644
@@ -2,7 +2,8 @@ table inet filter {
 	map portmap {
 		type inet_service : verdict
 		counter
-		elements = { 22 counter packets 0 bytes 0 : jump ssh_input, * counter packets 0 bytes 0 : drop }
+		elements = { 22 counter packets 0 bytes 0 : jump ssh_input,
+			     * counter packets 0 bytes 0 : drop }
 	}
 
 	chain ssh_input {
tests/shell/testcases/maps/dumps/0017_map_variable_0.nft +4 -2
index 796dd729566f..f6d7f6a4f3b9 100644
@@ -1,11 +1,13 @@
 table ip x {
 	map y {
 		typeof ip saddr : meta mark
-		elements = { 1.1.1.1 : 0x00000002, * : 0x00000003 }
+		elements = { 1.1.1.1 : 0x00000002,
+			     * : 0x00000003 }
 	}
 
 	map z {
 		typeof ip saddr : meta mark
-		elements = { 1.1.1.1 : 0x00000002, * : 0x00000003 }
+		elements = { 1.1.1.1 : 0x00000002,
+			     * : 0x00000003 }
 	}
 }
tests/shell/testcases/maps/dumps/0024named_objects_0.nft +2 -1
index 2ffa4f2ff757..3188ce2a53a3 100644
@@ -21,7 +21,8 @@ table inet x {
 
 	map test {
 		type ipv4_addr : quota
-		elements = { 192.168.2.2 : "user124", 192.168.2.3 : "user124" }
+		elements = { 192.168.2.2 : "user124",
+			     192.168.2.3 : "user124" }
 	}
 
 	chain y {
tests/shell/testcases/maps/dumps/typeof_maps_0.nft +2 -1
index a5c0a60927a7..e0efabab3b55 100644
@@ -6,7 +6,8 @@ table inet t {
 
 	map m2 {
 		typeof vlan id : meta mark
-		elements = { 1 : 0x00000001, 4095 : 0x00004095 }
+		elements = { 1 : 0x00000001,
+			     4095 : 0x00004095 }
 	}
 
 	map m3 {
tests/shell/testcases/maps/dumps/vmap_mark_bitwise_0.nft +2 -1
index beb5ffb098f5..6891e8611081 100644
@@ -5,7 +5,8 @@ table ip x {
 
 	map sctm_o0 {
 		type mark : verdict
-		elements = { 0x00000000 : jump sctm_o0_0, 0x00000001 : jump sctm_o0_1 }
+		elements = { 0x00000000 : jump sctm_o0_0,
+			     0x00000001 : jump sctm_o0_1 }
 	}
 
 	map sctm_o1 {
tests/shell/testcases/maps/typeof_maps_0 +2 -1
index 764206d26dc1..8f629c5dadf4 100755
@@ -64,7 +64,8 @@ EXPECTED="table inet t {
 
 	map m2 {
 		typeof vlan id : meta mark
-		elements = { 1 : 0x00000001, 4095 : 0x00004095 }
+		elements = { 1 : 0x00000001,
+			     4095 : 0x00004095 }
 	}
 
 	map m3 {
tests/shell/testcases/sets/dumps/0024synproxy_0.nft +2 -1
index e0ee86db2217..dd9a112a5efe 100644
@@ -13,7 +13,8 @@ table inet x {
 	map test2 {
 		type ipv4_addr : synproxy
 		flags interval
-		elements = { 192.168.1.0/24 : "https-synproxy", 192.168.2.0/24 : "other-synproxy" }
+		elements = { 192.168.1.0/24 : "https-synproxy",
+			     192.168.2.0/24 : "other-synproxy" }
 	}
 
 	chain y {
tests/shell/testcases/sets/dumps/0047nat_0.nft +2 -1
index 9fa9fc7456c5..86dbb70880ff 100644
@@ -2,7 +2,8 @@ table ip x {
 	map y {
 		type ipv4_addr : interval ipv4_addr
 		flags interval
-		elements = { 10.141.10.0/24 : 192.168.2.2-192.168.2.4, 10.141.11.0/24 : 192.168.4.2/31,
+		elements = { 10.141.10.0/24 : 192.168.2.2-192.168.2.4,
+			     10.141.11.0/24 : 192.168.4.2/31,
 			     10.141.12.0/24 : 192.168.5.10-192.168.5.20 }
 	}
 
tests/shell/testcases/sets/dumps/0048set_counters_0.nft +2 -1
index 2145f6b11b88..d6247868e9f0 100644
@@ -2,7 +2,8 @@ table ip x {
 	set y {
 		typeof ip saddr
 		counter
-		elements = { 192.168.10.35 counter packets 0 bytes 0, 192.168.10.101 counter packets 0 bytes 0,
+		elements = { 192.168.10.35 counter packets 0 bytes 0,
+			     192.168.10.101 counter packets 0 bytes 0,
 			     192.168.10.135 counter packets 0 bytes 0 }
 	}
 
tests/shell/testcases/sets/dumps/0060set_multistmt_0.nft +2 -1
index df68fcdf89e6..8521e3f7c176 100644
@@ -2,7 +2,8 @@ table ip x {
 	set y {
 		type ipv4_addr
 		limit rate 1/second burst 5 packets counter
-		elements = { 1.1.1.1 limit rate 1/second burst 5 packets counter packets 0 bytes 0, 4.4.4.4 limit rate 1/second burst 5 packets counter packets 0 bytes 0,
+		elements = { 1.1.1.1 limit rate 1/second burst 5 packets counter packets 0 bytes 0,
+			     4.4.4.4 limit rate 1/second burst 5 packets counter packets 0 bytes 0,
 			     5.5.5.5 limit rate 1/second burst 5 packets counter packets 0 bytes 0 }
 	}
 
tests/shell/testcases/sets/dumps/0060set_multistmt_1.nft +2 -1
index ac1bd26b3e58..befc2f75bd42 100644
@@ -4,7 +4,8 @@ table ip x {
 		size 65535
 		flags dynamic
 		counter quota 500 bytes
-		elements = { 1.1.1.1 counter packets 0 bytes 0 quota 500 bytes, 1.2.3.4 counter packets 9 bytes 756 quota 500 bytes used 500 bytes,
+		elements = { 1.1.1.1 counter packets 0 bytes 0 quota 500 bytes,
+			     1.2.3.4 counter packets 9 bytes 756 quota 500 bytes used 500 bytes,
 			     2.2.2.2 counter packets 0 bytes 0 quota 1000 bytes }
 	}
 
tests/shell/testcases/sets/dumps/0063set_catchall_0.nft +4 -2
index f0d42cc24e60..faa984bd6a1d 100644
@@ -2,13 +2,15 @@ table ip x {
 	set y {
 		type ipv4_addr
 		counter
-		elements = { 1.1.1.1 counter packets 0 bytes 0, * counter packets 0 bytes 0 }
+		elements = { 1.1.1.1 counter packets 0 bytes 0,
+			     * counter packets 0 bytes 0 }
 	}
 
 	set z {
 		type ipv4_addr
 		flags interval
 		counter
-		elements = { 1.1.1.0/24 counter packets 0 bytes 0, * counter packets 0 bytes 0 }
+		elements = { 1.1.1.0/24 counter packets 0 bytes 0,
+			     * counter packets 0 bytes 0 }
 	}
 }
tests/shell/testcases/sets/dumps/0064map_catchall_0.nft +4 -2
index 890ed2aab080..a1bba842844c 100644
@@ -1,13 +1,15 @@
 table ip x {
 	map y {
 		type ipv4_addr : ipv4_addr
-		elements = { 10.141.0.1 : 192.168.0.2, * : 192.168.0.4 }
+		elements = { 10.141.0.1 : 192.168.0.2,
+			     * : 192.168.0.4 }
 	}
 
 	map z {
 		type ipv4_addr : ipv4_addr
 		flags interval
-		elements = { 10.141.0.0/24 : 192.168.0.2, * : 192.168.0.3 }
+		elements = { 10.141.0.0/24 : 192.168.0.2,
+			     * : 192.168.0.3 }
 	}
 
 	chain y {
tests/shell/testcases/sets/dumps/0067nat_interval_0.nft +2 -1
index b6d07fcdc248..3e1584a8c5dc 100644
@@ -2,7 +2,8 @@ table ip nat {
 	map ipportmap {
 		type ipv4_addr : interval ipv4_addr . inet_service
 		flags interval
-		elements = { 192.168.1.2 : 10.141.10.1-10.141.10.3 . 8888-8999, 192.168.2.0/24 : 10.141.11.5-10.141.11.20 . 8888-8999 }
+		elements = { 192.168.1.2 : 10.141.10.1-10.141.10.3 . 8888-8999,
+			     192.168.2.0/24 : 10.141.11.5-10.141.11.20 . 8888-8999 }
 	}
 
 	chain prerouting {
tests/shell/testcases/transactions/dumps/0047set_0.nft +10 -5
index 4da397b28072..d8e8e38a0dd8 100644
@@ -2,10 +2,15 @@ table ip filter {
 	map group_10060 {
 		type ipv4_addr : classid
 		flags interval
-		elements = { 10.1.26.2 : 1:bbf8, 10.1.26.3 : 1:c1ad,
-			     10.1.26.4 : 1:b2d7, 10.1.26.5 : 1:f705,
-			     10.1.26.6 : 1:b895, 10.1.26.7 : 1:ec4c,
-			     10.1.26.8 : 1:de78, 10.1.26.9 : 1:b4f3,
-			     10.1.26.10 : 1:dec6, 10.1.26.11 : 1:b4c0 }
+		elements = { 10.1.26.2 : 1:bbf8,
+			     10.1.26.3 : 1:c1ad,
+			     10.1.26.4 : 1:b2d7,
+			     10.1.26.5 : 1:f705,
+			     10.1.26.6 : 1:b895,
+			     10.1.26.7 : 1:ec4c,
+			     10.1.26.8 : 1:de78,
+			     10.1.26.9 : 1:b4f3,
+			     10.1.26.10 : 1:dec6,
+			     10.1.26.11 : 1:b4c0 }
 	}
 }
Signature · 1 line
2.30.2
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Pablo Neira Ayuso

If the set element:

- represents a mapping
- has a timeout
- has a comment
- has counter/quota/linux
- concatenation (already printed in a single line before this patch)

Formatting is much saner after this, thanks for changing this.