blob: 8d24d234e12370852823c1fb8ee343f1c4941898 [file] [log] [blame]
Vernon Mauery9e801a22018-10-12 13:20:49 -07001#include "sd_event_loop.hpp"
2
3#include "main.hpp"
4#include "message_handler.hpp"
5
Dave Cobbley2c15f0c2017-11-13 16:19:09 -08006#include <netinet/in.h>
Tom Joseph7fd26dd2017-03-14 15:26:26 +05307#include <sys/ioctl.h>
Dave Cobbley2c15f0c2017-11-13 16:19:09 -08008#include <sys/socket.h>
Tom Joseph7fd26dd2017-03-14 15:26:26 +05309#include <systemd/sd-daemon.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -070010
Vernon Mauerycbccb052018-10-24 13:52:22 -070011#include <boost/asio/io_context.hpp>
Tom Joseph7fd26dd2017-03-14 15:26:26 +053012#include <phosphor-logging/log.hpp>
Vernon Mauerycbccb052018-10-24 13:52:22 -070013#include <sdbusplus/asio/sd_event.hpp>
Tom Joseph7fd26dd2017-03-14 15:26:26 +053014
15namespace eventloop
16{
17using namespace phosphor::logging;
18
Vernon Mauery7a0142c2018-11-09 08:38:16 -080019void EventLoop::handleRmcpPacket()
Tom Joseph7fd26dd2017-03-14 15:26:26 +053020{
Tom Joseph7fd26dd2017-03-14 15:26:26 +053021 try
22 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080023 auto channelPtr = std::make_shared<udpsocket::Channel>(udpSocket);
Tom Joseph7fd26dd2017-03-14 15:26:26 +053024
25 // Initialize the Message Handler with the socket channel
Vernon Mauery7a0142c2018-11-09 08:38:16 -080026 auto msgHandler = std::make_shared<message::Handler>(channelPtr);
Tom Joseph7fd26dd2017-03-14 15:26:26 +053027
Tom Joseph7fd26dd2017-03-14 15:26:26 +053028 // Read the incoming IPMI packet
Vernon Mauery7a0142c2018-11-09 08:38:16 -080029 std::shared_ptr<message::Message> inMessage(msgHandler->receive());
Tom Joseph7fd26dd2017-03-14 15:26:26 +053030 if (inMessage == nullptr)
31 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080032 return;
Tom Joseph7fd26dd2017-03-14 15:26:26 +053033 }
34
35 // Execute the Command
Vernon Mauery7a0142c2018-11-09 08:38:16 -080036 std::shared_ptr<message::Message> outMessage =
37 msgHandler->executeCommand(inMessage);
Tom Joseph7fd26dd2017-03-14 15:26:26 +053038 if (outMessage == nullptr)
39 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080040 return;
Tom Joseph7fd26dd2017-03-14 15:26:26 +053041 }
Tom Joseph7fd26dd2017-03-14 15:26:26 +053042 // Send the response IPMI Message
Vernon Mauery7a0142c2018-11-09 08:38:16 -080043 msgHandler->send(outMessage);
Tom Joseph7fd26dd2017-03-14 15:26:26 +053044 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080045 catch (const std::exception& e)
Tom Joseph7fd26dd2017-03-14 15:26:26 +053046 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080047 log<level::ERR>("Executing the IPMI message failed",
48 entry("EXCEPTION=%s", e.what()));
Tom Joseph7fd26dd2017-03-14 15:26:26 +053049 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080050}
Tom Joseph7fd26dd2017-03-14 15:26:26 +053051
Vernon Mauery7a0142c2018-11-09 08:38:16 -080052void EventLoop::startRmcpReceive()
53{
54 udpSocket->async_wait(boost::asio::socket_base::wait_read,
55 [this](const boost::system::error_code& ec) {
56 if (!ec)
57 {
58 io->post([this]() { startRmcpReceive(); });
59 handleRmcpPacket();
60 }
61 });
Tom Joseph7fd26dd2017-03-14 15:26:26 +053062}
63
Tom Josephe989c362017-03-14 17:16:50 +053064static int charAccTimerHandler(sd_event_source* s, uint64_t usec,
Vernon Mauery9e801a22018-10-12 13:20:49 -070065 void* userdata)
Tom Josephe989c362017-03-14 17:16:50 +053066{
Tom Josephe989c362017-03-14 17:16:50 +053067 auto bufferSize = std::get<sol::Manager&>(singletonPool).dataBuffer.size();
68
69 try
70 {
Patrick Venturea65e30d2018-10-26 17:55:08 -070071 // The instance is hardcoded to 1, in the case of supporting multiple
72 // payload instances we would need to populate it from userdata
73 uint8_t instance = 1;
74
Vernon Mauery9e801a22018-10-12 13:20:49 -070075 if (bufferSize > 0)
Tom Josephe989c362017-03-14 17:16:50 +053076 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070077 auto& context =
78 std::get<sol::Manager&>(singletonPool).getContext(instance);
Tom Joseph73063142017-03-14 18:20:20 +053079
Patrick Venturea65e30d2018-10-26 17:55:08 -070080 int rc = context.sendOutboundPayload();
Tom Joseph73063142017-03-14 18:20:20 +053081
82 if (rc == 0)
83 {
84 return 0;
85 }
Tom Josephe989c362017-03-14 17:16:50 +053086 }
87
Vernon Mauery9e801a22018-10-12 13:20:49 -070088 std::get<eventloop::EventLoop&>(singletonPool)
89 .switchTimer(instance, Timers::ACCUMULATE, true);
Tom Josephe989c362017-03-14 17:16:50 +053090 }
91 catch (std::exception& e)
92 {
93 log<level::ERR>(e.what());
94 }
95
96 return 0;
97}
98
Vernon Mauery9e801a22018-10-12 13:20:49 -070099static int retryTimerHandler(sd_event_source* s, uint64_t usec, void* userdata)
Tom Josephe989c362017-03-14 17:16:50 +0530100{
Tom Josephe989c362017-03-14 17:16:50 +0530101 try
102 {
Patrick Venturea65e30d2018-10-26 17:55:08 -0700103 // The instance is hardcoded to 1, in the case of supporting multiple
104 // payload instances we would need to populate it from userdata
105 uint8_t instance = 1;
106
Vernon Mauery9e801a22018-10-12 13:20:49 -0700107 auto& context =
108 std::get<sol::Manager&>(singletonPool).getContext(instance);
Tom Josephe989c362017-03-14 17:16:50 +0530109
110 if (context.retryCounter)
111 {
112 --context.retryCounter;
Vernon Mauery9e801a22018-10-12 13:20:49 -0700113 std::get<eventloop::EventLoop&>(singletonPool)
114 .switchTimer(instance, Timers::RETRY, true);
Tom Joseph2fd466f2017-03-30 08:16:47 +0530115 context.resendPayload(sol::Context::noClear);
Tom Josephe989c362017-03-14 17:16:50 +0530116 }
117 else
118 {
119 context.retryCounter = context.maxRetryCount;
Tom Joseph2fd466f2017-03-30 08:16:47 +0530120 context.resendPayload(sol::Context::clear);
Vernon Mauery9e801a22018-10-12 13:20:49 -0700121 std::get<eventloop::EventLoop&>(singletonPool)
122 .switchTimer(instance, Timers::RETRY, false);
123 std::get<eventloop::EventLoop&>(singletonPool)
124 .switchTimer(instance, Timers::ACCUMULATE, true);
Tom Josephe989c362017-03-14 17:16:50 +0530125 }
126 }
127 catch (std::exception& e)
128 {
129 log<level::ERR>(e.what());
130 }
131
132 return 0;
133}
134
Vernon Mauerycbccb052018-10-24 13:52:22 -0700135int EventLoop::startEventLoop()
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530136{
Vernon Mauerycbccb052018-10-24 13:52:22 -0700137 sdbusplus::asio::sd_event_wrapper sdEvents(*io);
138 event = sdEvents.get();
Marri Devender Rao3ecf0a12017-07-13 08:07:22 -0500139
Vernon Mauery22c8a212018-10-24 14:51:23 -0700140 // set up boost::asio signal handling
141 boost::asio::signal_set signals(*io, SIGINT, SIGTERM);
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800142 signals.async_wait(
143 [this](const boost::system::error_code& error, int signalNumber) {
144 udpSocket->cancel();
145 udpSocket->close();
146 io->stop();
147 });
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530148
Vernon Mauery9e801a22018-10-12 13:20:49 -0700149 // Create our own socket if SysD did not supply one.
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800150 int listensFdCount = sd_listen_fds(0);
151 if (listensFdCount == 1)
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530152 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800153 if (sd_is_socket(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_DGRAM, -1))
154 {
155 udpSocket = std::make_shared<boost::asio::ip::udp::socket>(
156 *io, boost::asio::ip::udp::v6(), SD_LISTEN_FDS_START);
157 }
Dave Cobbley2c15f0c2017-11-13 16:19:09 -0800158 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800159 else if (listensFdCount > 1)
Dave Cobbley2c15f0c2017-11-13 16:19:09 -0800160 {
161 log<level::ERR>("Too many file descriptors received");
Vernon Mauerycbccb052018-10-24 13:52:22 -0700162 return EXIT_FAILURE;
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530163 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -0800164 if (!udpSocket)
165 {
166 udpSocket = std::make_shared<boost::asio::ip::udp::socket>(
167 *io, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v6(),
168 IPMI_STD_PORT));
169 if (!udpSocket)
170 {
171 log<level::ERR>("Failed to start listening on RMCP socket");
172 return EXIT_FAILURE;
173 }
174 }
175 startRmcpReceive();
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530176
Vernon Mauerycbccb052018-10-24 13:52:22 -0700177 io->run();
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530178
Vernon Mauerycbccb052018-10-24 13:52:22 -0700179 return EXIT_SUCCESS;
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530180}
181
Tom Josephe989c362017-03-14 17:16:50 +0530182void EventLoop::startSOLPayloadInstance(uint8_t payloadInst,
183 IntervalType accumulateInterval,
184 IntervalType retryInterval)
185{
186 auto instance = payloadInst;
187 sd_event_source* accTimerSource = nullptr;
188 sd_event_source* retryTimerSource = nullptr;
189 int rc = 0;
190 uint64_t currentTime = 0;
191
192 rc = sd_event_now(event, CLOCK_MONOTONIC, &currentTime);
193 if (rc < 0)
194 {
195 log<level::ERR>("Failed to get the current timestamp",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700196 entry("RC=%d", rc));
Tom Josephe989c362017-03-14 17:16:50 +0530197 throw std::runtime_error("Failed to get current timestamp");
198 }
199
200 // Create character accumulate timer
Vernon Mauery9e801a22018-10-12 13:20:49 -0700201 rc = sd_event_add_time(event, &accTimerSource, CLOCK_MONOTONIC,
202 currentTime + accumulateInterval.count(), 0,
203 charAccTimerHandler, static_cast<void*>(&instance));
Tom Josephe989c362017-03-14 17:16:50 +0530204 if (rc < 0)
205 {
206 log<level::ERR>("Failed to setup the accumulate timer",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700207 entry("RC=%d", rc));
Tom Josephe989c362017-03-14 17:16:50 +0530208 throw std::runtime_error("Failed to setup accumulate timer");
209 }
210
211 // Create retry interval timer and add to the event loop
Vernon Mauery9e801a22018-10-12 13:20:49 -0700212 rc = sd_event_add_time(event, &retryTimerSource, CLOCK_MONOTONIC,
213 currentTime + retryInterval.count(), 0,
214 retryTimerHandler, static_cast<void*>(&instance));
Tom Josephe989c362017-03-14 17:16:50 +0530215 if (rc < 0)
216 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700217 log<level::ERR>("Failed to setup the retry timer", entry("RC=%d", rc));
Tom Josephe989c362017-03-14 17:16:50 +0530218 throw std::runtime_error("Failed to setup retry timer");
219 }
220
221 // Enable the Character Accumulate Timer
222 rc = sd_event_source_set_enabled(accTimerSource, SD_EVENT_ONESHOT);
223 if (rc < 0)
224 {
225 log<level::ERR>("Failed to enable the accumulate timer",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700226 entry("RC=%d", rc));
Tom Josephe989c362017-03-14 17:16:50 +0530227 throw std::runtime_error("Failed to enable accumulate timer");
228 }
229
230 // Disable the Retry Interval Timer
231 rc = sd_event_source_set_enabled(retryTimerSource, SD_EVENT_OFF);
232 if (rc < 0)
233 {
234 log<level::ERR>("Failed to disable the retry timer",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700235 entry("RC=%d", rc));
Tom Josephe989c362017-03-14 17:16:50 +0530236 throw std::runtime_error("Failed to disable retry timer");
237 }
238
239 EventSource accEventSource(accTimerSource);
240 EventSource retryEventSource(retryTimerSource);
241 accTimerSource = nullptr;
242 retryTimerSource = nullptr;
243
244 TimerMap timer;
245 timer.emplace(Timers::ACCUMULATE, std::make_tuple(std::move(accEventSource),
246 accumulateInterval));
Vernon Mauery9e801a22018-10-12 13:20:49 -0700247 timer.emplace(Timers::RETRY,
248 std::make_tuple(std::move(retryEventSource), retryInterval));
Tom Josephe989c362017-03-14 17:16:50 +0530249 payloadInfo.emplace(instance, std::move(timer));
250}
251
Tom Josepha891eb72017-03-14 17:25:01 +0530252void EventLoop::stopSOLPayloadInstance(uint8_t payloadInst)
253{
254 auto iter = payloadInfo.find(payloadInst);
255 if (iter == payloadInfo.end())
256 {
257 log<level::ERR>("SOL Payload instance not found",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700258 entry("PAYLOADINST=%d", payloadInst));
Tom Josepha891eb72017-03-14 17:25:01 +0530259 throw std::runtime_error("SOL payload instance not found");
260 }
261
262 int rc = 0;
263
264 /* Destroy the character accumulate timer event source */
265 rc = sd_event_source_set_enabled(
Vernon Mauery9e801a22018-10-12 13:20:49 -0700266 (std::get<0>(iter->second.at(Timers::ACCUMULATE))).get(), SD_EVENT_OFF);
Tom Josepha891eb72017-03-14 17:25:01 +0530267 if (rc < 0)
268 {
269 log<level::ERR>("Failed to disable the character accumulate timer",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700270 entry("RC=%d", rc));
Tom Josepha891eb72017-03-14 17:25:01 +0530271 payloadInfo.erase(payloadInst);
272 throw std::runtime_error("Failed to disable accumulate timer");
273 }
274
275 /* Destroy the retry interval timer event source */
276 rc = sd_event_source_set_enabled(
Vernon Mauery9e801a22018-10-12 13:20:49 -0700277 (std::get<0>(iter->second.at(Timers::RETRY))).get(), SD_EVENT_OFF);
Tom Josepha891eb72017-03-14 17:25:01 +0530278 if (rc < 0)
279 {
280 log<level::ERR>("Failed to disable the retry timer",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700281 entry("RC=%d", rc));
Tom Josepha891eb72017-03-14 17:25:01 +0530282 payloadInfo.erase(payloadInst);
283 throw std::runtime_error("Failed to disable retry timer");
284 }
285
286 payloadInfo.erase(payloadInst);
287}
288
Vernon Mauery9e801a22018-10-12 13:20:49 -0700289void EventLoop::switchTimer(uint8_t payloadInst, Timers type, bool status)
Tom Joseph0a0d8f62017-03-14 17:04:58 +0530290{
291 auto iter = payloadInfo.find(payloadInst);
292 if (iter == payloadInfo.end())
293 {
294 log<level::ERR>("SOL Payload instance not found",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700295 entry("PAYLOADINST=%d", payloadInst));
Tom Joseph0a0d8f62017-03-14 17:04:58 +0530296 throw std::runtime_error("SOL Payload instance not found");
297 }
298
299 int rc = 0;
300 auto source = (std::get<0>(iter->second.at(type))).get();
301 auto interval = std::get<1>(iter->second.at(type));
302
303 // Turn OFF the timer
304 if (!status)
305 {
306 rc = sd_event_source_set_enabled(source, SD_EVENT_OFF);
307 if (rc < 0)
308 {
309 log<level::ERR>("Failed to disable the timer", entry("RC=%d", rc));
310 throw std::runtime_error("Failed to disable timer");
311 }
312 return;
313 }
314
315 // Turn ON the timer
316 uint64_t currentTime = 0;
317 rc = sd_event_now(event, CLOCK_MONOTONIC, &currentTime);
318 if (rc < 0)
319 {
320 log<level::ERR>("Failed to get the current timestamp",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700321 entry("RC=%d", rc));
Tom Joseph0a0d8f62017-03-14 17:04:58 +0530322 throw std::runtime_error("Failed to get current timestamp");
323 }
324
325 rc = sd_event_source_set_time(source, currentTime + interval.count());
326 if (rc < 0)
327 {
328 log<level::ERR>("sd_event_source_set_time function failed",
Vernon Mauery9e801a22018-10-12 13:20:49 -0700329 entry("RC=%d", rc));
Tom Joseph0a0d8f62017-03-14 17:04:58 +0530330 throw std::runtime_error("sd_event_source_set_time function failed");
331 }
332
333 rc = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT);
334 if (rc < 0)
335 {
Vernon Mauery9e801a22018-10-12 13:20:49 -0700336 log<level::ERR>("Failed to enable the timer", entry("RC=%d", rc));
Tom Joseph0a0d8f62017-03-14 17:04:58 +0530337 throw std::runtime_error("Failed to enable timer");
338 }
339}
340
Tom Joseph7fd26dd2017-03-14 15:26:26 +0530341} // namespace eventloop