1 /* flow.c: Generic flow cache.
3 * Copyright (C) 2003 Alexey N. Kuznetsov (kuznet@ms2.inr.ac.ru)
4 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
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>
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>
24 #include <asm/atomic.h>
25 #include <asm/semaphore.h>
26 #include <linux/security.h>
28 struct flow_cache_entry
{
29 struct flow_cache_entry
*next
;
39 atomic_t flow_cache_genid
= ATOMIC_INIT(0);
41 static u32 flow_hash_shift
;
42 #define flow_hash_size (1 << flow_hash_shift)
43 static DEFINE_PER_CPU(struct flow_cache_entry
**, flow_tables
) = { NULL
};
45 #define flow_table(cpu) (per_cpu(flow_tables, cpu))
47 static kmem_cache_t
*flow_cachep __read_mostly
;
49 static int flow_lwm
, flow_hwm
;
51 struct flow_percpu_info
{
55 } ____cacheline_aligned
;
56 static DEFINE_PER_CPU(struct flow_percpu_info
, flow_hash_info
) = { 0 };
58 #define flow_hash_rnd_recalc(cpu) \
59 (per_cpu(flow_hash_info, cpu).hash_rnd_recalc)
60 #define flow_hash_rnd(cpu) \
61 (per_cpu(flow_hash_info, cpu).hash_rnd)
62 #define flow_count(cpu) \
63 (per_cpu(flow_hash_info, cpu).count)
65 static struct timer_list flow_hash_rnd_timer
;
67 #define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
69 struct flow_flush_info
{
71 struct completion completion
;
73 static DEFINE_PER_CPU(struct tasklet_struct
, flow_flush_tasklets
) = { NULL
};
75 #define flow_flush_tasklet(cpu) (&per_cpu(flow_flush_tasklets, cpu))
77 static void flow_cache_new_hashrnd(unsigned long arg
)
82 flow_hash_rnd_recalc(i
) = 1;
84 flow_hash_rnd_timer
.expires
= jiffies
+ FLOW_HASH_RND_PERIOD
;
85 add_timer(&flow_hash_rnd_timer
);
88 static void __flow_cache_shrink(int cpu
, int shrink_to
)
90 struct flow_cache_entry
*fle
, **flp
;
93 for (i
= 0; i
< flow_hash_size
; i
++) {
96 flp
= &flow_table(cpu
)[i
];
97 while ((fle
= *flp
) != NULL
&& k
< shrink_to
) {
101 while ((fle
= *flp
) != NULL
) {
104 atomic_dec(fle
->object_ref
);
105 kmem_cache_free(flow_cachep
, fle
);
111 static void flow_cache_shrink(int cpu
)
113 int shrink_to
= flow_lwm
/ flow_hash_size
;
115 __flow_cache_shrink(cpu
, shrink_to
);
118 static void flow_new_hash_rnd(int cpu
)
120 get_random_bytes(&flow_hash_rnd(cpu
), sizeof(u32
));
121 flow_hash_rnd_recalc(cpu
) = 0;
123 __flow_cache_shrink(cpu
, 0);
126 static u32
flow_hash_code(struct flowi
*key
, int cpu
)
128 u32
*k
= (u32
*) key
;
130 return (jhash2(k
, (sizeof(*key
) / sizeof(u32
)), flow_hash_rnd(cpu
)) &
131 (flow_hash_size
- 1));
134 #if (BITS_PER_LONG == 64)
135 typedef u64 flow_compare_t
;
137 typedef u32 flow_compare_t
;
140 extern void flowi_is_missized(void);
142 /* I hear what you're saying, use memcmp. But memcmp cannot make
143 * important assumptions that we can here, such as alignment and
146 static int flow_key_compare(struct flowi
*key1
, struct flowi
*key2
)
148 flow_compare_t
*k1
, *k1_lim
, *k2
;
149 const int n_elem
= sizeof(struct flowi
) / sizeof(flow_compare_t
);
151 if (sizeof(struct flowi
) % sizeof(flow_compare_t
))
154 k1
= (flow_compare_t
*) key1
;
155 k1_lim
= k1
+ n_elem
;
157 k2
= (flow_compare_t
*) key2
;
162 } while (k1
< k1_lim
);
167 void *flow_cache_lookup(struct flowi
*key
, u32 sk_sid
, u16 family
, u8 dir
,
168 flow_resolve_t resolver
)
170 struct flow_cache_entry
*fle
, **head
;
175 cpu
= smp_processor_id();
178 /* Packet really early in init? Making flow_cache_init a
179 * pre-smp initcall would solve this. --RR */
180 if (!flow_table(cpu
))
183 if (flow_hash_rnd_recalc(cpu
))
184 flow_new_hash_rnd(cpu
);
185 hash
= flow_hash_code(key
, cpu
);
187 head
= &flow_table(cpu
)[hash
];
188 for (fle
= *head
; fle
; fle
= fle
->next
) {
189 if (fle
->family
== family
&&
191 fle
->sk_sid
== sk_sid
&&
192 flow_key_compare(key
, &fle
->key
) == 0) {
193 if (fle
->genid
== atomic_read(&flow_cache_genid
)) {
194 void *ret
= fle
->object
;
197 atomic_inc(fle
->object_ref
);
207 if (flow_count(cpu
) > flow_hwm
)
208 flow_cache_shrink(cpu
);
210 fle
= kmem_cache_alloc(flow_cachep
, SLAB_ATOMIC
);
214 fle
->family
= family
;
216 fle
->sk_sid
= sk_sid
;
217 memcpy(&fle
->key
, key
, sizeof(*key
));
228 resolver(key
, sk_sid
, family
, dir
, &obj
, &obj_ref
);
231 fle
->genid
= atomic_read(&flow_cache_genid
);
234 atomic_dec(fle
->object_ref
);
237 fle
->object_ref
= obj_ref
;
239 atomic_inc(fle
->object_ref
);
247 static void flow_cache_flush_tasklet(unsigned long data
)
249 struct flow_flush_info
*info
= (void *)data
;
253 cpu
= smp_processor_id();
254 for (i
= 0; i
< flow_hash_size
; i
++) {
255 struct flow_cache_entry
*fle
;
257 fle
= flow_table(cpu
)[i
];
258 for (; fle
; fle
= fle
->next
) {
259 unsigned genid
= atomic_read(&flow_cache_genid
);
261 if (!fle
->object
|| fle
->genid
== genid
)
265 atomic_dec(fle
->object_ref
);
269 if (atomic_dec_and_test(&info
->cpuleft
))
270 complete(&info
->completion
);
273 static void flow_cache_flush_per_cpu(void *) __attribute__((__unused__
));
274 static void flow_cache_flush_per_cpu(void *data
)
276 struct flow_flush_info
*info
= data
;
278 struct tasklet_struct
*tasklet
;
280 cpu
= smp_processor_id();
282 tasklet
= flow_flush_tasklet(cpu
);
283 tasklet
->data
= (unsigned long)info
;
284 tasklet_schedule(tasklet
);
287 void flow_cache_flush(void)
289 struct flow_flush_info info
;
290 static DECLARE_MUTEX(flow_flush_sem
);
292 /* Don't want cpus going down or up during this. */
294 down(&flow_flush_sem
);
295 atomic_set(&info
.cpuleft
, num_online_cpus());
296 init_completion(&info
.completion
);
299 smp_call_function(flow_cache_flush_per_cpu
, &info
, 1, 0);
300 flow_cache_flush_tasklet((unsigned long)&info
);
303 wait_for_completion(&info
.completion
);
305 unlock_cpu_hotplug();
308 static void __devinit
flow_cache_cpu_prepare(int cpu
)
310 struct tasklet_struct
*tasklet
;
314 (PAGE_SIZE
<< order
) <
315 (sizeof(struct flow_cache_entry
*)*flow_hash_size
);
319 flow_table(cpu
) = (struct flow_cache_entry
**)
320 __get_free_pages(GFP_KERNEL
, order
);
321 if (!flow_table(cpu
))
322 panic("NET: failed to allocate flow cache order %lu\n", order
);
324 memset(flow_table(cpu
), 0, PAGE_SIZE
<< order
);
326 flow_hash_rnd_recalc(cpu
) = 1;
329 tasklet
= flow_flush_tasklet(cpu
);
330 tasklet_init(tasklet
, flow_cache_flush_tasklet
, 0);
333 #ifdef CONFIG_HOTPLUG_CPU
334 static int flow_cache_cpu(struct notifier_block
*nfb
,
335 unsigned long action
,
338 if (action
== CPU_DEAD
)
339 __flow_cache_shrink((unsigned long)hcpu
, 0);
342 #endif /* CONFIG_HOTPLUG_CPU */
344 static int __init
flow_cache_init(void)
348 flow_cachep
= kmem_cache_create("flow_cache",
349 sizeof(struct flow_cache_entry
),
350 0, SLAB_HWCACHE_ALIGN
,
354 panic("NET: failed to allocate flow cache slab\n");
356 flow_hash_shift
= 10;
357 flow_lwm
= 2 * flow_hash_size
;
358 flow_hwm
= 4 * flow_hash_size
;
360 init_timer(&flow_hash_rnd_timer
);
361 flow_hash_rnd_timer
.function
= flow_cache_new_hashrnd
;
362 flow_hash_rnd_timer
.expires
= jiffies
+ FLOW_HASH_RND_PERIOD
;
363 add_timer(&flow_hash_rnd_timer
);
366 flow_cache_cpu_prepare(i
);
368 hotcpu_notifier(flow_cache_cpu
, 0);
372 module_init(flow_cache_init
);
374 EXPORT_SYMBOL(flow_cache_genid
);
375 EXPORT_SYMBOL(flow_cache_lookup
);