]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
This patch changes ospfd from only listening mode for BFD status updates to interacti...
[mirror_frr.git] / ospfd / ospf_bfd.c
1 /**
2 * ospf_bfd.c: OSPF BFD handling routines
3 *
4 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
5 *
6 * This file is part of GNU Zebra.
7 *
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
11 * later version.
12 *
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.
17 *
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
21 * 02111-1307, USA.
22 */
23
24 #include <zebra.h>
25
26 #include "command.h"
27 #include "linklist.h"
28 #include "memory.h"
29 #include "prefix.h"
30 #include "thread.h"
31 #include "buffer.h"
32 #include "stream.h"
33 #include "zclient.h"
34 #include "vty.h"
35 #include "table.h"
36 #include "bfd.h"
37 #include "ospfd.h"
38 #include "ospf_asbr.h"
39 #include "ospf_lsa.h"
40 #include "ospf_lsdb.h"
41 #include "ospf_neighbor.h"
42 #include "ospf_interface.h"
43 #include "ospf_nsm.h"
44 #include "ospf_bfd.h"
45 #include "ospf_dump.h"
46 #include "ospf_vty.h"
47
48 extern struct zclient *zclient;
49
50 /*
51 * ospf_bfd_reg_dereg_nbr - Register/Deregister a neighbor with BFD through
52 * zebra for starting/stopping the monitoring of
53 * the neighbor rechahability.
54 */
55 static void
56 ospf_bfd_reg_dereg_nbr (struct ospf_neighbor *nbr, int command)
57 {
58 struct ospf_interface *oi = nbr->oi;
59 struct interface *ifp = oi->ifp;
60 struct ospf_if_params *params;
61 struct bfd_info *bfd_info;
62
63 /* Check if BFD is enabled */
64 params = IF_DEF_PARAMS (ifp);
65
66 /* Check if BFD is enabled */
67 if (!params->bfd_info)
68 return;
69 bfd_info = (struct bfd_info *)params->bfd_info;
70
71 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
72 zlog_debug ("%s nbr (%s) with BFD",
73 bfd_get_command_dbg_str(command),
74 inet_ntoa (nbr->src));
75
76 bfd_peer_sendmsg (zclient, bfd_info, AF_INET,
77 &nbr->src, NULL, ifp->name, 0, 0, command, 0);
78 }
79
80 /*
81 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
82 * neighbor state is changed to/from 2way.
83 */
84 void
85 ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
86 {
87 if ((old_state < NSM_TwoWay) && (state >= NSM_TwoWay))
88 ospf_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_REGISTER);
89 else if ((old_state >= NSM_TwoWay) && (state < NSM_TwoWay))
90 ospf_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_DEREGISTER);
91 }
92
93 /*
94 * ospf_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
95 * with a interface with BFD through
96 * zebra for starting/stopping the monitoring of
97 * the neighbor rechahability.
98 */
99 static int
100 ospf_bfd_reg_dereg_all_nbr (struct interface *ifp, int command)
101 {
102 struct ospf_interface *oi;
103 struct route_table *nbrs;
104 struct ospf_neighbor *nbr;
105 struct route_node *irn;
106 struct route_node *nrn;
107
108 for (irn = route_top (IF_OIFS (ifp)); irn; irn = route_next (irn))
109 {
110 if ((oi = irn->info) == NULL)
111 continue;
112
113 if ((nbrs = oi->nbrs) == NULL)
114 continue;
115
116 for (nrn = route_top (nbrs); nrn; nrn = route_next (nrn))
117 {
118 if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
119 continue;
120
121 if (nbr->state < NSM_TwoWay)
122 continue;
123
124 ospf_bfd_reg_dereg_nbr(nbr, command);
125 }
126 }
127
128 return 0;
129 }
130
131 /*
132 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
133 * to zebra
134 */
135 static int
136 ospf_bfd_nbr_replay (int command, struct zclient *client, zebra_size_t length)
137 {
138 struct listnode *inode, *node, *onode;
139 struct ospf *ospf;
140 struct ospf_interface *oi;
141 struct route_table *nbrs;
142 struct route_node *rn;
143 struct ospf_neighbor *nbr;
144 struct ospf_if_params *params;
145
146 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
147 {
148 zlog_debug("Zebra: BFD Dest replay request");
149 }
150
151 /* Replay the neighbor, if BFD is enabled in BGP */
152 for (ALL_LIST_ELEMENTS (om->ospf, node, onode, ospf))
153 {
154 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, inode, oi))
155 {
156 if ((nbrs = oi->nbrs) == NULL)
157 continue;
158
159 params = IF_DEF_PARAMS (oi->ifp);
160 if (!params->bfd_info)
161 continue;
162
163 for (rn = route_top (nbrs); rn; rn = route_next (rn))
164 {
165 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
166 continue;
167
168 if (nbr->state < NSM_TwoWay)
169 continue;
170
171 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
172 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr->src));
173
174 ospf_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_UPDATE);
175 }
176 }
177 }
178 return 0;
179 }
180
181 /*
182 * ospf_bfd_interface_dest_down - Find the neighbor for which the BFD status
183 * has changed and bring down the neighbor
184 * connectivity.
185 */
186 static int
187 ospf_bfd_interface_dest_down (int command, struct zclient *zclient,
188 zebra_size_t length)
189 {
190 struct interface *ifp;
191 struct ospf_interface *oi;
192 struct ospf_if_params *params;
193 struct ospf_neighbor *nbr;
194 struct route_node *node;
195 struct prefix p;
196
197 ifp = bfd_get_peer_info (zclient->ibuf, &p, NULL);
198
199 if ((ifp == NULL) || (p.family != AF_INET))
200 return 0;
201
202 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
203 {
204 char buf[128];
205 prefix2str(&p, buf, sizeof(buf));
206 zlog_debug("Zebra: interface %s bfd destination %s down", ifp->name, buf);
207 }
208
209 params = IF_DEF_PARAMS (ifp);
210 if (!params->bfd_info)
211 return 0;
212
213 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
214 {
215 if ((oi = node->info) == NULL)
216 continue;
217
218 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &p.u.prefix4);
219 if (!nbr)
220 continue;
221
222 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
223 zlog_debug ("NSM[%s:%s]: BFD Down",
224 IF_NAME (nbr->oi), inet_ntoa (nbr->address.u.prefix4));
225
226 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_InactivityTimer);
227 }
228
229 return 0;
230 }
231
232 /*
233 * ospf_bfd_write_config - Write the interface BFD configuration.
234 */
235 void
236 ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
237
238 {
239 struct bfd_info *bfd_info;
240
241 if (!params->bfd_info)
242 return;
243
244 bfd_info = (struct bfd_info *)params->bfd_info;
245
246 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
247 vty_out (vty, " ip ospf bfd %d %d %d%s",
248 bfd_info->detect_mult, bfd_info->required_min_rx,
249 bfd_info->desired_min_tx, VTY_NEWLINE);
250 else
251 vty_out (vty, " ip ospf bfd%s", VTY_NEWLINE);
252 }
253
254
255 /*
256 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
257 * interface.
258 */
259 static void
260 ospf_bfd_if_param_set (struct interface *ifp, u_int32_t min_rx,
261 u_int32_t min_tx, u_int8_t detect_mult, int defaults)
262 {
263 struct ospf_if_params *params;
264 int command = 0;
265
266 params = IF_DEF_PARAMS (ifp);
267
268 bfd_set_param(&(params->bfd_info), min_rx, min_tx, detect_mult,
269 defaults, &command);
270 if (command)
271 ospf_bfd_reg_dereg_all_nbr(ifp, command);
272 }
273
274
275 DEFUN (ip_ospf_bfd,
276 ip_ospf_bfd_cmd,
277 "ip ospf bfd",
278 "IP Information\n"
279 "OSPF interface commands\n"
280 "Enables BFD support\n")
281 {
282 struct interface *ifp = (struct interface *) vty->index;
283
284 assert (ifp);
285 ospf_bfd_if_param_set (ifp, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX,
286 BFD_DEF_DETECT_MULT, 1);
287
288 return CMD_SUCCESS;
289 }
290
291 DEFUN (ip_ospf_bfd_param,
292 ip_ospf_bfd_param_cmd,
293 "ip ospf bfd " BFD_CMD_DETECT_MULT_RANGE BFD_CMD_MIN_RX_RANGE BFD_CMD_MIN_TX_RANGE,
294 "IP Information\n"
295 "OSPF interface commands\n"
296 "Enables BFD support\n"
297 "Detect Multiplier\n"
298 "Required min receive interval\n"
299 "Desired min transmit interval\n")
300 {
301 struct interface *ifp = (struct interface *) vty->index;
302 u_int32_t rx_val;
303 u_int32_t tx_val;
304 u_int8_t dm_val;
305 int ret;
306
307 assert (ifp);
308
309 if ((ret = bfd_validate_param (vty, argv[0], argv[1], argv[2], &dm_val,
310 &rx_val, &tx_val)) != CMD_SUCCESS)
311 return ret;
312
313 ospf_bfd_if_param_set (ifp, rx_val, tx_val, dm_val, 0);
314
315 return CMD_SUCCESS;
316 }
317
318 DEFUN (no_ip_ospf_bfd,
319 no_ip_ospf_bfd_cmd,
320 "no ip ospf bfd",
321 NO_STR
322 "IP Information\n"
323 "OSPF interface commands\n"
324 "Disables BFD support\n")
325 {
326 struct interface *ifp = (struct interface *)vty->index;
327 struct ospf_if_params *params;
328
329 assert (ifp);
330
331 params = IF_DEF_PARAMS (ifp);
332 if (params->bfd_info)
333 {
334 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
335 bfd_info_free(&(params->bfd_info));
336 }
337
338 return CMD_SUCCESS;
339 }
340
341 void
342 ospf_bfd_init(void)
343 {
344 /* Initialize BFD client functions */
345 zclient->interface_bfd_dest_down = ospf_bfd_interface_dest_down;
346 zclient->bfd_dest_replay = ospf_bfd_nbr_replay;
347
348 /* Install BFD command */
349 install_element (INTERFACE_NODE, &ip_ospf_bfd_cmd);
350 install_element (INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
351 install_element (INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
352 }