]>
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 QY |
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. */ | |
bd8b71e4 QY |
51 | static pthread_mutex_t *peerhash_mtx; |
52 | static pthread_cond_t *peerhash_cond; | |
53 | static struct hash *peerhash; | |
03014d48 QY |
54 | |
55 | /* Thread control flag. */ | |
419dfe6a | 56 | bool bgp_keepalives_thread_run = false; |
03014d48 QY |
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 | } | |
03014d48 | 70 | |
bd8b71e4 | 71 | |
03014d48 | 72 | /* |
424ab01d QY |
73 | * Callback for hash_iterate. Determines if a peer needs a keepalive and if so, |
74 | * generates and sends it. | |
03014d48 QY |
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 | */ | |
bd8b71e4 | 91 | static void peer_process(struct hash_backet *hb, void *arg) |
03014d48 | 92 | { |
bd8b71e4 QY |
93 | struct pkat *pkat = hb->data; |
94 | ||
95 | struct timeval *next_update = arg; | |
03014d48 | 96 | |
03014d48 QY |
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 | ||
03014d48 QY |
101 | static struct timeval tolerance = {0, 100000}; |
102 | ||
bd8b71e4 QY |
103 | // calculate elapsed time since last keepalive |
104 | monotime_since(&pkat->last, &elapsed); | |
03014d48 | 105 | |
bd8b71e4 QY |
106 | // calculate difference between elapsed time and configured time |
107 | ka.tv_sec = pkat->peer->v_keepalive; | |
108 | timersub(&ka, &elapsed, &diff); | |
03014d48 | 109 | |
bd8b71e4 QY |
110 | int send_keepalive = |
111 | elapsed.tv_sec >= ka.tv_sec || timercmp(&diff, &tolerance, <); | |
03014d48 | 112 | |
bd8b71e4 QY |
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); | |
03014d48 | 117 | |
bd8b71e4 QY |
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 | |
03014d48 QY |
122 | } |
123 | ||
bd8b71e4 QY |
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; | |
03014d48 QY |
140 | } |
141 | ||
b72b6f4f | 142 | void bgp_keepalives_init() |
03014d48 | 143 | { |
424ab01d QY |
144 | peerhash_mtx = XCALLOC(MTYPE_TMP, sizeof(pthread_mutex_t)); |
145 | peerhash_cond = XCALLOC(MTYPE_TMP, sizeof(pthread_cond_t)); | |
03014d48 | 146 | |
419dfe6a | 147 | // initialize mutex |
bd8b71e4 | 148 | pthread_mutex_init(peerhash_mtx, NULL); |
03014d48 QY |
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); | |
bd8b71e4 | 154 | pthread_cond_init(peerhash_cond, &attrs); |
419dfe6a | 155 | pthread_condattr_destroy(&attrs); |
03014d48 | 156 | |
bd8b71e4 | 157 | // initialize peer hashtable |
152456fe | 158 | peerhash = hash_create_size(2048, peer_hash_key, peer_hash_cmp, NULL); |
419dfe6a QY |
159 | } |
160 | ||
b72b6f4f | 161 | static void bgp_keepalives_finish(void *arg) |
419dfe6a QY |
162 | { |
163 | bgp_keepalives_thread_run = false; | |
164 | ||
bd8b71e4 QY |
165 | if (peerhash) { |
166 | hash_clean(peerhash, pkat_del); | |
167 | hash_free(peerhash); | |
168 | } | |
419dfe6a | 169 | |
bd8b71e4 | 170 | peerhash = NULL; |
419dfe6a | 171 | |
bd8b71e4 QY |
172 | pthread_mutex_unlock(peerhash_mtx); |
173 | pthread_mutex_destroy(peerhash_mtx); | |
174 | pthread_cond_destroy(peerhash_cond); | |
419dfe6a | 175 | |
424ab01d QY |
176 | XFREE(MTYPE_TMP, peerhash_mtx); |
177 | XFREE(MTYPE_TMP, peerhash_cond); | |
419dfe6a QY |
178 | } |
179 | ||
180 | /** | |
181 | * Entry function for peer keepalive generation pthread. | |
182 | * | |
b72b6f4f | 183 | * bgp_keepalives_init() must be called prior to this. |
419dfe6a | 184 | */ |
b72b6f4f | 185 | void *bgp_keepalives_start(void *arg) |
419dfe6a QY |
186 | { |
187 | struct timeval currtime = {0, 0}; | |
bd8b71e4 | 188 | struct timeval aftertime = {0, 0}; |
419dfe6a QY |
189 | struct timeval next_update = {0, 0}; |
190 | struct timespec next_update_ts = {0, 0}; | |
191 | ||
bd8b71e4 | 192 | pthread_mutex_lock(peerhash_mtx); |
03014d48 | 193 | |
419dfe6a | 194 | // register cleanup handler |
b72b6f4f | 195 | pthread_cleanup_push(&bgp_keepalives_finish, NULL); |
03014d48 QY |
196 | |
197 | bgp_keepalives_thread_run = true; | |
198 | ||
199 | while (bgp_keepalives_thread_run) { | |
bd8b71e4 QY |
200 | if (peerhash->count > 0) |
201 | pthread_cond_timedwait(peerhash_cond, peerhash_mtx, | |
03014d48 QY |
202 | &next_update_ts); |
203 | else | |
bd8b71e4 | 204 | while (peerhash->count == 0 |
03014d48 | 205 | && bgp_keepalives_thread_run) |
bd8b71e4 | 206 | pthread_cond_wait(peerhash_cond, peerhash_mtx); |
03014d48 QY |
207 | |
208 | monotime(&currtime); | |
bd8b71e4 QY |
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 | ||
03014d48 QY |
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 | ||
b72b6f4f | 230 | void bgp_keepalives_on(struct peer *peer) |
03014d48 | 231 | { |
bd8b71e4 QY |
232 | /* placeholder bucket data to use for fast key lookups */ |
233 | static struct pkat holder = {0}; | |
234 | ||
235 | pthread_mutex_lock(peerhash_mtx); | |
03014d48 | 236 | { |
bd8b71e4 QY |
237 | holder.peer = peer; |
238 | if (!hash_lookup(peerhash, &holder)) { | |
239 | struct pkat *pkat = pkat_new(peer); | |
240 | hash_get(peerhash, pkat, hash_alloc_intern); | |
241 | peer_lock(peer); | |
242 | } | |
49507a6f | 243 | SET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON); |
03014d48 | 244 | } |
bd8b71e4 | 245 | pthread_mutex_unlock(peerhash_mtx); |
b72b6f4f | 246 | bgp_keepalives_wake(); |
03014d48 QY |
247 | } |
248 | ||
b72b6f4f | 249 | void bgp_keepalives_off(struct peer *peer) |
03014d48 | 250 | { |
bd8b71e4 QY |
251 | /* placeholder bucket data to use for fast key lookups */ |
252 | static struct pkat holder = {0}; | |
49507a6f | 253 | |
bd8b71e4 QY |
254 | pthread_mutex_lock(peerhash_mtx); |
255 | { | |
256 | holder.peer = peer; | |
257 | struct pkat *res = hash_release(peerhash, &holder); | |
258 | if (res) { | |
259 | pkat_del(res); | |
260 | peer_unlock(peer); | |
261 | } | |
49507a6f | 262 | UNSET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON); |
03014d48 | 263 | } |
bd8b71e4 | 264 | pthread_mutex_unlock(peerhash_mtx); |
03014d48 QY |
265 | } |
266 | ||
b72b6f4f | 267 | void bgp_keepalives_wake() |
03014d48 | 268 | { |
bd8b71e4 | 269 | pthread_mutex_lock(peerhash_mtx); |
03014d48 | 270 | { |
bd8b71e4 | 271 | pthread_cond_signal(peerhash_cond); |
03014d48 | 272 | } |
bd8b71e4 | 273 | pthread_mutex_unlock(peerhash_mtx); |
03014d48 | 274 | } |
0ca8b79f | 275 | |
b72b6f4f | 276 | int bgp_keepalives_stop(void **result, struct frr_pthread *fpt) |
0ca8b79f | 277 | { |
0ca8b79f | 278 | bgp_keepalives_thread_run = false; |
b72b6f4f | 279 | bgp_keepalives_wake(); |
0ca8b79f QY |
280 | pthread_join(fpt->thread, result); |
281 | return 0; | |
282 | } |