Skip to content

Commit

Permalink
ipv4: split inet_ehashfn to hash functions per compilation unit
Browse files Browse the repository at this point in the history
This duplicates a bit of code but let's us easily introduce
separate secret keys later. The separate compilation units are
ipv4/inet_hashtabbles.o, ipv4/udp.o and rds/connection.o.

Cc: Eric Dumazet <[email protected]>
Cc: "David S. Miller" <[email protected]>
Signed-off-by: Hannes Frederic Sowa <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
strssndktn authored and davem330 committed Oct 19, 2013
1 parent 53481da commit 65cd803
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
22 changes: 6 additions & 16 deletions include/net/inet_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,16 @@ extern u32 inet_ehash_secret;
extern u32 ipv6_hash_secret;
void build_ehash_secret(void);

static inline unsigned int inet_ehashfn(struct net *net,
const __be32 laddr, const __u16 lport,
const __be32 faddr, const __be16 fport)
static inline unsigned int __inet_ehashfn(const __be32 laddr,
const __u16 lport,
const __be32 faddr,
const __be16 fport,
u32 initval)
{
return jhash_3words((__force __u32) laddr,
(__force __u32) faddr,
((__u32) lport) << 16 | (__force __u32)fport,
inet_ehash_secret + net_hash_mix(net));
}

static inline int inet_sk_ehashfn(const struct sock *sk)
{
const struct inet_sock *inet = inet_sk(sk);
const __be32 laddr = inet->inet_rcv_saddr;
const __u16 lport = inet->inet_num;
const __be32 faddr = inet->inet_daddr;
const __be16 fport = inet->inet_dport;
struct net *net = sock_net(sk);

return inet_ehashfn(net, laddr, lport, faddr, fport);
initval);
}

static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops)
Expand Down
21 changes: 21 additions & 0 deletions net/ipv4/inet_hashtables.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@
#include <net/secure_seq.h>
#include <net/ip.h>

static unsigned int inet_ehashfn(struct net *net, const __be32 laddr,
const __u16 lport, const __be32 faddr,
const __be16 fport)
{
return __inet_ehashfn(laddr, lport, faddr, fport,
inet_ehash_secret + net_hash_mix(net));
}


static unsigned int inet_sk_ehashfn(const struct sock *sk)
{
const struct inet_sock *inet = inet_sk(sk);
const __be32 laddr = inet->inet_rcv_saddr;
const __u16 lport = inet->inet_num;
const __be32 faddr = inet->inet_daddr;
const __be16 fport = inet->inet_dport;
struct net *net = sock_net(sk);

return inet_ehashfn(net, laddr, lport, faddr, fport);
}

/*
* Allocate and initialize a new local port bind bucket.
* The bindhash mutex for snum's hash chain must be held here.
Expand Down
16 changes: 12 additions & 4 deletions net/ipv4/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ static inline int compute_score2(struct sock *sk, struct net *net,
return score;
}

static unsigned int udp_ehashfn(struct net *net, const __be32 laddr,
const __u16 lport, const __be32 faddr,
const __be16 fport)
{
return __inet_ehashfn(laddr, lport, faddr, fport,
inet_ehash_secret + net_hash_mix(net));
}


/* called with read_rcu_lock() */
static struct sock *udp4_lib_lookup2(struct net *net,
Expand All @@ -430,8 +438,8 @@ static struct sock *udp4_lib_lookup2(struct net *net,
badness = score;
reuseport = sk->sk_reuseport;
if (reuseport) {
hash = inet_ehashfn(net, daddr, hnum,
saddr, sport);
hash = udp_ehashfn(net, daddr, hnum,
saddr, sport);
matches = 1;
}
} else if (score == badness && reuseport) {
Expand Down Expand Up @@ -511,8 +519,8 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
badness = score;
reuseport = sk->sk_reuseport;
if (reuseport) {
hash = inet_ehashfn(net, daddr, hnum,
saddr, sport);
hash = udp_ehashfn(net, daddr, hnum,
saddr, sport);
matches = 1;
}
} else if (score == badness && reuseport) {
Expand Down
6 changes: 3 additions & 3 deletions net/rds/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ static struct kmem_cache *rds_conn_slab;
static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
{
/* Pass NULL, don't need struct net for hash */
unsigned long hash = inet_ehashfn(NULL,
be32_to_cpu(laddr), 0,
be32_to_cpu(faddr), 0);
unsigned long hash = __inet_ehashfn(be32_to_cpu(laddr), 0,
be32_to_cpu(faddr), 0,
inet_ehash_secret);
return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
}

Expand Down

0 comments on commit 65cd803

Please sign in to comment.