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);
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
*client
, zebra_size_t length
)
152 struct listnode
*inode
, *node
, *onode
;
154 struct ospf_interface
*oi
;
155 struct route_table
*nbrs
;
156 struct route_node
*rn
;
157 struct ospf_neighbor
*nbr
;
158 struct ospf_if_params
*params
;
160 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
162 zlog_debug("Zebra: BFD Dest replay request");
165 /* Replay the neighbor, if BFD is enabled in BGP */
166 for (ALL_LIST_ELEMENTS (om
->ospf
, node
, onode
, ospf
))
168 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, inode
, oi
))
170 if ((nbrs
= oi
->nbrs
) == NULL
)
173 params
= IF_DEF_PARAMS (oi
->ifp
);
174 if (!params
->bfd_info
)
177 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
179 if ((nbr
= rn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
182 if (nbr
->state
< NSM_TwoWay
)
185 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
186 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr
->src
));
188 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_UPDATE
);
196 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
197 * has changed and bring down the neighbor
198 * connectivity if the BFD status changed to
202 ospf_bfd_interface_dest_update (int command
, struct zclient
*zclient
,
205 struct interface
*ifp
;
206 struct ospf_interface
*oi
;
207 struct ospf_if_params
*params
;
208 struct ospf_neighbor
*nbr
;
209 struct route_node
*node
;
213 struct bfd_info
*bfd_info
;
216 ifp
= bfd_get_peer_info (zclient
->ibuf
, &p
, NULL
, &status
);
218 if ((ifp
== NULL
) || (p
.family
!= AF_INET
))
221 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
224 prefix2str(&p
, buf
, sizeof(buf
));
225 zlog_debug("Zebra: interface %s bfd destination %s %s", ifp
->name
, buf
,
226 bfd_get_status_str(status
));
229 params
= IF_DEF_PARAMS (ifp
);
230 if (!params
->bfd_info
)
233 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
235 if ((oi
= node
->info
) == NULL
)
238 nbr
= ospf_nbr_lookup_by_addr (oi
->nbrs
, &p
.u
.prefix4
);
239 if (!nbr
|| !nbr
->bfd_info
)
242 bfd_info
= (struct bfd_info
*)nbr
->bfd_info
;
243 if (bfd_info
->status
== status
)
246 old_status
= bfd_info
->status
;
247 bfd_info
->status
= status
;
248 quagga_gettime (QUAGGA_CLK_MONOTONIC
, &tv
);
249 bfd_info
->last_update
= tv
.tv_sec
;
251 if ((status
== BFD_STATUS_DOWN
) && (old_status
== BFD_STATUS_UP
))
253 if (IS_DEBUG_OSPF (nsm
, NSM_EVENTS
))
254 zlog_debug ("NSM[%s:%s]: BFD Down",
255 IF_NAME (nbr
->oi
), inet_ntoa (nbr
->address
.u
.prefix4
));
257 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_InactivityTimer
);
265 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
268 ospf_bfd_info_nbr_create (struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
)
270 struct bfd_info
*oi_bfd_info
;
271 struct bfd_info
*nbr_bfd_info
;
272 struct interface
*ifp
= oi
->ifp
;
273 struct ospf_if_params
*params
;
275 /* Check if BFD is enabled */
276 params
= IF_DEF_PARAMS (ifp
);
278 /* Check if BFD is enabled */
279 if (!params
->bfd_info
)
282 oi_bfd_info
= (struct bfd_info
*)params
->bfd_info
;
284 nbr
->bfd_info
= bfd_info_create();
286 nbr_bfd_info
= (struct bfd_info
*)nbr
->bfd_info
;
287 nbr_bfd_info
->detect_mult
= oi_bfd_info
->detect_mult
;
288 nbr_bfd_info
->desired_min_tx
= oi_bfd_info
->desired_min_tx
;
289 nbr_bfd_info
->required_min_rx
= oi_bfd_info
->required_min_rx
;
293 * ospf_bfd_write_config - Write the interface BFD configuration.
296 ospf_bfd_write_config(struct vty
*vty
, struct ospf_if_params
*params
)
299 struct bfd_info
*bfd_info
;
301 if (!params
->bfd_info
)
304 bfd_info
= (struct bfd_info
*)params
->bfd_info
;
306 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
307 vty_out (vty
, " ip ospf bfd %d %d %d%s",
308 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
309 bfd_info
->desired_min_tx
, VTY_NEWLINE
);
311 vty_out (vty
, " ip ospf bfd%s", VTY_NEWLINE
);
315 * ospf_bfd_show_info - Show BFD info structure
318 ospf_bfd_show_info(struct vty
*vty
, void *bfd_info
, json_object
*json_obj
,
319 u_char use_json
, int param_only
)
322 bfd_show_param(vty
, (struct bfd_info
*)bfd_info
, 1, 0, use_json
, json_obj
);
324 bfd_show_info(vty
, (struct bfd_info
*)bfd_info
, 0, 1, use_json
, json_obj
);
328 * ospf_bfd_interface_show - Show the interface BFD configuration.
331 ospf_bfd_interface_show(struct vty
*vty
, struct interface
*ifp
,
332 json_object
*json_interface_sub
, u_char use_json
)
334 struct ospf_if_params
*params
;
336 params
= IF_DEF_PARAMS (ifp
);
338 ospf_bfd_show_info(vty
, params
->bfd_info
, json_interface_sub
, use_json
, 1);
342 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
346 ospf_bfd_if_param_set (struct interface
*ifp
, u_int32_t min_rx
,
347 u_int32_t min_tx
, u_int8_t detect_mult
, int defaults
)
349 struct ospf_if_params
*params
;
352 params
= IF_DEF_PARAMS (ifp
);
354 bfd_set_param((struct bfd_info
**)&(params
->bfd_info
), min_rx
, min_tx
,
355 detect_mult
, defaults
, &command
);
357 ospf_bfd_reg_dereg_all_nbr(ifp
, command
);
364 "OSPF interface commands\n"
365 "Enables BFD support\n")
367 struct interface
*ifp
= (struct interface
*) vty
->index
;
370 ospf_bfd_if_param_set (ifp
, BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
371 BFD_DEF_DETECT_MULT
, 1);
376 DEFUN (ip_ospf_bfd_param
,
377 ip_ospf_bfd_param_cmd
,
378 "ip ospf bfd " BFD_CMD_DETECT_MULT_RANGE BFD_CMD_MIN_RX_RANGE BFD_CMD_MIN_TX_RANGE
,
380 "OSPF interface commands\n"
381 "Enables BFD support\n"
382 "Detect Multiplier\n"
383 "Required min receive interval\n"
384 "Desired min transmit interval\n")
386 struct interface
*ifp
= (struct interface
*) vty
->index
;
394 if ((ret
= bfd_validate_param (vty
, argv
[0], argv
[1], argv
[2], &dm_val
,
395 &rx_val
, &tx_val
)) != CMD_SUCCESS
)
398 ospf_bfd_if_param_set (ifp
, rx_val
, tx_val
, dm_val
, 0);
403 DEFUN (no_ip_ospf_bfd
,
408 "OSPF interface commands\n"
409 "Disables BFD support\n")
411 struct interface
*ifp
= (struct interface
*)vty
->index
;
412 struct ospf_if_params
*params
;
416 params
= IF_DEF_PARAMS (ifp
);
417 if (params
->bfd_info
)
419 ospf_bfd_reg_dereg_all_nbr(ifp
, ZEBRA_BFD_DEST_DEREGISTER
);
420 bfd_info_free(&(params
->bfd_info
));
429 /* Initialize BFD client functions */
430 zclient
->interface_bfd_dest_update
= ospf_bfd_interface_dest_update
;
431 zclient
->bfd_dest_replay
= ospf_bfd_nbr_replay
;
433 /* Install BFD command */
434 install_element (INTERFACE_NODE
, &ip_ospf_bfd_cmd
);
435 install_element (INTERFACE_NODE
, &ip_ospf_bfd_param_cmd
);
436 install_element (INTERFACE_NODE
, &no_ip_ospf_bfd_cmd
);