Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 1 | #include "bmc_epoch.hpp" |
| 2 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 3 | #include "utils.hpp" |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 4 | |
| 5 | #include <sys/timerfd.h> |
| 6 | #include <unistd.h> |
| 7 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/elog.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
| 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| 12 | #include <xyz/openbmc_project/Time/error.hpp> |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 13 | |
Gunnar Mills | 75710b6 | 2018-04-08 14:50:11 -0500 | [diff] [blame] | 14 | // Need to do this since its not exported outside of the kernel. |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 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) |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace time |
| 26 | { |
| 27 | namespace server = sdbusplus::xyz::openbmc_project::Time::server; |
| 28 | using namespace phosphor::logging; |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 29 | |
| 30 | using NotAllowedError = |
| 31 | sdbusplus::xyz::openbmc_project::Time::Error::NotAllowed; |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 32 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 33 | BmcEpoch::BmcEpoch(sdbusplus::bus::bus& bus, const char* objPath) : |
| 34 | EpochBase(bus, objPath), bus(bus) |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 35 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 36 | initialize(); |
| 37 | } |
| 38 | |
| 39 | void BmcEpoch::initialize() |
| 40 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 41 | using InternalFailure = |
| 42 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 43 | |
| 44 | // Subscribe time change event |
| 45 | // Choose the MAX time that is possible to avoid mis fires. |
| 46 | constexpr itimerspec maxTime = { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 47 | {0, 0}, // it_interval |
| 48 | {TIME_T_MAX, 0}, // it_value |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | timeFd = timerfd_create(CLOCK_REALTIME, 0); |
| 52 | if (timeFd == -1) |
| 53 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 54 | log<level::ERR>("Failed to create timerfd", entry("ERRNO=%d", errno), |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 55 | entry("ERR=%s", strerror(errno))); |
| 56 | elog<InternalFailure>(); |
| 57 | } |
| 58 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 59 | auto r = timerfd_settime( |
| 60 | timeFd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &maxTime, nullptr); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 61 | if (r != 0) |
| 62 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 63 | log<level::ERR>("Failed to set timerfd", entry("ERRNO=%d", errno), |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 64 | entry("ERR=%s", strerror(errno))); |
| 65 | elog<InternalFailure>(); |
| 66 | } |
| 67 | |
| 68 | sd_event_source* es; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 69 | r = sd_event_add_io(bus.get_event(), &es, timeFd, EPOLLIN, onTimeChange, |
| 70 | this); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 71 | if (r < 0) |
| 72 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 73 | log<level::ERR>("Failed to add event", entry("ERRNO=%d", -r), |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 74 | entry("ERR=%s", strerror(-r))); |
| 75 | elog<InternalFailure>(); |
| 76 | } |
| 77 | timeChangeEventSource.reset(es); |
| 78 | } |
| 79 | |
| 80 | BmcEpoch::~BmcEpoch() |
| 81 | { |
| 82 | close(timeFd); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | uint64_t BmcEpoch::elapsed() const |
| 86 | { |
| 87 | // It does not needs to check owner when getting time |
| 88 | return getTime().count(); |
| 89 | } |
| 90 | |
| 91 | uint64_t BmcEpoch::elapsed(uint64_t value) |
| 92 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 93 | /* |
| 94 | Mode | Owner | Set BMC Time |
| 95 | ----- | ----- | ------------- |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 96 | NTP | BMC | Fail to set |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 97 | NTP | HOST | Not allowed |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 98 | NTP | SPLIT | Fail to set |
| 99 | NTP | BOTH | Fail to set |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 100 | MANUAL| BMC | OK |
| 101 | MANUAL| HOST | Not allowed |
| 102 | MANUAL| SPLIT | OK |
| 103 | MANUAL| BOTH | OK |
| 104 | */ |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 105 | if (timeOwner == Owner::Host) |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 106 | { |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 107 | using namespace xyz::openbmc_project::Time; |
| 108 | elog<NotAllowedError>( |
| 109 | NotAllowed::OWNER(utils::ownerToStr(timeOwner).c_str()), |
| 110 | NotAllowed::SYNC_METHOD(utils::modeToStr(timeMode).c_str()), |
| 111 | NotAllowed::REASON( |
| 112 | "Setting BmcTime with HOST owner is not allowed")); |
Lei YU | e7abcdc | 2017-01-16 15:05:24 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 115 | auto time = microseconds(value); |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 116 | if (setTime(time)) |
| 117 | { |
| 118 | notifyBmcTimeChange(time); |
| 119 | } |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 120 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 121 | server::EpochTime::elapsed(value); |
| 122 | return value; |
| 123 | } |
| 124 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 125 | void BmcEpoch::setBmcTimeChangeListener(BmcTimeChangeListener* listener) |
| 126 | { |
| 127 | timeChangeListener = listener; |
| 128 | } |
| 129 | |
| 130 | void BmcEpoch::notifyBmcTimeChange(const microseconds& time) |
| 131 | { |
| 132 | // Notify listener if it exists |
| 133 | if (timeChangeListener) |
| 134 | { |
| 135 | timeChangeListener->onBmcTimeChanged(time); |
| 136 | } |
| 137 | } |
| 138 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 139 | int BmcEpoch::onTimeChange(sd_event_source* es, int fd, uint32_t /* revents */, |
| 140 | void* userdata) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 141 | { |
| 142 | auto bmcEpoch = static_cast<BmcEpoch*>(userdata); |
| 143 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 144 | std::array<char, 64> time{}; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 145 | |
| 146 | // We are not interested in the data here. |
| 147 | // So read until there is no new data here in the FD |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame^] | 148 | while (read(fd, time.data(), time.max_size()) > 0) |
| 149 | ; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 150 | |
| 151 | log<level::INFO>("BMC system time is changed"); |
| 152 | bmcEpoch->notifyBmcTimeChange(bmcEpoch->getTime()); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 157 | } // namespace time |
| 158 | } // namespace phosphor |