naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0012-tc-u32-add-support-for-json-output.patch

6548d8
From 0a250b280fbaf8e4d6ad173cf6d9e082658954b4 Mon Sep 17 00:00:00 2001
6548d8
Message-Id: <0a250b280fbaf8e4d6ad173cf6d9e082658954b4.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 support for json output
6548d8
6548d8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1989591
6548d8
Upstream Status: unknown commit c733722b
6548d8
6548d8
commit c733722b993cb82832722b1490cbc5002035fd20
6548d8
Author: Wen Liang <liangwen12year@gmail.com>
6548d8
Date:   Wed Jan 26 14:44:47 2022 -0500
6548d8
6548d8
    tc: u32: add support for json output
6548d8
6548d8
    Currently u32 filter output does not support json. This commit uses
6548d8
    proper json functions to add support for it.
6548d8
6548d8
    `sprint_u32_handle` adds an extra space after the raw check, remove the
6548d8
    extra space.
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 | 83 ++++++++++++++++++++++++++++++------------------------
6548d8
 1 file changed, 46 insertions(+), 37 deletions(-)
6548d8
6548d8
diff --git a/tc/f_u32.c b/tc/f_u32.c
6548d8
index a5747f67..11da202e 100644
6548d8
--- a/tc/f_u32.c
6548d8
+++ b/tc/f_u32.c
6548d8
@@ -109,7 +109,7 @@ static char *sprint_u32_handle(__u32 handle, char *buf)
6548d8
 		}
6548d8
 	}
6548d8
 	if (show_raw)
6548d8
-		snprintf(b, bsize, "[%08x] ", handle);
6548d8
+		snprintf(b, bsize, "[%08x]", handle);
6548d8
 	return buf;
6548d8
 }
6548d8
 
6548d8
@@ -1213,11 +1213,11 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
6548d8
 
6548d8
 	if (handle) {
6548d8
 		SPRINT_BUF(b1);
6548d8
-		fprintf(f, "fh %s ", sprint_u32_handle(handle, b1));
6548d8
+		print_string(PRINT_ANY, "fh", "fh %s ", sprint_u32_handle(handle, b1));
6548d8
 	}
6548d8
 
6548d8
 	if (TC_U32_NODE(handle))
6548d8
-		fprintf(f, "order %d ", TC_U32_NODE(handle));
6548d8
+		print_int(PRINT_ANY, "order", "order %d ", TC_U32_NODE(handle));
6548d8
 
6548d8
 	if (tb[TCA_U32_SEL]) {
6548d8
 		if (RTA_PAYLOAD(tb[TCA_U32_SEL])  < sizeof(*sel))
6548d8
@@ -1227,15 +1227,15 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
6548d8
 	}
6548d8
 
6548d8
 	if (tb[TCA_U32_DIVISOR]) {
6548d8
-		fprintf(f, "ht divisor %d ",
6548d8
-			rta_getattr_u32(tb[TCA_U32_DIVISOR]));
6548d8
+		__u32 htdivisor = rta_getattr_u32(tb[TCA_U32_DIVISOR]);
6548d8
+
6548d8
+		print_int(PRINT_ANY, "ht_divisor", "ht divisor %d ", htdivisor);
6548d8
 	} else if (tb[TCA_U32_HASH]) {
6548d8
 		__u32 htid = rta_getattr_u32(tb[TCA_U32_HASH]);
6548d8
-
6548d8
-		fprintf(f, "key ht %x bkt %x ", TC_U32_USERHTID(htid),
6548d8
-			TC_U32_HASH(htid));
6548d8
+		print_hex(PRINT_ANY, "key_ht", "key ht %x ", TC_U32_USERHTID(htid));
6548d8
+		print_hex(PRINT_ANY, "bkt", "bkt %x ", TC_U32_HASH(htid));
6548d8
 	} else {
6548d8
-		fprintf(f, "??? ");
6548d8
+		fprintf(stderr, "divisor and hash missing ");
6548d8
 	}
6548d8
 	if (tb[TCA_U32_CLASSID]) {
6548d8
 		SPRINT_BUF(b1);
6548d8
@@ -1244,27 +1244,27 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
6548d8
 			sprint_tc_classid(rta_getattr_u32(tb[TCA_U32_CLASSID]),
6548d8
 					  b1));
6548d8
 	} else if (sel && sel->flags & TC_U32_TERMINAL) {
6548d8
-		fprintf(f, "terminal flowid ??? ");
6548d8
+		print_string(PRINT_FP, NULL, "terminal flowid ", NULL);
6548d8
 	}
6548d8
 	if (tb[TCA_U32_LINK]) {
6548d8
 		SPRINT_BUF(b1);
6548d8
-		fprintf(f, "link %s ",
6548d8
-			sprint_u32_handle(rta_getattr_u32(tb[TCA_U32_LINK]),
6548d8
-					  b1));
6548d8
+		char *link = sprint_u32_handle(rta_getattr_u32(tb[TCA_U32_LINK]), b1);
6548d8
+
6548d8
+		print_string(PRINT_ANY, "link", "link %s ", link);
6548d8
 	}
6548d8
 
6548d8
 	if (tb[TCA_U32_FLAGS]) {
6548d8
 		__u32 flags = rta_getattr_u32(tb[TCA_U32_FLAGS]);
6548d8
 
6548d8
 		if (flags & TCA_CLS_FLAGS_SKIP_HW)
6548d8
-			fprintf(f, "skip_hw ");
6548d8
+			print_bool(PRINT_ANY, "skip_hw", "skip_hw ", true);
6548d8
 		if (flags & TCA_CLS_FLAGS_SKIP_SW)
6548d8
-			fprintf(f, "skip_sw ");
6548d8
+			print_bool(PRINT_ANY, "skip_sw", "skip_sw ", true);
6548d8
 
6548d8
 		if (flags & TCA_CLS_FLAGS_IN_HW)
6548d8
-			fprintf(f, "in_hw ");
6548d8
+			print_bool(PRINT_ANY, "in_hw", "in_hw ", true);
6548d8
 		else if (flags & TCA_CLS_FLAGS_NOT_IN_HW)
6548d8
-			fprintf(f, "not_in_hw ");
6548d8
+			print_bool(PRINT_ANY, "not_in_hw", "not_in_hw ", true);
6548d8
 	}
6548d8
 
6548d8
 	if (tb[TCA_U32_PCNT]) {
6548d8
@@ -1275,10 +1275,10 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
6548d8
 		pf = RTA_DATA(tb[TCA_U32_PCNT]);
6548d8
 	}
6548d8
 
6548d8
-	if (sel && show_stats && NULL != pf)
6548d8
-		fprintf(f, " (rule hit %llu success %llu)",
6548d8
-			(unsigned long long) pf->rcnt,
6548d8
-			(unsigned long long) pf->rhit);
6548d8
+	if (sel && show_stats && NULL != pf) {
6548d8
+		print_u64(PRINT_ANY, "rule_hit", "(rule hit %llu ", pf->rcnt);
6548d8
+		print_u64(PRINT_ANY, "success", "success %llu)", pf->rhit);
6548d8
+	}
6548d8
 
6548d8
 	if (tb[TCA_U32_MARK]) {
6548d8
 		struct tc_u32_mark *mark = RTA_DATA(tb[TCA_U32_MARK]);
6548d8
@@ -1286,8 +1286,10 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
6548d8
 		if (RTA_PAYLOAD(tb[TCA_U32_MARK]) < sizeof(*mark)) {
6548d8
 			fprintf(f, "\n  Invalid mark (kernel&iproute2 mismatch)\n");
6548d8
 		} else {
6548d8
-			fprintf(f, "\n  mark 0x%04x 0x%04x (success %d)",
6548d8
-				mark->val, mark->mask, mark->success);
6548d8
+			print_nl();
6548d8
+			print_0xhex(PRINT_ANY, "fwmark_value", "  mark 0x%04x ", mark->val);
6548d8
+			print_0xhex(PRINT_ANY, "fwmark_mask", "0x%04x ", mark->mask);
6548d8
+			print_int(PRINT_ANY, "fwmark_success", "(success %d)", mark->success);
6548d8
 		}
6548d8
 	}
6548d8
 
6548d8
@@ -1298,38 +1300,45 @@ static int u32_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
6548d8
 			for (i = 0; i < sel->nkeys; i++) {
6548d8
 				show_keys(f, sel->keys + i);
6548d8
 				if (show_stats && NULL != pf)
6548d8
-					fprintf(f, " (success %llu ) ",
6548d8
-						(unsigned long long) pf->kcnts[i]);
6548d8
+					print_u64(PRINT_ANY, "success", " (success %llu ) ",
6548d8
+						  pf->kcnts[i]);
6548d8
 			}
6548d8
 		}
6548d8
 
6548d8
 		if (sel->flags & (TC_U32_VAROFFSET | TC_U32_OFFSET)) {
6548d8
-			fprintf(f, "\n    offset ");
6548d8
-			if (sel->flags & TC_U32_VAROFFSET)
6548d8
-				fprintf(f, "%04x>>%d at %d ",
6548d8
-					ntohs(sel->offmask),
6548d8
-					sel->offshift,  sel->offoff);
6548d8
+			print_nl();
6548d8
+			print_string(PRINT_ANY, NULL, "%s", "    offset ");
6548d8
+			if (sel->flags & TC_U32_VAROFFSET) {
6548d8
+				print_hex(PRINT_ANY, "offset_mask", "%04x", ntohs(sel->offmask));
6548d8
+				print_int(PRINT_ANY, "offset_shift", ">>%d ", sel->offshift);
6548d8
+				print_int(PRINT_ANY, "offset_off", "at %d ", sel->offoff);
6548d8
+			}
6548d8
 			if (sel->off)
6548d8
-				fprintf(f, "plus %d ", sel->off);
6548d8
+				print_int(PRINT_ANY, "plus", "plus %d ", sel->off);
6548d8
 		}
6548d8
 		if (sel->flags & TC_U32_EAT)
6548d8
-			fprintf(f, " eat ");
6548d8
+			print_string(PRINT_ANY, NULL, "%s", " eat ");
6548d8
 
6548d8
 		if (sel->hmask) {
6548d8
-			fprintf(f, "\n    hash mask %08x at %d ",
6548d8
-				(unsigned int)htonl(sel->hmask), sel->hoff);
6548d8
+			print_nl();
6548d8
+			unsigned int hmask = (unsigned int)htonl(sel->hmask);
6548d8
+
6548d8
+			print_hex(PRINT_ANY, "hash_mask", "    hash mask %08x ", hmask);
6548d8
+			print_int(PRINT_ANY, "hash_off", "at %d ", sel->hoff);
6548d8
 		}
6548d8
 	}
6548d8
 
6548d8
 	if (tb[TCA_U32_POLICE]) {
6548d8
-		fprintf(f, "\n");
6548d8
+		print_nl();
6548d8
 		tc_print_police(f, tb[TCA_U32_POLICE]);
6548d8
 	}
6548d8
 
6548d8
 	if (tb[TCA_U32_INDEV]) {
6548d8
 		struct rtattr *idev = tb[TCA_U32_INDEV];
6548d8
-
6548d8
-		fprintf(f, "\n  input dev %s\n", rta_getattr_str(idev));
6548d8
+		print_nl();
6548d8
+		print_string(PRINT_ANY, "input_dev", "  input dev %s",
6548d8
+			     rta_getattr_str(idev));
6548d8
+		print_nl();
6548d8
 	}
6548d8
 
6548d8
 	if (tb[TCA_U32_ACT])
6548d8
-- 
6548d8
2.34.1
6548d8