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 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 "ospf_asbr.h"
39 #include "ospf_lsdb.h"
40 #include "ospf_neighbor.h"
41 #include "ospf_interface.h"
44 #include "ospf_dump.h"
47 extern struct zclient
*zclient
;
50 * ospf_bfd_info_free - Free BFD info structure
53 ospf_bfd_info_free(void **bfd_info
)
55 bfd_info_free((struct bfd_info
**) bfd_info
);
59 * ospf_bfd_reg_dereg_nbr - Register/Deregister a neighbor with BFD through
60 * zebra for starting/stopping the monitoring of
61 * the neighbor rechahability.
64 ospf_bfd_reg_dereg_nbr (struct ospf_neighbor
*nbr
, int command
)
66 struct ospf_interface
*oi
= nbr
->oi
;
67 struct interface
*ifp
= oi
->ifp
;
68 struct ospf_if_params
*params
;
69 struct bfd_info
*bfd_info
;
71 /* Check if BFD is enabled */
72 params
= IF_DEF_PARAMS (ifp
);
74 /* Check if BFD is enabled */
75 if (!params
->bfd_info
)
77 bfd_info
= (struct bfd_info
*)params
->bfd_info
;
79 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
80 zlog_debug ("%s nbr (%s) with BFD",
81 bfd_get_command_dbg_str(command
),
82 inet_ntoa (nbr
->src
));
84 bfd_peer_sendmsg (zclient
, bfd_info
, AF_INET
,
85 &nbr
->src
, NULL
, ifp
->name
, 0, 0, command
, 0, VRF_DEFAULT
);
89 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
90 * neighbor state is changed to/from 2way.
93 ospf_bfd_trigger_event(struct ospf_neighbor
*nbr
, int old_state
, int state
)
95 if ((old_state
< NSM_TwoWay
) && (state
>= NSM_TwoWay
))
96 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_REGISTER
);
97 else if ((old_state
>= NSM_TwoWay
) && (state
< NSM_TwoWay
))
98 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_DEREGISTER
);
102 * ospf_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
103 * with a interface with BFD through
104 * zebra for starting/stopping the monitoring of
105 * the neighbor rechahability.
108 ospf_bfd_reg_dereg_all_nbr (struct interface
*ifp
, int command
)
110 struct ospf_interface
*oi
;
111 struct route_table
*nbrs
;
112 struct ospf_neighbor
*nbr
;
113 struct route_node
*irn
;
114 struct route_node
*nrn
;
116 for (irn
= route_top (IF_OIFS (ifp
)); irn
; irn
= route_next (irn
))
118 if ((oi
= irn
->info
) == NULL
)
121 if ((nbrs
= oi
->nbrs
) == NULL
)
124 for (nrn
= route_top (nbrs
); nrn
; nrn
= route_next (nrn
))
126 if ((nbr
= nrn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
129 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
)
130 ospf_bfd_info_nbr_create(oi
, nbr
);
132 bfd_info_free((struct bfd_info
**)&nbr
->bfd_info
);
134 if (nbr
->state
< NSM_TwoWay
)
137 ospf_bfd_reg_dereg_nbr(nbr
, command
);
145 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
149 ospf_bfd_nbr_replay (int command
, struct zclient
*zclient
, 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 /* Send the client registration */
166 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
);
168 /* Replay the neighbor, if BFD is enabled in BGP */
169 for (ALL_LIST_ELEMENTS (om
->ospf
, node
, onode
, ospf
))
171 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, inode
, oi
))
173 if ((nbrs
= oi
->nbrs
) == NULL
)
176 params
= IF_DEF_PARAMS (oi
->ifp
);
177 if (!params
->bfd_info
)
180 for (rn
= route_top (nbrs
); rn
; rn
= route_next (rn
))
182 if ((nbr
= rn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
185 if (nbr
->state
< NSM_TwoWay
)
188 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
189 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr
->src
));
191 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_UPDATE
);
199 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
200 * has changed and bring down the neighbor
201 * connectivity if the BFD status changed to
205 ospf_bfd_interface_dest_update (int command
, struct zclient
*zclient
,
206 zebra_size_t length
, vrf_id_t vrf_id
)
208 struct interface
*ifp
;
209 struct ospf_interface
*oi
;
210 struct ospf_if_params
*params
;
211 struct ospf_neighbor
*nbr
;
212 struct route_node
*node
;
216 struct bfd_info
*bfd_info
;
219 ifp
= bfd_get_peer_info (zclient
->ibuf
, &p
, NULL
, &status
, vrf_id
);
221 if ((ifp
== NULL
) || (p
.family
!= AF_INET
))
224 if (IS_DEBUG_OSPF (zebra
, ZEBRA_INTERFACE
))
226 char buf
[PREFIX2STR_BUFFER
];
227 prefix2str(&p
, buf
, sizeof(buf
));
228 zlog_debug("Zebra: interface %s bfd destination %s %s", ifp
->name
, buf
,
229 bfd_get_status_str(status
));
232 params
= IF_DEF_PARAMS (ifp
);
233 if (!params
->bfd_info
)
236 for (node
= route_top (IF_OIFS (ifp
)); node
; node
= route_next (node
))
238 if ((oi
= node
->info
) == NULL
)
241 nbr
= ospf_nbr_lookup_by_addr (oi
->nbrs
, &p
.u
.prefix4
);
242 if (!nbr
|| !nbr
->bfd_info
)
245 bfd_info
= (struct bfd_info
*)nbr
->bfd_info
;
246 if (bfd_info
->status
== status
)
249 old_status
= bfd_info
->status
;
250 bfd_info
->status
= status
;
252 bfd_info
->last_update
= tv
.tv_sec
;
254 if ((status
== BFD_STATUS_DOWN
) && (old_status
== BFD_STATUS_UP
))
256 if (IS_DEBUG_OSPF (nsm
, NSM_EVENTS
))
257 zlog_debug ("NSM[%s:%s]: BFD Down",
258 IF_NAME (nbr
->oi
), inet_ntoa (nbr
->address
.u
.prefix4
));
260 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_InactivityTimer
);
268 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
271 ospf_bfd_info_nbr_create (struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
)
273 struct bfd_info
*oi_bfd_info
;
274 struct bfd_info
*nbr_bfd_info
;
275 struct interface
*ifp
= oi
->ifp
;
276 struct ospf_if_params
*params
;
278 /* Check if BFD is enabled */
279 params
= IF_DEF_PARAMS (ifp
);
281 /* Check if BFD is enabled */
282 if (!params
->bfd_info
)
285 oi_bfd_info
= (struct bfd_info
*)params
->bfd_info
;
287 nbr
->bfd_info
= bfd_info_create();
289 nbr_bfd_info
= (struct bfd_info
*)nbr
->bfd_info
;
290 nbr_bfd_info
->detect_mult
= oi_bfd_info
->detect_mult
;
291 nbr_bfd_info
->desired_min_tx
= oi_bfd_info
->desired_min_tx
;
292 nbr_bfd_info
->required_min_rx
= oi_bfd_info
->required_min_rx
;
296 * ospf_bfd_write_config - Write the interface BFD configuration.
299 ospf_bfd_write_config(struct vty
*vty
, struct ospf_if_params
*params
)
302 struct bfd_info
*bfd_info
;
304 if (!params
->bfd_info
)
307 bfd_info
= (struct bfd_info
*)params
->bfd_info
;
309 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
310 vty_out (vty
, " ip ospf bfd %d %d %d%s",
311 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
312 bfd_info
->desired_min_tx
, VTY_NEWLINE
);
314 vty_out (vty
, " ip ospf bfd%s", VTY_NEWLINE
);
318 * ospf_bfd_show_info - Show BFD info structure
321 ospf_bfd_show_info(struct vty
*vty
, void *bfd_info
, json_object
*json_obj
,
322 u_char use_json
, int param_only
)
325 bfd_show_param(vty
, (struct bfd_info
*)bfd_info
, 1, 0, use_json
, json_obj
);
327 bfd_show_info(vty
, (struct bfd_info
*)bfd_info
, 0, 1, use_json
, json_obj
);
331 * ospf_bfd_interface_show - Show the interface BFD configuration.
334 ospf_bfd_interface_show(struct vty
*vty
, struct interface
*ifp
,
335 json_object
*json_interface_sub
, u_char use_json
)
337 struct ospf_if_params
*params
;
339 params
= IF_DEF_PARAMS (ifp
);
341 ospf_bfd_show_info(vty
, params
->bfd_info
, json_interface_sub
, use_json
, 1);
345 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
349 ospf_bfd_if_param_set (struct interface
*ifp
, u_int32_t min_rx
,
350 u_int32_t min_tx
, u_int8_t detect_mult
, int defaults
)
352 struct ospf_if_params
*params
;
355 params
= IF_DEF_PARAMS (ifp
);
357 bfd_set_param((struct bfd_info
**)&(params
->bfd_info
), min_rx
, min_tx
,
358 detect_mult
, defaults
, &command
);
360 ospf_bfd_reg_dereg_all_nbr(ifp
, command
);
367 "OSPF interface commands\n"
368 "Enables BFD support\n")
370 VTY_DECLVAR_CONTEXT(interface
, ifp
);
371 struct ospf_if_params
*params
;
372 struct bfd_info
*bfd_info
;
375 params
= IF_DEF_PARAMS (ifp
);
376 bfd_info
= params
->bfd_info
;
378 if (!bfd_info
|| ! CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
379 ospf_bfd_if_param_set (ifp
, BFD_DEF_MIN_RX
, BFD_DEF_MIN_TX
,
380 BFD_DEF_DETECT_MULT
, 1);
385 DEFUN (ip_ospf_bfd_param
,
386 ip_ospf_bfd_param_cmd
,
387 "ip ospf bfd (2-255) (50-60000) (50-60000)",
389 "OSPF interface commands\n"
390 "Enables BFD support\n"
391 "Detect Multiplier\n"
392 "Required min receive interval\n"
393 "Desired min transmit interval\n")
395 VTY_DECLVAR_CONTEXT(interface
, ifp
);
397 int idx_number_2
= 4;
398 int idx_number_3
= 5;
406 if ((ret
= bfd_validate_param (vty
, argv
[idx_number
]->arg
, argv
[idx_number_2
]->arg
, argv
[idx_number_3
]->arg
, &dm_val
,
407 &rx_val
, &tx_val
)) != CMD_SUCCESS
)
410 ospf_bfd_if_param_set (ifp
, rx_val
, tx_val
, dm_val
, 0);
415 DEFUN (no_ip_ospf_bfd
,
417 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
420 "OSPF interface commands\n"
421 "Disables BFD support\n"
422 "Detect Multiplier\n"
423 "Required min receive interval\n"
424 "Desired min transmit interval\n")
426 VTY_DECLVAR_CONTEXT(interface
, ifp
);
427 struct ospf_if_params
*params
;
431 params
= IF_DEF_PARAMS (ifp
);
432 if (params
->bfd_info
)
434 ospf_bfd_reg_dereg_all_nbr(ifp
, ZEBRA_BFD_DEST_DEREGISTER
);
435 bfd_info_free(&(params
->bfd_info
));
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
);