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" 20d2037c6aSPatrick Venture 21d2037c6aSPatrick Venture #include <ipmid/api.h> 22d2037c6aSPatrick Venture 23bb90d4fdSPatrick Venture #include <cinttypes> 24d2037c6aSPatrick Venture #include <cstdio> 25d2037c6aSPatrick Venture #include <filesystem> 26d2037c6aSPatrick Venture #include <fstream> 27*07f85150SPatrick Venture #include <map> 28*07f85150SPatrick Venture #include <nlohmann/json.hpp> 29*07f85150SPatrick Venture #include <phosphor-logging/elog-errors.hpp> 30aa374120SPatrick Venture #include <phosphor-logging/log.hpp> 31aa374120SPatrick Venture #include <sdbusplus/bus.hpp> 32d2037c6aSPatrick Venture #include <sstream> 33d2037c6aSPatrick Venture #include <string> 34d2037c6aSPatrick Venture #include <tuple> 35*07f85150SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp> 36d2037c6aSPatrick Venture 37f085d91dSPatrick Venture // The phosphor-host-ipmi daemon requires a configuration that maps 38f085d91dSPatrick Venture // the if_name to the IPMI LAN channel. However, that doesn't strictly 39f085d91dSPatrick Venture // define which is meant to be used for NCSI. 40f085d91dSPatrick Venture #ifndef NCSI_IPMI_CHANNEL 41f085d91dSPatrick Venture #define NCSI_IPMI_CHANNEL 1 42f085d91dSPatrick Venture #endif 43f085d91dSPatrick Venture 44f085d91dSPatrick Venture #ifndef NCSI_IF_NAME 45f085d91dSPatrick Venture #define NCSI_IF_NAME eth0 46f085d91dSPatrick Venture #endif 47f085d91dSPatrick Venture 48f085d91dSPatrick Venture // To deal with receiving a string without quotes. 49f085d91dSPatrick Venture #define QUOTE(name) #name 50f085d91dSPatrick Venture #define STR(macro) QUOTE(macro) 51f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME) 52f085d91dSPatrick Venture 53f085d91dSPatrick Venture namespace google 54f085d91dSPatrick Venture { 55f085d91dSPatrick Venture namespace ipmi 56f085d91dSPatrick Venture { 57d2037c6aSPatrick Venture namespace fs = std::filesystem; 58*07f85150SPatrick Venture using Json = nlohmann::json; 59*07f85150SPatrick Venture using namespace phosphor::logging; 60*07f85150SPatrick Venture using InternalFailure = 61*07f85150SPatrick Venture sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 62f085d91dSPatrick Venture 63f085d91dSPatrick Venture std::tuple<std::uint8_t, std::string> Handler::getEthDetails() const 64f085d91dSPatrick Venture { 65f085d91dSPatrick Venture return std::make_tuple(NCSI_IPMI_CHANNEL, NCSI_IF_NAME_STR); 66f085d91dSPatrick Venture } 67f085d91dSPatrick Venture 68d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const 69d2037c6aSPatrick Venture { 70d2037c6aSPatrick Venture std::ostringstream opath; 71d2037c6aSPatrick Venture opath << "/sys/class/net/" << name << "/statistics/rx_packets"; 72d2037c6aSPatrick Venture std::string path = opath.str(); 73d2037c6aSPatrick Venture 74d2037c6aSPatrick Venture // Minor sanity & security check (of course, I'm less certain if unicode 75d2037c6aSPatrick Venture // comes into play here. 76d2037c6aSPatrick Venture // 77d2037c6aSPatrick Venture // Basically you can't easily inject ../ or /../ into the path below. 78d2037c6aSPatrick Venture if (name.find("/") != std::string::npos) 79d2037c6aSPatrick Venture { 80d2037c6aSPatrick Venture std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str()); 81d2037c6aSPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 82d2037c6aSPatrick Venture } 83d2037c6aSPatrick Venture 84d2037c6aSPatrick Venture std::error_code ec; 85d2037c6aSPatrick Venture if (!fs::exists(path, ec)) 86d2037c6aSPatrick Venture { 87d2037c6aSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str()); 88d2037c6aSPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 89d2037c6aSPatrick Venture } 90d2037c6aSPatrick Venture // We're uninterested in the state of ec. 91d2037c6aSPatrick Venture 92d2037c6aSPatrick Venture int64_t count = 0; 93d2037c6aSPatrick Venture std::ifstream ifs; 94d2037c6aSPatrick Venture ifs.exceptions(std::ifstream::failbit); 95d2037c6aSPatrick Venture try 96d2037c6aSPatrick Venture { 97d2037c6aSPatrick Venture ifs.open(path); 98d2037c6aSPatrick Venture ifs >> count; 99d2037c6aSPatrick Venture } 100d2037c6aSPatrick Venture catch (std::ios_base::failure& fail) 101d2037c6aSPatrick Venture { 102d2037c6aSPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 103d2037c6aSPatrick Venture } 104d2037c6aSPatrick Venture 105d2037c6aSPatrick Venture return count; 106d2037c6aSPatrick Venture } 107d2037c6aSPatrick Venture 108bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const 109bb90d4fdSPatrick Venture { 110bb90d4fdSPatrick Venture std::ostringstream opath; 111bb90d4fdSPatrick Venture opath << "/run/cpld" << id << ".version"; 112bb90d4fdSPatrick Venture // Check for file 113bb90d4fdSPatrick Venture 114bb90d4fdSPatrick Venture std::error_code ec; 115bb90d4fdSPatrick Venture if (!fs::exists(opath.str(), ec)) 116bb90d4fdSPatrick Venture { 117bb90d4fdSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", 118bb90d4fdSPatrick Venture opath.str().c_str()); 119bb90d4fdSPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 120bb90d4fdSPatrick Venture } 121bb90d4fdSPatrick Venture // We're uninterested in the state of ec. 122bb90d4fdSPatrick Venture 123bb90d4fdSPatrick Venture // If file exists, read. 124bb90d4fdSPatrick Venture std::ifstream ifs; 125bb90d4fdSPatrick Venture ifs.exceptions(std::ifstream::failbit); 126bb90d4fdSPatrick Venture std::string value; 127bb90d4fdSPatrick Venture try 128bb90d4fdSPatrick Venture { 129bb90d4fdSPatrick Venture ifs.open(opath.str()); 130bb90d4fdSPatrick Venture ifs >> value; 131bb90d4fdSPatrick Venture } 132bb90d4fdSPatrick Venture catch (std::ios_base::failure& fail) 133bb90d4fdSPatrick Venture { 134bb90d4fdSPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 135bb90d4fdSPatrick Venture } 136bb90d4fdSPatrick Venture 137bb90d4fdSPatrick Venture // If value parses as expected, return version. 138bb90d4fdSPatrick Venture VersionTuple version = std::make_tuple(0, 0, 0, 0); 139bb90d4fdSPatrick Venture 140bb90d4fdSPatrick Venture int num_fields = 141bb90d4fdSPatrick Venture std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, 142bb90d4fdSPatrick Venture &std::get<0>(version), &std::get<1>(version), 143bb90d4fdSPatrick Venture &std::get<2>(version), &std::get<3>(version)); 144bb90d4fdSPatrick Venture if (num_fields == 0) 145bb90d4fdSPatrick Venture { 146bb90d4fdSPatrick Venture std::fprintf(stderr, "Invalid version.\n"); 147bb90d4fdSPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 148bb90d4fdSPatrick Venture } 149bb90d4fdSPatrick Venture 150bb90d4fdSPatrick Venture return version; 151bb90d4fdSPatrick Venture } 152bb90d4fdSPatrick Venture 153aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay"; 154aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 155aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 156aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 157aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target"; 158aa374120SPatrick Venture 159aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const 160aa374120SPatrick Venture { 161aa374120SPatrick Venture using namespace phosphor::logging; 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 201*07f85150SPatrick Venture static Json config{}; 202*07f85150SPatrick Venture static bool parsed = false; 203*07f85150SPatrick Venture static const std::map<uint8_t, std::string> entityIdToName{ 204*07f85150SPatrick Venture {0x03, "cpu"}, 205*07f85150SPatrick Venture {0x04, "storage_device"}, 206*07f85150SPatrick Venture {0x06, "system_management_module"}, 207*07f85150SPatrick Venture {0x08, "memory_module"}, 208*07f85150SPatrick Venture {0x0B, "add_in_card"}, 209*07f85150SPatrick Venture {0x17, "system_chassis"}, 210*07f85150SPatrick Venture {0x20, "memory_device"}}; 211*07f85150SPatrick Venture static constexpr auto configFile = 212*07f85150SPatrick Venture "/usr/share/ipmi-entity-association/entity_association_map.json"; 213*07f85150SPatrick Venture 214*07f85150SPatrick Venture Json parse_config() 215*07f85150SPatrick Venture { 216*07f85150SPatrick Venture std::ifstream jsonFile(configFile); 217*07f85150SPatrick Venture if (!jsonFile.is_open()) 218*07f85150SPatrick Venture { 219*07f85150SPatrick Venture log<level::ERR>("Entity association JSON file not found"); 220*07f85150SPatrick Venture elog<InternalFailure>(); 221*07f85150SPatrick Venture } 222*07f85150SPatrick Venture 223*07f85150SPatrick Venture auto data = Json::parse(jsonFile, nullptr, false); 224*07f85150SPatrick Venture if (data.is_discarded()) 225*07f85150SPatrick Venture { 226*07f85150SPatrick Venture log<level::ERR>("Entity association JSON parser failure"); 227*07f85150SPatrick Venture elog<InternalFailure>(); 228*07f85150SPatrick Venture } 229*07f85150SPatrick Venture 230*07f85150SPatrick Venture return data; 231*07f85150SPatrick Venture } 232*07f85150SPatrick Venture 233*07f85150SPatrick Venture std::string read(const std::string& type, uint8_t instance, const Json& config) 234*07f85150SPatrick Venture { 235*07f85150SPatrick Venture static const std::vector<Json> empty{}; 236*07f85150SPatrick Venture std::vector<Json> readings = config.value(type, empty); 237*07f85150SPatrick Venture std::string name = ""; 238*07f85150SPatrick Venture for (const auto& j : readings) 239*07f85150SPatrick Venture { 240*07f85150SPatrick Venture uint8_t instanceNum = j.value("instance", 0); 241*07f85150SPatrick Venture // Not the instance we're interested in 242*07f85150SPatrick Venture if (instanceNum != instance) 243*07f85150SPatrick Venture { 244*07f85150SPatrick Venture continue; 245*07f85150SPatrick Venture } 246*07f85150SPatrick Venture 247*07f85150SPatrick Venture // Found the instance we're interested in 248*07f85150SPatrick Venture name = j.value("name", ""); 249*07f85150SPatrick Venture 250*07f85150SPatrick Venture break; 251*07f85150SPatrick Venture } 252*07f85150SPatrick Venture return name; 253*07f85150SPatrick Venture } 254*07f85150SPatrick Venture 255*07f85150SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance) 256*07f85150SPatrick Venture { 257*07f85150SPatrick Venture // Check if we support this Entity ID. 258*07f85150SPatrick Venture auto it = entityIdToName.find(id); 259*07f85150SPatrick Venture if (it == entityIdToName.end()) 260*07f85150SPatrick Venture { 261*07f85150SPatrick Venture log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id)); 262*07f85150SPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 263*07f85150SPatrick Venture } 264*07f85150SPatrick Venture 265*07f85150SPatrick Venture std::string entityName; 266*07f85150SPatrick Venture try 267*07f85150SPatrick Venture { 268*07f85150SPatrick Venture // Parse the JSON config file. 269*07f85150SPatrick Venture if (!parsed) 270*07f85150SPatrick Venture { 271*07f85150SPatrick Venture config = parse_config(); 272*07f85150SPatrick Venture parsed = true; 273*07f85150SPatrick Venture } 274*07f85150SPatrick Venture 275*07f85150SPatrick Venture // Find the "entity id:entity instance" mapping to entity name. 276*07f85150SPatrick Venture entityName = read(it->second, instance, config); 277*07f85150SPatrick Venture if (entityName.empty()) 278*07f85150SPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 279*07f85150SPatrick Venture } 280*07f85150SPatrick Venture catch (InternalFailure& e) 281*07f85150SPatrick Venture { 282*07f85150SPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 283*07f85150SPatrick Venture } 284*07f85150SPatrick Venture 285*07f85150SPatrick Venture return entityName; 286*07f85150SPatrick Venture } 287*07f85150SPatrick Venture 288f085d91dSPatrick Venture Handler handlerImpl; 289f085d91dSPatrick Venture 290f085d91dSPatrick Venture } // namespace ipmi 291f085d91dSPatrick Venture } // namespace google 292