Subject: [RFC 3/3] [RFC iproute2-next] tc: f_u32: fix the pretty print of ipv4 filters

From: Anton Danilov <[email-masked]>
Date: Tue, 8 Jun 2021 18:33:09 +0300

~$: tc f add dev dummy0 parent 1: protocol ip prio 10 \
    u32 match ip ihl 10 0xf match ip dsfield 0x0a 0xff flowid 1:1

Before patch:

~$ sudo tc -p -s f ls dev dummy0
filter parent 1: protocol ip pref 10 u32 chain 0
filter parent 1: protocol ip pref 10 u32 chain 0 fh 801: ht divisor 1
filter parent 1: protocol ip pref 10 u32 chain 0 fh 801::801 order 2049 key ht 801 bkt 0 flowid 1:1 not_in_hw  (rule hit 0 success 0) (success 0 )

After patch:
~$ sudo tc -p -s f ls dev dummy0
filter parent 1: protocol ip pref 10 u32 chain 0
filter parent 1: protocol ip pref 10 u32 chain 0 fh 801: ht divisor 1
filter parent 1: protocol ip pref 10 u32 chain 0 fh 801::801 order 2049 key ht 801 bkt 0 flowid 1:1 not_in_hw  (rule hit 0 success 0)
  match ip ihl 10 0xf
  match ip dsfield 0xa 0xff (success 0 )

Signed-off-by: Anton Danilov <[email-masked]>
---
 tc/f_u32.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/tc/f_u32.c b/tc/f_u32.c
index d7beb586..e60c787a 100644
--- a/tc/f_u32.c
+++ b/tc/f_u32.c
@@ -827,20 +827,25 @@ static int print_ipv4(FILE *f, const struct tc_u32_key *key)
 	if (key == NULL)
 		return 0;
 
+	int ret = 0;
+
 	switch (key->off) {
 	case 0:
-		switch (ntohl(key->mask)) {
-		case 0x0f000000:
-			return fprintf(f, "\n  match IP ihl %u",
-				ntohl(key->val) >> 24);
-		case 0x00ff0000:
-			return fprintf(f, "\n  match IP dsfield %#x",
-				ntohl(key->val) >> 16);
+		if (ntohl(key->mask) & 0x0f000000) {
+			ret = fprintf(f, "\n  match ip ihl %u %#x",
+				(ntohl(key->val) & 0x0f000000) >> 24,
+				(ntohl(key->mask) & 0x0f000000) >> 24);
+		}
+		if (ntohl(key->mask) & 0x00ff0000) {
+			ret += fprintf(f, "\n  match ip dsfield %#x %#x",
+				(ntohl(key->val) & 0x00ff0000) >> 16,
+				(ntohl(key->mask) & 0x00ff0000) >> 16);
 		}
+		return ret;
 		break;
 	case 8:
 		if (ntohl(key->mask) == 0x00ff0000) {
-			return fprintf(f, "\n  match IP protocol %d",
+			return fprintf(f, "\n  match ip protocol %d",
 				ntohl(key->val) >> 16);
 		}
 		break;
@@ -850,7 +855,7 @@ static int print_ipv4(FILE *f, const struct tc_u32_key *key)
 
 			if (bits >= 0) {
 				return fprintf(f, "\n  %s %s/%d",
-					key->off == 12 ? "match IP src" : "match IP dst",
+					key->off == 12 ? "match ip src" : "match ip dst",
 					inet_ntop(AF_INET, &key->val,
 						  abuf, sizeof(abuf)),
 					bits);
-- 
2.20.1