naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0013-tc-u32-add-json-support-in-print_raw-print_ipv4-prin.patch

6548d8
From 66efa0a6dc179f814614fbd2f47c37d7e20e4405 Mon Sep 17 00:00:00 2001
6548d8
Message-Id: <66efa0a6dc179f814614fbd2f47c37d7e20e4405.1644243783.git.aclaudi@redhat.com>
6548d8
In-Reply-To: <b30268eda844bdebbb8e5e4f5735e3b1bb666368.1644243783.git.aclaudi@redhat.com>
6548d8
References: <b30268eda844bdebbb8e5e4f5735e3b1bb666368.1644243783.git.aclaudi@redhat.com>
6548d8
From: Andrea Claudi <aclaudi@redhat.com>
6548d8
Date: Mon, 7 Feb 2022 15:16:36 +0100
6548d8
Subject: [PATCH] tc: u32: add json support in `print_raw`, `print_ipv4`,
6548d8
 `print_ipv6`
6548d8
6548d8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1989591
6548d8
Upstream Status: unknown commit 721435dc
6548d8
6548d8
commit 721435dcfd9274277af2fb6a4cec81d4a9bcc6b4
6548d8
Author: Wen Liang <liangwen12year@gmail.com>
6548d8
Date:   Wed Jan 26 14:44:48 2022 -0500
6548d8
6548d8
    tc: u32: add json support in `print_raw`, `print_ipv4`, `print_ipv6`
6548d8
6548d8
    Currently the key struct of u32 filter does not support json. This
6548d8
    commit adds json support for showing key.
6548d8
6548d8
    Signed-off-by: Wen Liang <liangwen12year@gmail.com>
6548d8
    Tested-by: Victor Nogueira <victor@mojatatu.com>
6548d8
    Signed-off-by: David Ahern <dsahern@kernel.org>
6548d8
---
6548d8
 tc/f_u32.c | 121 ++++++++++++++++++++++++++++++++++-------------------
6548d8
 1 file changed, 79 insertions(+), 42 deletions(-)
6548d8
6548d8
diff --git a/tc/f_u32.c b/tc/f_u32.c
6548d8
index 11da202e..d787eb91 100644
6548d8
--- a/tc/f_u32.c
6548d8
+++ b/tc/f_u32.c
6548d8
@@ -824,23 +824,27 @@ static void print_ipv4(FILE *f, const struct tc_u32_key *key)
6548d8
 {
6548d8
 	char abuf[256];
6548d8
 
6548d8
+	open_json_object("match");
6548d8
 	switch (key->off) {
6548d8
 	case 0:
6548d8
 		switch (ntohl(key->mask)) {
6548d8
 		case 0x0f000000:
6548d8
-			fprintf(f, "\n  match IP ihl %u",
6548d8
-				ntohl(key->val) >> 24);
6548d8
+			print_nl();
6548d8
+			print_uint(PRINT_ANY, "ip_ihl", "  match IP ihl %u",
6548d8
+				   ntohl(key->val) >> 24);
6548d8
 			return;
6548d8
 		case 0x00ff0000:
6548d8
-			fprintf(f, "\n  match IP dsfield %#x",
6548d8
-				ntohl(key->val) >> 16);
6548d8
+			print_nl();
6548d8
+			print_0xhex(PRINT_ANY, "ip_dsfield", "  match IP dsfield %#x",
6548d8
+				    ntohl(key->val) >> 16);
6548d8
 			return;
6548d8
 		}
6548d8
 		break;
6548d8
 	case 8:
6548d8
 		if (ntohl(key->mask) == 0x00ff0000) {
6548d8
-			fprintf(f, "\n  match IP protocol %d",
6548d8
-				ntohl(key->val) >> 16);
6548d8
+			print_nl();
6548d8
+			print_int(PRINT_ANY, "ip_protocol", "  match IP protocol %d",
6548d8
+				  ntohl(key->val) >> 16);
6548d8
 			return;
6548d8
 		}
6548d8
 		break;
6548d8
@@ -849,11 +853,21 @@ static void print_ipv4(FILE *f, const struct tc_u32_key *key)
6548d8
 			int bits = mask2bits(key->mask);
6548d8
 
6548d8
 			if (bits >= 0) {
6548d8
-				fprintf(f, "\n  %s %s/%d",
6548d8
-					key->off == 12 ? "match IP src" : "match IP dst",
6548d8
-					inet_ntop(AF_INET, &key->val,
6548d8
-						  abuf, sizeof(abuf)),
6548d8
-					bits);
6548d8
+				const char *addr;
6548d8
+
6548d8
+				if (key->off == 12) {
6548d8
+					print_nl();
6548d8
+					print_null(PRINT_FP, NULL, "  match IP src ", NULL);
6548d8
+					open_json_object("src");
6548d8
+				} else {
6548d8
+					print_nl();
6548d8
+					print_null(PRINT_FP, NULL, "  match IP dst ", NULL);
6548d8
+					open_json_object("dst");
6548d8
+				}
6548d8
+				addr = inet_ntop(AF_INET, &key->val, abuf, sizeof(abuf));
6548d8
+				print_string(PRINT_ANY, "address", "%s", addr);
6548d8
+				print_int(PRINT_ANY, "prefixlen", "/%d", bits);
6548d8
+				close_json_object();
6548d8
 				return;
6548d8
 			}
6548d8
 		}
6548d8
@@ -862,45 +876,52 @@ static void print_ipv4(FILE *f, const struct tc_u32_key *key)
6548d8
 	case 20:
6548d8
 		switch (ntohl(key->mask)) {
6548d8
 		case 0x0000ffff:
6548d8
-			fprintf(f, "\n  match dport %u",
6548d8
-				ntohl(key->val) & 0xffff);
6548d8
+			print_uint(PRINT_ANY, "dport", "match dport %u",
6548d8
+				   ntohl(key->val) & 0xffff);
6548d8
 			return;
6548d8
 		case 0xffff0000:
6548d8
-			fprintf(f, "\n  match sport %u",
6548d8
-				ntohl(key->val) >> 16);
6548d8
+			print_nl();
6548d8
+			print_uint(PRINT_ANY, "sport", "  match sport %u",
6548d8
+				   ntohl(key->val) >> 16);
6548d8
 			return;
6548d8
 		case 0xffffffff:
6548d8
-			fprintf(f, "\n  match dport %u, match sport %u",
6548d8
-				ntohl(key->val) & 0xffff,
6548d8
-				ntohl(key->val) >> 16);
6548d8
-
6548d8
+			print_nl();
6548d8
+			print_uint(PRINT_ANY, "dport", "  match dport %u, ",
6548d8
+				   ntohl(key->val) & 0xffff);
6548d8
+			print_uint(PRINT_ANY, "sport", "match sport %u",
6548d8
+				   ntohl(key->val) >> 16);
6548d8
 			return;
6548d8
 		}
6548d8
 		/* XXX: Default print_raw */
6548d8
 	}
6548d8
+	close_json_object();
6548d8
 }
6548d8
 
6548d8
 static void print_ipv6(FILE *f, const struct tc_u32_key *key)
6548d8
 {
6548d8
 	char abuf[256];
6548d8
 
6548d8
+	open_json_object("match");
6548d8
 	switch (key->off) {
6548d8
 	case 0:
6548d8
 		switch (ntohl(key->mask)) {
6548d8
 		case 0x0f000000:
6548d8
-			fprintf(f, "\n  match IP ihl %u",
6548d8
-				ntohl(key->val) >> 24);
6548d8
+			print_nl();
6548d8
+			print_uint(PRINT_ANY, "ip_ihl", "  match IP ihl %u",
6548d8
+				   ntohl(key->val) >> 24);
6548d8
 			return;
6548d8
 		case 0x00ff0000:
6548d8
-			fprintf(f, "\n  match IP dsfield %#x",
6548d8
-				ntohl(key->val) >> 16);
6548d8
+			print_nl();
6548d8
+			print_0xhex(PRINT_ANY, "ip_dsfield", "  match IP dsfield %#x",
6548d8
+				    ntohl(key->val) >> 16);
6548d8
 			return;
6548d8
 		}
6548d8
 		break;
6548d8
 	case 8:
6548d8
 		if (ntohl(key->mask) == 0x00ff0000) {
6548d8
-			fprintf(f, "\n  match IP protocol %d",
6548d8
-				ntohl(key->val) >> 16);
6548d8
+			print_nl();
6548d8
+			print_int(PRINT_ANY, "ip_protocol", "  match IP protocol %d",
6548d8
+				  ntohl(key->val) >> 16);
6548d8
 			return;
6548d8
 		}
6548d8
 		break;
6548d8
@@ -909,11 +930,21 @@ static void print_ipv6(FILE *f, const struct tc_u32_key *key)
6548d8
 			int bits = mask2bits(key->mask);
6548d8
 
6548d8
 			if (bits >= 0) {
6548d8
-				fprintf(f, "\n  %s %s/%d",
6548d8
-					key->off == 12 ? "match IP src" : "match IP dst",
6548d8
-					inet_ntop(AF_INET, &key->val,
6548d8
-						  abuf, sizeof(abuf)),
6548d8
-					bits);
6548d8
+				const char *addr;
6548d8
+
6548d8
+				if (key->off == 12) {
6548d8
+					print_nl();
6548d8
+					print_null(PRINT_FP, NULL, "  match IP src ", NULL);
6548d8
+					open_json_object("src");
6548d8
+				} else {
6548d8
+					print_nl();
6548d8
+					print_null(PRINT_FP, NULL, "  match IP dst ", NULL);
6548d8
+					open_json_object("dst");
6548d8
+				}
6548d8
+				addr = inet_ntop(AF_INET, &key->val, abuf, sizeof(abuf));
6548d8
+				print_string(PRINT_ANY, "address", "%s", addr);
6548d8
+				print_int(PRINT_ANY, "prefixlen", "/%d", bits);
6548d8
+				close_json_object();
6548d8
 				return;
6548d8
 			}
6548d8
 		}
6548d8
@@ -922,31 +953,37 @@ static void print_ipv6(FILE *f, const struct tc_u32_key *key)
6548d8
 	case 20:
6548d8
 		switch (ntohl(key->mask)) {
6548d8
 		case 0x0000ffff:
6548d8
-			fprintf(f, "\n  match sport %u",
6548d8
-				ntohl(key->val) & 0xffff);
6548d8
+			print_nl();
6548d8
+			print_uint(PRINT_ANY, "sport", "  match sport %u",
6548d8
+				   ntohl(key->val) & 0xffff);
6548d8
 			return;
6548d8
 		case 0xffff0000:
6548d8
-			fprintf(f, "\n  match dport %u",
6548d8
-				ntohl(key->val) >> 16);
6548d8
+			print_uint(PRINT_ANY, "dport", "match dport %u",
6548d8
+				   ntohl(key->val) >> 16);
6548d8
 			return;
6548d8
 		case 0xffffffff:
6548d8
-			fprintf(f, "\n  match sport %u, match dport %u",
6548d8
-				ntohl(key->val) & 0xffff,
6548d8
-				ntohl(key->val) >> 16);
6548d8
+			print_nl();
6548d8
+			print_uint(PRINT_ANY, "sport", "  match sport %u, ",
6548d8
+				   ntohl(key->val) & 0xffff);
6548d8
+			print_uint(PRINT_ANY, "dport", "match dport %u",
6548d8
+				   ntohl(key->val) >> 16);
6548d8
 
6548d8
 			return;
6548d8
 		}
6548d8
 		/* XXX: Default print_raw */
6548d8
 	}
6548d8
+	close_json_object();
6548d8
 }
6548d8
 
6548d8
 static void print_raw(FILE *f, const struct tc_u32_key *key)
6548d8
 {
6548d8
-	fprintf(f, "\n  match %08x/%08x at %s%d",
6548d8
-		(unsigned int)ntohl(key->val),
6548d8
-		(unsigned int)ntohl(key->mask),
6548d8
-		key->offmask ? "nexthdr+" : "",
6548d8
-		key->off);
6548d8
+	open_json_object("match");
6548d8
+	print_nl();
6548d8
+	print_hex(PRINT_ANY, "value", "  match %08x", (unsigned int)ntohl(key->val));
6548d8
+	print_hex(PRINT_ANY, "mask", "/%08x ", (unsigned int)ntohl(key->mask));
6548d8
+	print_string(PRINT_ANY, "offmask", "at %s", key->offmask ? "nexthdr+" : "");
6548d8
+	print_int(PRINT_ANY, "off", "%d", key->off);
6548d8
+	close_json_object();
6548d8
 }
6548d8
 
6548d8
 static const struct {
6548d8
-- 
6548d8
2.34.1
6548d8