Skip to content

Commit

Permalink
Updated lldpRemManAddrTable to use all the management ip address asso…
Browse files Browse the repository at this point in the history
…ciated with interface. (#201)

Fixes:-
sonic-net/sonic-buildimage#7036
sonic-net/sonic-buildimage#6636

Updated to use prefix of 16 byte instead of 6 byte for Ipv6 Management Address. Confirmed with other Vendor Nos

Fixed lookup() so that get-next work correctly from test-cases.

Updated all files to use new utility API ip2byte_tuple()
  • Loading branch information
abdosi committed Mar 15, 2021
1 parent 093a3c2 commit d81828c
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 86 deletions.
9 changes: 6 additions & 3 deletions src/ax_interface/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ipaddress
import re

from ax_interface import constants
Expand Down Expand Up @@ -98,10 +99,12 @@ def mac_decimals(mac):
"""
return tuple(int(h, 16) for h in mac.split(":"))

def ip2tuple_v4(ip):
def ip2byte_tuple(ip):
"""
>>> ip2tuple_v4("192.168.1.253")
>>> ip2byte_tuple("192.168.1.253")
(192, 168, 1, 253)
>>> ip2byte_tuple("2001:db8::3")
(32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3)
"""
return tuple(int(bs) for bs in str(ip).split('.'))
return tuple(i for i in ipaddress.ip_address(ip).packed)

88 changes: 30 additions & 58 deletions src/sonic_ax_impl/mibs/ieee802_1ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from swsssdk import port_util
from sonic_ax_impl import mibs, logger
from sonic_ax_impl.mibs import Namespace
from ax_interface.util import ip2byte_tuple
from ax_interface import MIBMeta, SubtreeMIBEntry, MIBEntry, MIBUpdater, ValueType


Expand Down Expand Up @@ -315,7 +316,8 @@ def reinit_data(self):
mgmt_ip_sub_oid = None
for mgmt_ip in self.mgmt_ip_str.split(','):
if '.' in mgmt_ip:
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip.split('.')])
mgmt_ip_tuple = ip2byte_tuple(mgmt_ip)
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple)
break
else:
logger.error("Could not find IPv4 address in lldp_loc_man_addr")
Expand Down Expand Up @@ -492,10 +494,8 @@ def __init__(self):
# establish connection to application database.
Namespace.connect_all_dbs(self.db_conn, mibs.APPL_DB)
self.if_range = []
self.mgmt_ips = {}
self.oid_name_map = {}
self.mgmt_oid_name_map = {}
self.mgmt_ip_str = None
self.pubsub = [None] * len(self.db_conn)

def update_rem_if_mgmt(self, if_oid, if_name):
Expand All @@ -509,28 +509,28 @@ def update_rem_if_mgmt(self, if_oid, if_name):
if len(mgmt_ip_str) == 0:
# the peer advertise an emtpy mgmt address
return
time_mark = int(lldp_kvs[b'lldp_rem_time_mark'])
remote_index = int(lldp_kvs[b'lldp_rem_index'])
subtype = self.get_subtype(mgmt_ip_str)
ip_hex = self.get_ip_hex(mgmt_ip_str, subtype)
if subtype == ManAddrConst.man_addr_subtype_ipv4:
addr_subtype_sub_oid = 4
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip_str.split('.')])
elif subtype == ManAddrConst.man_addr_subtype_ipv6:
addr_subtype_sub_oid = 6
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i, 16) if i else 0 for i in mgmt_ip_str.split(':')])
else:
logger.warning("Invalid management IP {}".format(mgmt_ip_str))
return
self.if_range.append((time_mark,
if_oid,
remote_index,
subtype,
*mgmt_ip_sub_oid))

self.mgmt_ips.update({if_name: {"ip_str": mgmt_ip_str,
"addr_subtype": subtype,
"addr_hex": ip_hex}})
mgmt_ip_set=set()
for mgmt_ip in mgmt_ip_str.split(','):
time_mark = int(lldp_kvs[b'lldp_rem_time_mark'])
remote_index = int(lldp_kvs[b'lldp_rem_index'])
subtype = self.get_subtype(mgmt_ip)
if not subtype:
logger.warning("Invalid management IP {}".format(mgmt_ip))
continue
mgmt_ip_tuple = ip2byte_tuple(mgmt_ip)
if mgmt_ip_tuple in mgmt_ip_set:
continue
elif subtype == ManAddrConst.man_addr_subtype_ipv4:
addr_subtype_sub_oid = 4
else:
addr_subtype_sub_oid = 16
mgmt_ip_set.add(mgmt_ip_tuple)
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple)
self.if_range.append((time_mark,
if_oid,
remote_index,
subtype,
*mgmt_ip_sub_oid))
except (KeyError, AttributeError) as e:
logger.warning("Error updating remote mgmt addr: {}".format(e))
return
Expand Down Expand Up @@ -560,7 +560,6 @@ def update_data(self):
self.pubsub[i] = mibs.get_redis_pubsub(self.db_conn[i], self.db_conn[i].APPL_DB, pattern)
self._update_per_namespace_data(self.pubsub[i])


def reinit_data(self):
"""
Subclass reinit data routine.
Expand All @@ -575,7 +574,6 @@ def reinit_data(self):
Namespace.connect_all_dbs(self.db_conn, mibs.APPL_DB)

self.if_range = []
self.mgmt_ips = {}
for if_oid, if_name in self.oid_name_map.items():
self.update_rem_if_mgmt(if_oid, if_name)

Expand All @@ -586,25 +584,9 @@ def get_next(self, sub_id):
return self.if_range[right]

def lookup(self, sub_id, callable):
if len(sub_id) == 0:
return None
sub_id = sub_id[1]
if sub_id not in self.oid_name_map:
return None
if_name = self.oid_name_map[sub_id]
if if_name not in self.mgmt_ips:
# no data for this interface
if sub_id not in self.if_range:
return None
return callable(sub_id, if_name)

def get_ip_hex(self, mgmt_ip_str, subtype):
if subtype == ManAddrConst.man_addr_subtype_ipv4:
hex_ip = " ".join([format(int(i), '02X') for i in mgmt_ip_str.split('.')])
elif subtype == ManAddrConst.man_addr_subtype_ipv6:
hex_ip = " ".join([format(int(i, 16), 'x') if i else "0" for i in mgmt_ip_str.split(':')])
else:
hex_ip = None
return hex_ip
return callable(sub_id)

def get_subtype(self, ip_str):
try:
Expand All @@ -621,24 +603,14 @@ def get_subtype(self, ip_str):
logger.warning("Invalid mgmt IP {}".format(ip_str))
return None

def man_addr_subtype(self, sub_id, if_name):
return self.mgmt_ips[if_name]['addr_subtype']

def man_addr(self, sub_id, if_name):
"""
:param sub_id:
:return: MGMT IP in HEX
"""
return self.mgmt_ips[if_name]['addr_hex']

@staticmethod
def man_addr_if_subtype(sub_id, _): return ManAddrConst.man_addr_if_subtype
def man_addr_if_subtype(sub_id): return ManAddrConst.man_addr_if_subtype

@staticmethod
def man_addr_if_id(sub_id, _): return ManAddrConst.man_addr_if_id
def man_addr_if_id(sub_id): return ManAddrConst.man_addr_if_id

@staticmethod
def man_addr_OID(sub_id, _): return ManAddrConst.man_addr_oid
def man_addr_OID(sub_id): return ManAddrConst.man_addr_oid


class LLDPLocalSystemData(metaclass=MIBMeta, prefix='.1.0.8802.1.1.2.1.3'):
Expand Down
6 changes: 3 additions & 3 deletions src/sonic_ax_impl/mibs/ietf/rfc1213.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sonic_ax_impl.mibs import Namespace
from ax_interface.mib import MIBMeta, ValueType, MIBUpdater, MIBEntry, SubtreeMIBEntry, OverlayAdpaterMIBEntry, OidMIBEntry
from ax_interface.encodings import ObjectIdentifier
from ax_interface.util import mac_decimals, ip2tuple_v4
from ax_interface.util import mac_decimals, ip2byte_tuple
from swsssdk.port_util import get_index_from_str

@unique
Expand Down Expand Up @@ -99,7 +99,7 @@ def _update_arp_info(self, dev, mac, ip):
# if MAC is all zero
#if not any(mac): continue

iptuple = ip2tuple_v4(ip)
iptuple = ip2byte_tuple(ip)

subid = (if_index,) + iptuple
self.arp_dest_map[subid] = machex
Expand Down Expand Up @@ -154,7 +154,7 @@ def update_data(self):
nexthops = ent[b"nexthop"].decode()
for nh in nexthops.split(','):
# TODO: if ipn contains IP range, create more sub_id here
sub_id = ip2tuple_v4(ipn.network_address)
sub_id = ip2byte_tuple(ipn.network_address)
self.route_list.append(sub_id)
self.nexthop_map[sub_id] = ipaddress.ip_address(nh).packed
break # Just need the first nexthop
Expand Down
6 changes: 3 additions & 3 deletions src/sonic_ax_impl/mibs/ietf/rfc4292.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sonic_ax_impl import mibs
from sonic_ax_impl.mibs import Namespace
from ax_interface import MIBMeta, ValueType, MIBUpdater, SubtreeMIBEntry
from ax_interface.util import ip2tuple_v4
from ax_interface.util import ip2byte_tuple
from bisect import bisect_right
from sonic_py_common import multi_asic

Expand Down Expand Up @@ -46,7 +46,7 @@ def update_data(self):

## The nexthop for loopbacks should be all zero
for loip in self.loips:
sub_id = ip2tuple_v4(loip) + (255, 255, 255, 255) + (self.tos,) + (0, 0, 0, 0)
sub_id = ip2byte_tuple(loip) + (255, 255, 255, 255) + (self.tos,) + (0, 0, 0, 0)
self.route_dest_list.append(sub_id)
self.route_dest_map[sub_id] = self.loips[loip].packed

Expand Down Expand Up @@ -85,7 +85,7 @@ def update_data(self):
port_table[ifn][multi_asic.PORT_ROLE] == multi_asic.INTERNAL_PORT):
continue

sub_id = ip2tuple_v4(ipn.network_address) + ip2tuple_v4(ipn.netmask) + (self.tos,) + ip2tuple_v4(nh)
sub_id = ip2byte_tuple(ipn.network_address) + ip2byte_tuple(ipn.netmask) + (self.tos,) + ip2byte_tuple(nh)
self.route_dest_list.append(sub_id)
self.route_dest_map[sub_id] = ipn.network_address.packed

Expand Down
3 changes: 2 additions & 1 deletion src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sonic_ax_impl import mibs
from ax_interface import MIBMeta, ValueType, MIBUpdater, SubtreeMIBEntry
from ax_interface.mib import MIBEntry
from ax_interface.util import ip2byte_tuple
from sonic_ax_impl.mibs import Namespace
import ipaddress

Expand Down Expand Up @@ -45,7 +46,7 @@ def update_data(self):
oid_head = (1, 4)
else:
oid_head = (2, 16)
oid_ip = tuple(i for i in ip.packed)
oid_ip = ip2byte_tuple(neigh_str)

if state.isdigit():
status = 6
Expand Down
10 changes: 5 additions & 5 deletions tests/mock_tables/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet1",
"lldp_rem_man_addr": "10.224.25.100"
"lldp_rem_man_addr": "10.224.25.100,2603:10e2:290:5016::"
},
"LLDP_ENTRY_TABLE:Ethernet4": {
"lldp_rem_port_id_subtype": "5",
Expand All @@ -25,7 +25,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet2",
"lldp_rem_man_addr": "fe80::268a:7ff:fe3f:834c"
"lldp_rem_man_addr": "fe80::268a::7ff:fe3f:834c,10.224.25.101"
},
"LLDP_ENTRY_TABLE:Ethernet8": {
"lldp_rem_port_id_subtype": "5",
Expand Down Expand Up @@ -53,7 +53,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet4",
"lldp_rem_man_addr": "10.224.25.103"
"lldp_rem_man_addr": "fe80:268a::7ff:fe3f:834c"
},
"LLDP_ENTRY_TABLE:Ethernet16": {
"lldp_rem_port_id_subtype": "5",
Expand All @@ -67,7 +67,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet5",
"lldp_rem_man_addr": "10.224.25.104"
"lldp_rem_man_addr": ""
},
"LLDP_ENTRY_TABLE:Ethernet20": {
"lldp_rem_port_id_subtype": "5",
Expand Down Expand Up @@ -333,7 +333,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet24",
"lldp_rem_man_addr": "10.224.25.123"
"lldp_rem_man_addr": "10.224.25.123,2603:10e2:290:5016::"
},
"LLDP_ENTRY_TABLE:Ethernet96": {
"lldp_rem_port_id_subtype": "5",
Expand Down
6 changes: 3 additions & 3 deletions tests/mock_tables/asic0/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"lldp_rem_index": "1",
"lldp_rem_chassis_id": "00:11:22:33:44:55",
"lldp_rem_sys_desc": "I'm a little teapot.",
"lldp_rem_time_mark": "18545",
"lldp_rem_time_mark": "0",
"lldp_rem_sys_cap_enabled": "28 00",
"lldp_rem_port_desc": " ",
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet1",
"lldp_rem_man_addr": "10.224.25.100"
"lldp_rem_man_addr": "10.224.25.123,2603:10e2:290:5016::"
},
"LLDP_ENTRY_TABLE:Ethernet4": {
"lldp_rem_port_id_subtype": "5",
Expand All @@ -25,7 +25,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet2",
"lldp_rem_man_addr": "fe80::268a:7ff:fe3f:834c"
"lldp_rem_man_addr": "fe80::268a::7ff:fe3f:834c,10.224.25.102"
},
"LLDP_LOC_CHASSIS": {
"lldp_loc_chassis_id_subtype": "5",
Expand Down
4 changes: 2 additions & 2 deletions tests/mock_tables/asic1/appl_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet3",
"lldp_rem_man_addr": "fe80::268a:7ff:fe3f:834c"
"lldp_rem_man_addr": "10.224.25.102"
},
"LLDP_ENTRY_TABLE:Ethernet12": {
"lldp_rem_port_id_subtype": "5",
Expand All @@ -25,7 +25,7 @@
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet4",
"lldp_rem_man_addr": "10.224.25.102"
"lldp_rem_man_addr": "fe80:268a::7ff:fe3f:834c"
},
"LLDP_LOC_CHASSIS": {
"lldp_loc_chassis_id_subtype": "5",
Expand Down
19 changes: 19 additions & 0 deletions tests/mock_tables/asic2/appl_db.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
{
"LLDP_ENTRY_TABLE:Ethernet16": {
"lldp_rem_port_id_subtype": "5",
"lldp_rem_sys_cap_supported": "28 00",
"lldp_rem_index": "1",
"lldp_rem_chassis_id": "00:11:22:33:44:55",
"lldp_rem_sys_desc": "I'm a little teapot.",
"lldp_rem_time_mark": "18543",
"lldp_rem_sys_cap_enabled": "28 00",
"lldp_rem_port_desc": " ",
"lldp_rem_chassis_id_subtype": "4",
"lldp_rem_sys_name": "switch13",
"lldp_rem_port_id": "Ethernet5",
"lldp_rem_man_addr": ""
},
"LLDP_LOC_CHASSIS": {
"lldp_loc_chassis_id_subtype": "5",
"lldp_loc_chassis_id": "00:11:22:AB:CD:EF",
Expand All @@ -8,6 +22,11 @@
"lldp_loc_sys_cap_supported": "28 00",
"lldp_loc_man_addr": "10.224.25.36,fe80::ce37:abff:feec:de9c"
},
"PORT_TABLE:Ethernet16": {
"description": "snowflake",
"alias": "etp5",
"speed": 100000
},
"PORT_TABLE:Ethernet-BP16": {
"description": "backplane",
"alias": "etp9",
Expand Down
Loading

0 comments on commit d81828c

Please sign in to comment.