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