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