* [iproute2-next v2 1/2] tipc: JSON support for showing nametable
@ 2018-06-11 2:16 Hoang Le
2018-06-11 2:17 ` [iproute2-next v2 2/2] tipc: JSON support for tipc link printouts Hoang Le
2018-06-11 15:05 ` [iproute2-next v2 1/2] tipc: JSON support for showing nametable Stephen Hemminger
0 siblings, 2 replies; 3+ messages in thread
From: Hoang Le @ 2018-06-11 2:16 UTC (permalink / raw)
To: netdev, tipc-discussion
Add json output support for nametable show
Example output:
$tipc -j -p nametable show
[ {
"type": 0,
"lower": 16781313,
"upper": 16781313,
"scope": "zone",
"port": 0,
"node": ""
},{
"type": 0,
"lower": 16781416,
"upper": 16781416,
"scope": "cluster",
"port": 0,
"node": ""
} ]
v2:
Replace variable 'json_flag' by 'json' declared in include/utils.h
Add new parameter '-pretty' to support pretty output
Acked-by: Jon Maloy <jon.maloy@ericsson.com>
Signed-off-by: Hoang Le <hoang.h.le@dektech.com.au>
---
tipc/nametable.c | 31 ++++++++++++++++++++++---------
tipc/tipc.c | 20 +++++++++++++++++++-
2 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/tipc/nametable.c b/tipc/nametable.c
index ae73dfa5f8b9..eb4bd0bda835 100644
--- a/tipc/nametable.c
+++ b/tipc/nametable.c
@@ -21,6 +21,7 @@
#include "msg.h"
#include "nametable.h"
#include "misc.h"
+#include "utils.h"
#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
@@ -46,7 +47,7 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data)
if (!publ[TIPC_NLA_NAME_TABLE_PUBL])
return MNL_CB_ERROR;
- if (!*iteration)
+ if (!*iteration && !is_json_context())
printf("%-10s %-10s %-10s %-8s %-10s %-33s\n",
"Type", "Lower", "Upper", "Scope", "Port",
"Node");
@@ -54,13 +55,20 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data)
hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str);
- printf("%-10u %-10u %-10u %-8s %-10u %s\n",
- mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]),
- mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
- mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]),
- scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])],
- mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]),
- str);
+ open_json_object(NULL);
+ print_uint(PRINT_ANY, "type", "%-10u",
+ mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
+ print_uint(PRINT_ANY, "lower", "%-10u",
+ mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]));
+ print_uint(PRINT_ANY, "upper", "%-10u",
+ mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
+ print_string(PRINT_ANY, "scope", "%-8s",
+ scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
+ print_uint(PRINT_ANY, "port", "%-10u",
+ mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]));
+ print_string(PRINT_ANY, "node", "%s", str);
+ print_string(PRINT_FP, NULL, "\n", "");
+ close_json_object();
return MNL_CB_OK;
}
@@ -70,6 +78,7 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
{
int iteration = 0;
char buf[MNL_SOCKET_BUFFER_SIZE];
+ int rc = 0;
if (help_flag) {
fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]);
@@ -81,7 +90,11 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
return -1;
}
- return msg_dumpit(nlh, nametable_show_cb, &iteration);
+ new_json_obj(json);
+ rc = msg_dumpit(nlh, nametable_show_cb, &iteration);
+ delete_json_obj();
+
+ return rc;
}
void cmd_nametable_help(struct cmdl *cmdl)
diff --git a/tipc/tipc.c b/tipc/tipc.c
index 600d5e2a160f..f85ddee0e278 100644
--- a/tipc/tipc.c
+++ b/tipc/tipc.c
@@ -24,6 +24,8 @@
#include "cmdl.h"
int help_flag;
+int json;
+int pretty;
static void about(struct cmdl *cmdl)
{
@@ -33,6 +35,8 @@ static void about(struct cmdl *cmdl)
"\n"
"Options:\n"
" -h, --help \t\tPrint help for last given command\n"
+ " -j, --json \t\tJson format printouts\n"
+ " -p, --pretty \t\tpretty print\n"
"\n"
"Commands:\n"
" bearer - Show or modify bearers\n"
@@ -53,6 +57,8 @@ int main(int argc, char *argv[])
const struct cmd cmd = {"tipc", NULL, about};
struct option long_options[] = {
{"help", no_argument, 0, 'h'},
+ {"json", no_argument, 0, 'j'},
+ {"pretty", no_argument, 0, 'p'},
{0, 0, 0, 0}
};
const struct cmd cmds[] = {
@@ -69,7 +75,7 @@ int main(int argc, char *argv[])
do {
int option_index = 0;
- i = getopt_long(argc, argv, "h", long_options, &option_index);
+ i = getopt_long(argc, argv, "hjp", long_options, &option_index);
switch (i) {
case 'h':
@@ -79,6 +85,18 @@ int main(int argc, char *argv[])
*/
help_flag = 1;
break;
+ case 'j':
+ /*
+ * Enable json format printouts
+ */
+ json = 1;
+ break;
+ case 'p':
+ /*
+ * Enable json pretty output
+ */
+ pretty = 1;
+ break;
case -1:
/* End of options */
break;
--
2.7.4
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [iproute2-next v2 2/2] tipc: JSON support for tipc link printouts
2018-06-11 2:16 [iproute2-next v2 1/2] tipc: JSON support for showing nametable Hoang Le
@ 2018-06-11 2:17 ` Hoang Le
2018-06-11 15:05 ` [iproute2-next v2 1/2] tipc: JSON support for showing nametable Stephen Hemminger
1 sibling, 0 replies; 3+ messages in thread
From: Hoang Le @ 2018-06-11 2:17 UTC (permalink / raw)
To: netdev, tipc-discussion
Add json output support for tipc link command
Example output:
$tipc -j -p link list
[ {
"broadcast-link": "up",
"1.1.1:bridge-1.1.104:eth0": "up",
"1.1.1:bridge-1.1.105:eth0": "up",
"1.1.1:bridge-1.1.106:eth0": "up"
} ]
--------------------
$tipc -j -p link stat show link broadcast-link
[ {
"link": "broadcast-link",
"window": 50,
"rx packets": {
"rx packets": 0,
"fragments": 0,
"fragmented": 0,
"bundles": 0,
"bundled": 0
},
"tx packets": {
"tx packets": 0,
"fragments": 0,
"fragmented": 0,
"bundles": 0,
"bundled": 0
},
"rx naks": {
"rx naks": 0,
"defs": 0,
"dups": 0
},
"tx naks": {
"tx naks": 0,
"acks": 0,
"retrans": 0
},
"congestion link": 0,
"send queue max": 0,
"avg": 0
} ]
v2:
Replace variable 'json_flag' by 'json' declared in include/utils.h
Acked-by: Jon Maloy <jon.maloy@ericsson.com>
Signed-off-by: Hoang Le <hoang.h.le@dektech.com.au>
---
tipc/link.c | 414 +++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 272 insertions(+), 142 deletions(-)
diff --git a/tipc/link.c b/tipc/link.c
index 02f14aadefa6..26d1293f121a 100644
--- a/tipc/link.c
+++ b/tipc/link.c
@@ -23,6 +23,11 @@
#include "msg.h"
#include "link.h"
#include "bearer.h"
+#include "utils.h"
+
+#define PRIORITY_STR "priority"
+#define TOLERANCE_STR "tolerance"
+#define WINDOW_STR "window"
static int link_list_cb(const struct nlmsghdr *nlh, void *data)
{
@@ -38,13 +43,14 @@ static int link_list_cb(const struct nlmsghdr *nlh, void *data)
if (!attrs[TIPC_NLA_LINK_NAME])
return MNL_CB_ERROR;
- printf("%s: ", mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]));
-
+ print_string(PRINT_FP, NULL, "%s: ",
+ mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]));
if (attrs[TIPC_NLA_LINK_UP])
- printf("up\n");
+ print_string(PRINT_ANY,
+ mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]),"%s\n", "up");
else
- printf("down\n");
-
+ print_string(PRINT_ANY,
+ mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]), "%s\n", "down");
return MNL_CB_OK;
}
@@ -52,6 +58,7 @@ static int cmd_link_list(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
+ int err = 0;
if (help_flag) {
fprintf(stderr, "Usage: %s link list\n", cmdl->argv[0]);
@@ -64,7 +71,12 @@ static int cmd_link_list(struct nlmsghdr *nlh, const struct cmd *cmd,
return -1;
}
- return msg_dumpit(nlh, link_list_cb, NULL);
+ new_json_obj(json);
+ open_json_object(NULL);
+ err = msg_dumpit(nlh, link_list_cb, NULL);
+ close_json_object();
+ delete_json_obj();
+ return err;
}
static int link_get_cb(const struct nlmsghdr *nlh, void *data)
@@ -87,8 +99,23 @@ static int link_get_cb(const struct nlmsghdr *nlh, void *data)
if (!props[*prop])
return MNL_CB_ERROR;
- printf("%u\n", mnl_attr_get_u32(props[*prop]));
-
+ new_json_obj(json);
+ open_json_object(NULL);
+ switch (*prop) {
+ case TIPC_NLA_PROP_PRIO:
+ print_uint(PRINT_ANY, PRIORITY_STR, "%u\n", mnl_attr_get_u32(props[*prop]));
+ break;
+ case TIPC_NLA_PROP_TOL:
+ print_uint(PRINT_ANY, TOLERANCE_STR, "%u\n", mnl_attr_get_u32(props[*prop]));
+ break;
+ case TIPC_NLA_PROP_WIN:
+ print_uint(PRINT_ANY, WINDOW_STR, "%u\n", mnl_attr_get_u32(props[*prop]));
+ break;
+ default:
+ break;
+ }
+ close_json_object();
+ delete_json_obj();
return MNL_CB_OK;
}
@@ -103,11 +130,11 @@ static int cmd_link_get_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
{ NULL }
};
- if (strcmp(cmd->cmd, "priority") == 0)
+ if (strcmp(cmd->cmd, PRIORITY_STR) == 0)
prop = TIPC_NLA_PROP_PRIO;
- else if ((strcmp(cmd->cmd, "tolerance") == 0))
+ else if ((strcmp(cmd->cmd, TOLERANCE_STR) == 0))
prop = TIPC_NLA_PROP_TOL;
- else if ((strcmp(cmd->cmd, "window") == 0))
+ else if ((strcmp(cmd->cmd, WINDOW_STR) == 0))
prop = TIPC_NLA_PROP_WIN;
else
return -EINVAL;
@@ -150,9 +177,9 @@ static int cmd_link_get(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
const struct cmd cmds[] = {
- { "priority", cmd_link_get_prop, cmd_link_get_help },
- { "tolerance", cmd_link_get_prop, cmd_link_get_help },
- { "window", cmd_link_get_prop, cmd_link_get_help },
+ { PRIORITY_STR, cmd_link_get_prop, cmd_link_get_help },
+ { TOLERANCE_STR, cmd_link_get_prop, cmd_link_get_help },
+ { WINDOW_STR, cmd_link_get_prop, cmd_link_get_help },
{ NULL }
};
@@ -211,109 +238,178 @@ static uint32_t perc(uint32_t count, uint32_t total)
return (count * 100 + (total / 2)) / total;
}
-static int _show_link_stat(struct nlattr *attrs[], struct nlattr *prop[],
- struct nlattr *stats[])
+static int _show_link_stat(const char *name, struct nlattr *attrs[],
+ struct nlattr *prop[], struct nlattr *stats[])
{
uint32_t proft;
+ open_json_object(NULL);
+
+ print_string(PRINT_ANY, "link", "\nLink <%s>\n", name);
+ print_string(PRINT_JSON, "state", "", NULL);
+ open_json_array(PRINT_JSON, NULL);
if (attrs[TIPC_NLA_LINK_ACTIVE])
- printf(" ACTIVE");
+ print_string(PRINT_ANY, NULL, " %s", "ACTIVE");
else if (attrs[TIPC_NLA_LINK_UP])
- printf(" STANDBY");
+ print_string(PRINT_ANY, NULL, " %s", "STANDBY");
else
- printf(" DEFUNCT");
-
- printf(" MTU:%u Priority:%u Tolerance:%u ms Window:%u packets\n",
- mnl_attr_get_u32(attrs[TIPC_NLA_LINK_MTU]),
- mnl_attr_get_u32(prop[TIPC_NLA_PROP_PRIO]),
- mnl_attr_get_u32(prop[TIPC_NLA_PROP_TOL]),
- mnl_attr_get_u32(prop[TIPC_NLA_PROP_WIN]));
-
- printf(" RX packets:%u fragments:%u/%u bundles:%u/%u\n",
- mnl_attr_get_u32(attrs[TIPC_NLA_LINK_RX]) -
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
-
- printf(" TX packets:%u fragments:%u/%u bundles:%u/%u\n",
- mnl_attr_get_u32(attrs[TIPC_NLA_LINK_TX]) -
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
+ print_string(PRINT_ANY, NULL, " %s", "DEFUNCT");
+ close_json_array(PRINT_JSON, NULL);
+
+ print_uint(PRINT_ANY, "mtu", " MTU:%u",
+ mnl_attr_get_u32(attrs[TIPC_NLA_LINK_MTU]));
+ print_uint(PRINT_ANY, PRIORITY_STR, " Priority:%u",
+ mnl_attr_get_u32(prop[TIPC_NLA_PROP_PRIO]));
+ print_uint(PRINT_ANY, TOLERANCE_STR, " Tolerance:%u ms",
+ mnl_attr_get_u32(prop[TIPC_NLA_PROP_TOL]));
+ print_uint(PRINT_ANY, WINDOW_STR, " Window:%u packets\n",
+ mnl_attr_get_u32(prop[TIPC_NLA_PROP_WIN]));
+
+ open_json_object("rx packets");
+ print_uint(PRINT_ANY, "rx packets", " RX packets:%u",
+ mnl_attr_get_u32(attrs[TIPC_NLA_LINK_RX]) -
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_INFO]));
+ print_uint(PRINT_ANY, "fragments", " fragments:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]));
+ print_uint(PRINT_ANY, "fragmented", "/%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]));
+ print_uint(PRINT_ANY, "bundles", " bundles:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]));
+ print_uint(PRINT_ANY, "bundled", "/%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
+ close_json_object();
+
+ open_json_object("tx packets");
+ print_uint(PRINT_ANY, "tx packets", " TX packets:%u",
+ mnl_attr_get_u32(attrs[TIPC_NLA_LINK_TX]) -
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_INFO]));
+ print_uint(PRINT_ANY, "fragments", " fragments:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]));
+ print_uint(PRINT_ANY, "fragmented", "/%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]));
+ print_uint(PRINT_ANY, "bundles", " bundles:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]));
+ print_uint(PRINT_ANY, "bundled", "/%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
+ close_json_object();
proft = mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]);
- printf(" TX profile sample:%u packets average:%u octets\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) / proft);
-
- printf(" 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% -16384:%u%% -32768:%u%% -66000:%u%%\n",
- perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]), proft),
- perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]), proft),
- perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]), proft),
- perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]), proft),
- perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]), proft),
- perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]), proft),
- perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]), proft));
-
- printf(" RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
-
- printf(" TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
-
- printf(" Congestion link:%u Send queue max:%u avg:%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
-
+ print_uint(PRINT_ANY, "tx profile sample", " TX profile sample:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]));
+ print_uint(PRINT_ANY, "packets average", " packets average:%u octets\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) / proft);
+
+ print_uint(PRINT_ANY, "0-64", " 0-64:%u%%",
+ perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]), proft));
+ print_uint(PRINT_ANY, "-256", " -256:%u%%",
+ perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]), proft));
+ print_uint(PRINT_ANY, "-1024", " -1024:%u%%",
+ perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]), proft));
+ print_uint(PRINT_ANY, "-4096", " -4096:%u%%",
+ perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]), proft));
+ print_uint(PRINT_ANY, "-16384", " -16384:%u%%",
+ perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]), proft));
+ print_uint(PRINT_ANY, "-32768", " -32768:%u%%",
+ perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]), proft));
+ print_uint(PRINT_ANY, "-66000", " -66000:%u%%\n",
+ perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]), proft));
+
+ open_json_object("rx states");
+ print_uint(PRINT_ANY, "rx states", " RX states:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_STATES]));
+ print_uint(PRINT_ANY, "probes", " probes:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]));
+ print_uint(PRINT_ANY, "naks", " naks:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]));
+ print_uint(PRINT_ANY, "defs", " defs:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]));
+ print_uint(PRINT_ANY, "dups", " dups:%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
+ close_json_object();
+
+ open_json_object("tx states");
+ print_uint(PRINT_ANY, "tx states", " TX states:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_STATES]));
+ print_uint(PRINT_ANY, "probes", " probes:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]));
+ print_uint(PRINT_ANY, "naks", " naks:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]));
+ print_uint(PRINT_ANY, "acks", " acks:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]));
+ print_uint(PRINT_ANY, "retrans", " retrans:%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
+ close_json_object();
+
+ print_uint(PRINT_ANY, "congestion link", " Congestion link:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]));
+ print_uint(PRINT_ANY, "send queue max", " Send queue max:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]));
+ print_uint(PRINT_ANY, "avg", " avg:%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
+
+ close_json_object();
return MNL_CB_OK;
}
-static int _show_bc_link_stat(struct nlattr *prop[], struct nlattr *stats[])
+static int _show_bc_link_stat(const char *name, struct nlattr *prop[],
+ struct nlattr *stats[])
{
- printf(" Window:%u packets\n",
- mnl_attr_get_u32(prop[TIPC_NLA_PROP_WIN]));
-
- printf(" RX packets:%u fragments:%u/%u bundles:%u/%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
-
- printf(" TX packets:%u fragments:%u/%u bundles:%u/%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
-
- printf(" RX naks:%u defs:%u dups:%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
-
- printf(" TX naks:%u acks:%u dups:%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
-
- printf(" Congestion link:%u Send queue max:%u avg:%u\n",
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
- mnl_attr_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
+ open_json_object(NULL);
+ print_string(PRINT_ANY, "link", "Link <%s>\n", name);
+ print_uint(PRINT_ANY, WINDOW_STR, " Window:%u packets\n",
+ mnl_attr_get_u32(prop[TIPC_NLA_PROP_WIN]));
+
+ open_json_object("rx packets");
+ print_uint(PRINT_ANY, "rx packets", " RX packets:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_INFO]));
+ print_uint(PRINT_ANY, "fragments", " fragments:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]));
+ print_uint(PRINT_ANY, "fragmented", "/%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]));
+ print_uint(PRINT_ANY, "bundles", " bundles:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]));
+ print_uint(PRINT_ANY, "bundled", "/%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
+ close_json_object();
+
+ open_json_object("tx packets");
+ print_uint(PRINT_ANY, "tx packets", " TX packets:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_INFO]));
+ print_uint(PRINT_ANY, "fragments", " fragments:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]));
+ print_uint(PRINT_ANY, "fragmented", "/%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]));
+ print_uint(PRINT_ANY, "bundles", " bundles:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]));
+ print_uint(PRINT_ANY, "bundled", "/%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
+ close_json_object();
+
+ open_json_object("rx naks");
+ print_uint(PRINT_ANY, "rx naks", " RX naks:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]));
+ print_uint(PRINT_ANY, "defs", " defs:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]));
+ print_uint(PRINT_ANY, "dups", " dups:%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
+ close_json_object();
+
+ open_json_object("tx naks");
+ print_uint(PRINT_ANY, "tx naks", " TX naks:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]));
+ print_uint(PRINT_ANY, "acks", " acks:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]));
+ print_uint(PRINT_ANY, "retrans", " retrans:%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
+ close_json_object();
+
+ print_uint(PRINT_ANY, "congestion link", " Congestion link:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]));
+ print_uint(PRINT_ANY, "send queue max", " Send queue max:%u",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]));
+ print_uint(PRINT_ANY, "avg", " avg:%u\n",
+ mnl_attr_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
+ close_json_object();
return MNL_CB_OK;
}
@@ -347,13 +443,10 @@ static int link_stat_show_cb(const struct nlmsghdr *nlh, void *data)
return MNL_CB_OK;
if (attrs[TIPC_NLA_LINK_BROADCAST]) {
- printf("Link <%s>\n", name);
- return _show_bc_link_stat(prop, stats);
+ return _show_bc_link_stat(name, prop, stats);
}
- printf("\nLink <%s>\n", name);
-
- return _show_link_stat(attrs, prop, stats);
+ return _show_link_stat(name, attrs, prop, stats);
}
static void cmd_link_stat_show_help(struct cmdl *cmdl)
@@ -372,6 +465,7 @@ static int cmd_link_stat_show(struct nlmsghdr *nlh, const struct cmd *cmd,
{ "link", OPT_KEYVAL, NULL },
{ NULL }
};
+ int err = 0;
if (help_flag) {
(cmd->help)(cmdl);
@@ -391,7 +485,10 @@ static int cmd_link_stat_show(struct nlmsghdr *nlh, const struct cmd *cmd,
if (opt)
link = opt->val;
- return msg_dumpit(nlh, link_stat_show_cb, link);
+ new_json_obj(json);
+ err = msg_dumpit(nlh, link_stat_show_cb, link);
+ delete_json_obj();
+ return err;
}
static void cmd_link_stat_help(struct cmdl *cmdl)
@@ -439,11 +536,11 @@ static int cmd_link_set_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
{ NULL }
};
- if (strcmp(cmd->cmd, "priority") == 0)
+ if (strcmp(cmd->cmd, PRIORITY_STR) == 0)
prop = TIPC_NLA_PROP_PRIO;
- else if ((strcmp(cmd->cmd, "tolerance") == 0))
+ else if ((strcmp(cmd->cmd, TOLERANCE_STR) == 0))
prop = TIPC_NLA_PROP_TOL;
- else if ((strcmp(cmd->cmd, "window") == 0))
+ else if ((strcmp(cmd->cmd, WINDOW_STR) == 0))
prop = TIPC_NLA_PROP_WIN;
else
return -EINVAL;
@@ -489,9 +586,9 @@ static int cmd_link_set(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
const struct cmd cmds[] = {
- { "priority", cmd_link_set_prop, cmd_link_set_help },
- { "tolerance", cmd_link_set_prop, cmd_link_set_help },
- { "window", cmd_link_set_prop, cmd_link_set_help },
+ { PRIORITY_STR, cmd_link_set_prop, cmd_link_set_help },
+ { TOLERANCE_STR, cmd_link_set_prop, cmd_link_set_help },
+ { WINDOW_STR, cmd_link_set_prop, cmd_link_set_help },
{ NULL }
};
@@ -537,15 +634,17 @@ static int link_mon_summary_cb(const struct nlmsghdr *nlh, void *data)
mnl_attr_parse_nested(info[TIPC_NLA_MON], parse_attrs, attrs);
- printf("\nbearer %s\n",
+ open_json_object(NULL);
+ print_string(PRINT_ANY, "bearer", "\nbearer %s\n",
mnl_attr_get_str(attrs[TIPC_NLA_MON_BEARER_NAME]));
- printf(" table_generation %u\n",
+ print_uint(PRINT_ANY, "table_generation", " table_generation %u\n",
mnl_attr_get_u32(attrs[TIPC_NLA_MON_LISTGEN]));
- printf(" cluster_size %u\n",
+ print_uint(PRINT_ANY, "cluster_size", " cluster_size %u\n",
mnl_attr_get_u32(attrs[TIPC_NLA_MON_PEERCNT]));
- printf(" algorithm %s\n",
+ print_string(PRINT_ANY, "algorithm", " algorithm %s\n",
attrs[TIPC_NLA_MON_ACTIVE] ? "overlapping-ring" : "full-mesh");
+ close_json_object();
return MNL_CB_OK;
}
@@ -554,6 +653,7 @@ static int cmd_link_mon_summary(struct nlmsghdr *nlh, const struct cmd *cmd,
struct cmdl *cmdl, void *data)
{
char buf[MNL_SOCKET_BUFFER_SIZE];
+ int err = 0;
if (help_flag) {
fprintf(stderr, "Usage: %s monitor summary\n", cmdl->argv[0]);
@@ -566,7 +666,11 @@ static int cmd_link_mon_summary(struct nlmsghdr *nlh, const struct cmd *cmd,
return -1;
}
- return msg_dumpit(nlh, link_mon_summary_cb, NULL);
+ new_json_obj(json);
+ err = msg_dumpit(nlh, link_mon_summary_cb, NULL);
+ delete_json_obj();
+
+ return err;
}
#define STATUS_WIDTH 7
@@ -587,16 +691,19 @@ static int map_get(uint64_t up_map, int i)
static void link_mon_print_applied(uint16_t applied, uint64_t up_map)
{
int i;
- char state;
+ open_json_array(PRINT_JSON, "applied_node_status");
for (i = 0; i < applied; i++) {
+ char state_str[2] = {0};
+
/* print the delimiter for every -n- entry */
if (i && !(i % APPL_NODE_STATUS_WIDTH))
- printf(",");
+ print_string(PRINT_FP, NULL, "%s", ",");
- state = map_get(up_map, i) ? 'U' : 'D';
- printf("%c", state);
+ sprintf(state_str, "%c", map_get(up_map, i) ? 'U' : 'D');
+ print_string(PRINT_ANY, NULL, "%s", state_str);
}
+ close_json_array(PRINT_JSON, "applied_node_status");
}
/* print the non applied members, since we dont know
@@ -608,19 +715,23 @@ static void link_mon_print_non_applied(uint16_t applied, uint16_t member_cnt,
int i;
char state;
- printf(" [");
+ open_json_array(PRINT_JSON, "[non_applied_node:status]");
+ print_string(PRINT_FP, NULL, " %s", "[");
for (i = applied; i < member_cnt; i++) {
char addr_str[16];
+ char full_state[17] = {0};
/* print the delimiter for every entry */
if (i != applied)
- printf(",");
+ print_string(PRINT_FP, NULL, "%s", ",");
sprintf(addr_str, "%x:", members[i]);
state = map_get(up_map, i) ? 'U' : 'D';
- printf("%s%c", addr_str, state);
+ sprintf(full_state, "%s%c", addr_str, state);
+ print_string(PRINT_ANY, NULL, "%s", full_state);
}
- printf("]");
+ print_string(PRINT_FP, NULL, "%s", "]");
+ close_json_array(PRINT_JSON, "[non_applied_node:status]");
}
static void link_mon_print_peer_state(const uint32_t addr, const char *status,
@@ -631,11 +742,17 @@ static void link_mon_print_peer_state(const uint32_t addr, const char *status,
sprintf(addr_str, "%u.%u.%u", tipc_zone(addr), tipc_cluster(addr),
tipc_node(addr));
-
- printf("%-*s", MAX_NODE_WIDTH, addr_str);
- printf("%-*s", STATUS_WIDTH, status);
- printf("%-*s", DIRECTLY_MON_WIDTH, monitored);
- printf("%-*u", MAX_DOM_GEN_WIDTH, dom_gen);
+ if (is_json_context()) {
+ print_string(PRINT_JSON, "node", NULL, addr_str);
+ print_string(PRINT_JSON, "status", NULL, status);
+ print_string(PRINT_JSON, "monitored", NULL, monitored);
+ print_uint(PRINT_JSON, "generation", NULL, dom_gen);
+ } else {
+ printf("%-*s", MAX_NODE_WIDTH, addr_str);
+ printf("%-*s", STATUS_WIDTH, status);
+ printf("%-*s", DIRECTLY_MON_WIDTH, monitored);
+ printf("%-*u", MAX_DOM_GEN_WIDTH, dom_gen);
+ }
}
static int link_mon_peer_list_cb(const struct nlmsghdr *nlh, void *data)
@@ -654,6 +771,7 @@ static int link_mon_peer_list_cb(const struct nlmsghdr *nlh, void *data)
if (!info[TIPC_NLA_MON_PEER])
return MNL_CB_ERROR;
+ open_json_object(NULL);
mnl_attr_parse_nested(info[TIPC_NLA_MON_PEER], parse_attrs, attrs);
(attrs[TIPC_NLA_MON_PEER_LOCAL] || attrs[TIPC_NLA_MON_PEER_HEAD]) ?
@@ -688,8 +806,9 @@ static int link_mon_peer_list_cb(const struct nlmsghdr *nlh, void *data)
mnl_attr_get_payload(attrs[TIPC_NLA_MON_PEER_MEMBERS]));
exit:
- printf("\n");
+ print_string(PRINT_FP, NULL, "\n", "");
+ close_json_object();
return MNL_CB_OK;
}
@@ -698,6 +817,7 @@ static int link_mon_peer_list(uint32_t mon_ref)
struct nlmsghdr *nlh;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlattr *nest;
+ int err = 0;
nlh = msg_init(buf, TIPC_NL_MON_PEER_GET);
if (!nlh) {
@@ -709,7 +829,8 @@ static int link_mon_peer_list(uint32_t mon_ref)
mnl_attr_put_u32(nlh, TIPC_NLA_MON_REF, mon_ref);
mnl_attr_nest_end(nlh, nest);
- return msg_dumpit(nlh, link_mon_peer_list_cb, NULL);
+ err = msg_dumpit(nlh, link_mon_peer_list_cb, NULL);
+ return err;
}
static int link_mon_list_cb(const struct nlmsghdr *nlh, void *data)
@@ -733,12 +854,16 @@ static int link_mon_list_cb(const struct nlmsghdr *nlh, void *data)
if (*req_bearer && (strcmp(req_bearer, bname) != 0))
return MNL_CB_OK;
- printf("\nbearer %s\n", bname);
- printf("%s\n", title);
+ open_json_object(NULL);
+ print_string(PRINT_ANY, "bearer", "\nbearer %s\n", bname);
+ print_string(PRINT_FP, NULL, "%s\n", title);
+ open_json_array(PRINT_JSON, bname);
if (mnl_attr_get_u32(attrs[TIPC_NLA_MON_PEERCNT]))
link_mon_peer_list(mnl_attr_get_u32(attrs[TIPC_NLA_MON_REF]));
+ close_json_array(PRINT_JSON, bname);
+ close_json_object();
return MNL_CB_OK;
}
@@ -804,7 +929,10 @@ static int cmd_link_mon_list(struct nlmsghdr *nlh, const struct cmd *cmd,
return -1;
}
- return msg_dumpit(nlh, link_mon_list_cb, bname);
+ new_json_obj(json);
+ err = msg_dumpit(nlh, link_mon_list_cb, bname);
+ delete_json_obj();
+ return err;
}
static void cmd_link_mon_set_help(struct cmdl *cmdl)
@@ -848,8 +976,10 @@ static int link_mon_get_cb(const struct nlmsghdr *nlh, void *data)
if (!attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD])
return MNL_CB_ERROR;
- printf("%u\n",
- mnl_attr_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]));
+ new_json_obj(json);
+ print_uint(PRINT_ANY, "threshold", "%u\n",
+ mnl_attr_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]));
+ delete_json_obj();
return MNL_CB_OK;
}
--
2.7.4
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [iproute2-next v2 1/2] tipc: JSON support for showing nametable
2018-06-11 2:16 [iproute2-next v2 1/2] tipc: JSON support for showing nametable Hoang Le
2018-06-11 2:17 ` [iproute2-next v2 2/2] tipc: JSON support for tipc link printouts Hoang Le
@ 2018-06-11 15:05 ` Stephen Hemminger
1 sibling, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2018-06-11 15:05 UTC (permalink / raw)
To: Hoang Le; +Cc: netdev, tipc-discussion
On Mon, 11 Jun 2018 09:16:59 +0700
Hoang Le <hoang.h.le@dektech.com.au> wrote:
> @@ -33,6 +35,8 @@ static void about(struct cmdl *cmdl)
> "\n"
> "Options:\n"
> " -h, --help \t\tPrint help for last given command\n"
> + " -j, --json \t\tJson format printouts\n"
> + " -p, --pretty \t\tpretty print\n"
> "\n"
You should also update manual page: man/man8/tipc-nametable.8
Just cut/paste text from ip or tc pages.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2018-06-11 15:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-06-11 2:16 [iproute2-next v2 1/2] tipc: JSON support for showing nametable Hoang Le
2018-06-11 2:17 ` [iproute2-next v2 2/2] tipc: JSON support for tipc link printouts Hoang Le
2018-06-11 15:05 ` [iproute2-next v2 1/2] tipc: JSON support for showing nametable Stephen Hemminger
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.