blob: 91e9947d70ba71bb5212e5f7004a5defbb285e00 [file] [log] [blame]
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05301#include "rtnetlink_server.hpp"
Ratan Guptaa54d8f82017-09-08 17:05:46 +05302#include "timer.hpp"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05303#include "types.hpp"
4#include "util.hpp"
5
6
7#include <netinet/in.h>
8#include <linux/netlink.h>
9#include <linux/rtnetlink.h>
10#include <net/if.h>
11#include <sys/types.h>
12#include <systemd/sd-daemon.h>
13#include <unistd.h>
14
15#include <phosphor-logging/log.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053016
17#include <memory>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053018
19namespace phosphor
20{
21namespace network
22{
Ratan Guptaa54d8f82017-09-08 17:05:46 +053023
24extern std::unique_ptr<phosphor::network::Timer> refreshTimer;
25
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053026namespace rtnetlink
27{
28
29/* Call Back for the sd event loop */
30static int eventHandler(sd_event_source* es, int fd, uint32_t revents,
31 void* userdata)
32{
33 char buffer[phosphor::network::rtnetlink::BUFSIZE] {};
34 int len {};
35
36 auto netLinkHeader = reinterpret_cast<struct nlmsghdr*>(buffer);
37 while ((len = recv(fd, netLinkHeader,
38 phosphor::network::rtnetlink::BUFSIZE, 0)) > 0)
39 {
40 for (; (NLMSG_OK(netLinkHeader, len)) &&
41 (netLinkHeader->nlmsg_type != NLMSG_DONE);
42 netLinkHeader = NLMSG_NEXT(netLinkHeader, len))
43 {
44 if (netLinkHeader->nlmsg_type == RTM_NEWADDR ||
45 netLinkHeader->nlmsg_type == RTM_DELADDR)
46 {
Ratan Guptaa54d8f82017-09-08 17:05:46 +053047 // starting the timer here to make sure that we don't want
48 // create the child objects multiple times.
49 if (refreshTimer->isExpired())
50 {
51 using namespace std::chrono;
52 auto time = duration_cast<microseconds>(networkChangeTimeout);
53 // if start timer throws exception then let the application
54 // crash
55 refreshTimer->startTimer(time);
56 } // end if
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053057 } // end if
58
59 } // end for
60
61 } // end while
62
63 return 0;
64}
65
66
67int Server::run()
68{
69 using namespace phosphor::logging;
70
71 struct sockaddr_nl addr {};
72
73 int fd = -1;
74 phosphor::Descriptor smartSock(fd);
75
76 int r {};
77
78 sigset_t ss {};
79
80
81 if (sigemptyset(&ss) < 0 || sigaddset(&ss, SIGTERM) < 0 ||
82 sigaddset(&ss, SIGINT) < 0)
83 {
84 r = -errno;
85 goto finish;
86 }
87 /* Block SIGTERM first, so that the event loop can handle it */
88 if (sigprocmask(SIG_BLOCK, &ss, NULL) < 0)
89 {
90 r = -errno;
91 goto finish;
92 }
93
94 /* Let's make use of the default handler and "floating"
95 reference features of sd_event_add_signal() */
96
97 r = sd_event_add_signal(eventPtr.get(), NULL, SIGTERM, NULL, NULL);
98 if (r < 0)
99 {
100 goto finish;
101 }
102
103 r = sd_event_add_signal(eventPtr.get(), NULL, SIGINT, NULL, NULL);
104 if (r < 0)
105 {
106 goto finish;
107 }
108
109 fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
110 if (fd < 0)
111 {
112 r = -errno;
113 goto finish;
114 }
115
116 smartSock.set(fd);
117 fd = -1;
118
119 memset(&addr, 0, sizeof(addr));
120 addr.nl_family = AF_NETLINK;
121 addr.nl_groups = RTMGRP_IPV4_IFADDR;
122
123 if (bind(smartSock(), (struct sockaddr*)&addr, sizeof(addr)) < 0)
124 {
125 r = -errno;
126 goto finish;
127 }
128
129 r = sd_event_add_io(eventPtr.get(), nullptr,
130 smartSock(), EPOLLIN, eventHandler, nullptr);
131 if (r < 0)
132 {
133 goto finish;
134 }
135
136 r = sd_event_loop(eventPtr.get());
137
138finish:
139
140 if (r < 0)
141 {
142 log<level::ERR>("Failure Occured in starting of server:",
143 entry("errno = %d", errno));
144 }
145
146 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
147}
148
149
150} //rtnetlink
151} //network
152} //phosphor