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 const union sockunion
*source
;
154 bool changed
= false;
161 /* Nothing to do for groups. */
162 if (CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
))
165 /* Figure out the correct source to use. */
166 if (CHECK_FLAG(p
->flags
, PEER_FLAG_UPDATE_SOURCE
) && p
->update_source
)
167 source
= p
->update_source
;
169 source
= p
->su_local
;
171 /* Update peer's source/destination addresses. */
172 bfd_sess_addresses(session
, &family
, &src
.v6
, &dst
.v6
);
173 if (family
== AF_INET
) {
174 if ((source
&& source
->sin
.sin_addr
.s_addr
!= src
.v4
.s_addr
)
175 || p
->su
.sin
.sin_addr
.s_addr
!= dst
.v4
.s_addr
) {
176 if (BGP_DEBUG(bfd
, BFD_LIB
))
178 "%s: address [%pI4->%pI4] to [%pI4->%pI4]",
179 __func__
, &src
.v4
, &dst
.v4
,
180 source
? &source
->sin
.sin_addr
182 &p
->su
.sin
.sin_addr
);
184 bfd_sess_set_ipv4_addrs(
185 session
, source
? &source
->sin
.sin_addr
: NULL
,
186 &p
->su
.sin
.sin_addr
);
190 if ((source
&& memcmp(&source
->sin6
, &src
.v6
, sizeof(src
.v6
)))
191 || memcmp(&p
->su
.sin6
, &dst
.v6
, sizeof(dst
.v6
))) {
192 if (BGP_DEBUG(bfd
, BFD_LIB
))
194 "%s: address [%pI6->%pI6] to [%pI6->%pI6]",
195 __func__
, &src
.v6
, &dst
.v6
,
196 source
? &source
->sin6
.sin6_addr
198 &p
->su
.sin6
.sin6_addr
);
200 bfd_sess_set_ipv6_addrs(session
,
201 source
? &source
->sin6
.sin6_addr
203 &p
->su
.sin6
.sin6_addr
);
208 /* Update interface. */
209 if (p
->nexthop
.ifp
&& bfd_sess_interface(session
) == NULL
) {
210 if (BGP_DEBUG(bfd
, BFD_LIB
))
211 zlog_debug("%s: interface none to %s", __func__
,
212 p
->nexthop
.ifp
->name
);
214 bfd_sess_set_interface(session
, p
->nexthop
.ifp
->name
);
222 * - We detected that the peer is a hop away from us (remove multi hop).
223 * (this happens when `p->shared_network` is set to `true`)
224 * - eBGP multi hop / TTL security changed.
226 if (!PEER_IS_MULTIHOP(p
) && bfd_sess_hop_count(session
) > 1) {
227 if (BGP_DEBUG(bfd
, BFD_LIB
))
228 zlog_debug("%s: TTL %d to 1", __func__
,
229 bfd_sess_hop_count(session
));
231 bfd_sess_set_hop_count(session
, 1);
234 if (PEER_IS_MULTIHOP(p
) && p
->ttl
!= bfd_sess_hop_count(session
)) {
235 if (BGP_DEBUG(bfd
, BFD_LIB
))
236 zlog_debug("%s: TTL %d to %d", __func__
,
237 bfd_sess_hop_count(session
), p
->ttl
);
239 bfd_sess_set_hop_count(session
, p
->ttl
);
244 if (bfd_sess_vrf_id(session
) != p
->bgp
->vrf_id
) {
245 if (BGP_DEBUG(bfd
, BFD_LIB
))
247 "%s: VRF %s(%d) to %s(%d)", __func__
,
248 bfd_sess_vrf(session
), bfd_sess_vrf_id(session
),
249 vrf_id_to_name(p
->bgp
->vrf_id
), p
->bgp
->vrf_id
);
251 bfd_sess_set_vrf(session
, p
->bgp
->vrf_id
);
256 bfd_sess_install(session
);
260 * Reset BFD configuration data structure to its defaults settings.
262 static void bgp_peer_bfd_reset(struct peer
*p
)
265 p
->bfd_config
->detection_multiplier
= BFD_DEF_DETECT_MULT
;
266 p
->bfd_config
->min_rx
= BFD_DEF_MIN_RX
;
267 p
->bfd_config
->min_tx
= BFD_DEF_MIN_TX
;
268 p
->bfd_config
->cbit
= false;
269 p
->bfd_config
->profile
[0] = 0;
272 void bgp_peer_configure_bfd(struct peer
*p
, bool manual
)
274 /* Groups should not call this. */
275 assert(!CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
277 /* Already configured, skip it. */
279 /* If manually active update flag. */
280 if (!p
->bfd_config
->manual
)
281 p
->bfd_config
->manual
= manual
;
286 /* Allocate memory for configuration overrides. */
287 p
->bfd_config
= XCALLOC(MTYPE_BFD_CONFIG
, sizeof(*p
->bfd_config
));
288 p
->bfd_config
->manual
= manual
;
290 /* Create new session and assign callback. */
291 p
->bfd_config
->session
= bfd_sess_new(bfd_session_status_update
, p
);
292 bgp_peer_bfd_reset(p
);
294 /* Configure session with basic BGP peer data. */
295 if (p
->su
.sa
.sa_family
== AF_INET
)
296 bfd_sess_set_ipv4_addrs(p
->bfd_config
->session
,
297 p
->su_local
? &p
->su_local
->sin
.sin_addr
299 &p
->su
.sin
.sin_addr
);
301 bfd_sess_set_ipv6_addrs(
302 p
->bfd_config
->session
,
303 p
->su_local
? &p
->su_local
->sin6
.sin6_addr
: NULL
,
304 &p
->su
.sin6
.sin6_addr
);
306 bfd_sess_set_vrf(p
->bfd_config
->session
, p
->bgp
->vrf_id
);
307 bfd_sess_set_hop_count(p
->bfd_config
->session
,
308 PEER_IS_MULTIHOP(p
) ? p
->ttl
: 1);
311 bfd_sess_set_interface(p
->bfd_config
->session
,
312 p
->nexthop
.ifp
->name
);
315 static void bgp_peer_remove_bfd(struct peer
*p
)
317 /* Groups should not call this. */
318 assert(!CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
321 * Peer configuration was removed, however we must check if there
322 * is still a group configuration to keep this running.
324 if (p
->group
&& p
->group
->conf
->bfd_config
) {
325 p
->bfd_config
->manual
= false;
326 bgp_peer_bfd_reset(p
);
327 bgp_peer_config_apply(p
, p
->group
);
332 bfd_sess_free(&p
->bfd_config
->session
);
334 XFREE(MTYPE_BFD_CONFIG
, p
->bfd_config
);
337 static void bgp_group_configure_bfd(struct peer
*p
)
342 /* Peers should not call this. */
343 assert(CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
345 /* Already allocated: do nothing. */
349 p
->bfd_config
= XCALLOC(MTYPE_BFD_CONFIG
, sizeof(*p
->bfd_config
));
352 p
->bfd_config
->detection_multiplier
= BFD_DEF_DETECT_MULT
;
353 p
->bfd_config
->min_rx
= BFD_DEF_MIN_RX
;
354 p
->bfd_config
->min_tx
= BFD_DEF_MIN_TX
;
356 for (ALL_LIST_ELEMENTS_RO(p
->group
->peer
, n
, pn
))
357 bgp_peer_configure_bfd(pn
, false);
360 static void bgp_group_remove_bfd(struct peer
*p
)
365 /* Peers should not call this. */
366 assert(CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
));
368 /* Already freed: do nothing. */
369 if (p
->bfd_config
== NULL
)
372 /* Free configuration and point to `NULL`. */
373 XFREE(MTYPE_BFD_CONFIG
, p
->bfd_config
);
375 /* Now that it is `NULL` recalculate configuration for all peers. */
376 for (ALL_LIST_ELEMENTS_RO(p
->group
->peer
, n
, pn
)) {
377 if (pn
->bfd_config
->manual
)
378 bgp_peer_config_apply(pn
, NULL
);
380 bgp_peer_remove_bfd(pn
);
384 void bgp_peer_remove_bfd_config(struct peer
*p
)
386 if (CHECK_FLAG(p
->sflags
, PEER_STATUS_GROUP
))
387 bgp_group_remove_bfd(p
);
389 bgp_peer_remove_bfd(p
);
393 * bgp_bfd_peer_config_write - Write the peer BFD configuration.
395 void bgp_bfd_peer_config_write(struct vty
*vty
, const struct peer
*peer
,
399 * Always show group BFD configuration, but peer only when explicitly
402 if ((!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)
403 && peer
->bfd_config
->manual
)
404 || CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
406 vty_out(vty
, " neighbor %s bfd\n", addr
);
408 vty_out(vty
, " neighbor %s bfd %d %d %d\n", addr
,
409 peer
->bfd_config
->detection_multiplier
,
410 peer
->bfd_config
->min_rx
, peer
->bfd_config
->min_tx
);
411 #endif /* HAVE_BFDD */
414 if (peer
->bfd_config
->profile
[0])
415 vty_out(vty
, " neighbor %s bfd profile %s\n", addr
,
416 peer
->bfd_config
->profile
);
418 if (peer
->bfd_config
->cbit
)
419 vty_out(vty
, " neighbor %s bfd check-control-plane-failure\n",
424 * bgp_bfd_show_info - Show the peer BFD information.
426 void bgp_bfd_show_info(struct vty
*vty
, const struct peer
*peer
,
427 json_object
*json_neigh
)
429 bfd_sess_show(vty
, json_neigh
, peer
->bfd_config
->session
);
434 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
437 "Enables BFD support\n")
442 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
444 return CMD_WARNING_CONFIG_FAILED
;
446 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
447 bgp_group_configure_bfd(peer
);
449 bgp_peer_configure_bfd(peer
, true);
451 bgp_peer_config_apply(peer
, peer
->group
);
460 #endif /* HAVE_BFDD */
462 neighbor_bfd_param_cmd
,
463 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd (2-255) (50-60000) (50-60000)",
466 "Enables BFD support\n"
467 "Detect Multiplier\n"
468 "Required min receive interval\n"
469 "Desired min transmit interval\n")
472 int idx_number_1
= 3;
473 int idx_number_2
= 4;
474 int idx_number_3
= 5;
475 long detection_multiplier
, min_rx
, min_tx
;
478 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
480 return CMD_WARNING_CONFIG_FAILED
;
482 detection_multiplier
= strtol(argv
[idx_number_1
]->arg
, NULL
, 10);
483 min_rx
= strtol(argv
[idx_number_2
]->arg
, NULL
, 10);
484 min_tx
= strtol(argv
[idx_number_3
]->arg
, NULL
, 10);
486 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
487 bgp_group_configure_bfd(peer
);
489 bgp_peer_configure_bfd(peer
, true);
491 peer
->bfd_config
->detection_multiplier
= detection_multiplier
;
492 peer
->bfd_config
->min_rx
= min_rx
;
493 peer
->bfd_config
->min_tx
= min_tx
;
494 bgp_peer_config_apply(peer
, peer
->group
);
499 DEFUN (neighbor_bfd_check_controlplane_failure
,
500 neighbor_bfd_check_controlplane_failure_cmd
,
501 "[no] neighbor <A.B.C.D|X:X::X:X|WORD> bfd check-control-plane-failure",
506 "Link dataplane status with BGP controlplane\n")
508 const char *no
= strmatch(argv
[0]->text
, "no") ? "no" : NULL
;
516 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
518 vty_out(vty
, "%% Specify remote-as or peer-group commands first\n");
519 return CMD_WARNING_CONFIG_FAILED
;
522 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
523 bgp_group_configure_bfd(peer
);
525 bgp_peer_configure_bfd(peer
, true);
527 peer
->bfd_config
->cbit
= no
== NULL
;
528 bgp_peer_config_apply(peer
, peer
->group
);
533 DEFUN (no_neighbor_bfd
,
536 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
538 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd [(2-255) (50-60000) (50-60000)]",
539 #endif /* HAVE_BFDD */
543 "Disables BFD support\n"
545 "Detect Multiplier\n"
546 "Required min receive interval\n"
547 "Desired min transmit interval\n"
548 #endif /* !HAVE_BFDD */
554 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
556 return CMD_WARNING_CONFIG_FAILED
;
558 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
559 bgp_group_remove_bfd(peer
);
561 bgp_peer_remove_bfd(peer
);
567 DEFUN(neighbor_bfd_profile
, neighbor_bfd_profile_cmd
,
568 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd profile BFDPROF",
573 BFD_PROFILE_NAME_STR
)
575 int idx_peer
= 1, idx_prof
= 4;
578 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
580 return CMD_WARNING_CONFIG_FAILED
;
582 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
583 bgp_group_configure_bfd(peer
);
585 bgp_peer_configure_bfd(peer
, true);
587 strlcpy(peer
->bfd_config
->profile
, argv
[idx_prof
]->arg
,
588 sizeof(peer
->bfd_config
->profile
));
589 bgp_peer_config_apply(peer
, peer
->group
);
594 DEFUN(no_neighbor_bfd_profile
, no_neighbor_bfd_profile_cmd
,
595 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd profile [BFDPROF]",
601 BFD_PROFILE_NAME_STR
)
606 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
608 return CMD_WARNING_CONFIG_FAILED
;
610 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
))
611 bgp_group_configure_bfd(peer
);
613 bgp_peer_configure_bfd(peer
, true);
615 peer
->bfd_config
->profile
[0] = 0;
616 bgp_peer_config_apply(peer
, peer
->group
);
620 #endif /* HAVE_BFDD */
622 void bgp_bfd_init(struct thread_master
*tm
)
624 /* Initialize BFD client functions */
625 bfd_protocol_integration_init(zclient
, tm
);
627 /* "neighbor bfd" commands. */
628 install_element(BGP_NODE
, &neighbor_bfd_cmd
);
629 install_element(BGP_NODE
, &neighbor_bfd_param_cmd
);
630 install_element(BGP_NODE
, &neighbor_bfd_check_controlplane_failure_cmd
);
631 install_element(BGP_NODE
, &no_neighbor_bfd_cmd
);
634 install_element(BGP_NODE
, &neighbor_bfd_profile_cmd
);
635 install_element(BGP_NODE
, &no_neighbor_bfd_profile_cmd
);
636 #endif /* HAVE_BFDD */