lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <0106018d927d04ff-efbd5d4b-b32f-4b39-a184-a28939608096-000000@ap-northeast-1.amazonses.com>
Date: Sat, 10 Feb 2024 10:08:03 +0000
From: Takanori Hirano <me@...tknr.net>
To: Stephen Hemminger <stephen@...workplumber.org>
Cc: netdev@...r.kernel.org, Takanori Hirano <me@...tknr.net>
Subject: [PATCH iproute2 v2] tc: Add support json option in filter.

Add support for "-json" output in tc-fw, tc-cgroup, tc-flow, and tc-route.

Signed-off-by: Takanori Hirano <me@...tknr.net>
---
Changes in v2:
- Fix terminology, in f_fw, handle -> fw
- Add json option support for tc-cgroup, tc-flow, and tc-route
---
 tc/f_cgroup.c |  4 ++--
 tc/f_flow.c   | 51 ++++++++++++++++++++++++++++++---------------------
 tc/f_fw.c     | 22 +++++++++++++++-------
 tc/f_route.c  | 38 ++++++++++++++++++++++++++++++--------
 4 files changed, 77 insertions(+), 38 deletions(-)

diff --git a/tc/f_cgroup.c b/tc/f_cgroup.c
index a4fc03d1..0b69db6d 100644
--- a/tc/f_cgroup.c
+++ b/tc/f_cgroup.c
@@ -86,13 +86,13 @@ static int cgroup_print_opt(struct filter_util *qu, FILE *f,
 	parse_rtattr_nested(tb, TCA_CGROUP_MAX, opt);
 
 	if (handle)
-		fprintf(f, "handle 0x%x ", handle);
+		print_0xhex(PRINT_ANY, "handle", "handle %#llx ", handle);
 
 	if (tb[TCA_CGROUP_EMATCHES])
 		print_ematch(f, tb[TCA_CGROUP_EMATCHES]);
 
 	if (tb[TCA_CGROUP_POLICE]) {
-		fprintf(f, "\n");
+		print_string(PRINT_FP, NULL, "\n", NULL);
 		tc_print_police(f, tb[TCA_CGROUP_POLICE]);
 	}
 
diff --git a/tc/f_flow.c b/tc/f_flow.c
index 2445aaef..1c9a636d 100644
--- a/tc/f_flow.c
+++ b/tc/f_flow.c
@@ -272,33 +272,37 @@ static int flow_print_opt(struct filter_util *fu, FILE *f, struct rtattr *opt,
 
 	parse_rtattr_nested(tb, TCA_FLOW_MAX, opt);
 
-	fprintf(f, "handle 0x%x ", handle);
+	print_0xhex(PRINT_ANY, "handle", "handle %#llx ", handle);
 
 	if (tb[TCA_FLOW_MODE]) {
 		__u32 mode = rta_getattr_u32(tb[TCA_FLOW_MODE]);
 
 		switch (mode) {
 		case FLOW_MODE_MAP:
-			fprintf(f, "map ");
+			open_json_object("map");
+			print_string(PRINT_FP, NULL, "map ", NULL);
 			break;
 		case FLOW_MODE_HASH:
-			fprintf(f, "hash ");
+			open_json_object("hash");
+			print_string(PRINT_FP, NULL, "hash ", NULL);
 			break;
 		}
 	}
 
 	if (tb[TCA_FLOW_KEYS]) {
 		__u32 keymask = rta_getattr_u32(tb[TCA_FLOW_KEYS]);
-		char *sep = "";
+		char *sep = " ";
 
-		fprintf(f, "keys ");
+		open_json_array(PRINT_ANY, "keys");
 		for (i = 0; i <= FLOW_KEY_MAX; i++) {
 			if (keymask & (1 << i)) {
-				fprintf(f, "%s%s", sep, flow_keys[i]);
+				print_string(PRINT_FP, NULL, "%s", sep);
+				print_string(PRINT_ANY, NULL, "%s",
+					     flow_keys[i]);
 				sep = ",";
 			}
 		}
-		fprintf(f, " ");
+		close_json_array(PRINT_ANY, " ");
 	}
 
 	if (tb[TCA_FLOW_MASK])
@@ -311,39 +315,44 @@ static int flow_print_opt(struct filter_util *fu, FILE *f, struct rtattr *opt,
 		__u32 xor = mask & val;
 
 		if (mask != ~0)
-			fprintf(f, "and 0x%.8x ", mask);
+			print_0xhex(PRINT_ANY, "and", "and 0x%.8x ", mask);
 		if (xor != 0)
-			fprintf(f, "xor 0x%.8x ", xor);
+			print_0xhex(PRINT_ANY, "xor", "xor 0x%.8x ", xor);
 		if (or != 0)
-			fprintf(f, "or 0x%.8x ", or);
+			print_0xhex(PRINT_ANY, "or", "or 0x%.8x ", or);
 	}
 
 	if (tb[TCA_FLOW_RSHIFT])
-		fprintf(f, "rshift %u ",
-			rta_getattr_u32(tb[TCA_FLOW_RSHIFT]));
+		print_uint(PRINT_ANY, "rshift", "rshift %u ",
+			   rta_getattr_u32(tb[TCA_FLOW_RSHIFT]));
 	if (tb[TCA_FLOW_ADDEND])
-		fprintf(f, "addend 0x%x ",
-			rta_getattr_u32(tb[TCA_FLOW_ADDEND]));
+		print_0xhex(PRINT_ANY, "addend", "addend 0x%x ",
+			    rta_getattr_u32(tb[TCA_FLOW_ADDEND]));
 
 	if (tb[TCA_FLOW_DIVISOR])
-		fprintf(f, "divisor %u ",
-			rta_getattr_u32(tb[TCA_FLOW_DIVISOR]));
+		print_uint(PRINT_ANY, "divisor", "divisor %u ",
+			   rta_getattr_u32(tb[TCA_FLOW_DIVISOR]));
 	if (tb[TCA_FLOW_BASECLASS])
-		fprintf(f, "baseclass %s ",
-			sprint_tc_classid(rta_getattr_u32(tb[TCA_FLOW_BASECLASS]), b1));
+		print_string(PRINT_ANY, "baseclass", "baseclass %s ",
+			     sprint_tc_classid(
+				     rta_getattr_u32(tb[TCA_FLOW_BASECLASS]),
+				     b1));
 
 	if (tb[TCA_FLOW_PERTURB])
-		fprintf(f, "perturb %usec ",
-			rta_getattr_u32(tb[TCA_FLOW_PERTURB]));
+		print_uint(PRINT_ANY, "perturb", "perturb %usec ",
+			   rta_getattr_u32(tb[TCA_FLOW_PERTURB]));
 
 	if (tb[TCA_FLOW_EMATCHES])
 		print_ematch(f, tb[TCA_FLOW_EMATCHES]);
 	if (tb[TCA_FLOW_POLICE])
 		tc_print_police(f, tb[TCA_FLOW_POLICE]);
 	if (tb[TCA_FLOW_ACT]) {
-		fprintf(f, "\n");
+		print_string(PRINT_FP, NULL, "\n", NULL);
 		tc_print_action(f, tb[TCA_FLOW_ACT], 0);
 	}
+	if (tb[TCA_FLOW_MODE]) {
+		close_json_object();
+	}
 	return 0;
 }
 
diff --git a/tc/f_fw.c b/tc/f_fw.c
index 38bec492..1af6b6a7 100644
--- a/tc/f_fw.c
+++ b/tc/f_fw.c
@@ -124,18 +124,25 @@ static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u
 	if (handle || tb[TCA_FW_MASK]) {
 		__u32 mark = 0, mask = 0;
 
+		open_json_object("fw");
 		if (handle)
 			mark = handle;
 		if (tb[TCA_FW_MASK] &&
-		    (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF)
-			fprintf(f, "handle 0x%x/0x%x ", mark, mask);
-		else
-			fprintf(f, "handle 0x%x ", handle);
+		    (mask = rta_getattr_u32(tb[TCA_FW_MASK])) != 0xFFFFFFFF) {
+			print_0xhex(PRINT_ANY, "mark", "handle 0x%x", mark);
+			print_0xhex(PRINT_ANY, "mask", "/0x%x ", mask);
+		} else {
+			print_0xhex(PRINT_ANY, "mark", "handle 0x%x ", mark);
+			print_0xhex(PRINT_JSON, "mask", NULL, 0xFFFFFFFF);
+		}
+		close_json_object();
 	}
 
 	if (tb[TCA_FW_CLASSID]) {
 		SPRINT_BUF(b1);
-		fprintf(f, "classid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
+		print_string(PRINT_ANY, "classid", "classid %s ",
+			     sprint_tc_classid(
+				     rta_getattr_u32(tb[TCA_FW_CLASSID]), b1));
 	}
 
 	if (tb[TCA_FW_POLICE])
@@ -143,11 +150,12 @@ static int fw_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u
 	if (tb[TCA_FW_INDEV]) {
 		struct rtattr *idev = tb[TCA_FW_INDEV];
 
-		fprintf(f, "input dev %s ", rta_getattr_str(idev));
+		print_string(PRINT_ANY, "indev", "input dev %s ",
+			     rta_getattr_str(idev));
 	}
 
 	if (tb[TCA_FW_ACT]) {
-		fprintf(f, "\n");
+		print_string(PRINT_FP, NULL, "\n", "");
 		tc_print_action(f, tb[TCA_FW_ACT], 0);
 	}
 	return 0;
diff --git a/tc/f_route.c b/tc/f_route.c
index e92c7985..21ca7f75 100644
--- a/tc/f_route.c
+++ b/tc/f_route.c
@@ -146,20 +146,42 @@ static int route_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt,
 	parse_rtattr_nested(tb, TCA_ROUTE4_MAX, opt);
 
 	if (handle)
-		fprintf(f, "fh 0x%08x ", handle);
+		print_0xhex(PRINT_ANY, "fh", "fh 0x%08x ", handle);
 	if (handle&0x7F00)
-		fprintf(f, "order %d ", (handle>>8)&0x7F);
+		print_uint(PRINT_ANY, "order", "order %d ",
+			   (handle >> 8) & 0x7F);
 
 	if (tb[TCA_ROUTE4_CLASSID]) {
 		SPRINT_BUF(b1);
-		fprintf(f, "flowid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_ROUTE4_CLASSID]), b1));
+		print_string(PRINT_ANY, "flowid", "flowid %s ",
+			     sprint_tc_classid(
+				     rta_getattr_u32(tb[TCA_ROUTE4_CLASSID]),
+				     b1));
+	}
+	if (tb[TCA_ROUTE4_TO]) {
+		open_json_object("to");
+		print_string(
+			PRINT_ANY, "name", "to %s ",
+			rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_TO]), b1,
+					 sizeof(b1)));
+		print_uint(PRINT_JSON, "id", NULL,
+			   rta_getattr_u32(tb[TCA_ROUTE4_TO]));
+		close_json_object();
+	}
+	if (tb[TCA_ROUTE4_FROM]) {
+		open_json_object("from");
+		print_string(
+			PRINT_ANY, "name", "from %s ",
+			rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_FROM]),
+					 b1, sizeof(b1)));
+		print_uint(PRINT_JSON, "id", NULL,
+			   rta_getattr_u32(tb[TCA_ROUTE4_FROM]));
+		close_json_object();
 	}
-	if (tb[TCA_ROUTE4_TO])
-		fprintf(f, "to %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_TO]), b1, sizeof(b1)));
-	if (tb[TCA_ROUTE4_FROM])
-		fprintf(f, "from %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_FROM]), b1, sizeof(b1)));
 	if (tb[TCA_ROUTE4_IIF])
-		fprintf(f, "fromif %s", ll_index_to_name(rta_getattr_u32(tb[TCA_ROUTE4_IIF])));
+		print_string(
+			PRINT_ANY, "fromif", "fromif %s",
+			ll_index_to_name(rta_getattr_u32(tb[TCA_ROUTE4_IIF])));
 	if (tb[TCA_ROUTE4_POLICE])
 		tc_print_police(f, tb[TCA_ROUTE4_POLICE]);
 	if (tb[TCA_ROUTE4_ACT])
-- 
2.34.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ