]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_keepalives.c
Merge pull request #1145 from qlyoung/bgpd-pthreads-frr
[mirror_frr.git] / bgpd / bgp_keepalives.c
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
21 */
22
23 /* clang-format off */
24 #include <zebra.h>
25 #include <pthread.h> // for pthread_mutex_lock, pthread_mutex_unlock
26
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
32
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"
37 /* clang-format on */
38
39 /**
40 * Peer KeepAlive Timer.
41 * Associates a peer with the time of its last keepalive.
42 */
43 struct pkat {
44 // the peer to send keepalives to
45 struct peer *peer;
46 // absolute time of last keepalive sent
47 struct timeval last;
48 };
49
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;
54
55 /* Thread control flag. */
56 bool bgp_keepalives_thread_run = false;
57
58 static struct pkat *pkat_new(struct peer *peer)
59 {
60 struct pkat *pkat = XMALLOC(MTYPE_TMP, sizeof(struct pkat));
61 pkat->peer = peer;
62 monotime(&pkat->last);
63 return pkat;
64 }
65
66 static void pkat_del(void *pkat)
67 {
68 XFREE(MTYPE_TMP, pkat);
69 }
70
71
72 /*
73 * Callback for hash_iterate. Determines if a peer needs a keepalive and if so,
74 * generates and sends it.
75 *
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.
84 *
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.
88 *
89 * @return maximum time to wait until next update (0 if infinity)
90 */
91 static void peer_process(struct hash_backet *hb, void *arg)
92 {
93 struct pkat *pkat = hb->data;
94
95 struct timeval *next_update = arg;
96
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
100
101 static struct timeval tolerance = {0, 100000};
102
103 // calculate elapsed time since last keepalive
104 monotime_since(&pkat->last, &elapsed);
105
106 // calculate difference between elapsed time and configured time
107 ka.tv_sec = pkat->peer->v_keepalive;
108 timersub(&ka, &elapsed, &diff);
109
110 int send_keepalive =
111 elapsed.tv_sec >= ka.tv_sec || timercmp(&diff, &tolerance, <);
112
113 if (send_keepalive) {
114 if (bgp_debug_neighbor_events(pkat->peer))
115 zlog_debug("%s [FSM] Timer (keepalive timer expire)",
116 pkat->peer->host);
117
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
122 }
123
124 // if calculated next update for this peer < current delay, use it
125 if (next_update->tv_sec <= 0 || timercmp(&diff, next_update, <))
126 *next_update = diff;
127 }
128
129 static int peer_hash_cmp(const void *f, const void *s)
130 {
131 const struct pkat *p1 = f;
132 const struct pkat *p2 = s;
133 return p1->peer == p2->peer;
134 }
135
136 static unsigned int peer_hash_key(void *arg)
137 {
138 struct pkat *pkat = arg;
139 return (uintptr_t)pkat->peer;
140 }
141
142 void bgp_keepalives_init()
143 {
144 peerhash_mtx = XCALLOC(MTYPE_TMP, sizeof(pthread_mutex_t));
145 peerhash_cond = XCALLOC(MTYPE_TMP, sizeof(pthread_cond_t));
146
147 // initialize mutex
148 pthread_mutex_init(peerhash_mtx, NULL);
149
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);
156
157 // initialize peer hashtable
158 peerhash = hash_create_size(2048, peer_hash_key, peer_hash_cmp, NULL);
159 }
160
161 static void bgp_keepalives_finish(void *arg)
162 {
163 bgp_keepalives_thread_run = false;
164
165 if (peerhash) {
166 hash_clean(peerhash, pkat_del);
167 hash_free(peerhash);
168 }
169
170 peerhash = NULL;
171
172 pthread_mutex_unlock(peerhash_mtx);
173 pthread_mutex_destroy(peerhash_mtx);
174 pthread_cond_destroy(peerhash_cond);
175
176 XFREE(MTYPE_TMP, peerhash_mtx);
177 XFREE(MTYPE_TMP, peerhash_cond);
178 }
179
180 /**
181 * Entry function for peer keepalive generation pthread.
182 *
183 * bgp_keepalives_init() must be called prior to this.
184 */
185 void *bgp_keepalives_start(void *arg)
186 {
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};
191
192 pthread_mutex_lock(peerhash_mtx);
193
194 // register cleanup handler
195 pthread_cleanup_push(&bgp_keepalives_finish, NULL);
196
197 bgp_keepalives_thread_run = true;
198
199 while (bgp_keepalives_thread_run) {
200 if (peerhash->count > 0)
201 pthread_cond_timedwait(peerhash_cond, peerhash_mtx,
202 &next_update_ts);
203 else
204 while (peerhash->count == 0
205 && bgp_keepalives_thread_run)
206 pthread_cond_wait(peerhash_cond, peerhash_mtx);
207
208 monotime(&currtime);
209
210 next_update.tv_sec = -1;
211
212 hash_iterate(peerhash, peer_process, &next_update);
213 if (next_update.tv_sec == -1)
214 memset(&next_update, 0x00, sizeof(next_update));
215
216 monotime_since(&currtime, &aftertime);
217
218 timeradd(&currtime, &next_update, &next_update);
219 TIMEVAL_TO_TIMESPEC(&next_update, &next_update_ts);
220 }
221
222 // clean up
223 pthread_cleanup_pop(1);
224
225 return NULL;
226 }
227
228 /* --- thread external functions ------------------------------------------- */
229
230 void bgp_keepalives_on(struct peer *peer)
231 {
232 /* placeholder bucket data to use for fast key lookups */
233 static struct pkat holder = {0};
234
235 if (!peerhash_mtx) {
236 zlog_warn("%s: call bgp_keepalives_init() first", __func__);
237 return;
238 }
239
240 pthread_mutex_lock(peerhash_mtx);
241 {
242 holder.peer = peer;
243 if (!hash_lookup(peerhash, &holder)) {
244 struct pkat *pkat = pkat_new(peer);
245 hash_get(peerhash, pkat, hash_alloc_intern);
246 peer_lock(peer);
247 }
248 SET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON);
249 }
250 pthread_mutex_unlock(peerhash_mtx);
251 bgp_keepalives_wake();
252 }
253
254 void bgp_keepalives_off(struct peer *peer)
255 {
256 /* placeholder bucket data to use for fast key lookups */
257 static struct pkat holder = {0};
258
259 if (!peerhash_mtx) {
260 zlog_warn("%s: call bgp_keepalives_init() first", __func__);
261 return;
262 }
263
264 pthread_mutex_lock(peerhash_mtx);
265 {
266 holder.peer = peer;
267 struct pkat *res = hash_release(peerhash, &holder);
268 if (res) {
269 pkat_del(res);
270 peer_unlock(peer);
271 }
272 UNSET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON);
273 }
274 pthread_mutex_unlock(peerhash_mtx);
275 }
276
277 void bgp_keepalives_wake()
278 {
279 pthread_mutex_lock(peerhash_mtx);
280 {
281 pthread_cond_signal(peerhash_cond);
282 }
283 pthread_mutex_unlock(peerhash_mtx);
284 }
285
286 int bgp_keepalives_stop(void **result, struct frr_pthread *fpt)
287 {
288 bgp_keepalives_thread_run = false;
289 bgp_keepalives_wake();
290 pthread_join(fpt->thread, result);
291 return 0;
292 }