blob: a7d80238c48b8bb4296714161df09db70142f494 [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
64 // Get the severity from the registry if it's there, otherwise get it
65 // from the OpenBMC event log severity value.
Matt Spinleraadccc82020-04-10 14:33:42 -050066 if (!entry.severity)
67 {
68 _eventSeverity = convertOBMCSeverityToPEL(severity);
69 }
70 else
71 {
72 // Find the severity possibly dependent on the system type.
Matt Spinler1ab66962020-10-29 13:21:44 -050073 auto sev = getSeverity(entry.severity.value(), dataIface);
Matt Spinleraadccc82020-04-10 14:33:42 -050074 if (sev)
75 {
76 _eventSeverity = *sev;
77 }
78 else
79 {
Matt Spinler1ab66962020-10-29 13:21:44 -050080 // Either someone screwed up the message registry
81 // or getSystemNames failed.
Matt Spinler6ea4d5f2020-05-20 13:31:07 -050082 std::string types;
Matt Spinleraadccc82020-04-10 14:33:42 -050083 log<level::ERR>(
Matt Spinler1ab66962020-10-29 13:21:44 -050084 "Failed finding the severity in the message registry",
85 phosphor::logging::entry("ERROR=%s", entry.name.c_str()));
Matt Spinleraadccc82020-04-10 14:33:42 -050086
87 // Have to choose something, just use informational.
88 _eventSeverity = 0;
89 }
90 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -050091
Vijay Lobo6b3f3452021-04-15 23:04:42 -050092 // Convert Critical error (0x50) to Critical Error-System Termination
93 // (0x51), if the AdditionalData is set to SYSTEM_TERM
94 auto sevLevel = additionalData.getValue("SEVERITY_DETAIL");
95 if ((_eventSeverity & 0xF0) == 0x50)
96 {
97 if (sevLevel.value_or("") == "SYSTEM_TERM")
98 {
99 // Change to Critical Error, System Termination
100 _eventSeverity = 0x51;
101 }
102 }
103
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500104 // TODO: ibm-dev/dev/#1144 Handle manufacturing sev & action flags
105
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500106 if (entry.eventType)
107 {
108 _eventType = *entry.eventType;
109 }
110 else
111 {
112 // There are different default event types for info errors
113 // vs non info ones.
114 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
Matt Spinlerf1e85e22019-11-01 11:31:31 -0500115 _eventType = (sevType == SeverityType::nonError)
116 ? static_cast<uint8_t>(EventType::miscInformational)
117 : static_cast<uint8_t>(EventType::notApplicable);
118 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500119
120 _reserved4Byte1 = 0;
121
122 // No uses for problem domain or vector
123 _problemDomain = 0;
124 _problemVector = 0;
125
Matt Spinler1f93c592020-09-10 10:43:08 -0500126 // These will be set in pel_rules::check() if they're still
127 // at the default value.
128 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500129
Matt Spinlereb111442019-11-07 13:05:36 -0600130 _states = 0;
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500131
132 _valid = true;
133}
134
Matt Spinler03c1d912019-07-10 14:12:15 -0500135UserHeader::UserHeader(Stream& pel)
136{
137 try
138 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500139 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500140 validate();
141 }
142 catch (const std::exception& e)
143 {
144 log<level::ERR>("Cannot unflatten user header",
145 entry("ERROR=%s", e.what()));
146 _valid = false;
147 }
148}
149
150void UserHeader::validate()
151{
152 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500153 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500154 {
155 log<level::ERR>("Invalid user header section ID",
156 entry("ID=0x%X", header().id));
157 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500158 }
159
160 if (header().version != userHeaderVersion)
161 {
162 log<level::ERR>("Invalid user header version",
163 entry("VERSION=0x%X", header().version));
164 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500165 }
166
167 _valid = (failed) ? false : true;
168}
169
Aatir Manzurad0e0472019-10-07 13:18:37 -0500170std::optional<std::string> UserHeader::getJSON() const
171{
172 std::string severity;
173 std::string subsystem;
174 std::string eventScope;
175 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800176 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600177 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
178 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
179 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
180 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800181 actionFlags =
182 pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600183
184 std::string hostState{"Invalid"};
185 auto iter = pv::transmissionStates.find(
186 static_cast<TransmissionState>(hostTransmissionState()));
187 if (iter != pv::transmissionStates.end())
188 {
189 hostState = iter->second;
190 }
191
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800192 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800193 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
194 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
195 jsonInsert(uh, "Log Committed by",
196 getNumberString("0x%X", _header.componentID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800197 jsonInsert(uh, "Subsystem", subsystem, 1);
198 jsonInsert(uh, "Event Scope", eventScope, 1);
199 jsonInsert(uh, "Event Severity", severity, 1);
200 jsonInsert(uh, "Event Type", eventType, 1);
201 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600202 jsonInsert(uh, "Host Transmission", hostState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800203 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500204 return uh;
205}
Matt Spinleraadccc82020-04-10 14:33:42 -0500206
207std::optional<uint8_t> UserHeader::getSeverity(
208 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler1ab66962020-10-29 13:21:44 -0500209 const DataInterfaceBase& dataIface) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500210{
211 const uint8_t* s = nullptr;
Matt Spinler1ab66962020-10-29 13:21:44 -0500212 std::vector<std::string> systemNames;
213
214 // getSystemNames makes D-Bus calls, so only call it if we
215 // know we'll need it because there is a system name in the sev list
216 if (std::any_of(severities.begin(), severities.end(),
217 [](const auto& sev) { return !sev.system.empty(); }))
218 {
219 try
220 {
221 systemNames = dataIface.getSystemNames();
222 }
223 catch (const std::exception& e)
224 {
225 log<level::ERR>("Failed trying to look up system names on D-Bus",
226 entry("ERROR=%s", e.what()));
227 return std::nullopt;
228 }
229 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500230
231 // Find the severity to use for this system type, or use the default
232 // entry (where no system type is specified).
233 for (const auto& sev : severities)
234 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500235 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
236 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500237 {
238 s = &sev.severity;
239 break;
240 }
241 else if (sev.system.empty())
242 {
243 s = &sev.severity;
244 }
245 }
246
247 if (s)
248 {
249 return *s;
250 }
251
252 return std::nullopt;
253}
254
Matt Spinler03c1d912019-07-10 14:12:15 -0500255} // namespace pels
256} // namespace openpower