2 * IS-IS Rout(e)ing protocol - BFD support
3 * Copyright (C) 2018 Christian Franke
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "lib_errors.h"
26 #include "isisd/isis_bfd.h"
27 #include "isisd/isis_zebra.h"
28 #include "isisd/isis_common.h"
29 #include "isisd/isis_constants.h"
30 #include "isisd/isis_adjacency.h"
31 #include "isisd/isis_circuit.h"
32 #include "isisd/isisd.h"
33 #include "isisd/fabricd.h"
35 DEFINE_MTYPE_STATIC(ISISD
, BFD_SESSION
, "ISIS BFD Session")
44 static struct bfd_session
*bfd_session_new(int family
, union g_addr
*dst_ip
,
47 struct bfd_session
*rv
;
49 rv
= XCALLOC(MTYPE_BFD_SESSION
, sizeof(*rv
));
56 static void bfd_session_free(struct bfd_session
**session
)
61 XFREE(MTYPE_BFD_SESSION
, *session
);
64 static bool bfd_session_same(const struct bfd_session
*session
, int family
,
65 const union g_addr
*src
, const union g_addr
*dst
)
67 if (session
->family
!= family
)
70 switch (session
->family
) {
72 if (!IPV4_ADDR_SAME(&session
->dst_ip
.ipv4
, &dst
->ipv4
))
74 if (!IPV4_ADDR_SAME(&session
->src_ip
.ipv4
, &src
->ipv4
))
78 if (!IPV6_ADDR_SAME(&session
->dst_ip
.ipv6
, &dst
->ipv6
))
80 if (!IPV6_ADDR_SAME(&session
->src_ip
.ipv6
, &src
->ipv6
))
84 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address-family: %u",
85 __func__
, session
->family
);
92 static void bfd_adj_event(struct isis_adjacency
*adj
, struct prefix
*dst
,
95 if (!adj
->bfd_session
)
98 if (adj
->bfd_session
->family
!= dst
->family
)
101 switch (adj
->bfd_session
->family
) {
103 if (!IPV4_ADDR_SAME(&adj
->bfd_session
->dst_ip
.ipv4
,
108 if (!IPV6_ADDR_SAME(&adj
->bfd_session
->dst_ip
.ipv6
,
113 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address-family: %u",
114 __func__
, adj
->bfd_session
->family
);
118 int old_status
= adj
->bfd_session
->status
;
120 BFD_SET_CLIENT_STATUS(adj
->bfd_session
->status
, new_status
);
122 if (old_status
== new_status
)
125 if (isis
->debugs
& DEBUG_BFD
) {
126 char dst_str
[INET6_ADDRSTRLEN
];
128 inet_ntop(adj
->bfd_session
->family
, &adj
->bfd_session
->dst_ip
,
129 dst_str
, sizeof(dst_str
));
130 zlog_debug("ISIS-BFD: Peer %s on %s changed from %s to %s",
131 dst_str
, adj
->circuit
->interface
->name
,
132 bfd_get_status_str(old_status
),
133 bfd_get_status_str(new_status
));
136 if (old_status
!= BFD_STATUS_UP
137 || new_status
!= BFD_STATUS_DOWN
) {
141 isis_adj_state_change(&adj
, ISIS_ADJ_DOWN
, "bfd session went down");
144 static int isis_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS
)
146 struct interface
*ifp
;
147 struct prefix dst_ip
;
150 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dst_ip
, NULL
, &status
,
152 if (!ifp
|| (dst_ip
.family
!= AF_INET
&& dst_ip
.family
!= AF_INET6
))
155 if (isis
->debugs
& DEBUG_BFD
) {
156 char dst_buf
[INET6_ADDRSTRLEN
];
158 inet_ntop(dst_ip
.family
, &dst_ip
.u
.prefix
, dst_buf
,
161 zlog_debug("ISIS-BFD: Received update for %s on %s: Changed state to %s",
162 dst_buf
, ifp
->name
, bfd_get_status_str(status
));
165 struct isis_circuit
*circuit
= circuit_scan_by_ifp(ifp
);
170 if (circuit
->circ_type
== CIRCUIT_T_BROADCAST
) {
171 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
172 struct list
*adjdb
= circuit
->u
.bc
.adjdb
[level
- 1];
174 struct listnode
*node
, *nnode
;
175 struct isis_adjacency
*adj
;
177 for (ALL_LIST_ELEMENTS(adjdb
, node
, nnode
, adj
))
178 bfd_adj_event(adj
, &dst_ip
, status
);
180 } else if (circuit
->circ_type
== CIRCUIT_T_P2P
) {
181 if (circuit
->u
.p2p
.neighbor
) {
182 bfd_adj_event(circuit
->u
.p2p
.neighbor
,
190 static int isis_bfd_nbr_replay(ZAPI_CALLBACK_ARGS
)
192 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
194 struct listnode
*anode
;
195 struct isis_area
*area
;
197 if (isis
->debugs
& DEBUG_BFD
)
198 zlog_debug("ISIS-BFD: Got neighbor replay request, resending neighbors.");
200 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, anode
, area
)) {
201 struct listnode
*cnode
;
202 struct isis_circuit
*circuit
;
204 for (ALL_LIST_ELEMENTS_RO(area
->circuit_list
, cnode
, circuit
))
205 isis_bfd_circuit_cmd(circuit
, ZEBRA_BFD_DEST_UPDATE
);
208 if (isis
->debugs
& DEBUG_BFD
)
209 zlog_debug("ISIS-BFD: Done with replay.");
214 static void (*orig_zebra_connected
)(struct zclient
*);
215 static void isis_bfd_zebra_connected(struct zclient
*zclient
)
217 if (orig_zebra_connected
)
218 orig_zebra_connected(zclient
);
220 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
223 static void bfd_debug(int family
, union g_addr
*dst
, union g_addr
*src
,
224 const char *interface
, int command
)
226 if (!(isis
->debugs
& DEBUG_BFD
))
229 char dst_str
[INET6_ADDRSTRLEN
];
230 char src_str
[INET6_ADDRSTRLEN
];
232 inet_ntop(family
, dst
, dst_str
, sizeof(dst_str
));
233 inet_ntop(family
, src
, src_str
, sizeof(src_str
));
235 const char *command_str
;
238 case ZEBRA_BFD_DEST_REGISTER
:
239 command_str
= "Register";
241 case ZEBRA_BFD_DEST_DEREGISTER
:
242 command_str
= "Deregister";
244 case ZEBRA_BFD_DEST_UPDATE
:
245 command_str
= "Update";
248 command_str
= "Unknown-Cmd";
252 zlog_debug("ISIS-BFD: %s peer %s on %s (src %s)",
253 command_str
, dst_str
, interface
, src_str
);
256 static void bfd_handle_adj_down(struct isis_adjacency
*adj
)
258 if (!adj
->bfd_session
)
261 bfd_debug(adj
->bfd_session
->family
, &adj
->bfd_session
->dst_ip
,
262 &adj
->bfd_session
->src_ip
, adj
->circuit
->interface
->name
,
263 ZEBRA_BFD_DEST_DEREGISTER
);
265 bfd_peer_sendmsg(zclient
, NULL
, adj
->bfd_session
->family
,
266 &adj
->bfd_session
->dst_ip
,
267 &adj
->bfd_session
->src_ip
,
268 adj
->circuit
->interface
->name
,
271 1, /* control plane independent bit is on */
272 ZEBRA_BFD_DEST_DEREGISTER
,
275 bfd_session_free(&adj
->bfd_session
);
278 static void bfd_handle_adj_up(struct isis_adjacency
*adj
, int command
)
280 struct isis_circuit
*circuit
= adj
->circuit
;
284 struct list
*local_ips
;
285 struct prefix
*local_ip
;
287 if (!circuit
->bfd_info
)
291 * If IS-IS is enabled for both IPv4 and IPv6 on the circuit, prefer
292 * creating a BFD session over IPv6.
294 if (circuit
->ipv6_router
&& adj
->ipv6_address_count
) {
296 dst_ip
.ipv6
= adj
->ipv6_addresses
[0];
297 local_ips
= circuit
->ipv6_link
;
298 if (!local_ips
|| list_isempty(local_ips
))
300 local_ip
= listgetdata(listhead(local_ips
));
301 src_ip
.ipv6
= local_ip
->u
.prefix6
;
302 } else if (circuit
->ip_router
&& adj
->ipv4_address_count
) {
304 dst_ip
.ipv4
= adj
->ipv4_addresses
[0];
305 local_ips
= fabricd_ip_addrs(adj
->circuit
);
306 if (!local_ips
|| list_isempty(local_ips
))
308 local_ip
= listgetdata(listhead(local_ips
));
309 src_ip
.ipv4
= local_ip
->u
.prefix4
;
313 if (adj
->bfd_session
) {
314 if (bfd_session_same(adj
->bfd_session
, family
, &src_ip
,
316 bfd_handle_adj_down(adj
);
319 if (!adj
->bfd_session
)
320 adj
->bfd_session
= bfd_session_new(family
, &dst_ip
, &src_ip
);
322 bfd_debug(adj
->bfd_session
->family
, &adj
->bfd_session
->dst_ip
,
323 &adj
->bfd_session
->src_ip
, circuit
->interface
->name
, command
);
324 bfd_peer_sendmsg(zclient
, circuit
->bfd_info
, adj
->bfd_session
->family
,
325 &adj
->bfd_session
->dst_ip
,
326 &adj
->bfd_session
->src_ip
,
327 circuit
->interface
->name
,
330 1, /* control plane independent bit is on */
336 bfd_handle_adj_down(adj
);
339 static int bfd_handle_adj_state_change(struct isis_adjacency
*adj
)
341 if (adj
->adj_state
== ISIS_ADJ_UP
)
342 bfd_handle_adj_up(adj
, ZEBRA_BFD_DEST_REGISTER
);
344 bfd_handle_adj_down(adj
);
348 static void bfd_adj_cmd(struct isis_adjacency
*adj
, int command
)
350 if (adj
->adj_state
== ISIS_ADJ_UP
351 && command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
352 bfd_handle_adj_up(adj
, command
);
354 bfd_handle_adj_down(adj
);
358 void isis_bfd_circuit_cmd(struct isis_circuit
*circuit
, int command
)
360 switch (circuit
->circ_type
) {
361 case CIRCUIT_T_BROADCAST
:
362 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
363 struct list
*adjdb
= circuit
->u
.bc
.adjdb
[level
- 1];
365 struct listnode
*node
;
366 struct isis_adjacency
*adj
;
368 for (ALL_LIST_ELEMENTS_RO(adjdb
, node
, adj
))
369 bfd_adj_cmd(adj
, command
);
373 if (circuit
->u
.p2p
.neighbor
)
374 bfd_adj_cmd(circuit
->u
.p2p
.neighbor
, command
);
381 void isis_bfd_circuit_param_set(struct isis_circuit
*circuit
,
382 uint32_t min_rx
, uint32_t min_tx
,
383 uint32_t detect_mult
, int defaults
)
387 bfd_set_param(&circuit
->bfd_info
, min_rx
,
388 min_tx
, detect_mult
, defaults
, &command
);
391 isis_bfd_circuit_cmd(circuit
, command
);
394 static int bfd_circuit_write_settings(struct isis_circuit
*circuit
,
397 struct bfd_info
*bfd_info
= circuit
->bfd_info
;
402 vty_out(vty
, " %s bfd\n", PROTO_NAME
);
406 void isis_bfd_init(void)
410 orig_zebra_connected
= zclient
->zebra_connected
;
411 zclient
->zebra_connected
= isis_bfd_zebra_connected
;
412 zclient
->interface_bfd_dest_update
= isis_bfd_interface_dest_update
;
413 zclient
->bfd_dest_replay
= isis_bfd_nbr_replay
;
414 hook_register(isis_adj_state_change_hook
,
415 bfd_handle_adj_state_change
);
416 hook_register(isis_circuit_config_write
,
417 bfd_circuit_write_settings
);