]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
Merge branch stable/2.0 into stable/3.0
[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_info_free - Free BFD info structure
52 */
53 void
54 ospf_bfd_info_free(void **bfd_info)
55 {
56 bfd_info_free((struct bfd_info **) bfd_info);
57 }
58
59 /*
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.
63 */
64 static void
65 ospf_bfd_reg_dereg_nbr (struct ospf_neighbor *nbr, int command)
66 {
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;
71
72 /* Check if BFD is enabled */
73 params = IF_DEF_PARAMS (ifp);
74
75 /* Check if BFD is enabled */
76 if (!params->bfd_info)
77 return;
78 bfd_info = (struct bfd_info *)params->bfd_info;
79
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));
84
85 bfd_peer_sendmsg (zclient, bfd_info, AF_INET,
86 &nbr->src, NULL, ifp->name, 0, 0, command, 0, VRF_DEFAULT);
87 }
88
89 /*
90 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
91 * neighbor state is changed to/from 2way.
92 */
93 void
94 ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
95 {
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);
100 }
101
102 /*
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.
107 */
108 static int
109 ospf_bfd_reg_dereg_all_nbr (struct interface *ifp, int command)
110 {
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;
116
117 for (irn = route_top (IF_OIFS (ifp)); irn; irn = route_next (irn))
118 {
119 if ((oi = irn->info) == NULL)
120 continue;
121
122 if ((nbrs = oi->nbrs) == NULL)
123 continue;
124
125 for (nrn = route_top (nbrs); nrn; nrn = route_next (nrn))
126 {
127 if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
128 continue;
129
130 if (command != ZEBRA_BFD_DEST_DEREGISTER)
131 ospf_bfd_info_nbr_create(oi, nbr);
132 else
133 bfd_info_free((struct bfd_info **)&nbr->bfd_info);
134
135 if (nbr->state < NSM_TwoWay)
136 continue;
137
138 ospf_bfd_reg_dereg_nbr(nbr, command);
139 }
140 }
141
142 return 0;
143 }
144
145 /*
146 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
147 * to zebra
148 */
149 static int
150 ospf_bfd_nbr_replay (int command, struct zclient *zclient, zebra_size_t length,
151 vrf_id_t vrf_id)
152 {
153 struct listnode *inode, *node, *onode;
154 struct ospf *ospf;
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;
160
161 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
162 {
163 zlog_debug("Zebra: BFD Dest replay request");
164 }
165
166 /* Send the client registration */
167 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
168
169 /* Replay the neighbor, if BFD is enabled in BGP */
170 for (ALL_LIST_ELEMENTS (om->ospf, node, onode, ospf))
171 {
172 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, inode, oi))
173 {
174 if ((nbrs = oi->nbrs) == NULL)
175 continue;
176
177 params = IF_DEF_PARAMS (oi->ifp);
178 if (!params->bfd_info)
179 continue;
180
181 for (rn = route_top (nbrs); rn; rn = route_next (rn))
182 {
183 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
184 continue;
185
186 if (nbr->state < NSM_TwoWay)
187 continue;
188
189 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
190 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr->src));
191
192 ospf_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_UPDATE);
193 }
194 }
195 }
196 return 0;
197 }
198
199 /*
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
203 * down.
204 */
205 static int
206 ospf_bfd_interface_dest_update (int command, struct zclient *zclient,
207 zebra_size_t length, vrf_id_t vrf_id)
208 {
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;
214 struct prefix p;
215 int status;
216 int old_status;
217 struct bfd_info *bfd_info;
218 struct timeval tv;
219
220 ifp = bfd_get_peer_info (zclient->ibuf, &p, NULL, &status, vrf_id);
221
222 if ((ifp == NULL) || (p.family != AF_INET))
223 return 0;
224
225 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
226 {
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));
231 }
232
233 params = IF_DEF_PARAMS (ifp);
234 if (!params->bfd_info)
235 return 0;
236
237 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
238 {
239 if ((oi = node->info) == NULL)
240 continue;
241
242 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &p.u.prefix4);
243 if (!nbr || !nbr->bfd_info)
244 continue;
245
246 bfd_info = (struct bfd_info *)nbr->bfd_info;
247 if (bfd_info->status == status)
248 continue;
249
250 old_status = bfd_info->status;
251 bfd_info->status = status;
252 monotime(&tv);
253 bfd_info->last_update = tv.tv_sec;
254
255 if ((status == BFD_STATUS_DOWN) && (old_status == BFD_STATUS_UP))
256 {
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));
260
261 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_InactivityTimer);
262 }
263 }
264
265 return 0;
266 }
267
268 /*
269 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
270 */
271 void
272 ospf_bfd_info_nbr_create (struct ospf_interface *oi, struct ospf_neighbor *nbr)
273 {
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;
278
279 /* Check if BFD is enabled */
280 params = IF_DEF_PARAMS (ifp);
281
282 /* Check if BFD is enabled */
283 if (!params->bfd_info)
284 return;
285
286 oi_bfd_info = (struct bfd_info *)params->bfd_info;
287 if (!nbr->bfd_info)
288 nbr->bfd_info = bfd_info_create();
289
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;
294 }
295
296 /*
297 * ospf_bfd_write_config - Write the interface BFD configuration.
298 */
299 void
300 ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
301
302 {
303 struct bfd_info *bfd_info;
304
305 if (!params->bfd_info)
306 return;
307
308 bfd_info = (struct bfd_info *)params->bfd_info;
309
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);
314 else
315 vty_out (vty, " ip ospf bfd%s", VTY_NEWLINE);
316 }
317
318 /*
319 * ospf_bfd_show_info - Show BFD info structure
320 */
321 void
322 ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
323 u_char use_json, int param_only)
324 {
325 if (param_only)
326 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json, json_obj);
327 else
328 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json, json_obj);
329 }
330
331 /*
332 * ospf_bfd_interface_show - Show the interface BFD configuration.
333 */
334 void
335 ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
336 json_object *json_interface_sub, u_char use_json)
337 {
338 struct ospf_if_params *params;
339
340 params = IF_DEF_PARAMS (ifp);
341
342 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json, 1);
343 }
344
345 /*
346 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
347 * interface.
348 */
349 static void
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)
352 {
353 struct ospf_if_params *params;
354 int command = 0;
355
356 params = IF_DEF_PARAMS (ifp);
357
358 bfd_set_param((struct bfd_info **)&(params->bfd_info), min_rx, min_tx,
359 detect_mult, defaults, &command);
360 if (command)
361 ospf_bfd_reg_dereg_all_nbr(ifp, command);
362 }
363
364 DEFUN (ip_ospf_bfd,
365 ip_ospf_bfd_cmd,
366 "ip ospf bfd",
367 "IP Information\n"
368 "OSPF interface commands\n"
369 "Enables BFD support\n")
370 {
371 VTY_DECLVAR_CONTEXT(interface, ifp);
372 struct ospf_if_params *params;
373 struct bfd_info *bfd_info;
374
375 assert (ifp);
376 params = IF_DEF_PARAMS (ifp);
377 bfd_info = params->bfd_info;
378
379 if (!bfd_info || ! CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
380 ospf_bfd_if_param_set (ifp, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX,
381 BFD_DEF_DETECT_MULT, 1);
382
383 return CMD_SUCCESS;
384 }
385
386 DEFUN (ip_ospf_bfd_param,
387 ip_ospf_bfd_param_cmd,
388 "ip ospf bfd (2-255) (50-60000) (50-60000)",
389 "IP Information\n"
390 "OSPF interface commands\n"
391 "Enables BFD support\n"
392 "Detect Multiplier\n"
393 "Required min receive interval\n"
394 "Desired min transmit interval\n")
395 {
396 VTY_DECLVAR_CONTEXT(interface, ifp);
397 int idx_number = 3;
398 int idx_number_2 = 4;
399 int idx_number_3 = 5;
400 u_int32_t rx_val;
401 u_int32_t tx_val;
402 u_int8_t dm_val;
403 int ret;
404
405 assert (ifp);
406
407 if ((ret = bfd_validate_param (vty, argv[idx_number]->arg, argv[idx_number_2]->arg, argv[idx_number_3]->arg, &dm_val,
408 &rx_val, &tx_val)) != CMD_SUCCESS)
409 return ret;
410
411 ospf_bfd_if_param_set (ifp, rx_val, tx_val, dm_val, 0);
412
413 return CMD_SUCCESS;
414 }
415
416 DEFUN (no_ip_ospf_bfd,
417 no_ip_ospf_bfd_cmd,
418 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
419 NO_STR
420 "IP Information\n"
421 "OSPF interface commands\n"
422 "Disables BFD support\n"
423 "Detect Multiplier\n"
424 "Required min receive interval\n"
425 "Desired min transmit interval\n")
426 {
427 VTY_DECLVAR_CONTEXT(interface, ifp);
428 struct ospf_if_params *params;
429
430 assert (ifp);
431
432 params = IF_DEF_PARAMS (ifp);
433 if (params->bfd_info)
434 {
435 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
436 bfd_info_free(&(params->bfd_info));
437 }
438
439 return CMD_SUCCESS;
440 }
441
442 void
443 ospf_bfd_init(void)
444 {
445 bfd_gbl_init();
446
447 /* Initialize BFD client functions */
448 zclient->interface_bfd_dest_update = ospf_bfd_interface_dest_update;
449 zclient->bfd_dest_replay = ospf_bfd_nbr_replay;
450
451 /* Install BFD command */
452 install_element (INTERFACE_NODE, &ip_ospf_bfd_cmd);
453 install_element (INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
454 install_element (INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
455 }