Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1559576pxa; Thu, 6 Aug 2020 10:15:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxGIAQkV6R4UXtk3p4RA0YYVeseRJnpvDHpCDksk2JrOjRFZcwYSS+NAImiB5zN6im49WE X-Received: by 2002:a50:9d8a:: with SMTP id w10mr4884485ede.16.1596734143964; Thu, 06 Aug 2020 10:15:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596734143; cv=none; d=google.com; s=arc-20160816; b=EhB8N2yYP1ALAi283ZlLWccS/4Jg2oASJB3FINOluztih3yfsZDghHJED+a/YFkIgX 8AJn3ZcX01Y47/Cg+abH3ABURtptIqm6Qag6E5VFAq50Il0LOxExh+UAJ5jWpHypoaW5 AR6L3/02ZQzpH0tGY9vCiKhBX31GPg2CM9YDJ1tQFXAoWepPPvEpV/J+hZCGLEWP7LpH mO+2HTxj/xYaDZaafJ4kG/MMT195pnSejglCmEmc5t18mGdouo/OBLywGhg1FF/QYM24 9K9Ebl66yRZ5VkxLG3WJ6QuEqgwpiXlTv+ohAMq3wVolkEskCK242rD0K25NkrRuDeff Iu1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1ErNZRpF+8oSZq4thj+VkoA7Mzk+13pAiSMJt4dWjiw=; b=ZIZsVQGlsRg60uiBodD6N6Co8woxfbkyeUR++mE8y1G926Aaj1dfTW/8qXrOQESO/i pNu1RnHHnLEeB5nJsu1AOtsvt6dnuW+pTA07x9/hFoxXs79RqBfic4vrukbwHs2mOhD6 4FIlUYW1PEmtZe/DnrwM0rsHwOnu5ljMXfSdwtl5B79USOuSaXXVhTusAEh0cBnlmC+5 Lfi1/YTG2QXNIiftV5mooYudcBbJi27fkO7KytsMphdccOTaw1QpCQwP0/+jU0zEybyb WDLVmPGM/KkOv/GImvyYuxD+WJi4nBcjh9ipd3vOl1mhI/jPdmwBscLSpiFvbVn1C91F aakg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tessares-net.20150623.gappssmtp.com header.s=20150623 header.b=eNe+XCX7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w9si3592980edq.487.2020.08.06.10.15.20; Thu, 06 Aug 2020 10:15:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@tessares-net.20150623.gappssmtp.com header.s=20150623 header.b=eNe+XCX7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730009AbgHFRPO (ORCPT + 99 others); Thu, 6 Aug 2020 13:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729986AbgHFRFb (ORCPT ); Thu, 6 Aug 2020 13:05:31 -0400 Received: from mail-ej1-x644.google.com (mail-ej1-x644.google.com [IPv6:2a00:1450:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14B6FC034602 for ; Thu, 6 Aug 2020 05:32:08 -0700 (PDT) Received: by mail-ej1-x644.google.com with SMTP id a26so24316911ejc.2 for ; Thu, 06 Aug 2020 05:32:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tessares-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1ErNZRpF+8oSZq4thj+VkoA7Mzk+13pAiSMJt4dWjiw=; b=eNe+XCX78H+NRhHx+wAc7SWkSiqhHAtvd8+5u3ePNr+D20EEBg/xRfE0+Jcfe0NVo9 gxs3GVSL/06sHgDhjxE/khDy9xtoxY3nFXT46+AxSU1haJE5My8R9ytCfQ09ichUWjv0 zcQIuTQOqORLcPtfTRrADa4Eecio5gQtNB6Wabc4XHuksmK/DPWbI9yDNo2RceIVs3Wp KEjVEnJXcSCAcUFxXwZxd9VNdRo3+41pHyMp268QYSERI6JgHV22qFCjb7MoIK+oxjbI asRRgcYb5rey8md5HZmt2WOxQorSXPs7Jvd93VMso20CMevToRZjPe/fHrom9XY2T6oa UKnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1ErNZRpF+8oSZq4thj+VkoA7Mzk+13pAiSMJt4dWjiw=; b=jjgL5OWvhM9p/wBBw3tQt9N5pAnljHGcQW2JcS4gigfz2I0rMnejdQbHT8mg38v//a CXRoK3QbCtkgYgsU2GnuBY0jFdBaGhmH9+Bp7cNkZ1jcupWP7uyj+dxJ8vfGTu3TRfmK pOVqMTeVqvtF6/xBELhJ7fHrv/axUvUyapRhxMvwbs7Ha4zBa3u+FnKWxpWdoZYnIb2A uzNsVRulEn8ACF8uC3esQcka5TSF3UfgEYUkossc+oAd89KClVyL2g/uOJkf680o8EdO 53c+LXTcf99rzuX5QAjoX+9hDf4eN0AnwmJMghq3wpQDfdF5fjLgh9L4yHU0WTY3gJLB A3pQ== X-Gm-Message-State: AOAM532i4qCtFFa4i6i04T0tkvlPfc1DOti269Xvyfdh5+y5o21EA4NE xGpwNftqYw6ffQGvz9LTvUV46Q== X-Received: by 2002:a17:906:32ce:: with SMTP id k14mr4303986ejk.412.1596717127478; Thu, 06 Aug 2020 05:32:07 -0700 (PDT) Received: from tim.froidcoeur.net (ptr-7tznw15pracyli75x11.18120a2.ip6.access.telenet.be. [2a02:1811:50e:f0f0:d05d:939:f42b:f575]) by smtp.gmail.com with ESMTPSA id c5sm3695778ejb.103.2020.08.06.05.32.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Aug 2020 05:32:07 -0700 (PDT) From: Tim Froidcoeur To: "David S. Miller" , Jakub Kicinski , Alexey Kuznetsov , Hideaki YOSHIFUJI , Patrick McHardy , KOVACS Krisztian Cc: Tim Froidcoeur , Matthieu Baerts , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net v3 1/2] net: refactor bind_bucket fastreuse into helper Date: Thu, 6 Aug 2020 14:30:22 +0200 Message-Id: <20200806123024.585212-2-tim.froidcoeur@tessares.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200806123024.585212-1-tim.froidcoeur@tessares.net> References: <20200806123024.585212-1-tim.froidcoeur@tessares.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Refactor the fastreuse update code in inet_csk_get_port into a small helper function that can be called from other places. Acked-by: Matthieu Baerts Signed-off-by: Tim Froidcoeur --- Notes: v2: - remove unnecessary cast (Matt) include/net/inet_connection_sock.h | 4 ++ net/ipv4/inet_connection_sock.c | 97 ++++++++++++++++-------------- 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/include/net/inet_connection_sock.h b/include/net/inet_connection_sock.h index e5b388f5fa20..1d59bf55bb4d 100644 --- a/include/net/inet_connection_sock.h +++ b/include/net/inet_connection_sock.h @@ -316,6 +316,10 @@ int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname, int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname, char __user *optval, unsigned int optlen); +/* update the fast reuse flag when adding a socket */ +void inet_csk_update_fastreuse(struct inet_bind_bucket *tb, + struct sock *sk); + struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu); #define TCP_PINGPONG_THRESH 3 diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index afaf582a5aa9..a1be020bde8e 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -296,6 +296,57 @@ static inline int sk_reuseport_match(struct inet_bind_bucket *tb, ipv6_only_sock(sk), true, false); } +void inet_csk_update_fastreuse(struct inet_bind_bucket *tb, + struct sock *sk) +{ + kuid_t uid = sock_i_uid(sk); + bool reuse = sk->sk_reuse && sk->sk_state != TCP_LISTEN; + + if (hlist_empty(&tb->owners)) { + tb->fastreuse = reuse; + if (sk->sk_reuseport) { + tb->fastreuseport = FASTREUSEPORT_ANY; + tb->fastuid = uid; + tb->fast_rcv_saddr = sk->sk_rcv_saddr; + tb->fast_ipv6_only = ipv6_only_sock(sk); + tb->fast_sk_family = sk->sk_family; +#if IS_ENABLED(CONFIG_IPV6) + tb->fast_v6_rcv_saddr = sk->sk_v6_rcv_saddr; +#endif + } else { + tb->fastreuseport = 0; + } + } else { + if (!reuse) + tb->fastreuse = 0; + if (sk->sk_reuseport) { + /* We didn't match or we don't have fastreuseport set on + * the tb, but we have sk_reuseport set on this socket + * and we know that there are no bind conflicts with + * this socket in this tb, so reset our tb's reuseport + * settings so that any subsequent sockets that match + * our current socket will be put on the fast path. + * + * If we reset we need to set FASTREUSEPORT_STRICT so we + * do extra checking for all subsequent sk_reuseport + * socks. + */ + if (!sk_reuseport_match(tb, sk)) { + tb->fastreuseport = FASTREUSEPORT_STRICT; + tb->fastuid = uid; + tb->fast_rcv_saddr = sk->sk_rcv_saddr; + tb->fast_ipv6_only = ipv6_only_sock(sk); + tb->fast_sk_family = sk->sk_family; +#if IS_ENABLED(CONFIG_IPV6) + tb->fast_v6_rcv_saddr = sk->sk_v6_rcv_saddr; +#endif + } + } else { + tb->fastreuseport = 0; + } + } +} + /* Obtain a reference to a local port for the given sock, * if snum is zero it means select any available local port. * We try to allocate an odd port (and leave even ports for connect()) @@ -308,7 +359,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) struct inet_bind_hashbucket *head; struct net *net = sock_net(sk); struct inet_bind_bucket *tb = NULL; - kuid_t uid = sock_i_uid(sk); int l3mdev; l3mdev = inet_sk_bound_l3mdev(sk); @@ -345,49 +395,8 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) goto fail_unlock; } success: - if (hlist_empty(&tb->owners)) { - tb->fastreuse = reuse; - if (sk->sk_reuseport) { - tb->fastreuseport = FASTREUSEPORT_ANY; - tb->fastuid = uid; - tb->fast_rcv_saddr = sk->sk_rcv_saddr; - tb->fast_ipv6_only = ipv6_only_sock(sk); - tb->fast_sk_family = sk->sk_family; -#if IS_ENABLED(CONFIG_IPV6) - tb->fast_v6_rcv_saddr = sk->sk_v6_rcv_saddr; -#endif - } else { - tb->fastreuseport = 0; - } - } else { - if (!reuse) - tb->fastreuse = 0; - if (sk->sk_reuseport) { - /* We didn't match or we don't have fastreuseport set on - * the tb, but we have sk_reuseport set on this socket - * and we know that there are no bind conflicts with - * this socket in this tb, so reset our tb's reuseport - * settings so that any subsequent sockets that match - * our current socket will be put on the fast path. - * - * If we reset we need to set FASTREUSEPORT_STRICT so we - * do extra checking for all subsequent sk_reuseport - * socks. - */ - if (!sk_reuseport_match(tb, sk)) { - tb->fastreuseport = FASTREUSEPORT_STRICT; - tb->fastuid = uid; - tb->fast_rcv_saddr = sk->sk_rcv_saddr; - tb->fast_ipv6_only = ipv6_only_sock(sk); - tb->fast_sk_family = sk->sk_family; -#if IS_ENABLED(CONFIG_IPV6) - tb->fast_v6_rcv_saddr = sk->sk_v6_rcv_saddr; -#endif - } - } else { - tb->fastreuseport = 0; - } - } + inet_csk_update_fastreuse(tb, sk); + if (!inet_csk(sk)->icsk_bind_hash) inet_bind_hash(sk, tb, port); WARN_ON(inet_csk(sk)->icsk_bind_hash != tb); -- 2.25.1