Skip to content

Commit 292024a

Browse files
authored
Updated lldpRemManAddrTable to use all the management ip address associated with interface. (sonic-net#201)
Fixes:- sonic-net#7036 sonic-net#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()
1 parent 9b83459 commit 292024a

File tree

11 files changed

+251
-86
lines changed

11 files changed

+251
-86
lines changed

src/ax_interface/util.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ipaddress
12
import re
23

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

101-
def ip2tuple_v4(ip):
102+
def ip2byte_tuple(ip):
102103
"""
103-
>>> ip2tuple_v4("192.168.1.253")
104+
>>> ip2byte_tuple("192.168.1.253")
104105
(192, 168, 1, 253)
106+
>>> ip2byte_tuple("2001:db8::3")
107+
(32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3)
105108
"""
106-
return tuple(int(bs) for bs in str(ip).split('.'))
109+
return tuple(i for i in ipaddress.ip_address(ip).packed)
107110

src/sonic_ax_impl/mibs/ieee802_1ab.py

+30-58
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from swsssdk import port_util
99
from sonic_ax_impl import mibs, logger
1010
from sonic_ax_impl.mibs import Namespace
11+
from ax_interface.util import ip2byte_tuple
1112
from ax_interface import MIBMeta, SubtreeMIBEntry, MIBEntry, MIBUpdater, ValueType
1213

1314

@@ -317,7 +318,8 @@ def reinit_data(self):
317318
mgmt_ip_sub_oid = None
318319
for mgmt_ip in self.mgmt_ip_str.split(','):
319320
if '.' in mgmt_ip:
320-
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip.split('.')])
321+
mgmt_ip_tuple = ip2byte_tuple(mgmt_ip)
322+
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple)
321323
break
322324
else:
323325
logger.error("Could not find IPv4 address in lldp_loc_man_addr")
@@ -494,10 +496,8 @@ def __init__(self):
494496
# establish connection to application database.
495497
Namespace.connect_all_dbs(self.db_conn, mibs.APPL_DB)
496498
self.if_range = []
497-
self.mgmt_ips = {}
498499
self.oid_name_map = {}
499500
self.mgmt_oid_name_map = {}
500-
self.mgmt_ip_str = None
501501
self.pubsub = [None] * len(self.db_conn)
502502

503503
def update_rem_if_mgmt(self, if_oid, if_name):
@@ -511,28 +511,28 @@ def update_rem_if_mgmt(self, if_oid, if_name):
511511
if len(mgmt_ip_str) == 0:
512512
# the peer advertise an emtpy mgmt address
513513
return
514-
time_mark = int(lldp_kvs['lldp_rem_time_mark'])
515-
remote_index = int(lldp_kvs['lldp_rem_index'])
516-
subtype = self.get_subtype(mgmt_ip_str)
517-
ip_hex = self.get_ip_hex(mgmt_ip_str, subtype)
518-
if subtype == ManAddrConst.man_addr_subtype_ipv4:
519-
addr_subtype_sub_oid = 4
520-
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip_str.split('.')])
521-
elif subtype == ManAddrConst.man_addr_subtype_ipv6:
522-
addr_subtype_sub_oid = 6
523-
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i, 16) if i else 0 for i in mgmt_ip_str.split(':')])
524-
else:
525-
logger.warning("Invalid management IP {}".format(mgmt_ip_str))
526-
return
527-
self.if_range.append((time_mark,
528-
if_oid,
529-
remote_index,
530-
subtype,
531-
*mgmt_ip_sub_oid))
532-
533-
self.mgmt_ips.update({if_name: {"ip_str": mgmt_ip_str,
534-
"addr_subtype": subtype,
535-
"addr_hex": ip_hex}})
514+
mgmt_ip_set=set()
515+
for mgmt_ip in mgmt_ip_str.split(','):
516+
time_mark = int(lldp_kvs['lldp_rem_time_mark'])
517+
remote_index = int(lldp_kvs['lldp_rem_index'])
518+
subtype = self.get_subtype(mgmt_ip)
519+
if not subtype:
520+
logger.warning("Invalid management IP {}".format(mgmt_ip))
521+
continue
522+
mgmt_ip_tuple = ip2byte_tuple(mgmt_ip)
523+
if mgmt_ip_tuple in mgmt_ip_set:
524+
continue
525+
elif subtype == ManAddrConst.man_addr_subtype_ipv4:
526+
addr_subtype_sub_oid = 4
527+
else:
528+
addr_subtype_sub_oid = 16
529+
mgmt_ip_set.add(mgmt_ip_tuple)
530+
mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple)
531+
self.if_range.append((time_mark,
532+
if_oid,
533+
remote_index,
534+
subtype,
535+
*mgmt_ip_sub_oid))
536536
except (KeyError, AttributeError) as e:
537537
logger.warning("Error updating remote mgmt addr: {}".format(e))
538538
return
@@ -562,7 +562,6 @@ def update_data(self):
562562
self.pubsub[i] = mibs.get_redis_pubsub(self.db_conn[i], self.db_conn[i].APPL_DB, pattern)
563563
self._update_per_namespace_data(self.pubsub[i])
564564

565-
566565
def reinit_data(self):
567566
"""
568567
Subclass reinit data routine.
@@ -577,7 +576,6 @@ def reinit_data(self):
577576
Namespace.connect_all_dbs(self.db_conn, mibs.APPL_DB)
578577

579578
self.if_range = []
580-
self.mgmt_ips = {}
581579
for if_oid, if_name in self.oid_name_map.items():
582580
self.update_rem_if_mgmt(if_oid, if_name)
583581

@@ -588,25 +586,9 @@ def get_next(self, sub_id):
588586
return self.if_range[right]
589587

590588
def lookup(self, sub_id, callable):
591-
if len(sub_id) == 0:
592-
return None
593-
sub_id = sub_id[1]
594-
if sub_id not in self.oid_name_map:
595-
return None
596-
if_name = self.oid_name_map[sub_id]
597-
if if_name not in self.mgmt_ips:
598-
# no data for this interface
589+
if sub_id not in self.if_range:
599590
return None
600-
return callable(sub_id, if_name)
601-
602-
def get_ip_hex(self, mgmt_ip_str, subtype):
603-
if subtype == ManAddrConst.man_addr_subtype_ipv4:
604-
hex_ip = " ".join([format(int(i), '02X') for i in mgmt_ip_str.split('.')])
605-
elif subtype == ManAddrConst.man_addr_subtype_ipv6:
606-
hex_ip = " ".join([format(int(i, 16), 'x') if i else "0" for i in mgmt_ip_str.split(':')])
607-
else:
608-
hex_ip = None
609-
return hex_ip
591+
return callable(sub_id)
610592

611593
def get_subtype(self, ip_str):
612594
try:
@@ -623,24 +605,14 @@ def get_subtype(self, ip_str):
623605
logger.warning("Invalid mgmt IP {}".format(ip_str))
624606
return None
625607

626-
def man_addr_subtype(self, sub_id, if_name):
627-
return self.mgmt_ips[if_name]['addr_subtype']
628-
629-
def man_addr(self, sub_id, if_name):
630-
"""
631-
:param sub_id:
632-
:return: MGMT IP in HEX
633-
"""
634-
return self.mgmt_ips[if_name]['addr_hex']
635-
636608
@staticmethod
637-
def man_addr_if_subtype(sub_id, _): return ManAddrConst.man_addr_if_subtype
609+
def man_addr_if_subtype(sub_id): return ManAddrConst.man_addr_if_subtype
638610

639611
@staticmethod
640-
def man_addr_if_id(sub_id, _): return ManAddrConst.man_addr_if_id
612+
def man_addr_if_id(sub_id): return ManAddrConst.man_addr_if_id
641613

642614
@staticmethod
643-
def man_addr_OID(sub_id, _): return ManAddrConst.man_addr_oid
615+
def man_addr_OID(sub_id): return ManAddrConst.man_addr_oid
644616

645617

646618
class LLDPLocalSystemData(metaclass=MIBMeta, prefix='.1.0.8802.1.1.2.1.3'):

src/sonic_ax_impl/mibs/ietf/rfc1213.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from sonic_ax_impl.mibs import Namespace
99
from ax_interface.mib import MIBMeta, ValueType, MIBUpdater, MIBEntry, SubtreeMIBEntry, OverlayAdpaterMIBEntry, OidMIBEntry
1010
from ax_interface.encodings import ObjectIdentifier
11-
from ax_interface.util import mac_decimals, ip2tuple_v4
11+
from ax_interface.util import mac_decimals, ip2byte_tuple
1212

1313
@unique
1414
class DbTables(int, Enum):
@@ -99,7 +99,7 @@ def _update_arp_info(self, dev, mac, ip):
9999
# if MAC is all zero
100100
#if not any(mac): continue
101101

102-
iptuple = ip2tuple_v4(ip)
102+
iptuple = ip2byte_tuple(ip)
103103

104104
subid = (if_index,) + iptuple
105105
self.arp_dest_map[subid] = machex
@@ -154,7 +154,7 @@ def update_data(self):
154154
nexthops = ent["nexthop"]
155155
for nh in nexthops.split(','):
156156
# TODO: if ipn contains IP range, create more sub_id here
157-
sub_id = ip2tuple_v4(ipn.network_address)
157+
sub_id = ip2byte_tuple(ipn.network_address)
158158
self.route_list.append(sub_id)
159159
self.nexthop_map[sub_id] = ipaddress.ip_address(nh).packed
160160
break # Just need the first nexthop

src/sonic_ax_impl/mibs/ietf/rfc4292.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sonic_ax_impl import mibs
44
from sonic_ax_impl.mibs import Namespace
55
from ax_interface import MIBMeta, ValueType, MIBUpdater, SubtreeMIBEntry
6-
from ax_interface.util import ip2tuple_v4
6+
from ax_interface.util import ip2byte_tuple
77
from bisect import bisect_right
88
from sonic_py_common import multi_asic
99

@@ -46,7 +46,7 @@ def update_data(self):
4646

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

@@ -84,7 +84,7 @@ def update_data(self):
8484
port_table[ifn][multi_asic.PORT_ROLE] == multi_asic.INTERNAL_PORT):
8585
continue
8686

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

src/sonic_ax_impl/mibs/vendor/cisco/bgp4.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from bisect import bisect_right
22
from sonic_ax_impl import mibs
33
from ax_interface import MIBMeta, ValueType, MIBUpdater, SubtreeMIBEntry
4+
from ax_interface.util import ip2byte_tuple
45
from sonic_ax_impl.mibs import Namespace
56
import ipaddress
67

@@ -43,7 +44,7 @@ def update_data(self):
4344
oid_head = (1, 4)
4445
else:
4546
oid_head = (2, 16)
46-
oid_ip = tuple(i for i in ip.packed)
47+
oid_ip = ip2byte_tuple(neigh_str)
4748

4849
if state.isdigit():
4950
status = 6

tests/mock_tables/appl_db.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"lldp_rem_chassis_id_subtype": "4",
1212
"lldp_rem_sys_name": "switch13",
1313
"lldp_rem_port_id": "Ethernet1",
14-
"lldp_rem_man_addr": "10.224.25.100"
14+
"lldp_rem_man_addr": "10.224.25.100,2603:10e2:290:5016::"
1515
},
1616
"LLDP_ENTRY_TABLE:Ethernet4": {
1717
"lldp_rem_port_id_subtype": "5",
@@ -25,7 +25,7 @@
2525
"lldp_rem_chassis_id_subtype": "4",
2626
"lldp_rem_sys_name": "switch13",
2727
"lldp_rem_port_id": "Ethernet2",
28-
"lldp_rem_man_addr": "fe80::268a:7ff:fe3f:834c"
28+
"lldp_rem_man_addr": "fe80::268a::7ff:fe3f:834c,10.224.25.101"
2929
},
3030
"LLDP_ENTRY_TABLE:Ethernet8": {
3131
"lldp_rem_port_id_subtype": "5",
@@ -53,7 +53,7 @@
5353
"lldp_rem_chassis_id_subtype": "4",
5454
"lldp_rem_sys_name": "switch13",
5555
"lldp_rem_port_id": "Ethernet4",
56-
"lldp_rem_man_addr": "10.224.25.103"
56+
"lldp_rem_man_addr": "fe80:268a::7ff:fe3f:834c"
5757
},
5858
"LLDP_ENTRY_TABLE:Ethernet16": {
5959
"lldp_rem_port_id_subtype": "5",
@@ -67,7 +67,7 @@
6767
"lldp_rem_chassis_id_subtype": "4",
6868
"lldp_rem_sys_name": "switch13",
6969
"lldp_rem_port_id": "Ethernet5",
70-
"lldp_rem_man_addr": "10.224.25.104"
70+
"lldp_rem_man_addr": ""
7171
},
7272
"LLDP_ENTRY_TABLE:Ethernet20": {
7373
"lldp_rem_port_id_subtype": "5",
@@ -333,7 +333,7 @@
333333
"lldp_rem_chassis_id_subtype": "4",
334334
"lldp_rem_sys_name": "switch13",
335335
"lldp_rem_port_id": "Ethernet24",
336-
"lldp_rem_man_addr": "10.224.25.123"
336+
"lldp_rem_man_addr": "10.224.25.123,2603:10e2:290:5016::"
337337
},
338338
"LLDP_ENTRY_TABLE:Ethernet96": {
339339
"lldp_rem_port_id_subtype": "5",

tests/mock_tables/asic0/appl_db.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
"lldp_rem_index": "1",
66
"lldp_rem_chassis_id": "00:11:22:33:44:55",
77
"lldp_rem_sys_desc": "I'm a little teapot.",
8-
"lldp_rem_time_mark": "18545",
8+
"lldp_rem_time_mark": "0",
99
"lldp_rem_sys_cap_enabled": "28 00",
1010
"lldp_rem_port_desc": " ",
1111
"lldp_rem_chassis_id_subtype": "4",
1212
"lldp_rem_sys_name": "switch13",
1313
"lldp_rem_port_id": "Ethernet1",
14-
"lldp_rem_man_addr": "10.224.25.100"
14+
"lldp_rem_man_addr": "10.224.25.123,2603:10e2:290:5016::"
1515
},
1616
"LLDP_ENTRY_TABLE:Ethernet4": {
1717
"lldp_rem_port_id_subtype": "5",
@@ -25,7 +25,7 @@
2525
"lldp_rem_chassis_id_subtype": "4",
2626
"lldp_rem_sys_name": "switch13",
2727
"lldp_rem_port_id": "Ethernet2",
28-
"lldp_rem_man_addr": "fe80::268a:7ff:fe3f:834c"
28+
"lldp_rem_man_addr": "fe80::268a::7ff:fe3f:834c,10.224.25.102"
2929
},
3030
"LLDP_LOC_CHASSIS": {
3131
"lldp_loc_chassis_id_subtype": "5",

tests/mock_tables/asic1/appl_db.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"lldp_rem_chassis_id_subtype": "4",
1212
"lldp_rem_sys_name": "switch13",
1313
"lldp_rem_port_id": "Ethernet3",
14-
"lldp_rem_man_addr": "fe80::268a:7ff:fe3f:834c"
14+
"lldp_rem_man_addr": "10.224.25.102"
1515
},
1616
"LLDP_ENTRY_TABLE:Ethernet12": {
1717
"lldp_rem_port_id_subtype": "5",
@@ -25,7 +25,7 @@
2525
"lldp_rem_chassis_id_subtype": "4",
2626
"lldp_rem_sys_name": "switch13",
2727
"lldp_rem_port_id": "Ethernet4",
28-
"lldp_rem_man_addr": "10.224.25.102"
28+
"lldp_rem_man_addr": "fe80:268a::7ff:fe3f:834c"
2929
},
3030
"LLDP_LOC_CHASSIS": {
3131
"lldp_loc_chassis_id_subtype": "5",

tests/mock_tables/asic2/appl_db.json

+19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
{
2+
"LLDP_ENTRY_TABLE:Ethernet16": {
3+
"lldp_rem_port_id_subtype": "5",
4+
"lldp_rem_sys_cap_supported": "28 00",
5+
"lldp_rem_index": "1",
6+
"lldp_rem_chassis_id": "00:11:22:33:44:55",
7+
"lldp_rem_sys_desc": "I'm a little teapot.",
8+
"lldp_rem_time_mark": "18543",
9+
"lldp_rem_sys_cap_enabled": "28 00",
10+
"lldp_rem_port_desc": " ",
11+
"lldp_rem_chassis_id_subtype": "4",
12+
"lldp_rem_sys_name": "switch13",
13+
"lldp_rem_port_id": "Ethernet5",
14+
"lldp_rem_man_addr": ""
15+
},
216
"LLDP_LOC_CHASSIS": {
317
"lldp_loc_chassis_id_subtype": "5",
418
"lldp_loc_chassis_id": "00:11:22:AB:CD:EF",
@@ -8,6 +22,11 @@
822
"lldp_loc_sys_cap_supported": "28 00",
923
"lldp_loc_man_addr": "fe80::ce37:abff:feec:de9c"
1024
},
25+
"PORT_TABLE:Ethernet16": {
26+
"description": "snowflake",
27+
"alias": "etp5",
28+
"speed": 100000
29+
},
1130
"PORT_TABLE:Ethernet-BP16": {
1231
"description": "backplane",
1332
"alias": "etp9",

0 commit comments

Comments
 (0)