Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp2468225rdb; Tue, 12 Sep 2023 02:54:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG27tiIpt3b8Vmd09jhpi97wgI0ChO0ONhPRrqoBLRr/za/G9rx31D/K+XGbjThCmASEvaq X-Received: by 2002:a54:4e83:0:b0:3a7:26fd:b229 with SMTP id c3-20020a544e83000000b003a726fdb229mr12426214oiy.48.1694512458196; Tue, 12 Sep 2023 02:54:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694512458; cv=none; d=google.com; s=arc-20160816; b=PLVmllMkdjv0OjY8yiakE3Iwocfcn1Zzzd94ftMAxHPESmoqiMP23WNhaqav6YYR2p 0FcV27fzG/Qzb++lT7KDnc7wuDZbVLC1NvYabqT2NywKwnQrZppJjUDz375wOL0G5AdI KaAUfrolk1efp1Wox6wCbpkpMjfR7NvrXoJbPQWGnwRJVpJTgomZxowMlKhO+oXYmKIV m6XCxQ8AD3GWJS9CmztTYWialJHHskEe6yDwPVT7vXZuQPRg7E+lO1HZkZaY7f2niJSB 3juJaRAXnzJTMV5ZquROjjatxE/PbWxjqmtmV1WXlLVwLYeZVj0l1rryke1r7XXXycw/ 0Lvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GupC7voHRJj5BwYpmyculQw3S+0sXKYYh1pK7zudlg4=; fh=58X8Twod/aefd4Yph/F0FPyIOMSYi96Sqi5HmPAz0To=; b=bwER2OuewaSevuPqkHlKPST+O7mEgxC/gyg9Uqq5L4RnkHBorj9FBAKOqc4mATPAOx Smk1iMtCV1bPzqFUcOBWMDHHfX+H3Z9sA3GBHggf4c9QDsOube3/+kKw7tiCGkeMFd20 nI6Qs557rRAwPAmRDIFXw/J1FXSd1AwCkmFqk2RWPZWxL/og1kNLM2VtT5PEku/mjgq3 mNhhq/HAERUFmqRHF5V9DPiHDUId9WnZZ1RXC00NCU0GYoQ9hrQr19hd3XxXaTeuEUMl wA6naw0aeG0YUNEY53YgL+KuQ6Nsa8XHocch4p64acW4XkQljsBSYqu234676aSAJCVF DGlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=Sx1PvQ5r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=arista.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id r19-20020a63d913000000b005653283b8a4si7786539pgg.785.2023.09.12.02.54.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 02:54:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@arista.com header.s=google header.b=Sx1PvQ5r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=arista.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 12A228026946; Mon, 11 Sep 2023 21:53:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.8 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236530AbjILCWN (ORCPT + 99 others); Mon, 11 Sep 2023 22:22:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235801AbjILCV5 (ORCPT ); Mon, 11 Sep 2023 22:21:57 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C42F4161263 for ; Mon, 11 Sep 2023 18:45:32 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-31427ddd3fbso4772734f8f.0 for ; Mon, 11 Sep 2023 18:45:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=google; t=1694483131; x=1695087931; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GupC7voHRJj5BwYpmyculQw3S+0sXKYYh1pK7zudlg4=; b=Sx1PvQ5rbjKa97IwPo5yz4eoc9DZsnMHnCJmS6LUeHNNkIxkZQEg0G3k5TwI73kChG nWGRPJXMF2su6KbkQOSSwnOIDSDScprOij/RrsKBVtDMy63VQAJ1jNbDiIj0RDZMWK5B okXYNdCrsqqfkDQw1cy7uieunGvFDXzyZXX3nfK47T3LAYbeaNaQOHYk/VLpwmxrUzdu dqsYOT9cgJeulhb70VcfKLCEjk9ex5ais+SRkJ6sOV520LZ61IIKf4EdGWsP4rqsnWpW VJ0kuKHuRWlW46Uso5EkNd6BNguvz8QurUimzVxFn51+JdqjIL14Q7A6nrkYdvYKx4pb K4cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694483131; x=1695087931; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GupC7voHRJj5BwYpmyculQw3S+0sXKYYh1pK7zudlg4=; b=hngEXp7jaZ80UJ0iE0hV08Q7VsvAlSz1+tFzCRP8zFqOLjVTF06GXWh273g74XR3sw M/k9ik5kQ5jEtLjoCCPes4EUNu6vCYlClzLkDOPv6QHdnNg/T8AFNudBASJmCIHRXHj/ 5Sh8O/BDiNfP+7Qy6x+jp4ug7c4Boy1e2JHEARx7WH33lpXleexOiqdGUbf6B5PTJpEl uKZessapkToucVnHzZWIEiHDc/XhTq3pft2jfbUm8JUzy0TmBnbt23fDhYpJfHVb23lN BTpTL+ZLqns9CL0qO+WHrteUmaymnfRdEeYSYvipptgJXfq8/oXIIbUe7bkZpppqBe0S UStw== X-Gm-Message-State: AOJu0YxUZ6NgUnvzCHtEUQ2f9WKFtqfXeubLGOG5QMJ67z4sCe4vI/rG owJl6fS6S2ANKPyZ6LGi+vZH7rT+QkLLQYiepQs= X-Received: by 2002:a1c:7917:0:b0:402:e68f:8898 with SMTP id l23-20020a1c7917000000b00402e68f8898mr8724677wme.0.1694466271877; Mon, 11 Sep 2023 14:04:31 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id z20-20020a1c4c14000000b00402e942561fsm14261699wmf.38.2023.09.11.14.04.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 14:04:31 -0700 (PDT) From: Dmitry Safonov To: David Ahern , Eric Dumazet , Paolo Abeni , Jakub Kicinski , "David S. Miller" Cc: linux-kernel@vger.kernel.org, Dmitry Safonov , Andy Lutomirski , Ard Biesheuvel , Bob Gilligan , Dan Carpenter , David Laight , Dmitry Safonov <0x7f454c46@gmail.com>, Donald Cassidy , Eric Biggers , "Eric W. Biederman" , Francesco Ruggeri , "Gaillardetz, Dominik" , Herbert Xu , Hideaki YOSHIFUJI , Ivan Delalande , Leonard Crestez , "Nassiri, Mohammad" , Salam Noureddine , Simon Horman , "Tetreault, Francois" , netdev@vger.kernel.org Subject: [PATCH v11 net-next 20/23] net/tcp: Add static_key for TCP-AO Date: Mon, 11 Sep 2023 22:03:40 +0100 Message-ID: <20230911210346.301750-21-dima@arista.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230911210346.301750-1-dima@arista.com> References: <20230911210346.301750-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 11 Sep 2023 21:53:30 -0700 (PDT) X-Spam-Status: No, score=-2.0 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Similarly to TCP-MD5, add a static key to TCP-AO that is patched out when there are no keys on a machine and dynamically enabled with the first setsockopt(TCP_AO) adds a key on any socket. The static key is as well dynamically disabled later when the socket is destructed. The lifetime of enabled static key here is the same as ao_info: it is enabled on allocation, passed over from full socket to twsk and destructed when ao_info is scheduled for destruction. Signed-off-by: Dmitry Safonov Acked-by: David Ahern --- include/net/tcp.h | 3 +++ include/net/tcp_ao.h | 2 ++ net/ipv4/tcp_ao.c | 22 +++++++++++++++++++++ net/ipv4/tcp_input.c | 46 +++++++++++++++++++++++++++++--------------- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index ac5f96f0ce19..fa100e4f2cde 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -2611,6 +2611,9 @@ static inline bool tcp_ao_required(struct sock *sk, const void *saddr, struct tcp_ao_info *ao_info; struct tcp_ao_key *ao_key; + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return false; + ao_info = rcu_dereference_check(tcp_sk(sk)->ao_info, lockdep_sock_is_held(sk)); if (!ao_info) diff --git a/include/net/tcp_ao.h b/include/net/tcp_ao.h index 09cf9d216b3a..b97e1b3c6448 100644 --- a/include/net/tcp_ao.h +++ b/include/net/tcp_ao.h @@ -151,6 +151,8 @@ do { \ #ifdef CONFIG_TCP_AO /* TCP-AO structures and functions */ +#include +extern struct static_key_false_deferred tcp_ao_needed; struct tcp4_ao_context { __be32 saddr; diff --git a/net/ipv4/tcp_ao.c b/net/ipv4/tcp_ao.c index c5bde089916d..24fd8772deea 100644 --- a/net/ipv4/tcp_ao.c +++ b/net/ipv4/tcp_ao.c @@ -17,6 +17,8 @@ #include #include +DEFINE_STATIC_KEY_DEFERRED_FALSE(tcp_ao_needed, HZ); + int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx, unsigned int len, struct tcp_sigpool *hp) { @@ -50,6 +52,9 @@ bool tcp_ao_ignore_icmp(const struct sock *sk, int type, int code) bool ignore_icmp = false; struct tcp_ao_info *ao; + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return false; + /* RFC5925, 7.8: * >> A TCP-AO implementation MUST default to ignore incoming ICMPv4 * messages of Type 3 (destination unreachable), Codes 2-4 (protocol @@ -185,6 +190,9 @@ static struct tcp_ao_key *__tcp_ao_do_lookup(const struct sock *sk, struct tcp_ao_key *key; struct tcp_ao_info *ao; + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return NULL; + ao = rcu_dereference_check(tcp_sk(sk)->ao_info, lockdep_sock_is_held(sk)); if (!ao) @@ -276,6 +284,7 @@ void tcp_ao_destroy_sock(struct sock *sk, bool twsk) } kfree_rcu(ao, rcu); + static_branch_slow_dec_deferred(&tcp_ao_needed); } void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp) @@ -1129,6 +1138,11 @@ int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk, goto free_and_exit; } + if (!static_key_fast_inc_not_disabled(&tcp_ao_needed.key.key)) { + ret = -EUSERS; + goto free_and_exit; + } + key_head = rcu_dereference(hlist_first_rcu(&new_ao->head)); first_key = hlist_entry_safe(key_head, struct tcp_ao_key, node); @@ -1556,6 +1570,10 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family, tcp_ao_link_mkt(ao_info, key); if (first) { + if (!static_branch_inc(&tcp_ao_needed.key)) { + ret = -EUSERS; + goto err_free_sock; + } sk_gso_disable(sk); rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info); } @@ -1824,6 +1842,10 @@ static int tcp_ao_info_cmd(struct sock *sk, unsigned short int family, if (new_rnext) WRITE_ONCE(ao_info->rnext_key, new_rnext); if (first) { + if (!static_branch_inc(&tcp_ao_needed.key)) { + err = -EUSERS; + goto out; + } sk_gso_disable(sk); rcu_assign_pointer(tcp_sk(sk)->ao_info, ao_info); } diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 1e9e423bb718..414c49d37390 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -3528,41 +3528,55 @@ static inline bool tcp_may_update_window(const struct tcp_sock *tp, (ack_seq == tp->snd_wl1 && (nwin > tp->snd_wnd || !nwin)); } -/* If we update tp->snd_una, also update tp->bytes_acked */ -static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack) +static void tcp_snd_sne_update(struct tcp_sock *tp, u32 ack) { - u32 delta = ack - tp->snd_una; #ifdef CONFIG_TCP_AO struct tcp_ao_info *ao; -#endif - sock_owned_by_me((struct sock *)tp); - tp->bytes_acked += delta; -#ifdef CONFIG_TCP_AO + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return; + ao = rcu_dereference_protected(tp->ao_info, lockdep_sock_is_held((struct sock *)tp)); if (ao && ack < tp->snd_una) ao->snd_sne++; #endif +} + +/* If we update tp->snd_una, also update tp->bytes_acked */ +static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack) +{ + u32 delta = ack - tp->snd_una; + + sock_owned_by_me((struct sock *)tp); + tp->bytes_acked += delta; + tcp_snd_sne_update(tp, ack); tp->snd_una = ack; } +static void tcp_rcv_sne_update(struct tcp_sock *tp, u32 seq) +{ +#ifdef CONFIG_TCP_AO + struct tcp_ao_info *ao; + + if (!static_branch_unlikely(&tcp_ao_needed.key)) + return; + + ao = rcu_dereference_protected(tp->ao_info, + lockdep_sock_is_held((struct sock *)tp)); + if (ao && seq < tp->rcv_nxt) + ao->rcv_sne++; +#endif +} + /* If we update tp->rcv_nxt, also update tp->bytes_received */ static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq) { u32 delta = seq - tp->rcv_nxt; -#ifdef CONFIG_TCP_AO - struct tcp_ao_info *ao; -#endif sock_owned_by_me((struct sock *)tp); tp->bytes_received += delta; -#ifdef CONFIG_TCP_AO - ao = rcu_dereference_protected(tp->ao_info, - lockdep_sock_is_held((struct sock *)tp)); - if (ao && seq < tp->rcv_nxt) - ao->rcv_sne++; -#endif + tcp_rcv_sne_update(tp, seq); WRITE_ONCE(tp->rcv_nxt, seq); } -- 2.41.0