Skip to contentSkip to conversation

netfilter: conntrack: allow increasing bucket size via sysctl too

PATCH Superseded Florian Westphal 2 participants 2 messages Jun 21, 2016 – Jun 22, 2016

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH v2 1/1 netfilter: conntrack: allow increasing bucket size via sysctl too

No need to restrict this to module parameter.

We export a copy of the real hash size -- when user alters the value we allocate the new table, copy entries etc before we update the real size to the requested one.

This is also needed because the real size is used by concurrent readers and cannot be changed without synchronizing the conntrack generation seqcnt.

We only allow changing this value from the initial net namespace.

---
Change since v1:
place assignment inside CONFIG_SYSCL so we don't get build breakage.

 include/net/netfilter/nf_conntrack.h    |  1 +
 net/netfilter/nf_conntrack_core.c       | 42 +++++++++++++++++++++++----------
 net/netfilter/nf_conntrack_standalone.c | 36 ++++++++++++++++++++++++----
 3 files changed, 61 insertions(+), 18 deletions(-)
include/net/netfilter/nf_conntrack.h +1 -0
index 9c0ed3d..5d3397f 100644
@@ -290,6 +290,7 @@ static inline bool nf_is_loopback_packet(const struct sk_buff *skb)
 struct kernel_param;
 
 int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp);
+int nf_conntrack_hash_resize(unsigned int hashsize);
 extern unsigned int nf_conntrack_htable_size;
 extern unsigned int nf_conntrack_max;
 
net/netfilter/nf_conntrack_core.c +29 -13

70 lines not on this page · raw message

net/netfilter/nf_conntrack_standalone.c +31 -5
index f87e84e..a0cc191 100644
@@ -434,8 +434,29 @@ static void nf_conntrack_standalone_fini_proc(struct net *net)
 
 #ifdef CONFIG_SYSCTL
 /* Log invalid packets of a given protocol */
-static int log_invalid_proto_min = 0;
-static int log_invalid_proto_max = 255;
+static int log_invalid_proto_min __read_mostly;
+static int log_invalid_proto_max __read_mostly = 255;
+
+/* size the user *wants to set */
+static unsigned int nf_conntrack_htable_size_user __read_mostly;
+
+static int
+nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
+			 void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	int ret;
+
+	ret = proc_dointvec(table, write, buffer, lenp, ppos);
+	if (ret < 0 || !write)
+		return ret;
+
+	/* update ret, we might not be able to satisfy request */
+	ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
+
+	/* update it to the actual value used by conntrack */
+	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
+	return ret;
+}
 
 static struct ctl_table_header *nf_ct_netfilter_header;
 
@@ -456,10 +477,10 @@ static struct ctl_table nf_ct_sysctl_table[] = {
 	},
 	{
 		.procname       = "nf_conntrack_buckets",
-		.data           = &nf_conntrack_htable_size,
+		.data           = &nf_conntrack_htable_size_user,
 		.maxlen         = sizeof(unsigned int),
-		.mode           = 0444,
-		.proc_handler   = proc_dointvec,
+		.mode           = 0644,
+		.proc_handler   = nf_conntrack_hash_sysctl,
 	},
 	{
 		.procname	= "nf_conntrack_checksum",
@@ -517,6 +538,9 @@ static int nf_conntrack_standalone_init_sysctl(struct net *net)
 	if (net->user_ns != &init_user_ns)
 		table[0].procname = NULL;
 
+	if (!net_eq(&init_net, net))
+		table[2].mode = 0444;
+
 	net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
 	if (!net->ct.sysctl_header)
 		goto out_unregister_netfilter;
@@ -606,6 +630,8 @@ static int __init nf_conntrack_standalone_init(void)
 		ret = -ENOMEM;
 		goto out_sysctl;
 	}
+
+	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
 #endif
 
 	ret = register_pernet_subsys(&nf_conntrack_net_ops);
Signature · 1 line
2.7.3
#1 lore raw

Hi Florian,

2016-06-22 2:46 GMT+08:00 Florian Westphal <[email protected]>:

net/netfilter/nf_conntrack_core.c int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp) · line 1649

Quoted 5 lines
net/netfilter/nf_conntrack_core.c +1 -0
@@ -1650,11 +1646,31 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
        write_seqcount_end(&nf_conntrack_generation);
        nf_conntrack_all_unlock();
        local_bh_enable();
+       synchronize_net();

synchronize_net() call here is redundant?

net/netfilter/nf_conntrack_core.c int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp) · line 1654

Quoted 5 lines
net/netfilter/nf_conntrack_core.c +0 -0
        synchronize_net();
        nf_ct_free_hashtable(old_hash, old_size);
        return 0;
 }

And according to your patch version 1, it seems that you missed to update the Documentation/networking/nf_conntrack-sysctl.txt this time.