zebra: Refactor v4 and v6 static_delete

Refactor v4 and v6 static delete into 1 function.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
This commit is contained in:
Donald Sharp 2016-08-24 00:07:49 -04:00
parent 28f6dde859
commit c7cfcb75e9
3 changed files with 25 additions and 79 deletions

View File

@ -355,7 +355,8 @@ static_add_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, ifindex_t
/* Distance or tag changed. */
if (update)
static_delete_ipv4 (safi, p, gate, ifindex, update->tag, update->distance, zvrf);
static_delete_route (AFI_IP, safi, type, p, (union g_addr *)gate,
ifindex, update->tag, update->distance, zvrf);
/* Make new static route structure. */
si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
@ -406,16 +407,16 @@ static_add_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, ifindex_t
}
int
static_delete_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, ifindex_t ifindex,
u_short tag, u_char distance, struct zebra_vrf *zvrf)
static_delete_route (afi_t afi, safi_t safi, u_char type, struct prefix *p,
union g_addr *gate, ifindex_t ifindex,
u_short tag, u_char distance, struct zebra_vrf *zvrf)
{
u_char type = 0;
struct route_node *rn;
struct static_route *si;
struct route_table *stable;
/* Lookup table. */
stable = zebra_vrf_static_table (AFI_IP, safi, zvrf);
stable = zebra_vrf_static_table (afi, safi, zvrf);
if (! stable)
return -1;
@ -424,18 +425,12 @@ static_delete_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, ifindex
if (! rn)
return 0;
/* Make flags. */
if (gate)
type = STATIC_IPV4_GATEWAY;
else if (ifindex)
type = STATIC_IFINDEX;
else
type = STATIC_IPV4_BLACKHOLE;
/* Find same static route is the tree */
for (si = rn->info; si; si = si->next)
if (type == si->type
&& (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
&& (! gate || (
(afi == AFI_IP && IPV4_ADDR_SAME (gate, &si->addr.ipv4)) ||
(afi == AFI_IP6 && IPV6_ADDR_SAME (gate, &si->addr.ipv6))))
&& (! ifindex || ifindex == si->ifindex)
&& (! tag || (tag == si->tag)))
break;
@ -513,7 +508,9 @@ static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
/* Distance or tag changed. */
if (update)
static_delete_ipv6 (p, type, gate, ifindex, update->tag, update->distance, zvrf);
static_delete_route (AFI_IP6, SAFI_UNICAST, type, p,
(union g_addr *)gate, ifindex,
update->tag, update->distance, zvrf);
/* Make new static route structure. */
si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
@ -562,56 +559,3 @@ static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
return 1;
}
/* Delete static route from static route configuration. */
int
static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
ifindex_t ifindex, u_short tag, u_char distance,
struct zebra_vrf *zvrf)
{
struct route_node *rn;
struct static_route *si;
struct route_table *stable;
/* Lookup table. */
stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, zvrf);
if (! stable)
return -1;
/* Lookup static route prefix. */
rn = route_node_lookup (stable, p);
if (! rn)
return 0;
/* Find same static route is the tree */
for (si = rn->info; si; si = si->next)
if (distance == si->distance
&& type == si->type
&& (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
&& (! ifindex || ifindex == si->ifindex)
&& (! tag || (tag == si->tag)))
break;
/* Can't find static route. */
if (! si)
{
route_unlock_node (rn);
return 0;
}
/* Install into rib. */
static_uninstall_route (AFI_IP6, SAFI_UNICAST, p, si);
/* Unlink static route from linked list. */
if (si->prev)
si->prev->next = si->next;
else
rn->info = si->next;
if (si->next)
si->next->prev = si->prev;
/* Free static route configuration. */
XFREE (MTYPE_STATIC_ROUTE, si);
return 1;
}

View File

@ -79,17 +79,14 @@ static_add_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, ifindex_t
u_char distance, struct zebra_vrf *zvrf);
extern int
static_delete_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, ifindex_t ifindex,
u_short tag, u_char distance, struct zebra_vrf *zvrf);
static_delete_route (afi_t, safi_t safi, u_char type, struct prefix *p,
union g_addr *gate, ifindex_t ifindex,
u_short tag, u_char distance,
struct zebra_vrf *zvrf);
extern int
static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
ifindex_t ifindex, const char *ifname, u_char flags,
u_short tag, u_char distance, struct zebra_vrf *zvrf);
extern int
static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
ifindex_t ifindex, u_short tag, u_char distance,
struct zebra_vrf *zvrf);
#endif

View File

@ -66,6 +66,7 @@ zebra_static_ipv4 (struct vty *vty, safi_t safi, int add_cmd,
struct zebra_vrf *zvrf = NULL;
unsigned int ifindex = 0;
const char *ifname = NULL;
u_char type = STATIC_IPV4_BLACKHOLE;
ret = str2prefix (dest_str, &p);
if (ret <= 0)
@ -119,7 +120,7 @@ zebra_static_ipv4 (struct vty *vty, safi_t safi, int add_cmd,
if (add_cmd)
static_add_ipv4 (safi, &p, NULL, ifindex, ifname, ZEBRA_FLAG_BLACKHOLE, tag, distance, zvrf);
else
static_delete_ipv4 (safi, &p, NULL, ifindex, tag, distance, zvrf);
static_delete_route (AFI_IP, safi, type, &p, NULL, ifindex, tag, distance, zvrf);
return CMD_SUCCESS;
}
@ -142,10 +143,11 @@ zebra_static_ipv4 (struct vty *vty, safi_t safi, int add_cmd,
if (gate_str == NULL)
{
type = STATIC_IFINDEX;
if (add_cmd)
static_add_ipv4 (safi, &p, NULL, ifindex, ifname, flag, tag, distance, zvrf);
else
static_delete_ipv4 (safi, &p, NULL, ifindex, tag, distance, zvrf);
static_delete_route (AFI_IP, safi, type, &p, NULL, ifindex, tag, distance, zvrf);
return CMD_SUCCESS;
}
@ -164,12 +166,15 @@ zebra_static_ipv4 (struct vty *vty, safi_t safi, int add_cmd,
else
ifindex = ifp->ifindex;
ifname = gate_str;
type = STATIC_IFINDEX;
}
else
type = STATIC_IPV4_GATEWAY;
if (add_cmd)
static_add_ipv4 (safi, &p, ifindex ? NULL : &gate, ifindex, ifname, flag, tag, distance, zvrf);
else
static_delete_ipv4 (safi, &p, ifindex ? NULL : &gate, ifindex, tag, distance, zvrf);
static_delete_route (AFI_IP, safi, type, &p, ifindex ? NULL : (union g_addr *)&gate, ifindex, tag, distance, zvrf);
return CMD_SUCCESS;
}
@ -3735,7 +3740,7 @@ static_ipv6_func (struct vty *vty, int add_cmd, const char *dest_str,
if (add_cmd)
static_add_ipv6 (&p, type, gate, ifindex, ifname, flag, tag, distance, zvrf);
else
static_delete_ipv6 (&p, type, gate, ifindex, tag, distance, zvrf);
static_delete_route (AFI_IP6, SAFI_UNICAST, type, &p, (union g_addr *)gate, ifindex, tag, distance, zvrf);
return CMD_SUCCESS;
}