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
;
99 vrf_id_t vrf_id
= VRF_DEFAULT
;
102 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
104 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
)
105 vrf_id
= peer
->bgp
->vrf_id
;
107 if (command
== ZEBRA_BFD_DEST_DEREGISTER
) {
109 CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
110 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
112 multihop
= bgp_bfd_is_peer_multihop(peer
);
113 if ((command
== ZEBRA_BFD_DEST_REGISTER
) && multihop
)
114 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
117 if (peer
->su
.sa
.sa_family
== AF_INET
)
119 zclient
, bfd_info
, AF_INET
, &peer
->su
.sin
.sin_addr
,
120 (peer
->su_local
) ? &peer
->su_local
->sin
.sin_addr
: NULL
,
121 (peer
->nexthop
.ifp
) ? peer
->nexthop
.ifp
->name
: NULL
,
122 peer
->ttl
, multihop
, command
, 1, vrf_id
);
123 else if (peer
->su
.sa
.sa_family
== AF_INET6
)
125 zclient
, bfd_info
, AF_INET6
, &peer
->su
.sin6
.sin6_addr
,
126 (peer
->su_local
) ? &peer
->su_local
->sin6
.sin6_addr
128 (peer
->nexthop
.ifp
) ? peer
->nexthop
.ifp
->name
: NULL
,
129 peer
->ttl
, multihop
, command
, 1, vrf_id
);
133 * bgp_bfd_register_peer - register a peer with BFD through zebra
134 * for monitoring the peer rechahability.
136 void bgp_bfd_register_peer(struct peer
*peer
)
138 struct bfd_info
*bfd_info
;
142 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
144 /* Check if BFD is enabled and peer has already been registered with BFD
146 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
149 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
153 * bgp_bfd_deregister_peer - deregister a peer with BFD through zebra
154 * for stopping the monitoring of the peer
157 void bgp_bfd_deregister_peer(struct peer
*peer
)
159 struct bfd_info
*bfd_info
;
163 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
165 /* Check if BFD is eanbled and peer has not been registered */
166 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
169 bfd_info
->status
= BFD_STATUS_DOWN
;
170 bfd_info
->last_update
= bgp_clock();
172 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
176 * bgp_bfd_update_peer - update peer with BFD with new BFD paramters
179 static void bgp_bfd_update_peer(struct peer
*peer
)
181 struct bfd_info
*bfd_info
;
185 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
187 /* Check if the peer has been registered with BFD*/
188 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
191 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_UPDATE
);
195 * bgp_bfd_update_type - update session type with BFD through zebra.
197 static void bgp_bfd_update_type(struct peer
*peer
)
199 struct bfd_info
*bfd_info
;
204 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
206 /* Check if the peer has been registered with BFD*/
207 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
210 if (bfd_info
->type
== BFD_TYPE_NOT_CONFIGURED
) {
211 multihop
= bgp_bfd_is_peer_multihop(peer
);
213 && !CHECK_FLAG(bfd_info
->flags
,
214 BFD_FLAG_BFD_TYPE_MULTIHOP
))
215 || (!multihop
&& CHECK_FLAG(bfd_info
->flags
,
216 BFD_FLAG_BFD_TYPE_MULTIHOP
))) {
217 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
218 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
221 if ((bfd_info
->type
== BFD_TYPE_MULTIHOP
222 && !CHECK_FLAG(bfd_info
->flags
,
223 BFD_FLAG_BFD_TYPE_MULTIHOP
))
224 || (bfd_info
->type
== BFD_TYPE_SINGLEHOP
225 && CHECK_FLAG(bfd_info
->flags
,
226 BFD_FLAG_BFD_TYPE_MULTIHOP
))) {
227 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
228 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
234 * bgp_bfd_dest_replay - Replay all the peers that have BFD enabled
237 static int bgp_bfd_dest_replay(int command
, struct zclient
*client
,
238 zebra_size_t length
, vrf_id_t vrf_id
)
240 struct listnode
*mnode
, *node
, *nnode
;
244 if (BGP_DEBUG(zebra
, ZEBRA
))
245 zlog_debug("Zebra: BFD Dest replay request");
247 /* Send the client registration */
248 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
250 /* Replay the peer, if BFD is enabled in BGP */
252 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, mnode
, bgp
))
253 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
254 bgp_bfd_update_peer(peer
);
261 * bgp_bfd_peer_status_update - Update the BFD status if it has changed. Bring
262 * down the peer if the BFD session went down from
265 static void bgp_bfd_peer_status_update(struct peer
*peer
, int status
)
267 struct bfd_info
*bfd_info
;
270 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
272 if (bfd_info
->status
== status
)
275 old_status
= bfd_info
->status
;
276 bfd_info
->status
= status
;
277 bfd_info
->last_update
= bgp_clock();
279 if ((status
== BFD_STATUS_DOWN
) && (old_status
== BFD_STATUS_UP
)) {
280 peer
->last_reset
= PEER_DOWN_BFD_DOWN
;
281 BGP_EVENT_ADD(peer
, BGP_Stop
);
286 * bgp_bfd_dest_update - Find the peer for which the BFD status
287 * has changed and bring down the peer
288 * connectivity if the BFD session went down.
290 static int bgp_bfd_dest_update(int command
, struct zclient
*zclient
,
291 zebra_size_t length
, vrf_id_t vrf_id
)
293 struct interface
*ifp
;
298 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &status
, vrf_id
);
300 if (BGP_DEBUG(zebra
, ZEBRA
)) {
301 char buf
[2][PREFIX2STR_BUFFER
];
302 prefix2str(&dp
, buf
[0], sizeof(buf
[0]));
305 "Zebra: vrf %u interface %s bfd destination %s %s",
306 vrf_id
, ifp
->name
, buf
[0],
307 bfd_get_status_str(status
));
309 prefix2str(&sp
, buf
[1], sizeof(buf
[1]));
311 "Zebra: vrf %u source %s bfd destination %s %s",
312 vrf_id
, buf
[1], buf
[0],
313 bfd_get_status_str(status
));
317 /* Bring the peer down if BFD is enabled in BGP */
319 struct listnode
*mnode
, *node
, *nnode
;
323 for (ALL_LIST_ELEMENTS_RO(bm
->bgp
, mnode
, bgp
))
324 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
328 if ((dp
.family
== AF_INET
)
329 && (peer
->su
.sa
.sa_family
== AF_INET
)) {
330 if (dp
.u
.prefix4
.s_addr
331 != peer
->su
.sin
.sin_addr
.s_addr
)
333 } else if ((dp
.family
== AF_INET6
)
334 && (peer
->su
.sa
.sa_family
336 if (memcmp(&dp
.u
.prefix6
,
337 &peer
->su
.sin6
.sin6_addr
,
338 sizeof(struct in6_addr
)))
343 if (ifp
&& (ifp
== peer
->nexthop
.ifp
)) {
344 bgp_bfd_peer_status_update(peer
,
350 if ((sp
.family
== AF_INET
)
351 && (peer
->su_local
->sa
.sa_family
353 if (sp
.u
.prefix4
.s_addr
354 != peer
->su_local
->sin
357 } else if ((sp
.family
== AF_INET6
)
358 && (peer
->su_local
->sa
361 if (memcmp(&sp
.u
.prefix6
,
362 &peer
->su_local
->sin6
370 if ((vrf_id
!= VRF_DEFAULT
)
371 && (peer
->bgp
->vrf_id
!= vrf_id
))
374 bgp_bfd_peer_status_update(peer
,
384 * bgp_bfd_peer_param_set - Set the configured BFD paramter values for peer.
386 static int bgp_bfd_peer_param_set(struct peer
*peer
, uint32_t min_rx
,
387 uint32_t min_tx
, uint8_t detect_mult
,
390 struct peer_group
*group
;
391 struct listnode
*node
, *nnode
;
394 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
), min_rx
, min_tx
,
395 detect_mult
, defaults
, &command
);
397 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
399 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
401 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
),
402 min_rx
, min_tx
, detect_mult
, defaults
,
405 if ((peer
->status
== Established
)
406 && (command
== ZEBRA_BFD_DEST_REGISTER
))
407 bgp_bfd_register_peer(peer
);
408 else if (command
== ZEBRA_BFD_DEST_UPDATE
)
409 bgp_bfd_update_peer(peer
);
412 if ((peer
->status
== Established
)
413 && (command
== ZEBRA_BFD_DEST_REGISTER
))
414 bgp_bfd_register_peer(peer
);
415 else if (command
== ZEBRA_BFD_DEST_UPDATE
)
416 bgp_bfd_update_peer(peer
);
422 * bgp_bfd_peer_param_unset - Delete the configured BFD paramter values for
425 static int bgp_bfd_peer_param_unset(struct peer
*peer
)
427 struct peer_group
*group
;
428 struct listnode
*node
, *nnode
;
433 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
434 bfd_info_free(&(peer
->bfd_info
));
436 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
437 bgp_bfd_deregister_peer(peer
);
438 bfd_info_free(&(peer
->bfd_info
));
441 bgp_bfd_deregister_peer(peer
);
442 bfd_info_free(&(peer
->bfd_info
));
448 * bgp_bfd_peer_param_type_set - set the BFD session type (multihop or
451 static int bgp_bfd_peer_param_type_set(struct peer
*peer
,
452 enum bfd_sess_type type
)
454 struct peer_group
*group
;
455 struct listnode
*node
, *nnode
;
457 struct bfd_info
*bfd_info
;
460 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
),
461 BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
462 BFD_DEF_DETECT_MULT
, 1, &command
);
464 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
465 bfd_info
->type
= type
;
467 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
469 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
473 (struct bfd_info
**)&(peer
->bfd_info
),
474 BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
475 BFD_DEF_DETECT_MULT
, 1, &command
);
477 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
478 bfd_info
->type
= type
;
480 if (peer
->status
== Established
) {
481 if (command
== ZEBRA_BFD_DEST_REGISTER
)
482 bgp_bfd_register_peer(peer
);
484 bgp_bfd_update_type(peer
);
488 if (peer
->status
== Established
) {
489 if (command
== ZEBRA_BFD_DEST_REGISTER
)
490 bgp_bfd_register_peer(peer
);
492 bgp_bfd_update_type(peer
);
500 * bgp_bfd_peer_config_write - Write the peer BFD configuration.
502 void bgp_bfd_peer_config_write(struct vty
*vty
, struct peer
*peer
, char *addr
)
504 struct bfd_info
*bfd_info
;
509 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
511 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
513 vty_out(vty
, " neighbor %s bfd\n", addr
);
515 vty_out(vty
, " neighbor %s bfd %d %d %d\n", addr
,
516 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
517 bfd_info
->desired_min_tx
);
518 #endif /* HAVE_BFDD */
520 if (bfd_info
->type
!= BFD_TYPE_NOT_CONFIGURED
)
521 vty_out(vty
, " neighbor %s bfd %s\n", addr
,
522 (bfd_info
->type
== BFD_TYPE_MULTIHOP
) ? "multihop"
525 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
)
526 && (bfd_info
->type
== BFD_TYPE_NOT_CONFIGURED
))
527 vty_out(vty
, " neighbor %s bfd\n", addr
);
531 * bgp_bfd_show_info - Show the peer BFD information.
533 void bgp_bfd_show_info(struct vty
*vty
, struct peer
*peer
, uint8_t use_json
,
534 json_object
*json_neigh
)
536 bfd_show_info(vty
, (struct bfd_info
*)peer
->bfd_info
,
537 bgp_bfd_is_peer_multihop(peer
), 0, use_json
, json_neigh
);
542 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
545 "Enables BFD support\n")
551 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
553 return CMD_WARNING_CONFIG_FAILED
;
555 ret
= bgp_bfd_peer_param_set(peer
, BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
556 BFD_DEF_DETECT_MULT
, 1);
558 return bgp_vty_return(vty
, ret
);
567 #endif /* HAVE_BFDD */
569 neighbor_bfd_param_cmd
,
570 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd (2-255) (50-60000) (50-60000)",
573 "Enables BFD support\n"
574 "Detect Multiplier\n"
575 "Required min receive interval\n"
576 "Desired min transmit interval\n")
579 int idx_number_1
= 3;
580 int idx_number_2
= 4;
581 int idx_number_3
= 5;
588 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
590 return CMD_WARNING_CONFIG_FAILED
;
592 if ((ret
= bfd_validate_param(
593 vty
, argv
[idx_number_1
]->arg
, argv
[idx_number_2
]->arg
,
594 argv
[idx_number_3
]->arg
, &dm_val
, &rx_val
, &tx_val
))
598 ret
= bgp_bfd_peer_param_set(peer
, rx_val
, tx_val
, dm_val
, 0);
600 return bgp_vty_return(vty
, ret
);
605 DEFUN_HIDDEN (neighbor_bfd_type
,
606 neighbor_bfd_type_cmd
,
607 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd <multihop|singlehop>",
610 "Enables BFD support\n"
612 "Single hop session\n")
617 enum bfd_sess_type type
;
620 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
622 return CMD_WARNING_CONFIG_FAILED
;
624 if (strmatch(argv
[idx_hop
]->text
, "singlehop"))
625 type
= BFD_TYPE_SINGLEHOP
;
626 else if (strmatch(argv
[idx_hop
]->text
, "multihop"))
627 type
= BFD_TYPE_MULTIHOP
;
629 return CMD_WARNING_CONFIG_FAILED
;
631 ret
= bgp_bfd_peer_param_type_set(peer
, type
);
633 return bgp_vty_return(vty
, ret
);
638 DEFUN (no_neighbor_bfd
,
641 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
643 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd [(2-255) (50-60000) (50-60000)]",
644 #endif /* HAVE_BFDD */
648 "Disables BFD support\n"
650 "Detect Multiplier\n"
651 "Required min receive interval\n"
652 "Desired min transmit interval\n"
653 #endif /* !HAVE_BFDD */
660 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
662 return CMD_WARNING_CONFIG_FAILED
;
664 ret
= bgp_bfd_peer_param_unset(peer
);
666 return bgp_vty_return(vty
, ret
);
672 DEFUN_HIDDEN (no_neighbor_bfd_type
,
673 no_neighbor_bfd_type_cmd
,
674 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd <multihop|singlehop>",
678 "Disables BFD support\n"
680 "Singlehop session\n")
686 peer
= peer_and_group_lookup_vty(vty
, argv
[idx_peer
]->arg
);
688 return CMD_WARNING_CONFIG_FAILED
;
693 ret
= bgp_bfd_peer_param_type_set(peer
, BFD_TYPE_NOT_CONFIGURED
);
695 return bgp_vty_return(vty
, ret
);
700 void bgp_bfd_init(void)
704 /* Initialize BFD client functions */
705 zclient
->interface_bfd_dest_update
= bgp_bfd_dest_update
;
706 zclient
->bfd_dest_replay
= bgp_bfd_dest_replay
;
708 /* "neighbor bfd" commands. */
709 install_element(BGP_NODE
, &neighbor_bfd_cmd
);
710 install_element(BGP_NODE
, &neighbor_bfd_param_cmd
);
711 install_element(BGP_NODE
, &neighbor_bfd_type_cmd
);
712 install_element(BGP_NODE
, &no_neighbor_bfd_cmd
);
713 install_element(BGP_NODE
, &no_neighbor_bfd_type_cmd
);