2 * Implements a producer thread to generate BGP keepalives for peers.
3 * Copyright (C) 2017 Cumulus Networks, Inc.
6 * This file is part of FRRouting.
8 * FRRouting is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2, or (at your option) any later
13 * FRRouting is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
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
23 /* clang-format off */
25 #include <pthread.h> // for pthread_mutex_lock, pthread_mutex_unlock
27 #include "frr_pthread.h" // for frr_pthread
28 #include "hash.h" // for hash, hash_clean, hash_create_size...
29 #include "log.h" // for zlog_debug
30 #include "memory.h" // for MTYPE_TMP, XFREE, XCALLOC, XMALLOC
31 #include "monotime.h" // for monotime, monotime_since
33 #include "bgpd/bgpd.h" // for peer, PEER_THREAD_KEEPALIVES_ON, peer...
34 #include "bgpd/bgp_debug.h" // for bgp_debug_neighbor_events
35 #include "bgpd/bgp_packet.h" // for bgp_keepalive_send
36 #include "bgpd/bgp_keepalives.h"
40 * Peer KeepAlive Timer.
41 * Associates a peer with the time of its last keepalive.
44 // the peer to send keepalives to
46 // absolute time of last keepalive sent
50 /* List of peers we are sending keepalives for, and associated mutex. */
51 static pthread_mutex_t
*peerhash_mtx
;
52 static pthread_cond_t
*peerhash_cond
;
53 static struct hash
*peerhash
;
55 /* Thread control flag. */
56 bool bgp_keepalives_thread_run
= false;
58 static struct pkat
*pkat_new(struct peer
*peer
)
60 struct pkat
*pkat
= XMALLOC(MTYPE_TMP
, sizeof(struct pkat
));
62 monotime(&pkat
->last
);
66 static void pkat_del(void *pkat
)
68 XFREE(MTYPE_TMP
, pkat
);
73 * Callback for hash_iterate. Determines if a peer needs a keepalive and if so,
74 * generates and sends it.
76 * For any given peer, if the elapsed time since its last keepalive exceeds its
77 * configured keepalive timer, a keepalive is sent to the peer and its
78 * last-sent time is reset. Additionally, If the elapsed time does not exceed
79 * the configured keepalive timer, but the time until the next keepalive is due
80 * is within a hardcoded tolerance, a keepalive is sent as if the configured
81 * timer was exceeded. Doing this helps alleviate nanosecond sleeps between
82 * ticks by grouping together peers who are due for keepalives at roughly the
83 * same time. This tolerance value is arbitrarily chosen to be 100ms.
85 * In addition, this function calculates the maximum amount of time that the
86 * keepalive thread can sleep before another tick needs to take place. This is
87 * equivalent to shortest time until a keepalive is due for any one peer.
89 * @return maximum time to wait until next update (0 if infinity)
91 static void peer_process(struct hash_backet
*hb
, void *arg
)
93 struct pkat
*pkat
= hb
->data
;
95 struct timeval
*next_update
= arg
;
97 static struct timeval elapsed
; // elapsed time since keepalive
98 static struct timeval ka
= {0}; // peer->v_keepalive as a timeval
99 static struct timeval diff
; // ka - elapsed
101 static struct timeval tolerance
= {0, 100000};
103 // calculate elapsed time since last keepalive
104 monotime_since(&pkat
->last
, &elapsed
);
106 // calculate difference between elapsed time and configured time
107 ka
.tv_sec
= pkat
->peer
->v_keepalive
;
108 timersub(&ka
, &elapsed
, &diff
);
111 elapsed
.tv_sec
>= ka
.tv_sec
|| timercmp(&diff
, &tolerance
, <);
113 if (send_keepalive
) {
114 if (bgp_debug_neighbor_events(pkat
->peer
))
115 zlog_debug("%s [FSM] Timer (keepalive timer expire)",
118 bgp_keepalive_send(pkat
->peer
);
119 monotime(&pkat
->last
);
120 memset(&elapsed
, 0x00, sizeof(struct timeval
));
121 diff
= ka
; // time until next keepalive == peer keepalive time
124 // if calculated next update for this peer < current delay, use it
125 if (next_update
->tv_sec
<= 0 || timercmp(&diff
, next_update
, <))
129 static int peer_hash_cmp(const void *f
, const void *s
)
131 const struct pkat
*p1
= f
;
132 const struct pkat
*p2
= s
;
133 return p1
->peer
== p2
->peer
;
136 static unsigned int peer_hash_key(void *arg
)
138 struct pkat
*pkat
= arg
;
139 return (uintptr_t)pkat
->peer
;
142 void bgp_keepalives_init()
144 peerhash_mtx
= XCALLOC(MTYPE_TMP
, sizeof(pthread_mutex_t
));
145 peerhash_cond
= XCALLOC(MTYPE_TMP
, sizeof(pthread_cond_t
));
148 pthread_mutex_init(peerhash_mtx
, NULL
);
150 // use monotonic clock with condition variable
151 pthread_condattr_t attrs
;
152 pthread_condattr_init(&attrs
);
153 pthread_condattr_setclock(&attrs
, CLOCK_MONOTONIC
);
154 pthread_cond_init(peerhash_cond
, &attrs
);
155 pthread_condattr_destroy(&attrs
);
157 // initialize peer hashtable
158 peerhash
= hash_create_size(2048, peer_hash_key
, peer_hash_cmp
, NULL
);
161 static void bgp_keepalives_finish(void *arg
)
163 bgp_keepalives_thread_run
= false;
166 hash_clean(peerhash
, pkat_del
);
172 pthread_mutex_unlock(peerhash_mtx
);
173 pthread_mutex_destroy(peerhash_mtx
);
174 pthread_cond_destroy(peerhash_cond
);
176 XFREE(MTYPE_TMP
, peerhash_mtx
);
177 XFREE(MTYPE_TMP
, peerhash_cond
);
181 * Entry function for peer keepalive generation pthread.
183 * bgp_keepalives_init() must be called prior to this.
185 void *bgp_keepalives_start(void *arg
)
187 struct timeval currtime
= {0, 0};
188 struct timeval aftertime
= {0, 0};
189 struct timeval next_update
= {0, 0};
190 struct timespec next_update_ts
= {0, 0};
192 pthread_mutex_lock(peerhash_mtx
);
194 // register cleanup handler
195 pthread_cleanup_push(&bgp_keepalives_finish
, NULL
);
197 bgp_keepalives_thread_run
= true;
199 while (bgp_keepalives_thread_run
) {
200 if (peerhash
->count
> 0)
201 pthread_cond_timedwait(peerhash_cond
, peerhash_mtx
,
204 while (peerhash
->count
== 0
205 && bgp_keepalives_thread_run
)
206 pthread_cond_wait(peerhash_cond
, peerhash_mtx
);
210 next_update
.tv_sec
= -1;
212 hash_iterate(peerhash
, peer_process
, &next_update
);
213 if (next_update
.tv_sec
== -1)
214 memset(&next_update
, 0x00, sizeof(next_update
));
216 monotime_since(&currtime
, &aftertime
);
218 timeradd(&currtime
, &next_update
, &next_update
);
219 TIMEVAL_TO_TIMESPEC(&next_update
, &next_update_ts
);
223 pthread_cleanup_pop(1);
228 /* --- thread external functions ------------------------------------------- */
230 void bgp_keepalives_on(struct peer
*peer
)
232 /* placeholder bucket data to use for fast key lookups */
233 static struct pkat holder
= {0};
236 zlog_warn("%s: call bgp_keepalives_init() first", __func__
);
240 pthread_mutex_lock(peerhash_mtx
);
243 if (!hash_lookup(peerhash
, &holder
)) {
244 struct pkat
*pkat
= pkat_new(peer
);
245 hash_get(peerhash
, pkat
, hash_alloc_intern
);
248 SET_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
);
250 pthread_mutex_unlock(peerhash_mtx
);
251 bgp_keepalives_wake();
254 void bgp_keepalives_off(struct peer
*peer
)
256 /* placeholder bucket data to use for fast key lookups */
257 static struct pkat holder
= {0};
260 zlog_warn("%s: call bgp_keepalives_init() first", __func__
);
264 pthread_mutex_lock(peerhash_mtx
);
267 struct pkat
*res
= hash_release(peerhash
, &holder
);
272 UNSET_FLAG(peer
->thread_flags
, PEER_THREAD_KEEPALIVES_ON
);
274 pthread_mutex_unlock(peerhash_mtx
);
277 void bgp_keepalives_wake()
279 pthread_mutex_lock(peerhash_mtx
);
281 pthread_cond_signal(peerhash_cond
);
283 pthread_mutex_unlock(peerhash_mtx
);
286 int bgp_keepalives_stop(void **result
, struct frr_pthread
*fpt
)
288 bgp_keepalives_thread_run
= false;
289 bgp_keepalives_wake();
290 pthread_join(fpt
->thread
, result
);