netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2-next 0/9] TC more JSON support
@ 2018-07-09 19:48 Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 1/9] tc: use JSON in error handling Stephen Hemminger
                   ` (9 more replies)
  0 siblings, 10 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Update core of TC command and library to do more JSON.
Most of this patch set is about getting tc utility functions
to be more friendly to the json_print infrastructure.

Stephen Hemminger (9):
  tc: use JSON in error handling
  tc: use const char in util
  tc: convert stats print to json
  tc/cbq: use sprint_rate
  tc/util: remove print_rate
  tc/util: remove unused print_size
  tc/util: remove unused print_time
  tc/util: add print helpers for JSON
  tc/sfq: add json support

 tc/q_cbq.c   | 15 ++++-----
 tc/q_sfq.c   | 65 +++++++++++++++++++++---------------
 tc/tc.c      | 19 ++++++-----
 tc/tc_util.c | 94 +++++++++++++++++++++++++++-------------------------
 tc/tc_util.h | 11 +++---
 5 files changed, 109 insertions(+), 95 deletions(-)

-- 
2.18.0

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 1/9] tc: use JSON in error handling
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 2/9] tc: use const char in util Stephen Hemminger
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

If option or qdisc is unknown, still output in JSON.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 tc/tc.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/tc/tc.c b/tc/tc.c
index 0d223281ba25..b6d2a6151793 100644
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -56,9 +56,11 @@ static struct filter_util *filter_list;
 static int print_noqopt(struct qdisc_util *qu, FILE *f,
 			struct rtattr *opt)
 {
-	if (opt && RTA_PAYLOAD(opt))
-		fprintf(f, "[Unknown qdisc, optlen=%u] ",
-			(unsigned int) RTA_PAYLOAD(opt));
+	if (opt && RTA_PAYLOAD(opt)) {
+		print_string(PRINT_JSON, "qdisc", NULL, "UNKNOWN");
+		print_uint(PRINT_ANY, "optlen", "[Unknown qdisc, optlen=%u] ",
+			   RTA_PAYLOAD(opt));
+	}
 	return 0;
 }
 
@@ -76,11 +78,12 @@ static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv,
 
 static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
 {
-	if (opt && RTA_PAYLOAD(opt))
-		fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
-			fhandle, (unsigned int) RTA_PAYLOAD(opt));
-	else if (fhandle)
-		fprintf(f, "fh %08x ", fhandle);
+	if (opt && RTA_PAYLOAD(opt)) {
+		print_0xhex(PRINT_ANY, "handle", "fh %08x ", fhandle);
+		print_uint(PRINT_ANY, "optlen", "[Unknown filter, optlen=%u] ",
+			RTA_PAYLOAD(opt));
+	} else if (fhandle)
+		print_0xhex(PRINT_ANY, "handle", "fh %08x ", fhandle);
 	return 0;
 }
 
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 2/9] tc: use const char in util
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 1/9] tc: use JSON in error handling Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 3/9] tc: convert stats print to json Stephen Hemminger
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

For prefix and path, can use const char.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 tc/tc_util.c | 8 +++++---
 tc/tc_util.h | 6 +++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index e0c96291ade0..e8e1241d083d 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -35,7 +35,7 @@ static struct db_names *cls_names;
 
 #define NAMES_DB "/etc/iproute2/tc_cls"
 
-int cls_names_init(char *path)
+int cls_names_init(const char *path)
 {
 	int ret;
 
@@ -800,7 +800,8 @@ void print_tm(FILE *f, const struct tcf_t *tm)
 	}
 }
 
-void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
+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];
@@ -868,7 +869,8 @@ void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtat
 		*xstats = tbs[TCA_STATS_APP] ? : NULL;
 }
 
-void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
+void print_tcstats_attr(FILE *fp, struct rtattr *tb[],
+			const char *prefix, struct rtattr **xstats)
 {
 	SPRINT_BUF(b1);
 
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 6632c4f9c528..64b309903c69 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -100,9 +100,9 @@ char *sprint_ticks(__u32 ticks, char *buf);
 char *sprint_linklayer(unsigned int linklayer, char *buf);
 
 void print_tcstats_attr(FILE *fp, struct rtattr *tb[],
-			char *prefix, struct rtattr **xstats);
+			const char *prefix, struct rtattr **xstats);
 void print_tcstats2_attr(FILE *fp, struct rtattr *rta,
-			 char *prefix, struct rtattr **xstats);
+			 const char *prefix, struct rtattr **xstats);
 
 int get_tc_classid(__u32 *h, const char *str);
 int print_tc_classid(char *buf, int len, __u32 h);
@@ -130,7 +130,7 @@ int parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n);
 void print_tm(FILE *f, const struct tcf_t *tm);
 int prio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt);
 
-int cls_names_init(char *path);
+int cls_names_init(const char *path);
 void cls_names_uninit(void);
 
 int action_a2n(char *arg, int *result, bool allow_num);
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 3/9] tc: convert stats print to json
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 1/9] tc: use JSON in error handling Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 2/9] tc: use const char in util Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 4/9] tc/cbq: use sprint_rate Stephen Hemminger
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Convert compatiablity statistics to print in JSON.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 tc/tc_util.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index e8e1241d083d..05b6c97563b3 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -887,25 +887,37 @@ void print_tcstats_attr(FILE *fp, struct rtattr *tb[],
 		/* handle case where kernel returns more/less than we know about */
 		memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
 
-		fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
-			prefix, (unsigned long long)st.bytes, st.packets, st.drops,
-			st.overlimits);
+		print_string(PRINT_FP, NULL, "%sSent ", prefix);
+		print_u64(PRINT_ANY, "bytes", "%llu bytes ", (unsigned long long)st.bytes);
+		print_uint(PRINT_ANY, "packets", "%u pkts ", st.packets);
+		print_uint(PRINT_ANY, "dropped", "(dropped %u, ", st.drops);
+		print_uint(PRINT_ANY, "overlimits", "overlimits %u) ", st.overlimits);
 
 		if (st.bps || st.pps || st.qlen || st.backlog) {
-			fprintf(fp, "\n%s", prefix);
+			print_string(PRINT_FP, "%s%s", _SL_, prefix);
+
 			if (st.bps || st.pps) {
-				fprintf(fp, "rate ");
+				print_string(PRINT_FP, NULL, "%s", "rate ");
+				print_uint(PRINT_JSON, "rate", NULL, st.bps);
 				if (st.bps)
-					fprintf(fp, "%s ", sprint_rate(st.bps, b1));
+					print_string(PRINT_FP, NULL, "%s ",
+						     sprint_rate(st.bps, b1));
+
+				print_uint(PRINT_JSON, "pps", NULL, st.pps);
 				if (st.pps)
-					fprintf(fp, "%upps ", st.pps);
+					print_uint(PRINT_FP, NULL, "%upps ", st.pps);
 			}
 			if (st.qlen || st.backlog) {
-				fprintf(fp, "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)
-					fprintf(fp, "%s ", sprint_size(st.backlog, b1));
+					print_string(PRINT_FP, NULL,
+						     "%s ", sprint_size(st.backlog, b1));
 				if (st.qlen)
-					fprintf(fp, "%up ", st.qlen);
+					print_uint(PRINT_FP, NULL,
+						     "%up ", st.qlen);
 			}
 		}
 	}
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 4/9] tc/cbq: use sprint_rate
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
                   ` (2 preceding siblings ...)
  2018-07-09 19:48 ` [PATCH iproute2-next 3/9] tc: convert stats print to json Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 5/9] tc/util: remove print_rate Stephen Hemminger
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

All other places in tc use sprint_rate.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 tc/q_cbq.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/tc/q_cbq.c b/tc/q_cbq.c
index e7f1a3bfaf5d..ad0170c41858 100644
--- a/tc/q_cbq.c
+++ b/tc/q_cbq.c
@@ -495,10 +495,9 @@ static int cbq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 	}
 
 	if (r) {
-		char buf[64];
 
-		print_rate(buf, sizeof(buf), r->rate);
-		fprintf(f, "rate %s ", buf);
+		fprintf(f, "rate %s ",
+			sprint_rate(r->rate, b1));
 		linklayer = (r->linklayer & TC_LINKLAYER_MASK);
 		if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
 			fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2));
@@ -530,14 +529,12 @@ static int cbq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 			fprintf(f, "prio %u", wrr->priority);
 		else
 			fprintf(f, "prio no-transmit");
-		if (show_details) {
-			char buf[64];
 
+		if (show_details) {
 			fprintf(f, "/%u ", wrr->cpriority);
-			if (wrr->weight != 1) {
-				print_rate(buf, sizeof(buf), wrr->weight);
-				fprintf(f, "weight %s ", buf);
-			}
+			if (wrr->weight != 1)
+				fprintf(f, "weight %s ",
+					sprint_rate(wrr->weight, b1));
 			if (wrr->allot)
 				fprintf(f, "allot %ub ", wrr->allot);
 		}
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 5/9] tc/util: remove print_rate
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
                   ` (3 preceding siblings ...)
  2018-07-09 19:48 ` [PATCH iproute2-next 4/9] tc/cbq: use sprint_rate Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 6/9] tc/util: remove unused print_size Stephen Hemminger
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

This function is not used, only sprint_rate is used.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 tc/tc_util.c | 10 +++-------
 tc/tc_util.h |  1 -
 2 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index 05b6c97563b3..f5ffe3443892 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -307,7 +307,7 @@ int get_rate64(__u64 *rate, const char *str)
 	return 0;
 }
 
-void print_rate(char *buf, int len, __u64 rate)
+char *sprint_rate(__u64 rate, char *buf)
 {
 	extern int use_iec;
 	unsigned long kilo = use_iec ? 1024 : 1000;
@@ -325,12 +325,8 @@ void print_rate(char *buf, int len, __u64 rate)
 		rate /= kilo;
 	}
 
-	snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
-}
-
-char *sprint_rate(__u64 rate, char *buf)
-{
-	print_rate(buf, SPRINT_BSIZE-1, rate);
+	snprintf(buf, SPRINT_BSIZE-1,
+		 "%.0f%s%sbit", (double)rate, units[i], str);
 	return buf;
 }
 
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 64b309903c69..56e214cbc8de 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -84,7 +84,6 @@ int get_size_and_cell(unsigned int *size, int *cell_log, char *str);
 int get_time(unsigned int *time, const char *str);
 int get_linklayer(unsigned int *val, const char *arg);
 
-void print_rate(char *buf, int len, __u64 rate);
 void print_size(char *buf, int len, __u32 size);
 void print_qdisc_handle(char *buf, int len, __u32 h);
 void print_time(char *buf, int len, __u32 time);
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 6/9] tc/util: remove unused print_size
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
                   ` (4 preceding siblings ...)
  2018-07-09 19:48 ` [PATCH iproute2-next 5/9] tc/util: remove print_rate Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 7/9] tc/util: remove unused print_time Stephen Hemminger
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Only sprint_size is used, so fold it in.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 tc/tc_util.c | 7 ++-----
 tc/tc_util.h | 1 -
 2 files changed, 2 insertions(+), 6 deletions(-)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index f5ffe3443892..01e131b5c5d7 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -451,8 +451,9 @@ void print_devname(enum output_type type, int ifindex)
 			   "dev", "%s ", ifname);
 }
 
-void print_size(char *buf, int len, __u32 sz)
+char *sprint_size(__u32 sz, char *buf)
 {
+	const size_t len = SPRINT_BSIZE - 1;
 	double tmp = sz;
 
 	if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
@@ -461,11 +462,7 @@ void print_size(char *buf, int len, __u32 sz)
 		snprintf(buf, len, "%gKb", rint(tmp/1024));
 	else
 		snprintf(buf, len, "%ub", sz);
-}
 
-char *sprint_size(__u32 size, char *buf)
-{
-	print_size(buf, SPRINT_BSIZE-1, size);
 	return buf;
 }
 
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 56e214cbc8de..01c6a09a8839 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -84,7 +84,6 @@ int get_size_and_cell(unsigned int *size, int *cell_log, char *str);
 int get_time(unsigned int *time, const char *str);
 int get_linklayer(unsigned int *val, const char *arg);
 
-void print_size(char *buf, int len, __u32 size);
 void print_qdisc_handle(char *buf, int len, __u32 h);
 void print_time(char *buf, int len, __u32 time);
 void print_linklayer(char *buf, int len, unsigned int linklayer);
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 7/9] tc/util: remove unused print_time
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
                   ` (5 preceding siblings ...)
  2018-07-09 19:48 ` [PATCH iproute2-next 6/9] tc/util: remove unused print_size Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 8/9] tc/util: add print helpers for JSON Stephen Hemminger
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Only sprint_time is used.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 tc/tc_util.c | 8 ++------
 tc/tc_util.h | 1 -
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index 01e131b5c5d7..95cb49b98612 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -357,9 +357,9 @@ int get_time(unsigned int *time, const char *str)
 	return 0;
 }
 
-
-void print_time(char *buf, int len, __u32 time)
+char *sprint_time(__u32 time, char *buf)
 {
+	const size_t len = SPRINT_BSIZE - 1;
 	double tmp = time;
 
 	if (tmp >= TIME_UNITS_PER_SEC)
@@ -368,11 +368,7 @@ void print_time(char *buf, int len, __u32 time)
 		snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
 	else
 		snprintf(buf, len, "%uus", time);
-}
 
-char *sprint_time(__u32 time, char *buf)
-{
-	print_time(buf, SPRINT_BSIZE-1, time);
 	return buf;
 }
 
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 01c6a09a8839..16babd21b473 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -85,7 +85,6 @@ int get_time(unsigned int *time, const char *str);
 int get_linklayer(unsigned int *val, const char *arg);
 
 void print_qdisc_handle(char *buf, int len, __u32 h);
-void print_time(char *buf, int len, __u32 time);
 void print_linklayer(char *buf, int len, unsigned int linklayer);
 void print_devname(enum output_type type, int ifindex);
 
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 8/9] tc/util: add print helpers for JSON
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
                   ` (6 preceding siblings ...)
  2018-07-09 19:48 ` [PATCH iproute2-next 7/9] tc/util: remove unused print_time Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-09 19:48 ` [PATCH iproute2-next 9/9] tc/sfq: add json support Stephen Hemminger
  2018-07-13 20:48 ` [PATCH iproute2-next 0/9] TC more JSON support David Ahern
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger, Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

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

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

diff --git a/tc/tc_util.c b/tc/tc_util.c
index 95cb49b98612..787c3fc8a57a 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;
@@ -462,6 +475,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);
@@ -792,7 +814,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);
@@ -823,21 +844,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);
 	}
 
@@ -847,9 +862,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);
 	}
@@ -861,8 +874,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)
@@ -887,26 +898,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 16babd21b473..d6bdfe035050 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -88,6 +88,8 @@ void print_qdisc_handle(char *buf, int len, __u32 h);
 void print_linklayer(char *buf, int len, unsigned int linklayer);
 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);
 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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH iproute2-next 9/9] tc/sfq: add json support
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
                   ` (7 preceding siblings ...)
  2018-07-09 19:48 ` [PATCH iproute2-next 8/9] tc/util: add print helpers for JSON Stephen Hemminger
@ 2018-07-09 19:48 ` Stephen Hemminger
  2018-07-13 20:48 ` [PATCH iproute2-next 0/9] TC more JSON support David Ahern
  9 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-09 19:48 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger, Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Convert to use JSON

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 tc/q_sfq.c | 65 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 39 insertions(+), 26 deletions(-)

diff --git a/tc/q_sfq.c b/tc/q_sfq.c
index 6a1d853b7c93..cc8ce0dddf7e 100644
--- a/tc/q_sfq.c
+++ b/tc/q_sfq.c
@@ -205,9 +205,6 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 	struct tc_sfq_qopt *qopt;
 	struct tc_sfq_qopt_v1 *qopt_ext = NULL;
 
-	SPRINT_BUF(b1);
-	SPRINT_BUF(b2);
-	SPRINT_BUF(b3);
 	if (opt == NULL)
 		return 0;
 
@@ -216,36 +213,53 @@ static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 	if (RTA_PAYLOAD(opt) >= sizeof(*qopt_ext))
 		qopt_ext = RTA_DATA(opt);
 	qopt = RTA_DATA(opt);
-	fprintf(f, "limit %up ", qopt->limit);
-	fprintf(f, "quantum %s ", sprint_size(qopt->quantum, b1));
+
+	print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit);
+	print_size("quantum", "quantum %s ", qopt->quantum);
+	
 	if (qopt_ext && qopt_ext->depth)
-		fprintf(f, "depth %u ", qopt_ext->depth);
+		print_uint(PRINT_ANY, "depth",
+			   "depth %u ", qopt_ext->depth);
 	if (qopt_ext && qopt_ext->headdrop)
-		fprintf(f, "headdrop ");
+		print_null(PRINT_ANY, "headdrop", "headdrop ", NULL);
 
 	if (show_details) {
-		fprintf(f, "flows %u/%u ", qopt->flows, qopt->divisor);
+		print_uint(PRINT_ANY, "flows", "flows %u", qopt->flows);
+		print_uint(PRINT_ANY, "divisor", "/%u ", qopt->divisor);
+	} else {
+		print_uint(PRINT_ANY, "divisor", "divisor %u ", qopt->divisor);
 	}
-	fprintf(f, "divisor %u ", qopt->divisor);
+
 	if (qopt->perturb_period)
-		fprintf(f, "perturb %dsec ", qopt->perturb_period);
+		print_int(PRINT_ANY, "perturb",
+			  "perturb %dsec ", qopt->perturb_period);
+
 	if (qopt_ext && qopt_ext->qth_min) {
-		fprintf(f, "\n ewma %u ", qopt_ext->Wlog);
-		fprintf(f, "min %s max %s probability %g ",
-			sprint_size(qopt_ext->qth_min, b2),
-			sprint_size(qopt_ext->qth_max, b3),
-			qopt_ext->max_P / pow(2, 32));
+		print_string(PRINT_FP, NULL, "%s", _SL_);
+		print_uint(PRINT_ANY, "ewma", " ewma %u ", qopt_ext->Wlog);
+		print_size("qth_min", "min %s" , qopt_ext->qth_min);
+		print_size("qth_max", "max %s ", qopt_ext->qth_max);
+		print_float(PRINT_ANY, "probability", "probability %g ",
+			    qopt_ext->max_P / pow(2, 32));
+
 		if (qopt_ext->flags & TC_RED_ECN)
-			fprintf(f, "ecn ");
+			print_null(PRINT_ANY, "ecn", "ecn ", NULL);
+
 		if (show_stats) {
-			fprintf(f, "\n prob_mark %u prob_mark_head %u prob_drop %u",
-				qopt_ext->stats.prob_mark,
-				qopt_ext->stats.prob_mark_head,
-				qopt_ext->stats.prob_drop);
-			fprintf(f, "\n forced_mark %u forced_mark_head %u forced_drop %u",
-				qopt_ext->stats.forced_mark,
-				qopt_ext->stats.forced_mark_head,
-				qopt_ext->stats.forced_drop);
+			print_string(PRINT_FP, NULL, "%s", _SL_);
+			print_uint(PRINT_ANY, "prob_mark", " prob_mark %u",
+				   qopt_ext->stats.prob_mark);
+			print_uint(PRINT_ANY, "prob_mark_head", " prob_mark_head %u",
+				   qopt_ext->stats.prob_mark_head);
+			print_uint(PRINT_ANY, "prob_drop"," prob_drop %u",
+				   qopt_ext->stats.prob_drop);
+			print_string(PRINT_FP, NULL, "%s", _SL_);
+			print_uint(PRINT_ANY, "forced_mark", " forced_mark %u",
+				   qopt_ext->stats.forced_mark);
+			print_uint(PRINT_ANY, "forced_mark_head", " forced_mark_head %u",
+				   qopt_ext->stats.forced_mark_head);
+			print_uint(PRINT_ANY, "forced_drop"," forced_drop %u",
+				   qopt_ext->stats.forced_drop);
 		}
 	}
 	return 0;
@@ -262,8 +276,7 @@ static int sfq_print_xstats(struct qdisc_util *qu, FILE *f,
 		return -1;
 	st = RTA_DATA(xstats);
 
-	fprintf(f, " allot %d ", st->allot);
-	fprintf(f, "\n");
+	print_int(PRINT_ANY, "allot", "allot %d\n", st->allot);
 	return 0;
 }
 
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH iproute2-next 0/9] TC more JSON support
  2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
                   ` (8 preceding siblings ...)
  2018-07-09 19:48 ` [PATCH iproute2-next 9/9] tc/sfq: add json support Stephen Hemminger
@ 2018-07-13 20:48 ` David Ahern
  2018-07-13 21:29   ` Jakub Kicinski
  9 siblings, 1 reply; 13+ messages in thread
From: David Ahern @ 2018-07-13 20:48 UTC (permalink / raw)
  To: Stephen Hemminger, netdev
  Cc: Stephen Hemminger, Jiri Pirko, Jakub Kicinski, Cong Wang,
	Jamal Hadi Salim

On 7/9/18 3:48 PM, Stephen Hemminger wrote:
> From: Stephen Hemminger <sthemmin@microsoft.com>
> 
> Update core of TC command and library to do more JSON.
> Most of this patch set is about getting tc utility functions
> to be more friendly to the json_print infrastructure.
> 
> Stephen Hemminger (9):
>   tc: use JSON in error handling
>   tc: use const char in util
>   tc: convert stats print to json
>   tc/cbq: use sprint_rate
>   tc/util: remove print_rate
>   tc/util: remove unused print_size
>   tc/util: remove unused print_time
>   tc/util: add print helpers for JSON
>   tc/sfq: add json support
> 
>  tc/q_cbq.c   | 15 ++++-----
>  tc/q_sfq.c   | 65 +++++++++++++++++++++---------------
>  tc/tc.c      | 19 ++++++-----
>  tc/tc_util.c | 94 +++++++++++++++++++++++++++-------------------------
>  tc/tc_util.h | 11 +++---
>  5 files changed, 109 insertions(+), 95 deletions(-)
> 

I'd prefer some tc folks to take a look at the json output and verify
everything is good.

Jamal, Jiri, Jakub, Cong, others?

There is a second set with 30 patches as well.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH iproute2-next 0/9] TC more JSON support
  2018-07-13 20:48 ` [PATCH iproute2-next 0/9] TC more JSON support David Ahern
@ 2018-07-13 21:29   ` Jakub Kicinski
  2018-07-13 22:22     ` Stephen Hemminger
  0 siblings, 1 reply; 13+ messages in thread
From: Jakub Kicinski @ 2018-07-13 21:29 UTC (permalink / raw)
  To: David Ahern
  Cc: Stephen Hemminger, netdev, Stephen Hemminger, Jiri Pirko,
	Cong Wang, Jamal Hadi Salim

On Fri, 13 Jul 2018 16:48:28 -0400, David Ahern wrote:
> On 7/9/18 3:48 PM, Stephen Hemminger wrote:
> > From: Stephen Hemminger <sthemmin@microsoft.com>
> > 
> > Update core of TC command and library to do more JSON.
> > Most of this patch set is about getting tc utility functions
> > to be more friendly to the json_print infrastructure.
> > 
> > Stephen Hemminger (9):
> >   tc: use JSON in error handling
> >   tc: use const char in util
> >   tc: convert stats print to json
> >   tc/cbq: use sprint_rate
> >   tc/util: remove print_rate
> >   tc/util: remove unused print_size
> >   tc/util: remove unused print_time
> >   tc/util: add print helpers for JSON
> >   tc/sfq: add json support
> > 
> >  tc/q_cbq.c   | 15 ++++-----
> >  tc/q_sfq.c   | 65 +++++++++++++++++++++---------------
> >  tc/tc.c      | 19 ++++++-----
> >  tc/tc_util.c | 94 +++++++++++++++++++++++++++-------------------------
> >  tc/tc_util.h | 11 +++---
> >  5 files changed, 109 insertions(+), 95 deletions(-)
> >   
> 
> I'd prefer some tc folks to take a look at the json output and verify
> everything is good.
> 
> Jamal, Jiri, Jakub, Cong, others?
> 
> There is a second set with 30 patches as well.

AFAIK the 31 patches supersede this set?  I only have tests that use
JSON for MQ and RED qdiscs, and those work fine with the v2 applied!

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH iproute2-next 0/9] TC more JSON support
  2018-07-13 21:29   ` Jakub Kicinski
@ 2018-07-13 22:22     ` Stephen Hemminger
  0 siblings, 0 replies; 13+ messages in thread
From: Stephen Hemminger @ 2018-07-13 22:22 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: David Ahern, netdev, Stephen Hemminger, Jiri Pirko, Cong Wang,
	Jamal Hadi Salim

On Fri, 13 Jul 2018 14:29:42 -0700
Jakub Kicinski <jakub.kicinski@netronome.com> wrote:

> On Fri, 13 Jul 2018 16:48:28 -0400, David Ahern wrote:
> > On 7/9/18 3:48 PM, Stephen Hemminger wrote:  
> > > From: Stephen Hemminger <sthemmin@microsoft.com>
> > > 
> > > Update core of TC command and library to do more JSON.
> > > Most of this patch set is about getting tc utility functions
> > > to be more friendly to the json_print infrastructure.
> > > 
> > > Stephen Hemminger (9):
> > >   tc: use JSON in error handling
> > >   tc: use const char in util
> > >   tc: convert stats print to json
> > >   tc/cbq: use sprint_rate
> > >   tc/util: remove print_rate
> > >   tc/util: remove unused print_size
> > >   tc/util: remove unused print_time
> > >   tc/util: add print helpers for JSON
> > >   tc/sfq: add json support
> > > 
> > >  tc/q_cbq.c   | 15 ++++-----
> > >  tc/q_sfq.c   | 65 +++++++++++++++++++++---------------
> > >  tc/tc.c      | 19 ++++++-----
> > >  tc/tc_util.c | 94 +++++++++++++++++++++++++++-------------------------
> > >  tc/tc_util.h | 11 +++---
> > >  5 files changed, 109 insertions(+), 95 deletions(-)
> > >     
> > 
> > I'd prefer some tc folks to take a look at the json output and verify
> > everything is good.
> > 
> > Jamal, Jiri, Jakub, Cong, others?
> > 
> > There is a second set with 30 patches as well.  
> 
> AFAIK the 31 patches supersede this set?  I only have tests that use
> JSON for MQ and RED qdiscs, and those work fine with the v2 applied!

This is what I used to test the trivial ones.  Some require more parameters.

#! /bin/sh

for q in cbq cbs choke clsact codel drr dsmark fifo \
	fq fq_codel gred hfsc hhf htb ingress mqprio \
	multiq netem pie prio qfq red rr sfb sfq tbf
do 
    echo -n $q ": "
    sudo tc qdisc add dev dummy0 root $q 2>/dev/null
    if [ $? -ne 0 ]; then
	echo "can not test"
    else
	/sbin/tc qdisc show dev dummy0 >$q.orig
	./tc/tc qdisc show dev dummy0 >$q.new
	if cmp $q.new $q.orig; then
		echo -n "ok"
	else
		echo -n "differ"
	fi
	./tc/tc -j qdisc show dev dummy0 >$q.json
	if python -mjson.tool <$q.json >/dev/null; then
		echo " json ok"
	else
		echo " json format error"
	fi

	sudo tc qdisc del dev dummy0 root
    fi
done

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-07-13 22:38 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-09 19:48 [PATCH iproute2-next 0/9] TC more JSON support Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 1/9] tc: use JSON in error handling Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 2/9] tc: use const char in util Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 3/9] tc: convert stats print to json Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 4/9] tc/cbq: use sprint_rate Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 5/9] tc/util: remove print_rate Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 6/9] tc/util: remove unused print_size Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 7/9] tc/util: remove unused print_time Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 8/9] tc/util: add print helpers for JSON Stephen Hemminger
2018-07-09 19:48 ` [PATCH iproute2-next 9/9] tc/sfq: add json support Stephen Hemminger
2018-07-13 20:48 ` [PATCH iproute2-next 0/9] TC more JSON support David Ahern
2018-07-13 21:29   ` Jakub Kicinski
2018-07-13 22:22     ` Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).