2 * ospf_bfd.c: OSPF 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
38 #include "ospf_asbr.h"
40 #include "ospf_lsdb.h"
41 #include "ospf_neighbor.h"
42 #include "ospf_interface.h"
45 #include "ospf_dump.h"
48 extern struct zclient
*zclient
;
51 * ospf_bfd_info_free - Free BFD info structure
54 ospf_bfd_info_free(void **bfd_info
)
56 bfd_info_free((struct bfd_info
**) bfd_info
);
60 * ospf_bfd_reg_dereg_nbr - Register/Deregister a neighbor with BFD through
61 * zebra for starting/stopping the monitoring of
62 * the neighbor rechahability.
65 ospf_bfd_reg_dereg_nbr (struct ospf_neighbor
*nbr
, int command
)
67 struct ospf_interface
*oi
= nbr
->oi
;
68 struct interface
*ifp
= oi
->ifp
;
69 struct ospf_if_params
*params
;
70 struct bfd_info
*bfd_info
;
72 /* Check if BFD is enabled */
73 params
= IF_DEF_PARAMS (ifp
);
75 /* Check if BFD is enabled */
76 if (!params
->bfd_info
)
78 bfd_info
= (struct bfd_info
*)params
->bfd_info
;
80 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
81 zlog_debug ("%s nbr (%s) with BFD",
82 bfd_get_command_dbg_str(command
),
83 inet_ntoa (nbr
->src
));
85 bfd_peer_sendmsg (zclient
, bfd_info
, AF_INET
,
86 &nbr
->src
, NULL
, ifp
->name
, 0, 0, command
, 0, VRF_DEFAULT
);
90 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
91 * neighbor state is changed to/from 2way.
94 ospf_bfd_trigger_event(struct ospf_neighbor
*nbr
, int old_state
, int state
)
96 if ((old_state
< NSM_TwoWay
) && (state
>= NSM_TwoWay
))
97 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_REGISTER
);
98 else if ((old_state
>= NSM_TwoWay
) && (state
< NSM_TwoWay
))
99 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_DEREGISTER
);
103 * ospf_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
104 * with a interface with BFD through
105 * zebra for starting/stopping the monitoring of
106 * the neighbor rechahability.
109 ospf_bfd_reg_dereg_all_nbr (struct interface
*ifp
, int command
)
111 struct ospf_interface
*oi
;
112 struct route_table
*nbrs
;
113 struct ospf_neighbor
*nbr
;
114 struct route_node
*irn
;
115 struct route_node
*nrn
;
117 for (irn
= route_top (IF_OIFS (ifp
)); irn
; irn
= route_next (irn
))
119 if ((oi
= irn
->info
) == NULL
)
122 if ((nbrs
= oi
->nbrs
) == NULL
)
125 for (nrn
= route_top (nbrs
); nrn
; nrn
= route_next (nrn
))
127 if ((nbr
= nrn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
130 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
)
131 ospf_bfd_info_nbr_create(oi
, nbr
);
133 bfd_info_free((struct bfd_info
**)&nbr
->bfd_info
);
135 if (nbr
->state
< NSM_TwoWay
)
138 ospf_bfd_reg_dereg_nbr(nbr
, command
);
146 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
150 ospf_bfd_nbr_replay (int command
, struct zclient
*zclient
, zebra_size_t length
,
153 struct listnode
*inode
, *node
, *onode
;
155 struct ospf_interface
*oi
;
156 struct route_table
*nbrs
;
157 struct route_node
*rn
;
158 struct ospf_neighbor
*nbr
;
159 struct ospf_if_params
*params
;
161 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
163 zlog_debug("Zebra: BFD Dest replay request");
166 /* Send the client registration */
167 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
169 /* Replay the neighbor, if BFD is enabled in BGP */
170 for (ALL_LIST_ELEMENTS (om
->ospf
, node
, onode
, ospf
))
172 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, inode
, oi
))
174 if ((nbrs
= oi
->nbrs
) == NULL
)
177 params
= IF_DEF_PARAMS (oi
->ifp
);
178 if (!params
->bfd_info
)
181 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
183 if ((nbr
= rn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
186 if (nbr
->state
< NSM_TwoWay
)
189 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
190 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr
->src
));
192 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_UPDATE
);
200 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
201 * has changed and bring down the neighbor
202 * connectivity if the BFD status changed to
206 ospf_bfd_interface_dest_update (int command
, struct zclient
*zclient
,
207 zebra_size_t length
, vrf_id_t vrf_id
)
209 struct interface
*ifp
;
210 struct ospf_interface
*oi
;
211 struct ospf_if_params
*params
;
212 struct ospf_neighbor
*nbr
;
213 struct route_node
*node
;
217 struct bfd_info
*bfd_info
;
220 ifp
= bfd_get_peer_info (zclient
->ibuf
, &p
, NULL
, &status
, vrf_id
);
222 if ((ifp
== NULL
) || (p
.family
!= AF_INET
))
225 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
227 char buf
[PREFIX2STR_BUFFER
];
228 prefix2str(&p
, buf
, sizeof(buf
));
229 zlog_debug("Zebra: interface %s bfd destination %s %s", ifp
->name
, buf
,
230 bfd_get_status_str(status
));
233 params
= IF_DEF_PARAMS (ifp
);
234 if (!params
->bfd_info
)
237 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
239 if ((oi
= node
->info
) == NULL
)
242 nbr
= ospf_nbr_lookup_by_addr (oi
->nbrs
, &p
.u
.prefix4
);
243 if (!nbr
|| !nbr
->bfd_info
)
246 bfd_info
= (struct bfd_info
*)nbr
->bfd_info
;
247 if (bfd_info
->status
== status
)
250 old_status
= bfd_info
->status
;
251 bfd_info
->status
= status
;
252 quagga_gettime (QUAGGA_CLK_MONOTONIC
, &tv
);
253 bfd_info
->last_update
= tv
.tv_sec
;
255 if ((status
== BFD_STATUS_DOWN
) && (old_status
== BFD_STATUS_UP
))
257 if (IS_DEBUG_OSPF (nsm
, NSM_EVENTS
))
258 zlog_debug ("NSM[%s:%s]: BFD Down",
259 IF_NAME (nbr
->oi
), inet_ntoa (nbr
->address
.u
.prefix4
));
261 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_InactivityTimer
);
269 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
272 ospf_bfd_info_nbr_create (struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
)
274 struct bfd_info
*oi_bfd_info
;
275 struct bfd_info
*nbr_bfd_info
;
276 struct interface
*ifp
= oi
->ifp
;
277 struct ospf_if_params
*params
;
279 /* Check if BFD is enabled */
280 params
= IF_DEF_PARAMS (ifp
);
282 /* Check if BFD is enabled */
283 if (!params
->bfd_info
)
286 oi_bfd_info
= (struct bfd_info
*)params
->bfd_info
;
288 nbr
->bfd_info
= bfd_info_create();
290 nbr_bfd_info
= (struct bfd_info
*)nbr
->bfd_info
;
291 nbr_bfd_info
->detect_mult
= oi_bfd_info
->detect_mult
;
292 nbr_bfd_info
->desired_min_tx
= oi_bfd_info
->desired_min_tx
;
293 nbr_bfd_info
->required_min_rx
= oi_bfd_info
->required_min_rx
;
297 * ospf_bfd_write_config - Write the interface BFD configuration.
300 ospf_bfd_write_config(struct vty
*vty
, struct ospf_if_params
*params
)
303 struct bfd_info
*bfd_info
;
305 if (!params
->bfd_info
)
308 bfd_info
= (struct bfd_info
*)params
->bfd_info
;
310 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
311 vty_out (vty
, " ip ospf bfd %d %d %d%s",
312 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
313 bfd_info
->desired_min_tx
, VTY_NEWLINE
);
315 vty_out (vty
, " ip ospf bfd%s", VTY_NEWLINE
);
319 * ospf_bfd_show_info - Show BFD info structure
322 ospf_bfd_show_info(struct vty
*vty
, void *bfd_info
, json_object
*json_obj
,
323 u_char use_json
, int param_only
)
326 bfd_show_param(vty
, (struct bfd_info
*)bfd_info
, 1, 0, use_json
, json_obj
);
328 bfd_show_info(vty
, (struct bfd_info
*)bfd_info
, 0, 1, use_json
, json_obj
);
332 * ospf_bfd_interface_show - Show the interface BFD configuration.
335 ospf_bfd_interface_show(struct vty
*vty
, struct interface
*ifp
,
336 json_object
*json_interface_sub
, u_char use_json
)
338 struct ospf_if_params
*params
;
340 params
= IF_DEF_PARAMS (ifp
);
342 ospf_bfd_show_info(vty
, params
->bfd_info
, json_interface_sub
, use_json
, 1);
346 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
350 ospf_bfd_if_param_set (struct interface
*ifp
, u_int32_t min_rx
,
351 u_int32_t min_tx
, u_int8_t detect_mult
, int defaults
)
353 struct ospf_if_params
*params
;
356 params
= IF_DEF_PARAMS (ifp
);
358 bfd_set_param((struct bfd_info
**)&(params
->bfd_info
), min_rx
, min_tx
,
359 detect_mult
, defaults
, &command
);
361 ospf_bfd_reg_dereg_all_nbr(ifp
, command
);
368 "OSPF interface commands\n"
369 "Enables BFD support\n")
371 struct interface
*ifp
= (struct interface
*) vty
->index
;
374 ospf_bfd_if_param_set (ifp
, BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
375 BFD_DEF_DETECT_MULT
, 1);
380 DEFUN (ip_ospf_bfd_param
,
381 ip_ospf_bfd_param_cmd
,
382 "ip ospf bfd " BFD_CMD_DETECT_MULT_RANGE BFD_CMD_MIN_RX_RANGE BFD_CMD_MIN_TX_RANGE
,
384 "OSPF interface commands\n"
385 "Enables BFD support\n"
386 "Detect Multiplier\n"
387 "Required min receive interval\n"
388 "Desired min transmit interval\n")
390 struct interface
*ifp
= (struct interface
*) vty
->index
;
398 if ((ret
= bfd_validate_param (vty
, argv
[0], argv
[1], argv
[2], &dm_val
,
399 &rx_val
, &tx_val
)) != CMD_SUCCESS
)
402 ospf_bfd_if_param_set (ifp
, rx_val
, tx_val
, dm_val
, 0);
407 DEFUN (no_ip_ospf_bfd
,
412 "OSPF interface commands\n"
413 "Disables BFD support\n")
415 struct interface
*ifp
= (struct interface
*)vty
->index
;
416 struct ospf_if_params
*params
;
420 params
= IF_DEF_PARAMS (ifp
);
421 if (params
->bfd_info
)
423 ospf_bfd_reg_dereg_all_nbr(ifp
, ZEBRA_BFD_DEST_DEREGISTER
);
424 bfd_info_free(&(params
->bfd_info
));
430 ALIAS (no_ip_ospf_bfd
,
431 no_ip_ospf_bfd_param_cmd
,
432 "no ip ospf bfd " BFD_CMD_DETECT_MULT_RANGE BFD_CMD_MIN_RX_RANGE BFD_CMD_MIN_TX_RANGE
,
435 "OSPF interface commands\n"
436 "Enables BFD support\n"
437 "Detect Multiplier\n"
438 "Required min receive interval\n"
439 "Desired min transmit interval\n")
446 /* Initialize BFD client functions */
447 zclient
->interface_bfd_dest_update
= ospf_bfd_interface_dest_update
;
448 zclient
->bfd_dest_replay
= ospf_bfd_nbr_replay
;
450 /* Install BFD command */
451 install_element (INTERFACE_NODE
, &ip_ospf_bfd_cmd
);
452 install_element (INTERFACE_NODE
, &ip_ospf_bfd_param_cmd
);
453 install_element (INTERFACE_NODE
, &no_ip_ospf_bfd_cmd
);
454 install_element (INTERFACE_NODE
, &no_ip_ospf_bfd_param_cmd
);
456 /* Send the client registration */
457 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);