blob: 1d27ca6b421d9d25a39272cc4ee3cece8c2866d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* flow.c: Generic flow cache.
2 *
3 * Copyright (C) 2003 Alexey N. Kuznetsov (kuznet@ms2.inr.ac.ru)
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/list.h>
10#include <linux/jhash.h>
11#include <linux/interrupt.h>
12#include <linux/mm.h>
13#include <linux/random.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/smp.h>
17#include <linux/completion.h>
18#include <linux/percpu.h>
19#include <linux/bitops.h>
20#include <linux/notifier.h>
21#include <linux/cpu.h>
22#include <linux/cpumask.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <net/flow.h>
25#include <asm/atomic.h>
Trent Jaegerdf718372005-12-13 23:12:27 -080026#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28struct flow_cache_entry {
29 struct flow_cache_entry *next;
30 u16 family;
31 u8 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 u32 genid;
Eric Dumazetdd5a1842008-02-07 23:30:42 -080033 struct flowi key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 void *object;
35 atomic_t *object_ref;
36};
37
Timo Teräsd7997fe2010-03-31 00:17:06 +000038struct flow_cache_percpu {
39 struct flow_cache_entry ** hash_table;
40 int hash_count;
41 u32 hash_rnd;
42 int hash_rnd_recalc;
43 struct tasklet_struct flush_tasklet;
Eric Dumazet5f58a5c2008-02-07 18:03:18 -080044};
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46struct flow_flush_info {
Timo Teräsd7997fe2010-03-31 00:17:06 +000047 struct flow_cache * cache;
48 atomic_t cpuleft;
49 struct completion completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Timo Teräsd7997fe2010-03-31 00:17:06 +000052struct flow_cache {
53 u32 hash_shift;
54 unsigned long order;
55 struct flow_cache_percpu * percpu;
56 struct notifier_block hotcpu_notifier;
57 int low_watermark;
58 int high_watermark;
59 struct timer_list rnd_timer;
60};
61
62atomic_t flow_cache_genid = ATOMIC_INIT(0);
63static struct flow_cache flow_cache_global;
64static struct kmem_cache *flow_cachep;
65
66#define flow_cache_hash_size(cache) (1 << (cache)->hash_shift)
67#define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static void flow_cache_new_hashrnd(unsigned long arg)
70{
Timo Teräsd7997fe2010-03-31 00:17:06 +000071 struct flow_cache *fc = (void *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int i;
73
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070074 for_each_possible_cpu(i)
Timo Teräsd7997fe2010-03-31 00:17:06 +000075 per_cpu_ptr(fc->percpu, i)->hash_rnd_recalc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Timo Teräsd7997fe2010-03-31 00:17:06 +000077 fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
78 add_timer(&fc->rnd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Timo Teräsd7997fe2010-03-31 00:17:06 +000081static void flow_entry_kill(struct flow_cache *fc,
82 struct flow_cache_percpu *fcp,
83 struct flow_cache_entry *fle)
James Morris134b0fc2006-10-05 15:42:27 -050084{
85 if (fle->object)
86 atomic_dec(fle->object_ref);
87 kmem_cache_free(flow_cachep, fle);
Timo Teräsd7997fe2010-03-31 00:17:06 +000088 fcp->hash_count--;
James Morris134b0fc2006-10-05 15:42:27 -050089}
90
Timo Teräsd7997fe2010-03-31 00:17:06 +000091static void __flow_cache_shrink(struct flow_cache *fc,
92 struct flow_cache_percpu *fcp,
93 int shrink_to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 struct flow_cache_entry *fle, **flp;
96 int i;
97
Timo Teräsd7997fe2010-03-31 00:17:06 +000098 for (i = 0; i < flow_cache_hash_size(fc); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int k = 0;
100
Timo Teräsd7997fe2010-03-31 00:17:06 +0000101 flp = &fcp->hash_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 while ((fle = *flp) != NULL && k < shrink_to) {
103 k++;
104 flp = &fle->next;
105 }
106 while ((fle = *flp) != NULL) {
107 *flp = fle->next;
Timo Teräsd7997fe2010-03-31 00:17:06 +0000108 flow_entry_kill(fc, fcp, fle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110 }
111}
112
Timo Teräsd7997fe2010-03-31 00:17:06 +0000113static void flow_cache_shrink(struct flow_cache *fc,
114 struct flow_cache_percpu *fcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000116 int shrink_to = fc->low_watermark / flow_cache_hash_size(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Timo Teräsd7997fe2010-03-31 00:17:06 +0000118 __flow_cache_shrink(fc, fcp, shrink_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Timo Teräsd7997fe2010-03-31 00:17:06 +0000121static void flow_new_hash_rnd(struct flow_cache *fc,
122 struct flow_cache_percpu *fcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000124 get_random_bytes(&fcp->hash_rnd, sizeof(u32));
125 fcp->hash_rnd_recalc = 0;
126 __flow_cache_shrink(fc, fcp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Timo Teräsd7997fe2010-03-31 00:17:06 +0000129static u32 flow_hash_code(struct flow_cache *fc,
130 struct flow_cache_percpu *fcp,
131 struct flowi *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 u32 *k = (u32 *) key;
134
Timo Teräsd7997fe2010-03-31 00:17:06 +0000135 return (jhash2(k, (sizeof(*key) / sizeof(u32)), fcp->hash_rnd)
136 & (flow_cache_hash_size(fc) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139#if (BITS_PER_LONG == 64)
140typedef u64 flow_compare_t;
141#else
142typedef u32 flow_compare_t;
143#endif
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/* I hear what you're saying, use memcmp. But memcmp cannot make
146 * important assumptions that we can here, such as alignment and
147 * constant size.
148 */
149static int flow_key_compare(struct flowi *key1, struct flowi *key2)
150{
151 flow_compare_t *k1, *k1_lim, *k2;
152 const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t);
153
Pavel Emelyanovf0fe91d2007-10-23 21:15:21 -0700154 BUILD_BUG_ON(sizeof(struct flowi) % sizeof(flow_compare_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 k1 = (flow_compare_t *) key1;
157 k1_lim = k1 + n_elem;
158
159 k2 = (flow_compare_t *) key2;
160
161 do {
162 if (*k1++ != *k2++)
163 return 1;
164 } while (k1 < k1_lim);
165
166 return 0;
167}
168
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800169void *flow_cache_lookup(struct net *net, struct flowi *key, u16 family, u8 dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 flow_resolve_t resolver)
171{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000172 struct flow_cache *fc = &flow_cache_global;
173 struct flow_cache_percpu *fcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct flow_cache_entry *fle, **head;
175 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 local_bh_disable();
Timo Teräsd7997fe2010-03-31 00:17:06 +0000178 fcp = per_cpu_ptr(fc->percpu, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 fle = NULL;
181 /* Packet really early in init? Making flow_cache_init a
182 * pre-smp initcall would solve this. --RR */
Timo Teräsd7997fe2010-03-31 00:17:06 +0000183 if (!fcp->hash_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto nocache;
185
Timo Teräsd7997fe2010-03-31 00:17:06 +0000186 if (fcp->hash_rnd_recalc)
187 flow_new_hash_rnd(fc, fcp);
188 hash = flow_hash_code(fc, fcp, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Timo Teräsd7997fe2010-03-31 00:17:06 +0000190 head = &fcp->hash_table[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 for (fle = *head; fle; fle = fle->next) {
192 if (fle->family == family &&
193 fle->dir == dir &&
194 flow_key_compare(key, &fle->key) == 0) {
195 if (fle->genid == atomic_read(&flow_cache_genid)) {
196 void *ret = fle->object;
197
198 if (ret)
199 atomic_inc(fle->object_ref);
200 local_bh_enable();
201
202 return ret;
203 }
204 break;
205 }
206 }
207
208 if (!fle) {
Timo Teräsd7997fe2010-03-31 00:17:06 +0000209 if (fcp->hash_count > fc->high_watermark)
210 flow_cache_shrink(fc, fcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800212 fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (fle) {
214 fle->next = *head;
215 *head = fle;
216 fle->family = family;
217 fle->dir = dir;
218 memcpy(&fle->key, key, sizeof(*key));
219 fle->object = NULL;
Timo Teräsd7997fe2010-03-31 00:17:06 +0000220 fcp->hash_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 }
223
224nocache:
225 {
James Morris134b0fc2006-10-05 15:42:27 -0500226 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 void *obj;
228 atomic_t *obj_ref;
229
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800230 err = resolver(net, key, family, dir, &obj, &obj_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Herbert Xue0e8f1c2007-01-10 22:06:32 -0800232 if (fle && !err) {
233 fle->genid = atomic_read(&flow_cache_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Herbert Xue0e8f1c2007-01-10 22:06:32 -0800235 if (fle->object)
236 atomic_dec(fle->object_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Herbert Xue0e8f1c2007-01-10 22:06:32 -0800238 fle->object = obj;
239 fle->object_ref = obj_ref;
240 if (obj)
241 atomic_inc(fle->object_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 local_bh_enable();
244
James Morris134b0fc2006-10-05 15:42:27 -0500245 if (err)
246 obj = ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return obj;
248 }
249}
250
251static void flow_cache_flush_tasklet(unsigned long data)
252{
253 struct flow_flush_info *info = (void *)data;
Timo Teräsd7997fe2010-03-31 00:17:06 +0000254 struct flow_cache *fc = info->cache;
255 struct flow_cache_percpu *fcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Timo Teräsd7997fe2010-03-31 00:17:06 +0000258 fcp = per_cpu_ptr(fc->percpu, smp_processor_id());
259 for (i = 0; i < flow_cache_hash_size(fc); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct flow_cache_entry *fle;
261
Timo Teräsd7997fe2010-03-31 00:17:06 +0000262 fle = fcp->hash_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 for (; fle; fle = fle->next) {
264 unsigned genid = atomic_read(&flow_cache_genid);
265
266 if (!fle->object || fle->genid == genid)
267 continue;
268
269 fle->object = NULL;
270 atomic_dec(fle->object_ref);
271 }
272 }
273
274 if (atomic_dec_and_test(&info->cpuleft))
275 complete(&info->completion);
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static void flow_cache_flush_per_cpu(void *data)
279{
280 struct flow_flush_info *info = data;
281 int cpu;
282 struct tasklet_struct *tasklet;
283
284 cpu = smp_processor_id();
Timo Teräsd7997fe2010-03-31 00:17:06 +0000285 tasklet = &per_cpu_ptr(info->cache->percpu, cpu)->flush_tasklet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 tasklet->data = (unsigned long)info;
287 tasklet_schedule(tasklet);
288}
289
290void flow_cache_flush(void)
291{
292 struct flow_flush_info info;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800293 static DEFINE_MUTEX(flow_flush_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* Don't want cpus going down or up during this. */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100296 get_online_cpus();
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800297 mutex_lock(&flow_flush_sem);
Timo Teräsd7997fe2010-03-31 00:17:06 +0000298 info.cache = &flow_cache_global;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 atomic_set(&info.cpuleft, num_online_cpus());
300 init_completion(&info.completion);
301
302 local_bh_disable();
Jens Axboe8691e5a2008-06-06 11:18:06 +0200303 smp_call_function(flow_cache_flush_per_cpu, &info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 flow_cache_flush_tasklet((unsigned long)&info);
305 local_bh_enable();
306
307 wait_for_completion(&info.completion);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800308 mutex_unlock(&flow_flush_sem);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100309 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Timo Teräsd7997fe2010-03-31 00:17:06 +0000312static void __init flow_cache_cpu_prepare(struct flow_cache *fc,
313 struct flow_cache_percpu *fcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000315 fcp->hash_table = (struct flow_cache_entry **)
316 __get_free_pages(GFP_KERNEL|__GFP_ZERO, fc->order);
317 if (!fcp->hash_table)
318 panic("NET: failed to allocate flow cache order %lu\n", fc->order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Timo Teräsd7997fe2010-03-31 00:17:06 +0000320 fcp->hash_rnd_recalc = 1;
321 fcp->hash_count = 0;
322 tasklet_init(&fcp->flush_tasklet, flow_cache_flush_tasklet, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static int flow_cache_cpu(struct notifier_block *nfb,
326 unsigned long action,
327 void *hcpu)
328{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000329 struct flow_cache *fc = container_of(nfb, struct flow_cache, hotcpu_notifier);
330 int cpu = (unsigned long) hcpu;
331 struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);
332
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700333 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
Timo Teräsd7997fe2010-03-31 00:17:06 +0000334 __flow_cache_shrink(fc, fcp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return NOTIFY_OK;
336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Timo Teräsd7997fe2010-03-31 00:17:06 +0000338static int flow_cache_init(struct flow_cache *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000340 unsigned long order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int i;
342
Timo Teräsd7997fe2010-03-31 00:17:06 +0000343 fc->hash_shift = 10;
344 fc->low_watermark = 2 * flow_cache_hash_size(fc);
345 fc->high_watermark = 4 * flow_cache_hash_size(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Timo Teräsd7997fe2010-03-31 00:17:06 +0000347 for (order = 0;
348 (PAGE_SIZE << order) <
349 (sizeof(struct flow_cache_entry *)*flow_cache_hash_size(fc));
350 order++)
351 /* NOTHING */;
352 fc->order = order;
353 fc->percpu = alloc_percpu(struct flow_cache_percpu);
354
355 setup_timer(&fc->rnd_timer, flow_cache_new_hashrnd,
356 (unsigned long) fc);
357 fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
358 add_timer(&fc->rnd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700360 for_each_possible_cpu(i)
Timo Teräsd7997fe2010-03-31 00:17:06 +0000361 flow_cache_cpu_prepare(fc, per_cpu_ptr(fc->percpu, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Timo Teräsd7997fe2010-03-31 00:17:06 +0000363 fc->hotcpu_notifier = (struct notifier_block){
364 .notifier_call = flow_cache_cpu,
365 };
366 register_hotcpu_notifier(&fc->hotcpu_notifier);
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
Timo Teräsd7997fe2010-03-31 00:17:06 +0000371static int __init flow_cache_init_global(void)
372{
373 flow_cachep = kmem_cache_create("flow_cache",
374 sizeof(struct flow_cache_entry),
375 0, SLAB_PANIC, NULL);
376
377 return flow_cache_init(&flow_cache_global);
378}
379
380module_init(flow_cache_init_global);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382EXPORT_SYMBOL(flow_cache_genid);
383EXPORT_SYMBOL(flow_cache_lookup);