forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 0
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
[Mellanox]Implement plugins for PSU and fan #23
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cdbb989
[plugins]add fan functions, add voltage, current, power for psu
3f6d774
[plugins]link fanutil.py and psuutil.py to those in 2700
2d6402a
[plugins]fix review comments
754be9d
[plugins]remove unnecessary code
f9dfb34
Merge branch 'plugins' of https://github.com/stephenxs/sonic-buildima…
fc51e86
[plugin]add thermal
0a9c732
[plugin]add symbol links for thermalutil for all SKUs
ab9186d
Merge pull request #25 from stephenxs/plugins-thermal
stephenxs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../x86_64-mlnx_msn2700-r0/plugins/fanutil.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../x86_64-mlnx_msn2700-r0/plugins/fanutil.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../x86_64-mlnx_msn2700-r0/plugins/fanutil.py |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../x86_64-mlnx_msn2700-r0/plugins/psuutil.py |
199 changes: 199 additions & 0 deletions
199
device/mellanox/x86_64-mlnx_msn2700-r0/plugins/fanutil.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
#!/usr/bin/env python | ||
|
||
############################################################################# | ||
# Mellanox | ||
# | ||
# Module contains an implementation of SONiC PSU Base API and | ||
# provides the PSUs status which are available in the platform | ||
# | ||
############################################################################# | ||
|
||
|
||
try: | ||
import os.path | ||
import syslog | ||
import subprocess | ||
from glob import glob | ||
from sonic_fan.fan_base import FanBase | ||
except ImportError as e: | ||
raise ImportError (str(e) + "- required module not found") | ||
|
||
def log_err(msg): | ||
syslog.openlog("fanutil") | ||
syslog.syslog(syslog.LOG_ERR, msg) | ||
syslog.closelog() | ||
|
||
class FanUtil(FanBase): | ||
"""Platform-specific FanUtil class""" | ||
|
||
PWM_MAX = 255 | ||
MAX_FAN_PER_DRAWER = 2 | ||
GET_HWSKU_CMD = "sonic-cfggen -d -v DEVICE_METADATA.localhost.hwsku" | ||
sku_without_fan_direction = ['ACS-MSN2010', 'ACS-MSN2100', 'ACS-MSN2410', 'ACS-MSN2700', 'Mellanox-SN2700', 'Mellanox-SN2700-D48C8', 'LS-SN2700', 'ACS-MSN2740'] | ||
sku_with_unpluggable_fan = ['ACS-MSN2010', 'ACS-MSN2100'] | ||
|
||
def __init__(self): | ||
FanBase.__init__(self) | ||
|
||
self.sku_name = self._get_sku_name() | ||
|
||
self.fan_path = "/var/run/hw-management/" | ||
if self.sku_name in self.sku_with_unpluggable_fan: | ||
self.fan_status = None | ||
self.unpluggable_fan = True | ||
else: | ||
self.fan_status = "thermal/fan{}_status" | ||
self.unpluggable_fan = False | ||
self.fan_get_speed = "thermal/fan{}_speed_get" | ||
self.fan_set_speed = "thermal/fan{}_speed_set" | ||
if self.sku_name in self.sku_without_fan_direction: | ||
self.fan_direction = None | ||
else: | ||
self.fan_direction = "system/fan_dir" | ||
|
||
self.fan_led_green = "led/led_fan*_green" | ||
self.num_of_fan, self.num_of_drawer = self._extract_num_of_fans_and_fan_drawers() | ||
|
||
def _get_sku_name(self): | ||
p = subprocess.Popen(self.GET_HWSKU_CMD, shell=True, stdout=subprocess.PIPE) | ||
out, err = p.communicate() | ||
return out.rstrip('\n') | ||
|
||
def _extract_num_of_fans_and_fan_drawers(self): | ||
# So far we don't have files representing the number of fans and drawers | ||
# The only way to retrieve the number is to count files. | ||
# for number of fans, we get it via couting the speed files. | ||
# for number of draws, we get it via couting the green led files. | ||
list_of_fan_speed = glob(self.fan_path + self.fan_get_speed.format("*")) | ||
num_of_fan = len(list_of_fan_speed) | ||
list_of_fan_leds = glob(self.fan_path + self.fan_led_green) | ||
num_of_drawer = len(list_of_fan_leds) | ||
|
||
return num_of_fan, num_of_drawer | ||
|
||
def _convert_fan_index_to_drawer_index(self, index): | ||
return (index + self.MAX_FAN_PER_DRAWER - 1) / self.MAX_FAN_PER_DRAWER | ||
|
||
def _read_file(self, file_pattern, index = 0): | ||
""" | ||
Reads the file of the fan | ||
|
||
:param file_pattern: The filename convention | ||
:param index: An integer, 1-based index of the fan of which to query status | ||
:return: int | ||
""" | ||
return_value = 0 | ||
try: | ||
with open(os.path.join(self.fan_path, file_pattern.format(index)), 'r') as file_to_read: | ||
return_value = int(file_to_read.read()) | ||
except IOError: | ||
log_err("Read file {} failed".format(self.fan_path + file_pattern.format(index))) | ||
return return_value | ||
|
||
return return_value | ||
|
||
def get_num_fans(self): | ||
""" | ||
Retrieves the number of FANs supported on the device | ||
|
||
:return: An integer, the number of FANs supported on the device | ||
""" | ||
return self.num_of_fan | ||
|
||
def get_status(self, index): | ||
""" | ||
Retrieves the operational status of FAN defined | ||
by index 1-based <index> | ||
|
||
:param index: An integer, 1-based index of the PSU of which to query status | ||
:return: Boolean, | ||
- True if FAN is running with some speed | ||
- False if FAN has stopped running | ||
""" | ||
if not self.get_presence(index): | ||
return False | ||
|
||
return self.get_speed(index) != 0 | ||
|
||
def get_presence(self, index): | ||
""" | ||
Retrieves the presence status of a FAN defined | ||
by 1-based index <index> | ||
|
||
:param index: An integer, 1-based index of the FAN of which to query status | ||
:return: Boolean, True if FAN is plugged, False if not | ||
""" | ||
if index > self.num_of_fan: | ||
raise RuntimeError("index ({}) shouldn't be greater than number of fans ({})".format(index, self.num_of_fan)) | ||
|
||
if self.unpluggable_fan: | ||
return True | ||
|
||
draw_index = self._convert_fan_index_to_drawer_index(index) | ||
presence = self._read_file(self.fan_status, draw_index) | ||
|
||
return presence != 0 | ||
|
||
def get_direction(self, index): | ||
""" | ||
Retrieves the airflow direction of a FAN defined | ||
by 1-based index <index> | ||
|
||
:param index: An integer, 1-based index of the FAN of which to query status | ||
:return: string, denoting FAN airflow direction | ||
Note: | ||
What Mellanox calls forward: | ||
Air flows from fans side to QSFP side, for example: MSN2700-CS2F | ||
which means intake in community | ||
What Mellanox calls reverse: | ||
Air flow from QSFP side to fans side, for example: MSN2700-CS2R | ||
which means exhaust in community | ||
According to hw-mgmt: | ||
1 stands for forward, in other words intake | ||
0 stands for reverse, in other words exhaust | ||
""" | ||
if not self.fan_direction: | ||
return self.FAN_DIRECTION_NOT_APPLICABLE | ||
|
||
if index > self.num_of_fan: | ||
raise RuntimeError("index ({}) shouldn't be greater than number of fans ({})".format(index, self.num_of_fan)) | ||
|
||
drawer_index = self._convert_fan_index_to_drawer_index(index) | ||
|
||
fan_dir_bits = self._read_file(self.fan_direction) | ||
fan_mask = 1 << drawer_index - 1 | ||
if fan_dir_bits & fan_mask: | ||
stephenxs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self.FAN_DIRECTION_INTAKE | ||
else: | ||
return self.FAN_DIRECTION_EXHAUST | ||
|
||
def get_speed(self, index): | ||
""" | ||
Retrieves the speed of a Front FAN in the tray in revolutions per minute defined | ||
by 1-based index <index> | ||
|
||
:param index: An integer, 1-based index of the FAN of which to query speed | ||
:return: integer, denoting front FAN speed | ||
""" | ||
speed = self._read_file(self.fan_get_speed, index) | ||
|
||
return speed | ||
|
||
def set_speed(self, val): | ||
""" | ||
Sets the speed of all the FANs to a value denoted by the duty-cycle percentage val | ||
|
||
:param val: An integer, <0-100> denoting FAN duty cycle percentage | ||
:return: Boolean, True if operation is successful, False if not | ||
""" | ||
status = True | ||
pwm = int(round(self.PWM_MAX*val/100.0)) | ||
|
||
try: | ||
with open(os.path.join(self.fan_path, self.fan_set_speed.format(1)), 'w') as fan_pwm: | ||
fan_pwm.write(str(pwm)) | ||
except (ValueError, IOError): | ||
log_err("Read file {} failed".format(self.fan_path + self.fan_set_speed.format(1))) | ||
status = False | ||
|
||
return status |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could 0 be a legal value in some case?