This source file includes following definitions.
- inet_peer_base_init
- inet_initpeers
- lookup
- inetpeer_free_rcu
- inet_peer_gc
- inet_getpeer
- inet_putpeer
- inet_peer_xrlim_allow
- inetpeer_invalidate_tree
1
2
3
4
5
6
7
8
9 #include <linux/cache.h>
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>
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>
21 #include <linux/workqueue.h>
22 #include <net/ip.h>
23 #include <net/inetpeer.h>
24 #include <net/secure_seq.h>
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 static struct kmem_cache *peer_cachep __ro_after_init;
56
57 void inet_peer_base_init(struct inet_peer_base *bp)
58 {
59 bp->rb_root = RB_ROOT;
60 seqlock_init(&bp->lock);
61 bp->total = 0;
62 }
63 EXPORT_SYMBOL_GPL(inet_peer_base_init);
64
65 #define PEER_MAX_GC 32
66
67
68 int inet_peer_threshold __read_mostly = 65536 + 128;
69
70 int inet_peer_minttl __read_mostly = 120 * HZ;
71 int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;
72
73
74 void __init inet_initpeers(void)
75 {
76 struct sysinfo si;
77
78
79 si_meminfo(&si);
80
81
82
83
84 if (si.totalram <= (32768*1024)/PAGE_SIZE)
85 inet_peer_threshold >>= 1;
86 if (si.totalram <= (16384*1024)/PAGE_SIZE)
87 inet_peer_threshold >>= 1;
88 if (si.totalram <= (8192*1024)/PAGE_SIZE)
89 inet_peer_threshold >>= 2;
90
91 peer_cachep = kmem_cache_create("inet_peer_cache",
92 sizeof(struct inet_peer),
93 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
94 NULL);
95 }
96
97
98 static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
99 struct inet_peer_base *base,
100 unsigned int seq,
101 struct inet_peer *gc_stack[],
102 unsigned int *gc_cnt,
103 struct rb_node **parent_p,
104 struct rb_node ***pp_p)
105 {
106 struct rb_node **pp, *parent, *next;
107 struct inet_peer *p;
108
109 pp = &base->rb_root.rb_node;
110 parent = NULL;
111 while (1) {
112 int cmp;
113
114 next = rcu_dereference_raw(*pp);
115 if (!next)
116 break;
117 parent = next;
118 p = rb_entry(parent, struct inet_peer, rb_node);
119 cmp = inetpeer_addr_cmp(daddr, &p->daddr);
120 if (cmp == 0) {
121 if (!refcount_inc_not_zero(&p->refcnt))
122 break;
123 return p;
124 }
125 if (gc_stack) {
126 if (*gc_cnt < PEER_MAX_GC)
127 gc_stack[(*gc_cnt)++] = p;
128 } else if (unlikely(read_seqretry(&base->lock, seq))) {
129 break;
130 }
131 if (cmp == -1)
132 pp = &next->rb_left;
133 else
134 pp = &next->rb_right;
135 }
136 *parent_p = parent;
137 *pp_p = pp;
138 return NULL;
139 }
140
141 static void inetpeer_free_rcu(struct rcu_head *head)
142 {
143 kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
144 }
145
146
147 static void inet_peer_gc(struct inet_peer_base *base,
148 struct inet_peer *gc_stack[],
149 unsigned int gc_cnt)
150 {
151 struct inet_peer *p;
152 __u32 delta, ttl;
153 int i;
154
155 if (base->total >= inet_peer_threshold)
156 ttl = 0;
157 else
158 ttl = inet_peer_maxttl
159 - (inet_peer_maxttl - inet_peer_minttl) / HZ *
160 base->total / inet_peer_threshold * HZ;
161 for (i = 0; i < gc_cnt; i++) {
162 p = gc_stack[i];
163
164
165
166
167 delta = (__u32)jiffies - READ_ONCE(p->dtime);
168
169 if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
170 gc_stack[i] = NULL;
171 }
172 for (i = 0; i < gc_cnt; i++) {
173 p = gc_stack[i];
174 if (p) {
175 rb_erase(&p->rb_node, &base->rb_root);
176 base->total--;
177 call_rcu(&p->rcu, inetpeer_free_rcu);
178 }
179 }
180 }
181
182 struct inet_peer *inet_getpeer(struct inet_peer_base *base,
183 const struct inetpeer_addr *daddr,
184 int create)
185 {
186 struct inet_peer *p, *gc_stack[PEER_MAX_GC];
187 struct rb_node **pp, *parent;
188 unsigned int gc_cnt, seq;
189 int invalidated;
190
191
192
193
194 rcu_read_lock();
195 seq = read_seqbegin(&base->lock);
196 p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
197 invalidated = read_seqretry(&base->lock, seq);
198 rcu_read_unlock();
199
200 if (p)
201 return p;
202
203
204 if (!create && !invalidated)
205 return NULL;
206
207
208
209
210 parent = NULL;
211 write_seqlock_bh(&base->lock);
212
213 gc_cnt = 0;
214 p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
215 if (!p && create) {
216 p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
217 if (p) {
218 p->daddr = *daddr;
219 p->dtime = (__u32)jiffies;
220 refcount_set(&p->refcnt, 2);
221 atomic_set(&p->rid, 0);
222 p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
223 p->rate_tokens = 0;
224 p->n_redirects = 0;
225
226
227
228 p->rate_last = jiffies - 60*HZ;
229
230 rb_link_node(&p->rb_node, parent, pp);
231 rb_insert_color(&p->rb_node, &base->rb_root);
232 base->total++;
233 }
234 }
235 if (gc_cnt)
236 inet_peer_gc(base, gc_stack, gc_cnt);
237 write_sequnlock_bh(&base->lock);
238
239 return p;
240 }
241 EXPORT_SYMBOL_GPL(inet_getpeer);
242
243 void inet_putpeer(struct inet_peer *p)
244 {
245
246
247
248 WRITE_ONCE(p->dtime, (__u32)jiffies);
249
250 if (refcount_dec_and_test(&p->refcnt))
251 call_rcu(&p->rcu, inetpeer_free_rcu);
252 }
253 EXPORT_SYMBOL_GPL(inet_putpeer);
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272 #define XRLIM_BURST_FACTOR 6
273 bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
274 {
275 unsigned long now, token;
276 bool rc = false;
277
278 if (!peer)
279 return true;
280
281 token = peer->rate_tokens;
282 now = jiffies;
283 token += now - peer->rate_last;
284 peer->rate_last = now;
285 if (token > XRLIM_BURST_FACTOR * timeout)
286 token = XRLIM_BURST_FACTOR * timeout;
287 if (token >= timeout) {
288 token -= timeout;
289 rc = true;
290 }
291 peer->rate_tokens = token;
292 return rc;
293 }
294 EXPORT_SYMBOL(inet_peer_xrlim_allow);
295
296 void inetpeer_invalidate_tree(struct inet_peer_base *base)
297 {
298 struct rb_node *p = rb_first(&base->rb_root);
299
300 while (p) {
301 struct inet_peer *peer = rb_entry(p, struct inet_peer, rb_node);
302
303 p = rb_next(p);
304 rb_erase(&peer->rb_node, &base->rb_root);
305 inet_putpeer(peer);
306 cond_resched();
307 }
308
309 base->total = 0;
310 }
311 EXPORT_SYMBOL(inetpeer_invalidate_tree);