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 void _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"
509 static void _bfd_sess_remove(struct bfd_session_params
*bsp
)
511 /* Not installed, nothing to do. */
515 /* Cancel any pending installation request. */
516 THREAD_OFF(bsp
->installev
);
518 /* Send request to remove any session. */
519 bsp
->lastev
= BSE_UNINSTALL
;
520 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
523 void bfd_sess_free(struct bfd_session_params
**bsp
)
528 /* Remove any installed session. */
529 _bfd_sess_remove(*bsp
);
531 /* Remove from global list. */
532 TAILQ_REMOVE(&bsglobal
.bsplist
, (*bsp
), entry
);
534 /* Free the memory and point to NULL. */
535 XFREE(MTYPE_BFD_INFO
, (*bsp
));
538 static bool bfd_sess_address_changed(const struct bfd_session_params
*bsp
,
540 const struct in6_addr
*src
,
541 const struct in6_addr
*dst
)
545 if (bsp
->args
.family
!= family
)
548 addrlen
= (family
== AF_INET
) ? sizeof(struct in_addr
)
549 : sizeof(struct in6_addr
);
550 if ((src
== NULL
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
))
551 || (src
&& memcmp(src
, &bsp
->args
.src
, addrlen
))
552 || memcmp(dst
, &bsp
->args
.dst
, addrlen
))
558 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
559 const struct in_addr
*src
,
560 const struct in_addr
*dst
)
562 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
563 (struct in6_addr
*)dst
))
566 /* If already installed, remove the old setting. */
567 _bfd_sess_remove(bsp
);
569 bsp
->args
.family
= AF_INET
;
571 /* Clean memory, set zero value and avoid static analyser warnings. */
572 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
573 memset(&bsp
->args
.dst
, 0, sizeof(bsp
->args
.dst
));
575 /* Copy the equivalent of IPv4 to arguments structure. */
577 memcpy(&bsp
->args
.src
, src
, sizeof(struct in_addr
));
580 memcpy(&bsp
->args
.dst
, dst
, sizeof(struct in_addr
));
583 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
584 const struct in6_addr
*src
,
585 const struct in6_addr
*dst
)
587 if (!bfd_sess_address_changed(bsp
, AF_INET6
, src
, dst
))
590 /* If already installed, remove the old setting. */
591 _bfd_sess_remove(bsp
);
593 bsp
->args
.family
= AF_INET6
;
595 /* Clean memory, set zero value and avoid static analyser warnings. */
596 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
599 bsp
->args
.src
= *src
;
602 bsp
->args
.dst
= *dst
;
605 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
)
607 if ((ifname
== NULL
&& bsp
->args
.ifnamelen
== 0)
608 || (ifname
&& strcmp(bsp
->args
.ifname
, ifname
) == 0))
611 /* If already installed, remove the old setting. */
612 _bfd_sess_remove(bsp
);
614 if (ifname
== NULL
) {
615 bsp
->args
.ifname
[0] = 0;
616 bsp
->args
.ifnamelen
= 0;
620 if (strlcpy(bsp
->args
.ifname
, ifname
, sizeof(bsp
->args
.ifname
))
621 > sizeof(bsp
->args
.ifname
))
622 zlog_warn("%s: interface name truncated: %s", __func__
, ifname
);
624 bsp
->args
.ifnamelen
= strlen(bsp
->args
.ifname
);
627 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
)
629 if (profile
== NULL
) {
630 bsp
->args
.profile
[0] = 0;
631 bsp
->args
.profilelen
= 0;
635 if (strlcpy(bsp
->args
.profile
, profile
, sizeof(bsp
->args
.profile
))
636 > sizeof(bsp
->args
.profile
))
637 zlog_warn("%s: profile name truncated: %s", __func__
, profile
);
639 bsp
->args
.profilelen
= strlen(bsp
->args
.profile
);
642 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
)
644 if (bsp
->args
.vrf_id
== vrf_id
)
647 /* If already installed, remove the old setting. */
648 _bfd_sess_remove(bsp
);
650 bsp
->args
.vrf_id
= vrf_id
;
653 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t hops
)
655 if (bsp
->args
.hops
== hops
)
658 /* If already installed, remove the old setting. */
659 _bfd_sess_remove(bsp
);
661 bsp
->args
.hops
= hops
;
662 bsp
->args
.mhop
= (hops
> 1);
666 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
)
668 bsp
->args
.cbit
= enable
;
671 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
672 uint8_t detection_multiplier
, uint32_t min_rx
,
675 bsp
->args
.detection_multiplier
= detection_multiplier
;
676 bsp
->args
.min_rx
= min_rx
;
677 bsp
->args
.min_tx
= min_tx
;
680 void bfd_sess_install(struct bfd_session_params
*bsp
)
682 bsp
->lastev
= BSE_INSTALL
;
683 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
686 void bfd_sess_uninstall(struct bfd_session_params
*bsp
)
688 bsp
->lastev
= BSE_UNINSTALL
;
689 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
692 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
)
694 return bsp
->bss
.state
;
697 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
)
699 return bsp
->args
.hops
;
702 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
)
704 return bsp
->args
.profilelen
? bsp
->args
.profile
: NULL
;
707 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
708 struct in6_addr
*src
, struct in6_addr
*dst
)
710 *family
= bsp
->args
.family
;
712 *src
= bsp
->args
.src
;
714 *dst
= bsp
->args
.dst
;
717 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
)
719 if (bsp
->args
.ifnamelen
)
720 return bsp
->args
.ifname
;
725 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
)
727 return vrf_id_to_name(bsp
->args
.vrf_id
);
730 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
)
732 return bsp
->args
.vrf_id
;
735 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
)
737 return bsp
->args
.cbit
;
740 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
741 uint8_t *detection_multiplier
, uint32_t *min_rx
,
744 *detection_multiplier
= bsp
->args
.detection_multiplier
;
745 *min_rx
= bsp
->args
.min_rx
;
746 *min_tx
= bsp
->args
.min_tx
;
749 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
750 struct bfd_session_params
*bsp
)
752 json_object
*json_bfd
= NULL
;
760 json_bfd
= json_object_new_object();
762 json_object_string_add(json_bfd
, "type", "multi hop");
764 json_object_string_add(json_bfd
, "type", "single hop");
766 vty_out(vty
, " BFD: Type: %s\n",
767 bsp
->args
.mhop
? "multi hop" : "single hop");
769 /* Show configuration. */
771 json_object_int_add(json_bfd
, "detectMultiplier",
772 bsp
->args
.detection_multiplier
);
773 json_object_int_add(json_bfd
, "rxMinInterval",
775 json_object_int_add(json_bfd
, "txMinInterval",
779 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
780 bsp
->args
.detection_multiplier
, bsp
->args
.min_rx
,
784 bfd_last_update(bsp
->bss
.last_event
, time_buf
, sizeof(time_buf
));
786 json_object_string_add(json_bfd
, "status",
787 bfd_get_status_str(bsp
->bss
.state
));
788 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
790 vty_out(vty
, " Status: %s, Last update: %s\n",
791 bfd_get_status_str(bsp
->bss
.state
), time_buf
);
794 json_object_object_add(json
, "peerBfdInfo", json_bfd
);
800 * Zebra communication related.
804 * Callback for reinstallation of all registered BFD sessions.
806 * Use this as `zclient` `bfd_dest_replay` callback.
808 int zclient_bfd_session_replay(ZAPI_CALLBACK_ARGS
)
810 struct bfd_session_params
*bsp
;
812 if (!zclient
->bfd_integration
)
815 /* Do nothing when shutting down. */
816 if (bsglobal
.shutting_down
)
819 if (bsglobal
.debugging
)
820 zlog_debug("%s: sending all sessions registered", __func__
);
822 /* Send the client registration */
823 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
825 /* Replay all activated peers. */
826 TAILQ_FOREACH (bsp
, &bsglobal
.bsplist
, entry
) {
827 /* Skip not installed sessions. */
831 /* We are reconnecting, so we must send installation. */
832 bsp
->installed
= false;
834 /* Cancel any pending installation request. */
835 THREAD_OFF(bsp
->installev
);
837 /* Ask for installation. */
838 bsp
->lastev
= BSE_INSTALL
;
839 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
845 int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS
)
847 struct bfd_session_params
*bsp
, *bspn
;
848 size_t sessions_updated
= 0;
849 struct interface
*ifp
;
850 int remote_cbit
= false;
851 int state
= BFD_STATUS_UNKNOWN
;
856 char ifstr
[128], cbitstr
[32];
858 if (!zclient
->bfd_integration
)
861 /* Do nothing when shutting down. */
862 if (bsglobal
.shutting_down
)
865 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &state
, &remote_cbit
,
868 * When interface lookup fails or an invalid stream is read, we must
869 * not proceed otherwise it will trigger an assertion while checking
872 if (dp
.family
== 0 || sp
.family
== 0)
875 if (bsglobal
.debugging
) {
878 snprintf(ifstr
, sizeof(ifstr
), " (interface %s)",
881 snprintf(cbitstr
, sizeof(cbitstr
), " (CPI bit %s)",
882 remote_cbit
? "yes" : "no");
884 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__
, &sp
,
885 &dp
, ifstr
, vrf_id_to_name(vrf_id
), vrf_id
, cbitstr
,
886 bfd_get_status_str(state
));
891 addrlen
= sizeof(struct in_addr
);
894 addrlen
= sizeof(struct in6_addr
);
898 /* Unexpected value. */
903 /* Cache current time to avoid multiple monotime clock calls. */
904 now
= monotime(NULL
);
906 /* Notify all matching sessions about update. */
907 TAILQ_FOREACH_SAFE (bsp
, &bsglobal
.bsplist
, entry
, bspn
) {
908 /* Skip not installed entries. */
911 /* Skip different VRFs. */
912 if (bsp
->args
.vrf_id
!= vrf_id
)
914 /* Skip different families. */
915 if (bsp
->args
.family
!= dp
.family
)
917 /* Skip different interface. */
918 if (bsp
->args
.ifnamelen
&& ifp
919 && strcmp(bsp
->args
.ifname
, ifp
->name
) != 0)
921 /* Skip non matching destination addresses. */
922 if (memcmp(&bsp
->args
.dst
, &dp
.u
, addrlen
) != 0)
925 * Source comparison test:
926 * We will only compare source if BFD daemon provided the
927 * source address and the protocol set a source address in
928 * the configuration otherwise we'll just skip it.
930 if (sp
.family
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
) != 0
931 && memcmp(&sp
.u
, &i6a_zero
, addrlen
) != 0
932 && memcmp(&bsp
->args
.src
, &sp
.u
, addrlen
) != 0)
934 /* No session state change. */
935 if ((int)bsp
->bss
.state
== state
)
938 bsp
->bss
.last_event
= now
;
939 bsp
->bss
.previous_state
= bsp
->bss
.state
;
940 bsp
->bss
.state
= state
;
941 bsp
->bss
.remote_cbit
= remote_cbit
;
942 bsp
->updatecb(bsp
, &bsp
->bss
, bsp
->arg
);
946 if (bsglobal
.debugging
)
947 zlog_debug("%s: sessions updated: %zu", __func__
,
953 void bfd_protocol_integration_init(struct zclient
*zc
, struct thread_master
*tm
)
955 /* Initialize data structure. */
956 TAILQ_INIT(&bsglobal
.bsplist
);
962 /* Enable BFD callbacks. */
963 zc
->bfd_integration
= true;
965 /* Send the client registration */
966 bfd_client_sendmsg(zc
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
969 void bfd_protocol_integration_set_debug(bool enable
)
971 bsglobal
.debugging
= enable
;
974 void bfd_protocol_integration_set_shutdown(bool enable
)
976 bsglobal
.shutting_down
= enable
;
979 bool bfd_protocol_integration_debug(void)
981 return bsglobal
.debugging
;
984 bool bfd_protocol_integration_shutting_down(void)
986 return bsglobal
.shutting_down
;