]> git.proxmox.com Git - mirror_frr.git/blob - lib/bfd.c
Merge pull request #9900 from SaiGomathiN/sai-pimjoin
[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 "vrf.h"
31 #include "zclient.h"
32 #include "table.h"
33 #include "vty.h"
34 #include "bfd.h"
35
36 DEFINE_MTYPE_STATIC(LIB, BFD_INFO, "BFD info");
37
38 /**
39 * BFD protocol integration configuration.
40 */
41
42 /** Events definitions. */
43 enum bfd_session_event {
44 /** Remove the BFD session configuration. */
45 BSE_UNINSTALL,
46 /** Install the BFD session configuration. */
47 BSE_INSTALL,
48 };
49
50 /**
51 * Data structure to do the necessary tricks to hide the BFD protocol
52 * integration internals.
53 */
54 struct bfd_session_params {
55 /** Contains the session parameters and more. */
56 struct bfd_session_arg args;
57 /** Contains the session state. */
58 struct bfd_session_status bss;
59 /** Protocol implementation status update callback. */
60 bsp_status_update updatecb;
61 /** Protocol implementation custom data pointer. */
62 void *arg;
63
64 /**
65 * Next event.
66 *
67 * This variable controls what action to execute when the command batch
68 * finishes. Normally we'd use `thread_add_event` value, however since
69 * that function is going to be called multiple times and the value
70 * might be different we'll use this variable to keep track of it.
71 */
72 enum bfd_session_event lastev;
73 /**
74 * BFD session configuration event.
75 *
76 * Multiple actions might be asked during a command batch (either via
77 * configuration load or northbound batch), so we'll use this to
78 * install/uninstall the BFD session parameters only once.
79 */
80 struct thread *installev;
81
82 /** BFD session installation state. */
83 bool installed;
84
85 /** Global BFD paramaters list. */
86 TAILQ_ENTRY(bfd_session_params) entry;
87 };
88
89 struct bfd_sessions_global {
90 /**
91 * Global BFD session parameters list for (re)installation and update
92 * without code duplication among daemons.
93 */
94 TAILQ_HEAD(bsplist, bfd_session_params) bsplist;
95
96 /** Pointer to FRR's event manager. */
97 struct thread_master *tm;
98 /** Pointer to zebra client data structure. */
99 struct zclient *zc;
100
101 /** Debugging state. */
102 bool debugging;
103 /** Is shutting down? */
104 bool shutting_down;
105 };
106
107 /** Global configuration variable. */
108 static struct bfd_sessions_global bsglobal;
109
110 /** Global empty address for IPv4/IPv6. */
111 static const struct in6_addr i6a_zero;
112
113 /*
114 * bfd_get_peer_info - Extract the Peer information for which the BFD session
115 * went down from the message sent from Zebra to clients.
116 */
117 static struct interface *bfd_get_peer_info(struct stream *s, struct prefix *dp,
118 struct prefix *sp, int *status,
119 int *remote_cbit, vrf_id_t vrf_id)
120 {
121 unsigned int ifindex;
122 struct interface *ifp = NULL;
123 int plen;
124 int local_remote_cbit;
125
126 /*
127 * If the ifindex lookup fails the
128 * rest of the data in the stream is
129 * not read. All examples of this function
130 * call immediately use the dp->family which
131 * is not good. Ensure we are not using
132 * random data
133 */
134 memset(dp, 0, sizeof(*dp));
135 memset(sp, 0, sizeof(*sp));
136
137 /* Get interface index. */
138 STREAM_GETL(s, ifindex);
139
140 /* Lookup index. */
141 if (ifindex != 0) {
142 ifp = if_lookup_by_index(ifindex, vrf_id);
143 if (ifp == NULL) {
144 if (bsglobal.debugging)
145 zlog_debug(
146 "zebra_interface_bfd_read: Can't find interface by ifindex: %d ",
147 ifindex);
148 return NULL;
149 }
150 }
151
152 /* Fetch destination address. */
153 STREAM_GETC(s, dp->family);
154
155 plen = prefix_blen(dp);
156 STREAM_GET(&dp->u.prefix, s, plen);
157 STREAM_GETC(s, dp->prefixlen);
158
159 /* Get BFD status. */
160 STREAM_GETL(s, (*status));
161
162 STREAM_GETC(s, sp->family);
163
164 plen = prefix_blen(sp);
165 STREAM_GET(&sp->u.prefix, s, plen);
166 STREAM_GETC(s, sp->prefixlen);
167
168 STREAM_GETC(s, local_remote_cbit);
169 if (remote_cbit)
170 *remote_cbit = local_remote_cbit;
171 return ifp;
172
173 stream_failure:
174 return NULL;
175 }
176
177 /*
178 * bfd_get_status_str - Convert BFD status to a display string.
179 */
180 const char *bfd_get_status_str(int status)
181 {
182 switch (status) {
183 case BFD_STATUS_DOWN:
184 return "Down";
185 case BFD_STATUS_UP:
186 return "Up";
187 case BFD_STATUS_ADMIN_DOWN:
188 return "Admin Down";
189 case BFD_STATUS_UNKNOWN:
190 default:
191 return "Unknown";
192 }
193 }
194
195 /*
196 * bfd_last_update - Calculate the last BFD update time and convert it
197 * into a dd:hh:mm:ss display format.
198 */
199 static void bfd_last_update(time_t last_update, char *buf, size_t len)
200 {
201 time_t curr;
202 time_t diff;
203 struct tm tm;
204 struct timeval tv;
205
206 /* If no BFD status update has ever been received, print `never'. */
207 if (last_update == 0) {
208 snprintf(buf, len, "never");
209 return;
210 }
211
212 /* Get current time. */
213 monotime(&tv);
214 curr = tv.tv_sec;
215 diff = curr - last_update;
216 gmtime_r(&diff, &tm);
217
218 snprintf(buf, len, "%d:%02d:%02d:%02d", tm.tm_yday, tm.tm_hour,
219 tm.tm_min, tm.tm_sec);
220 }
221
222 /*
223 * bfd_client_sendmsg - Format and send a client register
224 * command to Zebra to be forwarded to BFD
225 */
226 void bfd_client_sendmsg(struct zclient *zclient, int command,
227 vrf_id_t vrf_id)
228 {
229 struct stream *s;
230 enum zclient_send_status ret;
231
232 /* Check socket. */
233 if (!zclient || zclient->sock < 0) {
234 if (bsglobal.debugging)
235 zlog_debug(
236 "%s: Can't send BFD client register, Zebra client not established",
237 __func__);
238 return;
239 }
240
241 s = zclient->obuf;
242 stream_reset(s);
243 zclient_create_header(s, command, vrf_id);
244
245 stream_putl(s, getpid());
246
247 stream_putw_at(s, 0, stream_get_endp(s));
248
249 ret = zclient_send_message(zclient);
250
251 if (ret == ZCLIENT_SEND_FAILURE) {
252 if (bsglobal.debugging)
253 zlog_debug(
254 "bfd_client_sendmsg %ld: zclient_send_message() failed",
255 (long)getpid());
256 return;
257 }
258
259 return;
260 }
261
262 int zclient_bfd_command(struct zclient *zc, struct bfd_session_arg *args)
263 {
264 struct stream *s;
265 size_t addrlen;
266
267 /* Individual reg/dereg messages are suppressed during shutdown. */
268 if (bsglobal.shutting_down) {
269 if (bsglobal.debugging)
270 zlog_debug(
271 "%s: Suppressing BFD peer reg/dereg messages",
272 __func__);
273 return -1;
274 }
275
276 /* Check socket. */
277 if (!zc || zc->sock < 0) {
278 if (bsglobal.debugging)
279 zlog_debug("%s: zclient unavailable", __func__);
280 return -1;
281 }
282
283 s = zc->obuf;
284 stream_reset(s);
285
286 /* Create new message. */
287 zclient_create_header(s, args->command, args->vrf_id);
288 stream_putl(s, getpid());
289
290 /* Encode destination address. */
291 stream_putw(s, args->family);
292 addrlen = (args->family == AF_INET) ? sizeof(struct in_addr)
293 : sizeof(struct in6_addr);
294 stream_put(s, &args->dst, addrlen);
295
296 /*
297 * For more BFD integration protocol details, see function
298 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
299 */
300 #if HAVE_BFDD > 0
301 /* Session timers. */
302 stream_putl(s, args->min_rx);
303 stream_putl(s, args->min_tx);
304 stream_putc(s, args->detection_multiplier);
305
306 /* Is multi hop? */
307 stream_putc(s, args->mhop != 0);
308
309 /* Source address. */
310 stream_putw(s, args->family);
311 stream_put(s, &args->src, addrlen);
312
313 /* Send the expected TTL. */
314 stream_putc(s, args->ttl);
315
316 /* Send interface name if any. */
317 if (args->mhop) {
318 /* Don't send interface. */
319 stream_putc(s, 0);
320 if (bsglobal.debugging && args->ifnamelen)
321 zlog_debug("%s: multi hop is configured, not sending interface",
322 __func__);
323 } else {
324 stream_putc(s, args->ifnamelen);
325 if (args->ifnamelen)
326 stream_put(s, args->ifname, args->ifnamelen);
327 }
328
329 /* Send the C bit indicator. */
330 stream_putc(s, args->cbit);
331
332 /* Send profile name if any. */
333 stream_putc(s, args->profilelen);
334 if (args->profilelen)
335 stream_put(s, args->profile, args->profilelen);
336 #else /* PTM BFD */
337 /* Encode timers if this is a registration message. */
338 if (args->command != ZEBRA_BFD_DEST_DEREGISTER) {
339 stream_putl(s, args->min_rx);
340 stream_putl(s, args->min_tx);
341 stream_putc(s, args->detection_multiplier);
342 }
343
344 if (args->mhop) {
345 /* Multi hop indicator. */
346 stream_putc(s, 1);
347
348 /* Multi hop always sends the source address. */
349 stream_putw(s, args->family);
350 stream_put(s, &args->src, addrlen);
351
352 /* Send the expected TTL. */
353 stream_putc(s, args->ttl);
354 } else {
355 /* Multi hop indicator. */
356 stream_putc(s, 0);
357
358 /* Single hop only sends the source address when IPv6. */
359 if (args->family == AF_INET6) {
360 stream_putw(s, args->family);
361 stream_put(s, &args->src, addrlen);
362 }
363
364 /* Send interface name if any. */
365 stream_putc(s, args->ifnamelen);
366 if (args->ifnamelen)
367 stream_put(s, args->ifname, args->ifnamelen);
368 }
369
370 /* Send the C bit indicator. */
371 stream_putc(s, args->cbit);
372 #endif /* HAVE_BFDD */
373
374 /* Finish the message by writing the size. */
375 stream_putw_at(s, 0, stream_get_endp(s));
376
377 /* Send message to zebra. */
378 if (zclient_send_message(zc) == ZCLIENT_SEND_FAILURE) {
379 if (bsglobal.debugging)
380 zlog_debug("%s: zclient_send_message failed", __func__);
381 return -1;
382 }
383
384 return 0;
385 }
386
387 struct bfd_session_params *bfd_sess_new(bsp_status_update updatecb, void *arg)
388 {
389 struct bfd_session_params *bsp;
390
391 bsp = XCALLOC(MTYPE_BFD_INFO, sizeof(*bsp));
392
393 /* Save application data. */
394 bsp->updatecb = updatecb;
395 bsp->arg = arg;
396
397 /* Set defaults. */
398 bsp->args.detection_multiplier = BFD_DEF_DETECT_MULT;
399 bsp->args.ttl = 1;
400 bsp->args.min_rx = BFD_DEF_MIN_RX;
401 bsp->args.min_tx = BFD_DEF_MIN_TX;
402 bsp->args.vrf_id = VRF_DEFAULT;
403
404 /* Register in global list. */
405 TAILQ_INSERT_TAIL(&bsglobal.bsplist, bsp, entry);
406
407 return bsp;
408 }
409
410 static bool _bfd_sess_valid(const struct bfd_session_params *bsp)
411 {
412 /* Peer/local address not configured. */
413 if (bsp->args.family == 0)
414 return false;
415
416 /* Address configured but invalid. */
417 if (bsp->args.family != AF_INET && bsp->args.family != AF_INET6) {
418 if (bsglobal.debugging)
419 zlog_debug("%s: invalid session family: %d", __func__,
420 bsp->args.family);
421 return false;
422 }
423
424 /* Invalid address. */
425 if (memcmp(&bsp->args.dst, &i6a_zero, sizeof(i6a_zero)) == 0) {
426 if (bsglobal.debugging) {
427 if (bsp->args.family == AF_INET)
428 zlog_debug("%s: invalid address: %pI4",
429 __func__,
430 (struct in_addr *)&bsp->args.dst);
431 else
432 zlog_debug("%s: invalid address: %pI6",
433 __func__, &bsp->args.dst);
434 }
435 return false;
436 }
437
438 /* Multi hop requires local address. */
439 if (bsp->args.mhop
440 && memcmp(&i6a_zero, &bsp->args.src, sizeof(i6a_zero)) == 0) {
441 if (bsglobal.debugging)
442 zlog_debug(
443 "%s: multi hop but no local address provided",
444 __func__);
445 return false;
446 }
447
448 /* Check VRF ID. */
449 if (bsp->args.vrf_id == VRF_UNKNOWN) {
450 if (bsglobal.debugging)
451 zlog_debug("%s: asked for unknown VRF", __func__);
452 return false;
453 }
454
455 return true;
456 }
457
458 static int _bfd_sess_send(struct thread *t)
459 {
460 struct bfd_session_params *bsp = THREAD_ARG(t);
461 int rv;
462
463 /* Validate configuration before trying to send bogus data. */
464 if (!_bfd_sess_valid(bsp))
465 return 0;
466
467 if (bsp->lastev == BSE_INSTALL) {
468 bsp->args.command = bsp->installed ? ZEBRA_BFD_DEST_UPDATE
469 : ZEBRA_BFD_DEST_REGISTER;
470 } else
471 bsp->args.command = ZEBRA_BFD_DEST_DEREGISTER;
472
473 /* If not installed and asked for uninstall, do nothing. */
474 if (!bsp->installed && bsp->args.command == ZEBRA_BFD_DEST_DEREGISTER)
475 return 0;
476
477 rv = zclient_bfd_command(bsglobal.zc, &bsp->args);
478 /* Command was sent successfully. */
479 if (rv == 0) {
480 /* Update installation status. */
481 if (bsp->args.command == ZEBRA_BFD_DEST_DEREGISTER)
482 bsp->installed = false;
483 else if (bsp->args.command == ZEBRA_BFD_DEST_REGISTER)
484 bsp->installed = true;
485 } else {
486 struct ipaddr src, dst;
487
488 src.ipa_type = bsp->args.family;
489 src.ipaddr_v6 = bsp->args.src;
490 dst.ipa_type = bsp->args.family;
491 dst.ipaddr_v6 = bsp->args.dst;
492
493 zlog_err(
494 "%s: BFD session %pIA -> %pIA interface %s VRF %s(%u) was not %s",
495 __func__, &src, &dst,
496 bsp->args.ifnamelen ? bsp->args.ifname : "*",
497 vrf_id_to_name(bsp->args.vrf_id), bsp->args.vrf_id,
498 bsp->lastev == BSE_INSTALL ? "installed"
499 : "uninstalled");
500 }
501
502 return 0;
503 }
504
505 static void _bfd_sess_remove(struct bfd_session_params *bsp)
506 {
507 /* Not installed, nothing to do. */
508 if (!bsp->installed)
509 return;
510
511 /* Cancel any pending installation request. */
512 THREAD_OFF(bsp->installev);
513
514 /* Send request to remove any session. */
515 bsp->lastev = BSE_UNINSTALL;
516 thread_execute(bsglobal.tm, _bfd_sess_send, bsp, 0);
517 }
518
519 void bfd_sess_free(struct bfd_session_params **bsp)
520 {
521 if (*bsp == NULL)
522 return;
523
524 /* Remove any installed session. */
525 _bfd_sess_remove(*bsp);
526
527 /* Remove from global list. */
528 TAILQ_REMOVE(&bsglobal.bsplist, (*bsp), entry);
529
530 /* Free the memory and point to NULL. */
531 XFREE(MTYPE_BFD_INFO, (*bsp));
532 }
533
534 static bool bfd_sess_address_changed(const struct bfd_session_params *bsp,
535 uint32_t family,
536 const struct in6_addr *src,
537 const struct in6_addr *dst)
538 {
539 size_t addrlen;
540
541 if (bsp->args.family != family)
542 return true;
543
544 addrlen = (family == AF_INET) ? sizeof(struct in_addr)
545 : sizeof(struct in6_addr);
546 if ((src == NULL && memcmp(&bsp->args.src, &i6a_zero, addrlen))
547 || (src && memcmp(src, &bsp->args.src, addrlen))
548 || memcmp(dst, &bsp->args.dst, addrlen))
549 return true;
550
551 return false;
552 }
553
554 void bfd_sess_set_ipv4_addrs(struct bfd_session_params *bsp,
555 struct in_addr *src, struct in_addr *dst)
556 {
557 if (!bfd_sess_address_changed(bsp, AF_INET, (struct in6_addr *)src,
558 (struct in6_addr *)dst))
559 return;
560
561 /* If already installed, remove the old setting. */
562 _bfd_sess_remove(bsp);
563
564 bsp->args.family = AF_INET;
565
566 /* Clean memory, set zero value and avoid static analyser warnings. */
567 memset(&bsp->args.src, 0, sizeof(bsp->args.src));
568 memset(&bsp->args.dst, 0, sizeof(bsp->args.dst));
569
570 /* Copy the equivalent of IPv4 to arguments structure. */
571 if (src)
572 memcpy(&bsp->args.src, src, sizeof(struct in_addr));
573
574 assert(dst);
575 memcpy(&bsp->args.dst, dst, sizeof(struct in_addr));
576 }
577
578 void bfd_sess_set_ipv6_addrs(struct bfd_session_params *bsp,
579 struct in6_addr *src, struct in6_addr *dst)
580 {
581 if (!bfd_sess_address_changed(bsp, AF_INET, (struct in6_addr *)src,
582 (struct in6_addr *)dst))
583 return;
584
585 /* If already installed, remove the old setting. */
586 _bfd_sess_remove(bsp);
587
588 bsp->args.family = AF_INET6;
589
590 /* Clean memory, set zero value and avoid static analyser warnings. */
591 memset(&bsp->args.src, 0, sizeof(bsp->args.src));
592
593 if (src)
594 bsp->args.src = *src;
595
596 assert(dst);
597 bsp->args.dst = *dst;
598 }
599
600 void bfd_sess_set_interface(struct bfd_session_params *bsp, const char *ifname)
601 {
602 if ((ifname == NULL && bsp->args.ifnamelen == 0)
603 || (ifname && strcmp(bsp->args.ifname, ifname) == 0))
604 return;
605
606 /* If already installed, remove the old setting. */
607 _bfd_sess_remove(bsp);
608
609 if (ifname == NULL) {
610 bsp->args.ifname[0] = 0;
611 bsp->args.ifnamelen = 0;
612 return;
613 }
614
615 if (strlcpy(bsp->args.ifname, ifname, sizeof(bsp->args.ifname))
616 > sizeof(bsp->args.ifname))
617 zlog_warn("%s: interface name truncated: %s", __func__, ifname);
618
619 bsp->args.ifnamelen = strlen(bsp->args.ifname);
620 }
621
622 void bfd_sess_set_profile(struct bfd_session_params *bsp, const char *profile)
623 {
624 if (profile == NULL) {
625 bsp->args.profile[0] = 0;
626 bsp->args.profilelen = 0;
627 return;
628 }
629
630 if (strlcpy(bsp->args.profile, profile, sizeof(bsp->args.profile))
631 > sizeof(bsp->args.profile))
632 zlog_warn("%s: profile name truncated: %s", __func__, profile);
633
634 bsp->args.profilelen = strlen(bsp->args.profile);
635 }
636
637 void bfd_sess_set_vrf(struct bfd_session_params *bsp, vrf_id_t vrf_id)
638 {
639 if (bsp->args.vrf_id == vrf_id)
640 return;
641
642 /* If already installed, remove the old setting. */
643 _bfd_sess_remove(bsp);
644
645 bsp->args.vrf_id = vrf_id;
646 }
647
648 void bfd_sess_set_mininum_ttl(struct bfd_session_params *bsp, uint8_t min_ttl)
649 {
650 assert(min_ttl != 0);
651
652 if (bsp->args.ttl == ((BFD_SINGLE_HOP_TTL + 1) - min_ttl))
653 return;
654
655 /* If already installed, remove the old setting. */
656 _bfd_sess_remove(bsp);
657
658 /* Invert TTL value: protocol expects number of hops. */
659 min_ttl = (BFD_SINGLE_HOP_TTL + 1) - min_ttl;
660 bsp->args.ttl = min_ttl;
661 bsp->args.mhop = (min_ttl > 1);
662 }
663
664 void bfd_sess_set_hop_count(struct bfd_session_params *bsp, uint8_t min_ttl)
665 {
666 if (bsp->args.ttl == min_ttl)
667 return;
668
669 /* If already installed, remove the old setting. */
670 _bfd_sess_remove(bsp);
671
672 bsp->args.ttl = min_ttl;
673 bsp->args.mhop = (min_ttl > 1);
674 }
675
676
677 void bfd_sess_set_cbit(struct bfd_session_params *bsp, bool enable)
678 {
679 bsp->args.cbit = enable;
680 }
681
682 void bfd_sess_set_timers(struct bfd_session_params *bsp,
683 uint8_t detection_multiplier, uint32_t min_rx,
684 uint32_t min_tx)
685 {
686 bsp->args.detection_multiplier = detection_multiplier;
687 bsp->args.min_rx = min_rx;
688 bsp->args.min_tx = min_tx;
689 }
690
691 void bfd_sess_install(struct bfd_session_params *bsp)
692 {
693 bsp->lastev = BSE_INSTALL;
694 thread_add_event(bsglobal.tm, _bfd_sess_send, bsp, 0, &bsp->installev);
695 }
696
697 void bfd_sess_uninstall(struct bfd_session_params *bsp)
698 {
699 bsp->lastev = BSE_UNINSTALL;
700 thread_add_event(bsglobal.tm, _bfd_sess_send, bsp, 0, &bsp->installev);
701 }
702
703 enum bfd_session_state bfd_sess_status(const struct bfd_session_params *bsp)
704 {
705 return bsp->bss.state;
706 }
707
708 uint8_t bfd_sess_minimum_ttl(const struct bfd_session_params *bsp)
709 {
710 return ((BFD_SINGLE_HOP_TTL + 1) - bsp->args.ttl);
711 }
712
713 uint8_t bfd_sess_hop_count(const struct bfd_session_params *bsp)
714 {
715 return bsp->args.ttl;
716 }
717
718 const char *bfd_sess_profile(const struct bfd_session_params *bsp)
719 {
720 return bsp->args.profilelen ? bsp->args.profile : NULL;
721 }
722
723 void bfd_sess_addresses(const struct bfd_session_params *bsp, int *family,
724 struct in6_addr *src, struct in6_addr *dst)
725 {
726 *family = bsp->args.family;
727 if (src)
728 *src = bsp->args.src;
729 if (dst)
730 *dst = bsp->args.dst;
731 }
732
733 const char *bfd_sess_interface(const struct bfd_session_params *bsp)
734 {
735 if (bsp->args.ifnamelen)
736 return bsp->args.ifname;
737
738 return NULL;
739 }
740
741 const char *bfd_sess_vrf(const struct bfd_session_params *bsp)
742 {
743 return vrf_id_to_name(bsp->args.vrf_id);
744 }
745
746 vrf_id_t bfd_sess_vrf_id(const struct bfd_session_params *bsp)
747 {
748 return bsp->args.vrf_id;
749 }
750
751 bool bfd_sess_cbit(const struct bfd_session_params *bsp)
752 {
753 return bsp->args.cbit;
754 }
755
756 void bfd_sess_timers(const struct bfd_session_params *bsp,
757 uint8_t *detection_multiplier, uint32_t *min_rx,
758 uint32_t *min_tx)
759 {
760 *detection_multiplier = bsp->args.detection_multiplier;
761 *min_rx = bsp->args.min_rx;
762 *min_tx = bsp->args.min_tx;
763 }
764
765 void bfd_sess_show(struct vty *vty, struct json_object *json,
766 struct bfd_session_params *bsp)
767 {
768 json_object *json_bfd = NULL;
769 char time_buf[64];
770
771 if (!bsp)
772 return;
773
774 /* Show type. */
775 if (json) {
776 json_bfd = json_object_new_object();
777 if (bsp->args.mhop)
778 json_object_string_add(json_bfd, "type", "multi hop");
779 else
780 json_object_string_add(json_bfd, "type", "single hop");
781 } else
782 vty_out(vty, " BFD: Type: %s\n",
783 bsp->args.mhop ? "multi hop" : "single hop");
784
785 /* Show configuration. */
786 if (json) {
787 json_object_int_add(json_bfd, "detectMultiplier",
788 bsp->args.detection_multiplier);
789 json_object_int_add(json_bfd, "rxMinInterval",
790 bsp->args.min_rx);
791 json_object_int_add(json_bfd, "txMinInterval",
792 bsp->args.min_tx);
793 } else {
794 vty_out(vty,
795 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
796 bsp->args.detection_multiplier, bsp->args.min_rx,
797 bsp->args.min_tx);
798 }
799
800 bfd_last_update(bsp->bss.last_event, time_buf, sizeof(time_buf));
801 if (json) {
802 json_object_string_add(json_bfd, "status",
803 bfd_get_status_str(bsp->bss.state));
804 json_object_string_add(json_bfd, "lastUpdate", time_buf);
805 } else
806 vty_out(vty, " Status: %s, Last update: %s\n",
807 bfd_get_status_str(bsp->bss.state), time_buf);
808
809 if (json)
810 json_object_object_add(json, "peerBfdInfo", json_bfd);
811 else
812 vty_out(vty, "\n");
813 }
814
815 /*
816 * Zebra communication related.
817 */
818
819 /**
820 * Callback for reinstallation of all registered BFD sessions.
821 *
822 * Use this as `zclient` `bfd_dest_replay` callback.
823 */
824 int zclient_bfd_session_reply(ZAPI_CALLBACK_ARGS)
825 {
826 struct bfd_session_params *bsp;
827
828 if (!zclient->bfd_integration)
829 return 0;
830
831 /* Do nothing when shutting down. */
832 if (bsglobal.shutting_down)
833 return 0;
834
835 if (bsglobal.debugging)
836 zlog_debug("%s: sending all sessions registered", __func__);
837
838 /* Send the client registration */
839 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
840
841 /* Replay all activated peers. */
842 TAILQ_FOREACH (bsp, &bsglobal.bsplist, entry) {
843 /* Skip not installed sessions. */
844 if (!bsp->installed)
845 continue;
846
847 /* We are reconnecting, so we must send installation. */
848 bsp->installed = false;
849
850 /* Cancel any pending installation request. */
851 THREAD_OFF(bsp->installev);
852
853 /* Ask for installation. */
854 bsp->lastev = BSE_INSTALL;
855 thread_execute(bsglobal.tm, _bfd_sess_send, bsp, 0);
856 }
857
858 return 0;
859 }
860
861 int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS)
862 {
863 struct bfd_session_params *bsp, *bspn;
864 size_t sessions_updated = 0;
865 struct interface *ifp;
866 int remote_cbit = false;
867 int state = BFD_STATUS_UNKNOWN;
868 time_t now;
869 size_t addrlen;
870 struct prefix dp;
871 struct prefix sp;
872 char ifstr[128], cbitstr[32];
873
874 if (!zclient->bfd_integration)
875 return 0;
876
877 /* Do nothing when shutting down. */
878 if (bsglobal.shutting_down)
879 return 0;
880
881 ifp = bfd_get_peer_info(zclient->ibuf, &dp, &sp, &state, &remote_cbit,
882 vrf_id);
883 /*
884 * When interface lookup fails or an invalid stream is read, we must
885 * not proceed otherwise it will trigger an assertion while checking
886 * family type below.
887 */
888 if (dp.family == 0 || sp.family == 0)
889 return 0;
890
891 if (bsglobal.debugging) {
892 ifstr[0] = 0;
893 if (ifp)
894 snprintf(ifstr, sizeof(ifstr), " (interface %s)",
895 ifp->name);
896
897 snprintf(cbitstr, sizeof(cbitstr), " (CPI bit %s)",
898 remote_cbit ? "yes" : "no");
899
900 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__, &sp,
901 &dp, ifstr, vrf_id_to_name(vrf_id), vrf_id, cbitstr,
902 bfd_get_status_str(state));
903 }
904
905 switch (dp.family) {
906 case AF_INET:
907 addrlen = sizeof(struct in_addr);
908 break;
909 case AF_INET6:
910 addrlen = sizeof(struct in6_addr);
911 break;
912
913 default:
914 /* Unexpected value. */
915 assert(0);
916 break;
917 }
918
919 /* Cache current time to avoid multiple monotime clock calls. */
920 now = monotime(NULL);
921
922 /* Notify all matching sessions about update. */
923 TAILQ_FOREACH_SAFE (bsp, &bsglobal.bsplist, entry, bspn) {
924 /* Skip not installed entries. */
925 if (!bsp->installed)
926 continue;
927 /* Skip different VRFs. */
928 if (bsp->args.vrf_id != vrf_id)
929 continue;
930 /* Skip different families. */
931 if (bsp->args.family != dp.family)
932 continue;
933 /* Skip different interface. */
934 if (bsp->args.ifnamelen && ifp
935 && strcmp(bsp->args.ifname, ifp->name) != 0)
936 continue;
937 /* Skip non matching destination addresses. */
938 if (memcmp(&bsp->args.dst, &dp.u, addrlen) != 0)
939 continue;
940 /*
941 * Source comparison test:
942 * We will only compare source if BFD daemon provided the
943 * source address and the protocol set a source address in
944 * the configuration otherwise we'll just skip it.
945 */
946 if (sp.family && memcmp(&bsp->args.src, &i6a_zero, addrlen) != 0
947 && memcmp(&sp.u, &i6a_zero, addrlen) != 0
948 && memcmp(&bsp->args.src, &sp.u, addrlen) != 0)
949 continue;
950 /* No session state change. */
951 if ((int)bsp->bss.state == state)
952 continue;
953
954 bsp->bss.last_event = now;
955 bsp->bss.previous_state = bsp->bss.state;
956 bsp->bss.state = state;
957 bsp->bss.remote_cbit = remote_cbit;
958 bsp->updatecb(bsp, &bsp->bss, bsp->arg);
959 sessions_updated++;
960 }
961
962 if (bsglobal.debugging)
963 zlog_debug("%s: sessions updated: %zu", __func__,
964 sessions_updated);
965
966 return 0;
967 }
968
969 void bfd_protocol_integration_init(struct zclient *zc, struct thread_master *tm)
970 {
971 /* Initialize data structure. */
972 TAILQ_INIT(&bsglobal.bsplist);
973
974 /* Copy pointers. */
975 bsglobal.zc = zc;
976 bsglobal.tm = tm;
977
978 /* Enable BFD callbacks. */
979 zc->bfd_integration = true;
980
981 /* Send the client registration */
982 bfd_client_sendmsg(zc, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
983 }
984
985 void bfd_protocol_integration_set_debug(bool enable)
986 {
987 bsglobal.debugging = enable;
988 }
989
990 void bfd_protocol_integration_set_shutdown(bool enable)
991 {
992 bsglobal.shutting_down = enable;
993 }
994
995 bool bfd_protocol_integration_debug(void)
996 {
997 return bsglobal.debugging;
998 }
999
1000 bool bfd_protocol_integration_shutting_down(void)
1001 {
1002 return bsglobal.shutting_down;
1003 }