public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: Patrick McHardy <kaber@trash.net>
Cc: netdev@vger.kernel.org, netfilter-devel@vger.kernel.org,
	containers@lists.linux-foundation.org
Subject: [PATCH v2 7/6] netns ct: final netns tweaks
Date: Sat, 27 Sep 2008 03:59:01 +0400	[thread overview]
Message-ID: <20080926235901.GA10489@x200.localdomain> (raw)
In-Reply-To: <20080913105509.GG7440@x200.localdomain>

Add init_net checks to not remove kmem_caches twice and so on.

Refactor functions to split code which should be executed only for
init_net into one place.

ip_ct_attach and ip_ct_destroy assignments remain separate, because
they're separate stages in setup and teardown.

NOTE: NOTRACK code is in for-every-net part. It will be made per-netns
after we decidce how to do it correctly.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 net/netfilter/nf_conntrack_core.c   |  151 +++++++++++++++++++++++-------------
 net/netfilter/nf_conntrack_expect.c |   26 +++---
 2 files changed, 114 insertions(+), 63 deletions(-)

--- a/net/netfilter/nf_conntrack_core.c
+++ b/net/netfilter/nf_conntrack_core.c
@@ -1010,17 +1010,15 @@ void nf_conntrack_flush(struct net *net)
 }
 EXPORT_SYMBOL_GPL(nf_conntrack_flush);
 
-/* Mishearing the voices in his head, our hero wonders how he's
-   supposed to kill the mall. */
-void nf_conntrack_cleanup(struct net *net)
+static void nf_conntrack_cleanup_init_net(void)
 {
-	rcu_assign_pointer(ip_ct_attach, NULL);
-
-	/* This makes sure all current packets have passed through
-	   netfilter framework.  Roll on, two-stage module
-	   delete... */
-	synchronize_net();
+	nf_conntrack_helper_fini();
+	nf_conntrack_proto_fini();
+	kmem_cache_destroy(nf_conntrack_cachep);
+}
 
+static void nf_conntrack_cleanup_net(struct net *net)
+{
 	nf_ct_event_cache_flush(net);
 	nf_conntrack_ecache_fini(net);
  i_see_dead_people:
@@ -1033,17 +1031,31 @@ void nf_conntrack_cleanup(struct net *net)
 	while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
 		schedule();
 
-	rcu_assign_pointer(nf_ct_destroy, NULL);
-
-	kmem_cache_destroy(nf_conntrack_cachep);
 	nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
 			     nf_conntrack_htable_size);
-
 	nf_conntrack_acct_fini(net);
 	nf_conntrack_expect_fini(net);
 	free_percpu(net->ct.stat);
-	nf_conntrack_helper_fini();
-	nf_conntrack_proto_fini();
+}
+
+/* Mishearing the voices in his head, our hero wonders how he's
+   supposed to kill the mall. */
+void nf_conntrack_cleanup(struct net *net)
+{
+	if (net_eq(net, &init_net))
+		rcu_assign_pointer(ip_ct_attach, NULL);
+
+	/* This makes sure all current packets have passed through
+	   netfilter framework.  Roll on, two-stage module
+	   delete... */
+	synchronize_net();
+
+	nf_conntrack_cleanup_net(net);
+
+	if (net_eq(net, &init_net)) {
+		rcu_assign_pointer(nf_ct_destroy, NULL);
+		nf_conntrack_cleanup_init_net();
+	}
 }
 
 struct hlist_head *nf_ct_alloc_hashtable(unsigned int *sizep, int *vmalloced)
@@ -1128,7 +1140,7 @@ EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
 		  &nf_conntrack_htable_size, 0600);
 
-int nf_conntrack_init(struct net *net)
+static int nf_conntrack_init_init_net(void)
 {
 	int max_factor = 8;
 	int ret;
@@ -1150,21 +1162,6 @@ int nf_conntrack_init(struct net *net)
 		 * entries. */
 		max_factor = 4;
 	}
-	atomic_set(&net->ct.count, 0);
-	net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
-	if (!net->ct.stat)
-		goto err_stat;
-	ret = nf_conntrack_ecache_init(net);
-	if (ret < 0)
-		goto err_ecache;
-	net->ct.hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
-						  &net->ct.hash_vmalloc);
-	if (!net->ct.hash) {
-		printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
-		goto err_hash;
-	}
-	INIT_HLIST_HEAD(&net->ct.unconfirmed);
-
 	nf_conntrack_max = max_factor * nf_conntrack_htable_size;
 
 	printk("nf_conntrack version %s (%u buckets, %d max)\n",
@@ -1176,28 +1173,55 @@ int nf_conntrack_init(struct net *net)
 						0, 0, NULL);
 	if (!nf_conntrack_cachep) {
 		printk(KERN_ERR "Unable to create nf_conn slab cache\n");
-		goto err_free_hash;
+		ret = -ENOMEM;
+		goto err_cache;
 	}
 
 	ret = nf_conntrack_proto_init();
 	if (ret < 0)
-		goto err_free_conntrack_slab;
-
-	ret = nf_conntrack_expect_init(net);
-	if (ret < 0)
-		goto out_fini_proto;
+		goto err_proto;
 
 	ret = nf_conntrack_helper_init();
 	if (ret < 0)
-		goto out_fini_expect;
+		goto err_helper;
+
+	return 0;
+
+err_helper:
+	nf_conntrack_proto_fini();
+err_proto:
+	kmem_cache_destroy(nf_conntrack_cachep);
+err_cache:
+	return ret;
+}
+
+static int nf_conntrack_init_net(struct net *net)
+{
+	int ret;
 
+	atomic_set(&net->ct.count, 0);
+	INIT_HLIST_HEAD(&net->ct.unconfirmed);
+	net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
+	if (!net->ct.stat) {
+		ret = -ENOMEM;
+		goto err_stat;
+	}
+	ret = nf_conntrack_ecache_init(net);
+	if (ret < 0)
+		goto err_ecache;
+	net->ct.hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
+						  &net->ct.hash_vmalloc);
+	if (!net->ct.hash) {
+		ret = -ENOMEM;
+		printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
+		goto err_hash;
+	}
+	ret = nf_conntrack_expect_init(net);
+	if (ret < 0)
+		goto err_expect;
 	ret = nf_conntrack_acct_init(net);
 	if (ret < 0)
-		goto out_fini_helper;
-
-	/* For use by REJECT target */
-	rcu_assign_pointer(ip_ct_attach, nf_conntrack_attach);
-	rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
+		goto err_acct;
 
 	/* Set up fake conntrack:
 	    - to never be deleted, not in any hashes */
@@ -1208,17 +1232,11 @@ int nf_conntrack_init(struct net *net)
 	/*  - and look it like as a confirmed connection */
 	set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
 
-	return ret;
+	return 0;
 
-out_fini_helper:
-	nf_conntrack_helper_fini();
-out_fini_expect:
+err_acct:
 	nf_conntrack_expect_fini(net);
-out_fini_proto:
-	nf_conntrack_proto_fini();
-err_free_conntrack_slab:
-	kmem_cache_destroy(nf_conntrack_cachep);
-err_free_hash:
+err_expect:
 	nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
 			     nf_conntrack_htable_size);
 err_hash:
@@ -1226,5 +1244,32 @@ err_hash:
 err_ecache:
 	free_percpu(net->ct.stat);
 err_stat:
-	return -ENOMEM;
+	return ret;
+}
+
+int nf_conntrack_init(struct net *net)
+{
+	int ret;
+
+	if (net_eq(net, &init_net)) {
+		ret = nf_conntrack_init_init_net();
+		if (ret < 0)
+			goto out_init_net;
+	}
+	ret = nf_conntrack_init_net(net);
+	if (ret < 0)
+		goto out_net;
+
+	if (net_eq(net, &init_net)) {
+		/* For use by REJECT target */
+		rcu_assign_pointer(ip_ct_attach, nf_conntrack_attach);
+		rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
+	}
+	return 0;
+
+out_net:
+	if (net_eq(net, &init_net))
+		nf_conntrack_cleanup_init_net();
+out_init_net:
+	return ret;
 }
--- a/net/netfilter/nf_conntrack_expect.c
+++ b/net/netfilter/nf_conntrack_expect.c
@@ -563,12 +563,14 @@ int nf_conntrack_expect_init(struct net *net)
 {
 	int err = -ENOMEM;
 
-	if (!nf_ct_expect_hsize) {
-		nf_ct_expect_hsize = nf_conntrack_htable_size / 256;
-		if (!nf_ct_expect_hsize)
-			nf_ct_expect_hsize = 1;
+	if (net_eq(net, &init_net)) {
+		if (!nf_ct_expect_hsize) {
+			nf_ct_expect_hsize = nf_conntrack_htable_size / 256;
+			if (!nf_ct_expect_hsize)
+				nf_ct_expect_hsize = 1;
+		}
+		nf_ct_expect_max = nf_ct_expect_hsize * 4;
 	}
-	nf_ct_expect_max = nf_ct_expect_hsize * 4;
 
 	net->ct.expect_count = 0;
 	net->ct.expect_hash = nf_ct_alloc_hashtable(&nf_ct_expect_hsize,
@@ -576,11 +578,13 @@ int nf_conntrack_expect_init(struct net *net)
 	if (net->ct.expect_hash == NULL)
 		goto err1;
 
-	nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect",
+	if (net_eq(net, &init_net)) {
+		nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect",
 					sizeof(struct nf_conntrack_expect),
 					0, 0, NULL);
-	if (!nf_ct_expect_cachep)
-		goto err2;
+		if (!nf_ct_expect_cachep)
+			goto err2;
+	}
 
 	err = exp_proc_init(net);
 	if (err < 0)
@@ -589,7 +593,8 @@ int nf_conntrack_expect_init(struct net *net)
 	return 0;
 
 err3:
-	kmem_cache_destroy(nf_ct_expect_cachep);
+	if (net_eq(net, &init_net))
+		kmem_cache_destroy(nf_ct_expect_cachep);
 err2:
 	nf_ct_free_hashtable(net->ct.expect_hash, net->ct.expect_vmalloc,
 			     nf_ct_expect_hsize);
@@ -600,7 +605,8 @@ err1:
 void nf_conntrack_expect_fini(struct net *net)
 {
 	exp_proc_remove(net);
-	kmem_cache_destroy(nf_ct_expect_cachep);
+	if (net_eq(net, &init_net))
+		kmem_cache_destroy(nf_ct_expect_cachep);
 	nf_ct_free_hashtable(net->ct.expect_hash, net->ct.expect_vmalloc,
 			     nf_ct_expect_hsize);
 }

  reply	other threads:[~2008-09-26 23:59 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-21 22:00 [PATCH 10/38] netns ct: per-netns expectations adobriyan
2008-09-04 16:43 ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 01/33] nf_conntrack_sip: de-static helper pointers Alexey Dobriyan
2008-09-08  3:02   ` [PATCH 02/33] nf_conntrack_gre: more locking around keymap list Alexey Dobriyan
2008-09-08  3:02   ` [PATCH 03/33] nf_conntrack_gre: nf_ct_gre_keymap_flush() fixlet Alexey Dobriyan
2008-09-09  5:39     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 04/33] Fix {ip,6}_route_me_harder() in netns Alexey Dobriyan
2008-09-09  5:44     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 05/33] netns ct: per-netns expectations Alexey Dobriyan
2008-09-09  5:49     ` Patrick McHardy
2008-09-09  7:07       ` Alexey Dobriyan
2008-09-09  7:10         ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 06/33] netns ct: per-netns unconfirmed list Alexey Dobriyan
2008-09-09  5:50     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 07/33] netns ct: pass netns pointer to nf_conntrack_in() Alexey Dobriyan
2008-09-09  5:52     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 08/33] netns ct: pass netns pointer to L4 protocol's ->error hook Alexey Dobriyan
2008-09-09  5:54     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 09/33] netns ct: per-netns /proc/net/nf_conntrack, /proc/net/stat/nf_conntrack Alexey Dobriyan
2008-09-09  5:56     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 10/33] netns ct: per-netns /proc/net/nf_conntrack_expect Alexey Dobriyan
2008-09-09  5:57     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 11/33] netns ct: per-netns /proc/net/ip_conntrack, /proc/net/stat/ip_conntrack, /proc/net/ip_conntrack_expect Alexey Dobriyan
2008-09-09  5:59     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 12/33] netns ct: export netns list Alexey Dobriyan
2008-09-09  5:59     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 13/33] netns ct: unregister helper in every netns Alexey Dobriyan
2008-09-09  6:01     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 14/33] netns ct: cleanup after L3 and L4 proto unregister " Alexey Dobriyan
2008-09-09  6:03     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 15/33] netns ct: pass conntrack to nf_conntrack_event_cache() not skb Alexey Dobriyan
2008-09-09  6:04     ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 16/33] netns ct: per-netns event cache Alexey Dobriyan
     [not found]     ` <1220842990-30500-16-git-send-email-adobriyan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2008-09-09  6:12       ` Patrick McHardy
2008-09-09  7:07         ` Alexey Dobriyan
2008-09-09  7:07           ` Patrick McHardy
2008-09-09  7:16             ` Patrick McHardy
2008-09-08  3:02   ` [PATCH 17/33] netns ct: final init_net tweaks Alexey Dobriyan
2008-09-09  7:20     ` Patrick McHardy
2008-09-09  7:32       ` Alexey Dobriyan
2008-09-09  7:51         ` Patrick McHardy
2008-09-13 10:45           ` Alexey Dobriyan
2008-09-27  0:00             ` Alexey Dobriyan
2008-09-28 10:31               ` Patrick McHardy
2008-09-13 10:48           ` [PATCH v2 1/6] netns ct: per-netns statistics Alexey Dobriyan
2008-10-02  7:58             ` Patrick McHardy
2008-09-13 10:49           ` [PATCH v2 2/6] netns ct: per-netns /proc/net/stat/nf_conntrack , /proc/net/stat/ip_conntrack Alexey Dobriyan
2008-10-02  7:59             ` Patrick McHardy
2008-09-13 10:51           ` [PATCH v2 3/6] netns ct: per-netns net.netfilter.nf_conntrack_count sysctl Alexey Dobriyan
2008-10-02  8:00             ` Patrick McHardy
2008-09-13 10:52           ` [PATCH v2 4/6] netns ct: per-netns net.netfilter.nf_conntrack_checksum sysctl Alexey Dobriyan
2008-10-02  8:02             ` Patrick McHardy
2008-09-13 10:53           ` [PATCH v2 5/6] netns ct: per-netns net.netfilter.nf_conntrack_log_invalid sysctl Alexey Dobriyan
2008-10-02  8:04             ` Patrick McHardy
2008-09-13 10:55           ` [PATCH v2 6/6] netns ct: per-netns conntrack accounting Alexey Dobriyan
2008-09-26 23:59             ` Alexey Dobriyan [this message]
2008-10-02  8:11               ` [PATCH v2 7/6] netns ct: final netns tweaks Patrick McHardy
2008-10-02  8:06             ` [PATCH v2 6/6] netns ct: per-netns conntrack accounting Patrick McHardy
2008-09-08  3:02   ` [PATCH 17/33] netns ct: final init_net tweaks Alexey Dobriyan
2008-09-08  3:02   ` [PATCH 19/33] netns ct: per-netns /proc/net/stat/nf_conntrack, /proc/net/stat/ip_conntrack Alexey Dobriyan
2008-09-08  3:02   ` [PATCH 20/33] netns ct: per-netns net.netfilter.nf_conntrack_count sysctl Alexey Dobriyan
2008-09-08  3:02   ` [PATCH 21/33] netns ct: per-netns net.netfilter.nf_conntrack_checksum sysctl Alexey Dobriyan
2008-09-08  3:02   ` [PATCH 22/33] netns ct: per-netns net.netfilter.nf_conntrack_log_invalid sysctl Alexey Dobriyan
2008-09-08  3:03   ` Alexey Dobriyan
2008-09-08  3:03   ` [PATCH 24/33] netns ct: SIP conntracking in netns Alexey Dobriyan
2008-10-02  8:52     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 25/33] netns ct: H323 " Alexey Dobriyan
2008-10-02  8:52     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 26/33] netns ct: GRE " Alexey Dobriyan
2008-10-02  8:53     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 27/33] netns ct: PPTP " Alexey Dobriyan
2008-10-02  8:54     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 28/33] netns nat: fix ipt_MASQUERADE " Alexey Dobriyan
2008-10-02  9:06     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 29/33] netns nat: per-netns NAT table Alexey Dobriyan
2008-10-02  9:08     ` Patrick McHardy
2008-10-02  9:09     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 30/33] netns nat: per-netns bysource hash Alexey Dobriyan
2008-10-02  9:09     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 31/33] netns ct: fixup DNAT in netns Alexey Dobriyan
2008-10-02  9:10     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 32/33] netns nat: PPTP NAT " Alexey Dobriyan
2008-10-02  9:11     ` Patrick McHardy
2008-09-08  3:03   ` [PATCH 33/33] Enable netfilter " Alexey Dobriyan
2008-10-02  9:12     ` Patrick McHardy
2008-10-02  9:51       ` Alexey Dobriyan
2008-10-02 10:00         ` Patrick McHardy
2008-10-02  9:53       ` Alexey Dobriyan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080926235901.GA10489@x200.localdomain \
    --to=adobriyan@gmail.com \
    --cc=containers@lists.linux-foundation.org \
    --cc=kaber@trash.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox