]> git.proxmox.com Git - mirror_frr.git/commitdiff
bgpd: optimize bgp_nexthop_self()
authorJorge Boncompte [DTI2] <jorge@dti2.net>
Mon, 7 May 2012 16:52:52 +0000 (16:52 +0000)
committerDavid Lamparter <equinox@opensourcerouting.org>
Tue, 22 May 2012 18:25:07 +0000 (20:25 +0200)
  This function scores 2nd, profiling a full internet table load. It's called
for every prefix received.
  Instead of looping in the interface lists comparing addresses use a hash
to mantain them.

* bgpd.c: Init the own address hash.
* bgp_nexthop.c: Introduce methods to maintain an own address hash.
  (bgp_connected_add) add addresses to the hash.
  (bgp_connected_delete) delete addresses from the hash.
  (bgp_nexthop_self) lookup addresses in the hash. Removed the unused afi_t
  parameter.
* bgp_route.c: (bgp_update_main) Micro-optimization, rearranged condition to
  not lookup the hash for bogus nexthops (0.0.0.0 or a class D/E address)

Signed-off-by: Jorge Boncompte [DTI2] <jorge@dti2.net>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
bgpd/bgp_nexthop.c
bgpd/bgp_nexthop.h
bgpd/bgp_route.c
bgpd/bgpd.c
lib/memtypes.c

index fdf251b2a872bcd79feff25c9295f1867a53d8d6..723057b68cf2408552c938c9e3ebdc18bd1e422a 100644 (file)
@@ -28,6 +28,8 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include "network.h"
 #include "log.h"
 #include "memory.h"
+#include "hash.h"
+#include "jhash.h"
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_table.h"
@@ -524,6 +526,83 @@ bgp_scan_timer (struct thread *t)
 
   return 0;
 }
+
+/* BGP own address structure */
+struct bgp_addr
+{
+  struct in_addr addr;
+  int refcnt;
+};
+
+static struct hash *bgp_address_hash;
+
+static void *
+bgp_address_hash_alloc (void *p)
+{
+  struct in_addr *val = p;
+  struct bgp_addr *addr;
+
+  addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
+  addr->refcnt = 0;
+  addr->addr.s_addr = val->s_addr;
+
+  return addr;
+}
+
+static unsigned int
+bgp_address_hash_key_make (void *p)
+{
+  const struct bgp_addr *addr = p;
+
+  return jhash_1word(addr->addr.s_addr, 0);
+}
+
+static int
+bgp_address_hash_cmp (const void *p1, const void *p2)
+{
+  const struct bgp_addr *addr1 = p1;
+  const struct bgp_addr *addr2 = p2;
+
+  return addr1->addr.s_addr == addr2->addr.s_addr;
+}
+
+void
+bgp_address_init (void)
+{
+  bgp_address_hash = hash_create (bgp_address_hash_key_make,
+                                  bgp_address_hash_cmp);
+}
+
+static void
+bgp_address_add (struct prefix *p)
+{
+  struct bgp_addr tmp;
+  struct bgp_addr *addr;
+
+  tmp.addr = p->u.prefix4;
+
+  addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
+  addr->refcnt++;
+}
+
+static void
+bgp_address_del (struct prefix *p)
+{
+  struct bgp_addr tmp;
+  struct bgp_addr *addr;
+
+  tmp.addr = p->u.prefix4;
+
+  addr = hash_lookup (bgp_address_hash, &tmp);
+  addr->refcnt--;
+
+  if (addr->refcnt == 0)
+    {
+      hash_release (bgp_address_hash, addr);
+      XFREE (MTYPE_BGP_ADDR, addr);
+    }
+}
+
 \f
 struct bgp_connected_ref
 {
@@ -557,6 +636,8 @@ bgp_connected_add (struct connected *ifc)
       if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
        return;
 
+      bgp_address_add (addr);
+
       rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
       if (rn->info)
        {
@@ -622,6 +703,8 @@ bgp_connected_delete (struct connected *ifc)
       if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
        return;
 
+      bgp_address_del (addr);
+
       rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
       if (! rn)
        return;
@@ -666,25 +749,16 @@ bgp_connected_delete (struct connected *ifc)
 }
 
 int
-bgp_nexthop_self (afi_t afi, struct attr *attr)
+bgp_nexthop_self (struct attr *attr)
 {
-  struct listnode *node;
-  struct listnode *node2;
-  struct interface *ifp;
-  struct connected *ifc;
-  struct prefix *p;
+  struct bgp_addr tmp, *addr;
 
-  for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
-    {
-      for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
-       {
-         p = ifc->address;
+  tmp.addr = attr->nexthop;
+
+  addr = hash_lookup (bgp_address_hash, &tmp);
+  if (addr)
+    return 1;
 
-         if (p && p->family == AF_INET 
-             && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
-           return 1;
-       }
-    }
   return 0;
 }
 \f
index 874f3bba0d96166ad097aaefca2a1588f1f0e52f..6e5350ead852bc73f3adcac9b669dab537a43ece 100644 (file)
@@ -55,6 +55,7 @@ extern void bgp_connected_delete (struct connected *c);
 extern int bgp_multiaccess_check_v4 (struct in_addr, char *);
 extern int bgp_config_write_scan_time (struct vty *);
 extern int bgp_nexthop_onlink (afi_t, struct attr *);
-extern int bgp_nexthop_self (afi_t, struct attr *);
+extern int bgp_nexthop_self (struct attr *);
+extern void bgp_address_init (void);
 
 #endif /* _QUAGGA_BGP_NEXTHOP_H */
index 589c0738a1a75e7bd43a58a6c3c5fb4dc12241ac..12cb693aa5bb81ed67577ee59cb78d04bb6ecaa8 100644 (file)
@@ -2147,9 +2147,9 @@ bgp_update_main (struct peer *peer, struct prefix *p, struct attr *attr,
 
       /* Next hop must not be 0.0.0.0 nor Class D/E address. Next hop
         must not be my own address.  */
-      if (bgp_nexthop_self (afi, &new_attr)
-         || new_attr.nexthop.s_addr == 0
-         || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr)))
+      if (new_attr.nexthop.s_addr == 0
+         || IPV4_CLASS_DE (ntohl (new_attr.nexthop.s_addr))
+         || bgp_nexthop_self (&new_attr))
        {
          reason = "martian next-hop;";
          goto filtered;
index ab27783fa84439dd6f41e3cb3bd46c0796de18b7..30fb091f4e4156a18fb8ea0610c24fa5ffef1e79 100644 (file)
@@ -5358,6 +5358,7 @@ bgp_init (void)
   bgp_dump_init ();
   bgp_route_init ();
   bgp_route_map_init ();
+  bgp_address_init ();
   bgp_scan_init ();
   bgp_mplsvpn_init ();
 
index acbd16b96ce13f083cead8a56819d805ceb050f4..1723490f63c8d0f2608e7a09a658c47ba2b787bd 100644 (file)
@@ -150,6 +150,7 @@ struct memory_list memory_list_bgp[] =
   { MTYPE_BGP_DAMP_ARRAY,      "BGP Dampening array"           },
   { MTYPE_BGP_REGEXP,          "BGP regexp"                    },
   { MTYPE_BGP_AGGREGATE,       "BGP aggregate"                 },
+  { MTYPE_BGP_ADDR,            "BGP own address"               },
   { -1, NULL }
 };