Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 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 | #pragma once |
| 17 | |
| 18 | #include "fan.hpp" |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 19 | #include "fan_error.hpp" |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 20 | #include "power_off_rule.hpp" |
| 21 | #include "power_state.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 22 | #include "tach_sensor.hpp" |
| 23 | #include "trust_manager.hpp" |
| 24 | #include "types.hpp" |
| 25 | |
| 26 | #include <nlohmann/json.hpp> |
| 27 | #include <sdbusplus/bus.hpp> |
| 28 | #include <sdeventplus/event.hpp> |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 29 | #include <sdeventplus/source/event.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 30 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 31 | |
| 32 | #include <memory> |
| 33 | #include <optional> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace phosphor::fan::monitor |
| 37 | { |
| 38 | |
| 39 | using json = nlohmann::json; |
| 40 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 41 | // Mapping from service name to sensor |
| 42 | using SensorMapType = |
| 43 | std::map<std::string, std::set<std::shared_ptr<TachSensor>>>; |
| 44 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 45 | class System |
| 46 | { |
| 47 | public: |
| 48 | System() = delete; |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 49 | ~System() = default; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 50 | System(const System&) = delete; |
| 51 | System(System&&) = delete; |
| 52 | System& operator=(const System&) = delete; |
| 53 | System& operator=(System&&) = delete; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Constructor |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 57 | * |
| 58 | * @param[in] mode - mode of fan monitor |
| 59 | * @param[in] bus - sdbusplus bus object |
| 60 | * @param[in] event - event loop reference |
| 61 | */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 62 | System(Mode mode, sdbusplus::bus_t& bus, const sdeventplus::Event& event); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 63 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 64 | /** |
| 65 | * @brief Callback function to handle receiving a HUP signal to reload the |
| 66 | * JSON configuration. |
| 67 | */ |
| 68 | void sighupHandler(sdeventplus::source::Signal&, |
| 69 | const struct signalfd_siginfo*); |
| 70 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 71 | /** |
| 72 | * @brief Called from the fan when it changes either |
| 73 | * present or functional status to update the |
| 74 | * fan health map. |
| 75 | * |
| 76 | * @param[in] fan - The fan that changed |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 77 | * @param[in] skipRulesCheck - If the rules checks should be done now. |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 78 | */ |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 79 | void fanStatusChange(const Fan& fan, bool skipRulesCheck = false); |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 80 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 81 | /** |
| 82 | * @brief Called when a fan sensor's error timer expires, which |
| 83 | * happens when the sensor has been nonfunctional for a |
| 84 | * certain amount of time. An event log will be created. |
| 85 | * |
| 86 | * @param[in] fan - The parent fan of the sensor |
| 87 | * @param[in] sensor - The faulted sensor |
| 88 | */ |
| 89 | void sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor); |
| 90 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 91 | /** |
| 92 | * @brief Called when the timer that starts when a fan is missing |
| 93 | * has expired so an event log needs to be created. |
| 94 | * |
| 95 | * @param[in] fan - The missing fan. |
| 96 | */ |
| 97 | void fanMissingErrorTimerExpired(const Fan& fan); |
| 98 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 99 | /** |
| 100 | * @brief Called by the power off actions to log an error when there is |
| 101 | * a power off due to fan problems. |
| 102 | * |
| 103 | * The error it logs is just the last fan error that occurred. |
| 104 | */ |
| 105 | void logShutdownError(); |
| 106 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 107 | /** |
| 108 | * @brief Returns true if power is on |
| 109 | */ |
| 110 | bool isPowerOn() const |
| 111 | { |
| 112 | return _powerState->isPowerOn(); |
| 113 | } |
| 114 | |
| 115 | /** |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 116 | * @brief tests the presence of Inventory and calls load() if present, else |
| 117 | * waits for Inventory asynchronously and has a callback to load() when |
| 118 | * present |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 119 | */ |
Matthew Barth | 823bc49 | 2021-06-21 14:19:09 -0500 | [diff] [blame] | 120 | void start(); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 121 | |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 122 | /** |
| 123 | * @brief Parses and populates the fan monitor trust groups and list of fans |
| 124 | */ |
| 125 | void load(); |
| 126 | |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 127 | /** |
| 128 | * @brief Callback function to handle receiving a USR1 signal to dump |
| 129 | * debug data to a file. |
| 130 | */ |
| 131 | void dumpDebugData(sdeventplus::source::Signal&, |
| 132 | const struct signalfd_siginfo*); |
| 133 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 134 | private: |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 135 | /** |
| 136 | * @brief Callback from D-Bus when Inventory service comes online |
| 137 | * |
| 138 | * @param[in] msg - Service details. |
| 139 | */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 140 | void inventoryOnlineCb(sdbusplus::message_t& msg); |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 141 | |
Mike Capps | 683a96c | 2022-04-27 16:46:06 -0400 | [diff] [blame] | 142 | /** |
| 143 | * @brief Create a BMC Dump |
| 144 | */ |
| 145 | void createBmcDump() const; |
| 146 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 147 | /* The mode of fan monitor */ |
| 148 | Mode _mode; |
| 149 | |
| 150 | /* The sdbusplus bus object */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 151 | sdbusplus::bus_t& _bus; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 152 | |
| 153 | /* The event loop reference */ |
| 154 | const sdeventplus::Event& _event; |
| 155 | |
| 156 | /* Trust manager of trust groups */ |
| 157 | std::unique_ptr<phosphor::fan::trust::Manager> _trust; |
| 158 | |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 159 | /* match object to detect Inventory service */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 160 | std::unique_ptr<sdbusplus::bus::match_t> _inventoryMatch; |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 161 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 162 | /* List of fan objects to monitor */ |
| 163 | std::vector<std::unique_ptr<Fan>> _fans; |
| 164 | |
| 165 | /** |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 166 | * @brief The latest health of all the fans |
| 167 | */ |
| 168 | FanHealth _fanHealth; |
| 169 | |
| 170 | /** |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 171 | * @brief The object to watch the power state |
| 172 | */ |
| 173 | std::unique_ptr<PowerState> _powerState; |
| 174 | |
| 175 | /** |
| 176 | * @brief The power off rules, for shutting down the system |
| 177 | * due to fan failures. |
| 178 | */ |
| 179 | std::vector<std::unique_ptr<PowerOffRule>> _powerOffRules; |
| 180 | |
| 181 | /** |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 182 | * @brief The number of concurrently nonfunctional fan sensors |
| 183 | * there must be for an event log created due to a |
| 184 | * nonfunctional fan sensor to have an Error severity as |
| 185 | * opposed to an Informational one. |
| 186 | */ |
| 187 | std::optional<size_t> _numNonfuncSensorsBeforeError; |
| 188 | |
| 189 | /** |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 190 | * @brief The most recently committed fan error. |
| 191 | */ |
| 192 | std::unique_ptr<FanError> _lastError; |
| 193 | |
| 194 | /** |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 195 | * @brief The thermal alert D-Bus object |
| 196 | */ |
| 197 | ThermalAlertObject _thermalAlert; |
| 198 | |
| 199 | /** |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 200 | * @brief The tach sensors D-Bus match objects |
| 201 | */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 202 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> _sensorMatch; |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 203 | |
| 204 | /** |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 205 | * @brief true if config files have been loaded |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 206 | */ |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 207 | bool _loaded = false; |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 208 | |
| 209 | /** |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 210 | * @brief The name of the dump file |
| 211 | */ |
| 212 | static const std::string dumpFile; |
| 213 | |
| 214 | /** |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 215 | * @brief Captures tach sensor data as JSON for use in |
| 216 | * fan fault and fan missing event logs. |
| 217 | * |
| 218 | * @return json - The JSON data |
| 219 | */ |
| 220 | json captureSensorData(); |
| 221 | |
| 222 | /** |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 223 | * @brief creates a subscription (service->sensor) to take sensors |
| 224 | * on/offline when D-Bus starts/stops updating values |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 225 | * |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 226 | */ |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 227 | void subscribeSensorsToServices(); |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 228 | |
| 229 | /** |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 230 | * @brief Retrieve the configured trust groups |
| 231 | * |
| 232 | * @param[in] jsonObj - JSON object to parse from |
| 233 | * |
| 234 | * @return List of functions applied on trust groups |
| 235 | */ |
| 236 | const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj); |
| 237 | |
| 238 | /** |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 239 | * @brief Set the trust manager's list of trust group functions |
| 240 | * |
| 241 | * @param[in] groupFuncs - list of trust group functions |
| 242 | */ |
| 243 | void setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs); |
| 244 | |
| 245 | /** |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 246 | * @brief Retrieve the configured fan definitions |
| 247 | * |
| 248 | * @param[in] jsonObj - JSON object to parse from |
| 249 | * |
| 250 | * @return List of fan definition data on the fans configured |
| 251 | */ |
| 252 | const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * @brief Set the list of fans to be monitored |
| 256 | * |
| 257 | * @param[in] fanDefs - list of fan definitions to create fans monitored |
| 258 | */ |
| 259 | void setFans(const std::vector<FanDefinition>& fanDefs); |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * @brief Updates the fan health map entry for the fan passed in |
| 263 | * |
| 264 | * @param[in] fan - The fan to update the health map with |
| 265 | */ |
| 266 | void updateFanHealth(const Fan& fan); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 267 | |
| 268 | /** |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 269 | * @brief callback when a tach sensor signal goes offline |
| 270 | * |
| 271 | * @param[in] msg - D-Bus message containing details (inc. service name) |
| 272 | * |
| 273 | * @param[in] sensorMap - map providing sensor access for each service |
| 274 | */ |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 275 | void tachSignalOffline(sdbusplus::message_t& msg, |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 276 | const SensorMapType& sensorMap); |
| 277 | |
| 278 | /** |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 279 | * @brief The function that runs when the power state changes |
| 280 | * |
| 281 | * @param[in] powerStateOn - If power is now on or not |
| 282 | */ |
| 283 | void powerStateChanged(bool powerStateOn); |
| 284 | |
| 285 | /** |
| 286 | * @brief Reads the fault configuration from the JSON config |
| 287 | * file, such as the power off rule configuration. |
| 288 | * |
| 289 | * @param[in] jsonObj - JSON object to parse from |
| 290 | */ |
| 291 | void setFaultConfig(const json& jsonObj); |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * @brief Log an error and shut down due to an offline fan controller |
| 295 | */ |
| 296 | void handleOfflineFanController(); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | } // namespace phosphor::fan::monitor |