]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* flow.c: Generic flow cache. |
2 | * | |
3 | * Copyright (C) 2003 Alexey N. Kuznetsov ([email protected]) | |
4 | * Copyright (C) 2003 David S. Miller ([email protected]) | |
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> | |
4a3e2f71 | 23 | #include <linux/mutex.h> |
1da177e4 LT |
24 | #include <net/flow.h> |
25 | #include <asm/atomic.h> | |
26 | #include <asm/semaphore.h> | |
df71837d | 27 | #include <linux/security.h> |
1da177e4 LT |
28 | |
29 | struct flow_cache_entry { | |
30 | struct flow_cache_entry *next; | |
31 | u16 family; | |
32 | u8 dir; | |
33 | struct flowi key; | |
34 | u32 genid; | |
35 | void *object; | |
36 | atomic_t *object_ref; | |
37 | }; | |
38 | ||
39 | atomic_t flow_cache_genid = ATOMIC_INIT(0); | |
40 | ||
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 }; | |
44 | ||
45 | #define flow_table(cpu) (per_cpu(flow_tables, cpu)) | |
46 | ||
ba89966c | 47 | static kmem_cache_t *flow_cachep __read_mostly; |
1da177e4 LT |
48 | |
49 | static int flow_lwm, flow_hwm; | |
50 | ||
51 | struct flow_percpu_info { | |
52 | int hash_rnd_recalc; | |
53 | u32 hash_rnd; | |
54 | int count; | |
55 | } ____cacheline_aligned; | |
56 | static DEFINE_PER_CPU(struct flow_percpu_info, flow_hash_info) = { 0 }; | |
57 | ||
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) | |
64 | ||
65 | static struct timer_list flow_hash_rnd_timer; | |
66 | ||
67 | #define FLOW_HASH_RND_PERIOD (10 * 60 * HZ) | |
68 | ||
69 | struct flow_flush_info { | |
70 | atomic_t cpuleft; | |
71 | struct completion completion; | |
72 | }; | |
73 | static DEFINE_PER_CPU(struct tasklet_struct, flow_flush_tasklets) = { NULL }; | |
74 | ||
75 | #define flow_flush_tasklet(cpu) (&per_cpu(flow_flush_tasklets, cpu)) | |
76 | ||
77 | static void flow_cache_new_hashrnd(unsigned long arg) | |
78 | { | |
79 | int i; | |
80 | ||
6f912042 | 81 | for_each_possible_cpu(i) |
1da177e4 LT |
82 | flow_hash_rnd_recalc(i) = 1; |
83 | ||
84 | flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD; | |
85 | add_timer(&flow_hash_rnd_timer); | |
86 | } | |
87 | ||
88 | static void __flow_cache_shrink(int cpu, int shrink_to) | |
89 | { | |
90 | struct flow_cache_entry *fle, **flp; | |
91 | int i; | |
92 | ||
93 | for (i = 0; i < flow_hash_size; i++) { | |
94 | int k = 0; | |
95 | ||
96 | flp = &flow_table(cpu)[i]; | |
97 | while ((fle = *flp) != NULL && k < shrink_to) { | |
98 | k++; | |
99 | flp = &fle->next; | |
100 | } | |
101 | while ((fle = *flp) != NULL) { | |
102 | *flp = fle->next; | |
103 | if (fle->object) | |
104 | atomic_dec(fle->object_ref); | |
105 | kmem_cache_free(flow_cachep, fle); | |
106 | flow_count(cpu)--; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | static void flow_cache_shrink(int cpu) | |
112 | { | |
113 | int shrink_to = flow_lwm / flow_hash_size; | |
114 | ||
115 | __flow_cache_shrink(cpu, shrink_to); | |
116 | } | |
117 | ||
118 | static void flow_new_hash_rnd(int cpu) | |
119 | { | |
120 | get_random_bytes(&flow_hash_rnd(cpu), sizeof(u32)); | |
121 | flow_hash_rnd_recalc(cpu) = 0; | |
122 | ||
123 | __flow_cache_shrink(cpu, 0); | |
124 | } | |
125 | ||
126 | static u32 flow_hash_code(struct flowi *key, int cpu) | |
127 | { | |
128 | u32 *k = (u32 *) key; | |
129 | ||
130 | return (jhash2(k, (sizeof(*key) / sizeof(u32)), flow_hash_rnd(cpu)) & | |
131 | (flow_hash_size - 1)); | |
132 | } | |
133 | ||
134 | #if (BITS_PER_LONG == 64) | |
135 | typedef u64 flow_compare_t; | |
136 | #else | |
137 | typedef u32 flow_compare_t; | |
138 | #endif | |
139 | ||
140 | extern void flowi_is_missized(void); | |
141 | ||
142 | /* I hear what you're saying, use memcmp. But memcmp cannot make | |
143 | * important assumptions that we can here, such as alignment and | |
144 | * constant size. | |
145 | */ | |
146 | static int flow_key_compare(struct flowi *key1, struct flowi *key2) | |
147 | { | |
148 | flow_compare_t *k1, *k1_lim, *k2; | |
149 | const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t); | |
150 | ||
151 | if (sizeof(struct flowi) % sizeof(flow_compare_t)) | |
152 | flowi_is_missized(); | |
153 | ||
154 | k1 = (flow_compare_t *) key1; | |
155 | k1_lim = k1 + n_elem; | |
156 | ||
157 | k2 = (flow_compare_t *) key2; | |
158 | ||
159 | do { | |
160 | if (*k1++ != *k2++) | |
161 | return 1; | |
162 | } while (k1 < k1_lim); | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
e0d1caa7 | 167 | void *flow_cache_lookup(struct flowi *key, u16 family, u8 dir, |
1da177e4 LT |
168 | flow_resolve_t resolver) |
169 | { | |
170 | struct flow_cache_entry *fle, **head; | |
171 | unsigned int hash; | |
172 | int cpu; | |
173 | ||
174 | local_bh_disable(); | |
175 | cpu = smp_processor_id(); | |
176 | ||
177 | fle = NULL; | |
178 | /* Packet really early in init? Making flow_cache_init a | |
179 | * pre-smp initcall would solve this. --RR */ | |
180 | if (!flow_table(cpu)) | |
181 | goto nocache; | |
182 | ||
183 | if (flow_hash_rnd_recalc(cpu)) | |
184 | flow_new_hash_rnd(cpu); | |
185 | hash = flow_hash_code(key, cpu); | |
186 | ||
187 | head = &flow_table(cpu)[hash]; | |
188 | for (fle = *head; fle; fle = fle->next) { | |
189 | if (fle->family == family && | |
190 | fle->dir == dir && | |
191 | flow_key_compare(key, &fle->key) == 0) { | |
192 | if (fle->genid == atomic_read(&flow_cache_genid)) { | |
193 | void *ret = fle->object; | |
194 | ||
195 | if (ret) | |
196 | atomic_inc(fle->object_ref); | |
197 | local_bh_enable(); | |
198 | ||
199 | return ret; | |
200 | } | |
201 | break; | |
202 | } | |
203 | } | |
204 | ||
205 | if (!fle) { | |
206 | if (flow_count(cpu) > flow_hwm) | |
207 | flow_cache_shrink(cpu); | |
208 | ||
209 | fle = kmem_cache_alloc(flow_cachep, SLAB_ATOMIC); | |
210 | if (fle) { | |
211 | fle->next = *head; | |
212 | *head = fle; | |
213 | fle->family = family; | |
214 | fle->dir = dir; | |
215 | memcpy(&fle->key, key, sizeof(*key)); | |
216 | fle->object = NULL; | |
217 | flow_count(cpu)++; | |
218 | } | |
219 | } | |
220 | ||
221 | nocache: | |
222 | { | |
223 | void *obj; | |
224 | atomic_t *obj_ref; | |
225 | ||
e0d1caa7 | 226 | resolver(key, family, dir, &obj, &obj_ref); |
1da177e4 LT |
227 | |
228 | if (fle) { | |
229 | fle->genid = atomic_read(&flow_cache_genid); | |
230 | ||
231 | if (fle->object) | |
232 | atomic_dec(fle->object_ref); | |
233 | ||
234 | fle->object = obj; | |
235 | fle->object_ref = obj_ref; | |
236 | if (obj) | |
237 | atomic_inc(fle->object_ref); | |
238 | } | |
239 | local_bh_enable(); | |
240 | ||
241 | return obj; | |
242 | } | |
243 | } | |
244 | ||
245 | static void flow_cache_flush_tasklet(unsigned long data) | |
246 | { | |
247 | struct flow_flush_info *info = (void *)data; | |
248 | int i; | |
249 | int cpu; | |
250 | ||
251 | cpu = smp_processor_id(); | |
252 | for (i = 0; i < flow_hash_size; i++) { | |
253 | struct flow_cache_entry *fle; | |
254 | ||
255 | fle = flow_table(cpu)[i]; | |
256 | for (; fle; fle = fle->next) { | |
257 | unsigned genid = atomic_read(&flow_cache_genid); | |
258 | ||
259 | if (!fle->object || fle->genid == genid) | |
260 | continue; | |
261 | ||
262 | fle->object = NULL; | |
263 | atomic_dec(fle->object_ref); | |
264 | } | |
265 | } | |
266 | ||
267 | if (atomic_dec_and_test(&info->cpuleft)) | |
268 | complete(&info->completion); | |
269 | } | |
270 | ||
271 | static void flow_cache_flush_per_cpu(void *) __attribute__((__unused__)); | |
272 | static void flow_cache_flush_per_cpu(void *data) | |
273 | { | |
274 | struct flow_flush_info *info = data; | |
275 | int cpu; | |
276 | struct tasklet_struct *tasklet; | |
277 | ||
278 | cpu = smp_processor_id(); | |
279 | ||
280 | tasklet = flow_flush_tasklet(cpu); | |
281 | tasklet->data = (unsigned long)info; | |
282 | tasklet_schedule(tasklet); | |
283 | } | |
284 | ||
285 | void flow_cache_flush(void) | |
286 | { | |
287 | struct flow_flush_info info; | |
4a3e2f71 | 288 | static DEFINE_MUTEX(flow_flush_sem); |
1da177e4 LT |
289 | |
290 | /* Don't want cpus going down or up during this. */ | |
291 | lock_cpu_hotplug(); | |
4a3e2f71 | 292 | mutex_lock(&flow_flush_sem); |
1da177e4 LT |
293 | atomic_set(&info.cpuleft, num_online_cpus()); |
294 | init_completion(&info.completion); | |
295 | ||
296 | local_bh_disable(); | |
297 | smp_call_function(flow_cache_flush_per_cpu, &info, 1, 0); | |
298 | flow_cache_flush_tasklet((unsigned long)&info); | |
299 | local_bh_enable(); | |
300 | ||
301 | wait_for_completion(&info.completion); | |
4a3e2f71 | 302 | mutex_unlock(&flow_flush_sem); |
1da177e4 LT |
303 | unlock_cpu_hotplug(); |
304 | } | |
305 | ||
306 | static void __devinit flow_cache_cpu_prepare(int cpu) | |
307 | { | |
308 | struct tasklet_struct *tasklet; | |
309 | unsigned long order; | |
310 | ||
311 | for (order = 0; | |
312 | (PAGE_SIZE << order) < | |
313 | (sizeof(struct flow_cache_entry *)*flow_hash_size); | |
314 | order++) | |
315 | /* NOTHING */; | |
316 | ||
317 | flow_table(cpu) = (struct flow_cache_entry **) | |
77d04bd9 | 318 | __get_free_pages(GFP_KERNEL|__GFP_ZERO, order); |
1da177e4 LT |
319 | if (!flow_table(cpu)) |
320 | panic("NET: failed to allocate flow cache order %lu\n", order); | |
321 | ||
1da177e4 LT |
322 | flow_hash_rnd_recalc(cpu) = 1; |
323 | flow_count(cpu) = 0; | |
324 | ||
325 | tasklet = flow_flush_tasklet(cpu); | |
326 | tasklet_init(tasklet, flow_cache_flush_tasklet, 0); | |
327 | } | |
328 | ||
329 | #ifdef CONFIG_HOTPLUG_CPU | |
330 | static int flow_cache_cpu(struct notifier_block *nfb, | |
331 | unsigned long action, | |
332 | void *hcpu) | |
333 | { | |
334 | if (action == CPU_DEAD) | |
335 | __flow_cache_shrink((unsigned long)hcpu, 0); | |
336 | return NOTIFY_OK; | |
337 | } | |
338 | #endif /* CONFIG_HOTPLUG_CPU */ | |
339 | ||
340 | static int __init flow_cache_init(void) | |
341 | { | |
342 | int i; | |
343 | ||
344 | flow_cachep = kmem_cache_create("flow_cache", | |
345 | sizeof(struct flow_cache_entry), | |
e5d679f3 | 346 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
1da177e4 | 347 | NULL, NULL); |
1da177e4 LT |
348 | flow_hash_shift = 10; |
349 | flow_lwm = 2 * flow_hash_size; | |
350 | flow_hwm = 4 * flow_hash_size; | |
351 | ||
352 | init_timer(&flow_hash_rnd_timer); | |
353 | flow_hash_rnd_timer.function = flow_cache_new_hashrnd; | |
354 | flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD; | |
355 | add_timer(&flow_hash_rnd_timer); | |
356 | ||
6f912042 | 357 | for_each_possible_cpu(i) |
1da177e4 LT |
358 | flow_cache_cpu_prepare(i); |
359 | ||
360 | hotcpu_notifier(flow_cache_cpu, 0); | |
361 | return 0; | |
362 | } | |
363 | ||
364 | module_init(flow_cache_init); | |
365 | ||
366 | EXPORT_SYMBOL(flow_cache_genid); | |
367 | EXPORT_SYMBOL(flow_cache_lookup); |