blob: 3088c13db41d801fce7a324436193127f78b483b [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
Jayanth Othayoth1aa90d42023-09-13 04:25:45 -050025#include <format>
Patrick Williams2544b412022-10-04 08:41:06 -050026#include <iostream>
27
Matt Spinler03c1d912019-07-10 14:12:15 -050028namespace openpower
29{
30namespace pels
31{
32
Aatirc1489352019-12-09 13:13:20 -060033namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050034
Matt Spinlercf5a8d02019-09-05 12:58:53 -050035void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050036{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050037 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
38 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060039 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050040}
41
Matt Spinler06885452019-11-06 10:35:42 -060042void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050043{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050044 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
45 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060046 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050047}
48
Matt Spinlerfdb6a202019-09-20 14:09:20 -050049UserHeader::UserHeader(const message::Entry& entry,
Matt Spinleraadccc82020-04-10 14:33:42 -050050 phosphor::logging::Entry::Level severity,
Vijay Lobo6b3f3452021-04-15 23:04:42 -050051 const AdditionalData& additionalData,
Matt Spinleraadccc82020-04-10 14:33:42 -050052 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050053{
54 _header.id = static_cast<uint16_t>(SectionID::userHeader);
55 _header.size = UserHeader::flattenedSize();
56 _header.version = userHeaderVersion;
57 _header.subType = 0;
58 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
59
Matt Spinler23970b02022-02-25 16:34:46 -060060 std::optional<uint8_t> subsys;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050061
Sumit Kumar50bfa692022-01-06 06:48:26 -060062 // Check for additional data - PEL_SUBSYSTEM
63 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
64 if (ss)
65 {
66 auto eventSubsystem = std::stoul(*ss, NULL, 16);
Patrick Williams2544b412022-10-04 08:41:06 -050067 std::string subsystemString = pv::getValue(eventSubsystem,
68 pel_values::subsystemValues);
Matt Spinler23970b02022-02-25 16:34:46 -060069 if (subsystemString == "invalid")
Sumit Kumar50bfa692022-01-06 06:48:26 -060070 {
Arya K Padman5bc26532024-04-10 06:19:25 -050071 lg2::warning(
72 "UH: Invalid SubSystem value in PEL_SUBSYSTEM: {PEL_SUBSYSTEM}",
73 "PEL_SUBSYSTEM", lg2::hex, eventSubsystem);
Sumit Kumar50bfa692022-01-06 06:48:26 -060074 }
75 else
76 {
Matt Spinler23970b02022-02-25 16:34:46 -060077 subsys = eventSubsystem;
Sumit Kumar50bfa692022-01-06 06:48:26 -060078 }
79 }
Matt Spinler23970b02022-02-25 16:34:46 -060080 else
81 {
82 subsys = entry.subsystem;
83 }
84
85 if (subsys)
86 {
87 _eventSubsystem = *subsys;
88 }
89 else
90 {
91 // Gotta use something, how about 'others'.
Arya K Padman5bc26532024-04-10 06:19:25 -050092 lg2::warning(
Matt Spinler23970b02022-02-25 16:34:46 -060093 "No PEL subystem value supplied for error, using 'others'");
94 _eventSubsystem = 0x70;
95 }
Sumit Kumar50bfa692022-01-06 06:48:26 -060096
Matt Spinlerfdb6a202019-09-20 14:09:20 -050097 _eventScope = entry.eventScope.value_or(
98 static_cast<uint8_t>(EventScope::entirePlatform));
99
Matt Spinleraadccc82020-04-10 14:33:42 -0500100 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500101 bool mfgSevStatus = false;
102 bool mfgActionFlagStatus = false;
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500103
104 // Get the mfg severity & action flags
105 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -0500106 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500107 std::optional<uint8_t> sev = std::nullopt;
108 uint16_t val = 0;
109
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500110 if (entry.mfgSeverity)
111 {
112 // Find the mf severity possibly dependent on the system type.
113 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
114 }
115
116 if (entry.mfgActionFlags)
117 {
118 // Find the mfg action flags
119 val = entry.mfgActionFlags.value();
120 }
121
122 if (sev || val)
123 {
124 bool mfgProp = dataIface.getQuiesceOnError();
125 if (mfgProp)
126 {
127 if (sev)
128 {
129 _eventSeverity = *sev;
130 mfgSevStatus = true;
131 }
132
133 if (val)
134 {
135 _actionFlags = val;
136 mfgActionFlagStatus = true;
137 }
138 }
139 }
140 }
141
142 if (!mfgSevStatus)
143 {
144 // Get the severity from the registry if it's there, otherwise get
145 // it from the OpenBMC event log severity value.
146 if (!entry.severity)
147 {
148 _eventSeverity = convertOBMCSeverityToPEL(severity);
149 }
150 else
151 {
152 // Find the severity possibly dependent on the system type.
153 auto sev = getSeverity(entry.severity.value(), dataIface);
154 if (sev)
155 {
156 _eventSeverity = *sev;
157 }
158 else
159 {
160 // Either someone screwed up the message registry
161 // or getSystemNames failed.
Arya K Padman5bc26532024-04-10 06:19:25 -0500162 lg2::error(
163 "Failed finding the severity in the message registry ERROR={ERROR}",
164 "ERROR", entry.name);
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500165
166 // Have to choose something, just use informational.
167 _eventSeverity = 0;
168 }
169 }
170 }
171
172 // Convert Critical error (0x50) to Critical Error-System Termination
173 // (0x51), if the AdditionalData is set to SYSTEM_TERM
174 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
175 if ((_eventSeverity & 0xF0) == 0x50)
176 {
177 if (sevLevel.value_or("") == "SYSTEM_TERM")
178 {
179 // Change to Critical Error, System Termination
180 _eventSeverity = 0x51;
181 }
182 }
183
184 if (entry.eventType)
185 {
186 _eventType = *entry.eventType;
Matt Spinleraadccc82020-04-10 14:33:42 -0500187 }
188 else
189 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500190 // There are different default event types for info errors
191 // vs non info ones.
192 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
193 _eventType =
194 (sevType == SeverityType::nonError)
195 ? static_cast<uint8_t>(EventType::miscInformational)
196 : static_cast<uint8_t>(EventType::notApplicable);
Matt Spinleraadccc82020-04-10 14:33:42 -0500197 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500198
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500199 _reserved4Byte1 = 0;
200
201 // No uses for problem domain or vector
202 _problemDomain = 0;
203 _problemVector = 0;
204
205 // These will be set in pel_rules::check() if they're still
206 // at the default value.
207 if (!mfgActionFlagStatus)
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500208 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500209 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500210 }
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500211
212 _states = 0;
213
214 _valid = true;
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500215 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500216}
217
Matt Spinler03c1d912019-07-10 14:12:15 -0500218UserHeader::UserHeader(Stream& pel)
219{
220 try
221 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500222 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500223 validate();
224 }
225 catch (const std::exception& e)
226 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500227 lg2::error("Cannot unflatten user header: {EXCEPTION}", "EXCEPTION", e);
Matt Spinler03c1d912019-07-10 14:12:15 -0500228 _valid = false;
229 }
230}
231
232void UserHeader::validate()
233{
234 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500235 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500236 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500237 lg2::error("Invalid user header section ID: {HEADER_ID}", "HEADER_ID",
238 lg2::hex, header().id);
Matt Spinler03c1d912019-07-10 14:12:15 -0500239 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500240 }
241
242 if (header().version != userHeaderVersion)
243 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500244 lg2::error("Invalid user header version: {HEADER_VERSION}",
245 "HEADER_VERSION", lg2::hex, header().version);
Matt Spinler03c1d912019-07-10 14:12:15 -0500246 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500247 }
248
249 _valid = (failed) ? false : true;
250}
251
Matt Spinlerb832aa52023-03-21 15:32:34 -0500252std::optional<std::string> UserHeader::getJSON(uint8_t creatorID) const
Aatir Manzurad0e0472019-10-07 13:18:37 -0500253{
254 std::string severity;
255 std::string subsystem;
256 std::string eventScope;
257 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800258 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600259 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
260 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
261 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
262 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Patrick Williams2544b412022-10-04 08:41:06 -0500263 actionFlags = pv::getValuesBitwise(_actionFlags,
264 pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600265
266 std::string hostState{"Invalid"};
Vijay Lobo2fb10212021-08-22 23:24:16 -0500267 std::string hmcState{"Invalid"};
Matt Spinler455587e2020-01-15 14:31:52 -0600268 auto iter = pv::transmissionStates.find(
269 static_cast<TransmissionState>(hostTransmissionState()));
270 if (iter != pv::transmissionStates.end())
271 {
272 hostState = iter->second;
273 }
Vijay Lobo2fb10212021-08-22 23:24:16 -0500274 auto iter1 = pv::transmissionStates.find(
275 static_cast<TransmissionState>(hmcTransmissionState()));
276 if (iter1 != pv::transmissionStates.end())
277 {
278 hmcState = iter1->second;
279 }
Matt Spinler455587e2020-01-15 14:31:52 -0600280
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800281 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800282 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
283 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
284 jsonInsert(uh, "Log Committed by",
Matt Spinlerb832aa52023-03-21 15:32:34 -0500285 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800286 jsonInsert(uh, "Subsystem", subsystem, 1);
287 jsonInsert(uh, "Event Scope", eventScope, 1);
288 jsonInsert(uh, "Event Severity", severity, 1);
289 jsonInsert(uh, "Event Type", eventType, 1);
290 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600291 jsonInsert(uh, "Host Transmission", hostState, 1);
Vijay Lobo2fb10212021-08-22 23:24:16 -0500292 jsonInsert(uh, "HMC Transmission", hmcState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800293 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500294 return uh;
295}
Matt Spinleraadccc82020-04-10 14:33:42 -0500296
297std::optional<uint8_t> UserHeader::getSeverity(
298 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500299 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500300{
301 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500302 std::vector<std::string> systemNames;
303
304 // getSystemNames makes D-Bus calls, so only call it if we
305 // know we'll need it because there is a system name in the sev list
306 if (std::any_of(severities.begin(), severities.end(),
307 [](const auto& sev) { return !sev.system.empty(); }))
308 {
309 try
310 {
311 systemNames = dataIface.getSystemNames();
312 }
313 catch (const std::exception& e)
314 {
Arya K Padman5bc26532024-04-10 06:19:25 -0500315 lg2::error(
316 "Failed trying to look up system names on D-Bus ERROR={ERROR}",
317 "ERROR", e);
Matt Spinler1ab66962020-10-29 13:21:44 -0500318 return std::nullopt;
319 }
320 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500321
322 // Find the severity to use for this system type, or use the default
323 // entry (where no system type is specified).
324 for (const auto& sev : severities)
325 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500326 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
327 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500328 {
329 s = &sev.severity;
330 break;
331 }
332 else if (sev.system.empty())
333 {
334 s = &sev.severity;
335 }
336 }
337
338 if (s)
339 {
340 return *s;
341 }
342
343 return std::nullopt;
344}
345
Matt Spinler03c1d912019-07-10 14:12:15 -0500346} // namespace pels
347} // namespace openpower