]>
Commit | Line | Data |
---|---|---|
230aefe2 RZ |
1 | /* |
2 | * BFD data plane implementation (distributed BFD). | |
3 | * | |
4 | * Copyright (C) 2020 Network Device Education Foundation, Inc. ("NetDEF") | |
5 | * Rafael Zalamena | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include <netinet/in.h> | |
6655b43d | 25 | #include <netinet/tcp.h> |
230aefe2 RZ |
26 | #include <sys/socket.h> |
27 | #include <sys/un.h> | |
28 | ||
29 | #ifdef __FreeBSD__ | |
30 | #include <sys/endian.h> | |
31 | #else | |
32 | #include <endian.h> | |
33 | #endif /* __FreeBSD__ */ | |
34 | ||
35 | #include <errno.h> | |
36 | #include <time.h> | |
37 | ||
38 | #include "lib/hook.h" | |
6655b43d | 39 | #include "lib/network.h" |
230aefe2 RZ |
40 | #include "lib/printfrr.h" |
41 | #include "lib/stream.h" | |
42 | #include "lib/thread.h" | |
43 | ||
44 | #include "bfd.h" | |
45 | #include "bfddp_packet.h" | |
46 | ||
47 | #include "lib/openbsd-queue.h" | |
48 | ||
bf8d3d6a DL |
49 | DEFINE_MTYPE_STATIC(BFDD, BFDD_DPLANE_CTX, |
50 | "Data plane client allocated memory"); | |
230aefe2 RZ |
51 | |
52 | /** Data plane client socket buffer size. */ | |
53 | #define BFD_DPLANE_CLIENT_BUF_SIZE 8192 | |
54 | ||
55 | struct bfd_dplane_ctx { | |
56 | /** Client file descriptor. */ | |
57 | int sock; | |
6655b43d RZ |
58 | /** Is this a connected or accepted? */ |
59 | bool client; | |
60 | /** Is the socket still connecting? */ | |
61 | bool connecting; | |
62 | /** Client/server address. */ | |
63 | union { | |
64 | struct sockaddr sa; | |
65 | struct sockaddr_in sin; | |
66 | struct sockaddr_in6 sin6; | |
67 | struct sockaddr_un sun; | |
68 | } addr; | |
69 | /** Address length. */ | |
70 | socklen_t addrlen; | |
230aefe2 RZ |
71 | /** Data plane current last used ID. */ |
72 | uint16_t last_id; | |
73 | ||
74 | /** Input buffer data. */ | |
75 | struct stream *inbuf; | |
76 | /** Output buffer data. */ | |
77 | struct stream *outbuf; | |
78 | /** Input event data. */ | |
79 | struct thread *inbufev; | |
80 | /** Output event data. */ | |
81 | struct thread *outbufev; | |
6655b43d RZ |
82 | /** Connection event. */ |
83 | struct thread *connectev; | |
230aefe2 RZ |
84 | |
85 | /** Amount of bytes read. */ | |
86 | uint64_t in_bytes; | |
87 | /** Amount of bytes read peak. */ | |
88 | uint64_t in_bytes_peak; | |
89 | /** Amount of bytes written. */ | |
90 | uint64_t out_bytes; | |
91 | /** Amount of bytes written peak. */ | |
92 | uint64_t out_bytes_peak; | |
93 | /** Amount of output buffer full events (`bfd_dplane_enqueue` failed). | |
94 | */ | |
95 | uint64_t out_fullev; | |
96 | ||
97 | /** Amount of messages read (full messages). */ | |
98 | uint64_t in_msgs; | |
99 | /** Amount of messages enqueued (maybe written). */ | |
100 | uint64_t out_msgs; | |
101 | ||
102 | TAILQ_ENTRY(bfd_dplane_ctx) entry; | |
103 | }; | |
104 | ||
105 | /** | |
106 | * Callback type for `bfd_dplane_expect`. \see bfd_dplane_expect. | |
107 | */ | |
108 | typedef void (*bfd_dplane_expect_cb)(struct bfddp_message *msg, void *arg); | |
109 | ||
6655b43d RZ |
110 | static int bfd_dplane_client_connect(struct thread *t); |
111 | static bool bfd_dplane_client_connecting(struct bfd_dplane_ctx *bdc); | |
230aefe2 | 112 | static void bfd_dplane_ctx_free(struct bfd_dplane_ctx *bdc); |
efd04d60 RZ |
113 | static int _bfd_dplane_add_session(struct bfd_dplane_ctx *bdc, |
114 | struct bfd_session *bs); | |
230aefe2 RZ |
115 | |
116 | /* | |
117 | * BFD data plane helper functions. | |
118 | */ | |
119 | static const char *bfd_dplane_messagetype2str(enum bfddp_message_type bmt) | |
120 | { | |
121 | switch (bmt) { | |
122 | case ECHO_REQUEST: | |
123 | return "ECHO_REQUEST"; | |
124 | case ECHO_REPLY: | |
125 | return "ECHO_REPLY"; | |
126 | case DP_ADD_SESSION: | |
127 | return "DP_ADD_SESSION"; | |
128 | case DP_DELETE_SESSION: | |
129 | return "DP_DELETE_SESSION"; | |
130 | case BFD_STATE_CHANGE: | |
131 | return "BFD_STATE_CHANGE"; | |
132 | case DP_REQUEST_SESSION_COUNTERS: | |
133 | return "DP_REQUEST_SESSION_COUNTERS"; | |
134 | case BFD_SESSION_COUNTERS: | |
135 | return "BFD_SESSION_COUNTERS"; | |
136 | default: | |
137 | return "UNKNOWN"; | |
138 | } | |
139 | } | |
140 | ||
141 | static void bfd_dplane_debug_message(const struct bfddp_message *msg) | |
142 | { | |
143 | enum bfddp_message_type bmt; | |
144 | char buf[256], addrs[256]; | |
145 | uint32_t flags; | |
146 | int rv; | |
147 | ||
148 | if (!bglobal.debug_dplane) | |
149 | return; | |
150 | ||
151 | bmt = ntohs(msg->header.type); | |
152 | zlog_debug("dplane-packet: [version=%d length=%d type=%s (%d)]", | |
153 | msg->header.version, ntohs(msg->header.length), | |
154 | bfd_dplane_messagetype2str(bmt), bmt); | |
155 | ||
156 | switch (bmt) { | |
157 | case ECHO_REPLY: | |
158 | case ECHO_REQUEST: | |
159 | zlog_debug(" [dp_time=%" PRIu64 " bfdd_time=%" PRIu64 "]", | |
f62de63c DL |
160 | be64toh(msg->data.echo.dp_time), |
161 | be64toh(msg->data.echo.bfdd_time)); | |
230aefe2 RZ |
162 | break; |
163 | ||
164 | case DP_ADD_SESSION: | |
165 | case DP_DELETE_SESSION: | |
166 | flags = ntohl(msg->data.session.flags); | |
167 | if (flags & SESSION_IPV6) | |
168 | snprintfrr(addrs, sizeof(addrs), "src=%pI6 dst=%pI6", | |
169 | &msg->data.session.src, | |
170 | &msg->data.session.dst); | |
171 | else | |
172 | snprintfrr(addrs, sizeof(addrs), "src=%pI4 dst=%pI4", | |
f1db813d DL |
173 | (struct in_addr *)&msg->data.session.src, |
174 | (struct in_addr *)&msg->data.session.dst); | |
230aefe2 RZ |
175 | |
176 | buf[0] = 0; | |
177 | if (flags & SESSION_CBIT) | |
178 | strlcat(buf, "cpi ", sizeof(buf)); | |
179 | if (flags & SESSION_ECHO) | |
180 | strlcat(buf, "echo ", sizeof(buf)); | |
181 | if (flags & SESSION_IPV6) | |
182 | strlcat(buf, "ipv6 ", sizeof(buf)); | |
183 | if (flags & SESSION_DEMAND) | |
184 | strlcat(buf, "demand ", sizeof(buf)); | |
185 | if (flags & SESSION_PASSIVE) | |
186 | strlcat(buf, "passive ", sizeof(buf)); | |
187 | if (flags & SESSION_MULTIHOP) | |
188 | strlcat(buf, "multihop ", sizeof(buf)); | |
189 | if (flags & SESSION_SHUTDOWN) | |
190 | strlcat(buf, "shutdown ", sizeof(buf)); | |
191 | ||
192 | /* Remove the last space to make things prettier. */ | |
193 | rv = (int)strlen(buf); | |
194 | if (rv > 0) | |
195 | buf[rv - 1] = 0; | |
196 | ||
197 | zlog_debug( | |
198 | " [flags=0x%08x{%s} %s ttl=%d detect_mult=%d " | |
199 | "ifindex=%d ifname=%s]", | |
200 | flags, buf, addrs, msg->data.session.ttl, | |
201 | msg->data.session.detect_mult, | |
202 | ntohl(msg->data.session.ifindex), | |
203 | msg->data.session.ifname); | |
204 | break; | |
205 | ||
206 | case BFD_STATE_CHANGE: | |
207 | buf[0] = 0; | |
208 | flags = ntohl(msg->data.state.remote_flags); | |
209 | if (flags & RBIT_CPI) | |
210 | strlcat(buf, "cbit ", sizeof(buf)); | |
211 | if (flags & RBIT_DEMAND) | |
212 | strlcat(buf, "demand ", sizeof(buf)); | |
213 | if (flags & RBIT_MP) | |
214 | strlcat(buf, "mp ", sizeof(buf)); | |
215 | ||
216 | /* Remove the last space to make things prettier. */ | |
217 | rv = (int)strlen(buf); | |
218 | if (rv > 0) | |
219 | buf[rv - 1] = 0; | |
220 | ||
221 | zlog_debug( | |
222 | " [lid=%u rid=%u flags=0x%02x{%s} state=%s " | |
223 | "diagnostics=%s mult=%d tx=%u rx=%u erx=%u]", | |
224 | ntohl(msg->data.state.lid), ntohl(msg->data.state.rid), | |
225 | flags, buf, state_list[msg->data.state.state].str, | |
226 | diag2str(msg->data.state.diagnostics), | |
227 | msg->data.state.detection_multiplier, | |
228 | ntohl(msg->data.state.desired_tx), | |
229 | ntohl(msg->data.state.required_rx), | |
230 | ntohl(msg->data.state.required_echo_rx)); | |
231 | break; | |
232 | ||
233 | case DP_REQUEST_SESSION_COUNTERS: | |
234 | zlog_debug(" [lid=%u]", ntohl(msg->data.counters_req.lid)); | |
235 | break; | |
236 | ||
237 | case BFD_SESSION_COUNTERS: | |
238 | zlog_debug( | |
239 | " [lid=%u " | |
240 | "control{in %" PRIu64 " bytes (%" PRIu64 | |
241 | " packets), " | |
242 | "out %" PRIu64 " bytes (%" PRIu64 | |
243 | " packets)} " | |
244 | "echo{in %" PRIu64 " bytes (%" PRIu64 | |
245 | " packets), " | |
246 | "out %" PRIu64 " bytes (%" PRIu64 " packets)}]", | |
247 | ntohl(msg->data.session_counters.lid), | |
f62de63c DL |
248 | be64toh(msg->data.session_counters.control_input_bytes), |
249 | be64toh(msg->data.session_counters | |
250 | .control_input_packets), | |
251 | be64toh(msg->data.session_counters | |
252 | .control_output_bytes), | |
253 | be64toh(msg->data.session_counters | |
254 | .control_output_packets), | |
255 | be64toh(msg->data.session_counters.echo_input_bytes), | |
256 | be64toh(msg->data.session_counters.echo_input_packets), | |
257 | be64toh(msg->data.session_counters.echo_output_bytes), | |
258 | be64toh(msg->data.session_counters | |
259 | .echo_output_packets)); | |
230aefe2 RZ |
260 | break; |
261 | } | |
262 | } | |
263 | ||
400632a9 RZ |
264 | /** |
265 | * Gets the next unused non zero identification. | |
266 | * | |
267 | * \param bdc the data plane context. | |
268 | * | |
269 | * \returns next usable id. | |
270 | */ | |
271 | static uint16_t bfd_dplane_next_id(struct bfd_dplane_ctx *bdc) | |
272 | { | |
273 | bdc->last_id++; | |
274 | ||
275 | /* Don't use reserved id `0`. */ | |
276 | if (bdc->last_id == 0) | |
277 | bdc->last_id = 1; | |
278 | ||
279 | return bdc->last_id; | |
280 | } | |
281 | ||
230aefe2 RZ |
282 | static ssize_t bfd_dplane_flush(struct bfd_dplane_ctx *bdc) |
283 | { | |
284 | ssize_t total = 0; | |
285 | int rv; | |
286 | ||
287 | while (STREAM_READABLE(bdc->outbuf)) { | |
288 | /* Flush buffer contents to socket. */ | |
289 | rv = stream_flush(bdc->outbuf, bdc->sock); | |
290 | if (rv == -1) { | |
291 | /* Interruption: try again. */ | |
292 | if (errno == EAGAIN || errno == EWOULDBLOCK | |
293 | || errno == EINTR) | |
294 | continue; | |
295 | ||
296 | zlog_warn("%s: socket failed: %s", __func__, | |
297 | strerror(errno)); | |
298 | bfd_dplane_ctx_free(bdc); | |
299 | return 0; | |
300 | } | |
301 | if (rv == 0) { | |
302 | if (bglobal.debug_dplane) | |
303 | zlog_info("%s: connection closed", __func__); | |
304 | ||
305 | bfd_dplane_ctx_free(bdc); | |
306 | return 0; | |
307 | } | |
308 | ||
309 | /* Account total written. */ | |
310 | total += rv; | |
311 | ||
312 | /* Account output bytes. */ | |
313 | bdc->out_bytes += (uint64_t)rv; | |
314 | ||
315 | /* Forward pointer. */ | |
316 | stream_forward_getp(bdc->outbuf, (size_t)rv); | |
317 | } | |
318 | ||
319 | /* Make more space for new data. */ | |
320 | stream_pulldown(bdc->outbuf); | |
321 | ||
322 | /* Disable write ready events. */ | |
323 | THREAD_OFF(bdc->outbufev); | |
324 | ||
325 | return total; | |
326 | } | |
327 | ||
328 | static int bfd_dplane_write(struct thread *t) | |
329 | { | |
6655b43d RZ |
330 | struct bfd_dplane_ctx *bdc = THREAD_ARG(t); |
331 | ||
332 | /* Handle connection stage. */ | |
333 | if (bdc->connecting && bfd_dplane_client_connecting(bdc)) | |
334 | return 0; | |
335 | ||
336 | bfd_dplane_flush(bdc); | |
337 | ||
230aefe2 RZ |
338 | return 0; |
339 | } | |
340 | ||
341 | static void | |
342 | bfd_dplane_session_state_change(struct bfd_dplane_ctx *bdc, | |
343 | const struct bfddp_state_change *state) | |
344 | { | |
345 | struct bfd_session *bs; | |
346 | uint32_t flags; | |
347 | int old_state; | |
348 | ||
349 | /* Look up session. */ | |
350 | bs = bfd_id_lookup(ntohl(state->lid)); | |
351 | if (bs == NULL) { | |
352 | if (bglobal.debug_dplane) | |
353 | zlog_debug("%s: failed to find session to update", | |
354 | __func__); | |
355 | return; | |
356 | } | |
357 | ||
358 | flags = ntohl(state->remote_flags); | |
359 | old_state = bs->ses_state; | |
360 | ||
361 | /* Update session state. */ | |
362 | bs->ses_state = state->state; | |
363 | bs->remote_diag = state->diagnostics; | |
364 | bs->discrs.remote_discr = ntohl(state->rid); | |
365 | bs->remote_cbit = !!(flags & RBIT_CPI); | |
366 | bs->remote_detect_mult = state->detection_multiplier; | |
367 | bs->remote_timers.desired_min_tx = ntohl(state->desired_tx); | |
368 | bs->remote_timers.required_min_rx = ntohl(state->required_rx); | |
369 | bs->remote_timers.required_min_echo = ntohl(state->required_echo_rx); | |
370 | ||
371 | /* Notify and update counters. */ | |
372 | control_notify(bs, bs->ses_state); | |
373 | ||
374 | /* No state change. */ | |
375 | if (old_state == bs->ses_state) | |
376 | return; | |
377 | ||
378 | switch (bs->ses_state) { | |
379 | case PTM_BFD_ADM_DOWN: | |
380 | case PTM_BFD_DOWN: | |
381 | /* Both states mean down. */ | |
382 | if (old_state == PTM_BFD_ADM_DOWN || old_state == PTM_BFD_DOWN) | |
383 | break; | |
384 | ||
385 | monotime(&bs->downtime); | |
386 | bs->stats.session_down++; | |
387 | break; | |
388 | case PTM_BFD_UP: | |
389 | monotime(&bs->uptime); | |
390 | bs->stats.session_up++; | |
391 | break; | |
392 | case PTM_BFD_INIT: | |
393 | /* NOTHING */ | |
394 | break; | |
395 | ||
396 | default: | |
397 | zlog_warn("%s: unhandled new state %d", __func__, | |
398 | bs->ses_state); | |
399 | break; | |
400 | } | |
401 | ||
402 | if (bglobal.debug_peer_event) | |
403 | zlog_debug("state-change: [data plane: %s] %s -> %s", | |
404 | bs_to_string(bs), state_list[old_state].str, | |
405 | state_list[bs->ses_state].str); | |
406 | } | |
407 | ||
408 | /** | |
409 | * Enqueue message in output buffer. | |
410 | * | |
411 | * \param[in,out] bdc data plane client context. | |
412 | * \param[in] buf the message to buffer. | |
413 | * \param[in] buflen the amount of bytes to buffer. | |
414 | * | |
415 | * \returns `-1` on failure (buffer full) or `0` on success. | |
416 | */ | |
417 | static int bfd_dplane_enqueue(struct bfd_dplane_ctx *bdc, const void *buf, | |
418 | size_t buflen) | |
419 | { | |
420 | size_t rlen; | |
421 | ||
6655b43d RZ |
422 | /* Handle not connected yet client. */ |
423 | if (bdc->client && bdc->sock == -1) | |
424 | return -1; | |
425 | ||
230aefe2 RZ |
426 | /* Not enough space. */ |
427 | if (buflen > STREAM_WRITEABLE(bdc->outbuf)) { | |
428 | bdc->out_fullev++; | |
429 | return -1; | |
430 | } | |
431 | ||
432 | /* Show debug message if active. */ | |
433 | bfd_dplane_debug_message((struct bfddp_message *)buf); | |
434 | ||
435 | /* Buffer the message. */ | |
436 | stream_write(bdc->outbuf, buf, buflen); | |
437 | ||
438 | /* Account message as sent. */ | |
439 | bdc->out_msgs++; | |
440 | /* Register peak buffered bytes. */ | |
441 | rlen = STREAM_READABLE(bdc->outbuf); | |
442 | if (bdc->out_bytes_peak < rlen) | |
443 | bdc->out_bytes_peak = rlen; | |
444 | ||
445 | /* Schedule if it is not yet. */ | |
446 | if (bdc->outbufev == NULL) | |
447 | thread_add_write(master, bfd_dplane_write, bdc, bdc->sock, | |
448 | &bdc->outbufev); | |
449 | ||
450 | return 0; | |
451 | } | |
452 | ||
453 | static void bfd_dplane_echo_request_handle(struct bfd_dplane_ctx *bdc, | |
454 | const struct bfddp_message *bm) | |
455 | { | |
456 | struct bfddp_message msg = {}; | |
457 | uint16_t msglen = sizeof(msg.header) + sizeof(msg.data.echo); | |
458 | struct timeval tv; | |
459 | ||
460 | gettimeofday(&tv, NULL); | |
461 | ||
462 | /* Prepare header. */ | |
463 | msg.header.version = BFD_DP_VERSION; | |
464 | msg.header.type = htons(ECHO_REPLY); | |
465 | msg.header.length = htons(msglen); | |
466 | ||
467 | /* Prepare payload. */ | |
468 | msg.data.echo.dp_time = bm->data.echo.dp_time; | |
469 | msg.data.echo.bfdd_time = | |
470 | htobe64((uint64_t)((tv.tv_sec * 1000000) + tv.tv_usec)); | |
471 | ||
472 | /* Enqueue for output. */ | |
473 | bfd_dplane_enqueue(bdc, &msg, msglen); | |
474 | } | |
475 | ||
476 | static void bfd_dplane_handle_message(struct bfddp_message *msg, void *arg) | |
477 | { | |
478 | enum bfddp_message_type bmt; | |
479 | struct bfd_dplane_ctx *bdc = arg; | |
480 | ||
481 | /* Call the appropriated handler. */ | |
482 | bmt = ntohs(msg->header.type); | |
483 | switch (bmt) { | |
484 | case ECHO_REQUEST: | |
485 | bfd_dplane_echo_request_handle(bdc, msg); | |
486 | break; | |
487 | case BFD_STATE_CHANGE: | |
488 | bfd_dplane_session_state_change(bdc, &msg->data.state); | |
489 | break; | |
490 | case ECHO_REPLY: | |
491 | /* NOTHING: we don't do anything with this information. */ | |
492 | break; | |
493 | case DP_ADD_SESSION: | |
494 | case DP_DELETE_SESSION: | |
495 | case DP_REQUEST_SESSION_COUNTERS: | |
496 | /* NOTHING: we are not supposed to receive this. */ | |
497 | break; | |
498 | case BFD_SESSION_COUNTERS: | |
499 | /* | |
500 | * NOTHING: caller of DP_REQUEST_SESSION_COUNTERS should | |
501 | * handle this with `bfd_dplane_expect`. | |
502 | */ | |
503 | break; | |
504 | ||
505 | default: | |
506 | zlog_debug("%s: unhandled message type %d", __func__, bmt); | |
507 | break; | |
508 | } | |
509 | } | |
510 | ||
511 | /** | |
512 | * Reads the socket immediately to receive data plane answer to query. | |
513 | * | |
514 | * \param bdc the data plane context. | |
515 | * \param id the message ID waiting response. | |
516 | * \param cb the callback to call when ready. | |
517 | * \param arg the callback argument. | |
518 | * | |
519 | * \return | |
520 | * `-2` on unavailability (try again), `-1` on failure or `0` on success. | |
521 | */ | |
522 | static int bfd_dplane_expect(struct bfd_dplane_ctx *bdc, uint16_t id, | |
523 | bfd_dplane_expect_cb cb, void *arg) | |
524 | { | |
525 | struct bfddp_message_header *bh; | |
526 | size_t rlen = 0, reads = 0; | |
527 | ssize_t rv; | |
528 | ||
529 | /* | |
530 | * Don't attempt to read if buffer is full, otherwise we'll get a | |
531 | * bogus 'connection closed' signal (rv == 0). | |
532 | */ | |
533 | if (bdc->inbuf->endp == bdc->inbuf->size) | |
534 | goto skip_read; | |
535 | ||
536 | read_again: | |
537 | /* Attempt to read message from client. */ | |
538 | rv = stream_read_try(bdc->inbuf, bdc->sock, | |
539 | STREAM_WRITEABLE(bdc->inbuf)); | |
540 | if (rv == 0) { | |
541 | if (bglobal.debug_dplane) | |
542 | zlog_info("%s: socket closed", __func__); | |
543 | ||
544 | bfd_dplane_ctx_free(bdc); | |
545 | return -1; | |
546 | } | |
547 | if (rv == -1) { | |
548 | zlog_warn("%s: socket failed: %s", __func__, strerror(errno)); | |
549 | bfd_dplane_ctx_free(bdc); | |
550 | return -1; | |
551 | } | |
552 | ||
553 | /* We got interrupted, reschedule read. */ | |
554 | if (rv == -2) | |
555 | return -2; | |
556 | ||
557 | /* Account read bytes. */ | |
558 | bdc->in_bytes += (uint64_t)rv; | |
559 | /* Register peak buffered bytes. */ | |
560 | rlen = STREAM_READABLE(bdc->inbuf); | |
561 | if (bdc->in_bytes_peak < rlen) | |
562 | bdc->in_bytes_peak = rlen; | |
563 | ||
564 | skip_read: | |
565 | while (rlen > 0) { | |
566 | bh = (struct bfddp_message_header *)stream_pnt(bdc->inbuf); | |
567 | /* Not enough data read. */ | |
568 | if (ntohs(bh->length) > rlen) | |
569 | goto read_again; | |
570 | ||
571 | /* Account full message read. */ | |
572 | bdc->in_msgs++; | |
573 | ||
574 | /* Account this message as whole read for buffer reorganize. */ | |
575 | reads++; | |
576 | ||
577 | /* Check for bad version. */ | |
578 | if (bh->version != BFD_DP_VERSION) { | |
579 | zlog_err("%s: bad data plane client version: %d", | |
580 | __func__, bh->version); | |
581 | return -1; | |
582 | } | |
583 | ||
584 | /* Show debug message if active. */ | |
585 | bfd_dplane_debug_message((struct bfddp_message *)bh); | |
586 | ||
587 | /* | |
588 | * Handle incoming message with callback if the ID matches, | |
589 | * otherwise fallback to default handler. | |
590 | */ | |
591 | if (id && ntohs(bh->id) == id) | |
592 | cb((struct bfddp_message *)bh, arg); | |
593 | else | |
594 | bfd_dplane_handle_message((struct bfddp_message *)bh, | |
595 | bdc); | |
596 | ||
597 | /* Advance current read pointer. */ | |
598 | stream_forward_getp(bdc->inbuf, ntohs(bh->length)); | |
599 | ||
600 | /* Reduce the buffer available bytes. */ | |
601 | rlen -= ntohs(bh->length); | |
602 | ||
603 | /* Reorganize buffer to handle more bytes read. */ | |
604 | if (reads >= 3) { | |
605 | stream_pulldown(bdc->inbuf); | |
606 | reads = 0; | |
607 | } | |
608 | ||
609 | /* We found the message, return to caller. */ | |
610 | if (id && ntohs(bh->id) == id) | |
611 | break; | |
612 | } | |
613 | ||
614 | return 0; | |
615 | } | |
616 | ||
617 | static int bfd_dplane_read(struct thread *t) | |
618 | { | |
619 | struct bfd_dplane_ctx *bdc = THREAD_ARG(t); | |
620 | int rv; | |
621 | ||
622 | rv = bfd_dplane_expect(bdc, 0, bfd_dplane_handle_message, NULL); | |
623 | if (rv == -1) | |
624 | return 0; | |
625 | ||
626 | stream_pulldown(bdc->inbuf); | |
627 | thread_add_read(master, bfd_dplane_read, bdc, bdc->sock, &bdc->inbufev); | |
628 | return 0; | |
629 | } | |
630 | ||
efd04d60 RZ |
631 | static void _bfd_session_register_dplane(struct hash_bucket *hb, void *arg) |
632 | { | |
633 | struct bfd_session *bs = hb->data; | |
634 | struct bfd_dplane_ctx *bdc = arg; | |
635 | ||
636 | if (bs->bdc != NULL) | |
637 | return; | |
638 | ||
639 | /* Disable software session. */ | |
640 | bfd_session_disable(bs); | |
641 | ||
642 | /* Move session to data plane. */ | |
643 | _bfd_dplane_add_session(bdc, bs); | |
644 | } | |
645 | ||
230aefe2 RZ |
646 | static struct bfd_dplane_ctx *bfd_dplane_ctx_new(int sock) |
647 | { | |
648 | struct bfd_dplane_ctx *bdc; | |
649 | ||
650 | bdc = XCALLOC(MTYPE_BFDD_DPLANE_CTX, sizeof(*bdc)); | |
230aefe2 RZ |
651 | |
652 | bdc->sock = sock; | |
653 | bdc->inbuf = stream_new(BFD_DPLANE_CLIENT_BUF_SIZE); | |
654 | bdc->outbuf = stream_new(BFD_DPLANE_CLIENT_BUF_SIZE); | |
6655b43d RZ |
655 | |
656 | /* If not socket ready, skip read and session registration. */ | |
657 | if (sock == -1) | |
658 | return bdc; | |
659 | ||
230aefe2 RZ |
660 | thread_add_read(master, bfd_dplane_read, bdc, sock, &bdc->inbufev); |
661 | ||
efd04d60 RZ |
662 | /* Register all unattached sessions. */ |
663 | bfd_key_iterate(_bfd_session_register_dplane, bdc); | |
664 | ||
230aefe2 RZ |
665 | return bdc; |
666 | } | |
667 | ||
668 | static void _bfd_session_unregister_dplane(struct hash_bucket *hb, void *arg) | |
669 | { | |
670 | struct bfd_session *bs = hb->data; | |
671 | struct bfd_dplane_ctx *bdc = arg; | |
672 | ||
673 | if (bs->bdc != bdc) | |
674 | return; | |
675 | ||
676 | bs->bdc = NULL; | |
efd04d60 RZ |
677 | |
678 | /* Fallback to software. */ | |
679 | bfd_session_enable(bs); | |
230aefe2 RZ |
680 | } |
681 | ||
682 | static void bfd_dplane_ctx_free(struct bfd_dplane_ctx *bdc) | |
683 | { | |
684 | if (bglobal.debug_dplane) | |
685 | zlog_debug("%s: terminating data plane client %d", __func__, | |
686 | bdc->sock); | |
687 | ||
6655b43d RZ |
688 | /* Client mode has special treatment. */ |
689 | if (bdc->client) { | |
690 | /* Disable connection event if any. */ | |
691 | THREAD_OFF(bdc->connectev); | |
692 | ||
693 | /* Normal treatment on shutdown. */ | |
694 | if (bglobal.bg_shutdown) | |
695 | goto free_resources; | |
696 | ||
697 | /* Attempt reconnection. */ | |
698 | socket_close(&bdc->sock); | |
699 | THREAD_OFF(bdc->inbufev); | |
700 | THREAD_OFF(bdc->outbufev); | |
701 | thread_add_timer(master, bfd_dplane_client_connect, bdc, 3, | |
702 | &bdc->connectev); | |
703 | return; | |
704 | } | |
705 | ||
706 | free_resources: | |
230aefe2 RZ |
707 | /* Remove from the list of attached data planes. */ |
708 | TAILQ_REMOVE(&bglobal.bg_dplaneq, bdc, entry); | |
709 | ||
710 | /* Detach all associated sessions. */ | |
711 | if (bglobal.bg_shutdown == false) | |
712 | bfd_key_iterate(_bfd_session_unregister_dplane, bdc); | |
713 | ||
714 | /* Free resources. */ | |
715 | socket_close(&bdc->sock); | |
716 | stream_free(bdc->inbuf); | |
717 | stream_free(bdc->outbuf); | |
718 | THREAD_OFF(bdc->inbufev); | |
719 | THREAD_OFF(bdc->outbufev); | |
720 | XFREE(MTYPE_BFDD_DPLANE_CTX, bdc); | |
721 | } | |
722 | ||
efd04d60 RZ |
723 | static void _bfd_dplane_session_fill(const struct bfd_session *bs, |
724 | struct bfddp_message *msg) | |
725 | { | |
726 | uint16_t msglen = sizeof(msg->header) + sizeof(msg->data.session); | |
727 | ||
728 | /* Message header. */ | |
729 | msg->header.version = BFD_DP_VERSION; | |
730 | msg->header.length = ntohs(msglen); | |
731 | msg->header.type = ntohs(DP_ADD_SESSION); | |
732 | ||
733 | /* Message payload. */ | |
734 | msg->data.session.dst = bs->key.peer; | |
735 | msg->data.session.src = bs->key.local; | |
736 | msg->data.session.detect_mult = bs->detect_mult; | |
737 | ||
738 | if (bs->ifp) { | |
739 | msg->data.session.ifindex = htonl(bs->ifp->ifindex); | |
740 | strlcpy(msg->data.session.ifname, bs->ifp->name, | |
741 | sizeof(msg->data.session.ifname)); | |
742 | } | |
743 | if (bs->flags & BFD_SESS_FLAG_MH) { | |
744 | msg->data.session.flags |= SESSION_MULTIHOP; | |
745 | msg->data.session.ttl = bs->mh_ttl; | |
746 | } else | |
747 | msg->data.session.ttl = BFD_TTL_VAL; | |
748 | ||
749 | if (bs->flags & BFD_SESS_FLAG_IPV6) | |
750 | msg->data.session.flags |= SESSION_IPV6; | |
751 | if (bs->flags & BFD_SESS_FLAG_ECHO) | |
752 | msg->data.session.flags |= SESSION_ECHO; | |
753 | if (bs->flags & BFD_SESS_FLAG_CBIT) | |
754 | msg->data.session.flags |= SESSION_CBIT; | |
755 | if (bs->flags & BFD_SESS_FLAG_PASSIVE) | |
756 | msg->data.session.flags |= SESSION_PASSIVE; | |
757 | if (bs->flags & BFD_SESS_FLAG_SHUTDOWN) | |
758 | msg->data.session.flags |= SESSION_SHUTDOWN; | |
759 | ||
760 | msg->data.session.flags = htonl(msg->data.session.flags); | |
761 | msg->data.session.lid = htonl(bs->discrs.my_discr); | |
762 | msg->data.session.min_tx = htonl(bs->timers.desired_min_tx); | |
763 | msg->data.session.min_rx = htonl(bs->timers.required_min_rx); | |
4df3e31c IR |
764 | msg->data.session.min_echo_tx = htonl(bs->timers.desired_min_echo_tx); |
765 | msg->data.session.min_echo_rx = htonl(bs->timers.required_min_echo_rx); | |
efd04d60 RZ |
766 | } |
767 | ||
768 | static int _bfd_dplane_add_session(struct bfd_dplane_ctx *bdc, | |
769 | struct bfd_session *bs) | |
770 | { | |
771 | int rv; | |
772 | ||
773 | /* Associate session. */ | |
774 | bs->bdc = bdc; | |
775 | ||
776 | /* Reset previous state. */ | |
777 | bs->remote_diag = 0; | |
778 | bs->local_diag = 0; | |
779 | bs->ses_state = PTM_BFD_DOWN; | |
780 | ||
781 | /* Enqueue message to data plane client. */ | |
782 | rv = bfd_dplane_update_session(bs); | |
783 | if (rv != 0) | |
784 | bs->bdc = NULL; | |
785 | ||
786 | return rv; | |
787 | } | |
788 | ||
400632a9 RZ |
789 | static void _bfd_dplane_update_session_counters(struct bfddp_message *msg, |
790 | void *arg) | |
791 | { | |
792 | struct bfd_session *bs = arg; | |
793 | ||
794 | bs->stats.rx_ctrl_pkt = | |
795 | be64toh(msg->data.session_counters.control_input_packets); | |
796 | bs->stats.tx_ctrl_pkt = | |
797 | be64toh(msg->data.session_counters.control_output_packets); | |
798 | bs->stats.rx_echo_pkt = | |
799 | be64toh(msg->data.session_counters.echo_input_packets); | |
800 | bs->stats.tx_echo_pkt = | |
801 | be64toh(msg->data.session_counters.echo_output_bytes); | |
802 | } | |
803 | ||
804 | /** | |
805 | * Send message to data plane requesting the session counters. | |
806 | * | |
807 | * \param bs the BFD session. | |
808 | * | |
809 | * \returns `0` on failure or the request id. | |
810 | */ | |
811 | static uint16_t bfd_dplane_request_counters(const struct bfd_session *bs) | |
812 | { | |
813 | struct bfddp_message msg = {}; | |
814 | size_t msglen = sizeof(msg.header) + sizeof(msg.data.counters_req); | |
815 | ||
816 | /* Fill header information. */ | |
817 | msg.header.version = BFD_DP_VERSION; | |
818 | msg.header.length = htons(msglen); | |
819 | msg.header.type = htons(DP_REQUEST_SESSION_COUNTERS); | |
820 | msg.header.id = htons(bfd_dplane_next_id(bs->bdc)); | |
821 | ||
822 | /* Session to get counters. */ | |
823 | msg.data.counters_req.lid = htonl(bs->discrs.my_discr); | |
824 | ||
825 | /* If enqueue failed, let caller know. */ | |
826 | if (bfd_dplane_enqueue(bs->bdc, &msg, msglen) == -1) | |
827 | return 0; | |
828 | ||
829 | /* Flush socket. */ | |
830 | bfd_dplane_flush(bs->bdc); | |
831 | ||
832 | return ntohs(msg.header.id); | |
833 | } | |
834 | ||
230aefe2 RZ |
835 | /* |
836 | * Data plane listening socket. | |
837 | */ | |
838 | static int bfd_dplane_accept(struct thread *t) | |
839 | { | |
840 | struct bfd_global *bg = THREAD_ARG(t); | |
841 | struct bfd_dplane_ctx *bdc; | |
842 | int sock; | |
843 | ||
844 | /* Accept new connection. */ | |
845 | sock = accept(bg->bg_dplane_sock, NULL, 0); | |
846 | if (sock == -1) { | |
847 | zlog_warn("%s: accept failed: %s", __func__, strerror(errno)); | |
848 | goto reschedule_and_return; | |
849 | } | |
850 | ||
851 | /* Create and handle new connection. */ | |
852 | bdc = bfd_dplane_ctx_new(sock); | |
853 | TAILQ_INSERT_TAIL(&bglobal.bg_dplaneq, bdc, entry); | |
854 | ||
855 | if (bglobal.debug_dplane) | |
856 | zlog_debug("%s: new data plane client connected", __func__); | |
857 | ||
858 | reschedule_and_return: | |
859 | thread_add_read(master, bfd_dplane_accept, bg, bg->bg_dplane_sock, | |
860 | &bglobal.bg_dplane_sockev); | |
861 | return 0; | |
862 | } | |
863 | ||
6655b43d RZ |
864 | /* |
865 | * Data plane connecting socket. | |
866 | */ | |
867 | static void _bfd_dplane_client_bootstrap(struct bfd_dplane_ctx *bdc) | |
868 | { | |
869 | bdc->connecting = false; | |
870 | ||
871 | /* Clean up buffers. */ | |
872 | stream_reset(bdc->inbuf); | |
873 | stream_reset(bdc->outbuf); | |
874 | ||
875 | /* Ask for read notifications. */ | |
876 | thread_add_read(master, bfd_dplane_read, bdc, bdc->sock, &bdc->inbufev); | |
877 | ||
878 | /* Remove all sessions then register again to send them all. */ | |
879 | bfd_key_iterate(_bfd_session_unregister_dplane, bdc); | |
880 | bfd_key_iterate(_bfd_session_register_dplane, bdc); | |
881 | } | |
882 | ||
883 | static bool bfd_dplane_client_connecting(struct bfd_dplane_ctx *bdc) | |
884 | { | |
885 | int rv; | |
886 | socklen_t rvlen = sizeof(rv); | |
887 | ||
888 | /* Make sure `errno` is reset, then test `getsockopt` success. */ | |
889 | errno = 0; | |
890 | if (getsockopt(bdc->sock, SOL_SOCKET, SO_ERROR, &rv, &rvlen) == -1) | |
891 | rv = -1; | |
892 | ||
893 | /* Connection successful. */ | |
894 | if (rv == 0) { | |
895 | if (bglobal.debug_dplane) | |
896 | zlog_debug("%s: connected to server: %d", __func__, | |
897 | bdc->sock); | |
898 | ||
899 | _bfd_dplane_client_bootstrap(bdc); | |
900 | return false; | |
901 | } | |
902 | ||
903 | switch (rv) { | |
904 | case EINTR: | |
905 | case EAGAIN: | |
906 | case EALREADY: | |
907 | case EINPROGRESS: | |
908 | /* non error, wait more. */ | |
909 | return true; | |
910 | ||
911 | default: | |
912 | zlog_warn("%s: connection failed: %s", __func__, | |
913 | strerror(errno)); | |
914 | bfd_dplane_ctx_free(bdc); | |
915 | return true; | |
916 | } | |
917 | } | |
918 | ||
919 | static int bfd_dplane_client_connect(struct thread *t) | |
920 | { | |
921 | struct bfd_dplane_ctx *bdc = THREAD_ARG(t); | |
922 | int rv, sock; | |
923 | socklen_t rvlen = sizeof(rv); | |
924 | ||
925 | /* Allocate new socket. */ | |
926 | sock = socket(bdc->addr.sa.sa_family, SOCK_STREAM, 0); | |
927 | if (sock == -1) { | |
928 | zlog_warn("%s: failed to initialize socket: %s", __func__, | |
929 | strerror(errno)); | |
930 | goto reschedule_connect; | |
931 | } | |
932 | ||
933 | /* Set non blocking socket. */ | |
934 | set_nonblocking(sock); | |
935 | ||
936 | /* Set 'no delay' (disables nagle algorithm) for IPv4/IPv6. */ | |
937 | rv = 1; | |
938 | if (bdc->addr.sa.sa_family != AF_UNIX | |
939 | && setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &rv, rvlen) == -1) | |
940 | zlog_warn("%s: TCP_NODELAY: %s", __func__, strerror(errno)); | |
941 | ||
942 | /* Attempt to connect. */ | |
943 | rv = connect(sock, &bdc->addr.sa, bdc->addrlen); | |
944 | if (rv == -1 && (errno != EINPROGRESS && errno != EAGAIN)) { | |
945 | zlog_warn("%s: data plane connection failed: %s", __func__, | |
946 | strerror(errno)); | |
947 | goto reschedule_connect; | |
948 | } | |
949 | ||
950 | bdc->sock = sock; | |
951 | if (rv == -1) { | |
952 | if (bglobal.debug_dplane) | |
953 | zlog_debug("%s: server connection in progress: %d", | |
954 | __func__, sock); | |
955 | ||
956 | /* If we are not connected yet, ask for write notifications. */ | |
957 | bdc->connecting = true; | |
958 | thread_add_write(master, bfd_dplane_write, bdc, bdc->sock, | |
959 | &bdc->outbufev); | |
960 | } else { | |
961 | if (bglobal.debug_dplane) | |
962 | zlog_debug("%s: server connection: %d", __func__, sock); | |
963 | ||
964 | /* Otherwise just start accepting data. */ | |
965 | _bfd_dplane_client_bootstrap(bdc); | |
966 | } | |
967 | ||
968 | return 0; | |
969 | ||
970 | reschedule_connect: | |
971 | THREAD_OFF(bdc->inbufev); | |
972 | THREAD_OFF(bdc->outbufev); | |
973 | socket_close(&sock); | |
974 | thread_add_timer(master, bfd_dplane_client_connect, bdc, 3, | |
975 | &bdc->connectev); | |
976 | return 0; | |
977 | } | |
978 | ||
979 | static void bfd_dplane_client_init(const struct sockaddr *sa, socklen_t salen) | |
980 | { | |
981 | struct bfd_dplane_ctx *bdc; | |
982 | ||
983 | /* Allocate context and copy address for reconnection. */ | |
984 | bdc = bfd_dplane_ctx_new(-1); | |
985 | if (salen <= sizeof(bdc->addr)) { | |
986 | memcpy(&bdc->addr, sa, salen); | |
987 | bdc->addrlen = sizeof(bdc->addr); | |
988 | } else { | |
989 | memcpy(&bdc->addr, sa, sizeof(bdc->addr)); | |
990 | bdc->addrlen = sizeof(bdc->addr); | |
991 | zlog_warn("%s: server address truncated (from %d to %d)", | |
992 | __func__, salen, bdc->addrlen); | |
993 | } | |
994 | ||
995 | bdc->client = true; | |
996 | ||
997 | thread_add_timer(master, bfd_dplane_client_connect, bdc, 0, | |
998 | &bdc->connectev); | |
999 | ||
1000 | /* Insert into data plane lists. */ | |
1001 | TAILQ_INSERT_TAIL(&bglobal.bg_dplaneq, bdc, entry); | |
1002 | } | |
1003 | ||
230aefe2 RZ |
1004 | /** |
1005 | * Termination phase of the distributed BFD infrastructure: free all allocated | |
1006 | * resources. | |
1007 | */ | |
1008 | static int bfd_dplane_finish_late(void) | |
1009 | { | |
1010 | struct bfd_dplane_ctx *bdc; | |
1011 | ||
1012 | if (bglobal.debug_dplane) | |
1013 | zlog_debug("%s: terminating distributed BFD", __func__); | |
1014 | ||
1015 | /* Free all data plane client contexts. */ | |
1016 | while ((bdc = TAILQ_FIRST(&bglobal.bg_dplaneq)) != NULL) | |
1017 | bfd_dplane_ctx_free(bdc); | |
1018 | ||
1019 | /* Cancel accept thread and close socket. */ | |
1020 | THREAD_OFF(bglobal.bg_dplane_sockev); | |
1021 | close(bglobal.bg_dplane_sock); | |
1022 | ||
1023 | return 0; | |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * Data plane exported functions. | |
1028 | */ | |
6655b43d | 1029 | void bfd_dplane_init(const struct sockaddr *sa, socklen_t salen, bool client) |
230aefe2 RZ |
1030 | { |
1031 | int sock; | |
1032 | ||
1033 | zlog_info("initializing distributed BFD"); | |
1034 | ||
6655b43d RZ |
1035 | /* Initialize queue header. */ |
1036 | TAILQ_INIT(&bglobal.bg_dplaneq); | |
1037 | ||
1038 | /* Initialize listening socket. */ | |
1039 | bglobal.bg_dplane_sock = -1; | |
1040 | ||
1041 | /* Observe shutdown events. */ | |
1042 | hook_register(frr_fini, bfd_dplane_finish_late); | |
1043 | ||
1044 | /* Handle client mode. */ | |
1045 | if (client) { | |
1046 | bfd_dplane_client_init(sa, salen); | |
1047 | return; | |
1048 | } | |
1049 | ||
230aefe2 RZ |
1050 | /* |
1051 | * Data plane socket creation: | |
1052 | * - Set REUSEADDR option for taking over previously open socket. | |
1053 | * - Bind to address requested (maybe IPv4, IPv6, UNIX etc...). | |
1054 | * - Listen on that address for new connections. | |
1055 | * - Ask to be waken up when a new connection comes. | |
1056 | */ | |
1057 | sock = socket(sa->sa_family, SOCK_STREAM, 0); | |
1058 | if (sock == -1) { | |
1059 | zlog_warn("%s: failed to initialize socket: %s", __func__, | |
1060 | strerror(errno)); | |
1061 | return; | |
1062 | } | |
1063 | ||
1064 | if (sockopt_reuseaddr(sock) == -1) { | |
1065 | zlog_warn("%s: failed to set reuseaddr: %s", __func__, | |
1066 | strerror(errno)); | |
1067 | close(sock); | |
1068 | return; | |
1069 | } | |
1070 | ||
1071 | /* Handle UNIX socket: delete previous socket if any. */ | |
1072 | if (sa->sa_family == AF_UNIX) | |
1073 | unlink(((struct sockaddr_un *)sa)->sun_path); | |
1074 | ||
1075 | if (bind(sock, sa, salen) == -1) { | |
1076 | zlog_warn("%s: failed to bind socket: %s", __func__, | |
1077 | strerror(errno)); | |
1078 | close(sock); | |
1079 | return; | |
1080 | } | |
1081 | ||
1082 | if (listen(sock, SOMAXCONN) == -1) { | |
1083 | zlog_warn("%s: failed to put socket on listen: %s", __func__, | |
1084 | strerror(errno)); | |
1085 | close(sock); | |
1086 | return; | |
1087 | } | |
1088 | ||
1089 | bglobal.bg_dplane_sock = sock; | |
1090 | thread_add_read(master, bfd_dplane_accept, &bglobal, sock, | |
1091 | &bglobal.bg_dplane_sockev); | |
230aefe2 | 1092 | } |
efd04d60 RZ |
1093 | |
1094 | int bfd_dplane_add_session(struct bfd_session *bs) | |
1095 | { | |
1096 | struct bfd_dplane_ctx *bdc; | |
1097 | ||
1098 | /* Select a data plane client to install session. */ | |
1099 | TAILQ_FOREACH (bdc, &bglobal.bg_dplaneq, entry) { | |
1100 | if (_bfd_dplane_add_session(bdc, bs) == 0) | |
1101 | return 0; | |
1102 | } | |
1103 | ||
1104 | return -1; | |
1105 | } | |
1106 | ||
1107 | int bfd_dplane_update_session(const struct bfd_session *bs) | |
1108 | { | |
1109 | struct bfddp_message msg = {}; | |
1110 | ||
1111 | if (bs->bdc == NULL) | |
1112 | return 0; | |
1113 | ||
1114 | _bfd_dplane_session_fill(bs, &msg); | |
1115 | ||
1116 | /* Enqueue message to data plane client. */ | |
1117 | return bfd_dplane_enqueue(bs->bdc, &msg, ntohs(msg.header.length)); | |
1118 | } | |
1119 | ||
1120 | int bfd_dplane_delete_session(struct bfd_session *bs) | |
1121 | { | |
1122 | struct bfddp_message msg = {}; | |
1123 | int rv; | |
1124 | ||
1125 | /* Not using data plane, just return success. */ | |
1126 | if (bs->bdc == NULL) | |
1127 | return 0; | |
1128 | ||
1129 | /* Fill most of the common fields. */ | |
1130 | _bfd_dplane_session_fill(bs, &msg); | |
1131 | ||
1132 | /* Change the message type. */ | |
1133 | msg.header.type = ntohs(DP_DELETE_SESSION); | |
1134 | ||
1135 | /* Enqueue message to data plane client. */ | |
1136 | rv = bfd_dplane_enqueue(bs->bdc, &msg, ntohs(msg.header.length)); | |
1137 | ||
1138 | /* Remove association. */ | |
1139 | bs->bdc = NULL; | |
1140 | ||
1141 | return rv; | |
1142 | } | |
400632a9 RZ |
1143 | |
1144 | /* | |
1145 | * Data plane CLI. | |
1146 | */ | |
1147 | void bfd_dplane_show_counters(struct vty *vty) | |
1148 | { | |
1149 | struct bfd_dplane_ctx *bdc; | |
1150 | ||
1151 | #define SHOW_COUNTER(label, counter, formatter) \ | |
1152 | vty_out(vty, "%28s: %" formatter "\n", (label), (counter)) | |
1153 | ||
1154 | vty_out(vty, "%28s\n%28s\n", "Data plane", "=========="); | |
1155 | TAILQ_FOREACH (bdc, &bglobal.bg_dplaneq, entry) { | |
1156 | SHOW_COUNTER("File descriptor", bdc->sock, "d"); | |
1157 | SHOW_COUNTER("Input bytes", bdc->in_bytes, PRIu64); | |
1158 | SHOW_COUNTER("Input bytes peak", bdc->in_bytes_peak, PRIu64); | |
1159 | SHOW_COUNTER("Input messages", bdc->in_msgs, PRIu64); | |
1160 | SHOW_COUNTER("Input current usage", STREAM_READABLE(bdc->inbuf), | |
1161 | "zu"); | |
1162 | SHOW_COUNTER("Output bytes", bdc->out_bytes, PRIu64); | |
1163 | SHOW_COUNTER("Output bytes peak", bdc->out_bytes_peak, PRIu64); | |
1164 | SHOW_COUNTER("Output messages", bdc->out_msgs, PRIu64); | |
1165 | SHOW_COUNTER("Output full events", bdc->out_fullev, PRIu64); | |
1166 | SHOW_COUNTER("Output current usage", | |
1167 | STREAM_READABLE(bdc->inbuf), "zu"); | |
1168 | vty_out(vty, "\n"); | |
1169 | } | |
1170 | #undef SHOW_COUNTER | |
1171 | } | |
1172 | ||
1173 | int bfd_dplane_update_session_counters(struct bfd_session *bs) | |
1174 | { | |
1175 | uint16_t id; | |
1176 | int rv; | |
1177 | ||
1178 | /* If session is not using data plane, then just return success. */ | |
1179 | if (bs->bdc == NULL) | |
1180 | return 0; | |
1181 | ||
1182 | /* Make the request. */ | |
1183 | id = bfd_dplane_request_counters(bs); | |
1184 | if (id == 0) { | |
1185 | zlog_debug("%s: counters request failed", __func__); | |
1186 | return -1; | |
1187 | } | |
1188 | ||
1189 | /* Handle interruptions. */ | |
1190 | do { | |
1191 | rv = bfd_dplane_expect(bs->bdc, id, | |
1192 | _bfd_dplane_update_session_counters, bs); | |
1193 | } while (rv == -2); | |
1194 | ||
1195 | return rv; | |
1196 | } |