netfilter: remove per-netns conntrack tables, part 1
Subsystems (3)
Florian Westphal
maintainercover netfilter: remove per-netns conntrack tables, part 1
[ CCing netdev so netns folks can have a look too ]
This patch series removes the per-netns connection tracking tables. All conntrack objects are then stored in one global global table.
This avoids the infamous 'vmalloc' when lots of namespaces are used:
We no longer allocate a new conntrack table for each namespace (with 64k size this saves 512kb of memory per netns).
- net namespace address is made part of conntrack hash, to spread conntracks over entire table even if netns has overlapping ip addresses.
- lookup and iterators net_eq() to skip conntracks living in a different namespace.
Only the main conntrack table is converted here:
NAT bysrc and expectation hashes are still per namespace (will be unified in a followup series). Also, this retains the per-namespace kmem cache for the conntrack objects. This will also be resolved in a followup series.
Comments welcome.
include/net/netfilter/nf_conntrack_core.h | 11 include/net/netns/conntrack.h | 2 net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 2 net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c | 38 ++ net/netfilter/nf_conntrack_core.c | 233 +++++++++--------- net/netfilter/nf_conntrack_helper.c | 6 net/netfilter/nf_conntrack_netlink.c | 11 net/netfilter/nf_conntrack_standalone.c | 13 - net/netfilter/nf_nat_core.c | 2 net/netfilter/nfnetlink_cttimeout.c | 6 10 files changed, 179 insertions(+), 145 deletions(-)
Florian Westphal
maintainerPATCH 1/9 netfilter: conntrack: keep BH enabled during lookup
No need to disable BH here anymore:
stats are switched to _ATOMIC variant (== this_cpu_inc()), which nowadays generates same code as the non _ATOMIC NF_STAT, at least on x86.
--- net/netfilter/nf_conntrack_core.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-)
net/netfilter/nf_conntrack_core.c +8 -17
@@ -472,18 +472,13 @@ ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,struct hlist_nulls_node *n;unsigned int bucket =hash_bucket (hash, net); -/* Disable BHs the entire time since we normally need to disable them -* at least once for the stats anyway. -*/ -local_bh_disable (); begin:hlist_nulls_for_each_entry_rcu (h, n, &net->ct.hash[bucket], hnnode) {if (nf_ct_key_equal (h, tuple, zone)) { -NF_CT_STAT_INC (net, found); -local_bh_enable (); +NF_CT_STAT_INC_ATOMIC (net, found);return h; } -NF_CT_STAT_INC (net, searched); +NF_CT_STAT_INC_ATOMIC (net, searched); }/* * if the nulls value we got at the end of this lookup is
@@ -491,10 +486,9 @@ begin:* We probably met an item that was moved to another chain. */ if (get_nulls_value (n) != bucket) { -NF_CT_STAT_INC (net, search_restart); +NF_CT_STAT_INC_ATOMIC (net, search_restart); goto begin; } -local_bh_enable ();return NULL ; }
@@ -735,22 +729,19 @@ nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, zone =nf_ct_zone (ignored_conntrack); hash =hash_conntrack (net, tuple); -/* Disable BHs the entire time since we need to disable them at -* least once for the stats anyway. -*/ -rcu_read_lock_bh (); +rcu_read_lock ();hlist_nulls_for_each_entry_rcu (h, n, &net->ct.hash[hash], hnnode) { ct =nf_ct_tuplehash_to_ctrack (h);if (ct != ignored_conntrack &&nf_ct_tuple_equal (tuple, &h->tuple) &&nf_ct_zone_equal (ct, zone,NF_CT_DIRECTION (h))) { -NF_CT_STAT_INC (net, found); -rcu_read_unlock_bh (); +NF_CT_STAT_INC_ATOMIC (net, found); +rcu_read_unlock ();return 1 ; } -NF_CT_STAT_INC (net, searched); +NF_CT_STAT_INC_ATOMIC (net, searched); } -rcu_read_unlock_bh (); +rcu_read_unlock ();return 0 ; }
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 2/9 netfilter: conntrack: fix lookup race during hash resize
In reply to Florian WestphalWhen resizing the conntrack hash table at runtime via
echo 42 > /sys/module/nf_conntrack/parameters/hashsize, we are racing with the conntrack lookup path -- reads can happen in parallel and nothing prevents readers from observing a the newly allocated hash but the old size (or vice versa).
So access to hash[bucket] can trigger OOB read access in case the table got expanded and we saw the new size but the old hash pointer (or it got shrunk and we got new hash ptr but the size of the old and larger table):
kasan: GPF could be caused by NULL-ptr deref or user memory access general protection fault: 0000 [#1] SMP KASAN
CPU: 0 PID: 3 Comm: ksoftirqd/0 Not tainted 4.6.0-rc2+ #107 [..] Call Trace: [<ffffffff822c3d6a>] ? nf_conntrack_tuple_taken+0x12a/0xe90 [<ffffffff822c3ac1>] ? nf_ct_invert_tuplepr+0x221/0x3a0 [<ffffffff8230e703>] get_unique_tuple+0xfb3/0x2760
Use generation counter to obtain the address/length of the same table.
Also add a synchronize_net before freeing the old hash. AFAICS, without it we might access ct_hash[bucket] after ct_hash has been freed, provided that lockless reader got delayed by another event:
CPU1 CPU2seq_beginseq_retry
<delay> resize occurs
free oldhashfor_each(oldhash[size])
Note that resize is only supported in init_netns, it took over 2 minutes of constant resizing+flooding to produce the warning, so this isn't a big problem in practice.
--- net/netfilter/nf_conntrack_core.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_core.c +19 -5
@@ -469,11 +469,18 @@ ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone, const struct nf_conntrack_tuple *tuple, u32 hash) {struct nf_conntrack_tuple_hash *h; +struct hlist_nulls_head *ct_hash;struct hlist_nulls_node *n; -unsigned int bucket =hash_bucket (hash, net); +unsigned int bucket, sequence; begin: -hlist_nulls_for_each_entry_rcu (h, n, &net->ct.hash[bucket], hnnode) { +do { + sequence =read_seqcount_begin (&nf_conntrack_generation); + bucket =hash_bucket (hash, net); + ct_hash = net->ct.hash; + }while (read_seqcount_retry (&nf_conntrack_generation, sequence)); + +hlist_nulls_for_each_entry_rcu (h, n, &ct_hash[bucket], hnnode) {if (nf_ct_key_equal (h, tuple, zone)) {NF_CT_STAT_INC_ATOMIC (net, found);return h;
@@ -722,15 +729,21 @@ nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,struct net *net =nf_ct_net (ignored_conntrack);const struct nf_conntrack_zone *zone;struct nf_conntrack_tuple_hash *h; +struct hlist_nulls_head *ct_hash; +unsigned int hash, sequence;struct hlist_nulls_node *n;struct nf_conn *ct; -unsigned int hash; zone =nf_ct_zone (ignored_conntrack); - hash =hash_conntrack (net, tuple);rcu_read_lock (); -hlist_nulls_for_each_entry_rcu (h, n, &net->ct.hash[hash], hnnode) { +do { + sequence =read_seqcount_begin (&nf_conntrack_generation); + hash =hash_conntrack (net, tuple); + ct_hash = net->ct.hash; + }while (read_seqcount_retry (&nf_conntrack_generation, sequence)); + +hlist_nulls_for_each_entry_rcu (h, n, &ct_hash[hash], hnnode) { ct =nf_ct_tuplehash_to_ctrack (h); if (ct != ignored_conntrack && nf_ct_tuple_equal(tuple, &h->tuple) &&
@@ -1607,6 +1620,7 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)nf_conntrack_all_unlock ();local_bh_enable (); +synchronize_net ();nf_ct_free_hashtable (old_hash, old_size);return 0 ; }
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 3/9 netfilter: conntrack: don't attempt to iterate over empty table
In reply to Florian WestphalOnce we place all conntracks into same table iteration becomes more costly because the table contains conntracks that we are not interested in (belonging to other netns).
So don't bother scanning if the current namespace has no entries.
--- net/netfilter/nf_conntrack_core.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nf_conntrack_core.c +3 -0
@@ -1428,6 +1428,9 @@ void nf_ct_iterate_cleanup(struct net *net,might_sleep (); +if (atomic_read (&net->ct.count) ==0 ) +return ; +while ((ct =get_next_corpse (net, iter, data, &bucket)) !=NULL ) {/* Time to push up daises... */ if (del_timer (&ct->timeout))
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 4/9 netfilter: conntrack: use nf_ct_key_equal() in more places
In reply to Florian WestphalThis prepares for upcoming change that places all conntracks into a single, global table. For this to work we will need to also compare net pointer during lookup. To avoid open-coding such check use the nf_ct_key_equal helper and then later extend it to also consider net_eq.
--- net/netfilter/nf_conntrack_core.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-)
net/netfilter/nf_conntrack_core.c +11 -18
@@ -572,16 +572,13 @@ nf_conntrack_hash_check_insert(struct nf_conn *ct)/* See if there's one in the list already, including reverse */ hlist_nulls_for_each_entry (h, n, &net->ct.hash[hash], hnnode) -if (nf_ct_tuple_equal (&ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple, - &h->tuple) && -nf_ct_zone_equal (nf_ct_tuplehash_to_ctrack (h), zone, -NF_CT_DIRECTION (h))) +if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple, + zone)) goto out; +hlist_nulls_for_each_entry (h, n, &net->ct.hash[reply_hash], hnnode) -if (nf_ct_tuple_equal (&ct->tuplehash[IP_CT_DIR_REPLY ].tuple, - &h->tuple) && -nf_ct_zone_equal (nf_ct_tuplehash_to_ctrack (h), zone, -NF_CT_DIRECTION (h))) +if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_REPLY ].tuple, + zone)) goto out;add_timer (&ct->timeout);
@@ -665,16 +662,13 @@ __nf_conntrack_confirm(struct sk_buff *skb)NAT could have grabbed it without realizing, since we're not in the hash. If there is, we lost race. */ hlist_nulls_for_each_entry (h, n, &net->ct.hash[hash], hnnode) -if (nf_ct_tuple_equal (&ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple, - &h->tuple) && -nf_ct_zone_equal (nf_ct_tuplehash_to_ctrack (h), zone, -NF_CT_DIRECTION (h))) +if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple, + zone)) goto out; +hlist_nulls_for_each_entry (h, n, &net->ct.hash[reply_hash], hnnode) -if (nf_ct_tuple_equal (&ct->tuplehash[IP_CT_DIR_REPLY ].tuple, - &h->tuple) && -nf_ct_zone_equal (nf_ct_tuplehash_to_ctrack (h), zone, -NF_CT_DIRECTION (h))) +if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_REPLY ].tuple, + zone)) goto out;/* Timer relative to confirmation time, not original
@@ -746,8 +740,7 @@ nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,hlist_nulls_for_each_entry_rcu (h, n, &ct_hash[hash], hnnode) { ct =nf_ct_tuplehash_to_ctrack (h);if (ct != ignored_conntrack && -nf_ct_tuple_equal (tuple, &h->tuple) && -nf_ct_zone_equal (ct, zone,NF_CT_DIRECTION (h))) { +nf_ct_key_equal (h, tuple, zone)) {NF_CT_STAT_INC_ATOMIC (net, found);rcu_read_unlock ();return 1 ;
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 5/9 netfilter: conntrack: small refactoring of conntrack seq_printf
In reply to Florian WestphalThe iteration process is lockless, so we test if the conntrack object is eligible for printing (e.g. is AF_INET) after obtaining the reference count.
Once we put all conntracks into same hash table we might see more entries that need to be skipped.
So add a helper and first perform the test in a lockless fashion for fast skip.
Once we obtain the reference count, just repeat the check.
--- .../netfilter/nf_conntrack_l3proto_ipv4_compat.c | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +19 -5
@@ -114,6 +114,19 @@ static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) }#endif +static bool ct_seq_should_skip (const struct nf_conn *ct, +const struct nf_conntrack_tuple_hash *hash) +{ +/* we only want to print DIR_ORIGINAL */ +if (NF_CT_DIRECTION (hash)) +return true; + +if (nf_ct_l3num (ct) !=AF_INET ) +return true; + +return false; +} +static int ct_seq_show (struct seq_file *s,void *v) {struct nf_conntrack_tuple_hash *hash = v;
@@ -123,14 +136,15 @@ static int ct_seq_show(struct seq_file *s, void *v)int ret =0 ;NF_CT_ASSERT (ct); -if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use))) +if (ct_seq_should_skip (ct, hash))return 0 ; +if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use))) +return 0 ; -/* we only want to print DIR_ORIGINAL */ -if (NF_CT_DIRECTION (hash)) - goto release; -if (nf_ct_l3num (ct) !=AF_INET ) +/* check if we raced w. object reuse */ +if (!nf_ct_is_confirmed (ct) || +ct_seq_should_skip (ct, hash)) goto release; l3proto =__nf_ct_l3proto_find (nf_ct_l3num (ct));
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 6/9 netfilter: conntrack: check netns when comparing conntrack objects
In reply to Florian WestphalOnce we place all conntracks in the same hash table we must also compare the netns pointer to skip conntracks that belong to a different namespace.
--- .../netfilter/nf_conntrack_l3proto_ipv4_compat.c | 8 ++++++-- net/netfilter/nf_conntrack_core.c | 23 ++++++++++++---------- net/netfilter/nf_conntrack_netlink.c | 3 +++ 3 files changed, 22 insertions(+), 12 deletions(-)
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +6 -2
@@ -115,6 +115,7 @@ static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)#endif static bool ct_seq_should_skip (const struct nf_conn *ct, +const struct net *net,const struct nf_conntrack_tuple_hash *hash) {/* we only want to print DIR_ORIGINAL */
@@ -124,6 +125,9 @@ static bool ct_seq_should_skip(const struct nf_conn *ct,if (nf_ct_l3num (ct) !=AF_INET )return true; +if (!net_eq (nf_ct_net (ct), net)) +return true; +return false; }
@@ -136,7 +140,7 @@ static int ct_seq_show(struct seq_file *s, void *v)int ret =0 ;NF_CT_ASSERT (ct); -if (ct_seq_should_skip (ct, hash)) +if (ct_seq_should_skip (ct,seq_file_net (s), hash))return 0 ;if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use)))
@@ -144,7 +148,7 @@ static int ct_seq_show(struct seq_file *s, void *v)/* check if we raced w. object reuse */ if (!nf_ct_is_confirmed (ct) || -ct_seq_should_skip (ct, hash)) +ct_seq_should_skip (ct,seq_file_net (s), hash)) goto release; l3proto =__nf_ct_l3proto_find (nf_ct_l3num (ct));
net/netfilter/nf_conntrack_core.c +13 -10
@@ -447,7 +447,8 @@ static void death_by_timeout(unsigned long ul_conntrack)static inline bool nf_ct_key_equal (struct nf_conntrack_tuple_hash *h,const struct nf_conntrack_tuple *tuple, -const struct nf_conntrack_zone *zone) +const struct nf_conntrack_zone *zone, +const struct net *net) {struct nf_conn *ct =nf_ct_tuplehash_to_ctrack (h);
@@ -456,7 +457,8 @@ nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,*/ return nf_ct_tuple_equal (tuple, &h->tuple) &&nf_ct_zone_equal (ct, zone,NF_CT_DIRECTION (h)) && -nf_ct_is_confirmed (ct); +nf_ct_is_confirmed (ct) && +net_eq (net,nf_ct_net (ct)); }/*
@@ -481,7 +483,7 @@ begin: }while (read_seqcount_retry (&nf_conntrack_generation, sequence));hlist_nulls_for_each_entry_rcu (h, n, &ct_hash[bucket], hnnode) { -if (nf_ct_key_equal (h, tuple, zone)) { +if (nf_ct_key_equal (h, tuple, zone, net)) {NF_CT_STAT_INC_ATOMIC (net, found);return h; }
@@ -517,7 +519,7 @@ begin: !atomic_inc_not_zero(&ct->ct_general.use))) h = NULL; else { -if (unlikely (!nf_ct_key_equal (h, tuple, zone))) { +if (unlikely (!nf_ct_key_equal (h, tuple, zone, net))) {nf_ct_put (ct); goto begin; }
@@ -573,12 +575,12 @@ nf_conntrack_hash_check_insert(struct nf_conn *ct)/* See if there's one in the list already, including reverse */ hlist_nulls_for_each_entry (h, n, &net->ct.hash[hash], hnnode)if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple, - zone)) + zone, net)) goto out;hlist_nulls_for_each_entry (h, n, &net->ct.hash[reply_hash], hnnode)if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_REPLY ].tuple, - zone)) + zone, net)) goto out;add_timer (&ct->timeout);
@@ -663,12 +665,12 @@ __nf_conntrack_confirm(struct sk_buff *skb)not in the hash. If there is, we lost race. */ hlist_nulls_for_each_entry (h, n, &net->ct.hash[hash], hnnode)if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple, - zone)) + zone, net)) goto out;hlist_nulls_for_each_entry (h, n, &net->ct.hash[reply_hash], hnnode)if (nf_ct_key_equal (h, &ct->tuplehash[IP_CT_DIR_REPLY ].tuple, - zone)) + zone, net)) goto out;/* Timer relative to confirmation time, not original
@@ -740,7 +742,7 @@ nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,hlist_nulls_for_each_entry_rcu (h, n, &ct_hash[hash], hnnode) { ct =nf_ct_tuplehash_to_ctrack (h);if (ct != ignored_conntrack && -nf_ct_key_equal (h, tuple, zone)) { +nf_ct_key_equal (h, tuple, zone, net)) {NF_CT_STAT_INC_ATOMIC (net, found);rcu_read_unlock ();return 1 ;
@@ -1383,7 +1385,8 @@ get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),if (NF_CT_DIRECTION (h) !=IP_CT_DIR_ORIGINAL )continue ; ct =nf_ct_tuplehash_to_ctrack (h); -if (iter (ct, data)) +if (net_eq (nf_ct_net (ct), net) && +iter (ct, data)) goto found; } }
net/netfilter/nf_conntrack_netlink.c +3 -0
@@ -837,6 +837,9 @@ restart:if (NF_CT_DIRECTION (h) !=IP_CT_DIR_ORIGINAL )continue ; ct =nf_ct_tuplehash_to_ctrack (h); +if (!net_eq (net,nf_ct_net (ct))) +continue ; +/* Dump entries of a given L3 protocol number. * If it is not specified, ie. l3proto == 0, * then dump everything. */
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 7/9 netfilter: conntrack: make netns address part of hash
In reply to Florian WestphalOnce we place all conntracks into a global hash table we want them to be spread across entire hash table, even if namespaces have overlapping ip addresses.
We add nf_conntrack_netns_hash helper to later re-use it for nat bysrc and expectation hash handling. The helper also allows us to avoid the (then) pointless hashing of init_net if kernel is built without netns support.
--- include/net/netfilter/nf_conntrack_core.h | 10 +++++++++ net/netfilter/nf_conntrack_core.c | 34 +++++++++++++++---------------- 2 files changed, 27 insertions(+), 17 deletions(-)
include/net/netfilter/nf_conntrack_core.h +10 -0
@@ -12,6 +12,7 @@#ifndef _NF_CONNTRACK_CORE_H#define _NF_CONNTRACK_CORE_H +#include <linux/hash.h> #include <linux/netfilter.h> #include <net/netfilter/nf_conntrack_l3proto.h> #include <net/netfilter/nf_conntrack_l4proto.h>
@@ -86,4 +87,13 @@ void nf_conntrack_lock(spinlock_t *lock);extern spinlock_t nf_conntrack_expect_lock; +static inline u32 nf_conntrack_netns_hash (const struct net *net) +{ +#ifdef CONFIG_NET_NS +return hash_ptr (net,32 ); +#else +return 0 ; +#endif +} +#endif /* _NF_CONNTRACK_CORE_H */
net/netfilter/nf_conntrack_core.c +17 -17
@@ -144,9 +144,11 @@ EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);static unsigned int nf_conntrack_hash_rnd__read_mostly ; -static u32 hash_conntrack_raw (const struct nf_conntrack_tuple *tuple) +static u32 hash_conntrack_raw (const struct nf_conntrack_tuple *tuple, +const struct net *net) {unsigned int n; +u32 seed;get_random_once (&nf_conntrack_hash_rnd,sizeof (nf_conntrack_hash_rnd));
@@ -154,32 +156,29 @@ static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple)* destination ports (which is a multiple of 4) and treat the last * three bytes manually. */ + seed = nf_conntrack_hash_rnd ^nf_conntrack_netns_hash (net); n = (sizeof (tuple->src) +sizeof (tuple->dst.u3)) /sizeof (u32); -return jhash2 ((u32 *)tuple, n, nf_conntrack_hash_rnd ^ +return jhash2 ((u32 *)tuple, n, seed ^ (((__force __u16 )tuple->dst.u.all <<16 ) | tuple->dst.protonum)); } -static u32 __hash_bucket (u32 hash,unsigned int size) -{ -return reciprocal_scale (hash, size); -} -static u32 hash_bucket (u32 hash,const struct net *net) { -return __hash_bucket (hash, net->ct.htable_size); +return reciprocal_scale (hash, net->ct.htable_size); } -static u_int32_t __hash_conntrack (const struct nf_conntrack_tuple *tuple, -unsigned int size) +static u32 __hash_conntrack (const struct net *net, +const struct nf_conntrack_tuple *tuple, +unsigned int size) { -return __hash_bucket (hash_conntrack_raw (tuple), size); +return reciprocal_scale (hash_conntrack_raw (tuple, net), size); } -static inline u_int32_t hash_conntrack (const struct net *net, -const struct nf_conntrack_tuple *tuple) +static u32 hash_conntrack (const struct net *net, +const struct nf_conntrack_tuple *tuple) { -return __hash_conntrack (tuple, net->ct.htable_size); +return __hash_conntrack (net, tuple, net->ct.htable_size); }bool
@@ -535,7 +534,7 @@ nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, const struct nf_conntrack_tuple *tuple) {return __nf_conntrack_find_get (net, zone, tuple, -hash_conntrack_raw (tuple)); +hash_conntrack_raw (tuple, net)); }EXPORT_SYMBOL_GPL (nf_conntrack_find_get);
@@ -1041,7 +1040,7 @@ resolve_normal_ct(struct net *net, struct nf_conn *tmpl,/* look for tuple match */ zone =nf_ct_zone_tmpl (tmpl, skb, &tmp); - hash =hash_conntrack_raw (&tuple); + hash =hash_conntrack_raw (&tuple, net); h =__nf_conntrack_find_get (net, zone, &tuple, hash);if (!h) { h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
@@ -1605,7 +1604,8 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp) struct nf_conntrack_tuple_hash, hnnode); ct =nf_ct_tuplehash_to_ctrack (h);hlist_nulls_del_rcu (&h->hnnode); - bucket =__hash_conntrack (&h->tuple, hashsize); + bucket =__hash_conntrack (nf_ct_net (ct), + &h->tuple, hashsize);hlist_nulls_add_head_rcu (&h->hnnode, &hash[bucket]); } }
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 8/9 netfilter: conntrack: use a single hashtable for all namespaces
In reply to Florian WestphalWe already include netns address in the hash and compare the netns pointers during lookup, so even if namespaces have overlapping addresses entries will be spread across the table.
Assuming 64k bucket size, this change saves 0.5 mbyte per namespace on a 64bit system.
NAT bysrc and expectation hash is still per namespace, those will changed too soon.
Future patch will also make conntrack object slab cache global again.
---
checkpatch complains about 'WARNING: line over 80 characters' but forcing line breaks looked even worse to me.
include/net/netfilter/nf_conntrack_core.h | 1 + include/net/netns/conntrack.h | 2 - net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 2 +- .../netfilter/nf_conntrack_l3proto_ipv4_compat.c | 10 ++- net/netfilter/nf_conntrack_core.c | 78 +++++++++++----------- net/netfilter/nf_conntrack_helper.c | 6 +- net/netfilter/nf_conntrack_netlink.c | 8 +-- net/netfilter/nf_conntrack_standalone.c | 13 ++-- net/netfilter/nf_nat_core.c | 2 +- net/netfilter/nfnetlink_cttimeout.c | 6 +- 10 files changed, 60 insertions(+), 68 deletions(-)
include/net/netfilter/nf_conntrack_core.h +1 -0
@@ -82,6 +82,7 @@ print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,#define CONNTRACK_LOCKS 1024 +extern struct hlist_nulls_head *nf_conntrack_hash;extern spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS ];void nf_conntrack_lock (spinlock_t *lock);
include/net/netns/conntrack.h +0 -2
@@ -93,9 +93,7 @@ struct netns_ct {int sysctl_tstamp;int sysctl_checksum; -unsigned int htable_size;struct kmem_cache *nf_conntrack_cachep; -struct hlist_nulls_head *hash;struct hlist_head *expect_hash;struct ct_pcpu __percpu *pcpu_lists;struct ip_conntrack_stat __percpu *stat;
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +1 -1
@@ -360,7 +360,7 @@ static int ipv4_init_net(struct net *net) in->ctl_table[0 ].data = &nf_conntrack_max; in->ctl_table[1 ].data = &net->ct.count; - in->ctl_table[2 ].data = &net->ct.htable_size; + in->ctl_table[2 ].data = &nf_conntrack_htable_size; in->ctl_table[3 ].data = &net->ct.sysctl_checksum; in->ctl_table[4 ].data = &net->ct.sysctl_log_invalid;#endif
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +4 -6
@@ -31,15 +31,14 @@ struct ct_iter_state {static struct hlist_nulls_node *ct_get_first (struct seq_file *seq) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private;struct hlist_nulls_node *n;for (st->bucket =0 ; - st->bucket < net->ct.htable_size; + st->bucket < nf_conntrack_htable_size; st->bucket++) { n =rcu_dereference ( -hlist_nulls_first_rcu (&net->ct.hash[st->bucket])); +hlist_nulls_first_rcu (&nf_conntrack_hash[st->bucket]));if (!is_a_nulls (n))return n; }
@@ -49,17 +48,16 @@ static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)static struct hlist_nulls_node *ct_get_next (struct seq_file *seq,struct hlist_nulls_node *head) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private; head =rcu_dereference (hlist_nulls_next_rcu (head));while (is_a_nulls (head)) {if (likely (get_nulls_value (head) == st->bucket)) { -if (++st->bucket >= net->ct.htable_size) +if (++st->bucket >= nf_conntrack_htable_size)return NULL ; } head =rcu_dereference ( -hlist_nulls_first_rcu (&net->ct.hash[st->bucket])); +hlist_nulls_first_rcu (&nf_conntrack_hash[st->bucket])); }return head; }
net/netfilter/nf_conntrack_core.c +38 -40
213 lines not on this page · raw message
net/netfilter/nf_conntrack_helper.c +3 -3
@@ -424,10 +424,10 @@ static void __nf_conntrack_helper_unregister(struct nf_conntrack_helper *me,spin_unlock_bh (&pcpu->lock); }local_bh_disable (); -for (i =0 ; i < net->ct.htable_size; i++) { +for (i =0 ; i < nf_conntrack_htable_size; i++) {nf_conntrack_lock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]); -if (i < net->ct.htable_size) { -hlist_nulls_for_each_entry (h, nn, &net->ct.hash[i], hnnode) +if (i < nf_conntrack_htable_size) { +hlist_nulls_for_each_entry (h, nn, &nf_conntrack_hash[i], hnnode)unhelp (h, me); }spin_unlock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]);
net/netfilter/nf_conntrack_netlink.c +4 -4
@@ -824,16 +824,16 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) last = (struct nf_conn *)cb->args[1 ];local_bh_disable (); -for (; cb->args[0 ] < net->ct.htable_size; cb->args[0 ]++) { +for (; cb->args[0 ] < nf_conntrack_htable_size; cb->args[0 ]++) { restart: lockp = &nf_conntrack_locks[cb->args[0 ] %CONNTRACK_LOCKS ];nf_conntrack_lock (lockp); -if (cb->args[0 ] >= net->ct.htable_size) { +if (cb->args[0 ] >= nf_conntrack_htable_size) {spin_unlock (lockp); goto out; } -hlist_nulls_for_each_entry (h, n, &net->ct.hash[cb->args[0 ]], - hnnode) { +hlist_nulls_for_each_entry (h, n, &nf_conntrack_hash[cb->args[0 ]], + hnnode) {if (NF_CT_DIRECTION (h) !=IP_CT_DIR_ORIGINAL )continue ; ct =nf_ct_tuplehash_to_ctrack (h);
net/netfilter/nf_conntrack_standalone.c +5 -8
@@ -54,14 +54,13 @@ struct ct_iter_state {static struct hlist_nulls_node *ct_get_first (struct seq_file *seq) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private;struct hlist_nulls_node *n;for (st->bucket =0 ; - st->bucket < net->ct.htable_size; + st->bucket < nf_conntrack_htable_size; st->bucket++) { - n =rcu_dereference (hlist_nulls_first_rcu (&net->ct.hash[st->bucket])); + n =rcu_dereference (hlist_nulls_first_rcu (&nf_conntrack_hash[st->bucket]));if (!is_a_nulls (n))return n; }
@@ -71,18 +70,17 @@ static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)static struct hlist_nulls_node *ct_get_next (struct seq_file *seq,struct hlist_nulls_node *head) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private; head =rcu_dereference (hlist_nulls_next_rcu (head));while (is_a_nulls (head)) {if (likely (get_nulls_value (head) == st->bucket)) { -if (++st->bucket >= net->ct.htable_size) +if (++st->bucket >= nf_conntrack_htable_size)return NULL ; } head =rcu_dereference (hlist_nulls_first_rcu ( - &net->ct.hash[st->bucket])); + &nf_conntrack_hash[st->bucket])); }return head; }
@@ -458,7 +456,7 @@ static struct ctl_table nf_ct_sysctl_table[] = { }, { .procname = "nf_conntrack_buckets", - .data = &init_net.ct.htable_size, + .data = &nf_conntrack_htable_size, .maxlen = sizeof(unsigned int), .mode = 0444, .proc_handler = proc_dointvec,
@@ -512,7 +510,6 @@ static int nf_conntrack_standalone_init_sysctl(struct net *net) goto out_kmemdup; table[1 ].data = &net->ct.count; - table[2 ].data = &net->ct.htable_size; table[3 ].data = &net->ct.sysctl_checksum; table[4 ].data = &net->ct.sysctl_log_invalid;
net/netfilter/nf_nat_core.c +1 -1
@@ -824,7 +824,7 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct,static int __net_init nf_nat_net_init (struct net *net) {/* Leave them the same for the moment. */ - net->ct.nat_htable_size = net->ct.htable_size; + net->ct.nat_htable_size = nf_conntrack_htable_size; net->ct.nat_bysource =nf_ct_alloc_hashtable (&net->ct.nat_htable_size,0 );if (!net->ct.nat_bysource)return -ENOMEM ;
net/netfilter/nfnetlink_cttimeout.c +3 -3
@@ -306,10 +306,10 @@ static void ctnl_untimeout(struct net *net, struct ctnl_timeout *timeout)int i;local_bh_disable (); -for (i =0 ; i < net->ct.htable_size; i++) { +for (i =0 ; i < nf_conntrack_htable_size; i++) {nf_conntrack_lock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]); -if (i < net->ct.htable_size) { -hlist_nulls_for_each_entry (h, nn, &net->ct.hash[i], hnnode) +if (i < nf_conntrack_htable_size) { +hlist_nulls_for_each_entry (h, nn, &nf_conntrack_hash[i], hnnode)untimeout (h, timeout); }spin_unlock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]);
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH 9/9 netfilter: conntrack: consider ct netns in early_drop logic
In reply to Florian WestphalWhen iterating, skip conntrack entries living in a different netns.
We could ignore netns and kill some other non-assured one, but it has two problems:
- a netns can kill non-assured conntracks in other namespace
- we would start to 'over-subscribe' the affected/overlimit netns.
--- net/netfilter/nf_conntrack_core.c | 43 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-)
net/netfilter/nf_conntrack_core.c +25 -18
@@ -763,18 +763,20 @@ static noinline int early_drop(struct net *net, unsigned int _hash) {/* Use oldest entry, which is roughly LRU */ struct nf_conntrack_tuple_hash *h; -struct nf_conn *ct =NULL , *tmp; +struct nf_conn *tmp;struct hlist_nulls_node *n; -unsigned int i =0 , cnt =0 ; -int dropped =0 ; -unsigned int hash, sequence; +unsigned int i, hash, sequence; +struct nf_conn *ct =NULL ;spinlock_t *lockp; +bool ret = false; + + i =0 ;local_bh_disable (); restart: sequence =read_seqcount_begin (&nf_conntrack_generation); - hash =scale_hash (_hash); -for (; i < nf_conntrack_htable_size; i++) { +for (; i <NF_CT_EVICTION_RANGE ; i++) { + hash =scale_hash (_hash++); lockp = &nf_conntrack_locks[hash %CONNTRACK_LOCKS ];nf_conntrack_lock (lockp);if (read_seqcount_retry (&nf_conntrack_generation, sequence)) {
@@ -784,35 +786,40 @@ restart:hlist_nulls_for_each_entry_rcu (h, n, &nf_conntrack_hash[hash], hnnode) { tmp =nf_ct_tuplehash_to_ctrack (h); -if (!test_bit (IPS_ASSURED_BIT , &tmp->status) && - !nf_ct_is_dying (tmp) && -atomic_inc_not_zero (&tmp->ct_general.use)) { + +if (test_bit (IPS_ASSURED_BIT , &tmp->status) || + !net_eq (nf_ct_net (tmp), net) || +nf_ct_is_dying (tmp)) +continue ; + +if (atomic_inc_not_zero (&tmp->ct_general.use)) { ct = tmp;break ; } - cnt++; } - hash = (hash +1 ) % nf_conntrack_htable_size;spin_unlock (lockp); - -if (ct || cnt >=NF_CT_EVICTION_RANGE ) +if (ct)break ; - } +local_bh_enable ();if (!ct) -return dropped; +return false; -if (del_timer (&ct->timeout)) { +/* kill only if in same netns -- might have moved due to +* SLAB_DESTROY_BY_RCU rules +*/ +if (net_eq (nf_ct_net (ct), net) &&del_timer (&ct->timeout)) {if (nf_ct_delete (ct,0 ,0 )) { - dropped =1 ;NF_CT_STAT_INC_ATOMIC (net, early_drop); + ret = true; } } +nf_ct_put (ct); -return dropped; +return ret; } static struct nf_conn *
Signature · 1 line
2.7.3
Florian Westphal
maintainerRe: [PATCH nf-next 8/9] netfilter: conntrack: use a single hashtable for all namespaces
In reply to Florian WestphalQuoted 13 lines · Florian Westphal
We already include netns address in the hash and compare the netns pointers during lookup, so even if namespaces have overlapping addresses entries will be spread across the table.
Assuming 64k bucket size, this change saves 0.5 mbyte per namespace on a 64bit system.
NAT bysrc and expectation hash is still per namespace, those will changed too soon.
Future patch will also make conntrack object slab cache global again.
net/netfilter/nf_conntrack_core.c i_see_dead_people: · line 1530
Quoted 4 lines
net/netfilter/nf_conntrack_core.c +0
-1@@ -1527,7 +1528,6 @@ i_see_dead_people: }list_for_each_entry (net, net_exit_list, exit_list) { -nf_ct_free_hashtable (net->ct.hash, net->ct.htable_size);
Removing this is ok, but nf_ct_free_hashtable() must now be called in nf_conntrack_cleanup_end().
I'll wait with v2 for a couple of days.
Florian Westphal
maintainerPATCH v2 7/9 netfilter: conntrack: make netns address part of hash
In reply to Florian WestphalOnce we place all conntracks into a global hash table we want them to be spread across entire hash table, even if namespaces have overlapping ip addresses.
We add nf_conntrack_netns_hash helper to later re-use it for nat bysrc and expectation hash handling. The helper also allows us to avoid the (then) pointless hashing of init_net if kernel is built without netns support.
---
Changes since v1:
use hash32_ptr instead of hash_ptr
include/net/netfilter/nf_conntrack_core.h | 10 +++++++++ net/netfilter/nf_conntrack_core.c | 34 +++++++++++++++---------------- 2 files changed, 27 insertions(+), 17 deletions(-)
include/net/netfilter/nf_conntrack_core.h +10 -0
@@ -12,6 +12,7 @@#ifndef _NF_CONNTRACK_CORE_H#define _NF_CONNTRACK_CORE_H +#include <linux/hash.h> #include <linux/netfilter.h> #include <net/netfilter/nf_conntrack_l3proto.h> #include <net/netfilter/nf_conntrack_l4proto.h>
@@ -86,4 +87,13 @@ void nf_conntrack_lock(spinlock_t *lock);extern spinlock_t nf_conntrack_expect_lock; +static inline u32 nf_conntrack_netns_hash (const struct net *net) +{ +#ifdef CONFIG_NET_NS +return hash32_ptr (net); +#else +return 0 ; +#endif +} +#endif /* _NF_CONNTRACK_CORE_H */
net/netfilter/nf_conntrack_core.c +17 -17
@@ -144,9 +144,11 @@ EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);static unsigned int nf_conntrack_hash_rnd__read_mostly ; -static u32 hash_conntrack_raw (const struct nf_conntrack_tuple *tuple) +static u32 hash_conntrack_raw (const struct nf_conntrack_tuple *tuple, +const struct net *net) {unsigned int n; +u32 seed;get_random_once (&nf_conntrack_hash_rnd,sizeof (nf_conntrack_hash_rnd));
@@ -154,32 +156,29 @@ static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple)* destination ports (which is a multiple of 4) and treat the last * three bytes manually. */ + seed = nf_conntrack_hash_rnd ^nf_conntrack_netns_hash (net); n = (sizeof (tuple->src) +sizeof (tuple->dst.u3)) /sizeof (u32); -return jhash2 ((u32 *)tuple, n, nf_conntrack_hash_rnd ^ +return jhash2 ((u32 *)tuple, n, seed ^ (((__force __u16 )tuple->dst.u.all <<16 ) | tuple->dst.protonum)); } -static u32 __hash_bucket (u32 hash,unsigned int size) -{ -return reciprocal_scale (hash, size); -} -static u32 hash_bucket (u32 hash,const struct net *net) { -return __hash_bucket (hash, net->ct.htable_size); +return reciprocal_scale (hash, net->ct.htable_size); } -static u_int32_t __hash_conntrack (const struct nf_conntrack_tuple *tuple, -unsigned int size) +static u32 __hash_conntrack (const struct net *net, +const struct nf_conntrack_tuple *tuple, +unsigned int size) { -return __hash_bucket (hash_conntrack_raw (tuple), size); +return reciprocal_scale (hash_conntrack_raw (tuple, net), size); } -static inline u_int32_t hash_conntrack (const struct net *net, -const struct nf_conntrack_tuple *tuple) +static u32 hash_conntrack (const struct net *net, +const struct nf_conntrack_tuple *tuple) { -return __hash_conntrack (tuple, net->ct.htable_size); +return __hash_conntrack (net, tuple, net->ct.htable_size); }bool
@@ -535,7 +534,7 @@ nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, const struct nf_conntrack_tuple *tuple) {return __nf_conntrack_find_get (net, zone, tuple, -hash_conntrack_raw (tuple)); +hash_conntrack_raw (tuple, net)); }EXPORT_SYMBOL_GPL (nf_conntrack_find_get);
@@ -1041,7 +1040,7 @@ resolve_normal_ct(struct net *net, struct nf_conn *tmpl,/* look for tuple match */ zone =nf_ct_zone_tmpl (tmpl, skb, &tmp); - hash =hash_conntrack_raw (&tuple); + hash =hash_conntrack_raw (&tuple, net); h =__nf_conntrack_find_get (net, zone, &tuple, hash);if (!h) { h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
@@ -1605,7 +1604,8 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp) struct nf_conntrack_tuple_hash, hnnode); ct =nf_ct_tuplehash_to_ctrack (h);hlist_nulls_del_rcu (&h->hnnode); - bucket =__hash_conntrack (&h->tuple, hashsize); + bucket =__hash_conntrack (nf_ct_net (ct), + &h->tuple, hashsize);hlist_nulls_add_head_rcu (&h->hnnode, &hash[bucket]); } }
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH v2 8/9 netfilter: conntrack: use a single hashtable for all namespaces
In reply to Florian WestphalWe already include netns address in the hash and compare the netns pointers during lookup, so even if namespaces have overlapping addresses entries will be spread across the table.
Assuming 64k bucket size, this change saves 0.5 mbyte per namespace on a 64bit system.
NAT bysrc and expectation hash is still per namespace, those will changed too soon.
Future patch will also make conntrack object slab cache global again.
---
Changes since v1:
- call nf_ct_free_hashtable in nf_conntrack_cleanup_end().
checkpatch complains about 'WARNING: line over 80 characters' but forcing line breaks looked even worse to me.
include/net/netfilter/nf_conntrack_core.h | 1 + include/net/netns/conntrack.h | 2 - net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 2 +- .../netfilter/nf_conntrack_l3proto_ipv4_compat.c | 10 ++- net/netfilter/nf_conntrack_core.c | 80 +++++++++++----------- net/netfilter/nf_conntrack_helper.c | 6 +- net/netfilter/nf_conntrack_netlink.c | 8 +-- net/netfilter/nf_conntrack_standalone.c | 13 ++-- net/netfilter/nf_nat_core.c | 2 +- net/netfilter/nfnetlink_cttimeout.c | 6 +- 10 files changed, 62 insertions(+), 68 deletions(-)
include/net/netfilter/nf_conntrack_core.h +1 -0
@@ -82,6 +82,7 @@ print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,#define CONNTRACK_LOCKS 1024 +extern struct hlist_nulls_head *nf_conntrack_hash;extern spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS ];void nf_conntrack_lock (spinlock_t *lock);
include/net/netns/conntrack.h +0 -2
@@ -93,9 +93,7 @@ struct netns_ct {int sysctl_tstamp;int sysctl_checksum; -unsigned int htable_size;struct kmem_cache *nf_conntrack_cachep; -struct hlist_nulls_head *hash;struct hlist_head *expect_hash;struct ct_pcpu __percpu *pcpu_lists;struct ip_conntrack_stat __percpu *stat;
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +1 -1
@@ -360,7 +360,7 @@ static int ipv4_init_net(struct net *net) in->ctl_table[0 ].data = &nf_conntrack_max; in->ctl_table[1 ].data = &net->ct.count; - in->ctl_table[2 ].data = &net->ct.htable_size; + in->ctl_table[2 ].data = &nf_conntrack_htable_size; in->ctl_table[3 ].data = &net->ct.sysctl_checksum; in->ctl_table[4 ].data = &net->ct.sysctl_log_invalid;#endif
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +4 -6
@@ -31,15 +31,14 @@ struct ct_iter_state {static struct hlist_nulls_node *ct_get_first (struct seq_file *seq) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private;struct hlist_nulls_node *n;for (st->bucket =0 ; - st->bucket < net->ct.htable_size; + st->bucket < nf_conntrack_htable_size; st->bucket++) { n =rcu_dereference ( -hlist_nulls_first_rcu (&net->ct.hash[st->bucket])); +hlist_nulls_first_rcu (&nf_conntrack_hash[st->bucket]));if (!is_a_nulls (n))return n; }
@@ -49,17 +48,16 @@ static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)static struct hlist_nulls_node *ct_get_next (struct seq_file *seq,struct hlist_nulls_node *head) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private; head =rcu_dereference (hlist_nulls_next_rcu (head));while (is_a_nulls (head)) {if (likely (get_nulls_value (head) == st->bucket)) { -if (++st->bucket >= net->ct.htable_size) +if (++st->bucket >= nf_conntrack_htable_size)return NULL ; } head =rcu_dereference ( -hlist_nulls_first_rcu (&net->ct.hash[st->bucket])); +hlist_nulls_first_rcu (&nf_conntrack_hash[st->bucket])); }return head; }
net/netfilter/nf_conntrack_core.c +40 -40
221 lines not on this page · raw message
net/netfilter/nf_conntrack_helper.c +3 -3
@@ -424,10 +424,10 @@ static void __nf_conntrack_helper_unregister(struct nf_conntrack_helper *me,spin_unlock_bh (&pcpu->lock); }local_bh_disable (); -for (i =0 ; i < net->ct.htable_size; i++) { +for (i =0 ; i < nf_conntrack_htable_size; i++) {nf_conntrack_lock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]); -if (i < net->ct.htable_size) { -hlist_nulls_for_each_entry (h, nn, &net->ct.hash[i], hnnode) +if (i < nf_conntrack_htable_size) { +hlist_nulls_for_each_entry (h, nn, &nf_conntrack_hash[i], hnnode)unhelp (h, me); }spin_unlock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]);
net/netfilter/nf_conntrack_netlink.c +4 -4
@@ -824,16 +824,16 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) last = (struct nf_conn *)cb->args[1 ];local_bh_disable (); -for (; cb->args[0 ] < net->ct.htable_size; cb->args[0 ]++) { +for (; cb->args[0 ] < nf_conntrack_htable_size; cb->args[0 ]++) { restart: lockp = &nf_conntrack_locks[cb->args[0 ] %CONNTRACK_LOCKS ];nf_conntrack_lock (lockp); -if (cb->args[0 ] >= net->ct.htable_size) { +if (cb->args[0 ] >= nf_conntrack_htable_size) {spin_unlock (lockp); goto out; } -hlist_nulls_for_each_entry (h, n, &net->ct.hash[cb->args[0 ]], - hnnode) { +hlist_nulls_for_each_entry (h, n, &nf_conntrack_hash[cb->args[0 ]], + hnnode) {if (NF_CT_DIRECTION (h) !=IP_CT_DIR_ORIGINAL )continue ; ct =nf_ct_tuplehash_to_ctrack (h);
net/netfilter/nf_conntrack_standalone.c +5 -8
@@ -54,14 +54,13 @@ struct ct_iter_state {static struct hlist_nulls_node *ct_get_first (struct seq_file *seq) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private;struct hlist_nulls_node *n;for (st->bucket =0 ; - st->bucket < net->ct.htable_size; + st->bucket < nf_conntrack_htable_size; st->bucket++) { - n =rcu_dereference (hlist_nulls_first_rcu (&net->ct.hash[st->bucket])); + n =rcu_dereference (hlist_nulls_first_rcu (&nf_conntrack_hash[st->bucket]));if (!is_a_nulls (n))return n; }
@@ -71,18 +70,17 @@ static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)static struct hlist_nulls_node *ct_get_next (struct seq_file *seq,struct hlist_nulls_node *head) { -struct net *net =seq_file_net (seq);struct ct_iter_state *st = seq->private; head =rcu_dereference (hlist_nulls_next_rcu (head));while (is_a_nulls (head)) {if (likely (get_nulls_value (head) == st->bucket)) { -if (++st->bucket >= net->ct.htable_size) +if (++st->bucket >= nf_conntrack_htable_size)return NULL ; } head =rcu_dereference (hlist_nulls_first_rcu ( - &net->ct.hash[st->bucket])); + &nf_conntrack_hash[st->bucket])); }return head; }
@@ -458,7 +456,7 @@ static struct ctl_table nf_ct_sysctl_table[] = { }, { .procname = "nf_conntrack_buckets", - .data = &init_net.ct.htable_size, + .data = &nf_conntrack_htable_size, .maxlen = sizeof(unsigned int), .mode = 0444, .proc_handler = proc_dointvec,
@@ -512,7 +510,6 @@ static int nf_conntrack_standalone_init_sysctl(struct net *net) goto out_kmemdup; table[1 ].data = &net->ct.count; - table[2 ].data = &net->ct.htable_size; table[3 ].data = &net->ct.sysctl_checksum; table[4 ].data = &net->ct.sysctl_log_invalid;
net/netfilter/nf_nat_core.c +1 -1
@@ -824,7 +824,7 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct,static int __net_init nf_nat_net_init (struct net *net) {/* Leave them the same for the moment. */ - net->ct.nat_htable_size = net->ct.htable_size; + net->ct.nat_htable_size = nf_conntrack_htable_size; net->ct.nat_bysource =nf_ct_alloc_hashtable (&net->ct.nat_htable_size,0 );if (!net->ct.nat_bysource)return -ENOMEM ;
net/netfilter/nfnetlink_cttimeout.c +3 -3
@@ -306,10 +306,10 @@ static void ctnl_untimeout(struct net *net, struct ctnl_timeout *timeout)int i;local_bh_disable (); -for (i =0 ; i < net->ct.htable_size; i++) { +for (i =0 ; i < nf_conntrack_htable_size; i++) {nf_conntrack_lock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]); -if (i < net->ct.htable_size) { -hlist_nulls_for_each_entry (h, nn, &net->ct.hash[i], hnnode) +if (i < nf_conntrack_htable_size) { +hlist_nulls_for_each_entry (h, nn, &nf_conntrack_hash[i], hnnode)untimeout (h, timeout); }spin_unlock (&nf_conntrack_locks[i %CONNTRACK_LOCKS ]);
Signature · 1 line
2.7.3
Florian Westphal
maintainerPATCH v2 9/9 netfilter: conntrack: consider ct netns in early_drop logic
In reply to Florian WestphalWhen iterating, skip conntrack entries living in a different netns.
We could ignore netns and kill some other non-assured one, but it has two problems:
- a netns can kill non-assured conntracks in other namespace
- we would start to 'over-subscribe' the affected/overlimit netns.
--- net/netfilter/nf_conntrack_core.c | 43 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-)
net/netfilter/nf_conntrack_core.c +25 -18
@@ -763,18 +763,20 @@ static noinline int early_drop(struct net *net, unsigned int _hash) {/* Use oldest entry, which is roughly LRU */ struct nf_conntrack_tuple_hash *h; -struct nf_conn *ct =NULL , *tmp; +struct nf_conn *tmp;struct hlist_nulls_node *n; -unsigned int i =0 , cnt =0 ; -int dropped =0 ; -unsigned int hash, sequence; +unsigned int i, hash, sequence; +struct nf_conn *ct =NULL ;spinlock_t *lockp; +bool ret = false; + + i =0 ;local_bh_disable (); restart: sequence =read_seqcount_begin (&nf_conntrack_generation); - hash =scale_hash (_hash); -for (; i < nf_conntrack_htable_size; i++) { +for (; i <NF_CT_EVICTION_RANGE ; i++) { + hash =scale_hash (_hash++); lockp = &nf_conntrack_locks[hash %CONNTRACK_LOCKS ];nf_conntrack_lock (lockp);if (read_seqcount_retry (&nf_conntrack_generation, sequence)) {
@@ -784,35 +786,40 @@ restart:hlist_nulls_for_each_entry_rcu (h, n, &nf_conntrack_hash[hash], hnnode) { tmp =nf_ct_tuplehash_to_ctrack (h); -if (!test_bit (IPS_ASSURED_BIT , &tmp->status) && - !nf_ct_is_dying (tmp) && -atomic_inc_not_zero (&tmp->ct_general.use)) { + +if (test_bit (IPS_ASSURED_BIT , &tmp->status) || + !net_eq (nf_ct_net (tmp), net) || +nf_ct_is_dying (tmp)) +continue ; + +if (atomic_inc_not_zero (&tmp->ct_general.use)) { ct = tmp;break ; } - cnt++; } - hash = (hash +1 ) % nf_conntrack_htable_size;spin_unlock (lockp); - -if (ct || cnt >=NF_CT_EVICTION_RANGE ) +if (ct)break ; - } +local_bh_enable ();if (!ct) -return dropped; +return false; -if (del_timer (&ct->timeout)) { +/* kill only if in same netns -- might have moved due to +* SLAB_DESTROY_BY_RCU rules +*/ +if (net_eq (nf_ct_net (ct), net) &&del_timer (&ct->timeout)) {if (nf_ct_delete (ct,0 ,0 )) { - dropped =1 ;NF_CT_STAT_INC_ATOMIC (net, early_drop); + ret = true; } } +nf_ct_put (ct); -return dropped; +return ret; } static struct nf_conn *
Signature · 1 line
2.7.3
Re: [PATCH v2 nf-next 7/9] netfilter: conntrack: make netns address part of hash
In reply to Florian Westphalinclude/net/netfilter/nf_conntrack_core.h void nf_conntrack_lock(spinlock_t *lock); · line 98
Quoted 13 lines · Florian Westphal
Once we place all conntracks into a global hash table we want them to be spread across entire hash table, even if namespaces have overlapping ip addresses.
include/net/netfilter/nf_conntrack_core.h +9
-0+static inline u32 nf_conntrack_netns_hash (const struct net *net) +{ +#ifdef CONFIG_NET_NS +return hash32_ptr (net); +#else +return 0 ; +#endif +} +
Are you reinventing net_hash_mix() ?
If net_hash_mix() is not good enough, please fix it ;)
hash_ptr() is not that good, as ongoing thread in lkml shows.
Thanks.
Florian Westphal
maintainerRe: [PATCH v2 nf-next 7/9] netfilter: conntrack: make netns address part of hash
Quoted 12 lines · Eric Dumazet
include/net/netfilter/nf_conntrack_core.h void nf_conntrack_lock(spinlock_t *lock); · line 98
Quoted 9 lines · Florian Westphal
include/net/netfilter/nf_conntrack_core.h +9
-0+static inline u32 nf_conntrack_netns_hash (const struct net *net) +{ +#ifdef CONFIG_NET_NS +return hash32_ptr (net); +#else +return 0 ; +#endif +} +Are you reinventing
net_hash_mix()?
Yes, will respin, thanks Eric!
Quoted 1 line
If
net_hash_mix()is not good enough, please fix it ;)
No, I did not know about it, thats all. It will do just fine, thanks for the hint!
Florian Westphal
maintainerPATCH v3 7/9 netfilter: conntrack: make netns address part of hash
In reply to Florian WestphalOnce we place all conntracks into a global hash table we want them to be spread across entire hash table, even if namespaces have overlapping ip addresses.
---
Changes since v2:
use net_hash_mix() instead of re-inventing it (Eric Dumazet) Changes since v1:
use hash32_ptr instead of hash_ptr
net/netfilter/nf_conntrack_core.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
net/netfilter/nf_conntrack_core.c +18 -17
@@ -54,6 +54,7 @@#include <net/netfilter/nf_nat.h> #include <net/netfilter/nf_nat_core.h> #include <net/netfilter/nf_nat_helper.h> +#include <net/netns/hash.h> #define NF_CONNTRACK_VERSION "0.5.0"
@@ -144,9 +145,11 @@ EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);static unsigned int nf_conntrack_hash_rnd__read_mostly ; -static u32 hash_conntrack_raw (const struct nf_conntrack_tuple *tuple) +static u32 hash_conntrack_raw (const struct nf_conntrack_tuple *tuple, +const struct net *net) {unsigned int n; +u32 seed;get_random_once (&nf_conntrack_hash_rnd,sizeof (nf_conntrack_hash_rnd));
@@ -154,32 +157,29 @@ static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple)* destination ports (which is a multiple of 4) and treat the last * three bytes manually. */ + seed = nf_conntrack_hash_rnd ^net_hash_mix (net); n = (sizeof (tuple->src) +sizeof (tuple->dst.u3)) /sizeof (u32); -return jhash2 ((u32 *)tuple, n, nf_conntrack_hash_rnd ^ +return jhash2 ((u32 *)tuple, n, seed ^ (((__force __u16 )tuple->dst.u.all <<16 ) | tuple->dst.protonum)); } -static u32 __hash_bucket (u32 hash,unsigned int size) -{ -return reciprocal_scale (hash, size); -} -static u32 hash_bucket (u32 hash,const struct net *net) { -return __hash_bucket (hash, net->ct.htable_size); +return reciprocal_scale (hash, net->ct.htable_size); } -static u_int32_t __hash_conntrack (const struct nf_conntrack_tuple *tuple, -unsigned int size) +static u32 __hash_conntrack (const struct net *net, +const struct nf_conntrack_tuple *tuple, +unsigned int size) { -return __hash_bucket (hash_conntrack_raw (tuple), size); +return reciprocal_scale (hash_conntrack_raw (tuple, net), size); } -static inline u_int32_t hash_conntrack (const struct net *net, -const struct nf_conntrack_tuple *tuple) +static u32 hash_conntrack (const struct net *net, +const struct nf_conntrack_tuple *tuple) { -return __hash_conntrack (tuple, net->ct.htable_size); +return __hash_conntrack (net, tuple, net->ct.htable_size); }bool
@@ -535,7 +535,7 @@ nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone, const struct nf_conntrack_tuple *tuple) {return __nf_conntrack_find_get (net, zone, tuple, -hash_conntrack_raw (tuple)); +hash_conntrack_raw (tuple, net)); }EXPORT_SYMBOL_GPL (nf_conntrack_find_get);
@@ -1041,7 +1041,7 @@ resolve_normal_ct(struct net *net, struct nf_conn *tmpl,/* look for tuple match */ zone =nf_ct_zone_tmpl (tmpl, skb, &tmp); - hash =hash_conntrack_raw (&tuple); + hash =hash_conntrack_raw (&tuple, net); h =__nf_conntrack_find_get (net, zone, &tuple, hash);if (!h) { h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
@@ -1605,7 +1605,8 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp) struct nf_conntrack_tuple_hash, hnnode); ct =nf_ct_tuplehash_to_ctrack (h);hlist_nulls_del_rcu (&h->hnnode); - bucket =__hash_conntrack (&h->tuple, hashsize); + bucket =__hash_conntrack (nf_ct_net (ct), + &h->tuple, hashsize);hlist_nulls_add_head_rcu (&h->hnnode, &hash[bucket]); } }
Signature · 1 line
2.7.3
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 3/9] netfilter: conntrack: don't attempt to iterate over empty table
In reply to Florian WestphalQuoted 16 lines · Florian Westphal
Once we place all conntracks into same table iteration becomes more costly because the table contains conntracks that we are not interested in (belonging to other netns).
So don't bother scanning if the current namespace has no entries.
Signed-off-by Florian Westphal <[email protected]>--- net/netfilter/nf_conntrack_core.c | 3 +++ 1 file changed, 3 insertions(+)net/netfilter/nf_conntrack_core.c +0
-0
net/netfilter/nf_conntrack_core.c void nf_ct_iterate_cleanup(struct net *net, · line 1432
Quoted 5 lines
net/netfilter/nf_conntrack_core.c +2
-0@@ -1428,6 +1428,9 @@ void nf_ct_iterate_cleanup(struct net *net,might_sleep (); +if (atomic_read (&net->ct.count) ==0 ) +return ;
This optimization gets defeated with just one single conntrack (ie. net->ct.count == 1), so I wonder if this is practical thing.
At the cost of consuming more memory per conntrack, we may consider adding a per-net list so this iteration doesn't become a problem.
Quoted 10 lines
net/netfilter/nf_conntrack_core.c +0
-0while ((ct =get_next_corpse (net, iter, data, &bucket)) !=NULL ) {/* Time to push up daises... */ if (del_timer (&ct->timeout))Signature · 6 lines
2.7.3 -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Florian Westphal
maintainerRe: [PATCH nf-next 3/9] netfilter: conntrack: don't attempt to iterate over empty table
Quoted 18 lines · Pablo Neira Ayuso
Quoted 17 lines · Florian Westphal
Once we place all conntracks into same table iteration becomes more costly because the table contains conntracks that we are not interested in (belonging to other netns).
So don't bother scanning if the current namespace has no entries.
Signed-off-by Florian Westphal <[email protected]>--- net/netfilter/nf_conntrack_core.c | 3 +++ 1 file changed, 3 insertions(+)net/netfilter/nf_conntrack_core.c +0
-0@@ -1428,6 +1428,9 @@ void nf_ct_iterate_cleanup(struct net *net,
Quoted 7 lines
net/netfilter/nf_conntrack_core.c void nf_ct_iterate_cleanup(struct net *net, · line 1432
Quoted 4 lines
net/netfilter/nf_conntrack_core.c +2
-0might_sleep (); +if (atomic_read (&net->ct.count) ==0 ) +return ;This optimization gets defeated with just one single conntrack (ie.
net->ct.count== 1), so I wonder if this is practical thing.
I was thinking of the cleanup we do in the netns exit path (in nf_conntrack_cleanup_net_list() ).
If you don't like this I can move the check here:
i_see_dead_people:
busy = 0;
list_for_each_entry(net, net_exit_list, exit_list) {
// here
if (atomic_read .. > 0)
nf_ct_iterate_cleanup(net, kill_all, ...Quoted 2 lines
At the cost of consuming more memory per conntrack, we may consider adding a per-net list so this iteration doesn't become a problem.
I don't think that will be needed. We don't have any such iterations
in the fast path.
For dumps via ctnetlink it shouldn't be a big deal either, if needed we can optimize that to use rcu readlocks only and 'upgrade' to locked path only when we want to dump the candidate ct.
for deferred pruning).
early_drop will go away soon (i'll rework it to do the early_drop from work queue).
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 3/9] netfilter: conntrack: don't attempt to iterate over empty table
Quoted 21 lines · Florian Westphal
Quoted 20 lines · Pablo Neira Ayuso
Quoted 19 lines · Florian Westphal
Once we place all conntracks into same table iteration becomes more costly because the table contains conntracks that we are not interested in (belonging to other netns).
So don't bother scanning if the current namespace has no entries.
Signed-off-by Florian Westphal <[email protected]>--- net/netfilter/nf_conntrack_core.c | 3 +++ 1 file changed, 3 insertions(+)net/netfilter/nf_conntrack_core.c +0
-0@@ -1428,6 +1428,9 @@ void nf_ct_iterate_cleanup(struct net *net,might_sleep ();
Quoted 8 lines
Quoted 5 lines
net/netfilter/nf_conntrack_core.c void nf_ct_iterate_cleanup(struct net *net, · line 1432
Quoted 2 lines
net/netfilter/nf_conntrack_core.c +2
-0+if (atomic_read (&net->ct.count) ==0 ) +return ;This optimization gets defeated with just one single conntrack (ie.
net->ct.count== 1), so I wonder if this is practical thing.I was thinking of the cleanup we do in the netns exit path (in
nf_conntrack_cleanup_net_list()).
Right, but in that path we still have entries in the table.
Quoted 8 lines
If you don't like this I can move the check here:
i_see_dead_people:busy = 0; list_for_each_entry(net, net_exit_list, exit_list) { // here if (atomic_read .. > 0) nf_ct_iterate_cleanup(net, kill_all, ...
I don't mind about placing this or there, as I said, my question is how often we will hit this optimization in a real scenario.
If you think the answer is often, then this will help.
Otherwise, every time we'll go container destruction path, we'll hit slow path, ie. scanning the full table.
Quoted 6 lines
Quoted 2 lines
At the cost of consuming more memory per conntrack, we may consider adding a per-net list so this iteration doesn't become a problem.
I don't think that will be needed. We don't have any such iterations
in the fast path.
Quoted 6 lines
For dumps via ctnetlink it shouldn't be a big deal either, if needed we can optimize that to use rcu readlocks only and 'upgrade' to locked path only when we want to dump the candidate ct.
for deferred pruning).early_dropwill go away soon (i'll rework it to do theearly_dropfrom work queue).
OK.
Florian Westphal
maintainerRe: [PATCH nf-next 3/9] netfilter: conntrack: don't attempt to iterate over empty table
Quoted 4 lines · Pablo Neira Ayuso
Quoted 2 lines
I was thinking of the cleanup we do in the netns exit path (in
nf_conntrack_cleanup_net_list()).Right, but in that path we still have entries in the table.
Not necessarily, they might have already been removed (timeout, close).
Quoted 9 lines
Quoted 8 lines
If you don't like this I can move the check here:
i_see_dead_people:busy = 0; list_for_each_entry(net, net_exit_list, exit_list) { // here if (atomic_read .. > 0) nf_ct_iterate_cleanup(net, kill_all, ...
Quoted 4 lines
I don't mind about placing this or there, as I said, my question is how often we will hit this optimization in a real scenario.
If you think the answer is often, then this will help.
I think the extra atomic_read in this code does no harm and saves us the entire scan. Also, in the exit path, when we hit the 'i_see_dead_people' label we restart the entire loop, so if we have 200 netns on the list and the last one caused that restart, we re-iterate needlesly for 199 netns...
Quoted 2 lines
Otherwise, every time we'll go container destruction path, we'll hit slow path, ie. scanning the full table.
Yes, but I see no other choice.
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 5/9] netfilter: conntrack: small refactoring of conntrack seq_printf
In reply to Florian WestphalQuoted 48 lines · Florian Westphal
The iteration process is lockless, so we test if the conntrack object is eligible for printing (e.g. is
AF_INET) after obtaining the reference count.Once we put all conntracks into same hash table we might see more entries that need to be skipped.
So add a helper and first perform the test in a lockless fashion for fast skip.
Once we obtain the reference count, just repeat the check.
Signed-off-by Florian Westphal <[email protected]>--- .../netfilter/nf_conntrack_l3proto_ipv4_compat.c | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +14
-1@@ -114,6 +114,19 @@ static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) }#endif +static bool ct_seq_should_skip (const struct nf_conn *ct, +const struct nf_conntrack_tuple_hash *hash) +{ +/* we only want to print DIR_ORIGINAL */ +if (NF_CT_DIRECTION (hash)) +return true; + +if (nf_ct_l3num (ct) !=AF_INET ) +return true; + +return false; +} +static int ct_seq_show (struct seq_file *s,void *v) {struct nf_conntrack_tuple_hash *hash = v;@@ -123,14 +136,15 @@ static int ct_seq_show(struct seq_file *s, void *v)int ret =0 ;NF_CT_ASSERT (ct); -if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use))) +if (ct_seq_should_skip (ct, hash))return 0 ;
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c static int ct_seq_show(struct seq_file *s, void *v) · line 146
Quoted 10 lines
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +4
-4+if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use))) +return 0 ; -/* we only want to print DIR_ORIGINAL */ -if (NF_CT_DIRECTION (hash)) - goto release; -if (nf_ct_l3num (ct) !=AF_INET ) +/* check if we raced w. object reuse */ + if (!nf_ct_is_confirmed(ct) ||
This refactoring includes this new check, is this intentional?
Quoted 7 lines
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +1
-0+ ct_seq_should_skip(ct, hash)) goto release; l3proto =__nf_ct_l3proto_find (nf_ct_l3num (ct));Signature · 1 line
2.7.3
Florian Westphal
maintainerRe: [PATCH nf-next 5/9] netfilter: conntrack: small refactoring of conntrack seq_printf
Quoted 7 lines · Pablo Neira Ayuso
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c static int ct_seq_show(struct seq_file *s, void *v) · line 146
Quoted 5 lines
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +2
-3-if (NF_CT_DIRECTION (hash)) - goto release; -if (nf_ct_l3num (ct) !=AF_INET ) +/* check if we raced w. object reuse */ + if (!nf_ct_is_confirmed(ct) ||This refactoring includes this new check, is this intentional?
Hmm, yes and no.
I should have put it in an extra commit :-/
Without this, we might erronously print a conntrack that is NEW and which isn't confirmed yet.
We won't crash since seq_print doesn't depend on extensions being set up properly, but it seems better to only display those conntracks that are part of the conntrack hash table (i.e., have the confirmed bit set).
Let me know if you want me to respin this as a separate fix, thanks!
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 3/9] netfilter: conntrack: don't attempt to iterate over empty table
In reply to Florian WestphalQuoted 4 lines · Florian Westphal
Quoted 2 lines
Otherwise, every time we'll go container destruction path, we'll hit slow path, ie. scanning the full table.
Yes, but I see no other choice.
Fair enough, will place this in nf-next, thanks.
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 5/9] netfilter: conntrack: small refactoring of conntrack seq_printf
In reply to Pablo Neira AyusoQuoted 53 lines · Pablo Neira Ayuso
Quoted 52 lines · Florian Westphal
The iteration process is lockless, so we test if the conntrack object is eligible for printing (e.g. is
AF_INET) after obtaining the reference count.Once we put all conntracks into same hash table we might see more entries that need to be skipped.
So add a helper and first perform the test in a lockless fashion for fast skip.
Once we obtain the reference count, just repeat the check.
Signed-off-by Florian Westphal <[email protected]>--- .../netfilter/nf_conntrack_l3proto_ipv4_compat.c | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +16
-1@@ -114,6 +114,19 @@ static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct) }#endif +static bool ct_seq_should_skip (const struct nf_conn *ct, +const struct nf_conntrack_tuple_hash *hash) +{ +/* we only want to print DIR_ORIGINAL */ +if (NF_CT_DIRECTION (hash)) +return true; + +if (nf_ct_l3num (ct) !=AF_INET ) +return true; + +return false; +} +static int ct_seq_show (struct seq_file *s,void *v) {struct nf_conntrack_tuple_hash *hash = v;@@ -123,14 +136,15 @@ static int ct_seq_show(struct seq_file *s, void *v)int ret =0 ;NF_CT_ASSERT (ct); -if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use))) +if (ct_seq_should_skip (ct, hash))return 0 ; +if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use))) +return 0 ;
Quoted 8 lines
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c static int ct_seq_show(struct seq_file *s, void *v) · line 146
Quoted 6 lines
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +2
-4-/* we only want to print DIR_ORIGINAL */ -if (NF_CT_DIRECTION (hash)) - goto release; -if (nf_ct_l3num (ct) !=AF_INET ) +/* check if we raced w. object reuse */ + if (!nf_ct_is_confirmed(ct) ||This refactoring includes this new check, is this intentional?
It seems this check was previously missing, I can just amend the commit log with a couple of lines to document that this patch also includes this missing check. No problem.
Pablo Neira Ayuso
maintainerQuoted 14 lines · Florian Westphal
[ CCing netdev so netns folks can have a look too ]
This patch series removes the per-netns connection tracking tables. All conntrack objects are then stored in one global global table.
This avoids the infamous 'vmalloc' when lots of namespaces are used:
We no longer allocate a new conntrack table for each namespace (with 64k size this saves 512kb of memory per netns).- net namespace address is made part of conntrack hash, to spread conntracks over entire table even if netns has overlapping ip addresses.
- lookup and iteratorsnet_eq()to skip conntracks living in a different namespace.
Quoted 4 lines
Only the main conntrack table is converted here:
NAT bysrc and expectation hashes are still per namespace (will be unified in a followup series). Also, this retains the per-namespace kmem cache for the conntrack objects. This will also be resolved in a followup series.
This rework in important, I'm going to place this batch in the tree so you can keep working on this. Thanks.
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 5/9] netfilter: conntrack: small refactoring of conntrack seq_printf
In reply to Florian WestphalQuoted 13 lines · Florian Westphal
Quoted 7 lines · Pablo Neira Ayuso
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c static int ct_seq_show(struct seq_file *s, void *v) · line 146
Quoted 5 lines
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c +2
-3-if (NF_CT_DIRECTION (hash)) - goto release; -if (nf_ct_l3num (ct) !=AF_INET ) +/* check if we raced w. object reuse */ + if (!nf_ct_is_confirmed(ct) ||This refactoring includes this new check, is this intentional?
Hmm, yes and no.
I should have put it in an extra commit :-/
Quoted 7 lines
Without this, we might erronously print a conntrack that is NEW and which isn't confirmed yet.
We won't crash since
seq_printdoesn't depend on extensions being set up properly, but it seems better to only display those conntracks that are part of the conntrack hash table (i.e., have the confirmed bit set).
I see, a conntrack that shouldn't be printed be sneak in the listing.
Quoted 1 line
Let me know if you want me to respin this as a separate fix, thanks!
I will just append a notice on the commit message before applying.
Pablo Neira Ayuso
maintainerQuoted 14 lines · Florian Westphal
[ CCing netdev so netns folks can have a look too ]
This patch series removes the per-netns connection tracking tables. All conntrack objects are then stored in one global global table.
This avoids the infamous 'vmalloc' when lots of namespaces are used:
We no longer allocate a new conntrack table for each namespace (with 64k size this saves 512kb of memory per netns).- net namespace address is made part of conntrack hash, to spread conntracks over entire table even if netns has overlapping ip addresses.
- lookup and iteratorsnet_eq()to skip conntracks living in a different namespace.
Quoted 4 lines
Only the main conntrack table is converted here:
NAT bysrc and expectation hashes are still per namespace (will be unified in a followup series). Also, this retains the per-namespace kmem cache for the conntrack objects. This will also be resolved in a followup series.
Series applied, thanks Florian.
Quoted 5 lines · Florian Westphal
[ CCing netdev so netns folks can have a look too ]
This patch series removes the per-netns connection tracking tables. All conntrack objects are then stored in one global global table.
Quoted 8 lines
This avoids the infamous 'vmalloc' when lots of namespaces are used:
We no longer allocate a new conntrack table for each namespace (with 64k size this saves 512kb of memory per netns).- net namespace address is made part of conntrack hash, to spread conntracks over entire table even if netns has overlapping ip addresses.
- lookup and iteratorsnet_eq()to skip conntracks living in a different namespace.
Hi Florian,
Question on this series.
Openstack networking creates virtual routers using namespaces for isolation between users. VETH pairs are used to connect the interfaces on these routers to different networks, whether they are internal (private) or external (public).
In most cases NAT is done inside the namespace as packets move between the
networks.
I've seen cases where certain users are attacked, where the CT table is filled such that we start seeing "nf_conntrack: table full, dropping packet" messages (as expected). But other users continue to function normally, unaffected. Is this still the case - each netns has some limit it can't exceed? I didn't see it, but your comment in 9/9 seemed like something was there - "we would start to 'over-subscribe' the affected/overlimit netns".
Thanks,
-Brian
Florian Westphal
maintainerQuoted 11 lines · Brian Haley
Openstack networking creates virtual routers using namespaces for isolation between users. VETH pairs are used to connect the interfaces on these routers to different networks, whether they are internal (private) or external (public). In most cases NAT is done inside the namespace as packets move between the networks.
I've seen cases where certain users are attacked, where the CT table is filled such that we start seeing "
nf_conntrack: table full, dropping packet" messages (as expected). But other users continue to function normally, unaffected. Is this still the case - each netns has some limit it can't exceed?
The limit is global, the accounting per namespace.
If the bucket count (net.netfilter.nf_conntrack_buckets) is high enough to accomodate the expected load and noone can create arbitrary number of net namespaces things are fine.
I haven't changed the way this works yet because I did not have a better idea so far.
Quoted 7 lines · Florian Westphal
Quoted 6 lines · Brian Haley
Openstack networking creates virtual routers using namespaces for isolation between users. VETH pairs are used to connect the interfaces on these routers to different networks, whether they are internal (private) or external (public). In most cases NAT is done inside the namespace as packets move between the networks.
Quoted 7 lines
Quoted 5 lines
I've seen cases where certain users are attacked, where the CT table is filled such that we start seeing "
nf_conntrack: table full, dropping packet" messages (as expected). But other users continue to function normally, unaffected. Is this still the case - each netns has some limit it can't exceed?The limit is global, the accounting per namespace.
So this is a change from existing.
Quoted 3 lines
If the bucket count (net.netfilter.nf_conntrack_buckets) is high enough to accomodate the expected load and noone can create arbitrary number of net namespaces things are fine.
In my case we can't control the number of namespaces, each user will get one as a virtual router is created. We could change how we size things, but that doesn't stop one user from consuming larger than their 1/N share of entries. Typically we just increase the number of systems hosting these "routers" when we hit a limit, which decreases the netns count per node.
Quoted 2 lines
I haven't changed the way this works yet because I did not have a better idea so far.
Creating a per-netns maximum seems doable, but maybe not practical from the accounting side of things. Can't think of anything else at the moment.
-Brian
Florian Westphal
maintainerQuoted 9 lines · Brian Haley
Quoted 7 lines
Quoted 5 lines
I've seen cases where certain users are attacked, where the CT table is filled such that we start seeing "
nf_conntrack: table full, dropping packet" messages (as expected). But other users continue to function normally, unaffected. Is this still the case - each netns has some limit it can't exceed?The limit is global, the accounting per namespace.
So this is a change from existing.
No, see __nf_conntrack_alloc():
if (nf_conntrack_max &&
unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
...ct.count is whatever number of entries the namespace has allocated, so max number of possible conntracks is always infinite if number of net namespaces is unlimited (barring memory constraints, of course).
I did not change this.
Quoted 13 lines · Florian Westphal
Quoted 9 lines · Brian Haley
Quoted 7 lines
Quoted 5 lines
I've seen cases where certain users are attacked, where the CT table is filled such that we start seeing "
nf_conntrack: table full, dropping packet" messages (as expected). But other users continue to function normally, unaffected. Is this still the case - each netns has some limit it can't exceed?The limit is global, the accounting per namespace.
So this is a change from existing.
No, see
__nf_conntrack_alloc():
Quoted 7 lines
if (nf_conntrack_max && unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) { ...ct.count is whatever number of entries the namespace has allocated, so max number of possible conntracks is always infinite if number of net namespaces is unlimited (barring memory constraints, of course).
Ah yes, nf_conntrack_max is a global, thanks for setting me straight. So I guess the tuning might just include increasing the bucket count in order to try and keep the number of items in each one small since there will be more entries in this single table now.
Thanks,
-Brian