Commit 7629c73a authored by Jakub Sitnicki's avatar Jakub Sitnicki Committed by Alexei Starovoitov

udp: Extract helper for selecting socket from reuseport group

Prepare for calling into reuseport from __udp4_lib_lookup as well.
Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200717103536.397595-8-jakub@cloudflare.com
parent 1122702f
...@@ -408,6 +408,25 @@ static u32 udp_ehashfn(const struct net *net, const __be32 laddr, ...@@ -408,6 +408,25 @@ static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
udp_ehash_secret + net_hash_mix(net)); udp_ehash_secret + net_hash_mix(net));
} }
static inline struct sock *lookup_reuseport(struct net *net, struct sock *sk,
struct sk_buff *skb,
__be32 saddr, __be16 sport,
__be32 daddr, unsigned short hnum)
{
struct sock *reuse_sk = NULL;
u32 hash;
if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
hash = udp_ehashfn(net, daddr, hnum, saddr, sport);
reuse_sk = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr));
/* Fall back to scoring if group has connections */
if (reuseport_has_conns(sk, false))
return NULL;
}
return reuse_sk;
}
/* called with rcu_read_lock() */ /* called with rcu_read_lock() */
static struct sock *udp4_lib_lookup2(struct net *net, static struct sock *udp4_lib_lookup2(struct net *net,
__be32 saddr, __be16 sport, __be32 saddr, __be16 sport,
...@@ -418,7 +437,6 @@ static struct sock *udp4_lib_lookup2(struct net *net, ...@@ -418,7 +437,6 @@ static struct sock *udp4_lib_lookup2(struct net *net,
{ {
struct sock *sk, *result; struct sock *sk, *result;
int score, badness; int score, badness;
u32 hash = 0;
result = NULL; result = NULL;
badness = 0; badness = 0;
...@@ -426,15 +444,11 @@ static struct sock *udp4_lib_lookup2(struct net *net, ...@@ -426,15 +444,11 @@ static struct sock *udp4_lib_lookup2(struct net *net,
score = compute_score(sk, net, saddr, sport, score = compute_score(sk, net, saddr, sport,
daddr, hnum, dif, sdif); daddr, hnum, dif, sdif);
if (score > badness) { if (score > badness) {
if (sk->sk_reuseport && result = lookup_reuseport(net, sk, skb,
sk->sk_state != TCP_ESTABLISHED) { saddr, sport, daddr, hnum);
hash = udp_ehashfn(net, daddr, hnum, if (result)
saddr, sport);
result = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr));
if (result && !reuseport_has_conns(sk, false))
return result; return result;
}
badness = score; badness = score;
result = sk; result = sk;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment