blob: 66f5ee532850340edf2066cb986138851712a341 [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"
21
Matt Spinler6b1a5c82020-01-07 08:48:53 -060022#include <unistd.h>
23
Matt Spinler89fa0822019-07-17 13:54:30 -050024#include <filesystem>
25#include <fstream>
Matt Spinlera34ab722019-12-16 10:39:32 -060026#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler56ad2a02020-03-26 14:00:52 -050027#include <xyz/openbmc_project/Logging/Create/server.hpp>
Matt Spinler4e8078c2019-07-09 13:22:32 -050028
29namespace openpower
30{
31namespace pels
32{
33
34using namespace phosphor::logging;
Matt Spinler89fa0822019-07-17 13:54:30 -050035namespace fs = std::filesystem;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080036namespace rg = openpower::pels::message;
Matt Spinler4e8078c2019-07-09 13:22:32 -050037
Matt Spinlera34ab722019-12-16 10:39:32 -060038namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
39
Matt Spinler56ad2a02020-03-26 14:00:52 -050040using Create = sdbusplus::xyz::openbmc_project::Logging::server::Create;
41
Matt Spinler4e8078c2019-07-09 13:22:32 -050042namespace additional_data
43{
44constexpr auto rawPEL = "RAWPEL";
Matt Spinler19e72902020-01-24 11:05:20 -060045constexpr auto esel = "ESEL";
46} // namespace additional_data
Matt Spinler4e8078c2019-07-09 13:22:32 -050047
48void Manager::create(const std::string& message, uint32_t obmcLogID,
49 uint64_t timestamp, Entry::Level severity,
50 const std::vector<std::string>& additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050051 const std::vector<std::string>& associations,
52 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -050053{
54 AdditionalData ad{additionalData};
55
Matt Spinler19e72902020-01-24 11:05:20 -060056 // If a PEL was passed in via a filename or in an ESEL,
57 // use that. Otherwise, create one.
Matt Spinler4e8078c2019-07-09 13:22:32 -050058 auto rawPelPath = ad.getValue(additional_data::rawPEL);
59 if (rawPelPath)
60 {
61 addRawPEL(*rawPelPath, obmcLogID);
62 }
63 else
64 {
Matt Spinler19e72902020-01-24 11:05:20 -060065 auto esel = ad.getValue(additional_data::esel);
66 if (esel)
67 {
68 addESELPEL(*esel, obmcLogID);
69 }
70 else
71 {
72 createPEL(message, obmcLogID, timestamp, severity, additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -050073 associations, ffdc);
Matt Spinler19e72902020-01-24 11:05:20 -060074 }
Matt Spinler4e8078c2019-07-09 13:22:32 -050075 }
76}
77
78void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
79{
Matt Spinler89fa0822019-07-17 13:54:30 -050080 if (fs::exists(rawPelPath))
81 {
82 std::ifstream file(rawPelPath, std::ios::in | std::ios::binary);
83
84 auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file),
85 std::istreambuf_iterator<char>());
86 if (file.fail())
87 {
88 log<level::ERR>("Filesystem error reading a raw PEL",
89 entry("PELFILE=%s", rawPelPath.c_str()),
90 entry("OBMCLOGID=%d", obmcLogID));
91 // TODO, Decide what to do here. Maybe nothing.
92 return;
93 }
94
95 file.close();
96
Matt Spinler19e72902020-01-24 11:05:20 -060097 addPEL(data, obmcLogID);
Matt Spinler89fa0822019-07-17 13:54:30 -050098 }
99 else
100 {
101 log<level::ERR>("Raw PEL file from BMC event log does not exist",
102 entry("PELFILE=%s", (rawPelPath).c_str()),
103 entry("OBMCLOGID=%d", obmcLogID));
104 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500105}
106
Matt Spinler19e72902020-01-24 11:05:20 -0600107void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID)
108{
109
110 auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID);
111 if (pel->valid())
112 {
113 // PELs created by others still need these fields set by us.
114 pel->assignID();
115 pel->setCommitTime();
116
117 try
118 {
Matt Spinler5f5352e2020-03-05 16:23:27 -0600119 log<level::DEBUG>("Adding external PEL to repo",
120 entry("PEL_ID=0x%X", pel->id()));
121
Matt Spinler19e72902020-01-24 11:05:20 -0600122 _repo.add(pel);
123 }
124 catch (std::exception& e)
125 {
126 // Probably a full or r/o filesystem, not much we can do.
127 log<level::ERR>("Unable to add PEL to Repository",
128 entry("PEL_ID=0x%X", pel->id()));
129 }
130 }
131 else
132 {
133 log<level::ERR>("Invalid PEL received from the host",
134 entry("OBMCLOGID=%d", obmcLogID));
135
136 AdditionalData ad;
137 ad.add("PLID", getNumberString("0x%08X", pel->plid()));
138 ad.add("OBMC_LOG_ID", std::to_string(obmcLogID));
139 ad.add("PEL_SIZE", std::to_string(pelData.size()));
140
141 std::string asciiString;
142 auto src = pel->primarySRC();
143 if (src)
144 {
145 asciiString = (*src)->asciiString();
146 }
147
148 ad.add("SRC", asciiString);
149
150 _eventLogger.log("org.open_power.Logging.Error.BadHostPEL",
151 Entry::Level::Error, ad);
Matt Spinlerfe721892020-04-02 10:28:08 -0500152
153 // Save it to a file for debug in the lab. Just keep the latest.
154 // Not adding it to the PEL because it could already be max size
155 // and don't want to truncate an already invalid PEL.
156 std::ofstream pelFile{getPELRepoPath() / "badPEL"};
157 pelFile.write(reinterpret_cast<const char*>(pelData.data()),
158 pelData.size());
Matt Spinler19e72902020-01-24 11:05:20 -0600159 }
160}
161
162void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
163{
164 std::vector<uint8_t> data;
165
Matt Spinler5f5352e2020-03-05 16:23:27 -0600166 log<level::DEBUG>("Adding PEL from ESEL",
167 entry("OBMC_LOG_ID=%d", obmcLogID));
168
Matt Spinler19e72902020-01-24 11:05:20 -0600169 try
170 {
171 data = std::move(eselToRawData(esel));
172 }
173 catch (std::exception& e)
174 {
175 // Try to add it below anyway, so it follows the usual bad data path.
176 log<level::ERR>("Problems converting ESEL string to a byte vector");
177 }
178
179 addPEL(data, obmcLogID);
180}
181
182std::vector<uint8_t> Manager::eselToRawData(const std::string& esel)
183{
184 std::vector<uint8_t> data;
185 std::string byteString;
186
187 // As the eSEL string looks like: "50 48 00 ab ..." there are 3
188 // characters per raw byte, and since the actual PEL data starts
189 // at the 16th byte, the code will grab the PEL data starting at
190 // offset 48 in the string.
191 static constexpr size_t pelStart = 16 * 3;
192
193 if (esel.size() <= pelStart)
194 {
195 log<level::ERR>("ESEL data too short",
196 entry("ESEL_SIZE=%d", esel.size()));
197
198 throw std::length_error("ESEL data too short");
199 }
200
201 for (size_t i = pelStart; i < esel.size(); i += 3)
202 {
203 if (i + 1 < esel.size())
204 {
205 byteString = esel.substr(i, 2);
206 data.push_back(std::stoi(byteString, nullptr, 16));
207 }
208 else
209 {
210 log<level::ERR>("ESEL data too short",
211 entry("ESEL_SIZE=%d", esel.size()));
212 throw std::length_error("ESEL data too short");
213 }
214 }
215
216 return data;
217}
218
Matt Spinler4e8078c2019-07-09 13:22:32 -0500219void Manager::erase(uint32_t obmcLogID)
220{
Matt Spinler475e5742019-07-18 16:09:49 -0500221 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
222
223 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500224}
225
226bool Manager::isDeleteProhibited(uint32_t obmcLogID)
227{
228 return false;
229}
230
Matt Spinler56ad2a02020-03-26 14:00:52 -0500231PelFFDC Manager::convertToPelFFDC(const FFDCEntries& ffdc)
232{
233 PelFFDC pelFFDC;
234
235 std::for_each(ffdc.begin(), ffdc.end(), [&pelFFDC](const auto& f) {
236 PelFFDCfile pf;
237 pf.subType = std::get<ffdcSubtypePos>(f);
238 pf.version = std::get<ffdcVersionPos>(f);
239 pf.fd = std::get<ffdcFDPos>(f);
240
241 switch (std::get<ffdcFormatPos>(f))
242 {
243 case Create::FFDCFormat::JSON:
244 pf.format = UserDataFormat::json;
245 break;
246 case Create::FFDCFormat::CBOR:
247 pf.format = UserDataFormat::cbor;
248 break;
249 case Create::FFDCFormat::Text:
250 pf.format = UserDataFormat::text;
251 break;
252 case Create::FFDCFormat::Custom:
253 pf.format = UserDataFormat::custom;
254 break;
255 }
256
257 pelFFDC.push_back(pf);
258 });
259
260 return pelFFDC;
261}
262
Matt Spinler4e8078c2019-07-09 13:22:32 -0500263void Manager::createPEL(const std::string& message, uint32_t obmcLogID,
264 uint64_t timestamp,
265 phosphor::logging::Entry::Level severity,
266 const std::vector<std::string>& additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -0500267 const std::vector<std::string>& associations,
268 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500269{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800270 auto entry = _registry.lookup(message, rg::LookupType::name);
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600271 std::string msg;
Matt Spinler67456c22019-10-21 12:22:49 -0500272
273 if (entry)
274 {
275 AdditionalData ad{additionalData};
276
Matt Spinler56ad2a02020-03-26 14:00:52 -0500277 auto pelFFDC = convertToPelFFDC(ffdc);
278
Matt Spinlera34ab722019-12-16 10:39:32 -0600279 auto pel = std::make_unique<openpower::pels::PEL>(
Matt Spinler56ad2a02020-03-26 14:00:52 -0500280 *entry, obmcLogID, timestamp, severity, ad, pelFFDC, *_dataIface);
Matt Spinler67456c22019-10-21 12:22:49 -0500281
282 _repo.add(pel);
Matt Spinler67456c22019-10-21 12:22:49 -0500283
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600284 auto src = pel->primarySRC();
285 if (src)
286 {
287 using namespace std::literals::string_literals;
Matt Spinler19e72902020-01-24 11:05:20 -0600288 auto id = getNumberString("0x%08X", pel->id());
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600289 msg = "Created PEL "s + id + " with SRC "s + (*src)->asciiString();
290 while (msg.back() == ' ')
291 {
292 msg.pop_back();
293 }
294 log<level::INFO>(msg.c_str());
295 }
296 }
297 else
298 {
299 // TODO ibm-openbmc/dev/1151: Create a new PEL for this case.
300 // For now, just trace it.
301 msg = "Event not found in PEL message registry: " + message;
302 log<level::INFO>(msg.c_str());
303 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500304}
305
Matt Spinlera34ab722019-12-16 10:39:32 -0600306sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
307{
308 Repository::LogID id{Repository::LogID::Pel(pelID)};
309 std::optional<int> fd;
310
Matt Spinler5f5352e2020-03-05 16:23:27 -0600311 log<level::DEBUG>("getPEL", entry("PEL_ID=0x%X", pelID));
312
Matt Spinlera34ab722019-12-16 10:39:32 -0600313 try
314 {
315 fd = _repo.getPELFD(id);
316 }
317 catch (std::exception& e)
318 {
319 throw common_error::InternalFailure();
320 }
321
322 if (!fd)
323 {
324 throw common_error::InvalidArgument();
325 }
326
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600327 scheduleFDClose(*fd);
328
Matt Spinlera34ab722019-12-16 10:39:32 -0600329 return *fd;
330}
331
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600332void Manager::scheduleFDClose(int fd)
333{
Matt Spinler104e9362020-04-02 09:34:41 -0500334 sdeventplus::Event event = sdeventplus::Event::get_default();
335
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600336 _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinler104e9362020-04-02 09:34:41 -0500337 event, std::bind(std::mem_fn(&Manager::closeFD), this, fd,
338 std::placeholders::_1));
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600339}
340
341void Manager::closeFD(int fd, sdeventplus::source::EventBase& source)
342{
343 close(fd);
344 _fdCloserEventSource.reset();
345}
346
Matt Spinlera34ab722019-12-16 10:39:32 -0600347std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
348{
349 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
350 std::optional<std::vector<uint8_t>> data;
351
Matt Spinler5f5352e2020-03-05 16:23:27 -0600352 log<level::DEBUG>("getPELFromOBMCID", entry("OBMC_LOG_ID=%d", obmcLogID));
353
Matt Spinlera34ab722019-12-16 10:39:32 -0600354 try
355 {
356 data = _repo.getPELData(id);
357 }
358 catch (std::exception& e)
359 {
360 throw common_error::InternalFailure();
361 }
362
363 if (!data)
364 {
365 throw common_error::InvalidArgument();
366 }
367
368 return *data;
369}
370
371void Manager::hostAck(uint32_t pelID)
372{
373 Repository::LogID id{Repository::LogID::Pel(pelID)};
374
Matt Spinler5f5352e2020-03-05 16:23:27 -0600375 log<level::DEBUG>("HostAck", entry("PEL_ID=0x%X", pelID));
376
Matt Spinlera34ab722019-12-16 10:39:32 -0600377 if (!_repo.hasPEL(id))
378 {
379 throw common_error::InvalidArgument();
380 }
381
382 if (_hostNotifier)
383 {
384 _hostNotifier->ackPEL(pelID);
385 }
386}
387
388void Manager::hostReject(uint32_t pelID, RejectionReason reason)
389{
390 Repository::LogID id{Repository::LogID::Pel(pelID)};
391
Matt Spinler5f5352e2020-03-05 16:23:27 -0600392 log<level::DEBUG>("HostReject", entry("PEL_ID=0x%X", pelID),
393 entry("REASON=%d", static_cast<int>(reason)));
394
Matt Spinlera34ab722019-12-16 10:39:32 -0600395 if (!_repo.hasPEL(id))
396 {
397 throw common_error::InvalidArgument();
398 }
399
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600400 if (reason == RejectionReason::BadPEL)
Matt Spinlera34ab722019-12-16 10:39:32 -0600401 {
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600402 AdditionalData data;
403 data.add("BAD_ID", getNumberString("0x%08X", pelID));
404 _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
405 Entry::Level::Informational, data);
406 if (_hostNotifier)
Matt Spinlera34ab722019-12-16 10:39:32 -0600407 {
408 _hostNotifier->setBadPEL(pelID);
409 }
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600410 }
411 else if ((reason == RejectionReason::HostFull) && _hostNotifier)
412 {
413 _hostNotifier->setHostFull(pelID);
Matt Spinlera34ab722019-12-16 10:39:32 -0600414 }
415}
416
Matt Spinler4e8078c2019-07-09 13:22:32 -0500417} // namespace pels
418} // namespace openpower