blob: 1144998a22f150b051ff27a1e08f7bf981c2f91a [file] [log] [blame]
Matthew Barth9ea8bee2020-06-04 14:27:19 -05001/**
2 * Copyright © 2020 IBM Corporation
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 "json_parser.hpp"
17
Matthew Barth22ab93b2020-06-08 10:47:56 -050018#include "conditions.hpp"
Matthew Barth9ea8bee2020-06-04 14:27:19 -050019#include "json_config.hpp"
20#include "nonzero_speed_trust.hpp"
Matt Spinlerf06ab072020-10-14 12:58:22 -050021#include "power_interface.hpp"
22#include "power_off_rule.hpp"
Jolie Ku69f2f482020-10-21 09:59:43 +080023#include "tach_sensor.hpp"
Matthew Barth9ea8bee2020-06-04 14:27:19 -050024#include "types.hpp"
25
Matt Spinlerf06ab072020-10-14 12:58:22 -050026#include <fmt/format.h>
27
Matthew Barth9ea8bee2020-06-04 14:27:19 -050028#include <nlohmann/json.hpp>
29#include <phosphor-logging/log.hpp>
30
31#include <algorithm>
32#include <map>
33#include <memory>
Matthew Barth22ab93b2020-06-08 10:47:56 -050034#include <optional>
Matthew Barth9ea8bee2020-06-04 14:27:19 -050035#include <vector>
36
37namespace phosphor::fan::monitor
38{
39
40using json = nlohmann::json;
41using namespace phosphor::logging;
42
43namespace tClass
44{
45
46// Get a constructed trust group class for a non-zero speed group
47CreateGroupFunction
48 getNonZeroSpeed(const std::vector<trust::GroupDefinition>& group)
49{
50 return [group]() {
51 return std::make_unique<trust::NonzeroSpeed>(std::move(group));
52 };
53}
54
55} // namespace tClass
56
57const std::map<std::string, trustHandler> trusts = {
58 {"nonzerospeed", tClass::getNonZeroSpeed}};
Matthew Barth3ad14342020-06-08 16:17:42 -050059const std::map<std::string, condHandler> conditions = {
60 {"propertiesmatch", condition::getPropertiesMatch}};
Jolie Ku69f2f482020-10-21 09:59:43 +080061const std::map<std::string, size_t> methods = {
62 {"timebased", MethodMode::timebased}, {"count", MethodMode::count}};
Matthew Barth9ea8bee2020-06-04 14:27:19 -050063
64const std::vector<CreateGroupFunction> getTrustGrps(const json& obj)
65{
66 std::vector<CreateGroupFunction> grpFuncs;
67
68 if (obj.contains("sensor_trust_groups"))
69 {
70 for (auto& stg : obj["sensor_trust_groups"])
71 {
72 if (!stg.contains("class") || !stg.contains("group"))
73 {
74 // Log error on missing required parameters
75 log<level::ERR>(
76 "Missing required fan monitor trust group parameters",
77 entry("REQUIRED_PARAMETERS=%s", "{class, group}"));
78 throw std::runtime_error(
79 "Missing required fan trust group parameters");
80 }
81 auto tgClass = stg["class"].get<std::string>();
82 std::vector<trust::GroupDefinition> group;
83 for (auto& member : stg["group"])
84 {
85 // Construct list of group members
86 if (!member.contains("name"))
87 {
88 // Log error on missing required parameter
89 log<level::ERR>(
90 "Missing required fan monitor trust group member name",
91 entry("CLASS=%s", tgClass.c_str()));
92 throw std::runtime_error(
93 "Missing required fan monitor trust group member name");
94 }
95 auto in_trust = true;
96 if (member.contains("in_trust"))
97 {
98 in_trust = member["in_trust"].get<bool>();
99 }
100 group.emplace_back(trust::GroupDefinition{
101 member["name"].get<std::string>(), in_trust});
102 }
103 // The class for fan sensor trust groups
104 // (Must have a supported function within the tClass namespace)
105 std::transform(tgClass.begin(), tgClass.end(), tgClass.begin(),
106 tolower);
107 auto handler = trusts.find(tgClass);
108 if (handler != trusts.end())
109 {
110 // Call function for trust group class
111 grpFuncs.emplace_back(handler->second(group));
112 }
113 else
114 {
115 // Log error on unsupported trust group class
116 log<level::ERR>("Invalid fan monitor trust group class",
117 entry("CLASS=%s", tgClass.c_str()));
118 throw std::runtime_error(
119 "Invalid fan monitor trust group class");
120 }
121 }
122 }
123
124 return grpFuncs;
125}
126
Matthew Barth22ab93b2020-06-08 10:47:56 -0500127const std::vector<SensorDefinition> getSensorDefs(const json& sensors)
128{
129 std::vector<SensorDefinition> sensorDefs;
130
131 for (const auto& sensor : sensors)
132 {
133 if (!sensor.contains("name") || !sensor.contains("has_target"))
134 {
135 // Log error on missing required parameters
136 log<level::ERR>(
137 "Missing required fan sensor definition parameters",
138 entry("REQUIRED_PARAMETERS=%s", "{name, has_target}"));
139 throw std::runtime_error(
140 "Missing required fan sensor definition parameters");
141 }
142 // Target interface is optional and defaults to
143 // 'xyz.openbmc_project.Control.FanSpeed'
144 std::string targetIntf = "xyz.openbmc_project.Control.FanSpeed";
145 if (sensor.contains("target_interface"))
146 {
147 targetIntf = sensor["target_interface"].get<std::string>();
148 }
149 // Factor is optional and defaults to 1
150 auto factor = 1.0;
151 if (sensor.contains("factor"))
152 {
153 factor = sensor["factor"].get<double>();
154 }
155 // Offset is optional and defaults to 0
156 auto offset = 0;
157 if (sensor.contains("offset"))
158 {
159 offset = sensor["offset"].get<int64_t>();
160 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800161 // Threshold is optional and defaults to 1
162 auto threshold = 1;
163 if (sensor.contains("threshold"))
164 {
165 threshold = sensor["threshold"].get<size_t>();
166 }
Matthew Barth8a8aa442021-11-19 14:13:13 -0600167 // Ignore being above the allowed max is optional, defaults to not
168 bool ignoreAboveMax = false;
169 if (sensor.contains("ignore_above_max"))
170 {
171 ignoreAboveMax = sensor["ignore_above_max"].get<bool>();
172 }
Matthew Barth22ab93b2020-06-08 10:47:56 -0500173
Jolie Ku69f2f482020-10-21 09:59:43 +0800174 sensorDefs.emplace_back(std::tuple(
175 sensor["name"].get<std::string>(), sensor["has_target"].get<bool>(),
Matthew Barth8a8aa442021-11-19 14:13:13 -0600176 targetIntf, factor, offset, threshold, ignoreAboveMax));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500177 }
178
179 return sensorDefs;
180}
181
182const std::vector<FanDefinition> getFanDefs(const json& obj)
183{
184 std::vector<FanDefinition> fanDefs;
185
186 for (const auto& fan : obj["fans"])
187 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800188 if (!fan.contains("inventory") || !fan.contains("deviation") ||
189 !fan.contains("sensors"))
Matthew Barth22ab93b2020-06-08 10:47:56 -0500190 {
191 // Log error on missing required parameters
192 log<level::ERR>(
193 "Missing required fan monitor definition parameters",
194 entry("REQUIRED_PARAMETERS=%s",
Jolie Ku69f2f482020-10-21 09:59:43 +0800195 "{inventory, deviation, sensors}"));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500196 throw std::runtime_error(
197 "Missing required fan monitor definition parameters");
198 }
Matthew Barth4c4de262021-02-17 13:03:02 -0600199 // Valid deviation range is 0 - 100%
200 auto deviation = fan["deviation"].get<size_t>();
Mike Capps808d7fe2022-06-13 10:12:16 -0400201 if (100 < deviation)
Matthew Barth4c4de262021-02-17 13:03:02 -0600202 {
Mike Capps78182482022-06-23 11:28:26 -0400203 auto msg = fmt::format(
204 "Invalid deviation of {} found, must be between 0 and 100",
205 deviation);
206
207 log<level::ERR>(msg.c_str());
208 throw std::runtime_error(msg.c_str());
Matthew Barth4c4de262021-02-17 13:03:02 -0600209 }
210
Matthew Barth22ab93b2020-06-08 10:47:56 -0500211 // Construct the sensor definitions for this fan
212 auto sensorDefs = getSensorDefs(fan["sensors"]);
213
214 // Functional delay is optional and defaults to 0
215 size_t funcDelay = 0;
216 if (fan.contains("functional_delay"))
217 {
218 funcDelay = fan["functional_delay"].get<size_t>();
219 }
220
Jolie Ku69f2f482020-10-21 09:59:43 +0800221 // Method is optional and defaults to time based functional
222 // determination
223 size_t method = MethodMode::timebased;
Matt Spinler623635c2021-03-29 13:13:59 -0500224 size_t countInterval = 1;
Jolie Ku69f2f482020-10-21 09:59:43 +0800225 if (fan.contains("method"))
226 {
227 auto methodConf = fan["method"].get<std::string>();
228 auto methodFunc = methods.find(methodConf);
229 if (methodFunc != methods.end())
230 {
231 method = methodFunc->second;
232 }
233 else
234 {
235 // Log error on unsupported method parameter
236 log<level::ERR>("Invalid fan method");
237 throw std::runtime_error("Invalid fan method");
238 }
Matt Spinler623635c2021-03-29 13:13:59 -0500239
240 // Read the count interval value used with the count method.
241 if (method == MethodMode::count)
242 {
243 if (fan.contains("count_interval"))
244 {
245 countInterval = fan["count_interval"].get<size_t>();
246 }
247 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800248 }
249
250 // Timeout defaults to 0
251 size_t timeout = 0;
252 if (method == MethodMode::timebased)
253 {
254 if (!fan.contains("allowed_out_of_range_time"))
255 {
256 // Log error on missing required parameter
257 log<level::ERR>(
258 "Missing required fan monitor definition parameters",
259 entry("REQUIRED_PARAMETER=%s",
260 "{allowed_out_of_range_time}"));
261 throw std::runtime_error(
262 "Missing required fan monitor definition parameters");
263 }
264 else
265 {
266 timeout = fan["allowed_out_of_range_time"].get<size_t>();
267 }
268 }
269
Matt Spinlerb0412d02020-10-12 16:53:52 -0500270 // Monitor start delay is optional and defaults to 0
271 size_t monitorDelay = 0;
272 if (fan.contains("monitor_start_delay"))
273 {
274 monitorDelay = fan["monitor_start_delay"].get<size_t>();
275 }
276
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500277 // num_sensors_nonfunc_for_fan_nonfunc is optional and defaults
278 // to zero if not present, meaning the code will not set the
279 // parent fan to nonfunctional based on sensors.
280 size_t nonfuncSensorsCount = 0;
281 if (fan.contains("num_sensors_nonfunc_for_fan_nonfunc"))
282 {
283 nonfuncSensorsCount =
284 fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>();
285 }
286
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500287 // nonfunc_rotor_error_delay is optional, though it will
288 // default to zero if 'fault_handling' is present.
289 std::optional<size_t> nonfuncRotorErrorDelay;
290 if (fan.contains("nonfunc_rotor_error_delay"))
291 {
292 nonfuncRotorErrorDelay =
293 fan["nonfunc_rotor_error_delay"].get<size_t>();
294 }
295 else if (obj.contains("fault_handling"))
296 {
297 nonfuncRotorErrorDelay = 0;
298 }
299
Matt Spinler27f6b682020-10-27 08:43:37 -0500300 // fan_missing_error_delay is optional.
301 std::optional<size_t> fanMissingErrorDelay;
302 if (fan.contains("fan_missing_error_delay"))
303 {
304 fanMissingErrorDelay =
305 fan.at("fan_missing_error_delay").get<size_t>();
306 }
307
Matthew Barth3ad14342020-06-08 16:17:42 -0500308 // Handle optional conditions
Matthew Barth8a0c2322020-06-17 09:53:10 -0500309 auto cond = std::optional<Condition>();
Matthew Barth3ad14342020-06-08 16:17:42 -0500310 if (fan.contains("condition"))
311 {
312 if (!fan["condition"].contains("name"))
313 {
314 // Log error on missing required parameter
315 log<level::ERR>(
316 "Missing required fan monitor condition parameter",
317 entry("REQUIRED_PARAMETER=%s", "{name}"));
318 throw std::runtime_error(
319 "Missing required fan monitor condition parameter");
320 }
321 auto name = fan["condition"]["name"].get<std::string>();
322 // The function for fan monitoring condition
323 // (Must have a supported function within the condition namespace)
324 std::transform(name.begin(), name.end(), name.begin(), tolower);
325 auto handler = conditions.find(name);
326 if (handler != conditions.end())
327 {
328 cond = handler->second(fan["condition"]);
329 }
330 else
331 {
332 log<level::INFO>(
333 "No handler found for configured condition",
334 entry("CONDITION_NAME=%s", name.c_str()),
335 entry("JSON_DUMP=%s", fan["condition"].dump().c_str()));
336 }
337 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800338
Matt Spinlera3584bd2021-03-29 15:48:30 -0500339 // if the fan should be set to functional when plugged in
340 bool setFuncOnPresent = false;
341 if (fan.contains("set_func_on_present"))
342 {
343 setFuncOnPresent = fan["set_func_on_present"].get<bool>();
344 }
345
Matt Spinler27f6b682020-10-27 08:43:37 -0500346 fanDefs.emplace_back(std::tuple(
Jolie Ku69f2f482020-10-21 09:59:43 +0800347 fan["inventory"].get<std::string>(), method, funcDelay, timeout,
Matt Spinler623635c2021-03-29 13:13:59 -0500348 deviation, nonfuncSensorsCount, monitorDelay, countInterval,
Matt Spinlera3584bd2021-03-29 15:48:30 -0500349 nonfuncRotorErrorDelay, fanMissingErrorDelay, sensorDefs, cond,
350 setFuncOnPresent));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500351 }
352
353 return fanDefs;
354}
355
Matt Spinlerf06ab072020-10-14 12:58:22 -0500356PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig)
357{
358 // The state is optional and defaults to runtime
359 PowerRuleState ruleState{PowerRuleState::runtime};
360
361 if (powerOffConfig.contains("state"))
362 {
363 auto state = powerOffConfig.at("state").get<std::string>();
364 if (state == "at_pgood")
365 {
366 ruleState = PowerRuleState::atPgood;
367 }
368 else if (state != "runtime")
369 {
370 auto msg = fmt::format("Invalid power off state entry {}", state);
371 log<level::ERR>(msg.c_str());
372 throw std::runtime_error(msg.c_str());
373 }
374 }
375
376 return ruleState;
377}
378
379std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig)
380{
381 std::unique_ptr<PowerOffCause> cause;
382
383 if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause"))
384 {
385 const auto msg =
386 "Missing 'count' or 'cause' entries in power off config";
387 log<level::ERR>(msg);
388 throw std::runtime_error(msg);
389 }
390
391 auto count = powerOffConfig.at("count").get<size_t>();
392 auto powerOffCause = powerOffConfig.at("cause").get<std::string>();
393
394 const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>>
395 causes{
396 {"missing_fan_frus",
397 [count]() { return std::make_unique<MissingFanFRUCause>(count); }},
398 {"nonfunc_fan_rotors", [count]() {
399 return std::make_unique<NonfuncFanRotorCause>(count);
400 }}};
401
402 auto it = causes.find(powerOffCause);
403 if (it != causes.end())
404 {
405 cause = it->second();
406 }
407 else
408 {
409 auto msg =
410 fmt::format("Invalid power off cause {} in power off config JSON",
411 powerOffCause);
412 log<level::ERR>(msg.c_str());
413 throw std::runtime_error(msg.c_str());
414 }
415
416 return cause;
417}
418
419std::unique_ptr<PowerOffAction>
420 getPowerOffAction(const json& powerOffConfig,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500421 std::shared_ptr<PowerInterfaceBase>& powerInterface,
422 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500423{
424 std::unique_ptr<PowerOffAction> action;
425 if (!powerOffConfig.contains("type"))
426 {
427 const auto msg = "Missing 'type' entry in power off config";
428 log<level::ERR>(msg);
429 throw std::runtime_error(msg);
430 }
431
432 auto type = powerOffConfig.at("type").get<std::string>();
433
434 if (((type == "hard") || (type == "soft")) &&
435 !powerOffConfig.contains("delay"))
436 {
437 const auto msg = "Missing 'delay' entry in power off config";
438 log<level::ERR>(msg);
439 throw std::runtime_error(msg);
440 }
441 else if ((type == "epow") &&
442 (!powerOffConfig.contains("service_mode_delay") ||
443 !powerOffConfig.contains("meltdown_delay")))
444 {
445 const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' "
446 "entry in power off config";
447 log<level::ERR>(msg);
448 throw std::runtime_error(msg);
449 }
450
451 if (type == "hard")
452 {
453 action = std::make_unique<HardPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500454 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500455 }
456 else if (type == "soft")
457 {
458 action = std::make_unique<SoftPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500459 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500460 }
461 else if (type == "epow")
462 {
463 action = std::make_unique<EpowPowerOff>(
464 powerOffConfig.at("service_mode_delay").get<uint32_t>(),
Matt Spinlerac1efc12020-10-27 10:20:11 -0500465 powerOffConfig.at("meltdown_delay").get<uint32_t>(), powerInterface,
466 func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500467 }
468 else
469 {
470 auto msg =
471 fmt::format("Invalid 'type' entry {} in power off config", type);
472 log<level::ERR>(msg.c_str());
473 throw std::runtime_error(msg.c_str());
474 }
475
476 return action;
477}
478
479std::vector<std::unique_ptr<PowerOffRule>>
480 getPowerOffRules(const json& obj,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500481 std::shared_ptr<PowerInterfaceBase>& powerInterface,
482 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500483{
484 std::vector<std::unique_ptr<PowerOffRule>> rules;
485
486 if (!(obj.contains("fault_handling") &&
487 obj.at("fault_handling").contains("power_off_config")))
488 {
489 return rules;
490 }
491
492 for (const auto& config : obj.at("fault_handling").at("power_off_config"))
493 {
494 auto state = getPowerOffPowerRuleState(config);
495 auto cause = getPowerOffCause(config);
Matt Spinlerac1efc12020-10-27 10:20:11 -0500496 auto action = getPowerOffAction(config, powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500497
498 auto rule = std::make_unique<PowerOffRule>(
499 std::move(state), std::move(cause), std::move(action));
500 rules.push_back(std::move(rule));
501 }
502
503 return rules;
504}
505
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500506std::optional<size_t> getNumNonfuncRotorsBeforeError(const json& obj)
507{
508 std::optional<size_t> num;
509
510 if (obj.contains("fault_handling"))
511 {
512 // Defaults to 1 if not present inside of 'fault_handling'.
513 num = obj.at("fault_handling")
514 .value("num_nonfunc_rotors_before_error", 1);
515 }
516
517 return num;
518}
519
Matthew Barth9ea8bee2020-06-04 14:27:19 -0500520} // namespace phosphor::fan::monitor