blob: 043aef65b7f8485a6a1dfabb2c0f288912f00333 [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 Geissler58a18012018-01-19 19:36:05 -080098 /** @brief Set Value for Operating System Status */
99 OSStatus operatingSystemState(OSStatus value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -0500100
Andrew Geissler58a18012018-01-19 19:36:05 -0800101 /** @brief Set value of CurrentHostState */
102 HostState currentHostState(HostState value) override;
Andrew Geissler36529022016-11-29 15:23:54 -0600103
Andrew Geissler58a18012018-01-19 19:36:05 -0800104 /**
NodeMan97b4cbfac2022-05-09 23:56:39 -0500105 * @brief Set value for allowable auto-reboot count
106 *
107 * This override is responsible for ensuring that when external users
108 * set the number of automatic retry attempts that the number of
109 * automatic reboot attempts left will update accordingly.
110 *
111 * @param[in] value - desired Reboot count value
112 *
113 * @return number of reboot attempts allowed.
114 */
115 uint32_t retryAttempts(uint32_t value) override
116 {
Patrick Williams7e969cb2023-08-23 16:24:23 -0500117 if (sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500118 RebootAttempts::attemptsLeft() != value)
119 {
120 info("Automatic reboot retry attempts set to: {VALUE} ", "VALUE",
121 value);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500122 sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500123 RebootAttempts::attemptsLeft(value);
124 }
125
Patrick Williams7e969cb2023-08-23 16:24:23 -0500126 return (sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500127 RebootAttempts::retryAttempts(value));
128 }
129
130 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800131 * @brief Set host reboot count to default
132 *
133 * OpenBMC software controls the number of allowed reboot attempts so
134 * any external set request of this property will be overridden by
NodeMan97b4cbfac2022-05-09 23:56:39 -0500135 * this function and set to the number of the allowed auto-reboot
136 * retry attempts found on the system.
Andrew Geissler58a18012018-01-19 19:36:05 -0800137 *
138 * The only code responsible for decrementing the boot count resides
139 * within this process and that will use the sub class interface
140 * directly
141 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500142 * @param[in] value - Reboot count value
Andrew Geissler58a18012018-01-19 19:36:05 -0800143 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500144 * @return number of reboot attempts left(allowed by retry attempts
145 * property)
Andrew Geissler58a18012018-01-19 19:36:05 -0800146 */
147 uint32_t attemptsLeft(uint32_t value) override
148 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500149 debug("External request to reset reboot count");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500150 auto retryAttempts = sdbusplus::xyz::openbmc_project::Control::Boot::
151 server::RebootAttempts::retryAttempts();
152 return (
Patrick Williams7e969cb2023-08-23 16:24:23 -0500153 sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500154 RebootAttempts::attemptsLeft(std::min(value, retryAttempts)));
Andrew Geissler58a18012018-01-19 19:36:05 -0800155 }
Andrew Geissler7b90a622017-08-08 11:41:08 -0500156
Andrew Geissler58a18012018-01-19 19:36:05 -0800157 private:
158 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800159 * @brief Determine initial host state and set internally
160 *
161 * @return Will throw exceptions on failure
162 **/
163 void determineInitialState();
Andrew Geissler4da7e002017-01-24 15:21:40 -0600164
Allen.Wang79b45002022-02-10 17:59:20 +0800165 /**
Andrew Geissler54ce6392024-02-23 10:09:09 -0600166 * @brief Configure supported transitions for system
167 *
168 * @return Will throw exceptions on failure
169 **/
170 void setupSupportedTransitions();
171
172 /**
Allen.Wang79b45002022-02-10 17:59:20 +0800173 * create systemd target instance names and mapping table
174 **/
175 void createSystemdTargetMaps();
176
Andrew Geissler58a18012018-01-19 19:36:05 -0800177 /** @brief Execute the transition request
178 *
179 * This function assumes the state has been validated and the host
180 * is in an appropriate state for the transition to be started.
181 *
182 * @param[in] tranReq - Transition requested
183 */
184 void executeTransition(Transition tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600185
Andrew Geissler58a18012018-01-19 19:36:05 -0800186 /**
187 * @brief Determine if target is active
188 *
189 * This function determines if the target is active and
190 * helps prevent misleading log recorded states.
191 *
192 * @param[in] target - Target string to check on
193 *
194 * @return boolean corresponding to state active
195 **/
196 bool stateActive(const std::string& target);
Josh D. King929ef702017-03-02 10:58:11 -0600197
Andrew Geissler58a18012018-01-19 19:36:05 -0800198 /**
199 * @brief Determine if auto reboot flag is set
200 *
201 * @return boolean corresponding to current auto_reboot setting
202 **/
203 bool isAutoReboot();
Michael Tritz206a8332017-02-06 16:01:23 -0600204
Andrew Geissler58a18012018-01-19 19:36:05 -0800205 /** @brief Check if systemd state change is relevant to this object
206 *
207 * Instance specific interface to handle the detected systemd state
208 * change
209 *
210 * @param[in] msg - Data associated with subscribed signal
211 *
212 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500213 void sysStateChangeJobRemoved(sdbusplus::message_t& msg);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600214
Andrew Geissler47b96122020-02-11 16:13:13 -0600215 /** @brief Check if JobNew systemd signal is relevant to this object
216 *
217 * In certain instances phosphor-state-manager needs to monitor for the
218 * entry into a systemd target. This function will be used for these cases.
219 *
220 * Instance specific interface to handle the detected systemd state
221 * change
222 *
223 * @param[in] msg - Data associated with subscribed signal
224 *
225 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500226 void sysStateChangeJobNew(sdbusplus::message_t& msg);
Andrew Geissler47b96122020-02-11 16:13:13 -0600227
Andrew Geissler58a18012018-01-19 19:36:05 -0800228 /** @brief Decrement reboot count
229 *
230 * This is used internally to this application to decrement the boot
231 * count on each boot attempt. The host will use the external
232 * attemptsLeft() interface to reset the count when a boot is successful
233 *
234 * @return number of reboot count attempts left
235 */
236 uint32_t decrementRebootCount();
Andrew Geissler7b90a622017-08-08 11:41:08 -0500237
Andrew Geissler58a18012018-01-19 19:36:05 -0800238 // Allow cereal class access to allow these next two function to be
239 // private
240 friend class cereal::access;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500241
Andrew Geissler58a18012018-01-19 19:36:05 -0800242 /** @brief Function required by Cereal to perform serialization.
243 *
244 * @tparam Archive - Cereal archive type (binary in our case).
245 * @param[in] archive - reference to Cereal archive.
246 * @param[in] version - Class version that enables handling
247 * a serialized data across code levels
248 */
249 template <class Archive>
250 void save(Archive& archive, const std::uint32_t version) const
251 {
Andrew Geissler769a62f2019-12-06 13:36:08 -0600252 // version is not used currently
253 (void)(version);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500254 archive(sdbusplus::server::xyz::openbmc_project::control::boot::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500255 RebootAttempts::retryAttempts(),
256 convertForMessage(sdbusplus::xyz::openbmc_project::State::
Andrew Geissler58a18012018-01-19 19:36:05 -0800257 server::Host::requestedHostTransition()),
258 convertForMessage(sdbusplus::xyz::openbmc_project::State::Boot::
259 server::Progress::bootProgress()),
260 convertForMessage(
261 sdbusplus::xyz::openbmc_project::State::OperatingSystem::
262 server::Status::operatingSystemState()));
263 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500264
Andrew Geissler58a18012018-01-19 19:36:05 -0800265 /** @brief Function required by Cereal to perform deserialization.
266 *
267 * @tparam Archive - Cereal archive type (binary in our case).
268 * @param[in] archive - reference to Cereal archive.
269 * @param[in] version - Class version that enables handling
270 * a serialized data across code levels
271 */
272 template <class Archive>
273 void load(Archive& archive, const std::uint32_t version)
274 {
275 std::string reqTranState;
276 std::string bootProgress;
277 std::string osState;
NodeMan97b4cbfac2022-05-09 23:56:39 -0500278 // Older cereal archive without RetryAttempt may be implemented
279 // just set to (BOOT_COUNT_MAX_ALLOWED)
280 uint32_t retryAttempts = BOOT_COUNT_MAX_ALLOWED;
281 switch (version)
282 {
283 case 2:
284 archive(retryAttempts);
285 [[fallthrough]];
286 case 1:
287 archive(reqTranState, bootProgress, osState);
288 break;
289 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800290 auto reqTran = Host::convertTransitionFromString(reqTranState);
291 // When restoring, set the requested state with persistent value
292 // but don't call the override which would execute it
Patrick Williams7e969cb2023-08-23 16:24:23 -0500293 sdbusplus::server::xyz::openbmc_project::state::Host::
Andrew Geissler58a18012018-01-19 19:36:05 -0800294 requestedHostTransition(reqTran);
Patrick Williams7e969cb2023-08-23 16:24:23 -0500295 sdbusplus::server::xyz::openbmc_project::state::boot::Progress::
Andrew Geissler58a18012018-01-19 19:36:05 -0800296 bootProgress(Host::convertProgressStagesFromString(bootProgress));
Patrick Williams7e969cb2023-08-23 16:24:23 -0500297 sdbusplus::server::xyz::openbmc_project::state::operating_system::
Andrew Geissler58a18012018-01-19 19:36:05 -0800298 Status::operatingSystemState(
299 Host::convertOSStatusFromString(osState));
Patrick Williams7e969cb2023-08-23 16:24:23 -0500300 sdbusplus::server::xyz::openbmc_project::control::boot::RebootAttempts::
NodeMan97b4cbfac2022-05-09 23:56:39 -0500301 retryAttempts(retryAttempts);
Andrew Geissler58a18012018-01-19 19:36:05 -0800302 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500303
Andrew Geissler58a18012018-01-19 19:36:05 -0800304 /** @brief Serialize and persist requested host state
305 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800306 * @return fs::path - pathname of persisted requested host state.
307 */
Allen.Wangba182f02022-03-23 19:01:53 +0800308 fs::path serialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500309
Manojkiran Eda3ff5a362024-06-17 10:59:07 +0530310 /** @brief Deserialize a persisted requested host state.
Andrew Geissler58a18012018-01-19 19:36:05 -0800311 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800312 * @return bool - true if the deserialization was successful, false
313 * otherwise.
314 */
Allen.Wangba182f02022-03-23 19:01:53 +0800315 bool deserialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500316
Allen.Wang79b45002022-02-10 17:59:20 +0800317 /**
318 * @brief Get target name of a HostState
319 *
320 * @param[in] state - The state of the host
321 *
322 * @return string - systemd target name of the state
323 */
324 const std::string& getTarget(HostState state);
325
326 /**
327 * @brief Get target name of a TransitionRequest
328 *
329 * @param[in] tranReq - Transition requested
330 *
331 * @return string - systemd target name of Requested transition
332 */
333 const std::string& getTarget(Transition tranReq);
334
Andrew Geissler58a18012018-01-19 19:36:05 -0800335 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500336 sdbusplus::bus_t& bus;
Andrew Geissleref621162016-12-08 12:56:21 -0600337
Andrew Geisslere4039a82020-02-11 15:51:59 -0600338 /** @brief Used to subscribe to dbus systemd JobRemoved signal **/
339 sdbusplus::bus::match_t systemdSignalJobRemoved;
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500340
Andrew Geissler47b96122020-02-11 16:13:13 -0600341 /** @brief Used to subscribe to dbus systemd JobNew signal **/
342 sdbusplus::bus::match_t systemdSignalJobNew;
343
Potin Laic328a4c2022-03-18 23:49:37 +0800344 // Settings host objects of interest
345 settings::HostObjects settings;
Allen.Wang79b45002022-02-10 17:59:20 +0800346
347 /** @brief Host id. **/
348 const size_t id = 0;
349
350 /** @brief HostState to systemd target mapping table. **/
351 std::map<HostState, std::string> stateTargetTable;
352
353 /** @brief Requested Transition to systemd target mapping table. **/
354 std::map<Transition, std::string> transitionTargetTable;
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400355
356 /** @brief Target called when a host crash occurs **/
357 std::string hostCrashTarget;
Andrew Geissler36529022016-11-29 15:23:54 -0600358};
359
360} // namespace manager
361} // namespace state
362} // namespace phosphor