Skip to content

Commit 5e96ff3

Browse files
committed
fix some known issues
1 parent 3ffcab7 commit 5e96ff3

File tree

1 file changed

+49
-22
lines changed
  • src/sonic-bgpcfgd/staticroutebfd

1 file changed

+49
-22
lines changed

src/sonic-bgpcfgd/staticroutebfd/main.py

+49-22
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def set_bfd_session_into_appl_db(self, key, data):
188188
log_debug("set bfd session to appl_db, key %s, data %s"%(key, str(data)))
189189

190190
def del_bfd_session_from_appl_db(self, key):
191-
self.appl_db.delete("BFD_SESSION_TABLE:{}".format(key))
191+
self.bfd_appl_tbl.delete(key)
192192

193193
def interface_set_handler(self, key, data):
194194
valid, is_ipv4, if_name, ip = self.get_ip_from_key(key)
@@ -202,7 +202,7 @@ def interface_set_handler(self, key, data):
202202
self.set_local_db(LOCAL_INTERFACE_TABLE, if_name, value)
203203
return True
204204

205-
def interface_del_handler(self, key, data):
205+
def interface_del_handler(self, key):
206206
valid, is_ipv4, if_name, ip = self.get_ip_from_key(key)
207207
if not valid:
208208
return True
@@ -304,6 +304,28 @@ def check_skip(self, bfd_enable):
304304
return False
305305
return True
306306

307+
def refresh_active_nh(self, route_cfg_key):
308+
data = self.get_local_db(LOCAL_CONFIG_TABLE, route_cfg_key)
309+
310+
arg_list = lambda v: v.split(',') if len(v.strip()) != 0 else None
311+
nh_list = arg_list(data['nexthop']) if 'nexthop' in data else None
312+
nh_vrf_list = arg_list(data['nexthop-vrf']) if 'nexthop-vrf' in data else None
313+
314+
for index in range(len(nh_list)):
315+
nh_ip = nh_list[index]
316+
nh_vrf = nh_vrf_list[index]
317+
nh_key = nh_vrf + "|" + nh_ip
318+
bfd_key = nh_vrf + ":default:" + nh_ip
319+
320+
bfd_session = self.get_local_db(LOCAL_BFD_TABLE, bfd_key)
321+
if len(bfd_session) == 0:
322+
continue
323+
if "state" in bfd_session and bfd_session["state"].upper() == "UP":
324+
self.append_to_sr_table_entry(route_cfg_key, (nh_vrf, nh_ip))
325+
326+
new_config = self.reconstruct_static_route_config(data, self.get_local_db(LOCAL_SR_TABLE, route_cfg_key))
327+
self.set_static_route_into_appl_db(route_cfg_key.replace("|", ":"), new_config)
328+
307329
def static_route_set_handler(self, key, data):
308330
global ROUTE_ADVERTISE_ENABLE_TAG
309331
global ROUTE_ADVERTISE_DISABLE_TAG
@@ -313,7 +335,7 @@ def static_route_set_handler(self, key, data):
313335
return True
314336

315337
valid, vrf, ip_prefix = static_route_split_key(key)
316-
local_db_key = vrf + "|" + ip_prefix
338+
route_cfg_key = vrf + "|" + ip_prefix
317339
if not valid:
318340
return True
319341

@@ -343,7 +365,7 @@ def static_route_set_handler(self, key, data):
343365
return True
344366

345367

346-
data_exist = self.get_local_db(LOCAL_CONFIG_TABLE, local_db_key)
368+
data_exist = self.get_local_db(LOCAL_CONFIG_TABLE, route_cfg_key)
347369
if data_exist:
348370
# route with the prefix already exist, remove the deleted nexthops
349371
nh_list_exist = arg_list(data_exist['nexthop']) if 'nexthop' in data else None
@@ -361,13 +383,14 @@ def static_route_set_handler(self, key, data):
361383
nh_vrf = nh[0]
362384
nh_ip = nh[2]
363385
nh_key = nh_vrf + "|" + nh_ip
364-
self.remove_from_nh_table_entry(nh_key, vrf + "|" + ip_prefix)
386+
self.remove_from_sr_table_entry(route_cfg_key, (nh_vrf, nh_ip))
387+
self.remove_from_nh_table_entry(nh_key, route_cfg_key)
365388
if len(self.get_local_db(LOCAL_NEXTHOP_TABLE, nh_key)) == 0:
366389
bfd_key = nh_vrf + ":default:" + nh_ip
367390
self.remove_from_local_db(LOCAL_BFD_TABLE, bfd_key)
368391
self.del_bfd_session_from_appl_db(bfd_key)
369392

370-
self.set_local_db(LOCAL_CONFIG_TABLE, local_db_key, data)
393+
self.set_local_db(LOCAL_CONFIG_TABLE, route_cfg_key, data)
371394
for index in range(len(nh_list)):
372395
nh_ip = nh_list[index]
373396
intf = intf_list[index]
@@ -383,7 +406,7 @@ def static_route_set_handler(self, key, data):
383406
if len(self.get_local_db(LOCAL_NEXTHOP_TABLE, nh_key)) == 0 and len(bfd_session) == 0:
384407
valid, local_addr = self.find_interface_ip(intf, nh_ip)
385408
if not valid:
386-
log_warn("cannot find ip for interface: ", intf)
409+
log_warn("cannot find ip for interface: %s" %intf)
387410
continue
388411
bfd_entry_cfg = self.BFD_DEFAULT_CFG.copy()
389412
bfd_entry_cfg["local_addr"] = local_addr
@@ -393,21 +416,23 @@ def static_route_set_handler(self, key, data):
393416

394417
self.append_to_nh_table_entry(nh_key, vrf + "|" + ip_prefix)
395418

419+
self.refresh_active_nh(route_cfg_key)
420+
396421
return True
397422

398423
def static_route_del_handler(self, key):
399-
valid, vrf, ip_prefix = self.static_route_split_key(key)
424+
valid, vrf, ip_prefix = static_route_split_key(key)
400425
if not valid:
401426
return True
402-
local_db_key = vrf + "|" + ip_prefix
427+
route_cfg_key = vrf + "|" + ip_prefix
403428

404429
valid, is_ipv4, ip = check_ip(ip_prefix)
405430
if not valid:
406431
return True
407432

408-
data = self.get_local_db(LOCAL_CONFIG_TABLE, local_db_key)
433+
data = self.get_local_db(LOCAL_CONFIG_TABLE, route_cfg_key)
409434
if len(data) == 0:
410-
# this route is not handled by StaticRouteBfd, skip
435+
# this route is not handled by StaticRouteBfd, skip
411436
return True
412437

413438
arg_list = lambda v: v.split(',') if len(v.strip()) != 0 else None
@@ -417,20 +442,23 @@ def static_route_del_handler(self, key):
417442
nh_ip = nh_list[index]
418443
nh_vrf = nh_vrf_list[index]
419444
nh_key = nh_vrf + "|" + nh_ip
420-
self.remove_from_nh_table_entry(nh_key, vrf + "|" + ip_prefix)
445+
self.remove_from_nh_table_entry(nh_key, route_cfg_key)
421446

422447
if len(self.get_local_db(LOCAL_NEXTHOP_TABLE, nh_key)) == 0:
423448
bfd_key = nh_vrf + ":default:" + nh_ip
424449
self.remove_from_local_db(LOCAL_BFD_TABLE, bfd_key)
425450
self.del_bfd_session_from_appl_db(bfd_key)
426451

452+
self.del_static_route_from_appl_db(route_cfg_key.replace("|", ":"))
453+
self.remove_from_local_db(LOCAL_INTERFACE_TABLE, route_cfg_key)
454+
427455
return True
428456

429457
def interface_callback(self, key, op, data):
430458
if op == swsscommon.SET_COMMAND:
431459
self.interface_set_handler(key, data)
432460
elif op == swsscommon.DEL_COMMAND:
433-
self.interface_del_handler(key, data)
461+
self.interface_del_handler(key)
434462
else:
435463
log_err("Invalid operation '%s' for key '%s'" % (op, key))
436464

@@ -520,8 +548,10 @@ def reconstruct_static_route_config(self, original_config, reachable_nexthops):
520548

521549
return new_config
522550

551+
523552
def bfd_state_set_handler(self, key, data):
524-
#key are diff in state db and appl_db
553+
#key are diff in state db and appl_db,
554+
#intf is always default for multihop bfd
525555
vrf, intf, peer_ip = self.bfd_state_split_key(key)
526556
bfd_key = vrf + ":" + intf + ":" + peer_ip
527557

@@ -534,6 +564,8 @@ def bfd_state_set_handler(self, key, data):
534564
state = data['state'] if 'state' in data else "DOWN"
535565
log_info("bfd seesion %s state %s" %(bfd_key, state))
536566

567+
self.local_db[LOCAL_BFD_TABLE][bfd_key]["state"] = state
568+
537569
if state.upper() == "UP":
538570
for prefix in self.get_local_db(LOCAL_NEXTHOP_TABLE, nh_key):
539571
sr_key = prefix
@@ -556,22 +588,17 @@ def bfd_state_set_handler(self, key, data):
556588
self.set_static_route_into_appl_db(sr_key.replace("|", ":"), new_config)
557589

558590

559-
def bfd_state_del_handler(self, key, data):
591+
def bfd_state_del_handler(self, key):
560592
vrf, intf, peer_ip = self.bfd_state_split_key(key)
561593
bfd_key = vrf + ":" + intf + ":" + peer_ip
562594

563-
#check if the BFD session is in local table
564-
bfd_session = self.get_local_db(LOCAL_BFD_TABLE, bfd_key)
565-
if len(bfd_session) == 0:
566-
return True
567-
568595
nh_key = vrf + "|" + peer_ip
569596

570597
for prefix in self.get_local_db(LOCAL_NEXTHOP_TABLE, nh_key):
571598
sr_key = prefix
572599
config_key = prefix
573600
self.remove_from_sr_table_entry(sr_key, (vrf, peer_ip))
574-
if len(self.get_local_db(LOCAL_SR_TABLE, bfd_key)) == 0:
601+
if len(self.get_local_db(LOCAL_SR_TABLE, sr_key)) == 0:
575602
self.del_static_route_from_appl_db(sr_key.replace("|", ":"))
576603
else:
577604
config_data = self.get_local_db(LOCAL_CONFIG_TABLE, config_key)
@@ -582,7 +609,7 @@ def bfd_state_callback(self, key, op, data):
582609
if op == swsscommon.SET_COMMAND:
583610
self.bfd_state_set_handler(key, data)
584611
elif op == swsscommon.DEL_COMMAND:
585-
self.bfd_state_route_del_handler(key)
612+
self.bfd_state_del_handler(key)
586613
else:
587614
log_err("Invalid operation '%s' for key '%s'" % (op, key))
588615

0 commit comments

Comments
 (0)