blob: 4605e2faa778a5563a5714de4f1a70e674a99f33 [file] [log] [blame]
Patrick Venturef085d912019-03-15 08:50:00 -07001/*
2 * Copyright 2019 Google Inc.
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 */
16
17#include "handler.hpp"
18
Patrick Ventured2037c62019-03-15 10:29:47 -070019#include "errors.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070020#include "handler_impl.hpp"
Patrick Ventureab650002019-03-16 09:08:47 -070021#include "util.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070022
23#include <ipmid/api.h>
24
Patrick Venturebb90d4f2019-03-15 13:42:06 -070025#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070026#include <cstdio>
27#include <filesystem>
28#include <fstream>
Patrick Venture07f85152019-03-15 21:36:56 -070029#include <map>
30#include <nlohmann/json.hpp>
31#include <phosphor-logging/elog-errors.hpp>
Patrick Ventureaa374122019-03-15 15:09:10 -070032#include <phosphor-logging/log.hpp>
33#include <sdbusplus/bus.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070034#include <sstream>
35#include <string>
36#include <tuple>
Patrick Venture07f85152019-03-15 21:36:56 -070037#include <xyz/openbmc_project/Common/error.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070038
Patrick Venturef085d912019-03-15 08:50:00 -070039// The phosphor-host-ipmi daemon requires a configuration that maps
40// the if_name to the IPMI LAN channel. However, that doesn't strictly
41// define which is meant to be used for NCSI.
42#ifndef NCSI_IPMI_CHANNEL
43#define NCSI_IPMI_CHANNEL 1
44#endif
45
46#ifndef NCSI_IF_NAME
47#define NCSI_IF_NAME eth0
48#endif
49
50// To deal with receiving a string without quotes.
51#define QUOTE(name) #name
52#define STR(macro) QUOTE(macro)
53#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
54
55namespace google
56{
57namespace ipmi
58{
Patrick Ventured2037c62019-03-15 10:29:47 -070059namespace fs = std::filesystem;
Patrick Venture07f85152019-03-15 21:36:56 -070060using Json = nlohmann::json;
61using namespace phosphor::logging;
62using InternalFailure =
63 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venturef085d912019-03-15 08:50:00 -070064
65std::tuple<std::uint8_t, std::string> Handler::getEthDetails() const
66{
67 return std::make_tuple(NCSI_IPMI_CHANNEL, NCSI_IF_NAME_STR);
68}
69
Patrick Ventured2037c62019-03-15 10:29:47 -070070std::int64_t Handler::getRxPackets(const std::string& name) const
71{
72 std::ostringstream opath;
73 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
74 std::string path = opath.str();
75
76 // Minor sanity & security check (of course, I'm less certain if unicode
77 // comes into play here.
78 //
79 // Basically you can't easily inject ../ or /../ into the path below.
80 if (name.find("/") != std::string::npos)
81 {
82 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
83 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
84 }
85
86 std::error_code ec;
87 if (!fs::exists(path, ec))
88 {
89 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
90 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
91 }
92 // We're uninterested in the state of ec.
93
94 int64_t count = 0;
95 std::ifstream ifs;
96 ifs.exceptions(std::ifstream::failbit);
97 try
98 {
99 ifs.open(path);
100 ifs >> count;
101 }
102 catch (std::ios_base::failure& fail)
103 {
104 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
105 }
106
107 return count;
108}
109
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700110VersionTuple Handler::getCpldVersion(unsigned int id) const
111{
112 std::ostringstream opath;
113 opath << "/run/cpld" << id << ".version";
114 // Check for file
115
116 std::error_code ec;
117 if (!fs::exists(opath.str(), ec))
118 {
119 std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
120 opath.str().c_str());
121 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
122 }
123 // We're uninterested in the state of ec.
124
125 // If file exists, read.
126 std::ifstream ifs;
127 ifs.exceptions(std::ifstream::failbit);
128 std::string value;
129 try
130 {
131 ifs.open(opath.str());
132 ifs >> value;
133 }
134 catch (std::ios_base::failure& fail)
135 {
136 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
137 }
138
139 // If value parses as expected, return version.
140 VersionTuple version = std::make_tuple(0, 0, 0, 0);
141
142 int num_fields =
143 std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
144 &std::get<0>(version), &std::get<1>(version),
145 &std::get<2>(version), &std::get<3>(version));
146 if (num_fields == 0)
147 {
148 std::fprintf(stderr, "Invalid version.\n");
149 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
150 }
151
152 return version;
153}
154
Patrick Ventureaa374122019-03-15 15:09:10 -0700155static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
156static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
157static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
158static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
159static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
160
161void Handler::psuResetDelay(std::uint32_t delay) const
162{
Patrick Ventureaa374122019-03-15 15:09:10 -0700163 std::ofstream ofs;
164 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
165 if (!ofs.good())
166 {
167 std::fprintf(stderr, "Unable to open file for output.\n");
168 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
169 }
170
171 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
172 if (ofs.fail())
173 {
174 std::fprintf(stderr, "Write failed\n");
175 ofs.close();
176 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
177 }
178
179 // Write succeeded, please continue.
180 ofs.flush();
181 ofs.close();
182
183 auto bus = sdbusplus::bus::new_default();
184 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
185 SYSTEMD_INTERFACE, "StartUnit");
186
187 method.append(PSU_HARDRESET_TARGET);
188 method.append("replace");
189
190 try
191 {
192 bus.call_noreply(method);
193 }
194 catch (const sdbusplus::exception::SdBusError& ex)
195 {
196 log<level::ERR>("Failed to call PSU hard reset");
197 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
198 }
199}
200
Patrick Ventureab650002019-03-16 09:08:47 -0700201std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700202{
Patrick Ventureab650002019-03-16 09:08:47 -0700203 // Check if we support this Entity ID.
204 auto it = _entityIdToName.find(id);
205 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700206 {
Patrick Ventureab650002019-03-16 09:08:47 -0700207 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
208 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
Patrick Venture07f85152019-03-15 21:36:56 -0700209 }
210
Patrick Ventureab650002019-03-16 09:08:47 -0700211 std::string entityName;
212 try
Patrick Venture07f85152019-03-15 21:36:56 -0700213 {
Patrick Ventureab650002019-03-16 09:08:47 -0700214 // Parse the JSON config file.
215 if (!_entityConfigParsed)
216 {
217 _entityConfig = parseConfig(_configFile);
218 _entityConfigParsed = true;
219 }
220
221 // Find the "entity id:entity instance" mapping to entity name.
222 entityName = readNameFromConfig(it->second, instance, _entityConfig);
223 if (entityName.empty())
224 {
225 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
226 }
227 }
228 catch (InternalFailure& e)
229 {
230 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
Patrick Venture07f85152019-03-15 21:36:56 -0700231 }
232
Patrick Ventureab650002019-03-16 09:08:47 -0700233 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700234}
235
Patrick Ventureab650002019-03-16 09:08:47 -0700236std::string readNameFromConfig(const std::string& type, uint8_t instance,
237 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700238{
239 static const std::vector<Json> empty{};
240 std::vector<Json> readings = config.value(type, empty);
241 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700242
Patrick Venture07f85152019-03-15 21:36:56 -0700243 for (const auto& j : readings)
244 {
245 uint8_t instanceNum = j.value("instance", 0);
246 // Not the instance we're interested in
247 if (instanceNum != instance)
248 {
249 continue;
250 }
251
252 // Found the instance we're interested in
253 name = j.value("name", "");
254
255 break;
256 }
Patrick Ventureab650002019-03-16 09:08:47 -0700257
Patrick Venture07f85152019-03-15 21:36:56 -0700258 return name;
259}
260
Patrick Venture49f23ad2019-03-16 11:59:55 -0700261void Handler::buildI2cPcieMapping()
262{
263 _pcie_i2c_map = buildPcieMap();
264}
265
266size_t Handler::getI2cPcieMappingSize() const
267{
268 return _pcie_i2c_map.size();
269}
270
271std::tuple<std::uint32_t, std::string>
272 Handler::getI2cEntry(unsigned int entry) const
273{
274 return _pcie_i2c_map[entry];
275}
276
Patrick Ventureab650002019-03-16 09:08:47 -0700277const std::string defaultConfigFile =
278 "/usr/share/ipmi-entity-association/entity_association_map.json";
Patrick Venture07f85152019-03-15 21:36:56 -0700279
Patrick Venturef085d912019-03-15 08:50:00 -0700280} // namespace ipmi
281} // namespace google