]> git.puffer.fish Git - mirror/frr.git/commitdiff
zebra: Remove STATIC_XXX_IFNAME and use _IFINDEX
authorDonald Sharp <sharpd@cumulusnetworks.com>
Fri, 4 Dec 2015 17:34:42 +0000 (12:34 -0500)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Fri, 4 Dec 2015 18:29:58 +0000 (10:29 -0800)
When we get a static route through an interface convert the interface
name to an ifindex and pass it through to zebra_rib.c. zebra_rib.c
should not care about the ifname.

This code change will allow us to collapse some of the NEXTHOP_XXX types.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
zebra/rib.h
zebra/zebra_rib.c
zebra/zebra_vty.c

index aefe437be2756fa84494684c29b38a00cf6cc8b2..3a47c8ad5e71880a52dc7509eb810a67889831fd 100644 (file)
@@ -186,23 +186,22 @@ struct static_route
 
   /* Flag for this static route's type. */
   u_char type;
-#define STATIC_IPV4_GATEWAY          1
-#define STATIC_IPV4_IFNAME           2
+#define STATIC_IFINDEX               1
+#define STATIC_IPV4_GATEWAY          2
 #define STATIC_IPV4_BLACKHOLE        3
 #define STATIC_IPV6_GATEWAY          4
-#define STATIC_IPV6_GATEWAY_IFNAME   5
-#define STATIC_IPV6_IFNAME           6
+#define STATIC_IPV6_GATEWAY_IFINDEX  5
 
   /*
    * Nexthop value.
    *
-   * Under IPv4 addr and ifname are
+   * Under IPv4 addr and ifindex are
    * used independentyly.
    * STATIC_IPV4_GATEWAY uses addr
-   * STATIC_IPV4_IFNAME uses ifname
+   * STATIC_IFINDEX uses ifindex
    */
   union g_addr addr;
-  char *ifname;
+  unsigned int ifindex;
 
   /* bit flags */
   u_char flags;
@@ -446,11 +445,11 @@ extern void rib_queue_add (struct zebra_t *zebra, struct route_node *rn);
 
 
 extern int
-static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
+static_add_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex,
                  u_char flags, u_short tag, u_char distance, vrf_id_t vrf_id);
 
 extern int
-static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
+static_delete_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex,
                    u_short tag, u_char distance, vrf_id_t vrf_id);
 
 extern int
@@ -471,7 +470,7 @@ extern struct route_table *rib_table_ipv6;
 
 extern int
 static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
-                const char *ifname, u_char flags, u_short tag,
+                unsigned int ifindex, u_char flags, u_short tag,
                  u_char distance, vrf_id_t vrf_id);
 
 extern int
@@ -480,7 +479,7 @@ rib_add_ipv6_multipath (struct prefix *, struct rib *, safi_t,
 
 extern int
 static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
-                   const char *ifname, u_short tag, u_char distance,
+                   unsigned int ifindex, u_short tag, u_char distance,
                     vrf_id_t vrf_id);
 
 extern int rib_gc_dest (struct route_node *rn);
index 25166dea211cc90ab4551ece591a8081f905f851..a3f19a2442e7bb4f5b2c8b77f3258dd6b05337a4 100644 (file)
@@ -252,22 +252,6 @@ rib_nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6)
   return nexthop;
 }
 
-struct nexthop *
-rib_nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6,
-                            char *ifname)
-{
-  struct nexthop *nexthop;
-
-  nexthop = nexthop_new();
-  nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME;
-  nexthop->gate.ipv6 = *ipv6;
-  nexthop->ifname = XSTRDUP (0, ifname);
-
-  rib_nexthop_add (rib, nexthop);
-
-  return nexthop;
-}
-
 struct nexthop *
 rib_nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6,
                              unsigned int ifindex)
@@ -2599,8 +2583,8 @@ static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_ro
          nh_p.u.prefix4 = si->addr.ipv4;
          zebra_register_rnh_static_nh(&nh_p, rn);
          break;
-       case STATIC_IPV4_IFNAME:
-         rib_nexthop_ifname_add (rib, si->ifname);
+       case STATIC_IFINDEX:
+         rib_nexthop_ifindex_add (rib, si->ifindex);
          break;
        case STATIC_IPV4_BLACKHOLE:
          rib_nexthop_blackhole_add (rib);
@@ -2612,11 +2596,8 @@ static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_ro
          nh_p.u.prefix6 = si->addr.ipv6;
          zebra_register_rnh_static_nh(&nh_p, rn);
          break;
-       case STATIC_IPV6_IFNAME:
-         rib_nexthop_ifname_add (rib, si->ifname);
-         break;
-       case STATIC_IPV6_GATEWAY_IFNAME:
-         rib_nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname);
+       case STATIC_IPV6_GATEWAY_IFINDEX:
+         rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex);
          break;
         }
 
@@ -2655,8 +2636,8 @@ static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_ro
          nh_p.u.prefix4 = si->addr.ipv4;
          zebra_register_rnh_static_nh(&nh_p, rn);
          break;
-       case STATIC_IPV4_IFNAME:
-         rib_nexthop_ifname_add (rib, si->ifname);
+       case STATIC_IFINDEX:
+         rib_nexthop_ifindex_add (rib, si->ifindex);
          break;
        case STATIC_IPV4_BLACKHOLE:
          rib_nexthop_blackhole_add (rib);
@@ -2668,11 +2649,8 @@ static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_ro
          nh_p.u.prefix6 = si->addr.ipv6;
          zebra_register_rnh_static_nh(&nh_p, rn);
          break;
-       case STATIC_IPV6_IFNAME:
-         rib_nexthop_ifname_add (rib, si->ifname);
-         break;
-       case STATIC_IPV6_GATEWAY_IFNAME:
-         rib_nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname);
+       case STATIC_IPV6_GATEWAY_IFINDEX:
+         rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex);
          break;
         }
 
@@ -2701,9 +2679,9 @@ static_nexthop_same (struct nexthop *nexthop, struct static_route *si)
       && si->type == STATIC_IPV4_GATEWAY
       && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->addr.ipv4))
     return 1;
-  if (nexthop->type == NEXTHOP_TYPE_IFNAME
-      && si->type == STATIC_IPV4_IFNAME
-      && strcmp (nexthop->ifname, si->ifname) == 0)
+  if (nexthop->type == NEXTHOP_TYPE_IFINDEX
+      && si->type == STATIC_IFINDEX
+      && nexthop->ifindex == si->ifindex)
     return 1;
   if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE
       && si->type == STATIC_IPV4_BLACKHOLE)
@@ -2712,14 +2690,10 @@ static_nexthop_same (struct nexthop *nexthop, struct static_route *si)
       && si->type == STATIC_IPV6_GATEWAY
       && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6))
     return 1;
-  if (nexthop->type == NEXTHOP_TYPE_IFNAME
-      && si->type == STATIC_IPV6_IFNAME
-      && strcmp (nexthop->ifname, si->ifname) == 0)
-    return 1;
-  if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
-      && si->type == STATIC_IPV6_GATEWAY_IFNAME
+  if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
+      && si->type == STATIC_IPV6_GATEWAY_IFINDEX
       && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6)
-      && strcmp (nexthop->ifname, si->ifname) == 0)
+      && nexthop->ifindex == si->ifindex)
     return 1;
   return 0;
 }
@@ -2830,7 +2804,7 @@ static_uninstall_route (afi_t afi, safi_t safi, struct prefix *p, struct static_
 
 /* Add static route into static route configuration. */
 int
-static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
+static_add_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex,
                 u_char flags, u_short tag, u_char distance, vrf_id_t vrf_id)
 {
   u_char type = 0;
@@ -2851,8 +2825,8 @@ static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
   /* Make flags. */
   if (gate)
     type = STATIC_IPV4_GATEWAY;
-  else if (ifname)
-    type = STATIC_IPV4_IFNAME;
+  else if (ifindex)
+    type = STATIC_IFINDEX;
   else
     type = STATIC_IPV4_BLACKHOLE;
 
@@ -2861,7 +2835,7 @@ static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
     {
       if (type == si->type
          && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
-         && (! ifname || strcmp (ifname, si->ifname) == 0))
+         && (! ifindex || ifindex == si->ifindex))
        {
          if ((distance == si->distance) && (tag == si->tag))
            {
@@ -2875,7 +2849,7 @@ static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
 
   /* Distance or tag changed. */
   if (update)
-    static_delete_ipv4 (p, gate, ifname, update->tag, update->distance, vrf_id);
+    static_delete_ipv4 (p, gate, ifindex, update->tag, update->distance, vrf_id);
 
   /* Make new static route structure. */
   si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
@@ -2885,11 +2859,10 @@ static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
   si->flags = flags;
   si->tag = tag;
   si->vrf_id = vrf_id;
+  si->ifindex = ifindex;
 
   if (gate)
     si->addr.ipv4 = *gate;
-  if (ifname)
-    si->ifname = XSTRDUP (0, ifname);
 
   /* Add new static route information to the tree with sort by
      distance value and gateway address. */
@@ -2926,7 +2899,7 @@ static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
 
 /* Delete static route from static route configuration. */
 int
-static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
+static_delete_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex,
                    u_short tag, u_char distance, vrf_id_t vrf_id)
 {
   u_char type = 0;
@@ -2947,8 +2920,8 @@ static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
   /* Make flags. */
   if (gate)
     type = STATIC_IPV4_GATEWAY;
-  else if (ifname)
-    type = STATIC_IPV4_IFNAME;
+  else if (ifindex)
+    type = STATIC_IFINDEX;
   else
     type = STATIC_IPV4_BLACKHOLE;
 
@@ -2956,7 +2929,7 @@ static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
   for (si = rn->info; si; si = si->next)
     if (type == si->type
        && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
-       && (! ifname || strcmp (ifname, si->ifname) == 0)
+       && (! ifindex || ifindex == si->ifindex)
        && (! tag || (tag == si->tag)))
       break;
 
@@ -2980,8 +2953,6 @@ static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
   route_unlock_node (rn);
   
   /* Free static route configuration. */
-  if (ifname)
-    XFREE (0, si->ifname);
   XFREE (MTYPE_STATIC_ROUTE, si);
 
   route_unlock_node (rn);
@@ -3378,7 +3349,7 @@ rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
 /* Add static route into static route configuration. */
 int
 static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
-                const char *ifname, u_char flags, u_short tag,
+                unsigned int ifindex, u_char flags, u_short tag,
                  u_char distance, vrf_id_t vrf_id)
 {
   struct route_node *rn;
@@ -3393,11 +3364,11 @@ static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
     return -1;
     
   if (!gate &&
-      (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME))
+      (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFINDEX))
     return -1;
   
-  if (!ifname && 
-      (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME))
+  if (!ifindex &&
+      (type == STATIC_IPV6_GATEWAY_IFINDEX || type == STATIC_IFINDEX))
     return -1;
 
   /* Lookup static route prefix. */
@@ -3408,7 +3379,7 @@ static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
     {
       if (type == si->type
          && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
-         && (! ifname || strcmp (ifname, si->ifname) == 0))
+         && (! ifindex || ifindex ==  si->ifindex))
        {
          if ((distance == si->distance) && (tag == si->tag))
            {
@@ -3422,7 +3393,7 @@ 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, ifname, update->tag, update->distance, vrf_id);
+    static_delete_ipv6 (p, type, gate, ifindex, update->tag, update->distance, vrf_id);
 
   /* Make new static route structure. */
   si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
@@ -3432,18 +3403,15 @@ static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
   si->flags = flags;
   si->tag = tag;
   si->vrf_id = vrf_id;
+  si->ifindex = ifindex;
 
   switch (type)
     {
     case STATIC_IPV6_GATEWAY:
       si->addr.ipv6 = *gate;
       break;
-    case STATIC_IPV6_IFNAME:
-      si->ifname = XSTRDUP (0, ifname);
-      break;
-    case STATIC_IPV6_GATEWAY_IFNAME:
+    case STATIC_IPV6_GATEWAY_IFINDEX:
       si->addr.ipv6 = *gate;
-      si->ifname = XSTRDUP (0, ifname);
       break;
     }
 
@@ -3476,7 +3444,7 @@ static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
 /* Delete static route from static route configuration. */
 int
 static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
-                   const char *ifname, u_short tag, u_char distance,
+                   unsigned int ifindex, u_short tag, u_char distance,
                     vrf_id_t vrf_id)
 {
   struct route_node *rn;
@@ -3498,7 +3466,7 @@ static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
     if (distance == si->distance 
        && type == si->type
        && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
-       && (! ifname || strcmp (ifname, si->ifname) == 0)
+       && (! ifindex || ifindex == si->ifindex)
        && (! tag || (tag == si->tag)))
       break;
 
@@ -3521,8 +3489,6 @@ static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
     si->next->prev = si->prev;
   
   /* Free static route configuration. */
-  if (ifname)
-    XFREE (0, si->ifname);
   XFREE (MTYPE_STATIC_ROUTE, si);
 
   return 1;
index 2d7333a3b5f955463836fbfb6320638854c6dbcc..a2c6edbf62069f7cabbcd4a3c7a5038d03041beb 100644 (file)
@@ -49,11 +49,11 @@ zebra_static_ipv4 (struct vty *vty, int add_cmd, const char *dest_str,
   struct prefix p;
   struct in_addr gate;
   struct in_addr mask;
-  const char *ifname;
   u_char flag = 0;
   u_short tag = 0;
   vrf_id_t vrf_id = VRF_DEFAULT;
-  
+  unsigned int ifindex = 0;
+
   ret = str2prefix (dest_str, &p);
   if (ret <= 0)
     {
@@ -99,9 +99,9 @@ zebra_static_ipv4 (struct vty *vty, int add_cmd, const char *dest_str,
           return CMD_WARNING;
         }
       if (add_cmd)
-        static_add_ipv4 (&p, NULL, NULL, ZEBRA_FLAG_BLACKHOLE, tag, distance, vrf_id);
+        static_add_ipv4 (&p, NULL, ifindex, ZEBRA_FLAG_BLACKHOLE, tag, distance, vrf_id);
       else
-        static_delete_ipv4 (&p, NULL, NULL, tag, distance, vrf_id);
+        static_delete_ipv4 (&p, NULL, ifindex, tag, distance, vrf_id);
       return CMD_SUCCESS;
     }
 
@@ -125,9 +125,9 @@ zebra_static_ipv4 (struct vty *vty, int add_cmd, const char *dest_str,
   if (gate_str == NULL)
   {
     if (add_cmd)
-      static_add_ipv4 (&p, NULL, NULL, flag, tag, distance, vrf_id);
+      static_add_ipv4 (&p, NULL, ifindex, flag, tag, distance, vrf_id);
     else
-      static_delete_ipv4 (&p, NULL, NULL, tag, distance, vrf_id);
+      static_delete_ipv4 (&p, NULL, ifindex, tag, distance, vrf_id);
 
     return CMD_SUCCESS;
   }
@@ -135,15 +135,21 @@ zebra_static_ipv4 (struct vty *vty, int add_cmd, const char *dest_str,
   /* When gateway is A.B.C.D format, gate is treated as nexthop
      address other case gate is treated as interface name. */
   ret = inet_aton (gate_str, &gate);
-  if (ret)
-    ifname = NULL;
-  else
-    ifname = gate_str;
+  if (!ret)
+    {
+      struct interface *ifp = if_lookup_by_name_vrf (gate_str, vrf_id);
+      if (!ifp)
+       {
+         vty_out (vty, "%% Unknown interface: %s%s", gate_str, VTY_NEWLINE);
+         return CMD_WARNING;
+       }
+      ifindex = ifp->ifindex;
+    }
 
   if (add_cmd)
-    static_add_ipv4 (&p, ifname ? NULL : &gate, ifname, flag, tag, distance, vrf_id);
+    static_add_ipv4 (&p, ifindex ? NULL : &gate, ifindex, flag, tag, distance, vrf_id);
   else
-    static_delete_ipv4 (&p, ifname ? NULL : &gate, ifname, tag, distance, vrf_id);
+    static_delete_ipv4 (&p, ifindex ? NULL : &gate, ifindex, tag, distance, vrf_id);
 
   return CMD_SUCCESS;
 }
@@ -3043,8 +3049,8 @@ static_config_ipv4 (struct vty *vty, safi_t safi, const char *cmd)
               case STATIC_IPV4_GATEWAY:
                 vty_out (vty, " %s", inet_ntoa (si->addr.ipv4));
                 break;
-              case STATIC_IPV4_IFNAME:
-                vty_out (vty, " %s", si->ifname);
+              case STATIC_IFINDEX:
+                vty_out (vty, " %s", ifindex2ifname_vrf(si->ifindex, si->vrf_id));
                 break;
               case STATIC_IPV4_BLACKHOLE:
                 vty_out (vty, " Null0");
@@ -3178,6 +3184,8 @@ static_ipv6_func (struct vty *vty, int add_cmd, const char *dest_str,
   vrf_id_t vrf_id = VRF_DEFAULT;
   u_char flag = 0;
   u_short tag = 0;
+  unsigned int ifindex = 0;
+  struct interface *ifp = NULL;
   
   ret = str2prefix (dest_str, &p);
   if (ret <= 0)
@@ -3229,8 +3237,15 @@ static_ipv6_func (struct vty *vty, int add_cmd, const char *dest_str,
          vty_out (vty, "%% Malformed address%s", VTY_NEWLINE);
          return CMD_WARNING;
        }
-      type = STATIC_IPV6_GATEWAY_IFNAME;
+      type = STATIC_IPV6_GATEWAY_IFINDEX;
       gate = &gate_addr;
+      ifp = if_lookup_by_name_vrf (ifname, vrf_id);
+      if (!ifp)
+       {
+         vty_out (vty, "%% Malformed Interface name %s%s", ifname, VTY_NEWLINE);
+         return CMD_WARNING;
+       }
+      ifindex = ifp->ifindex;
     }
   else
     {
@@ -3241,8 +3256,14 @@ static_ipv6_func (struct vty *vty, int add_cmd, const char *dest_str,
        }
       else
        {
-         type = STATIC_IPV6_IFNAME;
-         ifname = gate_str;
+         type = STATIC_IFINDEX;
+         ifp = if_lookup_by_name_vrf (gate_str, vrf_id);
+         if (!ifp)
+           {
+             vty_out (vty, "%% Malformed Interface name %s%s", gate_str, VTY_NEWLINE);
+             return CMD_WARNING;
+           }
+         ifindex = ifp->ifindex;
        }
     }
 
@@ -3251,9 +3272,9 @@ static_ipv6_func (struct vty *vty, int add_cmd, const char *dest_str,
     VTY_GET_INTEGER ("VRF ID", vrf_id, vrf_id_str);
 
   if (add_cmd)
-    static_add_ipv6 (&p, type, gate, ifname, flag, tag, distance, vrf_id);
+    static_add_ipv6 (&p, type, gate, ifindex, flag, tag, distance, vrf_id);
   else
-    static_delete_ipv6 (&p, type, gate, ifname, tag, distance, vrf_id);
+    static_delete_ipv6 (&p, type, gate, ifindex, tag, distance, vrf_id);
 
   return CMD_SUCCESS;
 }
@@ -5189,12 +5210,13 @@ static_config_ipv6 (struct vty *vty)
              case STATIC_IPV6_GATEWAY:
                vty_out (vty, " %s", inet_ntop (AF_INET6, &si->addr.ipv6, buf, BUFSIZ));
                break;
-             case STATIC_IPV6_IFNAME:
-               vty_out (vty, " %s", si->ifname);
+             case STATIC_IFINDEX:
+               vty_out (vty, " %s", ifindex2ifname_vrf (si->ifindex, si->vrf_id));
                break;
-             case STATIC_IPV6_GATEWAY_IFNAME:
+             case STATIC_IPV6_GATEWAY_IFINDEX:
                vty_out (vty, " %s %s",
-                        inet_ntop (AF_INET6, &si->addr.ipv6, buf, BUFSIZ), si->ifname);
+                        inet_ntop (AF_INET6, &si->addr.ipv6, buf, BUFSIZ),
+                        ifindex2ifname_vrf (si->ifindex, si->vrf_id));
                break;
              }