2007-10-15 09:31:52 +00:00
|
|
|
/*
|
|
|
|
* inet fragments management
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Pavel Emelyanov <xemul@openvz.org>
|
|
|
|
* Started as consolidation of ipv4/ip_fragment.c,
|
|
|
|
* ipv6/reassembly. and ipv6 nf conntrack reassembly
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/mm.h>
|
2007-10-15 09:38:08 +00:00
|
|
|
#include <linux/random.h>
|
2007-10-15 09:39:14 +00:00
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2007-10-15 09:31:52 +00:00
|
|
|
|
|
|
|
#include <net/inet_frag.h>
|
|
|
|
|
2007-10-15 09:38:08 +00:00
|
|
|
static void inet_frag_secret_rebuild(unsigned long dummy)
|
|
|
|
{
|
|
|
|
struct inet_frags *f = (struct inet_frags *)dummy;
|
|
|
|
unsigned long now = jiffies;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
write_lock(&f->lock);
|
|
|
|
get_random_bytes(&f->rnd, sizeof(u32));
|
|
|
|
for (i = 0; i < INETFRAGS_HASHSZ; i++) {
|
|
|
|
struct inet_frag_queue *q;
|
|
|
|
struct hlist_node *p, *n;
|
|
|
|
|
|
|
|
hlist_for_each_entry_safe(q, p, n, &f->hash[i], list) {
|
|
|
|
unsigned int hval = f->hashfn(q);
|
|
|
|
|
|
|
|
if (hval != i) {
|
|
|
|
hlist_del(&q->list);
|
|
|
|
|
|
|
|
/* Relink to new hash chain. */
|
|
|
|
hlist_add_head(&q->list, &f->hash[hval]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write_unlock(&f->lock);
|
|
|
|
|
2008-01-22 14:11:04 +00:00
|
|
|
mod_timer(&f->secret_timer, now + f->secret_interval);
|
2007-10-15 09:38:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-15 09:31:52 +00:00
|
|
|
void inet_frags_init(struct inet_frags *f)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < INETFRAGS_HASHSZ; i++)
|
|
|
|
INIT_HLIST_HEAD(&f->hash[i]);
|
|
|
|
|
|
|
|
rwlock_init(&f->lock);
|
|
|
|
|
|
|
|
f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
|
|
|
|
(jiffies ^ (jiffies >> 6)));
|
|
|
|
|
2008-01-24 05:20:07 +00:00
|
|
|
setup_timer(&f->secret_timer, inet_frag_secret_rebuild,
|
|
|
|
(unsigned long)f);
|
2008-01-22 14:11:04 +00:00
|
|
|
f->secret_timer.expires = jiffies + f->secret_interval;
|
2007-10-15 09:38:08 +00:00
|
|
|
add_timer(&f->secret_timer);
|
2007-10-15 09:31:52 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frags_init);
|
|
|
|
|
2008-01-22 14:06:23 +00:00
|
|
|
void inet_frags_init_net(struct netns_frags *nf)
|
|
|
|
{
|
|
|
|
nf->nqueues = 0;
|
2013-01-28 23:45:12 +00:00
|
|
|
init_frag_mem_limit(nf);
|
2008-01-22 14:11:48 +00:00
|
|
|
INIT_LIST_HEAD(&nf->lru_list);
|
2013-01-28 23:45:51 +00:00
|
|
|
spin_lock_init(&nf->lru_lock);
|
2008-01-22 14:06:23 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frags_init_net);
|
|
|
|
|
2007-10-15 09:31:52 +00:00
|
|
|
void inet_frags_fini(struct inet_frags *f)
|
|
|
|
{
|
2007-10-15 09:38:08 +00:00
|
|
|
del_timer(&f->secret_timer);
|
2007-10-15 09:31:52 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frags_fini);
|
2007-10-15 09:37:18 +00:00
|
|
|
|
2008-01-22 14:12:39 +00:00
|
|
|
void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f)
|
|
|
|
{
|
|
|
|
nf->low_thresh = 0;
|
2008-03-29 00:30:18 +00:00
|
|
|
|
|
|
|
local_bh_disable();
|
2012-09-18 16:50:11 +00:00
|
|
|
inet_frag_evictor(nf, f, true);
|
2008-03-29 00:30:18 +00:00
|
|
|
local_bh_enable();
|
net: use lib/percpu_counter API for fragmentation mem accounting
Replace the per network namespace shared atomic "mem" accounting
variable, in the fragmentation code, with a lib/percpu_counter.
Getting percpu_counter to scale to the fragmentation code usage
requires some tweaks.
At first view, percpu_counter looks superfast, but it does not
scale on multi-CPU/NUMA machines, because the default batch size
is too small, for frag code usage. Thus, I have adjusted the
batch size by using __percpu_counter_add() directly, instead of
percpu_counter_sub() and percpu_counter_add().
The batch size is increased to 130.000, based on the largest 64K
fragment memory usage. This does introduce some imprecise
memory accounting, but its does not need to be strict for this
use-case.
It is also essential, that the percpu_counter, does not
share cacheline with other writers, to make this scale.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2013-01-28 23:45:33 +00:00
|
|
|
|
|
|
|
percpu_counter_destroy(&nf->mem);
|
2008-01-22 14:12:39 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frags_exit_net);
|
|
|
|
|
2007-10-15 09:37:18 +00:00
|
|
|
static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
|
|
|
|
{
|
|
|
|
write_lock(&f->lock);
|
|
|
|
hlist_del(&fq->list);
|
2008-01-22 14:06:23 +00:00
|
|
|
fq->net->nqueues--;
|
2007-10-15 09:37:18 +00:00
|
|
|
write_unlock(&f->lock);
|
2013-01-28 23:45:51 +00:00
|
|
|
inet_frag_lru_del(fq);
|
2007-10-15 09:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
|
|
|
|
{
|
|
|
|
if (del_timer(&fq->timer))
|
|
|
|
atomic_dec(&fq->refcnt);
|
|
|
|
|
2008-03-28 23:35:27 +00:00
|
|
|
if (!(fq->last_in & INET_FRAG_COMPLETE)) {
|
2007-10-15 09:37:18 +00:00
|
|
|
fq_unlink(fq, f);
|
|
|
|
atomic_dec(&fq->refcnt);
|
2008-03-28 23:35:27 +00:00
|
|
|
fq->last_in |= INET_FRAG_COMPLETE;
|
2007-10-15 09:37:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frag_kill);
|
2007-10-15 09:39:14 +00:00
|
|
|
|
2008-01-22 14:07:25 +00:00
|
|
|
static inline void frag_kfree_skb(struct netns_frags *nf, struct inet_frags *f,
|
2013-01-28 23:45:12 +00:00
|
|
|
struct sk_buff *skb)
|
2007-10-15 09:39:14 +00:00
|
|
|
{
|
|
|
|
if (f->skb_free)
|
|
|
|
f->skb_free(skb);
|
|
|
|
kfree_skb(skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f,
|
|
|
|
int *work)
|
|
|
|
{
|
|
|
|
struct sk_buff *fp;
|
2008-01-22 14:07:25 +00:00
|
|
|
struct netns_frags *nf;
|
2013-01-28 23:45:12 +00:00
|
|
|
unsigned int sum, sum_truesize = 0;
|
2007-10-15 09:39:14 +00:00
|
|
|
|
2008-07-26 04:43:18 +00:00
|
|
|
WARN_ON(!(q->last_in & INET_FRAG_COMPLETE));
|
|
|
|
WARN_ON(del_timer(&q->timer) != 0);
|
2007-10-15 09:39:14 +00:00
|
|
|
|
|
|
|
/* Release all fragment data. */
|
|
|
|
fp = q->fragments;
|
2008-01-22 14:07:25 +00:00
|
|
|
nf = q->net;
|
2007-10-15 09:39:14 +00:00
|
|
|
while (fp) {
|
|
|
|
struct sk_buff *xp = fp->next;
|
|
|
|
|
2013-01-28 23:45:12 +00:00
|
|
|
sum_truesize += fp->truesize;
|
|
|
|
frag_kfree_skb(nf, f, fp);
|
2007-10-15 09:39:14 +00:00
|
|
|
fp = xp;
|
|
|
|
}
|
2013-01-28 23:45:12 +00:00
|
|
|
sum = sum_truesize + f->qsize;
|
2007-10-15 09:39:14 +00:00
|
|
|
if (work)
|
2013-01-28 23:45:12 +00:00
|
|
|
*work -= sum;
|
|
|
|
sub_frag_mem_limit(q, sum);
|
2007-10-15 09:39:14 +00:00
|
|
|
|
2007-10-18 02:48:26 +00:00
|
|
|
if (f->destructor)
|
|
|
|
f->destructor(q);
|
|
|
|
kfree(q);
|
2007-10-15 09:39:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frag_destroy);
|
2007-10-15 09:40:06 +00:00
|
|
|
|
2012-09-18 16:50:11 +00:00
|
|
|
int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force)
|
2007-10-15 09:40:06 +00:00
|
|
|
{
|
|
|
|
struct inet_frag_queue *q;
|
|
|
|
int work, evicted = 0;
|
|
|
|
|
2012-09-18 16:50:11 +00:00
|
|
|
if (!force) {
|
2013-01-28 23:45:12 +00:00
|
|
|
if (frag_mem_limit(nf) <= nf->high_thresh)
|
2012-09-18 16:50:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-28 23:45:12 +00:00
|
|
|
work = frag_mem_limit(nf) - nf->low_thresh;
|
2007-10-15 09:40:06 +00:00
|
|
|
while (work > 0) {
|
2013-01-28 23:45:51 +00:00
|
|
|
spin_lock(&nf->lru_lock);
|
|
|
|
|
2008-01-22 14:11:48 +00:00
|
|
|
if (list_empty(&nf->lru_list)) {
|
2013-01-28 23:45:51 +00:00
|
|
|
spin_unlock(&nf->lru_lock);
|
2007-10-15 09:40:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-22 14:11:48 +00:00
|
|
|
q = list_first_entry(&nf->lru_list,
|
2007-10-15 09:40:06 +00:00
|
|
|
struct inet_frag_queue, lru_list);
|
|
|
|
atomic_inc(&q->refcnt);
|
2013-01-28 23:45:51 +00:00
|
|
|
spin_unlock(&nf->lru_lock);
|
2007-10-15 09:40:06 +00:00
|
|
|
|
|
|
|
spin_lock(&q->lock);
|
2008-03-28 23:35:27 +00:00
|
|
|
if (!(q->last_in & INET_FRAG_COMPLETE))
|
2007-10-15 09:40:06 +00:00
|
|
|
inet_frag_kill(q, f);
|
|
|
|
spin_unlock(&q->lock);
|
|
|
|
|
|
|
|
if (atomic_dec_and_test(&q->refcnt))
|
|
|
|
inet_frag_destroy(q, f, &work);
|
|
|
|
evicted++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return evicted;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frag_evictor);
|
2007-10-18 02:44:34 +00:00
|
|
|
|
2008-01-22 14:02:14 +00:00
|
|
|
static struct inet_frag_queue *inet_frag_intern(struct netns_frags *nf,
|
|
|
|
struct inet_frag_queue *qp_in, struct inet_frags *f,
|
2008-06-28 03:06:08 +00:00
|
|
|
void *arg)
|
2007-10-18 02:44:34 +00:00
|
|
|
{
|
|
|
|
struct inet_frag_queue *qp;
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
struct hlist_node *n;
|
|
|
|
#endif
|
2008-06-28 03:06:08 +00:00
|
|
|
unsigned int hash;
|
2007-10-18 02:44:34 +00:00
|
|
|
|
|
|
|
write_lock(&f->lock);
|
2008-06-28 03:06:08 +00:00
|
|
|
/*
|
|
|
|
* While we stayed w/o the lock other CPU could update
|
|
|
|
* the rnd seed, so we need to re-calculate the hash
|
|
|
|
* chain. Fortunatelly the qp_in can be used to get one.
|
|
|
|
*/
|
|
|
|
hash = f->hashfn(qp_in);
|
2007-10-18 02:44:34 +00:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* With SMP race we have to recheck hash table, because
|
|
|
|
* such entry could be created on other cpu, while we
|
|
|
|
* promoted read lock to write lock.
|
|
|
|
*/
|
|
|
|
hlist_for_each_entry(qp, n, &f->hash[hash], list) {
|
2008-01-22 14:02:14 +00:00
|
|
|
if (qp->net == nf && f->match(qp, arg)) {
|
2007-10-18 02:44:34 +00:00
|
|
|
atomic_inc(&qp->refcnt);
|
|
|
|
write_unlock(&f->lock);
|
2008-03-28 23:35:27 +00:00
|
|
|
qp_in->last_in |= INET_FRAG_COMPLETE;
|
2007-10-18 02:44:34 +00:00
|
|
|
inet_frag_put(qp_in, f);
|
|
|
|
return qp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
qp = qp_in;
|
2008-01-22 14:09:37 +00:00
|
|
|
if (!mod_timer(&qp->timer, jiffies + nf->timeout))
|
2007-10-18 02:44:34 +00:00
|
|
|
atomic_inc(&qp->refcnt);
|
|
|
|
|
|
|
|
atomic_inc(&qp->refcnt);
|
|
|
|
hlist_add_head(&qp->list, &f->hash[hash]);
|
2008-01-22 14:06:23 +00:00
|
|
|
nf->nqueues++;
|
2007-10-18 02:44:34 +00:00
|
|
|
write_unlock(&f->lock);
|
2013-01-28 23:45:51 +00:00
|
|
|
inet_frag_lru_add(nf, qp);
|
2007-10-18 02:44:34 +00:00
|
|
|
return qp;
|
|
|
|
}
|
2007-10-18 02:45:23 +00:00
|
|
|
|
2008-01-22 14:02:14 +00:00
|
|
|
static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
|
|
|
|
struct inet_frags *f, void *arg)
|
2007-10-18 02:45:23 +00:00
|
|
|
{
|
|
|
|
struct inet_frag_queue *q;
|
|
|
|
|
|
|
|
q = kzalloc(f->qsize, GFP_ATOMIC);
|
|
|
|
if (q == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2012-06-08 01:21:40 +00:00
|
|
|
q->net = nf;
|
2007-10-18 02:46:47 +00:00
|
|
|
f->constructor(q, arg);
|
2013-01-28 23:45:12 +00:00
|
|
|
add_frag_mem_limit(q, f->qsize);
|
|
|
|
|
2007-10-18 02:45:23 +00:00
|
|
|
setup_timer(&q->timer, f->frag_expire, (unsigned long)q);
|
|
|
|
spin_lock_init(&q->lock);
|
|
|
|
atomic_set(&q->refcnt, 1);
|
|
|
|
|
|
|
|
return q;
|
|
|
|
}
|
2007-10-18 02:46:47 +00:00
|
|
|
|
2008-01-22 14:02:14 +00:00
|
|
|
static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
|
2008-06-28 03:06:08 +00:00
|
|
|
struct inet_frags *f, void *arg)
|
2007-10-18 02:46:47 +00:00
|
|
|
{
|
|
|
|
struct inet_frag_queue *q;
|
|
|
|
|
2008-01-22 14:02:14 +00:00
|
|
|
q = inet_frag_alloc(nf, f, arg);
|
2007-10-18 02:46:47 +00:00
|
|
|
if (q == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2008-06-28 03:06:08 +00:00
|
|
|
return inet_frag_intern(nf, q, f, arg);
|
2007-10-18 02:46:47 +00:00
|
|
|
}
|
2007-10-18 02:47:21 +00:00
|
|
|
|
2008-01-22 14:02:14 +00:00
|
|
|
struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
|
|
|
|
struct inet_frags *f, void *key, unsigned int hash)
|
2009-02-25 10:32:52 +00:00
|
|
|
__releases(&f->lock)
|
2007-10-18 02:47:21 +00:00
|
|
|
{
|
|
|
|
struct inet_frag_queue *q;
|
|
|
|
struct hlist_node *n;
|
|
|
|
|
|
|
|
hlist_for_each_entry(q, n, &f->hash[hash], list) {
|
2008-01-22 14:02:14 +00:00
|
|
|
if (q->net == nf && f->match(q, key)) {
|
2007-10-18 02:47:21 +00:00
|
|
|
atomic_inc(&q->refcnt);
|
|
|
|
read_unlock(&f->lock);
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
read_unlock(&f->lock);
|
|
|
|
|
2008-06-28 03:06:08 +00:00
|
|
|
return inet_frag_create(nf, f, key);
|
2007-10-18 02:47:21 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(inet_frag_find);
|