From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 92039396593 for ; Wed, 4 Mar 2026 09:54:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772618071; cv=none; b=gxiKGcua2wkCmWxmuZKx0ZvP/vLagZTmIGBNucSMpzY/8D3dlZVrbdXKRtlpg89rYR9keGDJHlV4vrL1/05j3xwggYdSBVb1UKpFtn3hk3n85dnEhBTB4M2xZwygcfZ18KX1BjSR9BV9CN8sehwJq2ER7Z6MVfbZsNVWm4N/RsI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772618071; c=relaxed/simple; bh=S728nk8EoQpjrgV3+URW9SI+FEJ0ST1wDQyMuDgLiJY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=efUvYgW1pkIypJi/BqOFeSiJrwMfnYfTPihp3XzMXNGui3xphztDra3tc5mS3CrVUWXVDPvrvwz+1RgBB6+RJpkJJR/wsxC4vNziDsW+xVEX+vHXPAYf0S7OHjvb0++Tw9kg93fHPbY76kIbvSUjjuWJKHctwKDNMTFlBPs8LX8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=SUl099d2; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=RbhjeCEB; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=SUl099d2; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=RbhjeCEB; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="SUl099d2"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="RbhjeCEB"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="SUl099d2"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="RbhjeCEB" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9A2653F6E5; Wed, 4 Mar 2026 09:54:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1772618068; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EmYsk7hbwyI2Xvo8xd+DQ0fufVinD5TP56A7Ol+PJzE=; b=SUl099d2NWRIyGaVMbe+kBt5kDJFK4wiXPEYHoD1q02SvcJAJNQsTRuiVA7BqTJOv5P4Ys 8BoaLS+N2lBAjKsUfXn17JUjmg7FpX1cxiwMEz0lICMh0zqIR2t0mBcIioYBMw3ePagwrX olQ9aoI7I2vs0Fo3nZsy392kBalaiLs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1772618068; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EmYsk7hbwyI2Xvo8xd+DQ0fufVinD5TP56A7Ol+PJzE=; b=RbhjeCEB5FeNihbNkwlKBQLc/gm6h4E7vZgilhoI8e+ZvyiTvXeGPf2MK/itK/XBhvKpc8 ZHjey5kIZ8RKIIDw== Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=SUl099d2; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=RbhjeCEB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1772618068; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EmYsk7hbwyI2Xvo8xd+DQ0fufVinD5TP56A7Ol+PJzE=; b=SUl099d2NWRIyGaVMbe+kBt5kDJFK4wiXPEYHoD1q02SvcJAJNQsTRuiVA7BqTJOv5P4Ys 8BoaLS+N2lBAjKsUfXn17JUjmg7FpX1cxiwMEz0lICMh0zqIR2t0mBcIioYBMw3ePagwrX olQ9aoI7I2vs0Fo3nZsy392kBalaiLs= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1772618068; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EmYsk7hbwyI2Xvo8xd+DQ0fufVinD5TP56A7Ol+PJzE=; b=RbhjeCEB5FeNihbNkwlKBQLc/gm6h4E7vZgilhoI8e+ZvyiTvXeGPf2MK/itK/XBhvKpc8 ZHjey5kIZ8RKIIDw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B19CD3EA69; Wed, 4 Mar 2026 09:54:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id X21zKFMBqGkbZwAAD6G6ig (envelope-from ); Wed, 04 Mar 2026 09:54:27 +0000 Message-ID: Date: Wed, 4 Mar 2026 10:54:16 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH net-next v3] inet: add ip_local_port_step_width sysctl to improve port usage distribution To: Eric Dumazet Cc: netdev@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, chia-yu.chang@nokia-bell-labs.com, idosch@nvidia.com, willemb@google.com, dsahern@kernel.org, kuniyu@google.com, ncardwell@google.com, skhan@linuxfoundation.org, corbet@lwn.net, horms@kernel.org, pabeni@redhat.com, kuba@kernel.org, davem@davemloft.net References: <20260303172949.4741-1-fmancera@suse.de> Content-Language: en-US From: Fernando Fernandez Mancera In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.51 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; RCPT_COUNT_TWELVE(0.00)[16]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_TLS_ALL(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:dkim,suse.de:mid,suse.de:email,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,0xffsoftware.com:url]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:106:10:150:64:167:received,2a07:de40:b281:104:10:150:64:97:from]; DKIM_TRACE(0.00)[suse.de:+] X-Rspamd-Queue-Id: 9A2653F6E5 X-Spam-Flag: NO X-Spam-Score: -4.51 X-Spam-Level: On 3/4/26 8:05 AM, Eric Dumazet wrote: > On Tue, Mar 3, 2026 at 6:30 PM Fernando Fernandez Mancera > wrote: >> >> With the current port selection algorithm, ports after a reserved port >> range or long time used port are used more often than others [1]. This >> causes an uneven port usage distribution. This combines with cloud >> environments blocking connections between the application server and the >> database server if there was a previous connection with the same source >> port, leading to connectivity problems between applications on cloud >> environments. >> >> The real issue here is that these firewalls cannot cope with >> standards-compliant port reuse. This is a workaround for such situations >> and an improvement on the distribution of ports selected. >> >> The proposed solution is to implement a variant of RFC 6056 Algorithm 5. >> The step size is selected randomly on every connect() call ensuring it >> is a coprime with respect to the size of the range of ports we want to >> scan. This way, we can ensure that all ports within the range are >> scanned before returning an error. To enable this algorithm, the user >> must configure the new sysctl option "net.ipv4.ip_local_port_step_width". >> >> In addition, on graphs generated we can observe that the distribution of >> source ports is more even with the proposed approach. [2] >> >> [1] https://0xffsoftware.com/port_graph_current_alg.html >> >> [2] https://0xffsoftware.com/port_graph_random_step_alg.html >> >> Signed-off-by: Fernando Fernandez Mancera >> --- >> v2: used step to calculate remaining as (remaining / step) and avoid >> calculating gcd when scan_step and range are both even >> v3: xmas tree formatting and break the gdc() loop once scan_step is 1 >> --- >> Documentation/networking/ip-sysctl.rst | 9 ++++++ >> .../net_cachelines/netns_ipv4_sysctl.rst | 1 + >> include/net/netns/ipv4.h | 1 + >> net/ipv4/inet_hashtables.c | 28 +++++++++++++++++-- >> net/ipv4/sysctl_net_ipv4.c | 7 +++++ >> 5 files changed, 43 insertions(+), 3 deletions(-) >> >> diff --git a/Documentation/networking/ip-sysctl.rst b/Documentation/networking/ip-sysctl.rst >> index 265158534cda..da29806700e9 100644 >> --- a/Documentation/networking/ip-sysctl.rst >> +++ b/Documentation/networking/ip-sysctl.rst >> @@ -1630,6 +1630,15 @@ ip_local_reserved_ports - list of comma separated ranges >> >> Default: Empty >> >> +ip_local_port_step_width - INTEGER >> + Defines the numerical maximum increment between successive port >> + allocations within the ephemeral port range when an unavailable port is >> + reached. This can be used to mitigate accumulated nodes in port >> + distribution when reserved ports have been configured. Please note that >> + port collisions may be more frequent in a system with a very high load. >> + > > Patch SGTM, but I find this documentation obscure. > > Some guidance would be nice. What values have you tested/tried ? > As I am working on a patch series with improvements to ip-sysctl.rst documentation I will handle that there. FTR; I tested multiple scenarios and numbers. If the value is >= the whole range, the issue is always mitigated but of course this will have a hit on performance under port exhaustion situation. The value that works better in my experience is 2x 3x or even 4x the size of the largest reserved block. If only a couple of ports are marked as reserved, 128 is usually enough to avoid clustering.. Thank you all for the reviews! > Reviewed-by: Eric Dumazet