]>
Commit | Line | Data |
---|---|---|
5c0c651c QY |
1 | /* BGP Keepalives. |
2 | * Implements a producer thread to generate BGP keepalives for peers. | |
3 | * Copyright (C) 2017 Cumulus Networks, Inc. | |
4 | * Quentin Young | |
5 | * | |
6 | * This file is part of FRRouting. | |
7 | * | |
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 | |
11 | * version. | |
12 | * | |
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 | |
16 | * details. | |
17 | * | |
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 | |
03014d48 | 21 | */ |
5c0c651c | 22 | |
6ee8ea1c | 23 | /* clang-format off */ |
03014d48 | 24 | #include <zebra.h> |
6ee8ea1c | 25 | #include <pthread.h> // for pthread_mutex_lock, pthread_mutex_unlock |
03014d48 | 26 | |
6ee8ea1c QY |
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 | |
03014d48 | 32 | |
6ee8ea1c QY |
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 | |
03014d48 | 36 | #include "bgpd/bgp_keepalives.h" |
6ee8ea1c | 37 | /* clang-format on */ |
03014d48 | 38 | |
a715eab3 | 39 | /* |
03014d48 QY |
40 | * Peer KeepAlive Timer. |
41 | * Associates a peer with the time of its last keepalive. | |
42 | */ | |
43 | struct pkat { | |
a715eab3 | 44 | /* the peer to send keepalives to */ |
03014d48 | 45 | struct peer *peer; |
a715eab3 | 46 | /* absolute time of last keepalive sent */ |
03014d48 QY |
47 | struct timeval last; |
48 | }; | |
49 | ||
50 | /* List of peers we are sending keepalives for, and associated mutex. */ | |
bd8b71e4 QY |
51 | static pthread_mutex_t *peerhash_mtx; |
52 | static pthread_cond_t *peerhash_cond; | |
53 | static struct hash *peerhash; | |
03014d48 | 54 | |
03014d48 QY |
55 | static struct pkat *pkat_new(struct peer *peer) |
56 | { | |
57 | struct pkat *pkat = XMALLOC(MTYPE_TMP, sizeof(struct pkat)); | |
58 | pkat->peer = peer; | |
59 | monotime(&pkat->last); | |
60 | return pkat; | |
61 | } | |
62 | ||
63 | static void pkat_del(void *pkat) | |
64 | { | |
65 | XFREE(MTYPE_TMP, pkat); | |
66 | } | |
03014d48 | 67 | |
bd8b71e4 | 68 | |
03014d48 | 69 | /* |
424ab01d QY |
70 | * Callback for hash_iterate. Determines if a peer needs a keepalive and if so, |
71 | * generates and sends it. | |
03014d48 QY |
72 | * |
73 | * For any given peer, if the elapsed time since its last keepalive exceeds its | |
74 | * configured keepalive timer, a keepalive is sent to the peer and its | |
75 | * last-sent time is reset. Additionally, If the elapsed time does not exceed | |
76 | * the configured keepalive timer, but the time until the next keepalive is due | |
77 | * is within a hardcoded tolerance, a keepalive is sent as if the configured | |
78 | * timer was exceeded. Doing this helps alleviate nanosecond sleeps between | |
79 | * ticks by grouping together peers who are due for keepalives at roughly the | |
80 | * same time. This tolerance value is arbitrarily chosen to be 100ms. | |
81 | * | |
82 | * In addition, this function calculates the maximum amount of time that the | |
83 | * keepalive thread can sleep before another tick needs to take place. This is | |
84 | * equivalent to shortest time until a keepalive is due for any one peer. | |
85 | * | |
86 | * @return maximum time to wait until next update (0 if infinity) | |
87 | */ | |
e3b78da8 | 88 | static void peer_process(struct hash_bucket *hb, void *arg) |
03014d48 | 89 | { |
bd8b71e4 QY |
90 | struct pkat *pkat = hb->data; |
91 | ||
92 | struct timeval *next_update = arg; | |
03014d48 | 93 | |
03014d48 QY |
94 | static struct timeval elapsed; // elapsed time since keepalive |
95 | static struct timeval ka = {0}; // peer->v_keepalive as a timeval | |
96 | static struct timeval diff; // ka - elapsed | |
97 | ||
2b64873d | 98 | static const struct timeval tolerance = {0, 100000}; |
03014d48 | 99 | |
bfc18a02 QY |
100 | uint32_t v_ka = atomic_load_explicit(&pkat->peer->v_keepalive, |
101 | memory_order_relaxed); | |
102 | ||
103 | /* 0 keepalive timer means no keepalives */ | |
104 | if (v_ka == 0) | |
105 | return; | |
106 | ||
a715eab3 | 107 | /* calculate elapsed time since last keepalive */ |
bd8b71e4 | 108 | monotime_since(&pkat->last, &elapsed); |
03014d48 | 109 | |
a715eab3 | 110 | /* calculate difference between elapsed time and configured time */ |
bfc18a02 | 111 | ka.tv_sec = v_ka; |
bd8b71e4 | 112 | timersub(&ka, &elapsed, &diff); |
03014d48 | 113 | |
bd8b71e4 QY |
114 | int send_keepalive = |
115 | elapsed.tv_sec >= ka.tv_sec || timercmp(&diff, &tolerance, <); | |
03014d48 | 116 | |
bd8b71e4 QY |
117 | if (send_keepalive) { |
118 | if (bgp_debug_neighbor_events(pkat->peer)) | |
119 | zlog_debug("%s [FSM] Timer (keepalive timer expire)", | |
120 | pkat->peer->host); | |
03014d48 | 121 | |
bd8b71e4 QY |
122 | bgp_keepalive_send(pkat->peer); |
123 | monotime(&pkat->last); | |
6006b807 | 124 | memset(&elapsed, 0, sizeof(elapsed)); |
a715eab3 | 125 | diff = ka; |
03014d48 QY |
126 | } |
127 | ||
a715eab3 | 128 | /* if calculated next update for this peer < current delay, use it */ |
2ccf91b1 | 129 | if (next_update->tv_sec < 0 || timercmp(&diff, next_update, <)) |
bd8b71e4 QY |
130 | *next_update = diff; |
131 | } | |
132 | ||
74df8d6d | 133 | static bool peer_hash_cmp(const void *f, const void *s) |
bd8b71e4 QY |
134 | { |
135 | const struct pkat *p1 = f; | |
136 | const struct pkat *p2 = s; | |
74df8d6d | 137 | |
bd8b71e4 QY |
138 | return p1->peer == p2->peer; |
139 | } | |
140 | ||
d8b87afe | 141 | static unsigned int peer_hash_key(const void *arg) |
bd8b71e4 | 142 | { |
d8b87afe | 143 | const struct pkat *pkat = arg; |
bd8b71e4 | 144 | return (uintptr_t)pkat->peer; |
03014d48 QY |
145 | } |
146 | ||
a715eab3 | 147 | /* Cleanup handler / deinitializer. */ |
b72b6f4f | 148 | static void bgp_keepalives_finish(void *arg) |
419dfe6a | 149 | { |
bd8b71e4 QY |
150 | if (peerhash) { |
151 | hash_clean(peerhash, pkat_del); | |
152 | hash_free(peerhash); | |
153 | } | |
419dfe6a | 154 | |
bd8b71e4 | 155 | peerhash = NULL; |
419dfe6a | 156 | |
bd8b71e4 QY |
157 | pthread_mutex_unlock(peerhash_mtx); |
158 | pthread_mutex_destroy(peerhash_mtx); | |
159 | pthread_cond_destroy(peerhash_cond); | |
419dfe6a | 160 | |
424ab01d QY |
161 | XFREE(MTYPE_TMP, peerhash_mtx); |
162 | XFREE(MTYPE_TMP, peerhash_cond); | |
419dfe6a QY |
163 | } |
164 | ||
a715eab3 | 165 | /* |
419dfe6a | 166 | * Entry function for peer keepalive generation pthread. |
419dfe6a | 167 | */ |
b72b6f4f | 168 | void *bgp_keepalives_start(void *arg) |
419dfe6a | 169 | { |
a715eab3 QY |
170 | struct frr_pthread *fpt = arg; |
171 | fpt->master->owner = pthread_self(); | |
172 | ||
419dfe6a | 173 | struct timeval currtime = {0, 0}; |
bd8b71e4 | 174 | struct timeval aftertime = {0, 0}; |
419dfe6a QY |
175 | struct timeval next_update = {0, 0}; |
176 | struct timespec next_update_ts = {0, 0}; | |
177 | ||
85ba04f3 MS |
178 | /* |
179 | * The RCU mechanism for each pthread is initialized in a "locked" | |
180 | * state. That's ok for pthreads using the frr_pthread, | |
181 | * thread_fetch event loop, because that event loop unlocks regularly. | |
182 | * For foreign pthreads, the lock needs to be unlocked so that the | |
183 | * background rcu pthread can run. | |
184 | */ | |
185 | rcu_read_unlock(); | |
186 | ||
a715eab3 QY |
187 | peerhash_mtx = XCALLOC(MTYPE_TMP, sizeof(pthread_mutex_t)); |
188 | peerhash_cond = XCALLOC(MTYPE_TMP, sizeof(pthread_cond_t)); | |
189 | ||
190 | /* initialize mutex */ | |
191 | pthread_mutex_init(peerhash_mtx, NULL); | |
192 | ||
193 | /* use monotonic clock with condition variable */ | |
194 | pthread_condattr_t attrs; | |
195 | pthread_condattr_init(&attrs); | |
196 | pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC); | |
197 | pthread_cond_init(peerhash_cond, &attrs); | |
198 | pthread_condattr_destroy(&attrs); | |
199 | ||
c80bedb8 DS |
200 | /* |
201 | * We are not using normal FRR pthread mechanics and are | |
202 | * not using fpt_run | |
203 | */ | |
204 | frr_pthread_set_name(fpt); | |
a9198bc1 | 205 | |
a715eab3 QY |
206 | /* initialize peer hashtable */ |
207 | peerhash = hash_create_size(2048, peer_hash_key, peer_hash_cmp, NULL); | |
bd8b71e4 | 208 | pthread_mutex_lock(peerhash_mtx); |
03014d48 | 209 | |
a715eab3 | 210 | /* register cleanup handler */ |
b72b6f4f | 211 | pthread_cleanup_push(&bgp_keepalives_finish, NULL); |
03014d48 | 212 | |
a715eab3 QY |
213 | /* notify anybody waiting on us that we are done starting up */ |
214 | frr_pthread_notify_running(fpt); | |
03014d48 | 215 | |
a715eab3 | 216 | while (atomic_load_explicit(&fpt->running, memory_order_relaxed)) { |
bd8b71e4 QY |
217 | if (peerhash->count > 0) |
218 | pthread_cond_timedwait(peerhash_cond, peerhash_mtx, | |
03014d48 QY |
219 | &next_update_ts); |
220 | else | |
bd8b71e4 | 221 | while (peerhash->count == 0 |
a715eab3 QY |
222 | && atomic_load_explicit(&fpt->running, |
223 | memory_order_relaxed)) | |
bd8b71e4 | 224 | pthread_cond_wait(peerhash_cond, peerhash_mtx); |
03014d48 QY |
225 | |
226 | monotime(&currtime); | |
bd8b71e4 QY |
227 | |
228 | next_update.tv_sec = -1; | |
229 | ||
230 | hash_iterate(peerhash, peer_process, &next_update); | |
231 | if (next_update.tv_sec == -1) | |
6006b807 | 232 | memset(&next_update, 0, sizeof(next_update)); |
bd8b71e4 QY |
233 | |
234 | monotime_since(&currtime, &aftertime); | |
235 | ||
03014d48 QY |
236 | timeradd(&currtime, &next_update, &next_update); |
237 | TIMEVAL_TO_TIMESPEC(&next_update, &next_update_ts); | |
238 | } | |
239 | ||
a715eab3 | 240 | /* clean up */ |
03014d48 QY |
241 | pthread_cleanup_pop(1); |
242 | ||
243 | return NULL; | |
244 | } | |
245 | ||
246 | /* --- thread external functions ------------------------------------------- */ | |
247 | ||
b72b6f4f | 248 | void bgp_keepalives_on(struct peer *peer) |
03014d48 | 249 | { |
096476dd QY |
250 | if (CHECK_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON)) |
251 | return; | |
252 | ||
1ac267a2 | 253 | struct frr_pthread *fpt = bgp_pth_ka; |
a715eab3 QY |
254 | assert(fpt->running); |
255 | ||
bd8b71e4 QY |
256 | /* placeholder bucket data to use for fast key lookups */ |
257 | static struct pkat holder = {0}; | |
258 | ||
68ede9c4 DS |
259 | /* |
260 | * We need to ensure that bgp_keepalives_init was called first | |
261 | */ | |
262 | assert(peerhash_mtx); | |
934af458 | 263 | |
cb1991af | 264 | frr_with_mutex (peerhash_mtx) { |
bd8b71e4 QY |
265 | holder.peer = peer; |
266 | if (!hash_lookup(peerhash, &holder)) { | |
267 | struct pkat *pkat = pkat_new(peer); | |
8e3aae66 | 268 | (void)hash_get(peerhash, pkat, hash_alloc_intern); |
bd8b71e4 QY |
269 | peer_lock(peer); |
270 | } | |
49507a6f | 271 | SET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON); |
03014d48 | 272 | } |
b72b6f4f | 273 | bgp_keepalives_wake(); |
03014d48 QY |
274 | } |
275 | ||
b72b6f4f | 276 | void bgp_keepalives_off(struct peer *peer) |
03014d48 | 277 | { |
096476dd QY |
278 | if (!CHECK_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON)) |
279 | return; | |
280 | ||
1ac267a2 | 281 | struct frr_pthread *fpt = bgp_pth_ka; |
a715eab3 QY |
282 | assert(fpt->running); |
283 | ||
bd8b71e4 QY |
284 | /* placeholder bucket data to use for fast key lookups */ |
285 | static struct pkat holder = {0}; | |
49507a6f | 286 | |
68ede9c4 DS |
287 | /* |
288 | * We need to ensure that bgp_keepalives_init was called first | |
289 | */ | |
290 | assert(peerhash_mtx); | |
934af458 | 291 | |
cb1991af | 292 | frr_with_mutex (peerhash_mtx) { |
bd8b71e4 QY |
293 | holder.peer = peer; |
294 | struct pkat *res = hash_release(peerhash, &holder); | |
295 | if (res) { | |
296 | pkat_del(res); | |
297 | peer_unlock(peer); | |
298 | } | |
49507a6f | 299 | UNSET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON); |
03014d48 | 300 | } |
03014d48 QY |
301 | } |
302 | ||
4d762f26 | 303 | void bgp_keepalives_wake(void) |
03014d48 | 304 | { |
cb1991af | 305 | frr_with_mutex (peerhash_mtx) { |
bd8b71e4 | 306 | pthread_cond_signal(peerhash_cond); |
03014d48 | 307 | } |
03014d48 | 308 | } |
0ca8b79f | 309 | |
a715eab3 | 310 | int bgp_keepalives_stop(struct frr_pthread *fpt, void **result) |
0ca8b79f | 311 | { |
a715eab3 QY |
312 | assert(fpt->running); |
313 | ||
314 | atomic_store_explicit(&fpt->running, false, memory_order_relaxed); | |
b72b6f4f | 315 | bgp_keepalives_wake(); |
a715eab3 | 316 | |
0ca8b79f QY |
317 | pthread_join(fpt->thread, result); |
318 | return 0; | |
319 | } |