1711d51d8SMatt Spinler /** 2711d51d8SMatt Spinler * Copyright © 2019 IBM Corporation 3711d51d8SMatt Spinler * 4711d51d8SMatt Spinler * Licensed under the Apache License, Version 2.0 (the "License"); 5711d51d8SMatt Spinler * you may not use this file except in compliance with the License. 6711d51d8SMatt Spinler * You may obtain a copy of the License at 7711d51d8SMatt Spinler * 8711d51d8SMatt Spinler * http://www.apache.org/licenses/LICENSE-2.0 9711d51d8SMatt Spinler * 10711d51d8SMatt Spinler * Unless required by applicable law or agreed to in writing, software 11711d51d8SMatt Spinler * distributed under the License is distributed on an "AS IS" BASIS, 12711d51d8SMatt Spinler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13711d51d8SMatt Spinler * See the License for the specific language governing permissions and 14711d51d8SMatt Spinler * limitations under the License. 15711d51d8SMatt Spinler */ 1689fa082aSMatt Spinler #include "repository.hpp" 1789fa082aSMatt Spinler 18dd325c32SMatt Spinler #include <sys/stat.h> 19dd325c32SMatt Spinler 2089fa082aSMatt Spinler #include <fstream> 2189fa082aSMatt Spinler #include <phosphor-logging/log.hpp> 2289fa082aSMatt Spinler #include <xyz/openbmc_project/Common/File/error.hpp> 2389fa082aSMatt Spinler 2489fa082aSMatt Spinler namespace openpower 2589fa082aSMatt Spinler { 2689fa082aSMatt Spinler namespace pels 2789fa082aSMatt Spinler { 2889fa082aSMatt Spinler 2989fa082aSMatt Spinler namespace fs = std::filesystem; 3089fa082aSMatt Spinler using namespace phosphor::logging; 3189fa082aSMatt Spinler namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; 3289fa082aSMatt Spinler 33dd325c32SMatt Spinler /** 34dd325c32SMatt Spinler * @brief Returns the amount of space the file uses on disk. 35dd325c32SMatt Spinler * 36dd325c32SMatt Spinler * This is different than just the regular size of the file. 37dd325c32SMatt Spinler * 38dd325c32SMatt Spinler * @param[in] file - The file to get the size of 39dd325c32SMatt Spinler * 40dd325c32SMatt Spinler * @return size_t The disk space the file uses 41dd325c32SMatt Spinler */ 42dd325c32SMatt Spinler size_t getFileDiskSize(const std::filesystem::path& file) 43dd325c32SMatt Spinler { 44dd325c32SMatt Spinler constexpr size_t statBlockSize = 512; 45dd325c32SMatt Spinler struct stat statData; 46dd325c32SMatt Spinler auto rc = stat(file.c_str(), &statData); 47dd325c32SMatt Spinler if (rc != 0) 48dd325c32SMatt Spinler { 49dd325c32SMatt Spinler auto e = errno; 50dd325c32SMatt Spinler std::string msg = "call to stat() failed on " + file.native() + 51dd325c32SMatt Spinler " with errno " + std::to_string(e); 52dd325c32SMatt Spinler log<level::ERR>(msg.c_str()); 53dd325c32SMatt Spinler abort(); 54dd325c32SMatt Spinler } 55dd325c32SMatt Spinler 56dd325c32SMatt Spinler return statData.st_blocks * statBlockSize; 57dd325c32SMatt Spinler } 58dd325c32SMatt Spinler 598d5f3a2bSMatt Spinler Repository::Repository(const std::filesystem::path& basePath, size_t repoSize, 608d5f3a2bSMatt Spinler size_t maxNumPELs) : 618d5f3a2bSMatt Spinler _logPath(basePath / "logs"), 628d5f3a2bSMatt Spinler _maxRepoSize(repoSize), _maxNumPELs(maxNumPELs) 6389fa082aSMatt Spinler { 6489fa082aSMatt Spinler if (!fs::exists(_logPath)) 6589fa082aSMatt Spinler { 6689fa082aSMatt Spinler fs::create_directories(_logPath); 6789fa082aSMatt Spinler } 68475e574dSMatt Spinler 69475e574dSMatt Spinler restore(); 70475e574dSMatt Spinler } 71475e574dSMatt Spinler 72475e574dSMatt Spinler void Repository::restore() 73475e574dSMatt Spinler { 74475e574dSMatt Spinler for (auto& dirEntry : fs::directory_iterator(_logPath)) 75475e574dSMatt Spinler { 76475e574dSMatt Spinler try 77475e574dSMatt Spinler { 78475e574dSMatt Spinler if (!fs::is_regular_file(dirEntry.path())) 79475e574dSMatt Spinler { 80475e574dSMatt Spinler continue; 81475e574dSMatt Spinler } 82475e574dSMatt Spinler 83475e574dSMatt Spinler std::ifstream file{dirEntry.path()}; 84475e574dSMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 85475e574dSMatt Spinler std::istreambuf_iterator<char>()}; 86475e574dSMatt Spinler file.close(); 87475e574dSMatt Spinler 8807eefc54SMatt Spinler PEL pel{data}; 89475e574dSMatt Spinler if (pel.valid()) 90475e574dSMatt Spinler { 91a3c12a48SMatt Spinler // If the host hasn't acked it, reset the host state so 92a3c12a48SMatt Spinler // it will get sent up again. 93a3c12a48SMatt Spinler if (pel.hostTransmissionState() == TransmissionState::sent) 94a3c12a48SMatt Spinler { 95a3c12a48SMatt Spinler pel.setHostTransmissionState(TransmissionState::newPEL); 96a3c12a48SMatt Spinler try 97a3c12a48SMatt Spinler { 98a3c12a48SMatt Spinler write(pel, dirEntry.path()); 99a3c12a48SMatt Spinler } 100a3c12a48SMatt Spinler catch (std::exception& e) 101a3c12a48SMatt Spinler { 102a3c12a48SMatt Spinler log<level::ERR>( 103a3c12a48SMatt Spinler "Failed to save PEL after updating host state", 104a3c12a48SMatt Spinler entry("PELID=0x%X", pel.id())); 105a3c12a48SMatt Spinler } 106a3c12a48SMatt Spinler } 107a3c12a48SMatt Spinler 108dd325c32SMatt Spinler PELAttributes attributes{dirEntry.path(), 109dd325c32SMatt Spinler getFileDiskSize(dirEntry.path()), 110dd325c32SMatt Spinler pel.privateHeader().creatorID(), 111dd325c32SMatt Spinler pel.userHeader().severity(), 112dd325c32SMatt Spinler pel.userHeader().actionFlags(), 113dd325c32SMatt Spinler pel.hostTransmissionState(), 114dd325c32SMatt Spinler pel.hmcTransmissionState()}; 1150ff00485SMatt Spinler 116475e574dSMatt Spinler using pelID = LogID::Pel; 117475e574dSMatt Spinler using obmcID = LogID::Obmc; 1180ff00485SMatt Spinler _pelAttributes.emplace( 119475e574dSMatt Spinler LogID(pelID(pel.id()), obmcID(pel.obmcLogID())), 1200ff00485SMatt Spinler attributes); 121b188f78aSMatt Spinler 122b188f78aSMatt Spinler updateRepoStats(attributes, true); 123475e574dSMatt Spinler } 124475e574dSMatt Spinler else 125475e574dSMatt Spinler { 126475e574dSMatt Spinler log<level::ERR>( 127475e574dSMatt Spinler "Found invalid PEL file while restoring. Removing.", 128475e574dSMatt Spinler entry("FILENAME=%s", dirEntry.path().c_str())); 129475e574dSMatt Spinler fs::remove(dirEntry.path()); 130475e574dSMatt Spinler } 131475e574dSMatt Spinler } 132475e574dSMatt Spinler catch (std::exception& e) 133475e574dSMatt Spinler { 134475e574dSMatt Spinler log<level::ERR>("Hit exception while restoring PEL File", 135475e574dSMatt Spinler entry("FILENAME=%s", dirEntry.path().c_str()), 136475e574dSMatt Spinler entry("ERROR=%s", e.what())); 137475e574dSMatt Spinler } 138475e574dSMatt Spinler } 13989fa082aSMatt Spinler } 14089fa082aSMatt Spinler 14189fa082aSMatt Spinler std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time) 14289fa082aSMatt Spinler { 14389fa082aSMatt Spinler char name[50]; 14489fa082aSMatt Spinler sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB, 14589fa082aSMatt Spinler time.yearLSB, time.month, time.day, time.hour, time.minutes, 14689fa082aSMatt Spinler time.seconds, time.hundredths, pelID); 14789fa082aSMatt Spinler return std::string{name}; 14889fa082aSMatt Spinler } 14989fa082aSMatt Spinler 15089fa082aSMatt Spinler void Repository::add(std::unique_ptr<PEL>& pel) 15189fa082aSMatt Spinler { 152df43a305SMatt Spinler pel->setHostTransmissionState(TransmissionState::newPEL); 153df43a305SMatt Spinler pel->setHMCTransmissionState(TransmissionState::newPEL); 154df43a305SMatt Spinler 15589fa082aSMatt Spinler auto path = _logPath / getPELFilename(pel->id(), pel->commitTime()); 156ab1b97feSMatt Spinler 157ab1b97feSMatt Spinler write(*(pel.get()), path); 158ab1b97feSMatt Spinler 159dd325c32SMatt Spinler PELAttributes attributes{path, 160dd325c32SMatt Spinler getFileDiskSize(path), 161dd325c32SMatt Spinler pel->privateHeader().creatorID(), 162dd325c32SMatt Spinler pel->userHeader().severity(), 163dd325c32SMatt Spinler pel->userHeader().actionFlags(), 164346f99a1SMatt Spinler pel->hostTransmissionState(), 165346f99a1SMatt Spinler pel->hmcTransmissionState()}; 166ab1b97feSMatt Spinler 167ab1b97feSMatt Spinler using pelID = LogID::Pel; 168ab1b97feSMatt Spinler using obmcID = LogID::Obmc; 169ab1b97feSMatt Spinler _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())), 170ab1b97feSMatt Spinler attributes); 171ab1b97feSMatt Spinler 172b188f78aSMatt Spinler updateRepoStats(attributes, true); 173b188f78aSMatt Spinler 174ab1b97feSMatt Spinler processAddCallbacks(*pel); 175ab1b97feSMatt Spinler } 176ab1b97feSMatt Spinler 177ab1b97feSMatt Spinler void Repository::write(const PEL& pel, const fs::path& path) 178ab1b97feSMatt Spinler { 17989fa082aSMatt Spinler std::ofstream file{path, std::ios::binary}; 18089fa082aSMatt Spinler 18189fa082aSMatt Spinler if (!file.good()) 18289fa082aSMatt Spinler { 18389fa082aSMatt Spinler // If this fails, the filesystem is probably full so it isn't like 18489fa082aSMatt Spinler // we could successfully create yet another error log here. 18589fa082aSMatt Spinler auto e = errno; 18689fa082aSMatt Spinler fs::remove(path); 18789fa082aSMatt Spinler log<level::ERR>("Unable to open PEL file for writing", 18889fa082aSMatt Spinler entry("ERRNO=%d", e), entry("PATH=%s", path.c_str())); 18989fa082aSMatt Spinler throw file_error::Open(); 19089fa082aSMatt Spinler } 19189fa082aSMatt Spinler 192ab1b97feSMatt Spinler auto data = pel.data(); 19389fa082aSMatt Spinler file.write(reinterpret_cast<const char*>(data.data()), data.size()); 19489fa082aSMatt Spinler 19589fa082aSMatt Spinler if (file.fail()) 19689fa082aSMatt Spinler { 19789fa082aSMatt Spinler // Same note as above about not being able to create an error log 19889fa082aSMatt Spinler // for this case even if we wanted. 19989fa082aSMatt Spinler auto e = errno; 20089fa082aSMatt Spinler file.close(); 20189fa082aSMatt Spinler fs::remove(path); 20289fa082aSMatt Spinler log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e), 20389fa082aSMatt Spinler entry("PATH=%s", path.c_str())); 20489fa082aSMatt Spinler throw file_error::Write(); 20589fa082aSMatt Spinler } 206475e574dSMatt Spinler } 207475e574dSMatt Spinler 208475e574dSMatt Spinler void Repository::remove(const LogID& id) 209475e574dSMatt Spinler { 210475e574dSMatt Spinler auto pel = findPEL(id); 2110ff00485SMatt Spinler if (pel != _pelAttributes.end()) 212475e574dSMatt Spinler { 213b188f78aSMatt Spinler updateRepoStats(pel->second, false); 214b188f78aSMatt Spinler 2155f5352e5SMatt Spinler log<level::DEBUG>("Removing PEL from repository", 2165f5352e5SMatt Spinler entry("PEL_ID=0x%X", pel->first.pelID.id), 2175f5352e5SMatt Spinler entry("OBMC_LOG_ID=%d", pel->first.obmcID.id)); 2180ff00485SMatt Spinler fs::remove(pel->second.path); 2190ff00485SMatt Spinler _pelAttributes.erase(pel); 220421f6531SMatt Spinler 2215f5352e5SMatt Spinler processDeleteCallbacks(pel->first.pelID.id); 2225f5352e5SMatt Spinler } 2235f5352e5SMatt Spinler else 2245f5352e5SMatt Spinler { 2255f5352e5SMatt Spinler log<level::DEBUG>("Could not find PEL to remove", 2265f5352e5SMatt Spinler entry("PEL_ID=0x%X", id.pelID.id), 2275f5352e5SMatt Spinler entry("OBMC_LOG_ID=%d", id.obmcID.id)); 228475e574dSMatt Spinler } 22989fa082aSMatt Spinler } 23089fa082aSMatt Spinler 2312813f36dSMatt Spinler std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id) 2322813f36dSMatt Spinler { 2332813f36dSMatt Spinler auto pel = findPEL(id); 2340ff00485SMatt Spinler if (pel != _pelAttributes.end()) 2352813f36dSMatt Spinler { 2360ff00485SMatt Spinler std::ifstream file{pel->second.path.c_str()}; 2372813f36dSMatt Spinler if (!file.good()) 2382813f36dSMatt Spinler { 2392813f36dSMatt Spinler auto e = errno; 2402813f36dSMatt Spinler log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e), 2410ff00485SMatt Spinler entry("PATH=%s", pel->second.path.c_str())); 2422813f36dSMatt Spinler throw file_error::Open(); 2432813f36dSMatt Spinler } 2442813f36dSMatt Spinler 2452813f36dSMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 2462813f36dSMatt Spinler std::istreambuf_iterator<char>()}; 2472813f36dSMatt Spinler return data; 2482813f36dSMatt Spinler } 2492813f36dSMatt Spinler 2502813f36dSMatt Spinler return std::nullopt; 2512813f36dSMatt Spinler } 2522813f36dSMatt Spinler 2536d51224bSMatt Spinler std::optional<sdbusplus::message::unix_fd> Repository::getPELFD(const LogID& id) 2546d51224bSMatt Spinler { 2556d51224bSMatt Spinler auto pel = findPEL(id); 2566d51224bSMatt Spinler if (pel != _pelAttributes.end()) 2576d51224bSMatt Spinler { 2586d51224bSMatt Spinler FILE* fp = fopen(pel->second.path.c_str(), "rb"); 2596d51224bSMatt Spinler 2606d51224bSMatt Spinler if (fp == nullptr) 2616d51224bSMatt Spinler { 2626d51224bSMatt Spinler auto e = errno; 2636d51224bSMatt Spinler log<level::ERR>("Unable to open PEL File", entry("ERRNO=%d", e), 2646d51224bSMatt Spinler entry("PATH=%s", pel->second.path.c_str())); 2656d51224bSMatt Spinler throw file_error::Open(); 2666d51224bSMatt Spinler } 2676d51224bSMatt Spinler 2686d51224bSMatt Spinler // Must leave the file open here. It will be closed by sdbusplus 2696d51224bSMatt Spinler // when it sends it back over D-Bus. 2706d51224bSMatt Spinler 2716d51224bSMatt Spinler return fileno(fp); 2726d51224bSMatt Spinler } 2736d51224bSMatt Spinler return std::nullopt; 2746d51224bSMatt Spinler } 2756d51224bSMatt Spinler 2761ea78801SMatt Spinler void Repository::for_each(ForEachFunc func) const 2771ea78801SMatt Spinler { 2780ff00485SMatt Spinler for (const auto& [id, attributes] : _pelAttributes) 2791ea78801SMatt Spinler { 2800ff00485SMatt Spinler std::ifstream file{attributes.path}; 2811ea78801SMatt Spinler 2821ea78801SMatt Spinler if (!file.good()) 2831ea78801SMatt Spinler { 2841ea78801SMatt Spinler auto e = errno; 2851ea78801SMatt Spinler log<level::ERR>("Repository::for_each: Unable to open PEL file", 2861ea78801SMatt Spinler entry("ERRNO=%d", e), 2870ff00485SMatt Spinler entry("PATH=%s", attributes.path.c_str())); 2881ea78801SMatt Spinler continue; 2891ea78801SMatt Spinler } 2901ea78801SMatt Spinler 2911ea78801SMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 2921ea78801SMatt Spinler std::istreambuf_iterator<char>()}; 2931ea78801SMatt Spinler file.close(); 2941ea78801SMatt Spinler 2951ea78801SMatt Spinler PEL pel{data}; 2961ea78801SMatt Spinler 2971ea78801SMatt Spinler try 2981ea78801SMatt Spinler { 2991ea78801SMatt Spinler if (func(pel)) 3001ea78801SMatt Spinler { 3011ea78801SMatt Spinler break; 3021ea78801SMatt Spinler } 3031ea78801SMatt Spinler } 3041ea78801SMatt Spinler catch (std::exception& e) 3051ea78801SMatt Spinler { 3061ea78801SMatt Spinler log<level::ERR>("Repository::for_each function exception", 3071ea78801SMatt Spinler entry("ERROR=%s", e.what())); 3081ea78801SMatt Spinler } 3091ea78801SMatt Spinler } 3101ea78801SMatt Spinler } 3111ea78801SMatt Spinler 312421f6531SMatt Spinler void Repository::processAddCallbacks(const PEL& pel) const 313421f6531SMatt Spinler { 314421f6531SMatt Spinler for (auto& [name, func] : _addSubscriptions) 315421f6531SMatt Spinler { 316421f6531SMatt Spinler try 317421f6531SMatt Spinler { 318421f6531SMatt Spinler func(pel); 319421f6531SMatt Spinler } 320421f6531SMatt Spinler catch (std::exception& e) 321421f6531SMatt Spinler { 322421f6531SMatt Spinler log<level::ERR>("PEL Repository add callback exception", 323421f6531SMatt Spinler entry("NAME=%s", name.c_str()), 324421f6531SMatt Spinler entry("ERROR=%s", e.what())); 325421f6531SMatt Spinler } 326421f6531SMatt Spinler } 327421f6531SMatt Spinler } 328421f6531SMatt Spinler 329421f6531SMatt Spinler void Repository::processDeleteCallbacks(uint32_t id) const 330421f6531SMatt Spinler { 331421f6531SMatt Spinler for (auto& [name, func] : _deleteSubscriptions) 332421f6531SMatt Spinler { 333421f6531SMatt Spinler try 334421f6531SMatt Spinler { 335421f6531SMatt Spinler func(id); 336421f6531SMatt Spinler } 337421f6531SMatt Spinler catch (std::exception& e) 338421f6531SMatt Spinler { 339421f6531SMatt Spinler log<level::ERR>("PEL Repository delete callback exception", 340421f6531SMatt Spinler entry("NAME=%s", name.c_str()), 341421f6531SMatt Spinler entry("ERROR=%s", e.what())); 342421f6531SMatt Spinler } 343421f6531SMatt Spinler } 344421f6531SMatt Spinler } 345421f6531SMatt Spinler 3460ff00485SMatt Spinler std::optional<std::reference_wrapper<const Repository::PELAttributes>> 3470ff00485SMatt Spinler Repository::getPELAttributes(const LogID& id) const 3480ff00485SMatt Spinler { 3490ff00485SMatt Spinler auto pel = findPEL(id); 3500ff00485SMatt Spinler if (pel != _pelAttributes.end()) 3510ff00485SMatt Spinler { 3520ff00485SMatt Spinler return pel->second; 3530ff00485SMatt Spinler } 3540ff00485SMatt Spinler 3550ff00485SMatt Spinler return std::nullopt; 3560ff00485SMatt Spinler } 3570ff00485SMatt Spinler 35829d18c11SMatt Spinler void Repository::setPELHostTransState(uint32_t pelID, TransmissionState state) 35929d18c11SMatt Spinler { 36029d18c11SMatt Spinler LogID id{LogID::Pel{pelID}}; 36129d18c11SMatt Spinler auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(), 36229d18c11SMatt Spinler [&id](const auto& a) { return a.first == id; }); 36329d18c11SMatt Spinler 36429d18c11SMatt Spinler if ((attr != _pelAttributes.end()) && (attr->second.hostState != state)) 36529d18c11SMatt Spinler { 36629d18c11SMatt Spinler PELUpdateFunc func = [state](PEL& pel) { 36729d18c11SMatt Spinler pel.setHostTransmissionState(state); 36829d18c11SMatt Spinler }; 36929d18c11SMatt Spinler 37029d18c11SMatt Spinler try 37129d18c11SMatt Spinler { 37229d18c11SMatt Spinler updatePEL(attr->second.path, func); 37329d18c11SMatt Spinler 37429d18c11SMatt Spinler attr->second.hostState = state; 37529d18c11SMatt Spinler } 37629d18c11SMatt Spinler catch (std::exception& e) 37729d18c11SMatt Spinler { 37829d18c11SMatt Spinler log<level::ERR>("Unable to update PEL host transmission state", 37929d18c11SMatt Spinler entry("PATH=%s", attr->second.path.c_str()), 38029d18c11SMatt Spinler entry("ERROR=%s", e.what())); 38129d18c11SMatt Spinler } 38229d18c11SMatt Spinler } 38329d18c11SMatt Spinler } 38429d18c11SMatt Spinler 38529d18c11SMatt Spinler void Repository::setPELHMCTransState(uint32_t pelID, TransmissionState state) 38629d18c11SMatt Spinler { 38729d18c11SMatt Spinler LogID id{LogID::Pel{pelID}}; 38829d18c11SMatt Spinler auto attr = std::find_if(_pelAttributes.begin(), _pelAttributes.end(), 38929d18c11SMatt Spinler [&id](const auto& a) { return a.first == id; }); 39029d18c11SMatt Spinler 39129d18c11SMatt Spinler if ((attr != _pelAttributes.end()) && (attr->second.hmcState != state)) 39229d18c11SMatt Spinler { 39329d18c11SMatt Spinler PELUpdateFunc func = [state](PEL& pel) { 39429d18c11SMatt Spinler pel.setHMCTransmissionState(state); 39529d18c11SMatt Spinler }; 39629d18c11SMatt Spinler 39729d18c11SMatt Spinler try 39829d18c11SMatt Spinler { 39929d18c11SMatt Spinler updatePEL(attr->second.path, func); 40029d18c11SMatt Spinler 40129d18c11SMatt Spinler attr->second.hmcState = state; 40229d18c11SMatt Spinler } 40329d18c11SMatt Spinler catch (std::exception& e) 40429d18c11SMatt Spinler { 40529d18c11SMatt Spinler log<level::ERR>("Unable to update PEL HMC transmission state", 40629d18c11SMatt Spinler entry("PATH=%s", attr->second.path.c_str()), 40729d18c11SMatt Spinler entry("ERROR=%s", e.what())); 40829d18c11SMatt Spinler } 40929d18c11SMatt Spinler } 41029d18c11SMatt Spinler } 41129d18c11SMatt Spinler 41229d18c11SMatt Spinler void Repository::updatePEL(const fs::path& path, PELUpdateFunc updateFunc) 41329d18c11SMatt Spinler { 41429d18c11SMatt Spinler std::ifstream file{path}; 41529d18c11SMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 41629d18c11SMatt Spinler std::istreambuf_iterator<char>()}; 41729d18c11SMatt Spinler file.close(); 41829d18c11SMatt Spinler 41929d18c11SMatt Spinler PEL pel{data}; 42029d18c11SMatt Spinler 42129d18c11SMatt Spinler if (pel.valid()) 42229d18c11SMatt Spinler { 42329d18c11SMatt Spinler updateFunc(pel); 42429d18c11SMatt Spinler 42529d18c11SMatt Spinler write(pel, path); 42629d18c11SMatt Spinler } 42729d18c11SMatt Spinler else 42829d18c11SMatt Spinler { 42929d18c11SMatt Spinler throw std::runtime_error( 43029d18c11SMatt Spinler "Unable to read a valid PEL when trying to update it"); 43129d18c11SMatt Spinler } 43229d18c11SMatt Spinler } 43329d18c11SMatt Spinler 434b188f78aSMatt Spinler bool Repository::isServiceableSev(const PELAttributes& pel) 435b188f78aSMatt Spinler { 436b188f78aSMatt Spinler auto sevType = static_cast<SeverityType>(pel.severity & 0xF0); 437b188f78aSMatt Spinler auto sevPVEntry = 438b188f78aSMatt Spinler pel_values::findByValue(pel.severity, pel_values::severityValues); 439b188f78aSMatt Spinler std::string sevName = std::get<pel_values::registryNamePos>(*sevPVEntry); 440b188f78aSMatt Spinler 441b188f78aSMatt Spinler bool check1 = (sevType == SeverityType::predictive) || 442b188f78aSMatt Spinler (sevType == SeverityType::unrecoverable) || 443b188f78aSMatt Spinler (sevType == SeverityType::critical); 444b188f78aSMatt Spinler 445b188f78aSMatt Spinler bool check2 = ((sevType == SeverityType::recovered) || 446b188f78aSMatt Spinler (sevName == "symptom_recovered")) && 447b188f78aSMatt Spinler !pel.actionFlags.test(hiddenFlagBit); 448b188f78aSMatt Spinler 449b188f78aSMatt Spinler bool check3 = (sevName == "symptom_predictive") || 450b188f78aSMatt Spinler (sevName == "symptom_unrecoverable") || 451b188f78aSMatt Spinler (sevName == "symptom_critical"); 452b188f78aSMatt Spinler 453b188f78aSMatt Spinler return check1 || check2 || check3; 454b188f78aSMatt Spinler } 455b188f78aSMatt Spinler 456b188f78aSMatt Spinler void Repository::updateRepoStats(const PELAttributes& pel, bool pelAdded) 457b188f78aSMatt Spinler { 458b188f78aSMatt Spinler auto isServiceable = Repository::isServiceableSev(pel); 459b188f78aSMatt Spinler auto bmcPEL = CreatorID::openBMC == static_cast<CreatorID>(pel.creator); 460b188f78aSMatt Spinler 461b188f78aSMatt Spinler auto adjustSize = [pelAdded, &pel](auto& runningSize) { 462b188f78aSMatt Spinler if (pelAdded) 463b188f78aSMatt Spinler { 464b188f78aSMatt Spinler runningSize += pel.sizeOnDisk; 465b188f78aSMatt Spinler } 466b188f78aSMatt Spinler else 467b188f78aSMatt Spinler { 468b188f78aSMatt Spinler runningSize = std::max(static_cast<int64_t>(runningSize) - 469b188f78aSMatt Spinler static_cast<int64_t>(pel.sizeOnDisk), 470b188f78aSMatt Spinler static_cast<int64_t>(0)); 471b188f78aSMatt Spinler } 472b188f78aSMatt Spinler }; 473b188f78aSMatt Spinler 474b188f78aSMatt Spinler adjustSize(_sizes.total); 475b188f78aSMatt Spinler 476b188f78aSMatt Spinler if (bmcPEL) 477b188f78aSMatt Spinler { 478b188f78aSMatt Spinler adjustSize(_sizes.bmc); 479b188f78aSMatt Spinler if (isServiceable) 480b188f78aSMatt Spinler { 481b188f78aSMatt Spinler adjustSize(_sizes.bmcServiceable); 482b188f78aSMatt Spinler } 483b188f78aSMatt Spinler else 484b188f78aSMatt Spinler { 485b188f78aSMatt Spinler adjustSize(_sizes.bmcInfo); 486b188f78aSMatt Spinler } 487b188f78aSMatt Spinler } 488b188f78aSMatt Spinler else 489b188f78aSMatt Spinler { 490b188f78aSMatt Spinler adjustSize(_sizes.nonBMC); 491b188f78aSMatt Spinler if (isServiceable) 492b188f78aSMatt Spinler { 493b188f78aSMatt Spinler adjustSize(_sizes.nonBMCServiceable); 494b188f78aSMatt Spinler } 495b188f78aSMatt Spinler else 496b188f78aSMatt Spinler { 497b188f78aSMatt Spinler adjustSize(_sizes.nonBMCInfo); 498b188f78aSMatt Spinler } 499b188f78aSMatt Spinler } 500b188f78aSMatt Spinler } 501b188f78aSMatt Spinler 502*b0a8df5bSMatt Spinler std::vector<Repository::AttributesReference> 503*b0a8df5bSMatt Spinler Repository::getAllPELAttributes(SortOrder order) const 504*b0a8df5bSMatt Spinler { 505*b0a8df5bSMatt Spinler std::vector<Repository::AttributesReference> attributes; 506*b0a8df5bSMatt Spinler 507*b0a8df5bSMatt Spinler std::for_each( 508*b0a8df5bSMatt Spinler _pelAttributes.begin(), _pelAttributes.end(), 509*b0a8df5bSMatt Spinler [&attributes](auto& pelEntry) { attributes.push_back(pelEntry); }); 510*b0a8df5bSMatt Spinler 511*b0a8df5bSMatt Spinler std::sort(attributes.begin(), attributes.end(), 512*b0a8df5bSMatt Spinler [order](const auto& left, const auto& right) { 513*b0a8df5bSMatt Spinler if (order == SortOrder::ascending) 514*b0a8df5bSMatt Spinler { 515*b0a8df5bSMatt Spinler return left.get().second.path < right.get().second.path; 516*b0a8df5bSMatt Spinler } 517*b0a8df5bSMatt Spinler return left.get().second.path > right.get().second.path; 518*b0a8df5bSMatt Spinler }); 519*b0a8df5bSMatt Spinler 520*b0a8df5bSMatt Spinler return attributes; 521*b0a8df5bSMatt Spinler } 522*b0a8df5bSMatt Spinler 523*b0a8df5bSMatt Spinler std::vector<uint32_t> Repository::prune() 524*b0a8df5bSMatt Spinler { 525*b0a8df5bSMatt Spinler std::vector<uint32_t> obmcLogIDs; 526*b0a8df5bSMatt Spinler std::string msg = "Pruning PEL repository that takes up " + 527*b0a8df5bSMatt Spinler std::to_string(_sizes.total) + " bytes and has " + 528*b0a8df5bSMatt Spinler std::to_string(_pelAttributes.size()) + " PELs"; 529*b0a8df5bSMatt Spinler log<level::INFO>(msg.c_str()); 530*b0a8df5bSMatt Spinler 531*b0a8df5bSMatt Spinler // Set up the 5 functions to check if the PEL category 532*b0a8df5bSMatt Spinler // is still over its limits. 533*b0a8df5bSMatt Spinler 534*b0a8df5bSMatt Spinler // BMC informational PELs should only take up 15% 535*b0a8df5bSMatt Spinler IsOverLimitFunc overBMCInfoLimit = [this]() { 536*b0a8df5bSMatt Spinler return _sizes.bmcInfo > _maxRepoSize * 15 / 100; 537*b0a8df5bSMatt Spinler }; 538*b0a8df5bSMatt Spinler 539*b0a8df5bSMatt Spinler // BMC non informational PELs should only take up 30% 540*b0a8df5bSMatt Spinler IsOverLimitFunc overBMCNonInfoLimit = [this]() { 541*b0a8df5bSMatt Spinler return _sizes.bmcServiceable > _maxRepoSize * 30 / 100; 542*b0a8df5bSMatt Spinler }; 543*b0a8df5bSMatt Spinler 544*b0a8df5bSMatt Spinler // Non BMC informational PELs should only take up 15% 545*b0a8df5bSMatt Spinler IsOverLimitFunc overNonBMCInfoLimit = [this]() { 546*b0a8df5bSMatt Spinler return _sizes.nonBMCInfo > _maxRepoSize * 15 / 100; 547*b0a8df5bSMatt Spinler }; 548*b0a8df5bSMatt Spinler 549*b0a8df5bSMatt Spinler // Non BMC non informational PELs should only take up 15% 550*b0a8df5bSMatt Spinler IsOverLimitFunc overNonBMCNonInfoLimit = [this]() { 551*b0a8df5bSMatt Spinler return _sizes.nonBMCServiceable > _maxRepoSize * 30 / 100; 552*b0a8df5bSMatt Spinler }; 553*b0a8df5bSMatt Spinler 554*b0a8df5bSMatt Spinler // Bring the total number of PELs down to 80% of the max 555*b0a8df5bSMatt Spinler IsOverLimitFunc tooManyPELsLimit = [this]() { 556*b0a8df5bSMatt Spinler return _pelAttributes.size() > _maxNumPELs * 80 / 100; 557*b0a8df5bSMatt Spinler }; 558*b0a8df5bSMatt Spinler 559*b0a8df5bSMatt Spinler // Set up the functions to determine which category a PEL is in. 560*b0a8df5bSMatt Spinler // TODO: Return false in these functions if a PEL caused a guard record. 561*b0a8df5bSMatt Spinler 562*b0a8df5bSMatt Spinler // A BMC informational PEL 563*b0a8df5bSMatt Spinler IsPELTypeFunc isBMCInfo = [](const PELAttributes& pel) { 564*b0a8df5bSMatt Spinler return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) && 565*b0a8df5bSMatt Spinler !Repository::isServiceableSev(pel); 566*b0a8df5bSMatt Spinler }; 567*b0a8df5bSMatt Spinler 568*b0a8df5bSMatt Spinler // A BMC non informational PEL 569*b0a8df5bSMatt Spinler IsPELTypeFunc isBMCNonInfo = [](const PELAttributes& pel) { 570*b0a8df5bSMatt Spinler return (CreatorID::openBMC == static_cast<CreatorID>(pel.creator)) && 571*b0a8df5bSMatt Spinler Repository::isServiceableSev(pel); 572*b0a8df5bSMatt Spinler }; 573*b0a8df5bSMatt Spinler 574*b0a8df5bSMatt Spinler // A non BMC informational PEL 575*b0a8df5bSMatt Spinler IsPELTypeFunc isNonBMCInfo = [](const PELAttributes& pel) { 576*b0a8df5bSMatt Spinler return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) && 577*b0a8df5bSMatt Spinler !Repository::isServiceableSev(pel); 578*b0a8df5bSMatt Spinler }; 579*b0a8df5bSMatt Spinler 580*b0a8df5bSMatt Spinler // A non BMC non informational PEL 581*b0a8df5bSMatt Spinler IsPELTypeFunc isNonBMCNonInfo = [](const PELAttributes& pel) { 582*b0a8df5bSMatt Spinler return (CreatorID::openBMC != static_cast<CreatorID>(pel.creator)) && 583*b0a8df5bSMatt Spinler Repository::isServiceableSev(pel); 584*b0a8df5bSMatt Spinler }; 585*b0a8df5bSMatt Spinler 586*b0a8df5bSMatt Spinler // When counting PELs, count every PEL 587*b0a8df5bSMatt Spinler IsPELTypeFunc isAnyPEL = [](const PELAttributes& pel) { return true; }; 588*b0a8df5bSMatt Spinler 589*b0a8df5bSMatt Spinler // Check all 4 categories, which will result in at most 90% 590*b0a8df5bSMatt Spinler // usage (15 + 30 + 15 + 30). 591*b0a8df5bSMatt Spinler removePELs(overBMCInfoLimit, isBMCInfo, obmcLogIDs); 592*b0a8df5bSMatt Spinler removePELs(overBMCNonInfoLimit, isBMCNonInfo, obmcLogIDs); 593*b0a8df5bSMatt Spinler removePELs(overNonBMCInfoLimit, isNonBMCInfo, obmcLogIDs); 594*b0a8df5bSMatt Spinler removePELs(overNonBMCNonInfoLimit, isNonBMCNonInfo, obmcLogIDs); 595*b0a8df5bSMatt Spinler 596*b0a8df5bSMatt Spinler // After the above pruning check if there are still too many PELs, 597*b0a8df5bSMatt Spinler // which can happen depending on PEL sizes. 598*b0a8df5bSMatt Spinler if (_pelAttributes.size() > _maxNumPELs) 599*b0a8df5bSMatt Spinler { 600*b0a8df5bSMatt Spinler removePELs(tooManyPELsLimit, isAnyPEL, obmcLogIDs); 601*b0a8df5bSMatt Spinler } 602*b0a8df5bSMatt Spinler 603*b0a8df5bSMatt Spinler if (!obmcLogIDs.empty()) 604*b0a8df5bSMatt Spinler { 605*b0a8df5bSMatt Spinler std::string msg = "Number of PELs removed to save space: " + 606*b0a8df5bSMatt Spinler std::to_string(obmcLogIDs.size()); 607*b0a8df5bSMatt Spinler log<level::INFO>(msg.c_str()); 608*b0a8df5bSMatt Spinler } 609*b0a8df5bSMatt Spinler 610*b0a8df5bSMatt Spinler return obmcLogIDs; 611*b0a8df5bSMatt Spinler } 612*b0a8df5bSMatt Spinler 613*b0a8df5bSMatt Spinler void Repository::removePELs(IsOverLimitFunc& isOverLimit, 614*b0a8df5bSMatt Spinler IsPELTypeFunc& isPELType, 615*b0a8df5bSMatt Spinler std::vector<uint32_t>& removedBMCLogIDs) 616*b0a8df5bSMatt Spinler { 617*b0a8df5bSMatt Spinler if (!isOverLimit()) 618*b0a8df5bSMatt Spinler { 619*b0a8df5bSMatt Spinler return; 620*b0a8df5bSMatt Spinler } 621*b0a8df5bSMatt Spinler 622*b0a8df5bSMatt Spinler auto attributes = getAllPELAttributes(SortOrder::ascending); 623*b0a8df5bSMatt Spinler 624*b0a8df5bSMatt Spinler // Make 4 passes on the PELs, stopping as soon as isOverLimit 625*b0a8df5bSMatt Spinler // returns false. 626*b0a8df5bSMatt Spinler // Pass 1: only delete HMC acked PELs 627*b0a8df5bSMatt Spinler // Pass 2: only delete OS acked PELs 628*b0a8df5bSMatt Spinler // Pass 3: only delete PHYP sent PELs 629*b0a8df5bSMatt Spinler // Pass 4: delete all PELs 630*b0a8df5bSMatt Spinler static const std::vector<std::function<bool(const PELAttributes& pel)>> 631*b0a8df5bSMatt Spinler stateChecks{[](const auto& pel) { 632*b0a8df5bSMatt Spinler return pel.hmcState == TransmissionState::acked; 633*b0a8df5bSMatt Spinler }, 634*b0a8df5bSMatt Spinler 635*b0a8df5bSMatt Spinler [](const auto& pel) { 636*b0a8df5bSMatt Spinler return pel.hostState == TransmissionState::acked; 637*b0a8df5bSMatt Spinler }, 638*b0a8df5bSMatt Spinler 639*b0a8df5bSMatt Spinler [](const auto& pel) { 640*b0a8df5bSMatt Spinler return pel.hostState == TransmissionState::sent; 641*b0a8df5bSMatt Spinler }, 642*b0a8df5bSMatt Spinler 643*b0a8df5bSMatt Spinler [](const auto& pel) { return true; }}; 644*b0a8df5bSMatt Spinler 645*b0a8df5bSMatt Spinler for (const auto& stateCheck : stateChecks) 646*b0a8df5bSMatt Spinler { 647*b0a8df5bSMatt Spinler for (auto it = attributes.begin(); it != attributes.end();) 648*b0a8df5bSMatt Spinler { 649*b0a8df5bSMatt Spinler const auto& pel = it->get(); 650*b0a8df5bSMatt Spinler if (isPELType(pel.second) && stateCheck(pel.second)) 651*b0a8df5bSMatt Spinler { 652*b0a8df5bSMatt Spinler auto removedID = pel.first.obmcID.id; 653*b0a8df5bSMatt Spinler remove(pel.first); 654*b0a8df5bSMatt Spinler 655*b0a8df5bSMatt Spinler removedBMCLogIDs.push_back(removedID); 656*b0a8df5bSMatt Spinler 657*b0a8df5bSMatt Spinler attributes.erase(it); 658*b0a8df5bSMatt Spinler 659*b0a8df5bSMatt Spinler if (!isOverLimit()) 660*b0a8df5bSMatt Spinler { 661*b0a8df5bSMatt Spinler break; 662*b0a8df5bSMatt Spinler } 663*b0a8df5bSMatt Spinler } 664*b0a8df5bSMatt Spinler else 665*b0a8df5bSMatt Spinler { 666*b0a8df5bSMatt Spinler ++it; 667*b0a8df5bSMatt Spinler } 668*b0a8df5bSMatt Spinler } 669*b0a8df5bSMatt Spinler 670*b0a8df5bSMatt Spinler if (!isOverLimit()) 671*b0a8df5bSMatt Spinler { 672*b0a8df5bSMatt Spinler break; 673*b0a8df5bSMatt Spinler } 674*b0a8df5bSMatt Spinler } 675*b0a8df5bSMatt Spinler } 676*b0a8df5bSMatt Spinler 67789fa082aSMatt Spinler } // namespace pels 67889fa082aSMatt Spinler } // namespace openpower 679