From a215bcd8f1cdbfe27ad808817cce5c12a12e7d4b Mon Sep 17 00:00:00 2001 From: Kebo Liu Date: Fri, 3 Aug 2018 07:53:30 +0800 Subject: [PATCH] [mlnx-sfp-plugin] enhancement to support transceiver sensor monitoring (#1839) * [mlnx-sfpplugin] enhancement to support tranceiver sensor monitoring * Modify the eeprom folder to make it accessably from pmon container * implement the get_transceiver_change_event API file change list modified: device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py modified: device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py modified: device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py modified: device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py modified: device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py signed-off-by Liu Kebo kebol@mellanox.com * remove commented code * revise the get_transceiver_change_event implementation and remove unused function * remove blank --- .../plugins/sfputil.py | 43 ++++++++++++++++- .../x86_64-mlnx_msn2100-r0/plugins/sfputil.py | 43 ++++++++++++++++- .../x86_64-mlnx_msn2410-r0/plugins/sfputil.py | 48 +++++++++++++++++-- .../x86_64-mlnx_msn2700-r0/plugins/sfputil.py | 43 ++++++++++++++++- .../x86_64-mlnx_msn2740-r0/plugins/sfputil.py | 43 ++++++++++++++++- 5 files changed, 208 insertions(+), 12 deletions(-) diff --git a/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py b/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py index 6aef1310d5..2eb3d628ff 100644 --- a/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py @@ -10,10 +10,13 @@ try: except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +# parameters for DB connection +REDIS_HOSTNAME = "localhost" +REDIS_PORT = 6379 +REDIS_TIMEOUT_USECS = 0 class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" - PORT_START = 0 PORT_END = 31 PORTS_IN_BLOCK = 32 @@ -22,6 +25,12 @@ class SfpUtil(SfpUtilBase): _port_to_eeprom_mapping = {} + db_sel = None + db_sel_timeout = None + db_sel_object = None + db_sel_tbl = None + state_db = None + @property def port_start(self): return self.PORT_START @@ -39,7 +48,7 @@ class SfpUtil(SfpUtilBase): return self._port_to_eeprom_mapping def __init__(self): - eeprom_path = "/bsp/qsfp/qsfp{0}" + eeprom_path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp{0}_eeprom" for x in range(0, self.port_end + 1): self._port_to_eeprom_mapping[x] = eeprom_path.format(x + self.EEPROM_OFFSET) @@ -149,3 +158,33 @@ class SfpUtil(SfpUtilBase): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + status = True + + if self.db_sel == None: + from swsscommon import swsscommon + self.state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + self.db_sel = swsscommon.Select() + self.db_sel_tbl = swsscommon.NotificationConsumer(self.state_db, 'TRANSCEIVER_NOTIFY') + self.db_sel.addSelectable(self.db_sel_tbl) + self.db_sel_timeout = swsscommon.Select.TIMEOUT + self.db_sel_object = swsscommon.Select.OBJECT + + (state, c) = self.db_sel.select(timeout) + if state == self.db_sel_timeout: + status = True + elif state != self.db_sel_object: + status = False + else: + (key, op, fvp) = self.db_sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + diff --git a/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py b/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py index 4a7b063ac6..631a6b774f 100644 --- a/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py @@ -10,10 +10,13 @@ try: except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +# parameters for DB connection +REDIS_HOSTNAME = "localhost" +REDIS_PORT = 6379 +REDIS_TIMEOUT_USECS = 0 class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" - PORT_START = 0 PORT_END = 15 PORTS_IN_BLOCK = 16 @@ -22,6 +25,12 @@ class SfpUtil(SfpUtilBase): _port_to_eeprom_mapping = {} + db_sel = None + db_sel_timeout = None + db_sel_object = None + db_sel_tbl = None + state_db = None + @property def port_start(self): return self.PORT_START @@ -39,7 +48,7 @@ class SfpUtil(SfpUtilBase): return self._port_to_eeprom_mapping def __init__(self): - eeprom_path = "/bsp/qsfp/qsfp{0}" + eeprom_path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon4/qsfp{0}_eeprom" for x in range(0, self.port_end + 1): self._port_to_eeprom_mapping[x] = eeprom_path.format(x + self.EEPROM_OFFSET) @@ -149,3 +158,33 @@ class SfpUtil(SfpUtilBase): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + status = True + + if self.db_sel == None: + from swsscommon import swsscommon + self.state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + self.db_sel = swsscommon.Select() + self.db_sel_tbl = swsscommon.NotificationConsumer(self.state_db, 'TRANSCEIVER_NOTIFY') + self.db_sel.addSelectable(self.db_sel_tbl) + self.db_sel_timeout = swsscommon.Select.TIMEOUT + self.db_sel_object = swsscommon.Select.OBJECT + + (state, c) = self.db_sel.select(timeout) + if state == self.db_sel_timeout: + status = True + elif state != self.db_sel_object: + status = False + else: + (key, op, fvp) = self.db_sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + diff --git a/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py b/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py index d9699aca45..c379155b82 100644 --- a/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py @@ -10,18 +10,27 @@ try: except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +# parameters for DB connection +REDIS_HOSTNAME = "localhost" +REDIS_PORT = 6379 +REDIS_TIMEOUT_USECS = 0 class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" - PORT_START = 0 PORT_END = 55 PORTS_IN_BLOCK = 56 - + QSFP_PORT_START = 48 EEPROM_OFFSET = 1 _port_to_eeprom_mapping = {} + db_sel = None + db_sel_timeout = None + db_sel_object = None + db_sel_tbl = None + state_db = None + @property def port_start(self): return self.PORT_START @@ -32,20 +41,21 @@ class SfpUtil(SfpUtilBase): @property def qsfp_ports(self): - return range(0, self.PORTS_IN_BLOCK + 1) + return range(self.QSFP_PORT_START, self.PORTS_IN_BLOCK + 1) @property def port_to_eeprom_mapping(self): return self._port_to_eeprom_mapping def __init__(self): - eeprom_path = "/bsp/qsfp/qsfp{0}" + eeprom_path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp{0}_eeprom" for x in range(0, self.port_end + 1): self._port_to_eeprom_mapping[x] = eeprom_path.format(x + self.EEPROM_OFFSET) SfpUtilBase.__init__(self) + def get_presence(self, port_num): # Check for invalid port_num if port_num < self.port_start or port_num > self.port_end: @@ -149,3 +159,33 @@ class SfpUtil(SfpUtilBase): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + status = True + + if self.db_sel == None: + from swsscommon import swsscommon + self.state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + self.db_sel = swsscommon.Select() + self.db_sel_tbl = swsscommon.NotificationConsumer(self.state_db, 'TRANSCEIVER_NOTIFY') + self.db_sel.addSelectable(self.db_sel_tbl) + self.db_sel_timeout = swsscommon.Select.TIMEOUT + self.db_sel_object = swsscommon.Select.OBJECT + + (state, c) = self.db_sel.select(timeout) + if state == self.db_sel_timeout: + status = True + elif state != self.db_sel_object: + status = False + else: + (key, op, fvp) = self.db_sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + diff --git a/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py b/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py index 6aef1310d5..2eb3d628ff 100644 --- a/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py @@ -10,10 +10,13 @@ try: except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +# parameters for DB connection +REDIS_HOSTNAME = "localhost" +REDIS_PORT = 6379 +REDIS_TIMEOUT_USECS = 0 class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" - PORT_START = 0 PORT_END = 31 PORTS_IN_BLOCK = 32 @@ -22,6 +25,12 @@ class SfpUtil(SfpUtilBase): _port_to_eeprom_mapping = {} + db_sel = None + db_sel_timeout = None + db_sel_object = None + db_sel_tbl = None + state_db = None + @property def port_start(self): return self.PORT_START @@ -39,7 +48,7 @@ class SfpUtil(SfpUtilBase): return self._port_to_eeprom_mapping def __init__(self): - eeprom_path = "/bsp/qsfp/qsfp{0}" + eeprom_path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp{0}_eeprom" for x in range(0, self.port_end + 1): self._port_to_eeprom_mapping[x] = eeprom_path.format(x + self.EEPROM_OFFSET) @@ -149,3 +158,33 @@ class SfpUtil(SfpUtilBase): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + status = True + + if self.db_sel == None: + from swsscommon import swsscommon + self.state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + self.db_sel = swsscommon.Select() + self.db_sel_tbl = swsscommon.NotificationConsumer(self.state_db, 'TRANSCEIVER_NOTIFY') + self.db_sel.addSelectable(self.db_sel_tbl) + self.db_sel_timeout = swsscommon.Select.TIMEOUT + self.db_sel_object = swsscommon.Select.OBJECT + + (state, c) = self.db_sel.select(timeout) + if state == self.db_sel_timeout: + status = True + elif state != self.db_sel_object: + status = False + else: + (key, op, fvp) = self.db_sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + diff --git a/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py b/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py index 6aef1310d5..d617135e3e 100644 --- a/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py @@ -10,10 +10,13 @@ try: except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +# parameters for DB connection +REDIS_HOSTNAME = "localhost" +REDIS_PORT = 6379 +REDIS_TIMEOUT_USECS = 0 class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" - PORT_START = 0 PORT_END = 31 PORTS_IN_BLOCK = 32 @@ -22,6 +25,12 @@ class SfpUtil(SfpUtilBase): _port_to_eeprom_mapping = {} + db_sel = None + db_sel_timeout = None + db_sel_object = None + db_sel_tbl = None + state_db = None + @property def port_start(self): return self.PORT_START @@ -39,7 +48,7 @@ class SfpUtil(SfpUtilBase): return self._port_to_eeprom_mapping def __init__(self): - eeprom_path = "/bsp/qsfp/qsfp{0}" + eeprom_path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp{0}_eeprom" for x in range(0, self.port_end + 1): self._port_to_eeprom_mapping[x] = eeprom_path.format(x + self.EEPROM_OFFSET) @@ -149,3 +158,33 @@ class SfpUtil(SfpUtilBase): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + status = True + + if self.db_sel == None: + from swsscommon import swsscommon + self.state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + self.db_sel = swsscommon.Select() + self.db_sel_tbl = swsscommon.NotificationConsumer(self.state_db, 'TRANSCEIVER_NOTIFY') + self.db_sel.addSelectable(self.db_sel_tbl) + self.db_sel_timeout = swsscommon.Select.TIMEOUT + self.db_sel_object = swsscommon.Select.OBJECT + + (state, c) = self.db_sel.select(timeout) + if state == self.db_sel_timeout: + status = True + elif state != self.db_sel_object: + status = False + else: + (key, op, fvp) = self.db_sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict +