-
Notifications
You must be signed in to change notification settings - Fork 816
[sfpshow/sfputil] Enhance sfpshow and sfputil to behavior correctly on RJ45 ports #2111
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
Changes from all commits
2c385fd
e57ecd0
5e5e5bc
5285202
a425de4
6863fdd
2da251d
b622d5b
586af55
da447ce
cc540ed
d93bf37
79c3218
f21fc3e
8e11f59
09a891a
26bdd9c
942cb38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -267,6 +267,7 @@ | |
| 'voltage': 'Volts' | ||
| } | ||
|
|
||
| RJ45_PORT_TYPE = 'RJ45' | ||
|
|
||
| # Global platform-specific Chassis class instance | ||
| platform_chassis = None | ||
|
|
@@ -289,6 +290,34 @@ def is_sfp_present(port_name): | |
|
|
||
| return bool(presence) | ||
|
|
||
|
|
||
| # Below defined two flavors of functions to determin whether a port is a RJ45 port. | ||
| # They serve different types of SFP utilities. One type of SFP utility consume the | ||
| # info stored in the STATE_DB, these utilities shall call 'is_rj45_port_from_db' | ||
| # to judge the port type. Another type of utilities will call the platform API | ||
| # directly to access SFP, for them shall use 'is_rj45_port_from_api'. | ||
| def is_rj45_port_from_db(port_name, db): | ||
| intf_type = db.get(db.STATE_DB, 'TRANSCEIVER_INFO|{}'.format(port_name), 'type') | ||
| return intf_type == RJ45_PORT_TYPE | ||
|
|
||
|
|
||
| def is_rj45_port_from_api(port_name): | ||
|
Comment on lines
+299
to
+304
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @keboliu can put comments in the code why we need two different functions for getting the port type? Also when should one use each of them?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @prgeor please check the latest commits for the comments on the usage of the functions.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @keboliu as we discussed, lets have sfptuil always fetch the values by reading the eeprom and sfpshow utility use the DB to read the values. Hope this will be scoped and fixed in future PR. |
||
| physical_port = logical_port_to_physical_port_index(port_name) | ||
| sfp = platform_chassis.get_sfp(physical_port) | ||
|
|
||
| try: | ||
| port_type = sfp.get_transceiver_info()['type'] | ||
| except NotImplementedError: | ||
| click.echo("Not able to judge the port type due to get_transceiver_info not implemented!", err=True) | ||
| sys.exit(ERROR_NOT_IMPLEMENTED) | ||
|
|
||
| return port_type == RJ45_PORT_TYPE | ||
|
|
||
|
|
||
| def skip_if_port_is_rj45(port_name): | ||
| if is_rj45_port_from_api(port_name): | ||
| click.echo("This functionality is not applicable for RJ45 port {}.".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
| # ========================== Methods for formatting output ========================== | ||
|
|
||
| # Convert dict values to cli output string | ||
|
|
@@ -630,6 +659,11 @@ def eeprom(port, dump_dom, namespace): | |
| for physical_port in physical_port_list: | ||
| port_name = get_physical_port_name(logical_port_name, i, ganged) | ||
|
|
||
| if is_rj45_port_from_api(port_name): | ||
| output += "{}: SFP EEPROM is not applicable for RJ45 port\n".format(port_name) | ||
| output += '\n' | ||
| continue | ||
|
|
||
| try: | ||
| presence = platform_chassis.get_sfp(physical_port).get_presence() | ||
| except NotImplementedError: | ||
|
|
@@ -783,7 +817,10 @@ def fetch_error_status_from_platform_api(port): | |
| physical_port_list = logical_port_name_to_physical_port_list(logical_port_name) | ||
| port_name = get_physical_port_name(logical_port_name, 1, False) | ||
|
|
||
| output.append([port_name, output_dict.get(physical_port_list[0])]) | ||
| if is_rj45_port_from_api(logical_port_name): | ||
| output.append([port_name, "N/A"]) | ||
| else: | ||
| output.append([port_name, output_dict.get(physical_port_list[0])]) | ||
|
|
||
| return output | ||
|
|
||
|
|
@@ -806,15 +843,18 @@ def fetch_error_status_from_state_db(port, state_db): | |
| sorted_ports = natsort.natsorted(status) | ||
| output = [] | ||
| for port in sorted_ports: | ||
| statestring = status[port].get('status') | ||
| description = status[port].get('error') | ||
| if statestring == '1': | ||
| description = 'OK' | ||
| elif statestring == '0': | ||
| description = 'Unplugged' | ||
| elif description == 'N/A': | ||
| log.log_error("Inconsistent state found for port {}: state is {} but error description is N/A".format(port, statestring)) | ||
| description = 'Unknown state: {}'.format(statestring) | ||
| if is_rj45_port_from_db(port, state_db): | ||
| description = "N/A" | ||
| else: | ||
| statestring = status[port].get('status') | ||
| description = status[port].get('error') | ||
| if statestring == '1': | ||
| description = 'OK' | ||
| elif statestring == '0': | ||
| description = 'Unplugged' | ||
| elif description == 'N/A': | ||
| log.log_error("Inconsistent state found for port {}: state is {} but error description is N/A".format(port, statestring)) | ||
| description = 'Unknown state: {}'.format(statestring) | ||
|
|
||
| output.append([port, description]) | ||
|
|
||
|
|
@@ -879,24 +919,27 @@ def lpmode(port): | |
| click.echo("Error: No physical ports found for logical port '{}'".format(logical_port_name)) | ||
| return | ||
|
|
||
| if len(physical_port_list) > 1: | ||
| ganged = True | ||
| if is_rj45_port_from_api(logical_port_name): | ||
| output_table.append([logical_port_name, "N/A"]) | ||
| else: | ||
| if len(physical_port_list) > 1: | ||
| ganged = True | ||
|
|
||
| for physical_port in physical_port_list: | ||
| port_name = get_physical_port_name(logical_port_name, i, ganged) | ||
| for physical_port in physical_port_list: | ||
| port_name = get_physical_port_name(logical_port_name, i, ganged) | ||
|
|
||
| try: | ||
| lpmode = platform_chassis.get_sfp(physical_port).get_lpmode() | ||
| except NotImplementedError: | ||
| click.echo("This functionality is currently not implemented for this platform") | ||
| sys.exit(ERROR_NOT_IMPLEMENTED) | ||
| try: | ||
| lpmode = platform_chassis.get_sfp(physical_port).get_lpmode() | ||
| except NotImplementedError: | ||
| click.echo("This functionality is currently not implemented for this platform") | ||
| sys.exit(ERROR_NOT_IMPLEMENTED) | ||
|
|
||
| if lpmode: | ||
| output_table.append([port_name, "On"]) | ||
| else: | ||
| output_table.append([port_name, "Off"]) | ||
| if lpmode: | ||
| output_table.append([port_name, "On"]) | ||
| else: | ||
| output_table.append([port_name, "Off"]) | ||
|
|
||
| i += 1 | ||
| i += 1 | ||
|
|
||
| click.echo(tabulate(output_table, table_header, tablefmt='simple')) | ||
|
|
||
|
|
@@ -919,6 +962,10 @@ def fwversion(port_name): | |
| physical_port = logical_port_to_physical_port_index(port_name) | ||
| sfp = platform_chassis.get_sfp(physical_port) | ||
|
|
||
| if is_rj45_port_from_api(port_name): | ||
| click.echo("Show firmware version is not applicable for RJ45 port {}.".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
| try: | ||
| presence = sfp.get_presence() | ||
| except NotImplementedError: | ||
|
|
@@ -954,6 +1001,10 @@ def set_lpmode(logical_port, enable): | |
| click.echo("Error: No physical ports found for logical port '{}'".format(logical_port)) | ||
| return | ||
|
|
||
| if is_rj45_port_from_api(logical_port): | ||
| click.echo("{} low-power mode is not applicable for RJ45 port {}.".format("Enabling" if enable else "Disabling", logical_port)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
| if len(physical_port_list) > 1: | ||
| ganged = True | ||
|
|
||
|
|
@@ -1010,6 +1061,10 @@ def reset(port_name): | |
| click.echo("Error: No physical ports found for logical port '{}'".format(port_name)) | ||
| return | ||
|
|
||
| if is_rj45_port_from_api(port_name): | ||
| click.echo("Reset is not applicable for RJ45 port {}.".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
| if len(physical_port_list) > 1: | ||
| ganged = True | ||
|
|
||
|
|
@@ -1175,6 +1230,8 @@ def run(port_name, mode): | |
| click.echo("{}: SFP EEPROM not detected\n".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
| skip_if_port_is_rj45(port_name) | ||
|
|
||
| status = run_firmware(port_name, int(mode)) | ||
| if status != 1: | ||
| click.echo('Failed to run firmware in mode={}! CDB status: {}'.format(mode, status)) | ||
|
|
@@ -1192,6 +1249,8 @@ def commit(port_name): | |
| click.echo("{}: SFP EEPROM not detected\n".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
| skip_if_port_is_rj45(port_name) | ||
|
|
||
| status = commit_firmware(port_name) | ||
| if status != 1: | ||
| click.echo('Failed to commit firmware! CDB status: {}'.format(status)) | ||
|
|
@@ -1212,6 +1271,8 @@ def upgrade(port_name, filepath): | |
| click.echo("{}: SFP EEPROM not detected\n".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
| skip_if_port_is_rj45(port_name) | ||
|
|
||
| show_firmware_version(physical_port) | ||
|
|
||
| status = download_firmware(port_name, filepath) | ||
|
|
@@ -1246,6 +1307,8 @@ def download(port_name, filepath): | |
| click.echo("{}: SFP EEPROM not detected\n".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
| skip_if_port_is_rj45(port_name) | ||
|
|
||
| start = time.time() | ||
| status = download_firmware(port_name, filepath) | ||
| if status == 1: | ||
|
|
@@ -1266,6 +1329,8 @@ def unlock(port_name, password): | |
| physical_port = logical_port_to_physical_port_index(port_name) | ||
| sfp = platform_chassis.get_sfp(physical_port) | ||
|
|
||
| skip_if_port_is_rj45(port_name) | ||
|
|
||
| if not is_sfp_present(port_name): | ||
| click.echo("{}: SFP EEPROM not detected\n".format(port_name)) | ||
| sys.exit(EXIT_FAIL) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.