1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*********************************************************************
3 * Copyright 2013 Cumulus Networks, LLC. All rights reserved.
4 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
6 * bfd.c: implements the BFD protocol.
10 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
11 * Kanna Rajagopal [kanna@cumulusnetworks.com]
12 * Radhika Mahankali [Radhika@cumulusnetworks.com]
17 #include "lib/jhash.h"
18 #include "lib/network.h"
22 DEFINE_MTYPE_STATIC(BFDD
, BFDD_CONFIG
, "long-lived configuration memory");
23 DEFINE_MTYPE_STATIC(BFDD
, BFDD_PROFILE
, "long-lived profile memory");
24 DEFINE_MTYPE_STATIC(BFDD
, BFDD_SESSION_OBSERVER
, "Session observer");
25 DEFINE_MTYPE_STATIC(BFDD
, BFDD_VRF
, "BFD VRF");
30 static uint32_t ptm_bfd_gen_ID(void);
31 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
);
32 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
34 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
35 static const char *get_diag_str(int diag
);
37 static void bs_admin_down_handler(struct bfd_session
*bs
, int nstate
);
38 static void bs_down_handler(struct bfd_session
*bs
, int nstate
);
39 static void bs_init_handler(struct bfd_session
*bs
, int nstate
);
40 static void bs_up_handler(struct bfd_session
*bs
, int nstate
);
43 * Remove BFD profile from all BFD sessions so we don't leave dangling
46 static void bfd_profile_detach(struct bfd_profile
*bp
);
48 /* Zeroed array with the size of an IPv6 address. */
49 struct in6_addr zero_addr
;
51 /** BFD profiles list. */
52 struct bfdproflist bplist
;
57 struct bfd_profile
*bfd_profile_lookup(const char *name
)
59 struct bfd_profile
*bp
;
61 TAILQ_FOREACH (bp
, &bplist
, entry
) {
62 if (strcmp(name
, bp
->name
))
71 static void bfd_profile_set_default(struct bfd_profile
*bp
)
73 bp
->admin_shutdown
= false;
74 bp
->detection_multiplier
= BFD_DEFDETECTMULT
;
75 bp
->echo_mode
= false;
77 bp
->minimum_ttl
= BFD_DEF_MHOP_TTL
;
78 bp
->min_echo_rx
= BFD_DEF_REQ_MIN_ECHO_RX
;
79 bp
->min_echo_tx
= BFD_DEF_DES_MIN_ECHO_TX
;
80 bp
->min_rx
= BFD_DEFREQUIREDMINRX
;
81 bp
->min_tx
= BFD_DEFDESIREDMINTX
;
84 struct bfd_profile
*bfd_profile_new(const char *name
)
86 struct bfd_profile
*bp
;
88 /* Search for duplicates. */
89 if (bfd_profile_lookup(name
) != NULL
)
92 /* Allocate, name it and put into list. */
93 bp
= XCALLOC(MTYPE_BFDD_PROFILE
, sizeof(*bp
));
94 strlcpy(bp
->name
, name
, sizeof(bp
->name
));
95 TAILQ_INSERT_TAIL(&bplist
, bp
, entry
);
97 /* Set default values. */
98 bfd_profile_set_default(bp
);
103 void bfd_profile_free(struct bfd_profile
*bp
)
105 /* Detach from any session. */
106 if (bglobal
.bg_shutdown
== false)
107 bfd_profile_detach(bp
);
109 /* Remove from global list. */
110 TAILQ_REMOVE(&bplist
, bp
, entry
);
112 XFREE(MTYPE_BFDD_PROFILE
, bp
);
115 void bfd_profile_apply(const char *profname
, struct bfd_session
*bs
)
117 struct bfd_profile
*bp
;
119 /* Remove previous profile if any. */
120 if (bs
->profile_name
) {
121 /* We are changing profiles. */
122 if (strcmp(bs
->profile_name
, profname
)) {
123 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
125 XSTRDUP(MTYPE_BFDD_PROFILE
, profname
);
127 } else /* Save the current profile name (in case it doesn't exist). */
128 bs
->profile_name
= XSTRDUP(MTYPE_BFDD_PROFILE
, profname
);
130 /* Look up new profile to apply. */
131 bp
= bfd_profile_lookup(profname
);
133 /* Point to profile if it exists. */
136 /* Apply configuration. */
137 bfd_session_apply(bs
);
140 void bfd_session_apply(struct bfd_session
*bs
)
142 struct bfd_profile
*bp
;
143 uint32_t min_tx
= bs
->timers
.desired_min_tx
;
144 uint32_t min_rx
= bs
->timers
.required_min_rx
;
146 /* Pick the source of configuration. */
147 bp
= bs
->profile
? bs
->profile
: &bs
->peer_profile
;
149 /* Set multiplier if not the default. */
150 if (bs
->peer_profile
.detection_multiplier
== BFD_DEFDETECTMULT
)
151 bs
->detect_mult
= bp
->detection_multiplier
;
153 bs
->detect_mult
= bs
->peer_profile
.detection_multiplier
;
155 /* Set timers if not the default. */
156 if (bs
->peer_profile
.min_tx
== BFD_DEFDESIREDMINTX
)
157 bs
->timers
.desired_min_tx
= bp
->min_tx
;
159 bs
->timers
.desired_min_tx
= bs
->peer_profile
.min_tx
;
161 if (bs
->peer_profile
.min_rx
== BFD_DEFREQUIREDMINRX
)
162 bs
->timers
.required_min_rx
= bp
->min_rx
;
164 bs
->timers
.required_min_rx
= bs
->peer_profile
.min_rx
;
166 /* We can only apply echo options on single hop sessions. */
167 if (!CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
168 /* Configure echo timers if they were default. */
169 if (bs
->peer_profile
.min_echo_rx
== BFD_DEF_REQ_MIN_ECHO_RX
)
170 bs
->timers
.required_min_echo_rx
= bp
->min_echo_rx
;
172 bs
->timers
.required_min_echo_rx
=
173 bs
->peer_profile
.min_echo_rx
;
175 if (bs
->peer_profile
.min_echo_tx
== BFD_DEF_DES_MIN_ECHO_TX
)
176 bs
->timers
.desired_min_echo_tx
= bp
->min_echo_tx
;
178 bs
->timers
.desired_min_echo_tx
=
179 bs
->peer_profile
.min_echo_tx
;
181 /* Toggle echo if default value. */
182 if (bs
->peer_profile
.echo_mode
== false)
183 bfd_set_echo(bs
, bp
->echo_mode
);
185 bfd_set_echo(bs
, bs
->peer_profile
.echo_mode
);
187 /* Configure the TTL packet filter. */
188 if (bs
->peer_profile
.minimum_ttl
== BFD_DEF_MHOP_TTL
)
189 bs
->mh_ttl
= bp
->minimum_ttl
;
191 bs
->mh_ttl
= bs
->peer_profile
.minimum_ttl
;
194 /* Toggle 'passive-mode' if default value. */
195 if (bs
->peer_profile
.passive
== false)
196 bfd_set_passive_mode(bs
, bp
->passive
);
198 bfd_set_passive_mode(bs
, bs
->peer_profile
.passive
);
200 /* Toggle 'no shutdown' if default value. */
201 if (bs
->peer_profile
.admin_shutdown
== false)
202 bfd_set_shutdown(bs
, bp
->admin_shutdown
);
204 bfd_set_shutdown(bs
, bs
->peer_profile
.admin_shutdown
);
206 /* If session interval changed negotiate new timers. */
207 if (bs
->ses_state
== PTM_BFD_UP
208 && (bs
->timers
.desired_min_tx
!= min_tx
209 || bs
->timers
.required_min_rx
!= min_rx
))
212 /* Send updated information to data plane. */
213 bfd_dplane_update_session(bs
);
216 void bfd_profile_remove(struct bfd_session
*bs
)
218 /* Remove any previous set profile name. */
219 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
222 bfd_session_apply(bs
);
225 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
226 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
229 memset(key
, 0, sizeof(*key
));
231 switch (peer
->sa_sin
.sin_family
) {
233 key
->family
= AF_INET
;
234 memcpy(&key
->peer
, &peer
->sa_sin
.sin_addr
,
235 sizeof(peer
->sa_sin
.sin_addr
));
236 memcpy(&key
->local
, &local
->sa_sin
.sin_addr
,
237 sizeof(local
->sa_sin
.sin_addr
));
240 key
->family
= AF_INET6
;
241 memcpy(&key
->peer
, &peer
->sa_sin6
.sin6_addr
,
242 sizeof(peer
->sa_sin6
.sin6_addr
));
243 memcpy(&key
->local
, &local
->sa_sin6
.sin6_addr
,
244 sizeof(local
->sa_sin6
.sin6_addr
));
249 if (ifname
&& ifname
[0])
250 strlcpy(key
->ifname
, ifname
, sizeof(key
->ifname
));
251 if (vrfname
&& vrfname
[0])
252 strlcpy(key
->vrfname
, vrfname
, sizeof(key
->vrfname
));
254 strlcpy(key
->vrfname
, VRF_DEFAULT_NAME
, sizeof(key
->vrfname
));
257 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
)
259 struct bfd_session
*bs
;
260 struct peer_label
*pl
;
263 /* Try to find label first. */
264 if (bpc
->bpc_has_label
) {
265 pl
= pl_find(bpc
->bpc_label
);
272 /* Otherwise fallback to peer/local hash lookup. */
273 gen_bfd_key(&key
, &bpc
->bpc_peer
, &bpc
->bpc_local
, bpc
->bpc_mhop
,
274 bpc
->bpc_localif
, bpc
->bpc_vrfname
);
276 return bfd_key_lookup(key
);
280 * Starts a disabled BFD session.
282 * A session is disabled when the specified interface/VRF doesn't exist
283 * yet. It might happen on FRR boot or with virtual interfaces.
285 int bfd_session_enable(struct bfd_session
*bs
)
287 struct interface
*ifp
= NULL
;
288 struct vrf
*vrf
= NULL
;
291 /* We are using data plane, we don't need software. */
296 * If the interface or VRF doesn't exist, then we must register
297 * the session but delay its start.
299 if (bs
->key
.vrfname
[0]) {
300 vrf
= vrf_lookup_by_name(bs
->key
.vrfname
);
303 "session-enable: specified VRF %s doesn't exists.",
308 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
313 if (bs
->key
.ifname
[0]) {
314 ifp
= if_lookup_by_name(bs
->key
.ifname
, vrf
->vrf_id
);
317 "session-enable: specified interface %s (VRF %s) doesn't exist.",
318 bs
->key
.ifname
, vrf
->name
);
323 /* Assign interface/VRF pointers. */
326 /* Assign interface pointer (if any). */
329 /* Attempt to use data plane. */
330 if (bglobal
.bg_use_dplane
&& bfd_dplane_add_session(bs
) == 0) {
331 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bs
);
335 /* Sanity check: don't leak open sockets. */
336 if (bs
->sock
!= -1) {
337 if (bglobal
.debug_peer_event
)
338 zlog_debug("%s: previous socket open", __func__
);
345 * Get socket for transmitting control packets. Note that if we
346 * could use the destination port (3784) for the source
347 * port we wouldn't need a socket per session.
349 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_IPV6
) == 0) {
350 psock
= bp_peer_socket(bs
);
354 psock
= bp_peer_socketv6(bs
);
360 * We've got a valid socket, lets start the timers and the
365 /* Only start timers if we are using active mode. */
366 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
) == 0) {
367 bfd_recvtimer_update(bs
);
368 ptm_bfd_start_xmt_timer(bs
, false);
378 * Disabled a running BFD session.
380 * A session is disabled when the specified interface/VRF gets removed
381 * (e.g. virtual interfaces).
383 void bfd_session_disable(struct bfd_session
*bs
)
385 /* We are using data plane, we don't need software. */
389 /* Free up socket resources. */
390 if (bs
->sock
!= -1) {
395 /* Disable all timers. */
396 bfd_recvtimer_delete(bs
);
397 bfd_xmttimer_delete(bs
);
398 ptm_bfd_echo_stop(bs
);
400 /* Set session down so it doesn't report UP and disabled. */
401 ptm_bfd_sess_dn(bs
, BD_PATH_DOWN
);
404 static uint32_t ptm_bfd_gen_ID(void)
409 * RFC 5880, Section 6.8.1. recommends that we should generate
410 * random session identification numbers.
413 session_id
= ((frr_weak_random() << 16) & 0xFFFF0000)
414 | (frr_weak_random() & 0x0000FFFF);
415 } while (session_id
== 0 || bfd_id_lookup(session_id
) != NULL
);
420 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
)
422 uint64_t jitter
, xmt_TO
;
425 xmt_TO
= is_echo
? bfd
->echo_xmt_TO
: bfd
->xmt_TO
;
428 * From section 6.5.2: trasmit interval should be randomly jittered
430 * 75% and 100% of nominal value, unless detect_mult is 1, then should
432 * between 75% and 90%.
434 maxpercent
= (bfd
->detect_mult
== 1) ? 16 : 26;
435 jitter
= (xmt_TO
* (75 + (frr_weak_random() % maxpercent
))) / 100;
436 /* XXX remove that division above */
439 bfd_echo_xmttimer_update(bfd
, jitter
);
441 bfd_xmttimer_update(bfd
, jitter
);
444 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
)
446 /* Send the scheduled echo packet */
447 /* if ipv4 use the new echo implementation that causes
448 * the packet to be looped in forwarding plane of peer
450 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
) == 0)
452 ptm_bfd_echo_fp_snd(bfd
);
454 ptm_bfd_echo_snd(bfd
);
457 ptm_bfd_echo_snd(bfd
);
459 /* Restart the timer for next time */
460 ptm_bfd_start_xmt_timer(bfd
, true);
463 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
)
465 /* Send the scheduled control packet */
466 ptm_bfd_snd(bfd
, fbit
);
468 /* Restart the timer for next time */
469 ptm_bfd_start_xmt_timer(bfd
, false);
472 void ptm_bfd_echo_stop(struct bfd_session
*bfd
)
474 bfd
->echo_xmt_TO
= 0;
475 bfd
->echo_detect_TO
= 0;
476 UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
);
478 bfd_echo_xmttimer_delete(bfd
);
479 bfd_echo_recvtimer_delete(bfd
);
482 void ptm_bfd_echo_start(struct bfd_session
*bfd
)
484 bfd
->echo_detect_TO
= (bfd
->remote_detect_mult
* bfd
->echo_xmt_TO
);
485 if (bfd
->echo_detect_TO
> 0) {
486 bfd_echo_recvtimer_update(bfd
);
487 ptm_bfd_echo_xmt_TO(bfd
);
491 void ptm_bfd_sess_up(struct bfd_session
*bfd
)
493 int old_state
= bfd
->ses_state
;
496 bfd
->ses_state
= PTM_BFD_UP
;
497 monotime(&bfd
->uptime
);
499 /* Connection is up, lets negotiate timers. */
500 bfd_set_polling(bfd
);
502 /* Start sending control packets with poll bit immediately. */
505 control_notify(bfd
, bfd
->ses_state
);
507 if (old_state
!= bfd
->ses_state
) {
508 bfd
->stats
.session_up
++;
509 if (bglobal
.debug_peer_event
)
510 zlog_debug("state-change: [%s] %s -> %s",
511 bs_to_string(bfd
), state_list
[old_state
].str
,
512 state_list
[bfd
->ses_state
].str
);
516 void ptm_bfd_sess_dn(struct bfd_session
*bfd
, uint8_t diag
)
518 int old_state
= bfd
->ses_state
;
520 bfd
->local_diag
= diag
;
521 bfd
->discrs
.remote_discr
= 0;
522 bfd
->ses_state
= PTM_BFD_DOWN
;
524 bfd
->demand_mode
= 0;
525 monotime(&bfd
->downtime
);
528 * Only attempt to send if we have a valid socket:
529 * this function might be called by session disablers and in
530 * this case we won't have a valid socket (i.e. interface was
531 * removed or VRF doesn't exist anymore).
536 /* Slow down the control packets, the connection is down. */
537 bs_set_slow_timers(bfd
);
539 /* only signal clients when going from up->down state */
540 if (old_state
== PTM_BFD_UP
)
541 control_notify(bfd
, PTM_BFD_DOWN
);
543 /* Stop echo packet transmission if they are active */
544 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
545 ptm_bfd_echo_stop(bfd
);
547 /* Stop attempting to transmit or expect control packets if passive. */
548 if (CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_PASSIVE
)) {
549 bfd_recvtimer_delete(bfd
);
550 bfd_xmttimer_delete(bfd
);
553 if (old_state
!= bfd
->ses_state
) {
554 bfd
->stats
.session_down
++;
555 if (bglobal
.debug_peer_event
)
556 zlog_debug("state-change: [%s] %s -> %s reason:%s",
557 bs_to_string(bfd
), state_list
[old_state
].str
,
558 state_list
[bfd
->ses_state
].str
,
559 get_diag_str(bfd
->local_diag
));
562 /* clear peer's mac address */
563 UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MAC_SET
);
564 memset(bfd
->peer_hw_addr
, 0, sizeof(bfd
->peer_hw_addr
));
565 /* reset local address ,it might has been be changed after bfd is up*/
566 memset(&bfd
->local_address
, 0, sizeof(bfd
->local_address
));
572 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
575 struct bfd_session
*bs
;
577 bs
= bfd_id_lookup(ldisc
);
581 switch (bs
->key
.family
) {
583 if (memcmp(&sa
->sa_sin
.sin_addr
, &bs
->key
.peer
,
584 sizeof(sa
->sa_sin
.sin_addr
)))
588 if (memcmp(&sa
->sa_sin6
.sin6_addr
, &bs
->key
.peer
,
589 sizeof(sa
->sa_sin6
.sin6_addr
)))
597 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
598 struct sockaddr_any
*peer
,
599 struct sockaddr_any
*local
,
600 struct interface
*ifp
,
607 /* Find our session using the ID signaled by the remote end. */
608 if (cp
->discrs
.remote_discr
)
609 return bfd_find_disc(peer
, ntohl(cp
->discrs
.remote_discr
));
611 /* Search for session without using discriminator. */
612 vrf
= vrf_lookup_by_id(vrfid
);
614 gen_bfd_key(&key
, peer
, local
, is_mhop
, ifp
? ifp
->name
: NULL
,
615 vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
617 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
618 return bfd_key_lookup(key
);
621 void bfd_xmt_cb(struct thread
*t
)
623 struct bfd_session
*bs
= THREAD_ARG(t
);
625 ptm_bfd_xmt_TO(bs
, 0);
628 void bfd_echo_xmt_cb(struct thread
*t
)
630 struct bfd_session
*bs
= THREAD_ARG(t
);
632 if (bs
->echo_xmt_TO
> 0)
633 ptm_bfd_echo_xmt_TO(bs
);
636 /* Was ptm_bfd_detect_TO() */
637 void bfd_recvtimer_cb(struct thread
*t
)
639 struct bfd_session
*bs
= THREAD_ARG(t
);
641 switch (bs
->ses_state
) {
644 ptm_bfd_sess_dn(bs
, BD_CONTROL_EXPIRED
);
649 /* Was ptm_bfd_echo_detect_TO() */
650 void bfd_echo_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_ECHO_FAILED
);
662 struct bfd_session
*bfd_session_new(void)
664 struct bfd_session
*bs
;
666 bs
= XCALLOC(MTYPE_BFDD_CONFIG
, sizeof(*bs
));
668 /* Set peer session defaults. */
669 bfd_profile_set_default(&bs
->peer_profile
);
671 bs
->timers
.desired_min_tx
= BFD_DEFDESIREDMINTX
;
672 bs
->timers
.required_min_rx
= BFD_DEFREQUIREDMINRX
;
673 bs
->timers
.required_min_echo_rx
= BFD_DEF_REQ_MIN_ECHO_RX
;
674 bs
->timers
.desired_min_echo_tx
= BFD_DEF_DES_MIN_ECHO_TX
;
675 bs
->detect_mult
= BFD_DEFDETECTMULT
;
676 bs
->mh_ttl
= BFD_DEF_MHOP_TTL
;
677 bs
->ses_state
= PTM_BFD_DOWN
;
679 /* Initiate connection with slow timers. */
680 bs_set_slow_timers(bs
);
682 /* Initiate remote settings as well. */
683 bs
->remote_timers
= bs
->cur_timers
;
684 bs
->remote_detect_mult
= BFD_DEFDETECTMULT
;
687 monotime(&bs
->uptime
);
688 bs
->downtime
= bs
->uptime
;
693 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
)
695 /* New label treatment:
696 * - Check if the label is taken;
697 * - Try to allocate the memory for it and register;
699 if (bs
->pl
== NULL
) {
700 if (pl_find(nlabel
) != NULL
) {
701 /* Someone is already using it. */
711 * Test label change consistency:
712 * - Do nothing if it's the same label;
713 * - Check if the future label is already taken;
716 if (strcmp(nlabel
, bs
->pl
->pl_label
) == 0)
718 if (pl_find(nlabel
) != NULL
)
721 strlcpy(bs
->pl
->pl_label
, nlabel
, sizeof(bs
->pl
->pl_label
));
725 static void _bfd_session_update(struct bfd_session
*bs
,
726 struct bfd_peer_cfg
*bpc
)
728 if (bpc
->bpc_has_txinterval
) {
729 bs
->timers
.desired_min_tx
= bpc
->bpc_txinterval
* 1000;
730 bs
->peer_profile
.min_tx
= bs
->timers
.desired_min_tx
;
733 if (bpc
->bpc_has_recvinterval
) {
734 bs
->timers
.required_min_rx
= bpc
->bpc_recvinterval
* 1000;
735 bs
->peer_profile
.min_rx
= bs
->timers
.required_min_rx
;
738 if (bpc
->bpc_has_detectmultiplier
) {
739 bs
->detect_mult
= bpc
->bpc_detectmultiplier
;
740 bs
->peer_profile
.detection_multiplier
= bs
->detect_mult
;
743 if (bpc
->bpc_has_echorecvinterval
) {
744 bs
->timers
.required_min_echo_rx
= bpc
->bpc_echorecvinterval
* 1000;
745 bs
->peer_profile
.min_echo_rx
= bs
->timers
.required_min_echo_rx
;
748 if (bpc
->bpc_has_echotxinterval
) {
749 bs
->timers
.desired_min_echo_tx
= bpc
->bpc_echotxinterval
* 1000;
750 bs
->peer_profile
.min_echo_tx
= bs
->timers
.desired_min_echo_tx
;
753 if (bpc
->bpc_has_label
)
754 bfd_session_update_label(bs
, bpc
->bpc_label
);
757 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
759 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
761 if (bpc
->bpc_has_minimum_ttl
) {
762 bs
->mh_ttl
= bpc
->bpc_minimum_ttl
;
763 bs
->peer_profile
.minimum_ttl
= bpc
->bpc_minimum_ttl
;
766 bs
->peer_profile
.echo_mode
= bpc
->bpc_echo
;
767 bfd_set_echo(bs
, bpc
->bpc_echo
);
770 * Shutdown needs to be the last in order to avoid timers enable when
771 * the session is disabled.
773 bs
->peer_profile
.admin_shutdown
= bpc
->bpc_shutdown
;
774 bfd_set_passive_mode(bs
, bpc
->bpc_passive
);
775 bfd_set_shutdown(bs
, bpc
->bpc_shutdown
);
778 * Apply profile last: it also calls `bfd_set_shutdown`.
780 * There is no problem calling `shutdown` twice if the value doesn't
781 * change or if it is overridden by peer specific configuration.
783 if (bpc
->bpc_has_profile
)
784 bfd_profile_apply(bpc
->bpc_profile
, bs
);
787 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
789 /* User didn't want to update, return failure. */
790 if (bpc
->bpc_createonly
)
793 _bfd_session_update(bs
, bpc
);
795 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
800 void bfd_session_free(struct bfd_session
*bs
)
802 struct bfd_session_observer
*bso
;
804 bfd_session_disable(bs
);
806 /* Remove session from data plane if any. */
807 bfd_dplane_delete_session(bs
);
809 bfd_key_delete(bs
->key
);
810 bfd_id_delete(bs
->discrs
.my_discr
);
812 /* Remove observer if any. */
813 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
814 if (bso
->bso_bs
!= bs
)
820 bs_observer_del(bso
);
824 XFREE(MTYPE_BFDD_PROFILE
, bs
->profile_name
);
825 XFREE(MTYPE_BFDD_CONFIG
, bs
);
828 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
)
830 struct bfd_session
*bfd
, *l_bfd
;
832 /* check to see if this needs a new session */
833 l_bfd
= bs_peer_find(bpc
);
835 /* Requesting a duplicated peer means update configuration. */
836 if (bfd_session_update(l_bfd
, bpc
) == 0)
842 /* Get BFD session storage with its defaults. */
843 bfd
= bfd_session_new();
846 * Store interface/VRF name in case we need to delay session
847 * start. See `bfd_session_enable` for more information.
849 if (bpc
->bpc_has_localif
)
850 strlcpy(bfd
->key
.ifname
, bpc
->bpc_localif
,
851 sizeof(bfd
->key
.ifname
));
853 if (bpc
->bpc_has_vrfname
)
854 strlcpy(bfd
->key
.vrfname
, bpc
->bpc_vrfname
,
855 sizeof(bfd
->key
.vrfname
));
857 strlcpy(bfd
->key
.vrfname
, VRF_DEFAULT_NAME
,
858 sizeof(bfd
->key
.vrfname
));
860 /* Copy remaining data. */
861 if (bpc
->bpc_ipv4
== false)
862 SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
);
864 bfd
->key
.family
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
865 switch (bfd
->key
.family
) {
867 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin
.sin_addr
,
868 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
869 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin
.sin_addr
,
870 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
874 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin6
.sin6_addr
,
875 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
876 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin6
.sin6_addr
,
877 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
886 SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MH
);
888 bfd
->key
.mhop
= bpc
->bpc_mhop
;
890 if (bs_registrate(bfd
) == NULL
)
893 /* Apply other configurations. */
894 _bfd_session_update(bfd
, bpc
);
899 struct bfd_session
*bs_registrate(struct bfd_session
*bfd
)
901 /* Registrate session into data structures. */
903 bfd
->discrs
.my_discr
= ptm_bfd_gen_ID();
906 /* Try to enable session and schedule for packet receive/send. */
907 if (bfd_session_enable(bfd
) == -1) {
908 /* Unrecoverable failure, remove the session/peer. */
909 bfd_session_free(bfd
);
913 /* Add observer if we have moving parts. */
914 if (bfd
->key
.ifname
[0] || bfd
->key
.vrfname
[0] || bfd
->sock
== -1)
915 bs_observer_add(bfd
);
917 if (bglobal
.debug_peer_event
)
918 zlog_debug("session-new: %s", bs_to_string(bfd
));
920 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bfd
);
925 int ptm_bfd_sess_del(struct bfd_peer_cfg
*bpc
)
927 struct bfd_session
*bs
;
929 /* Find session and call free(). */
930 bs
= bs_peer_find(bpc
);
934 /* This pointer is being referenced, don't let it be deleted. */
935 if (bs
->refcount
> 0) {
936 zlog_err("session-delete: refcount failure: %" PRIu64
" references",
941 if (bglobal
.debug_peer_event
)
942 zlog_debug("%s: %s", __func__
, bs_to_string(bs
));
944 control_notify_config(BCM_NOTIFY_CONFIG_DELETE
, bs
);
946 bfd_session_free(bs
);
951 void bfd_set_polling(struct bfd_session
*bs
)
954 * Start polling procedure: the only timers that require polling
955 * to change value without losing connection are:
957 * - Desired minimum transmission interval;
958 * - Required minimum receive interval;
960 * RFC 5880, Section 6.8.3.
966 * bs_<state>_handler() functions implement the BFD state machine
967 * transition mechanism. `<state>` is the current session state and
968 * the parameter `nstate` is the peer new state.
970 static void bs_admin_down_handler(struct bfd_session
*bs
971 __attribute__((__unused__
)),
972 int nstate
__attribute__((__unused__
)))
975 * We are administratively down, there is no state machine
980 static void bs_down_handler(struct bfd_session
*bs
, int nstate
)
983 case PTM_BFD_ADM_DOWN
:
985 * Remote peer doesn't want to talk, so lets keep the
989 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
994 * Remote peer agreed that the path is down, lets try to
997 bs
->ses_state
= PTM_BFD_INIT
;
1000 * RFC 5880, Section 6.1.
1001 * A system taking the Passive role MUST NOT begin
1002 * sending BFD packets for a particular session until
1003 * it has received a BFD packet for that session, and thus
1004 * has learned the remote system's discriminator value.
1006 * Now we can start transmission timer in passive mode.
1008 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
))
1009 ptm_bfd_xmt_TO(bs
, 0);
1015 * Remote peer told us his path is up, lets turn
1016 * activate the session.
1018 ptm_bfd_sess_up(bs
);
1022 if (bglobal
.debug_peer_event
)
1023 zlog_debug("state-change: unhandled neighbor state: %d",
1029 static void bs_init_handler(struct bfd_session
*bs
, int nstate
)
1032 case PTM_BFD_ADM_DOWN
:
1034 * Remote peer doesn't want to talk, so lets make the
1037 ptm_bfd_sess_dn(bs
, BD_NEIGHBOR_DOWN
);
1041 /* Remote peer hasn't moved to first stage yet. */
1046 /* We agreed on the settings and the path is up. */
1047 ptm_bfd_sess_up(bs
);
1051 if (bglobal
.debug_peer_event
)
1052 zlog_debug("state-change: unhandled neighbor state: %d",
1058 static void bs_up_handler(struct bfd_session
*bs
, int nstate
)
1061 case PTM_BFD_ADM_DOWN
:
1063 /* Peer lost or asked to shutdown connection. */
1064 ptm_bfd_sess_dn(bs
, BD_NEIGHBOR_DOWN
);
1069 /* Path is up and working. */
1073 if (bglobal
.debug_peer_event
)
1074 zlog_debug("state-change: unhandled neighbor state: %d",
1080 void bs_state_handler(struct bfd_session
*bs
, int nstate
)
1082 switch (bs
->ses_state
) {
1083 case PTM_BFD_ADM_DOWN
:
1084 bs_admin_down_handler(bs
, nstate
);
1087 bs_down_handler(bs
, nstate
);
1090 bs_init_handler(bs
, nstate
);
1093 bs_up_handler(bs
, nstate
);
1097 if (bglobal
.debug_peer_event
)
1098 zlog_debug("state-change: [%s] is in invalid state: %d",
1099 bs_to_string(bs
), nstate
);
1105 * Handles echo timer manipulation after updating timer.
1107 void bs_echo_timer_handler(struct bfd_session
*bs
)
1112 * Before doing any echo handling, check if it is possible to
1115 * - Check for `echo-mode` configuration.
1116 * - Check that we are not using multi hop (RFC 5883,
1118 * - Check that we are already at the up state.
1120 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
) == 0
1121 || CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)
1122 || bs
->ses_state
!= PTM_BFD_UP
)
1125 /* Remote peer asked to stop echo. */
1126 if (bs
->remote_timers
.required_min_echo
== 0) {
1127 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
1128 ptm_bfd_echo_stop(bs
);
1134 * Calculate the echo transmission timer: we must not send
1135 * echo packets faster than the minimum required time
1136 * announced by the remote system.
1138 * RFC 5880, Section 6.8.9.
1140 old_timer
= bs
->echo_xmt_TO
;
1141 if (bs
->remote_timers
.required_min_echo
> bs
->timers
.desired_min_echo_tx
)
1142 bs
->echo_xmt_TO
= bs
->remote_timers
.required_min_echo
;
1144 bs
->echo_xmt_TO
= bs
->timers
.desired_min_echo_tx
;
1146 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
) == 0
1147 || old_timer
!= bs
->echo_xmt_TO
)
1148 ptm_bfd_echo_start(bs
);
1152 * RFC 5880 Section 6.5.
1154 * When a BFD control packet with the final bit is received, we must
1155 * update the session parameters.
1157 void bs_final_handler(struct bfd_session
*bs
)
1159 /* Start using our new timers. */
1160 bs
->cur_timers
.desired_min_tx
= bs
->timers
.desired_min_tx
;
1161 bs
->cur_timers
.required_min_rx
= bs
->timers
.required_min_rx
;
1164 * TODO: demand mode. See RFC 5880 Section 6.1.
1166 * When using demand mode we must disable the detection timer
1167 * for lost control packets.
1169 if (bs
->demand_mode
) {
1170 /* Notify watchers about changed timers. */
1171 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1176 * Calculate transmission time based on new timers.
1178 * Transmission calculation:
1179 * Unless specified by exceptions at the end of Section 6.8.7, the
1180 * transmission time will be determined by the system with the
1183 * RFC 5880, Section 6.8.7.
1185 if (bs
->timers
.desired_min_tx
> bs
->remote_timers
.required_min_rx
)
1186 bs
->xmt_TO
= bs
->timers
.desired_min_tx
;
1188 bs
->xmt_TO
= bs
->remote_timers
.required_min_rx
;
1190 /* Apply new transmission timer immediately. */
1191 ptm_bfd_start_xmt_timer(bs
, false);
1193 /* Notify watchers about changed timers. */
1194 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1197 void bs_set_slow_timers(struct bfd_session
*bs
)
1200 * BFD connection must use slow timers before going up or after
1201 * losing connectivity to avoid wasting bandwidth.
1203 * RFC 5880, Section 6.8.3.
1205 bs
->cur_timers
.desired_min_tx
= BFD_DEF_SLOWTX
;
1206 bs
->cur_timers
.required_min_rx
= BFD_DEF_SLOWTX
;
1207 bs
->cur_timers
.required_min_echo
= 0;
1209 /* Set the appropriated timeouts for slow connection. */
1210 bs
->detect_TO
= (BFD_DEFDETECTMULT
* BFD_DEF_SLOWTX
);
1211 bs
->xmt_TO
= BFD_DEF_SLOWTX
;
1214 void bfd_set_echo(struct bfd_session
*bs
, bool echo
)
1217 /* Check if echo mode is already active. */
1218 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
1221 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
1223 /* Activate/update echo receive timeout timer. */
1224 if (bs
->bdc
== NULL
)
1225 bs_echo_timer_handler(bs
);
1227 /* Check if echo mode is already disabled. */
1228 if (!CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
1231 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
1233 /* Deactivate timeout timer. */
1234 if (bs
->bdc
== NULL
)
1235 ptm_bfd_echo_stop(bs
);
1239 void bfd_set_shutdown(struct bfd_session
*bs
, bool shutdown
)
1244 * Special case: we are batching changes and the previous state was
1245 * not shutdown. Instead of potentially disconnect a running peer,
1246 * we'll get the current status to validate we were really down.
1248 if (bs
->ses_state
== PTM_BFD_UP
)
1249 is_shutdown
= false;
1251 is_shutdown
= CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1254 /* Already shutdown. */
1258 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1260 /* Handle data plane shutdown case. */
1262 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
1263 bfd_dplane_update_session(bs
);
1264 control_notify(bs
, bs
->ses_state
);
1268 /* Disable all events. */
1269 bfd_recvtimer_delete(bs
);
1270 bfd_echo_recvtimer_delete(bs
);
1271 bfd_xmttimer_delete(bs
);
1272 bfd_echo_xmttimer_delete(bs
);
1274 /* Change and notify state change. */
1275 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
1276 control_notify(bs
, bs
->ses_state
);
1278 /* Don't try to send packets with a disabled session. */
1282 /* Already working. */
1286 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
1288 /* Handle data plane shutdown case. */
1290 bs
->ses_state
= PTM_BFD_DOWN
;
1291 bfd_dplane_update_session(bs
);
1292 control_notify(bs
, bs
->ses_state
);
1296 /* Change and notify state change. */
1297 bs
->ses_state
= PTM_BFD_DOWN
;
1298 control_notify(bs
, bs
->ses_state
);
1300 /* Enable timers if non passive, otherwise stop them. */
1301 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
)) {
1302 bfd_recvtimer_delete(bs
);
1303 bfd_xmttimer_delete(bs
);
1305 bfd_recvtimer_update(bs
);
1306 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
1311 void bfd_set_passive_mode(struct bfd_session
*bs
, bool passive
)
1314 SET_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
);
1316 /* Session is already up and running, nothing to do now. */
1317 if (bs
->ses_state
!= PTM_BFD_DOWN
)
1320 /* Lets disable the timers since we are now passive. */
1321 bfd_recvtimer_delete(bs
);
1322 bfd_xmttimer_delete(bs
);
1324 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_PASSIVE
);
1326 /* Session is already up and running, nothing to do now. */
1327 if (bs
->ses_state
!= PTM_BFD_DOWN
)
1330 /* Session is down, let it attempt to start the connection. */
1331 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
1332 bfd_recvtimer_update(bs
);
1339 static const char *get_diag_str(int diag
)
1341 for (int i
= 0; diag_list
[i
].str
; i
++) {
1342 if (diag_list
[i
].type
== diag
)
1343 return diag_list
[i
].str
;
1348 const char *satostr(const struct sockaddr_any
*sa
)
1350 #define INETSTR_BUFCOUNT 8
1351 static char buf
[INETSTR_BUFCOUNT
][INET6_ADDRSTRLEN
];
1353 const struct sockaddr_in
*sin
= &sa
->sa_sin
;
1354 const struct sockaddr_in6
*sin6
= &sa
->sa_sin6
;
1356 bufidx
+= (bufidx
+ 1) % INETSTR_BUFCOUNT
;
1359 switch (sin
->sin_family
) {
1361 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
[bufidx
],
1362 sizeof(buf
[bufidx
]));
1365 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, buf
[bufidx
],
1366 sizeof(buf
[bufidx
]));
1370 strlcpy(buf
[bufidx
], "unknown", sizeof(buf
[bufidx
]));
1377 const char *diag2str(uint8_t diag
)
1383 return "control detection time expired";
1385 return "echo function failed";
1387 return "neighbor signaled session down";
1389 return "forwarding plane reset";
1393 return "concatenated path down";
1395 return "administratively down";
1397 return "reverse concatenated path down";
1403 int strtosa(const char *addr
, struct sockaddr_any
*sa
)
1405 memset(sa
, 0, sizeof(*sa
));
1407 if (inet_pton(AF_INET
, addr
, &sa
->sa_sin
.sin_addr
) == 1) {
1408 sa
->sa_sin
.sin_family
= AF_INET
;
1409 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1410 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
1411 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1415 if (inet_pton(AF_INET6
, addr
, &sa
->sa_sin6
.sin6_addr
) == 1) {
1416 sa
->sa_sin6
.sin6_family
= AF_INET6
;
1417 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1418 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
1419 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1426 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
)
1428 uint64_t year
, month
, day
, hour
, minute
, second
;
1431 #define MINUTES (60)
1432 #define HOURS (60 * MINUTES)
1433 #define DAYS (24 * HOURS)
1434 #define MONTHS (30 * DAYS)
1435 #define YEARS (12 * MONTHS)
1436 if (time
>= YEARS
) {
1437 year
= time
/ YEARS
;
1438 time
-= year
* YEARS
;
1440 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" year(s), ", year
);
1444 if (time
>= MONTHS
) {
1445 month
= time
/ MONTHS
;
1446 time
-= month
* MONTHS
;
1448 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" month(s), ", month
);
1456 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" day(s), ", day
);
1460 if (time
>= HOURS
) {
1461 hour
= time
/ HOURS
;
1462 time
-= hour
* HOURS
;
1464 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" hour(s), ", hour
);
1468 if (time
>= MINUTES
) {
1469 minute
= time
/ MINUTES
;
1470 time
-= minute
* MINUTES
;
1472 rv
= snprintfrr(buf
, buflen
, "%" PRIu64
" minute(s), ", minute
);
1476 second
= time
% MINUTES
;
1477 snprintfrr(buf
, buflen
, "%" PRIu64
" second(s)", second
);
1480 const char *bs_to_string(const struct bfd_session
*bs
)
1482 static char buf
[256];
1483 char addr_buf
[INET6_ADDRSTRLEN
];
1485 bool is_mhop
= CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
);
1487 pos
= snprintf(buf
, sizeof(buf
), "mhop:%s", is_mhop
? "yes" : "no");
1488 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " peer:%s",
1489 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
,
1491 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " local:%s",
1492 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
,
1494 if (bs
->key
.vrfname
[0])
1495 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " vrf:%s",
1497 if (bs
->key
.ifname
[0])
1498 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " ifname:%s",
1506 int bs_observer_add(struct bfd_session
*bs
)
1508 struct bfd_session_observer
*bso
;
1510 bso
= XCALLOC(MTYPE_BFDD_SESSION_OBSERVER
, sizeof(*bso
));
1512 bso
->bso_addr
.family
= bs
->key
.family
;
1513 memcpy(&bso
->bso_addr
.u
.prefix
, &bs
->key
.local
,
1514 sizeof(bs
->key
.local
));
1516 TAILQ_INSERT_TAIL(&bglobal
.bg_obslist
, bso
, bso_entry
);
1521 void bs_observer_del(struct bfd_session_observer
*bso
)
1523 TAILQ_REMOVE(&bglobal
.bg_obslist
, bso
, bso_entry
);
1524 XFREE(MTYPE_BFDD_SESSION_OBSERVER
, bso
);
1527 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
1529 memset(bpc
, 0, sizeof(*bpc
));
1531 bpc
->bpc_ipv4
= (bs
->key
.family
== AF_INET
);
1532 bpc
->bpc_mhop
= bs
->key
.mhop
;
1534 switch (bs
->key
.family
) {
1536 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET
;
1537 memcpy(&bpc
->bpc_peer
.sa_sin
.sin_addr
, &bs
->key
.peer
,
1538 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
1540 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
))) {
1541 bpc
->bpc_local
.sa_sin
.sin_family
= AF_INET6
;
1542 memcpy(&bpc
->bpc_local
.sa_sin
.sin_addr
, &bs
->key
.local
,
1543 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
1548 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET6
;
1549 memcpy(&bpc
->bpc_peer
.sa_sin6
.sin6_addr
, &bs
->key
.peer
,
1550 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
1552 bpc
->bpc_local
.sa_sin6
.sin6_family
= AF_INET6
;
1553 memcpy(&bpc
->bpc_local
.sa_sin6
.sin6_addr
, &bs
->key
.local
,
1554 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
1558 if (bs
->key
.ifname
[0]) {
1559 bpc
->bpc_has_localif
= true;
1560 strlcpy(bpc
->bpc_localif
, bs
->key
.ifname
,
1561 sizeof(bpc
->bpc_localif
));
1564 if (bs
->key
.vrfname
[0]) {
1565 bpc
->bpc_has_vrfname
= true;
1566 strlcpy(bpc
->bpc_vrfname
, bs
->key
.vrfname
,
1567 sizeof(bpc
->bpc_vrfname
));
1573 * BFD hash data structures to find sessions.
1575 static struct hash
*bfd_id_hash
;
1576 static struct hash
*bfd_key_hash
;
1578 static unsigned int bfd_id_hash_do(const void *p
);
1579 static unsigned int bfd_key_hash_do(const void *p
);
1581 static void _bfd_free(struct hash_bucket
*hb
,
1582 void *arg
__attribute__((__unused__
)));
1584 /* BFD hash for our discriminator. */
1585 static unsigned int bfd_id_hash_do(const void *p
)
1587 const struct bfd_session
*bs
= p
;
1589 return jhash_1word(bs
->discrs
.my_discr
, 0);
1592 static bool bfd_id_hash_cmp(const void *n1
, const void *n2
)
1594 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1596 return bs1
->discrs
.my_discr
== bs2
->discrs
.my_discr
;
1599 /* BFD hash for single hop. */
1600 static unsigned int bfd_key_hash_do(const void *p
)
1602 const struct bfd_session
*bs
= p
;
1603 struct bfd_key key
= bs
->key
;
1606 * Local address and interface name are optional and
1607 * can be filled any time after session creation.
1608 * Hash key should not depend on these fields.
1610 memset(&key
.local
, 0, sizeof(key
.local
));
1611 memset(key
.ifname
, 0, sizeof(key
.ifname
));
1613 return jhash(&key
, sizeof(key
), 0);
1616 static bool bfd_key_hash_cmp(const void *n1
, const void *n2
)
1618 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1620 if (bs1
->key
.family
!= bs2
->key
.family
)
1622 if (bs1
->key
.mhop
!= bs2
->key
.mhop
)
1624 if (memcmp(&bs1
->key
.peer
, &bs2
->key
.peer
, sizeof(bs1
->key
.peer
)))
1626 if (memcmp(bs1
->key
.vrfname
, bs2
->key
.vrfname
,
1627 sizeof(bs1
->key
.vrfname
)))
1631 * Local address is optional and can be empty.
1632 * If both addresses are not empty and different,
1633 * then the keys are different.
1635 if (memcmp(&bs1
->key
.local
, &zero_addr
, sizeof(bs1
->key
.local
))
1636 && memcmp(&bs2
->key
.local
, &zero_addr
, sizeof(bs2
->key
.local
))
1637 && memcmp(&bs1
->key
.local
, &bs2
->key
.local
, sizeof(bs1
->key
.local
)))
1641 * Interface name is optional and can be empty.
1642 * If both names are not empty and different,
1643 * then the keys are different.
1645 if (bs1
->key
.ifname
[0] && bs2
->key
.ifname
[0]
1646 && memcmp(bs1
->key
.ifname
, bs2
->key
.ifname
,
1647 sizeof(bs1
->key
.ifname
)))
1655 * Hash public interface / exported functions.
1658 /* Lookup functions. */
1659 struct bfd_session
*bfd_id_lookup(uint32_t id
)
1661 struct bfd_session bs
;
1663 bs
.discrs
.my_discr
= id
;
1665 return hash_lookup(bfd_id_hash
, &bs
);
1668 struct bfd_session
*bfd_key_lookup(struct bfd_key key
)
1670 struct bfd_session bs
;
1674 return hash_lookup(bfd_key_hash
, &bs
);
1680 * Delete functions searches and remove the item from the hash and
1681 * returns a pointer to the removed item data. If the item was not found
1682 * then it returns NULL.
1684 * The data stored inside the hash is not free()ed, so you must do it
1685 * manually after getting the pointer back.
1687 struct bfd_session
*bfd_id_delete(uint32_t id
)
1689 struct bfd_session bs
;
1691 bs
.discrs
.my_discr
= id
;
1693 return hash_release(bfd_id_hash
, &bs
);
1696 struct bfd_session
*bfd_key_delete(struct bfd_key key
)
1698 struct bfd_session bs
;
1702 return hash_release(bfd_key_hash
, &bs
);
1705 /* Iteration functions. */
1706 void bfd_id_iterate(hash_iter_func hif
, void *arg
)
1708 hash_iterate(bfd_id_hash
, hif
, arg
);
1711 void bfd_key_iterate(hash_iter_func hif
, void *arg
)
1713 hash_iterate(bfd_key_hash
, hif
, arg
);
1719 * Inserts session into hash and returns `true` on success, otherwise
1722 bool bfd_id_insert(struct bfd_session
*bs
)
1724 return (hash_get(bfd_id_hash
, bs
, hash_alloc_intern
) == bs
);
1727 bool bfd_key_insert(struct bfd_session
*bs
)
1729 return (hash_get(bfd_key_hash
, bs
, hash_alloc_intern
) == bs
);
1732 void bfd_initialize(void)
1734 bfd_id_hash
= hash_create(bfd_id_hash_do
, bfd_id_hash_cmp
,
1735 "BFD session discriminator hash");
1736 bfd_key_hash
= hash_create(bfd_key_hash_do
, bfd_key_hash_cmp
,
1737 "BFD session hash");
1738 TAILQ_INIT(&bplist
);
1741 static void _bfd_free(struct hash_bucket
*hb
,
1742 void *arg
__attribute__((__unused__
)))
1744 struct bfd_session
*bs
= hb
->data
;
1746 bfd_session_free(bs
);
1749 void bfd_shutdown(void)
1751 struct bfd_profile
*bp
;
1754 * Close and free all BFD sessions.
1756 * _bfd_free() will call bfd_session_free() which will take care
1757 * of removing the session from all hashes, so we just run an
1758 * assert() here to make sure it really happened.
1760 bfd_id_iterate(_bfd_free
, NULL
);
1761 assert(bfd_key_hash
->count
== 0);
1763 /* Now free the hashes themselves. */
1764 hash_free(bfd_id_hash
);
1765 hash_free(bfd_key_hash
);
1767 /* Free all profile allocations. */
1768 while ((bp
= TAILQ_FIRST(&bplist
)) != NULL
)
1769 bfd_profile_free(bp
);
1772 struct bfd_session_iterator
{
1775 const struct bfd_session
*bsi_bs
;
1778 static int _bfd_session_next(struct hash_bucket
*hb
, void *arg
)
1780 struct bfd_session_iterator
*bsi
= arg
;
1781 struct bfd_session
*bs
= hb
->data
;
1783 /* Previous entry signaled stop. */
1784 if (bsi
->bsi_stop
== 1) {
1785 /* Match the single/multi hop sessions. */
1786 if (bs
->key
.mhop
!= bsi
->bsi_mhop
)
1787 return HASHWALK_CONTINUE
;
1790 return HASHWALK_ABORT
;
1793 /* We found the current item, stop in the next one. */
1794 if (bsi
->bsi_bs
== hb
->data
) {
1796 /* Set entry to NULL to signal end of list. */
1798 } else if (bsi
->bsi_bs
== NULL
&& bsi
->bsi_mhop
== bs
->key
.mhop
) {
1799 /* We want the first list item. */
1801 bsi
->bsi_bs
= hb
->data
;
1802 return HASHWALK_ABORT
;
1805 return HASHWALK_CONTINUE
;
1809 * bfd_session_next: uses the current session to find the next.
1811 * `bs` might point to NULL to get the first item of the data structure.
1813 const struct bfd_session
*bfd_session_next(const struct bfd_session
*bs
,
1816 struct bfd_session_iterator bsi
;
1820 bsi
.bsi_mhop
= mhop
;
1821 hash_walk(bfd_key_hash
, _bfd_session_next
, &bsi
);
1822 if (bsi
.bsi_stop
== 0)
1828 static void _bfd_session_remove_manual(struct hash_bucket
*hb
,
1829 void *arg
__attribute__((__unused__
)))
1831 struct bfd_session
*bs
= hb
->data
;
1833 /* Delete only manually configured sessions. */
1834 if (CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
) == 0)
1838 UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
);
1840 /* Don't delete sessions still in use. */
1841 if (bs
->refcount
!= 0)
1844 bfd_session_free(bs
);
1848 * bfd_sessions_remove_manual: remove all manually configured sessions.
1850 * NOTE: this function doesn't remove automatically created sessions.
1852 void bfd_sessions_remove_manual(void)
1854 hash_iterate(bfd_key_hash
, _bfd_session_remove_manual
, NULL
);
1857 void bfd_profiles_remove(void)
1859 struct bfd_profile
*bp
;
1861 while ((bp
= TAILQ_FIRST(&bplist
)) != NULL
)
1862 bfd_profile_free(bp
);
1866 * Profile related hash functions.
1868 static void _bfd_profile_update(struct hash_bucket
*hb
, void *arg
)
1870 struct bfd_profile
*bp
= arg
;
1871 struct bfd_session
*bs
= hb
->data
;
1873 /* This session is not using the profile. */
1874 if (bs
->profile_name
== NULL
|| strcmp(bs
->profile_name
, bp
->name
) != 0)
1877 bfd_profile_apply(bp
->name
, bs
);
1880 void bfd_profile_update(struct bfd_profile
*bp
)
1882 hash_iterate(bfd_key_hash
, _bfd_profile_update
, bp
);
1885 static void _bfd_profile_detach(struct hash_bucket
*hb
, void *arg
)
1887 struct bfd_profile
*bp
= arg
;
1888 struct bfd_session
*bs
= hb
->data
;
1890 /* This session is not using the profile. */
1891 if (bs
->profile_name
== NULL
|| strcmp(bs
->profile_name
, bp
->name
) != 0)
1894 bfd_profile_remove(bs
);
1897 static void bfd_profile_detach(struct bfd_profile
*bp
)
1899 hash_iterate(bfd_key_hash
, _bfd_profile_detach
, bp
);
1903 * VRF related functions.
1905 static int bfd_vrf_new(struct vrf
*vrf
)
1907 if (bglobal
.debug_zebra
)
1908 zlog_debug("VRF Created: %s(%u)", vrf
->name
, vrf
->vrf_id
);
1913 static int bfd_vrf_delete(struct vrf
*vrf
)
1915 if (bglobal
.debug_zebra
)
1916 zlog_debug("VRF Deletion: %s(%u)", vrf
->name
, vrf
->vrf_id
);
1921 static int bfd_vrf_enable(struct vrf
*vrf
)
1923 struct bfd_vrf_global
*bvrf
;
1925 /* a different name */
1927 bvrf
= XCALLOC(MTYPE_BFDD_VRF
, sizeof(struct bfd_vrf_global
));
1929 vrf
->info
= (void *)bvrf
;
1931 /* Disable sockets if using data plane. */
1932 if (bglobal
.bg_use_dplane
) {
1935 bvrf
->bg_shop6
= -1;
1936 bvrf
->bg_mhop6
= -1;
1938 bvrf
->bg_echov6
= -1;
1943 if (bglobal
.debug_zebra
)
1944 zlog_debug("VRF enable add %s id %u", vrf
->name
, vrf
->vrf_id
);
1947 bvrf
->bg_shop
= bp_udp_shop(vrf
);
1949 bvrf
->bg_mhop
= bp_udp_mhop(vrf
);
1950 if (!bvrf
->bg_shop6
)
1951 bvrf
->bg_shop6
= bp_udp6_shop(vrf
);
1952 if (!bvrf
->bg_mhop6
)
1953 bvrf
->bg_mhop6
= bp_udp6_mhop(vrf
);
1955 bvrf
->bg_echo
= bp_echo_socket(vrf
);
1956 if (!bvrf
->bg_echov6
)
1957 bvrf
->bg_echov6
= bp_echov6_socket(vrf
);
1959 if (!bvrf
->bg_ev
[0] && bvrf
->bg_shop
!= -1)
1960 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_shop
,
1962 if (!bvrf
->bg_ev
[1] && bvrf
->bg_mhop
!= -1)
1963 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_mhop
,
1965 if (!bvrf
->bg_ev
[2] && bvrf
->bg_shop6
!= -1)
1966 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_shop6
,
1968 if (!bvrf
->bg_ev
[3] && bvrf
->bg_mhop6
!= -1)
1969 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_mhop6
,
1971 if (!bvrf
->bg_ev
[4] && bvrf
->bg_echo
!= -1)
1972 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_echo
,
1974 if (!bvrf
->bg_ev
[5] && bvrf
->bg_echov6
!= -1)
1975 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_echov6
,
1978 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
1979 bfdd_zclient_register(vrf
->vrf_id
);
1980 bfdd_sessions_enable_vrf(vrf
);
1985 static int bfd_vrf_disable(struct vrf
*vrf
)
1987 struct bfd_vrf_global
*bvrf
;
1993 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
1994 bfdd_sessions_disable_vrf(vrf
);
1995 bfdd_zclient_unregister(vrf
->vrf_id
);
1998 if (bglobal
.debug_zebra
)
1999 zlog_debug("VRF disable %s id %d", vrf
->name
, vrf
->vrf_id
);
2001 /* Disable read/write poll triggering. */
2002 THREAD_OFF(bvrf
->bg_ev
[0]);
2003 THREAD_OFF(bvrf
->bg_ev
[1]);
2004 THREAD_OFF(bvrf
->bg_ev
[2]);
2005 THREAD_OFF(bvrf
->bg_ev
[3]);
2006 THREAD_OFF(bvrf
->bg_ev
[4]);
2007 THREAD_OFF(bvrf
->bg_ev
[5]);
2009 /* Close all descriptors. */
2010 socket_close(&bvrf
->bg_echo
);
2011 socket_close(&bvrf
->bg_shop
);
2012 socket_close(&bvrf
->bg_mhop
);
2013 if (bvrf
->bg_shop6
!= -1)
2014 socket_close(&bvrf
->bg_shop6
);
2015 if (bvrf
->bg_mhop6
!= -1)
2016 socket_close(&bvrf
->bg_mhop6
);
2017 socket_close(&bvrf
->bg_echo
);
2018 if (bvrf
->bg_echov6
!= -1)
2019 socket_close(&bvrf
->bg_echov6
);
2022 XFREE(MTYPE_BFDD_VRF
, bvrf
);
2028 void bfd_vrf_init(void)
2030 vrf_init(bfd_vrf_new
, bfd_vrf_enable
, bfd_vrf_disable
, bfd_vrf_delete
);
2033 void bfd_vrf_terminate(void)
2038 struct bfd_vrf_global
*bfd_vrf_look_by_session(struct bfd_session
*bfd
)
2042 if (!vrf_is_backend_netns()) {
2043 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
2045 return (struct bfd_vrf_global
*)vrf
->info
;
2052 return bfd
->vrf
->info
;
2055 unsigned long bfd_get_session_count(void)
2057 return bfd_key_hash
->count
;
2060 void bfd_rtt_init(struct bfd_session
*bfd
)
2064 /* initialize RTT */
2067 for (i
= 0; i
< BFD_RTT_SAMPLE
; i
++)