summaryrefslogtreecommitdiffstats
path: root/ospf6d/ospf6_bfd.c
blob: 6379f9d992c59ebb7aa9e767b0ce1c095187d61d (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
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * ospf6_bfd.c: IPv6 OSPF BFD handling routines
 *
 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
 */

#include <zebra.h>

#include "command.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 "if.h"
#include "ospf6d.h"
#include "ospf6_message.h"
#include "ospf6_neighbor.h"
#include "ospf6_interface.h"
#include "ospf6_route.h"
#include "ospf6_zebra.h"
#include "ospf6_bfd.h"

extern struct zclient *zclient;

/*
 * ospf6_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
 *                           neighbor state is changed to/from 2way.
 */
void ospf6_bfd_trigger_event(struct ospf6_neighbor *on, int old_state,
			     int state)
{
	int family;
	struct in6_addr src, dst;

	/* Skip sessions without BFD. */
	if (on->bfd_session == NULL)
		return;

	if (old_state < OSPF6_NEIGHBOR_TWOWAY
	    && state >= OSPF6_NEIGHBOR_TWOWAY) {
		/*
		 * Check if neighbor address changed.
		 *
		 * When the neighbor is configured BFD before having an existing
		 * connection, then the destination address will be set to `::`
		 * which will cause session installation failure. This piece of
		 * code updates the address in that case.
		 */
		bfd_sess_addresses(on->bfd_session, &family, &src, &dst);
		if (memcmp(&on->linklocal_addr, &dst, sizeof(dst))) {
			bfd_sess_set_ipv6_addrs(on->bfd_session, &src,
						&on->linklocal_addr);
		}

		bfd_sess_install(on->bfd_session);
	} else if (old_state >= OSPF6_NEIGHBOR_TWOWAY
		   && state < OSPF6_NEIGHBOR_TWOWAY)
		bfd_sess_uninstall(on->bfd_session);
}

/*
 * ospf6_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
 *                               with a interface with BFD through
 *                               zebra for starting/stopping the monitoring of
 *                               the neighbor rechahability.
 */
static void ospf6_bfd_reg_dereg_all_nbr(struct ospf6_interface *oi,
					bool install)
{
	struct ospf6_neighbor *on;
	struct listnode *node;

	for (ALL_LIST_ELEMENTS_RO(oi->neighbor_list, node, on)) {
		/* Remove all sessions. */
		if (!install) {
			bfd_sess_free(&on->bfd_session);
			continue;
		}

		/* Always allocate session data even if not enabled. */
		ospf6_bfd_info_nbr_create(oi, on);

		/*
		 * If not connected yet, don't create any session but defer it
		 * for later. See function `ospf6_bfd_trigger_event`.
		 */
		if (on->state < OSPF6_NEIGHBOR_TWOWAY)
			continue;

		bfd_sess_install(on->bfd_session);
	}
}

static void ospf6_bfd_callback(struct bfd_session_params *bsp,
			       const struct bfd_session_status *bss, void *arg)
{
	struct ospf6_neighbor *on = arg;

	if (bss->state == BFD_STATUS_DOWN
	    && bss->previous_state == BFD_STATUS_UP) {
		EVENT_OFF(on->inactivity_timer);
		event_add_event(master, inactivity_timer, on, 0, NULL);
	}
}

/*
 * ospf6_bfd_info_nbr_create - Create/update BFD information for a neighbor.
 */
void ospf6_bfd_info_nbr_create(struct ospf6_interface *oi,
			       struct ospf6_neighbor *on)
{
	if (!oi->bfd_config.enabled)
		return;

	if (on->bfd_session == NULL)
		on->bfd_session = bfd_sess_new(ospf6_bfd_callback, on);

	bfd_sess_set_timers(on->bfd_session,
			    oi->bfd_config.detection_multiplier,
			    oi->bfd_config.min_rx, oi->bfd_config.min_tx);
	bfd_sess_set_ipv6_addrs(on->bfd_session, on->ospf6_if->linklocal_addr,
				&on->linklocal_addr);
	bfd_sess_set_interface(on->bfd_session, oi->interface->name);
	bfd_sess_set_vrf(on->bfd_session, oi->interface->vrf->vrf_id);
	bfd_sess_set_profile(on->bfd_session, oi->bfd_config.profile);
}

/*
 * ospf6_bfd_write_config - Write the interface BFD configuration.
 */
void ospf6_bfd_write_config(struct vty *vty, struct ospf6_interface *oi)
{
	if (!oi->bfd_config.enabled)
		return;

#if HAVE_BFDD == 0
	if (oi->bfd_config.detection_multiplier != BFD_DEF_DETECT_MULT
	    || oi->bfd_config.min_rx != BFD_DEF_MIN_RX
	    || oi->bfd_config.min_tx != BFD_DEF_MIN_TX)
		vty_out(vty, " ipv6 ospf6 bfd %d %d %d\n",
			oi->bfd_config.detection_multiplier,
			oi->bfd_config.min_rx, oi->bfd_config.min_tx);
	else
#endif /* ! HAVE_BFDD */
		vty_out(vty, " ipv6 ospf6 bfd\n");

	if (oi->bfd_config.profile)
		vty_out(vty, " ipv6 ospf6 bfd profile %s\n",
			oi->bfd_config.profile);
}

DEFUN(ipv6_ospf6_bfd, ipv6_ospf6_bfd_cmd,
      "ipv6 ospf6 bfd [profile BFDPROF]",
      IP6_STR OSPF6_STR
      "Enables BFD support\n"
      "BFD Profile selection\n"
      "BFD Profile name\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct ospf6_interface *oi;
	int prof_idx = 4;
	assert(ifp);

	oi = (struct ospf6_interface *)ifp->info;
	if (oi == NULL)
		oi = ospf6_interface_create(ifp);
	assert(oi);

	oi->bfd_config.detection_multiplier = BFD_DEF_DETECT_MULT;
	oi->bfd_config.min_rx = BFD_DEF_MIN_RX;
	oi->bfd_config.min_tx = BFD_DEF_MIN_TX;
	oi->bfd_config.enabled = true;
	if (argc > prof_idx) {
		XFREE(MTYPE_TMP, oi->bfd_config.profile);
		oi->bfd_config.profile =
			XSTRDUP(MTYPE_TMP, argv[prof_idx]->arg);
	}

	ospf6_bfd_reg_dereg_all_nbr(oi, true);

	return CMD_SUCCESS;
}

DEFUN(no_ipv6_ospf6_bfd_profile, no_ipv6_ospf6_bfd_profile_cmd,
      "no ipv6 ospf6 bfd profile [BFDPROF]",
      NO_STR IP6_STR OSPF6_STR
      "BFD support\n"
      "BFD Profile selection\n"
      "BFD Profile name\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct ospf6_interface *oi;
	assert(ifp);

	oi = (struct ospf6_interface *)ifp->info;
	if (oi == NULL)
		oi = ospf6_interface_create(ifp);
	assert(oi);

	/* BFD not enabled, nothing to do. */
	if (!oi->bfd_config.enabled)
		return CMD_SUCCESS;

	/* Remove profile and apply new configuration. */
	XFREE(MTYPE_TMP, oi->bfd_config.profile);
	ospf6_bfd_reg_dereg_all_nbr(oi, true);

	return CMD_SUCCESS;
}

#if HAVE_BFDD > 0
DEFUN_HIDDEN(
#else
DEFUN(
#endif /* HAVE_BFDD */
       ipv6_ospf6_bfd_param,
       ipv6_ospf6_bfd_param_cmd,
       "ipv6 ospf6 bfd (2-255) (50-60000) (50-60000)",
       IP6_STR
       OSPF6_STR
       "Enables BFD support\n"
       "Detect Multiplier\n"
       "Required min receive interval\n"
       "Desired min transmit interval\n")
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	int idx_number = 3;
	int idx_number_2 = 4;
	int idx_number_3 = 5;
	struct ospf6_interface *oi;

	assert(ifp);

	oi = (struct ospf6_interface *)ifp->info;
	if (oi == NULL)
		oi = ospf6_interface_create(ifp);
	assert(oi);

	oi->bfd_config.detection_multiplier =
		strtoul(argv[idx_number]->arg, NULL, 10);
	oi->bfd_config.min_rx = strtoul(argv[idx_number_2]->arg, NULL, 10);
	oi->bfd_config.min_tx = strtoul(argv[idx_number_3]->arg, NULL, 10);
	oi->bfd_config.enabled = true;

	ospf6_bfd_reg_dereg_all_nbr(oi, true);

	return CMD_SUCCESS;
}

DEFUN (no_ipv6_ospf6_bfd,
       no_ipv6_ospf6_bfd_cmd,
       "no ipv6 ospf6 bfd",
       NO_STR
       IP6_STR
       OSPF6_STR
       "Disables BFD support\n"
       )
{
	VTY_DECLVAR_CONTEXT(interface, ifp);
	struct ospf6_interface *oi;
	assert(ifp);

	oi = (struct ospf6_interface *)ifp->info;
	if (oi == NULL)
		oi = ospf6_interface_create(ifp);
	assert(oi);

	oi->bfd_config.enabled = false;
	ospf6_bfd_reg_dereg_all_nbr(oi, false);

	return CMD_SUCCESS;
}

void ospf6_bfd_init(void)
{
	bfd_protocol_integration_init(zclient, master);

	/* Install BFD command */
	install_element(INTERFACE_NODE, &ipv6_ospf6_bfd_cmd);
	install_element(INTERFACE_NODE, &ipv6_ospf6_bfd_param_cmd);
	install_element(INTERFACE_NODE, &no_ipv6_ospf6_bfd_profile_cmd);
	install_element(INTERFACE_NODE, &no_ipv6_ospf6_bfd_cmd);
}