Skip to content

Commit 870190a

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nat: convert nat bysrc hash to rhashtable
It did use a fixed-size bucket list plus single lock to protect add/del. Unlike the main conntrack table we only need to add and remove keys. Convert it to rhashtable to get table autosizing and per-bucket locking. The maximum number of entries is -- as before -- tied to the number of conntracks so we do not need another upperlimit. The change does not handle rhashtable_remove_fast error, only possible "error" is -ENOENT, and that is something that can happen legitimetely, e.g. because nat module was inserted at a later time and no src manip took place yet. Tested with http-client-benchmark + httpterm with DNAT and SNAT rules in place. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 7c96643 commit 870190a

File tree

3 files changed

+71
-59
lines changed

3 files changed

+71
-59
lines changed

include/net/netfilter/nf_conntrack.h

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

2122
#include <linux/netfilter/nf_conntrack_tcp.h>
2223
#include <linux/netfilter/nf_conntrack_dccp.h>
@@ -118,7 +119,7 @@ struct nf_conn {
118119
struct nf_ct_ext *ext;
119120

120121
#if IS_ENABLED(CONFIG_NF_NAT)
121-
struct hlist_node nat_bysource;
122+
struct rhash_head nat_bysource;
122123
#endif
123124
/* Storage reserved for other modules, must be the last member */
124125
union nf_conntrack_proto proto;

include/net/netfilter/nf_nat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef _NF_NAT_H
22
#define _NF_NAT_H
3+
#include <linux/rhashtable.h>
34
#include <linux/netfilter_ipv4.h>
45
#include <linux/netfilter/nf_nat.h>
56
#include <net/netfilter/nf_conntrack_tuple.h>

net/netfilter/nf_nat_core.c

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

33-
static DEFINE_SPINLOCK(nf_nat_lock);
34-
3533
static DEFINE_MUTEX(nf_nat_proto_mutex);
3634
static const struct nf_nat_l3proto __rcu *nf_nat_l3protos[NFPROTO_NUMPROTO]
3735
__read_mostly;
3836
static const struct nf_nat_l4proto __rcu **nf_nat_l4protos[NFPROTO_NUMPROTO]
3937
__read_mostly;
4038

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;
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 rhashtable nf_nat_bysource_table;
4446

4547
inline const struct nf_nat_l3proto *
4648
__nf_nat_l3proto_find(u8 family)
@@ -119,19 +121,17 @@ int nf_xfrm_me_harder(struct net *net, struct sk_buff *skb, unsigned int family)
119121
EXPORT_SYMBOL(nf_xfrm_me_harder);
120122
#endif /* CONFIG_XFRM */
121123

122-
/* We keep an extra hash for each conntrack, for fast searching. */
123-
static inline unsigned int
124-
hash_by_src(const struct net *n, const struct nf_conntrack_tuple *tuple)
124+
static u32 nf_nat_bysource_hash(const void *data, u32 len, u32 seed)
125125
{
126-
unsigned int hash;
127-
128-
get_random_once(&nf_nat_hash_rnd, sizeof(nf_nat_hash_rnd));
126+
const struct nf_conntrack_tuple *t;
127+
const struct nf_conn *ct = data;
129128

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

134-
return reciprocal_scale(hash, nf_nat_htable_size);
132+
seed ^= net_hash_mix(nf_ct_net(ct));
133+
return jhash2((const u32 *)&t->src, sizeof(t->src) / sizeof(u32),
134+
t->dst.protonum ^ seed);
135135
}
136136

137137
/* Is this tuple already taken? (not by us) */
@@ -187,6 +187,26 @@ same_src(const struct nf_conn *ct,
187187
t->src.u.all == tuple->src.u.all);
188188
}
189189

190+
static int nf_nat_bysource_cmp(struct rhashtable_compare_arg *arg,
191+
const void *obj)
192+
{
193+
const struct nf_nat_conn_key *key = arg->key;
194+
const struct nf_conn *ct = obj;
195+
196+
return same_src(ct, key->tuple) &&
197+
net_eq(nf_ct_net(ct), key->net) &&
198+
nf_ct_zone_equal(ct, key->zone, IP_CT_DIR_ORIGINAL);
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+
.nulls_base = (1U << RHT_BASE_SHIFT),
208+
};
209+
190210
/* Only called for SRC manip */
191211
static int
192212
find_appropriate_src(struct net *net,
@@ -197,23 +217,23 @@ find_appropriate_src(struct net *net,
197217
struct nf_conntrack_tuple *result,
198218
const struct nf_nat_range *range)
199219
{
200-
unsigned int h = hash_by_src(net, tuple);
201220
const struct nf_conn *ct;
221+
struct nf_nat_conn_key key = {
222+
.net = net,
223+
.tuple = tuple,
224+
.zone = zone
225+
};
202226

203-
hlist_for_each_entry_rcu(ct, &nf_nat_bysource[h], nat_bysource) {
204-
if (same_src(ct, tuple) &&
205-
net_eq(net, nf_ct_net(ct)) &&
206-
nf_ct_zone_equal(ct, zone, IP_CT_DIR_ORIGINAL)) {
207-
/* Copy source part from reply tuple. */
208-
nf_ct_invert_tuplepr(result,
209-
&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
210-
result->dst = tuple->dst;
211-
212-
if (in_range(l3proto, l4proto, result, range))
213-
return 1;
214-
}
215-
}
216-
return 0;
227+
ct = rhashtable_lookup_fast(&nf_nat_bysource_table, &key,
228+
nf_nat_bysource_params);
229+
if (!ct)
230+
return 0;
231+
232+
nf_ct_invert_tuplepr(result,
233+
&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
234+
result->dst = tuple->dst;
235+
236+
return in_range(l3proto, l4proto, result, range);
217237
}
218238

219239
/* For [FUTURE] fragmentation handling, we want the least-used
@@ -385,7 +405,6 @@ nf_nat_setup_info(struct nf_conn *ct,
385405
const struct nf_nat_range *range,
386406
enum nf_nat_manip_type maniptype)
387407
{
388-
struct net *net = nf_ct_net(ct);
389408
struct nf_conntrack_tuple curr_tuple, new_tuple;
390409
struct nf_conn_nat *nat;
391410

@@ -426,16 +445,13 @@ nf_nat_setup_info(struct nf_conn *ct,
426445
}
427446

428447
if (maniptype == NF_NAT_MANIP_SRC) {
429-
unsigned int srchash;
430-
431-
srchash = hash_by_src(net,
432-
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
433-
spin_lock_bh(&nf_nat_lock);
434-
/* nf_conntrack_alter_reply might re-allocate extension aera */
435-
nat = nfct_nat(ct);
436-
hlist_add_head_rcu(&ct->nat_bysource,
437-
&nf_nat_bysource[srchash]);
438-
spin_unlock_bh(&nf_nat_lock);
448+
int err;
449+
450+
err = rhashtable_insert_fast(&nf_nat_bysource_table,
451+
&ct->nat_bysource,
452+
nf_nat_bysource_params);
453+
if (err)
454+
return NF_DROP;
439455
}
440456

441457
/* It's done. */
@@ -552,10 +568,10 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
552568
if (!del_timer(&ct->timeout))
553569
return 1;
554570

555-
spin_lock_bh(&nf_nat_lock);
556-
hlist_del_rcu(&ct->nat_bysource);
557571
ct->status &= ~IPS_NAT_DONE_MASK;
558-
spin_unlock_bh(&nf_nat_lock);
572+
573+
rhashtable_remove_fast(&nf_nat_bysource_table, &ct->nat_bysource,
574+
nf_nat_bysource_params);
559575

560576
add_timer(&ct->timeout);
561577

@@ -687,11 +703,8 @@ static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
687703
if (!nat)
688704
return;
689705

690-
NF_CT_ASSERT(ct->status & IPS_SRC_NAT_DONE);
691-
692-
spin_lock_bh(&nf_nat_lock);
693-
hlist_del_rcu(&ct->nat_bysource);
694-
spin_unlock_bh(&nf_nat_lock);
706+
rhashtable_remove_fast(&nf_nat_bysource_table, &ct->nat_bysource,
707+
nf_nat_bysource_params);
695708
}
696709

697710
static struct nf_ct_ext_type nat_extend __read_mostly = {
@@ -826,16 +839,13 @@ static int __init nf_nat_init(void)
826839
{
827840
int ret;
828841

829-
/* Leave them the same for the moment. */
830-
nf_nat_htable_size = nf_conntrack_htable_size;
831-
832-
nf_nat_bysource = nf_ct_alloc_hashtable(&nf_nat_htable_size, 0);
833-
if (!nf_nat_bysource)
834-
return -ENOMEM;
842+
ret = rhashtable_init(&nf_nat_bysource_table, &nf_nat_bysource_params);
843+
if (ret)
844+
return ret;
835845

836846
ret = nf_ct_extend_register(&nat_extend);
837847
if (ret < 0) {
838-
nf_ct_free_hashtable(nf_nat_bysource, nf_nat_htable_size);
848+
rhashtable_destroy(&nf_nat_bysource_table);
839849
printk(KERN_ERR "nf_nat_core: Unable to register extension\n");
840850
return ret;
841851
}
@@ -859,7 +869,7 @@ static int __init nf_nat_init(void)
859869
return 0;
860870

861871
cleanup_extend:
862-
nf_ct_free_hashtable(nf_nat_bysource, nf_nat_htable_size);
872+
rhashtable_destroy(&nf_nat_bysource_table);
863873
nf_ct_extend_unregister(&nat_extend);
864874
return ret;
865875
}
@@ -877,8 +887,8 @@ static void __exit nf_nat_cleanup(void)
877887
#endif
878888
for (i = 0; i < NFPROTO_NUMPROTO; i++)
879889
kfree(nf_nat_l4protos[i]);
880-
synchronize_net();
881-
nf_ct_free_hashtable(nf_nat_bysource, nf_nat_htable_size);
890+
891+
rhashtable_destroy(&nf_nat_bysource_table);
882892
}
883893

884894
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)