Skip to content

Commit a4d40f2

Browse files
authored
[port_util] add get_rif_port_map, get_vlan_interface_oid_map (#78)
* [port_util] add get_rif_port_map, get_vlan_interface_oid_map * clear lgtm warning * fix comments
1 parent dd828d0 commit a4d40f2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/swsssdk/port_util.py

+46
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
in multi-asic platform.
1212
"""
1313
SONIC_ETHERNET_BP_RE_PATTERN = "^Ethernet-BP(\d+)$"
14+
SONIC_VLAN_RE_PATTERN = "^Vlan(\d+)$"
1415
SONIC_PORTCHANNEL_RE_PATTERN = "^PortChannel(\d+)$"
1516
SONIC_MGMT_PORT_RE_PATTERN = "^eth(\d+)$"
1617

1718

1819
class BaseIdx:
1920
ethernet_base_idx = 1
21+
vlan_interface_base_idx = 2000
2022
ethernet_bp_base_idx = 9000
2123
portchannel_base_idx = 1000
2224
mgmt_port_base_idx = 10000
@@ -25,6 +27,7 @@ def get_index(if_name):
2527
"""
2628
OIDs are 1-based, interfaces are 0-based, return the 1-based index
2729
Ethernet N = N + 1
30+
Vlan N = N + 2000
2831
Ethernet_BP N = N + 9000
2932
PortChannel N = N + 1000
3033
eth N = N + 10000
@@ -36,13 +39,15 @@ def get_index_from_str(if_name):
3639
"""
3740
OIDs are 1-based, interfaces are 0-based, return the 1-based index
3841
Ethernet N = N + 1
42+
Vlan N = N + 2000
3943
Ethernet_BP N = N + 9000
4044
PortChannel N = N + 1000
4145
eth N = N + 10000
4246
"""
4347
patterns = {
4448
SONIC_ETHERNET_RE_PATTERN: BaseIdx.ethernet_base_idx,
4549
SONIC_ETHERNET_BP_RE_PATTERN: BaseIdx.ethernet_bp_base_idx,
50+
SONIC_VLAN_RE_PATTERN: BaseIdx.vlan_interface_base_idx,
4651
SONIC_PORTCHANNEL_RE_PATTERN: BaseIdx.portchannel_base_idx,
4752
SONIC_MGMT_PORT_RE_PATTERN: BaseIdx.mgmt_port_base_idx
4853
}
@@ -103,3 +108,44 @@ def get_vlan_id_from_bvid(db, bvid):
103108

104109
return vlan_id
105110

111+
def get_rif_port_map(db):
112+
"""
113+
Get the RIF port mapping from ASIC DB
114+
"""
115+
db.connect('ASIC_DB')
116+
rif_keys_str = db.keys('ASIC_DB', "ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE:*")
117+
if not rif_keys_str:
118+
return {}
119+
120+
rif_port_oid_map = {}
121+
for rif_s in rif_keys_str:
122+
rif_id = rif_s.lstrip(b"ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE:oid:0x")
123+
ent = db.get_all('ASIC_DB', rif_s, blocking=True)
124+
if b"SAI_ROUTER_INTERFACE_ATTR_PORT_ID" in ent:
125+
port_id = ent[b"SAI_ROUTER_INTERFACE_ATTR_PORT_ID"].lstrip(b"oid:0x")
126+
rif_port_oid_map[rif_id] = port_id
127+
128+
return rif_port_oid_map
129+
130+
def get_vlan_interface_oid_map(db):
131+
"""
132+
Get Vlan Interface names and sai oids
133+
"""
134+
db.connect('COUNTERS_DB')
135+
rif_name_map = db.get_all('COUNTERS_DB', 'COUNTERS_RIF_NAME_MAP', blocking=True)
136+
rif_type_name_map = db.get_all('COUNTERS_DB', 'COUNTERS_RIF_TYPE_MAP', blocking=True)
137+
138+
if not rif_name_map or not rif_type_name_map:
139+
return {}
140+
141+
oid_pfx = len("oid:0x")
142+
vlan_if_name_map = {}
143+
144+
for if_name, sai_oid in rif_name_map.items():
145+
# Check if RIF is l3 vlan interface
146+
if rif_type_name_map[sai_oid] == b'SAI_ROUTER_INTERFACE_TYPE_VLAN':
147+
# Check if interface name is in style understood to be a SONiC interface
148+
if get_index(if_name):
149+
vlan_if_name_map[sai_oid[oid_pfx:]] = if_name
150+
151+
return vlan_if_name_map

0 commit comments

Comments
 (0)