blob: 58fbc7e2475e565e5e895ad04379df795ecbed1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INETPEER - A storage for permanent information about peers
3 *
4 * This source is covered by the GNU GPL, the same as all kernel sources.
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Authors: Andrey V. Savochkin <saw@msu.ru>
7 */
8
9#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/interrupt.h>
13#include <linux/spinlock.h>
14#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/timer.h>
16#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/net.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030020#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/inetpeer.h>
22
23/*
24 * Theory of operations.
25 * We keep one entry for each peer IP address. The nodes contains long-living
26 * information about the peer which doesn't depend on routes.
27 * At this moment this information consists only of ID field for the next
28 * outgoing IP packet. This field is incremented with each packet as encoded
29 * in inet_getid() function (include/net/inetpeer.h).
30 * At the moment of writing this notes identifier of IP packets is generated
31 * to be unpredictable using this code only for packets subjected
32 * (actually or potentially) to defragmentation. I.e. DF packets less than
33 * PMTU in size uses a constant ID and do not use this code (see
34 * ip_select_ident() in include/net/ip.h).
35 *
36 * Route cache entries hold references to our nodes.
37 * New cache entries get references via lookup by destination IP address in
38 * the avl tree. The reference is grabbed only when it's needed i.e. only
39 * when we try to output IP packet which needs an unpredictable ID (see
40 * __ip_select_ident() in net/ipv4/route.c).
41 * Nodes are removed only when reference counter goes to 0.
42 * When it's happened the node may be removed when a sufficient amount of
43 * time has been passed since its last use. The less-recently-used entry can
44 * also be removed if the pool is overloaded i.e. if the total amount of
45 * entries is greater-or-equal than the threshold.
46 *
47 * Node pool is organised as an AVL tree.
48 * Such an implementation has been chosen not just for fun. It's a way to
49 * prevent easy and efficient DoS attacks by creating hash collisions. A huge
50 * amount of long living nodes in a single hash slot would significantly delay
51 * lookups performed with disabled BHs.
52 *
53 * Serialisation issues.
Eric Dumazetaa1039e2010-06-15 08:23:14 +000054 * 1. Nodes may appear in the tree only with the pool lock held.
55 * 2. Nodes may disappear from the tree only with the pool lock held
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * AND reference count being 0.
57 * 3. Nodes appears and disappears from unused node list only under
58 * "inet_peer_unused_lock".
59 * 4. Global variable peer_total is modified under the pool lock.
60 * 5. struct inet_peer fields modification:
61 * avl_left, avl_right, avl_parent, avl_height: pool lock
Pavel Emelyanovd71209d2007-11-12 21:27:28 -080062 * unused: unused node list lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * refcnt: atomically against modifications on other CPU;
64 * usually under some other lock to prevent node disappearing
65 * dtime: unused node list lock
66 * v4daddr: unchangeable
67 * ip_id_count: idlock
68 */
69
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *peer_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define node_height(x) x->avl_height
Eric Dumazetd6cc1d62010-06-14 19:35:21 +000073
74#define peer_avl_empty ((struct inet_peer *)&peer_fake_node)
75static const struct inet_peer peer_fake_node = {
76 .avl_left = peer_avl_empty,
77 .avl_right = peer_avl_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .avl_height = 0
79};
Eric Dumazetd6cc1d62010-06-14 19:35:21 +000080
81static struct {
82 struct inet_peer *root;
Eric Dumazetaa1039e2010-06-15 08:23:14 +000083 spinlock_t lock;
Eric Dumazetd6cc1d62010-06-14 19:35:21 +000084 int total;
85} peers = {
86 .root = peer_avl_empty,
Eric Dumazetaa1039e2010-06-15 08:23:14 +000087 .lock = __SPIN_LOCK_UNLOCKED(peers.lock),
Eric Dumazetd6cc1d62010-06-14 19:35:21 +000088 .total = 0,
89};
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define PEER_MAXDEPTH 40 /* sufficient for about 2^27 nodes */
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Exported for sysctl_net_ipv4. */
Eric Dumazet243bbca2007-03-06 20:23:10 -080093int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries more
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * aggressively at this stage */
Eric Dumazet243bbca2007-03-06 20:23:10 -080095int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
96int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */
97int inet_peer_gc_mintime __read_mostly = 10 * HZ;
98int inet_peer_gc_maxtime __read_mostly = 120 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000100static struct {
101 struct list_head list;
102 spinlock_t lock;
103} unused_peers = {
104 .list = LIST_HEAD_INIT(unused_peers.list),
105 .lock = __SPIN_LOCK_UNLOCKED(unused_peers.lock),
106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static void peer_check_expire(unsigned long dummy);
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700109static DEFINE_TIMER(peer_periodic_timer, peer_check_expire, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Called from ip_output.c:ip_init */
113void __init inet_initpeers(void)
114{
115 struct sysinfo si;
116
117 /* Use the straight interface to information about memory. */
118 si_meminfo(&si);
119 /* The values below were suggested by Alexey Kuznetsov
120 * <kuznet@ms2.inr.ac.ru>. I don't have any opinion about the values
121 * myself. --SAW
122 */
123 if (si.totalram <= (32768*1024)/PAGE_SIZE)
124 inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
125 if (si.totalram <= (16384*1024)/PAGE_SIZE)
126 inet_peer_threshold >>= 1; /* about 512KB */
127 if (si.totalram <= (8192*1024)/PAGE_SIZE)
128 inet_peer_threshold >>= 2; /* about 128KB */
129
130 peer_cachep = kmem_cache_create("inet_peer_cache",
131 sizeof(struct inet_peer),
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000132 0, SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +0900133 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* All the timers, started at system startup tend
136 to synchronize. Perturb it a bit.
137 */
138 peer_periodic_timer.expires = jiffies
139 + net_random() % inet_peer_gc_maxtime
140 + inet_peer_gc_maxtime;
141 add_timer(&peer_periodic_timer);
142}
143
144/* Called with or without local BH being disabled. */
145static void unlink_from_unused(struct inet_peer *p)
146{
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000147 if (!list_empty(&p->unused)) {
148 spin_lock_bh(&unused_peers.lock);
149 list_del_init(&p->unused);
150 spin_unlock_bh(&unused_peers.lock);
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Eric Dumazet243bbca2007-03-06 20:23:10 -0800154/*
155 * Called with local BH disabled and the pool lock held.
Eric Dumazet243bbca2007-03-06 20:23:10 -0800156 */
Jianjun Kongd93191002008-11-03 00:23:42 -0800157#define lookup(_daddr, _stack) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158({ \
159 struct inet_peer *u, **v; \
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000160 \
161 stackptr = _stack; \
162 *stackptr++ = &peers.root; \
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000163 for (u = peers.root; u != peer_avl_empty; ) { \
Eric Dumazet243bbca2007-03-06 20:23:10 -0800164 if (_daddr == u->v4daddr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 break; \
Eric Dumazet243bbca2007-03-06 20:23:10 -0800166 if ((__force __u32)_daddr < (__force __u32)u->v4daddr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 v = &u->avl_left; \
168 else \
169 v = &u->avl_right; \
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000170 *stackptr++ = v; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 u = *v; \
172 } \
173 u; \
174})
175
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000176/*
177 * Called with rcu_read_lock_bh()
178 * Because we hold no lock against a writer, its quite possible we fall
179 * in an endless loop.
180 * But every pointer we follow is guaranteed to be valid thanks to RCU.
181 * We exit from this function if number of links exceeds PEER_MAXDEPTH
182 */
183static struct inet_peer *lookup_rcu_bh(__be32 daddr)
184{
185 struct inet_peer *u = rcu_dereference_bh(peers.root);
186 int count = 0;
187
188 while (u != peer_avl_empty) {
189 if (daddr == u->v4daddr) {
190 if (unlikely(!atomic_inc_not_zero(&u->refcnt)))
191 u = NULL;
192 return u;
193 }
194 if ((__force __u32)daddr < (__force __u32)u->v4daddr)
195 u = rcu_dereference_bh(u->avl_left);
196 else
197 u = rcu_dereference_bh(u->avl_right);
198 if (unlikely(++count == PEER_MAXDEPTH))
199 break;
200 }
201 return NULL;
202}
203
204/* Called with local BH disabled and the pool lock held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#define lookup_rightempty(start) \
206({ \
207 struct inet_peer *u, **v; \
208 *stackptr++ = &start->avl_left; \
209 v = &start->avl_left; \
210 for (u = *v; u->avl_right != peer_avl_empty; ) { \
211 v = &u->avl_right; \
212 *stackptr++ = v; \
213 u = *v; \
214 } \
215 u; \
216})
217
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000218/* Called with local BH disabled and the pool lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Variable names are the proof of operation correctness.
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000220 * Look into mm/map_avl.c for more detail description of the ideas.
221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static void peer_avl_rebalance(struct inet_peer **stack[],
223 struct inet_peer ***stackend)
224{
225 struct inet_peer **nodep, *node, *l, *r;
226 int lh, rh;
227
228 while (stackend > stack) {
229 nodep = *--stackend;
230 node = *nodep;
231 l = node->avl_left;
232 r = node->avl_right;
233 lh = node_height(l);
234 rh = node_height(r);
235 if (lh > rh + 1) { /* l: RH+2 */
236 struct inet_peer *ll, *lr, *lrl, *lrr;
237 int lrh;
238 ll = l->avl_left;
239 lr = l->avl_right;
240 lrh = node_height(lr);
241 if (lrh <= node_height(ll)) { /* ll: RH+1 */
242 node->avl_left = lr; /* lr: RH or RH+1 */
243 node->avl_right = r; /* r: RH */
244 node->avl_height = lrh + 1; /* RH+1 or RH+2 */
245 l->avl_left = ll; /* ll: RH+1 */
246 l->avl_right = node; /* node: RH+1 or RH+2 */
247 l->avl_height = node->avl_height + 1;
248 *nodep = l;
249 } else { /* ll: RH, lr: RH+1 */
250 lrl = lr->avl_left; /* lrl: RH or RH-1 */
251 lrr = lr->avl_right; /* lrr: RH or RH-1 */
252 node->avl_left = lrr; /* lrr: RH or RH-1 */
253 node->avl_right = r; /* r: RH */
254 node->avl_height = rh + 1; /* node: RH+1 */
255 l->avl_left = ll; /* ll: RH */
256 l->avl_right = lrl; /* lrl: RH or RH-1 */
257 l->avl_height = rh + 1; /* l: RH+1 */
258 lr->avl_left = l; /* l: RH+1 */
259 lr->avl_right = node; /* node: RH+1 */
260 lr->avl_height = rh + 2;
261 *nodep = lr;
262 }
263 } else if (rh > lh + 1) { /* r: LH+2 */
264 struct inet_peer *rr, *rl, *rlr, *rll;
265 int rlh;
266 rr = r->avl_right;
267 rl = r->avl_left;
268 rlh = node_height(rl);
269 if (rlh <= node_height(rr)) { /* rr: LH+1 */
270 node->avl_right = rl; /* rl: LH or LH+1 */
271 node->avl_left = l; /* l: LH */
272 node->avl_height = rlh + 1; /* LH+1 or LH+2 */
273 r->avl_right = rr; /* rr: LH+1 */
274 r->avl_left = node; /* node: LH+1 or LH+2 */
275 r->avl_height = node->avl_height + 1;
276 *nodep = r;
277 } else { /* rr: RH, rl: RH+1 */
278 rlr = rl->avl_right; /* rlr: LH or LH-1 */
279 rll = rl->avl_left; /* rll: LH or LH-1 */
280 node->avl_right = rll; /* rll: LH or LH-1 */
281 node->avl_left = l; /* l: LH */
282 node->avl_height = lh + 1; /* node: LH+1 */
283 r->avl_right = rr; /* rr: LH */
284 r->avl_left = rlr; /* rlr: LH or LH-1 */
285 r->avl_height = lh + 1; /* r: LH+1 */
286 rl->avl_right = r; /* r: LH+1 */
287 rl->avl_left = node; /* node: LH+1 */
288 rl->avl_height = lh + 2;
289 *nodep = rl;
290 }
291 } else {
292 node->avl_height = (lh > rh ? lh : rh) + 1;
293 }
294 }
295}
296
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000297/* Called with local BH disabled and the pool lock held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#define link_to_pool(n) \
299do { \
300 n->avl_height = 1; \
301 n->avl_left = peer_avl_empty; \
302 n->avl_right = peer_avl_empty; \
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000303 smp_wmb(); /* lockless readers can catch us now */ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 **--stackptr = n; \
305 peer_avl_rebalance(stack, stackptr); \
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000306} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000308static void inetpeer_free_rcu(struct rcu_head *head)
309{
310 kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/* May be called with local BH enabled. */
314static void unlink_from_pool(struct inet_peer *p)
315{
316 int do_free;
317
318 do_free = 0;
319
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000320 spin_lock_bh(&peers.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* Check the reference counter. It was artificially incremented by 1
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000322 * in cleanup() function to prevent sudden disappearing. If we can
323 * atomically (because of lockless readers) take this last reference,
324 * it's safe to remove the node and free it later.
325 */
326 if (atomic_cmpxchg(&p->refcnt, 1, 0) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct inet_peer **stack[PEER_MAXDEPTH];
328 struct inet_peer ***stackptr, ***delp;
Eric Dumazet243bbca2007-03-06 20:23:10 -0800329 if (lookup(p->v4daddr, stack) != p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 BUG();
331 delp = stackptr - 1; /* *delp[0] == p */
332 if (p->avl_left == peer_avl_empty) {
333 *delp[0] = p->avl_right;
334 --stackptr;
335 } else {
336 /* look for a node to insert instead of p */
337 struct inet_peer *t;
338 t = lookup_rightempty(p);
Kris Katterjohn09a62662006-01-08 22:24:28 -0800339 BUG_ON(*stackptr[-1] != t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 **--stackptr = t->avl_left;
341 /* t is removed, t->v4daddr > x->v4daddr for any
342 * x in p->avl_left subtree.
343 * Put t in the old place of p. */
344 *delp[0] = t;
345 t->avl_left = p->avl_left;
346 t->avl_right = p->avl_right;
347 t->avl_height = p->avl_height;
Kris Katterjohn09a62662006-01-08 22:24:28 -0800348 BUG_ON(delp[1] != &p->avl_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 delp[1] = &t->avl_left; /* was &p->avl_left */
350 }
351 peer_avl_rebalance(stack, stackptr);
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000352 peers.total--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 do_free = 1;
354 }
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000355 spin_unlock_bh(&peers.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (do_free)
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000358 call_rcu_bh(&p->rcu, inetpeer_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 else
360 /* The node is used again. Decrease the reference counter
361 * back. The loop "cleanup -> unlink_from_unused
362 * -> unlink_from_pool -> putpeer -> link_to_unused
363 * -> cleanup (for the same node)"
364 * doesn't really exist because the entry will have a
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000365 * recent deletion time and will not be cleaned again soon.
366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 inet_putpeer(p);
368}
369
370/* May be called with local BH enabled. */
371static int cleanup_once(unsigned long ttl)
372{
Pavel Emelyanovd71209d2007-11-12 21:27:28 -0800373 struct inet_peer *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* Remove the first entry from the list of unused nodes. */
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000376 spin_lock_bh(&unused_peers.lock);
377 if (!list_empty(&unused_peers.list)) {
Pavel Emelyanovd71209d2007-11-12 21:27:28 -0800378 __u32 delta;
379
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000380 p = list_first_entry(&unused_peers.list, struct inet_peer, unused);
Pavel Emelyanovd71209d2007-11-12 21:27:28 -0800381 delta = (__u32)jiffies - p->dtime;
382
Eric Dumazet4663afe2006-10-12 21:21:06 -0700383 if (delta < ttl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Do not prune fresh entries. */
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000385 spin_unlock_bh(&unused_peers.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -1;
387 }
Pavel Emelyanovd71209d2007-11-12 21:27:28 -0800388
389 list_del_init(&p->unused);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* Grab an extra reference to prevent node disappearing
392 * before unlink_from_pool() call. */
393 atomic_inc(&p->refcnt);
394 }
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000395 spin_unlock_bh(&unused_peers.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (p == NULL)
398 /* It means that the total number of USED entries has
399 * grown over inet_peer_threshold. It shouldn't really
400 * happen because of entry limits in route cache. */
401 return -1;
402
403 unlink_from_pool(p);
404 return 0;
405}
406
407/* Called with or without local BH being disabled. */
Al Viro53576d92006-09-26 22:18:43 -0700408struct inet_peer *inet_getpeer(__be32 daddr, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000410 struct inet_peer *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct inet_peer **stack[PEER_MAXDEPTH], ***stackptr;
412
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000413 /* Look up for the address quickly, lockless.
414 * Because of a concurrent writer, we might not find an existing entry.
415 */
416 rcu_read_lock_bh();
417 p = lookup_rcu_bh(daddr);
418 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000420 if (p) {
421 /* The existing node has been found.
422 * Remove the entry from unused list if it was there.
423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unlink_from_unused(p);
425 return p;
426 }
427
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000428 /* retry an exact lookup, taking the lock before.
429 * At least, nodes should be hot in our cache.
430 */
431 spin_lock_bh(&peers.lock);
Eric Dumazet243bbca2007-03-06 20:23:10 -0800432 p = lookup(daddr, stack);
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000433 if (p != peer_avl_empty) {
434 atomic_inc(&p->refcnt);
435 spin_unlock_bh(&peers.lock);
436 /* Remove the entry from unused list if it was there. */
437 unlink_from_unused(p);
438 return p;
439 }
440 p = create ? kmem_cache_alloc(peer_cachep, GFP_ATOMIC) : NULL;
441 if (p) {
442 p->v4daddr = daddr;
443 atomic_set(&p->refcnt, 1);
444 atomic_set(&p->rid, 0);
445 atomic_set(&p->ip_id_count, secure_ip_id(daddr));
446 p->tcp_ts_stamp = 0;
447 INIT_LIST_HEAD(&p->unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000449
450 /* Link the node. */
451 link_to_pool(p);
452 peers.total++;
453 }
454 spin_unlock_bh(&peers.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000456 if (peers.total >= inet_peer_threshold)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Remove one less-recently-used entry. */
458 cleanup_once(0);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return p;
461}
462
463/* Called with local BH disabled. */
464static void peer_check_expire(unsigned long dummy)
465{
Eric Dumazet4663afe2006-10-12 21:21:06 -0700466 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int ttl;
468
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000469 if (peers.total >= inet_peer_threshold)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ttl = inet_peer_minttl;
471 else
472 ttl = inet_peer_maxttl
473 - (inet_peer_maxttl - inet_peer_minttl) / HZ *
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000474 peers.total / inet_peer_threshold * HZ;
Eric Dumazet4663afe2006-10-12 21:21:06 -0700475 while (!cleanup_once(ttl)) {
476 if (jiffies != now)
477 break;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /* Trigger the timer after inet_peer_gc_mintime .. inet_peer_gc_maxtime
481 * interval depending on the total number of entries (more entries,
482 * less interval). */
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000483 if (peers.total >= inet_peer_threshold)
Dave Johnson1344a412005-08-23 10:10:15 -0700484 peer_periodic_timer.expires = jiffies + inet_peer_gc_mintime;
485 else
486 peer_periodic_timer.expires = jiffies
487 + inet_peer_gc_maxtime
488 - (inet_peer_gc_maxtime - inet_peer_gc_mintime) / HZ *
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000489 peers.total / inet_peer_threshold * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 add_timer(&peer_periodic_timer);
491}
Eric Dumazet4663afe2006-10-12 21:21:06 -0700492
493void inet_putpeer(struct inet_peer *p)
494{
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000495 local_bh_disable();
496
497 if (atomic_dec_and_lock(&p->refcnt, &unused_peers.lock)) {
498 list_add_tail(&p->unused, &unused_peers.list);
Eric Dumazet4663afe2006-10-12 21:21:06 -0700499 p->dtime = (__u32)jiffies;
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000500 spin_unlock(&unused_peers.lock);
Eric Dumazet4663afe2006-10-12 21:21:06 -0700501 }
Eric Dumazetd6cc1d62010-06-14 19:35:21 +0000502
503 local_bh_enable();
Eric Dumazet4663afe2006-10-12 21:21:06 -0700504}