From patchwork Thu Jul 20 15:30:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 13320627 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37BEBEB64DD for ; Thu, 20 Jul 2023 15:31:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232550AbjGTPbE (ORCPT ); Thu, 20 Jul 2023 11:31:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232555AbjGTPbC (ORCPT ); Thu, 20 Jul 2023 11:31:02 -0400 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 909872D56 for ; Thu, 20 Jul 2023 08:30:47 -0700 (PDT) Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-992f15c36fcso160920866b.3 for ; Thu, 20 Jul 2023 08:30:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689867046; x=1690471846; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=A1OV/Gg5jhKvjIdebEVDtlHxi2P2tNv/5e7W2/lf1TY=; b=KWmNNkPnYi34ViccPyP9omB6g3VYjIC+P5a6s3R9Zq6u27XIBTylnYDqSE/6QzsjTs c4BQNfgoTfurzpxPfd+rTn8+sHF5/3hals0R+SfDgHzqypyxIwn0CyQAwa6wASPY7dSC WSX3KiL+jD5rkhWpQMmdogXbeTaV6xpxCCb/PGp1nh+pz2q964TKvIrh7nu7of1oRsXy GyokRYBn9+JOIGMBjRK6HARA3XRPpt0CL69CznvVLzKRg4eurPEZWNCW6mGcXNy8BcxP qeOP6TwWpCx5xNk4BvN6p0tCYRY9XEUP5dePoU5V+TfZgVQ8xZyRgzGTYdxMXMg4bXSB JC9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689867046; x=1690471846; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=A1OV/Gg5jhKvjIdebEVDtlHxi2P2tNv/5e7W2/lf1TY=; b=UgXCf48kt4Ed6aUqG6pMiU5O4PuaecRJiVZchN4zZ8f1U98lDDDVLA21ioIUjzKIEs SIedGCijTMkThjqNZBfkyzaXI4jUAXutNPf0cmxNkqM90tpoCS+7sRC6UON9O3xYMrlU di+20iGasCH1Ho3c+pGrRfj01F/AgVq73VlPUXwsdAK0luonaau7XC7ZBcx5U5wo97BJ DGC6ZGoyVu5e6za74nwnd9CF//k14p2+AOn/y26UJA5vX8Omh3n1250mapgiIvwcRNpC 2LgvxjwEMsWaW0wGeInUAuHIV/C1x6tU44VuuS6NBu4Gv+AxRFRMyW8JHpMoG8BFsWl/ NCnA== X-Gm-Message-State: ABy/qLZUE1l+KcJSIjvByvuYKyT3Vj+kA5lvNlNggFFwvTSvIktNof1r YTr3f1Uci2lRzfkMvkeSGTq/UA== X-Google-Smtp-Source: APBJJlHARYpaFcrWesfr6JAczLs2z5T8X3ITGbp+DNZ36FmzOiZ0OW8gxonsvS3TpieAIL8VbM3a0w== X-Received: by 2002:a17:907:7888:b0:99b:5b07:64aa with SMTP id ku8-20020a170907788800b0099b5b0764aamr1054958ejc.35.1689867045943; Thu, 20 Jul 2023 08:30:45 -0700 (PDT) Received: from [192.168.188.151] (p200300c1c7176000b788d2ebe49c4b82.dip0.t-ipconnect.de. [2003:c1:c717:6000:b788:d2eb:e49c:4b82]) by smtp.gmail.com with ESMTPSA id x10-20020a170906804a00b009893b06e9e3sm851007ejw.225.2023.07.20.08.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 08:30:45 -0700 (PDT) From: Lorenz Bauer Date: Thu, 20 Jul 2023 17:30:05 +0200 Subject: [PATCH bpf-next v6 1/8] udp: re-score reuseport groups when connected sockets are present MIME-Version: 1.0 Message-Id: <20230720-so-reuseport-v6-1-7021b683cdae@isovalent.com> References: <20230720-so-reuseport-v6-0-7021b683cdae@isovalent.com> In-Reply-To: <20230720-so-reuseport-v6-0-7021b683cdae@isovalent.com> To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , David Ahern , Willem de Bruijn , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Joe Stringer , Mykola Lysenko , Shuah Khan , Kuniyuki Iwashima Cc: Hemanth Malla , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Lorenz Bauer X-Mailer: b4 0.12.3 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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 Signed-off-by: Lorenz Bauer --- net/ipv4/udp.c | 20 +++++++++++++++----- net/ipv6/udp.c | 19 ++++++++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 42a96b3547c9..c62d5e1c6675 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -451,14 +451,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; diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index b7c972aa09a7..dec69f0379e9 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -194,14 +194,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;