blob: d866085d49a6be30f50485edf4a8c65ec1fcff40 [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "config.h"
2
Andrew Geissler0d1c3f12021-07-27 16:21:01 -04003#include "host_check.hpp"
4
Andrew Geissler0971dea2017-03-28 14:32:40 -05005#include <unistd.h>
Andrew Geisslere426b582020-05-28 12:40:55 -05006
Andrew Geissler8ffdb262021-09-20 15:25:19 -05007#include <phosphor-logging/lg2.hpp>
Andrew Geisslerb09463d2017-03-24 15:55:17 -05008#include <sdbusplus/bus.hpp>
Anthony Wilson32c532e2018-10-25 21:56:07 -05009#include <sdbusplus/exception.hpp>
Patrick Williams9a286db2024-01-17 06:29:47 -060010#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 Geisslere426b582020-05-28 12:40:55 -050013
14#include <cstdio>
15#include <cstdlib>
16#include <fstream>
17#include <iostream>
Patrick Williamse9040bb2024-01-30 14:12:44 -060018#include <ranges>
Andrew Geisslercd0ebe82021-05-14 21:39:51 -050019#include <thread>
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050020#include <vector>
Andrew Geisslerb09463d2017-03-24 15:55:17 -050021
Andrew Geissler8ffdb262021-09-20 15:25:19 -050022namespace phosphor
23{
24namespace state
25{
26namespace manager
27{
28
29PHOSPHOR_LOG2_USING;
30
Andrew Geisslereeaccf82017-03-28 15:23:27 -050031using namespace std::literals;
Patrick Williams9a286db2024-01-17 06:29:47 -060032
33using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
34using Chassis = sdbusplus::client::xyz::openbmc_project::state::Chassis<>;
35using HostFirmware =
36 sdbusplus::client::xyz::openbmc_project::condition::HostFirmware<>;
Andrew Geissler0971dea2017-03-28 14:32:40 -050037
Andrew Geisslereeaccf82017-03-28 15:23:27 -050038// Required strings for sending the msg to check on host
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050039constexpr auto CONDITION_HOST_PROPERTY = "CurrentFirmwareCondition";
40constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
Andrew Geissler0971dea2017-03-28 14:32:40 -050041
Andrew Geissler0d1c3f12021-07-27 16:21:01 -040042constexpr auto CHASSIS_STATE_SVC = "xyz.openbmc_project.State.Chassis";
Andrew Geissler0d1c3f12021-07-27 16:21:01 -040043constexpr auto CHASSIS_STATE_POWER_PROP = "CurrentPowerState";
44
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050045// Find all implementations of Condition interface and check if host is
46// running over it
Patrick Williamsf053e6f2022-07-22 19:26:54 -050047bool checkFirmwareConditionRunning(sdbusplus::bus_t& bus)
Andrew Geissler0971dea2017-03-28 14:32:40 -050048{
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050049 // Find all implementations of host firmware condition interface
Patrick Williams9a286db2024-01-17 06:29:47 -060050 auto mapper = bus.new_method_call(ObjectMapper::default_service,
51 ObjectMapper::instance_path,
52 ObjectMapper::interface, "GetSubTree");
Andrew Geisslereeaccf82017-03-28 15:23:27 -050053
Patrick Williams9a286db2024-01-17 06:29:47 -060054 mapper.append("/", 0, std::vector<std::string>({HostFirmware::interface}));
Andrew Geisslereeaccf82017-03-28 15:23:27 -050055
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050056 std::map<std::string, std::map<std::string, std::vector<std::string>>>
57 mapperResponse;
Anthony Wilson32c532e2018-10-25 21:56:07 -050058
59 try
60 {
61 auto mapperResponseMsg = bus.call(mapper);
62 mapperResponseMsg.read(mapperResponse);
63 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -050064 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050065 {
Andrew Geisslerad65b2d2021-09-21 12:53:29 -050066 error(
67 "Error in mapper GetSubTree call for HostFirmware condition: {ERROR}",
68 "ERROR", e);
Anthony Wilson32c532e2018-10-25 21:56:07 -050069 throw;
Andrew Geisslereeaccf82017-03-28 15:23:27 -050070 }
71
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050072 if (mapperResponse.empty())
73 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050074 info("Mapper response for HostFirmware conditions is empty!");
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050075 return false;
76 }
77
78 // Now read the CurrentFirmwareCondition from all interfaces we found
Andrew Geisslercd0ebe82021-05-14 21:39:51 -050079 // 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)
Patrick Williamse9040bb2024-01-30 14:12:44 -060087 for (const auto& [path, services] : std::views::reverse(mapperResponse))
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050088 {
89 for (const auto& serviceIter : services)
90 {
91 const std::string& service = serviceIter.first;
92
93 try
94 {
95 auto method = bus.new_method_call(service.c_str(), path.c_str(),
96 PROPERTY_INTERFACE, "Get");
Patrick Williams9a286db2024-01-17 06:29:47 -060097 method.append(HostFirmware::interface, CONDITION_HOST_PROPERTY);
Andrew Geissler2d7b69e2021-05-06 13:12:45 -050098
99 auto response = bus.call(method);
Patrick Williams9a286db2024-01-17 06:29:47 -0600100 std::variant<HostFirmware::FirmwareCondition> currentFwCondV;
NodeMan9780db4752022-05-04 09:06:40 -0500101 response.read(currentFwCondV);
102 auto currentFwCond =
Patrick Williams9a286db2024-01-17 06:29:47 -0600103 std::get<HostFirmware::FirmwareCondition>(currentFwCondV);
Andrew Geissler2d7b69e2021-05-06 13:12:45 -0500104
NodeMan97ef828c42022-05-31 11:24:32 -0500105 info(
106 "Read host fw condition {COND_VALUE} from {COND_SERVICE}, {COND_PATH}",
107 "COND_VALUE", currentFwCond, "COND_SERVICE", service,
108 "COND_PATH", path);
109
Patrick Williams9a286db2024-01-17 06:29:47 -0600110 if (currentFwCond == HostFirmware::FirmwareCondition::Running)
Andrew Geissler2d7b69e2021-05-06 13:12:45 -0500111 {
112 return true;
113 }
114 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500115 catch (const sdbusplus::exception_t& e)
Andrew Geissler2d7b69e2021-05-06 13:12:45 -0500116 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500117 error("Error reading HostFirmware condition, error: {ERROR}, "
118 "service: {SERVICE} path: {PATH}",
119 "ERROR", e, "SERVICE", service, "PATH", path);
Andrew Geissler2d7b69e2021-05-06 13:12:45 -0500120 throw;
121 }
122 }
123 }
124 return false;
Andrew Geisslereeaccf82017-03-28 15:23:27 -0500125}
126
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400127// Helper function to check if chassis power is on
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500128bool isChassiPowerOn(sdbusplus::bus_t& bus, size_t id)
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400129{
Potin Lai07e73aa2022-03-29 15:16:55 +0800130 auto svcname = std::string{CHASSIS_STATE_SVC} + std::to_string(id);
Patrick Williams9a286db2024-01-17 06:29:47 -0600131 auto objpath = std::string{Chassis::namespace_path::value} + "/" +
132 std::string{Chassis::namespace_path::chassis} +
133 std::to_string(id);
Potin Lai70f36d82022-03-15 10:25:39 +0800134
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400135 try
136 {
Potin Lai70f36d82022-03-15 10:25:39 +0800137 auto method = bus.new_method_call(svcname.c_str(), objpath.c_str(),
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400138 PROPERTY_INTERFACE, "Get");
Patrick Williams9a286db2024-01-17 06:29:47 -0600139 method.append(Chassis::interface, CHASSIS_STATE_POWER_PROP);
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400140
141 auto response = bus.call(method);
Patrick Williams9a286db2024-01-17 06:29:47 -0600142 std::variant<Chassis::PowerState> currentPowerStateV;
NodeMan9780db4752022-05-04 09:06:40 -0500143 response.read(currentPowerStateV);
NodeMan97ef828c42022-05-31 11:24:32 -0500144
Patrick Williams9a286db2024-01-17 06:29:47 -0600145 auto currentPowerState =
146 std::get<Chassis::PowerState>(currentPowerStateV);
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400147
Patrick Williams9a286db2024-01-17 06:29:47 -0600148 if (currentPowerState == Chassis::PowerState::On)
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400149 {
150 return true;
151 }
152 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500153 catch (const sdbusplus::exception_t& e)
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400154 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500155 error("Error reading Chassis Power State, error: {ERROR}, "
156 "service: {SERVICE} path: {PATH}",
Potin Lai70f36d82022-03-15 10:25:39 +0800157 "ERROR", e, "SERVICE", svcname.c_str(), "PATH", objpath.c_str());
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400158 throw;
159 }
160 return false;
161}
162
Potin Lai70f36d82022-03-15 10:25:39 +0800163bool isHostRunning(size_t id)
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500164{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500165 info("Check if host is running");
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500166
Andrew Geissler0971dea2017-03-28 14:32:40 -0500167 auto bus = sdbusplus::bus::new_default();
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500168
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400169 // No need to check if chassis power is not on
Potin Lai70f36d82022-03-15 10:25:39 +0800170 if (!isChassiPowerOn(bus, id))
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400171 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500172 info("Chassis power not on, exit");
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400173 return false;
174 }
175
Andrew Geisslercd0ebe82021-05-14 21:39:51 -0500176 // This applications systemd service is setup to only run after all other
177 // application that could possibly implement the needed interface have
178 // been started. However, the use of mapper to find those interfaces means
179 // we have a condition where the interface may be on D-Bus but not stored
NodeMan97ef828c42022-05-31 11:24:32 -0500180 // within mapper yet. There are five built in retries to check if it's
181 // found the host is not up. This service is only called if chassis power
182 // is on when the BMC comes up, so this wont impact most normal cases
183 // where the BMC is rebooted with chassis power off. In cases where
184 // chassis power is on, the host is likely running so we want to be sure
185 // we check all interfaces
186 for (int i = 0; i < 5; i++)
Andrew Geissler0971dea2017-03-28 14:32:40 -0500187 {
NodeMan97ef828c42022-05-31 11:24:32 -0500188 debug(
189 "Introspecting new bus objects for bus id: {ID} sleeping for 1 second.",
190 "ID", id);
Andrew Geisslercd0ebe82021-05-14 21:39:51 -0500191 // Give mapper a small window to introspect new objects on bus
NodeMan97ef828c42022-05-31 11:24:32 -0500192 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
Andrew Geisslercd0ebe82021-05-14 21:39:51 -0500193 if (checkFirmwareConditionRunning(bus))
194 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500195 info("Host is running!");
Andrew Geisslercd0ebe82021-05-14 21:39:51 -0500196 // Create file for host instance and create in filesystem to
197 // indicate to services that host is running
198 auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0);
199 size++; // null
200 std::unique_ptr<char[]> buf(new char[size]);
201 std::snprintf(buf.get(), size, HOST_RUNNING_FILE, 0);
202 std::ofstream outfile(buf.get());
203 outfile.close();
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400204 return true;
Andrew Geisslercd0ebe82021-05-14 21:39:51 -0500205 }
Andrew Geissler07d0ed52017-03-28 15:44:18 -0500206 }
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500207 info("Host is not running!");
Andrew Geissler0d1c3f12021-07-27 16:21:01 -0400208 return false;
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500209}
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500210
211} // namespace manager
212} // namespace state
213} // namespace phosphor