From fb2ff139ca5396e49b0ec151968b6b62b4718784 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Thu, 4 Jun 2020 13:45:19 +0200 Subject: Add communities parameter to network.add --- ryu/lib/packet/bgp.py | 19 ++++++++++++++----- ryu/services/protocols/bgp/api/base.py | 1 + ryu/services/protocols/bgp/api/rtconf.py | 4 ++-- .../protocols/bgp/core_managers/table_manager.py | 7 ++++++- ryu/tests/unit/packet/.test_bgp.py.swp | Bin 0 -> 1024 bytes 5 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 ryu/tests/unit/packet/.test_bgp.py.swp diff --git a/ryu/lib/packet/bgp.py b/ryu/lib/packet/bgp.py index 89ed191e..6aabc843 100644 --- a/ryu/lib/packet/bgp.py +++ b/ryu/lib/packet/bgp.py @@ -106,10 +106,11 @@ BGP_ATTR_ORIGIN_INCOMPLETE = 0x02 AS_TRANS = 23456 # RFC 4893 -# Well known commmunities (RFC 1997) -BGP_COMMUNITY_NO_EXPORT = 0xffffff01 -BGP_COMMUNITY_NO_ADVERTISE = 0xffffff02 -BGP_COMMUNITY_NO_EXPORT_SUBCONFED = 0xffffff03 +# Well known commmunities +BGP_COMMUNITY_BLACKHOLE = 0xffff029a # RFC 7999 +BGP_COMMUNITY_NO_EXPORT = 0xffffff01 # RFC 1997 +BGP_COMMUNITY_NO_ADVERTISE = 0xffffff02 # RFC 1997 +BGP_COMMUNITY_NO_EXPORT_SUBCONFED = 0xffffff03 # RFC 1997 # RFC 4360 # The low-order octet of Type field (subtype) @@ -3963,10 +3964,11 @@ class BGPPathAttributeCommunities(_PathAttribute): _ATTR_FLAGS = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANSITIVE # String constants of well-known-communities + BLACKHOLE = int('0xFFFF029A', 16) NO_EXPORT = int('0xFFFFFF01', 16) NO_ADVERTISE = int('0xFFFFFF02', 16) NO_EXPORT_SUBCONFED = int('0xFFFFFF03', 16) - WELL_KNOW_COMMUNITIES = (NO_EXPORT, NO_ADVERTISE, NO_EXPORT_SUBCONFED) + WELL_KNOW_COMMUNITIES = (BLACKHOLE, NO_EXPORT, NO_ADVERTISE, NO_EXPORT_SUBCONFED) def __init__(self, communities, flags=0, type_=None, length=None): @@ -3997,6 +3999,13 @@ class BGPPathAttributeCommunities(_PathAttribute): buf += bincomm return buf + @staticmethod + def is_blackhole(comm_attr): + """Returns True if given value matches well-known community BLACKHOLE + attribute value. + """ + return comm_attr == BGPPathAttributeCommunities.BLACKHOLE + @staticmethod def is_no_export(comm_attr): """Returns True if given value matches well-known community NO_EXPORT diff --git a/ryu/services/protocols/bgp/api/base.py b/ryu/services/protocols/bgp/api/base.py index 598ee04b..33f2a7cc 100644 --- a/ryu/services/protocols/bgp/api/base.py +++ b/ryu/services/protocols/bgp/api/base.py @@ -58,6 +58,7 @@ FLOWSPEC_FAMILY = 'flowspec_family' FLOWSPEC_RULES = 'rules' FLOWSPEC_ACTIONS = 'actions' AS_PATH = 'as_path' +COMMUNITIES = 'communities' # API call registry _CALL_REGISTRY = {} diff --git a/ryu/services/protocols/bgp/api/rtconf.py b/ryu/services/protocols/bgp/api/rtconf.py index 82da22f0..54f89c27 100644 --- a/ryu/services/protocols/bgp/api/rtconf.py +++ b/ryu/services/protocols/bgp/api/rtconf.py @@ -273,9 +273,9 @@ def get_vrfs_conf(): @register(name='network.add') -def add_network(prefix, next_hop=None, as_path=None): +def add_network(prefix, next_hop=None, as_path=None, communities=None): tm = CORE_MANAGER.get_core_service().table_manager - tm.update_global_table(prefix, next_hop, as_path=as_path) + tm.update_global_table(prefix, next_hop, as_path=as_path, communities=communities) return True diff --git a/ryu/services/protocols/bgp/core_managers/table_manager.py b/ryu/services/protocols/bgp/core_managers/table_manager.py index b60b785d..89dd3182 100644 --- a/ryu/services/protocols/bgp/core_managers/table_manager.py +++ b/ryu/services/protocols/bgp/core_managers/table_manager.py @@ -50,9 +50,11 @@ from ryu.lib.packet.bgp import RF_VPNv4_FLOWSPEC from ryu.lib.packet.bgp import RF_VPNv6_FLOWSPEC from ryu.lib.packet.bgp import RF_L2VPN_FLOWSPEC from ryu.lib.packet.bgp import RF_RTC_UC +from ryu.lib.packet.bgp import BGPPathAttributeCommunities from ryu.lib.packet.bgp import BGPPathAttributeOrigin from ryu.lib.packet.bgp import BGPPathAttributeAsPath from ryu.lib.packet.bgp import BGPPathAttributeExtendedCommunities +from ryu.lib.packet.bgp import BGP_ATTR_TYPE_COMMUNITIES from ryu.lib.packet.bgp import BGP_ATTR_TYPE_ORIGIN from ryu.lib.packet.bgp import BGP_ATTR_TYPE_AS_PATH from ryu.lib.packet.bgp import BGP_ATTR_ORIGIN_IGP @@ -772,7 +774,7 @@ class TableCoreManager(object): nlri=prefix, communities=communities, is_withdraw=is_withdraw) - def update_global_table(self, prefix, next_hop=None, is_withdraw=False, as_path=None): + def update_global_table(self, prefix, next_hop=None, is_withdraw=False, as_path=None, communities=None): """Update a BGP route in the Global table for the given `prefix` with the given `next_hop`. @@ -792,6 +794,9 @@ class TableCoreManager(object): pathattrs[BGP_ATTR_TYPE_ORIGIN] = origin pathattrs[BGP_ATTR_TYPE_AS_PATH] = aspath + if communities: + pathattrs[BGP_ATTR_TYPE_COMMUNITIES] = BGPPathAttributeCommunities(communities) + net = netaddr.IPNetwork(prefix) addr = str(net.ip) masklen = net.prefixlen diff --git a/ryu/tests/unit/packet/.test_bgp.py.swp b/ryu/tests/unit/packet/.test_bgp.py.swp new file mode 100644 index 00000000..dd9b5b83 Binary files /dev/null and b/ryu/tests/unit/packet/.test_bgp.py.swp differ -- cgit v1.2.3