]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_bfd.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[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 *
896014f4
DL
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
7f342629
DS
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
47extern struct zclient *zclient;
48
68fe91d6 49/*
50 * ospf_bfd_info_free - Free BFD info structure
51 */
d62a17ae 52void ospf_bfd_info_free(void **bfd_info)
68fe91d6 53{
d62a17ae 54 bfd_info_free((struct bfd_info **)bfd_info);
68fe91d6 55}
56
7f342629
DS
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 */
d62a17ae 62static void ospf_bfd_reg_dereg_nbr(struct ospf_neighbor *nbr, int command)
7f342629 63{
d62a17ae 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))
b5a8894d 78 zlog_debug("%s nbr (%s) with BFD. OSPF vrf %s",
d62a17ae 79 bfd_get_command_dbg_str(command),
b5a8894d
CS
80 inet_ntoa(nbr->src),
81 ospf_vrf_id_to_name(oi->ospf->vrf_id));
d62a17ae 82
83 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->src, NULL, ifp->name,
b5a8894d 84 0, 0, command, 0, oi->ospf->vrf_id);
7f342629
DS
85}
86
87/*
88 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
89 * neighbor state is changed to/from 2way.
90 */
d62a17ae 91void ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
7f342629 92{
d62a17ae 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);
7f342629
DS
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 */
d62a17ae 105static int ospf_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
7f342629 106{
d62a17ae 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;
7f342629
DS
138}
139
140/*
141 * ospf_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
142 * to zebra
143 */
d62a17ae 144static int ospf_bfd_nbr_replay(int command, struct zclient *zclient,
145 zebra_size_t length, vrf_id_t vrf_id)
7f342629 146{
d62a17ae 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
b5a8894d 162 /* Replay the neighbor, if BFD is enabled in OSPF */
d62a17ae 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;
7f342629
DS
190}
191
192/*
68fe91d6 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.
7f342629 197 */
d62a17ae 198static int ospf_bfd_interface_dest_update(int command, struct zclient *zclient,
199 zebra_size_t length, vrf_id_t vrf_id)
7f342629 200{
d62a17ae 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;
7f342629
DS
257}
258
68fe91d6 259/*
260 * ospf_bfd_info_nbr_create - Create/update BFD information for a neighbor.
261 */
d62a17ae 262void ospf_bfd_info_nbr_create(struct ospf_interface *oi,
263 struct ospf_neighbor *nbr)
68fe91d6 264{
d62a17ae 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;
68fe91d6 285}
286
7f342629
DS
287/*
288 * ospf_bfd_write_config - Write the interface BFD configuration.
289 */
d62a17ae 290void ospf_bfd_write_config(struct vty *vty, struct ospf_if_params *params)
7f342629
DS
291
292{
a0841732 293#if HAVE_BFDD == 0
d62a17ae 294 struct bfd_info *bfd_info;
a0841732 295#endif /* ! HAVE_BFDD */
7f342629 296
d62a17ae 297 if (!params->bfd_info)
298 return;
7f342629 299
a0841732 300#if HAVE_BFDD == 0
d62a17ae 301 bfd_info = (struct bfd_info *)params->bfd_info;
7f342629 302
d62a17ae 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
a0841732 307#endif /* ! HAVE_BFDD */
d62a17ae 308 vty_out(vty, " ip ospf bfd\n");
7f342629
DS
309}
310
68fe91d6 311/*
312 * ospf_bfd_show_info - Show BFD info structure
313 */
d62a17ae 314void ospf_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
9f049418 315 bool use_json, int param_only)
68fe91d6 316{
d62a17ae 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);
68fe91d6 323}
324
325/*
326 * ospf_bfd_interface_show - Show the interface BFD configuration.
327 */
d62a17ae 328void ospf_bfd_interface_show(struct vty *vty, struct interface *ifp,
9f049418 329 json_object *json_interface_sub, bool use_json)
68fe91d6 330{
d62a17ae 331 struct ospf_if_params *params;
68fe91d6 332
d62a17ae 333 params = IF_DEF_PARAMS(ifp);
68fe91d6 334
d62a17ae 335 ospf_bfd_show_info(vty, params->bfd_info, json_interface_sub, use_json,
336 1);
68fe91d6 337}
7f342629
DS
338
339/*
340 * ospf_bfd_if_param_set - Set the configured BFD paramter values for
341 * interface.
342 */
d7c0a89a
QY
343static void ospf_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
344 uint32_t min_tx, uint8_t detect_mult,
d62a17ae 345 int defaults)
7f342629 346{
d62a17ae 347 struct ospf_if_params *params;
348 int command = 0;
7f342629 349
d62a17ae 350 params = IF_DEF_PARAMS(ifp);
7f342629 351
d62a17ae 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);
7f342629
DS
356}
357
7f342629
DS
358DEFUN (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{
d62a17ae 365 VTY_DECLVAR_CONTEXT(interface, ifp);
366 struct ospf_if_params *params;
367 struct bfd_info *bfd_info;
7f342629 368
d62a17ae 369 assert(ifp);
370 params = IF_DEF_PARAMS(ifp);
371 bfd_info = params->bfd_info;
487f2302 372
d62a17ae 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);
7f342629 376
d62a17ae 377 return CMD_SUCCESS;
7f342629
DS
378}
379
64dc4b2d
RZ
380#if HAVE_BFDD > 0
381DEFUN_HIDDEN(
382#else
383DEFUN(
384#endif /* HAVE_BFDD */
385 ip_ospf_bfd_param,
7f342629 386 ip_ospf_bfd_param_cmd,
9ccf14f7 387 "ip ospf bfd (2-255) (50-60000) (50-60000)",
7f342629
DS
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{
d62a17ae 395 VTY_DECLVAR_CONTEXT(interface, ifp);
396 int idx_number = 3;
397 int idx_number_2 = 4;
398 int idx_number_3 = 5;
d7c0a89a
QY
399 uint32_t rx_val;
400 uint32_t tx_val;
401 uint8_t dm_val;
d62a17ae 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;
7f342629
DS
415}
416
417DEFUN (no_ip_ospf_bfd,
418 no_ip_ospf_bfd_cmd,
64dc4b2d
RZ
419#if HAVE_BFDD > 0
420 "no ip ospf bfd",
421#else
692b4c65 422 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
64dc4b2d 423#endif /* HAVE_BFDD */
7f342629
DS
424 NO_STR
425 "IP Information\n"
426 "OSPF interface commands\n"
692b4c65 427 "Disables BFD support\n"
64dc4b2d 428#if HAVE_BFDD == 0
692b4c65
QY
429 "Detect Multiplier\n"
430 "Required min receive interval\n"
64dc4b2d
RZ
431 "Desired min transmit interval\n"
432#endif /* !HAVE_BFDD */
433)
7f342629 434{
d62a17ae 435 VTY_DECLVAR_CONTEXT(interface, ifp);
436 struct ospf_if_params *params;
7f342629 437
d62a17ae 438 assert(ifp);
7f342629 439
d62a17ae 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 }
7f342629 445
d62a17ae 446 return CMD_SUCCESS;
7f342629
DS
447}
448
d62a17ae 449void ospf_bfd_init(void)
7f342629 450{
d62a17ae 451 bfd_gbl_init();
567b877d 452
d62a17ae 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;
7f342629 456
d62a17ae 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);
7f342629 461}