Skip to contentSkip to conversation

: nf_conntrack: round up hashsize to next multiple of PAGE_SIZE

NETFILTER Patrick McHardy 1 participant 2 messages Jun 26, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I've added these four patches to my tree to improve the conntrack hashing behaviour and use a hash for expectations instead of the global list.

[NETFILTER]: nf_conntrack: round up hashsize to next multiple of PAGE_SIZE

Besides letting the rest of the page go to waste, this has the nice side-effect that it assures that the hash size is a power of two, so we can replace the modulo operation during hashing by a binary and.

---
commit 1b54b00d69fd371821949bf429642dfc98153bcb
tree 83853e7e8d280c88ffbb7eab0a794d1ac0624045
parent 07d16ab092ef4fb4b29037573a28bbfd9e2a6e4b
author Patrick McHardy <[email protected]> Tue, 26 Jun 2007 15:42:28 +0200 committer Patrick McHardy <[email protected]> Tue, 26 Jun 2007 15:42:28 +0200

 net/netfilter/nf_conntrack_core.c |   24 +++++++++++++-----------
 1 files changed, 13 insertions(+), 11 deletions(-)
net/netfilter/nf_conntrack_core.c +13 -11
index 035eb9f..ee9f825 100644
@@ -87,7 +87,7 @@ static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
 	b = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
 		   (tuple->src.u.all << 16) | tuple->dst.u.all);
 
-	return jhash_2words(a, b, rnd) % size;
+	return jhash_2words(a, b, rnd) & size;
 }
 
 static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
@@ -965,12 +965,14 @@ void nf_conntrack_cleanup(void)
 	nf_conntrack_helper_fini();
 }
 
-static struct list_head *alloc_hashtable(int size, int *vmalloced)
+static struct list_head *alloc_hashtable(int *sizep, int *vmalloced)
 {
 	struct list_head *hash;
-	unsigned int i;
+	unsigned int size, i;
 
 	*vmalloced = 0;
+
+	size = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct list_head));
 	hash = (void*)__get_free_pages(GFP_KERNEL,
 				       get_order(sizeof(struct list_head)
 						 * size));
@@ -1003,7 +1005,7 @@ int set_hashsize(const char *val, struct kernel_param *kp)
 	if (!hashsize)
 		return -EINVAL;
 
-	hash = alloc_hashtable(hashsize, &vmalloced);
+	hash = alloc_hashtable(&hashsize, &vmalloced);
 	if (!hash)
 		return -ENOMEM;
 
@@ -1053,19 +1055,19 @@ int __init nf_conntrack_init(void)
 		if (nf_conntrack_htable_size < 16)
 			nf_conntrack_htable_size = 16;
 	}
-	nf_conntrack_max = 8 * nf_conntrack_htable_size;
-
-	printk("nf_conntrack version %s (%u buckets, %d max)\n",
-	       NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
-	       nf_conntrack_max);
-
-	nf_conntrack_hash = alloc_hashtable(nf_conntrack_htable_size,
+	nf_conntrack_hash = alloc_hashtable(&nf_conntrack_htable_size,
 					    &nf_conntrack_vmalloc);
 	if (!nf_conntrack_hash) {
 		printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
 		goto err_out;
 	}
 
+	nf_conntrack_max = 8 * nf_conntrack_htable_size;
+
+	printk("nf_conntrack version %s (%u buckets, %d max)\n",
+	       NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
+	       nf_conntrack_max);
+
 	nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
 						sizeof(struct nf_conn),
 						0, 0, NULL, NULL);
#1 lore raw
Quoted 5 lines · Patrick McHardy

I've added these four patches to my tree to improve the conntrack hashing behaviour and use a hash for expectations instead of the global list.

+ return jhash_2words(a, b, rnd) & size;

And now fixed this obvious stupidity :)