bgpd: debug buffers cleanup and optimization

Just the first change pushes bgp_update_receive() from 6th to ~14th on a
full internet table load profiling session.

* bgp_debug.c: (bgp_update_receive) The attrstr initialization is expensive,
  moved under the debug conditional where it is used and just initialize the
  first char to NULL.
  (bgp_update_default_send) Initialize attrstr needed for bgp_dump_attr().
  Moved some buffers used for printing IP[4|6] addresses under the debug
  conditionals that use them and reduced its size.

Signed-off-by: Jorge Boncompte [DTI2] <jorge@dti2.net>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c
index 390b556..95ed804 100644
--- a/bgpd/bgp_packet.c
+++ b/bgpd/bgp_packet.c
@@ -149,7 +149,6 @@
   struct bgp_info *binfo = NULL;
   bgp_size_t total_attr_len = 0;
   unsigned long pos;
-  char buf[BUFSIZ];
 
   s = peer->work;
   stream_reset (s);
@@ -199,10 +198,14 @@
 	stream_put_prefix (s, &rn->p);
       
       if (BGP_DEBUG (update, UPDATE_OUT))
-	zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d",
-	      peer->host,
-	      inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ),
-	      rn->p.prefixlen);
+        {
+          char buf[INET6_BUFSIZ];
+
+          zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d",
+                peer->host,
+                inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
+                rn->p.prefixlen);
+        }
 
       /* Synchnorize attribute.  */
       if (adj->attr)
@@ -285,7 +288,6 @@
   unsigned long pos;
   bgp_size_t unfeasible_len;
   bgp_size_t total_attr_len;
-  char buf[BUFSIZ];
 
   s = peer->work;
   stream_reset (s);
@@ -324,10 +326,14 @@
 	}
 
       if (BGP_DEBUG (update, UPDATE_OUT))
-	zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
-	      peer->host,
-	      inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ),
-	      rn->p.prefixlen);
+        {
+          char buf[INET6_BUFSIZ];
+
+          zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
+                peer->host,
+                inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
+                rn->p.prefixlen);
+        }
 
       peer->scount[afi][safi]--;
 
@@ -366,8 +372,6 @@
   struct prefix p;
   unsigned long pos;
   bgp_size_t total_attr_len;
-  char attrstr[BUFSIZ];
-  char buf[BUFSIZ];
 
   if (DISABLE_BGP_ANNOUNCE)
     return;
@@ -382,9 +386,13 @@
   /* Logging the attribute. */
   if (BGP_DEBUG (update, UPDATE_OUT))
     {
+      char attrstr[BUFSIZ];
+      char buf[INET6_BUFSIZ];
+      attrstr[0] = '\0';
+
       bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
       zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s",
-	    peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
+	    peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
 	    p.prefixlen, attrstr);
     }
 
@@ -435,7 +443,6 @@
   unsigned long cp;
   bgp_size_t unfeasible_len;
   bgp_size_t total_attr_len;
-  char buf[BUFSIZ];
 
   if (DISABLE_BGP_ANNOUNCE)
     return;
@@ -451,9 +458,13 @@
   pos = 0;
 
   if (BGP_DEBUG (update, UPDATE_OUT))
-    zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
-          peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ),
-          p.prefixlen);
+    {
+      char buf[INET6_BUFSIZ];
+
+      zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
+            peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
+            p.prefixlen);
+    }
 
   s = stream_new (BGP_MAX_PACKET_SIZE);
 
@@ -1515,7 +1526,6 @@
   struct bgp_nlri withdraw;
   struct bgp_nlri mp_update;
   struct bgp_nlri mp_withdraw;
-  char attrstr[BUFSIZ] = "";
 
   /* Status must be Established. */
   if (peer->status != Established) 
@@ -1632,6 +1642,9 @@
   if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
       || BGP_DEBUG (update, UPDATE_IN))
     {
+      char attrstr[BUFSIZ];
+      attrstr[0] = '\0';
+
       ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
       int lvl = (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
                  ? LOG_ERR : LOG_DEBUG;
@@ -2027,7 +2040,6 @@
 	      u_int32_t seq;
 	      int psize;
 	      char name[BUFSIZ];
-	      char buf[BUFSIZ];
 	      int ret;
 
 	      if (BGP_DEBUG (normal, NORMAL))
@@ -2097,15 +2109,19 @@
 		  p_pnt += psize;
 
 		  if (BGP_DEBUG (normal, NORMAL))
-		    zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
-			       peer->host,
-			       (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"), 
-			       (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
-			       orfp.seq, 
-			       inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ),
-			       orfp.p.prefixlen, orfp.ge, orfp.le,
-			       ok ? "" : " MALFORMED");
-                  
+		    {
+		      char buf[INET6_BUFSIZ];
+
+		      zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
+			         peer->host,
+			         (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
+			         (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
+			         orfp.seq,
+			         inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, INET6_BUFSIZ),
+			         orfp.p.prefixlen, orfp.ge, orfp.le,
+			         ok ? "" : " MALFORMED");
+		    }
+
 		  if (ok)
 		    ret = prefix_bgp_orf_set (name, afi, &orfp,
 				   (common & ORF_COMMON_PART_DENY ? 0 : 1 ),