]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_bfd.c
build: improve COMMUNITY.md formatting
[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 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#include <zebra.h>
25
26#include "command.h"
27#include "linklist.h"
28#include "memory.h"
29#include "prefix.h"
30#include "thread.h"
31#include "buffer.h"
32#include "stream.h"
33#include "zclient.h"
34#include "vty.h"
35#include "table.h"
36#include "bfd.h"
37#include "ospfd.h"
38#include "ospf_asbr.h"
39#include "ospf_lsa.h"
40#include "ospf_lsdb.h"
41#include "ospf_neighbor.h"
42#include "ospf_interface.h"
43#include "ospf_nsm.h"
44#include "ospf_bfd.h"
45#include "ospf_dump.h"
46#include "ospf_vty.h"
47
48extern struct zclient *zclient;
49
68fe91d6 50/*
51 * ospf_bfd_info_free - Free BFD info structure
52 */
53void
54ospf_bfd_info_free(void **bfd_info)
55{
56 bfd_info_free((struct bfd_info **) bfd_info);
57}
58
7f342629
DS
59/*
60 * ospf_bfd_reg_dereg_nbr - Register/Deregister a neighbor with BFD through
61 * zebra for starting/stopping the monitoring of
62 * the neighbor rechahability.
63 */
64static void
65ospf_bfd_reg_dereg_nbr (struct ospf_neighbor *nbr, int command)
66{
67 struct ospf_interface *oi = nbr->oi;
68 struct interface *ifp = oi->ifp;
69 struct ospf_if_params *params;
70 struct bfd_info *bfd_info;
71
72 /* Check if BFD is enabled */
73 params = IF_DEF_PARAMS (ifp);
74
75 /* Check if BFD is enabled */
76 if (!params->bfd_info)
77 return;
78 bfd_info = (struct bfd_info *)params->bfd_info;
79
80 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
81 zlog_debug ("%s nbr (%s) with BFD",
82 bfd_get_command_dbg_str(command),
83 inet_ntoa (nbr->src));
84
85 bfd_peer_sendmsg (zclient, bfd_info, AF_INET,
7076bb2f 86 &nbr->src, NULL, ifp->name, 0, 0, command, 0, VRF_DEFAULT);
7f342629
DS
87}
88
89/*
90 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
91 * neighbor state is changed to/from 2way.
92 */
93void
94ospf_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 */
108static int
109ospf_bfd_reg_dereg_all_nbr (struct interface *ifp, int command)
110{
111 struct ospf_interface *oi;
112 struct route_table *nbrs;
113 struct ospf_neighbor *nbr;
114 struct route_node *irn;
115 struct route_node *nrn;
116
117 for (irn = route_top (IF_OIFS (ifp)); irn; irn = route_next (irn))
118 {
119 if ((oi = irn->info) == NULL)
120 continue;
121
122 if ((nbrs = oi->nbrs) == NULL)
123 continue;
124
125 for (nrn = route_top (nbrs); nrn; nrn = route_next (nrn))
126 {
127 if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
128 continue;
129
68fe91d6 130 if (command != ZEBRA_BFD_DEST_DEREGISTER)
131 ospf_bfd_info_nbr_create(oi, nbr);
132 else
133 bfd_info_free((struct bfd_info **)&nbr->bfd_info);
134
7f342629
DS
135 if (nbr->state < NSM_TwoWay)
136 continue;
137
138 ospf_bfd_reg_dereg_nbr(nbr, command);
139 }
140 }
141
142 return 0;
143}
144
145/*
146 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
147 * to zebra
148 */
149static int
055c4dfc 150ospf_bfd_nbr_replay (int command, struct zclient *zclient, zebra_size_t length,
7076bb2f 151 vrf_id_t vrf_id)
7f342629
DS
152{
153 struct listnode *inode, *node, *onode;
154 struct ospf *ospf;
155 struct ospf_interface *oi;
156 struct route_table *nbrs;
157 struct route_node *rn;
158 struct ospf_neighbor *nbr;
159 struct ospf_if_params *params;
160
161 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
162 {
163 zlog_debug("Zebra: BFD Dest replay request");
164 }
165
055c4dfc 166 /* Send the client registration */
167 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
168
7f342629
DS
169 /* Replay the neighbor, if BFD is enabled in BGP */
170 for (ALL_LIST_ELEMENTS (om->ospf, node, onode, ospf))
171 {
172 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, inode, oi))
173 {
174 if ((nbrs = oi->nbrs) == NULL)
175 continue;
176
177 params = IF_DEF_PARAMS (oi->ifp);
178 if (!params->bfd_info)
179 continue;
180
181 for (rn = route_top (nbrs); rn; rn = route_next (rn))
182 {
183 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
184 continue;
185
186 if (nbr->state < NSM_TwoWay)
187 continue;
188
189 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
190 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr->src));
191
192 ospf_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_UPDATE);
193 }
194 }
195 }
196 return 0;
197}
198
199/*
68fe91d6 200 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
201 * has changed and bring down the neighbor
202 * connectivity if the BFD status changed to
203 * down.
7f342629
DS
204 */
205static int
68fe91d6 206ospf_bfd_interface_dest_update (int command, struct zclient *zclient,
7076bb2f 207 zebra_size_t length, vrf_id_t vrf_id)
7f342629
DS
208{
209 struct interface *ifp;
210 struct ospf_interface *oi;
211 struct ospf_if_params *params;
212 struct ospf_neighbor *nbr;
213 struct route_node *node;
214 struct prefix p;
68fe91d6 215 int status;
216 int old_status;
217 struct bfd_info *bfd_info;
218 struct timeval tv;
7f342629 219
1e22a2af 220 ifp = bfd_get_peer_info (zclient->ibuf, &p, NULL, &status, vrf_id);
7f342629
DS
221
222 if ((ifp == NULL) || (p.family != AF_INET))
223 return 0;
224
225 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
226 {
4690c7d7 227 char buf[PREFIX2STR_BUFFER];
7f342629 228 prefix2str(&p, buf, sizeof(buf));
68fe91d6 229 zlog_debug("Zebra: interface %s bfd destination %s %s", ifp->name, buf,
230 bfd_get_status_str(status));
7f342629
DS
231 }
232
233 params = IF_DEF_PARAMS (ifp);
234 if (!params->bfd_info)
235 return 0;
236
237 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
238 {
239 if ((oi = node->info) == NULL)
240 continue;
241
242 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &p.u.prefix4);
68fe91d6 243 if (!nbr || !nbr->bfd_info)
244 continue;
245
246 bfd_info = (struct bfd_info *)nbr->bfd_info;
247 if (bfd_info->status == status)
7f342629
DS
248 continue;
249
68fe91d6 250 old_status = bfd_info->status;
251 bfd_info->status = status;
252 quagga_gettime (QUAGGA_CLK_MONOTONIC, &tv);
253 bfd_info->last_update = tv.tv_sec;
254
255 if ((status == BFD_STATUS_DOWN) && (old_status == BFD_STATUS_UP))
256 {
257 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
258 zlog_debug ("NSM[%s:%s]: BFD Down",
259 IF_NAME (nbr->oi), inet_ntoa (nbr->address.u.prefix4));
7f342629 260
68fe91d6 261 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_InactivityTimer);
262 }
7f342629
DS
263 }
264
265 return 0;
266}
267
68fe91d6 268/*
269 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
270 */
271void
272ospf_bfd_info_nbr_create (struct ospf_interface *oi, struct ospf_neighbor *nbr)
273{
274 struct bfd_info *oi_bfd_info;
275 struct bfd_info *nbr_bfd_info;
276 struct interface *ifp = oi->ifp;
277 struct ospf_if_params *params;
278
279 /* Check if BFD is enabled */
280 params = IF_DEF_PARAMS (ifp);
281
282 /* Check if BFD is enabled */
283 if (!params->bfd_info)
284 return;
285
286 oi_bfd_info = (struct bfd_info *)params->bfd_info;
287 if (!nbr->bfd_info)
288 nbr->bfd_info = bfd_info_create();
289
290 nbr_bfd_info = (struct bfd_info *)nbr->bfd_info;
291 nbr_bfd_info->detect_mult = oi_bfd_info->detect_mult;
292 nbr_bfd_info->desired_min_tx = oi_bfd_info->desired_min_tx;
293 nbr_bfd_info->required_min_rx = oi_bfd_info->required_min_rx;
294}
295
7f342629
DS
296/*
297 * ospf_bfd_write_config - Write the interface BFD configuration.
298 */
299void
300ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
301
302{
303 struct bfd_info *bfd_info;
304
305 if (!params->bfd_info)
306 return;
307
308 bfd_info = (struct bfd_info *)params->bfd_info;
309
310 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
311 vty_out (vty, " ip ospf bfd %d %d %d%s",
312 bfd_info->detect_mult, bfd_info->required_min_rx,
313 bfd_info->desired_min_tx, VTY_NEWLINE);
314 else
315 vty_out (vty, " ip ospf bfd%s", VTY_NEWLINE);
316}
317
68fe91d6 318/*
319 * ospf_bfd_show_info - Show BFD info structure
320 */
321void
322ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
323 u_char use_json, int param_only)
324{
325 if (param_only)
326 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json, json_obj);
327 else
328 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json, json_obj);
329}
330
331/*
332 * ospf_bfd_interface_show - Show the interface BFD configuration.
333 */
334void
335ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
336 json_object *json_interface_sub, u_char use_json)
337{
338 struct ospf_if_params *params;
339
340 params = IF_DEF_PARAMS (ifp);
341
342 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json, 1);
343}
7f342629
DS
344
345/*
346 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
347 * interface.
348 */
349static void
350ospf_bfd_if_param_set (struct interface *ifp, u_int32_t min_rx,
351 u_int32_t min_tx, u_int8_t detect_mult, int defaults)
352{
353 struct ospf_if_params *params;
354 int command = 0;
355
356 params = IF_DEF_PARAMS (ifp);
357
68fe91d6 358 bfd_set_param((struct bfd_info **)&(params->bfd_info), min_rx, min_tx,
359 detect_mult, defaults, &command);
7f342629
DS
360 if (command)
361 ospf_bfd_reg_dereg_all_nbr(ifp, command);
362}
363
7f342629
DS
364DEFUN (ip_ospf_bfd,
365 ip_ospf_bfd_cmd,
366 "ip ospf bfd",
367 "IP Information\n"
368 "OSPF interface commands\n"
369 "Enables BFD support\n")
370{
371 struct interface *ifp = (struct interface *) vty->index;
372
373 assert (ifp);
374 ospf_bfd_if_param_set (ifp, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX,
375 BFD_DEF_DETECT_MULT, 1);
376
377 return CMD_SUCCESS;
378}
379
380DEFUN (ip_ospf_bfd_param,
381 ip_ospf_bfd_param_cmd,
382 "ip ospf bfd " BFD_CMD_DETECT_MULT_RANGE BFD_CMD_MIN_RX_RANGE BFD_CMD_MIN_TX_RANGE,
383 "IP Information\n"
384 "OSPF interface commands\n"
385 "Enables BFD support\n"
386 "Detect Multiplier\n"
387 "Required min receive interval\n"
388 "Desired min transmit interval\n")
389{
390 struct interface *ifp = (struct interface *) vty->index;
391 u_int32_t rx_val;
392 u_int32_t tx_val;
393 u_int8_t dm_val;
394 int ret;
395
396 assert (ifp);
397
398 if ((ret = bfd_validate_param (vty, argv[0], argv[1], argv[2], &dm_val,
399 &rx_val, &tx_val)) != CMD_SUCCESS)
400 return ret;
401
402 ospf_bfd_if_param_set (ifp, rx_val, tx_val, dm_val, 0);
403
404 return CMD_SUCCESS;
405}
406
407DEFUN (no_ip_ospf_bfd,
408 no_ip_ospf_bfd_cmd,
409 "no ip ospf bfd",
410 NO_STR
411 "IP Information\n"
412 "OSPF interface commands\n"
413 "Disables BFD support\n")
414{
415 struct interface *ifp = (struct interface *)vty->index;
416 struct ospf_if_params *params;
417
418 assert (ifp);
419
420 params = IF_DEF_PARAMS (ifp);
421 if (params->bfd_info)
422 {
423 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
424 bfd_info_free(&(params->bfd_info));
425 }
426
427 return CMD_SUCCESS;
428}
429
813d4307
DW
430ALIAS (no_ip_ospf_bfd,
431 no_ip_ospf_bfd_param_cmd,
432 "no ip ospf bfd " BFD_CMD_DETECT_MULT_RANGE BFD_CMD_MIN_RX_RANGE BFD_CMD_MIN_TX_RANGE,
433 NO_STR
434 "IP Information\n"
435 "OSPF interface commands\n"
436 "Enables BFD support\n"
437 "Detect Multiplier\n"
438 "Required min receive interval\n"
439 "Desired min transmit interval\n")
440
7f342629
DS
441void
442ospf_bfd_init(void)
443{
567b877d 444 bfd_gbl_init();
445
7f342629 446 /* Initialize BFD client functions */
68fe91d6 447 zclient->interface_bfd_dest_update = ospf_bfd_interface_dest_update;
7f342629
DS
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);
813d4307 454 install_element (INTERFACE_NODE, &no_ip_ospf_bfd_param_cmd);
7f342629 455}