Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 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 <string> |
Matthew Barth | 0961fae | 2019-11-15 09:00:27 -0600 | [diff] [blame] | 17 | #include <filesystem> |
| 18 | #include <fstream> |
| 19 | #include <nlohmann/json.hpp> |
| 20 | #include <phosphor-logging/log.hpp> |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 21 | |
| 22 | #include "json_config.hpp" |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 23 | #include "tach.hpp" |
| 24 | #include "gpio.hpp" |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 25 | #include "anyof.hpp" |
| 26 | #include "fallback.hpp" |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 27 | |
| 28 | namespace phosphor |
| 29 | { |
| 30 | namespace fan |
| 31 | { |
| 32 | namespace presence |
| 33 | { |
| 34 | |
Matthew Barth | 0961fae | 2019-11-15 09:00:27 -0600 | [diff] [blame] | 35 | using json = nlohmann::json; |
| 36 | namespace fs = std::filesystem; |
| 37 | using namespace phosphor::logging; |
| 38 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 39 | policies JsonConfig::_policies; |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 40 | const std::map<std::string, methodHandler> JsonConfig::_methods = |
| 41 | { |
| 42 | {"tach", method::getTach}, |
| 43 | {"gpio", method::getGpio} |
| 44 | }; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 45 | const std::map<std::string, rpolicyHandler> JsonConfig::_rpolicies = |
| 46 | { |
| 47 | {"anyof", rpolicy::getAnyof}, |
| 48 | {"fallback", rpolicy::getFallback} |
| 49 | }; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 50 | |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 51 | JsonConfig::JsonConfig(const std::string& jsonConfigPath) : |
| 52 | _defaultFile(fs::path(fs::path{jsonConfigPath} / jsonFileName)) |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 53 | { |
Matthew Barth | 3a429d9 | 2019-12-05 16:58:27 -0600 | [diff] [blame] | 54 | if (!_defaultFile.empty()) |
| 55 | { |
| 56 | // Load and process the json configuration |
| 57 | load(); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | log<level::ERR>("No JSON config file provided"); |
| 62 | throw std::runtime_error("No JSON config file provided"); |
| 63 | } |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | const policies& JsonConfig::get() |
| 67 | { |
| 68 | return _policies; |
| 69 | } |
| 70 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 71 | void JsonConfig::sighupHandler(sdeventplus::source::Signal& sigSrc, |
| 72 | const struct signalfd_siginfo* sigInfo) |
| 73 | { |
| 74 | try |
| 75 | { |
| 76 | // Load and process the json configuration |
| 77 | load(); |
| 78 | for (auto& p: _policies) |
| 79 | { |
| 80 | p->monitor(); |
| 81 | } |
| 82 | log<level::INFO>("Configuration loaded successfully"); |
| 83 | } |
| 84 | catch (std::runtime_error& re) |
| 85 | { |
| 86 | log<level::ERR>("Error loading config, no config changes made", |
| 87 | entry("LOAD_ERROR=%s", re.what())); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void JsonConfig::load() |
| 92 | { |
Matthew Barth | f6d7f61 | 2019-12-10 15:22:54 -0600 | [diff] [blame] | 93 | fs::path confFile{fs::path{jsonOverridePath} / jsonFileName}; |
Matthew Barth | 4fb0af9 | 2019-12-04 09:15:00 -0600 | [diff] [blame] | 94 | if (!fs::exists(confFile)) |
| 95 | { |
| 96 | confFile = _defaultFile; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | log<level::INFO>("Loading alternate configuration", |
| 101 | entry("JSON_FILE=%s", confFile.c_str())); |
| 102 | } |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 103 | std::ifstream file; |
| 104 | json jsonConf; |
| 105 | |
Matthew Barth | 4fb0af9 | 2019-12-04 09:15:00 -0600 | [diff] [blame] | 106 | if (fs::exists(confFile)) |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 107 | { |
Matthew Barth | 4fb0af9 | 2019-12-04 09:15:00 -0600 | [diff] [blame] | 108 | file.open(confFile); |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 109 | try |
| 110 | { |
| 111 | jsonConf = json::parse(file); |
| 112 | } |
| 113 | catch (std::exception& e) |
| 114 | { |
| 115 | log<level::ERR>("Failed to parse JSON config file", |
Matthew Barth | 4fb0af9 | 2019-12-04 09:15:00 -0600 | [diff] [blame] | 116 | entry("JSON_FILE=%s", confFile.c_str()), |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 117 | entry("JSON_ERROR=%s", e.what())); |
| 118 | throw std::runtime_error("Failed to parse JSON config file"); |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | log<level::ERR>("Unable to open JSON config file", |
Matthew Barth | 4fb0af9 | 2019-12-04 09:15:00 -0600 | [diff] [blame] | 124 | entry("JSON_FILE=%s", confFile.c_str())); |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 125 | throw std::runtime_error("Unable to open JSON config file"); |
| 126 | } |
| 127 | |
| 128 | process(jsonConf); |
| 129 | } |
| 130 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 131 | void JsonConfig::process(const json& jsonConf) |
| 132 | { |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 133 | policies policies; |
| 134 | std::vector<fanPolicy> fans; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 135 | // Set the expected number of fan entries |
| 136 | // to be size of the list of fan json config entries |
| 137 | // (Must be done to eliminate vector reallocation of fan references) |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 138 | fans.reserve(jsonConf.size()); |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 139 | for (auto& member : jsonConf) |
| 140 | { |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 141 | if (!member.contains("name") || !member.contains("path") || |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 142 | !member.contains("methods") || !member.contains("rpolicy")) |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 143 | { |
| 144 | log<level::ERR>( |
| 145 | "Missing required fan presence properties", |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 146 | entry("REQUIRED_PROPERTIES=%s", |
| 147 | "{name, path, methods, rpolicy}")); |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 148 | throw std::runtime_error( |
| 149 | "Missing required fan presence properties"); |
| 150 | } |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 151 | |
| 152 | // Loop thru the configured methods of presence detection |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 153 | std::vector<std::unique_ptr<PresenceSensor>> sensors; |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 154 | for (auto& method : member["methods"].items()) |
| 155 | { |
| 156 | if (!method.value().contains("type")) |
| 157 | { |
| 158 | log<level::ERR>( |
| 159 | "Missing required fan presence method type", |
| 160 | entry("FAN_NAME=%s", |
| 161 | member["name"].get<std::string>().c_str())); |
| 162 | throw std::runtime_error( |
| 163 | "Missing required fan presence method type"); |
| 164 | } |
| 165 | // The method type of fan presence detection |
| 166 | // (Must have a supported function within the method namespace) |
| 167 | auto type = method.value()["type"].get<std::string>(); |
| 168 | std::transform(type.begin(), type.end(), type.begin(), tolower); |
| 169 | auto func = _methods.find(type); |
| 170 | if (func != _methods.end()) |
| 171 | { |
| 172 | // Call function for method type |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 173 | auto sensor = func->second(fans.size(), method.value()); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 174 | if (sensor) |
| 175 | { |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 176 | sensors.emplace_back(std::move(sensor)); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | log<level::ERR>("Invalid fan presence method type", |
| 182 | entry("FAN_NAME=%s", |
| 183 | member["name"].get<std::string>().c_str()), |
| 184 | entry("METHOD_TYPE=%s", type.c_str())); |
| 185 | throw std::runtime_error("Invalid fan presence method type"); |
| 186 | } |
| 187 | } |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 188 | auto fan = std::make_tuple(member["name"], member["path"]); |
| 189 | // Create a fan object |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 190 | fans.emplace_back(std::make_tuple(fan, std::move(sensors))); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 191 | |
| 192 | // Add fan presence policy |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 193 | auto policy = getPolicy(member["rpolicy"], fans.back()); |
| 194 | if (policy) |
| 195 | { |
| 196 | policies.emplace_back(std::move(policy)); |
| 197 | } |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 198 | } |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 199 | |
| 200 | // Success, refresh fans and policies lists |
| 201 | _fans.clear(); |
| 202 | _fans.swap(fans); |
| 203 | |
| 204 | _policies.clear(); |
| 205 | _policies.swap(policies); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 206 | } |
| 207 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 208 | std::unique_ptr<RedundancyPolicy> JsonConfig::getPolicy( |
| 209 | const json& rpolicy, |
| 210 | const fanPolicy& fpolicy) |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 211 | { |
| 212 | if (!rpolicy.contains("type")) |
| 213 | { |
| 214 | log<level::ERR>("Missing required fan presence policy type", |
| 215 | entry("FAN_NAME=%s", |
| 216 | std::get<fanPolicyFanPos>( |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 217 | std::get<Fan>(fpolicy)).c_str()), |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 218 | entry("REQUIRED_PROPERTIES=%s", "{type}")); |
| 219 | throw std::runtime_error("Missing required fan presence policy type"); |
| 220 | } |
| 221 | |
| 222 | // The redundancy policy type for fan presence detection |
| 223 | // (Must have a supported function within the rpolicy namespace) |
| 224 | auto type = rpolicy["type"].get<std::string>(); |
| 225 | std::transform(type.begin(), type.end(), type.begin(), tolower); |
| 226 | auto func = _rpolicies.find(type); |
| 227 | if (func != _rpolicies.end()) |
| 228 | { |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 229 | // Call function for redundancy policy type and return the policy |
| 230 | return func->second(fpolicy); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
| 234 | log<level::ERR>("Invalid fan presence policy type", |
| 235 | entry("FAN_NAME=%s", |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame] | 236 | std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()), |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 237 | entry("RPOLICY_TYPE=%s", type.c_str())); |
| 238 | throw std::runtime_error("Invalid fan presence methods policy type"); |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 242 | /** |
| 243 | * Methods of fan presence detection function definitions |
| 244 | */ |
| 245 | namespace method |
| 246 | { |
| 247 | // Get a constructed presence sensor for fan presence detection by tach |
| 248 | std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method) |
| 249 | { |
Matthew Barth | e0d98a3 | 2019-11-19 15:40:57 -0600 | [diff] [blame] | 250 | if (!method.contains("sensors") || |
| 251 | method["sensors"].size() == 0) |
| 252 | { |
| 253 | log<level::ERR>( |
| 254 | "Missing required tach method properties", |
| 255 | entry("FAN_ENTRY=%d", fanIndex), |
| 256 | entry("REQUIRED_PROPERTIES=%s", "{sensors}")); |
| 257 | throw std::runtime_error("Missing required tach method properties"); |
| 258 | } |
| 259 | |
| 260 | std::vector<std::string> sensors; |
| 261 | for (auto& sensor : method["sensors"]) |
| 262 | { |
| 263 | sensors.emplace_back(sensor.get<std::string>()); |
| 264 | } |
| 265 | |
| 266 | return std::make_unique<PolicyAccess<Tach, JsonConfig>>( |
| 267 | fanIndex, std::move(sensors)); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // Get a constructed presence sensor for fan presence detection by gpio |
| 271 | std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method) |
| 272 | { |
Matthew Barth | e0d98a3 | 2019-11-19 15:40:57 -0600 | [diff] [blame] | 273 | if (!method.contains("physpath") || |
| 274 | !method.contains("devpath") || |
| 275 | !method.contains("key")) |
| 276 | { |
| 277 | log<level::ERR>( |
| 278 | "Missing required gpio method properties", |
| 279 | entry("FAN_ENTRY=%d", fanIndex), |
| 280 | entry("REQUIRED_PROPERTIES=%s", "{physpath, devpath, key}")); |
| 281 | throw std::runtime_error("Missing required gpio method properties"); |
| 282 | } |
| 283 | |
| 284 | auto physpath = method["physpath"].get<std::string>(); |
| 285 | auto devpath = method["devpath"].get<std::string>(); |
| 286 | auto key = method["key"].get<unsigned int>(); |
| 287 | |
| 288 | return std::make_unique<PolicyAccess<Gpio, JsonConfig>>( |
| 289 | fanIndex, physpath, devpath, key); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | } // namespace method |
| 293 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 294 | /** |
| 295 | * Redundancy policies for fan presence detection function definitions |
| 296 | */ |
| 297 | namespace rpolicy |
| 298 | { |
| 299 | // Get an `Anyof` redundancy policy for the fan |
| 300 | std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan) |
| 301 | { |
Matthew Barth | 26ad44a | 2019-11-22 15:37:14 -0600 | [diff] [blame] | 302 | std::vector<std::reference_wrapper<PresenceSensor>> pSensors; |
| 303 | for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan)) |
| 304 | { |
| 305 | pSensors.emplace_back(*fanSensor); |
| 306 | } |
| 307 | |
| 308 | return std::make_unique<AnyOf>( |
| 309 | std::get<fanPolicyFanPos>(fan), pSensors); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // Get a `Fallback` redundancy policy for the fan |
| 313 | std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan) |
| 314 | { |
Matthew Barth | 26ad44a | 2019-11-22 15:37:14 -0600 | [diff] [blame] | 315 | std::vector<std::reference_wrapper<PresenceSensor>> pSensors; |
| 316 | for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan)) |
| 317 | { |
| 318 | // Place in the order given to fallback correctly |
| 319 | pSensors.emplace_back(*fanSensor); |
| 320 | } |
| 321 | |
| 322 | return std::make_unique<Fallback>( |
| 323 | std::get<fanPolicyFanPos>(fan), pSensors); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | } // namespace policy |
| 327 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 328 | } // namespace presence |
| 329 | } // namespace fan |
| 330 | } // namespace phosphor |