diff options
author | Toshiki Tsuboi <t.tsubo2000@gmail.com> | 2014-12-06 02:46:33 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-12-07 23:35:01 +0900 |
commit | 3c03ba0034d61e12eddbe979371bdecf9c4bbd21 (patch) | |
tree | 067d177777eac3d3cfcbc53818a2b4902b5dde00 | |
parent | 567ff4ec170296144f66605eb8a6a8730a34e9a0 (diff) |
bgp: support new handler in case of changing BGP session
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>
-rw-r--r-- | doc/source/library_bgp_speaker.rst | 6 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/bgpspeaker.py | 36 |
2 files changed, 39 insertions, 3 deletions
diff --git a/doc/source/library_bgp_speaker.rst b/doc/source/library_bgp_speaker.rst index e17d8247..cf809264 100644 --- a/doc/source/library_bgp_speaker.rst +++ b/doc/source/library_bgp_speaker.rst @@ -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. diff --git a/ryu/services/protocols/bgp/bgpspeaker.py b/ryu/services/protocols/bgp/bgpspeaker.py index 87fdfe54..6479bf86 100644 --- a/ryu/services/protocols/bgp/bgpspeaker.py +++ b/ryu/services/protocols/bgp/bgpspeaker.py @@ -108,6 +108,8 @@ class BGPSpeaker(object): 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 @@ -137,6 +139,12 @@ class BGPSpeaker(object): 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__() @@ -150,9 +158,23 @@ class BGPSpeaker(object): 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 @@ -182,8 +204,18 @@ class BGPSpeaker(object): 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): |