Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 1 | #include <iostream> |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 2 | #include <map> |
| 3 | #include <string> |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 4 | #include <systemd/sd-bus.h> |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 5 | #include <sdbusplus/server.hpp> |
Saqib Khan | a8006a2 | 2017-02-14 11:37:08 -0600 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 7 | #include "host_state_manager.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace state |
| 12 | { |
| 13 | namespace manager |
| 14 | { |
| 15 | |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 16 | // When you see server:: you know we're referencing our base class |
| 17 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 18 | |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 19 | using namespace phosphor::logging; |
| 20 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 21 | constexpr auto HOST_STATE_POWEROFF_TGT = "obmc-chassis-stop@0.target"; |
| 22 | constexpr auto HOST_STATE_POWERON_TGT = "obmc-chassis-start@0.target"; |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 23 | constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-quiesce-host@0.target"; |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 24 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 25 | /* Map a transition to it's systemd target */ |
| 26 | const std::map<server::Host::Transition,std::string> SYSTEMD_TARGET_TABLE = |
| 27 | { |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 28 | {server::Host::Transition::Off, HOST_STATE_POWEROFF_TGT}, |
| 29 | {server::Host::Transition::On, HOST_STATE_POWERON_TGT} |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 33 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 34 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 35 | |
| 36 | constexpr auto SYSTEM_SERVICE = "org.openbmc.managers.System"; |
| 37 | constexpr auto SYSTEM_OBJ_PATH = "/org/openbmc/managers/System"; |
| 38 | constexpr auto SYSTEM_INTERFACE = SYSTEM_SERVICE; |
| 39 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 40 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
Leonel Gonzalez | f318d87 | 2017-03-16 13:47:49 -0500 | [diff] [blame^] | 41 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 42 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 43 | |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 44 | constexpr auto REBOOTCOUNTER_SERVICE("org.openbmc.Sensors"); |
| 45 | constexpr auto REBOOTCOUNTER_PATH("/org/openbmc/sensors/host/BootCount"); |
| 46 | constexpr auto REBOOTCOUNTER_INTERFACE("org.openbmc.SensorValue"); |
| 47 | |
| 48 | const sdbusplus::message::variant<int> DEFAULT_BOOTCOUNT = 2; |
| 49 | |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 50 | /* Map a system state to the HostState */ |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 51 | const std::map<std::string, server::Host::HostState> SYS_HOST_STATE_TABLE = { |
| 52 | {"HOST_BOOTING", server::Host::HostState::Running}, |
Saqib Khan | adaa721 | 2017-02-23 13:19:28 -0600 | [diff] [blame] | 53 | {"HOST_POWERED_OFF", server::Host::HostState::Off}, |
| 54 | {"HOST_QUIESCED", server::Host::HostState::Quiesced} |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 55 | }; |
| 56 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 57 | void Host::subscribeToSystemdSignals() |
| 58 | { |
| 59 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 60 | SYSTEMD_OBJ_PATH, |
| 61 | SYSTEMD_INTERFACE, |
| 62 | "Subscribe"); |
| 63 | this->bus.call_noreply(method); |
| 64 | |
| 65 | return; |
| 66 | } |
| 67 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 68 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 69 | // and persistent storage design is in place |
| 70 | void Host::determineInitialState() |
| 71 | { |
| 72 | std::string sysState; |
| 73 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 74 | auto method = this->bus.new_method_call(SYSTEM_SERVICE, |
| 75 | SYSTEM_OBJ_PATH, |
| 76 | SYSTEM_INTERFACE, |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 77 | "getSystemState"); |
| 78 | |
| 79 | auto reply = this->bus.call(method); |
| 80 | |
| 81 | reply.read(sysState); |
| 82 | |
| 83 | if(sysState == "HOST_BOOTED") |
| 84 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 85 | log<level::INFO>("Initial Host State will be Running", |
| 86 | entry("CURRENT_HOST_STATE=%s", |
| 87 | convertForMessage(HostState::Running).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 88 | server::Host::currentHostState(HostState::Running); |
| 89 | server::Host::requestedHostTransition(Transition::On); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 90 | } |
| 91 | else |
| 92 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 93 | log<level::INFO>("Initial Host State will be Off", |
| 94 | entry("CURRENT_HOST_STATE=%s", |
| 95 | convertForMessage(HostState::Off).c_str())); |
Andrew Geissler | 9792414 | 2017-01-24 16:10:18 -0600 | [diff] [blame] | 96 | server::Host::currentHostState(HostState::Off); |
| 97 | server::Host::requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Set transition initially to Off |
| 101 | // TODO - Eventually need to restore this from persistent storage |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 102 | server::Host::requestedHostTransition(Transition::Off); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 103 | |
| 104 | return; |
| 105 | } |
| 106 | |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 107 | void Host::executeTransition(Transition tranReq) |
| 108 | { |
| 109 | auto sysdUnit = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 110 | |
| 111 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, |
| 112 | SYSTEMD_OBJ_PATH, |
| 113 | SYSTEMD_INTERFACE, |
| 114 | "StartUnit"); |
| 115 | |
| 116 | method.append(sysdUnit); |
| 117 | method.append("replace"); |
| 118 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 119 | this->bus.call_noreply(method); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 120 | |
| 121 | return; |
| 122 | } |
| 123 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 124 | bool Host::isAutoReboot() |
| 125 | { |
| 126 | sdbusplus::message::variant<std::string> autoRebootParam; |
| 127 | std::string strParam; |
| 128 | |
| 129 | std::string HOST_PATH("/org/openbmc/settings/host0"); |
| 130 | std::string HOST_INTERFACE("org.openbmc.settings.Host"); |
| 131 | |
| 132 | auto mapper = this->bus.new_method_call(MAPPER_BUSNAME, |
| 133 | MAPPER_PATH, |
| 134 | MAPPER_INTERFACE, |
| 135 | "GetObject"); |
| 136 | |
| 137 | mapper.append(HOST_PATH, std::vector<std::string>({HOST_INTERFACE})); |
| 138 | auto mapperResponseMsg = this->bus.call(mapper); |
| 139 | |
| 140 | if (mapperResponseMsg.is_method_error()) |
| 141 | { |
| 142 | log<level::ERR>("Error in mapper call"); |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 147 | mapperResponseMsg.read(mapperResponse); |
| 148 | if (mapperResponse.empty()) |
| 149 | { |
| 150 | log<level::ERR>("Error reading mapper response"); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | const auto& host = mapperResponse.begin()->first; |
| 155 | |
| 156 | auto method = this->bus.new_method_call(host.c_str(), |
| 157 | HOST_PATH.c_str(), |
| 158 | "org.freedesktop.DBus.Properties", |
| 159 | "Get"); |
| 160 | |
| 161 | method.append(HOST_INTERFACE.c_str(), "auto_reboot"); |
| 162 | auto reply = this->bus.call(method); |
| 163 | |
| 164 | if (reply.is_method_error()) |
| 165 | { |
| 166 | log<level::ERR>("Error in auto_reboot Get"); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | reply.read(autoRebootParam); |
| 171 | strParam = |
| 172 | sdbusplus::message::variant_ns::get<std::string>(autoRebootParam); |
| 173 | |
| 174 | if (strParam.empty()) |
| 175 | { |
| 176 | log<level::ERR>("Error reading auto_reboot response"); |
| 177 | return false; |
| 178 | } |
| 179 | |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 180 | sdbusplus::message::variant<int> rebootCounterParam; |
| 181 | method = this->bus.new_method_call(REBOOTCOUNTER_SERVICE, |
| 182 | REBOOTCOUNTER_PATH, |
| 183 | REBOOTCOUNTER_INTERFACE, |
| 184 | "getValue"); |
| 185 | reply = this->bus.call(method); |
| 186 | if (reply.is_method_error()) |
| 187 | { |
| 188 | log<level::ERR>("Error in BOOTCOUNT getValue"); |
| 189 | return false; |
| 190 | } |
| 191 | reply.read(rebootCounterParam); |
| 192 | |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 193 | if (strParam == "yes") |
| 194 | { |
Saqib Khan | cbe08d1 | 2017-03-10 01:29:20 -0600 | [diff] [blame] | 195 | method = this->bus.new_method_call(REBOOTCOUNTER_SERVICE, |
| 196 | REBOOTCOUNTER_PATH, |
| 197 | REBOOTCOUNTER_INTERFACE, |
| 198 | "setValue"); |
| 199 | if( rebootCounterParam > 0) |
| 200 | { |
| 201 | // Reduce BOOTCOUNT by 1 |
| 202 | method.append((sdbusplus::message::variant_ns:: |
| 203 | get<int>(rebootCounterParam)) - 1); |
| 204 | this->bus.call_noreply(method); |
| 205 | return true; |
| 206 | } |
| 207 | if(rebootCounterParam == 0) |
| 208 | { |
| 209 | // Reset reboot counter and go to quiesce state |
| 210 | method.append(DEFAULT_BOOTCOUNT); |
| 211 | this->bus.call_noreply(method); |
| 212 | return false; |
| 213 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 219 | int Host::sysStateChangeSignal(sd_bus_message *msg, void *userData, |
| 220 | sd_bus_error *retError) |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 221 | { |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 222 | return static_cast<Host*>(userData)->sysStateChange(msg, retError); |
| 223 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 224 | |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 225 | int Host::sysStateChange(sd_bus_message* msg, |
| 226 | sd_bus_error* retError) |
| 227 | { |
| 228 | uint32_t newStateID {}; |
| 229 | sdbusplus::message::object_path newStateObjPath; |
| 230 | std::string newStateUnit{}; |
| 231 | std::string newStateResult{}; |
| 232 | |
| 233 | auto sdPlusMsg = sdbusplus::message::message(msg); |
| 234 | //Read the msg and populate each variable |
| 235 | sdPlusMsg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 236 | |
| 237 | if((newStateUnit == HOST_STATE_POWEROFF_TGT) && |
| 238 | (newStateResult == "done")) |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 239 | { |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 240 | log<level::INFO>("Recieved signal that host is off"); |
| 241 | this->currentHostState(server::Host::HostState::Off); |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 242 | |
| 243 | // Check if we need to start a new transition (i.e. a Reboot) |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 244 | if(this->server::Host::requestedHostTransition() == |
| 245 | Transition::Reboot) |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 246 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 247 | log<level::DEBUG>("Reached intermediate state, going to next"); |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 248 | this->executeTransition(server::Host::Transition::On); |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 249 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 250 | } |
Andrew Geissler | 4da7e00 | 2017-01-24 15:21:40 -0600 | [diff] [blame] | 251 | else if((newStateUnit == HOST_STATE_POWERON_TGT) && |
| 252 | (newStateResult == "done")) |
| 253 | { |
| 254 | log<level::INFO>("Recieved signal that host is running"); |
| 255 | this->currentHostState(server::Host::HostState::Running); |
| 256 | } |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 257 | else if((newStateUnit == HOST_STATE_QUIESCE_TGT) && |
| 258 | (newStateResult == "done")) |
| 259 | { |
| 260 | if (Host::isAutoReboot()) |
| 261 | { |
| 262 | log<level::INFO>("Auto reboot enabled. Beginning reboot..."); |
| 263 | Host::requestedHostTransition(server::Host::Transition::Reboot); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | log<level::INFO>("Auto reboot disabled. Maintaining quiesce."); |
Saqib Khan | adaa721 | 2017-02-23 13:19:28 -0600 | [diff] [blame] | 268 | this->currentHostState(server::Host::HostState::Quiesced); |
Michael Tritz | 206a833 | 2017-02-06 16:01:23 -0600 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | } |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 276 | Host::Transition Host::requestedHostTransition(Transition value) |
| 277 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 278 | log<level::INFO>( |
| 279 | "Host State transaction request", |
| 280 | entry("REQUESTED_HOST_TRANSITION=%s", |
| 281 | convertForMessage(value).c_str())); |
Andrew Geissler | 0cd2eaf | 2016-12-07 10:50:13 -0600 | [diff] [blame] | 282 | |
Andrew Geissler | 06dbc5b | 2016-12-13 11:46:16 -0600 | [diff] [blame] | 283 | Transition tranReq = value; |
| 284 | if(value == server::Host::Transition::Reboot) |
| 285 | { |
| 286 | // On reboot requests we just need to do a off if we're on and |
| 287 | // vice versa. The handleSysStateChange() code above handles the |
| 288 | // second part of the reboot |
| 289 | if(this->server::Host::currentHostState() == |
| 290 | server::Host::HostState::Off) |
| 291 | { |
| 292 | tranReq = server::Host::Transition::On; |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | tranReq = server::Host::Transition::Off; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | executeTransition(tranReq); |
Andrew Geissler | 3e3b84b | 2016-12-02 15:46:17 -0600 | [diff] [blame] | 301 | return server::Host::requestedHostTransition(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 302 | } |
| 303 | |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 304 | Host::HostState Host::currentHostState(HostState value) |
| 305 | { |
Andrew Geissler | 1e3bf94 | 2016-12-13 15:32:22 -0600 | [diff] [blame] | 306 | log<level::INFO>("Change to Host State", |
| 307 | entry("CURRENT_HOST_STATE=%s", |
| 308 | convertForMessage(value).c_str())); |
Andrew Geissler | ef62116 | 2016-12-08 12:56:21 -0600 | [diff] [blame] | 309 | return server::Host::currentHostState(value); |
Andrew Geissler | ef3c184 | 2016-12-01 12:33:09 -0600 | [diff] [blame] | 310 | } |
| 311 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 312 | } // namespace manager |
| 313 | } // namespace state |
| 314 | } // namepsace phosphor |