]> git.proxmox.com Git - mirror_frr.git/blob - lib/bfd.c
*: make consistent & update GPLv2 file headers
[mirror_frr.git] / lib / bfd.c
1 /**
2 * bfd.c: 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 *
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
23 #include <zebra.h>
24
25 #include "command.h"
26 #include "memory.h"
27 #include "prefix.h"
28 #include "thread.h"
29 #include "stream.h"
30 #include "zclient.h"
31 #include "table.h"
32 #include "vty.h"
33 #include "bfd.h"
34
35 DEFINE_MTYPE_STATIC(LIB, BFD_INFO, "BFD info")
36
37 int bfd_debug = 0;
38 struct bfd_gbl bfd_gbl;
39
40 /*
41 * bfd_gbl_init - Initialize the BFD global structure
42 */
43 void
44 bfd_gbl_init(void)
45 {
46 memset(&bfd_gbl, 0, sizeof (struct bfd_gbl));
47 }
48
49 /*
50 * bfd_gbl_exit - Called when daemon exits
51 */
52 void
53 bfd_gbl_exit(void)
54 {
55 SET_FLAG (bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN);
56 }
57
58 /*
59 * bfd_info_create - Allocate the BFD information
60 */
61 struct bfd_info *
62 bfd_info_create(void)
63 {
64 struct bfd_info *bfd_info;
65
66 bfd_info = XCALLOC (MTYPE_BFD_INFO, sizeof (struct bfd_info));
67 assert(bfd_info);
68
69 bfd_info->status = BFD_STATUS_UNKNOWN;
70 bfd_info->type = BFD_TYPE_NOT_CONFIGURED;
71 bfd_info->last_update = 0;
72 return bfd_info;
73 }
74
75 /*
76 * bfd_info_free - Free the BFD information.
77 */
78 void
79 bfd_info_free(struct bfd_info **bfd_info)
80 {
81 if (*bfd_info)
82 {
83 XFREE (MTYPE_BFD_INFO, *bfd_info);
84 *bfd_info = NULL;
85 }
86 }
87
88 /*
89 * bfd_validate_param - Validate the BFD paramter information.
90 */
91 int
92 bfd_validate_param(struct vty *vty, const char *dm_str, const char *rx_str,
93 const char *tx_str, u_int8_t *dm_val, u_int32_t *rx_val,
94 u_int32_t *tx_val)
95 {
96 VTY_GET_INTEGER_RANGE ("detect-mul", *dm_val, dm_str,
97 BFD_MIN_DETECT_MULT, BFD_MAX_DETECT_MULT);
98 VTY_GET_INTEGER_RANGE ("min-rx", *rx_val, rx_str,
99 BFD_MIN_MIN_RX, BFD_MAX_MIN_RX);
100 VTY_GET_INTEGER_RANGE ("min-tx", *tx_val, tx_str,
101 BFD_MIN_MIN_TX, BFD_MAX_MIN_TX);
102 return CMD_SUCCESS;
103 }
104
105 /*
106 * bfd_set_param - Set the configured BFD paramter values
107 */
108 void
109 bfd_set_param (struct bfd_info **bfd_info, u_int32_t min_rx, u_int32_t min_tx,
110 u_int8_t detect_mult, int defaults, int *command)
111 {
112 if (!*bfd_info)
113 {
114 *bfd_info = bfd_info_create();
115 *command = ZEBRA_BFD_DEST_REGISTER;
116 }
117 else
118 {
119 if (((*bfd_info)->required_min_rx != min_rx) ||
120 ((*bfd_info)->desired_min_tx != min_tx) ||
121 ((*bfd_info)->detect_mult != detect_mult))
122 *command = ZEBRA_BFD_DEST_UPDATE;
123 }
124
125 if (*command)
126 {
127 (*bfd_info)->required_min_rx = min_rx;
128 (*bfd_info)->desired_min_tx = min_tx;
129 (*bfd_info)->detect_mult = detect_mult;
130 }
131
132 if (!defaults)
133 SET_FLAG ((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
134 else
135 UNSET_FLAG ((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
136 }
137
138 /*
139 * bfd_peer_sendmsg - Format and send a peer register/Unregister
140 * command to Zebra to be forwarded to BFD
141 */
142 void
143 bfd_peer_sendmsg (struct zclient *zclient, struct bfd_info *bfd_info,
144 int family, void *dst_ip, void *src_ip, char *if_name,
145 int ttl, int multihop, int command, int set_flag,
146 vrf_id_t vrf_id)
147 {
148 struct stream *s;
149 int ret;
150 int len;
151
152 /* Individual reg/dereg messages are supressed during shutdown. */
153 if (CHECK_FLAG (bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN))
154 {
155 if (bfd_debug)
156 zlog_debug("%s: Suppressing BFD peer reg/dereg messages", __FUNCTION__);
157 return;
158 }
159
160 /* Check socket. */
161 if (!zclient || zclient->sock < 0)
162 {
163 if (bfd_debug)
164 zlog_debug("%s: Can't send BFD peer register, Zebra client not "
165 "established", __FUNCTION__);
166 return;
167 }
168
169 s = zclient->obuf;
170 stream_reset (s);
171 zclient_create_header (s, command, vrf_id);
172
173 stream_putl(s, getpid());
174
175 stream_putw(s, family);
176 switch (family)
177 {
178 case AF_INET:
179 stream_put_in_addr (s, (struct in_addr *)dst_ip);
180 break;
181 case AF_INET6:
182 stream_put(s, dst_ip, 16);
183 break;
184 default:
185 break;
186 }
187
188 if (command != ZEBRA_BFD_DEST_DEREGISTER)
189 {
190 stream_putl(s, bfd_info->required_min_rx);
191 stream_putl(s, bfd_info->desired_min_tx);
192 stream_putc(s, bfd_info->detect_mult);
193 }
194
195 if (multihop)
196 {
197 stream_putc(s, 1);
198 /* Multi-hop destination send the source IP address to BFD */
199 if (src_ip)
200 {
201 stream_putw(s, family);
202 switch (family)
203 {
204 case AF_INET:
205 stream_put_in_addr (s, (struct in_addr *) src_ip);
206 break;
207 case AF_INET6:
208 stream_put(s, src_ip, 16);
209 break;
210 default:
211 break;
212 }
213 }
214 stream_putc(s, ttl);
215 }
216 else
217 {
218 stream_putc(s, 0);
219 if ((family == AF_INET6) && (src_ip))
220 {
221 stream_putw(s, family);
222 stream_put(s, src_ip, 16);
223 }
224 if (if_name)
225 {
226 len = strlen(if_name);
227 stream_putc(s, len);
228 stream_put(s, if_name, len);
229 }
230 else
231 {
232 stream_putc(s, 0);
233 }
234 }
235
236 stream_putw_at (s, 0, stream_get_endp (s));
237
238 ret = zclient_send_message(zclient);
239
240 if (ret < 0)
241 {
242 if (bfd_debug)
243 zlog_debug("bfd_peer_sendmsg: zclient_send_message() failed");
244 return;
245 }
246
247 if (set_flag)
248 {
249 if (command == ZEBRA_BFD_DEST_REGISTER)
250 SET_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG);
251 else if (command == ZEBRA_BFD_DEST_DEREGISTER)
252 UNSET_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG);
253 }
254
255 return;
256 }
257
258 /*
259 * bfd_get_command_dbg_str - Convert command to a debug string.
260 */
261 const char *
262 bfd_get_command_dbg_str(int command)
263 {
264 switch (command)
265 {
266 case ZEBRA_BFD_DEST_REGISTER:
267 return "Register";
268 case ZEBRA_BFD_DEST_DEREGISTER:
269 return "Deregister";
270 case ZEBRA_BFD_DEST_UPDATE:
271 return "Update";
272 default:
273 return "Unknown";
274 }
275 }
276
277 /*
278 * bfd_get_peer_info - Extract the Peer information for which the BFD session
279 * went down from the message sent from Zebra to clients.
280 */
281 struct interface *
282 bfd_get_peer_info (struct stream *s, struct prefix *dp, struct prefix *sp,
283 int *status, vrf_id_t vrf_id)
284 {
285 unsigned int ifindex;
286 struct interface *ifp = NULL;
287 int plen;
288
289 /* Get interface index. */
290 ifindex = stream_getl (s);
291
292 /* Lookup index. */
293 if (ifindex != 0)
294 {
295 ifp = if_lookup_by_index (ifindex, vrf_id);
296 if (ifp == NULL)
297 {
298 if (bfd_debug)
299 zlog_debug ("zebra_interface_bfd_read: "
300 "Can't find interface by ifindex: %d ", ifindex);
301 return NULL;
302 }
303 }
304
305 /* Fetch destination address. */
306 dp->family = stream_getc (s);
307
308 plen = prefix_blen (dp);
309 stream_get (&dp->u.prefix, s, plen);
310 dp->prefixlen = stream_getc (s);
311
312 /* Get BFD status. */
313 *status = stream_getl (s);
314
315 if (sp)
316 {
317 sp->family = stream_getc (s);
318
319 plen = prefix_blen (sp);
320 stream_get (&sp->u.prefix, s, plen);
321 sp->prefixlen = stream_getc (s);
322 }
323 return ifp;
324 }
325
326 /*
327 * bfd_get_status_str - Convert BFD status to a display string.
328 */
329 const char *
330 bfd_get_status_str(int status)
331 {
332 switch (status)
333 {
334 case BFD_STATUS_DOWN:
335 return "Down";
336 case BFD_STATUS_UP:
337 return "Up";
338 case BFD_STATUS_UNKNOWN:
339 default:
340 return "Unknown";
341 }
342 }
343
344 /*
345 * bfd_last_update - Calculate the last BFD update time and convert it
346 * into a dd:hh:mm:ss display format.
347 */
348 static void
349 bfd_last_update (time_t last_update, char *buf, size_t len)
350 {
351 time_t curr;
352 time_t diff;
353 struct tm *tm;
354 struct timeval tv;
355
356 /* If no BFD satatus update has ever been received, print `never'. */
357 if (last_update == 0)
358 {
359 snprintf (buf, len, "never");
360 return;
361 }
362
363 /* Get current time. */
364 monotime(&tv);
365 curr = tv.tv_sec;
366 diff = curr - last_update;
367 tm = gmtime (&diff);
368
369 snprintf (buf, len, "%d:%02d:%02d:%02d",
370 tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec);
371 }
372
373 /*
374 * bfd_show_param - Show the BFD parameter information.
375 */
376 void
377 bfd_show_param(struct vty *vty, struct bfd_info *bfd_info, int bfd_tag,
378 int extra_space, u_char use_json, json_object *json_obj)
379 {
380 json_object *json_bfd = NULL;
381
382 if (!bfd_info)
383 return;
384
385 if (use_json)
386 {
387 if (bfd_tag)
388 json_bfd = json_object_new_object();
389 else
390 json_bfd = json_obj;
391
392 json_object_int_add(json_bfd, "detectMultiplier", bfd_info->detect_mult);
393 json_object_int_add(json_bfd, "rxMinInterval", bfd_info->required_min_rx);
394 json_object_int_add(json_bfd, "txMinInterval", bfd_info->desired_min_tx);
395 if (bfd_tag)
396 json_object_object_add(json_obj, "peerBfdInfo", json_bfd);
397 }
398 else
399 {
400 vty_out (vty, " %s%sDetect Mul: %d, Min Rx interval: %d,"
401 " Min Tx interval: %d%s",
402 (extra_space) ? " ": "", (bfd_tag) ? "BFD: " : " ",
403 bfd_info->detect_mult, bfd_info->required_min_rx,
404 bfd_info->desired_min_tx, VTY_NEWLINE);
405 }
406 }
407
408 /*
409 * bfd_show_status - Show the BFD status information.
410 */
411 static void
412 bfd_show_status(struct vty *vty, struct bfd_info *bfd_info, int bfd_tag,
413 int extra_space, u_char use_json, json_object *json_bfd)
414 {
415 char time_buf[32];
416
417 if (!bfd_info)
418 return;
419
420 bfd_last_update(bfd_info->last_update, time_buf, 32);
421 if (use_json)
422 {
423 json_object_string_add(json_bfd, "status",
424 bfd_get_status_str(bfd_info->status));
425 json_object_string_add(json_bfd, "lastUpdate", time_buf);
426 }
427 else
428 {
429 vty_out (vty, " %s%sStatus: %s, Last update: %s%s",
430 (extra_space) ? " ": "", (bfd_tag) ? "BFD: " : " ",
431 bfd_get_status_str(bfd_info->status), time_buf, VTY_NEWLINE);
432 }
433 }
434
435 /*
436 * bfd_show_info - Show the BFD information.
437 */
438 void
439 bfd_show_info(struct vty *vty, struct bfd_info *bfd_info, int multihop,
440 int extra_space, u_char use_json, json_object *json_obj)
441 {
442 json_object *json_bfd = NULL;
443
444 if (!bfd_info)
445 return;
446
447 if (use_json)
448 {
449 json_bfd = json_object_new_object();
450 if (multihop)
451 json_object_string_add(json_bfd, "type", "multi hop");
452 else
453 json_object_string_add(json_bfd, "type", "single hop");
454 }
455 else
456 {
457 vty_out (vty, " %sBFD: Type: %s%s", (extra_space) ? " " : "",
458 (multihop) ? "multi hop" : "single hop", VTY_NEWLINE);
459 }
460
461 bfd_show_param(vty, bfd_info, 0, extra_space, use_json, json_bfd);
462 bfd_show_status(vty, bfd_info, 0, extra_space, use_json, json_bfd);
463
464 if (use_json)
465 json_object_object_add(json_obj, "peerBfdInfo", json_bfd);
466 else
467 vty_out (vty, "%s", VTY_NEWLINE);
468 }
469
470 /*
471 * bfd_client_sendmsg - Format and send a client register
472 * command to Zebra to be forwarded to BFD
473 */
474 void
475 bfd_client_sendmsg (struct zclient *zclient, int command)
476 {
477 struct stream *s;
478 int ret;
479
480 /* Check socket. */
481 if (!zclient || zclient->sock < 0)
482 {
483 if (bfd_debug)
484 zlog_debug ("%s: Can't send BFD client register, Zebra client not "
485 "established", __FUNCTION__);
486 return;
487 }
488
489 s = zclient->obuf;
490 stream_reset (s);
491 zclient_create_header (s, command, VRF_DEFAULT);
492
493 stream_putl(s, getpid());
494
495 stream_putw_at (s, 0, stream_get_endp (s));
496
497 ret = zclient_send_message(zclient);
498
499 if (ret < 0)
500 {
501 if (bfd_debug)
502 zlog_debug ("bfd_client_sendmsg %ld: zclient_send_message() failed",
503 (long) getpid());
504 return;
505 }
506
507 return;
508 }