flow.c 10.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* flow.c: Generic flow cache.
 *
 * Copyright (C) 2003 Alexey N. Kuznetsov (kuznet@ms2.inr.ac.ru)
 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/jhash.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/smp.h>
#include <linux/completion.h>
#include <linux/percpu.h>
#include <linux/bitops.h>
#include <linux/notifier.h>
#include <linux/cpu.h>
#include <linux/cpumask.h>
A
Arjan van de Ven 已提交
23
#include <linux/mutex.h>
L
Linus Torvalds 已提交
24 25
#include <net/flow.h>
#include <asm/atomic.h>
26
#include <linux/security.h>
L
Linus Torvalds 已提交
27 28

struct flow_cache_entry {
29 30 31 32
	union {
		struct hlist_node	hlist;
		struct list_head	gc_list;
	} u;
33 34 35 36 37
	u16				family;
	u8				dir;
	u32				genid;
	struct flowi			key;
	struct flow_cache_object	*object;
L
Linus Torvalds 已提交
38 39
};

T
Timo Teräs 已提交
40
struct flow_cache_percpu {
41
	struct hlist_head		*hash_table;
T
Timo Teräs 已提交
42 43 44 45
	int				hash_count;
	u32				hash_rnd;
	int				hash_rnd_recalc;
	struct tasklet_struct		flush_tasklet;
46
};
L
Linus Torvalds 已提交
47 48

struct flow_flush_info {
49
	struct flow_cache		*cache;
T
Timo Teräs 已提交
50 51
	atomic_t			cpuleft;
	struct completion		completion;
L
Linus Torvalds 已提交
52 53
};

T
Timo Teräs 已提交
54 55
struct flow_cache {
	u32				hash_shift;
E
Eric Dumazet 已提交
56
	struct flow_cache_percpu __percpu *percpu;
T
Timo Teräs 已提交
57 58 59 60 61 62 63
	struct notifier_block		hotcpu_notifier;
	int				low_watermark;
	int				high_watermark;
	struct timer_list		rnd_timer;
};

atomic_t flow_cache_genid = ATOMIC_INIT(0);
E
Eric Dumazet 已提交
64
EXPORT_SYMBOL(flow_cache_genid);
T
Timo Teräs 已提交
65
static struct flow_cache flow_cache_global;
E
Eric Dumazet 已提交
66
static struct kmem_cache *flow_cachep __read_mostly;
T
Timo Teräs 已提交
67

68 69 70
static DEFINE_SPINLOCK(flow_cache_gc_lock);
static LIST_HEAD(flow_cache_gc_list);

T
Timo Teräs 已提交
71 72
#define flow_cache_hash_size(cache)	(1 << (cache)->hash_shift)
#define FLOW_HASH_RND_PERIOD		(10 * 60 * HZ)
L
Linus Torvalds 已提交
73 74 75

static void flow_cache_new_hashrnd(unsigned long arg)
{
T
Timo Teräs 已提交
76
	struct flow_cache *fc = (void *) arg;
L
Linus Torvalds 已提交
77 78
	int i;

79
	for_each_possible_cpu(i)
T
Timo Teräs 已提交
80
		per_cpu_ptr(fc->percpu, i)->hash_rnd_recalc = 1;
L
Linus Torvalds 已提交
81

T
Timo Teräs 已提交
82 83
	fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
	add_timer(&fc->rnd_timer);
L
Linus Torvalds 已提交
84 85
}

86 87 88 89 90 91 92 93 94
static int flow_entry_valid(struct flow_cache_entry *fle)
{
	if (atomic_read(&flow_cache_genid) != fle->genid)
		return 0;
	if (fle->object && !fle->object->ops->check(fle->object))
		return 0;
	return 1;
}

95
static void flow_entry_kill(struct flow_cache_entry *fle)
96 97
{
	if (fle->object)
98
		fle->object->ops->delete(fle->object);
99
	kmem_cache_free(flow_cachep, fle);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
}

static void flow_cache_gc_task(struct work_struct *work)
{
	struct list_head gc_list;
	struct flow_cache_entry *fce, *n;

	INIT_LIST_HEAD(&gc_list);
	spin_lock_bh(&flow_cache_gc_lock);
	list_splice_tail_init(&flow_cache_gc_list, &gc_list);
	spin_unlock_bh(&flow_cache_gc_lock);

	list_for_each_entry_safe(fce, n, &gc_list, u.gc_list)
		flow_entry_kill(fce);
}
static DECLARE_WORK(flow_cache_gc_work, flow_cache_gc_task);

static void flow_cache_queue_garbage(struct flow_cache_percpu *fcp,
				     int deleted, struct list_head *gc_list)
{
	if (deleted) {
		fcp->hash_count -= deleted;
		spin_lock_bh(&flow_cache_gc_lock);
		list_splice_tail(gc_list, &flow_cache_gc_list);
		spin_unlock_bh(&flow_cache_gc_lock);
		schedule_work(&flow_cache_gc_work);
	}
127 128
}

T
Timo Teräs 已提交
129 130 131
static void __flow_cache_shrink(struct flow_cache *fc,
				struct flow_cache_percpu *fcp,
				int shrink_to)
L
Linus Torvalds 已提交
132
{
133 134 135 136
	struct flow_cache_entry *fle;
	struct hlist_node *entry, *tmp;
	LIST_HEAD(gc_list);
	int i, deleted = 0;
L
Linus Torvalds 已提交
137

T
Timo Teräs 已提交
138
	for (i = 0; i < flow_cache_hash_size(fc); i++) {
139
		int saved = 0;
L
Linus Torvalds 已提交
140

141 142
		hlist_for_each_entry_safe(fle, entry, tmp,
					  &fcp->hash_table[i], u.hlist) {
143 144 145 146
			if (saved < shrink_to &&
			    flow_entry_valid(fle)) {
				saved++;
			} else {
147 148 149
				deleted++;
				hlist_del(&fle->u.hlist);
				list_add_tail(&fle->u.gc_list, &gc_list);
150
			}
L
Linus Torvalds 已提交
151 152
		}
	}
153 154

	flow_cache_queue_garbage(fcp, deleted, &gc_list);
L
Linus Torvalds 已提交
155 156
}

T
Timo Teräs 已提交
157 158
static void flow_cache_shrink(struct flow_cache *fc,
			      struct flow_cache_percpu *fcp)
L
Linus Torvalds 已提交
159
{
T
Timo Teräs 已提交
160
	int shrink_to = fc->low_watermark / flow_cache_hash_size(fc);
L
Linus Torvalds 已提交
161

T
Timo Teräs 已提交
162
	__flow_cache_shrink(fc, fcp, shrink_to);
L
Linus Torvalds 已提交
163 164
}

T
Timo Teräs 已提交
165 166
static void flow_new_hash_rnd(struct flow_cache *fc,
			      struct flow_cache_percpu *fcp)
L
Linus Torvalds 已提交
167
{
T
Timo Teräs 已提交
168 169 170
	get_random_bytes(&fcp->hash_rnd, sizeof(u32));
	fcp->hash_rnd_recalc = 0;
	__flow_cache_shrink(fc, fcp, 0);
L
Linus Torvalds 已提交
171 172
}

T
Timo Teräs 已提交
173 174
static u32 flow_hash_code(struct flow_cache *fc,
			  struct flow_cache_percpu *fcp,
175
			  const struct flowi *key)
L
Linus Torvalds 已提交
176
{
177
	const u32 *k = (const u32 *) key;
L
Linus Torvalds 已提交
178

E
Eric Dumazet 已提交
179 180
	return jhash2(k, (sizeof(*key) / sizeof(u32)), fcp->hash_rnd)
		& (flow_cache_hash_size(fc) - 1);
L
Linus Torvalds 已提交
181 182
}

E
Eric Dumazet 已提交
183
typedef unsigned long flow_compare_t;
L
Linus Torvalds 已提交
184 185 186 187 188

/* I hear what you're saying, use memcmp.  But memcmp cannot make
 * important assumptions that we can here, such as alignment and
 * constant size.
 */
189
static int flow_key_compare(const struct flowi *key1, const struct flowi *key2)
L
Linus Torvalds 已提交
190
{
191
	const flow_compare_t *k1, *k1_lim, *k2;
L
Linus Torvalds 已提交
192 193
	const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t);

194
	BUILD_BUG_ON(sizeof(struct flowi) % sizeof(flow_compare_t));
L
Linus Torvalds 已提交
195

196
	k1 = (const flow_compare_t *) key1;
L
Linus Torvalds 已提交
197 198
	k1_lim = k1 + n_elem;

199
	k2 = (const flow_compare_t *) key2;
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208

	do {
		if (*k1++ != *k2++)
			return 1;
	} while (k1 < k1_lim);

	return 0;
}

209
struct flow_cache_object *
210
flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
211
		  flow_resolve_t resolver, void *ctx)
L
Linus Torvalds 已提交
212
{
T
Timo Teräs 已提交
213 214
	struct flow_cache *fc = &flow_cache_global;
	struct flow_cache_percpu *fcp;
215 216
	struct flow_cache_entry *fle, *tfle;
	struct hlist_node *entry;
217
	struct flow_cache_object *flo;
L
Linus Torvalds 已提交
218 219 220
	unsigned int hash;

	local_bh_disable();
E
Eric Dumazet 已提交
221
	fcp = this_cpu_ptr(fc->percpu);
L
Linus Torvalds 已提交
222 223

	fle = NULL;
224
	flo = NULL;
L
Linus Torvalds 已提交
225 226
	/* Packet really early in init?  Making flow_cache_init a
	 * pre-smp initcall would solve this.  --RR */
T
Timo Teräs 已提交
227
	if (!fcp->hash_table)
L
Linus Torvalds 已提交
228 229
		goto nocache;

T
Timo Teräs 已提交
230 231
	if (fcp->hash_rnd_recalc)
		flow_new_hash_rnd(fc, fcp);
L
Linus Torvalds 已提交
232

233
	hash = flow_hash_code(fc, fcp, key);
234 235 236 237 238
	hlist_for_each_entry(tfle, entry, &fcp->hash_table[hash], u.hlist) {
		if (tfle->family == family &&
		    tfle->dir == dir &&
		    flow_key_compare(key, &tfle->key) == 0) {
			fle = tfle;
L
Linus Torvalds 已提交
239
			break;
240
		}
L
Linus Torvalds 已提交
241 242
	}

243
	if (unlikely(!fle)) {
T
Timo Teräs 已提交
244 245
		if (fcp->hash_count > fc->high_watermark)
			flow_cache_shrink(fc, fcp);
L
Linus Torvalds 已提交
246

247
		fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC);
L
Linus Torvalds 已提交
248 249 250 251 252
		if (fle) {
			fle->family = family;
			fle->dir = dir;
			memcpy(&fle->key, key, sizeof(*key));
			fle->object = NULL;
253
			hlist_add_head(&fle->u.hlist, &fcp->hash_table[hash]);
T
Timo Teräs 已提交
254
			fcp->hash_count++;
L
Linus Torvalds 已提交
255
		}
256 257 258 259 260 261 262 263 264 265 266
	} else if (likely(fle->genid == atomic_read(&flow_cache_genid))) {
		flo = fle->object;
		if (!flo)
			goto ret_object;
		flo = flo->ops->get(flo);
		if (flo)
			goto ret_object;
	} else if (fle->object) {
	        flo = fle->object;
	        flo->ops->delete(flo);
	        fle->object = NULL;
L
Linus Torvalds 已提交
267 268 269
	}

nocache:
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	flo = NULL;
	if (fle) {
		flo = fle->object;
		fle->object = NULL;
	}
	flo = resolver(net, key, family, dir, flo, ctx);
	if (fle) {
		fle->genid = atomic_read(&flow_cache_genid);
		if (!IS_ERR(flo))
			fle->object = flo;
		else
			fle->genid--;
	} else {
		if (flo && !IS_ERR(flo))
			flo->ops->delete(flo);
L
Linus Torvalds 已提交
285
	}
286 287 288
ret_object:
	local_bh_enable();
	return flo;
L
Linus Torvalds 已提交
289
}
E
Eric Dumazet 已提交
290
EXPORT_SYMBOL(flow_cache_lookup);
L
Linus Torvalds 已提交
291 292 293 294

static void flow_cache_flush_tasklet(unsigned long data)
{
	struct flow_flush_info *info = (void *)data;
T
Timo Teräs 已提交
295 296
	struct flow_cache *fc = info->cache;
	struct flow_cache_percpu *fcp;
297 298 299 300
	struct flow_cache_entry *fle;
	struct hlist_node *entry, *tmp;
	LIST_HEAD(gc_list);
	int i, deleted = 0;
L
Linus Torvalds 已提交
301

E
Eric Dumazet 已提交
302
	fcp = this_cpu_ptr(fc->percpu);
T
Timo Teräs 已提交
303
	for (i = 0; i < flow_cache_hash_size(fc); i++) {
304 305
		hlist_for_each_entry_safe(fle, entry, tmp,
					  &fcp->hash_table[i], u.hlist) {
306
			if (flow_entry_valid(fle))
L
Linus Torvalds 已提交
307 308
				continue;

309 310 311
			deleted++;
			hlist_del(&fle->u.hlist);
			list_add_tail(&fle->u.gc_list, &gc_list);
L
Linus Torvalds 已提交
312 313 314
		}
	}

315 316
	flow_cache_queue_garbage(fcp, deleted, &gc_list);

L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327
	if (atomic_dec_and_test(&info->cpuleft))
		complete(&info->completion);
}

static void flow_cache_flush_per_cpu(void *data)
{
	struct flow_flush_info *info = data;
	int cpu;
	struct tasklet_struct *tasklet;

	cpu = smp_processor_id();
T
Timo Teräs 已提交
328
	tasklet = &per_cpu_ptr(info->cache->percpu, cpu)->flush_tasklet;
L
Linus Torvalds 已提交
329 330 331 332 333 334 335
	tasklet->data = (unsigned long)info;
	tasklet_schedule(tasklet);
}

void flow_cache_flush(void)
{
	struct flow_flush_info info;
A
Arjan van de Ven 已提交
336
	static DEFINE_MUTEX(flow_flush_sem);
L
Linus Torvalds 已提交
337 338

	/* Don't want cpus going down or up during this. */
339
	get_online_cpus();
A
Arjan van de Ven 已提交
340
	mutex_lock(&flow_flush_sem);
T
Timo Teräs 已提交
341
	info.cache = &flow_cache_global;
L
Linus Torvalds 已提交
342 343 344 345
	atomic_set(&info.cpuleft, num_online_cpus());
	init_completion(&info.completion);

	local_bh_disable();
346
	smp_call_function(flow_cache_flush_per_cpu, &info, 0);
L
Linus Torvalds 已提交
347 348 349 350
	flow_cache_flush_tasklet((unsigned long)&info);
	local_bh_enable();

	wait_for_completion(&info.completion);
A
Arjan van de Ven 已提交
351
	mutex_unlock(&flow_flush_sem);
352
	put_online_cpus();
L
Linus Torvalds 已提交
353 354
}

E
Eric Dumazet 已提交
355
static int __cpuinit flow_cache_cpu_prepare(struct flow_cache *fc, int cpu)
L
Linus Torvalds 已提交
356
{
E
Eric Dumazet 已提交
357 358
	struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);
	size_t sz = sizeof(struct hlist_head) * flow_cache_hash_size(fc);
T
Timo Teräs 已提交
359

E
Eric Dumazet 已提交
360 361 362 363 364 365 366 367 368 369 370
	if (!fcp->hash_table) {
		fcp->hash_table = kzalloc_node(sz, GFP_KERNEL, cpu_to_node(cpu));
		if (!fcp->hash_table) {
			pr_err("NET: failed to allocate flow cache sz %zu\n", sz);
			return -ENOMEM;
		}
		fcp->hash_rnd_recalc = 1;
		fcp->hash_count = 0;
		tasklet_init(&fcp->flush_tasklet, flow_cache_flush_tasklet, 0);
	}
	return 0;
L
Linus Torvalds 已提交
371 372
}

E
Eric Dumazet 已提交
373
static int __cpuinit flow_cache_cpu(struct notifier_block *nfb,
L
Linus Torvalds 已提交
374 375 376
			  unsigned long action,
			  void *hcpu)
{
T
Timo Teräs 已提交
377
	struct flow_cache *fc = container_of(nfb, struct flow_cache, hotcpu_notifier);
E
Eric Dumazet 已提交
378
	int res, cpu = (unsigned long) hcpu;
T
Timo Teräs 已提交
379 380
	struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);

E
Eric Dumazet 已提交
381 382 383 384 385 386 387 388 389
	switch (action) {
	case CPU_UP_PREPARE:
	case CPU_UP_PREPARE_FROZEN:
		res = flow_cache_cpu_prepare(fc, cpu);
		if (res)
			return notifier_from_errno(res);
		break;
	case CPU_DEAD:
	case CPU_DEAD_FROZEN:
T
Timo Teräs 已提交
390
		__flow_cache_shrink(fc, fcp, 0);
E
Eric Dumazet 已提交
391 392
		break;
	}
L
Linus Torvalds 已提交
393 394 395
	return NOTIFY_OK;
}

E
Eric Dumazet 已提交
396
static int __init flow_cache_init(struct flow_cache *fc)
L
Linus Torvalds 已提交
397 398 399
{
	int i;

T
Timo Teräs 已提交
400 401 402 403 404
	fc->hash_shift = 10;
	fc->low_watermark = 2 * flow_cache_hash_size(fc);
	fc->high_watermark = 4 * flow_cache_hash_size(fc);

	fc->percpu = alloc_percpu(struct flow_cache_percpu);
E
Eric Dumazet 已提交
405 406
	if (!fc->percpu)
		return -ENOMEM;
L
Linus Torvalds 已提交
407

E
Eric Dumazet 已提交
408 409 410 411
	for_each_online_cpu(i) {
		if (flow_cache_cpu_prepare(fc, i))
			return -ENOMEM;
	}
T
Timo Teräs 已提交
412 413 414 415
	fc->hotcpu_notifier = (struct notifier_block){
		.notifier_call = flow_cache_cpu,
	};
	register_hotcpu_notifier(&fc->hotcpu_notifier);
L
Linus Torvalds 已提交
416

E
Eric Dumazet 已提交
417 418 419 420 421
	setup_timer(&fc->rnd_timer, flow_cache_new_hashrnd,
		    (unsigned long) fc);
	fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
	add_timer(&fc->rnd_timer);

L
Linus Torvalds 已提交
422 423 424
	return 0;
}

T
Timo Teräs 已提交
425 426 427 428 429 430 431 432 433 434
static int __init flow_cache_init_global(void)
{
	flow_cachep = kmem_cache_create("flow_cache",
					sizeof(struct flow_cache_entry),
					0, SLAB_PANIC, NULL);

	return flow_cache_init(&flow_cache_global);
}

module_init(flow_cache_init_global);