Skip to content

Commit 7b88a42

Browse files
Gfrom2016mssonicbld
authored andcommitted
Prioritize configuration from config_db over constants.yml during BBR status initialization (#19590)
Why I did it There are two places which we can configure the BBR disabled/enabled, one is constants.yml, the other is by config_db. During run time, config_db win. But at the init stage, constants.yml win. This is a wrong logic, constants.yml only win when there is no such config in the config_db. Work item tracking Microsoft ADO (number only): 28302790 How I did it Initialize BBR status from constants.yml only when config_db doesn't have BBR entry. How to verify it Build image and run the following: # bbr status in constants.yml is set to disabled # change the bbr status in config_db to enabled and reload config admin@str2-7050cx3-acs-01:~$ redis-cli -n 4 hset "BGP_BBR|all" "status" "enabled" (integer) 0 admin@str2-7050cx3-acs-01:~$ redis-cli -n 4 hget "BGP_BBR|all" "status" "enabled" admin@str2-7050cx3-acs-01:~$ vtysh -c 'show run' | grep 'allowas' neighbor PEER_V4 allowas-in 1 neighbor PEER_V6 allowas-in 1 admin@str2-7050cx3-acs-01:~$ sudo config save -y Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json admin@str2-7050cx3-acs-01:~$ sudo config reload -y # check bgpcfgd log, bbr default status is enabled 2024 Aug 14 05:42:47.653216 str2-7050cx3-acs-01 INFO bgp#bgpcfgd: BBRMgr::Initialized and enabled from config_db. Default state: 'enabled'
1 parent b075547 commit 7b88a42

File tree

2 files changed

+85
-18
lines changed

2 files changed

+85
-18
lines changed

src/sonic-bgpcfgd/bgpcfgd/managers_bbr.py

+56-15
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,38 @@ def del_handler(self, key):
4545

4646
def __init(self):
4747
""" Initialize BBRMgr. Extracted from constructor """
48-
if not 'bgp' in self.constants:
49-
log_err("BBRMgr::Disabled: 'bgp' key is not found in constants")
50-
return
51-
if 'bbr' in self.constants['bgp'] \
52-
and 'enabled' in self.constants['bgp']['bbr'] \
53-
and self.constants['bgp']['bbr']['enabled']:
48+
# Check BGP_BBR table from config_db first
49+
bbr_status_from_config_db = self.get_bbr_status_from_config_db()
50+
51+
if bbr_status_from_config_db is None:
52+
if not 'bgp' in self.constants:
53+
log_err("BBRMgr::Disabled: 'bgp' key is not found in constants")
54+
return
55+
if 'bbr' in self.constants['bgp'] \
56+
and 'enabled' in self.constants['bgp']['bbr'] \
57+
and self.constants['bgp']['bbr']['enabled']:
58+
self.bbr_enabled_pgs = self.__read_pgs()
59+
if self.bbr_enabled_pgs:
60+
self.enabled = True
61+
if 'default_state' in self.constants['bgp']['bbr'] \
62+
and self.constants['bgp']['bbr']['default_state'] == 'enabled':
63+
default_status = "enabled"
64+
else:
65+
default_status = "disabled"
66+
self.directory.put(self.db_name, self.table_name, 'status', default_status)
67+
log_info("BBRMgr::Initialized and enabled from constants. Default state: '%s'" % default_status)
68+
else:
69+
log_info("BBRMgr::Disabled: no BBR enabled peers")
70+
else:
71+
log_info("BBRMgr::Disabled: no bgp.bbr.enabled in the constants")
72+
else:
5473
self.bbr_enabled_pgs = self.__read_pgs()
5574
if self.bbr_enabled_pgs:
5675
self.enabled = True
57-
if 'default_state' in self.constants['bgp']['bbr'] \
58-
and self.constants['bgp']['bbr']['default_state'] == 'enabled':
59-
default_status = "enabled"
60-
else:
61-
default_status = "disabled"
62-
self.directory.put(self.db_name, self.table_name, 'status', default_status)
63-
log_info("BBRMgr::Initialized and enabled. Default state: '%s'" % default_status)
76+
self.directory.put(self.db_name, self.table_name, 'status', bbr_status_from_config_db)
77+
log_info("BBRMgr::Initialized and enabled from config_db. Default state: '%s'" % bbr_status_from_config_db)
6478
else:
6579
log_info("BBRMgr::Disabled: no BBR enabled peers")
66-
else:
67-
log_info("BBRMgr::Disabled: no bgp.bbr.enabled in the constants")
6880

6981
def __read_pgs(self):
7082
"""
@@ -82,6 +94,35 @@ def __read_pgs(self):
8294
res[pg_name] = pg_afs
8395
return res
8496

97+
def get_bbr_status_from_config_db(self):
98+
"""
99+
Read BBR status from CONFIG_DB
100+
:return: BBR status from CONFIG_DB or None if not found
101+
"""
102+
try:
103+
config_db = swsscommon.ConfigDBConnector()
104+
if config_db is None:
105+
log_info("BBRMgr::Failed to connect to CONFIG_DB, get BBR default state from constants.yml")
106+
return None
107+
config_db.connect()
108+
except Exception as e:
109+
log_info("BBRMgr::Failed to connect to CONFIG_DB with exception %s, get BBR default state from constants.yml" % str(e))
110+
return None
111+
112+
try:
113+
bbr_table_data = config_db.get_table(self.table_name)
114+
if bbr_table_data and 'all' in bbr_table_data and 'status' in bbr_table_data["all"]:
115+
if bbr_table_data["all"]["status"] == "enabled":
116+
return "enabled"
117+
else:
118+
return "disabled"
119+
else:
120+
log_info("BBRMgr::BBR status is not found in CONFIG_DB, get BBR default state from constants.yml")
121+
return None
122+
except Exception as e:
123+
log_info("BBRMgr::Failed to read BBR status from CONFIG_DB with exception %s, get BBR default state from constants.yml" % str(e))
124+
return None
125+
85126
def __set_validation(self, key, data):
86127
""" Validate set-command arguments
87128
:param key: key of 'set' command

src/sonic-bgpcfgd/tests/test_bbr.py

+29-3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def __init_common(constants,
111111
'tf': TemplateFabric(),
112112
'constants': constants,
113113
}
114+
114115
m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR")
115116
m._BBRMgr__init()
116117
assert m.bbr_enabled_pgs == expected_bbr_enabled_pgs
@@ -156,7 +157,7 @@ def test___init_6():
156157
"bbr": expected_bbr_entries,
157158
}
158159
}
159-
__init_common(constants, "BBRMgr::Initialized and enabled. Default state: 'disabled'", None, expected_bbr_entries, "disabled")
160+
__init_common(constants, "BBRMgr::Initialized and enabled from constants. Default state: 'disabled'", None, expected_bbr_entries, "disabled")
160161

161162
def test___init_7():
162163
expected_bbr_entries = {
@@ -170,7 +171,7 @@ def test___init_7():
170171
"bbr": expected_bbr_entries,
171172
}
172173
}
173-
__init_common(constants, "BBRMgr::Initialized and enabled. Default state: 'disabled'", None, expected_bbr_entries, "disabled")
174+
__init_common(constants, "BBRMgr::Initialized and enabled from constants. Default state: 'disabled'", None, expected_bbr_entries, "disabled")
174175

175176
def test___init_8():
176177
expected_bbr_entries = {
@@ -184,7 +185,32 @@ def test___init_8():
184185
"bbr": expected_bbr_entries,
185186
}
186187
}
187-
__init_common(constants, "BBRMgr::Initialized and enabled. Default state: 'enabled'", None, expected_bbr_entries, "enabled")
188+
__init_common(constants, "BBRMgr::Initialized and enabled from constants. Default state: 'enabled'", None, expected_bbr_entries, "enabled")
189+
190+
@patch('bgpcfgd.managers_bbr.BBRMgr.get_bbr_status_from_config_db', return_value='disabled')
191+
def test___init_with_config_db_overwirte_constants(mocked_get_bbr_status_from_config_db):
192+
expected_bbr_entries = {
193+
"PEER_V4": ["ipv4"],
194+
"PEER_V6": ["ipv6"],
195+
}
196+
constants = deepcopy(global_constants)
197+
constants["bgp"]["bbr"] = {"enabled": True, "default_state": "enabled"}
198+
constants["bgp"]["peers"] = {
199+
"general": {
200+
"bbr": expected_bbr_entries,
201+
}
202+
}
203+
204+
# BBR status from config_db should be prioritized over constants
205+
__init_common(constants, "BBRMgr::Initialized and enabled from config_db. Default state: 'disabled'", None, expected_bbr_entries, "disabled")
206+
207+
@patch('bgpcfgd.managers_bbr.BBRMgr.get_bbr_status_from_config_db', return_value='enabled')
208+
def test___init_with_config_db_no_peers(mocked_get_bbr_status_from_config_db):
209+
210+
constants = deepcopy(global_constants)
211+
constants["bgp"]["bbr"] = {"enabled": True}
212+
213+
__init_common(constants, "BBRMgr::Disabled: no BBR enabled peers", None, {}, "disabled")
188214

189215
@patch('bgpcfgd.managers_bbr.log_info')
190216
def read_pgs_common(constants, expected_log_info, expected_bbr_enabled_pgs, mocked_log_info):

0 commit comments

Comments
 (0)