Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rfc1213] Interface MIB add l3 vlan interfaces & aggregate rif counters #169

Merged
merged 26 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 76 additions & 5 deletions src/sonic_ax_impl/mibs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@
TABLE_NAME_SEPARATOR_COLON = ':'
TABLE_NAME_SEPARATOR_VBAR = '|'

RIF_COUNTERS_AGGR_MAP = {
"SAI_PORT_STAT_IF_IN_OCTETS": "SAI_ROUTER_INTERFACE_STAT_IN_OCTETS",
"SAI_PORT_STAT_IF_IN_UCAST_PKTS": "SAI_ROUTER_INTERFACE_STAT_IN_PACKETS",
"SAI_PORT_STAT_IF_IN_ERRORS": "SAI_ROUTER_INTERFACE_STAT_IN_ERROR_PACKETS",
"SAI_PORT_STAT_IF_OUT_OCTETS": "SAI_ROUTER_INTERFACE_STAT_OUT_OCTETS",
"SAI_PORT_STAT_IF_OUT_UCAST_PKTS": "SAI_ROUTER_INTERFACE_STAT_OUT_PACKETS",
"SAI_PORT_STAT_IF_OUT_ERRORS": "SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_PACKETS"
}

RIF_DROPS_AGGR_MAP = {
"SAI_PORT_STAT_IF_IN_ERRORS": "SAI_ROUTER_INTERFACE_STAT_IN_ERROR_PACKETS",
"SAI_PORT_STAT_IF_OUT_ERRORS": "SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_PACKETS"
}

# IfIndex to OID multiplier for transceiver
IFINDEX_SUB_ID_MULTIPLIER = 1000

redis_kwargs = {'unix_socket_path': '/var/run/redis/redis.sock'}


Expand Down Expand Up @@ -134,6 +151,14 @@ def if_entry_table(if_name):
return 'PORT_TABLE:' + if_name


def vlan_entry_table(if_name):
"""
:param if_name: given interface to cast.
:return: VLAN_TABLE key.
"""
return b'VLAN_TABLE:' + if_name


def lag_entry_table(lag_name):
"""
:param lag_name: given lag to cast.
Expand Down Expand Up @@ -290,6 +315,46 @@ def init_sync_d_interface_tables(db_conn):

return if_name_map, if_alias_map, if_id_map, oid_name_map


def init_sync_d_rif_tables(db_conn):
"""
Initializes map of RIF SAI oids to port SAI oid.
:return: dict
"""
rif_port_map = {get_sai_id_key(db_conn.namespace, rif): get_sai_id_key(db_conn.namespace, port)
for rif, port in port_util.get_rif_port_map(db_conn).items()}
port_rif_map = {port: rif for rif, port in rif_port_map.items()}
logger.debug("Rif port map:\n" + pprint.pformat(rif_port_map, indent=2))

return rif_port_map, port_rif_map


def init_sync_d_vlan_tables(db_conn):
"""
Initializes vlan interface maps for SyncD-connected MIB(s).
:return: tuple(vlan_name_map, oid_sai_map, oid_name_map)
"""

vlan_name_map = port_util.get_vlan_interface_oid_map(db_conn)

logger.debug("Vlan oid map:\n" + pprint.pformat(vlan_name_map, indent=2))

# { OID -> sai_id }
oid_sai_map = {get_index_from_str(if_name): sai_id for sai_id, if_name in vlan_name_map.items()
# only map the interface if it's a style understood to be a SONiC interface.
if get_index_from_str(if_name) is not None}
logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2))

# { OID -> if_name (SONiC) }
oid_name_map = {get_index_from_str(if_name): if_name for sai_id, if_name in vlan_name_map.items()
# only map the interface if it's a style understood to be a SONiC interface.
if get_index_from_str(if_name) is not None}

logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2))

return vlan_name_map, oid_sai_map, oid_name_map


def init_sync_d_lag_tables(db_conn):
"""
Helper method. Connects to and initializes LAG interface maps for SyncD-connected MIB(s).
Expand All @@ -304,13 +369,19 @@ def init_sync_d_lag_tables(db_conn):
if_name_lag_name_map = {}
# { OID -> lag_name (SONiC) }
oid_lag_name_map = {}
# { lag_name (SONiC) -> lag_oid (SAI) }
lag_sai_map = {}

db_conn.connect(APPL_DB)

lag_entries = db_conn.keys(APPL_DB, "LAG_TABLE:*")

if not lag_entries:
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map, lag_sai_map

db_conn.connect(COUNTERS_DB)
lag_sai_map = db_conn.get_all(COUNTERS_DB, "COUNTERS_LAG_NAME_MAP")
lag_sai_map = {name: get_sai_id_key(db_conn.namespace, sai_id.lstrip("oid:0x")) for name, sai_id in lag_sai_map.items()}

for lag_entry in lag_entries:
lag_name = lag_entry[len("LAG_TABLE:"):]
Expand All @@ -332,7 +403,7 @@ def member_name_str(val, lag_name):
if idx:
oid_lag_name_map[idx] = if_name

return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map, lag_sai_map

def init_sync_d_queue_tables(db_conn):
"""
Expand All @@ -347,7 +418,7 @@ def init_sync_d_queue_tables(db_conn):

# Parse the queue_name_map and create the following maps:
# port_queues_map -> {"port_index : queue_index" : sai_oid}
# queue_stat_map -> {"port_index : queue stat table name" : {counter name : value}}
# queue_stat_map -> {"port_index : queue stat table name" : {counter name : value}}
# port_queue_list_map -> {port_index: [sorted queue list]}
port_queues_map = {}
queue_stat_map = {}
Expand Down Expand Up @@ -411,7 +482,7 @@ class RedisOidTreeUpdater(MIBUpdater):
def __init__(self, prefix_str):
super().__init__()

self.db_conn = Namespace.init_namespace_dbs()
self.db_conn = Namespace.init_namespace_dbs()
if prefix_str.startswith('.'):
prefix_str = prefix_str[1:]
self.prefix_str = prefix_str
Expand Down Expand Up @@ -524,7 +595,7 @@ def dbs_get_all(dbs, db_name, _hash, *args, **kwargs):
db get_all function executed on global and all namespace DBs.
"""
result = {}
# If there are multiple namespaces, _hash might not be
# If there are multiple namespaces, _hash might not be
# present in all namespace, ignore if not present in a
# specfic namespace.
if len(dbs) > 1:
Expand Down
88 changes: 79 additions & 9 deletions src/sonic_ax_impl/mibs/ietf/rfc1213.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class DbTables(int, Enum):
class IfTypes(int, Enum):
""" IANA ifTypes """
ethernetCsmacd = 6
ieee8023adLag = 161
l3ipvlan = 136
ieee8023adLag = 161

class ArpUpdater(MIBUpdater):
def __init__(self):
Expand Down Expand Up @@ -191,8 +192,13 @@ def __init__(self):
self.lag_name_if_name_map = {}
self.if_name_lag_name_map = {}
self.oid_lag_name_map = {}
self.lag_sai_map = {}
self.mgmt_oid_name_map = {}
self.mgmt_alias_map = {}
self.vlan_oid_name_map = {}
self.vlan_name_map = {}
self.rif_port_map = {}
self.port_rif_map = {}

# cache of interface counters
self.if_counters = {}
Expand All @@ -201,6 +207,8 @@ def __init__(self):
self.if_alias_map = {}
self.if_id_map = {}
self.oid_name_map = {}
self.rif_counters = {}

self.namespace_db_map = Namespace.get_namespace_db_map(self.db_conn)

def reinit_data(self):
Expand All @@ -219,26 +227,50 @@ def reinit_data(self):
self.mgmt_oid_name_map, \
self.mgmt_alias_map = mibs.init_mgmt_interface_tables(self.db_conn[0])

self.vlan_name_map, \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes [](start = 29, length = 5)

If you merge latest master, you don't need to encoding to byte array.

self.vlan_oid_sai_map, \
self.vlan_oid_name_map = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_vlan_tables, self.db_conn)

self.rif_port_map, \
self.port_rif_map = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_rif_tables, self.db_conn)

def update_data(self):
"""
Update redis (caches config)
Pulls the table references for each interface.
"""
for sai_id_key in self.if_id_map:
namespace, sai_id = mibs.split_sai_id_key(sai_id_key)
if_idx = mibs.get_index_from_str(self.if_id_map[sai_id_key])
self.if_counters[if_idx] = self.namespace_db_map[namespace].get_all(mibs.COUNTERS_DB, \
mibs.counter_table(sai_id), blocking=True)

self.update_if_counters()
self.update_rif_counters()

self.aggregate_counters()

self.lag_name_if_name_map, \
self.if_name_lag_name_map, \
self.oid_lag_name_map = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_lag_tables, self.db_conn)
self.oid_lag_name_map, \
self.lag_sai_map = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_lag_tables, self.db_conn)

self.if_range = sorted(list(self.oid_name_map.keys()) +
list(self.oid_lag_name_map.keys()) +
list(self.mgmt_oid_name_map.keys()))
list(self.mgmt_oid_name_map.keys()) +
list(self.vlan_oid_name_map.keys()))
self.if_range = [(i,) for i in self.if_range]

def update_if_counters(self):
for sai_id_key in self.if_id_map:
namespace, sai_id = mibs.split_sai_id_key(sai_id_key)
if_idx = mibs.get_index_from_str(self.if_id_map[sai_id_key])
self.if_counters[if_idx] = self.namespace_db_map[namespace].get_all(mibs.COUNTERS_DB, \
mibs.counter_table(sai_id), blocking=True)

def update_rif_counters(self):
rif_sai_ids = list(self.rif_port_map) + list(self.vlan_name_map)
self.rif_counters = \
{sai_id: Namespace.dbs_get_all(self.db_conn, mibs.COUNTERS_DB,
mibs.counter_table(mibs.split_sai_id_key(sai_id)[1]),
blocking=False)
for sai_id in rif_sai_ids}

def get_next(self, sub_id):
"""
:param sub_id: The 1-based sub-identifier query.
Expand Down Expand Up @@ -280,6 +312,8 @@ def interface_description(self, sub_id):
return self.oid_lag_name_map[oid]
elif oid in self.mgmt_oid_name_map:
return self.mgmt_alias_map[self.mgmt_oid_name_map[oid]]
elif oid in self.vlan_oid_name_map:
return self.vlan_oid_name_map[oid]

return self.if_alias_map[self.oid_name_map[oid]]

Expand All @@ -302,6 +336,31 @@ def _get_counter(self, oid, table_name):
mibs.logger.warning("SyncD 'COUNTERS_DB' missing attribute '{}'.".format(e))
return None

def aggregate_counters(self):
"""
For ports with l3 router interfaces l3 drops may be counted separately (RIF counters)
add l3 drops to l2 drop counters cache according to mapping

For l3vlan map l3 counters to l2 counters
"""
for rif_sai_id, port_sai_id in self.rif_port_map.items():
if port_sai_id in self.if_id_map:
port_idx = mibs.get_index_from_str(self.if_id_map[port_sai_id])
for port_counter_name, rif_counter_name in mibs.RIF_DROPS_AGGR_MAP.items():
self.if_counters[port_idx][port_counter_name] = \
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.if_counters [](start = 20, length = 16)

I see you change self.if_counters[][] type from str to int.
Although this is working in python, however we are moving the library to sonic-swss-common SWIG wrapper, and this will make SWIG confused.
Please make type fixed. #Closed

int(self.if_counters[port_idx][port_counter_name]) + \
int(self.rif_counters[rif_sai_id][rif_counter_name])

for vlan_sai_id, vlan_name in self.vlan_name_map.items():
for port_counter_name, rif_counter_name in mibs.RIF_COUNTERS_AGGR_MAP.items():
vlan_idx = mibs.get_index_from_str(vlan_name)
vlan_rif_counters = self.rif_counters[vlan_sai_id]
if rif_counter_name in vlan_rif_counters:
self.if_counters.setdefault(vlan_idx, {})
self.if_counters[vlan_idx][port_counter_name] = \
int(vlan_rif_counters[rif_counter_name])


def get_counter(self, sub_id, table_name):
"""
:param sub_id: The 1-based sub-identifier query.
Expand All @@ -321,7 +380,13 @@ def get_counter(self, sub_id, table_name):
counter_value = 0
for lag_member in self.lag_name_if_name_map[self.oid_lag_name_map[oid]]:
counter_value += self._get_counter(mibs.get_index_from_str(lag_member), table_name)

sai_lag_id = self.lag_sai_map[self.oid_lag_name_map[oid]]
sai_lag_rif_id = self.port_rif_map[sai_lag_id]
if sai_lag_rif_id in self.rif_port_map:
table_name = getattr(table_name, 'name', table_name)
if table_name in mibs.RIF_DROPS_AGGR_MAP:
rif_table_name = mibs.RIF_DROPS_AGGR_MAP[table_name]
counter_value += int(self.rif_counters[sai_lag_rif_id].get(rif_table_name, 0))
# truncate to 32-bit counter
return counter_value & 0x00000000ffffffff
else:
Expand Down Expand Up @@ -351,6 +416,8 @@ def _get_if_entry(self, sub_id):
elif oid in self.mgmt_oid_name_map:
if_table = mibs.mgmt_if_entry_table(self.mgmt_oid_name_map[oid])
db = mibs.CONFIG_DB
elif oid in self.vlan_oid_name_map:
if_table = mibs.vlan_entry_table(self.vlan_oid_name_map[oid])
elif oid in self.oid_name_map:
if_table = mibs.if_entry_table(self.oid_name_map[oid])
else:
Expand Down Expand Up @@ -455,6 +522,7 @@ def get_if_type(self, sub_id):

ethernetCsmacd(6), -- for all ethernet-like interfaces,
-- regardless of speed, as per RFC3635
l3ipvlan(136) -- Layer 3 Virtual LAN using IP
ieee8023adLag(161) -- IEEE 802.3ad Link Aggregate
"""
oid = self.get_oid(sub_id)
Expand All @@ -463,6 +531,8 @@ def get_if_type(self, sub_id):

if oid in self.oid_lag_name_map:
return IfTypes.ieee8023adLag
elif oid in self.vlan_oid_name_map:
return IfTypes.l3ipvlan
else:
return IfTypes.ethernetCsmacd

Expand Down
2 changes: 1 addition & 1 deletion src/sonic_ax_impl/mibs/ietf/rfc2863.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def reinit_data(self):

self.lag_name_if_name_map, \
self.if_name_lag_name_map, \
self.oid_lag_name_map = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_lag_tables, self.db_conn)
self.oid_lag_name_map, _ = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_lag_tables, self.db_conn)
"""
db_conn - will have db_conn to all namespace DBs and
global db. First db in the list is global db.
Expand Down
4 changes: 2 additions & 2 deletions src/sonic_ax_impl/mibs/vendor/cisco/ciscoPfcExtMIB.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def update_data(self):

self.lag_name_if_name_map, \
self.if_name_lag_name_map, \
self.oid_lag_name_map = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_lag_tables, self.db_conn)
self.oid_lag_name_map, _ = Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_lag_tables, self.db_conn)

self.if_range = sorted(list(self.oid_name_map.keys()) + list(self.oid_lag_name_map.keys()))
self.if_range = [(i,) for i in self.if_range]
Expand Down Expand Up @@ -94,7 +94,7 @@ def _get_counter(self, oid, counter_name):
_counter_name = getattr(counter_name, 'name', counter_name)

try:
counter_value = self.if_counters[oid][_counter_name]
counter_value = self.if_counters[oid][_counter_name]
counter_value = int(counter_value) & 0xffffffffffffffff
# done!
return counter_value
Expand Down
7 changes: 7 additions & 0 deletions tests/mock_tables/asic0/asic_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000005",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true"
},
"ASIC_STATE:SAI_OBJECT_TYPE_ROUTER_INTERFACE:oid:0x6000000000006": {
"SAI_ROUTER_INTERFACE_ATTR_TYPE": "SAI_ROUTER_INTERFACE_TYPE_PORT",
"SAI_ROUTER_INTERFACE_ATTR_PORT_ID": "oid:0x1000000000007"
},
"ASIC_STATE:SAI_OBJECT_TYPE_LAG:oid:0x1000000000007": {
"NULL": "NULL"
}
}
Loading