]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
Merge pull request #8038 from imzyxwvu/same-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 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 int cbit;
69
70 /* Check if BFD is enabled */
71 params = IF_DEF_PARAMS(ifp);
72
73 /* Check if BFD is enabled */
74 if (!params->bfd_info)
75 return;
76 bfd_info = (struct bfd_info *)params->bfd_info;
77
78 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
79 zlog_debug("%s nbr (%pI4) with BFD. OSPF vrf %s",
80 bfd_get_command_dbg_str(command),
81 &nbr->src,
82 ospf_vrf_id_to_name(oi->ospf->vrf_id));
83
84 cbit = CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CBIT_ON);
85
86 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->src, NULL, ifp->name,
87 0, 0, cbit, command, 0, oi->ospf->vrf_id);
88 }
89
90 /*
91 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
92 * neighbor state is changed to/from 2way.
93 */
94 void 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 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 if ((oi = irn->info) == NULL)
118 continue;
119
120 if ((nbrs = oi->nbrs) == NULL)
121 continue;
122
123 for (nrn = route_top(nbrs); nrn; nrn = route_next(nrn)) {
124 if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
125 continue;
126
127 if (command != ZEBRA_BFD_DEST_DEREGISTER)
128 ospf_bfd_info_nbr_create(oi, nbr);
129 else
130 bfd_info_free(
131 (struct bfd_info **)&nbr->bfd_info);
132
133 if (nbr->state < NSM_TwoWay)
134 continue;
135
136 ospf_bfd_reg_dereg_nbr(nbr, command);
137 }
138 }
139
140 return 0;
141 }
142
143 /*
144 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
145 * to zebra
146 */
147 static int ospf_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
148 {
149 struct listnode *inode, *node, *onode;
150 struct ospf *ospf;
151 struct ospf_interface *oi;
152 struct route_table *nbrs;
153 struct route_node *rn;
154 struct ospf_neighbor *nbr;
155 struct ospf_if_params *params;
156
157 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
158 zlog_debug("Zebra: BFD Dest replay request");
159 }
160
161 /* Send the client registration */
162 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
163
164 /* Replay the neighbor, if BFD is enabled in OSPF */
165 for (ALL_LIST_ELEMENTS(om->ospf, node, onode, ospf)) {
166 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, inode, oi)) {
167 if ((nbrs = oi->nbrs) == NULL)
168 continue;
169
170 params = IF_DEF_PARAMS(oi->ifp);
171 if (!params->bfd_info)
172 continue;
173
174 for (rn = route_top(nbrs); rn; rn = route_next(rn)) {
175 if ((nbr = rn->info) == NULL
176 || nbr == oi->nbr_self)
177 continue;
178
179 if (nbr->state < NSM_TwoWay)
180 continue;
181
182 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
183 zlog_debug("Replaying nbr (%pI4) to BFD",
184 &nbr->src);
185
186 ospf_bfd_reg_dereg_nbr(nbr,
187 ZEBRA_BFD_DEST_UPDATE);
188 }
189 }
190 }
191 return 0;
192 }
193
194 /*
195 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
196 * has changed and bring down the neighbor
197 * connectivity if the BFD status changed to
198 * down.
199 */
200 static int ospf_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
201 {
202 struct interface *ifp;
203 struct ospf_interface *oi;
204 struct ospf_if_params *params;
205 struct ospf_neighbor *nbr = NULL;
206 struct route_node *node;
207 struct route_node *n_node;
208 struct prefix p, src_p;
209 int status;
210 int old_status;
211 struct bfd_info *bfd_info;
212 struct timeval tv;
213
214 ifp = bfd_get_peer_info(zclient->ibuf, &p, &src_p, &status, NULL,
215 vrf_id);
216
217 if ((ifp == NULL) || (p.family != AF_INET))
218 return 0;
219
220 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
221 zlog_debug("Zebra: interface %s bfd destination %pFX %s",
222 ifp->name, &p, bfd_get_status_str(status));
223
224 params = IF_DEF_PARAMS(ifp);
225 if (!params->bfd_info)
226 return 0;
227
228 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
229 if ((oi = node->info) == NULL)
230 continue;
231
232 /* walk the neighbor list for point-to-point network */
233 if (oi->type == OSPF_IFTYPE_POINTOPOINT) {
234 for (n_node = route_top(oi->nbrs); n_node;
235 n_node = route_next(n_node)) {
236 nbr = n_node->info;
237 if (nbr) {
238 /* skip myself */
239 if (nbr == oi->nbr_self) {
240 nbr = NULL;
241 continue;
242 }
243
244 /* Found the matching neighbor */
245 if (nbr->src.s_addr ==
246 p.u.prefix4.s_addr)
247 break;
248 }
249 }
250 } else {
251 nbr = ospf_nbr_lookup_by_addr(oi->nbrs, &p.u.prefix4);
252 }
253
254 if (!nbr || !nbr->bfd_info)
255 continue;
256
257 bfd_info = (struct bfd_info *)nbr->bfd_info;
258 if (bfd_info->status == status)
259 continue;
260
261 old_status = bfd_info->status;
262 BFD_SET_CLIENT_STATUS(bfd_info->status, status);
263 monotime(&tv);
264 bfd_info->last_update = tv.tv_sec;
265
266 if ((status == BFD_STATUS_DOWN)
267 && (old_status == BFD_STATUS_UP)) {
268 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
269 zlog_debug("NSM[%s:%pI4]: BFD Down",
270 IF_NAME(nbr->oi),
271 &nbr->address.u.prefix4);
272
273 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_InactivityTimer);
274 }
275 if ((status == BFD_STATUS_UP)
276 && (old_status == BFD_STATUS_DOWN)) {
277 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
278 zlog_debug("NSM[%s:%pI4]: BFD Up",
279 IF_NAME(nbr->oi),
280 &nbr->address.u.prefix4);
281 }
282 }
283
284 return 0;
285 }
286
287 /*
288 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
289 */
290 void ospf_bfd_info_nbr_create(struct ospf_interface *oi,
291 struct ospf_neighbor *nbr)
292 {
293 struct bfd_info *oi_bfd_info;
294 struct bfd_info *nbr_bfd_info;
295 struct interface *ifp = oi->ifp;
296 struct ospf_if_params *params;
297
298 /* Check if BFD is enabled */
299 params = IF_DEF_PARAMS(ifp);
300
301 /* Check if BFD is enabled */
302 if (!params->bfd_info)
303 return;
304
305 oi_bfd_info = (struct bfd_info *)params->bfd_info;
306 if (!nbr->bfd_info)
307 nbr->bfd_info = bfd_info_create();
308
309 nbr_bfd_info = (struct bfd_info *)nbr->bfd_info;
310 nbr_bfd_info->detect_mult = oi_bfd_info->detect_mult;
311 nbr_bfd_info->desired_min_tx = oi_bfd_info->desired_min_tx;
312 nbr_bfd_info->required_min_rx = oi_bfd_info->required_min_rx;
313 }
314
315 /*
316 * ospf_bfd_write_config - Write the interface BFD configuration.
317 */
318 void ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
319
320 {
321 #if HAVE_BFDD == 0
322 struct bfd_info *bfd_info;
323 #endif /* ! HAVE_BFDD */
324
325 if (!params->bfd_info)
326 return;
327
328 #if HAVE_BFDD == 0
329 bfd_info = (struct bfd_info *)params->bfd_info;
330
331 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
332 vty_out(vty, " ip ospf bfd %d %d %d\n", bfd_info->detect_mult,
333 bfd_info->required_min_rx, bfd_info->desired_min_tx);
334 else
335 #endif /* ! HAVE_BFDD */
336 vty_out(vty, " ip ospf bfd\n");
337 }
338
339 /*
340 * ospf_bfd_show_info - Show BFD info structure
341 */
342 void ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
343 bool use_json, int param_only)
344 {
345 if (param_only)
346 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json,
347 json_obj);
348 else
349 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json,
350 json_obj);
351 }
352
353 /*
354 * ospf_bfd_interface_show - Show the interface BFD configuration.
355 */
356 void ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
357 json_object *json_interface_sub, bool use_json)
358 {
359 struct ospf_if_params *params;
360
361 params = IF_DEF_PARAMS(ifp);
362
363 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json,
364 1);
365 }
366
367 /*
368 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
369 * interface.
370 */
371 static void ospf_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
372 uint32_t min_tx, uint8_t detect_mult,
373 int defaults)
374 {
375 struct ospf_if_params *params;
376 int command = 0;
377
378 params = IF_DEF_PARAMS(ifp);
379
380 bfd_set_param((struct bfd_info **)&(params->bfd_info), min_rx, min_tx,
381 detect_mult, NULL, defaults, &command);
382 if (command)
383 ospf_bfd_reg_dereg_all_nbr(ifp, command);
384 }
385
386 DEFUN (ip_ospf_bfd,
387 ip_ospf_bfd_cmd,
388 "ip ospf bfd",
389 "IP Information\n"
390 "OSPF interface commands\n"
391 "Enables BFD support\n")
392 {
393 VTY_DECLVAR_CONTEXT(interface, ifp);
394 struct ospf_if_params *params;
395 struct bfd_info *bfd_info;
396
397 assert(ifp);
398 params = IF_DEF_PARAMS(ifp);
399 bfd_info = params->bfd_info;
400
401 if (!bfd_info || !CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
402 ospf_bfd_if_param_set(ifp, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX,
403 BFD_DEF_DETECT_MULT, 1);
404
405 return CMD_SUCCESS;
406 }
407
408 #if HAVE_BFDD > 0
409 DEFUN_HIDDEN(
410 #else
411 DEFUN(
412 #endif /* HAVE_BFDD */
413 ip_ospf_bfd_param,
414 ip_ospf_bfd_param_cmd,
415 "ip ospf bfd (2-255) (50-60000) (50-60000)",
416 "IP Information\n"
417 "OSPF interface commands\n"
418 "Enables BFD support\n"
419 "Detect Multiplier\n"
420 "Required min receive interval\n"
421 "Desired min transmit interval\n")
422 {
423 VTY_DECLVAR_CONTEXT(interface, ifp);
424 int idx_number = 3;
425 int idx_number_2 = 4;
426 int idx_number_3 = 5;
427 uint32_t rx_val;
428 uint32_t tx_val;
429 uint8_t dm_val;
430 int ret;
431
432 assert(ifp);
433
434 if ((ret = bfd_validate_param(
435 vty, argv[idx_number]->arg, argv[idx_number_2]->arg,
436 argv[idx_number_3]->arg, &dm_val, &rx_val, &tx_val))
437 != CMD_SUCCESS)
438 return ret;
439
440 ospf_bfd_if_param_set(ifp, rx_val, tx_val, dm_val, 0);
441
442 return CMD_SUCCESS;
443 }
444
445 DEFUN (no_ip_ospf_bfd,
446 no_ip_ospf_bfd_cmd,
447 #if HAVE_BFDD > 0
448 "no ip ospf bfd",
449 #else
450 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
451 #endif /* HAVE_BFDD */
452 NO_STR
453 "IP Information\n"
454 "OSPF interface commands\n"
455 "Disables BFD support\n"
456 #if HAVE_BFDD == 0
457 "Detect Multiplier\n"
458 "Required min receive interval\n"
459 "Desired min transmit interval\n"
460 #endif /* !HAVE_BFDD */
461 )
462 {
463 VTY_DECLVAR_CONTEXT(interface, ifp);
464 struct ospf_if_params *params;
465
466 assert(ifp);
467
468 params = IF_DEF_PARAMS(ifp);
469 if (params->bfd_info) {
470 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
471 bfd_info_free(&(params->bfd_info));
472 }
473
474 return CMD_SUCCESS;
475 }
476
477 void ospf_bfd_init(void)
478 {
479 bfd_gbl_init();
480
481 /* Initialize BFD client functions */
482 zclient->interface_bfd_dest_update = ospf_bfd_interface_dest_update;
483 zclient->bfd_dest_replay = ospf_bfd_nbr_replay;
484
485 /* Install BFD command */
486 install_element(INTERFACE_NODE, &ip_ospf_bfd_cmd);
487 install_element(INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
488 install_element(INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
489 }