blob: ee5919dcc2222818f23907f74580455d00fcdaa8 [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 Spinler89fa0822019-07-17 13:54:30 -050023
Matt Spinler22421b92020-07-17 09:41:08 -050024#include <fmt/format.h>
Matt Spinlerff9cec22020-07-15 13:06:35 -050025#include <sys/inotify.h>
Matt Spinler6b1a5c82020-01-07 08:48:53 -060026#include <unistd.h>
27
Matt Spinler89fa0822019-07-17 13:54:30 -050028#include <filesystem>
29#include <fstream>
Matt Spinler0003af12022-06-08 10:46:17 -050030#include <locale>
Matt Spinlera34ab722019-12-16 10:39:32 -060031#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler56ad2a02020-03-26 14:00:52 -050032#include <xyz/openbmc_project/Logging/Create/server.hpp>
Matt Spinler4e8078c2019-07-09 13:22:32 -050033
34namespace openpower
35{
36namespace pels
37{
38
39using namespace phosphor::logging;
Matt Spinler89fa0822019-07-17 13:54:30 -050040namespace fs = std::filesystem;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080041namespace rg = openpower::pels::message;
Matt Spinler4e8078c2019-07-09 13:22:32 -050042
Matt Spinlera34ab722019-12-16 10:39:32 -060043namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
44
Matt Spinler56ad2a02020-03-26 14:00:52 -050045using Create = sdbusplus::xyz::openbmc_project::Logging::server::Create;
46
Matt Spinler4e8078c2019-07-09 13:22:32 -050047namespace additional_data
48{
49constexpr auto rawPEL = "RAWPEL";
Matt Spinler19e72902020-01-24 11:05:20 -060050constexpr auto esel = "ESEL";
Matt Spinler30ddc9f2020-07-16 15:39:59 -050051constexpr auto error = "ERROR_NAME";
Matt Spinler19e72902020-01-24 11:05:20 -060052} // namespace additional_data
Matt Spinler4e8078c2019-07-09 13:22:32 -050053
Matt Spinler30ddc9f2020-07-16 15:39:59 -050054constexpr auto defaultLogMessage = "xyz.openbmc_project.Logging.Error.Default";
55
Matt Spinlerff9cec22020-07-15 13:06:35 -050056Manager::~Manager()
57{
58 if (_pelFileDeleteFD != -1)
59 {
60 if (_pelFileDeleteWatchFD != -1)
61 {
62 inotify_rm_watch(_pelFileDeleteFD, _pelFileDeleteWatchFD);
63 }
64 close(_pelFileDeleteFD);
65 }
66}
67
Matt Spinler4e8078c2019-07-09 13:22:32 -050068void Manager::create(const std::string& message, uint32_t obmcLogID,
69 uint64_t timestamp, Entry::Level severity,
70 const std::vector<std::string>& additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050071 const std::vector<std::string>& associations,
72 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -050073{
74 AdditionalData ad{additionalData};
75
Matt Spinler19e72902020-01-24 11:05:20 -060076 // If a PEL was passed in via a filename or in an ESEL,
77 // use that. Otherwise, create one.
Matt Spinler4e8078c2019-07-09 13:22:32 -050078 auto rawPelPath = ad.getValue(additional_data::rawPEL);
79 if (rawPelPath)
80 {
81 addRawPEL(*rawPelPath, obmcLogID);
82 }
83 else
84 {
Matt Spinler19e72902020-01-24 11:05:20 -060085 auto esel = ad.getValue(additional_data::esel);
86 if (esel)
87 {
88 addESELPEL(*esel, obmcLogID);
89 }
90 else
91 {
92 createPEL(message, obmcLogID, timestamp, severity, additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050093 associations, ffdc);
Matt Spinler19e72902020-01-24 11:05:20 -060094 }
Matt Spinler4e8078c2019-07-09 13:22:32 -050095 }
Adriana Kobylake7d271a2020-12-07 14:32:44 -060096
97 setEntryPath(obmcLogID);
Vijay Lobocbc93a42021-05-20 19:04:07 -050098 setServiceProviderNotifyFlag(obmcLogID);
Matt Spinler4e8078c2019-07-09 13:22:32 -050099}
100
101void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
102{
Matt Spinler89fa0822019-07-17 13:54:30 -0500103 if (fs::exists(rawPelPath))
104 {
105 std::ifstream file(rawPelPath, std::ios::in | std::ios::binary);
106
107 auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file),
108 std::istreambuf_iterator<char>());
109 if (file.fail())
110 {
111 log<level::ERR>("Filesystem error reading a raw PEL",
112 entry("PELFILE=%s", rawPelPath.c_str()),
113 entry("OBMCLOGID=%d", obmcLogID));
114 // TODO, Decide what to do here. Maybe nothing.
115 return;
116 }
117
118 file.close();
119
Matt Spinler19e72902020-01-24 11:05:20 -0600120 addPEL(data, obmcLogID);
Matt Spinler67416922021-07-19 12:34:57 -0600121
122 std::error_code ec;
123 fs::remove(rawPelPath, ec);
Matt Spinler89fa0822019-07-17 13:54:30 -0500124 }
125 else
126 {
127 log<level::ERR>("Raw PEL file from BMC event log does not exist",
128 entry("PELFILE=%s", (rawPelPath).c_str()),
129 entry("OBMCLOGID=%d", obmcLogID));
130 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500131}
132
Matt Spinler19e72902020-01-24 11:05:20 -0600133void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID)
134{
Matt Spinler19e72902020-01-24 11:05:20 -0600135 auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID);
136 if (pel->valid())
137 {
Sumit Kumar8ec41562021-10-29 05:39:37 -0500138 // PELs created by others still need this field set by us.
139 pel->setCommitTime();
140
Sumit Kumara1e40842021-06-23 09:52:25 -0500141 // Assign Id other than to Hostbot PEL
142 if ((pel->privateHeader()).creatorID() !=
143 static_cast<uint8_t>(CreatorID::hostboot))
144 {
145 pel->assignID();
146 }
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500147 else
148 {
149 const Repository::LogID id{Repository::LogID::Pel(pel->id())};
150 auto result = _repo.hasPEL(id);
151 if (result)
152 {
153 log<level::WARNING>(
154 fmt::format("Duplicate HostBoot PEL Id {:#X} found; "
155 "moving it to archive folder",
156 pel->id())
157 .c_str());
158
159 _repo.archivePEL(*pel);
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600160
161 // No need to keep around the openBMC event log entry
162 scheduleObmcLogDelete(obmcLogID);
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500163 return;
164 }
165 }
Sumit Kumara1e40842021-06-23 09:52:25 -0500166
Sumit Kumar3160a542021-04-26 08:07:04 -0500167 // Update System Info to Extended User Data
168 pel->updateSysInfoInExtendedUserDataSection(*_dataIface);
169
Sumit Kumar3e274432021-09-14 06:37:56 -0500170 // Check for severity 0x51 and update boot progress SRC
171 updateProgressSRC(pel);
172
Matt Spinler19e72902020-01-24 11:05:20 -0600173 try
174 {
Matt Spinlerd0ab1cf2021-02-10 13:26:18 -0600175 log<level::DEBUG>(
Matt Spinler6321ba32020-07-17 09:58:19 -0500176 fmt::format("Adding external PEL {:#x} (BMC ID {}) to repo",
177 pel->id(), obmcLogID)
178 .c_str());
Matt Spinler5f5352e2020-03-05 16:23:27 -0600179
Matt Spinler19e72902020-01-24 11:05:20 -0600180 _repo.add(pel);
Matt Spinler7e727a32020-07-07 15:00:17 -0500181
182 if (_repo.sizeWarning())
183 {
184 scheduleRepoPrune();
185 }
Matt Spinler1962e082020-08-05 13:44:53 -0500186
187 // Activate any resulting service indicators if necessary
188 auto policy = service_indicators::getPolicy(*_dataIface);
189 policy->activate(*pel);
Matt Spinler19e72902020-01-24 11:05:20 -0600190 }
Patrick Williams66491c62021-10-06 12:23:37 -0500191 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600192 {
193 // Probably a full or r/o filesystem, not much we can do.
194 log<level::ERR>("Unable to add PEL to Repository",
195 entry("PEL_ID=0x%X", pel->id()));
196 }
Andrew Geissler44fc3162020-07-09 09:21:31 -0500197
198 // Check if firmware should quiesce system due to error
199 checkPelAndQuiesce(pel);
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 Spinler19e72902020-01-24 11:05:20 -0600203 }
204 else
205 {
206 log<level::ERR>("Invalid PEL received from the host",
207 entry("OBMCLOGID=%d", obmcLogID));
208
209 AdditionalData ad;
210 ad.add("PLID", getNumberString("0x%08X", pel->plid()));
211 ad.add("OBMC_LOG_ID", std::to_string(obmcLogID));
212 ad.add("PEL_SIZE", std::to_string(pelData.size()));
213
214 std::string asciiString;
215 auto src = pel->primarySRC();
216 if (src)
217 {
218 asciiString = (*src)->asciiString();
219 }
220
221 ad.add("SRC", asciiString);
222
223 _eventLogger.log("org.open_power.Logging.Error.BadHostPEL",
224 Entry::Level::Error, ad);
Matt Spinlerfe721892020-04-02 10:28:08 -0500225
226 // Save it to a file for debug in the lab. Just keep the latest.
227 // Not adding it to the PEL because it could already be max size
228 // and don't want to truncate an already invalid PEL.
229 std::ofstream pelFile{getPELRepoPath() / "badPEL"};
230 pelFile.write(reinterpret_cast<const char*>(pelData.data()),
231 pelData.size());
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600232
233 // No need to keep around the openBMC event log entry
234 scheduleObmcLogDelete(obmcLogID);
Matt Spinler19e72902020-01-24 11:05:20 -0600235 }
236}
237
238void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
239{
240 std::vector<uint8_t> data;
241
Matt Spinler5f5352e2020-03-05 16:23:27 -0600242 log<level::DEBUG>("Adding PEL from ESEL",
243 entry("OBMC_LOG_ID=%d", obmcLogID));
244
Matt Spinler19e72902020-01-24 11:05:20 -0600245 try
246 {
247 data = std::move(eselToRawData(esel));
248 }
Patrick Williams66491c62021-10-06 12:23:37 -0500249 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600250 {
251 // Try to add it below anyway, so it follows the usual bad data path.
252 log<level::ERR>("Problems converting ESEL string to a byte vector");
253 }
254
255 addPEL(data, obmcLogID);
256}
257
258std::vector<uint8_t> Manager::eselToRawData(const std::string& esel)
259{
260 std::vector<uint8_t> data;
261 std::string byteString;
262
263 // As the eSEL string looks like: "50 48 00 ab ..." there are 3
264 // characters per raw byte, and since the actual PEL data starts
265 // at the 16th byte, the code will grab the PEL data starting at
266 // offset 48 in the string.
267 static constexpr size_t pelStart = 16 * 3;
268
269 if (esel.size() <= pelStart)
270 {
271 log<level::ERR>("ESEL data too short",
272 entry("ESEL_SIZE=%d", esel.size()));
273
274 throw std::length_error("ESEL data too short");
275 }
276
277 for (size_t i = pelStart; i < esel.size(); i += 3)
278 {
279 if (i + 1 < esel.size())
280 {
281 byteString = esel.substr(i, 2);
282 data.push_back(std::stoi(byteString, nullptr, 16));
283 }
284 else
285 {
286 log<level::ERR>("ESEL data too short",
287 entry("ESEL_SIZE=%d", esel.size()));
288 throw std::length_error("ESEL data too short");
289 }
290 }
291
292 return data;
293}
294
Matt Spinler4e8078c2019-07-09 13:22:32 -0500295void Manager::erase(uint32_t obmcLogID)
296{
Matt Spinler475e5742019-07-18 16:09:49 -0500297 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
298
Vijay Loboafb1b462021-07-21 23:29:13 -0500299 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
300 _pelEntries.erase(path);
Matt Spinler475e5742019-07-18 16:09:49 -0500301 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500302}
303
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500304bool Manager::isDeleteProhibited(uint32_t /*obmcLogID*/)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500305{
306 return false;
307}
308
Matt Spinler56ad2a02020-03-26 14:00:52 -0500309PelFFDC Manager::convertToPelFFDC(const FFDCEntries& ffdc)
310{
311 PelFFDC pelFFDC;
312
313 std::for_each(ffdc.begin(), ffdc.end(), [&pelFFDC](const auto& f) {
314 PelFFDCfile pf;
315 pf.subType = std::get<ffdcSubtypePos>(f);
316 pf.version = std::get<ffdcVersionPos>(f);
317 pf.fd = std::get<ffdcFDPos>(f);
318
319 switch (std::get<ffdcFormatPos>(f))
320 {
321 case Create::FFDCFormat::JSON:
322 pf.format = UserDataFormat::json;
323 break;
324 case Create::FFDCFormat::CBOR:
325 pf.format = UserDataFormat::cbor;
326 break;
327 case Create::FFDCFormat::Text:
328 pf.format = UserDataFormat::text;
329 break;
330 case Create::FFDCFormat::Custom:
331 pf.format = UserDataFormat::custom;
332 break;
333 }
334
335 pelFFDC.push_back(pf);
336 });
337
338 return pelFFDC;
339}
340
Matt Spinler4e8078c2019-07-09 13:22:32 -0500341void Manager::createPEL(const std::string& message, uint32_t obmcLogID,
342 uint64_t timestamp,
343 phosphor::logging::Entry::Level severity,
344 const std::vector<std::string>& additionalData,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500345 const std::vector<std::string>& /*associations*/,
Matt Spinler56ad2a02020-03-26 14:00:52 -0500346 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500347{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800348 auto entry = _registry.lookup(message, rg::LookupType::name);
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500349 auto pelFFDC = convertToPelFFDC(ffdc);
350 AdditionalData ad{additionalData};
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600351 std::string msg;
Matt Spinler67456c22019-10-21 12:22:49 -0500352
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500353 if (!entry)
Matt Spinler67456c22019-10-21 12:22:49 -0500354 {
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500355 // Instead, get the default entry that means there is no
356 // other matching entry. This error will still use the
357 // AdditionalData values of the original error, and this
358 // code will add the error message value that wasn't found
359 // to this AD. This way, there will at least be a PEL,
360 // possibly with callouts, to allow users to debug the
361 // issue that caused the error even without its own PEL.
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600362 msg = "Event not found in PEL message registry: " + message;
363 log<level::INFO>(msg.c_str());
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500364
365 entry = _registry.lookup(defaultLogMessage, rg::LookupType::name);
366 if (!entry)
367 {
368 log<level::ERR>("Default event not found in PEL message registry");
369 return;
370 }
371
372 ad.add(additional_data::error, message);
373 }
374
375 auto pel = std::make_unique<openpower::pels::PEL>(
376 *entry, obmcLogID, timestamp, severity, ad, pelFFDC, *_dataIface);
377
378 _repo.add(pel);
379
380 if (_repo.sizeWarning())
381 {
382 scheduleRepoPrune();
383 }
384
385 auto src = pel->primarySRC();
386 if (src)
387 {
Matt Spinler22421b92020-07-17 09:41:08 -0500388 auto msg =
389 fmt::format("Created PEL {:#x} (BMC ID {}) with SRC {}", pel->id(),
390 pel->obmcLogID(), (*src)->asciiString());
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500391 while (msg.back() == ' ')
392 {
393 msg.pop_back();
394 }
395 log<level::INFO>(msg.c_str());
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600396 }
Matt Spinler1962e082020-08-05 13:44:53 -0500397
Sumit Kumar3e274432021-09-14 06:37:56 -0500398 // Check for severity 0x51 and update boot progress SRC
399 updateProgressSRC(pel);
400
Matt Spinler1962e082020-08-05 13:44:53 -0500401 // Activate any resulting service indicators if necessary
402 auto policy = service_indicators::getPolicy(*_dataIface);
403 policy->activate(*pel);
Andrew Geissler44fc3162020-07-09 09:21:31 -0500404
405 // Check if firmware should quiesce system due to error
406 checkPelAndQuiesce(pel);
Vijay Lobod354a392021-06-01 16:21:02 -0500407 updateEventId(pel);
Matt Spinler28d6ae22022-03-18 11:18:27 -0500408 updateResolution(*pel);
Vijay Loboafb1b462021-07-21 23:29:13 -0500409 createPELEntry(obmcLogID);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500410}
411
Matt Spinlera34ab722019-12-16 10:39:32 -0600412sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
413{
414 Repository::LogID id{Repository::LogID::Pel(pelID)};
415 std::optional<int> fd;
416
Matt Spinler5f5352e2020-03-05 16:23:27 -0600417 log<level::DEBUG>("getPEL", entry("PEL_ID=0x%X", pelID));
418
Matt Spinlera34ab722019-12-16 10:39:32 -0600419 try
420 {
421 fd = _repo.getPELFD(id);
422 }
Patrick Williams66491c62021-10-06 12:23:37 -0500423 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600424 {
425 throw common_error::InternalFailure();
426 }
427
428 if (!fd)
429 {
430 throw common_error::InvalidArgument();
431 }
432
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600433 scheduleFDClose(*fd);
434
Matt Spinlera34ab722019-12-16 10:39:32 -0600435 return *fd;
436}
437
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600438void Manager::scheduleFDClose(int fd)
439{
440 _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500441 _event, std::bind(std::mem_fn(&Manager::closeFD), this, fd,
442 std::placeholders::_1));
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600443}
444
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500445void Manager::closeFD(int fd, sdeventplus::source::EventBase& /*source*/)
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600446{
447 close(fd);
448 _fdCloserEventSource.reset();
449}
450
Matt Spinlera34ab722019-12-16 10:39:32 -0600451std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
452{
453 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
454 std::optional<std::vector<uint8_t>> data;
455
Matt Spinler5f5352e2020-03-05 16:23:27 -0600456 log<level::DEBUG>("getPELFromOBMCID", entry("OBMC_LOG_ID=%d", obmcLogID));
457
Matt Spinlera34ab722019-12-16 10:39:32 -0600458 try
459 {
460 data = _repo.getPELData(id);
461 }
Patrick Williams66491c62021-10-06 12:23:37 -0500462 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600463 {
464 throw common_error::InternalFailure();
465 }
466
467 if (!data)
468 {
469 throw common_error::InvalidArgument();
470 }
471
472 return *data;
473}
474
475void Manager::hostAck(uint32_t pelID)
476{
477 Repository::LogID id{Repository::LogID::Pel(pelID)};
478
Matt Spinler5f5352e2020-03-05 16:23:27 -0600479 log<level::DEBUG>("HostAck", entry("PEL_ID=0x%X", pelID));
480
Matt Spinlera34ab722019-12-16 10:39:32 -0600481 if (!_repo.hasPEL(id))
482 {
483 throw common_error::InvalidArgument();
484 }
485
486 if (_hostNotifier)
487 {
488 _hostNotifier->ackPEL(pelID);
489 }
490}
491
492void Manager::hostReject(uint32_t pelID, RejectionReason reason)
493{
494 Repository::LogID id{Repository::LogID::Pel(pelID)};
495
Matt Spinler5f5352e2020-03-05 16:23:27 -0600496 log<level::DEBUG>("HostReject", entry("PEL_ID=0x%X", pelID),
497 entry("REASON=%d", static_cast<int>(reason)));
498
Matt Spinlera34ab722019-12-16 10:39:32 -0600499 if (!_repo.hasPEL(id))
500 {
501 throw common_error::InvalidArgument();
502 }
503
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600504 if (reason == RejectionReason::BadPEL)
Matt Spinlera34ab722019-12-16 10:39:32 -0600505 {
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600506 AdditionalData data;
507 data.add("BAD_ID", getNumberString("0x%08X", pelID));
508 _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
509 Entry::Level::Informational, data);
510 if (_hostNotifier)
Matt Spinlera34ab722019-12-16 10:39:32 -0600511 {
512 _hostNotifier->setBadPEL(pelID);
513 }
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600514 }
515 else if ((reason == RejectionReason::HostFull) && _hostNotifier)
516 {
517 _hostNotifier->setHostFull(pelID);
Matt Spinlera34ab722019-12-16 10:39:32 -0600518 }
519}
520
Matt Spinler7e727a32020-07-07 15:00:17 -0500521void Manager::scheduleRepoPrune()
522{
Matt Spinler7e727a32020-07-07 15:00:17 -0500523 _repoPrunerEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500524 _event, std::bind(std::mem_fn(&Manager::pruneRepo), this,
525 std::placeholders::_1));
Matt Spinler7e727a32020-07-07 15:00:17 -0500526}
527
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500528void Manager::pruneRepo(sdeventplus::source::EventBase& /*source*/)
Matt Spinler7e727a32020-07-07 15:00:17 -0500529{
Sumit Kumar027bf282022-01-24 11:25:19 -0600530 auto idsWithHwIsoEntry = _dataIface->getLogIDWithHwIsolation();
531
532 auto idsToDelete = _repo.prune(idsWithHwIsoEntry);
Matt Spinler7e727a32020-07-07 15:00:17 -0500533
534 // Remove the OpenBMC event logs for the PELs that were just removed.
535 std::for_each(idsToDelete.begin(), idsToDelete.end(),
536 [this](auto id) { this->_logManager.erase(id); });
537
538 _repoPrunerEventSource.reset();
539}
540
Matt Spinlerff9cec22020-07-15 13:06:35 -0500541void Manager::setupPELDeleteWatch()
542{
543 _pelFileDeleteFD = inotify_init1(IN_NONBLOCK);
544 if (-1 == _pelFileDeleteFD)
545 {
546 auto e = errno;
547 std::string msg =
548 "inotify_init1 failed with errno " + std::to_string(e);
549 log<level::ERR>(msg.c_str());
550 abort();
551 }
552
553 _pelFileDeleteWatchFD = inotify_add_watch(
554 _pelFileDeleteFD, _repo.repoPath().c_str(), IN_DELETE);
555 if (-1 == _pelFileDeleteWatchFD)
556 {
557 auto e = errno;
558 std::string msg =
559 "inotify_add_watch failed with error " + std::to_string(e);
560 log<level::ERR>(msg.c_str());
561 abort();
562 }
563
564 _pelFileDeleteEventSource = std::make_unique<sdeventplus::source::IO>(
565 _event, _pelFileDeleteFD, EPOLLIN,
566 std::bind(std::mem_fn(&Manager::pelFileDeleted), this,
567 std::placeholders::_1, std::placeholders::_2,
568 std::placeholders::_3));
569}
570
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500571void Manager::pelFileDeleted(sdeventplus::source::IO& /*io*/, int /*fd*/,
Matt Spinlerff9cec22020-07-15 13:06:35 -0500572 uint32_t revents)
573{
574 if (!(revents & EPOLLIN))
575 {
576 return;
577 }
578
579 // An event for 1 PEL uses 48B. When all PELs are deleted at once,
580 // as many events as there is room for can be handled in one callback.
581 // A size of 2000 will allow 41 to be processed, with additional
582 // callbacks being needed to process the remaining ones.
Matt Spinler9d59d582021-05-19 07:57:10 -0600583 std::array<uint8_t, 2000> data{};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500584 auto bytesRead = read(_pelFileDeleteFD, data.data(), data.size());
585 if (bytesRead < 0)
586 {
587 auto e = errno;
588 std::string msg = "Failed reading data from inotify event, errno = " +
589 std::to_string(e);
590 log<level::ERR>(msg.c_str());
591 abort();
592 }
593
594 auto offset = 0;
595 while (offset < bytesRead)
596 {
597 auto event = reinterpret_cast<inotify_event*>(&data[offset]);
598 if (event->mask & IN_DELETE)
599 {
600 std::string filename{event->name};
601
602 // Get the PEL ID from the filename and tell the
603 // repo it's been removed, and then delete the BMC
604 // event log if it's there.
605 auto pos = filename.find_first_of('_');
606 if (pos != std::string::npos)
607 {
608 try
609 {
610 auto idString = filename.substr(pos + 1);
611 auto pelID = std::stoul(idString, nullptr, 16);
612
613 Repository::LogID id{Repository::LogID::Pel(pelID)};
614 auto removedLogID = _repo.remove(id);
615 if (removedLogID)
616 {
617 _logManager.erase(removedLogID->obmcID.id);
618 }
619 }
620 catch (const std::exception& e)
621 {
622 log<level::INFO>("Could not find PEL ID from its filename",
623 entry("FILENAME=%s", filename.c_str()));
624 }
625 }
626 }
627
628 offset += offsetof(inotify_event, name) + event->len;
629 }
630}
Matt Spinler9cc30072020-09-16 15:39:34 -0500631
632std::tuple<uint32_t, uint32_t> Manager::createPELWithFFDCFiles(
633 std::string message, Entry::Level severity,
634 std::map<std::string, std::string> additionalData,
635 std::vector<std::tuple<
636 sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat,
637 uint8_t, uint8_t, sdbusplus::message::unix_fd>>
638 fFDC)
639{
Matt Spinler44893cc2020-08-26 11:34:17 -0500640 _logManager.createWithFFDC(message, severity, additionalData, fFDC);
641
642 return {_logManager.lastEntryID(), _repo.lastPelID()};
Matt Spinler9cc30072020-09-16 15:39:34 -0500643}
644
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500645std::string Manager::getPELJSON(uint32_t obmcLogID)
Matt Spinleraa85a072022-03-23 11:26:41 -0500646{
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500647 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
648
649 // Throws InvalidArgument if not found
650 auto pelID = getPELIdFromBMCLogId(obmcLogID);
651
652 auto cmd = fmt::format("/usr/bin/peltool -i {:#x}", pelID);
653
654 FILE* pipe = popen(cmd.c_str(), "r");
655 if (!pipe)
656 {
657 log<level::ERR>(fmt::format("Error running {}", cmd).c_str());
658 throw common_error::InternalFailure();
659 }
660
661 std::string output;
662 std::array<char, 1024> buffer;
663 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
664 {
665 output.append(buffer.data());
666 }
667
668 int rc = pclose(pipe);
669 if (WEXITSTATUS(rc) != 0)
670 {
671 log<level::ERR>(
672 fmt::format("Error running {}, rc = {}", cmd, rc).c_str());
673 throw common_error::InternalFailure();
674 }
675
676 return output;
Matt Spinleraa85a072022-03-23 11:26:41 -0500677}
678
Andrew Geissler44fc3162020-07-09 09:21:31 -0500679void Manager::checkPelAndQuiesce(std::unique_ptr<openpower::pels::PEL>& pel)
680{
Matt Spinlerb2abc042021-05-17 15:32:50 -0600681 if ((pel->userHeader().severity() ==
682 static_cast<uint8_t>(SeverityType::nonError)) ||
683 (pel->userHeader().severity() ==
684 static_cast<uint8_t>(SeverityType::recovered)))
Andrew Geissler44fc3162020-07-09 09:21:31 -0500685 {
Matt Spinlerb2abc042021-05-17 15:32:50 -0600686 log<level::DEBUG>(
687 "PEL severity informational or recovered. no quiesce needed");
Andrew Geissler44fc3162020-07-09 09:21:31 -0500688 return;
689 }
690 if (!_logManager.isQuiesceOnErrorEnabled())
691 {
692 log<level::DEBUG>("QuiesceOnHwError not enabled, no quiesce needed");
693 return;
694 }
695
Matt Spinler845c6242022-03-01 16:45:08 -0600696 CreatorID creatorID{pel->privateHeader().creatorID()};
697
698 if ((creatorID != CreatorID::openBMC) &&
699 (creatorID != CreatorID::hostboot) &&
700 (creatorID != CreatorID::ioDrawer) && (creatorID != CreatorID::occ) &&
701 (creatorID != CreatorID::phyp))
702 {
703 return;
704 }
705
Andrew Geissler44fc3162020-07-09 09:21:31 -0500706 // Now check if it has any type of callout
Andrew Geisslerf8e750d2022-01-14 14:56:13 -0600707 if (pel->isHwCalloutPresent())
Andrew Geissler44fc3162020-07-09 09:21:31 -0500708 {
Matt Spinlerb2abc042021-05-17 15:32:50 -0600709 log<level::INFO>(
710 "QuiesceOnHwError enabled, PEL severity not nonError or recovered, "
711 "and callout is present");
Andrew Geissler44fc3162020-07-09 09:21:31 -0500712
713 _logManager.quiesceOnError(pel->obmcLogID());
714 }
715}
716
Vijay Lobod354a392021-06-01 16:21:02 -0500717std::string Manager::getEventId(const openpower::pels::PEL& pel) const
718{
719 std::string str;
720 auto src = pel.primarySRC();
721 if (src)
722 {
723 const auto& hexwords = (*src)->hexwordData();
724
725 std::string refcode = (*src)->asciiString();
726 size_t pos = refcode.find_last_not_of(0x20);
727 if (pos != std::string::npos)
728 {
729 refcode.erase(pos + 1);
730 }
731 str = refcode;
732
733 for (auto& value : hexwords)
734 {
735 str += " ";
736 str += getNumberString("%08X", value);
737 }
738 }
Matt Spinler0003af12022-06-08 10:46:17 -0500739 return sanitizeFieldForDBus(str);
Vijay Lobod354a392021-06-01 16:21:02 -0500740}
741
742void Manager::updateEventId(std::unique_ptr<openpower::pels::PEL>& pel)
743{
744 std::string eventIdStr = getEventId(*pel);
745
746 auto entryN = _logManager.entries.find(pel->obmcLogID());
747 if (entryN != _logManager.entries.end())
748 {
749 entryN->second->eventId(eventIdStr);
750 }
751}
752
Matt Spinler0003af12022-06-08 10:46:17 -0500753std::string Manager::sanitizeFieldForDBus(std::string field)
754{
755 std::for_each(field.begin(), field.end(), [](char& ch) {
756 if (((ch < ' ') || (ch > '~')) && (ch != '\n') && (ch != '\t'))
757 {
758 ch = ' ';
759 }
760 });
761 return field;
762}
763
Vijay Lobo593a4c62021-06-16 14:25:26 -0500764std::string Manager::getResolution(const openpower::pels::PEL& pel) const
765{
766 std::string str;
767 std::string resolution;
768 auto src = pel.primarySRC();
769 if (src)
770 {
771 // First extract the callout pointer and then go through
772 const auto& callouts = (*src)->callouts();
773 namespace pv = openpower::pels::pel_values;
774 // All PELs dont have callout, check before parsing callout data
775 if (callouts)
776 {
777 const auto& entries = callouts->callouts();
778 // Entry starts with index 1
779 uint8_t index = 1;
780 for (auto& entry : entries)
781 {
782 resolution += std::to_string(index) + ". ";
783 // Adding Location code to resolution
784 if (!entry->locationCode().empty())
785 resolution +=
786 "Location Code: " + entry->locationCode() + ", ";
787 if (entry->fruIdentity())
788 {
789 // Get priority and set the resolution string
790 str = pv::getValue(entry->priority(),
791 pel_values::calloutPriorityValues,
792 pel_values::registryNamePos);
793 str[0] = toupper(str[0]);
794 resolution += "Priority: " + str + ", ";
795 if (entry->fruIdentity()->getPN().has_value())
796 {
797 resolution +=
798 "PN: " + entry->fruIdentity()->getPN().value() +
799 ", ";
800 }
801 if (entry->fruIdentity()->getSN().has_value())
802 {
803 resolution +=
804 "SN: " + entry->fruIdentity()->getSN().value() +
805 ", ";
806 }
807 if (entry->fruIdentity()->getCCIN().has_value())
808 {
809 resolution +=
810 "CCIN: " + entry->fruIdentity()->getCCIN().value() +
811 ", ";
812 }
813 // Add the maintenance procedure
814 if (entry->fruIdentity()->getMaintProc().has_value())
815 {
816 resolution +=
817 "Procedure: " +
818 entry->fruIdentity()->getMaintProc().value() + ", ";
819 }
820 }
821 resolution.resize(resolution.size() - 2);
822 resolution += "\n";
823 index++;
824 }
825 }
826 }
Matt Spinler0003af12022-06-08 10:46:17 -0500827 return sanitizeFieldForDBus(resolution);
Vijay Lobo593a4c62021-06-16 14:25:26 -0500828}
829
Matt Spinler28d6ae22022-03-18 11:18:27 -0500830bool Manager::updateResolution(const openpower::pels::PEL& pel)
Vijay Lobo593a4c62021-06-16 14:25:26 -0500831{
Matt Spinler28d6ae22022-03-18 11:18:27 -0500832 std::string callouts = getResolution(pel);
833 auto entryN = _logManager.entries.find(pel.obmcLogID());
Vijay Lobo593a4c62021-06-16 14:25:26 -0500834 if (entryN != _logManager.entries.end())
835 {
Matt Spinler734ed2b2022-01-21 09:31:46 -0600836 entryN->second->resolution(callouts, true);
Vijay Lobo593a4c62021-06-16 14:25:26 -0500837 }
Matt Spinler28d6ae22022-03-18 11:18:27 -0500838
839 return false;
Vijay Lobo593a4c62021-06-16 14:25:26 -0500840}
841
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600842void Manager::setEntryPath(uint32_t obmcLogID)
843{
844 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
845 if (auto attributes = _repo.getPELAttributes(id); attributes)
846 {
847 auto& attr = attributes.value().get();
848 auto entry = _logManager.entries.find(obmcLogID);
849 if (entry != _logManager.entries.end())
850 {
Matt Spinler734ed2b2022-01-21 09:31:46 -0600851 entry->second->path(attr.path, true);
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600852 }
853 }
854}
855
Vijay Lobocbc93a42021-05-20 19:04:07 -0500856void Manager::setServiceProviderNotifyFlag(uint32_t obmcLogID)
857{
858 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
859 if (auto attributes = _repo.getPELAttributes(id); attributes)
860 {
861 auto& attr = attributes.value().get();
862 auto entry = _logManager.entries.find(obmcLogID);
863 if (entry != _logManager.entries.end())
864 {
Vijay Loboafb1b462021-07-21 23:29:13 -0500865 entry->second->serviceProviderNotify(
Matt Spinler734ed2b2022-01-21 09:31:46 -0600866 attr.actionFlags.test(callHomeFlagBit), true);
Vijay Lobocbc93a42021-05-20 19:04:07 -0500867 }
868 }
869}
870
Matt Spinler734ed2b2022-01-21 09:31:46 -0600871void Manager::createPELEntry(uint32_t obmcLogID, bool skipIaSignal)
Vijay Loboafb1b462021-07-21 23:29:13 -0500872{
873 std::map<std::string, PropertiesVariant> varData;
874 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
875 if (auto attributes = _repo.getPELAttributes(id); attributes)
876 {
877 namespace pv = openpower::pels::pel_values;
878 auto& attr = attributes.value().get();
Vijay Lobob2e541e2021-08-31 23:12:47 -0500879
880 // get the hidden flag values
881 auto sevType = static_cast<SeverityType>(attr.severity & 0xF0);
882 auto isHidden = true;
883 if (((sevType != SeverityType::nonError) &&
884 attr.actionFlags.test(reportFlagBit) &&
885 !attr.actionFlags.test(hiddenFlagBit)) ||
886 ((sevType == SeverityType::nonError) &&
887 attr.actionFlags.test(serviceActionFlagBit)))
888 {
889 isHidden = false;
890 }
891 varData.emplace(std::string("Hidden"), isHidden);
Vijay Loboafb1b462021-07-21 23:29:13 -0500892 varData.emplace(
893 std::string("Subsystem"),
894 pv::getValue(attr.subsystem, pel_values::subsystemValues));
Vijay Lobo2fb10212021-08-22 23:24:16 -0500895
896 varData.emplace(
897 std::string("ManagementSystemAck"),
898 (attr.hmcState == TransmissionState::acked ? true : false));
899
Vijay Loboafb1b462021-07-21 23:29:13 -0500900 // Path to create PELEntry Interface is same as PEL
901 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
902 // Create Interface for PELEntry and set properties
Vijay Lobo2fb10212021-08-22 23:24:16 -0500903 auto pelEntry = std::make_unique<PELEntry>(_logManager.getBus(), path,
904 varData, obmcLogID, &_repo);
Matt Spinler734ed2b2022-01-21 09:31:46 -0600905 if (!skipIaSignal)
906 {
907 pelEntry->emit_added();
908 }
Vijay Loboafb1b462021-07-21 23:29:13 -0500909 _pelEntries.emplace(std::move(path), std::move(pelEntry));
910 }
911}
912
Ramesh Iyyarf4203c42021-06-24 06:09:23 -0500913uint32_t Manager::getPELIdFromBMCLogId(uint32_t bmcLogId)
914{
915 Repository::LogID id{Repository::LogID::Obmc(bmcLogId)};
916 if (auto logId = _repo.getLogID(id); !logId.has_value())
917 {
918 throw common_error::InvalidArgument();
919 }
920 else
921 {
922 return logId->pelID.id;
923 }
924}
925
Ramesh Iyyar530efbf2021-06-24 06:22:22 -0500926uint32_t Manager::getBMCLogIdFromPELId(uint32_t pelId)
927{
928 Repository::LogID id{Repository::LogID::Pel(pelId)};
929 if (auto logId = _repo.getLogID(id); !logId.has_value())
930 {
931 throw common_error::InvalidArgument();
932 }
933 else
934 {
935 return logId->obmcID.id;
936 }
937}
938
Sumit Kumar3e274432021-09-14 06:37:56 -0500939void Manager::updateProgressSRC(
940 std::unique_ptr<openpower::pels::PEL>& pel) const
941{
942 // Check for pel severity of type - 0x51 = critical error, system
943 // termination
944 if (pel->userHeader().severity() == 0x51)
945 {
946 auto src = pel->primarySRC();
947 if (src)
948 {
949 std::vector<uint8_t> asciiSRC = (*src)->getSrcStruct();
950 uint64_t srcRefCode = 0;
951
952 // Read bytes from offset [40-47] e.g. BD8D1001
953 for (int i = 0; i < 8; i++)
954 {
955 srcRefCode |=
956 (static_cast<uint64_t>(asciiSRC[40 + i]) << (8 * i));
957 }
958
959 try
960 {
961 _dataIface->createProgressSRC(srcRefCode, asciiSRC);
962 }
963 catch (std::exception& e)
964 {
965 // Exception - may be no boot progress interface on dbus
966 }
967 }
968 }
969}
970
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600971void Manager::scheduleObmcLogDelete(uint32_t obmcLogID)
972{
973 _obmcLogDeleteEventSource = std::make_unique<sdeventplus::source::Defer>(
974 _event, std::bind(std::mem_fn(&Manager::deleteObmcLog), this,
975 std::placeholders::_1, obmcLogID));
976}
977
978void Manager::deleteObmcLog(sdeventplus::source::EventBase&, uint32_t obmcLogID)
979{
980 log<level::INFO>(
981 fmt::format("Removing event log with no PEL: {}", obmcLogID).c_str());
982 _logManager.erase(obmcLogID);
983 _obmcLogDeleteEventSource.reset();
984}
985
Matt Spinler4e8078c2019-07-09 13:22:32 -0500986} // namespace pels
987} // namespace openpower