bgpd: remove some deep nesting

Some of this was so egregiously stupid, I couldn't look at it without
gouging my eyes out...

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
David Lamparter 2017-08-27 22:51:35 +02:00
parent 2b79110731
commit ea47320b1d
3 changed files with 900 additions and 931 deletions

View File

@ -375,7 +375,9 @@ static int bgp_show_ethernet_vpn(struct vty *vty, struct prefix_rd *prd,
if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
continue;
if ((table = rn->info) != NULL) {
if ((table = rn->info) == NULL)
continue;
rd_header = 1;
for (rm = bgp_table_top(table); rm;
@ -451,14 +453,11 @@ static int bgp_show_ethernet_vpn(struct vty *vty, struct prefix_rd *prd,
type = decode_rd_type(pnt);
/* Decode RD value. */
if (type == RD_TYPE_AS)
decode_rd_as(pnt + 2,
&rd_as);
decode_rd_as(pnt + 2, &rd_as);
else if (type == RD_TYPE_AS4)
decode_rd_as4(pnt + 2,
&rd_as);
decode_rd_as4(pnt + 2, &rd_as);
else if (type == RD_TYPE_IP)
decode_rd_ip(pnt + 2,
&rd_ip);
decode_rd_ip(pnt + 2, &rd_ip);
if (use_json) {
char buffer[BUFSIZ];
if (type == RD_TYPE_AS
@ -504,27 +503,23 @@ static int bgp_show_ethernet_vpn(struct vty *vty, struct prefix_rd *prd,
rd_header = 0;
}
if (use_json)
json_array =
json_object_new_array();
json_array = json_object_new_array();
else
json_array = NULL;
if (option == SHOW_DISPLAY_TAGS)
route_vty_out_tag(
vty, &rm->p, ri, 0,
SAFI_EVPN, json_array);
route_vty_out_tag(vty, &rm->p, ri, 0,
SAFI_EVPN,
json_array);
else if (option == SHOW_DISPLAY_OVERLAY)
route_vty_out_overlay(
vty, &rm->p, ri, 0,
json_array);
route_vty_out_overlay(vty, &rm->p, ri,
0, json_array);
else
route_vty_out(vty, &rm->p, ri,
0, SAFI_EVPN,
json_array);
route_vty_out(vty, &rm->p, ri, 0,
SAFI_EVPN, json_array);
output_count++;
}
/* XXX json */
}
}
if (output_count == 0)
vty_out(vty, "No prefixes displayed, %ld exist\n", total_count);
else

View File

@ -3391,23 +3391,24 @@ static void bgp_soft_reconfig_table(struct peer *peer, afi_t afi, safi_t safi,
for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
for (ain = rn->adj_in; ain; ain = ain->next) {
if (ain->peer == peer) {
if (ain->peer != peer)
continue;
struct bgp_info *ri = rn->info;
mpls_label_t label =
(ri && ri->extra) ? ri->extra->label
: MPLS_INVALID_LABEL;
ret = bgp_update(
peer, &rn->p, ain->addpath_rx_id,
ret = bgp_update(peer, &rn->p, ain->addpath_rx_id,
ain->attr, afi, safi, ZEBRA_ROUTE_BGP,
BGP_ROUTE_NORMAL, prd, &label, 1, NULL);
BGP_ROUTE_NORMAL, prd, &label, 1,
NULL);
if (ret < 0) {
bgp_unlock_node(rn);
return;
}
}
}
}
void bgp_soft_reconfig_in(struct peer *peer, afi_t afi, safi_t safi)
@ -3454,8 +3455,10 @@ static wq_item_status bgp_clear_route_node(struct work_queue *wq, void *data)
/* It is possible that we have multiple paths for a prefix from a peer
* if that peer is using AddPath.
*/
for (ri = rn->info; ri; ri = ri->next)
if (ri->peer == peer) {
for (ri = rn->info; ri; ri = ri->next) {
if (ri->peer != peer)
continue;
/* graceful restart STALE flag set. */
if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)
&& peer->nsf[afi][safi]
@ -3466,9 +3469,8 @@ static wq_item_status bgp_clear_route_node(struct work_queue *wq, void *data)
/* If this is an EVPN route, process for
* un-import. */
if (safi == SAFI_EVPN)
bgp_evpn_unimport_route(peer->bgp, afi,
safi, &rn->p,
ri);
bgp_evpn_unimport_route(peer->bgp, afi, safi,
&rn->p, ri);
bgp_rib_remove(rn, ri, peer, afi, safi);
}
}
@ -3705,32 +3707,31 @@ void bgp_clear_stale_route(struct peer *peer, afi_t afi, safi_t safi)
struct bgp_info *ri;
/* look for neighbor in tables */
if ((table = rn->info) != NULL) {
if ((table = rn->info) == NULL)
continue;
for (rm = bgp_table_top(table); rm;
rm = bgp_route_next(rm))
for (ri = rm->info; ri; ri = ri->next)
if (ri->peer == peer) {
if (CHECK_FLAG(
ri->flags,
for (ri = rm->info; ri; ri = ri->next) {
if (ri->peer != peer)
continue;
if (!CHECK_FLAG(ri->flags,
BGP_INFO_STALE))
bgp_rib_remove(
rm, ri,
peer,
afi,
safi);
break;
}
bgp_rib_remove(rm, ri, peer, afi, safi);
break;
}
}
} else {
for (rn = bgp_table_top(peer->bgp->rib[afi][safi]); rn;
rn = bgp_route_next(rn))
for (ri = rn->info; ri; ri = ri->next)
if (ri->peer == peer) {
if (CHECK_FLAG(ri->flags,
BGP_INFO_STALE))
bgp_rib_remove(rn, ri, peer,
afi, safi);
for (ri = rn->info; ri; ri = ri->next) {
if (ri->peer != peer)
continue;
if (!CHECK_FLAG(ri->flags, BGP_INFO_STALE))
break;
bgp_rib_remove(rn, ri, peer, afi, safi);
break;
}
}
@ -4556,8 +4557,10 @@ void bgp_static_add(struct bgp *bgp)
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
rn = bgp_route_next(rn))
if (rn->info != NULL) {
rn = bgp_route_next(rn)) {
if (rn->info == NULL)
continue;
if ((safi == SAFI_MPLS_VPN)
|| (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN)) {
@ -4594,8 +4597,10 @@ void bgp_static_delete(struct bgp *bgp)
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
rn = bgp_route_next(rn))
if (rn->info != NULL) {
rn = bgp_route_next(rn)) {
if (rn->info == NULL)
continue;
if ((safi == SAFI_MPLS_VPN)
|| (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN)) {
@ -4611,8 +4616,7 @@ void bgp_static_delete(struct bgp *bgp)
(struct
prefix_rd *)&rn
->p);
bgp_static_free(
bgp_static);
bgp_static_free(bgp_static);
rn->info = NULL;
bgp_unlock_node(rn);
}
@ -4641,8 +4645,10 @@ void bgp_static_redo_import_check(struct bgp *bgp)
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
rn = bgp_route_next(rn))
if (rn->info != NULL) {
rn = bgp_route_next(rn)) {
if (rn->info == NULL)
continue;
if ((safi == SAFI_MPLS_VPN)
|| (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN)) {
@ -5684,8 +5690,10 @@ static void bgp_aggregate_add(struct bgp *bgp, struct prefix *p, afi_t afi,
/* If routes exists below this node, generate aggregate routes. */
top = bgp_node_get(table, p);
for (rn = bgp_node_get(table, p); rn;
rn = bgp_route_next_until(rn, top))
if (rn->p.prefixlen > p->prefixlen) {
rn = bgp_route_next_until(rn, top)) {
if (rn->p.prefixlen <= p->prefixlen)
continue;
match = 0;
for (ri = rn->info; ri; ri = ri->next) {
@ -5696,48 +5704,40 @@ static void bgp_aggregate_add(struct bgp *bgp, struct prefix *p, afi_t afi,
& ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE))
atomic_aggregate = 1;
if (ri->sub_type != BGP_ROUTE_AGGREGATE) {
if (ri->sub_type == BGP_ROUTE_AGGREGATE)
continue;
/* summary-only aggregate route suppress
aggregated
route announcement. */
* aggregated route announcement. */
if (aggregate->summary_only) {
(bgp_info_extra_get(ri))
->suppress++;
bgp_info_set_flag(
rn, ri,
(bgp_info_extra_get(ri))->suppress++;
bgp_info_set_flag(rn, ri,
BGP_INFO_ATTR_CHANGED);
match++;
}
/* If at least one route among routes
* that are aggregated has
* ORIGIN with the value INCOMPLETE,
* then the aggregated route
* MUST have the ORIGIN attribute with
* the value INCOMPLETE.
* Otherwise, if at least one route
* among routes that are
* aggregated has ORIGIN with the value
* EGP, then the aggregated
* route MUST have the ORIGIN attribute
* with the value EGP.
/* If at least one route among routes that are
* aggregated has ORIGIN with the value INCOMPLETE,
* then the aggregated route MUST have the ORIGIN
* attribute with the value INCOMPLETE. Otherwise, if
* at least one route among routes that are aggregated
* has ORIGIN with the value EGP, then the aggregated
* route MUST have the ORIGIN attribute with the value
* EGP.
*/
if (origin < ri->attr->origin)
origin = ri->attr->origin;
/* as-set aggregate route generate
origin, as path,
community aggregation. */
/* as-set aggregate route generate origin, as path,
* community aggregation. */
if (aggregate->as_set) {
if (aspath) {
asmerge = aspath_aggregate(
aspath,
asmerge = aspath_aggregate(aspath,
ri->attr->aspath);
aspath_free(aspath);
aspath = asmerge;
} else
aspath = aspath_dup(
ri->attr->aspath);
aspath = aspath_dup(ri->attr->aspath);
if (ri->attr->community) {
if (community) {
@ -5755,7 +5755,6 @@ static void bgp_aggregate_add(struct bgp *bgp, struct prefix *p, afi_t afi,
}
aggregate->count++;
}
}
/* If this node is suppressed, process the change. */
if (match)
@ -5806,29 +5805,29 @@ void bgp_aggregate_delete(struct bgp *bgp, struct prefix *p, afi_t afi,
/* If routes exists below this node, generate aggregate routes. */
top = bgp_node_get(table, p);
for (rn = bgp_node_get(table, p); rn;
rn = bgp_route_next_until(rn, top))
if (rn->p.prefixlen > p->prefixlen) {
rn = bgp_route_next_until(rn, top)) {
if (rn->p.prefixlen <= p->prefixlen)
continue;
match = 0;
for (ri = rn->info; ri; ri = ri->next) {
if (BGP_INFO_HOLDDOWN(ri))
continue;
if (ri->sub_type != BGP_ROUTE_AGGREGATE) {
if (aggregate->summary_only
&& ri->extra) {
if (ri->sub_type == BGP_ROUTE_AGGREGATE)
continue;
if (aggregate->summary_only && ri->extra) {
ri->extra->suppress--;
if (ri->extra->suppress == 0) {
bgp_info_set_flag(
rn, ri,
bgp_info_set_flag(rn, ri,
BGP_INFO_ATTR_CHANGED);
match++;
}
}
aggregate->count--;
}
}
/* If this node was suppressed, process the change. */
if (match)
@ -8561,56 +8560,45 @@ static int bgp_show_route_in_table(struct vty *vty, struct bgp *bgp,
if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
continue;
if ((table = rn->info) != NULL) {
if ((table = rn->info) == NULL)
continue;
header = 1;
if ((rm = bgp_node_match(table, &match))
!= NULL) {
if ((rm = bgp_node_match(table, &match)) == NULL)
continue;
if (prefix_check
&& rm->p.prefixlen
!= match.prefixlen) {
&& rm->p.prefixlen != match.prefixlen) {
bgp_unlock_node(rm);
continue;
}
for (ri = rm->info; ri; ri = ri->next) {
if (header) {
route_vty_out_detail_header(
vty, bgp, rm,
(struct
prefix_rd *)&rn
->p,
AFI_IP, safi,
json);
route_vty_out_detail_header(vty, bgp,
rm, (struct prefix_rd *)&rn->p,
AFI_IP, safi, json);
header = 0;
}
display++;
if (pathtype == BGP_PATH_ALL
|| (pathtype
== BGP_PATH_BESTPATH
&& CHECK_FLAG(
ri->flags,
|| (pathtype == BGP_PATH_BESTPATH
&& CHECK_FLAG(ri->flags,
BGP_INFO_SELECTED))
|| (pathtype
== BGP_PATH_MULTIPATH
&& (CHECK_FLAG(
ri->flags,
|| (pathtype == BGP_PATH_MULTIPATH
&& (CHECK_FLAG(ri->flags,
BGP_INFO_MULTIPATH)
|| CHECK_FLAG(
ri->flags,
|| CHECK_FLAG(ri->flags,
BGP_INFO_SELECTED))))
route_vty_out_detail(
vty, bgp,
&rm->p, ri,
AFI_IP, safi,
route_vty_out_detail(vty, bgp, &rm->p,
ri, AFI_IP, safi,
json_paths);
}
bgp_unlock_node(rm);
}
}
}
} else {
header = 1;
@ -9995,7 +9983,8 @@ static void show_adj_route(struct vty *vty, struct peer *peer, afi_t afi,
for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
if (in) {
for (ain = rn->adj_in; ain; ain = ain->next) {
if (ain->peer == peer) {
if (ain->peer != peer)
continue;
if (header1) {
if (use_json) {
json_object_int_add(
@ -10029,28 +10018,22 @@ static void show_adj_route(struct vty *vty, struct peer *peer, afi_t afi,
}
if (header2) {
if (!use_json)
vty_out(vty,
BGP_SHOW_HEADER);
vty_out(vty, BGP_SHOW_HEADER);
header2 = 0;
}
if (ain->attr) {
bgp_attr_dup(&attr, ain->attr);
if (bgp_input_modifier(
peer, &rn->p, &attr,
afi, safi,
rmap_name)
if (bgp_input_modifier(peer, &rn->p,
&attr, afi, safi, rmap_name)
!= RMAP_DENY) {
route_vty_out_tmp(
vty, &rn->p,
&attr, safi,
use_json,
route_vty_out_tmp(vty, &rn->p,
&attr, safi, use_json,
json_ar);
output_count++;
} else
filtered_count++;
}
}
}
} else {
for (adj = rn->adj_out; adj; adj = adj->next)
SUBGRP_FOREACH_PEER(adj->subgroup, paf)
@ -10856,24 +10839,20 @@ static int bgp_clear_damp_route(struct vty *vty, const char *view_name,
rn = bgp_route_next(rn)) {
if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
continue;
if ((table = rn->info) == NULL)
continue;
if ((rm = bgp_node_match(table, &match)) == NULL)
continue;
if ((table = rn->info) != NULL)
if ((rm = bgp_node_match(table, &match))
!= NULL) {
if (!prefix_check
|| rm->p.prefixlen
== match.prefixlen) {
|| rm->p.prefixlen == match.prefixlen) {
ri = rm->info;
while (ri) {
if (ri->extra
&& ri->extra
->damp_info) {
ri_temp =
ri->next;
&& ri->extra->damp_info) {
ri_temp = ri->next;
bgp_damp_info_free(
ri->extra
->damp_info,
1);
ri->extra->damp_info, 1);
ri = ri_temp;
} else
ri = ri->next;
@ -10882,7 +10861,6 @@ static int bgp_clear_damp_route(struct vty *vty, const char *view_name,
bgp_unlock_node(rm);
}
}
} else {
if ((rn = bgp_node_match(bgp->rib[afi][safi], &match))
!= NULL) {
@ -10990,39 +10968,39 @@ static void bgp_config_write_network_vpn(struct vty *vty, struct bgp *bgp,
/* Network configuration. */
for (prn = bgp_table_top(bgp->route[afi][safi]); prn;
prn = bgp_route_next(prn))
if ((table = prn->info) != NULL)
prn = bgp_route_next(prn)) {
if ((table = prn->info) == NULL)
continue;
for (rn = bgp_table_top(table); rn;
rn = bgp_route_next(rn))
if ((bgp_static = rn->info) != NULL) {
rn = bgp_route_next(rn)) {
if ((bgp_static = rn->info) == NULL)
continue;
p = &rn->p;
prd = (struct prefix_rd *)&prn->p;
/* "network" configuration display. */
prefix_rd2str(prd, rdbuf,
RD_ADDRSTRLEN);
label = decode_label(
&bgp_static->label);
prefix_rd2str(prd, rdbuf, RD_ADDRSTRLEN);
label = decode_label(&bgp_static->label);
vty_out(vty, " network %s/%d rd %s",
inet_ntop(p->family,
&p->u.prefix, buf,
inet_ntop(p->family, &p->u.prefix, buf,
SU_ADDRSTRLEN),
p->prefixlen, rdbuf);
if (safi == SAFI_MPLS_VPN)
vty_out(vty, " label %u",
label);
vty_out(vty, " label %u", label);
if (bgp_static->rmap.name)
vty_out(vty, " route-map %s",
bgp_static->rmap.name);
else {
if (bgp_static->backdoor)
vty_out(vty,
" backdoor");
vty_out(vty, " backdoor");
}
vty_out(vty, "\n");
}
}
}
static void bgp_config_write_network_evpn(struct vty *vty, struct bgp *bgp,
@ -11040,48 +11018,45 @@ static void bgp_config_write_network_evpn(struct vty *vty, struct bgp *bgp,
/* Network configuration. */
for (prn = bgp_table_top(bgp->route[afi][safi]); prn;
prn = bgp_route_next(prn))
if ((table = prn->info) != NULL)
prn = bgp_route_next(prn)) {
if ((table = prn->info) == NULL)
continue;
for (rn = bgp_table_top(table); rn;
rn = bgp_route_next(rn))
if ((bgp_static = rn->info) != NULL) {
rn = bgp_route_next(rn)) {
if ((bgp_static = rn->info) == NULL)
continue;
char *macrouter = NULL;
char *esi = NULL;
if (bgp_static->router_mac)
macrouter = prefix_mac2str(
bgp_static->router_mac,
NULL, 0);
bgp_static->router_mac, NULL, 0);
if (bgp_static->eth_s_id)
esi = esi2str(
bgp_static->eth_s_id);
esi = esi2str(bgp_static->eth_s_id);
p = &rn->p;
prd = (struct prefix_rd *)&prn->p;
/* "network" configuration display. */
prefix_rd2str(prd, rdbuf,
RD_ADDRSTRLEN);
prefix_rd2str(prd, rdbuf, RD_ADDRSTRLEN);
inet_ntop(AF_INET,
&bgp_static->igpnexthop, buf2,
inet_ntop(AF_INET, &bgp_static->igpnexthop, buf2,
SU_ADDRSTRLEN);
prefix2str(p, buf, sizeof(buf)),
prefix2str(p, buf, sizeof(buf));
vty_out(vty,
" network %s rd %s ethtag %u tag %u esi %s gwip %s routermac %s",
buf, rdbuf,
p->u.prefix_evpn
.eth_tag,
decode_label(
&bgp_static
->label),
esi, buf2, macrouter);
vty_out(vty, "\n");
" network %s rd %s ethtag %u tag %u esi %s gwip %s routermac %s\n",
buf, rdbuf, p->u.prefix_evpn .eth_tag,
decode_label(&bgp_static ->label), esi, buf2,
macrouter);
if (macrouter)
XFREE(MTYPE_TMP, macrouter);
if (esi)
XFREE(MTYPE_TMP, esi);
}
}
}
/* Configuration of static route announcement and aggregate
@ -11107,8 +11082,10 @@ void bgp_config_write_network(struct vty *vty, struct bgp *bgp, afi_t afi,
/* Network configuration. */
for (rn = bgp_table_top(bgp->route[afi][safi]); rn;
rn = bgp_route_next(rn))
if ((bgp_static = rn->info) != NULL) {
rn = bgp_route_next(rn)) {
if ((bgp_static = rn->info) == NULL)
continue;
p = &rn->p;
/* "network" configuration display. */
@ -11123,17 +11100,13 @@ void bgp_config_write_network(struct vty *vty, struct bgp *bgp, afi_t afi,
inet_ntop(p->family, &p->u.prefix, buf,
SU_ADDRSTRLEN));
if ((IN_CLASSC(destination)
&& p->prefixlen == 24)
|| (IN_CLASSB(destination)
&& p->prefixlen == 16)
|| (IN_CLASSA(destination)
&& p->prefixlen == 8)
if ((IN_CLASSC(destination) && p->prefixlen == 24)
|| (IN_CLASSB(destination) && p->prefixlen == 16)
|| (IN_CLASSA(destination) && p->prefixlen == 8)
|| p->u.prefix4.s_addr == 0) {
/* Natural mask is not display. */
} else
vty_out(vty, " mask %s",
inet_ntoa(netmask));
vty_out(vty, " mask %s", inet_ntoa(netmask));
} else {
vty_out(vty, " network %s/%d",
inet_ntop(p->family, &p->u.prefix, buf,
@ -11146,8 +11119,7 @@ void bgp_config_write_network(struct vty *vty, struct bgp *bgp, afi_t afi,
bgp_static->label_index);
if (bgp_static->rmap.name)
vty_out(vty, " route-map %s",
bgp_static->rmap.name);
vty_out(vty, " route-map %s", bgp_static->rmap.name);
else {
if (bgp_static->backdoor)
vty_out(vty, " backdoor");
@ -11158,12 +11130,13 @@ void bgp_config_write_network(struct vty *vty, struct bgp *bgp, afi_t afi,
/* Aggregate-address configuration. */
for (rn = bgp_table_top(bgp->aggregate[afi][safi]); rn;
rn = bgp_route_next(rn))
if ((bgp_aggregate = rn->info) != NULL) {
rn = bgp_route_next(rn)) {
if ((bgp_aggregate = rn->info) == NULL)
continue;
p = &rn->p;
if (bgp_option_check(BGP_OPT_CONFIG_CISCO)
&& afi == AFI_IP) {
if (bgp_option_check(BGP_OPT_CONFIG_CISCO) && afi == AFI_IP) {
struct in_addr netmask;
masklen2ip(p->prefixlen, &netmask);

View File

@ -6610,7 +6610,9 @@ static int bgp_show_summary(struct vty *vty, struct bgp *bgp, int afi, int safi,
if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
continue;
if (peer->afc[afi][safi]) {
if (!peer->afc[afi][safi])
continue;
if (!count) {
unsigned long ents;
char memstrbuf[MTYPE_MEMSTR_LEN];
@ -6972,7 +6974,6 @@ static int bgp_show_summary(struct vty *vty, struct bgp *bgp, int afi, int safi,
vty_out(vty, "\n");
}
}
}
if (use_json) {
json_object_object_add(json, "peers", json_peers);