blob: 5314a569cdc84dc64d4e7e944e4f6e65fd15afda [file] [log] [blame]
Andrew Geissler36529022016-11-29 15:23:54 -06001#pragma once
2
Andrew Geisslere426b582020-05-28 12:40:55 -05003#include "config.h"
4
5#include "settings.hpp"
Andrew Geissler928bbf12023-02-14 13:30:14 -06006#include "utils.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05007
Andrew Geissler033fc3b2017-08-30 15:11:44 -05008#include <cereal/access.hpp>
Vishwanatha Subbanna0a838732017-10-05 12:43:19 +05309#include <cereal/cereal.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050010#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050011#include <sdbusplus/bus.hpp>
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050012#include <xyz/openbmc_project/Control/Boot/RebootAttempts/server.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050013#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
Patrick Williams9a286db2024-01-17 06:29:47 -060014#include <xyz/openbmc_project/State/Host/server.hpp>
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050015#include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050016
Patrick Williams6ed41ea2022-04-05 15:50:21 -050017#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050018#include <string>
Andrew Geissler36529022016-11-29 15:23:54 -060019
20namespace phosphor
21{
22namespace state
23{
24namespace manager
25{
26
Patrick Williamsf053e6f2022-07-22 19:26:54 -050027using HostInherit = sdbusplus::server::object_t<
Patrick Williams7e969cb2023-08-23 16:24:23 -050028 sdbusplus::server::xyz::openbmc_project::state::Host,
29 sdbusplus::server::xyz::openbmc_project::state::boot::Progress,
30 sdbusplus::server::xyz::openbmc_project::control::boot::RebootAttempts,
31 sdbusplus::server::xyz::openbmc_project::state::operating_system::Status>;
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050032
Andrew Geissler8ffdb262021-09-20 15:25:19 -050033PHOSPHOR_LOG2_USING;
Andrew Geissler7b90a622017-08-08 11:41:08 -050034
Patrick Williamsd22706f2017-05-04 05:42:49 -050035namespace sdbusRule = sdbusplus::bus::match::rules;
Patrick Williams6ed41ea2022-04-05 15:50:21 -050036namespace fs = std::filesystem;
Patrick Williamsd22706f2017-05-04 05:42:49 -050037
Andrew Geissler36529022016-11-29 15:23:54 -060038/** @class Host
39 * @brief OpenBMC host state management implementation.
40 * @details A concrete implementation for xyz.openbmc_project.State.Host
41 * DBus API.
42 */
Patrick Williamsd22706f2017-05-04 05:42:49 -050043class Host : public HostInherit
Andrew Geissler36529022016-11-29 15:23:54 -060044{
Andrew Geissler58a18012018-01-19 19:36:05 -080045 public:
46 /** @brief Constructs Host State Manager
47 *
48 * @note This constructor passes 'true' to the base class in order to
49 * defer dbus object registration until we can run
50 * determineInitialState() and set our properties
51 *
52 * @param[in] bus - The Dbus bus object
Andrew Geissler58a18012018-01-19 19:36:05 -080053 * @param[in] objPath - The Dbus object path
Allen.Wang79b45002022-02-10 17:59:20 +080054 * @param[in] id - The Host id
Andrew Geissler58a18012018-01-19 19:36:05 -080055 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -050056 Host(sdbusplus::bus_t& bus, const char* objPath, size_t id) :
Patrick Williams76070742022-04-05 15:20:12 -050057 HostInherit(bus, objPath, HostInherit::action::defer_emit), bus(bus),
Andrew Geisslere4039a82020-02-11 15:51:59 -060058 systemdSignalJobRemoved(
Andrew Geissler58a18012018-01-19 19:36:05 -080059 bus,
60 sdbusRule::type::signal() + sdbusRule::member("JobRemoved") +
61 sdbusRule::path("/org/freedesktop/systemd1") +
62 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080063 [this](sdbusplus::message_t& m) { sysStateChangeJobRemoved(m); }),
Andrew Geissler47b96122020-02-11 16:13:13 -060064 systemdSignalJobNew(
65 bus,
66 sdbusRule::type::signal() + sdbusRule::member("JobNew") +
67 sdbusRule::path("/org/freedesktop/systemd1") +
68 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080069 [this](sdbusplus::message_t& m) { sysStateChangeJobNew(m); }),
Potin Laic328a4c2022-03-18 23:49:37 +080070 settings(bus, id), id(id)
Andrew Geissler58a18012018-01-19 19:36:05 -080071 {
72 // Enable systemd signals
Andrew Geissler928bbf12023-02-14 13:30:14 -060073 utils::subscribeToSystemdSignals(bus);
Andrew Geissler4da7e002017-01-24 15:21:40 -060074
Allen.Wang79b45002022-02-10 17:59:20 +080075 // create map of target name base on host id
76 createSystemdTargetMaps();
77
Andrew Geissler58a18012018-01-19 19:36:05 -080078 // Will throw exception on fail
79 determineInitialState();
Andrew Geissleref3c1842016-12-01 12:33:09 -060080
Andrew Geissler54ce6392024-02-23 10:09:09 -060081 // Setup supported transitions against this host object
82 setupSupportedTransitions();
83
NodeMan97b4cbfac2022-05-09 23:56:39 -050084 // Sets auto-reboot attempts to max-allowed
Patrick Williams7e969cb2023-08-23 16:24:23 -050085 attemptsLeft(sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -050086 RebootAttempts::retryAttempts());
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050087
Andrew Geissler58a18012018-01-19 19:36:05 -080088 // We deferred this until we could get our property correct
89 this->emit_object_added();
90 }
Andrew Geissleref3c1842016-12-01 12:33:09 -060091
Andrew Geissler58a18012018-01-19 19:36:05 -080092 /** @brief Set value of HostTransition */
93 Transition requestedHostTransition(Transition value) override;
Andrew Geissleref3c1842016-12-01 12:33:09 -060094
Andrew Geissler58a18012018-01-19 19:36:05 -080095 /** @brief Set Value for boot progress */
96 ProgressStages bootProgress(ProgressStages value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -050097
Andrew Geissler4ccaeaa2025-10-14 09:08:29 -050098 /** @brief Updated whenever BootProgress is updated */
99 uint64_t bootProgressLastUpdate(uint64_t value) override;
100
Andrew Geissler58a18012018-01-19 19:36:05 -0800101 /** @brief Set Value for Operating System Status */
102 OSStatus operatingSystemState(OSStatus value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -0500103
Andrew Geissler58a18012018-01-19 19:36:05 -0800104 /** @brief Set value of CurrentHostState */
105 HostState currentHostState(HostState value) override;
Andrew Geissler36529022016-11-29 15:23:54 -0600106
Andrew Geissler58a18012018-01-19 19:36:05 -0800107 /**
NodeMan97b4cbfac2022-05-09 23:56:39 -0500108 * @brief Set value for allowable auto-reboot count
109 *
110 * This override is responsible for ensuring that when external users
111 * set the number of automatic retry attempts that the number of
112 * automatic reboot attempts left will update accordingly.
113 *
114 * @param[in] value - desired Reboot count value
115 *
116 * @return number of reboot attempts allowed.
117 */
118 uint32_t retryAttempts(uint32_t value) override
119 {
Patrick Williams7e969cb2023-08-23 16:24:23 -0500120 if (sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500121 RebootAttempts::attemptsLeft() != value)
122 {
123 info("Automatic reboot retry attempts set to: {VALUE} ", "VALUE",
124 value);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500125 sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500126 RebootAttempts::attemptsLeft(value);
127 }
128
Patrick Williams7e969cb2023-08-23 16:24:23 -0500129 return (sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500130 RebootAttempts::retryAttempts(value));
131 }
132
133 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800134 * @brief Set host reboot count to default
135 *
136 * OpenBMC software controls the number of allowed reboot attempts so
137 * any external set request of this property will be overridden by
NodeMan97b4cbfac2022-05-09 23:56:39 -0500138 * this function and set to the number of the allowed auto-reboot
139 * retry attempts found on the system.
Andrew Geissler58a18012018-01-19 19:36:05 -0800140 *
141 * The only code responsible for decrementing the boot count resides
142 * within this process and that will use the sub class interface
143 * directly
144 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500145 * @param[in] value - Reboot count value
Andrew Geissler58a18012018-01-19 19:36:05 -0800146 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500147 * @return number of reboot attempts left(allowed by retry attempts
148 * property)
Andrew Geissler58a18012018-01-19 19:36:05 -0800149 */
150 uint32_t attemptsLeft(uint32_t value) override
151 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500152 debug("External request to reset reboot count");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500153 auto retryAttempts = sdbusplus::xyz::openbmc_project::Control::Boot::
154 server::RebootAttempts::retryAttempts();
155 return (
Patrick Williams7e969cb2023-08-23 16:24:23 -0500156 sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500157 RebootAttempts::attemptsLeft(std::min(value, retryAttempts)));
Andrew Geissler58a18012018-01-19 19:36:05 -0800158 }
Andrew Geissler7b90a622017-08-08 11:41:08 -0500159
Andrew Geissler58a18012018-01-19 19:36:05 -0800160 private:
161 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800162 * @brief Determine initial host state and set internally
163 *
164 * @return Will throw exceptions on failure
165 **/
166 void determineInitialState();
Andrew Geissler4da7e002017-01-24 15:21:40 -0600167
Allen.Wang79b45002022-02-10 17:59:20 +0800168 /**
Andrew Geissler54ce6392024-02-23 10:09:09 -0600169 * @brief Configure supported transitions for system
170 *
171 * @return Will throw exceptions on failure
172 **/
173 void setupSupportedTransitions();
174
175 /**
Allen.Wang79b45002022-02-10 17:59:20 +0800176 * create systemd target instance names and mapping table
177 **/
178 void createSystemdTargetMaps();
179
Andrew Geissler58a18012018-01-19 19:36:05 -0800180 /** @brief Execute the transition request
181 *
182 * This function assumes the state has been validated and the host
183 * is in an appropriate state for the transition to be started.
184 *
185 * @param[in] tranReq - Transition requested
186 */
187 void executeTransition(Transition tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600188
Andrew Geissler58a18012018-01-19 19:36:05 -0800189 /**
190 * @brief Determine if target is active
191 *
192 * This function determines if the target is active and
193 * helps prevent misleading log recorded states.
194 *
195 * @param[in] target - Target string to check on
196 *
197 * @return boolean corresponding to state active
198 **/
199 bool stateActive(const std::string& target);
Josh D. King929ef702017-03-02 10:58:11 -0600200
Andrew Geissler58a18012018-01-19 19:36:05 -0800201 /**
202 * @brief Determine if auto reboot flag is set
203 *
204 * @return boolean corresponding to current auto_reboot setting
205 **/
206 bool isAutoReboot();
Michael Tritz206a8332017-02-06 16:01:23 -0600207
Andrew Geissler58a18012018-01-19 19:36:05 -0800208 /** @brief Check if systemd state change is relevant to this object
209 *
210 * Instance specific interface to handle the detected systemd state
211 * change
212 *
213 * @param[in] msg - Data associated with subscribed signal
214 *
215 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500216 void sysStateChangeJobRemoved(sdbusplus::message_t& msg);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600217
Andrew Geissler47b96122020-02-11 16:13:13 -0600218 /** @brief Check if JobNew systemd signal is relevant to this object
219 *
220 * In certain instances phosphor-state-manager needs to monitor for the
221 * entry into a systemd target. This function will be used for these cases.
222 *
223 * Instance specific interface to handle the detected systemd state
224 * change
225 *
226 * @param[in] msg - Data associated with subscribed signal
227 *
228 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500229 void sysStateChangeJobNew(sdbusplus::message_t& msg);
Andrew Geissler47b96122020-02-11 16:13:13 -0600230
Andrew Geissler58a18012018-01-19 19:36:05 -0800231 /** @brief Decrement reboot count
232 *
233 * This is used internally to this application to decrement the boot
234 * count on each boot attempt. The host will use the external
235 * attemptsLeft() interface to reset the count when a boot is successful
236 *
237 * @return number of reboot count attempts left
238 */
239 uint32_t decrementRebootCount();
Andrew Geissler7b90a622017-08-08 11:41:08 -0500240
Andrew Geissler58a18012018-01-19 19:36:05 -0800241 // Allow cereal class access to allow these next two function to be
242 // private
243 friend class cereal::access;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500244
Andrew Geissler58a18012018-01-19 19:36:05 -0800245 /** @brief Function required by Cereal to perform serialization.
246 *
247 * @tparam Archive - Cereal archive type (binary in our case).
248 * @param[in] archive - reference to Cereal archive.
249 * @param[in] version - Class version that enables handling
250 * a serialized data across code levels
251 */
252 template <class Archive>
253 void save(Archive& archive, const std::uint32_t version) const
254 {
Andrew Geissler769a62f2019-12-06 13:36:08 -0600255 // version is not used currently
256 (void)(version);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500257 archive(sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500258 RebootAttempts::retryAttempts(),
259 convertForMessage(sdbusplus::xyz::openbmc_project::State::
Andrew Geissler58a18012018-01-19 19:36:05 -0800260 server::Host::requestedHostTransition()),
261 convertForMessage(sdbusplus::xyz::openbmc_project::State::Boot::
262 server::Progress::bootProgress()),
263 convertForMessage(
264 sdbusplus::xyz::openbmc_project::State::OperatingSystem::
265 server::Status::operatingSystemState()));
266 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500267
Andrew Geissler58a18012018-01-19 19:36:05 -0800268 /** @brief Function required by Cereal to perform deserialization.
269 *
270 * @tparam Archive - Cereal archive type (binary in our case).
271 * @param[in] archive - reference to Cereal archive.
272 * @param[in] version - Class version that enables handling
273 * a serialized data across code levels
274 */
275 template <class Archive>
276 void load(Archive& archive, const std::uint32_t version)
277 {
278 std::string reqTranState;
279 std::string bootProgress;
280 std::string osState;
NodeMan97b4cbfac2022-05-09 23:56:39 -0500281 // Older cereal archive without RetryAttempt may be implemented
282 // just set to (BOOT_COUNT_MAX_ALLOWED)
283 uint32_t retryAttempts = BOOT_COUNT_MAX_ALLOWED;
284 switch (version)
285 {
286 case 2:
287 archive(retryAttempts);
288 [[fallthrough]];
289 case 1:
290 archive(reqTranState, bootProgress, osState);
291 break;
292 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800293 auto reqTran = Host::convertTransitionFromString(reqTranState);
294 // When restoring, set the requested state with persistent value
295 // but don't call the override which would execute it
Patrick Williams7e969cb2023-08-23 16:24:23 -0500296 sdbusplus::server::xyz::openbmc_project::state::Host::
Adi Fogeleaaaff52025-09-29 12:02:30 +0300297 requestedHostTransition(reqTran, true);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500298 sdbusplus::server::xyz::openbmc_project::state::boot::Progress::
Adi Fogeleaaaff52025-09-29 12:02:30 +0300299 bootProgress(Host::convertProgressStagesFromString(bootProgress),
300 true);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500301 sdbusplus::server::xyz::openbmc_project::state::operating_system::
Andrew Geissler58a18012018-01-19 19:36:05 -0800302 Status::operatingSystemState(
Adi Fogeleaaaff52025-09-29 12:02:30 +0300303 Host::convertOSStatusFromString(osState), true);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500304 sdbusplus::server::xyz::openbmc_project::control::boot::RebootAttempts::
Adi Fogeleaaaff52025-09-29 12:02:30 +0300305 retryAttempts(retryAttempts, true);
Andrew Geissler58a18012018-01-19 19:36:05 -0800306 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500307
Andrew Geissler58a18012018-01-19 19:36:05 -0800308 /** @brief Serialize and persist requested host state
309 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800310 * @return fs::path - pathname of persisted requested host state.
311 */
Allen.Wangba182f02022-03-23 19:01:53 +0800312 fs::path serialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500313
Manojkiran Eda3ff5a362024-06-17 10:59:07 +0530314 /** @brief Deserialize a persisted requested host state.
Andrew Geissler58a18012018-01-19 19:36:05 -0800315 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800316 * @return bool - true if the deserialization was successful, false
317 * otherwise.
318 */
Allen.Wangba182f02022-03-23 19:01:53 +0800319 bool deserialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500320
Allen.Wang79b45002022-02-10 17:59:20 +0800321 /**
322 * @brief Get target name of a HostState
323 *
324 * @param[in] state - The state of the host
325 *
326 * @return string - systemd target name of the state
327 */
328 const std::string& getTarget(HostState state);
329
330 /**
331 * @brief Get target name of a TransitionRequest
332 *
333 * @param[in] tranReq - Transition requested
334 *
335 * @return string - systemd target name of Requested transition
336 */
337 const std::string& getTarget(Transition tranReq);
338
Andrew Geissler58a18012018-01-19 19:36:05 -0800339 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500340 sdbusplus::bus_t& bus;
Andrew Geissleref621162016-12-08 12:56:21 -0600341
Andrew Geisslere4039a82020-02-11 15:51:59 -0600342 /** @brief Used to subscribe to dbus systemd JobRemoved signal **/
343 sdbusplus::bus::match_t systemdSignalJobRemoved;
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500344
Andrew Geissler47b96122020-02-11 16:13:13 -0600345 /** @brief Used to subscribe to dbus systemd JobNew signal **/
346 sdbusplus::bus::match_t systemdSignalJobNew;
347
Potin Laic328a4c2022-03-18 23:49:37 +0800348 // Settings host objects of interest
349 settings::HostObjects settings;
Allen.Wang79b45002022-02-10 17:59:20 +0800350
351 /** @brief Host id. **/
352 const size_t id = 0;
353
354 /** @brief HostState to systemd target mapping table. **/
355 std::map<HostState, std::string> stateTargetTable;
356
357 /** @brief Requested Transition to systemd target mapping table. **/
358 std::map<Transition, std::string> transitionTargetTable;
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400359
360 /** @brief Target called when a host crash occurs **/
361 std::string hostCrashTarget;
Andrew Geissler36529022016-11-29 15:23:54 -0600362};
363
364} // namespace manager
365} // namespace state
366} // namespace phosphor