blob: 152e4fb017fe37011cbc85127d44015cad5cc5e3 [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
Matt Spinler89fa0822019-07-17 13:54:30 -050029#include <filesystem>
30#include <fstream>
Matt Spinler0003af12022-06-08 10:46:17 -050031#include <locale>
Matt Spinlera34ab722019-12-16 10:39:32 -060032#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler56ad2a02020-03-26 14:00:52 -050033#include <xyz/openbmc_project/Logging/Create/server.hpp>
Matt Spinler4e8078c2019-07-09 13:22:32 -050034
35namespace openpower
36{
37namespace pels
38{
39
40using namespace phosphor::logging;
Matt Spinler89fa0822019-07-17 13:54:30 -050041namespace fs = std::filesystem;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080042namespace rg = openpower::pels::message;
Matt Spinler4e8078c2019-07-09 13:22:32 -050043
Matt Spinlera34ab722019-12-16 10:39:32 -060044namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
45
Matt Spinler56ad2a02020-03-26 14:00:52 -050046using Create = sdbusplus::xyz::openbmc_project::Logging::server::Create;
47
Matt Spinler4e8078c2019-07-09 13:22:32 -050048namespace additional_data
49{
50constexpr auto rawPEL = "RAWPEL";
Matt Spinler19e72902020-01-24 11:05:20 -060051constexpr auto esel = "ESEL";
Matt Spinler30ddc9f2020-07-16 15:39:59 -050052constexpr auto error = "ERROR_NAME";
Matt Spinler19e72902020-01-24 11:05:20 -060053} // namespace additional_data
Matt Spinler4e8078c2019-07-09 13:22:32 -050054
Matt Spinler30ddc9f2020-07-16 15:39:59 -050055constexpr auto defaultLogMessage = "xyz.openbmc_project.Logging.Error.Default";
56
Matt Spinlerff9cec22020-07-15 13:06:35 -050057Manager::~Manager()
58{
59 if (_pelFileDeleteFD != -1)
60 {
61 if (_pelFileDeleteWatchFD != -1)
62 {
63 inotify_rm_watch(_pelFileDeleteFD, _pelFileDeleteWatchFD);
64 }
65 close(_pelFileDeleteFD);
66 }
67}
68
Matt Spinler4e8078c2019-07-09 13:22:32 -050069void Manager::create(const std::string& message, uint32_t obmcLogID,
70 uint64_t timestamp, Entry::Level severity,
71 const std::vector<std::string>& additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050072 const std::vector<std::string>& associations,
73 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -050074{
75 AdditionalData ad{additionalData};
76
Matt Spinler19e72902020-01-24 11:05:20 -060077 // If a PEL was passed in via a filename or in an ESEL,
78 // use that. Otherwise, create one.
Matt Spinler4e8078c2019-07-09 13:22:32 -050079 auto rawPelPath = ad.getValue(additional_data::rawPEL);
80 if (rawPelPath)
81 {
82 addRawPEL(*rawPelPath, obmcLogID);
83 }
84 else
85 {
Matt Spinler19e72902020-01-24 11:05:20 -060086 auto esel = ad.getValue(additional_data::esel);
87 if (esel)
88 {
89 addESELPEL(*esel, obmcLogID);
90 }
91 else
92 {
93 createPEL(message, obmcLogID, timestamp, severity, additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050094 associations, ffdc);
Matt Spinler19e72902020-01-24 11:05:20 -060095 }
Matt Spinler4e8078c2019-07-09 13:22:32 -050096 }
Adriana Kobylake7d271a2020-12-07 14:32:44 -060097
98 setEntryPath(obmcLogID);
Vijay Lobocbc93a42021-05-20 19:04:07 -050099 setServiceProviderNotifyFlag(obmcLogID);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500100}
101
102void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
103{
Matt Spinler89fa0822019-07-17 13:54:30 -0500104 if (fs::exists(rawPelPath))
105 {
106 std::ifstream file(rawPelPath, std::ios::in | std::ios::binary);
107
108 auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file),
109 std::istreambuf_iterator<char>());
110 if (file.fail())
111 {
112 log<level::ERR>("Filesystem error reading a raw PEL",
113 entry("PELFILE=%s", rawPelPath.c_str()),
114 entry("OBMCLOGID=%d", obmcLogID));
115 // TODO, Decide what to do here. Maybe nothing.
116 return;
117 }
118
119 file.close();
120
Matt Spinler19e72902020-01-24 11:05:20 -0600121 addPEL(data, obmcLogID);
Matt Spinler67416922021-07-19 12:34:57 -0600122
123 std::error_code ec;
124 fs::remove(rawPelPath, ec);
Matt Spinler89fa0822019-07-17 13:54:30 -0500125 }
126 else
127 {
128 log<level::ERR>("Raw PEL file from BMC event log does not exist",
129 entry("PELFILE=%s", (rawPelPath).c_str()),
130 entry("OBMCLOGID=%d", obmcLogID));
131 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500132}
133
Matt Spinler19e72902020-01-24 11:05:20 -0600134void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID)
135{
Matt Spinler19e72902020-01-24 11:05:20 -0600136 auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID);
137 if (pel->valid())
138 {
Sumit Kumar8ec41562021-10-29 05:39:37 -0500139 // PELs created by others still need this field set by us.
140 pel->setCommitTime();
141
Sumit Kumara1e40842021-06-23 09:52:25 -0500142 // Assign Id other than to Hostbot PEL
143 if ((pel->privateHeader()).creatorID() !=
144 static_cast<uint8_t>(CreatorID::hostboot))
145 {
146 pel->assignID();
147 }
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500148 else
149 {
150 const Repository::LogID id{Repository::LogID::Pel(pel->id())};
151 auto result = _repo.hasPEL(id);
152 if (result)
153 {
154 log<level::WARNING>(
155 fmt::format("Duplicate HostBoot PEL Id {:#X} found; "
156 "moving it to archive folder",
157 pel->id())
158 .c_str());
159
160 _repo.archivePEL(*pel);
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600161
162 // No need to keep around the openBMC event log entry
163 scheduleObmcLogDelete(obmcLogID);
Sumit Kumar2ccdcef2021-07-31 10:04:58 -0500164 return;
165 }
166 }
Sumit Kumara1e40842021-06-23 09:52:25 -0500167
Sumit Kumar3160a542021-04-26 08:07:04 -0500168 // Update System Info to Extended User Data
169 pel->updateSysInfoInExtendedUserDataSection(*_dataIface);
170
Sumit Kumar3e274432021-09-14 06:37:56 -0500171 // Check for severity 0x51 and update boot progress SRC
172 updateProgressSRC(pel);
173
Matt Spinler19e72902020-01-24 11:05:20 -0600174 try
175 {
Matt Spinlerd0ab1cf2021-02-10 13:26:18 -0600176 log<level::DEBUG>(
Matt Spinler6321ba32020-07-17 09:58:19 -0500177 fmt::format("Adding external PEL {:#x} (BMC ID {}) to repo",
178 pel->id(), obmcLogID)
179 .c_str());
Matt Spinler5f5352e2020-03-05 16:23:27 -0600180
Matt Spinler19e72902020-01-24 11:05:20 -0600181 _repo.add(pel);
Matt Spinler7e727a32020-07-07 15:00:17 -0500182
183 if (_repo.sizeWarning())
184 {
185 scheduleRepoPrune();
186 }
Matt Spinler1962e082020-08-05 13:44:53 -0500187
188 // Activate any resulting service indicators if necessary
189 auto policy = service_indicators::getPolicy(*_dataIface);
190 policy->activate(*pel);
Matt Spinler19e72902020-01-24 11:05:20 -0600191 }
Patrick Williams66491c62021-10-06 12:23:37 -0500192 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600193 {
194 // Probably a full or r/o filesystem, not much we can do.
195 log<level::ERR>("Unable to add PEL to Repository",
196 entry("PEL_ID=0x%X", pel->id()));
197 }
Andrew Geissler44fc3162020-07-09 09:21:31 -0500198
Vijay Lobod354a392021-06-01 16:21:02 -0500199 updateEventId(pel);
Matt Spinler28d6ae22022-03-18 11:18:27 -0500200 updateResolution(*pel);
Vijay Loboafb1b462021-07-21 23:29:13 -0500201 createPELEntry(obmcLogID);
Matt Spinlerdf5cb832022-07-12 12:47:26 -0500202
203 // Check if firmware should quiesce system due to error
204 checkPelAndQuiesce(pel);
Matt Spinler19e72902020-01-24 11:05:20 -0600205 }
206 else
207 {
208 log<level::ERR>("Invalid PEL received from the host",
209 entry("OBMCLOGID=%d", obmcLogID));
210
211 AdditionalData ad;
212 ad.add("PLID", getNumberString("0x%08X", pel->plid()));
213 ad.add("OBMC_LOG_ID", std::to_string(obmcLogID));
214 ad.add("PEL_SIZE", std::to_string(pelData.size()));
215
216 std::string asciiString;
217 auto src = pel->primarySRC();
218 if (src)
219 {
220 asciiString = (*src)->asciiString();
221 }
222
223 ad.add("SRC", asciiString);
224
225 _eventLogger.log("org.open_power.Logging.Error.BadHostPEL",
226 Entry::Level::Error, ad);
Matt Spinlerfe721892020-04-02 10:28:08 -0500227
228 // Save it to a file for debug in the lab. Just keep the latest.
229 // Not adding it to the PEL because it could already be max size
230 // and don't want to truncate an already invalid PEL.
231 std::ofstream pelFile{getPELRepoPath() / "badPEL"};
232 pelFile.write(reinterpret_cast<const char*>(pelData.data()),
233 pelData.size());
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -0600234
235 // No need to keep around the openBMC event log entry
236 scheduleObmcLogDelete(obmcLogID);
Matt Spinler19e72902020-01-24 11:05:20 -0600237 }
238}
239
240void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
241{
242 std::vector<uint8_t> data;
243
Matt Spinler5f5352e2020-03-05 16:23:27 -0600244 log<level::DEBUG>("Adding PEL from ESEL",
245 entry("OBMC_LOG_ID=%d", obmcLogID));
246
Matt Spinler19e72902020-01-24 11:05:20 -0600247 try
248 {
249 data = std::move(eselToRawData(esel));
250 }
Patrick Williams66491c62021-10-06 12:23:37 -0500251 catch (const std::exception& e)
Matt Spinler19e72902020-01-24 11:05:20 -0600252 {
253 // Try to add it below anyway, so it follows the usual bad data path.
254 log<level::ERR>("Problems converting ESEL string to a byte vector");
255 }
256
257 addPEL(data, obmcLogID);
258}
259
260std::vector<uint8_t> Manager::eselToRawData(const std::string& esel)
261{
262 std::vector<uint8_t> data;
263 std::string byteString;
264
265 // As the eSEL string looks like: "50 48 00 ab ..." there are 3
266 // characters per raw byte, and since the actual PEL data starts
267 // at the 16th byte, the code will grab the PEL data starting at
268 // offset 48 in the string.
269 static constexpr size_t pelStart = 16 * 3;
270
271 if (esel.size() <= pelStart)
272 {
273 log<level::ERR>("ESEL data too short",
274 entry("ESEL_SIZE=%d", esel.size()));
275
276 throw std::length_error("ESEL data too short");
277 }
278
279 for (size_t i = pelStart; i < esel.size(); i += 3)
280 {
281 if (i + 1 < esel.size())
282 {
283 byteString = esel.substr(i, 2);
284 data.push_back(std::stoi(byteString, nullptr, 16));
285 }
286 else
287 {
288 log<level::ERR>("ESEL data too short",
289 entry("ESEL_SIZE=%d", esel.size()));
290 throw std::length_error("ESEL data too short");
291 }
292 }
293
294 return data;
295}
296
Matt Spinler4e8078c2019-07-09 13:22:32 -0500297void Manager::erase(uint32_t obmcLogID)
298{
Matt Spinler475e5742019-07-18 16:09:49 -0500299 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
300
Vijay Loboafb1b462021-07-21 23:29:13 -0500301 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
302 _pelEntries.erase(path);
Matt Spinler475e5742019-07-18 16:09:49 -0500303 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500304}
305
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500306bool Manager::isDeleteProhibited(uint32_t /*obmcLogID*/)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500307{
308 return false;
309}
310
Matt Spinler56ad2a02020-03-26 14:00:52 -0500311PelFFDC Manager::convertToPelFFDC(const FFDCEntries& ffdc)
312{
313 PelFFDC pelFFDC;
314
315 std::for_each(ffdc.begin(), ffdc.end(), [&pelFFDC](const auto& f) {
316 PelFFDCfile pf;
317 pf.subType = std::get<ffdcSubtypePos>(f);
318 pf.version = std::get<ffdcVersionPos>(f);
319 pf.fd = std::get<ffdcFDPos>(f);
320
321 switch (std::get<ffdcFormatPos>(f))
322 {
323 case Create::FFDCFormat::JSON:
324 pf.format = UserDataFormat::json;
325 break;
326 case Create::FFDCFormat::CBOR:
327 pf.format = UserDataFormat::cbor;
328 break;
329 case Create::FFDCFormat::Text:
330 pf.format = UserDataFormat::text;
331 break;
332 case Create::FFDCFormat::Custom:
333 pf.format = UserDataFormat::custom;
334 break;
335 }
336
337 pelFFDC.push_back(pf);
338 });
339
340 return pelFFDC;
341}
342
Matt Spinler4e8078c2019-07-09 13:22:32 -0500343void Manager::createPEL(const std::string& message, uint32_t obmcLogID,
344 uint64_t timestamp,
345 phosphor::logging::Entry::Level severity,
346 const std::vector<std::string>& additionalData,
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500347 const std::vector<std::string>& /*associations*/,
Matt Spinler56ad2a02020-03-26 14:00:52 -0500348 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500349{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800350 auto entry = _registry.lookup(message, rg::LookupType::name);
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500351 auto pelFFDC = convertToPelFFDC(ffdc);
352 AdditionalData ad{additionalData};
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600353 std::string msg;
Matt Spinler67456c22019-10-21 12:22:49 -0500354
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500355 if (!entry)
Matt Spinler67456c22019-10-21 12:22:49 -0500356 {
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500357 // Instead, get the default entry that means there is no
358 // other matching entry. This error will still use the
359 // AdditionalData values of the original error, and this
360 // code will add the error message value that wasn't found
361 // to this AD. This way, there will at least be a PEL,
362 // possibly with callouts, to allow users to debug the
363 // issue that caused the error even without its own PEL.
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600364 msg = "Event not found in PEL message registry: " + message;
365 log<level::INFO>(msg.c_str());
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500366
367 entry = _registry.lookup(defaultLogMessage, rg::LookupType::name);
368 if (!entry)
369 {
370 log<level::ERR>("Default event not found in PEL message registry");
371 return;
372 }
373
374 ad.add(additional_data::error, message);
375 }
376
377 auto pel = std::make_unique<openpower::pels::PEL>(
378 *entry, obmcLogID, timestamp, severity, ad, pelFFDC, *_dataIface);
379
380 _repo.add(pel);
381
382 if (_repo.sizeWarning())
383 {
384 scheduleRepoPrune();
385 }
386
387 auto src = pel->primarySRC();
388 if (src)
389 {
Matt Spinler22421b92020-07-17 09:41:08 -0500390 auto msg =
391 fmt::format("Created PEL {:#x} (BMC ID {}) with SRC {}", pel->id(),
392 pel->obmcLogID(), (*src)->asciiString());
Matt Spinler30ddc9f2020-07-16 15:39:59 -0500393 while (msg.back() == ' ')
394 {
395 msg.pop_back();
396 }
397 log<level::INFO>(msg.c_str());
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600398 }
Matt Spinler1962e082020-08-05 13:44:53 -0500399
Sumit Kumar3e274432021-09-14 06:37:56 -0500400 // Check for severity 0x51 and update boot progress SRC
401 updateProgressSRC(pel);
402
Matt Spinler1962e082020-08-05 13:44:53 -0500403 // Activate any resulting service indicators if necessary
404 auto policy = service_indicators::getPolicy(*_dataIface);
405 policy->activate(*pel);
Andrew Geissler44fc3162020-07-09 09:21:31 -0500406
Matt Spinler8b81ec02022-07-12 13:25:37 -0500407 updateDBusSeverity(*pel);
Vijay Lobod354a392021-06-01 16:21:02 -0500408 updateEventId(pel);
Matt Spinler28d6ae22022-03-18 11:18:27 -0500409 updateResolution(*pel);
Vijay Loboafb1b462021-07-21 23:29:13 -0500410 createPELEntry(obmcLogID);
Matt Spinlerdf5cb832022-07-12 12:47:26 -0500411
412 // Check if firmware should quiesce system due to error
413 checkPelAndQuiesce(pel);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500414}
415
Matt Spinlera34ab722019-12-16 10:39:32 -0600416sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
417{
418 Repository::LogID id{Repository::LogID::Pel(pelID)};
419 std::optional<int> fd;
420
Matt Spinler5f5352e2020-03-05 16:23:27 -0600421 log<level::DEBUG>("getPEL", entry("PEL_ID=0x%X", pelID));
422
Matt Spinlera34ab722019-12-16 10:39:32 -0600423 try
424 {
425 fd = _repo.getPELFD(id);
426 }
Patrick Williams66491c62021-10-06 12:23:37 -0500427 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600428 {
429 throw common_error::InternalFailure();
430 }
431
432 if (!fd)
433 {
434 throw common_error::InvalidArgument();
435 }
436
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600437 scheduleFDClose(*fd);
438
Matt Spinlera34ab722019-12-16 10:39:32 -0600439 return *fd;
440}
441
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600442void Manager::scheduleFDClose(int fd)
443{
444 _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500445 _event, std::bind(std::mem_fn(&Manager::closeFD), this, fd,
446 std::placeholders::_1));
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600447}
448
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500449void Manager::closeFD(int fd, sdeventplus::source::EventBase& /*source*/)
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600450{
451 close(fd);
452 _fdCloserEventSource.reset();
453}
454
Matt Spinlera34ab722019-12-16 10:39:32 -0600455std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
456{
457 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
458 std::optional<std::vector<uint8_t>> data;
459
Matt Spinler5f5352e2020-03-05 16:23:27 -0600460 log<level::DEBUG>("getPELFromOBMCID", entry("OBMC_LOG_ID=%d", obmcLogID));
461
Matt Spinlera34ab722019-12-16 10:39:32 -0600462 try
463 {
464 data = _repo.getPELData(id);
465 }
Patrick Williams66491c62021-10-06 12:23:37 -0500466 catch (const std::exception& e)
Matt Spinlera34ab722019-12-16 10:39:32 -0600467 {
468 throw common_error::InternalFailure();
469 }
470
471 if (!data)
472 {
473 throw common_error::InvalidArgument();
474 }
475
476 return *data;
477}
478
479void Manager::hostAck(uint32_t pelID)
480{
481 Repository::LogID id{Repository::LogID::Pel(pelID)};
482
Matt Spinler5f5352e2020-03-05 16:23:27 -0600483 log<level::DEBUG>("HostAck", entry("PEL_ID=0x%X", pelID));
484
Matt Spinlera34ab722019-12-16 10:39:32 -0600485 if (!_repo.hasPEL(id))
486 {
487 throw common_error::InvalidArgument();
488 }
489
490 if (_hostNotifier)
491 {
492 _hostNotifier->ackPEL(pelID);
493 }
494}
495
496void Manager::hostReject(uint32_t pelID, RejectionReason reason)
497{
498 Repository::LogID id{Repository::LogID::Pel(pelID)};
499
Matt Spinler5f5352e2020-03-05 16:23:27 -0600500 log<level::DEBUG>("HostReject", entry("PEL_ID=0x%X", pelID),
501 entry("REASON=%d", static_cast<int>(reason)));
502
Matt Spinlera34ab722019-12-16 10:39:32 -0600503 if (!_repo.hasPEL(id))
504 {
505 throw common_error::InvalidArgument();
506 }
507
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600508 if (reason == RejectionReason::BadPEL)
Matt Spinlera34ab722019-12-16 10:39:32 -0600509 {
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600510 AdditionalData data;
511 data.add("BAD_ID", getNumberString("0x%08X", pelID));
512 _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
513 Entry::Level::Informational, data);
514 if (_hostNotifier)
Matt Spinlera34ab722019-12-16 10:39:32 -0600515 {
516 _hostNotifier->setBadPEL(pelID);
517 }
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600518 }
519 else if ((reason == RejectionReason::HostFull) && _hostNotifier)
520 {
521 _hostNotifier->setHostFull(pelID);
Matt Spinlera34ab722019-12-16 10:39:32 -0600522 }
523}
524
Matt Spinler7e727a32020-07-07 15:00:17 -0500525void Manager::scheduleRepoPrune()
526{
Matt Spinler7e727a32020-07-07 15:00:17 -0500527 _repoPrunerEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinlerff9cec22020-07-15 13:06:35 -0500528 _event, std::bind(std::mem_fn(&Manager::pruneRepo), this,
529 std::placeholders::_1));
Matt Spinler7e727a32020-07-07 15:00:17 -0500530}
531
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500532void Manager::pruneRepo(sdeventplus::source::EventBase& /*source*/)
Matt Spinler7e727a32020-07-07 15:00:17 -0500533{
Sumit Kumar027bf282022-01-24 11:25:19 -0600534 auto idsWithHwIsoEntry = _dataIface->getLogIDWithHwIsolation();
535
536 auto idsToDelete = _repo.prune(idsWithHwIsoEntry);
Matt Spinler7e727a32020-07-07 15:00:17 -0500537
538 // Remove the OpenBMC event logs for the PELs that were just removed.
539 std::for_each(idsToDelete.begin(), idsToDelete.end(),
540 [this](auto id) { this->_logManager.erase(id); });
541
542 _repoPrunerEventSource.reset();
543}
544
Matt Spinlerff9cec22020-07-15 13:06:35 -0500545void Manager::setupPELDeleteWatch()
546{
547 _pelFileDeleteFD = inotify_init1(IN_NONBLOCK);
548 if (-1 == _pelFileDeleteFD)
549 {
550 auto e = errno;
551 std::string msg =
552 "inotify_init1 failed with errno " + std::to_string(e);
553 log<level::ERR>(msg.c_str());
554 abort();
555 }
556
557 _pelFileDeleteWatchFD = inotify_add_watch(
558 _pelFileDeleteFD, _repo.repoPath().c_str(), IN_DELETE);
559 if (-1 == _pelFileDeleteWatchFD)
560 {
561 auto e = errno;
562 std::string msg =
563 "inotify_add_watch failed with error " + std::to_string(e);
564 log<level::ERR>(msg.c_str());
565 abort();
566 }
567
568 _pelFileDeleteEventSource = std::make_unique<sdeventplus::source::IO>(
569 _event, _pelFileDeleteFD, EPOLLIN,
570 std::bind(std::mem_fn(&Manager::pelFileDeleted), this,
571 std::placeholders::_1, std::placeholders::_2,
572 std::placeholders::_3));
573}
574
Patrick Williamsd26fa3e2021-04-21 15:22:23 -0500575void Manager::pelFileDeleted(sdeventplus::source::IO& /*io*/, int /*fd*/,
Matt Spinlerff9cec22020-07-15 13:06:35 -0500576 uint32_t revents)
577{
578 if (!(revents & EPOLLIN))
579 {
580 return;
581 }
582
583 // An event for 1 PEL uses 48B. When all PELs are deleted at once,
584 // as many events as there is room for can be handled in one callback.
585 // A size of 2000 will allow 41 to be processed, with additional
586 // callbacks being needed to process the remaining ones.
Matt Spinler9d59d582021-05-19 07:57:10 -0600587 std::array<uint8_t, 2000> data{};
Matt Spinlerff9cec22020-07-15 13:06:35 -0500588 auto bytesRead = read(_pelFileDeleteFD, data.data(), data.size());
589 if (bytesRead < 0)
590 {
591 auto e = errno;
592 std::string msg = "Failed reading data from inotify event, errno = " +
593 std::to_string(e);
594 log<level::ERR>(msg.c_str());
595 abort();
596 }
597
598 auto offset = 0;
599 while (offset < bytesRead)
600 {
601 auto event = reinterpret_cast<inotify_event*>(&data[offset]);
602 if (event->mask & IN_DELETE)
603 {
604 std::string filename{event->name};
605
606 // Get the PEL ID from the filename and tell the
607 // repo it's been removed, and then delete the BMC
608 // event log if it's there.
609 auto pos = filename.find_first_of('_');
610 if (pos != std::string::npos)
611 {
612 try
613 {
614 auto idString = filename.substr(pos + 1);
615 auto pelID = std::stoul(idString, nullptr, 16);
616
617 Repository::LogID id{Repository::LogID::Pel(pelID)};
618 auto removedLogID = _repo.remove(id);
619 if (removedLogID)
620 {
621 _logManager.erase(removedLogID->obmcID.id);
622 }
623 }
624 catch (const std::exception& e)
625 {
626 log<level::INFO>("Could not find PEL ID from its filename",
627 entry("FILENAME=%s", filename.c_str()));
628 }
629 }
630 }
631
632 offset += offsetof(inotify_event, name) + event->len;
633 }
634}
Matt Spinler9cc30072020-09-16 15:39:34 -0500635
636std::tuple<uint32_t, uint32_t> Manager::createPELWithFFDCFiles(
637 std::string message, Entry::Level severity,
638 std::map<std::string, std::string> additionalData,
639 std::vector<std::tuple<
640 sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat,
641 uint8_t, uint8_t, sdbusplus::message::unix_fd>>
642 fFDC)
643{
Matt Spinler44893cc2020-08-26 11:34:17 -0500644 _logManager.createWithFFDC(message, severity, additionalData, fFDC);
645
646 return {_logManager.lastEntryID(), _repo.lastPelID()};
Matt Spinler9cc30072020-09-16 15:39:34 -0500647}
648
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500649std::string Manager::getPELJSON(uint32_t obmcLogID)
Matt Spinleraa85a072022-03-23 11:26:41 -0500650{
Matt Spinler8bd4ca42022-04-01 16:06:06 -0500651 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
652
653 // 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())
789 resolution +=
790 "Location Code: " + entry->locationCode() + ", ";
791 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 {
862 auto newSeverity =
863 fixupLogSeverity(entryN->second->severity(), sevType);
864 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 {
Vijay Loboafb1b462021-07-21 23:29:13 -0500903 entry->second->serviceProviderNotify(
Matt Spinler734ed2b2022-01-21 09:31:46 -0600904 attr.actionFlags.test(callHomeFlagBit), true);
Vijay Lobocbc93a42021-05-20 19:04:07 -0500905 }
906 }
907}
908
Matt Spinler734ed2b2022-01-21 09:31:46 -0600909void Manager::createPELEntry(uint32_t obmcLogID, bool skipIaSignal)
Vijay Loboafb1b462021-07-21 23:29:13 -0500910{
911 std::map<std::string, PropertiesVariant> varData;
912 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
913 if (auto attributes = _repo.getPELAttributes(id); attributes)
914 {
915 namespace pv = openpower::pels::pel_values;
916 auto& attr = attributes.value().get();
Vijay Lobob2e541e2021-08-31 23:12:47 -0500917
918 // get the hidden flag values
919 auto sevType = static_cast<SeverityType>(attr.severity & 0xF0);
920 auto isHidden = true;
921 if (((sevType != SeverityType::nonError) &&
922 attr.actionFlags.test(reportFlagBit) &&
923 !attr.actionFlags.test(hiddenFlagBit)) ||
924 ((sevType == SeverityType::nonError) &&
925 attr.actionFlags.test(serviceActionFlagBit)))
926 {
927 isHidden = false;
928 }
929 varData.emplace(std::string("Hidden"), isHidden);
Vijay Loboafb1b462021-07-21 23:29:13 -0500930 varData.emplace(
931 std::string("Subsystem"),
932 pv::getValue(attr.subsystem, pel_values::subsystemValues));
Vijay Lobo2fb10212021-08-22 23:24:16 -0500933
934 varData.emplace(
935 std::string("ManagementSystemAck"),
936 (attr.hmcState == TransmissionState::acked ? true : false));
937
Vijay Loboafb1b462021-07-21 23:29:13 -0500938 // Path to create PELEntry Interface is same as PEL
939 auto path = std::string(OBJ_ENTRY) + '/' + std::to_string(obmcLogID);
940 // Create Interface for PELEntry and set properties
Vijay Lobo2fb10212021-08-22 23:24:16 -0500941 auto pelEntry = std::make_unique<PELEntry>(_logManager.getBus(), path,
942 varData, obmcLogID, &_repo);
Matt Spinler734ed2b2022-01-21 09:31:46 -0600943 if (!skipIaSignal)
944 {
945 pelEntry->emit_added();
946 }
Vijay Loboafb1b462021-07-21 23:29:13 -0500947 _pelEntries.emplace(std::move(path), std::move(pelEntry));
948 }
949}
950
Ramesh Iyyarf4203c42021-06-24 06:09:23 -0500951uint32_t Manager::getPELIdFromBMCLogId(uint32_t bmcLogId)
952{
953 Repository::LogID id{Repository::LogID::Obmc(bmcLogId)};
954 if (auto logId = _repo.getLogID(id); !logId.has_value())
955 {
956 throw common_error::InvalidArgument();
957 }
958 else
959 {
960 return logId->pelID.id;
961 }
962}
963
Ramesh Iyyar530efbf2021-06-24 06:22:22 -0500964uint32_t Manager::getBMCLogIdFromPELId(uint32_t pelId)
965{
966 Repository::LogID id{Repository::LogID::Pel(pelId)};
967 if (auto logId = _repo.getLogID(id); !logId.has_value())
968 {
969 throw common_error::InvalidArgument();
970 }
971 else
972 {
973 return logId->obmcID.id;
974 }
975}
976
Sumit Kumar3e274432021-09-14 06:37:56 -0500977void Manager::updateProgressSRC(
978 std::unique_ptr<openpower::pels::PEL>& pel) const
979{
980 // Check for pel severity of type - 0x51 = critical error, system
981 // termination
982 if (pel->userHeader().severity() == 0x51)
983 {
984 auto src = pel->primarySRC();
985 if (src)
986 {
987 std::vector<uint8_t> asciiSRC = (*src)->getSrcStruct();
988 uint64_t srcRefCode = 0;
989
990 // Read bytes from offset [40-47] e.g. BD8D1001
991 for (int i = 0; i < 8; i++)
992 {
993 srcRefCode |=
994 (static_cast<uint64_t>(asciiSRC[40 + i]) << (8 * i));
995 }
996
997 try
998 {
999 _dataIface->createProgressSRC(srcRefCode, asciiSRC);
1000 }
1001 catch (std::exception& e)
1002 {
1003 // Exception - may be no boot progress interface on dbus
1004 }
1005 }
1006 }
1007}
1008
Matt Spinlerd8fb5ba2022-01-25 13:01:14 -06001009void Manager::scheduleObmcLogDelete(uint32_t obmcLogID)
1010{
1011 _obmcLogDeleteEventSource = std::make_unique<sdeventplus::source::Defer>(
1012 _event, std::bind(std::mem_fn(&Manager::deleteObmcLog), this,
1013 std::placeholders::_1, obmcLogID));
1014}
1015
1016void Manager::deleteObmcLog(sdeventplus::source::EventBase&, uint32_t obmcLogID)
1017{
1018 log<level::INFO>(
1019 fmt::format("Removing event log with no PEL: {}", obmcLogID).c_str());
1020 _logManager.erase(obmcLogID);
1021 _obmcLogDeleteEventSource.reset();
1022}
1023
Matt Spinler4e8078c2019-07-09 13:22:32 -05001024} // namespace pels
1025} // namespace openpower