]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_bfd.c
Merge pull request #4312 from lkrishnamoor/json_cli_bug_fix
[mirror_frr.git] / ospf6d / ospf6_bfd.c
1 /**
2 * ospf6_bfd.c: IPv6 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 "if.h"
37 #include "ospf6d.h"
38 #include "ospf6_message.h"
39 #include "ospf6_neighbor.h"
40 #include "ospf6_interface.h"
41 #include "ospf6_route.h"
42 #include "ospf6_zebra.h"
43 #include "ospf6_bfd.h"
44
45 extern struct zclient *zclient;
46
47 /*
48 * ospf6_bfd_info_free - Free BFD info structure
49 */
50 void ospf6_bfd_info_free(void **bfd_info)
51 {
52 bfd_info_free((struct bfd_info **)bfd_info);
53 }
54
55 /*
56 * ospf6_bfd_show_info - Show BFD info structure
57 */
58 void ospf6_bfd_show_info(struct vty *vty, void *bfd_info, int param_only)
59 {
60 if (param_only)
61 bfd_show_param(vty, bfd_info, 1, 0, 0, NULL);
62 else
63 bfd_show_info(vty, bfd_info, 0, 1, 0, NULL);
64 }
65
66 /*
67 * ospf6_bfd_reg_dereg_nbr - Register/Deregister a neighbor with BFD through
68 * zebra for starting/stopping the monitoring of
69 * the neighbor rechahability.
70 */
71 void ospf6_bfd_reg_dereg_nbr(struct ospf6_neighbor *on, int command)
72 {
73 struct ospf6_interface *oi = on->ospf6_if;
74 struct interface *ifp = oi->interface;
75 struct bfd_info *bfd_info;
76 char src[64];
77
78 if (!oi->bfd_info || !on->bfd_info)
79 return;
80 bfd_info = (struct bfd_info *)oi->bfd_info;
81
82 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
83 inet_ntop(AF_INET6, &on->linklocal_addr, src, sizeof(src));
84 zlog_debug("%s nbr (%s) with BFD",
85 bfd_get_command_dbg_str(command), src);
86 }
87
88 bfd_peer_sendmsg(zclient, bfd_info, AF_INET6, &on->linklocal_addr,
89 on->ospf6_if->linklocal_addr, ifp->name, 0, 0, command,
90 0, VRF_DEFAULT);
91
92 if (command == ZEBRA_BFD_DEST_DEREGISTER)
93 bfd_info_free((struct bfd_info **)&on->bfd_info);
94 }
95
96 /*
97 * ospf6_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
98 * neighbor state is changed to/from 2way.
99 */
100 void ospf6_bfd_trigger_event(struct ospf6_neighbor *on, int old_state,
101 int state)
102 {
103 if ((old_state < OSPF6_NEIGHBOR_TWOWAY)
104 && (state >= OSPF6_NEIGHBOR_TWOWAY))
105 ospf6_bfd_reg_dereg_nbr(on, ZEBRA_BFD_DEST_REGISTER);
106 else if ((old_state >= OSPF6_NEIGHBOR_TWOWAY)
107 && (state < OSPF6_NEIGHBOR_TWOWAY))
108 ospf6_bfd_reg_dereg_nbr(on, ZEBRA_BFD_DEST_DEREGISTER);
109 }
110
111 /*
112 * ospf6_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
113 * with a interface with BFD through
114 * zebra for starting/stopping the monitoring of
115 * the neighbor rechahability.
116 */
117 static void ospf6_bfd_reg_dereg_all_nbr(struct ospf6_interface *oi, int command)
118 {
119 struct ospf6_neighbor *on;
120 struct listnode *node;
121
122 for (ALL_LIST_ELEMENTS_RO(oi->neighbor_list, node, on)) {
123 if (command == ZEBRA_BFD_DEST_REGISTER)
124 ospf6_bfd_info_nbr_create(oi, on);
125
126 if (on->state < OSPF6_NEIGHBOR_TWOWAY) {
127 if (command == ZEBRA_BFD_DEST_DEREGISTER)
128 bfd_info_free(
129 (struct bfd_info **)&on->bfd_info);
130 continue;
131 }
132
133 ospf6_bfd_reg_dereg_nbr(on, command);
134 }
135 }
136
137 /*
138 * ospf6_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
139 * to zebra
140 */
141 static int ospf6_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
142 {
143 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
144 struct listnode *node;
145 struct interface *ifp;
146 struct ospf6_interface *oi;
147 struct ospf6_neighbor *on;
148 char dst[64];
149
150 if (IS_OSPF6_DEBUG_ZEBRA(RECV))
151 zlog_debug("Zebra: BFD Dest replay request");
152
153 /* Send the client registration */
154 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
155
156 /* Replay the neighbor, if BFD is enabled on the interface*/
157 FOR_ALL_INTERFACES (vrf, ifp) {
158 oi = (struct ospf6_interface *)ifp->info;
159
160 if (!oi || !oi->bfd_info)
161 continue;
162
163 for (ALL_LIST_ELEMENTS_RO(oi->neighbor_list, node, on)) {
164 if (on->state < OSPF6_NEIGHBOR_TWOWAY)
165 continue;
166
167 if (IS_OSPF6_DEBUG_ZEBRA(SEND)) {
168 inet_ntop(AF_INET6, &on->linklocal_addr, dst,
169 sizeof(dst));
170 zlog_debug("Replaying nbr (%s) to BFD", dst);
171 }
172
173 ospf6_bfd_reg_dereg_nbr(on, ZEBRA_BFD_DEST_UPDATE);
174 }
175 }
176 return 0;
177 }
178
179 /*
180 * ospf6_bfd_interface_dest_update - Find the neighbor for which the BFD status
181 * has changed and bring down the neighbor
182 * connectivity if BFD down is received.
183 */
184 static int ospf6_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
185 {
186 struct interface *ifp;
187 struct ospf6_interface *oi;
188 struct ospf6_neighbor *on;
189 struct prefix dp;
190 struct prefix sp;
191 struct listnode *node, *nnode;
192 char dst[64];
193 int status;
194 int old_status;
195 struct bfd_info *bfd_info;
196 struct timeval tv;
197
198 ifp = bfd_get_peer_info(zclient->ibuf, &dp, &sp, &status, vrf_id);
199
200 if ((ifp == NULL) || (dp.family != AF_INET6))
201 return 0;
202
203 if (IS_OSPF6_DEBUG_ZEBRA(RECV)) {
204 char buf[PREFIX2STR_BUFFER];
205 prefix2str(&dp, buf, sizeof(buf));
206 zlog_debug("Zebra: interface %s bfd destination %s %s",
207 ifp->name, buf, bfd_get_status_str(status));
208 }
209
210
211 oi = (struct ospf6_interface *)ifp->info;
212 if (!oi || !oi->bfd_info)
213 return 0;
214
215 for (ALL_LIST_ELEMENTS(oi->neighbor_list, node, nnode, on)) {
216 if (memcmp(&(on->linklocal_addr), &dp.u.prefix6,
217 sizeof(struct in6_addr)))
218 continue;
219
220 if (IS_OSPF6_DEBUG_NEIGHBOR(EVENT)) {
221 inet_ntop(AF_INET6, &on->linklocal_addr, dst,
222 sizeof(dst));
223 zlog_debug("[%s:%s]: BFD %s", ifp->name, dst,
224 bfd_get_status_str(status));
225 }
226
227 if (!on->bfd_info)
228 continue;
229
230 bfd_info = (struct bfd_info *)on->bfd_info;
231 if (bfd_info->status == status)
232 continue;
233
234 old_status = bfd_info->status;
235 bfd_info->status = status;
236 monotime(&tv);
237 bfd_info->last_update = tv.tv_sec;
238
239 if ((status == BFD_STATUS_DOWN)
240 && (old_status == BFD_STATUS_UP)) {
241 THREAD_OFF(on->inactivity_timer);
242 thread_add_event(master, inactivity_timer, on, 0, NULL);
243 }
244 }
245
246 return 0;
247 }
248
249 /*
250 * ospf6_bfd_info_nbr_create - Create/update BFD information for a neighbor.
251 */
252 void ospf6_bfd_info_nbr_create(struct ospf6_interface *oi,
253 struct ospf6_neighbor *on)
254 {
255 struct bfd_info *oi_bfd_info;
256 struct bfd_info *on_bfd_info;
257
258 if (!oi->bfd_info)
259 return;
260
261 oi_bfd_info = (struct bfd_info *)oi->bfd_info;
262
263 if (!on->bfd_info)
264 on->bfd_info = bfd_info_create();
265
266 on_bfd_info = (struct bfd_info *)on->bfd_info;
267 on_bfd_info->detect_mult = oi_bfd_info->detect_mult;
268 on_bfd_info->desired_min_tx = oi_bfd_info->desired_min_tx;
269 on_bfd_info->required_min_rx = oi_bfd_info->required_min_rx;
270 }
271
272 /*
273 * ospf6_bfd_write_config - Write the interface BFD configuration.
274 */
275 void ospf6_bfd_write_config(struct vty *vty, struct ospf6_interface *oi)
276 {
277 #if HAVE_BFDD == 0
278 struct bfd_info *bfd_info;
279 #endif /* ! HAVE_BFDD */
280
281 if (!oi->bfd_info)
282 return;
283
284 #if HAVE_BFDD == 0
285 bfd_info = (struct bfd_info *)oi->bfd_info;
286
287 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
288 vty_out(vty, " ipv6 ospf6 bfd %d %d %d\n",
289 bfd_info->detect_mult, bfd_info->required_min_rx,
290 bfd_info->desired_min_tx);
291 else
292 #endif /* ! HAVE_BFDD */
293 vty_out(vty, " ipv6 ospf6 bfd\n");
294 }
295
296 /*
297 * ospf6_bfd_if_param_set - Set the configured BFD paramter values for
298 * interface.
299 */
300 static void ospf6_bfd_if_param_set(struct ospf6_interface *oi, uint32_t min_rx,
301 uint32_t min_tx, uint8_t detect_mult,
302 int defaults)
303 {
304 int command = 0;
305
306 bfd_set_param((struct bfd_info **)&(oi->bfd_info), min_rx, min_tx,
307 detect_mult, defaults, &command);
308 if (command)
309 ospf6_bfd_reg_dereg_all_nbr(oi, command);
310 }
311
312 DEFUN (ipv6_ospf6_bfd,
313 ipv6_ospf6_bfd_cmd,
314 "ipv6 ospf6 bfd",
315 IP6_STR
316 OSPF6_STR
317 "Enables BFD support\n"
318 )
319 {
320 VTY_DECLVAR_CONTEXT(interface, ifp);
321 struct ospf6_interface *oi;
322 assert(ifp);
323
324 oi = (struct ospf6_interface *)ifp->info;
325 if (oi == NULL)
326 oi = ospf6_interface_create(ifp);
327 assert(oi);
328
329 ospf6_bfd_if_param_set(oi, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX,
330 BFD_DEF_DETECT_MULT, 1);
331 return CMD_SUCCESS;
332 }
333
334 #if HAVE_BFDD > 0
335 DEFUN_HIDDEN(
336 #else
337 DEFUN(
338 #endif /* HAVE_BFDD */
339 ipv6_ospf6_bfd_param,
340 ipv6_ospf6_bfd_param_cmd,
341 "ipv6 ospf6 bfd (2-255) (50-60000) (50-60000)",
342 IP6_STR
343 OSPF6_STR
344 "Enables BFD support\n"
345 "Detect Multiplier\n"
346 "Required min receive interval\n"
347 "Desired min transmit interval\n")
348 {
349 VTY_DECLVAR_CONTEXT(interface, ifp);
350 int idx_number = 3;
351 int idx_number_2 = 4;
352 int idx_number_3 = 5;
353 struct ospf6_interface *oi;
354 uint32_t rx_val;
355 uint32_t tx_val;
356 uint8_t dm_val;
357 int ret;
358
359 assert(ifp);
360
361 oi = (struct ospf6_interface *)ifp->info;
362 if (oi == NULL)
363 oi = ospf6_interface_create(ifp);
364 assert(oi);
365
366 if ((ret = bfd_validate_param(
367 vty, argv[idx_number]->arg, argv[idx_number_2]->arg,
368 argv[idx_number_3]->arg, &dm_val, &rx_val, &tx_val))
369 != CMD_SUCCESS)
370 return ret;
371
372 ospf6_bfd_if_param_set(oi, rx_val, tx_val, dm_val, 0);
373
374 return CMD_SUCCESS;
375 }
376
377 DEFUN (no_ipv6_ospf6_bfd,
378 no_ipv6_ospf6_bfd_cmd,
379 "no ipv6 ospf6 bfd",
380 NO_STR
381 IP6_STR
382 OSPF6_STR
383 "Disables BFD support\n"
384 )
385 {
386 VTY_DECLVAR_CONTEXT(interface, ifp);
387 struct ospf6_interface *oi;
388 assert(ifp);
389
390 oi = (struct ospf6_interface *)ifp->info;
391 if (oi == NULL)
392 oi = ospf6_interface_create(ifp);
393 assert(oi);
394
395 if (oi->bfd_info) {
396 ospf6_bfd_reg_dereg_all_nbr(oi, ZEBRA_BFD_DEST_DEREGISTER);
397 bfd_info_free((struct bfd_info **)&(oi->bfd_info));
398 }
399
400 return CMD_SUCCESS;
401 }
402
403 void ospf6_bfd_init(void)
404 {
405 bfd_gbl_init();
406
407 /* Initialize BFD client functions */
408 zclient->interface_bfd_dest_update = ospf6_bfd_interface_dest_update;
409 zclient->bfd_dest_replay = ospf6_bfd_nbr_replay;
410
411 /* Install BFD command */
412 install_element(INTERFACE_NODE, &ipv6_ospf6_bfd_cmd);
413 install_element(INTERFACE_NODE, &ipv6_ospf6_bfd_param_cmd);
414 install_element(INTERFACE_NODE, &no_ipv6_ospf6_bfd_cmd);
415 }