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