Skip to contentSkip to conversation

A few cosmetic changes

PATCH In review Phil Sutter 1 participant 7 messages Mar 8, 2021 – Mar 9, 2021 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover A few cosmetic changes

This is fallout from the sheer endless mini-project of fixing byteorder in debug output.

This series deals with minor nuisances I found:

* Dead code in socket, tunnel and xfrm expressions
* Trailing spaces in debug output for rules
* Ugly indenting due to long function nftnl_data_reg_value_snprintf_default()

Phil Sutter (5):
expr/socket: Kill dead code
expr/tunnel: Kill dead code
expr/xfrm: Kill dead code
rule: Avoid printing trailing spaces
expr: data_reg: Reduce indenting level a bit

 src/expr/data_reg.c | 31 ++++++++-----------------------
 src/expr/socket.c   | 13 -------------
 src/expr/tunnel.c   | 13 -------------
 src/expr/xfrm.c     | 28 ----------------------------
 src/rule.c          | 27 ++++++++++++++++++---------
 5 files changed, 26 insertions(+), 86 deletions(-)
Signature · 1 line
2.30.1
author reviewer
#1 lore raw

PATCH 1/5 expr/socket: Kill dead code

Function str2socket_key() was never used.

Fixes 038d226f2e6cc ("src: Add support for native socket matching")
---
 src/expr/socket.c | 13 -------------
 1 file changed, 13 deletions(-)
src/expr/socket.c +0 -13
index 8cd4536a7a8e6..76fc90346141e 100644
@@ -126,19 +126,6 @@ static const char *socket_key2str(uint8_t key)
 	return "unknown";
 }
 
-static inline int str2socket_key(const char *str)
-{
-	int i;
-
-	for (i = 0; i < NFT_SOCKET_MAX + 1; i++) {
-		if (strcmp(str, socket_key2str_array[i]) == 0)
-			return i;
-	}
-
-	errno = EINVAL;
-	return -1;
-}
-
 static int
 nftnl_expr_socket_snprintf_default(char *buf, size_t len,
 			       const struct nftnl_expr *e)
Signature · 1 line
2.30.1
author reviewer
#2 lore raw

PATCH 2/5 expr/tunnel: Kill dead code

In reply to Phil Sutter

Function str2tunnel_key() was never used.

Fixes 42468fb6df61a ("expr: add support for matching tunnel metadata")
---
 src/expr/tunnel.c | 13 -------------
 1 file changed, 13 deletions(-)
src/expr/tunnel.c +0 -13
index b2b8d7243a26c..62e164805fee6 100644
@@ -124,19 +124,6 @@ static const char *tunnel_key2str(uint8_t key)
 	return "unknown";
 }
 
-static inline int str2tunnel_key(const char *str)
-{
-	int i;
-
-	for (i = 0; i <= NFT_TUNNEL_MAX; i++) {
-		if (strcmp(str, tunnel_key2str_array[i]) == 0)
-			return i;
-	}
-
-	errno = EINVAL;
-	return -1;
-}
-
 static int
 nftnl_expr_tunnel_snprintf_default(char *buf, size_t len,
 				 const struct nftnl_expr *e)
Signature · 1 line
2.30.1
author reviewer
#3 lore raw

PATCH 3/5 expr/xfrm: Kill dead code

In reply to Phil Sutter

These functions were used by removed JSON parser only.

Fixes 80077787f8f21 ("src: remove json support")
---
 src/expr/xfrm.c | 28 ----------------------------
 1 file changed, 28 deletions(-)
src/expr/xfrm.c +0 -28
index 8fe5438bd792a..0502b53bb7edc 100644
@@ -171,34 +171,6 @@ static const char *xfrmdir2str(uint8_t dir)
 	return xfrmdir2str_array[dir];
 }
 
-#ifdef JSON_PARSING
-static uint32_t str2xfrmkey(const char *s)
-{
-	int i;
-
-	for (i = 0;
-	     i < sizeof(xfrmkey2str_array) / sizeof(xfrmkey2str_array[0]);
-	     i++) {
-		if (strcmp(xfrmkey2str_array[i], s) == 0)
-			return i;
-	}
-	return -1;
-}
-
-static int str2xfmrdir(const char *s)
-{
-	int i;
-
-	for (i = 0;
-	     i <  sizeof(xfrmdir2str_array) / sizeof(xfrmdir2str_array[0]);
-	     i++) {
-		if (strcmp(xfrmkey2str_array[i], s) == 0)
-			return i;
-	}
-	return -1;
-}
-#endif
-
 static int
 nftnl_expr_xfrm_snprintf_default(char *buf, size_t size,
 			       const struct nftnl_expr *e)
Signature · 1 line
2.30.1
author reviewer
#4 lore raw

PATCH 4/5 rule: Avoid printing trailing spaces

In reply to Phil Sutter

Introduce 'sep' variable to track whether something was printed already. While being at it, introduce PRIu64 for 'handle' and 'position' attributes.

---
 src/rule.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
src/rule.c +18 -9
index 480afc8ffc1b8..e82cf73e9bbe5 100644
@@ -551,44 +551,53 @@ static int nftnl_rule_snprintf_default(char *buf, size_t size,
 {
 	struct nftnl_expr *expr;
 	int ret, remain = size, offset = 0, i;
+	const char *sep = "";
 
 	if (r->flags & (1 << NFTNL_RULE_FAMILY)) {
-		ret = snprintf(buf + offset, remain, "%s ",
+		ret = snprintf(buf + offset, remain, "%s%s", sep,
 			       nftnl_family2str(r->family));
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+		sep = " ";
 	}
 
 	if (r->flags & (1 << NFTNL_RULE_TABLE)) {
-		ret = snprintf(buf + offset, remain, "%s ",
+		ret = snprintf(buf + offset, remain, "%s%s", sep,
 			       r->table);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+		sep = " ";
 	}
 
 	if (r->flags & (1 << NFTNL_RULE_CHAIN)) {
-		ret = snprintf(buf + offset, remain, "%s ",
+		ret = snprintf(buf + offset, remain, "%s%s", sep,
 			       r->chain);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+		sep = " ";
 	}
 	if (r->flags & (1 << NFTNL_RULE_HANDLE)) {
-		ret = snprintf(buf + offset, remain, "%llu ",
-			       (unsigned long long)r->handle);
+		ret = snprintf(buf + offset, remain, "%s%" PRIu64, sep,
+			       r->handle);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+		sep = " ";
 	}
 
 	if (r->flags & (1 << NFTNL_RULE_POSITION)) {
-		ret = snprintf(buf + offset, remain, "%llu ",
-			       (unsigned long long)r->position);
+		ret = snprintf(buf + offset, remain, "%s%" PRIu64, sep,
+			       r->position);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+		sep = " ";
 	}
 
 	if (r->flags & (1 << NFTNL_RULE_ID)) {
-		ret = snprintf(buf + offset, remain, "%u ", r->id);
+		ret = snprintf(buf + offset, remain, "%s%u", sep, r->id);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+		sep = " ";
 	}
 
 	if (r->flags & (1 << NFTNL_RULE_POSITION_ID)) {
-		ret = snprintf(buf + offset, remain, "%u ", r->position_id);
+		ret = snprintf(buf + offset, remain, "%s%u", sep,
+			       r->position_id);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+		sep = " ";
 	}
 
 	ret = snprintf(buf + offset, remain, "\n");
Signature · 1 line
2.30.1
author reviewer
#5 lore raw

PATCH 5/5 expr: data_reg: Reduce indenting level a bit

In reply to Phil Sutter

Given there's only a single valid output_format left, catch other values early to help a bit with those calls to overlong function names.

---
 src/expr/data_reg.c | 31 ++++++++-----------------------
 1 file changed, 8 insertions(+), 23 deletions(-)
src/expr/data_reg.c +8 -23
index d3ccc612ce812..60dc9b81cb4d3 100644
@@ -66,35 +66,20 @@ int nftnl_data_reg_snprintf(char *buf, size_t size,
 			    uint32_t output_format, uint32_t flags,
 			    int reg_type)
 {
+	if (output_format != NFTNL_OUTPUT_DEFAULT)
+		return -1;
+
 	switch(reg_type) {
 	case DATA_VALUE:
-		switch(output_format) {
-		case NFTNL_OUTPUT_DEFAULT:
-			return nftnl_data_reg_value_snprintf_default(buf, size,
-								   reg, flags);
-		case NFTNL_OUTPUT_JSON:
-		case NFTNL_OUTPUT_XML:
-		default:
-			break;
-		}
-		break;
+		return nftnl_data_reg_value_snprintf_default(buf, size, reg,
+							     flags);
 	case DATA_VERDICT:
 	case DATA_CHAIN:
-		switch(output_format) {
-		case NFTNL_OUTPUT_DEFAULT:
-			return nftnl_data_reg_verdict_snprintf_def(buf, size,
-								 reg, flags);
-		case NFTNL_OUTPUT_JSON:
-		case NFTNL_OUTPUT_XML:
-		default:
-			break;
-		}
-		break;
+		return nftnl_data_reg_verdict_snprintf_def(buf, size, reg,
+							   flags);
 	default:
-		break;
+		return -1;
 	}
-
-	return -1;
 }
 
 static int nftnl_data_parse_cb(const struct nlattr *attr, void *data)
Signature · 1 line
2.30.1
author reviewer
#6 lore raw

Re: [libnftnl PATCH 5/5] expr: data_reg: Reduce indenting level a bit

Quoted 2 lines · Phil Sutter

Given there's only a single valid output_format left, catch other values early to help a bit with those calls to overlong function names.

Please disregard this one, I'll combine it with a larger one dealing with the same matter.

Thanks, Phil