xref: /openbmc/google-ipmi-sys/handler.cpp (revision c87de558)
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"
20*c87de558SPatrick 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>
36d2037c6aSPatrick Venture #include <tuple>
3707f85150SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp>
38d2037c6aSPatrick Venture 
39f085d91dSPatrick Venture // The phosphor-host-ipmi daemon requires a configuration that maps
40f085d91dSPatrick Venture // the if_name to the IPMI LAN channel.  However, that doesn't strictly
41f085d91dSPatrick Venture // define which is meant to be used for NCSI.
42f085d91dSPatrick Venture #ifndef NCSI_IPMI_CHANNEL
43f085d91dSPatrick Venture #define NCSI_IPMI_CHANNEL 1
44f085d91dSPatrick Venture #endif
45f085d91dSPatrick Venture 
46f085d91dSPatrick Venture #ifndef NCSI_IF_NAME
47f085d91dSPatrick Venture #define NCSI_IF_NAME eth0
48f085d91dSPatrick Venture #endif
49f085d91dSPatrick Venture 
50f085d91dSPatrick Venture // To deal with receiving a string without quotes.
51f085d91dSPatrick Venture #define QUOTE(name) #name
52f085d91dSPatrick Venture #define STR(macro) QUOTE(macro)
53f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
54f085d91dSPatrick Venture 
55f085d91dSPatrick Venture namespace google
56f085d91dSPatrick Venture {
57f085d91dSPatrick Venture namespace ipmi
58f085d91dSPatrick Venture {
59d2037c6aSPatrick Venture namespace fs = std::filesystem;
6007f85150SPatrick Venture using Json = nlohmann::json;
6107f85150SPatrick Venture using namespace phosphor::logging;
6207f85150SPatrick Venture using InternalFailure =
6307f85150SPatrick Venture     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
64f085d91dSPatrick Venture 
65f085d91dSPatrick Venture std::tuple<std::uint8_t, std::string> Handler::getEthDetails() const
66f085d91dSPatrick Venture {
67f085d91dSPatrick Venture     return std::make_tuple(NCSI_IPMI_CHANNEL, NCSI_IF_NAME_STR);
68f085d91dSPatrick Venture }
69f085d91dSPatrick Venture 
70d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const
71d2037c6aSPatrick Venture {
72d2037c6aSPatrick Venture     std::ostringstream opath;
73d2037c6aSPatrick Venture     opath << "/sys/class/net/" << name << "/statistics/rx_packets";
74d2037c6aSPatrick Venture     std::string path = opath.str();
75d2037c6aSPatrick Venture 
76d2037c6aSPatrick Venture     // Minor sanity & security check (of course, I'm less certain if unicode
77d2037c6aSPatrick Venture     // comes into play here.
78d2037c6aSPatrick Venture     //
79d2037c6aSPatrick Venture     // Basically you can't easily inject ../ or /../ into the path below.
80d2037c6aSPatrick Venture     if (name.find("/") != std::string::npos)
81d2037c6aSPatrick Venture     {
82d2037c6aSPatrick Venture         std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
83d2037c6aSPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
84d2037c6aSPatrick Venture     }
85d2037c6aSPatrick Venture 
86d2037c6aSPatrick Venture     std::error_code ec;
87d2037c6aSPatrick Venture     if (!fs::exists(path, ec))
88d2037c6aSPatrick Venture     {
89d2037c6aSPatrick Venture         std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
90d2037c6aSPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
91d2037c6aSPatrick Venture     }
92d2037c6aSPatrick Venture     // We're uninterested in the state of ec.
93d2037c6aSPatrick Venture 
94d2037c6aSPatrick Venture     int64_t count = 0;
95d2037c6aSPatrick Venture     std::ifstream ifs;
96d2037c6aSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
97d2037c6aSPatrick Venture     try
98d2037c6aSPatrick Venture     {
99d2037c6aSPatrick Venture         ifs.open(path);
100d2037c6aSPatrick Venture         ifs >> count;
101d2037c6aSPatrick Venture     }
102d2037c6aSPatrick Venture     catch (std::ios_base::failure& fail)
103d2037c6aSPatrick Venture     {
104d2037c6aSPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
105d2037c6aSPatrick Venture     }
106d2037c6aSPatrick Venture 
107d2037c6aSPatrick Venture     return count;
108d2037c6aSPatrick Venture }
109d2037c6aSPatrick Venture 
110bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const
111bb90d4fdSPatrick Venture {
112bb90d4fdSPatrick Venture     std::ostringstream opath;
113bb90d4fdSPatrick Venture     opath << "/run/cpld" << id << ".version";
114bb90d4fdSPatrick Venture     // Check for file
115bb90d4fdSPatrick Venture 
116bb90d4fdSPatrick Venture     std::error_code ec;
117bb90d4fdSPatrick Venture     if (!fs::exists(opath.str(), ec))
118bb90d4fdSPatrick Venture     {
119bb90d4fdSPatrick Venture         std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
120bb90d4fdSPatrick Venture                      opath.str().c_str());
121bb90d4fdSPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
122bb90d4fdSPatrick Venture     }
123bb90d4fdSPatrick Venture     // We're uninterested in the state of ec.
124bb90d4fdSPatrick Venture 
125bb90d4fdSPatrick Venture     // If file exists, read.
126bb90d4fdSPatrick Venture     std::ifstream ifs;
127bb90d4fdSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
128bb90d4fdSPatrick Venture     std::string value;
129bb90d4fdSPatrick Venture     try
130bb90d4fdSPatrick Venture     {
131bb90d4fdSPatrick Venture         ifs.open(opath.str());
132bb90d4fdSPatrick Venture         ifs >> value;
133bb90d4fdSPatrick Venture     }
134bb90d4fdSPatrick Venture     catch (std::ios_base::failure& fail)
135bb90d4fdSPatrick Venture     {
136bb90d4fdSPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
137bb90d4fdSPatrick Venture     }
138bb90d4fdSPatrick Venture 
139bb90d4fdSPatrick Venture     // If value parses as expected, return version.
140bb90d4fdSPatrick Venture     VersionTuple version = std::make_tuple(0, 0, 0, 0);
141bb90d4fdSPatrick Venture 
142bb90d4fdSPatrick Venture     int num_fields =
143bb90d4fdSPatrick Venture         std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
144bb90d4fdSPatrick Venture                     &std::get<0>(version), &std::get<1>(version),
145bb90d4fdSPatrick Venture                     &std::get<2>(version), &std::get<3>(version));
146bb90d4fdSPatrick Venture     if (num_fields == 0)
147bb90d4fdSPatrick Venture     {
148bb90d4fdSPatrick Venture         std::fprintf(stderr, "Invalid version.\n");
149bb90d4fdSPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
150bb90d4fdSPatrick Venture     }
151bb90d4fdSPatrick Venture 
152bb90d4fdSPatrick Venture     return version;
153bb90d4fdSPatrick Venture }
154bb90d4fdSPatrick Venture 
155aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
156aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
157aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
158aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
159aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
160aa374120SPatrick Venture 
161aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const
162aa374120SPatrick Venture {
163aa374120SPatrick Venture     std::ofstream ofs;
164aa374120SPatrick Venture     ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
165aa374120SPatrick Venture     if (!ofs.good())
166aa374120SPatrick Venture     {
167aa374120SPatrick Venture         std::fprintf(stderr, "Unable to open file for output.\n");
168aa374120SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
169aa374120SPatrick Venture     }
170aa374120SPatrick Venture 
171aa374120SPatrick Venture     ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
172aa374120SPatrick Venture     if (ofs.fail())
173aa374120SPatrick Venture     {
174aa374120SPatrick Venture         std::fprintf(stderr, "Write failed\n");
175aa374120SPatrick Venture         ofs.close();
176aa374120SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
177aa374120SPatrick Venture     }
178aa374120SPatrick Venture 
179aa374120SPatrick Venture     // Write succeeded, please continue.
180aa374120SPatrick Venture     ofs.flush();
181aa374120SPatrick Venture     ofs.close();
182aa374120SPatrick Venture 
183aa374120SPatrick Venture     auto bus = sdbusplus::bus::new_default();
184aa374120SPatrick Venture     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
185aa374120SPatrick Venture                                       SYSTEMD_INTERFACE, "StartUnit");
186aa374120SPatrick Venture 
187aa374120SPatrick Venture     method.append(PSU_HARDRESET_TARGET);
188aa374120SPatrick Venture     method.append("replace");
189aa374120SPatrick Venture 
190aa374120SPatrick Venture     try
191aa374120SPatrick Venture     {
192aa374120SPatrick Venture         bus.call_noreply(method);
193aa374120SPatrick Venture     }
194aa374120SPatrick Venture     catch (const sdbusplus::exception::SdBusError& ex)
195aa374120SPatrick Venture     {
196aa374120SPatrick Venture         log<level::ERR>("Failed to call PSU hard reset");
197aa374120SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
198aa374120SPatrick Venture     }
199aa374120SPatrick Venture }
200aa374120SPatrick Venture 
201ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
20207f85150SPatrick Venture {
203ab650004SPatrick Venture     // Check if we support this Entity ID.
204ab650004SPatrick Venture     auto it = _entityIdToName.find(id);
205ab650004SPatrick Venture     if (it == _entityIdToName.end())
20607f85150SPatrick Venture     {
207ab650004SPatrick Venture         log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
208ab650004SPatrick Venture         throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
20907f85150SPatrick Venture     }
21007f85150SPatrick Venture 
211ab650004SPatrick Venture     std::string entityName;
212ab650004SPatrick Venture     try
21307f85150SPatrick Venture     {
214ab650004SPatrick Venture         // Parse the JSON config file.
215ab650004SPatrick Venture         if (!_entityConfigParsed)
216ab650004SPatrick Venture         {
217ab650004SPatrick Venture             _entityConfig = parseConfig(_configFile);
218ab650004SPatrick Venture             _entityConfigParsed = true;
21907f85150SPatrick Venture         }
22007f85150SPatrick Venture 
221ab650004SPatrick Venture         // Find the "entity id:entity instance" mapping to entity name.
222ab650004SPatrick Venture         entityName = readNameFromConfig(it->second, instance, _entityConfig);
223ab650004SPatrick Venture         if (entityName.empty())
224ab650004SPatrick Venture         {
225ab650004SPatrick Venture             throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
226ab650004SPatrick Venture         }
227ab650004SPatrick Venture     }
228ab650004SPatrick Venture     catch (InternalFailure& e)
229ab650004SPatrick Venture     {
230ab650004SPatrick Venture         throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
23107f85150SPatrick Venture     }
23207f85150SPatrick Venture 
233ab650004SPatrick Venture     return entityName;
234ab650004SPatrick Venture }
235ab650004SPatrick Venture 
236ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
237ab650004SPatrick Venture                                const Json& config)
23807f85150SPatrick Venture {
23907f85150SPatrick Venture     static const std::vector<Json> empty{};
24007f85150SPatrick Venture     std::vector<Json> readings = config.value(type, empty);
24107f85150SPatrick Venture     std::string name = "";
242ab650004SPatrick Venture 
24307f85150SPatrick Venture     for (const auto& j : readings)
24407f85150SPatrick Venture     {
24507f85150SPatrick Venture         uint8_t instanceNum = j.value("instance", 0);
24607f85150SPatrick Venture         // Not the instance we're interested in
24707f85150SPatrick Venture         if (instanceNum != instance)
24807f85150SPatrick Venture         {
24907f85150SPatrick Venture             continue;
25007f85150SPatrick Venture         }
25107f85150SPatrick Venture 
25207f85150SPatrick Venture         // Found the instance we're interested in
25307f85150SPatrick Venture         name = j.value("name", "");
25407f85150SPatrick Venture 
25507f85150SPatrick Venture         break;
25607f85150SPatrick Venture     }
257ab650004SPatrick Venture 
25807f85150SPatrick Venture     return name;
25907f85150SPatrick Venture }
26007f85150SPatrick Venture 
26149f23ad9SPatrick Venture void Handler::buildI2cPcieMapping()
26249f23ad9SPatrick Venture {
26349f23ad9SPatrick Venture     _pcie_i2c_map = buildPcieMap();
26449f23ad9SPatrick Venture }
26549f23ad9SPatrick Venture 
26649f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const
26749f23ad9SPatrick Venture {
26849f23ad9SPatrick Venture     return _pcie_i2c_map.size();
26949f23ad9SPatrick Venture }
27049f23ad9SPatrick Venture 
27149f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string>
27249f23ad9SPatrick Venture     Handler::getI2cEntry(unsigned int entry) const
27349f23ad9SPatrick Venture {
27449f23ad9SPatrick Venture     return _pcie_i2c_map[entry];
27549f23ad9SPatrick Venture }
27649f23ad9SPatrick Venture 
277ab650004SPatrick Venture const std::string defaultConfigFile =
278ab650004SPatrick Venture     "/usr/share/ipmi-entity-association/entity_association_map.json";
27907f85150SPatrick Venture 
280f085d91dSPatrick Venture } // namespace ipmi
281f085d91dSPatrick Venture } // namespace google
282