xref: /openbmc/google-ipmi-sys/handler.cpp (revision ac4a16f7)
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>
3629f35bceSWilliam 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 
202*ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
203*ac4a16f7SShounak Mitra 
204*ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const
205*ac4a16f7SShounak Mitra {
206*ac4a16f7SShounak Mitra     std::ofstream ofs;
207*ac4a16f7SShounak Mitra     ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
208*ac4a16f7SShounak Mitra     if (!ofs.good())
209*ac4a16f7SShounak Mitra     {
210*ac4a16f7SShounak Mitra         std::fprintf(stderr, "Unable to open file for output.\n");
211*ac4a16f7SShounak Mitra         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
212*ac4a16f7SShounak Mitra     }
213*ac4a16f7SShounak Mitra     ofs.close();
214*ac4a16f7SShounak Mitra }
215*ac4a16f7SShounak Mitra 
216ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
21707f85150SPatrick Venture {
218ab650004SPatrick Venture     // Check if we support this Entity ID.
219ab650004SPatrick Venture     auto it = _entityIdToName.find(id);
220ab650004SPatrick Venture     if (it == _entityIdToName.end())
22107f85150SPatrick Venture     {
222ab650004SPatrick Venture         log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
223ab650004SPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
22407f85150SPatrick Venture     }
22507f85150SPatrick Venture 
226ab650004SPatrick Venture     std::string entityName;
227ab650004SPatrick Venture     try
22807f85150SPatrick Venture     {
229ab650004SPatrick Venture         // Parse the JSON config file.
230ab650004SPatrick Venture         if (!_entityConfigParsed)
231ab650004SPatrick Venture         {
232ab650004SPatrick Venture             _entityConfig = parseConfig(_configFile);
233ab650004SPatrick Venture             _entityConfigParsed = true;
23407f85150SPatrick Venture         }
23507f85150SPatrick Venture 
236ab650004SPatrick Venture         // Find the "entity id:entity instance" mapping to entity name.
237ab650004SPatrick Venture         entityName = readNameFromConfig(it->second, instance, _entityConfig);
238ab650004SPatrick Venture         if (entityName.empty())
239ab650004SPatrick Venture         {
240ab650004SPatrick Venture             throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
241ab650004SPatrick Venture         }
242ab650004SPatrick Venture     }
243ab650004SPatrick Venture     catch (InternalFailure& e)
244ab650004SPatrick Venture     {
245ab650004SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
24607f85150SPatrick Venture     }
24707f85150SPatrick Venture 
248ab650004SPatrick Venture     return entityName;
249ab650004SPatrick Venture }
250ab650004SPatrick Venture 
25129f35bceSWilliam A. Kennington III std::string Handler::getMachineName()
25229f35bceSWilliam A. Kennington III {
25329f35bceSWilliam A. Kennington III     const char* path = "/etc/os-release";
25429f35bceSWilliam A. Kennington III     std::ifstream ifs(path);
25529f35bceSWilliam A. Kennington III     if (ifs.fail())
25629f35bceSWilliam A. Kennington III     {
25729f35bceSWilliam A. Kennington III         std::fprintf(stderr, "Failed to open: %s\n", path);
25829f35bceSWilliam A. Kennington III         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
25929f35bceSWilliam A. Kennington III     }
26029f35bceSWilliam A. Kennington III 
26129f35bceSWilliam A. Kennington III     std::string line;
26229f35bceSWilliam A. Kennington III     while (true)
26329f35bceSWilliam A. Kennington III     {
26429f35bceSWilliam A. Kennington III         std::getline(ifs, line);
26529f35bceSWilliam A. Kennington III         if (ifs.eof())
26629f35bceSWilliam A. Kennington III         {
26729f35bceSWilliam A. Kennington III             std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
26829f35bceSWilliam A. Kennington III                          path);
26929f35bceSWilliam A. Kennington III             throw IpmiException(IPMI_CC_INVALID);
27029f35bceSWilliam A. Kennington III         }
27129f35bceSWilliam A. Kennington III         if (ifs.fail())
27229f35bceSWilliam A. Kennington III         {
27329f35bceSWilliam A. Kennington III             std::fprintf(stderr, "Failed to read: %s\n", path);
27429f35bceSWilliam A. Kennington III             throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
27529f35bceSWilliam A. Kennington III         }
27629f35bceSWilliam A. Kennington III         std::string_view lineView(line);
27729f35bceSWilliam A. Kennington III         constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
27829f35bceSWilliam A. Kennington III         if (lineView.substr(0, prefix.size()) != prefix)
27929f35bceSWilliam A. Kennington III         {
28029f35bceSWilliam A. Kennington III             continue;
28129f35bceSWilliam A. Kennington III         }
28229f35bceSWilliam A. Kennington III         lineView.remove_prefix(prefix.size());
28329f35bceSWilliam A. Kennington III         lineView.remove_prefix(
28429f35bceSWilliam A. Kennington III             std::min(lineView.find_first_not_of('"'), lineView.size()));
28529f35bceSWilliam A. Kennington III         lineView.remove_suffix(
28629f35bceSWilliam A. Kennington III             lineView.size() - 1 -
28729f35bceSWilliam A. Kennington III             std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
28829f35bceSWilliam A. Kennington III         return std::string(lineView);
28929f35bceSWilliam A. Kennington III     }
29029f35bceSWilliam A. Kennington III }
29129f35bceSWilliam A. Kennington III 
292ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
293ab650004SPatrick Venture                                const Json& config)
29407f85150SPatrick Venture {
29507f85150SPatrick Venture     static const std::vector<Json> empty{};
29607f85150SPatrick Venture     std::vector<Json> readings = config.value(type, empty);
29707f85150SPatrick Venture     std::string name = "";
298ab650004SPatrick Venture 
29907f85150SPatrick Venture     for (const auto& j : readings)
30007f85150SPatrick Venture     {
30107f85150SPatrick Venture         uint8_t instanceNum = j.value("instance", 0);
30207f85150SPatrick Venture         // Not the instance we're interested in
30307f85150SPatrick Venture         if (instanceNum != instance)
30407f85150SPatrick Venture         {
30507f85150SPatrick Venture             continue;
30607f85150SPatrick Venture         }
30707f85150SPatrick Venture 
30807f85150SPatrick Venture         // Found the instance we're interested in
30907f85150SPatrick Venture         name = j.value("name", "");
31007f85150SPatrick Venture 
31107f85150SPatrick Venture         break;
31207f85150SPatrick Venture     }
313ab650004SPatrick Venture 
31407f85150SPatrick Venture     return name;
31507f85150SPatrick Venture }
31607f85150SPatrick Venture 
31749f23ad9SPatrick Venture void Handler::buildI2cPcieMapping()
31849f23ad9SPatrick Venture {
31949f23ad9SPatrick Venture     _pcie_i2c_map = buildPcieMap();
32049f23ad9SPatrick Venture }
32149f23ad9SPatrick Venture 
32249f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const
32349f23ad9SPatrick Venture {
32449f23ad9SPatrick Venture     return _pcie_i2c_map.size();
32549f23ad9SPatrick Venture }
32649f23ad9SPatrick Venture 
32749f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string>
32849f23ad9SPatrick Venture     Handler::getI2cEntry(unsigned int entry) const
32949f23ad9SPatrick Venture {
33049f23ad9SPatrick Venture     return _pcie_i2c_map[entry];
33149f23ad9SPatrick Venture }
33249f23ad9SPatrick Venture 
333f085d91dSPatrick Venture } // namespace ipmi
334f085d91dSPatrick Venture } // namespace google
335