Debian Codegraph @

neutron_dynamic_routing/services/bgp/agent/driver/os_ken/driver.py

raw symbols SCIP
1 # Copyright 2016 Huawei Technologies India Pvt. Ltd.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from os_ken.services.protocols.bgp import bgpspeaker
17 from os_ken.services.protocols.bgp.rtconf.neighbors import CONNECT_MODE_ACTIVE
18 from oslo_log import log as logging
19 from oslo_utils import encodeutils
20
21 from neutron_dynamic_routing._i18n import _LE, _LI
22 from neutron_dynamic_routing.services.bgp.agent.driver import base
23 from neutron_dynamic_routing.services.bgp.agent.driver import exceptions as bgp_driver_exc # noqa
24 from neutron_dynamic_routing.services.bgp.agent.driver import utils
25
26 LOG = logging.getLogger(__name__)
27
28
29 # Function for logging BGP peer and path changes.
30 def bgp_peer_down_cb(remote_ip, remote_as):
31 LOG.info(_LI('BGP Peer %(peer_ip)s for remote_as=%(peer_as)d went DOWN.'),
32 {'peer_ip': remote_ip, 'peer_as': remote_as})
33
34
35 def bgp_peer_up_cb(remote_ip, remote_as):
36 LOG.info(_LI('BGP Peer %(peer_ip)s for remote_as=%(peer_as)d is UP.'),
37 {'peer_ip': remote_ip, 'peer_as': remote_as})
38
39
40 def best_path_change_cb(event):
41 LOG.info(_LI("Best path change observed. cidr=%(prefix)s, "
42 "nexthop=%(nexthop)s, remote_as=%(remote_as)d, "
43 "is_withdraw=%(is_withdraw)s"),
44 {'prefix': event.prefix, 'nexthop': event.nexthop,
45 'remote_as': event.remote_as,
46 'is_withdraw': event.is_withdraw})
47
48
49 class OsKenBgpDriver(base.BgpDriverBase):
50 """BGP speaker implementation via os-ken."""
51
52 def __init__(self, cfg):
53 LOG.info(_LI('Initializing os-ken driver for BGP functionality.'))
54 self._read_config(cfg)
55
56 # Note: Even though os-ken can only support one BGP speaker as of now,
57 # we have tried making the framework generic for the future purposes.
58 self.cache = utils.BgpMultiSpeakerCache()
59
60 def _read_config(self, cfg):
61 if cfg is None or cfg.bgp_router_id is None:
62 # If either cfg or router_id is not specified, raise voice
63 LOG.error(_LE('BGP router-id MUST be specified for the correct '
64 'functional working.'))
65 else:
66 self.routerid = cfg.bgp_router_id
67 LOG.info(_LI('Initialized os-ken BGP Speaker driver interface '
68 'with bgp_router_id=%s'), self.routerid)
69
70 def add_bgp_speaker(self, speaker_as):
71 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
72 if curr_speaker is not None:
73 raise bgp_driver_exc.BgpSpeakerAlreadyScheduled(
74 current_as=speaker_as,
75 rtid=self.routerid)
76
77 # os-ken can only support One speaker
78 if self.cache.get_hosted_bgp_speakers_count() == 1:
79 raise bgp_driver_exc.BgpSpeakerMaxScheduled(count=1)
80
81 # Validate input parameters.
82 # speaker_as must be an integer in the allowed range.
83 utils.validate_as_num('local_as', speaker_as)
84
85 # Notify os-ken about BGP Speaker addition.
86 # Please note: Since, only the route-advertisement support is
87 # implemented we are explicitly setting the bgp_server_port
88 # attribute to 0 which disables listening on port 179.
89 curr_speaker = bgpspeaker.BGPSpeaker(
90 as_number=speaker_as,
91 router_id=self.routerid, bgp_server_port=0,
92 best_path_change_handler=best_path_change_cb,
93 peer_down_handler=bgp_peer_down_cb,
94 peer_up_handler=bgp_peer_up_cb)
95 LOG.info(_LI('Added BGP Speaker for local_as=%(as)d with '
96 'router_id= %(rtid)s.'),
97 {'as': speaker_as, 'rtid': self.routerid})
98
99 self.cache.put_bgp_speaker(speaker_as, curr_speaker)
100
101 def delete_bgp_speaker(self, speaker_as):
102 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
103 if not curr_speaker:
104 raise bgp_driver_exc.BgpSpeakerNotAdded(local_as=speaker_as,
105 rtid=self.routerid)
106 # Notify os-ken about BGP Speaker deletion
107 curr_speaker.shutdown()
108 LOG.info(_LI('Removed BGP Speaker for local_as=%(as)d with '
109 'router_id=%(rtid)s.'),
110 {'as': speaker_as, 'rtid': self.routerid})
111 self.cache.remove_bgp_speaker(speaker_as)
112
113 def add_bgp_peer(self, speaker_as, peer_ip, peer_as,
114 auth_type='none', password=None):
115 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
116 if not curr_speaker:
117 raise bgp_driver_exc.BgpSpeakerNotAdded(local_as=speaker_as,
118 rtid=self.routerid)
119
120 # Validate peer_ip and peer_as.
121 utils.validate_as_num('remote_as', peer_as)
122 utils.validate_ip_addr(peer_ip)
123 utils.validate_auth(auth_type, password)
124 if password is not None:
125 password = encodeutils.to_utf8(password)
126
127 curr_speaker.neighbor_add(address=peer_ip,
128 remote_as=peer_as,
129 enable_ipv4=True,
130 enable_ipv6=True,
131 password=password,
132 connect_mode=CONNECT_MODE_ACTIVE)
133 LOG.info(_LI('Added BGP Peer %(peer)s for remote_as=%(as)d to '
134 'BGP Speaker running for local_as=%(local_as)d.'),
135 {'peer': peer_ip, 'as': peer_as, 'local_as': speaker_as})
136
137 def delete_bgp_peer(self, speaker_as, peer_ip):
138 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
139 if not curr_speaker:
140 raise bgp_driver_exc.BgpSpeakerNotAdded(local_as=speaker_as,
141 rtid=self.routerid)
142 # Validate peer_ip. It must be a string.
143 utils.validate_ip_addr(peer_ip)
144
145 # Notify os-ken about BGP Peer removal
146 curr_speaker.neighbor_del(address=peer_ip)
147 LOG.info(_LI('Removed BGP Peer %(peer)s from BGP Speaker '
148 'running for local_as=%(local_as)d.'),
149 {'peer': peer_ip, 'local_as': speaker_as})
150
151 def advertise_route(self, speaker_as, cidr, nexthop):
152 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
153 if not curr_speaker:
154 raise bgp_driver_exc.BgpSpeakerNotAdded(local_as=speaker_as,
155 rtid=self.routerid)
156
157 # Validate cidr and nexthop. Both must be strings.
158 utils.validate_string(cidr)
159 utils.validate_string(nexthop)
160
161 # Notify os-ken about route advertisement
162 curr_speaker.prefix_add(prefix=cidr, next_hop=nexthop)
163 LOG.info(_LI('Route cidr=%(prefix)s, nexthop=%(nexthop)s is '
164 'advertised for BGP Speaker running for '
165 'local_as=%(local_as)d.'),
166 {'prefix': cidr, 'nexthop': nexthop, 'local_as': speaker_as})
167
168 def withdraw_route(self, speaker_as, cidr, nexthop=None):
169 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
170 if not curr_speaker:
171 raise bgp_driver_exc.BgpSpeakerNotAdded(local_as=speaker_as,
172 rtid=self.routerid)
173 # Validate cidr. It must be a string.
174 utils.validate_string(cidr)
175
176 # Notify os-ken about route withdrawal
177 curr_speaker.prefix_del(prefix=cidr)
178 LOG.info(_LI('Route cidr=%(prefix)s is withdrawn from BGP Speaker '
179 'running for local_as=%(local_as)d.'),
180 {'prefix': cidr, 'local_as': speaker_as})
181
182 def get_bgp_speaker_statistics(self, speaker_as):
183 LOG.info(_LI('Collecting BGP Speaker statistics for local_as=%d.'),
184 speaker_as)
185 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
186 if not curr_speaker:
187 raise bgp_driver_exc.BgpSpeakerNotAdded(local_as=speaker_as,
188 rtid=self.routerid)
189
190 # TODO(vikram): Filter and return the necessary information.
191 # Will be done as part of new RFE requirement
192 # https://bugs.launchpad.net/neutron/+bug/1527993
193 return curr_speaker.neighbor_state_get()
194
195 def get_bgp_peer_statistics(self, speaker_as, peer_ip):
196 LOG.info(_LI('Collecting BGP Peer statistics for peer_ip=%(peer)s, '
197 'running in speaker_as=%(speaker_as)d '),
198 {'peer': peer_ip, 'speaker_as': speaker_as})
199 curr_speaker = self.cache.get_bgp_speaker(speaker_as)
200 if not curr_speaker:
201 raise bgp_driver_exc.BgpSpeakerNotAdded(local_as=speaker_as,
202 rtid=self.routerid)
203
204 # TODO(vikram): Filter and return the necessary information.
205 # Will be done as part of new RFE requirement
206 # https://bugs.launchpad.net/neutron/+bug/1527993
207 return curr_speaker.neighbor_state_get(address=peer_ip)