blob: a687c5518207660c2d69d724c00f94847b3e1888 [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,
50 const DataInterfaceBase& dataIface)
Matt Spinlerfdb6a202019-09-20 14:09:20 -050051{
52 _header.id = static_cast<uint16_t>(SectionID::userHeader);
53 _header.size = UserHeader::flattenedSize();
54 _header.version = userHeaderVersion;
55 _header.subType = 0;
56 _header.componentID = static_cast<uint16_t>(ComponentID::phosphorLogging);
57
58 _eventSubsystem = entry.subsystem;
59
60 _eventScope = entry.eventScope.value_or(
61 static_cast<uint8_t>(EventScope::entirePlatform));
62
63 // Get the severity from the registry if it's there, otherwise get it
64 // from the OpenBMC event log severity value.
Matt Spinleraadccc82020-04-10 14:33:42 -050065 if (!entry.severity)
66 {
67 _eventSeverity = convertOBMCSeverityToPEL(severity);
68 }
69 else
70 {
71 // Find the severity possibly dependent on the system type.
72 auto sev =
Matt Spinler6ea4d5f2020-05-20 13:31:07 -050073 getSeverity(entry.severity.value(), dataIface.getSystemNames());
Matt Spinleraadccc82020-04-10 14:33:42 -050074 if (sev)
75 {
76 _eventSeverity = *sev;
77 }
78 else
79 {
80 // Someone screwed up the message registry.
Matt Spinler6ea4d5f2020-05-20 13:31:07 -050081 std::string types;
82 const auto& compatibles = dataIface.getSystemNames();
83 std::for_each(compatibles.begin(), compatibles.end(),
84 [&types](const auto& t) { types += t + '|'; });
85
Matt Spinleraadccc82020-04-10 14:33:42 -050086 log<level::ERR>(
Matt Spinler6ea4d5f2020-05-20 13:31:07 -050087 "No severity entry found for this error and system name",
Matt Spinleraadccc82020-04-10 14:33:42 -050088 phosphor::logging::entry("ERROR=%s", entry.name.c_str()),
Matt Spinler6ea4d5f2020-05-20 13:31:07 -050089 phosphor::logging::entry("SYSTEMNAMES=%s", types.c_str()));
Matt Spinleraadccc82020-04-10 14:33:42 -050090
91 // Have to choose something, just use informational.
92 _eventSeverity = 0;
93 }
94 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -050095
96 // TODO: ibm-dev/dev/#1144 Handle manufacturing sev & action flags
97
Matt Spinlerf1e85e22019-11-01 11:31:31 -050098 if (entry.eventType)
99 {
100 _eventType = *entry.eventType;
101 }
102 else
103 {
104 // There are different default event types for info errors
105 // vs non info ones.
106 auto sevType = static_cast<SeverityType>(_eventSeverity & 0xF0);
107
108 _eventType = (sevType == SeverityType::nonError)
109 ? static_cast<uint8_t>(EventType::miscInformational)
110 : static_cast<uint8_t>(EventType::notApplicable);
111 }
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500112
113 _reserved4Byte1 = 0;
114
115 // No uses for problem domain or vector
116 _problemDomain = 0;
117 _problemVector = 0;
118
Matt Spinler1f93c592020-09-10 10:43:08 -0500119 // These will be set in pel_rules::check() if they're still
120 // at the default value.
121 _actionFlags = entry.actionFlags.value_or(actionFlagsDefault);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500122
Matt Spinlereb111442019-11-07 13:05:36 -0600123 _states = 0;
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500124
125 _valid = true;
126}
127
Matt Spinler03c1d912019-07-10 14:12:15 -0500128UserHeader::UserHeader(Stream& pel)
129{
130 try
131 {
Matt Spinlercf5a8d02019-09-05 12:58:53 -0500132 unflatten(pel);
Matt Spinler03c1d912019-07-10 14:12:15 -0500133 validate();
134 }
135 catch (const std::exception& e)
136 {
137 log<level::ERR>("Cannot unflatten user header",
138 entry("ERROR=%s", e.what()));
139 _valid = false;
140 }
141}
142
143void UserHeader::validate()
144{
145 bool failed = false;
Matt Spinler1a94cc32019-09-11 13:32:12 -0500146 if (header().id != static_cast<uint16_t>(SectionID::userHeader))
Matt Spinler03c1d912019-07-10 14:12:15 -0500147 {
148 log<level::ERR>("Invalid user header section ID",
149 entry("ID=0x%X", header().id));
150 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500151 }
152
153 if (header().version != userHeaderVersion)
154 {
155 log<level::ERR>("Invalid user header version",
156 entry("VERSION=0x%X", header().version));
157 failed = true;
Matt Spinler03c1d912019-07-10 14:12:15 -0500158 }
159
160 _valid = (failed) ? false : true;
161}
162
Aatir Manzurad0e0472019-10-07 13:18:37 -0500163std::optional<std::string> UserHeader::getJSON() const
164{
165 std::string severity;
166 std::string subsystem;
167 std::string eventScope;
168 std::string eventType;
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800169 std::vector<std::string> actionFlags;
Aatirc1489352019-12-09 13:13:20 -0600170 severity = pv::getValue(_eventSeverity, pel_values::severityValues);
171 subsystem = pv::getValue(_eventSubsystem, pel_values::subsystemValues);
172 eventScope = pv::getValue(_eventScope, pel_values::eventScopeValues);
173 eventType = pv::getValue(_eventType, pel_values::eventTypeValues);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800174 actionFlags =
175 pv::getValuesBitwise(_actionFlags, pel_values::actionFlagsValues);
Matt Spinler455587e2020-01-15 14:31:52 -0600176
177 std::string hostState{"Invalid"};
178 auto iter = pv::transmissionStates.find(
179 static_cast<TransmissionState>(hostTransmissionState()));
180 if (iter != pv::transmissionStates.end())
181 {
182 hostState = iter->second;
183 }
184
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800185 std::string uh;
Harisuddin Mohamed Isabebeb942020-03-12 17:12:24 +0800186 jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
187 jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
188 jsonInsert(uh, "Log Committed by",
189 getNumberString("0x%X", _header.componentID), 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800190 jsonInsert(uh, "Subsystem", subsystem, 1);
191 jsonInsert(uh, "Event Scope", eventScope, 1);
192 jsonInsert(uh, "Event Severity", severity, 1);
193 jsonInsert(uh, "Event Type", eventType, 1);
194 jsonInsertArray(uh, "Action Flags", actionFlags, 1);
Matt Spinler455587e2020-01-15 14:31:52 -0600195 jsonInsert(uh, "Host Transmission", hostState, 1);
Harisuddin Mohamed Isa600d15a2019-12-20 12:42:26 +0800196 uh.erase(uh.size() - 2);
Aatir Manzurad0e0472019-10-07 13:18:37 -0500197 return uh;
198}
Matt Spinleraadccc82020-04-10 14:33:42 -0500199
200std::optional<uint8_t> UserHeader::getSeverity(
201 const std::vector<message::RegistrySeverity>& severities,
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500202 const std::vector<std::string>& systemNames) const
Matt Spinleraadccc82020-04-10 14:33:42 -0500203{
204 const uint8_t* s = nullptr;
205
206 // Find the severity to use for this system type, or use the default
207 // entry (where no system type is specified).
208 for (const auto& sev : severities)
209 {
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500210 if (std::find(systemNames.begin(), systemNames.end(), sev.system) !=
211 systemNames.end())
Matt Spinleraadccc82020-04-10 14:33:42 -0500212 {
213 s = &sev.severity;
214 break;
215 }
216 else if (sev.system.empty())
217 {
218 s = &sev.severity;
219 }
220 }
221
222 if (s)
223 {
224 return *s;
225 }
226
227 return std::nullopt;
228}
229
Matt Spinler03c1d912019-07-10 14:12:15 -0500230} // namespace pels
231} // namespace openpower