2020-10-03 15:46:21 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
try:
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
2021-02-11 19:53:15 -06:00
|
|
|
|
|
|
|
import tempfile
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from copy import copy
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
sys.path.append(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
from .platform_thrift_client import ThriftClient
|
2020-10-05 12:50:03 -05:00
|
|
|
from .platform_thrift_client import thrift_try
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
from sonic_platform_base.sfp_base import SfpBase
|
|
|
|
from sonic_platform_base.sonic_sfp.sfputilbase import SfpUtilBase
|
|
|
|
except ImportError as e:
|
|
|
|
raise ImportError (str(e) + "- required module not found")
|
|
|
|
|
|
|
|
class SfpUtil(SfpUtilBase):
|
|
|
|
"""Platform-specific SfpUtil class"""
|
|
|
|
|
|
|
|
PORT_START = 1
|
|
|
|
PORT_END = 0
|
|
|
|
PORTS_IN_BLOCK = 0
|
|
|
|
QSFP_PORT_START = 1
|
|
|
|
QSFP_PORT_END = 0
|
|
|
|
EEPROM_OFFSET = 0
|
|
|
|
QSFP_CHECK_INTERVAL = 4
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port_start(self):
|
|
|
|
self.update_port_info()
|
|
|
|
return self.PORT_START
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port_end(self):
|
|
|
|
self.update_port_info()
|
|
|
|
return self.PORT_END
|
|
|
|
|
|
|
|
@property
|
|
|
|
def qsfp_ports(self):
|
|
|
|
self.update_port_info()
|
|
|
|
return range(self.QSFP_PORT_START, self.PORTS_IN_BLOCK + 1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port_to_eeprom_mapping(self):
|
2021-01-04 17:00:48 -06:00
|
|
|
print("dependency on sysfs has been removed")
|
2020-10-03 15:46:21 -05:00
|
|
|
raise Exception()
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.ready = False
|
|
|
|
self.phy_port_dict = {'-1': 'system_not_ready'}
|
|
|
|
self.phy_port_cur_state = {}
|
|
|
|
self.qsfp_interval = self.QSFP_CHECK_INTERVAL
|
|
|
|
|
|
|
|
SfpUtilBase.__init__(self)
|
|
|
|
|
|
|
|
def update_port_info(self):
|
2020-10-05 12:50:03 -05:00
|
|
|
def qsfp_max_port_get(client):
|
|
|
|
return client.pltfm_mgr.pltfm_mgr_qsfp_get_max_port();
|
|
|
|
|
2020-10-03 15:46:21 -05:00
|
|
|
if self.QSFP_PORT_END == 0:
|
2020-10-05 12:50:03 -05:00
|
|
|
self.QSFP_PORT_END = thrift_try(qsfp_max_port_get)
|
|
|
|
self.PORT_END = self.QSFP_PORT_END
|
|
|
|
self.PORTS_IN_BLOCK = self.QSFP_PORT_END
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def get_presence(self, port_num):
|
|
|
|
# Check for invalid port_num
|
|
|
|
if port_num < self.port_start or port_num > self.port_end:
|
|
|
|
return False
|
|
|
|
|
|
|
|
presence = False
|
|
|
|
|
2020-10-05 12:50:03 -05:00
|
|
|
def qsfp_presence_get(client):
|
|
|
|
return client.pltfm_mgr.pltfm_mgr_qsfp_presence_get(port_num)
|
|
|
|
|
2020-10-03 15:46:21 -05:00
|
|
|
try:
|
2020-10-05 12:50:03 -05:00
|
|
|
presence = thrift_try(qsfp_presence_get)
|
2020-10-03 15:46:21 -05:00
|
|
|
except Exception as e:
|
2021-01-04 17:00:48 -06:00
|
|
|
print( e.__doc__)
|
|
|
|
print(e.message)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
return presence
|
|
|
|
|
|
|
|
def get_low_power_mode(self, port_num):
|
|
|
|
# Check for invalid port_num
|
|
|
|
if port_num < self.port_start or port_num > self.port_end:
|
|
|
|
return False
|
|
|
|
|
2020-10-05 12:50:03 -05:00
|
|
|
def qsfp_lpmode_get(client):
|
|
|
|
return client.pltfm_mgr.pltfm_mgr_qsfp_lpmode_get(port_num)
|
|
|
|
|
|
|
|
lpmode = thrift_try(qsfp_lpmode_get)
|
|
|
|
|
2020-10-03 15:46:21 -05:00
|
|
|
return lpmode
|
|
|
|
|
|
|
|
def set_low_power_mode(self, port_num, lpmode):
|
|
|
|
# Check for invalid port_num
|
|
|
|
if port_num < self.port_start or port_num > self.port_end:
|
|
|
|
return False
|
|
|
|
|
2020-10-05 12:50:03 -05:00
|
|
|
def qsfp_lpmode_set(client):
|
|
|
|
return client.pltfm_mgr.pltfm_mgr_qsfp_lpmode_set(port_num, lpmode)
|
|
|
|
|
|
|
|
status = thrift_try(qsfp_lpmode_set)
|
|
|
|
|
2020-10-03 15:46:21 -05:00
|
|
|
return (status == 0)
|
|
|
|
|
|
|
|
def reset(self, port_num):
|
|
|
|
# Check for invalid port_num
|
|
|
|
if port_num < self.port_start or port_num > self.port_end:
|
|
|
|
return False
|
|
|
|
|
2020-10-05 12:50:03 -05:00
|
|
|
def qsfp_reset(client):
|
2020-10-03 15:46:21 -05:00
|
|
|
client.pltfm_mgr.pltfm_mgr_qsfp_reset(port_num, True)
|
2020-10-05 12:50:03 -05:00
|
|
|
return client.pltfm_mgr.pltfm_mgr_qsfp_reset(port_num, False)
|
|
|
|
|
2021-02-11 20:13:53 -06:00
|
|
|
err = thrift_try(qsfp_reset)
|
2020-10-05 12:50:03 -05:00
|
|
|
|
2021-02-11 20:13:53 -06:00
|
|
|
return not err
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def check_transceiver_change(self):
|
|
|
|
if not self.ready:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.phy_port_dict = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
client = ThriftClient().open()
|
|
|
|
except Exception:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get presence of each SFP
|
|
|
|
for port in range(self.port_start, self.port_end + 1):
|
|
|
|
try:
|
|
|
|
sfp_resent = client.pltfm_mgr.pltfm_mgr_qsfp_presence_get(port)
|
|
|
|
except Exception:
|
|
|
|
sfp_resent = False
|
|
|
|
sfp_state = '1' if sfp_resent else '0'
|
|
|
|
|
|
|
|
if port in self.phy_port_cur_state:
|
|
|
|
if self.phy_port_cur_state[port] != sfp_state:
|
|
|
|
self.phy_port_dict[port] = sfp_state
|
|
|
|
else:
|
|
|
|
self.phy_port_dict[port] = sfp_state
|
|
|
|
|
|
|
|
# Update port current state
|
|
|
|
self.phy_port_cur_state[port] = sfp_state
|
|
|
|
|
|
|
|
client.close()
|
|
|
|
|
|
|
|
def get_transceiver_change_event(self, timeout=0):
|
|
|
|
forever = False
|
|
|
|
if timeout == 0:
|
|
|
|
forever = True
|
|
|
|
elif timeout > 0:
|
|
|
|
timeout = timeout / float(1000) # Convert to secs
|
|
|
|
else:
|
2021-01-04 17:00:48 -06:00
|
|
|
print("get_transceiver_change_event:Invalid timeout value", timeout)
|
2020-10-03 15:46:21 -05:00
|
|
|
return False, {}
|
|
|
|
|
|
|
|
while forever or timeout > 0:
|
|
|
|
if not self.ready:
|
|
|
|
try:
|
|
|
|
with ThriftClient(): pass
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.ready = True
|
|
|
|
self.phy_port_dict = {}
|
|
|
|
break
|
|
|
|
elif self.qsfp_interval == 0:
|
|
|
|
self.qsfp_interval = self.QSFP_CHECK_INTERVAL
|
|
|
|
|
|
|
|
# Process transceiver plug-in/out event
|
|
|
|
self.check_transceiver_change()
|
|
|
|
|
|
|
|
# Break if tranceiver state has changed
|
|
|
|
if bool(self.phy_port_dict):
|
|
|
|
break
|
|
|
|
|
|
|
|
if timeout:
|
|
|
|
timeout -= 1
|
|
|
|
|
|
|
|
if self.qsfp_interval:
|
|
|
|
self.qsfp_interval -= 1
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
return self.ready, self.phy_port_dict
|
|
|
|
|
2021-02-11 19:53:15 -06:00
|
|
|
@contextmanager
|
|
|
|
def eeprom_action(self):
|
|
|
|
u = copy(self)
|
|
|
|
with tempfile.NamedTemporaryFile() as f:
|
|
|
|
u.eeprom_path = f.name
|
|
|
|
yield u
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-02-11 19:53:15 -06:00
|
|
|
def _sfp_eeprom_present(self, client_eeprompath, offset):
|
|
|
|
return client_eeprompath and super(SfpUtil, self)._sfp_eeprom_present(client_eeprompath, offset)
|
|
|
|
|
|
|
|
def _get_port_eeprom_path(self, port_num, devid):
|
2020-10-05 12:50:03 -05:00
|
|
|
def qsfp_info_get(client):
|
|
|
|
return client.pltfm_mgr.pltfm_mgr_qsfp_info_get(port_num)
|
|
|
|
|
|
|
|
if self.get_presence(port_num):
|
|
|
|
eeprom_hex = thrift_try(qsfp_info_get)
|
|
|
|
eeprom_raw = bytearray.fromhex(eeprom_hex)
|
2021-02-11 19:53:15 -06:00
|
|
|
with open(self.eeprom_path, 'wb') as eeprom_cache:
|
|
|
|
eeprom_cache.write(eeprom_raw)
|
|
|
|
return self.eeprom_path
|
2020-10-03 15:46:21 -05:00
|
|
|
|
2021-02-11 19:53:15 -06:00
|
|
|
return None
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
class Sfp(SfpBase):
|
|
|
|
"""Platform-specific Sfp class"""
|
|
|
|
|
|
|
|
sfputil = SfpUtil()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def port_start():
|
|
|
|
return Sfp.sfputil.port_start
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def port_end():
|
|
|
|
return Sfp.sfputil.port_end
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def qsfp_ports():
|
|
|
|
return Sfp.sfputil.qsfp_ports()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_transceiver_change_event(timeout=0):
|
|
|
|
return Sfp.sfputil.get_transceiver_change_event()
|
|
|
|
|
|
|
|
def __init__(self, port_num):
|
|
|
|
self.port_num = port_num
|
|
|
|
SfpBase.__init__(self)
|
|
|
|
|
|
|
|
def get_presence(self):
|
2021-02-11 19:53:15 -06:00
|
|
|
with Sfp.sfputil.eeprom_action() as u:
|
|
|
|
return u.get_presence(self.port_num)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def get_lpmode(self):
|
2021-02-11 19:53:15 -06:00
|
|
|
with Sfp.sfputil.eeprom_action() as u:
|
|
|
|
return u.get_low_power_mode(self.port_num)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def set_lpmode(self, lpmode):
|
2021-02-11 19:53:15 -06:00
|
|
|
with Sfp.sfputil.eeprom_action() as u:
|
|
|
|
return u.set_low_power_mode(self.port_num, lpmode)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
return Sfp.sfputil.reset(self.port_num)
|
|
|
|
|
|
|
|
def get_transceiver_info(self):
|
2021-02-11 19:53:15 -06:00
|
|
|
with Sfp.sfputil.eeprom_action() as u:
|
|
|
|
return u.get_transceiver_info_dict(self.port_num)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def get_transceiver_bulk_status(self):
|
2021-02-11 19:53:15 -06:00
|
|
|
with Sfp.sfputil.eeprom_action() as u:
|
|
|
|
return u.get_transceiver_dom_info_dict(self.port_num)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def get_transceiver_threshold_info(self):
|
2021-02-11 19:53:15 -06:00
|
|
|
with Sfp.sfputil.eeprom_action() as u:
|
|
|
|
return u.get_transceiver_dom_threshold_info_dict(self.port_num)
|
2020-10-03 15:46:21 -05:00
|
|
|
|
|
|
|
def get_change_event(self, timeout=0):
|
|
|
|
return Sfp.get_transceiver_change_event(timeout)
|
2021-06-04 11:48:57 -05:00
|
|
|
|
|
|
|
def sfp_list_get():
|
|
|
|
sfp_list = []
|
|
|
|
for index in range(Sfp.port_start(), Sfp.port_end() + 1):
|
|
|
|
sfp_node = Sfp(index)
|
|
|
|
sfp_list.append(sfp_node)
|
|
|
|
return sfp_list
|