]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_bfd.c
c9d2ea8e21c92c91859d56c67f69a58701819b39
[mirror_frr.git] / isisd / isis_bfd.c
1 /*
2 * IS-IS Rout(e)ing protocol - BFD support
3 *
4 * Copyright (C) 2018 Christian Franke
5 *
6 * This file is part of FreeRangeRouting (FRR)
7 *
8 * FRR 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 * FRR 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 *
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
21 */
22 #include <zebra.h>
23
24 #include "zclient.h"
25 #include "bfd.h"
26
27 #include "isisd/isis_bfd.h"
28 #include "isisd/isis_zebra.h"
29 #include "isisd/isis_common.h"
30 #include "isisd/isis_constants.h"
31 #include "isisd/isis_adjacency.h"
32 #include "isisd/isis_circuit.h"
33 #include "isisd/isisd.h"
34 #include "isisd/fabricd.h"
35
36 DEFINE_MTYPE_STATIC(ISISD, BFD_SESSION, "ISIS BFD Session")
37
38 struct bfd_session {
39 struct in_addr dst_ip;
40 struct in_addr src_ip;
41 int status;
42 };
43
44 static struct bfd_session *bfd_session_new(struct in_addr *dst_ip,
45 struct in_addr *src_ip)
46 {
47 struct bfd_session *rv;
48
49 rv = XCALLOC(MTYPE_BFD_SESSION, sizeof(*rv));
50 rv->dst_ip = *dst_ip;
51 rv->src_ip = *src_ip;
52 return rv;
53 }
54
55 static void bfd_session_free(struct bfd_session **session)
56 {
57 if (!*session)
58 return;
59
60 XFREE(MTYPE_BFD_SESSION, *session);
61 *session = NULL;
62 }
63
64 static void bfd_adj_event(struct isis_adjacency *adj, struct prefix *dst,
65 int new_status)
66 {
67 if (!adj->bfd_session)
68 return;
69
70 if (adj->bfd_session->dst_ip.s_addr != dst->u.prefix4.s_addr)
71 return;
72
73 int old_status = adj->bfd_session->status;
74 adj->bfd_session->status = new_status;
75
76 if (old_status == new_status)
77 return;
78
79 if (isis->debugs & DEBUG_BFD) {
80 char dst_str[INET6_ADDRSTRLEN];
81
82 inet_ntop(AF_INET, &adj->bfd_session->dst_ip,
83 dst_str, sizeof(dst_str));
84 zlog_debug("ISIS-BFD: Peer %s on %s changed from %s to %s",
85 dst_str, adj->circuit->interface->name,
86 bfd_get_status_str(old_status),
87 bfd_get_status_str(new_status));
88 }
89
90 if (old_status != BFD_STATUS_UP
91 || new_status != BFD_STATUS_DOWN) {
92 return;
93 }
94
95 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "bfd session went down");
96 }
97
98 static int isis_bfd_interface_dest_update(int command, struct zclient *zclient,
99 zebra_size_t length, vrf_id_t vrf_id)
100 {
101 struct interface *ifp;
102 struct prefix dst_ip;
103 int status;
104
105 ifp = bfd_get_peer_info(zclient->ibuf, &dst_ip, NULL, &status, vrf_id);
106 if (!ifp || dst_ip.family != AF_INET)
107 return 0;
108
109 if (isis->debugs & DEBUG_BFD) {
110 char dst_buf[INET6_ADDRSTRLEN];
111 inet_ntop(AF_INET, &dst_ip.u.prefix4,
112 dst_buf, sizeof(dst_buf));
113
114 zlog_debug("ISIS-BFD: Received update for %s on %s: Changed state to %s",
115 dst_buf, ifp->name, bfd_get_status_str(status));
116 }
117
118 struct isis_circuit *circuit = circuit_scan_by_ifp(ifp);
119 if (!circuit)
120 return 0;
121
122 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
123 for (int level = ISIS_LEVEL1; level <= ISIS_LEVEL2; level++) {
124 struct list *adjdb = circuit->u.bc.adjdb[level - 1];
125
126 struct listnode *node, *nnode;
127 struct isis_adjacency *adj;
128
129 for (ALL_LIST_ELEMENTS(adjdb, node, nnode, adj))
130 bfd_adj_event(adj, &dst_ip, status);
131 }
132 } else if (circuit->circ_type == CIRCUIT_T_P2P) {
133 if (circuit->u.p2p.neighbor) {
134 bfd_adj_event(circuit->u.p2p.neighbor,
135 &dst_ip, status);
136 }
137 }
138
139 return 0;
140 }
141
142 static int isis_bfd_nbr_replay(int command, struct zclient *zclient,
143 zebra_size_t length, vrf_id_t vrf_id)
144 {
145 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
146
147 struct listnode *anode;
148 struct isis_area *area;
149
150 if (isis->debugs & DEBUG_BFD)
151 zlog_debug("ISIS-BFD: Got neighbor replay request, resending neighbors.");
152
153 for (ALL_LIST_ELEMENTS_RO(isis->area_list, anode, area)) {
154 struct listnode *cnode;
155 struct isis_circuit *circuit;
156
157 for (ALL_LIST_ELEMENTS_RO(area->circuit_list, cnode, circuit))
158 isis_bfd_circuit_cmd(circuit, ZEBRA_BFD_DEST_UPDATE);
159 }
160
161 if (isis->debugs & DEBUG_BFD)
162 zlog_debug("ISIS-BFD: Done with replay.");
163
164 return 0;
165 }
166
167 static void (*orig_zebra_connected)(struct zclient *);
168 static void isis_bfd_zebra_connected(struct zclient *zclient)
169 {
170 if (orig_zebra_connected)
171 orig_zebra_connected(zclient);
172
173 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
174 }
175
176 static void bfd_debug(struct in_addr *dst, struct in_addr *src,
177 const char *interface, int command)
178 {
179 if (!(isis->debugs & DEBUG_BFD))
180 return;
181
182 char dst_str[INET6_ADDRSTRLEN];
183 char src_str[INET6_ADDRSTRLEN];
184
185 inet_ntop(AF_INET, dst, dst_str, sizeof(dst_str));
186 inet_ntop(AF_INET, src, src_str, sizeof(src_str));
187
188 const char *command_str;
189
190 switch (command) {
191 case ZEBRA_BFD_DEST_REGISTER:
192 command_str = "Register";
193 break;
194 case ZEBRA_BFD_DEST_DEREGISTER:
195 command_str = "Deregister";
196 break;
197 case ZEBRA_BFD_DEST_UPDATE:
198 command_str = "Update";
199 break;
200 default:
201 command_str = "Unknown-Cmd";
202 break;
203 }
204
205 zlog_debug("ISIS-BFD: %s peer %s on %s (src %s)",
206 command_str, dst_str, interface, src_str);
207 }
208
209 static void bfd_handle_adj_down(struct isis_adjacency *adj)
210 {
211 if (!adj->bfd_session)
212 return;
213
214 bfd_debug(&adj->bfd_session->dst_ip, &adj->bfd_session->src_ip,
215 adj->circuit->interface->name, ZEBRA_BFD_DEST_DEREGISTER);
216
217 bfd_peer_sendmsg(zclient, NULL, AF_INET,
218 &adj->bfd_session->dst_ip,
219 &adj->bfd_session->src_ip,
220 adj->circuit->interface->name,
221 0, /* ttl */
222 0, /* multihop */
223 ZEBRA_BFD_DEST_DEREGISTER,
224 0, /* set_flag */
225 VRF_DEFAULT);
226 bfd_session_free(&adj->bfd_session);
227 }
228
229 static void bfd_handle_adj_up(struct isis_adjacency *adj, int command)
230 {
231 struct isis_circuit *circuit = adj->circuit;
232
233 if (!circuit->bfd_info
234 || !circuit->ip_router
235 || !adj->ipv4_address_count)
236 goto out;
237
238 struct list *local_ips = fabricd_ip_addrs(adj->circuit);
239 if (!local_ips)
240 goto out;
241
242 struct in_addr *dst_ip = &adj->ipv4_addresses[0];
243 struct prefix_ipv4 *local_ip = listgetdata(listhead(local_ips));
244 struct in_addr *src_ip = &local_ip->prefix;
245
246 if (adj->bfd_session) {
247 if (adj->bfd_session->dst_ip.s_addr != dst_ip->s_addr
248 || adj->bfd_session->src_ip.s_addr != src_ip->s_addr)
249 bfd_handle_adj_down(adj);
250 }
251
252 if (!adj->bfd_session)
253 adj->bfd_session = bfd_session_new(dst_ip, src_ip);
254
255 bfd_debug(&adj->bfd_session->dst_ip, &adj->bfd_session->src_ip,
256 circuit->interface->name, command);
257 bfd_peer_sendmsg(zclient, circuit->bfd_info, AF_INET,
258 &adj->bfd_session->dst_ip,
259 &adj->bfd_session->src_ip,
260 circuit->interface->name,
261 0, /* ttl */
262 0, /* multihop */
263 command,
264 0, /* set flag */
265 VRF_DEFAULT);
266 return;
267 out:
268 bfd_handle_adj_down(adj);
269 }
270
271 static int bfd_handle_adj_state_change(struct isis_adjacency *adj)
272 {
273 if (adj->adj_state == ISIS_ADJ_UP)
274 bfd_handle_adj_up(adj, ZEBRA_BFD_DEST_REGISTER);
275 else
276 bfd_handle_adj_down(adj);
277 return 0;
278 }
279
280 static void bfd_adj_cmd(struct isis_adjacency *adj, int command)
281 {
282 if (adj->adj_state == ISIS_ADJ_UP
283 && command != ZEBRA_BFD_DEST_DEREGISTER) {
284 bfd_handle_adj_up(adj, command);
285 } else {
286 bfd_handle_adj_down(adj);
287 }
288 }
289
290 void isis_bfd_circuit_cmd(struct isis_circuit *circuit, int command)
291 {
292 switch (circuit->circ_type) {
293 case CIRCUIT_T_BROADCAST:
294 for (int level = ISIS_LEVEL1; level <= ISIS_LEVEL2; level++) {
295 struct list *adjdb = circuit->u.bc.adjdb[level - 1];
296
297 struct listnode *node;
298 struct isis_adjacency *adj;
299 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
300 bfd_adj_cmd(adj, command);
301 }
302 break;
303 case CIRCUIT_T_P2P:
304 if (circuit->u.p2p.neighbor)
305 bfd_adj_cmd(circuit->u.p2p.neighbor, command);
306 break;
307 default:
308 break;
309 }
310 }
311
312 void isis_bfd_circuit_param_set(struct isis_circuit *circuit,
313 uint32_t min_rx, uint32_t min_tx,
314 uint32_t detect_mult, int defaults)
315 {
316 int command = 0;
317
318 bfd_set_param(&circuit->bfd_info, min_rx,
319 min_tx, detect_mult, defaults, &command);
320
321 if (command)
322 isis_bfd_circuit_cmd(circuit, command);
323 }
324
325 static int bfd_circuit_write_settings(struct isis_circuit *circuit,
326 struct vty *vty)
327 {
328 struct bfd_info *bfd_info = circuit->bfd_info;
329
330 if (!bfd_info)
331 return 0;
332
333 vty_out(vty, " %s bfd\n", PROTO_NAME);
334 return 1;
335 }
336
337 void isis_bfd_init(void)
338 {
339 bfd_gbl_init();
340
341 orig_zebra_connected = zclient->zebra_connected;
342 zclient->zebra_connected = isis_bfd_zebra_connected;
343 zclient->interface_bfd_dest_update = isis_bfd_interface_dest_update;
344 zclient->bfd_dest_replay = isis_bfd_nbr_replay;
345 hook_register(isis_adj_state_change_hook,
346 bfd_handle_adj_state_change);
347 hook_register(isis_circuit_config_write,
348 bfd_circuit_write_settings);
349 }