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 "zebra_interface_bfd_read: 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
;
178 * bfd_get_status_str - Convert BFD status to a display string.
180 const char *bfd_get_status_str(int status
)
183 case BFD_STATUS_DOWN
:
187 case BFD_STATUS_ADMIN_DOWN
:
189 case BFD_STATUS_UNKNOWN
:
196 * bfd_last_update - Calculate the last BFD update time and convert it
197 * into a dd:hh:mm:ss display format.
199 static void bfd_last_update(time_t last_update
, char *buf
, size_t len
)
206 /* If no BFD satatus update has ever been received, print `never'. */
207 if (last_update
== 0) {
208 snprintf(buf
, len
, "never");
212 /* Get current time. */
215 diff
= curr
- last_update
;
216 gmtime_r(&diff
, &tm
);
218 snprintf(buf
, len
, "%d:%02d:%02d:%02d", tm
.tm_yday
, tm
.tm_hour
,
219 tm
.tm_min
, tm
.tm_sec
);
223 * bfd_client_sendmsg - Format and send a client register
224 * command to Zebra to be forwarded to BFD
226 void bfd_client_sendmsg(struct zclient
*zclient
, int command
,
230 enum zclient_send_status ret
;
233 if (!zclient
|| zclient
->sock
< 0) {
234 if (bsglobal
.debugging
)
236 "%s: Can't send BFD client register, Zebra client not established",
243 zclient_create_header(s
, command
, vrf_id
);
245 stream_putl(s
, getpid());
247 stream_putw_at(s
, 0, stream_get_endp(s
));
249 ret
= zclient_send_message(zclient
);
251 if (ret
== ZCLIENT_SEND_FAILURE
) {
252 if (bsglobal
.debugging
)
254 "bfd_client_sendmsg %ld: zclient_send_message() failed",
262 int zclient_bfd_command(struct zclient
*zc
, struct bfd_session_arg
*args
)
267 /* Individual reg/dereg messages are suppressed during shutdown. */
268 if (bsglobal
.shutting_down
) {
269 if (bsglobal
.debugging
)
271 "%s: Suppressing BFD peer reg/dereg messages",
277 if (!zc
|| zc
->sock
< 0) {
278 if (bsglobal
.debugging
)
279 zlog_debug("%s: zclient unavailable", __func__
);
286 /* Create new message. */
287 zclient_create_header(s
, args
->command
, args
->vrf_id
);
288 stream_putl(s
, getpid());
290 /* Encode destination address. */
291 stream_putw(s
, args
->family
);
292 addrlen
= (args
->family
== AF_INET
) ? sizeof(struct in_addr
)
293 : sizeof(struct in6_addr
);
294 stream_put(s
, &args
->dst
, addrlen
);
297 * For more BFD integration protocol details, see function
298 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
301 /* Session timers. */
302 stream_putl(s
, args
->min_rx
);
303 stream_putl(s
, args
->min_tx
);
304 stream_putc(s
, args
->detection_multiplier
);
307 stream_putc(s
, args
->mhop
!= 0);
309 /* Source address. */
310 stream_putw(s
, args
->family
);
311 stream_put(s
, &args
->src
, addrlen
);
313 /* Send the expected TTL. */
314 stream_putc(s
, args
->ttl
);
316 /* Send interface name if any. */
318 /* Don't send interface. */
320 if (bsglobal
.debugging
&& args
->ifnamelen
)
321 zlog_debug("%s: multi hop is configured, not sending interface",
324 stream_putc(s
, args
->ifnamelen
);
326 stream_put(s
, args
->ifname
, args
->ifnamelen
);
329 /* Send the C bit indicator. */
330 stream_putc(s
, args
->cbit
);
332 /* Send profile name if any. */
333 stream_putc(s
, args
->profilelen
);
334 if (args
->profilelen
)
335 stream_put(s
, args
->profile
, args
->profilelen
);
337 /* Encode timers if this is a registration message. */
338 if (args
->command
!= ZEBRA_BFD_DEST_DEREGISTER
) {
339 stream_putl(s
, args
->min_rx
);
340 stream_putl(s
, args
->min_tx
);
341 stream_putc(s
, args
->detection_multiplier
);
345 /* Multi hop indicator. */
348 /* Multi hop always sends the source address. */
349 stream_putw(s
, args
->family
);
350 stream_put(s
, &args
->src
, addrlen
);
352 /* Send the expected TTL. */
353 stream_putc(s
, args
->ttl
);
355 /* Multi hop indicator. */
358 /* Single hop only sends the source address when IPv6. */
359 if (args
->family
== AF_INET6
) {
360 stream_putw(s
, args
->family
);
361 stream_put(s
, &args
->src
, addrlen
);
364 /* Send interface name if any. */
365 stream_putc(s
, args
->ifnamelen
);
367 stream_put(s
, args
->ifname
, args
->ifnamelen
);
369 #endif /* HAVE_BFDD */
371 /* Finish the message by writing the size. */
372 stream_putw_at(s
, 0, stream_get_endp(s
));
374 /* Send message to zebra. */
375 if (zclient_send_message(zc
) == ZCLIENT_SEND_FAILURE
) {
376 if (bsglobal
.debugging
)
377 zlog_debug("%s: zclient_send_message failed", __func__
);
384 struct bfd_session_params
*bfd_sess_new(bsp_status_update updatecb
, void *arg
)
386 struct bfd_session_params
*bsp
;
388 bsp
= XCALLOC(MTYPE_BFD_INFO
, sizeof(*bsp
));
390 /* Save application data. */
391 bsp
->updatecb
= updatecb
;
395 bsp
->args
.detection_multiplier
= BFD_DEF_DETECT_MULT
;
397 bsp
->args
.min_rx
= BFD_DEF_MIN_RX
;
398 bsp
->args
.min_tx
= BFD_DEF_MIN_TX
;
399 bsp
->args
.vrf_id
= VRF_DEFAULT
;
401 /* Register in global list. */
402 TAILQ_INSERT_TAIL(&bsglobal
.bsplist
, bsp
, entry
);
407 static bool _bfd_sess_valid(const struct bfd_session_params
*bsp
)
409 /* Peer/local address not configured. */
410 if (bsp
->args
.family
== 0)
413 /* Address configured but invalid. */
414 if (bsp
->args
.family
!= AF_INET
&& bsp
->args
.family
!= AF_INET6
) {
415 if (bsglobal
.debugging
)
416 zlog_debug("%s: invalid session family: %d", __func__
,
421 /* Invalid address. */
422 if (memcmp(&bsp
->args
.dst
, &i6a_zero
, sizeof(i6a_zero
)) == 0) {
423 if (bsglobal
.debugging
) {
424 if (bsp
->args
.family
== AF_INET
)
425 zlog_debug("%s: invalid address: %pI4",
427 (struct in_addr
*)&bsp
->args
.dst
);
429 zlog_debug("%s: invalid address: %pI6",
430 __func__
, &bsp
->args
.dst
);
435 /* Multi hop requires local address. */
437 && memcmp(&i6a_zero
, &bsp
->args
.src
, sizeof(i6a_zero
)) == 0) {
438 if (bsglobal
.debugging
)
440 "%s: multi hop but no local address provided",
446 if (bsp
->args
.vrf_id
== VRF_UNKNOWN
) {
447 if (bsglobal
.debugging
)
448 zlog_debug("%s: asked for unknown VRF", __func__
);
455 static int _bfd_sess_send(struct thread
*t
)
457 struct bfd_session_params
*bsp
= THREAD_ARG(t
);
460 /* Validate configuration before trying to send bogus data. */
461 if (!_bfd_sess_valid(bsp
))
464 if (bsp
->lastev
== BSE_INSTALL
) {
465 bsp
->args
.command
= bsp
->installed
? ZEBRA_BFD_DEST_UPDATE
466 : ZEBRA_BFD_DEST_REGISTER
;
468 bsp
->args
.command
= ZEBRA_BFD_DEST_DEREGISTER
;
470 /* If not installed and asked for uninstall, do nothing. */
471 if (!bsp
->installed
&& bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
474 rv
= zclient_bfd_command(bsglobal
.zc
, &bsp
->args
);
475 /* Command was sent successfully. */
477 /* Update installation status. */
478 if (bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
479 bsp
->installed
= false;
480 else if (bsp
->args
.command
== ZEBRA_BFD_DEST_REGISTER
)
481 bsp
->installed
= true;
483 struct ipaddr src
, dst
;
485 src
.ipa_type
= bsp
->args
.family
;
486 src
.ipaddr_v6
= bsp
->args
.src
;
487 dst
.ipa_type
= bsp
->args
.family
;
488 dst
.ipaddr_v6
= bsp
->args
.dst
;
491 "%s: BFD session %pIA -> %pIA interface %s VRF %s(%u) was not %s",
492 __func__
, &src
, &dst
,
493 bsp
->args
.ifnamelen
? bsp
->args
.ifname
: "*",
494 vrf_id_to_name(bsp
->args
.vrf_id
), bsp
->args
.vrf_id
,
495 bsp
->lastev
== BSE_INSTALL
? "installed"
502 static void _bfd_sess_remove(struct bfd_session_params
*bsp
)
504 /* Not installed, nothing to do. */
508 /* Cancel any pending installation request. */
509 THREAD_OFF(bsp
->installev
);
511 /* Send request to remove any session. */
512 bsp
->lastev
= BSE_UNINSTALL
;
513 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
516 void bfd_sess_free(struct bfd_session_params
**bsp
)
521 /* Remove any installed session. */
522 _bfd_sess_remove(*bsp
);
524 /* Remove from global list. */
525 TAILQ_REMOVE(&bsglobal
.bsplist
, (*bsp
), entry
);
527 /* Free the memory and point to NULL. */
528 XFREE(MTYPE_BFD_INFO
, (*bsp
));
531 static bool bfd_sess_address_changed(const struct bfd_session_params
*bsp
,
533 const struct in6_addr
*src
,
534 const struct in6_addr
*dst
)
538 if (bsp
->args
.family
!= family
)
541 addrlen
= (family
== AF_INET
) ? sizeof(struct in_addr
)
542 : sizeof(struct in6_addr
);
543 if ((src
== NULL
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
))
544 || (src
&& memcmp(src
, &bsp
->args
.src
, addrlen
))
545 || memcmp(dst
, &bsp
->args
.dst
, addrlen
))
551 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
552 struct in_addr
*src
, struct in_addr
*dst
)
554 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
555 (struct in6_addr
*)dst
))
558 /* If already installed, remove the old setting. */
559 _bfd_sess_remove(bsp
);
561 bsp
->args
.family
= AF_INET
;
563 /* Clean memory, set zero value and avoid static analyser warnings. */
564 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
565 memset(&bsp
->args
.dst
, 0, sizeof(bsp
->args
.dst
));
567 /* Copy the equivalent of IPv4 to arguments structure. */
569 memcpy(&bsp
->args
.src
, src
, sizeof(struct in_addr
));
572 memcpy(&bsp
->args
.dst
, dst
, sizeof(struct in_addr
));
575 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
576 struct in6_addr
*src
, struct in6_addr
*dst
)
578 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
579 (struct in6_addr
*)dst
))
582 /* If already installed, remove the old setting. */
583 _bfd_sess_remove(bsp
);
585 bsp
->args
.family
= AF_INET6
;
587 /* Clean memory, set zero value and avoid static analyser warnings. */
588 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
591 bsp
->args
.src
= *src
;
594 bsp
->args
.dst
= *dst
;
597 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
)
599 if ((ifname
== NULL
&& bsp
->args
.ifnamelen
== 0)
600 || (ifname
&& strcmp(bsp
->args
.ifname
, ifname
) == 0))
603 /* If already installed, remove the old setting. */
604 _bfd_sess_remove(bsp
);
606 if (ifname
== NULL
) {
607 bsp
->args
.ifname
[0] = 0;
608 bsp
->args
.ifnamelen
= 0;
612 if (strlcpy(bsp
->args
.ifname
, ifname
, sizeof(bsp
->args
.ifname
))
613 > sizeof(bsp
->args
.ifname
))
614 zlog_warn("%s: interface name truncated: %s", __func__
, ifname
);
616 bsp
->args
.ifnamelen
= strlen(bsp
->args
.ifname
);
619 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
)
621 if (profile
== NULL
) {
622 bsp
->args
.profile
[0] = 0;
623 bsp
->args
.profilelen
= 0;
627 if (strlcpy(bsp
->args
.profile
, profile
, sizeof(bsp
->args
.profile
))
628 > sizeof(bsp
->args
.profile
))
629 zlog_warn("%s: profile name truncated: %s", __func__
, profile
);
631 bsp
->args
.profilelen
= strlen(bsp
->args
.profile
);
634 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
)
636 if (bsp
->args
.vrf_id
== vrf_id
)
639 /* If already installed, remove the old setting. */
640 _bfd_sess_remove(bsp
);
642 bsp
->args
.vrf_id
= vrf_id
;
645 void bfd_sess_set_mininum_ttl(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
647 assert(min_ttl
!= 0);
649 if (bsp
->args
.ttl
== ((BFD_SINGLE_HOP_TTL
+ 1) - min_ttl
))
652 /* If already installed, remove the old setting. */
653 _bfd_sess_remove(bsp
);
655 /* Invert TTL value: protocol expects number of hops. */
656 min_ttl
= (BFD_SINGLE_HOP_TTL
+ 1) - min_ttl
;
657 bsp
->args
.ttl
= min_ttl
;
658 bsp
->args
.mhop
= (min_ttl
> 1);
661 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
663 if (bsp
->args
.ttl
== min_ttl
)
666 /* If already installed, remove the old setting. */
667 _bfd_sess_remove(bsp
);
669 bsp
->args
.ttl
= min_ttl
;
670 bsp
->args
.mhop
= (min_ttl
> 1);
674 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
)
676 bsp
->args
.cbit
= enable
;
679 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
680 uint8_t detection_multiplier
, uint32_t min_rx
,
683 bsp
->args
.detection_multiplier
= detection_multiplier
;
684 bsp
->args
.min_rx
= min_rx
;
685 bsp
->args
.min_tx
= min_tx
;
688 void bfd_sess_install(struct bfd_session_params
*bsp
)
690 bsp
->lastev
= BSE_INSTALL
;
691 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
694 void bfd_sess_uninstall(struct bfd_session_params
*bsp
)
696 bsp
->lastev
= BSE_UNINSTALL
;
697 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
700 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
)
702 return bsp
->bss
.state
;
705 uint8_t bfd_sess_minimum_ttl(const struct bfd_session_params
*bsp
)
707 return ((BFD_SINGLE_HOP_TTL
+ 1) - bsp
->args
.ttl
);
710 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
)
712 return bsp
->args
.ttl
;
715 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
)
717 return bsp
->args
.profilelen
? bsp
->args
.profile
: NULL
;
720 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
721 struct in6_addr
*src
, struct in6_addr
*dst
)
723 *family
= bsp
->args
.family
;
725 *src
= bsp
->args
.src
;
727 *dst
= bsp
->args
.dst
;
730 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
)
732 if (bsp
->args
.ifnamelen
)
733 return bsp
->args
.ifname
;
738 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
)
740 return vrf_id_to_name(bsp
->args
.vrf_id
);
743 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
)
745 return bsp
->args
.vrf_id
;
748 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
)
750 return bsp
->args
.cbit
;
753 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
754 uint8_t *detection_multiplier
, uint32_t *min_rx
,
757 *detection_multiplier
= bsp
->args
.detection_multiplier
;
758 *min_rx
= bsp
->args
.min_rx
;
759 *min_tx
= bsp
->args
.min_tx
;
762 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
763 struct bfd_session_params
*bsp
)
765 json_object
*json_bfd
= NULL
;
773 json_bfd
= json_object_new_object();
775 json_object_string_add(json_bfd
, "type", "multi hop");
777 json_object_string_add(json_bfd
, "type", "single hop");
779 vty_out(vty
, " BFD: Type: %s\n",
780 bsp
->args
.mhop
? "multi hop" : "single hop");
782 /* Show configuration. */
784 json_object_int_add(json_bfd
, "detectMultiplier",
785 bsp
->args
.detection_multiplier
);
786 json_object_int_add(json_bfd
, "rxMinInterval",
788 json_object_int_add(json_bfd
, "txMinInterval",
792 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
793 bsp
->args
.detection_multiplier
, bsp
->args
.min_rx
,
797 bfd_last_update(bsp
->bss
.last_event
, time_buf
, sizeof(time_buf
));
799 json_object_string_add(json_bfd
, "status",
800 bfd_get_status_str(bsp
->bss
.state
));
801 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
803 vty_out(vty
, " Status: %s, Last update: %s\n",
804 bfd_get_status_str(bsp
->bss
.state
), time_buf
);
807 json_object_object_add(json
, "peerBfdInfo", json_bfd
);
813 * Zebra communication related.
817 * Callback for reinstallation of all registered BFD sessions.
819 * Use this as `zclient` `bfd_dest_replay` callback.
821 static int zclient_bfd_session_reply(ZAPI_CALLBACK_ARGS
)
823 struct bfd_session_params
*bsp
;
825 /* Do nothing when shutting down. */
826 if (bsglobal
.shutting_down
)
829 if (bsglobal
.debugging
)
830 zlog_debug("%s: sending all sessions registered", __func__
);
832 /* Send the client registration */
833 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
835 /* Replay all activated peers. */
836 TAILQ_FOREACH (bsp
, &bsglobal
.bsplist
, entry
) {
837 /* Skip not installed sessions. */
841 /* We are reconnecting, so we must send installation. */
842 bsp
->installed
= false;
844 /* Cancel any pending installation request. */
845 THREAD_OFF(bsp
->installev
);
847 /* Ask for installation. */
848 bsp
->lastev
= BSE_INSTALL
;
849 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
855 static int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS
)
857 struct bfd_session_params
*bsp
, *bspn
;
858 size_t sessions_updated
= 0;
859 struct interface
*ifp
;
860 int remote_cbit
= false;
861 int state
= BFD_STATUS_UNKNOWN
;
866 char ifstr
[128], cbitstr
[32];
868 /* Do nothing when shutting down. */
869 if (bsglobal
.shutting_down
)
872 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &state
, &remote_cbit
,
875 * When interface lookup fails or an invalid stream is read, we must
876 * not proceed otherwise it will trigger an assertion while checking
879 if (dp
.family
== 0 || sp
.family
== 0)
882 if (bsglobal
.debugging
) {
885 snprintf(ifstr
, sizeof(ifstr
), " (interface %s)",
888 snprintf(cbitstr
, sizeof(cbitstr
), " (CPI bit %s)",
889 remote_cbit
? "yes" : "no");
891 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__
, &sp
,
892 &dp
, ifstr
, vrf_id_to_name(vrf_id
), vrf_id
, cbitstr
,
893 bfd_get_status_str(state
));
898 addrlen
= sizeof(struct in_addr
);
901 addrlen
= sizeof(struct in6_addr
);
905 /* Unexpected value. */
910 /* Cache current time to avoid multiple monotime clock calls. */
911 now
= monotime(NULL
);
913 /* Notify all matching sessions about update. */
914 TAILQ_FOREACH_SAFE (bsp
, &bsglobal
.bsplist
, entry
, bspn
) {
915 /* Skip not installed entries. */
918 /* Skip different VRFs. */
919 if (bsp
->args
.vrf_id
!= vrf_id
)
921 /* Skip different families. */
922 if (bsp
->args
.family
!= dp
.family
)
924 /* Skip different interface. */
925 if (bsp
->args
.ifnamelen
&& ifp
926 && strcmp(bsp
->args
.ifname
, ifp
->name
) != 0)
928 /* Skip non matching destination addresses. */
929 if (memcmp(&bsp
->args
.dst
, &dp
.u
, addrlen
) != 0)
932 * Source comparison test:
933 * We will only compare source if BFD daemon provided the
934 * source address and the protocol set a source address in
935 * the configuration otherwise we'll just skip it.
937 if (sp
.family
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
) != 0
938 && memcmp(&sp
.u
, &i6a_zero
, addrlen
) != 0
939 && memcmp(&bsp
->args
.src
, &sp
.u
, addrlen
) != 0)
941 /* No session state change. */
942 if ((int)bsp
->bss
.state
== state
)
945 bsp
->bss
.last_event
= now
;
946 bsp
->bss
.previous_state
= bsp
->bss
.state
;
947 bsp
->bss
.state
= state
;
948 bsp
->bss
.remote_cbit
= remote_cbit
;
949 bsp
->updatecb(bsp
, &bsp
->bss
, bsp
->arg
);
953 if (bsglobal
.debugging
)
954 zlog_debug("%s: sessions updated: %zu", __func__
,
960 void bfd_protocol_integration_init(struct zclient
*zc
, struct thread_master
*tm
)
962 /* Initialize data structure. */
963 TAILQ_INIT(&bsglobal
.bsplist
);
969 /* Install our callbacks. */
970 zc
->interface_bfd_dest_update
= zclient_bfd_session_update
;
971 zc
->bfd_dest_replay
= zclient_bfd_session_reply
;
973 /* Send the client registration */
974 bfd_client_sendmsg(zc
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
977 void bfd_protocol_integration_set_debug(bool enable
)
979 bsglobal
.debugging
= enable
;
982 void bfd_protocol_integration_set_shutdown(bool enable
)
984 bsglobal
.shutting_down
= enable
;
987 bool bfd_protocol_integration_debug(void)
989 return bsglobal
.debugging
;
992 bool bfd_protocol_integration_shutting_down(void)
994 return bsglobal
.shutting_down
;