Skip to content

Commit e1bf168

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nat: Revert "netfilter: nat: convert nat bysrc hash to rhashtable"
This reverts commit 870190a. It was not a good idea. The custom hash table was a much better fit for this purpose. A fast lookup is not essential, in fact for most cases there is no lookup at all because original tuple is not taken and can be used as-is. What needs to be fast is insertion and deletion. rhlist removal however requires a rhlist walk. We can have thousands of entries in such a list if source port/addresses are reused for multiple flows, if this happens removal requests are so expensive that deletions of a few thousand flows can take several seconds(!). The advantages that we got from rhashtable are: 1) table auto-sizing 2) multiple locks 1) would be nice to have, but it is not essential as we have at most one lookup per new flow, so even a million flows in the bysource table are not a problem compared to current deletion cost. 2) is easy to add to custom hash table. I tried to add hlist_node to rhlist to speed up rhltable_remove but this isn't doable without changing semantics. rhltable_remove_fast will check that the to-be-deleted object is part of the table and that requires a list walk that we want to avoid. Furthermore, using hlist_node increases size of struct rhlist_head, which in turn increases nf_conn size. Link: https://bugzilla.kernel.org/show_bug.cgi?id=196821 Reported-by: Ivan Babrou <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent a5d7a71 commit e1bf168

File tree

3 files changed

+54
-80
lines changed

3 files changed

+54
-80
lines changed

include/net/netfilter/nf_conntrack.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <linux/bitops.h>
1818
#include <linux/compiler.h>
1919
#include <linux/atomic.h>
20-
#include <linux/rhashtable.h>
2120

2221
#include <linux/netfilter/nf_conntrack_tcp.h>
2322
#include <linux/netfilter/nf_conntrack_dccp.h>
@@ -77,7 +76,7 @@ struct nf_conn {
7776
possible_net_t ct_net;
7877

7978
#if IS_ENABLED(CONFIG_NF_NAT)
80-
struct rhlist_head nat_bysource;
79+
struct hlist_node nat_bysource;
8180
#endif
8281
/* all members below initialized via memset */
8382
u8 __nfct_init_offset[0];

include/net/netfilter/nf_nat.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#ifndef _NF_NAT_H
22
#define _NF_NAT_H
3-
#include <linux/rhashtable.h>
43
#include <linux/netfilter_ipv4.h>
54
#include <linux/netfilter/nf_nat.h>
65
#include <net/netfilter/nf_conntrack_tuple.h>

net/netfilter/nf_nat_core.c

Lines changed: 53 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@
3030
#include <net/netfilter/nf_conntrack_zones.h>
3131
#include <linux/netfilter/nf_nat.h>
3232

33+
static DEFINE_SPINLOCK(nf_nat_lock);
34+
3335
static DEFINE_MUTEX(nf_nat_proto_mutex);
3436
static const struct nf_nat_l3proto __rcu *nf_nat_l3protos[NFPROTO_NUMPROTO]
3537
__read_mostly;
3638
static const struct nf_nat_l4proto __rcu **nf_nat_l4protos[NFPROTO_NUMPROTO]
3739
__read_mostly;
3840

39-
struct nf_nat_conn_key {
40-
const struct net *net;
41-
const struct nf_conntrack_tuple *tuple;
42-
const struct nf_conntrack_zone *zone;
43-
};
44-
45-
static struct rhltable nf_nat_bysource_table;
41+
static struct hlist_head *nf_nat_bysource __read_mostly;
42+
static unsigned int nf_nat_htable_size __read_mostly;
43+
static unsigned int nf_nat_hash_rnd __read_mostly;
4644

4745
inline const struct nf_nat_l3proto *
4846
__nf_nat_l3proto_find(u8 family)
@@ -118,17 +116,19 @@ int nf_xfrm_me_harder(struct net *net, struct sk_buff *skb, unsigned int family)
118116
EXPORT_SYMBOL(nf_xfrm_me_harder);
119117
#endif /* CONFIG_XFRM */
120118

121-
static u32 nf_nat_bysource_hash(const void *data, u32 len, u32 seed)
119+
/* We keep an extra hash for each conntrack, for fast searching. */
120+
static unsigned int
121+
hash_by_src(const struct net *n, const struct nf_conntrack_tuple *tuple)
122122
{
123-
const struct nf_conntrack_tuple *t;
124-
const struct nf_conn *ct = data;
123+
unsigned int hash;
124+
125+
get_random_once(&nf_nat_hash_rnd, sizeof(nf_nat_hash_rnd));
125126

126-
t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
127127
/* Original src, to ensure we map it consistently if poss. */
128+
hash = jhash2((u32 *)&tuple->src, sizeof(tuple->src) / sizeof(u32),
129+
tuple->dst.protonum ^ nf_nat_hash_rnd ^ net_hash_mix(n));
128130

129-
seed ^= net_hash_mix(nf_ct_net(ct));
130-
return jhash2((const u32 *)&t->src, sizeof(t->src) / sizeof(u32),
131-
t->dst.protonum ^ seed);
131+
return reciprocal_scale(hash, nf_nat_htable_size);
132132
}
133133

134134
/* Is this tuple already taken? (not by us) */
@@ -184,28 +184,6 @@ same_src(const struct nf_conn *ct,
184184
t->src.u.all == tuple->src.u.all);
185185
}
186186

187-
static int nf_nat_bysource_cmp(struct rhashtable_compare_arg *arg,
188-
const void *obj)
189-
{
190-
const struct nf_nat_conn_key *key = arg->key;
191-
const struct nf_conn *ct = obj;
192-
193-
if (!same_src(ct, key->tuple) ||
194-
!net_eq(nf_ct_net(ct), key->net) ||
195-
!nf_ct_zone_equal(ct, key->zone, IP_CT_DIR_ORIGINAL))
196-
return 1;
197-
198-
return 0;
199-
}
200-
201-
static struct rhashtable_params nf_nat_bysource_params = {
202-
.head_offset = offsetof(struct nf_conn, nat_bysource),
203-
.obj_hashfn = nf_nat_bysource_hash,
204-
.obj_cmpfn = nf_nat_bysource_cmp,
205-
.nelem_hint = 256,
206-
.min_size = 1024,
207-
};
208-
209187
/* Only called for SRC manip */
210188
static int
211189
find_appropriate_src(struct net *net,
@@ -216,26 +194,22 @@ find_appropriate_src(struct net *net,
216194
struct nf_conntrack_tuple *result,
217195
const struct nf_nat_range *range)
218196
{
197+
unsigned int h = hash_by_src(net, tuple);
219198
const struct nf_conn *ct;
220-
struct nf_nat_conn_key key = {
221-
.net = net,
222-
.tuple = tuple,
223-
.zone = zone
224-
};
225-
struct rhlist_head *hl, *h;
226-
227-
hl = rhltable_lookup(&nf_nat_bysource_table, &key,
228-
nf_nat_bysource_params);
229199

230-
rhl_for_each_entry_rcu(ct, h, hl, nat_bysource) {
231-
nf_ct_invert_tuplepr(result,
232-
&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
233-
result->dst = tuple->dst;
234-
235-
if (in_range(l3proto, l4proto, result, range))
236-
return 1;
200+
hlist_for_each_entry_rcu(ct, &nf_nat_bysource[h], nat_bysource) {
201+
if (same_src(ct, tuple) &&
202+
net_eq(net, nf_ct_net(ct)) &&
203+
nf_ct_zone_equal(ct, zone, IP_CT_DIR_ORIGINAL)) {
204+
/* Copy source part from reply tuple. */
205+
nf_ct_invert_tuplepr(result,
206+
&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
207+
result->dst = tuple->dst;
208+
209+
if (in_range(l3proto, l4proto, result, range))
210+
return 1;
211+
}
237212
}
238-
239213
return 0;
240214
}
241215

@@ -408,6 +382,7 @@ nf_nat_setup_info(struct nf_conn *ct,
408382
const struct nf_nat_range *range,
409383
enum nf_nat_manip_type maniptype)
410384
{
385+
struct net *net = nf_ct_net(ct);
411386
struct nf_conntrack_tuple curr_tuple, new_tuple;
412387

413388
/* Can't setup nat info for confirmed ct. */
@@ -449,19 +424,14 @@ nf_nat_setup_info(struct nf_conn *ct,
449424
}
450425

451426
if (maniptype == NF_NAT_MANIP_SRC) {
452-
struct nf_nat_conn_key key = {
453-
.net = nf_ct_net(ct),
454-
.tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
455-
.zone = nf_ct_zone(ct),
456-
};
457-
int err;
458-
459-
err = rhltable_insert_key(&nf_nat_bysource_table,
460-
&key,
461-
&ct->nat_bysource,
462-
nf_nat_bysource_params);
463-
if (err)
464-
return NF_DROP;
427+
unsigned int srchash;
428+
429+
srchash = hash_by_src(net,
430+
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
431+
spin_lock_bh(&nf_nat_lock);
432+
hlist_add_head_rcu(&ct->nat_bysource,
433+
&nf_nat_bysource[srchash]);
434+
spin_unlock_bh(&nf_nat_lock);
465435
}
466436

467437
/* It's done. */
@@ -570,8 +540,9 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
570540
* will delete entry from already-freed table.
571541
*/
572542
clear_bit(IPS_SRC_NAT_DONE_BIT, &ct->status);
573-
rhltable_remove(&nf_nat_bysource_table, &ct->nat_bysource,
574-
nf_nat_bysource_params);
543+
spin_lock_bh(&nf_nat_lock);
544+
hlist_del_rcu(&ct->nat_bysource);
545+
spin_unlock_bh(&nf_nat_lock);
575546

576547
/* don't delete conntrack. Although that would make things a lot
577548
* simpler, we'd end up flushing all conntracks on nat rmmod.
@@ -699,9 +670,11 @@ EXPORT_SYMBOL_GPL(nf_nat_l3proto_unregister);
699670
/* No one using conntrack by the time this called. */
700671
static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
701672
{
702-
if (ct->status & IPS_SRC_NAT_DONE)
703-
rhltable_remove(&nf_nat_bysource_table, &ct->nat_bysource,
704-
nf_nat_bysource_params);
673+
if (ct->status & IPS_SRC_NAT_DONE) {
674+
spin_lock_bh(&nf_nat_lock);
675+
hlist_del_rcu(&ct->nat_bysource);
676+
spin_unlock_bh(&nf_nat_lock);
677+
}
705678
}
706679

707680
static struct nf_ct_ext_type nat_extend __read_mostly = {
@@ -825,13 +798,16 @@ static int __init nf_nat_init(void)
825798
{
826799
int ret;
827800

828-
ret = rhltable_init(&nf_nat_bysource_table, &nf_nat_bysource_params);
829-
if (ret)
830-
return ret;
801+
/* Leave them the same for the moment. */
802+
nf_nat_htable_size = nf_conntrack_htable_size;
803+
804+
nf_nat_bysource = nf_ct_alloc_hashtable(&nf_nat_htable_size, 0);
805+
if (!nf_nat_bysource)
806+
return -ENOMEM;
831807

832808
ret = nf_ct_extend_register(&nat_extend);
833809
if (ret < 0) {
834-
rhltable_destroy(&nf_nat_bysource_table);
810+
nf_ct_free_hashtable(nf_nat_bysource, nf_nat_htable_size);
835811
printk(KERN_ERR "nf_nat_core: Unable to register extension\n");
836812
return ret;
837813
}
@@ -865,8 +841,8 @@ static void __exit nf_nat_cleanup(void)
865841

866842
for (i = 0; i < NFPROTO_NUMPROTO; i++)
867843
kfree(nf_nat_l4protos[i]);
868-
869-
rhltable_destroy(&nf_nat_bysource_table);
844+
synchronize_net();
845+
nf_ct_free_hashtable(nf_nat_bysource, nf_nat_htable_size);
870846
}
871847

872848
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)