]> git.proxmox.com Git - mirror_frr.git/blob - lib/bfd.c
Merge pull request #7546 from mjstapp/limit_fds
[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 static int bfd_debug = 0;
38 static struct bfd_gbl bfd_gbl;
39
40 /*
41 * bfd_gbl_init - Initialize the BFD global structure
42 */
43 void bfd_gbl_init(void)
44 {
45 memset(&bfd_gbl, 0, sizeof(struct bfd_gbl));
46 }
47
48 /*
49 * bfd_gbl_exit - Called when daemon exits
50 */
51 void bfd_gbl_exit(void)
52 {
53 SET_FLAG(bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN);
54 }
55
56 /*
57 * bfd_info_create - Allocate the BFD information
58 */
59 struct bfd_info *bfd_info_create(void)
60 {
61 struct bfd_info *bfd_info;
62
63 bfd_info = XCALLOC(MTYPE_BFD_INFO, sizeof(struct bfd_info));
64 assert(bfd_info);
65
66 bfd_info->status = BFD_STATUS_UNKNOWN;
67 bfd_info->type = BFD_TYPE_NOT_CONFIGURED;
68 bfd_info->last_update = 0;
69 return bfd_info;
70 }
71
72 /*
73 * bfd_info_free - Free the BFD information.
74 */
75 void bfd_info_free(struct bfd_info **bfd_info)
76 {
77 XFREE(MTYPE_BFD_INFO, *bfd_info);
78 }
79
80 /*
81 * bfd_validate_param - Validate the BFD paramter information.
82 */
83 int bfd_validate_param(struct vty *vty, const char *dm_str, const char *rx_str,
84 const char *tx_str, uint8_t *dm_val, uint32_t *rx_val,
85 uint32_t *tx_val)
86 {
87 *dm_val = strtoul(dm_str, NULL, 10);
88 *rx_val = strtoul(rx_str, NULL, 10);
89 *tx_val = strtoul(tx_str, NULL, 10);
90 return CMD_SUCCESS;
91 }
92
93 /*
94 * bfd_set_param - Set the configured BFD paramter values
95 */
96 void bfd_set_param(struct bfd_info **bfd_info, uint32_t min_rx, uint32_t min_tx,
97 uint8_t detect_mult, const char *profile, int defaults,
98 int *command)
99 {
100 if (!*bfd_info) {
101 *bfd_info = bfd_info_create();
102 *command = ZEBRA_BFD_DEST_REGISTER;
103 } else {
104 if (((*bfd_info)->required_min_rx != min_rx)
105 || ((*bfd_info)->desired_min_tx != min_tx)
106 || ((*bfd_info)->detect_mult != detect_mult)
107 || ((*bfd_info)->profile[0] == 0 && profile)
108 || ((*bfd_info)->profile[0] && profile == NULL)
109 || (profile && (*bfd_info)->profile[0]
110 && strcmp((*bfd_info)->profile, profile)))
111 *command = ZEBRA_BFD_DEST_UPDATE;
112 }
113
114 if (*command) {
115 (*bfd_info)->required_min_rx = min_rx;
116 (*bfd_info)->desired_min_tx = min_tx;
117 (*bfd_info)->detect_mult = detect_mult;
118 if (profile)
119 strlcpy((*bfd_info)->profile, profile,
120 BFD_PROFILE_NAME_LEN);
121 else
122 (*bfd_info)->profile[0] = '\0';
123 }
124
125 if (!defaults)
126 SET_FLAG((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
127 else
128 UNSET_FLAG((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
129 }
130
131 /*
132 * bfd_peer_sendmsg - Format and send a peer register/Unregister
133 * command to Zebra to be forwarded to BFD
134 *
135 * DEPRECATED: use zclient_bfd_command instead
136 */
137 void bfd_peer_sendmsg(struct zclient *zclient, struct bfd_info *bfd_info,
138 int family, void *dst_ip, void *src_ip, char *if_name,
139 int ttl, int multihop, int cbit, int command,
140 int set_flag, vrf_id_t vrf_id)
141 {
142 struct bfd_session_arg args = {};
143 size_t addrlen;
144
145 /* Individual reg/dereg messages are suppressed during shutdown. */
146 if (CHECK_FLAG(bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN)) {
147 if (bfd_debug)
148 zlog_debug(
149 "%s: Suppressing BFD peer reg/dereg messages",
150 __func__);
151 return;
152 }
153
154 /* Check socket. */
155 if (!zclient || zclient->sock < 0) {
156 if (bfd_debug)
157 zlog_debug(
158 "%s: Can't send BFD peer register, Zebra client not established",
159 __func__);
160 return;
161 }
162
163 /* Fill in all arguments. */
164 args.ttl = ttl;
165 args.cbit = cbit;
166 args.family = family;
167 args.mhop = multihop;
168 args.vrf_id = vrf_id;
169 args.command = command;
170 args.set_flag = set_flag;
171 args.bfd_info = bfd_info;
172 if (args.bfd_info) {
173 args.min_rx = bfd_info->required_min_rx;
174 args.min_tx = bfd_info->desired_min_tx;
175 args.detection_multiplier = bfd_info->detect_mult;
176 if (bfd_info->profile[0]) {
177 args.profilelen = strlen(bfd_info->profile);
178 strlcpy(args.profile, bfd_info->profile,
179 sizeof(args.profile));
180 }
181 }
182
183 addrlen = family == AF_INET ? sizeof(struct in_addr)
184 : sizeof(struct in6_addr);
185 memcpy(&args.dst, dst_ip, addrlen);
186 if (src_ip)
187 memcpy(&args.src, src_ip, addrlen);
188
189 if (if_name)
190 args.ifnamelen =
191 strlcpy(args.ifname, if_name, sizeof(args.ifname));
192
193 zclient_bfd_command(zclient, &args);
194 }
195
196 /*
197 * bfd_get_command_dbg_str - Convert command to a debug string.
198 */
199 const char *bfd_get_command_dbg_str(int command)
200 {
201 switch (command) {
202 case ZEBRA_BFD_DEST_REGISTER:
203 return "Register";
204 case ZEBRA_BFD_DEST_DEREGISTER:
205 return "Deregister";
206 case ZEBRA_BFD_DEST_UPDATE:
207 return "Update";
208 default:
209 return "Unknown";
210 }
211 }
212
213 /*
214 * bfd_get_peer_info - Extract the Peer information for which the BFD session
215 * went down from the message sent from Zebra to clients.
216 */
217 struct interface *bfd_get_peer_info(struct stream *s, struct prefix *dp,
218 struct prefix *sp, int *status,
219 int *remote_cbit,
220 vrf_id_t vrf_id)
221 {
222 unsigned int ifindex;
223 struct interface *ifp = NULL;
224 int plen;
225 int local_remote_cbit;
226
227 /* Get interface index. */
228 ifindex = stream_getl(s);
229
230 /* Lookup index. */
231 if (ifindex != 0) {
232 ifp = if_lookup_by_index(ifindex, vrf_id);
233 if (ifp == NULL) {
234 if (bfd_debug)
235 zlog_debug(
236 "zebra_interface_bfd_read: Can't find interface by ifindex: %d ",
237 ifindex);
238 return NULL;
239 }
240 }
241
242 /* Fetch destination address. */
243 dp->family = stream_getc(s);
244
245 plen = prefix_blen(dp);
246 stream_get(&dp->u.prefix, s, plen);
247 dp->prefixlen = stream_getc(s);
248
249 /* Get BFD status. */
250 *status = stream_getl(s);
251
252 if (sp) {
253 sp->family = stream_getc(s);
254
255 plen = prefix_blen(sp);
256 stream_get(&sp->u.prefix, s, plen);
257 sp->prefixlen = stream_getc(s);
258 }
259 local_remote_cbit = stream_getc(s);
260 if (remote_cbit)
261 *remote_cbit = local_remote_cbit;
262 return ifp;
263 }
264
265 /*
266 * bfd_get_status_str - Convert BFD status to a display string.
267 */
268 const char *bfd_get_status_str(int status)
269 {
270 switch (status) {
271 case BFD_STATUS_DOWN:
272 return "Down";
273 case BFD_STATUS_UP:
274 return "Up";
275 case BFD_STATUS_ADMIN_DOWN:
276 return "Admin Down";
277 case BFD_STATUS_UNKNOWN:
278 default:
279 return "Unknown";
280 }
281 }
282
283 /*
284 * bfd_last_update - Calculate the last BFD update time and convert it
285 * into a dd:hh:mm:ss display format.
286 */
287 static void bfd_last_update(time_t last_update, char *buf, size_t len)
288 {
289 time_t curr;
290 time_t diff;
291 struct tm tm;
292 struct timeval tv;
293
294 /* If no BFD satatus update has ever been received, print `never'. */
295 if (last_update == 0) {
296 snprintf(buf, len, "never");
297 return;
298 }
299
300 /* Get current time. */
301 monotime(&tv);
302 curr = tv.tv_sec;
303 diff = curr - last_update;
304 gmtime_r(&diff, &tm);
305
306 snprintf(buf, len, "%d:%02d:%02d:%02d", tm.tm_yday, tm.tm_hour,
307 tm.tm_min, tm.tm_sec);
308 }
309
310 /*
311 * bfd_show_param - Show the BFD parameter information.
312 */
313 void bfd_show_param(struct vty *vty, struct bfd_info *bfd_info, int bfd_tag,
314 int extra_space, bool use_json, json_object *json_obj)
315 {
316 json_object *json_bfd = NULL;
317
318 if (!bfd_info)
319 return;
320
321 if (use_json) {
322 if (bfd_tag)
323 json_bfd = json_object_new_object();
324 else
325 json_bfd = json_obj;
326
327 json_object_int_add(json_bfd, "detectMultiplier",
328 bfd_info->detect_mult);
329 json_object_int_add(json_bfd, "rxMinInterval",
330 bfd_info->required_min_rx);
331 json_object_int_add(json_bfd, "txMinInterval",
332 bfd_info->desired_min_tx);
333 if (bfd_tag)
334 json_object_object_add(json_obj, "peerBfdInfo",
335 json_bfd);
336 } else {
337 vty_out(vty,
338 " %s%sDetect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
339 (extra_space) ? " " : "", (bfd_tag) ? "BFD: " : " ",
340 bfd_info->detect_mult, bfd_info->required_min_rx,
341 bfd_info->desired_min_tx);
342 }
343 }
344
345 /*
346 * bfd_show_status - Show the BFD status information.
347 */
348 static void bfd_show_status(struct vty *vty, struct bfd_info *bfd_info,
349 int bfd_tag, int extra_space, bool use_json,
350 json_object *json_bfd)
351 {
352 char time_buf[32];
353
354 if (!bfd_info)
355 return;
356
357 bfd_last_update(bfd_info->last_update, time_buf, 32);
358 if (use_json) {
359 json_object_string_add(json_bfd, "status",
360 bfd_get_status_str(bfd_info->status));
361 json_object_string_add(json_bfd, "lastUpdate", time_buf);
362 } else {
363 vty_out(vty, " %s%sStatus: %s, Last update: %s\n",
364 (extra_space) ? " " : "", (bfd_tag) ? "BFD: " : " ",
365 bfd_get_status_str(bfd_info->status), time_buf);
366 }
367 }
368
369 /*
370 * bfd_show_info - Show the BFD information.
371 */
372 void bfd_show_info(struct vty *vty, struct bfd_info *bfd_info, int multihop,
373 int extra_space, bool use_json, json_object *json_obj)
374 {
375 json_object *json_bfd = NULL;
376
377 if (!bfd_info)
378 return;
379
380 if (use_json) {
381 json_bfd = json_object_new_object();
382 if (multihop)
383 json_object_string_add(json_bfd, "type", "multi hop");
384 else
385 json_object_string_add(json_bfd, "type", "single hop");
386 } else {
387 vty_out(vty, " %sBFD: Type: %s\n", (extra_space) ? " " : "",
388 (multihop) ? "multi hop" : "single hop");
389 }
390
391 bfd_show_param(vty, bfd_info, 0, extra_space, use_json, json_bfd);
392 bfd_show_status(vty, bfd_info, 0, extra_space, use_json, json_bfd);
393
394 if (use_json)
395 json_object_object_add(json_obj, "peerBfdInfo", json_bfd);
396 else
397 vty_out(vty, "\n");
398 }
399
400 /*
401 * bfd_client_sendmsg - Format and send a client register
402 * command to Zebra to be forwarded to BFD
403 */
404 void bfd_client_sendmsg(struct zclient *zclient, int command,
405 vrf_id_t vrf_id)
406 {
407 struct stream *s;
408 enum zclient_send_status ret;
409
410 /* Check socket. */
411 if (!zclient || zclient->sock < 0) {
412 if (bfd_debug)
413 zlog_debug(
414 "%s: Can't send BFD client register, Zebra client not established",
415 __func__);
416 return;
417 }
418
419 s = zclient->obuf;
420 stream_reset(s);
421 zclient_create_header(s, command, vrf_id);
422
423 stream_putl(s, getpid());
424
425 stream_putw_at(s, 0, stream_get_endp(s));
426
427 ret = zclient_send_message(zclient);
428
429 if (ret == ZCLIENT_SEND_FAILURE) {
430 if (bfd_debug)
431 zlog_debug(
432 "bfd_client_sendmsg %ld: zclient_send_message() failed",
433 (long)getpid());
434 return;
435 }
436
437 return;
438 }
439
440 int zclient_bfd_command(struct zclient *zc, struct bfd_session_arg *args)
441 {
442 struct stream *s;
443 size_t addrlen;
444
445 /* Individual reg/dereg messages are suppressed during shutdown. */
446 if (CHECK_FLAG(bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN)) {
447 if (bfd_debug)
448 zlog_debug(
449 "%s: Suppressing BFD peer reg/dereg messages",
450 __func__);
451 return -1;
452 }
453
454 /* Check socket. */
455 if (!zc || zc->sock < 0) {
456 if (bfd_debug)
457 zlog_debug("%s: zclient unavailable", __func__);
458 return -1;
459 }
460
461 s = zc->obuf;
462 stream_reset(s);
463
464 /* Create new message. */
465 zclient_create_header(s, args->command, args->vrf_id);
466 stream_putl(s, getpid());
467
468 /* Encode destination address. */
469 stream_putw(s, args->family);
470 addrlen = (args->family == AF_INET) ? sizeof(struct in_addr)
471 : sizeof(struct in6_addr);
472 stream_put(s, &args->dst, addrlen);
473
474 /*
475 * For more BFD integration protocol details, see function
476 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
477 */
478 #if HAVE_BFDD > 0
479 /* Session timers. */
480 stream_putl(s, args->min_rx);
481 stream_putl(s, args->min_tx);
482 stream_putc(s, args->detection_multiplier);
483
484 /* Is multi hop? */
485 stream_putc(s, args->mhop != 0);
486
487 /* Source address. */
488 stream_putw(s, args->family);
489 stream_put(s, &args->src, addrlen);
490
491 /* Send the expected TTL. */
492 stream_putc(s, args->ttl);
493
494 /* Send interface name if any. */
495 stream_putc(s, args->ifnamelen);
496 if (args->ifnamelen)
497 stream_put(s, args->ifname, args->ifnamelen);
498
499 /* Send the C bit indicator. */
500 stream_putc(s, args->cbit);
501
502 /* Send profile name if any. */
503 stream_putc(s, args->profilelen);
504 if (args->profilelen)
505 stream_put(s, args->profile, args->profilelen);
506 #else /* PTM BFD */
507 /* Encode timers if this is a registration message. */
508 if (args->command != ZEBRA_BFD_DEST_DEREGISTER) {
509 stream_putl(s, args->min_rx);
510 stream_putl(s, args->min_tx);
511 stream_putc(s, args->detection_multiplier);
512 }
513
514 if (args->mhop) {
515 /* Multi hop indicator. */
516 stream_putc(s, 1);
517
518 /* Multi hop always sends the source address. */
519 stream_putw(s, args->family);
520 stream_put(s, &args->src, addrlen);
521
522 /* Send the expected TTL. */
523 stream_putc(s, args->ttl);
524 } else {
525 /* Multi hop indicator. */
526 stream_putc(s, 0);
527
528 /* Single hop only sends the source address when IPv6. */
529 if (args->family == AF_INET6) {
530 stream_putw(s, args->family);
531 stream_put(s, &args->src, addrlen);
532 }
533
534 /* Send interface name if any. */
535 stream_putc(s, args->ifnamelen);
536 if (args->ifnamelen)
537 stream_put(s, args->ifname, args->ifnamelen);
538 }
539 #endif /* HAVE_BFDD */
540
541 /* Finish the message by writing the size. */
542 stream_putw_at(s, 0, stream_get_endp(s));
543
544 /* Send message to zebra. */
545 if (zclient_send_message(zc) == ZCLIENT_SEND_FAILURE) {
546 if (bfd_debug)
547 zlog_debug("%s: zclient_send_message failed", __func__);
548 return -1;
549 }
550
551 /* Write registration indicator into data structure. */
552 if (args->bfd_info && args->set_flag) {
553 if (args->command == ZEBRA_BFD_DEST_REGISTER)
554 SET_FLAG(args->bfd_info->flags, BFD_FLAG_BFD_REG);
555 else if (args->command == ZEBRA_BFD_DEST_DEREGISTER)
556 UNSET_FLAG(args->bfd_info->flags, BFD_FLAG_BFD_REG);
557 }
558
559 return 0;
560 }