Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read/write module EEPROM via SDK provided sys fs #152

Closed
wants to merge 1 commit into from

Conversation

Junchao-Mellanox
Copy link
Owner

Why I did it

Currently, implementation:

  • Reading EEPROM: via ethtool
  • Writing EEPROM: via mlxreg tool.

The problem is that spawning new process of ethtool/mlxreg is very slow. To optimize the performance, this PR will use SDK provided sys fs for EEPROM reading/writing.

How I did it

Replace the logic of sfp.read_eeprom and sfp.write_eeprom by SDK sys fs.

How to verify it

Manual test
Unit test cases (new test case added)
sonic-mgmt regression

Which release branch to backport (provide reason below if selected)

  • 201811
  • 201911
  • 202006
  • 202012
  • 202106
  • 202111
  • 202205

Description for the changelog

Ensure to add label/tag for the feature raised. example - PR#2174 where, Generic Config and Update feature has been labelled as GCU.

Link to config_db schema for YANG module changes

A picture of a cute animal (not mandatory but encouraged)

@fastiuk
Copy link

fastiuk commented Oct 14, 2022

I am reviewing your PR. Will provide feedback in the nearest hour

@@ -23,9 +23,10 @@
#############################################################################

try:
import ctypes
import glob
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see you are using it in your code. Remove it please

Comment on lines 130 to 131
SFP_PAGE_SIZE = 256 # page size of page0h
SFP_UPPER_PAGE_OFFSET = 128 # page size of other pages
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional:
IMO it is easier to read when comments are aligned.

SFP_PAGE_SIZE = 256          # page size of page0h
SFP_UPPER_PAGE_OFFSET = 128  # page size of other pages

return bytearray(content)
except (OSError, IOError) as e:
if log_on_error:
logger.log_error('Failed to read sfp={} EEPROM page={}, page_offset={}, size={}, offset={}, exception = {}'.format(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use f-strings instead of format. It is linear and you can see all variables you are printing in place. Change it everywhere in the new code.
  2. Use logger.exception

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. will fix
  2. logger is an instance of sonic_py_common.logger, it has no method "exception"

Comment on lines 345 to 348
logger.log_error('Failed to read sfp={} EEPROM page={}, page_offset={}, size={}, offset={}, errno = {}'.format(
self.sdk_index, page, page_offset, num_bytes, offset, os.strerror(ctypes.get_errno())))
return None
return bytearray(content)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. let's minimize exit points
    1.1. If if ctypes.get_errno() != 0 raise an exception. You will be able to handle them together with (OSError, IOError)
    1.2. return bytearray(content) - move it after try-except
    In conclusion: you will have two exit points here in regards how many errors you handle. If something bad happen you will exit in exception, otherwise you will return data after try-except block.
  2. Another story is log duplication. Logs are the same. Leave it in the exception only.

Comment on lines 383 to 390
if ret != num_bytes:
logger.log_error('Failed to write EEPROM data sfp={} EEPROM page={}, page_offset={}, size={}, offset={}, data = {}, ret = {}'.format(
self.sdk_index, page, page_offset, num_bytes, offset, ''.join('{:02x}'.format(x) for x in write_buffer), ret))
return False
if ctypes.get_errno() != 0:
logger.log_error('Failed to write EEPROM data sfp={} EEPROM page={}, page_offset={}, size={}, offset={}, data = {}, errno = {}'.format(
self.sdk_index, page, page_offset, num_bytes, offset, ''.join('{:02x}'.format(x) for x in write_buffer), os.strerror(ctypes.get_errno())))
return False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here. 4 exit points, very hard to read. A lot of duplications in the comments. Use the same advice I provided for the "read" function.

return None
return self._sfp_type_str

def _is_limited_eeprom(self, page, page_offset, num_bytes):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_is_limited_eeprom - limited doesn't explain what exactly is limited.
It is better to name it _is_read_only or _is_writable or _is_write_protected (Used to name corresponding EEPROM chip pin)

device_address = (offset - SFP_PAGE_SIZE) % SFP_UPPER_PAGE_OFFSET + SFP_UPPER_PAGE_OFFSET
return self._read_eeprom(offset, num_bytes)

def _read_eeprom(self, offset, num_bytes, log_on_error=True):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not the best place to put EEPROM API.
This API can be reusable for other EEPROM devices not only for SFP.
I would put this API into another module like eeprom.py and use it here.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only for SFP EEPROM reading. eeprom.py is for system EEPROM.

Junchao-Mellanox pushed a commit that referenced this pull request Nov 4, 2022
…n] update submodule head (sonic-net#12594)

linkmgrd:
* 8e9e49b 2022-11-02 | [active-standby][active-active] update link prober stats updating frequency to 30s (#152) (HEAD -> 202205) [Jing Zhang]
* 1ad3f18 2022-11-01 | [Active-Active] periodically re-sync soc side admin forwarding state  (#151) [Jing Zhang]
* cfcdb76 2022-11-01 | [202205] incrementing icmp buffer size (#150) [Jing Zhang]

swss:
* ac7570a 2022-11-03 | [Dynamic buffer calculation][Mellanox] Enhance the logic to identify buffer pools and profiles (sonic-net#2498) (HEAD -> 202205) [Stephen Sun]

swss-common:
* 300fc8f 2022-10-31 | [sonic-db-cli] Fix sonic-db-cli crash when database config file not ready issue. (sonic-net#701) (HEAD -> 202205) [Hua Liu]

platform-daemon:
* cd1608a 2022-10-28 | [ycabled] add support for detach mode in 'active-active' topology (sonic-net#309) (HEAD -> 202205) [vdahiya12]
* 2a5f0f4 2022-10-27 | Added filtering logic to send filtered fields from DB event (sonic-net#307) [mihirpat1]

platform-common:
* b521882 2022-10-30 |  CmisApi::get_application_advertisement catch AttributeError as well (sonic-net#316) (HEAD -> 202205) [Stephen Sun]

Signed-off-by: Ying Xie <ying.xie@microsoft.com>

Signed-off-by: Ying Xie <ying.xie@microsoft.com>
@Junchao-Mellanox Junchao-Mellanox deleted the sysfs_eeprom branch November 4, 2022 07:46
Junchao-Mellanox pushed a commit that referenced this pull request Nov 11, 2022
[sonic-linkmgrd][master] submodule update

b3501d2 Jing Zhang Wed Nov 2 22:22:45 2022 -0700 [active-standby][active-active] update link prober stats updating frequency to 30s (#152)
5d546ec Jing Zhang Tue Nov 1 16:12:17 2022 -0700 [202205] incrementing icmp buffer size (#150)
76b128a Jing Zhang Tue Nov 1 12:06:21 2022 -0700 [Active-Active] periodically re-sync soc side admin forwarding state (#151)

sign-off: Jing Zhang zhangjing@microsoft.com
Junchao-Mellanox pushed a commit that referenced this pull request Jan 12, 2023
[202012][sonic-linkmgrd] update submodule

cf7274a Jing Zhang Mon Nov 7 14:50:40 2022 -0800 [active-standby][active-active] update link prober stats updating frequency to 30s (#152) (#155)

sign-off: Jing Zhang zhangjing@microsoft.com
Junchao-Mellanox pushed a commit that referenced this pull request Jun 5, 2024
…ically (sonic-net#19169)

#### Why I did it
src/sonic-restapi
```
* 4a564ba - (HEAD -> master, origin/master, origin/HEAD) Fix buster-backports debian mirror issue. (#152) (4 days ago) [Liu Shilong]
```
#### How I did it
#### How to verify it
#### Description for the changelog
Junchao-Mellanox pushed a commit that referenced this pull request Jul 5, 2024
…ically (sonic-net#19166)

#### Why I did it
src/sonic-restapi
```
* 5e39692 - (HEAD -> 202311, origin/202311) Fix buster-backports debian mirror issue. (#152) (4 hours ago) [Liu Shilong]
```
#### How I did it
#### How to verify it
#### Description for the changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants