Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 1 | #include "scheduled_host_transition.hpp" |
| 2 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <phosphor-logging/elog.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <xyz/openbmc_project/ScheduledTime/error.hpp> |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 7 | #include <cereal/archives/json.hpp> |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 8 | #include <chrono> |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 9 | #include <filesystem> |
| 10 | #include <fstream> |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 11 | #include <sys/timerfd.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | // Need to do this since its not exported outside of the kernel. |
| 15 | // Refer : https://gist.github.com/lethean/446cea944b7441228298 |
| 16 | #ifndef TFD_TIMER_CANCEL_ON_SET |
| 17 | #define TFD_TIMER_CANCEL_ON_SET (1 << 1) |
| 18 | #endif |
| 19 | |
| 20 | // Needed to make sure timerfd does not misfire even though we set CANCEL_ON_SET |
| 21 | #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 22 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace state |
| 26 | { |
| 27 | namespace manager |
| 28 | { |
| 29 | |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 30 | namespace fs = std::filesystem; |
| 31 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 32 | using namespace std::chrono; |
| 33 | using namespace phosphor::logging; |
| 34 | using namespace xyz::openbmc_project::ScheduledTime; |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 35 | using sdbusplus::exception::SdBusError; |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 36 | using InvalidTimeError = |
| 37 | sdbusplus::xyz::openbmc_project::ScheduledTime::Error::InvalidTime; |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 38 | using HostTransition = |
| 39 | sdbusplus::xyz::openbmc_project::State::server::ScheduledHostTransition; |
| 40 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 41 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 42 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 43 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 44 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 45 | constexpr auto PROPERTY_TRANSITION = "RequestedHostTransition"; |
| 46 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 47 | uint64_t ScheduledHostTransition::scheduledTime(uint64_t value) |
| 48 | { |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 49 | if (value == 0) |
| 50 | { |
| 51 | // 0 means the function Scheduled Host Transition is disabled |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 52 | // Stop the timer if it's running |
| 53 | if (timer.isEnabled()) |
| 54 | { |
| 55 | timer.setEnabled(false); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 56 | log<level::INFO>("scheduledTime: The function Scheduled Host " |
| 57 | "Transition is disabled."); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 58 | } |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 59 | } |
| 60 | else |
| 61 | { |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 62 | auto deltaTime = seconds(value) - getTime(); |
| 63 | if (deltaTime < seconds(0)) |
| 64 | { |
| 65 | log<level::ERR>( |
| 66 | "Scheduled time is earlier than current time. Fail to " |
| 67 | "schedule host transition."); |
| 68 | elog<InvalidTimeError>( |
| 69 | InvalidTime::REASON("Scheduled time is in the past")); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | // Start a timer to do host transition at scheduled time |
| 74 | timer.restart(deltaTime); |
| 75 | } |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 76 | } |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 77 | |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 78 | // Set scheduledTime |
| 79 | HostTransition::scheduledTime(value); |
| 80 | // Store scheduled values |
| 81 | serializeScheduledValues(); |
| 82 | |
| 83 | return value; |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 86 | seconds ScheduledHostTransition::getTime() |
| 87 | { |
| 88 | auto now = system_clock::now(); |
| 89 | return duration_cast<seconds>(now.time_since_epoch()); |
| 90 | } |
| 91 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 92 | std::string getService(sdbusplus::bus::bus& bus, std::string path, |
| 93 | std::string interface) |
| 94 | { |
| 95 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 96 | MAPPER_INTERFACE, "GetObject"); |
| 97 | |
| 98 | mapper.append(path, std::vector<std::string>({interface})); |
| 99 | |
| 100 | std::vector<std::pair<std::string, std::vector<std::string>>> |
| 101 | mapperResponse; |
| 102 | |
| 103 | try |
| 104 | { |
| 105 | auto mapperResponseMsg = bus.call(mapper); |
| 106 | |
| 107 | mapperResponseMsg.read(mapperResponse); |
| 108 | if (mapperResponse.empty()) |
| 109 | { |
| 110 | log<level::ERR>("Error no matching service", |
| 111 | entry("PATH=%s", path.c_str()), |
| 112 | entry("INTERFACE=%s", interface.c_str())); |
| 113 | throw std::runtime_error("Error no matching service"); |
| 114 | } |
| 115 | } |
| 116 | catch (const SdBusError& e) |
| 117 | { |
| 118 | log<level::ERR>("Error in mapper call", entry("ERROR=%s", e.what()), |
| 119 | entry("PATH=%s", path.c_str()), |
| 120 | entry("INTERFACE=%s", interface.c_str())); |
| 121 | throw; |
| 122 | } |
| 123 | |
| 124 | return mapperResponse.begin()->first; |
| 125 | } |
| 126 | |
| 127 | void setProperty(sdbusplus::bus::bus& bus, const std::string& path, |
| 128 | const std::string& interface, const std::string& property, |
| 129 | const std::string& value) |
| 130 | { |
| 131 | sdbusplus::message::variant<std::string> variantValue = value; |
| 132 | std::string service = getService(bus, path, interface); |
| 133 | |
| 134 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 135 | PROPERTY_INTERFACE, "Set"); |
| 136 | |
| 137 | method.append(interface, property, variantValue); |
| 138 | bus.call_noreply(method); |
| 139 | |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | void ScheduledHostTransition::hostTransition() |
| 144 | { |
| 145 | auto hostPath = std::string{HOST_OBJPATH} + '0'; |
| 146 | |
| 147 | auto reqTrans = convertForMessage(HostTransition::scheduledTransition()); |
| 148 | |
| 149 | setProperty(bus, hostPath, HOST_BUSNAME, PROPERTY_TRANSITION, reqTrans); |
| 150 | |
| 151 | log<level::INFO>("Set requestedTransition", |
| 152 | entry("REQUESTED_TRANSITION=%s", reqTrans.c_str())); |
| 153 | } |
| 154 | |
| 155 | void ScheduledHostTransition::callback() |
| 156 | { |
| 157 | // Stop timer, since we need to do host transition once only |
| 158 | timer.setEnabled(false); |
| 159 | hostTransition(); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 160 | // Set scheduledTime to 0 to disable host transition and update scheduled |
| 161 | // values |
| 162 | scheduledTime(0); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 165 | void ScheduledHostTransition::initialize() |
| 166 | { |
| 167 | // Subscribe time change event |
| 168 | // Choose the MAX time that is possible to avoid mis fires. |
| 169 | constexpr itimerspec maxTime = { |
| 170 | {0, 0}, // it_interval |
| 171 | {TIME_T_MAX, 0}, // it_value |
| 172 | }; |
| 173 | |
| 174 | // Create and operate on a timer that delivers timer expiration |
| 175 | // notifications via a file descriptor. |
| 176 | timeFd = timerfd_create(CLOCK_REALTIME, 0); |
| 177 | if (timeFd == -1) |
| 178 | { |
| 179 | auto eno = errno; |
| 180 | log<level::ERR>("Failed to create timerfd", entry("ERRNO=%d", eno), |
| 181 | entry("RC=%d", timeFd)); |
| 182 | throw std::system_error(eno, std::system_category()); |
| 183 | } |
| 184 | |
| 185 | // Starts the timer referred to by the file descriptor fd. |
| 186 | // If TFD_TIMER_CANCEL_ON_SET is specified along with TFD_TIMER_ABSTIME |
| 187 | // and the clock for this timer is CLOCK_REALTIME, then mark this timer |
| 188 | // as cancelable if the real-time clock undergoes a discontinuous change. |
| 189 | // In this way, we can monitor whether BMC time is changed or not. |
| 190 | auto r = timerfd_settime( |
| 191 | timeFd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &maxTime, nullptr); |
| 192 | if (r != 0) |
| 193 | { |
| 194 | auto eno = errno; |
| 195 | log<level::ERR>("Failed to set timerfd", entry("ERRNO=%d", eno), |
| 196 | entry("RC=%d", r)); |
| 197 | throw std::system_error(eno, std::system_category()); |
| 198 | } |
| 199 | |
| 200 | sd_event_source* es; |
| 201 | // Add a new I/O event source to an event loop. onTimeChange will be called |
| 202 | // when the event source is triggered. |
| 203 | r = sd_event_add_io(event.get(), &es, timeFd, EPOLLIN, onTimeChange, this); |
| 204 | if (r < 0) |
| 205 | { |
| 206 | auto eno = errno; |
| 207 | log<level::ERR>("Failed to add event", entry("ERRNO=%d", eno), |
| 208 | entry("RC=%d", r)); |
| 209 | throw std::system_error(eno, std::system_category()); |
| 210 | } |
| 211 | timeChangeEventSource.reset(es); |
| 212 | } |
| 213 | |
| 214 | ScheduledHostTransition::~ScheduledHostTransition() |
| 215 | { |
| 216 | close(timeFd); |
| 217 | } |
| 218 | |
| 219 | void ScheduledHostTransition::handleTimeUpdates() |
| 220 | { |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 221 | // Stop the timer if it's running. |
| 222 | // Don't return directly when timer is stopped, because the timer is always |
| 223 | // disabled after the BMC is rebooted. |
| 224 | if (timer.isEnabled()) |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 225 | { |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 226 | timer.setEnabled(false); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 227 | } |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 228 | |
| 229 | // Get scheduled time |
| 230 | auto schedTime = HostTransition::scheduledTime(); |
| 231 | |
| 232 | if (schedTime == 0) |
| 233 | { |
| 234 | log<level::INFO>("handleTimeUpdates: The function Scheduled Host " |
| 235 | "Transition is disabled."); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | auto deltaTime = seconds(schedTime) - getTime(); |
| 240 | if (deltaTime <= seconds(0)) |
| 241 | { |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 242 | hostTransition(); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 243 | // Set scheduledTime to 0 to disable host transition and update |
| 244 | // scheduled values |
| 245 | scheduledTime(0); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 246 | } |
| 247 | else |
| 248 | { |
| 249 | // Start a timer to do host transition at scheduled time |
| 250 | timer.restart(deltaTime); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | int ScheduledHostTransition::onTimeChange(sd_event_source* /* es */, int fd, |
| 255 | uint32_t /* revents */, |
| 256 | void* userdata) |
| 257 | { |
| 258 | auto schedHostTran = static_cast<ScheduledHostTransition*>(userdata); |
| 259 | |
| 260 | std::array<char, 64> time{}; |
| 261 | |
| 262 | // We are not interested in the data here. |
| 263 | // So read until there is no new data here in the FD |
| 264 | while (read(fd, time.data(), time.max_size()) > 0) |
| 265 | ; |
| 266 | |
| 267 | log<level::INFO>("BMC system time is changed"); |
| 268 | schedHostTran->handleTimeUpdates(); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame^] | 273 | void ScheduledHostTransition::serializeScheduledValues() |
| 274 | { |
| 275 | fs::path path{SCHEDULED_HOST_TRANSITION_PERSIST_PATH}; |
| 276 | std::ofstream os(path.c_str(), std::ios::binary); |
| 277 | cereal::JSONOutputArchive oarchive(os); |
| 278 | |
| 279 | oarchive(HostTransition::scheduledTime(), |
| 280 | HostTransition::scheduledTransition()); |
| 281 | } |
| 282 | |
| 283 | bool ScheduledHostTransition::deserializeScheduledValues(uint64_t& time, |
| 284 | Transition& trans) |
| 285 | { |
| 286 | fs::path path{SCHEDULED_HOST_TRANSITION_PERSIST_PATH}; |
| 287 | |
| 288 | try |
| 289 | { |
| 290 | if (fs::exists(path)) |
| 291 | { |
| 292 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 293 | cereal::JSONInputArchive iarchive(is); |
| 294 | iarchive(time, trans); |
| 295 | return true; |
| 296 | } |
| 297 | } |
| 298 | catch (std::exception& e) |
| 299 | { |
| 300 | log<level::ERR>(e.what()); |
| 301 | fs::remove(path); |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | void ScheduledHostTransition::restoreScheduledValues() |
| 308 | { |
| 309 | uint64_t time; |
| 310 | Transition trans; |
| 311 | if (!deserializeScheduledValues(time, trans)) |
| 312 | { |
| 313 | // set to default value |
| 314 | HostTransition::scheduledTime(0); |
| 315 | HostTransition::scheduledTransition(Transition::On); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | HostTransition::scheduledTime(time); |
| 320 | HostTransition::scheduledTransition(trans); |
| 321 | // Rebooting BMC is something like the BMC time is changed, |
| 322 | // so go on with the same process as BMC time changed. |
| 323 | handleTimeUpdates(); |
| 324 | } |
| 325 | } |
| 326 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 327 | } // namespace manager |
| 328 | } // namespace state |
| 329 | } // namespace phosphor |