blob: e560d5e1d6df64df5698eb9b8a04ad64b685925b [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 Spinlercb6b0592019-07-16 15:58:51 -050016#include "pel.hpp"
17
18#include "bcd_time.hpp"
Matt Spinlerc63e2e82019-12-02 15:50:12 -060019#include "extended_user_header.hpp"
Matt Spinleraa659472019-10-23 09:26:48 -050020#include "failing_mtms.hpp"
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080021#include "json_utils.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050022#include "log_id.hpp"
Matt Spinlerf1e85e22019-11-01 11:31:31 -050023#include "pel_rules.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050024#include "pel_values.hpp"
Matt Spinler131870c2019-09-25 13:29:04 -050025#include "section_factory.hpp"
Matt Spinlerbd716f02019-10-15 10:54:11 -050026#include "src.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050027#include "stream.hpp"
Matt Spinlerafa857c2019-10-24 13:03:46 -050028#include "user_data_formats.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050029
Aatir186ce8c2019-10-20 15:13:39 -050030#include <iostream>
Matt Spinler07eefc52019-09-26 11:18:26 -050031#include <phosphor-logging/log.hpp>
32
Matt Spinlercb6b0592019-07-16 15:58:51 -050033namespace openpower
34{
35namespace pels
36{
Matt Spinlerb8323632019-09-20 15:11:04 -050037namespace message = openpower::pels::message;
Aatir186ce8c2019-10-20 15:13:39 -050038namespace pv = openpower::pels::pel_values;
Matt Spinlerb8323632019-09-20 15:11:04 -050039
Matt Spinler677381b2020-01-23 10:04:29 -060040constexpr auto unknownValue = "Unknown";
41
Matt Spinlerb8323632019-09-20 15:11:04 -050042PEL::PEL(const message::Entry& entry, uint32_t obmcLogID, uint64_t timestamp,
Matt Spinlerbd716f02019-10-15 10:54:11 -050043 phosphor::logging::Entry::Level severity,
Matt Spinleraa659472019-10-23 09:26:48 -050044 const AdditionalData& additionalData,
45 const DataInterfaceBase& dataIface)
Matt Spinlerb8323632019-09-20 15:11:04 -050046{
47 _ph = std::make_unique<PrivateHeader>(entry.componentID, obmcLogID,
48 timestamp);
49 _uh = std::make_unique<UserHeader>(entry, severity);
50
Matt Spinlerbd716f02019-10-15 10:54:11 -050051 auto src = std::make_unique<SRC>(entry, additionalData);
Matt Spinlerc63e2e82019-12-02 15:50:12 -060052
53 auto euh = std::make_unique<ExtendedUserHeader>(dataIface, entry, *src);
54
Matt Spinlerbd716f02019-10-15 10:54:11 -050055 _optionalSections.push_back(std::move(src));
Matt Spinlerc63e2e82019-12-02 15:50:12 -060056 _optionalSections.push_back(std::move(euh));
Matt Spinlerb8323632019-09-20 15:11:04 -050057
Matt Spinleraa659472019-10-23 09:26:48 -050058 auto mtms = std::make_unique<FailingMTMS>(dataIface);
59 _optionalSections.push_back(std::move(mtms));
Matt Spinlerbd716f02019-10-15 10:54:11 -050060
Matt Spinler4dcd3f42020-01-22 14:55:07 -060061 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
62 _optionalSections.push_back(std::move(ud));
63
Matt Spinlerafa857c2019-10-24 13:03:46 -050064 if (!additionalData.empty())
65 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -060066 ud = util::makeADUserDataSection(additionalData);
Matt Spinler6d663822020-01-22 14:50:46 -060067
68 // To be safe, check there isn't too much data
69 if (size() + ud->header().size <= _maxPELSize)
70 {
71 _optionalSections.push_back(std::move(ud));
72 }
Matt Spinlerafa857c2019-10-24 13:03:46 -050073 }
74
Matt Spinler97d19b42019-10-29 11:34:03 -050075 _ph->setSectionCount(2 + _optionalSections.size());
Matt Spinlerf1e85e22019-11-01 11:31:31 -050076
77 checkRulesAndFix();
Matt Spinlerb8323632019-09-20 15:11:04 -050078}
Matt Spinlercb6b0592019-07-16 15:58:51 -050079
Matt Spinler07eefc52019-09-26 11:18:26 -050080PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
Matt Spinlercb6b0592019-07-16 15:58:51 -050081{
82}
83
Matt Spinler07eefc52019-09-26 11:18:26 -050084PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -050085{
Matt Spinler07eefc52019-09-26 11:18:26 -050086 populateFromRawData(data, obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -050087}
88
Matt Spinler07eefc52019-09-26 11:18:26 -050089void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -050090{
Matt Spinler07eefc52019-09-26 11:18:26 -050091 Stream pelData{data};
Matt Spinlercb6b0592019-07-16 15:58:51 -050092 _ph = std::make_unique<PrivateHeader>(pelData);
93 if (obmcLogID != 0)
94 {
Matt Spinler97d19b42019-10-29 11:34:03 -050095 _ph->setOBMCLogID(obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -050096 }
97
98 _uh = std::make_unique<UserHeader>(pelData);
Matt Spinler131870c2019-09-25 13:29:04 -050099
100 // Use the section factory to create the rest of the objects
101 for (size_t i = 2; i < _ph->sectionCount(); i++)
102 {
103 auto section = section_factory::create(pelData);
104 _optionalSections.push_back(std::move(section));
105 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500106}
107
108bool PEL::valid() const
109{
110 bool valid = _ph->valid();
111
112 if (valid)
113 {
114 valid = _uh->valid();
115 }
116
Matt Spinler131870c2019-09-25 13:29:04 -0500117 if (valid)
118 {
119 if (!std::all_of(_optionalSections.begin(), _optionalSections.end(),
120 [](const auto& section) { return section->valid(); }))
121 {
122 valid = false;
123 }
124 }
125
Matt Spinlercb6b0592019-07-16 15:58:51 -0500126 return valid;
127}
128
129void PEL::setCommitTime()
130{
131 auto now = std::chrono::system_clock::now();
Matt Spinler97d19b42019-10-29 11:34:03 -0500132 _ph->setCommitTimestamp(getBCDTime(now));
Matt Spinlercb6b0592019-07-16 15:58:51 -0500133}
134
135void PEL::assignID()
136{
Matt Spinler97d19b42019-10-29 11:34:03 -0500137 _ph->setID(generatePELID());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500138}
139
Matt Spinler06885452019-11-06 10:35:42 -0600140void PEL::flatten(std::vector<uint8_t>& pelBuffer) const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500141{
142 Stream pelData{pelBuffer};
Matt Spinlerb8323632019-09-20 15:11:04 -0500143
Matt Spinler07eefc52019-09-26 11:18:26 -0500144 if (!valid())
Matt Spinlercb6b0592019-07-16 15:58:51 -0500145 {
Matt Spinler07eefc52019-09-26 11:18:26 -0500146 using namespace phosphor::logging;
147 log<level::WARNING>("Unflattening an invalid PEL");
Matt Spinlercb6b0592019-07-16 15:58:51 -0500148 }
149
Matt Spinler07eefc52019-09-26 11:18:26 -0500150 _ph->flatten(pelData);
Matt Spinlerb8323632019-09-20 15:11:04 -0500151 _uh->flatten(pelData);
Matt Spinler07eefc52019-09-26 11:18:26 -0500152
153 for (auto& section : _optionalSections)
154 {
155 section->flatten(pelData);
156 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500157}
158
Matt Spinler06885452019-11-06 10:35:42 -0600159std::vector<uint8_t> PEL::data() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500160{
Matt Spinler07eefc52019-09-26 11:18:26 -0500161 std::vector<uint8_t> pelData;
162 flatten(pelData);
163 return pelData;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500164}
165
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600166size_t PEL::size() const
167{
168 size_t size = 0;
169
170 if (_ph)
171 {
172 size += _ph->header().size;
173 }
174
175 if (_uh)
176 {
177 size += _uh->header().size;
178 }
179
180 for (const auto& section : _optionalSections)
181 {
182 size += section->header().size;
183 }
184
185 return size;
186}
187
Matt Spinlerbd716f02019-10-15 10:54:11 -0500188std::optional<SRC*> PEL::primarySRC() const
189{
190 auto src = std::find_if(
191 _optionalSections.begin(), _optionalSections.end(), [](auto& section) {
192 return section->header().id ==
193 static_cast<uint16_t>(SectionID::primarySRC);
194 });
195 if (src != _optionalSections.end())
196 {
197 return static_cast<SRC*>(src->get());
198 }
199
200 return std::nullopt;
201}
202
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500203void PEL::checkRulesAndFix()
204{
205 auto [actionFlags, eventType] =
206 pel_rules::check(_uh->actionFlags(), _uh->eventType(), _uh->severity());
207
208 _uh->setActionFlags(actionFlags);
209 _uh->setEventType(eventType);
210}
211
Matt Spinleracb7c102020-01-10 13:49:22 -0600212void PEL::printSectionInJSON(const Section& section, std::string& buf,
213 std::map<uint16_t, size_t>& pluralSections) const
Aatir186ce8c2019-10-20 15:13:39 -0500214{
215 char tmpB[5];
Aatir Manzurad0e0472019-10-07 13:18:37 -0500216 uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
217 static_cast<uint8_t>(section.header().id)};
218 sprintf(tmpB, "%c%c", id[0], id[1]);
219 std::string sectionID(tmpB);
220 std::string sectionName = pv::sectionTitles.count(sectionID)
221 ? pv::sectionTitles.at(sectionID)
222 : "Unknown Section";
Matt Spinleracb7c102020-01-10 13:49:22 -0600223
224 // Add a count if there are multiple of this type of section
225 auto count = pluralSections.find(section.header().id);
226 if (count != pluralSections.end())
227 {
228 sectionName += " " + std::to_string(count->second);
229 count->second++;
230 }
231
Aatir186ce8c2019-10-20 15:13:39 -0500232 if (section.valid())
233 {
Aatir Manzurad0e0472019-10-07 13:18:37 -0500234 auto json = section.getJSON();
235 if (json)
236 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800237 buf += "\n\"" + sectionName + "\": {\n";
238 buf += *json + "\n},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500239 }
240 else
241 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800242 buf += "\n\"" + sectionName + "\": [\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500243 std::vector<uint8_t> data;
244 Stream s{data};
245 section.flatten(s);
246 std::string dstr = dumpHex(std::data(data), data.size());
247 buf += dstr + "],\n";
248 }
Aatir186ce8c2019-10-20 15:13:39 -0500249 }
250 else
251 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800252 buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n";
Aatir186ce8c2019-10-20 15:13:39 -0500253 }
254}
255
Matt Spinleracb7c102020-01-10 13:49:22 -0600256std::map<uint16_t, size_t> PEL::getPluralSections() const
257{
258 std::map<uint16_t, size_t> sectionCounts;
259
260 for (const auto& section : optionalSections())
261 {
262 if (sectionCounts.find(section->header().id) == sectionCounts.end())
263 {
264 sectionCounts[section->header().id] = 1;
265 }
266 else
267 {
268 sectionCounts[section->header().id]++;
269 }
270 }
271
272 std::map<uint16_t, size_t> sections;
273 for (const auto& [id, count] : sectionCounts)
274 {
275 if (count > 1)
276 {
277 // Start with 0 here and printSectionInJSON()
278 // will increment it as it goes.
279 sections.emplace(id, 0);
280 }
281 }
282
283 return sections;
284}
285
Matt Spinler06885452019-11-06 10:35:42 -0600286void PEL::toJSON() const
Aatir186ce8c2019-10-20 15:13:39 -0500287{
Matt Spinleracb7c102020-01-10 13:49:22 -0600288 auto sections = getPluralSections();
289
Aatir186ce8c2019-10-20 15:13:39 -0500290 std::string buf = "{";
Matt Spinleracb7c102020-01-10 13:49:22 -0600291 printSectionInJSON(*(_ph.get()), buf, sections);
292 printSectionInJSON(*(_uh.get()), buf, sections);
Aatir186ce8c2019-10-20 15:13:39 -0500293 for (auto& section : this->optionalSections())
294 {
Matt Spinleracb7c102020-01-10 13:49:22 -0600295 printSectionInJSON(*(section.get()), buf, sections);
Aatir186ce8c2019-10-20 15:13:39 -0500296 }
297 buf += "}";
298 std::size_t found = buf.rfind(",");
299 if (found != std::string::npos)
300 buf.replace(found, 1, "");
301 std::cout << buf << std::endl;
302}
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800303
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600304namespace util
305{
306
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600307std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json)
308{
309 auto jsonString = json.dump();
310 std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
311
312 // Pad to a 4 byte boundary
313 while ((jsonData.size() % 4) != 0)
314 {
315 jsonData.push_back(0);
316 }
317
318 return std::make_unique<UserData>(
319 static_cast<uint16_t>(ComponentID::phosphorLogging),
320 static_cast<uint8_t>(UserDataFormat::json),
321 static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
322}
323
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600324std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
325{
326 assert(!ad.empty());
327 nlohmann::json json;
328
329 // Remove the 'ESEL' entry, as it contains a full PEL in the value.
330 if (ad.getValue("ESEL"))
331 {
332 auto newAD = ad;
333 newAD.remove("ESEL");
334 json = newAD.toJSON();
335 }
336 else
337 {
338 json = ad.toJSON();
339 }
340
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600341 return makeJSONUserDataSection(json);
342}
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600343
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600344void addProcessNameToJSON(nlohmann::json& json,
345 const std::optional<std::string>& pid,
346 const DataInterfaceBase& dataIface)
347{
Matt Spinler677381b2020-01-23 10:04:29 -0600348 std::string name{unknownValue};
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600349
350 try
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600351 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600352 if (pid)
353 {
354 auto n = dataIface.getProcessName(*pid);
355 if (n)
356 {
357 name = *n;
358 }
359 }
360 }
361 catch (std::exception& e)
362 {
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600363 }
364
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600365 json["Process Name"] = std::move(name);
366}
367
Matt Spinler677381b2020-01-23 10:04:29 -0600368void addBMCFWVersionIDToJSON(nlohmann::json& json,
369 const DataInterfaceBase& dataIface)
370{
371 auto id = dataIface.getBMCFWVersionID();
372 if (id.empty())
373 {
374 id = unknownValue;
375 }
376
377 json["BMC Version ID"] = std::move(id);
378}
379
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600380std::unique_ptr<UserData>
381 makeSysInfoUserDataSection(const AdditionalData& ad,
382 const DataInterfaceBase& dataIface)
383{
384 nlohmann::json json;
385
386 addProcessNameToJSON(json, ad.getValue("_PID"), dataIface);
Matt Spinler677381b2020-01-23 10:04:29 -0600387 addBMCFWVersionIDToJSON(json, dataIface);
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600388
389 return makeJSONUserDataSection(json);
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600390}
391
392} // namespace util
393
Matt Spinlercb6b0592019-07-16 15:58:51 -0500394} // namespace pels
395} // namespace openpower