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