bgpd: Fixup json with RouteDistinguishers

The json option for displaying a bgp table
with route Distinguishers in it was not properly
working.  This code cleans this issue up.

Additionally attempt to make the code a bit
easier to read and handle.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
This commit is contained in:
Donald Sharp 2017-10-05 10:11:36 -04:00
parent 98ce9a06b4
commit 445c24803d

View File

@ -6500,7 +6500,15 @@ void route_vty_out(struct vty *vty, struct prefix *p, struct bgp_info *binfo,
/* Print attribute */ /* Print attribute */
attr = binfo->attr; attr = binfo->attr;
if (attr) { if (!attr) {
if (json_paths)
json_object_array_add(json_paths, json_path);
else
vty_out(vty, "\n");
return;
}
/* /*
* For ENCAP and EVPN routes, nexthop address family is not * For ENCAP and EVPN routes, nexthop address family is not
* neccessarily the same as the prefix address family. * neccessarily the same as the prefix address family.
@ -6513,35 +6521,50 @@ void route_vty_out(struct vty *vty, struct prefix *p, struct bgp_info *binfo,
*/ */
if ((safi == SAFI_ENCAP) || (safi == SAFI_MPLS_VPN)) { if ((safi == SAFI_ENCAP) || (safi == SAFI_MPLS_VPN)) {
char buf[BUFSIZ]; char buf[BUFSIZ];
char nexthop[128];
int af = NEXTHOP_FAMILY(attr->mp_nexthop_len); int af = NEXTHOP_FAMILY(attr->mp_nexthop_len);
switch (af) { switch (af) {
case AF_INET: case AF_INET:
vty_out(vty, "%s", sprintf(nexthop, "%s",
inet_ntop(af, inet_ntop(af, &attr->mp_nexthop_global_in,
&attr->mp_nexthop_global_in,
buf, BUFSIZ)); buf, BUFSIZ));
break; break;
case AF_INET6: case AF_INET6:
vty_out(vty, "%s", sprintf(nexthop, "%s",
inet_ntop(af, &attr->mp_nexthop_global, inet_ntop(af, &attr->mp_nexthop_global,
buf, BUFSIZ)); buf, BUFSIZ));
break; break;
default: default:
vty_out(vty, "?"); sprintf(nexthop, "?");
break; break;
} }
if (json_paths) {
json_nexthop_global = json_object_new_object();
json_object_string_add(json_nexthop_global,
"afi",
(af == AF_INET) ?
"ip" : "ipv6");
json_object_string_add(json_nexthop_global,
(af == AF_INET) ?
"ip" : "ipv6",
nexthop);
json_object_boolean_true_add(json_nexthop_global,
"used");
} else
vty_out(vty, "%s", nexthop);
} else if (safi == SAFI_EVPN) { } else if (safi == SAFI_EVPN) {
if (json_paths) { if (json_paths) {
json_nexthop_global = json_object_new_object(); json_nexthop_global = json_object_new_object();
json_object_string_add( json_object_string_add(json_nexthop_global, "ip",
json_nexthop_global, "ip",
inet_ntoa(attr->nexthop)); inet_ntoa(attr->nexthop));
json_object_string_add(json_nexthop_global, json_object_string_add(json_nexthop_global,
"afi", "ipv4"); "afi", "ipv4");
json_object_boolean_true_add( json_object_boolean_true_add(json_nexthop_global,
json_nexthop_global, "used"); "used");
} else } else
vty_out(vty, "%-16s", inet_ntoa(attr->nexthop)); vty_out(vty, "%-16s", inet_ntoa(attr->nexthop));
} }
@ -6553,19 +6576,18 @@ void route_vty_out(struct vty *vty, struct prefix *p, struct bgp_info *binfo,
if ((safi == SAFI_MPLS_VPN) if ((safi == SAFI_MPLS_VPN)
|| (safi == SAFI_EVPN)) || (safi == SAFI_EVPN))
json_object_string_add( json_object_string_add(json_nexthop_global,
json_nexthop_global, "ip", "ip",
inet_ntoa( inet_ntoa(attr->mp_nexthop_global_in));
attr->mp_nexthop_global_in));
else else
json_object_string_add( json_object_string_add(json_nexthop_global,
json_nexthop_global, "ip", "ip",
inet_ntoa(attr->nexthop)); inet_ntoa(attr->nexthop));
json_object_string_add(json_nexthop_global, json_object_string_add(json_nexthop_global,
"afi", "ipv4"); "afi", "ipv4");
json_object_boolean_true_add( json_object_boolean_true_add(json_nexthop_global,
json_nexthop_global, "used"); "used");
} else { } else {
if ((safi == SAFI_MPLS_VPN) if ((safi == SAFI_MPLS_VPN)
|| (safi == SAFI_EVPN)) || (safi == SAFI_EVPN))
@ -6586,8 +6608,7 @@ void route_vty_out(struct vty *vty, struct prefix *p, struct bgp_info *binfo,
if (json_paths) { if (json_paths) {
json_nexthop_global = json_object_new_object(); json_nexthop_global = json_object_new_object();
json_object_string_add( json_object_string_add(json_nexthop_global, "ip",
json_nexthop_global, "ip",
inet_ntop(AF_INET6, inet_ntop(AF_INET6,
&attr->mp_nexthop_global, buf, &attr->mp_nexthop_global, buf,
BUFSIZ)); BUFSIZ));
@ -6667,10 +6688,8 @@ void route_vty_out(struct vty *vty, struct prefix *p, struct bgp_info *binfo,
" "); " ");
} }
} else { } else {
len = vty_out( len = vty_out(vty, "%s",
vty, "%s", inet_ntop(AF_INET6,
inet_ntop(
AF_INET6,
&attr->mp_nexthop_global, &attr->mp_nexthop_global,
buf, BUFSIZ)); buf, BUFSIZ));
len = 16 - len; len = 16 - len;
@ -6732,13 +6751,6 @@ void route_vty_out(struct vty *vty, struct prefix *p, struct bgp_info *binfo,
bgp_origin_long_str[attr->origin]); bgp_origin_long_str[attr->origin]);
else else
vty_out(vty, "%s", bgp_origin_str[attr->origin]); vty_out(vty, "%s", bgp_origin_str[attr->origin]);
} else {
if (json_paths)
json_object_string_add(json_path, "alert",
"No attributes");
else
vty_out(vty, "No attributes to print\n");
}
if (json_paths) { if (json_paths) {
if (json_nexthop_global || json_nexthop_ll) { if (json_nexthop_global || json_nexthop_ll) {
@ -8152,9 +8164,15 @@ static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi,
bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT ? "Default" bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT ? "Default"
: bgp->name, : bgp->name,
table->version, inet_ntoa(bgp->router_id)); table->version, inet_ntoa(bgp->router_id));
if (rd)
vty_out(vty, " \"routeDistinguishers\" : {");
json_paths = json_object_new_object(); json_paths = json_object_new_object();
} }
if (use_json && rd) {
vty_out(vty, " \"%s\" : { ", rd);
}
/* Start processing of routes. */ /* Start processing of routes. */
for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) { for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
if (rn->info == NULL) if (rn->info == NULL)
@ -8331,8 +8349,6 @@ static int bgp_show_table(struct vty *vty, struct bgp *bgp, safi_t safi,
vty_out(vty, vty_out(vty,
"Route Distinguisher: %s\n", "Route Distinguisher: %s\n",
rd); rd);
else
vty_out(vty, "rd:\"%s\",", rd);
} }
if (type == bgp_show_type_dampend_paths if (type == bgp_show_type_dampend_paths
|| type == bgp_show_type_damp_neighbor) || type == bgp_show_type_damp_neighbor)
@ -8428,6 +8444,8 @@ int bgp_show_table_rd(struct vty *vty, struct bgp *bgp, safi_t safi,
&output_cum, &total_cum); &output_cum, &total_cum);
} }
} }
if (use_json)
vty_out(vty, " } }");
return CMD_SUCCESS; return CMD_SUCCESS;
} }
static int bgp_show(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi, static int bgp_show(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t safi,