Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 1 | #include "scheduled_host_transition.hpp" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 3 | #include "utils.hpp" |
Andrew Geissler | 68a8c31 | 2021-12-03 14:26:23 -0600 | [diff] [blame] | 4 | #include "xyz/openbmc_project/State/Host/server.hpp" |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 5 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 6 | #include <sys/timerfd.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <cereal/archives/json.hpp> |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
| 11 | #include <phosphor-logging/elog.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 13 | #include <xyz/openbmc_project/ScheduledTime/error.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 14 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 15 | #include <chrono> |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 16 | #include <filesystem> |
| 17 | #include <fstream> |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 18 | |
| 19 | // Need to do this since its not exported outside of the kernel. |
| 20 | // Refer : https://gist.github.com/lethean/446cea944b7441228298 |
| 21 | #ifndef TFD_TIMER_CANCEL_ON_SET |
| 22 | #define TFD_TIMER_CANCEL_ON_SET (1 << 1) |
| 23 | #endif |
| 24 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 25 | namespace phosphor |
| 26 | { |
| 27 | namespace state |
| 28 | { |
| 29 | namespace manager |
| 30 | { |
| 31 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 32 | PHOSPHOR_LOG2_USING; |
| 33 | |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 34 | namespace fs = std::filesystem; |
| 35 | |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 36 | using namespace std::chrono; |
| 37 | using namespace phosphor::logging; |
| 38 | using namespace xyz::openbmc_project::ScheduledTime; |
| 39 | using InvalidTimeError = |
| 40 | sdbusplus::xyz::openbmc_project::ScheduledTime::Error::InvalidTime; |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 41 | using HostTransition = |
| 42 | sdbusplus::xyz::openbmc_project::State::server::ScheduledHostTransition; |
Andrew Geissler | 68a8c31 | 2021-12-03 14:26:23 -0600 | [diff] [blame] | 43 | using HostState = sdbusplus::xyz::openbmc_project::State::server::Host; |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 44 | |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 45 | constexpr auto PROPERTY_TRANSITION = "RequestedHostTransition"; |
Andrew Geissler | 68a8c31 | 2021-12-03 14:26:23 -0600 | [diff] [blame] | 46 | constexpr auto PROPERTY_RESTART_CAUSE = "RestartCause"; |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 47 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 48 | uint64_t ScheduledHostTransition::scheduledTime(uint64_t value) |
| 49 | { |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 50 | if (value == 0) |
| 51 | { |
| 52 | // 0 means the function Scheduled Host Transition is disabled |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 53 | // Stop the timer if it's running |
| 54 | if (timer.isEnabled()) |
| 55 | { |
| 56 | timer.setEnabled(false); |
Andrew Geissler | ad65b2d | 2021-09-21 12:53:29 -0500 | [diff] [blame] | 57 | debug( |
| 58 | "scheduledTime: The function Scheduled Host Transition is disabled."); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 59 | } |
Carol Wang | 4ca6f3f | 2020-02-19 16:28:59 +0800 | [diff] [blame] | 60 | } |
| 61 | else |
| 62 | { |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 63 | auto deltaTime = seconds(value) - getTime(); |
| 64 | if (deltaTime < seconds(0)) |
| 65 | { |
Andrew Geissler | ad65b2d | 2021-09-21 12:53:29 -0500 | [diff] [blame] | 66 | error( |
| 67 | "Scheduled time is earlier than current time. Fail to schedule host transition."); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 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 | void ScheduledHostTransition::hostTransition() |
| 93 | { |
Patrick Williams | 211d972 | 2022-04-07 21:24:33 -0500 | [diff] [blame] | 94 | auto hostPath = std::string{HOST_OBJPATH} + std::to_string(id); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 95 | |
Andrew Geissler | 68a8c31 | 2021-12-03 14:26:23 -0600 | [diff] [blame] | 96 | // Set RestartCause to indicate this transition is occurring due to a |
| 97 | // scheduled host transition as long as it's not an off request |
| 98 | if (HostTransition::scheduledTransition() != HostState::Transition::Off) |
| 99 | { |
| 100 | info("Set RestartCause to scheduled power on reason"); |
| 101 | auto resCause = |
| 102 | convertForMessage(HostState::RestartCause::ScheduledPowerOn); |
| 103 | utils::setProperty(bus, hostPath, HOST_BUSNAME, PROPERTY_RESTART_CAUSE, |
| 104 | resCause); |
| 105 | } |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 106 | auto reqTrans = convertForMessage(HostTransition::scheduledTransition()); |
| 107 | |
Carol Wang | dc05939 | 2020-03-13 17:39:17 +0800 | [diff] [blame] | 108 | utils::setProperty(bus, hostPath, HOST_BUSNAME, PROPERTY_TRANSITION, |
| 109 | reqTrans); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 110 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 111 | info("Set requestedTransition to {REQUESTED_TRANSITION}", |
| 112 | "REQUESTED_TRANSITION", reqTrans); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void ScheduledHostTransition::callback() |
| 116 | { |
| 117 | // Stop timer, since we need to do host transition once only |
| 118 | timer.setEnabled(false); |
| 119 | hostTransition(); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 120 | // Set scheduledTime to 0 to disable host transition and update scheduled |
| 121 | // values |
| 122 | scheduledTime(0); |
Carol Wang | 6a5db3d | 2020-02-21 10:12:01 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 125 | void ScheduledHostTransition::initialize() |
| 126 | { |
| 127 | // Subscribe time change event |
| 128 | // Choose the MAX time that is possible to avoid mis fires. |
| 129 | constexpr itimerspec maxTime = { |
Andrew Geissler | 63f7b1a | 2023-07-24 14:54:58 -0400 | [diff] [blame] | 130 | {0, 0}, // it_interval |
| 131 | {system_clock::duration::max().count(), 0}, // it_value |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | // Create and operate on a timer that delivers timer expiration |
| 135 | // notifications via a file descriptor. |
| 136 | timeFd = timerfd_create(CLOCK_REALTIME, 0); |
| 137 | if (timeFd == -1) |
| 138 | { |
| 139 | auto eno = errno; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 140 | error("Failed to create timerfd, errno: {ERRNO}, rc: {RC}", "ERRNO", |
| 141 | eno, "RC", timeFd); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 142 | throw std::system_error(eno, std::system_category()); |
| 143 | } |
| 144 | |
| 145 | // Starts the timer referred to by the file descriptor fd. |
| 146 | // If TFD_TIMER_CANCEL_ON_SET is specified along with TFD_TIMER_ABSTIME |
| 147 | // and the clock for this timer is CLOCK_REALTIME, then mark this timer |
| 148 | // as cancelable if the real-time clock undergoes a discontinuous change. |
| 149 | // In this way, we can monitor whether BMC time is changed or not. |
| 150 | auto r = timerfd_settime( |
| 151 | timeFd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &maxTime, nullptr); |
| 152 | if (r != 0) |
| 153 | { |
| 154 | auto eno = errno; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 155 | error("Failed to set timerfd, errno: {ERRNO}, rc: {RC}", "ERRNO", eno, |
| 156 | "RC", r); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 157 | throw std::system_error(eno, std::system_category()); |
| 158 | } |
| 159 | |
| 160 | sd_event_source* es; |
| 161 | // Add a new I/O event source to an event loop. onTimeChange will be called |
| 162 | // when the event source is triggered. |
| 163 | r = sd_event_add_io(event.get(), &es, timeFd, EPOLLIN, onTimeChange, this); |
| 164 | if (r < 0) |
| 165 | { |
| 166 | auto eno = errno; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 167 | error("Failed to add event, errno: {ERRNO}, rc: {RC}", "ERRNO", eno, |
| 168 | "RC", r); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 169 | throw std::system_error(eno, std::system_category()); |
| 170 | } |
| 171 | timeChangeEventSource.reset(es); |
| 172 | } |
| 173 | |
| 174 | ScheduledHostTransition::~ScheduledHostTransition() |
| 175 | { |
| 176 | close(timeFd); |
| 177 | } |
| 178 | |
| 179 | void ScheduledHostTransition::handleTimeUpdates() |
| 180 | { |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 181 | // Stop the timer if it's running. |
| 182 | // Don't return directly when timer is stopped, because the timer is always |
| 183 | // disabled after the BMC is rebooted. |
| 184 | if (timer.isEnabled()) |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 185 | { |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 186 | timer.setEnabled(false); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 187 | } |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 188 | |
| 189 | // Get scheduled time |
| 190 | auto schedTime = HostTransition::scheduledTime(); |
| 191 | |
| 192 | if (schedTime == 0) |
| 193 | { |
Andrew Geissler | ad65b2d | 2021-09-21 12:53:29 -0500 | [diff] [blame] | 194 | debug( |
| 195 | "handleTimeUpdates: The function Scheduled Host Transition is disabled."); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | auto deltaTime = seconds(schedTime) - getTime(); |
| 200 | if (deltaTime <= seconds(0)) |
| 201 | { |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 202 | hostTransition(); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 203 | // Set scheduledTime to 0 to disable host transition and update |
| 204 | // scheduled values |
| 205 | scheduledTime(0); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 206 | } |
| 207 | else |
| 208 | { |
| 209 | // Start a timer to do host transition at scheduled time |
| 210 | timer.restart(deltaTime); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | int ScheduledHostTransition::onTimeChange(sd_event_source* /* es */, int fd, |
| 215 | uint32_t /* revents */, |
| 216 | void* userdata) |
| 217 | { |
| 218 | auto schedHostTran = static_cast<ScheduledHostTransition*>(userdata); |
| 219 | |
| 220 | std::array<char, 64> time{}; |
| 221 | |
| 222 | // We are not interested in the data here. |
| 223 | // So read until there is no new data here in the FD |
| 224 | while (read(fd, time.data(), time.max_size()) > 0) |
| 225 | ; |
| 226 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 227 | debug("BMC system time is changed"); |
Carol Wang | ef7abe1 | 2020-02-25 16:19:34 +0800 | [diff] [blame] | 228 | schedHostTran->handleTimeUpdates(); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 233 | void ScheduledHostTransition::serializeScheduledValues() |
| 234 | { |
| 235 | fs::path path{SCHEDULED_HOST_TRANSITION_PERSIST_PATH}; |
| 236 | std::ofstream os(path.c_str(), std::ios::binary); |
| 237 | cereal::JSONOutputArchive oarchive(os); |
| 238 | |
| 239 | oarchive(HostTransition::scheduledTime(), |
| 240 | HostTransition::scheduledTransition()); |
| 241 | } |
| 242 | |
| 243 | bool ScheduledHostTransition::deserializeScheduledValues(uint64_t& time, |
| 244 | Transition& trans) |
| 245 | { |
| 246 | fs::path path{SCHEDULED_HOST_TRANSITION_PERSIST_PATH}; |
| 247 | |
| 248 | try |
| 249 | { |
| 250 | if (fs::exists(path)) |
| 251 | { |
| 252 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 253 | cereal::JSONInputArchive iarchive(is); |
| 254 | iarchive(time, trans); |
| 255 | return true; |
| 256 | } |
| 257 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame] | 258 | catch (const std::exception& e) |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 259 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 260 | error("deserialize exception: {ERROR}", "ERROR", e); |
Carol Wang | 1dbbef4 | 2020-03-09 11:51:23 +0800 | [diff] [blame] | 261 | fs::remove(path); |
| 262 | } |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | void ScheduledHostTransition::restoreScheduledValues() |
| 268 | { |
| 269 | uint64_t time; |
| 270 | Transition trans; |
| 271 | if (!deserializeScheduledValues(time, trans)) |
| 272 | { |
| 273 | // set to default value |
| 274 | HostTransition::scheduledTime(0); |
| 275 | HostTransition::scheduledTransition(Transition::On); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | HostTransition::scheduledTime(time); |
| 280 | HostTransition::scheduledTransition(trans); |
| 281 | // Rebooting BMC is something like the BMC time is changed, |
| 282 | // so go on with the same process as BMC time changed. |
| 283 | handleTimeUpdates(); |
| 284 | } |
| 285 | } |
| 286 | |
Carol Wang | 71230ef | 2020-02-18 17:39:49 +0800 | [diff] [blame] | 287 | } // namespace manager |
| 288 | } // namespace state |
| 289 | } // namespace phosphor |