blob: c7fff1ae02fbab3ddddbf384f620d4345544c40c [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
Sumit Kumar50bfa692022-01-06 06:48:26 -060023#include <fmt/format.h>
24
Matt Spinler03c1d912019-07-10 14:12:15 -050025#include <phosphor-logging/log.hpp>
26
Patrick Williams2544b412022-10-04 08:41:06 -050027#include <iostream>
28
Matt Spinler03c1d912019-07-10 14:12:15 -050029namespace openpower
30{
31namespace pels
32{
33
Aatirc1489352019-12-09 13:13:20 -060034namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050035using namespace phosphor::logging;
36
Matt Spinlercf5a8d02019-09-05 12:58:53 -050037void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050038{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050039 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
40 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060041 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050042}
43
Matt Spinler06885452019-11-06 10:35:42 -060044void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050045{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050046 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
47 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060048 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050049}
50
Matt Spinlerfdb6a202019-09-20 14:09:20 -050051UserHeader::UserHeader(const message::Entry& entry,
Matt Spinleraadccc82020-04-10 14:33:42 -050052 phosphor::logging::Entry::Level severity,
Vijay Lobo6b3f3452021-04-15 23:04:42 -050053 const AdditionalData& additionalData,
Matt Spinleraadccc82020-04-10 14:33:42 -050054 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050055{
56 _header.id = static_cast<uint16_t>(SectionID::userHeader);
57 _header.size = UserHeader::flattenedSize();
58 _header.version = userHeaderVersion;
59 _header.subType = 0;
60 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
61
Matt Spinler23970b02022-02-25 16:34:46 -060062 std::optional<uint8_t> subsys;
Matt Spinlerfdb6a202019-09-20 14:09:20 -050063
Sumit Kumar50bfa692022-01-06 06:48:26 -060064 // Check for additional data - PEL_SUBSYSTEM
65 auto ss = additionalData.getValue("PEL_SUBSYSTEM");
66 if (ss)
67 {
68 auto eventSubsystem = std::stoul(*ss, NULL, 16);
Patrick Williams2544b412022-10-04 08:41:06 -050069 std::string subsystemString = pv::getValue(eventSubsystem,
70 pel_values::subsystemValues);
Matt Spinler23970b02022-02-25 16:34:46 -060071 if (subsystemString == "invalid")
Sumit Kumar50bfa692022-01-06 06:48:26 -060072 {
73 log<level::WARNING>(
Matt Spinler23970b02022-02-25 16:34:46 -060074 fmt::format(
75 "UH: Invalid SubSystem value in PEL_SUBSYSTEM: {:#X}",
76 eventSubsystem)
Sumit Kumar50bfa692022-01-06 06:48:26 -060077 .c_str());
78 }
79 else
80 {
Matt Spinler23970b02022-02-25 16:34:46 -060081 subsys = eventSubsystem;
Sumit Kumar50bfa692022-01-06 06:48:26 -060082 }
83 }
Matt Spinler23970b02022-02-25 16:34:46 -060084 else
85 {
86 subsys = entry.subsystem;
87 }
88
89 if (subsys)
90 {
91 _eventSubsystem = *subsys;
92 }
93 else
94 {
95 // Gotta use something, how about 'others'.
96 log<level::WARNING>(
97 "No PEL subystem value supplied for error, using 'others'");
98 _eventSubsystem = 0x70;
99 }
Sumit Kumar50bfa692022-01-06 06:48:26 -0600100
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500101 _eventScope = entry.eventScope.value_or(
102 static_cast<uint8_t>(EventScope::entirePlatform));
103
Matt Spinleraadccc82020-04-10 14:33:42 -0500104 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500105 bool mfgSevStatus = false;
106 bool mfgActionFlagStatus = false;
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500107
108 // Get the mfg severity & action flags
109 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -0500110 {
Matt Spinlerbe952d22022-07-01 11:30:11 -0500111 std::optional<uint8_t> sev = std::nullopt;
112 uint16_t val = 0;
113
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500114 if (entry.mfgSeverity)
115 {
116 // Find the mf severity possibly dependent on the system type.
117 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
118 }
119
120 if (entry.mfgActionFlags)
121 {
122 // Find the mfg action flags
123 val = entry.mfgActionFlags.value();
124 }
125
126 if (sev || val)
127 {
128 bool mfgProp = dataIface.getQuiesceOnError();
129 if (mfgProp)
130 {
131 if (sev)
132 {
133 _eventSeverity = *sev;
134 mfgSevStatus = true;
135 }
136
137 if (val)
138 {
139 _actionFlags = val;
140 mfgActionFlagStatus = true;
141 }
142 }
143 }
144 }
145
146 if (!mfgSevStatus)
147 {
148 // Get the severity from the registry if it's there, otherwise get
149 // it from the OpenBMC event log severity value.
150 if (!entry.severity)
151 {
152 _eventSeverity = convertOBMCSeverityToPEL(severity);
153 }
154 else
155 {
156 // Find the severity possibly dependent on the system type.
157 auto sev = getSeverity(entry.severity.value(), dataIface);
158 if (sev)
159 {
160 _eventSeverity = *sev;
161 }
162 else
163 {
164 // Either someone screwed up the message registry
165 // or getSystemNames failed.
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500166 log<level::ERR>(
167 "Failed finding the severity in the message registry",
168 phosphor::logging::entry("ERROR=%s",
169 entry.name.c_str()));
170
171 // Have to choose something, just use informational.
172 _eventSeverity = 0;
173 }
174 }
175 }
176
177 // Convert Critical error (0x50) to Critical Error-System Termination
178 // (0x51), if the AdditionalData is set to SYSTEM_TERM
179 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
180 if ((_eventSeverity & 0xF0) == 0x50)
181 {
182 if (sevLevel.value_or("") == "SYSTEM_TERM")
183 {
184 // Change to Critical Error, System Termination
185 _eventSeverity = 0x51;
186 }
187 }
188
189 if (entry.eventType)
190 {
191 _eventType = *entry.eventType;
Matt Spinleraadccc82020-04-10 14:33:42 -0500192 }
193 else
194 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500195 // There are different default event types for info errors
196 // vs non info ones.
197 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
198 _eventType =
199 (sevType == SeverityType::nonError)
200 ? static_cast<uint8_t>(EventType::miscInformational)
201 : static_cast<uint8_t>(EventType::notApplicable);
Matt Spinleraadccc82020-04-10 14:33:42 -0500202 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500203
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500204 _reserved4Byte1 = 0;
205
206 // No uses for problem domain or vector
207 _problemDomain = 0;
208 _problemVector = 0;
209
210 // These will be set in pel_rules::check() if they're still
211 // at the default value.
212 if (!mfgActionFlagStatus)
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500213 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500214 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500215 }
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500216
217 _states = 0;
218
219 _valid = true;
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500220 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500221}
222
Matt Spinler03c1d912019-07-10 14:12:15 -0500223UserHeader::UserHeader(Stream& pel)
224{
225 try
226 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500227 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500228 validate();
229 }
230 catch (const std::exception& e)
231 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600232 log<level::ERR>(
233 fmt::format("Cannot unflatten user header: {}", e.what()).c_str());
Matt Spinler03c1d912019-07-10 14:12:15 -0500234 _valid = false;
235 }
236}
237
238void UserHeader::validate()
239{
240 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500241 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500242 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600243 log<level::ERR>(
244 fmt::format("Invalid user header section ID: {0:#x}", header().id)
245 .c_str());
Matt Spinler03c1d912019-07-10 14:12:15 -0500246 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500247 }
248
249 if (header().version != userHeaderVersion)
250 {
Matt Spinler8ac20502023-01-04 11:03:58 -0600251 log<level::ERR>(
252 fmt::format("Invalid user header version: {0:#x}", header().version)
253 .c_str());
Matt Spinler03c1d912019-07-10 14:12:15 -0500254 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500255 }
256
257 _valid = (failed) ? false : true;
258}
259
Matt Spinlerb832aa52023-03-21 15:32:34 -0500260std::optional<std::string> UserHeader::getJSON(uint8_t creatorID) const
Aatir Manzurad0e0472019-10-07 13:18:37 -0500261{
262 std::string severity;
263 std::string subsystem;
264 std::string eventScope;
265 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800266 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600267 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
268 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
269 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
270 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Patrick Williams2544b412022-10-04 08:41:06 -0500271 actionFlags = pv::getValuesBitwise(_actionFlags,
272 pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600273
274 std::string hostState{"Invalid"};
Vijay Lobo2fb10212021-08-22 23:24:16 -0500275 std::string hmcState{"Invalid"};
Matt Spinler455587e2020-01-15 14:31:52 -0600276 auto iter = pv::transmissionStates.find(
277 static_cast<TransmissionState>(hostTransmissionState()));
278 if (iter != pv::transmissionStates.end())
279 {
280 hostState = iter->second;
281 }
Vijay Lobo2fb10212021-08-22 23:24:16 -0500282 auto iter1 = pv::transmissionStates.find(
283 static_cast<TransmissionState>(hmcTransmissionState()));
284 if (iter1 != pv::transmissionStates.end())
285 {
286 hmcState = iter1->second;
287 }
Matt Spinler455587e2020-01-15 14:31:52 -0600288
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800289 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800290 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
291 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
292 jsonInsert(uh, "Log Committed by",
Matt Spinlerb832aa52023-03-21 15:32:34 -0500293 getComponentName(_header.componentID, creatorID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800294 jsonInsert(uh, "Subsystem", subsystem, 1);
295 jsonInsert(uh, "Event Scope", eventScope, 1);
296 jsonInsert(uh, "Event Severity", severity, 1);
297 jsonInsert(uh, "Event Type", eventType, 1);
298 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600299 jsonInsert(uh, "Host Transmission", hostState, 1);
Vijay Lobo2fb10212021-08-22 23:24:16 -0500300 jsonInsert(uh, "HMC Transmission", hmcState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800301 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500302 return uh;
303}
Matt Spinleraadccc82020-04-10 14:33:42 -0500304
305std::optional<uint8_t> UserHeader::getSeverity(
306 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500307 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500308{
309 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500310 std::vector<std::string> systemNames;
311
312 // getSystemNames makes D-Bus calls, so only call it if we
313 // know we'll need it because there is a system name in the sev list
314 if (std::any_of(severities.begin(), severities.end(),
315 [](const auto& sev) { return !sev.system.empty(); }))
316 {
317 try
318 {
319 systemNames = dataIface.getSystemNames();
320 }
321 catch (const std::exception& e)
322 {
323 log<level::ERR>("Failed trying to look up system names on D-Bus",
324 entry("ERROR=%s", e.what()));
325 return std::nullopt;
326 }
327 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500328
329 // Find the severity to use for this system type, or use the default
330 // entry (where no system type is specified).
331 for (const auto& sev : severities)
332 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500333 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
334 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500335 {
336 s = &sev.severity;
337 break;
338 }
339 else if (sev.system.empty())
340 {
341 s = &sev.severity;
342 }
343 }
344
345 if (s)
346 {
347 return *s;
348 }
349
350 return std::nullopt;
351}
352
Matt Spinler03c1d912019-07-10 14:12:15 -0500353} // namespace pels
354} // namespace openpower