blob: cd1389cbe03f9440f8f4d101b8b328cfc523d216 [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"
6#include "xyz/openbmc_project/State/Host/server.hpp"
7
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>
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050014#include <xyz/openbmc_project/State/OperatingSystem/Status/server.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050015
Patrick Williams6ed41ea2022-04-05 15:50:21 -050016#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050017#include <functional>
18#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"),
Andrew Geisslere4039a82020-02-11 15:51:59 -060063 std::bind(std::mem_fn(&Host::sysStateChangeJobRemoved), this,
Andrew Geissler58a18012018-01-19 19:36:05 -080064 std::placeholders::_1)),
Andrew Geissler47b96122020-02-11 16:13:13 -060065 systemdSignalJobNew(
66 bus,
67 sdbusRule::type::signal() + sdbusRule::member("JobNew") +
68 sdbusRule::path("/org/freedesktop/systemd1") +
69 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
70 std::bind(std::mem_fn(&Host::sysStateChangeJobNew), this,
71 std::placeholders::_1)),
Potin Laic328a4c2022-03-18 23:49:37 +080072 settings(bus, id), id(id)
Andrew Geissler58a18012018-01-19 19:36:05 -080073 {
74 // Enable systemd signals
75 subscribeToSystemdSignals();
Andrew Geissler4da7e002017-01-24 15:21:40 -060076
Allen.Wang79b45002022-02-10 17:59:20 +080077 // create map of target name base on host id
78 createSystemdTargetMaps();
79
Andrew Geissler58a18012018-01-19 19:36:05 -080080 // Will throw exception on fail
81 determineInitialState();
Andrew Geissleref3c1842016-12-01 12:33:09 -060082
NodeMan97b4cbfac2022-05-09 23:56:39 -050083 // Sets auto-reboot attempts to max-allowed
84 attemptsLeft(sdbusplus::xyz::openbmc_project::Control::Boot::server::
85 RebootAttempts::retryAttempts());
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050086
Andrew Geissler58a18012018-01-19 19:36:05 -080087 // We deferred this until we could get our property correct
88 this->emit_object_added();
89 }
Andrew Geissleref3c1842016-12-01 12:33:09 -060090
Andrew Geissler58a18012018-01-19 19:36:05 -080091 /** @brief Set value of HostTransition */
92 Transition requestedHostTransition(Transition value) override;
Andrew Geissleref3c1842016-12-01 12:33:09 -060093
Andrew Geissler58a18012018-01-19 19:36:05 -080094 /** @brief Set Value for boot progress */
95 ProgressStages bootProgress(ProgressStages value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -050096
Andrew Geissler58a18012018-01-19 19:36:05 -080097 /** @brief Set Value for Operating System Status */
98 OSStatus operatingSystemState(OSStatus value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -050099
Andrew Geissler58a18012018-01-19 19:36:05 -0800100 /** @brief Set value of CurrentHostState */
101 HostState currentHostState(HostState value) override;
Andrew Geissler36529022016-11-29 15:23:54 -0600102
Andrew Geissler58a18012018-01-19 19:36:05 -0800103 /**
NodeMan97b4cbfac2022-05-09 23:56:39 -0500104 * @brief Set value for allowable auto-reboot count
105 *
106 * This override is responsible for ensuring that when external users
107 * set the number of automatic retry attempts that the number of
108 * automatic reboot attempts left will update accordingly.
109 *
110 * @param[in] value - desired Reboot count value
111 *
112 * @return number of reboot attempts allowed.
113 */
114 uint32_t retryAttempts(uint32_t value) override
115 {
116 if (sdbusplus::xyz::openbmc_project::Control::Boot::server::
117 RebootAttempts::attemptsLeft() != value)
118 {
119 info("Automatic reboot retry attempts set to: {VALUE} ", "VALUE",
120 value);
121 sdbusplus::xyz::openbmc_project::Control::Boot::server::
122 RebootAttempts::attemptsLeft(value);
123 }
124
125 return (sdbusplus::xyz::openbmc_project::Control::Boot::server::
126 RebootAttempts::retryAttempts(value));
127 }
128
129 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800130 * @brief Set host reboot count to default
131 *
132 * OpenBMC software controls the number of allowed reboot attempts so
133 * any external set request of this property will be overridden by
NodeMan97b4cbfac2022-05-09 23:56:39 -0500134 * this function and set to the number of the allowed auto-reboot
135 * retry attempts found on the system.
Andrew Geissler58a18012018-01-19 19:36:05 -0800136 *
137 * The only code responsible for decrementing the boot count resides
138 * within this process and that will use the sub class interface
139 * directly
140 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500141 * @param[in] value - Reboot count value
Andrew Geissler58a18012018-01-19 19:36:05 -0800142 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500143 * @return number of reboot attempts left(allowed by retry attempts
144 * property)
Andrew Geissler58a18012018-01-19 19:36:05 -0800145 */
146 uint32_t attemptsLeft(uint32_t value) override
147 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500148 debug("External request to reset reboot count");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500149 auto retryAttempts = sdbusplus::xyz::openbmc_project::Control::Boot::
150 server::RebootAttempts::retryAttempts();
151 return (
152 sdbusplus::xyz::openbmc_project::Control::Boot::server::
153 RebootAttempts::attemptsLeft(std::min(value, retryAttempts)));
Andrew Geissler58a18012018-01-19 19:36:05 -0800154 }
Andrew Geissler7b90a622017-08-08 11:41:08 -0500155
Andrew Geissler58a18012018-01-19 19:36:05 -0800156 private:
157 /**
158 * @brief subscribe to the systemd signals
159 *
160 * This object needs to capture when it's systemd targets complete
161 * so it can keep it's state updated
162 *
163 **/
164 void subscribeToSystemdSignals();
Andrew Geissler4da7e002017-01-24 15:21:40 -0600165
Andrew Geissler58a18012018-01-19 19:36:05 -0800166 /**
167 * @brief Determine initial host state and set internally
168 *
169 * @return Will throw exceptions on failure
170 **/
171 void determineInitialState();
Andrew Geissler4da7e002017-01-24 15:21:40 -0600172
Allen.Wang79b45002022-02-10 17:59:20 +0800173 /**
174 * create systemd target instance names and mapping table
175 **/
176 void createSystemdTargetMaps();
177
Andrew Geissler58a18012018-01-19 19:36:05 -0800178 /** @brief Execute the transition request
179 *
180 * This function assumes the state has been validated and the host
181 * is in an appropriate state for the transition to be started.
182 *
183 * @param[in] tranReq - Transition requested
184 */
185 void executeTransition(Transition tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600186
Andrew Geissler58a18012018-01-19 19:36:05 -0800187 /**
188 * @brief Determine if target is active
189 *
190 * This function determines if the target is active and
191 * helps prevent misleading log recorded states.
192 *
193 * @param[in] target - Target string to check on
194 *
195 * @return boolean corresponding to state active
196 **/
197 bool stateActive(const std::string& target);
Josh D. King929ef702017-03-02 10:58:11 -0600198
Andrew Geissler58a18012018-01-19 19:36:05 -0800199 /**
200 * @brief Determine if auto reboot flag is set
201 *
202 * @return boolean corresponding to current auto_reboot setting
203 **/
204 bool isAutoReboot();
Michael Tritz206a8332017-02-06 16:01:23 -0600205
Andrew Geissler58a18012018-01-19 19:36:05 -0800206 /** @brief Check if systemd state change is relevant to this object
207 *
208 * Instance specific interface to handle the detected systemd state
209 * change
210 *
211 * @param[in] msg - Data associated with subscribed signal
212 *
213 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500214 void sysStateChangeJobRemoved(sdbusplus::message_t& msg);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600215
Andrew Geissler47b96122020-02-11 16:13:13 -0600216 /** @brief Check if JobNew systemd signal is relevant to this object
217 *
218 * In certain instances phosphor-state-manager needs to monitor for the
219 * entry into a systemd target. This function will be used for these cases.
220 *
221 * Instance specific interface to handle the detected systemd state
222 * change
223 *
224 * @param[in] msg - Data associated with subscribed signal
225 *
226 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500227 void sysStateChangeJobNew(sdbusplus::message_t& msg);
Andrew Geissler47b96122020-02-11 16:13:13 -0600228
Andrew Geissler58a18012018-01-19 19:36:05 -0800229 /** @brief Decrement reboot count
230 *
231 * This is used internally to this application to decrement the boot
232 * count on each boot attempt. The host will use the external
233 * attemptsLeft() interface to reset the count when a boot is successful
234 *
235 * @return number of reboot count attempts left
236 */
237 uint32_t decrementRebootCount();
Andrew Geissler7b90a622017-08-08 11:41:08 -0500238
Andrew Geissler58a18012018-01-19 19:36:05 -0800239 // Allow cereal class access to allow these next two function to be
240 // private
241 friend class cereal::access;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500242
Andrew Geissler58a18012018-01-19 19:36:05 -0800243 /** @brief Function required by Cereal to perform serialization.
244 *
245 * @tparam Archive - Cereal archive type (binary in our case).
246 * @param[in] archive - reference to Cereal archive.
247 * @param[in] version - Class version that enables handling
248 * a serialized data across code levels
249 */
250 template <class Archive>
251 void save(Archive& archive, const std::uint32_t version) const
252 {
Andrew Geissler769a62f2019-12-06 13:36:08 -0600253 // version is not used currently
254 (void)(version);
NodeMan97b4cbfac2022-05-09 23:56:39 -0500255 archive(sdbusplus::xyz::openbmc_project::Control::Boot::server::
256 RebootAttempts::retryAttempts(),
257 convertForMessage(sdbusplus::xyz::openbmc_project::State::
Andrew Geissler58a18012018-01-19 19:36:05 -0800258 server::Host::requestedHostTransition()),
259 convertForMessage(sdbusplus::xyz::openbmc_project::State::Boot::
260 server::Progress::bootProgress()),
261 convertForMessage(
262 sdbusplus::xyz::openbmc_project::State::OperatingSystem::
263 server::Status::operatingSystemState()));
264 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500265
Andrew Geissler58a18012018-01-19 19:36:05 -0800266 /** @brief Function required by Cereal to perform deserialization.
267 *
268 * @tparam Archive - Cereal archive type (binary in our case).
269 * @param[in] archive - reference to Cereal archive.
270 * @param[in] version - Class version that enables handling
271 * a serialized data across code levels
272 */
273 template <class Archive>
274 void load(Archive& archive, const std::uint32_t version)
275 {
276 std::string reqTranState;
277 std::string bootProgress;
278 std::string osState;
NodeMan97b4cbfac2022-05-09 23:56:39 -0500279 // Older cereal archive without RetryAttempt may be implemented
280 // just set to (BOOT_COUNT_MAX_ALLOWED)
281 uint32_t retryAttempts = BOOT_COUNT_MAX_ALLOWED;
282 switch (version)
283 {
284 case 2:
285 archive(retryAttempts);
286 [[fallthrough]];
287 case 1:
288 archive(reqTranState, bootProgress, osState);
289 break;
290 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800291 auto reqTran = Host::convertTransitionFromString(reqTranState);
292 // When restoring, set the requested state with persistent value
293 // but don't call the override which would execute it
294 sdbusplus::xyz::openbmc_project::State::server::Host::
295 requestedHostTransition(reqTran);
296 sdbusplus::xyz::openbmc_project::State::Boot::server::Progress::
297 bootProgress(Host::convertProgressStagesFromString(bootProgress));
298 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::
299 Status::operatingSystemState(
300 Host::convertOSStatusFromString(osState));
NodeMan97b4cbfac2022-05-09 23:56:39 -0500301 sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts::
302 retryAttempts(retryAttempts);
Andrew Geissler58a18012018-01-19 19:36:05 -0800303 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500304
Andrew Geissler58a18012018-01-19 19:36:05 -0800305 /** @brief Serialize and persist requested host state
306 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800307 * @return fs::path - pathname of persisted requested host state.
308 */
Allen.Wangba182f02022-03-23 19:01:53 +0800309 fs::path serialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500310
Andrew Geissler58a18012018-01-19 19:36:05 -0800311 /** @brief Deserialze a persisted requested host state.
312 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800313 * @return bool - true if the deserialization was successful, false
314 * otherwise.
315 */
Allen.Wangba182f02022-03-23 19:01:53 +0800316 bool deserialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500317
Allen.Wang79b45002022-02-10 17:59:20 +0800318 /**
319 * @brief Get target name of a HostState
320 *
321 * @param[in] state - The state of the host
322 *
323 * @return string - systemd target name of the state
324 */
325 const std::string& getTarget(HostState state);
326
327 /**
328 * @brief Get target name of a TransitionRequest
329 *
330 * @param[in] tranReq - Transition requested
331 *
332 * @return string - systemd target name of Requested transition
333 */
334 const std::string& getTarget(Transition tranReq);
335
Andrew Geissler58a18012018-01-19 19:36:05 -0800336 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500337 sdbusplus::bus_t& bus;
Andrew Geissleref621162016-12-08 12:56:21 -0600338
Andrew Geisslere4039a82020-02-11 15:51:59 -0600339 /** @brief Used to subscribe to dbus systemd JobRemoved signal **/
340 sdbusplus::bus::match_t systemdSignalJobRemoved;
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500341
Andrew Geissler47b96122020-02-11 16:13:13 -0600342 /** @brief Used to subscribe to dbus systemd JobNew signal **/
343 sdbusplus::bus::match_t systemdSignalJobNew;
344
Potin Laic328a4c2022-03-18 23:49:37 +0800345 // Settings host objects of interest
346 settings::HostObjects settings;
Allen.Wang79b45002022-02-10 17:59:20 +0800347
348 /** @brief Host id. **/
349 const size_t id = 0;
350
351 /** @brief HostState to systemd target mapping table. **/
352 std::map<HostState, std::string> stateTargetTable;
353
354 /** @brief Requested Transition to systemd target mapping table. **/
355 std::map<Transition, std::string> transitionTargetTable;
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400356
357 /** @brief Target called when a host crash occurs **/
358 std::string hostCrashTarget;
Andrew Geissler36529022016-11-29 15:23:54 -0600359};
360
361} // namespace manager
362} // namespace state
363} // namespace phosphor