]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[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 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 "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
47 extern struct zclient *zclient;
48
49 /*
50 * ospf_bfd_info_free - Free BFD info structure
51 */
52 void ospf_bfd_info_free(void **bfd_info)
53 {
54 bfd_info_free((struct bfd_info **)bfd_info);
55 }
56
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 */
62 static void ospf_bfd_reg_dereg_nbr(struct ospf_neighbor *nbr, int command)
63 {
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))
78 zlog_debug("%s nbr (%s) with BFD. OSPF vrf %s",
79 bfd_get_command_dbg_str(command),
80 inet_ntoa(nbr->src),
81 ospf_vrf_id_to_name(oi->ospf->vrf_id));
82
83 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->src, NULL, ifp->name,
84 0, 0, command, 0, oi->ospf->vrf_id);
85 }
86
87 /*
88 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
89 * neighbor state is changed to/from 2way.
90 */
91 void ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
92 {
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);
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 */
105 static int ospf_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
106 {
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;
138 }
139
140 /*
141 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
142 * to zebra
143 */
144 static int ospf_bfd_nbr_replay(int command, struct zclient *zclient,
145 zebra_size_t length, vrf_id_t vrf_id)
146 {
147 struct listnode *inode, *node, *onode;
148 struct ospf *ospf;
149 struct ospf_interface *oi;
150 struct route_table *nbrs;
151 struct route_node *rn;
152 struct ospf_neighbor *nbr;
153 struct ospf_if_params *params;
154
155 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
156 zlog_debug("Zebra: BFD Dest replay request");
157 }
158
159 /* Send the client registration */
160 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
161
162 /* Replay the neighbor, if BFD is enabled in OSPF */
163 for (ALL_LIST_ELEMENTS(om->ospf, node, onode, ospf)) {
164 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, inode, oi)) {
165 if ((nbrs = oi->nbrs) == NULL)
166 continue;
167
168 params = IF_DEF_PARAMS(oi->ifp);
169 if (!params->bfd_info)
170 continue;
171
172 for (rn = route_top(nbrs); rn; rn = route_next(rn)) {
173 if ((nbr = rn->info) == NULL
174 || nbr == oi->nbr_self)
175 continue;
176
177 if (nbr->state < NSM_TwoWay)
178 continue;
179
180 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
181 zlog_debug("Replaying nbr (%s) to BFD",
182 inet_ntoa(nbr->src));
183
184 ospf_bfd_reg_dereg_nbr(nbr,
185 ZEBRA_BFD_DEST_UPDATE);
186 }
187 }
188 }
189 return 0;
190 }
191
192 /*
193 * ospf_bfd_interface_dest_update - Find the neighbor for which the BFD status
194 * has changed and bring down the neighbor
195 * connectivity if the BFD status changed to
196 * down.
197 */
198 static int ospf_bfd_interface_dest_update(int command, struct zclient *zclient,
199 zebra_size_t length, vrf_id_t vrf_id)
200 {
201 struct interface *ifp;
202 struct ospf_interface *oi;
203 struct ospf_if_params *params;
204 struct ospf_neighbor *nbr;
205 struct route_node *node;
206 struct prefix p;
207 int status;
208 int old_status;
209 struct bfd_info *bfd_info;
210 struct timeval tv;
211
212 ifp = bfd_get_peer_info(zclient->ibuf, &p, NULL, &status, vrf_id);
213
214 if ((ifp == NULL) || (p.family != AF_INET))
215 return 0;
216
217 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE)) {
218 char buf[PREFIX2STR_BUFFER];
219 prefix2str(&p, buf, sizeof(buf));
220 zlog_debug("Zebra: interface %s bfd destination %s %s",
221 ifp->name, buf, bfd_get_status_str(status));
222 }
223
224 params = IF_DEF_PARAMS(ifp);
225 if (!params->bfd_info)
226 return 0;
227
228 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
229 if ((oi = node->info) == NULL)
230 continue;
231
232 nbr = ospf_nbr_lookup_by_addr(oi->nbrs, &p.u.prefix4);
233 if (!nbr || !nbr->bfd_info)
234 continue;
235
236 bfd_info = (struct bfd_info *)nbr->bfd_info;
237 if (bfd_info->status == status)
238 continue;
239
240 old_status = bfd_info->status;
241 bfd_info->status = status;
242 monotime(&tv);
243 bfd_info->last_update = tv.tv_sec;
244
245 if ((status == BFD_STATUS_DOWN)
246 && (old_status == BFD_STATUS_UP)) {
247 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
248 zlog_debug("NSM[%s:%s]: BFD Down",
249 IF_NAME(nbr->oi),
250 inet_ntoa(nbr->address.u.prefix4));
251
252 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_InactivityTimer);
253 }
254 }
255
256 return 0;
257 }
258
259 /*
260 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
261 */
262 void ospf_bfd_info_nbr_create(struct ospf_interface *oi,
263 struct ospf_neighbor *nbr)
264 {
265 struct bfd_info *oi_bfd_info;
266 struct bfd_info *nbr_bfd_info;
267 struct interface *ifp = oi->ifp;
268 struct ospf_if_params *params;
269
270 /* Check if BFD is enabled */
271 params = IF_DEF_PARAMS(ifp);
272
273 /* Check if BFD is enabled */
274 if (!params->bfd_info)
275 return;
276
277 oi_bfd_info = (struct bfd_info *)params->bfd_info;
278 if (!nbr->bfd_info)
279 nbr->bfd_info = bfd_info_create();
280
281 nbr_bfd_info = (struct bfd_info *)nbr->bfd_info;
282 nbr_bfd_info->detect_mult = oi_bfd_info->detect_mult;
283 nbr_bfd_info->desired_min_tx = oi_bfd_info->desired_min_tx;
284 nbr_bfd_info->required_min_rx = oi_bfd_info->required_min_rx;
285 }
286
287 /*
288 * ospf_bfd_write_config - Write the interface BFD configuration.
289 */
290 void ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
291
292 {
293 #if HAVE_BFDD == 0
294 struct bfd_info *bfd_info;
295 #endif /* ! HAVE_BFDD */
296
297 if (!params->bfd_info)
298 return;
299
300 #if HAVE_BFDD == 0
301 bfd_info = (struct bfd_info *)params->bfd_info;
302
303 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
304 vty_out(vty, " ip ospf bfd %d %d %d\n", bfd_info->detect_mult,
305 bfd_info->required_min_rx, bfd_info->desired_min_tx);
306 else
307 #endif /* ! HAVE_BFDD */
308 vty_out(vty, " ip ospf bfd\n");
309 }
310
311 /*
312 * ospf_bfd_show_info - Show BFD info structure
313 */
314 void ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
315 bool use_json, int param_only)
316 {
317 if (param_only)
318 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json,
319 json_obj);
320 else
321 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json,
322 json_obj);
323 }
324
325 /*
326 * ospf_bfd_interface_show - Show the interface BFD configuration.
327 */
328 void ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
329 json_object *json_interface_sub, bool use_json)
330 {
331 struct ospf_if_params *params;
332
333 params = IF_DEF_PARAMS(ifp);
334
335 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json,
336 1);
337 }
338
339 /*
340 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
341 * interface.
342 */
343 static void ospf_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
344 uint32_t min_tx, uint8_t detect_mult,
345 int defaults)
346 {
347 struct ospf_if_params *params;
348 int command = 0;
349
350 params = IF_DEF_PARAMS(ifp);
351
352 bfd_set_param((struct bfd_info **)&(params->bfd_info), min_rx, min_tx,
353 detect_mult, defaults, &command);
354 if (command)
355 ospf_bfd_reg_dereg_all_nbr(ifp, command);
356 }
357
358 DEFUN (ip_ospf_bfd,
359 ip_ospf_bfd_cmd,
360 "ip ospf bfd",
361 "IP Information\n"
362 "OSPF interface commands\n"
363 "Enables BFD support\n")
364 {
365 VTY_DECLVAR_CONTEXT(interface, ifp);
366 struct ospf_if_params *params;
367 struct bfd_info *bfd_info;
368
369 assert(ifp);
370 params = IF_DEF_PARAMS(ifp);
371 bfd_info = params->bfd_info;
372
373 if (!bfd_info || !CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
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
380 #if HAVE_BFDD > 0
381 DEFUN_HIDDEN(
382 #else
383 DEFUN(
384 #endif /* HAVE_BFDD */
385 ip_ospf_bfd_param,
386 ip_ospf_bfd_param_cmd,
387 "ip ospf bfd (2-255) (50-60000) (50-60000)",
388 "IP Information\n"
389 "OSPF interface commands\n"
390 "Enables BFD support\n"
391 "Detect Multiplier\n"
392 "Required min receive interval\n"
393 "Desired min transmit interval\n")
394 {
395 VTY_DECLVAR_CONTEXT(interface, ifp);
396 int idx_number = 3;
397 int idx_number_2 = 4;
398 int idx_number_3 = 5;
399 uint32_t rx_val;
400 uint32_t tx_val;
401 uint8_t dm_val;
402 int ret;
403
404 assert(ifp);
405
406 if ((ret = bfd_validate_param(
407 vty, argv[idx_number]->arg, argv[idx_number_2]->arg,
408 argv[idx_number_3]->arg, &dm_val, &rx_val, &tx_val))
409 != CMD_SUCCESS)
410 return ret;
411
412 ospf_bfd_if_param_set(ifp, rx_val, tx_val, dm_val, 0);
413
414 return CMD_SUCCESS;
415 }
416
417 DEFUN (no_ip_ospf_bfd,
418 no_ip_ospf_bfd_cmd,
419 #if HAVE_BFDD > 0
420 "no ip ospf bfd",
421 #else
422 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
423 #endif /* HAVE_BFDD */
424 NO_STR
425 "IP Information\n"
426 "OSPF interface commands\n"
427 "Disables BFD support\n"
428 #if HAVE_BFDD == 0
429 "Detect Multiplier\n"
430 "Required min receive interval\n"
431 "Desired min transmit interval\n"
432 #endif /* !HAVE_BFDD */
433 )
434 {
435 VTY_DECLVAR_CONTEXT(interface, ifp);
436 struct ospf_if_params *params;
437
438 assert(ifp);
439
440 params = IF_DEF_PARAMS(ifp);
441 if (params->bfd_info) {
442 ospf_bfd_reg_dereg_all_nbr(ifp, ZEBRA_BFD_DEST_DEREGISTER);
443 bfd_info_free(&(params->bfd_info));
444 }
445
446 return CMD_SUCCESS;
447 }
448
449 void ospf_bfd_init(void)
450 {
451 bfd_gbl_init();
452
453 /* Initialize BFD client functions */
454 zclient->interface_bfd_dest_update = ospf_bfd_interface_dest_update;
455 zclient->bfd_dest_replay = ospf_bfd_nbr_replay;
456
457 /* Install BFD command */
458 install_element(INTERFACE_NODE, &ip_ospf_bfd_cmd);
459 install_element(INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
460 install_element(INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
461 }