Commit f0ea27e7 authored by Lorenz Bauer's avatar Lorenz Bauer Committed by Martin KaFai Lau

udp: re-score reuseport groups when connected sockets are present

Contrary to TCP, UDP reuseport groups can contain TCP_ESTABLISHED
sockets. To support these properly we remember whether a group has
a connected socket and skip the fast reuseport early-return. In
effect we continue scoring all reuseport sockets and then choose the
one with the highest score.

The current code fails to re-calculate the score for the result of
lookup_reuseport. According to Kuniyuki Iwashima:

    1) SO_INCOMING_CPU is set
       -> selected sk might have +1 score

    2) BPF prog returns ESTABLISHED and/or SO_INCOMING_CPU sk
       -> selected sk will have more than 8

  Using the old score could trigger more lookups depending on the
  order that sockets are created.

    sk -> sk (SO_INCOMING_CPU) -> sk (ESTABLISHED)
    |     |
    `-> select the next SO_INCOMING_CPU sk
          |
          `-> select itself (We should save this lookup)

Fixes: efc6b6f6 ("udp: Improve load balancing for SO_REUSEPORT.")
Reviewed-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: default avatarLorenz Bauer <lmb@isovalent.com>
Link: https://lore.kernel.org/r/20230720-so-reuseport-v6-1-7021b683cdae@isovalent.comSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent 7b2b2012
...@@ -451,14 +451,24 @@ static struct sock *udp4_lib_lookup2(struct net *net, ...@@ -451,14 +451,24 @@ 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) {
result = lookup_reuseport(net, sk, skb, badness = score;
saddr, sport, daddr, hnum); result = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
if (!result) {
result = sk;
continue;
}
/* Fall back to scoring if group has connections */ /* Fall back to scoring if group has connections */
if (result && !reuseport_has_conns(sk)) if (!reuseport_has_conns(sk))
return result; return result;
result = result ? : sk; /* Reuseport logic returned an error, keep original score. */
badness = score; if (IS_ERR(result))
continue;
badness = compute_score(result, net, saddr, sport,
daddr, hnum, dif, sdif);
} }
} }
return result; return result;
......
...@@ -194,14 +194,23 @@ static struct sock *udp6_lib_lookup2(struct net *net, ...@@ -194,14 +194,23 @@ 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); daddr, hnum, dif, sdif);
if (score > badness) { if (score > badness) {
result = lookup_reuseport(net, sk, skb, badness = score;
saddr, sport, daddr, hnum); result = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
if (!result) {
result = sk;
continue;
}
/* Fall back to scoring if group has connections */ /* Fall back to scoring if group has connections */
if (result && !reuseport_has_conns(sk)) if (!reuseport_has_conns(sk))
return result; return result;
result = result ? : sk; /* Reuseport logic returned an error, keep original score. */
badness = score; if (IS_ERR(result))
continue;
badness = compute_score(sk, net, saddr, sport,
daddr, hnum, dif, sdif);
} }
} }
return result; return result;
......
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