1 /*********************************************************************
2 * Copyright 2013 Cumulus Networks, LLC. All rights reserved.
3 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * bfd.c: implements the BFD protocol.
23 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
24 * Kanna Rajagopal [kanna@cumulusnetworks.com]
25 * Radhika Mahankali [Radhika@cumulusnetworks.com]
30 #include "lib/jhash.h"
34 DEFINE_QOBJ_TYPE(bfd_session
);
39 static uint32_t ptm_bfd_gen_ID(void);
40 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
);
41 static void bfd_session_free(struct bfd_session
*bs
);
42 static struct bfd_session
*bfd_session_new(int sd
);
43 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
45 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
);
46 static const char *get_diag_str(int diag
);
52 struct bfd_session
*bs_peer_find(struct bfd_peer_cfg
*bpc
)
54 struct bfd_session
*bs
;
55 struct peer_label
*pl
;
56 struct bfd_mhop_key mhop
;
57 struct bfd_shop_key shop
;
59 /* Try to find label first. */
60 if (bpc
->bpc_has_label
) {
61 pl
= pl_find(bpc
->bpc_label
);
68 /* Otherwise fallback to peer/local hash lookup. */
70 memset(&mhop
, 0, sizeof(mhop
));
71 mhop
.peer
= bpc
->bpc_peer
;
72 mhop
.local
= bpc
->bpc_local
;
73 if (bpc
->bpc_has_vrfname
)
74 strlcpy(mhop
.vrf_name
, bpc
->bpc_vrfname
,
75 sizeof(mhop
.vrf_name
));
77 bs
= bfd_mhop_lookup(mhop
);
79 memset(&shop
, 0, sizeof(shop
));
80 shop
.peer
= bpc
->bpc_peer
;
81 if (bpc
->bpc_has_localif
)
82 strlcpy(shop
.port_name
, bpc
->bpc_localif
,
83 sizeof(shop
.port_name
));
85 bs
= bfd_shop_lookup(shop
);
91 static uint32_t ptm_bfd_gen_ID(void)
93 static uint32_t sessionID
= 1;
98 void ptm_bfd_start_xmt_timer(struct bfd_session
*bfd
, bool is_echo
)
100 uint64_t jitter
, xmt_TO
;
103 xmt_TO
= is_echo
? bfd
->echo_xmt_TO
: bfd
->xmt_TO
;
106 * From section 6.5.2: trasmit interval should be randomly jittered
108 * 75% and 100% of nominal value, unless detect_mult is 1, then should
110 * between 75% and 90%.
112 maxpercent
= (bfd
->detect_mult
== 1) ? 16 : 26;
113 jitter
= (xmt_TO
* (75 + (random() % maxpercent
))) / 100;
114 /* XXX remove that division above */
117 bfd_echo_xmttimer_update(bfd
, jitter
);
119 bfd_xmttimer_update(bfd
, jitter
);
122 static void ptm_bfd_echo_xmt_TO(struct bfd_session
*bfd
)
124 /* Send the scheduled echo packet */
125 ptm_bfd_echo_snd(bfd
);
127 /* Restart the timer for next time */
128 ptm_bfd_start_xmt_timer(bfd
, true);
131 void ptm_bfd_xmt_TO(struct bfd_session
*bfd
, int fbit
)
133 /* Send the scheduled control packet */
134 ptm_bfd_snd(bfd
, fbit
);
136 /* Restart the timer for next time */
137 ptm_bfd_start_xmt_timer(bfd
, false);
140 void ptm_bfd_echo_stop(struct bfd_session
*bfd
, int polling
)
142 bfd
->echo_xmt_TO
= 0;
143 bfd
->echo_detect_TO
= 0;
144 BFD_UNSET_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
);
146 bfd_echo_xmttimer_delete(bfd
);
147 bfd_echo_recvtimer_delete(bfd
);
150 bfd
->polling
= polling
;
151 bfd
->new_timers
.desired_min_tx
= bfd
->up_min_tx
;
152 bfd
->new_timers
.required_min_rx
= bfd
->timers
.required_min_rx
;
157 void ptm_bfd_echo_start(struct bfd_session
*bfd
)
159 bfd
->echo_detect_TO
= (bfd
->remote_detect_mult
* bfd
->echo_xmt_TO
);
160 if (bfd
->echo_detect_TO
> 0)
161 ptm_bfd_echo_xmt_TO(bfd
);
164 bfd
->new_timers
.desired_min_tx
= bfd
->up_min_tx
;
165 bfd
->new_timers
.required_min_rx
= bfd
->timers
.required_min_rx
;
169 void ptm_bfd_ses_up(struct bfd_session
*bfd
)
171 int old_state
= bfd
->ses_state
;
174 bfd
->ses_state
= PTM_BFD_UP
;
176 monotime(&bfd
->uptime
);
178 /* If the peer is capable to receiving Echo pkts */
179 if (bfd
->echo_xmt_TO
&& !BFD_CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_MH
)) {
180 ptm_bfd_echo_start(bfd
);
182 bfd
->new_timers
.desired_min_tx
= bfd
->up_min_tx
;
183 bfd
->new_timers
.required_min_rx
= bfd
->timers
.required_min_rx
;
189 if (old_state
!= bfd
->ses_state
) {
190 bfd
->stats
.session_up
++;
191 log_info("state-change: [%s] %s -> %s", bs_to_string(bfd
),
192 state_list
[old_state
].str
,
193 state_list
[bfd
->ses_state
].str
);
197 void ptm_bfd_ses_dn(struct bfd_session
*bfd
, uint8_t diag
)
199 int old_state
= bfd
->ses_state
;
201 bfd
->local_diag
= diag
;
202 bfd
->discrs
.remote_discr
= 0;
203 bfd
->ses_state
= PTM_BFD_DOWN
;
205 bfd
->demand_mode
= 0;
206 monotime(&bfd
->downtime
);
210 /* only signal clients when going from up->down state */
211 if (old_state
== PTM_BFD_UP
)
214 /* Stop echo packet transmission if they are active */
215 if (BFD_CHECK_FLAG(bfd
->flags
, BFD_SESS_FLAG_ECHO_ACTIVE
))
216 ptm_bfd_echo_stop(bfd
, 0);
218 if (old_state
!= bfd
->ses_state
) {
219 bfd
->stats
.session_down
++;
220 log_info("state-change: [%s] %s -> %s reason:%s",
221 bs_to_string(bfd
), state_list
[old_state
].str
,
222 state_list
[bfd
->ses_state
].str
,
223 get_diag_str(bfd
->local_diag
));
227 static int ptm_bfd_get_vrf_name(char *port_name
, char *vrf_name
)
229 struct bfd_iface
*iface
;
232 if ((port_name
== NULL
) || (vrf_name
== NULL
))
235 iface
= bfd_iface_lookup(port_name
);
237 vrf
= bfd_vrf_lookup(iface
->vrf_id
);
239 strlcpy(vrf_name
, vrf
->name
, sizeof(vrf
->name
));
246 static struct bfd_session
*bfd_find_disc(struct sockaddr_any
*sa
,
249 struct bfd_session
*bs
;
251 bs
= bfd_id_lookup(ldisc
);
255 /* Remove unused fields. */
256 switch (sa
->sa_sin
.sin_family
) {
258 sa
->sa_sin
.sin_port
= 0;
259 if (memcmp(sa
, &bs
->shop
.peer
, sizeof(sa
->sa_sin
)) == 0)
263 sa
->sa_sin6
.sin6_port
= 0;
264 if (memcmp(sa
, &bs
->shop
.peer
, sizeof(sa
->sa_sin6
)) == 0)
272 struct bfd_session
*ptm_bfd_sess_find(struct bfd_pkt
*cp
, char *port_name
,
273 struct sockaddr_any
*peer
,
274 struct sockaddr_any
*local
,
275 char *vrf_name
, bool is_mhop
)
277 struct bfd_session
*l_bfd
= NULL
;
278 struct bfd_mhop_key mhop
;
279 struct bfd_shop_key shop
;
280 char vrf_buf
[MAXNAMELEN
];
282 /* Find our session using the ID signaled by the remote end. */
283 if (cp
->discrs
.remote_discr
)
284 return bfd_find_disc(peer
, ntohl(cp
->discrs
.remote_discr
));
286 /* Search for session without using discriminator. */
288 memset(&mhop
, 0, sizeof(mhop
));
291 if (vrf_name
&& vrf_name
[0]) {
292 strlcpy(mhop
.vrf_name
, vrf_name
, sizeof(mhop
.vrf_name
));
293 } else if (port_name
&& port_name
[0]) {
294 memset(vrf_buf
, 0, sizeof(vrf_buf
));
295 if (ptm_bfd_get_vrf_name(port_name
, vrf_buf
) != -1)
296 strlcpy(mhop
.vrf_name
, vrf_buf
,
297 sizeof(mhop
.vrf_name
));
300 l_bfd
= bfd_mhop_lookup(mhop
);
302 memset(&shop
, 0, sizeof(shop
));
304 if (port_name
&& port_name
[0])
305 strlcpy(shop
.port_name
, port_name
,
306 sizeof(shop
.port_name
));
308 l_bfd
= bfd_shop_lookup(shop
);
311 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
315 int bfd_xmt_cb(struct thread
*t
)
317 struct bfd_session
*bs
= THREAD_ARG(t
);
319 ptm_bfd_xmt_TO(bs
, 0);
324 int bfd_echo_xmt_cb(struct thread
*t
)
326 struct bfd_session
*bs
= THREAD_ARG(t
);
328 if (bs
->echo_xmt_TO
> 0)
329 ptm_bfd_echo_xmt_TO(bs
);
334 /* Was ptm_bfd_detect_TO() */
335 int bfd_recvtimer_cb(struct thread
*t
)
337 struct bfd_session
*bs
= THREAD_ARG(t
);
339 switch (bs
->ses_state
) {
342 ptm_bfd_ses_dn(bs
, BD_CONTROL_EXPIRED
);
343 bfd_recvtimer_update(bs
);
347 /* Second detect time expiration, zero remote discr (section
350 bs
->discrs
.remote_discr
= 0;
357 /* Was ptm_bfd_echo_detect_TO() */
358 int bfd_echo_recvtimer_cb(struct thread
*t
)
360 struct bfd_session
*bs
= THREAD_ARG(t
);
362 switch (bs
->ses_state
) {
365 ptm_bfd_ses_dn(bs
, BD_ECHO_FAILED
);
372 static struct bfd_session
*bfd_session_new(int sd
)
374 struct bfd_session
*bs
;
376 bs
= XCALLOC(MTYPE_BFDD_CONFIG
, sizeof(*bs
));
380 QOBJ_REG(bs
, bfd_session
);
382 bs
->up_min_tx
= BFD_DEFDESIREDMINTX
;
383 bs
->timers
.required_min_rx
= BFD_DEFREQUIREDMINRX
;
384 bs
->timers
.required_min_echo
= BFD_DEF_REQ_MIN_ECHO
;
385 bs
->detect_mult
= BFD_DEFDETECTMULT
;
386 bs
->mh_ttl
= BFD_DEF_MHOP_TTL
;
389 monotime(&bs
->uptime
);
390 bs
->downtime
= bs
->uptime
;
395 int bfd_session_update_label(struct bfd_session
*bs
, const char *nlabel
)
397 /* New label treatment:
398 * - Check if the label is taken;
399 * - Try to allocate the memory for it and register;
401 if (bs
->pl
== NULL
) {
402 if (pl_find(nlabel
) != NULL
) {
403 /* Someone is already using it. */
407 if (pl_new(nlabel
, bs
) == NULL
)
414 * Test label change consistency:
415 * - Do nothing if it's the same label;
416 * - Check if the future label is already taken;
419 if (strcmp(nlabel
, bs
->pl
->pl_label
) == 0)
421 if (pl_find(nlabel
) != NULL
)
424 strlcpy(bs
->pl
->pl_label
, nlabel
, sizeof(bs
->pl
->pl_label
));
428 static void _bfd_session_update(struct bfd_session
*bs
,
429 struct bfd_peer_cfg
*bpc
)
432 /* Check if echo mode is already active. */
433 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
436 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
437 ptm_bfd_echo_start(bs
);
439 /* Activate/update echo receive timeout timer. */
440 bfd_echo_recvtimer_update(bs
);
442 /* Check if echo mode is already disabled. */
443 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
))
446 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
);
447 ptm_bfd_echo_stop(bs
, 0);
451 if (bpc
->bpc_has_txinterval
)
452 bs
->up_min_tx
= bpc
->bpc_txinterval
* 1000;
454 if (bpc
->bpc_has_recvinterval
)
455 bs
->timers
.required_min_rx
= bpc
->bpc_recvinterval
* 1000;
457 if (bpc
->bpc_has_detectmultiplier
)
458 bs
->detect_mult
= bpc
->bpc_detectmultiplier
;
460 if (bpc
->bpc_has_echointerval
)
461 bs
->timers
.required_min_echo
= bpc
->bpc_echointerval
* 1000;
463 if (bpc
->bpc_has_label
)
464 bfd_session_update_label(bs
, bpc
->bpc_label
);
466 if (bpc
->bpc_shutdown
) {
467 /* Check if already shutdown. */
468 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
471 BFD_SET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
473 /* Disable all events. */
474 bfd_recvtimer_delete(bs
);
475 bfd_echo_recvtimer_delete(bs
);
476 bfd_xmttimer_delete(bs
);
477 bfd_echo_xmttimer_delete(bs
);
479 /* Change and notify state change. */
480 bs
->ses_state
= PTM_BFD_ADM_DOWN
;
485 /* Check if already working. */
486 if (!BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
))
489 BFD_UNSET_FLAG(bs
->flags
, BFD_SESS_FLAG_SHUTDOWN
);
491 /* Change and notify state change. */
492 bs
->ses_state
= PTM_BFD_DOWN
;
495 /* Enable all timers. */
496 bfd_recvtimer_update(bs
);
497 bfd_xmttimer_update(bs
, bs
->xmt_TO
);
498 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_ECHO
)) {
499 bfd_echo_recvtimer_update(bs
);
500 bfd_echo_xmttimer_update(bs
, bs
->echo_xmt_TO
);
505 static int bfd_session_update(struct bfd_session
*bs
, struct bfd_peer_cfg
*bpc
)
507 /* User didn't want to update, return failure. */
508 if (bpc
->bpc_createonly
)
511 _bfd_session_update(bs
, bpc
);
513 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE
, bs
);
518 static void bfd_session_free(struct bfd_session
*bs
)
523 bfd_recvtimer_delete(bs
);
524 bfd_echo_recvtimer_delete(bs
);
525 bfd_xmttimer_delete(bs
);
526 bfd_echo_xmttimer_delete(bs
);
528 bfd_id_delete(bs
->discrs
.my_discr
);
529 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
))
530 bfd_mhop_delete(bs
->mhop
);
532 bfd_shop_delete(bs
->shop
);
537 XFREE(MTYPE_BFDD_CONFIG
, bs
);
540 struct bfd_session
*ptm_bfd_sess_new(struct bfd_peer_cfg
*bpc
)
542 struct bfd_session
*bfd
, *l_bfd
;
545 /* check to see if this needs a new session */
546 l_bfd
= bs_peer_find(bpc
);
548 /* Requesting a duplicated peer means update configuration. */
549 if (bfd_session_update(l_bfd
, bpc
) == 0)
556 * Get socket for transmitting control packets. Note that if we
557 * could use the destination port (3784) for the source
558 * port we wouldn't need a socket per session.
561 psock
= bp_peer_socket(bpc
);
565 psock
= bp_peer_socketv6(bpc
);
571 bfd
= bfd_session_new(psock
);
573 log_error("session-new: allocation failed");
577 if (bpc
->bpc_has_localif
&& !bpc
->bpc_mhop
) {
578 bfd
->ifindex
= ptm_bfd_fetch_ifindex(bpc
->bpc_localif
);
579 ptm_bfd_fetch_local_mac(bpc
->bpc_localif
, bfd
->local_mac
);
582 if (bpc
->bpc_ipv4
== false) {
583 BFD_SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_IPV6
);
585 /* Set the IPv6 scope id for link-local addresses. */
586 if (IN6_IS_ADDR_LINKLOCAL(&bpc
->bpc_local
.sa_sin6
.sin6_addr
))
587 bpc
->bpc_local
.sa_sin6
.sin6_scope_id
= bfd
->ifindex
;
588 if (IN6_IS_ADDR_LINKLOCAL(&bpc
->bpc_peer
.sa_sin6
.sin6_addr
))
589 bpc
->bpc_peer
.sa_sin6
.sin6_scope_id
= bfd
->ifindex
;
592 /* Initialize the session */
593 bfd
->ses_state
= PTM_BFD_DOWN
;
594 bfd
->discrs
.my_discr
= ptm_bfd_gen_ID();
595 bfd
->discrs
.remote_discr
= 0;
596 bfd
->local_ip
= bpc
->bpc_local
;
597 bfd
->local_address
= bpc
->bpc_local
;
598 bfd
->timers
.desired_min_tx
= bfd
->up_min_tx
;
599 bfd
->detect_TO
= (bfd
->detect_mult
* BFD_DEF_SLOWTX
);
601 /* Use detect_TO first for slow detection, then use recvtimer_update. */
602 bfd_recvtimer_update(bfd
);
607 BFD_SET_FLAG(bfd
->flags
, BFD_SESS_FLAG_MH
);
608 bfd
->mhop
.peer
= bpc
->bpc_peer
;
609 bfd
->mhop
.local
= bpc
->bpc_local
;
610 if (bpc
->bpc_has_vrfname
)
611 strlcpy(bfd
->mhop
.vrf_name
, bpc
->bpc_vrfname
,
612 sizeof(bfd
->mhop
.vrf_name
));
614 bfd_mhop_insert(bfd
);
616 bfd
->shop
.peer
= bpc
->bpc_peer
;
617 if (bpc
->bpc_has_localif
)
618 strlcpy(bfd
->shop
.port_name
, bpc
->bpc_localif
,
619 sizeof(bfd
->shop
.port_name
));
621 bfd_shop_insert(bfd
);
625 * XXX: session update triggers echo start, so we must have our
626 * discriminator ID set first.
628 _bfd_session_update(bfd
, bpc
);
630 /* Start transmitting with slow interval until peer responds */
631 bfd
->xmt_TO
= BFD_DEF_SLOWTX
;
633 ptm_bfd_xmt_TO(bfd
, 0);
635 log_info("session-new: %s", bs_to_string(bfd
));
637 control_notify_config(BCM_NOTIFY_CONFIG_ADD
, bfd
);
642 int ptm_bfd_ses_del(struct bfd_peer_cfg
*bpc
)
644 struct bfd_session
*bs
;
646 /* Find session and call free(). */
647 bs
= bs_peer_find(bpc
);
651 /* This pointer is being referenced, don't let it be deleted. */
652 if (bs
->refcount
> 0) {
653 log_error("session-delete: refcount failure: %" PRIu64
659 log_info("session-delete: %s", bs_to_string(bs
));
661 control_notify_config(BCM_NOTIFY_CONFIG_DELETE
, bs
);
663 bfd_session_free(bs
);
668 void bfd_set_polling(struct bfd_session
*bs
)
670 bs
->new_timers
.desired_min_tx
= bs
->up_min_tx
;
671 bs
->new_timers
.required_min_rx
= bs
->timers
.required_min_rx
;
672 bs
->new_timers
.required_min_echo
= bs
->timers
.required_min_echo
;
680 static const char *get_diag_str(int diag
)
682 for (int i
= 0; diag_list
[i
].str
; i
++) {
683 if (diag_list
[i
].type
== diag
)
684 return diag_list
[i
].str
;
689 const char *satostr(struct sockaddr_any
*sa
)
691 #define INETSTR_BUFCOUNT 8
692 static char buf
[INETSTR_BUFCOUNT
][INET6_ADDRSTRLEN
];
694 struct sockaddr_in
*sin
= &sa
->sa_sin
;
695 struct sockaddr_in6
*sin6
= &sa
->sa_sin6
;
697 bufidx
+= (bufidx
+ 1) % INETSTR_BUFCOUNT
;
700 switch (sin
->sin_family
) {
702 inet_ntop(AF_INET
, &sin
->sin_addr
, buf
[bufidx
],
703 sizeof(buf
[bufidx
]));
706 inet_ntop(AF_INET6
, &sin6
->sin6_addr
, buf
[bufidx
],
707 sizeof(buf
[bufidx
]));
711 strlcpy(buf
[bufidx
], "unknown", sizeof(buf
[bufidx
]));
718 const char *diag2str(uint8_t diag
)
724 return "control detection time expired";
726 return "echo function failed";
728 return "neighbor signaled session down";
730 return "forwarding plane reset";
734 return "concatenated path down";
736 return "administratively down";
738 return "reverse concatenated path down";
744 int strtosa(const char *addr
, struct sockaddr_any
*sa
)
746 memset(sa
, 0, sizeof(*sa
));
748 if (inet_pton(AF_INET
, addr
, &sa
->sa_sin
.sin_addr
) == 1) {
749 sa
->sa_sin
.sin_family
= AF_INET
;
750 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
751 sa
->sa_sin
.sin_len
= sizeof(sa
->sa_sin
);
752 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
756 if (inet_pton(AF_INET6
, addr
, &sa
->sa_sin6
.sin6_addr
) == 1) {
757 sa
->sa_sin6
.sin6_family
= AF_INET6
;
758 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
759 sa
->sa_sin6
.sin6_len
= sizeof(sa
->sa_sin6
);
760 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
767 void integer2timestr(uint64_t time
, char *buf
, size_t buflen
)
769 unsigned int year
, month
, day
, hour
, minute
, second
;
773 #define HOURS (24 * MINUTES)
774 #define DAYS (30 * HOURS)
775 #define MONTHS (12 * DAYS)
776 #define YEARS (MONTHS)
779 time
-= year
* YEARS
;
781 rv
= snprintf(buf
, buflen
, "%u year(s), ", year
);
785 if (time
>= MONTHS
) {
786 month
= time
/ MONTHS
;
787 time
-= month
* MONTHS
;
789 rv
= snprintf(buf
, buflen
, "%u month(s), ", month
);
797 rv
= snprintf(buf
, buflen
, "%u day(s), ", day
);
803 time
-= hour
* HOURS
;
805 rv
= snprintf(buf
, buflen
, "%u hour(s), ", hour
);
809 if (time
>= MINUTES
) {
810 minute
= time
/ MINUTES
;
811 time
-= minute
* MINUTES
;
813 rv
= snprintf(buf
, buflen
, "%u minute(s), ", minute
);
817 second
= time
% MINUTES
;
818 snprintf(buf
, buflen
, "%u second(s)", second
);
821 const char *bs_to_string(struct bfd_session
*bs
)
823 static char buf
[256];
825 bool is_mhop
= BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
);
827 pos
= snprintf(buf
, sizeof(buf
), "mhop:%s", is_mhop
? "yes" : "no");
828 if (BFD_CHECK_FLAG(bs
->flags
, BFD_SESS_FLAG_MH
)) {
829 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
,
830 " peer:%s local:%s", satostr(&bs
->mhop
.peer
),
831 satostr(&bs
->mhop
.local
));
833 if (bs
->mhop
.vrf_name
[0])
834 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " vrf:%s",
837 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
, " peer:%s",
838 satostr(&bs
->shop
.peer
));
840 if (bs
->local_address
.sa_sin
.sin_family
)
841 pos
+= snprintf(buf
+ pos
, sizeof(buf
) - pos
,
843 satostr(&bs
->local_address
));
845 if (bs
->shop
.port_name
[0])
846 snprintf(buf
+ pos
, sizeof(buf
) - pos
, " interface:%s",
855 * BFD hash data structures to find sessions.
857 static struct hash
*bfd_id_hash
;
858 static struct hash
*bfd_shop_hash
;
859 static struct hash
*bfd_mhop_hash
;
860 static struct hash
*bfd_vrf_hash
;
861 static struct hash
*bfd_iface_hash
;
863 static unsigned int bfd_id_hash_do(void *p
);
864 static unsigned int bfd_shop_hash_do(void *p
);
865 static unsigned int bfd_mhop_hash_do(void *p
);
866 static unsigned int bfd_vrf_hash_do(void *p
);
867 static unsigned int bfd_iface_hash_do(void *p
);
869 static void _shop_key(struct bfd_session
*bs
, const struct bfd_shop_key
*shop
);
870 static void _shop_key2(struct bfd_session
*bs
, const struct bfd_shop_key
*shop
);
871 static void _mhop_key(struct bfd_session
*bs
, const struct bfd_mhop_key
*mhop
);
872 static int _iface_key(struct bfd_iface
*iface
, const char *ifname
);
874 static void _bfd_free(struct hash_backet
*hb
,
875 void *arg
__attribute__((__unused__
)));
876 static void _vrf_free(void *arg
);
877 static void _iface_free(void *arg
);
879 /* BFD hash for our discriminator. */
880 static unsigned int bfd_id_hash_do(void *p
)
882 struct bfd_session
*bs
= p
;
884 return jhash_1word(bs
->discrs
.my_discr
, 0);
887 static bool bfd_id_hash_cmp(const void *n1
, const void *n2
)
889 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
891 return bs1
->discrs
.my_discr
== bs2
->discrs
.my_discr
;
894 /* BFD hash for single hop. */
895 static unsigned int bfd_shop_hash_do(void *p
)
897 struct bfd_session
*bs
= p
;
899 return jhash(&bs
->shop
, sizeof(bs
->shop
), 0);
902 static bool bfd_shop_hash_cmp(const void *n1
, const void *n2
)
904 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
906 return memcmp(&bs1
->shop
, &bs2
->shop
, sizeof(bs1
->shop
)) == 0;
909 /* BFD hash for multi hop. */
910 static unsigned int bfd_mhop_hash_do(void *p
)
912 struct bfd_session
*bs
= p
;
914 return jhash(&bs
->mhop
, sizeof(bs
->mhop
), 0);
917 static bool bfd_mhop_hash_cmp(const void *n1
, const void *n2
)
919 const struct bfd_session
*bs1
= n1
, *bs2
= n2
;
921 return memcmp(&bs1
->mhop
, &bs2
->mhop
, sizeof(bs1
->mhop
)) == 0;
924 /* BFD hash for VRFs. */
925 static unsigned int bfd_vrf_hash_do(void *p
)
927 struct bfd_vrf
*vrf
= p
;
929 return jhash_1word(vrf
->vrf_id
, 0);
932 static bool bfd_vrf_hash_cmp(const void *n1
, const void *n2
)
934 const struct bfd_vrf
*v1
= n1
, *v2
= n2
;
936 return v1
->vrf_id
== v2
->vrf_id
;
939 /* BFD hash for interfaces. */
940 static unsigned int bfd_iface_hash_do(void *p
)
942 struct bfd_iface
*iface
= p
;
944 return string_hash_make(iface
->ifname
);
947 static bool bfd_iface_hash_cmp(const void *n1
, const void *n2
)
949 const struct bfd_iface
*i1
= n1
, *i2
= n2
;
951 return strcmp(i1
->ifname
, i2
->ifname
) == 0;
954 /* Helper functions */
955 static void _shop_key(struct bfd_session
*bs
, const struct bfd_shop_key
*shop
)
959 /* Remove unused fields. */
960 switch (bs
->shop
.peer
.sa_sin
.sin_family
) {
962 bs
->shop
.peer
.sa_sin
.sin_port
= 0;
965 bs
->shop
.peer
.sa_sin6
.sin6_port
= 0;
970 static void _shop_key2(struct bfd_session
*bs
, const struct bfd_shop_key
*shop
)
973 memset(bs
->shop
.port_name
, 0, sizeof(bs
->shop
.port_name
));
976 static void _mhop_key(struct bfd_session
*bs
, const struct bfd_mhop_key
*mhop
)
980 /* Remove unused fields. */
981 switch (bs
->mhop
.peer
.sa_sin
.sin_family
) {
983 bs
->mhop
.peer
.sa_sin
.sin_port
= 0;
984 bs
->mhop
.local
.sa_sin
.sin_port
= 0;
987 bs
->mhop
.peer
.sa_sin6
.sin6_port
= 0;
988 bs
->mhop
.local
.sa_sin6
.sin6_port
= 0;
993 static int _iface_key(struct bfd_iface
*iface
, const char *ifname
)
995 size_t slen
= sizeof(iface
->ifname
);
997 memset(iface
->ifname
, 0, slen
);
998 if (strlcpy(iface
->ifname
, ifname
, slen
) >= slen
)
1005 * Hash public interface / exported functions.
1008 /* Lookup functions. */
1009 struct bfd_session
*bfd_id_lookup(uint32_t id
)
1011 struct bfd_session bs
;
1013 bs
.discrs
.my_discr
= id
;
1015 return hash_lookup(bfd_id_hash
, &bs
);
1018 struct bfd_session
*bfd_shop_lookup(struct bfd_shop_key shop
)
1020 struct bfd_session bs
, *bsp
;
1022 _shop_key(&bs
, &shop
);
1024 bsp
= hash_lookup(bfd_shop_hash
, &bs
);
1025 if (bsp
== NULL
&& bs
.shop
.port_name
[0] != 0) {
1027 * Since the local interface spec is optional, try
1028 * searching the key without it as well.
1030 _shop_key2(&bs
, &shop
);
1031 bsp
= hash_lookup(bfd_shop_hash
, &bs
);
1037 struct bfd_session
*bfd_mhop_lookup(struct bfd_mhop_key mhop
)
1039 struct bfd_session bs
;
1041 _mhop_key(&bs
, &mhop
);
1043 return hash_lookup(bfd_mhop_hash
, &bs
);
1046 struct bfd_vrf
*bfd_vrf_lookup(int vrf_id
)
1050 vrf
.vrf_id
= vrf_id
;
1052 return hash_lookup(bfd_vrf_hash
, &vrf
);
1055 struct bfd_iface
*bfd_iface_lookup(const char *ifname
)
1057 struct bfd_iface iface
;
1059 if (_iface_key(&iface
, ifname
) != 0)
1062 return hash_lookup(bfd_iface_hash
, &iface
);
1068 * Delete functions searches and remove the item from the hash and
1069 * returns a pointer to the removed item data. If the item was not found
1070 * then it returns NULL.
1072 * The data stored inside the hash is not free()ed, so you must do it
1073 * manually after getting the pointer back.
1075 struct bfd_session
*bfd_id_delete(uint32_t id
)
1077 struct bfd_session bs
;
1079 bs
.discrs
.my_discr
= id
;
1081 return hash_release(bfd_id_hash
, &bs
);
1084 struct bfd_session
*bfd_shop_delete(struct bfd_shop_key shop
)
1086 struct bfd_session bs
, *bsp
;
1088 _shop_key(&bs
, &shop
);
1089 bsp
= hash_release(bfd_shop_hash
, &bs
);
1090 if (bsp
== NULL
&& shop
.port_name
[0] != 0) {
1092 * Since the local interface spec is optional, try
1093 * searching the key without it as well.
1095 _shop_key2(&bs
, &shop
);
1096 bsp
= hash_release(bfd_shop_hash
, &bs
);
1102 struct bfd_session
*bfd_mhop_delete(struct bfd_mhop_key mhop
)
1104 struct bfd_session bs
;
1106 _mhop_key(&bs
, &mhop
);
1108 return hash_release(bfd_mhop_hash
, &bs
);
1111 struct bfd_vrf
*bfd_vrf_delete(int vrf_id
)
1115 vrf
.vrf_id
= vrf_id
;
1117 return hash_release(bfd_vrf_hash
, &vrf
);
1120 struct bfd_iface
*bfd_iface_delete(const char *ifname
)
1122 struct bfd_iface iface
;
1124 if (_iface_key(&iface
, ifname
) != 0)
1127 return hash_release(bfd_iface_hash
, &iface
);
1130 /* Iteration functions. */
1131 void bfd_id_iterate(hash_iter_func hif
, void *arg
)
1133 hash_iterate(bfd_id_hash
, hif
, arg
);
1136 void bfd_shop_iterate(hash_iter_func hif
, void *arg
)
1138 hash_iterate(bfd_shop_hash
, hif
, arg
);
1141 void bfd_mhop_iterate(hash_iter_func hif
, void *arg
)
1143 hash_iterate(bfd_mhop_hash
, hif
, arg
);
1146 void bfd_vrf_iterate(hash_iter_func hif
, void *arg
)
1148 hash_iterate(bfd_vrf_hash
, hif
, arg
);
1151 void bfd_iface_iterate(hash_iter_func hif
, void *arg
)
1153 hash_iterate(bfd_iface_hash
, hif
, arg
);
1159 * Inserts session into hash and returns `true` on success, otherwise
1162 bool bfd_id_insert(struct bfd_session
*bs
)
1164 return (hash_get(bfd_id_hash
, bs
, hash_alloc_intern
) == bs
);
1167 bool bfd_shop_insert(struct bfd_session
*bs
)
1169 return (hash_get(bfd_shop_hash
, bs
, hash_alloc_intern
) == bs
);
1172 bool bfd_mhop_insert(struct bfd_session
*bs
)
1174 return (hash_get(bfd_mhop_hash
, bs
, hash_alloc_intern
) == bs
);
1177 bool bfd_vrf_insert(struct bfd_vrf
*vrf
)
1179 return (hash_get(bfd_vrf_hash
, vrf
, hash_alloc_intern
) == vrf
);
1182 bool bfd_iface_insert(struct bfd_iface
*iface
)
1184 return (hash_get(bfd_iface_hash
, iface
, hash_alloc_intern
) == iface
);
1187 void bfd_initialize(void)
1189 bfd_id_hash
= hash_create(bfd_id_hash_do
, bfd_id_hash_cmp
,
1190 "BFD discriminator hash");
1191 bfd_shop_hash
= hash_create(bfd_shop_hash_do
, bfd_shop_hash_cmp
,
1192 "BFD single hop hash");
1193 bfd_mhop_hash
= hash_create(bfd_mhop_hash_do
, bfd_mhop_hash_cmp
,
1194 "BFD multihop hop hash");
1196 hash_create(bfd_vrf_hash_do
, bfd_vrf_hash_cmp
, "BFD VRF hash");
1197 bfd_iface_hash
= hash_create(bfd_iface_hash_do
, bfd_iface_hash_cmp
,
1198 "BFD interface hash");
1201 static void _bfd_free(struct hash_backet
*hb
,
1202 void *arg
__attribute__((__unused__
)))
1204 struct bfd_session
*bs
= hb
->data
;
1206 bfd_session_free(bs
);
1209 static void _vrf_free(void *arg
)
1211 struct bfd_vrf
*vrf
= arg
;
1213 XFREE(MTYPE_BFDD_CONFIG
, vrf
);
1216 static void _iface_free(void *arg
)
1218 struct bfd_iface
*iface
= arg
;
1220 XFREE(MTYPE_BFDD_CONFIG
, iface
);
1223 void bfd_shutdown(void)
1226 * Close and free all BFD sessions.
1228 * _bfd_free() will call bfd_session_free() which will take care
1229 * of removing the session from all hashes, so we just run an
1230 * assert() here to make sure it really happened.
1232 bfd_id_iterate(_bfd_free
, NULL
);
1233 assert(bfd_shop_hash
->count
== 0);
1234 assert(bfd_mhop_hash
->count
== 0);
1236 /* Clean the VRF and interface hashes. */
1237 hash_clean(bfd_vrf_hash
, _vrf_free
);
1238 hash_clean(bfd_iface_hash
, _iface_free
);
1240 /* Now free the hashes themselves. */
1241 hash_free(bfd_id_hash
);
1242 hash_free(bfd_shop_hash
);
1243 hash_free(bfd_mhop_hash
);
1244 hash_free(bfd_vrf_hash
);
1245 hash_free(bfd_iface_hash
);