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
52 void ospf_bfd_info_free(void **bfd_info
)
54 bfd_info_free((struct bfd_info
**)bfd_info
);
58 * ospf_bfd_reg_dereg_nbr - Register/Deregister a neighbor with BFD through
59 * zebra for starting/stopping the monitoring of
60 * the neighbor rechahability.
62 static void ospf_bfd_reg_dereg_nbr(struct ospf_neighbor
*nbr
, int command
)
64 struct ospf_interface
*oi
= nbr
->oi
;
65 struct interface
*ifp
= oi
->ifp
;
66 struct ospf_if_params
*params
;
67 struct bfd_info
*bfd_info
;
69 /* Check if BFD is enabled */
70 params
= IF_DEF_PARAMS(ifp
);
72 /* Check if BFD is enabled */
73 if (!params
->bfd_info
)
75 bfd_info
= (struct bfd_info
*)params
->bfd_info
;
77 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
78 zlog_debug("%s nbr (%s) with BFD. OSPF vrf %s",
79 bfd_get_command_dbg_str(command
),
81 ospf_vrf_id_to_name(oi
->ospf
->vrf_id
));
83 bfd_peer_sendmsg(zclient
, bfd_info
, AF_INET
, &nbr
->src
, NULL
, ifp
->name
,
84 0, 0, command
, 0, oi
->ospf
->vrf_id
);
88 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
89 * neighbor state is changed to/from 2way.
91 void ospf_bfd_trigger_event(struct ospf_neighbor
*nbr
, int old_state
, int state
)
93 if ((old_state
< NSM_TwoWay
) && (state
>= NSM_TwoWay
))
94 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_REGISTER
);
95 else if ((old_state
>= NSM_TwoWay
) && (state
< NSM_TwoWay
))
96 ospf_bfd_reg_dereg_nbr(nbr
, ZEBRA_BFD_DEST_DEREGISTER
);
100 * ospf_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
101 * with a interface with BFD through
102 * zebra for starting/stopping the monitoring of
103 * the neighbor rechahability.
105 static int ospf_bfd_reg_dereg_all_nbr(struct interface
*ifp
, int command
)
107 struct ospf_interface
*oi
;
108 struct route_table
*nbrs
;
109 struct ospf_neighbor
*nbr
;
110 struct route_node
*irn
;
111 struct route_node
*nrn
;
113 for (irn
= route_top(IF_OIFS(ifp
)); irn
; irn
= route_next(irn
)) {
114 if ((oi
= irn
->info
) == NULL
)
117 if ((nbrs
= oi
->nbrs
) == NULL
)
120 for (nrn
= route_top(nbrs
); nrn
; nrn
= route_next(nrn
)) {
121 if ((nbr
= nrn
->info
) == NULL
|| nbr
== oi
->nbr_self
)
124 if (command
!= ZEBRA_BFD_DEST_DEREGISTER
)
125 ospf_bfd_info_nbr_create(oi
, nbr
);
128 (struct bfd_info
**)&nbr
->bfd_info
);
130 if (nbr
->state
< NSM_TwoWay
)
133 ospf_bfd_reg_dereg_nbr(nbr
, command
);
141 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
144 static int ospf_bfd_nbr_replay(ZAPI_CALLBACK_ARGS
)
146 struct listnode
*inode
, *node
, *onode
;
148 struct ospf_interface
*oi
;
149 struct route_table
*nbrs
;
150 struct route_node
*rn
;
151 struct ospf_neighbor
*nbr
;
152 struct ospf_if_params
*params
;
154 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
)) {
155 zlog_debug("Zebra: BFD Dest replay request");
158 /* Send the client registration */
159 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
161 /* Replay the neighbor, if BFD is enabled in OSPF */
162 for (ALL_LIST_ELEMENTS(om
->ospf
, node
, onode
, ospf
)) {
163 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, inode
, oi
)) {
164 if ((nbrs
= oi
->nbrs
) == NULL
)
167 params
= IF_DEF_PARAMS(oi
->ifp
);
168 if (!params
->bfd_info
)
171 for (rn
= route_top(nbrs
); rn
; rn
= route_next(rn
)) {
172 if ((nbr
= rn
->info
) == NULL
173 || nbr
== oi
->nbr_self
)
176 if (nbr
->state
< NSM_TwoWay
)
179 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
180 zlog_debug("Replaying nbr (%s) to BFD",
181 inet_ntoa(nbr
->src
));
183 ospf_bfd_reg_dereg_nbr(nbr
,
184 ZEBRA_BFD_DEST_UPDATE
);
192 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
193 * has changed and bring down the neighbor
194 * connectivity if the BFD status changed to
197 static int ospf_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS
)
199 struct interface
*ifp
;
200 struct ospf_interface
*oi
;
201 struct ospf_if_params
*params
;
202 struct ospf_neighbor
*nbr
;
203 struct route_node
*node
;
207 struct bfd_info
*bfd_info
;
210 ifp
= bfd_get_peer_info(zclient
->ibuf
, &p
, NULL
, &status
, vrf_id
);
212 if ((ifp
== NULL
) || (p
.family
!= AF_INET
))
215 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
)) {
216 char buf
[PREFIX2STR_BUFFER
];
217 prefix2str(&p
, buf
, sizeof(buf
));
218 zlog_debug("Zebra: interface %s bfd destination %s %s",
219 ifp
->name
, buf
, bfd_get_status_str(status
));
222 params
= IF_DEF_PARAMS(ifp
);
223 if (!params
->bfd_info
)
226 for (node
= route_top(IF_OIFS(ifp
)); node
; node
= route_next(node
)) {
227 if ((oi
= node
->info
) == NULL
)
230 nbr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &p
.u
.prefix4
);
231 if (!nbr
|| !nbr
->bfd_info
)
234 bfd_info
= (struct bfd_info
*)nbr
->bfd_info
;
235 if (bfd_info
->status
== status
)
238 old_status
= bfd_info
->status
;
239 bfd_info
->status
= status
;
241 bfd_info
->last_update
= tv
.tv_sec
;
243 if ((status
== BFD_STATUS_DOWN
)
244 && (old_status
== BFD_STATUS_UP
)) {
245 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
246 zlog_debug("NSM[%s:%s]: BFD Down",
248 inet_ntoa(nbr
->address
.u
.prefix4
));
250 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_InactivityTimer
);
252 if ((status
== BFD_STATUS_UP
)
253 && (old_status
== BFD_STATUS_DOWN
)) {
254 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
255 zlog_debug("NSM[%s:%s]: BFD Up",
257 inet_ntoa(nbr
->address
.u
.prefix4
));
265 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
267 void ospf_bfd_info_nbr_create(struct ospf_interface
*oi
,
268 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.
295 void ospf_bfd_write_config(struct vty
*vty
, struct ospf_if_params
*params
)
299 struct bfd_info
*bfd_info
;
300 #endif /* ! HAVE_BFDD */
302 if (!params
->bfd_info
)
306 bfd_info
= (struct bfd_info
*)params
->bfd_info
;
308 if (CHECK_FLAG(bfd_info
->flags
, BFD_FLAG_PARAM_CFG
))
309 vty_out(vty
, " ip ospf bfd %d %d %d\n", bfd_info
->detect_mult
,
310 bfd_info
->required_min_rx
, bfd_info
->desired_min_tx
);
312 #endif /* ! HAVE_BFDD */
313 vty_out(vty
, " ip ospf bfd\n");
317 * ospf_bfd_show_info - Show BFD info structure
319 void ospf_bfd_show_info(struct vty
*vty
, void *bfd_info
, json_object
*json_obj
,
320 bool use_json
, int param_only
)
323 bfd_show_param(vty
, (struct bfd_info
*)bfd_info
, 1, 0, use_json
,
326 bfd_show_info(vty
, (struct bfd_info
*)bfd_info
, 0, 1, use_json
,
331 * ospf_bfd_interface_show - Show the interface BFD configuration.
333 void ospf_bfd_interface_show(struct vty
*vty
, struct interface
*ifp
,
334 json_object
*json_interface_sub
, bool use_json
)
336 struct ospf_if_params
*params
;
338 params
= IF_DEF_PARAMS(ifp
);
340 ospf_bfd_show_info(vty
, params
->bfd_info
, json_interface_sub
, use_json
,
345 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
348 static void ospf_bfd_if_param_set(struct interface
*ifp
, uint32_t min_rx
,
349 uint32_t min_tx
, uint8_t detect_mult
,
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);
389 #endif /* HAVE_BFDD */
391 ip_ospf_bfd_param_cmd
,
392 "ip ospf bfd (2-255) (50-60000) (50-60000)",
394 "OSPF interface commands\n"
395 "Enables BFD support\n"
396 "Detect Multiplier\n"
397 "Required min receive interval\n"
398 "Desired min transmit interval\n")
400 VTY_DECLVAR_CONTEXT(interface
, ifp
);
402 int idx_number_2
= 4;
403 int idx_number_3
= 5;
411 if ((ret
= bfd_validate_param(
412 vty
, argv
[idx_number
]->arg
, argv
[idx_number_2
]->arg
,
413 argv
[idx_number_3
]->arg
, &dm_val
, &rx_val
, &tx_val
))
417 ospf_bfd_if_param_set(ifp
, rx_val
, tx_val
, dm_val
, 0);
422 DEFUN (no_ip_ospf_bfd
,
427 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
428 #endif /* HAVE_BFDD */
431 "OSPF interface commands\n"
432 "Disables BFD support\n"
434 "Detect Multiplier\n"
435 "Required min receive interval\n"
436 "Desired min transmit interval\n"
437 #endif /* !HAVE_BFDD */
440 VTY_DECLVAR_CONTEXT(interface
, ifp
);
441 struct ospf_if_params
*params
;
445 params
= IF_DEF_PARAMS(ifp
);
446 if (params
->bfd_info
) {
447 ospf_bfd_reg_dereg_all_nbr(ifp
, ZEBRA_BFD_DEST_DEREGISTER
);
448 bfd_info_free(&(params
->bfd_info
));
454 void ospf_bfd_init(void)
458 /* Initialize BFD client functions */
459 zclient
->interface_bfd_dest_update
= ospf_bfd_interface_dest_update
;
460 zclient
->bfd_dest_replay
= ospf_bfd_nbr_replay
;
462 /* Install BFD command */
463 install_element(INTERFACE_NODE
, &ip_ospf_bfd_cmd
);
464 install_element(INTERFACE_NODE
, &ip_ospf_bfd_param_cmd
);
465 install_element(INTERFACE_NODE
, &no_ip_ospf_bfd_cmd
);