Alexander V. Chernikov | 6 Mar 2012 19:14
Picon

Re: BIRD-1.3.6 - show bgp summary command

On 28.02.2012 14:35, Alexander Shikoff wrote:
> Hello!
>
> On Mon, Feb 27, 2012 at 10:07:26PM +0400, Alexander V. Chernikov wrote:
>> On 27.02.2012 21:31, Alexander Shikoff wrote:
>>> Hello List,
>>>
>>> On Mon, Feb 27, 2012 at 11:41:53AM +0100, Ondrej Zajicek wrote:
>>>> On Sat, Feb 25, 2012 at 03:33:13PM +0200, Alexander Shikoff wrote:
>>>>> On Thu, Feb 23, 2012 at 04:48:00PM +0200, Dean Belev wrote:
>>>>>> Hi there,
>>>>>>
>>>>>> Is the patch for 1.3.4
>>>>>> (http://marc.info/?l=bird-users&m=132155489919052&w=2
>>>>>> <http://marc.info/?l=bird-users&m=132155489919052&w=2>) usable for 1.3.6 ?
>>>>>>
>>>>>> We have a strange behavior when we tried to implement it for 1.3.6 - the
>>>>>> process stops right after the Enter is triggered.
>>>>>> But the "?" function works correctly.
>>>>>
>>>>> Old patch does not fit the source code and bird architecture as well.
>>>>> I've changed it in order to more comply to a birdc syntax.
>>>>
>>>> To fit better with BIRD architecture, it would be perhaps better to
>>>> remove that from hooks and global code and move it completely to BGP
>>>> code (like 'show ospf' is in OSPF code) and name it something like
>>>> 'show bgp summary'.
>>>
>>> Yes, you're right. I'm going to modify a patch, but I'm wondering
>>> what is the best way to get a list of all BGP protocols?
>>>
>>> The only way I know at a moment is just walking through proto_list
>>> and comparing proto name to "BGP". Is there a better way?
>> You have to walk through all protocols, yes. However, comparison is a
>> bit simpler. You can take a look into bgp_incoming_connection() for more
>> details.
>
> Thank you Ondrej and Alexander for pointing me.
> Universal patch for both IPv4 and IPv6 sources is attached.
> Reviews and comments are kindly appreciated.

bird is based on protocol instances and it its reasonable to name 
protocols explicitly in config file.
'show bgp sum proto' gives us mapping for proto -> peer address, but 
what about reverse mapping?

Like this:
bird> show bgp summary
Proto      Peer                    AS Last state change   Prefixes 
rcvd/best   State/Last error
b_7204     10.2.33.4            65001 22:00               0/0 
        Idle          Received: No supported AFI/SAFI
b_7206     10.2.33.6            65001 22:00               8/3 
        Established
b_mega     10.0.0.1             42678 22:00               0/0 
        Connect

The rest of the patch is fine (to me).

P.S.
I've changed style a bit, new version attached.
>
>
> Example outputs:
>
> # birdc6
> BIRD 1.3.6 ready.
> bird>  show protocols
> name     proto    table    state  since       info
> device1  Device   master   up     2012-02-28 12:29:44
> ITCONSULTING BGP      master   up     2012-02-28 12:30:16  Established
> CITYNET  BGP      master   start  2012-02-28 12:29:44  Passive
> bird>  show bgp ?
> show bgp summary [<name>]                      Show BGP peers summary
> bird>  show bgp summary CITYNET
> Peer
>           AS Last state change   Prefixes rcvd/best   State/Last error
> 2001:7f8:63::39
>        16066 2012-02-28 12:29:43 0/0                  Passive
> bird>  show bgp summary
> Peer
>           AS Last state change   Prefixes rcvd/best   State/Last error
> 2001:7f8:63::11
>        25372 2012-02-28 12:30:15 1/1                  Established
> 2001:7f8:63::39
>        16066 2012-02-28 12:29:43 0/0                  Passive
> bird>
>
>
> # birdc
> BIRD 1.3.6 ready.
> bird>  show protocols
> name     proto    table    state  since       info
> device1  Device   master   up     2012-02-28 12:31:44
> ITCONS   BGP      master   up     2012-02-28 12:33:00  Established
> UOS      BGP      master   start  2012-02-28 12:33:29  Passive       Received: Peer de-configured
> BOOM     Static   master   up     2012-02-28 12:31:44
> bird>  show bgp summary BOOM
> BOOM: Not a BGP protocol
> bird>  show bgp summary ITCONS
> Peer                    AS Last state change   Prefixes rcvd/best   State/Last error
> 193.25.180.17        25372 2012-02-28 12:33:00 2/2                  Established
> bird>  show bgp summary
> Peer                    AS Last state change   Prefixes rcvd/best   State/Last error
> 193.25.180.17        25372 2012-02-28 12:33:00 2/2                  Established
> 193.25.180.26        42546 2012-02-28 12:33:29 0/0                  Passive       Received: Peer de-configured
> bird>
>

diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c
index 1e7071a..00bb577 100644
--- a/proto/bgp/bgp.c
+++ b/proto/bgp/bgp.c
 <at>  <at>  -77,7 +77,7  <at>  <at>  static void bgp_close(struct bgp_proto *p, int apply_md5);
 static void bgp_connect(struct bgp_proto *p);
 static void bgp_active(struct bgp_proto *p);
 static sock *bgp_setup_listen_sk(ip_addr addr, unsigned port, u32 flags);
-
+static void bgp_sh_proto_summary(struct proto *p);

 /**
  * bgp_open - open a BGP instance
 <at>  <at>  -1339,6 +1339,82  <at>  <at>  bgp_show_proto_info(struct proto *P)
     }
 }

+void
+bgp_sh_summary(struct symbol *sym)
+{
+  struct proto_config *pc = NULL;
+
+  if (sym)
+    {
+      if (sym->class != SYM_PROTO)
+        cf_error("%s: Not a protocol", sym->name);
+      pc = (struct proto_config *)sym->def;
+      if (pc->protocol != &proto_bgp)
+        cf_error("%s: Not a BGP protocol", sym->name);
+    }
+  else
+    {
+      if (EMPTY_LIST(config->protos))
+	{
+	  cli_msg(1006, "No protocols match");
+	  return;
+	}
+    }
+
+#ifdef IPV6
+      cli_msg(-2002, "%-10s %s\n%12s %-19s %-20s %s", "Proto", "Peer", "AS", "Last state change", "Prefixes
rcvd/best", "State/Last error" );
+#else
+      cli_msg(-2002, "%-10s %-15s %10s %-19s %-20s %s", "Proto", "Peer", "AS", "Last state change",
"Prefixes rcvd/best", "State/Last error" );
+#endif
+
+  if (pc)
+    {
+      /* Show single protocol info */
+      bgp_sh_proto_summary(pc->proto);
+      cli_msg(0, "");
+      return;
+    }
+
+  WALK_LIST(pc, config->protos)
+    {
+      if ((pc->protocol != &proto_bgp) || (!pc->proto))
+	continue;
+
+      bgp_sh_proto_summary(pc->proto);
+    }
+
+    cli_msg(0, "");
+}
+
+static void
+bgp_sh_proto_summary(struct proto *p)
+{
+  struct bgp_proto *bp;
+  byte tbuf[TM_DATETIME_BUFFER_SIZE];
+  char rs[20];
+  char ps[256];
+
+  /* Get protocol data */
+  tm_format_datetime(tbuf, &config->tf_proto, p->last_state_change);
+
+  struct proto_stats *s = &p->stats;
+  bsnprintf(rs, sizeof(rs), "%u/%u", s->imp_routes, s->pref_routes);
+
+  p->proto->get_status(p, ps);
+
+  /* Get BGP protocol from protocol */
+  bp = (struct bgp_proto *) p;
+
+  /* Display summary */
+#ifdef IPV6
+  cli_msg(-1006, "%-10s %I\n%12u %-19s %-20s %s",
+#else
+  cli_msg(-1006, "%-10s %-15I %10u %-19s %-20s %s",
+#endif
+		p->name, bp->cf->remote_ip,
+		bp->remote_as, tbuf, rs, ps);
+}
+
 struct protocol proto_bgp = {
   name:			"BGP",
   template:		"bgp%d",
diff --git a/proto/bgp/bgp.h b/proto/bgp/bgp.h
index 59313ef..7b77281 100644
--- a/proto/bgp/bgp.h
+++ b/proto/bgp/bgp.h
 <at>  <at>  -228,6 +228,8  <at>  <at>  static inline void set_next_hop(byte *b, ip_addr addr) { ((ip_addr *) b)[0] = ad
 static inline void set_next_hop(byte *b, ip_addr addr) { ((ip_addr *) b)[0] = addr; }
 #endif

+void bgp_sh_summary(struct symbol *sym);
+
 void bgp_attach_attr(struct ea_list **to, struct linpool *pool, unsigned attr, uintptr_t val);
 byte *bgp_attach_attr_wa(struct ea_list **to, struct linpool *pool, unsigned attr, unsigned len);
 struct rta *bgp_decode_attrs(struct bgp_conn *conn, byte *attr, unsigned int len, struct linpool
*pool, int mandatory, struct attr_helper *ah);
diff --git a/proto/bgp/config.Y b/proto/bgp/config.Y
index f76fbf2..82b3761 100644
--- a/proto/bgp/config.Y
+++ b/proto/bgp/config.Y
 <at>  <at>  -152,6 +152,11  <at>  <at>  CF_ADDTO(dynamic_attr, BGP_EXT_COMMUNITY

 CF_ENUM(T_ENUM_BGP_ORIGIN, ORIGIN_, IGP, EGP, INCOMPLETE)

+CF_CLI_HELP(SHOW BGP, , [[Show BGP protocol information]])
+
+CF_CLI(SHOW BGP SUMMARY, optsym, [<name>], [[Show BGP peers summary]])
+{ bgp_sh_summary($4); };
+
 CF_CODE

 CF_END

Gmane