diff mbox series

[iproute2,3/3] tc: jsonify class core

Message ID 20190506161840.30919-4-nir.weiner@oracle.com
State Changes Requested
Delegated to: David Ahern
Headers show
Series Adding json support for showing htb&tbf classes | expand

Commit Message

Nir Weiner May 6, 2019, 4:18 p.m. UTC
Add json output to class core.

Acked-by: John Haxby <john.haxby@oracle.com>
Reviewed-by: Liran Alon <liran.alon@oracle.com>
Signed-off-by: Nir Weiner <nir.weiner@oracle.com>
---
 tc/tc_class.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/tc/tc_class.c b/tc/tc_class.c
index 7ac700d7ab31..68627f4fcc70 100644
--- a/tc/tc_class.c
+++ b/tc/tc_class.c
@@ -333,8 +333,10 @@  int print_class(struct nlmsghdr *n, void *arg)
 		return -1;
 	}
 
+	open_json_object(NULL);
+
 	if (n->nlmsg_type == RTM_DELTCLASS)
-		fprintf(fp, "deleted ");
+		print_bool(PRINT_ANY, "deleted", "deleted ", true);
 
 	abuf[0] = 0;
 	if (t->tcm_handle) {
@@ -343,42 +345,47 @@  int print_class(struct nlmsghdr *n, void *arg)
 		else
 			print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
 	}
-	fprintf(fp, "class %s %s ", rta_getattr_str(tb[TCA_KIND]), abuf);
+
+	print_string(PRINT_ANY, "kind", "class %s ", rta_getattr_str(tb[TCA_KIND]));
+	print_string(PRINT_ANY, "classid", "%s ", abuf);
 
 	if (filter_ifindex == 0)
-		fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
+		print_string(PRINT_ANY, "dev", "dev %s ", ll_index_to_name(t->tcm_ifindex));
 
 	if (t->tcm_parent == TC_H_ROOT)
-		fprintf(fp, "root ");
+		print_bool(PRINT_ANY, "root", "root ", true);
 	else {
 		if (filter_qdisc)
 			print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
 		else
 			print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
-		fprintf(fp, "parent %s ", abuf);
+		print_string(PRINT_ANY, "parent", "parent %s ", abuf);
 	}
 	if (t->tcm_info)
-		fprintf(fp, "leaf %x: ", t->tcm_info>>16);
+		print_uint(PRINT_ANY, "leaf", "leaf %x: ", t->tcm_info>>16);
 	q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
 	if (tb[TCA_OPTIONS]) {
+		open_json_object("options");
 		if (q && q->print_copt)
 			q->print_copt(q, fp, tb[TCA_OPTIONS]);
 		else
-			fprintf(fp, "[cannot parse class parameters]");
+			print_string(PRINT_FP, NULL, "[cannot parse class parameters]", NULL);
+		close_json_object();
 	}
-	fprintf(fp, "\n");
+	print_string(PRINT_FP, NULL, "\n", NULL);
 	if (show_stats) {
 		struct rtattr *xstats = NULL;
 
 		if (tb[TCA_STATS] || tb[TCA_STATS2]) {
 			print_tcstats_attr(fp, tb, " ", &xstats);
-			fprintf(fp, "\n");
+			print_string(PRINT_FP, NULL, "\n", NULL);
 		}
 		if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) {
 			q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]);
-			fprintf(fp, "\n");
+			print_string(PRINT_FP, NULL, "\n", NULL);
 		}
 	}
+	close_json_object();
 	fflush(fp);
 	return 0;
 }
@@ -450,10 +457,12 @@  static int tc_class_list(int argc, char **argv)
 		return 1;
 	}
 
+	new_json_obj(json);
 	if (rtnl_dump_filter(&rth, print_class, stdout) < 0) {
 		fprintf(stderr, "Dump terminated\n");
 		return 1;
 	}
+	delete_json_obj();
 
 	if (show_graph)
 		graph_cls_show(stdout, &buf[0], &root_cls_list, 0);