blob: 8e6013039adcc1489b273f344de028c760fd04ae [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 <string>
Andrew Geissler36529022016-11-29 15:23:54 -060018
19namespace phosphor
20{
21namespace state
22{
23namespace manager
24{
25
Patrick Williamsf053e6f2022-07-22 19:26:54 -050026using HostInherit = sdbusplus::server::object_t<
Andrew Geissler58a18012018-01-19 19:36:05 -080027 sdbusplus::xyz::openbmc_project::State::server::Host,
28 sdbusplus::xyz::openbmc_project::State::Boot::server::Progress,
29 sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts,
30 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::Status>;
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050031
Andrew Geissler8ffdb262021-09-20 15:25:19 -050032PHOSPHOR_LOG2_USING;
Andrew Geissler7b90a622017-08-08 11:41:08 -050033
Patrick Williamsd22706f2017-05-04 05:42:49 -050034namespace sdbusRule = sdbusplus::bus::match::rules;
Patrick Williams6ed41ea2022-04-05 15:50:21 -050035namespace fs = std::filesystem;
Patrick Williamsd22706f2017-05-04 05:42:49 -050036
Andrew Geissler36529022016-11-29 15:23:54 -060037/** @class Host
38 * @brief OpenBMC host state management implementation.
39 * @details A concrete implementation for xyz.openbmc_project.State.Host
40 * DBus API.
41 */
Patrick Williamsd22706f2017-05-04 05:42:49 -050042class Host : public HostInherit
Andrew Geissler36529022016-11-29 15:23:54 -060043{
Andrew Geissler58a18012018-01-19 19:36:05 -080044 public:
45 /** @brief Constructs Host State Manager
46 *
47 * @note This constructor passes 'true' to the base class in order to
48 * defer dbus object registration until we can run
49 * determineInitialState() and set our properties
50 *
51 * @param[in] bus - The Dbus bus object
Andrew Geissler58a18012018-01-19 19:36:05 -080052 * @param[in] objPath - The Dbus object path
Allen.Wang79b45002022-02-10 17:59:20 +080053 * @param[in] id - The Host id
Andrew Geissler58a18012018-01-19 19:36:05 -080054 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -050055 Host(sdbusplus::bus_t& bus, const char* objPath, size_t id) :
Patrick Williams76070742022-04-05 15:20:12 -050056 HostInherit(bus, objPath, HostInherit::action::defer_emit), bus(bus),
Andrew Geisslere4039a82020-02-11 15:51:59 -060057 systemdSignalJobRemoved(
Andrew Geissler58a18012018-01-19 19:36:05 -080058 bus,
59 sdbusRule::type::signal() + sdbusRule::member("JobRemoved") +
60 sdbusRule::path("/org/freedesktop/systemd1") +
61 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080062 [this](sdbusplus::message_t& m) { sysStateChangeJobRemoved(m); }),
Andrew Geissler47b96122020-02-11 16:13:13 -060063 systemdSignalJobNew(
64 bus,
65 sdbusRule::type::signal() + sdbusRule::member("JobNew") +
66 sdbusRule::path("/org/freedesktop/systemd1") +
67 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
William A. Kennington IIIbd28f022022-11-22 17:11:49 -080068 [this](sdbusplus::message_t& m) { sysStateChangeJobNew(m); }),
Potin Laic328a4c2022-03-18 23:49:37 +080069 settings(bus, id), id(id)
Andrew Geissler58a18012018-01-19 19:36:05 -080070 {
71 // Enable systemd signals
72 subscribeToSystemdSignals();
Andrew Geissler4da7e002017-01-24 15:21:40 -060073
Allen.Wang79b45002022-02-10 17:59:20 +080074 // create map of target name base on host id
75 createSystemdTargetMaps();
76
Andrew Geissler58a18012018-01-19 19:36:05 -080077 // Will throw exception on fail
78 determineInitialState();
Andrew Geissleref3c1842016-12-01 12:33:09 -060079
NodeMan97b4cbfac2022-05-09 23:56:39 -050080 // Sets auto-reboot attempts to max-allowed
81 attemptsLeft(sdbusplus::xyz::openbmc_project::Control::Boot::server::
82 RebootAttempts::retryAttempts());
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -050083
Andrew Geissler58a18012018-01-19 19:36:05 -080084 // We deferred this until we could get our property correct
85 this->emit_object_added();
86 }
Andrew Geissleref3c1842016-12-01 12:33:09 -060087
Andrew Geissler58a18012018-01-19 19:36:05 -080088 /** @brief Set value of HostTransition */
89 Transition requestedHostTransition(Transition value) override;
Andrew Geissleref3c1842016-12-01 12:33:09 -060090
Andrew Geissler58a18012018-01-19 19:36:05 -080091 /** @brief Set Value for boot progress */
92 ProgressStages bootProgress(ProgressStages value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -050093
Andrew Geissler58a18012018-01-19 19:36:05 -080094 /** @brief Set Value for Operating System Status */
95 OSStatus operatingSystemState(OSStatus value) override;
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -050096
Andrew Geissler58a18012018-01-19 19:36:05 -080097 /** @brief Set value of CurrentHostState */
98 HostState currentHostState(HostState value) override;
Andrew Geissler36529022016-11-29 15:23:54 -060099
Andrew Geissler58a18012018-01-19 19:36:05 -0800100 /**
NodeMan97b4cbfac2022-05-09 23:56:39 -0500101 * @brief Set value for allowable auto-reboot count
102 *
103 * This override is responsible for ensuring that when external users
104 * set the number of automatic retry attempts that the number of
105 * automatic reboot attempts left will update accordingly.
106 *
107 * @param[in] value - desired Reboot count value
108 *
109 * @return number of reboot attempts allowed.
110 */
111 uint32_t retryAttempts(uint32_t value) override
112 {
113 if (sdbusplus::xyz::openbmc_project::Control::Boot::server::
114 RebootAttempts::attemptsLeft() != value)
115 {
116 info("Automatic reboot retry attempts set to: {VALUE} ", "VALUE",
117 value);
118 sdbusplus::xyz::openbmc_project::Control::Boot::server::
119 RebootAttempts::attemptsLeft(value);
120 }
121
122 return (sdbusplus::xyz::openbmc_project::Control::Boot::server::
123 RebootAttempts::retryAttempts(value));
124 }
125
126 /**
Andrew Geissler58a18012018-01-19 19:36:05 -0800127 * @brief Set host reboot count to default
128 *
129 * OpenBMC software controls the number of allowed reboot attempts so
130 * any external set request of this property will be overridden by
NodeMan97b4cbfac2022-05-09 23:56:39 -0500131 * this function and set to the number of the allowed auto-reboot
132 * retry attempts found on the system.
Andrew Geissler58a18012018-01-19 19:36:05 -0800133 *
134 * The only code responsible for decrementing the boot count resides
135 * within this process and that will use the sub class interface
136 * directly
137 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500138 * @param[in] value - Reboot count value
Andrew Geissler58a18012018-01-19 19:36:05 -0800139 *
NodeMan97b4cbfac2022-05-09 23:56:39 -0500140 * @return number of reboot attempts left(allowed by retry attempts
141 * property)
Andrew Geissler58a18012018-01-19 19:36:05 -0800142 */
143 uint32_t attemptsLeft(uint32_t value) override
144 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500145 debug("External request to reset reboot count");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500146 auto retryAttempts = sdbusplus::xyz::openbmc_project::Control::Boot::
147 server::RebootAttempts::retryAttempts();
148 return (
149 sdbusplus::xyz::openbmc_project::Control::Boot::server::
150 RebootAttempts::attemptsLeft(std::min(value, retryAttempts)));
Andrew Geissler58a18012018-01-19 19:36:05 -0800151 }
Andrew Geissler7b90a622017-08-08 11:41:08 -0500152
Andrew Geissler58a18012018-01-19 19:36:05 -0800153 private:
154 /**
155 * @brief subscribe to the systemd signals
156 *
157 * This object needs to capture when it's systemd targets complete
158 * so it can keep it's state updated
159 *
160 **/
161 void subscribeToSystemdSignals();
Andrew Geissler4da7e002017-01-24 15:21:40 -0600162
Andrew Geissler58a18012018-01-19 19:36:05 -0800163 /**
164 * @brief Determine initial host state and set internally
165 *
166 * @return Will throw exceptions on failure
167 **/
168 void determineInitialState();
Andrew Geissler4da7e002017-01-24 15:21:40 -0600169
Allen.Wang79b45002022-02-10 17:59:20 +0800170 /**
171 * create systemd target instance names and mapping table
172 **/
173 void createSystemdTargetMaps();
174
Andrew Geissler58a18012018-01-19 19:36:05 -0800175 /** @brief Execute the transition request
176 *
177 * This function assumes the state has been validated and the host
178 * is in an appropriate state for the transition to be started.
179 *
180 * @param[in] tranReq - Transition requested
181 */
182 void executeTransition(Transition tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600183
Andrew Geissler58a18012018-01-19 19:36:05 -0800184 /**
185 * @brief Determine if target is active
186 *
187 * This function determines if the target is active and
188 * helps prevent misleading log recorded states.
189 *
190 * @param[in] target - Target string to check on
191 *
192 * @return boolean corresponding to state active
193 **/
194 bool stateActive(const std::string& target);
Josh D. King929ef702017-03-02 10:58:11 -0600195
Andrew Geissler58a18012018-01-19 19:36:05 -0800196 /**
197 * @brief Determine if auto reboot flag is set
198 *
199 * @return boolean corresponding to current auto_reboot setting
200 **/
201 bool isAutoReboot();
Michael Tritz206a8332017-02-06 16:01:23 -0600202
Andrew Geissler58a18012018-01-19 19:36:05 -0800203 /** @brief Check if systemd state change is relevant to this object
204 *
205 * Instance specific interface to handle the detected systemd state
206 * change
207 *
208 * @param[in] msg - Data associated with subscribed signal
209 *
210 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500211 void sysStateChangeJobRemoved(sdbusplus::message_t& msg);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600212
Andrew Geissler47b96122020-02-11 16:13:13 -0600213 /** @brief Check if JobNew systemd signal is relevant to this object
214 *
215 * In certain instances phosphor-state-manager needs to monitor for the
216 * entry into a systemd target. This function will be used for these cases.
217 *
218 * Instance specific interface to handle the detected systemd state
219 * change
220 *
221 * @param[in] msg - Data associated with subscribed signal
222 *
223 */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500224 void sysStateChangeJobNew(sdbusplus::message_t& msg);
Andrew Geissler47b96122020-02-11 16:13:13 -0600225
Andrew Geissler58a18012018-01-19 19:36:05 -0800226 /** @brief Decrement reboot count
227 *
228 * This is used internally to this application to decrement the boot
229 * count on each boot attempt. The host will use the external
230 * attemptsLeft() interface to reset the count when a boot is successful
231 *
232 * @return number of reboot count attempts left
233 */
234 uint32_t decrementRebootCount();
Andrew Geissler7b90a622017-08-08 11:41:08 -0500235
Andrew Geissler58a18012018-01-19 19:36:05 -0800236 // Allow cereal class access to allow these next two function to be
237 // private
238 friend class cereal::access;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500239
Andrew Geissler58a18012018-01-19 19:36:05 -0800240 /** @brief Function required by Cereal to perform serialization.
241 *
242 * @tparam Archive - Cereal archive type (binary in our case).
243 * @param[in] archive - reference to Cereal archive.
244 * @param[in] version - Class version that enables handling
245 * a serialized data across code levels
246 */
247 template <class Archive>
248 void save(Archive& archive, const std::uint32_t version) const
249 {
Andrew Geissler769a62f2019-12-06 13:36:08 -0600250 // version is not used currently
251 (void)(version);
NodeMan97b4cbfac2022-05-09 23:56:39 -0500252 archive(sdbusplus::xyz::openbmc_project::Control::Boot::server::
253 RebootAttempts::retryAttempts(),
254 convertForMessage(sdbusplus::xyz::openbmc_project::State::
Andrew Geissler58a18012018-01-19 19:36:05 -0800255 server::Host::requestedHostTransition()),
256 convertForMessage(sdbusplus::xyz::openbmc_project::State::Boot::
257 server::Progress::bootProgress()),
258 convertForMessage(
259 sdbusplus::xyz::openbmc_project::State::OperatingSystem::
260 server::Status::operatingSystemState()));
261 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500262
Andrew Geissler58a18012018-01-19 19:36:05 -0800263 /** @brief Function required by Cereal to perform deserialization.
264 *
265 * @tparam Archive - Cereal archive type (binary in our case).
266 * @param[in] archive - reference to Cereal archive.
267 * @param[in] version - Class version that enables handling
268 * a serialized data across code levels
269 */
270 template <class Archive>
271 void load(Archive& archive, const std::uint32_t version)
272 {
273 std::string reqTranState;
274 std::string bootProgress;
275 std::string osState;
NodeMan97b4cbfac2022-05-09 23:56:39 -0500276 // Older cereal archive without RetryAttempt may be implemented
277 // just set to (BOOT_COUNT_MAX_ALLOWED)
278 uint32_t retryAttempts = BOOT_COUNT_MAX_ALLOWED;
279 switch (version)
280 {
281 case 2:
282 archive(retryAttempts);
283 [[fallthrough]];
284 case 1:
285 archive(reqTranState, bootProgress, osState);
286 break;
287 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800288 auto reqTran = Host::convertTransitionFromString(reqTranState);
289 // When restoring, set the requested state with persistent value
290 // but don't call the override which would execute it
291 sdbusplus::xyz::openbmc_project::State::server::Host::
292 requestedHostTransition(reqTran);
293 sdbusplus::xyz::openbmc_project::State::Boot::server::Progress::
294 bootProgress(Host::convertProgressStagesFromString(bootProgress));
295 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server::
296 Status::operatingSystemState(
297 Host::convertOSStatusFromString(osState));
NodeMan97b4cbfac2022-05-09 23:56:39 -0500298 sdbusplus::xyz::openbmc_project::Control::Boot::server::RebootAttempts::
299 retryAttempts(retryAttempts);
Andrew Geissler58a18012018-01-19 19:36:05 -0800300 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500301
Andrew Geissler58a18012018-01-19 19:36:05 -0800302 /** @brief Serialize and persist requested host state
303 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800304 * @return fs::path - pathname of persisted requested host state.
305 */
Allen.Wangba182f02022-03-23 19:01:53 +0800306 fs::path serialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500307
Andrew Geissler58a18012018-01-19 19:36:05 -0800308 /** @brief Deserialze a persisted requested host state.
309 *
Andrew Geissler58a18012018-01-19 19:36:05 -0800310 * @return bool - true if the deserialization was successful, false
311 * otherwise.
312 */
Allen.Wangba182f02022-03-23 19:01:53 +0800313 bool deserialize();
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500314
Allen.Wang79b45002022-02-10 17:59:20 +0800315 /**
316 * @brief Get target name of a HostState
317 *
318 * @param[in] state - The state of the host
319 *
320 * @return string - systemd target name of the state
321 */
322 const std::string& getTarget(HostState state);
323
324 /**
325 * @brief Get target name of a TransitionRequest
326 *
327 * @param[in] tranReq - Transition requested
328 *
329 * @return string - systemd target name of Requested transition
330 */
331 const std::string& getTarget(Transition tranReq);
332
Andrew Geissler58a18012018-01-19 19:36:05 -0800333 /** @brief Persistent sdbusplus DBus bus connection. */
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500334 sdbusplus::bus_t& bus;
Andrew Geissleref621162016-12-08 12:56:21 -0600335
Andrew Geisslere4039a82020-02-11 15:51:59 -0600336 /** @brief Used to subscribe to dbus systemd JobRemoved signal **/
337 sdbusplus::bus::match_t systemdSignalJobRemoved;
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500338
Andrew Geissler47b96122020-02-11 16:13:13 -0600339 /** @brief Used to subscribe to dbus systemd JobNew signal **/
340 sdbusplus::bus::match_t systemdSignalJobNew;
341
Potin Laic328a4c2022-03-18 23:49:37 +0800342 // Settings host objects of interest
343 settings::HostObjects settings;
Allen.Wang79b45002022-02-10 17:59:20 +0800344
345 /** @brief Host id. **/
346 const size_t id = 0;
347
348 /** @brief HostState to systemd target mapping table. **/
349 std::map<HostState, std::string> stateTargetTable;
350
351 /** @brief Requested Transition to systemd target mapping table. **/
352 std::map<Transition, std::string> transitionTargetTable;
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400353
354 /** @brief Target called when a host crash occurs **/
355 std::string hostCrashTarget;
Andrew Geissler36529022016-11-29 15:23:54 -0600356};
357
358} // namespace manager
359} // namespace state
360} // namespace phosphor