bridge: add json support for link command

Add json output for bridge link show command and reuse code
from ip command to display interface information.

This also changes the output format slightly for the non JSON case so
that it has same format as the ip link show command.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Stephen Hemminger 2018-02-20 11:24:06 -08:00 committed by David Ahern
parent c7c1a1ef51
commit f32e4977dc
1 changed files with 134 additions and 115 deletions

View File

@ -12,6 +12,7 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include "json_print.h"
#include "libnetlink.h" #include "libnetlink.h"
#include "utils.h" #include "utils.h"
#include "br_common.h" #include "br_common.h"
@ -26,15 +27,21 @@ static const char *port_states[] = {
[BR_STATE_BLOCKING] = "blocking", [BR_STATE_BLOCKING] = "blocking",
}; };
static void print_link_flags(FILE *fp, unsigned int flags) static const char *hw_mode[] = {
"VEB", "VEPA"
};
static void print_link_flags(FILE *fp, unsigned int flags, unsigned int mdown)
{ {
fprintf(fp, "<"); open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<");
if (flags & IFF_UP && !(flags & IFF_RUNNING)) if (flags & IFF_UP && !(flags & IFF_RUNNING))
fprintf(fp, "NO-CARRIER%s", flags ? "," : ""); print_string(PRINT_ANY, NULL,
flags ? "%s," : "%s", "NO-CARRIER");
flags &= ~IFF_RUNNING; flags &= ~IFF_RUNNING;
#define _PF(f) if (flags&IFF_##f) { \ #define _PF(f) if (flags&IFF_##f) { \
flags &= ~IFF_##f ; \ flags &= ~IFF_##f ; \
fprintf(fp, #f "%s", flags ? "," : ""); } print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
_PF(LOOPBACK); _PF(LOOPBACK);
_PF(BROADCAST); _PF(BROADCAST);
_PF(POINTOPOINT); _PF(POINTOPOINT);
@ -55,112 +62,64 @@ static void print_link_flags(FILE *fp, unsigned int flags)
_PF(ECHO); _PF(ECHO);
#undef _PF #undef _PF
if (flags) if (flags)
fprintf(fp, "%x", flags); print_hex(PRINT_ANY, NULL, "%x", flags);
fprintf(fp, "> "); if (mdown)
print_string(PRINT_ANY, NULL, ",%s", "M-DOWN");
close_json_array(PRINT_ANY, "> ");
} }
static const char *oper_states[] = { static void print_portstate(__u8 state)
"UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN",
"TESTING", "DORMANT", "UP"
};
static const char *hw_mode[] = {"VEB", "VEPA"};
static void print_operstate(FILE *f, __u8 state)
{
if (state >= ARRAY_SIZE(oper_states))
fprintf(f, "state %#x ", state);
else
fprintf(f, "state %s ", oper_states[state]);
}
static void print_portstate(FILE *f, __u8 state)
{ {
if (state <= BR_STATE_BLOCKING) if (state <= BR_STATE_BLOCKING)
fprintf(f, "state %s ", port_states[state]); print_string(PRINT_ANY, "state",
"state %s ", port_states[state]);
else else
fprintf(f, "state (%d) ", state); print_uint(PRINT_ANY, "state",
"state (%d) ", state);
} }
static void print_onoff(FILE *f, char *flag, __u8 val) static void print_onoff(FILE *fp, const char *flag, __u8 val)
{ {
fprintf(f, "%s %s ", flag, val ? "on" : "off"); if (is_json_context())
print_bool(PRINT_JSON, flag, NULL, val);
else
fprintf(fp, "%s %s ", flag, val ? "on" : "off");
} }
static void print_hwmode(FILE *f, __u16 mode) static void print_hwmode(__u16 mode)
{ {
if (mode >= ARRAY_SIZE(hw_mode)) if (mode >= ARRAY_SIZE(hw_mode))
fprintf(f, "hwmode %#hx ", mode); print_0xhex(PRINT_ANY, "hwmode",
"hwmode %#hx ", mode);
else else
fprintf(f, "hwmode %s ", hw_mode[mode]); print_string(PRINT_ANY, "hwmode",
"hwmode %s ", hw_mode[mode]);
} }
int print_linkinfo(const struct sockaddr_nl *who, static void print_protinfo(FILE *fp, struct rtattr *attr)
struct nlmsghdr *n, void *arg)
{ {
FILE *fp = arg; if (attr->rta_type & NLA_F_NESTED) {
struct ifinfomsg *ifi = NLMSG_DATA(n); struct rtattr *prtb[IFLA_BRPORT_MAX + 1];
struct rtattr *tb[IFLA_MAX+1];
int len = n->nlmsg_len;
const char *name;
len -= NLMSG_LENGTH(sizeof(*ifi)); parse_rtattr_nested(prtb, IFLA_BRPORT_MAX, attr);
if (len < 0) {
fprintf(stderr, "Message too short!\n");
return -1;
}
if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC))
return 0;
if (filter_index && filter_index != ifi->ifi_index)
return 0;
parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
if (!name)
return -1;
if (n->nlmsg_type == RTM_DELLINK)
fprintf(fp, "Deleted ");
fprintf(fp, "%d: ", ifi->ifi_index);
print_name_and_link("%s: ", COLOR_NONE, name, tb);
if (tb[IFLA_OPERSTATE])
print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
print_link_flags(fp, ifi->ifi_flags);
if (tb[IFLA_MTU])
fprintf(fp, "mtu %u ", rta_getattr_u32(tb[IFLA_MTU]));
if (tb[IFLA_MASTER]) {
int master = rta_getattr_u32(tb[IFLA_MASTER]);
fprintf(fp, "master %s ", ll_index_to_name(master));
}
if (tb[IFLA_PROTINFO]) {
if (tb[IFLA_PROTINFO]->rta_type & NLA_F_NESTED) {
struct rtattr *prtb[IFLA_BRPORT_MAX+1];
parse_rtattr_nested(prtb, IFLA_BRPORT_MAX,
tb[IFLA_PROTINFO]);
if (prtb[IFLA_BRPORT_STATE]) if (prtb[IFLA_BRPORT_STATE])
print_portstate(fp, print_portstate(rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
rta_getattr_u8(prtb[IFLA_BRPORT_STATE]));
if (prtb[IFLA_BRPORT_PRIORITY]) if (prtb[IFLA_BRPORT_PRIORITY])
fprintf(fp, "priority %hu ", print_uint(PRINT_ANY, "priority",
"priority %u ",
rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY])); rta_getattr_u16(prtb[IFLA_BRPORT_PRIORITY]));
if (prtb[IFLA_BRPORT_COST]) if (prtb[IFLA_BRPORT_COST])
fprintf(fp, "cost %u ", print_uint(PRINT_ANY, "cost",
"cost %u ",
rta_getattr_u32(prtb[IFLA_BRPORT_COST])); rta_getattr_u32(prtb[IFLA_BRPORT_COST]));
if (show_details) { if (!show_details)
return;
if (!is_json_context())
fprintf(fp, "%s ", _SL_); fprintf(fp, "%s ", _SL_);
if (prtb[IFLA_BRPORT_MODE]) if (prtb[IFLA_BRPORT_MODE])
@ -193,31 +152,87 @@ int print_linkinfo(const struct sockaddr_nl *who,
if (prtb[IFLA_BRPORT_VLAN_TUNNEL]) if (prtb[IFLA_BRPORT_VLAN_TUNNEL])
print_onoff(fp, "vlan_tunnel", print_onoff(fp, "vlan_tunnel",
rta_getattr_u8(prtb[IFLA_BRPORT_VLAN_TUNNEL])); rta_getattr_u8(prtb[IFLA_BRPORT_VLAN_TUNNEL]));
}
} else } else
print_portstate(fp, rta_getattr_u8(tb[IFLA_PROTINFO])); print_portstate(rta_getattr_u8(attr));
} }
if (tb[IFLA_AF_SPEC]) {
/* This is reported by HW devices that have some bridging /*
* This is reported by HW devices that have some bridging
* capabilities. * capabilities.
*/ */
static void print_af_spec(FILE *fp, struct rtattr *attr)
{
struct rtattr *aftb[IFLA_BRIDGE_MAX+1]; struct rtattr *aftb[IFLA_BRIDGE_MAX+1];
parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, tb[IFLA_AF_SPEC]); parse_rtattr_nested(aftb, IFLA_BRIDGE_MAX, attr);
if (aftb[IFLA_BRIDGE_MODE]) if (aftb[IFLA_BRIDGE_MODE])
print_hwmode(fp, rta_getattr_u16(aftb[IFLA_BRIDGE_MODE])); print_hwmode(rta_getattr_u16(aftb[IFLA_BRIDGE_MODE]));
if (show_details) {
if (aftb[IFLA_BRIDGE_VLAN_INFO]) { if (!show_details)
fprintf(fp, "\n"); return;
print_vlan_info(fp, tb[IFLA_AF_SPEC],
ifi->ifi_index); if (aftb[IFLA_BRIDGE_VLAN_INFO])
} print_vlan_info(fp, aftb[IFLA_BRIDGE_VLAN_INFO]);
} }
int print_linkinfo(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
FILE *fp = arg;
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr *tb[IFLA_MAX+1];
unsigned int m_flag = 0;
int len = n->nlmsg_len;
const char *name;
len -= NLMSG_LENGTH(sizeof(*ifi));
if (len < 0) {
fprintf(stderr, "Message too short!\n");
return -1;
} }
fprintf(fp, "\n"); if (!(ifi->ifi_family == AF_BRIDGE || ifi->ifi_family == AF_UNSPEC))
return 0;
if (filter_index && filter_index != ifi->ifi_index)
return 0;
parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
if (!name)
return -1;
open_json_object(NULL);
if (n->nlmsg_type == RTM_DELLINK)
print_bool(PRINT_ANY, "deleted", "Deleted ", true);
print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index);
m_flag = print_name_and_link("%s: ", COLOR_IFNAME, name, tb);
print_link_flags(fp, ifi->ifi_flags, m_flag);
if (tb[IFLA_MTU])
print_int(PRINT_ANY,
"mtu", "mtu %u ",
rta_getattr_u32(tb[IFLA_MTU]));
if (tb[IFLA_MASTER]) {
int master = rta_getattr_u32(tb[IFLA_MASTER]);
print_string(PRINT_ANY, "master", "master %s ",
ll_index_to_name(master));
}
if (tb[IFLA_PROTINFO])
print_protinfo(fp, tb[IFLA_PROTINFO]);
if (tb[IFLA_AF_SPEC])
print_af_spec(fp, tb[IFLA_AF_SPEC]);
print_string(PRINT_FP, NULL, "%s", "\n");
close_json_object();
fflush(fp); fflush(fp);
return 0; return 0;
} }
@ -492,10 +507,14 @@ static int brlink_show(int argc, char **argv)
} }
} }
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) { if (rtnl_dump_filter(&rth, print_linkinfo, stdout) < 0) {
fprintf(stderr, "Dump terminated\n"); fprintf(stderr, "Dump terminated\n");
exit(1); exit(1);
} }
delete_json_obj();
fflush(stdout);
return 0; return 0;
} }