blob: 167d28a003bc39e643024c5889af1a481a4c2f92 [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 Spinler4bfc9082020-03-24 15:05:54 -050039using namespace phosphor::logging;
Matt Spinlerb8323632019-09-20 15:11:04 -050040
Matt Spinler677381b2020-01-23 10:04:29 -060041constexpr auto unknownValue = "Unknown";
42
Matt Spinler4bfc9082020-03-24 15:05:54 -050043PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp,
Matt Spinlerbd716f02019-10-15 10:54:11 -050044 phosphor::logging::Entry::Level severity,
Matt Spinleraa659472019-10-23 09:26:48 -050045 const AdditionalData& additionalData,
46 const DataInterfaceBase& dataIface)
Matt Spinlerb8323632019-09-20 15:11:04 -050047{
Matt Spinler4bfc9082020-03-24 15:05:54 -050048 _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID,
Matt Spinlerb8323632019-09-20 15:11:04 -050049 timestamp);
Matt Spinler4bfc9082020-03-24 15:05:54 -050050 _uh = std::make_unique<UserHeader>(regEntry, severity);
Matt Spinlerb8323632019-09-20 15:11:04 -050051
Matt Spinler4bfc9082020-03-24 15:05:54 -050052 auto src = std::make_unique<SRC>(regEntry, additionalData, dataIface);
Matt Spinlerc63e2e82019-12-02 15:50:12 -060053
Matt Spinler4bfc9082020-03-24 15:05:54 -050054 auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src);
Matt Spinlerc63e2e82019-12-02 15:50:12 -060055
Matt Spinlerbd716f02019-10-15 10:54:11 -050056 _optionalSections.push_back(std::move(src));
Matt Spinlerc63e2e82019-12-02 15:50:12 -060057 _optionalSections.push_back(std::move(euh));
Matt Spinlerb8323632019-09-20 15:11:04 -050058
Matt Spinleraa659472019-10-23 09:26:48 -050059 auto mtms = std::make_unique<FailingMTMS>(dataIface);
60 _optionalSections.push_back(std::move(mtms));
Matt Spinlerbd716f02019-10-15 10:54:11 -050061
Matt Spinler4dcd3f42020-01-22 14:55:07 -060062 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
63 _optionalSections.push_back(std::move(ud));
64
Matt Spinler9b7e94f2020-03-24 15:44:41 -050065 // Create a UserData section from AdditionalData.
Matt Spinlerafa857c2019-10-24 13:03:46 -050066 if (!additionalData.empty())
67 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -060068 ud = util::makeADUserDataSection(additionalData);
Matt Spinler6d663822020-01-22 14:50:46 -060069
Matt Spinler9b7e94f2020-03-24 15:44:41 -050070 // Shrink the section if necessary.
71 if (size() + ud->header().size > _maxPELSize)
72 {
73 if (ud->shrink(_maxPELSize - size()))
74 {
75 _optionalSections.push_back(std::move(ud));
76 }
77 else
78 {
79 log<level::WARNING>(
80 "Dropping AdditionalData UserData section",
81 entry("SECTION_SIZE=%d\n", ud->header().size));
82 }
83 }
84 else
Matt Spinler6d663822020-01-22 14:50:46 -060085 {
86 _optionalSections.push_back(std::move(ud));
87 }
Matt Spinlerafa857c2019-10-24 13:03:46 -050088 }
89
Matt Spinler97d19b42019-10-29 11:34:03 -050090 _ph->setSectionCount(2 + _optionalSections.size());
Matt Spinlerf1e85e22019-11-01 11:31:31 -050091
92 checkRulesAndFix();
Matt Spinlerb8323632019-09-20 15:11:04 -050093}
Matt Spinlercb6b0592019-07-16 15:58:51 -050094
Matt Spinler07eefc52019-09-26 11:18:26 -050095PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
Matt Spinlercb6b0592019-07-16 15:58:51 -050096{
97}
98
Matt Spinler07eefc52019-09-26 11:18:26 -050099PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500100{
Matt Spinler07eefc52019-09-26 11:18:26 -0500101 populateFromRawData(data, obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500102}
103
Matt Spinler07eefc52019-09-26 11:18:26 -0500104void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -0500105{
Matt Spinler07eefc52019-09-26 11:18:26 -0500106 Stream pelData{data};
Matt Spinlercb6b0592019-07-16 15:58:51 -0500107 _ph = std::make_unique<PrivateHeader>(pelData);
108 if (obmcLogID != 0)
109 {
Matt Spinler97d19b42019-10-29 11:34:03 -0500110 _ph->setOBMCLogID(obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500111 }
112
113 _uh = std::make_unique<UserHeader>(pelData);
Matt Spinler131870c2019-09-25 13:29:04 -0500114
115 // Use the section factory to create the rest of the objects
116 for (size_t i = 2; i < _ph->sectionCount(); i++)
117 {
118 auto section = section_factory::create(pelData);
119 _optionalSections.push_back(std::move(section));
120 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500121}
122
123bool PEL::valid() const
124{
125 bool valid = _ph->valid();
126
127 if (valid)
128 {
129 valid = _uh->valid();
130 }
131
Matt Spinler131870c2019-09-25 13:29:04 -0500132 if (valid)
133 {
134 if (!std::all_of(_optionalSections.begin(), _optionalSections.end(),
135 [](const auto& section) { return section->valid(); }))
136 {
137 valid = false;
138 }
139 }
140
Matt Spinlercb6b0592019-07-16 15:58:51 -0500141 return valid;
142}
143
144void PEL::setCommitTime()
145{
146 auto now = std::chrono::system_clock::now();
Matt Spinler97d19b42019-10-29 11:34:03 -0500147 _ph->setCommitTimestamp(getBCDTime(now));
Matt Spinlercb6b0592019-07-16 15:58:51 -0500148}
149
150void PEL::assignID()
151{
Matt Spinler97d19b42019-10-29 11:34:03 -0500152 _ph->setID(generatePELID());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500153}
154
Matt Spinler06885452019-11-06 10:35:42 -0600155void PEL::flatten(std::vector<uint8_t>& pelBuffer) const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500156{
157 Stream pelData{pelBuffer};
Matt Spinlerb8323632019-09-20 15:11:04 -0500158
Matt Spinler07eefc52019-09-26 11:18:26 -0500159 if (!valid())
Matt Spinlercb6b0592019-07-16 15:58:51 -0500160 {
Matt Spinler07eefc52019-09-26 11:18:26 -0500161 log<level::WARNING>("Unflattening an invalid PEL");
Matt Spinlercb6b0592019-07-16 15:58:51 -0500162 }
163
Matt Spinler07eefc52019-09-26 11:18:26 -0500164 _ph->flatten(pelData);
Matt Spinlerb8323632019-09-20 15:11:04 -0500165 _uh->flatten(pelData);
Matt Spinler07eefc52019-09-26 11:18:26 -0500166
167 for (auto& section : _optionalSections)
168 {
169 section->flatten(pelData);
170 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500171}
172
Matt Spinler06885452019-11-06 10:35:42 -0600173std::vector<uint8_t> PEL::data() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500174{
Matt Spinler07eefc52019-09-26 11:18:26 -0500175 std::vector<uint8_t> pelData;
176 flatten(pelData);
177 return pelData;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500178}
179
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600180size_t PEL::size() const
181{
182 size_t size = 0;
183
184 if (_ph)
185 {
186 size += _ph->header().size;
187 }
188
189 if (_uh)
190 {
191 size += _uh->header().size;
192 }
193
194 for (const auto& section : _optionalSections)
195 {
196 size += section->header().size;
197 }
198
199 return size;
200}
201
Matt Spinlerbd716f02019-10-15 10:54:11 -0500202std::optional<SRC*> PEL::primarySRC() const
203{
204 auto src = std::find_if(
205 _optionalSections.begin(), _optionalSections.end(), [](auto& section) {
206 return section->header().id ==
207 static_cast<uint16_t>(SectionID::primarySRC);
208 });
209 if (src != _optionalSections.end())
210 {
211 return static_cast<SRC*>(src->get());
212 }
213
214 return std::nullopt;
215}
216
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500217void PEL::checkRulesAndFix()
218{
219 auto [actionFlags, eventType] =
220 pel_rules::check(_uh->actionFlags(), _uh->eventType(), _uh->severity());
221
222 _uh->setActionFlags(actionFlags);
223 _uh->setEventType(eventType);
224}
225
Matt Spinleracb7c102020-01-10 13:49:22 -0600226void PEL::printSectionInJSON(const Section& section, std::string& buf,
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800227 std::map<uint16_t, size_t>& pluralSections,
228 message::Registry& registry) const
Aatir186ce8c2019-10-20 15:13:39 -0500229{
230 char tmpB[5];
Aatir Manzurad0e0472019-10-07 13:18:37 -0500231 uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
232 static_cast<uint8_t>(section.header().id)};
233 sprintf(tmpB, "%c%c", id[0], id[1]);
234 std::string sectionID(tmpB);
235 std::string sectionName = pv::sectionTitles.count(sectionID)
236 ? pv::sectionTitles.at(sectionID)
237 : "Unknown Section";
Matt Spinleracb7c102020-01-10 13:49:22 -0600238
239 // Add a count if there are multiple of this type of section
240 auto count = pluralSections.find(section.header().id);
241 if (count != pluralSections.end())
242 {
243 sectionName += " " + std::to_string(count->second);
244 count->second++;
245 }
246
Aatir186ce8c2019-10-20 15:13:39 -0500247 if (section.valid())
248 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800249 auto json = (sectionID == "PS" || sectionID == "SS")
250 ? section.getJSON(registry)
251 : section.getJSON();
Matt Spinler4220a152020-03-26 10:18:09 -0500252
253 buf += "\"" + sectionName + "\": {\n";
254
Aatir Manzurad0e0472019-10-07 13:18:37 -0500255 if (json)
256 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800257 buf += *json + "\n},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500258 }
259 else
260 {
Matt Spinler4220a152020-03-26 10:18:09 -0500261 jsonInsert(buf, pv::sectionVer,
262 getNumberString("%d", section.header().version), 1);
263 jsonInsert(buf, pv::subSection,
264 getNumberString("%d", section.header().subType), 1);
265 jsonInsert(buf, pv::createdBy,
266 getNumberString("0x%X", section.header().componentID),
267 1);
268
Aatir Manzurad0e0472019-10-07 13:18:37 -0500269 std::vector<uint8_t> data;
270 Stream s{data};
271 section.flatten(s);
Matt Spinler4220a152020-03-26 10:18:09 -0500272 std::string dstr =
273 dumpHex(std::data(data) + SectionHeader::flattenedSize(),
274 data.size(), 2);
275
276 std::string jsonIndent(indentLevel, 0x20);
277 buf += jsonIndent + "\"Data\": [\n";
278 buf += dstr;
279 buf += jsonIndent + "]\n";
280 buf += "},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500281 }
Aatir186ce8c2019-10-20 15:13:39 -0500282 }
283 else
284 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800285 buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n";
Aatir186ce8c2019-10-20 15:13:39 -0500286 }
287}
288
Matt Spinleracb7c102020-01-10 13:49:22 -0600289std::map<uint16_t, size_t> PEL::getPluralSections() const
290{
291 std::map<uint16_t, size_t> sectionCounts;
292
293 for (const auto& section : optionalSections())
294 {
295 if (sectionCounts.find(section->header().id) == sectionCounts.end())
296 {
297 sectionCounts[section->header().id] = 1;
298 }
299 else
300 {
301 sectionCounts[section->header().id]++;
302 }
303 }
304
305 std::map<uint16_t, size_t> sections;
306 for (const auto& [id, count] : sectionCounts)
307 {
308 if (count > 1)
309 {
310 // Start with 0 here and printSectionInJSON()
311 // will increment it as it goes.
312 sections.emplace(id, 0);
313 }
314 }
315
316 return sections;
317}
318
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800319void PEL::toJSON(message::Registry& registry) const
Aatir186ce8c2019-10-20 15:13:39 -0500320{
Matt Spinleracb7c102020-01-10 13:49:22 -0600321 auto sections = getPluralSections();
322
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800323 std::string buf = "{\n";
324 printSectionInJSON(*(_ph.get()), buf, sections, registry);
325 printSectionInJSON(*(_uh.get()), buf, sections, registry);
Aatir186ce8c2019-10-20 15:13:39 -0500326 for (auto& section : this->optionalSections())
327 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800328 printSectionInJSON(*(section.get()), buf, sections, registry);
Aatir186ce8c2019-10-20 15:13:39 -0500329 }
330 buf += "}";
331 std::size_t found = buf.rfind(",");
332 if (found != std::string::npos)
333 buf.replace(found, 1, "");
334 std::cout << buf << std::endl;
335}
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800336
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600337namespace util
338{
339
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600340std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json)
341{
342 auto jsonString = json.dump();
343 std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
344
345 // Pad to a 4 byte boundary
346 while ((jsonData.size() % 4) != 0)
347 {
348 jsonData.push_back(0);
349 }
350
351 return std::make_unique<UserData>(
352 static_cast<uint16_t>(ComponentID::phosphorLogging),
353 static_cast<uint8_t>(UserDataFormat::json),
354 static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
355}
356
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600357std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
358{
359 assert(!ad.empty());
360 nlohmann::json json;
361
362 // Remove the 'ESEL' entry, as it contains a full PEL in the value.
363 if (ad.getValue("ESEL"))
364 {
365 auto newAD = ad;
366 newAD.remove("ESEL");
367 json = newAD.toJSON();
368 }
369 else
370 {
371 json = ad.toJSON();
372 }
373
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600374 return makeJSONUserDataSection(json);
375}
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600376
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600377void addProcessNameToJSON(nlohmann::json& json,
378 const std::optional<std::string>& pid,
379 const DataInterfaceBase& dataIface)
380{
Matt Spinler677381b2020-01-23 10:04:29 -0600381 std::string name{unknownValue};
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600382
383 try
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600384 {
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600385 if (pid)
386 {
387 auto n = dataIface.getProcessName(*pid);
388 if (n)
389 {
390 name = *n;
391 }
392 }
393 }
394 catch (std::exception& e)
395 {
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600396 }
397
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600398 json["Process Name"] = std::move(name);
399}
400
Matt Spinler677381b2020-01-23 10:04:29 -0600401void addBMCFWVersionIDToJSON(nlohmann::json& json,
402 const DataInterfaceBase& dataIface)
403{
404 auto id = dataIface.getBMCFWVersionID();
405 if (id.empty())
406 {
407 id = unknownValue;
408 }
409
410 json["BMC Version ID"] = std::move(id);
411}
412
Matt Spinler4aa23a12020-02-03 15:05:09 -0600413std::string lastSegment(char separator, std::string data)
414{
415 auto pos = data.find_last_of(separator);
416 if (pos != std::string::npos)
417 {
418 data = data.substr(pos + 1);
419 }
420
421 return data;
422}
423
424void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface)
425{
426 json["BMCState"] = lastSegment('.', dataIface.getBMCState());
427 json["ChassisState"] = lastSegment('.', dataIface.getChassisState());
428 json["HostState"] = lastSegment('.', dataIface.getHostState());
429}
430
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600431std::unique_ptr<UserData>
432 makeSysInfoUserDataSection(const AdditionalData& ad,
433 const DataInterfaceBase& dataIface)
434{
435 nlohmann::json json;
436
437 addProcessNameToJSON(json, ad.getValue("_PID"), dataIface);
Matt Spinler677381b2020-01-23 10:04:29 -0600438 addBMCFWVersionIDToJSON(json, dataIface);
Matt Spinler4aa23a12020-02-03 15:05:09 -0600439 addStatesToJSON(json, dataIface);
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600440
441 return makeJSONUserDataSection(json);
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600442}
443
444} // namespace util
445
Matt Spinlercb6b0592019-07-16 15:58:51 -0500446} // namespace pels
447} // namespace openpower