Browse Source

[bgpd] Trim memory usage of BGP routes

2007-05-03 Paul Jakma <paul.jakma@sun.com>

	* bgp_route.h: (struct info) Move less frequently used
	  fields to a lazily allocated struct info_extra.
	  Export bgp_info_extra_get
	* bgp_route.c: (bgp_info_extra_new) allocate extra
	  (bgp_info_extra_free) Free damp info and the info_extra.
	  (bgp_info_extra_get) Retrieve the info_extra of a struct
	  info, allocating as required.
	  (generally) adjust to use info->extra
	* bgp_damp.c: (generally) use bgp_info_extra_get to access
	  dampinfo
	* bgp_attr.h: Move rarely allocated attributes from struct attr
	  to a struct attr_extra, for a substantial saving in size of
	  struct attr.
	* bgp_attr.c: (bgp_attr_extra_{new,free}), new, self-explanatory.
	  (bgp_attr_extra_get) Get the attr_extra for a given struct
	  attr, allocating it if needs be.
	  (bgp_attr_dup) Shallow copy the struct attr and its attr_extra.
	  (generally) adjust to know about attr->extra.
	* bgp_debug.c: (bgp_dump_attr) ditto
	* bgp_vty.c: (show_bgp_memory) print attr and info extra sizes.
	* bgp_nexthop.c: (generally) adjust to know about attr->extra
	  and info->extra.
	* bgp_{packet,routemap,snmp,zebra}.c: ditto
	* lib/memtypes.c: Add MTYPE_ATTR_EXTRA and MTYPE_BGP_ROUTE_EXTRA
Paul Jakma 14 years ago
parent
commit
fb982c25aa
15 changed files with 706 additions and 378 deletions
  1. 26 1
      bgpd/ChangeLog
  2. 246 139
      bgpd/bgp_attr.c
  3. 42 17
      bgpd/bgp_attr.h
  4. 20 12
      bgpd/bgp_damp.c
  5. 21 18
      bgpd/bgp_debug.c
  6. 21 19
      bgpd/bgp_nexthop.c
  7. 12 8
      bgpd/bgp_packet.c
  8. 222 116
      bgpd/bgp_route.c
  9. 24 13
      bgpd/bgp_route.h
  10. 32 21
      bgpd/bgp_routemap.c
  11. 8 2
      bgpd/bgp_snmp.c
  12. 10 0
      bgpd/bgp_vty.c
  13. 15 11
      bgpd/bgp_zebra.c
  14. 4 0
      lib/ChangeLog
  15. 3 1
      lib/memtypes.c

+ 26 - 1
bgpd/ChangeLog

@@ -1,5 +1,30 @@
-2007-04-22 Sebastien Tandel <sebastien@tandel.be>
+2007-05-03 Paul Jakma <paul.jakma@sun.com>
+
+	* bgp_route.h: (struct info) Move less frequently used
+	  fields to a lazily allocated struct info_extra.
+	  Export bgp_info_extra_get
+	* bgp_route.c: (bgp_info_extra_new) allocate extra
+	  (bgp_info_extra_free) Free damp info and the info_extra.
+	  (bgp_info_extra_get) Retrieve the info_extra of a struct
+	  info, allocating as required.
+	  (generally) adjust to use info->extra
+	* bgp_damp.c: (generally) use bgp_info_extra_get to access
+	  dampinfo
+	* bgp_attr.h: Move rarely allocated attributes from struct attr
+	  to a struct attr_extra, for a substantial saving in size of
+	  struct attr.
+	* bgp_attr.c: (bgp_attr_extra_{new,free}), new, self-explanatory.
+	  (bgp_attr_extra_get) Get the attr_extra for a given struct
+	  attr, allocating it if needs be.
+	  (bgp_attr_dup) Shallow copy the struct attr and its attr_extra.
+	  (generally) adjust to know about attr->extra.
+	* bgp_debug.c: (bgp_dump_attr) ditto
+	* bgp_vty.c: (show_bgp_memory) print attr and info extra sizes.
+	* bgp_nexthop.c: (generally) adjust to know about attr->extra
+	  and info->extra.
+	* bgp_{packet,routemap,snmp,zebra}.c: ditto
 
+2007-04-22 Sebastien Tandel <sebastien@tandel.be>
 	* bgp_advertise.c : (baa_hash_alloc, baa_hash_key, baa_hash_cmp)
 	  conforms to quagga hash API. Defines _hash_[alloc|key|cmp] with
 	  void * arguments as defined by the API.

+ 246 - 139
bgpd/bgp_attr.c

@@ -282,9 +282,47 @@ transit_init ()
 }
 
 /* Attribute hash routines. */
-
 struct hash *attrhash;
 
+static struct attr_extra *
+bgp_attr_extra_new (void)
+{
+  return XCALLOC (MTYPE_ATTR_EXTRA, sizeof (struct attr_extra));
+}
+
+void
+bgp_attr_extra_free (struct attr *attr)
+{
+  if (attr->extra)
+    {
+      XFREE (MTYPE_ATTR_EXTRA, attr->extra);
+      attr->extra = NULL;
+    }
+}
+
+struct attr_extra *
+bgp_attr_extra_get (struct attr *attr)
+{
+  if (!attr->extra)
+    attr->extra = bgp_attr_extra_new();
+  return attr->extra;
+}
+
+/* Shallow copy of an attribute
+ * Though, not so shallow that it doesn't copy the contents
+ * of the attr_extra pointed to by 'extra'
+ */
+void
+bgp_attr_dup (struct attr *new, struct attr *orig)
+{
+  *new = *orig;
+  if (orig->extra)
+    {
+      new->extra = bgp_attr_extra_new();
+      *new->extra = *orig->extra;
+    }
+}
+
 unsigned long int
 attr_count (void)
 {
@@ -307,33 +345,41 @@ attrhash_key_make (void *p)
   key += attr->nexthop.s_addr;
   key += attr->med;
   key += attr->local_pref;
-  key += attr->aggregator_as;
-  key += attr->aggregator_addr.s_addr;
-  key += attr->weight;
-
-  key += attr->mp_nexthop_global_in.s_addr;
+  
+  if (attr->extra)
+    {
+      key += attr->extra->aggregator_as;
+      key += attr->extra->aggregator_addr.s_addr;
+      key += attr->extra->weight;
+      key += attr->extra->mp_nexthop_global_in.s_addr;
+    }
+  
   if (attr->aspath)
     key += aspath_key_make (attr->aspath);
   if (attr->community)
     key += community_hash_make (attr->community);
-  if (attr->ecommunity)
-    key += ecommunity_hash_make (attr->ecommunity);
-  if (attr->cluster)
-    key += cluster_hash_key_make (attr->cluster);
-  if (attr->transit)
-    key += transit_hash_key_make (attr->transit);
+  
+  if (attr->extra)
+    {
+      if (attr->extra->ecommunity)
+        key += ecommunity_hash_make (attr->extra->ecommunity);
+      if (attr->extra->cluster)
+        key += cluster_hash_key_make (attr->extra->cluster);
+      if (attr->extra->transit)
+        key += transit_hash_key_make (attr->extra->transit);
 
 #ifdef HAVE_IPV6
- {
-   int i;
-   
-   key += attr->mp_nexthop_len;
-   for (i = 0; i < 16; i++)
-     key += attr->mp_nexthop_global.s6_addr[i];
-   for (i = 0; i < 16; i++)
-     key += attr->mp_nexthop_local.s6_addr[i];
- }
+      {
+        int i;
+        
+        key += attr->extra->mp_nexthop_len;
+        for (i = 0; i < 16; i++)
+          key += attr->extra->mp_nexthop_global.s6_addr[i];
+        for (i = 0; i < 16; i++)
+          key += attr->extra->mp_nexthop_local.s6_addr[i];
+      }
 #endif /* HAVE_IPV6 */
+    }
 
   return key;
 }
@@ -347,23 +393,33 @@ attrhash_cmp (void *p1, void *p2)
   if (attr1->flag == attr2->flag
       && attr1->origin == attr2->origin
       && attr1->nexthop.s_addr == attr2->nexthop.s_addr
+      && attr1->aspath == attr2->aspath
+      && attr1->community == attr2->community
       && attr1->med == attr2->med
-      && attr1->local_pref == attr2->local_pref
-      && attr1->aggregator_as == attr2->aggregator_as
-      && attr1->aggregator_addr.s_addr == attr2->aggregator_addr.s_addr
-      && attr1->weight == attr2->weight
+      && attr1->local_pref == attr2->local_pref)
+    {
+      struct attr_extra *ae1 = attr1->extra;
+      struct attr_extra *ae2 = attr2->extra;
+      
+      if (ae1 && ae2
+          && ae1->aggregator_as == ae2->aggregator_as
+          && ae1->aggregator_addr.s_addr == ae2->aggregator_addr.s_addr
+          && ae1->weight == ae2->weight
 #ifdef HAVE_IPV6
-      && attr1->mp_nexthop_len == attr2->mp_nexthop_len
-      && IPV6_ADDR_SAME (&attr1->mp_nexthop_global, &attr2->mp_nexthop_global)
-      && IPV6_ADDR_SAME (&attr1->mp_nexthop_local, &attr2->mp_nexthop_local)
+          && ae1->mp_nexthop_len == ae2->mp_nexthop_len
+          && IPV6_ADDR_SAME (&ae1->mp_nexthop_global, &ae2->mp_nexthop_global)
+          && IPV6_ADDR_SAME (&ae1->mp_nexthop_local, &ae2->mp_nexthop_local)
 #endif /* HAVE_IPV6 */
-      && IPV4_ADDR_SAME (&attr1->mp_nexthop_global_in, &attr2->mp_nexthop_global_in)
-      && attr1->aspath == attr2->aspath
-      && attr1->community == attr2->community
-      && attr1->ecommunity == attr2->ecommunity
-      && attr1->cluster == attr2->cluster
-      && attr1->transit == attr2->transit)
-    return 1;
+          && IPV4_ADDR_SAME (&ae1->mp_nexthop_global_in, &ae2->mp_nexthop_global_in)
+          && ae1->ecommunity == ae2->ecommunity
+          && ae1->cluster == ae2->cluster
+          && ae1->transit == ae2->transit)
+        return 1;
+      else if (ae1 || ae2)
+        return 0;
+      /* neither attribute has extra attributes, so they're same */
+      return 1;
+    }
   else
     return 0;
 }
@@ -400,6 +456,11 @@ bgp_attr_hash_alloc (void *p)
 
   attr = XMALLOC (MTYPE_ATTR, sizeof (struct attr));
   *attr = *val;
+  if (val->extra)
+    {
+      attr->extra = bgp_attr_extra_new ();
+      *attr->extra = *val->extra;
+    }
   attr->refcnt = 0;
   return attr;
 }
@@ -425,26 +486,31 @@ bgp_attr_intern (struct attr *attr)
       else
 	attr->community->refcnt++;
     }
-  if (attr->ecommunity)
+  if (attr->extra)
     {
-      if (! attr->ecommunity->refcnt)
-	attr->ecommunity = ecommunity_intern (attr->ecommunity);
-      else
-	attr->ecommunity->refcnt++;
-    }
-  if (attr->cluster)
-    {
-      if (! attr->cluster->refcnt)
-	attr->cluster = cluster_intern (attr->cluster);
-      else
-	attr->cluster->refcnt++;
-    }
-  if (attr->transit)
-    {
-      if (! attr->transit->refcnt)
-	attr->transit = transit_intern (attr->transit);
-      else
-	attr->transit->refcnt++;
+      struct attr_extra *attre = attr->extra;
+      
+      if (attre->ecommunity)
+        {
+          if (! attre->ecommunity->refcnt)
+            attre->ecommunity = ecommunity_intern (attre->ecommunity);
+          else
+            attre->ecommunity->refcnt++;
+        }
+      if (attre->cluster)
+        {
+          if (! attre->cluster->refcnt)
+            attre->cluster = cluster_intern (attre->cluster);
+          else
+            attre->cluster->refcnt++;
+        }
+      if (attre->transit)
+        {
+          if (! attre->transit->refcnt)
+            attre->transit = transit_intern (attre->transit);
+          else
+            attre->transit->refcnt++;
+        }
     }
 
   find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc);
@@ -459,15 +525,16 @@ struct attr *
 bgp_attr_default_set (struct attr *attr, u_char origin)
 {
   memset (attr, 0, sizeof (struct attr));
-
+  bgp_attr_extra_get (attr);
+  
   attr->origin = origin;
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
   attr->aspath = aspath_empty ();
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
-  attr->weight = BGP_ATTR_DEFAULT_WEIGHT;
+  attr->extra->weight = BGP_ATTR_DEFAULT_WEIGHT;
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
 #ifdef HAVE_IPV6
-  attr->mp_nexthop_len = IPV6_MAX_BYTELEN;
+  attr->extra->mp_nexthop_len = IPV6_MAX_BYTELEN;
 #endif
 
   return attr;
@@ -480,10 +547,16 @@ bgp_attr_default_intern (u_char origin)
 {
   struct attr attr;
   struct attr *new;
-
+  struct attr_extra *attre;
+  
+  memset (&attr, 0, sizeof (struct attr));
+  attre = bgp_attr_extra_get (&attr);
+  
   bgp_attr_default_set(&attr, origin);
 
   new = bgp_attr_intern (&attr);
+  bgp_attr_extra_free (&attr);
+  
   aspath_unintern (new->aspath);
   return new;
 }
@@ -495,9 +568,11 @@ bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin,
 {
   struct attr attr;
   struct attr *new;
+  struct attr_extra *attre;
 
   memset (&attr, 0, sizeof (struct attr));
-
+  attre = bgp_attr_extra_get (&attr);
+  
   /* Origin attribute. */
   attr.origin = origin;
   attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
@@ -518,20 +593,22 @@ bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin,
       attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
     }
 
-  attr.weight = BGP_ATTR_DEFAULT_WEIGHT;
+  attre->weight = BGP_ATTR_DEFAULT_WEIGHT;
 #ifdef HAVE_IPV6
-  attr.mp_nexthop_len = IPV6_MAX_BYTELEN;
+  attre->mp_nexthop_len = IPV6_MAX_BYTELEN;
 #endif
   if (! as_set)
     attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
   attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
   if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION))
-    attr.aggregator_as = bgp->confed_id;
+    attre->aggregator_as = bgp->confed_id;
   else
-    attr.aggregator_as = bgp->as;
-  attr.aggregator_addr = bgp->router_id;
+    attre->aggregator_as = bgp->as;
+  attre->aggregator_addr = bgp->router_id;
 
   new = bgp_attr_intern (&attr);
+  bgp_attr_extra_free (&attr);
+  
   aspath_unintern (new->aspath);
   return new;
 }
@@ -543,23 +620,27 @@ bgp_attr_unintern (struct attr *attr)
   struct attr *ret;
   struct aspath *aspath;
   struct community *community;
-  struct ecommunity *ecommunity;
-  struct cluster_list *cluster;
-  struct transit *transit;
+  struct ecommunity *ecommunity = NULL;
+  struct cluster_list *cluster = NULL;
+  struct transit *transit = NULL;
 
   /* Decrement attribute reference. */
   attr->refcnt--;
   aspath = attr->aspath;
   community = attr->community;
-  ecommunity = attr->ecommunity;
-  cluster = attr->cluster;
-  transit = attr->transit;
+  if (attr->extra)
+    {
+      ecommunity = attr->extra->ecommunity;
+      cluster = attr->extra->cluster;
+      transit = attr->extra->transit;
+    }
 
   /* If reference becomes zero then free attribute object. */
   if (attr->refcnt == 0)
     {    
       ret = hash_release (attrhash, attr);
       assert (ret != NULL);
+      bgp_attr_extra_free (attr);
       XFREE (MTYPE_ATTR, attr);
     }
 
@@ -583,12 +664,16 @@ bgp_attr_flush (struct attr *attr)
     aspath_free (attr->aspath);
   if (attr->community && ! attr->community->refcnt)
     community_free (attr->community);
-  if (attr->ecommunity && ! attr->ecommunity->refcnt)
-    ecommunity_free (attr->ecommunity);
-  if (attr->cluster && ! attr->cluster->refcnt)
-    cluster_free (attr->cluster);
-  if (attr->transit && ! attr->transit->refcnt)
-    transit_free (attr->transit);
+  if (attr->extra)
+    {
+      struct attr_extra *attre = attr->extra;
+      if (attre->ecommunity && ! attre->ecommunity->refcnt)
+        ecommunity_free (attre->ecommunity);
+      if (attre->cluster && ! attre->cluster->refcnt)
+        cluster_free (attre->cluster);
+      if (attre->transit && ! attre->transit->refcnt)
+        transit_free (attre->transit);
+    }
 }
 
 /* Get origin attribute of the update message. */
@@ -851,6 +936,8 @@ static int
 bgp_attr_aggregator (struct peer *peer, bgp_size_t length,
 		     struct attr *attr, u_char flag)
 {
+  struct attr_extra *attre = bgp_attr_extra_get (attr);
+  
   if (length != 6)
     {
       zlog (peer->log, LOG_ERR, "Aggregator length is not 6 [%d]", length);
@@ -860,8 +947,8 @@ bgp_attr_aggregator (struct peer *peer, bgp_size_t length,
 		       BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
       return -1;
     }
-  attr->aggregator_as = stream_getw (peer->ibuf);
-  attr->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf);
+  attre->aggregator_as = stream_getw (peer->ibuf);
+  attre->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf);
 
   /* Set atomic aggregate flag. */
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
@@ -903,7 +990,8 @@ bgp_attr_originator_id (struct peer *peer, bgp_size_t length,
       return -1;
     }
 
-  attr->originator_id.s_addr = stream_get_ipv4 (peer->ibuf);
+  (bgp_attr_extra_get (attr))->originator_id.s_addr 
+    = stream_get_ipv4 (peer->ibuf);
 
   attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID);
 
@@ -926,8 +1014,8 @@ bgp_attr_cluster_list (struct peer *peer, bgp_size_t length,
       return -1;
     }
 
-  attr->cluster = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), 
-                                 length);
+  (bgp_attr_extra_get (attr))->cluster 
+    = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), length);
 
   stream_forward_getp (peer->ibuf, length);;
 
@@ -947,6 +1035,7 @@ bgp_mp_reach_parse (struct peer *peer, bgp_size_t length, struct attr *attr,
   size_t start;
   int ret;
   struct stream *s;
+  struct attr_extra *attre = bgp_attr_extra_get(attr);
   
   /* Set end of packet. */
   s = BGP_INPUT(peer);
@@ -962,16 +1051,16 @@ bgp_mp_reach_parse (struct peer *peer, bgp_size_t length, struct attr *attr,
   safi = stream_getc (s);
 
   /* Get nexthop length. */
-  attr->mp_nexthop_len = stream_getc (s);
+  attre->mp_nexthop_len = stream_getc (s);
   
-  if (STREAM_READABLE(s) < attr->mp_nexthop_len)
+  if (STREAM_READABLE(s) < attre->mp_nexthop_len)
     return -1;
   
   /* Nexthop length check. */
-  switch (attr->mp_nexthop_len)
+  switch (attre->mp_nexthop_len)
     {
     case 4:
-      stream_get (&attr->mp_nexthop_global_in, s, 4);
+      stream_get (&attre->mp_nexthop_global_in, s, 4);
       break;
     case 12:
       {
@@ -980,35 +1069,35 @@ bgp_mp_reach_parse (struct peer *peer, bgp_size_t length, struct attr *attr,
 
 	rd_high = stream_getl (s);
 	rd_low = stream_getl (s);
-	stream_get (&attr->mp_nexthop_global_in, s, 4);
+	stream_get (&attre->mp_nexthop_global_in, s, 4);
       }
       break;
 #ifdef HAVE_IPV6
     case 16:
-      stream_get (&attr->mp_nexthop_global, s, 16);
+      stream_get (&attre->mp_nexthop_global, s, 16);
       break;
     case 32:
-      stream_get (&attr->mp_nexthop_global, s, 16);
-      stream_get (&attr->mp_nexthop_local, s, 16);
-      if (! IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_local))
+      stream_get (&attre->mp_nexthop_global, s, 16);
+      stream_get (&attre->mp_nexthop_local, s, 16);
+      if (! IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local))
 	{
 	  char buf1[INET6_ADDRSTRLEN];
 	  char buf2[INET6_ADDRSTRLEN];
 
 	  if (BGP_DEBUG (update, UPDATE_IN))
 	    zlog_debug ("%s got two nexthop %s %s but second one is not a link-local nexthop", peer->host,
-		       inet_ntop (AF_INET6, &attr->mp_nexthop_global,
+		       inet_ntop (AF_INET6, &attre->mp_nexthop_global,
 				  buf1, INET6_ADDRSTRLEN),
-		       inet_ntop (AF_INET6, &attr->mp_nexthop_local,
+		       inet_ntop (AF_INET6, &attre->mp_nexthop_local,
 				  buf2, INET6_ADDRSTRLEN));
 
-	  attr->mp_nexthop_len = 16;
+	  attre->mp_nexthop_len = 16;
 	}
       break;
 #endif /* HAVE_IPV6 */
     default:
       zlog_info ("Wrong multiprotocol next hop length: %d", 
-		 attr->mp_nexthop_len);
+		 attre->mp_nexthop_len);
       return -1;
     }
 
@@ -1089,10 +1178,13 @@ bgp_attr_ext_communities (struct peer *peer, bgp_size_t length,
 			  struct attr *attr, u_char flag)
 {
   if (length == 0)
-    attr->ecommunity = NULL;
+    {
+      if (attr->extra)
+        attr->extra->ecommunity = NULL;
+    }
   else
     {
-      attr->ecommunity = 
+      (bgp_attr_extra_get (attr))->ecommunity = 
         ecommunity_parse ((u_int8_t *)stream_pnt (peer->ibuf), length);
       stream_forward_getp (peer->ibuf, length);
     }
@@ -1108,6 +1200,7 @@ bgp_attr_unknown (struct peer *peer, struct attr *attr, u_char flag,
 {
   bgp_size_t total;
   struct transit *transit;
+  struct attr_extra *attre;
 
   if (BGP_DEBUG (normal, NORMAL))
   zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
@@ -1149,13 +1242,13 @@ bgp_attr_unknown (struct peer *peer, struct attr *attr, u_char flag,
   SET_FLAG (*startp, BGP_ATTR_FLAG_PARTIAL);
 
   /* Store transitive attribute to the end of attr->transit. */
-  if (! attr->transit)
+  if (! ((attre = bgp_attr_extra_get(attr))->transit) )
     {
-      attr->transit = XMALLOC (MTYPE_TRANSIT, sizeof (struct transit));
-      memset (attr->transit, 0, sizeof (struct transit));
+      attre->transit = XMALLOC (MTYPE_TRANSIT, sizeof (struct transit));
+      memset (attre->transit, 0, sizeof (struct transit));
     }
 
-  transit = attr->transit;
+  transit = attre->transit;
 
   if (transit->val)
     transit->val = XREALLOC (MTYPE_TRANSIT_VAL, transit->val, 
@@ -1337,8 +1430,8 @@ bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
     }
 
   /* Finally intern unknown attribute. */
-  if (attr->transit)
-    attr->transit = transit_intern (attr->transit);
+  if (attr->extra && attr->extra->transit)
+    attr->extra->transit = transit_intern (attr->extra->transit);
 
   return 0;
 }
@@ -1500,11 +1593,12 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
   /* Aggregator. */
   if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
     {
+      assert (attr->extra);
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
       stream_putc (s, BGP_ATTR_AGGREGATOR);
       stream_putc (s, 6);
-      stream_putw (s, attr->aggregator_as);
-      stream_put_ipv4 (s, attr->aggregator_addr.s_addr);
+      stream_putw (s, attr->extra->aggregator_as);
+      stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
     }
 
   /* Community attribute. */
@@ -1531,13 +1625,15 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
       && from
       && peer_sort (from) == BGP_PEER_IBGP)
     {
+      assert (attr->extra);
+      
       /* Originator ID. */
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
       stream_putc (s, BGP_ATTR_ORIGINATOR_ID);
       stream_putc (s, 4);
 
       if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
-	stream_put_in_addr (s, &attr->originator_id);
+	stream_put_in_addr (s, &attr->extra->originator_id);
       else 
         stream_put_in_addr (s, &from->remote_id);
 
@@ -1545,15 +1641,16 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
       stream_putc (s, BGP_ATTR_CLUSTER_LIST);
       
-      if (attr->cluster)
+      if (attr->extra->cluster)
 	{
-	  stream_putc (s, attr->cluster->length + 4);
+	  stream_putc (s, attr->extra->cluster->length + 4);
 	  /* If this peer configuration's parent BGP has cluster_id. */
 	  if (bgp->config & BGP_CONFIG_CLUSTER_ID)
 	    stream_put_in_addr (s, &bgp->cluster_id);
 	  else
 	    stream_put_in_addr (s, &bgp->router_id);
-	  stream_put (s, attr->cluster->list, attr->cluster->length);
+	  stream_put (s, attr->extra->cluster->list, 
+	              attr->extra->cluster->length);
 	}
       else
 	{
@@ -1571,22 +1668,25 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
   if (p->family == AF_INET6)
     {
       unsigned long sizep;
-
+      struct attr_extra *attre = attr->extra;
+      
+      assert (attr->extra);
+      
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
       stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
       sizep = stream_get_endp (s);
-      stream_putc (s, 0);	/* Length of this attribute. */
+      stream_putc (s, 0);	/* Marker: Attribute length. */
       stream_putw (s, AFI_IP6);	/* AFI */
       stream_putc (s, safi);	/* SAFI */
 
-      stream_putc (s, attr->mp_nexthop_len);
+      stream_putc (s, attre->mp_nexthop_len);
 
-      if (attr->mp_nexthop_len == 16)
-	stream_put (s, &attr->mp_nexthop_global, 16);
-      else if (attr->mp_nexthop_len == 32)
+      if (attre->mp_nexthop_len == 16)
+	stream_put (s, &attre->mp_nexthop_global, 16);
+      else if (attre->mp_nexthop_len == 32)
 	{
-	  stream_put (s, &attr->mp_nexthop_global, 16);
-	  stream_put (s, &attr->mp_nexthop_local, 16);
+	  stream_put (s, &attre->mp_nexthop_global, 16);
+	  stream_put (s, &attre->mp_nexthop_local, 16);
 	}
       
       /* SNPA */
@@ -1607,7 +1707,7 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
       stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
       sizep = stream_get_endp (s);
-      stream_putc (s, 0);	/* Length of this attribute. */
+      stream_putc (s, 0);	/* Marker: Attribute Length. */
       stream_putw (s, AFI_IP);	/* AFI */
       stream_putc (s, SAFI_MULTICAST);	/* SAFI */
 
@@ -1638,7 +1738,7 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
       stream_putc (s, 12);
       stream_putl (s, 0);
       stream_putl (s, 0);
-      stream_put (s, &attr->mp_nexthop_global_in, 4);
+      stream_put (s, &attr->extra->mp_nexthop_global_in, 4);
 
       /* SNPA */
       stream_putc (s, 0);
@@ -1657,21 +1757,26 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
   if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY) 
       && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES)))
     {
-      if (peer_sort (peer) == BGP_PEER_IBGP || peer_sort (peer) == BGP_PEER_CONFED)
+      struct attr_extra *attre = attr->extra;
+      
+      assert (attre);
+      
+      if (peer_sort (peer) == BGP_PEER_IBGP 
+          || peer_sort (peer) == BGP_PEER_CONFED)
 	{
-	  if (attr->ecommunity->size * 8 > 255)
+	  if (attre->ecommunity->size * 8 > 255)
 	    {
 	      stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
 	      stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
-	      stream_putw (s, attr->ecommunity->size * 8);
+	      stream_putw (s, attre->ecommunity->size * 8);
 	    }
 	  else
 	    {
 	      stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
 	      stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
-	      stream_putc (s, attr->ecommunity->size * 8);
+	      stream_putc (s, attre->ecommunity->size * 8);
 	    }
-	  stream_put (s, attr->ecommunity->val, attr->ecommunity->size * 8);
+	  stream_put (s, attre->ecommunity->val, attre->ecommunity->size * 8);
 	}
       else
 	{
@@ -1680,9 +1785,9 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
 	  int ecom_tr_size = 0;
 	  int i;
 
-	  for (i = 0; i < attr->ecommunity->size; i++)
+	  for (i = 0; i < attre->ecommunity->size; i++)
 	    {
-	      pnt = attr->ecommunity->val + (i * 8);
+	      pnt = attre->ecommunity->val + (i * 8);
 	      tbit = *pnt;
 
 	      if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
@@ -1706,9 +1811,9 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
 		  stream_putc (s, ecom_tr_size * 8);
 		}
 
-	      for (i = 0; i < attr->ecommunity->size; i++)
+	      for (i = 0; i < attre->ecommunity->size; i++)
 		{
-		  pnt = attr->ecommunity->val + (i * 8);
+		  pnt = attre->ecommunity->val + (i * 8);
 		  tbit = *pnt;
 
 		  if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
@@ -1721,8 +1826,8 @@ bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
     }
 
   /* Unknown transit attribute. */
-  if (attr->transit)
-    stream_put (s, attr->transit->val, attr->transit->length);
+  if (attr->extra && attr->extra->transit)
+    stream_put (s, attr->extra->transit->val, attr->extra->transit->length);
 
   /* Return total size of attribute. */
   return stream_get_endp (s) - cp;
@@ -1869,11 +1974,12 @@ bgp_dump_routes_attr (struct stream *s, struct attr *attr,
   /* Aggregator. */
   if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
     {
+      assert (attr->extra);
       stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
       stream_putc (s, BGP_ATTR_AGGREGATOR);
       stream_putc (s, 6);
-      stream_putw (s, attr->aggregator_as);
-      stream_put_ipv4 (s, attr->aggregator_addr.s_addr);
+      stream_putw (s, attr->extra->aggregator_as);
+      stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
     }
 
   /* Community attribute. */
@@ -1896,25 +2002,26 @@ bgp_dump_routes_attr (struct stream *s, struct attr *attr,
 
 #ifdef HAVE_IPV6
   /* Add a MP_NLRI attribute to dump the IPv6 next hop */
-  if(prefix != NULL && prefix->family == AF_INET6 && 
-     (attr->mp_nexthop_len == 16 || attr->mp_nexthop_len == 32) )
+  if (prefix != NULL && prefix->family == AF_INET6 && attr->extra &&
+     (attr->extra->mp_nexthop_len == 16 || attr->extra->mp_nexthop_len == 32) )
     {
       int sizep;
-
+      struct attr_extra *attre = attr->extra;
+      
       stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
       stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
       sizep = stream_get_endp (s);
 
       /* MP header */
-      stream_putc (s, 0);		/* Length of this attribute. */
+      stream_putc (s, 0);		/* Marker: Attribute length. */
       stream_putw(s, AFI_IP6);		/* AFI */
       stream_putc(s, SAFI_UNICAST);	/* SAFI */
 
       /* Next hop */
-      stream_putc(s, attr->mp_nexthop_len);
-      stream_put(s, &attr->mp_nexthop_global, 16);
-      if(attr->mp_nexthop_len == 32)
-        stream_put(s, &attr->mp_nexthop_local, 16);
+      stream_putc(s, attre->mp_nexthop_len);
+      stream_put(s, &attre->mp_nexthop_global, 16);
+      if (attre->mp_nexthop_len == 32)
+        stream_put(s, &attre->mp_nexthop_local, 16);
 
       /* SNPA */
       stream_putc(s, 0);

+ 42 - 17
bgpd/bgp_attr.h

@@ -45,24 +45,20 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
 /* BGP attribute header must bigger than 2. */
 #define BGP_ATTR_MIN_LEN        2       /* Attribute flag and type. */
-
 #define BGP_ATTR_DEFAULT_WEIGHT 32768
 
-/* BGP attribute structure. */
-struct attr
+/* Additional/uncommon BGP attributes.
+ * lazily allocated as and when a struct attr
+ * requires it.
+ */
+struct attr_extra
 {
-  /* Attributes. */
+  /* Multi-Protocol Nexthop, AFI IPv6 */
 #ifdef HAVE_IPV6
   struct in6_addr mp_nexthop_global;
   struct in6_addr mp_nexthop_local;
 #endif /* HAVE_IPV6 */
 
-  /* AS Path structure */
-  struct aspath *aspath;
-
-  /* Community structure */
-  struct community *community;	
-
   /* Extended Communities attribute. */
   struct ecommunity *ecommunity;
   
@@ -72,6 +68,37 @@ struct attr
   /* Unknown transitive attribute. */
   struct transit *transit;
 
+  struct in_addr mp_nexthop_global_in;
+  struct in_addr mp_nexthop_local_in;
+  
+  /* Aggregator Router ID attribute */
+  struct in_addr aggregator_addr;
+  
+  /* Route Reflector Originator attribute */
+  struct in_addr originator_id;
+  
+  /* Local weight, not actually an attribute */
+  u_int32_t weight;
+  
+  /* Aggregator ASN */
+  as_t aggregator_as;
+  
+  /* MP Nexthop length */
+  u_char mp_nexthop_len;
+};
+
+/* BGP core attribute structure. */
+struct attr
+{
+  /* AS Path structure */
+  struct aspath *aspath;
+
+  /* Community structure */
+  struct community *community;	
+  
+  /* Lazily allocated pointer to extra attributes */
+  struct attr_extra *extra;
+  
   /* Reference count of this attribute. */
   unsigned long refcnt;
 
@@ -82,14 +109,9 @@ struct attr
   struct in_addr nexthop;
   u_int32_t med;
   u_int32_t local_pref;
-  struct in_addr aggregator_addr;
-  struct in_addr originator_id;
-  struct in_addr mp_nexthop_global_in;
-  struct in_addr mp_nexthop_local_in;
-  u_int32_t weight;
-  as_t aggregator_as;
+
+  /* Path origin attribute */
   u_char origin;
-  u_char mp_nexthop_len;
 };
 
 /* Router Reflector related structure. */
@@ -115,6 +137,9 @@ extern void bgp_attr_init (void);
 extern int bgp_attr_parse (struct peer *, struct attr *, bgp_size_t,
 		    struct bgp_nlri *, struct bgp_nlri *);
 extern int bgp_attr_check (struct peer *, struct attr *);
+extern struct attr_extra *bgp_attr_extra_get (struct attr *);
+extern void bgp_attr_extra_free (struct attr *);
+extern void bgp_attr_dup (struct attr *, struct attr *);
 extern struct attr *bgp_attr_intern (struct attr *attr);
 extern void bgp_attr_unintern (struct attr *);
 extern void bgp_attr_flush (struct attr *);

+ 20 - 12
bgpd/bgp_damp.c

@@ -178,14 +178,15 @@ bgp_damp_withdraw (struct bgp_info *binfo, struct bgp_node *rn,
 		   afi_t afi, safi_t safi, int attr_change)
 {
   time_t t_now;
-  struct bgp_damp_info *bdi;
+  struct bgp_damp_info *bdi = NULL;
   double last_penalty = 0;
   
   t_now = time (NULL);
 
   /* Processing Unreachable Messages.  */
-  bdi = binfo->damp_info;
-
+  if (binfo->extra)
+    bdi = binfo->extra->damp_info;
+  
   if (bdi == NULL)
     {
       /* If there is no previous stability history. */
@@ -205,7 +206,7 @@ bgp_damp_withdraw (struct bgp_info *binfo, struct bgp_node *rn,
       bdi->index = -1;
       bdi->afi = afi;
       bdi->safi = safi;
-      binfo->damp_info = bdi;
+      (bgp_info_extra_get (binfo))->damp_info = bdi;
       BGP_DAMP_LIST_ADD (damp, bdi);
     }
   else
@@ -264,8 +265,7 @@ bgp_damp_update (struct bgp_info *binfo, struct bgp_node *rn,
   struct bgp_damp_info *bdi;
   int status;
 
-  bdi = binfo->damp_info;
-  if (! bdi)
+  if (!binfo->extra || !((bdi = binfo->extra->damp_info)))
     return BGP_DAMP_USED;
 
   t_now = time (NULL);
@@ -303,9 +303,11 @@ bgp_damp_scan (struct bgp_info *binfo, afi_t afi, safi_t safi)
 {
   time_t t_now, t_diff;
   struct bgp_damp_info *bdi;
-
+  
+  assert (binfo->extra && binfo->extra->damp_info);
+  
   t_now = time (NULL);
-  bdi = binfo->damp_info;
+  bdi = binfo->extra->damp_info;
  
   if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED))
     {
@@ -353,7 +355,7 @@ bgp_damp_info_free (struct bgp_damp_info *bdi, int withdraw)
     return;
 
   binfo = bdi->binfo;
-  binfo->damp_info = NULL;
+  binfo->extra->damp_info = NULL;
 
   if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED))
     bgp_reuse_list_delete (bdi);
@@ -590,8 +592,11 @@ bgp_damp_info_vty (struct vty *vty, struct bgp_info *binfo)
   char timebuf[BGP_UPTIME_LEN];
   int penalty;
 
+  if (!binfo->extra)
+    return;
+  
   /* BGP dampening information.  */
-  bdi = binfo->damp_info;
+  bdi = binfo->extra->damp_info;
 
   /* If dampening is not enabled or there is no dampening information,
      return immediately.  */
@@ -622,9 +627,12 @@ bgp_damp_reuse_time_vty (struct vty *vty, struct bgp_info *binfo)
   time_t t_now, t_diff;
   char timebuf[BGP_UPTIME_LEN];
   int penalty;
-
+  
+  if (!binfo->extra)
+    return NULL;
+  
   /* BGP dampening information.  */
-  bdi = binfo->damp_info;
+  bdi = binfo->extra->damp_info;
 
   /* If dampening is not enabled or there is no dampening information,
      return immediately.  */

+ 21 - 18
bgpd/bgp_debug.c

@@ -176,20 +176,22 @@ bgp_dump_attr (struct peer *peer, struct attr *attr, char *buf, size_t size)
 	      bgp_origin_str[attr->origin]);
 
 #ifdef HAVE_IPV6
-  {
-    char addrbuf[BUFSIZ];
-
-    /* Add MP case. */
-    if (attr->mp_nexthop_len == 16 || attr->mp_nexthop_len == 32)
-      snprintf (buf + strlen (buf), size - strlen (buf), ", mp_nexthop %s",
-		inet_ntop (AF_INET6, &attr->mp_nexthop_global, 
-			   addrbuf, BUFSIZ));
-
-    if (attr->mp_nexthop_len == 32)
-      snprintf (buf + strlen (buf), size - strlen (buf), "(%s)",
-		inet_ntop (AF_INET6, &attr->mp_nexthop_local, 
-			   addrbuf, BUFSIZ));
-  }
+  if (attr->extra)
+    {
+      char addrbuf[BUFSIZ];
+
+      /* Add MP case. */
+      if (attr->extra->mp_nexthop_len == 16 
+          || attr->extra->mp_nexthop_len == 32)
+        snprintf (buf + strlen (buf), size - strlen (buf), ", mp_nexthop %s",
+                  inet_ntop (AF_INET6, &attr->extra->mp_nexthop_global, 
+                             addrbuf, BUFSIZ));
+
+      if (attr->extra->mp_nexthop_len == 32)
+        snprintf (buf + strlen (buf), size - strlen (buf), "(%s)",
+                  inet_ntop (AF_INET6, &attr->extra->mp_nexthop_local, 
+                             addrbuf, BUFSIZ));
+    }
 #endif /* HAVE_IPV6 */
 
   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)))
@@ -209,20 +211,21 @@ bgp_dump_attr (struct peer *peer, struct attr *attr, char *buf, size_t size)
 
   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR)))
     snprintf (buf + strlen (buf), size - strlen (buf), ", aggregated by %d %s",
-	      attr->aggregator_as, inet_ntoa (attr->aggregator_addr));
+	      attr->extra->aggregator_as,
+	      inet_ntoa (attr->extra->aggregator_addr));
 
   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID)))
     snprintf (buf + strlen (buf), size - strlen (buf), ", originator %s",
-	      inet_ntoa (attr->originator_id));
+	      inet_ntoa (attr->extra->originator_id));
 
   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST)))
     {
       int i;
 
       snprintf (buf + strlen (buf), size - strlen (buf), ", clusterlist");
-      for (i = 0; i < attr->cluster->length / 4; i++)
+      for (i = 0; i < attr->extra->cluster->length / 4; i++)
 	snprintf (buf + strlen (buf), size - strlen (buf), " %s",
-		  inet_ntoa (attr->cluster->list[i]));
+		  inet_ntoa (attr->extra->cluster->list[i]));
     }
 
   if (CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH))) 

+ 21 - 19
bgpd/bgp_nexthop.c

@@ -196,15 +196,15 @@ bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr)
 #ifdef HAVE_IPV6
   else if (afi == AFI_IP6)
     {
-      if (attr->mp_nexthop_len == 32)
+      if (attr->extra->mp_nexthop_len == 32)
 	return 1;
-      else if (attr->mp_nexthop_len == 16)
+      else if (attr->extra->mp_nexthop_len == 16)
 	{
-	  if (IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
+	  if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
 	    return 1;
 
 	  rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
-				      &attr->mp_nexthop_global);
+				      &attr->extra->mp_nexthop_global);
 	  if (rn)
 	    {
 	      bgp_unlock_node (rn);
@@ -230,21 +230,22 @@ bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
   /* If lookup is not enabled, return valid. */
   if (zlookup->sock < 0)
     {
-      ri->igpmetric = 0;
+      if (ri->extra)
+        ri->extra->igpmetric = 0;
       return 1;
     }
 
   /* Only check IPv6 global address only nexthop. */
   attr = ri->attr;
 
-  if (attr->mp_nexthop_len != 16 
-      || IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
+  if (attr->extra->mp_nexthop_len != 16 
+      || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
     return 1;
 
   memset (&p, 0, sizeof (struct prefix));
   p.family = AF_INET6;
   p.prefixlen = IPV6_MAX_BITLEN;
-  p.u.prefix6 = attr->mp_nexthop_global;
+  p.u.prefix6 = attr->extra->mp_nexthop_global;
 
   /* IBGP or ebgp-multihop */
   rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
@@ -256,7 +257,7 @@ bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
     }
   else
     {
-      bnc = zlookup_query_ipv6 (&attr->mp_nexthop_global);
+      bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global);
       if (bnc)
 	{
 	  struct bgp_table *old;
@@ -296,10 +297,10 @@ bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
   if (metricchanged)
     *metricchanged = bnc->metricchanged;
 
-  if (bnc->valid)
-    ri->igpmetric = bnc->metric;
-  else
-    ri->igpmetric = 0;
+  if (bnc->valid && bnc->metric)
+    (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
+  else if (ri->extra)
+    ri->extra->igpmetric = 0;
 
   return bnc->valid;
 }
@@ -318,7 +319,8 @@ bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
   /* If lookup is not enabled, return valid. */
   if (zlookup->sock < 0)
     {
-      ri->igpmetric = 0;
+      if (ri->extra)
+        ri->extra->igpmetric = 0;
       return 1;
     }
 
@@ -384,10 +386,10 @@ bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
   if (metricchanged)
     *metricchanged = bnc->metricchanged;
 
-  if (bnc->valid)
-    ri->igpmetric = bnc->metric;
-  else
-    ri->igpmetric = 0;
+  if (bnc->valid && bnc->metric)
+    (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
+  else if (ri->extra)
+    ri->extra->igpmetric = 0;
 
   return bnc->valid;
 }
@@ -490,7 +492,7 @@ bgp_scan (afi_t afi, safi_t safi)
 
               if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
 		  BGP_CONFIG_DAMPENING)
-                  &&  bi->damp_info )
+                  &&  bi->extra && bi->extra->damp_info )
                 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
 		  bgp_aggregate_increment (bgp, &rn->p, bi,
 					   afi, SAFI_UNICAST);

+ 12 - 8
bgpd/bgp_packet.c

@@ -177,9 +177,9 @@ bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi)
 	  
 	  if (rn->prn)
 	    prd = (struct prefix_rd *) &rn->prn->p;
-          if (binfo)
+          if (binfo && binfo->extra)
             {
-              tag = binfo->tag;
+              tag = binfo->extra->tag;
               from = binfo->peer;
             }
           
@@ -1706,12 +1706,16 @@ bgp_update_receive (struct peer *peer, bgp_size_t size)
     aspath_unintern (attr.aspath);
   if (attr.community)
     community_unintern (attr.community);
-  if (attr.ecommunity)
-    ecommunity_unintern (attr.ecommunity);
-  if (attr.cluster)
-    cluster_unintern (attr.cluster);
-  if (attr.transit)
-    transit_unintern (attr.transit);
+  if (attr.extra)
+    {
+      if (attr.extra->ecommunity)
+        ecommunity_unintern (attr.extra->ecommunity);
+      if (attr.extra->cluster)
+        cluster_unintern (attr.extra->cluster);
+      if (attr.extra->transit)
+        transit_unintern (attr.extra->transit);
+      bgp_attr_extra_free (&attr);
+    }
 
   /* If peering is stopped due to some reason, do not generate BGP
      event.  */

File diff suppressed because it is too large
+ 222 - 116
bgpd/bgp_route.c


+ 24 - 13
bgpd/bgp_route.h

@@ -23,6 +23,25 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
 #include "bgp_table.h"
 
+/* Ancillary information to struct bgp_info, 
+ * used for uncommonly used data (aggregation, MPLS, etc.)
+ * and lazily allocated to save memory.
+ */
+struct bgp_info_extra
+{
+  /* Pointer to dampening structure.  */
+  struct bgp_damp_info *damp_info;
+
+  /* This route is suppressed with aggregation.  */
+  int suppress;
+
+  /* Nexthop reachability check.  */
+  u_int32_t igpmetric;
+
+  /* MPLS label.  */
+  u_char tag[3];  
+};
+
 struct bgp_info
 {
   /* For linked list. */
@@ -34,19 +53,13 @@ struct bgp_info
 
   /* Attribute structure.  */
   struct attr *attr;
-
-  /* Pointer to dampening structure.  */
-  struct bgp_damp_info *damp_info;
-
+  
+  /* Extra information */
+  struct bgp_info_extra *extra;
+  
   /* Uptime.  */
   time_t uptime;
 
-  /* This route is suppressed with aggregation.  */
-  int suppress;
-  
-  /* Nexthop reachability check.  */
-  u_int32_t igpmetric;
-
   /* reference count */
   unsigned int lock;
   
@@ -64,9 +77,6 @@ struct bgp_info
 #define BGP_INFO_REMOVED        (1 << 9)
 #define BGP_INFO_COUNTED	(1 << 10)
 
-  /* MPLS label.  */
-  u_char tag[3];
-
   /* BGP route type.  This can be static, RIP, OSPF, BGP etc.  */
   u_char type;
 
@@ -162,6 +172,7 @@ extern struct bgp_info *bgp_info_lock (struct bgp_info *);
 extern struct bgp_info *bgp_info_unlock (struct bgp_info *);
 extern void bgp_info_add (struct bgp_node *rn, struct bgp_info *ri);
 extern void bgp_info_delete (struct bgp_node *rn, struct bgp_info *ri);
+extern struct bgp_info_extra *bgp_info_extra_get (struct bgp_info *);
 extern void bgp_info_set_flag (struct bgp_node *, struct bgp_info *, u_int32_t);
 extern void bgp_info_unset_flag (struct bgp_node *, struct bgp_info *, u_int32_t);
 

+ 32 - 21
bgpd/bgp_routemap.c

@@ -692,13 +692,16 @@ route_match_ecommunity (void *rule, struct prefix *prefix,
   if (type == RMAP_BGP) 
     {
       bgp_info = object;
-
+      
+      if (!bgp_info->attr->extra)
+        return RMAP_NOMATCH;
+      
       list = community_list_lookup (bgp_clist, (char *) rule,
 				    EXTCOMMUNITY_LIST_MASTER);
       if (! list)
 	return RMAP_NOMATCH;
 
-      if (ecommunity_list_match (bgp_info->attr->ecommunity, list))
+      if (ecommunity_list_match (bgp_info->attr->extra->ecommunity, list))
 	return RMAP_MATCH;
     }
   return RMAP_NOMATCH;
@@ -973,7 +976,10 @@ route_set_weight (void *rule, struct prefix *prefix, route_map_object_t type,
       bgp_info = object;
     
       /* Set weight value. */ 
-      bgp_info->attr->weight = *weight;
+      if (*weight)
+        (bgp_attr_extra_get (bgp_info->attr))->weight = *weight;
+      else if (bgp_info->attr->extra)
+        bgp_info->attr->extra->weight = 0;
     }
 
   return RMAP_OKAY;
@@ -1402,14 +1408,14 @@ route_set_ecommunity_rt (void *rule, struct prefix *prefix,
 	return RMAP_OKAY;
     
       /* We assume additive for Extended Community. */
-      old_ecom = bgp_info->attr->ecommunity;
+      old_ecom = (bgp_attr_extra_get (bgp_info->attr))->ecommunity;
 
       if (old_ecom)
 	new_ecom = ecommunity_merge (ecommunity_dup (old_ecom), ecom);
       else
 	new_ecom = ecommunity_dup (ecom);
 
-      bgp_info->attr->ecommunity = new_ecom;
+      bgp_info->attr->extra->ecommunity = new_ecom;
 
       if (old_ecom)
 	ecommunity_free (old_ecom);
@@ -1467,7 +1473,7 @@ route_set_ecommunity_soo (void *rule, struct prefix *prefix,
 	return RMAP_OKAY;
     
       bgp_info->attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
-      bgp_info->attr->ecommunity = ecommunity_dup (ecom);
+      (bgp_attr_extra_get (bgp_info->attr))->ecommunity = ecommunity_dup (ecom);
     }
   return RMAP_OKAY;
 }
@@ -1610,14 +1616,16 @@ route_set_aggregator_as (void *rule, struct prefix *prefix,
 {
   struct bgp_info *bgp_info;
   struct aggregator *aggregator;
+  struct attr_extra *ae;
 
   if (type == RMAP_BGP)
     {
       bgp_info = object;
       aggregator = rule;
-    
-      bgp_info->attr->aggregator_as = aggregator->as;
-      bgp_info->attr->aggregator_addr = aggregator->address;
+      ae = bgp_attr_extra_get (bgp_info->attr);
+      
+      ae->aggregator_as = aggregator->as;
+      ae->aggregator_addr = aggregator->address;
       bgp_info->attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
     }
 
@@ -1711,12 +1719,15 @@ route_match_ipv6_next_hop (void *rule, struct prefix *prefix,
     {
       addr = rule;
       bgp_info = object;
-    
-      if (IPV6_ADDR_SAME (&bgp_info->attr->mp_nexthop_global, rule))
+      
+      if (!bgp_info->attr->extra)
+        return RMAP_NOMATCH;
+      
+      if (IPV6_ADDR_SAME (&bgp_info->attr->extra->mp_nexthop_global, rule))
 	return RMAP_MATCH;
 
-      if (bgp_info->attr->mp_nexthop_len == 32 &&
-	  IPV6_ADDR_SAME (&bgp_info->attr->mp_nexthop_local, rule))
+      if (bgp_info->attr->extra->mp_nexthop_len == 32 &&
+	  IPV6_ADDR_SAME (&bgp_info->attr->extra->mp_nexthop_local, rule))
 	return RMAP_MATCH;
 
       return RMAP_NOMATCH;
@@ -1814,11 +1825,11 @@ route_set_ipv6_nexthop_global (void *rule, struct prefix *prefix,
       bgp_info = object;
     
       /* Set next hop value. */ 
-      bgp_info->attr->mp_nexthop_global = *address;
+      (bgp_attr_extra_get (bgp_info->attr))->mp_nexthop_global = *address;
     
       /* Set nexthop length. */
-      if (bgp_info->attr->mp_nexthop_len == 0)
-	bgp_info->attr->mp_nexthop_len = 16;
+      if (bgp_info->attr->extra->mp_nexthop_len == 0)
+	bgp_info->attr->extra->mp_nexthop_len = 16;
     }
 
   return RMAP_OKAY;
@@ -1878,11 +1889,11 @@ route_set_ipv6_nexthop_local (void *rule, struct prefix *prefix,
       bgp_info = object;
     
       /* Set next hop value. */ 
-      bgp_info->attr->mp_nexthop_local = *address;
+      (bgp_attr_extra_get (bgp_info->attr))->mp_nexthop_local = *address;
     
       /* Set nexthop length. */
-      if (bgp_info->attr->mp_nexthop_len != 32)
-	bgp_info->attr->mp_nexthop_len = 32;
+      if (bgp_info->attr->extra->mp_nexthop_len != 32)
+	bgp_info->attr->extra->mp_nexthop_len = 32;
     }
 
   return RMAP_OKAY;
@@ -1942,7 +1953,7 @@ route_set_vpnv4_nexthop (void *rule, struct prefix *prefix,
       bgp_info = object;
     
       /* Set next hop value. */ 
-      bgp_info->attr->mp_nexthop_global_in = *address;
+      (bgp_attr_extra_get (bgp_info->attr))->mp_nexthop_global_in = *address;
     }
 
   return RMAP_OKAY;
@@ -1997,7 +2008,7 @@ route_set_originator_id (void *rule, struct prefix *prefix, route_map_object_t t
       bgp_info = object;
     
       bgp_info->attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID);
-      bgp_info->attr->originator_id = *address;
+      (bgp_attr_extra_get (bgp_info->attr))->originator_id = *address;
     }
 
   return RMAP_OKAY;

+ 8 - 2
bgpd/bgp_snmp.c

@@ -797,10 +797,16 @@ bgp4PathAttrTable (struct variable *v, oid name[], size_t *length,
       return SNMP_INTEGER (1);
       break;
     case BGP4PATHATTRAGGREGATORAS: /* 10 */
-      return SNMP_INTEGER (binfo->attr->aggregator_as);
+      if (binfo->attr->extra)
+        return SNMP_INTEGER (binfo->attr->extra->aggregator_as);
+      else
+        return SNMP_INTEGER (0);
       break;
     case BGP4PATHATTRAGGREGATORADDR: /* 11 */
-      return SNMP_IPADDRESS (binfo->attr->aggregator_addr);
+      if (binfo->attr->extra)
+        return SNMP_IPADDRESS (binfo->attr->extra->aggregator_addr);
+      else
+        return SNMP_INTEGER (0);
       break;
     case BGP4PATHATTRCALCLOCALPREF: /* 12 */
       return SNMP_INTEGER (-1);

+ 10 - 0
bgpd/bgp_vty.c

@@ -6496,6 +6496,11 @@ DEFUN (show_bgp_memory,
            mtype_memstr (memstrbuf, sizeof (memstrbuf),
                          count * sizeof (struct bgp_info)),
            VTY_NEWLINE);
+  if ((count = mtype_stats_alloc (MTYPE_BGP_ROUTE_EXTRA)))
+    vty_out (vty, "%ld BGP route ancillaries, using %s of memory%s", count,
+             mtype_memstr (memstrbuf, sizeof (memstrbuf),
+                           count * sizeof (struct bgp_info_extra)),
+             VTY_NEWLINE);
   
   if ((count = mtype_stats_alloc (MTYPE_BGP_STATIC)))
     vty_out (vty, "%ld Static routes, using %s of memory%s", count,
@@ -6533,6 +6538,11 @@ DEFUN (show_bgp_memory,
            mtype_memstr (memstrbuf, sizeof (memstrbuf), 
                          count * sizeof(struct attr)), 
            VTY_NEWLINE);
+  if ((count = mtype_stats_alloc (MTYPE_ATTR_EXTRA)))
+    vty_out (vty, "%ld BGP extra attributes, using %s of memory%s", count, 
+             mtype_memstr (memstrbuf, sizeof (memstrbuf), 
+                           count * sizeof(struct attr_extra)), 
+             VTY_NEWLINE);
   
   if ((count = attr_unknown_count()))
     vty_out (vty, "%ld unknown attributes%s", count, VTY_NEWLINE);

+ 15 - 11
bgpd/bgp_zebra.c

@@ -746,20 +746,22 @@ bgp_zebra_announce (struct prefix *p, struct bgp_info *info, struct bgp *bgp)
 
       ifindex = 0;
       nexthop = NULL;
-
+      
+      assert (info->attr->extra);
+      
       /* Only global address nexthop exists. */
-      if (info->attr->mp_nexthop_len == 16)
-	nexthop = &info->attr->mp_nexthop_global;
+      if (info->attr->extra->mp_nexthop_len == 16)
+	nexthop = &info->attr->extra->mp_nexthop_global;
       
       /* If both global and link-local address present. */
-      if (info->attr->mp_nexthop_len == 32)
+      if (info->attr->extra->mp_nexthop_len == 32)
 	{
 	  /* Workaround for Cisco's nexthop bug.  */
-	  if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->mp_nexthop_global)
+	  if (IN6_IS_ADDR_UNSPECIFIED (&info->attr->extra->mp_nexthop_global)
 	      && peer->su_remote->sa.sa_family == AF_INET6)
 	    nexthop = &peer->su_remote->sin6.sin6_addr;
 	  else
-	    nexthop = &info->attr->mp_nexthop_local;
+	    nexthop = &info->attr->extra->mp_nexthop_local;
 
 	  if (info->peer->nexthop.ifp)
 	    ifindex = info->peer->nexthop.ifp->ifindex;
@@ -867,18 +869,20 @@ bgp_zebra_withdraw (struct prefix *p, struct bgp_info *info)
       struct zapi_ipv6 api;
       unsigned int ifindex;
       struct in6_addr *nexthop;
-
+      
+      assert (info->attr->extra);
+      
       ifindex = 0;
       nexthop = NULL;
 
       /* Only global address nexthop exists. */
-      if (info->attr->mp_nexthop_len == 16)
-	nexthop = &info->attr->mp_nexthop_global;
+      if (info->attr->extra->mp_nexthop_len == 16)
+	nexthop = &info->attr->extra->mp_nexthop_global;
 
       /* If both global and link-local address present. */
-      if (info->attr->mp_nexthop_len == 32)
+      if (info->attr->extra->mp_nexthop_len == 32)
 	{
-	  nexthop = &info->attr->mp_nexthop_local;
+	  nexthop = &info->attr->extra->mp_nexthop_local;
 	  if (info->peer->nexthop.ifp)
 	    ifindex = info->peer->nexthop.ifp->ifindex;
 	}

+ 4 - 0
lib/ChangeLog

@@ -1,3 +1,7 @@
+2007-05-03 Paul Jakma <paul.jakma@sun.com>
+
+	* memtypes.c: Add MTYPE_ATTR_EXTRA and MTYPE_BGP_ROUTE_EXTRA
+
 2007-05-01 David L Stevens <dlstevens@us.ibm.com>
 
 	* (general) These changes collectively add route-map and

+ 3 - 1
lib/memtypes.c

@@ -6,7 +6,7 @@
  * The script is sensitive to the format (though not whitespace), see
  * the top of memtypes.awk for more details.
  *
- * $Id: memtypes.c,v 1.11 2006/03/30 14:09:38 paul Exp $
+ * $Id$
  */
 
 #include "zebra.h"
@@ -96,6 +96,7 @@ struct memory_list memory_list_bgp[] =
   { MTYPE_PEER_GROUP,		"Peer group"			},
   { MTYPE_PEER_DESC,		"Peer description"		},
   { MTYPE_ATTR,			"BGP attribute"			},
+  { MTYPE_ATTR_EXTRA,		"BGP extra attributes"		},
   { MTYPE_AS_PATH,		"BGP aspath"			},
   { MTYPE_AS_SEG,		"BGP aspath seg"		},
   { MTYPE_AS_SEG_DATA,		"BGP aspath segment data"	},
@@ -104,6 +105,7 @@ struct memory_list memory_list_bgp[] =
   { MTYPE_BGP_TABLE,		"BGP table"			},
   { MTYPE_BGP_NODE,		"BGP node"			},
   { MTYPE_BGP_ROUTE,		"BGP route"			},
+  { MTYPE_BGP_ROUTE_EXTRA,	"BGP ancillary route info"	},
   { MTYPE_BGP_STATIC,		"BGP static"			},
   { MTYPE_BGP_ADVERTISE_ATTR,	"BGP adv attr"			},
   { MTYPE_BGP_ADVERTISE,	"BGP adv"			},

Some files were not shown because too many files changed in this diff