1111SFP_TYPE = "SFP"
1212QSFP_TYPE = "QSFP"
1313QSFP_DD_TYPE = "QSFP_DD"
14+ EEPROM_PAGE_SIZE = 128
1415
16+ try :
17+ from thrift .Thrift import TApplicationException
18+
19+ def cached_num_bytes_get (client ):
20+ return client .pltfm_mgr .pltfm_mgr_qsfp_cached_num_bytes_get (1 , 0 , 0 , 0 )
21+ thrift_try (cached_num_bytes_get , 1 )
22+ EEPROM_CACHED_API_SUPPORT = True
23+ except TApplicationException as e :
24+ EEPROM_CACHED_API_SUPPORT = False
1525
1626class Sfp (SfpOptoeBase ):
1727 """
1828 BFN Platform-specific SFP class
1929 """
2030
21- SFP_EEPROM_PATH = "/var/run/platform/sfp/"
22-
2331 def __init__ (self , port_num ):
2432 SfpOptoeBase .__init__ (self )
2533 self .index = port_num
2634 self .port_num = port_num
2735 self .sfp_type = QSFP_TYPE
36+ self .SFP_EEPROM_PATH = "/var/run/platform/sfp/"
2837
29- if not os . path . exists ( self . SFP_EEPROM_PATH ) :
30- try :
31- os . makedirs ( self . SFP_EEPROM_PATH )
32- except OSError as e :
33- if e . errno != errno . EEXIST :
34- raise
35-
36- self .eeprom_path = self .SFP_EEPROM_PATH + "sfp{}-eeprom-cache" .format (self .index )
38+ if not EEPROM_CACHED_API_SUPPORT :
39+ if not os . path . exists ( self . SFP_EEPROM_PATH ) :
40+ try :
41+ os . makedirs ( self . SFP_EEPROM_PATH )
42+ except OSError as e :
43+ if e . errno != errno . EEXIST :
44+ raise
45+ self .eeprom_path = self .SFP_EEPROM_PATH + "sfp{}-eeprom-cache" .format (self .index )
3746
3847 def get_presence (self ):
3948 """
@@ -47,7 +56,7 @@ def qsfp_presence_get(client):
4756 try :
4857 presence = thrift_try (qsfp_presence_get )
4958 except Exception as e :
50- print ( e .__doc__ )
59+ print (e .__doc__ )
5160 print (e .message )
5261
5362 return presence
@@ -75,14 +84,31 @@ def get_eeprom_path(self):
7584 def qsfp_info_get (client ):
7685 return client .pltfm_mgr .pltfm_mgr_qsfp_info_get (self .index )
7786
78- if self .get_presence ():
79- eeprom_hex = thrift_try (qsfp_info_get )
80- eeprom_raw = bytearray .fromhex (eeprom_hex )
81- with open (self .eeprom_path , 'wb' ) as fp :
82- fp .write (eeprom_raw )
83- return self .eeprom_path
87+ eeprom_hex = thrift_try (qsfp_info_get )
88+ eeprom_raw = bytearray .fromhex (eeprom_hex )
89+ with open (self .eeprom_path , 'wb' ) as fp :
90+ fp .write (eeprom_raw )
91+ return self .eeprom_path
92+
93+ def read_eeprom (self , offset , num_bytes ):
94+ if not self .get_presence ():
95+ return None
96+
97+ if not EEPROM_CACHED_API_SUPPORT :
98+ return super ().read_eeprom (offset , num_bytes )
99+
100+ def cached_num_bytes_get (page , offset , num_bytes ):
101+ def qsfp_cached_num_bytes_get (client ):
102+ return client .pltfm_mgr .pltfm_mgr_qsfp_cached_num_bytes_get (self .index , page , offset , num_bytes )
103+ return bytearray .fromhex (thrift_try (qsfp_cached_num_bytes_get ))
104+
105+ page_offset = offset % EEPROM_PAGE_SIZE
106+ if page_offset + num_bytes > EEPROM_PAGE_SIZE :
107+ curr_page_num_bytes_left = EEPROM_PAGE_SIZE - page_offset
108+ curr_page_bytes = cached_num_bytes_get (offset // EEPROM_PAGE_SIZE , page_offset , curr_page_num_bytes_left )
109+ return curr_page_bytes + self .read_eeprom (offset + curr_page_num_bytes_left , num_bytes - curr_page_num_bytes_left )
84110
85- return None
111+ return cached_num_bytes_get ( offset // EEPROM_PAGE_SIZE , page_offset , num_bytes )
86112
87113 def write_eeprom (self , offset , num_bytes , write_buffer ):
88114 # Not supported at the moment
0 commit comments