Skip to content

Commit 252168d

Browse files
authored
[2019111] Added Support for BGP allow list feature to have route-map action of setting tag (#10869)
What I did: Added support to create route-map action set tag when the the allow prefix list matches. The tag can ben define by user in constants.yml. Why I did: Since for Allow List feature we call from base route-map allow-list route-map having set tag option provides way for base route-map to do match tag and take any further action if needed. Adding tag provide metadata that can used by base route-map
1 parent be4dbb1 commit 252168d

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

src/sonic-bgpcfgd/bgpcfgd/managers_allow_list.py

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self, common_objs, db, table):
4040
)
4141
self.key_re = re.compile(r"^DEPLOYMENT_ID\|\d+\|\S+$|^DEPLOYMENT_ID\|\d+$")
4242
self.enabled = self.__get_enabled()
43+
self.prefix_match_tag = self.__get_routemap_tag()
4344
self.__load_constant_lists()
4445

4546
def set_handler(self, key, data):
@@ -396,6 +397,8 @@ def __update_allow_route_map_entry(self, af, allow_address_pl_name, community_na
396397
]
397398
if not community_name.endswith(self.EMPTY_COMMUNITY):
398399
cmds.append(" match community %s" % community_name)
400+
elif self.prefix_match_tag:
401+
cmds.append(" set tag %s" % self.prefix_match_tag)
399402
return cmds
400403

401404
def __update_default_route_map_entry(self, route_map_name, default_action_community):
@@ -612,6 +615,20 @@ def __get_route_map_calls(self, rms):
612615
inside_name = result.group(1)
613616
return rm_2_call
614617

618+
def __get_routemap_tag(self):
619+
"""
620+
Find if any user define tag is provided to be used when allow prefifx list is matched
621+
:return: string: prefix mix tag if define in constants.yml else None
622+
"""
623+
prefix_match_tag = None
624+
if 'bgp' in self.constants and \
625+
'allow_list' in self.constants["bgp"] and \
626+
'prefix_match_tag' in \
627+
self.constants["bgp"]["allow_list"]:
628+
prefix_match_tag = \
629+
self.constants["bgp"]["allow_list"]["prefix_match_tag"]
630+
return prefix_match_tag
631+
615632
@staticmethod
616633
def __get_peer_group_to_restart(deployment_id, pg_2_rm, rm_2_call):
617634
"""

src/sonic-bgpcfgd/tests/test_allow_list.py

+85-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,26 @@
2525
}
2626
}
2727

28+
global_constants_with_prefix_match_tag = {
29+
"bgp": {
30+
"allow_list": {
31+
"enabled": True,
32+
"default_pl_rules": {
33+
"v4": [ "deny 0.0.0.0/0 le 17" ],
34+
"v6": [
35+
"deny 0::/0 le 59",
36+
"deny 0::/0 ge 65"
37+
]
38+
},
39+
"default_action": "permit",
40+
"drop_community": "123:123",
41+
"prefix_match_tag": "1001"
42+
}
43+
}
44+
}
45+
2846
@patch.dict("sys.modules", swsscommon=swsscommon_module_mock)
29-
def set_del_test(op, args, currect_config, expected_config, update_global_default_action=None):
47+
def set_del_test(op, args, currect_config, expected_config, update_global_default_action=None, update_constant_prefix_match_tag=False):
3048
from bgpcfgd.managers_allow_list import BGPAllowListMgr
3149
set_del_test.push_list_called = False
3250
def push_list(args):
@@ -44,7 +62,7 @@ def push_list(args):
4462
'directory': Directory(),
4563
'cfg_mgr': cfg_mgr,
4664
'tf': TemplateFabric(),
47-
'constants': deepcopy(global_constants),
65+
'constants': deepcopy(global_constants) if not update_constant_prefix_match_tag else deepcopy(global_constants_with_prefix_match_tag)
4866
}
4967

5068
mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
@@ -91,6 +109,39 @@ def test_set_handler_with_community():
91109
' match community COMMUNITY_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020',
92110
]
93111
)
112+
113+
def test_set_handler_with_community_and_prefix_match_tag():
114+
set_del_test(
115+
"SET",
116+
("DEPLOYMENT_ID|5|1010:2020", {
117+
"prefixes_v4": "10.20.30.0/24,30.50.0.0/16",
118+
"prefixes_v6": "fc00:20::/64,fc00:30::/64",
119+
}),
120+
[
121+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V4 permit 65535',
122+
' set community 123:123 additive',
123+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V6 permit 65535',
124+
' set community 123:123 additive'
125+
],
126+
[
127+
'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V4 seq 10 deny 0.0.0.0/0 le 17',
128+
'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V4 seq 20 permit 10.20.30.0/24 le 32',
129+
'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V4 seq 30 permit 30.50.0.0/16 le 32',
130+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V6 seq 10 deny ::/0 le 59',
131+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V6 seq 20 deny ::/0 ge 65',
132+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V6 seq 30 permit fc00:20::/64 le 128',
133+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V6 seq 40 permit fc00:30::/64 le 128',
134+
'bgp community-list standard COMMUNITY_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020 permit 1010:2020',
135+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V4 permit 10',
136+
' match ip address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V4',
137+
' match community COMMUNITY_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020',
138+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V6 permit 10',
139+
' match ipv6 address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020_V6',
140+
' match community COMMUNITY_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_1010:2020',
141+
],
142+
None, True
143+
)
144+
94145
def test_set_handler_with_community_and_permit_action():
95146
set_del_test(
96147
"SET",
@@ -187,6 +238,38 @@ def test_set_handler_no_community():
187238
' match ipv6 address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6',
188239
]
189240
)
241+
242+
def test_set_handler_no_community_and_prefix_match_tag():
243+
set_del_test(
244+
"SET",
245+
("DEPLOYMENT_ID|5", {
246+
"prefixes_v4": "20.20.30.0/24,40.50.0.0/16",
247+
"prefixes_v6": "fc01:20::/64,fc01:30::/64",
248+
}),
249+
[
250+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V4 permit 65535',
251+
' set community 123:123 additive',
252+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V6 permit 65535',
253+
' set community 123:123 additive',
254+
],
255+
[
256+
'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4 seq 10 deny 0.0.0.0/0 le 17',
257+
'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4 seq 20 permit 20.20.30.0/24 le 32',
258+
'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4 seq 30 permit 40.50.0.0/16 le 32',
259+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 10 deny ::/0 le 59',
260+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 20 deny ::/0 ge 65',
261+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 30 permit fc01:20::/64 le 128',
262+
'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 40 permit fc01:30::/64 le 128',
263+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V4 permit 30000',
264+
' match ip address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4',
265+
' set tag 1001',
266+
'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V6 permit 30000',
267+
' match ipv6 address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6',
268+
' set tag 1001',
269+
],
270+
None,True
271+
)
272+
190273
def test_set_handler_no_community_with_permit_action():
191274
set_del_test(
192275
"SET",

0 commit comments

Comments
 (0)