Skip to content

Commit 202c31e

Browse files
vboykoxqiluo-msft
authored andcommitted
[barefoot][platform] Support fans and thermal (#7004)
Add support for fans and thermals to sonic-platform package for Montara platform Signed-off-by: Volodymyr Boyko <[email protected]>
1 parent e700951 commit 202c31e

3 files changed

Lines changed: 191 additions & 0 deletions

File tree

platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/chassis.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from sonic_platform_base.chassis_base import ChassisBase
66
from sonic_platform.sfp import Sfp
77
from sonic_platform.psu import Psu
8+
from sonic_platform.fan_drawer import fan_drawer_list_get
9+
from sonic_platform.thermal import thermal_list_get
810
from eeprom import Eeprom
911
except ImportError as e:
1012
raise ImportError(str(e) + "- required module not found")
@@ -26,6 +28,9 @@ def __init__(self):
2628
psu = Psu(i)
2729
self._psu_list.append(psu)
2830

31+
self._fan_drawer_list = fan_drawer_list_get()
32+
self._thermal_list = thermal_list_get()
33+
2934
def get_name(self):
3035
"""
3136
Retrieves the name of the chassis
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
try:
2+
from sonic_platform.platform_thrift_client import thrift_try
3+
from sonic_platform_base.fan_drawer_base import FanDrawerBase
4+
from sonic_platform_base.fan_base import FanBase
5+
except ImportError as e:
6+
raise ImportError (str(e) + "- required module not found")
7+
8+
_MAX_FAN = 10
9+
10+
def _fan_info_get(fan_num, cb, default=None):
11+
def get_data(client):
12+
return client.pltfm_mgr.pltfm_mgr_fan_info_get(fan_num)
13+
fan_info = thrift_try(get_data)
14+
if fan_num == fan_info.fan_num:
15+
return cb(fan_info)
16+
if default is None:
17+
raise LookupError
18+
return default
19+
20+
def _fan_info_get_all():
21+
for fan_num in range(1, _MAX_FAN + 1):
22+
def get_data(client, fan_num=fan_num):
23+
return client.pltfm_mgr.pltfm_mgr_fan_info_get(fan_num)
24+
fan_info = thrift_try(get_data)
25+
if fan_info.fan_num == fan_num:
26+
yield fan_info
27+
28+
# Fan -> FanBase -> DeviceBase
29+
class Fan(FanBase):
30+
def __init__(self, num):
31+
self.__num = num
32+
33+
# FanBase interface methods:
34+
# returns speed in percents
35+
def get_speed(self):
36+
def cb(info): return info.percent
37+
return _fan_info_get(self.__num, cb, 0)
38+
39+
def set_speed(self, percent):
40+
def set_fan_speed(client):
41+
return client.pltfm_mgr.pltfm_mgr_fan_speed_set(fan, percent)
42+
return thrift_try(set_fan_speed)
43+
44+
# DeviceBase interface methods:
45+
def get_name(self):
46+
return f"counter-rotating-fan-{self.__num}"
47+
48+
def get_presence(self):
49+
return _fan_info_get(self.__num, lambda _: True, False)
50+
51+
def get_position_in_parent(self):
52+
return self.__num
53+
54+
def is_replaceable(self):
55+
return True
56+
57+
def get_status(self):
58+
return True
59+
60+
# FanDrawer -> FanDrawerBase -> DeviceBase
61+
class FanDrawer(FanDrawerBase):
62+
def __init__(self):
63+
# For now we return only present fans
64+
self._fan_list = [Fan(i.fan_num) for i in _fan_info_get_all()]
65+
66+
# DeviceBase interface methods:
67+
def get_name(self):
68+
return 'fantray'
69+
70+
def get_presence(self):
71+
return True
72+
73+
def get_status(self):
74+
return True
75+
76+
def fan_drawer_list_get():
77+
return [FanDrawer()]
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
try:
2+
import subprocess
3+
4+
from sonic_platform.bfn_extensions.platform_sensors import platform_sensors_get
5+
from sonic_platform_base.thermal_base import ThermalBase
6+
except ImportError as e:
7+
raise ImportError (str(e) + "- required module not found")
8+
9+
'''
10+
data argument is in "sensors -A -u" format, example:
11+
coretemp-isa-0000
12+
Package id 0:
13+
temp1_input: 37.000
14+
temp1_max: 82.000
15+
temp1_crit: 104.000
16+
temp1_crit_alarm: 0.000
17+
Core 0:
18+
temp2_input: 37.000
19+
...
20+
'''
21+
def _sensors_chip_parsed(data: str):
22+
def kv(line):
23+
k, v, *_ = [t.strip(': ') for t in line.split(':') if t] + ['']
24+
return k, v
25+
26+
chip, *data = data.strip().split('\n')
27+
chip = chip.strip(': ')
28+
29+
sensors = []
30+
for line in data:
31+
if not line.startswith(' '):
32+
sensor_label = line.strip(': ')
33+
sensors.append((sensor_label, {}))
34+
continue
35+
36+
if len(sensors) == 0:
37+
raise RuntimeError(f'invalid data to parse: {data}')
38+
39+
attr, value = kv(line)
40+
sensor_label, sensor_data = sensors[-1]
41+
sensor_data.update({attr: value})
42+
43+
return chip, dict(sensors)
44+
45+
'''
46+
Example of returned dict:
47+
{
48+
'coretemp-isa-0000': {
49+
'Core 1': { "temp1_input": 40, ... },
50+
'Core 2': { ... }
51+
}
52+
}
53+
'''
54+
def _sensors_get() -> dict:
55+
data = platform_sensors_get(['-A', '-u']) or ''
56+
data += subprocess.check_output("/usr/bin/sensors -A -u",
57+
shell=True, text=True)
58+
data = data.split('\n\n')
59+
data = [_sensors_chip_parsed(chip_data) for chip_data in data if chip_data]
60+
data = dict(data)
61+
return data
62+
63+
def _value_get(d: dict, key_prefix, key_suffix=''):
64+
for k, v in d.items():
65+
if k.startswith(key_prefix) and k.endswith(key_suffix):
66+
return v
67+
return None
68+
69+
# Thermal -> ThermalBase -> DeviceBase
70+
class Thermal(ThermalBase):
71+
def __init__(self, chip, label):
72+
self.__chip = chip
73+
self.__label = label
74+
self.__name = f"{chip}:{label}".lower().replace(' ', '-')
75+
76+
def __get(self, attr_prefix, attr_suffix):
77+
sensor_data = _sensors_get().get(self.__chip, {}).get(self.__label, {})
78+
value = _value_get(sensor_data, attr_prefix, attr_suffix)
79+
if value is not None: return value
80+
raise NotImplementedError
81+
82+
# ThermalBase interface methods:
83+
def get_temperature(self) -> float:
84+
return float(self.__get('temp', 'input'))
85+
86+
def get_high_threshold(self) -> float:
87+
return float(self.__get('temp', 'max'))
88+
89+
def get_high_critical_threshold(self) -> float:
90+
return float(self.__get('temp', 'crit'))
91+
92+
# DeviceBase interface methods:
93+
def get_name(self):
94+
return self.__name
95+
96+
def get_presence(self):
97+
return True
98+
99+
def get_status(self):
100+
return True
101+
102+
def thermal_list_get():
103+
l = []
104+
for chip, chip_data in _sensors_get().items():
105+
for sensor, sensor_data in chip_data.items():
106+
# add only temperature sensors
107+
if _value_get(sensor_data, "temp") is not None:
108+
l.append(Thermal(chip, sensor))
109+
return l

0 commit comments

Comments
 (0)