]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
Merge pull request #4316 from LabNConsulting/working/master/tt-scale-vrfs
[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 ospf_bfd_info_free(void **bfd_info)
53 {
54 bfd_info_free((struct bfd_info **)bfd_info);
55 }
56
57 /*
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.
61 */
62 static void ospf_bfd_reg_dereg_nbr(struct ospf_neighbor *nbr, int command)
63 {
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;
68
69 /* Check if BFD is enabled */
70 params = IF_DEF_PARAMS(ifp);
71
72 /* Check if BFD is enabled */
73 if (!params->bfd_info)
74 return;
75 bfd_info = (struct bfd_info *)params->bfd_info;
76
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),
80 inet_ntoa(nbr->src),
81 ospf_vrf_id_to_name(oi->ospf->vrf_id));
82
83 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->src, NULL, ifp->name,
84 0, 0, command, 0, oi->ospf->vrf_id);
85 }
86
87 /*
88 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
89 * neighbor state is changed to/from 2way.
90 */
91 void ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
92 {
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);
97 }
98
99 /*
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.
104 */
105 static int ospf_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
106 {
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;
112
113 for (irn = route_top(IF_OIFS(ifp)); irn; irn = route_next(irn)) {
114 if ((oi = irn->info) == NULL)
115 continue;
116
117 if ((nbrs = oi->nbrs) == NULL)
118 continue;
119
120 for (nrn = route_top(nbrs); nrn; nrn = route_next(nrn)) {
121 if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
122 continue;
123
124 if (command != ZEBRA_BFD_DEST_DEREGISTER)
125 ospf_bfd_info_nbr_create(oi, nbr);
126 else
127 bfd_info_free(
128 (struct bfd_info **)&nbr->bfd_info);
129
130 if (nbr->state < NSM_TwoWay)
131 continue;
132
133 ospf_bfd_reg_dereg_nbr(nbr, command);
134 }
135 }
136
137 return 0;
138 }
139
140 /*
141 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
142 * to zebra
143 */
144 static int ospf_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
145 {
146 struct listnode *inode, *node, *onode;
147 struct ospf *ospf;
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;
153
154 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
155 zlog_debug("Zebra: BFD Dest replay request");
156 }
157
158 /* Send the client registration */
159 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
160
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)
165 continue;
166
167 params = IF_DEF_PARAMS(oi->ifp);
168 if (!params->bfd_info)
169 continue;
170
171 for (rn = route_top(nbrs); rn; rn = route_next(rn)) {
172 if ((nbr = rn->info) == NULL
173 || nbr == oi->nbr_self)
174 continue;
175
176 if (nbr->state < NSM_TwoWay)
177 continue;
178
179 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
180 zlog_debug("Replaying nbr (%s) to BFD",
181 inet_ntoa(nbr->src));
182
183 ospf_bfd_reg_dereg_nbr(nbr,
184 ZEBRA_BFD_DEST_UPDATE);
185 }
186 }
187 }
188 return 0;
189 }
190
191 /*
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
195 * down.
196 */
197 static int ospf_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
198 {
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;
204 struct prefix p;
205 int status;
206 int old_status;
207 struct bfd_info *bfd_info;
208 struct timeval tv;
209
210 ifp = bfd_get_peer_info(zclient->ibuf, &p, NULL, &status, vrf_id);
211
212 if ((ifp == NULL) || (p.family != AF_INET))
213 return 0;
214
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));
220 }
221
222 params = IF_DEF_PARAMS(ifp);
223 if (!params->bfd_info)
224 return 0;
225
226 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
227 if ((oi = node->info) == NULL)
228 continue;
229
230 nbr = ospf_nbr_lookup_by_addr(oi->nbrs, &p.u.prefix4);
231 if (!nbr || !nbr->bfd_info)
232 continue;
233
234 bfd_info = (struct bfd_info *)nbr->bfd_info;
235 if (bfd_info->status == status)
236 continue;
237
238 old_status = bfd_info->status;
239 bfd_info->status = status;
240 monotime(&tv);
241 bfd_info->last_update = tv.tv_sec;
242
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",
247 IF_NAME(nbr->oi),
248 inet_ntoa(nbr->address.u.prefix4));
249
250 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_InactivityTimer);
251 }
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",
256 IF_NAME(nbr->oi),
257 inet_ntoa(nbr->address.u.prefix4));
258 }
259 }
260
261 return 0;
262 }
263
264 /*
265 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
266 */
267 void ospf_bfd_info_nbr_create(struct ospf_interface *oi,
268 struct ospf_neighbor *nbr)
269 {
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;
274
275 /* Check if BFD is enabled */
276 params = IF_DEF_PARAMS(ifp);
277
278 /* Check if BFD is enabled */
279 if (!params->bfd_info)
280 return;
281
282 oi_bfd_info = (struct bfd_info *)params->bfd_info;
283 if (!nbr->bfd_info)
284 nbr->bfd_info = bfd_info_create();
285
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;
290 }
291
292 /*
293 * ospf_bfd_write_config - Write the interface BFD configuration.
294 */
295 void ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
296
297 {
298 #if HAVE_BFDD == 0
299 struct bfd_info *bfd_info;
300 #endif /* ! HAVE_BFDD */
301
302 if (!params->bfd_info)
303 return;
304
305 #if HAVE_BFDD == 0
306 bfd_info = (struct bfd_info *)params->bfd_info;
307
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);
311 else
312 #endif /* ! HAVE_BFDD */
313 vty_out(vty, " ip ospf bfd\n");
314 }
315
316 /*
317 * ospf_bfd_show_info - Show BFD info structure
318 */
319 void ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
320 bool use_json, int param_only)
321 {
322 if (param_only)
323 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json,
324 json_obj);
325 else
326 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json,
327 json_obj);
328 }
329
330 /*
331 * ospf_bfd_interface_show - Show the interface BFD configuration.
332 */
333 void ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
334 json_object *json_interface_sub, bool use_json)
335 {
336 struct ospf_if_params *params;
337
338 params = IF_DEF_PARAMS(ifp);
339
340 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json,
341 1);
342 }
343
344 /*
345 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
346 * interface.
347 */
348 static void ospf_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
349 uint32_t min_tx, uint8_t detect_mult,
350 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 #if HAVE_BFDD > 0
386 DEFUN_HIDDEN(
387 #else
388 DEFUN(
389 #endif /* HAVE_BFDD */
390 ip_ospf_bfd_param,
391 ip_ospf_bfd_param_cmd,
392 "ip ospf bfd (2-255) (50-60000) (50-60000)",
393 "IP Information\n"
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")
399 {
400 VTY_DECLVAR_CONTEXT(interface, ifp);
401 int idx_number = 3;
402 int idx_number_2 = 4;
403 int idx_number_3 = 5;
404 uint32_t rx_val;
405 uint32_t tx_val;
406 uint8_t dm_val;
407 int ret;
408
409 assert(ifp);
410
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))
414 != CMD_SUCCESS)
415 return ret;
416
417 ospf_bfd_if_param_set(ifp, rx_val, tx_val, dm_val, 0);
418
419 return CMD_SUCCESS;
420 }
421
422 DEFUN (no_ip_ospf_bfd,
423 no_ip_ospf_bfd_cmd,
424 #if HAVE_BFDD > 0
425 "no ip ospf bfd",
426 #else
427 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
428 #endif /* HAVE_BFDD */
429 NO_STR
430 "IP Information\n"
431 "OSPF interface commands\n"
432 "Disables BFD support\n"
433 #if HAVE_BFDD == 0
434 "Detect Multiplier\n"
435 "Required min receive interval\n"
436 "Desired min transmit interval\n"
437 #endif /* !HAVE_BFDD */
438 )
439 {
440 VTY_DECLVAR_CONTEXT(interface, ifp);
441 struct ospf_if_params *params;
442
443 assert(ifp);
444
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));
449 }
450
451 return CMD_SUCCESS;
452 }
453
454 void ospf_bfd_init(void)
455 {
456 bfd_gbl_init();
457
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;
461
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);
466 }