]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
Support of BFD status in Quagga
[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
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
48 extern struct zclient *zclient;
49
50 /*
51 * ospf_bfd_info_free - Free BFD info structure
52 */
53 void
54 ospf_bfd_info_free(void **bfd_info)
55 {
56 bfd_info_free((struct bfd_info **) bfd_info);
57 }
58
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 */
64 static void
65 ospf_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,
86 &nbr->src, NULL, ifp->name, 0, 0, command, 0);
87 }
88
89 /*
90 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
91 * neighbor state is changed to/from 2way.
92 */
93 void
94 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
109 ospf_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
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
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 */
149 static int
150 ospf_bfd_nbr_replay (int command, struct zclient *client, zebra_size_t length)
151 {
152 struct listnode *inode, *node, *onode;
153 struct ospf *ospf;
154 struct ospf_interface *oi;
155 struct route_table *nbrs;
156 struct route_node *rn;
157 struct ospf_neighbor *nbr;
158 struct ospf_if_params *params;
159
160 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
161 {
162 zlog_debug("Zebra: BFD Dest replay request");
163 }
164
165 /* Replay the neighbor, if BFD is enabled in BGP */
166 for (ALL_LIST_ELEMENTS (om->ospf, node, onode, ospf))
167 {
168 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, inode, oi))
169 {
170 if ((nbrs = oi->nbrs) == NULL)
171 continue;
172
173 params = IF_DEF_PARAMS (oi->ifp);
174 if (!params->bfd_info)
175 continue;
176
177 for (rn = route_top (nbrs); rn; rn = route_next (rn))
178 {
179 if ((nbr = rn->info) == NULL || nbr == oi->nbr_self)
180 continue;
181
182 if (nbr->state < NSM_TwoWay)
183 continue;
184
185 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
186 zlog_debug ("Replaying nbr (%s) to BFD", inet_ntoa (nbr->src));
187
188 ospf_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_UPDATE);
189 }
190 }
191 }
192 return 0;
193 }
194
195 /*
196 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
197 * has changed and bring down the neighbor
198 * connectivity if the BFD status changed to
199 * down.
200 */
201 static int
202 ospf_bfd_interface_dest_update (int command, struct zclient *zclient,
203 zebra_size_t length)
204 {
205 struct interface *ifp;
206 struct ospf_interface *oi;
207 struct ospf_if_params *params;
208 struct ospf_neighbor *nbr;
209 struct route_node *node;
210 struct prefix p;
211 int status;
212 int old_status;
213 struct bfd_info *bfd_info;
214 struct timeval tv;
215
216 ifp = bfd_get_peer_info (zclient->ibuf, &p, NULL, &status);
217
218 if ((ifp == NULL) || (p.family != AF_INET))
219 return 0;
220
221 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
222 {
223 char buf[128];
224 prefix2str(&p, buf, sizeof(buf));
225 zlog_debug("Zebra: interface %s bfd destination %s %s", ifp->name, buf,
226 bfd_get_status_str(status));
227 }
228
229 params = IF_DEF_PARAMS (ifp);
230 if (!params->bfd_info)
231 return 0;
232
233 for (node = route_top (IF_OIFS (ifp)); node; node = route_next (node))
234 {
235 if ((oi = node->info) == NULL)
236 continue;
237
238 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &p.u.prefix4);
239 if (!nbr || !nbr->bfd_info)
240 continue;
241
242 bfd_info = (struct bfd_info *)nbr->bfd_info;
243 if (bfd_info->status == status)
244 continue;
245
246 old_status = bfd_info->status;
247 bfd_info->status = status;
248 quagga_gettime (QUAGGA_CLK_MONOTONIC, &tv);
249 bfd_info->last_update = tv.tv_sec;
250
251 if ((status == BFD_STATUS_DOWN) && (old_status == BFD_STATUS_UP))
252 {
253 if (IS_DEBUG_OSPF (nsm, NSM_EVENTS))
254 zlog_debug ("NSM[%s:%s]: BFD Down",
255 IF_NAME (nbr->oi), inet_ntoa (nbr->address.u.prefix4));
256
257 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_InactivityTimer);
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
268 ospf_bfd_info_nbr_create (struct ospf_interface *oi, 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
296 ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
297
298 {
299 struct bfd_info *bfd_info;
300
301 if (!params->bfd_info)
302 return;
303
304 bfd_info = (struct bfd_info *)params->bfd_info;
305
306 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
307 vty_out (vty, " ip ospf bfd %d %d %d%s",
308 bfd_info->detect_mult, bfd_info->required_min_rx,
309 bfd_info->desired_min_tx, VTY_NEWLINE);
310 else
311 vty_out (vty, " ip ospf bfd%s", VTY_NEWLINE);
312 }
313
314 /*
315 * ospf_bfd_show_info - Show BFD info structure
316 */
317 void
318 ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
319 u_char use_json, int param_only)
320 {
321 if (param_only)
322 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json, json_obj);
323 else
324 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json, json_obj);
325 }
326
327 /*
328 * ospf_bfd_interface_show - Show the interface BFD configuration.
329 */
330 void
331 ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
332 json_object *json_interface_sub, u_char use_json)
333 {
334 struct ospf_if_params *params;
335
336 params = IF_DEF_PARAMS (ifp);
337
338 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json, 1);
339 }
340
341 /*
342 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
343 * interface.
344 */
345 static void
346 ospf_bfd_if_param_set (struct interface *ifp, u_int32_t min_rx,
347 u_int32_t min_tx, u_int8_t detect_mult, int defaults)
348 {
349 struct ospf_if_params *params;
350 int command = 0;
351
352 params = IF_DEF_PARAMS (ifp);
353
354 bfd_set_param((struct bfd_info **)&(params->bfd_info), min_rx, min_tx,
355 detect_mult, defaults, &command);
356 if (command)
357 ospf_bfd_reg_dereg_all_nbr(ifp, command);
358 }
359
360 DEFUN (ip_ospf_bfd,
361 ip_ospf_bfd_cmd,
362 "ip ospf bfd",
363 "IP Information\n"
364 "OSPF interface commands\n"
365 "Enables BFD support\n")
366 {
367 struct interface *ifp = (struct interface *) vty->index;
368
369 assert (ifp);
370 ospf_bfd_if_param_set (ifp, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX,
371 BFD_DEF_DETECT_MULT, 1);
372
373 return CMD_SUCCESS;
374 }
375
376 DEFUN (ip_ospf_bfd_param,
377 ip_ospf_bfd_param_cmd,
378 "ip ospf bfd " BFD_CMD_DETECT_MULT_RANGE BFD_CMD_MIN_RX_RANGE BFD_CMD_MIN_TX_RANGE,
379 "IP Information\n"
380 "OSPF interface commands\n"
381 "Enables BFD support\n"
382 "Detect Multiplier\n"
383 "Required min receive interval\n"
384 "Desired min transmit interval\n")
385 {
386 struct interface *ifp = (struct interface *) vty->index;
387 u_int32_t rx_val;
388 u_int32_t tx_val;
389 u_int8_t dm_val;
390 int ret;
391
392 assert (ifp);
393
394 if ((ret = bfd_validate_param (vty, argv[0], argv[1], argv[2], &dm_val,
395 &rx_val, &tx_val)) != CMD_SUCCESS)
396 return ret;
397
398 ospf_bfd_if_param_set (ifp, rx_val, tx_val, dm_val, 0);
399
400 return CMD_SUCCESS;
401 }
402
403 DEFUN (no_ip_ospf_bfd,
404 no_ip_ospf_bfd_cmd,
405 "no ip ospf bfd",
406 NO_STR
407 "IP Information\n"
408 "OSPF interface commands\n"
409 "Disables BFD support\n")
410 {
411 struct interface *ifp = (struct interface *)vty->index;
412 struct ospf_if_params *params;
413
414 assert (ifp);
415
416 params = IF_DEF_PARAMS (ifp);
417 if (params->bfd_info)
418 {
419 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
420 bfd_info_free(&(params->bfd_info));
421 }
422
423 return CMD_SUCCESS;
424 }
425
426 void
427 ospf_bfd_init(void)
428 {
429 /* Initialize BFD client functions */
430 zclient->interface_bfd_dest_update = ospf_bfd_interface_dest_update;
431 zclient->bfd_dest_replay = ospf_bfd_nbr_replay;
432
433 /* Install BFD command */
434 install_element (INTERFACE_NODE, &ip_ospf_bfd_cmd);
435 install_element (INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
436 install_element (INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
437 }