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