blob: 4eb67721c545a7a973936618df7a76851f0290be [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 Spinler4e8078c2019-07-09 13:22:32 -050027
28namespace openpower
29{
30namespace pels
31{
32
33using namespace phosphor::logging;
Matt Spinler89fa0822019-07-17 13:54:30 -050034namespace fs = std::filesystem;
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080035namespace rg = openpower::pels::message;
Matt Spinler4e8078c2019-07-09 13:22:32 -050036
Matt Spinlera34ab722019-12-16 10:39:32 -060037namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
38
Matt Spinler4e8078c2019-07-09 13:22:32 -050039namespace additional_data
40{
41constexpr auto rawPEL = "RAWPEL";
Matt Spinler19e72902020-01-24 11:05:20 -060042constexpr auto esel = "ESEL";
43} // namespace additional_data
Matt Spinler4e8078c2019-07-09 13:22:32 -050044
45void Manager::create(const std::string& message, uint32_t obmcLogID,
46 uint64_t timestamp, Entry::Level severity,
47 const std::vector<std::string>& additionalData,
48 const std::vector<std::string>& associations)
49{
50 AdditionalData ad{additionalData};
51
Matt Spinler19e72902020-01-24 11:05:20 -060052 // If a PEL was passed in via a filename or in an ESEL,
53 // use that. Otherwise, create one.
Matt Spinler4e8078c2019-07-09 13:22:32 -050054 auto rawPelPath = ad.getValue(additional_data::rawPEL);
55 if (rawPelPath)
56 {
57 addRawPEL(*rawPelPath, obmcLogID);
58 }
59 else
60 {
Matt Spinler19e72902020-01-24 11:05:20 -060061 auto esel = ad.getValue(additional_data::esel);
62 if (esel)
63 {
64 addESELPEL(*esel, obmcLogID);
65 }
66 else
67 {
68 createPEL(message, obmcLogID, timestamp, severity, additionalData,
69 associations);
70 }
Matt Spinler4e8078c2019-07-09 13:22:32 -050071 }
72}
73
74void Manager::addRawPEL(const std::string& rawPelPath, uint32_t obmcLogID)
75{
Matt Spinler89fa0822019-07-17 13:54:30 -050076 if (fs::exists(rawPelPath))
77 {
78 std::ifstream file(rawPelPath, std::ios::in | std::ios::binary);
79
80 auto data = std::vector<uint8_t>(std::istreambuf_iterator<char>(file),
81 std::istreambuf_iterator<char>());
82 if (file.fail())
83 {
84 log<level::ERR>("Filesystem error reading a raw PEL",
85 entry("PELFILE=%s", rawPelPath.c_str()),
86 entry("OBMCLOGID=%d", obmcLogID));
87 // TODO, Decide what to do here. Maybe nothing.
88 return;
89 }
90
91 file.close();
92
Matt Spinler19e72902020-01-24 11:05:20 -060093 addPEL(data, obmcLogID);
Matt Spinler89fa0822019-07-17 13:54:30 -050094 }
95 else
96 {
97 log<level::ERR>("Raw PEL file from BMC event log does not exist",
98 entry("PELFILE=%s", (rawPelPath).c_str()),
99 entry("OBMCLOGID=%d", obmcLogID));
100 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500101}
102
Matt Spinler19e72902020-01-24 11:05:20 -0600103void Manager::addPEL(std::vector<uint8_t>& pelData, uint32_t obmcLogID)
104{
105
106 auto pel = std::make_unique<openpower::pels::PEL>(pelData, obmcLogID);
107 if (pel->valid())
108 {
109 // PELs created by others still need these fields set by us.
110 pel->assignID();
111 pel->setCommitTime();
112
113 try
114 {
115 _repo.add(pel);
116 }
117 catch (std::exception& e)
118 {
119 // Probably a full or r/o filesystem, not much we can do.
120 log<level::ERR>("Unable to add PEL to Repository",
121 entry("PEL_ID=0x%X", pel->id()));
122 }
123 }
124 else
125 {
126 log<level::ERR>("Invalid PEL received from the host",
127 entry("OBMCLOGID=%d", obmcLogID));
128
129 AdditionalData ad;
130 ad.add("PLID", getNumberString("0x%08X", pel->plid()));
131 ad.add("OBMC_LOG_ID", std::to_string(obmcLogID));
132 ad.add("PEL_SIZE", std::to_string(pelData.size()));
133
134 std::string asciiString;
135 auto src = pel->primarySRC();
136 if (src)
137 {
138 asciiString = (*src)->asciiString();
139 }
140
141 ad.add("SRC", asciiString);
142
143 _eventLogger.log("org.open_power.Logging.Error.BadHostPEL",
144 Entry::Level::Error, ad);
145 }
146}
147
148void Manager::addESELPEL(const std::string& esel, uint32_t obmcLogID)
149{
150 std::vector<uint8_t> data;
151
152 try
153 {
154 data = std::move(eselToRawData(esel));
155 }
156 catch (std::exception& e)
157 {
158 // Try to add it below anyway, so it follows the usual bad data path.
159 log<level::ERR>("Problems converting ESEL string to a byte vector");
160 }
161
162 addPEL(data, obmcLogID);
163}
164
165std::vector<uint8_t> Manager::eselToRawData(const std::string& esel)
166{
167 std::vector<uint8_t> data;
168 std::string byteString;
169
170 // As the eSEL string looks like: "50 48 00 ab ..." there are 3
171 // characters per raw byte, and since the actual PEL data starts
172 // at the 16th byte, the code will grab the PEL data starting at
173 // offset 48 in the string.
174 static constexpr size_t pelStart = 16 * 3;
175
176 if (esel.size() <= pelStart)
177 {
178 log<level::ERR>("ESEL data too short",
179 entry("ESEL_SIZE=%d", esel.size()));
180
181 throw std::length_error("ESEL data too short");
182 }
183
184 for (size_t i = pelStart; i < esel.size(); i += 3)
185 {
186 if (i + 1 < esel.size())
187 {
188 byteString = esel.substr(i, 2);
189 data.push_back(std::stoi(byteString, nullptr, 16));
190 }
191 else
192 {
193 log<level::ERR>("ESEL data too short",
194 entry("ESEL_SIZE=%d", esel.size()));
195 throw std::length_error("ESEL data too short");
196 }
197 }
198
199 return data;
200}
201
Matt Spinler4e8078c2019-07-09 13:22:32 -0500202void Manager::erase(uint32_t obmcLogID)
203{
Matt Spinler475e5742019-07-18 16:09:49 -0500204 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
205
206 _repo.remove(id);
Matt Spinler4e8078c2019-07-09 13:22:32 -0500207}
208
209bool Manager::isDeleteProhibited(uint32_t obmcLogID)
210{
211 return false;
212}
213
214void Manager::createPEL(const std::string& message, uint32_t obmcLogID,
215 uint64_t timestamp,
216 phosphor::logging::Entry::Level severity,
217 const std::vector<std::string>& additionalData,
218 const std::vector<std::string>& associations)
219{
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800220 auto entry = _registry.lookup(message, rg::LookupType::name);
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600221 std::string msg;
Matt Spinler67456c22019-10-21 12:22:49 -0500222
223 if (entry)
224 {
225 AdditionalData ad{additionalData};
226
Matt Spinlera34ab722019-12-16 10:39:32 -0600227 auto pel = std::make_unique<openpower::pels::PEL>(
228 *entry, obmcLogID, timestamp, severity, ad, *_dataIface);
Matt Spinler67456c22019-10-21 12:22:49 -0500229
230 _repo.add(pel);
Matt Spinler67456c22019-10-21 12:22:49 -0500231
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600232 auto src = pel->primarySRC();
233 if (src)
234 {
235 using namespace std::literals::string_literals;
Matt Spinler19e72902020-01-24 11:05:20 -0600236 auto id = getNumberString("0x%08X", pel->id());
Matt Spinler1d4c74a2019-12-16 14:40:21 -0600237 msg = "Created PEL "s + id + " with SRC "s + (*src)->asciiString();
238 while (msg.back() == ' ')
239 {
240 msg.pop_back();
241 }
242 log<level::INFO>(msg.c_str());
243 }
244 }
245 else
246 {
247 // TODO ibm-openbmc/dev/1151: Create a new PEL for this case.
248 // For now, just trace it.
249 msg = "Event not found in PEL message registry: " + message;
250 log<level::INFO>(msg.c_str());
251 }
Matt Spinler4e8078c2019-07-09 13:22:32 -0500252}
253
Matt Spinlera34ab722019-12-16 10:39:32 -0600254sdbusplus::message::unix_fd Manager::getPEL(uint32_t pelID)
255{
256 Repository::LogID id{Repository::LogID::Pel(pelID)};
257 std::optional<int> fd;
258
259 try
260 {
261 fd = _repo.getPELFD(id);
262 }
263 catch (std::exception& e)
264 {
265 throw common_error::InternalFailure();
266 }
267
268 if (!fd)
269 {
270 throw common_error::InvalidArgument();
271 }
272
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600273 scheduleFDClose(*fd);
274
Matt Spinlera34ab722019-12-16 10:39:32 -0600275 return *fd;
276}
277
Matt Spinler6b1a5c82020-01-07 08:48:53 -0600278void Manager::scheduleFDClose(int fd)
279{
280 _fdCloserEventSource = std::make_unique<sdeventplus::source::Defer>(
281 _logManager.getBus().get_event(),
282 std::bind(std::mem_fn(&Manager::closeFD), this, fd,
283 std::placeholders::_1));
284}
285
286void Manager::closeFD(int fd, sdeventplus::source::EventBase& source)
287{
288 close(fd);
289 _fdCloserEventSource.reset();
290}
291
Matt Spinlera34ab722019-12-16 10:39:32 -0600292std::vector<uint8_t> Manager::getPELFromOBMCID(uint32_t obmcLogID)
293{
294 Repository::LogID id{Repository::LogID::Obmc(obmcLogID)};
295 std::optional<std::vector<uint8_t>> data;
296
297 try
298 {
299 data = _repo.getPELData(id);
300 }
301 catch (std::exception& e)
302 {
303 throw common_error::InternalFailure();
304 }
305
306 if (!data)
307 {
308 throw common_error::InvalidArgument();
309 }
310
311 return *data;
312}
313
314void Manager::hostAck(uint32_t pelID)
315{
316 Repository::LogID id{Repository::LogID::Pel(pelID)};
317
318 if (!_repo.hasPEL(id))
319 {
320 throw common_error::InvalidArgument();
321 }
322
323 if (_hostNotifier)
324 {
325 _hostNotifier->ackPEL(pelID);
326 }
327}
328
329void Manager::hostReject(uint32_t pelID, RejectionReason reason)
330{
331 Repository::LogID id{Repository::LogID::Pel(pelID)};
332
333 if (!_repo.hasPEL(id))
334 {
335 throw common_error::InvalidArgument();
336 }
337
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600338 if (reason == RejectionReason::BadPEL)
Matt Spinlera34ab722019-12-16 10:39:32 -0600339 {
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600340 AdditionalData data;
341 data.add("BAD_ID", getNumberString("0x%08X", pelID));
342 _eventLogger.log("org.open_power.Logging.Error.SentBadPELToHost",
343 Entry::Level::Informational, data);
344 if (_hostNotifier)
Matt Spinlera34ab722019-12-16 10:39:32 -0600345 {
346 _hostNotifier->setBadPEL(pelID);
347 }
Matt Spinler05c2c6c2019-12-18 14:02:09 -0600348 }
349 else if ((reason == RejectionReason::HostFull) && _hostNotifier)
350 {
351 _hostNotifier->setHostFull(pelID);
Matt Spinlera34ab722019-12-16 10:39:32 -0600352 }
353}
354
Matt Spinler4e8078c2019-07-09 13:22:32 -0500355} // namespace pels
356} // namespace openpower