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_QOBJ_TYPE(bfd_session
);
39 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
40 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
43 static uint32_t ptm_bfd_gen_ID(void);
44 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
);
45 static void bfd_session_free(struct bfd_session
*bs
);
46 static struct bfd_session
*bfd_session_new(void);
47 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
49 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
50 static const char *get_diag_str(int diag
);
52 static void bs_admin_down_handler(struct bfd_session
*bs
, int nstate
);
53 static void bs_down_handler(struct bfd_session
*bs
, int nstate
);
54 static void bs_init_handler(struct bfd_session
*bs
, int nstate
);
55 static void bs_up_handler(struct bfd_session
*bs
, int nstate
);
57 /* Zeroed array with the size of an IPv6 address. */
58 struct in6_addr zero_addr
;
63 void gen_bfd_key(struct bfd_key
*key
, struct sockaddr_any
*peer
,
64 struct sockaddr_any
*local
, bool mhop
, const char *ifname
,
67 memset(key
, 0, sizeof(*key
));
69 switch (peer
->sa_sin
.sin_family
) {
71 key
->family
= AF_INET
;
72 memcpy(&key
->peer
, &peer
->sa_sin
.sin_addr
,
73 sizeof(peer
->sa_sin
.sin_addr
));
74 memcpy(&key
->local
, &local
->sa_sin
.sin_addr
,
75 sizeof(local
->sa_sin
.sin_addr
));
78 key
->family
= AF_INET6
;
79 memcpy(&key
->peer
, &peer
->sa_sin6
.sin6_addr
,
80 sizeof(peer
->sa_sin6
.sin6_addr
));
81 memcpy(&key
->local
, &local
->sa_sin6
.sin6_addr
,
82 sizeof(local
->sa_sin6
.sin6_addr
));
87 if (ifname
&& ifname
[0])
88 strlcpy(key
->ifname
, ifname
, sizeof(key
->ifname
));
89 if (vrfname
&& vrfname
[0])
90 strlcpy(key
->vrfname
, vrfname
, 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
.ifname
[0]) {
132 ifp
= if_lookup_by_name_all_vrf(bs
->key
.ifname
);
135 "session-enable: specified interface doesn't exists.");
139 vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
142 "session-enable: specified VRF doesn't exists.");
147 if (bs
->key
.vrfname
[0]) {
148 vrf
= vrf_lookup_by_name(bs
->key
.vrfname
);
151 "session-enable: specified VRF doesn't exists.");
156 /* Assign interface/VRF pointers. */
159 bs
->vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
161 if (bs
->key
.ifname
[0]
162 && BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
) == 0)
165 /* Sanity check: don't leak open sockets. */
166 if (bs
->sock
!= -1) {
167 zlog_debug("session-enable: previous socket open");
173 * Get socket for transmitting control packets. Note that if we
174 * could use the destination port (3784) for the source
175 * port we wouldn't need a socket per session.
177 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_IPV6
) == 0) {
178 psock
= bp_peer_socket(bs
);
182 psock
= bp_peer_socketv6(bs
);
188 * We've got a valid socket, lets start the timers and the
192 bfd_recvtimer_update(bs
);
193 ptm_bfd_start_xmt_timer(bs
, false);
199 * Disabled a running BFD session.
201 * A session is disabled when the specified interface/VRF gets removed
202 * (e.g. virtual interfaces).
204 void bfd_session_disable(struct bfd_session
*bs
)
206 /* Free up socket resources. */
207 if (bs
->sock
!= -1) {
212 /* Disable all timers. */
213 bfd_recvtimer_delete(bs
);
214 bfd_echo_recvtimer_delete(bs
);
215 bfd_xmttimer_delete(bs
);
216 bfd_echo_xmttimer_delete(bs
);
219 static uint32_t ptm_bfd_gen_ID(void)
224 * RFC 5880, Section 6.8.1. recommends that we should generate
225 * random session identification numbers.
228 session_id
= ((random() << 16) & 0xFFFF0000)
229 | (random() & 0x0000FFFF);
230 } while (session_id
== 0 || bfd_id_lookup(session_id
) != NULL
);
235 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
)
237 uint64_t jitter
, xmt_TO
;
240 xmt_TO
= is_echo
? bfd
->echo_xmt_TO
: bfd
->xmt_TO
;
243 * From section 6.5.2: trasmit interval should be randomly jittered
245 * 75% and 100% of nominal value, unless detect_mult is 1, then should
247 * between 75% and 90%.
249 maxpercent
= (bfd
->detect_mult
== 1) ? 16 : 26;
250 jitter
= (xmt_TO
* (75 + (random() % maxpercent
))) / 100;
251 /* XXX remove that division above */
254 bfd_echo_xmttimer_update(bfd
, jitter
);
256 bfd_xmttimer_update(bfd
, jitter
);
259 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
)
261 /* Send the scheduled echo packet */
262 ptm_bfd_echo_snd(bfd
);
264 /* Restart the timer for next time */
265 ptm_bfd_start_xmt_timer(bfd
, true);
268 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
)
270 /* Send the scheduled control packet */
271 ptm_bfd_snd(bfd
, fbit
);
273 /* Restart the timer for next time */
274 ptm_bfd_start_xmt_timer(bfd
, false);
277 void ptm_bfd_echo_stop(struct bfd_session
*bfd
)
279 bfd
->echo_xmt_TO
= 0;
280 bfd
->echo_detect_TO
= 0;
281 BFD_UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
);
283 bfd_echo_xmttimer_delete(bfd
);
284 bfd_echo_recvtimer_delete(bfd
);
287 void ptm_bfd_echo_start(struct bfd_session
*bfd
)
289 bfd
->echo_detect_TO
= (bfd
->remote_detect_mult
* bfd
->echo_xmt_TO
);
290 if (bfd
->echo_detect_TO
> 0)
291 ptm_bfd_echo_xmt_TO(bfd
);
294 void ptm_bfd_ses_up(struct bfd_session
*bfd
)
296 int old_state
= bfd
->ses_state
;
299 bfd
->ses_state
= PTM_BFD_UP
;
300 monotime(&bfd
->uptime
);
302 /* Connection is up, lets negotiate timers. */
303 bfd_set_polling(bfd
);
305 /* Start sending control packets with poll bit immediately. */
310 if (old_state
!= bfd
->ses_state
) {
311 bfd
->stats
.session_up
++;
312 log_info("state-change: [%s] %s -> %s", bs_to_string(bfd
),
313 state_list
[old_state
].str
,
314 state_list
[bfd
->ses_state
].str
);
318 void ptm_bfd_ses_dn(struct bfd_session
*bfd
, uint8_t diag
)
320 int old_state
= bfd
->ses_state
;
322 bfd
->local_diag
= diag
;
323 bfd
->discrs
.remote_discr
= 0;
324 bfd
->ses_state
= PTM_BFD_DOWN
;
326 bfd
->demand_mode
= 0;
327 monotime(&bfd
->downtime
);
331 /* Slow down the control packets, the connection is down. */
332 bs_set_slow_timers(bfd
);
334 /* only signal clients when going from up->down state */
335 if (old_state
== PTM_BFD_UP
)
338 /* Stop echo packet transmission if they are active */
339 if (BFD_CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
340 ptm_bfd_echo_stop(bfd
);
342 if (old_state
!= bfd
->ses_state
) {
343 bfd
->stats
.session_down
++;
344 log_info("state-change: [%s] %s -> %s reason:%s",
345 bs_to_string(bfd
), state_list
[old_state
].str
,
346 state_list
[bfd
->ses_state
].str
,
347 get_diag_str(bfd
->local_diag
));
351 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
354 struct bfd_session
*bs
;
356 bs
= bfd_id_lookup(ldisc
);
360 switch (bs
->key
.family
) {
362 if (memcmp(&sa
->sa_sin
.sin_addr
, &bs
->key
.peer
,
363 sizeof(sa
->sa_sin
.sin_addr
)))
367 if (memcmp(&sa
->sa_sin6
.sin6_addr
, &bs
->key
.peer
,
368 sizeof(sa
->sa_sin6
.sin6_addr
)))
376 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
,
377 struct sockaddr_any
*peer
,
378 struct sockaddr_any
*local
,
379 ifindex_t ifindex
, vrf_id_t vrfid
,
382 struct interface
*ifp
;
386 /* Find our session using the ID signaled by the remote end. */
387 if (cp
->discrs
.remote_discr
)
388 return bfd_find_disc(peer
, ntohl(cp
->discrs
.remote_discr
));
390 /* Search for session without using discriminator. */
391 ifp
= if_lookup_by_index(ifindex
, vrfid
);
392 if (vrfid
== VRF_DEFAULT
) {
394 * Don't use the default vrf, otherwise we won't find
395 * sessions that doesn't specify it.
399 vrf
= vrf_lookup_by_id(vrfid
);
401 gen_bfd_key(&key
, peer
, local
, is_mhop
, ifp
? ifp
->name
: NULL
,
402 vrf
? vrf
->name
: NULL
);
404 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
405 return bfd_key_lookup(key
);
408 int bfd_xmt_cb(struct thread
*t
)
410 struct bfd_session
*bs
= THREAD_ARG(t
);
412 ptm_bfd_xmt_TO(bs
, 0);
417 int bfd_echo_xmt_cb(struct thread
*t
)
419 struct bfd_session
*bs
= THREAD_ARG(t
);
421 if (bs
->echo_xmt_TO
> 0)
422 ptm_bfd_echo_xmt_TO(bs
);
427 /* Was ptm_bfd_detect_TO() */
428 int bfd_recvtimer_cb(struct thread
*t
)
430 struct bfd_session
*bs
= THREAD_ARG(t
);
432 switch (bs
->ses_state
) {
435 ptm_bfd_ses_dn(bs
, BD_CONTROL_EXPIRED
);
436 bfd_recvtimer_update(bs
);
440 /* Second detect time expiration, zero remote discr (section
443 bs
->discrs
.remote_discr
= 0;
450 /* Was ptm_bfd_echo_detect_TO() */
451 int bfd_echo_recvtimer_cb(struct thread
*t
)
453 struct bfd_session
*bs
= THREAD_ARG(t
);
455 switch (bs
->ses_state
) {
458 ptm_bfd_ses_dn(bs
, BD_ECHO_FAILED
);
465 static struct bfd_session
*bfd_session_new(void)
467 struct bfd_session
*bs
;
469 bs
= XCALLOC(MTYPE_BFDD_CONFIG
, sizeof(*bs
));
471 QOBJ_REG(bs
, bfd_session
);
473 bs
->timers
.desired_min_tx
= BFD_DEFDESIREDMINTX
;
474 bs
->timers
.required_min_rx
= BFD_DEFREQUIREDMINRX
;
475 bs
->timers
.required_min_echo
= BFD_DEF_REQ_MIN_ECHO
;
476 bs
->detect_mult
= BFD_DEFDETECTMULT
;
477 bs
->mh_ttl
= BFD_DEF_MHOP_TTL
;
478 bs
->ses_state
= PTM_BFD_DOWN
;
480 /* Initiate connection with slow timers. */
481 bs_set_slow_timers(bs
);
483 /* Initiate remote settings as well. */
484 bs
->remote_timers
= bs
->cur_timers
;
485 bs
->remote_detect_mult
= BFD_DEFDETECTMULT
;
488 monotime(&bs
->uptime
);
489 bs
->downtime
= bs
->uptime
;
494 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
)
496 /* New label treatment:
497 * - Check if the label is taken;
498 * - Try to allocate the memory for it and register;
500 if (bs
->pl
== NULL
) {
501 if (pl_find(nlabel
) != NULL
) {
502 /* Someone is already using it. */
506 if (pl_new(nlabel
, bs
) == NULL
)
513 * Test label change consistency:
514 * - Do nothing if it's the same label;
515 * - Check if the future label is already taken;
518 if (strcmp(nlabel
, bs
->pl
->pl_label
) == 0)
520 if (pl_find(nlabel
) != NULL
)
523 strlcpy(bs
->pl
->pl_label
, nlabel
, sizeof(bs
->pl
->pl_label
));
527 static void _bfd_session_update(struct bfd_session
*bs
,
528 struct bfd_peer_cfg
*bpc
)
531 /* Check if echo mode is already active. */
532 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
535 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
537 /* Activate/update echo receive timeout timer. */
538 bs_echo_timer_handler(bs
);
540 /* Check if echo mode is already disabled. */
541 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
544 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
545 ptm_bfd_echo_stop(bs
);
549 if (bpc
->bpc_has_txinterval
)
550 bs
->timers
.desired_min_tx
= bpc
->bpc_txinterval
* 1000;
552 if (bpc
->bpc_has_recvinterval
)
553 bs
->timers
.required_min_rx
= bpc
->bpc_recvinterval
* 1000;
555 if (bpc
->bpc_has_detectmultiplier
)
556 bs
->detect_mult
= bpc
->bpc_detectmultiplier
;
558 if (bpc
->bpc_has_echointerval
)
559 bs
->timers
.required_min_echo
= bpc
->bpc_echointerval
* 1000;
561 if (bpc
->bpc_has_label
)
562 bfd_session_update_label(bs
, bpc
->bpc_label
);
564 if (bpc
->bpc_shutdown
) {
565 /* Check if already shutdown. */
566 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
569 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
571 /* Disable all events. */
572 bfd_recvtimer_delete(bs
);
573 bfd_echo_recvtimer_delete(bs
);
574 bfd_xmttimer_delete(bs
);
575 bfd_echo_xmttimer_delete(bs
);
577 /* Change and notify state change. */
578 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
581 /* Don't try to send packets with a disabled session. */
585 /* Check if already working. */
586 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
589 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
591 /* Change and notify state change. */
592 bs
->ses_state
= PTM_BFD_DOWN
;
595 /* Enable all timers. */
596 bfd_recvtimer_update(bs
);
597 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
601 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
603 /* User didn't want to update, return failure. */
604 if (bpc
->bpc_createonly
)
607 _bfd_session_update(bs
, bpc
);
609 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
614 static void bfd_session_free(struct bfd_session
*bs
)
616 struct bfd_session_observer
*bso
;
618 bfd_session_disable(bs
);
620 bfd_key_delete(bs
->key
);
621 bfd_id_delete(bs
->discrs
.my_discr
);
623 /* Remove observer if any. */
624 TAILQ_FOREACH(bso
, &bglobal
.bg_obslist
, bso_entry
) {
625 if (bso
->bso_bs
!= bs
)
631 bs_observer_del(bso
);
636 XFREE(MTYPE_BFDD_CONFIG
, bs
);
639 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
)
641 struct bfd_session
*bfd
, *l_bfd
;
643 /* check to see if this needs a new session */
644 l_bfd
= bs_peer_find(bpc
);
646 /* Requesting a duplicated peer means update configuration. */
647 if (bfd_session_update(l_bfd
, bpc
) == 0)
653 /* Get BFD session storage with its defaults. */
654 bfd
= bfd_session_new();
656 log_error("session-new: allocation failed");
661 * Store interface/VRF name in case we need to delay session
662 * start. See `bfd_session_enable` for more information.
664 if (bpc
->bpc_has_localif
)
665 strlcpy(bfd
->key
.ifname
, bpc
->bpc_localif
,
666 sizeof(bfd
->key
.ifname
));
668 if (bpc
->bpc_has_vrfname
)
669 strlcpy(bfd
->key
.vrfname
, bpc
->bpc_vrfname
,
670 sizeof(bfd
->key
.vrfname
));
672 /* Copy remaining data. */
673 if (bpc
->bpc_ipv4
== false)
674 BFD_SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
);
676 bfd
->key
.family
= (bpc
->bpc_ipv4
) ? AF_INET
: AF_INET6
;
677 switch (bfd
->key
.family
) {
679 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin
.sin_addr
,
680 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
681 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin
.sin_addr
,
682 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
686 memcpy(&bfd
->key
.peer
, &bpc
->bpc_peer
.sa_sin6
.sin6_addr
,
687 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
688 memcpy(&bfd
->key
.local
, &bpc
->bpc_local
.sa_sin6
.sin6_addr
,
689 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
698 BFD_SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MH
);
700 bfd
->key
.mhop
= bpc
->bpc_mhop
;
702 /* Registrate session into data structures. */
704 bfd
->discrs
.my_discr
= ptm_bfd_gen_ID();
707 /* Try to enable session and schedule for packet receive/send. */
708 if (bfd_session_enable(bfd
) == -1) {
709 /* Unrecoverable failure, remove the session/peer. */
710 bfd_session_free(bfd
);
714 /* Add observer if we have moving parts. */
715 if (bfd
->key
.ifname
[0] || bfd
->key
.vrfname
[0] || bfd
->sock
== -1)
716 bs_observer_add(bfd
);
718 /* Apply other configurations. */
719 _bfd_session_update(bfd
, bpc
);
721 log_info("session-new: %s", bs_to_string(bfd
));
723 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bfd
);
728 int ptm_bfd_ses_del(struct bfd_peer_cfg
*bpc
)
730 struct bfd_session
*bs
;
732 /* Find session and call free(). */
733 bs
= bs_peer_find(bpc
);
737 /* This pointer is being referenced, don't let it be deleted. */
738 if (bs
->refcount
> 0) {
739 log_error("session-delete: refcount failure: %" PRIu64
745 log_info("session-delete: %s", bs_to_string(bs
));
747 control_notify_config(BCM_NOTIFY_CONFIG_DELETE
, bs
);
749 bfd_session_free(bs
);
754 void bfd_set_polling(struct bfd_session
*bs
)
757 * Start polling procedure: the only timers that require polling
758 * to change value without losing connection are:
760 * - Desired minimum transmission interval;
761 * - Required minimum receive interval;
763 * RFC 5880, Section 6.8.3.
769 * bs_<state>_handler() functions implement the BFD state machine
770 * transition mechanism. `<state>` is the current session state and
771 * the parameter `nstate` is the peer new state.
773 static void bs_admin_down_handler(struct bfd_session
*bs
774 __attribute__((__unused__
)),
775 int nstate
__attribute__((__unused__
)))
778 * We are administratively down, there is no state machine
783 static void bs_down_handler(struct bfd_session
*bs
, int nstate
)
786 case PTM_BFD_ADM_DOWN
:
788 * Remote peer doesn't want to talk, so lets keep the
792 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
797 * Remote peer agreed that the path is down, lets try to
800 bs
->ses_state
= PTM_BFD_INIT
;
805 * Remote peer told us his path is up, lets turn
806 * activate the session.
812 log_debug("state-change: unhandled neighbor state: %d", nstate
);
817 static void bs_init_handler(struct bfd_session
*bs
, int nstate
)
820 case PTM_BFD_ADM_DOWN
:
822 * Remote peer doesn't want to talk, so lets make the
825 bs
->ses_state
= PTM_BFD_DOWN
;
829 /* Remote peer hasn't moved to first stage yet. */
834 /* We agreed on the settings and the path is up. */
839 log_debug("state-change: unhandled neighbor state: %d", nstate
);
844 static void bs_up_handler(struct bfd_session
*bs
, int nstate
)
847 case PTM_BFD_ADM_DOWN
:
849 /* Peer lost or asked to shutdown connection. */
850 ptm_bfd_ses_dn(bs
, BD_NEIGHBOR_DOWN
);
855 /* Path is up and working. */
859 log_debug("state-change: unhandled neighbor state: %d", nstate
);
864 void bs_state_handler(struct bfd_session
*bs
, int nstate
)
866 switch (bs
->ses_state
) {
867 case PTM_BFD_ADM_DOWN
:
868 bs_admin_down_handler(bs
, nstate
);
871 bs_down_handler(bs
, nstate
);
874 bs_init_handler(bs
, nstate
);
877 bs_up_handler(bs
, nstate
);
881 log_debug("state-change: [%s] is in invalid state: %d",
882 bs_to_string(bs
), nstate
);
888 * Handles echo timer manipulation after updating timer.
890 void bs_echo_timer_handler(struct bfd_session
*bs
)
895 * Before doing any echo handling, check if it is possible to
898 * - Check for `echo-mode` configuration.
899 * - Check that we are not using multi hop (RFC 5883,
901 * - Check that we are already at the up state.
903 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
) == 0
904 || BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)
905 || bs
->ses_state
!= PTM_BFD_UP
)
908 /* Remote peer asked to stop echo. */
909 if (bs
->remote_timers
.required_min_echo
== 0) {
910 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
911 ptm_bfd_echo_stop(bs
);
917 * Calculate the echo transmission timer: we must not send
918 * echo packets faster than the minimum required time
919 * announced by the remote system.
921 * RFC 5880, Section 6.8.9.
923 old_timer
= bs
->echo_xmt_TO
;
924 if (bs
->remote_timers
.required_min_echo
> bs
->timers
.required_min_echo
)
925 bs
->echo_xmt_TO
= bs
->remote_timers
.required_min_echo
;
927 bs
->echo_xmt_TO
= bs
->timers
.required_min_echo
;
929 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
) == 0
930 || old_timer
!= bs
->echo_xmt_TO
)
931 ptm_bfd_echo_start(bs
);
935 * RFC 5880 Section 6.5.
937 * When a BFD control packet with the final bit is received, we must
938 * update the session parameters.
940 void bs_final_handler(struct bfd_session
*bs
)
942 /* Start using our new timers. */
943 bs
->cur_timers
.desired_min_tx
= bs
->timers
.desired_min_tx
;
944 bs
->cur_timers
.required_min_rx
= bs
->timers
.required_min_rx
;
947 * TODO: demand mode. See RFC 5880 Section 6.1.
949 * When using demand mode we must disable the detection timer
950 * for lost control packets.
952 if (bs
->demand_mode
) {
953 /* Notify watchers about changed timers. */
954 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
959 * Calculate detection time based on new timers.
961 * Transmission calculation:
962 * We must respect the RequiredMinRxInterval from the remote
963 * system: if our desired transmission timer is more than the
964 * minimum receive rate, then we must lower it to at least the
965 * minimum receive interval.
967 * RFC 5880, Section 6.8.3.
969 if (bs
->timers
.desired_min_tx
> bs
->remote_timers
.required_min_rx
)
970 bs
->xmt_TO
= bs
->remote_timers
.required_min_rx
;
972 bs
->xmt_TO
= bs
->timers
.desired_min_tx
;
974 /* Apply new transmission timer immediately. */
975 ptm_bfd_start_xmt_timer(bs
, false);
978 * Detection timeout calculation:
979 * The minimum detection timeout is the remote detection
980 * multipler (number of packets to be missed) times the agreed
981 * transmission interval.
983 * RFC 5880, Section 6.8.4.
985 * TODO: support sending/counting more packets inside detection
988 if (bs
->remote_timers
.required_min_rx
> bs
->timers
.desired_min_tx
)
989 bs
->detect_TO
= bs
->remote_detect_mult
990 * bs
->remote_timers
.required_min_rx
;
992 bs
->detect_TO
= bs
->remote_detect_mult
993 * bs
->timers
.desired_min_tx
;
995 /* Apply new receive timer immediately. */
996 bfd_recvtimer_update(bs
);
998 /* Notify watchers about changed timers. */
999 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
1002 void bs_set_slow_timers(struct bfd_session
*bs
)
1005 * BFD connection must use slow timers before going up or after
1006 * losing connectivity to avoid wasting bandwidth.
1008 * RFC 5880, Section 6.8.3.
1010 bs
->cur_timers
.desired_min_tx
= BFD_DEF_SLOWTX
;
1011 bs
->cur_timers
.required_min_rx
= BFD_DEF_SLOWTX
;
1012 bs
->cur_timers
.required_min_echo
= 0;
1014 /* Set the appropriated timeouts for slow connection. */
1015 bs
->detect_TO
= (BFD_DEFDETECTMULT
* BFD_DEF_SLOWTX
);
1016 bs
->xmt_TO
= BFD_DEF_SLOWTX
;
1022 static const char *get_diag_str(int diag
)
1024 for (int i
= 0; diag_list
[i
].str
; i
++) {
1025 if (diag_list
[i
].type
== diag
)
1026 return diag_list
[i
].str
;
1031 const char *satostr(struct sockaddr_any
*sa
)
1033 #define INETSTR_BUFCOUNT 8
1034 static char buf
[INETSTR_BUFCOUNT
][INET6_ADDRSTRLEN
];
1036 struct sockaddr_in
*sin
= &sa
->sa_sin
;
1037 struct sockaddr_in6
*sin6
= &sa
->sa_sin6
;
1039 bufidx
+= (bufidx
+ 1) % INETSTR_BUFCOUNT
;
1042 switch (sin
->sin_family
) {
1044 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
[bufidx
],
1045 sizeof(buf
[bufidx
]));
1048 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, buf
[bufidx
],
1049 sizeof(buf
[bufidx
]));
1053 strlcpy(buf
[bufidx
], "unknown", sizeof(buf
[bufidx
]));
1060 const char *diag2str(uint8_t diag
)
1066 return "control detection time expired";
1068 return "echo function failed";
1070 return "neighbor signaled session down";
1072 return "forwarding plane reset";
1076 return "concatenated path down";
1078 return "administratively down";
1080 return "reverse concatenated path down";
1086 int strtosa(const char *addr
, struct sockaddr_any
*sa
)
1088 memset(sa
, 0, sizeof(*sa
));
1090 if (inet_pton(AF_INET
, addr
, &sa
->sa_sin
.sin_addr
) == 1) {
1091 sa
->sa_sin
.sin_family
= AF_INET
;
1092 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1093 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
1094 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1098 if (inet_pton(AF_INET6
, addr
, &sa
->sa_sin6
.sin6_addr
) == 1) {
1099 sa
->sa_sin6
.sin6_family
= AF_INET6
;
1100 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1101 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
1102 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1109 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
)
1111 unsigned int year
, month
, day
, hour
, minute
, second
;
1114 #define MINUTES (60)
1115 #define HOURS (60 * MINUTES)
1116 #define DAYS (24 * HOURS)
1117 #define MONTHS (30 * DAYS)
1118 #define YEARS (12 * MONTHS)
1119 if (time
>= YEARS
) {
1120 year
= time
/ YEARS
;
1121 time
-= year
* YEARS
;
1123 rv
= snprintf(buf
, buflen
, "%u year(s), ", year
);
1127 if (time
>= MONTHS
) {
1128 month
= time
/ MONTHS
;
1129 time
-= month
* MONTHS
;
1131 rv
= snprintf(buf
, buflen
, "%u month(s), ", month
);
1139 rv
= snprintf(buf
, buflen
, "%u day(s), ", day
);
1143 if (time
>= HOURS
) {
1144 hour
= time
/ HOURS
;
1145 time
-= hour
* HOURS
;
1147 rv
= snprintf(buf
, buflen
, "%u hour(s), ", hour
);
1151 if (time
>= MINUTES
) {
1152 minute
= time
/ MINUTES
;
1153 time
-= minute
* MINUTES
;
1155 rv
= snprintf(buf
, buflen
, "%u minute(s), ", minute
);
1159 second
= time
% MINUTES
;
1160 snprintf(buf
, buflen
, "%u second(s)", second
);
1163 const char *bs_to_string(const struct bfd_session
*bs
)
1165 static char buf
[256];
1166 char addr_buf
[INET6_ADDRSTRLEN
];
1168 bool is_mhop
= BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
);
1170 pos
= snprintf(buf
, sizeof(buf
), "mhop:%s", is_mhop
? "yes" : "no");
1171 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " peer:%s",
1172 inet_ntop(bs
->key
.family
, &bs
->key
.peer
, addr_buf
,
1174 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " local:%s",
1175 inet_ntop(bs
->key
.family
, &bs
->key
.local
, addr_buf
,
1177 if (bs
->key
.vrfname
[0])
1178 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " vrf:%s",
1180 if (bs
->key
.ifname
[0])
1181 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " ifname:%s",
1189 int bs_observer_add(struct bfd_session
*bs
)
1191 struct bfd_session_observer
*bso
;
1193 bso
= XCALLOC(MTYPE_BFDD_SESSION_OBSERVER
, sizeof(*bso
));
1194 bso
->bso_isaddress
= false;
1196 bso
->bso_isinterface
= !BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
);
1197 if (bso
->bso_isinterface
)
1198 strlcpy(bso
->bso_entryname
, bs
->key
.ifname
,
1199 sizeof(bso
->bso_entryname
));
1201 strlcpy(bso
->bso_entryname
, bs
->key
.vrfname
,
1202 sizeof(bso
->bso_entryname
));
1204 /* Handle socket binding failures caused by missing local addresses. */
1205 if (bs
->sock
== -1) {
1206 bso
->bso_isaddress
= true;
1207 bso
->bso_addr
.family
= bs
->key
.family
;
1208 memcpy(&bso
->bso_addr
.u
.prefix
, &bs
->key
.local
,
1209 sizeof(bs
->key
.local
));
1212 TAILQ_INSERT_TAIL(&bglobal
.bg_obslist
, bso
, bso_entry
);
1217 void bs_observer_del(struct bfd_session_observer
*bso
)
1219 TAILQ_REMOVE(&bglobal
.bg_obslist
, bso
, bso_entry
);
1220 XFREE(MTYPE_BFDD_SESSION_OBSERVER
, bso
);
1223 void bs_to_bpc(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
1225 memset(bpc
, 0, sizeof(*bpc
));
1227 bpc
->bpc_ipv4
= (bs
->key
.family
== AF_INET
);
1228 bpc
->bpc_mhop
= bs
->key
.mhop
;
1230 switch (bs
->key
.family
) {
1232 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET
;
1233 memcpy(&bpc
->bpc_peer
.sa_sin
.sin_addr
, &bs
->key
.peer
,
1234 sizeof(bpc
->bpc_peer
.sa_sin
.sin_addr
));
1236 if (memcmp(&bs
->key
.local
, &zero_addr
, sizeof(bs
->key
.local
))) {
1237 bpc
->bpc_local
.sa_sin
.sin_family
= AF_INET6
;
1238 memcpy(&bpc
->bpc_local
.sa_sin
.sin_addr
, &bs
->key
.local
,
1239 sizeof(bpc
->bpc_local
.sa_sin
.sin_addr
));
1244 bpc
->bpc_peer
.sa_sin
.sin_family
= AF_INET6
;
1245 memcpy(&bpc
->bpc_peer
.sa_sin6
.sin6_addr
, &bs
->key
.peer
,
1246 sizeof(bpc
->bpc_peer
.sa_sin6
.sin6_addr
));
1248 bpc
->bpc_local
.sa_sin6
.sin6_family
= AF_INET6
;
1249 memcpy(&bpc
->bpc_local
.sa_sin6
.sin6_addr
, &bs
->key
.local
,
1250 sizeof(bpc
->bpc_local
.sa_sin6
.sin6_addr
));
1254 if (bs
->key
.ifname
[0]) {
1255 bpc
->bpc_has_localif
= true;
1256 strlcpy(bpc
->bpc_localif
, bs
->key
.ifname
,
1257 sizeof(bpc
->bpc_localif
));
1260 if (bs
->key
.vrfname
[0]) {
1261 bpc
->bpc_has_vrfname
= true;
1262 strlcpy(bpc
->bpc_vrfname
, bs
->key
.vrfname
,
1263 sizeof(bpc
->bpc_vrfname
));
1269 * BFD hash data structures to find sessions.
1271 static struct hash
*bfd_id_hash
;
1272 static struct hash
*bfd_key_hash
;
1274 static unsigned int bfd_id_hash_do(void *p
);
1275 static unsigned int bfd_key_hash_do(void *p
);
1277 static void _bfd_free(struct hash_bucket
*hb
,
1278 void *arg
__attribute__((__unused__
)));
1280 /* BFD hash for our discriminator. */
1281 static unsigned int bfd_id_hash_do(void *p
)
1283 struct bfd_session
*bs
= p
;
1285 return jhash_1word(bs
->discrs
.my_discr
, 0);
1288 static bool bfd_id_hash_cmp(const void *n1
, const void *n2
)
1290 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1292 return bs1
->discrs
.my_discr
== bs2
->discrs
.my_discr
;
1295 /* BFD hash for single hop. */
1296 static unsigned int bfd_key_hash_do(void *p
)
1298 struct bfd_session
*bs
= p
;
1300 return jhash(&bs
->key
, sizeof(bs
->key
), 0);
1303 static bool bfd_key_hash_cmp(const void *n1
, const void *n2
)
1305 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
1307 return memcmp(&bs1
->key
, &bs2
->key
, sizeof(bs1
->key
)) == 0;
1312 * Hash public interface / exported functions.
1315 /* Lookup functions. */
1316 struct bfd_session
*bfd_id_lookup(uint32_t id
)
1318 struct bfd_session bs
;
1320 bs
.discrs
.my_discr
= id
;
1322 return hash_lookup(bfd_id_hash
, &bs
);
1325 struct bfd_session
*bfd_key_lookup(struct bfd_key key
)
1327 struct bfd_session bs
, *bsp
;
1330 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1332 /* Handle cases where local-address is optional. */
1333 if (bsp
== NULL
&& bs
.key
.family
== AF_INET
) {
1334 memset(&bs
.key
.local
, 0, sizeof(bs
.key
.local
));
1335 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1338 /* Handle cases where ifname is optional. */
1340 if (bsp
== NULL
&& bs
.key
.ifname
[0]) {
1341 memset(bs
.key
.ifname
, 0, sizeof(bs
.key
.ifname
));
1342 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1344 /* Handle cases where local-address and ifname are optional. */
1345 if (bsp
== NULL
&& bs
.key
.family
== AF_INET
) {
1346 memset(&bs
.key
.local
, 0, sizeof(bs
.key
.local
));
1347 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1357 * Delete functions searches and remove the item from the hash and
1358 * returns a pointer to the removed item data. If the item was not found
1359 * then it returns NULL.
1361 * The data stored inside the hash is not free()ed, so you must do it
1362 * manually after getting the pointer back.
1364 struct bfd_session
*bfd_id_delete(uint32_t id
)
1366 struct bfd_session bs
;
1368 bs
.discrs
.my_discr
= id
;
1370 return hash_release(bfd_id_hash
, &bs
);
1373 struct bfd_session
*bfd_key_delete(struct bfd_key key
)
1375 struct bfd_session bs
, *bsp
;
1378 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1379 if (bsp
== NULL
&& key
.ifname
[0]) {
1380 memset(bs
.key
.ifname
, 0, sizeof(bs
.key
.ifname
));
1381 bsp
= hash_lookup(bfd_key_hash
, &bs
);
1384 return hash_release(bfd_key_hash
, bsp
);
1387 /* Iteration functions. */
1388 void bfd_id_iterate(hash_iter_func hif
, void *arg
)
1390 hash_iterate(bfd_id_hash
, hif
, arg
);
1393 void bfd_key_iterate(hash_iter_func hif
, void *arg
)
1395 hash_iterate(bfd_key_hash
, hif
, arg
);
1401 * Inserts session into hash and returns `true` on success, otherwise
1404 bool bfd_id_insert(struct bfd_session
*bs
)
1406 return (hash_get(bfd_id_hash
, bs
, hash_alloc_intern
) == bs
);
1409 bool bfd_key_insert(struct bfd_session
*bs
)
1411 return (hash_get(bfd_key_hash
, bs
, hash_alloc_intern
) == bs
);
1414 void bfd_initialize(void)
1416 bfd_id_hash
= hash_create(bfd_id_hash_do
, bfd_id_hash_cmp
,
1417 "BFD session discriminator hash");
1418 bfd_key_hash
= hash_create(bfd_key_hash_do
, bfd_key_hash_cmp
,
1419 "BFD session hash");
1422 static void _bfd_free(struct hash_bucket
*hb
,
1423 void *arg
__attribute__((__unused__
)))
1425 struct bfd_session
*bs
= hb
->data
;
1427 bfd_session_free(bs
);
1430 void bfd_shutdown(void)
1433 * Close and free all BFD sessions.
1435 * _bfd_free() will call bfd_session_free() which will take care
1436 * of removing the session from all hashes, so we just run an
1437 * assert() here to make sure it really happened.
1439 bfd_id_iterate(_bfd_free
, NULL
);
1440 assert(bfd_key_hash
->count
== 0);
1442 /* Now free the hashes themselves. */
1443 hash_free(bfd_id_hash
);
1444 hash_free(bfd_key_hash
);