Skip to content

protocol: add PortForwardProtocol - #1961

Open
alextercete wants to merge 6 commits into
labgrid-project:masterfrom
ARM-software:port-forward-protocol
Open

protocol: add PortForwardProtocol#1961
alextercete wants to merge 6 commits into
labgrid-project:masterfrom
ARM-software:port-forward-protocol

Conversation

@alextercete

@alextercete alextercete commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Description

Introduce PortForwardProtocol and implement it in SSHDriver and ADBDriver.

SSHDriver now uses automatic allocation for remote ports and reads the allocated port from ssh -O forward. The existing methods are kept as deprecated compatibility wrappers.

ADBDriver uses adb forward and adb reverse. For RemoteUSBADBDevice, it chains ADB and SSH port forwards through the exporter.

labgrid-client forward now uses a configured PortForwardProtocol driver, with implicit SSH forwarding retained as a compatibility fallback.

Checklist

  • Documentation for the feature
  • Tests for the feature
  • The description in doc/configuration.rst has been updated
  • PR has been tested

Signed-off-by: Alex Tercete <alex.tercete@arm.com>
Reviewed-by: Alex Tercete <alext.tercete@arm.com> # gatekeeper
Keep `forward_local_port()` and `forward_remote_port()` as deprecated
compatibility wrappers around the new protocol methods.

Signed-off-by: Alex Tercete <alex.tercete@arm.com>
Reviewed-by: Alex Tercete <alext.tercete@arm.com> # gatekeeper
Capture the port reported for `-R0` forwards and retain the original
request so the forwarding can be cancelled reliably.

Signed-off-by: Alex Tercete <alex.tercete@arm.com>
Reviewed-by: Alex Tercete <alext.tercete@arm.com> # gatekeeper
Add context managers for forwarding ports through the SSH endpoint of an
exported resource, including automatic allocation and cleanup.

Signed-off-by: Alex Tercete <alex.tercete@arm.com>
Reviewed-by: Alex Tercete <alext.tercete@arm.com> # gatekeeper
Use `adb forward` and `adb reverse` for local and remote forwarding.
For `RemoteUSBADBDevice`, chain the ADB socket through the exporter SSH
connection.

Signed-off-by: Alex Tercete <alex.tercete@arm.com>
Reviewed-by: Alex Tercete <alext.tercete@arm.com> # gatekeeper
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.79646% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.1%. Comparing base (e4f22e7) to head (42287cb).
⚠️ Report is 16 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
labgrid/util/proxy.py 30.4% 16 Missing ⚠️
labgrid/driver/adb.py 68.9% 9 Missing ⚠️
labgrid/remote/client.py 58.3% 5 Missing ⚠️
labgrid/util/ssh.py 76.9% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1961     +/-   ##
========================================
+ Coverage    60.8%   61.1%   +0.3%     
========================================
  Files         184     185      +1     
  Lines       15099   15186     +87     
========================================
+ Hits         9186    9290    +104     
+ Misses       5913    5896     -17     
Flag Coverage Δ
3.10 61.1% <70.7%> (+0.3%) ⬆️
3.11 61.1% <70.7%> (+0.3%) ⬆️
3.12 61.1% <70.7%> (+0.3%) ⬆️
3.13 61.1% <70.7%> (+0.3%) ⬆️
3.14 61.1% <70.7%> (+0.3%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@Emantor
Emantor self-requested a review August 25, 2026 11:42
Select a configured port forwarding driver while retaining implicit SSH
creation as a compatibility fallback. Add a client regression test using
ADB.

Signed-off-by: Alex Tercete <alex.tercete@arm.com>
Reviewed-by: Alex Tercete <alext.tercete@arm.com> # gatekeeper
Comment thread labgrid/util/ssh.py
self._run_socket_command("cancel", [forward])
raise

self._r_forwards[remote_port, local_port, remote_bind] = forward

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a comment why we need to store the original forward string.

def forward_remote_port(self, remoteport, localport):
def forward_local_port(self, remoteport, localport=None):
warnings.warn(
"SSHDriver.forward_local_port() is deprecated, use local_forward() instead",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to deprecate the existing functions, instead of just making them explicit in the new Protocol?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted the protocol to support auto allocation when port 0 is requested, and make that the default. But the parameters in the existing SSHDriver methods didn't allow for that without breaking backwards compatibility.

I considered keeping the old method names and inspecting the parameters to make the method satisfy both the new protocol and the old signature, but I figured that was unnecessary complexity for what's probably niche functionality. So deprecation seemed the best route.

Happy to change the approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants