From 064ef8c5c023000bae7f4b6597ea64d6b856ef8d Mon Sep 17 00:00:00 2001 From: keboliu Date: Tue, 3 Jul 2018 12:31:18 +0300 Subject: [PATCH 1/4] [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 --- .../plugins/sfputil.py | 64 ++++++++++++++++- .../x86_64-mlnx_msn2100-r0/plugins/sfputil.py | 64 ++++++++++++++++- .../x86_64-mlnx_msn2410-r0/plugins/sfputil.py | 69 ++++++++++++++++++- .../x86_64-mlnx_msn2700-r0/plugins/sfputil.py | 64 ++++++++++++++++- .../x86_64-mlnx_msn2740-r0/plugins/sfputil.py | 64 ++++++++++++++++- 5 files changed, 319 insertions(+), 6 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 6aef1310d51..379b9772123 100644 --- a/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py @@ -10,6 +10,34 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +def determine_in_docker(): + with open('/proc/self/cgroup') as f: + lines = [line.rstrip('\n') for line in f] + for line in lines: + words = line.split('/') + if len(words) > 1 and words[1] == 'docker': + return True + + return False + +INSIDE_DOCKER = determine_in_docker() + +if INSIDE_DOCKER: + from swsscommon import swsscommon + REDIS_HOSTNAME = "localhost" + REDIS_PORT = 6379 + REDIS_TIMEOUT_USECS = 0 + + state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + sel = swsscommon.Select() + sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') + sel.addSelectable(sel_tbl) + class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" @@ -39,7 +67,7 @@ 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/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) @@ -64,6 +92,22 @@ def get_presence(self, port_num): return True return False + + def get_presence_status_file_path(self, port_num): + # Check for invalid port_num + if port_num < self.port_start or port_num > self.port_end: + return "" + + try: + path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp%d_status" % (port_num+1) + #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) + reg_file = open(path) + except IOError as e: + print "Error: unable to open file: %s" % str(e) + return "" + + reg_file.close() + return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -149,3 +193,21 @@ def reset(self, port_num): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + if INSIDE_DOCKER: + status = True + (state, c) = sel.select(timeout) + + if state == swsscommon.Select.TIMEOUT: + status = true + elif state != swsscommon.Select.OBJECT: + status = False + else: + (key, op, fvp) = sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + else: + return False, 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 4a7b063ac60..7c31b559825 100644 --- a/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py @@ -10,6 +10,34 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +def determine_in_docker(): + with open('/proc/self/cgroup') as f: + lines = [line.rstrip('\n') for line in f] + for line in lines: + words = line.split('/') + if len(words) > 1 and words[1] == 'docker': + return True + + return False + +INSIDE_DOCKER = determine_in_docker() + +if INSIDE_DOCKER: + from swsscommon import swsscommon + REDIS_HOSTNAME = "localhost" + REDIS_PORT = 6379 + REDIS_TIMEOUT_USECS = 0 + + state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + sel = swsscommon.Select() + sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') + sel.addSelectable(sel_tbl) + class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" @@ -39,7 +67,7 @@ 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/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) @@ -64,6 +92,22 @@ def get_presence(self, port_num): return True return False + + def get_presence_status_file_path(self, port_num): + # Check for invalid port_num + if port_num < self.port_start or port_num > self.port_end: + return "" + + try: + path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon4/qsfp%d_status" % (port_num+1) + #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) + reg_file = open(path) + except IOError as e: + print "Error: unable to open file: %s" % str(e) + return "" + + reg_file.close() + return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -149,3 +193,21 @@ def reset(self, port_num): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + if INSIDE_DOCKER: + status = True + (state, c) = sel.select(timeout) + + if state == swsscommon.Select.TIMEOUT: + status = true + elif state != swsscommon.Select.OBJECT: + status = False + else: + (key, op, fvp) = sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + else: + return False, 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 d9699aca45a..dfa17dc5773 100644 --- a/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py @@ -10,13 +10,43 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +def determine_in_docker(): + with open('/proc/self/cgroup') as f: + lines = [line.rstrip('\n') for line in f] + for line in lines: + words = line.split('/') + if len(words) > 1 and words[1] == 'docker': + return True + + return False + +INSIDE_DOCKER = determine_in_docker() + +if INSIDE_DOCKER: + from swsscommon import swsscommon + REDIS_HOSTNAME = "localhost" + REDIS_PORT = 6379 + REDIS_TIMEOUT_USECS = 0 + + state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + sel = swsscommon.Select() + sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') + sel.addSelectable(sel_tbl) + class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" + PORT_START = 0 PORT_END = 55 PORTS_IN_BLOCK = 56 + QSFP_PORT_START = 48 EEPROM_OFFSET = 1 @@ -32,20 +62,21 @@ def port_end(self): @property def qsfp_ports(self): - return range(0, self.PORTS_IN_BLOCK + 1) + return range(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: @@ -64,6 +95,22 @@ def get_presence(self, port_num): return True return False + + def get_presence_status_file_path(self, port_num): + # Check for invalid port_num + if port_num < self.port_start or port_num > self.port_end: + return "" + + try: + path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp%d_status" % (port_num+1) + #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) + reg_file = open(path) + except IOError as e: + print "Error: unable to open file: %s" % str(e) + return "" + + reg_file.close() + return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -149,3 +196,21 @@ def reset(self, port_num): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + if INSIDE_DOCKER: + status = True + (state, c) = sel.select(timeout) + + if state == swsscommon.Select.TIMEOUT: + status = true + elif state != swsscommon.Select.OBJECT: + status = False + else: + (key, op, fvp) = sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + else: + return False, 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 6aef1310d51..379b9772123 100644 --- a/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py @@ -10,6 +10,34 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +def determine_in_docker(): + with open('/proc/self/cgroup') as f: + lines = [line.rstrip('\n') for line in f] + for line in lines: + words = line.split('/') + if len(words) > 1 and words[1] == 'docker': + return True + + return False + +INSIDE_DOCKER = determine_in_docker() + +if INSIDE_DOCKER: + from swsscommon import swsscommon + REDIS_HOSTNAME = "localhost" + REDIS_PORT = 6379 + REDIS_TIMEOUT_USECS = 0 + + state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + sel = swsscommon.Select() + sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') + sel.addSelectable(sel_tbl) + class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" @@ -39,7 +67,7 @@ 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/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) @@ -64,6 +92,22 @@ def get_presence(self, port_num): return True return False + + def get_presence_status_file_path(self, port_num): + # Check for invalid port_num + if port_num < self.port_start or port_num > self.port_end: + return "" + + try: + path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp%d_status" % (port_num+1) + #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) + reg_file = open(path) + except IOError as e: + print "Error: unable to open file: %s" % str(e) + return "" + + reg_file.close() + return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -149,3 +193,21 @@ def reset(self, port_num): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + if INSIDE_DOCKER: + status = True + (state, c) = sel.select(timeout) + + if state == swsscommon.Select.TIMEOUT: + status = true + elif state != swsscommon.Select.OBJECT: + status = False + else: + (key, op, fvp) = sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + else: + return False, 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 6aef1310d51..5a06c8f009c 100644 --- a/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py @@ -10,6 +10,34 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) +def determine_in_docker(): + with open('/proc/self/cgroup') as f: + lines = [line.rstrip('\n') for line in f] + for line in lines: + words = line.split('/') + if len(words) > 1 and words[1] == 'docker': + return True + + return False + +INSIDE_DOCKER = determine_in_docker() + +if INSIDE_DOCKER: + from swsscommon import swsscommon + REDIS_HOSTNAME = "localhost" + REDIS_PORT = 6379 + REDIS_TIMEOUT_USECS = 0 + + state_db = swsscommon.DBConnector(swsscommon.STATE_DB, + REDIS_HOSTNAME, + REDIS_PORT, + REDIS_TIMEOUT_USECS) + + # Subscribe to state table for SFP change notifications + sel = swsscommon.Select() + sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') + sel.addSelectable(sel_tbl) + class SfpUtil(SfpUtilBase): """Platform-specific SfpUtil class""" @@ -39,7 +67,7 @@ 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) @@ -64,6 +92,22 @@ def get_presence(self, port_num): return True return False + + def get_presence_status_file_path(self, port_num): + # Check for invalid port_num + if port_num < self.port_start or port_num > self.port_end: + return "" + + try: + path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp%d_status" % (port_num+1) + #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) + reg_file = open(path) + except IOError as e: + print "Error: unable to open file: %s" % str(e) + return "" + + reg_file.close() + return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -149,3 +193,21 @@ def reset(self, port_num): return False return False + + def get_transceiver_change_event(self, timeout=0): + phy_port_dict = {} + if INSIDE_DOCKER: + status = True + (state, c) = sel.select(timeout) + + if state == swsscommon.Select.TIMEOUT: + status = true + elif state != swsscommon.Select.OBJECT: + status = False + else: + (key, op, fvp) = sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + else: + return False, phy_port_dict From e3d695564ea88d5441137f7bd74452419bc1fee3 Mon Sep 17 00:00:00 2001 From: Kebo Liu Date: Mon, 9 Jul 2018 06:01:48 +0300 Subject: [PATCH 2/4] remove commented code --- device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py | 1 - device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py | 1 - device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py | 1 - device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py | 1 - device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py | 1 - 5 files changed, 5 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 379b9772123..ee534ecfb61 100644 --- a/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py @@ -100,7 +100,6 @@ def get_presence_status_file_path(self, port_num): try: path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp%d_status" % (port_num+1) - #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) reg_file = open(path) except IOError as e: print "Error: unable to open file: %s" % str(e) 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 7c31b559825..114859bac8e 100644 --- a/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py @@ -100,7 +100,6 @@ def get_presence_status_file_path(self, port_num): try: path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon4/qsfp%d_status" % (port_num+1) - #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) reg_file = open(path) except IOError as e: print "Error: unable to open file: %s" % str(e) 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 dfa17dc5773..906b919ee41 100644 --- a/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py @@ -103,7 +103,6 @@ def get_presence_status_file_path(self, port_num): try: path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp%d_status" % (port_num+1) - #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) reg_file = open(path) except IOError as e: print "Error: unable to open file: %s" % str(e) 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 379b9772123..ee534ecfb61 100644 --- a/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py @@ -100,7 +100,6 @@ def get_presence_status_file_path(self, port_num): try: path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp%d_status" % (port_num+1) - #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) reg_file = open(path) except IOError as e: print "Error: unable to open file: %s" % str(e) 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 5a06c8f009c..947e69c2ed1 100644 --- a/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py @@ -100,7 +100,6 @@ def get_presence_status_file_path(self, port_num): try: path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp%d_status" % (port_num+1) - #path = "/bsp/qsfp/qsfp%d_status" % (port_num+1) reg_file = open(path) except IOError as e: print "Error: unable to open file: %s" % str(e) From c2315efb957d1187021a3123d4dd323b68b0a6b8 Mon Sep 17 00:00:00 2001 From: Kebo Liu Date: Tue, 10 Jul 2018 09:37:12 +0300 Subject: [PATCH 3/4] revise the get_transceiver_change_event implementation and remove unused function --- .../plugins/sfputil.py | 92 +++++++----------- .../x86_64-mlnx_msn2100-r0/plugins/sfputil.py | 92 +++++++----------- .../x86_64-mlnx_msn2410-r0/plugins/sfputil.py | 95 +++++++------------ .../x86_64-mlnx_msn2700-r0/plugins/sfputil.py | 92 +++++++----------- .../x86_64-mlnx_msn2740-r0/plugins/sfputil.py | 92 +++++++----------- 5 files changed, 176 insertions(+), 287 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 ee534ecfb61..2eb3d628ff9 100644 --- a/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_lssn2700-r0/plugins/sfputil.py @@ -10,38 +10,13 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) -def determine_in_docker(): - with open('/proc/self/cgroup') as f: - lines = [line.rstrip('\n') for line in f] - for line in lines: - words = line.split('/') - if len(words) > 1 and words[1] == 'docker': - return True - - return False - -INSIDE_DOCKER = determine_in_docker() - -if INSIDE_DOCKER: - from swsscommon import swsscommon - REDIS_HOSTNAME = "localhost" - REDIS_PORT = 6379 - REDIS_TIMEOUT_USECS = 0 - - state_db = swsscommon.DBConnector(swsscommon.STATE_DB, - REDIS_HOSTNAME, - REDIS_PORT, - REDIS_TIMEOUT_USECS) - - # Subscribe to state table for SFP change notifications - sel = swsscommon.Select() - sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') - sel.addSelectable(sel_tbl) - +# 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 @@ -50,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 @@ -92,21 +73,6 @@ def get_presence(self, port_num): return True return False - - def get_presence_status_file_path(self, port_num): - # Check for invalid port_num - if port_num < self.port_start or port_num > self.port_end: - return "" - - try: - path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp%d_status" % (port_num+1) - reg_file = open(path) - except IOError as e: - print "Error: unable to open file: %s" % str(e) - return "" - - reg_file.close() - return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -195,18 +161,30 @@ def reset(self, port_num): def get_transceiver_change_event(self, timeout=0): phy_port_dict = {} - if INSIDE_DOCKER: + 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 - (state, c) = sel.select(timeout) - - if state == swsscommon.Select.TIMEOUT: - status = true - elif state != swsscommon.Select.OBJECT: - status = False - else: - (key, op, fvp) = sel_tbl.pop() - phy_port_dict[key] = op - - return status, phy_port_dict + elif state != self.db_sel_object: + status = False else: - return False, phy_port_dict + (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 114859bac8e..631a6b774fb 100644 --- a/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2100-r0/plugins/sfputil.py @@ -10,38 +10,13 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) -def determine_in_docker(): - with open('/proc/self/cgroup') as f: - lines = [line.rstrip('\n') for line in f] - for line in lines: - words = line.split('/') - if len(words) > 1 and words[1] == 'docker': - return True - - return False - -INSIDE_DOCKER = determine_in_docker() - -if INSIDE_DOCKER: - from swsscommon import swsscommon - REDIS_HOSTNAME = "localhost" - REDIS_PORT = 6379 - REDIS_TIMEOUT_USECS = 0 - - state_db = swsscommon.DBConnector(swsscommon.STATE_DB, - REDIS_HOSTNAME, - REDIS_PORT, - REDIS_TIMEOUT_USECS) - - # Subscribe to state table for SFP change notifications - sel = swsscommon.Select() - sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') - sel.addSelectable(sel_tbl) - +# 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 @@ -50,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 @@ -92,21 +73,6 @@ def get_presence(self, port_num): return True return False - - def get_presence_status_file_path(self, port_num): - # Check for invalid port_num - if port_num < self.port_start or port_num > self.port_end: - return "" - - try: - path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon4/qsfp%d_status" % (port_num+1) - reg_file = open(path) - except IOError as e: - print "Error: unable to open file: %s" % str(e) - return "" - - reg_file.close() - return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -195,18 +161,30 @@ def reset(self, port_num): def get_transceiver_change_event(self, timeout=0): phy_port_dict = {} - if INSIDE_DOCKER: + 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 - (state, c) = sel.select(timeout) - - if state == swsscommon.Select.TIMEOUT: - status = true - elif state != swsscommon.Select.OBJECT: - status = False - else: - (key, op, fvp) = sel_tbl.pop() - phy_port_dict[key] = op - - return status, phy_port_dict + elif state != self.db_sel_object: + status = False else: - return False, phy_port_dict + (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 906b919ee41..a730a7e41cb 100644 --- a/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py @@ -10,39 +10,13 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) -def determine_in_docker(): - with open('/proc/self/cgroup') as f: - lines = [line.rstrip('\n') for line in f] - for line in lines: - words = line.split('/') - if len(words) > 1 and words[1] == 'docker': - return True - - return False - -INSIDE_DOCKER = determine_in_docker() - -if INSIDE_DOCKER: - from swsscommon import swsscommon - REDIS_HOSTNAME = "localhost" - REDIS_PORT = 6379 - REDIS_TIMEOUT_USECS = 0 - - state_db = swsscommon.DBConnector(swsscommon.STATE_DB, - REDIS_HOSTNAME, - REDIS_PORT, - REDIS_TIMEOUT_USECS) - - # Subscribe to state table for SFP change notifications - sel = swsscommon.Select() - sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') - sel.addSelectable(sel_tbl) - +# 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 @@ -52,6 +26,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 @@ -62,7 +42,7 @@ def port_end(self): @property def qsfp_ports(self): - return range(QSFP_PORT_START, self.PORTS_IN_BLOCK + 1) + return range(self.QSFP_PORT_START, self.PORTS_IN_BLOCK + 1) @property def port_to_eeprom_mapping(self): @@ -95,21 +75,6 @@ def get_presence(self, port_num): return True return False - - def get_presence_status_file_path(self, port_num): - # Check for invalid port_num - if port_num < self.port_start or port_num > self.port_end: - return "" - - try: - path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp%d_status" % (port_num+1) - reg_file = open(path) - except IOError as e: - print "Error: unable to open file: %s" % str(e) - return "" - - reg_file.close() - return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -198,18 +163,30 @@ def reset(self, port_num): def get_transceiver_change_event(self, timeout=0): phy_port_dict = {} - if INSIDE_DOCKER: + 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 - (state, c) = sel.select(timeout) - - if state == swsscommon.Select.TIMEOUT: - status = true - elif state != swsscommon.Select.OBJECT: - status = False - else: - (key, op, fvp) = sel_tbl.pop() - phy_port_dict[key] = op - - return status, phy_port_dict + elif state != self.db_sel_object: + status = False else: - return False, phy_port_dict + (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 ee534ecfb61..2eb3d628ff9 100644 --- a/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2700-r0/plugins/sfputil.py @@ -10,38 +10,13 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) -def determine_in_docker(): - with open('/proc/self/cgroup') as f: - lines = [line.rstrip('\n') for line in f] - for line in lines: - words = line.split('/') - if len(words) > 1 and words[1] == 'docker': - return True - - return False - -INSIDE_DOCKER = determine_in_docker() - -if INSIDE_DOCKER: - from swsscommon import swsscommon - REDIS_HOSTNAME = "localhost" - REDIS_PORT = 6379 - REDIS_TIMEOUT_USECS = 0 - - state_db = swsscommon.DBConnector(swsscommon.STATE_DB, - REDIS_HOSTNAME, - REDIS_PORT, - REDIS_TIMEOUT_USECS) - - # Subscribe to state table for SFP change notifications - sel = swsscommon.Select() - sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') - sel.addSelectable(sel_tbl) - +# 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 @@ -50,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 @@ -92,21 +73,6 @@ def get_presence(self, port_num): return True return False - - def get_presence_status_file_path(self, port_num): - # Check for invalid port_num - if port_num < self.port_start or port_num > self.port_end: - return "" - - try: - path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp%d_status" % (port_num+1) - reg_file = open(path) - except IOError as e: - print "Error: unable to open file: %s" % str(e) - return "" - - reg_file.close() - return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -195,18 +161,30 @@ def reset(self, port_num): def get_transceiver_change_event(self, timeout=0): phy_port_dict = {} - if INSIDE_DOCKER: + 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 - (state, c) = sel.select(timeout) - - if state == swsscommon.Select.TIMEOUT: - status = true - elif state != swsscommon.Select.OBJECT: - status = False - else: - (key, op, fvp) = sel_tbl.pop() - phy_port_dict[key] = op - - return status, phy_port_dict + elif state != self.db_sel_object: + status = False else: - return False, phy_port_dict + (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 947e69c2ed1..d617135e3e8 100644 --- a/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2740-r0/plugins/sfputil.py @@ -10,38 +10,13 @@ except ImportError as e: raise ImportError("%s - required module not found" % str(e)) -def determine_in_docker(): - with open('/proc/self/cgroup') as f: - lines = [line.rstrip('\n') for line in f] - for line in lines: - words = line.split('/') - if len(words) > 1 and words[1] == 'docker': - return True - - return False - -INSIDE_DOCKER = determine_in_docker() - -if INSIDE_DOCKER: - from swsscommon import swsscommon - REDIS_HOSTNAME = "localhost" - REDIS_PORT = 6379 - REDIS_TIMEOUT_USECS = 0 - - state_db = swsscommon.DBConnector(swsscommon.STATE_DB, - REDIS_HOSTNAME, - REDIS_PORT, - REDIS_TIMEOUT_USECS) - - # Subscribe to state table for SFP change notifications - sel = swsscommon.Select() - sel_tbl = swsscommon.NotificationConsumer(state_db, 'TRANSCEIVER_NOTIFY') - sel.addSelectable(sel_tbl) - +# 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 @@ -50,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 @@ -92,21 +73,6 @@ def get_presence(self, port_num): return True return False - - def get_presence_status_file_path(self, port_num): - # Check for invalid port_num - if port_num < self.port_start or port_num > self.port_end: - return "" - - try: - path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp%d_status" % (port_num+1) - reg_file = open(path) - except IOError as e: - print "Error: unable to open file: %s" % str(e) - return "" - - reg_file.close() - return path def get_low_power_mode(self, port_num): # Check for invalid port_num @@ -195,18 +161,30 @@ def reset(self, port_num): def get_transceiver_change_event(self, timeout=0): phy_port_dict = {} - if INSIDE_DOCKER: + 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 - (state, c) = sel.select(timeout) - - if state == swsscommon.Select.TIMEOUT: - status = true - elif state != swsscommon.Select.OBJECT: - status = False - else: - (key, op, fvp) = sel_tbl.pop() - phy_port_dict[key] = op - - return status, phy_port_dict + elif state != self.db_sel_object: + status = False else: - return False, phy_port_dict + (key, op, fvp) = self.db_sel_tbl.pop() + phy_port_dict[key] = op + + return status, phy_port_dict + From 33b8e181c69c7397dc43a0e968d66626901ff760 Mon Sep 17 00:00:00 2001 From: Kebo Liu Date: Sat, 14 Jul 2018 14:45:17 +0300 Subject: [PATCH 4/4] remove blank --- device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py | 1 - 1 file changed, 1 deletion(-) 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 a730a7e41cb..c379155b82e 100644 --- a/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py +++ b/device/mellanox/x86_64-mlnx_msn2410-r0/plugins/sfputil.py @@ -21,7 +21,6 @@ class SfpUtil(SfpUtilBase): PORT_END = 55 PORTS_IN_BLOCK = 56 QSFP_PORT_START = 48 - EEPROM_OFFSET = 1 _port_to_eeprom_mapping = {}