Skip to content

Commit 51f4bf1

Browse files
abdosiqiluo-msft
authored andcommitted
Added Support for BGP allow list feature to have route-map action of setting tag (#10731)
What I did: Added support to create route-map action set tag <user define value> 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 35c9bec commit 51f4bf1

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
@@ -26,8 +26,26 @@
2626
}
2727
}
2828

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

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

0 commit comments

Comments
 (0)