Commit 325ac51b authored by Kuniyuki Iwashima's avatar Kuniyuki Iwashima Committed by Greg Kroah-Hartman

udp: Improve load balancing for SO_REUSEPORT.

[ Upstream commit efc6b6f6 ]

Currently, SO_REUSEPORT does not work well if connected sockets are in a
UDP reuseport group.

Then reuseport_has_conns() returns true and the result of
reuseport_select_sock() is discarded. Also, unconnected sockets have the
same score, hence only does the first unconnected socket in udp_hslot
always receive all packets sent to unconnected sockets.

So, the result of reuseport_select_sock() should be used for load
balancing.

The noteworthy point is that the unconnected sockets placed after
connected sockets in sock_reuseport.socks will receive more packets than
others because of the algorithm in reuseport_select_sock().

    index | connected | reciprocal_scale | result
    ---------------------------------------------
    0     | no        | 20%              | 40%
    1     | no        | 20%              | 20%
    2     | yes       | 20%              | 0%
    3     | no        | 20%              | 40%
    4     | yes       | 20%              | 0%

If most of the sockets are connected, this can be a problem, but it still
works better than now.

Fixes: acdcecc6 ("udp: correct reuseport selection with connected sockets")
CC: Willem de Bruijn <willemb@google.com>
Reviewed-by: default avatarBenjamin Herrenschmidt <benh@amazon.com>
Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.co.jp>
Acked-by: default avatarWillem de Bruijn <willemb@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 0f26f967
...@@ -433,7 +433,7 @@ static struct sock *udp4_lib_lookup2(struct net *net, ...@@ -433,7 +433,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
struct udp_hslot *hslot2, struct udp_hslot *hslot2,
struct sk_buff *skb) struct sk_buff *skb)
{ {
struct sock *sk, *result; struct sock *sk, *result, *reuseport_result;
int score, badness; int score, badness;
u32 hash = 0; u32 hash = 0;
...@@ -443,17 +443,20 @@ static struct sock *udp4_lib_lookup2(struct net *net, ...@@ -443,17 +443,20 @@ 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, exact_dif); daddr, hnum, dif, sdif, exact_dif);
if (score > badness) { if (score > badness) {
reuseport_result = NULL;
if (sk->sk_reuseport && if (sk->sk_reuseport &&
sk->sk_state != TCP_ESTABLISHED) { sk->sk_state != TCP_ESTABLISHED) {
hash = udp_ehashfn(net, daddr, hnum, hash = udp_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
result = reuseport_select_sock(sk, hash, skb, reuseport_result = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr)); sizeof(struct udphdr));
if (result && !reuseport_has_conns(sk, false)) if (reuseport_result && !reuseport_has_conns(sk, false))
return result; return reuseport_result;
} }
result = reuseport_result ? : sk;
badness = score; badness = score;
result = sk;
} }
} }
return result; return result;
......
...@@ -167,7 +167,7 @@ static struct sock *udp6_lib_lookup2(struct net *net, ...@@ -167,7 +167,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
int dif, int sdif, bool exact_dif, int dif, int sdif, bool exact_dif,
struct udp_hslot *hslot2, struct sk_buff *skb) struct udp_hslot *hslot2, struct sk_buff *skb)
{ {
struct sock *sk, *result; struct sock *sk, *result, *reuseport_result;
int score, badness; int score, badness;
u32 hash = 0; u32 hash = 0;
...@@ -177,17 +177,20 @@ static struct sock *udp6_lib_lookup2(struct net *net, ...@@ -177,17 +177,20 @@ static struct sock *udp6_lib_lookup2(struct net *net,
score = compute_score(sk, net, saddr, sport, score = compute_score(sk, net, saddr, sport,
daddr, hnum, dif, sdif, exact_dif); daddr, hnum, dif, sdif, exact_dif);
if (score > badness) { if (score > badness) {
reuseport_result = NULL;
if (sk->sk_reuseport && if (sk->sk_reuseport &&
sk->sk_state != TCP_ESTABLISHED) { sk->sk_state != TCP_ESTABLISHED) {
hash = udp6_ehashfn(net, daddr, hnum, hash = udp6_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
result = reuseport_select_sock(sk, hash, skb, reuseport_result = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr)); sizeof(struct udphdr));
if (result && !reuseport_has_conns(sk, false)) if (reuseport_result && !reuseport_has_conns(sk, false))
return result; return reuseport_result;
} }
result = sk;
result = reuseport_result ? : sk;
badness = score; badness = score;
} }
} }
......
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