Merge branches 'pm-devfreq', 'pm-qos', 'pm-tools' and 'pm-docs'
[linux-2.6-block.git] / net / ipv4 / inetpeer.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
6 * Authors: Andrey V. Savochkin <saw@msu.ru>
7 */
8
08009a76 9#include <linux/cache.h>
1da177e4
LT
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/slab.h>
13#include <linux/interrupt.h>
14#include <linux/spinlock.h>
15#include <linux/random.h>
1da177e4
LT
16#include <linux/timer.h>
17#include <linux/time.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/net.h>
5faa5df1 21#include <linux/workqueue.h>
20380731 22#include <net/ip.h>
1da177e4 23#include <net/inetpeer.h>
6e5714ea 24#include <net/secure_seq.h>
1da177e4
LT
25
26/*
27 * Theory of operations.
28 * We keep one entry for each peer IP address. The nodes contains long-living
29 * information about the peer which doesn't depend on routes.
1da177e4 30 *
1da177e4
LT
31 * Nodes are removed only when reference counter goes to 0.
32 * When it's happened the node may be removed when a sufficient amount of
33 * time has been passed since its last use. The less-recently-used entry can
34 * also be removed if the pool is overloaded i.e. if the total amount of
35 * entries is greater-or-equal than the threshold.
36 *
b145425f 37 * Node pool is organised as an RB tree.
1da177e4
LT
38 * Such an implementation has been chosen not just for fun. It's a way to
39 * prevent easy and efficient DoS attacks by creating hash collisions. A huge
40 * amount of long living nodes in a single hash slot would significantly delay
41 * lookups performed with disabled BHs.
42 *
43 * Serialisation issues.
aa1039e7
ED
44 * 1. Nodes may appear in the tree only with the pool lock held.
45 * 2. Nodes may disappear from the tree only with the pool lock held
1da177e4 46 * AND reference count being 0.
4b9d9be8
ED
47 * 3. Global variable peer_total is modified under the pool lock.
48 * 4. struct inet_peer fields modification:
b145425f 49 * rb_node: pool lock
1da177e4
LT
50 * refcnt: atomically against modifications on other CPU;
51 * usually under some other lock to prevent node disappearing
582a72da 52 * daddr: unchangeable
1da177e4
LT
53 */
54
08009a76 55static struct kmem_cache *peer_cachep __ro_after_init;
1da177e4 56
c3426b47
DM
57void inet_peer_base_init(struct inet_peer_base *bp)
58{
b145425f 59 bp->rb_root = RB_ROOT;
c3426b47
DM
60 seqlock_init(&bp->lock);
61 bp->total = 0;
62}
63EXPORT_SYMBOL_GPL(inet_peer_base_init);
021e9299 64
b145425f 65#define PEER_MAX_GC 32
1da177e4 66
1da177e4 67/* Exported for sysctl_net_ipv4. */
8bd2a055 68int inet_peer_threshold __read_mostly; /* start to throw entries more
1da177e4 69 * aggressively at this stage */
243bbcaa
ED
70int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
71int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */
1da177e4 72
1da177e4
LT
73/* Called from ip_output.c:ip_init */
74void __init inet_initpeers(void)
75{
8bd2a055 76 u64 nr_entries;
1da177e4 77
8bd2a055
YD
78 /* 1% of physical memory */
79 nr_entries = div64_ul((u64)totalram_pages() << PAGE_SHIFT,
80 100 * L1_CACHE_ALIGN(sizeof(struct inet_peer)));
81
82 inet_peer_threshold = clamp_val(nr_entries, 4096, 65536 + 128);
1da177e4
LT
83
84 peer_cachep = kmem_cache_create("inet_peer_cache",
85 sizeof(struct inet_peer),
317fe0e6 86 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
20c2df83 87 NULL);
1da177e4
LT
88}
89
b145425f
ED
90/* Called with rcu_read_lock() or base->lock held */
91static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
92 struct inet_peer_base *base,
93 unsigned int seq,
94 struct inet_peer *gc_stack[],
95 unsigned int *gc_cnt,
96 struct rb_node **parent_p,
97 struct rb_node ***pp_p)
aa1039e7 98{
4cc5b44b 99 struct rb_node **pp, *parent, *next;
b145425f
ED
100 struct inet_peer *p;
101
102 pp = &base->rb_root.rb_node;
103 parent = NULL;
4cc5b44b 104 while (1) {
b145425f 105 int cmp;
aa1039e7 106
4cc5b44b
ED
107 next = rcu_dereference_raw(*pp);
108 if (!next)
109 break;
110 parent = next;
b145425f
ED
111 p = rb_entry(parent, struct inet_peer, rb_node);
112 cmp = inetpeer_addr_cmp(daddr, &p->daddr);
02663045 113 if (cmp == 0) {
b145425f
ED
114 if (!refcount_inc_not_zero(&p->refcnt))
115 break;
116 return p;
117 }
118 if (gc_stack) {
119 if (*gc_cnt < PEER_MAX_GC)
120 gc_stack[(*gc_cnt)++] = p;
121 } else if (unlikely(read_seqretry(&base->lock, seq))) {
122 break;
aa1039e7 123 }
02663045 124 if (cmp == -1)
35f493b8 125 pp = &next->rb_left;
aa1039e7 126 else
35f493b8 127 pp = &next->rb_right;
aa1039e7 128 }
b145425f
ED
129 *parent_p = parent;
130 *pp_p = pp;
aa1039e7
ED
131 return NULL;
132}
133
aa1039e7
ED
134static void inetpeer_free_rcu(struct rcu_head *head)
135{
136 kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
137}
138
4b9d9be8 139/* perform garbage collect on all items stacked during a lookup */
b145425f
ED
140static void inet_peer_gc(struct inet_peer_base *base,
141 struct inet_peer *gc_stack[],
142 unsigned int gc_cnt)
98158f5a 143{
3d32edf1 144 int peer_threshold, peer_maxttl, peer_minttl;
b145425f 145 struct inet_peer *p;
4b9d9be8 146 __u32 delta, ttl;
b145425f 147 int i;
d71209de 148
3d32edf1
KI
149 peer_threshold = READ_ONCE(inet_peer_threshold);
150 peer_maxttl = READ_ONCE(inet_peer_maxttl);
151 peer_minttl = READ_ONCE(inet_peer_minttl);
152
153 if (base->total >= peer_threshold)
4b9d9be8
ED
154 ttl = 0; /* be aggressive */
155 else
3d32edf1
KI
156 ttl = peer_maxttl - (peer_maxttl - peer_minttl) / HZ *
157 base->total / peer_threshold * HZ;
b145425f
ED
158 for (i = 0; i < gc_cnt; i++) {
159 p = gc_stack[i];
71685eb4
ED
160
161 /* The READ_ONCE() pairs with the WRITE_ONCE()
162 * in inet_putpeer()
163 */
164 delta = (__u32)jiffies - READ_ONCE(p->dtime);
165
b145425f
ED
166 if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
167 gc_stack[i] = NULL;
1da177e4 168 }
b145425f
ED
169 for (i = 0; i < gc_cnt; i++) {
170 p = gc_stack[i];
171 if (p) {
172 rb_erase(&p->rb_node, &base->rb_root);
173 base->total--;
174 call_rcu(&p->rcu, inetpeer_free_rcu);
175 }
4b9d9be8 176 }
1da177e4
LT
177}
178
c0efc887 179struct inet_peer *inet_getpeer(struct inet_peer_base *base,
c8a627ed
G
180 const struct inetpeer_addr *daddr,
181 int create)
1da177e4 182{
b145425f
ED
183 struct inet_peer *p, *gc_stack[PEER_MAX_GC];
184 struct rb_node **pp, *parent;
185 unsigned int gc_cnt, seq;
186 int invalidated;
1da177e4 187
4b9d9be8 188 /* Attempt a lockless lookup first.
aa1039e7
ED
189 * Because of a concurrent writer, we might not find an existing entry.
190 */
7b46ac4e 191 rcu_read_lock();
b145425f
ED
192 seq = read_seqbegin(&base->lock);
193 p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
194 invalidated = read_seqretry(&base->lock, seq);
7b46ac4e 195 rcu_read_unlock();
aa1039e7 196
4b9d9be8 197 if (p)
aa1039e7 198 return p;
1da177e4 199
65e8354e
ED
200 /* If no writer did a change during our lookup, we can return early. */
201 if (!create && !invalidated)
202 return NULL;
203
aa1039e7
ED
204 /* retry an exact lookup, taking the lock before.
205 * At least, nodes should be hot in our cache.
206 */
b145425f 207 parent = NULL;
65e8354e 208 write_seqlock_bh(&base->lock);
aa1039e7 209
b145425f
ED
210 gc_cnt = 0;
211 p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
212 if (!p && create) {
213 p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
214 if (p) {
215 p->daddr = *daddr;
b6a37e5e 216 p->dtime = (__u32)jiffies;
b145425f
ED
217 refcount_set(&p->refcnt, 2);
218 atomic_set(&p->rid, 0);
219 p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
220 p->rate_tokens = 0;
c09551c6 221 p->n_redirects = 0;
b145425f
ED
222 /* 60*HZ is arbitrary, but chosen enough high so that the first
223 * calculation of tokens is at its maximum.
224 */
225 p->rate_last = jiffies - 60*HZ;
226
227 rb_link_node(&p->rb_node, parent, pp);
228 rb_insert_color(&p->rb_node, &base->rb_root);
229 base->total++;
230 }
aa1039e7 231 }
b145425f
ED
232 if (gc_cnt)
233 inet_peer_gc(base, gc_stack, gc_cnt);
65e8354e 234 write_sequnlock_bh(&base->lock);
1da177e4 235
1da177e4
LT
236 return p;
237}
b3419363 238EXPORT_SYMBOL_GPL(inet_getpeer);
98158f5a 239
4663afe2
ED
240void inet_putpeer(struct inet_peer *p)
241{
71685eb4
ED
242 /* The WRITE_ONCE() pairs with itself (we run lockless)
243 * and the READ_ONCE() in inet_peer_gc()
244 */
245 WRITE_ONCE(p->dtime, (__u32)jiffies);
b145425f
ED
246
247 if (refcount_dec_and_test(&p->refcnt))
248 call_rcu(&p->rcu, inetpeer_free_rcu);
4663afe2 249}
b3419363 250EXPORT_SYMBOL_GPL(inet_putpeer);
92d86829
DM
251
252/*
253 * Check transmit rate limitation for given message.
254 * The rate information is held in the inet_peer entries now.
255 * This function is generic and could be used for other purposes
256 * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
257 *
258 * Note that the same inet_peer fields are modified by functions in
259 * route.c too, but these work for packet destinations while xrlim_allow
260 * works for icmp destinations. This means the rate limiting information
261 * for one "ip object" is shared - and these ICMPs are twice limited:
262 * by source and by destination.
263 *
264 * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
265 * SHOULD allow setting of rate limits
266 *
267 * Shared between ICMPv4 and ICMPv6.
268 */
269#define XRLIM_BURST_FACTOR 6
270bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
271{
272 unsigned long now, token;
273 bool rc = false;
274
275 if (!peer)
276 return true;
277
278 token = peer->rate_tokens;
279 now = jiffies;
280 token += now - peer->rate_last;
281 peer->rate_last = now;
282 if (token > XRLIM_BURST_FACTOR * timeout)
283 token = XRLIM_BURST_FACTOR * timeout;
284 if (token >= timeout) {
285 token -= timeout;
286 rc = true;
287 }
288 peer->rate_tokens = token;
289 return rc;
290}
291EXPORT_SYMBOL(inet_peer_xrlim_allow);
5faa5df1 292
56a6b248 293void inetpeer_invalidate_tree(struct inet_peer_base *base)
5faa5df1 294{
8f1975e3 295 struct rb_node *p = rb_first(&base->rb_root);
5faa5df1 296
8f1975e3
ED
297 while (p) {
298 struct inet_peer *peer = rb_entry(p, struct inet_peer, rb_node);
299
300 p = rb_next(p);
301 rb_erase(&peer->rb_node, &base->rb_root);
302 inet_putpeer(peer);
b145425f 303 cond_resched();
5faa5df1
SK
304 }
305
b145425f 306 base->total = 0;
5faa5df1
SK
307}
308EXPORT_SYMBOL(inetpeer_invalidate_tree);