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