diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2017-01-30 16:43:43 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2017-02-11 14:21:04 +0900 |
commit | c4218ccde3feecf4fd9bb54202227a791e52f647 (patch) | |
tree | 7a1aa18f6036c8df1ca7b6a0a79539db9b3e0240 | |
parent | 6c92a75aa8e3ef293104c82d1c55f45c354db9c3 (diff) |
BGPSpeaker: Support to advertise Type 1, 2 Route Target
Currently, BGPSpeaker supports only type 0 (Two-Octet AS-Specific) Route
Target (or Route Distinguisher).
This patch enables to advertise Type 1 (IPv4-Address-Specific) and Type
2 (Four-Octet AS-Specific) Route Targets.
Reported-by: Albert Siersema <albert@mediacaster.nl>
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/services/protocols/bgp/info_base/vrf.py | 16 | ||||
-rw-r--r-- | ryu/services/protocols/bgp/utils/bgp.py | 38 |
2 files changed, 41 insertions, 13 deletions
diff --git a/ryu/services/protocols/bgp/info_base/vrf.py b/ryu/services/protocols/bgp/info_base/vrf.py index c4e41efa..4900e12c 100644 --- a/ryu/services/protocols/bgp/info_base/vrf.py +++ b/ryu/services/protocols/bgp/info_base/vrf.py @@ -30,7 +30,6 @@ from ryu.lib.packet.bgp import BGPPathAttributeOrigin from ryu.lib.packet.bgp import BGPPathAttributeAsPath from ryu.lib.packet.bgp import EvpnEthernetSegmentNLRI from ryu.lib.packet.bgp import BGPPathAttributeExtendedCommunities -from ryu.lib.packet.bgp import BGPTwoOctetAsSpecificExtendedCommunity from ryu.lib.packet.bgp import BGPPathAttributeMultiExitDisc from ryu.lib.packet.bgp import BGPEncapsulationExtendedCommunity from ryu.lib.packet.bgp import BGPEvpnEsiLabelExtendedCommunity @@ -47,6 +46,7 @@ from ryu.services.protocols.bgp.constants import VRF_TABLE from ryu.services.protocols.bgp.info_base.base import Destination from ryu.services.protocols.bgp.info_base.base import Path from ryu.services.protocols.bgp.info_base.base import Table +from ryu.services.protocols.bgp.utils.bgp import create_rt_extended_community from ryu.services.protocols.bgp.utils.stats import LOCAL_ROUTES from ryu.services.protocols.bgp.utils.stats import REMOTE_ROUTES from ryu.services.protocols.bgp.utils.stats import RESOURCE_ID @@ -267,19 +267,9 @@ class VrfTable(Table): es_import=es_import)) for rt in vrf_conf.export_rts: - as_num, local_admin = rt.split(':') - subtype = 2 - communities.append(BGPTwoOctetAsSpecificExtendedCommunity( - as_number=int(as_num), - local_administrator=int(local_admin), - subtype=subtype)) + communities.append(create_rt_extended_community(rt, 2)) for soo in vrf_conf.soo_list: - as_num, local_admin = soo.split(':') - subtype = 3 - communities.append(BGPTwoOctetAsSpecificExtendedCommunity( - as_number=int(as_num), - local_administrator=int(local_admin), - subtype=subtype)) + communities.append(create_rt_extended_community(soo, 3)) # Set Tunnel Encapsulation Attribute tunnel_type = kwargs.get('tunnel_type', None) diff --git a/ryu/services/protocols/bgp/utils/bgp.py b/ryu/services/protocols/bgp/utils/bgp.py index 48ac3c6b..4979e441 100644 --- a/ryu/services/protocols/bgp/utils/bgp.py +++ b/ryu/services/protocols/bgp/utils/bgp.py @@ -19,6 +19,8 @@ import logging import socket +import netaddr + from ryu.lib.packet.bgp import ( BGPUpdate, RF_IPv4_UC, @@ -36,6 +38,9 @@ from ryu.lib.packet.bgp import ( BGPPathAttributeUnknown, BGP_ATTR_FLAG_OPTIONAL, BGP_ATTR_FLAG_TRANSITIVE, + BGPTwoOctetAsSpecificExtendedCommunity, + BGPIPv4AddressSpecificExtendedCommunity, + BGPFourOctetAsSpecificExtendedCommunity, ) from ryu.services.protocols.bgp.info_base.rtc import RtcPath from ryu.services.protocols.bgp.info_base.ipv4 import Ipv4Path @@ -141,3 +146,36 @@ def create_end_of_rib_update(): # Bgp update message instance that can used as End of RIB marker. UPDATE_EOR = create_end_of_rib_update() + + +def create_rt_extended_community(value, subtype=2): + """ + Creates an instance of the BGP Route Target Community (if "subtype=2") + or Route Origin Community ("subtype=3"). + + :param value: String of Route Target or Route Origin value. + :param subtype: Subtype of Extended Community. + :return: An instance of Route Target or Route Origin Community. + """ + global_admin, local_admin = value.split(':') + local_admin = int(local_admin) + if global_admin.isdigit() and 0 <= int(global_admin) <= 0xffff: + ext_com = BGPTwoOctetAsSpecificExtendedCommunity( + subtype=subtype, + as_number=int(global_admin), + local_administrator=local_admin) + elif global_admin.isdigit() and 0xffff < int(global_admin) <= 0xffffffff: + ext_com = BGPFourOctetAsSpecificExtendedCommunity( + subtype=subtype, + as_number=int(global_admin), + local_administrator=local_admin) + elif netaddr.valid_ipv4(global_admin): + ext_com = BGPIPv4AddressSpecificExtendedCommunity( + subtype=subtype, + ipv4_address=global_admin, + local_administrator=local_admin) + else: + raise ValueError( + 'Invalid Route Target or Route Origin value: %s' % value) + + return ext_com |