Skip to content

Commit 7c96643

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: move nat hlist_head to nf_conn
The nat extension structure is 32bytes in size on x86_64: struct nf_conn_nat { struct hlist_node bysource; /* 0 16 */ struct nf_conn * ct; /* 16 8 */ union nf_conntrack_nat_help help; /* 24 4 */ int masq_index; /* 28 4 */ /* size: 32, cachelines: 1, members: 4 */ /* last cacheline: 32 bytes */ }; The hlist is needed to quickly check for possible tuple collisions when installing a new nat binding. Storing this in the extension area has two drawbacks: 1. We need ct backpointer to get the conntrack struct from the extension. 2. When reallocation of extension area occurs we need to fixup the bysource hash head via hlist_replace_rcu. We can avoid both by placing the hlist_head in nf_conn and place nf_conn in the bysource hash rather than the extenstion. We can also remove the ->move support; no other extension needs it. Moving the entire nat extension into nf_conn would be possible as well but then we have to add yet another callback for deletion from the bysource hash table rather than just using nat extension ->destroy hook for this. nf_conn size doesn't increase due to aligment, followup patch replaces hlist_node with single pointer. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 242922a commit 7c96643

File tree

5 files changed

+12
-44
lines changed

5 files changed

+12
-44
lines changed

include/net/netfilter/nf_conntrack.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ struct nf_conn {
117117
/* Extensions */
118118
struct nf_ct_ext *ext;
119119

120+
#if IS_ENABLED(CONFIG_NF_NAT)
121+
struct hlist_node nat_bysource;
122+
#endif
120123
/* Storage reserved for other modules, must be the last member */
121124
union nf_conntrack_proto proto;
122125
};

include/net/netfilter/nf_conntrack_extend.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
9999
struct nf_ct_ext_type {
100100
/* Destroys relationships (can be NULL). */
101101
void (*destroy)(struct nf_conn *ct);
102-
/* Called when realloacted (can be NULL).
103-
Contents has already been moved. */
104-
void (*move)(void *new, void *old);
105102

106103
enum nf_ct_ext_id id;
107104

include/net/netfilter/nf_nat.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ struct nf_conn;
2929

3030
/* The structure embedded in the conntrack structure. */
3131
struct nf_conn_nat {
32-
struct hlist_node bysource;
33-
struct nf_conn *ct;
3432
union nf_conntrack_nat_help help;
3533
#if IS_ENABLED(CONFIG_NF_NAT_MASQUERADE_IPV4) || \
3634
IS_ENABLED(CONFIG_NF_NAT_MASQUERADE_IPV6)

net/netfilter/nf_conntrack_extend.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
7373
size_t var_alloc_len, gfp_t gfp)
7474
{
7575
struct nf_ct_ext *old, *new;
76-
int i, newlen, newoff;
76+
int newlen, newoff;
7777
struct nf_ct_ext_type *t;
7878

7979
/* Conntrack must not be confirmed to avoid races on reallocation. */
@@ -99,19 +99,8 @@ void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
9999
return NULL;
100100

101101
if (new != old) {
102-
for (i = 0; i < NF_CT_EXT_NUM; i++) {
103-
if (!__nf_ct_ext_exist(old, i))
104-
continue;
105-
106-
rcu_read_lock();
107-
t = rcu_dereference(nf_ct_ext_types[i]);
108-
if (t && t->move)
109-
t->move((void *)new + new->offset[i],
110-
(void *)old + old->offset[i]);
111-
rcu_read_unlock();
112-
}
113102
kfree_rcu(old, rcu);
114-
ct->ext = new;
103+
rcu_assign_pointer(ct->ext, new);
115104
}
116105

117106
new->offset[id] = newoff;

net/netfilter/nf_nat_core.c

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ find_appropriate_src(struct net *net,
198198
const struct nf_nat_range *range)
199199
{
200200
unsigned int h = hash_by_src(net, tuple);
201-
const struct nf_conn_nat *nat;
202201
const struct nf_conn *ct;
203202

204-
hlist_for_each_entry_rcu(nat, &nf_nat_bysource[h], bysource) {
205-
ct = nat->ct;
203+
hlist_for_each_entry_rcu(ct, &nf_nat_bysource[h], nat_bysource) {
206204
if (same_src(ct, tuple) &&
207205
net_eq(net, nf_ct_net(ct)) &&
208206
nf_ct_zone_equal(ct, zone, IP_CT_DIR_ORIGINAL)) {
@@ -435,8 +433,7 @@ nf_nat_setup_info(struct nf_conn *ct,
435433
spin_lock_bh(&nf_nat_lock);
436434
/* nf_conntrack_alter_reply might re-allocate extension aera */
437435
nat = nfct_nat(ct);
438-
nat->ct = ct;
439-
hlist_add_head_rcu(&nat->bysource,
436+
hlist_add_head_rcu(&ct->nat_bysource,
440437
&nf_nat_bysource[srchash]);
441438
spin_unlock_bh(&nf_nat_lock);
442439
}
@@ -543,7 +540,7 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
543540
if (nf_nat_proto_remove(ct, data))
544541
return 1;
545542

546-
if (!nat || !nat->ct)
543+
if (!nat)
547544
return 0;
548545

549546
/* This netns is being destroyed, and conntrack has nat null binding.
@@ -556,9 +553,8 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
556553
return 1;
557554

558555
spin_lock_bh(&nf_nat_lock);
559-
hlist_del_rcu(&nat->bysource);
556+
hlist_del_rcu(&ct->nat_bysource);
560557
ct->status &= ~IPS_NAT_DONE_MASK;
561-
nat->ct = NULL;
562558
spin_unlock_bh(&nf_nat_lock);
563559

564560
add_timer(&ct->timeout);
@@ -688,35 +684,20 @@ static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
688684
{
689685
struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT);
690686

691-
if (nat == NULL || nat->ct == NULL)
687+
if (!nat)
692688
return;
693689

694-
NF_CT_ASSERT(nat->ct->status & IPS_SRC_NAT_DONE);
695-
696-
spin_lock_bh(&nf_nat_lock);
697-
hlist_del_rcu(&nat->bysource);
698-
spin_unlock_bh(&nf_nat_lock);
699-
}
700-
701-
static void nf_nat_move_storage(void *new, void *old)
702-
{
703-
struct nf_conn_nat *new_nat = new;
704-
struct nf_conn_nat *old_nat = old;
705-
struct nf_conn *ct = old_nat->ct;
706-
707-
if (!ct || !(ct->status & IPS_SRC_NAT_DONE))
708-
return;
690+
NF_CT_ASSERT(ct->status & IPS_SRC_NAT_DONE);
709691

710692
spin_lock_bh(&nf_nat_lock);
711-
hlist_replace_rcu(&old_nat->bysource, &new_nat->bysource);
693+
hlist_del_rcu(&ct->nat_bysource);
712694
spin_unlock_bh(&nf_nat_lock);
713695
}
714696

715697
static struct nf_ct_ext_type nat_extend __read_mostly = {
716698
.len = sizeof(struct nf_conn_nat),
717699
.align = __alignof__(struct nf_conn_nat),
718700
.destroy = nf_nat_cleanup_conntrack,
719-
.move = nf_nat_move_storage,
720701
.id = NF_CT_EXT_NAT,
721702
.flags = NF_CT_EXT_F_PREALLOC,
722703
};

0 commit comments

Comments
 (0)