mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fernando Fernandez Mancera <fmancera@suse.de>
To: Eric Dumazet <edumazet@google.com>
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
Subject: Re: [PATCH net-next v3] inet: add ip_local_port_step_width sysctl to improve port usage distribution
Date: Wed, 4 Mar 2026 10:54:16 +0100	[thread overview]
Message-ID: <aa36b48c-6184-46f3-bf6e-4750c2266f35@suse.de> (raw)
In-Reply-To: <CANn89i+ntGv0gGYvRq8yziGTE01ozBJ_Mn_RDPXLgs+yU5whrg@mail.gmail.com>

On 3/4/26 8:05 AM, Eric Dumazet wrote:
> On Tue, Mar 3, 2026 at 6:30 PM Fernando Fernandez Mancera
> <fmancera@suse.de> 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 <fmancera@suse.de>
>> ---
>> 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 <edumazet@google.com>


  reply	other threads:[~2026-03-04  9:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-03 17:29 Fernando Fernandez Mancera
2026-03-03 21:18 ` Kuniyuki Iwashima
2026-03-04  7:05 ` Eric Dumazet
2026-03-04  9:54   ` Fernando Fernandez Mancera [this message]
2026-03-05  1:46     ` Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aa36b48c-6184-46f3-bf6e-4750c2266f35@suse.de \
    --to=fmancera@suse.de \
    --cc=chia-yu.chang@nokia-bell-labs.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=skhan@linuxfoundation.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®