xref: /openbmc/google-ipmi-sys/handler.cpp (revision 29f35bce)
1f085d91dSPatrick Venture /*
2f085d91dSPatrick Venture  * Copyright 2019 Google Inc.
3f085d91dSPatrick Venture  *
4f085d91dSPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5f085d91dSPatrick Venture  * you may not use this file except in compliance with the License.
6f085d91dSPatrick Venture  * You may obtain a copy of the License at
7f085d91dSPatrick Venture  *
8f085d91dSPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9f085d91dSPatrick Venture  *
10f085d91dSPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11f085d91dSPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12f085d91dSPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f085d91dSPatrick Venture  * See the License for the specific language governing permissions and
14f085d91dSPatrick Venture  * limitations under the License.
15f085d91dSPatrick Venture  */
16f085d91dSPatrick Venture 
17f085d91dSPatrick Venture #include "handler.hpp"
18f085d91dSPatrick Venture 
19d2037c6aSPatrick Venture #include "errors.hpp"
20c87de558SPatrick Venture #include "handler_impl.hpp"
21ab650004SPatrick Venture #include "util.hpp"
22d2037c6aSPatrick Venture 
23d2037c6aSPatrick Venture #include <ipmid/api.h>
24d2037c6aSPatrick Venture 
25bb90d4fdSPatrick Venture #include <cinttypes>
26d2037c6aSPatrick Venture #include <cstdio>
27d2037c6aSPatrick Venture #include <filesystem>
28d2037c6aSPatrick Venture #include <fstream>
2907f85150SPatrick Venture #include <map>
3007f85150SPatrick Venture #include <nlohmann/json.hpp>
3107f85150SPatrick Venture #include <phosphor-logging/elog-errors.hpp>
32aa374120SPatrick Venture #include <phosphor-logging/log.hpp>
33aa374120SPatrick Venture #include <sdbusplus/bus.hpp>
34d2037c6aSPatrick Venture #include <sstream>
35d2037c6aSPatrick Venture #include <string>
36*29f35bceSWilliam A. Kennington III #include <string_view>
37d2037c6aSPatrick Venture #include <tuple>
3807f85150SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp>
39d2037c6aSPatrick Venture 
40f085d91dSPatrick Venture // The phosphor-host-ipmi daemon requires a configuration that maps
41f085d91dSPatrick Venture // the if_name to the IPMI LAN channel.  However, that doesn't strictly
42f085d91dSPatrick Venture // define which is meant to be used for NCSI.
43f085d91dSPatrick Venture #ifndef NCSI_IPMI_CHANNEL
44f085d91dSPatrick Venture #define NCSI_IPMI_CHANNEL 1
45f085d91dSPatrick Venture #endif
46f085d91dSPatrick Venture 
47f085d91dSPatrick Venture #ifndef NCSI_IF_NAME
48f085d91dSPatrick Venture #define NCSI_IF_NAME eth0
49f085d91dSPatrick Venture #endif
50f085d91dSPatrick Venture 
51f085d91dSPatrick Venture // To deal with receiving a string without quotes.
52f085d91dSPatrick Venture #define QUOTE(name) #name
53f085d91dSPatrick Venture #define STR(macro) QUOTE(macro)
54f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
55f085d91dSPatrick Venture 
56f085d91dSPatrick Venture namespace google
57f085d91dSPatrick Venture {
58f085d91dSPatrick Venture namespace ipmi
59f085d91dSPatrick Venture {
60d2037c6aSPatrick Venture namespace fs = std::filesystem;
6107f85150SPatrick Venture using Json = nlohmann::json;
6207f85150SPatrick Venture using namespace phosphor::logging;
6307f85150SPatrick Venture using InternalFailure =
6407f85150SPatrick Venture     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
65f085d91dSPatrick Venture 
66f085d91dSPatrick Venture std::tuple<std::uint8_t, std::string> Handler::getEthDetails() const
67f085d91dSPatrick Venture {
68f085d91dSPatrick Venture     return std::make_tuple(NCSI_IPMI_CHANNEL, NCSI_IF_NAME_STR);
69f085d91dSPatrick Venture }
70f085d91dSPatrick Venture 
71d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const
72d2037c6aSPatrick Venture {
73d2037c6aSPatrick Venture     std::ostringstream opath;
74d2037c6aSPatrick Venture     opath << "/sys/class/net/" << name << "/statistics/rx_packets";
75d2037c6aSPatrick Venture     std::string path = opath.str();
76d2037c6aSPatrick Venture 
77d2037c6aSPatrick Venture     // Minor sanity & security check (of course, I'm less certain if unicode
78d2037c6aSPatrick Venture     // comes into play here.
79d2037c6aSPatrick Venture     //
80d2037c6aSPatrick Venture     // Basically you can't easily inject ../ or /../ into the path below.
81d2037c6aSPatrick Venture     if (name.find("/") != std::string::npos)
82d2037c6aSPatrick Venture     {
83d2037c6aSPatrick Venture         std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
84d2037c6aSPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
85d2037c6aSPatrick Venture     }
86d2037c6aSPatrick Venture 
87d2037c6aSPatrick Venture     std::error_code ec;
88d2037c6aSPatrick Venture     if (!fs::exists(path, ec))
89d2037c6aSPatrick Venture     {
90d2037c6aSPatrick Venture         std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
91d2037c6aSPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
92d2037c6aSPatrick Venture     }
93d2037c6aSPatrick Venture     // We're uninterested in the state of ec.
94d2037c6aSPatrick Venture 
95d2037c6aSPatrick Venture     int64_t count = 0;
96d2037c6aSPatrick Venture     std::ifstream ifs;
97d2037c6aSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
98d2037c6aSPatrick Venture     try
99d2037c6aSPatrick Venture     {
100d2037c6aSPatrick Venture         ifs.open(path);
101d2037c6aSPatrick Venture         ifs >> count;
102d2037c6aSPatrick Venture     }
103d2037c6aSPatrick Venture     catch (std::ios_base::failure& fail)
104d2037c6aSPatrick Venture     {
105d2037c6aSPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
106d2037c6aSPatrick Venture     }
107d2037c6aSPatrick Venture 
108d2037c6aSPatrick Venture     return count;
109d2037c6aSPatrick Venture }
110d2037c6aSPatrick Venture 
111bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const
112bb90d4fdSPatrick Venture {
113bb90d4fdSPatrick Venture     std::ostringstream opath;
114bb90d4fdSPatrick Venture     opath << "/run/cpld" << id << ".version";
115bb90d4fdSPatrick Venture     // Check for file
116bb90d4fdSPatrick Venture 
117bb90d4fdSPatrick Venture     std::error_code ec;
118bb90d4fdSPatrick Venture     if (!fs::exists(opath.str(), ec))
119bb90d4fdSPatrick Venture     {
120bb90d4fdSPatrick Venture         std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
121bb90d4fdSPatrick Venture                      opath.str().c_str());
122bb90d4fdSPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
123bb90d4fdSPatrick Venture     }
124bb90d4fdSPatrick Venture     // We're uninterested in the state of ec.
125bb90d4fdSPatrick Venture 
126bb90d4fdSPatrick Venture     // If file exists, read.
127bb90d4fdSPatrick Venture     std::ifstream ifs;
128bb90d4fdSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
129bb90d4fdSPatrick Venture     std::string value;
130bb90d4fdSPatrick Venture     try
131bb90d4fdSPatrick Venture     {
132bb90d4fdSPatrick Venture         ifs.open(opath.str());
133bb90d4fdSPatrick Venture         ifs >> value;
134bb90d4fdSPatrick Venture     }
135bb90d4fdSPatrick Venture     catch (std::ios_base::failure& fail)
136bb90d4fdSPatrick Venture     {
137bb90d4fdSPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
138bb90d4fdSPatrick Venture     }
139bb90d4fdSPatrick Venture 
140bb90d4fdSPatrick Venture     // If value parses as expected, return version.
141bb90d4fdSPatrick Venture     VersionTuple version = std::make_tuple(0, 0, 0, 0);
142bb90d4fdSPatrick Venture 
143bb90d4fdSPatrick Venture     int num_fields =
144bb90d4fdSPatrick Venture         std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
145bb90d4fdSPatrick Venture                     &std::get<0>(version), &std::get<1>(version),
146bb90d4fdSPatrick Venture                     &std::get<2>(version), &std::get<3>(version));
147bb90d4fdSPatrick Venture     if (num_fields == 0)
148bb90d4fdSPatrick Venture     {
149bb90d4fdSPatrick Venture         std::fprintf(stderr, "Invalid version.\n");
150bb90d4fdSPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
151bb90d4fdSPatrick Venture     }
152bb90d4fdSPatrick Venture 
153bb90d4fdSPatrick Venture     return version;
154bb90d4fdSPatrick Venture }
155bb90d4fdSPatrick Venture 
156aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
157aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
158aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
159aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
160aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
161aa374120SPatrick Venture 
162aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const
163aa374120SPatrick Venture {
164aa374120SPatrick Venture     std::ofstream ofs;
165aa374120SPatrick Venture     ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
166aa374120SPatrick Venture     if (!ofs.good())
167aa374120SPatrick Venture     {
168aa374120SPatrick Venture         std::fprintf(stderr, "Unable to open file for output.\n");
169aa374120SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
170aa374120SPatrick Venture     }
171aa374120SPatrick Venture 
172aa374120SPatrick Venture     ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
173aa374120SPatrick Venture     if (ofs.fail())
174aa374120SPatrick Venture     {
175aa374120SPatrick Venture         std::fprintf(stderr, "Write failed\n");
176aa374120SPatrick Venture         ofs.close();
177aa374120SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
178aa374120SPatrick Venture     }
179aa374120SPatrick Venture 
180aa374120SPatrick Venture     // Write succeeded, please continue.
181aa374120SPatrick Venture     ofs.flush();
182aa374120SPatrick Venture     ofs.close();
183aa374120SPatrick Venture 
184aa374120SPatrick Venture     auto bus = sdbusplus::bus::new_default();
185aa374120SPatrick Venture     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
186aa374120SPatrick Venture                                       SYSTEMD_INTERFACE, "StartUnit");
187aa374120SPatrick Venture 
188aa374120SPatrick Venture     method.append(PSU_HARDRESET_TARGET);
189aa374120SPatrick Venture     method.append("replace");
190aa374120SPatrick Venture 
191aa374120SPatrick Venture     try
192aa374120SPatrick Venture     {
193aa374120SPatrick Venture         bus.call_noreply(method);
194aa374120SPatrick Venture     }
195aa374120SPatrick Venture     catch (const sdbusplus::exception::SdBusError& ex)
196aa374120SPatrick Venture     {
197aa374120SPatrick Venture         log<level::ERR>("Failed to call PSU hard reset");
198aa374120SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
199aa374120SPatrick Venture     }
200aa374120SPatrick Venture }
201aa374120SPatrick Venture 
202ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
20307f85150SPatrick Venture {
204ab650004SPatrick Venture     // Check if we support this Entity ID.
205ab650004SPatrick Venture     auto it = _entityIdToName.find(id);
206ab650004SPatrick Venture     if (it == _entityIdToName.end())
20707f85150SPatrick Venture     {
208ab650004SPatrick Venture         log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
209ab650004SPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
21007f85150SPatrick Venture     }
21107f85150SPatrick Venture 
212ab650004SPatrick Venture     std::string entityName;
213ab650004SPatrick Venture     try
21407f85150SPatrick Venture     {
215ab650004SPatrick Venture         // Parse the JSON config file.
216ab650004SPatrick Venture         if (!_entityConfigParsed)
217ab650004SPatrick Venture         {
218ab650004SPatrick Venture             _entityConfig = parseConfig(_configFile);
219ab650004SPatrick Venture             _entityConfigParsed = true;
22007f85150SPatrick Venture         }
22107f85150SPatrick Venture 
222ab650004SPatrick Venture         // Find the "entity id:entity instance" mapping to entity name.
223ab650004SPatrick Venture         entityName = readNameFromConfig(it->second, instance, _entityConfig);
224ab650004SPatrick Venture         if (entityName.empty())
225ab650004SPatrick Venture         {
226ab650004SPatrick Venture             throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
227ab650004SPatrick Venture         }
228ab650004SPatrick Venture     }
229ab650004SPatrick Venture     catch (InternalFailure& e)
230ab650004SPatrick Venture     {
231ab650004SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
23207f85150SPatrick Venture     }
23307f85150SPatrick Venture 
234ab650004SPatrick Venture     return entityName;
235ab650004SPatrick Venture }
236ab650004SPatrick Venture 
237*29f35bceSWilliam A. Kennington III std::string Handler::getMachineName()
238*29f35bceSWilliam A. Kennington III {
239*29f35bceSWilliam A. Kennington III     const char* path = "/etc/os-release";
240*29f35bceSWilliam A. Kennington III     std::ifstream ifs(path);
241*29f35bceSWilliam A. Kennington III     if (ifs.fail())
242*29f35bceSWilliam A. Kennington III     {
243*29f35bceSWilliam A. Kennington III         std::fprintf(stderr, "Failed to open: %s\n", path);
244*29f35bceSWilliam A. Kennington III         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
245*29f35bceSWilliam A. Kennington III     }
246*29f35bceSWilliam A. Kennington III 
247*29f35bceSWilliam A. Kennington III     std::string line;
248*29f35bceSWilliam A. Kennington III     while (true)
249*29f35bceSWilliam A. Kennington III     {
250*29f35bceSWilliam A. Kennington III         std::getline(ifs, line);
251*29f35bceSWilliam A. Kennington III         if (ifs.eof())
252*29f35bceSWilliam A. Kennington III         {
253*29f35bceSWilliam A. Kennington III             std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
254*29f35bceSWilliam A. Kennington III                          path);
255*29f35bceSWilliam A. Kennington III             throw IpmiException(IPMI_CC_INVALID);
256*29f35bceSWilliam A. Kennington III         }
257*29f35bceSWilliam A. Kennington III         if (ifs.fail())
258*29f35bceSWilliam A. Kennington III         {
259*29f35bceSWilliam A. Kennington III             std::fprintf(stderr, "Failed to read: %s\n", path);
260*29f35bceSWilliam A. Kennington III             throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
261*29f35bceSWilliam A. Kennington III         }
262*29f35bceSWilliam A. Kennington III         std::string_view lineView(line);
263*29f35bceSWilliam A. Kennington III         constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
264*29f35bceSWilliam A. Kennington III         if (lineView.substr(0, prefix.size()) != prefix)
265*29f35bceSWilliam A. Kennington III         {
266*29f35bceSWilliam A. Kennington III             continue;
267*29f35bceSWilliam A. Kennington III         }
268*29f35bceSWilliam A. Kennington III         lineView.remove_prefix(prefix.size());
269*29f35bceSWilliam A. Kennington III         lineView.remove_prefix(
270*29f35bceSWilliam A. Kennington III             std::min(lineView.find_first_not_of('"'), lineView.size()));
271*29f35bceSWilliam A. Kennington III         lineView.remove_suffix(
272*29f35bceSWilliam A. Kennington III             lineView.size() - 1 -
273*29f35bceSWilliam A. Kennington III             std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
274*29f35bceSWilliam A. Kennington III         return std::string(lineView);
275*29f35bceSWilliam A. Kennington III     }
276*29f35bceSWilliam A. Kennington III }
277*29f35bceSWilliam A. Kennington III 
278ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
279ab650004SPatrick Venture                                const Json& config)
28007f85150SPatrick Venture {
28107f85150SPatrick Venture     static const std::vector<Json> empty{};
28207f85150SPatrick Venture     std::vector<Json> readings = config.value(type, empty);
28307f85150SPatrick Venture     std::string name = "";
284ab650004SPatrick Venture 
28507f85150SPatrick Venture     for (const auto& j : readings)
28607f85150SPatrick Venture     {
28707f85150SPatrick Venture         uint8_t instanceNum = j.value("instance", 0);
28807f85150SPatrick Venture         // Not the instance we're interested in
28907f85150SPatrick Venture         if (instanceNum != instance)
29007f85150SPatrick Venture         {
29107f85150SPatrick Venture             continue;
29207f85150SPatrick Venture         }
29307f85150SPatrick Venture 
29407f85150SPatrick Venture         // Found the instance we're interested in
29507f85150SPatrick Venture         name = j.value("name", "");
29607f85150SPatrick Venture 
29707f85150SPatrick Venture         break;
29807f85150SPatrick Venture     }
299ab650004SPatrick Venture 
30007f85150SPatrick Venture     return name;
30107f85150SPatrick Venture }
30207f85150SPatrick Venture 
30349f23ad9SPatrick Venture void Handler::buildI2cPcieMapping()
30449f23ad9SPatrick Venture {
30549f23ad9SPatrick Venture     _pcie_i2c_map = buildPcieMap();
30649f23ad9SPatrick Venture }
30749f23ad9SPatrick Venture 
30849f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const
30949f23ad9SPatrick Venture {
31049f23ad9SPatrick Venture     return _pcie_i2c_map.size();
31149f23ad9SPatrick Venture }
31249f23ad9SPatrick Venture 
31349f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string>
31449f23ad9SPatrick Venture     Handler::getI2cEntry(unsigned int entry) const
31549f23ad9SPatrick Venture {
31649f23ad9SPatrick Venture     return _pcie_i2c_map[entry];
31749f23ad9SPatrick Venture }
31849f23ad9SPatrick Venture 
319ab650004SPatrick Venture const std::string defaultConfigFile =
320ab650004SPatrick Venture     "/usr/share/ipmi-entity-association/entity_association_map.json";
32107f85150SPatrick Venture 
322f085d91dSPatrick Venture } // namespace ipmi
323f085d91dSPatrick Venture } // namespace google
324