]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_bfd.c
Merge pull request #5305 from ton31337/feature/draft-ietf-idr-deprecate-as-set-confed-set
[mirror_frr.git] / ospfd / ospf_bfd.c
CommitLineData
7f342629
DS
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 *
896014f4
DL
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
7f342629
DS
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
47extern struct zclient *zclient;
48
68fe91d6 49/*
50 * ospf_bfd_info_free - Free BFD info structure
51 */
d62a17ae 52void ospf_bfd_info_free(void **bfd_info)
68fe91d6 53{
d62a17ae 54 bfd_info_free((struct bfd_info **)bfd_info);
68fe91d6 55}
56
7f342629
DS
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 */
d62a17ae 62static void ospf_bfd_reg_dereg_nbr(struct ospf_neighbor *nbr, int command)
7f342629 63{
d62a17ae 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;
9beff0bd 68 int cbit;
d62a17ae 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))
b5a8894d 79 zlog_debug("%s nbr (%s) with BFD. OSPF vrf %s",
d62a17ae 80 bfd_get_command_dbg_str(command),
b5a8894d
CS
81 inet_ntoa(nbr->src),
82 ospf_vrf_id_to_name(oi->ospf->vrf_id));
d62a17ae 83
9beff0bd
PG
84 cbit = CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CBIT_ON);
85
d62a17ae 86 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->src, NULL, ifp->name,
9beff0bd 87 0, 0, cbit, command, 0, oi->ospf->vrf_id);
7f342629
DS
88}
89
90/*
91 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
92 * neighbor state is changed to/from 2way.
93 */
d62a17ae 94void ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
7f342629 95{
d62a17ae 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);
7f342629
DS
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 */
d62a17ae 108static int ospf_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
7f342629 109{
d62a17ae 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;
7f342629
DS
141}
142
143/*
144 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
145 * to zebra
146 */
121f9dee 147static int ospf_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
7f342629 148{
d62a17ae 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 */
0945d5ed 162 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
d62a17ae 163
b5a8894d 164 /* Replay the neighbor, if BFD is enabled in OSPF */
d62a17ae 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 (%s) to BFD",
184 inet_ntoa(nbr->src));
185
186 ospf_bfd_reg_dereg_nbr(nbr,
187 ZEBRA_BFD_DEST_UPDATE);
188 }
189 }
190 }
191 return 0;
7f342629
DS
192}
193
194/*
68fe91d6 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.
7f342629 199 */
121f9dee 200static int ospf_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
7f342629 201{
d62a17ae 202 struct interface *ifp;
203 struct ospf_interface *oi;
204 struct ospf_if_params *params;
6e641a8f 205 struct ospf_neighbor *nbr = NULL;
d62a17ae 206 struct route_node *node;
6e641a8f 207 struct route_node *n_node;
d62a17ae 208 struct prefix p;
209 int status;
210 int old_status;
211 struct bfd_info *bfd_info;
212 struct timeval tv;
213
9beff0bd
PG
214 ifp = bfd_get_peer_info(zclient->ibuf, &p, NULL, &status,
215 NULL, vrf_id);
d62a17ae 216
217 if ((ifp == NULL) || (p.family != AF_INET))
218 return 0;
219
220 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
221 char buf[PREFIX2STR_BUFFER];
222 prefix2str(&p, buf, sizeof(buf));
223 zlog_debug("Zebra: interface %s bfd destination %s %s",
224 ifp->name, buf, bfd_get_status_str(status));
225 }
226
227 params = IF_DEF_PARAMS(ifp);
228 if (!params->bfd_info)
229 return 0;
230
231 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
232 if ((oi = node->info) == NULL)
233 continue;
234
6e641a8f
RM
235 /* walk the neighbor list for point-to-point network */
236 if (oi->type == OSPF_IFTYPE_POINTOPOINT) {
237 for (n_node = route_top(oi->nbrs); n_node;
238 n_node = route_next(n_node)) {
239 nbr = n_node->info;
240 if (nbr) {
241 /* skip myself */
242 if (nbr == oi->nbr_self) {
243 nbr = NULL;
244 continue;
245 }
246
247 /* Found the matching neighbor */
248 if (nbr->src.s_addr ==
249 p.u.prefix4.s_addr)
250 break;
251 }
252 }
253 } else {
254 nbr = ospf_nbr_lookup_by_addr(oi->nbrs, &p.u.prefix4);
255 }
256
d62a17ae 257 if (!nbr || !nbr->bfd_info)
258 continue;
259
260 bfd_info = (struct bfd_info *)nbr->bfd_info;
261 if (bfd_info->status == status)
262 continue;
263
264 old_status = bfd_info->status;
7555dc61 265 BFD_SET_CLIENT_STATUS(bfd_info->status, status);
d62a17ae 266 monotime(&tv);
267 bfd_info->last_update = tv.tv_sec;
268
269 if ((status == BFD_STATUS_DOWN)
270 && (old_status == BFD_STATUS_UP)) {
271 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
272 zlog_debug("NSM[%s:%s]: BFD Down",
273 IF_NAME(nbr->oi),
274 inet_ntoa(nbr->address.u.prefix4));
275
276 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_InactivityTimer);
277 }
4828ea77
PG
278 if ((status == BFD_STATUS_UP)
279 && (old_status == BFD_STATUS_DOWN)) {
280 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
281 zlog_debug("NSM[%s:%s]: BFD Up",
282 IF_NAME(nbr->oi),
283 inet_ntoa(nbr->address.u.prefix4));
284 }
d62a17ae 285 }
286
287 return 0;
7f342629
DS
288}
289
68fe91d6 290/*
291 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
292 */
d62a17ae 293void ospf_bfd_info_nbr_create(struct ospf_interface *oi,
294 struct ospf_neighbor *nbr)
68fe91d6 295{
d62a17ae 296 struct bfd_info *oi_bfd_info;
297 struct bfd_info *nbr_bfd_info;
298 struct interface *ifp = oi->ifp;
299 struct ospf_if_params *params;
300
301 /* Check if BFD is enabled */
302 params = IF_DEF_PARAMS(ifp);
303
304 /* Check if BFD is enabled */
305 if (!params->bfd_info)
306 return;
307
308 oi_bfd_info = (struct bfd_info *)params->bfd_info;
309 if (!nbr->bfd_info)
310 nbr->bfd_info = bfd_info_create();
311
312 nbr_bfd_info = (struct bfd_info *)nbr->bfd_info;
313 nbr_bfd_info->detect_mult = oi_bfd_info->detect_mult;
314 nbr_bfd_info->desired_min_tx = oi_bfd_info->desired_min_tx;
315 nbr_bfd_info->required_min_rx = oi_bfd_info->required_min_rx;
68fe91d6 316}
317
7f342629
DS
318/*
319 * ospf_bfd_write_config - Write the interface BFD configuration.
320 */
d62a17ae 321void ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
7f342629
DS
322
323{
a0841732 324#if HAVE_BFDD == 0
d62a17ae 325 struct bfd_info *bfd_info;
a0841732 326#endif /* ! HAVE_BFDD */
7f342629 327
d62a17ae 328 if (!params->bfd_info)
329 return;
7f342629 330
a0841732 331#if HAVE_BFDD == 0
d62a17ae 332 bfd_info = (struct bfd_info *)params->bfd_info;
7f342629 333
d62a17ae 334 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
335 vty_out(vty, " ip ospf bfd %d %d %d\n", bfd_info->detect_mult,
336 bfd_info->required_min_rx, bfd_info->desired_min_tx);
337 else
a0841732 338#endif /* ! HAVE_BFDD */
d62a17ae 339 vty_out(vty, " ip ospf bfd\n");
7f342629
DS
340}
341
68fe91d6 342/*
343 * ospf_bfd_show_info - Show BFD info structure
344 */
d62a17ae 345void ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
9f049418 346 bool use_json, int param_only)
68fe91d6 347{
d62a17ae 348 if (param_only)
349 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json,
350 json_obj);
351 else
352 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json,
353 json_obj);
68fe91d6 354}
355
356/*
357 * ospf_bfd_interface_show - Show the interface BFD configuration.
358 */
d62a17ae 359void ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
9f049418 360 json_object *json_interface_sub, bool use_json)
68fe91d6 361{
d62a17ae 362 struct ospf_if_params *params;
68fe91d6 363
d62a17ae 364 params = IF_DEF_PARAMS(ifp);
68fe91d6 365
d62a17ae 366 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json,
367 1);
68fe91d6 368}
7f342629
DS
369
370/*
371 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
372 * interface.
373 */
d7c0a89a
QY
374static void ospf_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
375 uint32_t min_tx, uint8_t detect_mult,
d62a17ae 376 int defaults)
7f342629 377{
d62a17ae 378 struct ospf_if_params *params;
379 int command = 0;
7f342629 380
d62a17ae 381 params = IF_DEF_PARAMS(ifp);
7f342629 382
d62a17ae 383 bfd_set_param((struct bfd_info **)&(params->bfd_info), min_rx, min_tx,
384 detect_mult, defaults, &command);
385 if (command)
386 ospf_bfd_reg_dereg_all_nbr(ifp, command);
7f342629
DS
387}
388
7f342629
DS
389DEFUN (ip_ospf_bfd,
390 ip_ospf_bfd_cmd,
391 "ip ospf bfd",
392 "IP Information\n"
393 "OSPF interface commands\n"
394 "Enables BFD support\n")
395{
d62a17ae 396 VTY_DECLVAR_CONTEXT(interface, ifp);
397 struct ospf_if_params *params;
398 struct bfd_info *bfd_info;
7f342629 399
d62a17ae 400 assert(ifp);
401 params = IF_DEF_PARAMS(ifp);
402 bfd_info = params->bfd_info;
487f2302 403
d62a17ae 404 if (!bfd_info || !CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
405 ospf_bfd_if_param_set(ifp, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX,
406 BFD_DEF_DETECT_MULT, 1);
7f342629 407
d62a17ae 408 return CMD_SUCCESS;
7f342629
DS
409}
410
64dc4b2d
RZ
411#if HAVE_BFDD > 0
412DEFUN_HIDDEN(
413#else
414DEFUN(
415#endif /* HAVE_BFDD */
416 ip_ospf_bfd_param,
7f342629 417 ip_ospf_bfd_param_cmd,
9ccf14f7 418 "ip ospf bfd (2-255) (50-60000) (50-60000)",
7f342629
DS
419 "IP Information\n"
420 "OSPF interface commands\n"
421 "Enables BFD support\n"
422 "Detect Multiplier\n"
423 "Required min receive interval\n"
424 "Desired min transmit interval\n")
425{
d62a17ae 426 VTY_DECLVAR_CONTEXT(interface, ifp);
427 int idx_number = 3;
428 int idx_number_2 = 4;
429 int idx_number_3 = 5;
d7c0a89a
QY
430 uint32_t rx_val;
431 uint32_t tx_val;
432 uint8_t dm_val;
d62a17ae 433 int ret;
434
435 assert(ifp);
436
437 if ((ret = bfd_validate_param(
438 vty, argv[idx_number]->arg, argv[idx_number_2]->arg,
439 argv[idx_number_3]->arg, &dm_val, &rx_val, &tx_val))
440 != CMD_SUCCESS)
441 return ret;
442
443 ospf_bfd_if_param_set(ifp, rx_val, tx_val, dm_val, 0);
444
445 return CMD_SUCCESS;
7f342629
DS
446}
447
448DEFUN (no_ip_ospf_bfd,
449 no_ip_ospf_bfd_cmd,
64dc4b2d
RZ
450#if HAVE_BFDD > 0
451 "no ip ospf bfd",
452#else
692b4c65 453 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
64dc4b2d 454#endif /* HAVE_BFDD */
7f342629
DS
455 NO_STR
456 "IP Information\n"
457 "OSPF interface commands\n"
692b4c65 458 "Disables BFD support\n"
64dc4b2d 459#if HAVE_BFDD == 0
692b4c65
QY
460 "Detect Multiplier\n"
461 "Required min receive interval\n"
64dc4b2d
RZ
462 "Desired min transmit interval\n"
463#endif /* !HAVE_BFDD */
464)
7f342629 465{
d62a17ae 466 VTY_DECLVAR_CONTEXT(interface, ifp);
467 struct ospf_if_params *params;
7f342629 468
d62a17ae 469 assert(ifp);
7f342629 470
d62a17ae 471 params = IF_DEF_PARAMS(ifp);
472 if (params->bfd_info) {
473 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
474 bfd_info_free(&(params->bfd_info));
475 }
7f342629 476
d62a17ae 477 return CMD_SUCCESS;
7f342629
DS
478}
479
d62a17ae 480void ospf_bfd_init(void)
7f342629 481{
d62a17ae 482 bfd_gbl_init();
567b877d 483
d62a17ae 484 /* Initialize BFD client functions */
485 zclient->interface_bfd_dest_update = ospf_bfd_interface_dest_update;
486 zclient->bfd_dest_replay = ospf_bfd_nbr_replay;
7f342629 487
d62a17ae 488 /* Install BFD command */
489 install_element(INTERFACE_NODE, &ip_ospf_bfd_cmd);
490 install_element(INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
491 install_element(INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
7f342629 492}