Skip to content

Commit

Permalink
Updates to bgp config and show commands with BGP_INTERNAL_NEIGHBOR ta…
Browse files Browse the repository at this point in the history
  • Loading branch information
judyjoseph committed Nov 13, 2020
1 parent d683bb4 commit ce27b4a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
21 changes: 4 additions & 17 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,28 +381,19 @@ def get_interface_naming_mode():
mode = "default"
return mode

# Get the local BGP ASN from DEVICE_METADATA
def get_local_bgp_asn(config_db):
metadata = config_db.get_table('DEVICE_METADATA')
return metadata['localhost']['bgp_asn']

def _is_neighbor_ipaddress(config_db, ipaddress):
"""Returns True if a neighbor has the IP address <ipaddress>, False if not
"""
entry = config_db.get_entry('BGP_NEIGHBOR', ipaddress)
return True if entry else False

def _get_all_neighbor_ipaddresses(config_db, ignore_local_hosts=False):
def _get_all_neighbor_ipaddresses(config_db):
"""Returns list of strings containing IP addresses of all BGP neighbors
if the flag ignore_local_hosts is set to True, additional check to see if
if the BGP neighbor AS number is same as local BGP AS number, if so ignore that neigbor.
"""
addrs = []
bgp_sessions = config_db.get_table('BGP_NEIGHBOR')
local_as = get_local_bgp_asn(config_db)
for addr, session in bgp_sessions.iteritems():
if not ignore_local_hosts or (ignore_local_hosts and local_as != session['asn']):
addrs.append(addr)
addrs.append(addr)
return addrs

def _get_neighbor_ipaddress_list_by_hostname(config_db, hostname):
Expand Down Expand Up @@ -1797,19 +1788,17 @@ def all(verbose):
"""
log.log_info("'bgp shutdown all' executing...")
namespaces = [DEFAULT_NAMESPACE]
ignore_local_hosts = False

if multi_asic.is_multi_asic():
ns_list = multi_asic.get_all_namespaces()
namespaces = ns_list['front_ns']
ignore_local_hosts = True

# Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the
# namespaces (in case of multi ASIC) and do the sepcified "action" on the BGP neighbor(s)
for namespace in namespaces:
config_db = ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
config_db.connect()
bgp_neighbor_ip_list = _get_all_neighbor_ipaddresses(config_db, ignore_local_hosts)
bgp_neighbor_ip_list = _get_all_neighbor_ipaddresses(config_db)
for ipaddress in bgp_neighbor_ip_list:
_change_bgp_session_status_by_addr(config_db, ipaddress, 'down', verbose)

Expand Down Expand Up @@ -1854,19 +1843,17 @@ def all(verbose):
"""
log.log_info("'bgp startup all' executing...")
namespaces = [DEFAULT_NAMESPACE]
ignore_local_hosts = False

if multi_asic.is_multi_asic():
ns_list = multi_asic.get_all_namespaces()
namespaces = ns_list['front_ns']
ignore_local_hosts = True

# Connect to CONFIG_DB in linux host (in case of single ASIC) or CONFIG_DB in all the
# namespaces (in case of multi ASIC) and do the sepcified "action" on the BGP neighbor(s)
for namespace in namespaces:
config_db = ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
config_db.connect()
bgp_neighbor_ip_list = _get_all_neighbor_ipaddresses(config_db, ignore_local_hosts)
bgp_neighbor_ip_list = _get_all_neighbor_ipaddresses(config_db)
for ipaddress in bgp_neighbor_ip_list:
_change_bgp_session_status_by_addr(config_db, ipaddress, 'up', verbose)

Expand Down
13 changes: 8 additions & 5 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,13 +1315,16 @@ def get_bgp_peer():
"""
config_db = ConfigDBConnector()
config_db.connect()
data = config_db.get_table('BGP_NEIGHBOR')
bgp_peer = {}
bgp_neighbor_tables = ['BGP_NEIGHBOR', 'BGP_INTERNAL_NEIGHBOR']

for table in bgp_neighbor_tables:
data = config_db.get_table(table)
for neighbor_ip in data.keys():
local_addr = data[neighbor_ip]['local_addr']
neighbor_name = data[neighbor_ip]['name']
bgp_peer.setdefault(local_addr, [neighbor_name, neighbor_ip])

for neighbor_ip in data.keys():
local_addr = data[neighbor_ip]['local_addr']
neighbor_name = data[neighbor_ip]['name']
bgp_peer.setdefault(local_addr, [neighbor_name, neighbor_ip])
return bgp_peer

#
Expand Down
8 changes: 4 additions & 4 deletions sonic-utilities-tests/bgp_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
10.0.0.55 4 64012 0 0 0 0 0 never Active ARISTA12T0
10.0.0.57 4 64013 0 0 0 0 0 never Active ARISTA13T0
10.0.0.59 4 64014 0 0 0 0 0 never Active ARISTA14T0
10.0.0.61 4 64015 0 0 0 0 0 never Active ARISTA15T0
10.0.0.63 4 64016 0 0 0 0 0 never Active ARISTA16T0
10.0.0.61 4 64015 0 0 0 0 0 never Active INT_NEIGH0
10.0.0.63 4 64016 0 0 0 0 0 never Active INT_NEIGH1
Total number of neighbors 24
"""
Expand Down Expand Up @@ -75,8 +75,8 @@
fc00::62 4 64009 0 0 0 0 0 never Active ARISTA09T0
fc00::66 4 64010 0 0 0 0 0 never Active ARISTA10T0
fc00::72 4 64013 0 0 0 0 0 never Active ARISTA13T0
fc00::76 4 64014 0 0 0 0 0 never Active ARISTA14T0
fc00::a 4 65200 6665 6671 0 0 0 2d09h38m 6402 ARISTA03T2
fc00::76 4 64014 0 0 0 0 0 never Active INT_NEIGH0
fc00::a 4 65200 6665 6671 0 0 0 2d09h38m 6402 INT_NEIGH1
Total number of neighbors 24
"""
Expand Down
16 changes: 8 additions & 8 deletions sonic-utilities-tests/mock_tables/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,21 @@
"nhopself": "0",
"rrclient": "0"
},
"BGP_NEIGHBOR|10.0.0.61": {
"BGP_INTERNAL_NEIGHBOR|10.0.0.61": {
"asn": "64015",
"holdtime": "10",
"keepalive": "3",
"local_addr": "10.0.0.60",
"name": "ARISTA15T0",
"name": "INT_NEIGH0",
"nhopself": "0",
"rrclient": "0"
},
"BGP_NEIGHBOR|10.0.0.63": {
"BGP_INTERNAL_NEIGHBOR|10.0.0.63": {
"asn": "64016",
"holdtime": "10",
"keepalive": "3",
"local_addr": "10.0.0.62",
"name": "ARISTA16T0",
"name": "INT_NEIGH1",
"nhopself": "0",
"rrclient": "0"
},
Expand Down Expand Up @@ -625,21 +625,21 @@
"nhopself": "0",
"rrclient": "0"
},
"BGP_NEIGHBOR|fc00::76": {
"BGP_INTERNAL_NEIGHBOR|fc00::76": {
"asn": "64014",
"holdtime": "10",
"keepalive": "3",
"local_addr": "fc00::75",
"name": "ARISTA14T0",
"name": "INT_NEIGH0",
"nhopself": "0",
"rrclient": "0"
},
"BGP_NEIGHBOR|fc00::a": {
"BGP_INTERNAL_NEIGHBOR|fc00::a": {
"asn": "65200",
"holdtime": "10",
"keepalive": "3",
"local_addr": "fc00::9",
"name": "ARISTA03T2",
"name": "INT_NEIGH1",
"nhopself": "0",
"rrclient": "0"
},
Expand Down
2 changes: 2 additions & 0 deletions utilities_common/bgp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def get_bgp_neighbors_dict(namespace=multi_asic.DEFAULT_NAMESPACE):
dynamic_neighbors = {}
config_db = multi_asic.connect_config_db_for_ns(namespace)
static_neighbors = get_neighbor_dict_from_table(config_db, 'BGP_NEIGHBOR')
static_internal_neighbors = get_neighbor_dict_from_table(config_db, 'BGP_INTERNAL_NEIGHBOR')
static_neighbors.update(static_internal_neighbors)
bgp_monitors = get_neighbor_dict_from_table(config_db, 'BGP_MONITORS')
static_neighbors.update(bgp_monitors)
dynamic_neighbors = get_dynamic_neighbor_subnet(config_db)
Expand Down

0 comments on commit ce27b4a

Please sign in to comment.