blob: 2a153217e30bb04230f21000f59be474cec80dec [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3
Matt Spinler03c1d912019-07-10 14:12:15 -05004#include "user_header.hpp"
5
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +08006#include "json_utils.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -05007#include "pel_types.hpp"
Aatirc1489352019-12-09 13:13:20 -06008#include "pel_values.hpp"
Matt Spinlerfdb6a202019-09-20 14:09:20 -05009#include "severity.hpp"
Matt Spinler1a94cc32019-09-11 13:32:12 -050010
Arya K Padman5bc26532024-04-10 06:19:25 -050011#include <phosphor-logging/lg2.hpp>
Matt Spinler03c1d912019-07-10 14:12:15 -050012
13namespace openpower
14{
15namespace pels
16{
17
Aatirc1489352019-12-09 13:13:20 -060018namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050019
Matt Spinlercf5a8d02019-09-05 12:58:53 -050020void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050021{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050022 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
23 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060024 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050025}
26
Matt Spinler06885452019-11-06 10:35:42 -060027void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050028{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050029 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
30 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060031 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050032}
33
Matt Spinlerfdb6a202019-09-20 14:09:20 -050034UserHeader::UserHeader(const message::Entry& entry,
Matt Spinleraadccc82020-04-10 14:33:42 -050035 phosphor::logging::Entry::Level severity,
Vijay Lobo6b3f3452021-04-15 23:04:42 -050036 const AdditionalData& additionalData,
Matt Spinleraadccc82020-04-10 14:33:42 -050037 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050038{
39 _header.id = static_cast<uint16_t>(SectionID::userHeader);
40 _header.size = UserHeader::flattenedSize();
41 _header.version = userHeaderVersion;
42 _header.subType = 0;
43 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
44
Matt Spinler23970b02022-02-25 16:34:46 -060045 std::optional<uint8_t> subsys;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050046
Sumit Kumar50bfa692022-01-06 06:48:26 -060047 // Check for additional data - PEL_SUBSYSTEM
48 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
49 if (ss)
50 {
Matt Spinler6f07df32025-05-09 11:42:39 -050051 auto eventSubsystem = std::stoul(*ss, nullptr, 16);
Patrick Williams075c7922024-08-16 15:19:49 -040052 std::string subsystemString =
53 pv::getValue(eventSubsystem, pel_values::subsystemValues);
Matt Spinler23970b02022-02-25 16:34:46 -060054 if (subsystemString == "invalid")
Sumit Kumar50bfa692022-01-06 06:48:26 -060055 {
Arya K Padman5bc26532024-04-10 06:19:25 -050056 lg2::warning(
57 "UH: Invalid SubSystem value in PEL_SUBSYSTEM: {PEL_SUBSYSTEM}",
58 "PEL_SUBSYSTEM", lg2::hex, eventSubsystem);
Sumit Kumar50bfa692022-01-06 06:48:26 -060059 }
60 else
61 {
Matt Spinler23970b02022-02-25 16:34:46 -060062 subsys = eventSubsystem;
Sumit Kumar50bfa692022-01-06 06:48:26 -060063 }
64 }
Matt Spinler23970b02022-02-25 16:34:46 -060065 else
66 {
67 subsys = entry.subsystem;
68 }
69
70 if (subsys)
71 {
72 _eventSubsystem = *subsys;
73 }
74 else
75 {
76 // Gotta use something, how about 'others'.
Arya K Padman5bc26532024-04-10 06:19:25 -050077 lg2::warning(
Matt Spinler23970b02022-02-25 16:34:46 -060078 "No PEL subystem value supplied for error, using 'others'");
79 _eventSubsystem = 0x70;
80 }
Sumit Kumar50bfa692022-01-06 06:48:26 -060081
Matt Spinlerfdb6a202019-09-20 14:09:20 -050082 _eventScope = entry.eventScope.value_or(
83 static_cast<uint8_t>(EventScope::entirePlatform));
84
Matt Spinleraadccc82020-04-10 14:33:42 -050085 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050086 bool mfgSevStatus = false;
87 bool mfgActionFlagStatus = false;
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050088
89 // Get the mfg severity & action flags
90 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -050091 {
Matt Spinlerbe952d22022-07-01 11:30:11 -050092 std::optional<uint8_t> sev = std::nullopt;
93 uint16_t val = 0;
94
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050095 if (entry.mfgSeverity)
96 {
97 // Find the mf severity possibly dependent on the system type.
98 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
99 }
100
101 if (entry.mfgActionFlags)
102 {
103 // Find the mfg action flags
104 val = entry.mfgActionFlags.value();
105 }
106
107 if (sev || val)
108 {
109 bool mfgProp = dataIface.getQuiesceOnError();
110 if (mfgProp)
111 {
112 if (sev)
113 {
114 _eventSeverity = *sev;
115 mfgSevStatus = true;
116 }
117
118 if (val)
119 {
120 _actionFlags = val;
121 mfgActionFlagStatus = true;
122 }
123 }
124 }
125 }
126
127 if (!mfgSevStatus)
128 {
129 // Get the severity from the registry if it's there, otherwise get
130 // it from the OpenBMC event log severity value.
131 if (!entry.severity)
132 {
133 _eventSeverity = convertOBMCSeverityToPEL(severity);
134 }
135 else
136 {
137 // Find the severity possibly dependent on the system type.
138 auto sev = getSeverity(entry.severity.value(), dataIface);
139 if (sev)
140 {
141 _eventSeverity = *sev;
142 }
143 else
144 {
145 // Either someone screwed up the message registry
146 // or getSystemNames failed.
Arya K Padman5bc26532024-04-10 06:19:25 -0500147 lg2::error(
148 "Failed finding the severity in the message registry ERROR={ERROR}",
149 "ERROR", entry.name);
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500150
151 // Have to choose something, just use informational.
152 _eventSeverity = 0;
153 }
154 }
155 }
156
157 // Convert Critical error (0x50) to Critical Error-System Termination
158 // (0x51), if the AdditionalData is set to SYSTEM_TERM
159 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
160 if ((_eventSeverity & 0xF0) == 0x50)
161 {
162 if (sevLevel.value_or("") == "SYSTEM_TERM")
163 {
164 // Change to Critical Error, System Termination
165 _eventSeverity = 0x51;
166 }
167 }
168
169 if (entry.eventType)
170 {
171 _eventType = *entry.eventType;
Matt Spinleraadccc82020-04-10 14:33:42 -0500172 }
173 else
174 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500175 // There are different default event types for info errors
176 // vs non info ones.
177 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
178 _eventType =
179 (sevType == SeverityType::nonError)
180 ? static_cast<uint8_t>(EventType::miscInformational)
181 : static_cast<uint8_t>(EventType::notApplicable);
Matt Spinleraadccc82020-04-10 14:33:42 -0500182 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500183
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500184 _reserved4Byte1 = 0;
185
186 // No uses for problem domain or vector
187 _problemDomain = 0;
188 _problemVector = 0;
189
190 // These will be set in pel_rules::check() if they're still
191 // at the default value.
192 if (!mfgActionFlagStatus)
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500193 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500194 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500195 }
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500196
197 _states = 0;
198
199 _valid = true;
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500200 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500201}
202
Matt Spinler03c1d912019-07-10 14:12:15 -0500203UserHeader::UserHeader(Stream& pel)
204{
205 try
206 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500207 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500208 validate();
209 }
210 catch (const std::exception& e)
211 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500212 lg2::error("Cannot unflatten user header: {EXCEPTION}", "EXCEPTION", e);
Matt Spinler03c1d912019-07-10 14:12:15 -0500213 _valid = false;
214 }
215}
216
217void UserHeader::validate()
218{
219 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500220 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500221 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500222 lg2::error("Invalid user header section ID: {HEADER_ID}", "HEADER_ID",
223 lg2::hex, header().id);
Matt Spinler03c1d912019-07-10 14:12:15 -0500224 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500225 }
226
227 if (header().version != userHeaderVersion)
228 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500229 lg2::error("Invalid user header version: {HEADER_VERSION}",
230 "HEADER_VERSION", lg2::hex, header().version);
Matt Spinler03c1d912019-07-10 14:12:15 -0500231 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500232 }
233
234 _valid = (failed) ? false : true;
235}
236
Matt Spinlerb832aa52023-03-21 15:32:34 -0500237std::optional<std::string> UserHeader::getJSON(uint8_t creatorID) const
Aatir Manzurad0e0472019-10-07 13:18:37 -0500238{
239 std::string severity;
240 std::string subsystem;
241 std::string eventScope;
242 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800243 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600244 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
245 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
246 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
247 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Patrick Williams075c7922024-08-16 15:19:49 -0400248 actionFlags =
249 pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600250
251 std::string hostState{"Invalid"};
Vijay Lobo2fb10212021-08-22 23:24:16 -0500252 std::string hmcState{"Invalid"};
Matt Spinler455587e2020-01-15 14:31:52 -0600253 auto iter = pv::transmissionStates.find(
254 static_cast<TransmissionState>(hostTransmissionState()));
255 if (iter != pv::transmissionStates.end())
256 {
257 hostState = iter->second;
258 }
Vijay Lobo2fb10212021-08-22 23:24:16 -0500259 auto iter1 = pv::transmissionStates.find(
260 static_cast<TransmissionState>(hmcTransmissionState()));
261 if (iter1 != pv::transmissionStates.end())
262 {
263 hmcState = iter1->second;
264 }
Matt Spinler455587e2020-01-15 14:31:52 -0600265
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800266 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800267 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
268 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
269 jsonInsert(uh, "Log Committed by",
Matt Spinlerb832aa52023-03-21 15:32:34 -0500270 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800271 jsonInsert(uh, "Subsystem", subsystem, 1);
272 jsonInsert(uh, "Event Scope", eventScope, 1);
273 jsonInsert(uh, "Event Severity", severity, 1);
274 jsonInsert(uh, "Event Type", eventType, 1);
275 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600276 jsonInsert(uh, "Host Transmission", hostState, 1);
Vijay Lobo2fb10212021-08-22 23:24:16 -0500277 jsonInsert(uh, "HMC Transmission", hmcState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800278 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500279 return uh;
280}
Matt Spinleraadccc82020-04-10 14:33:42 -0500281
282std::optional<uint8_t> UserHeader::getSeverity(
283 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500284 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500285{
286 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500287 std::vector<std::string> systemNames;
288
289 // getSystemNames makes D-Bus calls, so only call it if we
290 // know we'll need it because there is a system name in the sev list
291 if (std::any_of(severities.begin(), severities.end(),
292 [](const auto& sev) { return !sev.system.empty(); }))
293 {
294 try
295 {
296 systemNames = dataIface.getSystemNames();
297 }
298 catch (const std::exception& e)
299 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500300 lg2::error(
301 "Failed trying to look up system names on D-Bus ERROR={ERROR}",
302 "ERROR", e);
Matt Spinler1ab66962020-10-29 13:21:44 -0500303 return std::nullopt;
304 }
305 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500306
307 // Find the severity to use for this system type, or use the default
308 // entry (where no system type is specified).
309 for (const auto& sev : severities)
310 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500311 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
312 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500313 {
314 s = &sev.severity;
315 break;
316 }
317 else if (sev.system.empty())
318 {
319 s = &sev.severity;
320 }
321 }
322
323 if (s)
324 {
325 return *s;
326 }
327
328 return std::nullopt;
329}
330
Matt Spinler03c1d912019-07-10 14:12:15 -0500331} // namespace pels
332} // namespace openpower