blob: 625d177064586b2057dbb39f335c5127054872cf [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
40PEL::PEL(const message::Entry& entry, uint32_t obmcLogID, uint64_t timestamp,
Matt Spinlerbd716f02019-10-15 10:54:11 -050041 phosphor::logging::Entry::Level severity,
Matt Spinleraa659472019-10-23 09:26:48 -050042 const AdditionalData& additionalData,
43 const DataInterfaceBase& dataIface)
Matt Spinlerb8323632019-09-20 15:11:04 -050044{
45 _ph = std::make_unique<PrivateHeader>(entry.componentID, obmcLogID,
46 timestamp);
47 _uh = std::make_unique<UserHeader>(entry, severity);
48
Matt Spinlerbd716f02019-10-15 10:54:11 -050049 auto src = std::make_unique<SRC>(entry, additionalData);
Matt Spinlerc63e2e82019-12-02 15:50:12 -060050
51 auto euh = std::make_unique<ExtendedUserHeader>(dataIface, entry, *src);
52
Matt Spinlerbd716f02019-10-15 10:54:11 -050053 _optionalSections.push_back(std::move(src));
Matt Spinlerc63e2e82019-12-02 15:50:12 -060054 _optionalSections.push_back(std::move(euh));
Matt Spinlerb8323632019-09-20 15:11:04 -050055
Matt Spinleraa659472019-10-23 09:26:48 -050056 auto mtms = std::make_unique<FailingMTMS>(dataIface);
57 _optionalSections.push_back(std::move(mtms));
Matt Spinlerbd716f02019-10-15 10:54:11 -050058
Matt Spinlerafa857c2019-10-24 13:03:46 -050059 if (!additionalData.empty())
60 {
61 auto ud = util::makeADUserDataSection(additionalData);
Matt Spinler6d663822020-01-22 14:50:46 -060062
63 // To be safe, check there isn't too much data
64 if (size() + ud->header().size <= _maxPELSize)
65 {
66 _optionalSections.push_back(std::move(ud));
67 }
Matt Spinlerafa857c2019-10-24 13:03:46 -050068 }
69
Matt Spinler97d19b42019-10-29 11:34:03 -050070 _ph->setSectionCount(2 + _optionalSections.size());
Matt Spinlerf1e85e22019-11-01 11:31:31 -050071
72 checkRulesAndFix();
Matt Spinlerb8323632019-09-20 15:11:04 -050073}
Matt Spinlercb6b0592019-07-16 15:58:51 -050074
Matt Spinler07eefc52019-09-26 11:18:26 -050075PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0)
Matt Spinlercb6b0592019-07-16 15:58:51 -050076{
77}
78
Matt Spinler07eefc52019-09-26 11:18:26 -050079PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -050080{
Matt Spinler07eefc52019-09-26 11:18:26 -050081 populateFromRawData(data, obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -050082}
83
Matt Spinler07eefc52019-09-26 11:18:26 -050084void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID)
Matt Spinlercb6b0592019-07-16 15:58:51 -050085{
Matt Spinler07eefc52019-09-26 11:18:26 -050086 Stream pelData{data};
Matt Spinlercb6b0592019-07-16 15:58:51 -050087 _ph = std::make_unique<PrivateHeader>(pelData);
88 if (obmcLogID != 0)
89 {
Matt Spinler97d19b42019-10-29 11:34:03 -050090 _ph->setOBMCLogID(obmcLogID);
Matt Spinlercb6b0592019-07-16 15:58:51 -050091 }
92
93 _uh = std::make_unique<UserHeader>(pelData);
Matt Spinler131870c2019-09-25 13:29:04 -050094
95 // Use the section factory to create the rest of the objects
96 for (size_t i = 2; i < _ph->sectionCount(); i++)
97 {
98 auto section = section_factory::create(pelData);
99 _optionalSections.push_back(std::move(section));
100 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500101}
102
103bool PEL::valid() const
104{
105 bool valid = _ph->valid();
106
107 if (valid)
108 {
109 valid = _uh->valid();
110 }
111
Matt Spinler131870c2019-09-25 13:29:04 -0500112 if (valid)
113 {
114 if (!std::all_of(_optionalSections.begin(), _optionalSections.end(),
115 [](const auto& section) { return section->valid(); }))
116 {
117 valid = false;
118 }
119 }
120
Matt Spinlercb6b0592019-07-16 15:58:51 -0500121 return valid;
122}
123
124void PEL::setCommitTime()
125{
126 auto now = std::chrono::system_clock::now();
Matt Spinler97d19b42019-10-29 11:34:03 -0500127 _ph->setCommitTimestamp(getBCDTime(now));
Matt Spinlercb6b0592019-07-16 15:58:51 -0500128}
129
130void PEL::assignID()
131{
Matt Spinler97d19b42019-10-29 11:34:03 -0500132 _ph->setID(generatePELID());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500133}
134
Matt Spinler06885452019-11-06 10:35:42 -0600135void PEL::flatten(std::vector<uint8_t>& pelBuffer) const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500136{
137 Stream pelData{pelBuffer};
Matt Spinlerb8323632019-09-20 15:11:04 -0500138
Matt Spinler07eefc52019-09-26 11:18:26 -0500139 if (!valid())
Matt Spinlercb6b0592019-07-16 15:58:51 -0500140 {
Matt Spinler07eefc52019-09-26 11:18:26 -0500141 using namespace phosphor::logging;
142 log<level::WARNING>("Unflattening an invalid PEL");
Matt Spinlercb6b0592019-07-16 15:58:51 -0500143 }
144
Matt Spinler07eefc52019-09-26 11:18:26 -0500145 _ph->flatten(pelData);
Matt Spinlerb8323632019-09-20 15:11:04 -0500146 _uh->flatten(pelData);
Matt Spinler07eefc52019-09-26 11:18:26 -0500147
148 for (auto& section : _optionalSections)
149 {
150 section->flatten(pelData);
151 }
Matt Spinlercb6b0592019-07-16 15:58:51 -0500152}
153
Matt Spinler06885452019-11-06 10:35:42 -0600154std::vector<uint8_t> PEL::data() const
Matt Spinlercb6b0592019-07-16 15:58:51 -0500155{
Matt Spinler07eefc52019-09-26 11:18:26 -0500156 std::vector<uint8_t> pelData;
157 flatten(pelData);
158 return pelData;
Matt Spinlercb6b0592019-07-16 15:58:51 -0500159}
160
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600161size_t PEL::size() const
162{
163 size_t size = 0;
164
165 if (_ph)
166 {
167 size += _ph->header().size;
168 }
169
170 if (_uh)
171 {
172 size += _uh->header().size;
173 }
174
175 for (const auto& section : _optionalSections)
176 {
177 size += section->header().size;
178 }
179
180 return size;
181}
182
Matt Spinlerbd716f02019-10-15 10:54:11 -0500183std::optional<SRC*> PEL::primarySRC() const
184{
185 auto src = std::find_if(
186 _optionalSections.begin(), _optionalSections.end(), [](auto& section) {
187 return section->header().id ==
188 static_cast<uint16_t>(SectionID::primarySRC);
189 });
190 if (src != _optionalSections.end())
191 {
192 return static_cast<SRC*>(src->get());
193 }
194
195 return std::nullopt;
196}
197
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500198void PEL::checkRulesAndFix()
199{
200 auto [actionFlags, eventType] =
201 pel_rules::check(_uh->actionFlags(), _uh->eventType(), _uh->severity());
202
203 _uh->setActionFlags(actionFlags);
204 _uh->setEventType(eventType);
205}
206
Matt Spinleracb7c102020-01-10 13:49:22 -0600207void PEL::printSectionInJSON(const Section& section, std::string& buf,
208 std::map<uint16_t, size_t>& pluralSections) const
Aatir186ce8c2019-10-20 15:13:39 -0500209{
210 char tmpB[5];
Aatir Manzurad0e0472019-10-07 13:18:37 -0500211 uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
212 static_cast<uint8_t>(section.header().id)};
213 sprintf(tmpB, "%c%c", id[0], id[1]);
214 std::string sectionID(tmpB);
215 std::string sectionName = pv::sectionTitles.count(sectionID)
216 ? pv::sectionTitles.at(sectionID)
217 : "Unknown Section";
Matt Spinleracb7c102020-01-10 13:49:22 -0600218
219 // Add a count if there are multiple of this type of section
220 auto count = pluralSections.find(section.header().id);
221 if (count != pluralSections.end())
222 {
223 sectionName += " " + std::to_string(count->second);
224 count->second++;
225 }
226
Aatir186ce8c2019-10-20 15:13:39 -0500227 if (section.valid())
228 {
Aatir Manzurad0e0472019-10-07 13:18:37 -0500229 auto json = section.getJSON();
230 if (json)
231 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800232 buf += "\n\"" + sectionName + "\": {\n";
233 buf += *json + "\n},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500234 }
235 else
236 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800237 buf += "\n\"" + sectionName + "\": [\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500238 std::vector<uint8_t> data;
239 Stream s{data};
240 section.flatten(s);
241 std::string dstr = dumpHex(std::data(data), data.size());
242 buf += dstr + "],\n";
243 }
Aatir186ce8c2019-10-20 15:13:39 -0500244 }
245 else
246 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800247 buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n";
Aatir186ce8c2019-10-20 15:13:39 -0500248 }
249}
250
Matt Spinleracb7c102020-01-10 13:49:22 -0600251std::map<uint16_t, size_t> PEL::getPluralSections() const
252{
253 std::map<uint16_t, size_t> sectionCounts;
254
255 for (const auto& section : optionalSections())
256 {
257 if (sectionCounts.find(section->header().id) == sectionCounts.end())
258 {
259 sectionCounts[section->header().id] = 1;
260 }
261 else
262 {
263 sectionCounts[section->header().id]++;
264 }
265 }
266
267 std::map<uint16_t, size_t> sections;
268 for (const auto& [id, count] : sectionCounts)
269 {
270 if (count > 1)
271 {
272 // Start with 0 here and printSectionInJSON()
273 // will increment it as it goes.
274 sections.emplace(id, 0);
275 }
276 }
277
278 return sections;
279}
280
Matt Spinler06885452019-11-06 10:35:42 -0600281void PEL::toJSON() const
Aatir186ce8c2019-10-20 15:13:39 -0500282{
Matt Spinleracb7c102020-01-10 13:49:22 -0600283 auto sections = getPluralSections();
284
Aatir186ce8c2019-10-20 15:13:39 -0500285 std::string buf = "{";
Matt Spinleracb7c102020-01-10 13:49:22 -0600286 printSectionInJSON(*(_ph.get()), buf, sections);
287 printSectionInJSON(*(_uh.get()), buf, sections);
Aatir186ce8c2019-10-20 15:13:39 -0500288 for (auto& section : this->optionalSections())
289 {
Matt Spinleracb7c102020-01-10 13:49:22 -0600290 printSectionInJSON(*(section.get()), buf, sections);
Aatir186ce8c2019-10-20 15:13:39 -0500291 }
292 buf += "}";
293 std::size_t found = buf.rfind(",");
294 if (found != std::string::npos)
295 buf.replace(found, 1, "");
296 std::cout << buf << std::endl;
297}
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800298
Matt Spinlerc7c3e402020-01-22 15:07:25 -0600299namespace util
300{
301
302std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
303{
304 assert(!ad.empty());
305 nlohmann::json json;
306
307 // Remove the 'ESEL' entry, as it contains a full PEL in the value.
308 if (ad.getValue("ESEL"))
309 {
310 auto newAD = ad;
311 newAD.remove("ESEL");
312 json = newAD.toJSON();
313 }
314 else
315 {
316 json = ad.toJSON();
317 }
318
319 auto jsonString = json.dump();
320 std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
321
322 // Pad to a 4 byte boundary
323 while ((jsonData.size() % 4) != 0)
324 {
325 jsonData.push_back(0);
326 }
327
328 return std::make_unique<UserData>(
329 static_cast<uint16_t>(ComponentID::phosphorLogging),
330 static_cast<uint8_t>(UserDataFormat::json),
331 static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
332}
333
334} // namespace util
335
Matt Spinlercb6b0592019-07-16 15:58:51 -0500336} // namespace pels
337} // namespace openpower