]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
tc: jsonify qdisc core
authorJiri Pirko <jiri@mellanox.com>
Sat, 25 Nov 2017 14:48:25 +0000 (15:48 +0100)
committerStephen Hemminger <stephen@networkplumber.org>
Sun, 26 Nov 2017 20:20:51 +0000 (12:20 -0800)
Add json output to qdisc core.

Signed-off-by: Jiri Pirko <jiri@mellanox.com>
tc/tc.c
tc/tc_qdisc.c
tc/tc_util.h

diff --git a/tc/tc.c b/tc/tc.c
index 793dca9f9e09d4b88b6f3f6308297f5f28a12bac..ad9f07e9786de0e93c1d2b66d194b6a6c44a89bb 100644 (file)
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -41,6 +41,7 @@ int batch_mode;
 int use_iec;
 int force;
 bool use_names;
+int json;
 
 static char *conf_file;
 
@@ -189,7 +190,7 @@ static void usage(void)
                        "       tc [-force] -batch filename\n"
                        "where  OBJECT := { qdisc | class | filter | action | monitor | exec }\n"
                        "       OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] | -n[etns] name |\n"
-                       "                    -nm | -nam[es] | { -cf | -conf } path }\n");
+                       "                    -nm | -nam[es] | { -cf | -conf } path } | -j[son]\n");
 }
 
 static int do_cmd(int argc, char **argv)
@@ -312,6 +313,8 @@ int main(int argc, char **argv)
                } else if (matches(argv[1], "-tshort") == 0) {
                        ++timestamp;
                        ++timestamp_short;
+               } else if (matches(argv[1], "-json") == 0) {
+                       ++json;
                } else {
                        fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
                        return -1;
index 2f88f11cb99a238e139c651b926fb3e40c2490d0..4431d5fde3240124608cd8d38a74a8b770a73bb9 100644 (file)
@@ -227,39 +227,44 @@ int print_qdisc(const struct sockaddr_nl *who,
                return -1;
        }
 
+       open_json_object(NULL);
+
        if (n->nlmsg_type == RTM_DELQDISC)
-               fprintf(fp, "deleted ");
+               print_bool(PRINT_ANY, "deleted", "deleted ", true);
 
        if (n->nlmsg_type == RTM_NEWQDISC &&
                        (n->nlmsg_flags & NLM_F_CREATE) &&
                        (n->nlmsg_flags & NLM_F_REPLACE))
-               fprintf(fp, "replaced ");
+               print_bool(PRINT_ANY, "replaced", "replaced ", true);
 
        if (n->nlmsg_type == RTM_NEWQDISC &&
                        (n->nlmsg_flags & NLM_F_CREATE) &&
                        (n->nlmsg_flags & NLM_F_EXCL))
-               fprintf(fp, "added ");
-
-       if (show_raw)
-               fprintf(fp, "qdisc %s %x:[%08x]  ",
-                       rta_getattr_str(tb[TCA_KIND]),
-                       t->tcm_handle >> 16, t->tcm_handle);
-       else
-               fprintf(fp, "qdisc %s %x: ", rta_getattr_str(tb[TCA_KIND]),
-                       t->tcm_handle >> 16);
+               print_bool(PRINT_ANY, "added", "added ", true);
+
+       print_string(PRINT_ANY, "kind", "qdisc %s",
+                    rta_getattr_str(tb[TCA_KIND]));
+       sprintf(abuf, "%x:", t->tcm_handle >> 16);
+       print_string(PRINT_ANY, "handle", " %s", abuf);
+       if (show_raw) {
+               sprintf(abuf, "[%08x]", t->tcm_handle);
+               print_string(PRINT_FP, NULL, "%s", abuf);
+       }
+       print_string(PRINT_FP, NULL, " ", NULL);
 
        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 (t->tcm_parent) {
                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 != 1)
-               fprintf(fp, "refcnt %d ", t->tcm_info);
+               print_uint(PRINT_ANY, "refcnt", "refcnt %u ", t->tcm_info);
 
        /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
        if (strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])) == 0)
@@ -267,17 +272,21 @@ int print_qdisc(const struct sockaddr_nl *who,
        else
                q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
 
+       open_json_object("options");
        if (tb[TCA_OPTIONS]) {
                if (q)
                        q->print_qopt(q, fp, tb[TCA_OPTIONS]);
                else
-                       fprintf(fp, "[cannot parse qdisc parameters]");
+                       print_string(PRINT_FP, NULL,
+                                    "[cannot parse qdisc parameters]", NULL);
        }
-       fprintf(fp, "\n");
+       close_json_object();
+
+       print_string(PRINT_FP, NULL, "\n", NULL);
 
        if (show_details && tb[TCA_STAB]) {
                print_size_table(fp, " ", tb[TCA_STAB]);
-               fprintf(fp, "\n");
+               print_string(PRINT_FP, NULL, "\n", NULL);
        }
 
        if (show_stats) {
@@ -285,14 +294,15 @@ int print_qdisc(const struct sockaddr_nl *who,
 
                if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
                        print_tcstats_attr(fp, tb, " ", &xstats);
-                       fprintf(fp, "\n");
+                       print_string(PRINT_FP, NULL, "\n", NULL);
                }
 
                if (q && xstats && q->print_xstats) {
                        q->print_xstats(q, fp, xstats);
-                       fprintf(fp, "\n");
+                       print_string(PRINT_FP, NULL, "\n", NULL);
                }
        }
+       close_json_object();
        fflush(fp);
        return 0;
 }
@@ -360,10 +370,12 @@ static int tc_qdisc_list(int argc, char **argv)
                return 1;
        }
 
+       new_json_obj(json);
        if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
                fprintf(stderr, "Dump terminated\n");
                return 1;
        }
+       delete_json_obj();
 
        return 0;
 }
index 40a8865d6e35761ea0c083629395d0d25d9072d9..1218610d770929716851b8937a4ad475d744f576 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/gen_stats.h>
 
 #include "tc_core.h"
+#include "json_print.h"
 
 /* This is the deprecated multiqueue interface */
 #ifndef TCA_PRIO_MAX