]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_bfd.c
lib: enforce vrf_name_to_id by returning default_vrf when name is null
[mirror_frr.git] / pimd / pim_bfd.c
1 /*
2 * pim_bfd.c: PIM BFD handling routines
3 *
4 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * Chirag Shah
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24
25 #include "lib/json.h"
26 #include "command.h"
27 #include "vty.h"
28 #include "zclient.h"
29
30 #include "pim_instance.h"
31 #include "pim_cmd.h"
32 #include "pim_vty.h"
33 #include "pim_iface.h"
34 #include "pim_bfd.h"
35 #include "bfd.h"
36 #include "pimd.h"
37 #include "pim_zebra.h"
38
39 /*
40 * pim_bfd_write_config - Write the interface BFD configuration.
41 */
42 void pim_bfd_write_config(struct vty *vty, struct interface *ifp)
43 {
44 struct pim_interface *pim_ifp = ifp->info;
45 struct bfd_info *bfd_info = NULL;
46
47 if (!pim_ifp)
48 return;
49
50 bfd_info = (struct bfd_info *)pim_ifp->bfd_info;
51 if (!bfd_info)
52 return;
53
54 #if HAVE_BFDD == 0
55 if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG))
56 vty_out(vty, " ip pim bfd %d %d %d\n", bfd_info->detect_mult,
57 bfd_info->required_min_rx, bfd_info->desired_min_tx);
58 else
59 #endif /* ! HAVE_BFDD */
60 vty_out(vty, " ip pim bfd\n");
61 }
62
63 /*
64 * pim_bfd_show_info - Show BFD info structure
65 */
66 void pim_bfd_show_info(struct vty *vty, void *bfd_info, json_object *json_obj,
67 bool use_json, int param_only)
68 {
69 if (param_only)
70 bfd_show_param(vty, (struct bfd_info *)bfd_info, 1, 0, use_json,
71 json_obj);
72 else
73 bfd_show_info(vty, (struct bfd_info *)bfd_info, 0, 1, use_json,
74 json_obj);
75 }
76
77 /*
78 * pim_bfd_info_nbr_create - Create/update BFD information for a neighbor.
79 */
80 void pim_bfd_info_nbr_create(struct pim_interface *pim_ifp,
81 struct pim_neighbor *neigh)
82 {
83 struct bfd_info *nbr_bfd_info = NULL;
84
85 /* Check if Pim Interface BFD is enabled */
86 if (!pim_ifp || !pim_ifp->bfd_info)
87 return;
88
89 if (!neigh->bfd_info)
90 neigh->bfd_info = bfd_info_create();
91
92 if (!neigh->bfd_info)
93 return;
94
95 nbr_bfd_info = (struct bfd_info *)neigh->bfd_info;
96 nbr_bfd_info->detect_mult = pim_ifp->bfd_info->detect_mult;
97 nbr_bfd_info->desired_min_tx = pim_ifp->bfd_info->desired_min_tx;
98 nbr_bfd_info->required_min_rx = pim_ifp->bfd_info->required_min_rx;
99 }
100
101 /*
102 * pim_bfd_info_free - Free BFD info structure
103 */
104 void pim_bfd_info_free(struct bfd_info **bfd_info)
105 {
106 bfd_info_free(bfd_info);
107 }
108
109 static void pim_bfd_reg_dereg_nbr(struct pim_neighbor *nbr, int command)
110 {
111 struct pim_interface *pim_ifp = NULL;
112 struct bfd_info *bfd_info = NULL;
113 struct zclient *zclient = NULL;
114
115 zclient = pim_zebra_zclient_get();
116
117 if (!nbr)
118 return;
119 pim_ifp = nbr->interface->info;
120 bfd_info = (struct bfd_info *)pim_ifp->bfd_info;
121 if (!bfd_info)
122 return;
123 if (PIM_DEBUG_PIM_TRACE) {
124 char str[INET_ADDRSTRLEN];
125 pim_inet4_dump("<bfd_nbr?>", nbr->source_addr, str,
126 sizeof(str));
127 zlog_debug("%s Nbr %s %s with BFD", __PRETTY_FUNCTION__, str,
128 bfd_get_command_dbg_str(command));
129 }
130 bfd_peer_sendmsg(zclient, bfd_info, AF_INET, &nbr->source_addr, NULL,
131 nbr->interface->name, 0, 0, command, 0, VRF_DEFAULT);
132 }
133
134 /*
135 * pim_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
136 * with a interface with BFD through
137 * zebra for starting/stopping the monitoring of
138 * the neighbor rechahability.
139 */
140 int pim_bfd_reg_dereg_all_nbr(struct interface *ifp, int command)
141 {
142 struct pim_interface *pim_ifp = NULL;
143 struct listnode *node = NULL;
144 struct pim_neighbor *neigh = NULL;
145
146 pim_ifp = ifp->info;
147 if (!pim_ifp)
148 return -1;
149 if (!pim_ifp->bfd_info)
150 return -1;
151
152 for (ALL_LIST_ELEMENTS_RO(pim_ifp->pim_neighbor_list, node, neigh)) {
153 if (command != ZEBRA_BFD_DEST_DEREGISTER)
154 pim_bfd_info_nbr_create(pim_ifp, neigh);
155 else
156 pim_bfd_info_free((struct bfd_info **)&neigh->bfd_info);
157
158 pim_bfd_reg_dereg_nbr(neigh, command);
159 }
160
161 return 0;
162 }
163
164 /*
165 * pim_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
166 * neighbor state is changed to/from 2way.
167 */
168 void pim_bfd_trigger_event(struct pim_interface *pim_ifp,
169 struct pim_neighbor *nbr, uint8_t nbr_up)
170 {
171 if (nbr_up) {
172 pim_bfd_info_nbr_create(pim_ifp, nbr);
173 pim_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_REGISTER);
174 } else {
175 pim_bfd_info_free(&nbr->bfd_info);
176 pim_bfd_reg_dereg_nbr(nbr, ZEBRA_BFD_DEST_DEREGISTER);
177 }
178 }
179
180 /*
181 * pim_bfd_if_param_set - Set the configured BFD paramter values for
182 * interface.
183 */
184 void pim_bfd_if_param_set(struct interface *ifp, uint32_t min_rx,
185 uint32_t min_tx, uint8_t detect_mult, int defaults)
186 {
187 struct pim_interface *pim_ifp = ifp->info;
188 int command = 0;
189
190 if (!pim_ifp)
191 return;
192 bfd_set_param((struct bfd_info **)&(pim_ifp->bfd_info), min_rx, min_tx,
193 detect_mult, defaults, &command);
194
195 if (pim_ifp->bfd_info) {
196 if (PIM_DEBUG_PIM_TRACE)
197 zlog_debug("%s: interface %s has bfd_info",
198 __PRETTY_FUNCTION__, ifp->name);
199 }
200 if (command)
201 pim_bfd_reg_dereg_all_nbr(ifp, command);
202 }
203
204
205 /*
206 * pim_bfd_interface_dest_update - Find the neighbor for which the BFD status
207 * has changed and bring down the neighbor
208 * connectivity if the BFD status changed to
209 * down.
210 */
211 static int pim_bfd_interface_dest_update(int command, struct zclient *zclient,
212 zebra_size_t length, vrf_id_t vrf_id)
213 {
214 struct interface *ifp = NULL;
215 struct pim_interface *pim_ifp = NULL;
216 struct prefix p;
217 int status;
218 char msg[100];
219 int old_status;
220 struct bfd_info *bfd_info = NULL;
221 struct timeval tv;
222 struct listnode *neigh_node = NULL;
223 struct listnode *neigh_nextnode = NULL;
224 struct pim_neighbor *neigh = NULL;
225
226 ifp = bfd_get_peer_info(zclient->ibuf, &p, NULL, &status, vrf_id);
227
228 if ((ifp == NULL) || (p.family != AF_INET))
229 return 0;
230
231 pim_ifp = ifp->info;
232 if (!pim_ifp)
233 return 0;
234
235 if (!pim_ifp->bfd_info) {
236 if (PIM_DEBUG_PIM_TRACE)
237 zlog_debug("%s: pim interface %s BFD is disabled ",
238 __PRETTY_FUNCTION__, ifp->name);
239 return 0;
240 }
241
242 if (PIM_DEBUG_PIM_TRACE) {
243 char buf[PREFIX2STR_BUFFER];
244 prefix2str(&p, buf, sizeof(buf));
245 zlog_debug("%s: interface %s bfd destination %s %s",
246 __PRETTY_FUNCTION__, ifp->name, buf,
247 bfd_get_status_str(status));
248 }
249
250 for (ALL_LIST_ELEMENTS(pim_ifp->pim_neighbor_list, neigh_node,
251 neigh_nextnode, neigh)) {
252 /* Check neigh address matches with BFD address */
253 if (neigh->source_addr.s_addr != p.u.prefix4.s_addr)
254 continue;
255
256 bfd_info = (struct bfd_info *)neigh->bfd_info;
257 if (bfd_info->status == status) {
258 if (PIM_DEBUG_PIM_TRACE) {
259 char str[INET_ADDRSTRLEN];
260 pim_inet4_dump("<nht_nbr?>", neigh->source_addr,
261 str, sizeof(str));
262 zlog_debug("%s: bfd status is same for nbr %s",
263 __PRETTY_FUNCTION__, str);
264 }
265 continue;
266 }
267 old_status = bfd_info->status;
268 bfd_info->status = status;
269 monotime(&tv);
270 bfd_info->last_update = tv.tv_sec;
271
272 if (PIM_DEBUG_PIM_TRACE) {
273 zlog_debug("%s: status %s old_status %s",
274 __PRETTY_FUNCTION__,
275 bfd_get_status_str(status),
276 bfd_get_status_str(old_status));
277 }
278 if ((status == BFD_STATUS_DOWN)
279 && (old_status == BFD_STATUS_UP)) {
280 snprintf(msg, sizeof(msg), "BFD Session Expired");
281 pim_neighbor_delete(ifp, neigh, msg);
282 }
283 }
284 return 0;
285 }
286
287 /*
288 * pim_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
289 * to zebra
290 */
291 static int pim_bfd_nbr_replay(int command, struct zclient *zclient,
292 zebra_size_t length, vrf_id_t vrf_id)
293 {
294 struct interface *ifp = NULL;
295 struct pim_interface *pim_ifp = NULL;
296 struct pim_neighbor *neigh = NULL;
297 struct listnode *neigh_node;
298 struct listnode *neigh_nextnode;
299 struct vrf *vrf = NULL;
300
301 /* Send the client registration */
302 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
303
304 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
305 FOR_ALL_INTERFACES (vrf, ifp) {
306 pim_ifp = ifp->info;
307
308 if (!pim_ifp)
309 continue;
310
311 if (pim_ifp->pim_sock_fd < 0)
312 continue;
313
314 for (ALL_LIST_ELEMENTS(pim_ifp->pim_neighbor_list,
315 neigh_node, neigh_nextnode,
316 neigh)) {
317 if (!neigh->bfd_info)
318 continue;
319 if (PIM_DEBUG_PIM_TRACE) {
320 char str[INET_ADDRSTRLEN];
321
322 pim_inet4_dump("<bfd_nbr?>",
323 neigh->source_addr, str,
324 sizeof(str));
325 zlog_debug(
326 "%s: Replaying Pim Neigh %s to BFD vrf_id %u",
327 __PRETTY_FUNCTION__, str,
328 vrf->vrf_id);
329 }
330 pim_bfd_reg_dereg_nbr(neigh,
331 ZEBRA_BFD_DEST_UPDATE);
332 }
333 }
334 }
335 return 0;
336 }
337
338 void pim_bfd_init(void)
339 {
340 struct zclient *zclient = NULL;
341
342 zclient = pim_zebra_zclient_get();
343
344 bfd_gbl_init();
345
346 zclient->interface_bfd_dest_update = pim_bfd_interface_dest_update;
347 zclient->bfd_dest_replay = pim_bfd_nbr_replay;
348 }