]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_bfd.c
tests: align BFD profiles tests for IS-IS
[mirror_frr.git] / isisd / isis_bfd.c
CommitLineData
52df8228
CF
1/*
2 * IS-IS Rout(e)ing protocol - BFD support
52df8228
CF
3 * Copyright (C) 2018 Christian Franke
4 *
d04f9fbf
CF
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)
8 * any later version.
52df8228 9 *
d04f9fbf
CF
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
13 * more details.
52df8228
CF
14 *
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
18 */
19#include <zebra.h>
20
21#include "zclient.h"
e782cca7 22#include "nexthop.h"
52df8228 23#include "bfd.h"
e782cca7 24#include "lib_errors.h"
52df8228
CF
25
26#include "isisd/isis_bfd.h"
27#include "isisd/isis_zebra.h"
215eccb0
CF
28#include "isisd/isis_common.h"
29#include "isisd/isis_constants.h"
30#include "isisd/isis_adjacency.h"
31#include "isisd/isis_circuit.h"
3015e3d1 32#include "isisd/isisd.h"
215eccb0 33#include "isisd/fabricd.h"
52df8228 34
20a42f01
CF
35DEFINE_MTYPE_STATIC(ISISD, BFD_SESSION, "ISIS BFD Session")
36
37struct bfd_session {
e782cca7
RW
38 int family;
39 union g_addr dst_ip;
40 union g_addr src_ip;
a5eba4e9 41 int status;
20a42f01
CF
42};
43
e782cca7
RW
44static struct bfd_session *bfd_session_new(int family, union g_addr *dst_ip,
45 union g_addr *src_ip)
20a42f01
CF
46{
47 struct bfd_session *rv;
48
a5eba4e9 49 rv = XCALLOC(MTYPE_BFD_SESSION, sizeof(*rv));
e782cca7 50 rv->family = family;
20a42f01
CF
51 rv->dst_ip = *dst_ip;
52 rv->src_ip = *src_ip;
53 return rv;
54}
55
56static void bfd_session_free(struct bfd_session **session)
57{
58 if (!*session)
59 return;
60
61 XFREE(MTYPE_BFD_SESSION, *session);
20a42f01
CF
62}
63
e782cca7
RW
64static bool bfd_session_same(const struct bfd_session *session, int family,
65 const union g_addr *src, const union g_addr *dst)
66{
67 if (session->family != family)
68 return false;
69
70 switch (session->family) {
71 case AF_INET:
72 if (!IPV4_ADDR_SAME(&session->dst_ip.ipv4, &dst->ipv4))
73 return false;
74 if (!IPV4_ADDR_SAME(&session->src_ip.ipv4, &src->ipv4))
75 return false;
76 break;
77 case AF_INET6:
78 if (!IPV6_ADDR_SAME(&session->dst_ip.ipv6, &dst->ipv6))
79 return false;
80 if (!IPV6_ADDR_SAME(&session->src_ip.ipv6, &src->ipv6))
81 return false;
82 break;
83 default:
84 flog_err(EC_LIB_DEVELOPMENT, "%s: unknown address-family: %u",
85 __func__, session->family);
86 exit(1);
87 }
88
89 return true;
90}
91
a5eba4e9
CF
92static void bfd_adj_event(struct isis_adjacency *adj, struct prefix *dst,
93 int new_status)
94{
95 if (!adj->bfd_session)
96 return;
97
e782cca7 98 if (adj->bfd_session->family != dst->family)
a5eba4e9
CF
99 return;
100
e782cca7
RW
101 switch (adj->bfd_session->family) {
102 case AF_INET:
103 if (!IPV4_ADDR_SAME(&adj->bfd_session->dst_ip.ipv4,
104 &dst->u.prefix4))
105 return;
106 break;
107 case AF_INET6:
108 if (!IPV6_ADDR_SAME(&adj->bfd_session->dst_ip.ipv6,
109 &dst->u.prefix6))
110 return;
111 break;
112 default:
113 flog_err(EC_LIB_DEVELOPMENT, "%s: unknown address-family: %u",
114 __func__, adj->bfd_session->family);
115 exit(1);
116 }
117
a5eba4e9 118 int old_status = adj->bfd_session->status;
a5eba4e9 119
7555dc61
S
120 BFD_SET_CLIENT_STATUS(adj->bfd_session->status, new_status);
121
a5eba4e9
CF
122 if (old_status == new_status)
123 return;
124
e740f9c1 125 if (IS_DEBUG_BFD) {
a5eba4e9
CF
126 char dst_str[INET6_ADDRSTRLEN];
127
e782cca7 128 inet_ntop(adj->bfd_session->family, &adj->bfd_session->dst_ip,
a5eba4e9
CF
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));
134 }
135
136 if (old_status != BFD_STATUS_UP
137 || new_status != BFD_STATUS_DOWN) {
138 return;
139 }
140
690497fb
G
141 adj->circuit->area->bfd_signalled_down = true;
142
16167b31 143 isis_adj_state_change(&adj, ISIS_ADJ_DOWN, "bfd session went down");
a5eba4e9
CF
144}
145
121f9dee 146static int isis_bfd_interface_dest_update(ZAPI_CALLBACK_ARGS)
52df8228 147{
2815f817
CF
148 struct interface *ifp;
149 struct prefix dst_ip;
150 int status;
151
9beff0bd
PG
152 ifp = bfd_get_peer_info(zclient->ibuf, &dst_ip, NULL, &status,
153 NULL, vrf_id);
e782cca7 154 if (!ifp || (dst_ip.family != AF_INET && dst_ip.family != AF_INET6))
2815f817
CF
155 return 0;
156
e740f9c1 157 if (IS_DEBUG_BFD) {
2815f817 158 char dst_buf[INET6_ADDRSTRLEN];
5489eb45 159
e782cca7
RW
160 inet_ntop(dst_ip.family, &dst_ip.u.prefix, dst_buf,
161 sizeof(dst_buf));
2815f817
CF
162
163 zlog_debug("ISIS-BFD: Received update for %s on %s: Changed state to %s",
164 dst_buf, ifp->name, bfd_get_status_str(status));
165 }
166
a5eba4e9 167 struct isis_circuit *circuit = circuit_scan_by_ifp(ifp);
5489eb45 168
a5eba4e9
CF
169 if (!circuit)
170 return 0;
171
172 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
173 for (int level = ISIS_LEVEL1; level <= ISIS_LEVEL2; level++) {
174 struct list *adjdb = circuit->u.bc.adjdb[level - 1];
175
176 struct listnode *node, *nnode;
177 struct isis_adjacency *adj;
178
179 for (ALL_LIST_ELEMENTS(adjdb, node, nnode, adj))
180 bfd_adj_event(adj, &dst_ip, status);
181 }
182 } else if (circuit->circ_type == CIRCUIT_T_P2P) {
183 if (circuit->u.p2p.neighbor) {
184 bfd_adj_event(circuit->u.p2p.neighbor,
185 &dst_ip, status);
186 }
187 }
188
52df8228
CF
189 return 0;
190}
191
121f9dee 192static int isis_bfd_nbr_replay(ZAPI_CALLBACK_ARGS)
52df8228 193{
0945d5ed 194 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
ab880eaf
CF
195
196 struct listnode *anode;
197 struct isis_area *area;
198
e740f9c1 199 if (IS_DEBUG_BFD)
2815f817
CF
200 zlog_debug("ISIS-BFD: Got neighbor replay request, resending neighbors.");
201
ab880eaf
CF
202 for (ALL_LIST_ELEMENTS_RO(isis->area_list, anode, area)) {
203 struct listnode *cnode;
204 struct isis_circuit *circuit;
205
206 for (ALL_LIST_ELEMENTS_RO(area->circuit_list, cnode, circuit))
207 isis_bfd_circuit_cmd(circuit, ZEBRA_BFD_DEST_UPDATE);
208 }
209
e740f9c1 210 if (IS_DEBUG_BFD)
2815f817
CF
211 zlog_debug("ISIS-BFD: Done with replay.");
212
52df8228
CF
213 return 0;
214}
215
216static void (*orig_zebra_connected)(struct zclient *);
217static void isis_bfd_zebra_connected(struct zclient *zclient)
218{
219 if (orig_zebra_connected)
220 orig_zebra_connected(zclient);
221
0945d5ed 222 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
52df8228
CF
223}
224
e782cca7 225static void bfd_debug(int family, union g_addr *dst, union g_addr *src,
2815f817
CF
226 const char *interface, int command)
227{
e740f9c1 228 if (!(IS_DEBUG_BFD))
2815f817
CF
229 return;
230
231 char dst_str[INET6_ADDRSTRLEN];
232 char src_str[INET6_ADDRSTRLEN];
233
e782cca7
RW
234 inet_ntop(family, dst, dst_str, sizeof(dst_str));
235 inet_ntop(family, src, src_str, sizeof(src_str));
2815f817
CF
236
237 const char *command_str;
238
239 switch (command) {
240 case ZEBRA_BFD_DEST_REGISTER:
241 command_str = "Register";
242 break;
243 case ZEBRA_BFD_DEST_DEREGISTER:
244 command_str = "Deregister";
245 break;
246 case ZEBRA_BFD_DEST_UPDATE:
247 command_str = "Update";
248 break;
249 default:
250 command_str = "Unknown-Cmd";
251 break;
252 }
253
254 zlog_debug("ISIS-BFD: %s peer %s on %s (src %s)",
255 command_str, dst_str, interface, src_str);
256}
257
20a42f01
CF
258static void bfd_handle_adj_down(struct isis_adjacency *adj)
259{
260 if (!adj->bfd_session)
261 return;
262
e782cca7
RW
263 bfd_debug(adj->bfd_session->family, &adj->bfd_session->dst_ip,
264 &adj->bfd_session->src_ip, adj->circuit->interface->name,
265 ZEBRA_BFD_DEST_DEREGISTER);
2815f817 266
e782cca7 267 bfd_peer_sendmsg(zclient, NULL, adj->bfd_session->family,
c2afd32f
RZ
268 &adj->bfd_session->dst_ip, &adj->bfd_session->src_ip,
269 (adj->circuit->interface)
270 ? adj->circuit->interface->name
271 : NULL,
20a42f01
CF
272 0, /* ttl */
273 0, /* multihop */
9beff0bd 274 1, /* control plane independent bit is on */
20a42f01
CF
275 ZEBRA_BFD_DEST_DEREGISTER,
276 0, /* set_flag */
277 VRF_DEFAULT);
278 bfd_session_free(&adj->bfd_session);
279}
280
281static void bfd_handle_adj_up(struct isis_adjacency *adj, int command)
215eccb0 282{
20a42f01 283 struct isis_circuit *circuit = adj->circuit;
e782cca7
RW
284 int family;
285 union g_addr dst_ip;
286 union g_addr src_ip;
287 struct list *local_ips;
288 struct prefix *local_ip;
20a42f01 289
e782cca7 290 if (!circuit->bfd_info)
20a42f01
CF
291 goto out;
292
e782cca7
RW
293 /*
294 * If IS-IS is enabled for both IPv4 and IPv6 on the circuit, prefer
295 * creating a BFD session over IPv6.
296 */
297 if (circuit->ipv6_router && adj->ipv6_address_count) {
298 family = AF_INET6;
299 dst_ip.ipv6 = adj->ipv6_addresses[0];
300 local_ips = circuit->ipv6_link;
301 if (!local_ips || list_isempty(local_ips))
302 goto out;
303 local_ip = listgetdata(listhead(local_ips));
304 src_ip.ipv6 = local_ip->u.prefix6;
305 } else if (circuit->ip_router && adj->ipv4_address_count) {
306 family = AF_INET;
307 dst_ip.ipv4 = adj->ipv4_addresses[0];
308 local_ips = fabricd_ip_addrs(adj->circuit);
309 if (!local_ips || list_isempty(local_ips))
310 goto out;
311 local_ip = listgetdata(listhead(local_ips));
312 src_ip.ipv4 = local_ip->u.prefix4;
313 } else
20a42f01
CF
314 goto out;
315
20a42f01 316 if (adj->bfd_session) {
e782cca7
RW
317 if (bfd_session_same(adj->bfd_session, family, &src_ip,
318 &dst_ip))
20a42f01
CF
319 bfd_handle_adj_down(adj);
320 }
321
322 if (!adj->bfd_session)
e782cca7 323 adj->bfd_session = bfd_session_new(family, &dst_ip, &src_ip);
20a42f01 324
e782cca7
RW
325 bfd_debug(adj->bfd_session->family, &adj->bfd_session->dst_ip,
326 &adj->bfd_session->src_ip, circuit->interface->name, command);
327 bfd_peer_sendmsg(zclient, circuit->bfd_info, adj->bfd_session->family,
20a42f01
CF
328 &adj->bfd_session->dst_ip,
329 &adj->bfd_session->src_ip,
c2afd32f
RZ
330 (adj->circuit->interface)
331 ? adj->circuit->interface->name
332 : NULL,
20a42f01
CF
333 0, /* ttl */
334 0, /* multihop */
9beff0bd 335 1, /* control plane independent bit is on */
20a42f01
CF
336 command,
337 0, /* set flag */
338 VRF_DEFAULT);
215eccb0 339 return;
20a42f01
CF
340out:
341 bfd_handle_adj_down(adj);
342}
343
344static int bfd_handle_adj_state_change(struct isis_adjacency *adj)
345{
346 if (adj->adj_state == ISIS_ADJ_UP)
347 bfd_handle_adj_up(adj, ZEBRA_BFD_DEST_REGISTER);
348 else
349 bfd_handle_adj_down(adj);
350 return 0;
351}
352
353static void bfd_adj_cmd(struct isis_adjacency *adj, int command)
354{
355 if (adj->adj_state == ISIS_ADJ_UP
356 && command != ZEBRA_BFD_DEST_DEREGISTER) {
357 bfd_handle_adj_up(adj, command);
358 } else {
359 bfd_handle_adj_down(adj);
360 }
361}
362
363void isis_bfd_circuit_cmd(struct isis_circuit *circuit, int command)
364{
365 switch (circuit->circ_type) {
366 case CIRCUIT_T_BROADCAST:
367 for (int level = ISIS_LEVEL1; level <= ISIS_LEVEL2; level++) {
368 struct list *adjdb = circuit->u.bc.adjdb[level - 1];
369
370 struct listnode *node;
371 struct isis_adjacency *adj;
5489eb45 372
20a42f01
CF
373 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
374 bfd_adj_cmd(adj, command);
375 }
376 break;
377 case CIRCUIT_T_P2P:
378 if (circuit->u.p2p.neighbor)
379 bfd_adj_cmd(circuit->u.p2p.neighbor, command);
380 break;
381 default:
382 break;
383 }
215eccb0
CF
384}
385
4affdba7
G
386void isis_bfd_circuit_param_set(struct isis_circuit *circuit, uint32_t min_rx,
387 uint32_t min_tx, uint32_t detect_mult,
388 const char *profile, int defaults)
215eccb0
CF
389{
390 int command = 0;
391
4affdba7
G
392 bfd_set_param(&circuit->bfd_info, min_rx, min_tx, detect_mult, profile,
393 defaults, &command);
215eccb0
CF
394
395 if (command)
396 isis_bfd_circuit_cmd(circuit, command);
397}
398
05e4ec37 399#ifdef FABRICD
3015e3d1
CF
400static int bfd_circuit_write_settings(struct isis_circuit *circuit,
401 struct vty *vty)
402{
403 struct bfd_info *bfd_info = circuit->bfd_info;
404
405 if (!bfd_info)
406 return 0;
407
490a6fc7 408 vty_out(vty, " %s bfd\n", PROTO_NAME);
3015e3d1
CF
409 return 1;
410}
05e4ec37 411#endif
3015e3d1 412
52df8228
CF
413void isis_bfd_init(void)
414{
415 bfd_gbl_init();
416
417 orig_zebra_connected = zclient->zebra_connected;
418 zclient->zebra_connected = isis_bfd_zebra_connected;
419 zclient->interface_bfd_dest_update = isis_bfd_interface_dest_update;
420 zclient->bfd_dest_replay = isis_bfd_nbr_replay;
20a42f01
CF
421 hook_register(isis_adj_state_change_hook,
422 bfd_handle_adj_state_change);
05e4ec37 423#ifdef FABRICD
3015e3d1
CF
424 hook_register(isis_circuit_config_write,
425 bfd_circuit_write_settings);
05e4ec37 426#endif
52df8228 427}