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: <20180710210558.20278-7-stephen@networkplumber.org>
Date:   Tue, 10 Jul 2018 14:05:33 -0700
From:   Stephen Hemminger <stephen@...workplumber.org>
To:     netdev@...r.kernel.org
Cc:     Stephen Hemminger <sthemmin@...rosoft.com>,
        Stephen Hemminger <stephen@...workplumber.org>
Subject: [PATCH v2 iproute2-next 06/31] tc/util: add print helpers for JSON

From: Stephen Hemminger <sthemmin@...rosoft.com>

Add a helper to print rate, time and size in numeric or pretty format
based on JSON flag.

Signed-off-by: Stephen Hemminger <stephen@...workplumber.org>
---
 tc/tc_util.c | 83 +++++++++++++++++++++++++++++++++-------------------
 tc/tc_util.h |  6 ++++
 2 files changed, 59 insertions(+), 30 deletions(-)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index 29bdbbff4a6e..43a67fd13f94 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -330,6 +330,19 @@ char *sprint_rate(__u64 rate, char *buf)
 	return buf;
 }
 
+/*
+ * print rate as numeric in JSON
+ * or in human format otherwise.
+ */
+void print_rate(const char *key, const char *fmt, __u64 rate)
+{
+	SPRINT_BUF(b1);
+
+	print_u64(PRINT_JSON, key, NULL, rate);
+	print_string(PRINT_FP, NULL, fmt,
+		     sprint_rate(rate, b1));
+}
+
 int get_time(unsigned int *time, const char *str)
 {
 	double t;
@@ -372,11 +385,27 @@ char *sprint_time(__u32 time, char *buf)
 	return buf;
 }
 
+void print_time(const char *key, const char *fmt, __u32 tm)
+{
+	double secs = (double)tm / TIME_UNITS_PER_SEC;
+	SPRINT_BUF(b1);
+
+	print_float(PRINT_JSON, key, NULL, secs);
+	print_string(PRINT_FP, NULL, fmt,
+		     sprint_time(tm, b1));
+}
+
+
 char *sprint_ticks(__u32 ticks, char *buf)
 {
 	return sprint_time(tc_core_tick2time(ticks), buf);
 }
 
+void print_ticks(const char *key, const char *fmt, __u32 ticks)
+{
+	print_time(key, fmt, tc_core_tick2time(ticks));
+}
+
 int get_size(unsigned int *size, const char *str)
 {
 	double sz;
@@ -462,6 +491,15 @@ char *sprint_size(__u32 sz, char *buf)
 	return buf;
 }
 
+void print_size(const char *key, const char *fmt, __u32 sz)
+{
+	SPRINT_BUF(b1);
+
+	print_uint(PRINT_JSON, key, NULL, sz);
+	print_string(PRINT_FP, NULL, fmt,
+		     sprint_size(sz, b1));
+}
+
 void print_qdisc_handle(char *buf, int len, __u32 h)
 {
 	snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
@@ -766,6 +804,14 @@ char *sprint_linklayer(unsigned int linklayer, char *buf)
 	return buf;
 }
 
+void print_linklayer(const char *key, const char *fmt, unsigned int linklayer)
+{
+	SPRINT_BUF(b1);
+
+	sprint_linklayer(linklayer, b1);
+	print_string(PRINT_ANY, key, fmt, b1);
+}
+
 void print_tm(FILE *f, const struct tcf_t *tm)
 {
 	int hz = get_user_hz();
@@ -790,7 +836,6 @@ void print_tm(FILE *f, const struct tcf_t *tm)
 void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 			 const char *prefix, struct rtattr **xstats)
 {
-	SPRINT_BUF(b1);
 	struct rtattr *tbs[TCA_STATS_MAX + 1];
 
 	parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
@@ -821,21 +866,15 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 		       MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
 			   sizeof(re)));
 		print_string(PRINT_FP, NULL, "\n%s", prefix);
-		print_lluint(PRINT_JSON, "rate", NULL, re.bps);
-		print_string(PRINT_FP, NULL, "rate %s",
-			     sprint_rate(re.bps, b1));
+		print_rate("rate", "rate %s", re.bps);
 		print_lluint(PRINT_ANY, "pps", " %llupps", re.pps);
 	} else if (tbs[TCA_STATS_RATE_EST]) {
 		struct gnet_stats_rate_est re = {0};
 
 		memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
 		       MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
-		fprintf(fp, "\n%srate %s %upps ",
-			prefix, sprint_rate(re.bps, b1), re.pps);
 		print_string(PRINT_FP, NULL, "\n%s", prefix);
-		print_uint(PRINT_JSON, "rate", NULL, re.bps);
-		print_string(PRINT_FP, NULL, "rate %s",
-			     sprint_rate(re.bps, b1));
+		print_rate("rate", "rate %s", re.bps);
 		print_uint(PRINT_ANY, "pps", " %upps", re.pps);
 	}
 
@@ -845,9 +884,7 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 		memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
 		if (!tbs[TCA_STATS_RATE_EST])
 			print_string(PRINT_FP, NULL, "\n%s", prefix);
-		print_uint(PRINT_JSON, "backlog", NULL, q.backlog);
-		print_string(PRINT_FP, NULL, "backlog %s",
-			     sprint_size(q.backlog, b1));
+		print_size("backlog",  "backlog %s", q.backlog);
 		print_uint(PRINT_ANY, "qlen", " %up", q.qlen);
 		print_uint(PRINT_FP, NULL, " requeues %u", q.requeues);
 	}
@@ -859,8 +896,6 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
 void print_tcstats_attr(FILE *fp, struct rtattr *tb[],
 			const char *prefix, struct rtattr **xstats)
 {
-	SPRINT_BUF(b1);
-
 	if (tb[TCA_STATS2]) {
 		print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
 		if (xstats && NULL == *xstats)
@@ -885,26 +920,14 @@ void print_tcstats_attr(FILE *fp, struct rtattr *tb[],
 
 			if (st.bps || st.pps) {
 				print_string(PRINT_FP, NULL, "%s", "rate ");
-				print_uint(PRINT_JSON, "rate", NULL, st.bps);
-				if (st.bps)
-					print_string(PRINT_FP, NULL, "%s ",
-						     sprint_rate(st.bps, b1));
-
-				print_uint(PRINT_JSON, "pps", NULL, st.pps);
-				if (st.pps)
-					print_uint(PRINT_FP, NULL, "%upps ", st.pps);
+				print_rate("rate", "%s ", st.bps);
+				print_uint(PRINT_JSON, "pps", "%upps ", st.pps);
 			}
 			if (st.qlen || st.backlog) {
 				print_string(PRINT_FP, NULL, "%s", "backlog ");
 
-				print_uint(PRINT_JSON, "backlog", NULL, st.backlog);
-				print_uint(PRINT_JSON, "qlen", NULL, st.qlen);
-				if (st.backlog)
-					print_string(PRINT_FP, NULL,
-						     "%s ", sprint_size(st.backlog, b1));
-				if (st.qlen)
-					print_uint(PRINT_FP, NULL,
-						     "%up ", st.qlen);
+				print_size("backlog", "%s ", st.backlog);
+				print_uint(PRINT_ANY, "qlen", "%up ", st.qlen);
 			}
 		}
 	}
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 1690ad262351..e83bb7dbcc76 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -87,6 +87,12 @@ int get_linklayer(unsigned int *val, const char *arg);
 void print_qdisc_handle(char *buf, int len, __u32 h);
 void print_devname(enum output_type type, int ifindex);
 
+void print_rate(const char *key, const char *fmt, __u64 rate);
+void print_size(const char *key, const char *fmt, __u32 sz);
+void print_time(const char *key, const char *fmt, __s32 tm);
+void print_ticks(const char *key, const char *fmt, __u32 ticks);
+void print_linklayer(const char *key, const char *fmt, unsigned int linklayer);
+
 char *sprint_rate(__u64 rate, char *buf);
 char *sprint_size(__u32 size, char *buf);
 char *sprint_qdisc_handle(__u32 h, char *buf);
-- 
2.18.0

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ