Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 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 Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 16 | #include "pel.hpp" |
| 17 | |
| 18 | #include "bcd_time.hpp" |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 19 | #include "extended_user_header.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 20 | #include "failing_mtms.hpp" |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 21 | #include "json_utils.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 22 | #include "log_id.hpp" |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 23 | #include "pel_rules.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 24 | #include "pel_values.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 25 | #include "section_factory.hpp" |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 26 | #include "src.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 27 | #include "stream.hpp" |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 28 | #include "user_data_formats.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 29 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 30 | #include <iostream> |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 31 | #include <phosphor-logging/log.hpp> |
| 32 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 33 | namespace openpower |
| 34 | { |
| 35 | namespace pels |
| 36 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 37 | namespace message = openpower::pels::message; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 38 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 39 | |
| 40 | PEL::PEL(const message::Entry& entry, uint32_t obmcLogID, uint64_t timestamp, |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 41 | phosphor::logging::Entry::Level severity, |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 42 | const AdditionalData& additionalData, |
| 43 | const DataInterfaceBase& dataIface) |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 44 | { |
| 45 | _ph = std::make_unique<PrivateHeader>(entry.componentID, obmcLogID, |
| 46 | timestamp); |
| 47 | _uh = std::make_unique<UserHeader>(entry, severity); |
| 48 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 49 | auto src = std::make_unique<SRC>(entry, additionalData); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 50 | |
| 51 | auto euh = std::make_unique<ExtendedUserHeader>(dataIface, entry, *src); |
| 52 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 53 | _optionalSections.push_back(std::move(src)); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 54 | _optionalSections.push_back(std::move(euh)); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 55 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 56 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 57 | _optionalSections.push_back(std::move(mtms)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 58 | |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 59 | if (!additionalData.empty()) |
| 60 | { |
| 61 | auto ud = util::makeADUserDataSection(additionalData); |
Matt Spinler | 6d66382 | 2020-01-22 14:50:46 -0600 | [diff] [blame^] | 62 | |
| 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 Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 70 | _ph->setSectionCount(2 + _optionalSections.size()); |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 71 | |
| 72 | checkRulesAndFix(); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 73 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 74 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 75 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 76 | { |
| 77 | } |
| 78 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 79 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 80 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 81 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 84 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 85 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 86 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 87 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 88 | if (obmcLogID != 0) |
| 89 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 90 | _ph->setOBMCLogID(obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 94 | |
| 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 Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool PEL::valid() const |
| 104 | { |
| 105 | bool valid = _ph->valid(); |
| 106 | |
| 107 | if (valid) |
| 108 | { |
| 109 | valid = _uh->valid(); |
| 110 | } |
| 111 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 112 | 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 Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 121 | return valid; |
| 122 | } |
| 123 | |
| 124 | void PEL::setCommitTime() |
| 125 | { |
| 126 | auto now = std::chrono::system_clock::now(); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 127 | _ph->setCommitTimestamp(getBCDTime(now)); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void PEL::assignID() |
| 131 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 132 | _ph->setID(generatePELID()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 135 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 136 | { |
| 137 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 138 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 139 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 140 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 141 | using namespace phosphor::logging; |
| 142 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 145 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 146 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 147 | |
| 148 | for (auto& section : _optionalSections) |
| 149 | { |
| 150 | section->flatten(pelData); |
| 151 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 154 | std::vector<uint8_t> PEL::data() const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 155 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 156 | std::vector<uint8_t> pelData; |
| 157 | flatten(pelData); |
| 158 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Matt Spinler | f1b46ff | 2020-01-22 14:10:04 -0600 | [diff] [blame] | 161 | size_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 Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 183 | std::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 Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 198 | void 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 Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 207 | namespace util |
| 208 | { |
| 209 | |
| 210 | std::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 Spinler | b466f78 | 2019-11-08 10:44:01 -0600 | [diff] [blame] | 230 | // Pad to a 4 byte boundary |
| 231 | while ((jsonData.size() % 4) != 0) |
| 232 | { |
| 233 | jsonData.push_back(0); |
| 234 | } |
| 235 | |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 236 | 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 Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 243 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 244 | void PEL::printSectionInJSON(const Section& section, std::string& buf, |
| 245 | std::map<uint16_t, size_t>& pluralSections) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 246 | { |
| 247 | char tmpB[5]; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 248 | 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 Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 255 | |
| 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 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 264 | if (section.valid()) |
| 265 | { |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 266 | auto json = section.getJSON(); |
| 267 | if (json) |
| 268 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 269 | buf += "\n\"" + sectionName + "\": {\n"; |
| 270 | buf += *json + "\n},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 271 | } |
| 272 | else |
| 273 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 274 | buf += "\n\"" + sectionName + "\": [\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 275 | 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 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 281 | } |
| 282 | else |
| 283 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 284 | buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n"; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 288 | std::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 Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 318 | void PEL::toJSON() const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 319 | { |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 320 | auto sections = getPluralSections(); |
| 321 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 322 | std::string buf = "{"; |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 323 | printSectionInJSON(*(_ph.get()), buf, sections); |
| 324 | printSectionInJSON(*(_uh.get()), buf, sections); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 325 | for (auto& section : this->optionalSections()) |
| 326 | { |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 327 | printSectionInJSON(*(section.get()), buf, sections); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 328 | } |
| 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 Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 335 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 336 | } // namespace pels |
| 337 | } // namespace openpower |