blob: 521df52a77d2be17518ba317d551755afac6fa4b [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 {
Timo Teräsfe1a5f02010-04-07 00:30:04 +000029 struct flow_cache_entry *next;
30 u16 family;
31 u8 dir;
32 u32 genid;
33 struct flowi key;
34 struct flow_cache_object *object;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Timo Teräsd7997fe2010-03-31 00:17:06 +000037struct flow_cache_percpu {
Timo Teräsfe1a5f02010-04-07 00:30:04 +000038 struct flow_cache_entry **hash_table;
Timo Teräsd7997fe2010-03-31 00:17:06 +000039 int hash_count;
40 u32 hash_rnd;
41 int hash_rnd_recalc;
42 struct tasklet_struct flush_tasklet;
Eric Dumazet5f58a5c2008-02-07 18:03:18 -080043};
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45struct flow_flush_info {
Timo Teräsfe1a5f02010-04-07 00:30:04 +000046 struct flow_cache *cache;
Timo Teräsd7997fe2010-03-31 00:17:06 +000047 atomic_t cpuleft;
48 struct completion completion;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Timo Teräsd7997fe2010-03-31 00:17:06 +000051struct flow_cache {
52 u32 hash_shift;
53 unsigned long order;
Timo Teräsfe1a5f02010-04-07 00:30:04 +000054 struct flow_cache_percpu *percpu;
Timo Teräsd7997fe2010-03-31 00:17:06 +000055 struct notifier_block hotcpu_notifier;
56 int low_watermark;
57 int high_watermark;
58 struct timer_list rnd_timer;
59};
60
61atomic_t flow_cache_genid = ATOMIC_INIT(0);
62static struct flow_cache flow_cache_global;
63static struct kmem_cache *flow_cachep;
64
65#define flow_cache_hash_size(cache) (1 << (cache)->hash_shift)
66#define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static void flow_cache_new_hashrnd(unsigned long arg)
69{
Timo Teräsd7997fe2010-03-31 00:17:06 +000070 struct flow_cache *fc = (void *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int i;
72
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -070073 for_each_possible_cpu(i)
Timo Teräsd7997fe2010-03-31 00:17:06 +000074 per_cpu_ptr(fc->percpu, i)->hash_rnd_recalc = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Timo Teräsd7997fe2010-03-31 00:17:06 +000076 fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
77 add_timer(&fc->rnd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Timo Teräsfe1a5f02010-04-07 00:30:04 +000080static int flow_entry_valid(struct flow_cache_entry *fle)
81{
82 if (atomic_read(&flow_cache_genid) != fle->genid)
83 return 0;
84 if (fle->object && !fle->object->ops->check(fle->object))
85 return 0;
86 return 1;
87}
88
Timo Teräsd7997fe2010-03-31 00:17:06 +000089static void flow_entry_kill(struct flow_cache *fc,
90 struct flow_cache_percpu *fcp,
91 struct flow_cache_entry *fle)
James Morris134b0fc2006-10-05 15:42:27 -050092{
93 if (fle->object)
Timo Teräsfe1a5f02010-04-07 00:30:04 +000094 fle->object->ops->delete(fle->object);
James Morris134b0fc2006-10-05 15:42:27 -050095 kmem_cache_free(flow_cachep, fle);
Timo Teräsd7997fe2010-03-31 00:17:06 +000096 fcp->hash_count--;
James Morris134b0fc2006-10-05 15:42:27 -050097}
98
Timo Teräsd7997fe2010-03-31 00:17:06 +000099static void __flow_cache_shrink(struct flow_cache *fc,
100 struct flow_cache_percpu *fcp,
101 int shrink_to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct flow_cache_entry *fle, **flp;
104 int i;
105
Timo Teräsd7997fe2010-03-31 00:17:06 +0000106 for (i = 0; i < flow_cache_hash_size(fc); i++) {
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000107 int saved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Timo Teräsd7997fe2010-03-31 00:17:06 +0000109 flp = &fcp->hash_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 while ((fle = *flp) != NULL) {
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000111 if (saved < shrink_to &&
112 flow_entry_valid(fle)) {
113 saved++;
114 flp = &fle->next;
115 } else {
116 *flp = fle->next;
117 flow_entry_kill(fc, fcp, fle);
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 }
121}
122
Timo Teräsd7997fe2010-03-31 00:17:06 +0000123static void flow_cache_shrink(struct flow_cache *fc,
124 struct flow_cache_percpu *fcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000126 int shrink_to = fc->low_watermark / flow_cache_hash_size(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Timo Teräsd7997fe2010-03-31 00:17:06 +0000128 __flow_cache_shrink(fc, fcp, shrink_to);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Timo Teräsd7997fe2010-03-31 00:17:06 +0000131static void flow_new_hash_rnd(struct flow_cache *fc,
132 struct flow_cache_percpu *fcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000134 get_random_bytes(&fcp->hash_rnd, sizeof(u32));
135 fcp->hash_rnd_recalc = 0;
136 __flow_cache_shrink(fc, fcp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Timo Teräsd7997fe2010-03-31 00:17:06 +0000139static u32 flow_hash_code(struct flow_cache *fc,
140 struct flow_cache_percpu *fcp,
141 struct flowi *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 u32 *k = (u32 *) key;
144
Timo Teräsd7997fe2010-03-31 00:17:06 +0000145 return (jhash2(k, (sizeof(*key) / sizeof(u32)), fcp->hash_rnd)
146 & (flow_cache_hash_size(fc) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149#if (BITS_PER_LONG == 64)
150typedef u64 flow_compare_t;
151#else
152typedef u32 flow_compare_t;
153#endif
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* I hear what you're saying, use memcmp. But memcmp cannot make
156 * important assumptions that we can here, such as alignment and
157 * constant size.
158 */
159static int flow_key_compare(struct flowi *key1, struct flowi *key2)
160{
161 flow_compare_t *k1, *k1_lim, *k2;
162 const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t);
163
Pavel Emelyanovf0fe91d2007-10-23 21:15:21 -0700164 BUILD_BUG_ON(sizeof(struct flowi) % sizeof(flow_compare_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 k1 = (flow_compare_t *) key1;
167 k1_lim = k1 + n_elem;
168
169 k2 = (flow_compare_t *) key2;
170
171 do {
172 if (*k1++ != *k2++)
173 return 1;
174 } while (k1 < k1_lim);
175
176 return 0;
177}
178
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000179struct flow_cache_object *
180flow_cache_lookup(struct net *net, struct flowi *key, u16 family, u8 dir,
181 flow_resolve_t resolver, void *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000183 struct flow_cache *fc = &flow_cache_global;
184 struct flow_cache_percpu *fcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct flow_cache_entry *fle, **head;
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000186 struct flow_cache_object *flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 local_bh_disable();
Timo Teräsd7997fe2010-03-31 00:17:06 +0000190 fcp = per_cpu_ptr(fc->percpu, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 fle = NULL;
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000193 flo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Packet really early in init? Making flow_cache_init a
195 * pre-smp initcall would solve this. --RR */
Timo Teräsd7997fe2010-03-31 00:17:06 +0000196 if (!fcp->hash_table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto nocache;
198
Timo Teräsd7997fe2010-03-31 00:17:06 +0000199 if (fcp->hash_rnd_recalc)
200 flow_new_hash_rnd(fc, fcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000202 hash = flow_hash_code(fc, fcp, key);
Timo Teräsd7997fe2010-03-31 00:17:06 +0000203 head = &fcp->hash_table[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 for (fle = *head; fle; fle = fle->next) {
205 if (fle->family == family &&
206 fle->dir == dir &&
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000207 flow_key_compare(key, &fle->key) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000211 if (unlikely(!fle)) {
Timo Teräsd7997fe2010-03-31 00:17:06 +0000212 if (fcp->hash_count > fc->high_watermark)
213 flow_cache_shrink(fc, fcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800215 fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (fle) {
217 fle->next = *head;
218 *head = fle;
219 fle->family = family;
220 fle->dir = dir;
221 memcpy(&fle->key, key, sizeof(*key));
222 fle->object = NULL;
Timo Teräsd7997fe2010-03-31 00:17:06 +0000223 fcp->hash_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000225 } else if (likely(fle->genid == atomic_read(&flow_cache_genid))) {
226 flo = fle->object;
227 if (!flo)
228 goto ret_object;
229 flo = flo->ops->get(flo);
230 if (flo)
231 goto ret_object;
232 } else if (fle->object) {
233 flo = fle->object;
234 flo->ops->delete(flo);
235 fle->object = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
238nocache:
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000239 flo = NULL;
240 if (fle) {
241 flo = fle->object;
242 fle->object = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000244 flo = resolver(net, key, family, dir, flo, ctx);
245 if (fle) {
246 fle->genid = atomic_read(&flow_cache_genid);
247 if (!IS_ERR(flo))
248 fle->object = flo;
249 else
250 fle->genid--;
251 } else {
252 if (flo && !IS_ERR(flo))
253 flo->ops->delete(flo);
254 }
255ret_object:
256 local_bh_enable();
257 return flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260static void flow_cache_flush_tasklet(unsigned long data)
261{
262 struct flow_flush_info *info = (void *)data;
Timo Teräsd7997fe2010-03-31 00:17:06 +0000263 struct flow_cache *fc = info->cache;
264 struct flow_cache_percpu *fcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Timo Teräsd7997fe2010-03-31 00:17:06 +0000267 fcp = per_cpu_ptr(fc->percpu, smp_processor_id());
268 for (i = 0; i < flow_cache_hash_size(fc); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct flow_cache_entry *fle;
270
Timo Teräsd7997fe2010-03-31 00:17:06 +0000271 fle = fcp->hash_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 for (; fle; fle = fle->next) {
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000273 if (flow_entry_valid(fle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 continue;
275
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000276 if (fle->object)
277 fle->object->ops->delete(fle->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 fle->object = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 }
281
282 if (atomic_dec_and_test(&info->cpuleft))
283 complete(&info->completion);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static void flow_cache_flush_per_cpu(void *data)
287{
288 struct flow_flush_info *info = data;
289 int cpu;
290 struct tasklet_struct *tasklet;
291
292 cpu = smp_processor_id();
Timo Teräsd7997fe2010-03-31 00:17:06 +0000293 tasklet = &per_cpu_ptr(info->cache->percpu, cpu)->flush_tasklet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 tasklet->data = (unsigned long)info;
295 tasklet_schedule(tasklet);
296}
297
298void flow_cache_flush(void)
299{
300 struct flow_flush_info info;
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800301 static DEFINE_MUTEX(flow_flush_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Don't want cpus going down or up during this. */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100304 get_online_cpus();
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800305 mutex_lock(&flow_flush_sem);
Timo Teräsd7997fe2010-03-31 00:17:06 +0000306 info.cache = &flow_cache_global;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 atomic_set(&info.cpuleft, num_online_cpus());
308 init_completion(&info.completion);
309
310 local_bh_disable();
Jens Axboe8691e5a2008-06-06 11:18:06 +0200311 smp_call_function(flow_cache_flush_per_cpu, &info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 flow_cache_flush_tasklet((unsigned long)&info);
313 local_bh_enable();
314
315 wait_for_completion(&info.completion);
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800316 mutex_unlock(&flow_flush_sem);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100317 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Timo Teräsd7997fe2010-03-31 00:17:06 +0000320static void __init flow_cache_cpu_prepare(struct flow_cache *fc,
321 struct flow_cache_percpu *fcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000323 fcp->hash_table = (struct flow_cache_entry **)
324 __get_free_pages(GFP_KERNEL|__GFP_ZERO, fc->order);
325 if (!fcp->hash_table)
326 panic("NET: failed to allocate flow cache order %lu\n", fc->order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Timo Teräsd7997fe2010-03-31 00:17:06 +0000328 fcp->hash_rnd_recalc = 1;
329 fcp->hash_count = 0;
330 tasklet_init(&fcp->flush_tasklet, flow_cache_flush_tasklet, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static int flow_cache_cpu(struct notifier_block *nfb,
334 unsigned long action,
335 void *hcpu)
336{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000337 struct flow_cache *fc = container_of(nfb, struct flow_cache, hotcpu_notifier);
338 int cpu = (unsigned long) hcpu;
339 struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);
340
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700341 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
Timo Teräsd7997fe2010-03-31 00:17:06 +0000342 __flow_cache_shrink(fc, fcp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return NOTIFY_OK;
344}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Timo Teräsd7997fe2010-03-31 00:17:06 +0000346static int flow_cache_init(struct flow_cache *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Timo Teräsd7997fe2010-03-31 00:17:06 +0000348 unsigned long order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int i;
350
Timo Teräsd7997fe2010-03-31 00:17:06 +0000351 fc->hash_shift = 10;
352 fc->low_watermark = 2 * flow_cache_hash_size(fc);
353 fc->high_watermark = 4 * flow_cache_hash_size(fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Timo Teräsd7997fe2010-03-31 00:17:06 +0000355 for (order = 0;
356 (PAGE_SIZE << order) <
357 (sizeof(struct flow_cache_entry *)*flow_cache_hash_size(fc));
358 order++)
359 /* NOTHING */;
360 fc->order = order;
361 fc->percpu = alloc_percpu(struct flow_cache_percpu);
362
363 setup_timer(&fc->rnd_timer, flow_cache_new_hashrnd,
364 (unsigned long) fc);
365 fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
366 add_timer(&fc->rnd_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700368 for_each_possible_cpu(i)
Timo Teräsd7997fe2010-03-31 00:17:06 +0000369 flow_cache_cpu_prepare(fc, per_cpu_ptr(fc->percpu, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Timo Teräsd7997fe2010-03-31 00:17:06 +0000371 fc->hotcpu_notifier = (struct notifier_block){
372 .notifier_call = flow_cache_cpu,
373 };
374 register_hotcpu_notifier(&fc->hotcpu_notifier);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
377}
378
Timo Teräsd7997fe2010-03-31 00:17:06 +0000379static int __init flow_cache_init_global(void)
380{
381 flow_cachep = kmem_cache_create("flow_cache",
382 sizeof(struct flow_cache_entry),
383 0, SLAB_PANIC, NULL);
384
385 return flow_cache_init(&flow_cache_global);
386}
387
388module_init(flow_cache_init_global);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390EXPORT_SYMBOL(flow_cache_genid);
391EXPORT_SYMBOL(flow_cache_lookup);