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