1
2
3
4
5
6
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>
15#include <linux/timer.h>
16#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/net.h>
20#include <net/ip.h>
21#include <net/inetpeer.h>
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
71DEFINE_SPINLOCK(inet_peer_idlock);
72
73static struct kmem_cache *peer_cachep __read_mostly;
74
75#define node_height(x) x->avl_height
76static struct inet_peer peer_fake_node = {
77 .avl_left = &peer_fake_node,
78 .avl_right = &peer_fake_node,
79 .avl_height = 0
80};
81#define peer_avl_empty (&peer_fake_node)
82static struct inet_peer *peer_root = peer_avl_empty;
83static DEFINE_RWLOCK(peer_pool_lock);
84#define PEER_MAXDEPTH 40
85
86static int peer_total;
87
88int inet_peer_threshold __read_mostly = 65536 + 128;
89
90int inet_peer_minttl __read_mostly = 120 * HZ;
91int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;
92int inet_peer_gc_mintime __read_mostly = 10 * HZ;
93int inet_peer_gc_maxtime __read_mostly = 120 * HZ;
94
95static LIST_HEAD(unused_peers);
96static DEFINE_SPINLOCK(inet_peer_unused_lock);
97
98static void peer_check_expire(unsigned long dummy);
99static DEFINE_TIMER(peer_periodic_timer, peer_check_expire, 0, 0);
100
101
102
103void __init inet_initpeers(void)
104{
105 struct sysinfo si;
106
107
108 si_meminfo(&si);
109
110
111
112
113 if (si.totalram <= (32768*1024)/PAGE_SIZE)
114 inet_peer_threshold >>= 1;
115 if (si.totalram <= (16384*1024)/PAGE_SIZE)
116 inet_peer_threshold >>= 1;
117 if (si.totalram <= (8192*1024)/PAGE_SIZE)
118 inet_peer_threshold >>= 2;
119
120 peer_cachep = kmem_cache_create("inet_peer_cache",
121 sizeof(struct inet_peer),
122 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
123 NULL);
124
125
126
127
128 peer_periodic_timer.expires = jiffies
129 + net_random() % inet_peer_gc_maxtime
130 + inet_peer_gc_maxtime;
131 add_timer(&peer_periodic_timer);
132}
133
134
135static void unlink_from_unused(struct inet_peer *p)
136{
137 spin_lock_bh(&inet_peer_unused_lock);
138 list_del_init(&p->unused);
139 spin_unlock_bh(&inet_peer_unused_lock);
140}
141
142
143
144
145
146
147#define lookup(_daddr, _stack) \
148({ \
149 struct inet_peer *u, **v; \
150 if (_stack != NULL) { \
151 stackptr = _stack; \
152 *stackptr++ = &peer_root; \
153 } \
154 for (u = peer_root; u != peer_avl_empty; ) { \
155 if (_daddr == u->v4daddr) \
156 break; \
157 if ((__force __u32)_daddr < (__force __u32)u->v4daddr) \
158 v = &u->avl_left; \
159 else \
160 v = &u->avl_right; \
161 if (_stack != NULL) \
162 *stackptr++ = v; \
163 u = *v; \
164 } \
165 u; \
166})
167
168
169#define lookup_rightempty(start) \
170({ \
171 struct inet_peer *u, **v; \
172 *stackptr++ = &start->avl_left; \
173 v = &start->avl_left; \
174 for (u = *v; u->avl_right != peer_avl_empty; ) { \
175 v = &u->avl_right; \
176 *stackptr++ = v; \
177 u = *v; \
178 } \
179 u; \
180})
181
182
183
184
185static void peer_avl_rebalance(struct inet_peer **stack[],
186 struct inet_peer ***stackend)
187{
188 struct inet_peer **nodep, *node, *l, *r;
189 int lh, rh;
190
191 while (stackend > stack) {
192 nodep = *--stackend;
193 node = *nodep;
194 l = node->avl_left;
195 r = node->avl_right;
196 lh = node_height(l);
197 rh = node_height(r);
198 if (lh > rh + 1) {
199 struct inet_peer *ll, *lr, *lrl, *lrr;
200 int lrh;
201 ll = l->avl_left;
202 lr = l->avl_right;
203 lrh = node_height(lr);
204 if (lrh <= node_height(ll)) {
205 node->avl_left = lr;
206 node->avl_right = r;
207 node->avl_height = lrh + 1;
208 l->avl_left = ll;
209 l->avl_right = node;
210 l->avl_height = node->avl_height + 1;
211 *nodep = l;
212 } else {
213 lrl = lr->avl_left;
214 lrr = lr->avl_right;
215 node->avl_left = lrr;
216 node->avl_right = r;
217 node->avl_height = rh + 1;
218 l->avl_left = ll;
219 l->avl_right = lrl;
220 l->avl_height = rh + 1;
221 lr->avl_left = l;
222 lr->avl_right = node;
223 lr->avl_height = rh + 2;
224 *nodep = lr;
225 }
226 } else if (rh > lh + 1) {
227 struct inet_peer *rr, *rl, *rlr, *rll;
228 int rlh;
229 rr = r->avl_right;
230 rl = r->avl_left;
231 rlh = node_height(rl);
232 if (rlh <= node_height(rr)) {
233 node->avl_right = rl;
234 node->avl_left = l;
235 node->avl_height = rlh + 1;
236 r->avl_right = rr;
237 r->avl_left = node;
238 r->avl_height = node->avl_height + 1;
239 *nodep = r;
240 } else {
241 rlr = rl->avl_right;
242 rll = rl->avl_left;
243 node->avl_right = rll;
244 node->avl_left = l;
245 node->avl_height = lh + 1;
246 r->avl_right = rr;
247 r->avl_left = rlr;
248 r->avl_height = lh + 1;
249 rl->avl_right = r;
250 rl->avl_left = node;
251 rl->avl_height = lh + 2;
252 *nodep = rl;
253 }
254 } else {
255 node->avl_height = (lh > rh ? lh : rh) + 1;
256 }
257 }
258}
259
260
261#define link_to_pool(n) \
262do { \
263 n->avl_height = 1; \
264 n->avl_left = peer_avl_empty; \
265 n->avl_right = peer_avl_empty; \
266 **--stackptr = n; \
267 peer_avl_rebalance(stack, stackptr); \
268} while(0)
269
270
271static void unlink_from_pool(struct inet_peer *p)
272{
273 int do_free;
274
275 do_free = 0;
276
277 write_lock_bh(&peer_pool_lock);
278
279
280
281
282
283 if (atomic_read(&p->refcnt) == 1) {
284 struct inet_peer **stack[PEER_MAXDEPTH];
285 struct inet_peer ***stackptr, ***delp;
286 if (lookup(p->v4daddr, stack) != p)
287 BUG();
288 delp = stackptr - 1;
289 if (p->avl_left == peer_avl_empty) {
290 *delp[0] = p->avl_right;
291 --stackptr;
292 } else {
293
294 struct inet_peer *t;
295 t = lookup_rightempty(p);
296 BUG_ON(*stackptr[-1] != t);
297 **--stackptr = t->avl_left;
298
299
300
301 *delp[0] = t;
302 t->avl_left = p->avl_left;
303 t->avl_right = p->avl_right;
304 t->avl_height = p->avl_height;
305 BUG_ON(delp[1] != &p->avl_left);
306 delp[1] = &t->avl_left;
307 }
308 peer_avl_rebalance(stack, stackptr);
309 peer_total--;
310 do_free = 1;
311 }
312 write_unlock_bh(&peer_pool_lock);
313
314 if (do_free)
315 kmem_cache_free(peer_cachep, p);
316 else
317
318
319
320
321
322
323 inet_putpeer(p);
324}
325
326
327static int cleanup_once(unsigned long ttl)
328{
329 struct inet_peer *p = NULL;
330
331
332 spin_lock_bh(&inet_peer_unused_lock);
333 if (!list_empty(&unused_peers)) {
334 __u32 delta;
335
336 p = list_first_entry(&unused_peers, struct inet_peer, unused);
337 delta = (__u32)jiffies - p->dtime;
338
339 if (delta < ttl) {
340
341 spin_unlock_bh(&inet_peer_unused_lock);
342 return -1;
343 }
344
345 list_del_init(&p->unused);
346
347
348
349 atomic_inc(&p->refcnt);
350 }
351 spin_unlock_bh(&inet_peer_unused_lock);
352
353 if (p == NULL)
354
355
356
357 return -1;
358
359 unlink_from_pool(p);
360 return 0;
361}
362
363
364struct inet_peer *inet_getpeer(__be32 daddr, int create)
365{
366 struct inet_peer *p, *n;
367 struct inet_peer **stack[PEER_MAXDEPTH], ***stackptr;
368
369
370 read_lock_bh(&peer_pool_lock);
371 p = lookup(daddr, NULL);
372 if (p != peer_avl_empty)
373 atomic_inc(&p->refcnt);
374 read_unlock_bh(&peer_pool_lock);
375
376 if (p != peer_avl_empty) {
377
378
379 unlink_from_unused(p);
380 return p;
381 }
382
383 if (!create)
384 return NULL;
385
386
387 n = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
388 if (n == NULL)
389 return NULL;
390 n->v4daddr = daddr;
391 atomic_set(&n->refcnt, 1);
392 atomic_set(&n->rid, 0);
393 n->ip_id_count = secure_ip_id(daddr);
394 n->tcp_ts_stamp = 0;
395
396 write_lock_bh(&peer_pool_lock);
397
398 p = lookup(daddr, stack);
399 if (p != peer_avl_empty)
400 goto out_free;
401
402
403 link_to_pool(n);
404 INIT_LIST_HEAD(&n->unused);
405 peer_total++;
406 write_unlock_bh(&peer_pool_lock);
407
408 if (peer_total >= inet_peer_threshold)
409
410 cleanup_once(0);
411
412 return n;
413
414out_free:
415
416 atomic_inc(&p->refcnt);
417 write_unlock_bh(&peer_pool_lock);
418
419 unlink_from_unused(p);
420
421 kmem_cache_free(peer_cachep, n);
422 return p;
423}
424
425
426static void peer_check_expire(unsigned long dummy)
427{
428 unsigned long now = jiffies;
429 int ttl;
430
431 if (peer_total >= inet_peer_threshold)
432 ttl = inet_peer_minttl;
433 else
434 ttl = inet_peer_maxttl
435 - (inet_peer_maxttl - inet_peer_minttl) / HZ *
436 peer_total / inet_peer_threshold * HZ;
437 while (!cleanup_once(ttl)) {
438 if (jiffies != now)
439 break;
440 }
441
442
443
444
445 if (peer_total >= inet_peer_threshold)
446 peer_periodic_timer.expires = jiffies + inet_peer_gc_mintime;
447 else
448 peer_periodic_timer.expires = jiffies
449 + inet_peer_gc_maxtime
450 - (inet_peer_gc_maxtime - inet_peer_gc_mintime) / HZ *
451 peer_total / inet_peer_threshold * HZ;
452 add_timer(&peer_periodic_timer);
453}
454
455void inet_putpeer(struct inet_peer *p)
456{
457 spin_lock_bh(&inet_peer_unused_lock);
458 if (atomic_dec_and_test(&p->refcnt)) {
459 list_add_tail(&p->unused, &unused_peers);
460 p->dtime = (__u32)jiffies;
461 }
462 spin_unlock_bh(&inet_peer_unused_lock);
463}
464