Subject: [PATCH net-next v4 4/5] selftests: drv-net: Introduce a function that checks whether a port is available on remote host

From: Gal Pressman <[email-masked]>
Date: Thu, 20 Feb 2025 13:34:34 +0200

Add a function that checks whether a port is available on the remote
host, this will be used downstream to verify that ports that were
allocated locally are also available on the remote side.

Reviewed-by: Nimrod Oren <[email-masked]>
Signed-off-by: Gal Pressman <[email-masked]>
---
 tools/testing/selftests/net/lib/py/utils.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py
index 5a13f4fd3784..903cc042ed0e 100644
--- a/tools/testing/selftests/net/lib/py/utils.py
+++ b/tools/testing/selftests/net/lib/py/utils.py
@@ -123,6 +123,13 @@ def ethtool(args, json=None, ns=None, host=None):
     return tool('ethtool', args, json=json, ns=ns, host=host)

+def check_port_available_remote(port, host):
+    """
+    Check if a port is available on remote host.
+    Raise exception if not available.
+    """
+    cmd(f"python3 -c 'import socket; s=socket.socket(socket.AF_INET6, socket.SOCK_STREAM); s.bind((\"\", {port}))'", host=host)
+
 def rand_port():
     """
     Get a random unprivileged port.
-- 
2.40.1