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 | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 16 | #include "user_header.hpp" |
| 17 | |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 18 | #include "json_utils.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 19 | #include "pel_types.hpp" |
Aatir | c148935 | 2019-12-09 13:13:20 -0600 | [diff] [blame] | 20 | #include "pel_values.hpp" |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 21 | #include "severity.hpp" |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 22 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 23 | #include <iostream> |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 24 | #include <phosphor-logging/log.hpp> |
| 25 | |
| 26 | namespace openpower |
| 27 | { |
| 28 | namespace pels |
| 29 | { |
| 30 | |
Aatir | c148935 | 2019-12-09 13:13:20 -0600 | [diff] [blame] | 31 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 32 | using namespace phosphor::logging; |
| 33 | |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 34 | void UserHeader::unflatten(Stream& stream) |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 35 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 36 | stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >> |
| 37 | _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >> |
Matt Spinler | eb11144 | 2019-11-07 13:05:36 -0600 | [diff] [blame] | 38 | _actionFlags >> _states; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 41 | void UserHeader::flatten(Stream& stream) const |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 42 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 43 | stream << _header << _eventSubsystem << _eventScope << _eventSeverity |
| 44 | << _eventType << _reserved4Byte1 << _problemDomain << _problemVector |
Matt Spinler | eb11144 | 2019-11-07 13:05:36 -0600 | [diff] [blame] | 45 | << _actionFlags << _states; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 48 | UserHeader::UserHeader(const message::Entry& entry, |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 49 | phosphor::logging::Entry::Level severity, |
| 50 | const DataInterfaceBase& dataIface) |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 51 | { |
| 52 | _header.id = static_cast<uint16_t>(SectionID::userHeader); |
| 53 | _header.size = UserHeader::flattenedSize(); |
| 54 | _header.version = userHeaderVersion; |
| 55 | _header.subType = 0; |
| 56 | _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 57 | |
| 58 | _eventSubsystem = entry.subsystem; |
| 59 | |
| 60 | _eventScope = entry.eventScope.value_or( |
| 61 | static_cast<uint8_t>(EventScope::entirePlatform)); |
| 62 | |
| 63 | // Get the severity from the registry if it's there, otherwise get it |
| 64 | // from the OpenBMC event log severity value. |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 65 | if (!entry.severity) |
| 66 | { |
| 67 | _eventSeverity = convertOBMCSeverityToPEL(severity); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | // Find the severity possibly dependent on the system type. |
| 72 | auto sev = |
Matt Spinler | 6ea4d5f | 2020-05-20 13:31:07 -0500 | [diff] [blame] | 73 | getSeverity(entry.severity.value(), dataIface.getSystemNames()); |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 74 | if (sev) |
| 75 | { |
| 76 | _eventSeverity = *sev; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | // Someone screwed up the message registry. |
Matt Spinler | 6ea4d5f | 2020-05-20 13:31:07 -0500 | [diff] [blame] | 81 | std::string types; |
| 82 | const auto& compatibles = dataIface.getSystemNames(); |
| 83 | std::for_each(compatibles.begin(), compatibles.end(), |
| 84 | [&types](const auto& t) { types += t + '|'; }); |
| 85 | |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 86 | log<level::ERR>( |
Matt Spinler | 6ea4d5f | 2020-05-20 13:31:07 -0500 | [diff] [blame] | 87 | "No severity entry found for this error and system name", |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 88 | phosphor::logging::entry("ERROR=%s", entry.name.c_str()), |
Matt Spinler | 6ea4d5f | 2020-05-20 13:31:07 -0500 | [diff] [blame] | 89 | phosphor::logging::entry("SYSTEMNAMES=%s", types.c_str())); |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 90 | |
| 91 | // Have to choose something, just use informational. |
| 92 | _eventSeverity = 0; |
| 93 | } |
| 94 | } |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 95 | |
| 96 | // TODO: ibm-dev/dev/#1144 Handle manufacturing sev & action flags |
| 97 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 98 | if (entry.eventType) |
| 99 | { |
| 100 | _eventType = *entry.eventType; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | // There are different default event types for info errors |
| 105 | // vs non info ones. |
| 106 | auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0); |
| 107 | |
| 108 | _eventType = (sevType == SeverityType::nonError) |
| 109 | ? static_cast<uint8_t>(EventType::miscInformational) |
| 110 | : static_cast<uint8_t>(EventType::notApplicable); |
| 111 | } |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 112 | |
| 113 | _reserved4Byte1 = 0; |
| 114 | |
| 115 | // No uses for problem domain or vector |
| 116 | _problemDomain = 0; |
| 117 | _problemVector = 0; |
| 118 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 119 | // These will be cleaned up later in pel_rules::check() |
Matt Spinler | e07f915 | 2019-11-01 10:48:36 -0500 | [diff] [blame] | 120 | _actionFlags = entry.actionFlags.value_or(0); |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 121 | |
Matt Spinler | eb11144 | 2019-11-07 13:05:36 -0600 | [diff] [blame] | 122 | _states = 0; |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 123 | |
| 124 | _valid = true; |
| 125 | } |
| 126 | |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 127 | UserHeader::UserHeader(Stream& pel) |
| 128 | { |
| 129 | try |
| 130 | { |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 131 | unflatten(pel); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 132 | validate(); |
| 133 | } |
| 134 | catch (const std::exception& e) |
| 135 | { |
| 136 | log<level::ERR>("Cannot unflatten user header", |
| 137 | entry("ERROR=%s", e.what())); |
| 138 | _valid = false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void UserHeader::validate() |
| 143 | { |
| 144 | bool failed = false; |
Matt Spinler | 1a94cc3 | 2019-09-11 13:32:12 -0500 | [diff] [blame] | 145 | if (header().id != static_cast<uint16_t>(SectionID::userHeader)) |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 146 | { |
| 147 | log<level::ERR>("Invalid user header section ID", |
| 148 | entry("ID=0x%X", header().id)); |
| 149 | failed = true; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (header().version != userHeaderVersion) |
| 153 | { |
| 154 | log<level::ERR>("Invalid user header version", |
| 155 | entry("VERSION=0x%X", header().version)); |
| 156 | failed = true; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | _valid = (failed) ? false : true; |
| 160 | } |
| 161 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 162 | std::optional<std::string> UserHeader::getJSON() const |
| 163 | { |
| 164 | std::string severity; |
| 165 | std::string subsystem; |
| 166 | std::string eventScope; |
| 167 | std::string eventType; |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 168 | std::vector<std::string> actionFlags; |
Aatir | c148935 | 2019-12-09 13:13:20 -0600 | [diff] [blame] | 169 | severity = pv::getValue(_eventSeverity, pel_values::severityValues); |
| 170 | subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues); |
| 171 | eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues); |
| 172 | eventType = pv::getValue(_eventType, pel_values::eventTypeValues); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 173 | actionFlags = |
| 174 | pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues); |
Matt Spinler | 455587e | 2020-01-15 14:31:52 -0600 | [diff] [blame] | 175 | |
| 176 | std::string hostState{"Invalid"}; |
| 177 | auto iter = pv::transmissionStates.find( |
| 178 | static_cast<TransmissionState>(hostTransmissionState())); |
| 179 | if (iter != pv::transmissionStates.end()) |
| 180 | { |
| 181 | hostState = iter->second; |
| 182 | } |
| 183 | |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 184 | std::string uh; |
Harisuddin Mohamed Isa | bebeb94 | 2020-03-12 17:12:24 +0800 | [diff] [blame] | 185 | jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1); |
| 186 | jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1); |
| 187 | jsonInsert(uh, "Log Committed by", |
| 188 | getNumberString("0x%X", _header.componentID), 1); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 189 | jsonInsert(uh, "Subsystem", subsystem, 1); |
| 190 | jsonInsert(uh, "Event Scope", eventScope, 1); |
| 191 | jsonInsert(uh, "Event Severity", severity, 1); |
| 192 | jsonInsert(uh, "Event Type", eventType, 1); |
| 193 | jsonInsertArray(uh, "Action Flags", actionFlags, 1); |
Matt Spinler | 455587e | 2020-01-15 14:31:52 -0600 | [diff] [blame] | 194 | jsonInsert(uh, "Host Transmission", hostState, 1); |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 195 | uh.erase(uh.size() - 2); |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 196 | return uh; |
| 197 | } |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 198 | |
| 199 | std::optional<uint8_t> UserHeader::getSeverity( |
| 200 | const std::vector<message::RegistrySeverity>& severities, |
Matt Spinler | 6ea4d5f | 2020-05-20 13:31:07 -0500 | [diff] [blame] | 201 | const std::vector<std::string>& systemNames) const |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 202 | { |
| 203 | const uint8_t* s = nullptr; |
| 204 | |
| 205 | // Find the severity to use for this system type, or use the default |
| 206 | // entry (where no system type is specified). |
| 207 | for (const auto& sev : severities) |
| 208 | { |
Matt Spinler | 6ea4d5f | 2020-05-20 13:31:07 -0500 | [diff] [blame] | 209 | if (std::find(systemNames.begin(), systemNames.end(), sev.system) != |
| 210 | systemNames.end()) |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 211 | { |
| 212 | s = &sev.severity; |
| 213 | break; |
| 214 | } |
| 215 | else if (sev.system.empty()) |
| 216 | { |
| 217 | s = &sev.severity; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (s) |
| 222 | { |
| 223 | return *s; |
| 224 | } |
| 225 | |
| 226 | return std::nullopt; |
| 227 | } |
| 228 | |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 229 | } // namespace pels |
| 230 | } // namespace openpower |