]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_bfd.c
Merge pull request #8309 from opensourcerouting/init-config-read
[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 "json.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 DEFINE_MTYPE_STATIC(OSPFD, BFD_CONFIG, "BFD configuration data");
49
50 /*
51 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
52 * neighbor state is changed to/from 2way.
53 */
54 void ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
55 {
56 if ((old_state < NSM_TwoWay) && (state >= NSM_TwoWay))
57 bfd_sess_install(nbr->bfd_session);
58 else if ((old_state >= NSM_TwoWay) && (state < NSM_TwoWay))
59 bfd_sess_uninstall(nbr->bfd_session);
60 }
61
62 static void ospf_bfd_session_change(struct bfd_session_params *bsp,
63 const struct bfd_session_status *bss,
64 void *arg)
65 {
66 struct ospf_neighbor *nbr = arg;
67
68 /* BFD peer went down. */
69 if (bss->state == BFD_STATUS_DOWN
70 && bss->previous_state == BFD_STATUS_UP) {
71 if (IS_DEBUG_OSPF(bfd, BFD_LIB))
72 zlog_debug("%s: NSM[%s:%pI4]: BFD Down", __func__,
73 IF_NAME(nbr->oi), &nbr->address.u.prefix4);
74
75 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_InactivityTimer);
76 }
77
78 /* BFD peer went up. */
79 if (bss->state == BSS_UP && bss->previous_state == BSS_DOWN)
80 if (IS_DEBUG_OSPF(bfd, BFD_LIB))
81 zlog_debug("%s: NSM[%s:%pI4]: BFD Up", __func__,
82 IF_NAME(nbr->oi), &nbr->address.u.prefix4);
83 }
84
85 void ospf_neighbor_bfd_apply(struct ospf_neighbor *nbr)
86 {
87 struct ospf_interface *oi = nbr->oi;
88 struct ospf_if_params *oip = IF_DEF_PARAMS(oi->ifp);
89
90 /* BFD configuration was removed. */
91 if (oip->bfd_config == NULL) {
92 bfd_sess_free(&nbr->bfd_session);
93 return;
94 }
95
96 /* New BFD session. */
97 if (nbr->bfd_session == NULL) {
98 nbr->bfd_session = bfd_sess_new(ospf_bfd_session_change, nbr);
99 bfd_sess_set_ipv4_addrs(nbr->bfd_session, NULL, &nbr->src);
100 bfd_sess_set_interface(nbr->bfd_session, oi->ifp->name);
101 bfd_sess_set_vrf(nbr->bfd_session, oi->ospf->vrf_id);
102 bfd_sess_enable(nbr->bfd_session, true);
103 }
104
105 /* Set new configuration. */
106 bfd_sess_set_timers(nbr->bfd_session,
107 oip->bfd_config->detection_multiplier,
108 oip->bfd_config->min_rx, oip->bfd_config->min_tx);
109 bfd_sess_set_profile(nbr->bfd_session, oip->bfd_config->profile);
110
111 /* Don't start sessions on down OSPF sessions. */
112 if (nbr->state < NSM_TwoWay)
113 return;
114
115 bfd_sess_install(nbr->bfd_session);
116 }
117
118 static void ospf_interface_bfd_apply(struct interface *ifp)
119 {
120 struct ospf_interface *oi;
121 struct route_table *nbrs;
122 struct ospf_neighbor *nbr;
123 struct route_node *irn;
124 struct route_node *nrn;
125
126 /* Iterate over all interfaces and set neighbors BFD session. */
127 for (irn = route_top(IF_OIFS(ifp)); irn; irn = route_next(irn)) {
128 if ((oi = irn->info) == NULL)
129 continue;
130 if ((nbrs = oi->nbrs) == NULL)
131 continue;
132 for (nrn = route_top(nbrs); nrn; nrn = route_next(nrn)) {
133 if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
134 continue;
135
136 ospf_neighbor_bfd_apply(nbr);
137 }
138 }
139 }
140
141 static void ospf_interface_enable_bfd(struct interface *ifp)
142 {
143 struct ospf_if_params *oip = IF_DEF_PARAMS(ifp);
144
145 if (oip->bfd_config)
146 return;
147
148 /* Allocate memory for configurations and set defaults. */
149 oip->bfd_config = XCALLOC(MTYPE_BFD_CONFIG, sizeof(*oip->bfd_config));
150 oip->bfd_config->detection_multiplier = BFD_DEF_DETECT_MULT;
151 oip->bfd_config->min_rx = BFD_DEF_MIN_RX;
152 oip->bfd_config->min_tx = BFD_DEF_MIN_TX;
153 }
154
155 void ospf_interface_disable_bfd(struct interface *ifp,
156 struct ospf_if_params *oip)
157 {
158 XFREE(MTYPE_BFD_CONFIG, oip->bfd_config);
159 ospf_interface_bfd_apply(ifp);
160 }
161
162 /*
163 * ospf_bfd_write_config - Write the interface BFD configuration.
164 */
165 void ospf_bfd_write_config(struct vty *vty, const struct ospf_if_params *params
166 __attribute__((unused)))
167 {
168 #if HAVE_BFDD == 0
169 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
170 vty_out(vty, " ip ospf bfd %d %d %d\n",
171 params->bfd_config->detection_multiplier,
172 params->bfd_config->min_rx, params->bfd_config->min_tx);
173 else
174 #endif /* ! HAVE_BFDD */
175 vty_out(vty, " ip ospf bfd\n");
176
177 if (params->bfd_config->profile[0])
178 vty_out(vty, " ip ospf bfd profile %s\n",
179 params->bfd_config->profile);
180 }
181
182 void ospf_interface_bfd_show(struct vty *vty, const struct interface *ifp,
183 struct json_object *json)
184 {
185 struct ospf_if_params *params = IF_DEF_PARAMS(ifp);
186 struct bfd_configuration *bfd_config = params->bfd_config;
187 struct json_object *json_bfd;
188
189 if (bfd_config == NULL)
190 return;
191
192 if (json) {
193 json_bfd = json_object_new_object();
194 json_object_int_add(json_bfd, "detectionMultiplier",
195 bfd_config->detection_multiplier);
196 json_object_int_add(json_bfd, "rxMinInterval",
197 bfd_config->min_rx);
198 json_object_int_add(json_bfd, "txMinInterval",
199 bfd_config->min_tx);
200 json_object_object_add(json, "peerBfdInfo", json_bfd);
201 } else
202 vty_out(vty,
203 " BFD: Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
204 bfd_config->detection_multiplier, bfd_config->min_rx,
205 bfd_config->min_tx);
206 }
207
208 DEFUN (ip_ospf_bfd,
209 ip_ospf_bfd_cmd,
210 "ip ospf bfd",
211 "IP Information\n"
212 "OSPF interface commands\n"
213 "Enables BFD support\n")
214 {
215 VTY_DECLVAR_CONTEXT(interface, ifp);
216 ospf_interface_enable_bfd(ifp);
217 ospf_interface_bfd_apply(ifp);
218 return CMD_SUCCESS;
219 }
220
221 #if HAVE_BFDD > 0
222 DEFUN_HIDDEN(
223 #else
224 DEFUN(
225 #endif /* HAVE_BFDD */
226 ip_ospf_bfd_param,
227 ip_ospf_bfd_param_cmd,
228 "ip ospf bfd (2-255) (50-60000) (50-60000)",
229 "IP Information\n"
230 "OSPF interface commands\n"
231 "Enables BFD support\n"
232 "Detect Multiplier\n"
233 "Required min receive interval\n"
234 "Desired min transmit interval\n")
235 {
236 VTY_DECLVAR_CONTEXT(interface, ifp);
237 struct ospf_if_params *params;
238 int idx_number = 3;
239 int idx_number_2 = 4;
240 int idx_number_3 = 5;
241
242 ospf_interface_enable_bfd(ifp);
243
244 params = IF_DEF_PARAMS(ifp);
245 params->bfd_config->detection_multiplier =
246 strtol(argv[idx_number]->arg, NULL, 10);
247 params->bfd_config->min_rx = strtol(argv[idx_number_2]->arg, NULL, 10);
248 params->bfd_config->min_tx = strtol(argv[idx_number_3]->arg, NULL, 10);
249
250 ospf_interface_bfd_apply(ifp);
251
252 return CMD_SUCCESS;
253 }
254
255 DEFUN (ip_ospf_bfd_prof,
256 ip_ospf_bfd_prof_cmd,
257 "ip ospf bfd profile BFDPROF",
258 "IP Information\n"
259 "OSPF interface commands\n"
260 "Enables BFD support\n"
261 BFD_PROFILE_STR
262 BFD_PROFILE_NAME_STR)
263 {
264 VTY_DECLVAR_CONTEXT(interface, ifp);
265 struct ospf_if_params *params;
266 int idx_prof = 4;
267
268 ospf_interface_enable_bfd(ifp);
269 params = IF_DEF_PARAMS(ifp);
270 strlcpy(params->bfd_config->profile, argv[idx_prof]->arg,
271 sizeof(params->bfd_config->profile));
272 ospf_interface_bfd_apply(ifp);
273
274 return CMD_SUCCESS;
275 }
276
277 DEFUN (no_ip_ospf_bfd_prof,
278 no_ip_ospf_bfd_prof_cmd,
279 "no ip ospf bfd profile [BFDPROF]",
280 NO_STR
281 "IP Information\n"
282 "OSPF interface commands\n"
283 "Enables BFD support\n"
284 BFD_PROFILE_STR
285 BFD_PROFILE_NAME_STR)
286 {
287 VTY_DECLVAR_CONTEXT(interface, ifp);
288 struct ospf_if_params *params;
289
290 ospf_interface_enable_bfd(ifp);
291 params = IF_DEF_PARAMS(ifp);
292 params->bfd_config->profile[0] = 0;
293 ospf_interface_bfd_apply(ifp);
294
295 return CMD_SUCCESS;
296 }
297
298 DEFUN (no_ip_ospf_bfd,
299 no_ip_ospf_bfd_cmd,
300 #if HAVE_BFDD > 0
301 "no ip ospf bfd",
302 #else
303 "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
304 #endif /* HAVE_BFDD */
305 NO_STR
306 "IP Information\n"
307 "OSPF interface commands\n"
308 "Disables BFD support\n"
309 #if HAVE_BFDD == 0
310 "Detect Multiplier\n"
311 "Required min receive interval\n"
312 "Desired min transmit interval\n"
313 #endif /* !HAVE_BFDD */
314 )
315 {
316 VTY_DECLVAR_CONTEXT(interface, ifp);
317 ospf_interface_disable_bfd(ifp, IF_DEF_PARAMS(ifp));
318 return CMD_SUCCESS;
319 }
320
321 void ospf_bfd_init(struct thread_master *tm)
322 {
323 bfd_protocol_integration_init(zclient, tm);
324
325 /* Install BFD command */
326 install_element(INTERFACE_NODE, &ip_ospf_bfd_cmd);
327 install_element(INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
328 install_element(INTERFACE_NODE, &ip_ospf_bfd_prof_cmd);
329 install_element(INTERFACE_NODE, &no_ip_ospf_bfd_prof_cmd);
330 install_element(INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
331 }