Skip to content

Commit 6229841

Browse files
committed
set preferred port for networkserialport
This allows for specifying a start port and a range up from that to be used when looking for a free port to use with ser2net when creating a networkserialport resource. This can be helpful on networks where access to ports on the network is limited. Signed-off-by: Benjamin B. Frost <[email protected]>
1 parent 1070d36 commit 6229841

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

labgrid/remote/exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def _start(self, start_params):
235235
assert self.local.avail
236236
assert self.child is None
237237
assert start_params["path"].startswith("/dev/")
238-
self.port = get_free_port()
238+
self.port = get_free_port(os.environ.get("LG_PREFERRED_NETWORKSERIAL_PORT", None))
239239

240240
# Ser2net has switched to using YAML format at version 4.0.0.
241241
result = subprocess.run([self.ser2net_bin, "-v"], capture_output=True, text=True)

labgrid/util/helper.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@
1515

1616
re_vt100 = re.compile(r"(\x1b\[|\x9b)[^@-_a-z]*[@-_a-z]|\x1b[@-_a-z]")
1717

18-
def get_free_port():
18+
def get_free_port(prefered_port=None):
1919
"""Helper function to always return an unused port."""
2020
with closing(socket(AF_INET, SOCK_STREAM)) as s:
21-
s.bind(('', 0))
21+
if prefered_port is not None:
22+
port_range = [int(i) for i in prefered_port.split(':')]
23+
port = port_range[0]
24+
max_range = port_range[1] if len(port_range) > 1 else 10
25+
max_no_of_ports = port + max_range
26+
while port <= max_no_of_ports:
27+
try:
28+
s.bind(('', port))
29+
except Exception as e:
30+
port += 1
31+
else:
32+
break
33+
else:
34+
s.bind(('', 0))
2235
return s.getsockname()[1]
2336

2437

man/labgrid-exporter.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ The following environment variable can be used to configure labgrid\-exporter.
104104
.sp
105105
This variable can be used to set the default coordinator in the format
106106
\fBHOST[:PORT]\fP (instead of using the \fB\-x\fP option).
107+
.SS LG_PREFERRED_NETWORKSERIAL_PORT
108+
.sp
109+
Set a preferred port to be used with NetworkSerialPort resources.
110+
Specify a port and a optional range counting up using the format \fBPORT[:RANGE]\fP
107111
.SH EXAMPLES
108112
.sp
109113
Start the exporter with the configuration file \fImy\-config.yaml\fP:

man/labgrid-exporter.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ LG_COORDINATOR
9696
This variable can be used to set the default coordinator in the format
9797
``HOST[:PORT]`` (instead of using the ``-x`` option).
9898

99+
LG_PREFERRED_NETWORKSERIAL_PORT
100+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101+
Set a preferred port to be used with NetworkSerialPort resources.
102+
Specify a port and a optional range counting up using the format ``PORT[:RANGE]``
103+
104+
105+
99106
EXAMPLES
100107
--------
101108

0 commit comments

Comments
 (0)