Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | #include "bcd_time.hpp" |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 3 | #include "paths.hpp" |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 4 | #include "pel.hpp" |
| 5 | |
| 6 | #include <algorithm> |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 7 | #include <bitset> |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 8 | #include <filesystem> |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 9 | #include <map> |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace pels |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @class Repository |
| 18 | * |
| 19 | * The class handles saving and retrieving PELs on the BMC. |
| 20 | */ |
| 21 | class Repository |
| 22 | { |
| 23 | public: |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 24 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 25 | * @brief Structure of commonly used PEL attributes. |
| 26 | */ |
| 27 | struct PELAttributes |
| 28 | { |
| 29 | std::filesystem::path path; |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 30 | size_t sizeOnDisk; |
| 31 | uint8_t creator; |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 32 | uint8_t subsystem; |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 33 | uint8_t severity; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 34 | std::bitset<16> actionFlags; |
Matt Spinler | 346f99a | 2019-11-21 13:06:35 -0600 | [diff] [blame] | 35 | TransmissionState hostState; |
| 36 | TransmissionState hmcState; |
Matt Spinler | 8e65f4e | 2023-05-02 13:40:08 -0500 | [diff] [blame] | 37 | uint32_t plid; |
| 38 | bool deconfig; |
| 39 | bool guard; |
| 40 | uint64_t creationTime; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 41 | |
| 42 | PELAttributes() = delete; |
| 43 | |
Matt Spinler | dd325c3 | 2020-07-07 11:01:54 -0500 | [diff] [blame] | 44 | PELAttributes(const std::filesystem::path& p, size_t size, |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 45 | uint8_t creator, uint8_t subsystem, uint8_t sev, |
| 46 | uint16_t flags, TransmissionState hostState, |
Matt Spinler | 8e65f4e | 2023-05-02 13:40:08 -0500 | [diff] [blame] | 47 | TransmissionState hmcState, uint32_t plid, bool deconfig, |
| 48 | bool guard, uint64_t creationTime) : |
Matt Spinler | 346f99a | 2019-11-21 13:06:35 -0600 | [diff] [blame] | 49 | path(p), |
Vijay Lobo | afb1b46 | 2021-07-21 23:29:13 -0500 | [diff] [blame] | 50 | sizeOnDisk(size), creator(creator), subsystem(subsystem), |
| 51 | severity(sev), actionFlags(flags), hostState(hostState), |
Matt Spinler | 8e65f4e | 2023-05-02 13:40:08 -0500 | [diff] [blame] | 52 | hmcState(hmcState), plid(plid), deconfig(deconfig), guard(guard), |
| 53 | creationTime(creationTime) |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 54 | {} |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /** |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 58 | * @brief A structure that holds both the PEL and corresponding |
| 59 | * OpenBMC IDs. |
| 60 | * Used for correlating the IDs with their data files for quick |
| 61 | * lookup. To find a PEL based on just one of the IDs, just use |
| 62 | * the constructor that takes that ID. |
| 63 | */ |
| 64 | struct LogID |
| 65 | { |
| 66 | struct Pel |
| 67 | { |
| 68 | uint32_t id; |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 69 | explicit Pel(uint32_t i) : id(i) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 70 | }; |
| 71 | struct Obmc |
| 72 | { |
| 73 | uint32_t id; |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 74 | explicit Obmc(uint32_t i) : id(i) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | Pel pelID; |
| 78 | |
| 79 | Obmc obmcID; |
| 80 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 81 | LogID(Pel pel, Obmc obmc) : pelID(pel), obmcID(obmc) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 82 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 83 | explicit LogID(Pel id) : pelID(id), obmcID(0) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 84 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 85 | explicit LogID(Obmc id) : pelID(0), obmcID(id) {} |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 86 | |
| 87 | LogID() = delete; |
| 88 | |
| 89 | /** |
| 90 | * @brief A == operator that will match on either ID |
| 91 | * being equal if the other is zero, so that |
| 92 | * one can look up a PEL with just one of the IDs. |
| 93 | */ |
| 94 | bool operator==(const LogID& id) const |
| 95 | { |
| 96 | if (id.pelID.id != 0) |
| 97 | { |
| 98 | return id.pelID.id == pelID.id; |
| 99 | } |
| 100 | if (id.obmcID.id != 0) |
| 101 | { |
| 102 | return id.obmcID.id == obmcID.id; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | bool operator<(const LogID& id) const |
| 108 | { |
| 109 | return pelID.id < id.pelID.id; |
| 110 | } |
| 111 | }; |
| 112 | |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 113 | using AttributesReference = |
| 114 | std::reference_wrapper<const std::pair<const LogID, PELAttributes>>; |
| 115 | |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 116 | /** |
| 117 | * @brief A structure for keeping a breakdown of the sizes of PELs |
| 118 | * of different types in the repository. |
| 119 | */ |
| 120 | struct SizeStats |
| 121 | { |
| 122 | uint64_t total; |
| 123 | uint64_t bmc; |
| 124 | uint64_t nonBMC; |
| 125 | uint64_t bmcServiceable; |
| 126 | uint64_t bmcInfo; |
| 127 | uint64_t nonBMCServiceable; |
| 128 | uint64_t nonBMCInfo; |
| 129 | |
| 130 | SizeStats() : |
| 131 | total(0), bmc(0), nonBMC(0), bmcServiceable(0), bmcInfo(0), |
| 132 | nonBMCServiceable(0), nonBMCInfo(0) |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 133 | {} |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 134 | }; |
| 135 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 136 | Repository() = delete; |
| 137 | ~Repository() = default; |
| 138 | Repository(const Repository&) = default; |
| 139 | Repository& operator=(const Repository&) = default; |
| 140 | Repository(Repository&&) = default; |
| 141 | Repository& operator=(Repository&&) = default; |
| 142 | |
| 143 | /** |
| 144 | * @brief Constructor |
| 145 | * |
| 146 | * @param[in] basePath - the base filesystem path for the repository |
| 147 | */ |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 148 | explicit Repository(const std::filesystem::path& basePath) : |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 149 | Repository(basePath, getPELRepoSize(), getMaxNumPELs()) |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 150 | {} |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * @brief Constructor that takes the repository size |
| 154 | * |
| 155 | * @param[in] basePath - the base filesystem path for the repository |
| 156 | * @param[in] repoSize - The maximum amount of space to use for PELs, |
| 157 | * in bytes |
| 158 | * @param[in] maxNumPELs - The maximum number of PELs to allow |
| 159 | */ |
| 160 | Repository(const std::filesystem::path& basePath, size_t repoSize, |
| 161 | size_t maxNumPELs); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * @brief Adds a PEL to the repository |
| 165 | * |
| 166 | * Throws File.Error.Open or File.Error.Write exceptions on failure |
| 167 | * |
| 168 | * @param[in] pel - the PEL to add |
| 169 | */ |
| 170 | void add(std::unique_ptr<PEL>& pel); |
| 171 | |
| 172 | /** |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 173 | * @brief Removes a PEL from the repository |
| 174 | * |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 175 | * Note that the returned LogID is the fully filled in LogID, i.e. |
| 176 | * it has both the PEL and OpenBMC IDs, unlike the passed in LogID |
| 177 | * which can just have one or the other. |
| 178 | * |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 179 | * @param[in] id - the ID (either the pel ID, OBMC ID, or both) to remove |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 180 | * |
| 181 | * @return std::optional<LogID> - The LogID of the removed PEL |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 182 | */ |
Matt Spinler | 52602e3 | 2020-07-15 12:37:28 -0500 | [diff] [blame] | 183 | std::optional<LogID> remove(const LogID& id); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 184 | |
| 185 | /** |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 186 | * @brief Generates the filename to use for the PEL ID and BCDTime. |
| 187 | * |
| 188 | * @param[in] pelID - the PEL ID |
| 189 | * @param[in] time - the BCD time |
| 190 | * |
| 191 | * @return string - A filename string of <BCD_time>_<pelID> |
| 192 | */ |
| 193 | static std::string getPELFilename(uint32_t pelID, const BCDTime& time); |
| 194 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 195 | /** |
| 196 | * @brief Returns true if the PEL with the specified ID is in the repo. |
| 197 | * |
| 198 | * @param[in] id - the ID (either the pel ID, OBMC ID, or both) |
| 199 | * @return bool - true if that PEL is present |
| 200 | */ |
| 201 | inline bool hasPEL(const LogID& id) |
| 202 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 203 | return findPEL(id) != _pelAttributes.end(); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 206 | /** |
| 207 | * @brief Returns the PEL data based on its ID. |
| 208 | * |
| 209 | * If the data can't be found for that ID, then the optional object |
| 210 | * will be empty. |
| 211 | * |
| 212 | * @param[in] id - the LogID to get the PEL for, which can be either a |
| 213 | * PEL ID or OpenBMC log ID. |
| 214 | * @return std::optional<std::vector<uint8_t>> - the PEL data |
| 215 | */ |
| 216 | std::optional<std::vector<uint8_t>> getPELData(const LogID& id); |
| 217 | |
Matt Spinler | 6d51224 | 2019-12-09 13:44:17 -0600 | [diff] [blame] | 218 | /** |
| 219 | * @brief Get a file descriptor to the PEL data |
| 220 | * |
| 221 | * @param[in] id - The ID to get the FD for |
| 222 | * |
| 223 | * @return std::optional<sdbusplus::message::unix_fd> - |
| 224 | * The FD, or an empty optional object. |
| 225 | */ |
| 226 | std::optional<sdbusplus::message::unix_fd> getPELFD(const LogID& id); |
| 227 | |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 228 | using ForEachFunc = std::function<bool(const PEL&)>; |
| 229 | |
| 230 | /** |
| 231 | * @brief Run a user defined function on every PEL in the repository. |
| 232 | * |
| 233 | * ForEachFunc takes a const PEL reference, and should return |
| 234 | * true to stop iterating and return out of for_each. |
| 235 | * |
| 236 | * For example, to save up to 100 IDs in the repo into a vector: |
| 237 | * |
| 238 | * std::vector<uint32_t> ids; |
| 239 | * ForEachFunc f = [&ids](const PEL& pel) { |
| 240 | * ids.push_back(pel.id()); |
| 241 | * return ids.size() == 100 ? true : false; |
| 242 | * }; |
| 243 | * |
| 244 | * @param[in] func - The function to run. |
| 245 | */ |
| 246 | void for_each(ForEachFunc func) const; |
| 247 | |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 248 | using AddCallback = std::function<void(const PEL&)>; |
| 249 | |
| 250 | /** |
| 251 | * @brief Subscribe to PELs being added to the repository. |
| 252 | * |
| 253 | * Every time a PEL is added to the repository, the provided |
| 254 | * function will be called with the new PEL as the argument. |
| 255 | * |
| 256 | * The function must be of type void(const PEL&). |
| 257 | * |
| 258 | * @param[in] name - The subscription name |
| 259 | * @param[in] func - The callback function |
| 260 | */ |
| 261 | void subscribeToAdds(const std::string& name, AddCallback func) |
| 262 | { |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 263 | _addSubscriptions.emplace(name, func); |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @brief Unsubscribe from new PELs. |
| 268 | * |
| 269 | * @param[in] name - The subscription name |
| 270 | */ |
| 271 | void unsubscribeFromAdds(const std::string& name) |
| 272 | { |
| 273 | _addSubscriptions.erase(name); |
| 274 | } |
| 275 | |
| 276 | using DeleteCallback = std::function<void(uint32_t)>; |
| 277 | |
| 278 | /** |
| 279 | * @brief Subscribe to PELs being deleted from the repository. |
| 280 | * |
| 281 | * Every time a PEL is deleted from the repository, the provided |
| 282 | * function will be called with the PEL ID as the argument. |
| 283 | * |
| 284 | * The function must be of type void(const uint32_t). |
| 285 | * |
| 286 | * @param[in] name - The subscription name |
| 287 | * @param[in] func - The callback function |
| 288 | */ |
| 289 | void subscribeToDeletes(const std::string& name, DeleteCallback func) |
| 290 | { |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 291 | _deleteSubscriptions.emplace(name, func); |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
| 295 | * @brief Unsubscribe from deleted PELs. |
| 296 | * |
| 297 | * @param[in] name - The subscription name |
| 298 | */ |
| 299 | void unsubscribeFromDeletes(const std::string& name) |
| 300 | { |
| 301 | _deleteSubscriptions.erase(name); |
| 302 | } |
| 303 | |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 304 | /** |
| 305 | * @brief Get the PEL attributes for a PEL |
| 306 | * |
| 307 | * @param[in] id - The ID to find the attributes for |
| 308 | * |
| 309 | * @return The attributes or an empty optional if not found |
| 310 | */ |
| 311 | std::optional<std::reference_wrapper<const PELAttributes>> |
| 312 | getPELAttributes(const LogID& id) const; |
| 313 | |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 314 | /** |
Matt Spinler | 0dd22c8 | 2023-05-04 15:28:12 -0500 | [diff] [blame] | 315 | * @brief Returns the attributes map so that others can traverse PELs. |
| 316 | * |
| 317 | * @return - A const reference to the attributes map. |
| 318 | */ |
| 319 | const std::map<LogID, PELAttributes>& getAttributesMap() const |
| 320 | { |
| 321 | return _pelAttributes; |
| 322 | } |
| 323 | |
| 324 | /** |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 325 | * @brief Sets the host transmission state on a PEL file |
| 326 | * |
| 327 | * Writes the host transmission state field in the User Header |
| 328 | * section in the PEL data specified by the ID. |
| 329 | * |
| 330 | * @param[in] pelID - The PEL ID |
| 331 | * @param[in] state - The state to write |
| 332 | */ |
| 333 | void setPELHostTransState(uint32_t pelID, TransmissionState state); |
| 334 | |
| 335 | /** |
| 336 | * @brief Sets the HMC transmission state on a PEL file |
| 337 | * |
| 338 | * Writes the HMC transmission state field in the User Header |
| 339 | * section in the PEL data specified by the ID. |
| 340 | * |
| 341 | * @param[in] pelID - The PEL ID |
| 342 | * @param[in] state - The state to write |
| 343 | */ |
| 344 | void setPELHMCTransState(uint32_t pelID, TransmissionState state); |
| 345 | |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 346 | /** |
| 347 | * @brief Returns the size stats structure |
| 348 | * |
| 349 | * @return const SizeStats& - The stats structure |
| 350 | */ |
| 351 | const SizeStats& getSizeStats() const |
| 352 | { |
| 353 | return _sizes; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * @brief Says if the PEL is considered serviceable (not just |
| 358 | * informational) as determined by its severity. |
| 359 | * |
| 360 | * @param[in] pel - The PELAttributes entry for the PEL |
| 361 | * @return bool - If serviceable or not |
| 362 | */ |
| 363 | static bool isServiceableSev(const PELAttributes& pel); |
| 364 | |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 365 | /** |
Matt Spinler | 7e727a3 | 2020-07-07 15:00:17 -0500 | [diff] [blame] | 366 | * @brief Returns true if the total amount of disk space occupied |
| 367 | * by the PELs in the repo is over 95% of the maximum |
| 368 | * size, or if there are over the maximum number of |
| 369 | * PELs allowed. |
| 370 | * |
| 371 | * @return bool - true if repo is > 95% full or too many PELs |
| 372 | */ |
Sumit Kumar | c296692 | 2021-07-21 10:14:03 -0500 | [diff] [blame] | 373 | bool sizeWarning(); |
Matt Spinler | 7e727a3 | 2020-07-07 15:00:17 -0500 | [diff] [blame] | 374 | |
| 375 | /** |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 376 | * @brief Deletes PELs to bring the repository size down |
| 377 | * to at most 90% full by placing PELs into 4 different |
| 378 | * catogories and then removing PELs until those catogories |
| 379 | * only take up certain percentages of the allowed space. |
| 380 | * |
| 381 | * This does not delete the corresponding OpenBMC event logs, which |
| 382 | * is why those IDs are returned, so they can be deleted later. |
| 383 | * |
| 384 | * The categories and their rules are: |
| 385 | * 1) Informational BMC PELs cannot take up more than 15% of |
| 386 | * the allocated space. |
| 387 | * 2) Non-informational BMC PELs cannot take up more than 30% |
| 388 | * of the allocated space. |
| 389 | * 3) Informational non-BMC PELs cannot take up more than 15% of |
| 390 | * the allocated space. |
| 391 | * 4) Non-informational non-BMC PELs cannot take up more than 30% |
| 392 | * of the allocated space. |
| 393 | * |
| 394 | * While removing PELs in a category, 4 passes will be made, with |
| 395 | * PELs being removed oldest first during each pass. |
| 396 | * |
| 397 | * Pass 1: only delete HMC acked PELs |
| 398 | * Pass 2: only delete OS acked PELs |
| 399 | * Pass 3: only delete PHYP sent PELs |
| 400 | * Pass 4: delete all PELs |
| 401 | * |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 402 | * @param[in] ids - The OpenBMC event log Ids with hardware isolation entry. |
| 403 | * |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 404 | * @return std::vector<uint32_t> - The OpenBMC event log IDs of |
| 405 | * the PELs that were deleted. |
| 406 | */ |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 407 | std::vector<uint32_t> prune(const std::vector<uint32_t>& idsWithHwIsoEntry); |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 408 | |
Matt Spinler | ff9cec2 | 2020-07-15 13:06:35 -0500 | [diff] [blame] | 409 | /** |
| 410 | * @brief Returns the path to the directory where the PEL |
| 411 | * files are stored. |
| 412 | * |
| 413 | * @return std::filesystem::path - The directory path |
| 414 | */ |
| 415 | const std::filesystem::path& repoPath() const |
| 416 | { |
| 417 | return _logPath; |
| 418 | } |
| 419 | |
Matt Spinler | 44893cc | 2020-08-26 11:34:17 -0500 | [diff] [blame] | 420 | /** |
| 421 | * @brief Returns the ID of the most recently added PEL. |
| 422 | * |
| 423 | * @return uint32_t - The PEL ID |
| 424 | */ |
| 425 | uint32_t lastPelID() const |
| 426 | { |
| 427 | return _lastPelID; |
| 428 | } |
| 429 | |
Ramesh Iyyar | 99f3717 | 2021-06-24 05:41:51 -0500 | [diff] [blame] | 430 | /** |
| 431 | * @brief Get the LogID based on the given ObmcLogId or PelId. |
| 432 | * |
| 433 | * @param[in] id - The ID to find the LogID. |
| 434 | * |
| 435 | * @return The LogID or an empty optional if not found. |
| 436 | * |
| 437 | * @note The returned LogID is the fully filled in LogID, i.e. |
| 438 | * it has both the PEL and OpenBMC Log IDs, unlike the passed in LogID |
| 439 | * which can just have one or the other. |
| 440 | */ |
| 441 | std::optional<LogID> getLogID(const LogID& id) const |
| 442 | { |
| 443 | if (auto logID = findPEL(id); logID != _pelAttributes.end()) |
| 444 | { |
| 445 | return logID->first; |
| 446 | } |
| 447 | return std::nullopt; |
| 448 | } |
| 449 | |
Sumit Kumar | 2ccdcef | 2021-07-31 10:04:58 -0500 | [diff] [blame] | 450 | /** |
| 451 | * @brief Save the PEL to archive folder |
| 452 | * |
| 453 | * @param[in] pel - The PEL data |
| 454 | */ |
| 455 | void archivePEL(const PEL& pel); |
| 456 | |
Matt Spinler | d0ccda3 | 2023-05-04 09:59:59 -0500 | [diff] [blame] | 457 | using PELUpdateFunc = std::function<bool(PEL&)>; |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * @brief Lets a function modify a PEL and saves the results |
| 461 | * |
Matt Spinler | d0ccda3 | 2023-05-04 09:59:59 -0500 | [diff] [blame] | 462 | * Runs updateFunc (a bool(PEL&) function) on the PEL data |
| 463 | * on the file specified, and writes the results back to the file |
| 464 | * if the function returned true. |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 465 | * |
| 466 | * @param[in] path - The file path to use |
| 467 | * @param[in] updateFunc - The function to run to update the PEL. |
Matt Spinler | 1cb59f7 | 2023-07-20 09:49:50 -0500 | [diff] [blame] | 468 | * |
| 469 | * @return bool - If the PEL was updated or not. |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 470 | */ |
Matt Spinler | 1cb59f7 | 2023-07-20 09:49:50 -0500 | [diff] [blame] | 471 | bool updatePEL(const std::filesystem::path& path, PELUpdateFunc updateFunc); |
Matt Spinler | 29d18c1 | 2019-11-21 13:31:27 -0600 | [diff] [blame] | 472 | |
Matt Spinler | 0dd22c8 | 2023-05-04 15:28:12 -0500 | [diff] [blame] | 473 | private: |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 474 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 475 | * @brief Finds an entry in the _pelAttributes map. |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 476 | * |
| 477 | * @param[in] id - the ID (either the pel ID, OBMC ID, or both) |
| 478 | * |
| 479 | * @return an iterator to the entry |
| 480 | */ |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 481 | std::map<LogID, PELAttributes>::const_iterator |
| 482 | findPEL(const LogID& id) const |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 483 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 484 | return std::find_if(_pelAttributes.begin(), _pelAttributes.end(), |
| 485 | [&id](const auto& a) { return a.first == id; }); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /** |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 489 | * @brief Call any subscribed functions for new PELs |
| 490 | * |
| 491 | * @param[in] pel - The new PEL |
| 492 | */ |
| 493 | void processAddCallbacks(const PEL& pel) const; |
| 494 | |
| 495 | /** |
| 496 | * @brief Call any subscribed functions for deleted PELs |
| 497 | * |
| 498 | * @param[in] id - The ID of the deleted PEL |
| 499 | */ |
| 500 | void processDeleteCallbacks(uint32_t id) const; |
| 501 | |
| 502 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 503 | * @brief Restores the _pelAttributes map on startup based on the existing |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 504 | * PEL data files. |
| 505 | */ |
| 506 | void restore(); |
| 507 | |
| 508 | /** |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame] | 509 | * @brief Stores a PEL object in the filesystem. |
| 510 | * |
| 511 | * @param[in] pel - The PEL to write |
| 512 | * @param[in] path - The file to write to |
| 513 | * |
| 514 | * Throws exceptions on failures. |
| 515 | */ |
| 516 | void write(const PEL& pel, const std::filesystem::path& path); |
| 517 | |
| 518 | /** |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 519 | * @brief Updates the repository statistics after a PEL is |
| 520 | * added or removed. |
| 521 | * |
| 522 | * @param[in] pel - The PELAttributes entry for the PEL |
| 523 | * @param[in] pelAdded - true if the PEL was added, false if removed |
| 524 | */ |
| 525 | void updateRepoStats(const PELAttributes& pel, bool pelAdded); |
| 526 | |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 527 | enum class SortOrder |
| 528 | { |
| 529 | ascending, |
| 530 | descending |
| 531 | }; |
| 532 | |
| 533 | /** |
| 534 | * @brief Returns a vector of all the _pelAttributes entries sorted |
| 535 | * as specified |
| 536 | * |
| 537 | * @param[in] order - If the PELs should be returned in ascending |
| 538 | * (oldest first) or descending order. |
| 539 | * |
| 540 | * @return std::vector<AttributesReference> - The sorted vector of |
| 541 | * references to the pair<LogID, PELAttributes> entries of |
| 542 | * _pelAttributes. |
| 543 | */ |
| 544 | std::vector<AttributesReference> getAllPELAttributes(SortOrder order) const; |
| 545 | |
| 546 | using IsOverLimitFunc = std::function<bool()>; |
| 547 | using IsPELTypeFunc = std::function<bool(const PELAttributes&)>; |
| 548 | |
| 549 | /** |
| 550 | * @brief Makes 4 passes on the PELs that meet the IsPELTypeFunc |
| 551 | * criteria removing PELs until IsOverLimitFunc returns false. |
| 552 | * |
| 553 | * Pass 1: only delete HMC acked PELs |
| 554 | * Pass 2: only delete Os acked PELs |
| 555 | * Pass 3: only delete PHYP sent PELs |
| 556 | * Pass 4: delete all PELs |
| 557 | * |
| 558 | * @param[in] isOverLimit - The bool(void) function that should |
| 559 | * return true if PELs still need to be |
| 560 | * removed. |
| 561 | * @param[in] isPELType - The bool(const PELAttributes&) function |
| 562 | * used to select the PELs to operate on. |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 563 | * @param[in] ids - The OpenBMC event log Ids with hardware isolation |
| 564 | * entry. |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 565 | * |
| 566 | * @param[out] removedBMCLogIDs - The OpenBMC event log IDs of the |
| 567 | * removed PELs. |
| 568 | */ |
Matt Spinler | 45796e8 | 2022-07-01 11:25:27 -0500 | [diff] [blame] | 569 | void removePELs(const IsOverLimitFunc& isOverLimit, |
| 570 | const IsPELTypeFunc& isPELType, |
Sumit Kumar | 027bf28 | 2022-01-24 11:25:19 -0600 | [diff] [blame] | 571 | const std::vector<uint32_t>& idsWithHwIsoEntry, |
Matt Spinler | b0a8df5 | 2020-07-07 14:41:06 -0500 | [diff] [blame] | 572 | std::vector<uint32_t>& removedBMCLogIDs); |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 573 | /** |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 574 | * @brief The filesystem path to the PEL logs. |
| 575 | */ |
| 576 | const std::filesystem::path _logPath; |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 577 | |
| 578 | /** |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 579 | * @brief A map of the PEL/OBMC IDs to PEL attributes. |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 580 | */ |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 581 | std::map<LogID, PELAttributes> _pelAttributes; |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 582 | |
| 583 | /** |
| 584 | * @brief Subcriptions for new PELs. |
| 585 | */ |
| 586 | std::map<std::string, AddCallback> _addSubscriptions; |
| 587 | |
| 588 | /** |
| 589 | * @brief Subscriptions for deleted PELs. |
| 590 | */ |
| 591 | std::map<std::string, DeleteCallback> _deleteSubscriptions; |
Matt Spinler | 8d5f3a2 | 2020-07-07 10:30:33 -0500 | [diff] [blame] | 592 | |
| 593 | /** |
| 594 | * @brief The maximum amount of space that the PELs in the |
| 595 | * repository can occupy. |
| 596 | */ |
| 597 | const uint64_t _maxRepoSize; |
| 598 | |
| 599 | /** |
| 600 | * @brief The maximum number of PELs to allow in the repo |
| 601 | * before pruning. |
| 602 | */ |
| 603 | const size_t _maxNumPELs; |
Matt Spinler | b188f78 | 2020-07-07 11:18:12 -0500 | [diff] [blame] | 604 | |
| 605 | /** |
| 606 | * @brief Statistics on the sizes of the stored PELs. |
| 607 | */ |
| 608 | SizeStats _sizes; |
Matt Spinler | 44893cc | 2020-08-26 11:34:17 -0500 | [diff] [blame] | 609 | |
| 610 | /** |
| 611 | * @brief The ID of the most recently added PEL. |
| 612 | */ |
| 613 | uint32_t _lastPelID = 0; |
Sumit Kumar | 1d8835b | 2021-06-07 09:35:30 -0500 | [diff] [blame] | 614 | |
| 615 | /** |
| 616 | * @brief The filesystem path to the archive PEL logs. |
| 617 | */ |
| 618 | const std::filesystem::path _archivePath; |
| 619 | |
| 620 | /** |
| 621 | * @brief The size of archive folder. |
| 622 | */ |
| 623 | uint64_t _archiveSize = 0; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | } // namespace pels |
| 627 | } // namespace openpower |