]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfd.c
bfdd: slow down on peer connection loss
[mirror_frr.git] / bfdd / bfd.c
CommitLineData
e9e2c950
RZ
1/*********************************************************************
2 * Copyright 2013 Cumulus Networks, LLC. All rights reserved.
3 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * bfd.c: implements the BFD protocol.
20 *
21 * Authors
22 * -------
23 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
24 * Kanna Rajagopal [kanna@cumulusnetworks.com]
25 * Radhika Mahankali [Radhika@cumulusnetworks.com]
26 */
27
28#include <zebra.h>
29
e9e2c950
RZ
30#include "lib/jhash.h"
31
32#include "bfd.h"
33
34DEFINE_QOBJ_TYPE(bfd_session);
35
36/*
37 * Prototypes
38 */
39static uint32_t ptm_bfd_gen_ID(void);
40static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd);
41static void bfd_session_free(struct bfd_session *bs);
42static struct bfd_session *bfd_session_new(int sd);
43static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
44 uint32_t ldisc);
45static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
46static const char *get_diag_str(int diag);
47
48
49/*
50 * Functions
51 */
52struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc)
53{
54 struct bfd_session *bs;
55 struct peer_label *pl;
56 struct bfd_mhop_key mhop;
57 struct bfd_shop_key shop;
58
59 /* Try to find label first. */
60 if (bpc->bpc_has_label) {
61 pl = pl_find(bpc->bpc_label);
62 if (pl != NULL) {
63 bs = pl->pl_bs;
64 return bs;
65 }
66 }
67
68 /* Otherwise fallback to peer/local hash lookup. */
69 if (bpc->bpc_mhop) {
70 memset(&mhop, 0, sizeof(mhop));
71 mhop.peer = bpc->bpc_peer;
72 mhop.local = bpc->bpc_local;
73 if (bpc->bpc_has_vrfname)
74 strlcpy(mhop.vrf_name, bpc->bpc_vrfname,
75 sizeof(mhop.vrf_name));
76
77 bs = bfd_mhop_lookup(mhop);
78 } else {
79 memset(&shop, 0, sizeof(shop));
80 shop.peer = bpc->bpc_peer;
8a9f760e 81 if (bpc->bpc_has_localif)
e9e2c950
RZ
82 strlcpy(shop.port_name, bpc->bpc_localif,
83 sizeof(shop.port_name));
84
85 bs = bfd_shop_lookup(shop);
86 }
87
88 return bs;
89}
90
91static uint32_t ptm_bfd_gen_ID(void)
92{
93 static uint32_t sessionID = 1;
94
95 return (sessionID++);
96}
97
98void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo)
99{
100 uint64_t jitter, xmt_TO;
101 int maxpercent;
102
103 xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO;
104
105 /*
106 * From section 6.5.2: trasmit interval should be randomly jittered
107 * between
108 * 75% and 100% of nominal value, unless detect_mult is 1, then should
109 * be
110 * between 75% and 90%.
111 */
112 maxpercent = (bfd->detect_mult == 1) ? 16 : 26;
113 jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100;
114 /* XXX remove that division above */
115
116 if (is_echo)
117 bfd_echo_xmttimer_update(bfd, jitter);
118 else
119 bfd_xmttimer_update(bfd, jitter);
120}
121
122static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd)
123{
124 /* Send the scheduled echo packet */
125 ptm_bfd_echo_snd(bfd);
126
127 /* Restart the timer for next time */
128 ptm_bfd_start_xmt_timer(bfd, true);
129}
130
131void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit)
132{
133 /* Send the scheduled control packet */
134 ptm_bfd_snd(bfd, fbit);
135
136 /* Restart the timer for next time */
137 ptm_bfd_start_xmt_timer(bfd, false);
138}
139
8bd859f6 140void ptm_bfd_echo_stop(struct bfd_session *bfd)
e9e2c950
RZ
141{
142 bfd->echo_xmt_TO = 0;
143 bfd->echo_detect_TO = 0;
144 BFD_UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
145
146 bfd_echo_xmttimer_delete(bfd);
147 bfd_echo_recvtimer_delete(bfd);
e9e2c950
RZ
148}
149
150void ptm_bfd_echo_start(struct bfd_session *bfd)
151{
152 bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO);
451eb5a2
RZ
153 if (bfd->echo_detect_TO > 0)
154 ptm_bfd_echo_xmt_TO(bfd);
e9e2c950
RZ
155}
156
157void ptm_bfd_ses_up(struct bfd_session *bfd)
158{
03e7f088
RZ
159 int old_state = bfd->ses_state;
160
e9e2c950
RZ
161 bfd->local_diag = 0;
162 bfd->ses_state = PTM_BFD_UP;
e9e2c950
RZ
163 monotime(&bfd->uptime);
164
c0ef9a8a
RZ
165 /* Connection is up, lets negotiate timers. */
166 bfd_set_polling(bfd);
167
168 /* Start sending control packets with poll bit immediately. */
169 ptm_bfd_snd(bfd, 0);
e9e2c950
RZ
170
171 control_notify(bfd);
172
0684c9b1
RZ
173 if (old_state != bfd->ses_state) {
174 bfd->stats.session_up++;
03e7f088
RZ
175 log_info("state-change: [%s] %s -> %s", bs_to_string(bfd),
176 state_list[old_state].str,
177 state_list[bfd->ses_state].str);
0684c9b1 178 }
e9e2c950
RZ
179}
180
181void ptm_bfd_ses_dn(struct bfd_session *bfd, uint8_t diag)
182{
183 int old_state = bfd->ses_state;
184
185 bfd->local_diag = diag;
186 bfd->discrs.remote_discr = 0;
187 bfd->ses_state = PTM_BFD_DOWN;
188 bfd->polling = 0;
189 bfd->demand_mode = 0;
190 monotime(&bfd->downtime);
191
192 ptm_bfd_snd(bfd, 0);
193
b912b189
RZ
194 /* Slow down the control packets, the connection is down. */
195 bs_set_slow_timers(bfd);
196
e9e2c950
RZ
197 /* only signal clients when going from up->down state */
198 if (old_state == PTM_BFD_UP)
199 control_notify(bfd);
200
e9e2c950
RZ
201 /* Stop echo packet transmission if they are active */
202 if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
8bd859f6 203 ptm_bfd_echo_stop(bfd);
03e7f088 204
0684c9b1
RZ
205 if (old_state != bfd->ses_state) {
206 bfd->stats.session_down++;
03e7f088
RZ
207 log_info("state-change: [%s] %s -> %s reason:%s",
208 bs_to_string(bfd), state_list[old_state].str,
209 state_list[bfd->ses_state].str,
210 get_diag_str(bfd->local_diag));
0684c9b1 211 }
e9e2c950
RZ
212}
213
214static int ptm_bfd_get_vrf_name(char *port_name, char *vrf_name)
215{
216 struct bfd_iface *iface;
217 struct bfd_vrf *vrf;
218
219 if ((port_name == NULL) || (vrf_name == NULL))
220 return -1;
221
222 iface = bfd_iface_lookup(port_name);
223 if (iface) {
224 vrf = bfd_vrf_lookup(iface->vrf_id);
225 if (vrf) {
226 strlcpy(vrf_name, vrf->name, sizeof(vrf->name));
227 return 0;
228 }
229 }
230 return -1;
231}
232
233static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
234 uint32_t ldisc)
235{
236 struct bfd_session *bs;
237
238 bs = bfd_id_lookup(ldisc);
239 if (bs == NULL)
240 return NULL;
241
242 /* Remove unused fields. */
243 switch (sa->sa_sin.sin_family) {
244 case AF_INET:
245 sa->sa_sin.sin_port = 0;
246 if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin)) == 0)
247 return bs;
248 break;
249 case AF_INET6:
250 sa->sa_sin6.sin6_port = 0;
251 if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin6)) == 0)
252 return bs;
253 break;
254 }
255
256 return NULL;
257}
258
259struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, char *port_name,
260 struct sockaddr_any *peer,
261 struct sockaddr_any *local,
262 char *vrf_name, bool is_mhop)
263{
264 struct bfd_session *l_bfd = NULL;
265 struct bfd_mhop_key mhop;
266 struct bfd_shop_key shop;
9d63adda 267 char vrf_buf[MAXNAMELEN];
e9e2c950 268
9d63adda
RZ
269 /* Find our session using the ID signaled by the remote end. */
270 if (cp->discrs.remote_discr)
271 return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr));
272
273 /* Search for session without using discriminator. */
274 if (is_mhop) {
275 memset(&mhop, 0, sizeof(mhop));
276 mhop.peer = *peer;
277 mhop.local = *local;
278 if (vrf_name && vrf_name[0]) {
279 strlcpy(mhop.vrf_name, vrf_name, sizeof(mhop.vrf_name));
280 } else if (port_name && port_name[0]) {
281 memset(vrf_buf, 0, sizeof(vrf_buf));
282 if (ptm_bfd_get_vrf_name(port_name, vrf_buf) != -1)
03e7f088
RZ
283 strlcpy(mhop.vrf_name, vrf_buf,
284 sizeof(mhop.vrf_name));
e9e2c950
RZ
285 }
286
9d63adda
RZ
287 l_bfd = bfd_mhop_lookup(mhop);
288 } else {
289 memset(&shop, 0, sizeof(shop));
290 shop.peer = *peer;
291 if (port_name && port_name[0])
03e7f088
RZ
292 strlcpy(shop.port_name, port_name,
293 sizeof(shop.port_name));
9d63adda
RZ
294
295 l_bfd = bfd_shop_lookup(shop);
e9e2c950
RZ
296 }
297
9d63adda
RZ
298 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
299 return l_bfd;
e9e2c950
RZ
300}
301
e9e2c950
RZ
302int bfd_xmt_cb(struct thread *t)
303{
304 struct bfd_session *bs = THREAD_ARG(t);
305
306 ptm_bfd_xmt_TO(bs, 0);
307
308 return 0;
309}
310
311int bfd_echo_xmt_cb(struct thread *t)
312{
313 struct bfd_session *bs = THREAD_ARG(t);
314
451eb5a2
RZ
315 if (bs->echo_xmt_TO > 0)
316 ptm_bfd_echo_xmt_TO(bs);
e9e2c950
RZ
317
318 return 0;
319}
320
321/* Was ptm_bfd_detect_TO() */
322int bfd_recvtimer_cb(struct thread *t)
323{
324 struct bfd_session *bs = THREAD_ARG(t);
e9e2c950
RZ
325
326 switch (bs->ses_state) {
327 case PTM_BFD_INIT:
328 case PTM_BFD_UP:
40675ea9 329 ptm_bfd_ses_dn(bs, BD_CONTROL_EXPIRED);
e9e2c950
RZ
330 bfd_recvtimer_update(bs);
331 break;
332
333 default:
334 /* Second detect time expiration, zero remote discr (section
335 * 6.5.1)
336 */
337 bs->discrs.remote_discr = 0;
338 break;
339 }
340
e9e2c950
RZ
341 return 0;
342}
343
344/* Was ptm_bfd_echo_detect_TO() */
345int bfd_echo_recvtimer_cb(struct thread *t)
346{
347 struct bfd_session *bs = THREAD_ARG(t);
e9e2c950
RZ
348
349 switch (bs->ses_state) {
350 case PTM_BFD_INIT:
351 case PTM_BFD_UP:
40675ea9 352 ptm_bfd_ses_dn(bs, BD_ECHO_FAILED);
e9e2c950
RZ
353 break;
354 }
355
e9e2c950
RZ
356 return 0;
357}
358
359static struct bfd_session *bfd_session_new(int sd)
360{
361 struct bfd_session *bs;
362
363 bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
364 if (bs == NULL)
365 return NULL;
366
367 QOBJ_REG(bs, bfd_session);
368
f43b9368 369 bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX;
e9e2c950
RZ
370 bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX;
371 bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO;
372 bs->detect_mult = BFD_DEFDETECTMULT;
373 bs->mh_ttl = BFD_DEF_MHOP_TTL;
374
b912b189
RZ
375 /* Initiate connection with slow timers. */
376 bs_set_slow_timers(bs);
f43b9368
RZ
377
378 /* Initiate remote settings as well. */
379 bs->remote_timers = bs->cur_timers;
380 bs->remote_detect_mult = BFD_DEFDETECTMULT;
381
e9e2c950
RZ
382 bs->sock = sd;
383 monotime(&bs->uptime);
384 bs->downtime = bs->uptime;
385
386 return bs;
387}
388
389int bfd_session_update_label(struct bfd_session *bs, const char *nlabel)
390{
391 /* New label treatment:
392 * - Check if the label is taken;
393 * - Try to allocate the memory for it and register;
394 */
395 if (bs->pl == NULL) {
396 if (pl_find(nlabel) != NULL) {
397 /* Someone is already using it. */
398 return -1;
399 }
400
401 if (pl_new(nlabel, bs) == NULL)
402 return -1;
403
404 return 0;
405 }
406
407 /*
408 * Test label change consistency:
409 * - Do nothing if it's the same label;
410 * - Check if the future label is already taken;
411 * - Change label;
412 */
413 if (strcmp(nlabel, bs->pl->pl_label) == 0)
414 return -1;
415 if (pl_find(nlabel) != NULL)
416 return -1;
417
418 strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label));
419 return 0;
420}
421
422static void _bfd_session_update(struct bfd_session *bs,
423 struct bfd_peer_cfg *bpc)
424{
425 if (bpc->bpc_echo) {
426 /* Check if echo mode is already active. */
427 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
428 goto skip_echo;
429
430 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
e9e2c950
RZ
431
432 /* Activate/update echo receive timeout timer. */
73c62f8e 433 bs_echo_timer_handler(bs);
e9e2c950
RZ
434 } else {
435 /* Check if echo mode is already disabled. */
436 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
437 goto skip_echo;
438
439 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
8bd859f6 440 ptm_bfd_echo_stop(bs);
e9e2c950
RZ
441 }
442
443skip_echo:
444 if (bpc->bpc_has_txinterval)
f43b9368 445 bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000;
e9e2c950
RZ
446
447 if (bpc->bpc_has_recvinterval)
448 bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000;
449
450 if (bpc->bpc_has_detectmultiplier)
451 bs->detect_mult = bpc->bpc_detectmultiplier;
452
453 if (bpc->bpc_has_echointerval)
454 bs->timers.required_min_echo = bpc->bpc_echointerval * 1000;
455
456 if (bpc->bpc_has_label)
457 bfd_session_update_label(bs, bpc->bpc_label);
458
459 if (bpc->bpc_shutdown) {
460 /* Check if already shutdown. */
461 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
462 return;
463
464 BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
465
466 /* Disable all events. */
467 bfd_recvtimer_delete(bs);
468 bfd_echo_recvtimer_delete(bs);
469 bfd_xmttimer_delete(bs);
470 bfd_echo_xmttimer_delete(bs);
471
472 /* Change and notify state change. */
473 bs->ses_state = PTM_BFD_ADM_DOWN;
474 control_notify(bs);
475
476 ptm_bfd_snd(bs, 0);
477 } else {
478 /* Check if already working. */
479 if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN))
480 return;
481
482 BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
483
484 /* Change and notify state change. */
485 bs->ses_state = PTM_BFD_DOWN;
486 control_notify(bs);
487
488 /* Enable all timers. */
489 bfd_recvtimer_update(bs);
490 bfd_xmttimer_update(bs, bs->xmt_TO);
e9e2c950
RZ
491 }
492}
493
494static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
495{
496 /* User didn't want to update, return failure. */
497 if (bpc->bpc_createonly)
498 return -1;
499
500 _bfd_session_update(bs, bpc);
501
e9e2c950
RZ
502 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
503
504 return 0;
505}
506
507static void bfd_session_free(struct bfd_session *bs)
508{
509 if (bs->sock != -1)
510 close(bs->sock);
511
512 bfd_recvtimer_delete(bs);
513 bfd_echo_recvtimer_delete(bs);
514 bfd_xmttimer_delete(bs);
515 bfd_echo_xmttimer_delete(bs);
516
517 bfd_id_delete(bs->discrs.my_discr);
518 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH))
519 bfd_mhop_delete(bs->mhop);
520 else
521 bfd_shop_delete(bs->shop);
522
523 pl_free(bs->pl);
524
525 QOBJ_UNREG(bs);
526 XFREE(MTYPE_BFDD_CONFIG, bs);
527}
528
529struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
530{
531 struct bfd_session *bfd, *l_bfd;
80edb675 532 struct interface *ifp = NULL;
e9e2c950
RZ
533 int psock;
534
535 /* check to see if this needs a new session */
536 l_bfd = bs_peer_find(bpc);
537 if (l_bfd) {
538 /* Requesting a duplicated peer means update configuration. */
539 if (bfd_session_update(l_bfd, bpc) == 0)
540 return l_bfd;
541 else
542 return NULL;
543 }
544
80edb675
RZ
545 /*
546 * No session found, we have to allocate a new one.
547 *
548 * First a few critical checks:
549 *
550 * * Check that the specified interface exists.
551 * * Attempt to create the UDP socket (might fail if we exceed
552 * our limits).
553 */
554 if (bpc->bpc_has_localif) {
555 ifp = if_lookup_by_name(bpc->bpc_localif, VRF_DEFAULT);
556 if (ifp == NULL) {
557 log_error(
558 "session-new: specified interface doesn't exists.");
559 return NULL;
560 }
561 }
562
e9e2c950
RZ
563 /*
564 * Get socket for transmitting control packets. Note that if we
565 * could use the destination port (3784) for the source
566 * port we wouldn't need a socket per session.
567 */
568 if (bpc->bpc_ipv4) {
569 psock = bp_peer_socket(bpc);
03e7f088 570 if (psock == -1)
e9e2c950 571 return NULL;
e9e2c950
RZ
572 } else {
573 psock = bp_peer_socketv6(bpc);
03e7f088 574 if (psock == -1)
e9e2c950 575 return NULL;
e9e2c950
RZ
576 }
577
578 /* Get memory */
579 bfd = bfd_session_new(psock);
580 if (bfd == NULL) {
03e7f088 581 log_error("session-new: allocation failed");
e9e2c950
RZ
582 return NULL;
583 }
584
80edb675
RZ
585 if (bpc->bpc_has_localif && !bpc->bpc_mhop)
586 bfd->ifp = ifp;
e9e2c950 587
43adc702 588 if (bpc->bpc_ipv4 == false) {
e9e2c950
RZ
589 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6);
590
43adc702
RZ
591 /* Set the IPv6 scope id for link-local addresses. */
592 if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_local.sa_sin6.sin6_addr))
80edb675
RZ
593 bpc->bpc_local.sa_sin6.sin6_scope_id =
594 bfd->ifp != NULL ? bfd->ifp->ifindex
595 : IFINDEX_INTERNAL;
43adc702 596 if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_peer.sa_sin6.sin6_addr))
80edb675
RZ
597 bpc->bpc_peer.sa_sin6.sin6_scope_id =
598 bfd->ifp != NULL ? bfd->ifp->ifindex
599 : IFINDEX_INTERNAL;
43adc702
RZ
600 }
601
e9e2c950
RZ
602 /* Initialize the session */
603 bfd->ses_state = PTM_BFD_DOWN;
604 bfd->discrs.my_discr = ptm_bfd_gen_ID();
605 bfd->discrs.remote_discr = 0;
606 bfd->local_ip = bpc->bpc_local;
607 bfd->local_address = bpc->bpc_local;
e9e2c950 608 bfd_recvtimer_update(bfd);
f43b9368 609 ptm_bfd_start_xmt_timer(bfd, false);
e9e2c950 610
f43b9368 611 /* Registrate session into data structures. */
e9e2c950
RZ
612 bfd_id_insert(bfd);
613
614 if (bpc->bpc_mhop) {
615 BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH);
616 bfd->mhop.peer = bpc->bpc_peer;
617 bfd->mhop.local = bpc->bpc_local;
618 if (bpc->bpc_has_vrfname)
619 strlcpy(bfd->mhop.vrf_name, bpc->bpc_vrfname,
620 sizeof(bfd->mhop.vrf_name));
621
622 bfd_mhop_insert(bfd);
623 } else {
624 bfd->shop.peer = bpc->bpc_peer;
8a9f760e 625 if (bpc->bpc_has_localif)
e9e2c950
RZ
626 strlcpy(bfd->shop.port_name, bpc->bpc_localif,
627 sizeof(bfd->shop.port_name));
628
629 bfd_shop_insert(bfd);
630 }
631
e9e2c950
RZ
632 _bfd_session_update(bfd, bpc);
633
03e7f088 634 log_info("session-new: %s", bs_to_string(bfd));
e9e2c950
RZ
635
636 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd);
637
638 return bfd;
639}
640
641int ptm_bfd_ses_del(struct bfd_peer_cfg *bpc)
642{
643 struct bfd_session *bs;
644
645 /* Find session and call free(). */
646 bs = bs_peer_find(bpc);
647 if (bs == NULL)
648 return -1;
649
650 /* This pointer is being referenced, don't let it be deleted. */
651 if (bs->refcount > 0) {
03e7f088
RZ
652 log_error("session-delete: refcount failure: %" PRIu64
653 " references",
654 bs->refcount);
e9e2c950
RZ
655 return -1;
656 }
657
03e7f088 658 log_info("session-delete: %s", bs_to_string(bs));
e9e2c950
RZ
659
660 control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs);
661
662 bfd_session_free(bs);
663
664 return 0;
665}
666
d3f3a2c4
RZ
667void bfd_set_polling(struct bfd_session *bs)
668{
c0ef9a8a
RZ
669 /*
670 * Start polling procedure: the only timers that require polling
671 * to change value without losing connection are:
672 *
673 * - Desired minimum transmission interval;
674 * - Required minimum receive interval;
675 *
676 * RFC 5880, Section 6.8.3.
677 */
d3f3a2c4
RZ
678 bs->polling = 1;
679}
680
aef131af
RZ
681/*
682 * bs_<state>_handler() functions implement the BFD state machine
683 * transition mechanism. `<state>` is the current session state and
684 * the parameter `nstate` is the peer new state.
685 */
686void bs_admin_down_handler(struct bfd_session *bs, int nstate);
687void bs_down_handler(struct bfd_session *bs, int nstate);
688void bs_init_handler(struct bfd_session *bs, int nstate);
689void bs_up_handler(struct bfd_session *bs, int nstate);
690
691void bs_admin_down_handler(struct bfd_session *bs __attribute__((__unused__)),
692 int nstate __attribute__((__unused__)))
693{
694 /*
695 * We are administratively down, there is no state machine
696 * handling.
697 */
698}
699
700void bs_down_handler(struct bfd_session *bs, int nstate)
701{
702 switch (nstate) {
703 case PTM_BFD_ADM_DOWN:
704 /*
705 * Remote peer doesn't want to talk, so lets keep the
706 * connection down.
707 */
708 case PTM_BFD_UP:
709 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
710 break;
711
712 case PTM_BFD_DOWN:
713 /*
714 * Remote peer agreed that the path is down, lets try to
715 * bring it up.
716 */
717 bs->ses_state = PTM_BFD_INIT;
718 break;
719
720 case PTM_BFD_INIT:
721 /*
722 * Remote peer told us his path is up, lets turn
723 * activate the session.
724 */
725 ptm_bfd_ses_up(bs);
726 break;
727
728 default:
729 log_debug("state-change: unhandled neighbor state: %d", nstate);
730 break;
731 }
732}
733
734void bs_init_handler(struct bfd_session *bs, int nstate)
735{
736 switch (nstate) {
737 case PTM_BFD_ADM_DOWN:
738 /*
739 * Remote peer doesn't want to talk, so lets make the
740 * connection down.
741 */
742 bs->ses_state = PTM_BFD_DOWN;
743 break;
744
745 case PTM_BFD_DOWN:
746 /* Remote peer hasn't moved to first stage yet. */
747 break;
748
749 case PTM_BFD_INIT:
750 case PTM_BFD_UP:
751 /* We agreed on the settings and the path is up. */
752 ptm_bfd_ses_up(bs);
753 break;
754
755 default:
756 log_debug("state-change: unhandled neighbor state: %d", nstate);
757 break;
758 }
759}
760
761void bs_up_handler(struct bfd_session *bs, int nstate)
762{
763 switch (nstate) {
764 case PTM_BFD_ADM_DOWN:
765 case PTM_BFD_DOWN:
766 /* Peer lost or asked to shutdown connection. */
767 ptm_bfd_ses_dn(bs, BD_NEIGHBOR_DOWN);
768 break;
769
770 case PTM_BFD_INIT:
771 case PTM_BFD_UP:
772 /* Path is up and working. */
773 break;
774
775 default:
776 log_debug("state-change: unhandled neighbor state: %d", nstate);
777 break;
778 }
779}
780
781void bs_state_handler(struct bfd_session *bs, int nstate)
782{
783 switch (bs->ses_state) {
784 case PTM_BFD_ADM_DOWN:
785 bs_admin_down_handler(bs, nstate);
786 break;
787 case PTM_BFD_DOWN:
788 bs_down_handler(bs, nstate);
789 break;
790 case PTM_BFD_INIT:
791 bs_init_handler(bs, nstate);
792 break;
793 case PTM_BFD_UP:
794 bs_up_handler(bs, nstate);
795 break;
796
797 default:
798 log_debug("state-change: [%s] is in invalid state: %d",
799 bs_to_string(bs), nstate);
800 break;
801 }
802}
803
c0ef9a8a
RZ
804/*
805 * Handles echo timer manipulation after updating timer.
806 */
807void bs_echo_timer_handler(struct bfd_session *bs)
808{
809 uint32_t old_timer;
810
811 /*
812 * Before doing any echo handling, check if it is possible to
813 * use it.
814 *
815 * - Check for `echo-mode` configuration.
816 * - Check that we are not using multi hop (RFC 5883,
817 * Section 3).
818 * - Check that we are already at the up state.
819 */
820 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0
821 || BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)
822 || bs->ses_state != PTM_BFD_UP)
823 return;
824
825 /* Remote peer asked to stop echo. */
826 if (bs->remote_timers.required_min_echo == 0) {
827 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
8bd859f6 828 ptm_bfd_echo_stop(bs);
c0ef9a8a
RZ
829
830 return;
831 }
832
833 /*
834 * Calculate the echo transmission timer: we must not send
835 * echo packets faster than the minimum required time
836 * announced by the remote system.
837 *
838 * RFC 5880, Section 6.8.9.
839 */
840 old_timer = bs->echo_xmt_TO;
841 if (bs->remote_timers.required_min_echo > bs->timers.required_min_echo)
842 bs->echo_xmt_TO = bs->remote_timers.required_min_echo;
843 else
844 bs->echo_xmt_TO = bs->timers.required_min_echo;
845
846 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0
847 || old_timer != bs->echo_xmt_TO)
848 ptm_bfd_echo_start(bs);
849}
850
851/*
852 * RFC 5880 Section 6.5.
853 *
854 * When a BFD control packet with the final bit is received, we must
855 * update the session parameters.
856 */
857void bs_final_handler(struct bfd_session *bs)
858{
859 /* Start using our new timers. */
f43b9368
RZ
860 bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx;
861 bs->cur_timers.required_min_rx = bs->timers.required_min_rx;
c0ef9a8a
RZ
862
863 /*
864 * TODO: demand mode. See RFC 5880 Section 6.1.
865 *
866 * When using demand mode we must disable the detection timer
867 * for lost control packets.
868 */
869 if (bs->demand_mode) {
870 /* Notify watchers about changed timers. */
871 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
872 return;
873 }
874
875 /*
876 * Calculate detection time based on new timers.
877 *
878 * Transmission calculation:
879 * We must respect the RequiredMinRxInterval from the remote
880 * system: if our desired transmission timer is more than the
881 * minimum receive rate, then we must lower it to at least the
882 * minimum receive interval.
883 *
884 * RFC 5880, Section 6.8.3.
885 */
886 if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx)
887 bs->xmt_TO = bs->remote_timers.required_min_rx;
888 else
889 bs->xmt_TO = bs->timers.desired_min_tx;
890
891 /* Apply new transmission timer immediately. */
892 ptm_bfd_start_xmt_timer(bs, false);
893
894 /*
895 * Detection timeout calculation:
896 * The minimum detection timeout is the remote detection
897 * multipler (number of packets to be missed) times the agreed
898 * transmission interval.
899 *
900 * RFC 5880, Section 6.8.4.
901 *
902 * TODO: support sending/counting more packets inside detection
903 * timeout.
904 */
905 if (bs->remote_timers.required_min_rx > bs->timers.desired_min_tx)
906 bs->detect_TO = bs->remote_detect_mult
907 * bs->remote_timers.required_min_rx;
908 else
909 bs->detect_TO = bs->remote_detect_mult
910 * bs->timers.desired_min_tx;
911
912 /* Apply new receive timer immediately. */
913 bfd_recvtimer_update(bs);
914
915 /* Notify watchers about changed timers. */
916 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
917}
918
b912b189
RZ
919void bs_set_slow_timers(struct bfd_session *bs)
920{
921 /*
922 * BFD connection must use slow timers before going up or after
923 * losing connectivity to avoid wasting bandwidth.
924 *
925 * RFC 5880, Section 6.8.3.
926 */
927 bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX;
928 bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX;
929 bs->cur_timers.required_min_echo = 0;
930
931 /* Set the appropriated timeouts for slow connection. */
932 bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX);
933 bs->xmt_TO = BFD_DEF_SLOWTX;
934}
e9e2c950
RZ
935
936/*
937 * Helper functions.
938 */
939static const char *get_diag_str(int diag)
940{
941 for (int i = 0; diag_list[i].str; i++) {
942 if (diag_list[i].type == diag)
943 return diag_list[i].str;
944 }
945 return "N/A";
946}
947
948const char *satostr(struct sockaddr_any *sa)
949{
950#define INETSTR_BUFCOUNT 8
951 static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN];
952 static int bufidx;
953 struct sockaddr_in *sin = &sa->sa_sin;
954 struct sockaddr_in6 *sin6 = &sa->sa_sin6;
955
956 bufidx += (bufidx + 1) % INETSTR_BUFCOUNT;
957 buf[bufidx][0] = 0;
958
959 switch (sin->sin_family) {
960 case AF_INET:
961 inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx],
962 sizeof(buf[bufidx]));
963 break;
964 case AF_INET6:
965 inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx],
966 sizeof(buf[bufidx]));
967 break;
968
969 default:
970 strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx]));
971 break;
972 }
973
974 return buf[bufidx];
975}
976
977const char *diag2str(uint8_t diag)
978{
979 switch (diag) {
980 case 0:
981 return "ok";
982 case 1:
983 return "control detection time expired";
984 case 2:
985 return "echo function failed";
986 case 3:
987 return "neighbor signaled session down";
988 case 4:
989 return "forwarding plane reset";
990 case 5:
991 return "path down";
992 case 6:
993 return "concatenated path down";
994 case 7:
995 return "administratively down";
996 case 8:
997 return "reverse concatenated path down";
998 default:
999 return "unknown";
1000 }
1001}
1002
1003int strtosa(const char *addr, struct sockaddr_any *sa)
1004{
1005 memset(sa, 0, sizeof(*sa));
1006
1007 if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) {
1008 sa->sa_sin.sin_family = AF_INET;
1009#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1010 sa->sa_sin.sin_len = sizeof(sa->sa_sin);
1011#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1012 return 0;
1013 }
1014
1015 if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) {
1016 sa->sa_sin6.sin6_family = AF_INET6;
1017#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1018 sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6);
1019#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1020 return 0;
1021 }
1022
1023 return -1;
1024}
1025
1026void integer2timestr(uint64_t time, char *buf, size_t buflen)
1027{
1028 unsigned int year, month, day, hour, minute, second;
1029 int rv;
1030
1031#define MINUTES (60)
23586b05
DS
1032#define HOURS (60 * MINUTES)
1033#define DAYS (24 * HOURS)
1034#define MONTHS (30 * DAYS)
1035#define YEARS (12 * MONTHS)
e9e2c950
RZ
1036 if (time >= YEARS) {
1037 year = time / YEARS;
1038 time -= year * YEARS;
1039
1040 rv = snprintf(buf, buflen, "%u year(s), ", year);
1041 buf += rv;
1042 buflen -= rv;
1043 }
1044 if (time >= MONTHS) {
1045 month = time / MONTHS;
1046 time -= month * MONTHS;
1047
1048 rv = snprintf(buf, buflen, "%u month(s), ", month);
1049 buf += rv;
1050 buflen -= rv;
1051 }
1052 if (time >= DAYS) {
1053 day = time / DAYS;
1054 time -= day * DAYS;
1055
1056 rv = snprintf(buf, buflen, "%u day(s), ", day);
1057 buf += rv;
1058 buflen -= rv;
1059 }
1060 if (time >= HOURS) {
1061 hour = time / HOURS;
1062 time -= hour * HOURS;
1063
1064 rv = snprintf(buf, buflen, "%u hour(s), ", hour);
1065 buf += rv;
1066 buflen -= rv;
1067 }
1068 if (time >= MINUTES) {
1069 minute = time / MINUTES;
1070 time -= minute * MINUTES;
1071
1072 rv = snprintf(buf, buflen, "%u minute(s), ", minute);
1073 buf += rv;
1074 buflen -= rv;
1075 }
1076 second = time % MINUTES;
1077 snprintf(buf, buflen, "%u second(s)", second);
1078}
1079
03e7f088
RZ
1080const char *bs_to_string(struct bfd_session *bs)
1081{
1082 static char buf[256];
1083 int pos;
1084 bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH);
1085
1086 pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no");
1087 if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
1088 pos += snprintf(buf + pos, sizeof(buf) - pos,
1089 " peer:%s local:%s", satostr(&bs->mhop.peer),
1090 satostr(&bs->mhop.local));
1091
1092 if (bs->mhop.vrf_name[0])
1093 snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s",
1094 bs->mhop.vrf_name);
1095 } else {
1096 pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s",
1097 satostr(&bs->shop.peer));
1098
1099 if (bs->local_address.sa_sin.sin_family)
1100 pos += snprintf(buf + pos, sizeof(buf) - pos,
1101 " local:%s",
1102 satostr(&bs->local_address));
1103
1104 if (bs->shop.port_name[0])
1105 snprintf(buf + pos, sizeof(buf) - pos, " interface:%s",
1106 bs->shop.port_name);
1107 }
1108
1109 return buf;
1110}
1111
e9e2c950
RZ
1112
1113/*
1114 * BFD hash data structures to find sessions.
1115 */
1116static struct hash *bfd_id_hash;
1117static struct hash *bfd_shop_hash;
1118static struct hash *bfd_mhop_hash;
1119static struct hash *bfd_vrf_hash;
1120static struct hash *bfd_iface_hash;
1121
1122static unsigned int bfd_id_hash_do(void *p);
e9e2c950 1123static unsigned int bfd_shop_hash_do(void *p);
e9e2c950 1124static unsigned int bfd_mhop_hash_do(void *p);
e9e2c950 1125static unsigned int bfd_vrf_hash_do(void *p);
e9e2c950 1126static unsigned int bfd_iface_hash_do(void *p);
e9e2c950
RZ
1127
1128static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop);
1129static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop);
1130static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop);
1131static int _iface_key(struct bfd_iface *iface, const char *ifname);
1132
1133static void _bfd_free(struct hash_backet *hb,
1134 void *arg __attribute__((__unused__)));
1135static void _vrf_free(void *arg);
1136static void _iface_free(void *arg);
1137
1138/* BFD hash for our discriminator. */
1139static unsigned int bfd_id_hash_do(void *p)
1140{
1141 struct bfd_session *bs = p;
1142
1143 return jhash_1word(bs->discrs.my_discr, 0);
1144}
1145
74df8d6d 1146static bool bfd_id_hash_cmp(const void *n1, const void *n2)
e9e2c950
RZ
1147{
1148 const struct bfd_session *bs1 = n1, *bs2 = n2;
1149
1150 return bs1->discrs.my_discr == bs2->discrs.my_discr;
1151}
1152
1153/* BFD hash for single hop. */
1154static unsigned int bfd_shop_hash_do(void *p)
1155{
1156 struct bfd_session *bs = p;
1157
1158 return jhash(&bs->shop, sizeof(bs->shop), 0);
1159}
1160
74df8d6d 1161static bool bfd_shop_hash_cmp(const void *n1, const void *n2)
e9e2c950
RZ
1162{
1163 const struct bfd_session *bs1 = n1, *bs2 = n2;
1164
1165 return memcmp(&bs1->shop, &bs2->shop, sizeof(bs1->shop)) == 0;
1166}
1167
1168/* BFD hash for multi hop. */
1169static unsigned int bfd_mhop_hash_do(void *p)
1170{
1171 struct bfd_session *bs = p;
1172
1173 return jhash(&bs->mhop, sizeof(bs->mhop), 0);
1174}
1175
74df8d6d 1176static bool bfd_mhop_hash_cmp(const void *n1, const void *n2)
e9e2c950
RZ
1177{
1178 const struct bfd_session *bs1 = n1, *bs2 = n2;
1179
1180 return memcmp(&bs1->mhop, &bs2->mhop, sizeof(bs1->mhop)) == 0;
1181}
1182
1183/* BFD hash for VRFs. */
1184static unsigned int bfd_vrf_hash_do(void *p)
1185{
1186 struct bfd_vrf *vrf = p;
1187
1188 return jhash_1word(vrf->vrf_id, 0);
1189}
1190
74df8d6d 1191static bool bfd_vrf_hash_cmp(const void *n1, const void *n2)
e9e2c950
RZ
1192{
1193 const struct bfd_vrf *v1 = n1, *v2 = n2;
1194
1195 return v1->vrf_id == v2->vrf_id;
1196}
1197
1198/* BFD hash for interfaces. */
1199static unsigned int bfd_iface_hash_do(void *p)
1200{
1201 struct bfd_iface *iface = p;
1202
1203 return string_hash_make(iface->ifname);
1204}
1205
74df8d6d 1206static bool bfd_iface_hash_cmp(const void *n1, const void *n2)
e9e2c950
RZ
1207{
1208 const struct bfd_iface *i1 = n1, *i2 = n2;
1209
1210 return strcmp(i1->ifname, i2->ifname) == 0;
1211}
1212
1213/* Helper functions */
1214static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop)
1215{
1216 bs->shop = *shop;
1217
1218 /* Remove unused fields. */
1219 switch (bs->shop.peer.sa_sin.sin_family) {
1220 case AF_INET:
1221 bs->shop.peer.sa_sin.sin_port = 0;
1222 break;
1223 case AF_INET6:
1224 bs->shop.peer.sa_sin6.sin6_port = 0;
1225 break;
1226 }
1227}
1228
1229static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop)
1230{
1231 _shop_key(bs, shop);
1232 memset(bs->shop.port_name, 0, sizeof(bs->shop.port_name));
1233}
1234
1235static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop)
1236{
1237 bs->mhop = *mhop;
1238
1239 /* Remove unused fields. */
1240 switch (bs->mhop.peer.sa_sin.sin_family) {
1241 case AF_INET:
1242 bs->mhop.peer.sa_sin.sin_port = 0;
1243 bs->mhop.local.sa_sin.sin_port = 0;
1244 break;
1245 case AF_INET6:
1246 bs->mhop.peer.sa_sin6.sin6_port = 0;
1247 bs->mhop.local.sa_sin6.sin6_port = 0;
1248 break;
1249 }
1250}
1251
1252static int _iface_key(struct bfd_iface *iface, const char *ifname)
1253{
1254 size_t slen = sizeof(iface->ifname);
1255
1256 memset(iface->ifname, 0, slen);
1257 if (strlcpy(iface->ifname, ifname, slen) >= slen)
1258 return -1;
1259
1260 return 0;
1261}
1262
e9e2c950
RZ
1263/*
1264 * Hash public interface / exported functions.
1265 */
1266
1267/* Lookup functions. */
1268struct bfd_session *bfd_id_lookup(uint32_t id)
1269{
1270 struct bfd_session bs;
1271
1272 bs.discrs.my_discr = id;
1273
1274 return hash_lookup(bfd_id_hash, &bs);
1275}
1276
1277struct bfd_session *bfd_shop_lookup(struct bfd_shop_key shop)
1278{
1279 struct bfd_session bs, *bsp;
1280
1281 _shop_key(&bs, &shop);
1282
1283 bsp = hash_lookup(bfd_shop_hash, &bs);
1284 if (bsp == NULL && bs.shop.port_name[0] != 0) {
1285 /*
1286 * Since the local interface spec is optional, try
1287 * searching the key without it as well.
1288 */
1289 _shop_key2(&bs, &shop);
1290 bsp = hash_lookup(bfd_shop_hash, &bs);
1291 }
1292
1293 return bsp;
1294}
1295
1296struct bfd_session *bfd_mhop_lookup(struct bfd_mhop_key mhop)
1297{
1298 struct bfd_session bs;
1299
1300 _mhop_key(&bs, &mhop);
1301
2055ea09 1302 return hash_lookup(bfd_mhop_hash, &bs);
e9e2c950
RZ
1303}
1304
1305struct bfd_vrf *bfd_vrf_lookup(int vrf_id)
1306{
1307 struct bfd_vrf vrf;
1308
1309 vrf.vrf_id = vrf_id;
1310
1311 return hash_lookup(bfd_vrf_hash, &vrf);
1312}
1313
1314struct bfd_iface *bfd_iface_lookup(const char *ifname)
1315{
1316 struct bfd_iface iface;
1317
1318 if (_iface_key(&iface, ifname) != 0)
1319 return NULL;
1320
1321 return hash_lookup(bfd_iface_hash, &iface);
1322}
1323
1324/*
1325 * Delete functions.
1326 *
1327 * Delete functions searches and remove the item from the hash and
1328 * returns a pointer to the removed item data. If the item was not found
1329 * then it returns NULL.
1330 *
1331 * The data stored inside the hash is not free()ed, so you must do it
1332 * manually after getting the pointer back.
1333 */
1334struct bfd_session *bfd_id_delete(uint32_t id)
1335{
1336 struct bfd_session bs;
1337
1338 bs.discrs.my_discr = id;
1339
1340 return hash_release(bfd_id_hash, &bs);
1341}
1342
1343struct bfd_session *bfd_shop_delete(struct bfd_shop_key shop)
1344{
1345 struct bfd_session bs, *bsp;
1346
1347 _shop_key(&bs, &shop);
1348 bsp = hash_release(bfd_shop_hash, &bs);
1349 if (bsp == NULL && shop.port_name[0] != 0) {
1350 /*
1351 * Since the local interface spec is optional, try
1352 * searching the key without it as well.
1353 */
1354 _shop_key2(&bs, &shop);
1355 bsp = hash_release(bfd_shop_hash, &bs);
1356 }
1357
1358 return bsp;
1359}
1360
1361struct bfd_session *bfd_mhop_delete(struct bfd_mhop_key mhop)
1362{
1363 struct bfd_session bs;
1364
1365 _mhop_key(&bs, &mhop);
1366
1367 return hash_release(bfd_mhop_hash, &bs);
1368}
1369
1370struct bfd_vrf *bfd_vrf_delete(int vrf_id)
1371{
1372 struct bfd_vrf vrf;
1373
1374 vrf.vrf_id = vrf_id;
1375
1376 return hash_release(bfd_vrf_hash, &vrf);
1377}
1378
1379struct bfd_iface *bfd_iface_delete(const char *ifname)
1380{
1381 struct bfd_iface iface;
1382
1383 if (_iface_key(&iface, ifname) != 0)
1384 return NULL;
1385
1386 return hash_release(bfd_iface_hash, &iface);
1387}
1388
1389/* Iteration functions. */
1390void bfd_id_iterate(hash_iter_func hif, void *arg)
1391{
1392 hash_iterate(bfd_id_hash, hif, arg);
1393}
1394
1395void bfd_shop_iterate(hash_iter_func hif, void *arg)
1396{
1397 hash_iterate(bfd_shop_hash, hif, arg);
1398}
1399
1400void bfd_mhop_iterate(hash_iter_func hif, void *arg)
1401{
1402 hash_iterate(bfd_mhop_hash, hif, arg);
1403}
1404
1405void bfd_vrf_iterate(hash_iter_func hif, void *arg)
1406{
1407 hash_iterate(bfd_vrf_hash, hif, arg);
1408}
1409
1410void bfd_iface_iterate(hash_iter_func hif, void *arg)
1411{
1412 hash_iterate(bfd_iface_hash, hif, arg);
1413}
1414
1415/*
1416 * Insert functions.
1417 *
1418 * Inserts session into hash and returns `true` on success, otherwise
1419 * `false`.
1420 */
1421bool bfd_id_insert(struct bfd_session *bs)
1422{
1423 return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs);
1424}
1425
1426bool bfd_shop_insert(struct bfd_session *bs)
1427{
1428 return (hash_get(bfd_shop_hash, bs, hash_alloc_intern) == bs);
1429}
1430
1431bool bfd_mhop_insert(struct bfd_session *bs)
1432{
1433 return (hash_get(bfd_mhop_hash, bs, hash_alloc_intern) == bs);
1434}
1435
1436bool bfd_vrf_insert(struct bfd_vrf *vrf)
1437{
1438 return (hash_get(bfd_vrf_hash, vrf, hash_alloc_intern) == vrf);
1439}
1440
1441bool bfd_iface_insert(struct bfd_iface *iface)
1442{
1443 return (hash_get(bfd_iface_hash, iface, hash_alloc_intern) == iface);
1444}
1445
1446void bfd_initialize(void)
1447{
1448 bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp,
1449 "BFD discriminator hash");
1450 bfd_shop_hash = hash_create(bfd_shop_hash_do, bfd_shop_hash_cmp,
1451 "BFD single hop hash");
1452 bfd_mhop_hash = hash_create(bfd_mhop_hash_do, bfd_mhop_hash_cmp,
1453 "BFD multihop hop hash");
1454 bfd_vrf_hash =
1455 hash_create(bfd_vrf_hash_do, bfd_vrf_hash_cmp, "BFD VRF hash");
1456 bfd_iface_hash = hash_create(bfd_iface_hash_do, bfd_iface_hash_cmp,
1457 "BFD interface hash");
1458}
1459
1460static void _bfd_free(struct hash_backet *hb,
1461 void *arg __attribute__((__unused__)))
1462{
1463 struct bfd_session *bs = hb->data;
1464
1465 bfd_session_free(bs);
1466}
1467
1468static void _vrf_free(void *arg)
1469{
1470 struct bfd_vrf *vrf = arg;
1471
1472 XFREE(MTYPE_BFDD_CONFIG, vrf);
1473}
1474
1475static void _iface_free(void *arg)
1476{
1477 struct bfd_iface *iface = arg;
1478
1479 XFREE(MTYPE_BFDD_CONFIG, iface);
1480}
1481
1482void bfd_shutdown(void)
1483{
1484 /*
1485 * Close and free all BFD sessions.
1486 *
1487 * _bfd_free() will call bfd_session_free() which will take care
1488 * of removing the session from all hashes, so we just run an
1489 * assert() here to make sure it really happened.
1490 */
1491 bfd_id_iterate(_bfd_free, NULL);
1492 assert(bfd_shop_hash->count == 0);
1493 assert(bfd_mhop_hash->count == 0);
1494
1495 /* Clean the VRF and interface hashes. */
1496 hash_clean(bfd_vrf_hash, _vrf_free);
1497 hash_clean(bfd_iface_hash, _iface_free);
1498
1499 /* Now free the hashes themselves. */
1500 hash_free(bfd_id_hash);
1501 hash_free(bfd_shop_hash);
1502 hash_free(bfd_mhop_hash);
1503 hash_free(bfd_vrf_hash);
1504 hash_free(bfd_iface_hash);
1505}