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