From: Jiri Pirko <j...@mellanox.com> Add json output to qdisc core.
Signed-off-by: Jiri Pirko <j...@mellanox.com> --- tc/tc.c | 5 ++++- tc/tc_qdisc.c | 52 ++++++++++++++++++++++++++++++++-------------------- tc/tc_util.h | 1 + 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/tc/tc.c b/tc/tc.c index 793dca9..ad9f07e 100644 --- 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; diff --git a/tc/tc_qdisc.c b/tc/tc_qdisc.c index 2f88f11..4431d5f 100644 --- a/tc/tc_qdisc.c +++ b/tc/tc_qdisc.c @@ -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; } diff --git a/tc/tc_util.h b/tc/tc_util.h index eae1091..b46b9c9 100644 --- a/tc/tc_util.h +++ b/tc/tc_util.h @@ -10,6 +10,7 @@ #include <linux/gen_stats.h> #include "tc_core.h" +#include "json_print.h" /* This is the deprecated multiqueue interface */ #ifndef TCA_PRIO_MAX -- 2.9.5