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 status 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
);
370 /* Send the C bit indicator. */
371 stream_putc(s
, args
->cbit
);
372 #endif /* HAVE_BFDD */
374 /* Finish the message by writing the size. */
375 stream_putw_at(s
, 0, stream_get_endp(s
));
377 /* Send message to zebra. */
378 if (zclient_send_message(zc
) == ZCLIENT_SEND_FAILURE
) {
379 if (bsglobal
.debugging
)
380 zlog_debug("%s: zclient_send_message failed", __func__
);
387 struct bfd_session_params
*bfd_sess_new(bsp_status_update updatecb
, void *arg
)
389 struct bfd_session_params
*bsp
;
391 bsp
= XCALLOC(MTYPE_BFD_INFO
, sizeof(*bsp
));
393 /* Save application data. */
394 bsp
->updatecb
= updatecb
;
398 bsp
->args
.detection_multiplier
= BFD_DEF_DETECT_MULT
;
400 bsp
->args
.min_rx
= BFD_DEF_MIN_RX
;
401 bsp
->args
.min_tx
= BFD_DEF_MIN_TX
;
402 bsp
->args
.vrf_id
= VRF_DEFAULT
;
404 /* Register in global list. */
405 TAILQ_INSERT_TAIL(&bsglobal
.bsplist
, bsp
, entry
);
410 static bool _bfd_sess_valid(const struct bfd_session_params
*bsp
)
412 /* Peer/local address not configured. */
413 if (bsp
->args
.family
== 0)
416 /* Address configured but invalid. */
417 if (bsp
->args
.family
!= AF_INET
&& bsp
->args
.family
!= AF_INET6
) {
418 if (bsglobal
.debugging
)
419 zlog_debug("%s: invalid session family: %d", __func__
,
424 /* Invalid address. */
425 if (memcmp(&bsp
->args
.dst
, &i6a_zero
, sizeof(i6a_zero
)) == 0) {
426 if (bsglobal
.debugging
) {
427 if (bsp
->args
.family
== AF_INET
)
428 zlog_debug("%s: invalid address: %pI4",
430 (struct in_addr
*)&bsp
->args
.dst
);
432 zlog_debug("%s: invalid address: %pI6",
433 __func__
, &bsp
->args
.dst
);
438 /* Multi hop requires local address. */
440 && memcmp(&i6a_zero
, &bsp
->args
.src
, sizeof(i6a_zero
)) == 0) {
441 if (bsglobal
.debugging
)
443 "%s: multi hop but no local address provided",
449 if (bsp
->args
.vrf_id
== VRF_UNKNOWN
) {
450 if (bsglobal
.debugging
)
451 zlog_debug("%s: asked for unknown VRF", __func__
);
458 static int _bfd_sess_send(struct thread
*t
)
460 struct bfd_session_params
*bsp
= THREAD_ARG(t
);
463 /* Validate configuration before trying to send bogus data. */
464 if (!_bfd_sess_valid(bsp
))
467 if (bsp
->lastev
== BSE_INSTALL
) {
468 bsp
->args
.command
= bsp
->installed
? ZEBRA_BFD_DEST_UPDATE
469 : ZEBRA_BFD_DEST_REGISTER
;
471 bsp
->args
.command
= ZEBRA_BFD_DEST_DEREGISTER
;
473 /* If not installed and asked for uninstall, do nothing. */
474 if (!bsp
->installed
&& bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
477 rv
= zclient_bfd_command(bsglobal
.zc
, &bsp
->args
);
478 /* Command was sent successfully. */
480 /* Update installation status. */
481 if (bsp
->args
.command
== ZEBRA_BFD_DEST_DEREGISTER
)
482 bsp
->installed
= false;
483 else if (bsp
->args
.command
== ZEBRA_BFD_DEST_REGISTER
)
484 bsp
->installed
= true;
486 struct ipaddr src
, dst
;
488 src
.ipa_type
= bsp
->args
.family
;
489 src
.ipaddr_v6
= bsp
->args
.src
;
490 dst
.ipa_type
= bsp
->args
.family
;
491 dst
.ipaddr_v6
= bsp
->args
.dst
;
494 "%s: BFD session %pIA -> %pIA interface %s VRF %s(%u) was not %s",
495 __func__
, &src
, &dst
,
496 bsp
->args
.ifnamelen
? bsp
->args
.ifname
: "*",
497 vrf_id_to_name(bsp
->args
.vrf_id
), bsp
->args
.vrf_id
,
498 bsp
->lastev
== BSE_INSTALL
? "installed"
505 static void _bfd_sess_remove(struct bfd_session_params
*bsp
)
507 /* Not installed, nothing to do. */
511 /* Cancel any pending installation request. */
512 THREAD_OFF(bsp
->installev
);
514 /* Send request to remove any session. */
515 bsp
->lastev
= BSE_UNINSTALL
;
516 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
519 void bfd_sess_free(struct bfd_session_params
**bsp
)
524 /* Remove any installed session. */
525 _bfd_sess_remove(*bsp
);
527 /* Remove from global list. */
528 TAILQ_REMOVE(&bsglobal
.bsplist
, (*bsp
), entry
);
530 /* Free the memory and point to NULL. */
531 XFREE(MTYPE_BFD_INFO
, (*bsp
));
534 static bool bfd_sess_address_changed(const struct bfd_session_params
*bsp
,
536 const struct in6_addr
*src
,
537 const struct in6_addr
*dst
)
541 if (bsp
->args
.family
!= family
)
544 addrlen
= (family
== AF_INET
) ? sizeof(struct in_addr
)
545 : sizeof(struct in6_addr
);
546 if ((src
== NULL
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
))
547 || (src
&& memcmp(src
, &bsp
->args
.src
, addrlen
))
548 || memcmp(dst
, &bsp
->args
.dst
, addrlen
))
554 void bfd_sess_set_ipv4_addrs(struct bfd_session_params
*bsp
,
555 struct in_addr
*src
, struct in_addr
*dst
)
557 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
558 (struct in6_addr
*)dst
))
561 /* If already installed, remove the old setting. */
562 _bfd_sess_remove(bsp
);
564 bsp
->args
.family
= AF_INET
;
566 /* Clean memory, set zero value and avoid static analyser warnings. */
567 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
568 memset(&bsp
->args
.dst
, 0, sizeof(bsp
->args
.dst
));
570 /* Copy the equivalent of IPv4 to arguments structure. */
572 memcpy(&bsp
->args
.src
, src
, sizeof(struct in_addr
));
575 memcpy(&bsp
->args
.dst
, dst
, sizeof(struct in_addr
));
578 void bfd_sess_set_ipv6_addrs(struct bfd_session_params
*bsp
,
579 struct in6_addr
*src
, struct in6_addr
*dst
)
581 if (!bfd_sess_address_changed(bsp
, AF_INET
, (struct in6_addr
*)src
,
582 (struct in6_addr
*)dst
))
585 /* If already installed, remove the old setting. */
586 _bfd_sess_remove(bsp
);
588 bsp
->args
.family
= AF_INET6
;
590 /* Clean memory, set zero value and avoid static analyser warnings. */
591 memset(&bsp
->args
.src
, 0, sizeof(bsp
->args
.src
));
594 bsp
->args
.src
= *src
;
597 bsp
->args
.dst
= *dst
;
600 void bfd_sess_set_interface(struct bfd_session_params
*bsp
, const char *ifname
)
602 if ((ifname
== NULL
&& bsp
->args
.ifnamelen
== 0)
603 || (ifname
&& strcmp(bsp
->args
.ifname
, ifname
) == 0))
606 /* If already installed, remove the old setting. */
607 _bfd_sess_remove(bsp
);
609 if (ifname
== NULL
) {
610 bsp
->args
.ifname
[0] = 0;
611 bsp
->args
.ifnamelen
= 0;
615 if (strlcpy(bsp
->args
.ifname
, ifname
, sizeof(bsp
->args
.ifname
))
616 > sizeof(bsp
->args
.ifname
))
617 zlog_warn("%s: interface name truncated: %s", __func__
, ifname
);
619 bsp
->args
.ifnamelen
= strlen(bsp
->args
.ifname
);
622 void bfd_sess_set_profile(struct bfd_session_params
*bsp
, const char *profile
)
624 if (profile
== NULL
) {
625 bsp
->args
.profile
[0] = 0;
626 bsp
->args
.profilelen
= 0;
630 if (strlcpy(bsp
->args
.profile
, profile
, sizeof(bsp
->args
.profile
))
631 > sizeof(bsp
->args
.profile
))
632 zlog_warn("%s: profile name truncated: %s", __func__
, profile
);
634 bsp
->args
.profilelen
= strlen(bsp
->args
.profile
);
637 void bfd_sess_set_vrf(struct bfd_session_params
*bsp
, vrf_id_t vrf_id
)
639 if (bsp
->args
.vrf_id
== vrf_id
)
642 /* If already installed, remove the old setting. */
643 _bfd_sess_remove(bsp
);
645 bsp
->args
.vrf_id
= vrf_id
;
648 void bfd_sess_set_mininum_ttl(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
650 assert(min_ttl
!= 0);
652 if (bsp
->args
.ttl
== ((BFD_SINGLE_HOP_TTL
+ 1) - min_ttl
))
655 /* If already installed, remove the old setting. */
656 _bfd_sess_remove(bsp
);
658 /* Invert TTL value: protocol expects number of hops. */
659 min_ttl
= (BFD_SINGLE_HOP_TTL
+ 1) - min_ttl
;
660 bsp
->args
.ttl
= min_ttl
;
661 bsp
->args
.mhop
= (min_ttl
> 1);
664 void bfd_sess_set_hop_count(struct bfd_session_params
*bsp
, uint8_t min_ttl
)
666 if (bsp
->args
.ttl
== min_ttl
)
669 /* If already installed, remove the old setting. */
670 _bfd_sess_remove(bsp
);
672 bsp
->args
.ttl
= min_ttl
;
673 bsp
->args
.mhop
= (min_ttl
> 1);
677 void bfd_sess_set_cbit(struct bfd_session_params
*bsp
, bool enable
)
679 bsp
->args
.cbit
= enable
;
682 void bfd_sess_set_timers(struct bfd_session_params
*bsp
,
683 uint8_t detection_multiplier
, uint32_t min_rx
,
686 bsp
->args
.detection_multiplier
= detection_multiplier
;
687 bsp
->args
.min_rx
= min_rx
;
688 bsp
->args
.min_tx
= min_tx
;
691 void bfd_sess_install(struct bfd_session_params
*bsp
)
693 bsp
->lastev
= BSE_INSTALL
;
694 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
697 void bfd_sess_uninstall(struct bfd_session_params
*bsp
)
699 bsp
->lastev
= BSE_UNINSTALL
;
700 thread_add_event(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0, &bsp
->installev
);
703 enum bfd_session_state
bfd_sess_status(const struct bfd_session_params
*bsp
)
705 return bsp
->bss
.state
;
708 uint8_t bfd_sess_minimum_ttl(const struct bfd_session_params
*bsp
)
710 return ((BFD_SINGLE_HOP_TTL
+ 1) - bsp
->args
.ttl
);
713 uint8_t bfd_sess_hop_count(const struct bfd_session_params
*bsp
)
715 return bsp
->args
.ttl
;
718 const char *bfd_sess_profile(const struct bfd_session_params
*bsp
)
720 return bsp
->args
.profilelen
? bsp
->args
.profile
: NULL
;
723 void bfd_sess_addresses(const struct bfd_session_params
*bsp
, int *family
,
724 struct in6_addr
*src
, struct in6_addr
*dst
)
726 *family
= bsp
->args
.family
;
728 *src
= bsp
->args
.src
;
730 *dst
= bsp
->args
.dst
;
733 const char *bfd_sess_interface(const struct bfd_session_params
*bsp
)
735 if (bsp
->args
.ifnamelen
)
736 return bsp
->args
.ifname
;
741 const char *bfd_sess_vrf(const struct bfd_session_params
*bsp
)
743 return vrf_id_to_name(bsp
->args
.vrf_id
);
746 vrf_id_t
bfd_sess_vrf_id(const struct bfd_session_params
*bsp
)
748 return bsp
->args
.vrf_id
;
751 bool bfd_sess_cbit(const struct bfd_session_params
*bsp
)
753 return bsp
->args
.cbit
;
756 void bfd_sess_timers(const struct bfd_session_params
*bsp
,
757 uint8_t *detection_multiplier
, uint32_t *min_rx
,
760 *detection_multiplier
= bsp
->args
.detection_multiplier
;
761 *min_rx
= bsp
->args
.min_rx
;
762 *min_tx
= bsp
->args
.min_tx
;
765 void bfd_sess_show(struct vty
*vty
, struct json_object
*json
,
766 struct bfd_session_params
*bsp
)
768 json_object
*json_bfd
= NULL
;
776 json_bfd
= json_object_new_object();
778 json_object_string_add(json_bfd
, "type", "multi hop");
780 json_object_string_add(json_bfd
, "type", "single hop");
782 vty_out(vty
, " BFD: Type: %s\n",
783 bsp
->args
.mhop
? "multi hop" : "single hop");
785 /* Show configuration. */
787 json_object_int_add(json_bfd
, "detectMultiplier",
788 bsp
->args
.detection_multiplier
);
789 json_object_int_add(json_bfd
, "rxMinInterval",
791 json_object_int_add(json_bfd
, "txMinInterval",
795 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
796 bsp
->args
.detection_multiplier
, bsp
->args
.min_rx
,
800 bfd_last_update(bsp
->bss
.last_event
, time_buf
, sizeof(time_buf
));
802 json_object_string_add(json_bfd
, "status",
803 bfd_get_status_str(bsp
->bss
.state
));
804 json_object_string_add(json_bfd
, "lastUpdate", time_buf
);
806 vty_out(vty
, " Status: %s, Last update: %s\n",
807 bfd_get_status_str(bsp
->bss
.state
), time_buf
);
810 json_object_object_add(json
, "peerBfdInfo", json_bfd
);
816 * Zebra communication related.
820 * Callback for reinstallation of all registered BFD sessions.
822 * Use this as `zclient` `bfd_dest_replay` callback.
824 int zclient_bfd_session_reply(ZAPI_CALLBACK_ARGS
)
826 struct bfd_session_params
*bsp
;
828 if (!zclient
->bfd_integration
)
831 /* Do nothing when shutting down. */
832 if (bsglobal
.shutting_down
)
835 if (bsglobal
.debugging
)
836 zlog_debug("%s: sending all sessions registered", __func__
);
838 /* Send the client registration */
839 bfd_client_sendmsg(zclient
, ZEBRA_BFD_CLIENT_REGISTER
, vrf_id
);
841 /* Replay all activated peers. */
842 TAILQ_FOREACH (bsp
, &bsglobal
.bsplist
, entry
) {
843 /* Skip not installed sessions. */
847 /* We are reconnecting, so we must send installation. */
848 bsp
->installed
= false;
850 /* Cancel any pending installation request. */
851 THREAD_OFF(bsp
->installev
);
853 /* Ask for installation. */
854 bsp
->lastev
= BSE_INSTALL
;
855 thread_execute(bsglobal
.tm
, _bfd_sess_send
, bsp
, 0);
861 int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS
)
863 struct bfd_session_params
*bsp
, *bspn
;
864 size_t sessions_updated
= 0;
865 struct interface
*ifp
;
866 int remote_cbit
= false;
867 int state
= BFD_STATUS_UNKNOWN
;
872 char ifstr
[128], cbitstr
[32];
874 if (!zclient
->bfd_integration
)
877 /* Do nothing when shutting down. */
878 if (bsglobal
.shutting_down
)
881 ifp
= bfd_get_peer_info(zclient
->ibuf
, &dp
, &sp
, &state
, &remote_cbit
,
884 * When interface lookup fails or an invalid stream is read, we must
885 * not proceed otherwise it will trigger an assertion while checking
888 if (dp
.family
== 0 || sp
.family
== 0)
891 if (bsglobal
.debugging
) {
894 snprintf(ifstr
, sizeof(ifstr
), " (interface %s)",
897 snprintf(cbitstr
, sizeof(cbitstr
), " (CPI bit %s)",
898 remote_cbit
? "yes" : "no");
900 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__
, &sp
,
901 &dp
, ifstr
, vrf_id_to_name(vrf_id
), vrf_id
, cbitstr
,
902 bfd_get_status_str(state
));
907 addrlen
= sizeof(struct in_addr
);
910 addrlen
= sizeof(struct in6_addr
);
914 /* Unexpected value. */
919 /* Cache current time to avoid multiple monotime clock calls. */
920 now
= monotime(NULL
);
922 /* Notify all matching sessions about update. */
923 TAILQ_FOREACH_SAFE (bsp
, &bsglobal
.bsplist
, entry
, bspn
) {
924 /* Skip not installed entries. */
927 /* Skip different VRFs. */
928 if (bsp
->args
.vrf_id
!= vrf_id
)
930 /* Skip different families. */
931 if (bsp
->args
.family
!= dp
.family
)
933 /* Skip different interface. */
934 if (bsp
->args
.ifnamelen
&& ifp
935 && strcmp(bsp
->args
.ifname
, ifp
->name
) != 0)
937 /* Skip non matching destination addresses. */
938 if (memcmp(&bsp
->args
.dst
, &dp
.u
, addrlen
) != 0)
941 * Source comparison test:
942 * We will only compare source if BFD daemon provided the
943 * source address and the protocol set a source address in
944 * the configuration otherwise we'll just skip it.
946 if (sp
.family
&& memcmp(&bsp
->args
.src
, &i6a_zero
, addrlen
) != 0
947 && memcmp(&sp
.u
, &i6a_zero
, addrlen
) != 0
948 && memcmp(&bsp
->args
.src
, &sp
.u
, addrlen
) != 0)
950 /* No session state change. */
951 if ((int)bsp
->bss
.state
== state
)
954 bsp
->bss
.last_event
= now
;
955 bsp
->bss
.previous_state
= bsp
->bss
.state
;
956 bsp
->bss
.state
= state
;
957 bsp
->bss
.remote_cbit
= remote_cbit
;
958 bsp
->updatecb(bsp
, &bsp
->bss
, bsp
->arg
);
962 if (bsglobal
.debugging
)
963 zlog_debug("%s: sessions updated: %zu", __func__
,
969 void bfd_protocol_integration_init(struct zclient
*zc
, struct thread_master
*tm
)
971 /* Initialize data structure. */
972 TAILQ_INIT(&bsglobal
.bsplist
);
978 /* Enable BFD callbacks. */
979 zc
->bfd_integration
= true;
981 /* Send the client registration */
982 bfd_client_sendmsg(zc
, ZEBRA_BFD_CLIENT_REGISTER
, VRF_DEFAULT
);
985 void bfd_protocol_integration_set_debug(bool enable
)
987 bsglobal
.debugging
= enable
;
990 void bfd_protocol_integration_set_shutdown(bool enable
)
992 bsglobal
.shutting_down
= enable
;
995 bool bfd_protocol_integration_debug(void)
997 return bsglobal
.debugging
;
1000 bool bfd_protocol_integration_shutting_down(void)
1002 return bsglobal
.shutting_down
;