]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_bfd.c
Merge remote-tracking branch 'frr/master' into table-hash-ospf6-lsdb-refactor
[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_cmd.h"
31 #include "pim_vty.h"
32 #include "pim_iface.h"
33 #include "pim_bfd.h"
34 #include "bfd.h"
35 #include "pimd.h"
36 #include "pim_zebra.h"
37
38 /*
39 * pim_bfd_write_config - Write the interface BFD configuration.
40 */
41 void
42 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 (CHECK_FLAG (bfd_info->flags, BFD_FLAG_PARAM_CFG))
55 vty_out (vty, " ip pim bfd %d %d %d\n",
56 bfd_info->detect_mult, bfd_info->required_min_rx,
57 bfd_info->desired_min_tx);
58 else
59 vty_out (vty, " ip pim bfd\n");
60 }
61
62 /*
63 * pim_bfd_show_info - Show BFD info structure
64 */
65 void
66 pim_bfd_show_info (struct vty *vty, void *bfd_info, json_object * json_obj,
67 u_char 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
81 pim_bfd_info_nbr_create (struct pim_interface *pim_ifp,
82 struct pim_neighbor *neigh)
83 {
84 struct bfd_info *nbr_bfd_info = NULL;
85
86 /* Check if Pim Interface BFD is enabled */
87 if (!pim_ifp || !pim_ifp->bfd_info)
88 return;
89
90 if (!neigh->bfd_info)
91 neigh->bfd_info = bfd_info_create ();
92
93 if (!neigh->bfd_info)
94 return;
95
96 nbr_bfd_info = (struct bfd_info *) neigh->bfd_info;
97 nbr_bfd_info->detect_mult = pim_ifp->bfd_info->detect_mult;
98 nbr_bfd_info->desired_min_tx = pim_ifp->bfd_info->desired_min_tx;
99 nbr_bfd_info->required_min_rx = pim_ifp->bfd_info->required_min_rx;
100 }
101
102 /*
103 * pim_bfd_info_free - Free BFD info structure
104 */
105 void
106 pim_bfd_info_free (void **bfd_info)
107 {
108 bfd_info_free ((struct bfd_info **) bfd_info);
109 }
110
111 static void
112 pim_bfd_reg_dereg_nbr (struct pim_neighbor *nbr, int command)
113 {
114 struct pim_interface *pim_ifp = NULL;
115 struct bfd_info *bfd_info = NULL;
116 struct zclient *zclient = NULL;
117
118 zclient = pim_zebra_zclient_get ();
119
120 if (!nbr)
121 return;
122 pim_ifp = nbr->interface->info;
123 bfd_info = (struct bfd_info *) pim_ifp->bfd_info;
124 if (!bfd_info)
125 return;
126 if (PIM_DEBUG_PIM_TRACE)
127 {
128 char str[INET_ADDRSTRLEN];
129 pim_inet4_dump ("<bfd_nbr?>", nbr->source_addr, str, sizeof (str));
130 zlog_debug ("%s Nbr %s %s with BFD", __PRETTY_FUNCTION__, str,
131 bfd_get_command_dbg_str (command));
132 }
133 bfd_peer_sendmsg (zclient, bfd_info, AF_INET,
134 &nbr->source_addr, NULL, nbr->interface->name,
135 0, 0, command, 0, VRF_DEFAULT);
136 }
137
138 /*
139 * pim_bfd_reg_dereg_all_nbr - Register/Deregister all neighbors associated
140 * with a interface with BFD through
141 * zebra for starting/stopping the monitoring of
142 * the neighbor rechahability.
143 */
144 int
145 pim_bfd_reg_dereg_all_nbr (struct interface *ifp, int command)
146 {
147 struct pim_interface *pim_ifp = NULL;
148 struct listnode *node = NULL;
149 struct pim_neighbor *neigh = NULL;
150
151 pim_ifp = ifp->info;
152 if (!pim_ifp)
153 return -1;
154 if (!pim_ifp->bfd_info)
155 return -1;
156
157 for (ALL_LIST_ELEMENTS_RO (pim_ifp->pim_neighbor_list, node, neigh))
158 {
159 if (command != ZEBRA_BFD_DEST_DEREGISTER)
160 pim_bfd_info_nbr_create (pim_ifp, neigh);
161 else
162 bfd_info_free ((struct bfd_info **) &neigh->bfd_info);
163
164 pim_bfd_reg_dereg_nbr (neigh, command);
165 }
166
167 return 0;
168 }
169
170 /*
171 * pim_bfd_trigger_event - Neighbor is registered/deregistered with BFD when
172 * neighbor state is changed to/from 2way.
173 */
174 void
175 pim_bfd_trigger_event (struct pim_interface *pim_ifp, struct pim_neighbor *nbr, uint8_t nbr_up)
176 {
177 if (nbr_up)
178 {
179 pim_bfd_info_nbr_create (pim_ifp, nbr);
180 pim_bfd_reg_dereg_nbr (nbr, ZEBRA_BFD_DEST_REGISTER);
181 }
182 else
183 {
184 pim_bfd_info_free ((void *)&nbr->bfd_info);
185 pim_bfd_reg_dereg_nbr (nbr, ZEBRA_BFD_DEST_DEREGISTER);
186 }
187 }
188
189 /*
190 * pim_bfd_if_param_set - Set the configured BFD paramter values for
191 * interface.
192 */
193 void
194 pim_bfd_if_param_set (struct interface *ifp, u_int32_t min_rx,
195 u_int32_t min_tx, u_int8_t detect_mult, int defaults)
196 {
197 struct pim_interface *pim_ifp = ifp->info;
198 int command = 0;
199
200 if (!pim_ifp)
201 return;
202 bfd_set_param ((struct bfd_info **) &(pim_ifp->bfd_info), min_rx, min_tx,
203 detect_mult, defaults, &command);
204
205 if (pim_ifp->bfd_info)
206 {
207 if (PIM_DEBUG_PIM_TRACE)
208 zlog_debug ("%s: interface %s has bfd_info", __PRETTY_FUNCTION__,
209 ifp->name);
210 }
211 if (command)
212 pim_bfd_reg_dereg_all_nbr (ifp, command);
213 }
214
215
216 /*
217 * pim_bfd_interface_dest_update - Find the neighbor for which the BFD status
218 * has changed and bring down the neighbor
219 * connectivity if the BFD status changed to
220 * down.
221 */
222 static int
223 pim_bfd_interface_dest_update (int command, struct zclient *zclient,
224 zebra_size_t length, vrf_id_t vrf_id)
225 {
226 struct interface *ifp = NULL;
227 struct pim_interface *pim_ifp = NULL;
228 struct prefix p;
229 int status;
230 char msg[100];
231 int old_status;
232 struct bfd_info *bfd_info = NULL;
233 struct timeval tv;
234 struct listnode *neigh_node = NULL;
235 struct listnode *neigh_nextnode = NULL;
236 struct pim_neighbor *neigh = NULL;
237
238 ifp = bfd_get_peer_info (zclient->ibuf, &p, NULL, &status, vrf_id);
239
240 if ((ifp == NULL) || (p.family != AF_INET))
241 return 0;
242
243 pim_ifp = ifp->info;
244 if (!pim_ifp)
245 return 0;
246
247 if (!pim_ifp->bfd_info)
248 {
249 if (PIM_DEBUG_PIM_TRACE)
250 zlog_debug ("%s: pim interface %s BFD is disabled ", __PRETTY_FUNCTION__,
251 ifp->name);
252 return 0;
253 }
254
255 if (PIM_DEBUG_PIM_TRACE)
256 {
257 char buf[PREFIX2STR_BUFFER];
258 prefix2str (&p, buf, sizeof (buf));
259 zlog_debug ("%s: interface %s bfd destination %s %s",
260 __PRETTY_FUNCTION__, ifp->name, buf,
261 bfd_get_status_str (status));
262 }
263
264 for (ALL_LIST_ELEMENTS (pim_ifp->pim_neighbor_list, neigh_node,
265 neigh_nextnode, neigh))
266 {
267 /* Check neigh address matches with BFD address */
268 if (neigh->source_addr.s_addr != p.u.prefix4.s_addr)
269 continue;
270
271 bfd_info = (struct bfd_info *) neigh->bfd_info;
272 if (bfd_info->status == status)
273 {
274 if (PIM_DEBUG_PIM_TRACE)
275 {
276 char str[INET_ADDRSTRLEN];
277 pim_inet4_dump ("<nht_nbr?>", neigh->source_addr, str,
278 sizeof (str));
279 zlog_debug ("%s: bfd status is same for nbr %s",
280 __PRETTY_FUNCTION__, str);
281 }
282 continue;
283 }
284 old_status = bfd_info->status;
285 bfd_info->status = status;
286 monotime(&tv);
287 bfd_info->last_update = tv.tv_sec;
288
289 if (PIM_DEBUG_PIM_TRACE)
290 {
291 zlog_debug ("%s: status %s old_status %s", __PRETTY_FUNCTION__,
292 bfd_get_status_str (status),
293 bfd_get_status_str (old_status));
294 }
295 if ((status == BFD_STATUS_DOWN) && (old_status == BFD_STATUS_UP))
296 {
297 snprintf (msg, sizeof (msg), "BFD Session Expired");
298 pim_neighbor_delete (ifp, neigh, msg);
299 }
300 }
301 return 0;
302 }
303
304 /*
305 * pim_bfd_nbr_replay - Replay all the neighbors that have BFD enabled
306 * to zebra
307 */
308 static int
309 pim_bfd_nbr_replay (int command, struct zclient *zclient, zebra_size_t length,
310 vrf_id_t vrf_id)
311 {
312 struct interface *ifp = NULL;
313 struct pim_interface *pim_ifp = NULL;
314 struct pim_neighbor *neigh = NULL;
315 struct listnode *node;
316 struct listnode *neigh_node;
317 struct listnode *neigh_nextnode;
318
319 /* Send the client registration */
320 bfd_client_sendmsg (zclient, ZEBRA_BFD_CLIENT_REGISTER);
321
322 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
323 {
324 pim_ifp = ifp->info;
325
326 if (!pim_ifp)
327 continue;
328
329 if (pim_ifp->pim_sock_fd < 0)
330 continue;
331
332 for (ALL_LIST_ELEMENTS (pim_ifp->pim_neighbor_list, neigh_node,
333 neigh_nextnode, neigh))
334 {
335 if (!neigh->bfd_info)
336 continue;
337 if (PIM_DEBUG_PIM_TRACE)
338 {
339 char str[INET_ADDRSTRLEN];
340 pim_inet4_dump ("<bfd_nbr?>", neigh->source_addr, str,
341 sizeof (str));
342 zlog_debug ("%s: Replaying Pim Neigh %s to BFD",
343 __PRETTY_FUNCTION__, str);
344 }
345 pim_bfd_reg_dereg_nbr (neigh, ZEBRA_BFD_DEST_UPDATE);
346
347 }
348 }
349 return 0;
350 }
351
352 void
353 pim_bfd_init (void)
354 {
355 struct zclient *zclient = NULL;
356
357 zclient = pim_zebra_zclient_get ();
358
359 bfd_gbl_init ();
360
361 zclient->interface_bfd_dest_update = pim_bfd_interface_dest_update;
362 zclient->bfd_dest_replay = pim_bfd_nbr_replay;
363 }