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