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
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
39 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_bfd.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_vty.h"
45 extern struct zclient
*zclient
;
48 * bgp_bfd_peer_group2peer_copy - Copy the BFD information from peer group template
52 bgp_bfd_peer_group2peer_copy(struct peer
*conf
, struct peer
*peer
)
54 struct bfd_info
*bfd_info
;
55 struct bfd_info
*conf_bfd_info
;
60 conf_bfd_info
= (struct bfd_info
*)conf
->bfd_info
;
62 peer
->bfd_info
= bfd_info_create();
64 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
66 /* Copy BFD parameter values */
67 bfd_info
->required_min_rx
= conf_bfd_info
->required_min_rx
;
68 bfd_info
->desired_min_tx
= conf_bfd_info
->desired_min_tx
;
69 bfd_info
->detect_mult
= conf_bfd_info
->detect_mult
;
70 bfd_info
->type
= conf_bfd_info
->type
;
74 * bgp_bfd_is_peer_multihop - returns whether BFD peer is multi-hop or single hop.
77 bgp_bfd_is_peer_multihop(struct peer
*peer
)
79 struct bfd_info
*bfd_info
;
81 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
86 if((bfd_info
->type
== BFD_TYPE_MULTIHOP
) ||
87 ((peer
->sort
== BGP_PEER_IBGP
) && !peer
->shared_network
) ||
88 is_ebgp_multihop_configured(peer
))
95 * bgp_bfd_peer_sendmsg - Format and send a Peer register/Unregister
96 * command to Zebra to be forwarded to BFD
99 bgp_bfd_peer_sendmsg (struct peer
*peer
, int command
)
101 struct bfd_info
*bfd_info
;
102 vrf_id_t vrf_id
= VRF_DEFAULT
;
105 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
107 if (peer
->bgp
&& (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VRF
))
108 vrf_id
= peer
->bgp
->vrf_id
;
110 if (command
== ZEBRA_BFD_DEST_DEREGISTER
)
112 multihop
= CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
113 UNSET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
117 multihop
= bgp_bfd_is_peer_multihop(peer
);
118 if ((command
== ZEBRA_BFD_DEST_REGISTER
) && multihop
)
119 SET_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
);
122 if (peer
->su
.sa
.sa_family
== AF_INET
)
123 bfd_peer_sendmsg (zclient
, bfd_info
, AF_INET
,
124 &peer
->su
.sin
.sin_addr
,
125 (peer
->su_local
) ? &peer
->su_local
->sin
.sin_addr
: NULL
,
126 (peer
->nexthop
.ifp
) ? peer
->nexthop
.ifp
->name
: NULL
,
127 peer
->ttl
, multihop
, command
, 1, vrf_id
);
128 else if (peer
->su
.sa
.sa_family
== AF_INET6
)
129 bfd_peer_sendmsg (zclient
, bfd_info
, AF_INET6
,
130 &peer
->su
.sin6
.sin6_addr
,
131 (peer
->su_local
) ? &peer
->su_local
->sin6
.sin6_addr
: NULL
,
132 (peer
->nexthop
.ifp
) ? peer
->nexthop
.ifp
->name
: NULL
,
133 peer
->ttl
, multihop
, command
, 1, vrf_id
);
137 * bgp_bfd_register_peer - register a peer with BFD through zebra
138 * for monitoring the peer rechahability.
141 bgp_bfd_register_peer (struct peer
*peer
)
143 struct bfd_info
*bfd_info
;
147 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
149 /* Check if BFD is enabled and peer has already been registered with BFD */
150 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
153 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
157 * bgp_bfd_deregister_peer - deregister a peer with BFD through zebra
158 * for stopping the monitoring of the peer
162 bgp_bfd_deregister_peer (struct peer
*peer
)
164 struct bfd_info
*bfd_info
;
168 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
170 /* Check if BFD is eanbled and peer has not been registered */
171 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
174 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
178 * bgp_bfd_update_peer - update peer with BFD with new BFD paramters
182 bgp_bfd_update_peer (struct peer
*peer
)
184 struct bfd_info
*bfd_info
;
188 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
190 /* Check if the peer has been registered with BFD*/
191 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
194 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_UPDATE
);
198 * bgp_bfd_update_type - update session type with BFD through zebra.
201 bgp_bfd_update_type (struct peer
*peer
)
203 struct bfd_info
*bfd_info
;
208 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
210 /* Check if the peer has been registered with BFD*/
211 if (!CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_REG
))
214 if (bfd_info
->type
== BFD_TYPE_NOT_CONFIGURED
)
216 multihop
= bgp_bfd_is_peer_multihop(peer
);
217 if ((multihop
&& !CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
)) ||
218 (!multihop
&& CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
)))
220 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
221 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
226 if ((bfd_info
->type
== BFD_TYPE_MULTIHOP
&&
227 !CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
)) ||
228 (bfd_info
->type
== BFD_TYPE_SINGLEHOP
&&
229 CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_BFD_TYPE_MULTIHOP
)))
231 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_DEREGISTER
);
232 bgp_bfd_peer_sendmsg(peer
, ZEBRA_BFD_DEST_REGISTER
);
238 * bgp_bfd_dest_replay - Replay all the peers that have BFD enabled
242 bgp_bfd_dest_replay (int command
, struct zclient
*client
, zebra_size_t length
,
245 struct listnode
*mnode
, *node
, *nnode
;
249 if (BGP_DEBUG (zebra
, ZEBRA
))
250 zlog_debug("Zebra: BFD Dest replay request");
252 /* Send the client registration */
253 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
255 /* Replay the peer, if BFD is enabled in BGP */
257 for (ALL_LIST_ELEMENTS_RO (bm
->bgp
, mnode
, bgp
))
258 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
260 bgp_bfd_update_peer(peer
);
267 * bgp_bfd_peer_status_update - Update the BFD status if it has changed. Bring
268 * down the peer if the BFD session went down from * up.
271 bgp_bfd_peer_status_update (struct peer
*peer
, int status
)
273 struct bfd_info
*bfd_info
;
276 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
278 if (bfd_info
->status
== status
)
281 old_status
= bfd_info
->status
;
282 bfd_info
->status
= status
;
283 bfd_info
->last_update
= bgp_clock();
285 if ((status
== BFD_STATUS_DOWN
) && (old_status
== BFD_STATUS_UP
))
287 peer
->last_reset
= PEER_DOWN_BFD_DOWN
;
288 BGP_EVENT_ADD (peer
, BGP_Stop
);
293 * bgp_bfd_dest_update - Find the peer for which the BFD status
294 * has changed and bring down the peer
295 * connectivity if the BFD session went down.
298 bgp_bfd_dest_update (int command
, struct zclient
*zclient
,
299 zebra_size_t length
, vrf_id_t vrf_id
)
301 struct interface
*ifp
;
306 ifp
= bfd_get_peer_info (zclient
->ibuf
, &dp
, &sp
, &status
, vrf_id
);
308 if (BGP_DEBUG (zebra
, ZEBRA
))
310 char buf
[2][PREFIX2STR_BUFFER
];
311 prefix2str(&dp
, buf
[0], sizeof(buf
[0]));
314 zlog_debug("Zebra: interface %s bfd destination %s %s",
315 ifp
->name
, buf
[0], bfd_get_status_str(status
));
319 prefix2str(&sp
, buf
[1], sizeof(buf
[1]));
320 zlog_debug("Zebra: source %s bfd destination %s %s",
321 buf
[1], buf
[0], bfd_get_status_str(status
));
325 /* Bring the peer down if BFD is enabled in BGP */
327 struct listnode
*mnode
, *node
, *nnode
;
331 for (ALL_LIST_ELEMENTS_RO (bm
->bgp
, mnode
, bgp
))
332 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
337 if ((dp
.family
== AF_INET
) && (peer
->su
.sa
.sa_family
== AF_INET
))
339 if (dp
.u
.prefix4
.s_addr
!= peer
->su
.sin
.sin_addr
.s_addr
)
342 else if ((dp
.family
== AF_INET6
) &&
343 (peer
->su
.sa
.sa_family
== AF_INET6
))
345 if (memcmp(&dp
.u
.prefix6
, &peer
->su
.sin6
.sin6_addr
,
346 sizeof (struct in6_addr
)))
352 if (ifp
&& (ifp
== peer
->nexthop
.ifp
))
354 bgp_bfd_peer_status_update(peer
, status
);
361 if ((sp
.family
== AF_INET
) &&
362 (peer
->su_local
->sa
.sa_family
== AF_INET
))
364 if (sp
.u
.prefix4
.s_addr
!= peer
->su_local
->sin
.sin_addr
.s_addr
)
367 else if ((sp
.family
== AF_INET6
) &&
368 (peer
->su_local
->sa
.sa_family
== AF_INET6
))
370 if (memcmp(&sp
.u
.prefix6
, &peer
->su_local
->sin6
.sin6_addr
,
371 sizeof (struct in6_addr
)))
377 if ((vrf_id
!= VRF_DEFAULT
) && (peer
->bgp
->vrf_id
!= vrf_id
))
380 bgp_bfd_peer_status_update(peer
, status
);
389 * bgp_bfd_peer_param_set - Set the configured BFD paramter values for peer.
392 bgp_bfd_peer_param_set (struct peer
*peer
, u_int32_t min_rx
, u_int32_t min_tx
,
393 u_int8_t detect_mult
, int defaults
)
395 struct peer_group
*group
;
396 struct listnode
*node
, *nnode
;
399 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
), min_rx
, min_tx
,
400 detect_mult
, defaults
, &command
);
402 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_GROUP
))
405 for (ALL_LIST_ELEMENTS (group
->peer
, node
, nnode
, peer
))
408 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
), min_rx
, min_tx
,
409 detect_mult
, defaults
, &command
);
411 if ((peer
->status
== Established
) &&
412 (command
== ZEBRA_BFD_DEST_REGISTER
))
413 bgp_bfd_register_peer(peer
);
414 else if (command
== ZEBRA_BFD_DEST_UPDATE
)
415 bgp_bfd_update_peer(peer
);
420 if ((peer
->status
== Established
) &&
421 (command
== ZEBRA_BFD_DEST_REGISTER
))
422 bgp_bfd_register_peer(peer
);
423 else if (command
== ZEBRA_BFD_DEST_UPDATE
)
424 bgp_bfd_update_peer(peer
);
430 * bgp_bfd_peer_param_unset - Delete the configured BFD paramter values for peer.
433 bgp_bfd_peer_param_unset (struct peer
*peer
)
435 struct peer_group
*group
;
436 struct listnode
*node
, *nnode
;
441 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
))
447 bgp_bfd_deregister_peer(peer
);
448 bfd_info_free(&(peer
->bfd_info
));
453 bgp_bfd_deregister_peer(peer
);
454 bfd_info_free(&(peer
->bfd_info
));
460 * bgp_bfd_peer_param_type_set - set the BFD session type (multihop or singlehop)
463 bgp_bfd_peer_param_type_set (struct peer
*peer
, enum bfd_sess_type type
)
465 struct peer_group
*group
;
466 struct listnode
*node
, *nnode
;
468 struct bfd_info
*bfd_info
;
471 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
), BFD_DEF_MIN_RX
,
472 BFD_DEF_MIN_TX
, BFD_DEF_DETECT_MULT
, 1, &command
);
474 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
475 bfd_info
->type
= type
;
477 if (CHECK_FLAG (peer
->sflags
, PEER_STATUS_GROUP
))
480 for (ALL_LIST_ELEMENTS (group
->peer
, node
, nnode
, peer
))
484 bfd_set_param((struct bfd_info
**)&(peer
->bfd_info
),
485 BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
486 BFD_DEF_DETECT_MULT
, 1, &command
);
488 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
489 bfd_info
->type
= type
;
491 if (peer
->status
== Established
)
493 if (command
== ZEBRA_BFD_DEST_REGISTER
)
494 bgp_bfd_register_peer(peer
);
496 bgp_bfd_update_type(peer
);
502 if (peer
->status
== Established
)
504 if (command
== ZEBRA_BFD_DEST_REGISTER
)
505 bgp_bfd_register_peer(peer
);
507 bgp_bfd_update_type(peer
);
515 * bgp_bfd_peer_config_write - Write the peer BFD configuration.
518 bgp_bfd_peer_config_write(struct vty
*vty
, struct peer
*peer
, char *addr
)
520 struct bfd_info
*bfd_info
;
525 bfd_info
= (struct bfd_info
*)peer
->bfd_info
;
527 if (CHECK_FLAG (bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
528 vty_out (vty
, " neighbor %s bfd %d %d %d%s", addr
,
529 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
530 bfd_info
->desired_min_tx
, VTY_NEWLINE
);
532 if (bfd_info
->type
!= BFD_TYPE_NOT_CONFIGURED
)
533 vty_out (vty
, " neighbor %s bfd %s%s", addr
,
534 (bfd_info
->type
== BFD_TYPE_MULTIHOP
) ? "multihop" : "singlehop",
537 if (!CHECK_FLAG (bfd_info
->flags
, BFD_FLAG_PARAM_CFG
) &&
538 (bfd_info
->type
== BFD_TYPE_NOT_CONFIGURED
))
539 vty_out (vty
, " neighbor %s bfd%s", addr
, VTY_NEWLINE
);
543 * bgp_bfd_show_info - Show the peer BFD information.
546 bgp_bfd_show_info(struct vty
*vty
, struct peer
*peer
, u_char use_json
, json_object
*json_neigh
)
548 bfd_show_info(vty
, (struct bfd_info
*)peer
->bfd_info
,
549 bgp_bfd_is_peer_multihop(peer
), 0, use_json
, json_neigh
);
554 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd",
557 "Enables BFD support\n")
563 peer
= peer_and_group_lookup_vty (vty
, argv
[idx_peer
]->arg
);
567 ret
= bgp_bfd_peer_param_set (peer
, BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
568 BFD_DEF_DETECT_MULT
, 1);
570 return bgp_vty_return (vty
, ret
);
576 DEFUN (neighbor_bfd_param
,
577 neighbor_bfd_param_cmd
,
578 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd (2-255) (50-60000) (50-60000)",
581 "Enables BFD support\n"
582 "Detect Multiplier\n"
583 "Required min receive interval\n"
584 "Desired min transmit interval\n")
587 int idx_number_1
= 3;
588 int idx_number_2
= 4;
589 int idx_number_3
= 5;
596 peer
= peer_and_group_lookup_vty (vty
, argv
[idx_peer
]->arg
);
600 if ((ret
= bfd_validate_param (vty
, argv
[idx_number_1
]->arg
, argv
[idx_number_2
]->arg
, argv
[idx_number_3
]->arg
, &dm_val
,
601 &rx_val
, &tx_val
)) != CMD_SUCCESS
)
604 ret
= bgp_bfd_peer_param_set (peer
, rx_val
, tx_val
, dm_val
, 0);
606 return bgp_vty_return (vty
, ret
);
612 DEFUN_HIDDEN (neighbor_bfd_type
,
613 neighbor_bfd_type_cmd
,
614 "neighbor <A.B.C.D|X:X::X:X|WORD> bfd <multihop|singlehop>",
617 "Enables BFD support\n"
619 "Single hop session\n")
624 enum bfd_sess_type type
;
627 peer
= peer_and_group_lookup_vty (vty
, argv
[idx_peer
]->arg
);
631 if (!strcmp(argv
[idx_hop
]->arg
, "singlehop"))
632 type
= BFD_TYPE_SINGLEHOP
;
633 else if (!strcmp(argv
[idx_hop
]->arg
, "multihop"))
634 type
= BFD_TYPE_MULTIHOP
;
638 ret
= bgp_bfd_peer_param_type_set (peer
, type
);
640 return bgp_vty_return (vty
, ret
);
645 DEFUN (no_neighbor_bfd
,
647 "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd [(2-255) (50-60000) (50-60000)]",
651 "Disables BFD support\n"
652 "Detect Multiplier\n"
653 "Required min receive interval\n"
654 "Desired min transmit interval\n")
660 peer
= peer_and_group_lookup_vty (vty
, argv
[idx_peer
]->arg
);
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
);
693 ret
= bgp_bfd_peer_param_type_set(peer
, BFD_TYPE_NOT_CONFIGURED
);
695 return bgp_vty_return (vty
, ret
);
705 /* Initialize BFD client functions */
706 zclient
->interface_bfd_dest_update
= bgp_bfd_dest_update
;
707 zclient
->bfd_dest_replay
= bgp_bfd_dest_replay
;
709 /* "neighbor bfd" commands. */
710 install_element (BGP_NODE
, &neighbor_bfd_cmd
);
711 install_element (BGP_NODE
, &neighbor_bfd_param_cmd
);
712 install_element (BGP_NODE
, &neighbor_bfd_type_cmd
);
713 install_element (BGP_NODE
, &no_neighbor_bfd_cmd
);
714 install_element (BGP_NODE
, &no_neighbor_bfd_type_cmd
);