Skip to contentSkip to conversation

TC more JSON support

PATCH Superseded Stephen Hemminger 3 participants 13 messages Jul 9, 2018 – Jul 13, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover TC more JSON support

From: Stephen Hemminger <[email protected]>

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(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH 1/9 tc: use JSON in error handling

From: Stephen Hemminger <[email protected]>

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

---
 tc/tc.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
tc/tc.c +11 -8
index 0d223281ba25..b6d2a6151793 100644
@@ -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;
 }
 
Signature · 1 line
2.18.0
#2 lore raw

PATCH 2/9 tc: use const char in util

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

For prefix and path, can use const char.

---
 tc/tc_util.c | 8 +++++---
 tc/tc_util.h | 6 +++---
 2 files changed, 8 insertions(+), 6 deletions(-)
tc/tc_util.c +5 -3
index e0c96291ade0..e8e1241d083d 100644
@@ -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);
 
tc/tc_util.h +3 -3
index 6632c4f9c528..64b309903c69 100644
@@ -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);
Signature · 1 line
2.18.0
#3 lore raw

PATCH 3/9 tc: convert stats print to json

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

Convert compatiablity statistics to print in JSON.

---
 tc/tc_util.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)
tc/tc_util.c +22 -10
index e8e1241d083d..05b6c97563b3 100644
@@ -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);
 			}
 		}
 	}
Signature · 1 line
2.18.0
#4 lore raw

PATCH 4/9 tc/cbq: use sprint_rate

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

All other places in tc use sprint_rate.

---
 tc/q_cbq.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
tc/q_cbq.c +6 -9
index e7f1a3bfaf5d..ad0170c41858 100644
@@ -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);
 		}
Signature · 1 line
2.18.0
#5 lore raw

PATCH 5/9 tc/util: remove print_rate

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

This function is not used, only sprint_rate is used.

---
 tc/tc_util.c | 10 +++-------
 tc/tc_util.h |  1 -
 2 files changed, 3 insertions(+), 8 deletions(-)
tc/tc_util.c +3 -7
index 05b6c97563b3..f5ffe3443892 100644
@@ -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;
 }
 
tc/tc_util.h +0 -1
index 64b309903c69..56e214cbc8de 100644
@@ -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);
Signature · 1 line
2.18.0
#6 lore raw

PATCH 6/9 tc/util: remove unused print_size

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

Only sprint_size is used, so fold it in.

---
 tc/tc_util.c | 7 ++-----
 tc/tc_util.h | 1 -
 2 files changed, 2 insertions(+), 6 deletions(-)
tc/tc_util.c +2 -5
index f5ffe3443892..01e131b5c5d7 100644
@@ -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;
 }
 
tc/tc_util.h +0 -1
index 56e214cbc8de..01c6a09a8839 100644
@@ -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);
Signature · 1 line
2.18.0
#7 lore raw

PATCH 7/9 tc/util: remove unused print_time

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

Only sprint_time is used.

---
 tc/tc_util.c | 8 ++------
 tc/tc_util.h | 1 -
 2 files changed, 2 insertions(+), 7 deletions(-)
tc/tc_util.c +2 -6
index 01e131b5c5d7..95cb49b98612 100644
@@ -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;
 }
 
tc/tc_util.h +0 -1
index 01c6a09a8839..16babd21b473 100644
@@ -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);
 
Signature · 1 line
2.18.0
#8 lore raw

PATCH 8/9 tc/util: add print helpers for JSON

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

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

---
 tc/tc_util.c | 59 ++++++++++++++++++++++++++--------------------------
 tc/tc_util.h |  2 ++
 2 files changed, 31 insertions(+), 30 deletions(-)
tc/tc_util.c +29 -30
index 95cb49b98612..787c3fc8a57a 100644
@@ -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);
 			}
 		}
 	}
tc/tc_util.h +2 -0
index 16babd21b473..d6bdfe035050 100644
@@ -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);
Signature · 1 line
2.18.0
#9 lore raw

PATCH 9/9 tc/sfq: add json support

In reply to Stephen Hemminger

From: Stephen Hemminger <[email protected]>

Convert to use JSON

---
 tc/q_sfq.c | 65 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 39 insertions(+), 26 deletions(-)
tc/q_sfq.c +39 -26
index 6a1d853b7c93..cc8ce0dddf7e 100644
@@ -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;
 }
 
Signature · 1 line
2.18.0
#10 lore raw
In reply to Stephen Hemminger
Quoted 17 lines · Stephen Hemminger

From: Stephen Hemminger <[email protected]>

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

Quoted 7 lines
 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.

#11 lore raw
Quoted 26 lines · David Ahern
Quoted 17 lines · Stephen Hemminger

From: Stephen Hemminger <[email protected]>

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

Quoted 7 lines
 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(-)
Quoted 6 lines

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!

#12 lore raw
Quoted 30 lines · On Fri, 13 Jul 2018 14:29:42 -0700 Jakub Kicinski…
Quoted 29 lines · David Ahern
Quoted 17 lines · Stephen Hemminger

From: Stephen Hemminger <[email protected]>

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

Quoted 7 lines
 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.

Quoted 6 lines
Quoted 3 lines

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