]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
Merge remote-tracking branch 'origin/master' into evpn_plus_struct_attr
[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 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
21 */
22
23 #include <zebra.h>
24
25 #include "command.h"
26 #include "linklist.h"
27 #include "memory.h"
28 #include "prefix.h"
29 #include "thread.h"
30 #include "buffer.h"
31 #include "stream.h"
32 #include "zclient.h"
33 #include "vty.h"
34 #include "table.h"
35 #include "bfd.h"
36 #include "ospfd.h"
37 #include "ospf_asbr.h"
38 #include "ospf_lsa.h"
39 #include "ospf_lsdb.h"
40 #include "ospf_neighbor.h"
41 #include "ospf_interface.h"
42 #include "ospf_nsm.h"
43 #include "ospf_bfd.h"
44 #include "ospf_dump.h"
45 #include "ospf_vty.h"
46
47 extern struct zclient *zclient;
48
49 /*
50 * ospf_bfd_info_free - Free BFD info structure
51 */
52 void
53 ospf_bfd_info_free(void **bfd_info)
54 {
55 bfd_info_free((struct bfd_info **) bfd_info);
56 }
57
58 /*
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.
62 */
63 static void
64 ospf_bfd_reg_dereg_nbr (struct ospf_neighbor *nbr, int command)
65 {
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;
70
71 /* Check if BFD is enabled */
72 params = IF_DEF_PARAMS (ifp);
73
74 /* Check if BFD is enabled */
75 if (!params->bfd_info)
76 return;
77 bfd_info = (struct bfd_info *)params->bfd_info;
78
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));
83
84 bfd_peer_sendmsg (zclient, bfd_info, AF_INET,
85 &nbr->src, NULL, ifp->name, 0, 0, command, 0, VRF_DEFAULT);
86 }
87
88 /*
89 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
90 * neighbor state is changed to/from 2way.
91 */
92 void
93 ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
94 {
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);
99 }
100
101 /*
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.
106 */
107 static int
108 ospf_bfd_reg_dereg_all_nbr (struct interface *ifp, int command)
109 {
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;
115
116 for (irn = route_top (IF_OIFS (ifp)); irn; irn = route_next (irn))
117 {
118 if ((oi = irn->info) == NULL)
119 continue;
120
121 if ((nbrs = oi->nbrs) == NULL)
122 continue;
123
124 for (nrn = route_top (nbrs); nrn; nrn = route_next (nrn))
125 {
126 if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
127 continue;
128
129 if (command != ZEBRA_BFD_DEST_DEREGISTER)
130 ospf_bfd_info_nbr_create(oi, nbr);
131 else
132 bfd_info_free((struct bfd_info **)&nbr->bfd_info);
133
134 if (nbr->state < NSM_TwoWay)
135 continue;
136
137 ospf_bfd_reg_dereg_nbr(nbr, command);
138 }
139 }
140
141 return 0;
142 }
143
144 /*
145 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
146 * to zebra
147 */
148 static int
149 ospf_bfd_nbr_replay (int command, struct zclient *zclient, zebra_size_t length,
150 vrf_id_t vrf_id)
151 {
152 struct listnode *inode, *node, *onode;
153 struct ospf *ospf;
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;
159
160 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
161 {
162 zlog_debug("Zebra: BFD Dest replay request");
163 }
164
165 /* Send the client registration */
166 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
167
168 /* Replay the neighbor, if BFD is enabled in BGP */
169 for (ALL_LIST_ELEMENTS (om->ospf, node, onode, ospf))
170 {
171 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, inode, oi))
172 {
173 if ((nbrs = oi->nbrs) == NULL)
174 continue;
175
176 params = IF_DEF_PARAMS (oi->ifp);
177 if (!params->bfd_info)
178 continue;
179
180 for (rn = route_top (nbrs); rn; rn = route_next (rn))
181 {
182 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
183 continue;
184
185 if (nbr->state < NSM_TwoWay)
186 continue;
187
188 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
189 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr->src));
190
191 ospf_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_UPDATE);
192 }
193 }
194 }
195 return 0;
196 }
197
198 /*
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
202 * down.
203 */
204 static int
205 ospf_bfd_interface_dest_update (int command, struct zclient *zclient,
206 zebra_size_t length, vrf_id_t vrf_id)
207 {
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;
213 struct prefix p;
214 int status;
215 int old_status;
216 struct bfd_info *bfd_info;
217 struct timeval tv;
218
219 ifp = bfd_get_peer_info (zclient->ibuf, &p, NULL, &status, vrf_id);
220
221 if ((ifp == NULL) || (p.family != AF_INET))
222 return 0;
223
224 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
225 {
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));
230 }
231
232 params = IF_DEF_PARAMS (ifp);
233 if (!params->bfd_info)
234 return 0;
235
236 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
237 {
238 if ((oi = node->info) == NULL)
239 continue;
240
241 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &p.u.prefix4);
242 if (!nbr || !nbr->bfd_info)
243 continue;
244
245 bfd_info = (struct bfd_info *)nbr->bfd_info;
246 if (bfd_info->status == status)
247 continue;
248
249 old_status = bfd_info->status;
250 bfd_info->status = status;
251 monotime(&tv);
252 bfd_info->last_update = tv.tv_sec;
253
254 if ((status == BFD_STATUS_DOWN) && (old_status == BFD_STATUS_UP))
255 {
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));
259
260 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_InactivityTimer);
261 }
262 }
263
264 return 0;
265 }
266
267 /*
268 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
269 */
270 void
271 ospf_bfd_info_nbr_create (struct ospf_interface *oi, struct ospf_neighbor *nbr)
272 {
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;
277
278 /* Check if BFD is enabled */
279 params = IF_DEF_PARAMS (ifp);
280
281 /* Check if BFD is enabled */
282 if (!params->bfd_info)
283 return;
284
285 oi_bfd_info = (struct bfd_info *)params->bfd_info;
286 if (!nbr->bfd_info)
287 nbr->bfd_info = bfd_info_create();
288
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;
293 }
294
295 /*
296 * ospf_bfd_write_config - Write the interface BFD configuration.
297 */
298 void
299 ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
300
301 {
302 struct bfd_info *bfd_info;
303
304 if (!params->bfd_info)
305 return;
306
307 bfd_info = (struct bfd_info *)params->bfd_info;
308
309 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
310 vty_out (vty, " ip ospf bfd %d %d %d\n",
311 bfd_info->detect_mult, bfd_info->required_min_rx,
312 bfd_info->desired_min_tx);
313 else
314 vty_out (vty, " ip ospf bfd\n");
315 }
316
317 /*
318 * ospf_bfd_show_info - Show BFD info structure
319 */
320 void
321 ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
322 u_char use_json, int param_only)
323 {
324 if (param_only)
325 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json, json_obj);
326 else
327 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json, json_obj);
328 }
329
330 /*
331 * ospf_bfd_interface_show - Show the interface BFD configuration.
332 */
333 void
334 ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
335 json_object *json_interface_sub, u_char use_json)
336 {
337 struct ospf_if_params *params;
338
339 params = IF_DEF_PARAMS (ifp);
340
341 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json, 1);
342 }
343
344 /*
345 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
346 * interface.
347 */
348 static void
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)
351 {
352 struct ospf_if_params *params;
353 int command = 0;
354
355 params = IF_DEF_PARAMS (ifp);
356
357 bfd_set_param((struct bfd_info **)&(params->bfd_info), min_rx, min_tx,
358 detect_mult, defaults, &command);
359 if (command)
360 ospf_bfd_reg_dereg_all_nbr(ifp, command);
361 }
362
363 DEFUN (ip_ospf_bfd,
364 ip_ospf_bfd_cmd,
365 "ip ospf bfd",
366 "IP Information\n"
367 "OSPF interface commands\n"
368 "Enables BFD support\n")
369 {
370 VTY_DECLVAR_CONTEXT(interface, ifp);
371 struct ospf_if_params *params;
372 struct bfd_info *bfd_info;
373
374 assert (ifp);
375 params = IF_DEF_PARAMS (ifp);
376 bfd_info = params->bfd_info;
377
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);
381
382 return CMD_SUCCESS;
383 }
384
385 DEFUN (ip_ospf_bfd_param,
386 ip_ospf_bfd_param_cmd,
387 "ip ospf bfd (2-255) (50-60000) (50-60000)",
388 "IP Information\n"
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")
394 {
395 VTY_DECLVAR_CONTEXT(interface, ifp);
396 int idx_number = 3;
397 int idx_number_2 = 4;
398 int idx_number_3 = 5;
399 u_int32_t rx_val;
400 u_int32_t tx_val;
401 u_int8_t dm_val;
402 int ret;
403
404 assert (ifp);
405
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)
408 return ret;
409
410 ospf_bfd_if_param_set (ifp, rx_val, tx_val, dm_val, 0);
411
412 return CMD_SUCCESS;
413 }
414
415 DEFUN (no_ip_ospf_bfd,
416 no_ip_ospf_bfd_cmd,
417 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
418 NO_STR
419 "IP Information\n"
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")
425 {
426 VTY_DECLVAR_CONTEXT(interface, ifp);
427 struct ospf_if_params *params;
428
429 assert (ifp);
430
431 params = IF_DEF_PARAMS (ifp);
432 if (params->bfd_info)
433 {
434 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
435 bfd_info_free(&(params->bfd_info));
436 }
437
438 return CMD_SUCCESS;
439 }
440
441 void
442 ospf_bfd_init(void)
443 {
444 bfd_gbl_init();
445
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;
449
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);
454 }