blob: fb0efb30ab54588474508749355a413e1ed7ced5 [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);
152 }
153}
154
155void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
156{
157 std::vector<uint8_t> data;
158
Matt Spinler5f5352e2020-03-05 16:23:27 -0600159 log<level::DEBUG>("Adding PEL from ESEL",
160 entry("OBMC_LOG_ID=%d", obmcLogID));
161
Matt Spinler19e72902020-01-24 11:05:20 -0600162 try
163 {
164 data = std::move(eselToRawData(esel));
165 }
166 catch (std::exception& e)
167 {
168 // Try to add it below anyway, so it follows the usual bad data path.
169 log<level::ERR>("Problems converting ESEL string to a byte vector");
170 }
171
172 addPEL(data, obmcLogID);
173}
174
175std::vector<uint8_t> Manager::eselToRawData(const std::string& esel)
176{
177 std::vector<uint8_t> data;
178 std::string byteString;
179
180 // As the eSEL string looks like: "50 48 00 ab ..." there are 3
181 // characters per raw byte, and since the actual PEL data starts
182 // at the 16th byte, the code will grab the PEL data starting at
183 // offset 48 in the string.
184 static constexpr size_t pelStart = 16 * 3;
185
186 if (esel.size() <= pelStart)
187 {
188 log<level::ERR>("ESEL data too short",
189 entry("ESEL_SIZE=%d", esel.size()));
190
191 throw std::length_error("ESEL data too short");
192 }
193
194 for (size_t i = pelStart; i < esel.size(); i += 3)
195 {
196 if (i + 1 < esel.size())
197 {
198 byteString = esel.substr(i, 2);
199 data.push_back(std::stoi(byteString, nullptr, 16));
200 }
201 else
202 {
203 log<level::ERR>("ESEL data too short",
204 entry("ESEL_SIZE=%d", esel.size()));
205 throw std::length_error("ESEL data too short");
206 }
207 }
208
209 return data;
210}
211
Matt Spinler4e8078c2019-07-09 13:22:32 -0500212void Manager::erase(uint32_t obmcLogID)
213{
Matt Spinler475e5742019-07-18 16:09:49 -0500214 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
215
216 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500217}
218
219bool Manager::isDeleteProhibited(uint32_t obmcLogID)
220{
221 return false;
222}
223
Matt Spinler56ad2a02020-03-26 14:00:52 -0500224PelFFDC Manager::convertToPelFFDC(const FFDCEntries& ffdc)
225{
226 PelFFDC pelFFDC;
227
228 std::for_each(ffdc.begin(), ffdc.end(), [&pelFFDC](const auto& f) {
229 PelFFDCfile pf;
230 pf.subType = std::get<ffdcSubtypePos>(f);
231 pf.version = std::get<ffdcVersionPos>(f);
232 pf.fd = std::get<ffdcFDPos>(f);
233
234 switch (std::get<ffdcFormatPos>(f))
235 {
236 case Create::FFDCFormat::JSON:
237 pf.format = UserDataFormat::json;
238 break;
239 case Create::FFDCFormat::CBOR:
240 pf.format = UserDataFormat::cbor;
241 break;
242 case Create::FFDCFormat::Text:
243 pf.format = UserDataFormat::text;
244 break;
245 case Create::FFDCFormat::Custom:
246 pf.format = UserDataFormat::custom;
247 break;
248 }
249
250 pelFFDC.push_back(pf);
251 });
252
253 return pelFFDC;
254}
255
Matt Spinler4e8078c2019-07-09 13:22:32 -0500256void Manager::createPEL(const std::string& message, uint32_t obmcLogID,
257 uint64_t timestamp,
258 phosphor::logging::Entry::Level severity,
259 const std::vector<std::string>& additionalData,
Matt Spinler56ad2a02020-03-26 14:00:52 -0500260 const std::vector<std::string>& associations,
261 const FFDCEntries& ffdc)
Matt Spinler4e8078c2019-07-09 13:22:32 -0500262{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800263 auto entry = _registry.lookup(message, rg::LookupType::name);
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600264 std::string msg;
Matt Spinler67456c22019-10-21 12:22:49 -0500265
266 if (entry)
267 {
268 AdditionalData ad{additionalData};
269
Matt Spinler56ad2a02020-03-26 14:00:52 -0500270 auto pelFFDC = convertToPelFFDC(ffdc);
271
Matt Spinlera34ab722019-12-16 10:39:32 -0600272 auto pel = std::make_unique<openpower::pels::PEL>(
Matt Spinler56ad2a02020-03-26 14:00:52 -0500273 *entry, obmcLogID, timestamp, severity, ad, pelFFDC, *_dataIface);
Matt Spinler67456c22019-10-21 12:22:49 -0500274
275 _repo.add(pel);
Matt Spinler67456c22019-10-21 12:22:49 -0500276
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600277 auto src = pel->primarySRC();
278 if (src)
279 {
280 using namespace std::literals::string_literals;
Matt Spinler19e72902020-01-24 11:05:20 -0600281 auto id = getNumberString("0x%08X", pel->id());
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600282 msg = "Created PEL "s + id + " with SRC "s + (*src)->asciiString();
283 while (msg.back() == ' ')
284 {
285 msg.pop_back();
286 }
287 log<level::INFO>(msg.c_str());
288 }
289 }
290 else
291 {
292 // TODO ibm-openbmc/dev/1151: Create a new PEL for this case.
293 // For now, just trace it.
294 msg = "Event not found in PEL message registry: " + message;
295 log<level::INFO>(msg.c_str());
296 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500297}
298
Matt Spinlera34ab722019-12-16 10:39:32 -0600299sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
300{
301 Repository::LogID id{Repository::LogID::Pel(pelID)};
302 std::optional<int> fd;
303
Matt Spinler5f5352e2020-03-05 16:23:27 -0600304 log<level::DEBUG>("getPEL", entry("PEL_ID=0x%X", pelID));
305
Matt Spinlera34ab722019-12-16 10:39:32 -0600306 try
307 {
308 fd = _repo.getPELFD(id);
309 }
310 catch (std::exception& e)
311 {
312 throw common_error::InternalFailure();
313 }
314
315 if (!fd)
316 {
317 throw common_error::InvalidArgument();
318 }
319
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600320 scheduleFDClose(*fd);
321
Matt Spinlera34ab722019-12-16 10:39:32 -0600322 return *fd;
323}
324
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600325void Manager::scheduleFDClose(int fd)
326{
Matt Spinler104e9362020-04-02 09:34:41 -0500327 sdeventplus::Event event = sdeventplus::Event::get_default();
328
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600329 _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
Matt Spinler104e9362020-04-02 09:34:41 -0500330 event, std::bind(std::mem_fn(&Manager::closeFD), this, fd,
331 std::placeholders::_1));
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600332}
333
334void Manager::closeFD(int fd, sdeventplus::source::EventBase& source)
335{
336 close(fd);
337 _fdCloserEventSource.reset();
338}
339
Matt Spinlera34ab722019-12-16 10:39:32 -0600340std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
341{
342 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
343 std::optional<std::vector<uint8_t>> data;
344
Matt Spinler5f5352e2020-03-05 16:23:27 -0600345 log<level::DEBUG>("getPELFromOBMCID", entry("OBMC_LOG_ID=%d", obmcLogID));
346
Matt Spinlera34ab722019-12-16 10:39:32 -0600347 try
348 {
349 data = _repo.getPELData(id);
350 }
351 catch (std::exception& e)
352 {
353 throw common_error::InternalFailure();
354 }
355
356 if (!data)
357 {
358 throw common_error::InvalidArgument();
359 }
360
361 return *data;
362}
363
364void Manager::hostAck(uint32_t pelID)
365{
366 Repository::LogID id{Repository::LogID::Pel(pelID)};
367
Matt Spinler5f5352e2020-03-05 16:23:27 -0600368 log<level::DEBUG>("HostAck", entry("PEL_ID=0x%X", pelID));
369
Matt Spinlera34ab722019-12-16 10:39:32 -0600370 if (!_repo.hasPEL(id))
371 {
372 throw common_error::InvalidArgument();
373 }
374
375 if (_hostNotifier)
376 {
377 _hostNotifier->ackPEL(pelID);
378 }
379}
380
381void Manager::hostReject(uint32_t pelID, RejectionReason reason)
382{
383 Repository::LogID id{Repository::LogID::Pel(pelID)};
384
Matt Spinler5f5352e2020-03-05 16:23:27 -0600385 log<level::DEBUG>("HostReject", entry("PEL_ID=0x%X", pelID),
386 entry("REASON=%d", static_cast<int>(reason)));
387
Matt Spinlera34ab722019-12-16 10:39:32 -0600388 if (!_repo.hasPEL(id))
389 {
390 throw common_error::InvalidArgument();
391 }
392
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600393 if (reason == RejectionReason::BadPEL)
Matt Spinlera34ab722019-12-16 10:39:32 -0600394 {
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600395 AdditionalData data;
396 data.add("BAD_ID", getNumberString("0x%08X", pelID));
397 _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
398 Entry::Level::Informational, data);
399 if (_hostNotifier)
Matt Spinlera34ab722019-12-16 10:39:32 -0600400 {
401 _hostNotifier->setBadPEL(pelID);
402 }
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600403 }
404 else if ((reason == RejectionReason::HostFull) && _hostNotifier)
405 {
406 _hostNotifier->setHostFull(pelID);
Matt Spinlera34ab722019-12-16 10:39:32 -0600407 }
408}
409
Matt Spinler4e8078c2019-07-09 13:22:32 -0500410} // namespace pels
411} // namespace openpower