1 /**
2 * Copyright 2017 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "config.h"
17
18 #include "dbuspassive.hpp"
19
20 #include "dbushelper_interface.hpp"
21 #include "dbuspassiveredundancy.hpp"
22 #include "dbusutil.hpp"
23 #include "util.hpp"
24
25 #include <sdbusplus/bus.hpp>
26
27 #include <chrono>
28 #include <cmath>
29 #include <memory>
30 #include <mutex>
31 #include <string>
32 #include <variant>
33
34 namespace pid_control
35 {
36
createDbusPassive(sdbusplus::bus_t & bus,const std::string & type,const std::string & id,std::unique_ptr<DbusHelperInterface> helper,const conf::SensorConfig * info,const std::shared_ptr<DbusPassiveRedundancy> & redundancy)37 std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
38 sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
39 std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info,
40 const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
41 {
42 if (helper == nullptr)
43 {
44 return nullptr;
45 }
46 if (!validType(type))
47 {
48 return nullptr;
49 }
50
51 /* Need to get the scale and initial value */
52 /* service == busname */
53 std::string path;
54 if (info->readPath.empty())
55 {
56 path = getSensorPath(type, id);
57 }
58 else
59 {
60 path = info->readPath;
61 }
62
63 SensorProperties settings;
64 bool failed;
65
66 try
67 {
68 std::string service = helper->getService(sensorintf, path);
69
70 helper->getProperties(service, path, &settings);
71 failed = helper->thresholdsAsserted(service, path);
72 }
73 catch (const std::exception& e)
74 {
75 return nullptr;
76 }
77
78 /* if these values are zero, they're ignored. */
79 if (info->ignoreDbusMinMax)
80 {
81 settings.min = 0;
82 settings.max = 0;
83 }
84
85 settings.unavailableAsFailed = info->unavailableAsFailed;
86
87 return std::make_unique<DbusPassive>(bus, type, id, std::move(helper),
88 settings, failed, path, redundancy);
89 }
90
DbusPassive(sdbusplus::bus_t & bus,const std::string & type,const std::string & id,std::unique_ptr<DbusHelperInterface> helper,const SensorProperties & settings,bool failed,const std::string & path,const std::shared_ptr<DbusPassiveRedundancy> & redundancy)91 DbusPassive::DbusPassive(
92 sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
93 std::unique_ptr<DbusHelperInterface> helper,
94 const SensorProperties& settings, bool failed, const std::string& path,
95 const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
96 ReadInterface(), _signal(bus, getMatch(path), dbusHandleSignal, this),
97 _id(id), _helper(std::move(helper)), _failed(failed), path(path),
98 redundancy(redundancy)
99
100 {
101 _scale = settings.scale;
102 _min = settings.min * std::pow(10.0, _scale);
103 _max = settings.max * std::pow(10.0, _scale);
104 _available = settings.available;
105 _unavailableAsFailed = settings.unavailableAsFailed;
106
107 // Cache this type knowledge, to avoid repeated string comparison
108 _typeMargin = (type == "margin");
109 _typeFan = (type == "fan");
110
111 // Force value to be stored, otherwise member would be uninitialized
112 updateValue(settings.value, true);
113 }
114
read(void)115 ReadReturn DbusPassive::read(void)
116 {
117 std::lock_guard<std::mutex> guard(_lock);
118
119 ReadReturn r = {_value, _updated, _unscaled};
120
121 return r;
122 }
123
setValue(double value,double unscaled)124 void DbusPassive::setValue(double value, double unscaled)
125 {
126 std::lock_guard<std::mutex> guard(_lock);
127
128 _value = value;
129 _unscaled = unscaled;
130 _updated = std::chrono::high_resolution_clock::now();
131 }
132
setValue(double value)133 void DbusPassive::setValue(double value)
134 {
135 // First param is scaled, second param is unscaled, assume same here
136 setValue(value, value);
137 }
138
getFailed(void) const139 bool DbusPassive::getFailed(void) const
140 {
141 if (redundancy)
142 {
143 const std::set<std::string>& failures = redundancy->getFailed();
144 if (failures.find(path) != failures.end())
145 {
146 return true;
147 }
148 }
149
150 /*
151 * Unavailable thermal sensors, who are not present or
152 * power-state-not-matching, should not trigger the failSafe mode. For
153 * example, when a system stays at a powered-off state, its CPU Temp
154 * sensors will be unavailable, these unavailable sensors should not be
155 * treated as failed and trigger failSafe.
156 * This is important for systems whose Fans are always on.
157 */
158 if (!_typeFan && !_available && !_unavailableAsFailed)
159 {
160 return false;
161 }
162
163 // If a reading has came in,
164 // but its value bad in some way (determined by sensor type),
165 // indicate this sensor has failed,
166 // until another value comes in that is no longer bad.
167 // This is different from the overall _failed flag,
168 // which is set and cleared by other causes.
169 if (_badReading)
170 {
171 return true;
172 }
173
174 // If a reading has came in, and it is not a bad reading,
175 // but it indicates there is no more thermal margin left,
176 // that is bad, something is wrong with the PID loops,
177 // they are not cooling the system, enable failsafe mode also.
178 if (_marginHot)
179 {
180 return true;
181 }
182
183 return _failed || !_available || !_functional;
184 }
185
setFailed(bool value)186 void DbusPassive::setFailed(bool value)
187 {
188 _failed = value;
189 }
190
setFunctional(bool value)191 void DbusPassive::setFunctional(bool value)
192 {
193 _functional = value;
194 }
195
setAvailable(bool value)196 void DbusPassive::setAvailable(bool value)
197 {
198 _available = value;
199 }
200
getScale(void)201 int64_t DbusPassive::getScale(void)
202 {
203 return _scale;
204 }
205
getID(void)206 std::string DbusPassive::getID(void)
207 {
208 return _id;
209 }
210
getMax(void)211 double DbusPassive::getMax(void)
212 {
213 return _max;
214 }
215
getMin(void)216 double DbusPassive::getMin(void)
217 {
218 return _min;
219 }
220
updateValue(double value,bool force)221 void DbusPassive::updateValue(double value, bool force)
222 {
223 _badReading = false;
224
225 // Do not let a NAN, or other floating-point oddity, be used to update
226 // the value, as that indicates the sensor has no valid reading.
227 if (!(std::isfinite(value)))
228 {
229 _badReading = true;
230
231 // Do not continue with a bad reading, unless caller forcing
232 if (!force)
233 {
234 return;
235 }
236 }
237
238 value *= std::pow(10.0, _scale);
239
240 auto unscaled = value;
241 scaleSensorReading(_min, _max, value);
242
243 if (_typeMargin)
244 {
245 _marginHot = false;
246
247 // Unlike an absolute temperature sensor,
248 // where 0 degrees C is a good reading,
249 // a value received of 0 (or negative) margin is worrisome,
250 // and should be flagged.
251 // Either it indicates margin not calculated properly,
252 // or somebody forgot to set the margin-zero setpoint,
253 // or the system is really overheating that much.
254 // This is a different condition from _failed
255 // and _badReading, so it merits its own flag.
256 // The sensor has not failed, the reading is good, but the zone
257 // still needs to know that it should go to failsafe mode.
258 if (unscaled <= 0.0)
259 {
260 _marginHot = true;
261 }
262 }
263
264 setValue(value, unscaled);
265 }
266
handleSensorValue(sdbusplus::message_t & msg,DbusPassive * owner)267 int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner)
268 {
269 std::string msgSensor;
270 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
271
272 msg.read(msgSensor, msgData);
273
274 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
275 {
276 auto valPropMap = msgData.find("Value");
277 if (valPropMap != msgData.end())
278 {
279 double value =
280 std::visit(VariantToDoubleVisitor(), valPropMap->second);
281
282 owner->updateValue(value, false);
283 }
284 }
285 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
286 {
287 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
288 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
289 if (criticalAlarmHigh == msgData.end() &&
290 criticalAlarmLow == msgData.end())
291 {
292 return 0;
293 }
294
295 bool asserted = false;
296 if (criticalAlarmLow != msgData.end())
297 {
298 asserted = std::get<bool>(criticalAlarmLow->second);
299 }
300
301 // checking both as in theory you could de-assert one threshold and
302 // assert the other at the same moment
303 if (!asserted && criticalAlarmHigh != msgData.end())
304 {
305 asserted = std::get<bool>(criticalAlarmHigh->second);
306 }
307 owner->setFailed(asserted);
308 }
309 #ifdef UNC_FAILSAFE
310 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Warning")
311 {
312 auto warningAlarmHigh = msgData.find("WarningAlarmHigh");
313 if (warningAlarmHigh == msgData.end())
314 {
315 return 0;
316 }
317
318 bool asserted = false;
319 if (warningAlarmHigh != msgData.end())
320 {
321 asserted = std::get<bool>(warningAlarmHigh->second);
322 }
323 owner->setFailed(asserted);
324 }
325 #endif
326 else if (msgSensor == "xyz.openbmc_project.State.Decorator.Availability")
327 {
328 auto available = msgData.find("Available");
329 if (available == msgData.end())
330 {
331 return 0;
332 }
333 bool asserted = std::get<bool>(available->second);
334 owner->setAvailable(asserted);
335 if (!asserted)
336 {
337 // A thermal controller will continue its PID calculation and not
338 // trigger a 'failsafe' when some inputs are unavailable.
339 // So, forced to clear the value here to prevent a historical
340 // value to participate in a latter PID calculation.
341 owner->updateValue(std::numeric_limits<double>::quiet_NaN(), true);
342 }
343 }
344 else if (msgSensor ==
345 "xyz.openbmc_project.State.Decorator.OperationalStatus")
346 {
347 auto functional = msgData.find("Functional");
348 if (functional == msgData.end())
349 {
350 return 0;
351 }
352 bool asserted = std::get<bool>(functional->second);
353 owner->setFunctional(asserted);
354 }
355
356 return 0;
357 }
358
dbusHandleSignal(sd_bus_message * msg,void * usrData,sd_bus_error * err)359 int dbusHandleSignal(sd_bus_message* msg, void* usrData,
360 [[maybe_unused]] sd_bus_error* err)
361 {
362 auto sdbpMsg = sdbusplus::message_t(msg);
363 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
364
365 return handleSensorValue(sdbpMsg, obj);
366 }
367
368 } // namespace pid_control
369