1 /*********************************************************************
2 * Copyright 2013 Cumulus Networks, LLC. All rights reserved.
3 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
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)
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
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
19 * bfd.c: implements the BFD protocol.
23 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
24 * Kanna Rajagopal [kanna@cumulusnetworks.com]
25 * Radhika Mahankali [Radhika@cumulusnetworks.com]
30 #include "lib/jhash.h"
31 #include "lib/network.h"
35 DEFINE_MTYPE_STATIC(BFDD
, BFDD_CONFIG
, "long-lived configuration memory")
36 DEFINE_MTYPE_STATIC(BFDD
, BFDD_PROFILE
, "long-lived profile memory")
37 DEFINE_MTYPE_STATIC(BFDD
, BFDD_SESSION_OBSERVER
, "Session observer")
38 DEFINE_MTYPE_STATIC(BFDD
, BFDD_VRF
, "BFD VRF")
43 static uint32_t ptm_bfd_gen_ID(void);
44 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
);
45 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
47 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
48 static const char *get_diag_str(int diag
);
50 static void bs_admin_down_handler(struct bfd_session
*bs
, int nstate
);
51 static void bs_down_handler(struct bfd_session
*bs
, int nstate
);
52 static void bs_init_handler(struct bfd_session
*bs
, int nstate
);
53 static void bs_up_handler(struct bfd_session
*bs
, int nstate
);
54 static void bs_neighbour_admin_down_handler(struct bfd_session
*bfd
,
58 * Remove BFD profile from all BFD sessions so we don't leave dangling
61 static void bfd_profile_detach(struct bfd_profile
*bp
);
63 /* Zeroed array with the size of an IPv6 address. */
64 struct in6_addr zero_addr
;
66 /** BFD profiles list. */
67 struct bfdproflist bplist
;
72 struct bfd_profile
*bfd_profile_lookup(const char *name
)
74 struct bfd_profile
*bp
;
76 TAILQ_FOREACH (bp
, &bplist
, entry
) {
77 if (strcmp(name
, bp
->name
))
86 static void bfd_profile_set_default(struct bfd_profile
*bp
)
88 bp
->admin_shutdown
= true;
89 bp
->detection_multiplier
= BFD_DEFDETECTMULT
;
90 bp
->echo_mode
= false;
92 bp
->minimum_ttl
= BFD_DEF_MHOP_TTL
;
93 bp
->min_echo_rx
= BFD_DEF_REQ_MIN_ECHO
;
94 bp
->min_rx
= BFD_DEFREQUIREDMINRX
;
95 bp
->min_tx
= BFD_DEFDESIREDMINTX
;
98 struct bfd_profile
*bfd_profile_new(const char *name
)
100 struct bfd_profile
*bp
;
102 /* Search for duplicates. */
103 if (bfd_profile_lookup(name
) != NULL
)
106 /* Allocate, name it and put into list. */
107 bp
= XCALLOC(MTYPE_BFDD_PROFILE
, sizeof(*bp
));
108 strlcpy(bp
->name
, name
, sizeof(bp
->name
));
109 TAILQ_INSERT_TAIL(&bplist
, bp
, entry
);
111 /* Set default values. */
112 bfd_profile_set_default(bp
);
117 void bfd_profile_free(struct bfd_profile
*bp
)
119 /* Detach from any session. */
120 if (bglobal
.bg_shutdown
== false)
121 bfd_profile_detach(bp
);
123 /* Remove from global list. */
124 TAILQ_REMOVE(&bplist
, bp
, entry
);
126 XFREE(MTYPE_BFDD_PROFILE
, bp
);
129 void bfd_profile_apply(const char *profname
, struct bfd_session
*bs
)
131 struct bfd_profile
*bp
;
133 /* Remove previous profile if any. */
134 if (bs
->profile_name
) {
135 /* We are changing profiles. */
136 if (strcmp(bs
->profile_name
, profname
)) {
137 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
139 XSTRDUP(MTYPE_BFDD_PROFILE
, profname
);
141 } else /* Save the current profile name (in case it doesn't exist). */
142 bs
->profile_name
= XSTRDUP(MTYPE_BFDD_PROFILE
, profname
);
144 /* Look up new profile to apply. */
145 bp
= bfd_profile_lookup(profname
);
147 /* Point to profile if it exists. */
150 /* Apply configuration. */
151 bfd_session_apply(bs
);
154 void bfd_session_apply(struct bfd_session
*bs
)
156 struct bfd_profile
*bp
;
157 uint32_t min_tx
= bs
->timers
.desired_min_tx
;
158 uint32_t min_rx
= bs
->timers
.required_min_rx
;
160 /* Pick the source of configuration. */
161 bp
= bs
->profile
? bs
->profile
: &bs
->peer_profile
;
163 /* Set multiplier if not the default. */
164 if (bs
->peer_profile
.detection_multiplier
== BFD_DEFDETECTMULT
)
165 bs
->detect_mult
= bp
->detection_multiplier
;
167 bs
->detect_mult
= bs
->peer_profile
.detection_multiplier
;
169 /* Set timers if not the default. */
170 if (bs
->peer_profile
.min_tx
== BFD_DEFDESIREDMINTX
)
171 bs
->timers
.desired_min_tx
= bp
->min_tx
;
173 bs
->timers
.desired_min_tx
= bs
->peer_profile
.min_tx
;
175 if (bs
->peer_profile
.min_rx
== BFD_DEFREQUIREDMINRX
)
176 bs
->timers
.required_min_rx
= bp
->min_rx
;
178 bs
->timers
.required_min_rx
= bs
->peer_profile
.min_rx
;
180 /* We can only apply echo options on single hop sessions. */
181 if (!CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
182 /* Configure remote echo if it was default. */
183 if (bs
->peer_profile
.min_echo_rx
== BFD_DEF_REQ_MIN_ECHO
)
184 bs
->timers
.required_min_echo
= bp
->min_echo_rx
;
186 bs
->timers
.required_min_echo
=
187 bs
->peer_profile
.min_echo_rx
;
189 /* Toggle echo if default value. */
190 if (bs
->peer_profile
.echo_mode
== false)
191 bfd_set_echo(bs
, bp
->echo_mode
);
193 bfd_set_echo(bs
, bs
->peer_profile
.echo_mode
);
195 /* Configure the TTL packet filter. */
196 if (bs
->peer_profile
.minimum_ttl
== BFD_DEF_MHOP_TTL
)
197 bs
->mh_ttl
= bp
->minimum_ttl
;
199 bs
->mh_ttl
= bs
->peer_profile
.minimum_ttl
;
202 /* Toggle 'passive-mode' if default value. */
203 if (bs
->peer_profile
.passive
== false)
204 bfd_set_passive_mode(bs
, bp
->passive
);
206 bfd_set_passive_mode(bs
, bs
->peer_profile
.passive
);
208 /* Toggle 'no shutdown' if default value. */
209 if (bs
->peer_profile
.admin_shutdown
)
210 bfd_set_shutdown(bs
, bp
->admin_shutdown
);
212 bfd_set_shutdown(bs
, bs
->peer_profile
.admin_shutdown
);
214 /* If session interval changed negotiate new timers. */
215 if (bs
->ses_state
== PTM_BFD_UP
216 && (bs
->timers
.desired_min_tx
!= min_tx
217 || bs
->timers
.required_min_rx
!= min_rx
))
220 /* Send updated information to data plane. */
221 bfd_dplane_update_session(bs
);
224 void bfd_profile_remove(struct bfd_session
*bs
)
226 /* Remove any previous set profile name. */
227 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
230 bfd_session_apply(bs
);
233 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
234 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
237 memset(key
, 0, sizeof(*key
));
239 switch (peer
->sa_sin
.sin_family
) {
241 key
->family
= AF_INET
;
242 memcpy(&key
->peer
, &peer
->sa_sin
.sin_addr
,
243 sizeof(peer
->sa_sin
.sin_addr
));
244 memcpy(&key
->local
, &local
->sa_sin
.sin_addr
,
245 sizeof(local
->sa_sin
.sin_addr
));
248 key
->family
= AF_INET6
;
249 memcpy(&key
->peer
, &peer
->sa_sin6
.sin6_addr
,
250 sizeof(peer
->sa_sin6
.sin6_addr
));
251 memcpy(&key
->local
, &local
->sa_sin6
.sin6_addr
,
252 sizeof(local
->sa_sin6
.sin6_addr
));
257 if (ifname
&& ifname
[0])
258 strlcpy(key
->ifname
, ifname
, sizeof(key
->ifname
));
259 if (vrfname
&& vrfname
[0])
260 strlcpy(key
->vrfname
, vrfname
, sizeof(key
->vrfname
));
262 strlcpy(key
->vrfname
, VRF_DEFAULT_NAME
, sizeof(key
->vrfname
));
265 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
)
267 struct bfd_session
*bs
;
268 struct peer_label
*pl
;
271 /* Try to find label first. */
272 if (bpc
->bpc_has_label
) {
273 pl
= pl_find(bpc
->bpc_label
);
280 /* Otherwise fallback to peer/local hash lookup. */
281 gen_bfd_key(&key
, &bpc
->bpc_peer
, &bpc
->bpc_local
, bpc
->bpc_mhop
,
282 bpc
->bpc_localif
, bpc
->bpc_vrfname
);
284 return bfd_key_lookup(key
);
288 * Starts a disabled BFD session.
290 * A session is disabled when the specified interface/VRF doesn't exist
291 * yet. It might happen on FRR boot or with virtual interfaces.
293 int bfd_session_enable(struct bfd_session
*bs
)
295 struct interface
*ifp
= NULL
;
296 struct vrf
*vrf
= NULL
;
299 /* We are using data plane, we don't need software. */
304 * If the interface or VRF doesn't exist, then we must register
305 * the session but delay its start.
307 if (bs
->key
.vrfname
[0]) {
308 vrf
= vrf_lookup_by_name(bs
->key
.vrfname
);
311 "session-enable: specified VRF doesn't exists.");
316 if (!vrf_is_backend_netns() && vrf
&& vrf
->vrf_id
!= VRF_DEFAULT
317 && !if_lookup_by_name(vrf
->name
, vrf
->vrf_id
)) {
318 zlog_err("session-enable: vrf interface %s not available yet",
323 if (bs
->key
.ifname
[0]) {
325 ifp
= if_lookup_by_name(bs
->key
.ifname
, vrf
->vrf_id
);
327 ifp
= if_lookup_by_name_all_vrf(bs
->key
.ifname
);
330 "session-enable: specified interface %s (VRF %s) doesn't exist.",
331 bs
->key
.ifname
, vrf
? vrf
->name
: "<all>");
334 if (bs
->key
.ifname
[0] && !vrf
) {
335 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
338 "session-enable: specified VRF %u doesn't exist.",
345 /* Assign interface/VRF pointers. */
348 bs
->vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
351 /* Assign interface pointer (if any). */
354 /* Attempt to use data plane. */
355 if (bglobal
.bg_use_dplane
&& bfd_dplane_add_session(bs
) == 0) {
356 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bs
);
360 /* Sanity check: don't leak open sockets. */
361 if (bs
->sock
!= -1) {
362 if (bglobal
.debug_peer_event
)
363 zlog_debug("session-enable: previous socket open");
370 * Get socket for transmitting control packets. Note that if we
371 * could use the destination port (3784) for the source
372 * port we wouldn't need a socket per session.
374 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_IPV6
) == 0) {
375 psock
= bp_peer_socket(bs
);
379 psock
= bp_peer_socketv6(bs
);
385 * We've got a valid socket, lets start the timers and the
390 /* Only start timers if we are using active mode. */
391 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
) == 0) {
392 bfd_recvtimer_update(bs
);
393 ptm_bfd_start_xmt_timer(bs
, false);
400 * Disabled a running BFD session.
402 * A session is disabled when the specified interface/VRF gets removed
403 * (e.g. virtual interfaces).
405 void bfd_session_disable(struct bfd_session
*bs
)
407 /* We are using data plane, we don't need software. */
411 /* Free up socket resources. */
412 if (bs
->sock
!= -1) {
417 /* Disable all timers. */
418 bfd_recvtimer_delete(bs
);
419 bfd_xmttimer_delete(bs
);
420 ptm_bfd_echo_stop(bs
);
422 /* Set session down so it doesn't report UP and disabled. */
423 ptm_bfd_sess_dn(bs
, BD_PATH_DOWN
);
426 static uint32_t ptm_bfd_gen_ID(void)
431 * RFC 5880, Section 6.8.1. recommends that we should generate
432 * random session identification numbers.
435 session_id
= ((frr_weak_random() << 16) & 0xFFFF0000)
436 | (frr_weak_random() & 0x0000FFFF);
437 } while (session_id
== 0 || bfd_id_lookup(session_id
) != NULL
);
442 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
)
444 uint64_t jitter
, xmt_TO
;
447 xmt_TO
= is_echo
? bfd
->echo_xmt_TO
: bfd
->xmt_TO
;
450 * From section 6.5.2: trasmit interval should be randomly jittered
452 * 75% and 100% of nominal value, unless detect_mult is 1, then should
454 * between 75% and 90%.
456 maxpercent
= (bfd
->detect_mult
== 1) ? 16 : 26;
457 jitter
= (xmt_TO
* (75 + (frr_weak_random() % maxpercent
))) / 100;
458 /* XXX remove that division above */
461 bfd_echo_xmttimer_update(bfd
, jitter
);
463 bfd_xmttimer_update(bfd
, jitter
);
466 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
)
468 /* Send the scheduled echo packet */
469 ptm_bfd_echo_snd(bfd
);
471 /* Restart the timer for next time */
472 ptm_bfd_start_xmt_timer(bfd
, true);
475 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
)
477 /* Send the scheduled control packet */
478 ptm_bfd_snd(bfd
, fbit
);
480 /* Restart the timer for next time */
481 ptm_bfd_start_xmt_timer(bfd
, false);
484 void ptm_bfd_echo_stop(struct bfd_session
*bfd
)
486 bfd
->echo_xmt_TO
= 0;
487 bfd
->echo_detect_TO
= 0;
488 UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
);
490 bfd_echo_xmttimer_delete(bfd
);
491 bfd_echo_recvtimer_delete(bfd
);
494 void ptm_bfd_echo_start(struct bfd_session
*bfd
)
496 bfd
->echo_detect_TO
= (bfd
->remote_detect_mult
* bfd
->echo_xmt_TO
);
497 if (bfd
->echo_detect_TO
> 0)
498 ptm_bfd_echo_xmt_TO(bfd
);
501 void ptm_bfd_sess_up(struct bfd_session
*bfd
)
503 int old_state
= bfd
->ses_state
;
506 bfd
->ses_state
= PTM_BFD_UP
;
507 monotime(&bfd
->uptime
);
509 /* Connection is up, lets negotiate timers. */
510 bfd_set_polling(bfd
);
512 /* Start sending control packets with poll bit immediately. */
515 control_notify(bfd
, bfd
->ses_state
);
517 if (old_state
!= bfd
->ses_state
) {
518 bfd
->stats
.session_up
++;
519 if (bglobal
.debug_peer_event
)
520 zlog_debug("state-change: [%s] %s -> %s",
521 bs_to_string(bfd
), state_list
[old_state
].str
,
522 state_list
[bfd
->ses_state
].str
);
526 void ptm_bfd_sess_dn(struct bfd_session
*bfd
, uint8_t diag
)
528 int old_state
= bfd
->ses_state
;
530 bfd
->local_diag
= diag
;
531 bfd
->discrs
.remote_discr
= 0;
532 bfd
->ses_state
= PTM_BFD_DOWN
;
534 bfd
->demand_mode
= 0;
535 monotime(&bfd
->downtime
);
538 * Only attempt to send if we have a valid socket:
539 * this function might be called by session disablers and in
540 * this case we won't have a valid socket (i.e. interface was
541 * removed or VRF doesn't exist anymore).
546 /* Slow down the control packets, the connection is down. */
547 bs_set_slow_timers(bfd
);
549 /* only signal clients when going from up->down state */
550 if (old_state
== PTM_BFD_UP
)
551 control_notify(bfd
, PTM_BFD_DOWN
);
553 /* Stop echo packet transmission if they are active */
554 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
555 ptm_bfd_echo_stop(bfd
);
557 /* Stop attempting to transmit or expect control packets if passive. */
558 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_PASSIVE
)) {
559 bfd_recvtimer_delete(bfd
);
560 bfd_xmttimer_delete(bfd
);
563 if (old_state
!= bfd
->ses_state
) {
564 bfd
->stats
.session_down
++;
565 if (bglobal
.debug_peer_event
)
566 zlog_debug("state-change: [%s] %s -> %s reason:%s",
567 bs_to_string(bfd
), state_list
[old_state
].str
,
568 state_list
[bfd
->ses_state
].str
,
569 get_diag_str(bfd
->local_diag
));
573 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
576 struct bfd_session
*bs
;
578 bs
= bfd_id_lookup(ldisc
);
582 switch (bs
->key
.family
) {
584 if (memcmp(&sa
->sa_sin
.sin_addr
, &bs
->key
.peer
,
585 sizeof(sa
->sa_sin
.sin_addr
)))
589 if (memcmp(&sa
->sa_sin6
.sin6_addr
, &bs
->key
.peer
,
590 sizeof(sa
->sa_sin6
.sin6_addr
)))
598 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
599 struct sockaddr_any
*peer
,
600 struct sockaddr_any
*local
,
601 ifindex_t ifindex
, vrf_id_t vrfid
,
604 struct interface
*ifp
;
608 /* Find our session using the ID signaled by the remote end. */
609 if (cp
->discrs
.remote_discr
)
610 return bfd_find_disc(peer
, ntohl(cp
->discrs
.remote_discr
));
613 * Search for session without using discriminator.
615 * XXX: we can't trust `vrfid` because the VRF handling is not
616 * properly implemented. Meanwhile we should use the interface
617 * VRF to find out which one it belongs.
619 ifp
= if_lookup_by_index_all_vrf(ifindex
);
621 if (vrfid
!= VRF_DEFAULT
)
622 vrf
= vrf_lookup_by_id(vrfid
);
626 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
628 gen_bfd_key(&key
, peer
, local
, is_mhop
, ifp
? ifp
->name
: NULL
,
629 vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
631 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
632 return bfd_key_lookup(key
);
635 int bfd_xmt_cb(struct thread
*t
)
637 struct bfd_session
*bs
= THREAD_ARG(t
);
639 ptm_bfd_xmt_TO(bs
, 0);
644 int bfd_echo_xmt_cb(struct thread
*t
)
646 struct bfd_session
*bs
= THREAD_ARG(t
);
648 if (bs
->echo_xmt_TO
> 0)
649 ptm_bfd_echo_xmt_TO(bs
);
654 /* Was ptm_bfd_detect_TO() */
655 int bfd_recvtimer_cb(struct thread
*t
)
657 struct bfd_session
*bs
= THREAD_ARG(t
);
659 switch (bs
->ses_state
) {
662 ptm_bfd_sess_dn(bs
, BD_CONTROL_EXPIRED
);
663 bfd_recvtimer_update(bs
);
667 /* Second detect time expiration, zero remote discr (section
670 bs
->discrs
.remote_discr
= 0;
677 /* Was ptm_bfd_echo_detect_TO() */
678 int bfd_echo_recvtimer_cb(struct thread
*t
)
680 struct bfd_session
*bs
= THREAD_ARG(t
);
682 switch (bs
->ses_state
) {
685 ptm_bfd_sess_dn(bs
, BD_ECHO_FAILED
);
692 struct bfd_session
*bfd_session_new(void)
694 struct bfd_session
*bs
;
696 bs
= XCALLOC(MTYPE_BFDD_CONFIG
, sizeof(*bs
));
698 /* Set peer session defaults. */
699 bfd_profile_set_default(&bs
->peer_profile
);
701 bs
->timers
.desired_min_tx
= BFD_DEFDESIREDMINTX
;
702 bs
->timers
.required_min_rx
= BFD_DEFREQUIREDMINRX
;
703 bs
->timers
.required_min_echo
= BFD_DEF_REQ_MIN_ECHO
;
704 bs
->detect_mult
= BFD_DEFDETECTMULT
;
705 bs
->mh_ttl
= BFD_DEF_MHOP_TTL
;
706 bs
->ses_state
= PTM_BFD_DOWN
;
708 /* Initiate connection with slow timers. */
709 bs_set_slow_timers(bs
);
711 /* Initiate remote settings as well. */
712 bs
->remote_timers
= bs
->cur_timers
;
713 bs
->remote_detect_mult
= BFD_DEFDETECTMULT
;
716 monotime(&bs
->uptime
);
717 bs
->downtime
= bs
->uptime
;
722 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
)
724 /* New label treatment:
725 * - Check if the label is taken;
726 * - Try to allocate the memory for it and register;
728 if (bs
->pl
== NULL
) {
729 if (pl_find(nlabel
) != NULL
) {
730 /* Someone is already using it. */
740 * Test label change consistency:
741 * - Do nothing if it's the same label;
742 * - Check if the future label is already taken;
745 if (strcmp(nlabel
, bs
->pl
->pl_label
) == 0)
747 if (pl_find(nlabel
) != NULL
)
750 strlcpy(bs
->pl
->pl_label
, nlabel
, sizeof(bs
->pl
->pl_label
));
754 static void _bfd_session_update(struct bfd_session
*bs
,
755 struct bfd_peer_cfg
*bpc
)
757 if (bpc
->bpc_has_txinterval
) {
758 bs
->timers
.desired_min_tx
= bpc
->bpc_txinterval
* 1000;
759 bs
->peer_profile
.min_tx
= bs
->timers
.desired_min_tx
;
762 if (bpc
->bpc_has_recvinterval
) {
763 bs
->timers
.required_min_rx
= bpc
->bpc_recvinterval
* 1000;
764 bs
->peer_profile
.min_rx
= bs
->timers
.required_min_rx
;
767 if (bpc
->bpc_has_detectmultiplier
) {
768 bs
->detect_mult
= bpc
->bpc_detectmultiplier
;
769 bs
->peer_profile
.detection_multiplier
= bs
->detect_mult
;
772 if (bpc
->bpc_has_echointerval
) {
773 bs
->timers
.required_min_echo
= bpc
->bpc_echointerval
* 1000;
774 bs
->peer_profile
.min_echo_rx
= bs
->timers
.required_min_echo
;
777 if (bpc
->bpc_has_label
)
778 bfd_session_update_label(bs
, bpc
->bpc_label
);
781 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
783 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
785 if (bpc
->bpc_has_minimum_ttl
) {
786 bs
->mh_ttl
= bpc
->bpc_minimum_ttl
;
787 bs
->peer_profile
.minimum_ttl
= bpc
->bpc_minimum_ttl
;
790 bs
->peer_profile
.echo_mode
= bpc
->bpc_echo
;
791 bfd_set_echo(bs
, bpc
->bpc_echo
);
794 * Shutdown needs to be the last in order to avoid timers enable when
795 * the session is disabled.
797 bs
->peer_profile
.admin_shutdown
= bpc
->bpc_shutdown
;
798 bfd_set_passive_mode(bs
, bpc
->bpc_passive
);
799 bfd_set_shutdown(bs
, bpc
->bpc_shutdown
);
802 * Apply profile last: it also calls `bfd_set_shutdown`.
804 * There is no problem calling `shutdown` twice if the value doesn't
805 * change or if it is overriden by peer specific configuration.
807 if (bpc
->bpc_has_profile
)
808 bfd_profile_apply(bpc
->bpc_profile
, bs
);
811 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
813 /* User didn't want to update, return failure. */
814 if (bpc
->bpc_createonly
)
817 _bfd_session_update(bs
, bpc
);
819 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
824 void bfd_session_free(struct bfd_session
*bs
)
826 struct bfd_session_observer
*bso
;
828 bfd_session_disable(bs
);
830 /* Remove session from data plane if any. */
831 bfd_dplane_delete_session(bs
);
833 bfd_key_delete(bs
->key
);
834 bfd_id_delete(bs
->discrs
.my_discr
);
836 /* Remove observer if any. */
837 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
838 if (bso
->bso_bs
!= bs
)
844 bs_observer_del(bso
);
848 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
849 XFREE(MTYPE_BFDD_CONFIG
, bs
);
852 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
)
854 struct bfd_session
*bfd
, *l_bfd
;
856 /* check to see if this needs a new session */
857 l_bfd
= bs_peer_find(bpc
);
859 /* Requesting a duplicated peer means update configuration. */
860 if (bfd_session_update(l_bfd
, bpc
) == 0)
866 /* Get BFD session storage with its defaults. */
867 bfd
= bfd_session_new();
870 * Store interface/VRF name in case we need to delay session
871 * start. See `bfd_session_enable` for more information.
873 if (bpc
->bpc_has_localif
)
874 strlcpy(bfd
->key
.ifname
, bpc
->bpc_localif
,
875 sizeof(bfd
->key
.ifname
));
877 if (bpc
->bpc_has_vrfname
)
878 strlcpy(bfd
->key
.vrfname
, bpc
->bpc_vrfname
,
879 sizeof(bfd
->key
.vrfname
));
881 strlcpy(bfd
->key
.vrfname
, VRF_DEFAULT_NAME
,
882 sizeof(bfd
->key
.vrfname
));
884 /* Copy remaining data. */
885 if (bpc
->bpc_ipv4
== false)
886 SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
);
888 bfd
->key
.family
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
889 switch (bfd
->key
.family
) {
891 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin
.sin_addr
,
892 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
893 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin
.sin_addr
,
894 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
898 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin6
.sin6_addr
,
899 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
900 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin6
.sin6_addr
,
901 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
910 SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MH
);
912 bfd
->key
.mhop
= bpc
->bpc_mhop
;
914 if (bs_registrate(bfd
) == NULL
)
917 /* Apply other configurations. */
918 _bfd_session_update(bfd
, bpc
);
923 struct bfd_session
*bs_registrate(struct bfd_session
*bfd
)
925 /* Registrate session into data structures. */
927 bfd
->discrs
.my_discr
= ptm_bfd_gen_ID();
930 /* Try to enable session and schedule for packet receive/send. */
931 if (bfd_session_enable(bfd
) == -1) {
932 /* Unrecoverable failure, remove the session/peer. */
933 bfd_session_free(bfd
);
937 /* Add observer if we have moving parts. */
938 if (bfd
->key
.ifname
[0] || bfd
->key
.vrfname
[0] || bfd
->sock
== -1)
939 bs_observer_add(bfd
);
941 if (bglobal
.debug_peer_event
)
942 zlog_debug("session-new: %s", bs_to_string(bfd
));
944 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bfd
);
949 int ptm_bfd_sess_del(struct bfd_peer_cfg
*bpc
)
951 struct bfd_session
*bs
;
953 /* Find session and call free(). */
954 bs
= bs_peer_find(bpc
);
958 /* This pointer is being referenced, don't let it be deleted. */
959 if (bs
->refcount
> 0) {
960 zlog_err("session-delete: refcount failure: %" PRIu64
" references",
965 if (bglobal
.debug_peer_event
)
966 zlog_debug("session-delete: %s", bs_to_string(bs
));
968 control_notify_config(BCM_NOTIFY_CONFIG_DELETE
, bs
);
970 bfd_session_free(bs
);
975 void bfd_set_polling(struct bfd_session
*bs
)
978 * Start polling procedure: the only timers that require polling
979 * to change value without losing connection are:
981 * - Desired minimum transmission interval;
982 * - Required minimum receive interval;
984 * RFC 5880, Section 6.8.3.
990 * bs_<state>_handler() functions implement the BFD state machine
991 * transition mechanism. `<state>` is the current session state and
992 * the parameter `nstate` is the peer new state.
994 static void bs_admin_down_handler(struct bfd_session
*bs
995 __attribute__((__unused__
)),
996 int nstate
__attribute__((__unused__
)))
999 * We are administratively down, there is no state machine
1004 static void bs_down_handler(struct bfd_session
*bs
, int nstate
)
1007 case PTM_BFD_ADM_DOWN
:
1009 * Remote peer doesn't want to talk, so lets keep the
1013 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
1018 * Remote peer agreed that the path is down, lets try to
1021 bs
->ses_state
= PTM_BFD_INIT
;
1023 /* Answer peer with INIT immediately in passive mode. */
1024 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
))
1030 * Remote peer told us his path is up, lets turn
1031 * activate the session.
1033 ptm_bfd_sess_up(bs
);
1037 if (bglobal
.debug_peer_event
)
1038 zlog_debug("state-change: unhandled neighbor state: %d",
1044 static void bs_init_handler(struct bfd_session
*bs
, int nstate
)
1047 case PTM_BFD_ADM_DOWN
:
1049 * Remote peer doesn't want to talk, so lets make the
1052 bs
->ses_state
= PTM_BFD_DOWN
;
1056 /* Remote peer hasn't moved to first stage yet. */
1061 /* We agreed on the settings and the path is up. */
1062 ptm_bfd_sess_up(bs
);
1066 if (bglobal
.debug_peer_event
)
1067 zlog_debug("state-change: unhandled neighbor state: %d",
1073 static void bs_neighbour_admin_down_handler(struct bfd_session
*bfd
,
1076 int old_state
= bfd
->ses_state
;
1078 bfd
->local_diag
= diag
;
1079 bfd
->discrs
.remote_discr
= 0;
1080 bfd
->ses_state
= PTM_BFD_DOWN
;
1082 bfd
->demand_mode
= 0;
1083 monotime(&bfd
->downtime
);
1085 /* Slow down the control packets, the connection is down. */
1086 bs_set_slow_timers(bfd
);
1088 /* only signal clients when going from up->down state */
1089 if (old_state
== PTM_BFD_UP
)
1090 control_notify(bfd
, PTM_BFD_ADM_DOWN
);
1092 /* Stop echo packet transmission if they are active */
1093 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
1094 ptm_bfd_echo_stop(bfd
);
1096 if (old_state
!= bfd
->ses_state
) {
1097 bfd
->stats
.session_down
++;
1098 if (bglobal
.debug_peer_event
)
1099 zlog_debug("state-change: [%s] %s -> %s reason:%s",
1100 bs_to_string(bfd
), state_list
[old_state
].str
,
1101 state_list
[bfd
->ses_state
].str
,
1102 get_diag_str(bfd
->local_diag
));
1106 static void bs_up_handler(struct bfd_session
*bs
, int nstate
)
1109 case PTM_BFD_ADM_DOWN
:
1110 bs_neighbour_admin_down_handler(bs
, BD_ADMIN_DOWN
);
1114 /* Peer lost or asked to shutdown connection. */
1115 ptm_bfd_sess_dn(bs
, BD_NEIGHBOR_DOWN
);
1120 /* Path is up and working. */
1124 if (bglobal
.debug_peer_event
)
1125 zlog_debug("state-change: unhandled neighbor state: %d",
1131 void bs_state_handler(struct bfd_session
*bs
, int nstate
)
1133 switch (bs
->ses_state
) {
1134 case PTM_BFD_ADM_DOWN
:
1135 bs_admin_down_handler(bs
, nstate
);
1138 bs_down_handler(bs
, nstate
);
1141 bs_init_handler(bs
, nstate
);
1144 bs_up_handler(bs
, nstate
);
1148 if (bglobal
.debug_peer_event
)
1149 zlog_debug("state-change: [%s] is in invalid state: %d",
1150 bs_to_string(bs
), nstate
);
1156 * Handles echo timer manipulation after updating timer.
1158 void bs_echo_timer_handler(struct bfd_session
*bs
)
1163 * Before doing any echo handling, check if it is possible to
1166 * - Check for `echo-mode` configuration.
1167 * - Check that we are not using multi hop (RFC 5883,
1169 * - Check that we are already at the up state.
1171 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
) == 0
1172 || CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)
1173 || bs
->ses_state
!= PTM_BFD_UP
)
1176 /* Remote peer asked to stop echo. */
1177 if (bs
->remote_timers
.required_min_echo
== 0) {
1178 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
1179 ptm_bfd_echo_stop(bs
);
1185 * Calculate the echo transmission timer: we must not send
1186 * echo packets faster than the minimum required time
1187 * announced by the remote system.
1189 * RFC 5880, Section 6.8.9.
1191 old_timer
= bs
->echo_xmt_TO
;
1192 if (bs
->remote_timers
.required_min_echo
> bs
->timers
.required_min_echo
)
1193 bs
->echo_xmt_TO
= bs
->remote_timers
.required_min_echo
;
1195 bs
->echo_xmt_TO
= bs
->timers
.required_min_echo
;
1197 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
) == 0
1198 || old_timer
!= bs
->echo_xmt_TO
)
1199 ptm_bfd_echo_start(bs
);
1203 * RFC 5880 Section 6.5.
1205 * When a BFD control packet with the final bit is received, we must
1206 * update the session parameters.
1208 void bs_final_handler(struct bfd_session
*bs
)
1210 /* Start using our new timers. */
1211 bs
->cur_timers
.desired_min_tx
= bs
->timers
.desired_min_tx
;
1212 bs
->cur_timers
.required_min_rx
= bs
->timers
.required_min_rx
;
1215 * TODO: demand mode. See RFC 5880 Section 6.1.
1217 * When using demand mode we must disable the detection timer
1218 * for lost control packets.
1220 if (bs
->demand_mode
) {
1221 /* Notify watchers about changed timers. */
1222 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1227 * Calculate transmission time based on new timers.
1229 * Transmission calculation:
1230 * Unless specified by exceptions at the end of Section 6.8.7, the
1231 * transmission time will be determined by the system with the
1234 * RFC 5880, Section 6.8.7.
1236 if (bs
->timers
.desired_min_tx
> bs
->remote_timers
.required_min_rx
)
1237 bs
->xmt_TO
= bs
->timers
.desired_min_tx
;
1239 bs
->xmt_TO
= bs
->remote_timers
.required_min_rx
;
1241 /* Apply new transmission timer immediately. */
1242 ptm_bfd_start_xmt_timer(bs
, false);
1245 * Detection timeout calculation:
1246 * The minimum detection timeout is the remote detection
1247 * multipler (number of packets to be missed) times the agreed
1248 * transmission interval.
1250 * RFC 5880, Section 6.8.4.
1252 * TODO: support sending/counting more packets inside detection
1255 if (bs
->remote_timers
.required_min_rx
> bs
->timers
.desired_min_tx
)
1256 bs
->detect_TO
= bs
->remote_detect_mult
1257 * bs
->remote_timers
.required_min_rx
;
1259 bs
->detect_TO
= bs
->remote_detect_mult
1260 * bs
->timers
.desired_min_tx
;
1262 /* Apply new receive timer immediately. */
1263 bfd_recvtimer_update(bs
);
1265 /* Notify watchers about changed timers. */
1266 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1269 void bs_set_slow_timers(struct bfd_session
*bs
)
1272 * BFD connection must use slow timers before going up or after
1273 * losing connectivity to avoid wasting bandwidth.
1275 * RFC 5880, Section 6.8.3.
1277 bs
->cur_timers
.desired_min_tx
= BFD_DEF_SLOWTX
;
1278 bs
->cur_timers
.required_min_rx
= BFD_DEF_SLOWTX
;
1279 bs
->cur_timers
.required_min_echo
= 0;
1281 /* Set the appropriated timeouts for slow connection. */
1282 bs
->detect_TO
= (BFD_DEFDETECTMULT
* BFD_DEF_SLOWTX
);
1283 bs
->xmt_TO
= BFD_DEF_SLOWTX
;
1286 void bfd_set_echo(struct bfd_session
*bs
, bool echo
)
1289 /* Check if echo mode is already active. */
1290 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
1293 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
1295 /* Activate/update echo receive timeout timer. */
1296 if (bs
->bdc
== NULL
)
1297 bs_echo_timer_handler(bs
);
1299 /* Check if echo mode is already disabled. */
1300 if (!CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
1303 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
1305 /* Deactivate timeout timer. */
1306 if (bs
->bdc
== NULL
)
1307 ptm_bfd_echo_stop(bs
);
1311 void bfd_set_shutdown(struct bfd_session
*bs
, bool shutdown
)
1316 * Special case: we are batching changes and the previous state was
1317 * not shutdown. Instead of potentially disconnect a running peer,
1318 * we'll get the current status to validate we were really down.
1320 if (bs
->ses_state
== PTM_BFD_UP
)
1321 is_shutdown
= false;
1323 is_shutdown
= CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1326 /* Already shutdown. */
1330 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1332 /* Handle data plane shutdown case. */
1334 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
1335 bfd_dplane_update_session(bs
);
1336 control_notify(bs
, bs
->ses_state
);
1340 /* Disable all events. */
1341 bfd_recvtimer_delete(bs
);
1342 bfd_echo_recvtimer_delete(bs
);
1343 bfd_xmttimer_delete(bs
);
1344 bfd_echo_xmttimer_delete(bs
);
1346 /* Change and notify state change. */
1347 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
1348 control_notify(bs
, bs
->ses_state
);
1350 /* Don't try to send packets with a disabled session. */
1354 /* Already working. */
1358 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1360 /* Handle data plane shutdown case. */
1362 bs
->ses_state
= PTM_BFD_DOWN
;
1363 bfd_dplane_update_session(bs
);
1364 control_notify(bs
, bs
->ses_state
);
1368 /* Change and notify state change. */
1369 bs
->ses_state
= PTM_BFD_DOWN
;
1370 control_notify(bs
, bs
->ses_state
);
1372 /* Enable timers if non passive, otherwise stop them. */
1373 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
)) {
1374 bfd_recvtimer_delete(bs
);
1375 bfd_xmttimer_delete(bs
);
1377 bfd_recvtimer_update(bs
);
1378 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
1383 void bfd_set_passive_mode(struct bfd_session
*bs
, bool passive
)
1386 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
);
1388 /* Session is already up and running, nothing to do now. */
1389 if (bs
->ses_state
!= PTM_BFD_DOWN
)
1392 /* Lets disable the timers since we are now passive. */
1393 bfd_recvtimer_delete(bs
);
1394 bfd_xmttimer_delete(bs
);
1396 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
);
1398 /* Session is already up and running, nothing to do now. */
1399 if (bs
->ses_state
!= PTM_BFD_DOWN
)
1402 /* Session is down, let it attempt to start the connection. */
1403 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
1404 bfd_recvtimer_update(bs
);
1411 static const char *get_diag_str(int diag
)
1413 for (int i
= 0; diag_list
[i
].str
; i
++) {
1414 if (diag_list
[i
].type
== diag
)
1415 return diag_list
[i
].str
;
1420 const char *satostr(const struct sockaddr_any
*sa
)
1422 #define INETSTR_BUFCOUNT 8
1423 static char buf
[INETSTR_BUFCOUNT
][INET6_ADDRSTRLEN
];
1425 const struct sockaddr_in
*sin
= &sa
->sa_sin
;
1426 const struct sockaddr_in6
*sin6
= &sa
->sa_sin6
;
1428 bufidx
+= (bufidx
+ 1) % INETSTR_BUFCOUNT
;
1431 switch (sin
->sin_family
) {
1433 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
[bufidx
],
1434 sizeof(buf
[bufidx
]));
1437 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, buf
[bufidx
],
1438 sizeof(buf
[bufidx
]));
1442 strlcpy(buf
[bufidx
], "unknown", sizeof(buf
[bufidx
]));
1449 const char *diag2str(uint8_t diag
)
1455 return "control detection time expired";
1457 return "echo function failed";
1459 return "neighbor signaled session down";
1461 return "forwarding plane reset";
1465 return "concatenated path down";
1467 return "administratively down";
1469 return "reverse concatenated path down";
1475 int strtosa(const char *addr
, struct sockaddr_any
*sa
)
1477 memset(sa
, 0, sizeof(*sa
));
1479 if (inet_pton(AF_INET
, addr
, &sa
->sa_sin
.sin_addr
) == 1) {
1480 sa
->sa_sin
.sin_family
= AF_INET
;
1481 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1482 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
1483 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1487 if (inet_pton(AF_INET6
, addr
, &sa
->sa_sin6
.sin6_addr
) == 1) {
1488 sa
->sa_sin6
.sin6_family
= AF_INET6
;
1489 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1490 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
1491 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1498 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
)
1500 unsigned int year
, month
, day
, hour
, minute
, second
;
1503 #define MINUTES (60)
1504 #define HOURS (60 * MINUTES)
1505 #define DAYS (24 * HOURS)
1506 #define MONTHS (30 * DAYS)
1507 #define YEARS (12 * MONTHS)
1508 if (time
>= YEARS
) {
1509 year
= time
/ YEARS
;
1510 time
-= year
* YEARS
;
1512 rv
= snprintf(buf
, buflen
, "%u year(s), ", year
);
1516 if (time
>= MONTHS
) {
1517 month
= time
/ MONTHS
;
1518 time
-= month
* MONTHS
;
1520 rv
= snprintf(buf
, buflen
, "%u month(s), ", month
);
1528 rv
= snprintf(buf
, buflen
, "%u day(s), ", day
);
1532 if (time
>= HOURS
) {
1533 hour
= time
/ HOURS
;
1534 time
-= hour
* HOURS
;
1536 rv
= snprintf(buf
, buflen
, "%u hour(s), ", hour
);
1540 if (time
>= MINUTES
) {
1541 minute
= time
/ MINUTES
;
1542 time
-= minute
* MINUTES
;
1544 rv
= snprintf(buf
, buflen
, "%u minute(s), ", minute
);
1548 second
= time
% MINUTES
;
1549 snprintf(buf
, buflen
, "%u second(s)", second
);
1552 const char *bs_to_string(const struct bfd_session
*bs
)
1554 static char buf
[256];
1555 char addr_buf
[INET6_ADDRSTRLEN
];
1557 bool is_mhop
= CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
);
1559 pos
= snprintf(buf
, sizeof(buf
), "mhop:%s", is_mhop
? "yes" : "no");
1560 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " peer:%s",
1561 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
,
1563 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " local:%s",
1564 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
,
1566 if (bs
->key
.vrfname
[0])
1567 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " vrf:%s",
1569 if (bs
->key
.ifname
[0])
1570 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " ifname:%s",
1578 int bs_observer_add(struct bfd_session
*bs
)
1580 struct bfd_session_observer
*bso
;
1582 bso
= XCALLOC(MTYPE_BFDD_SESSION_OBSERVER
, sizeof(*bso
));
1584 bso
->bso_addr
.family
= bs
->key
.family
;
1585 memcpy(&bso
->bso_addr
.u
.prefix
, &bs
->key
.local
,
1586 sizeof(bs
->key
.local
));
1588 TAILQ_INSERT_TAIL(&bglobal
.bg_obslist
, bso
, bso_entry
);
1593 void bs_observer_del(struct bfd_session_observer
*bso
)
1595 TAILQ_REMOVE(&bglobal
.bg_obslist
, bso
, bso_entry
);
1596 XFREE(MTYPE_BFDD_SESSION_OBSERVER
, bso
);
1599 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
1601 memset(bpc
, 0, sizeof(*bpc
));
1603 bpc
->bpc_ipv4
= (bs
->key
.family
== AF_INET
);
1604 bpc
->bpc_mhop
= bs
->key
.mhop
;
1606 switch (bs
->key
.family
) {
1608 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET
;
1609 memcpy(&bpc
->bpc_peer
.sa_sin
.sin_addr
, &bs
->key
.peer
,
1610 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
1612 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
))) {
1613 bpc
->bpc_local
.sa_sin
.sin_family
= AF_INET6
;
1614 memcpy(&bpc
->bpc_local
.sa_sin
.sin_addr
, &bs
->key
.local
,
1615 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
1620 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET6
;
1621 memcpy(&bpc
->bpc_peer
.sa_sin6
.sin6_addr
, &bs
->key
.peer
,
1622 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
1624 bpc
->bpc_local
.sa_sin6
.sin6_family
= AF_INET6
;
1625 memcpy(&bpc
->bpc_local
.sa_sin6
.sin6_addr
, &bs
->key
.local
,
1626 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
1630 if (bs
->key
.ifname
[0]) {
1631 bpc
->bpc_has_localif
= true;
1632 strlcpy(bpc
->bpc_localif
, bs
->key
.ifname
,
1633 sizeof(bpc
->bpc_localif
));
1636 if (bs
->key
.vrfname
[0]) {
1637 bpc
->bpc_has_vrfname
= true;
1638 strlcpy(bpc
->bpc_vrfname
, bs
->key
.vrfname
,
1639 sizeof(bpc
->bpc_vrfname
));
1645 * BFD hash data structures to find sessions.
1647 static struct hash
*bfd_id_hash
;
1648 static struct hash
*bfd_key_hash
;
1650 static unsigned int bfd_id_hash_do(const void *p
);
1651 static unsigned int bfd_key_hash_do(const void *p
);
1653 static void _bfd_free(struct hash_bucket
*hb
,
1654 void *arg
__attribute__((__unused__
)));
1656 /* BFD hash for our discriminator. */
1657 static unsigned int bfd_id_hash_do(const void *p
)
1659 const struct bfd_session
*bs
= p
;
1661 return jhash_1word(bs
->discrs
.my_discr
, 0);
1664 static bool bfd_id_hash_cmp(const void *n1
, const void *n2
)
1666 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1668 return bs1
->discrs
.my_discr
== bs2
->discrs
.my_discr
;
1671 /* BFD hash for single hop. */
1672 static unsigned int bfd_key_hash_do(const void *p
)
1674 const struct bfd_session
*bs
= p
;
1676 return jhash(&bs
->key
, sizeof(bs
->key
), 0);
1679 static bool bfd_key_hash_cmp(const void *n1
, const void *n2
)
1681 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1683 return memcmp(&bs1
->key
, &bs2
->key
, sizeof(bs1
->key
)) == 0;
1688 * Hash public interface / exported functions.
1691 /* Lookup functions. */
1692 struct bfd_session
*bfd_id_lookup(uint32_t id
)
1694 struct bfd_session bs
;
1696 bs
.discrs
.my_discr
= id
;
1698 return hash_lookup(bfd_id_hash
, &bs
);
1701 struct bfd_key_walk_partial_lookup
{
1702 struct bfd_session
*given
;
1703 struct bfd_session
*result
;
1706 /* ignore some parameters */
1707 static int bfd_key_lookup_ignore_partial_walker(struct hash_bucket
*b
,
1710 struct bfd_key_walk_partial_lookup
*ctx
=
1711 (struct bfd_key_walk_partial_lookup
*)data
;
1712 struct bfd_session
*given
= ctx
->given
;
1713 struct bfd_session
*parsed
= b
->data
;
1715 if (given
->key
.family
!= parsed
->key
.family
)
1716 return HASHWALK_CONTINUE
;
1717 if (given
->key
.mhop
!= parsed
->key
.mhop
)
1718 return HASHWALK_CONTINUE
;
1719 if (memcmp(&given
->key
.peer
, &parsed
->key
.peer
,
1720 sizeof(struct in6_addr
)))
1721 return HASHWALK_CONTINUE
;
1722 if (memcmp(given
->key
.vrfname
, parsed
->key
.vrfname
, MAXNAMELEN
))
1723 return HASHWALK_CONTINUE
;
1724 ctx
->result
= parsed
;
1725 /* ignore localaddr or interface */
1726 return HASHWALK_ABORT
;
1729 struct bfd_session
*bfd_key_lookup(struct bfd_key key
)
1731 struct bfd_session bs
, *bsp
;
1732 struct bfd_key_walk_partial_lookup ctx
;
1733 char peer_buf
[INET6_ADDRSTRLEN
];
1736 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1740 inet_ntop(bs
.key
.family
, &bs
.key
.peer
, peer_buf
,
1742 /* Handle cases where local-address is optional. */
1743 if (memcmp(&bs
.key
.local
, &zero_addr
, sizeof(bs
.key
.local
))) {
1744 memset(&bs
.key
.local
, 0, sizeof(bs
.key
.local
));
1745 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1747 if (bglobal
.debug_peer_event
) {
1748 char addr_buf
[INET6_ADDRSTRLEN
];
1749 inet_ntop(bs
.key
.family
, &key
.local
, addr_buf
,
1752 " peer %s found, but loc-addr %s ignored",
1753 peer_buf
, addr_buf
);
1760 /* Handle cases where ifname is optional. */
1761 if (bs
.key
.ifname
[0]) {
1762 memset(bs
.key
.ifname
, 0, sizeof(bs
.key
.ifname
));
1763 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1765 if (bglobal
.debug_peer_event
)
1766 zlog_debug(" peer %s found, but ifp %s ignored",
1767 peer_buf
, key
.ifname
);
1772 /* Handle cases where local-address and ifname are optional. */
1773 if (bs
.key
.family
== AF_INET
) {
1774 memset(&bs
.key
.local
, 0, sizeof(bs
.key
.local
));
1775 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1777 if (bglobal
.debug_peer_event
) {
1778 char addr_buf
[INET6_ADDRSTRLEN
];
1779 inet_ntop(bs
.key
.family
, &bs
.key
.local
,
1780 addr_buf
, sizeof(addr_buf
));
1782 " peer %s found, but ifp %s and loc-addr %s ignored",
1783 peer_buf
, key
.ifname
, addr_buf
);
1790 /* Handle case where a context more complex ctx is present.
1791 * input has no iface nor local-address, but a context may
1794 * Only applies to IPv4, because IPv6 requires either
1795 * local-address or interface.
1797 if (!bs
.key
.mhop
&& bs
.key
.family
== AF_INET
) {
1800 hash_walk(bfd_key_hash
, &bfd_key_lookup_ignore_partial_walker
,
1805 if (bglobal
.debug_peer_event
)
1807 " peer %s found, but ifp and/or loc-addr params ignored",
1817 * Delete functions searches and remove the item from the hash and
1818 * returns a pointer to the removed item data. If the item was not found
1819 * then it returns NULL.
1821 * The data stored inside the hash is not free()ed, so you must do it
1822 * manually after getting the pointer back.
1824 struct bfd_session
*bfd_id_delete(uint32_t id
)
1826 struct bfd_session bs
;
1828 bs
.discrs
.my_discr
= id
;
1830 return hash_release(bfd_id_hash
, &bs
);
1833 struct bfd_session
*bfd_key_delete(struct bfd_key key
)
1835 struct bfd_session bs
, *bsp
;
1838 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1839 if (bsp
== NULL
&& key
.ifname
[0]) {
1840 memset(bs
.key
.ifname
, 0, sizeof(bs
.key
.ifname
));
1841 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1844 return hash_release(bfd_key_hash
, bsp
);
1847 /* Iteration functions. */
1848 void bfd_id_iterate(hash_iter_func hif
, void *arg
)
1850 hash_iterate(bfd_id_hash
, hif
, arg
);
1853 void bfd_key_iterate(hash_iter_func hif
, void *arg
)
1855 hash_iterate(bfd_key_hash
, hif
, arg
);
1861 * Inserts session into hash and returns `true` on success, otherwise
1864 bool bfd_id_insert(struct bfd_session
*bs
)
1866 return (hash_get(bfd_id_hash
, bs
, hash_alloc_intern
) == bs
);
1869 bool bfd_key_insert(struct bfd_session
*bs
)
1871 return (hash_get(bfd_key_hash
, bs
, hash_alloc_intern
) == bs
);
1874 void bfd_initialize(void)
1876 bfd_id_hash
= hash_create(bfd_id_hash_do
, bfd_id_hash_cmp
,
1877 "BFD session discriminator hash");
1878 bfd_key_hash
= hash_create(bfd_key_hash_do
, bfd_key_hash_cmp
,
1879 "BFD session hash");
1880 TAILQ_INIT(&bplist
);
1883 static void _bfd_free(struct hash_bucket
*hb
,
1884 void *arg
__attribute__((__unused__
)))
1886 struct bfd_session
*bs
= hb
->data
;
1888 bfd_session_free(bs
);
1891 void bfd_shutdown(void)
1893 struct bfd_profile
*bp
;
1896 * Close and free all BFD sessions.
1898 * _bfd_free() will call bfd_session_free() which will take care
1899 * of removing the session from all hashes, so we just run an
1900 * assert() here to make sure it really happened.
1902 bfd_id_iterate(_bfd_free
, NULL
);
1903 assert(bfd_key_hash
->count
== 0);
1905 /* Now free the hashes themselves. */
1906 hash_free(bfd_id_hash
);
1907 hash_free(bfd_key_hash
);
1909 /* Free all profile allocations. */
1910 while ((bp
= TAILQ_FIRST(&bplist
)) != NULL
)
1911 bfd_profile_free(bp
);
1914 struct bfd_session_iterator
{
1917 const struct bfd_session
*bsi_bs
;
1920 static int _bfd_session_next(struct hash_bucket
*hb
, void *arg
)
1922 struct bfd_session_iterator
*bsi
= arg
;
1923 struct bfd_session
*bs
= hb
->data
;
1925 /* Previous entry signaled stop. */
1926 if (bsi
->bsi_stop
== 1) {
1927 /* Match the single/multi hop sessions. */
1928 if (bs
->key
.mhop
!= bsi
->bsi_mhop
)
1929 return HASHWALK_CONTINUE
;
1932 return HASHWALK_ABORT
;
1935 /* We found the current item, stop in the next one. */
1936 if (bsi
->bsi_bs
== hb
->data
) {
1938 /* Set entry to NULL to signal end of list. */
1940 } else if (bsi
->bsi_bs
== NULL
&& bsi
->bsi_mhop
== bs
->key
.mhop
) {
1941 /* We want the first list item. */
1943 bsi
->bsi_bs
= hb
->data
;
1944 return HASHWALK_ABORT
;
1947 return HASHWALK_CONTINUE
;
1951 * bfd_session_next: uses the current session to find the next.
1953 * `bs` might point to NULL to get the first item of the data structure.
1955 const struct bfd_session
*bfd_session_next(const struct bfd_session
*bs
,
1958 struct bfd_session_iterator bsi
;
1962 bsi
.bsi_mhop
= mhop
;
1963 hash_walk(bfd_key_hash
, _bfd_session_next
, &bsi
);
1964 if (bsi
.bsi_stop
== 0)
1970 static void _bfd_session_remove_manual(struct hash_bucket
*hb
,
1971 void *arg
__attribute__((__unused__
)))
1973 struct bfd_session
*bs
= hb
->data
;
1975 /* Delete only manually configured sessions. */
1976 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
) == 0)
1980 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
);
1982 /* Don't delete sessions still in use. */
1983 if (bs
->refcount
!= 0)
1986 bfd_session_free(bs
);
1990 * bfd_sessions_remove_manual: remove all manually configured sessions.
1992 * NOTE: this function doesn't remove automatically created sessions.
1994 void bfd_sessions_remove_manual(void)
1996 hash_iterate(bfd_key_hash
, _bfd_session_remove_manual
, NULL
);
2000 * Profile related hash functions.
2002 static void _bfd_profile_update(struct hash_bucket
*hb
, void *arg
)
2004 struct bfd_profile
*bp
= arg
;
2005 struct bfd_session
*bs
= hb
->data
;
2007 /* This session is not using the profile. */
2008 if (bs
->profile_name
== NULL
|| strcmp(bs
->profile_name
, bp
->name
) != 0)
2011 bfd_profile_apply(bp
->name
, bs
);
2014 void bfd_profile_update(struct bfd_profile
*bp
)
2016 hash_iterate(bfd_key_hash
, _bfd_profile_update
, bp
);
2019 static void _bfd_profile_detach(struct hash_bucket
*hb
, void *arg
)
2021 struct bfd_profile
*bp
= arg
;
2022 struct bfd_session
*bs
= hb
->data
;
2024 /* This session is not using the profile. */
2025 if (bs
->profile_name
== NULL
|| strcmp(bs
->profile_name
, bp
->name
) != 0)
2028 bfd_profile_remove(bs
);
2031 static void bfd_profile_detach(struct bfd_profile
*bp
)
2033 hash_iterate(bfd_key_hash
, _bfd_profile_detach
, bp
);
2037 * VRF related functions.
2039 static int bfd_vrf_new(struct vrf
*vrf
)
2041 if (bglobal
.debug_zebra
)
2042 zlog_debug("VRF Created: %s(%u)", vrf
->name
, vrf
->vrf_id
);
2047 static int bfd_vrf_delete(struct vrf
*vrf
)
2049 if (bglobal
.debug_zebra
)
2050 zlog_debug("VRF Deletion: %s(%u)", vrf
->name
, vrf
->vrf_id
);
2055 static int bfd_vrf_update(struct vrf
*vrf
)
2057 if (!vrf_is_enabled(vrf
))
2060 if (bglobal
.debug_zebra
)
2061 zlog_debug("VRF update: %s(%u)", vrf
->name
, vrf
->vrf_id
);
2063 /* a different name is given; update bfd list */
2064 bfdd_sessions_enable_vrf(vrf
);
2068 static int bfd_vrf_enable(struct vrf
*vrf
)
2070 struct bfd_vrf_global
*bvrf
;
2072 /* a different name */
2074 bvrf
= XCALLOC(MTYPE_BFDD_VRF
, sizeof(struct bfd_vrf_global
));
2076 vrf
->info
= (void *)bvrf
;
2078 /* Disable sockets if using data plane. */
2079 if (bglobal
.bg_use_dplane
) {
2082 bvrf
->bg_shop6
= -1;
2083 bvrf
->bg_mhop6
= -1;
2085 bvrf
->bg_echov6
= -1;
2090 if (bglobal
.debug_zebra
)
2091 zlog_debug("VRF enable add %s id %u", vrf
->name
, vrf
->vrf_id
);
2093 if (vrf
->vrf_id
== VRF_DEFAULT
||
2094 vrf_get_backend() == VRF_BACKEND_NETNS
) {
2096 bvrf
->bg_shop
= bp_udp_shop(vrf
);
2098 bvrf
->bg_mhop
= bp_udp_mhop(vrf
);
2099 if (!bvrf
->bg_shop6
)
2100 bvrf
->bg_shop6
= bp_udp6_shop(vrf
);
2101 if (!bvrf
->bg_mhop6
)
2102 bvrf
->bg_mhop6
= bp_udp6_mhop(vrf
);
2104 bvrf
->bg_echo
= bp_echo_socket(vrf
);
2105 if (!bvrf
->bg_echov6
)
2106 bvrf
->bg_echov6
= bp_echov6_socket(vrf
);
2108 if (!bvrf
->bg_ev
[0] && bvrf
->bg_shop
!= -1)
2109 thread_add_read(master
, bfd_recv_cb
, bvrf
,
2110 bvrf
->bg_shop
, &bvrf
->bg_ev
[0]);
2111 if (!bvrf
->bg_ev
[1] && bvrf
->bg_mhop
!= -1)
2112 thread_add_read(master
, bfd_recv_cb
, bvrf
,
2113 bvrf
->bg_mhop
, &bvrf
->bg_ev
[1]);
2114 if (!bvrf
->bg_ev
[2] && bvrf
->bg_shop6
!= -1)
2115 thread_add_read(master
, bfd_recv_cb
, bvrf
,
2116 bvrf
->bg_shop6
, &bvrf
->bg_ev
[2]);
2117 if (!bvrf
->bg_ev
[3] && bvrf
->bg_mhop6
!= -1)
2118 thread_add_read(master
, bfd_recv_cb
, bvrf
,
2119 bvrf
->bg_mhop6
, &bvrf
->bg_ev
[3]);
2120 if (!bvrf
->bg_ev
[4] && bvrf
->bg_echo
!= -1)
2121 thread_add_read(master
, bfd_recv_cb
, bvrf
,
2122 bvrf
->bg_echo
, &bvrf
->bg_ev
[4]);
2123 if (!bvrf
->bg_ev
[5] && bvrf
->bg_echov6
!= -1)
2124 thread_add_read(master
, bfd_recv_cb
, bvrf
,
2125 bvrf
->bg_echov6
, &bvrf
->bg_ev
[5]);
2127 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
2128 bfdd_zclient_register(vrf
->vrf_id
);
2129 bfdd_sessions_enable_vrf(vrf
);
2134 static int bfd_vrf_disable(struct vrf
*vrf
)
2136 struct bfd_vrf_global
*bvrf
;
2142 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
2143 bfdd_sessions_disable_vrf(vrf
);
2144 bfdd_zclient_unregister(vrf
->vrf_id
);
2147 if (bglobal
.debug_zebra
)
2148 zlog_debug("VRF disable %s id %d", vrf
->name
, vrf
->vrf_id
);
2150 /* Disable read/write poll triggering. */
2151 THREAD_OFF(bvrf
->bg_ev
[0]);
2152 THREAD_OFF(bvrf
->bg_ev
[1]);
2153 THREAD_OFF(bvrf
->bg_ev
[2]);
2154 THREAD_OFF(bvrf
->bg_ev
[3]);
2155 THREAD_OFF(bvrf
->bg_ev
[4]);
2156 THREAD_OFF(bvrf
->bg_ev
[5]);
2158 /* Close all descriptors. */
2159 socket_close(&bvrf
->bg_echo
);
2160 socket_close(&bvrf
->bg_shop
);
2161 socket_close(&bvrf
->bg_mhop
);
2162 if (bvrf
->bg_shop6
!= -1)
2163 socket_close(&bvrf
->bg_shop6
);
2164 if (bvrf
->bg_mhop6
!= -1)
2165 socket_close(&bvrf
->bg_mhop6
);
2166 socket_close(&bvrf
->bg_echo
);
2167 if (bvrf
->bg_echov6
!= -1)
2168 socket_close(&bvrf
->bg_echov6
);
2171 XFREE(MTYPE_BFDD_VRF
, bvrf
);
2177 void bfd_vrf_init(void)
2179 vrf_init(bfd_vrf_new
, bfd_vrf_enable
, bfd_vrf_disable
,
2180 bfd_vrf_delete
, bfd_vrf_update
);
2183 void bfd_vrf_terminate(void)
2188 struct bfd_vrf_global
*bfd_vrf_look_by_session(struct bfd_session
*bfd
)
2192 if (!vrf_is_backend_netns()) {
2193 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
2195 return (struct bfd_vrf_global
*)vrf
->info
;
2202 return bfd
->vrf
->info
;
2205 void bfd_session_update_vrf_name(struct bfd_session
*bs
, struct vrf
*vrf
)
2210 hash_release(bfd_key_hash
, bs
);
2212 * HACK: Change the BFD VRF in the running configuration directly,
2213 * bypassing the northbound layer. This is necessary to avoid deleting
2214 * the BFD and readding it in the new VRF, which would have
2215 * several implications.
2217 if (yang_module_find("frr-bfdd") && bs
->key
.vrfname
[0]) {
2218 struct lyd_node
*bfd_dnode
;
2219 char xpath
[XPATH_MAXLEN
], xpath_srcaddr
[XPATH_MAXLEN
+ 32];
2220 char oldpath
[XPATH_MAXLEN
], newpath
[XPATH_MAXLEN
];
2221 char addr_buf
[INET6_ADDRSTRLEN
];
2226 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
, sizeof(addr_buf
));
2227 snprintf(xpath_srcaddr
, sizeof(xpath_srcaddr
), "[source-addr='%s']",
2230 xpath_srcaddr
[0] = 0;
2231 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
, sizeof(addr_buf
));
2232 slen
= snprintf(xpath
, sizeof(xpath
),
2233 "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']",
2234 bs
->key
.mhop
? "multi-hop" : "single-hop", xpath_srcaddr
,
2236 if (bs
->key
.ifname
[0])
2237 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
2238 "[interface='%s']", bs
->key
.ifname
);
2240 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
2242 snprintf(xpath
+ slen
, sizeof(xpath
) - slen
, "[vrf='%s']/vrf",
2245 bfd_dnode
= yang_dnode_get(running_config
->dnode
, xpath
,
2248 yang_dnode_get_path(bfd_dnode
->parent
, oldpath
,
2250 yang_dnode_change_leaf(bfd_dnode
, vrf
->name
);
2251 yang_dnode_get_path(bfd_dnode
->parent
, newpath
,
2253 nb_running_move_tree(oldpath
, newpath
);
2254 running_config
->version
++;
2257 memset(bs
->key
.vrfname
, 0, sizeof(bs
->key
.vrfname
));
2258 strlcpy(bs
->key
.vrfname
, vrf
->name
, sizeof(bs
->key
.vrfname
));
2259 hash_get(bfd_key_hash
, bs
, hash_alloc_intern
);
2262 unsigned long bfd_get_session_count(void)
2264 return bfd_key_hash
->count
;