Skip to content

Commit d2c806a

Browse files
Florian Westphalummakynes
Florian Westphal
authored andcommitted
netfilter: conntrack: use siphash_4u64
This function is used for every packet, siphash_4u64 is noticeably faster than using local buffer + siphash: Before: 1.23% kpktgend_0 [kernel.vmlinux] [k] __siphash_unaligned 0.14% kpktgend_0 [nf_conntrack] [k] hash_conntrack_raw After: 0.79% kpktgend_0 [kernel.vmlinux] [k] siphash_4u64 0.15% kpktgend_0 [nf_conntrack] [k] hash_conntrack_raw In the pktgen test this gives about ~2.4% performance improvement. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 971095c commit d2c806a

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

net/netfilter/nf_conntrack_core.c

+13-17
Original file line numberDiff line numberDiff line change
@@ -211,28 +211,24 @@ static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
211211
unsigned int zoneid,
212212
const struct net *net)
213213
{
214-
struct {
215-
struct nf_conntrack_man src;
216-
union nf_inet_addr dst_addr;
217-
unsigned int zone;
218-
u32 net_mix;
219-
u16 dport;
220-
u16 proto;
221-
} __aligned(SIPHASH_ALIGNMENT) combined;
214+
u64 a, b, c, d;
222215

223216
get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
224217

225-
memset(&combined, 0, sizeof(combined));
218+
/* The direction must be ignored, handle usable tuplehash members manually */
219+
a = (u64)tuple->src.u3.all[0] << 32 | tuple->src.u3.all[3];
220+
b = (u64)tuple->dst.u3.all[0] << 32 | tuple->dst.u3.all[3];
226221

227-
/* The direction must be ignored, so handle usable members manually. */
228-
combined.src = tuple->src;
229-
combined.dst_addr = tuple->dst.u3;
230-
combined.zone = zoneid;
231-
combined.net_mix = net_hash_mix(net);
232-
combined.dport = (__force __u16)tuple->dst.u.all;
233-
combined.proto = tuple->dst.protonum;
222+
c = (__force u64)tuple->src.u.all << 32 | (__force u64)tuple->dst.u.all << 16;
223+
c |= tuple->dst.protonum;
234224

235-
return (u32)siphash(&combined, sizeof(combined), &nf_conntrack_hash_rnd);
225+
d = (u64)zoneid << 32 | net_hash_mix(net);
226+
227+
/* IPv4: u3.all[1,2,3] == 0 */
228+
c ^= (u64)tuple->src.u3.all[1] << 32 | tuple->src.u3.all[2];
229+
d += (u64)tuple->dst.u3.all[1] << 32 | tuple->dst.u3.all[2];
230+
231+
return (u32)siphash_4u64(a, b, c, d, &nf_conntrack_hash_rnd);
236232
}
237233

238234
static u32 scale_hash(u32 hash)

0 commit comments

Comments
 (0)