blob: 9f2a13e45dbaf08802e3ccf1c0dc54e1a1924b3b [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 Spinler4e8078c2019-07-09 13:22:32 -050016#include "manager.hpp"
17
18#include "additional_data.hpp"
Matt Spinler05c2c6c2019-12-18 14:02:09 -060019#include "json_utils.hpp"
Matt Spinler89fa0822019-07-17 13:54:30 -050020#include "pel.hpp"
Vijay Lobo2fb10212021-08-22 23:24:16 -050021#include "pel_entry.hpp"
Matt Spinler1962e082020-08-05 13:44:53 -050022#include "service_indicators.hpp"
Matt Spinler8b81ec02022-07-12 13:25:37 -050023#include "severity.hpp"
Matt Spinler89fa0822019-07-17 13:54:30 -050024
Matt Spinler22421b92020-07-17 09:41:08 -050025#include <fmt/format.h>
Matt Spinlerff9cec22020-07-15 13:06:35 -050026#include <sys/inotify.h>
Matt Spinler6b1a5c82020-01-07 08:48:53 -060027#include <unistd.h>
28
Patrick Williams2544b412022-10-04 08:41:06 -050029#include <xyz/openbmc_project/Common/error.hpp>
30#include <xyz/openbmc_project/Logging/Create/server.hpp>
31
Matt Spinler89fa0822019-07-17 13:54:30 -050032#include <filesystem>
33#include <fstream>
Matt Spinler0003af12022-06-08 10:46:17 -050034#include <locale>
Matt Spinler4e8078c2019-07-09 13:22:32 -050035
36namespace openpower
37{
38namespace pels
39{
40
41using namespace phosphor::logging;
Matt Spinler89fa0822019-07-17 13:54:30 -050042namespace fs = std::filesystem;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080043namespace rg = openpower::pels::message;
Matt Spinler4e8078c2019-07-09 13:22:32 -050044
Matt Spinlera34ab722019-12-16 10:39:32 -060045namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
46
Matt Spinler56ad2a02020-03-26 14:00:52 -050047using Create = sdbusplus::xyz::openbmc_project::Logging::server::Create;
48
Matt Spinler4e8078c2019-07-09 13:22:32 -050049namespace additional_data
50{
51constexpr auto rawPEL = "RAWPEL";
Matt Spinler19e72902020-01-24 11:05:20 -060052constexpr auto esel = "ESEL";
Matt Spinler30ddc9f2020-07-16 15:39:59 -050053constexpr auto error = "ERROR_NAME";
Matt Spinler19e72902020-01-24 11:05:20 -060054} // namespace additional_data
Matt Spinler4e8078c2019-07-09 13:22:32 -050055
Matt Spinler30ddc9f2020-07-16 15:39:59 -050056constexpr auto defaultLogMessage = "xyz.openbmc_project.Logging.Error.Default";
57
Matt Spinlerff9cec22020-07-15 13:06:35 -050058Manager::~Manager()
59{
60 if (_pelFileDeleteFD != -1)
61 {
62 if (_pelFileDeleteWatchFD != -1)
63 {
64 inotify_rm_watch(_pelFileDeleteFD, _pelFileDeleteWatchFD);
65 }
66 close(_pelFileDeleteFD);
67 }
68}
69
Matt Spinler4e8078c2019-07-09 13:22:32 -050070void Manager::create(const std::string& message, uint32_t obmcLogID,
71 uint64_t timestamp, Entry::Level severity,
72 const std::vector<std::string>& additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050073 const std::vector<std::string>& associations,
74 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -050075{
76 AdditionalData ad{additionalData};
77
Matt Spinler19e72902020-01-24 11:05:20 -060078 // If a PEL was passed in via a filename or in an ESEL,
79 // use that. Otherwise, create one.
Matt Spinler4e8078c2019-07-09 13:22:32 -050080 auto rawPelPath = ad.getValue(additional_data::rawPEL);
81 if (rawPelPath)
82 {
83 addRawPEL(*rawPelPath, obmcLogID);
84 }
85 else
86 {
Matt Spinler19e72902020-01-24 11:05:20 -060087 auto esel = ad.getValue(additional_data::esel);
88 if (esel)
89 {
90 addESELPEL(*esel, obmcLogID);
91 }
92 else
93 {
94 createPEL(message, obmcLogID, timestamp, severity, additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050095 associations, ffdc);
Matt Spinler19e72902020-01-24 11:05:20 -060096 }
Matt Spinler4e8078c2019-07-09 13:22:32 -050097 }
Adriana Kobylake7d271a2020-12-07 14:32:44 -060098
99 setEntryPath(obmcLogID);
Vijay Lobocbc93a42021-05-20 19:04:07 -0500100 setServiceProviderNotifyFlag(obmcLogID);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500101}
102
103void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
104{
Matt Spinler89fa0822019-07-17 13:54:30 -0500105 if (fs::exists(rawPelPath))
106 {
107 std::ifstream file(rawPelPath, std::ios::in | std::ios::binary);
108
109 auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file),
110 std::istreambuf_iterator<char>());
111 if (file.fail())
112 {
113 log<level::ERR>("Filesystem error reading a raw PEL",
114 entry("PELFILE=%s", rawPelPath.c_str()),
115 entry("OBMCLOGID=%d", obmcLogID));
116 // TODO, Decide what to do here. Maybe nothing.
117 return;
118 }
119
120 file.close();
121
Matt Spinler19e72902020-01-24 11:05:20 -0600122 addPEL(data, obmcLogID);
Matt Spinler67416922021-07-19 12:34:57 -0600123
124 std::error_code ec;
125 fs::remove(rawPelPath, ec);
Matt Spinler89fa0822019-07-17 13:54:30 -0500126 }
127 else
128 {
129 log<level::ERR>("Raw PEL file from BMC event log does not exist",
130 entry("PELFILE=%s", (rawPelPath).c_str()),
131 entry("OBMCLOGID=%d", obmcLogID));
132 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500133}
134
Matt Spinler19e72902020-01-24 11:05:20 -0600135void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID)
136{
Matt Spinler19e72902020-01-24 11:05:20 -0600137 auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID);
138 if (pel->valid())
139 {
Sumit Kumar8ec41562021-10-29 05:39:37 -0500140 // PELs created by others still need this field set by us.
141 pel->setCommitTime();
142
Sumit Kumara1e40842021-06-23 09:52:25 -0500143 // Assign Id other than to Hostbot PEL
144 if ((pel->privateHeader()).creatorID() !=
145 static_cast<uint8_t>(CreatorID::hostboot))
146 {
147 pel->assignID();
148 }
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500149 else
150 {
151 const Repository::LogID id{Repository::LogID::Pel(pel->id())};
152 auto result = _repo.hasPEL(id);
153 if (result)
154 {
155 log<level::WARNING>(
156 fmt::format("Duplicate HostBoot PEL Id {:#X} found; "
157 "moving it to archive folder",
158 pel->id())
159 .c_str());
160
161 _repo.archivePEL(*pel);
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600162
163 // No need to keep around the openBMC event log entry
164 scheduleObmcLogDelete(obmcLogID);
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500165 return;
166 }
167 }
Sumit Kumara1e40842021-06-23 09:52:25 -0500168
Sumit Kumar3160a542021-04-26 08:07:04 -0500169 // Update System Info to Extended User Data
170 pel->updateSysInfoInExtendedUserDataSection(*_dataIface);
171
Sumit Kumar3e274432021-09-14 06:37:56 -0500172 // Check for severity 0x51 and update boot progress SRC
173 updateProgressSRC(pel);
174
Matt Spinler19e72902020-01-24 11:05:20 -0600175 try
176 {
Matt Spinlerd0ab1cf2021-02-10 13:26:18 -0600177 log<level::DEBUG>(
Matt Spinler6321ba32020-07-17 09:58:19 -0500178 fmt::format("Adding external PEL {:#x} (BMC ID {}) to repo",
179 pel->id(), obmcLogID)
180 .c_str());
Matt Spinler5f5352e2020-03-05 16:23:27 -0600181
Matt Spinler19e72902020-01-24 11:05:20 -0600182 _repo.add(pel);
Matt Spinler7e727a32020-07-07 15:00:17 -0500183
184 if (_repo.sizeWarning())
185 {
186 scheduleRepoPrune();
187 }
Matt Spinler1962e082020-08-05 13:44:53 -0500188
189 // Activate any resulting service indicators if necessary
190 auto policy = service_indicators::getPolicy(*_dataIface);
191 policy->activate(*pel);
Matt Spinler19e72902020-01-24 11:05:20 -0600192 }
Patrick Williams66491c62021-10-06 12:23:37 -0500193 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600194 {
195 // Probably a full or r/o filesystem, not much we can do.
196 log<level::ERR>("Unable to add PEL to Repository",
197 entry("PEL_ID=0x%X", pel->id()));
198 }
Andrew Geissler44fc3162020-07-09 09:21:31 -0500199
Vijay Lobod354a392021-06-01 16:21:02 -0500200 updateEventId(pel);
Matt Spinler28d6ae22022-03-18 11:18:27 -0500201 updateResolution(*pel);
Vijay Loboafb1b462021-07-21 23:29:13 -0500202 createPELEntry(obmcLogID);
Matt Spinlerdf5cb832022-07-12 12:47:26 -0500203
204 // Check if firmware should quiesce system due to error
205 checkPelAndQuiesce(pel);
Matt Spinler19e72902020-01-24 11:05:20 -0600206 }
207 else
208 {
209 log<level::ERR>("Invalid PEL received from the host",
210 entry("OBMCLOGID=%d", obmcLogID));
211
212 AdditionalData ad;
213 ad.add("PLID", getNumberString("0x%08X", pel->plid()));
214 ad.add("OBMC_LOG_ID", std::to_string(obmcLogID));
215 ad.add("PEL_SIZE", std::to_string(pelData.size()));
216
217 std::string asciiString;
218 auto src = pel->primarySRC();
219 if (src)
220 {
221 asciiString = (*src)->asciiString();
222 }
223
224 ad.add("SRC", asciiString);
225
226 _eventLogger.log("org.open_power.Logging.Error.BadHostPEL",
227 Entry::Level::Error, ad);
Matt Spinlerfe721892020-04-02 10:28:08 -0500228
229 // Save it to a file for debug in the lab. Just keep the latest.
230 // Not adding it to the PEL because it could already be max size
231 // and don't want to truncate an already invalid PEL.
232 std::ofstream pelFile{getPELRepoPath() / "badPEL"};
233 pelFile.write(reinterpret_cast<const char*>(pelData.data()),
234 pelData.size());
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600235
236 // No need to keep around the openBMC event log entry
237 scheduleObmcLogDelete(obmcLogID);
Matt Spinler19e72902020-01-24 11:05:20 -0600238 }
239}
240
241void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
242{
243 std::vector<uint8_t> data;
244
Matt Spinler5f5352e2020-03-05 16:23:27 -0600245 log<level::DEBUG>("Adding PEL from ESEL",
246 entry("OBMC_LOG_ID=%d", obmcLogID));
247
Matt Spinler19e72902020-01-24 11:05:20 -0600248 try
249 {
250 data = std::move(eselToRawData(esel));
251 }
Patrick Williams66491c62021-10-06 12:23:37 -0500252 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600253 {
254 // Try to add it below anyway, so it follows the usual bad data path.
255 log<level::ERR>("Problems converting ESEL string to a byte vector");
256 }
257
258 addPEL(data, obmcLogID);
259}
260
261std::vector<uint8_t> Manager::eselToRawData(const std::string& esel)
262{
263 std::vector<uint8_t> data;
264 std::string byteString;
265
266 // As the eSEL string looks like: "50 48 00 ab ..." there are 3
267 // characters per raw byte, and since the actual PEL data starts
268 // at the 16th byte, the code will grab the PEL data starting at
269 // offset 48 in the string.
270 static constexpr size_t pelStart = 16 * 3;
271
272 if (esel.size() <= pelStart)
273 {
274 log<level::ERR>("ESEL data too short",
275 entry("ESEL_SIZE=%d", esel.size()));
276
277 throw std::length_error("ESEL data too short");
278 }
279
280 for (size_t i = pelStart; i < esel.size(); i += 3)
281 {
282 if (i + 1 < esel.size())
283 {
284 byteString = esel.substr(i, 2);
285 data.push_back(std::stoi(byteString, nullptr, 16));
286 }
287 else
288 {
289 log<level::ERR>("ESEL data too short",
290 entry("ESEL_SIZE=%d", esel.size()));
291 throw std::length_error("ESEL data too short");
292 }
293 }
294
295 return data;
296}
297
Matt Spinler4e8078c2019-07-09 13:22:32 -0500298void Manager::erase(uint32_t obmcLogID)
299{
Matt Spinler475e5742019-07-18 16:09:49 -0500300 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
301
Vijay Loboafb1b462021-07-21 23:29:13 -0500302 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
303 _pelEntries.erase(path);
Matt Spinler475e5742019-07-18 16:09:49 -0500304 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500305}
306
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500307bool Manager::isDeleteProhibited(uint32_t /*obmcLogID*/)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500308{
309 return false;
310}
311
Matt Spinler56ad2a02020-03-26 14:00:52 -0500312PelFFDC Manager::convertToPelFFDC(const FFDCEntries& ffdc)
313{
314 PelFFDC pelFFDC;
315
316 std::for_each(ffdc.begin(), ffdc.end(), [&pelFFDC](const auto& f) {
317 PelFFDCfile pf;
318 pf.subType = std::get<ffdcSubtypePos>(f);
319 pf.version = std::get<ffdcVersionPos>(f);
320 pf.fd = std::get<ffdcFDPos>(f);
321
322 switch (std::get<ffdcFormatPos>(f))
323 {
324 case Create::FFDCFormat::JSON:
325 pf.format = UserDataFormat::json;
326 break;
327 case Create::FFDCFormat::CBOR:
328 pf.format = UserDataFormat::cbor;
329 break;
330 case Create::FFDCFormat::Text:
331 pf.format = UserDataFormat::text;
332 break;
333 case Create::FFDCFormat::Custom:
334 pf.format = UserDataFormat::custom;
335 break;
336 }
337
338 pelFFDC.push_back(pf);
339 });
340
341 return pelFFDC;
342}
343
Matt Spinler4e8078c2019-07-09 13:22:32 -0500344void Manager::createPEL(const std::string& message, uint32_t obmcLogID,
345 uint64_t timestamp,
346 phosphor::logging::Entry::Level severity,
347 const std::vector<std::string>& additionalData,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500348 const std::vector<std::string>& /*associations*/,
Matt Spinler56ad2a02020-03-26 14:00:52 -0500349 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500350{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800351 auto entry = _registry.lookup(message, rg::LookupType::name);
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500352 auto pelFFDC = convertToPelFFDC(ffdc);
353 AdditionalData ad{additionalData};
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600354 std::string msg;
Matt Spinler67456c22019-10-21 12:22:49 -0500355
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500356 if (!entry)
Matt Spinler67456c22019-10-21 12:22:49 -0500357 {
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500358 // Instead, get the default entry that means there is no
359 // other matching entry. This error will still use the
360 // AdditionalData values of the original error, and this
361 // code will add the error message value that wasn't found
362 // to this AD. This way, there will at least be a PEL,
363 // possibly with callouts, to allow users to debug the
364 // issue that caused the error even without its own PEL.
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600365 msg = "Event not found in PEL message registry: " + message;
366 log<level::INFO>(msg.c_str());
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500367
368 entry = _registry.lookup(defaultLogMessage, rg::LookupType::name);
369 if (!entry)
370 {
371 log<level::ERR>("Default event not found in PEL message registry");
372 return;
373 }
374
375 ad.add(additional_data::error, message);
376 }
377
378 auto pel = std::make_unique<openpower::pels::PEL>(
Matt Spinler9d921092022-12-15 11:54:49 -0600379 *entry, obmcLogID, timestamp, severity, ad, pelFFDC, *_dataIface,
380 *_journal);
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500381
382 _repo.add(pel);
383
384 if (_repo.sizeWarning())
385 {
386 scheduleRepoPrune();
387 }
388
389 auto src = pel->primarySRC();
390 if (src)
391 {
Patrick Williams2544b412022-10-04 08:41:06 -0500392 auto m = fmt::format("Created PEL {:#x} (BMC ID {}) with SRC {}",
393 pel->id(), pel->obmcLogID(),
394 (*src)->asciiString());
Matt Spinler45796e82022-07-01 11:25:27 -0500395 while (m.back() == ' ')
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500396 {
Matt Spinler45796e82022-07-01 11:25:27 -0500397 m.pop_back();
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500398 }
Matt Spinler45796e82022-07-01 11:25:27 -0500399 log<level::INFO>(m.c_str());
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600400 }
Matt Spinler1962e082020-08-05 13:44:53 -0500401
Sumit Kumar3e274432021-09-14 06:37:56 -0500402 // Check for severity 0x51 and update boot progress SRC
403 updateProgressSRC(pel);
404
Matt Spinler1962e082020-08-05 13:44:53 -0500405 // Activate any resulting service indicators if necessary
406 auto policy = service_indicators::getPolicy(*_dataIface);
407 policy->activate(*pel);
Andrew Geissler44fc3162020-07-09 09:21:31 -0500408
Matt Spinler8b81ec02022-07-12 13:25:37 -0500409 updateDBusSeverity(*pel);
Vijay Lobod354a392021-06-01 16:21:02 -0500410 updateEventId(pel);
Matt Spinler28d6ae22022-03-18 11:18:27 -0500411 updateResolution(*pel);
Vijay Loboafb1b462021-07-21 23:29:13 -0500412 createPELEntry(obmcLogID);
Matt Spinlerdf5cb832022-07-12 12:47:26 -0500413
414 // Check if firmware should quiesce system due to error
415 checkPelAndQuiesce(pel);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500416}
417
Matt Spinlera34ab722019-12-16 10:39:32 -0600418sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
419{
420 Repository::LogID id{Repository::LogID::Pel(pelID)};
421 std::optional<int> fd;
422
Matt Spinler5f5352e2020-03-05 16:23:27 -0600423 log<level::DEBUG>("getPEL", entry("PEL_ID=0x%X", pelID));
424
Matt Spinlera34ab722019-12-16 10:39:32 -0600425 try
426 {
427 fd = _repo.getPELFD(id);
428 }
Patrick Williams66491c62021-10-06 12:23:37 -0500429 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600430 {
431 throw common_error::InternalFailure();
432 }
433
434 if (!fd)
435 {
436 throw common_error::InvalidArgument();
437 }
438
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600439 scheduleFDClose(*fd);
440
Matt Spinlera34ab722019-12-16 10:39:32 -0600441 return *fd;
442}
443
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600444void Manager::scheduleFDClose(int fd)
445{
446 _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500447 _event, std::bind(std::mem_fn(&Manager::closeFD), this, fd,
448 std::placeholders::_1));
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600449}
450
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500451void Manager::closeFD(int fd, sdeventplus::source::EventBase& /*source*/)
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600452{
453 close(fd);
454 _fdCloserEventSource.reset();
455}
456
Matt Spinlera34ab722019-12-16 10:39:32 -0600457std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
458{
459 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
460 std::optional<std::vector<uint8_t>> data;
461
Matt Spinler5f5352e2020-03-05 16:23:27 -0600462 log<level::DEBUG>("getPELFromOBMCID", entry("OBMC_LOG_ID=%d", obmcLogID));
463
Matt Spinlera34ab722019-12-16 10:39:32 -0600464 try
465 {
466 data = _repo.getPELData(id);
467 }
Patrick Williams66491c62021-10-06 12:23:37 -0500468 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600469 {
470 throw common_error::InternalFailure();
471 }
472
473 if (!data)
474 {
475 throw common_error::InvalidArgument();
476 }
477
478 return *data;
479}
480
481void Manager::hostAck(uint32_t pelID)
482{
483 Repository::LogID id{Repository::LogID::Pel(pelID)};
484
Matt Spinler5f5352e2020-03-05 16:23:27 -0600485 log<level::DEBUG>("HostAck", entry("PEL_ID=0x%X", pelID));
486
Matt Spinlera34ab722019-12-16 10:39:32 -0600487 if (!_repo.hasPEL(id))
488 {
489 throw common_error::InvalidArgument();
490 }
491
492 if (_hostNotifier)
493 {
494 _hostNotifier->ackPEL(pelID);
495 }
496}
497
498void Manager::hostReject(uint32_t pelID, RejectionReason reason)
499{
500 Repository::LogID id{Repository::LogID::Pel(pelID)};
501
Matt Spinler5f5352e2020-03-05 16:23:27 -0600502 log<level::DEBUG>("HostReject", entry("PEL_ID=0x%X", pelID),
503 entry("REASON=%d", static_cast<int>(reason)));
504
Matt Spinlera34ab722019-12-16 10:39:32 -0600505 if (!_repo.hasPEL(id))
506 {
507 throw common_error::InvalidArgument();
508 }
509
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600510 if (reason == RejectionReason::BadPEL)
Matt Spinlera34ab722019-12-16 10:39:32 -0600511 {
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600512 AdditionalData data;
513 data.add("BAD_ID", getNumberString("0x%08X", pelID));
514 _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
515 Entry::Level::Informational, data);
516 if (_hostNotifier)
Matt Spinlera34ab722019-12-16 10:39:32 -0600517 {
518 _hostNotifier->setBadPEL(pelID);
519 }
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600520 }
521 else if ((reason == RejectionReason::HostFull) && _hostNotifier)
522 {
523 _hostNotifier->setHostFull(pelID);
Matt Spinlera34ab722019-12-16 10:39:32 -0600524 }
525}
526
Matt Spinler7e727a32020-07-07 15:00:17 -0500527void Manager::scheduleRepoPrune()
528{
Matt Spinler7e727a32020-07-07 15:00:17 -0500529 _repoPrunerEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500530 _event, std::bind(std::mem_fn(&Manager::pruneRepo), this,
531 std::placeholders::_1));
Matt Spinler7e727a32020-07-07 15:00:17 -0500532}
533
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500534void Manager::pruneRepo(sdeventplus::source::EventBase& /*source*/)
Matt Spinler7e727a32020-07-07 15:00:17 -0500535{
Sumit Kumar027bf282022-01-24 11:25:19 -0600536 auto idsWithHwIsoEntry = _dataIface->getLogIDWithHwIsolation();
537
538 auto idsToDelete = _repo.prune(idsWithHwIsoEntry);
Matt Spinler7e727a32020-07-07 15:00:17 -0500539
540 // Remove the OpenBMC event logs for the PELs that were just removed.
541 std::for_each(idsToDelete.begin(), idsToDelete.end(),
542 [this](auto id) { this->_logManager.erase(id); });
543
544 _repoPrunerEventSource.reset();
545}
546
Matt Spinlerff9cec22020-07-15 13:06:35 -0500547void Manager::setupPELDeleteWatch()
548{
549 _pelFileDeleteFD = inotify_init1(IN_NONBLOCK);
550 if (-1 == _pelFileDeleteFD)
551 {
552 auto e = errno;
Patrick Williams2544b412022-10-04 08:41:06 -0500553 std::string msg = "inotify_init1 failed with errno " +
554 std::to_string(e);
Matt Spinlerff9cec22020-07-15 13:06:35 -0500555 log<level::ERR>(msg.c_str());
556 abort();
557 }
558
559 _pelFileDeleteWatchFD = inotify_add_watch(
560 _pelFileDeleteFD, _repo.repoPath().c_str(), IN_DELETE);
561 if (-1 == _pelFileDeleteWatchFD)
562 {
563 auto e = errno;
Patrick Williams2544b412022-10-04 08:41:06 -0500564 std::string msg = "inotify_add_watch failed with error " +
565 std::to_string(e);
Matt Spinlerff9cec22020-07-15 13:06:35 -0500566 log<level::ERR>(msg.c_str());
567 abort();
568 }
569
570 _pelFileDeleteEventSource = std::make_unique<sdeventplus::source::IO>(
571 _event, _pelFileDeleteFD, EPOLLIN,
572 std::bind(std::mem_fn(&Manager::pelFileDeleted), this,
573 std::placeholders::_1, std::placeholders::_2,
574 std::placeholders::_3));
575}
576
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500577void Manager::pelFileDeleted(sdeventplus::source::IO& /*io*/, int /*fd*/,
Matt Spinlerff9cec22020-07-15 13:06:35 -0500578 uint32_t revents)
579{
580 if (!(revents & EPOLLIN))
581 {
582 return;
583 }
584
585 // An event for 1 PEL uses 48B. When all PELs are deleted at once,
586 // as many events as there is room for can be handled in one callback.
587 // A size of 2000 will allow 41 to be processed, with additional
588 // callbacks being needed to process the remaining ones.
Matt Spinler9d59d582021-05-19 07:57:10 -0600589 std::array<uint8_t, 2000> data{};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500590 auto bytesRead = read(_pelFileDeleteFD, data.data(), data.size());
591 if (bytesRead < 0)
592 {
593 auto e = errno;
594 std::string msg = "Failed reading data from inotify event, errno = " +
595 std::to_string(e);
596 log<level::ERR>(msg.c_str());
597 abort();
598 }
599
600 auto offset = 0;
601 while (offset < bytesRead)
602 {
603 auto event = reinterpret_cast<inotify_event*>(&data[offset]);
604 if (event->mask & IN_DELETE)
605 {
606 std::string filename{event->name};
607
608 // Get the PEL ID from the filename and tell the
609 // repo it's been removed, and then delete the BMC
610 // event log if it's there.
611 auto pos = filename.find_first_of('_');
612 if (pos != std::string::npos)
613 {
614 try
615 {
616 auto idString = filename.substr(pos + 1);
617 auto pelID = std::stoul(idString, nullptr, 16);
618
619 Repository::LogID id{Repository::LogID::Pel(pelID)};
620 auto removedLogID = _repo.remove(id);
621 if (removedLogID)
622 {
623 _logManager.erase(removedLogID->obmcID.id);
624 }
625 }
626 catch (const std::exception& e)
627 {
628 log<level::INFO>("Could not find PEL ID from its filename",
629 entry("FILENAME=%s", filename.c_str()));
630 }
631 }
632 }
633
634 offset += offsetof(inotify_event, name) + event->len;
635 }
636}
Matt Spinler9cc30072020-09-16 15:39:34 -0500637
638std::tuple<uint32_t, uint32_t> Manager::createPELWithFFDCFiles(
639 std::string message, Entry::Level severity,
640 std::map<std::string, std::string> additionalData,
641 std::vector<std::tuple<
642 sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat,
643 uint8_t, uint8_t, sdbusplus::message::unix_fd>>
644 fFDC)
645{
Matt Spinler44893cc2020-08-26 11:34:17 -0500646 _logManager.createWithFFDC(message, severity, additionalData, fFDC);
647
648 return {_logManager.lastEntryID(), _repo.lastPelID()};
Matt Spinler9cc30072020-09-16 15:39:34 -0500649}
650
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500651std::string Manager::getPELJSON(uint32_t obmcLogID)
Matt Spinleraa85a072022-03-23 11:26:41 -0500652{
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500653 // Throws InvalidArgument if not found
654 auto pelID = getPELIdFromBMCLogId(obmcLogID);
655
656 auto cmd = fmt::format("/usr/bin/peltool -i {:#x}", pelID);
657
658 FILE* pipe = popen(cmd.c_str(), "r");
659 if (!pipe)
660 {
661 log<level::ERR>(fmt::format("Error running {}", cmd).c_str());
662 throw common_error::InternalFailure();
663 }
664
665 std::string output;
666 std::array<char, 1024> buffer;
667 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
668 {
669 output.append(buffer.data());
670 }
671
672 int rc = pclose(pipe);
673 if (WEXITSTATUS(rc) != 0)
674 {
675 log<level::ERR>(
676 fmt::format("Error running {}, rc = {}", cmd, rc).c_str());
677 throw common_error::InternalFailure();
678 }
679
680 return output;
Matt Spinleraa85a072022-03-23 11:26:41 -0500681}
682
Andrew Geissler44fc3162020-07-09 09:21:31 -0500683void Manager::checkPelAndQuiesce(std::unique_ptr<openpower::pels::PEL>& pel)
684{
Matt Spinlerb2abc042021-05-17 15:32:50 -0600685 if ((pel->userHeader().severity() ==
686 static_cast<uint8_t>(SeverityType::nonError)) ||
687 (pel->userHeader().severity() ==
688 static_cast<uint8_t>(SeverityType::recovered)))
Andrew Geissler44fc3162020-07-09 09:21:31 -0500689 {
Matt Spinlerb2abc042021-05-17 15:32:50 -0600690 log<level::DEBUG>(
691 "PEL severity informational or recovered. no quiesce needed");
Andrew Geissler44fc3162020-07-09 09:21:31 -0500692 return;
693 }
694 if (!_logManager.isQuiesceOnErrorEnabled())
695 {
696 log<level::DEBUG>("QuiesceOnHwError not enabled, no quiesce needed");
697 return;
698 }
699
Matt Spinler845c6242022-03-01 16:45:08 -0600700 CreatorID creatorID{pel->privateHeader().creatorID()};
701
702 if ((creatorID != CreatorID::openBMC) &&
703 (creatorID != CreatorID::hostboot) &&
704 (creatorID != CreatorID::ioDrawer) && (creatorID != CreatorID::occ) &&
705 (creatorID != CreatorID::phyp))
706 {
707 return;
708 }
709
Andrew Geissler44fc3162020-07-09 09:21:31 -0500710 // Now check if it has any type of callout
Andrew Geisslerf8e750d2022-01-14 14:56:13 -0600711 if (pel->isHwCalloutPresent())
Andrew Geissler44fc3162020-07-09 09:21:31 -0500712 {
Matt Spinlerb2abc042021-05-17 15:32:50 -0600713 log<level::INFO>(
714 "QuiesceOnHwError enabled, PEL severity not nonError or recovered, "
715 "and callout is present");
Andrew Geissler44fc3162020-07-09 09:21:31 -0500716
717 _logManager.quiesceOnError(pel->obmcLogID());
718 }
719}
720
Vijay Lobod354a392021-06-01 16:21:02 -0500721std::string Manager::getEventId(const openpower::pels::PEL& pel) const
722{
723 std::string str;
724 auto src = pel.primarySRC();
725 if (src)
726 {
727 const auto& hexwords = (*src)->hexwordData();
728
729 std::string refcode = (*src)->asciiString();
730 size_t pos = refcode.find_last_not_of(0x20);
731 if (pos != std::string::npos)
732 {
733 refcode.erase(pos + 1);
734 }
735 str = refcode;
736
737 for (auto& value : hexwords)
738 {
739 str += " ";
740 str += getNumberString("%08X", value);
741 }
742 }
Matt Spinler0003af12022-06-08 10:46:17 -0500743 return sanitizeFieldForDBus(str);
Vijay Lobod354a392021-06-01 16:21:02 -0500744}
745
746void Manager::updateEventId(std::unique_ptr<openpower::pels::PEL>& pel)
747{
748 std::string eventIdStr = getEventId(*pel);
749
750 auto entryN = _logManager.entries.find(pel->obmcLogID());
751 if (entryN != _logManager.entries.end())
752 {
753 entryN->second->eventId(eventIdStr);
754 }
755}
756
Matt Spinler0003af12022-06-08 10:46:17 -0500757std::string Manager::sanitizeFieldForDBus(std::string field)
758{
759 std::for_each(field.begin(), field.end(), [](char& ch) {
760 if (((ch < ' ') || (ch > '~')) && (ch != '\n') && (ch != '\t'))
761 {
762 ch = ' ';
763 }
764 });
765 return field;
766}
767
Vijay Lobo593a4c62021-06-16 14:25:26 -0500768std::string Manager::getResolution(const openpower::pels::PEL& pel) const
769{
770 std::string str;
771 std::string resolution;
772 auto src = pel.primarySRC();
773 if (src)
774 {
775 // First extract the callout pointer and then go through
776 const auto& callouts = (*src)->callouts();
777 namespace pv = openpower::pels::pel_values;
778 // All PELs dont have callout, check before parsing callout data
779 if (callouts)
780 {
781 const auto& entries = callouts->callouts();
782 // Entry starts with index 1
783 uint8_t index = 1;
784 for (auto& entry : entries)
785 {
786 resolution += std::to_string(index) + ". ";
787 // Adding Location code to resolution
788 if (!entry->locationCode().empty())
Patrick Williams2544b412022-10-04 08:41:06 -0500789 resolution += "Location Code: " + entry->locationCode() +
790 ", ";
Vijay Lobo593a4c62021-06-16 14:25:26 -0500791 if (entry->fruIdentity())
792 {
793 // Get priority and set the resolution string
794 str = pv::getValue(entry->priority(),
795 pel_values::calloutPriorityValues,
796 pel_values::registryNamePos);
797 str[0] = toupper(str[0]);
798 resolution += "Priority: " + str + ", ";
799 if (entry->fruIdentity()->getPN().has_value())
800 {
801 resolution +=
802 "PN: " + entry->fruIdentity()->getPN().value() +
803 ", ";
804 }
805 if (entry->fruIdentity()->getSN().has_value())
806 {
807 resolution +=
808 "SN: " + entry->fruIdentity()->getSN().value() +
809 ", ";
810 }
811 if (entry->fruIdentity()->getCCIN().has_value())
812 {
813 resolution +=
814 "CCIN: " + entry->fruIdentity()->getCCIN().value() +
815 ", ";
816 }
817 // Add the maintenance procedure
818 if (entry->fruIdentity()->getMaintProc().has_value())
819 {
820 resolution +=
821 "Procedure: " +
822 entry->fruIdentity()->getMaintProc().value() + ", ";
823 }
824 }
825 resolution.resize(resolution.size() - 2);
826 resolution += "\n";
827 index++;
828 }
829 }
830 }
Matt Spinler0003af12022-06-08 10:46:17 -0500831 return sanitizeFieldForDBus(resolution);
Vijay Lobo593a4c62021-06-16 14:25:26 -0500832}
833
Matt Spinler28d6ae22022-03-18 11:18:27 -0500834bool Manager::updateResolution(const openpower::pels::PEL& pel)
Vijay Lobo593a4c62021-06-16 14:25:26 -0500835{
Matt Spinler28d6ae22022-03-18 11:18:27 -0500836 std::string callouts = getResolution(pel);
837 auto entryN = _logManager.entries.find(pel.obmcLogID());
Vijay Lobo593a4c62021-06-16 14:25:26 -0500838 if (entryN != _logManager.entries.end())
839 {
Matt Spinler734ed2b2022-01-21 09:31:46 -0600840 entryN->second->resolution(callouts, true);
Vijay Lobo593a4c62021-06-16 14:25:26 -0500841 }
Matt Spinler28d6ae22022-03-18 11:18:27 -0500842
843 return false;
Vijay Lobo593a4c62021-06-16 14:25:26 -0500844}
845
Matt Spinler8b81ec02022-07-12 13:25:37 -0500846void Manager::updateDBusSeverity(const openpower::pels::PEL& pel)
847{
848 // The final severity of the PEL may not agree with the
849 // original severity of the D-Bus event log. Update the
850 // D-Bus property to match in some cases. This is to
851 // ensure there isn't a Critical or Warning Redfish event
852 // log for an informational or recovered PEL (or vice versa).
853 // This doesn't make an explicit call to serialize the new
854 // event log property value because updateEventId() is called
855 // right after this and will do it.
856 auto sevType =
857 static_cast<SeverityType>(pel.userHeader().severity() & 0xF0);
858
859 auto entryN = _logManager.entries.find(pel.obmcLogID());
860 if (entryN != _logManager.entries.end())
861 {
Patrick Williams2544b412022-10-04 08:41:06 -0500862 auto newSeverity = fixupLogSeverity(entryN->second->severity(),
863 sevType);
Matt Spinler8b81ec02022-07-12 13:25:37 -0500864 if (newSeverity)
865 {
866 log<level::INFO>(
867 fmt::format(
868 "Changing event log {} severity from {} "
869 "to {} to match PEL",
870 entryN->second->id(),
871 Entry::convertLevelToString(entryN->second->severity()),
872 Entry::convertLevelToString(*newSeverity))
873 .c_str());
874
875 entryN->second->severity(*newSeverity, true);
876 }
877 }
878}
879
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600880void Manager::setEntryPath(uint32_t obmcLogID)
881{
882 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
883 if (auto attributes = _repo.getPELAttributes(id); attributes)
884 {
885 auto& attr = attributes.value().get();
886 auto entry = _logManager.entries.find(obmcLogID);
887 if (entry != _logManager.entries.end())
888 {
Matt Spinler734ed2b2022-01-21 09:31:46 -0600889 entry->second->path(attr.path, true);
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600890 }
891 }
892}
893
Vijay Lobocbc93a42021-05-20 19:04:07 -0500894void Manager::setServiceProviderNotifyFlag(uint32_t obmcLogID)
895{
896 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
897 if (auto attributes = _repo.getPELAttributes(id); attributes)
898 {
899 auto& attr = attributes.value().get();
900 auto entry = _logManager.entries.find(obmcLogID);
901 if (entry != _logManager.entries.end())
902 {
Lakshmi Yadlapati7a3ede52022-11-18 13:26:17 -0600903 if (attr.actionFlags.test(callHomeFlagBit))
904 {
905 entry->second->serviceProviderNotify(Entry::Notify::Notify);
906 }
907 else
908 {
909 entry->second->serviceProviderNotify(Entry::Notify::Inhibit);
910 }
Vijay Lobocbc93a42021-05-20 19:04:07 -0500911 }
912 }
913}
914
Matt Spinler734ed2b2022-01-21 09:31:46 -0600915void Manager::createPELEntry(uint32_t obmcLogID, bool skipIaSignal)
Vijay Loboafb1b462021-07-21 23:29:13 -0500916{
917 std::map<std::string, PropertiesVariant> varData;
918 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
919 if (auto attributes = _repo.getPELAttributes(id); attributes)
920 {
921 namespace pv = openpower::pels::pel_values;
922 auto& attr = attributes.value().get();
Vijay Lobob2e541e2021-08-31 23:12:47 -0500923
924 // get the hidden flag values
925 auto sevType = static_cast<SeverityType>(attr.severity & 0xF0);
926 auto isHidden = true;
927 if (((sevType != SeverityType::nonError) &&
928 attr.actionFlags.test(reportFlagBit) &&
929 !attr.actionFlags.test(hiddenFlagBit)) ||
930 ((sevType == SeverityType::nonError) &&
931 attr.actionFlags.test(serviceActionFlagBit)))
932 {
933 isHidden = false;
934 }
935 varData.emplace(std::string("Hidden"), isHidden);
Vijay Loboafb1b462021-07-21 23:29:13 -0500936 varData.emplace(
937 std::string("Subsystem"),
938 pv::getValue(attr.subsystem, pel_values::subsystemValues));
Vijay Lobo2fb10212021-08-22 23:24:16 -0500939
940 varData.emplace(
941 std::string("ManagementSystemAck"),
942 (attr.hmcState == TransmissionState::acked ? true : false));
943
Matt Spinler8e65f4e2023-05-02 13:40:08 -0500944 varData.emplace("PlatformLogID", attr.plid);
945 varData.emplace("Deconfig", attr.deconfig);
946 varData.emplace("Guard", attr.guard);
947 varData.emplace("Timestamp", attr.creationTime);
948
Vijay Loboafb1b462021-07-21 23:29:13 -0500949 // Path to create PELEntry Interface is same as PEL
950 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
951 // Create Interface for PELEntry and set properties
Vijay Lobo2fb10212021-08-22 23:24:16 -0500952 auto pelEntry = std::make_unique<PELEntry>(_logManager.getBus(), path,
953 varData, obmcLogID, &_repo);
Matt Spinler734ed2b2022-01-21 09:31:46 -0600954 if (!skipIaSignal)
955 {
956 pelEntry->emit_added();
957 }
Vijay Loboafb1b462021-07-21 23:29:13 -0500958 _pelEntries.emplace(std::move(path), std::move(pelEntry));
959 }
960}
961
Ramesh Iyyarf4203c42021-06-24 06:09:23 -0500962uint32_t Manager::getPELIdFromBMCLogId(uint32_t bmcLogId)
963{
964 Repository::LogID id{Repository::LogID::Obmc(bmcLogId)};
965 if (auto logId = _repo.getLogID(id); !logId.has_value())
966 {
967 throw common_error::InvalidArgument();
968 }
969 else
970 {
971 return logId->pelID.id;
972 }
973}
974
Ramesh Iyyar530efbf2021-06-24 06:22:22 -0500975uint32_t Manager::getBMCLogIdFromPELId(uint32_t pelId)
976{
977 Repository::LogID id{Repository::LogID::Pel(pelId)};
978 if (auto logId = _repo.getLogID(id); !logId.has_value())
979 {
980 throw common_error::InvalidArgument();
981 }
982 else
983 {
984 return logId->obmcID.id;
985 }
986}
987
Sumit Kumar3e274432021-09-14 06:37:56 -0500988void Manager::updateProgressSRC(
989 std::unique_ptr<openpower::pels::PEL>& pel) const
990{
991 // Check for pel severity of type - 0x51 = critical error, system
992 // termination
993 if (pel->userHeader().severity() == 0x51)
994 {
995 auto src = pel->primarySRC();
996 if (src)
997 {
998 std::vector<uint8_t> asciiSRC = (*src)->getSrcStruct();
999 uint64_t srcRefCode = 0;
1000
1001 // Read bytes from offset [40-47] e.g. BD8D1001
1002 for (int i = 0; i < 8; i++)
1003 {
Patrick Williams2544b412022-10-04 08:41:06 -05001004 srcRefCode |= (static_cast<uint64_t>(asciiSRC[40 + i])
1005 << (8 * i));
Sumit Kumar3e274432021-09-14 06:37:56 -05001006 }
1007
1008 try
1009 {
1010 _dataIface->createProgressSRC(srcRefCode, asciiSRC);
1011 }
Matt Spinler87f39242023-05-01 11:36:18 -05001012 catch (const std::exception&)
Sumit Kumar3e274432021-09-14 06:37:56 -05001013 {
1014 // Exception - may be no boot progress interface on dbus
1015 }
1016 }
1017 }
1018}
1019
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -06001020void Manager::scheduleObmcLogDelete(uint32_t obmcLogID)
1021{
1022 _obmcLogDeleteEventSource = std::make_unique<sdeventplus::source::Defer>(
1023 _event, std::bind(std::mem_fn(&Manager::deleteObmcLog), this,
1024 std::placeholders::_1, obmcLogID));
1025}
1026
1027void Manager::deleteObmcLog(sdeventplus::source::EventBase&, uint32_t obmcLogID)
1028{
1029 log<level::INFO>(
1030 fmt::format("Removing event log with no PEL: {}", obmcLogID).c_str());
1031 _logManager.erase(obmcLogID);
1032 _obmcLogDeleteEventSource.reset();
1033}
1034
Matt Spinler4e8078c2019-07-09 13:22:32 -05001035} // namespace pels
1036} // namespace openpower