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
37 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_bfd.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_vty.h"
43 extern struct zclient
*zclient
;
46 * bgp_bfd_peer_group2peer_copy - Copy the BFD information from peer group
50 void bgp_bfd_peer_group2peer_copy(struct peer
*conf
, struct peer
*peer
)
52 struct bfd_info
*bfd_info
;
53 struct bfd_info
*conf_bfd_info
;
58 conf_bfd_info
= (struct bfd_info
*)conf
->bfd_info
;
60 peer
->bfd_info
= bfd_info_create();
62 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
64 /* Copy BFD parameter values */
65 bfd_info
->required_min_rx
= conf_bfd_info
->required_min_rx
;
66 bfd_info
->desired_min_tx
= conf_bfd_info
->desired_min_tx
;
67 bfd_info
->detect_mult
= conf_bfd_info
->detect_mult
;
68 bfd_info
->type
= conf_bfd_info
->type
;
72 * bgp_bfd_is_peer_multihop - returns whether BFD peer is multi-hop or single
75 int bgp_bfd_is_peer_multihop(struct peer
*peer
)
77 struct bfd_info
*bfd_info
;
79 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
84 if ((bfd_info
->type
== BFD_TYPE_MULTIHOP
)
85 || ((peer
->sort
== BGP_PEER_IBGP
) && !peer
->shared_network
)
86 || is_ebgp_multihop_configured(peer
))
93 * bgp_bfd_peer_sendmsg - Format and send a Peer register/Unregister
94 * command to Zebra to be forwarded to BFD
96 static void bgp_bfd_peer_sendmsg(struct peer
*peer
, int command
)
98 struct bfd_info
*bfd_info
;
102 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
104 vrf_id
= peer
->bgp
->vrf_id
;
106 if (command
== ZEBRA_BFD_DEST_DEREGISTER
) {
108 CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
109 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
111 multihop
= bgp_bfd_is_peer_multihop(peer
);
112 if ((command
== ZEBRA_BFD_DEST_REGISTER
) && multihop
)
113 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
116 if (peer
->su
.sa
.sa_family
== AF_INET
)
118 zclient
, bfd_info
, AF_INET
, &peer
->su
.sin
.sin_addr
,
119 (peer
->su_local
) ? &peer
->su_local
->sin
.sin_addr
: NULL
,
120 (peer
->nexthop
.ifp
) ? peer
->nexthop
.ifp
->name
: NULL
,
121 peer
->ttl
, multihop
, command
, 1, vrf_id
);
122 else if (peer
->su
.sa
.sa_family
== AF_INET6
)
124 zclient
, bfd_info
, AF_INET6
, &peer
->su
.sin6
.sin6_addr
,
125 (peer
->su_local
) ? &peer
->su_local
->sin6
.sin6_addr
127 (peer
->nexthop
.ifp
) ? peer
->nexthop
.ifp
->name
: NULL
,
128 peer
->ttl
, multihop
, command
, 1, vrf_id
);
132 * bgp_bfd_register_peer - register a peer with BFD through zebra
133 * for monitoring the peer rechahability.
135 void bgp_bfd_register_peer(struct peer
*peer
)
137 struct bfd_info
*bfd_info
;
141 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
143 /* Check if BFD is enabled and peer has already been registered with BFD
145 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
148 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
152 * bgp_bfd_deregister_peer - deregister a peer with BFD through zebra
153 * for stopping the monitoring of the peer
156 void bgp_bfd_deregister_peer(struct peer
*peer
)
158 struct bfd_info
*bfd_info
;
162 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
164 /* Check if BFD is eanbled and peer has not been registered */
165 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
168 bfd_info
->status
= BFD_STATUS_DOWN
;
169 bfd_info
->last_update
= bgp_clock();
171 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
175 * bgp_bfd_update_peer - update peer with BFD with new BFD paramters
178 static void bgp_bfd_update_peer(struct peer
*peer
)
180 struct bfd_info
*bfd_info
;
184 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
186 /* Check if the peer has been registered with BFD*/
187 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
190 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_UPDATE
);
194 * bgp_bfd_update_type - update session type with BFD through zebra.
196 static void bgp_bfd_update_type(struct peer
*peer
)
198 struct bfd_info
*bfd_info
;
203 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
205 /* Check if the peer has been registered with BFD*/
206 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
209 if (bfd_info
->type
== BFD_TYPE_NOT_CONFIGURED
) {
210 multihop
= bgp_bfd_is_peer_multihop(peer
);
212 && !CHECK_FLAG(bfd_info
->flags
,
213 BFD_FLAG_BFD_TYPE_MULTIHOP
))
214 || (!multihop
&& CHECK_FLAG(bfd_info
->flags
,
215 BFD_FLAG_BFD_TYPE_MULTIHOP
))) {
216 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
217 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
220 if ((bfd_info
->type
== BFD_TYPE_MULTIHOP
221 && !CHECK_FLAG(bfd_info
->flags
,
222 BFD_FLAG_BFD_TYPE_MULTIHOP
))
223 || (bfd_info
->type
== BFD_TYPE_SINGLEHOP
224 && CHECK_FLAG(bfd_info
->flags
,
225 BFD_FLAG_BFD_TYPE_MULTIHOP
))) {
226 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
227 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
233 * bgp_bfd_dest_replay - Replay all the peers that have BFD enabled
236 static int bgp_bfd_dest_replay(ZAPI_CALLBACK_ARGS
)
238 struct listnode
*mnode
, *node
, *nnode
;
242 if (BGP_DEBUG(zebra
, ZEBRA
))
243 zlog_debug("Zebra: BFD Dest replay request");
245 /* Send the client registration */
246 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
248 /* Replay the peer, if BFD is enabled in BGP */
250 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, mnode
, bgp
))
251 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
252 bgp_bfd_update_peer(peer
);
259 * bgp_bfd_peer_status_update - Update the BFD status if it has changed. Bring
260 * down the peer if the BFD session went down from
263 static void bgp_bfd_peer_status_update(struct peer
*peer
, int status
)
265 struct bfd_info
*bfd_info
;
268 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
270 if (bfd_info
->status
== status
)
273 old_status
= bfd_info
->status
;
274 bfd_info
->status
= status
;
275 bfd_info
->last_update
= bgp_clock();
277 if (status
!= old_status
) {
278 if (BGP_DEBUG(neighbor_events
, NEIGHBOR_EVENTS
))
279 zlog_debug("[%s]: BFD %s", peer
->host
,
280 bfd_get_status_str(status
));
282 if ((status
== BFD_STATUS_DOWN
) && (old_status
== BFD_STATUS_UP
)) {
283 peer
->last_reset
= PEER_DOWN_BFD_DOWN
;
284 BGP_EVENT_ADD(peer
, BGP_Stop
);
286 if ((status
== BFD_STATUS_UP
) && (old_status
== BFD_STATUS_DOWN
)
287 && peer
->status
!= Established
) {
288 if (!BGP_PEER_START_SUPPRESSED(peer
)) {
289 bgp_fsm_event_update(peer
, 1);
290 BGP_EVENT_ADD(peer
, BGP_Start
);
296 * bgp_bfd_dest_update - Find the peer for which the BFD status
297 * has changed and bring down the peer
298 * connectivity if the BFD session went down.
300 static int bgp_bfd_dest_update(ZAPI_CALLBACK_ARGS
)
302 struct interface
*ifp
;
307 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &status
, vrf_id
);
309 if (BGP_DEBUG(zebra
, ZEBRA
)) {
310 char buf
[2][PREFIX2STR_BUFFER
];
311 prefix2str(&dp
, buf
[0], sizeof(buf
[0]));
314 "Zebra: vrf %u interface %s bfd destination %s %s",
315 vrf_id
, ifp
->name
, buf
[0],
316 bfd_get_status_str(status
));
318 prefix2str(&sp
, buf
[1], sizeof(buf
[1]));
320 "Zebra: vrf %u source %s bfd destination %s %s",
321 vrf_id
, buf
[1], buf
[0],
322 bfd_get_status_str(status
));
326 /* Bring the peer down if BFD is enabled in BGP */
328 struct listnode
*mnode
, *node
, *nnode
;
332 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, mnode
, bgp
))
333 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
337 if ((dp
.family
== AF_INET
)
338 && (peer
->su
.sa
.sa_family
== AF_INET
)) {
339 if (dp
.u
.prefix4
.s_addr
340 != peer
->su
.sin
.sin_addr
.s_addr
)
342 } else if ((dp
.family
== AF_INET6
)
343 && (peer
->su
.sa
.sa_family
345 if (memcmp(&dp
.u
.prefix6
,
346 &peer
->su
.sin6
.sin6_addr
,
347 sizeof(struct in6_addr
)))
352 if (ifp
&& (ifp
== peer
->nexthop
.ifp
)) {
353 bgp_bfd_peer_status_update(peer
,
359 if ((sp
.family
== AF_INET
)
360 && (peer
->su_local
->sa
.sa_family
362 if (sp
.u
.prefix4
.s_addr
363 != peer
->su_local
->sin
366 } else if ((sp
.family
== AF_INET6
)
367 && (peer
->su_local
->sa
370 if (memcmp(&sp
.u
.prefix6
,
371 &peer
->su_local
->sin6
379 if ((vrf_id
!= VRF_DEFAULT
)
380 && (peer
->bgp
->vrf_id
!= vrf_id
))
383 bgp_bfd_peer_status_update(peer
,
393 * bgp_bfd_peer_param_set - Set the configured BFD paramter values for peer.
395 static int bgp_bfd_peer_param_set(struct peer
*peer
, uint32_t min_rx
,
396 uint32_t min_tx
, uint8_t detect_mult
,
399 struct peer_group
*group
;
400 struct listnode
*node
, *nnode
;
403 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
), min_rx
, min_tx
,
404 detect_mult
, defaults
, &command
);
406 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
408 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
410 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
),
411 min_rx
, min_tx
, detect_mult
, defaults
,
414 if ((peer
->status
== Established
)
415 && (command
== ZEBRA_BFD_DEST_REGISTER
))
416 bgp_bfd_register_peer(peer
);
417 else if (command
== ZEBRA_BFD_DEST_UPDATE
)
418 bgp_bfd_update_peer(peer
);
421 if ((peer
->status
== Established
)
422 && (command
== ZEBRA_BFD_DEST_REGISTER
))
423 bgp_bfd_register_peer(peer
);
424 else if (command
== ZEBRA_BFD_DEST_UPDATE
)
425 bgp_bfd_update_peer(peer
);
431 * bgp_bfd_peer_param_unset - Delete the configured BFD paramter values for
434 static int bgp_bfd_peer_param_unset(struct peer
*peer
)
436 struct peer_group
*group
;
437 struct listnode
*node
, *nnode
;
442 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
443 bfd_info_free(&(peer
->bfd_info
));
445 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
446 bgp_bfd_deregister_peer(peer
);
447 bfd_info_free(&(peer
->bfd_info
));
450 bgp_bfd_deregister_peer(peer
);
451 bfd_info_free(&(peer
->bfd_info
));
457 * bgp_bfd_peer_param_type_set - set the BFD session type (multihop or
460 static int bgp_bfd_peer_param_type_set(struct peer
*peer
,
461 enum bfd_sess_type type
)
463 struct peer_group
*group
;
464 struct listnode
*node
, *nnode
;
466 struct bfd_info
*bfd_info
;
469 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
),
470 BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
471 BFD_DEF_DETECT_MULT
, 1, &command
);
473 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
474 bfd_info
->type
= type
;
476 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
478 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
482 (struct bfd_info
**)&(peer
->bfd_info
),
483 BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
484 BFD_DEF_DETECT_MULT
, 1, &command
);
486 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
487 bfd_info
->type
= type
;
489 if (peer
->status
== Established
) {
490 if (command
== ZEBRA_BFD_DEST_REGISTER
)
491 bgp_bfd_register_peer(peer
);
493 bgp_bfd_update_type(peer
);
497 if (peer
->status
== Established
) {
498 if (command
== ZEBRA_BFD_DEST_REGISTER
)
499 bgp_bfd_register_peer(peer
);
501 bgp_bfd_update_type(peer
);
509 * bgp_bfd_peer_config_write - Write the peer BFD configuration.
511 void bgp_bfd_peer_config_write(struct vty
*vty
, struct peer
*peer
, char *addr
)
513 struct bfd_info
*bfd_info
;
518 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
520 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
522 vty_out(vty
, " neighbor %s bfd\n", addr
);
524 vty_out(vty
, " neighbor %s bfd %d %d %d\n", addr
,
525 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
526 bfd_info
->desired_min_tx
);
527 #endif /* HAVE_BFDD */
529 if (bfd_info
->type
!= BFD_TYPE_NOT_CONFIGURED
)
530 vty_out(vty
, " neighbor %s bfd %s\n", addr
,
531 (bfd_info
->type
== BFD_TYPE_MULTIHOP
) ? "multihop"
534 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
)
535 && (bfd_info
->type
== BFD_TYPE_NOT_CONFIGURED
))
536 vty_out(vty
, " neighbor %s bfd\n", addr
);
540 * bgp_bfd_show_info - Show the peer BFD information.
542 void bgp_bfd_show_info(struct vty
*vty
, struct peer
*peer
, bool use_json
,
543 json_object
*json_neigh
)
545 bfd_show_info(vty
, (struct bfd_info
*)peer
->bfd_info
,
546 bgp_bfd_is_peer_multihop(peer
), 0, use_json
, json_neigh
);
551 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
554 "Enables BFD support\n")
560 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
562 return CMD_WARNING_CONFIG_FAILED
;
564 ret
= bgp_bfd_peer_param_set(peer
, BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
565 BFD_DEF_DETECT_MULT
, 1);
567 return bgp_vty_return(vty
, ret
);
576 #endif /* HAVE_BFDD */
578 neighbor_bfd_param_cmd
,
579 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd (2-255) (50-60000) (50-60000)",
582 "Enables BFD support\n"
583 "Detect Multiplier\n"
584 "Required min receive interval\n"
585 "Desired min transmit interval\n")
588 int idx_number_1
= 3;
589 int idx_number_2
= 4;
590 int idx_number_3
= 5;
597 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
599 return CMD_WARNING_CONFIG_FAILED
;
601 if ((ret
= bfd_validate_param(
602 vty
, argv
[idx_number_1
]->arg
, argv
[idx_number_2
]->arg
,
603 argv
[idx_number_3
]->arg
, &dm_val
, &rx_val
, &tx_val
))
607 ret
= bgp_bfd_peer_param_set(peer
, rx_val
, tx_val
, dm_val
, 0);
609 return bgp_vty_return(vty
, ret
);
614 DEFUN_HIDDEN (neighbor_bfd_type
,
615 neighbor_bfd_type_cmd
,
616 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd <multihop|singlehop>",
619 "Enables BFD support\n"
621 "Single hop session\n")
626 enum bfd_sess_type type
;
629 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
631 return CMD_WARNING_CONFIG_FAILED
;
633 if (strmatch(argv
[idx_hop
]->text
, "singlehop"))
634 type
= BFD_TYPE_SINGLEHOP
;
635 else if (strmatch(argv
[idx_hop
]->text
, "multihop"))
636 type
= BFD_TYPE_MULTIHOP
;
638 return CMD_WARNING_CONFIG_FAILED
;
640 ret
= bgp_bfd_peer_param_type_set(peer
, type
);
642 return bgp_vty_return(vty
, ret
);
647 DEFUN (no_neighbor_bfd
,
650 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
652 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd [(2-255) (50-60000) (50-60000)]",
653 #endif /* HAVE_BFDD */
657 "Disables BFD support\n"
659 "Detect Multiplier\n"
660 "Required min receive interval\n"
661 "Desired min transmit interval\n"
662 #endif /* !HAVE_BFDD */
669 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
671 return CMD_WARNING_CONFIG_FAILED
;
673 ret
= bgp_bfd_peer_param_unset(peer
);
675 return bgp_vty_return(vty
, ret
);
681 DEFUN_HIDDEN (no_neighbor_bfd_type
,
682 no_neighbor_bfd_type_cmd
,
683 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd <multihop|singlehop>",
687 "Disables BFD support\n"
689 "Singlehop session\n")
695 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
697 return CMD_WARNING_CONFIG_FAILED
;
702 ret
= bgp_bfd_peer_param_type_set(peer
, BFD_TYPE_NOT_CONFIGURED
);
704 return bgp_vty_return(vty
, ret
);
709 void bgp_bfd_init(void)
713 /* Initialize BFD client functions */
714 zclient
->interface_bfd_dest_update
= bgp_bfd_dest_update
;
715 zclient
->bfd_dest_replay
= bgp_bfd_dest_replay
;
717 /* "neighbor bfd" commands. */
718 install_element(BGP_NODE
, &neighbor_bfd_cmd
);
719 install_element(BGP_NODE
, &neighbor_bfd_param_cmd
);
720 install_element(BGP_NODE
, &neighbor_bfd_type_cmd
);
721 install_element(BGP_NODE
, &no_neighbor_bfd_cmd
);
722 install_element(BGP_NODE
, &no_neighbor_bfd_type_cmd
);