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
);
56 * Remove BFD profile from all BFD sessions so we don't leave dangling
59 static void bfd_profile_detach(struct bfd_profile
*bp
);
61 /* Zeroed array with the size of an IPv6 address. */
62 struct in6_addr zero_addr
;
64 /** BFD profiles list. */
65 struct bfdproflist bplist
;
70 struct bfd_profile
*bfd_profile_lookup(const char *name
)
72 struct bfd_profile
*bp
;
74 TAILQ_FOREACH (bp
, &bplist
, entry
) {
75 if (strcmp(name
, bp
->name
))
84 static void bfd_profile_set_default(struct bfd_profile
*bp
)
86 bp
->admin_shutdown
= false;
87 bp
->detection_multiplier
= BFD_DEFDETECTMULT
;
88 bp
->echo_mode
= false;
90 bp
->minimum_ttl
= BFD_DEF_MHOP_TTL
;
91 bp
->min_echo_rx
= BFD_DEF_REQ_MIN_ECHO_RX
;
92 bp
->min_echo_tx
= BFD_DEF_DES_MIN_ECHO_TX
;
93 bp
->min_rx
= BFD_DEFREQUIREDMINRX
;
94 bp
->min_tx
= BFD_DEFDESIREDMINTX
;
97 struct bfd_profile
*bfd_profile_new(const char *name
)
99 struct bfd_profile
*bp
;
101 /* Search for duplicates. */
102 if (bfd_profile_lookup(name
) != NULL
)
105 /* Allocate, name it and put into list. */
106 bp
= XCALLOC(MTYPE_BFDD_PROFILE
, sizeof(*bp
));
107 strlcpy(bp
->name
, name
, sizeof(bp
->name
));
108 TAILQ_INSERT_TAIL(&bplist
, bp
, entry
);
110 /* Set default values. */
111 bfd_profile_set_default(bp
);
116 void bfd_profile_free(struct bfd_profile
*bp
)
118 /* Detach from any session. */
119 if (bglobal
.bg_shutdown
== false)
120 bfd_profile_detach(bp
);
122 /* Remove from global list. */
123 TAILQ_REMOVE(&bplist
, bp
, entry
);
125 XFREE(MTYPE_BFDD_PROFILE
, bp
);
128 void bfd_profile_apply(const char *profname
, struct bfd_session
*bs
)
130 struct bfd_profile
*bp
;
132 /* Remove previous profile if any. */
133 if (bs
->profile_name
) {
134 /* We are changing profiles. */
135 if (strcmp(bs
->profile_name
, profname
)) {
136 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
138 XSTRDUP(MTYPE_BFDD_PROFILE
, profname
);
140 } else /* Save the current profile name (in case it doesn't exist). */
141 bs
->profile_name
= XSTRDUP(MTYPE_BFDD_PROFILE
, profname
);
143 /* Look up new profile to apply. */
144 bp
= bfd_profile_lookup(profname
);
146 /* Point to profile if it exists. */
149 /* Apply configuration. */
150 bfd_session_apply(bs
);
153 void bfd_session_apply(struct bfd_session
*bs
)
155 struct bfd_profile
*bp
;
156 uint32_t min_tx
= bs
->timers
.desired_min_tx
;
157 uint32_t min_rx
= bs
->timers
.required_min_rx
;
159 /* Pick the source of configuration. */
160 bp
= bs
->profile
? bs
->profile
: &bs
->peer_profile
;
162 /* Set multiplier if not the default. */
163 if (bs
->peer_profile
.detection_multiplier
== BFD_DEFDETECTMULT
)
164 bs
->detect_mult
= bp
->detection_multiplier
;
166 bs
->detect_mult
= bs
->peer_profile
.detection_multiplier
;
168 /* Set timers if not the default. */
169 if (bs
->peer_profile
.min_tx
== BFD_DEFDESIREDMINTX
)
170 bs
->timers
.desired_min_tx
= bp
->min_tx
;
172 bs
->timers
.desired_min_tx
= bs
->peer_profile
.min_tx
;
174 if (bs
->peer_profile
.min_rx
== BFD_DEFREQUIREDMINRX
)
175 bs
->timers
.required_min_rx
= bp
->min_rx
;
177 bs
->timers
.required_min_rx
= bs
->peer_profile
.min_rx
;
179 /* We can only apply echo options on single hop sessions. */
180 if (!CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
181 /* Configure echo timers if they were default. */
182 if (bs
->peer_profile
.min_echo_rx
== BFD_DEF_REQ_MIN_ECHO_RX
)
183 bs
->timers
.required_min_echo_rx
= bp
->min_echo_rx
;
185 bs
->timers
.required_min_echo_rx
=
186 bs
->peer_profile
.min_echo_rx
;
188 if (bs
->peer_profile
.min_echo_tx
== BFD_DEF_DES_MIN_ECHO_TX
)
189 bs
->timers
.desired_min_echo_tx
= bp
->min_echo_tx
;
191 bs
->timers
.desired_min_echo_tx
=
192 bs
->peer_profile
.min_echo_tx
;
194 /* Toggle echo if default value. */
195 if (bs
->peer_profile
.echo_mode
== false)
196 bfd_set_echo(bs
, bp
->echo_mode
);
198 bfd_set_echo(bs
, bs
->peer_profile
.echo_mode
);
200 /* Configure the TTL packet filter. */
201 if (bs
->peer_profile
.minimum_ttl
== BFD_DEF_MHOP_TTL
)
202 bs
->mh_ttl
= bp
->minimum_ttl
;
204 bs
->mh_ttl
= bs
->peer_profile
.minimum_ttl
;
207 /* Toggle 'passive-mode' if default value. */
208 if (bs
->peer_profile
.passive
== false)
209 bfd_set_passive_mode(bs
, bp
->passive
);
211 bfd_set_passive_mode(bs
, bs
->peer_profile
.passive
);
213 /* Toggle 'no shutdown' if default value. */
214 if (bs
->peer_profile
.admin_shutdown
== false)
215 bfd_set_shutdown(bs
, bp
->admin_shutdown
);
217 bfd_set_shutdown(bs
, bs
->peer_profile
.admin_shutdown
);
219 /* If session interval changed negotiate new timers. */
220 if (bs
->ses_state
== PTM_BFD_UP
221 && (bs
->timers
.desired_min_tx
!= min_tx
222 || bs
->timers
.required_min_rx
!= min_rx
))
225 /* Send updated information to data plane. */
226 bfd_dplane_update_session(bs
);
229 void bfd_profile_remove(struct bfd_session
*bs
)
231 /* Remove any previous set profile name. */
232 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
235 bfd_session_apply(bs
);
238 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
239 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
242 memset(key
, 0, sizeof(*key
));
244 switch (peer
->sa_sin
.sin_family
) {
246 key
->family
= AF_INET
;
247 memcpy(&key
->peer
, &peer
->sa_sin
.sin_addr
,
248 sizeof(peer
->sa_sin
.sin_addr
));
249 memcpy(&key
->local
, &local
->sa_sin
.sin_addr
,
250 sizeof(local
->sa_sin
.sin_addr
));
253 key
->family
= AF_INET6
;
254 memcpy(&key
->peer
, &peer
->sa_sin6
.sin6_addr
,
255 sizeof(peer
->sa_sin6
.sin6_addr
));
256 memcpy(&key
->local
, &local
->sa_sin6
.sin6_addr
,
257 sizeof(local
->sa_sin6
.sin6_addr
));
262 if (ifname
&& ifname
[0])
263 strlcpy(key
->ifname
, ifname
, sizeof(key
->ifname
));
264 if (vrfname
&& vrfname
[0])
265 strlcpy(key
->vrfname
, vrfname
, sizeof(key
->vrfname
));
267 strlcpy(key
->vrfname
, VRF_DEFAULT_NAME
, sizeof(key
->vrfname
));
270 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
)
272 struct bfd_session
*bs
;
273 struct peer_label
*pl
;
276 /* Try to find label first. */
277 if (bpc
->bpc_has_label
) {
278 pl
= pl_find(bpc
->bpc_label
);
285 /* Otherwise fallback to peer/local hash lookup. */
286 gen_bfd_key(&key
, &bpc
->bpc_peer
, &bpc
->bpc_local
, bpc
->bpc_mhop
,
287 bpc
->bpc_localif
, bpc
->bpc_vrfname
);
289 return bfd_key_lookup(key
);
293 * Starts a disabled BFD session.
295 * A session is disabled when the specified interface/VRF doesn't exist
296 * yet. It might happen on FRR boot or with virtual interfaces.
298 int bfd_session_enable(struct bfd_session
*bs
)
300 struct interface
*ifp
= NULL
;
301 struct vrf
*vrf
= NULL
;
304 /* We are using data plane, we don't need software. */
309 * If the interface or VRF doesn't exist, then we must register
310 * the session but delay its start.
312 if (bs
->key
.vrfname
[0]) {
313 vrf
= vrf_lookup_by_name(bs
->key
.vrfname
);
316 "session-enable: specified VRF %s doesn't exists.",
321 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
326 if (bs
->key
.ifname
[0]) {
327 ifp
= if_lookup_by_name(bs
->key
.ifname
, vrf
->vrf_id
);
330 "session-enable: specified interface %s (VRF %s) doesn't exist.",
331 bs
->key
.ifname
, vrf
->name
);
336 /* Assign interface/VRF pointers. */
339 /* Assign interface pointer (if any). */
342 /* Attempt to use data plane. */
343 if (bglobal
.bg_use_dplane
&& bfd_dplane_add_session(bs
) == 0) {
344 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bs
);
348 /* Sanity check: don't leak open sockets. */
349 if (bs
->sock
!= -1) {
350 if (bglobal
.debug_peer_event
)
351 zlog_debug("%s: previous socket open", __func__
);
358 * Get socket for transmitting control packets. Note that if we
359 * could use the destination port (3784) for the source
360 * port we wouldn't need a socket per session.
362 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_IPV6
) == 0) {
363 psock
= bp_peer_socket(bs
);
367 psock
= bp_peer_socketv6(bs
);
373 * We've got a valid socket, lets start the timers and the
378 /* Only start timers if we are using active mode. */
379 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
) == 0) {
380 bfd_recvtimer_update(bs
);
381 ptm_bfd_start_xmt_timer(bs
, false);
391 * Disabled a running BFD session.
393 * A session is disabled when the specified interface/VRF gets removed
394 * (e.g. virtual interfaces).
396 void bfd_session_disable(struct bfd_session
*bs
)
398 /* We are using data plane, we don't need software. */
402 /* Free up socket resources. */
403 if (bs
->sock
!= -1) {
408 /* Disable all timers. */
409 bfd_recvtimer_delete(bs
);
410 bfd_xmttimer_delete(bs
);
411 ptm_bfd_echo_stop(bs
);
413 /* Set session down so it doesn't report UP and disabled. */
414 ptm_bfd_sess_dn(bs
, BD_PATH_DOWN
);
417 static uint32_t ptm_bfd_gen_ID(void)
422 * RFC 5880, Section 6.8.1. recommends that we should generate
423 * random session identification numbers.
426 session_id
= ((frr_weak_random() << 16) & 0xFFFF0000)
427 | (frr_weak_random() & 0x0000FFFF);
428 } while (session_id
== 0 || bfd_id_lookup(session_id
) != NULL
);
433 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
)
435 uint64_t jitter
, xmt_TO
;
438 xmt_TO
= is_echo
? bfd
->echo_xmt_TO
: bfd
->xmt_TO
;
441 * From section 6.5.2: trasmit interval should be randomly jittered
443 * 75% and 100% of nominal value, unless detect_mult is 1, then should
445 * between 75% and 90%.
447 maxpercent
= (bfd
->detect_mult
== 1) ? 16 : 26;
448 jitter
= (xmt_TO
* (75 + (frr_weak_random() % maxpercent
))) / 100;
449 /* XXX remove that division above */
452 bfd_echo_xmttimer_update(bfd
, jitter
);
454 bfd_xmttimer_update(bfd
, jitter
);
457 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
)
459 /* Send the scheduled echo packet */
460 /* if ipv4 use the new echo implementation that causes
461 * the packet to be looped in forwarding plane of peer
463 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
) == 0)
465 ptm_bfd_echo_fp_snd(bfd
);
467 ptm_bfd_echo_snd(bfd
);
470 ptm_bfd_echo_snd(bfd
);
472 /* Restart the timer for next time */
473 ptm_bfd_start_xmt_timer(bfd
, true);
476 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
)
478 /* Send the scheduled control packet */
479 ptm_bfd_snd(bfd
, fbit
);
481 /* Restart the timer for next time */
482 ptm_bfd_start_xmt_timer(bfd
, false);
485 void ptm_bfd_echo_stop(struct bfd_session
*bfd
)
487 bfd
->echo_xmt_TO
= 0;
488 bfd
->echo_detect_TO
= 0;
489 UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
);
491 bfd_echo_xmttimer_delete(bfd
);
492 bfd_echo_recvtimer_delete(bfd
);
495 void ptm_bfd_echo_start(struct bfd_session
*bfd
)
497 bfd
->echo_detect_TO
= (bfd
->remote_detect_mult
* bfd
->echo_xmt_TO
);
498 if (bfd
->echo_detect_TO
> 0) {
499 bfd_echo_recvtimer_update(bfd
);
500 ptm_bfd_echo_xmt_TO(bfd
);
504 void ptm_bfd_sess_up(struct bfd_session
*bfd
)
506 int old_state
= bfd
->ses_state
;
509 bfd
->ses_state
= PTM_BFD_UP
;
510 monotime(&bfd
->uptime
);
512 /* Connection is up, lets negotiate timers. */
513 bfd_set_polling(bfd
);
515 /* Start sending control packets with poll bit immediately. */
518 control_notify(bfd
, bfd
->ses_state
);
520 if (old_state
!= bfd
->ses_state
) {
521 bfd
->stats
.session_up
++;
522 if (bglobal
.debug_peer_event
)
523 zlog_debug("state-change: [%s] %s -> %s",
524 bs_to_string(bfd
), state_list
[old_state
].str
,
525 state_list
[bfd
->ses_state
].str
);
529 void ptm_bfd_sess_dn(struct bfd_session
*bfd
, uint8_t diag
)
531 int old_state
= bfd
->ses_state
;
533 bfd
->local_diag
= diag
;
534 bfd
->discrs
.remote_discr
= 0;
535 bfd
->ses_state
= PTM_BFD_DOWN
;
537 bfd
->demand_mode
= 0;
538 monotime(&bfd
->downtime
);
541 * Only attempt to send if we have a valid socket:
542 * this function might be called by session disablers and in
543 * this case we won't have a valid socket (i.e. interface was
544 * removed or VRF doesn't exist anymore).
549 /* Slow down the control packets, the connection is down. */
550 bs_set_slow_timers(bfd
);
552 /* only signal clients when going from up->down state */
553 if (old_state
== PTM_BFD_UP
)
554 control_notify(bfd
, PTM_BFD_DOWN
);
556 /* Stop echo packet transmission if they are active */
557 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
558 ptm_bfd_echo_stop(bfd
);
560 /* Stop attempting to transmit or expect control packets if passive. */
561 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_PASSIVE
)) {
562 bfd_recvtimer_delete(bfd
);
563 bfd_xmttimer_delete(bfd
);
566 if (old_state
!= bfd
->ses_state
) {
567 bfd
->stats
.session_down
++;
568 if (bglobal
.debug_peer_event
)
569 zlog_debug("state-change: [%s] %s -> %s reason:%s",
570 bs_to_string(bfd
), state_list
[old_state
].str
,
571 state_list
[bfd
->ses_state
].str
,
572 get_diag_str(bfd
->local_diag
));
575 /* clear peer's mac address */
576 UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MAC_SET
);
577 memset(bfd
->peer_hw_addr
, 0, sizeof(bfd
->peer_hw_addr
));
578 /* reset local address ,it might has been be changed after bfd is up*/
579 memset(&bfd
->local_address
, 0, sizeof(bfd
->local_address
));
585 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
588 struct bfd_session
*bs
;
590 bs
= bfd_id_lookup(ldisc
);
594 switch (bs
->key
.family
) {
596 if (memcmp(&sa
->sa_sin
.sin_addr
, &bs
->key
.peer
,
597 sizeof(sa
->sa_sin
.sin_addr
)))
601 if (memcmp(&sa
->sa_sin6
.sin6_addr
, &bs
->key
.peer
,
602 sizeof(sa
->sa_sin6
.sin6_addr
)))
610 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
611 struct sockaddr_any
*peer
,
612 struct sockaddr_any
*local
,
613 struct interface
*ifp
,
620 /* Find our session using the ID signaled by the remote end. */
621 if (cp
->discrs
.remote_discr
)
622 return bfd_find_disc(peer
, ntohl(cp
->discrs
.remote_discr
));
624 /* Search for session without using discriminator. */
625 vrf
= vrf_lookup_by_id(vrfid
);
627 gen_bfd_key(&key
, peer
, local
, is_mhop
, ifp
? ifp
->name
: NULL
,
628 vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
630 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
631 return bfd_key_lookup(key
);
634 void bfd_xmt_cb(struct thread
*t
)
636 struct bfd_session
*bs
= THREAD_ARG(t
);
638 ptm_bfd_xmt_TO(bs
, 0);
641 void bfd_echo_xmt_cb(struct thread
*t
)
643 struct bfd_session
*bs
= THREAD_ARG(t
);
645 if (bs
->echo_xmt_TO
> 0)
646 ptm_bfd_echo_xmt_TO(bs
);
649 /* Was ptm_bfd_detect_TO() */
650 void bfd_recvtimer_cb(struct thread
*t
)
652 struct bfd_session
*bs
= THREAD_ARG(t
);
654 switch (bs
->ses_state
) {
657 ptm_bfd_sess_dn(bs
, BD_CONTROL_EXPIRED
);
662 /* Was ptm_bfd_echo_detect_TO() */
663 void bfd_echo_recvtimer_cb(struct thread
*t
)
665 struct bfd_session
*bs
= THREAD_ARG(t
);
667 switch (bs
->ses_state
) {
670 ptm_bfd_sess_dn(bs
, BD_ECHO_FAILED
);
675 struct bfd_session
*bfd_session_new(void)
677 struct bfd_session
*bs
;
679 bs
= XCALLOC(MTYPE_BFDD_CONFIG
, sizeof(*bs
));
681 /* Set peer session defaults. */
682 bfd_profile_set_default(&bs
->peer_profile
);
684 bs
->timers
.desired_min_tx
= BFD_DEFDESIREDMINTX
;
685 bs
->timers
.required_min_rx
= BFD_DEFREQUIREDMINRX
;
686 bs
->timers
.required_min_echo_rx
= BFD_DEF_REQ_MIN_ECHO_RX
;
687 bs
->timers
.desired_min_echo_tx
= BFD_DEF_DES_MIN_ECHO_TX
;
688 bs
->detect_mult
= BFD_DEFDETECTMULT
;
689 bs
->mh_ttl
= BFD_DEF_MHOP_TTL
;
690 bs
->ses_state
= PTM_BFD_DOWN
;
692 /* Initiate connection with slow timers. */
693 bs_set_slow_timers(bs
);
695 /* Initiate remote settings as well. */
696 bs
->remote_timers
= bs
->cur_timers
;
697 bs
->remote_detect_mult
= BFD_DEFDETECTMULT
;
700 monotime(&bs
->uptime
);
701 bs
->downtime
= bs
->uptime
;
706 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
)
708 /* New label treatment:
709 * - Check if the label is taken;
710 * - Try to allocate the memory for it and register;
712 if (bs
->pl
== NULL
) {
713 if (pl_find(nlabel
) != NULL
) {
714 /* Someone is already using it. */
724 * Test label change consistency:
725 * - Do nothing if it's the same label;
726 * - Check if the future label is already taken;
729 if (strcmp(nlabel
, bs
->pl
->pl_label
) == 0)
731 if (pl_find(nlabel
) != NULL
)
734 strlcpy(bs
->pl
->pl_label
, nlabel
, sizeof(bs
->pl
->pl_label
));
738 static void _bfd_session_update(struct bfd_session
*bs
,
739 struct bfd_peer_cfg
*bpc
)
741 if (bpc
->bpc_has_txinterval
) {
742 bs
->timers
.desired_min_tx
= bpc
->bpc_txinterval
* 1000;
743 bs
->peer_profile
.min_tx
= bs
->timers
.desired_min_tx
;
746 if (bpc
->bpc_has_recvinterval
) {
747 bs
->timers
.required_min_rx
= bpc
->bpc_recvinterval
* 1000;
748 bs
->peer_profile
.min_rx
= bs
->timers
.required_min_rx
;
751 if (bpc
->bpc_has_detectmultiplier
) {
752 bs
->detect_mult
= bpc
->bpc_detectmultiplier
;
753 bs
->peer_profile
.detection_multiplier
= bs
->detect_mult
;
756 if (bpc
->bpc_has_echorecvinterval
) {
757 bs
->timers
.required_min_echo_rx
= bpc
->bpc_echorecvinterval
* 1000;
758 bs
->peer_profile
.min_echo_rx
= bs
->timers
.required_min_echo_rx
;
761 if (bpc
->bpc_has_echotxinterval
) {
762 bs
->timers
.desired_min_echo_tx
= bpc
->bpc_echotxinterval
* 1000;
763 bs
->peer_profile
.min_echo_tx
= bs
->timers
.desired_min_echo_tx
;
766 if (bpc
->bpc_has_label
)
767 bfd_session_update_label(bs
, bpc
->bpc_label
);
770 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
772 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
774 if (bpc
->bpc_has_minimum_ttl
) {
775 bs
->mh_ttl
= bpc
->bpc_minimum_ttl
;
776 bs
->peer_profile
.minimum_ttl
= bpc
->bpc_minimum_ttl
;
779 bs
->peer_profile
.echo_mode
= bpc
->bpc_echo
;
780 bfd_set_echo(bs
, bpc
->bpc_echo
);
783 * Shutdown needs to be the last in order to avoid timers enable when
784 * the session is disabled.
786 bs
->peer_profile
.admin_shutdown
= bpc
->bpc_shutdown
;
787 bfd_set_passive_mode(bs
, bpc
->bpc_passive
);
788 bfd_set_shutdown(bs
, bpc
->bpc_shutdown
);
791 * Apply profile last: it also calls `bfd_set_shutdown`.
793 * There is no problem calling `shutdown` twice if the value doesn't
794 * change or if it is overridden by peer specific configuration.
796 if (bpc
->bpc_has_profile
)
797 bfd_profile_apply(bpc
->bpc_profile
, bs
);
800 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
802 /* User didn't want to update, return failure. */
803 if (bpc
->bpc_createonly
)
806 _bfd_session_update(bs
, bpc
);
808 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
813 void bfd_session_free(struct bfd_session
*bs
)
815 struct bfd_session_observer
*bso
;
817 bfd_session_disable(bs
);
819 /* Remove session from data plane if any. */
820 bfd_dplane_delete_session(bs
);
822 bfd_key_delete(bs
->key
);
823 bfd_id_delete(bs
->discrs
.my_discr
);
825 /* Remove observer if any. */
826 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
827 if (bso
->bso_bs
!= bs
)
833 bs_observer_del(bso
);
837 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
838 XFREE(MTYPE_BFDD_CONFIG
, bs
);
841 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
)
843 struct bfd_session
*bfd
, *l_bfd
;
845 /* check to see if this needs a new session */
846 l_bfd
= bs_peer_find(bpc
);
848 /* Requesting a duplicated peer means update configuration. */
849 if (bfd_session_update(l_bfd
, bpc
) == 0)
855 /* Get BFD session storage with its defaults. */
856 bfd
= bfd_session_new();
859 * Store interface/VRF name in case we need to delay session
860 * start. See `bfd_session_enable` for more information.
862 if (bpc
->bpc_has_localif
)
863 strlcpy(bfd
->key
.ifname
, bpc
->bpc_localif
,
864 sizeof(bfd
->key
.ifname
));
866 if (bpc
->bpc_has_vrfname
)
867 strlcpy(bfd
->key
.vrfname
, bpc
->bpc_vrfname
,
868 sizeof(bfd
->key
.vrfname
));
870 strlcpy(bfd
->key
.vrfname
, VRF_DEFAULT_NAME
,
871 sizeof(bfd
->key
.vrfname
));
873 /* Copy remaining data. */
874 if (bpc
->bpc_ipv4
== false)
875 SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
);
877 bfd
->key
.family
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
878 switch (bfd
->key
.family
) {
880 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin
.sin_addr
,
881 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
882 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin
.sin_addr
,
883 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
887 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin6
.sin6_addr
,
888 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
889 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin6
.sin6_addr
,
890 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
899 SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MH
);
901 bfd
->key
.mhop
= bpc
->bpc_mhop
;
903 if (bs_registrate(bfd
) == NULL
)
906 /* Apply other configurations. */
907 _bfd_session_update(bfd
, bpc
);
912 struct bfd_session
*bs_registrate(struct bfd_session
*bfd
)
914 /* Registrate session into data structures. */
916 bfd
->discrs
.my_discr
= ptm_bfd_gen_ID();
919 /* Try to enable session and schedule for packet receive/send. */
920 if (bfd_session_enable(bfd
) == -1) {
921 /* Unrecoverable failure, remove the session/peer. */
922 bfd_session_free(bfd
);
926 /* Add observer if we have moving parts. */
927 if (bfd
->key
.ifname
[0] || bfd
->key
.vrfname
[0] || bfd
->sock
== -1)
928 bs_observer_add(bfd
);
930 if (bglobal
.debug_peer_event
)
931 zlog_debug("session-new: %s", bs_to_string(bfd
));
933 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bfd
);
938 int ptm_bfd_sess_del(struct bfd_peer_cfg
*bpc
)
940 struct bfd_session
*bs
;
942 /* Find session and call free(). */
943 bs
= bs_peer_find(bpc
);
947 /* This pointer is being referenced, don't let it be deleted. */
948 if (bs
->refcount
> 0) {
949 zlog_err("session-delete: refcount failure: %" PRIu64
" references",
954 if (bglobal
.debug_peer_event
)
955 zlog_debug("%s: %s", __func__
, bs_to_string(bs
));
957 control_notify_config(BCM_NOTIFY_CONFIG_DELETE
, bs
);
959 bfd_session_free(bs
);
964 void bfd_set_polling(struct bfd_session
*bs
)
967 * Start polling procedure: the only timers that require polling
968 * to change value without losing connection are:
970 * - Desired minimum transmission interval;
971 * - Required minimum receive interval;
973 * RFC 5880, Section 6.8.3.
979 * bs_<state>_handler() functions implement the BFD state machine
980 * transition mechanism. `<state>` is the current session state and
981 * the parameter `nstate` is the peer new state.
983 static void bs_admin_down_handler(struct bfd_session
*bs
984 __attribute__((__unused__
)),
985 int nstate
__attribute__((__unused__
)))
988 * We are administratively down, there is no state machine
993 static void bs_down_handler(struct bfd_session
*bs
, int nstate
)
996 case PTM_BFD_ADM_DOWN
:
998 * Remote peer doesn't want to talk, so lets keep the
1002 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
1007 * Remote peer agreed that the path is down, lets try to
1010 bs
->ses_state
= PTM_BFD_INIT
;
1013 * RFC 5880, Section 6.1.
1014 * A system taking the Passive role MUST NOT begin
1015 * sending BFD packets for a particular session until
1016 * it has received a BFD packet for that session, and thus
1017 * has learned the remote system's discriminator value.
1019 * Now we can start transmission timer in passive mode.
1021 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
))
1022 ptm_bfd_xmt_TO(bs
, 0);
1028 * Remote peer told us his path is up, lets turn
1029 * activate the session.
1031 ptm_bfd_sess_up(bs
);
1035 if (bglobal
.debug_peer_event
)
1036 zlog_debug("state-change: unhandled neighbor state: %d",
1042 static void bs_init_handler(struct bfd_session
*bs
, int nstate
)
1045 case PTM_BFD_ADM_DOWN
:
1047 * Remote peer doesn't want to talk, so lets make the
1050 ptm_bfd_sess_dn(bs
, BD_NEIGHBOR_DOWN
);
1054 /* Remote peer hasn't moved to first stage yet. */
1059 /* We agreed on the settings and the path is up. */
1060 ptm_bfd_sess_up(bs
);
1064 if (bglobal
.debug_peer_event
)
1065 zlog_debug("state-change: unhandled neighbor state: %d",
1071 static void bs_up_handler(struct bfd_session
*bs
, int nstate
)
1074 case PTM_BFD_ADM_DOWN
:
1076 /* Peer lost or asked to shutdown connection. */
1077 ptm_bfd_sess_dn(bs
, BD_NEIGHBOR_DOWN
);
1082 /* Path is up and working. */
1086 if (bglobal
.debug_peer_event
)
1087 zlog_debug("state-change: unhandled neighbor state: %d",
1093 void bs_state_handler(struct bfd_session
*bs
, int nstate
)
1095 switch (bs
->ses_state
) {
1096 case PTM_BFD_ADM_DOWN
:
1097 bs_admin_down_handler(bs
, nstate
);
1100 bs_down_handler(bs
, nstate
);
1103 bs_init_handler(bs
, nstate
);
1106 bs_up_handler(bs
, nstate
);
1110 if (bglobal
.debug_peer_event
)
1111 zlog_debug("state-change: [%s] is in invalid state: %d",
1112 bs_to_string(bs
), nstate
);
1118 * Handles echo timer manipulation after updating timer.
1120 void bs_echo_timer_handler(struct bfd_session
*bs
)
1125 * Before doing any echo handling, check if it is possible to
1128 * - Check for `echo-mode` configuration.
1129 * - Check that we are not using multi hop (RFC 5883,
1131 * - Check that we are already at the up state.
1133 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
) == 0
1134 || CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)
1135 || bs
->ses_state
!= PTM_BFD_UP
)
1138 /* Remote peer asked to stop echo. */
1139 if (bs
->remote_timers
.required_min_echo
== 0) {
1140 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
1141 ptm_bfd_echo_stop(bs
);
1147 * Calculate the echo transmission timer: we must not send
1148 * echo packets faster than the minimum required time
1149 * announced by the remote system.
1151 * RFC 5880, Section 6.8.9.
1153 old_timer
= bs
->echo_xmt_TO
;
1154 if (bs
->remote_timers
.required_min_echo
> bs
->timers
.desired_min_echo_tx
)
1155 bs
->echo_xmt_TO
= bs
->remote_timers
.required_min_echo
;
1157 bs
->echo_xmt_TO
= bs
->timers
.desired_min_echo_tx
;
1159 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
) == 0
1160 || old_timer
!= bs
->echo_xmt_TO
)
1161 ptm_bfd_echo_start(bs
);
1165 * RFC 5880 Section 6.5.
1167 * When a BFD control packet with the final bit is received, we must
1168 * update the session parameters.
1170 void bs_final_handler(struct bfd_session
*bs
)
1172 /* Start using our new timers. */
1173 bs
->cur_timers
.desired_min_tx
= bs
->timers
.desired_min_tx
;
1174 bs
->cur_timers
.required_min_rx
= bs
->timers
.required_min_rx
;
1177 * TODO: demand mode. See RFC 5880 Section 6.1.
1179 * When using demand mode we must disable the detection timer
1180 * for lost control packets.
1182 if (bs
->demand_mode
) {
1183 /* Notify watchers about changed timers. */
1184 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1189 * Calculate transmission time based on new timers.
1191 * Transmission calculation:
1192 * Unless specified by exceptions at the end of Section 6.8.7, the
1193 * transmission time will be determined by the system with the
1196 * RFC 5880, Section 6.8.7.
1198 if (bs
->timers
.desired_min_tx
> bs
->remote_timers
.required_min_rx
)
1199 bs
->xmt_TO
= bs
->timers
.desired_min_tx
;
1201 bs
->xmt_TO
= bs
->remote_timers
.required_min_rx
;
1203 /* Apply new transmission timer immediately. */
1204 ptm_bfd_start_xmt_timer(bs
, false);
1206 /* Notify watchers about changed timers. */
1207 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1210 void bs_set_slow_timers(struct bfd_session
*bs
)
1213 * BFD connection must use slow timers before going up or after
1214 * losing connectivity to avoid wasting bandwidth.
1216 * RFC 5880, Section 6.8.3.
1218 bs
->cur_timers
.desired_min_tx
= BFD_DEF_SLOWTX
;
1219 bs
->cur_timers
.required_min_rx
= BFD_DEF_SLOWTX
;
1220 bs
->cur_timers
.required_min_echo
= 0;
1222 /* Set the appropriated timeouts for slow connection. */
1223 bs
->detect_TO
= (BFD_DEFDETECTMULT
* BFD_DEF_SLOWTX
);
1224 bs
->xmt_TO
= BFD_DEF_SLOWTX
;
1227 void bfd_set_echo(struct bfd_session
*bs
, bool echo
)
1230 /* Check if echo mode is already active. */
1231 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
1234 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
1236 /* Activate/update echo receive timeout timer. */
1237 if (bs
->bdc
== NULL
)
1238 bs_echo_timer_handler(bs
);
1240 /* Check if echo mode is already disabled. */
1241 if (!CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
1244 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
1246 /* Deactivate timeout timer. */
1247 if (bs
->bdc
== NULL
)
1248 ptm_bfd_echo_stop(bs
);
1252 void bfd_set_shutdown(struct bfd_session
*bs
, bool shutdown
)
1257 * Special case: we are batching changes and the previous state was
1258 * not shutdown. Instead of potentially disconnect a running peer,
1259 * we'll get the current status to validate we were really down.
1261 if (bs
->ses_state
== PTM_BFD_UP
)
1262 is_shutdown
= false;
1264 is_shutdown
= CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1267 /* Already shutdown. */
1271 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1273 /* Handle data plane shutdown case. */
1275 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
1276 bfd_dplane_update_session(bs
);
1277 control_notify(bs
, bs
->ses_state
);
1281 /* Disable all events. */
1282 bfd_recvtimer_delete(bs
);
1283 bfd_echo_recvtimer_delete(bs
);
1284 bfd_xmttimer_delete(bs
);
1285 bfd_echo_xmttimer_delete(bs
);
1287 /* Change and notify state change. */
1288 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
1289 control_notify(bs
, bs
->ses_state
);
1291 /* Don't try to send packets with a disabled session. */
1295 /* Already working. */
1299 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1301 /* Handle data plane shutdown case. */
1303 bs
->ses_state
= PTM_BFD_DOWN
;
1304 bfd_dplane_update_session(bs
);
1305 control_notify(bs
, bs
->ses_state
);
1309 /* Change and notify state change. */
1310 bs
->ses_state
= PTM_BFD_DOWN
;
1311 control_notify(bs
, bs
->ses_state
);
1313 /* Enable timers if non passive, otherwise stop them. */
1314 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
)) {
1315 bfd_recvtimer_delete(bs
);
1316 bfd_xmttimer_delete(bs
);
1318 bfd_recvtimer_update(bs
);
1319 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
1324 void bfd_set_passive_mode(struct bfd_session
*bs
, bool passive
)
1327 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
);
1329 /* Session is already up and running, nothing to do now. */
1330 if (bs
->ses_state
!= PTM_BFD_DOWN
)
1333 /* Lets disable the timers since we are now passive. */
1334 bfd_recvtimer_delete(bs
);
1335 bfd_xmttimer_delete(bs
);
1337 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
);
1339 /* Session is already up and running, nothing to do now. */
1340 if (bs
->ses_state
!= PTM_BFD_DOWN
)
1343 /* Session is down, let it attempt to start the connection. */
1344 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
1345 bfd_recvtimer_update(bs
);
1352 static const char *get_diag_str(int diag
)
1354 for (int i
= 0; diag_list
[i
].str
; i
++) {
1355 if (diag_list
[i
].type
== diag
)
1356 return diag_list
[i
].str
;
1361 const char *satostr(const struct sockaddr_any
*sa
)
1363 #define INETSTR_BUFCOUNT 8
1364 static char buf
[INETSTR_BUFCOUNT
][INET6_ADDRSTRLEN
];
1366 const struct sockaddr_in
*sin
= &sa
->sa_sin
;
1367 const struct sockaddr_in6
*sin6
= &sa
->sa_sin6
;
1369 bufidx
+= (bufidx
+ 1) % INETSTR_BUFCOUNT
;
1372 switch (sin
->sin_family
) {
1374 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
[bufidx
],
1375 sizeof(buf
[bufidx
]));
1378 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, buf
[bufidx
],
1379 sizeof(buf
[bufidx
]));
1383 strlcpy(buf
[bufidx
], "unknown", sizeof(buf
[bufidx
]));
1390 const char *diag2str(uint8_t diag
)
1396 return "control detection time expired";
1398 return "echo function failed";
1400 return "neighbor signaled session down";
1402 return "forwarding plane reset";
1406 return "concatenated path down";
1408 return "administratively down";
1410 return "reverse concatenated path down";
1416 int strtosa(const char *addr
, struct sockaddr_any
*sa
)
1418 memset(sa
, 0, sizeof(*sa
));
1420 if (inet_pton(AF_INET
, addr
, &sa
->sa_sin
.sin_addr
) == 1) {
1421 sa
->sa_sin
.sin_family
= AF_INET
;
1422 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1423 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
1424 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1428 if (inet_pton(AF_INET6
, addr
, &sa
->sa_sin6
.sin6_addr
) == 1) {
1429 sa
->sa_sin6
.sin6_family
= AF_INET6
;
1430 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1431 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
1432 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1439 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
)
1441 uint64_t year
, month
, day
, hour
, minute
, second
;
1444 #define MINUTES (60)
1445 #define HOURS (60 * MINUTES)
1446 #define DAYS (24 * HOURS)
1447 #define MONTHS (30 * DAYS)
1448 #define YEARS (12 * MONTHS)
1449 if (time
>= YEARS
) {
1450 year
= time
/ YEARS
;
1451 time
-= year
* YEARS
;
1453 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" year(s), ", year
);
1457 if (time
>= MONTHS
) {
1458 month
= time
/ MONTHS
;
1459 time
-= month
* MONTHS
;
1461 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" month(s), ", month
);
1469 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" day(s), ", day
);
1473 if (time
>= HOURS
) {
1474 hour
= time
/ HOURS
;
1475 time
-= hour
* HOURS
;
1477 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" hour(s), ", hour
);
1481 if (time
>= MINUTES
) {
1482 minute
= time
/ MINUTES
;
1483 time
-= minute
* MINUTES
;
1485 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" minute(s), ", minute
);
1489 second
= time
% MINUTES
;
1490 snprintfrr(buf
, buflen
, "%" PRIu64
" second(s)", second
);
1493 const char *bs_to_string(const struct bfd_session
*bs
)
1495 static char buf
[256];
1496 char addr_buf
[INET6_ADDRSTRLEN
];
1498 bool is_mhop
= CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
);
1500 pos
= snprintf(buf
, sizeof(buf
), "mhop:%s", is_mhop
? "yes" : "no");
1501 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " peer:%s",
1502 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
,
1504 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " local:%s",
1505 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
,
1507 if (bs
->key
.vrfname
[0])
1508 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " vrf:%s",
1510 if (bs
->key
.ifname
[0])
1511 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " ifname:%s",
1519 int bs_observer_add(struct bfd_session
*bs
)
1521 struct bfd_session_observer
*bso
;
1523 bso
= XCALLOC(MTYPE_BFDD_SESSION_OBSERVER
, sizeof(*bso
));
1525 bso
->bso_addr
.family
= bs
->key
.family
;
1526 memcpy(&bso
->bso_addr
.u
.prefix
, &bs
->key
.local
,
1527 sizeof(bs
->key
.local
));
1529 TAILQ_INSERT_TAIL(&bglobal
.bg_obslist
, bso
, bso_entry
);
1534 void bs_observer_del(struct bfd_session_observer
*bso
)
1536 TAILQ_REMOVE(&bglobal
.bg_obslist
, bso
, bso_entry
);
1537 XFREE(MTYPE_BFDD_SESSION_OBSERVER
, bso
);
1540 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
1542 memset(bpc
, 0, sizeof(*bpc
));
1544 bpc
->bpc_ipv4
= (bs
->key
.family
== AF_INET
);
1545 bpc
->bpc_mhop
= bs
->key
.mhop
;
1547 switch (bs
->key
.family
) {
1549 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET
;
1550 memcpy(&bpc
->bpc_peer
.sa_sin
.sin_addr
, &bs
->key
.peer
,
1551 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
1553 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
))) {
1554 bpc
->bpc_local
.sa_sin
.sin_family
= AF_INET6
;
1555 memcpy(&bpc
->bpc_local
.sa_sin
.sin_addr
, &bs
->key
.local
,
1556 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
1561 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET6
;
1562 memcpy(&bpc
->bpc_peer
.sa_sin6
.sin6_addr
, &bs
->key
.peer
,
1563 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
1565 bpc
->bpc_local
.sa_sin6
.sin6_family
= AF_INET6
;
1566 memcpy(&bpc
->bpc_local
.sa_sin6
.sin6_addr
, &bs
->key
.local
,
1567 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
1571 if (bs
->key
.ifname
[0]) {
1572 bpc
->bpc_has_localif
= true;
1573 strlcpy(bpc
->bpc_localif
, bs
->key
.ifname
,
1574 sizeof(bpc
->bpc_localif
));
1577 if (bs
->key
.vrfname
[0]) {
1578 bpc
->bpc_has_vrfname
= true;
1579 strlcpy(bpc
->bpc_vrfname
, bs
->key
.vrfname
,
1580 sizeof(bpc
->bpc_vrfname
));
1586 * BFD hash data structures to find sessions.
1588 static struct hash
*bfd_id_hash
;
1589 static struct hash
*bfd_key_hash
;
1591 static unsigned int bfd_id_hash_do(const void *p
);
1592 static unsigned int bfd_key_hash_do(const void *p
);
1594 static void _bfd_free(struct hash_bucket
*hb
,
1595 void *arg
__attribute__((__unused__
)));
1597 /* BFD hash for our discriminator. */
1598 static unsigned int bfd_id_hash_do(const void *p
)
1600 const struct bfd_session
*bs
= p
;
1602 return jhash_1word(bs
->discrs
.my_discr
, 0);
1605 static bool bfd_id_hash_cmp(const void *n1
, const void *n2
)
1607 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1609 return bs1
->discrs
.my_discr
== bs2
->discrs
.my_discr
;
1612 /* BFD hash for single hop. */
1613 static unsigned int bfd_key_hash_do(const void *p
)
1615 const struct bfd_session
*bs
= p
;
1616 struct bfd_key key
= bs
->key
;
1619 * Local address and interface name are optional and
1620 * can be filled any time after session creation.
1621 * Hash key should not depend on these fields.
1623 memset(&key
.local
, 0, sizeof(key
.local
));
1624 memset(key
.ifname
, 0, sizeof(key
.ifname
));
1626 return jhash(&key
, sizeof(key
), 0);
1629 static bool bfd_key_hash_cmp(const void *n1
, const void *n2
)
1631 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1633 if (bs1
->key
.family
!= bs2
->key
.family
)
1635 if (bs1
->key
.mhop
!= bs2
->key
.mhop
)
1637 if (memcmp(&bs1
->key
.peer
, &bs2
->key
.peer
, sizeof(bs1
->key
.peer
)))
1639 if (memcmp(bs1
->key
.vrfname
, bs2
->key
.vrfname
,
1640 sizeof(bs1
->key
.vrfname
)))
1644 * Local address is optional and can be empty.
1645 * If both addresses are not empty and different,
1646 * then the keys are different.
1648 if (memcmp(&bs1
->key
.local
, &zero_addr
, sizeof(bs1
->key
.local
))
1649 && memcmp(&bs2
->key
.local
, &zero_addr
, sizeof(bs2
->key
.local
))
1650 && memcmp(&bs1
->key
.local
, &bs2
->key
.local
, sizeof(bs1
->key
.local
)))
1654 * Interface name is optional and can be empty.
1655 * If both names are not empty and different,
1656 * then the keys are different.
1658 if (bs1
->key
.ifname
[0] && bs2
->key
.ifname
[0]
1659 && memcmp(bs1
->key
.ifname
, bs2
->key
.ifname
,
1660 sizeof(bs1
->key
.ifname
)))
1668 * Hash public interface / exported functions.
1671 /* Lookup functions. */
1672 struct bfd_session
*bfd_id_lookup(uint32_t id
)
1674 struct bfd_session bs
;
1676 bs
.discrs
.my_discr
= id
;
1678 return hash_lookup(bfd_id_hash
, &bs
);
1681 struct bfd_session
*bfd_key_lookup(struct bfd_key key
)
1683 struct bfd_session bs
;
1687 return hash_lookup(bfd_key_hash
, &bs
);
1693 * Delete functions searches and remove the item from the hash and
1694 * returns a pointer to the removed item data. If the item was not found
1695 * then it returns NULL.
1697 * The data stored inside the hash is not free()ed, so you must do it
1698 * manually after getting the pointer back.
1700 struct bfd_session
*bfd_id_delete(uint32_t id
)
1702 struct bfd_session bs
;
1704 bs
.discrs
.my_discr
= id
;
1706 return hash_release(bfd_id_hash
, &bs
);
1709 struct bfd_session
*bfd_key_delete(struct bfd_key key
)
1711 struct bfd_session bs
;
1715 return hash_release(bfd_key_hash
, &bs
);
1718 /* Iteration functions. */
1719 void bfd_id_iterate(hash_iter_func hif
, void *arg
)
1721 hash_iterate(bfd_id_hash
, hif
, arg
);
1724 void bfd_key_iterate(hash_iter_func hif
, void *arg
)
1726 hash_iterate(bfd_key_hash
, hif
, arg
);
1732 * Inserts session into hash and returns `true` on success, otherwise
1735 bool bfd_id_insert(struct bfd_session
*bs
)
1737 return (hash_get(bfd_id_hash
, bs
, hash_alloc_intern
) == bs
);
1740 bool bfd_key_insert(struct bfd_session
*bs
)
1742 return (hash_get(bfd_key_hash
, bs
, hash_alloc_intern
) == bs
);
1745 void bfd_initialize(void)
1747 bfd_id_hash
= hash_create(bfd_id_hash_do
, bfd_id_hash_cmp
,
1748 "BFD session discriminator hash");
1749 bfd_key_hash
= hash_create(bfd_key_hash_do
, bfd_key_hash_cmp
,
1750 "BFD session hash");
1751 TAILQ_INIT(&bplist
);
1754 static void _bfd_free(struct hash_bucket
*hb
,
1755 void *arg
__attribute__((__unused__
)))
1757 struct bfd_session
*bs
= hb
->data
;
1759 bfd_session_free(bs
);
1762 void bfd_shutdown(void)
1764 struct bfd_profile
*bp
;
1767 * Close and free all BFD sessions.
1769 * _bfd_free() will call bfd_session_free() which will take care
1770 * of removing the session from all hashes, so we just run an
1771 * assert() here to make sure it really happened.
1773 bfd_id_iterate(_bfd_free
, NULL
);
1774 assert(bfd_key_hash
->count
== 0);
1776 /* Now free the hashes themselves. */
1777 hash_free(bfd_id_hash
);
1778 hash_free(bfd_key_hash
);
1780 /* Free all profile allocations. */
1781 while ((bp
= TAILQ_FIRST(&bplist
)) != NULL
)
1782 bfd_profile_free(bp
);
1785 struct bfd_session_iterator
{
1788 const struct bfd_session
*bsi_bs
;
1791 static int _bfd_session_next(struct hash_bucket
*hb
, void *arg
)
1793 struct bfd_session_iterator
*bsi
= arg
;
1794 struct bfd_session
*bs
= hb
->data
;
1796 /* Previous entry signaled stop. */
1797 if (bsi
->bsi_stop
== 1) {
1798 /* Match the single/multi hop sessions. */
1799 if (bs
->key
.mhop
!= bsi
->bsi_mhop
)
1800 return HASHWALK_CONTINUE
;
1803 return HASHWALK_ABORT
;
1806 /* We found the current item, stop in the next one. */
1807 if (bsi
->bsi_bs
== hb
->data
) {
1809 /* Set entry to NULL to signal end of list. */
1811 } else if (bsi
->bsi_bs
== NULL
&& bsi
->bsi_mhop
== bs
->key
.mhop
) {
1812 /* We want the first list item. */
1814 bsi
->bsi_bs
= hb
->data
;
1815 return HASHWALK_ABORT
;
1818 return HASHWALK_CONTINUE
;
1822 * bfd_session_next: uses the current session to find the next.
1824 * `bs` might point to NULL to get the first item of the data structure.
1826 const struct bfd_session
*bfd_session_next(const struct bfd_session
*bs
,
1829 struct bfd_session_iterator bsi
;
1833 bsi
.bsi_mhop
= mhop
;
1834 hash_walk(bfd_key_hash
, _bfd_session_next
, &bsi
);
1835 if (bsi
.bsi_stop
== 0)
1841 static void _bfd_session_remove_manual(struct hash_bucket
*hb
,
1842 void *arg
__attribute__((__unused__
)))
1844 struct bfd_session
*bs
= hb
->data
;
1846 /* Delete only manually configured sessions. */
1847 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
) == 0)
1851 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
);
1853 /* Don't delete sessions still in use. */
1854 if (bs
->refcount
!= 0)
1857 bfd_session_free(bs
);
1861 * bfd_sessions_remove_manual: remove all manually configured sessions.
1863 * NOTE: this function doesn't remove automatically created sessions.
1865 void bfd_sessions_remove_manual(void)
1867 hash_iterate(bfd_key_hash
, _bfd_session_remove_manual
, NULL
);
1870 void bfd_profiles_remove(void)
1872 struct bfd_profile
*bp
;
1874 while ((bp
= TAILQ_FIRST(&bplist
)) != NULL
)
1875 bfd_profile_free(bp
);
1879 * Profile related hash functions.
1881 static void _bfd_profile_update(struct hash_bucket
*hb
, void *arg
)
1883 struct bfd_profile
*bp
= arg
;
1884 struct bfd_session
*bs
= hb
->data
;
1886 /* This session is not using the profile. */
1887 if (bs
->profile_name
== NULL
|| strcmp(bs
->profile_name
, bp
->name
) != 0)
1890 bfd_profile_apply(bp
->name
, bs
);
1893 void bfd_profile_update(struct bfd_profile
*bp
)
1895 hash_iterate(bfd_key_hash
, _bfd_profile_update
, bp
);
1898 static void _bfd_profile_detach(struct hash_bucket
*hb
, void *arg
)
1900 struct bfd_profile
*bp
= arg
;
1901 struct bfd_session
*bs
= hb
->data
;
1903 /* This session is not using the profile. */
1904 if (bs
->profile_name
== NULL
|| strcmp(bs
->profile_name
, bp
->name
) != 0)
1907 bfd_profile_remove(bs
);
1910 static void bfd_profile_detach(struct bfd_profile
*bp
)
1912 hash_iterate(bfd_key_hash
, _bfd_profile_detach
, bp
);
1916 * VRF related functions.
1918 static int bfd_vrf_new(struct vrf
*vrf
)
1920 if (bglobal
.debug_zebra
)
1921 zlog_debug("VRF Created: %s(%u)", vrf
->name
, vrf
->vrf_id
);
1926 static int bfd_vrf_delete(struct vrf
*vrf
)
1928 if (bglobal
.debug_zebra
)
1929 zlog_debug("VRF Deletion: %s(%u)", vrf
->name
, vrf
->vrf_id
);
1934 static int bfd_vrf_enable(struct vrf
*vrf
)
1936 struct bfd_vrf_global
*bvrf
;
1938 /* a different name */
1940 bvrf
= XCALLOC(MTYPE_BFDD_VRF
, sizeof(struct bfd_vrf_global
));
1942 vrf
->info
= (void *)bvrf
;
1944 /* Disable sockets if using data plane. */
1945 if (bglobal
.bg_use_dplane
) {
1948 bvrf
->bg_shop6
= -1;
1949 bvrf
->bg_mhop6
= -1;
1951 bvrf
->bg_echov6
= -1;
1956 if (bglobal
.debug_zebra
)
1957 zlog_debug("VRF enable add %s id %u", vrf
->name
, vrf
->vrf_id
);
1960 bvrf
->bg_shop
= bp_udp_shop(vrf
);
1962 bvrf
->bg_mhop
= bp_udp_mhop(vrf
);
1963 if (!bvrf
->bg_shop6
)
1964 bvrf
->bg_shop6
= bp_udp6_shop(vrf
);
1965 if (!bvrf
->bg_mhop6
)
1966 bvrf
->bg_mhop6
= bp_udp6_mhop(vrf
);
1968 bvrf
->bg_echo
= bp_echo_socket(vrf
);
1969 if (!bvrf
->bg_echov6
)
1970 bvrf
->bg_echov6
= bp_echov6_socket(vrf
);
1972 if (!bvrf
->bg_ev
[0] && bvrf
->bg_shop
!= -1)
1973 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_shop
,
1975 if (!bvrf
->bg_ev
[1] && bvrf
->bg_mhop
!= -1)
1976 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_mhop
,
1978 if (!bvrf
->bg_ev
[2] && bvrf
->bg_shop6
!= -1)
1979 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_shop6
,
1981 if (!bvrf
->bg_ev
[3] && bvrf
->bg_mhop6
!= -1)
1982 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_mhop6
,
1984 if (!bvrf
->bg_ev
[4] && bvrf
->bg_echo
!= -1)
1985 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_echo
,
1987 if (!bvrf
->bg_ev
[5] && bvrf
->bg_echov6
!= -1)
1988 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_echov6
,
1991 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
1992 bfdd_zclient_register(vrf
->vrf_id
);
1993 bfdd_sessions_enable_vrf(vrf
);
1998 static int bfd_vrf_disable(struct vrf
*vrf
)
2000 struct bfd_vrf_global
*bvrf
;
2006 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
2007 bfdd_sessions_disable_vrf(vrf
);
2008 bfdd_zclient_unregister(vrf
->vrf_id
);
2011 if (bglobal
.debug_zebra
)
2012 zlog_debug("VRF disable %s id %d", vrf
->name
, vrf
->vrf_id
);
2014 /* Disable read/write poll triggering. */
2015 THREAD_OFF(bvrf
->bg_ev
[0]);
2016 THREAD_OFF(bvrf
->bg_ev
[1]);
2017 THREAD_OFF(bvrf
->bg_ev
[2]);
2018 THREAD_OFF(bvrf
->bg_ev
[3]);
2019 THREAD_OFF(bvrf
->bg_ev
[4]);
2020 THREAD_OFF(bvrf
->bg_ev
[5]);
2022 /* Close all descriptors. */
2023 socket_close(&bvrf
->bg_echo
);
2024 socket_close(&bvrf
->bg_shop
);
2025 socket_close(&bvrf
->bg_mhop
);
2026 if (bvrf
->bg_shop6
!= -1)
2027 socket_close(&bvrf
->bg_shop6
);
2028 if (bvrf
->bg_mhop6
!= -1)
2029 socket_close(&bvrf
->bg_mhop6
);
2030 socket_close(&bvrf
->bg_echo
);
2031 if (bvrf
->bg_echov6
!= -1)
2032 socket_close(&bvrf
->bg_echov6
);
2035 XFREE(MTYPE_BFDD_VRF
, bvrf
);
2041 void bfd_vrf_init(void)
2043 vrf_init(bfd_vrf_new
, bfd_vrf_enable
, bfd_vrf_disable
, bfd_vrf_delete
);
2046 void bfd_vrf_terminate(void)
2051 struct bfd_vrf_global
*bfd_vrf_look_by_session(struct bfd_session
*bfd
)
2055 if (!vrf_is_backend_netns()) {
2056 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
2058 return (struct bfd_vrf_global
*)vrf
->info
;
2065 return bfd
->vrf
->info
;
2068 unsigned long bfd_get_session_count(void)
2070 return bfd_key_hash
->count
;
2073 void bfd_rtt_init(struct bfd_session
*bfd
)
2077 /* initialize RTT */
2080 for (i
= 0; i
< BFD_RTT_SAMPLE
; i
++)