mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
selftests: drv-net: Make rand_port() get a port more reliably
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> Link: https://patch.msgid.link/20250224174416.499070-4-gal@nvidia.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
committed by
Jakub Kicinski
parent
4d20c9f2db
commit
0163250039
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user