blob: 9218634d5fb6174299a2d3796f90644992cbdd46 [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 Barth22ab93b2020-06-08 10:47:56 -0500167
Jolie Ku69f2f482020-10-21 09:59:43 +0800168 sensorDefs.emplace_back(std::tuple(
169 sensor["name"].get<std::string>(), sensor["has_target"].get<bool>(),
170 targetIntf, factor, offset, threshold));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500171 }
172
173 return sensorDefs;
174}
175
176const std::vector<FanDefinition> getFanDefs(const json& obj)
177{
178 std::vector<FanDefinition> fanDefs;
179
180 for (const auto& fan : obj["fans"])
181 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800182 if (!fan.contains("inventory") || !fan.contains("deviation") ||
183 !fan.contains("sensors"))
Matthew Barth22ab93b2020-06-08 10:47:56 -0500184 {
185 // Log error on missing required parameters
186 log<level::ERR>(
187 "Missing required fan monitor definition parameters",
188 entry("REQUIRED_PARAMETERS=%s",
Jolie Ku69f2f482020-10-21 09:59:43 +0800189 "{inventory, deviation, sensors}"));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500190 throw std::runtime_error(
191 "Missing required fan monitor definition parameters");
192 }
Matthew Barth4c4de262021-02-17 13:03:02 -0600193 // Valid deviation range is 0 - 100%
194 auto deviation = fan["deviation"].get<size_t>();
195 if (deviation < 0 || 100 < deviation)
196 {
197 auto msg =
198 fmt::format(
199 "Invalid deviation of {} found, must be between 0 and 100",
200 deviation)
201 .c_str();
202 log<level::ERR>(msg);
203 throw std::runtime_error(msg);
204 }
205
Matthew Barth22ab93b2020-06-08 10:47:56 -0500206 // Construct the sensor definitions for this fan
207 auto sensorDefs = getSensorDefs(fan["sensors"]);
208
209 // Functional delay is optional and defaults to 0
210 size_t funcDelay = 0;
211 if (fan.contains("functional_delay"))
212 {
213 funcDelay = fan["functional_delay"].get<size_t>();
214 }
215
Jolie Ku69f2f482020-10-21 09:59:43 +0800216 // Method is optional and defaults to time based functional
217 // determination
218 size_t method = MethodMode::timebased;
219 if (fan.contains("method"))
220 {
221 auto methodConf = fan["method"].get<std::string>();
222 auto methodFunc = methods.find(methodConf);
223 if (methodFunc != methods.end())
224 {
225 method = methodFunc->second;
226 }
227 else
228 {
229 // Log error on unsupported method parameter
230 log<level::ERR>("Invalid fan method");
231 throw std::runtime_error("Invalid fan method");
232 }
233 }
234
235 // Timeout defaults to 0
236 size_t timeout = 0;
237 if (method == MethodMode::timebased)
238 {
239 if (!fan.contains("allowed_out_of_range_time"))
240 {
241 // Log error on missing required parameter
242 log<level::ERR>(
243 "Missing required fan monitor definition parameters",
244 entry("REQUIRED_PARAMETER=%s",
245 "{allowed_out_of_range_time}"));
246 throw std::runtime_error(
247 "Missing required fan monitor definition parameters");
248 }
249 else
250 {
251 timeout = fan["allowed_out_of_range_time"].get<size_t>();
252 }
253 }
254
Matt Spinlerb0412d02020-10-12 16:53:52 -0500255 // Monitor start delay is optional and defaults to 0
256 size_t monitorDelay = 0;
257 if (fan.contains("monitor_start_delay"))
258 {
259 monitorDelay = fan["monitor_start_delay"].get<size_t>();
260 }
261
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500262 // num_sensors_nonfunc_for_fan_nonfunc is optional and defaults
263 // to zero if not present, meaning the code will not set the
264 // parent fan to nonfunctional based on sensors.
265 size_t nonfuncSensorsCount = 0;
266 if (fan.contains("num_sensors_nonfunc_for_fan_nonfunc"))
267 {
268 nonfuncSensorsCount =
269 fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>();
270 }
271
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500272 // nonfunc_rotor_error_delay is optional, though it will
273 // default to zero if 'fault_handling' is present.
274 std::optional<size_t> nonfuncRotorErrorDelay;
275 if (fan.contains("nonfunc_rotor_error_delay"))
276 {
277 nonfuncRotorErrorDelay =
278 fan["nonfunc_rotor_error_delay"].get<size_t>();
279 }
280 else if (obj.contains("fault_handling"))
281 {
282 nonfuncRotorErrorDelay = 0;
283 }
284
Matt Spinler27f6b682020-10-27 08:43:37 -0500285 // fan_missing_error_delay is optional.
286 std::optional<size_t> fanMissingErrorDelay;
287 if (fan.contains("fan_missing_error_delay"))
288 {
289 fanMissingErrorDelay =
290 fan.at("fan_missing_error_delay").get<size_t>();
291 }
292
Matthew Barth3ad14342020-06-08 16:17:42 -0500293 // Handle optional conditions
Matthew Barth8a0c2322020-06-17 09:53:10 -0500294 auto cond = std::optional<Condition>();
Matthew Barth3ad14342020-06-08 16:17:42 -0500295 if (fan.contains("condition"))
296 {
297 if (!fan["condition"].contains("name"))
298 {
299 // Log error on missing required parameter
300 log<level::ERR>(
301 "Missing required fan monitor condition parameter",
302 entry("REQUIRED_PARAMETER=%s", "{name}"));
303 throw std::runtime_error(
304 "Missing required fan monitor condition parameter");
305 }
306 auto name = fan["condition"]["name"].get<std::string>();
307 // The function for fan monitoring condition
308 // (Must have a supported function within the condition namespace)
309 std::transform(name.begin(), name.end(), name.begin(), tolower);
310 auto handler = conditions.find(name);
311 if (handler != conditions.end())
312 {
313 cond = handler->second(fan["condition"]);
314 }
315 else
316 {
317 log<level::INFO>(
318 "No handler found for configured condition",
319 entry("CONDITION_NAME=%s", name.c_str()),
320 entry("JSON_DUMP=%s", fan["condition"].dump().c_str()));
321 }
322 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800323
Matt Spinler27f6b682020-10-27 08:43:37 -0500324 fanDefs.emplace_back(std::tuple(
Jolie Ku69f2f482020-10-21 09:59:43 +0800325 fan["inventory"].get<std::string>(), method, funcDelay, timeout,
Matthew Barth4c4de262021-02-17 13:03:02 -0600326 deviation, nonfuncSensorsCount, monitorDelay,
Matt Spinler27f6b682020-10-27 08:43:37 -0500327 nonfuncRotorErrorDelay, fanMissingErrorDelay, sensorDefs, cond));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500328 }
329
330 return fanDefs;
331}
332
Matt Spinlerf06ab072020-10-14 12:58:22 -0500333PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig)
334{
335 // The state is optional and defaults to runtime
336 PowerRuleState ruleState{PowerRuleState::runtime};
337
338 if (powerOffConfig.contains("state"))
339 {
340 auto state = powerOffConfig.at("state").get<std::string>();
341 if (state == "at_pgood")
342 {
343 ruleState = PowerRuleState::atPgood;
344 }
345 else if (state != "runtime")
346 {
347 auto msg = fmt::format("Invalid power off state entry {}", state);
348 log<level::ERR>(msg.c_str());
349 throw std::runtime_error(msg.c_str());
350 }
351 }
352
353 return ruleState;
354}
355
356std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig)
357{
358 std::unique_ptr<PowerOffCause> cause;
359
360 if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause"))
361 {
362 const auto msg =
363 "Missing 'count' or 'cause' entries in power off config";
364 log<level::ERR>(msg);
365 throw std::runtime_error(msg);
366 }
367
368 auto count = powerOffConfig.at("count").get<size_t>();
369 auto powerOffCause = powerOffConfig.at("cause").get<std::string>();
370
371 const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>>
372 causes{
373 {"missing_fan_frus",
374 [count]() { return std::make_unique<MissingFanFRUCause>(count); }},
375 {"nonfunc_fan_rotors", [count]() {
376 return std::make_unique<NonfuncFanRotorCause>(count);
377 }}};
378
379 auto it = causes.find(powerOffCause);
380 if (it != causes.end())
381 {
382 cause = it->second();
383 }
384 else
385 {
386 auto msg =
387 fmt::format("Invalid power off cause {} in power off config JSON",
388 powerOffCause);
389 log<level::ERR>(msg.c_str());
390 throw std::runtime_error(msg.c_str());
391 }
392
393 return cause;
394}
395
396std::unique_ptr<PowerOffAction>
397 getPowerOffAction(const json& powerOffConfig,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500398 std::shared_ptr<PowerInterfaceBase>& powerInterface,
399 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500400{
401 std::unique_ptr<PowerOffAction> action;
402 if (!powerOffConfig.contains("type"))
403 {
404 const auto msg = "Missing 'type' entry in power off config";
405 log<level::ERR>(msg);
406 throw std::runtime_error(msg);
407 }
408
409 auto type = powerOffConfig.at("type").get<std::string>();
410
411 if (((type == "hard") || (type == "soft")) &&
412 !powerOffConfig.contains("delay"))
413 {
414 const auto msg = "Missing 'delay' entry in power off config";
415 log<level::ERR>(msg);
416 throw std::runtime_error(msg);
417 }
418 else if ((type == "epow") &&
419 (!powerOffConfig.contains("service_mode_delay") ||
420 !powerOffConfig.contains("meltdown_delay")))
421 {
422 const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' "
423 "entry in power off config";
424 log<level::ERR>(msg);
425 throw std::runtime_error(msg);
426 }
427
428 if (type == "hard")
429 {
430 action = std::make_unique<HardPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500431 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500432 }
433 else if (type == "soft")
434 {
435 action = std::make_unique<SoftPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500436 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500437 }
438 else if (type == "epow")
439 {
440 action = std::make_unique<EpowPowerOff>(
441 powerOffConfig.at("service_mode_delay").get<uint32_t>(),
Matt Spinlerac1efc12020-10-27 10:20:11 -0500442 powerOffConfig.at("meltdown_delay").get<uint32_t>(), powerInterface,
443 func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500444 }
445 else
446 {
447 auto msg =
448 fmt::format("Invalid 'type' entry {} in power off config", type);
449 log<level::ERR>(msg.c_str());
450 throw std::runtime_error(msg.c_str());
451 }
452
453 return action;
454}
455
456std::vector<std::unique_ptr<PowerOffRule>>
457 getPowerOffRules(const json& obj,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500458 std::shared_ptr<PowerInterfaceBase>& powerInterface,
459 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500460{
461 std::vector<std::unique_ptr<PowerOffRule>> rules;
462
463 if (!(obj.contains("fault_handling") &&
464 obj.at("fault_handling").contains("power_off_config")))
465 {
466 return rules;
467 }
468
469 for (const auto& config : obj.at("fault_handling").at("power_off_config"))
470 {
471 auto state = getPowerOffPowerRuleState(config);
472 auto cause = getPowerOffCause(config);
Matt Spinlerac1efc12020-10-27 10:20:11 -0500473 auto action = getPowerOffAction(config, powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500474
475 auto rule = std::make_unique<PowerOffRule>(
476 std::move(state), std::move(cause), std::move(action));
477 rules.push_back(std::move(rule));
478 }
479
480 return rules;
481}
482
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500483std::optional<size_t> getNumNonfuncRotorsBeforeError(const json& obj)
484{
485 std::optional<size_t> num;
486
487 if (obj.contains("fault_handling"))
488 {
489 // Defaults to 1 if not present inside of 'fault_handling'.
490 num = obj.at("fault_handling")
491 .value("num_nonfunc_rotors_before_error", 1);
492 }
493
494 return num;
495}
496
Matthew Barth9ea8bee2020-06-04 14:27:19 -0500497} // namespace phosphor::fan::monitor