summaryrefslogtreecommitdiffstats
path: root/pimd/pim_bfd.c
blob: 84f6733eafdf0535f56fa0d07a9d57b2b792ba19 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * pim_bfd.c: PIM BFD handling routines
 *
 * Copyright (C) 2017 Cumulus Networks, Inc.
 * Chirag Shah
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

#include <zebra.h>

#include "lib/json.h"
#include "command.h"
#include "vty.h"
#include "zclient.h"

#include "pim_instance.h"
#include "pim_cmd.h"
#include "pim_vty.h"
#include "pim_iface.h"
#include "pim_bfd.h"
#include "bfd.h"
#include "pimd.h"
#include "pim_zebra.h"

/*
 * pim_bfd_write_config - Write the interface BFD configuration.
 */
void pim_bfd_write_config(struct vty *vty, struct interface *ifp)
{
	struct pim_interface *pim_ifp = ifp->info;
	struct bfd_info *bfd_info = NULL;

	if (!pim_ifp)
		return;

	bfd_info = (struct bfd_info *)pim_ifp->bfd_info;
	if (!bfd_info)
		return;

#if HAVE_BFDD == 0
	if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
		vty_out(vty, " ip pim bfd %d %d %d\n", bfd_info->detect_mult,
			bfd_info->required_min_rx, bfd_info->desired_min_tx);
	else
#endif /* ! HAVE_BFDD */
		vty_out(vty, " ip pim bfd\n");
}

/*
 * pim_bfd_show_info - Show BFD info structure
 */
void pim_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
		       bool use_json, int param_only)
{
	if (param_only)
		bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json,
			       json_obj);
	else
		bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json,
			      json_obj);
}

/*
 * pim_bfd_info_nbr_create - Create/update BFD information for a neighbor.
 */
void pim_bfd_info_nbr_create(struct pim_interface *pim_ifp,
			     struct pim_neighbor *neigh)
{
	struct bfd_info *nbr_bfd_info = NULL;

	/* Check if Pim Interface BFD is enabled */
	if (!pim_ifp || !pim_ifp->bfd_info)
		return;

	if (!neigh->bfd_info)
		neigh->bfd_info = bfd_info_create();

	if (!neigh->bfd_info)
		return;

	nbr_bfd_info = (struct bfd_info *)neigh->bfd_info;
	nbr_bfd_info->detect_mult = pim_ifp->bfd_info->detect_mult;
	nbr_bfd_info->desired_min_tx = pim_ifp->bfd_info->desired_min_tx;
	nbr_bfd_info->required_min_rx = pim_ifp->bfd_info->required_min_rx;
}

/*
 * pim_bfd_info_free - Free BFD info structure
 */
void pim_bfd_info_free(struct bfd_info **bfd_info)
{
	bfd_info_free(bfd_info);
}

static void pim_bfd_reg_dereg_nbr(struct pim_neighbor *nbr, int command)
{
	struct pim_interface *pim_ifp = NULL;
	struct bfd_info *bfd_info = NULL;
	struct zclient *zclient = NULL;
	int cbit;

	zclient = pim_zebra_zclient_get();

	if (!nbr)
		return;
	pim_ifp = nbr->interface->info;
	bfd_info = (struct bfd_info *)pim_ifp->bfd_info;
	if (!bfd_info)
		return;
	if (PIM_DEBUG_PIM_TRACE) {
		char str[INET_ADDRSTRLEN];
		pim_inet4_dump("<bfd_nbr?>", nbr->source_addr, str,
			       sizeof(str));
		zlog_debug("%s Nbr %s %s with BFD", __func__, str,
			   bfd_get_command_dbg_str(command));
	}

	cbit = CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CBIT_ON);

	bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->source_addr, NULL,
			 nbr->interface->name, 0, 0, cbit,
			 command, 0, VRF_DEFAULT);
}

/*
 * pim_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.
 */
int pim_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
{
	struct pim_interface *pim_ifp = NULL;
	struct listnode *node = NULL;
	struct pim_neighbor *neigh = NULL;

	pim_ifp = ifp->info;
	if (!pim_ifp)
		return -1;
	if (!pim_ifp->bfd_info)
		return -1;

	for (ALL_LIST_ELEMENTS_RO(pim_ifp->pim_neighbor_list, node, neigh)) {
		if (command != ZEBRA_BFD_DEST_DEREGISTER)
			pim_bfd_info_nbr_create(pim_ifp, neigh);
		else
			pim_bfd_info_free((struct bfd_info **)&neigh->bfd_info);

		pim_bfd_reg_dereg_nbr(neigh, command);
	}

	return 0;
}

/*
 * pim_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
 *                          neighbor state is changed to/from 2way.
 */
void pim_bfd_trigger_event(struct pim_interface *pim_ifp,
			   struct pim_neighbor *nbr, uint8_t nbr_up)
{
	if (nbr_up) {
		pim_bfd_info_nbr_create(pim_ifp, nbr);
		pim_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_REGISTER);
	} else {
		pim_bfd_info_free(&nbr->bfd_info);
		pim_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_DEREGISTER);
	}
}

/*
 * pim_bfd_if_param_set - Set the configured BFD paramter values for
 *                         interface.
 */
void pim_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
			  uint32_t min_tx, uint8_t detect_mult, int defaults)
{
	struct pim_interface *pim_ifp = ifp->info;
	int command = 0;

	if (!pim_ifp)
		return;
	bfd_set_param((struct bfd_info **)&(pim_ifp->bfd_info), min_rx, min_tx,
		      detect_mult, defaults, &command);

	if (pim_ifp->bfd_info) {
		if (PIM_DEBUG_PIM_TRACE)
			zlog_debug("%s: interface %s has bfd_info", __func__,
				   ifp->name);
	}
	if (command)
		pim_bfd_reg_dereg_all_nbr(ifp, command);
}


/*
 * pim_bfd_interface_dest_update - Find the neighbor for which the BFD status
 *                                  has changed and bring down the neighbor
 *                                  connectivity if the BFD status changed to
 *                                  down.
 */
static int pim_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
{
	struct interface *ifp = NULL;
	struct pim_interface *pim_ifp = NULL;
	struct prefix p;
	int status;
	char msg[100];
	int old_status;
	struct bfd_info *bfd_info = NULL;
	struct timeval tv;
	struct listnode *neigh_node = NULL;
	struct listnode *neigh_nextnode = NULL;
	struct pim_neighbor *neigh = NULL;

	ifp = bfd_get_peer_info(zclient->ibuf, &p, NULL, &status,
				NULL, vrf_id);

	if ((ifp == NULL) || (p.family != AF_INET))
		return 0;

	pim_ifp = ifp->info;
	if (!pim_ifp)
		return 0;

	if (!pim_ifp->bfd_info) {
		if (PIM_DEBUG_PIM_TRACE)
			zlog_debug("%s: pim interface %s BFD is disabled ",
				   __func__, ifp->name);
		return 0;
	}

	if (PIM_DEBUG_PIM_TRACE) {
		char buf[PREFIX2STR_BUFFER];
		prefix2str(&p, buf, sizeof(buf));
		zlog_debug("%s: interface %s bfd destination %s %s", __func__,
			   ifp->name, buf, bfd_get_status_str(status));
	}

	for (ALL_LIST_ELEMENTS(pim_ifp->pim_neighbor_list, neigh_node,
			       neigh_nextnode, neigh)) {
		/* Check neigh address matches with BFD address */
		if (neigh->source_addr.s_addr != p.u.prefix4.s_addr)
			continue;

		bfd_info = (struct bfd_info *)neigh->bfd_info;
		if (bfd_info->status == status) {
			if (PIM_DEBUG_PIM_TRACE) {
				char str[INET_ADDRSTRLEN];
				pim_inet4_dump("<nht_nbr?>", neigh->source_addr,
					       str, sizeof(str));
				zlog_debug("%s: bfd status is same for nbr %s",
					   __func__, str);
			}
			continue;
		}
		old_status = bfd_info->status;
		BFD_SET_CLIENT_STATUS(bfd_info->status, status);
		monotime(&tv);
		bfd_info->last_update = tv.tv_sec;

		if (PIM_DEBUG_PIM_TRACE) {
			zlog_debug("%s: status %s old_status %s", __func__,
				   bfd_get_status_str(status),
				   bfd_get_status_str(old_status));
		}
		if ((status == BFD_STATUS_DOWN)
		    && (old_status == BFD_STATUS_UP)) {
			snprintf(msg, sizeof(msg), "BFD Session Expired");
			pim_neighbor_delete(ifp, neigh, msg);
		}
	}
	return 0;
}

/*
 * pim_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
 *                       to zebra
 */
static int pim_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
{
	struct interface *ifp = NULL;
	struct pim_interface *pim_ifp = NULL;
	struct pim_neighbor *neigh = NULL;
	struct listnode *neigh_node;
	struct listnode *neigh_nextnode;
	struct vrf *vrf = NULL;

	/* Send the client registration */
	bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);

	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
		FOR_ALL_INTERFACES (vrf, ifp) {
			pim_ifp = ifp->info;

			if (!pim_ifp)
				continue;

			if (pim_ifp->pim_sock_fd < 0)
				continue;

			for (ALL_LIST_ELEMENTS(pim_ifp->pim_neighbor_list,
					       neigh_node, neigh_nextnode,
					       neigh)) {
				if (!neigh->bfd_info)
					continue;
				if (PIM_DEBUG_PIM_TRACE) {
					char str[INET_ADDRSTRLEN];

					pim_inet4_dump("<bfd_nbr?>",
						       neigh->source_addr, str,
						       sizeof(str));
					zlog_debug(
						"%s: Replaying Pim Neigh %s to BFD vrf_id %u",
						__func__, str, vrf->vrf_id);
				}
				pim_bfd_reg_dereg_nbr(neigh,
						      ZEBRA_BFD_DEST_UPDATE);
			}
		}
	}
	return 0;
}

void pim_bfd_init(void)
{
	struct zclient *zclient = NULL;

	zclient = pim_zebra_zclient_get();

	bfd_gbl_init();

	zclient->interface_bfd_dest_update = pim_bfd_interface_dest_update;
	zclient->bfd_dest_replay = pim_bfd_nbr_replay;
}