blob: 884372dc37272e4d4721bc41abc713d4589edf60 [file] [log] [blame]
Andrew Geissler36529022016-11-29 15:23:54 -06001#include <iostream>
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -06002#include <map>
3#include <string>
Andrew Geissleref3c1842016-12-01 12:33:09 -06004#include <systemd/sd-bus.h>
Andrew Geissler033fc3b2017-08-30 15:11:44 -05005#include <cereal/cereal.hpp>
6#include <cereal/types/string.hpp>
7#include <cereal/types/vector.hpp>
8#include <cereal/types/tuple.hpp>
9#include <cereal/archives/json.hpp>
10#include <fstream>
Michael Tritz206a8332017-02-06 16:01:23 -060011#include <sdbusplus/server.hpp>
Saqib Khana8006a22017-02-14 11:37:08 -060012#include <phosphor-logging/log.hpp>
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050013#include <phosphor-logging/elog-errors.hpp>
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050014#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050015#include <xyz/openbmc_project/Common/error.hpp>
Andrew Geissler36529022016-11-29 15:23:54 -060016#include "host_state_manager.hpp"
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050017#include "config.h"
18
Vishwanatha Subbanna0a838732017-10-05 12:43:19 +053019// Register class version with Cereal
20CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION);
Andrew Geissler36529022016-11-29 15:23:54 -060021
22namespace phosphor
23{
24namespace state
25{
26namespace manager
27{
28
Andrew Geissler7b90a622017-08-08 11:41:08 -050029// When you see server:: or reboot:: you know we're referencing our base class
Andrew Geissler3e3b84b2016-12-02 15:46:17 -060030namespace server = sdbusplus::xyz::openbmc_project::State::server;
Andrew Geissler7b90a622017-08-08 11:41:08 -050031namespace reboot = sdbusplus::xyz::openbmc_project::Control::Boot::server;
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -050032namespace bootprogress = sdbusplus::xyz::openbmc_project::State::Boot::server;
33namespace osstatus =
34 sdbusplus::xyz::openbmc_project::State::OperatingSystem::server;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060035using namespace phosphor::logging;
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050036namespace fs = std::experimental::filesystem;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060037
Andrew Geissler4f309e82017-05-24 15:18:01 -050038// host-shutdown notifies host of shutdown and that leads to host-stop being
39// called so initiate a host shutdown with the -shutdown target and consider the
40// host shut down when the -stop target is complete
41constexpr auto HOST_STATE_SOFT_POWEROFF_TGT = "obmc-host-shutdown@0.target";
Josh D. Kingca357922017-04-11 13:44:09 -050042constexpr auto HOST_STATE_POWEROFF_TGT = "obmc-host-stop@0.target";
43constexpr auto HOST_STATE_POWERON_TGT = "obmc-host-start@0.target";
Andrew Geisslera27a6e82017-07-27 16:44:43 -050044constexpr auto HOST_STATE_REBOOT_TGT = "obmc-host-reboot@0.target";
Josh D. Kingcc3fb5d2017-04-19 15:45:10 -050045constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target";
Andrew Geissler4da7e002017-01-24 15:21:40 -060046
Josh D. King929ef702017-03-02 10:58:11 -060047constexpr auto ACTIVE_STATE = "active";
48constexpr auto ACTIVATING_STATE = "activating";
49
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060050/* Map a transition to it's systemd target */
51const std::map<server::Host::Transition,std::string> SYSTEMD_TARGET_TABLE =
52{
Andrew Geissler4f309e82017-05-24 15:18:01 -050053 {server::Host::Transition::Off, HOST_STATE_SOFT_POWEROFF_TGT},
Andrew Geisslera27a6e82017-07-27 16:44:43 -050054 {server::Host::Transition::On, HOST_STATE_POWERON_TGT},
55 {server::Host::Transition::Reboot, HOST_STATE_REBOOT_TGT}
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060056};
57
58constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
59constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
60constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
61
Michael Tritz206a8332017-02-06 16:01:23 -060062constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
Leonel Gonzalezf318d872017-03-16 13:47:49 -050063constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
Michael Tritz206a8332017-02-06 16:01:23 -060064constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
65
Josh D. King929ef702017-03-02 10:58:11 -060066constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
67constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
68
Andrew Geissleref621162016-12-08 12:56:21 -060069/* Map a system state to the HostState */
Andrew Geissleref621162016-12-08 12:56:21 -060070const std::map<std::string, server::Host::HostState> SYS_HOST_STATE_TABLE = {
71 {"HOST_BOOTING", server::Host::HostState::Running},
Saqib Khanadaa7212017-02-23 13:19:28 -060072 {"HOST_POWERED_OFF", server::Host::HostState::Off},
73 {"HOST_QUIESCED", server::Host::HostState::Quiesced}
Andrew Geissleref621162016-12-08 12:56:21 -060074};
75
Andrew Geissler4da7e002017-01-24 15:21:40 -060076void Host::subscribeToSystemdSignals()
77{
78 auto method = this->bus.new_method_call(SYSTEMD_SERVICE,
79 SYSTEMD_OBJ_PATH,
80 SYSTEMD_INTERFACE,
81 "Subscribe");
82 this->bus.call_noreply(method);
83
84 return;
85}
86
Andrew Geissleref3c1842016-12-01 12:33:09 -060087void Host::determineInitialState()
88{
Andrew Geissleref3c1842016-12-01 12:33:09 -060089
Josh D. King222014e2017-05-16 13:35:21 -050090 if(stateActive(HOST_STATE_POWERON_TGT))
Andrew Geissleref3c1842016-12-01 12:33:09 -060091 {
Andrew Geissler1e3bf942016-12-13 15:32:22 -060092 log<level::INFO>("Initial Host State will be Running",
93 entry("CURRENT_HOST_STATE=%s",
94 convertForMessage(HostState::Running).c_str()));
Andrew Geissler97924142017-01-24 16:10:18 -060095 server::Host::currentHostState(HostState::Running);
96 server::Host::requestedHostTransition(Transition::On);
Andrew Geissleref3c1842016-12-01 12:33:09 -060097 }
98 else
99 {
Andrew Geissler1e3bf942016-12-13 15:32:22 -0600100 log<level::INFO>("Initial Host State will be Off",
101 entry("CURRENT_HOST_STATE=%s",
102 convertForMessage(HostState::Off).c_str()));
Andrew Geissler97924142017-01-24 16:10:18 -0600103 server::Host::currentHostState(HostState::Off);
104 server::Host::requestedHostTransition(Transition::Off);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600105 }
106
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500107 if (!deserialize(HOST_STATE_PERSIST_PATH))
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500108 {
109 //set to default value.
110 server::Host::requestedHostTransition(Transition::Off);
111 }
Andrew Geissleref3c1842016-12-01 12:33:09 -0600112
113 return;
114}
115
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600116void Host::executeTransition(Transition tranReq)
117{
118 auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second;
119
120 auto method = this->bus.new_method_call(SYSTEMD_SERVICE,
121 SYSTEMD_OBJ_PATH,
122 SYSTEMD_INTERFACE,
123 "StartUnit");
124
125 method.append(sysdUnit);
126 method.append("replace");
127
Andrew Geissler4da7e002017-01-24 15:21:40 -0600128 this->bus.call_noreply(method);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600129
130 return;
131}
132
Josh D. King929ef702017-03-02 10:58:11 -0600133bool Host::stateActive(const std::string& target)
134{
135 sdbusplus::message::variant<std::string> currentState;
136 sdbusplus::message::object_path unitTargetPath;
137
138 auto method = this->bus.new_method_call(SYSTEMD_SERVICE,
139 SYSTEMD_OBJ_PATH,
140 SYSTEMD_INTERFACE,
141 "GetUnit");
142
143 method.append(target);
144 auto result = this->bus.call(method);
145
146 //Check that the bus call didn't result in an error
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500147 if (result.is_method_error())
Josh D. King929ef702017-03-02 10:58:11 -0600148 {
149 log<level::ERR>("Error in bus call - could not resolve GetUnit for:",
150 entry(" %s", SYSTEMD_INTERFACE));
151 return false;
152 }
153
154 result.read(unitTargetPath);
155
156 method = this->bus.new_method_call(SYSTEMD_SERVICE,
157 static_cast<const std::string&>
158 (unitTargetPath).c_str(),
159 SYSTEMD_PROPERTY_IFACE,
160 "Get");
161
162 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
163 result = this->bus.call(method);
164
165 //Check that the bus call didn't result in an error
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500166 if (result.is_method_error())
Josh D. King929ef702017-03-02 10:58:11 -0600167 {
168 log<level::ERR>("Error in bus call - could not resolve Get for:",
169 entry(" %s", SYSTEMD_PROPERTY_IFACE));
170 return false;
171 }
172
173 result.read(currentState);
174
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500175 if (currentState != ACTIVE_STATE && currentState != ACTIVATING_STATE)
Josh D. King929ef702017-03-02 10:58:11 -0600176 {
177 //False - not active
178 return false;
179 }
180 //True - active
181 return true;
182}
183
Michael Tritz206a8332017-02-06 16:01:23 -0600184bool Host::isAutoReboot()
185{
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500186 using namespace settings;
Michael Tritz206a8332017-02-06 16:01:23 -0600187
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500188 auto method =
189 bus.new_method_call(
190 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
191 settings.autoReboot.c_str(),
192 "org.freedesktop.DBus.Properties",
193 "Get");
194 method.append(autoRebootIntf, "AutoReboot");
195 auto reply = bus.call(method);
Michael Tritz206a8332017-02-06 16:01:23 -0600196 if (reply.is_method_error())
197 {
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500198 log<level::ERR>("Error in AutoReboot Get");
Michael Tritz206a8332017-02-06 16:01:23 -0600199 return false;
200 }
201
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500202 sdbusplus::message::variant<bool> result;
203 reply.read(result);
204 auto autoReboot = result.get<bool>();
Andrew Geissler7b90a622017-08-08 11:41:08 -0500205 auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft();
Saqib Khancbe08d12017-03-10 01:29:20 -0600206
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500207 if (autoReboot)
Michael Tritz206a8332017-02-06 16:01:23 -0600208 {
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -0500209 if (rebootCounterParam > 0)
Saqib Khancbe08d12017-03-10 01:29:20 -0600210 {
211 // Reduce BOOTCOUNT by 1
Andrew Geissler7b90a622017-08-08 11:41:08 -0500212 log<level::INFO>("Auto reboot enabled, rebooting");
Saqib Khancbe08d12017-03-10 01:29:20 -0600213 return true;
214 }
Saqib Khand5ac6352017-04-04 09:53:59 -0500215 else if(rebootCounterParam == 0)
Josh D. King929ef702017-03-02 10:58:11 -0600216 {
Saqib Khancbe08d12017-03-10 01:29:20 -0600217 // Reset reboot counter and go to quiesce state
Saqib Khand5ac6352017-04-04 09:53:59 -0500218 log<level::INFO>("Auto reboot enabled. "
219 "HOST BOOTCOUNT already set to 0.");
Dhruvaraj Subhashchandran2710e732017-06-19 06:43:22 -0500220 attemptsLeft(BOOT_COUNT_MAX_ALLOWED);
Saqib Khand5ac6352017-04-04 09:53:59 -0500221 return false;
222 }
223 else
224 {
225 log<level::INFO>("Auto reboot enabled. "
226 "HOST BOOTCOUNT has an invalid value.");
Saqib Khancbe08d12017-03-10 01:29:20 -0600227 return false;
228 }
Michael Tritz206a8332017-02-06 16:01:23 -0600229 }
Saqib Khand5ac6352017-04-04 09:53:59 -0500230 else
231 {
232 log<level::INFO>("Auto reboot disabled.");
233 return false;
234 }
Michael Tritz206a8332017-02-06 16:01:23 -0600235}
236
Patrick Williamsd22706f2017-05-04 05:42:49 -0500237void Host::sysStateChange(sdbusplus::message::message& msg)
Andrew Geissler4da7e002017-01-24 15:21:40 -0600238{
239 uint32_t newStateID {};
240 sdbusplus::message::object_path newStateObjPath;
241 std::string newStateUnit{};
242 std::string newStateResult{};
243
Andrew Geissler4da7e002017-01-24 15:21:40 -0600244 //Read the msg and populate each variable
Patrick Williamsd22706f2017-05-04 05:42:49 -0500245 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600246
247 if((newStateUnit == HOST_STATE_POWEROFF_TGT) &&
Josh D. King929ef702017-03-02 10:58:11 -0600248 (newStateResult == "done") &&
249 (!stateActive(HOST_STATE_POWERON_TGT)))
Andrew Geissleref621162016-12-08 12:56:21 -0600250 {
Andrew Jeffery55b983e2017-04-19 11:11:26 +0930251 log<level::INFO>("Received signal that host is off");
Andrew Geissler4da7e002017-01-24 15:21:40 -0600252 this->currentHostState(server::Host::HostState::Off);
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -0500253 this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified);
254 this->operatingSystemState(osstatus::Status::OSStatus::Inactive);
Andrew Geissler06dbc5b2016-12-13 11:46:16 -0600255
Andrew Geissleref621162016-12-08 12:56:21 -0600256 }
Andrew Geissler4da7e002017-01-24 15:21:40 -0600257 else if((newStateUnit == HOST_STATE_POWERON_TGT) &&
Josh D. King929ef702017-03-02 10:58:11 -0600258 (newStateResult == "done") &&
259 (stateActive(HOST_STATE_POWERON_TGT)))
Andrew Geissler4da7e002017-01-24 15:21:40 -0600260 {
Andrew Jeffery55b983e2017-04-19 11:11:26 +0930261 log<level::INFO>("Received signal that host is running");
Andrew Geissler4da7e002017-01-24 15:21:40 -0600262 this->currentHostState(server::Host::HostState::Running);
263 }
Michael Tritz206a8332017-02-06 16:01:23 -0600264 else if((newStateUnit == HOST_STATE_QUIESCE_TGT) &&
Josh D. King29d025d2017-04-27 12:40:22 -0500265 (newStateResult == "done") &&
266 (stateActive(HOST_STATE_QUIESCE_TGT)))
Michael Tritz206a8332017-02-06 16:01:23 -0600267 {
268 if (Host::isAutoReboot())
269 {
Saqib Khand5ac6352017-04-04 09:53:59 -0500270 log<level::INFO>("Beginning reboot...");
Michael Tritz206a8332017-02-06 16:01:23 -0600271 Host::requestedHostTransition(server::Host::Transition::Reboot);
272 }
273 else
274 {
Saqib Khand5ac6352017-04-04 09:53:59 -0500275 log<level::INFO>("Maintaining quiesce");
Saqib Khanadaa7212017-02-23 13:19:28 -0600276 this->currentHostState(server::Host::HostState::Quiesced);
Michael Tritz206a8332017-02-06 16:01:23 -0600277 }
278
279 }
Andrew Geissleref621162016-12-08 12:56:21 -0600280}
281
Andrew Geissler7b90a622017-08-08 11:41:08 -0500282uint32_t Host::decrementRebootCount()
283{
284 auto rebootCount = reboot::RebootAttempts::attemptsLeft();
285 if(rebootCount > 0)
286 {
287 return(reboot::RebootAttempts::attemptsLeft(rebootCount - 1));
288 }
289 return rebootCount;
290}
291
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500292fs::path Host::serialize(const fs::path& dir)
293{
294 std::ofstream os(dir.c_str(), std::ios::binary);
295 cereal::JSONOutputArchive oarchive(os);
296 oarchive(*this);
297 return dir;
298}
299
300bool Host::deserialize(const fs::path& path)
301{
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500302 try
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500303 {
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500304 if (fs::exists(path))
305 {
306 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
307 cereal::JSONInputArchive iarchive(is);
308 iarchive(*this);
309 return true;
310 }
311 return false;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500312 }
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500313 catch(cereal::Exception& e)
314 {
315 log<level::ERR>(e.what());
316 fs::remove(path);
317 return false;
318 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500319}
320
Andrew Geissleref3c1842016-12-01 12:33:09 -0600321Host::Transition Host::requestedHostTransition(Transition value)
322{
Andrew Geissler1e3bf942016-12-13 15:32:22 -0600323 log<level::INFO>(
324 "Host State transaction request",
325 entry("REQUESTED_HOST_TRANSITION=%s",
326 convertForMessage(value).c_str()));
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600327
Andrew Geissler7b90a622017-08-08 11:41:08 -0500328 // If this is not a power off request then we need to
329 // decrement the reboot counter. This code should
330 // never prevent a power on, it should just decrement
331 // the count to 0. The quiesce handling is where the
332 // check of this count will occur
333 if(value != server::Host::Transition::Off)
334 {
335 decrementRebootCount();
336 }
337
Andrew Geisslera27a6e82017-07-27 16:44:43 -0500338 executeTransition(value);
Andrew Geissler7b90a622017-08-08 11:41:08 -0500339
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500340 auto retVal = server::Host::requestedHostTransition(value);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500341 serialize();
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500342 return retVal;
Andrew Geissleref3c1842016-12-01 12:33:09 -0600343}
344
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -0500345Host::ProgressStages Host::bootProgress(ProgressStages value)
346{
347 auto retVal = bootprogress::Progress::bootProgress(value);
348 serialize();
349 return retVal;
350}
351
352Host::OSStatus Host::operatingSystemState(OSStatus value)
353{
354 auto retVal = osstatus::Status::operatingSystemState(value);
355 serialize();
356 return retVal;
357}
358
Andrew Geissleref3c1842016-12-01 12:33:09 -0600359Host::HostState Host::currentHostState(HostState value)
360{
Andrew Geissler1e3bf942016-12-13 15:32:22 -0600361 log<level::INFO>("Change to Host State",
362 entry("CURRENT_HOST_STATE=%s",
363 convertForMessage(value).c_str()));
Andrew Geissleref621162016-12-08 12:56:21 -0600364 return server::Host::currentHostState(value);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600365}
366
Andrew Geissler36529022016-11-29 15:23:54 -0600367} // namespace manager
368} // namespace state
369} // namepsace phosphor