From: Gal Pressman <gal@nvidia.com>
To: "David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>
Cc: <netdev@vger.kernel.org>, Eric Dumazet <edumazet@google.com>,
Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Tony Nguyen <anthony.l.nguyen@intel.com>,
Przemek Kitszel <przemyslaw.kitszel@intel.com>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"Tariq Toukan" <tariqt@nvidia.com>,
Edward Cree <ecree.xilinx@gmail.com>,
Ahmed Zaki <ahmed.zaki@intel.com>, <linux-doc@vger.kernel.org>,
Gal Pressman <gal@nvidia.com>, Nimrod Oren <noren@nvidia.com>
Subject: [PATCH net-next v5 3/4] selftests: drv-net: Make rand_port() get a port more reliably
Date: Mon, 24 Feb 2025 19:44:15 +0200 [thread overview]
Message-ID: <20250224174416.499070-4-gal@nvidia.com> (raw)
In-Reply-To: <20250224174416.499070-1-gal@nvidia.com>
Instead of guessing a port and checking whether it's available, get an
available port from the OS.
Reviewed-by: Nimrod Oren <noren@nvidia.com>
Signed-off-by: Gal Pressman <gal@nvidia.com>
---
tools/testing/selftests/net/lib/py/utils.py | 15 ++++-----------
1 file changed, 4 insertions(+), 11 deletions(-)
diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py
index d879700ef2b9..a6af97c7e283 100644
--- a/tools/testing/selftests/net/lib/py/utils.py
+++ b/tools/testing/selftests/net/lib/py/utils.py
@@ -187,18 +187,11 @@ def ethtool(args, json=None, ns=None, host=None):
def rand_port():
"""
- Get a random unprivileged port, try to make sure it's not already used.
+ Get a random unprivileged port.
"""
- for _ in range(1000):
- port = random.randint(10000, 65535)
- try:
- with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
- s.bind(("", port))
- return port
- except OSError as e:
- if e.errno != errno.EADDRINUSE:
- raise
- raise Exception("Can't find any free unprivileged port")
+ with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
+ s.bind(("", 0))
+ return s.getsockname()[1]
def wait_port_listen(port, proto="tcp", ns=None, host=None, sleep=0.005, deadline=5):
--
2.40.1
next prev parent reply other threads:[~2025-02-24 17:44 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-24 17:44 [PATCH net-next v5 0/4] Symmetric OR-XOR RSS hash Gal Pressman
2025-02-24 17:44 ` [PATCH net-next v5 1/4] ethtool: " Gal Pressman
2025-02-24 17:44 ` [PATCH net-next v5 2/4] net/mlx5e: Symmetric OR-XOR RSS hash control Gal Pressman
2025-02-24 17:44 ` Gal Pressman [this message]
2025-02-24 17:44 ` [PATCH net-next v5 4/4] selftests: drv-net-hw: Add a test for symmetric RSS hash Gal Pressman
2025-02-26 3:10 ` [PATCH net-next v5 0/4] Symmetric OR-XOR " patchwork-bot+netdevbpf
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=20250224174416.499070-4-gal@nvidia.com \
--to=gal@nvidia.com \
--cc=ahmed.zaki@intel.com \
--cc=andrew+netdev@lunn.ch \
--cc=anthony.l.nguyen@intel.com \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=ecree.xilinx@gmail.com \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=noren@nvidia.com \
--cc=pabeni@redhat.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=tariqt@nvidia.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;
as well as URLs for NNTP newsgroup(s).