[PATCH 3/3] test: py: implement --port-forward option

Ahmad Fatoum a.fatoum at barebox.org
Sun May 3 01:30:06 PDT 2026


Unsolicited host-to-guest UDP requires an explicit port forward.
As we already have a -nic option, the easiest way is to have a
barebox-specific option that's expanded to the correct QEMU
incantations, so implement it.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 Documentation/boards/emulated.rst | 25 +++++++++++++++++++++++++
 Documentation/user/networking.rst |  2 ++
 conftest.py                       | 13 ++++++++++++-
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/Documentation/boards/emulated.rst b/Documentation/boards/emulated.rst
index e3f7f78ab258..0d6dd85dc759 100644
--- a/Documentation/boards/emulated.rst
+++ b/Documentation/boards/emulated.rst
@@ -34,3 +34,28 @@ Emulated targets can be started interactively with ``pytest --interactive``::
 The test suite can be run by omitting the ``--interactive``.
 For more information, see the :ref:`labgrid` section in the
 :ref:`contributing` guide.
+
+Netconsole over QEMU user networking
+------------------------------------
+
+barebox' UDP-based :ref:`network console <network_console>` can also
+be used in combination with QEMU. With user-mode networking (SLIRP),
+guest-to-host UDP works via NAT out of the box,
+but unsolicited host-to-guest UDP requires an explicit port forward::
+
+  pytest --lg-env test/arm/multi_v8_defconfig.yaml --interactive \
+    --env nv/dev.netconsole.ip=10.0.2.2                          \
+    --env nv/dev.netconsole.port=6666                            \
+    --env init/netconsole="ifup -a1; netconsole.active=ioe"      \
+    --port-forward=6666
+
+This will point netconsole at the SLIRP gateway (``10.0.2.2`` is the host
+as seen from the guest) and bring up the interface::
+
+  netconsole: netconsole initialized with 10.0.2.2:6666
+
+The ``i`` flag in ``netconsole.active`` is required for input; without it
+only output reaches the host. On the host, you can then interact with
+the netconsole via::
+
+  scripts/netconsole -s 127.0.0.1 127.0.0.2 6666
diff --git a/Documentation/user/networking.rst b/Documentation/user/networking.rst
index d1db2768ad20..99ab4d8a3fda 100644
--- a/Documentation/user/networking.rst
+++ b/Documentation/user/networking.rst
@@ -173,6 +173,8 @@ variables:
   ``-o port=${global.nfs.port},mountport=${global.nfs.port}`` as argument
   to the :ref:`mount command <command_mount>`.
 
+.. _network_console:
+
 Network console
 ---------------
 
diff --git a/conftest.py b/conftest.py
index 72d2df792e6d..6f76586e010b 100644
--- a/conftest.py
+++ b/conftest.py
@@ -144,6 +144,8 @@ def pytest_addoption(parser):
                      help=('Pass all remaining options to QEMU as is'))
     parser.addoption('--bootarg', action='append', dest='bootarg', default=[],
                      help=('Pass boot arguments to barebox for debugging purposes'))
+    parser.addoption('--port-forward', metavar="PORT", action='append', dest='qemu_port', default=[],
+                     help=('Forward incoming TCP or UDP connections on specified PORT'))
 
 
 @pytest.fixture(scope="session")
@@ -258,12 +260,21 @@ def strategy(request, target, pytestconfig):  # noqa: max-complexity=30
     for arg in pytestconfig.option.qemu_arg:
         strategy.append_qemu_args(arg)
 
+    qemu_nic = "user,id=net0"
+
+    for port in pytestconfig.option.qemu_port:
+        qemu_nic += f",hostfwd=udp:127.0.0.2:{port}-:{port}"
+        qemu_nic += f",hostfwd=tcp:127.0.0.2:{port}-:{port}"
+
     if "testfs" in features:
         if not any(fs and fs[0] == "testfs" for fs in pytestconfig.option.qemu_fs):
             testfs_path = os.path.join(os.environ["LG_BUILDDIR"], "testfs")
             pytestconfig.option.qemu_fs.append(["testfs", testfs_path])
             os.makedirs(testfs_path, exist_ok=True)
-            strategy.append_qemu_args("-nic", f"user,id=net0,tftp={testfs_path}")
+            qemu_nic += f",tftp={testfs_path}"
+
+    if "qemu" in features:
+        strategy.append_qemu_args("-nic", qemu_nic)
 
     for i, fs in enumerate(pytestconfig.option.qemu_fs):
         if virtio:
-- 
2.47.3




More information about the barebox mailing list