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> |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 26 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | struct flow_cache_entry { |
| 29 | struct flow_cache_entry *next; |
| 30 | u16 family; |
| 31 | u8 dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | u32 genid; |
Eric Dumazet | dd5a184 | 2008-02-07 23:30:42 -0800 | [diff] [blame] | 33 | struct flowi key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | void *object; |
| 35 | atomic_t *object_ref; |
| 36 | }; |
| 37 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 38 | struct 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 Dumazet | 5f58a5c | 2008-02-07 18:03:18 -0800 | [diff] [blame] | 44 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | struct flow_flush_info { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 47 | struct flow_cache * cache; |
| 48 | atomic_t cpuleft; |
| 49 | struct completion completion; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 52 | struct 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 | |
| 62 | atomic_t flow_cache_genid = ATOMIC_INIT(0); |
| 63 | static struct flow_cache flow_cache_global; |
| 64 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | static void flow_cache_new_hashrnd(unsigned long arg) |
| 70 | { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 71 | struct flow_cache *fc = (void *) arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | int i; |
| 73 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 74 | for_each_possible_cpu(i) |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 75 | per_cpu_ptr(fc->percpu, i)->hash_rnd_recalc = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 77 | fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD; |
| 78 | add_timer(&fc->rnd_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 81 | static void flow_entry_kill(struct flow_cache *fc, |
| 82 | struct flow_cache_percpu *fcp, |
| 83 | struct flow_cache_entry *fle) |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 84 | { |
| 85 | if (fle->object) |
| 86 | atomic_dec(fle->object_ref); |
| 87 | kmem_cache_free(flow_cachep, fle); |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 88 | fcp->hash_count--; |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 91 | static void __flow_cache_shrink(struct flow_cache *fc, |
| 92 | struct flow_cache_percpu *fcp, |
| 93 | int shrink_to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | struct flow_cache_entry *fle, **flp; |
| 96 | int i; |
| 97 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 98 | for (i = 0; i < flow_cache_hash_size(fc); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | int k = 0; |
| 100 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 101 | flp = &fcp->hash_table[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | 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äs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 108 | flow_entry_kill(fc, fcp, fle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 113 | static void flow_cache_shrink(struct flow_cache *fc, |
| 114 | struct flow_cache_percpu *fcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 116 | int shrink_to = fc->low_watermark / flow_cache_hash_size(fc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 118 | __flow_cache_shrink(fc, fcp, shrink_to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 121 | static void flow_new_hash_rnd(struct flow_cache *fc, |
| 122 | struct flow_cache_percpu *fcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 124 | get_random_bytes(&fcp->hash_rnd, sizeof(u32)); |
| 125 | fcp->hash_rnd_recalc = 0; |
| 126 | __flow_cache_shrink(fc, fcp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 129 | static u32 flow_hash_code(struct flow_cache *fc, |
| 130 | struct flow_cache_percpu *fcp, |
| 131 | struct flowi *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | u32 *k = (u32 *) key; |
| 134 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 135 | return (jhash2(k, (sizeof(*key) / sizeof(u32)), fcp->hash_rnd) |
| 136 | & (flow_cache_hash_size(fc) - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 169 | void *flow_cache_lookup(struct net *net, 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 | { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 172 | struct flow_cache *fc = &flow_cache_global; |
| 173 | struct flow_cache_percpu *fcp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | struct flow_cache_entry *fle, **head; |
| 175 | unsigned int hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | local_bh_disable(); |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 178 | fcp = per_cpu_ptr(fc->percpu, smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | fle = NULL; |
| 181 | /* Packet really early in init? Making flow_cache_init a |
| 182 | * pre-smp initcall would solve this. --RR */ |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 183 | if (!fcp->hash_table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | goto nocache; |
| 185 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 186 | if (fcp->hash_rnd_recalc) |
| 187 | flow_new_hash_rnd(fc, fcp); |
| 188 | hash = flow_hash_code(fc, fcp, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 190 | head = &fcp->hash_table[hash]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | 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äs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 209 | if (fcp->hash_count > fc->high_watermark) |
| 210 | flow_cache_shrink(fc, fcp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 212 | fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | 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äs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 220 | fcp->hash_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | nocache: |
| 225 | { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 226 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | void *obj; |
| 228 | atomic_t *obj_ref; |
| 229 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 230 | err = resolver(net, key, family, dir, &obj, &obj_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Herbert Xu | e0e8f1c | 2007-01-10 22:06:32 -0800 | [diff] [blame] | 232 | if (fle && !err) { |
| 233 | fle->genid = atomic_read(&flow_cache_genid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Herbert Xu | e0e8f1c | 2007-01-10 22:06:32 -0800 | [diff] [blame] | 235 | if (fle->object) |
| 236 | atomic_dec(fle->object_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Herbert Xu | e0e8f1c | 2007-01-10 22:06:32 -0800 | [diff] [blame] | 238 | fle->object = obj; |
| 239 | fle->object_ref = obj_ref; |
| 240 | if (obj) |
| 241 | atomic_inc(fle->object_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | local_bh_enable(); |
| 244 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 245 | if (err) |
| 246 | obj = ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return obj; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | static void flow_cache_flush_tasklet(unsigned long data) |
| 252 | { |
| 253 | struct flow_flush_info *info = (void *)data; |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 254 | struct flow_cache *fc = info->cache; |
| 255 | struct flow_cache_percpu *fcp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 258 | fcp = per_cpu_ptr(fc->percpu, smp_processor_id()); |
| 259 | for (i = 0; i < flow_cache_hash_size(fc); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | struct flow_cache_entry *fle; |
| 261 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 262 | fle = fcp->hash_table[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | static 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äs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 285 | tasklet = &per_cpu_ptr(info->cache->percpu, cpu)->flush_tasklet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 298 | info.cache = &flow_cache_global; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | atomic_set(&info.cpuleft, num_online_cpus()); |
| 300 | init_completion(&info.completion); |
| 301 | |
| 302 | local_bh_disable(); |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 303 | smp_call_function(flow_cache_flush_per_cpu, &info, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | flow_cache_flush_tasklet((unsigned long)&info); |
| 305 | local_bh_enable(); |
| 306 | |
| 307 | wait_for_completion(&info.completion); |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 308 | mutex_unlock(&flow_flush_sem); |
Gautham R Shenoy | 86ef5c9 | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 309 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 312 | static void __init flow_cache_cpu_prepare(struct flow_cache *fc, |
| 313 | struct flow_cache_percpu *fcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 315 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 320 | fcp->hash_rnd_recalc = 1; |
| 321 | fcp->hash_count = 0; |
| 322 | tasklet_init(&fcp->flush_tasklet, flow_cache_flush_tasklet, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | static int flow_cache_cpu(struct notifier_block *nfb, |
| 326 | unsigned long action, |
| 327 | void *hcpu) |
| 328 | { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 329 | 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. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 333 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 334 | __flow_cache_shrink(fc, fcp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return NOTIFY_OK; |
| 336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 338 | static int flow_cache_init(struct flow_cache *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 340 | unsigned long order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | int i; |
| 342 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 343 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 347 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 360 | for_each_possible_cpu(i) |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 361 | flow_cache_cpu_prepare(fc, per_cpu_ptr(fc->percpu, i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 363 | fc->hotcpu_notifier = (struct notifier_block){ |
| 364 | .notifier_call = flow_cache_cpu, |
| 365 | }; |
| 366 | register_hotcpu_notifier(&fc->hotcpu_notifier); |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
Timo Teräs | d7997fe | 2010-03-31 00:17:06 +0000 | [diff] [blame^] | 371 | static 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 | |
| 380 | module_init(flow_cache_init_global); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | EXPORT_SYMBOL(flow_cache_genid); |
| 383 | EXPORT_SYMBOL(flow_cache_lookup); |