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 STREAM_GETL(s
, ifindex
);
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 STREAM_GETC(s
, dp
->family
);
257 plen
= prefix_blen(dp
);
258 STREAM_GET(&dp
->u
.prefix
, s
, plen
);
259 STREAM_GETC(s
, dp
->prefixlen
);
261 /* Get BFD status. */
262 STREAM_GETL(s
, (*status
));
264 STREAM_GETC(s
, sp
->family
);
266 plen
= prefix_blen(sp
);
267 STREAM_GET(&sp
->u
.prefix
, s
, plen
);
268 STREAM_GETC(s
, sp
->prefixlen
);
270 STREAM_GETC(s
, local_remote_cbit
);
272 *remote_cbit
= local_remote_cbit
;
280 * bfd_get_status_str - Convert BFD status to a display string.
282 const char *bfd_get_status_str(int status
)
285 case BFD_STATUS_DOWN
:
289 case BFD_STATUS_ADMIN_DOWN
:
291 case BFD_STATUS_UNKNOWN
:
298 * bfd_last_update - Calculate the last BFD update time and convert it
299 * into a dd:hh:mm:ss display format.
301 static void bfd_last_update(time_t last_update
, char *buf
, size_t len
)
308 /* If no BFD satatus update has ever been received, print `never'. */
309 if (last_update
== 0) {
310 snprintf(buf
, len
, "never");
314 /* Get current time. */
317 diff
= curr
- last_update
;
318 gmtime_r(&diff
, &tm
);
320 snprintf(buf
, len
, "%d:%02d:%02d:%02d", tm
.tm_yday
, tm
.tm_hour
,
321 tm
.tm_min
, tm
.tm_sec
);
325 * bfd_show_param - Show the BFD parameter information.
327 void bfd_show_param(struct vty
*vty
, struct bfd_info
*bfd_info
, int bfd_tag
,
328 int extra_space
, bool use_json
, json_object
*json_obj
)
330 json_object
*json_bfd
= NULL
;
337 json_bfd
= json_object_new_object();
341 json_object_int_add(json_bfd
, "detectMultiplier",
342 bfd_info
->detect_mult
);
343 json_object_int_add(json_bfd
, "rxMinInterval",
344 bfd_info
->required_min_rx
);
345 json_object_int_add(json_bfd
, "txMinInterval",
346 bfd_info
->desired_min_tx
);
348 json_object_object_add(json_obj
, "peerBfdInfo",
352 " %s%sDetect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
353 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
354 bfd_info
->detect_mult
, bfd_info
->required_min_rx
,
355 bfd_info
->desired_min_tx
);
360 * bfd_show_status - Show the BFD status information.
362 static void bfd_show_status(struct vty
*vty
, struct bfd_info
*bfd_info
,
363 int bfd_tag
, int extra_space
, bool use_json
,
364 json_object
*json_bfd
)
371 bfd_last_update(bfd_info
->last_update
, time_buf
, 32);
373 json_object_string_add(json_bfd
, "status",
374 bfd_get_status_str(bfd_info
->status
));
375 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
377 vty_out(vty
, " %s%sStatus: %s, Last update: %s\n",
378 (extra_space
) ? " " : "", (bfd_tag
) ? "BFD: " : " ",
379 bfd_get_status_str(bfd_info
->status
), time_buf
);
384 * bfd_show_info - Show the BFD information.
386 void bfd_show_info(struct vty
*vty
, struct bfd_info
*bfd_info
, int multihop
,
387 int extra_space
, bool use_json
, json_object
*json_obj
)
389 json_object
*json_bfd
= NULL
;
395 json_bfd
= json_object_new_object();
397 json_object_string_add(json_bfd
, "type", "multi hop");
399 json_object_string_add(json_bfd
, "type", "single hop");
401 vty_out(vty
, " %sBFD: Type: %s\n", (extra_space
) ? " " : "",
402 (multihop
) ? "multi hop" : "single hop");
405 bfd_show_param(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
406 bfd_show_status(vty
, bfd_info
, 0, extra_space
, use_json
, json_bfd
);
409 json_object_object_add(json_obj
, "peerBfdInfo", json_bfd
);
415 * bfd_client_sendmsg - Format and send a client register
416 * command to Zebra to be forwarded to BFD
418 void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
422 enum zclient_send_status ret
;
425 if (!zclient
|| zclient
->sock
< 0) {
428 "%s: Can't send BFD client register, Zebra client not established",
435 zclient_create_header(s
, command
, vrf_id
);
437 stream_putl(s
, getpid());
439 stream_putw_at(s
, 0, stream_get_endp(s
));
441 ret
= zclient_send_message(zclient
);
443 if (ret
== ZCLIENT_SEND_FAILURE
) {
446 "bfd_client_sendmsg %ld: zclient_send_message() failed",
454 int zclient_bfd_command(struct zclient
*zc
, struct bfd_session_arg
*args
)
459 /* Individual reg/dereg messages are suppressed during shutdown. */
460 if (CHECK_FLAG(bfd_gbl
.flags
, BFD_GBL_FLAG_IN_SHUTDOWN
)) {
463 "%s: Suppressing BFD peer reg/dereg messages",
469 if (!zc
|| zc
->sock
< 0) {
471 zlog_debug("%s: zclient unavailable", __func__
);
478 /* Create new message. */
479 zclient_create_header(s
, args
->command
, args
->vrf_id
);
480 stream_putl(s
, getpid());
482 /* Encode destination address. */
483 stream_putw(s
, args
->family
);
484 addrlen
= (args
->family
== AF_INET
) ? sizeof(struct in_addr
)
485 : sizeof(struct in6_addr
);
486 stream_put(s
, &args
->dst
, addrlen
);
489 * For more BFD integration protocol details, see function
490 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
493 /* Session timers. */
494 stream_putl(s
, args
->min_rx
);
495 stream_putl(s
, args
->min_tx
);
496 stream_putc(s
, args
->detection_multiplier
);
499 stream_putc(s
, args
->mhop
!= 0);
501 /* Source address. */
502 stream_putw(s
, args
->family
);
503 stream_put(s
, &args
->src
, addrlen
);
505 /* Send the expected TTL. */
506 stream_putc(s
, args
->ttl
);
508 /* Send interface name if any. */
509 stream_putc(s
, args
->ifnamelen
);
511 stream_put(s
, args
->ifname
, args
->ifnamelen
);
513 /* Send the C bit indicator. */
514 stream_putc(s
, args
->cbit
);
516 /* Send profile name if any. */
517 stream_putc(s
, args
->profilelen
);
518 if (args
->profilelen
)
519 stream_put(s
, args
->profile
, args
->profilelen
);
521 /* Encode timers if this is a registration message. */
522 if (args
->command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
523 stream_putl(s
, args
->min_rx
);
524 stream_putl(s
, args
->min_tx
);
525 stream_putc(s
, args
->detection_multiplier
);
529 /* Multi hop indicator. */
532 /* Multi hop always sends the source address. */
533 stream_putw(s
, args
->family
);
534 stream_put(s
, &args
->src
, addrlen
);
536 /* Send the expected TTL. */
537 stream_putc(s
, args
->ttl
);
539 /* Multi hop indicator. */
542 /* Single hop only sends the source address when IPv6. */
543 if (args
->family
== AF_INET6
) {
544 stream_putw(s
, args
->family
);
545 stream_put(s
, &args
->src
, addrlen
);
548 /* Send interface name if any. */
549 stream_putc(s
, args
->ifnamelen
);
551 stream_put(s
, args
->ifname
, args
->ifnamelen
);
553 #endif /* HAVE_BFDD */
555 /* Finish the message by writing the size. */
556 stream_putw_at(s
, 0, stream_get_endp(s
));
558 /* Send message to zebra. */
559 if (zclient_send_message(zc
) == ZCLIENT_SEND_FAILURE
) {
561 zlog_debug("%s: zclient_send_message failed", __func__
);
565 /* Write registration indicator into data structure. */
566 if (args
->bfd_info
&& args
->set_flag
) {
567 if (args
->command
== ZEBRA_BFD_DEST_REGISTER
)
568 SET_FLAG(args
->bfd_info
->flags
, BFD_FLAG_BFD_REG
);
569 else if (args
->command
== ZEBRA_BFD_DEST_DEREGISTER
)
570 UNSET_FLAG(args
->bfd_info
->flags
, BFD_FLAG_BFD_REG
);
577 * BFD protocol integration configuration.
580 /** Events definitions. */
581 enum bfd_session_event
{
582 /** Remove the BFD session configuration. */
584 /** Install the BFD session configuration. */
589 * Data structure to do the necessary tricks to hide the BFD protocol
590 * integration internals.
592 struct bfd_session_params
{
593 /** Contains the session parameters and more. */
594 struct bfd_session_arg args
;
595 /** Contains the session state. */
596 struct bfd_session_status bss
;
597 /** Protocol implementation status update callback. */
598 bsp_status_update updatecb
;
599 /** Protocol implementation custom data pointer. */
605 * This variable controls what action to execute when the command batch
606 * finishes. Normally we'd use `thread_add_event` value, however since
607 * that function is going to be called multiple times and the value
608 * might be different we'll use this variable to keep track of it.
610 enum bfd_session_event lastev
;
612 * BFD session configuration event.
614 * Multiple actions might be asked during a command batch (either via
615 * configuration load or northbound batch), so we'll use this to
616 * install/uninstall the BFD session parameters only once.
618 struct thread
*installev
;
620 /** BFD session installation state. */
623 /** Global BFD paramaters list. */
624 TAILQ_ENTRY(bfd_session_params
) entry
;
627 struct bfd_sessions_global
{
629 * Global BFD session parameters list for (re)installation and update
630 * without code duplication among daemons.
632 TAILQ_HEAD(bsplist
, bfd_session_params
) bsplist
;
634 /** Pointer to FRR's event manager. */
635 struct thread_master
*tm
;
636 /** Pointer to zebra client data structure. */
639 /** Debugging state. */
641 /** Is shutting down? */
645 /** Global configuration variable. */
646 static struct bfd_sessions_global bsglobal
;
648 /** Global empty address for IPv4/IPv6. */
649 static const struct in6_addr i6a_zero
;
651 struct bfd_session_params
*bfd_sess_new(bsp_status_update updatecb
, void *arg
)
653 struct bfd_session_params
*bsp
;
655 bsp
= XCALLOC(MTYPE_BFD_INFO
, sizeof(*bsp
));
657 /* Save application data. */
658 bsp
->updatecb
= updatecb
;
662 bsp
->args
.detection_multiplier
= BFD_DEF_DETECT_MULT
;
663 bsp
->args
.ttl
= BFD_SINGLE_HOP_TTL
;
664 bsp
->args
.min_rx
= BFD_DEF_MIN_RX
;
665 bsp
->args
.min_tx
= BFD_DEF_MIN_TX
;
666 bsp
->args
.vrf_id
= VRF_DEFAULT
;
668 /* Register in global list. */
669 TAILQ_INSERT_TAIL(&bsglobal
.bsplist
, bsp
, entry
);
674 static bool _bfd_sess_valid(const struct bfd_session_params
*bsp
)
676 /* Peer/local address not configured. */
677 if (bsp
->args
.family
== 0)
680 /* Address configured but invalid. */
681 if (bsp
->args
.family
!= AF_INET
&& bsp
->args
.family
!= AF_INET6
) {
682 if (bsglobal
.debugging
)
683 zlog_debug("%s: invalid session family: %d", __func__
,
688 /* Invalid address. */
689 if (memcmp(&bsp
->args
.dst
, &i6a_zero
, sizeof(i6a_zero
)) == 0) {
690 if (bsglobal
.debugging
) {
691 if (bsp
->args
.family
== AF_INET
)
692 zlog_debug("%s: invalid address: %pI4",
694 (struct in_addr
*)&bsp
->args
.dst
);
696 zlog_debug("%s: invalid address: %pI6",
697 __func__
, &bsp
->args
.dst
);
702 /* Multi hop requires local address. */
704 && memcmp(&i6a_zero
, &bsp
->args
.src
, sizeof(i6a_zero
)) == 0) {
705 if (bsglobal
.debugging
)
707 "%s: multi hop but no local address provided",
713 if (bsp
->args
.vrf_id
== VRF_UNKNOWN
) {
714 if (bsglobal
.debugging
)
715 zlog_debug("%s: asked for unknown VRF", __func__
);
722 static int _bfd_sess_send(struct thread
*t
)
724 struct bfd_session_params
*bsp
= THREAD_ARG(t
);
727 /* Validate configuration before trying to send bogus data. */
728 if (!_bfd_sess_valid(bsp
))
731 if (bsp
->lastev
== BSE_INSTALL
) {
732 bsp
->args
.command
= bsp
->installed
? ZEBRA_BFD_DEST_UPDATE
733 : ZEBRA_BFD_DEST_REGISTER
;
735 bsp
->args
.command
= ZEBRA_BFD_DEST_DEREGISTER
;
737 /* If not installed and asked for uninstall, do nothing. */
738 if (!bsp
->installed
&& bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
741 rv
= zclient_bfd_command(bsglobal
.zc
, &bsp
->args
);
742 /* Command was sent successfully. */
744 /* Update installation status. */
745 if (bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
746 bsp
->installed
= false;
747 else if (bsp
->args
.command
== ZEBRA_BFD_DEST_REGISTER
)
748 bsp
->installed
= true;
750 struct ipaddr src
, dst
;
752 src
.ipa_type
= bsp
->args
.family
;
753 src
.ipaddr_v6
= bsp
->args
.src
;
754 dst
.ipa_type
= bsp
->args
.family
;
755 dst
.ipaddr_v6
= bsp
->args
.dst
;
758 "%s: BFD session %pIA -> %pIA interface %s VRF %s(%u) was not %s",
759 __func__
, &src
, &dst
,
760 bsp
->args
.ifnamelen
? bsp
->args
.ifname
: "*",
761 vrf_id_to_name(bsp
->args
.vrf_id
), bsp
->args
.vrf_id
,
762 bsp
->lastev
== BSE_INSTALL
? "installed"
769 static void _bfd_sess_remove(struct bfd_session_params
*bsp
)
771 /* Not installed, nothing to do. */
775 /* Cancel any pending installation request. */
776 THREAD_OFF(bsp
->installev
);
778 /* Send request to remove any session. */
779 bsp
->lastev
= BSE_UNINSTALL
;
780 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
783 void bfd_sess_free(struct bfd_session_params
**bsp
)
788 /* Remove any installed session. */
789 _bfd_sess_remove(*bsp
);
791 /* Remove from global list. */
792 TAILQ_REMOVE(&bsglobal
.bsplist
, (*bsp
), entry
);
794 /* Free the memory and point to NULL. */
795 XFREE(MTYPE_BFD_INFO
, (*bsp
));
798 static bool bfd_sess_address_changed(const struct bfd_session_params
*bsp
,
800 const struct in6_addr
*src
,
801 const struct in6_addr
*dst
)
805 if (bsp
->args
.family
!= family
)
808 addrlen
= (family
== AF_INET
) ? sizeof(struct in_addr
)
809 : sizeof(struct in6_addr
);
810 if ((src
== NULL
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
))
811 || (src
&& memcmp(src
, &bsp
->args
.src
, addrlen
))
812 || memcmp(dst
, &bsp
->args
.dst
, addrlen
))
818 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
819 struct in_addr
*src
, struct in_addr
*dst
)
821 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
822 (struct in6_addr
*)dst
))
825 /* If already installed, remove the old setting. */
826 _bfd_sess_remove(bsp
);
828 bsp
->args
.family
= AF_INET
;
830 /* Clean memory, set zero value and avoid static analyser warnings. */
831 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
832 memset(&bsp
->args
.dst
, 0, sizeof(bsp
->args
.dst
));
834 /* Copy the equivalent of IPv4 to arguments structure. */
836 memcpy(&bsp
->args
.src
, src
, sizeof(struct in_addr
));
839 memcpy(&bsp
->args
.dst
, dst
, sizeof(struct in_addr
));
842 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
843 struct in6_addr
*src
, struct in6_addr
*dst
)
845 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
846 (struct in6_addr
*)dst
))
849 /* If already installed, remove the old setting. */
850 _bfd_sess_remove(bsp
);
852 bsp
->args
.family
= AF_INET6
;
854 /* Clean memory, set zero value and avoid static analyser warnings. */
855 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
858 bsp
->args
.src
= *src
;
861 bsp
->args
.dst
= *dst
;
864 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
)
866 if ((ifname
== NULL
&& bsp
->args
.ifnamelen
== 0)
867 || (ifname
&& strcmp(bsp
->args
.ifname
, ifname
) == 0))
870 /* If already installed, remove the old setting. */
871 _bfd_sess_remove(bsp
);
873 if (ifname
== NULL
) {
874 bsp
->args
.ifname
[0] = 0;
875 bsp
->args
.ifnamelen
= 0;
879 if (strlcpy(bsp
->args
.ifname
, ifname
, sizeof(bsp
->args
.ifname
))
880 > sizeof(bsp
->args
.ifname
))
881 zlog_warn("%s: interface name truncated: %s", __func__
, ifname
);
883 bsp
->args
.ifnamelen
= strlen(bsp
->args
.ifname
);
886 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
)
888 if (profile
== NULL
) {
889 bsp
->args
.profile
[0] = 0;
890 bsp
->args
.profilelen
= 0;
894 if (strlcpy(bsp
->args
.profile
, profile
, sizeof(bsp
->args
.profile
))
895 > sizeof(bsp
->args
.profile
))
896 zlog_warn("%s: profile name truncated: %s", __func__
, profile
);
898 bsp
->args
.profilelen
= strlen(bsp
->args
.profile
);
901 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
)
903 if (bsp
->args
.vrf_id
== vrf_id
)
906 /* If already installed, remove the old setting. */
907 _bfd_sess_remove(bsp
);
909 bsp
->args
.vrf_id
= vrf_id
;
912 void bfd_sess_set_mininum_ttl(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
914 assert(min_ttl
!= 0);
916 if (bsp
->args
.ttl
== ((BFD_SINGLE_HOP_TTL
+ 1) - min_ttl
))
919 /* If already installed, remove the old setting. */
920 _bfd_sess_remove(bsp
);
922 /* Invert TTL value: protocol expects number of hops. */
923 min_ttl
= (BFD_SINGLE_HOP_TTL
+ 1) - min_ttl
;
924 bsp
->args
.ttl
= min_ttl
;
925 bsp
->args
.mhop
= (min_ttl
> 1);
928 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
930 if (bsp
->args
.ttl
== min_ttl
)
933 /* If already installed, remove the old setting. */
934 _bfd_sess_remove(bsp
);
936 bsp
->args
.ttl
= min_ttl
;
937 bsp
->args
.mhop
= (min_ttl
> 1);
941 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
)
943 bsp
->args
.cbit
= enable
;
946 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
947 uint8_t detection_multiplier
, uint32_t min_rx
,
950 bsp
->args
.detection_multiplier
= detection_multiplier
;
951 bsp
->args
.min_rx
= min_rx
;
952 bsp
->args
.min_tx
= min_tx
;
955 void bfd_sess_install(struct bfd_session_params
*bsp
)
957 bsp
->lastev
= BSE_INSTALL
;
958 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
961 void bfd_sess_uninstall(struct bfd_session_params
*bsp
)
963 bsp
->lastev
= BSE_UNINSTALL
;
964 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
967 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
)
969 return bsp
->bss
.state
;
972 uint8_t bfd_sess_minimum_ttl(const struct bfd_session_params
*bsp
)
974 return ((BFD_SINGLE_HOP_TTL
+ 1) - bsp
->args
.ttl
);
977 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
)
979 return bsp
->args
.ttl
;
982 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
)
984 return bsp
->args
.profilelen
? bsp
->args
.profile
: NULL
;
987 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
988 struct in6_addr
*src
, struct in6_addr
*dst
)
990 *family
= bsp
->args
.family
;
992 *src
= bsp
->args
.src
;
994 *dst
= bsp
->args
.dst
;
997 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
)
999 if (bsp
->args
.ifnamelen
)
1000 return bsp
->args
.ifname
;
1005 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
)
1007 return vrf_id_to_name(bsp
->args
.vrf_id
);
1010 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
)
1012 return bsp
->args
.vrf_id
;
1015 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
)
1017 return bsp
->args
.cbit
;
1020 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
1021 uint8_t *detection_multiplier
, uint32_t *min_rx
,
1024 *detection_multiplier
= bsp
->args
.detection_multiplier
;
1025 *min_rx
= bsp
->args
.min_rx
;
1026 *min_tx
= bsp
->args
.min_tx
;
1029 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
1030 struct bfd_session_params
*bsp
)
1032 json_object
*json_bfd
= NULL
;
1040 json_bfd
= json_object_new_object();
1042 json_object_string_add(json_bfd
, "type", "multi hop");
1044 json_object_string_add(json_bfd
, "type", "single hop");
1046 vty_out(vty
, " BFD: Type: %s\n",
1047 bsp
->args
.mhop
? "multi hop" : "single hop");
1049 /* Show configuration. */
1051 json_object_int_add(json_bfd
, "detectMultiplier",
1052 bsp
->args
.detection_multiplier
);
1053 json_object_int_add(json_bfd
, "rxMinInterval",
1055 json_object_int_add(json_bfd
, "txMinInterval",
1059 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
1060 bsp
->args
.detection_multiplier
, bsp
->args
.min_rx
,
1064 bfd_last_update(bsp
->bss
.last_event
, time_buf
, sizeof(time_buf
));
1066 json_object_string_add(json_bfd
, "status",
1067 bfd_get_status_str(bsp
->bss
.state
));
1068 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
1070 vty_out(vty
, " Status: %s, Last update: %s\n",
1071 bfd_get_status_str(bsp
->bss
.state
), time_buf
);
1074 json_object_object_add(json
, "peerBfdInfo", json_bfd
);
1080 * Zebra communication related.
1084 * Callback for reinstallation of all registered BFD sessions.
1086 * Use this as `zclient` `bfd_dest_replay` callback.
1088 static int zclient_bfd_session_reply(ZAPI_CALLBACK_ARGS
)
1090 struct bfd_session_params
*bsp
;
1092 /* Do nothing when shutting down. */
1093 if (bsglobal
.shutting_down
)
1096 if (bsglobal
.debugging
)
1097 zlog_debug("%s: sending all sessions registered", __func__
);
1099 /* Send the client registration */
1100 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
1102 /* Replay all activated peers. */
1103 TAILQ_FOREACH (bsp
, &bsglobal
.bsplist
, entry
) {
1104 /* Skip not installed sessions. */
1105 if (!bsp
->installed
)
1108 /* We are reconnecting, so we must send installation. */
1109 bsp
->installed
= false;
1111 /* Cancel any pending installation request. */
1112 THREAD_OFF(bsp
->installev
);
1114 /* Ask for installation. */
1115 bsp
->lastev
= BSE_INSTALL
;
1116 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
1122 static int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS
)
1124 struct bfd_session_params
*bsp
, *bspn
;
1125 size_t sessions_updated
= 0;
1126 struct interface
*ifp
;
1127 int remote_cbit
= false;
1128 int state
= BFD_STATUS_UNKNOWN
;
1133 char ifstr
[128], cbitstr
[32];
1135 /* Do nothing when shutting down. */
1136 if (bsglobal
.shutting_down
)
1139 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &state
, &remote_cbit
,
1142 * When interface lookup fails or an invalid stream is read, we must
1143 * not proceed otherwise it will trigger an assertion while checking
1144 * family type below.
1146 if (dp
.family
== 0 || sp
.family
== 0)
1149 if (bsglobal
.debugging
) {
1152 snprintf(ifstr
, sizeof(ifstr
), " (interface %s)",
1155 snprintf(cbitstr
, sizeof(cbitstr
), " (CPI bit %s)",
1156 remote_cbit
? "yes" : "no");
1158 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__
, &sp
,
1159 &dp
, ifstr
, vrf_id_to_name(vrf_id
), vrf_id
, cbitstr
,
1160 bfd_get_status_str(state
));
1163 switch (dp
.family
) {
1165 addrlen
= sizeof(struct in_addr
);
1168 addrlen
= sizeof(struct in6_addr
);
1172 /* Unexpected value. */
1177 /* Cache current time to avoid multiple monotime clock calls. */
1178 now
= monotime(NULL
);
1180 /* Notify all matching sessions about update. */
1181 TAILQ_FOREACH_SAFE (bsp
, &bsglobal
.bsplist
, entry
, bspn
) {
1182 /* Skip not installed entries. */
1183 if (!bsp
->installed
)
1185 /* Skip different VRFs. */
1186 if (bsp
->args
.vrf_id
!= vrf_id
)
1188 /* Skip different families. */
1189 if (bsp
->args
.family
!= dp
.family
)
1191 /* Skip different interface. */
1192 if (bsp
->args
.ifnamelen
&& ifp
1193 && strcmp(bsp
->args
.ifname
, ifp
->name
) != 0)
1195 /* Skip non matching destination addresses. */
1196 if (memcmp(&bsp
->args
.dst
, &dp
.u
, addrlen
) != 0)
1199 * Source comparison test:
1200 * We will only compare source if BFD daemon provided the
1201 * source address and the protocol set a source address in
1202 * the configuration otherwise we'll just skip it.
1204 if (sp
.family
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
) != 0
1205 && memcmp(&sp
.u
, &i6a_zero
, addrlen
) != 0
1206 && memcmp(&bsp
->args
.src
, &sp
.u
, addrlen
) != 0)
1208 /* No session state change. */
1209 if ((int)bsp
->bss
.state
== state
)
1212 bsp
->bss
.last_event
= now
;
1213 bsp
->bss
.previous_state
= bsp
->bss
.state
;
1214 bsp
->bss
.state
= state
;
1215 bsp
->bss
.remote_cbit
= remote_cbit
;
1216 bsp
->updatecb(bsp
, &bsp
->bss
, bsp
->arg
);
1220 if (bsglobal
.debugging
)
1221 zlog_debug("%s: sessions updated: %zu", __func__
,
1227 void bfd_protocol_integration_init(struct zclient
*zc
, struct thread_master
*tm
)
1229 /* Initialize data structure. */
1230 TAILQ_INIT(&bsglobal
.bsplist
);
1232 /* Copy pointers. */
1236 /* Install our callbacks. */
1237 zc
->interface_bfd_dest_update
= zclient_bfd_session_update
;
1238 zc
->bfd_dest_replay
= zclient_bfd_session_reply
;
1240 /* Send the client registration */
1241 bfd_client_sendmsg(zc
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
1244 void bfd_protocol_integration_set_debug(bool enable
)
1246 bsglobal
.debugging
= enable
;
1249 void bfd_protocol_integration_set_shutdown(bool enable
)
1251 bsglobal
.shutting_down
= enable
;
1254 bool bfd_protocol_integration_debug(void)
1256 return bsglobal
.debugging
;
1259 bool bfd_protocol_integration_shutting_down(void)
1261 return bsglobal
.shutting_down
;