From da11a696bcb3f75f2941237590241ff91ca165b0 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 23 Nov 2015 13:29:39 -0800 Subject: [PATCH] bgpd: Refactor some code for nexthop handling A upstream CR comment was that we had a function that worked the same for v4 and v6, the code should be consolidated. Signed-off-by: Donald Sharp --- bgpd/bgp_nexthop.c | 163 +++++++++++++++++---------------------------- 1 file changed, 60 insertions(+), 103 deletions(-) diff --git a/bgpd/bgp_nexthop.c b/bgpd/bgp_nexthop.c index dafabe22ca..856e6c464d 100644 --- a/bgpd/bgp_nexthop.c +++ b/bgpd/bgp_nexthop.c @@ -391,120 +391,77 @@ show_ip_bgp_nexthop_table (struct vty *vty, int detail) { struct bgp_node *rn; struct bgp_nexthop_cache *bnc; - char buf[INET6_ADDRSTRLEN]; + char buf[PREFIX2STR_BUFFER]; struct nexthop *nexthop; time_t tbuf; + afi_t afi; vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE); - for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn)) - if ((bnc = rn->info) != NULL) - { - if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) + for (afi = AFI_IP ; afi < AFI_MAX ; afi++) + { + for (rn = bgp_table_top (bgp_nexthop_cache_table[afi]); rn; rn = bgp_route_next (rn)) { - vty_out (vty, " %s valid [IGP metric %d], #paths %d%s", - inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), - bnc->metric, bnc->path_count, VTY_NEWLINE); - if (detail) - for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) - switch (nexthop->type) + if ((bnc = rn->info) != NULL) + { + if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) { - case NEXTHOP_TYPE_IPV4: - vty_out (vty, " gate %s%s", - inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, - INET6_ADDRSTRLEN), VTY_NEWLINE); - break; - case NEXTHOP_TYPE_IFINDEX: - vty_out (vty, " if %s%s", - ifindex2ifname(nexthop->ifindex), VTY_NEWLINE); - break; - case NEXTHOP_TYPE_IPV4_IFINDEX: - vty_out (vty, " gate %s, if %s%s", - inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, - INET6_ADDRSTRLEN), - ifindex2ifname(nexthop->ifindex), VTY_NEWLINE); - break; - default: - vty_out (vty, " invalid nexthop type %u%s", - nexthop->type, VTY_NEWLINE); + vty_out (vty, " %s valid [IGP metric %d], #paths %d%s", + inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf)), + bnc->metric, bnc->path_count, VTY_NEWLINE); + if (detail) + for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) + switch (nexthop->type) + { + case NEXTHOP_TYPE_IPV6: + vty_out (vty, " gate %s%s", + inet_ntop (AF_INET6, &nexthop->gate.ipv6, + buf, sizeof (buf)), VTY_NEWLINE); + break; + case NEXTHOP_TYPE_IPV6_IFINDEX: + vty_out(vty, " gate %s, if %s%s", + inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, + sizeof (buf)), + ifindex2ifname(nexthop->ifindex), + VTY_NEWLINE); + break; + case NEXTHOP_TYPE_IPV4: + vty_out (vty, " gate %s%s", + inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, + sizeof (buf)), VTY_NEWLINE); + break; + case NEXTHOP_TYPE_IFINDEX: + vty_out (vty, " if %s%s", + ifindex2ifname(nexthop->ifindex), VTY_NEWLINE); + break; + case NEXTHOP_TYPE_IPV4_IFINDEX: + vty_out (vty, " gate %s, if %s%s", + inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, + sizeof (buf)), + ifindex2ifname(nexthop->ifindex), VTY_NEWLINE); + break; + default: + vty_out (vty, " invalid nexthop type %u%s", + nexthop->type, VTY_NEWLINE); + } + } + else + { + vty_out (vty, " %s invalid%s", + inet_ntop (rn->p.family, &rn->p.u.prefix, + buf, sizeof (buf)), VTY_NEWLINE); + if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) + vty_out (vty, " Must be Connected%s", VTY_NEWLINE); } - } - else - { - vty_out (vty, " %s invalid%s", - inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE); - - if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) - vty_out (vty, " Must be Connected%s", VTY_NEWLINE); - } -#ifdef HAVE_CLOCK_MONOTONIC - tbuf = time(NULL) - (bgp_clock() - bnc->last_update); - vty_out (vty, " Last update: %s", ctime(&tbuf)); -#else - vty_out (vty, " Last update: %s", ctime(&bnc->uptime)); -#endif /* HAVE_CLOCK_MONOTONIC */ - - vty_out(vty, "%s", VTY_NEWLINE); - } - -#ifdef HAVE_IPV6 - { - for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]); - rn; - rn = bgp_route_next (rn)) - if ((bnc = rn->info) != NULL) - { - if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) - { - vty_out (vty, " %s valid [IGP metric %d]%s", - inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, - INET6_ADDRSTRLEN), - bnc->metric, VTY_NEWLINE); - if (detail) - for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) - switch (nexthop->type) - { - case NEXTHOP_TYPE_IPV6: - vty_out (vty, " gate %s%s", - inet_ntop (AF_INET6, &nexthop->gate.ipv6, - buf, INET6_ADDRSTRLEN), VTY_NEWLINE); - break; - case NEXTHOP_TYPE_IPV6_IFINDEX: - vty_out(vty, " gate %s, if %s%s", - inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, - INET6_ADDRSTRLEN), - ifindex2ifname(nexthop->ifindex), - VTY_NEWLINE); - break; - case NEXTHOP_TYPE_IFINDEX: - vty_out (vty, " if %s%s", - ifindex2ifname(nexthop->ifindex), - VTY_NEWLINE); - break; - default: - vty_out (vty, " invalid nexthop type %u%s", - nexthop->type, VTY_NEWLINE); - } - } - else - { - vty_out (vty, " %s invalid%s", - inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN), - VTY_NEWLINE); - - if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) - vty_out (vty, " Must be Connected%s", VTY_NEWLINE); - } #ifdef HAVE_CLOCK_MONOTONIC - tbuf = time(NULL) - (bgp_clock() - bnc->last_update); - vty_out (vty, " Last update: %s", ctime(&tbuf)); + tbuf = time(NULL) - (bgp_clock() - bnc->last_update); + vty_out (vty, " Last update: %s", ctime(&tbuf)); #else - vty_out (vty, " Last update: %s", ctime(&bnc->uptime)); + vty_out (vty, " Last update: %s", ctime(&bnc->uptime)); #endif /* HAVE_CLOCK_MONOTONIC */ - - vty_out(vty, "%s", VTY_NEWLINE); + vty_out(vty, "%s", VTY_NEWLINE); + } } - } -#endif /* HAVE_IPV6 */ + } return CMD_SUCCESS; } -- 2.39.5