]>
Commit | Line | Data |
---|---|---|
c43ed2e4 DS |
1 | /** |
2 | * bgp_bfd.c: BGP BFD handling routines | |
3 | * | |
4 | * @copyright Copyright (C) 2015 Cumulus Networks, Inc. | |
5 | * | |
6 | * This file is part of GNU Zebra. | |
7 | * | |
8 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * GNU Zebra is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
896014f4 DL |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; see the file COPYING; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
c43ed2e4 DS |
21 | */ |
22 | ||
23 | #include <zebra.h> | |
24 | ||
25 | #include "command.h" | |
26 | #include "linklist.h" | |
27 | #include "memory.h" | |
28 | #include "prefix.h" | |
29 | #include "thread.h" | |
30 | #include "buffer.h" | |
31 | #include "stream.h" | |
32 | #include "zclient.h" | |
7f342629 | 33 | #include "bfd.h" |
856ca177 | 34 | #include "lib/json.h" |
039f3a34 DS |
35 | #include "filter.h" |
36 | ||
c43ed2e4 | 37 | #include "bgpd/bgpd.h" |
7f342629 | 38 | #include "bgp_fsm.h" |
c43ed2e4 DS |
39 | #include "bgpd/bgp_bfd.h" |
40 | #include "bgpd/bgp_debug.h" | |
41 | #include "bgpd/bgp_vty.h" | |
42 | ||
43 | extern struct zclient *zclient; | |
44 | ||
c43ed2e4 | 45 | /* |
d62a17ae | 46 | * bgp_bfd_peer_group2peer_copy - Copy the BFD information from peer group |
47 | * template | |
c43ed2e4 DS |
48 | * to peer. |
49 | */ | |
d62a17ae | 50 | void bgp_bfd_peer_group2peer_copy(struct peer *conf, struct peer *peer) |
c43ed2e4 | 51 | { |
d62a17ae | 52 | struct bfd_info *bfd_info; |
53 | struct bfd_info *conf_bfd_info; | |
7f342629 | 54 | |
d62a17ae | 55 | if (!conf->bfd_info) |
56 | return; | |
7f342629 | 57 | |
d62a17ae | 58 | conf_bfd_info = (struct bfd_info *)conf->bfd_info; |
59 | if (!peer->bfd_info) | |
60 | peer->bfd_info = bfd_info_create(); | |
c43ed2e4 | 61 | |
d62a17ae | 62 | bfd_info = (struct bfd_info *)peer->bfd_info; |
c43ed2e4 | 63 | |
d62a17ae | 64 | /* Copy BFD parameter values */ |
65 | bfd_info->required_min_rx = conf_bfd_info->required_min_rx; | |
66 | bfd_info->desired_min_tx = conf_bfd_info->desired_min_tx; | |
67 | bfd_info->detect_mult = conf_bfd_info->detect_mult; | |
68 | bfd_info->type = conf_bfd_info->type; | |
c43ed2e4 DS |
69 | } |
70 | ||
71 | /* | |
d62a17ae | 72 | * bgp_bfd_is_peer_multihop - returns whether BFD peer is multi-hop or single |
73 | * hop. | |
c43ed2e4 | 74 | */ |
d62a17ae | 75 | int bgp_bfd_is_peer_multihop(struct peer *peer) |
c43ed2e4 | 76 | { |
d62a17ae | 77 | struct bfd_info *bfd_info; |
986aa00f | 78 | |
d62a17ae | 79 | bfd_info = (struct bfd_info *)peer->bfd_info; |
986aa00f | 80 | |
d62a17ae | 81 | if (!bfd_info) |
82 | return 0; | |
986aa00f | 83 | |
d62a17ae | 84 | if ((bfd_info->type == BFD_TYPE_MULTIHOP) |
85 | || ((peer->sort == BGP_PEER_IBGP) && !peer->shared_network) | |
86 | || is_ebgp_multihop_configured(peer)) | |
87 | return 1; | |
88 | else | |
89 | return 0; | |
c43ed2e4 DS |
90 | } |
91 | ||
92 | /* | |
7f342629 DS |
93 | * bgp_bfd_peer_sendmsg - Format and send a Peer register/Unregister |
94 | * command to Zebra to be forwarded to BFD | |
c43ed2e4 | 95 | */ |
d62a17ae | 96 | static void bgp_bfd_peer_sendmsg(struct peer *peer, int command) |
c43ed2e4 | 97 | { |
d62a17ae | 98 | struct bfd_info *bfd_info; |
9beff0bd | 99 | int multihop, cbit = 0; |
0945d5ed | 100 | vrf_id_t vrf_id; |
d62a17ae | 101 | |
102 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
103 | ||
0945d5ed | 104 | vrf_id = peer->bgp->vrf_id; |
d62a17ae | 105 | |
106 | if (command == ZEBRA_BFD_DEST_DEREGISTER) { | |
107 | multihop = | |
108 | CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_TYPE_MULTIHOP); | |
109 | UNSET_FLAG(bfd_info->flags, BFD_FLAG_BFD_TYPE_MULTIHOP); | |
110 | } else { | |
111 | multihop = bgp_bfd_is_peer_multihop(peer); | |
112 | if ((command == ZEBRA_BFD_DEST_REGISTER) && multihop) | |
113 | SET_FLAG(bfd_info->flags, BFD_FLAG_BFD_TYPE_MULTIHOP); | |
114 | } | |
dcffea69 PG |
115 | /* while graceful restart with fwd path preserved |
116 | * and bfd controlplane check not configured is not kept | |
9beff0bd PG |
117 | * keep bfd independent controlplane bit set to 1 |
118 | */ | |
119 | if (!bgp_flag_check(peer->bgp, BGP_FLAG_GRACEFUL_RESTART) | |
dcffea69 PG |
120 | && !bgp_flag_check(peer->bgp, BGP_FLAG_GR_PRESERVE_FWD) |
121 | && !CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CHECK_CONTROLPLANE)) | |
9beff0bd PG |
122 | SET_FLAG(bfd_info->flags, BFD_FLAG_BFD_CBIT_ON); |
123 | ||
124 | cbit = CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CBIT_ON); | |
d62a17ae | 125 | |
126 | if (peer->su.sa.sa_family == AF_INET) | |
127 | bfd_peer_sendmsg( | |
128 | zclient, bfd_info, AF_INET, &peer->su.sin.sin_addr, | |
129 | (peer->su_local) ? &peer->su_local->sin.sin_addr : NULL, | |
130 | (peer->nexthop.ifp) ? peer->nexthop.ifp->name : NULL, | |
9beff0bd | 131 | peer->ttl, multihop, cbit, command, 1, vrf_id); |
d62a17ae | 132 | else if (peer->su.sa.sa_family == AF_INET6) |
133 | bfd_peer_sendmsg( | |
134 | zclient, bfd_info, AF_INET6, &peer->su.sin6.sin6_addr, | |
135 | (peer->su_local) ? &peer->su_local->sin6.sin6_addr | |
136 | : NULL, | |
137 | (peer->nexthop.ifp) ? peer->nexthop.ifp->name : NULL, | |
9beff0bd | 138 | peer->ttl, multihop, cbit, command, 1, vrf_id); |
c43ed2e4 DS |
139 | } |
140 | ||
141 | /* | |
142 | * bgp_bfd_register_peer - register a peer with BFD through zebra | |
143 | * for monitoring the peer rechahability. | |
144 | */ | |
d62a17ae | 145 | void bgp_bfd_register_peer(struct peer *peer) |
c43ed2e4 | 146 | { |
d62a17ae | 147 | struct bfd_info *bfd_info; |
c43ed2e4 | 148 | |
d62a17ae | 149 | if (!peer->bfd_info) |
150 | return; | |
151 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
c43ed2e4 | 152 | |
d62a17ae | 153 | /* Check if BFD is enabled and peer has already been registered with BFD |
154 | */ | |
155 | if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG)) | |
156 | return; | |
c43ed2e4 | 157 | |
d62a17ae | 158 | bgp_bfd_peer_sendmsg(peer, ZEBRA_BFD_DEST_REGISTER); |
c43ed2e4 DS |
159 | } |
160 | ||
161 | /** | |
162 | * bgp_bfd_deregister_peer - deregister a peer with BFD through zebra | |
163 | * for stopping the monitoring of the peer | |
164 | * rechahability. | |
165 | */ | |
d62a17ae | 166 | void bgp_bfd_deregister_peer(struct peer *peer) |
c43ed2e4 | 167 | { |
d62a17ae | 168 | struct bfd_info *bfd_info; |
c43ed2e4 | 169 | |
d62a17ae | 170 | if (!peer->bfd_info) |
171 | return; | |
172 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
c43ed2e4 | 173 | |
d62a17ae | 174 | /* Check if BFD is eanbled and peer has not been registered */ |
175 | if (!CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG)) | |
176 | return; | |
c43ed2e4 | 177 | |
d62a17ae | 178 | bfd_info->status = BFD_STATUS_DOWN; |
179 | bfd_info->last_update = bgp_clock(); | |
5c940836 | 180 | |
d62a17ae | 181 | bgp_bfd_peer_sendmsg(peer, ZEBRA_BFD_DEST_DEREGISTER); |
c43ed2e4 DS |
182 | } |
183 | ||
184 | /* | |
185 | * bgp_bfd_update_peer - update peer with BFD with new BFD paramters | |
186 | * through zebra. | |
187 | */ | |
d62a17ae | 188 | static void bgp_bfd_update_peer(struct peer *peer) |
c43ed2e4 | 189 | { |
d62a17ae | 190 | struct bfd_info *bfd_info; |
c43ed2e4 | 191 | |
d62a17ae | 192 | if (!peer->bfd_info) |
193 | return; | |
194 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
c43ed2e4 | 195 | |
d62a17ae | 196 | /* Check if the peer has been registered with BFD*/ |
197 | if (!CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG)) | |
198 | return; | |
c43ed2e4 | 199 | |
d62a17ae | 200 | bgp_bfd_peer_sendmsg(peer, ZEBRA_BFD_DEST_UPDATE); |
c43ed2e4 DS |
201 | } |
202 | ||
986aa00f | 203 | /* |
204 | * bgp_bfd_update_type - update session type with BFD through zebra. | |
205 | */ | |
d62a17ae | 206 | static void bgp_bfd_update_type(struct peer *peer) |
986aa00f | 207 | { |
d62a17ae | 208 | struct bfd_info *bfd_info; |
209 | int multihop; | |
210 | ||
211 | if (!peer->bfd_info) | |
212 | return; | |
213 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
214 | ||
215 | /* Check if the peer has been registered with BFD*/ | |
216 | if (!CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG)) | |
217 | return; | |
218 | ||
219 | if (bfd_info->type == BFD_TYPE_NOT_CONFIGURED) { | |
220 | multihop = bgp_bfd_is_peer_multihop(peer); | |
221 | if ((multihop | |
222 | && !CHECK_FLAG(bfd_info->flags, | |
223 | BFD_FLAG_BFD_TYPE_MULTIHOP)) | |
9d303b37 DL |
224 | || (!multihop && CHECK_FLAG(bfd_info->flags, |
225 | BFD_FLAG_BFD_TYPE_MULTIHOP))) { | |
d62a17ae | 226 | bgp_bfd_peer_sendmsg(peer, ZEBRA_BFD_DEST_DEREGISTER); |
227 | bgp_bfd_peer_sendmsg(peer, ZEBRA_BFD_DEST_REGISTER); | |
228 | } | |
229 | } else { | |
230 | if ((bfd_info->type == BFD_TYPE_MULTIHOP | |
231 | && !CHECK_FLAG(bfd_info->flags, | |
232 | BFD_FLAG_BFD_TYPE_MULTIHOP)) | |
233 | || (bfd_info->type == BFD_TYPE_SINGLEHOP | |
234 | && CHECK_FLAG(bfd_info->flags, | |
235 | BFD_FLAG_BFD_TYPE_MULTIHOP))) { | |
236 | bgp_bfd_peer_sendmsg(peer, ZEBRA_BFD_DEST_DEREGISTER); | |
237 | bgp_bfd_peer_sendmsg(peer, ZEBRA_BFD_DEST_REGISTER); | |
238 | } | |
239 | } | |
986aa00f | 240 | } |
241 | ||
c43ed2e4 DS |
242 | /* |
243 | * bgp_bfd_dest_replay - Replay all the peers that have BFD enabled | |
244 | * to zebra | |
245 | */ | |
121f9dee | 246 | static int bgp_bfd_dest_replay(ZAPI_CALLBACK_ARGS) |
c43ed2e4 | 247 | { |
d62a17ae | 248 | struct listnode *mnode, *node, *nnode; |
249 | struct bgp *bgp; | |
250 | struct peer *peer; | |
c43ed2e4 | 251 | |
d62a17ae | 252 | if (BGP_DEBUG(zebra, ZEBRA)) |
253 | zlog_debug("Zebra: BFD Dest replay request"); | |
c43ed2e4 | 254 | |
d62a17ae | 255 | /* Send the client registration */ |
0945d5ed | 256 | bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id); |
055c4dfc | 257 | |
d62a17ae | 258 | /* Replay the peer, if BFD is enabled in BGP */ |
c43ed2e4 | 259 | |
d62a17ae | 260 | for (ALL_LIST_ELEMENTS_RO(bm->bgp, mnode, bgp)) |
261 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
262 | bgp_bfd_update_peer(peer); | |
263 | } | |
c43ed2e4 | 264 | |
d62a17ae | 265 | return 0; |
c43ed2e4 DS |
266 | } |
267 | ||
268 | /* | |
68fe91d6 | 269 | * bgp_bfd_peer_status_update - Update the BFD status if it has changed. Bring |
d62a17ae | 270 | * down the peer if the BFD session went down from |
271 | * * up. | |
68fe91d6 | 272 | */ |
9beff0bd PG |
273 | static void bgp_bfd_peer_status_update(struct peer *peer, int status, |
274 | int remote_cbit) | |
68fe91d6 | 275 | { |
d62a17ae | 276 | struct bfd_info *bfd_info; |
277 | int old_status; | |
68fe91d6 | 278 | |
d62a17ae | 279 | bfd_info = (struct bfd_info *)peer->bfd_info; |
68fe91d6 | 280 | |
d62a17ae | 281 | if (bfd_info->status == status) |
282 | return; | |
68fe91d6 | 283 | |
d62a17ae | 284 | old_status = bfd_info->status; |
285 | bfd_info->status = status; | |
286 | bfd_info->last_update = bgp_clock(); | |
68fe91d6 | 287 | |
37bb7aca PG |
288 | if (status != old_status) { |
289 | if (BGP_DEBUG(neighbor_events, NEIGHBOR_EVENTS)) | |
290 | zlog_debug("[%s]: BFD %s", peer->host, | |
291 | bfd_get_status_str(status)); | |
292 | } | |
d62a17ae | 293 | if ((status == BFD_STATUS_DOWN) && (old_status == BFD_STATUS_UP)) { |
9beff0bd | 294 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE) && |
dcffea69 | 295 | CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CHECK_CONTROLPLANE) && |
9beff0bd PG |
296 | !remote_cbit) { |
297 | zlog_info("%s BFD DOWN message ignored in the process" | |
298 | " of graceful restart when C bit is cleared", | |
299 | peer->host); | |
300 | return; | |
301 | } | |
d62a17ae | 302 | peer->last_reset = PEER_DOWN_BFD_DOWN; |
303 | BGP_EVENT_ADD(peer, BGP_Stop); | |
304 | } | |
51728519 PG |
305 | if ((status == BFD_STATUS_UP) && (old_status == BFD_STATUS_DOWN) |
306 | && peer->status != Established) { | |
fc04a677 PG |
307 | if (!BGP_PEER_START_SUPPRESSED(peer)) { |
308 | bgp_fsm_event_update(peer, 1); | |
51728519 | 309 | BGP_EVENT_ADD(peer, BGP_Start); |
fc04a677 | 310 | } |
51728519 | 311 | } |
68fe91d6 | 312 | } |
313 | ||
314 | /* | |
315 | * bgp_bfd_dest_update - Find the peer for which the BFD status | |
316 | * has changed and bring down the peer | |
317 | * connectivity if the BFD session went down. | |
c43ed2e4 | 318 | */ |
121f9dee | 319 | static int bgp_bfd_dest_update(ZAPI_CALLBACK_ARGS) |
c43ed2e4 | 320 | { |
d62a17ae | 321 | struct interface *ifp; |
322 | struct prefix dp; | |
323 | struct prefix sp; | |
324 | int status; | |
9beff0bd | 325 | int remote_cbit; |
d62a17ae | 326 | |
9beff0bd PG |
327 | ifp = bfd_get_peer_info(zclient->ibuf, &dp, &sp, &status, |
328 | &remote_cbit, vrf_id); | |
d62a17ae | 329 | |
330 | if (BGP_DEBUG(zebra, ZEBRA)) { | |
331 | char buf[2][PREFIX2STR_BUFFER]; | |
332 | prefix2str(&dp, buf[0], sizeof(buf[0])); | |
333 | if (ifp) { | |
334 | zlog_debug( | |
9beff0bd | 335 | "Zebra: vrf %u interface %s bfd destination %s %s %s", |
d62a17ae | 336 | vrf_id, ifp->name, buf[0], |
9beff0bd PG |
337 | bfd_get_status_str(status), |
338 | remote_cbit ? "(cbit on)" : ""); | |
d62a17ae | 339 | } else { |
340 | prefix2str(&sp, buf[1], sizeof(buf[1])); | |
341 | zlog_debug( | |
9beff0bd | 342 | "Zebra: vrf %u source %s bfd destination %s %s %s", |
d62a17ae | 343 | vrf_id, buf[1], buf[0], |
9beff0bd PG |
344 | bfd_get_status_str(status), |
345 | remote_cbit ? "(cbit on)" : ""); | |
d62a17ae | 346 | } |
347 | } | |
348 | ||
349 | /* Bring the peer down if BFD is enabled in BGP */ | |
350 | { | |
351 | struct listnode *mnode, *node, *nnode; | |
352 | struct bgp *bgp; | |
353 | struct peer *peer; | |
354 | ||
355 | for (ALL_LIST_ELEMENTS_RO(bm->bgp, mnode, bgp)) | |
356 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
357 | if (!peer->bfd_info) | |
358 | continue; | |
359 | ||
360 | if ((dp.family == AF_INET) | |
361 | && (peer->su.sa.sa_family == AF_INET)) { | |
362 | if (dp.u.prefix4.s_addr | |
363 | != peer->su.sin.sin_addr.s_addr) | |
364 | continue; | |
365 | } else if ((dp.family == AF_INET6) | |
366 | && (peer->su.sa.sa_family | |
367 | == AF_INET6)) { | |
368 | if (memcmp(&dp.u.prefix6, | |
369 | &peer->su.sin6.sin6_addr, | |
370 | sizeof(struct in6_addr))) | |
371 | continue; | |
372 | } else | |
373 | continue; | |
374 | ||
375 | if (ifp && (ifp == peer->nexthop.ifp)) { | |
376 | bgp_bfd_peer_status_update(peer, | |
9beff0bd PG |
377 | status, |
378 | remote_cbit); | |
d62a17ae | 379 | } else { |
380 | if (!peer->su_local) | |
381 | continue; | |
382 | ||
383 | if ((sp.family == AF_INET) | |
384 | && (peer->su_local->sa.sa_family | |
385 | == AF_INET)) { | |
386 | if (sp.u.prefix4.s_addr | |
387 | != peer->su_local->sin | |
388 | .sin_addr.s_addr) | |
389 | continue; | |
390 | } else if ((sp.family == AF_INET6) | |
391 | && (peer->su_local->sa | |
392 | .sa_family | |
393 | == AF_INET6)) { | |
394 | if (memcmp(&sp.u.prefix6, | |
395 | &peer->su_local->sin6 | |
396 | .sin6_addr, | |
397 | sizeof(struct | |
398 | in6_addr))) | |
399 | continue; | |
400 | } else | |
401 | continue; | |
402 | ||
403 | if ((vrf_id != VRF_DEFAULT) | |
404 | && (peer->bgp->vrf_id != vrf_id)) | |
405 | continue; | |
406 | ||
407 | bgp_bfd_peer_status_update(peer, | |
9beff0bd PG |
408 | status, |
409 | remote_cbit); | |
d62a17ae | 410 | } |
411 | } | |
412 | } | |
413 | ||
414 | return 0; | |
c43ed2e4 DS |
415 | } |
416 | ||
417 | /* | |
418 | * bgp_bfd_peer_param_set - Set the configured BFD paramter values for peer. | |
419 | */ | |
d7c0a89a QY |
420 | static int bgp_bfd_peer_param_set(struct peer *peer, uint32_t min_rx, |
421 | uint32_t min_tx, uint8_t detect_mult, | |
d62a17ae | 422 | int defaults) |
c43ed2e4 | 423 | { |
d62a17ae | 424 | struct peer_group *group; |
425 | struct listnode *node, *nnode; | |
426 | int command = 0; | |
427 | ||
428 | bfd_set_param((struct bfd_info **)&(peer->bfd_info), min_rx, min_tx, | |
429 | detect_mult, defaults, &command); | |
430 | ||
431 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) { | |
432 | group = peer->group; | |
433 | for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) { | |
434 | command = 0; | |
435 | bfd_set_param((struct bfd_info **)&(peer->bfd_info), | |
436 | min_rx, min_tx, detect_mult, defaults, | |
437 | &command); | |
438 | ||
439 | if ((peer->status == Established) | |
440 | && (command == ZEBRA_BFD_DEST_REGISTER)) | |
441 | bgp_bfd_register_peer(peer); | |
442 | else if (command == ZEBRA_BFD_DEST_UPDATE) | |
443 | bgp_bfd_update_peer(peer); | |
444 | } | |
445 | } else { | |
446 | if ((peer->status == Established) | |
447 | && (command == ZEBRA_BFD_DEST_REGISTER)) | |
448 | bgp_bfd_register_peer(peer); | |
449 | else if (command == ZEBRA_BFD_DEST_UPDATE) | |
450 | bgp_bfd_update_peer(peer); | |
451 | } | |
452 | return 0; | |
c43ed2e4 DS |
453 | } |
454 | ||
455 | /* | |
d62a17ae | 456 | * bgp_bfd_peer_param_unset - Delete the configured BFD paramter values for |
457 | * peer. | |
c43ed2e4 | 458 | */ |
d62a17ae | 459 | static int bgp_bfd_peer_param_unset(struct peer *peer) |
c43ed2e4 | 460 | { |
d62a17ae | 461 | struct peer_group *group; |
462 | struct listnode *node, *nnode; | |
463 | ||
464 | if (!peer->bfd_info) | |
465 | return 0; | |
466 | ||
467 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) { | |
468 | bfd_info_free(&(peer->bfd_info)); | |
469 | group = peer->group; | |
470 | for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) { | |
471 | bgp_bfd_deregister_peer(peer); | |
472 | bfd_info_free(&(peer->bfd_info)); | |
473 | } | |
474 | } else { | |
475 | bgp_bfd_deregister_peer(peer); | |
476 | bfd_info_free(&(peer->bfd_info)); | |
477 | } | |
478 | return 0; | |
c43ed2e4 DS |
479 | } |
480 | ||
986aa00f | 481 | /* |
d62a17ae | 482 | * bgp_bfd_peer_param_type_set - set the BFD session type (multihop or |
483 | * singlehop) | |
986aa00f | 484 | */ |
d62a17ae | 485 | static int bgp_bfd_peer_param_type_set(struct peer *peer, |
486 | enum bfd_sess_type type) | |
986aa00f | 487 | { |
d62a17ae | 488 | struct peer_group *group; |
489 | struct listnode *node, *nnode; | |
490 | int command = 0; | |
491 | struct bfd_info *bfd_info; | |
492 | ||
493 | if (!peer->bfd_info) | |
494 | bfd_set_param((struct bfd_info **)&(peer->bfd_info), | |
495 | BFD_DEF_MIN_RX, BFD_DEF_MIN_TX, | |
496 | BFD_DEF_DETECT_MULT, 1, &command); | |
497 | ||
498 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
499 | bfd_info->type = type; | |
500 | ||
501 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) { | |
502 | group = peer->group; | |
503 | for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) { | |
504 | command = 0; | |
505 | if (!peer->bfd_info) | |
506 | bfd_set_param( | |
507 | (struct bfd_info **)&(peer->bfd_info), | |
508 | BFD_DEF_MIN_RX, BFD_DEF_MIN_TX, | |
509 | BFD_DEF_DETECT_MULT, 1, &command); | |
510 | ||
511 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
512 | bfd_info->type = type; | |
513 | ||
514 | if (peer->status == Established) { | |
515 | if (command == ZEBRA_BFD_DEST_REGISTER) | |
516 | bgp_bfd_register_peer(peer); | |
517 | else | |
518 | bgp_bfd_update_type(peer); | |
519 | } | |
520 | } | |
521 | } else { | |
522 | if (peer->status == Established) { | |
523 | if (command == ZEBRA_BFD_DEST_REGISTER) | |
524 | bgp_bfd_register_peer(peer); | |
525 | else | |
526 | bgp_bfd_update_type(peer); | |
527 | } | |
528 | } | |
529 | ||
530 | return 0; | |
986aa00f | 531 | } |
532 | ||
c43ed2e4 DS |
533 | /* |
534 | * bgp_bfd_peer_config_write - Write the peer BFD configuration. | |
535 | */ | |
d62a17ae | 536 | void bgp_bfd_peer_config_write(struct vty *vty, struct peer *peer, char *addr) |
c43ed2e4 | 537 | { |
d62a17ae | 538 | struct bfd_info *bfd_info; |
c43ed2e4 | 539 | |
d62a17ae | 540 | if (!peer->bfd_info) |
541 | return; | |
7f342629 | 542 | |
d62a17ae | 543 | bfd_info = (struct bfd_info *)peer->bfd_info; |
c43ed2e4 | 544 | |
d62a17ae | 545 | if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG)) |
a0841732 RZ |
546 | #if HAVE_BFDD > 0 |
547 | vty_out(vty, " neighbor %s bfd\n", addr); | |
548 | #else | |
d62a17ae | 549 | vty_out(vty, " neighbor %s bfd %d %d %d\n", addr, |
550 | bfd_info->detect_mult, bfd_info->required_min_rx, | |
551 | bfd_info->desired_min_tx); | |
a0841732 | 552 | #endif /* HAVE_BFDD */ |
986aa00f | 553 | |
d62a17ae | 554 | if (bfd_info->type != BFD_TYPE_NOT_CONFIGURED) |
555 | vty_out(vty, " neighbor %s bfd %s\n", addr, | |
556 | (bfd_info->type == BFD_TYPE_MULTIHOP) ? "multihop" | |
557 | : "singlehop"); | |
986aa00f | 558 | |
d62a17ae | 559 | if (!CHECK_FLAG(bfd_info->flags, BFD_FLAG_PARAM_CFG) |
560 | && (bfd_info->type == BFD_TYPE_NOT_CONFIGURED)) | |
561 | vty_out(vty, " neighbor %s bfd\n", addr); | |
dcffea69 PG |
562 | |
563 | if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CHECK_CONTROLPLANE)) | |
564 | vty_out(vty, " neighbor %s bfd check-control-plane-failure\n", addr); | |
c43ed2e4 DS |
565 | } |
566 | ||
567 | /* | |
568 | * bgp_bfd_show_info - Show the peer BFD information. | |
569 | */ | |
9f049418 | 570 | void bgp_bfd_show_info(struct vty *vty, struct peer *peer, bool use_json, |
d62a17ae | 571 | json_object *json_neigh) |
c43ed2e4 | 572 | { |
d62a17ae | 573 | bfd_show_info(vty, (struct bfd_info *)peer->bfd_info, |
574 | bgp_bfd_is_peer_multihop(peer), 0, use_json, json_neigh); | |
c43ed2e4 DS |
575 | } |
576 | ||
577 | DEFUN (neighbor_bfd, | |
578 | neighbor_bfd_cmd, | |
9ccf14f7 | 579 | "neighbor <A.B.C.D|X:X::X:X|WORD> bfd", |
c43ed2e4 DS |
580 | NEIGHBOR_STR |
581 | NEIGHBOR_ADDR_STR2 | |
582 | "Enables BFD support\n") | |
583 | { | |
d62a17ae | 584 | int idx_peer = 1; |
585 | struct peer *peer; | |
586 | int ret; | |
c43ed2e4 | 587 | |
d62a17ae | 588 | peer = peer_and_group_lookup_vty(vty, argv[idx_peer]->arg); |
589 | if (!peer) | |
590 | return CMD_WARNING_CONFIG_FAILED; | |
c43ed2e4 | 591 | |
d62a17ae | 592 | ret = bgp_bfd_peer_param_set(peer, BFD_DEF_MIN_RX, BFD_DEF_MIN_TX, |
593 | BFD_DEF_DETECT_MULT, 1); | |
594 | if (ret != 0) | |
595 | return bgp_vty_return(vty, ret); | |
c43ed2e4 | 596 | |
d62a17ae | 597 | return CMD_SUCCESS; |
c43ed2e4 DS |
598 | } |
599 | ||
64dc4b2d RZ |
600 | #if HAVE_BFDD > 0 |
601 | DEFUN_HIDDEN( | |
602 | #else | |
603 | DEFUN( | |
604 | #endif /* HAVE_BFDD */ | |
605 | neighbor_bfd_param, | |
c43ed2e4 | 606 | neighbor_bfd_param_cmd, |
9ccf14f7 | 607 | "neighbor <A.B.C.D|X:X::X:X|WORD> bfd (2-255) (50-60000) (50-60000)", |
c43ed2e4 DS |
608 | NEIGHBOR_STR |
609 | NEIGHBOR_ADDR_STR2 | |
610 | "Enables BFD support\n" | |
611 | "Detect Multiplier\n" | |
612 | "Required min receive interval\n" | |
613 | "Desired min transmit interval\n") | |
614 | { | |
d62a17ae | 615 | int idx_peer = 1; |
616 | int idx_number_1 = 3; | |
617 | int idx_number_2 = 4; | |
618 | int idx_number_3 = 5; | |
619 | struct peer *peer; | |
d7c0a89a QY |
620 | uint32_t rx_val; |
621 | uint32_t tx_val; | |
622 | uint8_t dm_val; | |
d62a17ae | 623 | int ret; |
624 | ||
625 | peer = peer_and_group_lookup_vty(vty, argv[idx_peer]->arg); | |
626 | if (!peer) | |
627 | return CMD_WARNING_CONFIG_FAILED; | |
628 | ||
629 | if ((ret = bfd_validate_param( | |
630 | vty, argv[idx_number_1]->arg, argv[idx_number_2]->arg, | |
631 | argv[idx_number_3]->arg, &dm_val, &rx_val, &tx_val)) | |
632 | != CMD_SUCCESS) | |
633 | return ret; | |
634 | ||
635 | ret = bgp_bfd_peer_param_set(peer, rx_val, tx_val, dm_val, 0); | |
636 | if (ret != 0) | |
637 | return bgp_vty_return(vty, ret); | |
638 | ||
639 | return CMD_SUCCESS; | |
c43ed2e4 DS |
640 | } |
641 | ||
986aa00f | 642 | DEFUN_HIDDEN (neighbor_bfd_type, |
643 | neighbor_bfd_type_cmd, | |
e83a9414 | 644 | "neighbor <A.B.C.D|X:X::X:X|WORD> bfd <multihop|singlehop>", |
986aa00f | 645 | NEIGHBOR_STR |
646 | NEIGHBOR_ADDR_STR2 | |
647 | "Enables BFD support\n" | |
d7fa34c1 QY |
648 | "Multihop session\n" |
649 | "Single hop session\n") | |
986aa00f | 650 | { |
d62a17ae | 651 | int idx_peer = 1; |
652 | int idx_hop = 3; | |
653 | struct peer *peer; | |
654 | enum bfd_sess_type type; | |
655 | int ret; | |
656 | ||
657 | peer = peer_and_group_lookup_vty(vty, argv[idx_peer]->arg); | |
658 | if (!peer) | |
659 | return CMD_WARNING_CONFIG_FAILED; | |
660 | ||
661 | if (strmatch(argv[idx_hop]->text, "singlehop")) | |
662 | type = BFD_TYPE_SINGLEHOP; | |
663 | else if (strmatch(argv[idx_hop]->text, "multihop")) | |
664 | type = BFD_TYPE_MULTIHOP; | |
665 | else | |
666 | return CMD_WARNING_CONFIG_FAILED; | |
667 | ||
668 | ret = bgp_bfd_peer_param_type_set(peer, type); | |
669 | if (ret != 0) | |
670 | return bgp_vty_return(vty, ret); | |
671 | ||
672 | return CMD_SUCCESS; | |
986aa00f | 673 | } |
674 | ||
dcffea69 PG |
675 | static int bgp_bfd_set_check_controlplane_failure_peer(struct vty *vty, struct peer *peer, |
676 | const char *no) | |
677 | { | |
678 | struct bfd_info *bfd_info; | |
679 | ||
680 | if (!peer->bfd_info) { | |
681 | if (no) | |
682 | return CMD_SUCCESS; | |
683 | vty_out(vty, "%% Specify bfd command first\n"); | |
684 | return CMD_WARNING_CONFIG_FAILED; | |
685 | } | |
686 | bfd_info = (struct bfd_info *)peer->bfd_info; | |
687 | if (!no) { | |
688 | if (!CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CHECK_CONTROLPLANE)) { | |
689 | SET_FLAG(bfd_info->flags, BFD_FLAG_BFD_CHECK_CONTROLPLANE); | |
690 | bgp_bfd_update_peer(peer); | |
691 | } | |
692 | } else { | |
693 | if (CHECK_FLAG(bfd_info->flags, BFD_FLAG_BFD_CHECK_CONTROLPLANE)) { | |
694 | UNSET_FLAG(bfd_info->flags, BFD_FLAG_BFD_CHECK_CONTROLPLANE); | |
695 | bgp_bfd_update_peer(peer); | |
696 | } | |
697 | } | |
698 | return CMD_SUCCESS; | |
699 | } | |
700 | ||
701 | ||
702 | DEFUN (neighbor_bfd_check_controlplane_failure, | |
703 | neighbor_bfd_check_controlplane_failure_cmd, | |
704 | "[no] neighbor <A.B.C.D|X:X::X:X|WORD> bfd check-control-plane-failure", | |
705 | NO_STR | |
706 | NEIGHBOR_STR | |
707 | NEIGHBOR_ADDR_STR2 | |
708 | "BFD support\n" | |
709 | "Link dataplane status with BGP controlplane\n") | |
710 | { | |
711 | const char *no = strmatch(argv[0]->text, "no") ? "no" : NULL; | |
712 | int idx_peer = 0; | |
713 | struct peer *peer; | |
714 | struct peer_group *group; | |
715 | struct listnode *node, *nnode; | |
716 | int ret = CMD_SUCCESS; | |
717 | ||
718 | if (no) | |
719 | idx_peer = 2; | |
720 | else | |
721 | idx_peer = 1; | |
722 | peer = peer_and_group_lookup_vty(vty, argv[idx_peer]->arg); | |
723 | if (!peer) { | |
724 | vty_out(vty, "%% Specify remote-as or peer-group commands first\n"); | |
725 | return CMD_WARNING_CONFIG_FAILED; | |
726 | } | |
727 | if (!peer->bfd_info) { | |
728 | if (no) | |
729 | return CMD_SUCCESS; | |
730 | vty_out(vty, "%% Specify bfd command first\n"); | |
731 | return CMD_WARNING_CONFIG_FAILED; | |
732 | } | |
733 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) { | |
734 | group = peer->group; | |
735 | for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) | |
736 | ret = bgp_bfd_set_check_controlplane_failure_peer(vty, peer, no); | |
737 | } else | |
738 | ret = bgp_bfd_set_check_controlplane_failure_peer(vty, peer, no); | |
739 | return ret; | |
740 | } | |
741 | ||
c43ed2e4 DS |
742 | DEFUN (no_neighbor_bfd, |
743 | no_neighbor_bfd_cmd, | |
64dc4b2d RZ |
744 | #if HAVE_BFDD > 0 |
745 | "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd", | |
746 | #else | |
5bf15956 | 747 | "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd [(2-255) (50-60000) (50-60000)]", |
64dc4b2d | 748 | #endif /* HAVE_BFDD */ |
c43ed2e4 DS |
749 | NO_STR |
750 | NEIGHBOR_STR | |
751 | NEIGHBOR_ADDR_STR2 | |
5bf15956 | 752 | "Disables BFD support\n" |
64dc4b2d | 753 | #if HAVE_BFDD == 0 |
5bf15956 DW |
754 | "Detect Multiplier\n" |
755 | "Required min receive interval\n" | |
64dc4b2d RZ |
756 | "Desired min transmit interval\n" |
757 | #endif /* !HAVE_BFDD */ | |
758 | ) | |
c43ed2e4 | 759 | { |
d62a17ae | 760 | int idx_peer = 2; |
761 | struct peer *peer; | |
762 | int ret; | |
c43ed2e4 | 763 | |
d62a17ae | 764 | peer = peer_and_group_lookup_vty(vty, argv[idx_peer]->arg); |
765 | if (!peer) | |
766 | return CMD_WARNING_CONFIG_FAILED; | |
c43ed2e4 | 767 | |
d62a17ae | 768 | ret = bgp_bfd_peer_param_unset(peer); |
769 | if (ret != 0) | |
770 | return bgp_vty_return(vty, ret); | |
c43ed2e4 | 771 | |
d62a17ae | 772 | return CMD_SUCCESS; |
c43ed2e4 DS |
773 | } |
774 | ||
813d4307 | 775 | |
986aa00f | 776 | DEFUN_HIDDEN (no_neighbor_bfd_type, |
777 | no_neighbor_bfd_type_cmd, | |
e83a9414 | 778 | "no neighbor <A.B.C.D|X:X::X:X|WORD> bfd <multihop|singlehop>", |
986aa00f | 779 | NO_STR |
780 | NEIGHBOR_STR | |
781 | NEIGHBOR_ADDR_STR2 | |
782 | "Disables BFD support\n" | |
3a2d747c QY |
783 | "Multihop session\n" |
784 | "Singlehop session\n") | |
986aa00f | 785 | { |
d62a17ae | 786 | int idx_peer = 2; |
787 | struct peer *peer; | |
788 | int ret; | |
986aa00f | 789 | |
d62a17ae | 790 | peer = peer_and_group_lookup_vty(vty, argv[idx_peer]->arg); |
791 | if (!peer) | |
792 | return CMD_WARNING_CONFIG_FAILED; | |
986aa00f | 793 | |
d62a17ae | 794 | if (!peer->bfd_info) |
795 | return 0; | |
986aa00f | 796 | |
d62a17ae | 797 | ret = bgp_bfd_peer_param_type_set(peer, BFD_TYPE_NOT_CONFIGURED); |
798 | if (ret != 0) | |
799 | return bgp_vty_return(vty, ret); | |
986aa00f | 800 | |
d62a17ae | 801 | return CMD_SUCCESS; |
986aa00f | 802 | } |
803 | ||
d62a17ae | 804 | void bgp_bfd_init(void) |
c43ed2e4 | 805 | { |
d62a17ae | 806 | bfd_gbl_init(); |
807 | ||
808 | /* Initialize BFD client functions */ | |
809 | zclient->interface_bfd_dest_update = bgp_bfd_dest_update; | |
810 | zclient->bfd_dest_replay = bgp_bfd_dest_replay; | |
811 | ||
812 | /* "neighbor bfd" commands. */ | |
813 | install_element(BGP_NODE, &neighbor_bfd_cmd); | |
814 | install_element(BGP_NODE, &neighbor_bfd_param_cmd); | |
815 | install_element(BGP_NODE, &neighbor_bfd_type_cmd); | |
dcffea69 | 816 | install_element(BGP_NODE, &neighbor_bfd_check_controlplane_failure_cmd); |
d62a17ae | 817 | install_element(BGP_NODE, &no_neighbor_bfd_cmd); |
818 | install_element(BGP_NODE, &no_neighbor_bfd_type_cmd); | |
c43ed2e4 | 819 | } |