blob: 6ba71d0eeaf62a52a1d312b60215bd72fc90de18 [file] [log] [blame]
Brad Bishopf6783cd2020-10-27 19:25:09 -04001#include "create_pel.hpp"
2
Jayanth Othayothc4831812021-06-08 01:33:40 -05003#include "util.hpp"
4
Brad Bishopf6783cd2020-10-27 19:25:09 -04005#include <fcntl.h>
6#include <fmt/format.h>
7#include <libekb.H>
8#include <unistd.h>
9
Brad Bishop5e5d4452020-10-27 19:46:13 -040010#include <phosphor-logging/elog.hpp>
11#include <xyz/openbmc_project/Logging/Create/server.hpp>
12#include <xyz/openbmc_project/Logging/Entry/server.hpp>
13
Brad Bishopf6783cd2020-10-27 19:25:09 -040014#include <cerrno>
15#include <cstdio>
16#include <cstdlib>
17#include <cstring>
18#include <map>
Brad Bishopf6783cd2020-10-27 19:25:09 -040019#include <stdexcept>
20#include <string>
21#include <tuple>
22#include <vector>
Brad Bishopf6783cd2020-10-27 19:25:09 -040023
24namespace openpower
25{
26using namespace phosphor::logging;
27
Brad Bishopf6783cd2020-10-27 19:25:09 -040028namespace pel
29{
Andrew Geissler61febf02021-06-22 17:19:32 -050030
31constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
32constexpr auto loggingInterface = "xyz.openbmc_project.Logging.Create";
Jayanth Othayothfe37aea2021-10-07 04:41:26 -050033constexpr auto opLoggingInterface = "org.open_power.Logging.PEL";
Andrew Geissler61febf02021-06-22 17:19:32 -050034
Jayanth Othayoth8fe9ff92021-11-14 09:15:59 -060035void createErrorPEL(const std::string& event, const json& calloutData,
36 const FFDCData& ffdcData)
Brad Bishopf6783cd2020-10-27 19:25:09 -040037{
Brad Bishopf6783cd2020-10-27 19:25:09 -040038 std::map<std::string, std::string> additionalData;
39 auto bus = sdbusplus::bus::new_default();
40 additionalData.emplace("_PID", std::to_string(getpid()));
41 for (auto& data : ffdcData)
42 {
43 additionalData.emplace(data);
44 }
45
46 try
47 {
48 FFDCFile ffdcFile(calloutData);
49
50 std::vector<std::tuple<sdbusplus::xyz::openbmc_project::Logging::
51 server::Create::FFDCFormat,
52 uint8_t, uint8_t, sdbusplus::message::unix_fd>>
53 pelCalloutInfo;
54
55 pelCalloutInfo.push_back(
56 std::make_tuple(sdbusplus::xyz::openbmc_project::Logging::server::
57 Create::FFDCFormat::JSON,
58 static_cast<uint8_t>(0xCA),
59 static_cast<uint8_t>(0x01), ffdcFile.getFileFD()));
60
Brad Bishopf6783cd2020-10-27 19:25:09 -040061 std::string service =
62 util::getService(bus, loggingObjectPath, loggingInterface);
63 auto method =
64 bus.new_method_call(service.c_str(), loggingObjectPath,
65 loggingInterface, "CreateWithFFDCFiles");
66 auto level =
67 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
68 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
69 Error);
Jayanth Othayoth8fe9ff92021-11-14 09:15:59 -060070 method.append(event, level, additionalData, pelCalloutInfo);
Brad Bishopf6783cd2020-10-27 19:25:09 -040071 auto resp = bus.call(method);
72 }
Patrick Williams2246cca2021-09-02 09:32:52 -050073 catch (const sdbusplus::exception::exception& e)
Brad Bishopf6783cd2020-10-27 19:25:09 -040074 {
Jayanth Othayoth8fe9ff92021-11-14 09:15:59 -060075 log<level::ERR>(
76 fmt::format("D-Bus call exception",
77 "OBJPATH={}, INTERFACE={}, event={}, EXCEPTION={}",
78 loggingObjectPath, loggingInterface, event, e.what())
79 .c_str());
Brad Bishopf6783cd2020-10-27 19:25:09 -040080 throw std::runtime_error(
81 "Error in invoking D-Bus logging create interface");
82 }
Patrick Williams1a9a5a62021-10-06 13:05:06 -050083 catch (const std::exception& e)
Brad Bishopf6783cd2020-10-27 19:25:09 -040084 {
85 throw e;
86 }
87}
88
Jayanth Othayothfe37aea2021-10-07 04:41:26 -050089uint32_t createSbeErrorPEL(const std::string& event, const sbeError_t& sbeError,
90 const FFDCData& ffdcData)
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -050091{
Jayanth Othayothfe37aea2021-10-07 04:41:26 -050092 uint32_t plid = 0;
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -050093 std::map<std::string, std::string> additionalData;
94 auto bus = sdbusplus::bus::new_default();
95
96 additionalData.emplace("_PID", std::to_string(getpid()));
97 additionalData.emplace("SBE_ERR_MSG", sbeError.what());
98
99 for (auto& data : ffdcData)
100 {
101 additionalData.emplace(data);
102 }
103
104 std::vector<std::tuple<
105 sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat,
106 uint8_t, uint8_t, sdbusplus::message::unix_fd>>
107 pelFFDCInfo;
108
Jayanth Othayothfe37aea2021-10-07 04:41:26 -0500109 // get SBE ffdc file descriptor
110 auto fd = sbeError.getFd();
111
112 // Negative fd value indicates error case or invalid file
113 // No need of special processing , just log error with additional ffdc.
114 if (fd > 0)
115 {
116 // Refer phosphor-logging/extensions/openpower-pels/README.md section
117 // "Self Boot Engine(SBE) First Failure Data Capture(FFDC) Support"
118 // for details of related to createPEL with SBE FFDC information
119 // usin g CreateWithFFDCFiles api.
120 pelFFDCInfo.push_back(
121 std::make_tuple(sdbusplus::xyz::openbmc_project::Logging::server::
122 Create::FFDCFormat::Custom,
123 static_cast<uint8_t>(0xCB),
124 static_cast<uint8_t>(0x01), sbeError.getFd()));
125 }
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -0500126 try
127 {
128 std::string service =
Jayanth Othayothfe37aea2021-10-07 04:41:26 -0500129 util::getService(bus, loggingObjectPath, opLoggingInterface);
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -0500130 auto method =
131 bus.new_method_call(service.c_str(), loggingObjectPath,
Jayanth Othayothfe37aea2021-10-07 04:41:26 -0500132 opLoggingInterface, "CreatePELWithFFDCFiles");
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -0500133 auto level =
134 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
135 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
136 Error);
137 method.append(event, level, additionalData, pelFFDCInfo);
Jayanth Othayothfe37aea2021-10-07 04:41:26 -0500138 auto response = bus.call(method);
139
140 // reply will be tuple containing bmc log id, platform log id
141 std::tuple<uint32_t, uint32_t> reply = {0, 0};
142
143 // parse dbus response into reply
144 response.read(reply);
145 plid = std::get<1>(reply); // platform log id is tuple "second"
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -0500146 }
147 catch (const sdbusplus::exception::exception& e)
148 {
149 log<level::ERR>(fmt::format("D-Bus call exception",
150 "OBJPATH={}, INTERFACE={}, EXCEPTION={}",
151 loggingObjectPath, loggingInterface,
152 e.what())
153 .c_str());
154 throw std::runtime_error(
155 "Error in invoking D-Bus logging create interface");
156 }
Patrick Williams1a9a5a62021-10-06 13:05:06 -0500157 catch (const std::exception& e)
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -0500158 {
159 throw e;
160 }
Jayanth Othayothfe37aea2021-10-07 04:41:26 -0500161
162 return plid;
Jayanth Othayoth2eb31ad2021-09-20 07:13:28 -0500163}
164
Jayanth Othayotha8d2f712021-09-20 07:02:51 -0500165void createPEL(const std::string& event, const FFDCData& ffdcData)
Andrew Geissler61febf02021-06-22 17:19:32 -0500166{
167 std::map<std::string, std::string> additionalData;
168 auto bus = sdbusplus::bus::new_default();
Jayanth Othayotha8d2f712021-09-20 07:02:51 -0500169
Andrew Geissler61febf02021-06-22 17:19:32 -0500170 additionalData.emplace("_PID", std::to_string(getpid()));
Jayanth Othayotha8d2f712021-09-20 07:02:51 -0500171 for (auto& data : ffdcData)
172 {
173 additionalData.emplace(data);
174 }
Andrew Geissler61febf02021-06-22 17:19:32 -0500175
176 try
177 {
Andrew Geissler61febf02021-06-22 17:19:32 -0500178 std::string service =
179 util::getService(bus, loggingObjectPath, loggingInterface);
180 auto method = bus.new_method_call(service.c_str(), loggingObjectPath,
181 loggingInterface, "Create");
182 auto level =
183 sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
184 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
185 Error);
Jayanth Othayothac95c562021-07-16 05:56:04 -0500186 method.append(event, level, additionalData);
Andrew Geissler61febf02021-06-22 17:19:32 -0500187 auto resp = bus.call(method);
188 }
Patrick Williams2246cca2021-09-02 09:32:52 -0500189 catch (const sdbusplus::exception::exception& e)
Andrew Geissler61febf02021-06-22 17:19:32 -0500190 {
Jayanth Othayothac95c562021-07-16 05:56:04 -0500191 log<level::ERR>(fmt::format("sdbusplus D-Bus call exception",
192 "OBJPATH={}, INTERFACE={}, EXCEPTION={}",
193 loggingObjectPath, loggingInterface,
194 e.what())
195 .c_str());
196 ;
Andrew Geissler61febf02021-06-22 17:19:32 -0500197
198 throw std::runtime_error(
199 "Error in invoking D-Bus logging create interface");
200 }
Patrick Williams1a9a5a62021-10-06 13:05:06 -0500201 catch (const std::exception& e)
Andrew Geissler61febf02021-06-22 17:19:32 -0500202 {
Jayanth Othayothac95c562021-07-16 05:56:04 -0500203 log<level::ERR>(
204 fmt::format("D-bus call exception", "EXCEPTION={}", e.what())
205 .c_str());
Andrew Geissler61febf02021-06-22 17:19:32 -0500206 throw e;
207 }
208}
209
Brad Bishopf6783cd2020-10-27 19:25:09 -0400210FFDCFile::FFDCFile(const json& pHALCalloutData) :
211 calloutData(pHALCalloutData.dump()),
212 calloutFile("/tmp/phalPELCalloutsJson.XXXXXX"), fileFD(-1)
213{
214 prepareFFDCFile();
215}
216
217FFDCFile::~FFDCFile()
218{
219 removeCalloutFile();
220}
221
222int FFDCFile::getFileFD() const
223{
224 return fileFD;
225}
226
227void FFDCFile::prepareFFDCFile()
228{
229 createCalloutFile();
230 writeCalloutData();
231 setCalloutFileSeekPos();
232}
233
234void FFDCFile::createCalloutFile()
235{
236 fileFD = mkostemp(const_cast<char*>(calloutFile.c_str()), O_RDWR);
237
238 if (fileFD == -1)
239 {
240 log<level::ERR>(fmt::format("Failed to create phalPELCallouts "
241 "file({}), errorno({}) and errormsg({})",
242 calloutFile, errno, strerror(errno))
243 .c_str());
244 throw std::runtime_error("Failed to create phalPELCallouts file");
245 }
246}
247
248void FFDCFile::writeCalloutData()
249{
250 ssize_t rc = write(fileFD, calloutData.c_str(), calloutData.size());
251
252 if (rc == -1)
253 {
254 log<level::ERR>(fmt::format("Failed to write phaPELCallout info "
255 "in file({}), errorno({}), errormsg({})",
256 calloutFile, errno, strerror(errno))
257 .c_str());
258 throw std::runtime_error("Failed to write phalPELCallouts info");
259 }
260 else if (rc != static_cast<ssize_t>(calloutData.size()))
261 {
262 log<level::WARNING>(fmt::format("Could not write all phal callout "
263 "info in file({}), written byte({}) "
264 "and total byte({})",
265 calloutFile, rc, calloutData.size())
266 .c_str());
267 }
268}
269
270void FFDCFile::setCalloutFileSeekPos()
271{
272 int rc = lseek(fileFD, 0, SEEK_SET);
273
274 if (rc == -1)
275 {
276 log<level::ERR>(fmt::format("Failed to set SEEK_SET for "
277 "phalPELCallouts in file({}), errorno({}) "
278 "and errormsg({})",
279 calloutFile, errno, strerror(errno))
280 .c_str());
281 throw std::runtime_error(
282 "Failed to set SEEK_SET for phalPELCallouts file");
283 }
284}
285
286void FFDCFile::removeCalloutFile()
287{
288 close(fileFD);
289 std::remove(calloutFile.c_str());
290}
291
292} // namespace pel
293} // namespace openpower