]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
udp: re-score reuseport groups when connected sockets are present
authorLorenz Bauer <lmb@isovalent.com>
Thu, 20 Jul 2023 15:30:05 +0000 (17:30 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 13 Sep 2023 07:48:01 +0000 (09:48 +0200)
[ Upstream commit f0ea27e7bfe1c34e1f451a63eb68faa1d4c3a86d ]

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: efc6b6f6c311 ("udp: Improve load balancing for SO_REUSEPORT.")
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
Link: https://lore.kernel.org/r/20230720-so-reuseport-v6-1-7021b683cdae@isovalent.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
net/ipv4/udp.c
net/ipv6/udp.c

index 6d327d6d978c5e8980be22d05d20c6cf53accb32..a3302136ce92e059c366e11eb33558830dec5d58 100644 (file)
@@ -452,14 +452,24 @@ static struct sock *udp4_lib_lookup2(struct net *net,
                score = compute_score(sk, net, saddr, sport,
                                      daddr, hnum, dif, sdif);
                if (score > badness) {
-                       result = lookup_reuseport(net, sk, skb,
-                                                 saddr, sport, daddr, hnum);
+                       badness = score;
+                       result = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
+                       if (!result) {
+                               result = sk;
+                               continue;
+                       }
+
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk))
+                       if (!reuseport_has_conns(sk))
                                return result;
 
-                       result = result ? : sk;
-                       badness = score;
+                       /* Reuseport logic returned an error, keep original score. */
+                       if (IS_ERR(result))
+                               continue;
+
+                       badness = compute_score(result, net, saddr, sport,
+                                               daddr, hnum, dif, sdif);
+
                }
        }
        return result;
index 8521729fb23751e881d6d7e329fb57c1e0e22fc9..9c7457823eb971bceddcd7d8b202faab9264b885 100644 (file)
@@ -195,14 +195,23 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                score = compute_score(sk, net, saddr, sport,
                                      daddr, hnum, dif, sdif);
                if (score > badness) {
-                       result = lookup_reuseport(net, sk, skb,
-                                                 saddr, sport, daddr, hnum);
+                       badness = score;
+                       result = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
+                       if (!result) {
+                               result = sk;
+                               continue;
+                       }
+
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk))
+                       if (!reuseport_has_conns(sk))
                                return result;
 
-                       result = result ? : sk;
-                       badness = score;
+                       /* Reuseport logic returned an error, keep original score. */
+                       if (IS_ERR(result))
+                               continue;
+
+                       badness = compute_score(sk, net, saddr, sport,
+                                               daddr, hnum, dif, sdif);
                }
        }
        return result;