blob: 8c9abf4996f9aa5fded659bcb0d32e50a7922775 [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 Mauery8d6f2002018-11-07 09:55:53 -080026 auto msgHandler = std::make_shared<message::Handler>(channelPtr, io);
Tom Joseph7fd26dd2017-03-14 15:26:26 +053027
Vernon Mauery8d6f2002018-11-07 09:55:53 -080028 msgHandler->processIncoming();
Tom Joseph7fd26dd2017-03-14 15:26:26 +053029 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080030 catch (const std::exception& e)
Tom Joseph7fd26dd2017-03-14 15:26:26 +053031 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080032 log<level::ERR>("Executing the IPMI message failed",
33 entry("EXCEPTION=%s", e.what()));
Tom Joseph7fd26dd2017-03-14 15:26:26 +053034 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080035}
Tom Joseph7fd26dd2017-03-14 15:26:26 +053036
Vernon Mauery7a0142c2018-11-09 08:38:16 -080037void EventLoop::startRmcpReceive()
38{
39 udpSocket->async_wait(boost::asio::socket_base::wait_read,
40 [this](const boost::system::error_code& ec) {
41 if (!ec)
42 {
43 io->post([this]() { startRmcpReceive(); });
44 handleRmcpPacket();
45 }
46 });
Tom Joseph7fd26dd2017-03-14 15:26:26 +053047}
48
Vernon Mauerycbccb052018-10-24 13:52:22 -070049int EventLoop::startEventLoop()
Tom Joseph7fd26dd2017-03-14 15:26:26 +053050{
Vernon Mauerycbccb052018-10-24 13:52:22 -070051 sdbusplus::asio::sd_event_wrapper sdEvents(*io);
52 event = sdEvents.get();
Marri Devender Rao3ecf0a12017-07-13 08:07:22 -050053
Vernon Mauery22c8a212018-10-24 14:51:23 -070054 // set up boost::asio signal handling
55 boost::asio::signal_set signals(*io, SIGINT, SIGTERM);
Vernon Mauery7a0142c2018-11-09 08:38:16 -080056 signals.async_wait(
57 [this](const boost::system::error_code& error, int signalNumber) {
58 udpSocket->cancel();
59 udpSocket->close();
60 io->stop();
61 });
Tom Joseph7fd26dd2017-03-14 15:26:26 +053062
Vernon Mauery9e801a22018-10-12 13:20:49 -070063 // Create our own socket if SysD did not supply one.
Vernon Mauery7a0142c2018-11-09 08:38:16 -080064 int listensFdCount = sd_listen_fds(0);
65 if (listensFdCount == 1)
Tom Joseph7fd26dd2017-03-14 15:26:26 +053066 {
Vernon Mauery7a0142c2018-11-09 08:38:16 -080067 if (sd_is_socket(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_DGRAM, -1))
68 {
69 udpSocket = std::make_shared<boost::asio::ip::udp::socket>(
70 *io, boost::asio::ip::udp::v6(), SD_LISTEN_FDS_START);
71 }
Dave Cobbley2c15f0c2017-11-13 16:19:09 -080072 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080073 else if (listensFdCount > 1)
Dave Cobbley2c15f0c2017-11-13 16:19:09 -080074 {
75 log<level::ERR>("Too many file descriptors received");
Vernon Mauerycbccb052018-10-24 13:52:22 -070076 return EXIT_FAILURE;
Tom Joseph7fd26dd2017-03-14 15:26:26 +053077 }
Vernon Mauery7a0142c2018-11-09 08:38:16 -080078 if (!udpSocket)
79 {
80 udpSocket = std::make_shared<boost::asio::ip::udp::socket>(
81 *io, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v6(),
82 IPMI_STD_PORT));
83 if (!udpSocket)
84 {
85 log<level::ERR>("Failed to start listening on RMCP socket");
86 return EXIT_FAILURE;
87 }
88 }
89 startRmcpReceive();
Tom Joseph7fd26dd2017-03-14 15:26:26 +053090
Vernon Mauerycbccb052018-10-24 13:52:22 -070091 io->run();
Tom Joseph7fd26dd2017-03-14 15:26:26 +053092
Vernon Mauerycbccb052018-10-24 13:52:22 -070093 return EXIT_SUCCESS;
Tom Joseph7fd26dd2017-03-14 15:26:26 +053094}
95
Tom Joseph7fd26dd2017-03-14 15:26:26 +053096} // namespace eventloop