Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 3 | #include "host_check.hpp" |
| 4 | |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 5 | #include <unistd.h> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 6 | |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 7 | #include <boost/range/adaptor/reversed.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 9 | #include <sdbusplus/bus.hpp> |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 10 | #include <sdbusplus/exception.hpp> |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Condition/HostFirmware/server.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 12 | |
| 13 | #include <cstdio> |
| 14 | #include <cstdlib> |
| 15 | #include <fstream> |
| 16 | #include <iostream> |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 17 | #include <thread> |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 18 | #include <vector> |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 19 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 20 | using namespace std::literals; |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 21 | using namespace phosphor::logging; |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 22 | using namespace sdbusplus::xyz::openbmc_project::Condition::server; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 23 | using sdbusplus::exception::SdBusError; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 24 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 25 | // Required strings for sending the msg to check on host |
| 26 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 27 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 28 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 29 | constexpr auto CONDITION_HOST_INTERFACE = |
| 30 | "xyz.openbmc_project.Condition.HostFirmware"; |
| 31 | constexpr auto CONDITION_HOST_PROPERTY = "CurrentFirmwareCondition"; |
| 32 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 33 | |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 34 | constexpr auto CHASSIS_STATE_SVC = "xyz.openbmc_project.State.Chassis"; |
| 35 | constexpr auto CHASSIS_STATE_PATH = "/xyz/openbmc_project/state/chassis0"; |
| 36 | constexpr auto CHASSIS_STATE_INTF = "xyz.openbmc_project.State.Chassis"; |
| 37 | constexpr auto CHASSIS_STATE_POWER_PROP = "CurrentPowerState"; |
| 38 | |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 39 | // Find all implementations of Condition interface and check if host is |
| 40 | // running over it |
| 41 | bool checkFirmwareConditionRunning(sdbusplus::bus::bus& bus) |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 42 | { |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 43 | // Find all implementations of host firmware condition interface |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 44 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 45 | MAPPER_INTERFACE, "GetSubTree"); |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 46 | |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 47 | mapper.append("/", 0, std::vector<std::string>({CONDITION_HOST_INTERFACE})); |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 48 | |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 49 | std::map<std::string, std::map<std::string, std::vector<std::string>>> |
| 50 | mapperResponse; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 51 | |
| 52 | try |
| 53 | { |
| 54 | auto mapperResponseMsg = bus.call(mapper); |
| 55 | mapperResponseMsg.read(mapperResponse); |
| 56 | } |
| 57 | catch (const SdBusError& e) |
| 58 | { |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 59 | log<level::ERR>( |
| 60 | "Error in mapper GetSubTree call for HostFirmware condition", |
| 61 | entry("ERROR=%s", e.what())); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 62 | throw; |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 65 | if (mapperResponse.empty()) |
| 66 | { |
| 67 | log<level::INFO>( |
| 68 | "Mapper response for HostFirmware conditions is empty!"); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // Now read the CurrentFirmwareCondition from all interfaces we found |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 73 | // Currently there are two implementations of this interface. One by IPMI |
| 74 | // and one by PLDM. The IPMI interface does a realtime check with the host |
| 75 | // when the interface is called. This means if the host is not running, |
| 76 | // we will have to wait for the timeout (currently set to 3 seconds). The |
| 77 | // PLDM interface reads a cached state. The PLDM service does not put itself |
| 78 | // on D-Bus until it has checked with the host. Therefore it's most |
| 79 | // efficient to call the PLDM interface first. Do that by going in reverse |
| 80 | // of the interfaces returned to us (PLDM will be last if available) |
| 81 | for (const auto& [path, services] : |
| 82 | boost::adaptors::reverse(mapperResponse)) |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 83 | { |
| 84 | for (const auto& serviceIter : services) |
| 85 | { |
| 86 | const std::string& service = serviceIter.first; |
| 87 | |
| 88 | try |
| 89 | { |
| 90 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 91 | PROPERTY_INTERFACE, "Get"); |
| 92 | method.append(CONDITION_HOST_INTERFACE, |
| 93 | CONDITION_HOST_PROPERTY); |
| 94 | |
| 95 | auto response = bus.call(method); |
| 96 | |
| 97 | std::variant<std::string> currentFwCond; |
| 98 | response.read(currentFwCond); |
| 99 | |
| 100 | if (std::get<std::string>(currentFwCond) == |
| 101 | "xyz.openbmc_project.Condition.HostFirmware." |
| 102 | "FirmwareCondition." |
| 103 | "Running") |
| 104 | { |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | catch (const SdBusError& e) |
| 109 | { |
| 110 | log<level::ERR>("Error reading HostFirmware condition", |
| 111 | entry("ERROR=%s", e.what()), |
| 112 | entry("SERVICE=%s", service.c_str()), |
| 113 | entry("PATH=%s", path.c_str())); |
| 114 | throw; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | return false; |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 121 | // Helper function to check if chassis power is on |
| 122 | bool isChassiPowerOn(sdbusplus::bus::bus& bus) |
| 123 | { |
| 124 | try |
| 125 | { |
| 126 | auto method = bus.new_method_call(CHASSIS_STATE_SVC, CHASSIS_STATE_PATH, |
| 127 | PROPERTY_INTERFACE, "Get"); |
| 128 | method.append(CHASSIS_STATE_INTF, CHASSIS_STATE_POWER_PROP); |
| 129 | |
| 130 | auto response = bus.call(method); |
| 131 | |
| 132 | std::variant<std::string> currentPowerState; |
| 133 | response.read(currentPowerState); |
| 134 | |
| 135 | if (std::get<std::string>(currentPowerState) == |
| 136 | "xyz.openbmc_project.State.Chassis.PowerState.On") |
| 137 | { |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | catch (const SdBusError& e) |
| 142 | { |
| 143 | log<level::ERR>("Error reading Chassis Power State", |
| 144 | entry("ERROR=%s", e.what()), |
| 145 | entry("SERVICE=%s", CHASSIS_STATE_SVC), |
| 146 | entry("PATH=%s", CHASSIS_STATE_PATH)); |
| 147 | |
| 148 | throw; |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | bool isHostRunning() |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 154 | { |
| 155 | log<level::INFO>("Check if host is running"); |
| 156 | |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 157 | auto bus = sdbusplus::bus::new_default(); |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 158 | |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 159 | // No need to check if chassis power is not on |
| 160 | if (!isChassiPowerOn(bus)) |
| 161 | { |
| 162 | log<level::INFO>("Chassis power not on, exit"); |
| 163 | return false; |
| 164 | } |
| 165 | |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 166 | // This applications systemd service is setup to only run after all other |
| 167 | // application that could possibly implement the needed interface have |
| 168 | // been started. However, the use of mapper to find those interfaces means |
| 169 | // we have a condition where the interface may be on D-Bus but not stored |
| 170 | // within mapper yet. Keep it simple and just build one retry into the |
| 171 | // check if it's found the host is not up. This service is only called if |
| 172 | // chassis power is on when the BMC comes up, so this wont impact most |
| 173 | // normal cases where the BMC is rebooted with chassis power off. In |
| 174 | // cases where chassis power is on, the host is likely running so we want |
| 175 | // to be sure we check all interfaces |
| 176 | for (int i = 0; i < 2; i++) |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 177 | { |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 178 | // Give mapper a small window to introspect new objects on bus |
| 179 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 180 | if (checkFirmwareConditionRunning(bus)) |
| 181 | { |
| 182 | log<level::INFO>("Host is running!"); |
| 183 | // Create file for host instance and create in filesystem to |
| 184 | // indicate to services that host is running |
| 185 | auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0); |
| 186 | size++; // null |
| 187 | std::unique_ptr<char[]> buf(new char[size]); |
| 188 | std::snprintf(buf.get(), size, HOST_RUNNING_FILE, 0); |
| 189 | std::ofstream outfile(buf.get()); |
| 190 | outfile.close(); |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 191 | return true; |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 192 | } |
Andrew Geissler | 07d0ed5 | 2017-03-28 15:44:18 -0500 | [diff] [blame] | 193 | } |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 194 | log<level::INFO>("Host is not running!"); |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 195 | return false; |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 196 | } |