Monday 13 January 2014

BGPFLAP: a simple tcl script to flap an interface

Hi all,
today I was trying the bgp dampening feature, and I wrote this simple tclsh script to flap an interface during my lab tests:

first, configure an interface with the route to flap:

R3#sh run int lo 10 | beg int
interface Loopback10
ip address 10.10.10.10 255.255.255.0
end

then configure the network statement for that route under the bgp process: 

R3#sh run | sec router bgp
router bgp 300
no synchronization
bgp log-neighbor-changes
bgp dampening
network 10.10.10.0 mask 255.255.255.0
neighbor 192.168.2.1 remote-as 200
neighbor 192.168.2.1 ebgp-multihop 2
neighbor 192.168.2.1 update-source Loopback0
no auto-summary
R3#


and here is the tclsh script, I reccomend a wait time of 40-50 sec with the default bgp timers


##################################################################################
## Tclsh BGPFLAP SCRIPT v0.1 Beta: a tclsh script to flap a bgp route
## By Marco Rizzi ( http://rizzitech.blogspot.com ) marco.rizzi.com[A_T]gmail.com
## Date Feb 28, 2010
## licensed under a Creative Commons Attribution 3.0 United States License
## ( http://creativecommons.org/licenses/by/3.0/us/ ) ;-)
##################################################################################
### USAGE: BGPFLAP [interface_to_flap] [number_of_flaps] [wait_secs]

## DON'T USE ON PRODUCTION SYSTEMS, IT'S ONLY FOR LAB TESTING
## No warranty, provided "AS IS"

## Main procedure
proc BGPFLAP {interface n_flaps wait} {

## Before you start, be shure you have a bgp process up and running
## and the interface you will flap declared under the bgp process
## BGP DAMPENING would be nice to have on neighbors too :-)

## calculates msecs to wait: 1 flap = 1 down + 1 up so wait/2
set wait [ expr {$wait/2 * 1000}]

## produces n flaps of the desired route shutting down the interface at wait/2 rate
for {set i 0} {$i <= $n_flaps} {incr i} {

ios_config "interface $interface" "shutdown"

#wait, let bgp withdraw the route
after $wait

ios_config "interface $interface" "no shutdown"

#wait, let bgp announce the route
after $wait
}
}


################################# END OF SCRIPT ###################################
##
##
### USAGE: BGPFLAP [interface_to_flap] [number_of_flaps] [wait_secs]
#### enjoy ;-)



Let's try with

R3(tcl)#
R3(tcl)#BGPFLAP loopback10 6 45

*Feb 28 16:08:43.443: %LINK-5-CHANGED: Interface Loopback10, changed state to administratively down
*Feb 28 16:08:44.447: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback10, changed state to down
*Feb 28 16:09:05.467: %LINK-3-UPDOWN: Interface Loopback10, changed state to up
*Feb 28 16:09:06.467: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback10, changed state to up
!-- ...and so on


on the other side you can see:

R2#debug ip bgp dampening
BGP dampening debugging is on for address family: IPv4 Unicast
R2#
*Feb 28 16:08:41.639: EvD: charge penalty 1000, new accum. penalty 1000, flap count 1
*Feb 28 16:08:41.643: BGP(0): charge penalty for 10.10.10.0/24 path 300 with halflife-time 15 reuse/suppress 750/2000
*Feb 28 16:08:41.647: BGP(0): flapped 1 times since 00:00:00. New penalty is 1000
R2#
*Feb 28 16:09:12.671: EvD: accum. penalty 977, not suppressed
R2#
*Feb 28 16:09:21.083: EvD: accum. penalty decayed to 973 after 8 second(s)
R2#
*Feb 28 16:09:42.759: EvD: accum. penalty decayed to 958 after 22 second(s)
*Feb 28 16:09:42.763: EvD: charge penalty 1000, new accum. penalty 1958, flap count 2
*Feb 28 16:09:42.767: BGP(0): charge penalty for 10.10.10.0/24 path 300 with halflife-time 15 reuse/suppress 750/2000
*Feb 28 16:09:42.767: BGP(0): flapped 2 times since 00:01:01. New penalty is 1958
R2#

R2#sh ip bgp 10.10.10.0
BGP routing table entry for 10.10.10.0/24, version 24
Paths: (1 available, no best path)
Flag: 0x820
Not advertised to any peer
300, (suppressed due to dampening)
192.168.3.1 (metric 65) from 192.168.3.1 (192.168.3.1)
Origin IGP, metric 0, localpref 100, valid, external
Dampinfo: penalty 3521, flapped 4 times in 00:05:19, reuse in 00:00:43
R2#

As you can see, 45 secs of flapping time is barely enough to let bgp announce/withdraw the route, here R2 has noticed only 4 flaps, with a penality of 3521 points...


hope this helps someone to play with bgp :-)

No comments:

Post a Comment