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"
34 DEFINE_MTYPE_STATIC(BFDD
, BFDD_CONFIG
, "long-lived configuration memory")
35 DEFINE_MTYPE_STATIC(BFDD
, BFDD_SESSION_OBSERVER
, "Session observer")
36 DEFINE_MTYPE_STATIC(BFDD
, BFDD_VRF
, "BFD VRF")
41 static uint32_t ptm_bfd_gen_ID(void);
42 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
);
43 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
45 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
46 static const char *get_diag_str(int diag
);
48 static void bs_admin_down_handler(struct bfd_session
*bs
, int nstate
);
49 static void bs_down_handler(struct bfd_session
*bs
, int nstate
);
50 static void bs_init_handler(struct bfd_session
*bs
, int nstate
);
51 static void bs_up_handler(struct bfd_session
*bs
, int nstate
);
52 static void bs_neighbour_admin_down_handler(struct bfd_session
*bfd
,
55 /* Zeroed array with the size of an IPv6 address. */
56 struct in6_addr zero_addr
;
61 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
62 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
65 memset(key
, 0, sizeof(*key
));
67 switch (peer
->sa_sin
.sin_family
) {
69 key
->family
= AF_INET
;
70 memcpy(&key
->peer
, &peer
->sa_sin
.sin_addr
,
71 sizeof(peer
->sa_sin
.sin_addr
));
72 memcpy(&key
->local
, &local
->sa_sin
.sin_addr
,
73 sizeof(local
->sa_sin
.sin_addr
));
76 key
->family
= AF_INET6
;
77 memcpy(&key
->peer
, &peer
->sa_sin6
.sin6_addr
,
78 sizeof(peer
->sa_sin6
.sin6_addr
));
79 memcpy(&key
->local
, &local
->sa_sin6
.sin6_addr
,
80 sizeof(local
->sa_sin6
.sin6_addr
));
85 if (ifname
&& ifname
[0])
86 strlcpy(key
->ifname
, ifname
, sizeof(key
->ifname
));
87 if (vrfname
&& vrfname
[0])
88 strlcpy(key
->vrfname
, vrfname
, sizeof(key
->vrfname
));
90 strlcpy(key
->vrfname
, VRF_DEFAULT_NAME
, sizeof(key
->vrfname
));
93 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
)
95 struct bfd_session
*bs
;
96 struct peer_label
*pl
;
99 /* Try to find label first. */
100 if (bpc
->bpc_has_label
) {
101 pl
= pl_find(bpc
->bpc_label
);
108 /* Otherwise fallback to peer/local hash lookup. */
109 gen_bfd_key(&key
, &bpc
->bpc_peer
, &bpc
->bpc_local
, bpc
->bpc_mhop
,
110 bpc
->bpc_localif
, bpc
->bpc_vrfname
);
112 return bfd_key_lookup(key
);
116 * Starts a disabled BFD session.
118 * A session is disabled when the specified interface/VRF doesn't exist
119 * yet. It might happen on FRR boot or with virtual interfaces.
121 int bfd_session_enable(struct bfd_session
*bs
)
123 struct interface
*ifp
= NULL
;
124 struct vrf
*vrf
= NULL
;
128 * If the interface or VRF doesn't exist, then we must register
129 * the session but delay its start.
131 if (bs
->key
.vrfname
[0]) {
132 vrf
= vrf_lookup_by_name(bs
->key
.vrfname
);
135 "session-enable: specified VRF doesn't exists.");
140 if (bs
->key
.ifname
[0]) {
142 ifp
= if_lookup_by_name(bs
->key
.ifname
, vrf
->vrf_id
);
144 ifp
= if_lookup_by_name_all_vrf(bs
->key
.ifname
);
147 "session-enable: specified interface doesn't exists.");
150 if (bs
->key
.ifname
[0] && !vrf
) {
151 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
154 "session-enable: specified VRF doesn't exists.");
160 /* Assign interface/VRF pointers. */
163 bs
->vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
166 if (bs
->key
.ifname
[0]
167 && BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
) == 0)
170 /* Sanity check: don't leak open sockets. */
171 if (bs
->sock
!= -1) {
172 log_debug("session-enable: previous socket open");
178 * Get socket for transmitting control packets. Note that if we
179 * could use the destination port (3784) for the source
180 * port we wouldn't need a socket per session.
182 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_IPV6
) == 0) {
183 psock
= bp_peer_socket(bs
);
187 psock
= bp_peer_socketv6(bs
);
193 * We've got a valid socket, lets start the timers and the
197 bfd_recvtimer_update(bs
);
198 ptm_bfd_start_xmt_timer(bs
, false);
204 * Disabled a running BFD session.
206 * A session is disabled when the specified interface/VRF gets removed
207 * (e.g. virtual interfaces).
209 void bfd_session_disable(struct bfd_session
*bs
)
211 /* Free up socket resources. */
212 if (bs
->sock
!= -1) {
217 /* Disable all timers. */
218 bfd_recvtimer_delete(bs
);
219 bfd_xmttimer_delete(bs
);
220 ptm_bfd_echo_stop(bs
);
224 /* Set session down so it doesn't report UP and disabled. */
225 ptm_bfd_sess_dn(bs
, BD_PATH_DOWN
);
228 static uint32_t ptm_bfd_gen_ID(void)
233 * RFC 5880, Section 6.8.1. recommends that we should generate
234 * random session identification numbers.
237 session_id
= ((random() << 16) & 0xFFFF0000)
238 | (random() & 0x0000FFFF);
239 } while (session_id
== 0 || bfd_id_lookup(session_id
) != NULL
);
244 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
)
246 uint64_t jitter
, xmt_TO
;
249 xmt_TO
= is_echo
? bfd
->echo_xmt_TO
: bfd
->xmt_TO
;
252 * From section 6.5.2: trasmit interval should be randomly jittered
254 * 75% and 100% of nominal value, unless detect_mult is 1, then should
256 * between 75% and 90%.
258 maxpercent
= (bfd
->detect_mult
== 1) ? 16 : 26;
259 jitter
= (xmt_TO
* (75 + (random() % maxpercent
))) / 100;
260 /* XXX remove that division above */
263 bfd_echo_xmttimer_update(bfd
, jitter
);
265 bfd_xmttimer_update(bfd
, jitter
);
268 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
)
270 /* Send the scheduled echo packet */
271 ptm_bfd_echo_snd(bfd
);
273 /* Restart the timer for next time */
274 ptm_bfd_start_xmt_timer(bfd
, true);
277 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
)
279 /* Send the scheduled control packet */
280 ptm_bfd_snd(bfd
, fbit
);
282 /* Restart the timer for next time */
283 ptm_bfd_start_xmt_timer(bfd
, false);
286 void ptm_bfd_echo_stop(struct bfd_session
*bfd
)
288 bfd
->echo_xmt_TO
= 0;
289 bfd
->echo_detect_TO
= 0;
290 BFD_UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
);
292 bfd_echo_xmttimer_delete(bfd
);
293 bfd_echo_recvtimer_delete(bfd
);
296 void ptm_bfd_echo_start(struct bfd_session
*bfd
)
298 bfd
->echo_detect_TO
= (bfd
->remote_detect_mult
* bfd
->echo_xmt_TO
);
299 if (bfd
->echo_detect_TO
> 0)
300 ptm_bfd_echo_xmt_TO(bfd
);
303 void ptm_bfd_sess_up(struct bfd_session
*bfd
)
305 int old_state
= bfd
->ses_state
;
308 bfd
->ses_state
= PTM_BFD_UP
;
309 monotime(&bfd
->uptime
);
311 /* Connection is up, lets negotiate timers. */
312 bfd_set_polling(bfd
);
314 /* Start sending control packets with poll bit immediately. */
317 control_notify(bfd
, bfd
->ses_state
);
319 if (old_state
!= bfd
->ses_state
) {
320 bfd
->stats
.session_up
++;
321 log_info("state-change: [%s] %s -> %s", bs_to_string(bfd
),
322 state_list
[old_state
].str
,
323 state_list
[bfd
->ses_state
].str
);
327 void ptm_bfd_sess_dn(struct bfd_session
*bfd
, uint8_t diag
)
329 int old_state
= bfd
->ses_state
;
331 bfd
->local_diag
= diag
;
332 bfd
->discrs
.remote_discr
= 0;
333 bfd
->ses_state
= PTM_BFD_DOWN
;
335 bfd
->demand_mode
= 0;
336 monotime(&bfd
->downtime
);
339 * Only attempt to send if we have a valid socket:
340 * this function might be called by session disablers and in
341 * this case we won't have a valid socket (i.e. interface was
342 * removed or VRF doesn't exist anymore).
347 /* Slow down the control packets, the connection is down. */
348 bs_set_slow_timers(bfd
);
350 /* only signal clients when going from up->down state */
351 if (old_state
== PTM_BFD_UP
)
352 control_notify(bfd
, PTM_BFD_DOWN
);
354 /* Stop echo packet transmission if they are active */
355 if (BFD_CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
356 ptm_bfd_echo_stop(bfd
);
358 if (old_state
!= bfd
->ses_state
) {
359 bfd
->stats
.session_down
++;
360 log_info("state-change: [%s] %s -> %s reason:%s",
361 bs_to_string(bfd
), state_list
[old_state
].str
,
362 state_list
[bfd
->ses_state
].str
,
363 get_diag_str(bfd
->local_diag
));
367 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
370 struct bfd_session
*bs
;
372 bs
= bfd_id_lookup(ldisc
);
376 switch (bs
->key
.family
) {
378 if (memcmp(&sa
->sa_sin
.sin_addr
, &bs
->key
.peer
,
379 sizeof(sa
->sa_sin
.sin_addr
)))
383 if (memcmp(&sa
->sa_sin6
.sin6_addr
, &bs
->key
.peer
,
384 sizeof(sa
->sa_sin6
.sin6_addr
)))
392 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
393 struct sockaddr_any
*peer
,
394 struct sockaddr_any
*local
,
395 ifindex_t ifindex
, vrf_id_t vrfid
,
398 struct interface
*ifp
;
402 /* Find our session using the ID signaled by the remote end. */
403 if (cp
->discrs
.remote_discr
)
404 return bfd_find_disc(peer
, ntohl(cp
->discrs
.remote_discr
));
407 * Search for session without using discriminator.
409 * XXX: we can't trust `vrfid` because the VRF handling is not
410 * properly implemented. Meanwhile we should use the interface
411 * VRF to find out which one it belongs.
413 ifp
= if_lookup_by_index_all_vrf(ifindex
);
415 if (vrfid
!= VRF_DEFAULT
)
416 vrf
= vrf_lookup_by_id(vrfid
);
420 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
422 gen_bfd_key(&key
, peer
, local
, is_mhop
, ifp
? ifp
->name
: NULL
,
423 vrf
? vrf
->name
: VRF_DEFAULT_NAME
);
425 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
426 return bfd_key_lookup(key
);
429 int bfd_xmt_cb(struct thread
*t
)
431 struct bfd_session
*bs
= THREAD_ARG(t
);
433 ptm_bfd_xmt_TO(bs
, 0);
438 int bfd_echo_xmt_cb(struct thread
*t
)
440 struct bfd_session
*bs
= THREAD_ARG(t
);
442 if (bs
->echo_xmt_TO
> 0)
443 ptm_bfd_echo_xmt_TO(bs
);
448 /* Was ptm_bfd_detect_TO() */
449 int bfd_recvtimer_cb(struct thread
*t
)
451 struct bfd_session
*bs
= THREAD_ARG(t
);
453 switch (bs
->ses_state
) {
456 ptm_bfd_sess_dn(bs
, BD_CONTROL_EXPIRED
);
457 bfd_recvtimer_update(bs
);
461 /* Second detect time expiration, zero remote discr (section
464 bs
->discrs
.remote_discr
= 0;
471 /* Was ptm_bfd_echo_detect_TO() */
472 int bfd_echo_recvtimer_cb(struct thread
*t
)
474 struct bfd_session
*bs
= THREAD_ARG(t
);
476 switch (bs
->ses_state
) {
479 ptm_bfd_sess_dn(bs
, BD_ECHO_FAILED
);
486 struct bfd_session
*bfd_session_new(void)
488 struct bfd_session
*bs
;
490 bs
= XCALLOC(MTYPE_BFDD_CONFIG
, sizeof(*bs
));
492 bs
->timers
.desired_min_tx
= BFD_DEFDESIREDMINTX
;
493 bs
->timers
.required_min_rx
= BFD_DEFREQUIREDMINRX
;
494 bs
->timers
.required_min_echo
= BFD_DEF_REQ_MIN_ECHO
;
495 bs
->detect_mult
= BFD_DEFDETECTMULT
;
496 bs
->mh_ttl
= BFD_DEF_MHOP_TTL
;
497 bs
->ses_state
= PTM_BFD_DOWN
;
499 /* Initiate connection with slow timers. */
500 bs_set_slow_timers(bs
);
502 /* Initiate remote settings as well. */
503 bs
->remote_timers
= bs
->cur_timers
;
504 bs
->remote_detect_mult
= BFD_DEFDETECTMULT
;
507 monotime(&bs
->uptime
);
508 bs
->downtime
= bs
->uptime
;
513 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
)
515 /* New label treatment:
516 * - Check if the label is taken;
517 * - Try to allocate the memory for it and register;
519 if (bs
->pl
== NULL
) {
520 if (pl_find(nlabel
) != NULL
) {
521 /* Someone is already using it. */
525 if (pl_new(nlabel
, bs
) == NULL
)
532 * Test label change consistency:
533 * - Do nothing if it's the same label;
534 * - Check if the future label is already taken;
537 if (strcmp(nlabel
, bs
->pl
->pl_label
) == 0)
539 if (pl_find(nlabel
) != NULL
)
542 strlcpy(bs
->pl
->pl_label
, nlabel
, sizeof(bs
->pl
->pl_label
));
546 static void _bfd_session_update(struct bfd_session
*bs
,
547 struct bfd_peer_cfg
*bpc
)
550 /* Check if echo mode is already active. */
551 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
554 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
556 /* Activate/update echo receive timeout timer. */
557 bs_echo_timer_handler(bs
);
559 /* Check if echo mode is already disabled. */
560 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
563 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
564 ptm_bfd_echo_stop(bs
);
568 if (bpc
->bpc_has_txinterval
)
569 bs
->timers
.desired_min_tx
= bpc
->bpc_txinterval
* 1000;
571 if (bpc
->bpc_has_recvinterval
)
572 bs
->timers
.required_min_rx
= bpc
->bpc_recvinterval
* 1000;
574 if (bpc
->bpc_has_detectmultiplier
)
575 bs
->detect_mult
= bpc
->bpc_detectmultiplier
;
577 if (bpc
->bpc_has_echointerval
)
578 bs
->timers
.required_min_echo
= bpc
->bpc_echointerval
* 1000;
580 if (bpc
->bpc_has_label
)
581 bfd_session_update_label(bs
, bpc
->bpc_label
);
583 if (bpc
->bpc_shutdown
) {
584 /* Check if already shutdown. */
585 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
588 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
590 /* Disable all events. */
591 bfd_recvtimer_delete(bs
);
592 bfd_echo_recvtimer_delete(bs
);
593 bfd_xmttimer_delete(bs
);
594 bfd_echo_xmttimer_delete(bs
);
596 /* Change and notify state change. */
597 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
598 control_notify(bs
, bs
->ses_state
);
600 /* Don't try to send packets with a disabled session. */
604 /* Check if already working. */
605 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
608 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
610 /* Change and notify state change. */
611 bs
->ses_state
= PTM_BFD_DOWN
;
612 control_notify(bs
, bs
->ses_state
);
614 /* Enable all timers. */
615 bfd_recvtimer_update(bs
);
616 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
619 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
))
622 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
624 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
))
627 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CBIT
);
631 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
633 /* User didn't want to update, return failure. */
634 if (bpc
->bpc_createonly
)
637 _bfd_session_update(bs
, bpc
);
639 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
644 void bfd_session_free(struct bfd_session
*bs
)
646 struct bfd_session_observer
*bso
;
648 bfd_session_disable(bs
);
650 bfd_key_delete(bs
->key
);
651 bfd_id_delete(bs
->discrs
.my_discr
);
653 /* Remove observer if any. */
654 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
655 if (bso
->bso_bs
!= bs
)
661 bs_observer_del(bso
);
665 XFREE(MTYPE_BFDD_CONFIG
, bs
);
668 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
)
670 struct bfd_session
*bfd
, *l_bfd
;
672 /* check to see if this needs a new session */
673 l_bfd
= bs_peer_find(bpc
);
675 /* Requesting a duplicated peer means update configuration. */
676 if (bfd_session_update(l_bfd
, bpc
) == 0)
682 /* Get BFD session storage with its defaults. */
683 bfd
= bfd_session_new();
685 log_error("session-new: allocation failed");
690 * Store interface/VRF name in case we need to delay session
691 * start. See `bfd_session_enable` for more information.
693 if (bpc
->bpc_has_localif
)
694 strlcpy(bfd
->key
.ifname
, bpc
->bpc_localif
,
695 sizeof(bfd
->key
.ifname
));
697 if (bpc
->bpc_has_vrfname
)
698 strlcpy(bfd
->key
.vrfname
, bpc
->bpc_vrfname
,
699 sizeof(bfd
->key
.vrfname
));
701 strlcpy(bfd
->key
.vrfname
, VRF_DEFAULT_NAME
,
702 sizeof(bfd
->key
.vrfname
));
704 /* Copy remaining data. */
705 if (bpc
->bpc_ipv4
== false)
706 BFD_SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
);
708 bfd
->key
.family
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
709 switch (bfd
->key
.family
) {
711 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin
.sin_addr
,
712 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
713 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin
.sin_addr
,
714 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
718 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin6
.sin6_addr
,
719 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
720 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin6
.sin6_addr
,
721 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
730 BFD_SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MH
);
732 bfd
->key
.mhop
= bpc
->bpc_mhop
;
734 if (bs_registrate(bfd
) == NULL
)
737 /* Apply other configurations. */
738 _bfd_session_update(bfd
, bpc
);
743 struct bfd_session
*bs_registrate(struct bfd_session
*bfd
)
745 /* Registrate session into data structures. */
747 bfd
->discrs
.my_discr
= ptm_bfd_gen_ID();
750 /* Try to enable session and schedule for packet receive/send. */
751 if (bfd_session_enable(bfd
) == -1) {
752 /* Unrecoverable failure, remove the session/peer. */
753 bfd_session_free(bfd
);
757 /* Add observer if we have moving parts. */
758 if (bfd
->key
.ifname
[0] || bfd
->key
.vrfname
[0] || bfd
->sock
== -1)
759 bs_observer_add(bfd
);
761 log_info("session-new: %s", bs_to_string(bfd
));
763 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bfd
);
768 int ptm_bfd_sess_del(struct bfd_peer_cfg
*bpc
)
770 struct bfd_session
*bs
;
772 /* Find session and call free(). */
773 bs
= bs_peer_find(bpc
);
777 /* This pointer is being referenced, don't let it be deleted. */
778 if (bs
->refcount
> 0) {
779 log_error("session-delete: refcount failure: %" PRIu64
785 log_info("session-delete: %s", bs_to_string(bs
));
787 control_notify_config(BCM_NOTIFY_CONFIG_DELETE
, bs
);
789 bfd_session_free(bs
);
794 void bfd_set_polling(struct bfd_session
*bs
)
797 * Start polling procedure: the only timers that require polling
798 * to change value without losing connection are:
800 * - Desired minimum transmission interval;
801 * - Required minimum receive interval;
803 * RFC 5880, Section 6.8.3.
809 * bs_<state>_handler() functions implement the BFD state machine
810 * transition mechanism. `<state>` is the current session state and
811 * the parameter `nstate` is the peer new state.
813 static void bs_admin_down_handler(struct bfd_session
*bs
814 __attribute__((__unused__
)),
815 int nstate
__attribute__((__unused__
)))
818 * We are administratively down, there is no state machine
823 static void bs_down_handler(struct bfd_session
*bs
, int nstate
)
826 case PTM_BFD_ADM_DOWN
:
828 * Remote peer doesn't want to talk, so lets keep the
832 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
837 * Remote peer agreed that the path is down, lets try to
840 bs
->ses_state
= PTM_BFD_INIT
;
845 * Remote peer told us his path is up, lets turn
846 * activate the session.
852 log_debug("state-change: unhandled neighbor state: %d", nstate
);
857 static void bs_init_handler(struct bfd_session
*bs
, int nstate
)
860 case PTM_BFD_ADM_DOWN
:
862 * Remote peer doesn't want to talk, so lets make the
865 bs
->ses_state
= PTM_BFD_DOWN
;
869 /* Remote peer hasn't moved to first stage yet. */
874 /* We agreed on the settings and the path is up. */
879 log_debug("state-change: unhandled neighbor state: %d", nstate
);
884 static void bs_neighbour_admin_down_handler(struct bfd_session
*bfd
,
887 int old_state
= bfd
->ses_state
;
889 bfd
->local_diag
= diag
;
890 bfd
->discrs
.remote_discr
= 0;
891 bfd
->ses_state
= PTM_BFD_DOWN
;
893 bfd
->demand_mode
= 0;
894 monotime(&bfd
->downtime
);
896 /* Slow down the control packets, the connection is down. */
897 bs_set_slow_timers(bfd
);
899 /* only signal clients when going from up->down state */
900 if (old_state
== PTM_BFD_UP
)
901 control_notify(bfd
, PTM_BFD_ADM_DOWN
);
903 /* Stop echo packet transmission if they are active */
904 if (BFD_CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
905 ptm_bfd_echo_stop(bfd
);
907 if (old_state
!= bfd
->ses_state
) {
908 bfd
->stats
.session_down
++;
910 log_info("state-change: [%s] %s -> %s reason:%s",
911 bs_to_string(bfd
), state_list
[old_state
].str
,
912 state_list
[bfd
->ses_state
].str
,
913 get_diag_str(bfd
->local_diag
));
917 static void bs_up_handler(struct bfd_session
*bs
, int nstate
)
920 case PTM_BFD_ADM_DOWN
:
921 bs_neighbour_admin_down_handler(bs
, BD_ADMIN_DOWN
);
925 /* Peer lost or asked to shutdown connection. */
926 ptm_bfd_sess_dn(bs
, BD_NEIGHBOR_DOWN
);
931 /* Path is up and working. */
935 log_debug("state-change: unhandled neighbor state: %d", nstate
);
940 void bs_state_handler(struct bfd_session
*bs
, int nstate
)
942 switch (bs
->ses_state
) {
943 case PTM_BFD_ADM_DOWN
:
944 bs_admin_down_handler(bs
, nstate
);
947 bs_down_handler(bs
, nstate
);
950 bs_init_handler(bs
, nstate
);
953 bs_up_handler(bs
, nstate
);
957 log_debug("state-change: [%s] is in invalid state: %d",
958 bs_to_string(bs
), nstate
);
964 * Handles echo timer manipulation after updating timer.
966 void bs_echo_timer_handler(struct bfd_session
*bs
)
971 * Before doing any echo handling, check if it is possible to
974 * - Check for `echo-mode` configuration.
975 * - Check that we are not using multi hop (RFC 5883,
977 * - Check that we are already at the up state.
979 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
) == 0
980 || BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)
981 || bs
->ses_state
!= PTM_BFD_UP
)
984 /* Remote peer asked to stop echo. */
985 if (bs
->remote_timers
.required_min_echo
== 0) {
986 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
987 ptm_bfd_echo_stop(bs
);
993 * Calculate the echo transmission timer: we must not send
994 * echo packets faster than the minimum required time
995 * announced by the remote system.
997 * RFC 5880, Section 6.8.9.
999 old_timer
= bs
->echo_xmt_TO
;
1000 if (bs
->remote_timers
.required_min_echo
> bs
->timers
.required_min_echo
)
1001 bs
->echo_xmt_TO
= bs
->remote_timers
.required_min_echo
;
1003 bs
->echo_xmt_TO
= bs
->timers
.required_min_echo
;
1005 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
) == 0
1006 || old_timer
!= bs
->echo_xmt_TO
)
1007 ptm_bfd_echo_start(bs
);
1011 * RFC 5880 Section 6.5.
1013 * When a BFD control packet with the final bit is received, we must
1014 * update the session parameters.
1016 void bs_final_handler(struct bfd_session
*bs
)
1018 /* Start using our new timers. */
1019 bs
->cur_timers
.desired_min_tx
= bs
->timers
.desired_min_tx
;
1020 bs
->cur_timers
.required_min_rx
= bs
->timers
.required_min_rx
;
1023 * TODO: demand mode. See RFC 5880 Section 6.1.
1025 * When using demand mode we must disable the detection timer
1026 * for lost control packets.
1028 if (bs
->demand_mode
) {
1029 /* Notify watchers about changed timers. */
1030 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1035 * Calculate detection time based on new timers.
1037 * Transmission calculation:
1038 * We must respect the RequiredMinRxInterval from the remote
1039 * system: if our desired transmission timer is more than the
1040 * minimum receive rate, then we must lower it to at least the
1041 * minimum receive interval.
1043 * RFC 5880, Section 6.8.3.
1045 if (bs
->timers
.desired_min_tx
> bs
->remote_timers
.required_min_rx
)
1046 bs
->xmt_TO
= bs
->remote_timers
.required_min_rx
;
1048 bs
->xmt_TO
= bs
->timers
.desired_min_tx
;
1050 /* Apply new transmission timer immediately. */
1051 ptm_bfd_start_xmt_timer(bs
, false);
1054 * Detection timeout calculation:
1055 * The minimum detection timeout is the remote detection
1056 * multipler (number of packets to be missed) times the agreed
1057 * transmission interval.
1059 * RFC 5880, Section 6.8.4.
1061 * TODO: support sending/counting more packets inside detection
1064 if (bs
->remote_timers
.required_min_rx
> bs
->timers
.desired_min_tx
)
1065 bs
->detect_TO
= bs
->remote_detect_mult
1066 * bs
->remote_timers
.required_min_rx
;
1068 bs
->detect_TO
= bs
->remote_detect_mult
1069 * bs
->timers
.desired_min_tx
;
1071 /* Apply new receive timer immediately. */
1072 bfd_recvtimer_update(bs
);
1074 /* Notify watchers about changed timers. */
1075 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1078 void bs_set_slow_timers(struct bfd_session
*bs
)
1081 * BFD connection must use slow timers before going up or after
1082 * losing connectivity to avoid wasting bandwidth.
1084 * RFC 5880, Section 6.8.3.
1086 bs
->cur_timers
.desired_min_tx
= BFD_DEF_SLOWTX
;
1087 bs
->cur_timers
.required_min_rx
= BFD_DEF_SLOWTX
;
1088 bs
->cur_timers
.required_min_echo
= 0;
1090 /* Set the appropriated timeouts for slow connection. */
1091 bs
->detect_TO
= (BFD_DEFDETECTMULT
* BFD_DEF_SLOWTX
);
1092 bs
->xmt_TO
= BFD_DEF_SLOWTX
;
1098 static const char *get_diag_str(int diag
)
1100 for (int i
= 0; diag_list
[i
].str
; i
++) {
1101 if (diag_list
[i
].type
== diag
)
1102 return diag_list
[i
].str
;
1107 const char *satostr(struct sockaddr_any
*sa
)
1109 #define INETSTR_BUFCOUNT 8
1110 static char buf
[INETSTR_BUFCOUNT
][INET6_ADDRSTRLEN
];
1112 struct sockaddr_in
*sin
= &sa
->sa_sin
;
1113 struct sockaddr_in6
*sin6
= &sa
->sa_sin6
;
1115 bufidx
+= (bufidx
+ 1) % INETSTR_BUFCOUNT
;
1118 switch (sin
->sin_family
) {
1120 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
[bufidx
],
1121 sizeof(buf
[bufidx
]));
1124 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, buf
[bufidx
],
1125 sizeof(buf
[bufidx
]));
1129 strlcpy(buf
[bufidx
], "unknown", sizeof(buf
[bufidx
]));
1136 const char *diag2str(uint8_t diag
)
1142 return "control detection time expired";
1144 return "echo function failed";
1146 return "neighbor signaled session down";
1148 return "forwarding plane reset";
1152 return "concatenated path down";
1154 return "administratively down";
1156 return "reverse concatenated path down";
1162 int strtosa(const char *addr
, struct sockaddr_any
*sa
)
1164 memset(sa
, 0, sizeof(*sa
));
1166 if (inet_pton(AF_INET
, addr
, &sa
->sa_sin
.sin_addr
) == 1) {
1167 sa
->sa_sin
.sin_family
= AF_INET
;
1168 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1169 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
1170 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1174 if (inet_pton(AF_INET6
, addr
, &sa
->sa_sin6
.sin6_addr
) == 1) {
1175 sa
->sa_sin6
.sin6_family
= AF_INET6
;
1176 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1177 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
1178 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1185 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
)
1187 unsigned int year
, month
, day
, hour
, minute
, second
;
1190 #define MINUTES (60)
1191 #define HOURS (60 * MINUTES)
1192 #define DAYS (24 * HOURS)
1193 #define MONTHS (30 * DAYS)
1194 #define YEARS (12 * MONTHS)
1195 if (time
>= YEARS
) {
1196 year
= time
/ YEARS
;
1197 time
-= year
* YEARS
;
1199 rv
= snprintf(buf
, buflen
, "%u year(s), ", year
);
1203 if (time
>= MONTHS
) {
1204 month
= time
/ MONTHS
;
1205 time
-= month
* MONTHS
;
1207 rv
= snprintf(buf
, buflen
, "%u month(s), ", month
);
1215 rv
= snprintf(buf
, buflen
, "%u day(s), ", day
);
1219 if (time
>= HOURS
) {
1220 hour
= time
/ HOURS
;
1221 time
-= hour
* HOURS
;
1223 rv
= snprintf(buf
, buflen
, "%u hour(s), ", hour
);
1227 if (time
>= MINUTES
) {
1228 minute
= time
/ MINUTES
;
1229 time
-= minute
* MINUTES
;
1231 rv
= snprintf(buf
, buflen
, "%u minute(s), ", minute
);
1235 second
= time
% MINUTES
;
1236 snprintf(buf
, buflen
, "%u second(s)", second
);
1239 const char *bs_to_string(const struct bfd_session
*bs
)
1241 static char buf
[256];
1242 char addr_buf
[INET6_ADDRSTRLEN
];
1244 bool is_mhop
= BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
);
1246 pos
= snprintf(buf
, sizeof(buf
), "mhop:%s", is_mhop
? "yes" : "no");
1247 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " peer:%s",
1248 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
,
1250 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " local:%s",
1251 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
,
1253 if (bs
->key
.vrfname
[0])
1254 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " vrf:%s",
1256 if (bs
->key
.ifname
[0])
1257 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " ifname:%s",
1265 int bs_observer_add(struct bfd_session
*bs
)
1267 struct bfd_session_observer
*bso
;
1269 bso
= XCALLOC(MTYPE_BFDD_SESSION_OBSERVER
, sizeof(*bso
));
1271 bso
->bso_addr
.family
= bs
->key
.family
;
1272 memcpy(&bso
->bso_addr
.u
.prefix
, &bs
->key
.local
,
1273 sizeof(bs
->key
.local
));
1275 TAILQ_INSERT_TAIL(&bglobal
.bg_obslist
, bso
, bso_entry
);
1280 void bs_observer_del(struct bfd_session_observer
*bso
)
1282 TAILQ_REMOVE(&bglobal
.bg_obslist
, bso
, bso_entry
);
1283 XFREE(MTYPE_BFDD_SESSION_OBSERVER
, bso
);
1286 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
1288 memset(bpc
, 0, sizeof(*bpc
));
1290 bpc
->bpc_ipv4
= (bs
->key
.family
== AF_INET
);
1291 bpc
->bpc_mhop
= bs
->key
.mhop
;
1293 switch (bs
->key
.family
) {
1295 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET
;
1296 memcpy(&bpc
->bpc_peer
.sa_sin
.sin_addr
, &bs
->key
.peer
,
1297 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
1299 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
))) {
1300 bpc
->bpc_local
.sa_sin
.sin_family
= AF_INET6
;
1301 memcpy(&bpc
->bpc_local
.sa_sin
.sin_addr
, &bs
->key
.local
,
1302 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
1307 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET6
;
1308 memcpy(&bpc
->bpc_peer
.sa_sin6
.sin6_addr
, &bs
->key
.peer
,
1309 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
1311 bpc
->bpc_local
.sa_sin6
.sin6_family
= AF_INET6
;
1312 memcpy(&bpc
->bpc_local
.sa_sin6
.sin6_addr
, &bs
->key
.local
,
1313 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
1317 if (bs
->key
.ifname
[0]) {
1318 bpc
->bpc_has_localif
= true;
1319 strlcpy(bpc
->bpc_localif
, bs
->key
.ifname
,
1320 sizeof(bpc
->bpc_localif
));
1323 if (bs
->key
.vrfname
[0]) {
1324 bpc
->bpc_has_vrfname
= true;
1325 strlcpy(bpc
->bpc_vrfname
, bs
->key
.vrfname
,
1326 sizeof(bpc
->bpc_vrfname
));
1332 * BFD hash data structures to find sessions.
1334 static struct hash
*bfd_id_hash
;
1335 static struct hash
*bfd_key_hash
;
1337 static unsigned int bfd_id_hash_do(const void *p
);
1338 static unsigned int bfd_key_hash_do(const void *p
);
1340 static void _bfd_free(struct hash_bucket
*hb
,
1341 void *arg
__attribute__((__unused__
)));
1343 /* BFD hash for our discriminator. */
1344 static unsigned int bfd_id_hash_do(const void *p
)
1346 const struct bfd_session
*bs
= p
;
1348 return jhash_1word(bs
->discrs
.my_discr
, 0);
1351 static bool bfd_id_hash_cmp(const void *n1
, const void *n2
)
1353 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1355 return bs1
->discrs
.my_discr
== bs2
->discrs
.my_discr
;
1358 /* BFD hash for single hop. */
1359 static unsigned int bfd_key_hash_do(const void *p
)
1361 const struct bfd_session
*bs
= p
;
1363 return jhash(&bs
->key
, sizeof(bs
->key
), 0);
1366 static bool bfd_key_hash_cmp(const void *n1
, const void *n2
)
1368 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1370 return memcmp(&bs1
->key
, &bs2
->key
, sizeof(bs1
->key
)) == 0;
1375 * Hash public interface / exported functions.
1378 /* Lookup functions. */
1379 struct bfd_session
*bfd_id_lookup(uint32_t id
)
1381 struct bfd_session bs
;
1383 bs
.discrs
.my_discr
= id
;
1385 return hash_lookup(bfd_id_hash
, &bs
);
1388 struct bfd_key_walk_partial_lookup
{
1389 struct bfd_session
*given
;
1390 struct bfd_session
*result
;
1393 /* ignore some parameters */
1394 static int bfd_key_lookup_ignore_partial_walker(struct hash_bucket
*b
,
1397 struct bfd_key_walk_partial_lookup
*ctx
=
1398 (struct bfd_key_walk_partial_lookup
*)data
;
1399 struct bfd_session
*given
= ctx
->given
;
1400 struct bfd_session
*parsed
= b
->data
;
1402 if (given
->key
.family
!= parsed
->key
.family
)
1403 return HASHWALK_CONTINUE
;
1404 if (given
->key
.mhop
!= parsed
->key
.mhop
)
1405 return HASHWALK_CONTINUE
;
1406 if (memcmp(&given
->key
.peer
, &parsed
->key
.peer
,
1407 sizeof(struct in6_addr
)))
1408 return HASHWALK_CONTINUE
;
1409 if (memcmp(given
->key
.vrfname
, parsed
->key
.vrfname
, MAXNAMELEN
))
1410 return HASHWALK_CONTINUE
;
1411 ctx
->result
= parsed
;
1412 /* ignore localaddr or interface */
1413 return HASHWALK_ABORT
;
1416 struct bfd_session
*bfd_key_lookup(struct bfd_key key
)
1418 struct bfd_session bs
, *bsp
;
1419 struct bfd_key_walk_partial_lookup ctx
;
1420 char peer_buf
[INET6_ADDRSTRLEN
];
1423 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1427 inet_ntop(bs
.key
.family
, &bs
.key
.peer
, peer_buf
,
1429 /* Handle cases where local-address is optional. */
1430 if (bs
.key
.family
== AF_INET
) {
1431 memset(&bs
.key
.local
, 0, sizeof(bs
.key
.local
));
1432 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1434 char addr_buf
[INET6_ADDRSTRLEN
];
1436 inet_ntop(bs
.key
.family
, &key
.local
, addr_buf
,
1438 log_debug(" peer %s found, but loc-addr %s ignored",
1439 peer_buf
, addr_buf
);
1445 /* Handle cases where ifname is optional. */
1446 if (bs
.key
.ifname
[0]) {
1447 memset(bs
.key
.ifname
, 0, sizeof(bs
.key
.ifname
));
1448 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1450 log_debug(" peer %s found, but ifp %s ignored",
1451 peer_buf
, key
.ifname
);
1456 /* Handle cases where local-address and ifname are optional. */
1457 if (bs
.key
.family
== AF_INET
) {
1458 memset(&bs
.key
.local
, 0, sizeof(bs
.key
.local
));
1459 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1461 char addr_buf
[INET6_ADDRSTRLEN
];
1463 inet_ntop(bs
.key
.family
, &bs
.key
.local
, addr_buf
,
1465 log_debug(" peer %s found, but ifp %s"
1466 " and loc-addr %s ignored",
1467 peer_buf
, key
.ifname
,
1474 /* Handle case where a context more complex ctx is present.
1475 * input has no iface nor local-address, but a context may
1480 hash_walk(bfd_key_hash
,
1481 &bfd_key_lookup_ignore_partial_walker
,
1486 log_debug(" peer %s found, but ifp"
1487 " and/or loc-addr params ignored", peer_buf
);
1495 * Delete functions searches and remove the item from the hash and
1496 * returns a pointer to the removed item data. If the item was not found
1497 * then it returns NULL.
1499 * The data stored inside the hash is not free()ed, so you must do it
1500 * manually after getting the pointer back.
1502 struct bfd_session
*bfd_id_delete(uint32_t id
)
1504 struct bfd_session bs
;
1506 bs
.discrs
.my_discr
= id
;
1508 return hash_release(bfd_id_hash
, &bs
);
1511 struct bfd_session
*bfd_key_delete(struct bfd_key key
)
1513 struct bfd_session bs
, *bsp
;
1516 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1517 if (bsp
== NULL
&& key
.ifname
[0]) {
1518 memset(bs
.key
.ifname
, 0, sizeof(bs
.key
.ifname
));
1519 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1522 return hash_release(bfd_key_hash
, bsp
);
1525 /* Iteration functions. */
1526 void bfd_id_iterate(hash_iter_func hif
, void *arg
)
1528 hash_iterate(bfd_id_hash
, hif
, arg
);
1531 void bfd_key_iterate(hash_iter_func hif
, void *arg
)
1533 hash_iterate(bfd_key_hash
, hif
, arg
);
1539 * Inserts session into hash and returns `true` on success, otherwise
1542 bool bfd_id_insert(struct bfd_session
*bs
)
1544 return (hash_get(bfd_id_hash
, bs
, hash_alloc_intern
) == bs
);
1547 bool bfd_key_insert(struct bfd_session
*bs
)
1549 return (hash_get(bfd_key_hash
, bs
, hash_alloc_intern
) == bs
);
1552 void bfd_initialize(void)
1554 bfd_id_hash
= hash_create(bfd_id_hash_do
, bfd_id_hash_cmp
,
1555 "BFD session discriminator hash");
1556 bfd_key_hash
= hash_create(bfd_key_hash_do
, bfd_key_hash_cmp
,
1557 "BFD session hash");
1560 static void _bfd_free(struct hash_bucket
*hb
,
1561 void *arg
__attribute__((__unused__
)))
1563 struct bfd_session
*bs
= hb
->data
;
1565 bfd_session_free(bs
);
1568 void bfd_shutdown(void)
1571 * Close and free all BFD sessions.
1573 * _bfd_free() will call bfd_session_free() which will take care
1574 * of removing the session from all hashes, so we just run an
1575 * assert() here to make sure it really happened.
1577 bfd_id_iterate(_bfd_free
, NULL
);
1578 assert(bfd_key_hash
->count
== 0);
1580 /* Now free the hashes themselves. */
1581 hash_free(bfd_id_hash
);
1582 hash_free(bfd_key_hash
);
1585 struct bfd_session_iterator
{
1588 const struct bfd_session
*bsi_bs
;
1591 static int _bfd_session_next(struct hash_bucket
*hb
, void *arg
)
1593 struct bfd_session_iterator
*bsi
= arg
;
1594 struct bfd_session
*bs
= hb
->data
;
1596 /* Previous entry signaled stop. */
1597 if (bsi
->bsi_stop
== 1) {
1598 /* Match the single/multi hop sessions. */
1599 if (bs
->key
.mhop
!= bsi
->bsi_mhop
)
1600 return HASHWALK_CONTINUE
;
1603 return HASHWALK_ABORT
;
1606 /* We found the current item, stop in the next one. */
1607 if (bsi
->bsi_bs
== hb
->data
) {
1609 /* Set entry to NULL to signal end of list. */
1611 } else if (bsi
->bsi_bs
== NULL
&& bsi
->bsi_mhop
== bs
->key
.mhop
) {
1612 /* We want the first list item. */
1614 bsi
->bsi_bs
= hb
->data
;
1615 return HASHWALK_ABORT
;
1618 return HASHWALK_CONTINUE
;
1622 * bfd_session_next: uses the current session to find the next.
1624 * `bs` might point to NULL to get the first item of the data structure.
1626 const struct bfd_session
*bfd_session_next(const struct bfd_session
*bs
,
1629 struct bfd_session_iterator bsi
;
1633 bsi
.bsi_mhop
= mhop
;
1634 hash_walk(bfd_key_hash
, _bfd_session_next
, &bsi
);
1635 if (bsi
.bsi_stop
== 0)
1641 static void _bfd_session_remove_manual(struct hash_bucket
*hb
,
1642 void *arg
__attribute__((__unused__
)))
1644 struct bfd_session
*bs
= hb
->data
;
1646 /* Delete only manually configured sessions. */
1647 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
) == 0)
1651 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_CONFIG
);
1653 /* Don't delete sessions still in use. */
1654 if (bs
->refcount
!= 0)
1657 bfd_session_free(bs
);
1661 * bfd_sessions_remove_manual: remove all manually configured sessions.
1663 * NOTE: this function doesn't remove automatically created sessions.
1665 void bfd_sessions_remove_manual(void)
1667 hash_iterate(bfd_key_hash
, _bfd_session_remove_manual
, NULL
);
1671 * VRF related functions.
1673 static int bfd_vrf_new(struct vrf
*vrf
)
1675 log_debug("VRF Created: %s(%u)", vrf
->name
, vrf
->vrf_id
);
1679 static int bfd_vrf_delete(struct vrf
*vrf
)
1681 log_debug("VRF Deletion: %s(%u)", vrf
->name
, vrf
->vrf_id
);
1685 static int bfd_vrf_update(struct vrf
*vrf
)
1687 if (!vrf_is_enabled(vrf
))
1689 log_debug("VRF update: %s(%u)", vrf
->name
, vrf
->vrf_id
);
1690 /* a different name is given; update bfd list */
1691 bfdd_sessions_enable_vrf(vrf
);
1695 static int bfd_vrf_enable(struct vrf
*vrf
)
1697 struct bfd_vrf_global
*bvrf
;
1699 /* a different name */
1701 bvrf
= XCALLOC(MTYPE_BFDD_VRF
, sizeof(struct bfd_vrf_global
));
1703 vrf
->info
= (void *)bvrf
;
1706 log_debug("VRF enable add %s id %u", vrf
->name
, vrf
->vrf_id
);
1707 if (vrf
->vrf_id
== VRF_DEFAULT
||
1708 vrf_get_backend() == VRF_BACKEND_NETNS
) {
1710 bvrf
->bg_shop
= bp_udp_shop(vrf
);
1712 bvrf
->bg_mhop
= bp_udp_mhop(vrf
);
1713 if (!bvrf
->bg_shop6
)
1714 bvrf
->bg_shop6
= bp_udp6_shop(vrf
);
1715 if (!bvrf
->bg_mhop6
)
1716 bvrf
->bg_mhop6
= bp_udp6_mhop(vrf
);
1718 bvrf
->bg_echo
= bp_echo_socket(vrf
);
1719 if (!bvrf
->bg_echov6
)
1720 bvrf
->bg_echov6
= bp_echov6_socket(vrf
);
1722 /* Add descriptors to the event loop. */
1723 if (!bvrf
->bg_ev
[0])
1724 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_shop
,
1726 if (!bvrf
->bg_ev
[1])
1727 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_mhop
,
1729 if (!bvrf
->bg_ev
[2])
1730 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_shop6
,
1732 if (!bvrf
->bg_ev
[3])
1733 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_mhop6
,
1735 if (!bvrf
->bg_ev
[4])
1736 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_echo
,
1738 if (!bvrf
->bg_ev
[5])
1739 thread_add_read(master
, bfd_recv_cb
, bvrf
, bvrf
->bg_echov6
,
1742 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
1743 bfdd_zclient_register(vrf
->vrf_id
);
1744 bfdd_sessions_enable_vrf(vrf
);
1749 static int bfd_vrf_disable(struct vrf
*vrf
)
1751 struct bfd_vrf_global
*bvrf
;
1757 if (vrf
->vrf_id
!= VRF_DEFAULT
) {
1758 bfdd_sessions_disable_vrf(vrf
);
1759 bfdd_zclient_unregister(vrf
->vrf_id
);
1762 log_debug("VRF disable %s id %d", vrf
->name
, vrf
->vrf_id
);
1764 /* Disable read/write poll triggering. */
1765 THREAD_OFF(bvrf
->bg_ev
[0]);
1766 THREAD_OFF(bvrf
->bg_ev
[1]);
1767 THREAD_OFF(bvrf
->bg_ev
[2]);
1768 THREAD_OFF(bvrf
->bg_ev
[3]);
1769 THREAD_OFF(bvrf
->bg_ev
[4]);
1770 THREAD_OFF(bvrf
->bg_ev
[5]);
1772 /* Close all descriptors. */
1773 socket_close(&bvrf
->bg_echo
);
1774 socket_close(&bvrf
->bg_shop
);
1775 socket_close(&bvrf
->bg_mhop
);
1776 socket_close(&bvrf
->bg_shop6
);
1777 socket_close(&bvrf
->bg_mhop6
);
1778 socket_close(&bvrf
->bg_echo
);
1779 socket_close(&bvrf
->bg_echov6
);
1782 XFREE(MTYPE_BFDD_VRF
, bvrf
);
1788 void bfd_vrf_init(void)
1790 vrf_init(bfd_vrf_new
, bfd_vrf_enable
, bfd_vrf_disable
,
1791 bfd_vrf_delete
, bfd_vrf_update
);
1794 void bfd_vrf_terminate(void)
1799 struct bfd_vrf_global
*bfd_vrf_look_by_session(struct bfd_session
*bfd
)
1803 if (!vrf_is_backend_netns()) {
1804 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
1806 return (struct bfd_vrf_global
*)vrf
->info
;
1813 return bfd
->vrf
->info
;
1816 void bfd_session_update_vrf_name(struct bfd_session
*bs
, struct vrf
*vrf
)
1821 hash_release(bfd_key_hash
, bs
);
1823 * HACK: Change the BFD VRF in the running configuration directly,
1824 * bypassing the northbound layer. This is necessary to avoid deleting
1825 * the BFD and readding it in the new VRF, which would have
1826 * several implications.
1828 if (yang_module_find("frr-bfdd") && bs
->key
.vrfname
[0]) {
1829 struct lyd_node
*bfd_dnode
;
1830 char xpath
[XPATH_MAXLEN
], xpath_srcaddr
[XPATH_MAXLEN
+ 32];
1831 char addr_buf
[INET6_ADDRSTRLEN
];
1836 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
, sizeof(addr_buf
));
1837 snprintf(xpath_srcaddr
, sizeof(xpath_srcaddr
), "[source-addr='%s']",
1840 xpath_srcaddr
[0] = 0;
1841 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
, sizeof(addr_buf
));
1842 slen
= snprintf(xpath
, sizeof(xpath
),
1843 "/frr-bfdd:bfdd/bfd/sessions/%s%s[dest-addr='%s']",
1844 bs
->key
.mhop
? "multi-hop" : "single-hop", xpath_srcaddr
,
1846 if (bs
->key
.ifname
[0])
1847 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
1848 "[interface='%s']", bs
->key
.ifname
);
1850 slen
+= snprintf(xpath
+ slen
, sizeof(xpath
) - slen
,
1852 snprintf(xpath
+ slen
, sizeof(xpath
) - slen
, "[vrf='%s']/vrf",
1855 bfd_dnode
= yang_dnode_get(running_config
->dnode
, xpath
,
1858 yang_dnode_change_leaf(bfd_dnode
, vrf
->name
);
1859 running_config
->version
++;
1862 memset(bs
->key
.vrfname
, 0, sizeof(bs
->key
.vrfname
));
1863 strlcpy(bs
->key
.vrfname
, vrf
->name
, sizeof(bs
->key
.vrfname
));
1864 hash_get(bfd_key_hash
, bs
, hash_alloc_intern
);
1867 unsigned long bfd_get_session_count(void)
1869 return bfd_key_hash
->count
;