[quagga-dev 3601] [PATCH] clean v4/v6 code in zebra/redistribute.c

Hugo Santos hsantos at av.it.pt
Sun Aug 21 00:05:13 BST 2005


Hi,

Attached patch's Changelog:

* redistribute.c: remove v4/v6 code duplication by aggregating the
  functionality in new functions with family/cmd arguments.

Please review, any comments are welcome.

Hugo Santos

-------------- next part --------------
Index: zebra/redistribute.c
===================================================================
RCS file: /var/cvsroot/quagga/zebra/redistribute.c,v
retrieving revision 1.10
diff -u -p -w -b -r1.10 redistribute.c
--- zebra/redistribute.c	29 Jul 2005 14:36:00 -0000	1.10
+++ zebra/redistribute.c	20 Aug 2005 22:59:57 -0000
@@ -83,86 +83,94 @@ is_default (struct prefix *p)
 }
 
 static void
-zebra_redistribute_default (struct zserv *client)
+zsend_route_add (struct zserv *client, struct prefix *p, struct rib *rib)
 {
-  struct prefix_ipv4 p;
-  struct route_table *table;
-  struct route_node *rn;
-  struct rib *newrib;
+  if (p->family == AF_INET)
+    zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
 #ifdef HAVE_IPV6
-  struct prefix_ipv6 p6;
+  else if (p->family == AF_INET6)
+    zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
 #endif /* HAVE_IPV6 */
+}
 
-
-  /* Lookup default route. */
-  memset (&p, 0, sizeof (struct prefix_ipv4));
-  p.family = AF_INET;
-
-  /* Lookup table.  */
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
-  if (table)
-    {
-      rn = route_node_lookup (table, (struct prefix *)&p);
-      if (rn)
+static void
+zsend_route_delete (struct zserv *client, struct prefix *p, struct rib *rib)
 	{
-	  for (newrib = rn->info; newrib; newrib = newrib->next)
-	    if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
-		&& newrib->distance != DISTANCE_INFINITY)
-	      zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
-	  route_unlock_node (rn);
-	}
+  if (p->family == AF_INET)
+    zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
+#ifdef HAVE_IPV6
+  else if (p->family == AF_INET6)
+    zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
+#endif /* HAVE_IPV6 */
     }
 
-#ifdef HAVE_IPV6
+static void
+redistribute_default (struct route_table *table,
+		      int family, struct zserv *client)
+{
+  struct prefix p;
+  struct route_node *rn;
+  struct rib *newrib;
+
+  if (!table)
+    return;
+
   /* Lookup default route. */
-  memset (&p6, 0, sizeof (struct prefix_ipv6));
-  p6.family = AF_INET6;
+  memset (&p, 0, sizeof(struct prefix));
+  p.family = family;
 
-  /* Lookup table.  */
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
-  if (table)
-    {
-      rn = route_node_lookup (table, (struct prefix *)&p6);
+  rn = route_node_lookup (table, &p);
       if (rn)
 	{
 	  for (newrib = rn->info; newrib; newrib = newrib->next)
 	    if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
 		&& newrib->distance != DISTANCE_INFINITY)
-	      zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
+	      zsend_route_add (client, &rn->p, newrib);
 	  route_unlock_node (rn);
 	}
     }
+
+static void
+zebra_redistribute_default (struct zserv *client)
+{
+  redistribute_default (vrf_table (AFI_IP, SAFI_UNICAST, 0),
+			AF_INET, client);
+
+#ifdef HAVE_IPV6
+  redistribute_default (vrf_table (AFI_IP6, SAFI_UNICAST, 0),
+			AF_INET6, client);
 #endif /* HAVE_IPV6 */
 }
 
-/* Redistribute routes. */
 static void
-zebra_redistribute (struct zserv *client, int type)
+redistribute_table (struct route_table *table,
+		    int type, struct zserv *client)
 {
   struct rib *newrib;
-  struct route_table *table;
   struct route_node *rn;
 
-  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
-  if (table)
+  if (!table)
+    return;
+
     for (rn = route_top (table); rn; rn = route_next (rn))
       for (newrib = rn->info; newrib; newrib = newrib->next)
 	if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED) 
 	    && newrib->type == type 
 	    && newrib->distance != DISTANCE_INFINITY
 	    && zebra_check_addr (&rn->p))
-	  zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, &rn->p, newrib);
+	  zsend_route_multipath (type, client, &rn->p, newrib);
+}
+
+/* Redistribute routes. */
+static void
+zebra_redistribute (struct zserv *client, int type)
+{
+  redistribute_table (vrf_table (AFI_IP, SAFI_UNICAST, 0),
+		      ZEBRA_IPV4_ROUTE_ADD, client);
   
 #ifdef HAVE_IPV6
-  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);
-  if (table)
-    for (rn = route_top (table); rn; rn = route_next (rn))
-      for (newrib = rn->info; newrib; newrib = newrib->next)
-	if (CHECK_FLAG (newrib->flags, ZEBRA_FLAG_SELECTED)
-	    && newrib->type == type 
-	    && newrib->distance != DISTANCE_INFINITY
-	    && zebra_check_addr (&rn->p))
-	  zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, &rn->p, newrib);
+  redistribute_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0),
+		      ZEBRA_IPV6_ROUTE_ADD, client);
 #endif /* HAVE_IPV6 */
 }
 
@@ -177,23 +185,11 @@ redistribute_add (struct prefix *p, stru
       if (is_default (p))
         {
           if (client->redist_default || client->redist[rib->type])
-            {
-              if (p->family == AF_INET)
-                zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
-#ifdef HAVE_IPV6
-              if (p->family == AF_INET6)
-                zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
-#endif /* HAVE_IPV6 */	  
-	    }
+	    zsend_route_add (client, p, rib);
         }
       else if (client->redist[rib->type])
         {
-          if (p->family == AF_INET)
-            zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD, client, p, rib);
-#ifdef HAVE_IPV6
-          if (p->family == AF_INET6)
-            zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD, client, p, rib);
-#endif /* HAVE_IPV6 */	  
+	  zsend_route_add (client, p, rib);
         }
     }
 }
@@ -214,24 +210,12 @@ redistribute_delete (struct prefix *p, s
 	{
 	  if (client->redist_default || client->redist[rib->type])
 	    {
-	      if (p->family == AF_INET)
-		zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p,
-				       rib);
-#ifdef HAVE_IPV6
-	      if (p->family == AF_INET6)
-		zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p,
-				       rib);
-#endif /* HAVE_IPV6 */
+	      zsend_route_delete (client, p, rib);
 	    }
 	}
       else if (client->redist[rib->type])
 	{
-	  if (p->family == AF_INET)
-	    zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE, client, p, rib);
-#ifdef HAVE_IPV6
-	  if (p->family == AF_INET6)
-	    zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE, client, p, rib);
-#endif /* HAVE_IPV6 */
+	  zsend_route_delete (client, p, rib);
 	}
     }
 }
Index: zebra/ChangeLog
===================================================================
RCS file: /var/cvsroot/quagga/zebra/ChangeLog,v
retrieving revision 1.94
diff -u -p -w -b -r1.94 ChangeLog
--- zebra/ChangeLog	29 Jul 2005 14:36:00 -0000	1.94
+++ zebra/ChangeLog	20 Aug 2005 22:59:58 -0000
@@ -1,3 +1,8 @@
+2005-08-20 Hugo Santos <hsantos at av.it.pt>
+
+	* redistribute.c: remove v4/v6 code duplication by aggregating the
+	functionality in new functions with family/cmd arguments.
+
 2005-07-29 Paul Jakma <paul.jakma at sun.com>
 
 	* interface.c: (if_delete_update) should always be available, not


More information about the Quagga-dev mailing list