Skip to content

Commit 0b44191

Browse files
xemuldavem330
authored andcommitted
netns: introduce the net_hash_mix "salt" for hashes
There are many possible ways to add this "salt", thus I made this patch to be the last in the series to change it if required. Currently I propose to use the struct net pointer itself as this salt, but since this pointer is most often cache-line aligned, shift this right to eliminate the bits, that are most often zeroed. After this, simply add this mix to prepared hashfn-s. For CONFIG_NET_NS=n case this salt is 0 and no changes in hashfn appear. Signed-off-by: Pavel Emelyanov <xemul@openvz.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 33de014 commit 0b44191

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

include/linux/udp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct udphdr {
3838
#ifdef __KERNEL__
3939
#include <net/inet_sock.h>
4040
#include <linux/skbuff.h>
41+
#include <net/netns/hash.h>
4142

4243
static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
4344
{
@@ -48,7 +49,7 @@ static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
4849

4950
static inline int udp_hashfn(struct net *net, const unsigned num)
5051
{
51-
return num & (UDP_HTABLE_SIZE - 1);
52+
return (num + net_hash_mix(net)) & (UDP_HTABLE_SIZE - 1);
5253
}
5354

5455
struct udp_sock {

include/net/inet6_hashtables.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <net/inet_sock.h>
2525

2626
#include <net/ipv6.h>
27+
#include <net/netns/hash.h>
2728

2829
struct inet_hashinfo;
2930

@@ -36,7 +37,7 @@ static inline unsigned int inet6_ehashfn(struct net *net,
3637

3738
return jhash_3words((__force u32)laddr->s6_addr32[3],
3839
(__force u32)faddr->s6_addr32[3],
39-
ports, inet_ehash_secret);
40+
ports, inet_ehash_secret + net_hash_mix(net));
4041
}
4142

4243
static inline int inet6_sk_ehashfn(const struct sock *sk)

include/net/inet_hashtables.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <net/inet_sock.h>
3030
#include <net/sock.h>
3131
#include <net/tcp_states.h>
32+
#include <net/netns/hash.h>
3233

3334
#include <asm/atomic.h>
3435
#include <asm/byteorder.h>
@@ -204,7 +205,7 @@ extern void inet_bind_bucket_destroy(struct kmem_cache *cachep,
204205
static inline int inet_bhashfn(struct net *net,
205206
const __u16 lport, const int bhash_size)
206207
{
207-
return lport & (bhash_size - 1);
208+
return (lport + net_hash_mix(net)) & (bhash_size - 1);
208209
}
209210

210211
extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
@@ -213,7 +214,7 @@ extern void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
213214
/* These can have wildcards, don't try too hard. */
214215
static inline int inet_lhashfn(struct net *net, const unsigned short num)
215216
{
216-
return num & (INET_LHTABLE_SIZE - 1);
217+
return (num + net_hash_mix(net)) & (INET_LHTABLE_SIZE - 1);
217218
}
218219

219220
static inline int inet_sk_listen_hashfn(const struct sock *sk)

include/net/inet_sock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <net/sock.h>
2626
#include <net/request_sock.h>
2727
#include <net/route.h>
28+
#include <net/netns/hash.h>
2829

2930
/** struct ip_options - IP Options
3031
*
@@ -178,7 +179,7 @@ static inline unsigned int inet_ehashfn(struct net *net,
178179
return jhash_3words((__force __u32) laddr,
179180
(__force __u32) faddr,
180181
((__u32) lport) << 16 | (__force __u32)fport,
181-
inet_ehash_secret);
182+
inet_ehash_secret + net_hash_mix(net));
182183
}
183184

184185
static inline int inet_sk_ehashfn(const struct sock *sk)

include/net/netns/hash.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __NET_NS_HASH_H__
2+
#define __NET_NS_HASH_H__
3+
4+
#include <asm/cache.h>
5+
6+
struct net;
7+
8+
static inline unsigned net_hash_mix(struct net *net)
9+
{
10+
#ifdef CONFIG_NET_NS
11+
/*
12+
* shift this right to eliminate bits, that are
13+
* always zeroed
14+
*/
15+
16+
return (unsigned)(((unsigned long)net) >> L1_CACHE_SHIFT);
17+
#else
18+
return 0;
19+
#endif
20+
}
21+
#endif

0 commit comments

Comments
 (0)