blob: fc80da8df9ce9359c0fd793323ac1caf831b70c8 [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#include "xyz/openbmc_project/State/Host/server.hpp"
8
Andrew Geissler033fc3b2017-08-30 15:11:44 -05009#include <cereal/access.hpp>
Vishwanatha Subbanna0a838732017-10-05 12:43:19 +053010#include <cereal/cereal.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050011#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050012#include <sdbusplus/bus.hpp>
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050013#include <xyz/openbmc_project/Control/Boot/RebootAttempts/server.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050014#include <xyz/openbmc_project/State/Boot/Progress/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<
Andrew Geissler58a18012018-01-19 19:36:05 -080028 sdbusplus::xyz::openbmc_project::State::server::Host,
29 sdbusplus::xyz::openbmc_project::State::Boot::server::Progress,
30 sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts,
31 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::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
NodeMan97b4cbfac2022-05-09 23:56:39 -050081 // Sets auto-reboot attempts to max-allowed
82 attemptsLeft(sdbusplus::xyz::openbmc_project::Control::Boot::server::
83 RebootAttempts::retryAttempts());
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050084
Andrew Geissler58a18012018-01-19 19:36:05 -080085 // We deferred this until we could get our property correct
86 this->emit_object_added();
87 }
Andrew Geissleref3c1842016-12-01 12:33:09 -060088
Andrew Geissler58a18012018-01-19 19:36:05 -080089 /** @brief Set value of HostTransition */
90 Transition requestedHostTransition(Transition value) override;
Andrew Geissleref3c1842016-12-01 12:33:09 -060091
Andrew Geissler58a18012018-01-19 19:36:05 -080092 /** @brief Set Value for boot progress */
93 ProgressStages bootProgress(ProgressStages value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -050094
Andrew Geissler58a18012018-01-19 19:36:05 -080095 /** @brief Set Value for Operating System Status */
96 OSStatus operatingSystemState(OSStatus value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -050097
Andrew Geissler58a18012018-01-19 19:36:05 -080098 /** @brief Set value of CurrentHostState */
99 HostState currentHostState(HostState value) override;
Andrew Geissler36529022016-11-29 15:23:54 -0600100
Andrew Geissler58a18012018-01-19 19:36:05 -0800101 /**
NodeMan97b4cbfac2022-05-09 23:56:39 -0500102 * @brief Set value for allowable auto-reboot count
103 *
104 * This override is responsible for ensuring that when external users
105 * set the number of automatic retry attempts that the number of
106 * automatic reboot attempts left will update accordingly.
107 *
108 * @param[in] value - desired Reboot count value
109 *
110 * @return number of reboot attempts allowed.
111 */
112 uint32_t retryAttempts(uint32_t value) override
113 {
114 if (sdbusplus::xyz::openbmc_project::Control::Boot::server::
115 RebootAttempts::attemptsLeft() != value)
116 {
117 info("Automatic reboot retry attempts set to: {VALUE} ", "VALUE",
118 value);
119 sdbusplus::xyz::openbmc_project::Control::Boot::server::
120 RebootAttempts::attemptsLeft(value);
121 }
122
123 return (sdbusplus::xyz::openbmc_project::Control::Boot::server::
124 RebootAttempts::retryAttempts(value));
125 }
126
127 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800128 * @brief Set host reboot count to default
129 *
130 * OpenBMC software controls the number of allowed reboot attempts so
131 * any external set request of this property will be overridden by
NodeMan97b4cbfac2022-05-09 23:56:39 -0500132 * this function and set to the number of the allowed auto-reboot
133 * retry attempts found on the system.
Andrew Geissler58a18012018-01-19 19:36:05 -0800134 *
135 * The only code responsible for decrementing the boot count resides
136 * within this process and that will use the sub class interface
137 * directly
138 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500139 * @param[in] value - Reboot count value
Andrew Geissler58a18012018-01-19 19:36:05 -0800140 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500141 * @return number of reboot attempts left(allowed by retry attempts
142 * property)
Andrew Geissler58a18012018-01-19 19:36:05 -0800143 */
144 uint32_t attemptsLeft(uint32_t value) override
145 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500146 debug("External request to reset reboot count");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500147 auto retryAttempts = sdbusplus::xyz::openbmc_project::Control::Boot::
148 server::RebootAttempts::retryAttempts();
149 return (
150 sdbusplus::xyz::openbmc_project::Control::Boot::server::
151 RebootAttempts::attemptsLeft(std::min(value, retryAttempts)));
Andrew Geissler58a18012018-01-19 19:36:05 -0800152 }
Andrew Geissler7b90a622017-08-08 11:41:08 -0500153
Andrew Geissler58a18012018-01-19 19:36:05 -0800154 private:
155 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800156 * @brief Determine initial host state and set internally
157 *
158 * @return Will throw exceptions on failure
159 **/
160 void determineInitialState();
Andrew Geissler4da7e002017-01-24 15:21:40 -0600161
Allen.Wang79b45002022-02-10 17:59:20 +0800162 /**
163 * create systemd target instance names and mapping table
164 **/
165 void createSystemdTargetMaps();
166
Andrew Geissler58a18012018-01-19 19:36:05 -0800167 /** @brief Execute the transition request
168 *
169 * This function assumes the state has been validated and the host
170 * is in an appropriate state for the transition to be started.
171 *
172 * @param[in] tranReq - Transition requested
173 */
174 void executeTransition(Transition tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600175
Andrew Geissler58a18012018-01-19 19:36:05 -0800176 /**
177 * @brief Determine if target is active
178 *
179 * This function determines if the target is active and
180 * helps prevent misleading log recorded states.
181 *
182 * @param[in] target - Target string to check on
183 *
184 * @return boolean corresponding to state active
185 **/
186 bool stateActive(const std::string& target);
Josh D. King929ef702017-03-02 10:58:11 -0600187
Andrew Geissler58a18012018-01-19 19:36:05 -0800188 /**
189 * @brief Determine if auto reboot flag is set
190 *
191 * @return boolean corresponding to current auto_reboot setting
192 **/
193 bool isAutoReboot();
Michael Tritz206a8332017-02-06 16:01:23 -0600194
Andrew Geissler58a18012018-01-19 19:36:05 -0800195 /** @brief Check if systemd state change is relevant to this object
196 *
197 * Instance specific interface to handle the detected systemd state
198 * change
199 *
200 * @param[in] msg - Data associated with subscribed signal
201 *
202 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500203 void sysStateChangeJobRemoved(sdbusplus::message_t& msg);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600204
Andrew Geissler47b96122020-02-11 16:13:13 -0600205 /** @brief Check if JobNew systemd signal is relevant to this object
206 *
207 * In certain instances phosphor-state-manager needs to monitor for the
208 * entry into a systemd target. This function will be used for these cases.
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 sysStateChangeJobNew(sdbusplus::message_t& msg);
Andrew Geissler47b96122020-02-11 16:13:13 -0600217
Andrew Geissler58a18012018-01-19 19:36:05 -0800218 /** @brief Decrement reboot count
219 *
220 * This is used internally to this application to decrement the boot
221 * count on each boot attempt. The host will use the external
222 * attemptsLeft() interface to reset the count when a boot is successful
223 *
224 * @return number of reboot count attempts left
225 */
226 uint32_t decrementRebootCount();
Andrew Geissler7b90a622017-08-08 11:41:08 -0500227
Andrew Geissler58a18012018-01-19 19:36:05 -0800228 // Allow cereal class access to allow these next two function to be
229 // private
230 friend class cereal::access;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500231
Andrew Geissler58a18012018-01-19 19:36:05 -0800232 /** @brief Function required by Cereal to perform serialization.
233 *
234 * @tparam Archive - Cereal archive type (binary in our case).
235 * @param[in] archive - reference to Cereal archive.
236 * @param[in] version - Class version that enables handling
237 * a serialized data across code levels
238 */
239 template <class Archive>
240 void save(Archive& archive, const std::uint32_t version) const
241 {
Andrew Geissler769a62f2019-12-06 13:36:08 -0600242 // version is not used currently
243 (void)(version);
NodeMan97b4cbfac2022-05-09 23:56:39 -0500244 archive(sdbusplus::xyz::openbmc_project::Control::Boot::server::
245 RebootAttempts::retryAttempts(),
246 convertForMessage(sdbusplus::xyz::openbmc_project::State::
Andrew Geissler58a18012018-01-19 19:36:05 -0800247 server::Host::requestedHostTransition()),
248 convertForMessage(sdbusplus::xyz::openbmc_project::State::Boot::
249 server::Progress::bootProgress()),
250 convertForMessage(
251 sdbusplus::xyz::openbmc_project::State::OperatingSystem::
252 server::Status::operatingSystemState()));
253 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500254
Andrew Geissler58a18012018-01-19 19:36:05 -0800255 /** @brief Function required by Cereal to perform deserialization.
256 *
257 * @tparam Archive - Cereal archive type (binary in our case).
258 * @param[in] archive - reference to Cereal archive.
259 * @param[in] version - Class version that enables handling
260 * a serialized data across code levels
261 */
262 template <class Archive>
263 void load(Archive& archive, const std::uint32_t version)
264 {
265 std::string reqTranState;
266 std::string bootProgress;
267 std::string osState;
NodeMan97b4cbfac2022-05-09 23:56:39 -0500268 // Older cereal archive without RetryAttempt may be implemented
269 // just set to (BOOT_COUNT_MAX_ALLOWED)
270 uint32_t retryAttempts = BOOT_COUNT_MAX_ALLOWED;
271 switch (version)
272 {
273 case 2:
274 archive(retryAttempts);
275 [[fallthrough]];
276 case 1:
277 archive(reqTranState, bootProgress, osState);
278 break;
279 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800280 auto reqTran = Host::convertTransitionFromString(reqTranState);
281 // When restoring, set the requested state with persistent value
282 // but don't call the override which would execute it
283 sdbusplus::xyz::openbmc_project::State::server::Host::
284 requestedHostTransition(reqTran);
285 sdbusplus::xyz::openbmc_project::State::Boot::server::Progress::
286 bootProgress(Host::convertProgressStagesFromString(bootProgress));
287 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::
288 Status::operatingSystemState(
289 Host::convertOSStatusFromString(osState));
NodeMan97b4cbfac2022-05-09 23:56:39 -0500290 sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts::
291 retryAttempts(retryAttempts);
Andrew Geissler58a18012018-01-19 19:36:05 -0800292 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500293
Andrew Geissler58a18012018-01-19 19:36:05 -0800294 /** @brief Serialize and persist requested host state
295 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800296 * @return fs::path - pathname of persisted requested host state.
297 */
Allen.Wangba182f02022-03-23 19:01:53 +0800298 fs::path serialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500299
Andrew Geissler58a18012018-01-19 19:36:05 -0800300 /** @brief Deserialze a persisted requested host state.
301 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800302 * @return bool - true if the deserialization was successful, false
303 * otherwise.
304 */
Allen.Wangba182f02022-03-23 19:01:53 +0800305 bool deserialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500306
Allen.Wang79b45002022-02-10 17:59:20 +0800307 /**
308 * @brief Get target name of a HostState
309 *
310 * @param[in] state - The state of the host
311 *
312 * @return string - systemd target name of the state
313 */
314 const std::string& getTarget(HostState state);
315
316 /**
317 * @brief Get target name of a TransitionRequest
318 *
319 * @param[in] tranReq - Transition requested
320 *
321 * @return string - systemd target name of Requested transition
322 */
323 const std::string& getTarget(Transition tranReq);
324
Andrew Geissler58a18012018-01-19 19:36:05 -0800325 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500326 sdbusplus::bus_t& bus;
Andrew Geissleref621162016-12-08 12:56:21 -0600327
Andrew Geisslere4039a82020-02-11 15:51:59 -0600328 /** @brief Used to subscribe to dbus systemd JobRemoved signal **/
329 sdbusplus::bus::match_t systemdSignalJobRemoved;
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500330
Andrew Geissler47b96122020-02-11 16:13:13 -0600331 /** @brief Used to subscribe to dbus systemd JobNew signal **/
332 sdbusplus::bus::match_t systemdSignalJobNew;
333
Potin Laic328a4c2022-03-18 23:49:37 +0800334 // Settings host objects of interest
335 settings::HostObjects settings;
Allen.Wang79b45002022-02-10 17:59:20 +0800336
337 /** @brief Host id. **/
338 const size_t id = 0;
339
340 /** @brief HostState to systemd target mapping table. **/
341 std::map<HostState, std::string> stateTargetTable;
342
343 /** @brief Requested Transition to systemd target mapping table. **/
344 std::map<Transition, std::string> transitionTargetTable;
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400345
346 /** @brief Target called when a host crash occurs **/
347 std::string hostCrashTarget;
Andrew Geissler36529022016-11-29 15:23:54 -0600348};
349
350} // namespace manager
351} // namespace state
352} // namespace phosphor