blob: 6d3f0103fd809605a918c1b187561308d630053c [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 Spinlerafa857c2019-10-24 13:03:46 -0500207namespace util
208{
209
210std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
211{
212 assert(!ad.empty());
213 nlohmann::json json;
214
215 // Remove the 'ESEL' entry, as it contains a full PEL in the value.
216 if (ad.getValue("ESEL"))
217 {
218 auto newAD = ad;
219 newAD.remove("ESEL");
220 json = newAD.toJSON();
221 }
222 else
223 {
224 json = ad.toJSON();
225 }
226
227 auto jsonString = json.dump();
228 std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
229
Matt Spinlerb466f782019-11-08 10:44:01 -0600230 // Pad to a 4 byte boundary
231 while ((jsonData.size() % 4) != 0)
232 {
233 jsonData.push_back(0);
234 }
235
Matt Spinlerafa857c2019-10-24 13:03:46 -0500236 return std::make_unique<UserData>(
237 static_cast<uint16_t>(ComponentID::phosphorLogging),
238 static_cast<uint8_t>(UserDataFormat::json),
239 static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
240}
241
242} // namespace util
Matt Spinler06885452019-11-06 10:35:42 -0600243
Matt Spinleracb7c102020-01-10 13:49:22 -0600244void PEL::printSectionInJSON(const Section& section, std::string& buf,
245 std::map<uint16_t, size_t>& pluralSections) const
Aatir186ce8c2019-10-20 15:13:39 -0500246{
247 char tmpB[5];
Aatir Manzurad0e0472019-10-07 13:18:37 -0500248 uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
249 static_cast<uint8_t>(section.header().id)};
250 sprintf(tmpB, "%c%c", id[0], id[1]);
251 std::string sectionID(tmpB);
252 std::string sectionName = pv::sectionTitles.count(sectionID)
253 ? pv::sectionTitles.at(sectionID)
254 : "Unknown Section";
Matt Spinleracb7c102020-01-10 13:49:22 -0600255
256 // Add a count if there are multiple of this type of section
257 auto count = pluralSections.find(section.header().id);
258 if (count != pluralSections.end())
259 {
260 sectionName += " " + std::to_string(count->second);
261 count->second++;
262 }
263
Aatir186ce8c2019-10-20 15:13:39 -0500264 if (section.valid())
265 {
Aatir Manzurad0e0472019-10-07 13:18:37 -0500266 auto json = section.getJSON();
267 if (json)
268 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800269 buf += "\n\"" + sectionName + "\": {\n";
270 buf += *json + "\n},\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500271 }
272 else
273 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800274 buf += "\n\"" + sectionName + "\": [\n";
Aatir Manzurad0e0472019-10-07 13:18:37 -0500275 std::vector<uint8_t> data;
276 Stream s{data};
277 section.flatten(s);
278 std::string dstr = dumpHex(std::data(data), data.size());
279 buf += dstr + "],\n";
280 }
Aatir186ce8c2019-10-20 15:13:39 -0500281 }
282 else
283 {
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800284 buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n";
Aatir186ce8c2019-10-20 15:13:39 -0500285 }
286}
287
Matt Spinleracb7c102020-01-10 13:49:22 -0600288std::map<uint16_t, size_t> PEL::getPluralSections() const
289{
290 std::map<uint16_t, size_t> sectionCounts;
291
292 for (const auto& section : optionalSections())
293 {
294 if (sectionCounts.find(section->header().id) == sectionCounts.end())
295 {
296 sectionCounts[section->header().id] = 1;
297 }
298 else
299 {
300 sectionCounts[section->header().id]++;
301 }
302 }
303
304 std::map<uint16_t, size_t> sections;
305 for (const auto& [id, count] : sectionCounts)
306 {
307 if (count > 1)
308 {
309 // Start with 0 here and printSectionInJSON()
310 // will increment it as it goes.
311 sections.emplace(id, 0);
312 }
313 }
314
315 return sections;
316}
317
Matt Spinler06885452019-11-06 10:35:42 -0600318void PEL::toJSON() const
Aatir186ce8c2019-10-20 15:13:39 -0500319{
Matt Spinleracb7c102020-01-10 13:49:22 -0600320 auto sections = getPluralSections();
321
Aatir186ce8c2019-10-20 15:13:39 -0500322 std::string buf = "{";
Matt Spinleracb7c102020-01-10 13:49:22 -0600323 printSectionInJSON(*(_ph.get()), buf, sections);
324 printSectionInJSON(*(_uh.get()), buf, sections);
Aatir186ce8c2019-10-20 15:13:39 -0500325 for (auto& section : this->optionalSections())
326 {
Matt Spinleracb7c102020-01-10 13:49:22 -0600327 printSectionInJSON(*(section.get()), buf, sections);
Aatir186ce8c2019-10-20 15:13:39 -0500328 }
329 buf += "}";
330 std::size_t found = buf.rfind(",");
331 if (found != std::string::npos)
332 buf.replace(found, 1, "");
333 std::cout << buf << std::endl;
334}
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800335
Matt Spinlercb6b0592019-07-16 15:58:51 -0500336} // namespace pels
337} // namespace openpower