2 * bfd.c: BFD handling routines
4 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
6 * This file is part of GNU Zebra.
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
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.
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
36 DEFINE_MTYPE_STATIC(LIB
, BFD_INFO
, "BFD info");
38 static int bfd_debug
= 0;
39 static struct bfd_gbl bfd_gbl
;
42 * bfd_gbl_init - Initialize the BFD global structure
44 void bfd_gbl_init(void)
46 memset(&bfd_gbl
, 0, sizeof(struct bfd_gbl
));
50 * bfd_gbl_exit - Called when daemon exits
52 void bfd_gbl_exit(void)
54 SET_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
);
58 * bfd_info_create - Allocate the BFD information
60 struct bfd_info
*bfd_info_create(void)
62 struct bfd_info
*bfd_info
;
64 bfd_info
= XCALLOC(MTYPE_BFD_INFO
, sizeof(struct bfd_info
));
67 bfd_info
->status
= BFD_STATUS_UNKNOWN
;
68 bfd_info
->type
= BFD_TYPE_NOT_CONFIGURED
;
69 bfd_info
->last_update
= 0;
74 * bfd_info_free - Free the BFD information.
76 void bfd_info_free(struct bfd_info
**bfd_info
)
78 XFREE(MTYPE_BFD_INFO
, *bfd_info
);
82 * bfd_validate_param - Validate the BFD paramter information.
84 int bfd_validate_param(struct vty
*vty
, const char *dm_str
, const char *rx_str
,
85 const char *tx_str
, uint8_t *dm_val
, uint32_t *rx_val
,
88 *dm_val
= strtoul(dm_str
, NULL
, 10);
89 *rx_val
= strtoul(rx_str
, NULL
, 10);
90 *tx_val
= strtoul(tx_str
, NULL
, 10);
95 * bfd_set_param - Set the configured BFD paramter values
97 void bfd_set_param(struct bfd_info
**bfd_info
, uint32_t min_rx
, uint32_t min_tx
,
98 uint8_t detect_mult
, const char *profile
, int defaults
,
102 *bfd_info
= bfd_info_create();
103 *command
= ZEBRA_BFD_DEST_REGISTER
;
105 if (((*bfd_info
)->required_min_rx
!= min_rx
)
106 || ((*bfd_info
)->desired_min_tx
!= min_tx
)
107 || ((*bfd_info
)->detect_mult
!= detect_mult
)
108 || ((*bfd_info
)->profile
[0] == 0 && profile
)
109 || ((*bfd_info
)->profile
[0] && profile
== NULL
)
110 || (profile
&& (*bfd_info
)->profile
[0]
111 && strcmp((*bfd_info
)->profile
, profile
)))
112 *command
= ZEBRA_BFD_DEST_UPDATE
;
116 (*bfd_info
)->required_min_rx
= min_rx
;
117 (*bfd_info
)->desired_min_tx
= min_tx
;
118 (*bfd_info
)->detect_mult
= detect_mult
;
120 strlcpy((*bfd_info
)->profile
, profile
,
121 BFD_PROFILE_NAME_LEN
);
123 (*bfd_info
)->profile
[0] = '\0';
127 SET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
129 UNSET_FLAG((*bfd_info
)->flags
, BFD_FLAG_PARAM_CFG
);
133 * bfd_peer_sendmsg - Format and send a peer register/Unregister
134 * command to Zebra to be forwarded to BFD
136 * DEPRECATED: use zclient_bfd_command instead
138 void bfd_peer_sendmsg(struct zclient
*zclient
, struct bfd_info
*bfd_info
,
139 int family
, void *dst_ip
, void *src_ip
, char *if_name
,
140 int ttl
, int multihop
, int cbit
, int command
,
141 int set_flag
, vrf_id_t vrf_id
)
143 struct bfd_session_arg args
= {};
146 /* Individual reg/dereg messages are suppressed during shutdown. */
147 if (CHECK_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
)) {
150 "%s: Suppressing BFD peer reg/dereg messages",
156 if (!zclient
|| zclient
->sock
< 0) {
159 "%s: Can't send BFD peer register, Zebra client not established",
164 /* Fill in all arguments. */
167 args
.family
= family
;
168 args
.mhop
= multihop
;
169 args
.vrf_id
= vrf_id
;
170 args
.command
= command
;
171 args
.set_flag
= set_flag
;
172 args
.bfd_info
= bfd_info
;
174 args
.min_rx
= bfd_info
->required_min_rx
;
175 args
.min_tx
= bfd_info
->desired_min_tx
;
176 args
.detection_multiplier
= bfd_info
->detect_mult
;
177 if (bfd_info
->profile
[0]) {
178 args
.profilelen
= strlen(bfd_info
->profile
);
179 strlcpy(args
.profile
, bfd_info
->profile
,
180 sizeof(args
.profile
));
184 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
185 : sizeof(struct in6_addr
);
186 memcpy(&args
.dst
, dst_ip
, addrlen
);
188 memcpy(&args
.src
, src_ip
, addrlen
);
192 strlcpy(args
.ifname
, if_name
, sizeof(args
.ifname
));
194 zclient_bfd_command(zclient
, &args
);
198 * bfd_get_command_dbg_str - Convert command to a debug string.
200 const char *bfd_get_command_dbg_str(int command
)
203 case ZEBRA_BFD_DEST_REGISTER
:
205 case ZEBRA_BFD_DEST_DEREGISTER
:
207 case ZEBRA_BFD_DEST_UPDATE
:
215 * bfd_get_peer_info - Extract the Peer information for which the BFD session
216 * went down from the message sent from Zebra to clients.
218 struct interface
*bfd_get_peer_info(struct stream
*s
, struct prefix
*dp
,
219 struct prefix
*sp
, int *status
,
223 unsigned int ifindex
;
224 struct interface
*ifp
= NULL
;
226 int local_remote_cbit
;
229 * If the ifindex lookup fails the
230 * rest of the data in the stream is
231 * not read. All examples of this function
232 * call immediately use the dp->family which
233 * is not good. Ensure we are not using
236 memset(dp
, 0, sizeof(*dp
));
237 memset(sp
, 0, sizeof(*sp
));
239 /* Get interface index. */
240 ifindex
= stream_getl(s
);
244 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
248 "zebra_interface_bfd_read: Can't find interface by ifindex: %d ",
254 /* Fetch destination address. */
255 dp
->family
= stream_getc(s
);
257 plen
= prefix_blen(dp
);
258 stream_get(&dp
->u
.prefix
, s
, plen
);
259 dp
->prefixlen
= stream_getc(s
);
261 /* Get BFD status. */
262 *status
= stream_getl(s
);
264 sp
->family
= stream_getc(s
);
266 plen
= prefix_blen(sp
);
267 stream_get(&sp
->u
.prefix
, s
, plen
);
268 sp
->prefixlen
= stream_getc(s
);
270 local_remote_cbit
= stream_getc(s
);
272 *remote_cbit
= local_remote_cbit
;
277 * bfd_get_status_str - Convert BFD status to a display string.
279 const char *bfd_get_status_str(int status
)
282 case BFD_STATUS_DOWN
:
286 case BFD_STATUS_ADMIN_DOWN
:
288 case BFD_STATUS_UNKNOWN
:
295 * bfd_last_update - Calculate the last BFD update time and convert it
296 * into a dd:hh:mm:ss display format.
298 static void bfd_last_update(time_t last_update
, char *buf
, size_t len
)
305 /* If no BFD satatus update has ever been received, print `never'. */
306 if (last_update
== 0) {
307 snprintf(buf
, len
, "never");
311 /* Get current time. */
314 diff
= curr
- last_update
;
315 gmtime_r(&diff
, &tm
);
317 snprintf(buf
, len
, "%d:%02d:%02d:%02d", tm
.tm_yday
, tm
.tm_hour
,
318 tm
.tm_min
, tm
.tm_sec
);
322 * bfd_show_param - Show the BFD parameter information.
324 void bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
325 int extra_space
, bool use_json
, json_object
*json_obj
)
327 json_object
*json_bfd
= NULL
;
334 json_bfd
= json_object_new_object();
338 json_object_int_add(json_bfd
, "detectMultiplier",
339 bfd_info
->detect_mult
);
340 json_object_int_add(json_bfd
, "rxMinInterval",
341 bfd_info
->required_min_rx
);
342 json_object_int_add(json_bfd
, "txMinInterval",
343 bfd_info
->desired_min_tx
);
345 json_object_object_add(json_obj
, "peerBfdInfo",
349 " %s%sDetect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
350 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
351 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
352 bfd_info
->desired_min_tx
);
357 * bfd_show_status - Show the BFD status information.
359 static void bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
,
360 int bfd_tag
, int extra_space
, bool use_json
,
361 json_object
*json_bfd
)
368 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
370 json_object_string_add(json_bfd
, "status",
371 bfd_get_status_str(bfd_info
->status
));
372 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
374 vty_out(vty
, " %s%sStatus: %s, Last update: %s\n",
375 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
376 bfd_get_status_str(bfd_info
->status
), time_buf
);
381 * bfd_show_info - Show the BFD information.
383 void bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
384 int extra_space
, bool use_json
, json_object
*json_obj
)
386 json_object
*json_bfd
= NULL
;
392 json_bfd
= json_object_new_object();
394 json_object_string_add(json_bfd
, "type", "multi hop");
396 json_object_string_add(json_bfd
, "type", "single hop");
398 vty_out(vty
, " %sBFD: Type: %s\n", (extra_space
) ? " " : "",
399 (multihop
) ? "multi hop" : "single hop");
402 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
403 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
406 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
412 * bfd_client_sendmsg - Format and send a client register
413 * command to Zebra to be forwarded to BFD
415 void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
419 enum zclient_send_status ret
;
422 if (!zclient
|| zclient
->sock
< 0) {
425 "%s: Can't send BFD client register, Zebra client not established",
432 zclient_create_header(s
, command
, vrf_id
);
434 stream_putl(s
, getpid());
436 stream_putw_at(s
, 0, stream_get_endp(s
));
438 ret
= zclient_send_message(zclient
);
440 if (ret
== ZCLIENT_SEND_FAILURE
) {
443 "bfd_client_sendmsg %ld: zclient_send_message() failed",
451 int zclient_bfd_command(struct zclient
*zc
, struct bfd_session_arg
*args
)
456 /* Individual reg/dereg messages are suppressed during shutdown. */
457 if (CHECK_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
)) {
460 "%s: Suppressing BFD peer reg/dereg messages",
466 if (!zc
|| zc
->sock
< 0) {
468 zlog_debug("%s: zclient unavailable", __func__
);
475 /* Create new message. */
476 zclient_create_header(s
, args
->command
, args
->vrf_id
);
477 stream_putl(s
, getpid());
479 /* Encode destination address. */
480 stream_putw(s
, args
->family
);
481 addrlen
= (args
->family
== AF_INET
) ? sizeof(struct in_addr
)
482 : sizeof(struct in6_addr
);
483 stream_put(s
, &args
->dst
, addrlen
);
486 * For more BFD integration protocol details, see function
487 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
490 /* Session timers. */
491 stream_putl(s
, args
->min_rx
);
492 stream_putl(s
, args
->min_tx
);
493 stream_putc(s
, args
->detection_multiplier
);
496 stream_putc(s
, args
->mhop
!= 0);
498 /* Source address. */
499 stream_putw(s
, args
->family
);
500 stream_put(s
, &args
->src
, addrlen
);
502 /* Send the expected TTL. */
503 stream_putc(s
, args
->ttl
);
505 /* Send interface name if any. */
506 stream_putc(s
, args
->ifnamelen
);
508 stream_put(s
, args
->ifname
, args
->ifnamelen
);
510 /* Send the C bit indicator. */
511 stream_putc(s
, args
->cbit
);
513 /* Send profile name if any. */
514 stream_putc(s
, args
->profilelen
);
515 if (args
->profilelen
)
516 stream_put(s
, args
->profile
, args
->profilelen
);
518 /* Encode timers if this is a registration message. */
519 if (args
->command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
520 stream_putl(s
, args
->min_rx
);
521 stream_putl(s
, args
->min_tx
);
522 stream_putc(s
, args
->detection_multiplier
);
526 /* Multi hop indicator. */
529 /* Multi hop always sends the source address. */
530 stream_putw(s
, args
->family
);
531 stream_put(s
, &args
->src
, addrlen
);
533 /* Send the expected TTL. */
534 stream_putc(s
, args
->ttl
);
536 /* Multi hop indicator. */
539 /* Single hop only sends the source address when IPv6. */
540 if (args
->family
== AF_INET6
) {
541 stream_putw(s
, args
->family
);
542 stream_put(s
, &args
->src
, addrlen
);
545 /* Send interface name if any. */
546 stream_putc(s
, args
->ifnamelen
);
548 stream_put(s
, args
->ifname
, args
->ifnamelen
);
550 #endif /* HAVE_BFDD */
552 /* Finish the message by writing the size. */
553 stream_putw_at(s
, 0, stream_get_endp(s
));
555 /* Send message to zebra. */
556 if (zclient_send_message(zc
) == ZCLIENT_SEND_FAILURE
) {
558 zlog_debug("%s: zclient_send_message failed", __func__
);
562 /* Write registration indicator into data structure. */
563 if (args
->bfd_info
&& args
->set_flag
) {
564 if (args
->command
== ZEBRA_BFD_DEST_REGISTER
)
565 SET_FLAG(args
->bfd_info
->flags
, BFD_FLAG_BFD_REG
);
566 else if (args
->command
== ZEBRA_BFD_DEST_DEREGISTER
)
567 UNSET_FLAG(args
->bfd_info
->flags
, BFD_FLAG_BFD_REG
);
574 * BFD protocol integration configuration.
577 /** Events definitions. */
578 enum bfd_session_event
{
579 /** Remove the BFD session configuration. */
581 /** Install the BFD session configuration. */
586 * Data structure to do the necessary tricks to hide the BFD protocol
587 * integration internals.
589 struct bfd_session_params
{
590 /** Contains the session parameters and more. */
591 struct bfd_session_arg args
;
592 /** Contains the session state. */
593 struct bfd_session_status bss
;
594 /** Protocol implementation status update callback. */
595 bsp_status_update updatecb
;
596 /** Protocol implementation custom data pointer. */
602 * This variable controls what action to execute when the command batch
603 * finishes. Normally we'd use `thread_add_event` value, however since
604 * that function is going to be called multiple times and the value
605 * might be different we'll use this variable to keep track of it.
607 enum bfd_session_event lastev
;
609 * BFD session configuration event.
611 * Multiple actions might be asked during a command batch (either via
612 * configuration load or northbound batch), so we'll use this to
613 * install/uninstall the BFD session parameters only once.
615 struct thread
*installev
;
617 /** BFD session installation state. */
619 /** BFD session enabled. */
622 /** Global BFD paramaters list. */
623 TAILQ_ENTRY(bfd_session_params
) entry
;
626 struct bfd_sessions_global
{
628 * Global BFD session parameters list for (re)installation and update
629 * without code duplication among daemons.
631 TAILQ_HEAD(bsplist
, bfd_session_params
) bsplist
;
633 /** Pointer to FRR's event manager. */
634 struct thread_master
*tm
;
635 /** Pointer to zebra client data structure. */
638 /** Debugging state. */
640 /** Is shutting down? */
644 /** Global configuration variable. */
645 static struct bfd_sessions_global bsglobal
;
647 /** Global empty address for IPv4/IPv6. */
648 static const struct in6_addr i6a_zero
;
650 struct bfd_session_params
*bfd_sess_new(bsp_status_update updatecb
, void *arg
)
652 struct bfd_session_params
*bsp
;
654 bsp
= XCALLOC(MTYPE_BFD_INFO
, sizeof(*bsp
));
656 /* Save application data. */
657 bsp
->updatecb
= updatecb
;
661 bsp
->args
.detection_multiplier
= BFD_DEF_DETECT_MULT
;
662 bsp
->args
.ttl
= BFD_SINGLE_HOP_TTL
;
663 bsp
->args
.min_rx
= BFD_DEF_MIN_RX
;
664 bsp
->args
.min_tx
= BFD_DEF_MIN_TX
;
665 bsp
->args
.vrf_id
= VRF_DEFAULT
;
667 /* Register in global list. */
668 TAILQ_INSERT_TAIL(&bsglobal
.bsplist
, bsp
, entry
);
673 static bool _bfd_sess_valid(const struct bfd_session_params
*bsp
)
675 /* Peer/local address not configured. */
676 if (bsp
->args
.family
== 0)
679 /* Address configured but invalid. */
680 if (bsp
->args
.family
!= AF_INET
&& bsp
->args
.family
!= AF_INET6
) {
681 if (bsglobal
.debugging
)
682 zlog_debug("%s: invalid session family: %d", __func__
,
687 /* Invalid address. */
688 if (memcmp(&bsp
->args
.dst
, &i6a_zero
, sizeof(i6a_zero
)) == 0) {
689 if (bsglobal
.debugging
) {
690 if (bsp
->args
.family
== AF_INET
)
691 zlog_debug("%s: invalid address: %pI4",
693 (struct in_addr
*)&bsp
->args
.dst
);
695 zlog_debug("%s: invalid address: %pI6",
696 __func__
, &bsp
->args
.dst
);
701 /* Multi hop requires local address. */
703 && memcmp(&i6a_zero
, &bsp
->args
.src
, sizeof(i6a_zero
)) == 0) {
704 if (bsglobal
.debugging
)
706 "%s: multi hop but no local address provided",
712 if (bsp
->args
.vrf_id
== VRF_UNKNOWN
) {
713 if (bsglobal
.debugging
)
714 zlog_debug("%s: asked for unknown VRF", __func__
);
721 static int _bfd_sess_send(struct thread
*t
)
723 struct bfd_session_params
*bsp
= THREAD_ARG(t
);
726 /* Validate configuration before trying to send bogus data. */
727 if (!_bfd_sess_valid(bsp
))
730 if (bsp
->lastev
== BSE_INSTALL
) {
731 bsp
->args
.command
= bsp
->installed
? ZEBRA_BFD_DEST_UPDATE
732 : ZEBRA_BFD_DEST_REGISTER
;
734 bsp
->args
.command
= ZEBRA_BFD_DEST_DEREGISTER
;
736 /* If not installed and asked for uninstall, do nothing. */
737 if (!bsp
->installed
&& bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
740 rv
= zclient_bfd_command(bsglobal
.zc
, &bsp
->args
);
741 /* Command was sent successfully. */
743 /* Update installation status. */
744 if (bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
745 bsp
->installed
= false;
746 else if (bsp
->args
.command
== ZEBRA_BFD_DEST_REGISTER
)
747 bsp
->installed
= true;
753 static void _bfd_sess_remove(struct bfd_session_params
*bsp
)
755 /* Not installed, nothing to do. */
759 /* Cancel any pending installation request. */
760 THREAD_OFF(bsp
->installev
);
762 /* Send request to remove any session. */
763 bsp
->lastev
= BSE_UNINSTALL
;
764 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
767 void bfd_sess_free(struct bfd_session_params
**bsp
)
772 /* Remove any installed session. */
773 _bfd_sess_remove(*bsp
);
775 /* Remove from global list. */
776 TAILQ_REMOVE(&bsglobal
.bsplist
, (*bsp
), entry
);
778 /* Free the memory and point to NULL. */
779 XFREE(MTYPE_BFD_INFO
, (*bsp
));
782 void bfd_sess_enable(struct bfd_session_params
*bsp
, bool enable
)
784 /* Remove the session when disabling. */
786 _bfd_sess_remove(bsp
);
788 bsp
->enabled
= enable
;
791 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
792 struct in_addr
*src
, struct in_addr
*dst
)
794 /* If already installed, remove the old setting. */
795 _bfd_sess_remove(bsp
);
797 bsp
->args
.family
= AF_INET
;
799 /* Clean memory, set zero value and avoid static analyser warnings. */
800 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
801 memset(&bsp
->args
.dst
, 0, sizeof(bsp
->args
.dst
));
803 /* Copy the equivalent of IPv4 to arguments structure. */
805 memcpy(&bsp
->args
.src
, src
, sizeof(struct in_addr
));
808 memcpy(&bsp
->args
.dst
, dst
, sizeof(struct in_addr
));
811 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
812 struct in6_addr
*src
, struct in6_addr
*dst
)
814 /* If already installed, remove the old setting. */
815 _bfd_sess_remove(bsp
);
817 bsp
->args
.family
= AF_INET6
;
819 /* Clean memory, set zero value and avoid static analyser warnings. */
820 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
823 bsp
->args
.src
= *src
;
826 bsp
->args
.dst
= *dst
;
829 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
)
831 /* If already installed, remove the old setting. */
832 _bfd_sess_remove(bsp
);
834 if (ifname
== NULL
) {
835 bsp
->args
.ifname
[0] = 0;
836 bsp
->args
.ifnamelen
= 0;
840 if (strlcpy(bsp
->args
.ifname
, ifname
, sizeof(bsp
->args
.ifname
))
841 > sizeof(bsp
->args
.ifname
))
842 zlog_warn("%s: interface name truncated: %s", __func__
, ifname
);
844 bsp
->args
.ifnamelen
= strlen(bsp
->args
.ifname
);
847 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
)
849 if (profile
== NULL
) {
850 bsp
->args
.profile
[0] = 0;
851 bsp
->args
.profilelen
= 0;
855 if (strlcpy(bsp
->args
.profile
, profile
, sizeof(bsp
->args
.profile
))
856 > sizeof(bsp
->args
.profile
))
857 zlog_warn("%s: profile name truncated: %s", __func__
, profile
);
859 bsp
->args
.profilelen
= strlen(bsp
->args
.profile
);
862 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
)
864 /* If already installed, remove the old setting. */
865 _bfd_sess_remove(bsp
);
867 bsp
->args
.vrf_id
= vrf_id
;
870 void bfd_sess_set_mininum_ttl(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
872 assert(min_ttl
!= 0);
874 /* If already installed, remove the old setting. */
875 _bfd_sess_remove(bsp
);
877 /* Invert TTL value: protocol expects number of hops. */
878 min_ttl
= (BFD_SINGLE_HOP_TTL
+ 1) - min_ttl
;
879 bsp
->args
.ttl
= min_ttl
;
880 bsp
->args
.mhop
= (min_ttl
> 1);
883 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
885 /* If already installed, remove the old setting. */
886 _bfd_sess_remove(bsp
);
888 bsp
->args
.ttl
= min_ttl
;
889 bsp
->args
.mhop
= (min_ttl
> 1);
893 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
)
895 bsp
->args
.cbit
= enable
;
898 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
899 uint8_t detection_multiplier
, uint32_t min_rx
,
902 bsp
->args
.detection_multiplier
= detection_multiplier
;
903 bsp
->args
.min_rx
= min_rx
;
904 bsp
->args
.min_tx
= min_tx
;
907 void bfd_sess_install(struct bfd_session_params
*bsp
)
909 /* Don't attempt to install/update when disabled. */
913 bsp
->lastev
= BSE_INSTALL
;
914 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
917 void bfd_sess_uninstall(struct bfd_session_params
*bsp
)
919 bsp
->lastev
= BSE_UNINSTALL
;
920 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
923 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
)
925 return bsp
->bss
.state
;
928 uint8_t bfd_sess_minimum_ttl(const struct bfd_session_params
*bsp
)
930 return ((BFD_SINGLE_HOP_TTL
+ 1) - bsp
->args
.ttl
);
933 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
)
935 return bsp
->args
.ttl
;
938 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
)
940 return bsp
->args
.profilelen
? bsp
->args
.profile
: NULL
;
943 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
944 struct in6_addr
*src
, struct in6_addr
*dst
)
946 *family
= bsp
->args
.family
;
948 *src
= bsp
->args
.src
;
950 *dst
= bsp
->args
.dst
;
953 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
)
955 if (bsp
->args
.ifnamelen
)
956 return bsp
->args
.ifname
;
961 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
)
963 return vrf_id_to_name(bsp
->args
.vrf_id
);
966 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
)
968 return bsp
->args
.vrf_id
;
971 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
)
973 return bsp
->args
.cbit
;
976 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
977 uint8_t *detection_multiplier
, uint32_t *min_rx
,
980 *detection_multiplier
= bsp
->args
.detection_multiplier
;
981 *min_rx
= bsp
->args
.min_rx
;
982 *min_tx
= bsp
->args
.min_tx
;
985 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
986 struct bfd_session_params
*bsp
)
988 json_object
*json_bfd
= NULL
;
993 json_bfd
= json_object_new_object();
995 json_object_string_add(json_bfd
, "type", "multi hop");
997 json_object_string_add(json_bfd
, "type", "single hop");
999 vty_out(vty
, " BFD: Type: %s\n",
1000 bsp
->args
.mhop
? "multi hop" : "single hop");
1002 /* Show configuration. */
1004 json_object_int_add(json_bfd
, "detectMultiplier",
1005 bsp
->args
.detection_multiplier
);
1006 json_object_int_add(json_bfd
, "rxMinInterval",
1008 json_object_int_add(json_bfd
, "txMinInterval",
1012 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
1013 bsp
->args
.detection_multiplier
, bsp
->args
.min_rx
,
1017 bfd_last_update(bsp
->bss
.last_event
, time_buf
, sizeof(time_buf
));
1019 json_object_string_add(json_bfd
, "status",
1020 bfd_get_status_str(bsp
->bss
.state
));
1021 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
1023 vty_out(vty
, " Status: %s, Last update: %s\n",
1024 bfd_get_status_str(bsp
->bss
.state
), time_buf
);
1027 json_object_object_add(json
, "peerBfdInfo", json_bfd
);
1033 * Zebra communication related.
1037 * Callback for reinstallation of all registered BFD sessions.
1039 * Use this as `zclient` `bfd_dest_replay` callback.
1041 static int zclient_bfd_session_reply(ZAPI_CALLBACK_ARGS
)
1043 struct bfd_session_params
*bsp
;
1045 /* Do nothing when shutting down. */
1046 if (bsglobal
.shutting_down
)
1049 if (bsglobal
.debugging
)
1050 zlog_debug("%s: sending all sessions registered", __func__
);
1052 /* Send the client registration */
1053 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
1055 /* Replay all activated peers. */
1056 TAILQ_FOREACH (bsp
, &bsglobal
.bsplist
, entry
) {
1057 /* Skip disabled sessions. */
1061 /* We are reconnecting, so we must send installation. */
1062 bsp
->installed
= false;
1064 /* Cancel any pending installation request. */
1065 THREAD_OFF(bsp
->installev
);
1067 /* Ask for installation. */
1068 bsp
->lastev
= BSE_INSTALL
;
1069 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
1075 static int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS
)
1077 struct bfd_session_params
*bsp
;
1078 size_t sessions_updated
= 0;
1079 struct interface
*ifp
;
1080 int remote_cbit
= false;
1081 int state
= BFD_STATUS_UNKNOWN
;
1086 char ifstr
[128], cbitstr
[32];
1088 /* Do nothing when shutting down. */
1089 if (bsglobal
.shutting_down
)
1092 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &state
, &remote_cbit
,
1095 if (bsglobal
.debugging
) {
1098 snprintf(ifstr
, sizeof(ifstr
), " (interface %s)",
1101 snprintf(cbitstr
, sizeof(cbitstr
), " (CPI bit %s)",
1102 remote_cbit
? "yes" : "no");
1104 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__
, &sp
,
1105 &dp
, ifstr
, vrf_id_to_name(vrf_id
), vrf_id
, cbitstr
,
1106 bfd_get_status_str(state
));
1109 switch (dp
.family
) {
1111 addrlen
= sizeof(struct in_addr
);
1114 addrlen
= sizeof(struct in6_addr
);
1118 /* Unexpected value. */
1123 /* Cache current time to avoid multiple monotime clock calls. */
1124 now
= monotime(NULL
);
1126 /* Notify all matching sessions about update. */
1127 TAILQ_FOREACH (bsp
, &bsglobal
.bsplist
, entry
) {
1128 /* Skip disabled or not installed entries. */
1129 if (!bsp
->enabled
|| !bsp
->installed
)
1131 /* Skip different VRFs. */
1132 if (bsp
->args
.vrf_id
!= vrf_id
)
1134 /* Skip different families. */
1135 if (bsp
->args
.family
!= dp
.family
)
1137 /* Skip different interface. */
1138 if (bsp
->args
.ifnamelen
&& ifp
1139 && strcmp(bsp
->args
.ifname
, ifp
->name
) != 0)
1141 /* Skip non matching destination addresses. */
1142 if (memcmp(&bsp
->args
.dst
, &dp
.u
, addrlen
) != 0)
1145 * Source comparison test:
1146 * We will only compare source if BFD daemon provided the
1147 * source address and the protocol set a source address in
1148 * the configuration otherwise we'll just skip it.
1150 if (sp
.family
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
) != 0
1151 && memcmp(&sp
.u
, &i6a_zero
, addrlen
) != 0
1152 && memcmp(&bsp
->args
.src
, &sp
.u
, addrlen
) != 0)
1154 /* No session state change. */
1155 if ((int)bsp
->bss
.state
== state
)
1158 bsp
->bss
.last_event
= now
;
1159 bsp
->bss
.previous_state
= bsp
->bss
.state
;
1160 bsp
->bss
.state
= state
;
1161 bsp
->bss
.remote_cbit
= remote_cbit
;
1162 bsp
->updatecb(bsp
, &bsp
->bss
, bsp
->arg
);
1166 if (bsglobal
.debugging
)
1167 zlog_debug("%s: sessions updated: %zu", __func__
,
1173 void bfd_protocol_integration_init(struct zclient
*zc
, struct thread_master
*tm
)
1175 /* Initialize data structure. */
1176 TAILQ_INIT(&bsglobal
.bsplist
);
1178 /* Copy pointers. */
1182 /* Install our callbacks. */
1183 zc
->interface_bfd_dest_update
= zclient_bfd_session_update
;
1184 zc
->bfd_dest_replay
= zclient_bfd_session_reply
;
1186 /* Send the client registration */
1187 bfd_client_sendmsg(zc
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
1190 void bfd_protocol_integration_set_debug(bool enable
)
1192 bsglobal
.debugging
= enable
;
1195 void bfd_protocol_integration_set_shutdown(bool enable
)
1197 bsglobal
.shutting_down
= enable
;
1200 bool bfd_protocol_integration_debug(void)
1202 return bsglobal
.debugging
;
1205 bool bfd_protocol_integration_shutting_down(void)
1207 return bsglobal
.shutting_down
;