]>
Commit | Line | Data |
---|---|---|
4b292b55 | 1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
d6e889b6 VB |
2 | /* |
3 | * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx> | |
4 | * | |
5 | * Permission to use, copy, modify, and/or distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
18 | #include "lldpd.h" | |
bdfe4193 | 19 | #include "trace.h" |
d6e889b6 VB |
20 | |
21 | #include <unistd.h> | |
22 | #include <signal.h> | |
e0478a46 | 23 | #include <errno.h> |
864a7bd5 | 24 | #include <time.h> |
bec75f84 | 25 | #include <fcntl.h> |
d7460a6f ALG |
26 | #if defined(__clang__) |
27 | #pragma clang diagnostic push | |
28 | #pragma clang diagnostic ignored "-Wdocumentation" | |
29 | #endif | |
d6e889b6 | 30 | #include <event2/event.h> |
e0478a46 VB |
31 | #include <event2/bufferevent.h> |
32 | #include <event2/buffer.h> | |
d7460a6f ALG |
33 | #if defined(__clang__) |
34 | #pragma clang diagnostic pop | |
35 | #endif | |
d6e889b6 | 36 | |
b0b8841b ST |
37 | #define EVENT_BUFFER 1024 |
38 | ||
d6e889b6 VB |
39 | static void |
40 | levent_log_cb(int severity, const char *msg) | |
41 | { | |
42 | switch (severity) { | |
26fa5d17 VB |
43 | case _EVENT_LOG_DEBUG: log_debug("libevent", "%s", msg); break; |
44 | case _EVENT_LOG_MSG: log_info ("libevent", "%s", msg); break; | |
45 | case _EVENT_LOG_WARN: log_warnx("libevent", "%s", msg); break; | |
46 | case _EVENT_LOG_ERR: log_warnx("libevent", "%s", msg); break; | |
d6e889b6 VB |
47 | } |
48 | } | |
49 | ||
d6e889b6 VB |
50 | struct lldpd_events { |
51 | TAILQ_ENTRY(lldpd_events) next; | |
52 | struct event *ev; | |
d6e889b6 VB |
53 | }; |
54 | TAILQ_HEAD(ev_l, lldpd_events); | |
55 | ||
56 | #define levent_snmp_fds(cfg) ((struct ev_l*)(cfg)->g_snmp_fds) | |
d6e889b6 VB |
57 | #define levent_hardware_fds(hardware) ((struct ev_l*)(hardware)->h_recv) |
58 | ||
59 | #ifdef USE_SNMP | |
60 | #include <net-snmp/net-snmp-config.h> | |
61 | #include <net-snmp/net-snmp-includes.h> | |
62 | #include <net-snmp/agent/net-snmp-agent-includes.h> | |
63 | #include <net-snmp/agent/snmp_vars.h> | |
64 | ||
59c32cf0 VB |
65 | /* Compatibility with older versions of NetSNMP */ |
66 | #ifndef HAVE_SNMP_SELECT_INFO2 | |
67 | # define netsnmp_large_fd_set fd_set | |
68 | # define snmp_read2 snmp_read | |
69 | # define snmp_select_info2 snmp_select_info | |
70 | # define netsnmp_large_fd_set_init(...) | |
71 | # define netsnmp_large_fd_set_cleanup(...) | |
72 | # define NETSNMP_LARGE_FD_SET FD_SET | |
73 | # define NETSNMP_LARGE_FD_CLR FD_CLR | |
74 | # define NETSNMP_LARGE_FD_ZERO FD_ZERO | |
75 | # define NETSNMP_LARGE_FD_ISSET FD_ISSET | |
76 | #else | |
77 | # include <net-snmp/library/large_fd_set.h> | |
78 | #endif | |
79 | ||
d6e889b6 VB |
80 | static void levent_snmp_update(struct lldpd *); |
81 | ||
82 | /* | |
83 | * Callback function when we have something to read from SNMP. | |
84 | * | |
85 | * This function is called because we have a read event on one SNMP | |
86 | * file descriptor. When need to call snmp_read() on it. | |
87 | */ | |
88 | static void | |
89 | levent_snmp_read(evutil_socket_t fd, short what, void *arg) | |
90 | { | |
d6e889b6 | 91 | struct lldpd *cfg = arg; |
59c32cf0 | 92 | netsnmp_large_fd_set fdset; |
5fd6695c | 93 | (void)what; |
59c32cf0 VB |
94 | netsnmp_large_fd_set_init(&fdset, FD_SETSIZE); |
95 | NETSNMP_LARGE_FD_ZERO(&fdset); | |
96 | NETSNMP_LARGE_FD_SET(fd, &fdset); | |
97 | snmp_read2(&fdset); | |
d6e889b6 VB |
98 | levent_snmp_update(cfg); |
99 | } | |
100 | ||
101 | /* | |
102 | * Callback function for a SNMP timeout. | |
103 | * | |
104 | * A SNMP timeout has occurred. Call `snmp_timeout()` to handle it. | |
105 | */ | |
106 | static void | |
107 | levent_snmp_timeout(evutil_socket_t fd, short what, void *arg) | |
108 | { | |
d6e889b6 | 109 | struct lldpd *cfg = arg; |
5fd6695c | 110 | (void)what; (void)fd; |
d6e889b6 | 111 | snmp_timeout(); |
219c432e | 112 | run_alarms(); |
d6e889b6 VB |
113 | levent_snmp_update(cfg); |
114 | } | |
115 | ||
116 | /* | |
117 | * Watch a new SNMP FD. | |
118 | * | |
119 | * @param base The libevent base we are working on. | |
120 | * @param fd The file descriptor we want to watch. | |
121 | * | |
122 | * The file descriptor is appended to the list of file descriptors we | |
123 | * want to watch. | |
124 | */ | |
125 | static void | |
126 | levent_snmp_add_fd(struct lldpd *cfg, int fd) | |
127 | { | |
128 | struct event_base *base = cfg->g_base; | |
129 | struct lldpd_events *snmpfd = calloc(1, sizeof(struct lldpd_events)); | |
130 | if (!snmpfd) { | |
6f8925be | 131 | log_warn("event", "unable to allocate memory for new SNMP event"); |
d6e889b6 VB |
132 | return; |
133 | } | |
bec75f84 | 134 | levent_make_socket_nonblocking(fd); |
d6e889b6 VB |
135 | if ((snmpfd->ev = event_new(base, fd, |
136 | EV_READ | EV_PERSIST, | |
137 | levent_snmp_read, | |
138 | cfg)) == NULL) { | |
6f8925be | 139 | log_warnx("event", "unable to allocate a new SNMP event for FD %d", fd); |
d6e889b6 VB |
140 | free(snmpfd); |
141 | return; | |
142 | } | |
143 | if (event_add(snmpfd->ev, NULL) == -1) { | |
6f8925be | 144 | log_warnx("event", "unable to schedule new SNMP event for FD %d", fd); |
d6e889b6 VB |
145 | event_free(snmpfd->ev); |
146 | free(snmpfd); | |
147 | return; | |
148 | } | |
149 | TAILQ_INSERT_TAIL(levent_snmp_fds(cfg), snmpfd, next); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Update SNMP event loop. | |
154 | * | |
155 | * New events are added and some other are removed. This function | |
156 | * should be called every time a SNMP event happens: either when | |
157 | * handling a SNMP packet, a SNMP timeout or when sending a SNMP | |
158 | * packet. This function will keep libevent in sync with NetSNMP. | |
159 | * | |
160 | * @param base The libevent base we are working on. | |
161 | */ | |
162 | static void | |
163 | levent_snmp_update(struct lldpd *cfg) | |
164 | { | |
165 | int maxfd = 0; | |
166 | int block = 1; | |
d6e889b6 VB |
167 | struct timeval timeout; |
168 | static int howmany = 0; | |
5fd6695c VB |
169 | int added = 0, removed = 0, current = 0; |
170 | struct lldpd_events *snmpfd, *snmpfd_next; | |
d6e889b6 VB |
171 | |
172 | /* snmp_select_info() can be tricky to understand. We set `block` to | |
173 | 1 to means that we don't request a timeout. snmp_select_info() | |
174 | will reset `block` to 0 if it wants us to setup a timeout. In | |
175 | this timeout, `snmp_timeout()` should be invoked. | |
59c32cf0 | 176 | |
d6e889b6 VB |
177 | Each FD in `fdset` will need to be watched for reading. If one of |
178 | them become active, `snmp_read()` should be called on it. | |
179 | */ | |
59c32cf0 VB |
180 | |
181 | netsnmp_large_fd_set fdset; | |
182 | netsnmp_large_fd_set_init(&fdset, FD_SETSIZE); | |
183 | NETSNMP_LARGE_FD_ZERO(&fdset); | |
184 | snmp_select_info2(&maxfd, &fdset, &timeout, &block); | |
185 | ||
d6e889b6 VB |
186 | /* We need to untrack any event whose FD is not in `fdset` |
187 | anymore */ | |
d6e889b6 VB |
188 | for (snmpfd = TAILQ_FIRST(levent_snmp_fds(cfg)); |
189 | snmpfd; | |
190 | snmpfd = snmpfd_next) { | |
191 | snmpfd_next = TAILQ_NEXT(snmpfd, next); | |
192 | if (event_get_fd(snmpfd->ev) >= maxfd || | |
59c32cf0 | 193 | (!NETSNMP_LARGE_FD_ISSET(event_get_fd(snmpfd->ev), &fdset))) { |
d6e889b6 VB |
194 | event_free(snmpfd->ev); |
195 | TAILQ_REMOVE(levent_snmp_fds(cfg), snmpfd, next); | |
196 | free(snmpfd); | |
197 | removed++; | |
198 | } else { | |
59c32cf0 | 199 | NETSNMP_LARGE_FD_CLR(event_get_fd(snmpfd->ev), &fdset); |
d6e889b6 VB |
200 | current++; |
201 | } | |
202 | } | |
59c32cf0 | 203 | |
d6e889b6 VB |
204 | /* Invariant: FD in `fdset` are not in list of FD */ |
205 | for (int fd = 0; fd < maxfd; fd++) { | |
59c32cf0 | 206 | if (NETSNMP_LARGE_FD_ISSET(fd, &fdset)) { |
d6e889b6 VB |
207 | levent_snmp_add_fd(cfg, fd); |
208 | added++; | |
209 | } | |
210 | } | |
211 | current += added; | |
212 | if (howmany != current) { | |
6f8925be | 213 | log_debug("event", "added %d events, removed %d events, total of %d events", |
d6e889b6 VB |
214 | added, removed, current); |
215 | howmany = current; | |
216 | } | |
217 | ||
218 | /* If needed, handle timeout */ | |
219 | if (evtimer_add(cfg->g_snmp_timeout, block?NULL:&timeout) == -1) | |
6f8925be | 220 | log_warnx("event", "unable to schedule timeout function for SNMP"); |
59c32cf0 VB |
221 | |
222 | netsnmp_large_fd_set_cleanup(&fdset); | |
d6e889b6 VB |
223 | } |
224 | #endif /* USE_SNMP */ | |
225 | ||
257db885 | 226 | struct lldpd_one_client { |
4e90a9e0 | 227 | TAILQ_ENTRY(lldpd_one_client) next; |
257db885 | 228 | struct lldpd *cfg; |
e0478a46 | 229 | struct bufferevent *bev; |
4e90a9e0 | 230 | int subscribed; /* Is this client subscribed to changes? */ |
257db885 | 231 | }; |
4e90a9e0 VB |
232 | TAILQ_HEAD(, lldpd_one_client) lldpd_clients; |
233 | ||
234 | static void | |
235 | levent_ctl_free_client(struct lldpd_one_client *client) | |
236 | { | |
237 | if (client && client->bev) bufferevent_free(client->bev); | |
238 | if (client) { | |
239 | TAILQ_REMOVE(&lldpd_clients, client, next); | |
240 | free(client); | |
241 | } | |
242 | } | |
257db885 | 243 | |
e0478a46 | 244 | static ssize_t |
4e90a9e0 | 245 | levent_ctl_send(struct lldpd_one_client *client, int type, void *data, size_t len) |
e0478a46 | 246 | { |
e0478a46 VB |
247 | struct bufferevent *bev = client->bev; |
248 | struct hmsg_header hdr = { .len = len, .type = type }; | |
249 | bufferevent_disable(bev, EV_WRITE); | |
250 | if (bufferevent_write(bev, &hdr, sizeof(struct hmsg_header)) == -1 || | |
251 | (len > 0 && bufferevent_write(bev, data, len) == -1)) { | |
6f8925be | 252 | log_warnx("event", "unable to create answer to client"); |
4e90a9e0 | 253 | levent_ctl_free_client(client); |
e0478a46 VB |
254 | return -1; |
255 | } | |
256 | bufferevent_enable(bev, EV_WRITE); | |
257 | return len; | |
258 | } | |
259 | ||
4e90a9e0 VB |
260 | void |
261 | levent_ctl_notify(char *ifname, int state, struct lldpd_port *neighbor) | |
262 | { | |
263 | struct lldpd_one_client *client, *client_next; | |
264 | struct lldpd_neighbor_change neigh = { | |
265 | .ifname = ifname, | |
266 | .state = state, | |
267 | .neighbor = neighbor | |
268 | }; | |
269 | void *output = NULL; | |
d79c3de4 | 270 | ssize_t output_len = 0; |
4e90a9e0 | 271 | |
4e90a9e0 | 272 | /* Don't use TAILQ_FOREACH, the client may be deleted in case of errors. */ |
6f8925be | 273 | log_debug("control", "notify clients of neighbor changes"); |
4e90a9e0 VB |
274 | for (client = TAILQ_FIRST(&lldpd_clients); |
275 | client; | |
276 | client = client_next) { | |
277 | client_next = TAILQ_NEXT(client, next); | |
278 | if (!client->subscribed) continue; | |
be969691 VB |
279 | |
280 | if (output == NULL) { | |
281 | /* Ugly hack: we don't want to transmit a list of | |
74e0080e | 282 | * ports. We patch the port to avoid this. */ |
be969691 VB |
283 | TAILQ_ENTRY(lldpd_port) backup_p_entries; |
284 | memcpy(&backup_p_entries, &neighbor->p_entries, | |
285 | sizeof(backup_p_entries)); | |
be969691 VB |
286 | memset(&neighbor->p_entries, 0, |
287 | sizeof(backup_p_entries)); | |
985a4cb5 | 288 | output_len = lldpd_neighbor_change_serialize(&neigh, &output); |
be969691 VB |
289 | memcpy(&neighbor->p_entries, &backup_p_entries, |
290 | sizeof(backup_p_entries)); | |
be969691 VB |
291 | |
292 | if (output_len <= 0) { | |
6f8925be | 293 | log_warnx("event", "unable to serialize changed neighbor"); |
be969691 VB |
294 | return; |
295 | } | |
296 | } | |
297 | ||
4e90a9e0 VB |
298 | levent_ctl_send(client, NOTIFICATION, output, output_len); |
299 | } | |
300 | ||
301 | free(output); | |
302 | } | |
303 | ||
304 | static ssize_t | |
305 | levent_ctl_send_cb(void *out, int type, void *data, size_t len) | |
306 | { | |
307 | struct lldpd_one_client *client = out; | |
308 | return levent_ctl_send(client, type, data, len); | |
309 | } | |
310 | ||
d6e889b6 | 311 | static void |
e0478a46 | 312 | levent_ctl_recv(struct bufferevent *bev, void *ptr) |
d6e889b6 | 313 | { |
e0478a46 VB |
314 | struct lldpd_one_client *client = ptr; |
315 | struct evbuffer *buffer = bufferevent_get_input(bev); | |
316 | size_t buffer_len = evbuffer_get_length(buffer); | |
317 | struct hmsg_header hdr; | |
318 | void *data = NULL; | |
319 | ||
6f8925be | 320 | log_debug("control", "receive data on Unix socket"); |
e0478a46 VB |
321 | if (buffer_len < sizeof(struct hmsg_header)) |
322 | return; /* Not enough data yet */ | |
323 | if (evbuffer_copyout(buffer, &hdr, | |
324 | sizeof(struct hmsg_header)) != sizeof(struct hmsg_header)) { | |
6f8925be | 325 | log_warnx("event", "not able to read header"); |
e0478a46 VB |
326 | return; |
327 | } | |
82374540 | 328 | if (hdr.len > HMSG_MAX_SIZE) { |
6f8925be | 329 | log_warnx("event", "message received is too large"); |
e0478a46 VB |
330 | goto recv_error; |
331 | } | |
d6e889b6 | 332 | |
e0478a46 VB |
333 | if (buffer_len < hdr.len + sizeof(struct hmsg_header)) |
334 | return; /* Not enough data yet */ | |
335 | if (hdr.len > 0 && (data = malloc(hdr.len)) == NULL) { | |
6f8925be | 336 | log_warnx("event", "not enough memory"); |
e0478a46 VB |
337 | goto recv_error; |
338 | } | |
339 | evbuffer_drain(buffer, sizeof(struct hmsg_header)); | |
340 | if (hdr.len > 0) evbuffer_remove(buffer, data, hdr.len); | |
4e90a9e0 VB |
341 | |
342 | /* Currently, we should not receive notification acknowledgment. But if | |
343 | * we receive one, we can discard it. */ | |
344 | if (hdr.len == 0 && hdr.type == NOTIFICATION) return; | |
e0478a46 | 345 | if (client_handle_client(client->cfg, |
4e90a9e0 VB |
346 | levent_ctl_send_cb, client, |
347 | hdr.type, data, hdr.len, | |
348 | &client->subscribed) == -1) goto recv_error; | |
e0478a46 VB |
349 | free(data); |
350 | return; | |
351 | ||
352 | recv_error: | |
353 | free(data); | |
4e90a9e0 | 354 | levent_ctl_free_client(client); |
e0478a46 VB |
355 | } |
356 | ||
357 | static void | |
358 | levent_ctl_event(struct bufferevent *bev, short events, void *ptr) | |
359 | { | |
360 | struct lldpd_one_client *client = ptr; | |
361 | if (events & BEV_EVENT_ERROR) { | |
6f8925be | 362 | log_warnx("event", "an error occurred with client: %s", |
e0478a46 | 363 | evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())); |
4e90a9e0 | 364 | levent_ctl_free_client(client); |
e0478a46 | 365 | } else if (events & BEV_EVENT_EOF) { |
6f8925be | 366 | log_debug("event", "client has been disconnected"); |
4e90a9e0 | 367 | levent_ctl_free_client(client); |
d6e889b6 | 368 | } |
d6e889b6 VB |
369 | } |
370 | ||
371 | static void | |
372 | levent_ctl_accept(evutil_socket_t fd, short what, void *arg) | |
373 | { | |
d6e889b6 | 374 | struct lldpd *cfg = arg; |
257db885 | 375 | struct lldpd_one_client *client = NULL; |
d6e889b6 | 376 | int s; |
5fd6695c VB |
377 | (void)what; |
378 | ||
6f8925be | 379 | log_debug("control", "accept a new connection"); |
d6e889b6 | 380 | if ((s = accept(fd, NULL, NULL)) == -1) { |
6f8925be | 381 | log_warn("event", "unable to accept connection from socket"); |
d6e889b6 VB |
382 | return; |
383 | } | |
257db885 VB |
384 | client = calloc(1, sizeof(struct lldpd_one_client)); |
385 | if (!client) { | |
6f8925be | 386 | log_warnx("event", "unable to allocate memory for new client"); |
4e90a9e0 | 387 | close(s); |
257db885 | 388 | goto accept_failed; |
d6e889b6 | 389 | } |
257db885 | 390 | client->cfg = cfg; |
bec75f84 | 391 | levent_make_socket_nonblocking(s); |
506273e9 | 392 | TAILQ_INSERT_TAIL(&lldpd_clients, client, next); |
e0478a46 VB |
393 | if ((client->bev = bufferevent_socket_new(cfg->g_base, s, |
394 | BEV_OPT_CLOSE_ON_FREE)) == NULL) { | |
6f8925be | 395 | log_warnx("event", "unable to allocate a new buffer event for new client"); |
4e90a9e0 | 396 | close(s); |
257db885 | 397 | goto accept_failed; |
d6e889b6 | 398 | } |
e0478a46 VB |
399 | bufferevent_setcb(client->bev, |
400 | levent_ctl_recv, NULL, levent_ctl_event, | |
401 | client); | |
402 | bufferevent_enable(client->bev, EV_READ | EV_WRITE); | |
6f8925be | 403 | log_debug("event", "new client accepted"); |
87dfd175 VB |
404 | /* coverity[leaked_handle] |
405 | s has been saved by bufferevent_socket_new */ | |
257db885 VB |
406 | return; |
407 | accept_failed: | |
4e90a9e0 | 408 | levent_ctl_free_client(client); |
d6e889b6 VB |
409 | } |
410 | ||
327b1d62 VB |
411 | static void |
412 | levent_priv(evutil_socket_t fd, short what, void *arg) | |
413 | { | |
414 | struct event_base *base = arg; | |
415 | ssize_t n; | |
416 | int err; | |
417 | char one; | |
418 | (void)what; | |
419 | /* Check if we have some data available. We need to pass the socket in | |
420 | * non-blocking mode to be able to run the check without disruption. */ | |
421 | levent_make_socket_nonblocking(fd); | |
422 | n = read(fd, &one, 0); err = errno; | |
423 | levent_make_socket_blocking(fd); | |
424 | ||
425 | switch (n) { | |
426 | case -1: | |
427 | if (err == EAGAIN || err == EWOULDBLOCK) | |
428 | /* No data, all good */ | |
429 | return; | |
430 | log_warnx("event", "unable to poll monitor process, exit"); | |
431 | break; | |
432 | case 0: | |
433 | log_warnx("event", "monitor process has terminated, exit"); | |
434 | break; | |
435 | default: | |
436 | /* Unfortunately, dead code, if we have data, we have requested | |
437 | * 0 byte, so we will fall in the previous case. It seems safer | |
438 | * to ask for 0 byte than asking for 1 byte. In the later case, | |
439 | * if we have to speak with the monitor again before exiting, we | |
440 | * would be out of sync. */ | |
441 | log_warnx("event", "received unexpected data from monitor process, exit"); | |
442 | break; | |
443 | } | |
444 | event_base_loopbreak(base); | |
445 | } | |
446 | ||
d6e889b6 VB |
447 | static void |
448 | levent_dump(evutil_socket_t fd, short what, void *arg) | |
449 | { | |
d6e889b6 | 450 | struct event_base *base = arg; |
5fd6695c | 451 | (void)fd; (void)what; |
6f8925be | 452 | log_debug("event", "dumping all events"); |
d6e889b6 VB |
453 | event_base_dump_events(base, stderr); |
454 | } | |
455 | static void | |
456 | levent_stop(evutil_socket_t fd, short what, void *arg) | |
457 | { | |
d6e889b6 | 458 | struct event_base *base = arg; |
5fd6695c | 459 | (void)fd; (void)what; |
d6e889b6 VB |
460 | event_base_loopbreak(base); |
461 | } | |
462 | ||
463 | static void | |
464 | levent_update_and_send(evutil_socket_t fd, short what, void *arg) | |
465 | { | |
d6e889b6 | 466 | struct lldpd *cfg = arg; |
579bedd5 | 467 | struct timeval tv = { cfg->g_config.c_tx_interval, 0 }; |
5fd6695c | 468 | (void)fd; (void)what; |
d6e889b6 | 469 | lldpd_loop(cfg); |
579bedd5 VB |
470 | if (cfg->g_iface_event != NULL) |
471 | tv.tv_sec *= 20; | |
d6e889b6 VB |
472 | event_add(cfg->g_main_loop, &tv); |
473 | } | |
474 | ||
e681c859 VB |
475 | void |
476 | levent_update_now(struct lldpd *cfg) | |
477 | { | |
478 | if (cfg->g_main_loop) | |
479 | event_active(cfg->g_main_loop, EV_TIMEOUT, 1); | |
480 | } | |
481 | ||
47287a61 VB |
482 | void |
483 | levent_send_now(struct lldpd *cfg) | |
484 | { | |
959a54d4 | 485 | struct lldpd_hardware *hardware; |
126970da VB |
486 | TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) { |
487 | if (hardware->h_timer) | |
488 | event_active(hardware->h_timer, EV_TIMEOUT, 1); | |
489 | else | |
490 | log_warnx("event", "BUG: no timer present for interface %s", | |
491 | hardware->h_ifname); | |
492 | } | |
47287a61 VB |
493 | } |
494 | ||
d6e889b6 VB |
495 | static void |
496 | levent_init(struct lldpd *cfg) | |
497 | { | |
498 | /* Setup libevent */ | |
6f8925be | 499 | log_debug("event", "initialize libevent"); |
d6e889b6 VB |
500 | event_set_log_callback(levent_log_cb); |
501 | if (!(cfg->g_base = event_base_new())) | |
a87db231 | 502 | fatalx("event", "unable to create a new libevent base"); |
6f8925be | 503 | log_info("event", "libevent %s initialized with %s method", |
d6e889b6 VB |
504 | event_get_version(), |
505 | event_base_get_method(cfg->g_base)); | |
506 | ||
507 | /* Setup SNMP */ | |
508 | #ifdef USE_SNMP | |
509 | if (cfg->g_snmp) { | |
510 | agent_init(cfg, cfg->g_snmp_agentx); | |
511 | cfg->g_snmp_timeout = evtimer_new(cfg->g_base, | |
512 | levent_snmp_timeout, | |
513 | cfg); | |
514 | if (!cfg->g_snmp_timeout) | |
a87db231 | 515 | fatalx("event", "unable to setup timeout function for SNMP"); |
d6e889b6 VB |
516 | if ((cfg->g_snmp_fds = |
517 | malloc(sizeof(struct ev_l))) == NULL) | |
a87db231 | 518 | fatalx("event", "unable to allocate memory for SNMP events"); |
d6e889b6 VB |
519 | TAILQ_INIT(levent_snmp_fds(cfg)); |
520 | } | |
521 | #endif | |
6f8925be | 522 | |
579bedd5 | 523 | /* Setup loop that will run every X seconds. */ |
6f8925be | 524 | log_debug("event", "register loop timer"); |
d6e889b6 VB |
525 | if (!(cfg->g_main_loop = event_new(cfg->g_base, -1, 0, |
526 | levent_update_and_send, | |
527 | cfg))) | |
a87db231 | 528 | fatalx("event", "unable to setup main timer"); |
86b5c6fb | 529 | event_active(cfg->g_main_loop, EV_TIMEOUT, 1); |
d6e889b6 VB |
530 | |
531 | /* Setup unix socket */ | |
50efc5f7 | 532 | struct event *ctl_event; |
6f8925be | 533 | log_debug("event", "register Unix socket"); |
4e90a9e0 | 534 | TAILQ_INIT(&lldpd_clients); |
bec75f84 | 535 | levent_make_socket_nonblocking(cfg->g_ctl); |
50efc5f7 | 536 | if ((ctl_event = event_new(cfg->g_base, cfg->g_ctl, |
d6e889b6 | 537 | EV_READ|EV_PERSIST, levent_ctl_accept, cfg)) == NULL) |
a87db231 | 538 | fatalx("event", "unable to setup control socket event"); |
50efc5f7 | 539 | event_add(ctl_event, NULL); |
d6e889b6 | 540 | |
327b1d62 VB |
541 | /* Somehow monitor the monitor process */ |
542 | struct event *monitor_event; | |
543 | log_debug("event", "monitor the monitor process"); | |
544 | if ((monitor_event = event_new(cfg->g_base, priv_fd(PRIV_UNPRIVILEGED), | |
545 | EV_READ|EV_PERSIST, levent_priv, cfg->g_base)) == NULL) | |
546 | fatalx("event", "unable to monitor monitor process"); | |
547 | event_add(monitor_event, NULL); | |
548 | ||
d6e889b6 | 549 | /* Signals */ |
6f8925be | 550 | log_debug("event", "register signals"); |
d6e889b6 VB |
551 | evsignal_add(evsignal_new(cfg->g_base, SIGUSR1, |
552 | levent_dump, cfg->g_base), | |
553 | NULL); | |
d6e889b6 VB |
554 | evsignal_add(evsignal_new(cfg->g_base, SIGINT, |
555 | levent_stop, cfg->g_base), | |
556 | NULL); | |
557 | evsignal_add(evsignal_new(cfg->g_base, SIGTERM, | |
558 | levent_stop, cfg->g_base), | |
559 | NULL); | |
560 | } | |
561 | ||
562 | /* Initialize libevent and start the event loop */ | |
563 | void | |
564 | levent_loop(struct lldpd *cfg) | |
565 | { | |
566 | levent_init(cfg); | |
e4ff3ed5 | 567 | lldpd_loop(cfg); |
dbcb6846 VB |
568 | #ifdef USE_SNMP |
569 | if (cfg->g_snmp) levent_snmp_update(cfg); | |
570 | #endif | |
d6e889b6 VB |
571 | |
572 | /* libevent loop */ | |
573 | do { | |
bdfe4193 | 574 | TRACE(LLDPD_EVENT_LOOP()); |
d6e889b6 VB |
575 | if (event_base_got_break(cfg->g_base) || |
576 | event_base_got_exit(cfg->g_base)) | |
577 | break; | |
d6e889b6 VB |
578 | } while (event_base_loop(cfg->g_base, EVLOOP_ONCE) == 0); |
579 | ||
3744168c VB |
580 | if (cfg->g_iface_timer_event != NULL) |
581 | event_free(cfg->g_iface_timer_event); | |
582 | ||
d6e889b6 VB |
583 | #ifdef USE_SNMP |
584 | if (cfg->g_snmp) | |
585 | agent_shutdown(); | |
586 | #endif /* USE_SNMP */ | |
587 | ||
588 | } | |
589 | ||
590 | static void | |
591 | levent_hardware_recv(evutil_socket_t fd, short what, void *arg) | |
592 | { | |
d6e889b6 VB |
593 | struct lldpd_hardware *hardware = arg; |
594 | struct lldpd *cfg = hardware->h_cfg; | |
5fd6695c | 595 | (void)what; |
6f8925be VB |
596 | log_debug("event", "received something for %s", |
597 | hardware->h_ifname); | |
d6e889b6 | 598 | lldpd_recv(cfg, hardware, fd); |
3333d2a8 | 599 | levent_schedule_cleanup(cfg); |
d6e889b6 VB |
600 | } |
601 | ||
602 | void | |
603 | levent_hardware_init(struct lldpd_hardware *hardware) | |
604 | { | |
6f8925be | 605 | log_debug("event", "initialize events for %s", hardware->h_ifname); |
d6e889b6 VB |
606 | if ((hardware->h_recv = |
607 | malloc(sizeof(struct ev_l))) == NULL) { | |
6f8925be | 608 | log_warnx("event", "unable to allocate memory for %s", |
d6e889b6 VB |
609 | hardware->h_ifname); |
610 | return; | |
611 | } | |
612 | TAILQ_INIT(levent_hardware_fds(hardware)); | |
613 | } | |
614 | ||
615 | void | |
616 | levent_hardware_add_fd(struct lldpd_hardware *hardware, int fd) | |
617 | { | |
5fd6695c | 618 | struct lldpd_events *hfd = NULL; |
d6e889b6 VB |
619 | if (!hardware->h_recv) return; |
620 | ||
5fd6695c | 621 | hfd = calloc(1, sizeof(struct lldpd_events)); |
d6e889b6 | 622 | if (!hfd) { |
6f8925be | 623 | log_warnx("event", "unable to allocate new event for %s", |
d6e889b6 VB |
624 | hardware->h_ifname); |
625 | return; | |
626 | } | |
bec75f84 | 627 | levent_make_socket_nonblocking(fd); |
d6e889b6 VB |
628 | if ((hfd->ev = event_new(hardware->h_cfg->g_base, fd, |
629 | EV_READ | EV_PERSIST, | |
630 | levent_hardware_recv, | |
631 | hardware)) == NULL) { | |
6f8925be | 632 | log_warnx("event", "unable to allocate a new event for %s", |
d6e889b6 VB |
633 | hardware->h_ifname); |
634 | free(hfd); | |
635 | return; | |
636 | } | |
637 | if (event_add(hfd->ev, NULL) == -1) { | |
6f8925be | 638 | log_warnx("event", "unable to schedule new event for %s", |
d6e889b6 VB |
639 | hardware->h_ifname); |
640 | event_free(hfd->ev); | |
641 | free(hfd); | |
642 | return; | |
643 | } | |
644 | TAILQ_INSERT_TAIL(levent_hardware_fds(hardware), hfd, next); | |
645 | } | |
646 | ||
647 | void | |
648 | levent_hardware_release(struct lldpd_hardware *hardware) | |
649 | { | |
5fd6695c | 650 | struct lldpd_events *ev, *ev_next; |
dbfa89c6 VB |
651 | if (hardware->h_timer) { |
652 | event_free(hardware->h_timer); | |
653 | hardware->h_timer = NULL; | |
654 | } | |
d6e889b6 VB |
655 | if (!hardware->h_recv) return; |
656 | ||
6f8925be | 657 | log_debug("event", "release events for %s", hardware->h_ifname); |
d6e889b6 VB |
658 | for (ev = TAILQ_FIRST(levent_hardware_fds(hardware)); |
659 | ev; | |
660 | ev = ev_next) { | |
661 | ev_next = TAILQ_NEXT(ev, next); | |
662 | /* We may close several time the same FD. This is harmless. */ | |
663 | close(event_get_fd(ev->ev)); | |
664 | event_free(ev->ev); | |
665 | TAILQ_REMOVE(levent_hardware_fds(hardware), ev, next); | |
666 | free(ev); | |
667 | } | |
668 | free(levent_hardware_fds(hardware)); | |
669 | } | |
0484f180 VB |
670 | |
671 | static void | |
672 | levent_iface_trigger(evutil_socket_t fd, short what, void *arg) | |
673 | { | |
674 | struct lldpd *cfg = arg; | |
4f670a1e | 675 | log_debug("event", |
0484f180 VB |
676 | "triggering update of all interfaces"); |
677 | lldpd_update_localports(cfg); | |
678 | } | |
679 | ||
680 | static void | |
681 | levent_iface_recv(evutil_socket_t fd, short what, void *arg) | |
682 | { | |
683 | struct lldpd *cfg = arg; | |
b0b8841b | 684 | char buffer[EVENT_BUFFER]; |
0484f180 VB |
685 | int n; |
686 | ||
13181ede VB |
687 | if (cfg->g_iface_cb == NULL) { |
688 | /* Discard the message */ | |
689 | while (1) { | |
690 | n = read(fd, buffer, sizeof(buffer)); | |
691 | if (n == -1 && | |
692 | (errno == EWOULDBLOCK || | |
693 | errno == EAGAIN)) break; | |
694 | if (n == -1) { | |
695 | log_warn("event", | |
696 | "unable to receive interface change notification message"); | |
697 | return; | |
698 | } | |
699 | if (n == 0) { | |
700 | log_warnx("event", | |
701 | "end of file reached while getting interface change notification message"); | |
702 | return; | |
703 | } | |
0484f180 | 704 | } |
13181ede VB |
705 | } else { |
706 | cfg->g_iface_cb(cfg); | |
0484f180 VB |
707 | } |
708 | ||
709 | /* Schedule local port update. We don't run it right away because we may | |
710 | * receive a batch of events like this. */ | |
711 | struct timeval one_sec = {1, 0}; | |
bdfe4193 | 712 | TRACE(LLDPD_INTERFACES_NOTIFICATION()); |
0484f180 VB |
713 | log_debug("event", |
714 | "received notification change, schedule an update of all interfaces in one second"); | |
715 | if (cfg->g_iface_timer_event == NULL) { | |
716 | if ((cfg->g_iface_timer_event = evtimer_new(cfg->g_base, | |
717 | levent_iface_trigger, cfg)) == NULL) { | |
718 | log_warnx("event", | |
719 | "unable to create a new event to trigger interface update"); | |
720 | return; | |
721 | } | |
722 | } | |
723 | if (evtimer_add(cfg->g_iface_timer_event, &one_sec) == -1) { | |
724 | log_warnx("event", | |
725 | "unable to schedule interface updates"); | |
726 | return; | |
727 | } | |
728 | } | |
729 | ||
aa313f2a | 730 | int |
0484f180 VB |
731 | levent_iface_subscribe(struct lldpd *cfg, int socket) |
732 | { | |
733 | log_debug("event", "subscribe to interface changes from socket %d", | |
734 | socket); | |
bec75f84 | 735 | levent_make_socket_nonblocking(socket); |
0484f180 VB |
736 | cfg->g_iface_event = event_new(cfg->g_base, socket, |
737 | EV_READ | EV_PERSIST, levent_iface_recv, cfg); | |
738 | if (cfg->g_iface_event == NULL) { | |
739 | log_warnx("event", | |
740 | "unable to allocate a new event for interface changes"); | |
aa313f2a | 741 | return -1; |
0484f180 VB |
742 | } |
743 | if (event_add(cfg->g_iface_event, NULL) == -1) { | |
744 | log_warnx("event", | |
745 | "unable to schedule new interface changes event"); | |
746 | event_free(cfg->g_iface_event); | |
747 | cfg->g_iface_event = NULL; | |
aa313f2a | 748 | return -1; |
0484f180 | 749 | } |
aa313f2a | 750 | return 0; |
0484f180 | 751 | } |
579bedd5 | 752 | |
3333d2a8 VB |
753 | static void |
754 | levent_trigger_cleanup(evutil_socket_t fd, short what, void *arg) | |
755 | { | |
756 | struct lldpd *cfg = arg; | |
757 | lldpd_cleanup(cfg); | |
758 | } | |
759 | ||
760 | void | |
761 | levent_schedule_cleanup(struct lldpd *cfg) | |
762 | { | |
763 | log_debug("event", "schedule next cleanup"); | |
764 | if (cfg->g_cleanup_timer != NULL) { | |
765 | event_free(cfg->g_cleanup_timer); | |
766 | } | |
767 | cfg->g_cleanup_timer = evtimer_new(cfg->g_base, levent_trigger_cleanup, cfg); | |
768 | if (cfg->g_cleanup_timer == NULL) { | |
769 | log_warnx("event", | |
770 | "unable to allocate a new event for cleanup tasks"); | |
771 | return; | |
772 | } | |
773 | ||
774 | /* Compute the next TTL event */ | |
775 | struct timeval tv = { LOCAL_CHASSIS(cfg)->c_ttl, 0 }; | |
776 | time_t now = time(NULL); | |
777 | time_t next; | |
778 | struct lldpd_hardware *hardware; | |
779 | struct lldpd_port *port; | |
780 | TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) { | |
781 | TAILQ_FOREACH(port, &hardware->h_rports, p_entries) { | |
6afb24c2 | 782 | if (now >= port->p_lastupdate + port->p_chassis->c_ttl) { |
5a215d4b | 783 | tv.tv_sec = 0; |
6afb24c2 VB |
784 | log_debug("event", "immediate cleanup on port %s (%ld, %d, %ld)", |
785 | hardware->h_ifname, now, port->p_chassis->c_ttl, port->p_lastupdate); | |
5a215d4b VB |
786 | break; |
787 | } | |
3333d2a8 | 788 | next = port->p_chassis->c_ttl - (now - port->p_lastupdate); |
5a215d4b | 789 | if (next < tv.tv_sec) |
3333d2a8 VB |
790 | tv.tv_sec = next; |
791 | } | |
792 | } | |
793 | ||
794 | log_debug("event", "next cleanup in %ld seconds", | |
795 | (long)tv.tv_sec); | |
796 | if (event_add(cfg->g_cleanup_timer, &tv) == -1) { | |
797 | log_warnx("event", | |
798 | "unable to schedula cleanup task"); | |
799 | event_free(cfg->g_cleanup_timer); | |
800 | cfg->g_cleanup_timer = NULL; | |
801 | return; | |
802 | } | |
803 | } | |
804 | ||
579bedd5 VB |
805 | static void |
806 | levent_send_pdu(evutil_socket_t fd, short what, void *arg) | |
807 | { | |
808 | struct lldpd_hardware *hardware = arg; | |
be511d00 | 809 | int tx_interval = hardware->h_cfg->g_config.c_tx_interval; |
b9de0ca6 | 810 | |
579bedd5 VB |
811 | log_debug("event", "trigger sending PDU for port %s", |
812 | hardware->h_ifname); | |
813 | lldpd_send(hardware); | |
814 | ||
be511d00 | 815 | #ifdef ENABLE_LLDPMED |
b9de0ca6 | 816 | if (hardware->h_tx_fast > 0) |
817 | hardware->h_tx_fast--; | |
818 | ||
819 | if (hardware->h_tx_fast > 0) | |
820 | tx_interval = hardware->h_cfg->g_config.c_tx_fast_interval; | |
be511d00 VB |
821 | #endif |
822 | ||
b9de0ca6 | 823 | struct timeval tv = { tx_interval, 0 }; |
579bedd5 VB |
824 | if (event_add(hardware->h_timer, &tv) == -1) { |
825 | log_warnx("event", "unable to re-register timer event for port %s", | |
826 | hardware->h_ifname); | |
827 | event_free(hardware->h_timer); | |
828 | hardware->h_timer = NULL; | |
829 | return; | |
830 | } | |
831 | } | |
832 | ||
833 | void | |
834 | levent_schedule_pdu(struct lldpd_hardware *hardware) | |
835 | { | |
836 | log_debug("event", "schedule sending PDU on %s", | |
837 | hardware->h_ifname); | |
838 | if (hardware->h_timer == NULL) { | |
839 | hardware->h_timer = evtimer_new(hardware->h_cfg->g_base, | |
840 | levent_send_pdu, hardware); | |
841 | if (hardware->h_timer == NULL) { | |
842 | log_warnx("event", "unable to schedule PDU sending for port %s", | |
843 | hardware->h_ifname); | |
844 | return; | |
845 | } | |
846 | } | |
847 | ||
848 | struct timeval tv = { 0, 0 }; | |
849 | if (event_add(hardware->h_timer, &tv) == -1) { | |
850 | log_warnx("event", "unable to register timer event for port %s", | |
851 | hardware->h_ifname); | |
852 | event_free(hardware->h_timer); | |
853 | hardware->h_timer = NULL; | |
854 | return; | |
855 | } | |
856 | } | |
bec75f84 VB |
857 | |
858 | int | |
859 | levent_make_socket_nonblocking(int fd) | |
860 | { | |
861 | int flags; | |
862 | if ((flags = fcntl(fd, F_GETFL, NULL)) < 0) { | |
863 | log_warn("event", "fcntl(%d, F_GETFL)", fd); | |
864 | return -1; | |
865 | } | |
866 | if (flags & O_NONBLOCK) return 0; | |
867 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { | |
868 | log_warn("event", "fcntl(%d, F_SETFL)", fd); | |
869 | return -1; | |
870 | } | |
871 | return 0; | |
872 | } | |
327b1d62 VB |
873 | |
874 | int | |
875 | levent_make_socket_blocking(int fd) | |
876 | { | |
877 | int flags; | |
878 | if ((flags = fcntl(fd, F_GETFL, NULL)) < 0) { | |
879 | log_warn("event", "fcntl(%d, F_GETFL)", fd); | |
880 | return -1; | |
881 | } | |
882 | if (!(flags & O_NONBLOCK)) return 0; | |
883 | if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) == -1) { | |
884 | log_warn("event", "fcntl(%d, F_SETFL)", fd); | |
885 | return -1; | |
886 | } | |
887 | return 0; | |
888 | } |