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 |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 49 | bool checkFirmwareConditionRunning(sdbusplus::bus_t& 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 | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 66 | catch (const sdbusplus::exception_t& 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); |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 104 | std::variant<FirmwareCondition> currentFwCondV; |
| 105 | response.read(currentFwCondV); |
| 106 | auto currentFwCond = |
| 107 | std::get<FirmwareCondition>(currentFwCondV); |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 108 | |
NodeMan97 | ef828c4 | 2022-05-31 11:24:32 -0500 | [diff] [blame] | 109 | info( |
| 110 | "Read host fw condition {COND_VALUE} from {COND_SERVICE}, {COND_PATH}", |
| 111 | "COND_VALUE", currentFwCond, "COND_SERVICE", service, |
| 112 | "COND_PATH", path); |
| 113 | |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 114 | if (currentFwCond == FirmwareCondition::Running) |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 115 | { |
| 116 | return true; |
| 117 | } |
| 118 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 119 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 120 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 121 | error("Error reading HostFirmware condition, error: {ERROR}, " |
| 122 | "service: {SERVICE} path: {PATH}", |
| 123 | "ERROR", e, "SERVICE", service, "PATH", path); |
Andrew Geissler | 2d7b69e | 2021-05-06 13:12:45 -0500 | [diff] [blame] | 124 | throw; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return false; |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 131 | // Helper function to check if chassis power is on |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 132 | bool isChassiPowerOn(sdbusplus::bus_t& bus, size_t id) |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 133 | { |
Potin Lai | 07e73aa | 2022-03-29 15:16:55 +0800 | [diff] [blame] | 134 | auto svcname = std::string{CHASSIS_STATE_SVC} + std::to_string(id); |
| 135 | auto objpath = std::string{CHASSIS_STATE_PATH} + std::to_string(id); |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 136 | |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 137 | try |
| 138 | { |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 139 | using PowerState = |
| 140 | sdbusplus::xyz::openbmc_project::State::server::Chassis::PowerState; |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 141 | auto method = bus.new_method_call(svcname.c_str(), objpath.c_str(), |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 142 | PROPERTY_INTERFACE, "Get"); |
| 143 | method.append(CHASSIS_STATE_INTF, CHASSIS_STATE_POWER_PROP); |
| 144 | |
| 145 | auto response = bus.call(method); |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 146 | std::variant<PowerState> currentPowerStateV; |
| 147 | response.read(currentPowerStateV); |
NodeMan97 | ef828c4 | 2022-05-31 11:24:32 -0500 | [diff] [blame] | 148 | |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 149 | auto currentPowerState = std::get<PowerState>(currentPowerStateV); |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 150 | |
NodeMan97 | 80db475 | 2022-05-04 09:06:40 -0500 | [diff] [blame] | 151 | if (currentPowerState == PowerState::On) |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 152 | { |
| 153 | return true; |
| 154 | } |
| 155 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 156 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 157 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 158 | error("Error reading Chassis Power State, error: {ERROR}, " |
| 159 | "service: {SERVICE} path: {PATH}", |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 160 | "ERROR", e, "SERVICE", svcname.c_str(), "PATH", objpath.c_str()); |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 161 | throw; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 166 | bool isHostRunning(size_t id) |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 167 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 168 | info("Check if host is running"); |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 169 | |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 170 | auto bus = sdbusplus::bus::new_default(); |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 171 | |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 172 | // No need to check if chassis power is not on |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 173 | if (!isChassiPowerOn(bus, id)) |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 174 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 175 | info("Chassis power not on, exit"); |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 179 | // This applications systemd service is setup to only run after all other |
| 180 | // application that could possibly implement the needed interface have |
| 181 | // been started. However, the use of mapper to find those interfaces means |
| 182 | // we have a condition where the interface may be on D-Bus but not stored |
NodeMan97 | ef828c4 | 2022-05-31 11:24:32 -0500 | [diff] [blame] | 183 | // within mapper yet. There are five built in retries to check if it's |
| 184 | // found the host is not up. This service is only called if chassis power |
| 185 | // is on when the BMC comes up, so this wont impact most normal cases |
| 186 | // where the BMC is rebooted with chassis power off. In cases where |
| 187 | // chassis power is on, the host is likely running so we want to be sure |
| 188 | // we check all interfaces |
| 189 | for (int i = 0; i < 5; i++) |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 190 | { |
NodeMan97 | ef828c4 | 2022-05-31 11:24:32 -0500 | [diff] [blame] | 191 | debug( |
| 192 | "Introspecting new bus objects for bus id: {ID} sleeping for 1 second.", |
| 193 | "ID", id); |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 194 | // Give mapper a small window to introspect new objects on bus |
NodeMan97 | ef828c4 | 2022-05-31 11:24:32 -0500 | [diff] [blame] | 195 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 196 | if (checkFirmwareConditionRunning(bus)) |
| 197 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 198 | info("Host is running!"); |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 199 | // Create file for host instance and create in filesystem to |
| 200 | // indicate to services that host is running |
| 201 | auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0); |
| 202 | size++; // null |
| 203 | std::unique_ptr<char[]> buf(new char[size]); |
| 204 | std::snprintf(buf.get(), size, HOST_RUNNING_FILE, 0); |
| 205 | std::ofstream outfile(buf.get()); |
| 206 | outfile.close(); |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 207 | return true; |
Andrew Geissler | cd0ebe8 | 2021-05-14 21:39:51 -0500 | [diff] [blame] | 208 | } |
Andrew Geissler | 07d0ed5 | 2017-03-28 15:44:18 -0500 | [diff] [blame] | 209 | } |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 210 | info("Host is not running!"); |
Andrew Geissler | 0d1c3f1 | 2021-07-27 16:21:01 -0400 | [diff] [blame] | 211 | return false; |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 212 | } |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 213 | |
| 214 | } // namespace manager |
| 215 | } // namespace state |
| 216 | } // namespace phosphor |