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");
39 * BFD protocol integration configuration.
42 /** Events definitions. */
43 enum bfd_session_event
{
44 /** Remove the BFD session configuration. */
46 /** Install the BFD session configuration. */
51 * Data structure to do the necessary tricks to hide the BFD protocol
52 * integration internals.
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. */
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.
72 enum bfd_session_event lastev
;
74 * BFD session configuration event.
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.
80 struct thread
*installev
;
82 /** BFD session installation state. */
85 /** Global BFD paramaters list. */
86 TAILQ_ENTRY(bfd_session_params
) entry
;
89 struct bfd_sessions_global
{
91 * Global BFD session parameters list for (re)installation and update
92 * without code duplication among daemons.
94 TAILQ_HEAD(bsplist
, bfd_session_params
) bsplist
;
96 /** Pointer to FRR's event manager. */
97 struct thread_master
*tm
;
98 /** Pointer to zebra client data structure. */
101 /** Debugging state. */
103 /** Is shutting down? */
107 /** Global configuration variable. */
108 static struct bfd_sessions_global bsglobal
;
110 /** Global empty address for IPv4/IPv6. */
111 static const struct in6_addr i6a_zero
;
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.
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
)
121 unsigned int ifindex
;
122 struct interface
*ifp
= NULL
;
124 int local_remote_cbit
;
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
134 memset(dp
, 0, sizeof(*dp
));
135 memset(sp
, 0, sizeof(*sp
));
137 /* Get interface index. */
138 STREAM_GETL(s
, ifindex
);
142 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
144 if (bsglobal
.debugging
)
146 "%s: Can't find interface by ifindex: %d ",
152 /* Fetch destination address. */
153 STREAM_GETC(s
, dp
->family
);
155 plen
= prefix_blen(dp
);
156 STREAM_GET(&dp
->u
.prefix
, s
, plen
);
157 STREAM_GETC(s
, dp
->prefixlen
);
159 /* Get BFD status. */
160 STREAM_GETL(s
, (*status
));
162 STREAM_GETC(s
, sp
->family
);
164 plen
= prefix_blen(sp
);
165 STREAM_GET(&sp
->u
.prefix
, s
, plen
);
166 STREAM_GETC(s
, sp
->prefixlen
);
168 STREAM_GETC(s
, local_remote_cbit
);
170 *remote_cbit
= local_remote_cbit
;
175 * Clean dp and sp because caller
176 * will immediately check them valid or not
178 memset(dp
, 0, sizeof(*dp
));
179 memset(sp
, 0, sizeof(*sp
));
184 * bfd_get_status_str - Convert BFD status to a display string.
186 const char *bfd_get_status_str(int status
)
189 case BFD_STATUS_DOWN
:
193 case BFD_STATUS_ADMIN_DOWN
:
195 case BFD_STATUS_UNKNOWN
:
202 * bfd_last_update - Calculate the last BFD update time and convert it
203 * into a dd:hh:mm:ss display format.
205 static void bfd_last_update(time_t last_update
, char *buf
, size_t len
)
212 /* If no BFD status update has ever been received, print `never'. */
213 if (last_update
== 0) {
214 snprintf(buf
, len
, "never");
218 /* Get current time. */
221 diff
= curr
- last_update
;
222 gmtime_r(&diff
, &tm
);
224 snprintf(buf
, len
, "%d:%02d:%02d:%02d", tm
.tm_yday
, tm
.tm_hour
,
225 tm
.tm_min
, tm
.tm_sec
);
229 * bfd_client_sendmsg - Format and send a client register
230 * command to Zebra to be forwarded to BFD
232 void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
236 enum zclient_send_status ret
;
239 if (!zclient
|| zclient
->sock
< 0) {
240 if (bsglobal
.debugging
)
242 "%s: Can't send BFD client register, Zebra client not established",
249 zclient_create_header(s
, command
, vrf_id
);
251 stream_putl(s
, getpid());
253 stream_putw_at(s
, 0, stream_get_endp(s
));
255 ret
= zclient_send_message(zclient
);
257 if (ret
== ZCLIENT_SEND_FAILURE
) {
258 if (bsglobal
.debugging
)
260 "%s: %ld: zclient_send_message() failed",
261 __func__
, (long)getpid());
268 int zclient_bfd_command(struct zclient
*zc
, struct bfd_session_arg
*args
)
273 /* Individual reg/dereg messages are suppressed during shutdown. */
274 if (bsglobal
.shutting_down
) {
275 if (bsglobal
.debugging
)
277 "%s: Suppressing BFD peer reg/dereg messages",
283 if (!zc
|| zc
->sock
< 0) {
284 if (bsglobal
.debugging
)
285 zlog_debug("%s: zclient unavailable", __func__
);
292 /* Create new message. */
293 zclient_create_header(s
, args
->command
, args
->vrf_id
);
294 stream_putl(s
, getpid());
296 /* Encode destination address. */
297 stream_putw(s
, args
->family
);
298 addrlen
= (args
->family
== AF_INET
) ? sizeof(struct in_addr
)
299 : sizeof(struct in6_addr
);
300 stream_put(s
, &args
->dst
, addrlen
);
303 * For more BFD integration protocol details, see function
304 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
307 /* Session timers. */
308 stream_putl(s
, args
->min_rx
);
309 stream_putl(s
, args
->min_tx
);
310 stream_putc(s
, args
->detection_multiplier
);
313 stream_putc(s
, args
->mhop
!= 0);
315 /* Source address. */
316 stream_putw(s
, args
->family
);
317 stream_put(s
, &args
->src
, addrlen
);
319 /* Send the expected hops. */
320 stream_putc(s
, args
->hops
);
322 /* Send interface name if any. */
324 /* Don't send interface. */
326 if (bsglobal
.debugging
&& args
->ifnamelen
)
327 zlog_debug("%s: multi hop is configured, not sending interface",
330 stream_putc(s
, args
->ifnamelen
);
332 stream_put(s
, args
->ifname
, args
->ifnamelen
);
335 /* Send the C bit indicator. */
336 stream_putc(s
, args
->cbit
);
338 /* Send profile name if any. */
339 stream_putc(s
, args
->profilelen
);
340 if (args
->profilelen
)
341 stream_put(s
, args
->profile
, args
->profilelen
);
343 /* Encode timers if this is a registration message. */
344 if (args
->command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
345 stream_putl(s
, args
->min_rx
);
346 stream_putl(s
, args
->min_tx
);
347 stream_putc(s
, args
->detection_multiplier
);
351 /* Multi hop indicator. */
354 /* Multi hop always sends the source address. */
355 stream_putw(s
, args
->family
);
356 stream_put(s
, &args
->src
, addrlen
);
358 /* Send the expected hops. */
359 stream_putc(s
, args
->hops
);
361 /* Multi hop indicator. */
364 /* Single hop only sends the source address when IPv6. */
365 if (args
->family
== AF_INET6
) {
366 stream_putw(s
, args
->family
);
367 stream_put(s
, &args
->src
, addrlen
);
370 /* Send interface name if any. */
371 stream_putc(s
, args
->ifnamelen
);
373 stream_put(s
, args
->ifname
, args
->ifnamelen
);
376 /* Send the C bit indicator. */
377 stream_putc(s
, args
->cbit
);
378 #endif /* HAVE_BFDD */
380 /* Finish the message by writing the size. */
381 stream_putw_at(s
, 0, stream_get_endp(s
));
383 /* Send message to zebra. */
384 if (zclient_send_message(zc
) == ZCLIENT_SEND_FAILURE
) {
385 if (bsglobal
.debugging
)
386 zlog_debug("%s: zclient_send_message failed", __func__
);
393 struct bfd_session_params
*bfd_sess_new(bsp_status_update updatecb
, void *arg
)
395 struct bfd_session_params
*bsp
;
397 bsp
= XCALLOC(MTYPE_BFD_INFO
, sizeof(*bsp
));
399 /* Save application data. */
400 bsp
->updatecb
= updatecb
;
404 bsp
->args
.detection_multiplier
= BFD_DEF_DETECT_MULT
;
406 bsp
->args
.min_rx
= BFD_DEF_MIN_RX
;
407 bsp
->args
.min_tx
= BFD_DEF_MIN_TX
;
408 bsp
->args
.vrf_id
= VRF_DEFAULT
;
410 /* Register in global list. */
411 TAILQ_INSERT_TAIL(&bsglobal
.bsplist
, bsp
, entry
);
416 static bool _bfd_sess_valid(const struct bfd_session_params
*bsp
)
418 /* Peer/local address not configured. */
419 if (bsp
->args
.family
== 0)
422 /* Address configured but invalid. */
423 if (bsp
->args
.family
!= AF_INET
&& bsp
->args
.family
!= AF_INET6
) {
424 if (bsglobal
.debugging
)
425 zlog_debug("%s: invalid session family: %d", __func__
,
430 /* Invalid address. */
431 if (memcmp(&bsp
->args
.dst
, &i6a_zero
, sizeof(i6a_zero
)) == 0) {
432 if (bsglobal
.debugging
) {
433 if (bsp
->args
.family
== AF_INET
)
434 zlog_debug("%s: invalid address: %pI4",
436 (struct in_addr
*)&bsp
->args
.dst
);
438 zlog_debug("%s: invalid address: %pI6",
439 __func__
, &bsp
->args
.dst
);
444 /* Multi hop requires local address. */
446 && memcmp(&i6a_zero
, &bsp
->args
.src
, sizeof(i6a_zero
)) == 0) {
447 if (bsglobal
.debugging
)
449 "%s: multi hop but no local address provided",
455 if (bsp
->args
.vrf_id
== VRF_UNKNOWN
) {
456 if (bsglobal
.debugging
)
457 zlog_debug("%s: asked for unknown VRF", __func__
);
464 static int _bfd_sess_send(struct thread
*t
)
466 struct bfd_session_params
*bsp
= THREAD_ARG(t
);
469 /* Validate configuration before trying to send bogus data. */
470 if (!_bfd_sess_valid(bsp
))
473 if (bsp
->lastev
== BSE_INSTALL
) {
474 bsp
->args
.command
= bsp
->installed
? ZEBRA_BFD_DEST_UPDATE
475 : ZEBRA_BFD_DEST_REGISTER
;
477 bsp
->args
.command
= ZEBRA_BFD_DEST_DEREGISTER
;
479 /* If not installed and asked for uninstall, do nothing. */
480 if (!bsp
->installed
&& bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
483 rv
= zclient_bfd_command(bsglobal
.zc
, &bsp
->args
);
484 /* Command was sent successfully. */
486 /* Update installation status. */
487 if (bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
488 bsp
->installed
= false;
489 else if (bsp
->args
.command
== ZEBRA_BFD_DEST_REGISTER
)
490 bsp
->installed
= true;
492 struct ipaddr src
, dst
;
494 src
.ipa_type
= bsp
->args
.family
;
495 src
.ipaddr_v6
= bsp
->args
.src
;
496 dst
.ipa_type
= bsp
->args
.family
;
497 dst
.ipaddr_v6
= bsp
->args
.dst
;
500 "%s: BFD session %pIA -> %pIA interface %s VRF %s(%u) was not %s",
501 __func__
, &src
, &dst
,
502 bsp
->args
.ifnamelen
? bsp
->args
.ifname
: "*",
503 vrf_id_to_name(bsp
->args
.vrf_id
), bsp
->args
.vrf_id
,
504 bsp
->lastev
== BSE_INSTALL
? "installed"
511 static void _bfd_sess_remove(struct bfd_session_params
*bsp
)
513 /* Not installed, nothing to do. */
517 /* Cancel any pending installation request. */
518 THREAD_OFF(bsp
->installev
);
520 /* Send request to remove any session. */
521 bsp
->lastev
= BSE_UNINSTALL
;
522 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
525 void bfd_sess_free(struct bfd_session_params
**bsp
)
530 /* Remove any installed session. */
531 _bfd_sess_remove(*bsp
);
533 /* Remove from global list. */
534 TAILQ_REMOVE(&bsglobal
.bsplist
, (*bsp
), entry
);
536 /* Free the memory and point to NULL. */
537 XFREE(MTYPE_BFD_INFO
, (*bsp
));
540 static bool bfd_sess_address_changed(const struct bfd_session_params
*bsp
,
542 const struct in6_addr
*src
,
543 const struct in6_addr
*dst
)
547 if (bsp
->args
.family
!= family
)
550 addrlen
= (family
== AF_INET
) ? sizeof(struct in_addr
)
551 : sizeof(struct in6_addr
);
552 if ((src
== NULL
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
))
553 || (src
&& memcmp(src
, &bsp
->args
.src
, addrlen
))
554 || memcmp(dst
, &bsp
->args
.dst
, addrlen
))
560 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
561 const struct in_addr
*src
,
562 const struct in_addr
*dst
)
564 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
565 (struct in6_addr
*)dst
))
568 /* If already installed, remove the old setting. */
569 _bfd_sess_remove(bsp
);
571 bsp
->args
.family
= AF_INET
;
573 /* Clean memory, set zero value and avoid static analyser warnings. */
574 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
575 memset(&bsp
->args
.dst
, 0, sizeof(bsp
->args
.dst
));
577 /* Copy the equivalent of IPv4 to arguments structure. */
579 memcpy(&bsp
->args
.src
, src
, sizeof(struct in_addr
));
582 memcpy(&bsp
->args
.dst
, dst
, sizeof(struct in_addr
));
585 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
586 const struct in6_addr
*src
,
587 const struct in6_addr
*dst
)
589 if (!bfd_sess_address_changed(bsp
, AF_INET6
, src
, dst
))
592 /* If already installed, remove the old setting. */
593 _bfd_sess_remove(bsp
);
595 bsp
->args
.family
= AF_INET6
;
597 /* Clean memory, set zero value and avoid static analyser warnings. */
598 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
601 bsp
->args
.src
= *src
;
604 bsp
->args
.dst
= *dst
;
607 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
)
609 if ((ifname
== NULL
&& bsp
->args
.ifnamelen
== 0)
610 || (ifname
&& strcmp(bsp
->args
.ifname
, ifname
) == 0))
613 /* If already installed, remove the old setting. */
614 _bfd_sess_remove(bsp
);
616 if (ifname
== NULL
) {
617 bsp
->args
.ifname
[0] = 0;
618 bsp
->args
.ifnamelen
= 0;
622 if (strlcpy(bsp
->args
.ifname
, ifname
, sizeof(bsp
->args
.ifname
))
623 > sizeof(bsp
->args
.ifname
))
624 zlog_warn("%s: interface name truncated: %s", __func__
, ifname
);
626 bsp
->args
.ifnamelen
= strlen(bsp
->args
.ifname
);
629 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
)
631 if (profile
== NULL
) {
632 bsp
->args
.profile
[0] = 0;
633 bsp
->args
.profilelen
= 0;
637 if (strlcpy(bsp
->args
.profile
, profile
, sizeof(bsp
->args
.profile
))
638 > sizeof(bsp
->args
.profile
))
639 zlog_warn("%s: profile name truncated: %s", __func__
, profile
);
641 bsp
->args
.profilelen
= strlen(bsp
->args
.profile
);
644 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
)
646 if (bsp
->args
.vrf_id
== vrf_id
)
649 /* If already installed, remove the old setting. */
650 _bfd_sess_remove(bsp
);
652 bsp
->args
.vrf_id
= vrf_id
;
655 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t hops
)
657 if (bsp
->args
.hops
== hops
)
660 /* If already installed, remove the old setting. */
661 _bfd_sess_remove(bsp
);
663 bsp
->args
.hops
= hops
;
664 bsp
->args
.mhop
= (hops
> 1);
668 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
)
670 bsp
->args
.cbit
= enable
;
673 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
674 uint8_t detection_multiplier
, uint32_t min_rx
,
677 bsp
->args
.detection_multiplier
= detection_multiplier
;
678 bsp
->args
.min_rx
= min_rx
;
679 bsp
->args
.min_tx
= min_tx
;
682 void bfd_sess_install(struct bfd_session_params
*bsp
)
684 bsp
->lastev
= BSE_INSTALL
;
685 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
688 void bfd_sess_uninstall(struct bfd_session_params
*bsp
)
690 bsp
->lastev
= BSE_UNINSTALL
;
691 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
694 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
)
696 return bsp
->bss
.state
;
699 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
)
701 return bsp
->args
.hops
;
704 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
)
706 return bsp
->args
.profilelen
? bsp
->args
.profile
: NULL
;
709 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
710 struct in6_addr
*src
, struct in6_addr
*dst
)
712 *family
= bsp
->args
.family
;
714 *src
= bsp
->args
.src
;
716 *dst
= bsp
->args
.dst
;
719 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
)
721 if (bsp
->args
.ifnamelen
)
722 return bsp
->args
.ifname
;
727 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
)
729 return vrf_id_to_name(bsp
->args
.vrf_id
);
732 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
)
734 return bsp
->args
.vrf_id
;
737 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
)
739 return bsp
->args
.cbit
;
742 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
743 uint8_t *detection_multiplier
, uint32_t *min_rx
,
746 *detection_multiplier
= bsp
->args
.detection_multiplier
;
747 *min_rx
= bsp
->args
.min_rx
;
748 *min_tx
= bsp
->args
.min_tx
;
751 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
752 struct bfd_session_params
*bsp
)
754 json_object
*json_bfd
= NULL
;
762 json_bfd
= json_object_new_object();
764 json_object_string_add(json_bfd
, "type", "multi hop");
766 json_object_string_add(json_bfd
, "type", "single hop");
768 vty_out(vty
, " BFD: Type: %s\n",
769 bsp
->args
.mhop
? "multi hop" : "single hop");
771 /* Show configuration. */
773 json_object_int_add(json_bfd
, "detectMultiplier",
774 bsp
->args
.detection_multiplier
);
775 json_object_int_add(json_bfd
, "rxMinInterval",
777 json_object_int_add(json_bfd
, "txMinInterval",
781 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
782 bsp
->args
.detection_multiplier
, bsp
->args
.min_rx
,
786 bfd_last_update(bsp
->bss
.last_event
, time_buf
, sizeof(time_buf
));
788 json_object_string_add(json_bfd
, "status",
789 bfd_get_status_str(bsp
->bss
.state
));
790 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
792 vty_out(vty
, " Status: %s, Last update: %s\n",
793 bfd_get_status_str(bsp
->bss
.state
), time_buf
);
796 json_object_object_add(json
, "peerBfdInfo", json_bfd
);
802 * Zebra communication related.
806 * Callback for reinstallation of all registered BFD sessions.
808 * Use this as `zclient` `bfd_dest_replay` callback.
810 int zclient_bfd_session_replay(ZAPI_CALLBACK_ARGS
)
812 struct bfd_session_params
*bsp
;
814 if (!zclient
->bfd_integration
)
817 /* Do nothing when shutting down. */
818 if (bsglobal
.shutting_down
)
821 if (bsglobal
.debugging
)
822 zlog_debug("%s: sending all sessions registered", __func__
);
824 /* Send the client registration */
825 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
827 /* Replay all activated peers. */
828 TAILQ_FOREACH (bsp
, &bsglobal
.bsplist
, entry
) {
829 /* Skip not installed sessions. */
833 /* We are reconnecting, so we must send installation. */
834 bsp
->installed
= false;
836 /* Cancel any pending installation request. */
837 THREAD_OFF(bsp
->installev
);
839 /* Ask for installation. */
840 bsp
->lastev
= BSE_INSTALL
;
841 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
847 int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS
)
849 struct bfd_session_params
*bsp
, *bspn
;
850 size_t sessions_updated
= 0;
851 struct interface
*ifp
;
852 int remote_cbit
= false;
853 int state
= BFD_STATUS_UNKNOWN
;
858 char ifstr
[128], cbitstr
[32];
860 if (!zclient
->bfd_integration
)
863 /* Do nothing when shutting down. */
864 if (bsglobal
.shutting_down
)
867 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &state
, &remote_cbit
,
870 * When interface lookup fails or an invalid stream is read, we must
871 * not proceed otherwise it will trigger an assertion while checking
874 if (dp
.family
== 0 || sp
.family
== 0)
877 if (bsglobal
.debugging
) {
880 snprintf(ifstr
, sizeof(ifstr
), " (interface %s)",
883 snprintf(cbitstr
, sizeof(cbitstr
), " (CPI bit %s)",
884 remote_cbit
? "yes" : "no");
886 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__
, &sp
,
887 &dp
, ifstr
, vrf_id_to_name(vrf_id
), vrf_id
, cbitstr
,
888 bfd_get_status_str(state
));
893 addrlen
= sizeof(struct in_addr
);
896 addrlen
= sizeof(struct in6_addr
);
900 /* Unexpected value. */
905 /* Cache current time to avoid multiple monotime clock calls. */
906 now
= monotime(NULL
);
908 /* Notify all matching sessions about update. */
909 TAILQ_FOREACH_SAFE (bsp
, &bsglobal
.bsplist
, entry
, bspn
) {
910 /* Skip not installed entries. */
913 /* Skip different VRFs. */
914 if (bsp
->args
.vrf_id
!= vrf_id
)
916 /* Skip different families. */
917 if (bsp
->args
.family
!= dp
.family
)
919 /* Skip different interface. */
920 if (bsp
->args
.ifnamelen
&& ifp
921 && strcmp(bsp
->args
.ifname
, ifp
->name
) != 0)
923 /* Skip non matching destination addresses. */
924 if (memcmp(&bsp
->args
.dst
, &dp
.u
, addrlen
) != 0)
927 * Source comparison test:
928 * We will only compare source if BFD daemon provided the
929 * source address and the protocol set a source address in
930 * the configuration otherwise we'll just skip it.
932 if (sp
.family
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
) != 0
933 && memcmp(&sp
.u
, &i6a_zero
, addrlen
) != 0
934 && memcmp(&bsp
->args
.src
, &sp
.u
, addrlen
) != 0)
936 /* No session state change. */
937 if ((int)bsp
->bss
.state
== state
)
940 bsp
->bss
.last_event
= now
;
941 bsp
->bss
.previous_state
= bsp
->bss
.state
;
942 bsp
->bss
.state
= state
;
943 bsp
->bss
.remote_cbit
= remote_cbit
;
944 bsp
->updatecb(bsp
, &bsp
->bss
, bsp
->arg
);
948 if (bsglobal
.debugging
)
949 zlog_debug("%s: sessions updated: %zu", __func__
,
955 void bfd_protocol_integration_init(struct zclient
*zc
, struct thread_master
*tm
)
957 /* Initialize data structure. */
958 TAILQ_INIT(&bsglobal
.bsplist
);
964 /* Enable BFD callbacks. */
965 zc
->bfd_integration
= true;
967 /* Send the client registration */
968 bfd_client_sendmsg(zc
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
971 void bfd_protocol_integration_set_debug(bool enable
)
973 bsglobal
.debugging
= enable
;
976 void bfd_protocol_integration_set_shutdown(bool enable
)
978 bsglobal
.shutting_down
= enable
;
981 bool bfd_protocol_integration_debug(void)
983 return bsglobal
.debugging
;
986 bool bfd_protocol_integration_shutting_down(void)
988 return bsglobal
.shutting_down
;