From c7cfcb75e926f19f833d3c7881127bc743e4c661 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 24 Aug 2016 00:07:49 -0400 Subject: [PATCH] zebra: Refactor v4 and v6 static_delete Refactor v4 and v6 static delete into 1 function. Signed-off-by: Donald Sharp --- zebra/zebra_static.c | 80 +++++++------------------------------------- zebra/zebra_static.h | 11 +++--- zebra/zebra_vty.c | 13 ++++--- 3 files changed, 25 insertions(+), 79 deletions(-) diff --git a/zebra/zebra_static.c b/zebra/zebra_static.c index 9d16f12679..74a37c45c1 100644 --- a/zebra/zebra_static.c +++ b/zebra/zebra_static.c @@ -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; -} diff --git a/zebra/zebra_static.h b/zebra/zebra_static.h index a3096725f7..b2f5ea0bb1 100644 --- a/zebra/zebra_static.h +++ b/zebra/zebra_static.h @@ -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 diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c index 716e74c0b0..f8774f0026 100644 --- a/zebra/zebra_vty.c +++ b/zebra/zebra_vty.c @@ -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; } -- 2.39.5