blob: eeaf37913e1a3370ad9f26821529be8dd307d9d7 [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 Spinler03c1d912019-07-10 14:12:15 -050016#include "user_header.hpp"
17
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +080018#include "json_utils.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050019#include "pel_types.hpp"
Aatirc1489352019-12-09 13:13:20 -060020#include "pel_values.hpp"
Matt Spinlerfdb6a202019-09-20 14:09:20 -050021#include "severity.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050022
Arya K Padman5bc26532024-04-10 06:19:25 -050023#include <phosphor-logging/lg2.hpp>
Matt Spinler03c1d912019-07-10 14:12:15 -050024
25namespace openpower
26{
27namespace pels
28{
29
Aatirc1489352019-12-09 13:13:20 -060030namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050031
Matt Spinlercf5a8d02019-09-05 12:58:53 -050032void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050033{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050034 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
35 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060036 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050037}
38
Matt Spinler06885452019-11-06 10:35:42 -060039void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050040{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050041 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
42 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060043 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050044}
45
Matt Spinlerfdb6a202019-09-20 14:09:20 -050046UserHeader::UserHeader(const message::Entry& entry,
Matt Spinleraadccc82020-04-10 14:33:42 -050047 phosphor::logging::Entry::Level severity,
Vijay Lobo6b3f3452021-04-15 23:04:42 -050048 const AdditionalData& additionalData,
Matt Spinleraadccc82020-04-10 14:33:42 -050049 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050050{
51 _header.id = static_cast<uint16_t>(SectionID::userHeader);
52 _header.size = UserHeader::flattenedSize();
53 _header.version = userHeaderVersion;
54 _header.subType = 0;
55 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
56
Matt Spinler23970b02022-02-25 16:34:46 -060057 std::optional<uint8_t> subsys;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050058
Sumit Kumar50bfa692022-01-06 06:48:26 -060059 // Check for additional data - PEL_SUBSYSTEM
60 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
61 if (ss)
62 {
Matt Spinler6f07df32025-05-09 11:42:39 -050063 auto eventSubsystem = std::stoul(*ss, nullptr, 16);
Patrick Williams075c7922024-08-16 15:19:49 -040064 std::string subsystemString =
65 pv::getValue(eventSubsystem, pel_values::subsystemValues);
Matt Spinler23970b02022-02-25 16:34:46 -060066 if (subsystemString == "invalid")
Sumit Kumar50bfa692022-01-06 06:48:26 -060067 {
Arya K Padman5bc26532024-04-10 06:19:25 -050068 lg2::warning(
69 "UH: Invalid SubSystem value in PEL_SUBSYSTEM: {PEL_SUBSYSTEM}",
70 "PEL_SUBSYSTEM", lg2::hex, eventSubsystem);
Sumit Kumar50bfa692022-01-06 06:48:26 -060071 }
72 else
73 {
Matt Spinler23970b02022-02-25 16:34:46 -060074 subsys = eventSubsystem;
Sumit Kumar50bfa692022-01-06 06:48:26 -060075 }
76 }
Matt Spinler23970b02022-02-25 16:34:46 -060077 else
78 {
79 subsys = entry.subsystem;
80 }
81
82 if (subsys)
83 {
84 _eventSubsystem = *subsys;
85 }
86 else
87 {
88 // Gotta use something, how about 'others'.
Arya K Padman5bc26532024-04-10 06:19:25 -050089 lg2::warning(
Matt Spinler23970b02022-02-25 16:34:46 -060090 "No PEL subystem value supplied for error, using 'others'");
91 _eventSubsystem = 0x70;
92 }
Sumit Kumar50bfa692022-01-06 06:48:26 -060093
Matt Spinlerfdb6a202019-09-20 14:09:20 -050094 _eventScope = entry.eventScope.value_or(
95 static_cast<uint8_t>(EventScope::entirePlatform));
96
Matt Spinleraadccc82020-04-10 14:33:42 -050097 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050098 bool mfgSevStatus = false;
99 bool mfgActionFlagStatus = false;
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500100
101 // Get the mfg severity & action flags
102 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -0500103 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500104 std::optional<uint8_t> sev = std::nullopt;
105 uint16_t val = 0;
106
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500107 if (entry.mfgSeverity)
108 {
109 // Find the mf severity possibly dependent on the system type.
110 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
111 }
112
113 if (entry.mfgActionFlags)
114 {
115 // Find the mfg action flags
116 val = entry.mfgActionFlags.value();
117 }
118
119 if (sev || val)
120 {
121 bool mfgProp = dataIface.getQuiesceOnError();
122 if (mfgProp)
123 {
124 if (sev)
125 {
126 _eventSeverity = *sev;
127 mfgSevStatus = true;
128 }
129
130 if (val)
131 {
132 _actionFlags = val;
133 mfgActionFlagStatus = true;
134 }
135 }
136 }
137 }
138
139 if (!mfgSevStatus)
140 {
141 // Get the severity from the registry if it's there, otherwise get
142 // it from the OpenBMC event log severity value.
143 if (!entry.severity)
144 {
145 _eventSeverity = convertOBMCSeverityToPEL(severity);
146 }
147 else
148 {
149 // Find the severity possibly dependent on the system type.
150 auto sev = getSeverity(entry.severity.value(), dataIface);
151 if (sev)
152 {
153 _eventSeverity = *sev;
154 }
155 else
156 {
157 // Either someone screwed up the message registry
158 // or getSystemNames failed.
Arya K Padman5bc26532024-04-10 06:19:25 -0500159 lg2::error(
160 "Failed finding the severity in the message registry ERROR={ERROR}",
161 "ERROR", entry.name);
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500162
163 // Have to choose something, just use informational.
164 _eventSeverity = 0;
165 }
166 }
167 }
168
169 // Convert Critical error (0x50) to Critical Error-System Termination
170 // (0x51), if the AdditionalData is set to SYSTEM_TERM
171 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
172 if ((_eventSeverity & 0xF0) == 0x50)
173 {
174 if (sevLevel.value_or("") == "SYSTEM_TERM")
175 {
176 // Change to Critical Error, System Termination
177 _eventSeverity = 0x51;
178 }
179 }
180
181 if (entry.eventType)
182 {
183 _eventType = *entry.eventType;
Matt Spinleraadccc82020-04-10 14:33:42 -0500184 }
185 else
186 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500187 // There are different default event types for info errors
188 // vs non info ones.
189 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
190 _eventType =
191 (sevType == SeverityType::nonError)
192 ? static_cast<uint8_t>(EventType::miscInformational)
193 : static_cast<uint8_t>(EventType::notApplicable);
Matt Spinleraadccc82020-04-10 14:33:42 -0500194 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500195
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500196 _reserved4Byte1 = 0;
197
198 // No uses for problem domain or vector
199 _problemDomain = 0;
200 _problemVector = 0;
201
202 // These will be set in pel_rules::check() if they're still
203 // at the default value.
204 if (!mfgActionFlagStatus)
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500205 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500206 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500207 }
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500208
209 _states = 0;
210
211 _valid = true;
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500212 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500213}
214
Matt Spinler03c1d912019-07-10 14:12:15 -0500215UserHeader::UserHeader(Stream& pel)
216{
217 try
218 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500219 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500220 validate();
221 }
222 catch (const std::exception& e)
223 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500224 lg2::error("Cannot unflatten user header: {EXCEPTION}", "EXCEPTION", e);
Matt Spinler03c1d912019-07-10 14:12:15 -0500225 _valid = false;
226 }
227}
228
229void UserHeader::validate()
230{
231 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500232 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500233 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500234 lg2::error("Invalid user header section ID: {HEADER_ID}", "HEADER_ID",
235 lg2::hex, header().id);
Matt Spinler03c1d912019-07-10 14:12:15 -0500236 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500237 }
238
239 if (header().version != userHeaderVersion)
240 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500241 lg2::error("Invalid user header version: {HEADER_VERSION}",
242 "HEADER_VERSION", lg2::hex, header().version);
Matt Spinler03c1d912019-07-10 14:12:15 -0500243 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500244 }
245
246 _valid = (failed) ? false : true;
247}
248
Matt Spinlerb832aa52023-03-21 15:32:34 -0500249std::optional<std::string> UserHeader::getJSON(uint8_t creatorID) const
Aatir Manzurad0e0472019-10-07 13:18:37 -0500250{
251 std::string severity;
252 std::string subsystem;
253 std::string eventScope;
254 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800255 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600256 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
257 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
258 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
259 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Patrick Williams075c7922024-08-16 15:19:49 -0400260 actionFlags =
261 pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600262
263 std::string hostState{"Invalid"};
Vijay Lobo2fb10212021-08-22 23:24:16 -0500264 std::string hmcState{"Invalid"};
Matt Spinler455587e2020-01-15 14:31:52 -0600265 auto iter = pv::transmissionStates.find(
266 static_cast<TransmissionState>(hostTransmissionState()));
267 if (iter != pv::transmissionStates.end())
268 {
269 hostState = iter->second;
270 }
Vijay Lobo2fb10212021-08-22 23:24:16 -0500271 auto iter1 = pv::transmissionStates.find(
272 static_cast<TransmissionState>(hmcTransmissionState()));
273 if (iter1 != pv::transmissionStates.end())
274 {
275 hmcState = iter1->second;
276 }
Matt Spinler455587e2020-01-15 14:31:52 -0600277
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800278 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800279 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
280 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
281 jsonInsert(uh, "Log Committed by",
Matt Spinlerb832aa52023-03-21 15:32:34 -0500282 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800283 jsonInsert(uh, "Subsystem", subsystem, 1);
284 jsonInsert(uh, "Event Scope", eventScope, 1);
285 jsonInsert(uh, "Event Severity", severity, 1);
286 jsonInsert(uh, "Event Type", eventType, 1);
287 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600288 jsonInsert(uh, "Host Transmission", hostState, 1);
Vijay Lobo2fb10212021-08-22 23:24:16 -0500289 jsonInsert(uh, "HMC Transmission", hmcState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800290 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500291 return uh;
292}
Matt Spinleraadccc82020-04-10 14:33:42 -0500293
294std::optional<uint8_t> UserHeader::getSeverity(
295 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500296 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500297{
298 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500299 std::vector<std::string> systemNames;
300
301 // getSystemNames makes D-Bus calls, so only call it if we
302 // know we'll need it because there is a system name in the sev list
303 if (std::any_of(severities.begin(), severities.end(),
304 [](const auto& sev) { return !sev.system.empty(); }))
305 {
306 try
307 {
308 systemNames = dataIface.getSystemNames();
309 }
310 catch (const std::exception& e)
311 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500312 lg2::error(
313 "Failed trying to look up system names on D-Bus ERROR={ERROR}",
314 "ERROR", e);
Matt Spinler1ab66962020-10-29 13:21:44 -0500315 return std::nullopt;
316 }
317 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500318
319 // Find the severity to use for this system type, or use the default
320 // entry (where no system type is specified).
321 for (const auto& sev : severities)
322 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500323 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
324 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500325 {
326 s = &sev.severity;
327 break;
328 }
329 else if (sev.system.empty())
330 {
331 s = &sev.severity;
332 }
333 }
334
335 if (s)
336 {
337 return *s;
338 }
339
340 return std::nullopt;
341}
342
Matt Spinler03c1d912019-07-10 14:12:15 -0500343} // namespace pels
344} // namespace openpower