Message ID | 20240625010210.2002310-2-kuba@kernel.org (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | selftests: drv-net: rss_ctx: add tests for RSS contexts | expand |
On 6/25/24 03:02, Jakub Kicinski wrote: > 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. > > Signed-off-by: Jakub Kicinski <kuba@kernel.org> > --- > v2: > - remove v4 check (Willem) > - update comment (David, Przemek) > - cap the iterations (Przemek) > --- > tools/testing/selftests/net/lib/py/utils.py | 14 ++++++++++++-- > 1 file changed, 12 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..16907b51e034 100644 > --- a/tools/testing/selftests/net/lib/py/utils.py > +++ b/tools/testing/selftests/net/lib/py/utils.py > @@ -3,6 +3,7 @@ > import json as _json > import random > import re > +import socket > import subprocess > import time > > @@ -79,9 +80,18 @@ import time > > 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 != 98: # already in use > + 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): Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
On Mon, Jun 24, 2024 at 06:02:07PM -0700, Jakub Kicinski wrote: > 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. > > Signed-off-by: Jakub Kicinski <kuba@kernel.org> > --- > v2: > - remove v4 check (Willem) > - update comment (David, Przemek) > - cap the iterations (Przemek) > --- > tools/testing/selftests/net/lib/py/utils.py | 14 ++++++++++++-- > 1 file changed, 12 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..16907b51e034 100644 > --- a/tools/testing/selftests/net/lib/py/utils.py > +++ b/tools/testing/selftests/net/lib/py/utils.py > + except OSError as e: > + if e.errno != 98: # already in use To make it a bit clearer, you can use something as: import errno if e.errno != errno.EADDRINUSE
diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py index 0540ea24921d..16907b51e034 100644 --- a/tools/testing/selftests/net/lib/py/utils.py +++ b/tools/testing/selftests/net/lib/py/utils.py @@ -3,6 +3,7 @@ import json as _json import random import re +import socket import subprocess import time @@ -79,9 +80,18 @@ import time 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 != 98: # already in use + 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):
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. Signed-off-by: Jakub Kicinski <kuba@kernel.org> --- v2: - remove v4 check (Willem) - update comment (David, Przemek) - cap the iterations (Przemek) --- tools/testing/selftests/net/lib/py/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)