Matt Spinler | 711d51d | 2019-11-06 09:36:51 -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 | */ |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 16 | #include "manager.hpp" |
| 17 | |
| 18 | #include "additional_data.hpp" |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 19 | #include "json_utils.hpp" |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 20 | #include "pel.hpp" |
| 21 | |
Matt Spinler | 6b1a5c8 | 2020-01-07 08:48:53 -0600 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 24 | #include <filesystem> |
| 25 | #include <fstream> |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 26 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 27 | |
| 28 | namespace openpower |
| 29 | { |
| 30 | namespace pels |
| 31 | { |
| 32 | |
| 33 | using namespace phosphor::logging; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 34 | namespace fs = std::filesystem; |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 35 | namespace rg = openpower::pels::message; |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 36 | |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 37 | namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error; |
| 38 | |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 39 | namespace additional_data |
| 40 | { |
| 41 | constexpr auto rawPEL = "RAWPEL"; |
Matt Spinler | 19e7290 | 2020-01-24 11:05:20 -0600 | [diff] [blame] | 42 | constexpr auto esel = "ESEL"; |
| 43 | } // namespace additional_data |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 44 | |
| 45 | void Manager::create(const std::string& message, uint32_t obmcLogID, |
| 46 | uint64_t timestamp, Entry::Level severity, |
| 47 | const std::vector<std::string>& additionalData, |
| 48 | const std::vector<std::string>& associations) |
| 49 | { |
| 50 | AdditionalData ad{additionalData}; |
| 51 | |
Matt Spinler | 19e7290 | 2020-01-24 11:05:20 -0600 | [diff] [blame] | 52 | // If a PEL was passed in via a filename or in an ESEL, |
| 53 | // use that. Otherwise, create one. |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 54 | auto rawPelPath = ad.getValue(additional_data::rawPEL); |
| 55 | if (rawPelPath) |
| 56 | { |
| 57 | addRawPEL(*rawPelPath, obmcLogID); |
| 58 | } |
| 59 | else |
| 60 | { |
Matt Spinler | 19e7290 | 2020-01-24 11:05:20 -0600 | [diff] [blame] | 61 | auto esel = ad.getValue(additional_data::esel); |
| 62 | if (esel) |
| 63 | { |
| 64 | addESELPEL(*esel, obmcLogID); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | createPEL(message, obmcLogID, timestamp, severity, additionalData, |
| 69 | associations); |
| 70 | } |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID) |
| 75 | { |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 76 | if (fs::exists(rawPelPath)) |
| 77 | { |
| 78 | std::ifstream file(rawPelPath, std::ios::in | std::ios::binary); |
| 79 | |
| 80 | auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file), |
| 81 | std::istreambuf_iterator<char>()); |
| 82 | if (file.fail()) |
| 83 | { |
| 84 | log<level::ERR>("Filesystem error reading a raw PEL", |
| 85 | entry("PELFILE=%s", rawPelPath.c_str()), |
| 86 | entry("OBMCLOGID=%d", obmcLogID)); |
| 87 | // TODO, Decide what to do here. Maybe nothing. |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | file.close(); |
| 92 | |
Matt Spinler | 19e7290 | 2020-01-24 11:05:20 -0600 | [diff] [blame] | 93 | addPEL(data, obmcLogID); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 94 | } |
| 95 | else |
| 96 | { |
| 97 | log<level::ERR>("Raw PEL file from BMC event log does not exist", |
| 98 | entry("PELFILE=%s", (rawPelPath).c_str()), |
| 99 | entry("OBMCLOGID=%d", obmcLogID)); |
| 100 | } |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Matt Spinler | 19e7290 | 2020-01-24 11:05:20 -0600 | [diff] [blame] | 103 | void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID) |
| 104 | { |
| 105 | |
| 106 | auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID); |
| 107 | if (pel->valid()) |
| 108 | { |
| 109 | // PELs created by others still need these fields set by us. |
| 110 | pel->assignID(); |
| 111 | pel->setCommitTime(); |
| 112 | |
| 113 | try |
| 114 | { |
| 115 | _repo.add(pel); |
| 116 | } |
| 117 | catch (std::exception& e) |
| 118 | { |
| 119 | // Probably a full or r/o filesystem, not much we can do. |
| 120 | log<level::ERR>("Unable to add PEL to Repository", |
| 121 | entry("PEL_ID=0x%X", pel->id())); |
| 122 | } |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | log<level::ERR>("Invalid PEL received from the host", |
| 127 | entry("OBMCLOGID=%d", obmcLogID)); |
| 128 | |
| 129 | AdditionalData ad; |
| 130 | ad.add("PLID", getNumberString("0x%08X", pel->plid())); |
| 131 | ad.add("OBMC_LOG_ID", std::to_string(obmcLogID)); |
| 132 | ad.add("PEL_SIZE", std::to_string(pelData.size())); |
| 133 | |
| 134 | std::string asciiString; |
| 135 | auto src = pel->primarySRC(); |
| 136 | if (src) |
| 137 | { |
| 138 | asciiString = (*src)->asciiString(); |
| 139 | } |
| 140 | |
| 141 | ad.add("SRC", asciiString); |
| 142 | |
| 143 | _eventLogger.log("org.open_power.Logging.Error.BadHostPEL", |
| 144 | Entry::Level::Error, ad); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID) |
| 149 | { |
| 150 | std::vector<uint8_t> data; |
| 151 | |
| 152 | try |
| 153 | { |
| 154 | data = std::move(eselToRawData(esel)); |
| 155 | } |
| 156 | catch (std::exception& e) |
| 157 | { |
| 158 | // Try to add it below anyway, so it follows the usual bad data path. |
| 159 | log<level::ERR>("Problems converting ESEL string to a byte vector"); |
| 160 | } |
| 161 | |
| 162 | addPEL(data, obmcLogID); |
| 163 | } |
| 164 | |
| 165 | std::vector<uint8_t> Manager::eselToRawData(const std::string& esel) |
| 166 | { |
| 167 | std::vector<uint8_t> data; |
| 168 | std::string byteString; |
| 169 | |
| 170 | // As the eSEL string looks like: "50 48 00 ab ..." there are 3 |
| 171 | // characters per raw byte, and since the actual PEL data starts |
| 172 | // at the 16th byte, the code will grab the PEL data starting at |
| 173 | // offset 48 in the string. |
| 174 | static constexpr size_t pelStart = 16 * 3; |
| 175 | |
| 176 | if (esel.size() <= pelStart) |
| 177 | { |
| 178 | log<level::ERR>("ESEL data too short", |
| 179 | entry("ESEL_SIZE=%d", esel.size())); |
| 180 | |
| 181 | throw std::length_error("ESEL data too short"); |
| 182 | } |
| 183 | |
| 184 | for (size_t i = pelStart; i < esel.size(); i += 3) |
| 185 | { |
| 186 | if (i + 1 < esel.size()) |
| 187 | { |
| 188 | byteString = esel.substr(i, 2); |
| 189 | data.push_back(std::stoi(byteString, nullptr, 16)); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | log<level::ERR>("ESEL data too short", |
| 194 | entry("ESEL_SIZE=%d", esel.size())); |
| 195 | throw std::length_error("ESEL data too short"); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return data; |
| 200 | } |
| 201 | |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 202 | void Manager::erase(uint32_t obmcLogID) |
| 203 | { |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 204 | Repository::LogID id{Repository::LogID::Obmc(obmcLogID)}; |
| 205 | |
| 206 | _repo.remove(id); |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bool Manager::isDeleteProhibited(uint32_t obmcLogID) |
| 210 | { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | void Manager::createPEL(const std::string& message, uint32_t obmcLogID, |
| 215 | uint64_t timestamp, |
| 216 | phosphor::logging::Entry::Level severity, |
| 217 | const std::vector<std::string>& additionalData, |
| 218 | const std::vector<std::string>& associations) |
| 219 | { |
Harisuddin Mohamed Isa | 0f717e1 | 2020-01-15 20:05:33 +0800 | [diff] [blame] | 220 | auto entry = _registry.lookup(message, rg::LookupType::name); |
Matt Spinler | 1d4c74a | 2019-12-16 14:40:21 -0600 | [diff] [blame] | 221 | std::string msg; |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 222 | |
| 223 | if (entry) |
| 224 | { |
| 225 | AdditionalData ad{additionalData}; |
| 226 | |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 227 | auto pel = std::make_unique<openpower::pels::PEL>( |
| 228 | *entry, obmcLogID, timestamp, severity, ad, *_dataIface); |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 229 | |
| 230 | _repo.add(pel); |
Matt Spinler | 67456c2 | 2019-10-21 12:22:49 -0500 | [diff] [blame] | 231 | |
Matt Spinler | 1d4c74a | 2019-12-16 14:40:21 -0600 | [diff] [blame] | 232 | auto src = pel->primarySRC(); |
| 233 | if (src) |
| 234 | { |
| 235 | using namespace std::literals::string_literals; |
Matt Spinler | 19e7290 | 2020-01-24 11:05:20 -0600 | [diff] [blame] | 236 | auto id = getNumberString("0x%08X", pel->id()); |
Matt Spinler | 1d4c74a | 2019-12-16 14:40:21 -0600 | [diff] [blame] | 237 | msg = "Created PEL "s + id + " with SRC "s + (*src)->asciiString(); |
| 238 | while (msg.back() == ' ') |
| 239 | { |
| 240 | msg.pop_back(); |
| 241 | } |
| 242 | log<level::INFO>(msg.c_str()); |
| 243 | } |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | // TODO ibm-openbmc/dev/1151: Create a new PEL for this case. |
| 248 | // For now, just trace it. |
| 249 | msg = "Event not found in PEL message registry: " + message; |
| 250 | log<level::INFO>(msg.c_str()); |
| 251 | } |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 254 | sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID) |
| 255 | { |
| 256 | Repository::LogID id{Repository::LogID::Pel(pelID)}; |
| 257 | std::optional<int> fd; |
| 258 | |
| 259 | try |
| 260 | { |
| 261 | fd = _repo.getPELFD(id); |
| 262 | } |
| 263 | catch (std::exception& e) |
| 264 | { |
| 265 | throw common_error::InternalFailure(); |
| 266 | } |
| 267 | |
| 268 | if (!fd) |
| 269 | { |
| 270 | throw common_error::InvalidArgument(); |
| 271 | } |
| 272 | |
Matt Spinler | 6b1a5c8 | 2020-01-07 08:48:53 -0600 | [diff] [blame] | 273 | scheduleFDClose(*fd); |
| 274 | |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 275 | return *fd; |
| 276 | } |
| 277 | |
Matt Spinler | 6b1a5c8 | 2020-01-07 08:48:53 -0600 | [diff] [blame] | 278 | void Manager::scheduleFDClose(int fd) |
| 279 | { |
| 280 | _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>( |
| 281 | _logManager.getBus().get_event(), |
| 282 | std::bind(std::mem_fn(&Manager::closeFD), this, fd, |
| 283 | std::placeholders::_1)); |
| 284 | } |
| 285 | |
| 286 | void Manager::closeFD(int fd, sdeventplus::source::EventBase& source) |
| 287 | { |
| 288 | close(fd); |
| 289 | _fdCloserEventSource.reset(); |
| 290 | } |
| 291 | |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 292 | std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID) |
| 293 | { |
| 294 | Repository::LogID id{Repository::LogID::Obmc(obmcLogID)}; |
| 295 | std::optional<std::vector<uint8_t>> data; |
| 296 | |
| 297 | try |
| 298 | { |
| 299 | data = _repo.getPELData(id); |
| 300 | } |
| 301 | catch (std::exception& e) |
| 302 | { |
| 303 | throw common_error::InternalFailure(); |
| 304 | } |
| 305 | |
| 306 | if (!data) |
| 307 | { |
| 308 | throw common_error::InvalidArgument(); |
| 309 | } |
| 310 | |
| 311 | return *data; |
| 312 | } |
| 313 | |
| 314 | void Manager::hostAck(uint32_t pelID) |
| 315 | { |
| 316 | Repository::LogID id{Repository::LogID::Pel(pelID)}; |
| 317 | |
| 318 | if (!_repo.hasPEL(id)) |
| 319 | { |
| 320 | throw common_error::InvalidArgument(); |
| 321 | } |
| 322 | |
| 323 | if (_hostNotifier) |
| 324 | { |
| 325 | _hostNotifier->ackPEL(pelID); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void Manager::hostReject(uint32_t pelID, RejectionReason reason) |
| 330 | { |
| 331 | Repository::LogID id{Repository::LogID::Pel(pelID)}; |
| 332 | |
| 333 | if (!_repo.hasPEL(id)) |
| 334 | { |
| 335 | throw common_error::InvalidArgument(); |
| 336 | } |
| 337 | |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 338 | if (reason == RejectionReason::BadPEL) |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 339 | { |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 340 | AdditionalData data; |
| 341 | data.add("BAD_ID", getNumberString("0x%08X", pelID)); |
| 342 | _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost", |
| 343 | Entry::Level::Informational, data); |
| 344 | if (_hostNotifier) |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 345 | { |
| 346 | _hostNotifier->setBadPEL(pelID); |
| 347 | } |
Matt Spinler | 05c2c6c | 2019-12-18 14:02:09 -0600 | [diff] [blame] | 348 | } |
| 349 | else if ((reason == RejectionReason::HostFull) && _hostNotifier) |
| 350 | { |
| 351 | _hostNotifier->setHostFull(pelID); |
Matt Spinler | a34ab72 | 2019-12-16 10:39:32 -0600 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Matt Spinler | 4e8078c | 2019-07-09 13:22:32 -0500 | [diff] [blame] | 355 | } // namespace pels |
| 356 | } // namespace openpower |