blob: a1b6194160b84f7b4e4e18005a85a34b4c3f11d5 [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
Aatir Manzurad0e0472019-10-07 13:18:37 -050023#include <iostream>
Matt Spinler03c1d912019-07-10 14:12:15 -050024#include <phosphor-logging/log.hpp>
25
26namespace openpower
27{
28namespace pels
29{
30
Aatirc1489352019-12-09 13:13:20 -060031namespace pv = openpower::pels::pel_values;
Matt Spinler03c1d912019-07-10 14:12:15 -050032using namespace phosphor::logging;
33
Matt Spinlercf5a8d02019-09-05 12:58:53 -050034void UserHeader::unflatten(Stream& stream)
Matt Spinler03c1d912019-07-10 14:12:15 -050035{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050036 stream >> _header >> _eventSubsystem >> _eventScope >> _eventSeverity >>
37 _eventType >> _reserved4Byte1 >> _problemDomain >> _problemVector >>
Matt Spinlereb111442019-11-07 13:05:36 -060038 _actionFlags >> _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050039}
40
Matt Spinler06885452019-11-06 10:35:42 -060041void UserHeader::flatten(Stream& stream) const
Matt Spinler03c1d912019-07-10 14:12:15 -050042{
Matt Spinlercf5a8d02019-09-05 12:58:53 -050043 stream << _header << _eventSubsystem << _eventScope << _eventSeverity
44 << _eventType << _reserved4Byte1 << _problemDomain << _problemVector
Matt Spinlereb111442019-11-07 13:05:36 -060045 << _actionFlags << _states;
Matt Spinler03c1d912019-07-10 14:12:15 -050046}
47
Matt Spinlerfdb6a202019-09-20 14:09:20 -050048UserHeader::UserHeader(const message::Entry& entry,
Matt Spinleraadccc82020-04-10 14:33:42 -050049 phosphor::logging::Entry::Level severity,
Vijay Lobo6b3f3452021-04-15 23:04:42 -050050 const AdditionalData& additionalData,
Matt Spinleraadccc82020-04-10 14:33:42 -050051 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050052{
53 _header.id = static_cast<uint16_t>(SectionID::userHeader);
54 _header.size = UserHeader::flattenedSize();
55 _header.version = userHeaderVersion;
56 _header.subType = 0;
57 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
58
59 _eventSubsystem = entry.subsystem;
60
61 _eventScope = entry.eventScope.value_or(
62 static_cast<uint8_t>(EventScope::entirePlatform));
63
Matt Spinleraadccc82020-04-10 14:33:42 -050064 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050065 bool mfgSevStatus = false;
66 bool mfgActionFlagStatus = false;
67 std::optional<uint8_t> sev = std::nullopt;
68 uint16_t val = 0;
69
70 // Get the mfg severity & action flags
71 if (entry.mfgSeverity || entry.mfgActionFlags)
Matt Spinleraadccc82020-04-10 14:33:42 -050072 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -050073 if (entry.mfgSeverity)
74 {
75 // Find the mf severity possibly dependent on the system type.
76 sev = getSeverity(entry.mfgSeverity.value(), dataIface);
77 }
78
79 if (entry.mfgActionFlags)
80 {
81 // Find the mfg action flags
82 val = entry.mfgActionFlags.value();
83 }
84
85 if (sev || val)
86 {
87 bool mfgProp = dataIface.getQuiesceOnError();
88 if (mfgProp)
89 {
90 if (sev)
91 {
92 _eventSeverity = *sev;
93 mfgSevStatus = true;
94 }
95
96 if (val)
97 {
98 _actionFlags = val;
99 mfgActionFlagStatus = true;
100 }
101 }
102 }
103 }
104
105 if (!mfgSevStatus)
106 {
107 // Get the severity from the registry if it's there, otherwise get
108 // it from the OpenBMC event log severity value.
109 if (!entry.severity)
110 {
111 _eventSeverity = convertOBMCSeverityToPEL(severity);
112 }
113 else
114 {
115 // Find the severity possibly dependent on the system type.
116 auto sev = getSeverity(entry.severity.value(), dataIface);
117 if (sev)
118 {
119 _eventSeverity = *sev;
120 }
121 else
122 {
123 // Either someone screwed up the message registry
124 // or getSystemNames failed.
125 std::string types;
126 log<level::ERR>(
127 "Failed finding the severity in the message registry",
128 phosphor::logging::entry("ERROR=%s",
129 entry.name.c_str()));
130
131 // Have to choose something, just use informational.
132 _eventSeverity = 0;
133 }
134 }
135 }
136
137 // Convert Critical error (0x50) to Critical Error-System Termination
138 // (0x51), if the AdditionalData is set to SYSTEM_TERM
139 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
140 if ((_eventSeverity & 0xF0) == 0x50)
141 {
142 if (sevLevel.value_or("") == "SYSTEM_TERM")
143 {
144 // Change to Critical Error, System Termination
145 _eventSeverity = 0x51;
146 }
147 }
148
149 if (entry.eventType)
150 {
151 _eventType = *entry.eventType;
Matt Spinleraadccc82020-04-10 14:33:42 -0500152 }
153 else
154 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500155 // There are different default event types for info errors
156 // vs non info ones.
157 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
158 _eventType =
159 (sevType == SeverityType::nonError)
160 ? static_cast<uint8_t>(EventType::miscInformational)
161 : static_cast<uint8_t>(EventType::notApplicable);
Matt Spinleraadccc82020-04-10 14:33:42 -0500162 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500163
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500164 _reserved4Byte1 = 0;
165
166 // No uses for problem domain or vector
167 _problemDomain = 0;
168 _problemVector = 0;
169
170 // These will be set in pel_rules::check() if they're still
171 // at the default value.
172 if (!mfgActionFlagStatus)
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500173 {
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500174 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500175 }
Sumit Kumar3b8ed7f2021-05-18 12:38:35 -0500176
177 _states = 0;
178
179 _valid = true;
Vijay Lobo6b3f3452021-04-15 23:04:42 -0500180 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500181}
182
Matt Spinler03c1d912019-07-10 14:12:15 -0500183UserHeader::UserHeader(Stream& pel)
184{
185 try
186 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500187 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500188 validate();
189 }
190 catch (const std::exception& e)
191 {
192 log<level::ERR>("Cannot unflatten user header",
193 entry("ERROR=%s", e.what()));
194 _valid = false;
195 }
196}
197
198void UserHeader::validate()
199{
200 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500201 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500202 {
203 log<level::ERR>("Invalid user header section ID",
204 entry("ID=0x%X", header().id));
205 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500206 }
207
208 if (header().version != userHeaderVersion)
209 {
210 log<level::ERR>("Invalid user header version",
211 entry("VERSION=0x%X", header().version));
212 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500213 }
214
215 _valid = (failed) ? false : true;
216}
217
Aatir Manzurad0e0472019-10-07 13:18:37 -0500218std::optional<std::string> UserHeader::getJSON() const
219{
220 std::string severity;
221 std::string subsystem;
222 std::string eventScope;
223 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800224 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600225 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
226 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
227 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
228 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800229 actionFlags =
230 pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600231
232 std::string hostState{"Invalid"};
233 auto iter = pv::transmissionStates.find(
234 static_cast<TransmissionState>(hostTransmissionState()));
235 if (iter != pv::transmissionStates.end())
236 {
237 hostState = iter->second;
238 }
239
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800240 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800241 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
242 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
243 jsonInsert(uh, "Log Committed by",
244 getNumberString("0x%X", _header.componentID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800245 jsonInsert(uh, "Subsystem", subsystem, 1);
246 jsonInsert(uh, "Event Scope", eventScope, 1);
247 jsonInsert(uh, "Event Severity", severity, 1);
248 jsonInsert(uh, "Event Type", eventType, 1);
249 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600250 jsonInsert(uh, "Host Transmission", hostState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800251 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500252 return uh;
253}
Matt Spinleraadccc82020-04-10 14:33:42 -0500254
255std::optional<uint8_t> UserHeader::getSeverity(
256 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500257 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500258{
259 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500260 std::vector<std::string> systemNames;
261
262 // getSystemNames makes D-Bus calls, so only call it if we
263 // know we'll need it because there is a system name in the sev list
264 if (std::any_of(severities.begin(), severities.end(),
265 [](const auto& sev) { return !sev.system.empty(); }))
266 {
267 try
268 {
269 systemNames = dataIface.getSystemNames();
270 }
271 catch (const std::exception& e)
272 {
273 log<level::ERR>("Failed trying to look up system names on D-Bus",
274 entry("ERROR=%s", e.what()));
275 return std::nullopt;
276 }
277 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500278
279 // Find the severity to use for this system type, or use the default
280 // entry (where no system type is specified).
281 for (const auto& sev : severities)
282 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500283 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
284 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500285 {
286 s = &sev.severity;
287 break;
288 }
289 else if (sev.system.empty())
290 {
291 s = &sev.severity;
292 }
293 }
294
295 if (s)
296 {
297 return *s;
298 }
299
300 return std::nullopt;
301}
302
Matt Spinler03c1d912019-07-10 14:12:15 -0500303} // namespace pels
304} // namespace openpower