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
);
65 static bool bfd_session_same(const struct bfd_session
*session
, int family
,
66 const union g_addr
*src
, const union g_addr
*dst
)
68 if (session
->family
!= family
)
71 switch (session
->family
) {
73 if (!IPV4_ADDR_SAME(&session
->dst_ip
.ipv4
, &dst
->ipv4
))
75 if (!IPV4_ADDR_SAME(&session
->src_ip
.ipv4
, &src
->ipv4
))
79 if (!IPV6_ADDR_SAME(&session
->dst_ip
.ipv6
, &dst
->ipv6
))
81 if (!IPV6_ADDR_SAME(&session
->src_ip
.ipv6
, &src
->ipv6
))
85 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address-family: %u",
86 __func__
, session
->family
);
93 static void bfd_adj_event(struct isis_adjacency
*adj
, struct prefix
*dst
,
96 if (!adj
->bfd_session
)
99 if (adj
->bfd_session
->family
!= dst
->family
)
102 switch (adj
->bfd_session
->family
) {
104 if (!IPV4_ADDR_SAME(&adj
->bfd_session
->dst_ip
.ipv4
,
109 if (!IPV6_ADDR_SAME(&adj
->bfd_session
->dst_ip
.ipv6
,
114 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address-family: %u",
115 __func__
, adj
->bfd_session
->family
);
119 int old_status
= adj
->bfd_session
->status
;
121 BFD_SET_CLIENT_STATUS(adj
->bfd_session
->status
, new_status
);
123 if (old_status
== new_status
)
126 if (isis
->debugs
& DEBUG_BFD
) {
127 char dst_str
[INET6_ADDRSTRLEN
];
129 inet_ntop(adj
->bfd_session
->family
, &adj
->bfd_session
->dst_ip
,
130 dst_str
, sizeof(dst_str
));
131 zlog_debug("ISIS-BFD: Peer %s on %s changed from %s to %s",
132 dst_str
, adj
->circuit
->interface
->name
,
133 bfd_get_status_str(old_status
),
134 bfd_get_status_str(new_status
));
137 if (old_status
!= BFD_STATUS_UP
138 || new_status
!= BFD_STATUS_DOWN
) {
142 isis_adj_state_change(adj
, ISIS_ADJ_DOWN
, "bfd session went down");
145 static int isis_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS
)
147 struct interface
*ifp
;
148 struct prefix dst_ip
;
151 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dst_ip
, NULL
, &status
,
153 if (!ifp
|| (dst_ip
.family
!= AF_INET
&& dst_ip
.family
!= AF_INET6
))
156 if (isis
->debugs
& DEBUG_BFD
) {
157 char dst_buf
[INET6_ADDRSTRLEN
];
159 inet_ntop(dst_ip
.family
, &dst_ip
.u
.prefix
, dst_buf
,
162 zlog_debug("ISIS-BFD: Received update for %s on %s: Changed state to %s",
163 dst_buf
, ifp
->name
, bfd_get_status_str(status
));
166 struct isis_circuit
*circuit
= circuit_scan_by_ifp(ifp
);
171 if (circuit
->circ_type
== CIRCUIT_T_BROADCAST
) {
172 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
173 struct list
*adjdb
= circuit
->u
.bc
.adjdb
[level
- 1];
175 struct listnode
*node
, *nnode
;
176 struct isis_adjacency
*adj
;
178 for (ALL_LIST_ELEMENTS(adjdb
, node
, nnode
, adj
))
179 bfd_adj_event(adj
, &dst_ip
, status
);
181 } else if (circuit
->circ_type
== CIRCUIT_T_P2P
) {
182 if (circuit
->u
.p2p
.neighbor
) {
183 bfd_adj_event(circuit
->u
.p2p
.neighbor
,
191 static int isis_bfd_nbr_replay(ZAPI_CALLBACK_ARGS
)
193 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
195 struct listnode
*anode
;
196 struct isis_area
*area
;
198 if (isis
->debugs
& DEBUG_BFD
)
199 zlog_debug("ISIS-BFD: Got neighbor replay request, resending neighbors.");
201 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, anode
, area
)) {
202 struct listnode
*cnode
;
203 struct isis_circuit
*circuit
;
205 for (ALL_LIST_ELEMENTS_RO(area
->circuit_list
, cnode
, circuit
))
206 isis_bfd_circuit_cmd(circuit
, ZEBRA_BFD_DEST_UPDATE
);
209 if (isis
->debugs
& DEBUG_BFD
)
210 zlog_debug("ISIS-BFD: Done with replay.");
215 static void (*orig_zebra_connected
)(struct zclient
*);
216 static void isis_bfd_zebra_connected(struct zclient
*zclient
)
218 if (orig_zebra_connected
)
219 orig_zebra_connected(zclient
);
221 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
224 static void bfd_debug(int family
, union g_addr
*dst
, union g_addr
*src
,
225 const char *interface
, int command
)
227 if (!(isis
->debugs
& DEBUG_BFD
))
230 char dst_str
[INET6_ADDRSTRLEN
];
231 char src_str
[INET6_ADDRSTRLEN
];
233 inet_ntop(family
, dst
, dst_str
, sizeof(dst_str
));
234 inet_ntop(family
, src
, src_str
, sizeof(src_str
));
236 const char *command_str
;
239 case ZEBRA_BFD_DEST_REGISTER
:
240 command_str
= "Register";
242 case ZEBRA_BFD_DEST_DEREGISTER
:
243 command_str
= "Deregister";
245 case ZEBRA_BFD_DEST_UPDATE
:
246 command_str
= "Update";
249 command_str
= "Unknown-Cmd";
253 zlog_debug("ISIS-BFD: %s peer %s on %s (src %s)",
254 command_str
, dst_str
, interface
, src_str
);
257 static void bfd_handle_adj_down(struct isis_adjacency
*adj
)
259 if (!adj
->bfd_session
)
262 bfd_debug(adj
->bfd_session
->family
, &adj
->bfd_session
->dst_ip
,
263 &adj
->bfd_session
->src_ip
, adj
->circuit
->interface
->name
,
264 ZEBRA_BFD_DEST_DEREGISTER
);
266 bfd_peer_sendmsg(zclient
, NULL
, adj
->bfd_session
->family
,
267 &adj
->bfd_session
->dst_ip
,
268 &adj
->bfd_session
->src_ip
,
269 adj
->circuit
->interface
->name
,
272 1, /* control plane independent bit is on */
273 ZEBRA_BFD_DEST_DEREGISTER
,
276 bfd_session_free(&adj
->bfd_session
);
279 static void bfd_handle_adj_up(struct isis_adjacency
*adj
, int command
)
281 struct isis_circuit
*circuit
= adj
->circuit
;
285 struct list
*local_ips
;
286 struct prefix
*local_ip
;
288 if (!circuit
->bfd_info
)
292 * If IS-IS is enabled for both IPv4 and IPv6 on the circuit, prefer
293 * creating a BFD session over IPv6.
295 if (circuit
->ipv6_router
&& adj
->ipv6_address_count
) {
297 dst_ip
.ipv6
= adj
->ipv6_addresses
[0];
298 local_ips
= circuit
->ipv6_link
;
299 if (!local_ips
|| list_isempty(local_ips
))
301 local_ip
= listgetdata(listhead(local_ips
));
302 src_ip
.ipv6
= local_ip
->u
.prefix6
;
303 } else if (circuit
->ip_router
&& adj
->ipv4_address_count
) {
305 dst_ip
.ipv4
= adj
->ipv4_addresses
[0];
306 local_ips
= fabricd_ip_addrs(adj
->circuit
);
307 if (!local_ips
|| list_isempty(local_ips
))
309 local_ip
= listgetdata(listhead(local_ips
));
310 src_ip
.ipv4
= local_ip
->u
.prefix4
;
314 if (adj
->bfd_session
) {
315 if (bfd_session_same(adj
->bfd_session
, family
, &src_ip
,
317 bfd_handle_adj_down(adj
);
320 if (!adj
->bfd_session
)
321 adj
->bfd_session
= bfd_session_new(family
, &dst_ip
, &src_ip
);
323 bfd_debug(adj
->bfd_session
->family
, &adj
->bfd_session
->dst_ip
,
324 &adj
->bfd_session
->src_ip
, circuit
->interface
->name
, command
);
325 bfd_peer_sendmsg(zclient
, circuit
->bfd_info
, adj
->bfd_session
->family
,
326 &adj
->bfd_session
->dst_ip
,
327 &adj
->bfd_session
->src_ip
,
328 circuit
->interface
->name
,
331 1, /* control plane independent bit is on */
337 bfd_handle_adj_down(adj
);
340 static int bfd_handle_adj_state_change(struct isis_adjacency
*adj
)
342 if (adj
->adj_state
== ISIS_ADJ_UP
)
343 bfd_handle_adj_up(adj
, ZEBRA_BFD_DEST_REGISTER
);
345 bfd_handle_adj_down(adj
);
349 static void bfd_adj_cmd(struct isis_adjacency
*adj
, int command
)
351 if (adj
->adj_state
== ISIS_ADJ_UP
352 && command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
353 bfd_handle_adj_up(adj
, command
);
355 bfd_handle_adj_down(adj
);
359 void isis_bfd_circuit_cmd(struct isis_circuit
*circuit
, int command
)
361 switch (circuit
->circ_type
) {
362 case CIRCUIT_T_BROADCAST
:
363 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
364 struct list
*adjdb
= circuit
->u
.bc
.adjdb
[level
- 1];
366 struct listnode
*node
;
367 struct isis_adjacency
*adj
;
369 for (ALL_LIST_ELEMENTS_RO(adjdb
, node
, adj
))
370 bfd_adj_cmd(adj
, command
);
374 if (circuit
->u
.p2p
.neighbor
)
375 bfd_adj_cmd(circuit
->u
.p2p
.neighbor
, command
);
382 void isis_bfd_circuit_param_set(struct isis_circuit
*circuit
,
383 uint32_t min_rx
, uint32_t min_tx
,
384 uint32_t detect_mult
, int defaults
)
388 bfd_set_param(&circuit
->bfd_info
, min_rx
,
389 min_tx
, detect_mult
, defaults
, &command
);
392 isis_bfd_circuit_cmd(circuit
, command
);
395 static int bfd_circuit_write_settings(struct isis_circuit
*circuit
,
398 struct bfd_info
*bfd_info
= circuit
->bfd_info
;
403 vty_out(vty
, " %s bfd\n", PROTO_NAME
);
407 void isis_bfd_init(void)
411 orig_zebra_connected
= zclient
->zebra_connected
;
412 zclient
->zebra_connected
= isis_bfd_zebra_connected
;
413 zclient
->interface_bfd_dest_update
= isis_bfd_interface_dest_update
;
414 zclient
->bfd_dest_replay
= isis_bfd_nbr_replay
;
415 hook_register(isis_adj_state_change_hook
,
416 bfd_handle_adj_state_change
);
417 hook_register(isis_circuit_config_write
,
418 bfd_circuit_write_settings
);