* [PATCH 2/3] netconsole: add support for binding to specific IP
2026-05-03 8:30 [PATCH 1/3] netconsole: terminate netcat background process on exit Ahmad Fatoum
@ 2026-05-03 8:30 ` Ahmad Fatoum
2026-05-03 8:30 ` [PATCH 3/3] test: py: implement --port-forward option Ahmad Fatoum
2026-05-07 10:38 ` [PATCH 1/3] netconsole: terminate netcat background process on exit Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2026-05-03 8:30 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
If scripts/netconsole is already running when QEMU starts up, the host
forwarding rule may break QEMU startup:
qemu-system-aarch64: -nic user,id=net0,hostfwd=udp:127.0.0.2:6666-:6666,\
tftp=/src/build/barebox/imx8/testfs:
Could not set up host forwarding rule 'udp:127.0.0.2:6666-:6666'
This can be worked around by having QEMU bind to a different loopback
address than what's netcat is going to use.
Add a -s option to control this.
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
scripts/netconsole | 33 ++++++++++++++++++++++++---------
1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/scripts/netconsole b/scripts/netconsole
index 7ab81a3a9191..1945de7f9381 100755
--- a/scripts/netconsole
+++ b/scripts/netconsole
@@ -2,9 +2,14 @@
usage() {
(
- echo "Usage: $0 <board IP> [board port]"
+ echo "Usage: $0 [-sh] <board IP> [board port]"
echo ""
echo "If port is not specified, '6666' will be used"
+ echo ""
+ echo "Options:"
+ echo " -s SADDR Bind to SADDR on host side"
+ echo " -h Show this help text and exit"
+
[ -z "$*" ] && exit 0
echo ""
echo "ERROR: $*"
@@ -13,16 +18,26 @@ usage() {
exit $?
}
-while [ -n "$1" ] ; do
- case $1 in
- -h|--help) usage;;
- --) break;;
- -*) usage "Invalid option $1";;
- *) break;;
+if [ "$1" = "--help" ]; then
+ usage
+fi
+
+while getopts ":hs:" opt; do
+ case "$opt" in
+ h) usage ;;
+ s) saddr="$OPTARG" ;;
+ \?) usage "Invalid option -$OPTARG" ;;
+ :) usage "Option -$OPTARG requires an argument" ;;
esac
- shift
done
+shift $((OPTIND - 1))
+
+# handle end-of-options marker
+if [ "$1" = "--" ]; then
+ shift
+fi
+
ip=$1
port=${2:-6666}
@@ -50,7 +65,7 @@ echo "NOTE: the interrupt signal (normally ^C) has been remapped to ^T"
stty -icanon -echo intr ^T
set -m
(
- while ${nc} -u -l -p ${port} < /dev/null ; do
+ while ${nc} ${saddr:+-s "$saddr"} -u -l -p ${port} < /dev/null ; do
:
done
) &
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 3/3] test: py: implement --port-forward option
2026-05-03 8:30 [PATCH 1/3] netconsole: terminate netcat background process on exit Ahmad Fatoum
2026-05-03 8:30 ` [PATCH 2/3] netconsole: add support for binding to specific IP Ahmad Fatoum
@ 2026-05-03 8:30 ` Ahmad Fatoum
2026-05-07 10:38 ` [PATCH 1/3] netconsole: terminate netcat background process on exit Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2026-05-03 8:30 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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@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
^ permalink raw reply [flat|nested] 4+ messages in thread