1 # Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 Defines base data types and models required specifically for VPN support.
24 from ryu.lib.packet.bgp import RF_L2_EVPN
25 from ryu.services.protocols.bgp.info_base.base import Destination
26 from ryu.services.protocols.bgp.info_base.base import NonVrfPathProcessingMixin
27 from ryu.services.protocols.bgp.info_base.base import Path
28 from ryu.services.protocols.bgp.info_base.base import Table
30 LOG = logging.getLogger('bgpspeaker.info_base.vpn')
33 class VpnTable(Table):
34 """Global table to store VPNv4 routing information.
36 Uses `VpnvXDest` to store destination information for each known vpnvX
42 def __init__(self, core_service, signal_bus):
43 super(VpnTable, self).__init__(None, core_service, signal_bus)
45 def _table_key(self, vpn_nlri):
46 """Return a key that will uniquely identify this vpnvX NLRI inside
49 return vpn_nlri.route_dist + ':' + vpn_nlri.prefix
51 def _create_dest(self, nlri):
52 return self.VPN_DEST_CLASS(self, nlri)
55 return '%s(scope_id: %s, rf: %s)' % (
56 self.__class__.__name__, self.scope_id, self.route_family
60 @six.add_metaclass(abc.ABCMeta)
66 def clone_to_vrf(self, is_withdraw=False):
67 if self.ROUTE_FAMILY == RF_L2_EVPN:
68 # Because NLRI class is the same if the route family is EVPN,
69 # we re-use the NLRI instance.
71 else: # self.ROUTE_FAMILY in [RF_IPv4_VPN, RF_IPv46_VPN]
72 vrf_nlri = self.NLRI_CLASS(self._nlri.prefix)
76 pathattrs = self.pathattr_map
78 vrf_path = self.VRF_PATH_CLASS(
79 puid=self.VRF_PATH_CLASS.create_puid(
80 self._nlri.route_dist,
84 src_ver_num=self.source_version_num,
87 is_withdraw=is_withdraw,
88 label_list=self._nlri.label_list)
93 @six.add_metaclass(abc.ABCMeta)
94 class VpnDest(Destination, NonVrfPathProcessingMixin):
95 """Base class for VPN destinations."""
97 def _best_path_lost(self):
98 old_best_path = self._best_path
99 NonVrfPathProcessingMixin._best_path_lost(self)
100 self._core_service._signal_bus.best_path_changed(old_best_path, True)
102 # Best-path might have been imported into VRF tables, we have to
103 # withdraw from them, if the source is a peer.
105 withdraw_clone = old_best_path.clone(for_withdrawal=True)
106 tm = self._core_service.table_manager
107 tm.import_single_vpn_path_to_all_vrfs(
108 withdraw_clone, path_rts=old_best_path.get_rts()
111 def _new_best_path(self, best_path):
112 NonVrfPathProcessingMixin._new_best_path(self, best_path)
113 self._core_service._signal_bus.best_path_changed(best_path, False)
115 # Extranet feature requires that we import new best path into VRFs.
116 tm = self._core_service.table_manager
117 tm.import_single_vpn_path_to_all_vrfs(
118 self._best_path, self._best_path.get_rts())