summaryrefslogtreecommitdiffstats
path: root/ospfd/ospf_bfd.c
blob: 7d4c7c06b81e40f2c252426cd261a9c8a3d2ef41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * ospf_bfd.c: OSPF BFD handling routines
 *
 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
 */

#include <zebra.h>

#include "command.h"
#include "json.h"
#include "linklist.h"
#include "memory.h"
#include "prefix.h"
#include "frrevent.h"
#include "buffer.h"
#include "stream.h"
#include "zclient.h"
#include "vty.h"
#include "table.h"
#include "bfd.h"
#include "ospfd.h"
#include "ospf_asbr.h"
#include "ospf_lsa.h"
#include "ospf_lsdb.h"
#include "ospf_neighbor.h"
#include "ospf_interface.h"
#include "ospf_nsm.h"
#include "ospf_bfd.h"
#include "ospf_dump.h"
#include "ospf_vty.h"

DEFINE_MTYPE_STATIC(OSPFD, BFD_CONFIG, "BFD configuration data");

/*
 * ospf_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
 *                          neighbor state is changed to/from 2way.
 */
void ospf_bfd_trigger_event(struct ospf_neighbor *nbr, int old_state, int state)
{
	if ((old_state < NSM_TwoWay) && (state >= NSM_TwoWay))
		bfd_sess_install(nbr->bfd_session);
	else if ((old_state >= NSM_TwoWay) && (state < NSM_TwoWay))
		bfd_sess_uninstall(nbr->bfd_session);
}

static void ospf_bfd_session_change(struct bfd_session_params *bsp,
				    const struct bfd_session_status *bss,
				    void *arg)
{
	struct ospf_neighbor *nbr = arg;

	/* BFD peer went down. */
	if (bss->state == BFD_STATUS_DOWN
	    && bss->previous_state == BFD_STATUS_UP) {
		if (IS_DEBUG_OSPF(bfd, BFD_LIB))
			zlog_debug("%s: NSM[%s:%pI4]: BFD Down", __func__,
				   IF_NAME(nbr->oi), &nbr->address.u.prefix4);

		OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_InactivityTimer);
	}

	/* BFD peer went up. */
	if (bss->state == BSS_UP && bss->previous_state == BSS_DOWN)
		if (IS_DEBUG_OSPF(bfd, BFD_LIB))
			zlog_debug("%s: NSM[%s:%pI4]: BFD Up", __func__,
				   IF_NAME(nbr->oi), &nbr->address.u.prefix4);
}

void ospf_neighbor_bfd_apply(struct ospf_neighbor *nbr)
{
	struct ospf_interface *oi = nbr->oi;
	struct ospf_if_params *oip = IF_DEF_PARAMS(oi->ifp);

	/* BFD configuration was removed. */
	if (oip->bfd_config == NULL) {
		bfd_sess_free(&nbr->bfd_session);
		return;
	}

	/* New BFD session. */
	if (nbr->bfd_session == NULL) {
		nbr->bfd_session = bfd_sess_new(ospf_bfd_session_change, nbr);
		bfd_sess_set_ipv4_addrs(nbr->bfd_session, NULL, &nbr->src);
		bfd_sess_set_interface(nbr->bfd_session, oi->ifp->name);
		bfd_sess_set_vrf(nbr->bfd_session, oi->ospf->vrf_id);
	}

	/* Set new configuration. */
	bfd_sess_set_timers(nbr->bfd_session,
			    oip->bfd_config->detection_multiplier,
			    oip->bfd_config->min_rx, oip->bfd_config->min_tx);
	bfd_sess_set_profile(nbr->bfd_session, oip->bfd_config->profile);

	/* Don't start sessions on down OSPF sessions. */
	if (nbr->state < NSM_TwoWay)
		return;

	bfd_sess_install(nbr->bfd_session);
}

static void ospf_interface_bfd_apply(struct interface *ifp)
{
	struct ospf_interface *oi;
	struct route_table *nbrs;
	struct ospf_neighbor *nbr;
	struct route_node *irn;
	struct route_node *nrn;

	/* Iterate over all interfaces and set neighbors BFD session. */
	for (irn = route_top(IF_OIFS(ifp)); irn; irn = route_next(irn)) {
		if ((oi = irn->info) == NULL)
			continue;
		if ((nbrs = oi->nbrs) == NULL)
			continue;
		for (nrn = route_top(nbrs); nrn; nrn = route_next(nrn)) {
			if ((nbr = nrn->info) == NULL || nbr == oi->nbr_self)
				continue;

			ospf_neighbor_bfd_apply(nbr);
		}
	}
}

static void ospf_interface_enable_bfd(struct interface *ifp)
{
	struct ospf_if_params *oip = IF_DEF_PARAMS(ifp);

	if (oip->bfd_config)
		return;

	/* Allocate memory for configurations and set defaults. */
	oip->bfd_config = XCALLOC(MTYPE_BFD_CONFIG, sizeof(*oip->bfd_config));
	oip->bfd_config->detection_multiplier = BFD_DEF_DETECT_MULT;
	oip->bfd_config->min_rx = BFD_DEF_MIN_RX;
	oip->bfd_config->min_tx = BFD_DEF_MIN_TX;
}

void ospf_interface_disable_bfd(struct interface *ifp,
				struct ospf_if_params *oip)
{
	XFREE(MTYPE_BFD_CONFIG, oip->bfd_config);
	ospf_interface_bfd_apply(ifp);
}

/*
 * ospf_bfd_write_config - Write the interface BFD configuration.
 */
void ospf_bfd_write_config(struct vty *vty, const struct ospf_if_params *params
			   __attribute__((unused)))
{
#if HAVE_BFDD == 0
	if (params->bfd_config->detection_multiplier != BFD_DEF_DETECT_MULT
	    || params->bfd_config->min_rx != BFD_DEF_MIN_RX
	    || params->bfd_config->min_tx != BFD_DEF_MIN_TX)
		vty_out(vty, " ip ospf bfd %d %d %d\n",
			params->bfd_config->detection_multiplier,
			params->bfd_config->min_rx, params->bfd_config->min_tx);
	else
#endif /* ! HAVE_BFDD */
		vty_out(vty, " ip ospf bfd\n");

	if (params->bfd_config->profile[0])
		vty_out(vty, " ip ospf bfd profile %s\n",
			params->bfd_config->profile);
}

void ospf_interface_bfd_show(struct vty *vty, const struct interface *ifp,
			     struct json_object *json)
{
	struct ospf_if_params *params = IF_DEF_PARAMS(ifp);
	struct bfd_configuration *bfd_config = params->bfd_config;
	struct json_object *json_bfd;

	if (bfd_config == NULL)
		return;

	if (json) {
		json_bfd = json_object_new_object();
		json_object_int_add(json_bfd, "detectionMultiplier",
				    bfd_config->detection_multiplier);
		json_object_int_add(json_bfd, "rxMinInterval",
				    bfd_config->min_rx);
		json_object_int_add(json_bfd, "txMinInterval",
				    bfd_config->min_tx);
		json_object_object_add(json, "peerBfdInfo", json_bfd);
	} else
		vty_out(vty,
			"  BFD: Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
			bfd_config->detection_multiplier, bfd_config->min_rx,
			bfd_config->min_tx);
}

DEFUN (ip_ospf_bfd,
       ip_ospf_bfd_cmd,
       "ip ospf bfd",
       "IP Information\n"
       "OSPF interface commands\n"
       "Enables BFD support\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	ospf_interface_enable_bfd(ifp);
	ospf_interface_bfd_apply(ifp);
	return CMD_SUCCESS;
}

#if HAVE_BFDD > 0
DEFUN_HIDDEN(
#else
DEFUN(
#endif /* HAVE_BFDD */
       ip_ospf_bfd_param,
       ip_ospf_bfd_param_cmd,
       "ip ospf bfd (2-255) (50-60000) (50-60000)",
       "IP Information\n"
       "OSPF interface commands\n"
       "Enables BFD support\n"
       "Detect Multiplier\n"
       "Required min receive interval\n"
       "Desired min transmit interval\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct ospf_if_params *params;
	int idx_number = 3;
	int idx_number_2 = 4;
	int idx_number_3 = 5;

	ospf_interface_enable_bfd(ifp);

	params = IF_DEF_PARAMS(ifp);
	params->bfd_config->detection_multiplier =
		strtol(argv[idx_number]->arg, NULL, 10);
	params->bfd_config->min_rx = strtol(argv[idx_number_2]->arg, NULL, 10);
	params->bfd_config->min_tx = strtol(argv[idx_number_3]->arg, NULL, 10);

	ospf_interface_bfd_apply(ifp);

	return CMD_SUCCESS;
}

DEFUN (ip_ospf_bfd_prof,
       ip_ospf_bfd_prof_cmd,
       "ip ospf bfd profile BFDPROF",
       "IP Information\n"
       "OSPF interface commands\n"
       "Enables BFD support\n"
       BFD_PROFILE_STR
       BFD_PROFILE_NAME_STR)
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct ospf_if_params *params;
	int idx_prof = 4;

	params = IF_DEF_PARAMS(ifp);
	if (!params->bfd_config) {
		vty_out(vty, "ip ospf bfd has not been set\n");
		return CMD_WARNING;
	}

	strlcpy(params->bfd_config->profile, argv[idx_prof]->arg,
		sizeof(params->bfd_config->profile));
	ospf_interface_bfd_apply(ifp);

	return CMD_SUCCESS;
}

DEFUN (no_ip_ospf_bfd_prof,
       no_ip_ospf_bfd_prof_cmd,
       "no ip ospf bfd profile [BFDPROF]",
       NO_STR
       "IP Information\n"
       "OSPF interface commands\n"
       "Enables BFD support\n"
       BFD_PROFILE_STR
       BFD_PROFILE_NAME_STR)
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct ospf_if_params *params;

	params = IF_DEF_PARAMS(ifp);
	if (!params->bfd_config)
		return CMD_SUCCESS;

	params->bfd_config->profile[0] = 0;
	ospf_interface_bfd_apply(ifp);

	return CMD_SUCCESS;
}

DEFUN (no_ip_ospf_bfd,
       no_ip_ospf_bfd_cmd,
#if HAVE_BFDD > 0
       "no ip ospf bfd",
#else
       "no ip ospf bfd [(2-255) (50-60000) (50-60000)]",
#endif /* HAVE_BFDD */
       NO_STR
       "IP Information\n"
       "OSPF interface commands\n"
       "Disables BFD support\n"
#if HAVE_BFDD == 0
       "Detect Multiplier\n"
       "Required min receive interval\n"
       "Desired min transmit interval\n"
#endif /* !HAVE_BFDD */
)
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	ospf_interface_disable_bfd(ifp, IF_DEF_PARAMS(ifp));
	return CMD_SUCCESS;
}

void ospf_bfd_init(struct event_loop *tm)
{
	bfd_protocol_integration_init(zclient, tm);

	/* Install BFD command */
	install_element(INTERFACE_NODE, &ip_ospf_bfd_cmd);
	install_element(INTERFACE_NODE, &ip_ospf_bfd_param_cmd);
	install_element(INTERFACE_NODE, &ip_ospf_bfd_prof_cmd);
	install_element(INTERFACE_NODE, &no_ip_ospf_bfd_prof_cmd);
	install_element(INTERFACE_NODE, &no_ip_ospf_bfd_cmd);
}