1 /** 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 18 #include "conditions.hpp" 19 #include "json_config.hpp" 20 #include "nonzero_speed_trust.hpp" 21 #include "power_interface.hpp" 22 #include "power_off_rule.hpp" 23 #include "tach_sensor.hpp" 24 #include "types.hpp" 25 26 #include <fmt/format.h> 27 28 #include <nlohmann/json.hpp> 29 #include <phosphor-logging/log.hpp> 30 31 #include <algorithm> 32 #include <map> 33 #include <memory> 34 #include <optional> 35 #include <vector> 36 37 namespace phosphor::fan::monitor 38 { 39 40 using json = nlohmann::json; 41 using namespace phosphor::logging; 42 43 namespace tClass 44 { 45 46 // Get a constructed trust group class for a non-zero speed group 47 CreateGroupFunction 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 57 const std::map<std::string, trustHandler> trusts = { 58 {"nonzerospeed", tClass::getNonZeroSpeed}}; 59 const std::map<std::string, condHandler> conditions = { 60 {"propertiesmatch", condition::getPropertiesMatch}}; 61 const std::map<std::string, size_t> methods = { 62 {"timebased", MethodMode::timebased}, {"count", MethodMode::count}}; 63 64 const 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 127 const 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 } 161 // 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 } 167 // 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 } 173 174 sensorDefs.emplace_back(std::tuple( 175 sensor["name"].get<std::string>(), sensor["has_target"].get<bool>(), 176 targetIntf, factor, offset, threshold, ignoreAboveMax)); 177 } 178 179 return sensorDefs; 180 } 181 182 const std::vector<FanDefinition> getFanDefs(const json& obj) 183 { 184 std::vector<FanDefinition> fanDefs; 185 186 for (const auto& fan : obj["fans"]) 187 { 188 if (!fan.contains("inventory") || !fan.contains("deviation") || 189 !fan.contains("sensors")) 190 { 191 // Log error on missing required parameters 192 log<level::ERR>( 193 "Missing required fan monitor definition parameters", 194 entry("REQUIRED_PARAMETERS=%s", 195 "{inventory, deviation, sensors}")); 196 throw std::runtime_error( 197 "Missing required fan monitor definition parameters"); 198 } 199 // Valid deviation range is 0 - 100% 200 auto deviation = fan["deviation"].get<size_t>(); 201 if (deviation < 0 || 100 < deviation) 202 { 203 auto msg = 204 fmt::format( 205 "Invalid deviation of {} found, must be between 0 and 100", 206 deviation) 207 .c_str(); 208 log<level::ERR>(msg); 209 throw std::runtime_error(msg); 210 } 211 212 // Construct the sensor definitions for this fan 213 auto sensorDefs = getSensorDefs(fan["sensors"]); 214 215 // Functional delay is optional and defaults to 0 216 size_t funcDelay = 0; 217 if (fan.contains("functional_delay")) 218 { 219 funcDelay = fan["functional_delay"].get<size_t>(); 220 } 221 222 // Method is optional and defaults to time based functional 223 // determination 224 size_t method = MethodMode::timebased; 225 size_t countInterval = 1; 226 if (fan.contains("method")) 227 { 228 auto methodConf = fan["method"].get<std::string>(); 229 auto methodFunc = methods.find(methodConf); 230 if (methodFunc != methods.end()) 231 { 232 method = methodFunc->second; 233 } 234 else 235 { 236 // Log error on unsupported method parameter 237 log<level::ERR>("Invalid fan method"); 238 throw std::runtime_error("Invalid fan method"); 239 } 240 241 // Read the count interval value used with the count method. 242 if (method == MethodMode::count) 243 { 244 if (fan.contains("count_interval")) 245 { 246 countInterval = fan["count_interval"].get<size_t>(); 247 } 248 } 249 } 250 251 // Timeout defaults to 0 252 size_t timeout = 0; 253 if (method == MethodMode::timebased) 254 { 255 if (!fan.contains("allowed_out_of_range_time")) 256 { 257 // Log error on missing required parameter 258 log<level::ERR>( 259 "Missing required fan monitor definition parameters", 260 entry("REQUIRED_PARAMETER=%s", 261 "{allowed_out_of_range_time}")); 262 throw std::runtime_error( 263 "Missing required fan monitor definition parameters"); 264 } 265 else 266 { 267 timeout = fan["allowed_out_of_range_time"].get<size_t>(); 268 } 269 } 270 271 // Monitor start delay is optional and defaults to 0 272 size_t monitorDelay = 0; 273 if (fan.contains("monitor_start_delay")) 274 { 275 monitorDelay = fan["monitor_start_delay"].get<size_t>(); 276 } 277 278 // num_sensors_nonfunc_for_fan_nonfunc is optional and defaults 279 // to zero if not present, meaning the code will not set the 280 // parent fan to nonfunctional based on sensors. 281 size_t nonfuncSensorsCount = 0; 282 if (fan.contains("num_sensors_nonfunc_for_fan_nonfunc")) 283 { 284 nonfuncSensorsCount = 285 fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>(); 286 } 287 288 // nonfunc_rotor_error_delay is optional, though it will 289 // default to zero if 'fault_handling' is present. 290 std::optional<size_t> nonfuncRotorErrorDelay; 291 if (fan.contains("nonfunc_rotor_error_delay")) 292 { 293 nonfuncRotorErrorDelay = 294 fan["nonfunc_rotor_error_delay"].get<size_t>(); 295 } 296 else if (obj.contains("fault_handling")) 297 { 298 nonfuncRotorErrorDelay = 0; 299 } 300 301 // fan_missing_error_delay is optional. 302 std::optional<size_t> fanMissingErrorDelay; 303 if (fan.contains("fan_missing_error_delay")) 304 { 305 fanMissingErrorDelay = 306 fan.at("fan_missing_error_delay").get<size_t>(); 307 } 308 309 // Handle optional conditions 310 auto cond = std::optional<Condition>(); 311 if (fan.contains("condition")) 312 { 313 if (!fan["condition"].contains("name")) 314 { 315 // Log error on missing required parameter 316 log<level::ERR>( 317 "Missing required fan monitor condition parameter", 318 entry("REQUIRED_PARAMETER=%s", "{name}")); 319 throw std::runtime_error( 320 "Missing required fan monitor condition parameter"); 321 } 322 auto name = fan["condition"]["name"].get<std::string>(); 323 // The function for fan monitoring condition 324 // (Must have a supported function within the condition namespace) 325 std::transform(name.begin(), name.end(), name.begin(), tolower); 326 auto handler = conditions.find(name); 327 if (handler != conditions.end()) 328 { 329 cond = handler->second(fan["condition"]); 330 } 331 else 332 { 333 log<level::INFO>( 334 "No handler found for configured condition", 335 entry("CONDITION_NAME=%s", name.c_str()), 336 entry("JSON_DUMP=%s", fan["condition"].dump().c_str())); 337 } 338 } 339 340 // if the fan should be set to functional when plugged in 341 bool setFuncOnPresent = false; 342 if (fan.contains("set_func_on_present")) 343 { 344 setFuncOnPresent = fan["set_func_on_present"].get<bool>(); 345 } 346 347 fanDefs.emplace_back(std::tuple( 348 fan["inventory"].get<std::string>(), method, funcDelay, timeout, 349 deviation, nonfuncSensorsCount, monitorDelay, countInterval, 350 nonfuncRotorErrorDelay, fanMissingErrorDelay, sensorDefs, cond, 351 setFuncOnPresent)); 352 } 353 354 return fanDefs; 355 } 356 357 PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig) 358 { 359 // The state is optional and defaults to runtime 360 PowerRuleState ruleState{PowerRuleState::runtime}; 361 362 if (powerOffConfig.contains("state")) 363 { 364 auto state = powerOffConfig.at("state").get<std::string>(); 365 if (state == "at_pgood") 366 { 367 ruleState = PowerRuleState::atPgood; 368 } 369 else if (state != "runtime") 370 { 371 auto msg = fmt::format("Invalid power off state entry {}", state); 372 log<level::ERR>(msg.c_str()); 373 throw std::runtime_error(msg.c_str()); 374 } 375 } 376 377 return ruleState; 378 } 379 380 std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig) 381 { 382 std::unique_ptr<PowerOffCause> cause; 383 384 if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause")) 385 { 386 const auto msg = 387 "Missing 'count' or 'cause' entries in power off config"; 388 log<level::ERR>(msg); 389 throw std::runtime_error(msg); 390 } 391 392 auto count = powerOffConfig.at("count").get<size_t>(); 393 auto powerOffCause = powerOffConfig.at("cause").get<std::string>(); 394 395 const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>> 396 causes{ 397 {"missing_fan_frus", 398 [count]() { return std::make_unique<MissingFanFRUCause>(count); }}, 399 {"nonfunc_fan_rotors", [count]() { 400 return std::make_unique<NonfuncFanRotorCause>(count); 401 }}}; 402 403 auto it = causes.find(powerOffCause); 404 if (it != causes.end()) 405 { 406 cause = it->second(); 407 } 408 else 409 { 410 auto msg = 411 fmt::format("Invalid power off cause {} in power off config JSON", 412 powerOffCause); 413 log<level::ERR>(msg.c_str()); 414 throw std::runtime_error(msg.c_str()); 415 } 416 417 return cause; 418 } 419 420 std::unique_ptr<PowerOffAction> 421 getPowerOffAction(const json& powerOffConfig, 422 std::shared_ptr<PowerInterfaceBase>& powerInterface, 423 PowerOffAction::PrePowerOffFunc& func) 424 { 425 std::unique_ptr<PowerOffAction> action; 426 if (!powerOffConfig.contains("type")) 427 { 428 const auto msg = "Missing 'type' entry in power off config"; 429 log<level::ERR>(msg); 430 throw std::runtime_error(msg); 431 } 432 433 auto type = powerOffConfig.at("type").get<std::string>(); 434 435 if (((type == "hard") || (type == "soft")) && 436 !powerOffConfig.contains("delay")) 437 { 438 const auto msg = "Missing 'delay' entry in power off config"; 439 log<level::ERR>(msg); 440 throw std::runtime_error(msg); 441 } 442 else if ((type == "epow") && 443 (!powerOffConfig.contains("service_mode_delay") || 444 !powerOffConfig.contains("meltdown_delay"))) 445 { 446 const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' " 447 "entry in power off config"; 448 log<level::ERR>(msg); 449 throw std::runtime_error(msg); 450 } 451 452 if (type == "hard") 453 { 454 action = std::make_unique<HardPowerOff>( 455 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func); 456 } 457 else if (type == "soft") 458 { 459 action = std::make_unique<SoftPowerOff>( 460 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func); 461 } 462 else if (type == "epow") 463 { 464 action = std::make_unique<EpowPowerOff>( 465 powerOffConfig.at("service_mode_delay").get<uint32_t>(), 466 powerOffConfig.at("meltdown_delay").get<uint32_t>(), powerInterface, 467 func); 468 } 469 else 470 { 471 auto msg = 472 fmt::format("Invalid 'type' entry {} in power off config", type); 473 log<level::ERR>(msg.c_str()); 474 throw std::runtime_error(msg.c_str()); 475 } 476 477 return action; 478 } 479 480 std::vector<std::unique_ptr<PowerOffRule>> 481 getPowerOffRules(const json& obj, 482 std::shared_ptr<PowerInterfaceBase>& powerInterface, 483 PowerOffAction::PrePowerOffFunc& func) 484 { 485 std::vector<std::unique_ptr<PowerOffRule>> rules; 486 487 if (!(obj.contains("fault_handling") && 488 obj.at("fault_handling").contains("power_off_config"))) 489 { 490 return rules; 491 } 492 493 for (const auto& config : obj.at("fault_handling").at("power_off_config")) 494 { 495 auto state = getPowerOffPowerRuleState(config); 496 auto cause = getPowerOffCause(config); 497 auto action = getPowerOffAction(config, powerInterface, func); 498 499 auto rule = std::make_unique<PowerOffRule>( 500 std::move(state), std::move(cause), std::move(action)); 501 rules.push_back(std::move(rule)); 502 } 503 504 return rules; 505 } 506 507 std::optional<size_t> getNumNonfuncRotorsBeforeError(const json& obj) 508 { 509 std::optional<size_t> num; 510 511 if (obj.contains("fault_handling")) 512 { 513 // Defaults to 1 if not present inside of 'fault_handling'. 514 num = obj.at("fault_handling") 515 .value("num_nonfunc_rotors_before_error", 1); 516 } 517 518 return num; 519 } 520 521 } // namespace phosphor::fan::monitor 522