Blame SOURCES/0006-police-Add-support-for-json-output.patch

d5a8e7
From 04b921c03a4680931df6660b88444f2478fb585c Mon Sep 17 00:00:00 2001
d5a8e7
Message-Id: <04b921c03a4680931df6660b88444f2478fb585c.1628790091.git.aclaudi@redhat.com>
d5a8e7
In-Reply-To: <650694eb0120722499207078f965442ef7343bb1.1628790091.git.aclaudi@redhat.com>
d5a8e7
References: <650694eb0120722499207078f965442ef7343bb1.1628790091.git.aclaudi@redhat.com>
d5a8e7
From: Andrea Claudi <aclaudi@redhat.com>
d5a8e7
Date: Wed, 11 Aug 2021 12:55:14 +0200
d5a8e7
Subject: [PATCH] police: Add support for json output
d5a8e7
d5a8e7
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1981393
d5a8e7
Upstream Status: iproute2.git commit 0d5cf51e
d5a8e7
d5a8e7
commit 0d5cf51e0d6c7bfdc51754381b85367b5f8e254a
d5a8e7
Author: Roi Dayan <roid@nvidia.com>
d5a8e7
Date:   Mon Jun 7 09:44:08 2021 +0300
d5a8e7
d5a8e7
    police: Add support for json output
d5a8e7
d5a8e7
    Change to use the print wrappers instead of fprintf().
d5a8e7
d5a8e7
    This is example output of the options part before this commit:
d5a8e7
d5a8e7
            "options": {
d5a8e7
                "handle": 1,
d5a8e7
                "in_hw": true,
d5a8e7
                "actions": [ {
d5a8e7
                        "order": 1 police 0x2 ,
d5a8e7
                        "control_action": {
d5a8e7
                            "type": "drop"
d5a8e7
                        },
d5a8e7
                        "control_action": {
d5a8e7
                            "type": "continue"
d5a8e7
                        }overhead 0b linklayer unspec
d5a8e7
            ref 1 bind 1
d5a8e7
    ,
d5a8e7
                        "used_hw_stats": [ "delayed" ]
d5a8e7
                    } ]
d5a8e7
            }
d5a8e7
d5a8e7
    This is the output of the same dump with this commit:
d5a8e7
d5a8e7
            "options": {
d5a8e7
                "handle": 1,
d5a8e7
                "in_hw": true,
d5a8e7
                "actions": [ {
d5a8e7
                        "order": 1,
d5a8e7
                        "kind": "police",
d5a8e7
                        "index": 2,
d5a8e7
                        "control_action": {
d5a8e7
                            "type": "drop"
d5a8e7
                        },
d5a8e7
                        "control_action": {
d5a8e7
                            "type": "continue"
d5a8e7
                        },
d5a8e7
                        "overhead": 0,
d5a8e7
                        "linklayer": "unspec",
d5a8e7
                        "ref": 1,
d5a8e7
                        "bind": 1,
d5a8e7
                        "used_hw_stats": [ "delayed" ]
d5a8e7
                    } ]
d5a8e7
            }
d5a8e7
d5a8e7
    Signed-off-by: Roi Dayan <roid@nvidia.com>
d5a8e7
    Reviewed-by: Paul Blakey <paulb@nvidia.com>
d5a8e7
    Signed-off-by: David Ahern <dsahern@kernel.org>
d5a8e7
d5a8e7
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
d5a8e7
---
d5a8e7
 tc/m_police.c | 30 +++++++++++++++++-------------
d5a8e7
 1 file changed, 17 insertions(+), 13 deletions(-)
d5a8e7
d5a8e7
diff --git a/tc/m_police.c b/tc/m_police.c
d5a8e7
index 9ef0e40b..2594c089 100644
d5a8e7
--- a/tc/m_police.c
d5a8e7
+++ b/tc/m_police.c
d5a8e7
@@ -278,18 +278,19 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
d5a8e7
 	__u64 rate64, prate64;
d5a8e7
 	__u64 pps64, ppsburst64;
d5a8e7
 
d5a8e7
+	print_string(PRINT_ANY, "kind", "%s", "police");
d5a8e7
 	if (arg == NULL)
d5a8e7
 		return 0;
d5a8e7
 
d5a8e7
 	parse_rtattr_nested(tb, TCA_POLICE_MAX, arg);
d5a8e7
 
d5a8e7
 	if (tb[TCA_POLICE_TBF] == NULL) {
d5a8e7
-		fprintf(f, "[NULL police tbf]");
d5a8e7
-		return 0;
d5a8e7
+		fprintf(stderr, "[NULL police tbf]");
d5a8e7
+		return -1;
d5a8e7
 	}
d5a8e7
 #ifndef STOOPID_8BYTE
d5a8e7
 	if (RTA_PAYLOAD(tb[TCA_POLICE_TBF])  < sizeof(*p)) {
d5a8e7
-		fprintf(f, "[truncated police tbf]");
d5a8e7
+		fprintf(stderr, "[truncated police tbf]");
d5a8e7
 		return -1;
d5a8e7
 	}
d5a8e7
 #endif
d5a8e7
@@ -300,13 +301,13 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
d5a8e7
 	    RTA_PAYLOAD(tb[TCA_POLICE_RATE64]) >= sizeof(rate64))
d5a8e7
 		rate64 = rta_getattr_u64(tb[TCA_POLICE_RATE64]);
d5a8e7
 
d5a8e7
-	fprintf(f, " police 0x%x ", p->index);
d5a8e7
+	print_uint(PRINT_ANY, "index", "\t index %u ", p->index);
d5a8e7
 	tc_print_rate(PRINT_FP, NULL, "rate %s ", rate64);
d5a8e7
 	buffer = tc_calc_xmitsize(rate64, p->burst);
d5a8e7
 	print_size(PRINT_FP, NULL, "burst %s ", buffer);
d5a8e7
 	print_size(PRINT_FP, NULL, "mtu %s ", p->mtu);
d5a8e7
 	if (show_raw)
d5a8e7
-		fprintf(f, "[%08x] ", p->burst);
d5a8e7
+		print_hex(PRINT_FP, NULL, "[%08x] ", p->burst);
d5a8e7
 
d5a8e7
 	prate64 = p->peakrate.rate;
d5a8e7
 	if (tb[TCA_POLICE_PEAKRATE64] &&
d5a8e7
@@ -327,8 +328,8 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
d5a8e7
 		pps64 = rta_getattr_u64(tb[TCA_POLICE_PKTRATE64]);
d5a8e7
 		ppsburst64 = rta_getattr_u64(tb[TCA_POLICE_PKTBURST64]);
d5a8e7
 		ppsburst64 = tc_calc_xmitsize(pps64, ppsburst64);
d5a8e7
-		fprintf(f, "pkts_rate %llu ", pps64);
d5a8e7
-		fprintf(f, "pkts_burst %llu ", ppsburst64);
d5a8e7
+		print_u64(PRINT_ANY, "pkts_rate", "pkts_rate %llu ", pps64);
d5a8e7
+		print_u64(PRINT_ANY, "pkts_burst", "pkts_burst %llu ", ppsburst64);
d5a8e7
 	}
d5a8e7
 
d5a8e7
 	print_action_control(f, "action ", p->action, "");
d5a8e7
@@ -337,14 +338,17 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
d5a8e7
 		__u32 action = rta_getattr_u32(tb[TCA_POLICE_RESULT]);
d5a8e7
 
d5a8e7
 		print_action_control(f, "/", action, " ");
d5a8e7
-	} else
d5a8e7
-		fprintf(f, " ");
d5a8e7
+	} else {
d5a8e7
+		print_string(PRINT_FP, NULL, " ", NULL);
d5a8e7
+	}
d5a8e7
 
d5a8e7
-	fprintf(f, "overhead %ub ", p->rate.overhead);
d5a8e7
+	print_uint(PRINT_ANY, "overhead", "overhead %u ", p->rate.overhead);
d5a8e7
 	linklayer = (p->rate.linklayer & TC_LINKLAYER_MASK);
d5a8e7
 	if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
d5a8e7
-		fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2));
d5a8e7
-	fprintf(f, "\n\tref %d bind %d", p->refcnt, p->bindcnt);
d5a8e7
+		print_string(PRINT_ANY, "linklayer", "linklayer %s ",
d5a8e7
+			     sprint_linklayer(linklayer, b2));
d5a8e7
+	print_int(PRINT_ANY, "ref", "ref %d ", p->refcnt);
d5a8e7
+	print_int(PRINT_ANY, "bind", "bind %d ", p->bindcnt);
d5a8e7
 	if (show_stats) {
d5a8e7
 		if (tb[TCA_POLICE_TM]) {
d5a8e7
 			struct tcf_t *tm = RTA_DATA(tb[TCA_POLICE_TM]);
d5a8e7
@@ -352,7 +356,7 @@ static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
d5a8e7
 			print_tm(f, tm);
d5a8e7
 		}
d5a8e7
 	}
d5a8e7
-	fprintf(f, "\n");
d5a8e7
+	print_nl();
d5a8e7
 
d5a8e7
 
d5a8e7
 	return 0;
d5a8e7
-- 
d5a8e7
2.31.1
d5a8e7