aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/net/lib/py/utils.py
diff options
context:
space:
mode:
authorJakub Kicinski <[email protected]>2024-06-26 01:24:53 +0000
committerJakub Kicinski <[email protected]>2024-06-27 02:06:03 +0000
commit8b8fe280155d9fb6f3f6310f05f613cbdf30196b (patch)
treed09aeb35e05b7eeac974725ac70a99240a9ff15c /tools/testing/selftests/net/lib/py/utils.py
parentMerge branch 'mlxsw-reduce-memory-footprint-of-mlxsw-driver' (diff)
downloadkernel-8b8fe280155d9fb6f3f6310f05f613cbdf30196b.tar.gz
kernel-8b8fe280155d9fb6f3f6310f05f613cbdf30196b.zip
selftests: drv-net: try to check if port is in use
We use random ports for communication. As Willem predicted this leads to occasional failures. Try to check if port is already in use by opening a socket and binding to that port. Reviewed-by: Przemek Kitszel <[email protected]> Reviewed-by: Petr Machata <[email protected]> Reviewed-by: Willem de Bruijn <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
Diffstat (limited to 'tools/testing/selftests/net/lib/py/utils.py')
-rw-r--r--tools/testing/selftests/net/lib/py/utils.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py
index 0540ea24921d..dd9d2b9f2b20 100644
--- a/tools/testing/selftests/net/lib/py/utils.py
+++ b/tools/testing/selftests/net/lib/py/utils.py
@@ -1,8 +1,10 @@
# SPDX-License-Identifier: GPL-2.0
+import errno
import json as _json
import random
import re
+import socket
import subprocess
import time
@@ -79,9 +81,18 @@ def ip(args, json=None, ns=None, host=None):
def rand_port():
"""
- Get unprivileged port, for now just random, one day we may decide to check if used.
+ Get a random unprivileged port, try to make sure it's not already used.
"""
- return random.randint(10000, 65535)
+ 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")
def wait_port_listen(port, proto="tcp", ns=None, host=None, sleep=0.005, deadline=5):