summaryrefslogtreecommitdiff
path: root/bgpd/bgp_keepalives.c
blob: 4944e3ea23276b8749b8f74aa64ac8fa6469ad2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* BGP Keepalives.
 *
 * Implemented server-style in a pthread.
 * --------------------------------------
 * Copyright (C) 2017 Cumulus Networks, Inc.
 *
 * This file is part of Free Range Routing.
 *
 * Free Range Routing is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * Free Range Routing is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GN5U General Public License along
 * with Free Range Routing; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
#include <zebra.h>
#include <signal.h>
#include <sys/time.h>

#include "thread.h"
#include "log.h"
#include "vty.h"
#include "monotime.h"

#include "bgpd/bgpd.h"
#include "bgpd/bgp_keepalives.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_packet.h"

/**
 * Peer KeepAlive Timer.
 * Associates a peer with the time of its last keepalive.
 */
struct pkat {
	// the peer to send keepalives to
	struct peer *peer;
	// absolute time of last keepalive sent
	struct timeval last;
};

/* List of peers we are sending keepalives for, and associated mutex. */
static pthread_mutex_t peerlist_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t peerlist_cond;
static struct list *peerlist;

/* Thread control flag. */
bool bgp_keepalives_thread_run;

static struct pkat *pkat_new(struct peer *peer)
{
	struct pkat *pkat = XMALLOC(MTYPE_TMP, sizeof(struct pkat));
	pkat->peer = peer;
	monotime(&pkat->last);
	return pkat;
}

static void pkat_del(void *pkat)
{
	XFREE(MTYPE_TMP, pkat);
}
/**
 * Cleanup thread resources at termination.
 *
 * @param arg not used
 */
static void cleanup_handler(void *arg)
{
	if (peerlist)
		list_delete(peerlist);

	peerlist = NULL;

	pthread_mutex_unlock(&peerlist_mtx);
	pthread_cond_destroy(&peerlist_cond);
	memset(&peerlist_cond, 0, sizeof(peerlist_cond));
}

/*
 * Walks the list of peers, sending keepalives to those that are due for them.
 *
 * For any given peer, if the elapsed time since its last keepalive exceeds its
 * configured keepalive timer, a keepalive is sent to the peer and its
 * last-sent time is reset. Additionally, If the elapsed time does not exceed
 * the configured keepalive timer, but the time until the next keepalive is due
 * is within a hardcoded tolerance, a keepalive is sent as if the configured
 * timer was exceeded. Doing this helps alleviate nanosecond sleeps between
 * ticks by grouping together peers who are due for keepalives at roughly the
 * same time. This tolerance value is arbitrarily chosen to be 100ms.
 *
 * In addition, this function calculates the maximum amount of time that the
 * keepalive thread can sleep before another tick needs to take place. This is
 * equivalent to shortest time until a keepalive is due for any one peer.
 *
 * @return maximum time to wait until next update (0 if infinity)
 */
static struct timeval update()
{
	struct listnode *ln;
	struct pkat *pkat;

	int update_set = 0;		// whether next_update has been set
	struct timeval next_update;     // max sleep until next tick
	static struct timeval elapsed;  // elapsed time since keepalive
	static struct timeval ka = {0}; // peer->v_keepalive as a timeval
	static struct timeval diff;     // ka - elapsed

	// see function comment
	static struct timeval tolerance = {0, 100000};

	for (ALL_LIST_ELEMENTS_RO(peerlist, ln, pkat)) {
		// calculate elapsed time since last keepalive
		monotime_since(&pkat->last, &elapsed);

		// calculate difference between elapsed time and configured time
		ka.tv_sec = pkat->peer->v_keepalive;
		timersub(&ka, &elapsed, &diff);

		int send_keepalive = elapsed.tv_sec >= ka.tv_sec
				     || timercmp(&diff, &tolerance, <);

		if (send_keepalive) {
			if (bgp_debug_neighbor_events(pkat->peer))
				zlog_debug(
					"%s [FSM] Timer (keepalive timer expire)",
					pkat->peer->host);

			bgp_keepalive_send(pkat->peer);
			monotime(&pkat->last);
			memset(&elapsed, 0x00, sizeof(struct timeval));
			diff = ka; // time until next keepalive == peer
				   // keepalive time
		}

		// if calculated next update for this peer < current delay, use
		// it
		if (!update_set || timercmp(&diff, &next_update, <)) {
			next_update = diff;
			update_set = 1;
		}
	}

	return next_update;
}

void *peer_keepalives_start(void *arg)
{
	struct timeval currtime = {0, 0};
	struct timeval next_update = {0, 0};
	struct timespec next_update_ts = {0, 0};

	// initialize synchronization primitives
	pthread_mutex_lock(&peerlist_mtx);

	// use monotonic clock with condition variable
	pthread_condattr_t attrs;
	pthread_condattr_init(&attrs);
	pthread_condattr_setclock(&attrs, CLOCK_MONOTONIC);
	pthread_cond_init(&peerlist_cond, &attrs);

	// initialize peerlist
	peerlist = list_new();
	peerlist->del = pkat_del;

	// register cleanup handlers
	pthread_cleanup_push(&cleanup_handler, NULL);

	bgp_keepalives_thread_run = true;

	while (bgp_keepalives_thread_run) {
		if (peerlist->count > 0)
			pthread_cond_timedwait(&peerlist_cond, &peerlist_mtx,
					       &next_update_ts);
		else
			while (peerlist->count == 0
			       && bgp_keepalives_thread_run)
				pthread_cond_wait(&peerlist_cond,
						  &peerlist_mtx);

		monotime(&currtime);
		next_update = update();
		timeradd(&currtime, &next_update, &next_update);
		TIMEVAL_TO_TIMESPEC(&next_update, &next_update_ts);
	}

	// clean up
	pthread_cleanup_pop(1);

	return NULL;
}

/* --- thread external functions ------------------------------------------- */

void peer_keepalives_on(struct peer *peer)
{
	pthread_mutex_lock(&peerlist_mtx);
	{
		struct listnode *ln, *nn;
		struct pkat *pkat;

		for (ALL_LIST_ELEMENTS(peerlist, ln, nn, pkat))
			if (pkat->peer == peer) {
				pthread_mutex_unlock(&peerlist_mtx);
				return;
			}

		pkat = pkat_new(peer);
		listnode_add(peerlist, pkat);
		peer_lock(peer);
		SET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON);
	}
	pthread_mutex_unlock(&peerlist_mtx);
	peer_keepalives_wake();
}

void peer_keepalives_off(struct peer *peer)
{
	pthread_mutex_lock(&peerlist_mtx);
	{
		struct listnode *ln, *nn;
		struct pkat *pkat;

		for (ALL_LIST_ELEMENTS(peerlist, ln, nn, pkat))
			if (pkat->peer == peer) {
				XFREE(MTYPE_TMP, pkat);
				list_delete_node(peerlist, ln);
				peer_unlock(peer);
			}

		UNSET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON);
	}
	pthread_mutex_unlock(&peerlist_mtx);
}

void peer_keepalives_wake()
{
	pthread_mutex_lock(&peerlist_mtx);
	{
		pthread_cond_signal(&peerlist_cond);
	}
	pthread_mutex_unlock(&peerlist_mtx);
}