Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <net/flow.h> |
| 25 | #include <asm/atomic.h> |
| 26 | #include <asm/semaphore.h> |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 27 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 47 | static struct kmem_cache *flow_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 81 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 88 | static void flow_entry_kill(int cpu, struct flow_cache_entry *fle) |
| 89 | { |
| 90 | if (fle->object) |
| 91 | atomic_dec(fle->object_ref); |
| 92 | kmem_cache_free(flow_cachep, fle); |
| 93 | flow_count(cpu)--; |
| 94 | } |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | static void __flow_cache_shrink(int cpu, int shrink_to) |
| 97 | { |
| 98 | struct flow_cache_entry *fle, **flp; |
| 99 | int i; |
| 100 | |
| 101 | for (i = 0; i < flow_hash_size; i++) { |
| 102 | int k = 0; |
| 103 | |
| 104 | flp = &flow_table(cpu)[i]; |
| 105 | while ((fle = *flp) != NULL && k < shrink_to) { |
| 106 | k++; |
| 107 | flp = &fle->next; |
| 108 | } |
| 109 | while ((fle = *flp) != NULL) { |
| 110 | *flp = fle->next; |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 111 | flow_entry_kill(cpu, fle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static void flow_cache_shrink(int cpu) |
| 117 | { |
| 118 | int shrink_to = flow_lwm / flow_hash_size; |
| 119 | |
| 120 | __flow_cache_shrink(cpu, shrink_to); |
| 121 | } |
| 122 | |
| 123 | static void flow_new_hash_rnd(int cpu) |
| 124 | { |
| 125 | get_random_bytes(&flow_hash_rnd(cpu), sizeof(u32)); |
| 126 | flow_hash_rnd_recalc(cpu) = 0; |
| 127 | |
| 128 | __flow_cache_shrink(cpu, 0); |
| 129 | } |
| 130 | |
| 131 | static u32 flow_hash_code(struct flowi *key, int cpu) |
| 132 | { |
| 133 | u32 *k = (u32 *) key; |
| 134 | |
| 135 | return (jhash2(k, (sizeof(*key) / sizeof(u32)), flow_hash_rnd(cpu)) & |
| 136 | (flow_hash_size - 1)); |
| 137 | } |
| 138 | |
| 139 | #if (BITS_PER_LONG == 64) |
| 140 | typedef u64 flow_compare_t; |
| 141 | #else |
| 142 | typedef u32 flow_compare_t; |
| 143 | #endif |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* 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 | */ |
| 149 | static 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 Emelyanov | f0fe91d | 2007-10-23 21:15:21 -0700 | [diff] [blame] | 154 | BUILD_BUG_ON(sizeof(struct flowi) % sizeof(flow_compare_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 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 | |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 169 | void *flow_cache_lookup(struct flowi *key, u16 family, u8 dir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | flow_resolve_t resolver) |
| 171 | { |
| 172 | struct flow_cache_entry *fle, **head; |
| 173 | unsigned int hash; |
| 174 | int cpu; |
| 175 | |
| 176 | local_bh_disable(); |
| 177 | cpu = smp_processor_id(); |
| 178 | |
| 179 | fle = NULL; |
| 180 | /* Packet really early in init? Making flow_cache_init a |
| 181 | * pre-smp initcall would solve this. --RR */ |
| 182 | if (!flow_table(cpu)) |
| 183 | goto nocache; |
| 184 | |
| 185 | if (flow_hash_rnd_recalc(cpu)) |
| 186 | flow_new_hash_rnd(cpu); |
| 187 | hash = flow_hash_code(key, cpu); |
| 188 | |
| 189 | head = &flow_table(cpu)[hash]; |
| 190 | for (fle = *head; fle; fle = fle->next) { |
| 191 | if (fle->family == family && |
| 192 | fle->dir == dir && |
| 193 | flow_key_compare(key, &fle->key) == 0) { |
| 194 | if (fle->genid == atomic_read(&flow_cache_genid)) { |
| 195 | void *ret = fle->object; |
| 196 | |
| 197 | if (ret) |
| 198 | atomic_inc(fle->object_ref); |
| 199 | local_bh_enable(); |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (!fle) { |
| 208 | if (flow_count(cpu) > flow_hwm) |
| 209 | flow_cache_shrink(cpu); |
| 210 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 211 | fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (fle) { |
| 213 | fle->next = *head; |
| 214 | *head = fle; |
| 215 | fle->family = family; |
| 216 | fle->dir = dir; |
| 217 | memcpy(&fle->key, key, sizeof(*key)); |
| 218 | fle->object = NULL; |
| 219 | flow_count(cpu)++; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | nocache: |
| 224 | { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 225 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | void *obj; |
| 227 | atomic_t *obj_ref; |
| 228 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 229 | err = resolver(key, family, dir, &obj, &obj_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Herbert Xu | e0e8f1c | 2007-01-10 22:06:32 -0800 | [diff] [blame] | 231 | if (fle && !err) { |
| 232 | fle->genid = atomic_read(&flow_cache_genid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Herbert Xu | e0e8f1c | 2007-01-10 22:06:32 -0800 | [diff] [blame] | 234 | if (fle->object) |
| 235 | atomic_dec(fle->object_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Herbert Xu | e0e8f1c | 2007-01-10 22:06:32 -0800 | [diff] [blame] | 237 | fle->object = obj; |
| 238 | fle->object_ref = obj_ref; |
| 239 | if (obj) |
| 240 | atomic_inc(fle->object_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | local_bh_enable(); |
| 243 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 244 | if (err) |
| 245 | obj = ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return obj; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | static void flow_cache_flush_tasklet(unsigned long data) |
| 251 | { |
| 252 | struct flow_flush_info *info = (void *)data; |
| 253 | int i; |
| 254 | int cpu; |
| 255 | |
| 256 | cpu = smp_processor_id(); |
| 257 | for (i = 0; i < flow_hash_size; i++) { |
| 258 | struct flow_cache_entry *fle; |
| 259 | |
| 260 | fle = flow_table(cpu)[i]; |
| 261 | for (; fle; fle = fle->next) { |
| 262 | unsigned genid = atomic_read(&flow_cache_genid); |
| 263 | |
| 264 | if (!fle->object || fle->genid == genid) |
| 265 | continue; |
| 266 | |
| 267 | fle->object = NULL; |
| 268 | atomic_dec(fle->object_ref); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (atomic_dec_and_test(&info->cpuleft)) |
| 273 | complete(&info->completion); |
| 274 | } |
| 275 | |
| 276 | static void flow_cache_flush_per_cpu(void *) __attribute__((__unused__)); |
| 277 | static void flow_cache_flush_per_cpu(void *data) |
| 278 | { |
| 279 | struct flow_flush_info *info = data; |
| 280 | int cpu; |
| 281 | struct tasklet_struct *tasklet; |
| 282 | |
| 283 | cpu = smp_processor_id(); |
| 284 | |
| 285 | tasklet = flow_flush_tasklet(cpu); |
| 286 | tasklet->data = (unsigned long)info; |
| 287 | tasklet_schedule(tasklet); |
| 288 | } |
| 289 | |
| 290 | void flow_cache_flush(void) |
| 291 | { |
| 292 | struct flow_flush_info info; |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 293 | static DEFINE_MUTEX(flow_flush_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | /* Don't want cpus going down or up during this. */ |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 296 | get_online_cpus(); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 297 | mutex_lock(&flow_flush_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | atomic_set(&info.cpuleft, num_online_cpus()); |
| 299 | init_completion(&info.completion); |
| 300 | |
| 301 | local_bh_disable(); |
| 302 | smp_call_function(flow_cache_flush_per_cpu, &info, 1, 0); |
| 303 | flow_cache_flush_tasklet((unsigned long)&info); |
| 304 | local_bh_enable(); |
| 305 | |
| 306 | wait_for_completion(&info.completion); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 307 | mutex_unlock(&flow_flush_sem); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 308 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void __devinit flow_cache_cpu_prepare(int cpu) |
| 312 | { |
| 313 | struct tasklet_struct *tasklet; |
| 314 | unsigned long order; |
| 315 | |
| 316 | for (order = 0; |
| 317 | (PAGE_SIZE << order) < |
| 318 | (sizeof(struct flow_cache_entry *)*flow_hash_size); |
| 319 | order++) |
| 320 | /* NOTHING */; |
| 321 | |
| 322 | flow_table(cpu) = (struct flow_cache_entry **) |
Andrew Morton | 77d04bd | 2006-04-07 14:52:59 -0700 | [diff] [blame] | 323 | __get_free_pages(GFP_KERNEL|__GFP_ZERO, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | if (!flow_table(cpu)) |
| 325 | panic("NET: failed to allocate flow cache order %lu\n", order); |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | flow_hash_rnd_recalc(cpu) = 1; |
| 328 | flow_count(cpu) = 0; |
| 329 | |
| 330 | tasklet = flow_flush_tasklet(cpu); |
| 331 | tasklet_init(tasklet, flow_cache_flush_tasklet, 0); |
| 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | static int flow_cache_cpu(struct notifier_block *nfb, |
| 335 | unsigned long action, |
| 336 | void *hcpu) |
| 337 | { |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 338 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | __flow_cache_shrink((unsigned long)hcpu, 0); |
| 340 | return NOTIFY_OK; |
| 341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | static int __init flow_cache_init(void) |
| 344 | { |
| 345 | int i; |
| 346 | |
| 347 | flow_cachep = kmem_cache_create("flow_cache", |
| 348 | sizeof(struct flow_cache_entry), |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 349 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 350 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | flow_hash_shift = 10; |
| 352 | flow_lwm = 2 * flow_hash_size; |
| 353 | flow_hwm = 4 * flow_hash_size; |
| 354 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 355 | setup_timer(&flow_hash_rnd_timer, flow_cache_new_hashrnd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD; |
| 357 | add_timer(&flow_hash_rnd_timer); |
| 358 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 359 | for_each_possible_cpu(i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | flow_cache_cpu_prepare(i); |
| 361 | |
| 362 | hotcpu_notifier(flow_cache_cpu, 0); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | module_init(flow_cache_init); |
| 367 | |
| 368 | EXPORT_SYMBOL(flow_cache_genid); |
| 369 | EXPORT_SYMBOL(flow_cache_lookup); |