Skip to content

Commit f9697e6

Browse files
aparcarBastian-Krause
authored andcommitted
driver/qemudriver: add support for netdev option in add_port_forward
In case there are more `user` network interfaces defined, allow to specific the netdev. If omitted, an empty string is passed and the behaviour is like before. This is useful for testing OpenWrt inside a QEMU instance, since OpenWrt expects by default a LAN interface on eth0 and a WAN interface (with uplink) on eth1. Previously the port forward would always be added to eth1, which doesn't support SSH due to firewall policies. By adding the netdev to the forward function, the SSHDriver works as expected. Signed-off-by: Paul Spooren <mail@aparcar.org> [bst: rebased, prevent double space in QMP command, adjust commit message to fit labgrid's style] Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent b327f81 commit f9697e6

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ New Features in 25.1
88
instead of a line feed (``\n``). barebox v2025.03.0 onwards handles
99
CTRL+D specially to halt autoboot countdown without running interactive
1010
hooks like bringing up network interfaces automatically.
11-
11+
- The `QEMUDriver` now supports a ``netdev`` argument which can be added to
12+
``add_port_forward()`` and ``remove_port_forward()`` in case there is more
13+
than one network interface defined.
1214
- Guermok HDMI to USB 3.0 capture dongle supported
1315

1416
Breaking changes in 25.1

labgrid/driver/qemudriver.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,25 @@ def monitor_command(self, command, arguments={}):
315315
"Can't use monitor command on non-running target")
316316
return self.qmp.execute(command, arguments)
317317

318-
def _add_port_forward(self, proto, local_address, local_port, remote_address, remote_port):
318+
def _add_port_forward(self, proto, local_address, local_port, remote_address, remote_port, netdev):
319+
command = ("hostfwd_add", netdev, f"{proto}:{local_address}:{local_port}-{remote_address}:{remote_port}")
320+
command = filter(None, command)
319321
self.monitor_command(
320322
"human-monitor-command",
321-
{"command-line": f"hostfwd_add {proto}:{local_address}:{local_port}-{remote_address}:{remote_port}"},
323+
{"command-line": " ".join(command)},
322324
)
323325

324-
def add_port_forward(self, proto, local_address, local_port, remote_address, remote_port):
325-
self._add_port_forward(proto, local_address, local_port, remote_address, remote_port)
326-
self._forwarded_ports[(proto, local_address, local_port)] = (proto, local_address, local_port, remote_address, remote_port)
326+
def add_port_forward(self, proto, local_address, local_port, remote_address, remote_port, netdev=""):
327+
self._add_port_forward(proto, local_address, local_port, remote_address, remote_port, netdev)
328+
self._forwarded_ports[(proto, local_address, local_port, netdev)] = (proto, local_address, local_port, remote_address, remote_port, netdev)
327329

328-
def remove_port_forward(self, proto, local_address, local_port):
329-
del self._forwarded_ports[(proto, local_address, local_port)]
330+
def remove_port_forward(self, proto, local_address, local_port, netdev=""):
331+
del self._forwarded_ports[(proto, local_address, local_port, netdev)]
332+
command = ("hostfwd_remove", netdev, f"{proto}:{local_address}:{local_port}")
333+
command = filter(None, command)
330334
self.monitor_command(
331335
"human-monitor-command",
332-
{"command-line": f"hostfwd_remove {proto}:{local_address}:{local_port}"},
336+
{"command-line": " ".join(command)},
333337
)
334338

335339
def _read(self, size=1, timeout=10, max_size=None):

tests/test_qemudriver.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,25 @@ def test_qemu_read_write(qemu_target, qemu_driver, qemu_mock, qemu_version_mock)
9090
qemu_driver.write(b'abc')
9191

9292
qemu_target.deactivate(qemu_driver)
93+
94+
def test_qemu_port_forwarding(qemu_target, qemu_driver, qemu_mock, qemu_version_mock):
95+
qemu_target.activate(qemu_driver)
96+
97+
qemu_driver.on()
98+
qemu_driver.add_port_forward('tcp', '127.0.0.1', 8080, '127.0.0.1', 80)
99+
assert ('tcp', '127.0.0.1', 8080, '') in qemu_driver._forwarded_ports.keys()
100+
qemu_driver.remove_port_forward('tcp', '127.0.0.1', 8080)
101+
assert qemu_driver._forwarded_ports == {}
102+
103+
qemu_target.deactivate(qemu_driver)
104+
105+
def test_qemu_port_forwarding_with_netdev(qemu_target, qemu_driver, qemu_mock, qemu_version_mock):
106+
qemu_target.activate(qemu_driver)
107+
108+
qemu_driver.on()
109+
qemu_driver.add_port_forward('tcp', '127.0.0.1', 8080, '127.0.0.1', 80, netdev='netdev0')
110+
assert ('tcp', '127.0.0.1', 8080, 'netdev0') in qemu_driver._forwarded_ports.keys()
111+
qemu_driver.remove_port_forward('tcp', '127.0.0.1', 8080, netdev='netdev0')
112+
assert qemu_driver._forwarded_ports == {}
113+
114+
qemu_target.deactivate(qemu_driver)

0 commit comments

Comments
 (0)