]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_bfd.c
Merge pull request #5366 from ton31337/fix/addpath_total_peer_update_7.1
[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;
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))
b5a8894d 78 zlog_debug("%s nbr (%s) with BFD. OSPF vrf %s",
d62a17ae 79 bfd_get_command_dbg_str(command),
b5a8894d
CS
80 inet_ntoa(nbr->src),
81 ospf_vrf_id_to_name(oi->ospf->vrf_id));
d62a17ae 82
83 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->src, NULL, ifp->name,
b5a8894d 84 0, 0, command, 0, oi->ospf->vrf_id);
7f342629
DS
85}
86
87/*
88 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
89 * neighbor state is changed to/from 2way.
90 */
d62a17ae 91void ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
7f342629 92{
d62a17ae 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);
7f342629
DS
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 */
d62a17ae 105static int ospf_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
7f342629 106{
d62a17ae 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;
7f342629
DS
138}
139
140/*
141 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
142 * to zebra
143 */
121f9dee 144static int ospf_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
7f342629 145{
d62a17ae 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);
160
b5a8894d 161 /* Replay the neighbor, if BFD is enabled in OSPF */
d62a17ae 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;
7f342629
DS
189}
190
191/*
68fe91d6 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.
7f342629 196 */
121f9dee 197static int ospf_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
7f342629 198{
d62a17ae 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 }
4828ea77
PG
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 }
d62a17ae 259 }
260
261 return 0;
7f342629
DS
262}
263
68fe91d6 264/*
265 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
266 */
d62a17ae 267void ospf_bfd_info_nbr_create(struct ospf_interface *oi,
268 struct ospf_neighbor *nbr)
68fe91d6 269{
d62a17ae 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;
68fe91d6 290}
291
7f342629
DS
292/*
293 * ospf_bfd_write_config - Write the interface BFD configuration.
294 */
d62a17ae 295void ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
7f342629
DS
296
297{
a0841732 298#if HAVE_BFDD == 0
d62a17ae 299 struct bfd_info *bfd_info;
a0841732 300#endif /* ! HAVE_BFDD */
7f342629 301
d62a17ae 302 if (!params->bfd_info)
303 return;
7f342629 304
a0841732 305#if HAVE_BFDD == 0
d62a17ae 306 bfd_info = (struct bfd_info *)params->bfd_info;
7f342629 307
d62a17ae 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
a0841732 312#endif /* ! HAVE_BFDD */
d62a17ae 313 vty_out(vty, " ip ospf bfd\n");
7f342629
DS
314}
315
68fe91d6 316/*
317 * ospf_bfd_show_info - Show BFD info structure
318 */
d62a17ae 319void ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
9f049418 320 bool use_json, int param_only)
68fe91d6 321{
d62a17ae 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);
68fe91d6 328}
329
330/*
331 * ospf_bfd_interface_show - Show the interface BFD configuration.
332 */
d62a17ae 333void ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
9f049418 334 json_object *json_interface_sub, bool use_json)
68fe91d6 335{
d62a17ae 336 struct ospf_if_params *params;
68fe91d6 337
d62a17ae 338 params = IF_DEF_PARAMS(ifp);
68fe91d6 339
d62a17ae 340 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json,
341 1);
68fe91d6 342}
7f342629
DS
343
344/*
345 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
346 * interface.
347 */
d7c0a89a
QY
348static void ospf_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
349 uint32_t min_tx, uint8_t detect_mult,
d62a17ae 350 int defaults)
7f342629 351{
d62a17ae 352 struct ospf_if_params *params;
353 int command = 0;
7f342629 354
d62a17ae 355 params = IF_DEF_PARAMS(ifp);
7f342629 356
d62a17ae 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);
7f342629
DS
361}
362
7f342629
DS
363DEFUN (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{
d62a17ae 370 VTY_DECLVAR_CONTEXT(interface, ifp);
371 struct ospf_if_params *params;
372 struct bfd_info *bfd_info;
7f342629 373
d62a17ae 374 assert(ifp);
375 params = IF_DEF_PARAMS(ifp);
376 bfd_info = params->bfd_info;
487f2302 377
d62a17ae 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);
7f342629 381
d62a17ae 382 return CMD_SUCCESS;
7f342629
DS
383}
384
64dc4b2d
RZ
385#if HAVE_BFDD > 0
386DEFUN_HIDDEN(
387#else
388DEFUN(
389#endif /* HAVE_BFDD */
390 ip_ospf_bfd_param,
7f342629 391 ip_ospf_bfd_param_cmd,
9ccf14f7 392 "ip ospf bfd (2-255) (50-60000) (50-60000)",
7f342629
DS
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{
d62a17ae 400 VTY_DECLVAR_CONTEXT(interface, ifp);
401 int idx_number = 3;
402 int idx_number_2 = 4;
403 int idx_number_3 = 5;
d7c0a89a
QY
404 uint32_t rx_val;
405 uint32_t tx_val;
406 uint8_t dm_val;
d62a17ae 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;
7f342629
DS
420}
421
422DEFUN (no_ip_ospf_bfd,
423 no_ip_ospf_bfd_cmd,
64dc4b2d
RZ
424#if HAVE_BFDD > 0
425 "no ip ospf bfd",
426#else
692b4c65 427 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
64dc4b2d 428#endif /* HAVE_BFDD */
7f342629
DS
429 NO_STR
430 "IP Information\n"
431 "OSPF interface commands\n"
692b4c65 432 "Disables BFD support\n"
64dc4b2d 433#if HAVE_BFDD == 0
692b4c65
QY
434 "Detect Multiplier\n"
435 "Required min receive interval\n"
64dc4b2d
RZ
436 "Desired min transmit interval\n"
437#endif /* !HAVE_BFDD */
438)
7f342629 439{
d62a17ae 440 VTY_DECLVAR_CONTEXT(interface, ifp);
441 struct ospf_if_params *params;
7f342629 442
d62a17ae 443 assert(ifp);
7f342629 444
d62a17ae 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 }
7f342629 450
d62a17ae 451 return CMD_SUCCESS;
7f342629
DS
452}
453
d62a17ae 454void ospf_bfd_init(void)
7f342629 455{
d62a17ae 456 bfd_gbl_init();
567b877d 457
d62a17ae 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;
7f342629 461
d62a17ae 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);
7f342629 466}