2 * bgp_bfd.c: BGP BFD handling routines
4 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include "bgpd/bgpd.h"
40 #include "bgpd/bgp_bfd.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_vty.h"
44 DEFINE_MTYPE_STATIC(BGPD
, BFD_CONFIG
, "BFD configuration data");
46 extern struct zclient
*zclient
;
48 static void bfd_session_status_update(struct bfd_session_params
*bsp
,
49 const struct bfd_session_status
*bss
,
52 struct peer
*peer
= arg
;
54 if (BGP_DEBUG(bfd
, BFD_LIB
))
55 zlog_debug("%s: neighbor %s vrf %s(%u) bfd state %s -> %s",
56 __func__
, peer
->conf_if
? peer
->conf_if
: peer
->host
,
57 bfd_sess_vrf(bsp
), bfd_sess_vrf_id(bsp
),
58 bfd_get_status_str(bss
->previous_state
),
59 bfd_get_status_str(bss
->state
));
61 if (bss
->state
== BSS_DOWN
&& bss
->previous_state
== BSS_UP
) {
62 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)
63 && bfd_sess_cbit(bsp
) && !bss
->remote_cbit
) {
64 if (BGP_DEBUG(bfd
, BFD_LIB
))
66 "%s BFD DOWN message ignored in the process of graceful restart when C bit is cleared",
70 peer
->last_reset
= PEER_DOWN_BFD_DOWN
;
71 BGP_EVENT_ADD(peer
, BGP_Stop
);
74 if (bss
->state
== BSS_UP
&& bss
->previous_state
!= BSS_UP
75 && !peer_established(peer
)) {
76 if (!BGP_PEER_START_SUPPRESSED(peer
)) {
77 bgp_fsm_nht_update(peer
, true);
78 BGP_EVENT_ADD(peer
, BGP_Start
);
83 void bgp_peer_config_apply(struct peer
*p
, struct peer_group
*pg
)
89 /* When called on a group, apply to all peers. */
90 if (CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
)) {
91 for (ALL_LIST_ELEMENTS_RO(p
->group
->peer
, n
, pn
))
92 bgp_peer_config_apply(pn
, pg
);
96 /* No group, just use current configuration. */
97 if (pg
== NULL
|| pg
->conf
->bfd_config
== NULL
) {
98 bfd_sess_set_timers(p
->bfd_config
->session
,
99 p
->bfd_config
->detection_multiplier
,
100 p
->bfd_config
->min_rx
,
101 p
->bfd_config
->min_tx
);
102 bfd_sess_set_cbit(p
->bfd_config
->session
, p
->bfd_config
->cbit
);
103 bfd_sess_set_profile(p
->bfd_config
->session
,
104 p
->bfd_config
->profile
);
105 bfd_sess_install(p
->bfd_config
->session
);
110 * Check if the group configuration was overwritten or apply group
116 * If using default control plane independent configuration,
117 * then prefer group's (e.g. it means it wasn't manually configured).
119 if (!p
->bfd_config
->cbit
)
120 bfd_sess_set_cbit(p
->bfd_config
->session
,
121 gconfig
->bfd_config
->cbit
);
123 bfd_sess_set_cbit(p
->bfd_config
->session
, p
->bfd_config
->cbit
);
125 /* If no profile was specified in peer, then use the group profile. */
126 if (p
->bfd_config
->profile
[0] == 0)
127 bfd_sess_set_profile(p
->bfd_config
->session
,
128 gconfig
->bfd_config
->profile
);
130 bfd_sess_set_profile(p
->bfd_config
->session
,
131 p
->bfd_config
->profile
);
133 /* If no specific timers were configured, then use the group timers. */
134 if (p
->bfd_config
->detection_multiplier
== BFD_DEF_DETECT_MULT
135 || p
->bfd_config
->min_rx
== BFD_DEF_MIN_RX
136 || p
->bfd_config
->min_tx
== BFD_DEF_MIN_TX
)
137 bfd_sess_set_timers(p
->bfd_config
->session
,
138 gconfig
->bfd_config
->detection_multiplier
,
139 gconfig
->bfd_config
->min_rx
,
140 gconfig
->bfd_config
->min_tx
);
142 bfd_sess_set_timers(p
->bfd_config
->session
,
143 p
->bfd_config
->detection_multiplier
,
144 p
->bfd_config
->min_rx
,
145 p
->bfd_config
->min_tx
);
147 bfd_sess_install(p
->bfd_config
->session
);
150 void bgp_peer_bfd_update_source(struct peer
*p
)
152 struct bfd_session_params
*session
= p
->bfd_config
->session
;
153 bool changed
= false;
160 /* Nothing to do for groups. */
161 if (CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
))
164 /* Update peer's source/destination addresses. */
165 bfd_sess_addresses(session
, &family
, &src
.v6
, &dst
.v6
);
166 if (family
== AF_INET
) {
168 && p
->su_local
->sin
.sin_addr
.s_addr
!= src
.v4
.s_addr
)
169 || p
->su
.sin
.sin_addr
.s_addr
!= dst
.v4
.s_addr
) {
170 if (BGP_DEBUG(bfd
, BFD_LIB
))
172 "%s: address [%pI4->%pI4] to [%pI4->%pI4]",
173 __func__
, &src
.v4
, &dst
.v4
,
174 p
->su_local
? &p
->su_local
->sin
.sin_addr
176 &p
->su
.sin
.sin_addr
);
178 bfd_sess_set_ipv4_addrs(
180 p
->su_local
? &p
->su_local
->sin
.sin_addr
: NULL
,
181 &p
->su
.sin
.sin_addr
);
186 && memcmp(&p
->su_local
->sin6
, &src
.v6
, sizeof(src
.v6
)))
187 || memcmp(&p
->su
.sin6
, &dst
.v6
, sizeof(dst
.v6
))) {
188 if (BGP_DEBUG(bfd
, BFD_LIB
))
190 "%s: address [%pI6->%pI6] to [%pI6->%pI6]",
191 __func__
, &src
.v6
, &dst
.v6
,
193 ? &p
->su_local
->sin6
.sin6_addr
195 &p
->su
.sin6
.sin6_addr
);
197 bfd_sess_set_ipv6_addrs(
199 p
->su_local
? &p
->su_local
->sin6
.sin6_addr
201 &p
->su
.sin6
.sin6_addr
);
206 /* Update interface. */
207 if (p
->nexthop
.ifp
&& bfd_sess_interface(session
) == NULL
) {
208 if (BGP_DEBUG(bfd
, BFD_LIB
))
209 zlog_debug("%s: interface none to %s", __func__
,
210 p
->nexthop
.ifp
->name
);
212 bfd_sess_set_interface(session
, p
->nexthop
.ifp
->name
);
220 * - We detected that the peer is a hop away from us (remove multi hop).
221 * (this happens when `p->shared_network` is set to `true`)
222 * - eBGP multi hop / TTL security changed.
224 if (!PEER_IS_MULTIHOP(p
) && bfd_sess_hop_count(session
) > 1) {
225 if (BGP_DEBUG(bfd
, BFD_LIB
))
226 zlog_debug("%s: TTL %d to 1", __func__
,
227 bfd_sess_hop_count(session
));
229 bfd_sess_set_hop_count(session
, 1);
232 if (PEER_IS_MULTIHOP(p
) && p
->ttl
!= bfd_sess_hop_count(session
)) {
233 if (BGP_DEBUG(bfd
, BFD_LIB
))
234 zlog_debug("%s: TTL %d to %d", __func__
,
235 bfd_sess_hop_count(session
), p
->ttl
);
237 bfd_sess_set_hop_count(session
, p
->ttl
);
242 if (bfd_sess_vrf_id(session
) != p
->bgp
->vrf_id
) {
243 if (BGP_DEBUG(bfd
, BFD_LIB
))
245 "%s: VRF %s(%d) to %s(%d)", __func__
,
246 bfd_sess_vrf(session
), bfd_sess_vrf_id(session
),
247 vrf_id_to_name(p
->bgp
->vrf_id
), p
->bgp
->vrf_id
);
249 bfd_sess_set_vrf(session
, p
->bgp
->vrf_id
);
254 bfd_sess_install(session
);
258 * Reset BFD configuration data structure to its defaults settings.
260 static void bgp_peer_bfd_reset(struct peer
*p
)
263 p
->bfd_config
->detection_multiplier
= BFD_DEF_DETECT_MULT
;
264 p
->bfd_config
->min_rx
= BFD_DEF_MIN_RX
;
265 p
->bfd_config
->min_tx
= BFD_DEF_MIN_TX
;
266 p
->bfd_config
->cbit
= false;
267 p
->bfd_config
->profile
[0] = 0;
270 void bgp_peer_configure_bfd(struct peer
*p
, bool manual
)
272 /* Groups should not call this. */
273 assert(!CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
275 /* Already configured, skip it. */
277 /* If manually active update flag. */
278 if (!p
->bfd_config
->manual
)
279 p
->bfd_config
->manual
= manual
;
284 /* Allocate memory for configuration overrides. */
285 p
->bfd_config
= XCALLOC(MTYPE_BFD_CONFIG
, sizeof(*p
->bfd_config
));
286 p
->bfd_config
->manual
= manual
;
288 /* Create new session and assign callback. */
289 p
->bfd_config
->session
= bfd_sess_new(bfd_session_status_update
, p
);
290 bgp_peer_bfd_reset(p
);
292 /* Configure session with basic BGP peer data. */
293 if (p
->su
.sa
.sa_family
== AF_INET
)
294 bfd_sess_set_ipv4_addrs(p
->bfd_config
->session
,
295 p
->su_local
? &p
->su_local
->sin
.sin_addr
297 &p
->su
.sin
.sin_addr
);
299 bfd_sess_set_ipv6_addrs(
300 p
->bfd_config
->session
,
301 p
->su_local
? &p
->su_local
->sin6
.sin6_addr
: NULL
,
302 &p
->su
.sin6
.sin6_addr
);
304 bfd_sess_set_vrf(p
->bfd_config
->session
, p
->bgp
->vrf_id
);
305 bfd_sess_set_hop_count(p
->bfd_config
->session
,
306 PEER_IS_MULTIHOP(p
) ? p
->ttl
: 1);
309 bfd_sess_set_interface(p
->bfd_config
->session
,
310 p
->nexthop
.ifp
->name
);
313 static void bgp_peer_remove_bfd(struct peer
*p
)
315 /* Groups should not call this. */
316 assert(!CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
319 * Peer configuration was removed, however we must check if there
320 * is still a group configuration to keep this running.
322 if (p
->group
&& p
->group
->conf
->bfd_config
) {
323 p
->bfd_config
->manual
= false;
324 bgp_peer_bfd_reset(p
);
325 bgp_peer_config_apply(p
, p
->group
);
330 bfd_sess_free(&p
->bfd_config
->session
);
332 XFREE(MTYPE_BFD_CONFIG
, p
->bfd_config
);
335 static void bgp_group_configure_bfd(struct peer
*p
)
340 /* Peers should not call this. */
341 assert(CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
343 /* Already allocated: do nothing. */
347 p
->bfd_config
= XCALLOC(MTYPE_BFD_CONFIG
, sizeof(*p
->bfd_config
));
350 p
->bfd_config
->detection_multiplier
= BFD_DEF_DETECT_MULT
;
351 p
->bfd_config
->min_rx
= BFD_DEF_MIN_RX
;
352 p
->bfd_config
->min_tx
= BFD_DEF_MIN_TX
;
354 for (ALL_LIST_ELEMENTS_RO(p
->group
->peer
, n
, pn
))
355 bgp_peer_configure_bfd(pn
, false);
358 static void bgp_group_remove_bfd(struct peer
*p
)
363 /* Peers should not call this. */
364 assert(CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
366 /* Already freed: do nothing. */
367 if (p
->bfd_config
== NULL
)
370 /* Free configuration and point to `NULL`. */
371 XFREE(MTYPE_BFD_CONFIG
, p
->bfd_config
);
373 /* Now that it is `NULL` recalculate configuration for all peers. */
374 for (ALL_LIST_ELEMENTS_RO(p
->group
->peer
, n
, pn
)) {
375 if (pn
->bfd_config
->manual
)
376 bgp_peer_config_apply(pn
, NULL
);
378 bgp_peer_remove_bfd(pn
);
382 void bgp_peer_remove_bfd_config(struct peer
*p
)
384 if (CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
))
385 bgp_group_remove_bfd(p
);
387 bgp_peer_remove_bfd(p
);
391 * bgp_bfd_peer_config_write - Write the peer BFD configuration.
393 void bgp_bfd_peer_config_write(struct vty
*vty
, const struct peer
*peer
,
397 * Always show group BFD configuration, but peer only when explicitly
400 if ((!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
401 && peer
->bfd_config
->manual
)
402 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
404 vty_out(vty
, " neighbor %s bfd\n", addr
);
406 vty_out(vty
, " neighbor %s bfd %d %d %d\n", addr
,
407 peer
->bfd_config
->detection_multiplier
,
408 peer
->bfd_config
->min_rx
, peer
->bfd_config
->min_tx
);
409 #endif /* HAVE_BFDD */
412 if (peer
->bfd_config
->profile
[0])
413 vty_out(vty
, " neighbor %s bfd profile %s\n", addr
,
414 peer
->bfd_config
->profile
);
416 if (peer
->bfd_config
->cbit
)
417 vty_out(vty
, " neighbor %s bfd check-control-plane-failure\n",
422 * bgp_bfd_show_info - Show the peer BFD information.
424 void bgp_bfd_show_info(struct vty
*vty
, const struct peer
*peer
,
425 json_object
*json_neigh
)
427 bfd_sess_show(vty
, json_neigh
, peer
->bfd_config
->session
);
432 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
435 "Enables BFD support\n")
440 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
442 return CMD_WARNING_CONFIG_FAILED
;
444 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
445 bgp_group_configure_bfd(peer
);
447 bgp_peer_configure_bfd(peer
, true);
449 bgp_peer_config_apply(peer
, peer
->group
);
458 #endif /* HAVE_BFDD */
460 neighbor_bfd_param_cmd
,
461 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd (2-255) (50-60000) (50-60000)",
464 "Enables BFD support\n"
465 "Detect Multiplier\n"
466 "Required min receive interval\n"
467 "Desired min transmit interval\n")
470 int idx_number_1
= 3;
471 int idx_number_2
= 4;
472 int idx_number_3
= 5;
473 long detection_multiplier
, min_rx
, min_tx
;
476 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
478 return CMD_WARNING_CONFIG_FAILED
;
480 detection_multiplier
= strtol(argv
[idx_number_1
]->arg
, NULL
, 10);
481 min_rx
= strtol(argv
[idx_number_2
]->arg
, NULL
, 10);
482 min_tx
= strtol(argv
[idx_number_3
]->arg
, NULL
, 10);
484 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
485 bgp_group_configure_bfd(peer
);
487 bgp_peer_configure_bfd(peer
, true);
489 peer
->bfd_config
->detection_multiplier
= detection_multiplier
;
490 peer
->bfd_config
->min_rx
= min_rx
;
491 peer
->bfd_config
->min_tx
= min_tx
;
492 bgp_peer_config_apply(peer
, peer
->group
);
497 DEFUN (neighbor_bfd_check_controlplane_failure
,
498 neighbor_bfd_check_controlplane_failure_cmd
,
499 "[no] neighbor <A.B.C.D|X:X::X:X|WORD> bfd check-control-plane-failure",
504 "Link dataplane status with BGP controlplane\n")
506 const char *no
= strmatch(argv
[0]->text
, "no") ? "no" : NULL
;
514 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
516 vty_out(vty
, "%% Specify remote-as or peer-group commands first\n");
517 return CMD_WARNING_CONFIG_FAILED
;
520 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
521 bgp_group_configure_bfd(peer
);
523 bgp_peer_configure_bfd(peer
, true);
525 peer
->bfd_config
->cbit
= no
== NULL
;
526 bgp_peer_config_apply(peer
, peer
->group
);
531 DEFUN (no_neighbor_bfd
,
534 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
536 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd [(2-255) (50-60000) (50-60000)]",
537 #endif /* HAVE_BFDD */
541 "Disables BFD support\n"
543 "Detect Multiplier\n"
544 "Required min receive interval\n"
545 "Desired min transmit interval\n"
546 #endif /* !HAVE_BFDD */
552 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
554 return CMD_WARNING_CONFIG_FAILED
;
556 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
557 bgp_group_remove_bfd(peer
);
559 bgp_peer_remove_bfd(peer
);
565 DEFUN(neighbor_bfd_profile
, neighbor_bfd_profile_cmd
,
566 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd profile BFDPROF",
571 BFD_PROFILE_NAME_STR
)
573 int idx_peer
= 1, idx_prof
= 4;
576 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
578 return CMD_WARNING_CONFIG_FAILED
;
580 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
581 bgp_group_configure_bfd(peer
);
583 bgp_peer_configure_bfd(peer
, true);
585 strlcpy(peer
->bfd_config
->profile
, argv
[idx_prof
]->arg
,
586 sizeof(peer
->bfd_config
->profile
));
587 bgp_peer_config_apply(peer
, peer
->group
);
592 DEFUN(no_neighbor_bfd_profile
, no_neighbor_bfd_profile_cmd
,
593 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd profile [BFDPROF]",
599 BFD_PROFILE_NAME_STR
)
604 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
606 return CMD_WARNING_CONFIG_FAILED
;
608 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
609 bgp_group_configure_bfd(peer
);
611 bgp_peer_configure_bfd(peer
, true);
613 peer
->bfd_config
->profile
[0] = 0;
614 bgp_peer_config_apply(peer
, peer
->group
);
618 #endif /* HAVE_BFDD */
620 void bgp_bfd_init(struct thread_master
*tm
)
622 /* Initialize BFD client functions */
623 bfd_protocol_integration_init(zclient
, tm
);
625 /* "neighbor bfd" commands. */
626 install_element(BGP_NODE
, &neighbor_bfd_cmd
);
627 install_element(BGP_NODE
, &neighbor_bfd_param_cmd
);
628 install_element(BGP_NODE
, &neighbor_bfd_check_controlplane_failure_cmd
);
629 install_element(BGP_NODE
, &no_neighbor_bfd_cmd
);
632 install_element(BGP_NODE
, &neighbor_bfd_profile_cmd
);
633 install_element(BGP_NODE
, &no_neighbor_bfd_profile_cmd
);
634 #endif /* HAVE_BFDD */