Skip to content

Commit

Permalink
bgp: support new handler in case of changing BGP session
Browse files Browse the repository at this point in the history
When BGP session goes up/down, BGPSpeaker can detect changing the session.

Signed-off-by: Toshiki Tsuboi <t.tsubo2000@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
  • Loading branch information
ttsubo authored and fujita committed Dec 7, 2014
1 parent 567ff4e commit 3c03ba0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
6 changes: 5 additions & 1 deletion doc/source/library_bgp_speaker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ instance advertizes some prefixes.
print 'the best path changed:', event.remote_as, event.prefix,\
event.nexthop, event.is_withdraw
def detect_peer_down(remote_ip, remote_as):
print 'Peer down:', remote_ip, remote_as
if __name__ == "__main__":
speaker = BGPSpeaker(as_number=64512, router_id='10.0.0.1',
best_path_change_handler=dump_remote_best_path_change)
best_path_change_handler=dump_remote_best_path_change,
peer_down_handler=detect_peer_down)
speaker.neighbor_add('192.168.177.32', 64513)
# uncomment the below line if the speaker needs to talk with a bmp server.
Expand Down
36 changes: 34 additions & 2 deletions ryu/services/protocols/bgp/bgpspeaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def __init__(self, as_number, router_id,
refresh_stalepath_time=DEFAULT_REFRESH_STALEPATH_TIME,
refresh_max_eor_time=DEFAULT_REFRESH_MAX_EOR_TIME,
best_path_change_handler=None,
peer_down_handler=None,
peer_up_handler=None,
ssh_console=False,
label_range=DEFAULT_LABEL_RANGE):
"""Create a new BGPSpeaker object with as_number and router_id to
Expand Down Expand Up @@ -137,6 +139,12 @@ def __init__(self, as_number, router_id,
peer down. The handler is supposed to take one argument, the
instance of an EventPrefix class instance.
``peer_down_handler``, if specified, is called when BGP peering
session goes down.
``peer_up_handler``, if specified, is called when BGP peering
session goes up.
"""
super(BGPSpeaker, self).__init__()

Expand All @@ -150,9 +158,23 @@ def __init__(self, as_number, router_id,
self._core_start(settings)
self._init_signal_listeners()
self._best_path_change_handler = best_path_change_handler
self._peer_down_handler = peer_down_handler
self._peer_up_handler = peer_up_handler
if ssh_console:
hub.spawn(ssh.SSH_CLI_CONTROLLER.start)

def _notify_peer_down(self, peer):
remote_ip = peer.protocol.recv_open_msg.bgp_identifier
remote_as = peer.protocol.recv_open_msg.my_as
if self._peer_down_handler:
self._peer_down_handler(remote_ip, remote_as)

def _notify_peer_up(self, peer):
remote_ip = peer.protocol.recv_open_msg.bgp_identifier
remote_as = peer.protocol.recv_open_msg.my_as
if self._peer_up_handler:
self._peer_up_handler(remote_ip, remote_as)

def _notify_best_path_changed(self, path, is_withdraw):
if path.source:
nexthop = path.nexthop
Expand Down Expand Up @@ -182,8 +204,18 @@ def _init_signal_listeners(self):
CORE_MANAGER.get_core_service()._signal_bus.register_listener(
BgpSignalBus.BGP_BEST_PATH_CHANGED,
lambda _, info:
self._notify_best_path_changed(info['path'],
info['is_withdraw'])
self._notify_best_path_changed(info['path'],
info['is_withdraw'])
)
CORE_MANAGER.get_core_service()._signal_bus.register_listener(
BgpSignalBus.BGP_ADJ_DOWN,
lambda _, info:
self._notify_peer_down(info['peer'])
)
CORE_MANAGER.get_core_service()._signal_bus.register_listener(
BgpSignalBus.BGP_ADJ_UP,
lambda _, info:
self._notify_peer_up(info['peer'])
)

def _core_start(self, settings):
Expand Down

0 comments on commit 3c03ba0

Please sign in to comment.