From 64f0def9275f6bc6275f51093ba641fb140178e8 Mon Sep 17 00:00:00 2001 From: Lukas Stockner Date: Thu, 14 Sep 2023 20:51:43 +0200 Subject: [PATCH] Support interface speed for PortChannels (#262) **- What I did** This change makes SNMP report the correct interface speed for PortChannels instead of defaulting to 40G. **- How I did it** Similar to `intfutil`, the code simply sums up the interface speeds of all member ports. The logic for iterating over members is based on `_get_counter`. **- How to verify it** - Check `snmpwalk -v 2c -c public ifHighSpeed`, note that PortChannels are reported as 40G - Apply the change - Check again, note that the same speed as in `show interface status` is reported now. **- Description for the changelog** Support reporting interface speeds for PortChannels through SNMP --- src/sonic_ax_impl/mibs/ietf/rfc2863.py | 26 +++++++++++++++++++------- tests/mock_tables/appl_db.json | 3 +++ tests/test_hc_interfaces.py | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/sonic_ax_impl/mibs/ietf/rfc2863.py b/src/sonic_ax_impl/mibs/ietf/rfc2863.py index 78fcda8d0..562d07b0a 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc2863.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc2863.py @@ -150,7 +150,11 @@ def interface_alias(self, sub_id): :param sub_id: The 1-based sub-identifier query. :return: The SONiC interface description, empty string if not defined. """ - entry = self._get_if_entry(sub_id) + oid = self.get_oid(sub_id) + if not oid: + return + + entry = self._get_if_entry(oid) if not entry: return @@ -202,15 +206,11 @@ def _get_counter(self, oid, table_name, mask): mibs.logger.warning("SyncD 'COUNTERS_DB' missing attribute '{}'.".format(e)) return None - def _get_if_entry(self, sub_id): + def _get_if_entry(self, oid): """ :param oid: The 1-based sub-identifier query. :return: the DB entry for the respective sub_id. """ - oid = self.get_oid(sub_id) - if not oid: - return - if_table = "" # Once PORT_TABLE will be moved to CONFIG DB # we will get entry from CONFIG_DB for all cases @@ -232,7 +232,19 @@ def get_high_speed(self, sub_id): :param sub_id: The 1-based sub-identifier query. :return: speed value for the respective sub_id or 40000 if not defined. """ - entry = self._get_if_entry(sub_id) + oid = self.get_oid(sub_id) + if not oid: + return + + if oid in self.oid_lag_name_map: + speed = 0 + for lag_member in self.lag_name_if_name_map[self.oid_lag_name_map[oid]]: + entry = self._get_if_entry(mibs.get_index_from_str(lag_member)) + if entry: + speed += int(entry.get("speed", 0)) + return speed + + entry = self._get_if_entry(oid) if not entry: return diff --git a/tests/mock_tables/appl_db.json b/tests/mock_tables/appl_db.json index 69bdbf1ad..c76c1e534 100644 --- a/tests/mock_tables/appl_db.json +++ b/tests/mock_tables/appl_db.json @@ -640,6 +640,9 @@ "nexthop": "", "ifname": "lo" }, + "LAG_MEMBER_TABLE:PortChannel01:Ethernet108": { + "status": "enabled" + }, "LAG_MEMBER_TABLE:PortChannel01:Ethernet112": { "status": "enabled" }, diff --git a/tests/test_hc_interfaces.py b/tests/test_hc_interfaces.py index 28d59a07d..ea5638c3a 100644 --- a/tests/test_hc_interfaces.py +++ b/tests/test_hc_interfaces.py @@ -147,6 +147,25 @@ def test_no_speed(self): self.assertEqual(str(value0.name), str(ObjectIdentifier(12, 0, 1, 0, (1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 15, 121)))) self.assertEqual(value0.data, 40000) + def test_portchannel_speed(self): + """ + For a portchannel, the speed should be the sum of all members' speeds + """ + oid = ObjectIdentifier(12, 0, 0, 0, (1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 15, 1000)) + get_pdu = GetNextPDU( + header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0), + oids=[oid] + ) + + encoded = get_pdu.encode() + response = get_pdu.make_response(self.lut) + print(response) + + value0 = response.values[0] + self.assertEqual(value0.type_, ValueType.GAUGE_32) + self.assertEqual(str(value0.name), str(ObjectIdentifier(12, 0, 1, 0, (1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 15, 1001)))) + self.assertEqual(value0.data, 200000) + def test_no_description(self): """ For a port with no speed in the db the result should be 40000